1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
8 #include <linux/dma-buf.h>
10 #include <drm/drm_drv.h>
11 #include <drm/drm_gem_ttm_helper.h>
12 #include <drm/drm_managed.h>
13 #include <drm/ttm/ttm_device.h>
14 #include <drm/ttm/ttm_placement.h>
15 #include <drm/ttm/ttm_tt.h>
16 #include <drm/xe_drm.h>
18 #include "xe_device.h"
19 #include "xe_dma_buf.h"
20 #include "xe_drm_client.h"
24 #include "xe_migrate.h"
25 #include "xe_preempt_fence.h"
26 #include "xe_res_cursor.h"
28 #include "xe_ttm_stolen_mgr.h"
31 static const struct ttm_place sys_placement_flags = {
34 .mem_type = XE_PL_SYSTEM,
38 static struct ttm_placement sys_placement = {
40 .placement = &sys_placement_flags,
41 .num_busy_placement = 1,
42 .busy_placement = &sys_placement_flags,
45 static const struct ttm_place tt_placement_flags = {
52 static struct ttm_placement tt_placement = {
54 .placement = &tt_placement_flags,
55 .num_busy_placement = 1,
56 .busy_placement = &sys_placement_flags,
59 bool mem_type_is_vram(u32 mem_type)
61 return mem_type >= XE_PL_VRAM0 && mem_type != XE_PL_STOLEN;
64 static bool resource_is_stolen_vram(struct xe_device *xe, struct ttm_resource *res)
66 return res->mem_type == XE_PL_STOLEN && IS_DGFX(xe);
69 static bool resource_is_vram(struct ttm_resource *res)
71 return mem_type_is_vram(res->mem_type);
74 bool xe_bo_is_vram(struct xe_bo *bo)
76 return resource_is_vram(bo->ttm.resource) ||
77 resource_is_stolen_vram(xe_bo_device(bo), bo->ttm.resource);
80 bool xe_bo_is_stolen(struct xe_bo *bo)
82 return bo->ttm.resource->mem_type == XE_PL_STOLEN;
86 * xe_bo_is_stolen_devmem - check if BO is of stolen type accessed via PCI BAR
89 * The stolen memory is accessed through the PCI BAR for both DGFX and some
90 * integrated platforms that have a dedicated bit in the PTE for devmem (DM).
92 * Returns: true if it's stolen memory accessed via PCI BAR, false otherwise.
94 bool xe_bo_is_stolen_devmem(struct xe_bo *bo)
96 return xe_bo_is_stolen(bo) &&
97 GRAPHICS_VERx100(xe_bo_device(bo)) >= 1270;
100 static bool xe_bo_is_user(struct xe_bo *bo)
102 return bo->flags & XE_BO_CREATE_USER_BIT;
105 static struct xe_migrate *
106 mem_type_to_migrate(struct xe_device *xe, u32 mem_type)
108 struct xe_tile *tile;
110 xe_assert(xe, mem_type == XE_PL_STOLEN || mem_type_is_vram(mem_type));
111 tile = &xe->tiles[mem_type == XE_PL_STOLEN ? 0 : (mem_type - XE_PL_VRAM0)];
112 return tile->migrate;
115 static struct xe_mem_region *res_to_mem_region(struct ttm_resource *res)
117 struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
118 struct ttm_resource_manager *mgr;
120 xe_assert(xe, resource_is_vram(res));
121 mgr = ttm_manager_type(&xe->ttm, res->mem_type);
122 return to_xe_ttm_vram_mgr(mgr)->vram;
125 static void try_add_system(struct xe_device *xe, struct xe_bo *bo,
126 u32 bo_flags, u32 *c)
128 if (bo_flags & XE_BO_CREATE_SYSTEM_BIT) {
129 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
131 bo->placements[*c] = (struct ttm_place) {
132 .mem_type = XE_PL_TT,
136 if (bo->props.preferred_mem_type == XE_BO_PROPS_INVALID)
137 bo->props.preferred_mem_type = XE_PL_TT;
141 static void add_vram(struct xe_device *xe, struct xe_bo *bo,
142 struct ttm_place *places, u32 bo_flags, u32 mem_type, u32 *c)
144 struct ttm_place place = { .mem_type = mem_type };
145 struct xe_mem_region *vram;
148 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
150 vram = to_xe_ttm_vram_mgr(ttm_manager_type(&xe->ttm, mem_type))->vram;
151 xe_assert(xe, vram && vram->usable_size);
152 io_size = vram->io_size;
155 * For eviction / restore on suspend / resume objects
156 * pinned in VRAM must be contiguous
158 if (bo_flags & (XE_BO_CREATE_PINNED_BIT |
159 XE_BO_CREATE_GGTT_BIT))
160 place.flags |= TTM_PL_FLAG_CONTIGUOUS;
162 if (io_size < vram->usable_size) {
163 if (bo_flags & XE_BO_NEEDS_CPU_ACCESS) {
165 place.lpfn = io_size >> PAGE_SHIFT;
167 place.flags |= TTM_PL_FLAG_TOPDOWN;
173 if (bo->props.preferred_mem_type == XE_BO_PROPS_INVALID)
174 bo->props.preferred_mem_type = mem_type;
177 static void try_add_vram(struct xe_device *xe, struct xe_bo *bo,
178 u32 bo_flags, u32 *c)
180 if (bo->props.preferred_gt == XE_GT1) {
181 if (bo_flags & XE_BO_CREATE_VRAM1_BIT)
182 add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM1, c);
183 if (bo_flags & XE_BO_CREATE_VRAM0_BIT)
184 add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM0, c);
186 if (bo_flags & XE_BO_CREATE_VRAM0_BIT)
187 add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM0, c);
188 if (bo_flags & XE_BO_CREATE_VRAM1_BIT)
189 add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM1, c);
193 static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo,
194 u32 bo_flags, u32 *c)
196 if (bo_flags & XE_BO_CREATE_STOLEN_BIT) {
197 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
199 bo->placements[*c] = (struct ttm_place) {
200 .mem_type = XE_PL_STOLEN,
201 .flags = bo_flags & (XE_BO_CREATE_PINNED_BIT |
202 XE_BO_CREATE_GGTT_BIT) ?
203 TTM_PL_FLAG_CONTIGUOUS : 0,
209 static int __xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
214 bo->props.preferred_mem_type = XE_BO_PROPS_INVALID;
216 /* The order of placements should indicate preferred location */
218 if (bo->props.preferred_mem_class == DRM_XE_MEM_REGION_CLASS_SYSMEM) {
219 try_add_system(xe, bo, bo_flags, &c);
220 try_add_vram(xe, bo, bo_flags, &c);
222 try_add_vram(xe, bo, bo_flags, &c);
223 try_add_system(xe, bo, bo_flags, &c);
225 try_add_stolen(xe, bo, bo_flags, &c);
230 bo->placement = (struct ttm_placement) {
232 .placement = bo->placements,
233 .num_busy_placement = c,
234 .busy_placement = bo->placements,
240 int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
243 xe_bo_assert_held(bo);
244 return __xe_bo_placement_for_flags(xe, bo, bo_flags);
247 static void xe_evict_flags(struct ttm_buffer_object *tbo,
248 struct ttm_placement *placement)
250 if (!xe_bo_is_xe_bo(tbo)) {
251 /* Don't handle scatter gather BOs */
252 if (tbo->type == ttm_bo_type_sg) {
253 placement->num_placement = 0;
254 placement->num_busy_placement = 0;
258 *placement = sys_placement;
263 * For xe, sg bos that are evicted to system just triggers a
264 * rebind of the sg list upon subsequent validation to XE_PL_TT.
266 switch (tbo->resource->mem_type) {
270 *placement = tt_placement;
274 *placement = sys_placement;
286 static int xe_tt_map_sg(struct ttm_tt *tt)
288 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
289 unsigned long num_pages = tt->num_pages;
292 XE_WARN_ON(tt->page_flags & TTM_TT_FLAG_EXTERNAL);
297 ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages,
299 (u64)num_pages << PAGE_SHIFT,
300 xe_sg_segment_size(xe_tt->dev),
305 xe_tt->sg = &xe_tt->sgt;
306 ret = dma_map_sgtable(xe_tt->dev, xe_tt->sg, DMA_BIDIRECTIONAL,
307 DMA_ATTR_SKIP_CPU_SYNC);
309 sg_free_table(xe_tt->sg);
317 struct sg_table *xe_bo_sg(struct xe_bo *bo)
319 struct ttm_tt *tt = bo->ttm.ttm;
320 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
325 static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
328 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
329 struct xe_device *xe = xe_bo_device(bo);
330 struct xe_ttm_tt *tt;
331 unsigned long extra_pages;
332 enum ttm_caching caching;
335 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
339 tt->dev = xe->drm.dev;
342 if (xe_bo_needs_ccs_pages(bo))
343 extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, bo->size),
346 switch (bo->cpu_caching) {
347 case DRM_XE_GEM_CPU_CACHING_WC:
348 caching = ttm_write_combined;
351 caching = ttm_cached;
355 WARN_ON((bo->flags & XE_BO_CREATE_USER_BIT) && !bo->cpu_caching);
358 * Display scanout is always non-coherent with the CPU cache.
360 * For Xe_LPG and beyond, PPGTT PTE lookups are also non-coherent and
361 * require a CPU:WC mapping.
363 if ((!bo->cpu_caching && bo->flags & XE_BO_SCANOUT_BIT) ||
364 (xe->info.graphics_verx100 >= 1270 && bo->flags & XE_BO_PAGETABLE))
365 caching = ttm_write_combined;
367 err = ttm_tt_init(&tt->ttm, &bo->ttm, page_flags, caching, extra_pages);
376 static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt,
377 struct ttm_operation_ctx *ctx)
382 * dma-bufs are not populated with pages, and the dma-
383 * addresses are set up when moved to XE_PL_TT.
385 if (tt->page_flags & TTM_TT_FLAG_EXTERNAL)
388 err = ttm_pool_alloc(&ttm_dev->pool, tt, ctx);
392 /* A follow up may move this xe_bo_move when BO is moved to XE_PL_TT */
393 err = xe_tt_map_sg(tt);
395 ttm_pool_free(&ttm_dev->pool, tt);
400 static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
402 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
404 if (tt->page_flags & TTM_TT_FLAG_EXTERNAL)
408 dma_unmap_sgtable(xe_tt->dev, xe_tt->sg,
409 DMA_BIDIRECTIONAL, 0);
410 sg_free_table(xe_tt->sg);
414 return ttm_pool_free(&ttm_dev->pool, tt);
417 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
423 static int xe_ttm_io_mem_reserve(struct ttm_device *bdev,
424 struct ttm_resource *mem)
426 struct xe_device *xe = ttm_to_xe_device(bdev);
428 switch (mem->mem_type) {
434 struct xe_ttm_vram_mgr_resource *vres =
435 to_xe_ttm_vram_mgr_resource(mem);
436 struct xe_mem_region *vram = res_to_mem_region(mem);
438 if (vres->used_visible_size < mem->size)
441 mem->bus.offset = mem->start << PAGE_SHIFT;
444 mem->placement & TTM_PL_FLAG_CONTIGUOUS)
445 mem->bus.addr = (u8 __force *)vram->mapping +
448 mem->bus.offset += vram->io_start;
449 mem->bus.is_iomem = true;
451 #if !defined(CONFIG_X86)
452 mem->bus.caching = ttm_write_combined;
456 return xe_ttm_stolen_io_mem_reserve(xe, mem);
462 static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
463 const struct ttm_operation_ctx *ctx)
465 struct dma_resv_iter cursor;
466 struct dma_fence *fence;
467 struct drm_gem_object *obj = &bo->ttm.base;
468 struct drm_gpuvm_bo *vm_bo;
472 dma_resv_assert_held(bo->ttm.base.resv);
474 if (!list_empty(&bo->ttm.base.gpuva.list)) {
475 dma_resv_iter_begin(&cursor, bo->ttm.base.resv,
476 DMA_RESV_USAGE_BOOKKEEP);
477 dma_resv_for_each_fence_unlocked(&cursor, fence)
478 dma_fence_enable_sw_signaling(fence);
479 dma_resv_iter_end(&cursor);
482 drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
483 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
484 struct drm_gpuva *gpuva;
486 if (!xe_vm_in_fault_mode(vm)) {
487 drm_gpuvm_bo_evict(vm_bo, true);
494 if (ctx->no_wait_gpu &&
495 !dma_resv_test_signaled(bo->ttm.base.resv,
496 DMA_RESV_USAGE_BOOKKEEP))
499 timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
500 DMA_RESV_USAGE_BOOKKEEP,
502 MAX_SCHEDULE_TIMEOUT);
511 drm_gpuvm_bo_for_each_va(gpuva, vm_bo) {
512 struct xe_vma *vma = gpuva_to_vma(gpuva);
514 trace_xe_vma_evict(vma);
515 ret = xe_vm_invalidate_vma(vma);
525 * The dma-buf map_attachment() / unmap_attachment() is hooked up here.
526 * Note that unmapping the attachment is deferred to the next
527 * map_attachment time, or to bo destroy (after idling) whichever comes first.
528 * This is to avoid syncing before unmap_attachment(), assuming that the
529 * caller relies on idling the reservation object before moving the
530 * backing store out. Should that assumption not hold, then we will be able
531 * to unconditionally call unmap_attachment() when moving out to system.
533 static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
534 struct ttm_resource *new_res)
536 struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
537 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
539 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
542 xe_assert(xe, attach);
543 xe_assert(xe, ttm_bo->ttm);
545 if (new_res->mem_type == XE_PL_SYSTEM)
549 dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
553 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
561 ttm_bo_move_null(ttm_bo, new_res);
567 * xe_bo_move_notify - Notify subsystems of a pending move
568 * @bo: The buffer object
569 * @ctx: The struct ttm_operation_ctx controlling locking and waits.
571 * This function notifies subsystems of an upcoming buffer move.
572 * Upon receiving such a notification, subsystems should schedule
573 * halting access to the underlying pages and optionally add a fence
574 * to the buffer object's dma_resv object, that signals when access is
575 * stopped. The caller will wait on all dma_resv fences before
578 * A subsystem may commence access to the object after obtaining
579 * bindings to the new backing memory under the object lock.
581 * Return: 0 on success, -EINTR or -ERESTARTSYS if interrupted in fault mode,
582 * negative error code on error.
584 static int xe_bo_move_notify(struct xe_bo *bo,
585 const struct ttm_operation_ctx *ctx)
587 struct ttm_buffer_object *ttm_bo = &bo->ttm;
588 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
592 * If this starts to call into many components, consider
593 * using a notification chain here.
596 if (xe_bo_is_pinned(bo))
600 ret = xe_bo_trigger_rebind(xe, bo, ctx);
604 /* Don't call move_notify() for imported dma-bufs. */
605 if (ttm_bo->base.dma_buf && !ttm_bo->base.import_attach)
606 dma_buf_move_notify(ttm_bo->base.dma_buf);
611 static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
612 struct ttm_operation_ctx *ctx,
613 struct ttm_resource *new_mem,
614 struct ttm_place *hop)
616 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
617 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
618 struct ttm_resource *old_mem = ttm_bo->resource;
619 u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM;
620 struct ttm_tt *ttm = ttm_bo->ttm;
621 struct xe_migrate *migrate = NULL;
622 struct dma_fence *fence;
623 bool move_lacks_source;
626 bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) &&
627 ttm && ttm_tt_is_populated(ttm)) ? true : false;
629 /* Bo creation path, moving to system or TT. */
630 if ((!old_mem && ttm) && !handle_system_ccs) {
631 ttm_bo_move_null(ttm_bo, new_mem);
635 if (ttm_bo->type == ttm_bo_type_sg) {
636 ret = xe_bo_move_notify(bo, ctx);
638 ret = xe_bo_move_dmabuf(ttm_bo, new_mem);
642 tt_has_data = ttm && (ttm_tt_is_populated(ttm) ||
643 (ttm->page_flags & TTM_TT_FLAG_SWAPPED));
645 move_lacks_source = handle_system_ccs ? (!bo->ccs_cleared) :
646 (!mem_type_is_vram(old_mem_type) && !tt_has_data);
648 needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
649 (!ttm && ttm_bo->type == ttm_bo_type_device);
651 if ((move_lacks_source && !needs_clear)) {
652 ttm_bo_move_null(ttm_bo, new_mem);
656 if (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT && !handle_system_ccs) {
657 ttm_bo_move_null(ttm_bo, new_mem);
662 * Failed multi-hop where the old_mem is still marked as
663 * TTM_PL_FLAG_TEMPORARY, should just be a dummy move.
665 if (old_mem_type == XE_PL_TT &&
666 new_mem->mem_type == XE_PL_TT) {
667 ttm_bo_move_null(ttm_bo, new_mem);
671 if (!move_lacks_source && !xe_bo_is_pinned(bo)) {
672 ret = xe_bo_move_notify(bo, ctx);
677 if (old_mem_type == XE_PL_TT &&
678 new_mem->mem_type == XE_PL_SYSTEM) {
679 long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
680 DMA_RESV_USAGE_BOOKKEEP,
682 MAX_SCHEDULE_TIMEOUT);
688 if (!handle_system_ccs) {
689 ttm_bo_move_null(ttm_bo, new_mem);
694 if (!move_lacks_source &&
695 ((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) ||
696 (mem_type_is_vram(old_mem_type) &&
697 new_mem->mem_type == XE_PL_SYSTEM))) {
700 hop->mem_type = XE_PL_TT;
701 hop->flags = TTM_PL_FLAG_TEMPORARY;
707 migrate = bo->tile->migrate;
708 else if (resource_is_vram(new_mem))
709 migrate = mem_type_to_migrate(xe, new_mem->mem_type);
710 else if (mem_type_is_vram(old_mem_type))
711 migrate = mem_type_to_migrate(xe, old_mem_type);
713 migrate = xe->tiles[0].migrate;
715 xe_assert(xe, migrate);
717 trace_xe_bo_move(bo);
718 xe_device_mem_access_get(xe);
720 if (xe_bo_is_pinned(bo) && !xe_bo_is_user(bo)) {
722 * Kernel memory that is pinned should only be moved on suspend
723 * / resume, some of the pinned memory is required for the
724 * device to resume / use the GPU to move other evicted memory
725 * (user memory) around. This likely could be optimized a bit
726 * futher where we find the minimum set of pinned memory
727 * required for resume but for simplity doing a memcpy for all
730 ret = xe_bo_vmap(bo);
732 ret = ttm_bo_move_memcpy(ttm_bo, ctx, new_mem);
734 /* Create a new VMAP once kernel BO back in VRAM */
735 if (!ret && resource_is_vram(new_mem)) {
736 struct xe_mem_region *vram = res_to_mem_region(new_mem);
737 void __iomem *new_addr = vram->mapping +
738 (new_mem->start << PAGE_SHIFT);
740 if (XE_WARN_ON(new_mem->start == XE_BO_INVALID_OFFSET)) {
742 xe_device_mem_access_put(xe);
746 xe_assert(xe, new_mem->start ==
747 bo->placements->fpfn);
749 iosys_map_set_vaddr_iomem(&bo->vmap, new_addr);
753 if (move_lacks_source)
754 fence = xe_migrate_clear(migrate, bo, new_mem);
756 fence = xe_migrate_copy(migrate, bo, bo, old_mem,
757 new_mem, handle_system_ccs);
759 ret = PTR_ERR(fence);
760 xe_device_mem_access_put(xe);
763 if (!move_lacks_source) {
764 ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict,
767 dma_fence_wait(fence, false);
768 ttm_bo_move_null(ttm_bo, new_mem);
773 * ttm_bo_move_accel_cleanup() may blow up if
774 * bo->resource == NULL, so just attach the
775 * fence and set the new resource.
777 dma_resv_add_fence(ttm_bo->base.resv, fence,
778 DMA_RESV_USAGE_KERNEL);
779 ttm_bo_move_null(ttm_bo, new_mem);
782 dma_fence_put(fence);
785 xe_device_mem_access_put(xe);
793 * xe_bo_evict_pinned() - Evict a pinned VRAM object to system memory
794 * @bo: The buffer object to move.
796 * On successful completion, the object memory will be moved to sytem memory.
797 * This function blocks until the object has been fully moved.
799 * This is needed to for special handling of pinned VRAM object during
802 * Return: 0 on success. Negative error code on failure.
804 int xe_bo_evict_pinned(struct xe_bo *bo)
806 struct ttm_place place = {
807 .mem_type = XE_PL_TT,
809 struct ttm_placement placement = {
813 struct ttm_operation_ctx ctx = {
814 .interruptible = false,
816 struct ttm_resource *new_mem;
819 xe_bo_assert_held(bo);
821 if (WARN_ON(!bo->ttm.resource))
824 if (WARN_ON(!xe_bo_is_pinned(bo)))
827 if (WARN_ON(!xe_bo_is_vram(bo)))
830 ret = ttm_bo_mem_space(&bo->ttm, &placement, &new_mem, &ctx);
835 bo->ttm.ttm = xe_ttm_tt_create(&bo->ttm, 0);
842 ret = ttm_tt_populate(bo->ttm.bdev, bo->ttm.ttm, &ctx);
846 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
850 ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
854 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
855 false, MAX_SCHEDULE_TIMEOUT);
860 ttm_resource_free(&bo->ttm, &new_mem);
865 * xe_bo_restore_pinned() - Restore a pinned VRAM object
866 * @bo: The buffer object to move.
868 * On successful completion, the object memory will be moved back to VRAM.
869 * This function blocks until the object has been fully moved.
871 * This is needed to for special handling of pinned VRAM object during
874 * Return: 0 on success. Negative error code on failure.
876 int xe_bo_restore_pinned(struct xe_bo *bo)
878 struct ttm_operation_ctx ctx = {
879 .interruptible = false,
881 struct ttm_resource *new_mem;
884 xe_bo_assert_held(bo);
886 if (WARN_ON(!bo->ttm.resource))
889 if (WARN_ON(!xe_bo_is_pinned(bo)))
892 if (WARN_ON(xe_bo_is_vram(bo) || !bo->ttm.ttm))
895 ret = ttm_bo_mem_space(&bo->ttm, &bo->placement, &new_mem, &ctx);
899 ret = ttm_tt_populate(bo->ttm.bdev, bo->ttm.ttm, &ctx);
903 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
907 ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
911 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
912 false, MAX_SCHEDULE_TIMEOUT);
917 ttm_resource_free(&bo->ttm, &new_mem);
921 static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo,
922 unsigned long page_offset)
924 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
925 struct xe_res_cursor cursor;
926 struct xe_mem_region *vram;
928 if (ttm_bo->resource->mem_type == XE_PL_STOLEN)
929 return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT;
931 vram = res_to_mem_region(ttm_bo->resource);
932 xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor);
933 return (vram->io_start + cursor.start) >> PAGE_SHIFT;
936 static void __xe_bo_vunmap(struct xe_bo *bo);
939 * TODO: Move this function to TTM so we don't rely on how TTM does its
940 * locking, thereby abusing TTM internals.
942 static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo)
944 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
947 xe_assert(xe, !kref_read(&ttm_bo->kref));
950 * We can typically only race with TTM trylocking under the
951 * lru_lock, which will immediately be unlocked again since
952 * the ttm_bo refcount is zero at this point. So trylocking *should*
953 * always succeed here, as long as we hold the lru lock.
955 spin_lock(&ttm_bo->bdev->lru_lock);
956 locked = dma_resv_trylock(ttm_bo->base.resv);
957 spin_unlock(&ttm_bo->bdev->lru_lock);
958 xe_assert(xe, locked);
963 static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
965 struct dma_resv_iter cursor;
966 struct dma_fence *fence;
967 struct dma_fence *replacement = NULL;
970 if (!xe_bo_is_xe_bo(ttm_bo))
973 bo = ttm_to_xe_bo(ttm_bo);
974 xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount)));
977 * Corner case where TTM fails to allocate memory and this BOs resv
978 * still points the VMs resv
980 if (ttm_bo->base.resv != &ttm_bo->base._resv)
983 if (!xe_ttm_bo_lock_in_destructor(ttm_bo))
987 * Scrub the preempt fences if any. The unbind fence is already
988 * attached to the resv.
989 * TODO: Don't do this for external bos once we scrub them after
992 dma_resv_for_each_fence(&cursor, ttm_bo->base.resv,
993 DMA_RESV_USAGE_BOOKKEEP, fence) {
994 if (xe_fence_is_xe_preempt(fence) &&
995 !dma_fence_is_signaled(fence)) {
997 replacement = dma_fence_get_stub();
999 dma_resv_replace_fences(ttm_bo->base.resv,
1002 DMA_RESV_USAGE_BOOKKEEP);
1005 dma_fence_put(replacement);
1007 dma_resv_unlock(ttm_bo->base.resv);
1010 static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
1012 if (!xe_bo_is_xe_bo(ttm_bo))
1016 * Object is idle and about to be destroyed. Release the
1017 * dma-buf attachment.
1019 if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
1020 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
1021 struct xe_ttm_tt, ttm);
1023 dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
1030 struct ttm_device_funcs xe_ttm_funcs = {
1031 .ttm_tt_create = xe_ttm_tt_create,
1032 .ttm_tt_populate = xe_ttm_tt_populate,
1033 .ttm_tt_unpopulate = xe_ttm_tt_unpopulate,
1034 .ttm_tt_destroy = xe_ttm_tt_destroy,
1035 .evict_flags = xe_evict_flags,
1037 .io_mem_reserve = xe_ttm_io_mem_reserve,
1038 .io_mem_pfn = xe_ttm_io_mem_pfn,
1039 .release_notify = xe_ttm_bo_release_notify,
1040 .eviction_valuable = ttm_bo_eviction_valuable,
1041 .delete_mem_notify = xe_ttm_bo_delete_mem_notify,
1044 static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo)
1046 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1047 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1049 if (bo->ttm.base.import_attach)
1050 drm_prime_gem_destroy(&bo->ttm.base, NULL);
1051 drm_gem_object_release(&bo->ttm.base);
1053 xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list));
1055 if (bo->ggtt_node.size)
1056 xe_ggtt_remove_bo(bo->tile->mem.ggtt, bo);
1058 #ifdef CONFIG_PROC_FS
1060 xe_drm_client_remove_bo(bo);
1063 if (bo->vm && xe_bo_is_user(bo))
1069 static void xe_gem_object_free(struct drm_gem_object *obj)
1071 /* Our BO reference counting scheme works as follows:
1073 * The gem object kref is typically used throughout the driver,
1074 * and the gem object holds a ttm_buffer_object refcount, so
1075 * that when the last gem object reference is put, which is when
1076 * we end up in this function, we put also that ttm_buffer_object
1077 * refcount. Anything using gem interfaces is then no longer
1078 * allowed to access the object in a way that requires a gem
1079 * refcount, including locking the object.
1081 * driver ttm callbacks is allowed to use the ttm_buffer_object
1082 * refcount directly if needed.
1084 __xe_bo_vunmap(gem_to_xe_bo(obj));
1085 ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
1088 static void xe_gem_object_close(struct drm_gem_object *obj,
1089 struct drm_file *file_priv)
1091 struct xe_bo *bo = gem_to_xe_bo(obj);
1093 if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) {
1094 xe_assert(xe_bo_device(bo), xe_bo_is_user(bo));
1096 xe_bo_lock(bo, false);
1097 ttm_bo_set_bulk_move(&bo->ttm, NULL);
1102 static bool should_migrate_to_system(struct xe_bo *bo)
1104 struct xe_device *xe = xe_bo_device(bo);
1106 return xe_device_in_fault_mode(xe) && bo->props.cpu_atomic;
1109 static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
1111 struct ttm_buffer_object *tbo = vmf->vma->vm_private_data;
1112 struct drm_device *ddev = tbo->base.dev;
1116 ret = ttm_bo_vm_reserve(tbo, vmf);
1120 if (drm_dev_enter(ddev, &idx)) {
1121 struct xe_bo *bo = ttm_to_xe_bo(tbo);
1123 trace_xe_bo_cpu_fault(bo);
1125 if (should_migrate_to_system(bo)) {
1126 r = xe_bo_migrate(bo, XE_PL_TT);
1127 if (r == -EBUSY || r == -ERESTARTSYS || r == -EINTR)
1128 ret = VM_FAULT_NOPAGE;
1130 ret = VM_FAULT_SIGBUS;
1133 ret = ttm_bo_vm_fault_reserved(vmf,
1134 vmf->vma->vm_page_prot,
1135 TTM_BO_VM_NUM_PREFAULT);
1138 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1140 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1143 dma_resv_unlock(tbo->base.resv);
1147 static const struct vm_operations_struct xe_gem_vm_ops = {
1148 .fault = xe_gem_fault,
1149 .open = ttm_bo_vm_open,
1150 .close = ttm_bo_vm_close,
1151 .access = ttm_bo_vm_access
1154 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
1155 .free = xe_gem_object_free,
1156 .close = xe_gem_object_close,
1157 .mmap = drm_gem_ttm_mmap,
1158 .export = xe_gem_prime_export,
1159 .vm_ops = &xe_gem_vm_ops,
1163 * xe_bo_alloc - Allocate storage for a struct xe_bo
1165 * This funcition is intended to allocate storage to be used for input
1166 * to __xe_bo_create_locked(), in the case a pointer to the bo to be
1167 * created is needed before the call to __xe_bo_create_locked().
1168 * If __xe_bo_create_locked ends up never to be called, then the
1169 * storage allocated with this function needs to be freed using
1172 * Return: A pointer to an uninitialized struct xe_bo on success,
1173 * ERR_PTR(-ENOMEM) on error.
1175 struct xe_bo *xe_bo_alloc(void)
1177 struct xe_bo *bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1180 return ERR_PTR(-ENOMEM);
1186 * xe_bo_free - Free storage allocated using xe_bo_alloc()
1187 * @bo: The buffer object storage.
1189 * Refer to xe_bo_alloc() documentation for valid use-cases.
1191 void xe_bo_free(struct xe_bo *bo)
1196 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
1197 struct xe_tile *tile, struct dma_resv *resv,
1198 struct ttm_lru_bulk_move *bulk, size_t size,
1199 u16 cpu_caching, enum ttm_bo_type type,
1202 struct ttm_operation_ctx ctx = {
1203 .interruptible = true,
1204 .no_wait_gpu = false,
1206 struct ttm_placement *placement;
1208 size_t aligned_size;
1211 /* Only kernel objects should set GT */
1212 xe_assert(xe, !tile || type == ttm_bo_type_kernel);
1214 if (XE_WARN_ON(!size)) {
1216 return ERR_PTR(-EINVAL);
1219 if (flags & (XE_BO_CREATE_VRAM_MASK | XE_BO_CREATE_STOLEN_BIT) &&
1220 !(flags & XE_BO_CREATE_IGNORE_MIN_PAGE_SIZE_BIT) &&
1221 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) {
1222 aligned_size = ALIGN(size, SZ_64K);
1223 if (type != ttm_bo_type_device)
1224 size = ALIGN(size, SZ_64K);
1225 flags |= XE_BO_INTERNAL_64K;
1226 alignment = SZ_64K >> PAGE_SHIFT;
1229 aligned_size = ALIGN(size, SZ_4K);
1230 flags &= ~XE_BO_INTERNAL_64K;
1231 alignment = SZ_4K >> PAGE_SHIFT;
1234 if (type == ttm_bo_type_device && aligned_size != size)
1235 return ERR_PTR(-EINVAL);
1243 bo->ccs_cleared = false;
1247 bo->cpu_caching = cpu_caching;
1248 bo->ttm.base.funcs = &xe_gem_object_funcs;
1249 bo->props.preferred_mem_class = XE_BO_PROPS_INVALID;
1250 bo->props.preferred_gt = XE_BO_PROPS_INVALID;
1251 bo->props.preferred_mem_type = XE_BO_PROPS_INVALID;
1252 bo->ttm.priority = XE_BO_PRIORITY_NORMAL;
1253 INIT_LIST_HEAD(&bo->pinned_link);
1254 #ifdef CONFIG_PROC_FS
1255 INIT_LIST_HEAD(&bo->client_link);
1258 drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size);
1261 ctx.allow_res_evict = !(flags & XE_BO_CREATE_NO_RESV_EVICT);
1265 if (!(flags & XE_BO_FIXED_PLACEMENT_BIT)) {
1266 err = __xe_bo_placement_for_flags(xe, bo, bo->flags);
1268 xe_ttm_bo_destroy(&bo->ttm);
1269 return ERR_PTR(err);
1273 /* Defer populating type_sg bos */
1274 placement = (type == ttm_bo_type_sg ||
1275 bo->flags & XE_BO_DEFER_BACKING) ? &sys_placement :
1277 err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type,
1278 placement, alignment,
1279 &ctx, NULL, resv, xe_ttm_bo_destroy);
1281 return ERR_PTR(err);
1284 * The VRAM pages underneath are potentially still being accessed by the
1285 * GPU, as per async GPU clearing and async evictions. However TTM makes
1286 * sure to add any corresponding move/clear fences into the objects
1287 * dma-resv using the DMA_RESV_USAGE_KERNEL slot.
1289 * For KMD internal buffers we don't care about GPU clearing, however we
1290 * still need to handle async evictions, where the VRAM is still being
1291 * accessed by the GPU. Most internal callers are not expecting this,
1292 * since they are missing the required synchronisation before accessing
1293 * the memory. To keep things simple just sync wait any kernel fences
1294 * here, if the buffer is designated KMD internal.
1296 * For normal userspace objects we should already have the required
1297 * pipelining or sync waiting elsewhere, since we already have to deal
1298 * with things like async GPU clearing.
1300 if (type == ttm_bo_type_kernel) {
1301 long timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
1302 DMA_RESV_USAGE_KERNEL,
1304 MAX_SCHEDULE_TIMEOUT);
1308 dma_resv_unlock(bo->ttm.base.resv);
1310 return ERR_PTR(timeout);
1316 ttm_bo_set_bulk_move(&bo->ttm, bulk);
1318 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1323 static int __xe_bo_fixed_placement(struct xe_device *xe,
1326 u64 start, u64 end, u64 size)
1328 struct ttm_place *place = bo->placements;
1330 if (flags & (XE_BO_CREATE_USER_BIT|XE_BO_CREATE_SYSTEM_BIT))
1333 place->flags = TTM_PL_FLAG_CONTIGUOUS;
1334 place->fpfn = start >> PAGE_SHIFT;
1335 place->lpfn = end >> PAGE_SHIFT;
1337 switch (flags & (XE_BO_CREATE_STOLEN_BIT | XE_BO_CREATE_VRAM_MASK)) {
1338 case XE_BO_CREATE_VRAM0_BIT:
1339 place->mem_type = XE_PL_VRAM0;
1341 case XE_BO_CREATE_VRAM1_BIT:
1342 place->mem_type = XE_PL_VRAM1;
1344 case XE_BO_CREATE_STOLEN_BIT:
1345 place->mem_type = XE_PL_STOLEN;
1349 /* 0 or multiple of the above set */
1353 bo->placement = (struct ttm_placement) {
1356 .num_busy_placement = 1,
1357 .busy_placement = place,
1363 static struct xe_bo *
1364 __xe_bo_create_locked(struct xe_device *xe,
1365 struct xe_tile *tile, struct xe_vm *vm,
1366 size_t size, u64 start, u64 end,
1367 u16 cpu_caching, enum ttm_bo_type type, u32 flags)
1369 struct xe_bo *bo = NULL;
1373 xe_vm_assert_held(vm);
1375 if (start || end != ~0ULL) {
1380 flags |= XE_BO_FIXED_PLACEMENT_BIT;
1381 err = __xe_bo_fixed_placement(xe, bo, flags, start, end, size);
1384 return ERR_PTR(err);
1388 bo = ___xe_bo_create_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL,
1389 vm && !xe_vm_in_fault_mode(vm) &&
1390 flags & XE_BO_CREATE_USER_BIT ?
1391 &vm->lru_bulk_move : NULL, size,
1392 cpu_caching, type, flags);
1397 * Note that instead of taking a reference no the drm_gpuvm_resv_bo(),
1398 * to ensure the shared resv doesn't disappear under the bo, the bo
1399 * will keep a reference to the vm, and avoid circular references
1400 * by having all the vm's bo refereferences released at vm close
1403 if (vm && xe_bo_is_user(bo))
1407 if (bo->flags & XE_BO_CREATE_GGTT_BIT) {
1408 if (!tile && flags & XE_BO_CREATE_STOLEN_BIT)
1409 tile = xe_device_get_root_tile(xe);
1411 xe_assert(xe, tile);
1413 if (flags & XE_BO_FIXED_PLACEMENT_BIT) {
1414 err = xe_ggtt_insert_bo_at(tile->mem.ggtt, bo,
1415 start + bo->size, U64_MAX);
1417 err = xe_ggtt_insert_bo(tile->mem.ggtt, bo);
1420 goto err_unlock_put_bo;
1426 __xe_bo_unset_bulk_move(bo);
1427 xe_bo_unlock_vm_held(bo);
1429 return ERR_PTR(err);
1433 xe_bo_create_locked_range(struct xe_device *xe,
1434 struct xe_tile *tile, struct xe_vm *vm,
1435 size_t size, u64 start, u64 end,
1436 enum ttm_bo_type type, u32 flags)
1438 return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type, flags);
1441 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
1442 struct xe_vm *vm, size_t size,
1443 enum ttm_bo_type type, u32 flags)
1445 return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type, flags);
1448 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
1449 struct xe_vm *vm, size_t size,
1451 enum ttm_bo_type type,
1454 struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL,
1456 flags | XE_BO_CREATE_USER_BIT);
1458 xe_bo_unlock_vm_held(bo);
1463 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
1464 struct xe_vm *vm, size_t size,
1465 enum ttm_bo_type type, u32 flags)
1467 struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags);
1470 xe_bo_unlock_vm_held(bo);
1475 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
1477 size_t size, u64 offset,
1478 enum ttm_bo_type type, u32 flags)
1482 u64 start = offset == ~0ull ? 0 : offset;
1483 u64 end = offset == ~0ull ? offset : start + size;
1485 if (flags & XE_BO_CREATE_STOLEN_BIT &&
1486 xe_ttm_stolen_cpu_access_needs_ggtt(xe))
1487 flags |= XE_BO_CREATE_GGTT_BIT;
1489 bo = xe_bo_create_locked_range(xe, tile, vm, size, start, end, type,
1490 flags | XE_BO_NEEDS_CPU_ACCESS);
1494 err = xe_bo_pin(bo);
1498 err = xe_bo_vmap(bo);
1502 xe_bo_unlock_vm_held(bo);
1509 xe_bo_unlock_vm_held(bo);
1511 return ERR_PTR(err);
1514 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
1515 struct xe_vm *vm, size_t size,
1516 enum ttm_bo_type type, u32 flags)
1518 return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull, type, flags);
1521 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
1522 const void *data, size_t size,
1523 enum ttm_bo_type type, u32 flags)
1525 struct xe_bo *bo = xe_bo_create_pin_map(xe, tile, NULL,
1526 ALIGN(size, PAGE_SIZE),
1531 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
1536 static void __xe_bo_unpin_map_no_vm(struct drm_device *drm, void *arg)
1538 xe_bo_unpin_map_no_vm(arg);
1541 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
1542 size_t size, u32 flags)
1547 bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel, flags);
1551 ret = drmm_add_action_or_reset(&xe->drm, __xe_bo_unpin_map_no_vm, bo);
1553 return ERR_PTR(ret);
1558 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
1559 const void *data, size_t size, u32 flags)
1561 struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags);
1566 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
1572 * XXX: This is in the VM bind data path, likely should calculate this once and
1573 * store, with a recalculation if the BO is moved.
1575 uint64_t vram_region_gpu_offset(struct ttm_resource *res)
1577 struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
1579 if (res->mem_type == XE_PL_STOLEN)
1580 return xe_ttm_stolen_gpu_offset(xe);
1582 return res_to_mem_region(res)->dpa_base;
1586 * xe_bo_pin_external - pin an external BO
1587 * @bo: buffer object to be pinned
1589 * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD)
1590 * BO. Unique call compared to xe_bo_pin as this function has it own set of
1591 * asserts and code to ensure evict / restore on suspend / resume.
1593 * Returns 0 for success, negative error code otherwise.
1595 int xe_bo_pin_external(struct xe_bo *bo)
1597 struct xe_device *xe = xe_bo_device(bo);
1600 xe_assert(xe, !bo->vm);
1601 xe_assert(xe, xe_bo_is_user(bo));
1603 if (!xe_bo_is_pinned(bo)) {
1604 err = xe_bo_validate(bo, NULL, false);
1608 if (xe_bo_is_vram(bo)) {
1609 spin_lock(&xe->pinned.lock);
1610 list_add_tail(&bo->pinned_link,
1611 &xe->pinned.external_vram);
1612 spin_unlock(&xe->pinned.lock);
1616 ttm_bo_pin(&bo->ttm);
1619 * FIXME: If we always use the reserve / unreserve functions for locking
1620 * we do not need this.
1622 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1627 int xe_bo_pin(struct xe_bo *bo)
1629 struct xe_device *xe = xe_bo_device(bo);
1632 /* We currently don't expect user BO to be pinned */
1633 xe_assert(xe, !xe_bo_is_user(bo));
1635 /* Pinned object must be in GGTT or have pinned flag */
1636 xe_assert(xe, bo->flags & (XE_BO_CREATE_PINNED_BIT |
1637 XE_BO_CREATE_GGTT_BIT));
1640 * No reason we can't support pinning imported dma-bufs we just don't
1641 * expect to pin an imported dma-buf.
1643 xe_assert(xe, !bo->ttm.base.import_attach);
1645 /* We only expect at most 1 pin */
1646 xe_assert(xe, !xe_bo_is_pinned(bo));
1648 err = xe_bo_validate(bo, NULL, false);
1653 * For pinned objects in on DGFX, which are also in vram, we expect
1654 * these to be in contiguous VRAM memory. Required eviction / restore
1655 * during suspend / resume (force restore to same physical address).
1657 if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) &&
1658 bo->flags & XE_BO_INTERNAL_TEST)) {
1659 struct ttm_place *place = &(bo->placements[0]);
1661 if (mem_type_is_vram(place->mem_type)) {
1662 xe_assert(xe, place->flags & TTM_PL_FLAG_CONTIGUOUS);
1664 place->fpfn = (xe_bo_addr(bo, 0, PAGE_SIZE) -
1665 vram_region_gpu_offset(bo->ttm.resource)) >> PAGE_SHIFT;
1666 place->lpfn = place->fpfn + (bo->size >> PAGE_SHIFT);
1668 spin_lock(&xe->pinned.lock);
1669 list_add_tail(&bo->pinned_link, &xe->pinned.kernel_bo_present);
1670 spin_unlock(&xe->pinned.lock);
1674 ttm_bo_pin(&bo->ttm);
1677 * FIXME: If we always use the reserve / unreserve functions for locking
1678 * we do not need this.
1680 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1686 * xe_bo_unpin_external - unpin an external BO
1687 * @bo: buffer object to be unpinned
1689 * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD)
1690 * BO. Unique call compared to xe_bo_unpin as this function has it own set of
1691 * asserts and code to ensure evict / restore on suspend / resume.
1693 * Returns 0 for success, negative error code otherwise.
1695 void xe_bo_unpin_external(struct xe_bo *bo)
1697 struct xe_device *xe = xe_bo_device(bo);
1699 xe_assert(xe, !bo->vm);
1700 xe_assert(xe, xe_bo_is_pinned(bo));
1701 xe_assert(xe, xe_bo_is_user(bo));
1703 if (bo->ttm.pin_count == 1 && !list_empty(&bo->pinned_link)) {
1704 spin_lock(&xe->pinned.lock);
1705 list_del_init(&bo->pinned_link);
1706 spin_unlock(&xe->pinned.lock);
1709 ttm_bo_unpin(&bo->ttm);
1712 * FIXME: If we always use the reserve / unreserve functions for locking
1713 * we do not need this.
1715 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1718 void xe_bo_unpin(struct xe_bo *bo)
1720 struct xe_device *xe = xe_bo_device(bo);
1722 xe_assert(xe, !bo->ttm.base.import_attach);
1723 xe_assert(xe, xe_bo_is_pinned(bo));
1725 if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) &&
1726 bo->flags & XE_BO_INTERNAL_TEST)) {
1727 struct ttm_place *place = &(bo->placements[0]);
1729 if (mem_type_is_vram(place->mem_type)) {
1730 xe_assert(xe, !list_empty(&bo->pinned_link));
1732 spin_lock(&xe->pinned.lock);
1733 list_del_init(&bo->pinned_link);
1734 spin_unlock(&xe->pinned.lock);
1738 ttm_bo_unpin(&bo->ttm);
1742 * xe_bo_validate() - Make sure the bo is in an allowed placement
1744 * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or
1745 * NULL. Used together with @allow_res_evict.
1746 * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's
1747 * reservation object.
1749 * Make sure the bo is in allowed placement, migrating it if necessary. If
1750 * needed, other bos will be evicted. If bos selected for eviction shares
1751 * the @vm's reservation object, they can be evicted iff @allow_res_evict is
1752 * set to true, otherwise they will be bypassed.
1754 * Return: 0 on success, negative error code on failure. May return
1755 * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal.
1757 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict)
1759 struct ttm_operation_ctx ctx = {
1760 .interruptible = true,
1761 .no_wait_gpu = false,
1765 lockdep_assert_held(&vm->lock);
1766 xe_vm_assert_held(vm);
1768 ctx.allow_res_evict = allow_res_evict;
1769 ctx.resv = xe_vm_resv(vm);
1772 return ttm_bo_validate(&bo->ttm, &bo->placement, &ctx);
1775 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo)
1777 if (bo->destroy == &xe_ttm_bo_destroy)
1784 * Resolve a BO address. There is no assert to check if the proper lock is held
1785 * so it should only be used in cases where it is not fatal to get the wrong
1786 * address, such as printing debug information, but not in cases where memory is
1787 * written based on this result.
1789 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
1791 struct xe_device *xe = xe_bo_device(bo);
1792 struct xe_res_cursor cur;
1795 xe_assert(xe, page_size <= PAGE_SIZE);
1796 page = offset >> PAGE_SHIFT;
1797 offset &= (PAGE_SIZE - 1);
1799 if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
1800 xe_assert(xe, bo->ttm.ttm);
1802 xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
1804 return xe_res_dma(&cur) + offset;
1806 struct xe_res_cursor cur;
1808 xe_res_first(bo->ttm.resource, page << PAGE_SHIFT,
1810 return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource);
1814 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
1816 if (!READ_ONCE(bo->ttm.pin_count))
1817 xe_bo_assert_held(bo);
1818 return __xe_bo_addr(bo, offset, page_size);
1821 int xe_bo_vmap(struct xe_bo *bo)
1827 xe_bo_assert_held(bo);
1829 if (!(bo->flags & XE_BO_NEEDS_CPU_ACCESS))
1832 if (!iosys_map_is_null(&bo->vmap))
1836 * We use this more or less deprecated interface for now since
1837 * ttm_bo_vmap() doesn't offer the optimization of kmapping
1838 * single page bos, which is done here.
1839 * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap
1840 * to use struct iosys_map.
1842 ret = ttm_bo_kmap(&bo->ttm, 0, bo->size >> PAGE_SHIFT, &bo->kmap);
1846 virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
1848 iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual);
1850 iosys_map_set_vaddr(&bo->vmap, virtual);
1855 static void __xe_bo_vunmap(struct xe_bo *bo)
1857 if (!iosys_map_is_null(&bo->vmap)) {
1858 iosys_map_clear(&bo->vmap);
1859 ttm_bo_kunmap(&bo->kmap);
1863 void xe_bo_vunmap(struct xe_bo *bo)
1865 xe_bo_assert_held(bo);
1869 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
1870 struct drm_file *file)
1872 struct xe_device *xe = to_xe_device(dev);
1873 struct xe_file *xef = to_xe_file(file);
1874 struct drm_xe_gem_create *args = data;
1875 struct xe_vm *vm = NULL;
1877 unsigned int bo_flags;
1881 if (XE_IOCTL_DBG(xe, args->extensions) ||
1882 XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) ||
1883 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1886 /* at least one valid memory placement must be specified */
1887 if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) ||
1891 if (XE_IOCTL_DBG(xe, args->flags &
1892 ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING |
1893 DRM_XE_GEM_CREATE_FLAG_SCANOUT |
1894 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM)))
1897 if (XE_IOCTL_DBG(xe, args->handle))
1900 if (XE_IOCTL_DBG(xe, !args->size))
1903 if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX))
1906 if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK))
1910 if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING)
1911 bo_flags |= XE_BO_DEFER_BACKING;
1913 if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT)
1914 bo_flags |= XE_BO_SCANOUT_BIT;
1916 bo_flags |= args->placement << (ffs(XE_BO_CREATE_SYSTEM_BIT) - 1);
1918 if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) {
1919 if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_CREATE_VRAM_MASK)))
1922 bo_flags |= XE_BO_NEEDS_CPU_ACCESS;
1925 if (XE_IOCTL_DBG(xe, !args->cpu_caching ||
1926 args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC))
1929 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_CREATE_VRAM_MASK &&
1930 args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC))
1933 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_SCANOUT_BIT &&
1934 args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB))
1938 vm = xe_vm_lookup(xef, args->vm_id);
1939 if (XE_IOCTL_DBG(xe, !vm))
1941 err = xe_vm_lock(vm, true);
1946 bo = xe_bo_create_user(xe, NULL, vm, args->size, args->cpu_caching,
1947 ttm_bo_type_device, bo_flags);
1957 err = drm_gem_handle_create(file, &bo->ttm.base, &handle);
1961 args->handle = handle;
1965 if (vm && !xe_vm_in_fault_mode(vm)) {
1966 xe_vm_lock(vm, false);
1967 __xe_bo_unset_bulk_move(bo);
1979 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
1980 struct drm_file *file)
1982 struct xe_device *xe = to_xe_device(dev);
1983 struct drm_xe_gem_mmap_offset *args = data;
1984 struct drm_gem_object *gem_obj;
1986 if (XE_IOCTL_DBG(xe, args->extensions) ||
1987 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1990 if (XE_IOCTL_DBG(xe, args->flags))
1993 gem_obj = drm_gem_object_lookup(file, args->handle);
1994 if (XE_IOCTL_DBG(xe, !gem_obj))
1997 /* The mmap offset was set up at BO allocation time. */
1998 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
2000 xe_bo_put(gem_to_xe_bo(gem_obj));
2005 * xe_bo_lock() - Lock the buffer object's dma_resv object
2006 * @bo: The struct xe_bo whose lock is to be taken
2007 * @intr: Whether to perform any wait interruptible
2009 * Locks the buffer object's dma_resv object. If the buffer object is
2010 * pointing to a shared dma_resv object, that shared lock is locked.
2012 * Return: 0 on success, -EINTR if @intr is true and the wait for a
2013 * contended lock was interrupted. If @intr is set to false, the
2014 * function always returns 0.
2016 int xe_bo_lock(struct xe_bo *bo, bool intr)
2019 return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL);
2021 dma_resv_lock(bo->ttm.base.resv, NULL);
2027 * xe_bo_unlock() - Unlock the buffer object's dma_resv object
2028 * @bo: The struct xe_bo whose lock is to be released.
2030 * Unlock a buffer object lock that was locked by xe_bo_lock().
2032 void xe_bo_unlock(struct xe_bo *bo)
2034 dma_resv_unlock(bo->ttm.base.resv);
2038 * xe_bo_can_migrate - Whether a buffer object likely can be migrated
2039 * @bo: The buffer object to migrate
2040 * @mem_type: The TTM memory type intended to migrate to
2042 * Check whether the buffer object supports migration to the
2043 * given memory type. Note that pinning may affect the ability to migrate as
2044 * returned by this function.
2046 * This function is primarily intended as a helper for checking the
2047 * possibility to migrate buffer objects and can be called without
2048 * the object lock held.
2050 * Return: true if migration is possible, false otherwise.
2052 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type)
2054 unsigned int cur_place;
2056 if (bo->ttm.type == ttm_bo_type_kernel)
2059 if (bo->ttm.type == ttm_bo_type_sg)
2062 for (cur_place = 0; cur_place < bo->placement.num_placement;
2064 if (bo->placements[cur_place].mem_type == mem_type)
2071 static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
2073 memset(place, 0, sizeof(*place));
2074 place->mem_type = mem_type;
2078 * xe_bo_migrate - Migrate an object to the desired region id
2079 * @bo: The buffer object to migrate.
2080 * @mem_type: The TTM region type to migrate to.
2082 * Attempt to migrate the buffer object to the desired memory region. The
2083 * buffer object may not be pinned, and must be locked.
2084 * On successful completion, the object memory type will be updated,
2085 * but an async migration task may not have completed yet, and to
2086 * accomplish that, the object's kernel fences must be signaled with
2087 * the object lock held.
2089 * Return: 0 on success. Negative error code on failure. In particular may
2090 * return -EINTR or -ERESTARTSYS if signal pending.
2092 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type)
2094 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
2095 struct ttm_operation_ctx ctx = {
2096 .interruptible = true,
2097 .no_wait_gpu = false,
2099 struct ttm_placement placement;
2100 struct ttm_place requested;
2102 xe_bo_assert_held(bo);
2104 if (bo->ttm.resource->mem_type == mem_type)
2107 if (xe_bo_is_pinned(bo))
2110 if (!xe_bo_can_migrate(bo, mem_type))
2113 xe_place_from_ttm_type(mem_type, &requested);
2114 placement.num_placement = 1;
2115 placement.num_busy_placement = 1;
2116 placement.placement = &requested;
2117 placement.busy_placement = &requested;
2120 * Stolen needs to be handled like below VRAM handling if we ever need
2123 drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN);
2125 if (mem_type_is_vram(mem_type)) {
2128 add_vram(xe, bo, &requested, bo->flags, mem_type, &c);
2131 return ttm_bo_validate(&bo->ttm, &placement, &ctx);
2135 * xe_bo_evict - Evict an object to evict placement
2136 * @bo: The buffer object to migrate.
2137 * @force_alloc: Set force_alloc in ttm_operation_ctx
2139 * On successful completion, the object memory will be moved to evict
2140 * placement. Ths function blocks until the object has been fully moved.
2142 * Return: 0 on success. Negative error code on failure.
2144 int xe_bo_evict(struct xe_bo *bo, bool force_alloc)
2146 struct ttm_operation_ctx ctx = {
2147 .interruptible = false,
2148 .no_wait_gpu = false,
2149 .force_alloc = force_alloc,
2151 struct ttm_placement placement;
2154 xe_evict_flags(&bo->ttm, &placement);
2155 ret = ttm_bo_validate(&bo->ttm, &placement, &ctx);
2159 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
2160 false, MAX_SCHEDULE_TIMEOUT);
2166 * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when
2167 * placed in system memory.
2170 * Return: true if extra pages need to be allocated, false otherwise.
2172 bool xe_bo_needs_ccs_pages(struct xe_bo *bo)
2174 struct xe_device *xe = xe_bo_device(bo);
2176 if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device)
2179 /* On discrete GPUs, if the GPU can access this buffer from
2180 * system memory (i.e., it allows XE_PL_TT placement), FlatCCS
2181 * can't be used since there's no CCS storage associated with
2182 * non-VRAM addresses.
2184 if (IS_DGFX(xe) && (bo->flags & XE_BO_CREATE_SYSTEM_BIT))
2191 * __xe_bo_release_dummy() - Dummy kref release function
2192 * @kref: The embedded struct kref.
2194 * Dummy release function for xe_bo_put_deferred(). Keep off.
2196 void __xe_bo_release_dummy(struct kref *kref)
2201 * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred().
2202 * @deferred: The lockless list used for the call to xe_bo_put_deferred().
2204 * Puts all bos whose put was deferred by xe_bo_put_deferred().
2205 * The @deferred list can be either an onstack local list or a global
2206 * shared list used by a workqueue.
2208 void xe_bo_put_commit(struct llist_head *deferred)
2210 struct llist_node *freed;
2211 struct xe_bo *bo, *next;
2216 freed = llist_del_all(deferred);
2220 llist_for_each_entry_safe(bo, next, freed, freed)
2221 drm_gem_object_free(&bo->ttm.base.refcount);
2225 * xe_bo_dumb_create - Create a dumb bo as backing for a fb
2230 * See dumb_create() hook in include/drm/drm_drv.h
2234 int xe_bo_dumb_create(struct drm_file *file_priv,
2235 struct drm_device *dev,
2236 struct drm_mode_create_dumb *args)
2238 struct xe_device *xe = to_xe_device(dev);
2241 int cpp = DIV_ROUND_UP(args->bpp, 8);
2243 u32 page_size = max_t(u32, PAGE_SIZE,
2244 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K);
2246 args->pitch = ALIGN(args->width * cpp, 64);
2247 args->size = ALIGN(mul_u32_u32(args->pitch, args->height),
2250 bo = xe_bo_create_user(xe, NULL, NULL, args->size,
2251 DRM_XE_GEM_CPU_CACHING_WC,
2253 XE_BO_CREATE_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
2254 XE_BO_CREATE_USER_BIT | XE_BO_SCANOUT_BIT |
2255 XE_BO_NEEDS_CPU_ACCESS);
2259 err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle);
2260 /* drop reference from allocate - handle holds it now */
2261 drm_gem_object_put(&bo->ttm.base);
2263 args->handle = handle;
2267 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
2268 #include "tests/xe_bo.c"