2 * Copyright © 2014 Broadcom
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:
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
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
25 * DOC: Interrupt management for the V3D engine
27 * We have an interrupt status register (V3D_INTCTL) which reports
28 * interrupts, and where writing 1 bits clears those interrupts.
29 * There are also a pair of interrupt registers
30 * (V3D_INTENA/V3D_INTDIS) where writing a 1 to their bits enables or
31 * disables that specific interrupt, and 0s written are ignored
32 * (reading either one returns the set of enabled interrupts).
34 * When we take a binning flush done interrupt, we need to submit the
35 * next frame for binning and move the finished frame to the render
38 * When we take a render frame interrupt, we need to wake the
39 * processes waiting for some frame to be done, and get the next frame
40 * submitted ASAP (so the hardware doesn't sit idle when there's work
43 * When we take the binner out of memory interrupt, we need to
44 * allocate some new memory and pass it to the binner so that the
45 * current job can make progress.
48 #include <linux/platform_device.h>
50 #include <drm/drm_drv.h>
55 #define V3D_DRIVER_IRQS (V3D_INT_OUTOMEM | \
59 DECLARE_WAIT_QUEUE_HEAD(render_wait);
62 vc4_overflow_mem_work(struct work_struct *work)
65 container_of(work, struct vc4_dev, overflow_mem_work);
68 struct vc4_exec_info *exec;
69 unsigned long irqflags;
71 mutex_lock(&vc4->bin_bo_lock);
78 bin_bo_slot = vc4_v3d_get_bin_slot(vc4);
79 if (bin_bo_slot < 0) {
80 DRM_ERROR("Couldn't allocate binner overflow mem\n");
84 spin_lock_irqsave(&vc4->job_lock, irqflags);
86 if (vc4->bin_alloc_overflow) {
87 /* If we had overflow memory allocated previously,
88 * then that chunk will free when the current bin job
89 * is done. If we don't have a bin job running, then
90 * the chunk will be done whenever the list of render
93 exec = vc4_first_bin_job(vc4);
95 exec = vc4_last_render_job(vc4);
97 exec->bin_slots |= vc4->bin_alloc_overflow;
99 /* There's nothing queued in the hardware, so
100 * the old slot is free immediately.
102 vc4->bin_alloc_used &= ~vc4->bin_alloc_overflow;
105 vc4->bin_alloc_overflow = BIT(bin_bo_slot);
107 V3D_WRITE(V3D_BPOA, bo->base.paddr + bin_bo_slot * vc4->bin_alloc_size);
108 V3D_WRITE(V3D_BPOS, bo->base.base.size);
109 V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
110 V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
111 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
114 mutex_unlock(&vc4->bin_bo_lock);
118 vc4_irq_finish_bin_job(struct drm_device *dev)
120 struct vc4_dev *vc4 = to_vc4_dev(dev);
121 struct vc4_exec_info *next, *exec = vc4_first_bin_job(vc4);
126 vc4_move_job_to_render(dev, exec);
127 next = vc4_first_bin_job(vc4);
129 /* Only submit the next job in the bin list if it matches the perfmon
130 * attached to the one that just finished (or if both jobs don't have
131 * perfmon attached to them).
133 if (next && next->perfmon == exec->perfmon)
134 vc4_submit_next_bin_job(dev);
138 vc4_cancel_bin_job(struct drm_device *dev)
140 struct vc4_dev *vc4 = to_vc4_dev(dev);
141 struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
146 /* Stop the perfmon so that the next bin job can be started. */
148 vc4_perfmon_stop(vc4, exec->perfmon, false);
150 list_move_tail(&exec->head, &vc4->bin_job_list);
151 vc4_submit_next_bin_job(dev);
155 vc4_irq_finish_render_job(struct drm_device *dev)
157 struct vc4_dev *vc4 = to_vc4_dev(dev);
158 struct vc4_exec_info *exec = vc4_first_render_job(vc4);
159 struct vc4_exec_info *nextbin, *nextrender;
164 vc4->finished_seqno++;
165 list_move_tail(&exec->head, &vc4->job_done_list);
167 nextbin = vc4_first_bin_job(vc4);
168 nextrender = vc4_first_render_job(vc4);
170 /* Only stop the perfmon if following jobs in the queue don't expect it
173 if (exec->perfmon && !nextrender &&
174 (!nextbin || nextbin->perfmon != exec->perfmon))
175 vc4_perfmon_stop(vc4, exec->perfmon, true);
177 /* If there's a render job waiting, start it. If this is not the case
178 * we may have to unblock the binner if it's been stalled because of
179 * perfmon (this can be checked by comparing the perfmon attached to
180 * the finished renderjob to the one attached to the next bin job: if
181 * they don't match, this means the binner is stalled and should be
185 vc4_submit_next_render_job(dev);
186 else if (nextbin && nextbin->perfmon != exec->perfmon)
187 vc4_submit_next_bin_job(dev);
190 dma_fence_signal_locked(exec->fence);
191 dma_fence_put(exec->fence);
195 wake_up_all(&vc4->job_wait_queue);
196 schedule_work(&vc4->job_done_work);
200 vc4_irq(int irq, void *arg)
202 struct drm_device *dev = arg;
203 struct vc4_dev *vc4 = to_vc4_dev(dev);
205 irqreturn_t status = IRQ_NONE;
208 intctl = V3D_READ(V3D_INTCTL);
210 /* Acknowledge the interrupts we're handling here. The binner
211 * last flush / render frame done interrupt will be cleared,
212 * while OUTOMEM will stay high until the underlying cause is
215 V3D_WRITE(V3D_INTCTL, intctl);
217 if (intctl & V3D_INT_OUTOMEM) {
218 /* Disable OUTOMEM until the work is done. */
219 V3D_WRITE(V3D_INTDIS, V3D_INT_OUTOMEM);
220 schedule_work(&vc4->overflow_mem_work);
221 status = IRQ_HANDLED;
224 if (intctl & V3D_INT_FLDONE) {
225 spin_lock(&vc4->job_lock);
226 vc4_irq_finish_bin_job(dev);
227 spin_unlock(&vc4->job_lock);
228 status = IRQ_HANDLED;
231 if (intctl & V3D_INT_FRDONE) {
232 spin_lock(&vc4->job_lock);
233 vc4_irq_finish_render_job(dev);
234 spin_unlock(&vc4->job_lock);
235 status = IRQ_HANDLED;
242 vc4_irq_prepare(struct drm_device *dev)
244 struct vc4_dev *vc4 = to_vc4_dev(dev);
249 init_waitqueue_head(&vc4->job_wait_queue);
250 INIT_WORK(&vc4->overflow_mem_work, vc4_overflow_mem_work);
252 /* Clear any pending interrupts someone might have left around
255 V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
259 vc4_irq_enable(struct drm_device *dev)
261 struct vc4_dev *vc4 = to_vc4_dev(dev);
263 if (WARN_ON_ONCE(vc4->is_vc5))
269 /* Enable the render done interrupts. The out-of-memory interrupt is
270 * enabled as soon as we have a binner BO allocated.
272 V3D_WRITE(V3D_INTENA, V3D_INT_FLDONE | V3D_INT_FRDONE);
276 vc4_irq_disable(struct drm_device *dev)
278 struct vc4_dev *vc4 = to_vc4_dev(dev);
280 if (WARN_ON_ONCE(vc4->is_vc5))
286 /* Disable sending interrupts for our driver's IRQs. */
287 V3D_WRITE(V3D_INTDIS, V3D_DRIVER_IRQS);
289 /* Clear any pending interrupts we might have left. */
290 V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
292 /* Finish any interrupt handler still in flight. */
293 synchronize_irq(vc4->irq);
295 cancel_work_sync(&vc4->overflow_mem_work);
298 int vc4_irq_install(struct drm_device *dev, int irq)
300 struct vc4_dev *vc4 = to_vc4_dev(dev);
303 if (WARN_ON_ONCE(vc4->is_vc5))
306 if (irq == IRQ_NOTCONNECTED)
309 vc4_irq_prepare(dev);
311 ret = request_irq(irq, vc4_irq, 0, dev->driver->name, dev);
320 void vc4_irq_uninstall(struct drm_device *dev)
322 struct vc4_dev *vc4 = to_vc4_dev(dev);
324 if (WARN_ON_ONCE(vc4->is_vc5))
327 vc4_irq_disable(dev);
328 free_irq(vc4->irq, dev);
331 /** Reinitializes interrupt registers when a GPU reset is performed. */
332 void vc4_irq_reset(struct drm_device *dev)
334 struct vc4_dev *vc4 = to_vc4_dev(dev);
335 unsigned long irqflags;
337 if (WARN_ON_ONCE(vc4->is_vc5))
340 /* Acknowledge any stale IRQs. */
341 V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
344 * Turn all our interrupts on. Binner out of memory is the
345 * only one we expect to trigger at this point, since we've
346 * just come from poweron and haven't supplied any overflow
349 V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
351 spin_lock_irqsave(&vc4->job_lock, irqflags);
352 vc4_cancel_bin_job(dev);
353 vc4_irq_finish_render_job(dev);
354 spin_unlock_irqrestore(&vc4->job_lock, irqflags);