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