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