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