adreno_gpu.h 10 KB

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