1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013 Red Hat 4 * Author: Rob Clark <robdclark@gmail.com> 5 */ 6 7 #include "drm/drm_drv.h" 8 9 #include "msm_gpu.h" 10 #include "msm_gem.h" 11 #include "msm_mmu.h" 12 #include "msm_fence.h" 13 #include "msm_gpu_trace.h" 14 //#include "adreno/adreno_gpu.h" 15 16 #include <linux/string_helpers.h> 17 #include <linux/devcoredump.h> 18 #include <linux/sched/task.h> 19 #include <linux/sched/mm.h> 20 #include <linux/utsname.h> 21 22 /* 23 * Power Management: 24 */ 25 26 static int enable_pwrrail(struct msm_gpu *gpu) 27 { 28 struct drm_device *dev = gpu->dev; 29 int ret = 0; 30 31 if (gpu->gpu_reg) { 32 ret = regulator_enable(gpu->gpu_reg); 33 if (ret) { 34 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_reg': %d\n", ret); 35 return ret; 36 } 37 } 38 39 if (gpu->gpu_cx) { 40 ret = regulator_enable(gpu->gpu_cx); 41 if (ret) { 42 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_cx': %d\n", ret); 43 return ret; 44 } 45 } 46 47 return 0; 48 } 49 50 static int disable_pwrrail(struct msm_gpu *gpu) 51 { 52 if (gpu->gpu_cx) 53 regulator_disable(gpu->gpu_cx); 54 if (gpu->gpu_reg) 55 regulator_disable(gpu->gpu_reg); 56 return 0; 57 } 58 59 static int enable_clk(struct msm_gpu *gpu) 60 { 61 if (gpu->core_clk && gpu->fast_rate) 62 dev_pm_opp_set_rate(&gpu->pdev->dev, gpu->fast_rate); 63 64 /* Set the RBBM timer rate to 19.2Mhz */ 65 if (gpu->rbbmtimer_clk) 66 clk_set_rate(gpu->rbbmtimer_clk, 19200000); 67 68 return clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks); 69 } 70 71 static int disable_clk(struct msm_gpu *gpu) 72 { 73 clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks); 74 75 /* 76 * Set the clock to a deliberately low rate. On older targets the clock 77 * speed had to be non zero to avoid problems. On newer targets this 78 * will be rounded down to zero anyway so it all works out. 79 */ 80 if (gpu->core_clk) 81 dev_pm_opp_set_rate(&gpu->pdev->dev, 27000000); 82 83 if (gpu->rbbmtimer_clk) 84 clk_set_rate(gpu->rbbmtimer_clk, 0); 85 86 return 0; 87 } 88 89 static int enable_axi(struct msm_gpu *gpu) 90 { 91 return clk_prepare_enable(gpu->ebi1_clk); 92 } 93 94 static int disable_axi(struct msm_gpu *gpu) 95 { 96 clk_disable_unprepare(gpu->ebi1_clk); 97 return 0; 98 } 99 100 int msm_gpu_pm_resume(struct msm_gpu *gpu) 101 { 102 int ret; 103 104 DBG("%s", gpu->name); 105 trace_msm_gpu_resume(0); 106 107 ret = enable_pwrrail(gpu); 108 if (ret) 109 return ret; 110 111 ret = enable_clk(gpu); 112 if (ret) 113 return ret; 114 115 ret = enable_axi(gpu); 116 if (ret) 117 return ret; 118 119 msm_devfreq_resume(gpu); 120 121 gpu->needs_hw_init = true; 122 123 return 0; 124 } 125 126 int msm_gpu_pm_suspend(struct msm_gpu *gpu) 127 { 128 int ret; 129 130 DBG("%s", gpu->name); 131 trace_msm_gpu_suspend(0); 132 133 msm_devfreq_suspend(gpu); 134 135 ret = disable_axi(gpu); 136 if (ret) 137 return ret; 138 139 ret = disable_clk(gpu); 140 if (ret) 141 return ret; 142 143 ret = disable_pwrrail(gpu); 144 if (ret) 145 return ret; 146 147 gpu->suspend_count++; 148 149 return 0; 150 } 151 152 void msm_gpu_show_fdinfo(struct msm_gpu *gpu, struct msm_context *ctx, 153 struct drm_printer *p) 154 { 155 drm_printf(p, "drm-engine-gpu:\t%llu ns\n", ctx->elapsed_ns); 156 drm_printf(p, "drm-cycles-gpu:\t%llu\n", ctx->cycles); 157 drm_printf(p, "drm-maxfreq-gpu:\t%u Hz\n", gpu->fast_rate); 158 } 159 160 int msm_gpu_hw_init(struct msm_gpu *gpu) 161 { 162 int ret; 163 164 WARN_ON(!mutex_is_locked(&gpu->lock)); 165 166 if (!gpu->needs_hw_init) 167 return 0; 168 169 disable_irq(gpu->irq); 170 ret = gpu->funcs->hw_init(gpu); 171 if (!ret) 172 gpu->needs_hw_init = false; 173 enable_irq(gpu->irq); 174 175 return ret; 176 } 177 178 #ifdef CONFIG_DEV_COREDUMP 179 static ssize_t msm_gpu_devcoredump_read(char *buffer, loff_t offset, 180 size_t count, void *data, size_t datalen) 181 { 182 struct msm_gpu *gpu = data; 183 struct drm_print_iterator iter; 184 struct drm_printer p; 185 struct msm_gpu_state *state; 186 187 state = msm_gpu_crashstate_get(gpu); 188 if (!state) 189 return 0; 190 191 iter.data = buffer; 192 iter.offset = 0; 193 iter.start = offset; 194 iter.remain = count; 195 196 p = drm_coredump_printer(&iter); 197 198 drm_printf(&p, "---\n"); 199 drm_printf(&p, "kernel: %s\n", init_utsname()->release); 200 drm_printf(&p, "module: " KBUILD_MODNAME "\n"); 201 drm_printf(&p, "time: %ptSp\n", &state->time); 202 if (state->comm) 203 drm_printf(&p, "comm: %s\n", state->comm); 204 if (state->cmd) 205 drm_printf(&p, "cmdline: %s\n", state->cmd); 206 207 gpu->funcs->show(gpu, state, &p); 208 209 msm_gpu_crashstate_put(gpu); 210 211 return count - iter.remain; 212 } 213 214 static void msm_gpu_devcoredump_free(void *data) 215 { 216 struct msm_gpu *gpu = data; 217 218 msm_gpu_crashstate_put(gpu); 219 } 220 221 static void msm_gpu_crashstate_get_bo(struct msm_gpu_state *state, 222 struct drm_gem_object *obj, u64 iova, 223 bool full, size_t offset, size_t size) 224 { 225 struct msm_gpu_state_bo *state_bo = &state->bos[state->nr_bos]; 226 struct msm_gem_object *msm_obj = to_msm_bo(obj); 227 228 /* Don't record write only objects */ 229 state_bo->size = size; 230 state_bo->flags = msm_obj->flags; 231 state_bo->iova = iova; 232 233 BUILD_BUG_ON(sizeof(state_bo->name) != sizeof(msm_obj->name)); 234 235 memcpy(state_bo->name, msm_obj->name, sizeof(state_bo->name)); 236 237 if (full) { 238 void *ptr; 239 240 state_bo->data = kvmalloc(size, GFP_KERNEL); 241 if (!state_bo->data) 242 goto out; 243 244 ptr = msm_gem_get_vaddr_active(obj); 245 if (IS_ERR(ptr)) { 246 kvfree(state_bo->data); 247 state_bo->data = NULL; 248 goto out; 249 } 250 251 memcpy(state_bo->data, ptr + offset, size); 252 msm_gem_put_vaddr_locked(obj); 253 } 254 out: 255 state->nr_bos++; 256 } 257 258 static void crashstate_get_bos(struct msm_gpu_state *state, struct msm_gem_submit *submit) 259 { 260 extern bool rd_full; 261 262 if (msm_context_is_vmbind(submit->queue->ctx)) { 263 struct drm_exec exec; 264 struct drm_gpuva *vma; 265 unsigned cnt = 0; 266 267 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 268 drm_exec_until_all_locked(&exec) { 269 cnt = 0; 270 271 drm_exec_lock_obj(&exec, drm_gpuvm_resv_obj(submit->vm)); 272 drm_exec_retry_on_contention(&exec); 273 274 drm_gpuvm_for_each_va (vma, submit->vm) { 275 if (!vma->gem.obj) 276 continue; 277 278 cnt++; 279 drm_exec_lock_obj(&exec, vma->gem.obj); 280 drm_exec_retry_on_contention(&exec); 281 } 282 283 } 284 285 drm_gpuvm_for_each_va (vma, submit->vm) 286 cnt++; 287 288 state->bos = kcalloc(cnt, sizeof(struct msm_gpu_state_bo), GFP_KERNEL); 289 290 if (state->bos) 291 drm_gpuvm_for_each_va(vma, submit->vm) { 292 bool dump = rd_full || (vma->flags & MSM_VMA_DUMP); 293 294 /* Skip MAP_NULL/PRR VMAs: */ 295 if (!vma->gem.obj) 296 continue; 297 298 msm_gpu_crashstate_get_bo(state, vma->gem.obj, vma->va.addr, 299 dump, vma->gem.offset, vma->va.range); 300 } 301 302 drm_exec_fini(&exec); 303 } else { 304 state->bos = kcalloc(submit->nr_bos, 305 sizeof(struct msm_gpu_state_bo), GFP_KERNEL); 306 307 for (int i = 0; state->bos && i < submit->nr_bos; i++) { 308 struct drm_gem_object *obj = submit->bos[i].obj; 309 bool dump = rd_full || (submit->bos[i].flags & MSM_SUBMIT_BO_DUMP); 310 311 msm_gem_lock(obj); 312 msm_gpu_crashstate_get_bo(state, obj, submit->bos[i].iova, 313 dump, 0, obj->size); 314 msm_gem_unlock(obj); 315 } 316 } 317 } 318 319 static void crashstate_get_vm_logs(struct msm_gpu_state *state, struct msm_gem_vm *vm) 320 { 321 uint32_t vm_log_len = (1 << vm->log_shift); 322 uint32_t vm_log_mask = vm_log_len - 1; 323 int first; 324 325 /* Bail if no log, or empty log: */ 326 if (!vm->log || !vm->log[0].op) 327 return; 328 329 mutex_lock(&vm->mmu_lock); 330 331 /* 332 * log_idx is the next entry to overwrite, meaning it is the oldest, or 333 * first, entry (other than the special case handled below where the 334 * log hasn't wrapped around yet) 335 */ 336 first = vm->log_idx; 337 338 if (!vm->log[first].op) { 339 /* 340 * If the next log entry has not been written yet, then only 341 * entries 0 to idx-1 are valid (ie. we haven't wrapped around 342 * yet) 343 */ 344 state->nr_vm_logs = MAX(0, first - 1); 345 first = 0; 346 } else { 347 state->nr_vm_logs = vm_log_len; 348 } 349 350 state->vm_logs = kmalloc_objs(vm->log[0], state->nr_vm_logs); 351 if (!state->vm_logs) { 352 state->nr_vm_logs = 0; 353 } 354 355 for (int i = 0; i < state->nr_vm_logs; i++) { 356 int idx = (i + first) & vm_log_mask; 357 358 state->vm_logs[i] = vm->log[idx]; 359 } 360 361 mutex_unlock(&vm->mmu_lock); 362 } 363 364 static void msm_gpu_crashstate_capture(struct msm_gpu *gpu, 365 struct msm_gem_submit *submit, struct msm_gpu_fault_info *fault_info, 366 char *comm, char *cmd) 367 { 368 struct msm_gpu_state *state; 369 370 /* Check if the target supports capturing crash state */ 371 if (!gpu->funcs->gpu_state_get) 372 return; 373 374 /* Only save one crash state at a time */ 375 if (gpu->crashstate) 376 return; 377 378 state = gpu->funcs->gpu_state_get(gpu); 379 if (IS_ERR_OR_NULL(state)) 380 return; 381 382 /* Fill in the additional crash state information */ 383 state->comm = kstrdup(comm, GFP_KERNEL); 384 state->cmd = kstrdup(cmd, GFP_KERNEL); 385 if (fault_info) 386 state->fault_info = *fault_info; 387 388 if (submit && state->fault_info.ttbr0) { 389 struct msm_gpu_fault_info *info = &state->fault_info; 390 struct msm_mmu *mmu = to_msm_vm(submit->vm)->mmu; 391 392 msm_iommu_pagetable_params(mmu, &info->pgtbl_ttbr0, 393 &info->asid); 394 msm_iommu_pagetable_walk(mmu, info->iova, info->ptes); 395 } 396 397 if (submit) { 398 crashstate_get_vm_logs(state, to_msm_vm(submit->vm)); 399 crashstate_get_bos(state, submit); 400 } 401 402 /* Set the active crash state to be dumped on failure */ 403 gpu->crashstate = state; 404 405 dev_coredumpm(&gpu->pdev->dev, THIS_MODULE, gpu, 0, GFP_KERNEL, 406 msm_gpu_devcoredump_read, msm_gpu_devcoredump_free); 407 } 408 #else 409 static void msm_gpu_crashstate_capture(struct msm_gpu *gpu, 410 struct msm_gem_submit *submit, struct msm_gpu_fault_info *fault_info, 411 char *comm, char *cmd) 412 { 413 } 414 #endif 415 416 /* 417 * Hangcheck detection for locked gpu: 418 */ 419 420 static struct msm_gem_submit * 421 find_submit(struct msm_ringbuffer *ring, uint32_t fence) 422 { 423 struct msm_gem_submit *submit; 424 unsigned long flags; 425 426 spin_lock_irqsave(&ring->submit_lock, flags); 427 list_for_each_entry(submit, &ring->submits, node) { 428 if (submit->seqno == fence) { 429 spin_unlock_irqrestore(&ring->submit_lock, flags); 430 return submit; 431 } 432 } 433 spin_unlock_irqrestore(&ring->submit_lock, flags); 434 435 return NULL; 436 } 437 438 static void retire_submits(struct msm_gpu *gpu); 439 440 static void get_comm_cmdline(struct msm_gem_submit *submit, char **comm, char **cmd) 441 { 442 struct msm_context *ctx = submit->queue->ctx; 443 struct task_struct *task; 444 445 WARN_ON(!mutex_is_locked(&submit->gpu->lock)); 446 447 /* Note that kstrdup will return NULL if argument is NULL: */ 448 *comm = kstrdup(ctx->comm, GFP_KERNEL); 449 *cmd = kstrdup(ctx->cmdline, GFP_KERNEL); 450 451 task = get_pid_task(submit->pid, PIDTYPE_PID); 452 if (!task) 453 return; 454 455 if (!*comm) 456 *comm = kstrdup(task->comm, GFP_KERNEL); 457 458 if (!*cmd) 459 *cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL); 460 461 put_task_struct(task); 462 } 463 464 static void recover_worker(struct kthread_work *work) 465 { 466 struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work); 467 struct drm_device *dev = gpu->dev; 468 struct msm_drm_private *priv = dev->dev_private; 469 struct msm_gem_submit *submit; 470 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu); 471 char *comm = NULL, *cmd = NULL; 472 unsigned int noreclaim_flag; 473 struct task_struct *task; 474 int i; 475 476 mutex_lock(&gpu->lock); 477 478 DRM_DEV_ERROR(dev->dev, "%s: hangcheck recover!\n", gpu->name); 479 480 submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1); 481 482 /* 483 * If the submit retired while we were waiting for the worker to run, 484 * or waiting to acquire the gpu lock, then nothing more to do. 485 */ 486 if (!submit) 487 goto out_unlock; 488 489 /* Increment the fault counts */ 490 submit->queue->faults++; 491 492 task = get_pid_task(submit->pid, PIDTYPE_PID); 493 if (!task) 494 gpu->global_faults++; 495 else { 496 struct msm_gem_vm *vm = to_msm_vm(submit->vm); 497 498 vm->faults++; 499 500 /* 501 * If userspace has opted-in to VM_BIND (and therefore userspace 502 * management of the VM), faults mark the VM as unusable. This 503 * matches vulkan expectations (vulkan is the main target for 504 * VM_BIND). 505 */ 506 if (!vm->managed) 507 msm_gem_vm_unusable(submit->vm); 508 } 509 510 noreclaim_flag = memalloc_noreclaim_save(); 511 512 get_comm_cmdline(submit, &comm, &cmd); 513 514 if (comm && cmd) { 515 DRM_DEV_ERROR(dev->dev, "%s: offending task: %s (%s)\n", 516 gpu->name, comm, cmd); 517 518 msm_rd_dump_submit(priv->hangrd, submit, 519 "offending task: %s (%s)", comm, cmd); 520 } else { 521 DRM_DEV_ERROR(dev->dev, "%s: offending task: unknown\n", gpu->name); 522 523 msm_rd_dump_submit(priv->hangrd, submit, NULL); 524 } 525 526 /* Record the crash state */ 527 pm_runtime_get_sync(&gpu->pdev->dev); 528 msm_gpu_crashstate_capture(gpu, submit, NULL, comm, cmd); 529 530 memalloc_noreclaim_restore(noreclaim_flag); 531 532 kfree(cmd); 533 kfree(comm); 534 535 /* 536 * Update all the rings with the latest and greatest fence.. this 537 * needs to happen after msm_rd_dump_submit() to ensure that the 538 * bo's referenced by the offending submit are still around. 539 */ 540 for (i = 0; i < gpu->nr_rings; i++) { 541 struct msm_ringbuffer *ring = gpu->rb[i]; 542 543 uint32_t fence = ring->memptrs->fence; 544 545 /* 546 * For the current (faulting?) ring/submit advance the fence by 547 * one more to clear the faulting submit 548 */ 549 if (ring == cur_ring) 550 ring->memptrs->fence = ++fence; 551 552 msm_update_fence(ring->fctx, fence); 553 } 554 555 /* retire completed submits, plus the one that hung: */ 556 retire_submits(gpu); 557 558 gpu->funcs->recover(gpu); 559 560 /* 561 * Replay all remaining submits starting with highest priority 562 * ring 563 */ 564 for (i = 0; i < gpu->nr_rings; i++) { 565 struct msm_ringbuffer *ring = gpu->rb[i]; 566 unsigned long flags; 567 568 spin_lock_irqsave(&ring->submit_lock, flags); 569 list_for_each_entry(submit, &ring->submits, node) { 570 /* 571 * If the submit uses an unusable vm make sure 572 * we don't actually run it 573 */ 574 if (to_msm_vm(submit->vm)->unusable) 575 submit->nr_cmds = 0; 576 gpu->funcs->submit(gpu, submit); 577 } 578 spin_unlock_irqrestore(&ring->submit_lock, flags); 579 } 580 581 pm_runtime_put(&gpu->pdev->dev); 582 583 out_unlock: 584 mutex_unlock(&gpu->lock); 585 586 msm_gpu_retire(gpu); 587 } 588 589 void msm_gpu_fault_crashstate_capture(struct msm_gpu *gpu, struct msm_gpu_fault_info *fault_info) 590 { 591 struct msm_gem_submit *submit; 592 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu); 593 char *comm = NULL, *cmd = NULL; 594 unsigned int noreclaim_flag; 595 596 mutex_lock(&gpu->lock); 597 598 submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1); 599 if (submit && submit->fault_dumped) 600 goto resume_smmu; 601 602 noreclaim_flag = memalloc_noreclaim_save(); 603 604 if (submit) { 605 get_comm_cmdline(submit, &comm, &cmd); 606 607 /* 608 * When we get GPU iova faults, we can get 1000s of them, 609 * but we really only want to log the first one. 610 */ 611 submit->fault_dumped = true; 612 } 613 614 /* Record the crash state */ 615 pm_runtime_get_sync(&gpu->pdev->dev); 616 msm_gpu_crashstate_capture(gpu, submit, fault_info, comm, cmd); 617 pm_runtime_put_sync(&gpu->pdev->dev); 618 619 memalloc_noreclaim_restore(noreclaim_flag); 620 621 kfree(cmd); 622 kfree(comm); 623 624 resume_smmu: 625 mutex_unlock(&gpu->lock); 626 } 627 628 static void hangcheck_timer_reset(struct msm_gpu *gpu) 629 { 630 struct msm_drm_private *priv = gpu->dev->dev_private; 631 mod_timer(&gpu->hangcheck_timer, 632 round_jiffies_up(jiffies + msecs_to_jiffies(priv->hangcheck_period))); 633 } 634 635 static bool made_progress(struct msm_gpu *gpu, struct msm_ringbuffer *ring) 636 { 637 if (ring->hangcheck_progress_retries >= DRM_MSM_HANGCHECK_PROGRESS_RETRIES) 638 return false; 639 640 if (!gpu->funcs->progress) 641 return false; 642 643 if (!gpu->funcs->progress(gpu, ring)) 644 return false; 645 646 ring->hangcheck_progress_retries++; 647 return true; 648 } 649 650 static void hangcheck_handler(struct timer_list *t) 651 { 652 struct msm_gpu *gpu = timer_container_of(gpu, t, hangcheck_timer); 653 struct drm_device *dev = gpu->dev; 654 struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu); 655 uint32_t fence = ring->memptrs->fence; 656 657 if (fence != ring->hangcheck_fence) { 658 /* some progress has been made.. ya! */ 659 ring->hangcheck_fence = fence; 660 ring->hangcheck_progress_retries = 0; 661 } else if (fence_before(fence, ring->fctx->last_fence) && 662 !made_progress(gpu, ring)) { 663 /* no progress and not done.. hung! */ 664 ring->hangcheck_fence = fence; 665 ring->hangcheck_progress_retries = 0; 666 DRM_DEV_ERROR(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n", 667 gpu->name, ring->id); 668 DRM_DEV_ERROR(dev->dev, "%s: completed fence: %u\n", 669 gpu->name, fence); 670 DRM_DEV_ERROR(dev->dev, "%s: submitted fence: %u\n", 671 gpu->name, ring->fctx->last_fence); 672 673 kthread_queue_work(gpu->worker, &gpu->recover_work); 674 } 675 676 /* if still more pending work, reset the hangcheck timer: */ 677 if (fence_after(ring->fctx->last_fence, ring->hangcheck_fence)) 678 hangcheck_timer_reset(gpu); 679 680 /* workaround for missing irq: */ 681 msm_gpu_retire(gpu); 682 } 683 684 /* 685 * Cmdstream submission/retirement: 686 */ 687 688 static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring, 689 struct msm_gem_submit *submit) 690 { 691 int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT; 692 volatile struct msm_gpu_submit_stats *stats; 693 u64 elapsed, clock = 0, cycles; 694 unsigned long flags; 695 696 stats = &ring->memptrs->stats[index]; 697 /* Convert 19.2Mhz alwayson ticks to nanoseconds for elapsed time */ 698 elapsed = (stats->alwayson_end - stats->alwayson_start) * 10000; 699 do_div(elapsed, 192); 700 701 cycles = stats->cpcycles_end - stats->cpcycles_start; 702 703 /* Calculate the clock frequency from the number of CP cycles */ 704 if (elapsed) { 705 clock = cycles * 1000; 706 do_div(clock, elapsed); 707 } 708 709 submit->queue->ctx->elapsed_ns += elapsed; 710 submit->queue->ctx->cycles += cycles; 711 712 trace_msm_gpu_submit_retired(submit, elapsed, clock, 713 stats->alwayson_start, stats->alwayson_end); 714 715 msm_submit_retire(submit); 716 717 pm_runtime_mark_last_busy(&gpu->pdev->dev); 718 719 spin_lock_irqsave(&ring->submit_lock, flags); 720 list_del(&submit->node); 721 spin_unlock_irqrestore(&ring->submit_lock, flags); 722 723 /* Update devfreq on transition from active->idle: */ 724 mutex_lock(&gpu->active_lock); 725 gpu->active_submits--; 726 WARN_ON(gpu->active_submits < 0); 727 if (!gpu->active_submits) { 728 msm_devfreq_idle(gpu); 729 pm_runtime_put_autosuspend(&gpu->pdev->dev); 730 } 731 732 mutex_unlock(&gpu->active_lock); 733 734 msm_gem_submit_put(submit); 735 } 736 737 static void retire_submits(struct msm_gpu *gpu) 738 { 739 int i; 740 741 /* Retire the commits starting with highest priority */ 742 for (i = 0; i < gpu->nr_rings; i++) { 743 struct msm_ringbuffer *ring = gpu->rb[i]; 744 745 while (true) { 746 struct msm_gem_submit *submit = NULL; 747 unsigned long flags; 748 749 spin_lock_irqsave(&ring->submit_lock, flags); 750 submit = list_first_entry_or_null(&ring->submits, 751 struct msm_gem_submit, node); 752 spin_unlock_irqrestore(&ring->submit_lock, flags); 753 754 /* 755 * If no submit, we are done. If submit->fence hasn't 756 * been signalled, then later submits are not signalled 757 * either, so we are also done. 758 */ 759 if (submit && dma_fence_is_signaled(submit->hw_fence)) { 760 retire_submit(gpu, ring, submit); 761 } else { 762 break; 763 } 764 } 765 } 766 767 wake_up_all(&gpu->retire_event); 768 } 769 770 static void retire_worker(struct kthread_work *work) 771 { 772 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work); 773 774 retire_submits(gpu); 775 } 776 777 /* call from irq handler to schedule work to retire bo's */ 778 void msm_gpu_retire(struct msm_gpu *gpu) 779 { 780 int i; 781 782 for (i = 0; i < gpu->nr_rings; i++) 783 msm_update_fence(gpu->rb[i]->fctx, gpu->rb[i]->memptrs->fence); 784 785 kthread_queue_work(gpu->worker, &gpu->retire_work); 786 } 787 788 /* add bo's to gpu's ring, and kick gpu: */ 789 void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit) 790 { 791 struct msm_ringbuffer *ring = submit->ring; 792 unsigned long flags; 793 794 WARN_ON(!mutex_is_locked(&gpu->lock)); 795 796 pm_runtime_get_sync(&gpu->pdev->dev); 797 798 msm_gpu_hw_init(gpu); 799 800 submit->seqno = submit->hw_fence->seqno; 801 802 /* 803 * ring->submits holds a ref to the submit, to deal with the case 804 * that a submit completes before msm_ioctl_gem_submit() returns. 805 */ 806 msm_gem_submit_get(submit); 807 808 spin_lock_irqsave(&ring->submit_lock, flags); 809 list_add_tail(&submit->node, &ring->submits); 810 spin_unlock_irqrestore(&ring->submit_lock, flags); 811 812 /* Update devfreq on transition from idle->active: */ 813 mutex_lock(&gpu->active_lock); 814 if (!gpu->active_submits) { 815 pm_runtime_get(&gpu->pdev->dev); 816 msm_devfreq_active(gpu); 817 } 818 gpu->active_submits++; 819 mutex_unlock(&gpu->active_lock); 820 821 gpu->funcs->submit(gpu, submit); 822 submit->ring->cur_ctx_seqno = submit->queue->ctx->seqno; 823 824 pm_runtime_put(&gpu->pdev->dev); 825 hangcheck_timer_reset(gpu); 826 } 827 828 /* 829 * Init/Cleanup: 830 */ 831 832 static irqreturn_t irq_handler(int irq, void *data) 833 { 834 struct msm_gpu *gpu = data; 835 return gpu->funcs->irq(gpu); 836 } 837 838 static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu) 839 { 840 int ret = devm_clk_bulk_get_all(&pdev->dev, &gpu->grp_clks); 841 842 if (ret < 1) { 843 gpu->nr_clocks = 0; 844 return ret; 845 } 846 847 gpu->nr_clocks = ret; 848 849 gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks, 850 gpu->nr_clocks, "core"); 851 852 gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks, 853 gpu->nr_clocks, "rbbmtimer"); 854 855 return 0; 856 } 857 858 /* Return a new address space for a msm_drm_private instance */ 859 struct drm_gpuvm * 860 msm_gpu_create_private_vm(struct msm_gpu *gpu, struct task_struct *task, 861 bool kernel_managed) 862 { 863 struct drm_gpuvm *vm = NULL; 864 865 if (!gpu) 866 return NULL; 867 868 /* 869 * If the target doesn't support private address spaces then return 870 * the global one 871 */ 872 if (gpu->funcs->create_private_vm) { 873 vm = gpu->funcs->create_private_vm(gpu, kernel_managed); 874 if (!IS_ERR(vm)) 875 to_msm_vm(vm)->pid = get_pid(task_pid(task)); 876 } 877 878 if (IS_ERR_OR_NULL(vm)) 879 vm = drm_gpuvm_get(gpu->vm); 880 881 return vm; 882 } 883 884 int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev, 885 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs, 886 const char *name, struct msm_gpu_config *config) 887 { 888 struct msm_drm_private *priv = drm->dev_private; 889 int i, ret, nr_rings = config->nr_rings; 890 void *memptrs; 891 uint64_t memptrs_iova; 892 893 gpu->dev = drm; 894 gpu->funcs = funcs; 895 gpu->name = name; 896 897 gpu->worker = kthread_run_worker(0, "gpu-worker"); 898 if (IS_ERR(gpu->worker)) { 899 ret = PTR_ERR(gpu->worker); 900 gpu->worker = NULL; 901 goto fail; 902 } 903 904 sched_set_fifo_low(gpu->worker->task); 905 906 mutex_init(&gpu->active_lock); 907 mutex_init(&gpu->lock); 908 init_waitqueue_head(&gpu->retire_event); 909 kthread_init_work(&gpu->retire_work, retire_worker); 910 kthread_init_work(&gpu->recover_work, recover_worker); 911 912 priv->hangcheck_period = DRM_MSM_HANGCHECK_DEFAULT_PERIOD; 913 914 /* 915 * If progress detection is supported, halve the hangcheck timer 916 * duration, as it takes two iterations of the hangcheck handler 917 * to detect a hang. 918 */ 919 if (funcs->progress) 920 priv->hangcheck_period /= 2; 921 922 timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0); 923 924 /* Map registers: */ 925 gpu->mmio = msm_ioremap(pdev, config->ioname); 926 if (IS_ERR(gpu->mmio)) { 927 ret = PTR_ERR(gpu->mmio); 928 goto fail; 929 } 930 931 /* Get Interrupt: */ 932 gpu->irq = platform_get_irq(pdev, 0); 933 if (gpu->irq < 0) { 934 ret = gpu->irq; 935 goto fail; 936 } 937 938 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 939 IRQF_TRIGGER_HIGH, "gpu-irq", gpu); 940 if (ret) { 941 DRM_DEV_ERROR(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret); 942 goto fail; 943 } 944 945 ret = get_clocks(pdev, gpu); 946 if (ret) 947 goto fail; 948 949 gpu->ebi1_clk = msm_clk_get(pdev, "bus"); 950 DBG("ebi1_clk: %p", gpu->ebi1_clk); 951 if (IS_ERR(gpu->ebi1_clk)) 952 gpu->ebi1_clk = NULL; 953 954 /* Acquire regulators: */ 955 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd"); 956 DBG("gpu_reg: %p", gpu->gpu_reg); 957 if (IS_ERR(gpu->gpu_reg)) 958 gpu->gpu_reg = NULL; 959 960 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx"); 961 DBG("gpu_cx: %p", gpu->gpu_cx); 962 if (IS_ERR(gpu->gpu_cx)) 963 gpu->gpu_cx = NULL; 964 965 platform_set_drvdata(pdev, &gpu->adreno_smmu); 966 967 msm_devfreq_init(gpu); 968 969 gpu->vm = gpu->funcs->create_vm(gpu, pdev); 970 if (IS_ERR(gpu->vm)) { 971 ret = PTR_ERR(gpu->vm); 972 goto fail; 973 } 974 975 memptrs = msm_gem_kernel_new(drm, 976 sizeof(struct msm_rbmemptrs) * nr_rings, 977 check_apriv(gpu, MSM_BO_WC), gpu->vm, &gpu->memptrs_bo, 978 &memptrs_iova); 979 980 if (IS_ERR(memptrs)) { 981 ret = PTR_ERR(memptrs); 982 DRM_DEV_ERROR(drm->dev, "could not allocate memptrs: %d\n", ret); 983 goto fail; 984 } 985 986 msm_gem_object_set_name(gpu->memptrs_bo, "memptrs"); 987 988 if (nr_rings > ARRAY_SIZE(gpu->rb)) { 989 DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n", 990 ARRAY_SIZE(gpu->rb)); 991 nr_rings = ARRAY_SIZE(gpu->rb); 992 } 993 994 /* Create ringbuffer(s): */ 995 for (i = 0; i < nr_rings; i++) { 996 gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova); 997 998 if (IS_ERR(gpu->rb[i])) { 999 ret = PTR_ERR(gpu->rb[i]); 1000 DRM_DEV_ERROR(drm->dev, 1001 "could not create ringbuffer %d: %d\n", i, ret); 1002 goto fail; 1003 } 1004 1005 memptrs += sizeof(struct msm_rbmemptrs); 1006 memptrs_iova += sizeof(struct msm_rbmemptrs); 1007 } 1008 1009 gpu->nr_rings = nr_rings; 1010 1011 refcount_set(&gpu->sysprof_active, 1); 1012 1013 mutex_init(&gpu->perfcntr_lock); 1014 1015 if (gpu->num_perfcntr_groups > 0) { 1016 gpu->perfcntrs = msm_perfcntr_init(gpu); 1017 if (IS_ERR(gpu->perfcntrs)) { 1018 ret = PTR_ERR(gpu->perfcntrs); 1019 gpu->perfcntrs = NULL; 1020 goto fail; 1021 } 1022 } 1023 1024 return 0; 1025 1026 fail: 1027 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { 1028 msm_ringbuffer_destroy(gpu->rb[i]); 1029 gpu->rb[i] = NULL; 1030 } 1031 1032 msm_gem_kernel_put(gpu->memptrs_bo, gpu->vm); 1033 1034 platform_set_drvdata(pdev, NULL); 1035 return ret; 1036 } 1037 1038 void msm_gpu_cleanup(struct msm_gpu *gpu) 1039 { 1040 int i; 1041 1042 DBG("%s", gpu->name); 1043 1044 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { 1045 msm_ringbuffer_destroy(gpu->rb[i]); 1046 gpu->rb[i] = NULL; 1047 } 1048 1049 msm_gem_kernel_put(gpu->memptrs_bo, gpu->vm); 1050 1051 if (!IS_ERR_OR_NULL(gpu->vm)) { 1052 struct msm_mmu *mmu = to_msm_vm(gpu->vm)->mmu; 1053 mmu->funcs->detach(mmu); 1054 drm_gpuvm_put(gpu->vm); 1055 } 1056 1057 if (gpu->worker) { 1058 kthread_destroy_worker(gpu->worker); 1059 } 1060 1061 msm_devfreq_cleanup(gpu); 1062 msm_perfcntr_cleanup(gpu); 1063 1064 platform_set_drvdata(gpu->pdev, NULL); 1065 } 1066