adreno_gpu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * Copyright (c) 2014,2017 The Linux Foundation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __ADRENO_GPU_H__
  20. #define __ADRENO_GPU_H__
  21. #include <linux/firmware.h>
  22. #include "msm_gpu.h"
  23. #include "adreno_common.xml.h"
  24. #include "adreno_pm4.xml.h"
  25. #define REG_ADRENO_DEFINE(_offset, _reg) [_offset] = (_reg) + 1
  26. #define REG_SKIP ~0
  27. #define REG_ADRENO_SKIP(_offset) [_offset] = REG_SKIP
  28. /**
  29. * adreno_regs: List of registers that are used in across all
  30. * 3D devices. Each device type has different offset value for the same
  31. * register, so an array of register offsets are declared for every device
  32. * and are indexed by the enumeration values defined in this enum
  33. */
  34. enum adreno_regs {
  35. REG_ADRENO_CP_RB_BASE,
  36. REG_ADRENO_CP_RB_BASE_HI,
  37. REG_ADRENO_CP_RB_RPTR_ADDR,
  38. REG_ADRENO_CP_RB_RPTR_ADDR_HI,
  39. REG_ADRENO_CP_RB_RPTR,
  40. REG_ADRENO_CP_RB_WPTR,
  41. REG_ADRENO_CP_RB_CNTL,
  42. REG_ADRENO_REGISTER_MAX,
  43. };
  44. enum {
  45. ADRENO_FW_PM4 = 0,
  46. ADRENO_FW_SQE = 0, /* a6xx */
  47. ADRENO_FW_PFP = 1,
  48. ADRENO_FW_GMU = 1, /* a6xx */
  49. ADRENO_FW_GPMU = 2,
  50. ADRENO_FW_MAX,
  51. };
  52. enum adreno_quirks {
  53. ADRENO_QUIRK_TWO_PASS_USE_WFI = 1,
  54. ADRENO_QUIRK_FAULT_DETECT_MASK = 2,
  55. };
  56. struct adreno_rev {
  57. uint8_t core;
  58. uint8_t major;
  59. uint8_t minor;
  60. uint8_t patchid;
  61. };
  62. #define ADRENO_REV(core, major, minor, patchid) \
  63. ((struct adreno_rev){ core, major, minor, patchid })
  64. struct adreno_gpu_funcs {
  65. struct msm_gpu_funcs base;
  66. int (*get_timestamp)(struct msm_gpu *gpu, uint64_t *value);
  67. };
  68. struct adreno_info {
  69. struct adreno_rev rev;
  70. uint32_t revn;
  71. const char *name;
  72. const char *fw[ADRENO_FW_MAX];
  73. uint32_t gmem;
  74. enum adreno_quirks quirks;
  75. struct msm_gpu *(*init)(struct drm_device *dev);
  76. const char *zapfw;
  77. u32 inactive_period;
  78. };
  79. const struct adreno_info *adreno_info(struct adreno_rev rev);
  80. struct adreno_gpu {
  81. struct msm_gpu base;
  82. struct adreno_rev rev;
  83. const struct adreno_info *info;
  84. uint32_t gmem; /* actual gmem size */
  85. uint32_t revn; /* numeric revision name */
  86. const struct adreno_gpu_funcs *funcs;
  87. /* interesting register offsets to dump: */
  88. const unsigned int *registers;
  89. /*
  90. * Are we loading fw from legacy path? Prior to addition
  91. * of gpu firmware to linux-firmware, the fw files were
  92. * placed in toplevel firmware directory, following qcom's
  93. * android kernel. But linux-firmware preferred they be
  94. * placed in a 'qcom' subdirectory.
  95. *
  96. * For backwards compatibility, we try first to load from
  97. * the new path, using request_firmware_direct() to avoid
  98. * any potential timeout waiting for usermode helper, then
  99. * fall back to the old path (with direct load). And
  100. * finally fall back to request_firmware() with the new
  101. * path to allow the usermode helper.
  102. */
  103. enum {
  104. FW_LOCATION_UNKNOWN = 0,
  105. FW_LOCATION_NEW, /* /lib/firmware/qcom/$fwfile */
  106. FW_LOCATION_LEGACY, /* /lib/firmware/$fwfile */
  107. FW_LOCATION_HELPER,
  108. } fwloc;
  109. /* firmware: */
  110. const struct firmware *fw[ADRENO_FW_MAX];
  111. /*
  112. * Register offsets are different between some GPUs.
  113. * GPU specific offsets will be exported by GPU specific
  114. * code (a3xx_gpu.c) and stored in this common location.
  115. */
  116. const unsigned int *reg_offsets;
  117. };
  118. #define to_adreno_gpu(x) container_of(x, struct adreno_gpu, base)
  119. /* platform config data (ie. from DT, or pdata) */
  120. struct adreno_platform_config {
  121. struct adreno_rev rev;
  122. };
  123. #define ADRENO_IDLE_TIMEOUT msecs_to_jiffies(1000)
  124. #define spin_until(X) ({ \
  125. int __ret = -ETIMEDOUT; \
  126. unsigned long __t = jiffies + ADRENO_IDLE_TIMEOUT; \
  127. do { \
  128. if (X) { \
  129. __ret = 0; \
  130. break; \
  131. } \
  132. } while (time_before(jiffies, __t)); \
  133. __ret; \
  134. })
  135. static inline bool adreno_is_a3xx(struct adreno_gpu *gpu)
  136. {
  137. return (gpu->revn >= 300) && (gpu->revn < 400);
  138. }
  139. static inline bool adreno_is_a305(struct adreno_gpu *gpu)
  140. {
  141. return gpu->revn == 305;
  142. }
  143. static inline bool adreno_is_a306(struct adreno_gpu *gpu)
  144. {
  145. /* yes, 307, because a305c is 306 */
  146. return gpu->revn == 307;
  147. }
  148. static inline bool adreno_is_a320(struct adreno_gpu *gpu)
  149. {
  150. return gpu->revn == 320;
  151. }
  152. static inline bool adreno_is_a330(struct adreno_gpu *gpu)
  153. {
  154. return gpu->revn == 330;
  155. }
  156. static inline bool adreno_is_a330v2(struct adreno_gpu *gpu)
  157. {
  158. return adreno_is_a330(gpu) && (gpu->rev.patchid > 0);
  159. }
  160. static inline bool adreno_is_a4xx(struct adreno_gpu *gpu)
  161. {
  162. return (gpu->revn >= 400) && (gpu->revn < 500);
  163. }
  164. static inline int adreno_is_a420(struct adreno_gpu *gpu)
  165. {
  166. return gpu->revn == 420;
  167. }
  168. static inline int adreno_is_a430(struct adreno_gpu *gpu)
  169. {
  170. return gpu->revn == 430;
  171. }
  172. static inline int adreno_is_a530(struct adreno_gpu *gpu)
  173. {
  174. return gpu->revn == 530;
  175. }
  176. int adreno_get_param(struct msm_gpu *gpu, uint32_t param, uint64_t *value);
  177. const struct firmware *adreno_request_fw(struct adreno_gpu *adreno_gpu,
  178. const char *fwname);
  179. struct drm_gem_object *adreno_fw_create_bo(struct msm_gpu *gpu,
  180. const struct firmware *fw, u64 *iova);
  181. int adreno_hw_init(struct msm_gpu *gpu);
  182. void adreno_recover(struct msm_gpu *gpu);
  183. void adreno_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  184. struct msm_file_private *ctx);
  185. void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
  186. bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
  187. #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
  188. void adreno_show(struct msm_gpu *gpu, struct msm_gpu_state *state,
  189. struct drm_printer *p);
  190. #endif
  191. void adreno_dump_info(struct msm_gpu *gpu);
  192. void adreno_dump(struct msm_gpu *gpu);
  193. void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords);
  194. struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu);
  195. int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  196. struct adreno_gpu *gpu, const struct adreno_gpu_funcs *funcs,
  197. int nr_rings);
  198. void adreno_gpu_cleanup(struct adreno_gpu *gpu);
  199. int adreno_load_fw(struct adreno_gpu *adreno_gpu);
  200. void adreno_gpu_state_destroy(struct msm_gpu_state *state);
  201. int adreno_gpu_state_get(struct msm_gpu *gpu, struct msm_gpu_state *state);
  202. int adreno_gpu_state_put(struct msm_gpu_state *state);
  203. /* ringbuffer helpers (the parts that are adreno specific) */
  204. static inline void
  205. OUT_PKT0(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt)
  206. {
  207. adreno_wait_ring(ring, cnt+1);
  208. OUT_RING(ring, CP_TYPE0_PKT | ((cnt-1) << 16) | (regindx & 0x7FFF));
  209. }
  210. /* no-op packet: */
  211. static inline void
  212. OUT_PKT2(struct msm_ringbuffer *ring)
  213. {
  214. adreno_wait_ring(ring, 1);
  215. OUT_RING(ring, CP_TYPE2_PKT);
  216. }
  217. static inline void
  218. OUT_PKT3(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt)
  219. {
  220. adreno_wait_ring(ring, cnt+1);
  221. OUT_RING(ring, CP_TYPE3_PKT | ((cnt-1) << 16) | ((opcode & 0xFF) << 8));
  222. }
  223. static inline u32 PM4_PARITY(u32 val)
  224. {
  225. return (0x9669 >> (0xF & (val ^
  226. (val >> 4) ^ (val >> 8) ^ (val >> 12) ^
  227. (val >> 16) ^ ((val) >> 20) ^ (val >> 24) ^
  228. (val >> 28)))) & 1;
  229. }
  230. /* Maximum number of values that can be executed for one opcode */
  231. #define TYPE4_MAX_PAYLOAD 127
  232. #define PKT4(_reg, _cnt) \
  233. (CP_TYPE4_PKT | ((_cnt) << 0) | (PM4_PARITY((_cnt)) << 7) | \
  234. (((_reg) & 0x3FFFF) << 8) | (PM4_PARITY((_reg)) << 27))
  235. static inline void
  236. OUT_PKT4(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt)
  237. {
  238. adreno_wait_ring(ring, cnt + 1);
  239. OUT_RING(ring, PKT4(regindx, cnt));
  240. }
  241. static inline void
  242. OUT_PKT7(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt)
  243. {
  244. adreno_wait_ring(ring, cnt + 1);
  245. OUT_RING(ring, CP_TYPE7_PKT | (cnt << 0) | (PM4_PARITY(cnt) << 15) |
  246. ((opcode & 0x7F) << 16) | (PM4_PARITY(opcode) << 23));
  247. }
  248. /*
  249. * adreno_reg_check() - Checks the validity of a register enum
  250. * @gpu: Pointer to struct adreno_gpu
  251. * @offset_name: The register enum that is checked
  252. */
  253. static inline bool adreno_reg_check(struct adreno_gpu *gpu,
  254. enum adreno_regs offset_name)
  255. {
  256. if (offset_name >= REG_ADRENO_REGISTER_MAX ||
  257. !gpu->reg_offsets[offset_name]) {
  258. BUG();
  259. }
  260. /*
  261. * REG_SKIP is a special value that tell us that the register in
  262. * question isn't implemented on target but don't trigger a BUG(). This
  263. * is used to cleanly implement adreno_gpu_write64() and
  264. * adreno_gpu_read64() in a generic fashion
  265. */
  266. if (gpu->reg_offsets[offset_name] == REG_SKIP)
  267. return false;
  268. return true;
  269. }
  270. static inline u32 adreno_gpu_read(struct adreno_gpu *gpu,
  271. enum adreno_regs offset_name)
  272. {
  273. u32 reg = gpu->reg_offsets[offset_name];
  274. u32 val = 0;
  275. if(adreno_reg_check(gpu,offset_name))
  276. val = gpu_read(&gpu->base, reg - 1);
  277. return val;
  278. }
  279. static inline void adreno_gpu_write(struct adreno_gpu *gpu,
  280. enum adreno_regs offset_name, u32 data)
  281. {
  282. u32 reg = gpu->reg_offsets[offset_name];
  283. if(adreno_reg_check(gpu, offset_name))
  284. gpu_write(&gpu->base, reg - 1, data);
  285. }
  286. struct msm_gpu *a3xx_gpu_init(struct drm_device *dev);
  287. struct msm_gpu *a4xx_gpu_init(struct drm_device *dev);
  288. struct msm_gpu *a5xx_gpu_init(struct drm_device *dev);
  289. struct msm_gpu *a6xx_gpu_init(struct drm_device *dev);
  290. static inline void adreno_gpu_write64(struct adreno_gpu *gpu,
  291. enum adreno_regs lo, enum adreno_regs hi, u64 data)
  292. {
  293. adreno_gpu_write(gpu, lo, lower_32_bits(data));
  294. adreno_gpu_write(gpu, hi, upper_32_bits(data));
  295. }
  296. static inline uint32_t get_wptr(struct msm_ringbuffer *ring)
  297. {
  298. return (ring->cur - ring->start) % (MSM_GPU_RINGBUFFER_SZ >> 2);
  299. }
  300. /*
  301. * Given a register and a count, return a value to program into
  302. * REG_CP_PROTECT_REG(n) - this will block both reads and writes for _len
  303. * registers starting at _reg.
  304. *
  305. * The register base needs to be a multiple of the length. If it is not, the
  306. * hardware will quietly mask off the bits for you and shift the size. For
  307. * example, if you intend the protection to start at 0x07 for a length of 4
  308. * (0x07-0x0A) the hardware will actually protect (0x04-0x07) which might
  309. * expose registers you intended to protect!
  310. */
  311. #define ADRENO_PROTECT_RW(_reg, _len) \
  312. ((1 << 30) | (1 << 29) | \
  313. ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF))
  314. /*
  315. * Same as above, but allow reads over the range. For areas of mixed use (such
  316. * as performance counters) this allows us to protect a much larger range with a
  317. * single register
  318. */
  319. #define ADRENO_PROTECT_RDONLY(_reg, _len) \
  320. ((1 << 29) \
  321. ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF))
  322. #endif /* __ADRENO_GPU_H__ */