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