at commit c03c1a14

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