v3d_drv.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. u64 finished_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. struct {
  69. u32 num_allocated;
  70. u32 pages_allocated;
  71. } bo_stats;
  72. };
  73. static inline struct v3d_dev *
  74. to_v3d_dev(struct drm_device *dev)
  75. {
  76. return (struct v3d_dev *)dev->dev_private;
  77. }
  78. /* The per-fd struct, which tracks the MMU mappings. */
  79. struct v3d_file_priv {
  80. struct v3d_dev *v3d;
  81. struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
  82. };
  83. /* Tracks a mapping of a BO into a per-fd address space */
  84. struct v3d_vma {
  85. struct v3d_page_table *pt;
  86. struct list_head list; /* entry in v3d_bo.vmas */
  87. };
  88. struct v3d_bo {
  89. struct drm_gem_object base;
  90. struct mutex lock;
  91. struct drm_mm_node node;
  92. u32 pages_refcount;
  93. struct page **pages;
  94. struct sg_table *sgt;
  95. void *vaddr;
  96. struct list_head vmas; /* list of v3d_vma */
  97. /* List entry for the BO's position in
  98. * v3d_exec_info->unref_list
  99. */
  100. struct list_head unref_head;
  101. /* normally (resv == &_resv) except for imported bo's */
  102. struct reservation_object *resv;
  103. struct reservation_object _resv;
  104. };
  105. static inline struct v3d_bo *
  106. to_v3d_bo(struct drm_gem_object *bo)
  107. {
  108. return (struct v3d_bo *)bo;
  109. }
  110. struct v3d_fence {
  111. struct dma_fence base;
  112. struct drm_device *dev;
  113. /* v3d seqno for signaled() test */
  114. u64 seqno;
  115. enum v3d_queue queue;
  116. };
  117. static inline struct v3d_fence *
  118. to_v3d_fence(struct dma_fence *fence)
  119. {
  120. return (struct v3d_fence *)fence;
  121. }
  122. #define V3D_READ(offset) readl(v3d->hub_regs + offset)
  123. #define V3D_WRITE(offset, val) writel(val, v3d->hub_regs + offset)
  124. #define V3D_BRIDGE_READ(offset) readl(v3d->bridge_regs + offset)
  125. #define V3D_BRIDGE_WRITE(offset, val) writel(val, v3d->bridge_regs + offset)
  126. #define V3D_GCA_READ(offset) readl(v3d->gca_regs + offset)
  127. #define V3D_GCA_WRITE(offset, val) writel(val, v3d->gca_regs + offset)
  128. #define V3D_CORE_READ(core, offset) readl(v3d->core_regs[core] + offset)
  129. #define V3D_CORE_WRITE(core, offset, val) writel(val, v3d->core_regs[core] + offset)
  130. struct v3d_job {
  131. struct drm_sched_job base;
  132. struct v3d_exec_info *exec;
  133. /* An optional fence userspace can pass in for the job to depend on. */
  134. struct dma_fence *in_fence;
  135. /* v3d fence to be signaled by IRQ handler when the job is complete. */
  136. struct dma_fence *done_fence;
  137. /* GPU virtual addresses of the start/end of the CL job. */
  138. u32 start, end;
  139. };
  140. struct v3d_exec_info {
  141. struct v3d_dev *v3d;
  142. struct v3d_job bin, render;
  143. /* Fence for when the scheduler considers the binner to be
  144. * done, for render to depend on.
  145. */
  146. struct dma_fence *bin_done_fence;
  147. struct kref refcount;
  148. /* This is the array of BOs that were looked up at the start of exec. */
  149. struct v3d_bo **bo;
  150. u32 bo_count;
  151. /* List of overflow BOs used in the job that need to be
  152. * released once the job is complete.
  153. */
  154. struct list_head unref_list;
  155. /* Submitted tile memory allocation start/size, tile state. */
  156. u32 qma, qms, qts;
  157. };
  158. /**
  159. * _wait_for - magic (register) wait macro
  160. *
  161. * Does the right thing for modeset paths when run under kdgb or similar atomic
  162. * contexts. Note that it's important that we check the condition again after
  163. * having timed out, since the timeout could be due to preemption or similar and
  164. * we've never had a chance to check the condition before the timeout.
  165. */
  166. #define wait_for(COND, MS) ({ \
  167. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
  168. int ret__ = 0; \
  169. while (!(COND)) { \
  170. if (time_after(jiffies, timeout__)) { \
  171. if (!(COND)) \
  172. ret__ = -ETIMEDOUT; \
  173. break; \
  174. } \
  175. msleep(1); \
  176. } \
  177. ret__; \
  178. })
  179. static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
  180. {
  181. /* nsecs_to_jiffies64() does not guard against overflow */
  182. if (NSEC_PER_SEC % HZ &&
  183. div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
  184. return MAX_JIFFY_OFFSET;
  185. return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
  186. }
  187. /* v3d_bo.c */
  188. void v3d_free_object(struct drm_gem_object *gem_obj);
  189. struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
  190. size_t size);
  191. int v3d_create_bo_ioctl(struct drm_device *dev, void *data,
  192. struct drm_file *file_priv);
  193. int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data,
  194. struct drm_file *file_priv);
  195. int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
  196. struct drm_file *file_priv);
  197. int v3d_gem_fault(struct vm_fault *vmf);
  198. int v3d_mmap(struct file *filp, struct vm_area_struct *vma);
  199. struct reservation_object *v3d_prime_res_obj(struct drm_gem_object *obj);
  200. int v3d_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
  201. struct sg_table *v3d_prime_get_sg_table(struct drm_gem_object *obj);
  202. struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
  203. struct dma_buf_attachment *attach,
  204. struct sg_table *sgt);
  205. /* v3d_debugfs.c */
  206. int v3d_debugfs_init(struct drm_minor *minor);
  207. /* v3d_fence.c */
  208. extern const struct dma_fence_ops v3d_fence_ops;
  209. struct dma_fence *v3d_fence_create(struct v3d_dev *v3d, enum v3d_queue queue);
  210. /* v3d_gem.c */
  211. int v3d_gem_init(struct drm_device *dev);
  212. void v3d_gem_destroy(struct drm_device *dev);
  213. int v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
  214. struct drm_file *file_priv);
  215. int v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
  216. struct drm_file *file_priv);
  217. void v3d_exec_put(struct v3d_exec_info *exec);
  218. void v3d_reset(struct v3d_dev *v3d);
  219. void v3d_invalidate_caches(struct v3d_dev *v3d);
  220. void v3d_flush_caches(struct v3d_dev *v3d);
  221. /* v3d_irq.c */
  222. void v3d_irq_init(struct v3d_dev *v3d);
  223. void v3d_irq_enable(struct v3d_dev *v3d);
  224. void v3d_irq_disable(struct v3d_dev *v3d);
  225. void v3d_irq_reset(struct v3d_dev *v3d);
  226. /* v3d_mmu.c */
  227. int v3d_mmu_get_offset(struct drm_file *file_priv, struct v3d_bo *bo,
  228. u32 *offset);
  229. int v3d_mmu_set_page_table(struct v3d_dev *v3d);
  230. void v3d_mmu_insert_ptes(struct v3d_bo *bo);
  231. void v3d_mmu_remove_ptes(struct v3d_bo *bo);
  232. /* v3d_sched.c */
  233. int v3d_sched_init(struct v3d_dev *v3d);
  234. void v3d_sched_fini(struct v3d_dev *v3d);