1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
7 #include "drm/drm_drv.h"
12 #include "msm_fence.h"
13 #include "msm_gpu_trace.h"
14 #include "adreno/adreno_gpu.h"
16 #include <generated/utsrelease.h>
17 #include <linux/string_helpers.h>
18 #include <linux/devcoredump.h>
19 #include <linux/sched/task.h>
25 static int enable_pwrrail(struct msm_gpu *gpu)
27 struct drm_device *dev = gpu->dev;
31 ret = regulator_enable(gpu->gpu_reg);
33 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
39 ret = regulator_enable(gpu->gpu_cx);
41 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
49 static int disable_pwrrail(struct msm_gpu *gpu)
52 regulator_disable(gpu->gpu_cx);
54 regulator_disable(gpu->gpu_reg);
58 static int enable_clk(struct msm_gpu *gpu)
60 if (gpu->core_clk && gpu->fast_rate)
61 dev_pm_opp_set_rate(&gpu->pdev->dev, gpu->fast_rate);
63 /* Set the RBBM timer rate to 19.2Mhz */
64 if (gpu->rbbmtimer_clk)
65 clk_set_rate(gpu->rbbmtimer_clk, 19200000);
67 return clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
70 static int disable_clk(struct msm_gpu *gpu)
72 clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
75 * Set the clock to a deliberately low rate. On older targets the clock
76 * speed had to be non zero to avoid problems. On newer targets this
77 * will be rounded down to zero anyway so it all works out.
80 dev_pm_opp_set_rate(&gpu->pdev->dev, 27000000);
82 if (gpu->rbbmtimer_clk)
83 clk_set_rate(gpu->rbbmtimer_clk, 0);
88 static int enable_axi(struct msm_gpu *gpu)
90 return clk_prepare_enable(gpu->ebi1_clk);
93 static int disable_axi(struct msm_gpu *gpu)
95 clk_disable_unprepare(gpu->ebi1_clk);
99 int msm_gpu_pm_resume(struct msm_gpu *gpu)
103 DBG("%s", gpu->name);
104 trace_msm_gpu_resume(0);
106 ret = enable_pwrrail(gpu);
110 ret = enable_clk(gpu);
114 ret = enable_axi(gpu);
118 msm_devfreq_resume(gpu);
120 gpu->needs_hw_init = true;
125 int msm_gpu_pm_suspend(struct msm_gpu *gpu)
129 DBG("%s", gpu->name);
130 trace_msm_gpu_suspend(0);
132 msm_devfreq_suspend(gpu);
134 ret = disable_axi(gpu);
138 ret = disable_clk(gpu);
142 ret = disable_pwrrail(gpu);
146 gpu->suspend_count++;
151 void msm_gpu_show_fdinfo(struct msm_gpu *gpu, struct msm_file_private *ctx,
152 struct drm_printer *p)
154 drm_printf(p, "drm-engine-gpu:\t%llu ns\n", ctx->elapsed_ns);
155 drm_printf(p, "drm-cycles-gpu:\t%llu\n", ctx->cycles);
156 drm_printf(p, "drm-maxfreq-gpu:\t%u Hz\n", gpu->fast_rate);
159 int msm_gpu_hw_init(struct msm_gpu *gpu)
163 WARN_ON(!mutex_is_locked(&gpu->lock));
165 if (!gpu->needs_hw_init)
168 disable_irq(gpu->irq);
169 ret = gpu->funcs->hw_init(gpu);
171 gpu->needs_hw_init = false;
172 enable_irq(gpu->irq);
177 #ifdef CONFIG_DEV_COREDUMP
178 static ssize_t msm_gpu_devcoredump_read(char *buffer, loff_t offset,
179 size_t count, void *data, size_t datalen)
181 struct msm_gpu *gpu = data;
182 struct drm_print_iterator iter;
183 struct drm_printer p;
184 struct msm_gpu_state *state;
186 state = msm_gpu_crashstate_get(gpu);
195 p = drm_coredump_printer(&iter);
197 drm_printf(&p, "---\n");
198 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
199 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
200 drm_printf(&p, "time: %lld.%09ld\n",
201 state->time.tv_sec, state->time.tv_nsec);
203 drm_printf(&p, "comm: %s\n", state->comm);
205 drm_printf(&p, "cmdline: %s\n", state->cmd);
207 gpu->funcs->show(gpu, state, &p);
209 msm_gpu_crashstate_put(gpu);
211 return count - iter.remain;
214 static void msm_gpu_devcoredump_free(void *data)
216 struct msm_gpu *gpu = data;
218 msm_gpu_crashstate_put(gpu);
221 static void msm_gpu_crashstate_get_bo(struct msm_gpu_state *state,
222 struct drm_gem_object *obj, u64 iova, bool full)
224 struct msm_gpu_state_bo *state_bo = &state->bos[state->nr_bos];
226 /* Don't record write only objects */
227 state_bo->size = obj->size;
228 state_bo->iova = iova;
230 BUILD_BUG_ON(sizeof(state_bo->name) != sizeof(to_msm_bo(obj)->name));
232 memcpy(state_bo->name, to_msm_bo(obj)->name, sizeof(state_bo->name));
237 state_bo->data = kvmalloc(obj->size, GFP_KERNEL);
242 ptr = msm_gem_get_vaddr_active(obj);
245 kvfree(state_bo->data);
246 state_bo->data = NULL;
250 memcpy(state_bo->data, ptr, obj->size);
251 msm_gem_put_vaddr(obj);
257 static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
258 struct msm_gem_submit *submit, char *comm, char *cmd)
260 struct msm_gpu_state *state;
262 /* Check if the target supports capturing crash state */
263 if (!gpu->funcs->gpu_state_get)
266 /* Only save one crash state at a time */
270 state = gpu->funcs->gpu_state_get(gpu);
271 if (IS_ERR_OR_NULL(state))
274 /* Fill in the additional crash state information */
275 state->comm = kstrdup(comm, GFP_KERNEL);
276 state->cmd = kstrdup(cmd, GFP_KERNEL);
277 state->fault_info = gpu->fault_info;
282 state->bos = kcalloc(submit->nr_bos,
283 sizeof(struct msm_gpu_state_bo), GFP_KERNEL);
285 for (i = 0; state->bos && i < submit->nr_bos; i++) {
286 msm_gpu_crashstate_get_bo(state, submit->bos[i].obj,
288 should_dump(submit, i));
292 /* Set the active crash state to be dumped on failure */
293 gpu->crashstate = state;
295 /* FIXME: Release the crashstate if this errors out? */
296 dev_coredumpm(gpu->dev->dev, THIS_MODULE, gpu, 0, GFP_KERNEL,
297 msm_gpu_devcoredump_read, msm_gpu_devcoredump_free);
300 static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
301 struct msm_gem_submit *submit, char *comm, char *cmd)
307 * Hangcheck detection for locked gpu:
310 static struct msm_gem_submit *
311 find_submit(struct msm_ringbuffer *ring, uint32_t fence)
313 struct msm_gem_submit *submit;
316 spin_lock_irqsave(&ring->submit_lock, flags);
317 list_for_each_entry(submit, &ring->submits, node) {
318 if (submit->seqno == fence) {
319 spin_unlock_irqrestore(&ring->submit_lock, flags);
323 spin_unlock_irqrestore(&ring->submit_lock, flags);
328 static void retire_submits(struct msm_gpu *gpu);
330 static void get_comm_cmdline(struct msm_gem_submit *submit, char **comm, char **cmd)
332 struct msm_file_private *ctx = submit->queue->ctx;
333 struct task_struct *task;
335 WARN_ON(!mutex_is_locked(&submit->gpu->lock));
337 /* Note that kstrdup will return NULL if argument is NULL: */
338 *comm = kstrdup(ctx->comm, GFP_KERNEL);
339 *cmd = kstrdup(ctx->cmdline, GFP_KERNEL);
341 task = get_pid_task(submit->pid, PIDTYPE_PID);
346 *comm = kstrdup(task->comm, GFP_KERNEL);
349 *cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL);
351 put_task_struct(task);
354 static void recover_worker(struct kthread_work *work)
356 struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
357 struct drm_device *dev = gpu->dev;
358 struct msm_drm_private *priv = dev->dev_private;
359 struct msm_gem_submit *submit;
360 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
361 char *comm = NULL, *cmd = NULL;
364 mutex_lock(&gpu->lock);
366 DRM_DEV_ERROR(dev->dev, "%s: hangcheck recover!\n", gpu->name);
368 submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1);
370 /* Increment the fault counts */
371 submit->queue->faults++;
373 submit->aspace->faults++;
375 get_comm_cmdline(submit, &comm, &cmd);
378 DRM_DEV_ERROR(dev->dev, "%s: offending task: %s (%s)\n",
379 gpu->name, comm, cmd);
381 msm_rd_dump_submit(priv->hangrd, submit,
382 "offending task: %s (%s)", comm, cmd);
384 msm_rd_dump_submit(priv->hangrd, submit, NULL);
388 * We couldn't attribute this fault to any particular context,
389 * so increment the global fault count instead.
391 gpu->global_faults++;
394 /* Record the crash state */
395 pm_runtime_get_sync(&gpu->pdev->dev);
396 msm_gpu_crashstate_capture(gpu, submit, comm, cmd);
402 * Update all the rings with the latest and greatest fence.. this
403 * needs to happen after msm_rd_dump_submit() to ensure that the
404 * bo's referenced by the offending submit are still around.
406 for (i = 0; i < gpu->nr_rings; i++) {
407 struct msm_ringbuffer *ring = gpu->rb[i];
409 uint32_t fence = ring->memptrs->fence;
412 * For the current (faulting?) ring/submit advance the fence by
413 * one more to clear the faulting submit
415 if (ring == cur_ring)
416 ring->memptrs->fence = ++fence;
418 msm_update_fence(ring->fctx, fence);
421 if (msm_gpu_active(gpu)) {
422 /* retire completed submits, plus the one that hung: */
425 gpu->funcs->recover(gpu);
428 * Replay all remaining submits starting with highest priority
431 for (i = 0; i < gpu->nr_rings; i++) {
432 struct msm_ringbuffer *ring = gpu->rb[i];
435 spin_lock_irqsave(&ring->submit_lock, flags);
436 list_for_each_entry(submit, &ring->submits, node)
437 gpu->funcs->submit(gpu, submit);
438 spin_unlock_irqrestore(&ring->submit_lock, flags);
442 pm_runtime_put(&gpu->pdev->dev);
444 mutex_unlock(&gpu->lock);
449 static void fault_worker(struct kthread_work *work)
451 struct msm_gpu *gpu = container_of(work, struct msm_gpu, fault_work);
452 struct msm_gem_submit *submit;
453 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
454 char *comm = NULL, *cmd = NULL;
456 mutex_lock(&gpu->lock);
458 submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1);
459 if (submit && submit->fault_dumped)
463 get_comm_cmdline(submit, &comm, &cmd);
466 * When we get GPU iova faults, we can get 1000s of them,
467 * but we really only want to log the first one.
469 submit->fault_dumped = true;
472 /* Record the crash state */
473 pm_runtime_get_sync(&gpu->pdev->dev);
474 msm_gpu_crashstate_capture(gpu, submit, comm, cmd);
475 pm_runtime_put_sync(&gpu->pdev->dev);
481 memset(&gpu->fault_info, 0, sizeof(gpu->fault_info));
482 gpu->aspace->mmu->funcs->resume_translation(gpu->aspace->mmu);
484 mutex_unlock(&gpu->lock);
487 static void hangcheck_timer_reset(struct msm_gpu *gpu)
489 struct msm_drm_private *priv = gpu->dev->dev_private;
490 mod_timer(&gpu->hangcheck_timer,
491 round_jiffies_up(jiffies + msecs_to_jiffies(priv->hangcheck_period)));
494 static bool made_progress(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
496 if (ring->hangcheck_progress_retries >= DRM_MSM_HANGCHECK_PROGRESS_RETRIES)
499 if (!gpu->funcs->progress)
502 if (!gpu->funcs->progress(gpu, ring))
505 ring->hangcheck_progress_retries++;
509 static void hangcheck_handler(struct timer_list *t)
511 struct msm_gpu *gpu = from_timer(gpu, t, hangcheck_timer);
512 struct drm_device *dev = gpu->dev;
513 struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
514 uint32_t fence = ring->memptrs->fence;
516 if (fence != ring->hangcheck_fence) {
517 /* some progress has been made.. ya! */
518 ring->hangcheck_fence = fence;
519 ring->hangcheck_progress_retries = 0;
520 } else if (fence_before(fence, ring->fctx->last_fence) &&
521 !made_progress(gpu, ring)) {
522 /* no progress and not done.. hung! */
523 ring->hangcheck_fence = fence;
524 ring->hangcheck_progress_retries = 0;
525 DRM_DEV_ERROR(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n",
526 gpu->name, ring->id);
527 DRM_DEV_ERROR(dev->dev, "%s: completed fence: %u\n",
529 DRM_DEV_ERROR(dev->dev, "%s: submitted fence: %u\n",
530 gpu->name, ring->fctx->last_fence);
532 kthread_queue_work(gpu->worker, &gpu->recover_work);
535 /* if still more pending work, reset the hangcheck timer: */
536 if (fence_after(ring->fctx->last_fence, ring->hangcheck_fence))
537 hangcheck_timer_reset(gpu);
539 /* workaround for missing irq: */
544 * Performance Counters:
547 /* called under perf_lock */
548 static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
550 uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
551 int i, n = min(ncntrs, gpu->num_perfcntrs);
553 /* read current values: */
554 for (i = 0; i < gpu->num_perfcntrs; i++)
555 current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
558 for (i = 0; i < n; i++)
559 cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
561 /* save current values: */
562 for (i = 0; i < gpu->num_perfcntrs; i++)
563 gpu->last_cntrs[i] = current_cntrs[i];
568 static void update_sw_cntrs(struct msm_gpu *gpu)
574 spin_lock_irqsave(&gpu->perf_lock, flags);
575 if (!gpu->perfcntr_active)
579 elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
581 gpu->totaltime += elapsed;
582 if (gpu->last_sample.active)
583 gpu->activetime += elapsed;
585 gpu->last_sample.active = msm_gpu_active(gpu);
586 gpu->last_sample.time = time;
589 spin_unlock_irqrestore(&gpu->perf_lock, flags);
592 void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
596 pm_runtime_get_sync(&gpu->pdev->dev);
598 spin_lock_irqsave(&gpu->perf_lock, flags);
599 /* we could dynamically enable/disable perfcntr registers too.. */
600 gpu->last_sample.active = msm_gpu_active(gpu);
601 gpu->last_sample.time = ktime_get();
602 gpu->activetime = gpu->totaltime = 0;
603 gpu->perfcntr_active = true;
604 update_hw_cntrs(gpu, 0, NULL);
605 spin_unlock_irqrestore(&gpu->perf_lock, flags);
608 void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
610 gpu->perfcntr_active = false;
611 pm_runtime_put_sync(&gpu->pdev->dev);
614 /* returns -errno or # of cntrs sampled */
615 int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
616 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
621 spin_lock_irqsave(&gpu->perf_lock, flags);
623 if (!gpu->perfcntr_active) {
628 *activetime = gpu->activetime;
629 *totaltime = gpu->totaltime;
631 gpu->activetime = gpu->totaltime = 0;
633 ret = update_hw_cntrs(gpu, ncntrs, cntrs);
636 spin_unlock_irqrestore(&gpu->perf_lock, flags);
642 * Cmdstream submission/retirement:
645 static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
646 struct msm_gem_submit *submit)
648 int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT;
649 volatile struct msm_gpu_submit_stats *stats;
650 u64 elapsed, clock = 0, cycles;
653 stats = &ring->memptrs->stats[index];
654 /* Convert 19.2Mhz alwayson ticks to nanoseconds for elapsed time */
655 elapsed = (stats->alwayson_end - stats->alwayson_start) * 10000;
656 do_div(elapsed, 192);
658 cycles = stats->cpcycles_end - stats->cpcycles_start;
660 /* Calculate the clock frequency from the number of CP cycles */
662 clock = cycles * 1000;
663 do_div(clock, elapsed);
666 submit->queue->ctx->elapsed_ns += elapsed;
667 submit->queue->ctx->cycles += cycles;
669 trace_msm_gpu_submit_retired(submit, elapsed, clock,
670 stats->alwayson_start, stats->alwayson_end);
672 msm_submit_retire(submit);
674 pm_runtime_mark_last_busy(&gpu->pdev->dev);
676 spin_lock_irqsave(&ring->submit_lock, flags);
677 list_del(&submit->node);
678 spin_unlock_irqrestore(&ring->submit_lock, flags);
680 /* Update devfreq on transition from active->idle: */
681 mutex_lock(&gpu->active_lock);
682 gpu->active_submits--;
683 WARN_ON(gpu->active_submits < 0);
684 if (!gpu->active_submits) {
685 msm_devfreq_idle(gpu);
686 pm_runtime_put_autosuspend(&gpu->pdev->dev);
689 mutex_unlock(&gpu->active_lock);
691 msm_gem_submit_put(submit);
694 static void retire_submits(struct msm_gpu *gpu)
698 /* Retire the commits starting with highest priority */
699 for (i = 0; i < gpu->nr_rings; i++) {
700 struct msm_ringbuffer *ring = gpu->rb[i];
703 struct msm_gem_submit *submit = NULL;
706 spin_lock_irqsave(&ring->submit_lock, flags);
707 submit = list_first_entry_or_null(&ring->submits,
708 struct msm_gem_submit, node);
709 spin_unlock_irqrestore(&ring->submit_lock, flags);
712 * If no submit, we are done. If submit->fence hasn't
713 * been signalled, then later submits are not signalled
714 * either, so we are also done.
716 if (submit && dma_fence_is_signaled(submit->hw_fence)) {
717 retire_submit(gpu, ring, submit);
724 wake_up_all(&gpu->retire_event);
727 static void retire_worker(struct kthread_work *work)
729 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
734 /* call from irq handler to schedule work to retire bo's */
735 void msm_gpu_retire(struct msm_gpu *gpu)
739 for (i = 0; i < gpu->nr_rings; i++)
740 msm_update_fence(gpu->rb[i]->fctx, gpu->rb[i]->memptrs->fence);
742 kthread_queue_work(gpu->worker, &gpu->retire_work);
743 update_sw_cntrs(gpu);
746 /* add bo's to gpu's ring, and kick gpu: */
747 void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
749 struct msm_ringbuffer *ring = submit->ring;
752 pm_runtime_get_sync(&gpu->pdev->dev);
754 mutex_lock(&gpu->lock);
756 msm_gpu_hw_init(gpu);
758 update_sw_cntrs(gpu);
761 * ring->submits holds a ref to the submit, to deal with the case
762 * that a submit completes before msm_ioctl_gem_submit() returns.
764 msm_gem_submit_get(submit);
766 spin_lock_irqsave(&ring->submit_lock, flags);
767 list_add_tail(&submit->node, &ring->submits);
768 spin_unlock_irqrestore(&ring->submit_lock, flags);
770 /* Update devfreq on transition from idle->active: */
771 mutex_lock(&gpu->active_lock);
772 if (!gpu->active_submits) {
773 pm_runtime_get(&gpu->pdev->dev);
774 msm_devfreq_active(gpu);
776 gpu->active_submits++;
777 mutex_unlock(&gpu->active_lock);
779 gpu->funcs->submit(gpu, submit);
780 gpu->cur_ctx_seqno = submit->queue->ctx->seqno;
782 hangcheck_timer_reset(gpu);
784 mutex_unlock(&gpu->lock);
786 pm_runtime_put(&gpu->pdev->dev);
793 static irqreturn_t irq_handler(int irq, void *data)
795 struct msm_gpu *gpu = data;
796 return gpu->funcs->irq(gpu);
799 static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu)
801 int ret = devm_clk_bulk_get_all(&pdev->dev, &gpu->grp_clks);
808 gpu->nr_clocks = ret;
810 gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
811 gpu->nr_clocks, "core");
813 gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
814 gpu->nr_clocks, "rbbmtimer");
819 /* Return a new address space for a msm_drm_private instance */
820 struct msm_gem_address_space *
821 msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task)
823 struct msm_gem_address_space *aspace = NULL;
828 * If the target doesn't support private address spaces then return
831 if (gpu->funcs->create_private_address_space) {
832 aspace = gpu->funcs->create_private_address_space(gpu);
834 aspace->pid = get_pid(task_pid(task));
837 if (IS_ERR_OR_NULL(aspace))
838 aspace = msm_gem_address_space_get(gpu->aspace);
843 int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
844 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
845 const char *name, struct msm_gpu_config *config)
847 struct msm_drm_private *priv = drm->dev_private;
848 int i, ret, nr_rings = config->nr_rings;
850 uint64_t memptrs_iova;
852 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
853 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
859 gpu->worker = kthread_create_worker(0, "gpu-worker");
860 if (IS_ERR(gpu->worker)) {
861 ret = PTR_ERR(gpu->worker);
866 sched_set_fifo_low(gpu->worker->task);
868 mutex_init(&gpu->active_lock);
869 mutex_init(&gpu->lock);
870 init_waitqueue_head(&gpu->retire_event);
871 kthread_init_work(&gpu->retire_work, retire_worker);
872 kthread_init_work(&gpu->recover_work, recover_worker);
873 kthread_init_work(&gpu->fault_work, fault_worker);
875 priv->hangcheck_period = DRM_MSM_HANGCHECK_DEFAULT_PERIOD;
878 * If progress detection is supported, halve the hangcheck timer
879 * duration, as it takes two iterations of the hangcheck handler
883 priv->hangcheck_period /= 2;
885 timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0);
887 spin_lock_init(&gpu->perf_lock);
891 gpu->mmio = msm_ioremap(pdev, config->ioname);
892 if (IS_ERR(gpu->mmio)) {
893 ret = PTR_ERR(gpu->mmio);
898 gpu->irq = platform_get_irq(pdev, 0);
904 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
905 IRQF_TRIGGER_HIGH, "gpu-irq", gpu);
907 DRM_DEV_ERROR(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
911 ret = get_clocks(pdev, gpu);
915 gpu->ebi1_clk = msm_clk_get(pdev, "bus");
916 DBG("ebi1_clk: %p", gpu->ebi1_clk);
917 if (IS_ERR(gpu->ebi1_clk))
918 gpu->ebi1_clk = NULL;
920 /* Acquire regulators: */
921 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
922 DBG("gpu_reg: %p", gpu->gpu_reg);
923 if (IS_ERR(gpu->gpu_reg))
926 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
927 DBG("gpu_cx: %p", gpu->gpu_cx);
928 if (IS_ERR(gpu->gpu_cx))
932 platform_set_drvdata(pdev, &gpu->adreno_smmu);
934 msm_devfreq_init(gpu);
937 gpu->aspace = gpu->funcs->create_address_space(gpu, pdev);
939 if (gpu->aspace == NULL)
940 DRM_DEV_INFO(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
941 else if (IS_ERR(gpu->aspace)) {
942 ret = PTR_ERR(gpu->aspace);
946 memptrs = msm_gem_kernel_new(drm,
947 sizeof(struct msm_rbmemptrs) * nr_rings,
948 check_apriv(gpu, MSM_BO_WC), gpu->aspace, &gpu->memptrs_bo,
951 if (IS_ERR(memptrs)) {
952 ret = PTR_ERR(memptrs);
953 DRM_DEV_ERROR(drm->dev, "could not allocate memptrs: %d\n", ret);
957 msm_gem_object_set_name(gpu->memptrs_bo, "memptrs");
959 if (nr_rings > ARRAY_SIZE(gpu->rb)) {
960 DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n",
961 ARRAY_SIZE(gpu->rb));
962 nr_rings = ARRAY_SIZE(gpu->rb);
965 /* Create ringbuffer(s): */
966 for (i = 0; i < nr_rings; i++) {
967 gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova);
969 if (IS_ERR(gpu->rb[i])) {
970 ret = PTR_ERR(gpu->rb[i]);
971 DRM_DEV_ERROR(drm->dev,
972 "could not create ringbuffer %d: %d\n", i, ret);
976 memptrs += sizeof(struct msm_rbmemptrs);
977 memptrs_iova += sizeof(struct msm_rbmemptrs);
980 gpu->nr_rings = nr_rings;
982 refcount_set(&gpu->sysprof_active, 1);
987 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
988 msm_ringbuffer_destroy(gpu->rb[i]);
992 msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace);
994 platform_set_drvdata(pdev, NULL);
998 void msm_gpu_cleanup(struct msm_gpu *gpu)
1002 DBG("%s", gpu->name);
1004 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
1005 msm_ringbuffer_destroy(gpu->rb[i]);
1009 msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace);
1011 if (!IS_ERR_OR_NULL(gpu->aspace)) {
1012 gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu);
1013 msm_gem_address_space_put(gpu->aspace);
1017 kthread_destroy_worker(gpu->worker);
1020 msm_devfreq_cleanup(gpu);
1022 platform_set_drvdata(gpu->pdev, NULL);