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 | cb1e381 | 2017-06-13 09:15:36 -0400 | [diff] [blame] | 153 | WARN_ON(!mutex_is_locked(&gpu->dev->struct_mutex)); |
| 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) { |
| 175 | if (submit->seqno > fence) |
| 176 | break; |
| 177 | |
| 178 | msm_update_fence(submit->ring->fctx, |
| 179 | submit->fence->seqno); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 180 | dma_fence_signal(submit->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 | |
Rob Clark | e515af8 | 2020-02-18 13:20:12 -0800 | [diff] [blame] | 299 | for (i = 0; i < submit->nr_bos; i++) { |
| 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 | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 364 | mutex_lock(&dev->struct_mutex); |
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 | |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 383 | /* msm_rd_dump_submit() needs bo locked to dump: */ |
| 384 | for (i = 0; i < submit->nr_bos; i++) |
| 385 | msm_gem_lock(&submit->bos[i].obj->base); |
| 386 | |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 387 | if (comm && cmd) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 388 | DRM_DEV_ERROR(dev->dev, "%s: offending task: %s (%s)\n", |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 389 | gpu->name, comm, cmd); |
| 390 | |
| 391 | msm_rd_dump_submit(priv->hangrd, submit, |
| 392 | "offending task: %s (%s)", comm, cmd); |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 393 | } else { |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 394 | msm_rd_dump_submit(priv->hangrd, submit, NULL); |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | for (i = 0; i < submit->nr_bos; i++) |
| 398 | msm_gem_unlock(&submit->bos[i].obj->base); |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 399 | } |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 400 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 401 | /* Record the crash state */ |
| 402 | pm_runtime_get_sync(&gpu->pdev->dev); |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 403 | msm_gpu_crashstate_capture(gpu, submit, comm, cmd); |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 404 | pm_runtime_put_sync(&gpu->pdev->dev); |
| 405 | |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 406 | kfree(cmd); |
| 407 | kfree(comm); |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * Update all the rings with the latest and greatest fence.. this |
| 411 | * needs to happen after msm_rd_dump_submit() to ensure that the |
| 412 | * bo's referenced by the offending submit are still around. |
| 413 | */ |
Jordan Crouse | 7ddae82 | 2017-12-13 13:45:44 -0700 | [diff] [blame] | 414 | for (i = 0; i < gpu->nr_rings; i++) { |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 415 | struct msm_ringbuffer *ring = gpu->rb[i]; |
| 416 | |
| 417 | uint32_t fence = ring->memptrs->fence; |
| 418 | |
| 419 | /* |
| 420 | * For the current (faulting?) ring/submit advance the fence by |
| 421 | * one more to clear the faulting submit |
| 422 | */ |
| 423 | if (ring == cur_ring) |
| 424 | fence++; |
| 425 | |
| 426 | update_fences(gpu, ring, fence); |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | if (msm_gpu_active(gpu)) { |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 430 | /* retire completed submits, plus the one that hung: */ |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 431 | retire_submits(gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 432 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 433 | pm_runtime_get_sync(&gpu->pdev->dev); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 434 | gpu->funcs->recover(gpu); |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 435 | pm_runtime_put_sync(&gpu->pdev->dev); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 436 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 437 | /* |
| 438 | * Replay all remaining submits starting with highest priority |
| 439 | * ring |
| 440 | */ |
Jordan Crouse | b1fc283 | 2017-10-20 11:07:01 -0600 | [diff] [blame] | 441 | for (i = 0; i < gpu->nr_rings; i++) { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 442 | struct msm_ringbuffer *ring = gpu->rb[i]; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 443 | unsigned long flags; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 444 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 445 | spin_lock_irqsave(&ring->submit_lock, flags); |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 446 | list_for_each_entry(submit, &ring->submits, node) |
Jordan Crouse | 15eb9ad | 2020-08-17 15:01:37 -0700 | [diff] [blame] | 447 | gpu->funcs->submit(gpu, submit); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 448 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 449 | } |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 450 | } |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 451 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 452 | mutex_unlock(&dev->struct_mutex); |
| 453 | |
| 454 | msm_gpu_retire(gpu); |
| 455 | } |
| 456 | |
Rob Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 457 | static void fault_worker(struct kthread_work *work) |
| 458 | { |
| 459 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, fault_work); |
| 460 | struct drm_device *dev = gpu->dev; |
| 461 | struct msm_gem_submit *submit; |
| 462 | struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu); |
| 463 | char *comm = NULL, *cmd = NULL; |
| 464 | |
| 465 | mutex_lock(&dev->struct_mutex); |
| 466 | |
| 467 | submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1); |
| 468 | if (submit && submit->fault_dumped) |
| 469 | goto resume_smmu; |
| 470 | |
| 471 | if (submit) { |
| 472 | struct task_struct *task; |
| 473 | |
| 474 | task = get_pid_task(submit->pid, PIDTYPE_PID); |
| 475 | if (task) { |
| 476 | comm = kstrdup(task->comm, GFP_KERNEL); |
| 477 | cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL); |
| 478 | put_task_struct(task); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * When we get GPU iova faults, we can get 1000s of them, |
| 483 | * but we really only want to log the first one. |
| 484 | */ |
| 485 | submit->fault_dumped = true; |
| 486 | } |
| 487 | |
| 488 | /* Record the crash state */ |
| 489 | pm_runtime_get_sync(&gpu->pdev->dev); |
| 490 | msm_gpu_crashstate_capture(gpu, submit, comm, cmd); |
| 491 | pm_runtime_put_sync(&gpu->pdev->dev); |
| 492 | |
| 493 | kfree(cmd); |
| 494 | kfree(comm); |
| 495 | |
| 496 | resume_smmu: |
| 497 | memset(&gpu->fault_info, 0, sizeof(gpu->fault_info)); |
| 498 | gpu->aspace->mmu->funcs->resume_translation(gpu->aspace->mmu); |
| 499 | |
| 500 | mutex_unlock(&dev->struct_mutex); |
| 501 | } |
| 502 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 503 | static void hangcheck_timer_reset(struct msm_gpu *gpu) |
| 504 | { |
Samuel Iglesias Gonsalvez | 1d2fa58 | 2021-06-07 12:44:41 +0200 | [diff] [blame] | 505 | struct msm_drm_private *priv = gpu->dev->dev_private; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 506 | mod_timer(&gpu->hangcheck_timer, |
Samuel Iglesias Gonsalvez | 1d2fa58 | 2021-06-07 12:44:41 +0200 | [diff] [blame] | 507 | round_jiffies_up(jiffies + msecs_to_jiffies(priv->hangcheck_period))); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 510 | static void hangcheck_handler(struct timer_list *t) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 511 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 512 | struct msm_gpu *gpu = from_timer(gpu, t, hangcheck_timer); |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 513 | struct drm_device *dev = gpu->dev; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 514 | struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu); |
| 515 | uint32_t fence = ring->memptrs->fence; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 516 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 517 | if (fence != ring->hangcheck_fence) { |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 518 | /* some progress has been made.. ya! */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 519 | ring->hangcheck_fence = fence; |
| 520 | } else if (fence < ring->seqno) { |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 521 | /* no progress and not done.. hung! */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 522 | ring->hangcheck_fence = fence; |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 523 | 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] | 524 | gpu->name, ring->id); |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 525 | DRM_DEV_ERROR(dev->dev, "%s: completed fence: %u\n", |
Rob Clark | 26791c4 | 2013-09-03 07:12:03 -0400 | [diff] [blame] | 526 | gpu->name, fence); |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 527 | DRM_DEV_ERROR(dev->dev, "%s: submitted fence: %u\n", |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 528 | gpu->name, ring->seqno); |
| 529 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 530 | kthread_queue_work(gpu->worker, &gpu->recover_work); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* if still more pending work, reset the hangcheck timer: */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 534 | if (ring->seqno > ring->hangcheck_fence) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 535 | hangcheck_timer_reset(gpu); |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 536 | |
| 537 | /* workaround for missing irq: */ |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 538 | msm_gpu_retire(gpu); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 542 | * Performance Counters: |
| 543 | */ |
| 544 | |
| 545 | /* called under perf_lock */ |
| 546 | static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs) |
| 547 | { |
| 548 | uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)]; |
| 549 | int i, n = min(ncntrs, gpu->num_perfcntrs); |
| 550 | |
| 551 | /* read current values: */ |
| 552 | for (i = 0; i < gpu->num_perfcntrs; i++) |
| 553 | current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg); |
| 554 | |
| 555 | /* update cntrs: */ |
| 556 | for (i = 0; i < n; i++) |
| 557 | cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i]; |
| 558 | |
| 559 | /* save current values: */ |
| 560 | for (i = 0; i < gpu->num_perfcntrs; i++) |
| 561 | gpu->last_cntrs[i] = current_cntrs[i]; |
| 562 | |
| 563 | return n; |
| 564 | } |
| 565 | |
| 566 | static void update_sw_cntrs(struct msm_gpu *gpu) |
| 567 | { |
| 568 | ktime_t time; |
| 569 | uint32_t elapsed; |
| 570 | unsigned long flags; |
| 571 | |
| 572 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 573 | if (!gpu->perfcntr_active) |
| 574 | goto out; |
| 575 | |
| 576 | time = ktime_get(); |
| 577 | elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time)); |
| 578 | |
| 579 | gpu->totaltime += elapsed; |
| 580 | if (gpu->last_sample.active) |
| 581 | gpu->activetime += elapsed; |
| 582 | |
| 583 | gpu->last_sample.active = msm_gpu_active(gpu); |
| 584 | gpu->last_sample.time = time; |
| 585 | |
| 586 | out: |
| 587 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 588 | } |
| 589 | |
| 590 | void msm_gpu_perfcntr_start(struct msm_gpu *gpu) |
| 591 | { |
| 592 | unsigned long flags; |
| 593 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 594 | pm_runtime_get_sync(&gpu->pdev->dev); |
| 595 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 596 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 597 | /* we could dynamically enable/disable perfcntr registers too.. */ |
| 598 | gpu->last_sample.active = msm_gpu_active(gpu); |
| 599 | gpu->last_sample.time = ktime_get(); |
| 600 | gpu->activetime = gpu->totaltime = 0; |
| 601 | gpu->perfcntr_active = true; |
| 602 | update_hw_cntrs(gpu, 0, NULL); |
| 603 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 604 | } |
| 605 | |
| 606 | void msm_gpu_perfcntr_stop(struct msm_gpu *gpu) |
| 607 | { |
| 608 | gpu->perfcntr_active = false; |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 609 | pm_runtime_put_sync(&gpu->pdev->dev); |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* returns -errno or # of cntrs sampled */ |
| 613 | int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime, |
| 614 | uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs) |
| 615 | { |
| 616 | unsigned long flags; |
| 617 | int ret; |
| 618 | |
| 619 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 620 | |
| 621 | if (!gpu->perfcntr_active) { |
| 622 | ret = -EINVAL; |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | *activetime = gpu->activetime; |
| 627 | *totaltime = gpu->totaltime; |
| 628 | |
| 629 | gpu->activetime = gpu->totaltime = 0; |
| 630 | |
| 631 | ret = update_hw_cntrs(gpu, ncntrs, cntrs); |
| 632 | |
| 633 | out: |
| 634 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 635 | |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | /* |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 640 | * Cmdstream submission/retirement: |
| 641 | */ |
| 642 | |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 643 | static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring, |
| 644 | struct msm_gem_submit *submit) |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 645 | { |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 646 | int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT; |
| 647 | volatile struct msm_gpu_submit_stats *stats; |
| 648 | u64 elapsed, clock = 0; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 649 | unsigned long flags; |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 650 | int i; |
| 651 | |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 652 | stats = &ring->memptrs->stats[index]; |
| 653 | /* Convert 19.2Mhz alwayson ticks to nanoseconds for elapsed time */ |
| 654 | elapsed = (stats->alwayson_end - stats->alwayson_start) * 10000; |
| 655 | do_div(elapsed, 192); |
| 656 | |
| 657 | /* Calculate the clock frequency from the number of CP cycles */ |
| 658 | if (elapsed) { |
| 659 | clock = (stats->cpcycles_end - stats->cpcycles_start) * 1000; |
| 660 | do_div(clock, elapsed); |
| 661 | } |
| 662 | |
| 663 | trace_msm_gpu_submit_retired(submit, elapsed, clock, |
| 664 | stats->alwayson_start, stats->alwayson_end); |
| 665 | |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 666 | for (i = 0; i < submit->nr_bos; i++) { |
Rob Clark | ab5c54c | 2020-11-16 09:48:49 -0800 | [diff] [blame] | 667 | struct drm_gem_object *obj = &submit->bos[i].obj->base; |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 668 | |
Rob Clark | ab5c54c | 2020-11-16 09:48:49 -0800 | [diff] [blame] | 669 | msm_gem_lock(obj); |
| 670 | msm_gem_active_put(obj); |
| 671 | msm_gem_unpin_iova_locked(obj, submit->aspace); |
| 672 | msm_gem_unlock(obj); |
| 673 | drm_gem_object_put(obj); |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 674 | } |
| 675 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 676 | pm_runtime_mark_last_busy(&gpu->pdev->dev); |
| 677 | pm_runtime_put_autosuspend(&gpu->pdev->dev); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 678 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 679 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 680 | list_del(&submit->node); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 681 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 682 | |
| 683 | msm_gem_submit_put(submit); |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 684 | } |
| 685 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 686 | static void retire_submits(struct msm_gpu *gpu) |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 687 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 688 | int i; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 689 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 690 | /* Retire the commits starting with highest priority */ |
Jordan Crouse | b1fc283 | 2017-10-20 11:07:01 -0600 | [diff] [blame] | 691 | for (i = 0; i < gpu->nr_rings; i++) { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 692 | struct msm_ringbuffer *ring = gpu->rb[i]; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 693 | |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 694 | while (true) { |
| 695 | struct msm_gem_submit *submit = NULL; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 696 | unsigned long flags; |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 697 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 698 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 699 | submit = list_first_entry_or_null(&ring->submits, |
| 700 | struct msm_gem_submit, node); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 701 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * If no submit, we are done. If submit->fence hasn't |
| 705 | * been signalled, then later submits are not signalled |
| 706 | * either, so we are also done. |
| 707 | */ |
| 708 | if (submit && dma_fence_is_signaled(submit->fence)) { |
Jordan Crouse | 4241db4 | 2018-11-02 09:25:21 -0600 | [diff] [blame] | 709 | retire_submit(gpu, ring, submit); |
Rob Clark | 77d2052 | 2020-10-23 09:51:16 -0700 | [diff] [blame] | 710 | } else { |
| 711 | break; |
| 712 | } |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 717 | static void retire_worker(struct kthread_work *work) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 718 | { |
| 719 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work); |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame] | 720 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 721 | retire_submits(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /* call from irq handler to schedule work to retire bo's */ |
| 725 | void msm_gpu_retire(struct msm_gpu *gpu) |
| 726 | { |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 727 | int i; |
| 728 | |
| 729 | for (i = 0; i < gpu->nr_rings; i++) |
| 730 | update_fences(gpu, gpu->rb[i], gpu->rb[i]->memptrs->fence); |
| 731 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 732 | kthread_queue_work(gpu->worker, &gpu->retire_work); |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 733 | update_sw_cntrs(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* add bo's to gpu's ring, and kick gpu: */ |
Jordan Crouse | 15eb9ad | 2020-08-17 15:01:37 -0700 | [diff] [blame] | 737 | 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] | 738 | { |
| 739 | struct drm_device *dev = gpu->dev; |
| 740 | struct msm_drm_private *priv = dev->dev_private; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 741 | struct msm_ringbuffer *ring = submit->ring; |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 742 | unsigned long flags; |
Rob Clark | f44d32c | 2016-06-16 16:37:38 -0400 | [diff] [blame] | 743 | int i; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 744 | |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 745 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 746 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 747 | pm_runtime_get_sync(&gpu->pdev->dev); |
| 748 | |
| 749 | msm_gpu_hw_init(gpu); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 750 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 751 | submit->seqno = ++ring->seqno; |
| 752 | |
Rob Clark | 998b9a5 | 2017-09-15 10:46:45 -0400 | [diff] [blame] | 753 | msm_rd_dump_submit(priv->rd, submit, NULL); |
Rob Clark | a7d3c95 | 2014-05-30 14:47:38 -0400 | [diff] [blame] | 754 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 755 | update_sw_cntrs(gpu); |
| 756 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 757 | for (i = 0; i < submit->nr_bos; i++) { |
| 758 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 759 | struct drm_gem_object *drm_obj = &msm_obj->base; |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 760 | uint64_t iova; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 761 | |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 762 | /* submit takes a reference to the bo and iova until retired: */ |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 763 | drm_gem_object_get(&msm_obj->base); |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 764 | msm_gem_get_and_pin_iova_locked(&msm_obj->base, submit->aspace, &iova); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 765 | |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 766 | if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE) |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 767 | dma_resv_add_excl_fence(drm_obj->resv, submit->fence); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 768 | else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ) |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 769 | dma_resv_add_shared_fence(drm_obj->resv, submit->fence); |
| 770 | |
| 771 | msm_gem_active_get(drm_obj, gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 772 | } |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 773 | |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 774 | /* |
| 775 | * ring->submits holds a ref to the submit, to deal with the case |
| 776 | * that a submit completes before msm_ioctl_gem_submit() returns. |
| 777 | */ |
| 778 | msm_gem_submit_get(submit); |
| 779 | |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 780 | spin_lock_irqsave(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 781 | list_add_tail(&submit->node, &ring->submits); |
Rob Clark | 298287f | 2021-07-26 07:43:58 -0700 | [diff] [blame] | 782 | spin_unlock_irqrestore(&ring->submit_lock, flags); |
Rob Clark | 964d2f9 | 2020-10-23 09:51:17 -0700 | [diff] [blame] | 783 | |
Jordan Crouse | 15eb9ad | 2020-08-17 15:01:37 -0700 | [diff] [blame] | 784 | gpu->funcs->submit(gpu, submit); |
| 785 | priv->lastctx = submit->queue->ctx; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 786 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 787 | hangcheck_timer_reset(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Init/Cleanup: |
| 792 | */ |
| 793 | |
| 794 | static irqreturn_t irq_handler(int irq, void *data) |
| 795 | { |
| 796 | struct msm_gpu *gpu = data; |
| 797 | return gpu->funcs->irq(gpu); |
| 798 | } |
| 799 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 800 | static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu) |
| 801 | { |
Jordan Crouse | 8e3e791 | 2019-07-25 10:53:55 -0600 | [diff] [blame] | 802 | int ret = devm_clk_bulk_get_all(&pdev->dev, &gpu->grp_clks); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 803 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 804 | if (ret < 1) { |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 805 | gpu->nr_clocks = 0; |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 806 | return ret; |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 809 | gpu->nr_clocks = ret; |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 810 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 811 | gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks, |
| 812 | gpu->nr_clocks, "core"); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 813 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame] | 814 | gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks, |
| 815 | gpu->nr_clocks, "rbbmtimer"); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 816 | |
| 817 | return 0; |
| 818 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 819 | |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 820 | /* Return a new address space for a msm_drm_private instance */ |
| 821 | struct msm_gem_address_space * |
Rob Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 822 | 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] | 823 | { |
| 824 | struct msm_gem_address_space *aspace = NULL; |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 825 | if (!gpu) |
| 826 | return NULL; |
| 827 | |
| 828 | /* |
| 829 | * If the target doesn't support private address spaces then return |
| 830 | * the global one |
| 831 | */ |
Rob Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 832 | if (gpu->funcs->create_private_address_space) { |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 833 | aspace = gpu->funcs->create_private_address_space(gpu); |
Rob Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 834 | if (!IS_ERR(aspace)) |
| 835 | aspace->pid = get_pid(task_pid(task)); |
| 836 | } |
Jordan Crouse | 933415e | 2020-08-17 15:01:40 -0700 | [diff] [blame] | 837 | |
| 838 | if (IS_ERR_OR_NULL(aspace)) |
| 839 | aspace = msm_gem_address_space_get(gpu->aspace); |
| 840 | |
| 841 | return aspace; |
| 842 | } |
| 843 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 844 | int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev, |
| 845 | struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs, |
Jordan Crouse | 5770fc7 | 2017-05-08 14:35:03 -0600 | [diff] [blame] | 846 | const char *name, struct msm_gpu_config *config) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 847 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 848 | int i, ret, nr_rings = config->nr_rings; |
| 849 | void *memptrs; |
| 850 | uint64_t memptrs_iova; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 851 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 852 | if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs))) |
| 853 | gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs); |
| 854 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 855 | gpu->dev = drm; |
| 856 | gpu->funcs = funcs; |
| 857 | gpu->name = name; |
| 858 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 859 | gpu->worker = kthread_create_worker(0, "%s-worker", gpu->name); |
| 860 | if (IS_ERR(gpu->worker)) { |
| 861 | ret = PTR_ERR(gpu->worker); |
| 862 | gpu->worker = NULL; |
| 863 | goto fail; |
| 864 | } |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 865 | |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 866 | sched_set_fifo_low(gpu->worker->task); |
| 867 | |
| 868 | INIT_LIST_HEAD(&gpu->active_list); |
| 869 | kthread_init_work(&gpu->retire_work, retire_worker); |
| 870 | kthread_init_work(&gpu->recover_work, recover_worker); |
Rob Clark | e25e92e | 2021-06-10 14:44:13 -0700 | [diff] [blame] | 871 | kthread_init_work(&gpu->fault_work, fault_worker); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 872 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 873 | timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 874 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 875 | spin_lock_init(&gpu->perf_lock); |
| 876 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 877 | |
| 878 | /* Map registers: */ |
Jordan Crouse | 5770fc7 | 2017-05-08 14:35:03 -0600 | [diff] [blame] | 879 | gpu->mmio = msm_ioremap(pdev, config->ioname, name); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 880 | if (IS_ERR(gpu->mmio)) { |
| 881 | ret = PTR_ERR(gpu->mmio); |
| 882 | goto fail; |
| 883 | } |
| 884 | |
| 885 | /* Get Interrupt: */ |
Jordan Crouse | 878411a | 2018-12-18 11:32:36 -0700 | [diff] [blame] | 886 | gpu->irq = platform_get_irq(pdev, 0); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 887 | if (gpu->irq < 0) { |
| 888 | ret = gpu->irq; |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 889 | DRM_DEV_ERROR(drm->dev, "failed to get irq: %d\n", ret); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 890 | goto fail; |
| 891 | } |
| 892 | |
| 893 | ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, |
| 894 | IRQF_TRIGGER_HIGH, gpu->name, gpu); |
| 895 | if (ret) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 896 | 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] | 897 | goto fail; |
| 898 | } |
| 899 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 900 | ret = get_clocks(pdev, gpu); |
| 901 | if (ret) |
| 902 | goto fail; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 903 | |
Rob Clark | 720c3bb | 2017-01-30 11:30:58 -0500 | [diff] [blame] | 904 | gpu->ebi1_clk = msm_clk_get(pdev, "bus"); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 905 | DBG("ebi1_clk: %p", gpu->ebi1_clk); |
| 906 | if (IS_ERR(gpu->ebi1_clk)) |
| 907 | gpu->ebi1_clk = NULL; |
| 908 | |
| 909 | /* Acquire regulators: */ |
| 910 | gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd"); |
| 911 | DBG("gpu_reg: %p", gpu->gpu_reg); |
| 912 | if (IS_ERR(gpu->gpu_reg)) |
| 913 | gpu->gpu_reg = NULL; |
| 914 | |
| 915 | gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx"); |
| 916 | DBG("gpu_cx: %p", gpu->gpu_cx); |
| 917 | if (IS_ERR(gpu->gpu_cx)) |
| 918 | gpu->gpu_cx = NULL; |
| 919 | |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 920 | gpu->pdev = pdev; |
Rob Clark | 9cba405 | 2020-08-17 15:01:32 -0700 | [diff] [blame] | 921 | platform_set_drvdata(pdev, &gpu->adreno_smmu); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 922 | |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 923 | msm_devfreq_init(gpu); |
| 924 | |
Jordan Crouse | ccac7ce | 2020-05-22 16:03:15 -0600 | [diff] [blame] | 925 | |
| 926 | gpu->aspace = gpu->funcs->create_address_space(gpu, pdev); |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 927 | |
| 928 | if (gpu->aspace == NULL) |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 929 | 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] | 930 | else if (IS_ERR(gpu->aspace)) { |
| 931 | ret = PTR_ERR(gpu->aspace); |
| 932 | goto fail; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 933 | } |
Rob Clark | a1ad352 | 2014-07-11 11:59:22 -0400 | [diff] [blame] | 934 | |
Jordan Crouse | 546ec7b | 2018-11-02 09:25:18 -0600 | [diff] [blame] | 935 | memptrs = msm_gem_kernel_new(drm, |
| 936 | sizeof(struct msm_rbmemptrs) * nr_rings, |
Jordan Crouse | 604234f | 2020-09-03 20:03:11 -0600 | [diff] [blame] | 937 | check_apriv(gpu, MSM_BO_UNCACHED), gpu->aspace, &gpu->memptrs_bo, |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 938 | &memptrs_iova); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 939 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 940 | if (IS_ERR(memptrs)) { |
| 941 | ret = PTR_ERR(memptrs); |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 942 | DRM_DEV_ERROR(drm->dev, "could not allocate memptrs: %d\n", ret); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 943 | goto fail; |
| 944 | } |
| 945 | |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 946 | msm_gem_object_set_name(gpu->memptrs_bo, "memptrs"); |
| 947 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 948 | if (nr_rings > ARRAY_SIZE(gpu->rb)) { |
Arnd Bergmann | 39ae0d3 | 2017-08-03 13:50:48 +0200 | [diff] [blame] | 949 | DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n", |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 950 | ARRAY_SIZE(gpu->rb)); |
| 951 | nr_rings = ARRAY_SIZE(gpu->rb); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 952 | } |
| 953 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 954 | /* Create ringbuffer(s): */ |
| 955 | for (i = 0; i < nr_rings; i++) { |
| 956 | gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova); |
| 957 | |
| 958 | if (IS_ERR(gpu->rb[i])) { |
| 959 | ret = PTR_ERR(gpu->rb[i]); |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 960 | DRM_DEV_ERROR(drm->dev, |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 961 | "could not create ringbuffer %d: %d\n", i, ret); |
| 962 | goto fail; |
| 963 | } |
| 964 | |
| 965 | memptrs += sizeof(struct msm_rbmemptrs); |
| 966 | memptrs_iova += sizeof(struct msm_rbmemptrs); |
| 967 | } |
| 968 | |
| 969 | gpu->nr_rings = nr_rings; |
| 970 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 971 | return 0; |
| 972 | |
| 973 | fail: |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 974 | for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { |
| 975 | msm_ringbuffer_destroy(gpu->rb[i]); |
| 976 | gpu->rb[i] = NULL; |
| 977 | } |
| 978 | |
Jordan Crouse | 1e29dff | 2018-11-07 15:35:46 -0700 | [diff] [blame] | 979 | msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace, false); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 980 | |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 981 | platform_set_drvdata(pdev, NULL); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | void msm_gpu_cleanup(struct msm_gpu *gpu) |
| 986 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 987 | int i; |
| 988 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 989 | DBG("%s", gpu->name); |
| 990 | |
| 991 | WARN_ON(!list_empty(&gpu->active_list)); |
| 992 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 993 | for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { |
| 994 | msm_ringbuffer_destroy(gpu->rb[i]); |
| 995 | gpu->rb[i] = NULL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 996 | } |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 997 | |
Jordan Crouse | 1e29dff | 2018-11-07 15:35:46 -0700 | [diff] [blame] | 998 | msm_gem_kernel_put(gpu->memptrs_bo, gpu->aspace, false); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 999 | |
| 1000 | if (!IS_ERR_OR_NULL(gpu->aspace)) { |
Drew Davenport | 53bf7f7 | 2019-09-16 14:11:54 -0600 | [diff] [blame] | 1001 | gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu); |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 1002 | msm_gem_address_space_put(gpu->aspace); |
| 1003 | } |
Rob Clark | 7e68829 | 2020-10-19 14:10:51 -0700 | [diff] [blame] | 1004 | |
| 1005 | if (gpu->worker) { |
| 1006 | kthread_destroy_worker(gpu->worker); |
| 1007 | } |
Akhil P Oommen | ec793cf | 2020-10-30 16:17:10 +0530 | [diff] [blame] | 1008 | |
Rob Clark | af5b4ff | 2021-07-26 07:46:48 -0700 | [diff] [blame^] | 1009 | msm_devfreq_cleanup(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 1010 | } |