msm_gpu.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __MSM_GPU_H__
  18. #define __MSM_GPU_H__
  19. #include <linux/clk.h>
  20. #include <linux/regulator/consumer.h>
  21. #include "msm_drv.h"
  22. #include "msm_fence.h"
  23. #include "msm_ringbuffer.h"
  24. struct msm_gem_submit;
  25. struct msm_gpu_perfcntr;
  26. struct msm_gpu_state;
  27. struct msm_gpu_config {
  28. const char *ioname;
  29. const char *irqname;
  30. uint64_t va_start;
  31. uint64_t va_end;
  32. unsigned int nr_rings;
  33. };
  34. /* So far, with hardware that I've seen to date, we can have:
  35. * + zero, one, or two z180 2d cores
  36. * + a3xx or a2xx 3d core, which share a common CP (the firmware
  37. * for the CP seems to implement some different PM4 packet types
  38. * but the basics of cmdstream submission are the same)
  39. *
  40. * Which means that the eventual complete "class" hierarchy, once
  41. * support for all past and present hw is in place, becomes:
  42. * + msm_gpu
  43. * + adreno_gpu
  44. * + a3xx_gpu
  45. * + a2xx_gpu
  46. * + z180_gpu
  47. */
  48. struct msm_gpu_funcs {
  49. int (*get_param)(struct msm_gpu *gpu, uint32_t param, uint64_t *value);
  50. int (*hw_init)(struct msm_gpu *gpu);
  51. int (*pm_suspend)(struct msm_gpu *gpu);
  52. int (*pm_resume)(struct msm_gpu *gpu);
  53. void (*submit)(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  54. struct msm_file_private *ctx);
  55. void (*flush)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
  56. irqreturn_t (*irq)(struct msm_gpu *irq);
  57. struct msm_ringbuffer *(*active_ring)(struct msm_gpu *gpu);
  58. void (*recover)(struct msm_gpu *gpu);
  59. void (*destroy)(struct msm_gpu *gpu);
  60. #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
  61. /* show GPU status in debugfs: */
  62. void (*show)(struct msm_gpu *gpu, struct msm_gpu_state *state,
  63. struct drm_printer *p);
  64. /* for generation specific debugfs: */
  65. int (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
  66. #endif
  67. int (*gpu_busy)(struct msm_gpu *gpu, uint64_t *value);
  68. struct msm_gpu_state *(*gpu_state_get)(struct msm_gpu *gpu);
  69. int (*gpu_state_put)(struct msm_gpu_state *state);
  70. };
  71. struct msm_gpu {
  72. const char *name;
  73. struct drm_device *dev;
  74. struct platform_device *pdev;
  75. const struct msm_gpu_funcs *funcs;
  76. /* performance counters (hw & sw): */
  77. spinlock_t perf_lock;
  78. bool perfcntr_active;
  79. struct {
  80. bool active;
  81. ktime_t time;
  82. } last_sample;
  83. uint32_t totaltime, activetime; /* sw counters */
  84. uint32_t last_cntrs[5]; /* hw counters */
  85. const struct msm_gpu_perfcntr *perfcntrs;
  86. uint32_t num_perfcntrs;
  87. struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
  88. int nr_rings;
  89. /* list of GEM active objects: */
  90. struct list_head active_list;
  91. /* does gpu need hw_init? */
  92. bool needs_hw_init;
  93. /* worker for handling active-list retiring: */
  94. struct work_struct retire_work;
  95. void __iomem *mmio;
  96. int irq;
  97. struct msm_gem_address_space *aspace;
  98. /* Power Control: */
  99. struct regulator *gpu_reg, *gpu_cx;
  100. struct clk_bulk_data *grp_clks;
  101. int nr_clocks;
  102. struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
  103. uint32_t fast_rate;
  104. /* Hang and Inactivity Detection:
  105. */
  106. #define DRM_MSM_INACTIVE_PERIOD 66 /* in ms (roughly four frames) */
  107. #define DRM_MSM_HANGCHECK_PERIOD 500 /* in ms */
  108. #define DRM_MSM_HANGCHECK_JIFFIES msecs_to_jiffies(DRM_MSM_HANGCHECK_PERIOD)
  109. struct timer_list hangcheck_timer;
  110. struct work_struct recover_work;
  111. struct drm_gem_object *memptrs_bo;
  112. struct {
  113. struct devfreq *devfreq;
  114. u64 busy_cycles;
  115. ktime_t time;
  116. } devfreq;
  117. struct msm_gpu_state *crashstate;
  118. };
  119. /* It turns out that all targets use the same ringbuffer size */
  120. #define MSM_GPU_RINGBUFFER_SZ SZ_32K
  121. #define MSM_GPU_RINGBUFFER_BLKSIZE 32
  122. #define MSM_GPU_RB_CNTL_DEFAULT \
  123. (AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
  124. AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
  125. static inline bool msm_gpu_active(struct msm_gpu *gpu)
  126. {
  127. int i;
  128. for (i = 0; i < gpu->nr_rings; i++) {
  129. struct msm_ringbuffer *ring = gpu->rb[i];
  130. if (ring->seqno > ring->memptrs->fence)
  131. return true;
  132. }
  133. return false;
  134. }
  135. /* Perf-Counters:
  136. * The select_reg and select_val are just there for the benefit of the child
  137. * class that actually enables the perf counter.. but msm_gpu base class
  138. * will handle sampling/displaying the counters.
  139. */
  140. struct msm_gpu_perfcntr {
  141. uint32_t select_reg;
  142. uint32_t sample_reg;
  143. uint32_t select_val;
  144. const char *name;
  145. };
  146. struct msm_gpu_submitqueue {
  147. int id;
  148. u32 flags;
  149. u32 prio;
  150. int faults;
  151. struct list_head node;
  152. struct kref ref;
  153. };
  154. struct msm_gpu_state_bo {
  155. u64 iova;
  156. size_t size;
  157. void *data;
  158. };
  159. struct msm_gpu_state {
  160. struct kref ref;
  161. struct timespec64 time;
  162. struct {
  163. u64 iova;
  164. u32 fence;
  165. u32 seqno;
  166. u32 rptr;
  167. u32 wptr;
  168. void *data;
  169. int data_size;
  170. } ring[MSM_GPU_MAX_RINGS];
  171. int nr_registers;
  172. u32 *registers;
  173. u32 rbbm_status;
  174. char *comm;
  175. char *cmd;
  176. int nr_bos;
  177. struct msm_gpu_state_bo *bos;
  178. };
  179. static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
  180. {
  181. msm_writel(data, gpu->mmio + (reg << 2));
  182. }
  183. static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
  184. {
  185. return msm_readl(gpu->mmio + (reg << 2));
  186. }
  187. static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
  188. {
  189. uint32_t val = gpu_read(gpu, reg);
  190. val &= ~mask;
  191. gpu_write(gpu, reg, val | or);
  192. }
  193. static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
  194. {
  195. u64 val;
  196. /*
  197. * Why not a readq here? Two reasons: 1) many of the LO registers are
  198. * not quad word aligned and 2) the GPU hardware designers have a bit
  199. * of a history of putting registers where they fit, especially in
  200. * spins. The longer a GPU family goes the higher the chance that
  201. * we'll get burned. We could do a series of validity checks if we
  202. * wanted to, but really is a readq() that much better? Nah.
  203. */
  204. /*
  205. * For some lo/hi registers (like perfcounters), the hi value is latched
  206. * when the lo is read, so make sure to read the lo first to trigger
  207. * that
  208. */
  209. val = (u64) msm_readl(gpu->mmio + (lo << 2));
  210. val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
  211. return val;
  212. }
  213. static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
  214. {
  215. /* Why not a writeq here? Read the screed above */
  216. msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
  217. msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
  218. }
  219. int msm_gpu_pm_suspend(struct msm_gpu *gpu);
  220. int msm_gpu_pm_resume(struct msm_gpu *gpu);
  221. int msm_gpu_hw_init(struct msm_gpu *gpu);
  222. void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
  223. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
  224. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  225. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
  226. void msm_gpu_retire(struct msm_gpu *gpu);
  227. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  228. struct msm_file_private *ctx);
  229. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  230. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  231. const char *name, struct msm_gpu_config *config);
  232. void msm_gpu_cleanup(struct msm_gpu *gpu);
  233. struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
  234. void __init adreno_register(void);
  235. void __exit adreno_unregister(void);
  236. static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
  237. {
  238. if (queue)
  239. kref_put(&queue->ref, msm_submitqueue_destroy);
  240. }
  241. static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
  242. {
  243. struct msm_gpu_state *state = NULL;
  244. mutex_lock(&gpu->dev->struct_mutex);
  245. if (gpu->crashstate) {
  246. kref_get(&gpu->crashstate->ref);
  247. state = gpu->crashstate;
  248. }
  249. mutex_unlock(&gpu->dev->struct_mutex);
  250. return state;
  251. }
  252. static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
  253. {
  254. mutex_lock(&gpu->dev->struct_mutex);
  255. if (gpu->crashstate) {
  256. if (gpu->funcs->gpu_state_put(gpu->crashstate))
  257. gpu->crashstate = NULL;
  258. }
  259. mutex_unlock(&gpu->dev->struct_mutex);
  260. }
  261. #endif /* __MSM_GPU_H__ */