1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2013 Red Hat 4 * Author: Rob Clark <robdclark@gmail.com> 5 * 6 * Copyright (c) 2014,2017, 2019 The Linux Foundation. All rights reserved. 7 */ 8 9 #ifndef __ADRENO_GPU_H__ 10 #define __ADRENO_GPU_H__ 11 12 #include <linux/firmware.h> 13 #include <linux/iopoll.h> 14 15 #include "msm_gpu.h" 16 17 #include "adreno_common.xml.h" 18 #include "adreno_pm4.xml.h" 19 20 extern bool snapshot_debugbus; 21 extern bool allow_vram_carveout; 22 23 enum { 24 ADRENO_FW_PM4 = 0, 25 ADRENO_FW_SQE = 0, /* a6xx */ 26 ADRENO_FW_PFP = 1, 27 ADRENO_FW_GMU = 1, /* a6xx */ 28 ADRENO_FW_GPMU = 2, 29 ADRENO_FW_MAX, 30 }; 31 32 /** 33 * @enum adreno_family: identify generation and possibly sub-generation 34 * 35 * In some cases there are distinct sub-generations within a major revision 36 * so it helps to be able to group the GPU devices by generation and if 37 * necessary sub-generation. 38 */ 39 enum adreno_family { 40 ADRENO_2XX_GEN1, /* a20x */ 41 ADRENO_2XX_GEN2, /* a22x */ 42 ADRENO_3XX, 43 ADRENO_4XX, 44 ADRENO_5XX, 45 ADRENO_6XX_GEN1, /* a630 family */ 46 ADRENO_6XX_GEN2, /* a640 family */ 47 ADRENO_6XX_GEN3, /* a650 family */ 48 ADRENO_6XX_GEN4, /* a660 family */ 49 ADRENO_7XX_GEN1, /* a730 family */ 50 ADRENO_7XX_GEN2, /* a740 family */ 51 ADRENO_7XX_GEN3, /* a750 family */ 52 }; 53 54 #define ADRENO_QUIRK_TWO_PASS_USE_WFI BIT(0) 55 #define ADRENO_QUIRK_FAULT_DETECT_MASK BIT(1) 56 #define ADRENO_QUIRK_LMLOADKILL_DISABLE BIT(2) 57 #define ADRENO_QUIRK_HAS_HW_APRIV BIT(3) 58 #define ADRENO_QUIRK_HAS_CACHED_COHERENT BIT(4) 59 60 /* Helper for formating the chip_id in the way that userspace tools like 61 * crashdec expect. 62 */ 63 #define ADRENO_CHIPID_FMT "u.%u.%u.%u" 64 #define ADRENO_CHIPID_ARGS(_c) \ 65 (((_c) >> 24) & 0xff), \ 66 (((_c) >> 16) & 0xff), \ 67 (((_c) >> 8) & 0xff), \ 68 ((_c) & 0xff) 69 70 struct adreno_gpu_funcs { 71 struct msm_gpu_funcs base; 72 int (*get_timestamp)(struct msm_gpu *gpu, uint64_t *value); 73 }; 74 75 struct adreno_reglist { 76 u32 offset; 77 u32 value; 78 }; 79 80 struct adreno_speedbin { 81 uint16_t fuse; 82 uint16_t speedbin; 83 }; 84 85 struct a6xx_info; 86 87 struct adreno_info { 88 const char *machine; 89 /** 90 * @chipids: Table of matching chip-ids 91 * 92 * Terminated with 0 sentinal 93 */ 94 uint32_t *chip_ids; 95 enum adreno_family family; 96 uint32_t revn; 97 const char *fw[ADRENO_FW_MAX]; 98 uint32_t gmem; 99 u64 quirks; 100 struct msm_gpu *(*init)(struct drm_device *dev); 101 const char *zapfw; 102 u32 inactive_period; 103 union { 104 const struct a6xx_info *a6xx; 105 }; 106 u64 address_space_size; 107 /** 108 * @speedbins: Optional table of fuse to speedbin mappings 109 * 110 * Consists of pairs of fuse, index mappings, terminated with 111 * {SHRT_MAX, 0} sentinal. 112 */ 113 struct adreno_speedbin *speedbins; 114 }; 115 116 #define ADRENO_CHIP_IDS(tbl...) (uint32_t[]) { tbl, 0 } 117 118 struct adreno_gpulist { 119 const struct adreno_info *gpus; 120 unsigned gpus_count; 121 }; 122 123 #define DECLARE_ADRENO_GPULIST(name) \ 124 const struct adreno_gpulist name ## _gpulist = { \ 125 name ## _gpus, ARRAY_SIZE(name ## _gpus) \ 126 } 127 128 /* 129 * Helper to build a speedbin table, ie. the table: 130 * fuse | speedbin 131 * -----+--------- 132 * 0 | 0 133 * 169 | 1 134 * 174 | 2 135 * 136 * would be declared as: 137 * 138 * .speedbins = ADRENO_SPEEDBINS( 139 * { 0, 0 }, 140 * { 169, 1 }, 141 * { 174, 2 }, 142 * ), 143 */ 144 #define ADRENO_SPEEDBINS(tbl...) (struct adreno_speedbin[]) { tbl {SHRT_MAX, 0} } 145 146 struct adreno_protect { 147 const uint32_t *regs; 148 uint32_t count; 149 uint32_t count_max; 150 }; 151 152 #define DECLARE_ADRENO_PROTECT(name, __count_max) \ 153 static const struct adreno_protect name = { \ 154 .regs = name ## _regs, \ 155 .count = ARRAY_SIZE(name ## _regs), \ 156 .count_max = __count_max, \ 157 }; 158 159 struct adreno_gpu { 160 struct msm_gpu base; 161 const struct adreno_info *info; 162 uint32_t chip_id; 163 uint16_t speedbin; 164 const struct adreno_gpu_funcs *funcs; 165 166 /* interesting register offsets to dump: */ 167 const unsigned int *registers; 168 169 /* 170 * Are we loading fw from legacy path? Prior to addition 171 * of gpu firmware to linux-firmware, the fw files were 172 * placed in toplevel firmware directory, following qcom's 173 * android kernel. But linux-firmware preferred they be 174 * placed in a 'qcom' subdirectory. 175 * 176 * For backwards compatibility, we try first to load from 177 * the new path, using request_firmware_direct() to avoid 178 * any potential timeout waiting for usermode helper, then 179 * fall back to the old path (with direct load). And 180 * finally fall back to request_firmware() with the new 181 * path to allow the usermode helper. 182 */ 183 enum { 184 FW_LOCATION_UNKNOWN = 0, 185 FW_LOCATION_NEW, /* /lib/firmware/qcom/$fwfile */ 186 FW_LOCATION_LEGACY, /* /lib/firmware/$fwfile */ 187 FW_LOCATION_HELPER, 188 } fwloc; 189 190 /* firmware: */ 191 const struct firmware *fw[ADRENO_FW_MAX]; 192 193 struct { 194 u32 rgb565_predicator; 195 u32 uavflagprd_inv; 196 u32 min_acc_len; 197 u32 ubwc_mode; 198 u32 highest_bank_bit; 199 u32 amsbc; 200 } ubwc_config; 201 202 /* 203 * Register offsets are different between some GPUs. 204 * GPU specific offsets will be exported by GPU specific 205 * code (a3xx_gpu.c) and stored in this common location. 206 */ 207 const unsigned int *reg_offsets; 208 bool gmu_is_wrapper; 209 210 bool has_ray_tracing; 211 }; 212 #define to_adreno_gpu(x) container_of(x, struct adreno_gpu, base) 213 214 struct adreno_ocmem { 215 struct ocmem *ocmem; 216 unsigned long base; 217 void *hdl; 218 }; 219 220 /* platform config data (ie. from DT, or pdata) */ 221 struct adreno_platform_config { 222 uint32_t chip_id; 223 const struct adreno_info *info; 224 }; 225 226 #define ADRENO_IDLE_TIMEOUT msecs_to_jiffies(1000) 227 228 #define spin_until(X) ({ \ 229 int __ret = -ETIMEDOUT; \ 230 unsigned long __t = jiffies + ADRENO_IDLE_TIMEOUT; \ 231 do { \ 232 if (X) { \ 233 __ret = 0; \ 234 break; \ 235 } \ 236 } while (time_before(jiffies, __t)); \ 237 __ret; \ 238 }) 239 240 static inline uint8_t adreno_patchid(const struct adreno_gpu *gpu) 241 { 242 /* It is probably ok to assume legacy "adreno_rev" format 243 * for all a6xx devices, but probably best to limit this 244 * to older things. 245 */ 246 WARN_ON_ONCE(gpu->info->family >= ADRENO_6XX_GEN1); 247 return gpu->chip_id & 0xff; 248 } 249 250 static inline bool adreno_is_revn(const struct adreno_gpu *gpu, uint32_t revn) 251 { 252 if (WARN_ON_ONCE(!gpu->info)) 253 return false; 254 return gpu->info->revn == revn; 255 } 256 257 static inline bool adreno_has_gmu_wrapper(const struct adreno_gpu *gpu) 258 { 259 return gpu->gmu_is_wrapper; 260 } 261 262 static inline bool adreno_is_a2xx(const struct adreno_gpu *gpu) 263 { 264 if (WARN_ON_ONCE(!gpu->info)) 265 return false; 266 return gpu->info->family <= ADRENO_2XX_GEN2; 267 } 268 269 static inline bool adreno_is_a20x(const struct adreno_gpu *gpu) 270 { 271 if (WARN_ON_ONCE(!gpu->info)) 272 return false; 273 return gpu->info->family == ADRENO_2XX_GEN1; 274 } 275 276 static inline bool adreno_is_a225(const struct adreno_gpu *gpu) 277 { 278 return adreno_is_revn(gpu, 225); 279 } 280 281 static inline bool adreno_is_a305(const struct adreno_gpu *gpu) 282 { 283 return adreno_is_revn(gpu, 305); 284 } 285 286 static inline bool adreno_is_a305b(const struct adreno_gpu *gpu) 287 { 288 return gpu->info->chip_ids[0] == 0x03000512; 289 } 290 291 static inline bool adreno_is_a306(const struct adreno_gpu *gpu) 292 { 293 /* yes, 307, because a305c is 306 */ 294 return adreno_is_revn(gpu, 307); 295 } 296 297 static inline bool adreno_is_a320(const struct adreno_gpu *gpu) 298 { 299 return adreno_is_revn(gpu, 320); 300 } 301 302 static inline bool adreno_is_a330(const struct adreno_gpu *gpu) 303 { 304 return adreno_is_revn(gpu, 330); 305 } 306 307 static inline bool adreno_is_a330v2(const struct adreno_gpu *gpu) 308 { 309 return adreno_is_a330(gpu) && (adreno_patchid(gpu) > 0); 310 } 311 312 static inline int adreno_is_a405(const struct adreno_gpu *gpu) 313 { 314 return adreno_is_revn(gpu, 405); 315 } 316 317 static inline int adreno_is_a420(const struct adreno_gpu *gpu) 318 { 319 return adreno_is_revn(gpu, 420); 320 } 321 322 static inline int adreno_is_a430(const struct adreno_gpu *gpu) 323 { 324 return adreno_is_revn(gpu, 430); 325 } 326 327 static inline int adreno_is_a505(const struct adreno_gpu *gpu) 328 { 329 return adreno_is_revn(gpu, 505); 330 } 331 332 static inline int adreno_is_a506(const struct adreno_gpu *gpu) 333 { 334 return adreno_is_revn(gpu, 506); 335 } 336 337 static inline int adreno_is_a508(const struct adreno_gpu *gpu) 338 { 339 return adreno_is_revn(gpu, 508); 340 } 341 342 static inline int adreno_is_a509(const struct adreno_gpu *gpu) 343 { 344 return adreno_is_revn(gpu, 509); 345 } 346 347 static inline int adreno_is_a510(const struct adreno_gpu *gpu) 348 { 349 return adreno_is_revn(gpu, 510); 350 } 351 352 static inline int adreno_is_a512(const struct adreno_gpu *gpu) 353 { 354 return adreno_is_revn(gpu, 512); 355 } 356 357 static inline int adreno_is_a530(const struct adreno_gpu *gpu) 358 { 359 return adreno_is_revn(gpu, 530); 360 } 361 362 static inline int adreno_is_a540(const struct adreno_gpu *gpu) 363 { 364 return adreno_is_revn(gpu, 540); 365 } 366 367 static inline int adreno_is_a610(const struct adreno_gpu *gpu) 368 { 369 return adreno_is_revn(gpu, 610); 370 } 371 372 static inline int adreno_is_a618(const struct adreno_gpu *gpu) 373 { 374 return adreno_is_revn(gpu, 618); 375 } 376 377 static inline int adreno_is_a619(const struct adreno_gpu *gpu) 378 { 379 return adreno_is_revn(gpu, 619); 380 } 381 382 static inline int adreno_is_a619_holi(const struct adreno_gpu *gpu) 383 { 384 return adreno_is_a619(gpu) && adreno_has_gmu_wrapper(gpu); 385 } 386 387 static inline int adreno_is_a630(const struct adreno_gpu *gpu) 388 { 389 return adreno_is_revn(gpu, 630); 390 } 391 392 static inline int adreno_is_a640(const struct adreno_gpu *gpu) 393 { 394 return adreno_is_revn(gpu, 640); 395 } 396 397 static inline int adreno_is_a650(const struct adreno_gpu *gpu) 398 { 399 return adreno_is_revn(gpu, 650); 400 } 401 402 static inline int adreno_is_7c3(const struct adreno_gpu *gpu) 403 { 404 return gpu->info->chip_ids[0] == 0x06030500; 405 } 406 407 static inline int adreno_is_a660(const struct adreno_gpu *gpu) 408 { 409 return adreno_is_revn(gpu, 660); 410 } 411 412 static inline int adreno_is_a680(const struct adreno_gpu *gpu) 413 { 414 return adreno_is_revn(gpu, 680); 415 } 416 417 static inline int adreno_is_a690(const struct adreno_gpu *gpu) 418 { 419 return gpu->info->chip_ids[0] == 0x06090000; 420 } 421 422 static inline int adreno_is_a702(const struct adreno_gpu *gpu) 423 { 424 return gpu->info->chip_ids[0] == 0x07000200; 425 } 426 427 static inline int adreno_is_a610_family(const struct adreno_gpu *gpu) 428 { 429 if (WARN_ON_ONCE(!gpu->info)) 430 return false; 431 432 /* TODO: A612 */ 433 return adreno_is_a610(gpu) || adreno_is_a702(gpu); 434 } 435 436 /* check for a615, a616, a618, a619 or any a630 derivatives */ 437 static inline int adreno_is_a630_family(const struct adreno_gpu *gpu) 438 { 439 if (WARN_ON_ONCE(!gpu->info)) 440 return false; 441 return gpu->info->family == ADRENO_6XX_GEN1; 442 } 443 444 static inline int adreno_is_a660_family(const struct adreno_gpu *gpu) 445 { 446 if (WARN_ON_ONCE(!gpu->info)) 447 return false; 448 return gpu->info->family == ADRENO_6XX_GEN4; 449 } 450 451 /* check for a650, a660, or any derivatives */ 452 static inline int adreno_is_a650_family(const struct adreno_gpu *gpu) 453 { 454 if (WARN_ON_ONCE(!gpu->info)) 455 return false; 456 return gpu->info->family == ADRENO_6XX_GEN3 || 457 gpu->info->family == ADRENO_6XX_GEN4; 458 } 459 460 static inline int adreno_is_a640_family(const struct adreno_gpu *gpu) 461 { 462 if (WARN_ON_ONCE(!gpu->info)) 463 return false; 464 return gpu->info->family == ADRENO_6XX_GEN2; 465 } 466 467 static inline int adreno_is_a730(struct adreno_gpu *gpu) 468 { 469 return gpu->info->chip_ids[0] == 0x07030001; 470 } 471 472 static inline int adreno_is_a740(struct adreno_gpu *gpu) 473 { 474 return gpu->info->chip_ids[0] == 0x43050a01; 475 } 476 477 static inline int adreno_is_a750(struct adreno_gpu *gpu) 478 { 479 return gpu->info->chip_ids[0] == 0x43051401; 480 } 481 482 static inline int adreno_is_x185(struct adreno_gpu *gpu) 483 { 484 return gpu->info->chip_ids[0] == 0x43050c01; 485 } 486 487 static inline int adreno_is_a740_family(struct adreno_gpu *gpu) 488 { 489 if (WARN_ON_ONCE(!gpu->info)) 490 return false; 491 return gpu->info->family == ADRENO_7XX_GEN2 || 492 gpu->info->family == ADRENO_7XX_GEN3; 493 } 494 495 static inline int adreno_is_a7xx(struct adreno_gpu *gpu) 496 { 497 /* Update with non-fake (i.e. non-A702) Gen 7 GPUs */ 498 return gpu->info->family == ADRENO_7XX_GEN1 || 499 adreno_is_a740_family(gpu); 500 } 501 502 u64 adreno_private_address_space_size(struct msm_gpu *gpu); 503 int adreno_get_param(struct msm_gpu *gpu, struct msm_file_private *ctx, 504 uint32_t param, uint64_t *value, uint32_t *len); 505 int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx, 506 uint32_t param, uint64_t value, uint32_t len); 507 const struct firmware *adreno_request_fw(struct adreno_gpu *adreno_gpu, 508 const char *fwname); 509 struct drm_gem_object *adreno_fw_create_bo(struct msm_gpu *gpu, 510 const struct firmware *fw, u64 *iova); 511 int adreno_hw_init(struct msm_gpu *gpu); 512 void adreno_recover(struct msm_gpu *gpu); 513 void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring, u32 reg); 514 bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring); 515 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP) 516 void adreno_show(struct msm_gpu *gpu, struct msm_gpu_state *state, 517 struct drm_printer *p); 518 #endif 519 void adreno_dump_info(struct msm_gpu *gpu); 520 void adreno_dump(struct msm_gpu *gpu); 521 void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords); 522 struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu); 523 524 int adreno_gpu_ocmem_init(struct device *dev, struct adreno_gpu *adreno_gpu, 525 struct adreno_ocmem *ocmem); 526 void adreno_gpu_ocmem_cleanup(struct adreno_ocmem *ocmem); 527 528 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev, 529 struct adreno_gpu *gpu, const struct adreno_gpu_funcs *funcs, 530 int nr_rings); 531 void adreno_gpu_cleanup(struct adreno_gpu *gpu); 532 int adreno_load_fw(struct adreno_gpu *adreno_gpu); 533 534 void adreno_gpu_state_destroy(struct msm_gpu_state *state); 535 536 int adreno_gpu_state_get(struct msm_gpu *gpu, struct msm_gpu_state *state); 537 int adreno_gpu_state_put(struct msm_gpu_state *state); 538 void adreno_show_object(struct drm_printer *p, void **ptr, int len, 539 bool *encoded); 540 541 /* 542 * Common helper function to initialize the default address space for arm-smmu 543 * attached targets 544 */ 545 struct msm_gem_address_space * 546 adreno_create_address_space(struct msm_gpu *gpu, 547 struct platform_device *pdev); 548 549 struct msm_gem_address_space * 550 adreno_iommu_create_address_space(struct msm_gpu *gpu, 551 struct platform_device *pdev, 552 unsigned long quirks); 553 554 int adreno_fault_handler(struct msm_gpu *gpu, unsigned long iova, int flags, 555 struct adreno_smmu_fault_info *info, const char *block, 556 u32 scratch[4]); 557 558 int adreno_read_speedbin(struct device *dev, u32 *speedbin); 559 560 /* 561 * For a5xx and a6xx targets load the zap shader that is used to pull the GPU 562 * out of secure mode 563 */ 564 int adreno_zap_shader_load(struct msm_gpu *gpu, u32 pasid); 565 566 /* ringbuffer helpers (the parts that are adreno specific) */ 567 568 static inline void 569 OUT_PKT0(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt) 570 { 571 adreno_wait_ring(ring, cnt+1); 572 OUT_RING(ring, CP_TYPE0_PKT | ((cnt-1) << 16) | (regindx & 0x7FFF)); 573 } 574 575 /* no-op packet: */ 576 static inline void 577 OUT_PKT2(struct msm_ringbuffer *ring) 578 { 579 adreno_wait_ring(ring, 1); 580 OUT_RING(ring, CP_TYPE2_PKT); 581 } 582 583 static inline void 584 OUT_PKT3(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt) 585 { 586 adreno_wait_ring(ring, cnt+1); 587 OUT_RING(ring, CP_TYPE3_PKT | ((cnt-1) << 16) | ((opcode & 0xFF) << 8)); 588 } 589 590 static inline u32 PM4_PARITY(u32 val) 591 { 592 return (0x9669 >> (0xF & (val ^ 593 (val >> 4) ^ (val >> 8) ^ (val >> 12) ^ 594 (val >> 16) ^ ((val) >> 20) ^ (val >> 24) ^ 595 (val >> 28)))) & 1; 596 } 597 598 /* Maximum number of values that can be executed for one opcode */ 599 #define TYPE4_MAX_PAYLOAD 127 600 601 #define PKT4(_reg, _cnt) \ 602 (CP_TYPE4_PKT | ((_cnt) << 0) | (PM4_PARITY((_cnt)) << 7) | \ 603 (((_reg) & 0x3FFFF) << 8) | (PM4_PARITY((_reg)) << 27)) 604 605 static inline void 606 OUT_PKT4(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt) 607 { 608 adreno_wait_ring(ring, cnt + 1); 609 OUT_RING(ring, PKT4(regindx, cnt)); 610 } 611 612 static inline void 613 OUT_PKT7(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt) 614 { 615 adreno_wait_ring(ring, cnt + 1); 616 OUT_RING(ring, CP_TYPE7_PKT | (cnt << 0) | (PM4_PARITY(cnt) << 15) | 617 ((opcode & 0x7F) << 16) | (PM4_PARITY(opcode) << 23)); 618 } 619 620 struct msm_gpu *a2xx_gpu_init(struct drm_device *dev); 621 struct msm_gpu *a3xx_gpu_init(struct drm_device *dev); 622 struct msm_gpu *a4xx_gpu_init(struct drm_device *dev); 623 struct msm_gpu *a5xx_gpu_init(struct drm_device *dev); 624 struct msm_gpu *a6xx_gpu_init(struct drm_device *dev); 625 626 static inline uint32_t get_wptr(struct msm_ringbuffer *ring) 627 { 628 return (ring->cur - ring->start) % (MSM_GPU_RINGBUFFER_SZ >> 2); 629 } 630 631 /* 632 * Given a register and a count, return a value to program into 633 * REG_CP_PROTECT_REG(n) - this will block both reads and writes for _len 634 * registers starting at _reg. 635 * 636 * The register base needs to be a multiple of the length. If it is not, the 637 * hardware will quietly mask off the bits for you and shift the size. For 638 * example, if you intend the protection to start at 0x07 for a length of 4 639 * (0x07-0x0A) the hardware will actually protect (0x04-0x07) which might 640 * expose registers you intended to protect! 641 */ 642 #define ADRENO_PROTECT_RW(_reg, _len) \ 643 ((1 << 30) | (1 << 29) | \ 644 ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF)) 645 646 /* 647 * Same as above, but allow reads over the range. For areas of mixed use (such 648 * as performance counters) this allows us to protect a much larger range with a 649 * single register 650 */ 651 #define ADRENO_PROTECT_RDONLY(_reg, _len) \ 652 ((1 << 29) \ 653 ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF)) 654 655 656 #define gpu_poll_timeout(gpu, addr, val, cond, interval, timeout) \ 657 readl_poll_timeout((gpu)->mmio + ((addr) << 2), val, cond, \ 658 interval, timeout) 659 660 #endif /* __ADRENO_GPU_H__ */ 661