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