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