2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "msm_fence.h"
28 #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
29 #include <mach/board.h>
30 static void bs_init(struct msm_gpu *gpu)
32 if (gpu->bus_scale_table) {
33 gpu->bsc = msm_bus_scale_register_client(gpu->bus_scale_table);
34 DBG("bus scale client: %08x", gpu->bsc);
38 static void bs_fini(struct msm_gpu *gpu)
41 msm_bus_scale_unregister_client(gpu->bsc);
46 static void bs_set(struct msm_gpu *gpu, int idx)
49 DBG("set bus scaling: %d", idx);
50 msm_bus_scale_client_update_request(gpu->bsc, idx);
54 static void bs_init(struct msm_gpu *gpu) {}
55 static void bs_fini(struct msm_gpu *gpu) {}
56 static void bs_set(struct msm_gpu *gpu, int idx) {}
59 static int enable_pwrrail(struct msm_gpu *gpu)
61 struct drm_device *dev = gpu->dev;
65 ret = regulator_enable(gpu->gpu_reg);
67 dev_err(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
73 ret = regulator_enable(gpu->gpu_cx);
75 dev_err(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
83 static int disable_pwrrail(struct msm_gpu *gpu)
86 regulator_disable(gpu->gpu_cx);
88 regulator_disable(gpu->gpu_reg);
92 static int enable_clk(struct msm_gpu *gpu)
96 if (gpu->core_clk && gpu->fast_rate)
97 clk_set_rate(gpu->core_clk, gpu->fast_rate);
99 /* Set the RBBM timer rate to 19.2Mhz */
100 if (gpu->rbbmtimer_clk)
101 clk_set_rate(gpu->rbbmtimer_clk, 19200000);
103 for (i = gpu->nr_clocks - 1; i >= 0; i--)
104 if (gpu->grp_clks[i])
105 clk_prepare(gpu->grp_clks[i]);
107 for (i = gpu->nr_clocks - 1; i >= 0; i--)
108 if (gpu->grp_clks[i])
109 clk_enable(gpu->grp_clks[i]);
114 static int disable_clk(struct msm_gpu *gpu)
118 for (i = gpu->nr_clocks - 1; i >= 0; i--)
119 if (gpu->grp_clks[i])
120 clk_disable(gpu->grp_clks[i]);
122 for (i = gpu->nr_clocks - 1; i >= 0; i--)
123 if (gpu->grp_clks[i])
124 clk_unprepare(gpu->grp_clks[i]);
127 * Set the clock to a deliberately low rate. On older targets the clock
128 * speed had to be non zero to avoid problems. On newer targets this
129 * will be rounded down to zero anyway so it all works out.
132 clk_set_rate(gpu->core_clk, 27000000);
134 if (gpu->rbbmtimer_clk)
135 clk_set_rate(gpu->rbbmtimer_clk, 0);
140 static int enable_axi(struct msm_gpu *gpu)
143 clk_prepare_enable(gpu->ebi1_clk);
145 bs_set(gpu, gpu->bus_freq);
149 static int disable_axi(struct msm_gpu *gpu)
152 clk_disable_unprepare(gpu->ebi1_clk);
158 int msm_gpu_pm_resume(struct msm_gpu *gpu)
162 DBG("%s", gpu->name);
164 ret = enable_pwrrail(gpu);
168 ret = enable_clk(gpu);
172 ret = enable_axi(gpu);
176 gpu->needs_hw_init = true;
181 int msm_gpu_pm_suspend(struct msm_gpu *gpu)
185 DBG("%s", gpu->name);
187 ret = disable_axi(gpu);
191 ret = disable_clk(gpu);
195 ret = disable_pwrrail(gpu);
202 int msm_gpu_hw_init(struct msm_gpu *gpu)
206 WARN_ON(!mutex_is_locked(&gpu->dev->struct_mutex));
208 if (!gpu->needs_hw_init)
211 disable_irq(gpu->irq);
212 ret = gpu->funcs->hw_init(gpu);
214 gpu->needs_hw_init = false;
215 enable_irq(gpu->irq);
221 * Hangcheck detection for locked gpu:
224 static void update_fences(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
227 struct msm_gem_submit *submit;
229 list_for_each_entry(submit, &ring->submits, node) {
230 if (submit->seqno > fence)
233 msm_update_fence(submit->ring->fctx,
234 submit->fence->seqno);
238 static void retire_submits(struct msm_gpu *gpu);
240 static void recover_worker(struct work_struct *work)
242 struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
243 struct drm_device *dev = gpu->dev;
244 struct msm_gem_submit *submit;
245 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
249 /* Update all the rings with the latest and greatest fence */
250 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
251 struct msm_ringbuffer *ring = gpu->rb[i];
253 fence = ring->memptrs->fence;
256 * For the current (faulting?) ring/submit advance the fence by
257 * one more to clear the faulting submit
259 if (ring == cur_ring)
262 update_fences(gpu, ring, fence);
265 mutex_lock(&dev->struct_mutex);
268 dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
269 fence = cur_ring->memptrs->fence + 1;
271 list_for_each_entry(submit, &cur_ring->submits, node) {
272 if (submit->seqno == fence) {
273 struct task_struct *task;
276 task = pid_task(submit->pid, PIDTYPE_PID);
278 dev_err(dev->dev, "%s: offending task: %s\n",
279 gpu->name, task->comm);
286 if (msm_gpu_active(gpu)) {
287 /* retire completed submits, plus the one that hung: */
290 pm_runtime_get_sync(&gpu->pdev->dev);
291 gpu->funcs->recover(gpu);
292 pm_runtime_put_sync(&gpu->pdev->dev);
295 * Replay all remaining submits starting with highest priority
298 for (i = 0; i < gpu->nr_rings; i++) {
299 struct msm_ringbuffer *ring = gpu->rb[i];
301 list_for_each_entry(submit, &ring->submits, node)
302 gpu->funcs->submit(gpu, submit, NULL);
306 mutex_unlock(&dev->struct_mutex);
311 static void hangcheck_timer_reset(struct msm_gpu *gpu)
313 DBG("%s", gpu->name);
314 mod_timer(&gpu->hangcheck_timer,
315 round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
318 static void hangcheck_handler(unsigned long data)
320 struct msm_gpu *gpu = (struct msm_gpu *)data;
321 struct drm_device *dev = gpu->dev;
322 struct msm_drm_private *priv = dev->dev_private;
323 struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
324 uint32_t fence = ring->memptrs->fence;
326 if (fence != ring->hangcheck_fence) {
327 /* some progress has been made.. ya! */
328 ring->hangcheck_fence = fence;
329 } else if (fence < ring->seqno) {
330 /* no progress and not done.. hung! */
331 ring->hangcheck_fence = fence;
332 dev_err(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n",
333 gpu->name, ring->id);
334 dev_err(dev->dev, "%s: completed fence: %u\n",
336 dev_err(dev->dev, "%s: submitted fence: %u\n",
337 gpu->name, ring->seqno);
339 queue_work(priv->wq, &gpu->recover_work);
342 /* if still more pending work, reset the hangcheck timer: */
343 if (ring->seqno > ring->hangcheck_fence)
344 hangcheck_timer_reset(gpu);
346 /* workaround for missing irq: */
347 queue_work(priv->wq, &gpu->retire_work);
351 * Performance Counters:
354 /* called under perf_lock */
355 static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
357 uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
358 int i, n = min(ncntrs, gpu->num_perfcntrs);
360 /* read current values: */
361 for (i = 0; i < gpu->num_perfcntrs; i++)
362 current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
365 for (i = 0; i < n; i++)
366 cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
368 /* save current values: */
369 for (i = 0; i < gpu->num_perfcntrs; i++)
370 gpu->last_cntrs[i] = current_cntrs[i];
375 static void update_sw_cntrs(struct msm_gpu *gpu)
381 spin_lock_irqsave(&gpu->perf_lock, flags);
382 if (!gpu->perfcntr_active)
386 elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
388 gpu->totaltime += elapsed;
389 if (gpu->last_sample.active)
390 gpu->activetime += elapsed;
392 gpu->last_sample.active = msm_gpu_active(gpu);
393 gpu->last_sample.time = time;
396 spin_unlock_irqrestore(&gpu->perf_lock, flags);
399 void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
403 pm_runtime_get_sync(&gpu->pdev->dev);
405 spin_lock_irqsave(&gpu->perf_lock, flags);
406 /* we could dynamically enable/disable perfcntr registers too.. */
407 gpu->last_sample.active = msm_gpu_active(gpu);
408 gpu->last_sample.time = ktime_get();
409 gpu->activetime = gpu->totaltime = 0;
410 gpu->perfcntr_active = true;
411 update_hw_cntrs(gpu, 0, NULL);
412 spin_unlock_irqrestore(&gpu->perf_lock, flags);
415 void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
417 gpu->perfcntr_active = false;
418 pm_runtime_put_sync(&gpu->pdev->dev);
421 /* returns -errno or # of cntrs sampled */
422 int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
423 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
428 spin_lock_irqsave(&gpu->perf_lock, flags);
430 if (!gpu->perfcntr_active) {
435 *activetime = gpu->activetime;
436 *totaltime = gpu->totaltime;
438 gpu->activetime = gpu->totaltime = 0;
440 ret = update_hw_cntrs(gpu, ncntrs, cntrs);
443 spin_unlock_irqrestore(&gpu->perf_lock, flags);
449 * Cmdstream submission/retirement:
452 static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
456 for (i = 0; i < submit->nr_bos; i++) {
457 struct msm_gem_object *msm_obj = submit->bos[i].obj;
458 /* move to inactive: */
459 msm_gem_move_to_inactive(&msm_obj->base);
460 msm_gem_put_iova(&msm_obj->base, gpu->aspace);
461 drm_gem_object_unreference(&msm_obj->base);
464 pm_runtime_mark_last_busy(&gpu->pdev->dev);
465 pm_runtime_put_autosuspend(&gpu->pdev->dev);
466 msm_gem_submit_free(submit);
469 static void retire_submits(struct msm_gpu *gpu)
471 struct drm_device *dev = gpu->dev;
472 struct msm_gem_submit *submit, *tmp;
475 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
477 /* Retire the commits starting with highest priority */
478 for (i = 0; i < gpu->nr_rings; i++) {
479 struct msm_ringbuffer *ring = gpu->rb[i];
481 list_for_each_entry_safe(submit, tmp, &ring->submits, node) {
482 if (dma_fence_is_signaled(submit->fence))
483 retire_submit(gpu, submit);
488 static void retire_worker(struct work_struct *work)
490 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
491 struct drm_device *dev = gpu->dev;
494 for (i = 0; i < gpu->nr_rings; i++)
495 update_fences(gpu, gpu->rb[i], gpu->rb[i]->memptrs->fence);
497 mutex_lock(&dev->struct_mutex);
499 mutex_unlock(&dev->struct_mutex);
502 /* call from irq handler to schedule work to retire bo's */
503 void msm_gpu_retire(struct msm_gpu *gpu)
505 struct msm_drm_private *priv = gpu->dev->dev_private;
506 queue_work(priv->wq, &gpu->retire_work);
507 update_sw_cntrs(gpu);
510 /* add bo's to gpu's ring, and kick gpu: */
511 void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
512 struct msm_file_private *ctx)
514 struct drm_device *dev = gpu->dev;
515 struct msm_drm_private *priv = dev->dev_private;
516 struct msm_ringbuffer *ring = submit->ring;
519 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
521 pm_runtime_get_sync(&gpu->pdev->dev);
523 msm_gpu_hw_init(gpu);
525 submit->seqno = ++ring->seqno;
527 list_add_tail(&submit->node, &ring->submits);
529 msm_rd_dump_submit(submit);
531 update_sw_cntrs(gpu);
533 for (i = 0; i < submit->nr_bos; i++) {
534 struct msm_gem_object *msm_obj = submit->bos[i].obj;
537 /* can't happen yet.. but when we add 2d support we'll have
538 * to deal w/ cross-ring synchronization:
540 WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
542 /* submit takes a reference to the bo and iova until retired: */
543 drm_gem_object_reference(&msm_obj->base);
544 msm_gem_get_iova(&msm_obj->base,
545 submit->gpu->aspace, &iova);
547 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
548 msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
549 else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
550 msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
553 gpu->funcs->submit(gpu, submit, ctx);
556 hangcheck_timer_reset(gpu);
563 static irqreturn_t irq_handler(int irq, void *data)
565 struct msm_gpu *gpu = data;
566 return gpu->funcs->irq(gpu);
569 static struct clk *get_clock(struct device *dev, const char *name)
571 struct clk *clk = devm_clk_get(dev, name);
573 return IS_ERR(clk) ? NULL : clk;
576 static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu)
578 struct device *dev = &pdev->dev;
579 struct property *prop;
583 gpu->nr_clocks = of_property_count_strings(dev->of_node, "clock-names");
584 if (gpu->nr_clocks < 1) {
589 gpu->grp_clks = devm_kcalloc(dev, sizeof(struct clk *), gpu->nr_clocks,
594 of_property_for_each_string(dev->of_node, "clock-names", prop, name) {
595 gpu->grp_clks[i] = get_clock(dev, name);
597 /* Remember the key clocks that we need to control later */
598 if (!strcmp(name, "core") || !strcmp(name, "core_clk"))
599 gpu->core_clk = gpu->grp_clks[i];
600 else if (!strcmp(name, "rbbmtimer") || !strcmp(name, "rbbmtimer_clk"))
601 gpu->rbbmtimer_clk = gpu->grp_clks[i];
609 static struct msm_gem_address_space *
610 msm_gpu_create_address_space(struct msm_gpu *gpu, struct platform_device *pdev,
611 uint64_t va_start, uint64_t va_end)
613 struct iommu_domain *iommu;
614 struct msm_gem_address_space *aspace;
618 * Setup IOMMU.. eventually we will (I think) do this once per context
619 * and have separate page tables per context. For now, to keep things
620 * simple and to get something working, just use a single address space:
622 iommu = iommu_domain_alloc(&platform_bus_type);
626 iommu->geometry.aperture_start = va_start;
627 iommu->geometry.aperture_end = va_end;
629 dev_info(gpu->dev->dev, "%s: using IOMMU\n", gpu->name);
631 aspace = msm_gem_address_space_create(&pdev->dev, iommu, "gpu");
632 if (IS_ERR(aspace)) {
633 dev_err(gpu->dev->dev, "failed to init iommu: %ld\n",
635 iommu_domain_free(iommu);
636 return ERR_CAST(aspace);
639 ret = aspace->mmu->funcs->attach(aspace->mmu, NULL, 0);
641 msm_gem_address_space_put(aspace);
648 int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
649 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
650 const char *name, struct msm_gpu_config *config)
652 int i, ret, nr_rings = config->nr_rings;
654 uint64_t memptrs_iova;
656 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
657 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
663 INIT_LIST_HEAD(&gpu->active_list);
664 INIT_WORK(&gpu->retire_work, retire_worker);
665 INIT_WORK(&gpu->recover_work, recover_worker);
668 setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
671 spin_lock_init(&gpu->perf_lock);
675 gpu->mmio = msm_ioremap(pdev, config->ioname, name);
676 if (IS_ERR(gpu->mmio)) {
677 ret = PTR_ERR(gpu->mmio);
682 gpu->irq = platform_get_irq_byname(pdev, config->irqname);
685 dev_err(drm->dev, "failed to get irq: %d\n", ret);
689 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
690 IRQF_TRIGGER_HIGH, gpu->name, gpu);
692 dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
696 ret = get_clocks(pdev, gpu);
700 gpu->ebi1_clk = msm_clk_get(pdev, "bus");
701 DBG("ebi1_clk: %p", gpu->ebi1_clk);
702 if (IS_ERR(gpu->ebi1_clk))
703 gpu->ebi1_clk = NULL;
705 /* Acquire regulators: */
706 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
707 DBG("gpu_reg: %p", gpu->gpu_reg);
708 if (IS_ERR(gpu->gpu_reg))
711 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
712 DBG("gpu_cx: %p", gpu->gpu_cx);
713 if (IS_ERR(gpu->gpu_cx))
717 platform_set_drvdata(pdev, gpu);
721 gpu->aspace = msm_gpu_create_address_space(gpu, pdev,
722 config->va_start, config->va_end);
724 if (gpu->aspace == NULL)
725 dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
726 else if (IS_ERR(gpu->aspace)) {
727 ret = PTR_ERR(gpu->aspace);
731 memptrs = msm_gem_kernel_new(drm, sizeof(*gpu->memptrs_bo),
732 MSM_BO_UNCACHED, gpu->aspace, &gpu->memptrs_bo,
735 if (IS_ERR(memptrs)) {
736 ret = PTR_ERR(memptrs);
737 dev_err(drm->dev, "could not allocate memptrs: %d\n", ret);
741 if (nr_rings > ARRAY_SIZE(gpu->rb)) {
742 DRM_DEV_INFO_ONCE(drm->dev, "Only creating %lu ringbuffers\n",
743 ARRAY_SIZE(gpu->rb));
744 nr_rings = ARRAY_SIZE(gpu->rb);
747 /* Create ringbuffer(s): */
748 for (i = 0; i < nr_rings; i++) {
749 gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova);
751 if (IS_ERR(gpu->rb[i])) {
752 ret = PTR_ERR(gpu->rb[i]);
754 "could not create ringbuffer %d: %d\n", i, ret);
758 memptrs += sizeof(struct msm_rbmemptrs);
759 memptrs_iova += sizeof(struct msm_rbmemptrs);
762 gpu->nr_rings = nr_rings;
767 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
768 msm_ringbuffer_destroy(gpu->rb[i]);
772 if (gpu->memptrs_bo) {
773 msm_gem_put_vaddr(gpu->memptrs_bo);
774 msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace);
775 drm_gem_object_unreference_unlocked(gpu->memptrs_bo);
778 platform_set_drvdata(pdev, NULL);
782 void msm_gpu_cleanup(struct msm_gpu *gpu)
786 DBG("%s", gpu->name);
788 WARN_ON(!list_empty(&gpu->active_list));
792 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
793 msm_ringbuffer_destroy(gpu->rb[i]);
797 if (gpu->memptrs_bo) {
798 msm_gem_put_vaddr(gpu->memptrs_bo);
799 msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace);
800 drm_gem_object_unreference_unlocked(gpu->memptrs_bo);
803 if (!IS_ERR_OR_NULL(gpu->aspace)) {
804 gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu,
806 msm_gem_address_space_put(gpu->aspace);