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