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