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