1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2021-2023 Intel Corporation 4 */ 5 6 #include "xe_mmio.h" 7 8 #include <linux/delay.h> 9 #include <linux/io-64-nonatomic-lo-hi.h> 10 #include <linux/minmax.h> 11 #include <linux/pci.h> 12 13 #include <drm/drm_managed.h> 14 15 #include "regs/xe_bars.h" 16 #include "xe_device.h" 17 #include "xe_gt_sriov_vf.h" 18 #include "xe_printk.h" 19 #include "xe_sriov.h" 20 #include "xe_tile_printk.h" 21 #include "xe_trace.h" 22 #include "xe_wa.h" 23 24 #include "generated/xe_device_wa_oob.h" 25 26 /* 27 * On multi-tile devices, partition the BAR space for MMIO on each tile, 28 * possibly accounting for register override on the number of tiles available. 29 * tile_mmio_size contains both the tile's 4MB register space, as well as 30 * additional space for the GTT and other (possibly unused) regions). 31 * Resulting memory layout is like below: 32 * 33 * .----------------------. <- tile_count * tile_mmio_size 34 * | .... | 35 * |----------------------| <- 2 * tile_mmio_size 36 * | tile1 GTT + other | 37 * |----------------------| <- 1 * tile_mmio_size + 4MB 38 * | tile1->mmio.regs | 39 * |----------------------| <- 1 * tile_mmio_size 40 * | tile0 GTT + other | 41 * |----------------------| <- 4MB 42 * | tile0->mmio.regs | 43 * '----------------------' <- 0MB 44 */ 45 static void mmio_multi_tile_setup(struct xe_device *xe, size_t tile_mmio_size) 46 { 47 struct xe_tile *tile; 48 u8 id; 49 50 for_each_remote_tile(tile, xe, id) 51 xe_mmio_init(&tile->mmio, tile, xe->mmio.regs + id * tile_mmio_size, SZ_4M); 52 } 53 54 /** 55 * xe_mmio_probe_tiles() - Initialize all tiles' MMIO 56 * @xe: the &xe_device 57 * 58 * Initialize the remaining tiles' MMIO instances. 59 * 60 * Return: 0 on success or a negative error code on failure. 61 */ 62 int xe_mmio_probe_tiles(struct xe_device *xe) 63 { 64 size_t tile_mmio_size = SZ_16M; 65 66 /* 67 * Nothing to be done as tile 0 has already been setup earlier with the 68 * entire BAR mapped - see xe_mmio_probe_early() 69 */ 70 if (xe->info.tile_count == 1) 71 return 0; 72 73 if (xe->mmio.size < xe->info.tile_count * tile_mmio_size) { 74 xe_err(xe, "GTTMMADR_BAR is too small for %d tiles: %zu\n", 75 xe->info.tile_count, xe->mmio.size); 76 return -EIO; 77 } 78 79 mmio_multi_tile_setup(xe, tile_mmio_size); 80 return 0; 81 } 82 83 static void mmio_fini(void *arg) 84 { 85 struct xe_device *xe = arg; 86 87 xe->mmio.regs = NULL; 88 } 89 90 /** 91 * xe_mmio_probe_early() - Probe and initialize device's MMIO 92 * @xe: the &xe_device 93 * 94 * Map the entire GTTMMADR_BAR and initialize the first tile's MMIO instance. 95 * 96 * The first 16MB of the GTTMMADR_BAR always belongs to the root tile, and 97 * includes: registers (0-4MB), reserved space (4MB-8MB) and GGTT (8MB-16MB). 98 * 99 * Return: 0 on success or a negative error code on failure. 100 */ 101 int xe_mmio_probe_early(struct xe_device *xe) 102 { 103 struct xe_tile *root_tile = xe_device_get_root_tile(xe); 104 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 105 106 xe->mmio.regs = pcim_iomap(pdev, GTTMMADR_BAR, 0); 107 if (!xe->mmio.regs) { 108 xe_err(xe, "Failed to map GTTMMADR_BAR\n"); 109 return -EIO; 110 } 111 112 xe->mmio.size = pci_resource_len(pdev, GTTMMADR_BAR); 113 if (xe->mmio.size < SZ_16M) { 114 xe_err(xe, "GTTMMADR_BAR is too small: %zu\n", xe->mmio.size); 115 return -EIO; 116 } 117 118 /* Setup first tile; other tiles (if present) will be setup later. */ 119 xe_mmio_init(&root_tile->mmio, root_tile, xe->mmio.regs, SZ_4M); 120 121 return devm_add_action_or_reset(xe->drm.dev, mmio_fini, xe); 122 } 123 ALLOW_ERROR_INJECTION(xe_mmio_probe_early, ERRNO); /* See xe_pci_probe() */ 124 125 /** 126 * xe_mmio_init() - Initialize an MMIO instance 127 * @mmio: Pointer to the MMIO instance to initialize 128 * @tile: The tile to which the MMIO region belongs 129 * @ptr: Pointer to the start of the MMIO region 130 * @size: The size of the MMIO region in bytes 131 * 132 * This is a convenience function for minimal initialization of struct xe_mmio. 133 */ 134 void xe_mmio_init(struct xe_mmio *mmio, struct xe_tile *tile, void __iomem *ptr, u32 size) 135 { 136 xe_tile_assert(tile, size <= XE_REG_ADDR_MAX); 137 138 mmio->regs = ptr; 139 mmio->regs_size = size; 140 mmio->tile = tile; 141 } 142 143 static bool mmio_available(struct xe_mmio *mmio) 144 { 145 return !xe_tile_WARN_ON_ONCE(mmio->tile, !mmio->tile->xe->mmio.regs); 146 } 147 148 static void mmio_flush_pending_writes(struct xe_mmio *mmio) 149 { 150 #define DUMMY_REG_OFFSET 0x130030 151 int i; 152 153 if (!XE_DEVICE_WA(mmio->tile->xe, 15015404425)) 154 return; 155 156 /* 4 dummy writes */ 157 for (i = 0; i < 4; i++) 158 writel(0, mmio->regs + DUMMY_REG_OFFSET); 159 } 160 161 u8 xe_mmio_read8(struct xe_mmio *mmio, struct xe_reg reg) 162 { 163 u32 addr = xe_mmio_adjusted_addr(mmio, reg.addr); 164 u8 val; 165 166 if (!mmio_available(mmio)) 167 return 0; 168 169 mmio_flush_pending_writes(mmio); 170 171 val = readb(mmio->regs + addr); 172 trace_xe_reg_rw(mmio, false, addr, val, sizeof(val)); 173 174 return val; 175 } 176 177 void xe_mmio_write8(struct xe_mmio *mmio, struct xe_reg reg, u8 val) 178 { 179 u32 addr = xe_mmio_adjusted_addr(mmio, reg.addr); 180 181 if (!mmio_available(mmio)) 182 return; 183 184 trace_xe_reg_rw(mmio, true, addr, val, sizeof(val)); 185 186 writeb(val, mmio->regs + addr); 187 } 188 189 u16 xe_mmio_read16(struct xe_mmio *mmio, struct xe_reg reg) 190 { 191 u32 addr = xe_mmio_adjusted_addr(mmio, reg.addr); 192 u16 val; 193 194 if (!mmio_available(mmio)) 195 return 0; 196 197 mmio_flush_pending_writes(mmio); 198 199 val = readw(mmio->regs + addr); 200 trace_xe_reg_rw(mmio, false, addr, val, sizeof(val)); 201 202 return val; 203 } 204 205 void xe_mmio_write32(struct xe_mmio *mmio, struct xe_reg reg, u32 val) 206 { 207 u32 addr = xe_mmio_adjusted_addr(mmio, reg.addr); 208 209 if (!mmio_available(mmio)) 210 return; 211 212 trace_xe_reg_rw(mmio, true, addr, val, sizeof(val)); 213 214 if (!reg.vf && IS_SRIOV_VF(mmio->tile->xe)) 215 xe_gt_sriov_vf_write32(mmio->sriov_vf_gt ?: 216 mmio->tile->primary_gt, reg, val); 217 else 218 writel(val, mmio->regs + addr); 219 } 220 221 u32 xe_mmio_read32(struct xe_mmio *mmio, struct xe_reg reg) 222 { 223 u32 addr = xe_mmio_adjusted_addr(mmio, reg.addr); 224 u32 val; 225 226 if (!mmio_available(mmio)) 227 return 0; 228 229 mmio_flush_pending_writes(mmio); 230 231 if (!reg.vf && IS_SRIOV_VF(mmio->tile->xe)) 232 val = xe_gt_sriov_vf_read32(mmio->sriov_vf_gt ?: 233 mmio->tile->primary_gt, reg); 234 else 235 val = readl(mmio->regs + addr); 236 237 trace_xe_reg_rw(mmio, false, addr, val, sizeof(val)); 238 239 return val; 240 } 241 242 u32 xe_mmio_rmw32(struct xe_mmio *mmio, struct xe_reg reg, u32 clr, u32 set) 243 { 244 u32 old, reg_val; 245 246 old = xe_mmio_read32(mmio, reg); 247 reg_val = (old & ~clr) | set; 248 xe_mmio_write32(mmio, reg, reg_val); 249 250 return old; 251 } 252 253 int xe_mmio_write32_and_verify(struct xe_mmio *mmio, 254 struct xe_reg reg, u32 val, u32 mask, u32 eval) 255 { 256 u32 reg_val; 257 258 xe_mmio_write32(mmio, reg, val); 259 reg_val = xe_mmio_read32(mmio, reg); 260 261 return (reg_val & mask) != eval ? -EINVAL : 0; 262 } 263 264 bool xe_mmio_in_range(const struct xe_mmio *mmio, 265 const struct xe_mmio_range *range, 266 struct xe_reg reg) 267 { 268 u32 addr = xe_mmio_adjusted_addr(mmio, reg.addr); 269 270 return range && addr >= range->start && addr <= range->end; 271 } 272 273 /** 274 * xe_mmio_read64_2x32() - Read a 64-bit register as two 32-bit reads 275 * @mmio: MMIO target 276 * @reg: register to read value from 277 * 278 * Although Intel GPUs have some 64-bit registers, the hardware officially 279 * only supports GTTMMADR register reads of 32 bits or smaller. Even if 280 * a readq operation may return a reasonable value, that violation of the 281 * spec shouldn't be relied upon and all 64-bit register reads should be 282 * performed as two 32-bit reads of the upper and lower dwords. 283 * 284 * When reading registers that may be changing (such as 285 * counters), a rollover of the lower dword between the two 32-bit reads 286 * can be problematic. This function attempts to ensure the upper dword has 287 * stabilized before returning the 64-bit value. 288 * 289 * Note that because this function may re-read the register multiple times 290 * while waiting for the value to stabilize it should not be used to read 291 * any registers where read operations have side effects. 292 * 293 * Returns the value of the 64-bit register. 294 */ 295 u64 xe_mmio_read64_2x32(struct xe_mmio *mmio, struct xe_reg reg) 296 { 297 struct xe_reg reg_udw = { .addr = reg.addr + 0x4 }; 298 u32 ldw, udw, oldudw, retries; 299 300 /* 301 * The two dwords of a 64-bit register can never straddle the offset 302 * adjustment cutoff. 303 */ 304 xe_tile_assert(mmio->tile, !in_range(mmio->adj_limit, reg.addr + 1, 7)); 305 306 oldudw = xe_mmio_read32(mmio, reg_udw); 307 for (retries = 5; retries; --retries) { 308 ldw = xe_mmio_read32(mmio, reg); 309 udw = xe_mmio_read32(mmio, reg_udw); 310 311 if (udw == oldudw) 312 break; 313 314 oldudw = udw; 315 } 316 317 xe_tile_WARN(mmio->tile, retries == 0, 318 "MMIO: 64-bit read of %#x did not stabilize\n", reg.addr); 319 320 return (u64)udw << 32 | ldw; 321 } 322 323 static int __xe_mmio_wait32(struct xe_mmio *mmio, struct xe_reg reg, u32 mask, u32 val, 324 u32 timeout_us, u32 *out_val, bool atomic, bool expect_match) 325 { 326 ktime_t cur = ktime_get_raw(); 327 const ktime_t end = ktime_add_us(cur, timeout_us); 328 int ret = -ETIMEDOUT; 329 s64 wait = 10; 330 u32 read; 331 bool check; 332 333 for (;;) { 334 read = xe_mmio_read32(mmio, reg); 335 336 check = (read & mask) == val; 337 if (!expect_match) 338 check = !check; 339 340 if (check) { 341 ret = 0; 342 break; 343 } 344 345 cur = ktime_get_raw(); 346 if (!ktime_before(cur, end)) 347 break; 348 349 if (ktime_after(ktime_add_us(cur, wait), end)) 350 wait = ktime_us_delta(end, cur); 351 352 if (atomic) 353 udelay(wait); 354 else 355 usleep_range(wait, wait << 1); 356 wait <<= 1; 357 } 358 359 if (ret != 0) { 360 read = xe_mmio_read32(mmio, reg); 361 362 check = (read & mask) == val; 363 if (!expect_match) 364 check = !check; 365 366 if (check) 367 ret = 0; 368 } 369 370 if (out_val) 371 *out_val = read; 372 373 return ret; 374 } 375 376 /** 377 * xe_mmio_wait32() - Wait for a register to match the desired masked value 378 * @mmio: MMIO target 379 * @reg: register to read value from 380 * @mask: mask to be applied to the value read from the register 381 * @val: desired value after applying the mask 382 * @timeout_us: time out after this period of time. Wait logic tries to be 383 * smart, applying an exponential backoff until @timeout_us is reached. 384 * @out_val: if not NULL, points where to store the last unmasked value 385 * @atomic: needs to be true if calling from an atomic context 386 * 387 * This function polls for the desired masked value and returns zero on success 388 * or -ETIMEDOUT if timed out. 389 * 390 * Note that @timeout_us represents the minimum amount of time to wait before 391 * giving up. The actual time taken by this function can be a little more than 392 * @timeout_us for different reasons, specially in non-atomic contexts. Thus, 393 * it is possible that this function succeeds even after @timeout_us has passed. 394 */ 395 int xe_mmio_wait32(struct xe_mmio *mmio, struct xe_reg reg, u32 mask, u32 val, u32 timeout_us, 396 u32 *out_val, bool atomic) 397 { 398 return __xe_mmio_wait32(mmio, reg, mask, val, timeout_us, out_val, atomic, true); 399 } 400 401 /** 402 * xe_mmio_wait32_not() - Wait for a register to return anything other than the given masked value 403 * @mmio: MMIO target 404 * @reg: register to read value from 405 * @mask: mask to be applied to the value read from the register 406 * @val: value not to be matched after applying the mask 407 * @timeout_us: time out after this period of time 408 * @out_val: if not NULL, points where to store the last unmasked value 409 * @atomic: needs to be true if calling from an atomic context 410 * 411 * This function works exactly like xe_mmio_wait32() with the exception that 412 * @val is expected not to be matched. 413 */ 414 int xe_mmio_wait32_not(struct xe_mmio *mmio, struct xe_reg reg, u32 mask, u32 val, u32 timeout_us, 415 u32 *out_val, bool atomic) 416 { 417 return __xe_mmio_wait32(mmio, reg, mask, val, timeout_us, out_val, atomic, false); 418 } 419 420 #ifdef CONFIG_PCI_IOV 421 static size_t vf_regs_stride(struct xe_device *xe) 422 { 423 return GRAPHICS_VERx100(xe) > 1200 ? 0x400 : 0x1000; 424 } 425 426 /** 427 * xe_mmio_init_vf_view() - Initialize an MMIO instance for accesses like the VF 428 * @mmio: the target &xe_mmio to initialize as VF's view 429 * @base: the source &xe_mmio to initialize from 430 * @vfid: the VF identifier 431 */ 432 void xe_mmio_init_vf_view(struct xe_mmio *mmio, const struct xe_mmio *base, unsigned int vfid) 433 { 434 struct xe_tile *tile = base->tile; 435 struct xe_device *xe = tile->xe; 436 size_t offset = vf_regs_stride(xe) * vfid; 437 438 xe_assert(xe, IS_SRIOV_PF(xe)); 439 xe_assert(xe, vfid); 440 xe_assert(xe, !base->sriov_vf_gt); 441 xe_assert(xe, base->regs_size > offset); 442 443 *mmio = *base; 444 mmio->regs += offset; 445 mmio->regs_size -= offset; 446 } 447 #endif 448