xref: /linux/drivers/gpu/drm/xe/xe_ggtt.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include "xe_ggtt.h"
7 
8 #include <kunit/visibility.h>
9 #include <linux/fault-inject.h>
10 #include <linux/io-64-nonatomic-lo-hi.h>
11 #include <linux/sizes.h>
12 
13 #include <drm/drm_drv.h>
14 #include <drm/drm_managed.h>
15 #include <drm/intel/i915_drm.h>
16 #include <generated/xe_wa_oob.h>
17 
18 #include "regs/xe_gt_regs.h"
19 #include "regs/xe_gtt_defs.h"
20 #include "regs/xe_regs.h"
21 #include "xe_assert.h"
22 #include "xe_bo.h"
23 #include "xe_device.h"
24 #include "xe_gt.h"
25 #include "xe_gt_printk.h"
26 #include "xe_map.h"
27 #include "xe_mmio.h"
28 #include "xe_pm.h"
29 #include "xe_res_cursor.h"
30 #include "xe_sriov.h"
31 #include "xe_tile_printk.h"
32 #include "xe_tile_sriov_vf.h"
33 #include "xe_tlb_inval.h"
34 #include "xe_wa.h"
35 #include "xe_wopcm.h"
36 
37 /**
38  * DOC: Global Graphics Translation Table (GGTT)
39  *
40  * Xe GGTT implements the support for a Global Virtual Address space that is used
41  * for resources that are accessible to privileged (i.e. kernel-mode) processes,
42  * and not tied to a specific user-level process. For example, the Graphics
43  * micro-Controller (GuC) and Display Engine (if present) utilize this Global
44  * address space.
45  *
46  * The Global GTT (GGTT) translates from the Global virtual address to a physical
47  * address that can be accessed by HW. The GGTT is a flat, single-level table.
48  *
49  * Xe implements a simplified version of the GGTT specifically managing only a
50  * certain range of it that goes from the Write Once Protected Content Memory (WOPCM)
51  * Layout to a predefined GUC_GGTT_TOP. This approach avoids complications related to
52  * the GuC (Graphics Microcontroller) hardware limitations. The GuC address space
53  * is limited on both ends of the GGTT, because the GuC shim HW redirects
54  * accesses to those addresses to other HW areas instead of going through the
55  * GGTT. On the bottom end, the GuC can't access offsets below the WOPCM size,
56  * while on the top side the limit is fixed at GUC_GGTT_TOP. To keep things
57  * simple, instead of checking each object to see if they are accessed by GuC or
58  * not, we just exclude those areas from the allocator. Additionally, to simplify
59  * the driver load, we use the maximum WOPCM size in this logic instead of the
60  * programmed one, so we don't need to wait until the actual size to be
61  * programmed is determined (which requires FW fetch) before initializing the
62  * GGTT. These simplifications might waste space in the GGTT (about 20-25 MBs
63  * depending on the platform) but we can live with this. Another benefit of this
64  * is the GuC bootrom can't access anything below the WOPCM max size so anything
65  * the bootrom needs to access (e.g. a RSA key) needs to be placed in the GGTT
66  * above the WOPCM max size. Starting the GGTT allocations above the WOPCM max
67  * give us the correct placement for free.
68  */
69 
70 static u64 xelp_ggtt_pte_flags(struct xe_bo *bo, u16 pat_index)
71 {
72 	u64 pte = XE_PAGE_PRESENT;
73 
74 	if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
75 		pte |= XE_GGTT_PTE_DM;
76 
77 	return pte;
78 }
79 
80 static u64 xelpg_ggtt_pte_flags(struct xe_bo *bo, u16 pat_index)
81 {
82 	struct xe_device *xe = xe_bo_device(bo);
83 	u64 pte;
84 
85 	pte = xelp_ggtt_pte_flags(bo, pat_index);
86 
87 	xe_assert(xe, pat_index <= 3);
88 
89 	if (pat_index & BIT(0))
90 		pte |= XELPG_GGTT_PTE_PAT0;
91 
92 	if (pat_index & BIT(1))
93 		pte |= XELPG_GGTT_PTE_PAT1;
94 
95 	return pte;
96 }
97 
98 static unsigned int probe_gsm_size(struct pci_dev *pdev)
99 {
100 	u16 gmch_ctl, ggms;
101 
102 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &gmch_ctl);
103 	ggms = (gmch_ctl >> BDW_GMCH_GGMS_SHIFT) & BDW_GMCH_GGMS_MASK;
104 	return ggms ? SZ_1M << ggms : 0;
105 }
106 
107 static void ggtt_update_access_counter(struct xe_ggtt *ggtt)
108 {
109 	struct xe_tile *tile = ggtt->tile;
110 	struct xe_gt *affected_gt;
111 	u32 max_gtt_writes;
112 
113 	if (tile->primary_gt && XE_GT_WA(tile->primary_gt, 22019338487)) {
114 		affected_gt = tile->primary_gt;
115 		max_gtt_writes = 1100;
116 
117 		/* Only expected to apply to primary GT on dgpu platforms */
118 		xe_tile_assert(tile, IS_DGFX(tile_to_xe(tile)));
119 	} else {
120 		affected_gt = tile->media_gt;
121 		max_gtt_writes = 63;
122 
123 		/* Only expected to apply to media GT on igpu platforms */
124 		xe_tile_assert(tile, !IS_DGFX(tile_to_xe(tile)));
125 	}
126 
127 	/*
128 	 * Wa_22019338487: GMD_ID is a RO register, a dummy write forces gunit
129 	 * to wait for completion of prior GTT writes before letting this through.
130 	 * This needs to be done for all GGTT writes originating from the CPU.
131 	 */
132 	lockdep_assert_held(&ggtt->lock);
133 
134 	if ((++ggtt->access_count % max_gtt_writes) == 0) {
135 		xe_mmio_write32(&affected_gt->mmio, GMD_ID, 0x0);
136 		ggtt->access_count = 0;
137 	}
138 }
139 
140 static void xe_ggtt_set_pte(struct xe_ggtt *ggtt, u64 addr, u64 pte)
141 {
142 	xe_tile_assert(ggtt->tile, !(addr & XE_PTE_MASK));
143 	xe_tile_assert(ggtt->tile, addr < ggtt->size);
144 
145 	writeq(pte, &ggtt->gsm[addr >> XE_PTE_SHIFT]);
146 }
147 
148 static void xe_ggtt_set_pte_and_flush(struct xe_ggtt *ggtt, u64 addr, u64 pte)
149 {
150 	xe_ggtt_set_pte(ggtt, addr, pte);
151 	ggtt_update_access_counter(ggtt);
152 }
153 
154 static void xe_ggtt_clear(struct xe_ggtt *ggtt, u64 start, u64 size)
155 {
156 	u16 pat_index = tile_to_xe(ggtt->tile)->pat.idx[XE_CACHE_WB];
157 	u64 end = start + size - 1;
158 	u64 scratch_pte;
159 
160 	xe_tile_assert(ggtt->tile, start < end);
161 
162 	if (ggtt->scratch)
163 		scratch_pte = xe_bo_addr(ggtt->scratch, 0, XE_PAGE_SIZE) |
164 			      ggtt->pt_ops->pte_encode_flags(ggtt->scratch,
165 							     pat_index);
166 	else
167 		scratch_pte = 0;
168 
169 	while (start < end) {
170 		ggtt->pt_ops->ggtt_set_pte(ggtt, start, scratch_pte);
171 		start += XE_PAGE_SIZE;
172 	}
173 }
174 
175 static void primelockdep(struct xe_ggtt *ggtt)
176 {
177 	if (!IS_ENABLED(CONFIG_LOCKDEP))
178 		return;
179 
180 	fs_reclaim_acquire(GFP_KERNEL);
181 	might_lock(&ggtt->lock);
182 	fs_reclaim_release(GFP_KERNEL);
183 }
184 
185 /**
186  * xe_ggtt_alloc - Allocate a GGTT for a given &xe_tile
187  * @tile: &xe_tile
188  *
189  * Allocates a &xe_ggtt for a given tile.
190  *
191  * Return: &xe_ggtt on success, or NULL when out of memory.
192  */
193 struct xe_ggtt *xe_ggtt_alloc(struct xe_tile *tile)
194 {
195 	struct xe_device *xe = tile_to_xe(tile);
196 	struct xe_ggtt *ggtt;
197 
198 	ggtt = drmm_kzalloc(&xe->drm, sizeof(*ggtt), GFP_KERNEL);
199 	if (!ggtt)
200 		return NULL;
201 
202 	if (drmm_mutex_init(&xe->drm, &ggtt->lock))
203 		return NULL;
204 
205 	primelockdep(ggtt);
206 	ggtt->tile = tile;
207 
208 	return ggtt;
209 }
210 
211 static void ggtt_fini_early(struct drm_device *drm, void *arg)
212 {
213 	struct xe_ggtt *ggtt = arg;
214 
215 	destroy_workqueue(ggtt->wq);
216 	drm_mm_takedown(&ggtt->mm);
217 }
218 
219 static void ggtt_fini(void *arg)
220 {
221 	struct xe_ggtt *ggtt = arg;
222 
223 	ggtt->scratch = NULL;
224 }
225 
226 #ifdef CONFIG_LOCKDEP
227 void xe_ggtt_might_lock(struct xe_ggtt *ggtt)
228 {
229 	might_lock(&ggtt->lock);
230 }
231 #endif
232 
233 static const struct xe_ggtt_pt_ops xelp_pt_ops = {
234 	.pte_encode_flags = xelp_ggtt_pte_flags,
235 	.ggtt_set_pte = xe_ggtt_set_pte,
236 };
237 
238 static const struct xe_ggtt_pt_ops xelpg_pt_ops = {
239 	.pte_encode_flags = xelpg_ggtt_pte_flags,
240 	.ggtt_set_pte = xe_ggtt_set_pte,
241 };
242 
243 static const struct xe_ggtt_pt_ops xelpg_pt_wa_ops = {
244 	.pte_encode_flags = xelpg_ggtt_pte_flags,
245 	.ggtt_set_pte = xe_ggtt_set_pte_and_flush,
246 };
247 
248 static void __xe_ggtt_init_early(struct xe_ggtt *ggtt, u32 reserved)
249 {
250 	drm_mm_init(&ggtt->mm, reserved,
251 		    ggtt->size - reserved);
252 }
253 
254 int xe_ggtt_init_kunit(struct xe_ggtt *ggtt, u32 reserved, u32 size)
255 {
256 	ggtt->size = size;
257 	__xe_ggtt_init_early(ggtt, reserved);
258 	return 0;
259 }
260 EXPORT_SYMBOL_IF_KUNIT(xe_ggtt_init_kunit);
261 
262 static void dev_fini_ggtt(void *arg)
263 {
264 	struct xe_ggtt *ggtt = arg;
265 
266 	drain_workqueue(ggtt->wq);
267 }
268 
269 /**
270  * xe_ggtt_init_early - Early GGTT initialization
271  * @ggtt: the &xe_ggtt to be initialized
272  *
273  * It allows to create new mappings usable by the GuC.
274  * Mappings are not usable by the HW engines, as it doesn't have scratch nor
275  * initial clear done to it yet. That will happen in the regular, non-early
276  * GGTT initialization.
277  *
278  * Return: 0 on success or a negative error code on failure.
279  */
280 int xe_ggtt_init_early(struct xe_ggtt *ggtt)
281 {
282 	struct xe_device *xe = tile_to_xe(ggtt->tile);
283 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
284 	unsigned int gsm_size;
285 	int err;
286 
287 	if (IS_SRIOV_VF(xe) || GRAPHICS_VERx100(xe) >= 1250)
288 		gsm_size = SZ_8M; /* GGTT is expected to be 4GiB */
289 	else
290 		gsm_size = probe_gsm_size(pdev);
291 
292 	if (gsm_size == 0) {
293 		xe_tile_err(ggtt->tile, "Hardware reported no preallocated GSM\n");
294 		return -ENOMEM;
295 	}
296 
297 	ggtt->gsm = ggtt->tile->mmio.regs + SZ_8M;
298 	ggtt->size = (gsm_size / 8) * (u64) XE_PAGE_SIZE;
299 
300 	if (IS_DGFX(xe) && xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)
301 		ggtt->flags |= XE_GGTT_FLAGS_64K;
302 
303 	if (ggtt->size > GUC_GGTT_TOP)
304 		ggtt->size = GUC_GGTT_TOP;
305 
306 	if (GRAPHICS_VERx100(xe) >= 1270)
307 		ggtt->pt_ops =
308 			(ggtt->tile->media_gt && XE_GT_WA(ggtt->tile->media_gt, 22019338487)) ||
309 			(ggtt->tile->primary_gt && XE_GT_WA(ggtt->tile->primary_gt, 22019338487)) ?
310 			&xelpg_pt_wa_ops : &xelpg_pt_ops;
311 	else
312 		ggtt->pt_ops = &xelp_pt_ops;
313 
314 	ggtt->wq = alloc_workqueue("xe-ggtt-wq", 0, WQ_MEM_RECLAIM);
315 	__xe_ggtt_init_early(ggtt, xe_wopcm_size(xe));
316 
317 	err = drmm_add_action_or_reset(&xe->drm, ggtt_fini_early, ggtt);
318 	if (err)
319 		return err;
320 
321 	err = devm_add_action_or_reset(xe->drm.dev, dev_fini_ggtt, ggtt);
322 	if (err)
323 		return err;
324 
325 	if (IS_SRIOV_VF(xe)) {
326 		err = xe_tile_sriov_vf_prepare_ggtt(ggtt->tile);
327 		if (err)
328 			return err;
329 	}
330 
331 	return 0;
332 }
333 ALLOW_ERROR_INJECTION(xe_ggtt_init_early, ERRNO); /* See xe_pci_probe() */
334 
335 static void xe_ggtt_invalidate(struct xe_ggtt *ggtt);
336 
337 static void xe_ggtt_initial_clear(struct xe_ggtt *ggtt)
338 {
339 	struct drm_mm_node *hole;
340 	u64 start, end;
341 
342 	/* Display may have allocated inside ggtt, so be careful with clearing here */
343 	mutex_lock(&ggtt->lock);
344 	drm_mm_for_each_hole(hole, &ggtt->mm, start, end)
345 		xe_ggtt_clear(ggtt, start, end - start);
346 
347 	xe_ggtt_invalidate(ggtt);
348 	mutex_unlock(&ggtt->lock);
349 }
350 
351 static void ggtt_node_remove(struct xe_ggtt_node *node)
352 {
353 	struct xe_ggtt *ggtt = node->ggtt;
354 	struct xe_device *xe = tile_to_xe(ggtt->tile);
355 	bool bound;
356 	int idx;
357 
358 	bound = drm_dev_enter(&xe->drm, &idx);
359 
360 	mutex_lock(&ggtt->lock);
361 	if (bound)
362 		xe_ggtt_clear(ggtt, node->base.start, node->base.size);
363 	drm_mm_remove_node(&node->base);
364 	node->base.size = 0;
365 	mutex_unlock(&ggtt->lock);
366 
367 	if (!bound)
368 		goto free_node;
369 
370 	if (node->invalidate_on_remove)
371 		xe_ggtt_invalidate(ggtt);
372 
373 	drm_dev_exit(idx);
374 
375 free_node:
376 	xe_ggtt_node_fini(node);
377 }
378 
379 static void ggtt_node_remove_work_func(struct work_struct *work)
380 {
381 	struct xe_ggtt_node *node = container_of(work, typeof(*node),
382 						 delayed_removal_work);
383 	struct xe_device *xe = tile_to_xe(node->ggtt->tile);
384 
385 	xe_pm_runtime_get(xe);
386 	ggtt_node_remove(node);
387 	xe_pm_runtime_put(xe);
388 }
389 
390 /**
391  * xe_ggtt_node_remove - Remove a &xe_ggtt_node from the GGTT
392  * @node: the &xe_ggtt_node to be removed
393  * @invalidate: if node needs invalidation upon removal
394  */
395 void xe_ggtt_node_remove(struct xe_ggtt_node *node, bool invalidate)
396 {
397 	struct xe_ggtt *ggtt;
398 	struct xe_device *xe;
399 
400 	if (!node || !node->ggtt)
401 		return;
402 
403 	ggtt = node->ggtt;
404 	xe = tile_to_xe(ggtt->tile);
405 
406 	node->invalidate_on_remove = invalidate;
407 
408 	if (xe_pm_runtime_get_if_active(xe)) {
409 		ggtt_node_remove(node);
410 		xe_pm_runtime_put(xe);
411 	} else {
412 		queue_work(ggtt->wq, &node->delayed_removal_work);
413 	}
414 }
415 
416 /**
417  * xe_ggtt_init - Regular non-early GGTT initialization
418  * @ggtt: the &xe_ggtt to be initialized
419  *
420  * Return: 0 on success or a negative error code on failure.
421  */
422 int xe_ggtt_init(struct xe_ggtt *ggtt)
423 {
424 	struct xe_device *xe = tile_to_xe(ggtt->tile);
425 	unsigned int flags;
426 	int err;
427 
428 	/*
429 	 * So we don't need to worry about 64K GGTT layout when dealing with
430 	 * scratch entries, rather keep the scratch page in system memory on
431 	 * platforms where 64K pages are needed for VRAM.
432 	 */
433 	flags = 0;
434 	if (ggtt->flags & XE_GGTT_FLAGS_64K)
435 		flags |= XE_BO_FLAG_SYSTEM;
436 	else
437 		flags |= XE_BO_FLAG_VRAM_IF_DGFX(ggtt->tile);
438 
439 	ggtt->scratch = xe_managed_bo_create_pin_map(xe, ggtt->tile, XE_PAGE_SIZE, flags);
440 	if (IS_ERR(ggtt->scratch)) {
441 		err = PTR_ERR(ggtt->scratch);
442 		goto err;
443 	}
444 
445 	xe_map_memset(xe, &ggtt->scratch->vmap, 0, 0, xe_bo_size(ggtt->scratch));
446 
447 	xe_ggtt_initial_clear(ggtt);
448 
449 	return devm_add_action_or_reset(xe->drm.dev, ggtt_fini, ggtt);
450 err:
451 	ggtt->scratch = NULL;
452 	return err;
453 }
454 
455 static void ggtt_invalidate_gt_tlb(struct xe_gt *gt)
456 {
457 	int err;
458 
459 	if (!gt)
460 		return;
461 
462 	err = xe_tlb_inval_ggtt(&gt->tlb_inval);
463 	xe_gt_WARN(gt, err, "Failed to invalidate GGTT (%pe)", ERR_PTR(err));
464 }
465 
466 static void xe_ggtt_invalidate(struct xe_ggtt *ggtt)
467 {
468 	struct xe_device *xe = tile_to_xe(ggtt->tile);
469 
470 	/*
471 	 * XXX: Barrier for GGTT pages. Unsure exactly why this required but
472 	 * without this LNL is having issues with the GuC reading scratch page
473 	 * vs. correct GGTT page. Not particularly a hot code path so blindly
474 	 * do a mmio read here which results in GuC reading correct GGTT page.
475 	 */
476 	xe_mmio_read32(xe_root_tile_mmio(xe), VF_CAP_REG);
477 
478 	/* Each GT in a tile has its own TLB to cache GGTT lookups */
479 	ggtt_invalidate_gt_tlb(ggtt->tile->primary_gt);
480 	ggtt_invalidate_gt_tlb(ggtt->tile->media_gt);
481 }
482 
483 static void xe_ggtt_dump_node(struct xe_ggtt *ggtt,
484 			      const struct drm_mm_node *node, const char *description)
485 {
486 	char buf[10];
487 
488 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
489 		string_get_size(node->size, 1, STRING_UNITS_2, buf, sizeof(buf));
490 		xe_tile_dbg(ggtt->tile, "GGTT %#llx-%#llx (%s) %s\n",
491 			    node->start, node->start + node->size, buf, description);
492 	}
493 }
494 
495 /**
496  * xe_ggtt_node_insert_balloon_locked - prevent allocation of specified GGTT addresses
497  * @node: the &xe_ggtt_node to hold reserved GGTT node
498  * @start: the starting GGTT address of the reserved region
499  * @end: then end GGTT address of the reserved region
500  *
501  * To be used in cases where ggtt->lock is already taken.
502  * Use xe_ggtt_node_remove_balloon_locked() to release a reserved GGTT node.
503  *
504  * Return: 0 on success or a negative error code on failure.
505  */
506 int xe_ggtt_node_insert_balloon_locked(struct xe_ggtt_node *node, u64 start, u64 end)
507 {
508 	struct xe_ggtt *ggtt = node->ggtt;
509 	int err;
510 
511 	xe_tile_assert(ggtt->tile, start < end);
512 	xe_tile_assert(ggtt->tile, IS_ALIGNED(start, XE_PAGE_SIZE));
513 	xe_tile_assert(ggtt->tile, IS_ALIGNED(end, XE_PAGE_SIZE));
514 	xe_tile_assert(ggtt->tile, !drm_mm_node_allocated(&node->base));
515 	lockdep_assert_held(&ggtt->lock);
516 
517 	node->base.color = 0;
518 	node->base.start = start;
519 	node->base.size = end - start;
520 
521 	err = drm_mm_reserve_node(&ggtt->mm, &node->base);
522 
523 	if (xe_tile_WARN(ggtt->tile, err, "Failed to balloon GGTT %#llx-%#llx (%pe)\n",
524 			 node->base.start, node->base.start + node->base.size, ERR_PTR(err)))
525 		return err;
526 
527 	xe_ggtt_dump_node(ggtt, &node->base, "balloon");
528 	return 0;
529 }
530 
531 /**
532  * xe_ggtt_node_remove_balloon_locked - release a reserved GGTT region
533  * @node: the &xe_ggtt_node with reserved GGTT region
534  *
535  * To be used in cases where ggtt->lock is already taken.
536  * See xe_ggtt_node_insert_balloon_locked() for details.
537  */
538 void xe_ggtt_node_remove_balloon_locked(struct xe_ggtt_node *node)
539 {
540 	if (!xe_ggtt_node_allocated(node))
541 		return;
542 
543 	lockdep_assert_held(&node->ggtt->lock);
544 
545 	xe_ggtt_dump_node(node->ggtt, &node->base, "remove-balloon");
546 
547 	drm_mm_remove_node(&node->base);
548 }
549 
550 static void xe_ggtt_assert_fit(struct xe_ggtt *ggtt, u64 start, u64 size)
551 {
552 	struct xe_tile *tile = ggtt->tile;
553 	struct xe_device *xe = tile_to_xe(tile);
554 	u64 __maybe_unused wopcm = xe_wopcm_size(xe);
555 
556 	xe_tile_assert(tile, start >= wopcm);
557 	xe_tile_assert(tile, start + size < ggtt->size - wopcm);
558 }
559 
560 /**
561  * xe_ggtt_shift_nodes_locked - Shift GGTT nodes to adjust for a change in usable address range.
562  * @ggtt: the &xe_ggtt struct instance
563  * @shift: change to the location of area provisioned for current VF
564  *
565  * This function moves all nodes from the GGTT VM, to a temp list. These nodes are expected
566  * to represent allocations in range formerly assigned to current VF, before the range changed.
567  * When the GGTT VM is completely clear of any nodes, they are re-added with shifted offsets.
568  *
569  * The function has no ability of failing - because it shifts existing nodes, without
570  * any additional processing. If the nodes were successfully existing at the old address,
571  * they will do the same at the new one. A fail inside this function would indicate that
572  * the list of nodes was either already damaged, or that the shift brings the address range
573  * outside of valid bounds. Both cases justify an assert rather than error code.
574  */
575 void xe_ggtt_shift_nodes_locked(struct xe_ggtt *ggtt, s64 shift)
576 {
577 	struct xe_tile *tile __maybe_unused = ggtt->tile;
578 	struct drm_mm_node *node, *tmpn;
579 	LIST_HEAD(temp_list_head);
580 
581 	lockdep_assert_held(&ggtt->lock);
582 
583 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG))
584 		drm_mm_for_each_node_safe(node, tmpn, &ggtt->mm)
585 			xe_ggtt_assert_fit(ggtt, node->start + shift, node->size);
586 
587 	drm_mm_for_each_node_safe(node, tmpn, &ggtt->mm) {
588 		drm_mm_remove_node(node);
589 		list_add(&node->node_list, &temp_list_head);
590 	}
591 
592 	list_for_each_entry_safe(node, tmpn, &temp_list_head, node_list) {
593 		list_del(&node->node_list);
594 		node->start += shift;
595 		drm_mm_reserve_node(&ggtt->mm, node);
596 		xe_tile_assert(tile, drm_mm_node_allocated(node));
597 	}
598 }
599 
600 /**
601  * xe_ggtt_node_insert_locked - Locked version to insert a &xe_ggtt_node into the GGTT
602  * @node: the &xe_ggtt_node to be inserted
603  * @size: size of the node
604  * @align: alignment constrain of the node
605  * @mm_flags: flags to control the node behavior
606  *
607  * It cannot be called without first having called xe_ggtt_init() once.
608  * To be used in cases where ggtt->lock is already taken.
609  *
610  * Return: 0 on success or a negative error code on failure.
611  */
612 int xe_ggtt_node_insert_locked(struct xe_ggtt_node *node,
613 			       u32 size, u32 align, u32 mm_flags)
614 {
615 	return drm_mm_insert_node_generic(&node->ggtt->mm, &node->base, size, align, 0,
616 					  mm_flags);
617 }
618 
619 /**
620  * xe_ggtt_node_insert - Insert a &xe_ggtt_node into the GGTT
621  * @node: the &xe_ggtt_node to be inserted
622  * @size: size of the node
623  * @align: alignment constrain of the node
624  *
625  * It cannot be called without first having called xe_ggtt_init() once.
626  *
627  * Return: 0 on success or a negative error code on failure.
628  */
629 int xe_ggtt_node_insert(struct xe_ggtt_node *node, u32 size, u32 align)
630 {
631 	int ret;
632 
633 	if (!node || !node->ggtt)
634 		return -ENOENT;
635 
636 	mutex_lock(&node->ggtt->lock);
637 	ret = xe_ggtt_node_insert_locked(node, size, align,
638 					 DRM_MM_INSERT_HIGH);
639 	mutex_unlock(&node->ggtt->lock);
640 
641 	return ret;
642 }
643 
644 /**
645  * xe_ggtt_node_init - Initialize %xe_ggtt_node struct
646  * @ggtt: the &xe_ggtt where the new node will later be inserted/reserved.
647  *
648  * This function will allocate the struct %xe_ggtt_node and return its pointer.
649  * This struct will then be freed after the node removal upon xe_ggtt_node_remove()
650  * or xe_ggtt_node_remove_balloon_locked().
651  * Having %xe_ggtt_node struct allocated doesn't mean that the node is already allocated
652  * in GGTT. Only the xe_ggtt_node_insert(), xe_ggtt_node_insert_locked(),
653  * xe_ggtt_node_insert_balloon_locked() will ensure the node is inserted or reserved in GGTT.
654  *
655  * Return: A pointer to %xe_ggtt_node struct on success. An ERR_PTR otherwise.
656  **/
657 struct xe_ggtt_node *xe_ggtt_node_init(struct xe_ggtt *ggtt)
658 {
659 	struct xe_ggtt_node *node = kzalloc(sizeof(*node), GFP_NOFS);
660 
661 	if (!node)
662 		return ERR_PTR(-ENOMEM);
663 
664 	INIT_WORK(&node->delayed_removal_work, ggtt_node_remove_work_func);
665 	node->ggtt = ggtt;
666 
667 	return node;
668 }
669 
670 /**
671  * xe_ggtt_node_fini - Forcebly finalize %xe_ggtt_node struct
672  * @node: the &xe_ggtt_node to be freed
673  *
674  * If anything went wrong with either xe_ggtt_node_insert(), xe_ggtt_node_insert_locked(),
675  * or xe_ggtt_node_insert_balloon_locked(); and this @node is not going to be reused, then,
676  * this function needs to be called to free the %xe_ggtt_node struct
677  **/
678 void xe_ggtt_node_fini(struct xe_ggtt_node *node)
679 {
680 	kfree(node);
681 }
682 
683 /**
684  * xe_ggtt_node_allocated - Check if node is allocated in GGTT
685  * @node: the &xe_ggtt_node to be inspected
686  *
687  * Return: True if allocated, False otherwise.
688  */
689 bool xe_ggtt_node_allocated(const struct xe_ggtt_node *node)
690 {
691 	if (!node || !node->ggtt)
692 		return false;
693 
694 	return drm_mm_node_allocated(&node->base);
695 }
696 
697 /**
698  * xe_ggtt_map_bo - Map the BO into GGTT
699  * @ggtt: the &xe_ggtt where node will be mapped
700  * @node: the &xe_ggtt_node where this BO is mapped
701  * @bo: the &xe_bo to be mapped
702  * @pat_index: Which pat_index to use.
703  */
704 void xe_ggtt_map_bo(struct xe_ggtt *ggtt, struct xe_ggtt_node *node,
705 		    struct xe_bo *bo, u16 pat_index)
706 {
707 
708 	u64 start, pte, end;
709 	struct xe_res_cursor cur;
710 
711 	if (XE_WARN_ON(!node))
712 		return;
713 
714 	start = node->base.start;
715 	end = start + xe_bo_size(bo);
716 
717 	pte = ggtt->pt_ops->pte_encode_flags(bo, pat_index);
718 	if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
719 		xe_assert(xe_bo_device(bo), bo->ttm.ttm);
720 
721 		for (xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &cur);
722 		     cur.remaining; xe_res_next(&cur, XE_PAGE_SIZE))
723 			ggtt->pt_ops->ggtt_set_pte(ggtt, end - cur.remaining,
724 						   pte | xe_res_dma(&cur));
725 	} else {
726 		/* Prepend GPU offset */
727 		pte |= vram_region_gpu_offset(bo->ttm.resource);
728 
729 		for (xe_res_first(bo->ttm.resource, 0, xe_bo_size(bo), &cur);
730 		     cur.remaining; xe_res_next(&cur, XE_PAGE_SIZE))
731 			ggtt->pt_ops->ggtt_set_pte(ggtt, end - cur.remaining,
732 						   pte + cur.start);
733 	}
734 }
735 
736 /**
737  * xe_ggtt_map_bo_unlocked - Restore a mapping of a BO into GGTT
738  * @ggtt: the &xe_ggtt where node will be mapped
739  * @bo: the &xe_bo to be mapped
740  *
741  * This is used to restore a GGTT mapping after suspend.
742  */
743 void xe_ggtt_map_bo_unlocked(struct xe_ggtt *ggtt, struct xe_bo *bo)
744 {
745 	u16 cache_mode = bo->flags & XE_BO_FLAG_NEEDS_UC ? XE_CACHE_NONE : XE_CACHE_WB;
746 	u16 pat_index = tile_to_xe(ggtt->tile)->pat.idx[cache_mode];
747 
748 	mutex_lock(&ggtt->lock);
749 	xe_ggtt_map_bo(ggtt, bo->ggtt_node[ggtt->tile->id], bo, pat_index);
750 	mutex_unlock(&ggtt->lock);
751 }
752 
753 static int __xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,
754 				  u64 start, u64 end, struct drm_exec *exec)
755 {
756 	u64 alignment = bo->min_align > 0 ? bo->min_align : XE_PAGE_SIZE;
757 	u8 tile_id = ggtt->tile->id;
758 	int err;
759 
760 	if (xe_bo_is_vram(bo) && ggtt->flags & XE_GGTT_FLAGS_64K)
761 		alignment = SZ_64K;
762 
763 	if (XE_WARN_ON(bo->ggtt_node[tile_id])) {
764 		/* Someone's already inserted this BO in the GGTT */
765 		xe_tile_assert(ggtt->tile, bo->ggtt_node[tile_id]->base.size == xe_bo_size(bo));
766 		return 0;
767 	}
768 
769 	err = xe_bo_validate(bo, NULL, false, exec);
770 	if (err)
771 		return err;
772 
773 	xe_pm_runtime_get_noresume(tile_to_xe(ggtt->tile));
774 
775 	bo->ggtt_node[tile_id] = xe_ggtt_node_init(ggtt);
776 	if (IS_ERR(bo->ggtt_node[tile_id])) {
777 		err = PTR_ERR(bo->ggtt_node[tile_id]);
778 		bo->ggtt_node[tile_id] = NULL;
779 		goto out;
780 	}
781 
782 	mutex_lock(&ggtt->lock);
783 	err = drm_mm_insert_node_in_range(&ggtt->mm, &bo->ggtt_node[tile_id]->base,
784 					  xe_bo_size(bo), alignment, 0, start, end, 0);
785 	if (err) {
786 		xe_ggtt_node_fini(bo->ggtt_node[tile_id]);
787 		bo->ggtt_node[tile_id] = NULL;
788 	} else {
789 		u16 cache_mode = bo->flags & XE_BO_FLAG_NEEDS_UC ? XE_CACHE_NONE : XE_CACHE_WB;
790 		u16 pat_index = tile_to_xe(ggtt->tile)->pat.idx[cache_mode];
791 
792 		xe_ggtt_map_bo(ggtt, bo->ggtt_node[tile_id], bo, pat_index);
793 	}
794 	mutex_unlock(&ggtt->lock);
795 
796 	if (!err && bo->flags & XE_BO_FLAG_GGTT_INVALIDATE)
797 		xe_ggtt_invalidate(ggtt);
798 
799 out:
800 	xe_pm_runtime_put(tile_to_xe(ggtt->tile));
801 
802 	return err;
803 }
804 
805 /**
806  * xe_ggtt_insert_bo_at - Insert BO at a specific GGTT space
807  * @ggtt: the &xe_ggtt where bo will be inserted
808  * @bo: the &xe_bo to be inserted
809  * @start: address where it will be inserted
810  * @end: end of the range where it will be inserted
811  * @exec: The drm_exec transaction to use for exhaustive eviction.
812  *
813  * Return: 0 on success or a negative error code on failure.
814  */
815 int xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,
816 			 u64 start, u64 end, struct drm_exec *exec)
817 {
818 	return __xe_ggtt_insert_bo_at(ggtt, bo, start, end, exec);
819 }
820 
821 /**
822  * xe_ggtt_insert_bo - Insert BO into GGTT
823  * @ggtt: the &xe_ggtt where bo will be inserted
824  * @bo: the &xe_bo to be inserted
825  * @exec: The drm_exec transaction to use for exhaustive eviction.
826  *
827  * Return: 0 on success or a negative error code on failure.
828  */
829 int xe_ggtt_insert_bo(struct xe_ggtt *ggtt, struct xe_bo *bo,
830 		      struct drm_exec *exec)
831 {
832 	return __xe_ggtt_insert_bo_at(ggtt, bo, 0, U64_MAX, exec);
833 }
834 
835 /**
836  * xe_ggtt_remove_bo - Remove a BO from the GGTT
837  * @ggtt: the &xe_ggtt where node will be removed
838  * @bo: the &xe_bo to be removed
839  */
840 void xe_ggtt_remove_bo(struct xe_ggtt *ggtt, struct xe_bo *bo)
841 {
842 	u8 tile_id = ggtt->tile->id;
843 
844 	if (XE_WARN_ON(!bo->ggtt_node[tile_id]))
845 		return;
846 
847 	/* This BO is not currently in the GGTT */
848 	xe_tile_assert(ggtt->tile, bo->ggtt_node[tile_id]->base.size == xe_bo_size(bo));
849 
850 	xe_ggtt_node_remove(bo->ggtt_node[tile_id],
851 			    bo->flags & XE_BO_FLAG_GGTT_INVALIDATE);
852 }
853 
854 /**
855  * xe_ggtt_largest_hole - Largest GGTT hole
856  * @ggtt: the &xe_ggtt that will be inspected
857  * @alignment: minimum alignment
858  * @spare: If not NULL: in: desired memory size to be spared / out: Adjusted possible spare
859  *
860  * Return: size of the largest continuous GGTT region
861  */
862 u64 xe_ggtt_largest_hole(struct xe_ggtt *ggtt, u64 alignment, u64 *spare)
863 {
864 	const struct drm_mm *mm = &ggtt->mm;
865 	const struct drm_mm_node *entry;
866 	u64 hole_min_start = xe_wopcm_size(tile_to_xe(ggtt->tile));
867 	u64 hole_start, hole_end, hole_size;
868 	u64 max_hole = 0;
869 
870 	mutex_lock(&ggtt->lock);
871 
872 	drm_mm_for_each_hole(entry, mm, hole_start, hole_end) {
873 		hole_start = max(hole_start, hole_min_start);
874 		hole_start = ALIGN(hole_start, alignment);
875 		hole_end = ALIGN_DOWN(hole_end, alignment);
876 		if (hole_start >= hole_end)
877 			continue;
878 		hole_size = hole_end - hole_start;
879 		if (spare)
880 			*spare -= min3(*spare, hole_size, max_hole);
881 		max_hole = max(max_hole, hole_size);
882 	}
883 
884 	mutex_unlock(&ggtt->lock);
885 
886 	return max_hole;
887 }
888 
889 #ifdef CONFIG_PCI_IOV
890 static u64 xe_encode_vfid_pte(u16 vfid)
891 {
892 	return FIELD_PREP(GGTT_PTE_VFID, vfid) | XE_PAGE_PRESENT;
893 }
894 
895 static void xe_ggtt_assign_locked(struct xe_ggtt *ggtt, const struct drm_mm_node *node, u16 vfid)
896 {
897 	u64 start = node->start;
898 	u64 size = node->size;
899 	u64 end = start + size - 1;
900 	u64 pte = xe_encode_vfid_pte(vfid);
901 
902 	lockdep_assert_held(&ggtt->lock);
903 
904 	if (!drm_mm_node_allocated(node))
905 		return;
906 
907 	while (start < end) {
908 		ggtt->pt_ops->ggtt_set_pte(ggtt, start, pte);
909 		start += XE_PAGE_SIZE;
910 	}
911 
912 	xe_ggtt_invalidate(ggtt);
913 }
914 
915 /**
916  * xe_ggtt_assign - assign a GGTT region to the VF
917  * @node: the &xe_ggtt_node to update
918  * @vfid: the VF identifier
919  *
920  * This function is used by the PF driver to assign a GGTT region to the VF.
921  * In addition to PTE's VFID bits 11:2 also PRESENT bit 0 is set as on some
922  * platforms VFs can't modify that either.
923  */
924 void xe_ggtt_assign(const struct xe_ggtt_node *node, u16 vfid)
925 {
926 	mutex_lock(&node->ggtt->lock);
927 	xe_ggtt_assign_locked(node->ggtt, &node->base, vfid);
928 	mutex_unlock(&node->ggtt->lock);
929 }
930 #endif
931 
932 /**
933  * xe_ggtt_dump - Dump GGTT for debug
934  * @ggtt: the &xe_ggtt to be dumped
935  * @p: the &drm_mm_printer helper handle to be used to dump the information
936  *
937  * Return: 0 on success or a negative error code on failure.
938  */
939 int xe_ggtt_dump(struct xe_ggtt *ggtt, struct drm_printer *p)
940 {
941 	int err;
942 
943 	err = mutex_lock_interruptible(&ggtt->lock);
944 	if (err)
945 		return err;
946 
947 	drm_mm_print(&ggtt->mm, p);
948 	mutex_unlock(&ggtt->lock);
949 	return err;
950 }
951 
952 /**
953  * xe_ggtt_print_holes - Print holes
954  * @ggtt: the &xe_ggtt to be inspected
955  * @alignment: min alignment
956  * @p: the &drm_printer
957  *
958  * Print GGTT ranges that are available and return total size available.
959  *
960  * Return: Total available size.
961  */
962 u64 xe_ggtt_print_holes(struct xe_ggtt *ggtt, u64 alignment, struct drm_printer *p)
963 {
964 	const struct drm_mm *mm = &ggtt->mm;
965 	const struct drm_mm_node *entry;
966 	u64 hole_min_start = xe_wopcm_size(tile_to_xe(ggtt->tile));
967 	u64 hole_start, hole_end, hole_size;
968 	u64 total = 0;
969 	char buf[10];
970 
971 	mutex_lock(&ggtt->lock);
972 
973 	drm_mm_for_each_hole(entry, mm, hole_start, hole_end) {
974 		hole_start = max(hole_start, hole_min_start);
975 		hole_start = ALIGN(hole_start, alignment);
976 		hole_end = ALIGN_DOWN(hole_end, alignment);
977 		if (hole_start >= hole_end)
978 			continue;
979 		hole_size = hole_end - hole_start;
980 		total += hole_size;
981 
982 		string_get_size(hole_size, 1, STRING_UNITS_2, buf, sizeof(buf));
983 		drm_printf(p, "range:\t%#llx-%#llx\t(%s)\n",
984 			   hole_start, hole_end - 1, buf);
985 	}
986 
987 	mutex_unlock(&ggtt->lock);
988 
989 	return total;
990 }
991 
992 /**
993  * xe_ggtt_encode_pte_flags - Get PTE encoding flags for BO
994  * @ggtt: &xe_ggtt
995  * @bo: &xe_bo
996  * @pat_index: The pat_index for the PTE.
997  *
998  * This function returns the pte_flags for a given BO, without  address.
999  * It's used for DPT to fill a GGTT mapped BO with a linear lookup table.
1000  */
1001 u64 xe_ggtt_encode_pte_flags(struct xe_ggtt *ggtt,
1002 			     struct xe_bo *bo, u16 pat_index)
1003 {
1004 	return ggtt->pt_ops->pte_encode_flags(bo, pat_index);
1005 }
1006 
1007 /**
1008  * xe_ggtt_read_pte - Read a PTE from the GGTT
1009  * @ggtt: &xe_ggtt
1010  * @offset: the offset for which the mapping should be read.
1011  *
1012  * Used by testcases, and by display reading out an inherited bios FB.
1013  */
1014 u64 xe_ggtt_read_pte(struct xe_ggtt *ggtt, u64 offset)
1015 {
1016 	return ioread64(ggtt->gsm + (offset / XE_PAGE_SIZE));
1017 }
1018