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