adreno_gpu.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * Copyright (c) 2014 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 adreno_quirks {
  45. ADRENO_QUIRK_TWO_PASS_USE_WFI = 1,
  46. ADRENO_QUIRK_FAULT_DETECT_MASK = 2,
  47. };
  48. struct adreno_rev {
  49. uint8_t core;
  50. uint8_t major;
  51. uint8_t minor;
  52. uint8_t patchid;
  53. };
  54. #define ADRENO_REV(core, major, minor, patchid) \
  55. ((struct adreno_rev){ core, major, minor, patchid })
  56. struct adreno_gpu_funcs {
  57. struct msm_gpu_funcs base;
  58. int (*get_timestamp)(struct msm_gpu *gpu, uint64_t *value);
  59. };
  60. struct adreno_info {
  61. struct adreno_rev rev;
  62. uint32_t revn;
  63. const char *name;
  64. const char *pm4fw, *pfpfw;
  65. const char *gpmufw;
  66. uint32_t gmem;
  67. struct msm_gpu *(*init)(struct drm_device *dev);
  68. };
  69. const struct adreno_info *adreno_info(struct adreno_rev rev);
  70. #define rbmemptr(adreno_gpu, member) \
  71. ((adreno_gpu)->memptrs_iova + offsetof(struct adreno_rbmemptrs, member))
  72. struct adreno_rbmemptrs {
  73. volatile uint32_t rptr;
  74. volatile uint32_t wptr;
  75. volatile uint32_t fence;
  76. };
  77. struct adreno_gpu {
  78. struct msm_gpu base;
  79. struct adreno_rev rev;
  80. const struct adreno_info *info;
  81. uint32_t gmem; /* actual gmem size */
  82. uint32_t revn; /* numeric revision name */
  83. const struct adreno_gpu_funcs *funcs;
  84. /* interesting register offsets to dump: */
  85. const unsigned int *registers;
  86. /* firmware: */
  87. const struct firmware *pm4, *pfp;
  88. /* ringbuffer rptr/wptr: */
  89. // TODO should this be in msm_ringbuffer? I think it would be
  90. // different for z180..
  91. struct adreno_rbmemptrs *memptrs;
  92. struct drm_gem_object *memptrs_bo;
  93. uint64_t memptrs_iova;
  94. /*
  95. * Register offsets are different between some GPUs.
  96. * GPU specific offsets will be exported by GPU specific
  97. * code (a3xx_gpu.c) and stored in this common location.
  98. */
  99. const unsigned int *reg_offsets;
  100. uint32_t quirks;
  101. };
  102. #define to_adreno_gpu(x) container_of(x, struct adreno_gpu, base)
  103. /* platform config data (ie. from DT, or pdata) */
  104. struct adreno_platform_config {
  105. struct adreno_rev rev;
  106. uint32_t fast_rate, slow_rate, bus_freq;
  107. #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
  108. struct msm_bus_scale_pdata *bus_scale_table;
  109. #endif
  110. uint32_t quirks;
  111. };
  112. #define ADRENO_IDLE_TIMEOUT msecs_to_jiffies(1000)
  113. #define spin_until(X) ({ \
  114. int __ret = -ETIMEDOUT; \
  115. unsigned long __t = jiffies + ADRENO_IDLE_TIMEOUT; \
  116. do { \
  117. if (X) { \
  118. __ret = 0; \
  119. break; \
  120. } \
  121. } while (time_before(jiffies, __t)); \
  122. __ret; \
  123. })
  124. static inline bool adreno_is_a3xx(struct adreno_gpu *gpu)
  125. {
  126. return (gpu->revn >= 300) && (gpu->revn < 400);
  127. }
  128. static inline bool adreno_is_a305(struct adreno_gpu *gpu)
  129. {
  130. return gpu->revn == 305;
  131. }
  132. static inline bool adreno_is_a306(struct adreno_gpu *gpu)
  133. {
  134. /* yes, 307, because a305c is 306 */
  135. return gpu->revn == 307;
  136. }
  137. static inline bool adreno_is_a320(struct adreno_gpu *gpu)
  138. {
  139. return gpu->revn == 320;
  140. }
  141. static inline bool adreno_is_a330(struct adreno_gpu *gpu)
  142. {
  143. return gpu->revn == 330;
  144. }
  145. static inline bool adreno_is_a330v2(struct adreno_gpu *gpu)
  146. {
  147. return adreno_is_a330(gpu) && (gpu->rev.patchid > 0);
  148. }
  149. static inline bool adreno_is_a4xx(struct adreno_gpu *gpu)
  150. {
  151. return (gpu->revn >= 400) && (gpu->revn < 500);
  152. }
  153. static inline int adreno_is_a420(struct adreno_gpu *gpu)
  154. {
  155. return gpu->revn == 420;
  156. }
  157. static inline int adreno_is_a430(struct adreno_gpu *gpu)
  158. {
  159. return gpu->revn == 430;
  160. }
  161. static inline int adreno_is_a530(struct adreno_gpu *gpu)
  162. {
  163. return gpu->revn == 530;
  164. }
  165. int adreno_get_param(struct msm_gpu *gpu, uint32_t param, uint64_t *value);
  166. int adreno_hw_init(struct msm_gpu *gpu);
  167. uint32_t adreno_last_fence(struct msm_gpu *gpu);
  168. void adreno_recover(struct msm_gpu *gpu);
  169. void adreno_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  170. struct msm_file_private *ctx);
  171. void adreno_flush(struct msm_gpu *gpu);
  172. bool adreno_idle(struct msm_gpu *gpu);
  173. #ifdef CONFIG_DEBUG_FS
  174. void adreno_show(struct msm_gpu *gpu, struct seq_file *m);
  175. #endif
  176. void adreno_dump_info(struct msm_gpu *gpu);
  177. void adreno_dump(struct msm_gpu *gpu);
  178. void adreno_wait_ring(struct msm_gpu *gpu, uint32_t ndwords);
  179. int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  180. struct adreno_gpu *gpu, const struct adreno_gpu_funcs *funcs);
  181. void adreno_gpu_cleanup(struct adreno_gpu *gpu);
  182. /* ringbuffer helpers (the parts that are adreno specific) */
  183. static inline void
  184. OUT_PKT0(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt)
  185. {
  186. adreno_wait_ring(ring->gpu, cnt+1);
  187. OUT_RING(ring, CP_TYPE0_PKT | ((cnt-1) << 16) | (regindx & 0x7FFF));
  188. }
  189. /* no-op packet: */
  190. static inline void
  191. OUT_PKT2(struct msm_ringbuffer *ring)
  192. {
  193. adreno_wait_ring(ring->gpu, 1);
  194. OUT_RING(ring, CP_TYPE2_PKT);
  195. }
  196. static inline void
  197. OUT_PKT3(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt)
  198. {
  199. adreno_wait_ring(ring->gpu, cnt+1);
  200. OUT_RING(ring, CP_TYPE3_PKT | ((cnt-1) << 16) | ((opcode & 0xFF) << 8));
  201. }
  202. static inline u32 PM4_PARITY(u32 val)
  203. {
  204. return (0x9669 >> (0xF & (val ^
  205. (val >> 4) ^ (val >> 8) ^ (val >> 12) ^
  206. (val >> 16) ^ ((val) >> 20) ^ (val >> 24) ^
  207. (val >> 28)))) & 1;
  208. }
  209. /* Maximum number of values that can be executed for one opcode */
  210. #define TYPE4_MAX_PAYLOAD 127
  211. #define PKT4(_reg, _cnt) \
  212. (CP_TYPE4_PKT | ((_cnt) << 0) | (PM4_PARITY((_cnt)) << 7) | \
  213. (((_reg) & 0x3FFFF) << 8) | (PM4_PARITY((_reg)) << 27))
  214. static inline void
  215. OUT_PKT4(struct msm_ringbuffer *ring, uint16_t regindx, uint16_t cnt)
  216. {
  217. adreno_wait_ring(ring->gpu, cnt + 1);
  218. OUT_RING(ring, PKT4(regindx, cnt));
  219. }
  220. static inline void
  221. OUT_PKT7(struct msm_ringbuffer *ring, uint8_t opcode, uint16_t cnt)
  222. {
  223. adreno_wait_ring(ring->gpu, cnt + 1);
  224. OUT_RING(ring, CP_TYPE7_PKT | (cnt << 0) | (PM4_PARITY(cnt) << 15) |
  225. ((opcode & 0x7F) << 16) | (PM4_PARITY(opcode) << 23));
  226. }
  227. /*
  228. * adreno_reg_check() - Checks the validity of a register enum
  229. * @gpu: Pointer to struct adreno_gpu
  230. * @offset_name: The register enum that is checked
  231. */
  232. static inline bool adreno_reg_check(struct adreno_gpu *gpu,
  233. enum adreno_regs offset_name)
  234. {
  235. if (offset_name >= REG_ADRENO_REGISTER_MAX ||
  236. !gpu->reg_offsets[offset_name]) {
  237. BUG();
  238. }
  239. /*
  240. * REG_SKIP is a special value that tell us that the register in
  241. * question isn't implemented on target but don't trigger a BUG(). This
  242. * is used to cleanly implement adreno_gpu_write64() and
  243. * adreno_gpu_read64() in a generic fashion
  244. */
  245. if (gpu->reg_offsets[offset_name] == REG_SKIP)
  246. return false;
  247. return true;
  248. }
  249. static inline u32 adreno_gpu_read(struct adreno_gpu *gpu,
  250. enum adreno_regs offset_name)
  251. {
  252. u32 reg = gpu->reg_offsets[offset_name];
  253. u32 val = 0;
  254. if(adreno_reg_check(gpu,offset_name))
  255. val = gpu_read(&gpu->base, reg - 1);
  256. return val;
  257. }
  258. static inline void adreno_gpu_write(struct adreno_gpu *gpu,
  259. enum adreno_regs offset_name, u32 data)
  260. {
  261. u32 reg = gpu->reg_offsets[offset_name];
  262. if(adreno_reg_check(gpu, offset_name))
  263. gpu_write(&gpu->base, reg - 1, data);
  264. }
  265. struct msm_gpu *a3xx_gpu_init(struct drm_device *dev);
  266. struct msm_gpu *a4xx_gpu_init(struct drm_device *dev);
  267. struct msm_gpu *a5xx_gpu_init(struct drm_device *dev);
  268. static inline void adreno_gpu_write64(struct adreno_gpu *gpu,
  269. enum adreno_regs lo, enum adreno_regs hi, u64 data)
  270. {
  271. adreno_gpu_write(gpu, lo, lower_32_bits(data));
  272. adreno_gpu_write(gpu, hi, upper_32_bits(data));
  273. }
  274. /*
  275. * Given a register and a count, return a value to program into
  276. * REG_CP_PROTECT_REG(n) - this will block both reads and writes for _len
  277. * registers starting at _reg.
  278. *
  279. * The register base needs to be a multiple of the length. If it is not, the
  280. * hardware will quietly mask off the bits for you and shift the size. For
  281. * example, if you intend the protection to start at 0x07 for a length of 4
  282. * (0x07-0x0A) the hardware will actually protect (0x04-0x07) which might
  283. * expose registers you intended to protect!
  284. */
  285. #define ADRENO_PROTECT_RW(_reg, _len) \
  286. ((1 << 30) | (1 << 29) | \
  287. ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF))
  288. /*
  289. * Same as above, but allow reads over the range. For areas of mixed use (such
  290. * as performance counters) this allows us to protect a much larger range with a
  291. * single register
  292. */
  293. #define ADRENO_PROTECT_RDONLY(_reg, _len) \
  294. ((1 << 29) \
  295. ((ilog2((_len)) & 0x1F) << 24) | (((_reg) << 2) & 0xFFFFF))
  296. #endif /* __ADRENO_GPU_H__ */