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