1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2018 Broadcom */
5 * DOC: Broadcom V3D scheduling
7 * The shared DRM GPU scheduler is used to coordinate submitting jobs
8 * to the hardware. Each DRM fd (roughly a client process) gets its
9 * own scheduler entity, which will process jobs in order. The GPU
10 * scheduler will round-robin between clients to submit the next job.
12 * For simplicity, and in order to keep latency low for interactive
13 * jobs when bulk background jobs are queued up, we submit a new job
14 * to the HW only when it has completed the last one, instead of
15 * filling up the CT[01]Q FIFOs with jobs. Similarly, we use
16 * v3d_job_dependency() to manage the dependency between bin and
17 * render, instead of having the clients submit jobs using the HW's
18 * semaphores to interlock between them.
21 #include <linux/kthread.h>
25 #include "v3d_trace.h"
27 static struct v3d_job *
28 to_v3d_job(struct drm_sched_job *sched_job)
30 return container_of(sched_job, struct v3d_job, base);
33 static struct v3d_tfu_job *
34 to_tfu_job(struct drm_sched_job *sched_job)
36 return container_of(sched_job, struct v3d_tfu_job, base);
40 v3d_job_free(struct drm_sched_job *sched_job)
42 struct v3d_job *job = to_v3d_job(sched_job);
44 drm_sched_job_cleanup(sched_job);
46 v3d_exec_put(job->exec);
50 v3d_tfu_job_free(struct drm_sched_job *sched_job)
52 struct v3d_tfu_job *job = to_tfu_job(sched_job);
54 drm_sched_job_cleanup(sched_job);
60 * Returns the fences that the bin or render job depends on, one by one.
61 * v3d_job_run() won't be called until all of them have been signaled.
63 static struct dma_fence *
64 v3d_job_dependency(struct drm_sched_job *sched_job,
65 struct drm_sched_entity *s_entity)
67 struct v3d_job *job = to_v3d_job(sched_job);
68 struct v3d_exec_info *exec = job->exec;
69 enum v3d_queue q = job == &exec->bin ? V3D_BIN : V3D_RENDER;
70 struct dma_fence *fence;
72 fence = job->in_fence;
78 if (q == V3D_RENDER) {
79 /* If we had a bin job, the render job definitely depends on
80 * it. We first have to wait for bin to be scheduled, so that
81 * its done_fence is created.
83 fence = exec->bin_done_fence;
85 exec->bin_done_fence = NULL;
90 /* XXX: Wait on a fence for switching the GMP if necessary,
98 * Returns the fences that the TFU job depends on, one by one.
99 * v3d_tfu_job_run() won't be called until all of them have been
102 static struct dma_fence *
103 v3d_tfu_job_dependency(struct drm_sched_job *sched_job,
104 struct drm_sched_entity *s_entity)
106 struct v3d_tfu_job *job = to_tfu_job(sched_job);
107 struct dma_fence *fence;
109 fence = job->in_fence;
111 job->in_fence = NULL;
118 static struct dma_fence *v3d_job_run(struct drm_sched_job *sched_job)
120 struct v3d_job *job = to_v3d_job(sched_job);
121 struct v3d_exec_info *exec = job->exec;
122 enum v3d_queue q = job == &exec->bin ? V3D_BIN : V3D_RENDER;
123 struct v3d_dev *v3d = exec->v3d;
124 struct drm_device *dev = &v3d->drm;
125 struct dma_fence *fence;
126 unsigned long irqflags;
128 if (unlikely(job->base.s_fence->finished.error))
131 /* Lock required around bin_job update vs
132 * v3d_overflow_mem_work().
134 spin_lock_irqsave(&v3d->job_lock, irqflags);
136 v3d->bin_job = job->exec;
138 /* Clear out the overflow allocation, so we don't
139 * reuse the overflow attached to a previous job.
141 V3D_CORE_WRITE(0, V3D_PTB_BPOS, 0);
143 v3d->render_job = job->exec;
145 spin_unlock_irqrestore(&v3d->job_lock, irqflags);
147 /* Can we avoid this flush when q==RENDER? We need to be
148 * careful of scheduling, though -- imagine job0 rendering to
149 * texture and job1 reading, and them being executed as bin0,
150 * bin1, render0, render1, so that render1's flush at bin time
153 v3d_invalidate_caches(v3d);
155 fence = v3d_fence_create(v3d, q);
160 dma_fence_put(job->done_fence);
161 job->done_fence = dma_fence_get(fence);
163 trace_v3d_submit_cl(dev, q == V3D_RENDER, to_v3d_fence(fence)->seqno,
164 job->start, job->end);
168 V3D_CORE_WRITE(0, V3D_CLE_CT0QMA, exec->qma);
169 V3D_CORE_WRITE(0, V3D_CLE_CT0QMS, exec->qms);
172 V3D_CORE_WRITE(0, V3D_CLE_CT0QTS,
173 V3D_CLE_CT0QTS_ENABLE |
177 /* XXX: Set the QCFG */
180 /* Set the current and end address of the control list.
181 * Writing the end register is what starts the job.
183 V3D_CORE_WRITE(0, V3D_CLE_CTNQBA(q), job->start);
184 V3D_CORE_WRITE(0, V3D_CLE_CTNQEA(q), job->end);
189 static struct dma_fence *
190 v3d_tfu_job_run(struct drm_sched_job *sched_job)
192 struct v3d_tfu_job *job = to_tfu_job(sched_job);
193 struct v3d_dev *v3d = job->v3d;
194 struct drm_device *dev = &v3d->drm;
195 struct dma_fence *fence;
197 fence = v3d_fence_create(v3d, V3D_TFU);
203 dma_fence_put(job->done_fence);
204 job->done_fence = dma_fence_get(fence);
206 trace_v3d_submit_tfu(dev, to_v3d_fence(fence)->seqno);
208 V3D_WRITE(V3D_TFU_IIA, job->args.iia);
209 V3D_WRITE(V3D_TFU_IIS, job->args.iis);
210 V3D_WRITE(V3D_TFU_ICA, job->args.ica);
211 V3D_WRITE(V3D_TFU_IUA, job->args.iua);
212 V3D_WRITE(V3D_TFU_IOA, job->args.ioa);
213 V3D_WRITE(V3D_TFU_IOS, job->args.ios);
214 V3D_WRITE(V3D_TFU_COEF0, job->args.coef[0]);
215 if (job->args.coef[0] & V3D_TFU_COEF0_USECOEF) {
216 V3D_WRITE(V3D_TFU_COEF1, job->args.coef[1]);
217 V3D_WRITE(V3D_TFU_COEF2, job->args.coef[2]);
218 V3D_WRITE(V3D_TFU_COEF3, job->args.coef[3]);
220 /* ICFG kicks off the job. */
221 V3D_WRITE(V3D_TFU_ICFG, job->args.icfg | V3D_TFU_ICFG_IOC);
227 v3d_gpu_reset_for_timeout(struct v3d_dev *v3d, struct drm_sched_job *sched_job)
231 mutex_lock(&v3d->reset_lock);
233 /* block scheduler */
234 for (q = 0; q < V3D_MAX_QUEUES; q++) {
235 struct drm_gpu_scheduler *sched = &v3d->queue[q].sched;
237 kthread_park(sched->thread);
238 drm_sched_hw_job_reset(sched, (sched_job->sched == sched ?
242 /* get the GPU back into the init state */
245 /* Unblock schedulers and restart their jobs. */
246 for (q = 0; q < V3D_MAX_QUEUES; q++) {
247 drm_sched_job_recovery(&v3d->queue[q].sched);
248 kthread_unpark(v3d->queue[q].sched.thread);
251 mutex_unlock(&v3d->reset_lock);
255 v3d_job_timedout(struct drm_sched_job *sched_job)
257 struct v3d_job *job = to_v3d_job(sched_job);
258 struct v3d_exec_info *exec = job->exec;
259 struct v3d_dev *v3d = exec->v3d;
260 enum v3d_queue job_q = job == &exec->bin ? V3D_BIN : V3D_RENDER;
261 u32 ctca = V3D_CORE_READ(0, V3D_CLE_CTNCA(job_q));
262 u32 ctra = V3D_CORE_READ(0, V3D_CLE_CTNRA(job_q));
264 /* If the current address or return address have changed, then
265 * the GPU has probably made progress and we should delay the
266 * reset. This could fail if the GPU got in an infinite loop
267 * in the CL, but that is pretty unlikely outside of an i-g-t
270 if (job->timedout_ctca != ctca || job->timedout_ctra != ctra) {
271 job->timedout_ctca = ctca;
272 job->timedout_ctra = ctra;
276 v3d_gpu_reset_for_timeout(v3d, sched_job);
280 v3d_tfu_job_timedout(struct drm_sched_job *sched_job)
282 struct v3d_tfu_job *job = to_tfu_job(sched_job);
284 v3d_gpu_reset_for_timeout(job->v3d, sched_job);
287 static const struct drm_sched_backend_ops v3d_sched_ops = {
288 .dependency = v3d_job_dependency,
289 .run_job = v3d_job_run,
290 .timedout_job = v3d_job_timedout,
291 .free_job = v3d_job_free
294 static const struct drm_sched_backend_ops v3d_tfu_sched_ops = {
295 .dependency = v3d_tfu_job_dependency,
296 .run_job = v3d_tfu_job_run,
297 .timedout_job = v3d_tfu_job_timedout,
298 .free_job = v3d_tfu_job_free
302 v3d_sched_init(struct v3d_dev *v3d)
304 int hw_jobs_limit = 1;
305 int job_hang_limit = 0;
306 int hang_limit_ms = 500;
309 ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
311 hw_jobs_limit, job_hang_limit,
312 msecs_to_jiffies(hang_limit_ms),
315 dev_err(v3d->dev, "Failed to create bin scheduler: %d.", ret);
319 ret = drm_sched_init(&v3d->queue[V3D_RENDER].sched,
321 hw_jobs_limit, job_hang_limit,
322 msecs_to_jiffies(hang_limit_ms),
325 dev_err(v3d->dev, "Failed to create render scheduler: %d.",
327 drm_sched_fini(&v3d->queue[V3D_BIN].sched);
331 ret = drm_sched_init(&v3d->queue[V3D_TFU].sched,
333 hw_jobs_limit, job_hang_limit,
334 msecs_to_jiffies(hang_limit_ms),
337 dev_err(v3d->dev, "Failed to create TFU scheduler: %d.",
339 drm_sched_fini(&v3d->queue[V3D_RENDER].sched);
340 drm_sched_fini(&v3d->queue[V3D_BIN].sched);
348 v3d_sched_fini(struct v3d_dev *v3d)
352 for (q = 0; q < V3D_MAX_QUEUES; q++)
353 drm_sched_fini(&v3d->queue[q].sched);