msm_gpu.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_config {
  27. const char *ioname;
  28. const char *irqname;
  29. uint64_t va_start;
  30. uint64_t va_end;
  31. unsigned int nr_rings;
  32. };
  33. /* So far, with hardware that I've seen to date, we can have:
  34. * + zero, one, or two z180 2d cores
  35. * + a3xx or a2xx 3d core, which share a common CP (the firmware
  36. * for the CP seems to implement some different PM4 packet types
  37. * but the basics of cmdstream submission are the same)
  38. *
  39. * Which means that the eventual complete "class" hierarchy, once
  40. * support for all past and present hw is in place, becomes:
  41. * + msm_gpu
  42. * + adreno_gpu
  43. * + a3xx_gpu
  44. * + a2xx_gpu
  45. * + z180_gpu
  46. */
  47. struct msm_gpu_funcs {
  48. int (*get_param)(struct msm_gpu *gpu, uint32_t param, uint64_t *value);
  49. int (*hw_init)(struct msm_gpu *gpu);
  50. int (*pm_suspend)(struct msm_gpu *gpu);
  51. int (*pm_resume)(struct msm_gpu *gpu);
  52. void (*submit)(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  53. struct msm_file_private *ctx);
  54. void (*flush)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
  55. irqreturn_t (*irq)(struct msm_gpu *irq);
  56. struct msm_ringbuffer *(*active_ring)(struct msm_gpu *gpu);
  57. void (*recover)(struct msm_gpu *gpu);
  58. void (*destroy)(struct msm_gpu *gpu);
  59. #ifdef CONFIG_DEBUG_FS
  60. /* show GPU status in debugfs: */
  61. void (*show)(struct msm_gpu *gpu, struct seq_file *m);
  62. /* for generation specific debugfs: */
  63. int (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
  64. #endif
  65. int (*gpu_busy)(struct msm_gpu *gpu, uint64_t *value);
  66. };
  67. struct msm_gpu {
  68. const char *name;
  69. struct drm_device *dev;
  70. struct platform_device *pdev;
  71. const struct msm_gpu_funcs *funcs;
  72. /* performance counters (hw & sw): */
  73. spinlock_t perf_lock;
  74. bool perfcntr_active;
  75. struct {
  76. bool active;
  77. ktime_t time;
  78. } last_sample;
  79. uint32_t totaltime, activetime; /* sw counters */
  80. uint32_t last_cntrs[5]; /* hw counters */
  81. const struct msm_gpu_perfcntr *perfcntrs;
  82. uint32_t num_perfcntrs;
  83. struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
  84. int nr_rings;
  85. /* list of GEM active objects: */
  86. struct list_head active_list;
  87. /* does gpu need hw_init? */
  88. bool needs_hw_init;
  89. /* worker for handling active-list retiring: */
  90. struct work_struct retire_work;
  91. void __iomem *mmio;
  92. int irq;
  93. struct msm_gem_address_space *aspace;
  94. /* Power Control: */
  95. struct regulator *gpu_reg, *gpu_cx;
  96. struct clk **grp_clks;
  97. int nr_clocks;
  98. struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
  99. uint32_t fast_rate;
  100. /* Hang and Inactivity Detection:
  101. */
  102. #define DRM_MSM_INACTIVE_PERIOD 66 /* in ms (roughly four frames) */
  103. #define DRM_MSM_HANGCHECK_PERIOD 500 /* in ms */
  104. #define DRM_MSM_HANGCHECK_JIFFIES msecs_to_jiffies(DRM_MSM_HANGCHECK_PERIOD)
  105. struct timer_list hangcheck_timer;
  106. struct work_struct recover_work;
  107. struct drm_gem_object *memptrs_bo;
  108. struct {
  109. struct devfreq *devfreq;
  110. u64 busy_cycles;
  111. ktime_t time;
  112. } devfreq;
  113. };
  114. /* It turns out that all targets use the same ringbuffer size */
  115. #define MSM_GPU_RINGBUFFER_SZ SZ_32K
  116. #define MSM_GPU_RINGBUFFER_BLKSIZE 32
  117. #define MSM_GPU_RB_CNTL_DEFAULT \
  118. (AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
  119. AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
  120. static inline bool msm_gpu_active(struct msm_gpu *gpu)
  121. {
  122. int i;
  123. for (i = 0; i < gpu->nr_rings; i++) {
  124. struct msm_ringbuffer *ring = gpu->rb[i];
  125. if (ring->seqno > ring->memptrs->fence)
  126. return true;
  127. }
  128. return false;
  129. }
  130. /* Perf-Counters:
  131. * The select_reg and select_val are just there for the benefit of the child
  132. * class that actually enables the perf counter.. but msm_gpu base class
  133. * will handle sampling/displaying the counters.
  134. */
  135. struct msm_gpu_perfcntr {
  136. uint32_t select_reg;
  137. uint32_t sample_reg;
  138. uint32_t select_val;
  139. const char *name;
  140. };
  141. struct msm_gpu_submitqueue {
  142. int id;
  143. u32 flags;
  144. u32 prio;
  145. int faults;
  146. struct list_head node;
  147. struct kref ref;
  148. };
  149. static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
  150. {
  151. msm_writel(data, gpu->mmio + (reg << 2));
  152. }
  153. static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
  154. {
  155. return msm_readl(gpu->mmio + (reg << 2));
  156. }
  157. static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
  158. {
  159. uint32_t val = gpu_read(gpu, reg);
  160. val &= ~mask;
  161. gpu_write(gpu, reg, val | or);
  162. }
  163. static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
  164. {
  165. u64 val;
  166. /*
  167. * Why not a readq here? Two reasons: 1) many of the LO registers are
  168. * not quad word aligned and 2) the GPU hardware designers have a bit
  169. * of a history of putting registers where they fit, especially in
  170. * spins. The longer a GPU family goes the higher the chance that
  171. * we'll get burned. We could do a series of validity checks if we
  172. * wanted to, but really is a readq() that much better? Nah.
  173. */
  174. /*
  175. * For some lo/hi registers (like perfcounters), the hi value is latched
  176. * when the lo is read, so make sure to read the lo first to trigger
  177. * that
  178. */
  179. val = (u64) msm_readl(gpu->mmio + (lo << 2));
  180. val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
  181. return val;
  182. }
  183. static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
  184. {
  185. /* Why not a writeq here? Read the screed above */
  186. msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
  187. msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
  188. }
  189. int msm_gpu_pm_suspend(struct msm_gpu *gpu);
  190. int msm_gpu_pm_resume(struct msm_gpu *gpu);
  191. int msm_gpu_hw_init(struct msm_gpu *gpu);
  192. void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
  193. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
  194. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  195. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
  196. void msm_gpu_retire(struct msm_gpu *gpu);
  197. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  198. struct msm_file_private *ctx);
  199. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  200. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  201. const char *name, struct msm_gpu_config *config);
  202. void msm_gpu_cleanup(struct msm_gpu *gpu);
  203. struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
  204. void __init adreno_register(void);
  205. void __exit adreno_unregister(void);
  206. static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
  207. {
  208. if (queue)
  209. kref_put(&queue->ref, msm_submitqueue_destroy);
  210. }
  211. #endif /* __MSM_GPU_H__ */