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