blob: 0f78c26152722173251cce26f68222501dad944a [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clark7198e6b2013-07-19 12:59:32 -04002/*
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
Rob Clark7198e6b2013-07-19 12:59:32 -04005 */
6
7#include "msm_gpu.h"
8#include "msm_gem.h"
Rob Clark871d8122013-11-16 12:56:06 -05009#include "msm_mmu.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040010#include "msm_fence.h"
Jordan Crouse4241db42018-11-02 09:25:21 -060011#include "msm_gpu_trace.h"
Jonathan Marekc2052a42018-11-14 17:08:04 -050012#include "adreno/adreno_gpu.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040013
Jordan Crousec0fec7f2018-07-24 10:33:27 -060014#include <generated/utsrelease.h>
Rob Clark18bb8a62017-09-13 10:17:18 -040015#include <linux/string_helpers.h>
Jordan Crousec0fec7f2018-07-24 10:33:27 -060016#include <linux/devcoredump.h>
Arnd Bergmann70082a52019-09-18 21:57:07 +020017#include <linux/sched/task.h>
Rob Clark7198e6b2013-07-19 12:59:32 -040018
19/*
20 * Power Management:
21 */
22
Rob Clark7198e6b2013-07-19 12:59:32 -040023static int enable_pwrrail(struct msm_gpu *gpu)
24{
25 struct drm_device *dev = gpu->dev;
26 int ret = 0;
27
28 if (gpu->gpu_reg) {
29 ret = regulator_enable(gpu->gpu_reg);
30 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +053031 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
Rob Clark7198e6b2013-07-19 12:59:32 -040032 return ret;
33 }
34 }
35
36 if (gpu->gpu_cx) {
37 ret = regulator_enable(gpu->gpu_cx);
38 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +053039 DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
Rob Clark7198e6b2013-07-19 12:59:32 -040040 return ret;
41 }
42 }
43
44 return 0;
45}
46
47static int disable_pwrrail(struct msm_gpu *gpu)
48{
49 if (gpu->gpu_cx)
50 regulator_disable(gpu->gpu_cx);
51 if (gpu->gpu_reg)
52 regulator_disable(gpu->gpu_reg);
53 return 0;
54}
55
56static int enable_clk(struct msm_gpu *gpu)
57{
Jordan Crouse98db8032017-03-07 10:02:56 -070058 if (gpu->core_clk && gpu->fast_rate)
59 clk_set_rate(gpu->core_clk, gpu->fast_rate);
Jordan Crouse89d777a2016-11-28 12:28:31 -070060
Jordan Crouseb5f103a2016-11-28 12:28:33 -070061 /* Set the RBBM timer rate to 19.2Mhz */
Jordan Crouse98db8032017-03-07 10:02:56 -070062 if (gpu->rbbmtimer_clk)
63 clk_set_rate(gpu->rbbmtimer_clk, 19200000);
Jordan Crouseb5f103a2016-11-28 12:28:33 -070064
Jordan Crouse8e54eea2018-08-06 11:33:21 -060065 return clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
Rob Clark7198e6b2013-07-19 12:59:32 -040066}
67
68static int disable_clk(struct msm_gpu *gpu)
69{
Jordan Crouse8e54eea2018-08-06 11:33:21 -060070 clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
Rob Clark7198e6b2013-07-19 12:59:32 -040071
Jordan Crousebf5af4a2017-03-07 10:02:54 -070072 /*
73 * Set the clock to a deliberately low rate. On older targets the clock
74 * speed had to be non zero to avoid problems. On newer targets this
75 * will be rounded down to zero anyway so it all works out.
76 */
Jordan Crouse98db8032017-03-07 10:02:56 -070077 if (gpu->core_clk)
78 clk_set_rate(gpu->core_clk, 27000000);
Jordan Crouse89d777a2016-11-28 12:28:31 -070079
Jordan Crouse98db8032017-03-07 10:02:56 -070080 if (gpu->rbbmtimer_clk)
81 clk_set_rate(gpu->rbbmtimer_clk, 0);
Jordan Crouseb5f103a2016-11-28 12:28:33 -070082
Rob Clark7198e6b2013-07-19 12:59:32 -040083 return 0;
84}
85
86static int enable_axi(struct msm_gpu *gpu)
87{
Tian Taodd29bd42020-10-19 14:04:22 +080088 return clk_prepare_enable(gpu->ebi1_clk);
Rob Clark7198e6b2013-07-19 12:59:32 -040089}
90
91static int disable_axi(struct msm_gpu *gpu)
92{
Tian Taodd29bd42020-10-19 14:04:22 +080093 clk_disable_unprepare(gpu->ebi1_clk);
Rob Clark7198e6b2013-07-19 12:59:32 -040094 return 0;
95}
96
97int msm_gpu_pm_resume(struct msm_gpu *gpu)
98{
99 int ret;
100
Rob Clarkeeb75472017-02-10 15:36:33 -0500101 DBG("%s", gpu->name);
Rob Clarkec1cb6e2020-09-01 08:41:56 -0700102 trace_msm_gpu_resume(0);
Rob Clark7198e6b2013-07-19 12:59:32 -0400103
104 ret = enable_pwrrail(gpu);
105 if (ret)
106 return ret;
107
108 ret = enable_clk(gpu);
109 if (ret)
110 return ret;
111
112 ret = enable_axi(gpu);
113 if (ret)
114 return ret;
115
Rob Clarkaf5b4ff2021-07-26 07:46:48 -0700116 msm_devfreq_resume(gpu);
Jordan Crousef91c14a2018-01-10 10:41:54 -0700117
Rob Clarkeeb75472017-02-10 15:36:33 -0500118 gpu->needs_hw_init = true;
119
Rob Clark7198e6b2013-07-19 12:59:32 -0400120 return 0;
121}
122
123int msm_gpu_pm_suspend(struct msm_gpu *gpu)
124{
125 int ret;
126
Rob Clarkeeb75472017-02-10 15:36:33 -0500127 DBG("%s", gpu->name);
Rob Clarkec1cb6e2020-09-01 08:41:56 -0700128 trace_msm_gpu_suspend(0);
Rob Clark7198e6b2013-07-19 12:59:32 -0400129
Rob Clarkaf5b4ff2021-07-26 07:46:48 -0700130 msm_devfreq_suspend(gpu);
Jordan Crousef91c14a2018-01-10 10:41:54 -0700131
Rob Clark7198e6b2013-07-19 12:59:32 -0400132 ret = disable_axi(gpu);
133 if (ret)
134 return ret;
135
136 ret = disable_clk(gpu);
137 if (ret)
138 return ret;
139
140 ret = disable_pwrrail(gpu);
141 if (ret)
142 return ret;
143
Rob Clark3ab1c5c2021-03-24 18:23:53 -0700144 gpu->suspend_count++;
145
Rob Clark7198e6b2013-07-19 12:59:32 -0400146 return 0;
147}
148
Rob Clarkeeb75472017-02-10 15:36:33 -0500149int msm_gpu_hw_init(struct msm_gpu *gpu)
Rob Clark37d77c32014-01-11 16:25:08 -0500150{
Rob Clarkeeb75472017-02-10 15:36:33 -0500151 int ret;
Rob Clark37d77c32014-01-11 16:25:08 -0500152
Rob Clarkc28e2f22021-11-09 10:11:03 -0800153 WARN_ON(!mutex_is_locked(&gpu->lock));
Rob Clarkcb1e3812017-06-13 09:15:36 -0400154
Rob Clarkeeb75472017-02-10 15:36:33 -0500155 if (!gpu->needs_hw_init)
156 return 0;
Rob Clark37d77c32014-01-11 16:25:08 -0500157
Rob Clarkeeb75472017-02-10 15:36:33 -0500158 disable_irq(gpu->irq);
159 ret = gpu->funcs->hw_init(gpu);
160 if (!ret)
161 gpu->needs_hw_init = false;
162 enable_irq(gpu->irq);
Rob Clark37d77c32014-01-11 16:25:08 -0500163
Rob Clarkeeb75472017-02-10 15:36:33 -0500164 return ret;
Rob Clark37d77c32014-01-11 16:25:08 -0500165}
166
Rob Clark2a86efb2020-10-23 09:51:13 -0700167static void update_fences(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
168 uint32_t fence)
169{
170 struct msm_gem_submit *submit;
Rob Clark298287f2021-07-26 07:43:58 -0700171 unsigned long flags;
Rob Clark2a86efb2020-10-23 09:51:13 -0700172
Rob Clark298287f2021-07-26 07:43:58 -0700173 spin_lock_irqsave(&ring->submit_lock, flags);
Rob Clark2a86efb2020-10-23 09:51:13 -0700174 list_for_each_entry(submit, &ring->submits, node) {
Rob Clark5f3aee42021-11-09 10:11:04 -0800175 if (fence_after(submit->seqno, fence))
Rob Clark2a86efb2020-10-23 09:51:13 -0700176 break;
177
178 msm_update_fence(submit->ring->fctx,
Rob Clark1d8a5ca2021-07-27 18:06:14 -0700179 submit->hw_fence->seqno);
180 dma_fence_signal(submit->hw_fence);
Rob Clark2a86efb2020-10-23 09:51:13 -0700181 }
Rob Clark298287f2021-07-26 07:43:58 -0700182 spin_unlock_irqrestore(&ring->submit_lock, flags);
Rob Clark2a86efb2020-10-23 09:51:13 -0700183}
184
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600185#ifdef CONFIG_DEV_COREDUMP
186static ssize_t msm_gpu_devcoredump_read(char *buffer, loff_t offset,
187 size_t count, void *data, size_t datalen)
188{
189 struct msm_gpu *gpu = data;
190 struct drm_print_iterator iter;
191 struct drm_printer p;
192 struct msm_gpu_state *state;
193
194 state = msm_gpu_crashstate_get(gpu);
195 if (!state)
196 return 0;
197
198 iter.data = buffer;
199 iter.offset = 0;
200 iter.start = offset;
201 iter.remain = count;
202
203 p = drm_coredump_printer(&iter);
204
205 drm_printf(&p, "---\n");
206 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
207 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
Arnd Bergmann3530a172018-07-26 14:39:25 +0200208 drm_printf(&p, "time: %lld.%09ld\n",
209 state->time.tv_sec, state->time.tv_nsec);
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600210 if (state->comm)
211 drm_printf(&p, "comm: %s\n", state->comm);
212 if (state->cmd)
213 drm_printf(&p, "cmdline: %s\n", state->cmd);
214
215 gpu->funcs->show(gpu, state, &p);
216
217 msm_gpu_crashstate_put(gpu);
218
219 return count - iter.remain;
220}
221
222static void msm_gpu_devcoredump_free(void *data)
223{
224 struct msm_gpu *gpu = data;
225
226 msm_gpu_crashstate_put(gpu);
227}
228
Jordan Crousecdb95932018-07-24 10:33:31 -0600229static void msm_gpu_crashstate_get_bo(struct msm_gpu_state *state,
230 struct msm_gem_object *obj, u64 iova, u32 flags)
231{
232 struct msm_gpu_state_bo *state_bo = &state->bos[state->nr_bos];
233
234 /* Don't record write only objects */
Jordan Crousecdb95932018-07-24 10:33:31 -0600235 state_bo->size = obj->base.size;
236 state_bo->iova = iova;
237
Jordan Crouse896a2482018-11-02 09:25:22 -0600238 /* Only store data for non imported buffer objects marked for read */
239 if ((flags & MSM_SUBMIT_BO_READ) && !obj->base.import_attach) {
Jordan Crousecdb95932018-07-24 10:33:31 -0600240 void *ptr;
241
242 state_bo->data = kvmalloc(obj->base.size, GFP_KERNEL);
243 if (!state_bo->data)
Jordan Crouse896a2482018-11-02 09:25:22 -0600244 goto out;
Jordan Crousecdb95932018-07-24 10:33:31 -0600245
Rob Clark6c0e3ea2020-10-23 09:51:10 -0700246 msm_gem_lock(&obj->base);
Jordan Crousecdb95932018-07-24 10:33:31 -0600247 ptr = msm_gem_get_vaddr_active(&obj->base);
Rob Clark6c0e3ea2020-10-23 09:51:10 -0700248 msm_gem_unlock(&obj->base);
Jordan Crousecdb95932018-07-24 10:33:31 -0600249 if (IS_ERR(ptr)) {
250 kvfree(state_bo->data);
Jordan Crouse896a2482018-11-02 09:25:22 -0600251 state_bo->data = NULL;
252 goto out;
Jordan Crousecdb95932018-07-24 10:33:31 -0600253 }
254
255 memcpy(state_bo->data, ptr, obj->base.size);
256 msm_gem_put_vaddr(&obj->base);
257 }
Jordan Crouse896a2482018-11-02 09:25:22 -0600258out:
Jordan Crousecdb95932018-07-24 10:33:31 -0600259 state->nr_bos++;
260}
261
262static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
263 struct msm_gem_submit *submit, char *comm, char *cmd)
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600264{
265 struct msm_gpu_state *state;
266
Sharat Masetty4f3a31a2018-10-12 14:26:55 +0530267 /* Check if the target supports capturing crash state */
268 if (!gpu->funcs->gpu_state_get)
269 return;
270
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600271 /* Only save one crash state at a time */
272 if (gpu->crashstate)
273 return;
274
275 state = gpu->funcs->gpu_state_get(gpu);
276 if (IS_ERR_OR_NULL(state))
277 return;
278
279 /* Fill in the additional crash state information */
280 state->comm = kstrdup(comm, GFP_KERNEL);
281 state->cmd = kstrdup(cmd, GFP_KERNEL);
Rob Clarke25e92e2021-06-10 14:44:13 -0700282 state->fault_info = gpu->fault_info;
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600283
Jordan Crousecdb95932018-07-24 10:33:31 -0600284 if (submit) {
Rob Clarke515af82020-02-18 13:20:12 -0800285 int i, nr = 0;
Jordan Crousecdb95932018-07-24 10:33:31 -0600286
Rob Clarke515af82020-02-18 13:20:12 -0800287 /* count # of buffers to dump: */
288 for (i = 0; i < submit->nr_bos; i++)
289 if (should_dump(submit, i))
290 nr++;
291 /* always dump cmd bo's, but don't double count them: */
292 for (i = 0; i < submit->nr_cmds; i++)
293 if (!should_dump(submit, submit->cmd[i].idx))
294 nr++;
295
296 state->bos = kcalloc(nr,
Jordan Crousecdb95932018-07-24 10:33:31 -0600297 sizeof(struct msm_gpu_state_bo), GFP_KERNEL);
298
Tim Gardnerb220c152021-09-29 10:25:54 -0600299 for (i = 0; state->bos && i < submit->nr_bos; i++) {
Rob Clarke515af82020-02-18 13:20:12 -0800300 if (should_dump(submit, i)) {
301 msm_gpu_crashstate_get_bo(state, submit->bos[i].obj,
302 submit->bos[i].iova, submit->bos[i].flags);
303 }
304 }
305
Jordan Crouse896a2482018-11-02 09:25:22 -0600306 for (i = 0; state->bos && i < submit->nr_cmds; i++) {
307 int idx = submit->cmd[i].idx;
308
Rob Clarke515af82020-02-18 13:20:12 -0800309 if (!should_dump(submit, submit->cmd[i].idx)) {
310 msm_gpu_crashstate_get_bo(state, submit->bos[idx].obj,
311 submit->bos[idx].iova, submit->bos[idx].flags);
312 }
Jordan Crouse896a2482018-11-02 09:25:22 -0600313 }
Jordan Crousecdb95932018-07-24 10:33:31 -0600314 }
315
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600316 /* Set the active crash state to be dumped on failure */
317 gpu->crashstate = state;
318
319 /* FIXME: Release the crashstate if this errors out? */
320 dev_coredumpm(gpu->dev->dev, THIS_MODULE, gpu, 0, GFP_KERNEL,
321 msm_gpu_devcoredump_read, msm_gpu_devcoredump_free);
322}
323#else
Anders Roxell69690192018-07-31 22:45:32 +0200324static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
325 struct msm_gem_submit *submit, char *comm, char *cmd)
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600326{
327}
328#endif
329
Rob Clark37d77c32014-01-11 16:25:08 -0500330/*
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400331 * Hangcheck detection for locked gpu:
332 */
333
Rob Clark18bb8a62017-09-13 10:17:18 -0400334static struct msm_gem_submit *
335find_submit(struct msm_ringbuffer *ring, uint32_t fence)
336{
337 struct msm_gem_submit *submit;
Rob Clark298287f2021-07-26 07:43:58 -0700338 unsigned long flags;
Rob Clark18bb8a62017-09-13 10:17:18 -0400339
Rob Clark298287f2021-07-26 07:43:58 -0700340 spin_lock_irqsave(&ring->submit_lock, flags);
Rob Clark77d20522020-10-23 09:51:16 -0700341 list_for_each_entry(submit, &ring->submits, node) {
342 if (submit->seqno == fence) {
Rob Clark298287f2021-07-26 07:43:58 -0700343 spin_unlock_irqrestore(&ring->submit_lock, flags);
Rob Clark18bb8a62017-09-13 10:17:18 -0400344 return submit;
Rob Clark77d20522020-10-23 09:51:16 -0700345 }
346 }
Rob Clark298287f2021-07-26 07:43:58 -0700347 spin_unlock_irqrestore(&ring->submit_lock, flags);
Rob Clark18bb8a62017-09-13 10:17:18 -0400348
349 return NULL;
350}
351
Rob Clarkb6295f92016-03-15 18:26:28 -0400352static void retire_submits(struct msm_gpu *gpu);
Rob Clark1a370be2015-06-07 13:46:04 -0400353
Rob Clark7e688292020-10-19 14:10:51 -0700354static void recover_worker(struct kthread_work *work)
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400355{
356 struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
357 struct drm_device *dev = gpu->dev;
Rob Clark96169f42017-09-15 11:04:44 -0400358 struct msm_drm_private *priv = dev->dev_private;
Rob Clark4816b622016-05-03 10:10:15 -0400359 struct msm_gem_submit *submit;
Jordan Crousef97deca2017-10-20 11:06:57 -0600360 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
Jordan Crouse65a3c272018-07-24 10:33:26 -0600361 char *comm = NULL, *cmd = NULL;
Jordan Crousef97deca2017-10-20 11:06:57 -0600362 int i;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400363
Rob Clarkc28e2f22021-11-09 10:11:03 -0800364 mutex_lock(&gpu->lock);
Rob Clark1a370be2015-06-07 13:46:04 -0400365
Mamta Shukla6a41da12018-10-20 23:19:26 +0530366 DRM_DEV_ERROR(dev->dev, "%s: hangcheck recover!\n", gpu->name);
Jordan Crousef97deca2017-10-20 11:06:57 -0600367
Rob Clark96169f42017-09-15 11:04:44 -0400368 submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1);
Rob Clark18bb8a62017-09-13 10:17:18 -0400369 if (submit) {
370 struct task_struct *task;
Rob Clark4816b622016-05-03 10:10:15 -0400371
Jordan Crouseb0fb6602019-03-22 14:21:22 -0600372 /* Increment the fault counts */
Rob Clark48dc4242019-04-16 16:13:28 -0700373 gpu->global_faults++;
Jordan Crouseb0fb6602019-03-22 14:21:22 -0600374 submit->queue->faults++;
Rob Clark48dc4242019-04-16 16:13:28 -0700375
Sharat Masetty482f9632018-10-12 14:26:56 +0530376 task = get_pid_task(submit->pid, PIDTYPE_PID);
Rob Clark18bb8a62017-09-13 10:17:18 -0400377 if (task) {
Sharat Masetty482f9632018-10-12 14:26:56 +0530378 comm = kstrdup(task->comm, GFP_KERNEL);
Sharat Masetty482f9632018-10-12 14:26:56 +0530379 cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL);
380 put_task_struct(task);
Rob Clark4816b622016-05-03 10:10:15 -0400381 }
Jordan Crouse65a3c272018-07-24 10:33:26 -0600382
383 if (comm && cmd) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530384 DRM_DEV_ERROR(dev->dev, "%s: offending task: %s (%s)\n",
Jordan Crouse65a3c272018-07-24 10:33:26 -0600385 gpu->name, comm, cmd);
386
387 msm_rd_dump_submit(priv->hangrd, submit,
388 "offending task: %s (%s)", comm, cmd);
Rob Clark6c0e3ea2020-10-23 09:51:10 -0700389 } else {
Jordan Crouse65a3c272018-07-24 10:33:26 -0600390 msm_rd_dump_submit(priv->hangrd, submit, NULL);
Rob Clark6c0e3ea2020-10-23 09:51:10 -0700391 }
Rob Clark96169f42017-09-15 11:04:44 -0400392 }
Rob Clark18bb8a62017-09-13 10:17:18 -0400393
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600394 /* Record the crash state */
395 pm_runtime_get_sync(&gpu->pdev->dev);
Jordan Crousecdb95932018-07-24 10:33:31 -0600396 msm_gpu_crashstate_capture(gpu, submit, comm, cmd);
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600397 pm_runtime_put_sync(&gpu->pdev->dev);
398
Jordan Crouse65a3c272018-07-24 10:33:26 -0600399 kfree(cmd);
400 kfree(comm);
Rob Clark96169f42017-09-15 11:04:44 -0400401
402 /*
403 * Update all the rings with the latest and greatest fence.. this
404 * needs to happen after msm_rd_dump_submit() to ensure that the
405 * bo's referenced by the offending submit are still around.
406 */
Jordan Crouse7ddae822017-12-13 13:45:44 -0700407 for (i = 0; i < gpu->nr_rings; i++) {
Rob Clark96169f42017-09-15 11:04:44 -0400408 struct msm_ringbuffer *ring = gpu->rb[i];
409
410 uint32_t fence = ring->memptrs->fence;
411
412 /*
413 * For the current (faulting?) ring/submit advance the fence by
414 * one more to clear the faulting submit
415 */
416 if (ring == cur_ring)
417 fence++;
418
419 update_fences(gpu, ring, fence);
Rob Clark4816b622016-05-03 10:10:15 -0400420 }
421
422 if (msm_gpu_active(gpu)) {
Rob Clark1a370be2015-06-07 13:46:04 -0400423 /* retire completed submits, plus the one that hung: */
Rob Clarkb6295f92016-03-15 18:26:28 -0400424 retire_submits(gpu);
Rob Clark1a370be2015-06-07 13:46:04 -0400425
Rob Clarkeeb75472017-02-10 15:36:33 -0500426 pm_runtime_get_sync(&gpu->pdev->dev);
Rob Clark37d77c32014-01-11 16:25:08 -0500427 gpu->funcs->recover(gpu);
Rob Clarkeeb75472017-02-10 15:36:33 -0500428 pm_runtime_put_sync(&gpu->pdev->dev);
Rob Clark1a370be2015-06-07 13:46:04 -0400429
Jordan Crousef97deca2017-10-20 11:06:57 -0600430 /*
431 * Replay all remaining submits starting with highest priority
432 * ring
433 */
Jordan Crouseb1fc2832017-10-20 11:07:01 -0600434 for (i = 0; i < gpu->nr_rings; i++) {
Jordan Crousef97deca2017-10-20 11:06:57 -0600435 struct msm_ringbuffer *ring = gpu->rb[i];
Rob Clark298287f2021-07-26 07:43:58 -0700436 unsigned long flags;
Jordan Crousef97deca2017-10-20 11:06:57 -0600437
Rob Clark298287f2021-07-26 07:43:58 -0700438 spin_lock_irqsave(&ring->submit_lock, flags);
Jordan Crousef97deca2017-10-20 11:06:57 -0600439 list_for_each_entry(submit, &ring->submits, node)
Jordan Crouse15eb9ad2020-08-17 15:01:37 -0700440 gpu->funcs->submit(gpu, submit);
Rob Clark298287f2021-07-26 07:43:58 -0700441 spin_unlock_irqrestore(&ring->submit_lock, flags);
Rob Clark1a370be2015-06-07 13:46:04 -0400442 }
Rob Clark37d77c32014-01-11 16:25:08 -0500443 }
Rob Clark4816b622016-05-03 10:10:15 -0400444
Rob Clarkc28e2f22021-11-09 10:11:03 -0800445 mutex_unlock(&gpu->lock);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400446
447 msm_gpu_retire(gpu);
448}
449
Rob Clarke25e92e2021-06-10 14:44:13 -0700450static void fault_worker(struct kthread_work *work)
451{
452 struct msm_gpu *gpu = container_of(work, struct msm_gpu, fault_work);
Rob Clarke25e92e2021-06-10 14:44:13 -0700453 struct msm_gem_submit *submit;
454 struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
455 char *comm = NULL, *cmd = NULL;
456
Rob Clarkc28e2f22021-11-09 10:11:03 -0800457 mutex_lock(&gpu->lock);
Rob Clarke25e92e2021-06-10 14:44:13 -0700458
459 submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1);
460 if (submit && submit->fault_dumped)
461 goto resume_smmu;
462
463 if (submit) {
464 struct task_struct *task;
465
466 task = get_pid_task(submit->pid, PIDTYPE_PID);
467 if (task) {
468 comm = kstrdup(task->comm, GFP_KERNEL);
469 cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL);
470 put_task_struct(task);
471 }
472
473 /*
474 * When we get GPU iova faults, we can get 1000s of them,
475 * but we really only want to log the first one.
476 */
477 submit->fault_dumped = true;
478 }
479
480 /* Record the crash state */
481 pm_runtime_get_sync(&gpu->pdev->dev);
482 msm_gpu_crashstate_capture(gpu, submit, comm, cmd);
483 pm_runtime_put_sync(&gpu->pdev->dev);
484
485 kfree(cmd);
486 kfree(comm);
487
488resume_smmu:
489 memset(&gpu->fault_info, 0, sizeof(gpu->fault_info));
490 gpu->aspace->mmu->funcs->resume_translation(gpu->aspace->mmu);
491
Rob Clarkc28e2f22021-11-09 10:11:03 -0800492 mutex_unlock(&gpu->lock);
Rob Clarke25e92e2021-06-10 14:44:13 -0700493}
494
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400495static void hangcheck_timer_reset(struct msm_gpu *gpu)
496{
Samuel Iglesias Gonsalvez1d2fa582021-06-07 12:44:41 +0200497 struct msm_drm_private *priv = gpu->dev->dev_private;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400498 mod_timer(&gpu->hangcheck_timer,
Samuel Iglesias Gonsalvez1d2fa582021-06-07 12:44:41 +0200499 round_jiffies_up(jiffies + msecs_to_jiffies(priv->hangcheck_period)));
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400500}
501
Kees Cooke99e88a2017-10-16 14:43:17 -0700502static void hangcheck_handler(struct timer_list *t)
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400503{
Kees Cooke99e88a2017-10-16 14:43:17 -0700504 struct msm_gpu *gpu = from_timer(gpu, t, hangcheck_timer);
Rob Clark6b8819c2013-09-11 17:14:30 -0400505 struct drm_device *dev = gpu->dev;
Jordan Crousef97deca2017-10-20 11:06:57 -0600506 struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
507 uint32_t fence = ring->memptrs->fence;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400508
Jordan Crousef97deca2017-10-20 11:06:57 -0600509 if (fence != ring->hangcheck_fence) {
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400510 /* some progress has been made.. ya! */
Jordan Crousef97deca2017-10-20 11:06:57 -0600511 ring->hangcheck_fence = fence;
Rob Clark5f3aee42021-11-09 10:11:04 -0800512 } else if (fence_before(fence, ring->seqno)) {
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400513 /* no progress and not done.. hung! */
Jordan Crousef97deca2017-10-20 11:06:57 -0600514 ring->hangcheck_fence = fence;
Mamta Shukla6a41da12018-10-20 23:19:26 +0530515 DRM_DEV_ERROR(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n",
Jordan Crousef97deca2017-10-20 11:06:57 -0600516 gpu->name, ring->id);
Mamta Shukla6a41da12018-10-20 23:19:26 +0530517 DRM_DEV_ERROR(dev->dev, "%s: completed fence: %u\n",
Rob Clark26791c42013-09-03 07:12:03 -0400518 gpu->name, fence);
Mamta Shukla6a41da12018-10-20 23:19:26 +0530519 DRM_DEV_ERROR(dev->dev, "%s: submitted fence: %u\n",
Jordan Crousef97deca2017-10-20 11:06:57 -0600520 gpu->name, ring->seqno);
521
Rob Clark7e688292020-10-19 14:10:51 -0700522 kthread_queue_work(gpu->worker, &gpu->recover_work);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400523 }
524
525 /* if still more pending work, reset the hangcheck timer: */
Rob Clark5f3aee42021-11-09 10:11:04 -0800526 if (fence_after(ring->seqno, ring->hangcheck_fence))
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400527 hangcheck_timer_reset(gpu);
Rob Clark6b8819c2013-09-11 17:14:30 -0400528
529 /* workaround for missing irq: */
Rob Clark298287f2021-07-26 07:43:58 -0700530 msm_gpu_retire(gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400531}
532
533/*
Rob Clark70c70f02014-05-30 14:49:43 -0400534 * Performance Counters:
535 */
536
537/* called under perf_lock */
538static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
539{
540 uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
541 int i, n = min(ncntrs, gpu->num_perfcntrs);
542
543 /* read current values: */
544 for (i = 0; i < gpu->num_perfcntrs; i++)
545 current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
546
547 /* update cntrs: */
548 for (i = 0; i < n; i++)
549 cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
550
551 /* save current values: */
552 for (i = 0; i < gpu->num_perfcntrs; i++)
553 gpu->last_cntrs[i] = current_cntrs[i];
554
555 return n;
556}
557
558static void update_sw_cntrs(struct msm_gpu *gpu)
559{
560 ktime_t time;
561 uint32_t elapsed;
562 unsigned long flags;
563
564 spin_lock_irqsave(&gpu->perf_lock, flags);
565 if (!gpu->perfcntr_active)
566 goto out;
567
568 time = ktime_get();
569 elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
570
571 gpu->totaltime += elapsed;
572 if (gpu->last_sample.active)
573 gpu->activetime += elapsed;
574
575 gpu->last_sample.active = msm_gpu_active(gpu);
576 gpu->last_sample.time = time;
577
578out:
579 spin_unlock_irqrestore(&gpu->perf_lock, flags);
580}
581
582void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
583{
584 unsigned long flags;
585
Rob Clarkeeb75472017-02-10 15:36:33 -0500586 pm_runtime_get_sync(&gpu->pdev->dev);
587
Rob Clark70c70f02014-05-30 14:49:43 -0400588 spin_lock_irqsave(&gpu->perf_lock, flags);
589 /* we could dynamically enable/disable perfcntr registers too.. */
590 gpu->last_sample.active = msm_gpu_active(gpu);
591 gpu->last_sample.time = ktime_get();
592 gpu->activetime = gpu->totaltime = 0;
593 gpu->perfcntr_active = true;
594 update_hw_cntrs(gpu, 0, NULL);
595 spin_unlock_irqrestore(&gpu->perf_lock, flags);
596}
597
598void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
599{
600 gpu->perfcntr_active = false;
Rob Clarkeeb75472017-02-10 15:36:33 -0500601 pm_runtime_put_sync(&gpu->pdev->dev);
Rob Clark70c70f02014-05-30 14:49:43 -0400602}
603
604/* returns -errno or # of cntrs sampled */
605int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
606 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
607{
608 unsigned long flags;
609 int ret;
610
611 spin_lock_irqsave(&gpu->perf_lock, flags);
612
613 if (!gpu->perfcntr_active) {
614 ret = -EINVAL;
615 goto out;
616 }
617
618 *activetime = gpu->activetime;
619 *totaltime = gpu->totaltime;
620
621 gpu->activetime = gpu->totaltime = 0;
622
623 ret = update_hw_cntrs(gpu, ncntrs, cntrs);
624
625out:
626 spin_unlock_irqrestore(&gpu->perf_lock, flags);
627
628 return ret;
629}
630
631/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400632 * Cmdstream submission/retirement:
633 */
634
Jordan Crouse4241db42018-11-02 09:25:21 -0600635static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
636 struct msm_gem_submit *submit)
Rob Clark7d12a272016-03-16 16:07:38 -0400637{
Jordan Crouse4241db42018-11-02 09:25:21 -0600638 int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT;
639 volatile struct msm_gpu_submit_stats *stats;
640 u64 elapsed, clock = 0;
Rob Clark298287f2021-07-26 07:43:58 -0700641 unsigned long flags;
Rob Clark7d12a272016-03-16 16:07:38 -0400642
Jordan Crouse4241db42018-11-02 09:25:21 -0600643 stats = &ring->memptrs->stats[index];
644 /* Convert 19.2Mhz alwayson ticks to nanoseconds for elapsed time */
645 elapsed = (stats->alwayson_end - stats->alwayson_start) * 10000;
646 do_div(elapsed, 192);
647
648 /* Calculate the clock frequency from the number of CP cycles */
649 if (elapsed) {
650 clock = (stats->cpcycles_end - stats->cpcycles_start) * 1000;
651 do_div(clock, elapsed);
652 }
653
654 trace_msm_gpu_submit_retired(submit, elapsed, clock,
655 stats->alwayson_start, stats->alwayson_end);
656
Rob Clarkbe405962021-07-27 18:06:11 -0700657 msm_submit_retire(submit);
Rob Clark7d12a272016-03-16 16:07:38 -0400658
Rob Clarkeeb75472017-02-10 15:36:33 -0500659 pm_runtime_mark_last_busy(&gpu->pdev->dev);
660 pm_runtime_put_autosuspend(&gpu->pdev->dev);
Rob Clark964d2f92020-10-23 09:51:17 -0700661
Rob Clark298287f2021-07-26 07:43:58 -0700662 spin_lock_irqsave(&ring->submit_lock, flags);
Rob Clark964d2f92020-10-23 09:51:17 -0700663 list_del(&submit->node);
Rob Clark298287f2021-07-26 07:43:58 -0700664 spin_unlock_irqrestore(&ring->submit_lock, flags);
Rob Clark964d2f92020-10-23 09:51:17 -0700665
Rob Clark9bc95572021-07-26 07:46:50 -0700666 /* Update devfreq on transition from active->idle: */
667 mutex_lock(&gpu->active_lock);
668 gpu->active_submits--;
669 WARN_ON(gpu->active_submits < 0);
670 if (!gpu->active_submits)
671 msm_devfreq_idle(gpu);
672 mutex_unlock(&gpu->active_lock);
673
Rob Clark964d2f92020-10-23 09:51:17 -0700674 msm_gem_submit_put(submit);
Rob Clark7d12a272016-03-16 16:07:38 -0400675}
676
Rob Clarkb6295f92016-03-15 18:26:28 -0400677static void retire_submits(struct msm_gpu *gpu)
Rob Clark1a370be2015-06-07 13:46:04 -0400678{
Jordan Crousef97deca2017-10-20 11:06:57 -0600679 int i;
Rob Clark1a370be2015-06-07 13:46:04 -0400680
Jordan Crousef97deca2017-10-20 11:06:57 -0600681 /* Retire the commits starting with highest priority */
Jordan Crouseb1fc2832017-10-20 11:07:01 -0600682 for (i = 0; i < gpu->nr_rings; i++) {
Jordan Crousef97deca2017-10-20 11:06:57 -0600683 struct msm_ringbuffer *ring = gpu->rb[i];
Rob Clark1a370be2015-06-07 13:46:04 -0400684
Rob Clark77d20522020-10-23 09:51:16 -0700685 while (true) {
686 struct msm_gem_submit *submit = NULL;
Rob Clark298287f2021-07-26 07:43:58 -0700687 unsigned long flags;
Rob Clark77d20522020-10-23 09:51:16 -0700688
Rob Clark298287f2021-07-26 07:43:58 -0700689 spin_lock_irqsave(&ring->submit_lock, flags);
Rob Clark77d20522020-10-23 09:51:16 -0700690 submit = list_first_entry_or_null(&ring->submits,
691 struct msm_gem_submit, node);
Rob Clark298287f2021-07-26 07:43:58 -0700692 spin_unlock_irqrestore(&ring->submit_lock, flags);
Rob Clark77d20522020-10-23 09:51:16 -0700693
694 /*
695 * If no submit, we are done. If submit->fence hasn't
696 * been signalled, then later submits are not signalled
697 * either, so we are also done.
698 */
Rob Clark1d8a5ca2021-07-27 18:06:14 -0700699 if (submit && dma_fence_is_signaled(submit->hw_fence)) {
Jordan Crouse4241db42018-11-02 09:25:21 -0600700 retire_submit(gpu, ring, submit);
Rob Clark77d20522020-10-23 09:51:16 -0700701 } else {
702 break;
703 }
Rob Clark1a370be2015-06-07 13:46:04 -0400704 }
705 }
706}
707
Rob Clark7e688292020-10-19 14:10:51 -0700708static void retire_worker(struct kthread_work *work)
Rob Clark7198e6b2013-07-19 12:59:32 -0400709{
710 struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
Rob Clarkedd4fc62013-09-14 14:01:55 -0400711
Rob Clarkb6295f92016-03-15 18:26:28 -0400712 retire_submits(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400713}
714
715/* call from irq handler to schedule work to retire bo's */
716void msm_gpu_retire(struct msm_gpu *gpu)
717{
Rob Clark298287f2021-07-26 07:43:58 -0700718 int i;
719
720 for (i = 0; i < gpu->nr_rings; i++)
721 update_fences(gpu, gpu->rb[i], gpu->rb[i]->memptrs->fence);
722
Rob Clark7e688292020-10-19 14:10:51 -0700723 kthread_queue_work(gpu->worker, &gpu->retire_work);
Rob Clark70c70f02014-05-30 14:49:43 -0400724 update_sw_cntrs(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400725}
726
727/* add bo's to gpu's ring, and kick gpu: */
Jordan Crouse15eb9ad2020-08-17 15:01:37 -0700728void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400729{
730 struct drm_device *dev = gpu->dev;
731 struct msm_drm_private *priv = dev->dev_private;
Jordan Crousef97deca2017-10-20 11:06:57 -0600732 struct msm_ringbuffer *ring = submit->ring;
Rob Clark298287f2021-07-26 07:43:58 -0700733 unsigned long flags;
Rob Clark7198e6b2013-07-19 12:59:32 -0400734
Rob Clarkc28e2f22021-11-09 10:11:03 -0800735 WARN_ON(!mutex_is_locked(&gpu->lock));
Rob Clark1a370be2015-06-07 13:46:04 -0400736
Rob Clarkeeb75472017-02-10 15:36:33 -0500737 pm_runtime_get_sync(&gpu->pdev->dev);
738
739 msm_gpu_hw_init(gpu);
Rob Clark37d77c32014-01-11 16:25:08 -0500740
Jordan Crousef97deca2017-10-20 11:06:57 -0600741 submit->seqno = ++ring->seqno;
742
Rob Clark998b9a52017-09-15 10:46:45 -0400743 msm_rd_dump_submit(priv->rd, submit, NULL);
Rob Clarka7d3c952014-05-30 14:47:38 -0400744
Rob Clark70c70f02014-05-30 14:49:43 -0400745 update_sw_cntrs(gpu);
746
Rob Clark964d2f92020-10-23 09:51:17 -0700747 /*
748 * ring->submits holds a ref to the submit, to deal with the case
749 * that a submit completes before msm_ioctl_gem_submit() returns.
750 */
751 msm_gem_submit_get(submit);
752
Rob Clark298287f2021-07-26 07:43:58 -0700753 spin_lock_irqsave(&ring->submit_lock, flags);
Rob Clark964d2f92020-10-23 09:51:17 -0700754 list_add_tail(&submit->node, &ring->submits);
Rob Clark298287f2021-07-26 07:43:58 -0700755 spin_unlock_irqrestore(&ring->submit_lock, flags);
Rob Clark964d2f92020-10-23 09:51:17 -0700756
Rob Clark9bc95572021-07-26 07:46:50 -0700757 /* Update devfreq on transition from idle->active: */
758 mutex_lock(&gpu->active_lock);
759 if (!gpu->active_submits)
760 msm_devfreq_active(gpu);
761 gpu->active_submits++;
762 mutex_unlock(&gpu->active_lock);
763
Jordan Crouse15eb9ad2020-08-17 15:01:37 -0700764 gpu->funcs->submit(gpu, submit);
Rob Clark1d054c92021-11-09 10:11:02 -0800765 gpu->cur_ctx_seqno = submit->queue->ctx->seqno;
Rob Clark1a370be2015-06-07 13:46:04 -0400766
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400767 hangcheck_timer_reset(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400768}
769
770/*
771 * Init/Cleanup:
772 */
773
774static irqreturn_t irq_handler(int irq, void *data)
775{
776 struct msm_gpu *gpu = data;
777 return gpu->funcs->irq(gpu);
778}
779
Jordan Crouse98db8032017-03-07 10:02:56 -0700780static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu)
781{
Jordan Crouse8e3e7912019-07-25 10:53:55 -0600782 int ret = devm_clk_bulk_get_all(&pdev->dev, &gpu->grp_clks);
Jordan Crouse98db8032017-03-07 10:02:56 -0700783
Jordan Crouse8e54eea2018-08-06 11:33:21 -0600784 if (ret < 1) {
Jordan Crouse98db8032017-03-07 10:02:56 -0700785 gpu->nr_clocks = 0;
Jordan Crouse8e54eea2018-08-06 11:33:21 -0600786 return ret;
Jordan Crouse98db8032017-03-07 10:02:56 -0700787 }
788
Jordan Crouse8e54eea2018-08-06 11:33:21 -0600789 gpu->nr_clocks = ret;
Jordan Crouse98db8032017-03-07 10:02:56 -0700790
Jordan Crouse8e54eea2018-08-06 11:33:21 -0600791 gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
792 gpu->nr_clocks, "core");
Jordan Crouse98db8032017-03-07 10:02:56 -0700793
Jordan Crouse8e54eea2018-08-06 11:33:21 -0600794 gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
795 gpu->nr_clocks, "rbbmtimer");
Jordan Crouse98db8032017-03-07 10:02:56 -0700796
797 return 0;
798}
Rob Clark7198e6b2013-07-19 12:59:32 -0400799
Jordan Crouse933415e2020-08-17 15:01:40 -0700800/* Return a new address space for a msm_drm_private instance */
801struct msm_gem_address_space *
Rob Clark25faf2f2020-08-17 15:01:45 -0700802msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task)
Jordan Crouse933415e2020-08-17 15:01:40 -0700803{
804 struct msm_gem_address_space *aspace = NULL;
Jordan Crouse933415e2020-08-17 15:01:40 -0700805 if (!gpu)
806 return NULL;
807
808 /*
809 * If the target doesn't support private address spaces then return
810 * the global one
811 */
Rob Clark25faf2f2020-08-17 15:01:45 -0700812 if (gpu->funcs->create_private_address_space) {
Jordan Crouse933415e2020-08-17 15:01:40 -0700813 aspace = gpu->funcs->create_private_address_space(gpu);
Rob Clark25faf2f2020-08-17 15:01:45 -0700814 if (!IS_ERR(aspace))
815 aspace->pid = get_pid(task_pid(task));
816 }
Jordan Crouse933415e2020-08-17 15:01:40 -0700817
818 if (IS_ERR_OR_NULL(aspace))
819 aspace = msm_gem_address_space_get(gpu->aspace);
820
821 return aspace;
822}
823
Rob Clark7198e6b2013-07-19 12:59:32 -0400824int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
825 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
Jordan Crouse5770fc72017-05-08 14:35:03 -0600826 const char *name, struct msm_gpu_config *config)
Rob Clark7198e6b2013-07-19 12:59:32 -0400827{
Jordan Crousef97deca2017-10-20 11:06:57 -0600828 int i, ret, nr_rings = config->nr_rings;
829 void *memptrs;
830 uint64_t memptrs_iova;
Rob Clark7198e6b2013-07-19 12:59:32 -0400831
Rob Clark70c70f02014-05-30 14:49:43 -0400832 if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
833 gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
834
Rob Clark7198e6b2013-07-19 12:59:32 -0400835 gpu->dev = drm;
836 gpu->funcs = funcs;
837 gpu->name = name;
838
Rob Clark7e688292020-10-19 14:10:51 -0700839 gpu->worker = kthread_create_worker(0, "%s-worker", gpu->name);
840 if (IS_ERR(gpu->worker)) {
841 ret = PTR_ERR(gpu->worker);
842 gpu->worker = NULL;
843 goto fail;
844 }
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400845
Rob Clark7e688292020-10-19 14:10:51 -0700846 sched_set_fifo_low(gpu->worker->task);
847
848 INIT_LIST_HEAD(&gpu->active_list);
Rob Clark9bc95572021-07-26 07:46:50 -0700849 mutex_init(&gpu->active_lock);
Rob Clarkc28e2f22021-11-09 10:11:03 -0800850 mutex_init(&gpu->lock);
Rob Clark7e688292020-10-19 14:10:51 -0700851 kthread_init_work(&gpu->retire_work, retire_worker);
852 kthread_init_work(&gpu->recover_work, recover_worker);
Rob Clarke25e92e2021-06-10 14:44:13 -0700853 kthread_init_work(&gpu->fault_work, fault_worker);
Rob Clark1a370be2015-06-07 13:46:04 -0400854
Kees Cooke99e88a2017-10-16 14:43:17 -0700855 timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0);
Rob Clark7198e6b2013-07-19 12:59:32 -0400856
Rob Clark70c70f02014-05-30 14:49:43 -0400857 spin_lock_init(&gpu->perf_lock);
858
Rob Clark7198e6b2013-07-19 12:59:32 -0400859
860 /* Map registers: */
Jordan Crouse5770fc72017-05-08 14:35:03 -0600861 gpu->mmio = msm_ioremap(pdev, config->ioname, name);
Rob Clark7198e6b2013-07-19 12:59:32 -0400862 if (IS_ERR(gpu->mmio)) {
863 ret = PTR_ERR(gpu->mmio);
864 goto fail;
865 }
866
867 /* Get Interrupt: */
Jordan Crouse878411a2018-12-18 11:32:36 -0700868 gpu->irq = platform_get_irq(pdev, 0);
Rob Clark7198e6b2013-07-19 12:59:32 -0400869 if (gpu->irq < 0) {
870 ret = gpu->irq;
Mamta Shukla6a41da12018-10-20 23:19:26 +0530871 DRM_DEV_ERROR(drm->dev, "failed to get irq: %d\n", ret);
Rob Clark7198e6b2013-07-19 12:59:32 -0400872 goto fail;
873 }
874
875 ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
876 IRQF_TRIGGER_HIGH, gpu->name, gpu);
877 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530878 DRM_DEV_ERROR(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
Rob Clark7198e6b2013-07-19 12:59:32 -0400879 goto fail;
880 }
881
Jordan Crouse98db8032017-03-07 10:02:56 -0700882 ret = get_clocks(pdev, gpu);
883 if (ret)
884 goto fail;
Rob Clark7198e6b2013-07-19 12:59:32 -0400885
Rob Clark720c3bb2017-01-30 11:30:58 -0500886 gpu->ebi1_clk = msm_clk_get(pdev, "bus");
Rob Clark7198e6b2013-07-19 12:59:32 -0400887 DBG("ebi1_clk: %p", gpu->ebi1_clk);
888 if (IS_ERR(gpu->ebi1_clk))
889 gpu->ebi1_clk = NULL;
890
891 /* Acquire regulators: */
892 gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
893 DBG("gpu_reg: %p", gpu->gpu_reg);
894 if (IS_ERR(gpu->gpu_reg))
895 gpu->gpu_reg = NULL;
896
897 gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
898 DBG("gpu_cx: %p", gpu->gpu_cx);
899 if (IS_ERR(gpu->gpu_cx))
900 gpu->gpu_cx = NULL;
901
Jordan Crouse1267a4d2017-07-27 10:42:39 -0600902 gpu->pdev = pdev;
Rob Clark9cba4052020-08-17 15:01:32 -0700903 platform_set_drvdata(pdev, &gpu->adreno_smmu);
Rob Clark667ce332016-09-28 19:58:32 -0400904
Jordan Crousef91c14a2018-01-10 10:41:54 -0700905 msm_devfreq_init(gpu);
906
Jordan Crouseccac7ce2020-05-22 16:03:15 -0600907
908 gpu->aspace = gpu->funcs->create_address_space(gpu, pdev);
Jordan Crouse1267a4d2017-07-27 10:42:39 -0600909
910 if (gpu->aspace == NULL)
Mamta Shukla6a41da12018-10-20 23:19:26 +0530911 DRM_DEV_INFO(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
Jordan Crouse1267a4d2017-07-27 10:42:39 -0600912 else if (IS_ERR(gpu->aspace)) {
913 ret = PTR_ERR(gpu->aspace);
914 goto fail;
Rob Clark7198e6b2013-07-19 12:59:32 -0400915 }
Rob Clarka1ad3522014-07-11 11:59:22 -0400916
Jordan Crouse546ec7b2018-11-02 09:25:18 -0600917 memptrs = msm_gem_kernel_new(drm,
918 sizeof(struct msm_rbmemptrs) * nr_rings,
Jordan Crouse604234f2020-09-03 20:03:11 -0600919 check_apriv(gpu, MSM_BO_UNCACHED), gpu->aspace, &gpu->memptrs_bo,
Jordan Crousef97deca2017-10-20 11:06:57 -0600920 &memptrs_iova);
Jordan Crousecd414f32017-10-20 11:06:56 -0600921
Jordan Crousef97deca2017-10-20 11:06:57 -0600922 if (IS_ERR(memptrs)) {
923 ret = PTR_ERR(memptrs);
Mamta Shukla6a41da12018-10-20 23:19:26 +0530924 DRM_DEV_ERROR(drm->dev, "could not allocate memptrs: %d\n", ret);
Jordan Crousecd414f32017-10-20 11:06:56 -0600925 goto fail;
926 }
927
Jordan Crouse0815d772018-11-07 15:35:52 -0700928 msm_gem_object_set_name(gpu->memptrs_bo, "memptrs");
929
Jordan Crousef97deca2017-10-20 11:06:57 -0600930 if (nr_rings > ARRAY_SIZE(gpu->rb)) {
Arnd Bergmann39ae0d32017-08-03 13:50:48 +0200931 DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n",
Jordan Crousef97deca2017-10-20 11:06:57 -0600932 ARRAY_SIZE(gpu->rb));
933 nr_rings = ARRAY_SIZE(gpu->rb);
Rob Clark7198e6b2013-07-19 12:59:32 -0400934 }
935
Jordan Crousef97deca2017-10-20 11:06:57 -0600936 /* Create ringbuffer(s): */
937 for (i = 0; i < nr_rings; i++) {
938 gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova);
939
940 if (IS_ERR(gpu->rb[i])) {
941 ret = PTR_ERR(gpu->rb[i]);
Mamta Shukla6a41da12018-10-20 23:19:26 +0530942 DRM_DEV_ERROR(drm->dev,
Jordan Crousef97deca2017-10-20 11:06:57 -0600943 "could not create ringbuffer %d: %d\n", i, ret);
944 goto fail;
945 }
946
947 memptrs += sizeof(struct msm_rbmemptrs);
948 memptrs_iova += sizeof(struct msm_rbmemptrs);
949 }
950
951 gpu->nr_rings = nr_rings;
952
Rob Clark7198e6b2013-07-19 12:59:32 -0400953 return 0;
954
955fail:
Jordan Crousef97deca2017-10-20 11:06:57 -0600956 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
957 msm_ringbuffer_destroy(gpu->rb[i]);
958 gpu->rb[i] = NULL;
959 }
960
Rob Clark030af2b2021-07-27 18:06:08 -0700961 msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace);
Jordan Crousecd414f32017-10-20 11:06:56 -0600962
Jordan Crouse1267a4d2017-07-27 10:42:39 -0600963 platform_set_drvdata(pdev, NULL);
Rob Clark7198e6b2013-07-19 12:59:32 -0400964 return ret;
965}
966
967void msm_gpu_cleanup(struct msm_gpu *gpu)
968{
Jordan Crousef97deca2017-10-20 11:06:57 -0600969 int i;
970
Rob Clark7198e6b2013-07-19 12:59:32 -0400971 DBG("%s", gpu->name);
972
973 WARN_ON(!list_empty(&gpu->active_list));
974
Jordan Crousef97deca2017-10-20 11:06:57 -0600975 for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
976 msm_ringbuffer_destroy(gpu->rb[i]);
977 gpu->rb[i] = NULL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400978 }
Jordan Crousecd414f32017-10-20 11:06:56 -0600979
Rob Clark030af2b2021-07-27 18:06:08 -0700980 msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace);
Jordan Crousecd414f32017-10-20 11:06:56 -0600981
982 if (!IS_ERR_OR_NULL(gpu->aspace)) {
Drew Davenport53bf7f72019-09-16 14:11:54 -0600983 gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu);
Jordan Crouse1267a4d2017-07-27 10:42:39 -0600984 msm_gem_address_space_put(gpu->aspace);
985 }
Rob Clark7e688292020-10-19 14:10:51 -0700986
987 if (gpu->worker) {
988 kthread_destroy_worker(gpu->worker);
989 }
Akhil P Oommenec793cf2020-10-30 16:17:10 +0530990
Rob Clarkaf5b4ff2021-07-26 07:46:48 -0700991 msm_devfreq_cleanup(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400992}