msm_gpu.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #ifdef CONFIG_DEBUG_FS
  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. unsigned long (*gpu_busy)(struct msm_gpu *gpu);
  68. struct msm_gpu_state *(*gpu_state_get)(struct msm_gpu *gpu);
  69. int (*gpu_state_put)(struct msm_gpu_state *state);
  70. unsigned long (*gpu_get_freq)(struct msm_gpu *gpu);
  71. void (*gpu_set_freq)(struct msm_gpu *gpu, unsigned long freq);
  72. };
  73. struct msm_gpu {
  74. const char *name;
  75. struct drm_device *dev;
  76. struct platform_device *pdev;
  77. const struct msm_gpu_funcs *funcs;
  78. /* performance counters (hw & sw): */
  79. spinlock_t perf_lock;
  80. bool perfcntr_active;
  81. struct {
  82. bool active;
  83. ktime_t time;
  84. } last_sample;
  85. uint32_t totaltime, activetime; /* sw counters */
  86. uint32_t last_cntrs[5]; /* hw counters */
  87. const struct msm_gpu_perfcntr *perfcntrs;
  88. uint32_t num_perfcntrs;
  89. struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
  90. int nr_rings;
  91. /* list of GEM active objects: */
  92. struct list_head active_list;
  93. /* does gpu need hw_init? */
  94. bool needs_hw_init;
  95. /* worker for handling active-list retiring: */
  96. struct work_struct retire_work;
  97. void __iomem *mmio;
  98. int irq;
  99. struct msm_gem_address_space *aspace;
  100. /* Power Control: */
  101. struct regulator *gpu_reg, *gpu_cx;
  102. struct clk_bulk_data *grp_clks;
  103. int nr_clocks;
  104. struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
  105. uint32_t fast_rate;
  106. /* Hang and Inactivity Detection:
  107. */
  108. #define DRM_MSM_INACTIVE_PERIOD 66 /* in ms (roughly four frames) */
  109. #define DRM_MSM_HANGCHECK_PERIOD 500 /* in ms */
  110. #define DRM_MSM_HANGCHECK_JIFFIES msecs_to_jiffies(DRM_MSM_HANGCHECK_PERIOD)
  111. struct timer_list hangcheck_timer;
  112. struct work_struct recover_work;
  113. struct drm_gem_object *memptrs_bo;
  114. struct {
  115. struct devfreq *devfreq;
  116. u64 busy_cycles;
  117. ktime_t time;
  118. } devfreq;
  119. struct msm_gpu_state *crashstate;
  120. };
  121. /* It turns out that all targets use the same ringbuffer size */
  122. #define MSM_GPU_RINGBUFFER_SZ SZ_32K
  123. #define MSM_GPU_RINGBUFFER_BLKSIZE 32
  124. #define MSM_GPU_RB_CNTL_DEFAULT \
  125. (AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
  126. AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
  127. static inline bool msm_gpu_active(struct msm_gpu *gpu)
  128. {
  129. int i;
  130. for (i = 0; i < gpu->nr_rings; i++) {
  131. struct msm_ringbuffer *ring = gpu->rb[i];
  132. if (ring->seqno > ring->memptrs->fence)
  133. return true;
  134. }
  135. return false;
  136. }
  137. /* Perf-Counters:
  138. * The select_reg and select_val are just there for the benefit of the child
  139. * class that actually enables the perf counter.. but msm_gpu base class
  140. * will handle sampling/displaying the counters.
  141. */
  142. struct msm_gpu_perfcntr {
  143. uint32_t select_reg;
  144. uint32_t sample_reg;
  145. uint32_t select_val;
  146. const char *name;
  147. };
  148. struct msm_gpu_submitqueue {
  149. int id;
  150. u32 flags;
  151. u32 prio;
  152. int faults;
  153. struct list_head node;
  154. struct kref ref;
  155. };
  156. struct msm_gpu_state_bo {
  157. u64 iova;
  158. size_t size;
  159. void *data;
  160. };
  161. struct msm_gpu_state {
  162. struct kref ref;
  163. struct timespec64 time;
  164. struct {
  165. u64 iova;
  166. u32 fence;
  167. u32 seqno;
  168. u32 rptr;
  169. u32 wptr;
  170. void *data;
  171. int data_size;
  172. } ring[MSM_GPU_MAX_RINGS];
  173. int nr_registers;
  174. u32 *registers;
  175. u32 rbbm_status;
  176. char *comm;
  177. char *cmd;
  178. int nr_bos;
  179. struct msm_gpu_state_bo *bos;
  180. };
  181. static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
  182. {
  183. msm_writel(data, gpu->mmio + (reg << 2));
  184. }
  185. static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
  186. {
  187. return msm_readl(gpu->mmio + (reg << 2));
  188. }
  189. static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
  190. {
  191. uint32_t val = gpu_read(gpu, reg);
  192. val &= ~mask;
  193. gpu_write(gpu, reg, val | or);
  194. }
  195. static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
  196. {
  197. u64 val;
  198. /*
  199. * Why not a readq here? Two reasons: 1) many of the LO registers are
  200. * not quad word aligned and 2) the GPU hardware designers have a bit
  201. * of a history of putting registers where they fit, especially in
  202. * spins. The longer a GPU family goes the higher the chance that
  203. * we'll get burned. We could do a series of validity checks if we
  204. * wanted to, but really is a readq() that much better? Nah.
  205. */
  206. /*
  207. * For some lo/hi registers (like perfcounters), the hi value is latched
  208. * when the lo is read, so make sure to read the lo first to trigger
  209. * that
  210. */
  211. val = (u64) msm_readl(gpu->mmio + (lo << 2));
  212. val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
  213. return val;
  214. }
  215. static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
  216. {
  217. /* Why not a writeq here? Read the screed above */
  218. msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
  219. msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
  220. }
  221. int msm_gpu_pm_suspend(struct msm_gpu *gpu);
  222. int msm_gpu_pm_resume(struct msm_gpu *gpu);
  223. void msm_gpu_resume_devfreq(struct msm_gpu *gpu);
  224. int msm_gpu_hw_init(struct msm_gpu *gpu);
  225. void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
  226. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
  227. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  228. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
  229. void msm_gpu_retire(struct msm_gpu *gpu);
  230. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  231. struct msm_file_private *ctx);
  232. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  233. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  234. const char *name, struct msm_gpu_config *config);
  235. void msm_gpu_cleanup(struct msm_gpu *gpu);
  236. struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
  237. void __init adreno_register(void);
  238. void __exit adreno_unregister(void);
  239. static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
  240. {
  241. if (queue)
  242. kref_put(&queue->ref, msm_submitqueue_destroy);
  243. }
  244. static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
  245. {
  246. struct msm_gpu_state *state = NULL;
  247. mutex_lock(&gpu->dev->struct_mutex);
  248. if (gpu->crashstate) {
  249. kref_get(&gpu->crashstate->ref);
  250. state = gpu->crashstate;
  251. }
  252. mutex_unlock(&gpu->dev->struct_mutex);
  253. return state;
  254. }
  255. static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
  256. {
  257. mutex_lock(&gpu->dev->struct_mutex);
  258. if (gpu->crashstate) {
  259. if (gpu->funcs->gpu_state_put(gpu->crashstate))
  260. gpu->crashstate = NULL;
  261. }
  262. mutex_unlock(&gpu->dev->struct_mutex);
  263. }
  264. #endif /* __MSM_GPU_H__ */