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 obj->write_domain = 0;
32 if (i915_gem_object_has_struct_page(obj))
33 drm_clflush_sg(pages);
34 obj->cache_dirty = false;
37 obj->mm.get_page.sg_pos = pages->sgl;
38 obj->mm.get_page.sg_idx = 0;
39 obj->mm.get_dma_page.sg_pos = pages->sgl;
40 obj->mm.get_dma_page.sg_idx = 0;
42 obj->mm.pages = pages;
44 GEM_BUG_ON(!sg_page_sizes);
45 obj->mm.page_sizes.phys = sg_page_sizes;
48 * Calculate the supported page-sizes which fit into the given
49 * sg_page_sizes. This will give us the page-sizes which we may be able
50 * to use opportunistically when later inserting into the GTT. For
51 * example if phys=2G, then in theory we should be able to use 1G, 2M,
52 * 64K or 4K pages, although in practice this will depend on a number of
55 obj->mm.page_sizes.sg = 0;
56 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
57 if (obj->mm.page_sizes.phys & ~0u << i)
58 obj->mm.page_sizes.sg |= BIT(i);
60 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
62 shrinkable = i915_gem_object_is_shrinkable(obj);
64 if (i915_gem_object_is_tiled(obj) &&
65 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
66 GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
67 i915_gem_object_set_tiling_quirk(obj);
68 GEM_BUG_ON(!list_empty(&obj->mm.link));
69 atomic_inc(&obj->mm.shrink_pin);
74 struct list_head *list;
77 assert_object_held(obj);
78 spin_lock_irqsave(&i915->mm.obj_lock, flags);
80 i915->mm.shrink_count++;
81 i915->mm.shrink_memory += obj->base.size;
83 if (obj->mm.madv != I915_MADV_WILLNEED)
84 list = &i915->mm.purge_list;
86 list = &i915->mm.shrink_list;
87 list_add_tail(&obj->mm.link, list);
89 atomic_set(&obj->mm.shrink_pin, 0);
90 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
94 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
96 struct drm_i915_private *i915 = to_i915(obj->base.dev);
99 assert_object_held_shared(obj);
101 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
103 "Attempting to obtain a purgeable object\n");
107 err = obj->ops->get_pages(obj);
108 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
113 /* Ensure that the associated pages are gathered from the backing storage
114 * and pinned into our object. i915_gem_object_pin_pages() may be called
115 * multiple times before they are released by a single call to
116 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
117 * either as a result of memory pressure (reaping pages under the shrinker)
118 * or as the object is itself released.
120 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
124 assert_object_held(obj);
126 assert_object_held_shared(obj);
128 if (unlikely(!i915_gem_object_has_pages(obj))) {
129 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
131 err = ____i915_gem_object_get_pages(obj);
135 smp_mb__before_atomic();
137 atomic_inc(&obj->mm.pages_pin_count);
142 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj)
144 struct i915_gem_ww_ctx ww;
147 i915_gem_ww_ctx_init(&ww, true);
149 err = i915_gem_object_lock(obj, &ww);
151 err = i915_gem_object_pin_pages(obj);
153 if (err == -EDEADLK) {
154 err = i915_gem_ww_ctx_backoff(&ww);
158 i915_gem_ww_ctx_fini(&ww);
162 /* Immediately discard the backing storage */
163 void i915_gem_object_truncate(struct drm_i915_gem_object *obj)
165 drm_gem_free_mmap_offset(&obj->base);
166 if (obj->ops->truncate)
167 obj->ops->truncate(obj);
170 /* Try to discard unwanted pages */
171 void i915_gem_object_writeback(struct drm_i915_gem_object *obj)
173 assert_object_held_shared(obj);
174 GEM_BUG_ON(i915_gem_object_has_pages(obj));
176 if (obj->ops->writeback)
177 obj->ops->writeback(obj);
180 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
182 struct radix_tree_iter iter;
186 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
187 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
188 radix_tree_for_each_slot(slot, &obj->mm.get_dma_page.radix, &iter, 0)
189 radix_tree_delete(&obj->mm.get_dma_page.radix, iter.index);
193 static void unmap_object(struct drm_i915_gem_object *obj, void *ptr)
195 if (is_vmalloc_addr(ptr))
200 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
202 struct sg_table *pages;
204 assert_object_held_shared(obj);
206 pages = fetch_and_zero(&obj->mm.pages);
207 if (IS_ERR_OR_NULL(pages))
210 if (i915_gem_object_is_volatile(obj))
211 obj->mm.madv = I915_MADV_WILLNEED;
213 i915_gem_object_make_unshrinkable(obj);
215 if (obj->mm.mapping) {
216 unmap_object(obj, page_mask_bits(obj->mm.mapping));
217 obj->mm.mapping = NULL;
220 __i915_gem_object_reset_page_iter(obj);
221 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
223 if (test_and_clear_bit(I915_BO_WAS_BOUND_BIT, &obj->flags)) {
224 struct drm_i915_private *i915 = to_i915(obj->base.dev);
225 intel_wakeref_t wakeref;
227 with_intel_runtime_pm_if_active(&i915->runtime_pm, wakeref)
228 intel_gt_invalidate_tlbs(&i915->gt);
234 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
236 struct sg_table *pages;
238 if (i915_gem_object_has_pinned_pages(obj))
241 /* May be called by shrinker from within get_pages() (on another bo) */
242 assert_object_held_shared(obj);
244 i915_gem_object_release_mmap_offset(obj);
247 * ->put_pages might need to allocate memory for the bit17 swizzle
248 * array, hence protect them from being reaped by removing them from gtt
251 pages = __i915_gem_object_unset_pages(obj);
254 * XXX Temporary hijinx to avoid updating all backends to handle
255 * NULL pages. In the future, when we have more asynchronous
256 * get_pages backends we should be better able to handle the
257 * cancellation of the async task in a more uniform manner.
259 if (!IS_ERR_OR_NULL(pages))
260 obj->ops->put_pages(obj, pages);
265 /* The 'mapping' part of i915_gem_object_pin_map() below */
266 static void *i915_gem_object_map_page(struct drm_i915_gem_object *obj,
267 enum i915_map_type type)
269 unsigned long n_pages = obj->base.size >> PAGE_SHIFT, i;
270 struct page *stack[32], **pages = stack, *page;
271 struct sgt_iter iter;
278 fallthrough; /* to use PAGE_KERNEL anyway */
281 * On 32b, highmem using a finite set of indirect PTE (i.e.
282 * vmap) to provide virtual mappings of the high pages.
283 * As these are finite, map_new_virtual() must wait for some
284 * other kmap() to finish when it runs out. If we map a large
285 * number of objects, there is no method for it to tell us
286 * to release the mappings, and we deadlock.
288 * However, if we make an explicit vmap of the page, that
289 * uses a larger vmalloc arena, and also has the ability
290 * to tell us to release unwanted mappings. Most importantly,
291 * it will fail and propagate an error instead of waiting
294 * So if the page is beyond the 32b boundary, make an explicit
297 if (n_pages == 1 && !PageHighMem(sg_page(obj->mm.pages->sgl)))
298 return page_address(sg_page(obj->mm.pages->sgl));
299 pgprot = PAGE_KERNEL;
302 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
306 if (n_pages > ARRAY_SIZE(stack)) {
307 /* Too big for stack -- allocate temporary array instead */
308 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
310 return ERR_PTR(-ENOMEM);
314 for_each_sgt_page(page, iter, obj->mm.pages)
316 vaddr = vmap(pages, n_pages, 0, pgprot);
320 return vaddr ?: ERR_PTR(-ENOMEM);
323 static void *i915_gem_object_map_pfn(struct drm_i915_gem_object *obj,
324 enum i915_map_type type)
326 resource_size_t iomap = obj->mm.region->iomap.base -
327 obj->mm.region->region.start;
328 unsigned long n_pfn = obj->base.size >> PAGE_SHIFT;
329 unsigned long stack[32], *pfns = stack, i;
330 struct sgt_iter iter;
334 GEM_BUG_ON(type != I915_MAP_WC);
336 if (n_pfn > ARRAY_SIZE(stack)) {
337 /* Too big for stack -- allocate temporary array instead */
338 pfns = kvmalloc_array(n_pfn, sizeof(*pfns), GFP_KERNEL);
340 return ERR_PTR(-ENOMEM);
344 for_each_sgt_daddr(addr, iter, obj->mm.pages)
345 pfns[i++] = (iomap + addr) >> PAGE_SHIFT;
346 vaddr = vmap_pfn(pfns, n_pfn, pgprot_writecombine(PAGE_KERNEL_IO));
350 return vaddr ?: ERR_PTR(-ENOMEM);
353 /* get, pin, and map the pages of the object into kernel space */
354 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
355 enum i915_map_type type)
357 enum i915_map_type has_type;
362 if (!i915_gem_object_has_struct_page(obj) &&
363 !i915_gem_object_has_iomem(obj))
364 return ERR_PTR(-ENXIO);
366 assert_object_held(obj);
368 pinned = !(type & I915_MAP_OVERRIDE);
369 type &= ~I915_MAP_OVERRIDE;
371 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
372 if (unlikely(!i915_gem_object_has_pages(obj))) {
373 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
375 err = ____i915_gem_object_get_pages(obj);
379 smp_mb__before_atomic();
381 atomic_inc(&obj->mm.pages_pin_count);
384 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
387 * For discrete our CPU mappings needs to be consistent in order to
388 * function correctly on !x86. When mapping things through TTM, we use
389 * the same rules to determine the caching type.
391 * The caching rules, starting from DG1:
393 * - If the object can be placed in device local-memory, then the
394 * pages should be allocated and mapped as write-combined only.
396 * - Everything else is always allocated and mapped as write-back,
397 * with the guarantee that everything is also coherent with the
400 * Internal users of lmem are already expected to get this right, so no
401 * fudging needed there.
403 if (i915_gem_object_placement_possible(obj, INTEL_MEMORY_LOCAL)) {
404 if (type != I915_MAP_WC && !obj->mm.n_placements) {
405 ptr = ERR_PTR(-ENODEV);
410 } else if (IS_DGFX(to_i915(obj->base.dev))) {
414 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
415 if (ptr && has_type != type) {
417 ptr = ERR_PTR(-EBUSY);
421 unmap_object(obj, ptr);
423 ptr = obj->mm.mapping = NULL;
427 if (GEM_WARN_ON(type == I915_MAP_WC &&
428 !static_cpu_has(X86_FEATURE_PAT)))
429 ptr = ERR_PTR(-ENODEV);
430 else if (i915_gem_object_has_struct_page(obj))
431 ptr = i915_gem_object_map_page(obj, type);
433 ptr = i915_gem_object_map_pfn(obj, type);
437 obj->mm.mapping = page_pack_bits(ptr, type);
443 atomic_dec(&obj->mm.pages_pin_count);
447 void *i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
448 enum i915_map_type type)
452 i915_gem_object_lock(obj, NULL);
453 ret = i915_gem_object_pin_map(obj, type);
454 i915_gem_object_unlock(obj);
459 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
460 unsigned long offset,
463 enum i915_map_type has_type;
466 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
467 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
468 offset, size, obj->base.size));
470 wmb(); /* let all previous writes be visible to coherent partners */
471 obj->mm.dirty = true;
473 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
476 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
477 if (has_type == I915_MAP_WC)
480 drm_clflush_virt_range(ptr + offset, size);
481 if (size == obj->base.size) {
482 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
483 obj->cache_dirty = false;
487 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj)
489 GEM_BUG_ON(!obj->mm.mapping);
492 * We allow removing the mapping from underneath pinned pages!
494 * Furthermore, since this is an unsafe operation reserved only
495 * for construction time manipulation, we ignore locking prudence.
497 unmap_object(obj, page_mask_bits(fetch_and_zero(&obj->mm.mapping)));
499 i915_gem_object_unpin_map(obj);
503 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
504 struct i915_gem_object_page_iter *iter,
506 unsigned int *offset,
509 struct scatterlist *sg;
510 unsigned int idx, count;
513 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
514 if (!i915_gem_object_has_pinned_pages(obj))
515 assert_object_held(obj);
517 /* As we iterate forward through the sg, we record each entry in a
518 * radixtree for quick repeated (backwards) lookups. If we have seen
519 * this index previously, we will have an entry for it.
521 * Initial lookup is O(N), but this is amortized to O(1) for
522 * sequential page access (where each new request is consecutive
523 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
524 * i.e. O(1) with a large constant!
526 if (n < READ_ONCE(iter->sg_idx))
529 mutex_lock(&iter->lock);
531 /* We prefer to reuse the last sg so that repeated lookup of this
532 * (or the subsequent) sg are fast - comparing against the last
533 * sg is faster than going through the radixtree.
538 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
540 while (idx + count <= n) {
545 /* If we cannot allocate and insert this entry, or the
546 * individual pages from this range, cancel updating the
547 * sg_idx so that on this lookup we are forced to linearly
548 * scan onwards, but on future lookups we will try the
549 * insertion again (in which case we need to be careful of
550 * the error return reporting that we have already inserted
553 ret = radix_tree_insert(&iter->radix, idx, sg);
554 if (ret && ret != -EEXIST)
557 entry = xa_mk_value(idx);
558 for (i = 1; i < count; i++) {
559 ret = radix_tree_insert(&iter->radix, idx + i, entry);
560 if (ret && ret != -EEXIST)
565 sg = ____sg_next(sg);
566 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
573 mutex_unlock(&iter->lock);
575 if (unlikely(n < idx)) /* insertion completed by another thread */
578 /* In case we failed to insert the entry into the radixtree, we need
579 * to look beyond the current sg.
581 while (idx + count <= n) {
583 sg = ____sg_next(sg);
584 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
593 sg = radix_tree_lookup(&iter->radix, n);
596 /* If this index is in the middle of multi-page sg entry,
597 * the radix tree will contain a value entry that points
598 * to the start of that range. We will return the pointer to
599 * the base page and the offset of this page within the
603 if (unlikely(xa_is_value(sg))) {
604 unsigned long base = xa_to_value(sg);
606 sg = radix_tree_lookup(&iter->radix, base);
618 i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
620 struct scatterlist *sg;
623 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
625 sg = i915_gem_object_get_sg(obj, n, &offset);
626 return nth_page(sg_page(sg), offset);
629 /* Like i915_gem_object_get_page(), but mark the returned page dirty */
631 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
636 page = i915_gem_object_get_page(obj, n);
638 set_page_dirty(page);
644 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
648 struct scatterlist *sg;
651 sg = i915_gem_object_get_sg_dma(obj, n, &offset);
654 *len = sg_dma_len(sg) - (offset << PAGE_SHIFT);
656 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
660 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
663 return i915_gem_object_get_dma_address_len(obj, n, NULL);