xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/vc4/vc4_irq.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2014 Broadcom
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*4882a593Smuzhiyun  * IN THE SOFTWARE.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun  * DOC: Interrupt management for the V3D engine
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * We have an interrupt status register (V3D_INTCTL) which reports
28*4882a593Smuzhiyun  * interrupts, and where writing 1 bits clears those interrupts.
29*4882a593Smuzhiyun  * There are also a pair of interrupt registers
30*4882a593Smuzhiyun  * (V3D_INTENA/V3D_INTDIS) where writing a 1 to their bits enables or
31*4882a593Smuzhiyun  * disables that specific interrupt, and 0s written are ignored
32*4882a593Smuzhiyun  * (reading either one returns the set of enabled interrupts).
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * When we take a binning flush done interrupt, we need to submit the
35*4882a593Smuzhiyun  * next frame for binning and move the finished frame to the render
36*4882a593Smuzhiyun  * thread.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * When we take a render frame interrupt, we need to wake the
39*4882a593Smuzhiyun  * processes waiting for some frame to be done, and get the next frame
40*4882a593Smuzhiyun  * submitted ASAP (so the hardware doesn't sit idle when there's work
41*4882a593Smuzhiyun  * to do).
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * When we take the binner out of memory interrupt, we need to
44*4882a593Smuzhiyun  * allocate some new memory and pass it to the binner so that the
45*4882a593Smuzhiyun  * current job can make progress.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include "vc4_drv.h"
49*4882a593Smuzhiyun #include "vc4_regs.h"
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define V3D_DRIVER_IRQS (V3D_INT_OUTOMEM | \
52*4882a593Smuzhiyun 			 V3D_INT_FLDONE | \
53*4882a593Smuzhiyun 			 V3D_INT_FRDONE)
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun DECLARE_WAIT_QUEUE_HEAD(render_wait);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static void
vc4_overflow_mem_work(struct work_struct * work)58*4882a593Smuzhiyun vc4_overflow_mem_work(struct work_struct *work)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	struct vc4_dev *vc4 =
61*4882a593Smuzhiyun 		container_of(work, struct vc4_dev, overflow_mem_work);
62*4882a593Smuzhiyun 	struct vc4_bo *bo;
63*4882a593Smuzhiyun 	int bin_bo_slot;
64*4882a593Smuzhiyun 	struct vc4_exec_info *exec;
65*4882a593Smuzhiyun 	unsigned long irqflags;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	mutex_lock(&vc4->bin_bo_lock);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	if (!vc4->bin_bo)
70*4882a593Smuzhiyun 		goto complete;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	bo = vc4->bin_bo;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	bin_bo_slot = vc4_v3d_get_bin_slot(vc4);
75*4882a593Smuzhiyun 	if (bin_bo_slot < 0) {
76*4882a593Smuzhiyun 		DRM_ERROR("Couldn't allocate binner overflow mem\n");
77*4882a593Smuzhiyun 		goto complete;
78*4882a593Smuzhiyun 	}
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	spin_lock_irqsave(&vc4->job_lock, irqflags);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	if (vc4->bin_alloc_overflow) {
83*4882a593Smuzhiyun 		/* If we had overflow memory allocated previously,
84*4882a593Smuzhiyun 		 * then that chunk will free when the current bin job
85*4882a593Smuzhiyun 		 * is done.  If we don't have a bin job running, then
86*4882a593Smuzhiyun 		 * the chunk will be done whenever the list of render
87*4882a593Smuzhiyun 		 * jobs has drained.
88*4882a593Smuzhiyun 		 */
89*4882a593Smuzhiyun 		exec = vc4_first_bin_job(vc4);
90*4882a593Smuzhiyun 		if (!exec)
91*4882a593Smuzhiyun 			exec = vc4_last_render_job(vc4);
92*4882a593Smuzhiyun 		if (exec) {
93*4882a593Smuzhiyun 			exec->bin_slots |= vc4->bin_alloc_overflow;
94*4882a593Smuzhiyun 		} else {
95*4882a593Smuzhiyun 			/* There's nothing queued in the hardware, so
96*4882a593Smuzhiyun 			 * the old slot is free immediately.
97*4882a593Smuzhiyun 			 */
98*4882a593Smuzhiyun 			vc4->bin_alloc_used &= ~vc4->bin_alloc_overflow;
99*4882a593Smuzhiyun 		}
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 	vc4->bin_alloc_overflow = BIT(bin_bo_slot);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	V3D_WRITE(V3D_BPOA, bo->base.paddr + bin_bo_slot * vc4->bin_alloc_size);
104*4882a593Smuzhiyun 	V3D_WRITE(V3D_BPOS, bo->base.base.size);
105*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
106*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
107*4882a593Smuzhiyun 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun complete:
110*4882a593Smuzhiyun 	mutex_unlock(&vc4->bin_bo_lock);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun static void
vc4_irq_finish_bin_job(struct drm_device * dev)114*4882a593Smuzhiyun vc4_irq_finish_bin_job(struct drm_device *dev)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
117*4882a593Smuzhiyun 	struct vc4_exec_info *next, *exec = vc4_first_bin_job(vc4);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (!exec)
120*4882a593Smuzhiyun 		return;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	vc4_move_job_to_render(dev, exec);
123*4882a593Smuzhiyun 	next = vc4_first_bin_job(vc4);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* Only submit the next job in the bin list if it matches the perfmon
126*4882a593Smuzhiyun 	 * attached to the one that just finished (or if both jobs don't have
127*4882a593Smuzhiyun 	 * perfmon attached to them).
128*4882a593Smuzhiyun 	 */
129*4882a593Smuzhiyun 	if (next && next->perfmon == exec->perfmon)
130*4882a593Smuzhiyun 		vc4_submit_next_bin_job(dev);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static void
vc4_cancel_bin_job(struct drm_device * dev)134*4882a593Smuzhiyun vc4_cancel_bin_job(struct drm_device *dev)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
137*4882a593Smuzhiyun 	struct vc4_exec_info *exec = vc4_first_bin_job(vc4);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (!exec)
140*4882a593Smuzhiyun 		return;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* Stop the perfmon so that the next bin job can be started. */
143*4882a593Smuzhiyun 	if (exec->perfmon)
144*4882a593Smuzhiyun 		vc4_perfmon_stop(vc4, exec->perfmon, false);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	list_move_tail(&exec->head, &vc4->bin_job_list);
147*4882a593Smuzhiyun 	vc4_submit_next_bin_job(dev);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun static void
vc4_irq_finish_render_job(struct drm_device * dev)151*4882a593Smuzhiyun vc4_irq_finish_render_job(struct drm_device *dev)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
154*4882a593Smuzhiyun 	struct vc4_exec_info *exec = vc4_first_render_job(vc4);
155*4882a593Smuzhiyun 	struct vc4_exec_info *nextbin, *nextrender;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	if (!exec)
158*4882a593Smuzhiyun 		return;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	vc4->finished_seqno++;
161*4882a593Smuzhiyun 	list_move_tail(&exec->head, &vc4->job_done_list);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	nextbin = vc4_first_bin_job(vc4);
164*4882a593Smuzhiyun 	nextrender = vc4_first_render_job(vc4);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/* Only stop the perfmon if following jobs in the queue don't expect it
167*4882a593Smuzhiyun 	 * to be enabled.
168*4882a593Smuzhiyun 	 */
169*4882a593Smuzhiyun 	if (exec->perfmon && !nextrender &&
170*4882a593Smuzhiyun 	    (!nextbin || nextbin->perfmon != exec->perfmon))
171*4882a593Smuzhiyun 		vc4_perfmon_stop(vc4, exec->perfmon, true);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	/* If there's a render job waiting, start it. If this is not the case
174*4882a593Smuzhiyun 	 * we may have to unblock the binner if it's been stalled because of
175*4882a593Smuzhiyun 	 * perfmon (this can be checked by comparing the perfmon attached to
176*4882a593Smuzhiyun 	 * the finished renderjob to the one attached to the next bin job: if
177*4882a593Smuzhiyun 	 * they don't match, this means the binner is stalled and should be
178*4882a593Smuzhiyun 	 * restarted).
179*4882a593Smuzhiyun 	 */
180*4882a593Smuzhiyun 	if (nextrender)
181*4882a593Smuzhiyun 		vc4_submit_next_render_job(dev);
182*4882a593Smuzhiyun 	else if (nextbin && nextbin->perfmon != exec->perfmon)
183*4882a593Smuzhiyun 		vc4_submit_next_bin_job(dev);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (exec->fence) {
186*4882a593Smuzhiyun 		dma_fence_signal_locked(exec->fence);
187*4882a593Smuzhiyun 		dma_fence_put(exec->fence);
188*4882a593Smuzhiyun 		exec->fence = NULL;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	wake_up_all(&vc4->job_wait_queue);
192*4882a593Smuzhiyun 	schedule_work(&vc4->job_done_work);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun irqreturn_t
vc4_irq(int irq,void * arg)196*4882a593Smuzhiyun vc4_irq(int irq, void *arg)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	struct drm_device *dev = arg;
199*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
200*4882a593Smuzhiyun 	uint32_t intctl;
201*4882a593Smuzhiyun 	irqreturn_t status = IRQ_NONE;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	barrier();
204*4882a593Smuzhiyun 	intctl = V3D_READ(V3D_INTCTL);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/* Acknowledge the interrupts we're handling here. The binner
207*4882a593Smuzhiyun 	 * last flush / render frame done interrupt will be cleared,
208*4882a593Smuzhiyun 	 * while OUTOMEM will stay high until the underlying cause is
209*4882a593Smuzhiyun 	 * cleared.
210*4882a593Smuzhiyun 	 */
211*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTCTL, intctl);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (intctl & V3D_INT_OUTOMEM) {
214*4882a593Smuzhiyun 		/* Disable OUTOMEM until the work is done. */
215*4882a593Smuzhiyun 		V3D_WRITE(V3D_INTDIS, V3D_INT_OUTOMEM);
216*4882a593Smuzhiyun 		schedule_work(&vc4->overflow_mem_work);
217*4882a593Smuzhiyun 		status = IRQ_HANDLED;
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	if (intctl & V3D_INT_FLDONE) {
221*4882a593Smuzhiyun 		spin_lock(&vc4->job_lock);
222*4882a593Smuzhiyun 		vc4_irq_finish_bin_job(dev);
223*4882a593Smuzhiyun 		spin_unlock(&vc4->job_lock);
224*4882a593Smuzhiyun 		status = IRQ_HANDLED;
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if (intctl & V3D_INT_FRDONE) {
228*4882a593Smuzhiyun 		spin_lock(&vc4->job_lock);
229*4882a593Smuzhiyun 		vc4_irq_finish_render_job(dev);
230*4882a593Smuzhiyun 		spin_unlock(&vc4->job_lock);
231*4882a593Smuzhiyun 		status = IRQ_HANDLED;
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return status;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun void
vc4_irq_preinstall(struct drm_device * dev)238*4882a593Smuzhiyun vc4_irq_preinstall(struct drm_device *dev)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	if (!vc4->v3d)
243*4882a593Smuzhiyun 		return;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	init_waitqueue_head(&vc4->job_wait_queue);
246*4882a593Smuzhiyun 	INIT_WORK(&vc4->overflow_mem_work, vc4_overflow_mem_work);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/* Clear any pending interrupts someone might have left around
249*4882a593Smuzhiyun 	 * for us.
250*4882a593Smuzhiyun 	 */
251*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun int
vc4_irq_postinstall(struct drm_device * dev)255*4882a593Smuzhiyun vc4_irq_postinstall(struct drm_device *dev)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	if (!vc4->v3d)
260*4882a593Smuzhiyun 		return 0;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Enable the render done interrupts. The out-of-memory interrupt is
263*4882a593Smuzhiyun 	 * enabled as soon as we have a binner BO allocated.
264*4882a593Smuzhiyun 	 */
265*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTENA, V3D_INT_FLDONE | V3D_INT_FRDONE);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return 0;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun void
vc4_irq_uninstall(struct drm_device * dev)271*4882a593Smuzhiyun vc4_irq_uninstall(struct drm_device *dev)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (!vc4->v3d)
276*4882a593Smuzhiyun 		return;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	/* Disable sending interrupts for our driver's IRQs. */
279*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTDIS, V3D_DRIVER_IRQS);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* Clear any pending interrupts we might have left. */
282*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	/* Finish any interrupt handler still in flight. */
285*4882a593Smuzhiyun 	disable_irq(dev->irq);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	cancel_work_sync(&vc4->overflow_mem_work);
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun /** Reinitializes interrupt registers when a GPU reset is performed. */
vc4_irq_reset(struct drm_device * dev)291*4882a593Smuzhiyun void vc4_irq_reset(struct drm_device *dev)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct vc4_dev *vc4 = to_vc4_dev(dev);
294*4882a593Smuzhiyun 	unsigned long irqflags;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* Acknowledge any stale IRQs. */
297*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	/*
300*4882a593Smuzhiyun 	 * Turn all our interrupts on.  Binner out of memory is the
301*4882a593Smuzhiyun 	 * only one we expect to trigger at this point, since we've
302*4882a593Smuzhiyun 	 * just come from poweron and haven't supplied any overflow
303*4882a593Smuzhiyun 	 * memory yet.
304*4882a593Smuzhiyun 	 */
305*4882a593Smuzhiyun 	V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	spin_lock_irqsave(&vc4->job_lock, irqflags);
308*4882a593Smuzhiyun 	vc4_cancel_bin_job(dev);
309*4882a593Smuzhiyun 	vc4_irq_finish_render_job(dev);
310*4882a593Smuzhiyun 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
311*4882a593Smuzhiyun }
312