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 * drm_sched_job_add_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/sched/clock.h>
22 #include <linux/kthread.h>
24 #include <drm/drm_syncobj.h>
28 #include "v3d_trace.h"
30 #define V3D_CSD_CFG012_WG_COUNT_SHIFT 16
32 static struct v3d_job *
33 to_v3d_job(struct drm_sched_job *sched_job)
35 return container_of(sched_job, struct v3d_job, base);
38 static struct v3d_bin_job *
39 to_bin_job(struct drm_sched_job *sched_job)
41 return container_of(sched_job, struct v3d_bin_job, base.base);
44 static struct v3d_render_job *
45 to_render_job(struct drm_sched_job *sched_job)
47 return container_of(sched_job, struct v3d_render_job, base.base);
50 static struct v3d_tfu_job *
51 to_tfu_job(struct drm_sched_job *sched_job)
53 return container_of(sched_job, struct v3d_tfu_job, base.base);
56 static struct v3d_csd_job *
57 to_csd_job(struct drm_sched_job *sched_job)
59 return container_of(sched_job, struct v3d_csd_job, base.base);
62 static struct v3d_cpu_job *
63 to_cpu_job(struct drm_sched_job *sched_job)
65 return container_of(sched_job, struct v3d_cpu_job, base.base);
69 v3d_sched_job_free(struct drm_sched_job *sched_job)
71 struct v3d_job *job = to_v3d_job(sched_job);
77 v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
80 if (query_info->queries) {
83 for (i = 0; i < count; i++)
84 drm_syncobj_put(query_info->queries[i].syncobj);
86 kvfree(query_info->queries);
91 v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
94 if (query_info->queries) {
97 for (i = 0; i < count; i++) {
98 drm_syncobj_put(query_info->queries[i].syncobj);
99 kvfree(query_info->queries[i].kperfmon_ids);
102 kvfree(query_info->queries);
107 v3d_cpu_job_free(struct drm_sched_job *sched_job)
109 struct v3d_cpu_job *job = to_cpu_job(sched_job);
111 v3d_timestamp_query_info_free(&job->timestamp_query,
112 job->timestamp_query.count);
114 v3d_performance_query_info_free(&job->performance_query,
115 job->performance_query.count);
117 v3d_job_cleanup(&job->base);
121 v3d_switch_perfmon(struct v3d_dev *v3d, struct v3d_job *job)
123 if (job->perfmon != v3d->active_perfmon)
124 v3d_perfmon_stop(v3d, v3d->active_perfmon, true);
126 if (job->perfmon && v3d->active_perfmon != job->perfmon)
127 v3d_perfmon_start(v3d, job->perfmon);
131 v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
133 struct v3d_dev *v3d = job->v3d;
134 struct v3d_file_priv *file = job->file->driver_priv;
135 struct v3d_stats *global_stats = &v3d->queue[queue].stats;
136 struct v3d_stats *local_stats = &file->stats[queue];
137 u64 now = local_clock();
141 * We only need to disable local interrupts to appease lockdep who
142 * otherwise would think v3d_job_start_stats vs v3d_stats_update has an
143 * unsafe in-irq vs no-irq-off usage problem. This is a false positive
144 * because all the locks are per queue and stats type, and all jobs are
145 * completely one at a time serialised. More specifically:
147 * 1. Locks for GPU queues are updated from interrupt handlers under a
148 * spin lock and started here with preemption disabled.
150 * 2. Locks for CPU queues are updated from the worker with preemption
151 * disabled and equally started here with preemption disabled.
153 * Therefore both are consistent.
155 * 3. Because next job can only be queued after the previous one has
156 * been signaled, and locks are per queue, there is also no scope for
157 * the start part to race with the update part.
159 if (IS_ENABLED(CONFIG_LOCKDEP))
160 local_irq_save(flags);
164 write_seqcount_begin(&local_stats->lock);
165 local_stats->start_ns = now;
166 write_seqcount_end(&local_stats->lock);
168 write_seqcount_begin(&global_stats->lock);
169 global_stats->start_ns = now;
170 write_seqcount_end(&global_stats->lock);
172 if (IS_ENABLED(CONFIG_LOCKDEP))
173 local_irq_restore(flags);
179 v3d_stats_update(struct v3d_stats *stats, u64 now)
181 write_seqcount_begin(&stats->lock);
182 stats->enabled_ns += now - stats->start_ns;
183 stats->jobs_completed++;
185 write_seqcount_end(&stats->lock);
189 v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
191 struct v3d_dev *v3d = job->v3d;
192 struct v3d_file_priv *file = job->file->driver_priv;
193 struct v3d_stats *global_stats = &v3d->queue[queue].stats;
194 struct v3d_stats *local_stats = &file->stats[queue];
195 u64 now = local_clock();
198 /* See comment in v3d_job_start_stats() */
199 if (IS_ENABLED(CONFIG_LOCKDEP))
200 local_irq_save(flags);
204 v3d_stats_update(local_stats, now);
205 v3d_stats_update(global_stats, now);
207 if (IS_ENABLED(CONFIG_LOCKDEP))
208 local_irq_restore(flags);
213 static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
215 struct v3d_bin_job *job = to_bin_job(sched_job);
216 struct v3d_dev *v3d = job->base.v3d;
217 struct drm_device *dev = &v3d->drm;
218 struct dma_fence *fence;
219 unsigned long irqflags;
221 if (unlikely(job->base.base.s_fence->finished.error))
224 /* Lock required around bin_job update vs
225 * v3d_overflow_mem_work().
227 spin_lock_irqsave(&v3d->job_lock, irqflags);
229 /* Clear out the overflow allocation, so we don't
230 * reuse the overflow attached to a previous job.
232 V3D_CORE_WRITE(0, V3D_PTB_BPOS, 0);
233 spin_unlock_irqrestore(&v3d->job_lock, irqflags);
235 v3d_invalidate_caches(v3d);
237 fence = v3d_fence_create(v3d, V3D_BIN);
241 if (job->base.irq_fence)
242 dma_fence_put(job->base.irq_fence);
243 job->base.irq_fence = dma_fence_get(fence);
245 trace_v3d_submit_cl(dev, false, to_v3d_fence(fence)->seqno,
246 job->start, job->end);
248 v3d_job_start_stats(&job->base, V3D_BIN);
249 v3d_switch_perfmon(v3d, &job->base);
251 /* Set the current and end address of the control list.
252 * Writing the end register is what starts the job.
255 V3D_CORE_WRITE(0, V3D_CLE_CT0QMA, job->qma);
256 V3D_CORE_WRITE(0, V3D_CLE_CT0QMS, job->qms);
259 V3D_CORE_WRITE(0, V3D_CLE_CT0QTS,
260 V3D_CLE_CT0QTS_ENABLE |
263 V3D_CORE_WRITE(0, V3D_CLE_CT0QBA, job->start);
264 V3D_CORE_WRITE(0, V3D_CLE_CT0QEA, job->end);
269 static struct dma_fence *v3d_render_job_run(struct drm_sched_job *sched_job)
271 struct v3d_render_job *job = to_render_job(sched_job);
272 struct v3d_dev *v3d = job->base.v3d;
273 struct drm_device *dev = &v3d->drm;
274 struct dma_fence *fence;
276 if (unlikely(job->base.base.s_fence->finished.error))
279 v3d->render_job = job;
281 /* Can we avoid this flush? We need to be careful of
282 * scheduling, though -- imagine job0 rendering to texture and
283 * job1 reading, and them being executed as bin0, bin1,
284 * render0, render1, so that render1's flush at bin time
287 v3d_invalidate_caches(v3d);
289 fence = v3d_fence_create(v3d, V3D_RENDER);
293 if (job->base.irq_fence)
294 dma_fence_put(job->base.irq_fence);
295 job->base.irq_fence = dma_fence_get(fence);
297 trace_v3d_submit_cl(dev, true, to_v3d_fence(fence)->seqno,
298 job->start, job->end);
300 v3d_job_start_stats(&job->base, V3D_RENDER);
301 v3d_switch_perfmon(v3d, &job->base);
303 /* XXX: Set the QCFG */
305 /* Set the current and end address of the control list.
306 * Writing the end register is what starts the job.
308 V3D_CORE_WRITE(0, V3D_CLE_CT1QBA, job->start);
309 V3D_CORE_WRITE(0, V3D_CLE_CT1QEA, job->end);
314 static struct dma_fence *
315 v3d_tfu_job_run(struct drm_sched_job *sched_job)
317 struct v3d_tfu_job *job = to_tfu_job(sched_job);
318 struct v3d_dev *v3d = job->base.v3d;
319 struct drm_device *dev = &v3d->drm;
320 struct dma_fence *fence;
322 fence = v3d_fence_create(v3d, V3D_TFU);
327 if (job->base.irq_fence)
328 dma_fence_put(job->base.irq_fence);
329 job->base.irq_fence = dma_fence_get(fence);
331 trace_v3d_submit_tfu(dev, to_v3d_fence(fence)->seqno);
333 v3d_job_start_stats(&job->base, V3D_TFU);
335 V3D_WRITE(V3D_TFU_IIA(v3d->ver), job->args.iia);
336 V3D_WRITE(V3D_TFU_IIS(v3d->ver), job->args.iis);
337 V3D_WRITE(V3D_TFU_ICA(v3d->ver), job->args.ica);
338 V3D_WRITE(V3D_TFU_IUA(v3d->ver), job->args.iua);
339 V3D_WRITE(V3D_TFU_IOA(v3d->ver), job->args.ioa);
341 V3D_WRITE(V3D_V7_TFU_IOC, job->args.v71.ioc);
342 V3D_WRITE(V3D_TFU_IOS(v3d->ver), job->args.ios);
343 V3D_WRITE(V3D_TFU_COEF0(v3d->ver), job->args.coef[0]);
344 if (v3d->ver >= 71 || (job->args.coef[0] & V3D_TFU_COEF0_USECOEF)) {
345 V3D_WRITE(V3D_TFU_COEF1(v3d->ver), job->args.coef[1]);
346 V3D_WRITE(V3D_TFU_COEF2(v3d->ver), job->args.coef[2]);
347 V3D_WRITE(V3D_TFU_COEF3(v3d->ver), job->args.coef[3]);
349 /* ICFG kicks off the job. */
350 V3D_WRITE(V3D_TFU_ICFG(v3d->ver), job->args.icfg | V3D_TFU_ICFG_IOC);
355 static struct dma_fence *
356 v3d_csd_job_run(struct drm_sched_job *sched_job)
358 struct v3d_csd_job *job = to_csd_job(sched_job);
359 struct v3d_dev *v3d = job->base.v3d;
360 struct drm_device *dev = &v3d->drm;
361 struct dma_fence *fence;
366 v3d_invalidate_caches(v3d);
368 fence = v3d_fence_create(v3d, V3D_CSD);
372 if (job->base.irq_fence)
373 dma_fence_put(job->base.irq_fence);
374 job->base.irq_fence = dma_fence_get(fence);
376 trace_v3d_submit_csd(dev, to_v3d_fence(fence)->seqno);
378 v3d_job_start_stats(&job->base, V3D_CSD);
379 v3d_switch_perfmon(v3d, &job->base);
381 csd_cfg0_reg = V3D_CSD_QUEUED_CFG0(v3d->ver);
382 for (i = 1; i <= 6; i++)
383 V3D_CORE_WRITE(0, csd_cfg0_reg + 4 * i, job->args.cfg[i]);
385 /* Although V3D 7.1 has an eighth configuration register, we are not
386 * using it. Therefore, make sure it remains unused.
388 * XXX: Set the CFG7 register
391 V3D_CORE_WRITE(0, V3D_V7_CSD_QUEUED_CFG7, 0);
393 /* CFG0 write kicks off the job. */
394 V3D_CORE_WRITE(0, csd_cfg0_reg, job->args.cfg[0]);
400 v3d_rewrite_csd_job_wg_counts_from_indirect(struct v3d_cpu_job *job)
402 struct v3d_indirect_csd_info *indirect_csd = &job->indirect_csd;
403 struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
404 struct v3d_bo *indirect = to_v3d_bo(indirect_csd->indirect);
405 struct drm_v3d_submit_csd *args = &indirect_csd->job->args;
408 v3d_get_bo_vaddr(bo);
409 v3d_get_bo_vaddr(indirect);
411 wg_counts = (uint32_t *)(bo->vaddr + indirect_csd->offset);
413 if (wg_counts[0] == 0 || wg_counts[1] == 0 || wg_counts[2] == 0)
416 args->cfg[0] = wg_counts[0] << V3D_CSD_CFG012_WG_COUNT_SHIFT;
417 args->cfg[1] = wg_counts[1] << V3D_CSD_CFG012_WG_COUNT_SHIFT;
418 args->cfg[2] = wg_counts[2] << V3D_CSD_CFG012_WG_COUNT_SHIFT;
419 args->cfg[4] = DIV_ROUND_UP(indirect_csd->wg_size, 16) *
420 (wg_counts[0] * wg_counts[1] * wg_counts[2]) - 1;
422 for (int i = 0; i < 3; i++) {
423 /* 0xffffffff indicates that the uniform rewrite is not needed */
424 if (indirect_csd->wg_uniform_offsets[i] != 0xffffffff) {
425 u32 uniform_idx = indirect_csd->wg_uniform_offsets[i];
426 ((uint32_t *)indirect->vaddr)[uniform_idx] = wg_counts[i];
430 v3d_put_bo_vaddr(indirect);
431 v3d_put_bo_vaddr(bo);
435 v3d_timestamp_query(struct v3d_cpu_job *job)
437 struct v3d_timestamp_query_info *timestamp_query = &job->timestamp_query;
438 struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
441 v3d_get_bo_vaddr(bo);
443 for (int i = 0; i < timestamp_query->count; i++) {
444 value_addr = ((u8 *)bo->vaddr) + timestamp_query->queries[i].offset;
445 *((u64 *)value_addr) = i == 0 ? ktime_get_ns() : 0ull;
447 drm_syncobj_replace_fence(timestamp_query->queries[i].syncobj,
448 job->base.done_fence);
451 v3d_put_bo_vaddr(bo);
455 v3d_reset_timestamp_queries(struct v3d_cpu_job *job)
457 struct v3d_timestamp_query_info *timestamp_query = &job->timestamp_query;
458 struct v3d_timestamp_query *queries = timestamp_query->queries;
459 struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
462 v3d_get_bo_vaddr(bo);
464 for (int i = 0; i < timestamp_query->count; i++) {
465 value_addr = ((u8 *)bo->vaddr) + queries[i].offset;
466 *((u64 *)value_addr) = 0;
468 drm_syncobj_replace_fence(queries[i].syncobj, NULL);
471 v3d_put_bo_vaddr(bo);
474 static void write_to_buffer_32(u32 *dst, unsigned int idx, u32 value)
479 static void write_to_buffer_64(u64 *dst, unsigned int idx, u64 value)
485 write_to_buffer(void *dst, unsigned int idx, bool do_64bit, u64 value)
488 write_to_buffer_64(dst, idx, value);
490 write_to_buffer_32(dst, idx, value);
494 v3d_copy_query_results(struct v3d_cpu_job *job)
496 struct v3d_timestamp_query_info *timestamp_query = &job->timestamp_query;
497 struct v3d_timestamp_query *queries = timestamp_query->queries;
498 struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
499 struct v3d_bo *timestamp = to_v3d_bo(job->base.bo[1]);
500 struct v3d_copy_query_results_info *copy = &job->copy;
501 struct dma_fence *fence;
503 bool available, write_result;
507 v3d_get_bo_vaddr(bo);
508 v3d_get_bo_vaddr(timestamp);
510 data = ((u8 *)bo->vaddr) + copy->offset;
512 for (i = 0; i < timestamp_query->count; i++) {
513 fence = drm_syncobj_fence_get(queries[i].syncobj);
514 available = fence ? dma_fence_is_signaled(fence) : false;
516 write_result = available || copy->do_partial;
518 query_addr = ((u8 *)timestamp->vaddr) + queries[i].offset;
519 write_to_buffer(data, 0, copy->do_64bit, *((u64 *)query_addr));
522 if (copy->availability_bit)
523 write_to_buffer(data, 1, copy->do_64bit, available ? 1u : 0u);
525 data += copy->stride;
527 dma_fence_put(fence);
530 v3d_put_bo_vaddr(timestamp);
531 v3d_put_bo_vaddr(bo);
535 v3d_reset_performance_queries(struct v3d_cpu_job *job)
537 struct v3d_performance_query_info *performance_query = &job->performance_query;
538 struct v3d_file_priv *v3d_priv = job->base.file->driver_priv;
539 struct v3d_dev *v3d = job->base.v3d;
540 struct v3d_perfmon *perfmon;
542 for (int i = 0; i < performance_query->count; i++) {
543 for (int j = 0; j < performance_query->nperfmons; j++) {
544 perfmon = v3d_perfmon_find(v3d_priv,
545 performance_query->queries[i].kperfmon_ids[j]);
547 DRM_DEBUG("Failed to find perfmon.");
551 v3d_perfmon_stop(v3d, perfmon, false);
553 memset(perfmon->values, 0, perfmon->ncounters * sizeof(u64));
555 v3d_perfmon_put(perfmon);
558 drm_syncobj_replace_fence(performance_query->queries[i].syncobj, NULL);
563 v3d_write_performance_query_result(struct v3d_cpu_job *job, void *data,
566 struct v3d_performance_query_info *performance_query =
567 &job->performance_query;
568 struct v3d_file_priv *v3d_priv = job->base.file->driver_priv;
569 struct v3d_performance_query *perf_query =
570 &performance_query->queries[query];
571 struct v3d_dev *v3d = job->base.v3d;
572 unsigned int i, j, offset;
574 for (i = 0, offset = 0;
575 i < performance_query->nperfmons;
576 i++, offset += DRM_V3D_MAX_PERF_COUNTERS) {
577 struct v3d_perfmon *perfmon;
579 perfmon = v3d_perfmon_find(v3d_priv,
580 perf_query->kperfmon_ids[i]);
582 DRM_DEBUG("Failed to find perfmon.");
586 v3d_perfmon_stop(v3d, perfmon, true);
588 if (job->copy.do_64bit) {
589 for (j = 0; j < perfmon->ncounters; j++)
590 write_to_buffer_64(data, offset + j,
593 for (j = 0; j < perfmon->ncounters; j++)
594 write_to_buffer_32(data, offset + j,
598 v3d_perfmon_put(perfmon);
603 v3d_copy_performance_query(struct v3d_cpu_job *job)
605 struct v3d_performance_query_info *performance_query = &job->performance_query;
606 struct v3d_copy_query_results_info *copy = &job->copy;
607 struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
608 struct dma_fence *fence;
609 bool available, write_result;
612 v3d_get_bo_vaddr(bo);
614 data = ((u8 *)bo->vaddr) + copy->offset;
616 for (int i = 0; i < performance_query->count; i++) {
617 fence = drm_syncobj_fence_get(performance_query->queries[i].syncobj);
618 available = fence ? dma_fence_is_signaled(fence) : false;
620 write_result = available || copy->do_partial;
622 v3d_write_performance_query_result(job, data, i);
624 if (copy->availability_bit)
625 write_to_buffer(data, performance_query->ncounters,
626 copy->do_64bit, available ? 1u : 0u);
628 data += copy->stride;
630 dma_fence_put(fence);
633 v3d_put_bo_vaddr(bo);
636 static const v3d_cpu_job_fn cpu_job_function[] = {
637 [V3D_CPU_JOB_TYPE_INDIRECT_CSD] = v3d_rewrite_csd_job_wg_counts_from_indirect,
638 [V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = v3d_timestamp_query,
639 [V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = v3d_reset_timestamp_queries,
640 [V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = v3d_copy_query_results,
641 [V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = v3d_reset_performance_queries,
642 [V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = v3d_copy_performance_query,
645 static struct dma_fence *
646 v3d_cpu_job_run(struct drm_sched_job *sched_job)
648 struct v3d_cpu_job *job = to_cpu_job(sched_job);
649 struct v3d_dev *v3d = job->base.v3d;
653 if (job->job_type >= ARRAY_SIZE(cpu_job_function)) {
654 DRM_DEBUG_DRIVER("Unknown CPU job: %d\n", job->job_type);
658 v3d_job_start_stats(&job->base, V3D_CPU);
659 trace_v3d_cpu_job_begin(&v3d->drm, job->job_type);
661 cpu_job_function[job->job_type](job);
663 trace_v3d_cpu_job_end(&v3d->drm, job->job_type);
664 v3d_job_update_stats(&job->base, V3D_CPU);
669 static struct dma_fence *
670 v3d_cache_clean_job_run(struct drm_sched_job *sched_job)
672 struct v3d_job *job = to_v3d_job(sched_job);
673 struct v3d_dev *v3d = job->v3d;
675 v3d_job_start_stats(job, V3D_CACHE_CLEAN);
677 v3d_clean_caches(v3d);
679 v3d_job_update_stats(job, V3D_CACHE_CLEAN);
684 static enum drm_gpu_sched_stat
685 v3d_gpu_reset_for_timeout(struct v3d_dev *v3d, struct drm_sched_job *sched_job)
689 mutex_lock(&v3d->reset_lock);
691 /* block scheduler */
692 for (q = 0; q < V3D_MAX_QUEUES; q++)
693 drm_sched_stop(&v3d->queue[q].sched, sched_job);
696 drm_sched_increase_karma(sched_job);
698 /* get the GPU back into the init state */
701 for (q = 0; q < V3D_MAX_QUEUES; q++)
702 drm_sched_resubmit_jobs(&v3d->queue[q].sched);
704 /* Unblock schedulers and restart their jobs. */
705 for (q = 0; q < V3D_MAX_QUEUES; q++) {
706 drm_sched_start(&v3d->queue[q].sched, 0);
709 mutex_unlock(&v3d->reset_lock);
711 return DRM_GPU_SCHED_STAT_NOMINAL;
714 /* If the current address or return address have changed, then the GPU
715 * has probably made progress and we should delay the reset. This
716 * could fail if the GPU got in an infinite loop in the CL, but that
717 * is pretty unlikely outside of an i-g-t testcase.
719 static enum drm_gpu_sched_stat
720 v3d_cl_job_timedout(struct drm_sched_job *sched_job, enum v3d_queue q,
721 u32 *timedout_ctca, u32 *timedout_ctra)
723 struct v3d_job *job = to_v3d_job(sched_job);
724 struct v3d_dev *v3d = job->v3d;
725 u32 ctca = V3D_CORE_READ(0, V3D_CLE_CTNCA(q));
726 u32 ctra = V3D_CORE_READ(0, V3D_CLE_CTNRA(q));
728 if (*timedout_ctca != ctca || *timedout_ctra != ctra) {
729 *timedout_ctca = ctca;
730 *timedout_ctra = ctra;
731 return DRM_GPU_SCHED_STAT_NOMINAL;
734 return v3d_gpu_reset_for_timeout(v3d, sched_job);
737 static enum drm_gpu_sched_stat
738 v3d_bin_job_timedout(struct drm_sched_job *sched_job)
740 struct v3d_bin_job *job = to_bin_job(sched_job);
742 return v3d_cl_job_timedout(sched_job, V3D_BIN,
743 &job->timedout_ctca, &job->timedout_ctra);
746 static enum drm_gpu_sched_stat
747 v3d_render_job_timedout(struct drm_sched_job *sched_job)
749 struct v3d_render_job *job = to_render_job(sched_job);
751 return v3d_cl_job_timedout(sched_job, V3D_RENDER,
752 &job->timedout_ctca, &job->timedout_ctra);
755 static enum drm_gpu_sched_stat
756 v3d_generic_job_timedout(struct drm_sched_job *sched_job)
758 struct v3d_job *job = to_v3d_job(sched_job);
760 return v3d_gpu_reset_for_timeout(job->v3d, sched_job);
763 static enum drm_gpu_sched_stat
764 v3d_csd_job_timedout(struct drm_sched_job *sched_job)
766 struct v3d_csd_job *job = to_csd_job(sched_job);
767 struct v3d_dev *v3d = job->base.v3d;
768 u32 batches = V3D_CORE_READ(0, V3D_CSD_CURRENT_CFG4(v3d->ver));
770 /* If we've made progress, skip reset and let the timer get
773 if (job->timedout_batches != batches) {
774 job->timedout_batches = batches;
775 return DRM_GPU_SCHED_STAT_NOMINAL;
778 return v3d_gpu_reset_for_timeout(v3d, sched_job);
781 static const struct drm_sched_backend_ops v3d_bin_sched_ops = {
782 .run_job = v3d_bin_job_run,
783 .timedout_job = v3d_bin_job_timedout,
784 .free_job = v3d_sched_job_free,
787 static const struct drm_sched_backend_ops v3d_render_sched_ops = {
788 .run_job = v3d_render_job_run,
789 .timedout_job = v3d_render_job_timedout,
790 .free_job = v3d_sched_job_free,
793 static const struct drm_sched_backend_ops v3d_tfu_sched_ops = {
794 .run_job = v3d_tfu_job_run,
795 .timedout_job = v3d_generic_job_timedout,
796 .free_job = v3d_sched_job_free,
799 static const struct drm_sched_backend_ops v3d_csd_sched_ops = {
800 .run_job = v3d_csd_job_run,
801 .timedout_job = v3d_csd_job_timedout,
802 .free_job = v3d_sched_job_free
805 static const struct drm_sched_backend_ops v3d_cache_clean_sched_ops = {
806 .run_job = v3d_cache_clean_job_run,
807 .timedout_job = v3d_generic_job_timedout,
808 .free_job = v3d_sched_job_free
811 static const struct drm_sched_backend_ops v3d_cpu_sched_ops = {
812 .run_job = v3d_cpu_job_run,
813 .timedout_job = v3d_generic_job_timedout,
814 .free_job = v3d_cpu_job_free
818 v3d_sched_init(struct v3d_dev *v3d)
820 int hw_jobs_limit = 1;
821 int job_hang_limit = 0;
822 int hang_limit_ms = 500;
825 ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
826 &v3d_bin_sched_ops, NULL,
827 DRM_SCHED_PRIORITY_COUNT,
828 hw_jobs_limit, job_hang_limit,
829 msecs_to_jiffies(hang_limit_ms), NULL,
830 NULL, "v3d_bin", v3d->drm.dev);
834 ret = drm_sched_init(&v3d->queue[V3D_RENDER].sched,
835 &v3d_render_sched_ops, NULL,
836 DRM_SCHED_PRIORITY_COUNT,
837 hw_jobs_limit, job_hang_limit,
838 msecs_to_jiffies(hang_limit_ms), NULL,
839 NULL, "v3d_render", v3d->drm.dev);
843 ret = drm_sched_init(&v3d->queue[V3D_TFU].sched,
844 &v3d_tfu_sched_ops, NULL,
845 DRM_SCHED_PRIORITY_COUNT,
846 hw_jobs_limit, job_hang_limit,
847 msecs_to_jiffies(hang_limit_ms), NULL,
848 NULL, "v3d_tfu", v3d->drm.dev);
852 if (v3d_has_csd(v3d)) {
853 ret = drm_sched_init(&v3d->queue[V3D_CSD].sched,
854 &v3d_csd_sched_ops, NULL,
855 DRM_SCHED_PRIORITY_COUNT,
856 hw_jobs_limit, job_hang_limit,
857 msecs_to_jiffies(hang_limit_ms), NULL,
858 NULL, "v3d_csd", v3d->drm.dev);
862 ret = drm_sched_init(&v3d->queue[V3D_CACHE_CLEAN].sched,
863 &v3d_cache_clean_sched_ops, NULL,
864 DRM_SCHED_PRIORITY_COUNT,
865 hw_jobs_limit, job_hang_limit,
866 msecs_to_jiffies(hang_limit_ms), NULL,
867 NULL, "v3d_cache_clean", v3d->drm.dev);
872 ret = drm_sched_init(&v3d->queue[V3D_CPU].sched,
873 &v3d_cpu_sched_ops, NULL,
874 DRM_SCHED_PRIORITY_COUNT,
876 msecs_to_jiffies(hang_limit_ms), NULL,
877 NULL, "v3d_cpu", v3d->drm.dev);
889 v3d_sched_fini(struct v3d_dev *v3d)
893 for (q = 0; q < V3D_MAX_QUEUES; q++) {
894 if (v3d->queue[q].sched.ready)
895 drm_sched_fini(&v3d->queue[q].sched);