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