1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2013 Red Hat
6 * Copyright (c) 2014,2017, 2019 The Linux Foundation. All rights reserved.
9 #ifndef __ADRENO_GPU_H__
10 #define __ADRENO_GPU_H__
12 #include <linux/firmware.h>
13 #include <linux/iopoll.h>
17 #include "adreno_common.xml.h"
18 #include "adreno_pm4.xml.h"
20 extern bool snapshot_debugbus;
21 extern bool allow_vram_carveout;
25 ADRENO_FW_SQE = 0, /* a6xx */
27 ADRENO_FW_GMU = 1, /* a6xx */
33 * @enum adreno_family: identify generation and possibly sub-generation
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.
40 ADRENO_2XX_GEN1, /* a20x */
41 ADRENO_2XX_GEN2, /* a22x */
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 */
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 #define ADRENO_QUIRK_PREEMPTION BIT(5)
61 /* Helper for formating the chip_id in the way that userspace tools like
64 #define ADRENO_CHIPID_FMT "u.%u.%u.%u"
65 #define ADRENO_CHIPID_ARGS(_c) \
66 (((_c) >> 24) & 0xff), \
67 (((_c) >> 16) & 0xff), \
68 (((_c) >> 8) & 0xff), \
71 struct adreno_gpu_funcs {
72 struct msm_gpu_funcs base;
73 int (*get_timestamp)(struct msm_gpu *gpu, uint64_t *value);
76 struct adreno_reglist {
81 struct adreno_speedbin {
91 * @chipids: Table of matching chip-ids
93 * Terminated with 0 sentinal
96 enum adreno_family family;
98 const char *fw[ADRENO_FW_MAX];
101 struct msm_gpu *(*init)(struct drm_device *dev);
105 const struct a6xx_info *a6xx;
107 u64 address_space_size;
109 * @speedbins: Optional table of fuse to speedbin mappings
111 * Consists of pairs of fuse, index mappings, terminated with
112 * {SHRT_MAX, 0} sentinal.
114 struct adreno_speedbin *speedbins;
115 u64 preempt_record_size;
118 #define ADRENO_CHIP_IDS(tbl...) (uint32_t[]) { tbl, 0 }
120 struct adreno_gpulist {
121 const struct adreno_info *gpus;
125 #define DECLARE_ADRENO_GPULIST(name) \
126 const struct adreno_gpulist name ## _gpulist = { \
127 name ## _gpus, ARRAY_SIZE(name ## _gpus) \
131 * Helper to build a speedbin table, ie. the table:
138 * would be declared as:
140 * .speedbins = ADRENO_SPEEDBINS(
146 #define ADRENO_SPEEDBINS(tbl...) (struct adreno_speedbin[]) { tbl {SHRT_MAX, 0} }
148 struct adreno_protect {
149 const uint32_t *regs;
154 #define DECLARE_ADRENO_PROTECT(name, __count_max) \
155 static const struct adreno_protect name = { \
156 .regs = name ## _regs, \
157 .count = ARRAY_SIZE(name ## _regs), \
158 .count_max = __count_max, \
161 struct adreno_reglist_list {
162 /** @reg: List of register **/
164 /** @count: Number of registers in the list **/
168 #define DECLARE_ADRENO_REGLIST_LIST(name) \
169 static const struct adreno_reglist_list name = { \
170 .regs = name ## _regs, \
171 .count = ARRAY_SIZE(name ## _regs), \
176 const struct adreno_info *info;
179 const struct adreno_gpu_funcs *funcs;
181 /* interesting register offsets to dump: */
182 const unsigned int *registers;
185 * Are we loading fw from legacy path? Prior to addition
186 * of gpu firmware to linux-firmware, the fw files were
187 * placed in toplevel firmware directory, following qcom's
188 * android kernel. But linux-firmware preferred they be
189 * placed in a 'qcom' subdirectory.
191 * For backwards compatibility, we try first to load from
192 * the new path, using request_firmware_direct() to avoid
193 * any potential timeout waiting for usermode helper, then
194 * fall back to the old path (with direct load). And
195 * finally fall back to request_firmware() with the new
196 * path to allow the usermode helper.
199 FW_LOCATION_UNKNOWN = 0,
200 FW_LOCATION_NEW, /* /lib/firmware/qcom/$fwfile */
201 FW_LOCATION_LEGACY, /* /lib/firmware/$fwfile */
206 const struct firmware *fw[ADRENO_FW_MAX];
210 * @rgb565_predicator: Unknown, introduced with A650 family,
211 * related to UBWC mode/ver 4
213 u32 rgb565_predicator;
214 /** @uavflagprd_inv: Unknown, introduced with A650 family */
216 /** @min_acc_len: Whether the minimum access length is 64 bits */
219 * @ubwc_swizzle: Whether to enable level 1, 2 & 3 bank swizzling.
221 * UBWC 1.0 always enables all three levels.
222 * UBWC 2.0 removes level 1 bank swizzling, leaving levels 2 & 3.
223 * UBWC 4.0 adds the optional ability to disable levels 2 & 3.
225 * This is a bitmask where BIT(0) enables level 1, BIT(1)
226 * controls level 2, and BIT(2) enables level 3.
230 * @highest_bank_bit: Highest Bank Bit
232 * The Highest Bank Bit value represents the bit of the highest
233 * DDR bank. This should ideally use DRAM type detection.
235 u32 highest_bank_bit;
238 * @macrotile_mode: Macrotile Mode
240 * Whether to use 4-channel macrotiling mode or the newer
241 * 8-channel macrotiling mode introduced in UBWC 3.1. 0 is
242 * 4-channel and 1 is 8-channel.
248 * Register offsets are different between some GPUs.
249 * GPU specific offsets will be exported by GPU specific
250 * code (a3xx_gpu.c) and stored in this common location.
252 const unsigned int *reg_offsets;
255 bool has_ray_tracing;
259 #define to_adreno_gpu(x) container_of(x, struct adreno_gpu, base)
261 struct adreno_ocmem {
267 /* platform config data (ie. from DT, or pdata) */
268 struct adreno_platform_config {
270 const struct adreno_info *info;
273 #define ADRENO_IDLE_TIMEOUT msecs_to_jiffies(1000)
275 #define spin_until(X) ({ \
276 int __ret = -ETIMEDOUT; \
277 unsigned long __t = jiffies + ADRENO_IDLE_TIMEOUT; \
283 } while (time_before(jiffies, __t)); \
287 static inline uint8_t adreno_patchid(const struct adreno_gpu *gpu)
289 /* It is probably ok to assume legacy "adreno_rev" format
290 * for all a6xx devices, but probably best to limit this
293 WARN_ON_ONCE(gpu->info->family >= ADRENO_6XX_GEN1);
294 return gpu->chip_id & 0xff;
297 static inline bool adreno_is_revn(const struct adreno_gpu *gpu, uint32_t revn)
299 if (WARN_ON_ONCE(!gpu->info))
301 return gpu->info->revn == revn;
304 static inline bool adreno_has_gmu_wrapper(const struct adreno_gpu *gpu)
306 return gpu->gmu_is_wrapper;
309 static inline bool adreno_is_a2xx(const struct adreno_gpu *gpu)
311 if (WARN_ON_ONCE(!gpu->info))
313 return gpu->info->family <= ADRENO_2XX_GEN2;
316 static inline bool adreno_is_a20x(const struct adreno_gpu *gpu)
318 if (WARN_ON_ONCE(!gpu->info))
320 return gpu->info->family == ADRENO_2XX_GEN1;
323 static inline bool adreno_is_a225(const struct adreno_gpu *gpu)
325 return adreno_is_revn(gpu, 225);
328 static inline bool adreno_is_a305(const struct adreno_gpu *gpu)
330 return adreno_is_revn(gpu, 305);
333 static inline bool adreno_is_a305b(const struct adreno_gpu *gpu)
335 return gpu->info->chip_ids[0] == 0x03000512;
338 static inline bool adreno_is_a306(const struct adreno_gpu *gpu)
340 /* yes, 307, because a305c is 306 */
341 return adreno_is_revn(gpu, 307);
344 static inline bool adreno_is_a306a(const struct adreno_gpu *gpu)
346 /* a306a (marketing name is a308) */
347 return adreno_is_revn(gpu, 308);
350 static inline bool adreno_is_a320(const struct adreno_gpu *gpu)
352 return adreno_is_revn(gpu, 320);
355 static inline bool adreno_is_a330(const struct adreno_gpu *gpu)
357 return adreno_is_revn(gpu, 330);
360 static inline bool adreno_is_a330v2(const struct adreno_gpu *gpu)
362 return adreno_is_a330(gpu) && (adreno_patchid(gpu) > 0);
365 static inline int adreno_is_a405(const struct adreno_gpu *gpu)
367 return adreno_is_revn(gpu, 405);
370 static inline int adreno_is_a420(const struct adreno_gpu *gpu)
372 return adreno_is_revn(gpu, 420);
375 static inline int adreno_is_a430(const struct adreno_gpu *gpu)
377 return adreno_is_revn(gpu, 430);
380 static inline int adreno_is_a505(const struct adreno_gpu *gpu)
382 return adreno_is_revn(gpu, 505);
385 static inline int adreno_is_a506(const struct adreno_gpu *gpu)
387 return adreno_is_revn(gpu, 506);
390 static inline int adreno_is_a508(const struct adreno_gpu *gpu)
392 return adreno_is_revn(gpu, 508);
395 static inline int adreno_is_a509(const struct adreno_gpu *gpu)
397 return adreno_is_revn(gpu, 509);
400 static inline int adreno_is_a510(const struct adreno_gpu *gpu)
402 return adreno_is_revn(gpu, 510);
405 static inline int adreno_is_a512(const struct adreno_gpu *gpu)
407 return adreno_is_revn(gpu, 512);
410 static inline int adreno_is_a530(const struct adreno_gpu *gpu)
412 return adreno_is_revn(gpu, 530);
415 static inline int adreno_is_a540(const struct adreno_gpu *gpu)
417 return adreno_is_revn(gpu, 540);
420 static inline int adreno_is_a610(const struct adreno_gpu *gpu)
422 return adreno_is_revn(gpu, 610);
425 static inline int adreno_is_a618(const struct adreno_gpu *gpu)
427 return adreno_is_revn(gpu, 618);
430 static inline int adreno_is_a619(const struct adreno_gpu *gpu)
432 return adreno_is_revn(gpu, 619);
435 static inline int adreno_is_a619_holi(const struct adreno_gpu *gpu)
437 return adreno_is_a619(gpu) && adreno_has_gmu_wrapper(gpu);
440 static inline int adreno_is_a621(const struct adreno_gpu *gpu)
442 return gpu->info->chip_ids[0] == 0x06020100;
445 static inline int adreno_is_a630(const struct adreno_gpu *gpu)
447 return adreno_is_revn(gpu, 630);
450 static inline int adreno_is_a640(const struct adreno_gpu *gpu)
452 return adreno_is_revn(gpu, 640);
455 static inline int adreno_is_a650(const struct adreno_gpu *gpu)
457 return adreno_is_revn(gpu, 650);
460 static inline int adreno_is_7c3(const struct adreno_gpu *gpu)
462 return gpu->info->chip_ids[0] == 0x06030500;
465 static inline int adreno_is_a660(const struct adreno_gpu *gpu)
467 return adreno_is_revn(gpu, 660);
470 static inline int adreno_is_a680(const struct adreno_gpu *gpu)
472 return adreno_is_revn(gpu, 680);
475 static inline int adreno_is_a663(const struct adreno_gpu *gpu)
477 return gpu->info->chip_ids[0] == 0x06060300;
480 static inline int adreno_is_a690(const struct adreno_gpu *gpu)
482 return gpu->info->chip_ids[0] == 0x06090000;
485 static inline int adreno_is_a702(const struct adreno_gpu *gpu)
487 return gpu->info->chip_ids[0] == 0x07000200;
490 static inline int adreno_is_a610_family(const struct adreno_gpu *gpu)
492 if (WARN_ON_ONCE(!gpu->info))
496 return adreno_is_a610(gpu) || adreno_is_a702(gpu);
500 static inline int adreno_is_a615_family(const struct adreno_gpu *gpu)
502 return adreno_is_a618(gpu) ||
506 static inline int adreno_is_a630_family(const struct adreno_gpu *gpu)
508 if (WARN_ON_ONCE(!gpu->info))
510 return gpu->info->family == ADRENO_6XX_GEN1;
513 static inline int adreno_is_a660_family(const struct adreno_gpu *gpu)
515 if (WARN_ON_ONCE(!gpu->info))
517 return gpu->info->family == ADRENO_6XX_GEN4;
520 /* check for a650, a660, or any derivatives */
521 static inline int adreno_is_a650_family(const struct adreno_gpu *gpu)
523 if (WARN_ON_ONCE(!gpu->info))
525 return gpu->info->family == ADRENO_6XX_GEN3 ||
526 gpu->info->family == ADRENO_6XX_GEN4;
529 static inline int adreno_is_a640_family(const struct adreno_gpu *gpu)
531 if (WARN_ON_ONCE(!gpu->info))
533 return gpu->info->family == ADRENO_6XX_GEN2;
536 static inline int adreno_is_a730(struct adreno_gpu *gpu)
538 return gpu->info->chip_ids[0] == 0x07030001;
541 static inline int adreno_is_a740(struct adreno_gpu *gpu)
543 return gpu->info->chip_ids[0] == 0x43050a01;
546 static inline int adreno_is_a750(struct adreno_gpu *gpu)
548 return gpu->info->chip_ids[0] == 0x43051401;
551 static inline int adreno_is_x185(struct adreno_gpu *gpu)
553 return gpu->info->chip_ids[0] == 0x43050c01;
556 static inline int adreno_is_a740_family(struct adreno_gpu *gpu)
558 if (WARN_ON_ONCE(!gpu->info))
560 return gpu->info->family == ADRENO_7XX_GEN2 ||
561 gpu->info->family == ADRENO_7XX_GEN3;
564 static inline int adreno_is_a750_family(struct adreno_gpu *gpu)
566 return gpu->info->family == ADRENO_7XX_GEN3;
569 static inline int adreno_is_a7xx(struct adreno_gpu *gpu)
571 /* Update with non-fake (i.e. non-A702) Gen 7 GPUs */
572 return gpu->info->family == ADRENO_7XX_GEN1 ||
573 adreno_is_a740_family(gpu);
576 u64 adreno_private_address_space_size(struct msm_gpu *gpu);
577 int adreno_get_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
578 uint32_t param, uint64_t *value, uint32_t *len);
579 int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
580 uint32_t param, uint64_t value, uint32_t len);
581 const struct firmware *adreno_request_fw(struct adreno_gpu *adreno_gpu,
583 struct drm_gem_object *adreno_fw_create_bo(struct msm_gpu *gpu,
584 const struct firmware *fw, u64 *iova);
585 int adreno_hw_init(struct msm_gpu *gpu);
586 void adreno_recover(struct msm_gpu *gpu);
587 void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring, u32 reg);
588 bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
589 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
590 void adreno_show(struct msm_gpu *gpu, struct msm_gpu_state *state,
591 struct drm_printer *p);
593 void adreno_dump_info(struct msm_gpu *gpu);
594 void adreno_dump(struct msm_gpu *gpu);
595 void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords);
596 struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu);
598 int adreno_gpu_ocmem_init(struct device *dev, struct adreno_gpu *adreno_gpu,
599 struct adreno_ocmem *ocmem);
600 void adreno_gpu_ocmem_cleanup(struct adreno_ocmem *ocmem);
602 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
603 struct adreno_gpu *gpu, const struct adreno_gpu_funcs *funcs,
605 void adreno_gpu_cleanup(struct adreno_gpu *gpu);
606 int adreno_load_fw(struct adreno_gpu *adreno_gpu);
608 void adreno_gpu_state_destroy(struct msm_gpu_state *state);
610 int adreno_gpu_state_get(struct msm_gpu *gpu, struct msm_gpu_state *state);
611 int adreno_gpu_state_put(struct msm_gpu_state *state);
612 void adreno_show_object(struct drm_printer *p, void **ptr, int len,
616 * Common helper function to initialize the default address space for arm-smmu
619 struct msm_gem_address_space *
620 adreno_create_address_space(struct msm_gpu *gpu,
621 struct platform_device *pdev);
623 struct msm_gem_address_space *
624 adreno_iommu_create_address_space(struct msm_gpu *gpu,
625 struct platform_device *pdev,
626 unsigned long quirks);
628 int adreno_fault_handler(struct msm_gpu *gpu, unsigned long iova, int flags,
629 struct adreno_smmu_fault_info *info, const char *block,
632 int adreno_read_speedbin(struct device *dev, u32 *speedbin);
635 * For a5xx and a6xx targets load the zap shader that is used to pull the GPU
638 int adreno_zap_shader_load(struct msm_gpu *gpu, u32 pasid);
640 /* ringbuffer helpers (the parts that are adreno specific) */
643 OUT_PKT0(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt)
645 adreno_wait_ring(ring, cnt+1);
646 OUT_RING(ring, CP_TYPE0_PKT | ((cnt-1) << 16) | (regindx & 0x7FFF));
651 OUT_PKT2(struct msm_ringbuffer *ring)
653 adreno_wait_ring(ring, 1);
654 OUT_RING(ring, CP_TYPE2_PKT);
658 OUT_PKT3(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt)
660 adreno_wait_ring(ring, cnt+1);
661 OUT_RING(ring, CP_TYPE3_PKT | ((cnt-1) << 16) | ((opcode & 0xFF) << 8));
664 static inline u32 PM4_PARITY(u32 val)
666 return (0x9669 >> (0xF & (val ^
667 (val >> 4) ^ (val >> 8) ^ (val >> 12) ^
668 (val >> 16) ^ ((val) >> 20) ^ (val >> 24) ^
672 /* Maximum number of values that can be executed for one opcode */
673 #define TYPE4_MAX_PAYLOAD 127
675 #define PKT4(_reg, _cnt) \
676 (CP_TYPE4_PKT | ((_cnt) << 0) | (PM4_PARITY((_cnt)) << 7) | \
677 (((_reg) & 0x3FFFF) << 8) | (PM4_PARITY((_reg)) << 27))
680 OUT_PKT4(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt)
682 adreno_wait_ring(ring, cnt + 1);
683 OUT_RING(ring, PKT4(regindx, cnt));
686 #define PKT7(opcode, cnt) \
687 (CP_TYPE7_PKT | (cnt << 0) | (PM4_PARITY(cnt) << 15) | \
688 ((opcode & 0x7F) << 16) | (PM4_PARITY(opcode) << 23))
691 OUT_PKT7(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt)
693 adreno_wait_ring(ring, cnt + 1);
694 OUT_RING(ring, PKT7(opcode, cnt));
697 struct msm_gpu *a2xx_gpu_init(struct drm_device *dev);
698 struct msm_gpu *a3xx_gpu_init(struct drm_device *dev);
699 struct msm_gpu *a4xx_gpu_init(struct drm_device *dev);
700 struct msm_gpu *a5xx_gpu_init(struct drm_device *dev);
701 struct msm_gpu *a6xx_gpu_init(struct drm_device *dev);
703 static inline uint32_t get_wptr(struct msm_ringbuffer *ring)
705 return (ring->cur - ring->start) % (MSM_GPU_RINGBUFFER_SZ >> 2);
709 * Given a register and a count, return a value to program into
710 * REG_CP_PROTECT_REG(n) - this will block both reads and writes for _len
711 * registers starting at _reg.
713 * The register base needs to be a multiple of the length. If it is not, the
714 * hardware will quietly mask off the bits for you and shift the size. For
715 * example, if you intend the protection to start at 0x07 for a length of 4
716 * (0x07-0x0A) the hardware will actually protect (0x04-0x07) which might
717 * expose registers you intended to protect!
719 #define ADRENO_PROTECT_RW(_reg, _len) \
720 ((1 << 30) | (1 << 29) | \
721 ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF))
724 * Same as above, but allow reads over the range. For areas of mixed use (such
725 * as performance counters) this allows us to protect a much larger range with a
728 #define ADRENO_PROTECT_RDONLY(_reg, _len) \
730 ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF))
733 #define gpu_poll_timeout(gpu, addr, val, cond, interval, timeout) \
734 readl_poll_timeout((gpu)->mmio + ((addr) << 2), val, cond, \
737 #endif /* __ADRENO_GPU_H__ */