vc4_irq.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <linux/platform_device.h>
  47. #include <drm/drm_drv.h>
  48. #include "vc4_drv.h"
  49. #include "vc4_regs.h"
  50. #include "vc4_trace.h"
  51. #define V3D_DRIVER_IRQS (V3D_INT_OUTOMEM | \
  52. V3D_INT_FLDONE | \
  53. V3D_INT_FRDONE)
  54. static void
  55. vc4_overflow_mem_work(struct work_struct *work)
  56. {
  57. struct vc4_dev *vc4 =
  58. container_of(work, struct vc4_dev, overflow_mem_work);
  59. struct vc4_bo *bo;
  60. int bin_bo_slot;
  61. struct vc4_exec_info *exec;
  62. unsigned long irqflags;
  63. mutex_lock(&vc4->bin_bo_lock);
  64. if (!vc4->bin_bo)
  65. goto complete;
  66. bo = vc4->bin_bo;
  67. bin_bo_slot = vc4_v3d_get_bin_slot(vc4);
  68. if (bin_bo_slot < 0) {
  69. drm_err(&vc4->base, "Couldn't allocate binner overflow mem\n");
  70. goto complete;
  71. }
  72. spin_lock_irqsave(&vc4->job_lock, irqflags);
  73. if (vc4->bin_alloc_overflow) {
  74. /* If we had overflow memory allocated previously,
  75. * then that chunk will free when the current bin job
  76. * is done. If we don't have a bin job running, then
  77. * the chunk will be done whenever the list of render
  78. * jobs has drained.
  79. */
  80. exec = vc4_first_bin_job(vc4);
  81. if (!exec)
  82. exec = vc4_last_render_job(vc4);
  83. if (exec) {
  84. exec->bin_slots |= vc4->bin_alloc_overflow;
  85. } else {
  86. /* There's nothing queued in the hardware, so
  87. * the old slot is free immediately.
  88. */
  89. vc4->bin_alloc_used &= ~vc4->bin_alloc_overflow;
  90. }
  91. }
  92. vc4->bin_alloc_overflow = BIT(bin_bo_slot);
  93. V3D_WRITE(V3D_BPOA, bo->base.dma_addr + bin_bo_slot * vc4->bin_alloc_size);
  94. V3D_WRITE(V3D_BPOS, bo->base.base.size);
  95. V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
  96. V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
  97. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  98. complete:
  99. mutex_unlock(&vc4->bin_bo_lock);
  100. }
  101. static void
  102. vc4_irq_finish_bin_job(struct drm_device *dev)
  103. {
  104. struct vc4_dev *vc4 = to_vc4_dev(dev);
  105. struct vc4_exec_info *next, *exec = vc4_first_bin_job(vc4);
  106. if (!exec)
  107. return;
  108. trace_vc4_bcl_end_irq(dev, exec->seqno);
  109. vc4_move_job_to_render(dev, exec);
  110. next = vc4_first_bin_job(vc4);
  111. /* Only submit the next job in the bin list if it matches the perfmon
  112. * attached to the one that just finished (or if both jobs don't have
  113. * perfmon attached to them).
  114. */
  115. if (next && next->perfmon == exec->perfmon)
  116. vc4_submit_next_bin_job(dev);
  117. }
  118. static void
  119. vc4_cancel_bin_job(struct drm_device *dev)
  120. {
  121. struct vc4_dev *vc4 = to_vc4_dev(dev);
  122. struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
  123. if (!exec)
  124. return;
  125. /* Stop the perfmon so that the next bin job can be started. */
  126. if (exec->perfmon)
  127. vc4_perfmon_stop(vc4, exec->perfmon, false);
  128. list_move_tail(&exec->head, &vc4->bin_job_list);
  129. vc4_submit_next_bin_job(dev);
  130. }
  131. static void
  132. vc4_irq_finish_render_job(struct drm_device *dev)
  133. {
  134. struct vc4_dev *vc4 = to_vc4_dev(dev);
  135. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  136. struct vc4_exec_info *nextbin, *nextrender;
  137. if (!exec)
  138. return;
  139. trace_vc4_rcl_end_irq(dev, exec->seqno);
  140. vc4->finished_seqno++;
  141. list_move_tail(&exec->head, &vc4->job_done_list);
  142. nextbin = vc4_first_bin_job(vc4);
  143. nextrender = vc4_first_render_job(vc4);
  144. /* Only stop the perfmon if following jobs in the queue don't expect it
  145. * to be enabled.
  146. */
  147. if (exec->perfmon && !nextrender &&
  148. (!nextbin || nextbin->perfmon != exec->perfmon))
  149. vc4_perfmon_stop(vc4, exec->perfmon, true);
  150. /* If there's a render job waiting, start it. If this is not the case
  151. * we may have to unblock the binner if it's been stalled because of
  152. * perfmon (this can be checked by comparing the perfmon attached to
  153. * the finished renderjob to the one attached to the next bin job: if
  154. * they don't match, this means the binner is stalled and should be
  155. * restarted).
  156. */
  157. if (nextrender)
  158. vc4_submit_next_render_job(dev);
  159. else if (nextbin && nextbin->perfmon != exec->perfmon)
  160. vc4_submit_next_bin_job(dev);
  161. if (exec->fence) {
  162. dma_fence_signal_locked(exec->fence);
  163. dma_fence_put(exec->fence);
  164. exec->fence = NULL;
  165. }
  166. wake_up_all(&vc4->job_wait_queue);
  167. schedule_work(&vc4->job_done_work);
  168. }
  169. static irqreturn_t
  170. vc4_irq(int irq, void *arg)
  171. {
  172. struct drm_device *dev = arg;
  173. struct vc4_dev *vc4 = to_vc4_dev(dev);
  174. uint32_t intctl;
  175. irqreturn_t status = IRQ_NONE;
  176. barrier();
  177. intctl = V3D_READ(V3D_INTCTL);
  178. /* Acknowledge the interrupts we're handling here. The binner
  179. * last flush / render frame done interrupt will be cleared,
  180. * while OUTOMEM will stay high until the underlying cause is
  181. * cleared.
  182. */
  183. V3D_WRITE(V3D_INTCTL, intctl);
  184. if (intctl & V3D_INT_OUTOMEM) {
  185. /* Disable OUTOMEM until the work is done. */
  186. V3D_WRITE(V3D_INTDIS, V3D_INT_OUTOMEM);
  187. schedule_work(&vc4->overflow_mem_work);
  188. status = IRQ_HANDLED;
  189. }
  190. if (intctl & V3D_INT_FLDONE) {
  191. spin_lock(&vc4->job_lock);
  192. vc4_irq_finish_bin_job(dev);
  193. spin_unlock(&vc4->job_lock);
  194. status = IRQ_HANDLED;
  195. }
  196. if (intctl & V3D_INT_FRDONE) {
  197. spin_lock(&vc4->job_lock);
  198. vc4_irq_finish_render_job(dev);
  199. spin_unlock(&vc4->job_lock);
  200. status = IRQ_HANDLED;
  201. }
  202. return status;
  203. }
  204. static void
  205. vc4_irq_prepare(struct drm_device *dev)
  206. {
  207. struct vc4_dev *vc4 = to_vc4_dev(dev);
  208. if (!vc4->v3d)
  209. return;
  210. init_waitqueue_head(&vc4->job_wait_queue);
  211. INIT_WORK(&vc4->overflow_mem_work, vc4_overflow_mem_work);
  212. /* Clear any pending interrupts someone might have left around
  213. * for us.
  214. */
  215. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  216. }
  217. void
  218. vc4_irq_enable(struct drm_device *dev)
  219. {
  220. struct vc4_dev *vc4 = to_vc4_dev(dev);
  221. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  222. return;
  223. if (!vc4->v3d)
  224. return;
  225. /* Enable the render done interrupts. The out-of-memory interrupt is
  226. * enabled as soon as we have a binner BO allocated.
  227. */
  228. V3D_WRITE(V3D_INTENA, V3D_INT_FLDONE | V3D_INT_FRDONE);
  229. }
  230. void
  231. vc4_irq_disable(struct drm_device *dev)
  232. {
  233. struct vc4_dev *vc4 = to_vc4_dev(dev);
  234. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  235. return;
  236. if (!vc4->v3d)
  237. return;
  238. /* Disable sending interrupts for our driver's IRQs. */
  239. V3D_WRITE(V3D_INTDIS, V3D_DRIVER_IRQS);
  240. /* Clear any pending interrupts we might have left. */
  241. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  242. /* Finish any interrupt handler still in flight. */
  243. synchronize_irq(vc4->irq);
  244. cancel_work_sync(&vc4->overflow_mem_work);
  245. }
  246. int vc4_irq_install(struct drm_device *dev, int irq)
  247. {
  248. struct vc4_dev *vc4 = to_vc4_dev(dev);
  249. int ret;
  250. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  251. return -ENODEV;
  252. if (irq == IRQ_NOTCONNECTED)
  253. return -ENOTCONN;
  254. vc4_irq_prepare(dev);
  255. ret = request_irq(irq, vc4_irq, 0, dev->driver->name, dev);
  256. if (ret)
  257. return ret;
  258. vc4_irq_enable(dev);
  259. return 0;
  260. }
  261. void vc4_irq_uninstall(struct drm_device *dev)
  262. {
  263. struct vc4_dev *vc4 = to_vc4_dev(dev);
  264. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  265. return;
  266. vc4_irq_disable(dev);
  267. free_irq(vc4->irq, dev);
  268. }
  269. /** Reinitializes interrupt registers when a GPU reset is performed. */
  270. void vc4_irq_reset(struct drm_device *dev)
  271. {
  272. struct vc4_dev *vc4 = to_vc4_dev(dev);
  273. unsigned long irqflags;
  274. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  275. return;
  276. /* Acknowledge any stale IRQs. */
  277. V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  278. /*
  279. * Turn all our interrupts on. Binner out of memory is the
  280. * only one we expect to trigger at this point, since we've
  281. * just come from poweron and haven't supplied any overflow
  282. * memory yet.
  283. */
  284. V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  285. spin_lock_irqsave(&vc4->job_lock, irqflags);
  286. vc4_cancel_bin_job(dev);
  287. vc4_irq_finish_render_job(dev);
  288. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  289. }