1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2021-2024 Intel Corporation
4 */
5
6 #include <kunit/visibility.h>
7 #include <linux/pci.h>
8
9 #include <drm/drm_managed.h>
10 #include <drm/drm_print.h>
11
12 #include "regs/xe_bars.h"
13 #include "regs/xe_gt_regs.h"
14 #include "regs/xe_regs.h"
15 #include "xe_assert.h"
16 #include "xe_device.h"
17 #include "xe_force_wake.h"
18 #include "xe_gt_mcr.h"
19 #include "xe_gt_sriov_vf.h"
20 #include "xe_mmio.h"
21 #include "xe_module.h"
22 #include "xe_sriov.h"
23 #include "xe_ttm_vram_mgr.h"
24 #include "xe_vram.h"
25 #include "xe_vram_types.h"
26
27 #define BAR_SIZE_SHIFT 20
28
29 /*
30 * Release all the BARs that could influence/block LMEMBAR resizing, i.e.
31 * assigned IORESOURCE_MEM_64 BARs
32 */
release_bars(struct pci_dev * pdev)33 static void release_bars(struct pci_dev *pdev)
34 {
35 struct resource *res;
36 int i;
37
38 pci_dev_for_each_resource(pdev, res, i) {
39 /* Resource already un-assigned, do not reset it */
40 if (!res->parent)
41 continue;
42
43 /* No need to release unrelated BARs */
44 if (!(res->flags & IORESOURCE_MEM_64))
45 continue;
46
47 pci_release_resource(pdev, i);
48 }
49 }
50
resize_bar(struct xe_device * xe,int resno,resource_size_t size)51 static void resize_bar(struct xe_device *xe, int resno, resource_size_t size)
52 {
53 struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
54 int bar_size = pci_rebar_bytes_to_size(size);
55 int ret;
56
57 release_bars(pdev);
58
59 ret = pci_resize_resource(pdev, resno, bar_size);
60 if (ret) {
61 drm_info(&xe->drm, "Failed to resize BAR%d to %dM (%pe). Consider enabling 'Resizable BAR' support in your BIOS\n",
62 resno, 1 << bar_size, ERR_PTR(ret));
63 return;
64 }
65
66 drm_info(&xe->drm, "BAR%d resized to %dM\n", resno, 1 << bar_size);
67 }
68
69 /*
70 * if force_vram_bar_size is set, attempt to set to the requested size
71 * else set to maximum possible size
72 */
xe_vram_resize_bar(struct xe_device * xe)73 void xe_vram_resize_bar(struct xe_device *xe)
74 {
75 int force_vram_bar_size = xe_modparam.force_vram_bar_size;
76 struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
77 struct pci_bus *root = pdev->bus;
78 resource_size_t current_size;
79 resource_size_t rebar_size;
80 struct resource *root_res;
81 u32 bar_size_mask;
82 u32 pci_cmd;
83 int i;
84
85 /* gather some relevant info */
86 current_size = pci_resource_len(pdev, LMEM_BAR);
87 bar_size_mask = pci_rebar_get_possible_sizes(pdev, LMEM_BAR);
88
89 if (!bar_size_mask)
90 return;
91
92 if (force_vram_bar_size < 0)
93 return;
94
95 /* set to a specific size? */
96 if (force_vram_bar_size) {
97 u32 bar_size_bit;
98
99 rebar_size = force_vram_bar_size * (resource_size_t)SZ_1M;
100
101 bar_size_bit = bar_size_mask & BIT(pci_rebar_bytes_to_size(rebar_size));
102
103 if (!bar_size_bit) {
104 drm_info(&xe->drm,
105 "Requested size: %lluMiB is not supported by rebar sizes: 0x%x. Leaving default: %lluMiB\n",
106 (u64)rebar_size >> 20, bar_size_mask, (u64)current_size >> 20);
107 return;
108 }
109
110 rebar_size = 1ULL << (__fls(bar_size_bit) + BAR_SIZE_SHIFT);
111
112 if (rebar_size == current_size)
113 return;
114 } else {
115 rebar_size = 1ULL << (__fls(bar_size_mask) + BAR_SIZE_SHIFT);
116
117 /* only resize if larger than current */
118 if (rebar_size <= current_size)
119 return;
120 }
121
122 drm_info(&xe->drm, "Attempting to resize bar from %lluMiB -> %lluMiB\n",
123 (u64)current_size >> 20, (u64)rebar_size >> 20);
124
125 while (root->parent)
126 root = root->parent;
127
128 pci_bus_for_each_resource(root, root_res, i) {
129 if (root_res && root_res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
130 (u64)root_res->start > 0x100000000ul)
131 break;
132 }
133
134 if (!root_res) {
135 drm_info(&xe->drm, "Can't resize VRAM BAR - platform support is missing. Consider enabling 'Resizable BAR' support in your BIOS\n");
136 return;
137 }
138
139 pci_read_config_dword(pdev, PCI_COMMAND, &pci_cmd);
140 pci_write_config_dword(pdev, PCI_COMMAND, pci_cmd & ~PCI_COMMAND_MEMORY);
141
142 resize_bar(xe, LMEM_BAR, rebar_size);
143
144 pci_assign_unassigned_bus_resources(pdev->bus);
145 pci_write_config_dword(pdev, PCI_COMMAND, pci_cmd);
146 }
147
resource_is_valid(struct pci_dev * pdev,int bar)148 static bool resource_is_valid(struct pci_dev *pdev, int bar)
149 {
150 if (!pci_resource_flags(pdev, bar))
151 return false;
152
153 if (pci_resource_flags(pdev, bar) & IORESOURCE_UNSET)
154 return false;
155
156 if (!pci_resource_len(pdev, bar))
157 return false;
158
159 return true;
160 }
161
determine_lmem_bar_size(struct xe_device * xe,struct xe_vram_region * lmem_bar)162 static int determine_lmem_bar_size(struct xe_device *xe, struct xe_vram_region *lmem_bar)
163 {
164 struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
165
166 if (!resource_is_valid(pdev, LMEM_BAR)) {
167 drm_err(&xe->drm, "pci resource is not valid\n");
168 return -ENXIO;
169 }
170
171 lmem_bar->io_start = pci_resource_start(pdev, LMEM_BAR);
172 lmem_bar->io_size = pci_resource_len(pdev, LMEM_BAR);
173 if (!lmem_bar->io_size)
174 return -EIO;
175
176 /* XXX: Need to change when xe link code is ready */
177 lmem_bar->dpa_base = 0;
178
179 /* set up a map to the total memory area. */
180 lmem_bar->mapping = devm_ioremap_wc(&pdev->dev, lmem_bar->io_start, lmem_bar->io_size);
181
182 return 0;
183 }
184
get_flat_ccs_offset(struct xe_gt * gt,u64 tile_size)185 static inline u64 get_flat_ccs_offset(struct xe_gt *gt, u64 tile_size)
186 {
187 struct xe_device *xe = gt_to_xe(gt);
188 u64 offset;
189 u32 reg;
190
191 if (GRAPHICS_VER(xe) >= 20) {
192 u64 ccs_size = tile_size / 512;
193 u64 offset_hi, offset_lo;
194 u32 nodes, num_enabled;
195
196 reg = xe_mmio_read32(>->mmio, MIRROR_FUSE3);
197 nodes = REG_FIELD_GET(XE2_NODE_ENABLE_MASK, reg);
198 num_enabled = hweight32(nodes); /* Number of enabled l3 nodes */
199
200 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER);
201 offset_lo = REG_FIELD_GET(XE2_FLAT_CCS_BASE_LOWER_ADDR_MASK, reg);
202
203 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_UPPER);
204 offset_hi = REG_FIELD_GET(XE2_FLAT_CCS_BASE_UPPER_ADDR_MASK, reg);
205
206 offset = offset_hi << 32; /* HW view bits 39:32 */
207 offset |= offset_lo << 6; /* HW view bits 31:6 */
208 offset *= num_enabled; /* convert to SW view */
209 offset = round_up(offset, SZ_128K); /* SW must round up to nearest 128K */
210
211 /* We don't expect any holes */
212 xe_assert_msg(xe, offset == (xe_mmio_read64_2x32(>_to_tile(gt)->mmio, GSMBASE) -
213 ccs_size),
214 "Hole between CCS and GSM.\n");
215 } else {
216 reg = xe_gt_mcr_unicast_read_any(gt, XEHP_FLAT_CCS_BASE_ADDR);
217 offset = (u64)REG_FIELD_GET(XEHP_FLAT_CCS_PTR, reg) * SZ_64K;
218 }
219
220 return offset;
221 }
222
223 /*
224 * tile_vram_size() - Collect vram size and offset information
225 * @tile: tile to get info for
226 * @vram_size: available vram (size - device reserved portions)
227 * @tile_size: actual vram size
228 * @tile_offset: physical start point in the vram address space
229 *
230 * There are 4 places for size information:
231 * - io size (from pci_resource_len of LMEM bar) (only used for small bar and DG1)
232 * - TILEx size (actual vram size)
233 * - GSMBASE offset (TILEx - "stolen")
234 * - CSSBASE offset (TILEx - CSS space necessary)
235 *
236 * CSSBASE is always a lower/smaller offset then GSMBASE.
237 *
238 * The actual available size of memory is to the CCS or GSM base.
239 * NOTE: multi-tile bases will include the tile offset.
240 *
241 */
tile_vram_size(struct xe_tile * tile,u64 * vram_size,u64 * tile_size,u64 * tile_offset)242 static int tile_vram_size(struct xe_tile *tile, u64 *vram_size,
243 u64 *tile_size, u64 *tile_offset)
244 {
245 struct xe_device *xe = tile_to_xe(tile);
246 struct xe_gt *gt = tile->primary_gt;
247 unsigned int fw_ref;
248 u64 offset;
249 u32 reg;
250
251 if (IS_SRIOV_VF(xe)) {
252 struct xe_tile *t;
253 int id;
254
255 offset = 0;
256 for_each_tile(t, xe, id)
257 for_each_if(t->id < tile->id)
258 offset += xe_gt_sriov_vf_lmem(t->primary_gt);
259
260 *tile_size = xe_gt_sriov_vf_lmem(gt);
261 *vram_size = *tile_size;
262 *tile_offset = offset;
263
264 return 0;
265 }
266
267 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
268 if (!fw_ref)
269 return -ETIMEDOUT;
270
271 /* actual size */
272 if (unlikely(xe->info.platform == XE_DG1)) {
273 *tile_size = pci_resource_len(to_pci_dev(xe->drm.dev), LMEM_BAR);
274 *tile_offset = 0;
275 } else {
276 reg = xe_gt_mcr_unicast_read_any(gt, XEHP_TILE_ADDR_RANGE(gt->info.id));
277 *tile_size = (u64)REG_FIELD_GET(GENMASK(14, 8), reg) * SZ_1G;
278 *tile_offset = (u64)REG_FIELD_GET(GENMASK(7, 1), reg) * SZ_1G;
279 }
280
281 /* minus device usage */
282 if (xe->info.has_flat_ccs) {
283 offset = get_flat_ccs_offset(gt, *tile_size);
284 } else {
285 offset = xe_mmio_read64_2x32(&tile->mmio, GSMBASE);
286 }
287
288 /* remove the tile offset so we have just the available size */
289 *vram_size = offset - *tile_offset;
290
291 xe_force_wake_put(gt_to_fw(gt), fw_ref);
292
293 return 0;
294 }
295
vram_fini(void * arg)296 static void vram_fini(void *arg)
297 {
298 struct xe_device *xe = arg;
299 struct xe_tile *tile;
300 int id;
301
302 xe->mem.vram->mapping = NULL;
303
304 for_each_tile(tile, xe, id)
305 tile->mem.vram->mapping = NULL;
306 }
307
xe_vram_region_alloc(struct xe_device * xe,u8 id,u32 placement)308 struct xe_vram_region *xe_vram_region_alloc(struct xe_device *xe, u8 id, u32 placement)
309 {
310 struct xe_vram_region *vram;
311 struct drm_device *drm = &xe->drm;
312
313 xe_assert(xe, id < xe->info.tile_count);
314
315 vram = drmm_kzalloc(drm, sizeof(*vram), GFP_KERNEL);
316 if (!vram)
317 return NULL;
318
319 vram->xe = xe;
320 vram->id = id;
321 vram->placement = placement;
322 #if defined(CONFIG_DRM_XE_PAGEMAP)
323 vram->migrate = xe->tiles[id].migrate;
324 #endif
325 return vram;
326 }
327
print_vram_region_info(struct xe_device * xe,struct xe_vram_region * vram)328 static void print_vram_region_info(struct xe_device *xe, struct xe_vram_region *vram)
329 {
330 struct drm_device *drm = &xe->drm;
331
332 if (vram->io_size < vram->usable_size)
333 drm_info(drm, "Small BAR device\n");
334
335 drm_info(drm,
336 "VRAM[%u]: Actual physical size %pa, usable size exclude stolen %pa, CPU accessible size %pa\n",
337 vram->id, &vram->actual_physical_size, &vram->usable_size, &vram->io_size);
338 drm_info(drm, "VRAM[%u]: DPA range: [%pa-%llx], io range: [%pa-%llx]\n",
339 vram->id, &vram->dpa_base, vram->dpa_base + (u64)vram->actual_physical_size,
340 &vram->io_start, vram->io_start + (u64)vram->io_size);
341 }
342
vram_region_init(struct xe_device * xe,struct xe_vram_region * vram,struct xe_vram_region * lmem_bar,u64 offset,u64 usable_size,u64 region_size,resource_size_t remain_io_size)343 static int vram_region_init(struct xe_device *xe, struct xe_vram_region *vram,
344 struct xe_vram_region *lmem_bar, u64 offset, u64 usable_size,
345 u64 region_size, resource_size_t remain_io_size)
346 {
347 /* Check if VRAM region is already initialized */
348 if (vram->mapping)
349 return 0;
350
351 vram->actual_physical_size = region_size;
352 vram->io_start = lmem_bar->io_start + offset;
353 vram->io_size = min_t(u64, usable_size, remain_io_size);
354
355 if (!vram->io_size) {
356 drm_err(&xe->drm, "Tile without any CPU visible VRAM. Aborting.\n");
357 return -ENODEV;
358 }
359
360 vram->dpa_base = lmem_bar->dpa_base + offset;
361 vram->mapping = lmem_bar->mapping + offset;
362 vram->usable_size = usable_size;
363
364 print_vram_region_info(xe, vram);
365
366 return 0;
367 }
368
369 /**
370 * xe_vram_probe() - Probe VRAM configuration
371 * @xe: the &xe_device
372 *
373 * Collect VRAM size and offset information for all tiles.
374 *
375 * Return: 0 on success, error code on failure
376 */
xe_vram_probe(struct xe_device * xe)377 int xe_vram_probe(struct xe_device *xe)
378 {
379 struct xe_tile *tile;
380 struct xe_vram_region lmem_bar;
381 resource_size_t remain_io_size;
382 u64 available_size = 0;
383 u64 total_size = 0;
384 int err;
385 u8 id;
386
387 if (!IS_DGFX(xe))
388 return 0;
389
390 err = determine_lmem_bar_size(xe, &lmem_bar);
391 if (err)
392 return err;
393 drm_info(&xe->drm, "VISIBLE VRAM: %pa, %pa\n", &lmem_bar.io_start, &lmem_bar.io_size);
394
395 remain_io_size = lmem_bar.io_size;
396
397 for_each_tile(tile, xe, id) {
398 u64 region_size;
399 u64 usable_size;
400 u64 tile_offset;
401
402 err = tile_vram_size(tile, &usable_size, ®ion_size, &tile_offset);
403 if (err)
404 return err;
405
406 total_size += region_size;
407 available_size += usable_size;
408
409 err = vram_region_init(xe, tile->mem.vram, &lmem_bar, tile_offset, usable_size,
410 region_size, remain_io_size);
411 if (err)
412 return err;
413
414 if (total_size > lmem_bar.io_size) {
415 drm_info(&xe->drm, "VRAM: %pa is larger than resource %pa\n",
416 &total_size, &lmem_bar.io_size);
417 }
418
419 remain_io_size -= min_t(u64, tile->mem.vram->actual_physical_size, remain_io_size);
420 }
421
422 err = vram_region_init(xe, xe->mem.vram, &lmem_bar, 0, available_size, total_size,
423 lmem_bar.io_size);
424 if (err)
425 return err;
426
427 return devm_add_action_or_reset(xe->drm.dev, vram_fini, xe);
428 }
429
430 /**
431 * xe_vram_region_io_start - Get the IO start of a VRAM region
432 * @vram: the VRAM region
433 *
434 * Return: the IO start of the VRAM region, or 0 if not valid
435 */
xe_vram_region_io_start(const struct xe_vram_region * vram)436 resource_size_t xe_vram_region_io_start(const struct xe_vram_region *vram)
437 {
438 return vram ? vram->io_start : 0;
439 }
440
441 /**
442 * xe_vram_region_io_size - Get the IO size of a VRAM region
443 * @vram: the VRAM region
444 *
445 * Return: the IO size of the VRAM region, or 0 if not valid
446 */
xe_vram_region_io_size(const struct xe_vram_region * vram)447 resource_size_t xe_vram_region_io_size(const struct xe_vram_region *vram)
448 {
449 return vram ? vram->io_size : 0;
450 }
451
452 /**
453 * xe_vram_region_dpa_base - Get the DPA base of a VRAM region
454 * @vram: the VRAM region
455 *
456 * Return: the DPA base of the VRAM region, or 0 if not valid
457 */
xe_vram_region_dpa_base(const struct xe_vram_region * vram)458 resource_size_t xe_vram_region_dpa_base(const struct xe_vram_region *vram)
459 {
460 return vram ? vram->dpa_base : 0;
461 }
462
463 /**
464 * xe_vram_region_usable_size - Get the usable size of a VRAM region
465 * @vram: the VRAM region
466 *
467 * Return: the usable size of the VRAM region, or 0 if not valid
468 */
xe_vram_region_usable_size(const struct xe_vram_region * vram)469 resource_size_t xe_vram_region_usable_size(const struct xe_vram_region *vram)
470 {
471 return vram ? vram->usable_size : 0;
472 }
473
474 /**
475 * xe_vram_region_actual_physical_size - Get the actual physical size of a VRAM region
476 * @vram: the VRAM region
477 *
478 * Return: the actual physical size of the VRAM region, or 0 if not valid
479 */
xe_vram_region_actual_physical_size(const struct xe_vram_region * vram)480 resource_size_t xe_vram_region_actual_physical_size(const struct xe_vram_region *vram)
481 {
482 return vram ? vram->actual_physical_size : 0;
483 }
484 EXPORT_SYMBOL_IF_KUNIT(xe_vram_region_actual_physical_size);
485