blob: 49d9e104af31cdf5952e9531c0a75eed320faec6 [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 Clarkbf2b33a2013-11-15 09:03:15 -050030static void bs_init(struct msm_gpu *gpu)
Rob Clark7198e6b2013-07-19 12:59:32 -040031{
Rob Clarkbf2b33a2013-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 Clarkbf2b33a2013-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 Clark4816b622016-05-03 10:10:15 -0400275 struct msm_gem_submit *submit;
Rob Clarkb6295f92016-03-15 18:26:28 -0400276 uint32_t fence = gpu->funcs->last_fence(gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400277
Rob Clarkb6295f92016-03-15 18:26:28 -0400278 msm_update_fence(gpu->fctx, fence + 1);
279
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400280 mutex_lock(&dev->struct_mutex);
Rob Clark1a370be2015-06-07 13:46:04 -0400281
Rob Clark4816b622016-05-03 10:10:15 -0400282 dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
283 list_for_each_entry(submit, &gpu->submit_list, node) {
284 if (submit->fence->seqno == (fence + 1)) {
285 struct task_struct *task;
286
287 rcu_read_lock();
288 task = pid_task(submit->pid, PIDTYPE_PID);
289 if (task) {
290 dev_err(dev->dev, "%s: offending task: %s\n",
291 gpu->name, task->comm);
292 }
293 rcu_read_unlock();
294 break;
295 }
296 }
297
298 if (msm_gpu_active(gpu)) {
Rob Clark1a370be2015-06-07 13:46:04 -0400299 /* retire completed submits, plus the one that hung: */
Rob Clarkb6295f92016-03-15 18:26:28 -0400300 retire_submits(gpu);
Rob Clark1a370be2015-06-07 13:46:04 -0400301
Rob Clark37d77c32014-01-11 16:25:08 -0500302 inactive_cancel(gpu);
303 gpu->funcs->recover(gpu);
Rob Clark1a370be2015-06-07 13:46:04 -0400304
305 /* replay the remaining submits after the one that hung: */
306 list_for_each_entry(submit, &gpu->submit_list, node) {
307 gpu->funcs->submit(gpu, submit, NULL);
308 }
Rob Clark37d77c32014-01-11 16:25:08 -0500309 }
Rob Clark4816b622016-05-03 10:10:15 -0400310
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400311 mutex_unlock(&dev->struct_mutex);
312
313 msm_gpu_retire(gpu);
314}
315
316static void hangcheck_timer_reset(struct msm_gpu *gpu)
317{
318 DBG("%s", gpu->name);
319 mod_timer(&gpu->hangcheck_timer,
320 round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
321}
322
323static void hangcheck_handler(unsigned long data)
324{
325 struct msm_gpu *gpu = (struct msm_gpu *)data;
Rob Clark6b8819c2013-09-11 17:14:30 -0400326 struct drm_device *dev = gpu->dev;
327 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400328 uint32_t fence = gpu->funcs->last_fence(gpu);
329
330 if (fence != gpu->hangcheck_fence) {
331 /* some progress has been made.. ya! */
332 gpu->hangcheck_fence = fence;
Rob Clarkca762a82016-03-15 17:22:13 -0400333 } else if (fence < gpu->fctx->last_fence) {
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400334 /* no progress and not done.. hung! */
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400335 gpu->hangcheck_fence = fence;
Rob Clark26791c42013-09-03 07:12:03 -0400336 dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
337 gpu->name);
338 dev_err(dev->dev, "%s: completed fence: %u\n",
339 gpu->name, fence);
340 dev_err(dev->dev, "%s: submitted fence: %u\n",
Rob Clarkca762a82016-03-15 17:22:13 -0400341 gpu->name, gpu->fctx->last_fence);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400342 queue_work(priv->wq, &gpu->recover_work);
343 }
344
345 /* if still more pending work, reset the hangcheck timer: */
Rob Clarkca762a82016-03-15 17:22:13 -0400346 if (gpu->fctx->last_fence > gpu->hangcheck_fence)
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400347 hangcheck_timer_reset(gpu);
Rob Clark6b8819c2013-09-11 17:14:30 -0400348
349 /* workaround for missing irq: */
350 queue_work(priv->wq, &gpu->retire_work);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400351}
352
353/*
Rob Clark70c70f02014-05-30 14:49:43 -0400354 * Performance Counters:
355 */
356
357/* called under perf_lock */
358static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
359{
360 uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
361 int i, n = min(ncntrs, gpu->num_perfcntrs);
362
363 /* read current values: */
364 for (i = 0; i < gpu->num_perfcntrs; i++)
365 current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
366
367 /* update cntrs: */
368 for (i = 0; i < n; i++)
369 cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
370
371 /* save current values: */
372 for (i = 0; i < gpu->num_perfcntrs; i++)
373 gpu->last_cntrs[i] = current_cntrs[i];
374
375 return n;
376}
377
378static void update_sw_cntrs(struct msm_gpu *gpu)
379{
380 ktime_t time;
381 uint32_t elapsed;
382 unsigned long flags;
383
384 spin_lock_irqsave(&gpu->perf_lock, flags);
385 if (!gpu->perfcntr_active)
386 goto out;
387
388 time = ktime_get();
389 elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
390
391 gpu->totaltime += elapsed;
392 if (gpu->last_sample.active)
393 gpu->activetime += elapsed;
394
395 gpu->last_sample.active = msm_gpu_active(gpu);
396 gpu->last_sample.time = time;
397
398out:
399 spin_unlock_irqrestore(&gpu->perf_lock, flags);
400}
401
402void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
403{
404 unsigned long flags;
405
406 spin_lock_irqsave(&gpu->perf_lock, flags);
407 /* we could dynamically enable/disable perfcntr registers too.. */
408 gpu->last_sample.active = msm_gpu_active(gpu);
409 gpu->last_sample.time = ktime_get();
410 gpu->activetime = gpu->totaltime = 0;
411 gpu->perfcntr_active = true;
412 update_hw_cntrs(gpu, 0, NULL);
413 spin_unlock_irqrestore(&gpu->perf_lock, flags);
414}
415
416void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
417{
418 gpu->perfcntr_active = false;
419}
420
421/* returns -errno or # of cntrs sampled */
422int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
423 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
424{
425 unsigned long flags;
426 int ret;
427
428 spin_lock_irqsave(&gpu->perf_lock, flags);
429
430 if (!gpu->perfcntr_active) {
431 ret = -EINVAL;
432 goto out;
433 }
434
435 *activetime = gpu->activetime;
436 *totaltime = gpu->totaltime;
437
438 gpu->activetime = gpu->totaltime = 0;
439
440 ret = update_hw_cntrs(gpu, ncntrs, cntrs);
441
442out:
443 spin_unlock_irqrestore(&gpu->perf_lock, flags);
444
445 return ret;
446}
447
448/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400449 * Cmdstream submission/retirement:
450 */
451
Rob Clark7d12a272016-03-16 16:07:38 -0400452static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
453{
454 int i;
455
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);
Jordan Croused8e96522017-02-13 10:14:16 -0700460 msm_gem_put_iova(&msm_obj->base, gpu->aspace);
Rob Clark7d12a272016-03-16 16:07:38 -0400461 drm_gem_object_unreference(&msm_obj->base);
462 }
463
Rob Clark40e68152016-05-03 09:50:26 -0400464 msm_gem_submit_free(submit);
Rob Clark7d12a272016-03-16 16:07:38 -0400465}
466
Rob Clarkb6295f92016-03-15 18:26:28 -0400467static void retire_submits(struct msm_gpu *gpu)
Rob Clark1a370be2015-06-07 13:46:04 -0400468{
469 struct drm_device *dev = gpu->dev;
470
471 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
472
473 while (!list_empty(&gpu->submit_list)) {
474 struct msm_gem_submit *submit;
475
476 submit = list_first_entry(&gpu->submit_list,
477 struct msm_gem_submit, node);
478
Rob Clarkb6295f92016-03-15 18:26:28 -0400479 if (fence_is_signaled(submit->fence)) {
Rob Clark7d12a272016-03-16 16:07:38 -0400480 retire_submit(gpu, submit);
Rob Clark1a370be2015-06-07 13:46:04 -0400481 } else {
482 break;
483 }
484 }
485}
486
Rob Clark7198e6b2013-07-19 12:59:32 -0400487static void retire_worker(struct work_struct *work)
488{
489 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
490 struct drm_device *dev = gpu->dev;
491 uint32_t fence = gpu->funcs->last_fence(gpu);
492
Rob Clarkca762a82016-03-15 17:22:13 -0400493 msm_update_fence(gpu->fctx, fence);
Rob Clarkedd4fc62013-09-14 14:01:55 -0400494
Rob Clark7198e6b2013-07-19 12:59:32 -0400495 mutex_lock(&dev->struct_mutex);
Rob Clarkb6295f92016-03-15 18:26:28 -0400496 retire_submits(gpu);
Jordan Croused8e96522017-02-13 10:14:16 -0700497
498 retire_submits(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400499 mutex_unlock(&dev->struct_mutex);
Rob Clark37d77c32014-01-11 16:25:08 -0500500
501 if (!msm_gpu_active(gpu))
502 inactive_start(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400503}
504
505/* call from irq handler to schedule work to retire bo's */
506void msm_gpu_retire(struct msm_gpu *gpu)
507{
508 struct msm_drm_private *priv = gpu->dev->dev_private;
509 queue_work(priv->wq, &gpu->retire_work);
Rob Clark70c70f02014-05-30 14:49:43 -0400510 update_sw_cntrs(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400511}
512
513/* add bo's to gpu's ring, and kick gpu: */
Rob Clarkf44d32c2016-06-16 16:37:38 -0400514void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
Rob Clark7198e6b2013-07-19 12:59:32 -0400515 struct msm_file_private *ctx)
516{
517 struct drm_device *dev = gpu->dev;
518 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkf44d32c2016-06-16 16:37:38 -0400519 int i;
Rob Clark7198e6b2013-07-19 12:59:32 -0400520
Rob Clark1a370be2015-06-07 13:46:04 -0400521 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
522
Rob Clark37d77c32014-01-11 16:25:08 -0500523 inactive_cancel(gpu);
524
Rob Clark1a370be2015-06-07 13:46:04 -0400525 list_add_tail(&submit->node, &gpu->submit_list);
526
Rob Clarka7d3c952014-05-30 14:47:38 -0400527 msm_rd_dump_submit(submit);
528
Rob Clark70c70f02014-05-30 14:49:43 -0400529 update_sw_cntrs(gpu);
530
Rob Clark7198e6b2013-07-19 12:59:32 -0400531 for (i = 0; i < submit->nr_bos; i++) {
532 struct msm_gem_object *msm_obj = submit->bos[i].obj;
Rob Clark7d12a272016-03-16 16:07:38 -0400533 uint32_t iova;
Rob Clark7198e6b2013-07-19 12:59:32 -0400534
535 /* can't happen yet.. but when we add 2d support we'll have
536 * to deal w/ cross-ring synchronization:
537 */
538 WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
539
Rob Clark7d12a272016-03-16 16:07:38 -0400540 /* submit takes a reference to the bo and iova until retired: */
541 drm_gem_object_reference(&msm_obj->base);
542 msm_gem_get_iova_locked(&msm_obj->base,
Jordan Croused8e96522017-02-13 10:14:16 -0700543 submit->gpu->aspace, &iova);
Rob Clarkbf6811f2013-09-01 13:25:09 -0400544 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
545 msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
Rob Clarkb6295f92016-03-15 18:26:28 -0400546 else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
547 msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
Rob Clark7198e6b2013-07-19 12:59:32 -0400548 }
Rob Clark1a370be2015-06-07 13:46:04 -0400549
Rob Clark1193c3b2016-05-03 09:46:49 -0400550 gpu->funcs->submit(gpu, submit, ctx);
Rob Clark1a370be2015-06-07 13:46:04 -0400551 priv->lastctx = ctx;
552
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400553 hangcheck_timer_reset(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400554}
555
556/*
557 * Init/Cleanup:
558 */
559
560static irqreturn_t irq_handler(int irq, void *data)
561{
562 struct msm_gpu *gpu = data;
563 return gpu->funcs->irq(gpu);
564}
565
566static const char *clk_names[] = {
567 "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
Rob Clarkde558cd2015-05-06 13:14:30 -0400568 "alt_mem_iface_clk",
Rob Clark7198e6b2013-07-19 12:59:32 -0400569};
570
571int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
572 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
573 const char *name, const char *ioname, const char *irqname, int ringsz)
574{
Rob Clark871d8122013-11-16 12:56:06 -0500575 struct iommu_domain *iommu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400576 int i, ret;
577
Rob Clark70c70f02014-05-30 14:49:43 -0400578 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
579 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
580
Rob Clark7198e6b2013-07-19 12:59:32 -0400581 gpu->dev = drm;
582 gpu->funcs = funcs;
583 gpu->name = name;
Rob Clark37d77c32014-01-11 16:25:08 -0500584 gpu->inactive = true;
Rob Clarkca762a82016-03-15 17:22:13 -0400585 gpu->fctx = msm_fence_context_alloc(drm, name);
586 if (IS_ERR(gpu->fctx)) {
587 ret = PTR_ERR(gpu->fctx);
588 gpu->fctx = NULL;
589 goto fail;
590 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400591
592 INIT_LIST_HEAD(&gpu->active_list);
593 INIT_WORK(&gpu->retire_work, retire_worker);
Rob Clark37d77c32014-01-11 16:25:08 -0500594 INIT_WORK(&gpu->inactive_work, inactive_worker);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400595 INIT_WORK(&gpu->recover_work, recover_worker);
596
Rob Clark1a370be2015-06-07 13:46:04 -0400597 INIT_LIST_HEAD(&gpu->submit_list);
598
Rob Clark37d77c32014-01-11 16:25:08 -0500599 setup_timer(&gpu->inactive_timer, inactive_handler,
600 (unsigned long)gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400601 setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
602 (unsigned long)gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400603
Rob Clark70c70f02014-05-30 14:49:43 -0400604 spin_lock_init(&gpu->perf_lock);
605
Rob Clark7198e6b2013-07-19 12:59:32 -0400606 BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
607
608 /* Map registers: */
609 gpu->mmio = msm_ioremap(pdev, ioname, name);
610 if (IS_ERR(gpu->mmio)) {
611 ret = PTR_ERR(gpu->mmio);
612 goto fail;
613 }
614
615 /* Get Interrupt: */
616 gpu->irq = platform_get_irq_byname(pdev, irqname);
617 if (gpu->irq < 0) {
618 ret = gpu->irq;
619 dev_err(drm->dev, "failed to get irq: %d\n", ret);
620 goto fail;
621 }
622
623 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
624 IRQF_TRIGGER_HIGH, gpu->name, gpu);
625 if (ret) {
626 dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
627 goto fail;
628 }
629
630 /* Acquire clocks: */
631 for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
632 gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
633 DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
634 if (IS_ERR(gpu->grp_clks[i]))
635 gpu->grp_clks[i] = NULL;
636 }
637
638 gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
639 DBG("ebi1_clk: %p", gpu->ebi1_clk);
640 if (IS_ERR(gpu->ebi1_clk))
641 gpu->ebi1_clk = NULL;
642
643 /* Acquire regulators: */
644 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
645 DBG("gpu_reg: %p", gpu->gpu_reg);
646 if (IS_ERR(gpu->gpu_reg))
647 gpu->gpu_reg = NULL;
648
649 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
650 DBG("gpu_cx: %p", gpu->gpu_cx);
651 if (IS_ERR(gpu->gpu_cx))
652 gpu->gpu_cx = NULL;
653
654 /* Setup IOMMU.. eventually we will (I think) do this once per context
655 * and have separate page tables per context. For now, to keep things
656 * simple and to get something working, just use a single address space:
657 */
Rob Clark871d8122013-11-16 12:56:06 -0500658 iommu = iommu_domain_alloc(&platform_bus_type);
659 if (iommu) {
Rob Clarke22a2fb2017-02-13 10:14:11 -0700660 /* TODO 32b vs 64b address space.. */
661 iommu->geometry.aperture_start = 0x1000;
662 iommu->geometry.aperture_end = 0xffffffff;
663
Rob Clark871d8122013-11-16 12:56:06 -0500664 dev_info(drm->dev, "%s: using IOMMU\n", name);
Rob Clarke22a2fb2017-02-13 10:14:11 -0700665 gpu->aspace = msm_gem_address_space_create(&pdev->dev,
666 iommu, "gpu");
667 if (IS_ERR(gpu->aspace)) {
668 ret = PTR_ERR(gpu->aspace);
Stephane Viau5e921b12015-09-15 08:41:46 -0400669 dev_err(drm->dev, "failed to init iommu: %d\n", ret);
Rob Clarke22a2fb2017-02-13 10:14:11 -0700670 gpu->aspace = NULL;
Stephane Viau5e921b12015-09-15 08:41:46 -0400671 iommu_domain_free(iommu);
672 goto fail;
673 }
674
Rob Clark871d8122013-11-16 12:56:06 -0500675 } else {
676 dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
Rob Clark7198e6b2013-07-19 12:59:32 -0400677 }
Rob Clarka1ad3522014-07-11 11:59:22 -0400678
Rob Clark7198e6b2013-07-19 12:59:32 -0400679 /* Create ringbuffer: */
Rob Clarka1ad3522014-07-11 11:59:22 -0400680 mutex_lock(&drm->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400681 gpu->rb = msm_ringbuffer_new(gpu, ringsz);
Rob Clarka1ad3522014-07-11 11:59:22 -0400682 mutex_unlock(&drm->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400683 if (IS_ERR(gpu->rb)) {
684 ret = PTR_ERR(gpu->rb);
685 gpu->rb = NULL;
686 dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
687 goto fail;
688 }
689
Rob Clarkbf2b33a2013-11-15 09:03:15 -0500690 bs_init(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400691
692 return 0;
693
694fail:
695 return ret;
696}
697
698void msm_gpu_cleanup(struct msm_gpu *gpu)
699{
700 DBG("%s", gpu->name);
701
702 WARN_ON(!list_empty(&gpu->active_list));
703
704 bs_fini(gpu);
705
706 if (gpu->rb) {
707 if (gpu->rb_iova)
Jordan Croused8e96522017-02-13 10:14:16 -0700708 msm_gem_put_iova(gpu->rb->bo, gpu->aspace);
Rob Clark7198e6b2013-07-19 12:59:32 -0400709 msm_ringbuffer_destroy(gpu->rb);
710 }
711
Rob Clarkca762a82016-03-15 17:22:13 -0400712 if (gpu->fctx)
713 msm_fence_context_free(gpu->fctx);
Rob Clarke22a2fb2017-02-13 10:14:11 -0700714
715 if (gpu->aspace)
716 msm_gem_address_space_destroy(gpu->aspace);
Rob Clark7198e6b2013-07-19 12:59:32 -0400717}