vc4_irq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright © 2014 Broadcom
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. /**
  24. * DOC: Interrupt management for the V3D engine
  25. *
  26. * We have an interrupt status register (V3D_INTCTL) which reports
  27. * interrupts, and where writing 1 bits clears those interrupts.
  28. * There are also a pair of interrupt registers
  29. * (V3D_INTENA/V3D_INTDIS) where writing a 1 to their bits enables or
  30. * disables that specific interrupt, and 0s written are ignored
  31. * (reading either one returns the set of enabled interrupts).
  32. *
  33. * When we take a binning flush done interrupt, we need to submit the
  34. * next frame for binning and move the finished frame to the render
  35. * thread.
  36. *
  37. * When we take a render frame interrupt, we need to wake the
  38. * processes waiting for some frame to be done, and get the next frame
  39. * submitted ASAP (so the hardware doesn't sit idle when there's work
  40. * to do).
  41. *
  42. * When we take the binner out of memory interrupt, we need to
  43. * allocate some new memory and pass it to the binner so that the
  44. * current job can make progress.
  45. */
  46. #include "vc4_drv.h"
  47. #include "vc4_regs.h"
  48. #define V3D_DRIVER_IRQS (V3D_INT_OUTOMEM | \
  49. V3D_INT_FLDONE | \
  50. V3D_INT_FRDONE)
  51. DECLARE_WAIT_QUEUE_HEAD(render_wait);
  52. static void
  53. vc4_overflow_mem_work(struct work_struct *work)
  54. {
  55. struct vc4_dev *vc4 =
  56. container_of(work, struct vc4_dev, overflow_mem_work);
  57. struct vc4_bo *bo = vc4->bin_bo;
  58. int bin_bo_slot;
  59. struct vc4_exec_info *exec;
  60. unsigned long irqflags;
  61. bin_bo_slot = vc4_v3d_get_bin_slot(vc4);
  62. if (bin_bo_slot < 0) {
  63. DRM_ERROR("Couldn't allocate binner overflow mem\n");
  64. return;
  65. }
  66. spin_lock_irqsave(&vc4->job_lock, irqflags);
  67. if (vc4->bin_alloc_overflow) {
  68. /* If we had overflow memory allocated previously,
  69. * then that chunk will free when the current bin job
  70. * is done. If we don't have a bin job running, then
  71. * the chunk will be done whenever the list of render
  72. * jobs has drained.
  73. */
  74. exec = vc4_first_bin_job(vc4);
  75. if (!exec)
  76. exec = vc4_last_render_job(vc4);
  77. if (exec) {
  78. exec->bin_slots |= vc4->bin_alloc_overflow;
  79. } else {
  80. /* There's nothing queued in the hardware, so
  81. * the old slot is free immediately.
  82. */
  83. vc4->bin_alloc_used &= ~vc4->bin_alloc_overflow;
  84. }
  85. }
  86. vc4->bin_alloc_overflow = BIT(bin_bo_slot);
  87. V3D_WRITE(V3D_BPOA, bo->base.paddr + bin_bo_slot * vc4->bin_alloc_size);
  88. V3D_WRITE(V3D_BPOS, bo->base.base.size);
  89. V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
  90. V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
  91. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  92. }
  93. static void
  94. vc4_irq_finish_bin_job(struct drm_device *dev)
  95. {
  96. struct vc4_dev *vc4 = to_vc4_dev(dev);
  97. struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
  98. if (!exec)
  99. return;
  100. vc4_move_job_to_render(dev, exec);
  101. vc4_submit_next_bin_job(dev);
  102. }
  103. static void
  104. vc4_cancel_bin_job(struct drm_device *dev)
  105. {
  106. struct vc4_dev *vc4 = to_vc4_dev(dev);
  107. struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
  108. if (!exec)
  109. return;
  110. list_move_tail(&exec->head, &vc4->bin_job_list);
  111. vc4_submit_next_bin_job(dev);
  112. }
  113. static void
  114. vc4_irq_finish_render_job(struct drm_device *dev)
  115. {
  116. struct vc4_dev *vc4 = to_vc4_dev(dev);
  117. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  118. if (!exec)
  119. return;
  120. vc4->finished_seqno++;
  121. list_move_tail(&exec->head, &vc4->job_done_list);
  122. if (exec->fence) {
  123. dma_fence_signal_locked(exec->fence);
  124. exec->fence = NULL;
  125. }
  126. vc4_submit_next_render_job(dev);
  127. wake_up_all(&vc4->job_wait_queue);
  128. schedule_work(&vc4->job_done_work);
  129. }
  130. irqreturn_t
  131. vc4_irq(int irq, void *arg)
  132. {
  133. struct drm_device *dev = arg;
  134. struct vc4_dev *vc4 = to_vc4_dev(dev);
  135. uint32_t intctl;
  136. irqreturn_t status = IRQ_NONE;
  137. barrier();
  138. intctl = V3D_READ(V3D_INTCTL);
  139. /* Acknowledge the interrupts we're handling here. The binner
  140. * last flush / render frame done interrupt will be cleared,
  141. * while OUTOMEM will stay high until the underlying cause is
  142. * cleared.
  143. */
  144. V3D_WRITE(V3D_INTCTL, intctl);
  145. if (intctl & V3D_INT_OUTOMEM) {
  146. /* Disable OUTOMEM until the work is done. */
  147. V3D_WRITE(V3D_INTDIS, V3D_INT_OUTOMEM);
  148. schedule_work(&vc4->overflow_mem_work);
  149. status = IRQ_HANDLED;
  150. }
  151. if (intctl & V3D_INT_FLDONE) {
  152. spin_lock(&vc4->job_lock);
  153. vc4_irq_finish_bin_job(dev);
  154. spin_unlock(&vc4->job_lock);
  155. status = IRQ_HANDLED;
  156. }
  157. if (intctl & V3D_INT_FRDONE) {
  158. spin_lock(&vc4->job_lock);
  159. vc4_irq_finish_render_job(dev);
  160. spin_unlock(&vc4->job_lock);
  161. status = IRQ_HANDLED;
  162. }
  163. return status;
  164. }
  165. void
  166. vc4_irq_preinstall(struct drm_device *dev)
  167. {
  168. struct vc4_dev *vc4 = to_vc4_dev(dev);
  169. init_waitqueue_head(&vc4->job_wait_queue);
  170. INIT_WORK(&vc4->overflow_mem_work, vc4_overflow_mem_work);
  171. /* Clear any pending interrupts someone might have left around
  172. * for us.
  173. */
  174. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  175. }
  176. int
  177. vc4_irq_postinstall(struct drm_device *dev)
  178. {
  179. struct vc4_dev *vc4 = to_vc4_dev(dev);
  180. /* Undo the effects of a previous vc4_irq_uninstall. */
  181. enable_irq(dev->irq);
  182. /* Enable both the render done and out of memory interrupts. */
  183. V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  184. return 0;
  185. }
  186. void
  187. vc4_irq_uninstall(struct drm_device *dev)
  188. {
  189. struct vc4_dev *vc4 = to_vc4_dev(dev);
  190. /* Disable sending interrupts for our driver's IRQs. */
  191. V3D_WRITE(V3D_INTDIS, V3D_DRIVER_IRQS);
  192. /* Clear any pending interrupts we might have left. */
  193. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  194. /* Finish any interrupt handler still in flight. */
  195. disable_irq(dev->irq);
  196. cancel_work_sync(&vc4->overflow_mem_work);
  197. }
  198. /** Reinitializes interrupt registers when a GPU reset is performed. */
  199. void vc4_irq_reset(struct drm_device *dev)
  200. {
  201. struct vc4_dev *vc4 = to_vc4_dev(dev);
  202. unsigned long irqflags;
  203. /* Acknowledge any stale IRQs. */
  204. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  205. /*
  206. * Turn all our interrupts on. Binner out of memory is the
  207. * only one we expect to trigger at this point, since we've
  208. * just come from poweron and haven't supplied any overflow
  209. * memory yet.
  210. */
  211. V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  212. spin_lock_irqsave(&vc4->job_lock, irqflags);
  213. vc4_cancel_bin_job(dev);
  214. vc4_irq_finish_render_job(dev);
  215. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  216. }