Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Red Hat |
| 3 | * Author: Rob Clark <robdclark@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "msm_gpu.h" |
| 19 | #include "msm_gem.h" |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 20 | #include "msm_mmu.h" |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 21 | #include "msm_fence.h" |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 22 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 23 | #include <generated/utsrelease.h> |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 24 | #include <linux/string_helpers.h> |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 25 | #include <linux/pm_opp.h> |
| 26 | #include <linux/devfreq.h> |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 27 | #include <linux/devcoredump.h> |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Power Management: |
| 31 | */ |
| 32 | |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 33 | static int msm_devfreq_target(struct device *dev, unsigned long *freq, |
| 34 | u32 flags) |
| 35 | { |
| 36 | struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev)); |
| 37 | struct dev_pm_opp *opp; |
| 38 | |
| 39 | opp = devfreq_recommended_opp(dev, freq, flags); |
| 40 | |
| 41 | if (IS_ERR(opp)) |
| 42 | return PTR_ERR(opp); |
| 43 | |
| 44 | clk_set_rate(gpu->core_clk, *freq); |
| 45 | dev_pm_opp_put(opp); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int msm_devfreq_get_dev_status(struct device *dev, |
| 51 | struct devfreq_dev_status *status) |
| 52 | { |
| 53 | struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev)); |
| 54 | u64 cycles; |
| 55 | u32 freq = ((u32) status->current_frequency) / 1000000; |
| 56 | ktime_t time; |
| 57 | |
| 58 | status->current_frequency = (unsigned long) clk_get_rate(gpu->core_clk); |
| 59 | gpu->funcs->gpu_busy(gpu, &cycles); |
| 60 | |
| 61 | status->busy_time = ((u32) (cycles - gpu->devfreq.busy_cycles)) / freq; |
| 62 | |
| 63 | gpu->devfreq.busy_cycles = cycles; |
| 64 | |
| 65 | time = ktime_get(); |
| 66 | status->total_time = ktime_us_delta(time, gpu->devfreq.time); |
| 67 | gpu->devfreq.time = time; |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int msm_devfreq_get_cur_freq(struct device *dev, unsigned long *freq) |
| 73 | { |
| 74 | struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev)); |
| 75 | |
| 76 | *freq = (unsigned long) clk_get_rate(gpu->core_clk); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static struct devfreq_dev_profile msm_devfreq_profile = { |
| 82 | .polling_ms = 10, |
| 83 | .target = msm_devfreq_target, |
| 84 | .get_dev_status = msm_devfreq_get_dev_status, |
| 85 | .get_cur_freq = msm_devfreq_get_cur_freq, |
| 86 | }; |
| 87 | |
| 88 | static void msm_devfreq_init(struct msm_gpu *gpu) |
| 89 | { |
| 90 | /* We need target support to do devfreq */ |
| 91 | if (!gpu->funcs->gpu_busy) |
| 92 | return; |
| 93 | |
| 94 | msm_devfreq_profile.initial_freq = gpu->fast_rate; |
| 95 | |
| 96 | /* |
| 97 | * Don't set the freq_table or max_state and let devfreq build the table |
| 98 | * from OPP |
| 99 | */ |
| 100 | |
| 101 | gpu->devfreq.devfreq = devm_devfreq_add_device(&gpu->pdev->dev, |
| 102 | &msm_devfreq_profile, "simple_ondemand", NULL); |
| 103 | |
| 104 | if (IS_ERR(gpu->devfreq.devfreq)) { |
| 105 | dev_err(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n"); |
| 106 | gpu->devfreq.devfreq = NULL; |
| 107 | } |
| 108 | } |
| 109 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 110 | static int enable_pwrrail(struct msm_gpu *gpu) |
| 111 | { |
| 112 | struct drm_device *dev = gpu->dev; |
| 113 | int ret = 0; |
| 114 | |
| 115 | if (gpu->gpu_reg) { |
| 116 | ret = regulator_enable(gpu->gpu_reg); |
| 117 | if (ret) { |
| 118 | dev_err(dev->dev, "failed to enable 'gpu_reg': %d\n", ret); |
| 119 | return ret; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (gpu->gpu_cx) { |
| 124 | ret = regulator_enable(gpu->gpu_cx); |
| 125 | if (ret) { |
| 126 | dev_err(dev->dev, "failed to enable 'gpu_cx': %d\n", ret); |
| 127 | return ret; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int disable_pwrrail(struct msm_gpu *gpu) |
| 135 | { |
| 136 | if (gpu->gpu_cx) |
| 137 | regulator_disable(gpu->gpu_cx); |
| 138 | if (gpu->gpu_reg) |
| 139 | regulator_disable(gpu->gpu_reg); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int enable_clk(struct msm_gpu *gpu) |
| 144 | { |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 145 | if (gpu->core_clk && gpu->fast_rate) |
| 146 | clk_set_rate(gpu->core_clk, gpu->fast_rate); |
Jordan Crouse | 89d777a | 2016-11-28 12:28:31 -0700 | [diff] [blame] | 147 | |
Jordan Crouse | b5f103a | 2016-11-28 12:28:33 -0700 | [diff] [blame] | 148 | /* Set the RBBM timer rate to 19.2Mhz */ |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 149 | if (gpu->rbbmtimer_clk) |
| 150 | clk_set_rate(gpu->rbbmtimer_clk, 19200000); |
Jordan Crouse | b5f103a | 2016-11-28 12:28:33 -0700 | [diff] [blame] | 151 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame^] | 152 | return clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int disable_clk(struct msm_gpu *gpu) |
| 156 | { |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame^] | 157 | clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 158 | |
Jordan Crouse | bf5af4a | 2017-03-07 10:02:54 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Set the clock to a deliberately low rate. On older targets the clock |
| 161 | * speed had to be non zero to avoid problems. On newer targets this |
| 162 | * will be rounded down to zero anyway so it all works out. |
| 163 | */ |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 164 | if (gpu->core_clk) |
| 165 | clk_set_rate(gpu->core_clk, 27000000); |
Jordan Crouse | 89d777a | 2016-11-28 12:28:31 -0700 | [diff] [blame] | 166 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 167 | if (gpu->rbbmtimer_clk) |
| 168 | clk_set_rate(gpu->rbbmtimer_clk, 0); |
Jordan Crouse | b5f103a | 2016-11-28 12:28:33 -0700 | [diff] [blame] | 169 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int enable_axi(struct msm_gpu *gpu) |
| 174 | { |
| 175 | if (gpu->ebi1_clk) |
| 176 | clk_prepare_enable(gpu->ebi1_clk); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int disable_axi(struct msm_gpu *gpu) |
| 181 | { |
| 182 | if (gpu->ebi1_clk) |
| 183 | clk_disable_unprepare(gpu->ebi1_clk); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int msm_gpu_pm_resume(struct msm_gpu *gpu) |
| 188 | { |
| 189 | int ret; |
| 190 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 191 | DBG("%s", gpu->name); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 192 | |
| 193 | ret = enable_pwrrail(gpu); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | |
| 197 | ret = enable_clk(gpu); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | |
| 201 | ret = enable_axi(gpu); |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 205 | if (gpu->devfreq.devfreq) { |
| 206 | gpu->devfreq.busy_cycles = 0; |
| 207 | gpu->devfreq.time = ktime_get(); |
| 208 | |
| 209 | devfreq_resume_device(gpu->devfreq.devfreq); |
| 210 | } |
| 211 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 212 | gpu->needs_hw_init = true; |
| 213 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int msm_gpu_pm_suspend(struct msm_gpu *gpu) |
| 218 | { |
| 219 | int ret; |
| 220 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 221 | DBG("%s", gpu->name); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 222 | |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 223 | if (gpu->devfreq.devfreq) |
| 224 | devfreq_suspend_device(gpu->devfreq.devfreq); |
| 225 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 226 | ret = disable_axi(gpu); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | |
| 230 | ret = disable_clk(gpu); |
| 231 | if (ret) |
| 232 | return ret; |
| 233 | |
| 234 | ret = disable_pwrrail(gpu); |
| 235 | if (ret) |
| 236 | return ret; |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 241 | int msm_gpu_hw_init(struct msm_gpu *gpu) |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 242 | { |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 243 | int ret; |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 244 | |
Rob Clark | cb1e381 | 2017-06-13 09:15:36 -0400 | [diff] [blame] | 245 | WARN_ON(!mutex_is_locked(&gpu->dev->struct_mutex)); |
| 246 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 247 | if (!gpu->needs_hw_init) |
| 248 | return 0; |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 249 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 250 | disable_irq(gpu->irq); |
| 251 | ret = gpu->funcs->hw_init(gpu); |
| 252 | if (!ret) |
| 253 | gpu->needs_hw_init = false; |
| 254 | enable_irq(gpu->irq); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 255 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 256 | return ret; |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 259 | #ifdef CONFIG_DEV_COREDUMP |
| 260 | static ssize_t msm_gpu_devcoredump_read(char *buffer, loff_t offset, |
| 261 | size_t count, void *data, size_t datalen) |
| 262 | { |
| 263 | struct msm_gpu *gpu = data; |
| 264 | struct drm_print_iterator iter; |
| 265 | struct drm_printer p; |
| 266 | struct msm_gpu_state *state; |
| 267 | |
| 268 | state = msm_gpu_crashstate_get(gpu); |
| 269 | if (!state) |
| 270 | return 0; |
| 271 | |
| 272 | iter.data = buffer; |
| 273 | iter.offset = 0; |
| 274 | iter.start = offset; |
| 275 | iter.remain = count; |
| 276 | |
| 277 | p = drm_coredump_printer(&iter); |
| 278 | |
| 279 | drm_printf(&p, "---\n"); |
| 280 | drm_printf(&p, "kernel: " UTS_RELEASE "\n"); |
| 281 | drm_printf(&p, "module: " KBUILD_MODNAME "\n"); |
Arnd Bergmann | 3530a17 | 2018-07-26 14:39:25 +0200 | [diff] [blame] | 282 | drm_printf(&p, "time: %lld.%09ld\n", |
| 283 | state->time.tv_sec, state->time.tv_nsec); |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 284 | if (state->comm) |
| 285 | drm_printf(&p, "comm: %s\n", state->comm); |
| 286 | if (state->cmd) |
| 287 | drm_printf(&p, "cmdline: %s\n", state->cmd); |
| 288 | |
| 289 | gpu->funcs->show(gpu, state, &p); |
| 290 | |
| 291 | msm_gpu_crashstate_put(gpu); |
| 292 | |
| 293 | return count - iter.remain; |
| 294 | } |
| 295 | |
| 296 | static void msm_gpu_devcoredump_free(void *data) |
| 297 | { |
| 298 | struct msm_gpu *gpu = data; |
| 299 | |
| 300 | msm_gpu_crashstate_put(gpu); |
| 301 | } |
| 302 | |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 303 | static void msm_gpu_crashstate_get_bo(struct msm_gpu_state *state, |
| 304 | struct msm_gem_object *obj, u64 iova, u32 flags) |
| 305 | { |
| 306 | struct msm_gpu_state_bo *state_bo = &state->bos[state->nr_bos]; |
| 307 | |
| 308 | /* Don't record write only objects */ |
| 309 | |
| 310 | state_bo->size = obj->base.size; |
| 311 | state_bo->iova = iova; |
| 312 | |
| 313 | /* Only store the data for buffer objects marked for read */ |
| 314 | if ((flags & MSM_SUBMIT_BO_READ)) { |
| 315 | void *ptr; |
| 316 | |
| 317 | state_bo->data = kvmalloc(obj->base.size, GFP_KERNEL); |
| 318 | if (!state_bo->data) |
| 319 | return; |
| 320 | |
| 321 | ptr = msm_gem_get_vaddr_active(&obj->base); |
| 322 | if (IS_ERR(ptr)) { |
| 323 | kvfree(state_bo->data); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | memcpy(state_bo->data, ptr, obj->base.size); |
| 328 | msm_gem_put_vaddr(&obj->base); |
| 329 | } |
| 330 | |
| 331 | state->nr_bos++; |
| 332 | } |
| 333 | |
| 334 | static void msm_gpu_crashstate_capture(struct msm_gpu *gpu, |
| 335 | struct msm_gem_submit *submit, char *comm, char *cmd) |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 336 | { |
| 337 | struct msm_gpu_state *state; |
| 338 | |
| 339 | /* Only save one crash state at a time */ |
| 340 | if (gpu->crashstate) |
| 341 | return; |
| 342 | |
| 343 | state = gpu->funcs->gpu_state_get(gpu); |
| 344 | if (IS_ERR_OR_NULL(state)) |
| 345 | return; |
| 346 | |
| 347 | /* Fill in the additional crash state information */ |
| 348 | state->comm = kstrdup(comm, GFP_KERNEL); |
| 349 | state->cmd = kstrdup(cmd, GFP_KERNEL); |
| 350 | |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 351 | if (submit) { |
| 352 | int i; |
| 353 | |
| 354 | state->bos = kcalloc(submit->nr_bos, |
| 355 | sizeof(struct msm_gpu_state_bo), GFP_KERNEL); |
| 356 | |
| 357 | for (i = 0; state->bos && i < submit->nr_bos; i++) |
| 358 | msm_gpu_crashstate_get_bo(state, submit->bos[i].obj, |
| 359 | submit->bos[i].iova, submit->bos[i].flags); |
| 360 | } |
| 361 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 362 | /* Set the active crash state to be dumped on failure */ |
| 363 | gpu->crashstate = state; |
| 364 | |
| 365 | /* FIXME: Release the crashstate if this errors out? */ |
| 366 | dev_coredumpm(gpu->dev->dev, THIS_MODULE, gpu, 0, GFP_KERNEL, |
| 367 | msm_gpu_devcoredump_read, msm_gpu_devcoredump_free); |
| 368 | } |
| 369 | #else |
| 370 | static void msm_gpu_crashstate_capture(struct msm_gpu *gpu, char *comm, |
| 371 | char *cmd) |
| 372 | { |
| 373 | } |
| 374 | #endif |
| 375 | |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 376 | /* |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 377 | * Hangcheck detection for locked gpu: |
| 378 | */ |
| 379 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 380 | static void update_fences(struct msm_gpu *gpu, struct msm_ringbuffer *ring, |
| 381 | uint32_t fence) |
| 382 | { |
| 383 | struct msm_gem_submit *submit; |
| 384 | |
| 385 | list_for_each_entry(submit, &ring->submits, node) { |
| 386 | if (submit->seqno > fence) |
| 387 | break; |
| 388 | |
| 389 | msm_update_fence(submit->ring->fctx, |
| 390 | submit->fence->seqno); |
| 391 | } |
| 392 | } |
| 393 | |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 394 | static struct msm_gem_submit * |
| 395 | find_submit(struct msm_ringbuffer *ring, uint32_t fence) |
| 396 | { |
| 397 | struct msm_gem_submit *submit; |
| 398 | |
| 399 | WARN_ON(!mutex_is_locked(&ring->gpu->dev->struct_mutex)); |
| 400 | |
| 401 | list_for_each_entry(submit, &ring->submits, node) |
| 402 | if (submit->seqno == fence) |
| 403 | return submit; |
| 404 | |
| 405 | return NULL; |
| 406 | } |
| 407 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 408 | static void retire_submits(struct msm_gpu *gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 409 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 410 | static void recover_worker(struct work_struct *work) |
| 411 | { |
| 412 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work); |
| 413 | struct drm_device *dev = gpu->dev; |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 414 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 415 | struct msm_gem_submit *submit; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 416 | struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu); |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 417 | char *comm = NULL, *cmd = NULL; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 418 | int i; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 419 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 420 | mutex_lock(&dev->struct_mutex); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 421 | |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 422 | dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name); |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 423 | |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 424 | submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1); |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 425 | if (submit) { |
| 426 | struct task_struct *task; |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 427 | |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 428 | rcu_read_lock(); |
| 429 | task = pid_task(submit->pid, PIDTYPE_PID); |
| 430 | if (task) { |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 431 | comm = kstrdup(task->comm, GFP_ATOMIC); |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * So slightly annoying, in other paths like |
| 435 | * mmap'ing gem buffers, mmap_sem is acquired |
| 436 | * before struct_mutex, which means we can't |
| 437 | * hold struct_mutex across the call to |
| 438 | * get_cmdline(). But submits are retired |
| 439 | * from the same in-order workqueue, so we can |
| 440 | * safely drop the lock here without worrying |
| 441 | * about the submit going away. |
| 442 | */ |
| 443 | mutex_unlock(&dev->struct_mutex); |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 444 | cmd = kstrdup_quotable_cmdline(task, GFP_ATOMIC); |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 445 | mutex_lock(&dev->struct_mutex); |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 446 | } |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 447 | rcu_read_unlock(); |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 448 | |
| 449 | if (comm && cmd) { |
| 450 | dev_err(dev->dev, "%s: offending task: %s (%s)\n", |
| 451 | gpu->name, comm, cmd); |
| 452 | |
| 453 | msm_rd_dump_submit(priv->hangrd, submit, |
| 454 | "offending task: %s (%s)", comm, cmd); |
| 455 | } else |
| 456 | msm_rd_dump_submit(priv->hangrd, submit, NULL); |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 457 | } |
Rob Clark | 18bb8a6 | 2017-09-13 10:17:18 -0400 | [diff] [blame] | 458 | |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 459 | /* Record the crash state */ |
| 460 | pm_runtime_get_sync(&gpu->pdev->dev); |
Jordan Crouse | cdb9593 | 2018-07-24 10:33:31 -0600 | [diff] [blame] | 461 | msm_gpu_crashstate_capture(gpu, submit, comm, cmd); |
Jordan Crouse | c0fec7f | 2018-07-24 10:33:27 -0600 | [diff] [blame] | 462 | pm_runtime_put_sync(&gpu->pdev->dev); |
| 463 | |
Jordan Crouse | 65a3c27 | 2018-07-24 10:33:26 -0600 | [diff] [blame] | 464 | kfree(cmd); |
| 465 | kfree(comm); |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Update all the rings with the latest and greatest fence.. this |
| 469 | * needs to happen after msm_rd_dump_submit() to ensure that the |
| 470 | * bo's referenced by the offending submit are still around. |
| 471 | */ |
Jordan Crouse | 7ddae82 | 2017-12-13 13:45:44 -0700 | [diff] [blame] | 472 | for (i = 0; i < gpu->nr_rings; i++) { |
Rob Clark | 96169f4 | 2017-09-15 11:04:44 -0400 | [diff] [blame] | 473 | struct msm_ringbuffer *ring = gpu->rb[i]; |
| 474 | |
| 475 | uint32_t fence = ring->memptrs->fence; |
| 476 | |
| 477 | /* |
| 478 | * For the current (faulting?) ring/submit advance the fence by |
| 479 | * one more to clear the faulting submit |
| 480 | */ |
| 481 | if (ring == cur_ring) |
| 482 | fence++; |
| 483 | |
| 484 | update_fences(gpu, ring, fence); |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if (msm_gpu_active(gpu)) { |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 488 | /* retire completed submits, plus the one that hung: */ |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 489 | retire_submits(gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 490 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 491 | pm_runtime_get_sync(&gpu->pdev->dev); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 492 | gpu->funcs->recover(gpu); |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 493 | pm_runtime_put_sync(&gpu->pdev->dev); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 494 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 495 | /* |
| 496 | * Replay all remaining submits starting with highest priority |
| 497 | * ring |
| 498 | */ |
Jordan Crouse | b1fc283 | 2017-10-20 11:07:01 -0600 | [diff] [blame] | 499 | for (i = 0; i < gpu->nr_rings; i++) { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 500 | struct msm_ringbuffer *ring = gpu->rb[i]; |
| 501 | |
| 502 | list_for_each_entry(submit, &ring->submits, node) |
| 503 | gpu->funcs->submit(gpu, submit, NULL); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 504 | } |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 505 | } |
Rob Clark | 4816b62 | 2016-05-03 10:10:15 -0400 | [diff] [blame] | 506 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 507 | mutex_unlock(&dev->struct_mutex); |
| 508 | |
| 509 | msm_gpu_retire(gpu); |
| 510 | } |
| 511 | |
| 512 | static void hangcheck_timer_reset(struct msm_gpu *gpu) |
| 513 | { |
| 514 | DBG("%s", gpu->name); |
| 515 | mod_timer(&gpu->hangcheck_timer, |
| 516 | round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES)); |
| 517 | } |
| 518 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 519 | static void hangcheck_handler(struct timer_list *t) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 520 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 521 | struct msm_gpu *gpu = from_timer(gpu, t, hangcheck_timer); |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 522 | struct drm_device *dev = gpu->dev; |
| 523 | struct msm_drm_private *priv = dev->dev_private; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 524 | struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu); |
| 525 | uint32_t fence = ring->memptrs->fence; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 526 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 527 | if (fence != ring->hangcheck_fence) { |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 528 | /* some progress has been made.. ya! */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 529 | ring->hangcheck_fence = fence; |
| 530 | } else if (fence < ring->seqno) { |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 531 | /* no progress and not done.. hung! */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 532 | ring->hangcheck_fence = fence; |
| 533 | dev_err(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n", |
| 534 | gpu->name, ring->id); |
Rob Clark | 26791c4 | 2013-09-03 07:12:03 -0400 | [diff] [blame] | 535 | dev_err(dev->dev, "%s: completed fence: %u\n", |
| 536 | gpu->name, fence); |
| 537 | dev_err(dev->dev, "%s: submitted fence: %u\n", |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 538 | gpu->name, ring->seqno); |
| 539 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 540 | queue_work(priv->wq, &gpu->recover_work); |
| 541 | } |
| 542 | |
| 543 | /* if still more pending work, reset the hangcheck timer: */ |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 544 | if (ring->seqno > ring->hangcheck_fence) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 545 | hangcheck_timer_reset(gpu); |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 546 | |
| 547 | /* workaround for missing irq: */ |
| 548 | queue_work(priv->wq, &gpu->retire_work); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 552 | * Performance Counters: |
| 553 | */ |
| 554 | |
| 555 | /* called under perf_lock */ |
| 556 | static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs) |
| 557 | { |
| 558 | uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)]; |
| 559 | int i, n = min(ncntrs, gpu->num_perfcntrs); |
| 560 | |
| 561 | /* read current values: */ |
| 562 | for (i = 0; i < gpu->num_perfcntrs; i++) |
| 563 | current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg); |
| 564 | |
| 565 | /* update cntrs: */ |
| 566 | for (i = 0; i < n; i++) |
| 567 | cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i]; |
| 568 | |
| 569 | /* save current values: */ |
| 570 | for (i = 0; i < gpu->num_perfcntrs; i++) |
| 571 | gpu->last_cntrs[i] = current_cntrs[i]; |
| 572 | |
| 573 | return n; |
| 574 | } |
| 575 | |
| 576 | static void update_sw_cntrs(struct msm_gpu *gpu) |
| 577 | { |
| 578 | ktime_t time; |
| 579 | uint32_t elapsed; |
| 580 | unsigned long flags; |
| 581 | |
| 582 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 583 | if (!gpu->perfcntr_active) |
| 584 | goto out; |
| 585 | |
| 586 | time = ktime_get(); |
| 587 | elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time)); |
| 588 | |
| 589 | gpu->totaltime += elapsed; |
| 590 | if (gpu->last_sample.active) |
| 591 | gpu->activetime += elapsed; |
| 592 | |
| 593 | gpu->last_sample.active = msm_gpu_active(gpu); |
| 594 | gpu->last_sample.time = time; |
| 595 | |
| 596 | out: |
| 597 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 598 | } |
| 599 | |
| 600 | void msm_gpu_perfcntr_start(struct msm_gpu *gpu) |
| 601 | { |
| 602 | unsigned long flags; |
| 603 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 604 | pm_runtime_get_sync(&gpu->pdev->dev); |
| 605 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 606 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 607 | /* we could dynamically enable/disable perfcntr registers too.. */ |
| 608 | gpu->last_sample.active = msm_gpu_active(gpu); |
| 609 | gpu->last_sample.time = ktime_get(); |
| 610 | gpu->activetime = gpu->totaltime = 0; |
| 611 | gpu->perfcntr_active = true; |
| 612 | update_hw_cntrs(gpu, 0, NULL); |
| 613 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 614 | } |
| 615 | |
| 616 | void msm_gpu_perfcntr_stop(struct msm_gpu *gpu) |
| 617 | { |
| 618 | gpu->perfcntr_active = false; |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 619 | pm_runtime_put_sync(&gpu->pdev->dev); |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* returns -errno or # of cntrs sampled */ |
| 623 | int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime, |
| 624 | uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs) |
| 625 | { |
| 626 | unsigned long flags; |
| 627 | int ret; |
| 628 | |
| 629 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 630 | |
| 631 | if (!gpu->perfcntr_active) { |
| 632 | ret = -EINVAL; |
| 633 | goto out; |
| 634 | } |
| 635 | |
| 636 | *activetime = gpu->activetime; |
| 637 | *totaltime = gpu->totaltime; |
| 638 | |
| 639 | gpu->activetime = gpu->totaltime = 0; |
| 640 | |
| 641 | ret = update_hw_cntrs(gpu, ncntrs, cntrs); |
| 642 | |
| 643 | out: |
| 644 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | /* |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 650 | * Cmdstream submission/retirement: |
| 651 | */ |
| 652 | |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 653 | static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit) |
| 654 | { |
| 655 | int i; |
| 656 | |
| 657 | for (i = 0; i < submit->nr_bos; i++) { |
| 658 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
| 659 | /* move to inactive: */ |
| 660 | msm_gem_move_to_inactive(&msm_obj->base); |
Rob Clark | 8bdcd94 | 2017-06-13 11:07:08 -0400 | [diff] [blame] | 661 | msm_gem_put_iova(&msm_obj->base, gpu->aspace); |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 662 | drm_gem_object_put(&msm_obj->base); |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 663 | } |
| 664 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 665 | pm_runtime_mark_last_busy(&gpu->pdev->dev); |
| 666 | pm_runtime_put_autosuspend(&gpu->pdev->dev); |
Rob Clark | 40e6815 | 2016-05-03 09:50:26 -0400 | [diff] [blame] | 667 | msm_gem_submit_free(submit); |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 670 | static void retire_submits(struct msm_gpu *gpu) |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 671 | { |
| 672 | struct drm_device *dev = gpu->dev; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 673 | struct msm_gem_submit *submit, *tmp; |
| 674 | int i; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 675 | |
| 676 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 677 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 678 | /* Retire the commits starting with highest priority */ |
Jordan Crouse | b1fc283 | 2017-10-20 11:07:01 -0600 | [diff] [blame] | 679 | for (i = 0; i < gpu->nr_rings; i++) { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 680 | struct msm_ringbuffer *ring = gpu->rb[i]; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 681 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 682 | list_for_each_entry_safe(submit, tmp, &ring->submits, node) { |
| 683 | if (dma_fence_is_signaled(submit->fence)) |
| 684 | retire_submit(gpu, submit); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 689 | static void retire_worker(struct work_struct *work) |
| 690 | { |
| 691 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work); |
| 692 | struct drm_device *dev = gpu->dev; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 693 | int i; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 694 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 695 | for (i = 0; i < gpu->nr_rings; i++) |
| 696 | update_fences(gpu, gpu->rb[i], gpu->rb[i]->memptrs->fence); |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame] | 697 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 698 | mutex_lock(&dev->struct_mutex); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 699 | retire_submits(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 700 | mutex_unlock(&dev->struct_mutex); |
| 701 | } |
| 702 | |
| 703 | /* call from irq handler to schedule work to retire bo's */ |
| 704 | void msm_gpu_retire(struct msm_gpu *gpu) |
| 705 | { |
| 706 | struct msm_drm_private *priv = gpu->dev->dev_private; |
| 707 | queue_work(priv->wq, &gpu->retire_work); |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 708 | update_sw_cntrs(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* add bo's to gpu's ring, and kick gpu: */ |
Rob Clark | f44d32c | 2016-06-16 16:37:38 -0400 | [diff] [blame] | 712 | 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] | 713 | struct msm_file_private *ctx) |
| 714 | { |
| 715 | struct drm_device *dev = gpu->dev; |
| 716 | struct msm_drm_private *priv = dev->dev_private; |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 717 | struct msm_ringbuffer *ring = submit->ring; |
Rob Clark | f44d32c | 2016-06-16 16:37:38 -0400 | [diff] [blame] | 718 | int i; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 719 | |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 720 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 721 | |
Rob Clark | eeb7547 | 2017-02-10 15:36:33 -0500 | [diff] [blame] | 722 | pm_runtime_get_sync(&gpu->pdev->dev); |
| 723 | |
| 724 | msm_gpu_hw_init(gpu); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 725 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 726 | submit->seqno = ++ring->seqno; |
| 727 | |
| 728 | list_add_tail(&submit->node, &ring->submits); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 729 | |
Rob Clark | 998b9a5 | 2017-09-15 10:46:45 -0400 | [diff] [blame] | 730 | msm_rd_dump_submit(priv->rd, submit, NULL); |
Rob Clark | a7d3c95 | 2014-05-30 14:47:38 -0400 | [diff] [blame] | 731 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 732 | update_sw_cntrs(gpu); |
| 733 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 734 | for (i = 0; i < submit->nr_bos; i++) { |
| 735 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 736 | uint64_t iova; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 737 | |
| 738 | /* can't happen yet.. but when we add 2d support we'll have |
| 739 | * to deal w/ cross-ring synchronization: |
| 740 | */ |
| 741 | WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu)); |
| 742 | |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 743 | /* submit takes a reference to the bo and iova until retired: */ |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 744 | drm_gem_object_get(&msm_obj->base); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 745 | msm_gem_get_iova(&msm_obj->base, |
Rob Clark | 8bdcd94 | 2017-06-13 11:07:08 -0400 | [diff] [blame] | 746 | submit->gpu->aspace, &iova); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 747 | |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 748 | if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE) |
| 749 | msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 750 | else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ) |
| 751 | msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 752 | } |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 753 | |
Rob Clark | 1193c3b | 2016-05-03 09:46:49 -0400 | [diff] [blame] | 754 | gpu->funcs->submit(gpu, submit, ctx); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 755 | priv->lastctx = ctx; |
| 756 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 757 | hangcheck_timer_reset(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Init/Cleanup: |
| 762 | */ |
| 763 | |
| 764 | static irqreturn_t irq_handler(int irq, void *data) |
| 765 | { |
| 766 | struct msm_gpu *gpu = data; |
| 767 | return gpu->funcs->irq(gpu); |
| 768 | } |
| 769 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 770 | static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu) |
| 771 | { |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame^] | 772 | int ret = msm_clk_bulk_get(&pdev->dev, &gpu->grp_clks); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 773 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame^] | 774 | if (ret < 1) { |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 775 | gpu->nr_clocks = 0; |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame^] | 776 | return ret; |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame^] | 779 | gpu->nr_clocks = ret; |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 780 | |
Jordan Crouse | 8e54eea | 2018-08-06 11:33:21 -0600 | [diff] [blame^] | 781 | gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks, |
| 782 | gpu->nr_clocks, "core"); |
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 | gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks, |
| 785 | gpu->nr_clocks, "rbbmtimer"); |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 786 | |
| 787 | return 0; |
| 788 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 789 | |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 790 | static struct msm_gem_address_space * |
| 791 | msm_gpu_create_address_space(struct msm_gpu *gpu, struct platform_device *pdev, |
| 792 | uint64_t va_start, uint64_t va_end) |
| 793 | { |
| 794 | struct iommu_domain *iommu; |
| 795 | struct msm_gem_address_space *aspace; |
| 796 | int ret; |
| 797 | |
| 798 | /* |
| 799 | * Setup IOMMU.. eventually we will (I think) do this once per context |
| 800 | * and have separate page tables per context. For now, to keep things |
| 801 | * simple and to get something working, just use a single address space: |
| 802 | */ |
| 803 | iommu = iommu_domain_alloc(&platform_bus_type); |
| 804 | if (!iommu) |
| 805 | return NULL; |
| 806 | |
| 807 | iommu->geometry.aperture_start = va_start; |
| 808 | iommu->geometry.aperture_end = va_end; |
| 809 | |
| 810 | dev_info(gpu->dev->dev, "%s: using IOMMU\n", gpu->name); |
| 811 | |
| 812 | aspace = msm_gem_address_space_create(&pdev->dev, iommu, "gpu"); |
| 813 | if (IS_ERR(aspace)) { |
| 814 | dev_err(gpu->dev->dev, "failed to init iommu: %ld\n", |
| 815 | PTR_ERR(aspace)); |
| 816 | iommu_domain_free(iommu); |
| 817 | return ERR_CAST(aspace); |
| 818 | } |
| 819 | |
| 820 | ret = aspace->mmu->funcs->attach(aspace->mmu, NULL, 0); |
| 821 | if (ret) { |
| 822 | msm_gem_address_space_put(aspace); |
| 823 | return ERR_PTR(ret); |
| 824 | } |
| 825 | |
| 826 | return aspace; |
| 827 | } |
| 828 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 829 | int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev, |
| 830 | struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs, |
Jordan Crouse | 5770fc7 | 2017-05-08 14:35:03 -0600 | [diff] [blame] | 831 | const char *name, struct msm_gpu_config *config) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 832 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 833 | int i, ret, nr_rings = config->nr_rings; |
| 834 | void *memptrs; |
| 835 | uint64_t memptrs_iova; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 836 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 837 | if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs))) |
| 838 | gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs); |
| 839 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 840 | gpu->dev = drm; |
| 841 | gpu->funcs = funcs; |
| 842 | gpu->name = name; |
| 843 | |
| 844 | INIT_LIST_HEAD(&gpu->active_list); |
| 845 | INIT_WORK(&gpu->retire_work, retire_worker); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 846 | INIT_WORK(&gpu->recover_work, recover_worker); |
| 847 | |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 848 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 849 | timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 850 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 851 | spin_lock_init(&gpu->perf_lock); |
| 852 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 853 | |
| 854 | /* Map registers: */ |
Jordan Crouse | 5770fc7 | 2017-05-08 14:35:03 -0600 | [diff] [blame] | 855 | gpu->mmio = msm_ioremap(pdev, config->ioname, name); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 856 | if (IS_ERR(gpu->mmio)) { |
| 857 | ret = PTR_ERR(gpu->mmio); |
| 858 | goto fail; |
| 859 | } |
| 860 | |
| 861 | /* Get Interrupt: */ |
Jordan Crouse | 5770fc7 | 2017-05-08 14:35:03 -0600 | [diff] [blame] | 862 | gpu->irq = platform_get_irq_byname(pdev, config->irqname); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 863 | if (gpu->irq < 0) { |
| 864 | ret = gpu->irq; |
| 865 | dev_err(drm->dev, "failed to get irq: %d\n", ret); |
| 866 | goto fail; |
| 867 | } |
| 868 | |
| 869 | ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, |
| 870 | IRQF_TRIGGER_HIGH, gpu->name, gpu); |
| 871 | if (ret) { |
| 872 | dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret); |
| 873 | goto fail; |
| 874 | } |
| 875 | |
Jordan Crouse | 98db803 | 2017-03-07 10:02:56 -0700 | [diff] [blame] | 876 | ret = get_clocks(pdev, gpu); |
| 877 | if (ret) |
| 878 | goto fail; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 879 | |
Rob Clark | 720c3bb | 2017-01-30 11:30:58 -0500 | [diff] [blame] | 880 | gpu->ebi1_clk = msm_clk_get(pdev, "bus"); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 881 | DBG("ebi1_clk: %p", gpu->ebi1_clk); |
| 882 | if (IS_ERR(gpu->ebi1_clk)) |
| 883 | gpu->ebi1_clk = NULL; |
| 884 | |
| 885 | /* Acquire regulators: */ |
| 886 | gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd"); |
| 887 | DBG("gpu_reg: %p", gpu->gpu_reg); |
| 888 | if (IS_ERR(gpu->gpu_reg)) |
| 889 | gpu->gpu_reg = NULL; |
| 890 | |
| 891 | gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx"); |
| 892 | DBG("gpu_cx: %p", gpu->gpu_cx); |
| 893 | if (IS_ERR(gpu->gpu_cx)) |
| 894 | gpu->gpu_cx = NULL; |
| 895 | |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 896 | gpu->pdev = pdev; |
| 897 | platform_set_drvdata(pdev, gpu); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 898 | |
Jordan Crouse | f91c14a | 2018-01-10 10:41:54 -0700 | [diff] [blame] | 899 | msm_devfreq_init(gpu); |
| 900 | |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 901 | gpu->aspace = msm_gpu_create_address_space(gpu, pdev, |
| 902 | config->va_start, config->va_end); |
| 903 | |
| 904 | if (gpu->aspace == NULL) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 905 | 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] | 906 | else if (IS_ERR(gpu->aspace)) { |
| 907 | ret = PTR_ERR(gpu->aspace); |
| 908 | goto fail; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 909 | } |
Rob Clark | a1ad352 | 2014-07-11 11:59:22 -0400 | [diff] [blame] | 910 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 911 | memptrs = msm_gem_kernel_new(drm, sizeof(*gpu->memptrs_bo), |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 912 | MSM_BO_UNCACHED, gpu->aspace, &gpu->memptrs_bo, |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 913 | &memptrs_iova); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 914 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 915 | if (IS_ERR(memptrs)) { |
| 916 | ret = PTR_ERR(memptrs); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 917 | dev_err(drm->dev, "could not allocate memptrs: %d\n", ret); |
| 918 | goto fail; |
| 919 | } |
| 920 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 921 | if (nr_rings > ARRAY_SIZE(gpu->rb)) { |
Arnd Bergmann | 39ae0d3 | 2017-08-03 13:50:48 +0200 | [diff] [blame] | 922 | DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n", |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 923 | ARRAY_SIZE(gpu->rb)); |
| 924 | nr_rings = ARRAY_SIZE(gpu->rb); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 925 | } |
| 926 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 927 | /* Create ringbuffer(s): */ |
| 928 | for (i = 0; i < nr_rings; i++) { |
| 929 | gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova); |
| 930 | |
| 931 | if (IS_ERR(gpu->rb[i])) { |
| 932 | ret = PTR_ERR(gpu->rb[i]); |
| 933 | dev_err(drm->dev, |
| 934 | "could not create ringbuffer %d: %d\n", i, ret); |
| 935 | goto fail; |
| 936 | } |
| 937 | |
| 938 | memptrs += sizeof(struct msm_rbmemptrs); |
| 939 | memptrs_iova += sizeof(struct msm_rbmemptrs); |
| 940 | } |
| 941 | |
| 942 | gpu->nr_rings = nr_rings; |
| 943 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 944 | return 0; |
| 945 | |
| 946 | fail: |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 947 | for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { |
| 948 | msm_ringbuffer_destroy(gpu->rb[i]); |
| 949 | gpu->rb[i] = NULL; |
| 950 | } |
| 951 | |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 952 | if (gpu->memptrs_bo) { |
| 953 | msm_gem_put_vaddr(gpu->memptrs_bo); |
| 954 | msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace); |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 955 | drm_gem_object_put_unlocked(gpu->memptrs_bo); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 956 | } |
| 957 | |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 958 | platform_set_drvdata(pdev, NULL); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | void msm_gpu_cleanup(struct msm_gpu *gpu) |
| 963 | { |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 964 | int i; |
| 965 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 966 | DBG("%s", gpu->name); |
| 967 | |
| 968 | WARN_ON(!list_empty(&gpu->active_list)); |
| 969 | |
Jordan Crouse | f97deca | 2017-10-20 11:06:57 -0600 | [diff] [blame] | 970 | for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) { |
| 971 | msm_ringbuffer_destroy(gpu->rb[i]); |
| 972 | gpu->rb[i] = NULL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 973 | } |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 974 | |
| 975 | if (gpu->memptrs_bo) { |
| 976 | msm_gem_put_vaddr(gpu->memptrs_bo); |
| 977 | msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace); |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 978 | drm_gem_object_put_unlocked(gpu->memptrs_bo); |
Jordan Crouse | cd414f3 | 2017-10-20 11:06:56 -0600 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | if (!IS_ERR_OR_NULL(gpu->aspace)) { |
Jordan Crouse | 1267a4d | 2017-07-27 10:42:39 -0600 | [diff] [blame] | 982 | gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu, |
| 983 | NULL, 0); |
| 984 | msm_gem_address_space_put(gpu->aspace); |
| 985 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 986 | } |