vc4_irq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /** DOC: Interrupt management for the V3D engine.
  24. *
  25. * We have an interrupt status register (V3D_INTCTL) which reports
  26. * interrupts, and where writing 1 bits clears those interrupts.
  27. * There are also a pair of interrupt registers
  28. * (V3D_INTENA/V3D_INTDIS) where writing a 1 to their bits enables or
  29. * disables that specific interrupt, and 0s written are ignored
  30. * (reading either one returns the set of enabled interrupts).
  31. *
  32. * When we take a binning flush done interrupt, we need to submit the
  33. * next frame for binning and move the finished frame to the render
  34. * thread.
  35. *
  36. * When we take a render frame interrupt, we need to wake the
  37. * processes waiting for some frame to be done, and get the next frame
  38. * submitted ASAP (so the hardware doesn't sit idle when there's work
  39. * to do).
  40. *
  41. * When we take the binner out of memory interrupt, we need to
  42. * allocate some new memory and pass it to the binner so that the
  43. * current job can make progress.
  44. */
  45. #include "vc4_drv.h"
  46. #include "vc4_regs.h"
  47. #define V3D_DRIVER_IRQS (V3D_INT_OUTOMEM | \
  48. V3D_INT_FLDONE | \
  49. V3D_INT_FRDONE)
  50. DECLARE_WAIT_QUEUE_HEAD(render_wait);
  51. static void
  52. vc4_overflow_mem_work(struct work_struct *work)
  53. {
  54. struct vc4_dev *vc4 =
  55. container_of(work, struct vc4_dev, overflow_mem_work);
  56. struct drm_device *dev = vc4->dev;
  57. struct vc4_bo *bo;
  58. bo = vc4_bo_create(dev, 256 * 1024, true);
  59. if (IS_ERR(bo)) {
  60. DRM_ERROR("Couldn't allocate binner overflow mem\n");
  61. return;
  62. }
  63. /* If there's a job executing currently, then our previous
  64. * overflow allocation is getting used in that job and we need
  65. * to queue it to be released when the job is done. But if no
  66. * job is executing at all, then we can free the old overflow
  67. * object direcctly.
  68. *
  69. * No lock necessary for this pointer since we're the only
  70. * ones that update the pointer, and our workqueue won't
  71. * reenter.
  72. */
  73. if (vc4->overflow_mem) {
  74. struct vc4_exec_info *current_exec;
  75. unsigned long irqflags;
  76. spin_lock_irqsave(&vc4->job_lock, irqflags);
  77. current_exec = vc4_first_bin_job(vc4);
  78. if (!current_exec)
  79. current_exec = vc4_last_render_job(vc4);
  80. if (current_exec) {
  81. vc4->overflow_mem->seqno = current_exec->seqno;
  82. list_add_tail(&vc4->overflow_mem->unref_head,
  83. &current_exec->unref_list);
  84. vc4->overflow_mem = NULL;
  85. }
  86. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  87. }
  88. if (vc4->overflow_mem)
  89. drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base);
  90. vc4->overflow_mem = bo;
  91. V3D_WRITE(V3D_BPOA, bo->base.paddr);
  92. V3D_WRITE(V3D_BPOS, bo->base.base.size);
  93. V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
  94. V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
  95. }
  96. static void
  97. vc4_irq_finish_bin_job(struct drm_device *dev)
  98. {
  99. struct vc4_dev *vc4 = to_vc4_dev(dev);
  100. struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
  101. if (!exec)
  102. return;
  103. vc4_move_job_to_render(dev, exec);
  104. vc4_submit_next_bin_job(dev);
  105. }
  106. static void
  107. vc4_cancel_bin_job(struct drm_device *dev)
  108. {
  109. struct vc4_dev *vc4 = to_vc4_dev(dev);
  110. struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
  111. if (!exec)
  112. return;
  113. list_move_tail(&exec->head, &vc4->bin_job_list);
  114. vc4_submit_next_bin_job(dev);
  115. }
  116. static void
  117. vc4_irq_finish_render_job(struct drm_device *dev)
  118. {
  119. struct vc4_dev *vc4 = to_vc4_dev(dev);
  120. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  121. if (!exec)
  122. return;
  123. vc4->finished_seqno++;
  124. list_move_tail(&exec->head, &vc4->job_done_list);
  125. vc4_submit_next_render_job(dev);
  126. wake_up_all(&vc4->job_wait_queue);
  127. schedule_work(&vc4->job_done_work);
  128. }
  129. irqreturn_t
  130. vc4_irq(int irq, void *arg)
  131. {
  132. struct drm_device *dev = arg;
  133. struct vc4_dev *vc4 = to_vc4_dev(dev);
  134. uint32_t intctl;
  135. irqreturn_t status = IRQ_NONE;
  136. barrier();
  137. intctl = V3D_READ(V3D_INTCTL);
  138. /* Acknowledge the interrupts we're handling here. The binner
  139. * last flush / render frame done interrupt will be cleared,
  140. * while OUTOMEM will stay high until the underlying cause is
  141. * cleared.
  142. */
  143. V3D_WRITE(V3D_INTCTL, intctl);
  144. if (intctl & V3D_INT_OUTOMEM) {
  145. /* Disable OUTOMEM until the work is done. */
  146. V3D_WRITE(V3D_INTDIS, V3D_INT_OUTOMEM);
  147. schedule_work(&vc4->overflow_mem_work);
  148. status = IRQ_HANDLED;
  149. }
  150. if (intctl & V3D_INT_FLDONE) {
  151. spin_lock(&vc4->job_lock);
  152. vc4_irq_finish_bin_job(dev);
  153. spin_unlock(&vc4->job_lock);
  154. status = IRQ_HANDLED;
  155. }
  156. if (intctl & V3D_INT_FRDONE) {
  157. spin_lock(&vc4->job_lock);
  158. vc4_irq_finish_render_job(dev);
  159. spin_unlock(&vc4->job_lock);
  160. status = IRQ_HANDLED;
  161. }
  162. return status;
  163. }
  164. void
  165. vc4_irq_preinstall(struct drm_device *dev)
  166. {
  167. struct vc4_dev *vc4 = to_vc4_dev(dev);
  168. init_waitqueue_head(&vc4->job_wait_queue);
  169. INIT_WORK(&vc4->overflow_mem_work, vc4_overflow_mem_work);
  170. /* Clear any pending interrupts someone might have left around
  171. * for us.
  172. */
  173. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  174. }
  175. int
  176. vc4_irq_postinstall(struct drm_device *dev)
  177. {
  178. struct vc4_dev *vc4 = to_vc4_dev(dev);
  179. /* Enable both the render done and out of memory interrupts. */
  180. V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  181. return 0;
  182. }
  183. void
  184. vc4_irq_uninstall(struct drm_device *dev)
  185. {
  186. struct vc4_dev *vc4 = to_vc4_dev(dev);
  187. /* Disable sending interrupts for our driver's IRQs. */
  188. V3D_WRITE(V3D_INTDIS, V3D_DRIVER_IRQS);
  189. /* Clear any pending interrupts we might have left. */
  190. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  191. /* Finish any interrupt handler still in flight. */
  192. disable_irq(dev->irq);
  193. cancel_work_sync(&vc4->overflow_mem_work);
  194. }
  195. /** Reinitializes interrupt registers when a GPU reset is performed. */
  196. void vc4_irq_reset(struct drm_device *dev)
  197. {
  198. struct vc4_dev *vc4 = to_vc4_dev(dev);
  199. unsigned long irqflags;
  200. /* Acknowledge any stale IRQs. */
  201. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  202. /*
  203. * Turn all our interrupts on. Binner out of memory is the
  204. * only one we expect to trigger at this point, since we've
  205. * just come from poweron and haven't supplied any overflow
  206. * memory yet.
  207. */
  208. V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  209. spin_lock_irqsave(&vc4->job_lock, irqflags);
  210. vc4_cancel_bin_job(dev);
  211. vc4_irq_finish_render_job(dev);
  212. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  213. }