2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2016 Intel Corporation
7 #include <drm/drm_cache.h>
9 #include "gt/intel_gt.h"
10 #include "gt/intel_tlb.h"
13 #include "i915_gem_object.h"
14 #include "i915_scatterlist.h"
15 #include "i915_gem_lmem.h"
16 #include "i915_gem_mman.h"
18 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
19 struct sg_table *pages)
21 struct drm_i915_private *i915 = to_i915(obj->base.dev);
22 unsigned long supported = RUNTIME_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 obj->mm.page_sizes.phys = i915_sg_dma_sizes(pages->sgl);
48 GEM_BUG_ON(!obj->mm.page_sizes.phys);
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->gem_quirks & GEM_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))
193 static void flush_tlb_invalidate(struct drm_i915_gem_object *obj)
195 struct drm_i915_private *i915 = to_i915(obj->base.dev);
199 for_each_gt(gt, i915, id) {
200 if (!obj->mm.tlb[id])
203 intel_gt_invalidate_tlb_full(gt, obj->mm.tlb[id]);
209 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
211 struct sg_table *pages;
213 assert_object_held_shared(obj);
215 pages = fetch_and_zero(&obj->mm.pages);
216 if (IS_ERR_OR_NULL(pages))
219 if (i915_gem_object_is_volatile(obj))
220 obj->mm.madv = I915_MADV_WILLNEED;
222 if (!i915_gem_object_has_self_managed_shrink_list(obj))
223 i915_gem_object_make_unshrinkable(obj);
225 if (obj->mm.mapping) {
226 unmap_object(obj, page_mask_bits(obj->mm.mapping));
227 obj->mm.mapping = NULL;
230 __i915_gem_object_reset_page_iter(obj);
231 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
233 flush_tlb_invalidate(obj);
238 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
240 struct sg_table *pages;
242 if (i915_gem_object_has_pinned_pages(obj))
245 /* May be called by shrinker from within get_pages() (on another bo) */
246 assert_object_held_shared(obj);
248 i915_gem_object_release_mmap_offset(obj);
251 * ->put_pages might need to allocate memory for the bit17 swizzle
252 * array, hence protect them from being reaped by removing them from gtt
255 pages = __i915_gem_object_unset_pages(obj);
258 * XXX Temporary hijinx to avoid updating all backends to handle
259 * NULL pages. In the future, when we have more asynchronous
260 * get_pages backends we should be better able to handle the
261 * cancellation of the async task in a more uniform manner.
263 if (!IS_ERR_OR_NULL(pages))
264 obj->ops->put_pages(obj, pages);
269 /* The 'mapping' part of i915_gem_object_pin_map() below */
270 static void *i915_gem_object_map_page(struct drm_i915_gem_object *obj,
271 enum i915_map_type type)
273 unsigned long n_pages = obj->base.size >> PAGE_SHIFT, i;
274 struct page *stack[32], **pages = stack, *page;
275 struct sgt_iter iter;
282 fallthrough; /* to use PAGE_KERNEL anyway */
285 * On 32b, highmem using a finite set of indirect PTE (i.e.
286 * vmap) to provide virtual mappings of the high pages.
287 * As these are finite, map_new_virtual() must wait for some
288 * other kmap() to finish when it runs out. If we map a large
289 * number of objects, there is no method for it to tell us
290 * to release the mappings, and we deadlock.
292 * However, if we make an explicit vmap of the page, that
293 * uses a larger vmalloc arena, and also has the ability
294 * to tell us to release unwanted mappings. Most importantly,
295 * it will fail and propagate an error instead of waiting
298 * So if the page is beyond the 32b boundary, make an explicit
301 if (n_pages == 1 && !PageHighMem(sg_page(obj->mm.pages->sgl)))
302 return page_address(sg_page(obj->mm.pages->sgl));
303 pgprot = PAGE_KERNEL;
306 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
310 if (n_pages > ARRAY_SIZE(stack)) {
311 /* Too big for stack -- allocate temporary array instead */
312 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
314 return ERR_PTR(-ENOMEM);
318 for_each_sgt_page(page, iter, obj->mm.pages)
320 vaddr = vmap(pages, n_pages, 0, pgprot);
324 return vaddr ?: ERR_PTR(-ENOMEM);
327 static void *i915_gem_object_map_pfn(struct drm_i915_gem_object *obj,
328 enum i915_map_type type)
330 resource_size_t iomap = obj->mm.region->iomap.base -
331 obj->mm.region->region.start;
332 unsigned long n_pfn = obj->base.size >> PAGE_SHIFT;
333 unsigned long stack[32], *pfns = stack, i;
334 struct sgt_iter iter;
338 GEM_BUG_ON(type != I915_MAP_WC);
340 if (n_pfn > ARRAY_SIZE(stack)) {
341 /* Too big for stack -- allocate temporary array instead */
342 pfns = kvmalloc_array(n_pfn, sizeof(*pfns), GFP_KERNEL);
344 return ERR_PTR(-ENOMEM);
348 for_each_sgt_daddr(addr, iter, obj->mm.pages)
349 pfns[i++] = (iomap + addr) >> PAGE_SHIFT;
350 vaddr = vmap_pfn(pfns, n_pfn, pgprot_writecombine(PAGE_KERNEL_IO));
354 return vaddr ?: ERR_PTR(-ENOMEM);
357 /* get, pin, and map the pages of the object into kernel space */
358 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
359 enum i915_map_type type)
361 enum i915_map_type has_type;
366 if (!i915_gem_object_has_struct_page(obj) &&
367 !i915_gem_object_has_iomem(obj))
368 return ERR_PTR(-ENXIO);
370 if (WARN_ON_ONCE(obj->flags & I915_BO_ALLOC_GPU_ONLY))
371 return ERR_PTR(-EINVAL);
373 assert_object_held(obj);
375 pinned = !(type & I915_MAP_OVERRIDE);
376 type &= ~I915_MAP_OVERRIDE;
378 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
379 if (unlikely(!i915_gem_object_has_pages(obj))) {
380 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
382 err = ____i915_gem_object_get_pages(obj);
386 smp_mb__before_atomic();
388 atomic_inc(&obj->mm.pages_pin_count);
391 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
394 * For discrete our CPU mappings needs to be consistent in order to
395 * function correctly on !x86. When mapping things through TTM, we use
396 * the same rules to determine the caching type.
398 * The caching rules, starting from DG1:
400 * - If the object can be placed in device local-memory, then the
401 * pages should be allocated and mapped as write-combined only.
403 * - Everything else is always allocated and mapped as write-back,
404 * with the guarantee that everything is also coherent with the
407 * Internal users of lmem are already expected to get this right, so no
408 * fudging needed there.
410 if (i915_gem_object_placement_possible(obj, INTEL_MEMORY_LOCAL)) {
411 if (type != I915_MAP_WC && !obj->mm.n_placements) {
412 ptr = ERR_PTR(-ENODEV);
417 } else if (IS_DGFX(to_i915(obj->base.dev))) {
421 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
422 if (ptr && has_type != type) {
424 ptr = ERR_PTR(-EBUSY);
428 unmap_object(obj, ptr);
430 ptr = obj->mm.mapping = NULL;
434 err = i915_gem_object_wait_moving_fence(obj, true);
440 if (GEM_WARN_ON(type == I915_MAP_WC && !pat_enabled()))
441 ptr = ERR_PTR(-ENODEV);
442 else if (i915_gem_object_has_struct_page(obj))
443 ptr = i915_gem_object_map_page(obj, type);
445 ptr = i915_gem_object_map_pfn(obj, type);
449 obj->mm.mapping = page_pack_bits(ptr, type);
455 atomic_dec(&obj->mm.pages_pin_count);
459 void *i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
460 enum i915_map_type type)
464 i915_gem_object_lock(obj, NULL);
465 ret = i915_gem_object_pin_map(obj, type);
466 i915_gem_object_unlock(obj);
471 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
472 unsigned long offset,
475 enum i915_map_type has_type;
478 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
479 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
480 offset, size, obj->base.size));
482 wmb(); /* let all previous writes be visible to coherent partners */
483 obj->mm.dirty = true;
485 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
488 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
489 if (has_type == I915_MAP_WC)
492 drm_clflush_virt_range(ptr + offset, size);
493 if (size == obj->base.size) {
494 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
495 obj->cache_dirty = false;
499 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj)
501 GEM_BUG_ON(!obj->mm.mapping);
504 * We allow removing the mapping from underneath pinned pages!
506 * Furthermore, since this is an unsafe operation reserved only
507 * for construction time manipulation, we ignore locking prudence.
509 unmap_object(obj, page_mask_bits(fetch_and_zero(&obj->mm.mapping)));
511 i915_gem_object_unpin_map(obj);
515 __i915_gem_object_page_iter_get_sg(struct drm_i915_gem_object *obj,
516 struct i915_gem_object_page_iter *iter,
518 unsigned int *offset)
521 const bool dma = iter == &obj->mm.get_dma_page ||
522 iter == &obj->ttm.get_io_page;
523 unsigned int idx, count;
524 struct scatterlist *sg;
527 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
528 if (!i915_gem_object_has_pinned_pages(obj))
529 assert_object_held(obj);
531 /* As we iterate forward through the sg, we record each entry in a
532 * radixtree for quick repeated (backwards) lookups. If we have seen
533 * this index previously, we will have an entry for it.
535 * Initial lookup is O(N), but this is amortized to O(1) for
536 * sequential page access (where each new request is consecutive
537 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
538 * i.e. O(1) with a large constant!
540 if (n < READ_ONCE(iter->sg_idx))
543 mutex_lock(&iter->lock);
545 /* We prefer to reuse the last sg so that repeated lookup of this
546 * (or the subsequent) sg are fast - comparing against the last
547 * sg is faster than going through the radixtree.
552 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
554 while (idx + count <= n) {
559 /* If we cannot allocate and insert this entry, or the
560 * individual pages from this range, cancel updating the
561 * sg_idx so that on this lookup we are forced to linearly
562 * scan onwards, but on future lookups we will try the
563 * insertion again (in which case we need to be careful of
564 * the error return reporting that we have already inserted
567 ret = radix_tree_insert(&iter->radix, idx, sg);
568 if (ret && ret != -EEXIST)
571 entry = xa_mk_value(idx);
572 for (i = 1; i < count; i++) {
573 ret = radix_tree_insert(&iter->radix, idx + i, entry);
574 if (ret && ret != -EEXIST)
579 sg = ____sg_next(sg);
580 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
587 mutex_unlock(&iter->lock);
589 if (unlikely(n < idx)) /* insertion completed by another thread */
592 /* In case we failed to insert the entry into the radixtree, we need
593 * to look beyond the current sg.
595 while (idx + count <= n) {
597 sg = ____sg_next(sg);
598 count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
607 sg = radix_tree_lookup(&iter->radix, n);
610 /* If this index is in the middle of multi-page sg entry,
611 * the radix tree will contain a value entry that points
612 * to the start of that range. We will return the pointer to
613 * the base page and the offset of this page within the
617 if (unlikely(xa_is_value(sg))) {
618 unsigned long base = xa_to_value(sg);
620 sg = radix_tree_lookup(&iter->radix, base);
632 __i915_gem_object_get_page(struct drm_i915_gem_object *obj, pgoff_t n)
634 struct scatterlist *sg;
637 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
639 sg = i915_gem_object_get_sg(obj, n, &offset);
640 return nth_page(sg_page(sg), offset);
643 /* Like i915_gem_object_get_page(), but mark the returned page dirty */
645 __i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, pgoff_t n)
649 page = i915_gem_object_get_page(obj, n);
651 set_page_dirty(page);
657 __i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
658 pgoff_t n, unsigned int *len)
660 struct scatterlist *sg;
663 sg = i915_gem_object_get_sg_dma(obj, n, &offset);
666 *len = sg_dma_len(sg) - (offset << PAGE_SHIFT);
668 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
672 __i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, pgoff_t n)
674 return i915_gem_object_get_dma_address_len(obj, n, NULL);