v3d_drv.h 7.8 KB

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