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