v3d_drv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2015-2018 Broadcom */
  3. #include <linux/delay.h>
  4. #include <linux/mutex.h>
  5. #include <linux/spinlock_types.h>
  6. #include <linux/workqueue.h>
  7. #include <drm/drm_encoder.h>
  8. #include <drm/drm_gem.h>
  9. #include <drm/drm_gem_shmem_helper.h>
  10. #include <drm/gpu_scheduler.h>
  11. #include "uapi/drm/v3d_drm.h"
  12. struct clk;
  13. struct platform_device;
  14. struct reset_control;
  15. #define GMP_GRANULARITY (128 * 1024)
  16. #define V3D_MAX_QUEUES (V3D_CACHE_CLEAN + 1)
  17. struct v3d_queue_state {
  18. struct drm_gpu_scheduler sched;
  19. u64 fence_context;
  20. u64 emit_seqno;
  21. };
  22. /* Performance monitor object. The perform lifetime is controlled by userspace
  23. * using perfmon related ioctls. A perfmon can be attached to a submit_cl
  24. * request, and when this is the case, HW perf counters will be activated just
  25. * before the submit_cl is submitted to the GPU and disabled when the job is
  26. * done. This way, only events related to a specific job will be counted.
  27. */
  28. struct v3d_perfmon {
  29. /* Tracks the number of users of the perfmon, when this counter reaches
  30. * zero the perfmon is destroyed.
  31. */
  32. refcount_t refcnt;
  33. /* Protects perfmon stop, as it can be invoked from multiple places. */
  34. struct mutex lock;
  35. /* Number of counters activated in this perfmon instance
  36. * (should be less than DRM_V3D_MAX_PERF_COUNTERS).
  37. */
  38. u8 ncounters;
  39. /* Events counted by the HW perf counters. */
  40. u8 counters[DRM_V3D_MAX_PERF_COUNTERS];
  41. /* Storage for counter values. Counters are incremented by the
  42. * HW perf counter values every time the perfmon is attached
  43. * to a GPU job. This way, perfmon users don't have to
  44. * retrieve the results after each job if they want to track
  45. * events covering several submissions. Note that counter
  46. * values can't be reset, but you can fake a reset by
  47. * destroying the perfmon and creating a new one.
  48. */
  49. u64 values[];
  50. };
  51. struct v3d_dev {
  52. struct drm_device drm;
  53. /* Short representation (e.g. 33, 41) of the V3D tech version
  54. * and revision.
  55. */
  56. int ver;
  57. bool single_irq_line;
  58. void __iomem *hub_regs;
  59. void __iomem *core_regs[3];
  60. void __iomem *bridge_regs;
  61. void __iomem *gca_regs;
  62. struct clk *clk;
  63. struct reset_control *reset;
  64. /* Virtual and DMA addresses of the single shared page table. */
  65. volatile u32 *pt;
  66. dma_addr_t pt_paddr;
  67. /* Virtual and DMA addresses of the MMU's scratch page. When
  68. * a read or write is invalid in the MMU, it will be
  69. * redirected here.
  70. */
  71. void *mmu_scratch;
  72. dma_addr_t mmu_scratch_paddr;
  73. /* virtual address bits from V3D to the MMU. */
  74. int va_width;
  75. /* Number of V3D cores. */
  76. u32 cores;
  77. /* Allocator managing the address space. All units are in
  78. * number of pages.
  79. */
  80. struct drm_mm mm;
  81. spinlock_t mm_lock;
  82. struct work_struct overflow_mem_work;
  83. struct v3d_bin_job *bin_job;
  84. struct v3d_render_job *render_job;
  85. struct v3d_tfu_job *tfu_job;
  86. struct v3d_csd_job *csd_job;
  87. struct v3d_queue_state queue[V3D_MAX_QUEUES];
  88. /* Spinlock used to synchronize the overflow memory
  89. * management against bin job submission.
  90. */
  91. spinlock_t job_lock;
  92. /* Used to track the active perfmon if any. */
  93. struct v3d_perfmon *active_perfmon;
  94. /* Protects bo_stats */
  95. struct mutex bo_lock;
  96. /* Lock taken when resetting the GPU, to keep multiple
  97. * processes from trying to park the scheduler threads and
  98. * reset at once.
  99. */
  100. struct mutex reset_lock;
  101. /* Lock taken when creating and pushing the GPU scheduler
  102. * jobs, to keep the sched-fence seqnos in order.
  103. */
  104. struct mutex sched_lock;
  105. /* Lock taken during a cache clean and when initiating an L2
  106. * flush, to keep L2 flushes from interfering with the
  107. * synchronous L2 cleans.
  108. */
  109. struct mutex cache_clean_lock;
  110. struct {
  111. u32 num_allocated;
  112. u32 pages_allocated;
  113. } bo_stats;
  114. };
  115. static inline struct v3d_dev *
  116. to_v3d_dev(struct drm_device *dev)
  117. {
  118. return container_of(dev, struct v3d_dev, drm);
  119. }
  120. static inline bool
  121. v3d_has_csd(struct v3d_dev *v3d)
  122. {
  123. return v3d->ver >= 41;
  124. }
  125. #define v3d_to_pdev(v3d) to_platform_device((v3d)->drm.dev)
  126. /* The per-fd struct, which tracks the MMU mappings. */
  127. struct v3d_file_priv {
  128. struct v3d_dev *v3d;
  129. struct {
  130. struct idr idr;
  131. struct mutex lock;
  132. } perfmon;
  133. struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
  134. };
  135. struct v3d_bo {
  136. struct drm_gem_shmem_object base;
  137. struct drm_mm_node node;
  138. /* List entry for the BO's position in
  139. * v3d_render_job->unref_list
  140. */
  141. struct list_head unref_head;
  142. };
  143. static inline struct v3d_bo *
  144. to_v3d_bo(struct drm_gem_object *bo)
  145. {
  146. return (struct v3d_bo *)bo;
  147. }
  148. struct v3d_fence {
  149. struct dma_fence base;
  150. struct drm_device *dev;
  151. /* v3d seqno for signaled() test */
  152. u64 seqno;
  153. enum v3d_queue queue;
  154. };
  155. static inline struct v3d_fence *
  156. to_v3d_fence(struct dma_fence *fence)
  157. {
  158. return (struct v3d_fence *)fence;
  159. }
  160. #define V3D_READ(offset) readl(v3d->hub_regs + offset)
  161. #define V3D_WRITE(offset, val) writel(val, v3d->hub_regs + offset)
  162. #define V3D_BRIDGE_READ(offset) readl(v3d->bridge_regs + offset)
  163. #define V3D_BRIDGE_WRITE(offset, val) writel(val, v3d->bridge_regs + offset)
  164. #define V3D_GCA_READ(offset) readl(v3d->gca_regs + offset)
  165. #define V3D_GCA_WRITE(offset, val) writel(val, v3d->gca_regs + offset)
  166. #define V3D_CORE_READ(core, offset) readl(v3d->core_regs[core] + offset)
  167. #define V3D_CORE_WRITE(core, offset, val) writel(val, v3d->core_regs[core] + offset)
  168. struct v3d_job {
  169. struct drm_sched_job base;
  170. struct kref refcount;
  171. struct v3d_dev *v3d;
  172. /* This is the array of BOs that were looked up at the start
  173. * of submission.
  174. */
  175. struct drm_gem_object **bo;
  176. u32 bo_count;
  177. /* v3d fence to be signaled by IRQ handler when the job is complete. */
  178. struct dma_fence *irq_fence;
  179. /* scheduler fence for when the job is considered complete and
  180. * the BO reservations can be released.
  181. */
  182. struct dma_fence *done_fence;
  183. /* Pointer to a performance monitor object if the user requested it,
  184. * NULL otherwise.
  185. */
  186. struct v3d_perfmon *perfmon;
  187. /* Callback for the freeing of the job on refcount going to 0. */
  188. void (*free)(struct kref *ref);
  189. };
  190. struct v3d_bin_job {
  191. struct v3d_job base;
  192. /* GPU virtual addresses of the start/end of the CL job. */
  193. u32 start, end;
  194. u32 timedout_ctca, timedout_ctra;
  195. /* Corresponding render job, for attaching our overflow memory. */
  196. struct v3d_render_job *render;
  197. /* Submitted tile memory allocation start/size, tile state. */
  198. u32 qma, qms, qts;
  199. };
  200. struct v3d_render_job {
  201. struct v3d_job base;
  202. /* GPU virtual addresses of the start/end of the CL job. */
  203. u32 start, end;
  204. u32 timedout_ctca, timedout_ctra;
  205. /* List of overflow BOs used in the job that need to be
  206. * released once the job is complete.
  207. */
  208. struct list_head unref_list;
  209. };
  210. struct v3d_tfu_job {
  211. struct v3d_job base;
  212. struct drm_v3d_submit_tfu args;
  213. };
  214. struct v3d_csd_job {
  215. struct v3d_job base;
  216. u32 timedout_batches;
  217. struct drm_v3d_submit_csd args;
  218. };
  219. struct v3d_submit_outsync {
  220. struct drm_syncobj *syncobj;
  221. };
  222. struct v3d_submit_ext {
  223. u32 flags;
  224. u32 wait_stage;
  225. u32 in_sync_count;
  226. u64 in_syncs;
  227. u32 out_sync_count;
  228. struct v3d_submit_outsync *out_syncs;
  229. };
  230. /**
  231. * __wait_for - magic wait macro
  232. *
  233. * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
  234. * important that we check the condition again after having timed out, since the
  235. * timeout could be due to preemption or similar and we've never had a chance to
  236. * check the condition before the timeout.
  237. */
  238. #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
  239. const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
  240. long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
  241. int ret__; \
  242. might_sleep(); \
  243. for (;;) { \
  244. const bool expired__ = ktime_after(ktime_get_raw(), end__); \
  245. OP; \
  246. /* Guarantee COND check prior to timeout */ \
  247. barrier(); \
  248. if (COND) { \
  249. ret__ = 0; \
  250. break; \
  251. } \
  252. if (expired__) { \
  253. ret__ = -ETIMEDOUT; \
  254. break; \
  255. } \
  256. usleep_range(wait__, wait__ * 2); \
  257. if (wait__ < (Wmax)) \
  258. wait__ <<= 1; \
  259. } \
  260. ret__; \
  261. })
  262. #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
  263. (Wmax))
  264. #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
  265. static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
  266. {
  267. /* nsecs_to_jiffies64() does not guard against overflow */
  268. if (NSEC_PER_SEC % HZ &&
  269. div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
  270. return MAX_JIFFY_OFFSET;
  271. return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
  272. }
  273. /* v3d_bo.c */
  274. struct drm_gem_object *v3d_create_object(struct drm_device *dev, size_t size);
  275. void v3d_free_object(struct drm_gem_object *gem_obj);
  276. struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
  277. size_t size);
  278. int v3d_create_bo_ioctl(struct drm_device *dev, void *data,
  279. struct drm_file *file_priv);
  280. int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data,
  281. struct drm_file *file_priv);
  282. int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
  283. struct drm_file *file_priv);
  284. struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
  285. struct dma_buf_attachment *attach,
  286. struct sg_table *sgt);
  287. /* v3d_debugfs.c */
  288. void v3d_debugfs_init(struct drm_minor *minor);
  289. /* v3d_fence.c */
  290. extern const struct dma_fence_ops v3d_fence_ops;
  291. struct dma_fence *v3d_fence_create(struct v3d_dev *v3d, enum v3d_queue queue);
  292. /* v3d_gem.c */
  293. int v3d_gem_init(struct drm_device *dev);
  294. void v3d_gem_destroy(struct drm_device *dev);
  295. int v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
  296. struct drm_file *file_priv);
  297. int v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
  298. struct drm_file *file_priv);
  299. int v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
  300. struct drm_file *file_priv);
  301. int v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
  302. struct drm_file *file_priv);
  303. void v3d_job_cleanup(struct v3d_job *job);
  304. void v3d_job_put(struct v3d_job *job);
  305. void v3d_reset(struct v3d_dev *v3d);
  306. void v3d_invalidate_caches(struct v3d_dev *v3d);
  307. void v3d_clean_caches(struct v3d_dev *v3d);
  308. /* v3d_irq.c */
  309. int v3d_irq_init(struct v3d_dev *v3d);
  310. void v3d_irq_enable(struct v3d_dev *v3d);
  311. void v3d_irq_disable(struct v3d_dev *v3d);
  312. void v3d_irq_reset(struct v3d_dev *v3d);
  313. /* v3d_mmu.c */
  314. int v3d_mmu_get_offset(struct drm_file *file_priv, struct v3d_bo *bo,
  315. u32 *offset);
  316. int v3d_mmu_set_page_table(struct v3d_dev *v3d);
  317. void v3d_mmu_insert_ptes(struct v3d_bo *bo);
  318. void v3d_mmu_remove_ptes(struct v3d_bo *bo);
  319. /* v3d_sched.c */
  320. int v3d_sched_init(struct v3d_dev *v3d);
  321. void v3d_sched_fini(struct v3d_dev *v3d);
  322. /* v3d_perfmon.c */
  323. void v3d_perfmon_get(struct v3d_perfmon *perfmon);
  324. void v3d_perfmon_put(struct v3d_perfmon *perfmon);
  325. void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon);
  326. void v3d_perfmon_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon,
  327. bool capture);
  328. struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id);
  329. void v3d_perfmon_open_file(struct v3d_file_priv *v3d_priv);
  330. void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv);
  331. int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,
  332. struct drm_file *file_priv);
  333. int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
  334. struct drm_file *file_priv);
  335. int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
  336. struct drm_file *file_priv);