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