1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2021 Intel Corporation
4 */
5
6 #include "xe_bo.h"
7
8 #include <linux/dma-buf.h>
9 #include <linux/nospec.h>
10
11 #include <drm/drm_drv.h>
12 #include <drm/drm_gem_ttm_helper.h>
13 #include <drm/drm_managed.h>
14 #include <drm/ttm/ttm_backup.h>
15 #include <drm/ttm/ttm_device.h>
16 #include <drm/ttm/ttm_placement.h>
17 #include <drm/ttm/ttm_tt.h>
18 #include <uapi/drm/xe_drm.h>
19
20 #include <kunit/static_stub.h>
21
22 #include "xe_device.h"
23 #include "xe_dma_buf.h"
24 #include "xe_drm_client.h"
25 #include "xe_ggtt.h"
26 #include "xe_gt.h"
27 #include "xe_map.h"
28 #include "xe_migrate.h"
29 #include "xe_pm.h"
30 #include "xe_preempt_fence.h"
31 #include "xe_pxp.h"
32 #include "xe_res_cursor.h"
33 #include "xe_shrinker.h"
34 #include "xe_trace_bo.h"
35 #include "xe_ttm_stolen_mgr.h"
36 #include "xe_vm.h"
37
38 const char *const xe_mem_type_to_name[TTM_NUM_MEM_TYPES] = {
39 [XE_PL_SYSTEM] = "system",
40 [XE_PL_TT] = "gtt",
41 [XE_PL_VRAM0] = "vram0",
42 [XE_PL_VRAM1] = "vram1",
43 [XE_PL_STOLEN] = "stolen"
44 };
45
46 static const struct ttm_place sys_placement_flags = {
47 .fpfn = 0,
48 .lpfn = 0,
49 .mem_type = XE_PL_SYSTEM,
50 .flags = 0,
51 };
52
53 static struct ttm_placement sys_placement = {
54 .num_placement = 1,
55 .placement = &sys_placement_flags,
56 };
57
58 static const struct ttm_place tt_placement_flags[] = {
59 {
60 .fpfn = 0,
61 .lpfn = 0,
62 .mem_type = XE_PL_TT,
63 .flags = TTM_PL_FLAG_DESIRED,
64 },
65 {
66 .fpfn = 0,
67 .lpfn = 0,
68 .mem_type = XE_PL_SYSTEM,
69 .flags = TTM_PL_FLAG_FALLBACK,
70 }
71 };
72
73 static struct ttm_placement tt_placement = {
74 .num_placement = 2,
75 .placement = tt_placement_flags,
76 };
77
mem_type_is_vram(u32 mem_type)78 bool mem_type_is_vram(u32 mem_type)
79 {
80 return mem_type >= XE_PL_VRAM0 && mem_type != XE_PL_STOLEN;
81 }
82
resource_is_stolen_vram(struct xe_device * xe,struct ttm_resource * res)83 static bool resource_is_stolen_vram(struct xe_device *xe, struct ttm_resource *res)
84 {
85 return res->mem_type == XE_PL_STOLEN && IS_DGFX(xe);
86 }
87
resource_is_vram(struct ttm_resource * res)88 static bool resource_is_vram(struct ttm_resource *res)
89 {
90 return mem_type_is_vram(res->mem_type);
91 }
92
xe_bo_is_vram(struct xe_bo * bo)93 bool xe_bo_is_vram(struct xe_bo *bo)
94 {
95 return resource_is_vram(bo->ttm.resource) ||
96 resource_is_stolen_vram(xe_bo_device(bo), bo->ttm.resource);
97 }
98
xe_bo_is_stolen(struct xe_bo * bo)99 bool xe_bo_is_stolen(struct xe_bo *bo)
100 {
101 return bo->ttm.resource->mem_type == XE_PL_STOLEN;
102 }
103
104 /**
105 * xe_bo_has_single_placement - check if BO is placed only in one memory location
106 * @bo: The BO
107 *
108 * This function checks whether a given BO is placed in only one memory location.
109 *
110 * Returns: true if the BO is placed in a single memory location, false otherwise.
111 *
112 */
xe_bo_has_single_placement(struct xe_bo * bo)113 bool xe_bo_has_single_placement(struct xe_bo *bo)
114 {
115 return bo->placement.num_placement == 1;
116 }
117
118 /**
119 * xe_bo_is_stolen_devmem - check if BO is of stolen type accessed via PCI BAR
120 * @bo: The BO
121 *
122 * The stolen memory is accessed through the PCI BAR for both DGFX and some
123 * integrated platforms that have a dedicated bit in the PTE for devmem (DM).
124 *
125 * Returns: true if it's stolen memory accessed via PCI BAR, false otherwise.
126 */
xe_bo_is_stolen_devmem(struct xe_bo * bo)127 bool xe_bo_is_stolen_devmem(struct xe_bo *bo)
128 {
129 return xe_bo_is_stolen(bo) &&
130 GRAPHICS_VERx100(xe_bo_device(bo)) >= 1270;
131 }
132
133 /**
134 * xe_bo_is_vm_bound - check if BO has any mappings through VM_BIND
135 * @bo: The BO
136 *
137 * Check if a given bo is bound through VM_BIND. This requires the
138 * reservation lock for the BO to be held.
139 *
140 * Returns: boolean
141 */
xe_bo_is_vm_bound(struct xe_bo * bo)142 bool xe_bo_is_vm_bound(struct xe_bo *bo)
143 {
144 xe_bo_assert_held(bo);
145
146 return !list_empty(&bo->ttm.base.gpuva.list);
147 }
148
xe_bo_is_user(struct xe_bo * bo)149 static bool xe_bo_is_user(struct xe_bo *bo)
150 {
151 return bo->flags & XE_BO_FLAG_USER;
152 }
153
154 static struct xe_migrate *
mem_type_to_migrate(struct xe_device * xe,u32 mem_type)155 mem_type_to_migrate(struct xe_device *xe, u32 mem_type)
156 {
157 struct xe_tile *tile;
158
159 xe_assert(xe, mem_type == XE_PL_STOLEN || mem_type_is_vram(mem_type));
160 tile = &xe->tiles[mem_type == XE_PL_STOLEN ? 0 : (mem_type - XE_PL_VRAM0)];
161 return tile->migrate;
162 }
163
res_to_mem_region(struct ttm_resource * res)164 static struct xe_vram_region *res_to_mem_region(struct ttm_resource *res)
165 {
166 struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
167 struct ttm_resource_manager *mgr;
168 struct xe_ttm_vram_mgr *vram_mgr;
169
170 xe_assert(xe, resource_is_vram(res));
171 mgr = ttm_manager_type(&xe->ttm, res->mem_type);
172 vram_mgr = to_xe_ttm_vram_mgr(mgr);
173
174 return container_of(vram_mgr, struct xe_vram_region, ttm);
175 }
176
try_add_system(struct xe_device * xe,struct xe_bo * bo,u32 bo_flags,u32 * c)177 static void try_add_system(struct xe_device *xe, struct xe_bo *bo,
178 u32 bo_flags, u32 *c)
179 {
180 if (bo_flags & XE_BO_FLAG_SYSTEM) {
181 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
182
183 bo->placements[*c] = (struct ttm_place) {
184 .mem_type = XE_PL_TT,
185 };
186 *c += 1;
187 }
188 }
189
force_contiguous(u32 bo_flags)190 static bool force_contiguous(u32 bo_flags)
191 {
192 /*
193 * For eviction / restore on suspend / resume objects pinned in VRAM
194 * must be contiguous, also only contiguous BOs support xe_bo_vmap.
195 */
196 return bo_flags & (XE_BO_FLAG_PINNED | XE_BO_FLAG_GGTT);
197 }
198
add_vram(struct xe_device * xe,struct xe_bo * bo,struct ttm_place * places,u32 bo_flags,u32 mem_type,u32 * c)199 static void add_vram(struct xe_device *xe, struct xe_bo *bo,
200 struct ttm_place *places, u32 bo_flags, u32 mem_type, u32 *c)
201 {
202 struct ttm_place place = { .mem_type = mem_type };
203 struct ttm_resource_manager *mgr = ttm_manager_type(&xe->ttm, mem_type);
204 struct xe_ttm_vram_mgr *vram_mgr = to_xe_ttm_vram_mgr(mgr);
205
206 struct xe_vram_region *vram;
207 u64 io_size;
208
209 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
210
211 vram = container_of(vram_mgr, struct xe_vram_region, ttm);
212 xe_assert(xe, vram && vram->usable_size);
213 io_size = vram->io_size;
214
215 if (force_contiguous(bo_flags))
216 place.flags |= TTM_PL_FLAG_CONTIGUOUS;
217
218 if (io_size < vram->usable_size) {
219 if (bo_flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) {
220 place.fpfn = 0;
221 place.lpfn = io_size >> PAGE_SHIFT;
222 } else {
223 place.flags |= TTM_PL_FLAG_TOPDOWN;
224 }
225 }
226 places[*c] = place;
227 *c += 1;
228 }
229
try_add_vram(struct xe_device * xe,struct xe_bo * bo,u32 bo_flags,u32 * c)230 static void try_add_vram(struct xe_device *xe, struct xe_bo *bo,
231 u32 bo_flags, u32 *c)
232 {
233 if (bo_flags & XE_BO_FLAG_VRAM0)
234 add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM0, c);
235 if (bo_flags & XE_BO_FLAG_VRAM1)
236 add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM1, c);
237 }
238
try_add_stolen(struct xe_device * xe,struct xe_bo * bo,u32 bo_flags,u32 * c)239 static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo,
240 u32 bo_flags, u32 *c)
241 {
242 if (bo_flags & XE_BO_FLAG_STOLEN) {
243 xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
244
245 bo->placements[*c] = (struct ttm_place) {
246 .mem_type = XE_PL_STOLEN,
247 .flags = force_contiguous(bo_flags) ?
248 TTM_PL_FLAG_CONTIGUOUS : 0,
249 };
250 *c += 1;
251 }
252 }
253
__xe_bo_placement_for_flags(struct xe_device * xe,struct xe_bo * bo,u32 bo_flags)254 static int __xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
255 u32 bo_flags)
256 {
257 u32 c = 0;
258
259 try_add_vram(xe, bo, bo_flags, &c);
260 try_add_system(xe, bo, bo_flags, &c);
261 try_add_stolen(xe, bo, bo_flags, &c);
262
263 if (!c)
264 return -EINVAL;
265
266 bo->placement = (struct ttm_placement) {
267 .num_placement = c,
268 .placement = bo->placements,
269 };
270
271 return 0;
272 }
273
xe_bo_placement_for_flags(struct xe_device * xe,struct xe_bo * bo,u32 bo_flags)274 int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
275 u32 bo_flags)
276 {
277 xe_bo_assert_held(bo);
278 return __xe_bo_placement_for_flags(xe, bo, bo_flags);
279 }
280
xe_evict_flags(struct ttm_buffer_object * tbo,struct ttm_placement * placement)281 static void xe_evict_flags(struct ttm_buffer_object *tbo,
282 struct ttm_placement *placement)
283 {
284 struct xe_bo *bo;
285
286 if (!xe_bo_is_xe_bo(tbo)) {
287 /* Don't handle scatter gather BOs */
288 if (tbo->type == ttm_bo_type_sg) {
289 placement->num_placement = 0;
290 return;
291 }
292
293 *placement = sys_placement;
294 return;
295 }
296
297 bo = ttm_to_xe_bo(tbo);
298 if (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) {
299 *placement = sys_placement;
300 return;
301 }
302
303 /*
304 * For xe, sg bos that are evicted to system just triggers a
305 * rebind of the sg list upon subsequent validation to XE_PL_TT.
306 */
307 switch (tbo->resource->mem_type) {
308 case XE_PL_VRAM0:
309 case XE_PL_VRAM1:
310 case XE_PL_STOLEN:
311 *placement = tt_placement;
312 break;
313 case XE_PL_TT:
314 default:
315 *placement = sys_placement;
316 break;
317 }
318 }
319
320 /* struct xe_ttm_tt - Subclassed ttm_tt for xe */
321 struct xe_ttm_tt {
322 struct ttm_tt ttm;
323 /** @xe - The xe device */
324 struct xe_device *xe;
325 struct sg_table sgt;
326 struct sg_table *sg;
327 /** @purgeable: Whether the content of the pages of @ttm is purgeable. */
328 bool purgeable;
329 };
330
xe_tt_map_sg(struct ttm_tt * tt)331 static int xe_tt_map_sg(struct ttm_tt *tt)
332 {
333 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
334 unsigned long num_pages = tt->num_pages;
335 int ret;
336
337 XE_WARN_ON((tt->page_flags & TTM_TT_FLAG_EXTERNAL) &&
338 !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE));
339
340 if (xe_tt->sg)
341 return 0;
342
343 ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages,
344 num_pages, 0,
345 (u64)num_pages << PAGE_SHIFT,
346 xe_sg_segment_size(xe_tt->xe->drm.dev),
347 GFP_KERNEL);
348 if (ret)
349 return ret;
350
351 xe_tt->sg = &xe_tt->sgt;
352 ret = dma_map_sgtable(xe_tt->xe->drm.dev, xe_tt->sg, DMA_BIDIRECTIONAL,
353 DMA_ATTR_SKIP_CPU_SYNC);
354 if (ret) {
355 sg_free_table(xe_tt->sg);
356 xe_tt->sg = NULL;
357 return ret;
358 }
359
360 return 0;
361 }
362
xe_tt_unmap_sg(struct ttm_tt * tt)363 static void xe_tt_unmap_sg(struct ttm_tt *tt)
364 {
365 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
366
367 if (xe_tt->sg) {
368 dma_unmap_sgtable(xe_tt->xe->drm.dev, xe_tt->sg,
369 DMA_BIDIRECTIONAL, 0);
370 sg_free_table(xe_tt->sg);
371 xe_tt->sg = NULL;
372 }
373 }
374
xe_bo_sg(struct xe_bo * bo)375 struct sg_table *xe_bo_sg(struct xe_bo *bo)
376 {
377 struct ttm_tt *tt = bo->ttm.ttm;
378 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
379
380 return xe_tt->sg;
381 }
382
383 /*
384 * Account ttm pages against the device shrinker's shrinkable and
385 * purgeable counts.
386 */
xe_ttm_tt_account_add(struct ttm_tt * tt)387 static void xe_ttm_tt_account_add(struct ttm_tt *tt)
388 {
389 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
390
391 if (xe_tt->purgeable)
392 xe_shrinker_mod_pages(xe_tt->xe->mem.shrinker, 0, tt->num_pages);
393 else
394 xe_shrinker_mod_pages(xe_tt->xe->mem.shrinker, tt->num_pages, 0);
395 }
396
xe_ttm_tt_account_subtract(struct ttm_tt * tt)397 static void xe_ttm_tt_account_subtract(struct ttm_tt *tt)
398 {
399 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
400
401 if (xe_tt->purgeable)
402 xe_shrinker_mod_pages(xe_tt->xe->mem.shrinker, 0, -(long)tt->num_pages);
403 else
404 xe_shrinker_mod_pages(xe_tt->xe->mem.shrinker, -(long)tt->num_pages, 0);
405 }
406
xe_ttm_tt_create(struct ttm_buffer_object * ttm_bo,u32 page_flags)407 static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
408 u32 page_flags)
409 {
410 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
411 struct xe_device *xe = xe_bo_device(bo);
412 struct xe_ttm_tt *xe_tt;
413 struct ttm_tt *tt;
414 unsigned long extra_pages;
415 enum ttm_caching caching = ttm_cached;
416 int err;
417
418 xe_tt = kzalloc(sizeof(*xe_tt), GFP_KERNEL);
419 if (!xe_tt)
420 return NULL;
421
422 tt = &xe_tt->ttm;
423 xe_tt->xe = xe;
424
425 extra_pages = 0;
426 if (xe_bo_needs_ccs_pages(bo))
427 extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, bo->size),
428 PAGE_SIZE);
429
430 /*
431 * DGFX system memory is always WB / ttm_cached, since
432 * other caching modes are only supported on x86. DGFX
433 * GPU system memory accesses are always coherent with the
434 * CPU.
435 */
436 if (!IS_DGFX(xe)) {
437 switch (bo->cpu_caching) {
438 case DRM_XE_GEM_CPU_CACHING_WC:
439 caching = ttm_write_combined;
440 break;
441 default:
442 caching = ttm_cached;
443 break;
444 }
445
446 WARN_ON((bo->flags & XE_BO_FLAG_USER) && !bo->cpu_caching);
447
448 /*
449 * Display scanout is always non-coherent with the CPU cache.
450 *
451 * For Xe_LPG and beyond, PPGTT PTE lookups are also
452 * non-coherent and require a CPU:WC mapping.
453 */
454 if ((!bo->cpu_caching && bo->flags & XE_BO_FLAG_SCANOUT) ||
455 (xe->info.graphics_verx100 >= 1270 &&
456 bo->flags & XE_BO_FLAG_PAGETABLE))
457 caching = ttm_write_combined;
458 }
459
460 if (bo->flags & XE_BO_FLAG_NEEDS_UC) {
461 /*
462 * Valid only for internally-created buffers only, for
463 * which cpu_caching is never initialized.
464 */
465 xe_assert(xe, bo->cpu_caching == 0);
466 caching = ttm_uncached;
467 }
468
469 if (ttm_bo->type != ttm_bo_type_sg)
470 page_flags |= TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE;
471
472 err = ttm_tt_init(tt, &bo->ttm, page_flags, caching, extra_pages);
473 if (err) {
474 kfree(xe_tt);
475 return NULL;
476 }
477
478 if (ttm_bo->type != ttm_bo_type_sg) {
479 err = ttm_tt_setup_backup(tt);
480 if (err) {
481 ttm_tt_fini(tt);
482 kfree(xe_tt);
483 return NULL;
484 }
485 }
486
487 return tt;
488 }
489
xe_ttm_tt_populate(struct ttm_device * ttm_dev,struct ttm_tt * tt,struct ttm_operation_ctx * ctx)490 static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt,
491 struct ttm_operation_ctx *ctx)
492 {
493 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
494 int err;
495
496 /*
497 * dma-bufs are not populated with pages, and the dma-
498 * addresses are set up when moved to XE_PL_TT.
499 */
500 if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) &&
501 !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
502 return 0;
503
504 if (ttm_tt_is_backed_up(tt) && !xe_tt->purgeable) {
505 err = ttm_tt_restore(ttm_dev, tt, ctx);
506 } else {
507 ttm_tt_clear_backed_up(tt);
508 err = ttm_pool_alloc(&ttm_dev->pool, tt, ctx);
509 }
510 if (err)
511 return err;
512
513 xe_tt->purgeable = false;
514 xe_ttm_tt_account_add(tt);
515
516 return 0;
517 }
518
xe_ttm_tt_unpopulate(struct ttm_device * ttm_dev,struct ttm_tt * tt)519 static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
520 {
521 if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) &&
522 !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
523 return;
524
525 xe_tt_unmap_sg(tt);
526
527 ttm_pool_free(&ttm_dev->pool, tt);
528 xe_ttm_tt_account_subtract(tt);
529 }
530
xe_ttm_tt_destroy(struct ttm_device * ttm_dev,struct ttm_tt * tt)531 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
532 {
533 ttm_tt_fini(tt);
534 kfree(tt);
535 }
536
xe_ttm_resource_visible(struct ttm_resource * mem)537 static bool xe_ttm_resource_visible(struct ttm_resource *mem)
538 {
539 struct xe_ttm_vram_mgr_resource *vres =
540 to_xe_ttm_vram_mgr_resource(mem);
541
542 return vres->used_visible_size == mem->size;
543 }
544
xe_ttm_io_mem_reserve(struct ttm_device * bdev,struct ttm_resource * mem)545 static int xe_ttm_io_mem_reserve(struct ttm_device *bdev,
546 struct ttm_resource *mem)
547 {
548 struct xe_device *xe = ttm_to_xe_device(bdev);
549
550 switch (mem->mem_type) {
551 case XE_PL_SYSTEM:
552 case XE_PL_TT:
553 return 0;
554 case XE_PL_VRAM0:
555 case XE_PL_VRAM1: {
556 struct xe_vram_region *vram = res_to_mem_region(mem);
557
558 if (!xe_ttm_resource_visible(mem))
559 return -EINVAL;
560
561 mem->bus.offset = mem->start << PAGE_SHIFT;
562
563 if (vram->mapping &&
564 mem->placement & TTM_PL_FLAG_CONTIGUOUS)
565 mem->bus.addr = (u8 __force *)vram->mapping +
566 mem->bus.offset;
567
568 mem->bus.offset += vram->io_start;
569 mem->bus.is_iomem = true;
570
571 #if !IS_ENABLED(CONFIG_X86)
572 mem->bus.caching = ttm_write_combined;
573 #endif
574 return 0;
575 } case XE_PL_STOLEN:
576 return xe_ttm_stolen_io_mem_reserve(xe, mem);
577 default:
578 return -EINVAL;
579 }
580 }
581
xe_bo_trigger_rebind(struct xe_device * xe,struct xe_bo * bo,const struct ttm_operation_ctx * ctx)582 static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
583 const struct ttm_operation_ctx *ctx)
584 {
585 struct dma_resv_iter cursor;
586 struct dma_fence *fence;
587 struct drm_gem_object *obj = &bo->ttm.base;
588 struct drm_gpuvm_bo *vm_bo;
589 bool idle = false;
590 int ret = 0;
591
592 dma_resv_assert_held(bo->ttm.base.resv);
593
594 if (!list_empty(&bo->ttm.base.gpuva.list)) {
595 dma_resv_iter_begin(&cursor, bo->ttm.base.resv,
596 DMA_RESV_USAGE_BOOKKEEP);
597 dma_resv_for_each_fence_unlocked(&cursor, fence)
598 dma_fence_enable_sw_signaling(fence);
599 dma_resv_iter_end(&cursor);
600 }
601
602 drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
603 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
604 struct drm_gpuva *gpuva;
605
606 if (!xe_vm_in_fault_mode(vm)) {
607 drm_gpuvm_bo_evict(vm_bo, true);
608 continue;
609 }
610
611 if (!idle) {
612 long timeout;
613
614 if (ctx->no_wait_gpu &&
615 !dma_resv_test_signaled(bo->ttm.base.resv,
616 DMA_RESV_USAGE_BOOKKEEP))
617 return -EBUSY;
618
619 timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
620 DMA_RESV_USAGE_BOOKKEEP,
621 ctx->interruptible,
622 MAX_SCHEDULE_TIMEOUT);
623 if (!timeout)
624 return -ETIME;
625 if (timeout < 0)
626 return timeout;
627
628 idle = true;
629 }
630
631 drm_gpuvm_bo_for_each_va(gpuva, vm_bo) {
632 struct xe_vma *vma = gpuva_to_vma(gpuva);
633
634 trace_xe_vma_evict(vma);
635 ret = xe_vm_invalidate_vma(vma);
636 if (XE_WARN_ON(ret))
637 return ret;
638 }
639 }
640
641 return ret;
642 }
643
644 /*
645 * The dma-buf map_attachment() / unmap_attachment() is hooked up here.
646 * Note that unmapping the attachment is deferred to the next
647 * map_attachment time, or to bo destroy (after idling) whichever comes first.
648 * This is to avoid syncing before unmap_attachment(), assuming that the
649 * caller relies on idling the reservation object before moving the
650 * backing store out. Should that assumption not hold, then we will be able
651 * to unconditionally call unmap_attachment() when moving out to system.
652 */
xe_bo_move_dmabuf(struct ttm_buffer_object * ttm_bo,struct ttm_resource * new_res)653 static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
654 struct ttm_resource *new_res)
655 {
656 struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
657 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
658 ttm);
659 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
660 struct sg_table *sg;
661
662 xe_assert(xe, attach);
663 xe_assert(xe, ttm_bo->ttm);
664
665 if (new_res->mem_type == XE_PL_SYSTEM)
666 goto out;
667
668 if (ttm_bo->sg) {
669 dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
670 ttm_bo->sg = NULL;
671 }
672
673 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
674 if (IS_ERR(sg))
675 return PTR_ERR(sg);
676
677 ttm_bo->sg = sg;
678 xe_tt->sg = sg;
679
680 out:
681 ttm_bo_move_null(ttm_bo, new_res);
682
683 return 0;
684 }
685
686 /**
687 * xe_bo_move_notify - Notify subsystems of a pending move
688 * @bo: The buffer object
689 * @ctx: The struct ttm_operation_ctx controlling locking and waits.
690 *
691 * This function notifies subsystems of an upcoming buffer move.
692 * Upon receiving such a notification, subsystems should schedule
693 * halting access to the underlying pages and optionally add a fence
694 * to the buffer object's dma_resv object, that signals when access is
695 * stopped. The caller will wait on all dma_resv fences before
696 * starting the move.
697 *
698 * A subsystem may commence access to the object after obtaining
699 * bindings to the new backing memory under the object lock.
700 *
701 * Return: 0 on success, -EINTR or -ERESTARTSYS if interrupted in fault mode,
702 * negative error code on error.
703 */
xe_bo_move_notify(struct xe_bo * bo,const struct ttm_operation_ctx * ctx)704 static int xe_bo_move_notify(struct xe_bo *bo,
705 const struct ttm_operation_ctx *ctx)
706 {
707 struct ttm_buffer_object *ttm_bo = &bo->ttm;
708 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
709 struct ttm_resource *old_mem = ttm_bo->resource;
710 u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM;
711 int ret;
712
713 /*
714 * If this starts to call into many components, consider
715 * using a notification chain here.
716 */
717
718 if (xe_bo_is_pinned(bo))
719 return -EINVAL;
720
721 xe_bo_vunmap(bo);
722 ret = xe_bo_trigger_rebind(xe, bo, ctx);
723 if (ret)
724 return ret;
725
726 /* Don't call move_notify() for imported dma-bufs. */
727 if (ttm_bo->base.dma_buf && !ttm_bo->base.import_attach)
728 dma_buf_move_notify(ttm_bo->base.dma_buf);
729
730 /*
731 * TTM has already nuked the mmap for us (see ttm_bo_unmap_virtual),
732 * so if we moved from VRAM make sure to unlink this from the userfault
733 * tracking.
734 */
735 if (mem_type_is_vram(old_mem_type)) {
736 mutex_lock(&xe->mem_access.vram_userfault.lock);
737 if (!list_empty(&bo->vram_userfault_link))
738 list_del_init(&bo->vram_userfault_link);
739 mutex_unlock(&xe->mem_access.vram_userfault.lock);
740 }
741
742 return 0;
743 }
744
xe_bo_move(struct ttm_buffer_object * ttm_bo,bool evict,struct ttm_operation_ctx * ctx,struct ttm_resource * new_mem,struct ttm_place * hop)745 static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
746 struct ttm_operation_ctx *ctx,
747 struct ttm_resource *new_mem,
748 struct ttm_place *hop)
749 {
750 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
751 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
752 struct ttm_resource *old_mem = ttm_bo->resource;
753 u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM;
754 struct ttm_tt *ttm = ttm_bo->ttm;
755 struct xe_migrate *migrate = NULL;
756 struct dma_fence *fence;
757 bool move_lacks_source;
758 bool tt_has_data;
759 bool needs_clear;
760 bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) &&
761 ttm && ttm_tt_is_populated(ttm)) ? true : false;
762 int ret = 0;
763
764 /* Bo creation path, moving to system or TT. */
765 if ((!old_mem && ttm) && !handle_system_ccs) {
766 if (new_mem->mem_type == XE_PL_TT)
767 ret = xe_tt_map_sg(ttm);
768 if (!ret)
769 ttm_bo_move_null(ttm_bo, new_mem);
770 goto out;
771 }
772
773 if (ttm_bo->type == ttm_bo_type_sg) {
774 ret = xe_bo_move_notify(bo, ctx);
775 if (!ret)
776 ret = xe_bo_move_dmabuf(ttm_bo, new_mem);
777 return ret;
778 }
779
780 tt_has_data = ttm && (ttm_tt_is_populated(ttm) ||
781 (ttm->page_flags & TTM_TT_FLAG_SWAPPED));
782
783 move_lacks_source = !old_mem || (handle_system_ccs ? (!bo->ccs_cleared) :
784 (!mem_type_is_vram(old_mem_type) && !tt_has_data));
785
786 needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
787 (!ttm && ttm_bo->type == ttm_bo_type_device);
788
789 if (new_mem->mem_type == XE_PL_TT) {
790 ret = xe_tt_map_sg(ttm);
791 if (ret)
792 goto out;
793 }
794
795 if ((move_lacks_source && !needs_clear)) {
796 ttm_bo_move_null(ttm_bo, new_mem);
797 goto out;
798 }
799
800 if (!move_lacks_source && (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) &&
801 new_mem->mem_type == XE_PL_SYSTEM) {
802 ret = xe_svm_bo_evict(bo);
803 if (!ret) {
804 drm_dbg(&xe->drm, "Evict system allocator BO success\n");
805 ttm_bo_move_null(ttm_bo, new_mem);
806 } else {
807 drm_dbg(&xe->drm, "Evict system allocator BO failed=%pe\n",
808 ERR_PTR(ret));
809 }
810
811 goto out;
812 }
813
814 if (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT && !handle_system_ccs) {
815 ttm_bo_move_null(ttm_bo, new_mem);
816 goto out;
817 }
818
819 /* Reject BO eviction if BO is bound to current VM. */
820 if (evict && ctx->resv) {
821 struct drm_gpuvm_bo *vm_bo;
822
823 drm_gem_for_each_gpuvm_bo(vm_bo, &bo->ttm.base) {
824 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
825
826 if (xe_vm_resv(vm) == ctx->resv &&
827 xe_vm_in_preempt_fence_mode(vm)) {
828 ret = -EBUSY;
829 goto out;
830 }
831 }
832 }
833
834 /*
835 * Failed multi-hop where the old_mem is still marked as
836 * TTM_PL_FLAG_TEMPORARY, should just be a dummy move.
837 */
838 if (old_mem_type == XE_PL_TT &&
839 new_mem->mem_type == XE_PL_TT) {
840 ttm_bo_move_null(ttm_bo, new_mem);
841 goto out;
842 }
843
844 if (!move_lacks_source && !xe_bo_is_pinned(bo)) {
845 ret = xe_bo_move_notify(bo, ctx);
846 if (ret)
847 goto out;
848 }
849
850 if (old_mem_type == XE_PL_TT &&
851 new_mem->mem_type == XE_PL_SYSTEM) {
852 long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
853 DMA_RESV_USAGE_BOOKKEEP,
854 false,
855 MAX_SCHEDULE_TIMEOUT);
856 if (timeout < 0) {
857 ret = timeout;
858 goto out;
859 }
860
861 if (!handle_system_ccs) {
862 ttm_bo_move_null(ttm_bo, new_mem);
863 goto out;
864 }
865 }
866
867 if (!move_lacks_source &&
868 ((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) ||
869 (mem_type_is_vram(old_mem_type) &&
870 new_mem->mem_type == XE_PL_SYSTEM))) {
871 hop->fpfn = 0;
872 hop->lpfn = 0;
873 hop->mem_type = XE_PL_TT;
874 hop->flags = TTM_PL_FLAG_TEMPORARY;
875 ret = -EMULTIHOP;
876 goto out;
877 }
878
879 if (bo->tile)
880 migrate = bo->tile->migrate;
881 else if (resource_is_vram(new_mem))
882 migrate = mem_type_to_migrate(xe, new_mem->mem_type);
883 else if (mem_type_is_vram(old_mem_type))
884 migrate = mem_type_to_migrate(xe, old_mem_type);
885 else
886 migrate = xe->tiles[0].migrate;
887
888 xe_assert(xe, migrate);
889 trace_xe_bo_move(bo, new_mem->mem_type, old_mem_type, move_lacks_source);
890 if (xe_rpm_reclaim_safe(xe)) {
891 /*
892 * We might be called through swapout in the validation path of
893 * another TTM device, so acquire rpm here.
894 */
895 xe_pm_runtime_get(xe);
896 } else {
897 drm_WARN_ON(&xe->drm, handle_system_ccs);
898 xe_pm_runtime_get_noresume(xe);
899 }
900
901 if (xe_bo_is_pinned(bo) && !xe_bo_is_user(bo)) {
902 /*
903 * Kernel memory that is pinned should only be moved on suspend
904 * / resume, some of the pinned memory is required for the
905 * device to resume / use the GPU to move other evicted memory
906 * (user memory) around. This likely could be optimized a bit
907 * further where we find the minimum set of pinned memory
908 * required for resume but for simplity doing a memcpy for all
909 * pinned memory.
910 */
911 ret = xe_bo_vmap(bo);
912 if (!ret) {
913 ret = ttm_bo_move_memcpy(ttm_bo, ctx, new_mem);
914
915 /* Create a new VMAP once kernel BO back in VRAM */
916 if (!ret && resource_is_vram(new_mem)) {
917 struct xe_vram_region *vram = res_to_mem_region(new_mem);
918 void __iomem *new_addr = vram->mapping +
919 (new_mem->start << PAGE_SHIFT);
920
921 if (XE_WARN_ON(new_mem->start == XE_BO_INVALID_OFFSET)) {
922 ret = -EINVAL;
923 xe_pm_runtime_put(xe);
924 goto out;
925 }
926
927 xe_assert(xe, new_mem->start ==
928 bo->placements->fpfn);
929
930 iosys_map_set_vaddr_iomem(&bo->vmap, new_addr);
931 }
932 }
933 } else {
934 if (move_lacks_source) {
935 u32 flags = 0;
936
937 if (mem_type_is_vram(new_mem->mem_type))
938 flags |= XE_MIGRATE_CLEAR_FLAG_FULL;
939 else if (handle_system_ccs)
940 flags |= XE_MIGRATE_CLEAR_FLAG_CCS_DATA;
941
942 fence = xe_migrate_clear(migrate, bo, new_mem, flags);
943 }
944 else
945 fence = xe_migrate_copy(migrate, bo, bo, old_mem,
946 new_mem, handle_system_ccs);
947 if (IS_ERR(fence)) {
948 ret = PTR_ERR(fence);
949 xe_pm_runtime_put(xe);
950 goto out;
951 }
952 if (!move_lacks_source) {
953 ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict,
954 true, new_mem);
955 if (ret) {
956 dma_fence_wait(fence, false);
957 ttm_bo_move_null(ttm_bo, new_mem);
958 ret = 0;
959 }
960 } else {
961 /*
962 * ttm_bo_move_accel_cleanup() may blow up if
963 * bo->resource == NULL, so just attach the
964 * fence and set the new resource.
965 */
966 dma_resv_add_fence(ttm_bo->base.resv, fence,
967 DMA_RESV_USAGE_KERNEL);
968 ttm_bo_move_null(ttm_bo, new_mem);
969 }
970
971 dma_fence_put(fence);
972 }
973
974 xe_pm_runtime_put(xe);
975
976 out:
977 if ((!ttm_bo->resource || ttm_bo->resource->mem_type == XE_PL_SYSTEM) &&
978 ttm_bo->ttm) {
979 long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
980 DMA_RESV_USAGE_KERNEL,
981 false,
982 MAX_SCHEDULE_TIMEOUT);
983 if (timeout < 0)
984 ret = timeout;
985
986 xe_tt_unmap_sg(ttm_bo->ttm);
987 }
988
989 return ret;
990 }
991
xe_bo_shrink_purge(struct ttm_operation_ctx * ctx,struct ttm_buffer_object * bo,unsigned long * scanned)992 static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx,
993 struct ttm_buffer_object *bo,
994 unsigned long *scanned)
995 {
996 long lret;
997
998 /* Fake move to system, without copying data. */
999 if (bo->resource->mem_type != XE_PL_SYSTEM) {
1000 struct ttm_resource *new_resource;
1001
1002 lret = ttm_bo_wait_ctx(bo, ctx);
1003 if (lret)
1004 return lret;
1005
1006 lret = ttm_bo_mem_space(bo, &sys_placement, &new_resource, ctx);
1007 if (lret)
1008 return lret;
1009
1010 xe_tt_unmap_sg(bo->ttm);
1011 ttm_bo_move_null(bo, new_resource);
1012 }
1013
1014 *scanned += bo->ttm->num_pages;
1015 lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags)
1016 {.purge = true,
1017 .writeback = false,
1018 .allow_move = false});
1019
1020 if (lret > 0)
1021 xe_ttm_tt_account_subtract(bo->ttm);
1022
1023 return lret;
1024 }
1025
1026 /**
1027 * xe_bo_shrink() - Try to shrink an xe bo.
1028 * @ctx: The struct ttm_operation_ctx used for shrinking.
1029 * @bo: The TTM buffer object whose pages to shrink.
1030 * @flags: Flags governing the shrink behaviour.
1031 * @scanned: Pointer to a counter of the number of pages
1032 * attempted to shrink.
1033 *
1034 * Try to shrink- or purge a bo, and if it succeeds, unmap dma.
1035 * Note that we need to be able to handle also non xe bos
1036 * (ghost bos), but only if the struct ttm_tt is embedded in
1037 * a struct xe_ttm_tt. When the function attempts to shrink
1038 * the pages of a buffer object, The value pointed to by @scanned
1039 * is updated.
1040 *
1041 * Return: The number of pages shrunken or purged, or negative error
1042 * code on failure.
1043 */
xe_bo_shrink(struct ttm_operation_ctx * ctx,struct ttm_buffer_object * bo,const struct xe_bo_shrink_flags flags,unsigned long * scanned)1044 long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
1045 const struct xe_bo_shrink_flags flags,
1046 unsigned long *scanned)
1047 {
1048 struct ttm_tt *tt = bo->ttm;
1049 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
1050 struct ttm_place place = {.mem_type = bo->resource->mem_type};
1051 struct xe_bo *xe_bo = ttm_to_xe_bo(bo);
1052 struct xe_device *xe = xe_tt->xe;
1053 bool needs_rpm;
1054 long lret = 0L;
1055
1056 if (!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE) ||
1057 (flags.purge && !xe_tt->purgeable))
1058 return -EBUSY;
1059
1060 if (!ttm_bo_eviction_valuable(bo, &place))
1061 return -EBUSY;
1062
1063 if (!xe_bo_is_xe_bo(bo) || !xe_bo_get_unless_zero(xe_bo))
1064 return xe_bo_shrink_purge(ctx, bo, scanned);
1065
1066 if (xe_tt->purgeable) {
1067 if (bo->resource->mem_type != XE_PL_SYSTEM)
1068 lret = xe_bo_move_notify(xe_bo, ctx);
1069 if (!lret)
1070 lret = xe_bo_shrink_purge(ctx, bo, scanned);
1071 goto out_unref;
1072 }
1073
1074 /* System CCS needs gpu copy when moving PL_TT -> PL_SYSTEM */
1075 needs_rpm = (!IS_DGFX(xe) && bo->resource->mem_type != XE_PL_SYSTEM &&
1076 xe_bo_needs_ccs_pages(xe_bo));
1077 if (needs_rpm && !xe_pm_runtime_get_if_active(xe))
1078 goto out_unref;
1079
1080 *scanned += tt->num_pages;
1081 lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags)
1082 {.purge = false,
1083 .writeback = flags.writeback,
1084 .allow_move = true});
1085 if (needs_rpm)
1086 xe_pm_runtime_put(xe);
1087
1088 if (lret > 0)
1089 xe_ttm_tt_account_subtract(tt);
1090
1091 out_unref:
1092 xe_bo_put(xe_bo);
1093
1094 return lret;
1095 }
1096
1097 /**
1098 * xe_bo_evict_pinned() - Evict a pinned VRAM object to system memory
1099 * @bo: The buffer object to move.
1100 *
1101 * On successful completion, the object memory will be moved to system memory.
1102 *
1103 * This is needed to for special handling of pinned VRAM object during
1104 * suspend-resume.
1105 *
1106 * Return: 0 on success. Negative error code on failure.
1107 */
xe_bo_evict_pinned(struct xe_bo * bo)1108 int xe_bo_evict_pinned(struct xe_bo *bo)
1109 {
1110 struct ttm_place place = {
1111 .mem_type = XE_PL_TT,
1112 };
1113 struct ttm_placement placement = {
1114 .placement = &place,
1115 .num_placement = 1,
1116 };
1117 struct ttm_operation_ctx ctx = {
1118 .interruptible = false,
1119 .gfp_retry_mayfail = true,
1120 };
1121 struct ttm_resource *new_mem;
1122 int ret;
1123
1124 xe_bo_assert_held(bo);
1125
1126 if (WARN_ON(!bo->ttm.resource))
1127 return -EINVAL;
1128
1129 if (WARN_ON(!xe_bo_is_pinned(bo)))
1130 return -EINVAL;
1131
1132 if (!xe_bo_is_vram(bo))
1133 return 0;
1134
1135 ret = ttm_bo_mem_space(&bo->ttm, &placement, &new_mem, &ctx);
1136 if (ret)
1137 return ret;
1138
1139 if (!bo->ttm.ttm) {
1140 bo->ttm.ttm = xe_ttm_tt_create(&bo->ttm, 0);
1141 if (!bo->ttm.ttm) {
1142 ret = -ENOMEM;
1143 goto err_res_free;
1144 }
1145 }
1146
1147 ret = ttm_bo_populate(&bo->ttm, &ctx);
1148 if (ret)
1149 goto err_res_free;
1150
1151 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
1152 if (ret)
1153 goto err_res_free;
1154
1155 ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
1156 if (ret)
1157 goto err_res_free;
1158
1159 return 0;
1160
1161 err_res_free:
1162 ttm_resource_free(&bo->ttm, &new_mem);
1163 return ret;
1164 }
1165
1166 /**
1167 * xe_bo_restore_pinned() - Restore a pinned VRAM object
1168 * @bo: The buffer object to move.
1169 *
1170 * On successful completion, the object memory will be moved back to VRAM.
1171 *
1172 * This is needed to for special handling of pinned VRAM object during
1173 * suspend-resume.
1174 *
1175 * Return: 0 on success. Negative error code on failure.
1176 */
xe_bo_restore_pinned(struct xe_bo * bo)1177 int xe_bo_restore_pinned(struct xe_bo *bo)
1178 {
1179 struct ttm_operation_ctx ctx = {
1180 .interruptible = false,
1181 .gfp_retry_mayfail = false,
1182 };
1183 struct ttm_resource *new_mem;
1184 struct ttm_place *place = &bo->placements[0];
1185 int ret;
1186
1187 xe_bo_assert_held(bo);
1188
1189 if (WARN_ON(!bo->ttm.resource))
1190 return -EINVAL;
1191
1192 if (WARN_ON(!xe_bo_is_pinned(bo)))
1193 return -EINVAL;
1194
1195 if (WARN_ON(xe_bo_is_vram(bo)))
1196 return -EINVAL;
1197
1198 if (WARN_ON(!bo->ttm.ttm && !xe_bo_is_stolen(bo)))
1199 return -EINVAL;
1200
1201 if (!mem_type_is_vram(place->mem_type))
1202 return 0;
1203
1204 ret = ttm_bo_mem_space(&bo->ttm, &bo->placement, &new_mem, &ctx);
1205 if (ret)
1206 return ret;
1207
1208 ret = ttm_bo_populate(&bo->ttm, &ctx);
1209 if (ret)
1210 goto err_res_free;
1211
1212 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
1213 if (ret)
1214 goto err_res_free;
1215
1216 ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
1217 if (ret)
1218 goto err_res_free;
1219
1220 return 0;
1221
1222 err_res_free:
1223 ttm_resource_free(&bo->ttm, &new_mem);
1224 return ret;
1225 }
1226
xe_ttm_io_mem_pfn(struct ttm_buffer_object * ttm_bo,unsigned long page_offset)1227 static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo,
1228 unsigned long page_offset)
1229 {
1230 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1231 struct xe_res_cursor cursor;
1232 struct xe_vram_region *vram;
1233
1234 if (ttm_bo->resource->mem_type == XE_PL_STOLEN)
1235 return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT;
1236
1237 vram = res_to_mem_region(ttm_bo->resource);
1238 xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor);
1239 return (vram->io_start + cursor.start) >> PAGE_SHIFT;
1240 }
1241
1242 static void __xe_bo_vunmap(struct xe_bo *bo);
1243
1244 /*
1245 * TODO: Move this function to TTM so we don't rely on how TTM does its
1246 * locking, thereby abusing TTM internals.
1247 */
xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object * ttm_bo)1248 static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo)
1249 {
1250 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1251 bool locked;
1252
1253 xe_assert(xe, !kref_read(&ttm_bo->kref));
1254
1255 /*
1256 * We can typically only race with TTM trylocking under the
1257 * lru_lock, which will immediately be unlocked again since
1258 * the ttm_bo refcount is zero at this point. So trylocking *should*
1259 * always succeed here, as long as we hold the lru lock.
1260 */
1261 spin_lock(&ttm_bo->bdev->lru_lock);
1262 locked = dma_resv_trylock(ttm_bo->base.resv);
1263 spin_unlock(&ttm_bo->bdev->lru_lock);
1264 xe_assert(xe, locked);
1265
1266 return locked;
1267 }
1268
xe_ttm_bo_release_notify(struct ttm_buffer_object * ttm_bo)1269 static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
1270 {
1271 struct dma_resv_iter cursor;
1272 struct dma_fence *fence;
1273 struct dma_fence *replacement = NULL;
1274 struct xe_bo *bo;
1275
1276 if (!xe_bo_is_xe_bo(ttm_bo))
1277 return;
1278
1279 bo = ttm_to_xe_bo(ttm_bo);
1280 xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount)));
1281
1282 /*
1283 * Corner case where TTM fails to allocate memory and this BOs resv
1284 * still points the VMs resv
1285 */
1286 if (ttm_bo->base.resv != &ttm_bo->base._resv)
1287 return;
1288
1289 if (!xe_ttm_bo_lock_in_destructor(ttm_bo))
1290 return;
1291
1292 /*
1293 * Scrub the preempt fences if any. The unbind fence is already
1294 * attached to the resv.
1295 * TODO: Don't do this for external bos once we scrub them after
1296 * unbind.
1297 */
1298 dma_resv_for_each_fence(&cursor, ttm_bo->base.resv,
1299 DMA_RESV_USAGE_BOOKKEEP, fence) {
1300 if (xe_fence_is_xe_preempt(fence) &&
1301 !dma_fence_is_signaled(fence)) {
1302 if (!replacement)
1303 replacement = dma_fence_get_stub();
1304
1305 dma_resv_replace_fences(ttm_bo->base.resv,
1306 fence->context,
1307 replacement,
1308 DMA_RESV_USAGE_BOOKKEEP);
1309 }
1310 }
1311 dma_fence_put(replacement);
1312
1313 dma_resv_unlock(ttm_bo->base.resv);
1314 }
1315
xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object * ttm_bo)1316 static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
1317 {
1318 if (!xe_bo_is_xe_bo(ttm_bo))
1319 return;
1320
1321 /*
1322 * Object is idle and about to be destroyed. Release the
1323 * dma-buf attachment.
1324 */
1325 if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
1326 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
1327 struct xe_ttm_tt, ttm);
1328
1329 dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
1330 DMA_BIDIRECTIONAL);
1331 ttm_bo->sg = NULL;
1332 xe_tt->sg = NULL;
1333 }
1334 }
1335
xe_ttm_bo_purge(struct ttm_buffer_object * ttm_bo,struct ttm_operation_ctx * ctx)1336 static void xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *ctx)
1337 {
1338 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1339
1340 if (ttm_bo->ttm) {
1341 struct ttm_placement place = {};
1342 int ret = ttm_bo_validate(ttm_bo, &place, ctx);
1343
1344 drm_WARN_ON(&xe->drm, ret);
1345 }
1346 }
1347
xe_ttm_bo_swap_notify(struct ttm_buffer_object * ttm_bo)1348 static void xe_ttm_bo_swap_notify(struct ttm_buffer_object *ttm_bo)
1349 {
1350 struct ttm_operation_ctx ctx = {
1351 .interruptible = false,
1352 .gfp_retry_mayfail = false,
1353 };
1354
1355 if (ttm_bo->ttm) {
1356 struct xe_ttm_tt *xe_tt =
1357 container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm);
1358
1359 if (xe_tt->purgeable)
1360 xe_ttm_bo_purge(ttm_bo, &ctx);
1361 }
1362 }
1363
xe_ttm_access_memory(struct ttm_buffer_object * ttm_bo,unsigned long offset,void * buf,int len,int write)1364 static int xe_ttm_access_memory(struct ttm_buffer_object *ttm_bo,
1365 unsigned long offset, void *buf, int len,
1366 int write)
1367 {
1368 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1369 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1370 struct iosys_map vmap;
1371 struct xe_res_cursor cursor;
1372 struct xe_vram_region *vram;
1373 int bytes_left = len;
1374
1375 xe_bo_assert_held(bo);
1376 xe_device_assert_mem_access(xe);
1377
1378 if (!mem_type_is_vram(ttm_bo->resource->mem_type))
1379 return -EIO;
1380
1381 /* FIXME: Use GPU for non-visible VRAM */
1382 if (!xe_ttm_resource_visible(ttm_bo->resource))
1383 return -EIO;
1384
1385 vram = res_to_mem_region(ttm_bo->resource);
1386 xe_res_first(ttm_bo->resource, offset & PAGE_MASK,
1387 bo->size - (offset & PAGE_MASK), &cursor);
1388
1389 do {
1390 unsigned long page_offset = (offset & ~PAGE_MASK);
1391 int byte_count = min((int)(PAGE_SIZE - page_offset), bytes_left);
1392
1393 iosys_map_set_vaddr_iomem(&vmap, (u8 __iomem *)vram->mapping +
1394 cursor.start);
1395 if (write)
1396 xe_map_memcpy_to(xe, &vmap, page_offset, buf, byte_count);
1397 else
1398 xe_map_memcpy_from(xe, buf, &vmap, page_offset, byte_count);
1399
1400 buf += byte_count;
1401 offset += byte_count;
1402 bytes_left -= byte_count;
1403 if (bytes_left)
1404 xe_res_next(&cursor, PAGE_SIZE);
1405 } while (bytes_left);
1406
1407 return len;
1408 }
1409
1410 const struct ttm_device_funcs xe_ttm_funcs = {
1411 .ttm_tt_create = xe_ttm_tt_create,
1412 .ttm_tt_populate = xe_ttm_tt_populate,
1413 .ttm_tt_unpopulate = xe_ttm_tt_unpopulate,
1414 .ttm_tt_destroy = xe_ttm_tt_destroy,
1415 .evict_flags = xe_evict_flags,
1416 .move = xe_bo_move,
1417 .io_mem_reserve = xe_ttm_io_mem_reserve,
1418 .io_mem_pfn = xe_ttm_io_mem_pfn,
1419 .access_memory = xe_ttm_access_memory,
1420 .release_notify = xe_ttm_bo_release_notify,
1421 .eviction_valuable = ttm_bo_eviction_valuable,
1422 .delete_mem_notify = xe_ttm_bo_delete_mem_notify,
1423 .swap_notify = xe_ttm_bo_swap_notify,
1424 };
1425
xe_ttm_bo_destroy(struct ttm_buffer_object * ttm_bo)1426 static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo)
1427 {
1428 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1429 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1430 struct xe_tile *tile;
1431 u8 id;
1432
1433 if (bo->ttm.base.import_attach)
1434 drm_prime_gem_destroy(&bo->ttm.base, NULL);
1435 drm_gem_object_release(&bo->ttm.base);
1436
1437 xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list));
1438
1439 for_each_tile(tile, xe, id)
1440 if (bo->ggtt_node[id] && bo->ggtt_node[id]->base.size)
1441 xe_ggtt_remove_bo(tile->mem.ggtt, bo);
1442
1443 #ifdef CONFIG_PROC_FS
1444 if (bo->client)
1445 xe_drm_client_remove_bo(bo);
1446 #endif
1447
1448 if (bo->vm && xe_bo_is_user(bo))
1449 xe_vm_put(bo->vm);
1450
1451 mutex_lock(&xe->mem_access.vram_userfault.lock);
1452 if (!list_empty(&bo->vram_userfault_link))
1453 list_del(&bo->vram_userfault_link);
1454 mutex_unlock(&xe->mem_access.vram_userfault.lock);
1455
1456 kfree(bo);
1457 }
1458
xe_gem_object_free(struct drm_gem_object * obj)1459 static void xe_gem_object_free(struct drm_gem_object *obj)
1460 {
1461 /* Our BO reference counting scheme works as follows:
1462 *
1463 * The gem object kref is typically used throughout the driver,
1464 * and the gem object holds a ttm_buffer_object refcount, so
1465 * that when the last gem object reference is put, which is when
1466 * we end up in this function, we put also that ttm_buffer_object
1467 * refcount. Anything using gem interfaces is then no longer
1468 * allowed to access the object in a way that requires a gem
1469 * refcount, including locking the object.
1470 *
1471 * driver ttm callbacks is allowed to use the ttm_buffer_object
1472 * refcount directly if needed.
1473 */
1474 __xe_bo_vunmap(gem_to_xe_bo(obj));
1475 ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
1476 }
1477
xe_gem_object_close(struct drm_gem_object * obj,struct drm_file * file_priv)1478 static void xe_gem_object_close(struct drm_gem_object *obj,
1479 struct drm_file *file_priv)
1480 {
1481 struct xe_bo *bo = gem_to_xe_bo(obj);
1482
1483 if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) {
1484 xe_assert(xe_bo_device(bo), xe_bo_is_user(bo));
1485
1486 xe_bo_lock(bo, false);
1487 ttm_bo_set_bulk_move(&bo->ttm, NULL);
1488 xe_bo_unlock(bo);
1489 }
1490 }
1491
xe_gem_fault(struct vm_fault * vmf)1492 static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
1493 {
1494 struct ttm_buffer_object *tbo = vmf->vma->vm_private_data;
1495 struct drm_device *ddev = tbo->base.dev;
1496 struct xe_device *xe = to_xe_device(ddev);
1497 struct xe_bo *bo = ttm_to_xe_bo(tbo);
1498 bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK;
1499 vm_fault_t ret;
1500 int idx;
1501
1502 if (needs_rpm)
1503 xe_pm_runtime_get(xe);
1504
1505 ret = ttm_bo_vm_reserve(tbo, vmf);
1506 if (ret)
1507 goto out;
1508
1509 if (drm_dev_enter(ddev, &idx)) {
1510 trace_xe_bo_cpu_fault(bo);
1511
1512 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
1513 TTM_BO_VM_NUM_PREFAULT);
1514 drm_dev_exit(idx);
1515 } else {
1516 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1517 }
1518
1519 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1520 goto out;
1521 /*
1522 * ttm_bo_vm_reserve() already has dma_resv_lock.
1523 */
1524 if (ret == VM_FAULT_NOPAGE && mem_type_is_vram(tbo->resource->mem_type)) {
1525 mutex_lock(&xe->mem_access.vram_userfault.lock);
1526 if (list_empty(&bo->vram_userfault_link))
1527 list_add(&bo->vram_userfault_link, &xe->mem_access.vram_userfault.list);
1528 mutex_unlock(&xe->mem_access.vram_userfault.lock);
1529 }
1530
1531 dma_resv_unlock(tbo->base.resv);
1532 out:
1533 if (needs_rpm)
1534 xe_pm_runtime_put(xe);
1535
1536 return ret;
1537 }
1538
xe_bo_vm_access(struct vm_area_struct * vma,unsigned long addr,void * buf,int len,int write)1539 static int xe_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
1540 void *buf, int len, int write)
1541 {
1542 struct ttm_buffer_object *ttm_bo = vma->vm_private_data;
1543 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1544 struct xe_device *xe = xe_bo_device(bo);
1545 int ret;
1546
1547 xe_pm_runtime_get(xe);
1548 ret = ttm_bo_vm_access(vma, addr, buf, len, write);
1549 xe_pm_runtime_put(xe);
1550
1551 return ret;
1552 }
1553
1554 /**
1555 * xe_bo_read() - Read from an xe_bo
1556 * @bo: The buffer object to read from.
1557 * @offset: The byte offset to start reading from.
1558 * @dst: Location to store the read.
1559 * @size: Size in bytes for the read.
1560 *
1561 * Read @size bytes from the @bo, starting from @offset, storing into @dst.
1562 *
1563 * Return: Zero on success, or negative error.
1564 */
xe_bo_read(struct xe_bo * bo,u64 offset,void * dst,int size)1565 int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size)
1566 {
1567 int ret;
1568
1569 ret = ttm_bo_access(&bo->ttm, offset, dst, size, 0);
1570 if (ret >= 0 && ret != size)
1571 ret = -EIO;
1572 else if (ret == size)
1573 ret = 0;
1574
1575 return ret;
1576 }
1577
1578 static const struct vm_operations_struct xe_gem_vm_ops = {
1579 .fault = xe_gem_fault,
1580 .open = ttm_bo_vm_open,
1581 .close = ttm_bo_vm_close,
1582 .access = xe_bo_vm_access,
1583 };
1584
1585 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
1586 .free = xe_gem_object_free,
1587 .close = xe_gem_object_close,
1588 .mmap = drm_gem_ttm_mmap,
1589 .export = xe_gem_prime_export,
1590 .vm_ops = &xe_gem_vm_ops,
1591 };
1592
1593 /**
1594 * xe_bo_alloc - Allocate storage for a struct xe_bo
1595 *
1596 * This function is intended to allocate storage to be used for input
1597 * to __xe_bo_create_locked(), in the case a pointer to the bo to be
1598 * created is needed before the call to __xe_bo_create_locked().
1599 * If __xe_bo_create_locked ends up never to be called, then the
1600 * storage allocated with this function needs to be freed using
1601 * xe_bo_free().
1602 *
1603 * Return: A pointer to an uninitialized struct xe_bo on success,
1604 * ERR_PTR(-ENOMEM) on error.
1605 */
xe_bo_alloc(void)1606 struct xe_bo *xe_bo_alloc(void)
1607 {
1608 struct xe_bo *bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1609
1610 if (!bo)
1611 return ERR_PTR(-ENOMEM);
1612
1613 return bo;
1614 }
1615
1616 /**
1617 * xe_bo_free - Free storage allocated using xe_bo_alloc()
1618 * @bo: The buffer object storage.
1619 *
1620 * Refer to xe_bo_alloc() documentation for valid use-cases.
1621 */
xe_bo_free(struct xe_bo * bo)1622 void xe_bo_free(struct xe_bo *bo)
1623 {
1624 kfree(bo);
1625 }
1626
___xe_bo_create_locked(struct xe_device * xe,struct xe_bo * bo,struct xe_tile * tile,struct dma_resv * resv,struct ttm_lru_bulk_move * bulk,size_t size,u16 cpu_caching,enum ttm_bo_type type,u32 flags)1627 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
1628 struct xe_tile *tile, struct dma_resv *resv,
1629 struct ttm_lru_bulk_move *bulk, size_t size,
1630 u16 cpu_caching, enum ttm_bo_type type,
1631 u32 flags)
1632 {
1633 struct ttm_operation_ctx ctx = {
1634 .interruptible = true,
1635 .no_wait_gpu = false,
1636 .gfp_retry_mayfail = true,
1637 };
1638 struct ttm_placement *placement;
1639 uint32_t alignment;
1640 size_t aligned_size;
1641 int err;
1642
1643 /* Only kernel objects should set GT */
1644 xe_assert(xe, !tile || type == ttm_bo_type_kernel);
1645
1646 if (XE_WARN_ON(!size)) {
1647 xe_bo_free(bo);
1648 return ERR_PTR(-EINVAL);
1649 }
1650
1651 /* XE_BO_FLAG_GGTTx requires XE_BO_FLAG_GGTT also be set */
1652 if ((flags & XE_BO_FLAG_GGTT_ALL) && !(flags & XE_BO_FLAG_GGTT))
1653 return ERR_PTR(-EINVAL);
1654
1655 if (flags & (XE_BO_FLAG_VRAM_MASK | XE_BO_FLAG_STOLEN) &&
1656 !(flags & XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE) &&
1657 ((xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) ||
1658 (flags & (XE_BO_FLAG_NEEDS_64K | XE_BO_FLAG_NEEDS_2M)))) {
1659 size_t align = flags & XE_BO_FLAG_NEEDS_2M ? SZ_2M : SZ_64K;
1660
1661 aligned_size = ALIGN(size, align);
1662 if (type != ttm_bo_type_device)
1663 size = ALIGN(size, align);
1664 flags |= XE_BO_FLAG_INTERNAL_64K;
1665 alignment = align >> PAGE_SHIFT;
1666 } else {
1667 aligned_size = ALIGN(size, SZ_4K);
1668 flags &= ~XE_BO_FLAG_INTERNAL_64K;
1669 alignment = SZ_4K >> PAGE_SHIFT;
1670 }
1671
1672 if (type == ttm_bo_type_device && aligned_size != size)
1673 return ERR_PTR(-EINVAL);
1674
1675 if (!bo) {
1676 bo = xe_bo_alloc();
1677 if (IS_ERR(bo))
1678 return bo;
1679 }
1680
1681 bo->ccs_cleared = false;
1682 bo->tile = tile;
1683 bo->size = size;
1684 bo->flags = flags;
1685 bo->cpu_caching = cpu_caching;
1686 bo->ttm.base.funcs = &xe_gem_object_funcs;
1687 bo->ttm.priority = XE_BO_PRIORITY_NORMAL;
1688 INIT_LIST_HEAD(&bo->pinned_link);
1689 #ifdef CONFIG_PROC_FS
1690 INIT_LIST_HEAD(&bo->client_link);
1691 #endif
1692 INIT_LIST_HEAD(&bo->vram_userfault_link);
1693
1694 drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size);
1695
1696 if (resv) {
1697 ctx.allow_res_evict = !(flags & XE_BO_FLAG_NO_RESV_EVICT);
1698 ctx.resv = resv;
1699 }
1700
1701 if (!(flags & XE_BO_FLAG_FIXED_PLACEMENT)) {
1702 err = __xe_bo_placement_for_flags(xe, bo, bo->flags);
1703 if (WARN_ON(err)) {
1704 xe_ttm_bo_destroy(&bo->ttm);
1705 return ERR_PTR(err);
1706 }
1707 }
1708
1709 /* Defer populating type_sg bos */
1710 placement = (type == ttm_bo_type_sg ||
1711 bo->flags & XE_BO_FLAG_DEFER_BACKING) ? &sys_placement :
1712 &bo->placement;
1713 err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type,
1714 placement, alignment,
1715 &ctx, NULL, resv, xe_ttm_bo_destroy);
1716 if (err)
1717 return ERR_PTR(err);
1718
1719 /*
1720 * The VRAM pages underneath are potentially still being accessed by the
1721 * GPU, as per async GPU clearing and async evictions. However TTM makes
1722 * sure to add any corresponding move/clear fences into the objects
1723 * dma-resv using the DMA_RESV_USAGE_KERNEL slot.
1724 *
1725 * For KMD internal buffers we don't care about GPU clearing, however we
1726 * still need to handle async evictions, where the VRAM is still being
1727 * accessed by the GPU. Most internal callers are not expecting this,
1728 * since they are missing the required synchronisation before accessing
1729 * the memory. To keep things simple just sync wait any kernel fences
1730 * here, if the buffer is designated KMD internal.
1731 *
1732 * For normal userspace objects we should already have the required
1733 * pipelining or sync waiting elsewhere, since we already have to deal
1734 * with things like async GPU clearing.
1735 */
1736 if (type == ttm_bo_type_kernel) {
1737 long timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
1738 DMA_RESV_USAGE_KERNEL,
1739 ctx.interruptible,
1740 MAX_SCHEDULE_TIMEOUT);
1741
1742 if (timeout < 0) {
1743 if (!resv)
1744 dma_resv_unlock(bo->ttm.base.resv);
1745 xe_bo_put(bo);
1746 return ERR_PTR(timeout);
1747 }
1748 }
1749
1750 bo->created = true;
1751 if (bulk)
1752 ttm_bo_set_bulk_move(&bo->ttm, bulk);
1753 else
1754 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1755
1756 return bo;
1757 }
1758
__xe_bo_fixed_placement(struct xe_device * xe,struct xe_bo * bo,u32 flags,u64 start,u64 end,u64 size)1759 static int __xe_bo_fixed_placement(struct xe_device *xe,
1760 struct xe_bo *bo,
1761 u32 flags,
1762 u64 start, u64 end, u64 size)
1763 {
1764 struct ttm_place *place = bo->placements;
1765
1766 if (flags & (XE_BO_FLAG_USER | XE_BO_FLAG_SYSTEM))
1767 return -EINVAL;
1768
1769 place->flags = TTM_PL_FLAG_CONTIGUOUS;
1770 place->fpfn = start >> PAGE_SHIFT;
1771 place->lpfn = end >> PAGE_SHIFT;
1772
1773 switch (flags & (XE_BO_FLAG_STOLEN | XE_BO_FLAG_VRAM_MASK)) {
1774 case XE_BO_FLAG_VRAM0:
1775 place->mem_type = XE_PL_VRAM0;
1776 break;
1777 case XE_BO_FLAG_VRAM1:
1778 place->mem_type = XE_PL_VRAM1;
1779 break;
1780 case XE_BO_FLAG_STOLEN:
1781 place->mem_type = XE_PL_STOLEN;
1782 break;
1783
1784 default:
1785 /* 0 or multiple of the above set */
1786 return -EINVAL;
1787 }
1788
1789 bo->placement = (struct ttm_placement) {
1790 .num_placement = 1,
1791 .placement = place,
1792 };
1793
1794 return 0;
1795 }
1796
1797 static struct xe_bo *
__xe_bo_create_locked(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,u64 start,u64 end,u16 cpu_caching,enum ttm_bo_type type,u32 flags,u64 alignment)1798 __xe_bo_create_locked(struct xe_device *xe,
1799 struct xe_tile *tile, struct xe_vm *vm,
1800 size_t size, u64 start, u64 end,
1801 u16 cpu_caching, enum ttm_bo_type type, u32 flags,
1802 u64 alignment)
1803 {
1804 struct xe_bo *bo = NULL;
1805 int err;
1806
1807 if (vm)
1808 xe_vm_assert_held(vm);
1809
1810 if (start || end != ~0ULL) {
1811 bo = xe_bo_alloc();
1812 if (IS_ERR(bo))
1813 return bo;
1814
1815 flags |= XE_BO_FLAG_FIXED_PLACEMENT;
1816 err = __xe_bo_fixed_placement(xe, bo, flags, start, end, size);
1817 if (err) {
1818 xe_bo_free(bo);
1819 return ERR_PTR(err);
1820 }
1821 }
1822
1823 bo = ___xe_bo_create_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL,
1824 vm && !xe_vm_in_fault_mode(vm) &&
1825 flags & XE_BO_FLAG_USER ?
1826 &vm->lru_bulk_move : NULL, size,
1827 cpu_caching, type, flags);
1828 if (IS_ERR(bo))
1829 return bo;
1830
1831 bo->min_align = alignment;
1832
1833 /*
1834 * Note that instead of taking a reference no the drm_gpuvm_resv_bo(),
1835 * to ensure the shared resv doesn't disappear under the bo, the bo
1836 * will keep a reference to the vm, and avoid circular references
1837 * by having all the vm's bo refereferences released at vm close
1838 * time.
1839 */
1840 if (vm && xe_bo_is_user(bo))
1841 xe_vm_get(vm);
1842 bo->vm = vm;
1843
1844 if (bo->flags & XE_BO_FLAG_GGTT) {
1845 struct xe_tile *t;
1846 u8 id;
1847
1848 if (!(bo->flags & XE_BO_FLAG_GGTT_ALL)) {
1849 if (!tile && flags & XE_BO_FLAG_STOLEN)
1850 tile = xe_device_get_root_tile(xe);
1851
1852 xe_assert(xe, tile);
1853 }
1854
1855 for_each_tile(t, xe, id) {
1856 if (t != tile && !(bo->flags & XE_BO_FLAG_GGTTx(t)))
1857 continue;
1858
1859 if (flags & XE_BO_FLAG_FIXED_PLACEMENT) {
1860 err = xe_ggtt_insert_bo_at(t->mem.ggtt, bo,
1861 start + bo->size, U64_MAX);
1862 } else {
1863 err = xe_ggtt_insert_bo(t->mem.ggtt, bo);
1864 }
1865 if (err)
1866 goto err_unlock_put_bo;
1867 }
1868 }
1869
1870 trace_xe_bo_create(bo);
1871 return bo;
1872
1873 err_unlock_put_bo:
1874 __xe_bo_unset_bulk_move(bo);
1875 xe_bo_unlock_vm_held(bo);
1876 xe_bo_put(bo);
1877 return ERR_PTR(err);
1878 }
1879
1880 struct xe_bo *
xe_bo_create_locked_range(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,u64 start,u64 end,enum ttm_bo_type type,u32 flags,u64 alignment)1881 xe_bo_create_locked_range(struct xe_device *xe,
1882 struct xe_tile *tile, struct xe_vm *vm,
1883 size_t size, u64 start, u64 end,
1884 enum ttm_bo_type type, u32 flags, u64 alignment)
1885 {
1886 return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type,
1887 flags, alignment);
1888 }
1889
xe_bo_create_locked(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,enum ttm_bo_type type,u32 flags)1890 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
1891 struct xe_vm *vm, size_t size,
1892 enum ttm_bo_type type, u32 flags)
1893 {
1894 return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type,
1895 flags, 0);
1896 }
1897
xe_bo_create_user(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,u16 cpu_caching,u32 flags)1898 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
1899 struct xe_vm *vm, size_t size,
1900 u16 cpu_caching,
1901 u32 flags)
1902 {
1903 struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL,
1904 cpu_caching, ttm_bo_type_device,
1905 flags | XE_BO_FLAG_USER, 0);
1906 if (!IS_ERR(bo))
1907 xe_bo_unlock_vm_held(bo);
1908
1909 return bo;
1910 }
1911
xe_bo_create(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,enum ttm_bo_type type,u32 flags)1912 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
1913 struct xe_vm *vm, size_t size,
1914 enum ttm_bo_type type, u32 flags)
1915 {
1916 struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags);
1917
1918 if (!IS_ERR(bo))
1919 xe_bo_unlock_vm_held(bo);
1920
1921 return bo;
1922 }
1923
xe_bo_create_pin_map_at(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,u64 offset,enum ttm_bo_type type,u32 flags)1924 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
1925 struct xe_vm *vm,
1926 size_t size, u64 offset,
1927 enum ttm_bo_type type, u32 flags)
1928 {
1929 return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, offset,
1930 type, flags, 0);
1931 }
1932
xe_bo_create_pin_map_at_aligned(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,u64 offset,enum ttm_bo_type type,u32 flags,u64 alignment)1933 struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
1934 struct xe_tile *tile,
1935 struct xe_vm *vm,
1936 size_t size, u64 offset,
1937 enum ttm_bo_type type, u32 flags,
1938 u64 alignment)
1939 {
1940 struct xe_bo *bo;
1941 int err;
1942 u64 start = offset == ~0ull ? 0 : offset;
1943 u64 end = offset == ~0ull ? offset : start + size;
1944
1945 if (flags & XE_BO_FLAG_STOLEN &&
1946 xe_ttm_stolen_cpu_access_needs_ggtt(xe))
1947 flags |= XE_BO_FLAG_GGTT;
1948
1949 bo = xe_bo_create_locked_range(xe, tile, vm, size, start, end, type,
1950 flags | XE_BO_FLAG_NEEDS_CPU_ACCESS,
1951 alignment);
1952 if (IS_ERR(bo))
1953 return bo;
1954
1955 err = xe_bo_pin(bo);
1956 if (err)
1957 goto err_put;
1958
1959 err = xe_bo_vmap(bo);
1960 if (err)
1961 goto err_unpin;
1962
1963 xe_bo_unlock_vm_held(bo);
1964
1965 return bo;
1966
1967 err_unpin:
1968 xe_bo_unpin(bo);
1969 err_put:
1970 xe_bo_unlock_vm_held(bo);
1971 xe_bo_put(bo);
1972 return ERR_PTR(err);
1973 }
1974
xe_bo_create_pin_map(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,size_t size,enum ttm_bo_type type,u32 flags)1975 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
1976 struct xe_vm *vm, size_t size,
1977 enum ttm_bo_type type, u32 flags)
1978 {
1979 return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull, type, flags);
1980 }
1981
xe_bo_create_from_data(struct xe_device * xe,struct xe_tile * tile,const void * data,size_t size,enum ttm_bo_type type,u32 flags)1982 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
1983 const void *data, size_t size,
1984 enum ttm_bo_type type, u32 flags)
1985 {
1986 struct xe_bo *bo = xe_bo_create_pin_map(xe, tile, NULL,
1987 ALIGN(size, PAGE_SIZE),
1988 type, flags);
1989 if (IS_ERR(bo))
1990 return bo;
1991
1992 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
1993
1994 return bo;
1995 }
1996
__xe_bo_unpin_map_no_vm(void * arg)1997 static void __xe_bo_unpin_map_no_vm(void *arg)
1998 {
1999 xe_bo_unpin_map_no_vm(arg);
2000 }
2001
xe_managed_bo_create_pin_map(struct xe_device * xe,struct xe_tile * tile,size_t size,u32 flags)2002 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
2003 size_t size, u32 flags)
2004 {
2005 struct xe_bo *bo;
2006 int ret;
2007
2008 KUNIT_STATIC_STUB_REDIRECT(xe_managed_bo_create_pin_map, xe, tile, size, flags);
2009
2010 bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel, flags);
2011 if (IS_ERR(bo))
2012 return bo;
2013
2014 ret = devm_add_action_or_reset(xe->drm.dev, __xe_bo_unpin_map_no_vm, bo);
2015 if (ret)
2016 return ERR_PTR(ret);
2017
2018 return bo;
2019 }
2020
xe_managed_bo_create_from_data(struct xe_device * xe,struct xe_tile * tile,const void * data,size_t size,u32 flags)2021 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
2022 const void *data, size_t size, u32 flags)
2023 {
2024 struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags);
2025
2026 if (IS_ERR(bo))
2027 return bo;
2028
2029 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
2030
2031 return bo;
2032 }
2033
2034 /**
2035 * xe_managed_bo_reinit_in_vram
2036 * @xe: xe device
2037 * @tile: Tile where the new buffer will be created
2038 * @src: Managed buffer object allocated in system memory
2039 *
2040 * Replace a managed src buffer object allocated in system memory with a new
2041 * one allocated in vram, copying the data between them.
2042 * Buffer object in VRAM is not going to have the same GGTT address, the caller
2043 * is responsible for making sure that any old references to it are updated.
2044 *
2045 * Returns 0 for success, negative error code otherwise.
2046 */
xe_managed_bo_reinit_in_vram(struct xe_device * xe,struct xe_tile * tile,struct xe_bo ** src)2047 int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src)
2048 {
2049 struct xe_bo *bo;
2050 u32 dst_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile) | XE_BO_FLAG_GGTT;
2051
2052 dst_flags |= (*src)->flags & XE_BO_FLAG_GGTT_INVALIDATE;
2053
2054 xe_assert(xe, IS_DGFX(xe));
2055 xe_assert(xe, !(*src)->vmap.is_iomem);
2056
2057 bo = xe_managed_bo_create_from_data(xe, tile, (*src)->vmap.vaddr,
2058 (*src)->size, dst_flags);
2059 if (IS_ERR(bo))
2060 return PTR_ERR(bo);
2061
2062 devm_release_action(xe->drm.dev, __xe_bo_unpin_map_no_vm, *src);
2063 *src = bo;
2064
2065 return 0;
2066 }
2067
2068 /*
2069 * XXX: This is in the VM bind data path, likely should calculate this once and
2070 * store, with a recalculation if the BO is moved.
2071 */
vram_region_gpu_offset(struct ttm_resource * res)2072 uint64_t vram_region_gpu_offset(struct ttm_resource *res)
2073 {
2074 struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
2075
2076 if (res->mem_type == XE_PL_STOLEN)
2077 return xe_ttm_stolen_gpu_offset(xe);
2078
2079 return res_to_mem_region(res)->dpa_base;
2080 }
2081
2082 /**
2083 * xe_bo_pin_external - pin an external BO
2084 * @bo: buffer object to be pinned
2085 *
2086 * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD)
2087 * BO. Unique call compared to xe_bo_pin as this function has it own set of
2088 * asserts and code to ensure evict / restore on suspend / resume.
2089 *
2090 * Returns 0 for success, negative error code otherwise.
2091 */
xe_bo_pin_external(struct xe_bo * bo)2092 int xe_bo_pin_external(struct xe_bo *bo)
2093 {
2094 struct xe_device *xe = xe_bo_device(bo);
2095 int err;
2096
2097 xe_assert(xe, !bo->vm);
2098 xe_assert(xe, xe_bo_is_user(bo));
2099
2100 if (!xe_bo_is_pinned(bo)) {
2101 err = xe_bo_validate(bo, NULL, false);
2102 if (err)
2103 return err;
2104
2105 if (xe_bo_is_vram(bo)) {
2106 spin_lock(&xe->pinned.lock);
2107 list_add_tail(&bo->pinned_link,
2108 &xe->pinned.external_vram);
2109 spin_unlock(&xe->pinned.lock);
2110 }
2111 }
2112
2113 ttm_bo_pin(&bo->ttm);
2114 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2115 xe_ttm_tt_account_subtract(bo->ttm.ttm);
2116
2117 /*
2118 * FIXME: If we always use the reserve / unreserve functions for locking
2119 * we do not need this.
2120 */
2121 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
2122
2123 return 0;
2124 }
2125
xe_bo_pin(struct xe_bo * bo)2126 int xe_bo_pin(struct xe_bo *bo)
2127 {
2128 struct ttm_place *place = &bo->placements[0];
2129 struct xe_device *xe = xe_bo_device(bo);
2130 int err;
2131
2132 /* We currently don't expect user BO to be pinned */
2133 xe_assert(xe, !xe_bo_is_user(bo));
2134
2135 /* Pinned object must be in GGTT or have pinned flag */
2136 xe_assert(xe, bo->flags & (XE_BO_FLAG_PINNED |
2137 XE_BO_FLAG_GGTT));
2138
2139 /*
2140 * No reason we can't support pinning imported dma-bufs we just don't
2141 * expect to pin an imported dma-buf.
2142 */
2143 xe_assert(xe, !bo->ttm.base.import_attach);
2144
2145 /* We only expect at most 1 pin */
2146 xe_assert(xe, !xe_bo_is_pinned(bo));
2147
2148 err = xe_bo_validate(bo, NULL, false);
2149 if (err)
2150 return err;
2151
2152 /*
2153 * For pinned objects in on DGFX, which are also in vram, we expect
2154 * these to be in contiguous VRAM memory. Required eviction / restore
2155 * during suspend / resume (force restore to same physical address).
2156 */
2157 if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) &&
2158 bo->flags & XE_BO_FLAG_INTERNAL_TEST)) {
2159 if (mem_type_is_vram(place->mem_type)) {
2160 xe_assert(xe, place->flags & TTM_PL_FLAG_CONTIGUOUS);
2161
2162 place->fpfn = (xe_bo_addr(bo, 0, PAGE_SIZE) -
2163 vram_region_gpu_offset(bo->ttm.resource)) >> PAGE_SHIFT;
2164 place->lpfn = place->fpfn + (bo->size >> PAGE_SHIFT);
2165 }
2166 }
2167
2168 if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) {
2169 spin_lock(&xe->pinned.lock);
2170 list_add_tail(&bo->pinned_link, &xe->pinned.kernel_bo_present);
2171 spin_unlock(&xe->pinned.lock);
2172 }
2173
2174 ttm_bo_pin(&bo->ttm);
2175 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2176 xe_ttm_tt_account_subtract(bo->ttm.ttm);
2177
2178 /*
2179 * FIXME: If we always use the reserve / unreserve functions for locking
2180 * we do not need this.
2181 */
2182 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
2183
2184 return 0;
2185 }
2186
2187 /**
2188 * xe_bo_unpin_external - unpin an external BO
2189 * @bo: buffer object to be unpinned
2190 *
2191 * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD)
2192 * BO. Unique call compared to xe_bo_unpin as this function has it own set of
2193 * asserts and code to ensure evict / restore on suspend / resume.
2194 *
2195 * Returns 0 for success, negative error code otherwise.
2196 */
xe_bo_unpin_external(struct xe_bo * bo)2197 void xe_bo_unpin_external(struct xe_bo *bo)
2198 {
2199 struct xe_device *xe = xe_bo_device(bo);
2200
2201 xe_assert(xe, !bo->vm);
2202 xe_assert(xe, xe_bo_is_pinned(bo));
2203 xe_assert(xe, xe_bo_is_user(bo));
2204
2205 spin_lock(&xe->pinned.lock);
2206 if (bo->ttm.pin_count == 1 && !list_empty(&bo->pinned_link))
2207 list_del_init(&bo->pinned_link);
2208 spin_unlock(&xe->pinned.lock);
2209
2210 ttm_bo_unpin(&bo->ttm);
2211 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2212 xe_ttm_tt_account_add(bo->ttm.ttm);
2213
2214 /*
2215 * FIXME: If we always use the reserve / unreserve functions for locking
2216 * we do not need this.
2217 */
2218 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
2219 }
2220
xe_bo_unpin(struct xe_bo * bo)2221 void xe_bo_unpin(struct xe_bo *bo)
2222 {
2223 struct ttm_place *place = &bo->placements[0];
2224 struct xe_device *xe = xe_bo_device(bo);
2225
2226 xe_assert(xe, !bo->ttm.base.import_attach);
2227 xe_assert(xe, xe_bo_is_pinned(bo));
2228
2229 if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) {
2230 spin_lock(&xe->pinned.lock);
2231 xe_assert(xe, !list_empty(&bo->pinned_link));
2232 list_del_init(&bo->pinned_link);
2233 spin_unlock(&xe->pinned.lock);
2234 }
2235 ttm_bo_unpin(&bo->ttm);
2236 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2237 xe_ttm_tt_account_add(bo->ttm.ttm);
2238 }
2239
2240 /**
2241 * xe_bo_validate() - Make sure the bo is in an allowed placement
2242 * @bo: The bo,
2243 * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or
2244 * NULL. Used together with @allow_res_evict.
2245 * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's
2246 * reservation object.
2247 *
2248 * Make sure the bo is in allowed placement, migrating it if necessary. If
2249 * needed, other bos will be evicted. If bos selected for eviction shares
2250 * the @vm's reservation object, they can be evicted iff @allow_res_evict is
2251 * set to true, otherwise they will be bypassed.
2252 *
2253 * Return: 0 on success, negative error code on failure. May return
2254 * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal.
2255 */
xe_bo_validate(struct xe_bo * bo,struct xe_vm * vm,bool allow_res_evict)2256 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict)
2257 {
2258 struct ttm_operation_ctx ctx = {
2259 .interruptible = true,
2260 .no_wait_gpu = false,
2261 .gfp_retry_mayfail = true,
2262 };
2263
2264 if (vm) {
2265 lockdep_assert_held(&vm->lock);
2266 xe_vm_assert_held(vm);
2267
2268 ctx.allow_res_evict = allow_res_evict;
2269 ctx.resv = xe_vm_resv(vm);
2270 }
2271
2272 trace_xe_bo_validate(bo);
2273 return ttm_bo_validate(&bo->ttm, &bo->placement, &ctx);
2274 }
2275
xe_bo_is_xe_bo(struct ttm_buffer_object * bo)2276 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo)
2277 {
2278 if (bo->destroy == &xe_ttm_bo_destroy)
2279 return true;
2280
2281 return false;
2282 }
2283
2284 /*
2285 * Resolve a BO address. There is no assert to check if the proper lock is held
2286 * so it should only be used in cases where it is not fatal to get the wrong
2287 * address, such as printing debug information, but not in cases where memory is
2288 * written based on this result.
2289 */
__xe_bo_addr(struct xe_bo * bo,u64 offset,size_t page_size)2290 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
2291 {
2292 struct xe_device *xe = xe_bo_device(bo);
2293 struct xe_res_cursor cur;
2294 u64 page;
2295
2296 xe_assert(xe, page_size <= PAGE_SIZE);
2297 page = offset >> PAGE_SHIFT;
2298 offset &= (PAGE_SIZE - 1);
2299
2300 if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
2301 xe_assert(xe, bo->ttm.ttm);
2302
2303 xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
2304 page_size, &cur);
2305 return xe_res_dma(&cur) + offset;
2306 } else {
2307 struct xe_res_cursor cur;
2308
2309 xe_res_first(bo->ttm.resource, page << PAGE_SHIFT,
2310 page_size, &cur);
2311 return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource);
2312 }
2313 }
2314
xe_bo_addr(struct xe_bo * bo,u64 offset,size_t page_size)2315 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
2316 {
2317 if (!READ_ONCE(bo->ttm.pin_count))
2318 xe_bo_assert_held(bo);
2319 return __xe_bo_addr(bo, offset, page_size);
2320 }
2321
xe_bo_vmap(struct xe_bo * bo)2322 int xe_bo_vmap(struct xe_bo *bo)
2323 {
2324 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
2325 void *virtual;
2326 bool is_iomem;
2327 int ret;
2328
2329 xe_bo_assert_held(bo);
2330
2331 if (drm_WARN_ON(&xe->drm, !(bo->flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) ||
2332 !force_contiguous(bo->flags)))
2333 return -EINVAL;
2334
2335 if (!iosys_map_is_null(&bo->vmap))
2336 return 0;
2337
2338 /*
2339 * We use this more or less deprecated interface for now since
2340 * ttm_bo_vmap() doesn't offer the optimization of kmapping
2341 * single page bos, which is done here.
2342 * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap
2343 * to use struct iosys_map.
2344 */
2345 ret = ttm_bo_kmap(&bo->ttm, 0, bo->size >> PAGE_SHIFT, &bo->kmap);
2346 if (ret)
2347 return ret;
2348
2349 virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
2350 if (is_iomem)
2351 iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual);
2352 else
2353 iosys_map_set_vaddr(&bo->vmap, virtual);
2354
2355 return 0;
2356 }
2357
__xe_bo_vunmap(struct xe_bo * bo)2358 static void __xe_bo_vunmap(struct xe_bo *bo)
2359 {
2360 if (!iosys_map_is_null(&bo->vmap)) {
2361 iosys_map_clear(&bo->vmap);
2362 ttm_bo_kunmap(&bo->kmap);
2363 }
2364 }
2365
xe_bo_vunmap(struct xe_bo * bo)2366 void xe_bo_vunmap(struct xe_bo *bo)
2367 {
2368 xe_bo_assert_held(bo);
2369 __xe_bo_vunmap(bo);
2370 }
2371
gem_create_set_pxp_type(struct xe_device * xe,struct xe_bo * bo,u64 value)2372 static int gem_create_set_pxp_type(struct xe_device *xe, struct xe_bo *bo, u64 value)
2373 {
2374 if (value == DRM_XE_PXP_TYPE_NONE)
2375 return 0;
2376
2377 /* we only support DRM_XE_PXP_TYPE_HWDRM for now */
2378 if (XE_IOCTL_DBG(xe, value != DRM_XE_PXP_TYPE_HWDRM))
2379 return -EINVAL;
2380
2381 return xe_pxp_key_assign(xe->pxp, bo);
2382 }
2383
2384 typedef int (*xe_gem_create_set_property_fn)(struct xe_device *xe,
2385 struct xe_bo *bo,
2386 u64 value);
2387
2388 static const xe_gem_create_set_property_fn gem_create_set_property_funcs[] = {
2389 [DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY] = gem_create_set_pxp_type,
2390 };
2391
gem_create_user_ext_set_property(struct xe_device * xe,struct xe_bo * bo,u64 extension)2392 static int gem_create_user_ext_set_property(struct xe_device *xe,
2393 struct xe_bo *bo,
2394 u64 extension)
2395 {
2396 u64 __user *address = u64_to_user_ptr(extension);
2397 struct drm_xe_ext_set_property ext;
2398 int err;
2399 u32 idx;
2400
2401 err = __copy_from_user(&ext, address, sizeof(ext));
2402 if (XE_IOCTL_DBG(xe, err))
2403 return -EFAULT;
2404
2405 if (XE_IOCTL_DBG(xe, ext.property >=
2406 ARRAY_SIZE(gem_create_set_property_funcs)) ||
2407 XE_IOCTL_DBG(xe, ext.pad) ||
2408 XE_IOCTL_DBG(xe, ext.property != DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY))
2409 return -EINVAL;
2410
2411 idx = array_index_nospec(ext.property, ARRAY_SIZE(gem_create_set_property_funcs));
2412 if (!gem_create_set_property_funcs[idx])
2413 return -EINVAL;
2414
2415 return gem_create_set_property_funcs[idx](xe, bo, ext.value);
2416 }
2417
2418 typedef int (*xe_gem_create_user_extension_fn)(struct xe_device *xe,
2419 struct xe_bo *bo,
2420 u64 extension);
2421
2422 static const xe_gem_create_user_extension_fn gem_create_user_extension_funcs[] = {
2423 [DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY] = gem_create_user_ext_set_property,
2424 };
2425
2426 #define MAX_USER_EXTENSIONS 16
gem_create_user_extensions(struct xe_device * xe,struct xe_bo * bo,u64 extensions,int ext_number)2427 static int gem_create_user_extensions(struct xe_device *xe, struct xe_bo *bo,
2428 u64 extensions, int ext_number)
2429 {
2430 u64 __user *address = u64_to_user_ptr(extensions);
2431 struct drm_xe_user_extension ext;
2432 int err;
2433 u32 idx;
2434
2435 if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS))
2436 return -E2BIG;
2437
2438 err = __copy_from_user(&ext, address, sizeof(ext));
2439 if (XE_IOCTL_DBG(xe, err))
2440 return -EFAULT;
2441
2442 if (XE_IOCTL_DBG(xe, ext.pad) ||
2443 XE_IOCTL_DBG(xe, ext.name >= ARRAY_SIZE(gem_create_user_extension_funcs)))
2444 return -EINVAL;
2445
2446 idx = array_index_nospec(ext.name,
2447 ARRAY_SIZE(gem_create_user_extension_funcs));
2448 err = gem_create_user_extension_funcs[idx](xe, bo, extensions);
2449 if (XE_IOCTL_DBG(xe, err))
2450 return err;
2451
2452 if (ext.next_extension)
2453 return gem_create_user_extensions(xe, bo, ext.next_extension,
2454 ++ext_number);
2455
2456 return 0;
2457 }
2458
xe_gem_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2459 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
2460 struct drm_file *file)
2461 {
2462 struct xe_device *xe = to_xe_device(dev);
2463 struct xe_file *xef = to_xe_file(file);
2464 struct drm_xe_gem_create *args = data;
2465 struct xe_vm *vm = NULL;
2466 ktime_t end = 0;
2467 struct xe_bo *bo;
2468 unsigned int bo_flags;
2469 u32 handle;
2470 int err;
2471
2472 if (XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) ||
2473 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2474 return -EINVAL;
2475
2476 /* at least one valid memory placement must be specified */
2477 if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) ||
2478 !args->placement))
2479 return -EINVAL;
2480
2481 if (XE_IOCTL_DBG(xe, args->flags &
2482 ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING |
2483 DRM_XE_GEM_CREATE_FLAG_SCANOUT |
2484 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM)))
2485 return -EINVAL;
2486
2487 if (XE_IOCTL_DBG(xe, args->handle))
2488 return -EINVAL;
2489
2490 if (XE_IOCTL_DBG(xe, !args->size))
2491 return -EINVAL;
2492
2493 if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX))
2494 return -EINVAL;
2495
2496 if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK))
2497 return -EINVAL;
2498
2499 bo_flags = 0;
2500 if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING)
2501 bo_flags |= XE_BO_FLAG_DEFER_BACKING;
2502
2503 if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT)
2504 bo_flags |= XE_BO_FLAG_SCANOUT;
2505
2506 bo_flags |= args->placement << (ffs(XE_BO_FLAG_SYSTEM) - 1);
2507
2508 /* CCS formats need physical placement at a 64K alignment in VRAM. */
2509 if ((bo_flags & XE_BO_FLAG_VRAM_MASK) &&
2510 (bo_flags & XE_BO_FLAG_SCANOUT) &&
2511 !(xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) &&
2512 IS_ALIGNED(args->size, SZ_64K))
2513 bo_flags |= XE_BO_FLAG_NEEDS_64K;
2514
2515 if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) {
2516 if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_FLAG_VRAM_MASK)))
2517 return -EINVAL;
2518
2519 bo_flags |= XE_BO_FLAG_NEEDS_CPU_ACCESS;
2520 }
2521
2522 if (XE_IOCTL_DBG(xe, !args->cpu_caching ||
2523 args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC))
2524 return -EINVAL;
2525
2526 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_VRAM_MASK &&
2527 args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC))
2528 return -EINVAL;
2529
2530 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_SCANOUT &&
2531 args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB))
2532 return -EINVAL;
2533
2534 if (args->vm_id) {
2535 vm = xe_vm_lookup(xef, args->vm_id);
2536 if (XE_IOCTL_DBG(xe, !vm))
2537 return -ENOENT;
2538 }
2539
2540 retry:
2541 if (vm) {
2542 err = xe_vm_lock(vm, true);
2543 if (err)
2544 goto out_vm;
2545 }
2546
2547 bo = xe_bo_create_user(xe, NULL, vm, args->size, args->cpu_caching,
2548 bo_flags);
2549
2550 if (vm)
2551 xe_vm_unlock(vm);
2552
2553 if (IS_ERR(bo)) {
2554 err = PTR_ERR(bo);
2555 if (xe_vm_validate_should_retry(NULL, err, &end))
2556 goto retry;
2557 goto out_vm;
2558 }
2559
2560 if (args->extensions) {
2561 err = gem_create_user_extensions(xe, bo, args->extensions, 0);
2562 if (err)
2563 goto out_bulk;
2564 }
2565
2566 err = drm_gem_handle_create(file, &bo->ttm.base, &handle);
2567 if (err)
2568 goto out_bulk;
2569
2570 args->handle = handle;
2571 goto out_put;
2572
2573 out_bulk:
2574 if (vm && !xe_vm_in_fault_mode(vm)) {
2575 xe_vm_lock(vm, false);
2576 __xe_bo_unset_bulk_move(bo);
2577 xe_vm_unlock(vm);
2578 }
2579 out_put:
2580 xe_bo_put(bo);
2581 out_vm:
2582 if (vm)
2583 xe_vm_put(vm);
2584
2585 return err;
2586 }
2587
xe_gem_mmap_offset_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2588 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
2589 struct drm_file *file)
2590 {
2591 struct xe_device *xe = to_xe_device(dev);
2592 struct drm_xe_gem_mmap_offset *args = data;
2593 struct drm_gem_object *gem_obj;
2594
2595 if (XE_IOCTL_DBG(xe, args->extensions) ||
2596 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2597 return -EINVAL;
2598
2599 if (XE_IOCTL_DBG(xe, args->flags &
2600 ~DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER))
2601 return -EINVAL;
2602
2603 if (args->flags & DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER) {
2604 if (XE_IOCTL_DBG(xe, !IS_DGFX(xe)))
2605 return -EINVAL;
2606
2607 if (XE_IOCTL_DBG(xe, args->handle))
2608 return -EINVAL;
2609
2610 if (XE_IOCTL_DBG(xe, PAGE_SIZE > SZ_4K))
2611 return -EINVAL;
2612
2613 BUILD_BUG_ON(((XE_PCI_BARRIER_MMAP_OFFSET >> XE_PTE_SHIFT) +
2614 SZ_4K) >= DRM_FILE_PAGE_OFFSET_START);
2615 args->offset = XE_PCI_BARRIER_MMAP_OFFSET;
2616 return 0;
2617 }
2618
2619 gem_obj = drm_gem_object_lookup(file, args->handle);
2620 if (XE_IOCTL_DBG(xe, !gem_obj))
2621 return -ENOENT;
2622
2623 /* The mmap offset was set up at BO allocation time. */
2624 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
2625
2626 xe_bo_put(gem_to_xe_bo(gem_obj));
2627 return 0;
2628 }
2629
2630 /**
2631 * xe_bo_lock() - Lock the buffer object's dma_resv object
2632 * @bo: The struct xe_bo whose lock is to be taken
2633 * @intr: Whether to perform any wait interruptible
2634 *
2635 * Locks the buffer object's dma_resv object. If the buffer object is
2636 * pointing to a shared dma_resv object, that shared lock is locked.
2637 *
2638 * Return: 0 on success, -EINTR if @intr is true and the wait for a
2639 * contended lock was interrupted. If @intr is set to false, the
2640 * function always returns 0.
2641 */
xe_bo_lock(struct xe_bo * bo,bool intr)2642 int xe_bo_lock(struct xe_bo *bo, bool intr)
2643 {
2644 if (intr)
2645 return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL);
2646
2647 dma_resv_lock(bo->ttm.base.resv, NULL);
2648
2649 return 0;
2650 }
2651
2652 /**
2653 * xe_bo_unlock() - Unlock the buffer object's dma_resv object
2654 * @bo: The struct xe_bo whose lock is to be released.
2655 *
2656 * Unlock a buffer object lock that was locked by xe_bo_lock().
2657 */
xe_bo_unlock(struct xe_bo * bo)2658 void xe_bo_unlock(struct xe_bo *bo)
2659 {
2660 dma_resv_unlock(bo->ttm.base.resv);
2661 }
2662
2663 /**
2664 * xe_bo_can_migrate - Whether a buffer object likely can be migrated
2665 * @bo: The buffer object to migrate
2666 * @mem_type: The TTM memory type intended to migrate to
2667 *
2668 * Check whether the buffer object supports migration to the
2669 * given memory type. Note that pinning may affect the ability to migrate as
2670 * returned by this function.
2671 *
2672 * This function is primarily intended as a helper for checking the
2673 * possibility to migrate buffer objects and can be called without
2674 * the object lock held.
2675 *
2676 * Return: true if migration is possible, false otherwise.
2677 */
xe_bo_can_migrate(struct xe_bo * bo,u32 mem_type)2678 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type)
2679 {
2680 unsigned int cur_place;
2681
2682 if (bo->ttm.type == ttm_bo_type_kernel)
2683 return true;
2684
2685 if (bo->ttm.type == ttm_bo_type_sg)
2686 return false;
2687
2688 for (cur_place = 0; cur_place < bo->placement.num_placement;
2689 cur_place++) {
2690 if (bo->placements[cur_place].mem_type == mem_type)
2691 return true;
2692 }
2693
2694 return false;
2695 }
2696
xe_place_from_ttm_type(u32 mem_type,struct ttm_place * place)2697 static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
2698 {
2699 memset(place, 0, sizeof(*place));
2700 place->mem_type = mem_type;
2701 }
2702
2703 /**
2704 * xe_bo_migrate - Migrate an object to the desired region id
2705 * @bo: The buffer object to migrate.
2706 * @mem_type: The TTM region type to migrate to.
2707 *
2708 * Attempt to migrate the buffer object to the desired memory region. The
2709 * buffer object may not be pinned, and must be locked.
2710 * On successful completion, the object memory type will be updated,
2711 * but an async migration task may not have completed yet, and to
2712 * accomplish that, the object's kernel fences must be signaled with
2713 * the object lock held.
2714 *
2715 * Return: 0 on success. Negative error code on failure. In particular may
2716 * return -EINTR or -ERESTARTSYS if signal pending.
2717 */
xe_bo_migrate(struct xe_bo * bo,u32 mem_type)2718 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type)
2719 {
2720 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
2721 struct ttm_operation_ctx ctx = {
2722 .interruptible = true,
2723 .no_wait_gpu = false,
2724 .gfp_retry_mayfail = true,
2725 };
2726 struct ttm_placement placement;
2727 struct ttm_place requested;
2728
2729 xe_bo_assert_held(bo);
2730
2731 if (bo->ttm.resource->mem_type == mem_type)
2732 return 0;
2733
2734 if (xe_bo_is_pinned(bo))
2735 return -EBUSY;
2736
2737 if (!xe_bo_can_migrate(bo, mem_type))
2738 return -EINVAL;
2739
2740 xe_place_from_ttm_type(mem_type, &requested);
2741 placement.num_placement = 1;
2742 placement.placement = &requested;
2743
2744 /*
2745 * Stolen needs to be handled like below VRAM handling if we ever need
2746 * to support it.
2747 */
2748 drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN);
2749
2750 if (mem_type_is_vram(mem_type)) {
2751 u32 c = 0;
2752
2753 add_vram(xe, bo, &requested, bo->flags, mem_type, &c);
2754 }
2755
2756 return ttm_bo_validate(&bo->ttm, &placement, &ctx);
2757 }
2758
2759 /**
2760 * xe_bo_evict - Evict an object to evict placement
2761 * @bo: The buffer object to migrate.
2762 * @force_alloc: Set force_alloc in ttm_operation_ctx
2763 *
2764 * On successful completion, the object memory will be moved to evict
2765 * placement. This function blocks until the object has been fully moved.
2766 *
2767 * Return: 0 on success. Negative error code on failure.
2768 */
xe_bo_evict(struct xe_bo * bo,bool force_alloc)2769 int xe_bo_evict(struct xe_bo *bo, bool force_alloc)
2770 {
2771 struct ttm_operation_ctx ctx = {
2772 .interruptible = false,
2773 .no_wait_gpu = false,
2774 .force_alloc = force_alloc,
2775 .gfp_retry_mayfail = true,
2776 };
2777 struct ttm_placement placement;
2778 int ret;
2779
2780 xe_evict_flags(&bo->ttm, &placement);
2781 ret = ttm_bo_validate(&bo->ttm, &placement, &ctx);
2782 if (ret)
2783 return ret;
2784
2785 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
2786 false, MAX_SCHEDULE_TIMEOUT);
2787
2788 return 0;
2789 }
2790
2791 /**
2792 * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when
2793 * placed in system memory.
2794 * @bo: The xe_bo
2795 *
2796 * Return: true if extra pages need to be allocated, false otherwise.
2797 */
xe_bo_needs_ccs_pages(struct xe_bo * bo)2798 bool xe_bo_needs_ccs_pages(struct xe_bo *bo)
2799 {
2800 struct xe_device *xe = xe_bo_device(bo);
2801
2802 if (GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe))
2803 return false;
2804
2805 if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device)
2806 return false;
2807
2808 /* On discrete GPUs, if the GPU can access this buffer from
2809 * system memory (i.e., it allows XE_PL_TT placement), FlatCCS
2810 * can't be used since there's no CCS storage associated with
2811 * non-VRAM addresses.
2812 */
2813 if (IS_DGFX(xe) && (bo->flags & XE_BO_FLAG_SYSTEM))
2814 return false;
2815
2816 return true;
2817 }
2818
2819 /**
2820 * __xe_bo_release_dummy() - Dummy kref release function
2821 * @kref: The embedded struct kref.
2822 *
2823 * Dummy release function for xe_bo_put_deferred(). Keep off.
2824 */
__xe_bo_release_dummy(struct kref * kref)2825 void __xe_bo_release_dummy(struct kref *kref)
2826 {
2827 }
2828
2829 /**
2830 * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred().
2831 * @deferred: The lockless list used for the call to xe_bo_put_deferred().
2832 *
2833 * Puts all bos whose put was deferred by xe_bo_put_deferred().
2834 * The @deferred list can be either an onstack local list or a global
2835 * shared list used by a workqueue.
2836 */
xe_bo_put_commit(struct llist_head * deferred)2837 void xe_bo_put_commit(struct llist_head *deferred)
2838 {
2839 struct llist_node *freed;
2840 struct xe_bo *bo, *next;
2841
2842 if (!deferred)
2843 return;
2844
2845 freed = llist_del_all(deferred);
2846 if (!freed)
2847 return;
2848
2849 llist_for_each_entry_safe(bo, next, freed, freed)
2850 drm_gem_object_free(&bo->ttm.base.refcount);
2851 }
2852
xe_bo_dev_work_func(struct work_struct * work)2853 static void xe_bo_dev_work_func(struct work_struct *work)
2854 {
2855 struct xe_bo_dev *bo_dev = container_of(work, typeof(*bo_dev), async_free);
2856
2857 xe_bo_put_commit(&bo_dev->async_list);
2858 }
2859
2860 /**
2861 * xe_bo_dev_init() - Initialize BO dev to manage async BO freeing
2862 * @bo_dev: The BO dev structure
2863 */
xe_bo_dev_init(struct xe_bo_dev * bo_dev)2864 void xe_bo_dev_init(struct xe_bo_dev *bo_dev)
2865 {
2866 INIT_WORK(&bo_dev->async_free, xe_bo_dev_work_func);
2867 }
2868
2869 /**
2870 * xe_bo_dev_fini() - Finalize BO dev managing async BO freeing
2871 * @bo_dev: The BO dev structure
2872 */
xe_bo_dev_fini(struct xe_bo_dev * bo_dev)2873 void xe_bo_dev_fini(struct xe_bo_dev *bo_dev)
2874 {
2875 flush_work(&bo_dev->async_free);
2876 }
2877
xe_bo_put(struct xe_bo * bo)2878 void xe_bo_put(struct xe_bo *bo)
2879 {
2880 struct xe_tile *tile;
2881 u8 id;
2882
2883 might_sleep();
2884 if (bo) {
2885 #ifdef CONFIG_PROC_FS
2886 if (bo->client)
2887 might_lock(&bo->client->bos_lock);
2888 #endif
2889 for_each_tile(tile, xe_bo_device(bo), id)
2890 if (bo->ggtt_node[id] && bo->ggtt_node[id]->ggtt)
2891 might_lock(&bo->ggtt_node[id]->ggtt->lock);
2892 drm_gem_object_put(&bo->ttm.base);
2893 }
2894 }
2895
2896 /**
2897 * xe_bo_dumb_create - Create a dumb bo as backing for a fb
2898 * @file_priv: ...
2899 * @dev: ...
2900 * @args: ...
2901 *
2902 * See dumb_create() hook in include/drm/drm_drv.h
2903 *
2904 * Return: ...
2905 */
xe_bo_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)2906 int xe_bo_dumb_create(struct drm_file *file_priv,
2907 struct drm_device *dev,
2908 struct drm_mode_create_dumb *args)
2909 {
2910 struct xe_device *xe = to_xe_device(dev);
2911 struct xe_bo *bo;
2912 uint32_t handle;
2913 int cpp = DIV_ROUND_UP(args->bpp, 8);
2914 int err;
2915 u32 page_size = max_t(u32, PAGE_SIZE,
2916 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K);
2917
2918 args->pitch = ALIGN(args->width * cpp, 64);
2919 args->size = ALIGN(mul_u32_u32(args->pitch, args->height),
2920 page_size);
2921
2922 bo = xe_bo_create_user(xe, NULL, NULL, args->size,
2923 DRM_XE_GEM_CPU_CACHING_WC,
2924 XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
2925 XE_BO_FLAG_SCANOUT |
2926 XE_BO_FLAG_NEEDS_CPU_ACCESS);
2927 if (IS_ERR(bo))
2928 return PTR_ERR(bo);
2929
2930 err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle);
2931 /* drop reference from allocate - handle holds it now */
2932 drm_gem_object_put(&bo->ttm.base);
2933 if (!err)
2934 args->handle = handle;
2935 return err;
2936 }
2937
xe_bo_runtime_pm_release_mmap_offset(struct xe_bo * bo)2938 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo)
2939 {
2940 struct ttm_buffer_object *tbo = &bo->ttm;
2941 struct ttm_device *bdev = tbo->bdev;
2942
2943 drm_vma_node_unmap(&tbo->base.vma_node, bdev->dev_mapping);
2944
2945 list_del_init(&bo->vram_userfault_link);
2946 }
2947
2948 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
2949 #include "tests/xe_bo.c"
2950 #endif
2951