adreno_gpu.h 9.7 KB

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