blob: 5aed93a87e4c1b20dc61539785196048faecab64 [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 Clark40e68152016-05-03 09:50:26 -0400449 msm_gem_submit_free(submit);
Rob Clark7d12a272016-03-16 16:07:38 -0400450}
451
Rob Clarkb6295f92016-03-15 18:26:28 -0400452static void retire_submits(struct msm_gpu *gpu)
Rob Clark1a370be2015-06-07 13:46:04 -0400453{
454 struct drm_device *dev = gpu->dev;
455
456 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
457
458 while (!list_empty(&gpu->submit_list)) {
459 struct msm_gem_submit *submit;
460
461 submit = list_first_entry(&gpu->submit_list,
462 struct msm_gem_submit, node);
463
Rob Clarkb6295f92016-03-15 18:26:28 -0400464 if (fence_is_signaled(submit->fence)) {
Rob Clark7d12a272016-03-16 16:07:38 -0400465 retire_submit(gpu, submit);
Rob Clark1a370be2015-06-07 13:46:04 -0400466 } else {
467 break;
468 }
469 }
470}
471
Rob Clark7198e6b2013-07-19 12:59:32 -0400472static void retire_worker(struct work_struct *work)
473{
474 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
475 struct drm_device *dev = gpu->dev;
476 uint32_t fence = gpu->funcs->last_fence(gpu);
477
Rob Clarkca762a82016-03-15 17:22:13 -0400478 msm_update_fence(gpu->fctx, fence);
Rob Clarkedd4fc62013-09-14 14:01:55 -0400479
Rob Clark7198e6b2013-07-19 12:59:32 -0400480 mutex_lock(&dev->struct_mutex);
Rob Clarkb6295f92016-03-15 18:26:28 -0400481 retire_submits(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400482 mutex_unlock(&dev->struct_mutex);
Rob Clark37d77c32014-01-11 16:25:08 -0500483
484 if (!msm_gpu_active(gpu))
485 inactive_start(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400486}
487
488/* call from irq handler to schedule work to retire bo's */
489void msm_gpu_retire(struct msm_gpu *gpu)
490{
491 struct msm_drm_private *priv = gpu->dev->dev_private;
492 queue_work(priv->wq, &gpu->retire_work);
Rob Clark70c70f02014-05-30 14:49:43 -0400493 update_sw_cntrs(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400494}
495
496/* add bo's to gpu's ring, and kick gpu: */
497int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
498 struct msm_file_private *ctx)
499{
500 struct drm_device *dev = gpu->dev;
501 struct msm_drm_private *priv = dev->dev_private;
502 int i, ret;
503
Rob Clark1a370be2015-06-07 13:46:04 -0400504 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
505
Rob Clarkb6295f92016-03-15 18:26:28 -0400506 submit->fence = msm_fence_alloc(gpu->fctx);
507 if (IS_ERR(submit->fence)) {
508 ret = PTR_ERR(submit->fence);
509 submit->fence = NULL;
510 return ret;
511 }
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400512
Rob Clark37d77c32014-01-11 16:25:08 -0500513 inactive_cancel(gpu);
514
Rob Clark1a370be2015-06-07 13:46:04 -0400515 list_add_tail(&submit->node, &gpu->submit_list);
516
Rob Clarka7d3c952014-05-30 14:47:38 -0400517 msm_rd_dump_submit(submit);
518
Rob Clark70c70f02014-05-30 14:49:43 -0400519 update_sw_cntrs(gpu);
520
Rob Clark7198e6b2013-07-19 12:59:32 -0400521 for (i = 0; i < submit->nr_bos; i++) {
522 struct msm_gem_object *msm_obj = submit->bos[i].obj;
Rob Clark7d12a272016-03-16 16:07:38 -0400523 uint32_t iova;
Rob Clark7198e6b2013-07-19 12:59:32 -0400524
525 /* can't happen yet.. but when we add 2d support we'll have
526 * to deal w/ cross-ring synchronization:
527 */
528 WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
529
Rob Clark7d12a272016-03-16 16:07:38 -0400530 /* submit takes a reference to the bo and iova until retired: */
531 drm_gem_object_reference(&msm_obj->base);
532 msm_gem_get_iova_locked(&msm_obj->base,
533 submit->gpu->id, &iova);
Rob Clark7198e6b2013-07-19 12:59:32 -0400534
Rob Clarkbf6811f2013-09-01 13:25:09 -0400535 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
536 msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
Rob Clarkb6295f92016-03-15 18:26:28 -0400537 else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
538 msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
Rob Clark7198e6b2013-07-19 12:59:32 -0400539 }
Rob Clark1a370be2015-06-07 13:46:04 -0400540
Rob Clark1193c3b2016-05-03 09:46:49 -0400541 gpu->funcs->submit(gpu, submit, ctx);
Rob Clark1a370be2015-06-07 13:46:04 -0400542 priv->lastctx = ctx;
543
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400544 hangcheck_timer_reset(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400545
Rob Clark1193c3b2016-05-03 09:46:49 -0400546 return 0;
Rob Clark7198e6b2013-07-19 12:59:32 -0400547}
548
549/*
550 * Init/Cleanup:
551 */
552
553static irqreturn_t irq_handler(int irq, void *data)
554{
555 struct msm_gpu *gpu = data;
556 return gpu->funcs->irq(gpu);
557}
558
559static const char *clk_names[] = {
560 "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
Rob Clarkde558cd2015-05-06 13:14:30 -0400561 "alt_mem_iface_clk",
Rob Clark7198e6b2013-07-19 12:59:32 -0400562};
563
564int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
565 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
566 const char *name, const char *ioname, const char *irqname, int ringsz)
567{
Rob Clark871d8122013-11-16 12:56:06 -0500568 struct iommu_domain *iommu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400569 int i, ret;
570
Rob Clark70c70f02014-05-30 14:49:43 -0400571 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
572 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
573
Rob Clark7198e6b2013-07-19 12:59:32 -0400574 gpu->dev = drm;
575 gpu->funcs = funcs;
576 gpu->name = name;
Rob Clark37d77c32014-01-11 16:25:08 -0500577 gpu->inactive = true;
Rob Clarkca762a82016-03-15 17:22:13 -0400578 gpu->fctx = msm_fence_context_alloc(drm, name);
579 if (IS_ERR(gpu->fctx)) {
580 ret = PTR_ERR(gpu->fctx);
581 gpu->fctx = NULL;
582 goto fail;
583 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400584
585 INIT_LIST_HEAD(&gpu->active_list);
586 INIT_WORK(&gpu->retire_work, retire_worker);
Rob Clark37d77c32014-01-11 16:25:08 -0500587 INIT_WORK(&gpu->inactive_work, inactive_worker);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400588 INIT_WORK(&gpu->recover_work, recover_worker);
589
Rob Clark1a370be2015-06-07 13:46:04 -0400590 INIT_LIST_HEAD(&gpu->submit_list);
591
Rob Clark37d77c32014-01-11 16:25:08 -0500592 setup_timer(&gpu->inactive_timer, inactive_handler,
593 (unsigned long)gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400594 setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
595 (unsigned long)gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400596
Rob Clark70c70f02014-05-30 14:49:43 -0400597 spin_lock_init(&gpu->perf_lock);
598
Rob Clark7198e6b2013-07-19 12:59:32 -0400599 BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
600
601 /* Map registers: */
602 gpu->mmio = msm_ioremap(pdev, ioname, name);
603 if (IS_ERR(gpu->mmio)) {
604 ret = PTR_ERR(gpu->mmio);
605 goto fail;
606 }
607
608 /* Get Interrupt: */
609 gpu->irq = platform_get_irq_byname(pdev, irqname);
610 if (gpu->irq < 0) {
611 ret = gpu->irq;
612 dev_err(drm->dev, "failed to get irq: %d\n", ret);
613 goto fail;
614 }
615
616 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
617 IRQF_TRIGGER_HIGH, gpu->name, gpu);
618 if (ret) {
619 dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
620 goto fail;
621 }
622
623 /* Acquire clocks: */
624 for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
625 gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
626 DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
627 if (IS_ERR(gpu->grp_clks[i]))
628 gpu->grp_clks[i] = NULL;
629 }
630
631 gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
632 DBG("ebi1_clk: %p", gpu->ebi1_clk);
633 if (IS_ERR(gpu->ebi1_clk))
634 gpu->ebi1_clk = NULL;
635
636 /* Acquire regulators: */
637 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
638 DBG("gpu_reg: %p", gpu->gpu_reg);
639 if (IS_ERR(gpu->gpu_reg))
640 gpu->gpu_reg = NULL;
641
642 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
643 DBG("gpu_cx: %p", gpu->gpu_cx);
644 if (IS_ERR(gpu->gpu_cx))
645 gpu->gpu_cx = NULL;
646
647 /* Setup IOMMU.. eventually we will (I think) do this once per context
648 * and have separate page tables per context. For now, to keep things
649 * simple and to get something working, just use a single address space:
650 */
Rob Clark871d8122013-11-16 12:56:06 -0500651 iommu = iommu_domain_alloc(&platform_bus_type);
652 if (iommu) {
653 dev_info(drm->dev, "%s: using IOMMU\n", name);
Rob Clark944fc362014-07-09 22:08:15 -0400654 gpu->mmu = msm_iommu_new(&pdev->dev, iommu);
Stephane Viau5e921b12015-09-15 08:41:46 -0400655 if (IS_ERR(gpu->mmu)) {
656 ret = PTR_ERR(gpu->mmu);
657 dev_err(drm->dev, "failed to init iommu: %d\n", ret);
658 gpu->mmu = NULL;
659 iommu_domain_free(iommu);
660 goto fail;
661 }
662
Rob Clark871d8122013-11-16 12:56:06 -0500663 } else {
664 dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
Rob Clark7198e6b2013-07-19 12:59:32 -0400665 }
Rob Clark871d8122013-11-16 12:56:06 -0500666 gpu->id = msm_register_mmu(drm, gpu->mmu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400667
Rob Clarka1ad3522014-07-11 11:59:22 -0400668
Rob Clark7198e6b2013-07-19 12:59:32 -0400669 /* Create ringbuffer: */
Rob Clarka1ad3522014-07-11 11:59:22 -0400670 mutex_lock(&drm->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400671 gpu->rb = msm_ringbuffer_new(gpu, ringsz);
Rob Clarka1ad3522014-07-11 11:59:22 -0400672 mutex_unlock(&drm->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400673 if (IS_ERR(gpu->rb)) {
674 ret = PTR_ERR(gpu->rb);
675 gpu->rb = NULL;
676 dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
677 goto fail;
678 }
679
Rob Clarkbf2b33af2013-11-15 09:03:15 -0500680 bs_init(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400681
682 return 0;
683
684fail:
685 return ret;
686}
687
688void msm_gpu_cleanup(struct msm_gpu *gpu)
689{
690 DBG("%s", gpu->name);
691
692 WARN_ON(!list_empty(&gpu->active_list));
693
694 bs_fini(gpu);
695
696 if (gpu->rb) {
697 if (gpu->rb_iova)
698 msm_gem_put_iova(gpu->rb->bo, gpu->id);
699 msm_ringbuffer_destroy(gpu->rb);
700 }
701
Rob Clark871d8122013-11-16 12:56:06 -0500702 if (gpu->mmu)
703 gpu->mmu->funcs->destroy(gpu->mmu);
Rob Clarkca762a82016-03-15 17:22:13 -0400704
705 if (gpu->fctx)
706 msm_fence_context_free(gpu->fctx);
Rob Clark7198e6b2013-07-19 12:59:32 -0400707}