1 /* 2 * Copyright © 2014 Broadcom 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #include <linux/module.h> 25 #include <linux/platform_device.h> 26 #include <linux/reset.h> 27 #include <linux/device.h> 28 #include <linux/io.h> 29 #include <linux/sched/signal.h> 30 #include <linux/dma-fence-array.h> 31 32 #include <drm/drm_exec.h> 33 #include <drm/drm_print.h> 34 #include <drm/drm_syncobj.h> 35 36 #include "vc4_drv.h" 37 #include "vc4_regs.h" 38 #include "vc4_trace.h" 39 40 static void 41 vc4_queue_hangcheck(struct drm_device *dev) 42 { 43 struct vc4_dev *vc4 = to_vc4_dev(dev); 44 45 mod_timer(&vc4->hangcheck.timer, 46 round_jiffies_up(jiffies + msecs_to_jiffies(100))); 47 } 48 49 struct vc4_hang_state { 50 struct drm_vc4_get_hang_state user_state; 51 52 u32 bo_count; 53 struct drm_gem_object **bo; 54 }; 55 56 static void 57 vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state) 58 { 59 unsigned int i; 60 61 for (i = 0; i < state->user_state.bo_count; i++) 62 drm_gem_object_put(state->bo[i]); 63 64 kfree(state->bo); 65 kfree(state); 66 } 67 68 int 69 vc4_get_hang_state_ioctl(struct drm_device *dev, void *data, 70 struct drm_file *file_priv) 71 { 72 struct drm_vc4_get_hang_state *get_state = data; 73 struct drm_vc4_get_hang_state_bo *bo_state; 74 struct vc4_hang_state *kernel_state; 75 struct drm_vc4_get_hang_state *state; 76 struct vc4_dev *vc4 = to_vc4_dev(dev); 77 unsigned long irqflags; 78 u32 i; 79 int ret = 0; 80 81 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 82 return -ENODEV; 83 84 if (!vc4->v3d) { 85 DRM_DEBUG("VC4_GET_HANG_STATE with no VC4 V3D probed\n"); 86 return -ENODEV; 87 } 88 89 spin_lock_irqsave(&vc4->job_lock, irqflags); 90 kernel_state = vc4->hang_state; 91 if (!kernel_state) { 92 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 93 return -ENOENT; 94 } 95 state = &kernel_state->user_state; 96 97 /* If the user's array isn't big enough, just return the 98 * required array size. 99 */ 100 if (get_state->bo_count < state->bo_count) { 101 get_state->bo_count = state->bo_count; 102 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 103 return 0; 104 } 105 106 vc4->hang_state = NULL; 107 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 108 109 /* Save the user's BO pointer, so we don't stomp it with the memcpy. */ 110 state->bo = get_state->bo; 111 memcpy(get_state, state, sizeof(*state)); 112 113 bo_state = kzalloc_objs(*bo_state, state->bo_count); 114 if (!bo_state) { 115 ret = -ENOMEM; 116 goto err_free; 117 } 118 119 for (i = 0; i < state->bo_count; i++) { 120 struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]); 121 u32 handle; 122 123 ret = drm_gem_handle_create(file_priv, kernel_state->bo[i], 124 &handle); 125 126 if (ret) { 127 state->bo_count = i; 128 goto err_delete_handle; 129 } 130 bo_state[i].handle = handle; 131 bo_state[i].paddr = vc4_bo->base.dma_addr; 132 bo_state[i].size = vc4_bo->base.base.size; 133 } 134 135 if (copy_to_user(u64_to_user_ptr(get_state->bo), 136 bo_state, 137 state->bo_count * sizeof(*bo_state))) 138 ret = -EFAULT; 139 140 err_delete_handle: 141 if (ret) { 142 for (i = 0; i < state->bo_count; i++) 143 drm_gem_handle_delete(file_priv, bo_state[i].handle); 144 } 145 146 err_free: 147 vc4_free_hang_state(dev, kernel_state); 148 kfree(bo_state); 149 150 return ret; 151 } 152 153 static void 154 vc4_save_hang_state(struct drm_device *dev) 155 { 156 struct vc4_dev *vc4 = to_vc4_dev(dev); 157 struct drm_vc4_get_hang_state *state; 158 struct vc4_hang_state *kernel_state; 159 struct vc4_exec_info *exec[2]; 160 struct vc4_bo *bo; 161 unsigned long irqflags; 162 unsigned int i, j, k, unref_list_count; 163 164 kernel_state = kzalloc_objs(*kernel_state, 1); 165 if (!kernel_state) 166 return; 167 168 state = &kernel_state->user_state; 169 170 spin_lock_irqsave(&vc4->job_lock, irqflags); 171 exec[0] = vc4_first_bin_job(vc4); 172 exec[1] = vc4_first_render_job(vc4); 173 if (!exec[0] && !exec[1]) 174 goto err_free_state; 175 176 /* Get the bos from both binner and renderer into hang state. */ 177 state->bo_count = 0; 178 for (i = 0; i < 2; i++) { 179 if (!exec[i]) 180 continue; 181 182 unref_list_count = 0; 183 list_for_each_entry(bo, &exec[i]->unref_list, unref_head) 184 unref_list_count++; 185 state->bo_count += exec[i]->bo_count + unref_list_count; 186 } 187 188 kernel_state->bo = kzalloc_objs(*kernel_state->bo, state->bo_count, 189 GFP_ATOMIC); 190 191 if (!kernel_state->bo) 192 goto err_free_state; 193 194 k = 0; 195 for (i = 0; i < 2; i++) { 196 if (!exec[i]) 197 continue; 198 199 for (j = 0; j < exec[i]->bo_count; j++) { 200 bo = to_vc4_bo(exec[i]->bo[j]); 201 202 /* Retain BOs just in case they were marked purgeable. 203 * This prevents the BO from being purged before 204 * someone had a chance to dump the hang state. 205 */ 206 WARN_ON(!refcount_read(&bo->usecnt)); 207 refcount_inc(&bo->usecnt); 208 drm_gem_object_get(exec[i]->bo[j]); 209 kernel_state->bo[k++] = exec[i]->bo[j]; 210 } 211 212 list_for_each_entry(bo, &exec[i]->unref_list, unref_head) { 213 /* No need to retain BOs coming from the ->unref_list 214 * because they are naturally unpurgeable. 215 */ 216 drm_gem_object_get(&bo->base.base); 217 kernel_state->bo[k++] = &bo->base.base; 218 } 219 } 220 221 WARN_ON_ONCE(k != state->bo_count); 222 223 if (exec[0]) 224 state->start_bin = exec[0]->ct0ca; 225 if (exec[1]) 226 state->start_render = exec[1]->ct1ca; 227 228 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 229 230 state->ct0ca = V3D_READ(V3D_CTNCA(0)); 231 state->ct0ea = V3D_READ(V3D_CTNEA(0)); 232 233 state->ct1ca = V3D_READ(V3D_CTNCA(1)); 234 state->ct1ea = V3D_READ(V3D_CTNEA(1)); 235 236 state->ct0cs = V3D_READ(V3D_CTNCS(0)); 237 state->ct1cs = V3D_READ(V3D_CTNCS(1)); 238 239 state->ct0ra0 = V3D_READ(V3D_CT00RA0); 240 state->ct1ra0 = V3D_READ(V3D_CT01RA0); 241 242 state->bpca = V3D_READ(V3D_BPCA); 243 state->bpcs = V3D_READ(V3D_BPCS); 244 state->bpoa = V3D_READ(V3D_BPOA); 245 state->bpos = V3D_READ(V3D_BPOS); 246 247 state->vpmbase = V3D_READ(V3D_VPMBASE); 248 249 state->dbge = V3D_READ(V3D_DBGE); 250 state->fdbgo = V3D_READ(V3D_FDBGO); 251 state->fdbgb = V3D_READ(V3D_FDBGB); 252 state->fdbgr = V3D_READ(V3D_FDBGR); 253 state->fdbgs = V3D_READ(V3D_FDBGS); 254 state->errstat = V3D_READ(V3D_ERRSTAT); 255 256 /* We need to turn purgeable BOs into unpurgeable ones so that 257 * userspace has a chance to dump the hang state before the kernel 258 * decides to purge those BOs. 259 * Note that BO consistency at dump time cannot be guaranteed. For 260 * example, if the owner of these BOs decides to re-use them or mark 261 * them purgeable again there's nothing we can do to prevent it. 262 */ 263 for (i = 0; i < kernel_state->user_state.bo_count; i++) { 264 struct vc4_bo *bo = to_vc4_bo(kernel_state->bo[i]); 265 266 if (bo->madv == __VC4_MADV_NOTSUPP) 267 continue; 268 269 mutex_lock(&bo->madv_lock); 270 if (!WARN_ON(bo->madv == __VC4_MADV_PURGED)) 271 bo->madv = VC4_MADV_WILLNEED; 272 refcount_dec(&bo->usecnt); 273 mutex_unlock(&bo->madv_lock); 274 } 275 276 spin_lock_irqsave(&vc4->job_lock, irqflags); 277 if (vc4->hang_state) { 278 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 279 vc4_free_hang_state(dev, kernel_state); 280 } else { 281 vc4->hang_state = kernel_state; 282 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 283 } 284 285 return; 286 287 err_free_state: 288 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 289 kfree(kernel_state); 290 } 291 292 static void 293 vc4_reset(struct drm_device *dev) 294 { 295 struct vc4_v3d *v3d = to_vc4_dev(dev)->v3d; 296 int ret; 297 298 vc4_irq_disable(dev); 299 300 if (v3d->reset) { 301 drm_info(dev, "Resetting GPU.\n"); 302 303 ret = reset_control_reset(v3d->reset); 304 if (ret) 305 drm_err(dev, "Failed to reset the GPU: %d\n", ret); 306 307 vc4_v3d_init_hw(dev); 308 } else { 309 drm_info_once(dev, "No reset line; GPU state is not reset.\n"); 310 } 311 312 vc4_irq_reset(dev); 313 314 /* Rearm the hangcheck -- another job might have been waiting 315 * for our hung one to get kicked off, and vc4_irq_reset() 316 * would have started it. 317 */ 318 vc4_queue_hangcheck(dev); 319 } 320 321 static void 322 vc4_reset_work(struct work_struct *work) 323 { 324 struct vc4_dev *vc4 = 325 container_of(work, struct vc4_dev, hangcheck.reset_work); 326 int ret; 327 328 /* Make sure the device is not suspended during the reset. */ 329 ret = vc4_v3d_pm_get(vc4); 330 if (ret) { 331 drm_err(&vc4->base, "Failed to resume V3D for GPU reset: %d\n", ret); 332 return; 333 } 334 335 vc4_save_hang_state(&vc4->base); 336 vc4_reset(&vc4->base); 337 338 vc4_v3d_pm_put(vc4); 339 } 340 341 static void 342 vc4_hangcheck_elapsed(struct timer_list *t) 343 { 344 struct vc4_dev *vc4 = timer_container_of(vc4, t, hangcheck.timer); 345 struct drm_device *dev = &vc4->base; 346 uint32_t ct0ca, ct1ca; 347 unsigned long irqflags; 348 struct vc4_exec_info *bin_exec, *render_exec; 349 350 spin_lock_irqsave(&vc4->job_lock, irqflags); 351 352 bin_exec = vc4_first_bin_job(vc4); 353 render_exec = vc4_first_render_job(vc4); 354 355 /* If idle, we can stop watching for hangs. */ 356 if (!bin_exec && !render_exec) { 357 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 358 return; 359 } 360 361 ct0ca = V3D_READ(V3D_CTNCA(0)); 362 ct1ca = V3D_READ(V3D_CTNCA(1)); 363 364 /* If we've made any progress in execution, rearm the timer 365 * and wait. 366 */ 367 if ((bin_exec && ct0ca != bin_exec->last_ct0ca) || 368 (render_exec && ct1ca != render_exec->last_ct1ca)) { 369 if (bin_exec) 370 bin_exec->last_ct0ca = ct0ca; 371 if (render_exec) 372 render_exec->last_ct1ca = ct1ca; 373 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 374 vc4_queue_hangcheck(dev); 375 return; 376 } 377 378 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 379 380 /* We've gone too long with no progress, reset. This has to 381 * be done from a work struct, since resetting can sleep and 382 * this timer hook isn't allowed to. 383 */ 384 schedule_work(&vc4->hangcheck.reset_work); 385 } 386 387 static void 388 submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end) 389 { 390 struct vc4_dev *vc4 = to_vc4_dev(dev); 391 392 /* Set the current and end address of the control list. 393 * Writing the end register is what starts the job. 394 */ 395 V3D_WRITE(V3D_CTNCA(thread), start); 396 V3D_WRITE(V3D_CTNEA(thread), end); 397 } 398 399 int 400 vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns, 401 bool interruptible) 402 { 403 struct vc4_dev *vc4 = to_vc4_dev(dev); 404 int ret = 0; 405 unsigned long timeout_expire; 406 DEFINE_WAIT(wait); 407 408 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 409 return -ENODEV; 410 411 if (vc4->finished_seqno >= seqno) 412 return 0; 413 414 if (timeout_ns == 0) 415 return -ETIME; 416 417 timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns); 418 419 trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns); 420 for (;;) { 421 prepare_to_wait(&vc4->job_wait_queue, &wait, 422 interruptible ? TASK_INTERRUPTIBLE : 423 TASK_UNINTERRUPTIBLE); 424 425 if (interruptible && signal_pending(current)) { 426 ret = -ERESTARTSYS; 427 break; 428 } 429 430 if (vc4->finished_seqno >= seqno) 431 break; 432 433 if (timeout_ns != ~0ull) { 434 if (time_after_eq(jiffies, timeout_expire)) { 435 ret = -ETIME; 436 break; 437 } 438 schedule_timeout(timeout_expire - jiffies); 439 } else { 440 schedule(); 441 } 442 } 443 444 finish_wait(&vc4->job_wait_queue, &wait); 445 trace_vc4_wait_for_seqno_end(dev, seqno); 446 447 return ret; 448 } 449 450 static void 451 vc4_flush_caches(struct drm_device *dev) 452 { 453 struct vc4_dev *vc4 = to_vc4_dev(dev); 454 455 /* Flush the GPU L2 caches. These caches sit on top of system 456 * L3 (the 128kb or so shared with the CPU), and are 457 * non-allocating in the L3. 458 */ 459 V3D_WRITE(V3D_L2CACTL, 460 V3D_L2CACTL_L2CCLR); 461 462 V3D_WRITE(V3D_SLCACTL, 463 VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) | 464 VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) | 465 VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) | 466 VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC)); 467 } 468 469 static void 470 vc4_flush_texture_caches(struct drm_device *dev) 471 { 472 struct vc4_dev *vc4 = to_vc4_dev(dev); 473 474 V3D_WRITE(V3D_L2CACTL, 475 V3D_L2CACTL_L2CCLR); 476 477 V3D_WRITE(V3D_SLCACTL, 478 VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) | 479 VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC)); 480 } 481 482 /* Sets the registers for the next job to be actually be executed in 483 * the hardware. 484 * 485 * The job_lock should be held during this. 486 */ 487 void 488 vc4_submit_next_bin_job(struct drm_device *dev) 489 { 490 struct vc4_dev *vc4 = to_vc4_dev(dev); 491 struct vc4_exec_info *exec; 492 493 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 494 return; 495 496 again: 497 exec = vc4_first_bin_job(vc4); 498 if (!exec) 499 return; 500 501 vc4_flush_caches(dev); 502 503 /* Only start the perfmon if it was not already started by a previous 504 * job. 505 */ 506 if (exec->perfmon && vc4->active_perfmon != exec->perfmon) 507 vc4_perfmon_start(vc4, exec->perfmon); 508 509 /* Either put the job in the binner if it uses the binner, or 510 * immediately move it to the to-be-rendered queue. 511 */ 512 if (exec->ct0ca != exec->ct0ea) { 513 trace_vc4_submit_cl(dev, false, exec->seqno, exec->ct0ca, 514 exec->ct0ea); 515 submit_cl(dev, 0, exec->ct0ca, exec->ct0ea); 516 } else { 517 struct vc4_exec_info *next; 518 519 vc4_move_job_to_render(dev, exec); 520 next = vc4_first_bin_job(vc4); 521 522 /* We can't start the next bin job if the previous job had a 523 * different perfmon instance attached to it. The same goes 524 * if one of them had a perfmon attached to it and the other 525 * one doesn't. 526 */ 527 if (next && next->perfmon == exec->perfmon) 528 goto again; 529 } 530 } 531 532 void 533 vc4_submit_next_render_job(struct drm_device *dev) 534 { 535 struct vc4_dev *vc4 = to_vc4_dev(dev); 536 struct vc4_exec_info *exec = vc4_first_render_job(vc4); 537 538 if (!exec) 539 return; 540 541 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 542 return; 543 544 /* A previous RCL may have written to one of our textures, and 545 * our full cache flush at bin time may have occurred before 546 * that RCL completed. Flush the texture cache now, but not 547 * the instructions or uniforms (since we don't write those 548 * from an RCL). 549 */ 550 vc4_flush_texture_caches(dev); 551 552 trace_vc4_submit_cl(dev, true, exec->seqno, exec->ct1ca, exec->ct1ea); 553 submit_cl(dev, 1, exec->ct1ca, exec->ct1ea); 554 } 555 556 void 557 vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec) 558 { 559 struct vc4_dev *vc4 = to_vc4_dev(dev); 560 bool was_empty = list_empty(&vc4->render_job_list); 561 562 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 563 return; 564 565 list_move_tail(&exec->head, &vc4->render_job_list); 566 if (was_empty) 567 vc4_submit_next_render_job(dev); 568 } 569 570 static void 571 vc4_attach_fences(struct vc4_exec_info *exec) 572 { 573 struct vc4_bo *bo; 574 unsigned i; 575 576 for (i = 0; i < exec->bo_count; i++) { 577 bo = to_vc4_bo(exec->bo[i]); 578 dma_resv_add_fence(bo->base.base.resv, exec->fence, 579 DMA_RESV_USAGE_READ); 580 } 581 582 for (i = 0; i < exec->rcl_write_bo_count; i++) { 583 bo = to_vc4_bo(&exec->rcl_write_bo[i]->base); 584 dma_resv_add_fence(bo->base.base.resv, exec->fence, 585 DMA_RESV_USAGE_WRITE); 586 } 587 } 588 589 /* Takes the reservation lock on all the BOs being referenced, so that 590 * at queue submit time we can update the reservations. 591 * 592 * We don't lock the RCL the tile alloc/state BOs, or overflow memory 593 * (all of which are on exec->unref_list). They're entirely private 594 * to vc4, so we don't attach dma-buf fences to them. 595 */ 596 static int 597 vc4_lock_bo_reservations(struct vc4_exec_info *exec, 598 struct drm_exec *exec_ctx) 599 { 600 int ret; 601 602 /* Reserve space for our shared (read-only) fence references, 603 * before we commit the CL to the hardware. 604 */ 605 drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec->bo_count); 606 drm_exec_until_all_locked(exec_ctx) { 607 ret = drm_exec_prepare_array(exec_ctx, exec->bo, 608 exec->bo_count, 1); 609 } 610 611 if (ret) { 612 drm_exec_fini(exec_ctx); 613 return ret; 614 } 615 616 return 0; 617 } 618 619 /* Queues a struct vc4_exec_info for execution. If no job is 620 * currently executing, then submits it. 621 * 622 * Unlike most GPUs, our hardware only handles one command list at a 623 * time. To queue multiple jobs at once, we'd need to edit the 624 * previous command list to have a jump to the new one at the end, and 625 * then bump the end address. That's a change for a later date, 626 * though. 627 */ 628 static int 629 vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, 630 struct drm_exec *exec_ctx, 631 struct drm_syncobj *out_sync) 632 { 633 struct vc4_dev *vc4 = to_vc4_dev(dev); 634 struct vc4_exec_info *renderjob; 635 uint64_t seqno; 636 unsigned long irqflags; 637 struct vc4_fence *fence; 638 639 fence = kzalloc_obj(*fence); 640 if (!fence) 641 return -ENOMEM; 642 fence->dev = dev; 643 644 spin_lock_irqsave(&vc4->job_lock, irqflags); 645 646 seqno = ++vc4->emit_seqno; 647 exec->seqno = seqno; 648 649 dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock, 650 vc4->dma_fence_context, exec->seqno); 651 fence->seqno = exec->seqno; 652 exec->fence = &fence->base; 653 654 if (out_sync) 655 drm_syncobj_replace_fence(out_sync, exec->fence); 656 657 vc4_attach_fences(exec); 658 659 drm_exec_fini(exec_ctx); 660 661 list_add_tail(&exec->head, &vc4->bin_job_list); 662 663 /* If no bin job was executing and if the render job (if any) has the 664 * same perfmon as our job attached to it (or if both jobs don't have 665 * perfmon activated), then kick ours off. Otherwise, it'll get 666 * started when the previous job's flush/render done interrupt occurs. 667 */ 668 renderjob = vc4_first_render_job(vc4); 669 if (vc4_first_bin_job(vc4) == exec && 670 (!renderjob || renderjob->perfmon == exec->perfmon)) { 671 vc4_submit_next_bin_job(dev); 672 vc4_queue_hangcheck(dev); 673 } 674 675 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 676 677 return 0; 678 } 679 680 /** 681 * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects 682 * referenced by the job. 683 * @dev: DRM device 684 * @file_priv: DRM file for this fd 685 * @exec: V3D job being set up 686 * 687 * The command validator needs to reference BOs by their index within 688 * the submitted job's BO list. This does the validation of the job's 689 * BO list and reference counting for the lifetime of the job. 690 */ 691 static int 692 vc4_cl_lookup_bos(struct drm_device *dev, 693 struct drm_file *file_priv, 694 struct vc4_exec_info *exec) 695 { 696 struct drm_vc4_submit_cl *args = exec->args; 697 int ret = 0; 698 int i; 699 700 exec->bo_count = args->bo_handle_count; 701 702 if (!exec->bo_count) { 703 /* See comment on bo_index for why we have to check 704 * this. 705 */ 706 DRM_DEBUG("Rendering requires BOs to validate\n"); 707 return -EINVAL; 708 } 709 710 ret = drm_gem_objects_lookup(file_priv, u64_to_user_ptr(args->bo_handles), 711 exec->bo_count, &exec->bo); 712 713 if (ret) 714 goto fail_put_bo; 715 716 for (i = 0; i < exec->bo_count; i++) { 717 ret = vc4_bo_inc_usecnt(to_vc4_bo(exec->bo[i])); 718 if (ret) 719 goto fail_dec_usecnt; 720 } 721 722 return 0; 723 724 fail_dec_usecnt: 725 /* Decrease usecnt on acquired objects. 726 * We cannot rely on vc4_complete_exec() to release resources here, 727 * because vc4_complete_exec() has no information about which BO has 728 * had its ->usecnt incremented. 729 * To make things easier we just free everything explicitly and set 730 * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release' 731 * step. 732 */ 733 for (i-- ; i >= 0; i--) 734 vc4_bo_dec_usecnt(to_vc4_bo(exec->bo[i])); 735 736 fail_put_bo: 737 /* Release any reference to acquired objects. */ 738 for (i = 0; i < exec->bo_count && exec->bo[i]; i++) 739 drm_gem_object_put(exec->bo[i]); 740 741 kvfree(exec->bo); 742 exec->bo = NULL; 743 return ret; 744 } 745 746 static int 747 vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec) 748 { 749 struct drm_vc4_submit_cl *args = exec->args; 750 struct vc4_dev *vc4 = to_vc4_dev(dev); 751 void *temp = NULL; 752 void *bin; 753 int ret = 0; 754 uint32_t bin_offset = 0; 755 uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size, 756 16); 757 uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size; 758 uint32_t exec_size = uniforms_offset + args->uniforms_size; 759 uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) * 760 args->shader_rec_count); 761 struct vc4_bo *bo; 762 763 if (shader_rec_offset < args->bin_cl_size || 764 uniforms_offset < shader_rec_offset || 765 exec_size < uniforms_offset || 766 args->shader_rec_count >= (UINT_MAX / 767 sizeof(struct vc4_shader_state)) || 768 temp_size < exec_size) { 769 DRM_DEBUG("overflow in exec arguments\n"); 770 ret = -EINVAL; 771 goto fail; 772 } 773 774 /* Allocate space where we'll store the copied in user command lists 775 * and shader records. 776 * 777 * We don't just copy directly into the BOs because we need to 778 * read the contents back for validation, and I think the 779 * bo->vaddr is uncached access. 780 */ 781 temp = kvmalloc_array(temp_size, 1, GFP_KERNEL); 782 if (!temp) { 783 drm_err(dev, "Failed to allocate storage for copying " 784 "in bin/render CLs.\n"); 785 ret = -ENOMEM; 786 goto fail; 787 } 788 bin = temp + bin_offset; 789 exec->shader_rec_u = temp + shader_rec_offset; 790 exec->uniforms_u = temp + uniforms_offset; 791 exec->shader_state = temp + exec_size; 792 exec->shader_state_size = args->shader_rec_count; 793 794 if (copy_from_user(bin, 795 u64_to_user_ptr(args->bin_cl), 796 args->bin_cl_size)) { 797 ret = -EFAULT; 798 goto fail; 799 } 800 801 if (copy_from_user(exec->shader_rec_u, 802 u64_to_user_ptr(args->shader_rec), 803 args->shader_rec_size)) { 804 ret = -EFAULT; 805 goto fail; 806 } 807 808 if (copy_from_user(exec->uniforms_u, 809 u64_to_user_ptr(args->uniforms), 810 args->uniforms_size)) { 811 ret = -EFAULT; 812 goto fail; 813 } 814 815 bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL); 816 if (IS_ERR(bo)) { 817 drm_err(dev, "Couldn't allocate BO for binning\n"); 818 ret = PTR_ERR(bo); 819 goto fail; 820 } 821 exec->exec_bo = &bo->base; 822 823 list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head, 824 &exec->unref_list); 825 826 exec->ct0ca = exec->exec_bo->dma_addr + bin_offset; 827 828 exec->bin_u = bin; 829 830 exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset; 831 exec->shader_rec_p = exec->exec_bo->dma_addr + shader_rec_offset; 832 exec->shader_rec_size = args->shader_rec_size; 833 834 exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset; 835 exec->uniforms_p = exec->exec_bo->dma_addr + uniforms_offset; 836 exec->uniforms_size = args->uniforms_size; 837 838 ret = vc4_validate_bin_cl(dev, 839 exec->exec_bo->vaddr + bin_offset, 840 bin, 841 exec); 842 if (ret) 843 goto fail; 844 845 ret = vc4_validate_shader_recs(dev, exec); 846 if (ret) 847 goto fail; 848 849 if (exec->found_tile_binning_mode_config_packet) { 850 ret = vc4_v3d_bin_bo_get(vc4, &exec->bin_bo_used); 851 if (ret) 852 goto fail; 853 } 854 855 fail: 856 kvfree(temp); 857 return ret; 858 } 859 860 static void 861 vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec) 862 { 863 struct vc4_dev *vc4 = to_vc4_dev(dev); 864 unsigned long irqflags; 865 unsigned i; 866 867 /* If we got force-completed because of GPU reset rather than 868 * through our IRQ handler, signal the fence now. 869 */ 870 if (exec->fence) { 871 dma_fence_signal(exec->fence); 872 dma_fence_put(exec->fence); 873 } 874 875 if (exec->bo) { 876 for (i = 0; i < exec->bo_count; i++) { 877 struct vc4_bo *bo = to_vc4_bo(exec->bo[i]); 878 879 vc4_bo_dec_usecnt(bo); 880 drm_gem_object_put(exec->bo[i]); 881 } 882 kvfree(exec->bo); 883 } 884 885 while (!list_empty(&exec->unref_list)) { 886 struct vc4_bo *bo = list_first_entry(&exec->unref_list, 887 struct vc4_bo, unref_head); 888 list_del(&bo->unref_head); 889 drm_gem_object_put(&bo->base.base); 890 } 891 892 /* Free up the allocation of any bin slots we used. */ 893 spin_lock_irqsave(&vc4->job_lock, irqflags); 894 vc4->bin_alloc_used &= ~exec->bin_slots; 895 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 896 897 /* Release the reference on the binner BO if needed. */ 898 if (exec->bin_bo_used) 899 vc4_v3d_bin_bo_put(vc4); 900 901 /* Release the reference we had on the perf monitor. */ 902 vc4_perfmon_put(exec->perfmon); 903 904 vc4_v3d_pm_put(vc4); 905 906 kfree(exec); 907 } 908 909 void 910 vc4_job_handle_completed(struct vc4_dev *vc4) 911 { 912 unsigned long irqflags; 913 914 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 915 return; 916 917 spin_lock_irqsave(&vc4->job_lock, irqflags); 918 while (!list_empty(&vc4->job_done_list)) { 919 struct vc4_exec_info *exec = 920 list_first_entry(&vc4->job_done_list, 921 struct vc4_exec_info, head); 922 list_del(&exec->head); 923 924 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 925 vc4_complete_exec(&vc4->base, exec); 926 spin_lock_irqsave(&vc4->job_lock, irqflags); 927 } 928 929 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 930 } 931 932 /* Scheduled when any job has been completed, this walks the list of 933 * jobs that had completed and unrefs their BOs and frees their exec 934 * structs. 935 */ 936 static void 937 vc4_job_done_work(struct work_struct *work) 938 { 939 struct vc4_dev *vc4 = 940 container_of(work, struct vc4_dev, job_done_work); 941 942 vc4_job_handle_completed(vc4); 943 } 944 945 static int 946 vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev, 947 uint64_t seqno, 948 uint64_t *timeout_ns) 949 { 950 unsigned long start = jiffies; 951 int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true); 952 953 if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) { 954 uint64_t delta = jiffies_to_nsecs(jiffies - start); 955 956 if (*timeout_ns >= delta) 957 *timeout_ns -= delta; 958 } 959 960 return ret; 961 } 962 963 int 964 vc4_wait_seqno_ioctl(struct drm_device *dev, void *data, 965 struct drm_file *file_priv) 966 { 967 struct vc4_dev *vc4 = to_vc4_dev(dev); 968 struct drm_vc4_wait_seqno *args = data; 969 970 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 971 return -ENODEV; 972 973 return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno, 974 &args->timeout_ns); 975 } 976 977 int 978 vc4_wait_bo_ioctl(struct drm_device *dev, void *data, 979 struct drm_file *file_priv) 980 { 981 struct vc4_dev *vc4 = to_vc4_dev(dev); 982 int ret; 983 struct drm_vc4_wait_bo *args = data; 984 unsigned long timeout_jiffies = 985 usecs_to_jiffies(div_u64(args->timeout_ns, 1000)); 986 ktime_t start = ktime_get(); 987 u64 delta_ns; 988 989 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 990 return -ENODEV; 991 992 if (args->pad != 0) 993 return -EINVAL; 994 995 ret = drm_gem_dma_resv_wait(file_priv, args->handle, 996 true, timeout_jiffies); 997 998 /* Decrement the user's timeout, in case we got interrupted 999 * such that the ioctl will be restarted. 1000 */ 1001 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start)); 1002 if (delta_ns < args->timeout_ns) 1003 args->timeout_ns -= delta_ns; 1004 else 1005 args->timeout_ns = 0; 1006 1007 return ret; 1008 } 1009 1010 /** 1011 * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4. 1012 * @dev: DRM device 1013 * @data: ioctl argument 1014 * @file_priv: DRM file for this fd 1015 * 1016 * This is the main entrypoint for userspace to submit a 3D frame to 1017 * the GPU. Userspace provides the binner command list (if 1018 * applicable), and the kernel sets up the render command list to draw 1019 * to the framebuffer described in the ioctl, using the command lists 1020 * that the 3D engine's binner will produce. 1021 */ 1022 int 1023 vc4_submit_cl_ioctl(struct drm_device *dev, void *data, 1024 struct drm_file *file_priv) 1025 { 1026 struct vc4_dev *vc4 = to_vc4_dev(dev); 1027 struct vc4_file *vc4file = file_priv->driver_priv; 1028 struct drm_vc4_submit_cl *args = data; 1029 struct drm_syncobj *out_sync = NULL; 1030 struct vc4_exec_info *exec; 1031 struct drm_exec exec_ctx; 1032 struct dma_fence *in_fence; 1033 int ret = 0; 1034 1035 trace_vc4_submit_cl_ioctl(dev, args->bin_cl_size, 1036 args->shader_rec_size, 1037 args->bo_handle_count); 1038 1039 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 1040 return -ENODEV; 1041 1042 if (!vc4->v3d) { 1043 DRM_DEBUG("VC4_SUBMIT_CL with no VC4 V3D probed\n"); 1044 return -ENODEV; 1045 } 1046 1047 if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR | 1048 VC4_SUBMIT_CL_FIXED_RCL_ORDER | 1049 VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X | 1050 VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) { 1051 DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags); 1052 return -EINVAL; 1053 } 1054 1055 if (args->pad2 != 0) { 1056 DRM_DEBUG("Invalid pad: 0x%08x\n", args->pad2); 1057 return -EINVAL; 1058 } 1059 1060 exec = kzalloc_objs(*exec, 1); 1061 if (!exec) 1062 return -ENOMEM; 1063 1064 exec->dev = vc4; 1065 1066 ret = vc4_v3d_pm_get(vc4); 1067 if (ret) { 1068 kfree(exec); 1069 return ret; 1070 } 1071 1072 exec->args = args; 1073 INIT_LIST_HEAD(&exec->unref_list); 1074 1075 ret = vc4_cl_lookup_bos(dev, file_priv, exec); 1076 if (ret) 1077 goto fail; 1078 1079 if (args->perfmonid) { 1080 exec->perfmon = vc4_perfmon_find(vc4file, 1081 args->perfmonid); 1082 if (!exec->perfmon) { 1083 ret = -ENOENT; 1084 goto fail; 1085 } 1086 } 1087 1088 if (args->in_sync) { 1089 ret = drm_syncobj_find_fence(file_priv, args->in_sync, 1090 0, 0, &in_fence); 1091 if (ret) 1092 goto fail; 1093 1094 /* When the fence (or fence array) is exclusively from our 1095 * context we can skip the wait since jobs are executed in 1096 * order of their submission through this ioctl and this can 1097 * only have fences from a prior job. 1098 */ 1099 if (!dma_fence_match_context(in_fence, 1100 vc4->dma_fence_context)) { 1101 ret = dma_fence_wait(in_fence, true); 1102 if (ret) { 1103 dma_fence_put(in_fence); 1104 goto fail; 1105 } 1106 } 1107 1108 dma_fence_put(in_fence); 1109 } 1110 1111 if (exec->args->bin_cl_size != 0) { 1112 ret = vc4_get_bcl(dev, exec); 1113 if (ret) 1114 goto fail; 1115 } else { 1116 exec->ct0ca = 0; 1117 exec->ct0ea = 0; 1118 } 1119 1120 ret = vc4_get_rcl(dev, exec); 1121 if (ret) 1122 goto fail; 1123 1124 ret = vc4_lock_bo_reservations(exec, &exec_ctx); 1125 if (ret) 1126 goto fail; 1127 1128 if (args->out_sync) { 1129 out_sync = drm_syncobj_find(file_priv, args->out_sync); 1130 if (!out_sync) { 1131 ret = -EINVAL; 1132 goto fail_unreserve; 1133 } 1134 1135 /* We replace the fence in out_sync in vc4_queue_submit since 1136 * the render job could execute immediately after that call. 1137 * If it finishes before our ioctl processing resumes the 1138 * render job fence could already have been freed. 1139 */ 1140 } 1141 1142 /* Clear this out of the struct we'll be putting in the queue, 1143 * since it's part of our stack. 1144 */ 1145 exec->args = NULL; 1146 1147 ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync); 1148 1149 /* The syncobj isn't part of the exec data and we need to free our 1150 * reference even if job submission failed. 1151 */ 1152 if (out_sync) 1153 drm_syncobj_put(out_sync); 1154 1155 if (ret) 1156 goto fail_unreserve; 1157 1158 /* Return the seqno for our job. */ 1159 args->seqno = vc4->emit_seqno; 1160 1161 return 0; 1162 1163 fail_unreserve: 1164 drm_exec_fini(&exec_ctx); 1165 fail: 1166 vc4_complete_exec(&vc4->base, exec); 1167 1168 return ret; 1169 } 1170 1171 static void vc4_gem_destroy(struct drm_device *dev, void *unused); 1172 int vc4_gem_init(struct drm_device *dev) 1173 { 1174 struct vc4_dev *vc4 = to_vc4_dev(dev); 1175 int ret; 1176 1177 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 1178 return -ENODEV; 1179 1180 vc4->dma_fence_context = dma_fence_context_alloc(1); 1181 1182 INIT_LIST_HEAD(&vc4->bin_job_list); 1183 INIT_LIST_HEAD(&vc4->render_job_list); 1184 INIT_LIST_HEAD(&vc4->job_done_list); 1185 spin_lock_init(&vc4->job_lock); 1186 1187 INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work); 1188 timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0); 1189 1190 INIT_WORK(&vc4->job_done_work, vc4_job_done_work); 1191 1192 INIT_LIST_HEAD(&vc4->purgeable.list); 1193 1194 ret = drmm_mutex_init(dev, &vc4->purgeable.lock); 1195 if (ret) 1196 return ret; 1197 1198 return drmm_add_action_or_reset(dev, vc4_gem_destroy, NULL); 1199 } 1200 1201 static void vc4_gem_destroy(struct drm_device *dev, void *unused) 1202 { 1203 struct vc4_dev *vc4 = to_vc4_dev(dev); 1204 1205 /* Waiting for exec to finish would need to be done before 1206 * unregistering V3D. 1207 */ 1208 WARN_ON(vc4->emit_seqno != vc4->finished_seqno); 1209 1210 /* V3D should already have disabled its interrupt and cleared 1211 * the overflow allocation registers. Now free the object. 1212 */ 1213 if (vc4->bin_bo) { 1214 drm_gem_object_put(&vc4->bin_bo->base.base); 1215 vc4->bin_bo = NULL; 1216 } 1217 1218 if (vc4->hang_state) 1219 vc4_free_hang_state(dev, vc4->hang_state); 1220 } 1221 1222 int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data, 1223 struct drm_file *file_priv) 1224 { 1225 struct vc4_dev *vc4 = to_vc4_dev(dev); 1226 struct drm_vc4_gem_madvise *args = data; 1227 struct drm_gem_object *gem_obj; 1228 struct vc4_bo *bo; 1229 int ret; 1230 1231 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 1232 return -ENODEV; 1233 1234 switch (args->madv) { 1235 case VC4_MADV_DONTNEED: 1236 case VC4_MADV_WILLNEED: 1237 break; 1238 default: 1239 return -EINVAL; 1240 } 1241 1242 if (args->pad != 0) 1243 return -EINVAL; 1244 1245 gem_obj = drm_gem_object_lookup(file_priv, args->handle); 1246 if (!gem_obj) { 1247 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle); 1248 return -ENOENT; 1249 } 1250 1251 bo = to_vc4_bo(gem_obj); 1252 1253 /* Only BOs exposed to userspace can be purged. */ 1254 if (bo->madv == __VC4_MADV_NOTSUPP) { 1255 DRM_DEBUG("madvise not supported on this BO\n"); 1256 ret = -EINVAL; 1257 goto out_put_gem; 1258 } 1259 1260 /* Not sure it's safe to purge imported BOs. Let's just assume it's 1261 * not until proven otherwise. 1262 */ 1263 if (drm_gem_is_imported(gem_obj)) { 1264 DRM_DEBUG("madvise not supported on imported BOs\n"); 1265 ret = -EINVAL; 1266 goto out_put_gem; 1267 } 1268 1269 mutex_lock(&bo->madv_lock); 1270 1271 if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED && 1272 !refcount_read(&bo->usecnt)) { 1273 /* If the BO is about to be marked as purgeable, is not used 1274 * and is not already purgeable or purged, add it to the 1275 * purgeable list. 1276 */ 1277 vc4_bo_add_to_purgeable_pool(bo); 1278 } else if (args->madv == VC4_MADV_WILLNEED && 1279 bo->madv == VC4_MADV_DONTNEED && 1280 !refcount_read(&bo->usecnt)) { 1281 /* The BO has not been purged yet, just remove it from 1282 * the purgeable list. 1283 */ 1284 vc4_bo_remove_from_purgeable_pool(bo); 1285 } 1286 1287 /* Save the purged state. */ 1288 args->retained = bo->madv != __VC4_MADV_PURGED; 1289 1290 /* Update internal madv state only if the bo was not purged. */ 1291 if (bo->madv != __VC4_MADV_PURGED) 1292 bo->madv = args->madv; 1293 1294 mutex_unlock(&bo->madv_lock); 1295 1296 ret = 0; 1297 1298 out_put_gem: 1299 drm_gem_object_put(gem_obj); 1300 1301 return ret; 1302 } 1303