1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
10 #include "msm_fence.h"
11 #include "msm_gpu_trace.h"
12 #include "adreno/adreno_gpu.h"
14 #include <generated/utsrelease.h>
15 #include <linux/string_helpers.h>
16 #include <linux/pm_opp.h>
17 #include <linux/devfreq.h>
18 #include <linux/devcoredump.h>
24 static int msm_devfreq_target(struct device *dev, unsigned long *freq,
27 struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev));
28 struct dev_pm_opp *opp;
30 opp = devfreq_recommended_opp(dev, freq, flags);
35 if (gpu->funcs->gpu_set_freq)
36 gpu->funcs->gpu_set_freq(gpu, (u64)*freq);
38 clk_set_rate(gpu->core_clk, *freq);
45 static int msm_devfreq_get_dev_status(struct device *dev,
46 struct devfreq_dev_status *status)
48 struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev));
51 if (gpu->funcs->gpu_get_freq)
52 status->current_frequency = gpu->funcs->gpu_get_freq(gpu);
54 status->current_frequency = clk_get_rate(gpu->core_clk);
56 status->busy_time = gpu->funcs->gpu_busy(gpu);
59 status->total_time = ktime_us_delta(time, gpu->devfreq.time);
60 gpu->devfreq.time = time;
65 static int msm_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
67 struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev));
69 if (gpu->funcs->gpu_get_freq)
70 *freq = gpu->funcs->gpu_get_freq(gpu);
72 *freq = clk_get_rate(gpu->core_clk);
77 static struct devfreq_dev_profile msm_devfreq_profile = {
79 .target = msm_devfreq_target,
80 .get_dev_status = msm_devfreq_get_dev_status,
81 .get_cur_freq = msm_devfreq_get_cur_freq,
84 static void msm_devfreq_init(struct msm_gpu *gpu)
86 /* We need target support to do devfreq */
87 if (!gpu->funcs->gpu_busy)
90 msm_devfreq_profile.initial_freq = gpu->fast_rate;
93 * Don't set the freq_table or max_state and let devfreq build the table
97 gpu->devfreq.devfreq = devm_devfreq_add_device(&gpu->pdev->dev,
98 &msm_devfreq_profile, "simple_ondemand", NULL);
100 if (IS_ERR(gpu->devfreq.devfreq)) {
101 DRM_DEV_ERROR(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n");
102 gpu->devfreq.devfreq = NULL;
105 devfreq_suspend_device(gpu->devfreq.devfreq);
108 static int enable_pwrrail(struct msm_gpu *gpu)
110 struct drm_device *dev = gpu->dev;
114 ret = regulator_enable(gpu->gpu_reg);
116 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
122 ret = regulator_enable(gpu->gpu_cx);
124 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
132 static int disable_pwrrail(struct msm_gpu *gpu)
135 regulator_disable(gpu->gpu_cx);
137 regulator_disable(gpu->gpu_reg);
141 static int enable_clk(struct msm_gpu *gpu)
143 if (gpu->core_clk && gpu->fast_rate)
144 clk_set_rate(gpu->core_clk, gpu->fast_rate);
146 /* Set the RBBM timer rate to 19.2Mhz */
147 if (gpu->rbbmtimer_clk)
148 clk_set_rate(gpu->rbbmtimer_clk, 19200000);
150 return clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
153 static int disable_clk(struct msm_gpu *gpu)
155 clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
158 * Set the clock to a deliberately low rate. On older targets the clock
159 * speed had to be non zero to avoid problems. On newer targets this
160 * will be rounded down to zero anyway so it all works out.
163 clk_set_rate(gpu->core_clk, 27000000);
165 if (gpu->rbbmtimer_clk)
166 clk_set_rate(gpu->rbbmtimer_clk, 0);
171 static int enable_axi(struct msm_gpu *gpu)
174 clk_prepare_enable(gpu->ebi1_clk);
178 static int disable_axi(struct msm_gpu *gpu)
181 clk_disable_unprepare(gpu->ebi1_clk);
185 void msm_gpu_resume_devfreq(struct msm_gpu *gpu)
187 gpu->devfreq.busy_cycles = 0;
188 gpu->devfreq.time = ktime_get();
190 devfreq_resume_device(gpu->devfreq.devfreq);
193 int msm_gpu_pm_resume(struct msm_gpu *gpu)
197 DBG("%s", gpu->name);
199 ret = enable_pwrrail(gpu);
203 ret = enable_clk(gpu);
207 ret = enable_axi(gpu);
211 msm_gpu_resume_devfreq(gpu);
213 gpu->needs_hw_init = true;
218 int msm_gpu_pm_suspend(struct msm_gpu *gpu)
222 DBG("%s", gpu->name);
224 devfreq_suspend_device(gpu->devfreq.devfreq);
226 ret = disable_axi(gpu);
230 ret = disable_clk(gpu);
234 ret = disable_pwrrail(gpu);
241 int msm_gpu_hw_init(struct msm_gpu *gpu)
245 WARN_ON(!mutex_is_locked(&gpu->dev->struct_mutex));
247 if (!gpu->needs_hw_init)
250 disable_irq(gpu->irq);
251 ret = gpu->funcs->hw_init(gpu);
253 gpu->needs_hw_init = false;
254 enable_irq(gpu->irq);
259 #ifdef CONFIG_DEV_COREDUMP
260 static ssize_t msm_gpu_devcoredump_read(char *buffer, loff_t offset,
261 size_t count, void *data, size_t datalen)
263 struct msm_gpu *gpu = data;
264 struct drm_print_iterator iter;
265 struct drm_printer p;
266 struct msm_gpu_state *state;
268 state = msm_gpu_crashstate_get(gpu);
277 p = drm_coredump_printer(&iter);
279 drm_printf(&p, "---\n");
280 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
281 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
282 drm_printf(&p, "time: %lld.%09ld\n",
283 state->time.tv_sec, state->time.tv_nsec);
285 drm_printf(&p, "comm: %s\n", state->comm);
287 drm_printf(&p, "cmdline: %s\n", state->cmd);
289 gpu->funcs->show(gpu, state, &p);
291 msm_gpu_crashstate_put(gpu);
293 return count - iter.remain;
296 static void msm_gpu_devcoredump_free(void *data)
298 struct msm_gpu *gpu = data;
300 msm_gpu_crashstate_put(gpu);
303 static void msm_gpu_crashstate_get_bo(struct msm_gpu_state *state,
304 struct msm_gem_object *obj, u64 iova, u32 flags)
306 struct msm_gpu_state_bo *state_bo = &state->bos[state->nr_bos];
308 /* Don't record write only objects */
309 state_bo->size = obj->base.size;
310 state_bo->iova = iova;
312 /* Only store data for non imported buffer objects marked for read */
313 if ((flags & MSM_SUBMIT_BO_READ) && !obj->base.import_attach) {
316 state_bo->data = kvmalloc(obj->base.size, GFP_KERNEL);
320 ptr = msm_gem_get_vaddr_active(&obj->base);
322 kvfree(state_bo->data);
323 state_bo->data = NULL;
327 memcpy(state_bo->data, ptr, obj->base.size);
328 msm_gem_put_vaddr(&obj->base);
334 static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
335 struct msm_gem_submit *submit, char *comm, char *cmd)
337 struct msm_gpu_state *state;
339 /* Check if the target supports capturing crash state */
340 if (!gpu->funcs->gpu_state_get)
343 /* Only save one crash state at a time */
347 state = gpu->funcs->gpu_state_get(gpu);
348 if (IS_ERR_OR_NULL(state))
351 /* Fill in the additional crash state information */
352 state->comm = kstrdup(comm, GFP_KERNEL);
353 state->cmd = kstrdup(cmd, GFP_KERNEL);
358 state->bos = kcalloc(submit->nr_cmds,
359 sizeof(struct msm_gpu_state_bo), GFP_KERNEL);
361 for (i = 0; state->bos && i < submit->nr_cmds; i++) {
362 int idx = submit->cmd[i].idx;
364 msm_gpu_crashstate_get_bo(state, submit->bos[idx].obj,
365 submit->bos[idx].iova, submit->bos[idx].flags);
369 /* Set the active crash state to be dumped on failure */
370 gpu->crashstate = state;
372 /* FIXME: Release the crashstate if this errors out? */
373 dev_coredumpm(gpu->dev->dev, THIS_MODULE, gpu, 0, GFP_KERNEL,
374 msm_gpu_devcoredump_read, msm_gpu_devcoredump_free);
377 static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
378 struct msm_gem_submit *submit, char *comm, char *cmd)
384 * Hangcheck detection for locked gpu:
387 static void update_fences(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
390 struct msm_gem_submit *submit;
392 list_for_each_entry(submit, &ring->submits, node) {
393 if (submit->seqno > fence)
396 msm_update_fence(submit->ring->fctx,
397 submit->fence->seqno);
401 static struct msm_gem_submit *
402 find_submit(struct msm_ringbuffer *ring, uint32_t fence)
404 struct msm_gem_submit *submit;
406 WARN_ON(!mutex_is_locked(&ring->gpu->dev->struct_mutex));
408 list_for_each_entry(submit, &ring->submits, node)
409 if (submit->seqno == fence)
415 static void retire_submits(struct msm_gpu *gpu);
417 static void recover_worker(struct work_struct *work)
419 struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
420 struct drm_device *dev = gpu->dev;
421 struct msm_drm_private *priv = dev->dev_private;
422 struct msm_gem_submit *submit;
423 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
424 char *comm = NULL, *cmd = NULL;
427 mutex_lock(&dev->struct_mutex);
429 DRM_DEV_ERROR(dev->dev, "%s: hangcheck recover!\n", gpu->name);
431 submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1);
433 struct task_struct *task;
435 /* Increment the fault counts */
436 gpu->global_faults++;
437 submit->queue->faults++;
439 task = get_pid_task(submit->pid, PIDTYPE_PID);
441 comm = kstrdup(task->comm, GFP_KERNEL);
442 cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL);
443 put_task_struct(task);
447 DRM_DEV_ERROR(dev->dev, "%s: offending task: %s (%s)\n",
448 gpu->name, comm, cmd);
450 msm_rd_dump_submit(priv->hangrd, submit,
451 "offending task: %s (%s)", comm, cmd);
453 msm_rd_dump_submit(priv->hangrd, submit, NULL);
456 /* Record the crash state */
457 pm_runtime_get_sync(&gpu->pdev->dev);
458 msm_gpu_crashstate_capture(gpu, submit, comm, cmd);
459 pm_runtime_put_sync(&gpu->pdev->dev);
465 * Update all the rings with the latest and greatest fence.. this
466 * needs to happen after msm_rd_dump_submit() to ensure that the
467 * bo's referenced by the offending submit are still around.
469 for (i = 0; i < gpu->nr_rings; i++) {
470 struct msm_ringbuffer *ring = gpu->rb[i];
472 uint32_t fence = ring->memptrs->fence;
475 * For the current (faulting?) ring/submit advance the fence by
476 * one more to clear the faulting submit
478 if (ring == cur_ring)
481 update_fences(gpu, ring, fence);
484 if (msm_gpu_active(gpu)) {
485 /* retire completed submits, plus the one that hung: */
488 pm_runtime_get_sync(&gpu->pdev->dev);
489 gpu->funcs->recover(gpu);
490 pm_runtime_put_sync(&gpu->pdev->dev);
493 * Replay all remaining submits starting with highest priority
496 for (i = 0; i < gpu->nr_rings; i++) {
497 struct msm_ringbuffer *ring = gpu->rb[i];
499 list_for_each_entry(submit, &ring->submits, node)
500 gpu->funcs->submit(gpu, submit, NULL);
504 mutex_unlock(&dev->struct_mutex);
509 static void hangcheck_timer_reset(struct msm_gpu *gpu)
511 DBG("%s", gpu->name);
512 mod_timer(&gpu->hangcheck_timer,
513 round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
516 static void hangcheck_handler(struct timer_list *t)
518 struct msm_gpu *gpu = from_timer(gpu, t, hangcheck_timer);
519 struct drm_device *dev = gpu->dev;
520 struct msm_drm_private *priv = dev->dev_private;
521 struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
522 uint32_t fence = ring->memptrs->fence;
524 if (fence != ring->hangcheck_fence) {
525 /* some progress has been made.. ya! */
526 ring->hangcheck_fence = fence;
527 } else if (fence < ring->seqno) {
528 /* no progress and not done.. hung! */
529 ring->hangcheck_fence = fence;
530 DRM_DEV_ERROR(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n",
531 gpu->name, ring->id);
532 DRM_DEV_ERROR(dev->dev, "%s: completed fence: %u\n",
534 DRM_DEV_ERROR(dev->dev, "%s: submitted fence: %u\n",
535 gpu->name, ring->seqno);
537 queue_work(priv->wq, &gpu->recover_work);
540 /* if still more pending work, reset the hangcheck timer: */
541 if (ring->seqno > ring->hangcheck_fence)
542 hangcheck_timer_reset(gpu);
544 /* workaround for missing irq: */
545 queue_work(priv->wq, &gpu->retire_work);
549 * Performance Counters:
552 /* called under perf_lock */
553 static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
555 uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
556 int i, n = min(ncntrs, gpu->num_perfcntrs);
558 /* read current values: */
559 for (i = 0; i < gpu->num_perfcntrs; i++)
560 current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
563 for (i = 0; i < n; i++)
564 cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
566 /* save current values: */
567 for (i = 0; i < gpu->num_perfcntrs; i++)
568 gpu->last_cntrs[i] = current_cntrs[i];
573 static void update_sw_cntrs(struct msm_gpu *gpu)
579 spin_lock_irqsave(&gpu->perf_lock, flags);
580 if (!gpu->perfcntr_active)
584 elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
586 gpu->totaltime += elapsed;
587 if (gpu->last_sample.active)
588 gpu->activetime += elapsed;
590 gpu->last_sample.active = msm_gpu_active(gpu);
591 gpu->last_sample.time = time;
594 spin_unlock_irqrestore(&gpu->perf_lock, flags);
597 void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
601 pm_runtime_get_sync(&gpu->pdev->dev);
603 spin_lock_irqsave(&gpu->perf_lock, flags);
604 /* we could dynamically enable/disable perfcntr registers too.. */
605 gpu->last_sample.active = msm_gpu_active(gpu);
606 gpu->last_sample.time = ktime_get();
607 gpu->activetime = gpu->totaltime = 0;
608 gpu->perfcntr_active = true;
609 update_hw_cntrs(gpu, 0, NULL);
610 spin_unlock_irqrestore(&gpu->perf_lock, flags);
613 void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
615 gpu->perfcntr_active = false;
616 pm_runtime_put_sync(&gpu->pdev->dev);
619 /* returns -errno or # of cntrs sampled */
620 int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
621 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
626 spin_lock_irqsave(&gpu->perf_lock, flags);
628 if (!gpu->perfcntr_active) {
633 *activetime = gpu->activetime;
634 *totaltime = gpu->totaltime;
636 gpu->activetime = gpu->totaltime = 0;
638 ret = update_hw_cntrs(gpu, ncntrs, cntrs);
641 spin_unlock_irqrestore(&gpu->perf_lock, flags);
647 * Cmdstream submission/retirement:
650 static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
651 struct msm_gem_submit *submit)
653 int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT;
654 volatile struct msm_gpu_submit_stats *stats;
655 u64 elapsed, clock = 0;
658 stats = &ring->memptrs->stats[index];
659 /* Convert 19.2Mhz alwayson ticks to nanoseconds for elapsed time */
660 elapsed = (stats->alwayson_end - stats->alwayson_start) * 10000;
661 do_div(elapsed, 192);
663 /* Calculate the clock frequency from the number of CP cycles */
665 clock = (stats->cpcycles_end - stats->cpcycles_start) * 1000;
666 do_div(clock, elapsed);
669 trace_msm_gpu_submit_retired(submit, elapsed, clock,
670 stats->alwayson_start, stats->alwayson_end);
672 for (i = 0; i < submit->nr_bos; i++) {
673 struct msm_gem_object *msm_obj = submit->bos[i].obj;
674 /* move to inactive: */
675 msm_gem_move_to_inactive(&msm_obj->base);
676 msm_gem_unpin_iova(&msm_obj->base, submit->aspace);
677 drm_gem_object_put(&msm_obj->base);
680 pm_runtime_mark_last_busy(&gpu->pdev->dev);
681 pm_runtime_put_autosuspend(&gpu->pdev->dev);
682 msm_gem_submit_free(submit);
685 static void retire_submits(struct msm_gpu *gpu)
687 struct drm_device *dev = gpu->dev;
688 struct msm_gem_submit *submit, *tmp;
691 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
693 /* Retire the commits starting with highest priority */
694 for (i = 0; i < gpu->nr_rings; i++) {
695 struct msm_ringbuffer *ring = gpu->rb[i];
697 list_for_each_entry_safe(submit, tmp, &ring->submits, node) {
698 if (dma_fence_is_signaled(submit->fence))
699 retire_submit(gpu, ring, submit);
704 static void retire_worker(struct work_struct *work)
706 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
707 struct drm_device *dev = gpu->dev;
710 for (i = 0; i < gpu->nr_rings; i++)
711 update_fences(gpu, gpu->rb[i], gpu->rb[i]->memptrs->fence);
713 mutex_lock(&dev->struct_mutex);
715 mutex_unlock(&dev->struct_mutex);
718 /* call from irq handler to schedule work to retire bo's */
719 void msm_gpu_retire(struct msm_gpu *gpu)
721 struct msm_drm_private *priv = gpu->dev->dev_private;
722 queue_work(priv->wq, &gpu->retire_work);
723 update_sw_cntrs(gpu);
726 /* add bo's to gpu's ring, and kick gpu: */
727 void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
728 struct msm_file_private *ctx)
730 struct drm_device *dev = gpu->dev;
731 struct msm_drm_private *priv = dev->dev_private;
732 struct msm_ringbuffer *ring = submit->ring;
735 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
737 pm_runtime_get_sync(&gpu->pdev->dev);
739 msm_gpu_hw_init(gpu);
741 submit->seqno = ++ring->seqno;
743 list_add_tail(&submit->node, &ring->submits);
745 msm_rd_dump_submit(priv->rd, submit, NULL);
747 update_sw_cntrs(gpu);
749 for (i = 0; i < submit->nr_bos; i++) {
750 struct msm_gem_object *msm_obj = submit->bos[i].obj;
753 /* can't happen yet.. but when we add 2d support we'll have
754 * to deal w/ cross-ring synchronization:
756 WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
758 /* submit takes a reference to the bo and iova until retired: */
759 drm_gem_object_get(&msm_obj->base);
760 msm_gem_get_and_pin_iova(&msm_obj->base, submit->aspace, &iova);
762 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
763 msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
764 else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
765 msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
768 gpu->funcs->submit(gpu, submit, ctx);
771 hangcheck_timer_reset(gpu);
778 static irqreturn_t irq_handler(int irq, void *data)
780 struct msm_gpu *gpu = data;
781 return gpu->funcs->irq(gpu);
784 static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu)
786 int ret = msm_clk_bulk_get(&pdev->dev, &gpu->grp_clks);
793 gpu->nr_clocks = ret;
795 gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
796 gpu->nr_clocks, "core");
798 gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
799 gpu->nr_clocks, "rbbmtimer");
804 static struct msm_gem_address_space *
805 msm_gpu_create_address_space(struct msm_gpu *gpu, struct platform_device *pdev,
806 uint64_t va_start, uint64_t va_end)
808 struct msm_gem_address_space *aspace;
812 * Setup IOMMU.. eventually we will (I think) do this once per context
813 * and have separate page tables per context. For now, to keep things
814 * simple and to get something working, just use a single address space:
816 if (!adreno_is_a2xx(to_adreno_gpu(gpu))) {
817 struct iommu_domain *iommu = iommu_domain_alloc(&platform_bus_type);
821 iommu->geometry.aperture_start = va_start;
822 iommu->geometry.aperture_end = va_end;
824 DRM_DEV_INFO(gpu->dev->dev, "%s: using IOMMU\n", gpu->name);
826 aspace = msm_gem_address_space_create(&pdev->dev, iommu, "gpu");
828 iommu_domain_free(iommu);
830 aspace = msm_gem_address_space_create_a2xx(&pdev->dev, gpu, "gpu",
834 if (IS_ERR(aspace)) {
835 DRM_DEV_ERROR(gpu->dev->dev, "failed to init mmu: %ld\n",
837 return ERR_CAST(aspace);
840 ret = aspace->mmu->funcs->attach(aspace->mmu, NULL, 0);
842 msm_gem_address_space_put(aspace);
849 int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
850 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
851 const char *name, struct msm_gpu_config *config)
853 int i, ret, nr_rings = config->nr_rings;
855 uint64_t memptrs_iova;
857 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
858 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
864 INIT_LIST_HEAD(&gpu->active_list);
865 INIT_WORK(&gpu->retire_work, retire_worker);
866 INIT_WORK(&gpu->recover_work, recover_worker);
869 timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0);
871 spin_lock_init(&gpu->perf_lock);
875 gpu->mmio = msm_ioremap(pdev, config->ioname, name);
876 if (IS_ERR(gpu->mmio)) {
877 ret = PTR_ERR(gpu->mmio);
882 gpu->irq = platform_get_irq(pdev, 0);
885 DRM_DEV_ERROR(drm->dev, "failed to get irq: %d\n", ret);
889 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
890 IRQF_TRIGGER_HIGH, gpu->name, gpu);
892 DRM_DEV_ERROR(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
896 ret = get_clocks(pdev, gpu);
900 gpu->ebi1_clk = msm_clk_get(pdev, "bus");
901 DBG("ebi1_clk: %p", gpu->ebi1_clk);
902 if (IS_ERR(gpu->ebi1_clk))
903 gpu->ebi1_clk = NULL;
905 /* Acquire regulators: */
906 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
907 DBG("gpu_reg: %p", gpu->gpu_reg);
908 if (IS_ERR(gpu->gpu_reg))
911 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
912 DBG("gpu_cx: %p", gpu->gpu_cx);
913 if (IS_ERR(gpu->gpu_cx))
917 platform_set_drvdata(pdev, gpu);
919 msm_devfreq_init(gpu);
921 gpu->aspace = msm_gpu_create_address_space(gpu, pdev,
922 config->va_start, config->va_end);
924 if (gpu->aspace == NULL)
925 DRM_DEV_INFO(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
926 else if (IS_ERR(gpu->aspace)) {
927 ret = PTR_ERR(gpu->aspace);
931 memptrs = msm_gem_kernel_new(drm,
932 sizeof(struct msm_rbmemptrs) * nr_rings,
933 MSM_BO_UNCACHED, gpu->aspace, &gpu->memptrs_bo,
936 if (IS_ERR(memptrs)) {
937 ret = PTR_ERR(memptrs);
938 DRM_DEV_ERROR(drm->dev, "could not allocate memptrs: %d\n", ret);
942 msm_gem_object_set_name(gpu->memptrs_bo, "memptrs");
944 if (nr_rings > ARRAY_SIZE(gpu->rb)) {
945 DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n",
946 ARRAY_SIZE(gpu->rb));
947 nr_rings = ARRAY_SIZE(gpu->rb);
950 /* Create ringbuffer(s): */
951 for (i = 0; i < nr_rings; i++) {
952 gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova);
954 if (IS_ERR(gpu->rb[i])) {
955 ret = PTR_ERR(gpu->rb[i]);
956 DRM_DEV_ERROR(drm->dev,
957 "could not create ringbuffer %d: %d\n", i, ret);
961 memptrs += sizeof(struct msm_rbmemptrs);
962 memptrs_iova += sizeof(struct msm_rbmemptrs);
965 gpu->nr_rings = nr_rings;
970 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
971 msm_ringbuffer_destroy(gpu->rb[i]);
975 msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace, false);
977 platform_set_drvdata(pdev, NULL);
981 void msm_gpu_cleanup(struct msm_gpu *gpu)
985 DBG("%s", gpu->name);
987 WARN_ON(!list_empty(&gpu->active_list));
989 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
990 msm_ringbuffer_destroy(gpu->rb[i]);
994 msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace, false);
996 if (!IS_ERR_OR_NULL(gpu->aspace)) {
997 gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu,
999 msm_gem_address_space_put(gpu->aspace);