2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2016 Intel Corporation
8 #include "i915_gem_object.h"
9 #include "i915_scatterlist.h"
10 #include "i915_gem_lmem.h"
11 #include "i915_gem_mman.h"
13 #include "gt/intel_gt.h"
15 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
16 struct sg_table *pages,
17 unsigned int sg_page_sizes)
19 struct drm_i915_private *i915 = to_i915(obj->base.dev);
20 unsigned long supported = INTEL_INFO(i915)->page_sizes;
24 assert_object_held_shared(obj);
26 if (i915_gem_object_is_volatile(obj))
27 obj->mm.madv = I915_MADV_DONTNEED;
29 /* Make the pages coherent with the GPU (flushing any swapin). */
30 if (obj->cache_dirty) {
31 WARN_ON_ONCE(IS_DGFX(i915));
32 obj->write_domain = 0;
33 if (i915_gem_object_has_struct_page(obj))
34 drm_clflush_sg(pages);
35 obj->cache_dirty = false;
38 obj->mm.get_page.sg_pos = pages->sgl;
39 obj->mm.get_page.sg_idx = 0;
40 obj->mm.get_dma_page.sg_pos = pages->sgl;
41 obj->mm.get_dma_page.sg_idx = 0;
43 obj->mm.pages = pages;
45 GEM_BUG_ON(!sg_page_sizes);
46 obj->mm.page_sizes.phys = sg_page_sizes;
49 * Calculate the supported page-sizes which fit into the given
50 * sg_page_sizes. This will give us the page-sizes which we may be able
51 * to use opportunistically when later inserting into the GTT. For
52 * example if phys=2G, then in theory we should be able to use 1G, 2M,
53 * 64K or 4K pages, although in practice this will depend on a number of
56 obj->mm.page_sizes.sg = 0;
57 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
58 if (obj->mm.page_sizes.phys & ~0u << i)
59 obj->mm.page_sizes.sg |= BIT(i);
61 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
63 shrinkable = i915_gem_object_is_shrinkable(obj);
65 if (i915_gem_object_is_tiled(obj) &&
66 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
67 GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
68 i915_gem_object_set_tiling_quirk(obj);
69 GEM_BUG_ON(!list_empty(&obj->mm.link));
70 atomic_inc(&obj->mm.shrink_pin);
74 if (shrinkable && !i915_gem_object_has_self_managed_shrink_list(obj)) {
75 struct list_head *list;
78 assert_object_held(obj);
79 spin_lock_irqsave(&i915->mm.obj_lock, flags);
81 i915->mm.shrink_count++;
82 i915->mm.shrink_memory += obj->base.size;
84 if (obj->mm.madv != I915_MADV_WILLNEED)
85 list = &i915->mm.purge_list;
87 list = &i915->mm.shrink_list;
88 list_add_tail(&obj->mm.link, list);
90 atomic_set(&obj->mm.shrink_pin, 0);
91 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
95 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
97 struct drm_i915_private *i915 = to_i915(obj->base.dev);
100 assert_object_held_shared(obj);
102 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
104 "Attempting to obtain a purgeable object\n");
108 err = obj->ops->get_pages(obj);
109 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
114 /* Ensure that the associated pages are gathered from the backing storage
115 * and pinned into our object. i915_gem_object_pin_pages() may be called
116 * multiple times before they are released by a single call to
117 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
118 * either as a result of memory pressure (reaping pages under the shrinker)
119 * or as the object is itself released.
121 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
125 assert_object_held(obj);
127 assert_object_held_shared(obj);
129 if (unlikely(!i915_gem_object_has_pages(obj))) {
130 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
132 err = ____i915_gem_object_get_pages(obj);
136 smp_mb__before_atomic();
138 atomic_inc(&obj->mm.pages_pin_count);
143 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj)
145 struct i915_gem_ww_ctx ww;
148 i915_gem_ww_ctx_init(&ww, true);
150 err = i915_gem_object_lock(obj, &ww);
152 err = i915_gem_object_pin_pages(obj);
154 if (err == -EDEADLK) {
155 err = i915_gem_ww_ctx_backoff(&ww);
159 i915_gem_ww_ctx_fini(&ww);
163 /* Immediately discard the backing storage */
164 int i915_gem_object_truncate(struct drm_i915_gem_object *obj)
166 if (obj->ops->truncate)
167 return obj->ops->truncate(obj);
172 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
174 struct radix_tree_iter iter;
178 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
179 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
180 radix_tree_for_each_slot(slot, &obj->mm.get_dma_page.radix, &iter, 0)
181 radix_tree_delete(&obj->mm.get_dma_page.radix, iter.index);
185 static void unmap_object(struct drm_i915_gem_object *obj, void *ptr)
187 if (is_vmalloc_addr(ptr))
192 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
194 struct sg_table *pages;
196 assert_object_held_shared(obj);
198 pages = fetch_and_zero(&obj->mm.pages);
199 if (IS_ERR_OR_NULL(pages))
202 if (i915_gem_object_is_volatile(obj))
203 obj->mm.madv = I915_MADV_WILLNEED;
205 if (!i915_gem_object_has_self_managed_shrink_list(obj))
206 i915_gem_object_make_unshrinkable(obj);
208 if (obj->mm.mapping) {
209 unmap_object(obj, page_mask_bits(obj->mm.mapping));
210 obj->mm.mapping = NULL;
213 __i915_gem_object_reset_page_iter(obj);
214 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
216 if (test_and_clear_bit(I915_BO_WAS_BOUND_BIT, &obj->flags)) {
217 struct drm_i915_private *i915 = to_i915(obj->base.dev);
218 intel_wakeref_t wakeref;
220 with_intel_runtime_pm_if_active(&i915->runtime_pm, wakeref)
221 intel_gt_invalidate_tlbs(to_gt(i915));
227 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
229 struct sg_table *pages;
231 if (i915_gem_object_has_pinned_pages(obj))
234 /* May be called by shrinker from within get_pages() (on another bo) */
235 assert_object_held_shared(obj);
237 i915_gem_object_release_mmap_offset(obj);
240 * ->put_pages might need to allocate memory for the bit17 swizzle
241 * array, hence protect them from being reaped by removing them from gtt
244 pages = __i915_gem_object_unset_pages(obj);
247 * XXX Temporary hijinx to avoid updating all backends to handle
248 * NULL pages. In the future, when we have more asynchronous
249 * get_pages backends we should be better able to handle the
250 * cancellation of the async task in a more uniform manner.
252 if (!IS_ERR_OR_NULL(pages))
253 obj->ops->put_pages(obj, pages);
258 /* The 'mapping' part of i915_gem_object_pin_map() below */
259 static void *i915_gem_object_map_page(struct drm_i915_gem_object *obj,
260 enum i915_map_type type)
262 unsigned long n_pages = obj->base.size >> PAGE_SHIFT, i;
263 struct page *stack[32], **pages = stack, *page;
264 struct sgt_iter iter;
271 fallthrough; /* to use PAGE_KERNEL anyway */
274 * On 32b, highmem using a finite set of indirect PTE (i.e.
275 * vmap) to provide virtual mappings of the high pages.
276 * As these are finite, map_new_virtual() must wait for some
277 * other kmap() to finish when it runs out. If we map a large
278 * number of objects, there is no method for it to tell us
279 * to release the mappings, and we deadlock.
281 * However, if we make an explicit vmap of the page, that
282 * uses a larger vmalloc arena, and also has the ability
283 * to tell us to release unwanted mappings. Most importantly,
284 * it will fail and propagate an error instead of waiting
287 * So if the page is beyond the 32b boundary, make an explicit
290 if (n_pages == 1 && !PageHighMem(sg_page(obj->mm.pages->sgl)))
291 return page_address(sg_page(obj->mm.pages->sgl));
292 pgprot = PAGE_KERNEL;
295 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
299 if (n_pages > ARRAY_SIZE(stack)) {
300 /* Too big for stack -- allocate temporary array instead */
301 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
303 return ERR_PTR(-ENOMEM);
307 for_each_sgt_page(page, iter, obj->mm.pages)
309 vaddr = vmap(pages, n_pages, 0, pgprot);
313 return vaddr ?: ERR_PTR(-ENOMEM);
316 static void *i915_gem_object_map_pfn(struct drm_i915_gem_object *obj,
317 enum i915_map_type type)
319 resource_size_t iomap = obj->mm.region->iomap.base -
320 obj->mm.region->region.start;
321 unsigned long n_pfn = obj->base.size >> PAGE_SHIFT;
322 unsigned long stack[32], *pfns = stack, i;
323 struct sgt_iter iter;
327 GEM_BUG_ON(type != I915_MAP_WC);
329 if (n_pfn > ARRAY_SIZE(stack)) {
330 /* Too big for stack -- allocate temporary array instead */
331 pfns = kvmalloc_array(n_pfn, sizeof(*pfns), GFP_KERNEL);
333 return ERR_PTR(-ENOMEM);
337 for_each_sgt_daddr(addr, iter, obj->mm.pages)
338 pfns[i++] = (iomap + addr) >> PAGE_SHIFT;
339 vaddr = vmap_pfn(pfns, n_pfn, pgprot_writecombine(PAGE_KERNEL_IO));
343 return vaddr ?: ERR_PTR(-ENOMEM);
346 /* get, pin, and map the pages of the object into kernel space */
347 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
348 enum i915_map_type type)
350 enum i915_map_type has_type;
355 if (!i915_gem_object_has_struct_page(obj) &&
356 !i915_gem_object_has_iomem(obj))
357 return ERR_PTR(-ENXIO);
359 assert_object_held(obj);
361 pinned = !(type & I915_MAP_OVERRIDE);
362 type &= ~I915_MAP_OVERRIDE;
364 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
365 if (unlikely(!i915_gem_object_has_pages(obj))) {
366 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
368 err = ____i915_gem_object_get_pages(obj);
372 smp_mb__before_atomic();
374 atomic_inc(&obj->mm.pages_pin_count);
377 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
380 * For discrete our CPU mappings needs to be consistent in order to
381 * function correctly on !x86. When mapping things through TTM, we use
382 * the same rules to determine the caching type.
384 * The caching rules, starting from DG1:
386 * - If the object can be placed in device local-memory, then the
387 * pages should be allocated and mapped as write-combined only.
389 * - Everything else is always allocated and mapped as write-back,
390 * with the guarantee that everything is also coherent with the
393 * Internal users of lmem are already expected to get this right, so no
394 * fudging needed there.
396 if (i915_gem_object_placement_possible(obj, INTEL_MEMORY_LOCAL)) {
397 if (type != I915_MAP_WC && !obj->mm.n_placements) {
398 ptr = ERR_PTR(-ENODEV);
403 } else if (IS_DGFX(to_i915(obj->base.dev))) {
407 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
408 if (ptr && has_type != type) {
410 ptr = ERR_PTR(-EBUSY);
414 unmap_object(obj, ptr);
416 ptr = obj->mm.mapping = NULL;
420 err = i915_gem_object_wait_moving_fence(obj, true);
426 if (GEM_WARN_ON(type == I915_MAP_WC && !pat_enabled()))
427 ptr = ERR_PTR(-ENODEV);
428 else if (i915_gem_object_has_struct_page(obj))
429 ptr = i915_gem_object_map_page(obj, type);
431 ptr = i915_gem_object_map_pfn(obj, type);
435 obj->mm.mapping = page_pack_bits(ptr, type);
441 atomic_dec(&obj->mm.pages_pin_count);
445 void *i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
446 enum i915_map_type type)
450 i915_gem_object_lock(obj, NULL);
451 ret = i915_gem_object_pin_map(obj, type);
452 i915_gem_object_unlock(obj);
457 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
458 unsigned long offset,
461 enum i915_map_type has_type;
464 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
465 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
466 offset, size, obj->base.size));
468 wmb(); /* let all previous writes be visible to coherent partners */
469 obj->mm.dirty = true;
471 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
474 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
475 if (has_type == I915_MAP_WC)
478 drm_clflush_virt_range(ptr + offset, size);
479 if (size == obj->base.size) {
480 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
481 obj->cache_dirty = false;
485 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj)
487 GEM_BUG_ON(!obj->mm.mapping);
490 * We allow removing the mapping from underneath pinned pages!
492 * Furthermore, since this is an unsafe operation reserved only
493 * for construction time manipulation, we ignore locking prudence.
495 unmap_object(obj, page_mask_bits(fetch_and_zero(&obj->mm.mapping)));
497 i915_gem_object_unpin_map(obj);
501 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
502 struct i915_gem_object_page_iter *iter,
504 unsigned int *offset,
507 struct scatterlist *sg;
508 unsigned int idx, count;
511 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
512 if (!i915_gem_object_has_pinned_pages(obj))
513 assert_object_held(obj);
515 /* As we iterate forward through the sg, we record each entry in a
516 * radixtree for quick repeated (backwards) lookups. If we have seen
517 * this index previously, we will have an entry for it.
519 * Initial lookup is O(N), but this is amortized to O(1) for
520 * sequential page access (where each new request is consecutive
521 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
522 * i.e. O(1) with a large constant!
524 if (n < READ_ONCE(iter->sg_idx))
527 mutex_lock(&iter->lock);
529 /* We prefer to reuse the last sg so that repeated lookup of this
530 * (or the subsequent) sg are fast - comparing against the last
531 * sg is faster than going through the radixtree.
536 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
538 while (idx + count <= n) {
543 /* If we cannot allocate and insert this entry, or the
544 * individual pages from this range, cancel updating the
545 * sg_idx so that on this lookup we are forced to linearly
546 * scan onwards, but on future lookups we will try the
547 * insertion again (in which case we need to be careful of
548 * the error return reporting that we have already inserted
551 ret = radix_tree_insert(&iter->radix, idx, sg);
552 if (ret && ret != -EEXIST)
555 entry = xa_mk_value(idx);
556 for (i = 1; i < count; i++) {
557 ret = radix_tree_insert(&iter->radix, idx + i, entry);
558 if (ret && ret != -EEXIST)
563 sg = ____sg_next(sg);
564 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
571 mutex_unlock(&iter->lock);
573 if (unlikely(n < idx)) /* insertion completed by another thread */
576 /* In case we failed to insert the entry into the radixtree, we need
577 * to look beyond the current sg.
579 while (idx + count <= n) {
581 sg = ____sg_next(sg);
582 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
591 sg = radix_tree_lookup(&iter->radix, n);
594 /* If this index is in the middle of multi-page sg entry,
595 * the radix tree will contain a value entry that points
596 * to the start of that range. We will return the pointer to
597 * the base page and the offset of this page within the
601 if (unlikely(xa_is_value(sg))) {
602 unsigned long base = xa_to_value(sg);
604 sg = radix_tree_lookup(&iter->radix, base);
616 i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
618 struct scatterlist *sg;
621 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
623 sg = i915_gem_object_get_sg(obj, n, &offset);
624 return nth_page(sg_page(sg), offset);
627 /* Like i915_gem_object_get_page(), but mark the returned page dirty */
629 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
634 page = i915_gem_object_get_page(obj, n);
636 set_page_dirty(page);
642 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
646 struct scatterlist *sg;
649 sg = i915_gem_object_get_sg_dma(obj, n, &offset);
652 *len = sg_dma_len(sg) - (offset << PAGE_SHIFT);
654 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
658 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
661 return i915_gem_object_get_dma_address_len(obj, n, NULL);