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 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
130 if (bo_flags & XE_BO_CREATE_SYSTEM_BIT) {
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 vram = to_xe_ttm_vram_mgr(ttm_manager_type(&xe->ttm, mem_type))->vram;
149 xe_assert(xe, vram && vram->usable_size);
150 io_size = vram->io_size;
153 * For eviction / restore on suspend / resume objects
154 * pinned in VRAM must be contiguous
156 if (bo_flags & (XE_BO_CREATE_PINNED_BIT |
157 XE_BO_CREATE_GGTT_BIT))
158 place.flags |= TTM_PL_FLAG_CONTIGUOUS;
160 if (io_size < vram->usable_size) {
161 if (bo_flags & XE_BO_NEEDS_CPU_ACCESS) {
163 place.lpfn = io_size >> PAGE_SHIFT;
165 place.flags |= TTM_PL_FLAG_TOPDOWN;
171 if (bo->props.preferred_mem_type == XE_BO_PROPS_INVALID)
172 bo->props.preferred_mem_type = mem_type;
175 static void try_add_vram(struct xe_device *xe, struct xe_bo *bo,
176 u32 bo_flags, u32 *c)
178 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
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 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
198 if (bo_flags & XE_BO_CREATE_STOLEN_BIT) {
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)
252 if (!xe_bo_is_xe_bo(tbo)) {
253 /* Don't handle scatter gather BOs */
254 if (tbo->type == ttm_bo_type_sg) {
255 placement->num_placement = 0;
256 placement->num_busy_placement = 0;
260 *placement = sys_placement;
265 * For xe, sg bos that are evicted to system just triggers a
266 * rebind of the sg list upon subsequent validation to XE_PL_TT.
269 bo = ttm_to_xe_bo(tbo);
270 switch (tbo->resource->mem_type) {
274 *placement = tt_placement;
278 *placement = sys_placement;
290 static int xe_tt_map_sg(struct ttm_tt *tt)
292 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
293 unsigned long num_pages = tt->num_pages;
296 XE_WARN_ON(tt->page_flags & TTM_TT_FLAG_EXTERNAL);
301 ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages,
303 (u64)num_pages << PAGE_SHIFT,
304 xe_sg_segment_size(xe_tt->dev),
309 xe_tt->sg = &xe_tt->sgt;
310 ret = dma_map_sgtable(xe_tt->dev, xe_tt->sg, DMA_BIDIRECTIONAL,
311 DMA_ATTR_SKIP_CPU_SYNC);
313 sg_free_table(xe_tt->sg);
321 struct sg_table *xe_bo_sg(struct xe_bo *bo)
323 struct ttm_tt *tt = bo->ttm.ttm;
324 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
329 static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
332 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
333 struct xe_device *xe = xe_bo_device(bo);
334 struct xe_ttm_tt *tt;
335 unsigned long extra_pages;
336 enum ttm_caching caching;
339 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
343 tt->dev = xe->drm.dev;
346 if (xe_bo_needs_ccs_pages(bo))
347 extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, bo->size),
350 switch (bo->cpu_caching) {
351 case DRM_XE_GEM_CPU_CACHING_WC:
352 caching = ttm_write_combined;
355 caching = ttm_cached;
359 WARN_ON((bo->flags & XE_BO_CREATE_USER_BIT) && !bo->cpu_caching);
362 * Display scanout is always non-coherent with the CPU cache.
364 * For Xe_LPG and beyond, PPGTT PTE lookups are also non-coherent and
365 * require a CPU:WC mapping.
367 if ((!bo->cpu_caching && bo->flags & XE_BO_SCANOUT_BIT) ||
368 (xe->info.graphics_verx100 >= 1270 && bo->flags & XE_BO_PAGETABLE))
369 caching = ttm_write_combined;
371 err = ttm_tt_init(&tt->ttm, &bo->ttm, page_flags, caching, extra_pages);
380 static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt,
381 struct ttm_operation_ctx *ctx)
386 * dma-bufs are not populated with pages, and the dma-
387 * addresses are set up when moved to XE_PL_TT.
389 if (tt->page_flags & TTM_TT_FLAG_EXTERNAL)
392 err = ttm_pool_alloc(&ttm_dev->pool, tt, ctx);
396 /* A follow up may move this xe_bo_move when BO is moved to XE_PL_TT */
397 err = xe_tt_map_sg(tt);
399 ttm_pool_free(&ttm_dev->pool, tt);
404 static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
406 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
408 if (tt->page_flags & TTM_TT_FLAG_EXTERNAL)
412 dma_unmap_sgtable(xe_tt->dev, xe_tt->sg,
413 DMA_BIDIRECTIONAL, 0);
414 sg_free_table(xe_tt->sg);
418 return ttm_pool_free(&ttm_dev->pool, tt);
421 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
427 static int xe_ttm_io_mem_reserve(struct ttm_device *bdev,
428 struct ttm_resource *mem)
430 struct xe_device *xe = ttm_to_xe_device(bdev);
432 switch (mem->mem_type) {
438 struct xe_ttm_vram_mgr_resource *vres =
439 to_xe_ttm_vram_mgr_resource(mem);
440 struct xe_mem_region *vram = res_to_mem_region(mem);
442 if (vres->used_visible_size < mem->size)
445 mem->bus.offset = mem->start << PAGE_SHIFT;
448 mem->placement & TTM_PL_FLAG_CONTIGUOUS)
449 mem->bus.addr = (u8 *)vram->mapping +
452 mem->bus.offset += vram->io_start;
453 mem->bus.is_iomem = true;
455 #if !defined(CONFIG_X86)
456 mem->bus.caching = ttm_write_combined;
460 return xe_ttm_stolen_io_mem_reserve(xe, mem);
466 static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
467 const struct ttm_operation_ctx *ctx)
469 struct dma_resv_iter cursor;
470 struct dma_fence *fence;
471 struct drm_gem_object *obj = &bo->ttm.base;
472 struct drm_gpuvm_bo *vm_bo;
476 dma_resv_assert_held(bo->ttm.base.resv);
478 if (!list_empty(&bo->ttm.base.gpuva.list)) {
479 dma_resv_iter_begin(&cursor, bo->ttm.base.resv,
480 DMA_RESV_USAGE_BOOKKEEP);
481 dma_resv_for_each_fence_unlocked(&cursor, fence)
482 dma_fence_enable_sw_signaling(fence);
483 dma_resv_iter_end(&cursor);
486 drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
487 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
488 struct drm_gpuva *gpuva;
490 if (!xe_vm_in_fault_mode(vm)) {
491 drm_gpuvm_bo_evict(vm_bo, true);
498 if (ctx->no_wait_gpu &&
499 !dma_resv_test_signaled(bo->ttm.base.resv,
500 DMA_RESV_USAGE_BOOKKEEP))
503 timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
504 DMA_RESV_USAGE_BOOKKEEP,
506 MAX_SCHEDULE_TIMEOUT);
515 drm_gpuvm_bo_for_each_va(gpuva, vm_bo) {
516 struct xe_vma *vma = gpuva_to_vma(gpuva);
518 trace_xe_vma_evict(vma);
519 ret = xe_vm_invalidate_vma(vma);
529 * The dma-buf map_attachment() / unmap_attachment() is hooked up here.
530 * Note that unmapping the attachment is deferred to the next
531 * map_attachment time, or to bo destroy (after idling) whichever comes first.
532 * This is to avoid syncing before unmap_attachment(), assuming that the
533 * caller relies on idling the reservation object before moving the
534 * backing store out. Should that assumption not hold, then we will be able
535 * to unconditionally call unmap_attachment() when moving out to system.
537 static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
538 struct ttm_resource *new_res)
540 struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
541 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
543 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
546 xe_assert(xe, attach);
547 xe_assert(xe, ttm_bo->ttm);
549 if (new_res->mem_type == XE_PL_SYSTEM)
553 dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
557 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
565 ttm_bo_move_null(ttm_bo, new_res);
571 * xe_bo_move_notify - Notify subsystems of a pending move
572 * @bo: The buffer object
573 * @ctx: The struct ttm_operation_ctx controlling locking and waits.
575 * This function notifies subsystems of an upcoming buffer move.
576 * Upon receiving such a notification, subsystems should schedule
577 * halting access to the underlying pages and optionally add a fence
578 * to the buffer object's dma_resv object, that signals when access is
579 * stopped. The caller will wait on all dma_resv fences before
582 * A subsystem may commence access to the object after obtaining
583 * bindings to the new backing memory under the object lock.
585 * Return: 0 on success, -EINTR or -ERESTARTSYS if interrupted in fault mode,
586 * negative error code on error.
588 static int xe_bo_move_notify(struct xe_bo *bo,
589 const struct ttm_operation_ctx *ctx)
591 struct ttm_buffer_object *ttm_bo = &bo->ttm;
592 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
596 * If this starts to call into many components, consider
597 * using a notification chain here.
600 if (xe_bo_is_pinned(bo))
604 ret = xe_bo_trigger_rebind(xe, bo, ctx);
608 /* Don't call move_notify() for imported dma-bufs. */
609 if (ttm_bo->base.dma_buf && !ttm_bo->base.import_attach)
610 dma_buf_move_notify(ttm_bo->base.dma_buf);
615 static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
616 struct ttm_operation_ctx *ctx,
617 struct ttm_resource *new_mem,
618 struct ttm_place *hop)
620 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
621 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
622 struct ttm_resource *old_mem = ttm_bo->resource;
623 u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM;
624 struct ttm_tt *ttm = ttm_bo->ttm;
625 struct xe_migrate *migrate = NULL;
626 struct dma_fence *fence;
627 bool move_lacks_source;
632 /* Bo creation path, moving to system or TT. No clearing required. */
633 if (!old_mem && ttm) {
634 ttm_bo_move_null(ttm_bo, new_mem);
638 if (ttm_bo->type == ttm_bo_type_sg) {
639 ret = xe_bo_move_notify(bo, ctx);
641 ret = xe_bo_move_dmabuf(ttm_bo, new_mem);
645 tt_has_data = ttm && (ttm_tt_is_populated(ttm) ||
646 (ttm->page_flags & TTM_TT_FLAG_SWAPPED));
648 move_lacks_source = !mem_type_is_vram(old_mem_type) && !tt_has_data;
650 needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
651 (!ttm && ttm_bo->type == ttm_bo_type_device);
653 if ((move_lacks_source && !needs_clear) ||
654 (old_mem_type == XE_PL_SYSTEM &&
655 new_mem->mem_type == XE_PL_TT)) {
656 ttm_bo_move_null(ttm_bo, new_mem);
661 * Failed multi-hop where the old_mem is still marked as
662 * TTM_PL_FLAG_TEMPORARY, should just be a dummy move.
664 if (old_mem_type == XE_PL_TT &&
665 new_mem->mem_type == XE_PL_TT) {
666 ttm_bo_move_null(ttm_bo, new_mem);
670 if (!move_lacks_source && !xe_bo_is_pinned(bo)) {
671 ret = xe_bo_move_notify(bo, ctx);
676 if (old_mem_type == XE_PL_TT &&
677 new_mem->mem_type == XE_PL_SYSTEM) {
678 long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
679 DMA_RESV_USAGE_BOOKKEEP,
681 MAX_SCHEDULE_TIMEOUT);
686 ttm_bo_move_null(ttm_bo, new_mem);
690 if (!move_lacks_source &&
691 ((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) ||
692 (mem_type_is_vram(old_mem_type) &&
693 new_mem->mem_type == XE_PL_SYSTEM))) {
696 hop->mem_type = XE_PL_TT;
697 hop->flags = TTM_PL_FLAG_TEMPORARY;
703 migrate = bo->tile->migrate;
704 else if (resource_is_vram(new_mem))
705 migrate = mem_type_to_migrate(xe, new_mem->mem_type);
706 else if (mem_type_is_vram(old_mem_type))
707 migrate = mem_type_to_migrate(xe, old_mem_type);
709 xe_assert(xe, migrate);
711 trace_xe_bo_move(bo);
712 xe_device_mem_access_get(xe);
714 if (xe_bo_is_pinned(bo) && !xe_bo_is_user(bo)) {
716 * Kernel memory that is pinned should only be moved on suspend
717 * / resume, some of the pinned memory is required for the
718 * device to resume / use the GPU to move other evicted memory
719 * (user memory) around. This likely could be optimized a bit
720 * futher where we find the minimum set of pinned memory
721 * required for resume but for simplity doing a memcpy for all
724 ret = xe_bo_vmap(bo);
726 ret = ttm_bo_move_memcpy(ttm_bo, ctx, new_mem);
728 /* Create a new VMAP once kernel BO back in VRAM */
729 if (!ret && resource_is_vram(new_mem)) {
730 struct xe_mem_region *vram = res_to_mem_region(new_mem);
731 void *new_addr = vram->mapping +
732 (new_mem->start << PAGE_SHIFT);
734 if (XE_WARN_ON(new_mem->start == XE_BO_INVALID_OFFSET)) {
736 xe_device_mem_access_put(xe);
740 xe_assert(xe, new_mem->start ==
741 bo->placements->fpfn);
743 iosys_map_set_vaddr_iomem(&bo->vmap, new_addr);
747 if (move_lacks_source)
748 fence = xe_migrate_clear(migrate, bo, new_mem);
750 fence = xe_migrate_copy(migrate,
751 bo, bo, old_mem, new_mem);
753 ret = PTR_ERR(fence);
754 xe_device_mem_access_put(xe);
757 if (!move_lacks_source) {
758 ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict,
761 dma_fence_wait(fence, false);
762 ttm_bo_move_null(ttm_bo, new_mem);
767 * ttm_bo_move_accel_cleanup() may blow up if
768 * bo->resource == NULL, so just attach the
769 * fence and set the new resource.
771 dma_resv_add_fence(ttm_bo->base.resv, fence,
772 DMA_RESV_USAGE_KERNEL);
773 ttm_bo_move_null(ttm_bo, new_mem);
776 dma_fence_put(fence);
779 xe_device_mem_access_put(xe);
787 * xe_bo_evict_pinned() - Evict a pinned VRAM object to system memory
788 * @bo: The buffer object to move.
790 * On successful completion, the object memory will be moved to sytem memory.
791 * This function blocks until the object has been fully moved.
793 * This is needed to for special handling of pinned VRAM object during
796 * Return: 0 on success. Negative error code on failure.
798 int xe_bo_evict_pinned(struct xe_bo *bo)
800 struct ttm_place place = {
801 .mem_type = XE_PL_TT,
803 struct ttm_placement placement = {
807 struct ttm_operation_ctx ctx = {
808 .interruptible = false,
810 struct ttm_resource *new_mem;
813 xe_bo_assert_held(bo);
815 if (WARN_ON(!bo->ttm.resource))
818 if (WARN_ON(!xe_bo_is_pinned(bo)))
821 if (WARN_ON(!xe_bo_is_vram(bo)))
824 ret = ttm_bo_mem_space(&bo->ttm, &placement, &new_mem, &ctx);
829 bo->ttm.ttm = xe_ttm_tt_create(&bo->ttm, 0);
836 ret = ttm_tt_populate(bo->ttm.bdev, bo->ttm.ttm, &ctx);
840 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
844 ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
848 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
849 false, MAX_SCHEDULE_TIMEOUT);
854 ttm_resource_free(&bo->ttm, &new_mem);
859 * xe_bo_restore_pinned() - Restore a pinned VRAM object
860 * @bo: The buffer object to move.
862 * On successful completion, the object memory will be moved back to VRAM.
863 * This function blocks until the object has been fully moved.
865 * This is needed to for special handling of pinned VRAM object during
868 * Return: 0 on success. Negative error code on failure.
870 int xe_bo_restore_pinned(struct xe_bo *bo)
872 struct ttm_operation_ctx ctx = {
873 .interruptible = false,
875 struct ttm_resource *new_mem;
878 xe_bo_assert_held(bo);
880 if (WARN_ON(!bo->ttm.resource))
883 if (WARN_ON(!xe_bo_is_pinned(bo)))
886 if (WARN_ON(xe_bo_is_vram(bo) || !bo->ttm.ttm))
889 ret = ttm_bo_mem_space(&bo->ttm, &bo->placement, &new_mem, &ctx);
893 ret = ttm_tt_populate(bo->ttm.bdev, bo->ttm.ttm, &ctx);
897 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
901 ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
905 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
906 false, MAX_SCHEDULE_TIMEOUT);
911 ttm_resource_free(&bo->ttm, &new_mem);
915 static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo,
916 unsigned long page_offset)
918 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
919 struct xe_res_cursor cursor;
920 struct xe_mem_region *vram;
922 if (ttm_bo->resource->mem_type == XE_PL_STOLEN)
923 return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT;
925 vram = res_to_mem_region(ttm_bo->resource);
926 xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor);
927 return (vram->io_start + cursor.start) >> PAGE_SHIFT;
930 static void __xe_bo_vunmap(struct xe_bo *bo);
933 * TODO: Move this function to TTM so we don't rely on how TTM does its
934 * locking, thereby abusing TTM internals.
936 static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo)
938 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
941 xe_assert(xe, !kref_read(&ttm_bo->kref));
944 * We can typically only race with TTM trylocking under the
945 * lru_lock, which will immediately be unlocked again since
946 * the ttm_bo refcount is zero at this point. So trylocking *should*
947 * always succeed here, as long as we hold the lru lock.
949 spin_lock(&ttm_bo->bdev->lru_lock);
950 locked = dma_resv_trylock(ttm_bo->base.resv);
951 spin_unlock(&ttm_bo->bdev->lru_lock);
952 xe_assert(xe, locked);
957 static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
959 struct dma_resv_iter cursor;
960 struct dma_fence *fence;
961 struct dma_fence *replacement = NULL;
964 if (!xe_bo_is_xe_bo(ttm_bo))
967 bo = ttm_to_xe_bo(ttm_bo);
968 xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount)));
971 * Corner case where TTM fails to allocate memory and this BOs resv
972 * still points the VMs resv
974 if (ttm_bo->base.resv != &ttm_bo->base._resv)
977 if (!xe_ttm_bo_lock_in_destructor(ttm_bo))
981 * Scrub the preempt fences if any. The unbind fence is already
982 * attached to the resv.
983 * TODO: Don't do this for external bos once we scrub them after
986 dma_resv_for_each_fence(&cursor, ttm_bo->base.resv,
987 DMA_RESV_USAGE_BOOKKEEP, fence) {
988 if (xe_fence_is_xe_preempt(fence) &&
989 !dma_fence_is_signaled(fence)) {
991 replacement = dma_fence_get_stub();
993 dma_resv_replace_fences(ttm_bo->base.resv,
996 DMA_RESV_USAGE_BOOKKEEP);
999 dma_fence_put(replacement);
1001 dma_resv_unlock(ttm_bo->base.resv);
1004 static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
1006 if (!xe_bo_is_xe_bo(ttm_bo))
1010 * Object is idle and about to be destroyed. Release the
1011 * dma-buf attachment.
1013 if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
1014 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
1015 struct xe_ttm_tt, ttm);
1017 dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
1024 struct ttm_device_funcs xe_ttm_funcs = {
1025 .ttm_tt_create = xe_ttm_tt_create,
1026 .ttm_tt_populate = xe_ttm_tt_populate,
1027 .ttm_tt_unpopulate = xe_ttm_tt_unpopulate,
1028 .ttm_tt_destroy = xe_ttm_tt_destroy,
1029 .evict_flags = xe_evict_flags,
1031 .io_mem_reserve = xe_ttm_io_mem_reserve,
1032 .io_mem_pfn = xe_ttm_io_mem_pfn,
1033 .release_notify = xe_ttm_bo_release_notify,
1034 .eviction_valuable = ttm_bo_eviction_valuable,
1035 .delete_mem_notify = xe_ttm_bo_delete_mem_notify,
1038 static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo)
1040 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1041 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1043 if (bo->ttm.base.import_attach)
1044 drm_prime_gem_destroy(&bo->ttm.base, NULL);
1045 drm_gem_object_release(&bo->ttm.base);
1047 xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list));
1049 if (bo->ggtt_node.size)
1050 xe_ggtt_remove_bo(bo->tile->mem.ggtt, bo);
1052 #ifdef CONFIG_PROC_FS
1054 xe_drm_client_remove_bo(bo);
1057 if (bo->vm && xe_bo_is_user(bo))
1063 static void xe_gem_object_free(struct drm_gem_object *obj)
1065 /* Our BO reference counting scheme works as follows:
1067 * The gem object kref is typically used throughout the driver,
1068 * and the gem object holds a ttm_buffer_object refcount, so
1069 * that when the last gem object reference is put, which is when
1070 * we end up in this function, we put also that ttm_buffer_object
1071 * refcount. Anything using gem interfaces is then no longer
1072 * allowed to access the object in a way that requires a gem
1073 * refcount, including locking the object.
1075 * driver ttm callbacks is allowed to use the ttm_buffer_object
1076 * refcount directly if needed.
1078 __xe_bo_vunmap(gem_to_xe_bo(obj));
1079 ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
1082 static void xe_gem_object_close(struct drm_gem_object *obj,
1083 struct drm_file *file_priv)
1085 struct xe_bo *bo = gem_to_xe_bo(obj);
1087 if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) {
1088 xe_assert(xe_bo_device(bo), xe_bo_is_user(bo));
1090 xe_bo_lock(bo, false);
1091 ttm_bo_set_bulk_move(&bo->ttm, NULL);
1096 static bool should_migrate_to_system(struct xe_bo *bo)
1098 struct xe_device *xe = xe_bo_device(bo);
1100 return xe_device_in_fault_mode(xe) && bo->props.cpu_atomic;
1103 static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
1105 struct ttm_buffer_object *tbo = vmf->vma->vm_private_data;
1106 struct drm_device *ddev = tbo->base.dev;
1110 ret = ttm_bo_vm_reserve(tbo, vmf);
1114 if (drm_dev_enter(ddev, &idx)) {
1115 struct xe_bo *bo = ttm_to_xe_bo(tbo);
1117 trace_xe_bo_cpu_fault(bo);
1119 if (should_migrate_to_system(bo)) {
1120 r = xe_bo_migrate(bo, XE_PL_TT);
1121 if (r == -EBUSY || r == -ERESTARTSYS || r == -EINTR)
1122 ret = VM_FAULT_NOPAGE;
1124 ret = VM_FAULT_SIGBUS;
1127 ret = ttm_bo_vm_fault_reserved(vmf,
1128 vmf->vma->vm_page_prot,
1129 TTM_BO_VM_NUM_PREFAULT);
1132 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1134 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1137 dma_resv_unlock(tbo->base.resv);
1141 static const struct vm_operations_struct xe_gem_vm_ops = {
1142 .fault = xe_gem_fault,
1143 .open = ttm_bo_vm_open,
1144 .close = ttm_bo_vm_close,
1145 .access = ttm_bo_vm_access
1148 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
1149 .free = xe_gem_object_free,
1150 .close = xe_gem_object_close,
1151 .mmap = drm_gem_ttm_mmap,
1152 .export = xe_gem_prime_export,
1153 .vm_ops = &xe_gem_vm_ops,
1157 * xe_bo_alloc - Allocate storage for a struct xe_bo
1159 * This funcition is intended to allocate storage to be used for input
1160 * to __xe_bo_create_locked(), in the case a pointer to the bo to be
1161 * created is needed before the call to __xe_bo_create_locked().
1162 * If __xe_bo_create_locked ends up never to be called, then the
1163 * storage allocated with this function needs to be freed using
1166 * Return: A pointer to an uninitialized struct xe_bo on success,
1167 * ERR_PTR(-ENOMEM) on error.
1169 struct xe_bo *xe_bo_alloc(void)
1171 struct xe_bo *bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1174 return ERR_PTR(-ENOMEM);
1180 * xe_bo_free - Free storage allocated using xe_bo_alloc()
1181 * @bo: The buffer object storage.
1183 * Refer to xe_bo_alloc() documentation for valid use-cases.
1185 void xe_bo_free(struct xe_bo *bo)
1190 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
1191 struct xe_tile *tile, struct dma_resv *resv,
1192 struct ttm_lru_bulk_move *bulk, size_t size,
1193 u16 cpu_caching, enum ttm_bo_type type,
1196 struct ttm_operation_ctx ctx = {
1197 .interruptible = true,
1198 .no_wait_gpu = false,
1200 struct ttm_placement *placement;
1202 size_t aligned_size;
1205 /* Only kernel objects should set GT */
1206 xe_assert(xe, !tile || type == ttm_bo_type_kernel);
1208 if (XE_WARN_ON(!size)) {
1210 return ERR_PTR(-EINVAL);
1213 if (flags & (XE_BO_CREATE_VRAM_MASK | XE_BO_CREATE_STOLEN_BIT) &&
1214 !(flags & XE_BO_CREATE_IGNORE_MIN_PAGE_SIZE_BIT) &&
1215 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) {
1216 aligned_size = ALIGN(size, SZ_64K);
1217 if (type != ttm_bo_type_device)
1218 size = ALIGN(size, SZ_64K);
1219 flags |= XE_BO_INTERNAL_64K;
1220 alignment = SZ_64K >> PAGE_SHIFT;
1223 aligned_size = ALIGN(size, SZ_4K);
1224 flags &= ~XE_BO_INTERNAL_64K;
1225 alignment = SZ_4K >> PAGE_SHIFT;
1228 if (type == ttm_bo_type_device && aligned_size != size)
1229 return ERR_PTR(-EINVAL);
1240 bo->cpu_caching = cpu_caching;
1241 bo->ttm.base.funcs = &xe_gem_object_funcs;
1242 bo->props.preferred_mem_class = XE_BO_PROPS_INVALID;
1243 bo->props.preferred_gt = XE_BO_PROPS_INVALID;
1244 bo->props.preferred_mem_type = XE_BO_PROPS_INVALID;
1245 bo->ttm.priority = XE_BO_PRIORITY_NORMAL;
1246 INIT_LIST_HEAD(&bo->pinned_link);
1247 #ifdef CONFIG_PROC_FS
1248 INIT_LIST_HEAD(&bo->client_link);
1251 drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size);
1254 ctx.allow_res_evict = !(flags & XE_BO_CREATE_NO_RESV_EVICT);
1258 if (!(flags & XE_BO_FIXED_PLACEMENT_BIT)) {
1259 err = __xe_bo_placement_for_flags(xe, bo, bo->flags);
1261 xe_ttm_bo_destroy(&bo->ttm);
1262 return ERR_PTR(err);
1266 /* Defer populating type_sg bos */
1267 placement = (type == ttm_bo_type_sg ||
1268 bo->flags & XE_BO_DEFER_BACKING) ? &sys_placement :
1270 err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type,
1271 placement, alignment,
1272 &ctx, NULL, resv, xe_ttm_bo_destroy);
1274 return ERR_PTR(err);
1277 * The VRAM pages underneath are potentially still being accessed by the
1278 * GPU, as per async GPU clearing and async evictions. However TTM makes
1279 * sure to add any corresponding move/clear fences into the objects
1280 * dma-resv using the DMA_RESV_USAGE_KERNEL slot.
1282 * For KMD internal buffers we don't care about GPU clearing, however we
1283 * still need to handle async evictions, where the VRAM is still being
1284 * accessed by the GPU. Most internal callers are not expecting this,
1285 * since they are missing the required synchronisation before accessing
1286 * the memory. To keep things simple just sync wait any kernel fences
1287 * here, if the buffer is designated KMD internal.
1289 * For normal userspace objects we should already have the required
1290 * pipelining or sync waiting elsewhere, since we already have to deal
1291 * with things like async GPU clearing.
1293 if (type == ttm_bo_type_kernel) {
1294 long timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
1295 DMA_RESV_USAGE_KERNEL,
1297 MAX_SCHEDULE_TIMEOUT);
1301 dma_resv_unlock(bo->ttm.base.resv);
1303 return ERR_PTR(timeout);
1309 ttm_bo_set_bulk_move(&bo->ttm, bulk);
1311 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1316 static int __xe_bo_fixed_placement(struct xe_device *xe,
1319 u64 start, u64 end, u64 size)
1321 struct ttm_place *place = bo->placements;
1323 if (flags & (XE_BO_CREATE_USER_BIT|XE_BO_CREATE_SYSTEM_BIT))
1326 place->flags = TTM_PL_FLAG_CONTIGUOUS;
1327 place->fpfn = start >> PAGE_SHIFT;
1328 place->lpfn = end >> PAGE_SHIFT;
1330 switch (flags & (XE_BO_CREATE_STOLEN_BIT | XE_BO_CREATE_VRAM_MASK)) {
1331 case XE_BO_CREATE_VRAM0_BIT:
1332 place->mem_type = XE_PL_VRAM0;
1334 case XE_BO_CREATE_VRAM1_BIT:
1335 place->mem_type = XE_PL_VRAM1;
1337 case XE_BO_CREATE_STOLEN_BIT:
1338 place->mem_type = XE_PL_STOLEN;
1342 /* 0 or multiple of the above set */
1346 bo->placement = (struct ttm_placement) {
1349 .num_busy_placement = 1,
1350 .busy_placement = place,
1356 static struct xe_bo *
1357 __xe_bo_create_locked(struct xe_device *xe,
1358 struct xe_tile *tile, struct xe_vm *vm,
1359 size_t size, u64 start, u64 end,
1360 u16 cpu_caching, enum ttm_bo_type type, u32 flags)
1362 struct xe_bo *bo = NULL;
1366 xe_vm_assert_held(vm);
1368 if (start || end != ~0ULL) {
1373 flags |= XE_BO_FIXED_PLACEMENT_BIT;
1374 err = __xe_bo_fixed_placement(xe, bo, flags, start, end, size);
1377 return ERR_PTR(err);
1381 bo = ___xe_bo_create_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL,
1382 vm && !xe_vm_in_fault_mode(vm) &&
1383 flags & XE_BO_CREATE_USER_BIT ?
1384 &vm->lru_bulk_move : NULL, size,
1385 cpu_caching, type, flags);
1390 * Note that instead of taking a reference no the drm_gpuvm_resv_bo(),
1391 * to ensure the shared resv doesn't disappear under the bo, the bo
1392 * will keep a reference to the vm, and avoid circular references
1393 * by having all the vm's bo refereferences released at vm close
1396 if (vm && xe_bo_is_user(bo))
1400 if (bo->flags & XE_BO_CREATE_GGTT_BIT) {
1401 if (!tile && flags & XE_BO_CREATE_STOLEN_BIT)
1402 tile = xe_device_get_root_tile(xe);
1404 xe_assert(xe, tile);
1406 if (flags & XE_BO_FIXED_PLACEMENT_BIT) {
1407 err = xe_ggtt_insert_bo_at(tile->mem.ggtt, bo,
1408 start + bo->size, U64_MAX);
1410 err = xe_ggtt_insert_bo(tile->mem.ggtt, bo);
1413 goto err_unlock_put_bo;
1419 __xe_bo_unset_bulk_move(bo);
1420 xe_bo_unlock_vm_held(bo);
1422 return ERR_PTR(err);
1426 xe_bo_create_locked_range(struct xe_device *xe,
1427 struct xe_tile *tile, struct xe_vm *vm,
1428 size_t size, u64 start, u64 end,
1429 enum ttm_bo_type type, u32 flags)
1431 return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type, flags);
1434 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
1435 struct xe_vm *vm, size_t size,
1436 enum ttm_bo_type type, u32 flags)
1438 return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type, flags);
1441 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
1442 struct xe_vm *vm, size_t size,
1444 enum ttm_bo_type type,
1447 struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL,
1449 flags | XE_BO_CREATE_USER_BIT);
1451 xe_bo_unlock_vm_held(bo);
1456 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
1457 struct xe_vm *vm, size_t size,
1458 enum ttm_bo_type type, u32 flags)
1460 struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags);
1463 xe_bo_unlock_vm_held(bo);
1468 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
1470 size_t size, u64 offset,
1471 enum ttm_bo_type type, u32 flags)
1475 u64 start = offset == ~0ull ? 0 : offset;
1476 u64 end = offset == ~0ull ? offset : start + size;
1478 if (flags & XE_BO_CREATE_STOLEN_BIT &&
1479 xe_ttm_stolen_cpu_access_needs_ggtt(xe))
1480 flags |= XE_BO_CREATE_GGTT_BIT;
1482 bo = xe_bo_create_locked_range(xe, tile, vm, size, start, end, type,
1483 flags | XE_BO_NEEDS_CPU_ACCESS);
1487 err = xe_bo_pin(bo);
1491 err = xe_bo_vmap(bo);
1495 xe_bo_unlock_vm_held(bo);
1502 xe_bo_unlock_vm_held(bo);
1504 return ERR_PTR(err);
1507 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
1508 struct xe_vm *vm, size_t size,
1509 enum ttm_bo_type type, u32 flags)
1511 return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull, type, flags);
1514 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
1515 const void *data, size_t size,
1516 enum ttm_bo_type type, u32 flags)
1518 struct xe_bo *bo = xe_bo_create_pin_map(xe, tile, NULL,
1519 ALIGN(size, PAGE_SIZE),
1524 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
1529 static void __xe_bo_unpin_map_no_vm(struct drm_device *drm, void *arg)
1531 xe_bo_unpin_map_no_vm(arg);
1534 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
1535 size_t size, u32 flags)
1540 bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel, flags);
1544 ret = drmm_add_action_or_reset(&xe->drm, __xe_bo_unpin_map_no_vm, bo);
1546 return ERR_PTR(ret);
1551 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
1552 const void *data, size_t size, u32 flags)
1554 struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags);
1559 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
1565 * XXX: This is in the VM bind data path, likely should calculate this once and
1566 * store, with a recalculation if the BO is moved.
1568 uint64_t vram_region_gpu_offset(struct ttm_resource *res)
1570 struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
1572 if (res->mem_type == XE_PL_STOLEN)
1573 return xe_ttm_stolen_gpu_offset(xe);
1575 return res_to_mem_region(res)->dpa_base;
1579 * xe_bo_pin_external - pin an external BO
1580 * @bo: buffer object to be pinned
1582 * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD)
1583 * BO. Unique call compared to xe_bo_pin as this function has it own set of
1584 * asserts and code to ensure evict / restore on suspend / resume.
1586 * Returns 0 for success, negative error code otherwise.
1588 int xe_bo_pin_external(struct xe_bo *bo)
1590 struct xe_device *xe = xe_bo_device(bo);
1593 xe_assert(xe, !bo->vm);
1594 xe_assert(xe, xe_bo_is_user(bo));
1596 if (!xe_bo_is_pinned(bo)) {
1597 err = xe_bo_validate(bo, NULL, false);
1601 if (xe_bo_is_vram(bo)) {
1602 spin_lock(&xe->pinned.lock);
1603 list_add_tail(&bo->pinned_link,
1604 &xe->pinned.external_vram);
1605 spin_unlock(&xe->pinned.lock);
1609 ttm_bo_pin(&bo->ttm);
1612 * FIXME: If we always use the reserve / unreserve functions for locking
1613 * we do not need this.
1615 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1620 int xe_bo_pin(struct xe_bo *bo)
1622 struct xe_device *xe = xe_bo_device(bo);
1625 /* We currently don't expect user BO to be pinned */
1626 xe_assert(xe, !xe_bo_is_user(bo));
1628 /* Pinned object must be in GGTT or have pinned flag */
1629 xe_assert(xe, bo->flags & (XE_BO_CREATE_PINNED_BIT |
1630 XE_BO_CREATE_GGTT_BIT));
1633 * No reason we can't support pinning imported dma-bufs we just don't
1634 * expect to pin an imported dma-buf.
1636 xe_assert(xe, !bo->ttm.base.import_attach);
1638 /* We only expect at most 1 pin */
1639 xe_assert(xe, !xe_bo_is_pinned(bo));
1641 err = xe_bo_validate(bo, NULL, false);
1646 * For pinned objects in on DGFX, which are also in vram, we expect
1647 * these to be in contiguous VRAM memory. Required eviction / restore
1648 * during suspend / resume (force restore to same physical address).
1650 if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) &&
1651 bo->flags & XE_BO_INTERNAL_TEST)) {
1652 struct ttm_place *place = &(bo->placements[0]);
1654 if (mem_type_is_vram(place->mem_type)) {
1655 xe_assert(xe, place->flags & TTM_PL_FLAG_CONTIGUOUS);
1657 place->fpfn = (xe_bo_addr(bo, 0, PAGE_SIZE) -
1658 vram_region_gpu_offset(bo->ttm.resource)) >> PAGE_SHIFT;
1659 place->lpfn = place->fpfn + (bo->size >> PAGE_SHIFT);
1661 spin_lock(&xe->pinned.lock);
1662 list_add_tail(&bo->pinned_link, &xe->pinned.kernel_bo_present);
1663 spin_unlock(&xe->pinned.lock);
1667 ttm_bo_pin(&bo->ttm);
1670 * FIXME: If we always use the reserve / unreserve functions for locking
1671 * we do not need this.
1673 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1679 * xe_bo_unpin_external - unpin an external BO
1680 * @bo: buffer object to be unpinned
1682 * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD)
1683 * BO. Unique call compared to xe_bo_unpin as this function has it own set of
1684 * asserts and code to ensure evict / restore on suspend / resume.
1686 * Returns 0 for success, negative error code otherwise.
1688 void xe_bo_unpin_external(struct xe_bo *bo)
1690 struct xe_device *xe = xe_bo_device(bo);
1692 xe_assert(xe, !bo->vm);
1693 xe_assert(xe, xe_bo_is_pinned(bo));
1694 xe_assert(xe, xe_bo_is_user(bo));
1696 if (bo->ttm.pin_count == 1 && !list_empty(&bo->pinned_link)) {
1697 spin_lock(&xe->pinned.lock);
1698 list_del_init(&bo->pinned_link);
1699 spin_unlock(&xe->pinned.lock);
1702 ttm_bo_unpin(&bo->ttm);
1705 * FIXME: If we always use the reserve / unreserve functions for locking
1706 * we do not need this.
1708 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1711 void xe_bo_unpin(struct xe_bo *bo)
1713 struct xe_device *xe = xe_bo_device(bo);
1715 xe_assert(xe, !bo->ttm.base.import_attach);
1716 xe_assert(xe, xe_bo_is_pinned(bo));
1718 if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) &&
1719 bo->flags & XE_BO_INTERNAL_TEST)) {
1720 struct ttm_place *place = &(bo->placements[0]);
1722 if (mem_type_is_vram(place->mem_type)) {
1723 xe_assert(xe, !list_empty(&bo->pinned_link));
1725 spin_lock(&xe->pinned.lock);
1726 list_del_init(&bo->pinned_link);
1727 spin_unlock(&xe->pinned.lock);
1731 ttm_bo_unpin(&bo->ttm);
1735 * xe_bo_validate() - Make sure the bo is in an allowed placement
1737 * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or
1738 * NULL. Used together with @allow_res_evict.
1739 * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's
1740 * reservation object.
1742 * Make sure the bo is in allowed placement, migrating it if necessary. If
1743 * needed, other bos will be evicted. If bos selected for eviction shares
1744 * the @vm's reservation object, they can be evicted iff @allow_res_evict is
1745 * set to true, otherwise they will be bypassed.
1747 * Return: 0 on success, negative error code on failure. May return
1748 * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal.
1750 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict)
1752 struct ttm_operation_ctx ctx = {
1753 .interruptible = true,
1754 .no_wait_gpu = false,
1758 lockdep_assert_held(&vm->lock);
1759 xe_vm_assert_held(vm);
1761 ctx.allow_res_evict = allow_res_evict;
1762 ctx.resv = xe_vm_resv(vm);
1765 return ttm_bo_validate(&bo->ttm, &bo->placement, &ctx);
1768 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo)
1770 if (bo->destroy == &xe_ttm_bo_destroy)
1777 * Resolve a BO address. There is no assert to check if the proper lock is held
1778 * so it should only be used in cases where it is not fatal to get the wrong
1779 * address, such as printing debug information, but not in cases where memory is
1780 * written based on this result.
1782 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
1784 struct xe_device *xe = xe_bo_device(bo);
1785 struct xe_res_cursor cur;
1788 xe_assert(xe, page_size <= PAGE_SIZE);
1789 page = offset >> PAGE_SHIFT;
1790 offset &= (PAGE_SIZE - 1);
1792 if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
1793 xe_assert(xe, bo->ttm.ttm);
1795 xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
1797 return xe_res_dma(&cur) + offset;
1799 struct xe_res_cursor cur;
1801 xe_res_first(bo->ttm.resource, page << PAGE_SHIFT,
1803 return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource);
1807 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
1809 if (!READ_ONCE(bo->ttm.pin_count))
1810 xe_bo_assert_held(bo);
1811 return __xe_bo_addr(bo, offset, page_size);
1814 int xe_bo_vmap(struct xe_bo *bo)
1820 xe_bo_assert_held(bo);
1822 if (!(bo->flags & XE_BO_NEEDS_CPU_ACCESS))
1825 if (!iosys_map_is_null(&bo->vmap))
1829 * We use this more or less deprecated interface for now since
1830 * ttm_bo_vmap() doesn't offer the optimization of kmapping
1831 * single page bos, which is done here.
1832 * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap
1833 * to use struct iosys_map.
1835 ret = ttm_bo_kmap(&bo->ttm, 0, bo->size >> PAGE_SHIFT, &bo->kmap);
1839 virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
1841 iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual);
1843 iosys_map_set_vaddr(&bo->vmap, virtual);
1848 static void __xe_bo_vunmap(struct xe_bo *bo)
1850 if (!iosys_map_is_null(&bo->vmap)) {
1851 iosys_map_clear(&bo->vmap);
1852 ttm_bo_kunmap(&bo->kmap);
1856 void xe_bo_vunmap(struct xe_bo *bo)
1858 xe_bo_assert_held(bo);
1862 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
1863 struct drm_file *file)
1865 struct xe_device *xe = to_xe_device(dev);
1866 struct xe_file *xef = to_xe_file(file);
1867 struct drm_xe_gem_create *args = data;
1868 struct xe_vm *vm = NULL;
1870 unsigned int bo_flags;
1874 if (XE_IOCTL_DBG(xe, args->extensions) ||
1875 XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) ||
1876 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1879 /* at least one valid memory placement must be specified */
1880 if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) ||
1884 if (XE_IOCTL_DBG(xe, args->flags &
1885 ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING |
1886 DRM_XE_GEM_CREATE_FLAG_SCANOUT |
1887 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM)))
1890 if (XE_IOCTL_DBG(xe, args->handle))
1893 if (XE_IOCTL_DBG(xe, !args->size))
1896 if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX))
1899 if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK))
1903 if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING)
1904 bo_flags |= XE_BO_DEFER_BACKING;
1906 if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT)
1907 bo_flags |= XE_BO_SCANOUT_BIT;
1909 bo_flags |= args->placement << (ffs(XE_BO_CREATE_SYSTEM_BIT) - 1);
1911 if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) {
1912 if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_CREATE_VRAM_MASK)))
1915 bo_flags |= XE_BO_NEEDS_CPU_ACCESS;
1918 if (XE_IOCTL_DBG(xe, !args->cpu_caching ||
1919 args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC))
1922 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_CREATE_VRAM_MASK &&
1923 args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC))
1926 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_SCANOUT_BIT &&
1927 args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB))
1931 vm = xe_vm_lookup(xef, args->vm_id);
1932 if (XE_IOCTL_DBG(xe, !vm))
1934 err = xe_vm_lock(vm, true);
1939 bo = xe_bo_create_user(xe, NULL, vm, args->size, args->cpu_caching,
1940 ttm_bo_type_device, bo_flags);
1950 err = drm_gem_handle_create(file, &bo->ttm.base, &handle);
1954 args->handle = handle;
1958 if (vm && !xe_vm_in_fault_mode(vm)) {
1959 xe_vm_lock(vm, false);
1960 __xe_bo_unset_bulk_move(bo);
1972 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
1973 struct drm_file *file)
1975 struct xe_device *xe = to_xe_device(dev);
1976 struct drm_xe_gem_mmap_offset *args = data;
1977 struct drm_gem_object *gem_obj;
1979 if (XE_IOCTL_DBG(xe, args->extensions) ||
1980 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1983 if (XE_IOCTL_DBG(xe, args->flags))
1986 gem_obj = drm_gem_object_lookup(file, args->handle);
1987 if (XE_IOCTL_DBG(xe, !gem_obj))
1990 /* The mmap offset was set up at BO allocation time. */
1991 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
1993 xe_bo_put(gem_to_xe_bo(gem_obj));
1998 * xe_bo_lock() - Lock the buffer object's dma_resv object
1999 * @bo: The struct xe_bo whose lock is to be taken
2000 * @intr: Whether to perform any wait interruptible
2002 * Locks the buffer object's dma_resv object. If the buffer object is
2003 * pointing to a shared dma_resv object, that shared lock is locked.
2005 * Return: 0 on success, -EINTR if @intr is true and the wait for a
2006 * contended lock was interrupted. If @intr is set to false, the
2007 * function always returns 0.
2009 int xe_bo_lock(struct xe_bo *bo, bool intr)
2012 return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL);
2014 dma_resv_lock(bo->ttm.base.resv, NULL);
2020 * xe_bo_unlock() - Unlock the buffer object's dma_resv object
2021 * @bo: The struct xe_bo whose lock is to be released.
2023 * Unlock a buffer object lock that was locked by xe_bo_lock().
2025 void xe_bo_unlock(struct xe_bo *bo)
2027 dma_resv_unlock(bo->ttm.base.resv);
2031 * xe_bo_can_migrate - Whether a buffer object likely can be migrated
2032 * @bo: The buffer object to migrate
2033 * @mem_type: The TTM memory type intended to migrate to
2035 * Check whether the buffer object supports migration to the
2036 * given memory type. Note that pinning may affect the ability to migrate as
2037 * returned by this function.
2039 * This function is primarily intended as a helper for checking the
2040 * possibility to migrate buffer objects and can be called without
2041 * the object lock held.
2043 * Return: true if migration is possible, false otherwise.
2045 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type)
2047 unsigned int cur_place;
2049 if (bo->ttm.type == ttm_bo_type_kernel)
2052 if (bo->ttm.type == ttm_bo_type_sg)
2055 for (cur_place = 0; cur_place < bo->placement.num_placement;
2057 if (bo->placements[cur_place].mem_type == mem_type)
2064 static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
2066 memset(place, 0, sizeof(*place));
2067 place->mem_type = mem_type;
2071 * xe_bo_migrate - Migrate an object to the desired region id
2072 * @bo: The buffer object to migrate.
2073 * @mem_type: The TTM region type to migrate to.
2075 * Attempt to migrate the buffer object to the desired memory region. The
2076 * buffer object may not be pinned, and must be locked.
2077 * On successful completion, the object memory type will be updated,
2078 * but an async migration task may not have completed yet, and to
2079 * accomplish that, the object's kernel fences must be signaled with
2080 * the object lock held.
2082 * Return: 0 on success. Negative error code on failure. In particular may
2083 * return -EINTR or -ERESTARTSYS if signal pending.
2085 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type)
2087 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
2088 struct ttm_operation_ctx ctx = {
2089 .interruptible = true,
2090 .no_wait_gpu = false,
2092 struct ttm_placement placement;
2093 struct ttm_place requested;
2095 xe_bo_assert_held(bo);
2097 if (bo->ttm.resource->mem_type == mem_type)
2100 if (xe_bo_is_pinned(bo))
2103 if (!xe_bo_can_migrate(bo, mem_type))
2106 xe_place_from_ttm_type(mem_type, &requested);
2107 placement.num_placement = 1;
2108 placement.num_busy_placement = 1;
2109 placement.placement = &requested;
2110 placement.busy_placement = &requested;
2113 * Stolen needs to be handled like below VRAM handling if we ever need
2116 drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN);
2118 if (mem_type_is_vram(mem_type)) {
2121 add_vram(xe, bo, &requested, bo->flags, mem_type, &c);
2124 return ttm_bo_validate(&bo->ttm, &placement, &ctx);
2128 * xe_bo_evict - Evict an object to evict placement
2129 * @bo: The buffer object to migrate.
2130 * @force_alloc: Set force_alloc in ttm_operation_ctx
2132 * On successful completion, the object memory will be moved to evict
2133 * placement. Ths function blocks until the object has been fully moved.
2135 * Return: 0 on success. Negative error code on failure.
2137 int xe_bo_evict(struct xe_bo *bo, bool force_alloc)
2139 struct ttm_operation_ctx ctx = {
2140 .interruptible = false,
2141 .no_wait_gpu = false,
2142 .force_alloc = force_alloc,
2144 struct ttm_placement placement;
2147 xe_evict_flags(&bo->ttm, &placement);
2148 ret = ttm_bo_validate(&bo->ttm, &placement, &ctx);
2152 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
2153 false, MAX_SCHEDULE_TIMEOUT);
2159 * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when
2160 * placed in system memory.
2163 * Return: true if extra pages need to be allocated, false otherwise.
2165 bool xe_bo_needs_ccs_pages(struct xe_bo *bo)
2167 struct xe_device *xe = xe_bo_device(bo);
2169 if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device)
2172 /* On discrete GPUs, if the GPU can access this buffer from
2173 * system memory (i.e., it allows XE_PL_TT placement), FlatCCS
2174 * can't be used since there's no CCS storage associated with
2175 * non-VRAM addresses.
2177 if (IS_DGFX(xe) && (bo->flags & XE_BO_CREATE_SYSTEM_BIT))
2184 * __xe_bo_release_dummy() - Dummy kref release function
2185 * @kref: The embedded struct kref.
2187 * Dummy release function for xe_bo_put_deferred(). Keep off.
2189 void __xe_bo_release_dummy(struct kref *kref)
2194 * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred().
2195 * @deferred: The lockless list used for the call to xe_bo_put_deferred().
2197 * Puts all bos whose put was deferred by xe_bo_put_deferred().
2198 * The @deferred list can be either an onstack local list or a global
2199 * shared list used by a workqueue.
2201 void xe_bo_put_commit(struct llist_head *deferred)
2203 struct llist_node *freed;
2204 struct xe_bo *bo, *next;
2209 freed = llist_del_all(deferred);
2213 llist_for_each_entry_safe(bo, next, freed, freed)
2214 drm_gem_object_free(&bo->ttm.base.refcount);
2218 * xe_bo_dumb_create - Create a dumb bo as backing for a fb
2223 * See dumb_create() hook in include/drm/drm_drv.h
2227 int xe_bo_dumb_create(struct drm_file *file_priv,
2228 struct drm_device *dev,
2229 struct drm_mode_create_dumb *args)
2231 struct xe_device *xe = to_xe_device(dev);
2234 int cpp = DIV_ROUND_UP(args->bpp, 8);
2236 u32 page_size = max_t(u32, PAGE_SIZE,
2237 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K);
2239 args->pitch = ALIGN(args->width * cpp, 64);
2240 args->size = ALIGN(mul_u32_u32(args->pitch, args->height),
2243 bo = xe_bo_create_user(xe, NULL, NULL, args->size,
2244 DRM_XE_GEM_CPU_CACHING_WC,
2246 XE_BO_CREATE_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
2247 XE_BO_CREATE_USER_BIT | XE_BO_SCANOUT_BIT |
2248 XE_BO_NEEDS_CPU_ACCESS);
2252 err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle);
2253 /* drop reference from allocate - handle holds it now */
2254 drm_gem_object_put(&bo->ttm.base);
2256 args->handle = handle;
2260 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
2261 #include "tests/xe_bo.c"