Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Red Hat |
| 4 | * Author: Rob Clark <robdclark@gmail.com> |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include "msm_gpu.h" |
| 8 | #include "msm_gem.h" |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 9 | #include "msm_mmu.h" |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 10 | #include "msm_fence.h" |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 11 | #include "msm_gpu_trace.h" |
Jonathan Marek | c2052a4 | 2018-11-14 17:08:04 -0500 | [diff] [blame] | 12 | #include "adreno/adreno_gpu.h" |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 13 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 14 | #include <generated/utsrelease.h> |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 15 | #include <linux/string_helpers.h> |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 16 | #include <linux/devcoredump.h> |
Arnd Bergmann | 70082a5 | 2019-09-18 21:57:07 +0200 | [diff] [blame] | 17 | #include <linux/sched/task.h> |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Power Management: |
| 21 | */ |
| 22 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 23 | static 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 Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 31 | DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_reg': %d\n", ret); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 32 | return ret; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if (gpu->gpu_cx) { |
| 37 | ret = regulator_enable(gpu->gpu_cx); |
| 38 | if (ret) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 39 | DRM_DEV_ERROR(dev->dev, "failed to enable 'gpu_cx': %d\n", ret); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 40 | return ret; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static 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 | |
| 56 | static int enable_clk(struct msm_gpu *gpu) |
| 57 | { |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 58 | if (gpu->core_clk && gpu->fast_rate) |
| 59 | clk_set_rate(gpu->core_clk, gpu->fast_rate); |
Jordan Crouse | 89d777a | 2016-11-28 12:28:31 -0700 | [diff] [blame] | 60 | |
Jordan Crouse | b5f103a | 2016-11-28 12:28:33 -0700 | [diff] [blame] | 61 | /* Set the RBBM timer rate to 19.2Mhz */ |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 62 | if (gpu->rbbmtimer_clk) |
| 63 | clk_set_rate(gpu->rbbmtimer_clk, 19200000); |
Jordan Crouse | b5f103a | 2016-11-28 12:28:33 -0700 | [diff] [blame] | 64 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 65 | return clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int disable_clk(struct msm_gpu *gpu) |
| 69 | { |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 70 | clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 71 | |
Jordan Crouse | bf5af4a | 2017-03-07 10:02:54 -0700 | [diff] [blame] | 72 | /* |
| 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 Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 77 | if (gpu->core_clk) |
| 78 | clk_set_rate(gpu->core_clk, 27000000); |
Jordan Crouse | 89d777a | 2016-11-28 12:28:31 -0700 | [diff] [blame] | 79 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 80 | if (gpu->rbbmtimer_clk) |
| 81 | clk_set_rate(gpu->rbbmtimer_clk, 0); |
Jordan Crouse | b5f103a | 2016-11-28 12:28:33 -0700 | [diff] [blame] | 82 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int enable_axi(struct msm_gpu *gpu) |
| 87 | { |
Tian Tao | dd29bd4 | 2020-10-19 14:04:22 +0800 | [diff] [blame] | 88 | return clk_prepare_enable(gpu->ebi1_clk); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int disable_axi(struct msm_gpu *gpu) |
| 92 | { |
Tian Tao | dd29bd4 | 2020-10-19 14:04:22 +0800 | [diff] [blame] | 93 | clk_disable_unprepare(gpu->ebi1_clk); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int msm_gpu_pm_resume(struct msm_gpu *gpu) |
| 98 | { |
| 99 | int ret; |
| 100 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 101 | DBG("%s", gpu->name); |
Rob Clark | ec1cb6e | 2020-09-01 08:41:56 -0700 | [diff] [blame] | 102 | trace_msm_gpu_resume(0); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 103 | |
| 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 Clark | af5b4ff | 2021-07-26 07:46:48 -0700 | [diff] [blame] | 116 | msm_devfreq_resume(gpu); |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 117 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 118 | gpu->needs_hw_init = true; |
| 119 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int msm_gpu_pm_suspend(struct msm_gpu *gpu) |
| 124 | { |
| 125 | int ret; |
| 126 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 127 | DBG("%s", gpu->name); |
Rob Clark | ec1cb6e | 2020-09-01 08:41:56 -0700 | [diff] [blame] | 128 | trace_msm_gpu_suspend(0); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 129 | |
Rob Clark | af5b4ff | 2021-07-26 07:46:48 -0700 | [diff] [blame] | 130 | msm_devfreq_suspend(gpu); |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 131 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 132 | 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 Clark | 3ab1c5c | 2021-03-24 18:23:53 -0700 | [diff] [blame] | 144 | gpu->suspend_count++; |
| 145 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 149 | int msm_gpu_hw_init(struct msm_gpu *gpu) |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 150 | { |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 151 | int ret; |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 152 | |
Rob Clark | c28e2f2 | 2021-11-09 10:11:03 -0800 | [diff] [blame] | 153 | WARN_ON(!mutex_is_locked(&gpu->lock)); |
Rob Clark | cb1e381 | 2017-06-13 09:15:36 -0400 | [diff] [blame] | 154 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 155 | if (!gpu->needs_hw_init) |
| 156 | return 0; |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 157 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 158 | 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 Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 163 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 164 | return ret; |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Rob Clark | 2a86efb | 2020-10-23 09:51:13 -0700 | [diff] [blame] | 167 | static void update_fences(struct msm_gpu *gpu, struct msm_ringbuffer *ring, |
| 168 | uint32_t fence) |
| 169 | { |
| 170 | struct msm_gem_submit *submit; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 171 | unsigned long flags; |
Rob Clark | 2a86efb | 2020-10-23 09:51:13 -0700 | [diff] [blame] | 172 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 173 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 2a86efb | 2020-10-23 09:51:13 -0700 | [diff] [blame] | 174 | list_for_each_entry(submit, &ring->submits, node) { |
Rob Clark | 5f3aee4 | 2021-11-09 10:11:04 -0800 | [diff] [blame] | 175 | if (fence_after(submit->seqno, fence)) |
Rob Clark | 2a86efb | 2020-10-23 09:51:13 -0700 | [diff] [blame] | 176 | break; |
| 177 | |
| 178 | msm_update_fence(submit->ring->fctx, |
Rob Clark | 1d8a5ca | 2021-07-27 18:06:14 -0700 | [diff] [blame] | 179 | submit->hw_fence->seqno); |
| 180 | dma_fence_signal(submit->hw_fence); |
Rob Clark | 2a86efb | 2020-10-23 09:51:13 -0700 | [diff] [blame] | 181 | } |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 182 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 2a86efb | 2020-10-23 09:51:13 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 185 | #ifdef CONFIG_DEV_COREDUMP |
| 186 | static 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 Bergmann | 3530a17 | 2018-07-26 14:39:25 +0200 | [diff] [blame] | 208 | drm_printf(&p, "time: %lld.%09ld\n", |
| 209 | state->time.tv_sec, state->time.tv_nsec); |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 210 | 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 | |
| 222 | static void msm_gpu_devcoredump_free(void *data) |
| 223 | { |
| 224 | struct msm_gpu *gpu = data; |
| 225 | |
| 226 | msm_gpu_crashstate_put(gpu); |
| 227 | } |
| 228 | |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 229 | static 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 Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 235 | state_bo->size = obj->base.size; |
| 236 | state_bo->iova = iova; |
| 237 | |
Jordan Crouse | 896a248 | 2018-11-02 09:25:22 -0600 | [diff] [blame] | 238 | /* Only store data for non imported buffer objects marked for read */ |
| 239 | if ((flags & MSM_SUBMIT_BO_READ) && !obj->base.import_attach) { |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 240 | void *ptr; |
| 241 | |
| 242 | state_bo->data = kvmalloc(obj->base.size, GFP_KERNEL); |
| 243 | if (!state_bo->data) |
Jordan Crouse | 896a248 | 2018-11-02 09:25:22 -0600 | [diff] [blame] | 244 | goto out; |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 245 | |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 246 | msm_gem_lock(&obj->base); |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 247 | ptr = msm_gem_get_vaddr_active(&obj->base); |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 248 | msm_gem_unlock(&obj->base); |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 249 | if (IS_ERR(ptr)) { |
| 250 | kvfree(state_bo->data); |
Jordan Crouse | 896a248 | 2018-11-02 09:25:22 -0600 | [diff] [blame] | 251 | state_bo->data = NULL; |
| 252 | goto out; |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | memcpy(state_bo->data, ptr, obj->base.size); |
| 256 | msm_gem_put_vaddr(&obj->base); |
| 257 | } |
Jordan Crouse | 896a248 | 2018-11-02 09:25:22 -0600 | [diff] [blame] | 258 | out: |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 259 | state->nr_bos++; |
| 260 | } |
| 261 | |
| 262 | static void msm_gpu_crashstate_capture(struct msm_gpu *gpu, |
| 263 | struct msm_gem_submit *submit, char *comm, char *cmd) |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 264 | { |
| 265 | struct msm_gpu_state *state; |
| 266 | |
Sharat Masetty | 4f3a31a | 2018-10-12 14:26:55 +0530 | [diff] [blame] | 267 | /* Check if the target supports capturing crash state */ |
| 268 | if (!gpu->funcs->gpu_state_get) |
| 269 | return; |
| 270 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 271 | /* 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 Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 282 | state->fault_info = gpu->fault_info; |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 283 | |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 284 | if (submit) { |
Rob Clark | e515af8 | 2020-02-18 13:20:12 -0800 | [diff] [blame] | 285 | int i, nr = 0; |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 286 | |
Rob Clark | e515af8 | 2020-02-18 13:20:12 -0800 | [diff] [blame] | 287 | /* 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 Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 297 | sizeof(struct msm_gpu_state_bo), GFP_KERNEL); |
| 298 | |
Tim Gardner | b220c15 | 2021-09-29 10:25:54 -0600 | [diff] [blame] | 299 | for (i = 0; state->bos && i < submit->nr_bos; i++) { |
Rob Clark | e515af8 | 2020-02-18 13:20:12 -0800 | [diff] [blame] | 300 | 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 Crouse | 896a248 | 2018-11-02 09:25:22 -0600 | [diff] [blame] | 306 | for (i = 0; state->bos && i < submit->nr_cmds; i++) { |
| 307 | int idx = submit->cmd[i].idx; |
| 308 | |
Rob Clark | e515af8 | 2020-02-18 13:20:12 -0800 | [diff] [blame] | 309 | 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 Crouse | 896a248 | 2018-11-02 09:25:22 -0600 | [diff] [blame] | 313 | } |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 314 | } |
| 315 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 316 | /* 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 Roxell | 6969019 | 2018-07-31 22:45:32 +0200 | [diff] [blame] | 324 | static void msm_gpu_crashstate_capture(struct msm_gpu *gpu, |
| 325 | struct msm_gem_submit *submit, char *comm, char *cmd) |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 326 | { |
| 327 | } |
| 328 | #endif |
| 329 | |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 330 | /* |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 331 | * Hangcheck detection for locked gpu: |
| 332 | */ |
| 333 | |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 334 | static struct msm_gem_submit * |
| 335 | find_submit(struct msm_ringbuffer *ring, uint32_t fence) |
| 336 | { |
| 337 | struct msm_gem_submit *submit; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 338 | unsigned long flags; |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 339 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 340 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 341 | list_for_each_entry(submit, &ring->submits, node) { |
| 342 | if (submit->seqno == fence) { |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 343 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 344 | return submit; |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 347 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 348 | |
| 349 | return NULL; |
| 350 | } |
| 351 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 352 | static void retire_submits(struct msm_gpu *gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 353 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 354 | static void recover_worker(struct kthread_work *work) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 355 | { |
| 356 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work); |
| 357 | struct drm_device *dev = gpu->dev; |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 358 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 359 | struct msm_gem_submit *submit; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 360 | struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu); |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 361 | char *comm = NULL, *cmd = NULL; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 362 | int i; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 363 | |
Rob Clark | c28e2f2 | 2021-11-09 10:11:03 -0800 | [diff] [blame] | 364 | mutex_lock(&gpu->lock); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 365 | |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 366 | DRM_DEV_ERROR(dev->dev, "%s: hangcheck recover!\n", gpu->name); |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 367 | |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 368 | submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1); |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 369 | if (submit) { |
| 370 | struct task_struct *task; |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 371 | |
Jordan Crouse | b0fb660 | 2019-03-22 14:21:22 -0600 | [diff] [blame] | 372 | /* Increment the fault counts */ |
Rob Clark | 48dc424 | 2019-04-16 16:13:28 -0700 | [diff] [blame] | 373 | gpu->global_faults++; |
Jordan Crouse | b0fb660 | 2019-03-22 14:21:22 -0600 | [diff] [blame] | 374 | submit->queue->faults++; |
Rob Clark | 48dc424 | 2019-04-16 16:13:28 -0700 | [diff] [blame] | 375 | |
Sharat Masetty | 482f963 | 2018-10-12 14:26:56 +0530 | [diff] [blame] | 376 | task = get_pid_task(submit->pid, PIDTYPE_PID); |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 377 | if (task) { |
Sharat Masetty | 482f963 | 2018-10-12 14:26:56 +0530 | [diff] [blame] | 378 | comm = kstrdup(task->comm, GFP_KERNEL); |
Sharat Masetty | 482f963 | 2018-10-12 14:26:56 +0530 | [diff] [blame] | 379 | cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL); |
| 380 | put_task_struct(task); |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 381 | } |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 382 | |
| 383 | if (comm && cmd) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 384 | DRM_DEV_ERROR(dev->dev, "%s: offending task: %s (%s)\n", |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 385 | gpu->name, comm, cmd); |
| 386 | |
| 387 | msm_rd_dump_submit(priv->hangrd, submit, |
| 388 | "offending task: %s (%s)", comm, cmd); |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 389 | } else { |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 390 | msm_rd_dump_submit(priv->hangrd, submit, NULL); |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 391 | } |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 392 | } |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 393 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 394 | /* Record the crash state */ |
| 395 | pm_runtime_get_sync(&gpu->pdev->dev); |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 396 | msm_gpu_crashstate_capture(gpu, submit, comm, cmd); |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 397 | pm_runtime_put_sync(&gpu->pdev->dev); |
| 398 | |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 399 | kfree(cmd); |
| 400 | kfree(comm); |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 401 | |
| 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 Crouse | 7ddae82 | 2017-12-13 13:45:44 -0700 | [diff] [blame] | 407 | for (i = 0; i < gpu->nr_rings; i++) { |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 408 | 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 Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (msm_gpu_active(gpu)) { |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 423 | /* retire completed submits, plus the one that hung: */ |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 424 | retire_submits(gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 425 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 426 | pm_runtime_get_sync(&gpu->pdev->dev); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 427 | gpu->funcs->recover(gpu); |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 428 | pm_runtime_put_sync(&gpu->pdev->dev); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 429 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 430 | /* |
| 431 | * Replay all remaining submits starting with highest priority |
| 432 | * ring |
| 433 | */ |
Jordan Crouse | b1fc283 | 2017-10-20 11:07:01 -0600 | [diff] [blame] | 434 | for (i = 0; i < gpu->nr_rings; i++) { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 435 | struct msm_ringbuffer *ring = gpu->rb[i]; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 436 | unsigned long flags; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 437 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 438 | spin_lock_irqsave(&ring->submit_lock, flags); |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 439 | list_for_each_entry(submit, &ring->submits, node) |
Jordan Crouse | 15eb9ad | 2020-08-17 15:01:37 -0700 | [diff] [blame] | 440 | gpu->funcs->submit(gpu, submit); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 441 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 442 | } |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 443 | } |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 444 | |
Rob Clark | c28e2f2 | 2021-11-09 10:11:03 -0800 | [diff] [blame] | 445 | mutex_unlock(&gpu->lock); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 446 | |
| 447 | msm_gpu_retire(gpu); |
| 448 | } |
| 449 | |
Rob Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 450 | static void fault_worker(struct kthread_work *work) |
| 451 | { |
| 452 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, fault_work); |
Rob Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 453 | struct msm_gem_submit *submit; |
| 454 | struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu); |
| 455 | char *comm = NULL, *cmd = NULL; |
| 456 | |
Rob Clark | c28e2f2 | 2021-11-09 10:11:03 -0800 | [diff] [blame] | 457 | mutex_lock(&gpu->lock); |
Rob Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 458 | |
| 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 | |
| 488 | resume_smmu: |
| 489 | memset(&gpu->fault_info, 0, sizeof(gpu->fault_info)); |
| 490 | gpu->aspace->mmu->funcs->resume_translation(gpu->aspace->mmu); |
| 491 | |
Rob Clark | c28e2f2 | 2021-11-09 10:11:03 -0800 | [diff] [blame] | 492 | mutex_unlock(&gpu->lock); |
Rob Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 495 | static void hangcheck_timer_reset(struct msm_gpu *gpu) |
| 496 | { |
Samuel Iglesias Gonsalvez | 1d2fa58 | 2021-06-07 12:44:41 +0200 | [diff] [blame] | 497 | struct msm_drm_private *priv = gpu->dev->dev_private; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 498 | mod_timer(&gpu->hangcheck_timer, |
Samuel Iglesias Gonsalvez | 1d2fa58 | 2021-06-07 12:44:41 +0200 | [diff] [blame] | 499 | round_jiffies_up(jiffies + msecs_to_jiffies(priv->hangcheck_period))); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 502 | static void hangcheck_handler(struct timer_list *t) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 503 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 504 | struct msm_gpu *gpu = from_timer(gpu, t, hangcheck_timer); |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 505 | struct drm_device *dev = gpu->dev; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 506 | struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu); |
| 507 | uint32_t fence = ring->memptrs->fence; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 508 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 509 | if (fence != ring->hangcheck_fence) { |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 510 | /* some progress has been made.. ya! */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 511 | ring->hangcheck_fence = fence; |
Rob Clark | 5f3aee4 | 2021-11-09 10:11:04 -0800 | [diff] [blame] | 512 | } else if (fence_before(fence, ring->seqno)) { |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 513 | /* no progress and not done.. hung! */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 514 | ring->hangcheck_fence = fence; |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 515 | DRM_DEV_ERROR(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n", |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 516 | gpu->name, ring->id); |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 517 | DRM_DEV_ERROR(dev->dev, "%s: completed fence: %u\n", |
Rob Clark | 26791c4 | 2013-09-03 07:12:03 -0400 | [diff] [blame] | 518 | gpu->name, fence); |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 519 | DRM_DEV_ERROR(dev->dev, "%s: submitted fence: %u\n", |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 520 | gpu->name, ring->seqno); |
| 521 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 522 | kthread_queue_work(gpu->worker, &gpu->recover_work); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* if still more pending work, reset the hangcheck timer: */ |
Rob Clark | 5f3aee4 | 2021-11-09 10:11:04 -0800 | [diff] [blame] | 526 | if (fence_after(ring->seqno, ring->hangcheck_fence)) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 527 | hangcheck_timer_reset(gpu); |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 528 | |
| 529 | /* workaround for missing irq: */ |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 530 | msm_gpu_retire(gpu); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 534 | * Performance Counters: |
| 535 | */ |
| 536 | |
| 537 | /* called under perf_lock */ |
| 538 | static 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 | |
| 558 | static 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 | |
| 578 | out: |
| 579 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 580 | } |
| 581 | |
| 582 | void msm_gpu_perfcntr_start(struct msm_gpu *gpu) |
| 583 | { |
| 584 | unsigned long flags; |
| 585 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 586 | pm_runtime_get_sync(&gpu->pdev->dev); |
| 587 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 588 | 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 | |
| 598 | void msm_gpu_perfcntr_stop(struct msm_gpu *gpu) |
| 599 | { |
| 600 | gpu->perfcntr_active = false; |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 601 | pm_runtime_put_sync(&gpu->pdev->dev); |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* returns -errno or # of cntrs sampled */ |
| 605 | int 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 | |
| 625 | out: |
| 626 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 627 | |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | /* |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 632 | * Cmdstream submission/retirement: |
| 633 | */ |
| 634 | |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 635 | static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring, |
| 636 | struct msm_gem_submit *submit) |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 637 | { |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 638 | int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT; |
| 639 | volatile struct msm_gpu_submit_stats *stats; |
| 640 | u64 elapsed, clock = 0; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 641 | unsigned long flags; |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 642 | |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 643 | 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 Clark | be40596 | 2021-07-27 18:06:11 -0700 | [diff] [blame] | 657 | msm_submit_retire(submit); |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 658 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 659 | pm_runtime_mark_last_busy(&gpu->pdev->dev); |
| 660 | pm_runtime_put_autosuspend(&gpu->pdev->dev); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 661 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 662 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 663 | list_del(&submit->node); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 664 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 665 | |
Rob Clark | 9bc9557 | 2021-07-26 07:46:50 -0700 | [diff] [blame] | 666 | /* 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 Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 674 | msm_gem_submit_put(submit); |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 675 | } |
| 676 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 677 | static void retire_submits(struct msm_gpu *gpu) |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 678 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 679 | int i; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 680 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 681 | /* Retire the commits starting with highest priority */ |
Jordan Crouse | b1fc283 | 2017-10-20 11:07:01 -0600 | [diff] [blame] | 682 | for (i = 0; i < gpu->nr_rings; i++) { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 683 | struct msm_ringbuffer *ring = gpu->rb[i]; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 684 | |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 685 | while (true) { |
| 686 | struct msm_gem_submit *submit = NULL; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 687 | unsigned long flags; |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 688 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 689 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 690 | submit = list_first_entry_or_null(&ring->submits, |
| 691 | struct msm_gem_submit, node); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 692 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 693 | |
| 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 Clark | 1d8a5ca | 2021-07-27 18:06:14 -0700 | [diff] [blame] | 699 | if (submit && dma_fence_is_signaled(submit->hw_fence)) { |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 700 | retire_submit(gpu, ring, submit); |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 701 | } else { |
| 702 | break; |
| 703 | } |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 708 | static void retire_worker(struct kthread_work *work) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 709 | { |
| 710 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work); |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame] | 711 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 712 | retire_submits(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* call from irq handler to schedule work to retire bo's */ |
| 716 | void msm_gpu_retire(struct msm_gpu *gpu) |
| 717 | { |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 718 | 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 Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 723 | kthread_queue_work(gpu->worker, &gpu->retire_work); |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 724 | update_sw_cntrs(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* add bo's to gpu's ring, and kick gpu: */ |
Jordan Crouse | 15eb9ad | 2020-08-17 15:01:37 -0700 | [diff] [blame] | 728 | void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 729 | { |
| 730 | struct drm_device *dev = gpu->dev; |
| 731 | struct msm_drm_private *priv = dev->dev_private; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 732 | struct msm_ringbuffer *ring = submit->ring; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 733 | unsigned long flags; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 734 | |
Rob Clark | c28e2f2 | 2021-11-09 10:11:03 -0800 | [diff] [blame] | 735 | WARN_ON(!mutex_is_locked(&gpu->lock)); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 736 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 737 | pm_runtime_get_sync(&gpu->pdev->dev); |
| 738 | |
| 739 | msm_gpu_hw_init(gpu); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 740 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 741 | submit->seqno = ++ring->seqno; |
| 742 | |
Rob Clark | 998b9a5 | 2017-09-15 10:46:45 -0400 | [diff] [blame] | 743 | msm_rd_dump_submit(priv->rd, submit, NULL); |
Rob Clark | a7d3c95 | 2014-05-30 14:47:38 -0400 | [diff] [blame] | 744 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 745 | update_sw_cntrs(gpu); |
| 746 | |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 747 | /* |
| 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 Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 753 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 754 | list_add_tail(&submit->node, &ring->submits); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 755 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 756 | |
Rob Clark | 9bc9557 | 2021-07-26 07:46:50 -0700 | [diff] [blame] | 757 | /* 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 Crouse | 15eb9ad | 2020-08-17 15:01:37 -0700 | [diff] [blame] | 764 | gpu->funcs->submit(gpu, submit); |
Rob Clark | 1d054c9 | 2021-11-09 10:11:02 -0800 | [diff] [blame] | 765 | gpu->cur_ctx_seqno = submit->queue->ctx->seqno; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 766 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 767 | hangcheck_timer_reset(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Init/Cleanup: |
| 772 | */ |
| 773 | |
| 774 | static irqreturn_t irq_handler(int irq, void *data) |
| 775 | { |
| 776 | struct msm_gpu *gpu = data; |
| 777 | return gpu->funcs->irq(gpu); |
| 778 | } |
| 779 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 780 | static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu) |
| 781 | { |
Jordan Crouse | 8e3e791 | 2019-07-25 10:53:55 -0600 | [diff] [blame] | 782 | int ret = devm_clk_bulk_get_all(&pdev->dev, &gpu->grp_clks); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 783 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 784 | if (ret < 1) { |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 785 | gpu->nr_clocks = 0; |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 786 | return ret; |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 789 | gpu->nr_clocks = ret; |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 790 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 791 | gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks, |
| 792 | gpu->nr_clocks, "core"); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 793 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 794 | gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks, |
| 795 | gpu->nr_clocks, "rbbmtimer"); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 796 | |
| 797 | return 0; |
| 798 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 799 | |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 800 | /* Return a new address space for a msm_drm_private instance */ |
| 801 | struct msm_gem_address_space * |
Rob Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 802 | msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task) |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 803 | { |
| 804 | struct msm_gem_address_space *aspace = NULL; |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 805 | 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 Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 812 | if (gpu->funcs->create_private_address_space) { |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 813 | aspace = gpu->funcs->create_private_address_space(gpu); |
Rob Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 814 | if (!IS_ERR(aspace)) |
| 815 | aspace->pid = get_pid(task_pid(task)); |
| 816 | } |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 817 | |
| 818 | if (IS_ERR_OR_NULL(aspace)) |
| 819 | aspace = msm_gem_address_space_get(gpu->aspace); |
| 820 | |
| 821 | return aspace; |
| 822 | } |
| 823 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 824 | int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev, |
| 825 | struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs, |
Jordan Crouse | 5770fc7 | 2017-05-08 14:35:03 -0600 | [diff] [blame] | 826 | const char *name, struct msm_gpu_config *config) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 827 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 828 | int i, ret, nr_rings = config->nr_rings; |
| 829 | void *memptrs; |
| 830 | uint64_t memptrs_iova; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 831 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 832 | if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs))) |
| 833 | gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs); |
| 834 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 835 | gpu->dev = drm; |
| 836 | gpu->funcs = funcs; |
| 837 | gpu->name = name; |
| 838 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 839 | 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 Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 845 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 846 | sched_set_fifo_low(gpu->worker->task); |
| 847 | |
| 848 | INIT_LIST_HEAD(&gpu->active_list); |
Rob Clark | 9bc9557 | 2021-07-26 07:46:50 -0700 | [diff] [blame] | 849 | mutex_init(&gpu->active_lock); |
Rob Clark | c28e2f2 | 2021-11-09 10:11:03 -0800 | [diff] [blame] | 850 | mutex_init(&gpu->lock); |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 851 | kthread_init_work(&gpu->retire_work, retire_worker); |
| 852 | kthread_init_work(&gpu->recover_work, recover_worker); |
Rob Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 853 | kthread_init_work(&gpu->fault_work, fault_worker); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 854 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 855 | timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 856 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 857 | spin_lock_init(&gpu->perf_lock); |
| 858 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 859 | |
| 860 | /* Map registers: */ |
Jordan Crouse | 5770fc7 | 2017-05-08 14:35:03 -0600 | [diff] [blame] | 861 | gpu->mmio = msm_ioremap(pdev, config->ioname, name); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 862 | if (IS_ERR(gpu->mmio)) { |
| 863 | ret = PTR_ERR(gpu->mmio); |
| 864 | goto fail; |
| 865 | } |
| 866 | |
| 867 | /* Get Interrupt: */ |
Jordan Crouse | 878411a | 2018-12-18 11:32:36 -0700 | [diff] [blame] | 868 | gpu->irq = platform_get_irq(pdev, 0); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 869 | if (gpu->irq < 0) { |
| 870 | ret = gpu->irq; |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 871 | DRM_DEV_ERROR(drm->dev, "failed to get irq: %d\n", ret); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 872 | 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 Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 878 | DRM_DEV_ERROR(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 879 | goto fail; |
| 880 | } |
| 881 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 882 | ret = get_clocks(pdev, gpu); |
| 883 | if (ret) |
| 884 | goto fail; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 885 | |
Rob Clark | 720c3bb | 2017-01-30 11:30:58 -0500 | [diff] [blame] | 886 | gpu->ebi1_clk = msm_clk_get(pdev, "bus"); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 887 | 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 Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 902 | gpu->pdev = pdev; |
Rob Clark | 9cba405 | 2020-08-17 15:01:32 -0700 | [diff] [blame] | 903 | platform_set_drvdata(pdev, &gpu->adreno_smmu); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 904 | |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 905 | msm_devfreq_init(gpu); |
| 906 | |
Jordan Crouse | ccac7ce | 2020-05-22 16:03:15 -0600 | [diff] [blame] | 907 | |
| 908 | gpu->aspace = gpu->funcs->create_address_space(gpu, pdev); |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 909 | |
| 910 | if (gpu->aspace == NULL) |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 911 | DRM_DEV_INFO(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name); |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 912 | else if (IS_ERR(gpu->aspace)) { |
| 913 | ret = PTR_ERR(gpu->aspace); |
| 914 | goto fail; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 915 | } |
Rob Clark | a1ad352 | 2014-07-11 11:59:22 -0400 | [diff] [blame] | 916 | |
Jordan Crouse | 546ec7b | 2018-11-02 09:25:18 -0600 | [diff] [blame] | 917 | memptrs = msm_gem_kernel_new(drm, |
| 918 | sizeof(struct msm_rbmemptrs) * nr_rings, |
Jordan Crouse | 604234f | 2020-09-03 20:03:11 -0600 | [diff] [blame] | 919 | check_apriv(gpu, MSM_BO_UNCACHED), gpu->aspace, &gpu->memptrs_bo, |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 920 | &memptrs_iova); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 921 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 922 | if (IS_ERR(memptrs)) { |
| 923 | ret = PTR_ERR(memptrs); |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 924 | DRM_DEV_ERROR(drm->dev, "could not allocate memptrs: %d\n", ret); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 925 | goto fail; |
| 926 | } |
| 927 | |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 928 | msm_gem_object_set_name(gpu->memptrs_bo, "memptrs"); |
| 929 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 930 | if (nr_rings > ARRAY_SIZE(gpu->rb)) { |
Arnd Bergmann | 39ae0d3 | 2017-08-03 13:50:48 +0200 | [diff] [blame] | 931 | DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n", |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 932 | ARRAY_SIZE(gpu->rb)); |
| 933 | nr_rings = ARRAY_SIZE(gpu->rb); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 936 | /* 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 Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 942 | DRM_DEV_ERROR(drm->dev, |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 943 | "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 Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 953 | return 0; |
| 954 | |
| 955 | fail: |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 956 | for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { |
| 957 | msm_ringbuffer_destroy(gpu->rb[i]); |
| 958 | gpu->rb[i] = NULL; |
| 959 | } |
| 960 | |
Rob Clark | 030af2b | 2021-07-27 18:06:08 -0700 | [diff] [blame] | 961 | msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 962 | |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 963 | platform_set_drvdata(pdev, NULL); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | void msm_gpu_cleanup(struct msm_gpu *gpu) |
| 968 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 969 | int i; |
| 970 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 971 | DBG("%s", gpu->name); |
| 972 | |
| 973 | WARN_ON(!list_empty(&gpu->active_list)); |
| 974 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 975 | for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { |
| 976 | msm_ringbuffer_destroy(gpu->rb[i]); |
| 977 | gpu->rb[i] = NULL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 978 | } |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 979 | |
Rob Clark | 030af2b | 2021-07-27 18:06:08 -0700 | [diff] [blame] | 980 | msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 981 | |
| 982 | if (!IS_ERR_OR_NULL(gpu->aspace)) { |
Drew Davenport | 53bf7f7 | 2019-09-16 14:11:54 -0600 | [diff] [blame] | 983 | gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu); |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 984 | msm_gem_address_space_put(gpu->aspace); |
| 985 | } |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 986 | |
| 987 | if (gpu->worker) { |
| 988 | kthread_destroy_worker(gpu->worker); |
| 989 | } |
Akhil P Oommen | ec793cf | 2020-10-30 16:17:10 +0530 | [diff] [blame] | 990 | |
Rob Clark | af5b4ff | 2021-07-26 07:46:48 -0700 | [diff] [blame] | 991 | msm_devfreq_cleanup(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 992 | } |