xref: /linux/drivers/gpu/drm/xe/xe_bo.c (revision 16280ded45fba1216d1d4c6acfc20c2d5b45ef50)
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 
80 bool mem_type_is_vram(u32 mem_type)
81 {
82 	return mem_type >= XE_PL_VRAM0 && mem_type != XE_PL_STOLEN;
83 }
84 
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 
90 static bool resource_is_vram(struct ttm_resource *res)
91 {
92 	return mem_type_is_vram(res->mem_type);
93 }
94 
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 
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  */
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  */
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  */
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 
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 *
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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  */
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 
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 
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 
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 
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 
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 
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 
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 
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  */
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  */
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 
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 	/* Reject BO eviction if BO is bound to current VM. */
845 	if (evict && ctx->resv) {
846 		struct drm_gpuvm_bo *vm_bo;
847 
848 		drm_gem_for_each_gpuvm_bo(vm_bo, &bo->ttm.base) {
849 			struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
850 
851 			if (xe_vm_resv(vm) == ctx->resv &&
852 			    xe_vm_in_preempt_fence_mode(vm)) {
853 				ret = -EBUSY;
854 				goto out;
855 			}
856 		}
857 	}
858 
859 	/*
860 	 * Failed multi-hop where the old_mem is still marked as
861 	 * TTM_PL_FLAG_TEMPORARY, should just be a dummy move.
862 	 */
863 	if (old_mem_type == XE_PL_TT &&
864 	    new_mem->mem_type == XE_PL_TT) {
865 		ttm_bo_move_null(ttm_bo, new_mem);
866 		goto out;
867 	}
868 
869 	if (!move_lacks_source && !xe_bo_is_pinned(bo)) {
870 		ret = xe_bo_move_notify(bo, ctx);
871 		if (ret)
872 			goto out;
873 	}
874 
875 	if (old_mem_type == XE_PL_TT &&
876 	    new_mem->mem_type == XE_PL_SYSTEM) {
877 		long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
878 						     DMA_RESV_USAGE_BOOKKEEP,
879 						     false,
880 						     MAX_SCHEDULE_TIMEOUT);
881 		if (timeout < 0) {
882 			ret = timeout;
883 			goto out;
884 		}
885 
886 		if (!handle_system_ccs) {
887 			ttm_bo_move_null(ttm_bo, new_mem);
888 			goto out;
889 		}
890 	}
891 
892 	if (!move_lacks_source &&
893 	    ((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) ||
894 	     (mem_type_is_vram(old_mem_type) &&
895 	      new_mem->mem_type == XE_PL_SYSTEM))) {
896 		hop->fpfn = 0;
897 		hop->lpfn = 0;
898 		hop->mem_type = XE_PL_TT;
899 		hop->flags = TTM_PL_FLAG_TEMPORARY;
900 		ret = -EMULTIHOP;
901 		goto out;
902 	}
903 
904 	if (bo->tile)
905 		migrate = bo->tile->migrate;
906 	else if (resource_is_vram(new_mem))
907 		migrate = mem_type_to_migrate(xe, new_mem->mem_type);
908 	else if (mem_type_is_vram(old_mem_type))
909 		migrate = mem_type_to_migrate(xe, old_mem_type);
910 	else
911 		migrate = xe->tiles[0].migrate;
912 
913 	xe_assert(xe, migrate);
914 	trace_xe_bo_move(bo, new_mem->mem_type, old_mem_type, move_lacks_source);
915 	if (xe_rpm_reclaim_safe(xe)) {
916 		/*
917 		 * We might be called through swapout in the validation path of
918 		 * another TTM device, so acquire rpm here.
919 		 */
920 		xe_pm_runtime_get(xe);
921 	} else {
922 		drm_WARN_ON(&xe->drm, handle_system_ccs);
923 		xe_pm_runtime_get_noresume(xe);
924 	}
925 
926 	if (move_lacks_source) {
927 		u32 flags = 0;
928 
929 		if (mem_type_is_vram(new_mem->mem_type))
930 			flags |= XE_MIGRATE_CLEAR_FLAG_FULL;
931 		else if (handle_system_ccs)
932 			flags |= XE_MIGRATE_CLEAR_FLAG_CCS_DATA;
933 
934 		fence = xe_migrate_clear(migrate, bo, new_mem, flags);
935 	} else {
936 		fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
937 					handle_system_ccs);
938 	}
939 	if (IS_ERR(fence)) {
940 		ret = PTR_ERR(fence);
941 		xe_pm_runtime_put(xe);
942 		goto out;
943 	}
944 	if (!move_lacks_source) {
945 		ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict, true,
946 						new_mem);
947 		if (ret) {
948 			dma_fence_wait(fence, false);
949 			ttm_bo_move_null(ttm_bo, new_mem);
950 			ret = 0;
951 		}
952 	} else {
953 		/*
954 		 * ttm_bo_move_accel_cleanup() may blow up if
955 		 * bo->resource == NULL, so just attach the
956 		 * fence and set the new resource.
957 		 */
958 		dma_resv_add_fence(ttm_bo->base.resv, fence,
959 				   DMA_RESV_USAGE_KERNEL);
960 		ttm_bo_move_null(ttm_bo, new_mem);
961 	}
962 
963 	dma_fence_put(fence);
964 	xe_pm_runtime_put(xe);
965 
966 out:
967 	if ((!ttm_bo->resource || ttm_bo->resource->mem_type == XE_PL_SYSTEM) &&
968 	    ttm_bo->ttm) {
969 		long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
970 						     DMA_RESV_USAGE_KERNEL,
971 						     false,
972 						     MAX_SCHEDULE_TIMEOUT);
973 		if (timeout < 0)
974 			ret = timeout;
975 
976 		xe_tt_unmap_sg(ttm_bo->ttm);
977 	}
978 
979 	return ret;
980 }
981 
982 static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx,
983 			       struct ttm_buffer_object *bo,
984 			       unsigned long *scanned)
985 {
986 	long lret;
987 
988 	/* Fake move to system, without copying data. */
989 	if (bo->resource->mem_type != XE_PL_SYSTEM) {
990 		struct ttm_resource *new_resource;
991 
992 		lret = ttm_bo_wait_ctx(bo, ctx);
993 		if (lret)
994 			return lret;
995 
996 		lret = ttm_bo_mem_space(bo, &sys_placement, &new_resource, ctx);
997 		if (lret)
998 			return lret;
999 
1000 		xe_tt_unmap_sg(bo->ttm);
1001 		ttm_bo_move_null(bo, new_resource);
1002 	}
1003 
1004 	*scanned += bo->ttm->num_pages;
1005 	lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags)
1006 			     {.purge = true,
1007 			      .writeback = false,
1008 			      .allow_move = false});
1009 
1010 	if (lret > 0)
1011 		xe_ttm_tt_account_subtract(bo->ttm);
1012 
1013 	return lret;
1014 }
1015 
1016 /**
1017  * xe_bo_shrink() - Try to shrink an xe bo.
1018  * @ctx: The struct ttm_operation_ctx used for shrinking.
1019  * @bo: The TTM buffer object whose pages to shrink.
1020  * @flags: Flags governing the shrink behaviour.
1021  * @scanned: Pointer to a counter of the number of pages
1022  * attempted to shrink.
1023  *
1024  * Try to shrink- or purge a bo, and if it succeeds, unmap dma.
1025  * Note that we need to be able to handle also non xe bos
1026  * (ghost bos), but only if the struct ttm_tt is embedded in
1027  * a struct xe_ttm_tt. When the function attempts to shrink
1028  * the pages of a buffer object, The value pointed to by @scanned
1029  * is updated.
1030  *
1031  * Return: The number of pages shrunken or purged, or negative error
1032  * code on failure.
1033  */
1034 long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
1035 		  const struct xe_bo_shrink_flags flags,
1036 		  unsigned long *scanned)
1037 {
1038 	struct ttm_tt *tt = bo->ttm;
1039 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
1040 	struct ttm_place place = {.mem_type = bo->resource->mem_type};
1041 	struct xe_bo *xe_bo = ttm_to_xe_bo(bo);
1042 	struct xe_device *xe = xe_tt->xe;
1043 	bool needs_rpm;
1044 	long lret = 0L;
1045 
1046 	if (!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE) ||
1047 	    (flags.purge && !xe_tt->purgeable))
1048 		return -EBUSY;
1049 
1050 	if (!ttm_bo_eviction_valuable(bo, &place))
1051 		return -EBUSY;
1052 
1053 	if (!xe_bo_is_xe_bo(bo) || !xe_bo_get_unless_zero(xe_bo))
1054 		return xe_bo_shrink_purge(ctx, bo, scanned);
1055 
1056 	if (xe_tt->purgeable) {
1057 		if (bo->resource->mem_type != XE_PL_SYSTEM)
1058 			lret = xe_bo_move_notify(xe_bo, ctx);
1059 		if (!lret)
1060 			lret = xe_bo_shrink_purge(ctx, bo, scanned);
1061 		goto out_unref;
1062 	}
1063 
1064 	/* System CCS needs gpu copy when moving PL_TT -> PL_SYSTEM */
1065 	needs_rpm = (!IS_DGFX(xe) && bo->resource->mem_type != XE_PL_SYSTEM &&
1066 		     xe_bo_needs_ccs_pages(xe_bo));
1067 	if (needs_rpm && !xe_pm_runtime_get_if_active(xe))
1068 		goto out_unref;
1069 
1070 	*scanned += tt->num_pages;
1071 	lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags)
1072 			     {.purge = false,
1073 			      .writeback = flags.writeback,
1074 			      .allow_move = true});
1075 	if (needs_rpm)
1076 		xe_pm_runtime_put(xe);
1077 
1078 	if (lret > 0)
1079 		xe_ttm_tt_account_subtract(tt);
1080 
1081 out_unref:
1082 	xe_bo_put(xe_bo);
1083 
1084 	return lret;
1085 }
1086 
1087 /**
1088  * xe_bo_evict_pinned() - Evict a pinned VRAM object to system memory
1089  * @bo: The buffer object to move.
1090  *
1091  * On successful completion, the object memory will be moved to system memory.
1092  *
1093  * This is needed to for special handling of pinned VRAM object during
1094  * suspend-resume.
1095  *
1096  * Return: 0 on success. Negative error code on failure.
1097  */
1098 int xe_bo_evict_pinned(struct xe_bo *bo)
1099 {
1100 	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
1101 	struct xe_bo *backup;
1102 	bool unmap = false;
1103 	int ret = 0;
1104 
1105 	xe_bo_lock(bo, false);
1106 
1107 	if (WARN_ON(!bo->ttm.resource)) {
1108 		ret = -EINVAL;
1109 		goto out_unlock_bo;
1110 	}
1111 
1112 	if (WARN_ON(!xe_bo_is_pinned(bo))) {
1113 		ret = -EINVAL;
1114 		goto out_unlock_bo;
1115 	}
1116 
1117 	if (!xe_bo_is_vram(bo))
1118 		goto out_unlock_bo;
1119 
1120 	if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
1121 		goto out_unlock_bo;
1122 
1123 	backup = xe_bo_create_locked(xe, NULL, NULL, bo->size, ttm_bo_type_kernel,
1124 				     XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |
1125 				     XE_BO_FLAG_PINNED);
1126 	if (IS_ERR(backup)) {
1127 		ret = PTR_ERR(backup);
1128 		goto out_unlock_bo;
1129 	}
1130 
1131 	if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) {
1132 		struct xe_migrate *migrate;
1133 		struct dma_fence *fence;
1134 
1135 		if (bo->tile)
1136 			migrate = bo->tile->migrate;
1137 		else
1138 			migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type);
1139 
1140 		ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
1141 		if (ret)
1142 			goto out_backup;
1143 
1144 		ret = dma_resv_reserve_fences(backup->ttm.base.resv, 1);
1145 		if (ret)
1146 			goto out_backup;
1147 
1148 		fence = xe_migrate_copy(migrate, bo, backup, bo->ttm.resource,
1149 					backup->ttm.resource, false);
1150 		if (IS_ERR(fence)) {
1151 			ret = PTR_ERR(fence);
1152 			goto out_backup;
1153 		}
1154 
1155 		dma_resv_add_fence(bo->ttm.base.resv, fence,
1156 				   DMA_RESV_USAGE_KERNEL);
1157 		dma_resv_add_fence(backup->ttm.base.resv, fence,
1158 				   DMA_RESV_USAGE_KERNEL);
1159 		dma_fence_put(fence);
1160 	} else {
1161 		ret = xe_bo_vmap(backup);
1162 		if (ret)
1163 			goto out_backup;
1164 
1165 		if (iosys_map_is_null(&bo->vmap)) {
1166 			ret = xe_bo_vmap(bo);
1167 			if (ret)
1168 				goto out_backup;
1169 			unmap = true;
1170 		}
1171 
1172 		xe_map_memcpy_from(xe, backup->vmap.vaddr, &bo->vmap, 0,
1173 				   bo->size);
1174 	}
1175 
1176 	bo->backup_obj = backup;
1177 
1178 out_backup:
1179 	xe_bo_vunmap(backup);
1180 	xe_bo_unlock(backup);
1181 	if (ret)
1182 		xe_bo_put(backup);
1183 out_unlock_bo:
1184 	if (unmap)
1185 		xe_bo_vunmap(bo);
1186 	xe_bo_unlock(bo);
1187 	return ret;
1188 }
1189 
1190 /**
1191  * xe_bo_restore_pinned() - Restore a pinned VRAM object
1192  * @bo: The buffer object to move.
1193  *
1194  * On successful completion, the object memory will be moved back to VRAM.
1195  *
1196  * This is needed to for special handling of pinned VRAM object during
1197  * suspend-resume.
1198  *
1199  * Return: 0 on success. Negative error code on failure.
1200  */
1201 int xe_bo_restore_pinned(struct xe_bo *bo)
1202 {
1203 	struct ttm_operation_ctx ctx = {
1204 		.interruptible = false,
1205 		.gfp_retry_mayfail = false,
1206 	};
1207 	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
1208 	struct xe_bo *backup = bo->backup_obj;
1209 	bool unmap = false;
1210 	int ret;
1211 
1212 	if (!backup)
1213 		return 0;
1214 
1215 	xe_bo_lock(backup, false);
1216 
1217 	ret = ttm_bo_validate(&backup->ttm, &backup->placement, &ctx);
1218 	if (ret)
1219 		goto out_backup;
1220 
1221 	if (WARN_ON(!dma_resv_trylock(bo->ttm.base.resv))) {
1222 		ret = -EBUSY;
1223 		goto out_backup;
1224 	}
1225 
1226 	if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) {
1227 		struct xe_migrate *migrate;
1228 		struct dma_fence *fence;
1229 
1230 		if (bo->tile)
1231 			migrate = bo->tile->migrate;
1232 		else
1233 			migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type);
1234 
1235 		ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
1236 		if (ret)
1237 			goto out_unlock_bo;
1238 
1239 		ret = dma_resv_reserve_fences(backup->ttm.base.resv, 1);
1240 		if (ret)
1241 			goto out_unlock_bo;
1242 
1243 		fence = xe_migrate_copy(migrate, backup, bo,
1244 					backup->ttm.resource, bo->ttm.resource,
1245 					false);
1246 		if (IS_ERR(fence)) {
1247 			ret = PTR_ERR(fence);
1248 			goto out_unlock_bo;
1249 		}
1250 
1251 		dma_resv_add_fence(bo->ttm.base.resv, fence,
1252 				   DMA_RESV_USAGE_KERNEL);
1253 		dma_resv_add_fence(backup->ttm.base.resv, fence,
1254 				   DMA_RESV_USAGE_KERNEL);
1255 		dma_fence_put(fence);
1256 	} else {
1257 		ret = xe_bo_vmap(backup);
1258 		if (ret)
1259 			goto out_unlock_bo;
1260 
1261 		if (iosys_map_is_null(&bo->vmap)) {
1262 			ret = xe_bo_vmap(bo);
1263 			if (ret)
1264 				goto out_unlock_bo;
1265 			unmap = true;
1266 		}
1267 
1268 		xe_map_memcpy_to(xe, &bo->vmap, 0, backup->vmap.vaddr,
1269 				 bo->size);
1270 	}
1271 
1272 	bo->backup_obj = NULL;
1273 
1274 out_unlock_bo:
1275 	if (unmap)
1276 		xe_bo_vunmap(bo);
1277 	xe_bo_unlock(bo);
1278 out_backup:
1279 	xe_bo_vunmap(backup);
1280 	xe_bo_unlock(backup);
1281 	if (!bo->backup_obj)
1282 		xe_bo_put(backup);
1283 	return ret;
1284 }
1285 
1286 int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
1287 {
1288 	struct ttm_buffer_object *ttm_bo = &bo->ttm;
1289 	struct ttm_tt *tt = ttm_bo->ttm;
1290 
1291 	if (tt) {
1292 		struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
1293 
1294 		if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
1295 			dma_buf_unmap_attachment(ttm_bo->base.import_attach,
1296 						 ttm_bo->sg,
1297 						 DMA_BIDIRECTIONAL);
1298 			ttm_bo->sg = NULL;
1299 			xe_tt->sg = NULL;
1300 		} else if (xe_tt->sg) {
1301 			dma_unmap_sgtable(xe_tt->xe->drm.dev, xe_tt->sg,
1302 					  DMA_BIDIRECTIONAL, 0);
1303 			sg_free_table(xe_tt->sg);
1304 			xe_tt->sg = NULL;
1305 		}
1306 	}
1307 
1308 	return 0;
1309 }
1310 
1311 static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo,
1312 				       unsigned long page_offset)
1313 {
1314 	struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1315 	struct xe_res_cursor cursor;
1316 	struct xe_vram_region *vram;
1317 
1318 	if (ttm_bo->resource->mem_type == XE_PL_STOLEN)
1319 		return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT;
1320 
1321 	vram = res_to_mem_region(ttm_bo->resource);
1322 	xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor);
1323 	return (vram->io_start + cursor.start) >> PAGE_SHIFT;
1324 }
1325 
1326 static void __xe_bo_vunmap(struct xe_bo *bo);
1327 
1328 /*
1329  * TODO: Move this function to TTM so we don't rely on how TTM does its
1330  * locking, thereby abusing TTM internals.
1331  */
1332 static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo)
1333 {
1334 	struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1335 	bool locked;
1336 
1337 	xe_assert(xe, !kref_read(&ttm_bo->kref));
1338 
1339 	/*
1340 	 * We can typically only race with TTM trylocking under the
1341 	 * lru_lock, which will immediately be unlocked again since
1342 	 * the ttm_bo refcount is zero at this point. So trylocking *should*
1343 	 * always succeed here, as long as we hold the lru lock.
1344 	 */
1345 	spin_lock(&ttm_bo->bdev->lru_lock);
1346 	locked = dma_resv_trylock(ttm_bo->base.resv);
1347 	spin_unlock(&ttm_bo->bdev->lru_lock);
1348 	xe_assert(xe, locked);
1349 
1350 	return locked;
1351 }
1352 
1353 static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
1354 {
1355 	struct dma_resv_iter cursor;
1356 	struct dma_fence *fence;
1357 	struct dma_fence *replacement = NULL;
1358 	struct xe_bo *bo;
1359 
1360 	if (!xe_bo_is_xe_bo(ttm_bo))
1361 		return;
1362 
1363 	bo = ttm_to_xe_bo(ttm_bo);
1364 	xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount)));
1365 
1366 	/*
1367 	 * Corner case where TTM fails to allocate memory and this BOs resv
1368 	 * still points the VMs resv
1369 	 */
1370 	if (ttm_bo->base.resv != &ttm_bo->base._resv)
1371 		return;
1372 
1373 	if (!xe_ttm_bo_lock_in_destructor(ttm_bo))
1374 		return;
1375 
1376 	/*
1377 	 * Scrub the preempt fences if any. The unbind fence is already
1378 	 * attached to the resv.
1379 	 * TODO: Don't do this for external bos once we scrub them after
1380 	 * unbind.
1381 	 */
1382 	dma_resv_for_each_fence(&cursor, ttm_bo->base.resv,
1383 				DMA_RESV_USAGE_BOOKKEEP, fence) {
1384 		if (xe_fence_is_xe_preempt(fence) &&
1385 		    !dma_fence_is_signaled(fence)) {
1386 			if (!replacement)
1387 				replacement = dma_fence_get_stub();
1388 
1389 			dma_resv_replace_fences(ttm_bo->base.resv,
1390 						fence->context,
1391 						replacement,
1392 						DMA_RESV_USAGE_BOOKKEEP);
1393 		}
1394 	}
1395 	dma_fence_put(replacement);
1396 
1397 	dma_resv_unlock(ttm_bo->base.resv);
1398 }
1399 
1400 static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
1401 {
1402 	if (!xe_bo_is_xe_bo(ttm_bo))
1403 		return;
1404 
1405 	/*
1406 	 * Object is idle and about to be destroyed. Release the
1407 	 * dma-buf attachment.
1408 	 */
1409 	if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
1410 		struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
1411 						       struct xe_ttm_tt, ttm);
1412 
1413 		dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
1414 					 DMA_BIDIRECTIONAL);
1415 		ttm_bo->sg = NULL;
1416 		xe_tt->sg = NULL;
1417 	}
1418 }
1419 
1420 static void xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *ctx)
1421 {
1422 	struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1423 
1424 	if (ttm_bo->ttm) {
1425 		struct ttm_placement place = {};
1426 		int ret = ttm_bo_validate(ttm_bo, &place, ctx);
1427 
1428 		drm_WARN_ON(&xe->drm, ret);
1429 	}
1430 }
1431 
1432 static void xe_ttm_bo_swap_notify(struct ttm_buffer_object *ttm_bo)
1433 {
1434 	struct ttm_operation_ctx ctx = {
1435 		.interruptible = false,
1436 		.gfp_retry_mayfail = false,
1437 	};
1438 
1439 	if (ttm_bo->ttm) {
1440 		struct xe_ttm_tt *xe_tt =
1441 			container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm);
1442 
1443 		if (xe_tt->purgeable)
1444 			xe_ttm_bo_purge(ttm_bo, &ctx);
1445 	}
1446 }
1447 
1448 static int xe_ttm_access_memory(struct ttm_buffer_object *ttm_bo,
1449 				unsigned long offset, void *buf, int len,
1450 				int write)
1451 {
1452 	struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1453 	struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1454 	struct iosys_map vmap;
1455 	struct xe_res_cursor cursor;
1456 	struct xe_vram_region *vram;
1457 	int bytes_left = len;
1458 
1459 	xe_bo_assert_held(bo);
1460 	xe_device_assert_mem_access(xe);
1461 
1462 	if (!mem_type_is_vram(ttm_bo->resource->mem_type))
1463 		return -EIO;
1464 
1465 	/* FIXME: Use GPU for non-visible VRAM */
1466 	if (!xe_ttm_resource_visible(ttm_bo->resource))
1467 		return -EIO;
1468 
1469 	vram = res_to_mem_region(ttm_bo->resource);
1470 	xe_res_first(ttm_bo->resource, offset & PAGE_MASK,
1471 		     bo->size - (offset & PAGE_MASK), &cursor);
1472 
1473 	do {
1474 		unsigned long page_offset = (offset & ~PAGE_MASK);
1475 		int byte_count = min((int)(PAGE_SIZE - page_offset), bytes_left);
1476 
1477 		iosys_map_set_vaddr_iomem(&vmap, (u8 __iomem *)vram->mapping +
1478 					  cursor.start);
1479 		if (write)
1480 			xe_map_memcpy_to(xe, &vmap, page_offset, buf, byte_count);
1481 		else
1482 			xe_map_memcpy_from(xe, buf, &vmap, page_offset, byte_count);
1483 
1484 		buf += byte_count;
1485 		offset += byte_count;
1486 		bytes_left -= byte_count;
1487 		if (bytes_left)
1488 			xe_res_next(&cursor, PAGE_SIZE);
1489 	} while (bytes_left);
1490 
1491 	return len;
1492 }
1493 
1494 const struct ttm_device_funcs xe_ttm_funcs = {
1495 	.ttm_tt_create = xe_ttm_tt_create,
1496 	.ttm_tt_populate = xe_ttm_tt_populate,
1497 	.ttm_tt_unpopulate = xe_ttm_tt_unpopulate,
1498 	.ttm_tt_destroy = xe_ttm_tt_destroy,
1499 	.evict_flags = xe_evict_flags,
1500 	.move = xe_bo_move,
1501 	.io_mem_reserve = xe_ttm_io_mem_reserve,
1502 	.io_mem_pfn = xe_ttm_io_mem_pfn,
1503 	.access_memory = xe_ttm_access_memory,
1504 	.release_notify = xe_ttm_bo_release_notify,
1505 	.eviction_valuable = ttm_bo_eviction_valuable,
1506 	.delete_mem_notify = xe_ttm_bo_delete_mem_notify,
1507 	.swap_notify = xe_ttm_bo_swap_notify,
1508 };
1509 
1510 static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo)
1511 {
1512 	struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1513 	struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1514 	struct xe_tile *tile;
1515 	u8 id;
1516 
1517 	if (bo->ttm.base.import_attach)
1518 		drm_prime_gem_destroy(&bo->ttm.base, NULL);
1519 	drm_gem_object_release(&bo->ttm.base);
1520 
1521 	xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list));
1522 
1523 	for_each_tile(tile, xe, id)
1524 		if (bo->ggtt_node[id] && bo->ggtt_node[id]->base.size)
1525 			xe_ggtt_remove_bo(tile->mem.ggtt, bo);
1526 
1527 #ifdef CONFIG_PROC_FS
1528 	if (bo->client)
1529 		xe_drm_client_remove_bo(bo);
1530 #endif
1531 
1532 	if (bo->vm && xe_bo_is_user(bo))
1533 		xe_vm_put(bo->vm);
1534 
1535 	mutex_lock(&xe->mem_access.vram_userfault.lock);
1536 	if (!list_empty(&bo->vram_userfault_link))
1537 		list_del(&bo->vram_userfault_link);
1538 	mutex_unlock(&xe->mem_access.vram_userfault.lock);
1539 
1540 	kfree(bo);
1541 }
1542 
1543 static void xe_gem_object_free(struct drm_gem_object *obj)
1544 {
1545 	/* Our BO reference counting scheme works as follows:
1546 	 *
1547 	 * The gem object kref is typically used throughout the driver,
1548 	 * and the gem object holds a ttm_buffer_object refcount, so
1549 	 * that when the last gem object reference is put, which is when
1550 	 * we end up in this function, we put also that ttm_buffer_object
1551 	 * refcount. Anything using gem interfaces is then no longer
1552 	 * allowed to access the object in a way that requires a gem
1553 	 * refcount, including locking the object.
1554 	 *
1555 	 * driver ttm callbacks is allowed to use the ttm_buffer_object
1556 	 * refcount directly if needed.
1557 	 */
1558 	__xe_bo_vunmap(gem_to_xe_bo(obj));
1559 	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
1560 }
1561 
1562 static void xe_gem_object_close(struct drm_gem_object *obj,
1563 				struct drm_file *file_priv)
1564 {
1565 	struct xe_bo *bo = gem_to_xe_bo(obj);
1566 
1567 	if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) {
1568 		xe_assert(xe_bo_device(bo), xe_bo_is_user(bo));
1569 
1570 		xe_bo_lock(bo, false);
1571 		ttm_bo_set_bulk_move(&bo->ttm, NULL);
1572 		xe_bo_unlock(bo);
1573 	}
1574 }
1575 
1576 static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
1577 {
1578 	struct ttm_buffer_object *tbo = vmf->vma->vm_private_data;
1579 	struct drm_device *ddev = tbo->base.dev;
1580 	struct xe_device *xe = to_xe_device(ddev);
1581 	struct xe_bo *bo = ttm_to_xe_bo(tbo);
1582 	bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK;
1583 	vm_fault_t ret;
1584 	int idx;
1585 
1586 	if (needs_rpm)
1587 		xe_pm_runtime_get(xe);
1588 
1589 	ret = ttm_bo_vm_reserve(tbo, vmf);
1590 	if (ret)
1591 		goto out;
1592 
1593 	if (drm_dev_enter(ddev, &idx)) {
1594 		trace_xe_bo_cpu_fault(bo);
1595 
1596 		ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
1597 					       TTM_BO_VM_NUM_PREFAULT);
1598 		drm_dev_exit(idx);
1599 	} else {
1600 		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1601 	}
1602 
1603 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1604 		goto out;
1605 	/*
1606 	 * ttm_bo_vm_reserve() already has dma_resv_lock.
1607 	 */
1608 	if (ret == VM_FAULT_NOPAGE && mem_type_is_vram(tbo->resource->mem_type)) {
1609 		mutex_lock(&xe->mem_access.vram_userfault.lock);
1610 		if (list_empty(&bo->vram_userfault_link))
1611 			list_add(&bo->vram_userfault_link, &xe->mem_access.vram_userfault.list);
1612 		mutex_unlock(&xe->mem_access.vram_userfault.lock);
1613 	}
1614 
1615 	dma_resv_unlock(tbo->base.resv);
1616 out:
1617 	if (needs_rpm)
1618 		xe_pm_runtime_put(xe);
1619 
1620 	return ret;
1621 }
1622 
1623 static int xe_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
1624 			   void *buf, int len, int write)
1625 {
1626 	struct ttm_buffer_object *ttm_bo = vma->vm_private_data;
1627 	struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1628 	struct xe_device *xe = xe_bo_device(bo);
1629 	int ret;
1630 
1631 	xe_pm_runtime_get(xe);
1632 	ret = ttm_bo_vm_access(vma, addr, buf, len, write);
1633 	xe_pm_runtime_put(xe);
1634 
1635 	return ret;
1636 }
1637 
1638 /**
1639  * xe_bo_read() - Read from an xe_bo
1640  * @bo: The buffer object to read from.
1641  * @offset: The byte offset to start reading from.
1642  * @dst: Location to store the read.
1643  * @size: Size in bytes for the read.
1644  *
1645  * Read @size bytes from the @bo, starting from @offset, storing into @dst.
1646  *
1647  * Return: Zero on success, or negative error.
1648  */
1649 int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size)
1650 {
1651 	int ret;
1652 
1653 	ret = ttm_bo_access(&bo->ttm, offset, dst, size, 0);
1654 	if (ret >= 0 && ret != size)
1655 		ret = -EIO;
1656 	else if (ret == size)
1657 		ret = 0;
1658 
1659 	return ret;
1660 }
1661 
1662 static const struct vm_operations_struct xe_gem_vm_ops = {
1663 	.fault = xe_gem_fault,
1664 	.open = ttm_bo_vm_open,
1665 	.close = ttm_bo_vm_close,
1666 	.access = xe_bo_vm_access,
1667 };
1668 
1669 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
1670 	.free = xe_gem_object_free,
1671 	.close = xe_gem_object_close,
1672 	.mmap = drm_gem_ttm_mmap,
1673 	.export = xe_gem_prime_export,
1674 	.vm_ops = &xe_gem_vm_ops,
1675 };
1676 
1677 /**
1678  * xe_bo_alloc - Allocate storage for a struct xe_bo
1679  *
1680  * This function is intended to allocate storage to be used for input
1681  * to __xe_bo_create_locked(), in the case a pointer to the bo to be
1682  * created is needed before the call to __xe_bo_create_locked().
1683  * If __xe_bo_create_locked ends up never to be called, then the
1684  * storage allocated with this function needs to be freed using
1685  * xe_bo_free().
1686  *
1687  * Return: A pointer to an uninitialized struct xe_bo on success,
1688  * ERR_PTR(-ENOMEM) on error.
1689  */
1690 struct xe_bo *xe_bo_alloc(void)
1691 {
1692 	struct xe_bo *bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1693 
1694 	if (!bo)
1695 		return ERR_PTR(-ENOMEM);
1696 
1697 	return bo;
1698 }
1699 
1700 /**
1701  * xe_bo_free - Free storage allocated using xe_bo_alloc()
1702  * @bo: The buffer object storage.
1703  *
1704  * Refer to xe_bo_alloc() documentation for valid use-cases.
1705  */
1706 void xe_bo_free(struct xe_bo *bo)
1707 {
1708 	kfree(bo);
1709 }
1710 
1711 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
1712 				     struct xe_tile *tile, struct dma_resv *resv,
1713 				     struct ttm_lru_bulk_move *bulk, size_t size,
1714 				     u16 cpu_caching, enum ttm_bo_type type,
1715 				     u32 flags)
1716 {
1717 	struct ttm_operation_ctx ctx = {
1718 		.interruptible = true,
1719 		.no_wait_gpu = false,
1720 		.gfp_retry_mayfail = true,
1721 	};
1722 	struct ttm_placement *placement;
1723 	uint32_t alignment;
1724 	size_t aligned_size;
1725 	int err;
1726 
1727 	/* Only kernel objects should set GT */
1728 	xe_assert(xe, !tile || type == ttm_bo_type_kernel);
1729 
1730 	if (XE_WARN_ON(!size)) {
1731 		xe_bo_free(bo);
1732 		return ERR_PTR(-EINVAL);
1733 	}
1734 
1735 	/* XE_BO_FLAG_GGTTx requires XE_BO_FLAG_GGTT also be set */
1736 	if ((flags & XE_BO_FLAG_GGTT_ALL) && !(flags & XE_BO_FLAG_GGTT))
1737 		return ERR_PTR(-EINVAL);
1738 
1739 	if (flags & (XE_BO_FLAG_VRAM_MASK | XE_BO_FLAG_STOLEN) &&
1740 	    !(flags & XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE) &&
1741 	    ((xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) ||
1742 	     (flags & (XE_BO_FLAG_NEEDS_64K | XE_BO_FLAG_NEEDS_2M)))) {
1743 		size_t align = flags & XE_BO_FLAG_NEEDS_2M ? SZ_2M : SZ_64K;
1744 
1745 		aligned_size = ALIGN(size, align);
1746 		if (type != ttm_bo_type_device)
1747 			size = ALIGN(size, align);
1748 		flags |= XE_BO_FLAG_INTERNAL_64K;
1749 		alignment = align >> PAGE_SHIFT;
1750 	} else {
1751 		aligned_size = ALIGN(size, SZ_4K);
1752 		flags &= ~XE_BO_FLAG_INTERNAL_64K;
1753 		alignment = SZ_4K >> PAGE_SHIFT;
1754 	}
1755 
1756 	if (type == ttm_bo_type_device && aligned_size != size)
1757 		return ERR_PTR(-EINVAL);
1758 
1759 	if (!bo) {
1760 		bo = xe_bo_alloc();
1761 		if (IS_ERR(bo))
1762 			return bo;
1763 	}
1764 
1765 	bo->ccs_cleared = false;
1766 	bo->tile = tile;
1767 	bo->size = size;
1768 	bo->flags = flags;
1769 	bo->cpu_caching = cpu_caching;
1770 	bo->ttm.base.funcs = &xe_gem_object_funcs;
1771 	bo->ttm.priority = XE_BO_PRIORITY_NORMAL;
1772 	INIT_LIST_HEAD(&bo->pinned_link);
1773 #ifdef CONFIG_PROC_FS
1774 	INIT_LIST_HEAD(&bo->client_link);
1775 #endif
1776 	INIT_LIST_HEAD(&bo->vram_userfault_link);
1777 
1778 	drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size);
1779 
1780 	if (resv) {
1781 		ctx.allow_res_evict = !(flags & XE_BO_FLAG_NO_RESV_EVICT);
1782 		ctx.resv = resv;
1783 	}
1784 
1785 	if (!(flags & XE_BO_FLAG_FIXED_PLACEMENT)) {
1786 		err = __xe_bo_placement_for_flags(xe, bo, bo->flags);
1787 		if (WARN_ON(err)) {
1788 			xe_ttm_bo_destroy(&bo->ttm);
1789 			return ERR_PTR(err);
1790 		}
1791 	}
1792 
1793 	/* Defer populating type_sg bos */
1794 	placement = (type == ttm_bo_type_sg ||
1795 		     bo->flags & XE_BO_FLAG_DEFER_BACKING) ? &sys_placement :
1796 		&bo->placement;
1797 	err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type,
1798 				   placement, alignment,
1799 				   &ctx, NULL, resv, xe_ttm_bo_destroy);
1800 	if (err)
1801 		return ERR_PTR(err);
1802 
1803 	/*
1804 	 * The VRAM pages underneath are potentially still being accessed by the
1805 	 * GPU, as per async GPU clearing and async evictions. However TTM makes
1806 	 * sure to add any corresponding move/clear fences into the objects
1807 	 * dma-resv using the DMA_RESV_USAGE_KERNEL slot.
1808 	 *
1809 	 * For KMD internal buffers we don't care about GPU clearing, however we
1810 	 * still need to handle async evictions, where the VRAM is still being
1811 	 * accessed by the GPU. Most internal callers are not expecting this,
1812 	 * since they are missing the required synchronisation before accessing
1813 	 * the memory. To keep things simple just sync wait any kernel fences
1814 	 * here, if the buffer is designated KMD internal.
1815 	 *
1816 	 * For normal userspace objects we should already have the required
1817 	 * pipelining or sync waiting elsewhere, since we already have to deal
1818 	 * with things like async GPU clearing.
1819 	 */
1820 	if (type == ttm_bo_type_kernel) {
1821 		long timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
1822 						     DMA_RESV_USAGE_KERNEL,
1823 						     ctx.interruptible,
1824 						     MAX_SCHEDULE_TIMEOUT);
1825 
1826 		if (timeout < 0) {
1827 			if (!resv)
1828 				dma_resv_unlock(bo->ttm.base.resv);
1829 			xe_bo_put(bo);
1830 			return ERR_PTR(timeout);
1831 		}
1832 	}
1833 
1834 	bo->created = true;
1835 	if (bulk)
1836 		ttm_bo_set_bulk_move(&bo->ttm, bulk);
1837 	else
1838 		ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1839 
1840 	return bo;
1841 }
1842 
1843 static int __xe_bo_fixed_placement(struct xe_device *xe,
1844 				   struct xe_bo *bo,
1845 				   u32 flags,
1846 				   u64 start, u64 end, u64 size)
1847 {
1848 	struct ttm_place *place = bo->placements;
1849 
1850 	if (flags & (XE_BO_FLAG_USER | XE_BO_FLAG_SYSTEM))
1851 		return -EINVAL;
1852 
1853 	place->flags = TTM_PL_FLAG_CONTIGUOUS;
1854 	place->fpfn = start >> PAGE_SHIFT;
1855 	place->lpfn = end >> PAGE_SHIFT;
1856 
1857 	switch (flags & (XE_BO_FLAG_STOLEN | XE_BO_FLAG_VRAM_MASK)) {
1858 	case XE_BO_FLAG_VRAM0:
1859 		place->mem_type = XE_PL_VRAM0;
1860 		break;
1861 	case XE_BO_FLAG_VRAM1:
1862 		place->mem_type = XE_PL_VRAM1;
1863 		break;
1864 	case XE_BO_FLAG_STOLEN:
1865 		place->mem_type = XE_PL_STOLEN;
1866 		break;
1867 
1868 	default:
1869 		/* 0 or multiple of the above set */
1870 		return -EINVAL;
1871 	}
1872 
1873 	bo->placement = (struct ttm_placement) {
1874 		.num_placement = 1,
1875 		.placement = place,
1876 	};
1877 
1878 	return 0;
1879 }
1880 
1881 static struct xe_bo *
1882 __xe_bo_create_locked(struct xe_device *xe,
1883 		      struct xe_tile *tile, struct xe_vm *vm,
1884 		      size_t size, u64 start, u64 end,
1885 		      u16 cpu_caching, enum ttm_bo_type type, u32 flags,
1886 		      u64 alignment)
1887 {
1888 	struct xe_bo *bo = NULL;
1889 	int err;
1890 
1891 	if (vm)
1892 		xe_vm_assert_held(vm);
1893 
1894 	if (start || end != ~0ULL) {
1895 		bo = xe_bo_alloc();
1896 		if (IS_ERR(bo))
1897 			return bo;
1898 
1899 		flags |= XE_BO_FLAG_FIXED_PLACEMENT;
1900 		err = __xe_bo_fixed_placement(xe, bo, flags, start, end, size);
1901 		if (err) {
1902 			xe_bo_free(bo);
1903 			return ERR_PTR(err);
1904 		}
1905 	}
1906 
1907 	bo = ___xe_bo_create_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL,
1908 				    vm && !xe_vm_in_fault_mode(vm) &&
1909 				    flags & XE_BO_FLAG_USER ?
1910 				    &vm->lru_bulk_move : NULL, size,
1911 				    cpu_caching, type, flags);
1912 	if (IS_ERR(bo))
1913 		return bo;
1914 
1915 	bo->min_align = alignment;
1916 
1917 	/*
1918 	 * Note that instead of taking a reference no the drm_gpuvm_resv_bo(),
1919 	 * to ensure the shared resv doesn't disappear under the bo, the bo
1920 	 * will keep a reference to the vm, and avoid circular references
1921 	 * by having all the vm's bo refereferences released at vm close
1922 	 * time.
1923 	 */
1924 	if (vm && xe_bo_is_user(bo))
1925 		xe_vm_get(vm);
1926 	bo->vm = vm;
1927 
1928 	if (bo->flags & XE_BO_FLAG_GGTT) {
1929 		struct xe_tile *t;
1930 		u8 id;
1931 
1932 		if (!(bo->flags & XE_BO_FLAG_GGTT_ALL)) {
1933 			if (!tile && flags & XE_BO_FLAG_STOLEN)
1934 				tile = xe_device_get_root_tile(xe);
1935 
1936 			xe_assert(xe, tile);
1937 		}
1938 
1939 		for_each_tile(t, xe, id) {
1940 			if (t != tile && !(bo->flags & XE_BO_FLAG_GGTTx(t)))
1941 				continue;
1942 
1943 			if (flags & XE_BO_FLAG_FIXED_PLACEMENT) {
1944 				err = xe_ggtt_insert_bo_at(t->mem.ggtt, bo,
1945 							   start + bo->size, U64_MAX);
1946 			} else {
1947 				err = xe_ggtt_insert_bo(t->mem.ggtt, bo);
1948 			}
1949 			if (err)
1950 				goto err_unlock_put_bo;
1951 		}
1952 	}
1953 
1954 	trace_xe_bo_create(bo);
1955 	return bo;
1956 
1957 err_unlock_put_bo:
1958 	__xe_bo_unset_bulk_move(bo);
1959 	xe_bo_unlock_vm_held(bo);
1960 	xe_bo_put(bo);
1961 	return ERR_PTR(err);
1962 }
1963 
1964 struct xe_bo *
1965 xe_bo_create_locked_range(struct xe_device *xe,
1966 			  struct xe_tile *tile, struct xe_vm *vm,
1967 			  size_t size, u64 start, u64 end,
1968 			  enum ttm_bo_type type, u32 flags, u64 alignment)
1969 {
1970 	return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type,
1971 				     flags, alignment);
1972 }
1973 
1974 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
1975 				  struct xe_vm *vm, size_t size,
1976 				  enum ttm_bo_type type, u32 flags)
1977 {
1978 	return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type,
1979 				     flags, 0);
1980 }
1981 
1982 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
1983 				struct xe_vm *vm, size_t size,
1984 				u16 cpu_caching,
1985 				u32 flags)
1986 {
1987 	struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL,
1988 						 cpu_caching, ttm_bo_type_device,
1989 						 flags | XE_BO_FLAG_USER, 0);
1990 	if (!IS_ERR(bo))
1991 		xe_bo_unlock_vm_held(bo);
1992 
1993 	return bo;
1994 }
1995 
1996 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
1997 			   struct xe_vm *vm, size_t size,
1998 			   enum ttm_bo_type type, u32 flags)
1999 {
2000 	struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags);
2001 
2002 	if (!IS_ERR(bo))
2003 		xe_bo_unlock_vm_held(bo);
2004 
2005 	return bo;
2006 }
2007 
2008 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
2009 				      struct xe_vm *vm,
2010 				      size_t size, u64 offset,
2011 				      enum ttm_bo_type type, u32 flags)
2012 {
2013 	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, offset,
2014 					       type, flags, 0);
2015 }
2016 
2017 struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
2018 					      struct xe_tile *tile,
2019 					      struct xe_vm *vm,
2020 					      size_t size, u64 offset,
2021 					      enum ttm_bo_type type, u32 flags,
2022 					      u64 alignment)
2023 {
2024 	struct xe_bo *bo;
2025 	int err;
2026 	u64 start = offset == ~0ull ? 0 : offset;
2027 	u64 end = offset == ~0ull ? offset : start + size;
2028 
2029 	if (flags & XE_BO_FLAG_STOLEN &&
2030 	    xe_ttm_stolen_cpu_access_needs_ggtt(xe))
2031 		flags |= XE_BO_FLAG_GGTT;
2032 
2033 	bo = xe_bo_create_locked_range(xe, tile, vm, size, start, end, type,
2034 				       flags | XE_BO_FLAG_NEEDS_CPU_ACCESS | XE_BO_FLAG_PINNED,
2035 				       alignment);
2036 	if (IS_ERR(bo))
2037 		return bo;
2038 
2039 	err = xe_bo_pin(bo);
2040 	if (err)
2041 		goto err_put;
2042 
2043 	err = xe_bo_vmap(bo);
2044 	if (err)
2045 		goto err_unpin;
2046 
2047 	xe_bo_unlock_vm_held(bo);
2048 
2049 	return bo;
2050 
2051 err_unpin:
2052 	xe_bo_unpin(bo);
2053 err_put:
2054 	xe_bo_unlock_vm_held(bo);
2055 	xe_bo_put(bo);
2056 	return ERR_PTR(err);
2057 }
2058 
2059 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
2060 				   struct xe_vm *vm, size_t size,
2061 				   enum ttm_bo_type type, u32 flags)
2062 {
2063 	return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull, type, flags);
2064 }
2065 
2066 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
2067 				     const void *data, size_t size,
2068 				     enum ttm_bo_type type, u32 flags)
2069 {
2070 	struct xe_bo *bo = xe_bo_create_pin_map(xe, tile, NULL,
2071 						ALIGN(size, PAGE_SIZE),
2072 						type, flags);
2073 	if (IS_ERR(bo))
2074 		return bo;
2075 
2076 	xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
2077 
2078 	return bo;
2079 }
2080 
2081 static void __xe_bo_unpin_map_no_vm(void *arg)
2082 {
2083 	xe_bo_unpin_map_no_vm(arg);
2084 }
2085 
2086 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
2087 					   size_t size, u32 flags)
2088 {
2089 	struct xe_bo *bo;
2090 	int ret;
2091 
2092 	KUNIT_STATIC_STUB_REDIRECT(xe_managed_bo_create_pin_map, xe, tile, size, flags);
2093 
2094 	bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel, flags);
2095 	if (IS_ERR(bo))
2096 		return bo;
2097 
2098 	ret = devm_add_action_or_reset(xe->drm.dev, __xe_bo_unpin_map_no_vm, bo);
2099 	if (ret)
2100 		return ERR_PTR(ret);
2101 
2102 	return bo;
2103 }
2104 
2105 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
2106 					     const void *data, size_t size, u32 flags)
2107 {
2108 	struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags);
2109 
2110 	if (IS_ERR(bo))
2111 		return bo;
2112 
2113 	xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
2114 
2115 	return bo;
2116 }
2117 
2118 /**
2119  * xe_managed_bo_reinit_in_vram
2120  * @xe: xe device
2121  * @tile: Tile where the new buffer will be created
2122  * @src: Managed buffer object allocated in system memory
2123  *
2124  * Replace a managed src buffer object allocated in system memory with a new
2125  * one allocated in vram, copying the data between them.
2126  * Buffer object in VRAM is not going to have the same GGTT address, the caller
2127  * is responsible for making sure that any old references to it are updated.
2128  *
2129  * Returns 0 for success, negative error code otherwise.
2130  */
2131 int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src)
2132 {
2133 	struct xe_bo *bo;
2134 	u32 dst_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile) | XE_BO_FLAG_GGTT;
2135 
2136 	dst_flags |= (*src)->flags & (XE_BO_FLAG_GGTT_INVALIDATE |
2137 				      XE_BO_FLAG_PINNED_NORESTORE);
2138 
2139 	xe_assert(xe, IS_DGFX(xe));
2140 	xe_assert(xe, !(*src)->vmap.is_iomem);
2141 
2142 	bo = xe_managed_bo_create_from_data(xe, tile, (*src)->vmap.vaddr,
2143 					    (*src)->size, dst_flags);
2144 	if (IS_ERR(bo))
2145 		return PTR_ERR(bo);
2146 
2147 	devm_release_action(xe->drm.dev, __xe_bo_unpin_map_no_vm, *src);
2148 	*src = bo;
2149 
2150 	return 0;
2151 }
2152 
2153 /*
2154  * XXX: This is in the VM bind data path, likely should calculate this once and
2155  * store, with a recalculation if the BO is moved.
2156  */
2157 uint64_t vram_region_gpu_offset(struct ttm_resource *res)
2158 {
2159 	struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
2160 
2161 	switch (res->mem_type) {
2162 	case XE_PL_STOLEN:
2163 		return xe_ttm_stolen_gpu_offset(xe);
2164 	case XE_PL_TT:
2165 	case XE_PL_SYSTEM:
2166 		return 0;
2167 	default:
2168 		return res_to_mem_region(res)->dpa_base;
2169 	}
2170 	return 0;
2171 }
2172 
2173 /**
2174  * xe_bo_pin_external - pin an external BO
2175  * @bo: buffer object to be pinned
2176  *
2177  * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD)
2178  * BO. Unique call compared to xe_bo_pin as this function has it own set of
2179  * asserts and code to ensure evict / restore on suspend / resume.
2180  *
2181  * Returns 0 for success, negative error code otherwise.
2182  */
2183 int xe_bo_pin_external(struct xe_bo *bo)
2184 {
2185 	struct xe_device *xe = xe_bo_device(bo);
2186 	int err;
2187 
2188 	xe_assert(xe, !bo->vm);
2189 	xe_assert(xe, xe_bo_is_user(bo));
2190 
2191 	if (!xe_bo_is_pinned(bo)) {
2192 		err = xe_bo_validate(bo, NULL, false);
2193 		if (err)
2194 			return err;
2195 
2196 		spin_lock(&xe->pinned.lock);
2197 		list_add_tail(&bo->pinned_link, &xe->pinned.late.external);
2198 		spin_unlock(&xe->pinned.lock);
2199 	}
2200 
2201 	ttm_bo_pin(&bo->ttm);
2202 	if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2203 		xe_ttm_tt_account_subtract(bo->ttm.ttm);
2204 
2205 	/*
2206 	 * FIXME: If we always use the reserve / unreserve functions for locking
2207 	 * we do not need this.
2208 	 */
2209 	ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
2210 
2211 	return 0;
2212 }
2213 
2214 int xe_bo_pin(struct xe_bo *bo)
2215 {
2216 	struct ttm_place *place = &bo->placements[0];
2217 	struct xe_device *xe = xe_bo_device(bo);
2218 	int err;
2219 
2220 	/* We currently don't expect user BO to be pinned */
2221 	xe_assert(xe, !xe_bo_is_user(bo));
2222 
2223 	/* Pinned object must be in GGTT or have pinned flag */
2224 	xe_assert(xe, bo->flags & (XE_BO_FLAG_PINNED |
2225 				   XE_BO_FLAG_GGTT));
2226 
2227 	/*
2228 	 * No reason we can't support pinning imported dma-bufs we just don't
2229 	 * expect to pin an imported dma-buf.
2230 	 */
2231 	xe_assert(xe, !bo->ttm.base.import_attach);
2232 
2233 	/* We only expect at most 1 pin */
2234 	xe_assert(xe, !xe_bo_is_pinned(bo));
2235 
2236 	err = xe_bo_validate(bo, NULL, false);
2237 	if (err)
2238 		return err;
2239 
2240 	if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) {
2241 		spin_lock(&xe->pinned.lock);
2242 		if (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)
2243 			list_add_tail(&bo->pinned_link, &xe->pinned.late.kernel_bo_present);
2244 		else
2245 			list_add_tail(&bo->pinned_link, &xe->pinned.early.kernel_bo_present);
2246 		spin_unlock(&xe->pinned.lock);
2247 	}
2248 
2249 	ttm_bo_pin(&bo->ttm);
2250 	if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2251 		xe_ttm_tt_account_subtract(bo->ttm.ttm);
2252 
2253 	/*
2254 	 * FIXME: If we always use the reserve / unreserve functions for locking
2255 	 * we do not need this.
2256 	 */
2257 	ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
2258 
2259 	return 0;
2260 }
2261 
2262 /**
2263  * xe_bo_unpin_external - unpin an external BO
2264  * @bo: buffer object to be unpinned
2265  *
2266  * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD)
2267  * BO. Unique call compared to xe_bo_unpin as this function has it own set of
2268  * asserts and code to ensure evict / restore on suspend / resume.
2269  *
2270  * Returns 0 for success, negative error code otherwise.
2271  */
2272 void xe_bo_unpin_external(struct xe_bo *bo)
2273 {
2274 	struct xe_device *xe = xe_bo_device(bo);
2275 
2276 	xe_assert(xe, !bo->vm);
2277 	xe_assert(xe, xe_bo_is_pinned(bo));
2278 	xe_assert(xe, xe_bo_is_user(bo));
2279 
2280 	spin_lock(&xe->pinned.lock);
2281 	if (bo->ttm.pin_count == 1 && !list_empty(&bo->pinned_link))
2282 		list_del_init(&bo->pinned_link);
2283 	spin_unlock(&xe->pinned.lock);
2284 
2285 	ttm_bo_unpin(&bo->ttm);
2286 	if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2287 		xe_ttm_tt_account_add(bo->ttm.ttm);
2288 
2289 	/*
2290 	 * FIXME: If we always use the reserve / unreserve functions for locking
2291 	 * we do not need this.
2292 	 */
2293 	ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
2294 }
2295 
2296 void xe_bo_unpin(struct xe_bo *bo)
2297 {
2298 	struct ttm_place *place = &bo->placements[0];
2299 	struct xe_device *xe = xe_bo_device(bo);
2300 
2301 	xe_assert(xe, !bo->ttm.base.import_attach);
2302 	xe_assert(xe, xe_bo_is_pinned(bo));
2303 
2304 	if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) {
2305 		spin_lock(&xe->pinned.lock);
2306 		xe_assert(xe, !list_empty(&bo->pinned_link));
2307 		list_del_init(&bo->pinned_link);
2308 		spin_unlock(&xe->pinned.lock);
2309 	}
2310 	ttm_bo_unpin(&bo->ttm);
2311 	if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm))
2312 		xe_ttm_tt_account_add(bo->ttm.ttm);
2313 }
2314 
2315 /**
2316  * xe_bo_validate() - Make sure the bo is in an allowed placement
2317  * @bo: The bo,
2318  * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or
2319  *      NULL. Used together with @allow_res_evict.
2320  * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's
2321  *                   reservation object.
2322  *
2323  * Make sure the bo is in allowed placement, migrating it if necessary. If
2324  * needed, other bos will be evicted. If bos selected for eviction shares
2325  * the @vm's reservation object, they can be evicted iff @allow_res_evict is
2326  * set to true, otherwise they will be bypassed.
2327  *
2328  * Return: 0 on success, negative error code on failure. May return
2329  * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal.
2330  */
2331 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict)
2332 {
2333 	struct ttm_operation_ctx ctx = {
2334 		.interruptible = true,
2335 		.no_wait_gpu = false,
2336 		.gfp_retry_mayfail = true,
2337 	};
2338 
2339 	if (vm) {
2340 		lockdep_assert_held(&vm->lock);
2341 		xe_vm_assert_held(vm);
2342 
2343 		ctx.allow_res_evict = allow_res_evict;
2344 		ctx.resv = xe_vm_resv(vm);
2345 	}
2346 
2347 	trace_xe_bo_validate(bo);
2348 	return ttm_bo_validate(&bo->ttm, &bo->placement, &ctx);
2349 }
2350 
2351 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo)
2352 {
2353 	if (bo->destroy == &xe_ttm_bo_destroy)
2354 		return true;
2355 
2356 	return false;
2357 }
2358 
2359 /*
2360  * Resolve a BO address. There is no assert to check if the proper lock is held
2361  * so it should only be used in cases where it is not fatal to get the wrong
2362  * address, such as printing debug information, but not in cases where memory is
2363  * written based on this result.
2364  */
2365 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
2366 {
2367 	struct xe_device *xe = xe_bo_device(bo);
2368 	struct xe_res_cursor cur;
2369 	u64 page;
2370 
2371 	xe_assert(xe, page_size <= PAGE_SIZE);
2372 	page = offset >> PAGE_SHIFT;
2373 	offset &= (PAGE_SIZE - 1);
2374 
2375 	if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
2376 		xe_assert(xe, bo->ttm.ttm);
2377 
2378 		xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
2379 				page_size, &cur);
2380 		return xe_res_dma(&cur) + offset;
2381 	} else {
2382 		struct xe_res_cursor cur;
2383 
2384 		xe_res_first(bo->ttm.resource, page << PAGE_SHIFT,
2385 			     page_size, &cur);
2386 		return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource);
2387 	}
2388 }
2389 
2390 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
2391 {
2392 	if (!READ_ONCE(bo->ttm.pin_count))
2393 		xe_bo_assert_held(bo);
2394 	return __xe_bo_addr(bo, offset, page_size);
2395 }
2396 
2397 int xe_bo_vmap(struct xe_bo *bo)
2398 {
2399 	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
2400 	void *virtual;
2401 	bool is_iomem;
2402 	int ret;
2403 
2404 	xe_bo_assert_held(bo);
2405 
2406 	if (drm_WARN_ON(&xe->drm, !(bo->flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) ||
2407 			!force_contiguous(bo->flags)))
2408 		return -EINVAL;
2409 
2410 	if (!iosys_map_is_null(&bo->vmap))
2411 		return 0;
2412 
2413 	/*
2414 	 * We use this more or less deprecated interface for now since
2415 	 * ttm_bo_vmap() doesn't offer the optimization of kmapping
2416 	 * single page bos, which is done here.
2417 	 * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap
2418 	 * to use struct iosys_map.
2419 	 */
2420 	ret = ttm_bo_kmap(&bo->ttm, 0, bo->size >> PAGE_SHIFT, &bo->kmap);
2421 	if (ret)
2422 		return ret;
2423 
2424 	virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
2425 	if (is_iomem)
2426 		iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual);
2427 	else
2428 		iosys_map_set_vaddr(&bo->vmap, virtual);
2429 
2430 	return 0;
2431 }
2432 
2433 static void __xe_bo_vunmap(struct xe_bo *bo)
2434 {
2435 	if (!iosys_map_is_null(&bo->vmap)) {
2436 		iosys_map_clear(&bo->vmap);
2437 		ttm_bo_kunmap(&bo->kmap);
2438 	}
2439 }
2440 
2441 void xe_bo_vunmap(struct xe_bo *bo)
2442 {
2443 	xe_bo_assert_held(bo);
2444 	__xe_bo_vunmap(bo);
2445 }
2446 
2447 static int gem_create_set_pxp_type(struct xe_device *xe, struct xe_bo *bo, u64 value)
2448 {
2449 	if (value == DRM_XE_PXP_TYPE_NONE)
2450 		return 0;
2451 
2452 	/* we only support DRM_XE_PXP_TYPE_HWDRM for now */
2453 	if (XE_IOCTL_DBG(xe, value != DRM_XE_PXP_TYPE_HWDRM))
2454 		return -EINVAL;
2455 
2456 	return xe_pxp_key_assign(xe->pxp, bo);
2457 }
2458 
2459 typedef int (*xe_gem_create_set_property_fn)(struct xe_device *xe,
2460 					     struct xe_bo *bo,
2461 					     u64 value);
2462 
2463 static const xe_gem_create_set_property_fn gem_create_set_property_funcs[] = {
2464 	[DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY] = gem_create_set_pxp_type,
2465 };
2466 
2467 static int gem_create_user_ext_set_property(struct xe_device *xe,
2468 					    struct xe_bo *bo,
2469 					    u64 extension)
2470 {
2471 	u64 __user *address = u64_to_user_ptr(extension);
2472 	struct drm_xe_ext_set_property ext;
2473 	int err;
2474 	u32 idx;
2475 
2476 	err = __copy_from_user(&ext, address, sizeof(ext));
2477 	if (XE_IOCTL_DBG(xe, err))
2478 		return -EFAULT;
2479 
2480 	if (XE_IOCTL_DBG(xe, ext.property >=
2481 			 ARRAY_SIZE(gem_create_set_property_funcs)) ||
2482 	    XE_IOCTL_DBG(xe, ext.pad) ||
2483 	    XE_IOCTL_DBG(xe, ext.property != DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY))
2484 		return -EINVAL;
2485 
2486 	idx = array_index_nospec(ext.property, ARRAY_SIZE(gem_create_set_property_funcs));
2487 	if (!gem_create_set_property_funcs[idx])
2488 		return -EINVAL;
2489 
2490 	return gem_create_set_property_funcs[idx](xe, bo, ext.value);
2491 }
2492 
2493 typedef int (*xe_gem_create_user_extension_fn)(struct xe_device *xe,
2494 					       struct xe_bo *bo,
2495 					       u64 extension);
2496 
2497 static const xe_gem_create_user_extension_fn gem_create_user_extension_funcs[] = {
2498 	[DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY] = gem_create_user_ext_set_property,
2499 };
2500 
2501 #define MAX_USER_EXTENSIONS	16
2502 static int gem_create_user_extensions(struct xe_device *xe, struct xe_bo *bo,
2503 				      u64 extensions, int ext_number)
2504 {
2505 	u64 __user *address = u64_to_user_ptr(extensions);
2506 	struct drm_xe_user_extension ext;
2507 	int err;
2508 	u32 idx;
2509 
2510 	if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS))
2511 		return -E2BIG;
2512 
2513 	err = __copy_from_user(&ext, address, sizeof(ext));
2514 	if (XE_IOCTL_DBG(xe, err))
2515 		return -EFAULT;
2516 
2517 	if (XE_IOCTL_DBG(xe, ext.pad) ||
2518 	    XE_IOCTL_DBG(xe, ext.name >= ARRAY_SIZE(gem_create_user_extension_funcs)))
2519 		return -EINVAL;
2520 
2521 	idx = array_index_nospec(ext.name,
2522 				 ARRAY_SIZE(gem_create_user_extension_funcs));
2523 	err = gem_create_user_extension_funcs[idx](xe, bo, extensions);
2524 	if (XE_IOCTL_DBG(xe, err))
2525 		return err;
2526 
2527 	if (ext.next_extension)
2528 		return gem_create_user_extensions(xe, bo, ext.next_extension,
2529 						  ++ext_number);
2530 
2531 	return 0;
2532 }
2533 
2534 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
2535 			struct drm_file *file)
2536 {
2537 	struct xe_device *xe = to_xe_device(dev);
2538 	struct xe_file *xef = to_xe_file(file);
2539 	struct drm_xe_gem_create *args = data;
2540 	struct xe_vm *vm = NULL;
2541 	ktime_t end = 0;
2542 	struct xe_bo *bo;
2543 	unsigned int bo_flags;
2544 	u32 handle;
2545 	int err;
2546 
2547 	if (XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) ||
2548 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2549 		return -EINVAL;
2550 
2551 	/* at least one valid memory placement must be specified */
2552 	if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) ||
2553 			 !args->placement))
2554 		return -EINVAL;
2555 
2556 	if (XE_IOCTL_DBG(xe, args->flags &
2557 			 ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING |
2558 			   DRM_XE_GEM_CREATE_FLAG_SCANOUT |
2559 			   DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM)))
2560 		return -EINVAL;
2561 
2562 	if (XE_IOCTL_DBG(xe, args->handle))
2563 		return -EINVAL;
2564 
2565 	if (XE_IOCTL_DBG(xe, !args->size))
2566 		return -EINVAL;
2567 
2568 	if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX))
2569 		return -EINVAL;
2570 
2571 	if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK))
2572 		return -EINVAL;
2573 
2574 	bo_flags = 0;
2575 	if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING)
2576 		bo_flags |= XE_BO_FLAG_DEFER_BACKING;
2577 
2578 	if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT)
2579 		bo_flags |= XE_BO_FLAG_SCANOUT;
2580 
2581 	bo_flags |= args->placement << (ffs(XE_BO_FLAG_SYSTEM) - 1);
2582 
2583 	/* CCS formats need physical placement at a 64K alignment in VRAM. */
2584 	if ((bo_flags & XE_BO_FLAG_VRAM_MASK) &&
2585 	    (bo_flags & XE_BO_FLAG_SCANOUT) &&
2586 	    !(xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) &&
2587 	    IS_ALIGNED(args->size, SZ_64K))
2588 		bo_flags |= XE_BO_FLAG_NEEDS_64K;
2589 
2590 	if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) {
2591 		if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_FLAG_VRAM_MASK)))
2592 			return -EINVAL;
2593 
2594 		bo_flags |= XE_BO_FLAG_NEEDS_CPU_ACCESS;
2595 	}
2596 
2597 	if (XE_IOCTL_DBG(xe, !args->cpu_caching ||
2598 			 args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC))
2599 		return -EINVAL;
2600 
2601 	if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_VRAM_MASK &&
2602 			 args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC))
2603 		return -EINVAL;
2604 
2605 	if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_SCANOUT &&
2606 			 args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB))
2607 		return -EINVAL;
2608 
2609 	if (args->vm_id) {
2610 		vm = xe_vm_lookup(xef, args->vm_id);
2611 		if (XE_IOCTL_DBG(xe, !vm))
2612 			return -ENOENT;
2613 	}
2614 
2615 retry:
2616 	if (vm) {
2617 		err = xe_vm_lock(vm, true);
2618 		if (err)
2619 			goto out_vm;
2620 	}
2621 
2622 	bo = xe_bo_create_user(xe, NULL, vm, args->size, args->cpu_caching,
2623 			       bo_flags);
2624 
2625 	if (vm)
2626 		xe_vm_unlock(vm);
2627 
2628 	if (IS_ERR(bo)) {
2629 		err = PTR_ERR(bo);
2630 		if (xe_vm_validate_should_retry(NULL, err, &end))
2631 			goto retry;
2632 		goto out_vm;
2633 	}
2634 
2635 	if (args->extensions) {
2636 		err = gem_create_user_extensions(xe, bo, args->extensions, 0);
2637 		if (err)
2638 			goto out_bulk;
2639 	}
2640 
2641 	err = drm_gem_handle_create(file, &bo->ttm.base, &handle);
2642 	if (err)
2643 		goto out_bulk;
2644 
2645 	args->handle = handle;
2646 	goto out_put;
2647 
2648 out_bulk:
2649 	if (vm && !xe_vm_in_fault_mode(vm)) {
2650 		xe_vm_lock(vm, false);
2651 		__xe_bo_unset_bulk_move(bo);
2652 		xe_vm_unlock(vm);
2653 	}
2654 out_put:
2655 	xe_bo_put(bo);
2656 out_vm:
2657 	if (vm)
2658 		xe_vm_put(vm);
2659 
2660 	return err;
2661 }
2662 
2663 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
2664 			     struct drm_file *file)
2665 {
2666 	struct xe_device *xe = to_xe_device(dev);
2667 	struct drm_xe_gem_mmap_offset *args = data;
2668 	struct drm_gem_object *gem_obj;
2669 
2670 	if (XE_IOCTL_DBG(xe, args->extensions) ||
2671 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2672 		return -EINVAL;
2673 
2674 	if (XE_IOCTL_DBG(xe, args->flags &
2675 			 ~DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER))
2676 		return -EINVAL;
2677 
2678 	if (args->flags & DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER) {
2679 		if (XE_IOCTL_DBG(xe, !IS_DGFX(xe)))
2680 			return -EINVAL;
2681 
2682 		if (XE_IOCTL_DBG(xe, args->handle))
2683 			return -EINVAL;
2684 
2685 		if (XE_IOCTL_DBG(xe, PAGE_SIZE > SZ_4K))
2686 			return -EINVAL;
2687 
2688 		BUILD_BUG_ON(((XE_PCI_BARRIER_MMAP_OFFSET >> XE_PTE_SHIFT) +
2689 			      SZ_4K) >= DRM_FILE_PAGE_OFFSET_START);
2690 		args->offset = XE_PCI_BARRIER_MMAP_OFFSET;
2691 		return 0;
2692 	}
2693 
2694 	gem_obj = drm_gem_object_lookup(file, args->handle);
2695 	if (XE_IOCTL_DBG(xe, !gem_obj))
2696 		return -ENOENT;
2697 
2698 	/* The mmap offset was set up at BO allocation time. */
2699 	args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
2700 
2701 	xe_bo_put(gem_to_xe_bo(gem_obj));
2702 	return 0;
2703 }
2704 
2705 /**
2706  * xe_bo_lock() - Lock the buffer object's dma_resv object
2707  * @bo: The struct xe_bo whose lock is to be taken
2708  * @intr: Whether to perform any wait interruptible
2709  *
2710  * Locks the buffer object's dma_resv object. If the buffer object is
2711  * pointing to a shared dma_resv object, that shared lock is locked.
2712  *
2713  * Return: 0 on success, -EINTR if @intr is true and the wait for a
2714  * contended lock was interrupted. If @intr is set to false, the
2715  * function always returns 0.
2716  */
2717 int xe_bo_lock(struct xe_bo *bo, bool intr)
2718 {
2719 	if (intr)
2720 		return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL);
2721 
2722 	dma_resv_lock(bo->ttm.base.resv, NULL);
2723 
2724 	return 0;
2725 }
2726 
2727 /**
2728  * xe_bo_unlock() - Unlock the buffer object's dma_resv object
2729  * @bo: The struct xe_bo whose lock is to be released.
2730  *
2731  * Unlock a buffer object lock that was locked by xe_bo_lock().
2732  */
2733 void xe_bo_unlock(struct xe_bo *bo)
2734 {
2735 	dma_resv_unlock(bo->ttm.base.resv);
2736 }
2737 
2738 /**
2739  * xe_bo_can_migrate - Whether a buffer object likely can be migrated
2740  * @bo: The buffer object to migrate
2741  * @mem_type: The TTM memory type intended to migrate to
2742  *
2743  * Check whether the buffer object supports migration to the
2744  * given memory type. Note that pinning may affect the ability to migrate as
2745  * returned by this function.
2746  *
2747  * This function is primarily intended as a helper for checking the
2748  * possibility to migrate buffer objects and can be called without
2749  * the object lock held.
2750  *
2751  * Return: true if migration is possible, false otherwise.
2752  */
2753 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type)
2754 {
2755 	unsigned int cur_place;
2756 
2757 	if (bo->ttm.type == ttm_bo_type_kernel)
2758 		return true;
2759 
2760 	if (bo->ttm.type == ttm_bo_type_sg)
2761 		return false;
2762 
2763 	for (cur_place = 0; cur_place < bo->placement.num_placement;
2764 	     cur_place++) {
2765 		if (bo->placements[cur_place].mem_type == mem_type)
2766 			return true;
2767 	}
2768 
2769 	return false;
2770 }
2771 
2772 static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
2773 {
2774 	memset(place, 0, sizeof(*place));
2775 	place->mem_type = mem_type;
2776 }
2777 
2778 /**
2779  * xe_bo_migrate - Migrate an object to the desired region id
2780  * @bo: The buffer object to migrate.
2781  * @mem_type: The TTM region type to migrate to.
2782  *
2783  * Attempt to migrate the buffer object to the desired memory region. The
2784  * buffer object may not be pinned, and must be locked.
2785  * On successful completion, the object memory type will be updated,
2786  * but an async migration task may not have completed yet, and to
2787  * accomplish that, the object's kernel fences must be signaled with
2788  * the object lock held.
2789  *
2790  * Return: 0 on success. Negative error code on failure. In particular may
2791  * return -EINTR or -ERESTARTSYS if signal pending.
2792  */
2793 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type)
2794 {
2795 	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
2796 	struct ttm_operation_ctx ctx = {
2797 		.interruptible = true,
2798 		.no_wait_gpu = false,
2799 		.gfp_retry_mayfail = true,
2800 	};
2801 	struct ttm_placement placement;
2802 	struct ttm_place requested;
2803 
2804 	xe_bo_assert_held(bo);
2805 
2806 	if (bo->ttm.resource->mem_type == mem_type)
2807 		return 0;
2808 
2809 	if (xe_bo_is_pinned(bo))
2810 		return -EBUSY;
2811 
2812 	if (!xe_bo_can_migrate(bo, mem_type))
2813 		return -EINVAL;
2814 
2815 	xe_place_from_ttm_type(mem_type, &requested);
2816 	placement.num_placement = 1;
2817 	placement.placement = &requested;
2818 
2819 	/*
2820 	 * Stolen needs to be handled like below VRAM handling if we ever need
2821 	 * to support it.
2822 	 */
2823 	drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN);
2824 
2825 	if (mem_type_is_vram(mem_type)) {
2826 		u32 c = 0;
2827 
2828 		add_vram(xe, bo, &requested, bo->flags, mem_type, &c);
2829 	}
2830 
2831 	return ttm_bo_validate(&bo->ttm, &placement, &ctx);
2832 }
2833 
2834 /**
2835  * xe_bo_evict - Evict an object to evict placement
2836  * @bo: The buffer object to migrate.
2837  * @force_alloc: Set force_alloc in ttm_operation_ctx
2838  *
2839  * On successful completion, the object memory will be moved to evict
2840  * placement. This function blocks until the object has been fully moved.
2841  *
2842  * Return: 0 on success. Negative error code on failure.
2843  */
2844 int xe_bo_evict(struct xe_bo *bo, bool force_alloc)
2845 {
2846 	struct ttm_operation_ctx ctx = {
2847 		.interruptible = false,
2848 		.no_wait_gpu = false,
2849 		.force_alloc = force_alloc,
2850 		.gfp_retry_mayfail = true,
2851 	};
2852 	struct ttm_placement placement;
2853 	int ret;
2854 
2855 	xe_evict_flags(&bo->ttm, &placement);
2856 	ret = ttm_bo_validate(&bo->ttm, &placement, &ctx);
2857 	if (ret)
2858 		return ret;
2859 
2860 	dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
2861 			      false, MAX_SCHEDULE_TIMEOUT);
2862 
2863 	return 0;
2864 }
2865 
2866 /**
2867  * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when
2868  * placed in system memory.
2869  * @bo: The xe_bo
2870  *
2871  * Return: true if extra pages need to be allocated, false otherwise.
2872  */
2873 bool xe_bo_needs_ccs_pages(struct xe_bo *bo)
2874 {
2875 	struct xe_device *xe = xe_bo_device(bo);
2876 
2877 	if (GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe))
2878 		return false;
2879 
2880 	if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device)
2881 		return false;
2882 
2883 	/* On discrete GPUs, if the GPU can access this buffer from
2884 	 * system memory (i.e., it allows XE_PL_TT placement), FlatCCS
2885 	 * can't be used since there's no CCS storage associated with
2886 	 * non-VRAM addresses.
2887 	 */
2888 	if (IS_DGFX(xe) && (bo->flags & XE_BO_FLAG_SYSTEM))
2889 		return false;
2890 
2891 	return true;
2892 }
2893 
2894 /**
2895  * __xe_bo_release_dummy() - Dummy kref release function
2896  * @kref: The embedded struct kref.
2897  *
2898  * Dummy release function for xe_bo_put_deferred(). Keep off.
2899  */
2900 void __xe_bo_release_dummy(struct kref *kref)
2901 {
2902 }
2903 
2904 /**
2905  * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred().
2906  * @deferred: The lockless list used for the call to xe_bo_put_deferred().
2907  *
2908  * Puts all bos whose put was deferred by xe_bo_put_deferred().
2909  * The @deferred list can be either an onstack local list or a global
2910  * shared list used by a workqueue.
2911  */
2912 void xe_bo_put_commit(struct llist_head *deferred)
2913 {
2914 	struct llist_node *freed;
2915 	struct xe_bo *bo, *next;
2916 
2917 	if (!deferred)
2918 		return;
2919 
2920 	freed = llist_del_all(deferred);
2921 	if (!freed)
2922 		return;
2923 
2924 	llist_for_each_entry_safe(bo, next, freed, freed)
2925 		drm_gem_object_free(&bo->ttm.base.refcount);
2926 }
2927 
2928 static void xe_bo_dev_work_func(struct work_struct *work)
2929 {
2930 	struct xe_bo_dev *bo_dev = container_of(work, typeof(*bo_dev), async_free);
2931 
2932 	xe_bo_put_commit(&bo_dev->async_list);
2933 }
2934 
2935 /**
2936  * xe_bo_dev_init() - Initialize BO dev to manage async BO freeing
2937  * @bo_dev: The BO dev structure
2938  */
2939 void xe_bo_dev_init(struct xe_bo_dev *bo_dev)
2940 {
2941 	INIT_WORK(&bo_dev->async_free, xe_bo_dev_work_func);
2942 }
2943 
2944 /**
2945  * xe_bo_dev_fini() - Finalize BO dev managing async BO freeing
2946  * @bo_dev: The BO dev structure
2947  */
2948 void xe_bo_dev_fini(struct xe_bo_dev *bo_dev)
2949 {
2950 	flush_work(&bo_dev->async_free);
2951 }
2952 
2953 void xe_bo_put(struct xe_bo *bo)
2954 {
2955 	struct xe_tile *tile;
2956 	u8 id;
2957 
2958 	might_sleep();
2959 	if (bo) {
2960 #ifdef CONFIG_PROC_FS
2961 		if (bo->client)
2962 			might_lock(&bo->client->bos_lock);
2963 #endif
2964 		for_each_tile(tile, xe_bo_device(bo), id)
2965 			if (bo->ggtt_node[id] && bo->ggtt_node[id]->ggtt)
2966 				might_lock(&bo->ggtt_node[id]->ggtt->lock);
2967 		drm_gem_object_put(&bo->ttm.base);
2968 	}
2969 }
2970 
2971 /**
2972  * xe_bo_dumb_create - Create a dumb bo as backing for a fb
2973  * @file_priv: ...
2974  * @dev: ...
2975  * @args: ...
2976  *
2977  * See dumb_create() hook in include/drm/drm_drv.h
2978  *
2979  * Return: ...
2980  */
2981 int xe_bo_dumb_create(struct drm_file *file_priv,
2982 		      struct drm_device *dev,
2983 		      struct drm_mode_create_dumb *args)
2984 {
2985 	struct xe_device *xe = to_xe_device(dev);
2986 	struct xe_bo *bo;
2987 	uint32_t handle;
2988 	int cpp = DIV_ROUND_UP(args->bpp, 8);
2989 	int err;
2990 	u32 page_size = max_t(u32, PAGE_SIZE,
2991 		xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K);
2992 
2993 	args->pitch = ALIGN(args->width * cpp, 64);
2994 	args->size = ALIGN(mul_u32_u32(args->pitch, args->height),
2995 			   page_size);
2996 
2997 	bo = xe_bo_create_user(xe, NULL, NULL, args->size,
2998 			       DRM_XE_GEM_CPU_CACHING_WC,
2999 			       XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
3000 			       XE_BO_FLAG_SCANOUT |
3001 			       XE_BO_FLAG_NEEDS_CPU_ACCESS);
3002 	if (IS_ERR(bo))
3003 		return PTR_ERR(bo);
3004 
3005 	err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle);
3006 	/* drop reference from allocate - handle holds it now */
3007 	drm_gem_object_put(&bo->ttm.base);
3008 	if (!err)
3009 		args->handle = handle;
3010 	return err;
3011 }
3012 
3013 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo)
3014 {
3015 	struct ttm_buffer_object *tbo = &bo->ttm;
3016 	struct ttm_device *bdev = tbo->bdev;
3017 
3018 	drm_vma_node_unmap(&tbo->base.vma_node, bdev->dev_mapping);
3019 
3020 	list_del_init(&bo->vram_userfault_link);
3021 }
3022 
3023 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
3024 #include "tests/xe_bo.c"
3025 #endif
3026