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 int get_flat_ccs_offset(struct xe_gt *gt, u64 tile_size, u64 *poffset) 187 { 188 struct xe_device *xe = gt_to_xe(gt); 189 u64 offset; 190 u32 reg; 191 192 CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT); 193 if (!fw_ref.domains) 194 return -ETIMEDOUT; 195 196 if (GRAPHICS_VER(xe) >= 20) { 197 u64 ccs_size = tile_size / 512; 198 u64 offset_hi, offset_lo; 199 u32 nodes, num_enabled; 200 201 reg = xe_mmio_read32(>->mmio, MIRROR_FUSE3); 202 nodes = REG_FIELD_GET(XE2_NODE_ENABLE_MASK, reg); 203 num_enabled = hweight32(nodes); /* Number of enabled l3 nodes */ 204 205 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER); 206 offset_lo = REG_FIELD_GET(XE2_FLAT_CCS_BASE_LOWER_ADDR_MASK, reg); 207 208 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_UPPER); 209 offset_hi = REG_FIELD_GET(XE2_FLAT_CCS_BASE_UPPER_ADDR_MASK, reg); 210 211 offset = offset_hi << 32; /* HW view bits 39:32 */ 212 offset |= offset_lo << 6; /* HW view bits 31:6 */ 213 offset *= num_enabled; /* convert to SW view */ 214 offset = round_up(offset, SZ_128K); /* SW must round up to nearest 128K */ 215 216 /* We don't expect any holes */ 217 xe_assert_msg(xe, offset == (xe_mmio_read64_2x32(>_to_tile(gt)->mmio, GSMBASE) - 218 ccs_size), 219 "Hole between CCS and GSM.\n"); 220 } else { 221 reg = xe_gt_mcr_unicast_read_any(gt, XEHP_FLAT_CCS_BASE_ADDR); 222 offset = (u64)REG_FIELD_GET(XEHP_FLAT_CCS_PTR, reg) * SZ_64K; 223 } 224 225 *poffset = offset; 226 227 return 0; 228 } 229 230 /* 231 * tile_vram_size() - Collect vram size and offset information 232 * @tile: tile to get info for 233 * @vram_size: available vram (size - device reserved portions) 234 * @tile_size: actual vram size 235 * @tile_offset: physical start point in the vram address space 236 * 237 * There are 4 places for size information: 238 * - io size (from pci_resource_len of LMEM bar) (only used for small bar and DG1) 239 * - TILEx size (actual vram size) 240 * - GSMBASE offset (TILEx - "stolen") 241 * - CSSBASE offset (TILEx - CSS space necessary) 242 * 243 * CSSBASE is always a lower/smaller offset then GSMBASE. 244 * 245 * The actual available size of memory is to the CCS or GSM base. 246 * NOTE: multi-tile bases will include the tile offset. 247 * 248 */ 249 static int tile_vram_size(struct xe_tile *tile, u64 *vram_size, 250 u64 *tile_size, u64 *tile_offset) 251 { 252 struct xe_device *xe = tile_to_xe(tile); 253 struct xe_gt *gt = tile->primary_gt; 254 u64 offset; 255 u32 reg; 256 257 if (IS_SRIOV_VF(xe)) { 258 struct xe_tile *t; 259 int id; 260 261 offset = 0; 262 for_each_tile(t, xe, id) 263 for_each_if(t->id < tile->id) 264 offset += xe_tile_sriov_vf_lmem(t); 265 266 *tile_size = xe_tile_sriov_vf_lmem(tile); 267 *vram_size = *tile_size; 268 *tile_offset = offset; 269 270 return 0; 271 } 272 273 /* actual size */ 274 if (unlikely(xe->info.platform == XE_DG1)) { 275 *tile_size = pci_resource_len(to_pci_dev(xe->drm.dev), LMEM_BAR); 276 *tile_offset = 0; 277 } else { 278 reg = xe_mmio_read32(&tile->mmio, SG_TILE_ADDR_RANGE(tile->id)); 279 *tile_size = (u64)REG_FIELD_GET(GENMASK(14, 8), reg) * SZ_1G; 280 *tile_offset = (u64)REG_FIELD_GET(GENMASK(7, 1), reg) * SZ_1G; 281 } 282 283 /* minus device usage */ 284 if (xe->info.has_flat_ccs) { 285 int ret = get_flat_ccs_offset(gt, *tile_size, &offset); 286 287 if (ret) 288 return ret; 289 } else { 290 offset = xe_mmio_read64_2x32(&tile->mmio, GSMBASE); 291 } 292 293 /* remove the tile offset so we have just the available size */ 294 *vram_size = offset - *tile_offset; 295 296 return 0; 297 } 298 299 static void vram_fini(void *arg) 300 { 301 struct xe_device *xe = arg; 302 struct xe_tile *tile; 303 int id; 304 305 xe->mem.vram->mapping = NULL; 306 307 for_each_tile(tile, xe, id) { 308 tile->mem.vram->mapping = NULL; 309 if (tile->mem.kernel_vram) 310 tile->mem.kernel_vram->mapping = NULL; 311 } 312 } 313 314 struct xe_vram_region *xe_vram_region_alloc(struct xe_device *xe, u8 id, u32 placement) 315 { 316 struct xe_vram_region *vram; 317 struct drm_device *drm = &xe->drm; 318 319 xe_assert(xe, id < xe->info.tile_count); 320 321 vram = drmm_kzalloc(drm, sizeof(*vram), GFP_KERNEL); 322 if (!vram) 323 return NULL; 324 325 vram->xe = xe; 326 vram->id = id; 327 vram->placement = placement; 328 #if defined(CONFIG_DRM_XE_PAGEMAP) 329 vram->migrate = xe->tiles[id].migrate; 330 #endif 331 return vram; 332 } 333 334 static void print_vram_region_info(struct xe_device *xe, struct xe_vram_region *vram) 335 { 336 struct drm_device *drm = &xe->drm; 337 338 if (vram->io_size < vram->usable_size) 339 drm_info(drm, "Small BAR device\n"); 340 341 drm_info(drm, 342 "VRAM[%u]: Actual physical size %pa, usable size exclude stolen %pa, CPU accessible size %pa\n", 343 vram->id, &vram->actual_physical_size, &vram->usable_size, &vram->io_size); 344 drm_info(drm, "VRAM[%u]: DPA range: [%pa-%llx], io range: [%pa-%llx]\n", 345 vram->id, &vram->dpa_base, vram->dpa_base + (u64)vram->actual_physical_size, 346 &vram->io_start, vram->io_start + (u64)vram->io_size); 347 } 348 349 static int vram_region_init(struct xe_device *xe, struct xe_vram_region *vram, 350 struct xe_vram_region *lmem_bar, u64 offset, u64 usable_size, 351 u64 region_size, resource_size_t remain_io_size) 352 { 353 /* Check if VRAM region is already initialized */ 354 if (vram->mapping) 355 return 0; 356 357 vram->actual_physical_size = region_size; 358 vram->io_start = lmem_bar->io_start + offset; 359 vram->io_size = min_t(u64, usable_size, remain_io_size); 360 361 if (!vram->io_size) { 362 drm_err(&xe->drm, "Tile without any CPU visible VRAM. Aborting.\n"); 363 return -ENODEV; 364 } 365 366 vram->dpa_base = lmem_bar->dpa_base + offset; 367 vram->mapping = lmem_bar->mapping + offset; 368 vram->usable_size = usable_size; 369 370 print_vram_region_info(xe, vram); 371 372 return 0; 373 } 374 375 /** 376 * xe_vram_probe() - Probe VRAM configuration 377 * @xe: the &xe_device 378 * 379 * Collect VRAM size and offset information for all tiles. 380 * 381 * Return: 0 on success, error code on failure 382 */ 383 int xe_vram_probe(struct xe_device *xe) 384 { 385 struct xe_tile *tile; 386 struct xe_vram_region lmem_bar; 387 resource_size_t remain_io_size; 388 u64 available_size = 0; 389 u64 total_size = 0; 390 int err; 391 u8 id; 392 393 if (!IS_DGFX(xe)) 394 return 0; 395 396 err = determine_lmem_bar_size(xe, &lmem_bar); 397 if (err) 398 return err; 399 drm_info(&xe->drm, "VISIBLE VRAM: %pa, %pa\n", &lmem_bar.io_start, &lmem_bar.io_size); 400 401 remain_io_size = lmem_bar.io_size; 402 403 for_each_tile(tile, xe, id) { 404 u64 region_size; 405 u64 usable_size; 406 u64 tile_offset; 407 408 err = tile_vram_size(tile, &usable_size, ®ion_size, &tile_offset); 409 if (err) 410 return err; 411 412 total_size += region_size; 413 available_size += usable_size; 414 415 err = vram_region_init(xe, tile->mem.vram, &lmem_bar, tile_offset, usable_size, 416 region_size, remain_io_size); 417 if (err) 418 return err; 419 420 if (total_size > lmem_bar.io_size) { 421 drm_info(&xe->drm, "VRAM: %pa is larger than resource %pa\n", 422 &total_size, &lmem_bar.io_size); 423 } 424 425 remain_io_size -= min_t(u64, tile->mem.vram->actual_physical_size, remain_io_size); 426 } 427 428 err = vram_region_init(xe, xe->mem.vram, &lmem_bar, 0, available_size, total_size, 429 lmem_bar.io_size); 430 if (err) 431 return err; 432 433 return devm_add_action_or_reset(xe->drm.dev, vram_fini, xe); 434 } 435 436 /** 437 * xe_vram_region_io_start - Get the IO start of a VRAM region 438 * @vram: the VRAM region 439 * 440 * Return: the IO start of the VRAM region, or 0 if not valid 441 */ 442 resource_size_t xe_vram_region_io_start(const struct xe_vram_region *vram) 443 { 444 return vram ? vram->io_start : 0; 445 } 446 447 /** 448 * xe_vram_region_io_size - Get the IO size of a VRAM region 449 * @vram: the VRAM region 450 * 451 * Return: the IO size of the VRAM region, or 0 if not valid 452 */ 453 resource_size_t xe_vram_region_io_size(const struct xe_vram_region *vram) 454 { 455 return vram ? vram->io_size : 0; 456 } 457 458 /** 459 * xe_vram_region_dpa_base - Get the DPA base of a VRAM region 460 * @vram: the VRAM region 461 * 462 * Return: the DPA base of the VRAM region, or 0 if not valid 463 */ 464 resource_size_t xe_vram_region_dpa_base(const struct xe_vram_region *vram) 465 { 466 return vram ? vram->dpa_base : 0; 467 } 468 469 /** 470 * xe_vram_region_usable_size - Get the usable size of a VRAM region 471 * @vram: the VRAM region 472 * 473 * Return: the usable size of the VRAM region, or 0 if not valid 474 */ 475 resource_size_t xe_vram_region_usable_size(const struct xe_vram_region *vram) 476 { 477 return vram ? vram->usable_size : 0; 478 } 479 480 /** 481 * xe_vram_region_actual_physical_size - Get the actual physical size of a VRAM region 482 * @vram: the VRAM region 483 * 484 * Return: the actual physical size of the VRAM region, or 0 if not valid 485 */ 486 resource_size_t xe_vram_region_actual_physical_size(const struct xe_vram_region *vram) 487 { 488 return vram ? vram->actual_physical_size : 0; 489 } 490 EXPORT_SYMBOL_IF_KUNIT(xe_vram_region_actual_physical_size); 491