vc4_irq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 drm_device *dev = vc4->dev;
  58. struct vc4_bo *bo;
  59. bo = vc4_bo_create(dev, 256 * 1024, true);
  60. if (IS_ERR(bo)) {
  61. DRM_ERROR("Couldn't allocate binner overflow mem\n");
  62. return;
  63. }
  64. /* If there's a job executing currently, then our previous
  65. * overflow allocation is getting used in that job and we need
  66. * to queue it to be released when the job is done. But if no
  67. * job is executing at all, then we can free the old overflow
  68. * object direcctly.
  69. *
  70. * No lock necessary for this pointer since we're the only
  71. * ones that update the pointer, and our workqueue won't
  72. * reenter.
  73. */
  74. if (vc4->overflow_mem) {
  75. struct vc4_exec_info *current_exec;
  76. unsigned long irqflags;
  77. spin_lock_irqsave(&vc4->job_lock, irqflags);
  78. current_exec = vc4_first_bin_job(vc4);
  79. if (!current_exec)
  80. current_exec = vc4_last_render_job(vc4);
  81. if (current_exec) {
  82. vc4->overflow_mem->seqno = current_exec->seqno;
  83. list_add_tail(&vc4->overflow_mem->unref_head,
  84. &current_exec->unref_list);
  85. vc4->overflow_mem = NULL;
  86. }
  87. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  88. }
  89. if (vc4->overflow_mem)
  90. drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base);
  91. vc4->overflow_mem = bo;
  92. V3D_WRITE(V3D_BPOA, bo->base.paddr);
  93. V3D_WRITE(V3D_BPOS, bo->base.base.size);
  94. V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
  95. V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
  96. }
  97. static void
  98. vc4_irq_finish_bin_job(struct drm_device *dev)
  99. {
  100. struct vc4_dev *vc4 = to_vc4_dev(dev);
  101. struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
  102. if (!exec)
  103. return;
  104. vc4_move_job_to_render(dev, exec);
  105. vc4_submit_next_bin_job(dev);
  106. }
  107. static void
  108. vc4_cancel_bin_job(struct drm_device *dev)
  109. {
  110. struct vc4_dev *vc4 = to_vc4_dev(dev);
  111. struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
  112. if (!exec)
  113. return;
  114. list_move_tail(&exec->head, &vc4->bin_job_list);
  115. vc4_submit_next_bin_job(dev);
  116. }
  117. static void
  118. vc4_irq_finish_render_job(struct drm_device *dev)
  119. {
  120. struct vc4_dev *vc4 = to_vc4_dev(dev);
  121. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  122. if (!exec)
  123. return;
  124. vc4->finished_seqno++;
  125. list_move_tail(&exec->head, &vc4->job_done_list);
  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. /* Enable both the render done and out of memory interrupts. */
  181. V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  182. return 0;
  183. }
  184. void
  185. vc4_irq_uninstall(struct drm_device *dev)
  186. {
  187. struct vc4_dev *vc4 = to_vc4_dev(dev);
  188. /* Disable sending interrupts for our driver's IRQs. */
  189. V3D_WRITE(V3D_INTDIS, V3D_DRIVER_IRQS);
  190. /* Clear any pending interrupts we might have left. */
  191. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  192. cancel_work_sync(&vc4->overflow_mem_work);
  193. }
  194. /** Reinitializes interrupt registers when a GPU reset is performed. */
  195. void vc4_irq_reset(struct drm_device *dev)
  196. {
  197. struct vc4_dev *vc4 = to_vc4_dev(dev);
  198. unsigned long irqflags;
  199. /* Acknowledge any stale IRQs. */
  200. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  201. /*
  202. * Turn all our interrupts on. Binner out of memory is the
  203. * only one we expect to trigger at this point, since we've
  204. * just come from poweron and haven't supplied any overflow
  205. * memory yet.
  206. */
  207. V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  208. spin_lock_irqsave(&vc4->job_lock, irqflags);
  209. vc4_cancel_bin_job(dev);
  210. vc4_irq_finish_render_job(dev);
  211. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  212. }