v3d_drv.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2015-2018 Broadcom */
  3. #include <linux/reservation.h>
  4. #include <linux/mm_types.h>
  5. #include <drm/drmP.h>
  6. #include <drm/drm_encoder.h>
  7. #include <drm/drm_gem.h>
  8. #include <drm/gpu_scheduler.h>
  9. #define GMP_GRANULARITY (128 * 1024)
  10. /* Enum for each of the V3D queues. We maintain various queue
  11. * tracking as an array because at some point we'll want to support
  12. * the TFU (texture formatting unit) as another queue.
  13. */
  14. enum v3d_queue {
  15. V3D_BIN,
  16. V3D_RENDER,
  17. };
  18. #define V3D_MAX_QUEUES (V3D_RENDER + 1)
  19. struct v3d_queue_state {
  20. struct drm_gpu_scheduler sched;
  21. u64 fence_context;
  22. u64 emit_seqno;
  23. };
  24. struct v3d_dev {
  25. struct drm_device drm;
  26. /* Short representation (e.g. 33, 41) of the V3D tech version
  27. * and revision.
  28. */
  29. int ver;
  30. struct device *dev;
  31. struct platform_device *pdev;
  32. void __iomem *hub_regs;
  33. void __iomem *core_regs[3];
  34. void __iomem *bridge_regs;
  35. void __iomem *gca_regs;
  36. struct clk *clk;
  37. /* Virtual and DMA addresses of the single shared page table. */
  38. volatile u32 *pt;
  39. dma_addr_t pt_paddr;
  40. /* Virtual and DMA addresses of the MMU's scratch page. When
  41. * a read or write is invalid in the MMU, it will be
  42. * redirected here.
  43. */
  44. void *mmu_scratch;
  45. dma_addr_t mmu_scratch_paddr;
  46. /* Number of V3D cores. */
  47. u32 cores;
  48. /* Allocator managing the address space. All units are in
  49. * number of pages.
  50. */
  51. struct drm_mm mm;
  52. spinlock_t mm_lock;
  53. struct work_struct overflow_mem_work;
  54. struct v3d_exec_info *bin_job;
  55. struct v3d_exec_info *render_job;
  56. struct v3d_queue_state queue[V3D_MAX_QUEUES];
  57. /* Spinlock used to synchronize the overflow memory
  58. * management against bin job submission.
  59. */
  60. spinlock_t job_lock;
  61. /* Protects bo_stats */
  62. struct mutex bo_lock;
  63. /* Lock taken when resetting the GPU, to keep multiple
  64. * processes from trying to park the scheduler threads and
  65. * reset at once.
  66. */
  67. struct mutex reset_lock;
  68. /* Lock taken when creating and pushing the GPU scheduler
  69. * jobs, to keep the sched-fence seqnos in order.
  70. */
  71. struct mutex sched_lock;
  72. struct {
  73. u32 num_allocated;
  74. u32 pages_allocated;
  75. } bo_stats;
  76. };
  77. static inline struct v3d_dev *
  78. to_v3d_dev(struct drm_device *dev)
  79. {
  80. return (struct v3d_dev *)dev->dev_private;
  81. }
  82. /* The per-fd struct, which tracks the MMU mappings. */
  83. struct v3d_file_priv {
  84. struct v3d_dev *v3d;
  85. struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
  86. };
  87. /* Tracks a mapping of a BO into a per-fd address space */
  88. struct v3d_vma {
  89. struct v3d_page_table *pt;
  90. struct list_head list; /* entry in v3d_bo.vmas */
  91. };
  92. struct v3d_bo {
  93. struct drm_gem_object base;
  94. struct mutex lock;
  95. struct drm_mm_node node;
  96. u32 pages_refcount;
  97. struct page **pages;
  98. struct sg_table *sgt;
  99. void *vaddr;
  100. struct list_head vmas; /* list of v3d_vma */
  101. /* List entry for the BO's position in
  102. * v3d_exec_info->unref_list
  103. */
  104. struct list_head unref_head;
  105. /* normally (resv == &_resv) except for imported bo's */
  106. struct reservation_object *resv;
  107. struct reservation_object _resv;
  108. };
  109. static inline struct v3d_bo *
  110. to_v3d_bo(struct drm_gem_object *bo)
  111. {
  112. return (struct v3d_bo *)bo;
  113. }
  114. struct v3d_fence {
  115. struct dma_fence base;
  116. struct drm_device *dev;
  117. /* v3d seqno for signaled() test */
  118. u64 seqno;
  119. enum v3d_queue queue;
  120. };
  121. static inline struct v3d_fence *
  122. to_v3d_fence(struct dma_fence *fence)
  123. {
  124. return (struct v3d_fence *)fence;
  125. }
  126. #define V3D_READ(offset) readl(v3d->hub_regs + offset)
  127. #define V3D_WRITE(offset, val) writel(val, v3d->hub_regs + offset)
  128. #define V3D_BRIDGE_READ(offset) readl(v3d->bridge_regs + offset)
  129. #define V3D_BRIDGE_WRITE(offset, val) writel(val, v3d->bridge_regs + offset)
  130. #define V3D_GCA_READ(offset) readl(v3d->gca_regs + offset)
  131. #define V3D_GCA_WRITE(offset, val) writel(val, v3d->gca_regs + offset)
  132. #define V3D_CORE_READ(core, offset) readl(v3d->core_regs[core] + offset)
  133. #define V3D_CORE_WRITE(core, offset, val) writel(val, v3d->core_regs[core] + offset)
  134. struct v3d_job {
  135. struct drm_sched_job base;
  136. struct v3d_exec_info *exec;
  137. /* An optional fence userspace can pass in for the job to depend on. */
  138. struct dma_fence *in_fence;
  139. /* v3d fence to be signaled by IRQ handler when the job is complete. */
  140. struct dma_fence *done_fence;
  141. /* GPU virtual addresses of the start/end of the CL job. */
  142. u32 start, end;
  143. u32 timedout_ctca, timedout_ctra;
  144. };
  145. struct v3d_exec_info {
  146. struct v3d_dev *v3d;
  147. struct v3d_job bin, render;
  148. /* Fence for when the scheduler considers the binner to be
  149. * done, for render to depend on.
  150. */
  151. struct dma_fence *bin_done_fence;
  152. struct kref refcount;
  153. /* This is the array of BOs that were looked up at the start of exec. */
  154. struct v3d_bo **bo;
  155. u32 bo_count;
  156. /* List of overflow BOs used in the job that need to be
  157. * released once the job is complete.
  158. */
  159. struct list_head unref_list;
  160. /* Submitted tile memory allocation start/size, tile state. */
  161. u32 qma, qms, qts;
  162. };
  163. /**
  164. * _wait_for - magic (register) wait macro
  165. *
  166. * Does the right thing for modeset paths when run under kdgb or similar atomic
  167. * contexts. Note that it's important that we check the condition again after
  168. * having timed out, since the timeout could be due to preemption or similar and
  169. * we've never had a chance to check the condition before the timeout.
  170. */
  171. #define wait_for(COND, MS) ({ \
  172. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
  173. int ret__ = 0; \
  174. while (!(COND)) { \
  175. if (time_after(jiffies, timeout__)) { \
  176. if (!(COND)) \
  177. ret__ = -ETIMEDOUT; \
  178. break; \
  179. } \
  180. msleep(1); \
  181. } \
  182. ret__; \
  183. })
  184. static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
  185. {
  186. /* nsecs_to_jiffies64() does not guard against overflow */
  187. if (NSEC_PER_SEC % HZ &&
  188. div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
  189. return MAX_JIFFY_OFFSET;
  190. return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
  191. }
  192. /* v3d_bo.c */
  193. void v3d_free_object(struct drm_gem_object *gem_obj);
  194. struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
  195. size_t size);
  196. int v3d_create_bo_ioctl(struct drm_device *dev, void *data,
  197. struct drm_file *file_priv);
  198. int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data,
  199. struct drm_file *file_priv);
  200. int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
  201. struct drm_file *file_priv);
  202. vm_fault_t v3d_gem_fault(struct vm_fault *vmf);
  203. int v3d_mmap(struct file *filp, struct vm_area_struct *vma);
  204. struct reservation_object *v3d_prime_res_obj(struct drm_gem_object *obj);
  205. int v3d_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
  206. struct sg_table *v3d_prime_get_sg_table(struct drm_gem_object *obj);
  207. struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
  208. struct dma_buf_attachment *attach,
  209. struct sg_table *sgt);
  210. /* v3d_debugfs.c */
  211. int v3d_debugfs_init(struct drm_minor *minor);
  212. /* v3d_fence.c */
  213. extern const struct dma_fence_ops v3d_fence_ops;
  214. struct dma_fence *v3d_fence_create(struct v3d_dev *v3d, enum v3d_queue queue);
  215. /* v3d_gem.c */
  216. int v3d_gem_init(struct drm_device *dev);
  217. void v3d_gem_destroy(struct drm_device *dev);
  218. int v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
  219. struct drm_file *file_priv);
  220. int v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
  221. struct drm_file *file_priv);
  222. void v3d_exec_put(struct v3d_exec_info *exec);
  223. void v3d_reset(struct v3d_dev *v3d);
  224. void v3d_invalidate_caches(struct v3d_dev *v3d);
  225. void v3d_flush_caches(struct v3d_dev *v3d);
  226. /* v3d_irq.c */
  227. void v3d_irq_init(struct v3d_dev *v3d);
  228. void v3d_irq_enable(struct v3d_dev *v3d);
  229. void v3d_irq_disable(struct v3d_dev *v3d);
  230. void v3d_irq_reset(struct v3d_dev *v3d);
  231. /* v3d_mmu.c */
  232. int v3d_mmu_get_offset(struct drm_file *file_priv, struct v3d_bo *bo,
  233. u32 *offset);
  234. int v3d_mmu_set_page_table(struct v3d_dev *v3d);
  235. void v3d_mmu_insert_ptes(struct v3d_bo *bo);
  236. void v3d_mmu_remove_ptes(struct v3d_bo *bo);
  237. /* v3d_sched.c */
  238. int v3d_sched_init(struct v3d_dev *v3d);
  239. void v3d_sched_fini(struct v3d_dev *v3d);