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