2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2016 Intel Corporation
7 #include <drm/drm_cache.h>
10 #include "i915_gem_object.h"
11 #include "i915_scatterlist.h"
12 #include "i915_gem_lmem.h"
13 #include "i915_gem_mman.h"
15 #include "gt/intel_gt.h"
17 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
18 struct sg_table *pages,
19 unsigned int sg_page_sizes)
21 struct drm_i915_private *i915 = to_i915(obj->base.dev);
22 unsigned long supported = INTEL_INFO(i915)->page_sizes;
26 assert_object_held_shared(obj);
28 if (i915_gem_object_is_volatile(obj))
29 obj->mm.madv = I915_MADV_DONTNEED;
31 /* Make the pages coherent with the GPU (flushing any swapin). */
32 if (obj->cache_dirty) {
33 WARN_ON_ONCE(IS_DGFX(i915));
34 obj->write_domain = 0;
35 if (i915_gem_object_has_struct_page(obj))
36 drm_clflush_sg(pages);
37 obj->cache_dirty = false;
40 obj->mm.get_page.sg_pos = pages->sgl;
41 obj->mm.get_page.sg_idx = 0;
42 obj->mm.get_dma_page.sg_pos = pages->sgl;
43 obj->mm.get_dma_page.sg_idx = 0;
45 obj->mm.pages = pages;
47 GEM_BUG_ON(!sg_page_sizes);
48 obj->mm.page_sizes.phys = sg_page_sizes;
51 * Calculate the supported page-sizes which fit into the given
52 * sg_page_sizes. This will give us the page-sizes which we may be able
53 * to use opportunistically when later inserting into the GTT. For
54 * example if phys=2G, then in theory we should be able to use 1G, 2M,
55 * 64K or 4K pages, although in practice this will depend on a number of
58 obj->mm.page_sizes.sg = 0;
59 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
60 if (obj->mm.page_sizes.phys & ~0u << i)
61 obj->mm.page_sizes.sg |= BIT(i);
63 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
65 shrinkable = i915_gem_object_is_shrinkable(obj);
67 if (i915_gem_object_is_tiled(obj) &&
68 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
69 GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
70 i915_gem_object_set_tiling_quirk(obj);
71 GEM_BUG_ON(!list_empty(&obj->mm.link));
72 atomic_inc(&obj->mm.shrink_pin);
76 if (shrinkable && !i915_gem_object_has_self_managed_shrink_list(obj)) {
77 struct list_head *list;
80 assert_object_held(obj);
81 spin_lock_irqsave(&i915->mm.obj_lock, flags);
83 i915->mm.shrink_count++;
84 i915->mm.shrink_memory += obj->base.size;
86 if (obj->mm.madv != I915_MADV_WILLNEED)
87 list = &i915->mm.purge_list;
89 list = &i915->mm.shrink_list;
90 list_add_tail(&obj->mm.link, list);
92 atomic_set(&obj->mm.shrink_pin, 0);
93 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
97 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
99 struct drm_i915_private *i915 = to_i915(obj->base.dev);
102 assert_object_held_shared(obj);
104 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
106 "Attempting to obtain a purgeable object\n");
110 err = obj->ops->get_pages(obj);
111 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
116 /* Ensure that the associated pages are gathered from the backing storage
117 * and pinned into our object. i915_gem_object_pin_pages() may be called
118 * multiple times before they are released by a single call to
119 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
120 * either as a result of memory pressure (reaping pages under the shrinker)
121 * or as the object is itself released.
123 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
127 assert_object_held(obj);
129 assert_object_held_shared(obj);
131 if (unlikely(!i915_gem_object_has_pages(obj))) {
132 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
134 err = ____i915_gem_object_get_pages(obj);
138 smp_mb__before_atomic();
140 atomic_inc(&obj->mm.pages_pin_count);
145 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj)
147 struct i915_gem_ww_ctx ww;
150 i915_gem_ww_ctx_init(&ww, true);
152 err = i915_gem_object_lock(obj, &ww);
154 err = i915_gem_object_pin_pages(obj);
156 if (err == -EDEADLK) {
157 err = i915_gem_ww_ctx_backoff(&ww);
161 i915_gem_ww_ctx_fini(&ww);
165 /* Immediately discard the backing storage */
166 int i915_gem_object_truncate(struct drm_i915_gem_object *obj)
168 if (obj->ops->truncate)
169 return obj->ops->truncate(obj);
174 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
176 struct radix_tree_iter iter;
180 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
181 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
182 radix_tree_for_each_slot(slot, &obj->mm.get_dma_page.radix, &iter, 0)
183 radix_tree_delete(&obj->mm.get_dma_page.radix, iter.index);
187 static void unmap_object(struct drm_i915_gem_object *obj, void *ptr)
189 if (is_vmalloc_addr(ptr))
194 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
196 struct sg_table *pages;
198 assert_object_held_shared(obj);
200 pages = fetch_and_zero(&obj->mm.pages);
201 if (IS_ERR_OR_NULL(pages))
204 if (i915_gem_object_is_volatile(obj))
205 obj->mm.madv = I915_MADV_WILLNEED;
207 if (!i915_gem_object_has_self_managed_shrink_list(obj))
208 i915_gem_object_make_unshrinkable(obj);
210 if (obj->mm.mapping) {
211 unmap_object(obj, page_mask_bits(obj->mm.mapping));
212 obj->mm.mapping = NULL;
215 __i915_gem_object_reset_page_iter(obj);
216 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
218 if (test_and_clear_bit(I915_BO_WAS_BOUND_BIT, &obj->flags)) {
219 struct drm_i915_private *i915 = to_i915(obj->base.dev);
220 intel_wakeref_t wakeref;
222 with_intel_runtime_pm_if_active(&i915->runtime_pm, wakeref)
223 intel_gt_invalidate_tlbs(to_gt(i915));
229 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
231 struct sg_table *pages;
233 if (i915_gem_object_has_pinned_pages(obj))
236 /* May be called by shrinker from within get_pages() (on another bo) */
237 assert_object_held_shared(obj);
239 i915_gem_object_release_mmap_offset(obj);
242 * ->put_pages might need to allocate memory for the bit17 swizzle
243 * array, hence protect them from being reaped by removing them from gtt
246 pages = __i915_gem_object_unset_pages(obj);
249 * XXX Temporary hijinx to avoid updating all backends to handle
250 * NULL pages. In the future, when we have more asynchronous
251 * get_pages backends we should be better able to handle the
252 * cancellation of the async task in a more uniform manner.
254 if (!IS_ERR_OR_NULL(pages))
255 obj->ops->put_pages(obj, pages);
260 /* The 'mapping' part of i915_gem_object_pin_map() below */
261 static void *i915_gem_object_map_page(struct drm_i915_gem_object *obj,
262 enum i915_map_type type)
264 unsigned long n_pages = obj->base.size >> PAGE_SHIFT, i;
265 struct page *stack[32], **pages = stack, *page;
266 struct sgt_iter iter;
273 fallthrough; /* to use PAGE_KERNEL anyway */
276 * On 32b, highmem using a finite set of indirect PTE (i.e.
277 * vmap) to provide virtual mappings of the high pages.
278 * As these are finite, map_new_virtual() must wait for some
279 * other kmap() to finish when it runs out. If we map a large
280 * number of objects, there is no method for it to tell us
281 * to release the mappings, and we deadlock.
283 * However, if we make an explicit vmap of the page, that
284 * uses a larger vmalloc arena, and also has the ability
285 * to tell us to release unwanted mappings. Most importantly,
286 * it will fail and propagate an error instead of waiting
289 * So if the page is beyond the 32b boundary, make an explicit
292 if (n_pages == 1 && !PageHighMem(sg_page(obj->mm.pages->sgl)))
293 return page_address(sg_page(obj->mm.pages->sgl));
294 pgprot = PAGE_KERNEL;
297 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
301 if (n_pages > ARRAY_SIZE(stack)) {
302 /* Too big for stack -- allocate temporary array instead */
303 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
305 return ERR_PTR(-ENOMEM);
309 for_each_sgt_page(page, iter, obj->mm.pages)
311 vaddr = vmap(pages, n_pages, 0, pgprot);
315 return vaddr ?: ERR_PTR(-ENOMEM);
318 static void *i915_gem_object_map_pfn(struct drm_i915_gem_object *obj,
319 enum i915_map_type type)
321 resource_size_t iomap = obj->mm.region->iomap.base -
322 obj->mm.region->region.start;
323 unsigned long n_pfn = obj->base.size >> PAGE_SHIFT;
324 unsigned long stack[32], *pfns = stack, i;
325 struct sgt_iter iter;
329 GEM_BUG_ON(type != I915_MAP_WC);
331 if (n_pfn > ARRAY_SIZE(stack)) {
332 /* Too big for stack -- allocate temporary array instead */
333 pfns = kvmalloc_array(n_pfn, sizeof(*pfns), GFP_KERNEL);
335 return ERR_PTR(-ENOMEM);
339 for_each_sgt_daddr(addr, iter, obj->mm.pages)
340 pfns[i++] = (iomap + addr) >> PAGE_SHIFT;
341 vaddr = vmap_pfn(pfns, n_pfn, pgprot_writecombine(PAGE_KERNEL_IO));
345 return vaddr ?: ERR_PTR(-ENOMEM);
348 /* get, pin, and map the pages of the object into kernel space */
349 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
350 enum i915_map_type type)
352 enum i915_map_type has_type;
357 if (!i915_gem_object_has_struct_page(obj) &&
358 !i915_gem_object_has_iomem(obj))
359 return ERR_PTR(-ENXIO);
361 if (WARN_ON_ONCE(obj->flags & I915_BO_ALLOC_GPU_ONLY))
362 return ERR_PTR(-EINVAL);
364 assert_object_held(obj);
366 pinned = !(type & I915_MAP_OVERRIDE);
367 type &= ~I915_MAP_OVERRIDE;
369 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
370 if (unlikely(!i915_gem_object_has_pages(obj))) {
371 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
373 err = ____i915_gem_object_get_pages(obj);
377 smp_mb__before_atomic();
379 atomic_inc(&obj->mm.pages_pin_count);
382 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
385 * For discrete our CPU mappings needs to be consistent in order to
386 * function correctly on !x86. When mapping things through TTM, we use
387 * the same rules to determine the caching type.
389 * The caching rules, starting from DG1:
391 * - If the object can be placed in device local-memory, then the
392 * pages should be allocated and mapped as write-combined only.
394 * - Everything else is always allocated and mapped as write-back,
395 * with the guarantee that everything is also coherent with the
398 * Internal users of lmem are already expected to get this right, so no
399 * fudging needed there.
401 if (i915_gem_object_placement_possible(obj, INTEL_MEMORY_LOCAL)) {
402 if (type != I915_MAP_WC && !obj->mm.n_placements) {
403 ptr = ERR_PTR(-ENODEV);
408 } else if (IS_DGFX(to_i915(obj->base.dev))) {
412 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
413 if (ptr && has_type != type) {
415 ptr = ERR_PTR(-EBUSY);
419 unmap_object(obj, ptr);
421 ptr = obj->mm.mapping = NULL;
425 err = i915_gem_object_wait_moving_fence(obj, true);
431 if (GEM_WARN_ON(type == I915_MAP_WC && !pat_enabled()))
432 ptr = ERR_PTR(-ENODEV);
433 else if (i915_gem_object_has_struct_page(obj))
434 ptr = i915_gem_object_map_page(obj, type);
436 ptr = i915_gem_object_map_pfn(obj, type);
440 obj->mm.mapping = page_pack_bits(ptr, type);
446 atomic_dec(&obj->mm.pages_pin_count);
450 void *i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
451 enum i915_map_type type)
455 i915_gem_object_lock(obj, NULL);
456 ret = i915_gem_object_pin_map(obj, type);
457 i915_gem_object_unlock(obj);
462 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
463 unsigned long offset,
466 enum i915_map_type has_type;
469 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
470 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
471 offset, size, obj->base.size));
473 wmb(); /* let all previous writes be visible to coherent partners */
474 obj->mm.dirty = true;
476 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
479 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
480 if (has_type == I915_MAP_WC)
483 drm_clflush_virt_range(ptr + offset, size);
484 if (size == obj->base.size) {
485 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
486 obj->cache_dirty = false;
490 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj)
492 GEM_BUG_ON(!obj->mm.mapping);
495 * We allow removing the mapping from underneath pinned pages!
497 * Furthermore, since this is an unsafe operation reserved only
498 * for construction time manipulation, we ignore locking prudence.
500 unmap_object(obj, page_mask_bits(fetch_and_zero(&obj->mm.mapping)));
502 i915_gem_object_unpin_map(obj);
506 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
507 struct i915_gem_object_page_iter *iter,
509 unsigned int *offset,
512 struct scatterlist *sg;
513 unsigned int idx, count;
516 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
517 if (!i915_gem_object_has_pinned_pages(obj))
518 assert_object_held(obj);
520 /* As we iterate forward through the sg, we record each entry in a
521 * radixtree for quick repeated (backwards) lookups. If we have seen
522 * this index previously, we will have an entry for it.
524 * Initial lookup is O(N), but this is amortized to O(1) for
525 * sequential page access (where each new request is consecutive
526 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
527 * i.e. O(1) with a large constant!
529 if (n < READ_ONCE(iter->sg_idx))
532 mutex_lock(&iter->lock);
534 /* We prefer to reuse the last sg so that repeated lookup of this
535 * (or the subsequent) sg are fast - comparing against the last
536 * sg is faster than going through the radixtree.
541 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
543 while (idx + count <= n) {
548 /* If we cannot allocate and insert this entry, or the
549 * individual pages from this range, cancel updating the
550 * sg_idx so that on this lookup we are forced to linearly
551 * scan onwards, but on future lookups we will try the
552 * insertion again (in which case we need to be careful of
553 * the error return reporting that we have already inserted
556 ret = radix_tree_insert(&iter->radix, idx, sg);
557 if (ret && ret != -EEXIST)
560 entry = xa_mk_value(idx);
561 for (i = 1; i < count; i++) {
562 ret = radix_tree_insert(&iter->radix, idx + i, entry);
563 if (ret && ret != -EEXIST)
568 sg = ____sg_next(sg);
569 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
576 mutex_unlock(&iter->lock);
578 if (unlikely(n < idx)) /* insertion completed by another thread */
581 /* In case we failed to insert the entry into the radixtree, we need
582 * to look beyond the current sg.
584 while (idx + count <= n) {
586 sg = ____sg_next(sg);
587 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
596 sg = radix_tree_lookup(&iter->radix, n);
599 /* If this index is in the middle of multi-page sg entry,
600 * the radix tree will contain a value entry that points
601 * to the start of that range. We will return the pointer to
602 * the base page and the offset of this page within the
606 if (unlikely(xa_is_value(sg))) {
607 unsigned long base = xa_to_value(sg);
609 sg = radix_tree_lookup(&iter->radix, base);
621 i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
623 struct scatterlist *sg;
626 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
628 sg = i915_gem_object_get_sg(obj, n, &offset);
629 return nth_page(sg_page(sg), offset);
632 /* Like i915_gem_object_get_page(), but mark the returned page dirty */
634 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
639 page = i915_gem_object_get_page(obj, n);
641 set_page_dirty(page);
647 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
651 struct scatterlist *sg;
654 sg = i915_gem_object_get_sg_dma(obj, n, &offset);
657 *len = sg_dma_len(sg) - (offset << PAGE_SHIFT);
659 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
663 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
666 return i915_gem_object_get_dma_address_len(obj, n, NULL);