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