xref: /linux/drivers/gpu/drm/v3d/v3d_sched.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2018 Broadcom */
3 
4 /**
5  * DOC: Broadcom V3D scheduling
6  *
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 schedule the clients with a FIFO scheduling algorithm.
11  *
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
17  * and render, instead of having the clients submit jobs using the HW's
18  * semaphores to interlock between them.
19  */
20 
21 #include <linux/sched/clock.h>
22 #include <linux/kthread.h>
23 
24 #include <drm/drm_print.h>
25 #include <drm/drm_syncobj.h>
26 
27 #include "v3d_drv.h"
28 #include "v3d_regs.h"
29 #include "v3d_trace.h"
30 
31 #define V3D_CSD_CFG012_WG_COUNT_SHIFT 16
32 
33 static struct v3d_job *
34 to_v3d_job(struct drm_sched_job *sched_job)
35 {
36 	return container_of(sched_job, struct v3d_job, base);
37 }
38 
39 static struct v3d_bin_job *
40 to_bin_job(struct drm_sched_job *sched_job)
41 {
42 	return container_of(sched_job, struct v3d_bin_job, base.base);
43 }
44 
45 static struct v3d_render_job *
46 to_render_job(struct drm_sched_job *sched_job)
47 {
48 	return container_of(sched_job, struct v3d_render_job, base.base);
49 }
50 
51 static struct v3d_tfu_job *
52 to_tfu_job(struct drm_sched_job *sched_job)
53 {
54 	return container_of(sched_job, struct v3d_tfu_job, base.base);
55 }
56 
57 static struct v3d_csd_job *
58 to_csd_job(struct drm_sched_job *sched_job)
59 {
60 	return container_of(sched_job, struct v3d_csd_job, base.base);
61 }
62 
63 static struct v3d_cpu_job *
64 to_cpu_job(struct drm_sched_job *sched_job)
65 {
66 	return container_of(sched_job, struct v3d_cpu_job, base.base);
67 }
68 
69 void v3d_stats_release(struct kref *refcount)
70 {
71 	struct v3d_stats *stats = container_of(refcount, typeof(*stats), refcount);
72 
73 	kfree(stats);
74 }
75 
76 struct v3d_stats *v3d_stats_alloc(void)
77 {
78 	struct v3d_stats *stats;
79 
80 	stats = kzalloc_obj(*stats);
81 	if (!stats)
82 		return NULL;
83 
84 	kref_init(&stats->refcount);
85 	seqcount_init(&stats->lock);
86 
87 	return stats;
88 }
89 
90 static void
91 v3d_sched_job_free(struct drm_sched_job *sched_job)
92 {
93 	struct v3d_job *job = to_v3d_job(sched_job);
94 
95 	v3d_job_cleanup(job);
96 }
97 
98 void
99 v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
100 			      unsigned int count)
101 {
102 	if (query_info->queries) {
103 		unsigned int i;
104 
105 		for (i = 0; i < count; i++)
106 			drm_syncobj_put(query_info->queries[i].syncobj);
107 
108 		kvfree(query_info->queries);
109 	}
110 }
111 
112 void
113 v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
114 				unsigned int count)
115 {
116 	if (query_info->queries) {
117 		unsigned int i;
118 
119 		for (i = 0; i < count; i++) {
120 			drm_syncobj_put(query_info->queries[i].syncobj);
121 			kvfree(query_info->queries[i].kperfmon_ids);
122 		}
123 
124 		kvfree(query_info->queries);
125 	}
126 }
127 
128 static void
129 v3d_stats_start(struct v3d_stats *stats, u64 now)
130 {
131 	raw_write_seqcount_begin(&stats->lock);
132 	stats->start_ns = now;
133 	raw_write_seqcount_end(&stats->lock);
134 }
135 
136 static void
137 v3d_job_start_stats(struct v3d_job *job)
138 {
139 	u64 now = local_clock();
140 
141 	preempt_disable();
142 	v3d_stats_start(job->client_stats, now);
143 	v3d_stats_start(job->global_stats, now);
144 	preempt_enable();
145 }
146 
147 static void
148 v3d_stats_update(struct v3d_stats *stats, u64 now)
149 {
150 	raw_write_seqcount_begin(&stats->lock);
151 	stats->enabled_ns += now - stats->start_ns;
152 	stats->jobs_completed++;
153 	stats->start_ns = 0;
154 	raw_write_seqcount_end(&stats->lock);
155 }
156 
157 void
158 v3d_job_update_stats(struct v3d_job *job)
159 {
160 	u64 now = local_clock();
161 
162 	preempt_disable();
163 	v3d_stats_update(job->client_stats, now);
164 	v3d_stats_update(job->global_stats, now);
165 	preempt_enable();
166 }
167 
168 static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
169 {
170 	struct v3d_bin_job *job = to_bin_job(sched_job);
171 	struct v3d_dev *v3d = job->base.v3d;
172 	struct v3d_queue_state *queue = &v3d->queue[V3D_BIN];
173 	struct drm_device *dev = &v3d->drm;
174 	struct dma_fence *fence = NULL;
175 	unsigned long irqflags;
176 
177 	if (unlikely(job->base.base.s_fence->finished.error))
178 		goto out_clean_job;
179 
180 	/* Lock required around bin_job update vs
181 	 * v3d_overflow_mem_work().
182 	 */
183 	spin_lock_irqsave(&queue->queue_lock, irqflags);
184 	queue->active_job = &job->base;
185 	/* Clear out the overflow allocation, so we don't
186 	 * reuse the overflow attached to a previous job.
187 	 */
188 	V3D_CORE_WRITE(0, V3D_PTB_BPOS, 0);
189 	spin_unlock_irqrestore(&queue->queue_lock, irqflags);
190 
191 	v3d_invalidate_caches(v3d);
192 
193 	fence = v3d_fence_create(v3d, V3D_BIN);
194 	if (IS_ERR(fence))
195 		goto out_clean_job;
196 
197 	if (job->base.irq_fence)
198 		dma_fence_put(job->base.irq_fence);
199 	job->base.irq_fence = dma_fence_get(fence);
200 
201 	trace_v3d_submit_cl(dev, false, to_v3d_fence(fence)->seqno,
202 			    job->start, job->end);
203 
204 	v3d_job_start_stats(&job->base);
205 	v3d_perfmon_start(v3d, job->base.perfmon);
206 
207 	/* Set the current and end address of the control list.
208 	 * Writing the end register is what starts the job.
209 	 */
210 	if (job->qma) {
211 		V3D_CORE_WRITE(0, V3D_CLE_CT0QMA, job->qma);
212 		V3D_CORE_WRITE(0, V3D_CLE_CT0QMS, job->qms);
213 	}
214 	if (job->qts) {
215 		V3D_CORE_WRITE(0, V3D_CLE_CT0QTS,
216 			       V3D_CLE_CT0QTS_ENABLE |
217 			       job->qts);
218 	}
219 	V3D_CORE_WRITE(0, V3D_CLE_CT0QBA, job->start);
220 	V3D_CORE_WRITE(0, V3D_CLE_CT0QEA, job->end);
221 
222 	return fence;
223 
224 out_clean_job:
225 	spin_lock_irqsave(&queue->queue_lock, irqflags);
226 	queue->active_job = NULL;
227 	spin_unlock_irqrestore(&queue->queue_lock, irqflags);
228 	return fence;
229 }
230 
231 static struct dma_fence *v3d_render_job_run(struct drm_sched_job *sched_job)
232 {
233 	struct v3d_render_job *job = to_render_job(sched_job);
234 	struct v3d_dev *v3d = job->base.v3d;
235 	struct drm_device *dev = &v3d->drm;
236 	struct dma_fence *fence = NULL;
237 
238 	if (unlikely(job->base.base.s_fence->finished.error))
239 		goto out_clean_job;
240 
241 	v3d->queue[V3D_RENDER].active_job = &job->base;
242 
243 	/* Can we avoid this flush?  We need to be careful of
244 	 * scheduling, though -- imagine job0 rendering to texture and
245 	 * job1 reading, and them being executed as bin0, bin1,
246 	 * render0, render1, so that render1's flush at bin time
247 	 * wasn't enough.
248 	 */
249 	v3d_invalidate_caches(v3d);
250 
251 	fence = v3d_fence_create(v3d, V3D_RENDER);
252 	if (IS_ERR(fence))
253 		goto out_clean_job;
254 
255 	if (job->base.irq_fence)
256 		dma_fence_put(job->base.irq_fence);
257 	job->base.irq_fence = dma_fence_get(fence);
258 
259 	trace_v3d_submit_cl(dev, true, to_v3d_fence(fence)->seqno,
260 			    job->start, job->end);
261 
262 	v3d_job_start_stats(&job->base);
263 	v3d_perfmon_start(v3d, job->base.perfmon);
264 
265 	/* XXX: Set the QCFG */
266 
267 	/* Set the current and end address of the control list.
268 	 * Writing the end register is what starts the job.
269 	 */
270 	V3D_CORE_WRITE(0, V3D_CLE_CT1QBA, job->start);
271 	V3D_CORE_WRITE(0, V3D_CLE_CT1QEA, job->end);
272 
273 	return fence;
274 
275 out_clean_job:
276 	v3d->queue[V3D_RENDER].active_job = NULL;
277 	return fence;
278 }
279 
280 static struct dma_fence *
281 v3d_tfu_job_run(struct drm_sched_job *sched_job)
282 {
283 	struct v3d_tfu_job *job = to_tfu_job(sched_job);
284 	struct v3d_dev *v3d = job->base.v3d;
285 	struct drm_device *dev = &v3d->drm;
286 	struct dma_fence *fence = NULL;
287 
288 	if (unlikely(job->base.base.s_fence->finished.error))
289 		goto out_clean_job;
290 
291 	v3d->queue[V3D_TFU].active_job = &job->base;
292 
293 	fence = v3d_fence_create(v3d, V3D_TFU);
294 	if (IS_ERR(fence))
295 		goto out_clean_job;
296 
297 	if (job->base.irq_fence)
298 		dma_fence_put(job->base.irq_fence);
299 	job->base.irq_fence = dma_fence_get(fence);
300 
301 	trace_v3d_submit_tfu(dev, to_v3d_fence(fence)->seqno);
302 
303 	v3d_job_start_stats(&job->base);
304 
305 	V3D_WRITE(V3D_TFU_IIA(v3d->ver), job->args.iia);
306 	V3D_WRITE(V3D_TFU_IIS(v3d->ver), job->args.iis);
307 	V3D_WRITE(V3D_TFU_ICA(v3d->ver), job->args.ica);
308 	V3D_WRITE(V3D_TFU_IUA(v3d->ver), job->args.iua);
309 	V3D_WRITE(V3D_TFU_IOA(v3d->ver), job->args.ioa);
310 	if (v3d->ver >= V3D_GEN_71)
311 		V3D_WRITE(V3D_V7_TFU_IOC, job->args.v71.ioc);
312 	V3D_WRITE(V3D_TFU_IOS(v3d->ver), job->args.ios);
313 	V3D_WRITE(V3D_TFU_COEF0(v3d->ver), job->args.coef[0]);
314 	if (v3d->ver >= V3D_GEN_71 || (job->args.coef[0] & V3D_TFU_COEF0_USECOEF)) {
315 		V3D_WRITE(V3D_TFU_COEF1(v3d->ver), job->args.coef[1]);
316 		V3D_WRITE(V3D_TFU_COEF2(v3d->ver), job->args.coef[2]);
317 		V3D_WRITE(V3D_TFU_COEF3(v3d->ver), job->args.coef[3]);
318 	}
319 	/* ICFG kicks off the job. */
320 	V3D_WRITE(V3D_TFU_ICFG(v3d->ver), job->args.icfg | V3D_TFU_ICFG_IOC);
321 
322 	return fence;
323 
324 out_clean_job:
325 	v3d->queue[V3D_TFU].active_job = NULL;
326 	return fence;
327 }
328 
329 static struct dma_fence *
330 v3d_csd_job_run(struct drm_sched_job *sched_job)
331 {
332 	struct v3d_csd_job *job = to_csd_job(sched_job);
333 	struct v3d_dev *v3d = job->base.v3d;
334 	struct drm_device *dev = &v3d->drm;
335 	struct dma_fence *fence = NULL;
336 	int i, csd_cfg0_reg;
337 
338 	if (unlikely(job->base.base.s_fence->finished.error))
339 		goto out_clean_job;
340 
341 	/* The HW interprets a workgroup size of 0 as 65536; however, the
342 	 * user-space driver exposes a maximum of 65535. Therefore, a 0 in
343 	 * any dimension means that we have no workgroups and the compute
344 	 * shader should not be dispatched.
345 	 */
346 	if (!V3D_GET_FIELD(job->args.cfg[0], V3D_CSD_QUEUED_CFG0_NUM_WGS_X) ||
347 	    !V3D_GET_FIELD(job->args.cfg[1], V3D_CSD_QUEUED_CFG1_NUM_WGS_Y) ||
348 	    !V3D_GET_FIELD(job->args.cfg[2], V3D_CSD_QUEUED_CFG2_NUM_WGS_Z))
349 		return NULL;
350 
351 	v3d->queue[V3D_CSD].active_job = &job->base;
352 
353 	v3d_invalidate_caches(v3d);
354 
355 	fence = v3d_fence_create(v3d, V3D_CSD);
356 	if (IS_ERR(fence))
357 		goto out_clean_job;
358 
359 	if (job->base.irq_fence)
360 		dma_fence_put(job->base.irq_fence);
361 	job->base.irq_fence = dma_fence_get(fence);
362 
363 	job->timedout_batches = job->args.cfg[4];
364 
365 	trace_v3d_submit_csd(dev, to_v3d_fence(fence)->seqno);
366 
367 	v3d_job_start_stats(&job->base);
368 	v3d_perfmon_start(v3d, job->base.perfmon);
369 
370 	csd_cfg0_reg = V3D_CSD_QUEUED_CFG0(v3d->ver);
371 	for (i = 1; i <= 6; i++)
372 		V3D_CORE_WRITE(0, csd_cfg0_reg + 4 * i, job->args.cfg[i]);
373 
374 	/* Although V3D 7.1 has an eighth configuration register, we are not
375 	 * using it. Therefore, make sure it remains unused.
376 	 *
377 	 * XXX: Set the CFG7 register
378 	 */
379 	if (v3d->ver >= V3D_GEN_71)
380 		V3D_CORE_WRITE(0, V3D_V7_CSD_QUEUED_CFG7, 0);
381 
382 	/* CFG0 write kicks off the job. */
383 	V3D_CORE_WRITE(0, csd_cfg0_reg, job->args.cfg[0]);
384 
385 	return fence;
386 
387 out_clean_job:
388 	v3d->queue[V3D_CSD].active_job = NULL;
389 	return fence;
390 }
391 
392 static void
393 v3d_rewrite_csd_job_wg_counts_from_indirect(struct v3d_cpu_job *job)
394 {
395 	struct v3d_indirect_csd_info *indirect_csd = &job->indirect_csd;
396 	struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
397 	struct v3d_bo *indirect = to_v3d_bo(indirect_csd->indirect);
398 	struct drm_v3d_submit_csd *args = &indirect_csd->job->args;
399 	struct v3d_dev *v3d = job->base.v3d;
400 	u32 num_batches, *wg_counts;
401 
402 	v3d_get_bo_vaddr(bo);
403 	v3d_get_bo_vaddr(indirect);
404 
405 	wg_counts = (uint32_t *)(bo->vaddr + indirect_csd->offset);
406 
407 	args->cfg[0] = wg_counts[0] << V3D_CSD_CFG012_WG_COUNT_SHIFT;
408 	args->cfg[1] = wg_counts[1] << V3D_CSD_CFG012_WG_COUNT_SHIFT;
409 	args->cfg[2] = wg_counts[2] << V3D_CSD_CFG012_WG_COUNT_SHIFT;
410 
411 	if (wg_counts[0] == 0 || wg_counts[1] == 0 || wg_counts[2] == 0)
412 		goto unmap_bo;
413 
414 	num_batches = DIV_ROUND_UP(indirect_csd->wg_size, 16) *
415 		      (wg_counts[0] * wg_counts[1] * wg_counts[2]);
416 
417 	/* V3D 7.1.6 and later don't subtract 1 from the number of batches */
418 	if (v3d->ver < 71 || (v3d->ver == 71 && v3d->rev < 6))
419 		args->cfg[4] = num_batches - 1;
420 	else
421 		args->cfg[4] = num_batches;
422 
423 	WARN_ON(args->cfg[4] == ~0);
424 
425 	for (int i = 0; i < 3; i++) {
426 		/* 0xffffffff indicates that the uniform rewrite is not needed */
427 		if (indirect_csd->wg_uniform_offsets[i] != 0xffffffff) {
428 			u32 uniform_idx = indirect_csd->wg_uniform_offsets[i];
429 			((uint32_t *)indirect->vaddr)[uniform_idx] = wg_counts[i];
430 		}
431 	}
432 
433 unmap_bo:
434 	v3d_put_bo_vaddr(indirect);
435 	v3d_put_bo_vaddr(bo);
436 }
437 
438 static void
439 v3d_timestamp_query(struct v3d_cpu_job *job)
440 {
441 	struct v3d_timestamp_query_info *timestamp_query = &job->timestamp_query;
442 	struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
443 	u8 *value_addr;
444 
445 	v3d_get_bo_vaddr(bo);
446 
447 	for (int i = 0; i < timestamp_query->count; i++) {
448 		value_addr = ((u8 *)bo->vaddr) + timestamp_query->queries[i].offset;
449 		*((u64 *)value_addr) = i == 0 ? ktime_get_ns() : 0ull;
450 
451 		drm_syncobj_replace_fence(timestamp_query->queries[i].syncobj,
452 					  job->base.done_fence);
453 	}
454 
455 	v3d_put_bo_vaddr(bo);
456 }
457 
458 static void
459 v3d_reset_timestamp_queries(struct v3d_cpu_job *job)
460 {
461 	struct v3d_timestamp_query_info *timestamp_query = &job->timestamp_query;
462 	struct v3d_timestamp_query *queries = timestamp_query->queries;
463 	struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
464 	u8 *value_addr;
465 
466 	v3d_get_bo_vaddr(bo);
467 
468 	for (int i = 0; i < timestamp_query->count; i++) {
469 		value_addr = ((u8 *)bo->vaddr) + queries[i].offset;
470 		*((u64 *)value_addr) = 0;
471 
472 		drm_syncobj_replace_fence(queries[i].syncobj, NULL);
473 	}
474 
475 	v3d_put_bo_vaddr(bo);
476 }
477 
478 static void write_to_buffer_32(u32 *dst, unsigned int idx, u32 value)
479 {
480 	dst[idx] = value;
481 }
482 
483 static void write_to_buffer_64(u64 *dst, unsigned int idx, u64 value)
484 {
485 	dst[idx] = value;
486 }
487 
488 static void
489 write_to_buffer(void *dst, unsigned int idx, bool do_64bit, u64 value)
490 {
491 	if (do_64bit)
492 		write_to_buffer_64(dst, idx, value);
493 	else
494 		write_to_buffer_32(dst, idx, value);
495 }
496 
497 static void
498 v3d_copy_query_results(struct v3d_cpu_job *job)
499 {
500 	struct v3d_timestamp_query_info *timestamp_query = &job->timestamp_query;
501 	struct v3d_timestamp_query *queries = timestamp_query->queries;
502 	struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
503 	struct v3d_bo *timestamp = to_v3d_bo(job->base.bo[1]);
504 	struct v3d_copy_query_results_info *copy = &job->copy;
505 	struct dma_fence *fence;
506 	u8 *query_addr;
507 	bool available, write_result;
508 	u8 *data;
509 	int i;
510 
511 	v3d_get_bo_vaddr(bo);
512 	v3d_get_bo_vaddr(timestamp);
513 
514 	data = ((u8 *)bo->vaddr) + copy->offset;
515 
516 	for (i = 0; i < timestamp_query->count; i++) {
517 		fence = drm_syncobj_fence_get(queries[i].syncobj);
518 		available = fence ? dma_fence_is_signaled(fence) : false;
519 
520 		write_result = available || copy->do_partial;
521 		if (write_result) {
522 			query_addr = ((u8 *)timestamp->vaddr) + queries[i].offset;
523 			write_to_buffer(data, 0, copy->do_64bit, *((u64 *)query_addr));
524 		}
525 
526 		if (copy->availability_bit)
527 			write_to_buffer(data, 1, copy->do_64bit, available ? 1u : 0u);
528 
529 		data += copy->stride;
530 
531 		dma_fence_put(fence);
532 	}
533 
534 	v3d_put_bo_vaddr(timestamp);
535 	v3d_put_bo_vaddr(bo);
536 }
537 
538 static void
539 v3d_reset_performance_queries(struct v3d_cpu_job *job)
540 {
541 	struct v3d_performance_query_info *performance_query = &job->performance_query;
542 	struct v3d_file_priv *v3d_priv = job->base.file_priv;
543 	struct v3d_dev *v3d = job->base.v3d;
544 	struct v3d_perfmon *perfmon;
545 
546 	for (int i = 0; i < performance_query->count; i++) {
547 		for (int j = 0; j < performance_query->nperfmons; j++) {
548 			perfmon = v3d_perfmon_find(v3d_priv,
549 						   performance_query->queries[i].kperfmon_ids[j]);
550 			if (!perfmon) {
551 				drm_dbg(&v3d->drm, "Failed to find perfmon.");
552 				continue;
553 			}
554 
555 			v3d_perfmon_stop(v3d, perfmon, false);
556 
557 			memset(perfmon->values, 0, perfmon->ncounters * sizeof(u64));
558 
559 			v3d_perfmon_put(perfmon);
560 		}
561 
562 		drm_syncobj_replace_fence(performance_query->queries[i].syncobj, NULL);
563 	}
564 }
565 
566 static void
567 v3d_write_performance_query_result(struct v3d_cpu_job *job, void *data,
568 				   unsigned int query)
569 {
570 	struct v3d_performance_query_info *performance_query =
571 						&job->performance_query;
572 	struct v3d_file_priv *v3d_priv = job->base.file_priv;
573 	struct v3d_performance_query *perf_query =
574 			&performance_query->queries[query];
575 	struct v3d_dev *v3d = job->base.v3d;
576 	unsigned int i, j, offset;
577 
578 	for (i = 0, offset = 0;
579 	     i < performance_query->nperfmons;
580 	     i++, offset += DRM_V3D_MAX_PERF_COUNTERS) {
581 		struct v3d_perfmon *perfmon;
582 
583 		perfmon = v3d_perfmon_find(v3d_priv,
584 					   perf_query->kperfmon_ids[i]);
585 		if (!perfmon) {
586 			drm_dbg(&v3d->drm, "Failed to find perfmon.");
587 			continue;
588 		}
589 
590 		v3d_perfmon_stop(v3d, perfmon, true);
591 
592 		for (j = 0; j < perfmon->ncounters; j++)
593 			write_to_buffer(data, offset + j, job->copy.do_64bit, perfmon->values[j]);
594 
595 		v3d_perfmon_put(perfmon);
596 	}
597 }
598 
599 static void
600 v3d_copy_performance_query(struct v3d_cpu_job *job)
601 {
602 	struct v3d_performance_query_info *performance_query = &job->performance_query;
603 	struct v3d_copy_query_results_info *copy = &job->copy;
604 	struct v3d_bo *bo = to_v3d_bo(job->base.bo[0]);
605 	struct dma_fence *fence;
606 	bool available, write_result;
607 	u8 *data;
608 
609 	v3d_get_bo_vaddr(bo);
610 
611 	data = ((u8 *)bo->vaddr) + copy->offset;
612 
613 	for (int i = 0; i < performance_query->count; i++) {
614 		fence = drm_syncobj_fence_get(performance_query->queries[i].syncobj);
615 		available = fence ? dma_fence_is_signaled(fence) : false;
616 
617 		write_result = available || copy->do_partial;
618 		if (write_result)
619 			v3d_write_performance_query_result(job, data, i);
620 
621 		if (copy->availability_bit)
622 			write_to_buffer(data, performance_query->ncounters,
623 					copy->do_64bit, available ? 1u : 0u);
624 
625 		data += copy->stride;
626 
627 		dma_fence_put(fence);
628 	}
629 
630 	v3d_put_bo_vaddr(bo);
631 }
632 
633 static const v3d_cpu_job_fn cpu_job_function[] = {
634 	[V3D_CPU_JOB_TYPE_INDIRECT_CSD] = v3d_rewrite_csd_job_wg_counts_from_indirect,
635 	[V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = v3d_timestamp_query,
636 	[V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = v3d_reset_timestamp_queries,
637 	[V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = v3d_copy_query_results,
638 	[V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = v3d_reset_performance_queries,
639 	[V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = v3d_copy_performance_query,
640 };
641 
642 static struct dma_fence *
643 v3d_cpu_job_run(struct drm_sched_job *sched_job)
644 {
645 	struct v3d_cpu_job *job = to_cpu_job(sched_job);
646 	struct v3d_dev *v3d = job->base.v3d;
647 
648 	if (unlikely(job->base.base.s_fence->finished.error))
649 		return NULL;
650 
651 	if (job->job_type >= ARRAY_SIZE(cpu_job_function)) {
652 		drm_dbg(&v3d->drm, "Unknown CPU job: %d\n", job->job_type);
653 		return NULL;
654 	}
655 
656 	v3d_job_start_stats(&job->base);
657 	trace_v3d_cpu_job_begin(&v3d->drm, job->job_type);
658 
659 	cpu_job_function[job->job_type](job);
660 
661 	trace_v3d_cpu_job_end(&v3d->drm, job->job_type);
662 	v3d_job_update_stats(&job->base);
663 
664 	/* Synchronous operation, so no fence to wait on. */
665 	return NULL;
666 }
667 
668 static struct dma_fence *
669 v3d_cache_clean_job_run(struct drm_sched_job *sched_job)
670 {
671 	struct v3d_job *job = to_v3d_job(sched_job);
672 	struct v3d_dev *v3d = job->v3d;
673 
674 	if (unlikely(job->base.s_fence->finished.error))
675 		return NULL;
676 
677 	v3d_job_start_stats(job);
678 
679 	v3d_clean_caches(v3d);
680 
681 	v3d_job_update_stats(job);
682 
683 	/* Synchronous operation, so no fence to wait on. */
684 	return NULL;
685 }
686 
687 static enum drm_gpu_sched_stat
688 v3d_gpu_reset_for_timeout(struct v3d_dev *v3d, struct drm_sched_job *sched_job,
689 			  enum v3d_queue q)
690 {
691 	struct v3d_job *job = to_v3d_job(sched_job);
692 	enum v3d_queue i;
693 
694 	/* block scheduler */
695 	for (i = 0; i < V3D_MAX_QUEUES; i++)
696 		drm_sched_stop(&v3d->queue[i].sched, sched_job);
697 
698 	if (sched_job)
699 		drm_sched_increase_karma(sched_job);
700 
701 	v3d_perfmon_stop(v3d, job->perfmon, false);
702 
703 	/* get the GPU back into the init state */
704 	v3d_reset(v3d);
705 
706 	atomic_inc(&v3d->reset_counter);
707 	atomic_inc(&job->client_stats->reset_counter);
708 
709 	for (i = 0; i < V3D_MAX_QUEUES; i++)
710 		drm_sched_resubmit_jobs(&v3d->queue[i].sched);
711 
712 	/* Unblock schedulers and restart their jobs. */
713 	for (i = 0; i < V3D_MAX_QUEUES; i++)
714 		drm_sched_start(&v3d->queue[i].sched, 0);
715 
716 	return DRM_GPU_SCHED_STAT_RESET;
717 }
718 
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)
722 {
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));
727 
728 	/* If the current address or return address have changed, then the GPU
729 	 * has probably made progress and we should delay the reset. This
730 	 * could fail if the GPU got in an infinite loop in the CL, but that
731 	 * is pretty unlikely outside of an i-g-t testcase.
732 	 */
733 	if (*timedout_ctca != ctca || *timedout_ctra != ctra) {
734 		*timedout_ctca = ctca;
735 		*timedout_ctra = ctra;
736 
737 		return DRM_GPU_SCHED_STAT_NO_HANG;
738 	}
739 
740 	return v3d_gpu_reset_for_timeout(v3d, sched_job, q);
741 }
742 
743 static enum drm_gpu_sched_stat
744 v3d_bin_job_timedout(struct drm_sched_job *sched_job)
745 {
746 	struct v3d_bin_job *job = to_bin_job(sched_job);
747 
748 	return v3d_cl_job_timedout(sched_job, V3D_BIN,
749 				   &job->timedout_ctca, &job->timedout_ctra);
750 }
751 
752 static enum drm_gpu_sched_stat
753 v3d_render_job_timedout(struct drm_sched_job *sched_job)
754 {
755 	struct v3d_render_job *job = to_render_job(sched_job);
756 
757 	return v3d_cl_job_timedout(sched_job, V3D_RENDER,
758 				   &job->timedout_ctca, &job->timedout_ctra);
759 }
760 
761 static enum drm_gpu_sched_stat
762 v3d_tfu_job_timedout(struct drm_sched_job *sched_job)
763 {
764 	struct v3d_job *job = to_v3d_job(sched_job);
765 
766 	return v3d_gpu_reset_for_timeout(job->v3d, sched_job, V3D_TFU);
767 }
768 
769 static enum drm_gpu_sched_stat
770 v3d_csd_job_timedout(struct drm_sched_job *sched_job)
771 {
772 	struct v3d_csd_job *job = to_csd_job(sched_job);
773 	struct v3d_dev *v3d = job->base.v3d;
774 	u32 batches = V3D_CORE_READ(0, V3D_CSD_CURRENT_CFG4(v3d->ver));
775 
776 	/* If we've made progress, skip reset, add the job to the pending
777 	 * list, and let the timer get rearmed.
778 	 */
779 	if (job->timedout_batches != batches) {
780 		job->timedout_batches = batches;
781 
782 		return DRM_GPU_SCHED_STAT_NO_HANG;
783 	}
784 
785 	return v3d_gpu_reset_for_timeout(v3d, sched_job, V3D_CSD);
786 }
787 
788 static const struct drm_sched_backend_ops v3d_bin_sched_ops = {
789 	.run_job = v3d_bin_job_run,
790 	.timedout_job = v3d_bin_job_timedout,
791 	.free_job = v3d_sched_job_free,
792 };
793 
794 static const struct drm_sched_backend_ops v3d_render_sched_ops = {
795 	.run_job = v3d_render_job_run,
796 	.timedout_job = v3d_render_job_timedout,
797 	.free_job = v3d_sched_job_free,
798 };
799 
800 static const struct drm_sched_backend_ops v3d_tfu_sched_ops = {
801 	.run_job = v3d_tfu_job_run,
802 	.timedout_job = v3d_tfu_job_timedout,
803 	.free_job = v3d_sched_job_free,
804 };
805 
806 static const struct drm_sched_backend_ops v3d_csd_sched_ops = {
807 	.run_job = v3d_csd_job_run,
808 	.timedout_job = v3d_csd_job_timedout,
809 	.free_job = v3d_sched_job_free
810 };
811 
812 static const struct drm_sched_backend_ops v3d_cache_clean_sched_ops = {
813 	.run_job = v3d_cache_clean_job_run,
814 	.free_job = v3d_sched_job_free
815 };
816 
817 static const struct drm_sched_backend_ops v3d_cpu_sched_ops = {
818 	.run_job = v3d_cpu_job_run,
819 	.free_job = v3d_sched_job_free
820 };
821 
822 static int
823 v3d_queue_sched_init(struct v3d_dev *v3d, const struct drm_sched_backend_ops *ops,
824 		     enum v3d_queue queue, const char *name)
825 {
826 	struct drm_sched_init_args args = {
827 		.num_rqs = DRM_SCHED_PRIORITY_COUNT,
828 		.credit_limit = 1,
829 		.timeout = msecs_to_jiffies(500),
830 		.timeout_wq = v3d->reset_wq,
831 		.dev = v3d->drm.dev,
832 	};
833 
834 	args.ops = ops;
835 	args.name = name;
836 
837 	return drm_sched_init(&v3d->queue[queue].sched, &args);
838 }
839 
840 int
841 v3d_sched_init(struct v3d_dev *v3d)
842 {
843 	int ret;
844 
845 	v3d->reset_wq = alloc_ordered_workqueue("v3d_reset", 0);
846 	if (!v3d->reset_wq)
847 		return -ENOMEM;
848 
849 	ret = v3d_queue_sched_init(v3d, &v3d_bin_sched_ops, V3D_BIN, "v3d_bin");
850 	if (ret)
851 		goto fail;
852 
853 	ret = v3d_queue_sched_init(v3d, &v3d_render_sched_ops, V3D_RENDER,
854 				   "v3d_render");
855 	if (ret)
856 		goto fail;
857 
858 	ret = v3d_queue_sched_init(v3d, &v3d_tfu_sched_ops, V3D_TFU, "v3d_tfu");
859 	if (ret)
860 		goto fail;
861 
862 	if (v3d_has_csd(v3d)) {
863 		ret = v3d_queue_sched_init(v3d, &v3d_csd_sched_ops, V3D_CSD,
864 					   "v3d_csd");
865 		if (ret)
866 			goto fail;
867 
868 		ret = v3d_queue_sched_init(v3d, &v3d_cache_clean_sched_ops,
869 					   V3D_CACHE_CLEAN, "v3d_cache_clean");
870 		if (ret)
871 			goto fail;
872 	}
873 
874 	ret = v3d_queue_sched_init(v3d, &v3d_cpu_sched_ops, V3D_CPU, "v3d_cpu");
875 	if (ret)
876 		goto fail;
877 
878 	return 0;
879 
880 fail:
881 	v3d_sched_fini(v3d);
882 	return ret;
883 }
884 
885 void
886 v3d_sched_fini(struct v3d_dev *v3d)
887 {
888 	enum v3d_queue q;
889 
890 	for (q = 0; q < V3D_MAX_QUEUES; q++) {
891 		if (v3d->queue[q].sched.ready)
892 			drm_sched_fini(&v3d->queue[q].sched);
893 	}
894 
895 	destroy_workqueue(v3d->reset_wq);
896 }
897