blob: f94d01099e19838064c96591f56a4cdfa8640a32 [file] [log] [blame]
Rob Clark7198e6b2013-07-19 12:59:32 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
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.
8 *
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
12 * more details.
13 *
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/>.
16 */
17
18#include "msm_gpu.h"
19#include "msm_gem.h"
Rob Clark871d8122013-11-16 12:56:06 -050020#include "msm_mmu.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040021#include "msm_fence.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040022
23
24/*
25 * Power Management:
26 */
27
Rob Clark6490ad42015-06-04 10:26:37 -040028#ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
Rob Clark7198e6b2013-07-19 12:59:32 -040029#include <mach/board.h>
Rob Clarkbf2b33af2013-11-15 09:03:15 -050030static void bs_init(struct msm_gpu *gpu)
Rob Clark7198e6b2013-07-19 12:59:32 -040031{
Rob Clarkbf2b33af2013-11-15 09:03:15 -050032 if (gpu->bus_scale_table) {
33 gpu->bsc = msm_bus_scale_register_client(gpu->bus_scale_table);
Rob Clark7198e6b2013-07-19 12:59:32 -040034 DBG("bus scale client: %08x", gpu->bsc);
35 }
36}
37
38static void bs_fini(struct msm_gpu *gpu)
39{
40 if (gpu->bsc) {
41 msm_bus_scale_unregister_client(gpu->bsc);
42 gpu->bsc = 0;
43 }
44}
45
46static void bs_set(struct msm_gpu *gpu, int idx)
47{
48 if (gpu->bsc) {
49 DBG("set bus scaling: %d", idx);
50 msm_bus_scale_client_update_request(gpu->bsc, idx);
51 }
52}
53#else
Rob Clarkbf2b33af2013-11-15 09:03:15 -050054static void bs_init(struct msm_gpu *gpu) {}
Rob Clark7198e6b2013-07-19 12:59:32 -040055static void bs_fini(struct msm_gpu *gpu) {}
56static void bs_set(struct msm_gpu *gpu, int idx) {}
57#endif
58
59static int enable_pwrrail(struct msm_gpu *gpu)
60{
61 struct drm_device *dev = gpu->dev;
62 int ret = 0;
63
64 if (gpu->gpu_reg) {
65 ret = regulator_enable(gpu->gpu_reg);
66 if (ret) {
67 dev_err(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
68 return ret;
69 }
70 }
71
72 if (gpu->gpu_cx) {
73 ret = regulator_enable(gpu->gpu_cx);
74 if (ret) {
75 dev_err(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
76 return ret;
77 }
78 }
79
80 return 0;
81}
82
83static int disable_pwrrail(struct msm_gpu *gpu)
84{
85 if (gpu->gpu_cx)
86 regulator_disable(gpu->gpu_cx);
87 if (gpu->gpu_reg)
88 regulator_disable(gpu->gpu_reg);
89 return 0;
90}
91
92static int enable_clk(struct msm_gpu *gpu)
93{
94 struct clk *rate_clk = NULL;
95 int i;
96
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];
102 }
103 }
104
105 if (rate_clk && gpu->fast_rate)
106 clk_set_rate(rate_clk, gpu->fast_rate);
107
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]);
111
112 return 0;
113}
114
115static int disable_clk(struct msm_gpu *gpu)
116{
117 struct clk *rate_clk = NULL;
118 int i;
119
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];
125 }
126 }
127
128 if (rate_clk && gpu->slow_rate)
129 clk_set_rate(rate_clk, gpu->slow_rate);
130
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]);
134
135 return 0;
136}
137
138static int enable_axi(struct msm_gpu *gpu)
139{
140 if (gpu->ebi1_clk)
141 clk_prepare_enable(gpu->ebi1_clk);
142 if (gpu->bus_freq)
143 bs_set(gpu, gpu->bus_freq);
144 return 0;
145}
146
147static int disable_axi(struct msm_gpu *gpu)
148{
149 if (gpu->ebi1_clk)
150 clk_disable_unprepare(gpu->ebi1_clk);
151 if (gpu->bus_freq)
152 bs_set(gpu, 0);
153 return 0;
154}
155
156int msm_gpu_pm_resume(struct msm_gpu *gpu)
157{
Rob Clark37d77c32014-01-11 16:25:08 -0500158 struct drm_device *dev = gpu->dev;
Rob Clark7198e6b2013-07-19 12:59:32 -0400159 int ret;
160
Rob Clark37d77c32014-01-11 16:25:08 -0500161 DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
162
163 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
164
165 if (gpu->active_cnt++ > 0)
166 return 0;
167
168 if (WARN_ON(gpu->active_cnt <= 0))
169 return -EINVAL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400170
171 ret = enable_pwrrail(gpu);
172 if (ret)
173 return ret;
174
175 ret = enable_clk(gpu);
176 if (ret)
177 return ret;
178
179 ret = enable_axi(gpu);
180 if (ret)
181 return ret;
182
183 return 0;
184}
185
186int msm_gpu_pm_suspend(struct msm_gpu *gpu)
187{
Rob Clark37d77c32014-01-11 16:25:08 -0500188 struct drm_device *dev = gpu->dev;
Rob Clark7198e6b2013-07-19 12:59:32 -0400189 int ret;
190
Rob Clark37d77c32014-01-11 16:25:08 -0500191 DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
192
193 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
194
195 if (--gpu->active_cnt > 0)
196 return 0;
197
198 if (WARN_ON(gpu->active_cnt < 0))
199 return -EINVAL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400200
201 ret = disable_axi(gpu);
202 if (ret)
203 return ret;
204
205 ret = disable_clk(gpu);
206 if (ret)
207 return ret;
208
209 ret = disable_pwrrail(gpu);
210 if (ret)
211 return ret;
212
213 return 0;
214}
215
216/*
Rob Clark37d77c32014-01-11 16:25:08 -0500217 * Inactivity detection (for suspend):
218 */
219
220static void inactive_worker(struct work_struct *work)
221{
222 struct msm_gpu *gpu = container_of(work, struct msm_gpu, inactive_work);
223 struct drm_device *dev = gpu->dev;
224
225 if (gpu->inactive)
226 return;
227
228 DBG("%s: inactive!\n", gpu->name);
229 mutex_lock(&dev->struct_mutex);
230 if (!(msm_gpu_active(gpu) || gpu->inactive)) {
231 disable_axi(gpu);
232 disable_clk(gpu);
233 gpu->inactive = true;
234 }
235 mutex_unlock(&dev->struct_mutex);
236}
237
238static void inactive_handler(unsigned long data)
239{
240 struct msm_gpu *gpu = (struct msm_gpu *)data;
241 struct msm_drm_private *priv = gpu->dev->dev_private;
242
243 queue_work(priv->wq, &gpu->inactive_work);
244}
245
246/* cancel inactive timer and make sure we are awake: */
247static void inactive_cancel(struct msm_gpu *gpu)
248{
249 DBG("%s", gpu->name);
250 del_timer(&gpu->inactive_timer);
251 if (gpu->inactive) {
252 enable_clk(gpu);
253 enable_axi(gpu);
254 gpu->inactive = false;
255 }
256}
257
258static void inactive_start(struct msm_gpu *gpu)
259{
260 DBG("%s", gpu->name);
261 mod_timer(&gpu->inactive_timer,
262 round_jiffies_up(jiffies + DRM_MSM_INACTIVE_JIFFIES));
263}
264
265/*
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400266 * Hangcheck detection for locked gpu:
267 */
268
Rob Clarkb6295f92016-03-15 18:26:28 -0400269static void retire_submits(struct msm_gpu *gpu);
Rob Clark1a370be2015-06-07 13:46:04 -0400270
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400271static void recover_worker(struct work_struct *work)
272{
273 struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
274 struct drm_device *dev = gpu->dev;
Rob Clarkb6295f92016-03-15 18:26:28 -0400275 uint32_t fence = gpu->funcs->last_fence(gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400276
277 dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
278
Rob Clarkb6295f92016-03-15 18:26:28 -0400279 msm_update_fence(gpu->fctx, fence + 1);
280
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400281 mutex_lock(&dev->struct_mutex);
Rob Clark37d77c32014-01-11 16:25:08 -0500282 if (msm_gpu_active(gpu)) {
Rob Clark1a370be2015-06-07 13:46:04 -0400283 struct msm_gem_submit *submit;
Rob Clark1a370be2015-06-07 13:46:04 -0400284
285 /* retire completed submits, plus the one that hung: */
Rob Clarkb6295f92016-03-15 18:26:28 -0400286 retire_submits(gpu);
Rob Clark1a370be2015-06-07 13:46:04 -0400287
Rob Clark37d77c32014-01-11 16:25:08 -0500288 inactive_cancel(gpu);
289 gpu->funcs->recover(gpu);
Rob Clark1a370be2015-06-07 13:46:04 -0400290
291 /* replay the remaining submits after the one that hung: */
292 list_for_each_entry(submit, &gpu->submit_list, node) {
293 gpu->funcs->submit(gpu, submit, NULL);
294 }
Rob Clark37d77c32014-01-11 16:25:08 -0500295 }
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400296 mutex_unlock(&dev->struct_mutex);
297
298 msm_gpu_retire(gpu);
299}
300
301static void hangcheck_timer_reset(struct msm_gpu *gpu)
302{
303 DBG("%s", gpu->name);
304 mod_timer(&gpu->hangcheck_timer,
305 round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
306}
307
308static void hangcheck_handler(unsigned long data)
309{
310 struct msm_gpu *gpu = (struct msm_gpu *)data;
Rob Clark6b8819c2013-09-11 17:14:30 -0400311 struct drm_device *dev = gpu->dev;
312 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400313 uint32_t fence = gpu->funcs->last_fence(gpu);
314
315 if (fence != gpu->hangcheck_fence) {
316 /* some progress has been made.. ya! */
317 gpu->hangcheck_fence = fence;
Rob Clarkca762a82016-03-15 17:22:13 -0400318 } else if (fence < gpu->fctx->last_fence) {
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400319 /* no progress and not done.. hung! */
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400320 gpu->hangcheck_fence = fence;
Rob Clark26791c42013-09-03 07:12:03 -0400321 dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
322 gpu->name);
323 dev_err(dev->dev, "%s: completed fence: %u\n",
324 gpu->name, fence);
325 dev_err(dev->dev, "%s: submitted fence: %u\n",
Rob Clarkca762a82016-03-15 17:22:13 -0400326 gpu->name, gpu->fctx->last_fence);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400327 queue_work(priv->wq, &gpu->recover_work);
328 }
329
330 /* if still more pending work, reset the hangcheck timer: */
Rob Clarkca762a82016-03-15 17:22:13 -0400331 if (gpu->fctx->last_fence > gpu->hangcheck_fence)
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400332 hangcheck_timer_reset(gpu);
Rob Clark6b8819c2013-09-11 17:14:30 -0400333
334 /* workaround for missing irq: */
335 queue_work(priv->wq, &gpu->retire_work);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400336}
337
338/*
Rob Clark70c70f02014-05-30 14:49:43 -0400339 * Performance Counters:
340 */
341
342/* called under perf_lock */
343static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
344{
345 uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
346 int i, n = min(ncntrs, gpu->num_perfcntrs);
347
348 /* read current values: */
349 for (i = 0; i < gpu->num_perfcntrs; i++)
350 current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
351
352 /* update cntrs: */
353 for (i = 0; i < n; i++)
354 cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
355
356 /* save current values: */
357 for (i = 0; i < gpu->num_perfcntrs; i++)
358 gpu->last_cntrs[i] = current_cntrs[i];
359
360 return n;
361}
362
363static void update_sw_cntrs(struct msm_gpu *gpu)
364{
365 ktime_t time;
366 uint32_t elapsed;
367 unsigned long flags;
368
369 spin_lock_irqsave(&gpu->perf_lock, flags);
370 if (!gpu->perfcntr_active)
371 goto out;
372
373 time = ktime_get();
374 elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
375
376 gpu->totaltime += elapsed;
377 if (gpu->last_sample.active)
378 gpu->activetime += elapsed;
379
380 gpu->last_sample.active = msm_gpu_active(gpu);
381 gpu->last_sample.time = time;
382
383out:
384 spin_unlock_irqrestore(&gpu->perf_lock, flags);
385}
386
387void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
388{
389 unsigned long flags;
390
391 spin_lock_irqsave(&gpu->perf_lock, flags);
392 /* we could dynamically enable/disable perfcntr registers too.. */
393 gpu->last_sample.active = msm_gpu_active(gpu);
394 gpu->last_sample.time = ktime_get();
395 gpu->activetime = gpu->totaltime = 0;
396 gpu->perfcntr_active = true;
397 update_hw_cntrs(gpu, 0, NULL);
398 spin_unlock_irqrestore(&gpu->perf_lock, flags);
399}
400
401void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
402{
403 gpu->perfcntr_active = false;
404}
405
406/* returns -errno or # of cntrs sampled */
407int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
408 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
409{
410 unsigned long flags;
411 int ret;
412
413 spin_lock_irqsave(&gpu->perf_lock, flags);
414
415 if (!gpu->perfcntr_active) {
416 ret = -EINVAL;
417 goto out;
418 }
419
420 *activetime = gpu->activetime;
421 *totaltime = gpu->totaltime;
422
423 gpu->activetime = gpu->totaltime = 0;
424
425 ret = update_hw_cntrs(gpu, ncntrs, cntrs);
426
427out:
428 spin_unlock_irqrestore(&gpu->perf_lock, flags);
429
430 return ret;
431}
432
433/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400434 * Cmdstream submission/retirement:
435 */
436
Rob Clark7d12a272016-03-16 16:07:38 -0400437static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
438{
439 int i;
440
441 for (i = 0; i < submit->nr_bos; i++) {
442 struct msm_gem_object *msm_obj = submit->bos[i].obj;
443 /* move to inactive: */
444 msm_gem_move_to_inactive(&msm_obj->base);
445 msm_gem_put_iova(&msm_obj->base, gpu->id);
446 drm_gem_object_unreference(&msm_obj->base);
447 }
448
Rob Clarkb6295f92016-03-15 18:26:28 -0400449 fence_put(submit->fence);
Rob Clark7d12a272016-03-16 16:07:38 -0400450 list_del(&submit->node);
451 kfree(submit);
452}
453
Rob Clarkb6295f92016-03-15 18:26:28 -0400454static void retire_submits(struct msm_gpu *gpu)
Rob Clark1a370be2015-06-07 13:46:04 -0400455{
456 struct drm_device *dev = gpu->dev;
457
458 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
459
460 while (!list_empty(&gpu->submit_list)) {
461 struct msm_gem_submit *submit;
462
463 submit = list_first_entry(&gpu->submit_list,
464 struct msm_gem_submit, node);
465
Rob Clarkb6295f92016-03-15 18:26:28 -0400466 if (fence_is_signaled(submit->fence)) {
Rob Clark7d12a272016-03-16 16:07:38 -0400467 retire_submit(gpu, submit);
Rob Clark1a370be2015-06-07 13:46:04 -0400468 } else {
469 break;
470 }
471 }
472}
473
Rob Clark7198e6b2013-07-19 12:59:32 -0400474static void retire_worker(struct work_struct *work)
475{
476 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
477 struct drm_device *dev = gpu->dev;
478 uint32_t fence = gpu->funcs->last_fence(gpu);
479
Rob Clarkca762a82016-03-15 17:22:13 -0400480 msm_update_fence(gpu->fctx, fence);
Rob Clarkedd4fc62013-09-14 14:01:55 -0400481
Rob Clark7198e6b2013-07-19 12:59:32 -0400482 mutex_lock(&dev->struct_mutex);
Rob Clarkb6295f92016-03-15 18:26:28 -0400483 retire_submits(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400484 mutex_unlock(&dev->struct_mutex);
Rob Clark37d77c32014-01-11 16:25:08 -0500485
486 if (!msm_gpu_active(gpu))
487 inactive_start(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400488}
489
490/* call from irq handler to schedule work to retire bo's */
491void msm_gpu_retire(struct msm_gpu *gpu)
492{
493 struct msm_drm_private *priv = gpu->dev->dev_private;
494 queue_work(priv->wq, &gpu->retire_work);
Rob Clark70c70f02014-05-30 14:49:43 -0400495 update_sw_cntrs(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400496}
497
498/* add bo's to gpu's ring, and kick gpu: */
499int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
500 struct msm_file_private *ctx)
501{
502 struct drm_device *dev = gpu->dev;
503 struct msm_drm_private *priv = dev->dev_private;
504 int i, ret;
505
Rob Clark1a370be2015-06-07 13:46:04 -0400506 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
507
Rob Clarkb6295f92016-03-15 18:26:28 -0400508 submit->fence = msm_fence_alloc(gpu->fctx);
509 if (IS_ERR(submit->fence)) {
510 ret = PTR_ERR(submit->fence);
511 submit->fence = NULL;
512 return ret;
513 }
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400514
Rob Clark37d77c32014-01-11 16:25:08 -0500515 inactive_cancel(gpu);
516
Rob Clark1a370be2015-06-07 13:46:04 -0400517 list_add_tail(&submit->node, &gpu->submit_list);
518
Rob Clarka7d3c952014-05-30 14:47:38 -0400519 msm_rd_dump_submit(submit);
520
Rob Clark70c70f02014-05-30 14:49:43 -0400521 update_sw_cntrs(gpu);
522
Rob Clark7198e6b2013-07-19 12:59:32 -0400523 for (i = 0; i < submit->nr_bos; i++) {
524 struct msm_gem_object *msm_obj = submit->bos[i].obj;
Rob Clark7d12a272016-03-16 16:07:38 -0400525 uint32_t iova;
Rob Clark7198e6b2013-07-19 12:59:32 -0400526
527 /* can't happen yet.. but when we add 2d support we'll have
528 * to deal w/ cross-ring synchronization:
529 */
530 WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
531
Rob Clark7d12a272016-03-16 16:07:38 -0400532 /* submit takes a reference to the bo and iova until retired: */
533 drm_gem_object_reference(&msm_obj->base);
534 msm_gem_get_iova_locked(&msm_obj->base,
535 submit->gpu->id, &iova);
Rob Clark7198e6b2013-07-19 12:59:32 -0400536
Rob Clarkbf6811f2013-09-01 13:25:09 -0400537 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
538 msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
Rob Clarkb6295f92016-03-15 18:26:28 -0400539 else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
540 msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
Rob Clark7198e6b2013-07-19 12:59:32 -0400541 }
Rob Clark1a370be2015-06-07 13:46:04 -0400542
543 ret = gpu->funcs->submit(gpu, submit, ctx);
544 priv->lastctx = ctx;
545
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400546 hangcheck_timer_reset(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400547
548 return ret;
549}
550
551/*
552 * Init/Cleanup:
553 */
554
555static irqreturn_t irq_handler(int irq, void *data)
556{
557 struct msm_gpu *gpu = data;
558 return gpu->funcs->irq(gpu);
559}
560
561static const char *clk_names[] = {
562 "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
Rob Clarkde558cd2015-05-06 13:14:30 -0400563 "alt_mem_iface_clk",
Rob Clark7198e6b2013-07-19 12:59:32 -0400564};
565
566int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
567 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
568 const char *name, const char *ioname, const char *irqname, int ringsz)
569{
Rob Clark871d8122013-11-16 12:56:06 -0500570 struct iommu_domain *iommu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400571 int i, ret;
572
Rob Clark70c70f02014-05-30 14:49:43 -0400573 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
574 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
575
Rob Clark7198e6b2013-07-19 12:59:32 -0400576 gpu->dev = drm;
577 gpu->funcs = funcs;
578 gpu->name = name;
Rob Clark37d77c32014-01-11 16:25:08 -0500579 gpu->inactive = true;
Rob Clarkca762a82016-03-15 17:22:13 -0400580 gpu->fctx = msm_fence_context_alloc(drm, name);
581 if (IS_ERR(gpu->fctx)) {
582 ret = PTR_ERR(gpu->fctx);
583 gpu->fctx = NULL;
584 goto fail;
585 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400586
587 INIT_LIST_HEAD(&gpu->active_list);
588 INIT_WORK(&gpu->retire_work, retire_worker);
Rob Clark37d77c32014-01-11 16:25:08 -0500589 INIT_WORK(&gpu->inactive_work, inactive_worker);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400590 INIT_WORK(&gpu->recover_work, recover_worker);
591
Rob Clark1a370be2015-06-07 13:46:04 -0400592 INIT_LIST_HEAD(&gpu->submit_list);
593
Rob Clark37d77c32014-01-11 16:25:08 -0500594 setup_timer(&gpu->inactive_timer, inactive_handler,
595 (unsigned long)gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400596 setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
597 (unsigned long)gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400598
Rob Clark70c70f02014-05-30 14:49:43 -0400599 spin_lock_init(&gpu->perf_lock);
600
Rob Clark7198e6b2013-07-19 12:59:32 -0400601 BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
602
603 /* Map registers: */
604 gpu->mmio = msm_ioremap(pdev, ioname, name);
605 if (IS_ERR(gpu->mmio)) {
606 ret = PTR_ERR(gpu->mmio);
607 goto fail;
608 }
609
610 /* Get Interrupt: */
611 gpu->irq = platform_get_irq_byname(pdev, irqname);
612 if (gpu->irq < 0) {
613 ret = gpu->irq;
614 dev_err(drm->dev, "failed to get irq: %d\n", ret);
615 goto fail;
616 }
617
618 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
619 IRQF_TRIGGER_HIGH, gpu->name, gpu);
620 if (ret) {
621 dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
622 goto fail;
623 }
624
625 /* Acquire clocks: */
626 for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
627 gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
628 DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
629 if (IS_ERR(gpu->grp_clks[i]))
630 gpu->grp_clks[i] = NULL;
631 }
632
633 gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
634 DBG("ebi1_clk: %p", gpu->ebi1_clk);
635 if (IS_ERR(gpu->ebi1_clk))
636 gpu->ebi1_clk = NULL;
637
638 /* Acquire regulators: */
639 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
640 DBG("gpu_reg: %p", gpu->gpu_reg);
641 if (IS_ERR(gpu->gpu_reg))
642 gpu->gpu_reg = NULL;
643
644 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
645 DBG("gpu_cx: %p", gpu->gpu_cx);
646 if (IS_ERR(gpu->gpu_cx))
647 gpu->gpu_cx = NULL;
648
649 /* Setup IOMMU.. eventually we will (I think) do this once per context
650 * and have separate page tables per context. For now, to keep things
651 * simple and to get something working, just use a single address space:
652 */
Rob Clark871d8122013-11-16 12:56:06 -0500653 iommu = iommu_domain_alloc(&platform_bus_type);
654 if (iommu) {
655 dev_info(drm->dev, "%s: using IOMMU\n", name);
Rob Clark944fc362014-07-09 22:08:15 -0400656 gpu->mmu = msm_iommu_new(&pdev->dev, iommu);
Stephane Viau5e921b12015-09-15 08:41:46 -0400657 if (IS_ERR(gpu->mmu)) {
658 ret = PTR_ERR(gpu->mmu);
659 dev_err(drm->dev, "failed to init iommu: %d\n", ret);
660 gpu->mmu = NULL;
661 iommu_domain_free(iommu);
662 goto fail;
663 }
664
Rob Clark871d8122013-11-16 12:56:06 -0500665 } else {
666 dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
Rob Clark7198e6b2013-07-19 12:59:32 -0400667 }
Rob Clark871d8122013-11-16 12:56:06 -0500668 gpu->id = msm_register_mmu(drm, gpu->mmu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400669
Rob Clarka1ad3522014-07-11 11:59:22 -0400670
Rob Clark7198e6b2013-07-19 12:59:32 -0400671 /* Create ringbuffer: */
Rob Clarka1ad3522014-07-11 11:59:22 -0400672 mutex_lock(&drm->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400673 gpu->rb = msm_ringbuffer_new(gpu, ringsz);
Rob Clarka1ad3522014-07-11 11:59:22 -0400674 mutex_unlock(&drm->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400675 if (IS_ERR(gpu->rb)) {
676 ret = PTR_ERR(gpu->rb);
677 gpu->rb = NULL;
678 dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
679 goto fail;
680 }
681
Rob Clarkbf2b33af2013-11-15 09:03:15 -0500682 bs_init(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400683
684 return 0;
685
686fail:
687 return ret;
688}
689
690void msm_gpu_cleanup(struct msm_gpu *gpu)
691{
692 DBG("%s", gpu->name);
693
694 WARN_ON(!list_empty(&gpu->active_list));
695
696 bs_fini(gpu);
697
698 if (gpu->rb) {
699 if (gpu->rb_iova)
700 msm_gem_put_iova(gpu->rb->bo, gpu->id);
701 msm_ringbuffer_destroy(gpu->rb);
702 }
703
Rob Clark871d8122013-11-16 12:56:06 -0500704 if (gpu->mmu)
705 gpu->mmu->funcs->destroy(gpu->mmu);
Rob Clarkca762a82016-03-15 17:22:13 -0400706
707 if (gpu->fctx)
708 msm_fence_context_free(gpu->fctx);
Rob Clark7198e6b2013-07-19 12:59:32 -0400709}