1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
6 #include <linux/shmem_fs.h>
8 #include <drm/ttm/ttm_placement.h>
9 #include <drm/ttm/ttm_tt.h>
10 #include <drm/drm_buddy.h>
13 #include "i915_ttm_buddy_manager.h"
14 #include "intel_memory_region.h"
15 #include "intel_region_ttm.h"
17 #include "gem/i915_gem_mman.h"
18 #include "gem/i915_gem_object.h"
19 #include "gem/i915_gem_region.h"
20 #include "gem/i915_gem_ttm.h"
21 #include "gem/i915_gem_ttm_move.h"
22 #include "gem/i915_gem_ttm_pm.h"
23 #include "gt/intel_gpu_commands.h"
25 #define I915_TTM_PRIO_PURGE 0
26 #define I915_TTM_PRIO_NO_PAGES 1
27 #define I915_TTM_PRIO_HAS_PAGES 2
28 #define I915_TTM_PRIO_NEEDS_CPU_ACCESS 3
31 * Size of struct ttm_place vector in on-stack struct ttm_placement allocs
33 #define I915_TTM_MAX_PLACEMENTS INTEL_REGION_UNKNOWN
36 * struct i915_ttm_tt - TTM page vector with additional private information
37 * @ttm: The base TTM page vector.
38 * @dev: The struct device used for dma mapping and unmapping.
39 * @cached_rsgt: The cached scatter-gather table.
40 * @is_shmem: Set if using shmem.
41 * @filp: The shmem file, if using shmem backend.
43 * Note that DMA may be going on right up to the point where the page-
44 * vector is unpopulated in delayed destroy. Hence keep the
45 * scatter-gather table mapped and cached up to that point. This is
46 * different from the cached gem object io scatter-gather table which
47 * doesn't have an associated dma mapping.
52 struct i915_refct_sgt cached_rsgt;
58 static const struct ttm_place sys_placement_flags = {
61 .mem_type = I915_PL_SYSTEM,
65 static struct ttm_placement i915_sys_placement = {
67 .placement = &sys_placement_flags,
68 .num_busy_placement = 1,
69 .busy_placement = &sys_placement_flags,
73 * i915_ttm_sys_placement - Return the struct ttm_placement to be
74 * used for an object in system memory.
76 * Rather than making the struct extern, use this
79 * Return: A pointer to a static variable for sys placement.
81 struct ttm_placement *i915_ttm_sys_placement(void)
83 return &i915_sys_placement;
86 static int i915_ttm_err_to_gem(int err)
95 * TTM likes to convert -EDEADLK to -EBUSY, and wants us to
96 * restart the operation, since we don't record the contending
97 * lock. We use -EAGAIN to restart.
102 * Memory type / region is full, and we can't evict.
103 * Except possibly system, that returns -ENOMEM;
113 static enum ttm_caching
114 i915_ttm_select_tt_caching(const struct drm_i915_gem_object *obj)
117 * Objects only allowed in system get cached cpu-mappings, or when
118 * evicting lmem-only buffers to system for swapping. Other objects get
119 * WC mapping for now. Even if in system.
121 if (obj->mm.n_placements <= 1)
124 return ttm_write_combined;
128 i915_ttm_place_from_region(const struct intel_memory_region *mr,
129 struct ttm_place *place,
130 resource_size_t offset,
131 resource_size_t size,
134 memset(place, 0, sizeof(*place));
135 place->mem_type = intel_region_to_ttm_type(mr);
137 if (mr->type == INTEL_MEMORY_SYSTEM)
140 if (flags & I915_BO_ALLOC_CONTIGUOUS)
141 place->flags |= TTM_PL_FLAG_CONTIGUOUS;
142 if (offset != I915_BO_INVALID_OFFSET) {
143 place->fpfn = offset >> PAGE_SHIFT;
144 place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
145 } else if (mr->io_size && mr->io_size < mr->total) {
146 if (flags & I915_BO_ALLOC_GPU_ONLY) {
147 place->flags |= TTM_PL_FLAG_TOPDOWN;
150 place->lpfn = mr->io_size >> PAGE_SHIFT;
156 i915_ttm_placement_from_obj(const struct drm_i915_gem_object *obj,
157 struct ttm_place *requested,
158 struct ttm_place *busy,
159 struct ttm_placement *placement)
161 unsigned int num_allowed = obj->mm.n_placements;
162 unsigned int flags = obj->flags;
165 placement->num_placement = 1;
166 i915_ttm_place_from_region(num_allowed ? obj->mm.placements[0] :
167 obj->mm.region, requested, obj->bo_offset,
168 obj->base.size, flags);
170 /* Cache this on object? */
171 placement->num_busy_placement = num_allowed;
172 for (i = 0; i < placement->num_busy_placement; ++i)
173 i915_ttm_place_from_region(obj->mm.placements[i], busy + i,
174 obj->bo_offset, obj->base.size, flags);
176 if (num_allowed == 0) {
178 placement->num_busy_placement = 1;
181 placement->placement = requested;
182 placement->busy_placement = busy;
185 static int i915_ttm_tt_shmem_populate(struct ttm_device *bdev,
187 struct ttm_operation_ctx *ctx)
189 struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev);
190 struct intel_memory_region *mr = i915->mm.regions[INTEL_MEMORY_SYSTEM];
191 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
192 const unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
193 const size_t size = (size_t)ttm->num_pages << PAGE_SHIFT;
194 struct file *filp = i915_tt->filp;
195 struct sgt_iter sgt_iter;
202 struct address_space *mapping;
205 filp = shmem_file_setup("i915-shmem-tt", size, VM_NORESERVE);
207 return PTR_ERR(filp);
209 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
211 mapping = filp->f_mapping;
212 mapping_set_gfp_mask(mapping, mask);
213 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
215 i915_tt->filp = filp;
218 st = &i915_tt->cached_rsgt.table;
219 err = shmem_sg_alloc_table(i915, st, size, mr, filp->f_mapping,
224 err = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL,
225 DMA_ATTR_SKIP_CPU_SYNC);
230 for_each_sgt_page(page, sgt_iter, st)
231 ttm->pages[i++] = page;
233 if (ttm->page_flags & TTM_TT_FLAG_SWAPPED)
234 ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;
239 shmem_sg_free_table(st, filp->f_mapping, false, false);
244 static void i915_ttm_tt_shmem_unpopulate(struct ttm_tt *ttm)
246 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
247 bool backup = ttm->page_flags & TTM_TT_FLAG_SWAPPED;
248 struct sg_table *st = &i915_tt->cached_rsgt.table;
250 shmem_sg_free_table(st, file_inode(i915_tt->filp)->i_mapping,
254 static void i915_ttm_tt_release(struct kref *ref)
256 struct i915_ttm_tt *i915_tt =
257 container_of(ref, typeof(*i915_tt), cached_rsgt.kref);
258 struct sg_table *st = &i915_tt->cached_rsgt.table;
260 GEM_WARN_ON(st->sgl);
265 static const struct i915_refct_sgt_ops tt_rsgt_ops = {
266 .release = i915_ttm_tt_release
269 static struct ttm_tt *i915_ttm_tt_create(struct ttm_buffer_object *bo,
272 struct drm_i915_private *i915 = container_of(bo->bdev, typeof(*i915),
274 struct ttm_resource_manager *man =
275 ttm_manager_type(bo->bdev, bo->resource->mem_type);
276 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
277 unsigned long ccs_pages = 0;
278 enum ttm_caching caching;
279 struct i915_ttm_tt *i915_tt;
282 if (i915_ttm_is_ghost_object(bo))
285 i915_tt = kzalloc(sizeof(*i915_tt), GFP_KERNEL);
289 if (obj->flags & I915_BO_ALLOC_CPU_CLEAR &&
291 page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
293 caching = i915_ttm_select_tt_caching(obj);
294 if (i915_gem_object_is_shrinkable(obj) && caching == ttm_cached) {
295 page_flags |= TTM_TT_FLAG_EXTERNAL |
296 TTM_TT_FLAG_EXTERNAL_MAPPABLE;
297 i915_tt->is_shmem = true;
300 if (i915_gem_object_needs_ccs_pages(obj))
301 ccs_pages = DIV_ROUND_UP(DIV_ROUND_UP(bo->base.size,
302 NUM_BYTES_PER_CCS_BYTE),
305 ret = ttm_tt_init(&i915_tt->ttm, bo, page_flags, caching, ccs_pages);
309 __i915_refct_sgt_init(&i915_tt->cached_rsgt, bo->base.size,
312 i915_tt->dev = obj->base.dev->dev;
314 return &i915_tt->ttm;
321 static int i915_ttm_tt_populate(struct ttm_device *bdev,
323 struct ttm_operation_ctx *ctx)
325 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
327 if (i915_tt->is_shmem)
328 return i915_ttm_tt_shmem_populate(bdev, ttm, ctx);
330 return ttm_pool_alloc(&bdev->pool, ttm, ctx);
333 static void i915_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
335 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
336 struct sg_table *st = &i915_tt->cached_rsgt.table;
339 dma_unmap_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
341 if (i915_tt->is_shmem) {
342 i915_ttm_tt_shmem_unpopulate(ttm);
345 ttm_pool_free(&bdev->pool, ttm);
349 static void i915_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
351 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
357 i915_refct_sgt_put(&i915_tt->cached_rsgt);
360 static bool i915_ttm_eviction_valuable(struct ttm_buffer_object *bo,
361 const struct ttm_place *place)
363 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
365 if (i915_ttm_is_ghost_object(bo))
369 * EXTERNAL objects should never be swapped out by TTM, instead we need
370 * to handle that ourselves. TTM will already skip such objects for us,
371 * but we would like to avoid grabbing locks for no good reason.
373 if (bo->ttm && bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
376 /* Will do for now. Our pinned objects are still on TTM's LRU lists */
377 if (!i915_gem_object_evictable(obj))
380 return ttm_bo_eviction_valuable(bo, place);
383 static void i915_ttm_evict_flags(struct ttm_buffer_object *bo,
384 struct ttm_placement *placement)
386 *placement = i915_sys_placement;
390 * i915_ttm_free_cached_io_rsgt - Free object cached LMEM information
391 * @obj: The GEM object
392 * This function frees any LMEM-related information that is cached on
393 * the object. For example the radix tree for fast page lookup and the
394 * cached refcounted sg-table
396 void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj)
398 struct radix_tree_iter iter;
401 if (!obj->ttm.cached_io_rsgt)
405 radix_tree_for_each_slot(slot, &obj->ttm.get_io_page.radix, &iter, 0)
406 radix_tree_delete(&obj->ttm.get_io_page.radix, iter.index);
409 i915_refct_sgt_put(obj->ttm.cached_io_rsgt);
410 obj->ttm.cached_io_rsgt = NULL;
414 * i915_ttm_purge - Clear an object of its memory
417 * This function is called to clear an object of it's memory when it is
418 * marked as not needed anymore.
420 * Return: 0 on success, negative error code on failure.
422 int i915_ttm_purge(struct drm_i915_gem_object *obj)
424 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
425 struct i915_ttm_tt *i915_tt =
426 container_of(bo->ttm, typeof(*i915_tt), ttm);
427 struct ttm_operation_ctx ctx = {
428 .interruptible = true,
429 .no_wait_gpu = false,
431 struct ttm_placement place = {};
434 if (obj->mm.madv == __I915_MADV_PURGED)
437 ret = ttm_bo_validate(bo, &place, &ctx);
441 if (bo->ttm && i915_tt->filp) {
443 * The below fput(which eventually calls shmem_truncate) might
444 * be delayed by worker, so when directly called to purge the
445 * pages(like by the shrinker) we should try to be more
446 * aggressive and release the pages immediately.
448 shmem_truncate_range(file_inode(i915_tt->filp),
450 fput(fetch_and_zero(&i915_tt->filp));
453 obj->write_domain = 0;
454 obj->read_domains = 0;
455 i915_ttm_adjust_gem_after_move(obj);
456 i915_ttm_free_cached_io_rsgt(obj);
457 obj->mm.madv = __I915_MADV_PURGED;
462 static int i915_ttm_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
464 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
465 struct i915_ttm_tt *i915_tt =
466 container_of(bo->ttm, typeof(*i915_tt), ttm);
467 struct ttm_operation_ctx ctx = {
468 .interruptible = true,
469 .no_wait_gpu = flags & I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT,
471 struct ttm_placement place = {};
474 if (!bo->ttm || bo->resource->mem_type != TTM_PL_SYSTEM)
477 GEM_BUG_ON(!i915_tt->is_shmem);
482 ret = ttm_bo_wait_ctx(bo, &ctx);
486 switch (obj->mm.madv) {
487 case I915_MADV_DONTNEED:
488 return i915_ttm_purge(obj);
489 case __I915_MADV_PURGED:
493 if (bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED)
496 bo->ttm->page_flags |= TTM_TT_FLAG_SWAPPED;
497 ret = ttm_bo_validate(bo, &place, &ctx);
499 bo->ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;
503 if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
504 __shmem_writeback(obj->base.size, i915_tt->filp->f_mapping);
509 static void i915_ttm_delete_mem_notify(struct ttm_buffer_object *bo)
511 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
513 if (bo->resource && !i915_ttm_is_ghost_object(bo)) {
514 __i915_gem_object_pages_fini(obj);
515 i915_ttm_free_cached_io_rsgt(obj);
519 static struct i915_refct_sgt *i915_ttm_tt_get_st(struct ttm_tt *ttm)
521 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
525 if (i915_tt->cached_rsgt.table.sgl)
526 return i915_refct_sgt_get(&i915_tt->cached_rsgt);
528 st = &i915_tt->cached_rsgt.table;
529 ret = sg_alloc_table_from_pages_segment(st,
530 ttm->pages, ttm->num_pages,
531 0, (unsigned long)ttm->num_pages << PAGE_SHIFT,
532 i915_sg_segment_size(i915_tt->dev), GFP_KERNEL);
538 ret = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
544 return i915_refct_sgt_get(&i915_tt->cached_rsgt);
548 * i915_ttm_resource_get_st - Get a refcounted sg-table pointing to the
550 * @obj: The GEM object used for sg-table caching
551 * @res: The struct ttm_resource for which an sg-table is requested.
553 * This function returns a refcounted sg-table representing the memory
554 * pointed to by @res. If @res is the object's current resource it may also
555 * cache the sg_table on the object or attempt to access an already cached
556 * sg-table. The refcounted sg-table needs to be put when no-longer in use.
558 * Return: A valid pointer to a struct i915_refct_sgt or error pointer on
561 struct i915_refct_sgt *
562 i915_ttm_resource_get_st(struct drm_i915_gem_object *obj,
563 struct ttm_resource *res)
565 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
568 if (!i915_ttm_gtt_binds_lmem(res))
569 return i915_ttm_tt_get_st(bo->ttm);
571 page_alignment = bo->page_alignment << PAGE_SHIFT;
573 page_alignment = obj->mm.region->min_page_size;
576 * If CPU mapping differs, we need to add the ttm_tt pages to
577 * the resulting st. Might make sense for GGTT.
579 GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(res));
580 if (bo->resource == res) {
581 if (!obj->ttm.cached_io_rsgt) {
582 struct i915_refct_sgt *rsgt;
584 rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region,
590 obj->ttm.cached_io_rsgt = rsgt;
592 return i915_refct_sgt_get(obj->ttm.cached_io_rsgt);
595 return intel_region_ttm_resource_to_rsgt(obj->mm.region, res,
599 static int i915_ttm_truncate(struct drm_i915_gem_object *obj)
601 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
604 WARN_ON_ONCE(obj->mm.madv == I915_MADV_WILLNEED);
606 err = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
613 err = i915_ttm_move_notify(bo);
617 return i915_ttm_purge(obj);
620 static void i915_ttm_swap_notify(struct ttm_buffer_object *bo)
622 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
625 if (i915_ttm_is_ghost_object(bo))
628 ret = i915_ttm_move_notify(bo);
630 GEM_WARN_ON(obj->ttm.cached_io_rsgt);
631 if (!ret && obj->mm.madv != I915_MADV_WILLNEED)
636 * i915_ttm_resource_mappable - Return true if the ttm resource is CPU
638 * @res: The TTM resource to check.
640 * This is interesting on small-BAR systems where we may encounter lmem objects
641 * that can't be accessed via the CPU.
643 bool i915_ttm_resource_mappable(struct ttm_resource *res)
645 struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
647 if (!i915_ttm_cpu_maps_iomem(res))
650 return bman_res->used_visible_size == PFN_UP(bman_res->base.size);
653 static int i915_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
655 struct drm_i915_gem_object *obj = i915_ttm_to_gem(mem->bo);
658 if (i915_ttm_is_ghost_object(mem->bo))
661 if (!kref_get_unless_zero(&obj->base.refcount))
664 assert_object_held(obj);
666 unknown_state = i915_gem_object_has_unknown_state(obj);
667 i915_gem_object_put(obj);
671 if (!i915_ttm_cpu_maps_iomem(mem))
674 if (!i915_ttm_resource_mappable(mem))
677 mem->bus.caching = ttm_write_combined;
678 mem->bus.is_iomem = true;
683 static unsigned long i915_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
684 unsigned long page_offset)
686 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
687 struct scatterlist *sg;
691 GEM_BUG_ON(i915_ttm_is_ghost_object(bo));
692 GEM_WARN_ON(bo->ttm);
694 base = obj->mm.region->iomap.base - obj->mm.region->region.start;
695 sg = __i915_gem_object_get_sg(obj, &obj->ttm.get_io_page, page_offset, &ofs, true);
697 return ((base + sg_dma_address(sg)) >> PAGE_SHIFT) + ofs;
700 static int i915_ttm_access_memory(struct ttm_buffer_object *bo,
701 unsigned long offset, void *buf,
704 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
705 resource_size_t iomap = obj->mm.region->iomap.base -
706 obj->mm.region->region.start;
707 unsigned long page = offset >> PAGE_SHIFT;
708 unsigned long bytes_left = len;
711 * TODO: For now just let it fail if the resource is non-mappable,
712 * otherwise we need to perform the memcpy from the gpu here, without
713 * interfering with the object (like moving the entire thing).
715 if (!i915_ttm_resource_mappable(bo->resource))
718 offset -= page << PAGE_SHIFT;
720 unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
724 daddr = i915_gem_object_get_dma_address(obj, page);
725 ptr = ioremap_wc(iomap + daddr + offset, bytes);
730 memcpy_toio(ptr, buf, bytes);
732 memcpy_fromio(buf, ptr, bytes);
739 } while (bytes_left);
745 * All callbacks need to take care not to downcast a struct ttm_buffer_object
746 * without checking its subclass, since it might be a TTM ghost object.
748 static struct ttm_device_funcs i915_ttm_bo_driver = {
749 .ttm_tt_create = i915_ttm_tt_create,
750 .ttm_tt_populate = i915_ttm_tt_populate,
751 .ttm_tt_unpopulate = i915_ttm_tt_unpopulate,
752 .ttm_tt_destroy = i915_ttm_tt_destroy,
753 .eviction_valuable = i915_ttm_eviction_valuable,
754 .evict_flags = i915_ttm_evict_flags,
755 .move = i915_ttm_move,
756 .swap_notify = i915_ttm_swap_notify,
757 .delete_mem_notify = i915_ttm_delete_mem_notify,
758 .io_mem_reserve = i915_ttm_io_mem_reserve,
759 .io_mem_pfn = i915_ttm_io_mem_pfn,
760 .access_memory = i915_ttm_access_memory,
764 * i915_ttm_driver - Return a pointer to the TTM device funcs
766 * Return: Pointer to statically allocated TTM device funcs.
768 struct ttm_device_funcs *i915_ttm_driver(void)
770 return &i915_ttm_bo_driver;
773 static int __i915_ttm_get_pages(struct drm_i915_gem_object *obj,
774 struct ttm_placement *placement)
776 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
777 struct ttm_operation_ctx ctx = {
778 .interruptible = true,
779 .no_wait_gpu = false,
784 /* First try only the requested placement. No eviction. */
785 real_num_busy = fetch_and_zero(&placement->num_busy_placement);
786 ret = ttm_bo_validate(bo, placement, &ctx);
788 ret = i915_ttm_err_to_gem(ret);
790 * Anything that wants to restart the operation gets to
793 if (ret == -EDEADLK || ret == -EINTR || ret == -ERESTARTSYS ||
798 * If the initial attempt fails, allow all accepted placements,
799 * evicting if necessary.
801 placement->num_busy_placement = real_num_busy;
802 ret = ttm_bo_validate(bo, placement, &ctx);
804 return i915_ttm_err_to_gem(ret);
807 if (bo->ttm && !ttm_tt_is_populated(bo->ttm)) {
808 ret = ttm_tt_populate(bo->bdev, bo->ttm, &ctx);
812 i915_ttm_adjust_domains_after_move(obj);
813 i915_ttm_adjust_gem_after_move(obj);
816 if (!i915_gem_object_has_pages(obj)) {
817 struct i915_refct_sgt *rsgt =
818 i915_ttm_resource_get_st(obj, bo->resource);
821 return PTR_ERR(rsgt);
823 GEM_BUG_ON(obj->mm.rsgt);
825 __i915_gem_object_set_pages(obj, &rsgt->table);
828 GEM_BUG_ON(bo->ttm && ((obj->base.size >> PAGE_SHIFT) < bo->ttm->num_pages));
829 i915_ttm_adjust_lru(obj);
833 static int i915_ttm_get_pages(struct drm_i915_gem_object *obj)
835 struct ttm_place requested, busy[I915_TTM_MAX_PLACEMENTS];
836 struct ttm_placement placement;
838 GEM_BUG_ON(obj->mm.n_placements > I915_TTM_MAX_PLACEMENTS);
840 /* Move to the requested placement. */
841 i915_ttm_placement_from_obj(obj, &requested, busy, &placement);
843 return __i915_ttm_get_pages(obj, &placement);
847 * DOC: Migration vs eviction
849 * GEM migration may not be the same as TTM migration / eviction. If
850 * the TTM core decides to evict an object it may be evicted to a
851 * TTM memory type that is not in the object's allowable GEM regions, or
852 * in fact theoretically to a TTM memory type that doesn't correspond to
853 * a GEM memory region. In that case the object's GEM region is not
854 * updated, and the data is migrated back to the GEM region at
855 * get_pages time. TTM may however set up CPU ptes to the object even
856 * when it is evicted.
857 * Gem forced migration using the i915_ttm_migrate() op, is allowed even
858 * to regions that are not in the object's list of allowable placements.
860 static int __i915_ttm_migrate(struct drm_i915_gem_object *obj,
861 struct intel_memory_region *mr,
864 struct ttm_place requested;
865 struct ttm_placement placement;
868 i915_ttm_place_from_region(mr, &requested, obj->bo_offset,
869 obj->base.size, flags);
870 placement.num_placement = 1;
871 placement.num_busy_placement = 1;
872 placement.placement = &requested;
873 placement.busy_placement = &requested;
875 ret = __i915_ttm_get_pages(obj, &placement);
880 * Reinitialize the region bindings. This is primarily
881 * required for objects where the new region is not in
882 * its allowable placements.
884 if (obj->mm.region != mr) {
885 i915_gem_object_release_memory_region(obj);
886 i915_gem_object_init_memory_region(obj, mr);
892 static int i915_ttm_migrate(struct drm_i915_gem_object *obj,
893 struct intel_memory_region *mr,
896 return __i915_ttm_migrate(obj, mr, flags);
899 static void i915_ttm_put_pages(struct drm_i915_gem_object *obj,
903 * We're currently not called from a shrinker, so put_pages()
904 * typically means the object is about to destroyed, or called
905 * from move_notify(). So just avoid doing much for now.
906 * If the object is not destroyed next, The TTM eviction logic
907 * and shrinkers will move it out if needed.
911 i915_refct_sgt_put(fetch_and_zero(&obj->mm.rsgt));
915 * i915_ttm_adjust_lru - Adjust an object's position on relevant LRU lists.
918 void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj)
920 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
921 struct i915_ttm_tt *i915_tt =
922 container_of(bo->ttm, typeof(*i915_tt), ttm);
924 bo->ttm && i915_tt->filp && ttm_tt_is_populated(bo->ttm);
927 * Don't manipulate the TTM LRUs while in TTM bo destruction.
928 * We're called through i915_ttm_delete_mem_notify().
930 if (!kref_read(&bo->kref))
934 * We skip managing the shrinker LRU in set_pages() and just manage
935 * everything here. This does at least solve the issue with having
936 * temporary shmem mappings(like with evicted lmem) not being visible to
937 * the shrinker. Only our shmem objects are shrinkable, everything else
938 * we keep as unshrinkable.
940 * To make sure everything plays nice we keep an extra shrink pin in TTM
941 * if the underlying pages are not currently shrinkable. Once we release
942 * our pin, like when the pages are moved to shmem, the pages will then
943 * be added to the shrinker LRU, assuming the caller isn't also holding
946 * TODO: consider maybe also bumping the shrinker list here when we have
947 * already unpinned it, which should give us something more like an LRU.
949 * TODO: There is a small window of opportunity for this function to
950 * get called from eviction after we've dropped the last GEM refcount,
951 * but before the TTM deleted flag is set on the object. Avoid
952 * adjusting the shrinker list in such cases, since the object is
953 * not available to the shrinker anyway due to its zero refcount.
954 * To fix this properly we should move to a TTM shrinker LRU list for
957 if (kref_get_unless_zero(&obj->base.refcount)) {
958 if (shrinkable != obj->mm.ttm_shrinkable) {
960 if (obj->mm.madv == I915_MADV_WILLNEED)
961 __i915_gem_object_make_shrinkable(obj);
963 __i915_gem_object_make_purgeable(obj);
965 i915_gem_object_make_unshrinkable(obj);
968 obj->mm.ttm_shrinkable = shrinkable;
970 i915_gem_object_put(obj);
974 * Put on the correct LRU list depending on the MADV status
976 spin_lock(&bo->bdev->lru_lock);
978 /* Try to keep shmem_tt from being considered for shrinking. */
979 bo->priority = TTM_MAX_BO_PRIORITY - 1;
980 } else if (obj->mm.madv != I915_MADV_WILLNEED) {
981 bo->priority = I915_TTM_PRIO_PURGE;
982 } else if (!i915_gem_object_has_pages(obj)) {
983 bo->priority = I915_TTM_PRIO_NO_PAGES;
985 struct ttm_resource_manager *man =
986 ttm_manager_type(bo->bdev, bo->resource->mem_type);
989 * If we need to place an LMEM resource which doesn't need CPU
990 * access then we should try not to victimize mappable objects
991 * first, since we likely end up stealing more of the mappable
992 * portion. And likewise when we try to find space for a mappble
993 * object, we know not to ever victimize objects that don't
994 * occupy any mappable pages.
996 if (i915_ttm_cpu_maps_iomem(bo->resource) &&
997 i915_ttm_buddy_man_visible_size(man) < man->size &&
998 !(obj->flags & I915_BO_ALLOC_GPU_ONLY))
999 bo->priority = I915_TTM_PRIO_NEEDS_CPU_ACCESS;
1001 bo->priority = I915_TTM_PRIO_HAS_PAGES;
1004 ttm_bo_move_to_lru_tail(bo);
1005 spin_unlock(&bo->bdev->lru_lock);
1009 * TTM-backed gem object destruction requires some clarification.
1010 * Basically we have two possibilities here. We can either rely on the
1011 * i915 delayed destruction and put the TTM object when the object
1012 * is idle. This would be detected by TTM which would bypass the
1013 * TTM delayed destroy handling. The other approach is to put the TTM
1014 * object early and rely on the TTM destroyed handling, and then free
1015 * the leftover parts of the GEM object once TTM's destroyed list handling is
1016 * complete. For now, we rely on the latter for two reasons:
1017 * a) TTM can evict an object even when it's on the delayed destroy list,
1018 * which in theory allows for complete eviction.
1019 * b) There is work going on in TTM to allow freeing an object even when
1020 * it's not idle, and using the TTM destroyed list handling could help us
1021 * benefit from that.
1023 static void i915_ttm_delayed_free(struct drm_i915_gem_object *obj)
1025 GEM_BUG_ON(!obj->ttm.created);
1027 ttm_bo_put(i915_gem_to_ttm(obj));
1030 static vm_fault_t vm_fault_ttm(struct vm_fault *vmf)
1032 struct vm_area_struct *area = vmf->vma;
1033 struct ttm_buffer_object *bo = area->vm_private_data;
1034 struct drm_device *dev = bo->base.dev;
1035 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
1036 intel_wakeref_t wakeref = 0;
1040 /* Sanity check that we allow writing into this object */
1041 if (unlikely(i915_gem_object_is_readonly(obj) &&
1042 area->vm_flags & VM_WRITE))
1043 return VM_FAULT_SIGBUS;
1045 ret = ttm_bo_vm_reserve(bo, vmf);
1049 if (obj->mm.madv != I915_MADV_WILLNEED) {
1050 dma_resv_unlock(bo->base.resv);
1051 return VM_FAULT_SIGBUS;
1054 if (!i915_ttm_resource_mappable(bo->resource)) {
1058 for (i = 0; i < obj->mm.n_placements; i++) {
1059 struct intel_memory_region *mr = obj->mm.placements[i];
1062 if (!mr->io_size && mr->type != INTEL_MEMORY_SYSTEM)
1066 flags &= ~I915_BO_ALLOC_GPU_ONLY;
1067 err = __i915_ttm_migrate(obj, mr, flags);
1073 drm_dbg(dev, "Unable to make resource CPU accessible(err = %pe)\n",
1075 dma_resv_unlock(bo->base.resv);
1076 ret = VM_FAULT_SIGBUS;
1081 if (i915_ttm_cpu_maps_iomem(bo->resource))
1082 wakeref = intel_runtime_pm_get(&to_i915(obj->base.dev)->runtime_pm);
1084 if (drm_dev_enter(dev, &idx)) {
1085 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
1086 TTM_BO_VM_NUM_PREFAULT);
1089 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1092 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1096 * ttm_bo_vm_reserve() already has dma_resv_lock.
1097 * userfault_count is protected by dma_resv lock and rpm wakeref.
1099 if (ret == VM_FAULT_NOPAGE && wakeref && !obj->userfault_count) {
1100 obj->userfault_count = 1;
1101 spin_lock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1102 list_add(&obj->userfault_link, &to_i915(obj->base.dev)->runtime_pm.lmem_userfault_list);
1103 spin_unlock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1105 GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(bo->resource));
1108 if (wakeref & CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)
1109 intel_wakeref_auto(&to_i915(obj->base.dev)->runtime_pm.userfault_wakeref,
1110 msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND));
1112 i915_ttm_adjust_lru(obj);
1114 dma_resv_unlock(bo->base.resv);
1118 intel_runtime_pm_put(&to_i915(obj->base.dev)->runtime_pm, wakeref);
1124 vm_access_ttm(struct vm_area_struct *area, unsigned long addr,
1125 void *buf, int len, int write)
1127 struct drm_i915_gem_object *obj =
1128 i915_ttm_to_gem(area->vm_private_data);
1130 if (i915_gem_object_is_readonly(obj) && write)
1133 return ttm_bo_vm_access(area, addr, buf, len, write);
1136 static void ttm_vm_open(struct vm_area_struct *vma)
1138 struct drm_i915_gem_object *obj =
1139 i915_ttm_to_gem(vma->vm_private_data);
1141 GEM_BUG_ON(i915_ttm_is_ghost_object(vma->vm_private_data));
1142 i915_gem_object_get(obj);
1145 static void ttm_vm_close(struct vm_area_struct *vma)
1147 struct drm_i915_gem_object *obj =
1148 i915_ttm_to_gem(vma->vm_private_data);
1150 GEM_BUG_ON(i915_ttm_is_ghost_object(vma->vm_private_data));
1151 i915_gem_object_put(obj);
1154 static const struct vm_operations_struct vm_ops_ttm = {
1155 .fault = vm_fault_ttm,
1156 .access = vm_access_ttm,
1157 .open = ttm_vm_open,
1158 .close = ttm_vm_close,
1161 static u64 i915_ttm_mmap_offset(struct drm_i915_gem_object *obj)
1163 /* The ttm_bo must be allocated with I915_BO_ALLOC_USER */
1164 GEM_BUG_ON(!drm_mm_node_allocated(&obj->base.vma_node.vm_node));
1166 return drm_vma_node_offset_addr(&obj->base.vma_node);
1169 static void i915_ttm_unmap_virtual(struct drm_i915_gem_object *obj)
1171 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
1172 intel_wakeref_t wakeref = 0;
1174 assert_object_held_shared(obj);
1176 if (i915_ttm_cpu_maps_iomem(bo->resource)) {
1177 wakeref = intel_runtime_pm_get(&to_i915(obj->base.dev)->runtime_pm);
1179 /* userfault_count is protected by obj lock and rpm wakeref. */
1180 if (obj->userfault_count) {
1181 spin_lock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1182 list_del(&obj->userfault_link);
1183 spin_unlock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1184 obj->userfault_count = 0;
1188 GEM_WARN_ON(obj->userfault_count);
1190 ttm_bo_unmap_virtual(i915_gem_to_ttm(obj));
1193 intel_runtime_pm_put(&to_i915(obj->base.dev)->runtime_pm, wakeref);
1196 static const struct drm_i915_gem_object_ops i915_gem_ttm_obj_ops = {
1197 .name = "i915_gem_object_ttm",
1198 .flags = I915_GEM_OBJECT_IS_SHRINKABLE |
1199 I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST,
1201 .get_pages = i915_ttm_get_pages,
1202 .put_pages = i915_ttm_put_pages,
1203 .truncate = i915_ttm_truncate,
1204 .shrink = i915_ttm_shrink,
1206 .adjust_lru = i915_ttm_adjust_lru,
1207 .delayed_free = i915_ttm_delayed_free,
1208 .migrate = i915_ttm_migrate,
1210 .mmap_offset = i915_ttm_mmap_offset,
1211 .unmap_virtual = i915_ttm_unmap_virtual,
1212 .mmap_ops = &vm_ops_ttm,
1215 void i915_ttm_bo_destroy(struct ttm_buffer_object *bo)
1217 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
1219 i915_gem_object_release_memory_region(obj);
1220 mutex_destroy(&obj->ttm.get_io_page.lock);
1222 if (obj->ttm.created) {
1224 * We freely manage the shrinker LRU outide of the mm.pages life
1225 * cycle. As a result when destroying the object we should be
1226 * extra paranoid and ensure we remove it from the LRU, before
1227 * we free the object.
1229 * Touching the ttm_shrinkable outside of the object lock here
1230 * should be safe now that the last GEM object ref was dropped.
1232 if (obj->mm.ttm_shrinkable)
1233 i915_gem_object_make_unshrinkable(obj);
1235 i915_ttm_backup_free(obj);
1237 /* This releases all gem object bindings to the backend. */
1238 __i915_gem_free_object(obj);
1240 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
1242 __i915_gem_object_fini(obj);
1247 * __i915_gem_ttm_object_init - Initialize a ttm-backed i915 gem object
1248 * @mem: The initial memory region for the object.
1249 * @obj: The gem object.
1250 * @size: Object size in bytes.
1251 * @flags: gem object flags.
1253 * Return: 0 on success, negative error code on failure.
1255 int __i915_gem_ttm_object_init(struct intel_memory_region *mem,
1256 struct drm_i915_gem_object *obj,
1257 resource_size_t offset,
1258 resource_size_t size,
1259 resource_size_t page_size,
1262 static struct lock_class_key lock_class;
1263 struct drm_i915_private *i915 = mem->i915;
1264 struct ttm_operation_ctx ctx = {
1265 .interruptible = true,
1266 .no_wait_gpu = false,
1268 enum ttm_bo_type bo_type;
1271 drm_gem_private_object_init(&i915->drm, &obj->base, size);
1272 i915_gem_object_init(obj, &i915_gem_ttm_obj_ops, &lock_class, flags);
1274 obj->bo_offset = offset;
1276 /* Don't put on a region list until we're either locked or fully initialized. */
1277 obj->mm.region = mem;
1278 INIT_LIST_HEAD(&obj->mm.region_link);
1280 INIT_RADIX_TREE(&obj->ttm.get_io_page.radix, GFP_KERNEL | __GFP_NOWARN);
1281 mutex_init(&obj->ttm.get_io_page.lock);
1282 bo_type = (obj->flags & I915_BO_ALLOC_USER) ? ttm_bo_type_device :
1285 obj->base.vma_node.driver_private = i915_gem_to_ttm(obj);
1287 /* Forcing the page size is kernel internal only */
1288 GEM_BUG_ON(page_size && obj->mm.n_placements);
1291 * Keep an extra shrink pin to prevent the object from being made
1292 * shrinkable too early. If the ttm_tt is ever allocated in shmem, we
1293 * drop the pin. The TTM backend manages the shrinker LRU itself,
1294 * outside of the normal mm.pages life cycle.
1296 i915_gem_object_make_unshrinkable(obj);
1299 * If this function fails, it will call the destructor, but
1300 * our caller still owns the object. So no freeing in the
1301 * destructor until obj->ttm.created is true.
1302 * Similarly, in delayed_destroy, we can't call ttm_bo_put()
1303 * until successful initialization.
1305 ret = ttm_bo_init_reserved(&i915->bdev, i915_gem_to_ttm(obj), bo_type,
1306 &i915_sys_placement, page_size >> PAGE_SHIFT,
1307 &ctx, NULL, NULL, i915_ttm_bo_destroy);
1309 return i915_ttm_err_to_gem(ret);
1311 obj->ttm.created = true;
1312 i915_gem_object_release_memory_region(obj);
1313 i915_gem_object_init_memory_region(obj, mem);
1314 i915_ttm_adjust_domains_after_move(obj);
1315 i915_ttm_adjust_gem_after_move(obj);
1316 i915_gem_object_unlock(obj);
1321 static const struct intel_memory_region_ops ttm_system_region_ops = {
1322 .init_object = __i915_gem_ttm_object_init,
1323 .release = intel_region_ttm_fini,
1326 struct intel_memory_region *
1327 i915_gem_ttm_system_setup(struct drm_i915_private *i915,
1328 u16 type, u16 instance)
1330 struct intel_memory_region *mr;
1332 mr = intel_memory_region_create(i915, 0,
1333 totalram_pages() << PAGE_SHIFT,
1336 &ttm_system_region_ops);
1340 intel_memory_region_set_name(mr, "system-ttm");