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)
94 struct clk *rate_clk = NULL;
97 /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */
98 for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) {
99 if (gpu->grp_clks[i]) {
100 clk_prepare(gpu->grp_clks[i]);
101 rate_clk = gpu->grp_clks[i];
105 if (rate_clk && gpu->fast_rate)
106 clk_set_rate(rate_clk, gpu->fast_rate);
108 for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--)
109 if (gpu->grp_clks[i])
110 clk_enable(gpu->grp_clks[i]);
115 static int disable_clk(struct msm_gpu *gpu)
117 struct clk *rate_clk = NULL;
120 /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */
121 for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) {
122 if (gpu->grp_clks[i]) {
123 clk_disable(gpu->grp_clks[i]);
124 rate_clk = gpu->grp_clks[i];
128 if (rate_clk && gpu->slow_rate)
129 clk_set_rate(rate_clk, gpu->slow_rate);
131 for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--)
132 if (gpu->grp_clks[i])
133 clk_unprepare(gpu->grp_clks[i]);
138 static int enable_axi(struct msm_gpu *gpu)
141 clk_prepare_enable(gpu->ebi1_clk);
143 bs_set(gpu, gpu->bus_freq);
147 static int disable_axi(struct msm_gpu *gpu)
150 clk_disable_unprepare(gpu->ebi1_clk);
156 int msm_gpu_pm_resume(struct msm_gpu *gpu)
158 struct drm_device *dev = gpu->dev;
161 DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
163 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
165 if (gpu->active_cnt++ > 0)
168 if (WARN_ON(gpu->active_cnt <= 0))
171 ret = enable_pwrrail(gpu);
175 ret = enable_clk(gpu);
179 ret = enable_axi(gpu);
186 int msm_gpu_pm_suspend(struct msm_gpu *gpu)
188 struct drm_device *dev = gpu->dev;
191 DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
193 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
195 if (--gpu->active_cnt > 0)
198 if (WARN_ON(gpu->active_cnt < 0))
201 ret = disable_axi(gpu);
205 ret = disable_clk(gpu);
209 ret = disable_pwrrail(gpu);
217 * Inactivity detection (for suspend):
220 static void inactive_worker(struct work_struct *work)
222 struct msm_gpu *gpu = container_of(work, struct msm_gpu, inactive_work);
223 struct drm_device *dev = gpu->dev;
228 DBG("%s: inactive!\n", gpu->name);
229 mutex_lock(&dev->struct_mutex);
230 if (!(msm_gpu_active(gpu) || gpu->inactive)) {
233 gpu->inactive = true;
235 mutex_unlock(&dev->struct_mutex);
238 static void inactive_handler(unsigned long data)
240 struct msm_gpu *gpu = (struct msm_gpu *)data;
241 struct msm_drm_private *priv = gpu->dev->dev_private;
243 queue_work(priv->wq, &gpu->inactive_work);
246 /* cancel inactive timer and make sure we are awake: */
247 static void inactive_cancel(struct msm_gpu *gpu)
249 DBG("%s", gpu->name);
250 del_timer(&gpu->inactive_timer);
254 gpu->inactive = false;
258 static void inactive_start(struct msm_gpu *gpu)
260 DBG("%s", gpu->name);
261 mod_timer(&gpu->inactive_timer,
262 round_jiffies_up(jiffies + DRM_MSM_INACTIVE_JIFFIES));
266 * Hangcheck detection for locked gpu:
269 static void retire_submits(struct msm_gpu *gpu, uint32_t fence);
271 static void recover_worker(struct work_struct *work)
273 struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
274 struct drm_device *dev = gpu->dev;
276 dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
278 mutex_lock(&dev->struct_mutex);
279 if (msm_gpu_active(gpu)) {
280 struct msm_gem_submit *submit;
281 uint32_t fence = gpu->funcs->last_fence(gpu);
283 /* retire completed submits, plus the one that hung: */
284 retire_submits(gpu, fence + 1);
286 inactive_cancel(gpu);
287 gpu->funcs->recover(gpu);
289 /* replay the remaining submits after the one that hung: */
290 list_for_each_entry(submit, &gpu->submit_list, node) {
291 gpu->funcs->submit(gpu, submit, NULL);
294 mutex_unlock(&dev->struct_mutex);
299 static void hangcheck_timer_reset(struct msm_gpu *gpu)
301 DBG("%s", gpu->name);
302 mod_timer(&gpu->hangcheck_timer,
303 round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
306 static void hangcheck_handler(unsigned long data)
308 struct msm_gpu *gpu = (struct msm_gpu *)data;
309 struct drm_device *dev = gpu->dev;
310 struct msm_drm_private *priv = dev->dev_private;
311 uint32_t fence = gpu->funcs->last_fence(gpu);
313 if (fence != gpu->hangcheck_fence) {
314 /* some progress has been made.. ya! */
315 gpu->hangcheck_fence = fence;
316 } else if (fence < gpu->submitted_fence) {
317 /* no progress and not done.. hung! */
318 gpu->hangcheck_fence = fence;
319 dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
321 dev_err(dev->dev, "%s: completed fence: %u\n",
323 dev_err(dev->dev, "%s: submitted fence: %u\n",
324 gpu->name, gpu->submitted_fence);
325 queue_work(priv->wq, &gpu->recover_work);
328 /* if still more pending work, reset the hangcheck timer: */
329 if (gpu->submitted_fence > gpu->hangcheck_fence)
330 hangcheck_timer_reset(gpu);
332 /* workaround for missing irq: */
333 queue_work(priv->wq, &gpu->retire_work);
337 * Performance Counters:
340 /* called under perf_lock */
341 static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
343 uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
344 int i, n = min(ncntrs, gpu->num_perfcntrs);
346 /* read current values: */
347 for (i = 0; i < gpu->num_perfcntrs; i++)
348 current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
351 for (i = 0; i < n; i++)
352 cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
354 /* save current values: */
355 for (i = 0; i < gpu->num_perfcntrs; i++)
356 gpu->last_cntrs[i] = current_cntrs[i];
361 static void update_sw_cntrs(struct msm_gpu *gpu)
367 spin_lock_irqsave(&gpu->perf_lock, flags);
368 if (!gpu->perfcntr_active)
372 elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
374 gpu->totaltime += elapsed;
375 if (gpu->last_sample.active)
376 gpu->activetime += elapsed;
378 gpu->last_sample.active = msm_gpu_active(gpu);
379 gpu->last_sample.time = time;
382 spin_unlock_irqrestore(&gpu->perf_lock, flags);
385 void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
389 spin_lock_irqsave(&gpu->perf_lock, flags);
390 /* we could dynamically enable/disable perfcntr registers too.. */
391 gpu->last_sample.active = msm_gpu_active(gpu);
392 gpu->last_sample.time = ktime_get();
393 gpu->activetime = gpu->totaltime = 0;
394 gpu->perfcntr_active = true;
395 update_hw_cntrs(gpu, 0, NULL);
396 spin_unlock_irqrestore(&gpu->perf_lock, flags);
399 void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
401 gpu->perfcntr_active = false;
404 /* returns -errno or # of cntrs sampled */
405 int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
406 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
411 spin_lock_irqsave(&gpu->perf_lock, flags);
413 if (!gpu->perfcntr_active) {
418 *activetime = gpu->activetime;
419 *totaltime = gpu->totaltime;
421 gpu->activetime = gpu->totaltime = 0;
423 ret = update_hw_cntrs(gpu, ncntrs, cntrs);
426 spin_unlock_irqrestore(&gpu->perf_lock, flags);
432 * Cmdstream submission/retirement:
435 static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
439 for (i = 0; i < submit->nr_bos; i++) {
440 struct msm_gem_object *msm_obj = submit->bos[i].obj;
441 /* move to inactive: */
442 msm_gem_move_to_inactive(&msm_obj->base);
443 msm_gem_put_iova(&msm_obj->base, gpu->id);
444 drm_gem_object_unreference(&msm_obj->base);
447 list_del(&submit->node);
451 static void retire_submits(struct msm_gpu *gpu, uint32_t fence)
453 struct drm_device *dev = gpu->dev;
455 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
457 while (!list_empty(&gpu->submit_list)) {
458 struct msm_gem_submit *submit;
460 submit = list_first_entry(&gpu->submit_list,
461 struct msm_gem_submit, node);
463 if (submit->fence <= fence) {
464 retire_submit(gpu, submit);
471 static void retire_worker(struct work_struct *work)
473 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
474 struct drm_device *dev = gpu->dev;
475 uint32_t fence = gpu->funcs->last_fence(gpu);
477 msm_update_fence(gpu->dev, fence);
479 mutex_lock(&dev->struct_mutex);
480 retire_submits(gpu, fence);
481 mutex_unlock(&dev->struct_mutex);
483 if (!msm_gpu_active(gpu))
487 /* call from irq handler to schedule work to retire bo's */
488 void msm_gpu_retire(struct msm_gpu *gpu)
490 struct msm_drm_private *priv = gpu->dev->dev_private;
491 queue_work(priv->wq, &gpu->retire_work);
492 update_sw_cntrs(gpu);
495 /* add bo's to gpu's ring, and kick gpu: */
496 int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
497 struct msm_file_private *ctx)
499 struct drm_device *dev = gpu->dev;
500 struct msm_drm_private *priv = dev->dev_private;
503 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
505 submit->fence = ++priv->next_fence;
507 gpu->submitted_fence = submit->fence;
509 inactive_cancel(gpu);
511 list_add_tail(&submit->node, &gpu->submit_list);
513 msm_rd_dump_submit(submit);
515 gpu->submitted_fence = submit->fence;
517 update_sw_cntrs(gpu);
519 for (i = 0; i < submit->nr_bos; i++) {
520 struct msm_gem_object *msm_obj = submit->bos[i].obj;
523 /* can't happen yet.. but when we add 2d support we'll have
524 * to deal w/ cross-ring synchronization:
526 WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
528 /* submit takes a reference to the bo and iova until retired: */
529 drm_gem_object_reference(&msm_obj->base);
530 msm_gem_get_iova_locked(&msm_obj->base,
531 submit->gpu->id, &iova);
533 if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
534 msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
536 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
537 msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
540 ret = gpu->funcs->submit(gpu, submit, ctx);
543 hangcheck_timer_reset(gpu);
552 static irqreturn_t irq_handler(int irq, void *data)
554 struct msm_gpu *gpu = data;
555 return gpu->funcs->irq(gpu);
558 static const char *clk_names[] = {
559 "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
563 int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
564 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
565 const char *name, const char *ioname, const char *irqname, int ringsz)
567 struct iommu_domain *iommu;
570 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
571 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
576 gpu->inactive = true;
578 INIT_LIST_HEAD(&gpu->active_list);
579 INIT_WORK(&gpu->retire_work, retire_worker);
580 INIT_WORK(&gpu->inactive_work, inactive_worker);
581 INIT_WORK(&gpu->recover_work, recover_worker);
583 INIT_LIST_HEAD(&gpu->submit_list);
585 setup_timer(&gpu->inactive_timer, inactive_handler,
587 setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
590 spin_lock_init(&gpu->perf_lock);
592 BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
595 gpu->mmio = msm_ioremap(pdev, ioname, name);
596 if (IS_ERR(gpu->mmio)) {
597 ret = PTR_ERR(gpu->mmio);
602 gpu->irq = platform_get_irq_byname(pdev, irqname);
605 dev_err(drm->dev, "failed to get irq: %d\n", ret);
609 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
610 IRQF_TRIGGER_HIGH, gpu->name, gpu);
612 dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
616 /* Acquire clocks: */
617 for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
618 gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
619 DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
620 if (IS_ERR(gpu->grp_clks[i]))
621 gpu->grp_clks[i] = NULL;
624 gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
625 DBG("ebi1_clk: %p", gpu->ebi1_clk);
626 if (IS_ERR(gpu->ebi1_clk))
627 gpu->ebi1_clk = NULL;
629 /* Acquire regulators: */
630 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
631 DBG("gpu_reg: %p", gpu->gpu_reg);
632 if (IS_ERR(gpu->gpu_reg))
635 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
636 DBG("gpu_cx: %p", gpu->gpu_cx);
637 if (IS_ERR(gpu->gpu_cx))
640 /* Setup IOMMU.. eventually we will (I think) do this once per context
641 * and have separate page tables per context. For now, to keep things
642 * simple and to get something working, just use a single address space:
644 iommu = iommu_domain_alloc(&platform_bus_type);
646 dev_info(drm->dev, "%s: using IOMMU\n", name);
647 gpu->mmu = msm_iommu_new(&pdev->dev, iommu);
648 if (IS_ERR(gpu->mmu)) {
649 ret = PTR_ERR(gpu->mmu);
650 dev_err(drm->dev, "failed to init iommu: %d\n", ret);
652 iommu_domain_free(iommu);
657 dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
659 gpu->id = msm_register_mmu(drm, gpu->mmu);
662 /* Create ringbuffer: */
663 mutex_lock(&drm->struct_mutex);
664 gpu->rb = msm_ringbuffer_new(gpu, ringsz);
665 mutex_unlock(&drm->struct_mutex);
666 if (IS_ERR(gpu->rb)) {
667 ret = PTR_ERR(gpu->rb);
669 dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
681 void msm_gpu_cleanup(struct msm_gpu *gpu)
683 DBG("%s", gpu->name);
685 WARN_ON(!list_empty(&gpu->active_list));
691 msm_gem_put_iova(gpu->rb->bo, gpu->id);
692 msm_ringbuffer_destroy(gpu->rb);
696 gpu->mmu->funcs->destroy(gpu->mmu);