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 | |
| 23 | |
| 24 | /* |
| 25 | * Power Management: |
| 26 | */ |
| 27 | |
Rob Clark | 6490ad4 | 2015-06-04 10:26:37 -0400 | [diff] [blame] | 28 | #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 29 | #include <mach/board.h> |
Rob Clark | bf2b33af | 2013-11-15 09:03:15 -0500 | [diff] [blame] | 30 | static void bs_init(struct msm_gpu *gpu) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 31 | { |
Rob Clark | bf2b33af | 2013-11-15 09:03:15 -0500 | [diff] [blame] | 32 | if (gpu->bus_scale_table) { |
| 33 | gpu->bsc = msm_bus_scale_register_client(gpu->bus_scale_table); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 34 | DBG("bus scale client: %08x", gpu->bsc); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static void bs_fini(struct msm_gpu *gpu) |
| 39 | { |
| 40 | if (gpu->bsc) { |
| 41 | msm_bus_scale_unregister_client(gpu->bsc); |
| 42 | gpu->bsc = 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static void bs_set(struct msm_gpu *gpu, int idx) |
| 47 | { |
| 48 | if (gpu->bsc) { |
| 49 | DBG("set bus scaling: %d", idx); |
| 50 | msm_bus_scale_client_update_request(gpu->bsc, idx); |
| 51 | } |
| 52 | } |
| 53 | #else |
Rob Clark | bf2b33af | 2013-11-15 09:03:15 -0500 | [diff] [blame] | 54 | static void bs_init(struct msm_gpu *gpu) {} |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 55 | static void bs_fini(struct msm_gpu *gpu) {} |
| 56 | static void bs_set(struct msm_gpu *gpu, int idx) {} |
| 57 | #endif |
| 58 | |
| 59 | static int enable_pwrrail(struct msm_gpu *gpu) |
| 60 | { |
| 61 | struct drm_device *dev = gpu->dev; |
| 62 | int ret = 0; |
| 63 | |
| 64 | if (gpu->gpu_reg) { |
| 65 | ret = regulator_enable(gpu->gpu_reg); |
| 66 | if (ret) { |
| 67 | dev_err(dev->dev, "failed to enable 'gpu_reg': %d\n", ret); |
| 68 | return ret; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (gpu->gpu_cx) { |
| 73 | ret = regulator_enable(gpu->gpu_cx); |
| 74 | if (ret) { |
| 75 | dev_err(dev->dev, "failed to enable 'gpu_cx': %d\n", ret); |
| 76 | return ret; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int disable_pwrrail(struct msm_gpu *gpu) |
| 84 | { |
| 85 | if (gpu->gpu_cx) |
| 86 | regulator_disable(gpu->gpu_cx); |
| 87 | if (gpu->gpu_reg) |
| 88 | regulator_disable(gpu->gpu_reg); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int enable_clk(struct msm_gpu *gpu) |
| 93 | { |
| 94 | struct clk *rate_clk = NULL; |
| 95 | int i; |
| 96 | |
| 97 | /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */ |
| 98 | for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) { |
| 99 | if (gpu->grp_clks[i]) { |
| 100 | clk_prepare(gpu->grp_clks[i]); |
| 101 | rate_clk = gpu->grp_clks[i]; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (rate_clk && gpu->fast_rate) |
| 106 | clk_set_rate(rate_clk, gpu->fast_rate); |
| 107 | |
| 108 | for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) |
| 109 | if (gpu->grp_clks[i]) |
| 110 | clk_enable(gpu->grp_clks[i]); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int disable_clk(struct msm_gpu *gpu) |
| 116 | { |
| 117 | struct clk *rate_clk = NULL; |
| 118 | int i; |
| 119 | |
| 120 | /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */ |
| 121 | for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) { |
| 122 | if (gpu->grp_clks[i]) { |
| 123 | clk_disable(gpu->grp_clks[i]); |
| 124 | rate_clk = gpu->grp_clks[i]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (rate_clk && gpu->slow_rate) |
| 129 | clk_set_rate(rate_clk, gpu->slow_rate); |
| 130 | |
| 131 | for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) |
| 132 | if (gpu->grp_clks[i]) |
| 133 | clk_unprepare(gpu->grp_clks[i]); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int enable_axi(struct msm_gpu *gpu) |
| 139 | { |
| 140 | if (gpu->ebi1_clk) |
| 141 | clk_prepare_enable(gpu->ebi1_clk); |
| 142 | if (gpu->bus_freq) |
| 143 | bs_set(gpu, gpu->bus_freq); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int disable_axi(struct msm_gpu *gpu) |
| 148 | { |
| 149 | if (gpu->ebi1_clk) |
| 150 | clk_disable_unprepare(gpu->ebi1_clk); |
| 151 | if (gpu->bus_freq) |
| 152 | bs_set(gpu, 0); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int msm_gpu_pm_resume(struct msm_gpu *gpu) |
| 157 | { |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 158 | struct drm_device *dev = gpu->dev; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 159 | int ret; |
| 160 | |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 161 | DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt); |
| 162 | |
| 163 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 164 | |
| 165 | if (gpu->active_cnt++ > 0) |
| 166 | return 0; |
| 167 | |
| 168 | if (WARN_ON(gpu->active_cnt <= 0)) |
| 169 | return -EINVAL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 170 | |
| 171 | ret = enable_pwrrail(gpu); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | ret = enable_clk(gpu); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | ret = enable_axi(gpu); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int msm_gpu_pm_suspend(struct msm_gpu *gpu) |
| 187 | { |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 188 | struct drm_device *dev = gpu->dev; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 189 | int ret; |
| 190 | |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 191 | DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt); |
| 192 | |
| 193 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 194 | |
| 195 | if (--gpu->active_cnt > 0) |
| 196 | return 0; |
| 197 | |
| 198 | if (WARN_ON(gpu->active_cnt < 0)) |
| 199 | return -EINVAL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 200 | |
| 201 | ret = disable_axi(gpu); |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | |
| 205 | ret = disable_clk(gpu); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | |
| 209 | ret = disable_pwrrail(gpu); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /* |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 217 | * Inactivity detection (for suspend): |
| 218 | */ |
| 219 | |
| 220 | static void inactive_worker(struct work_struct *work) |
| 221 | { |
| 222 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, inactive_work); |
| 223 | struct drm_device *dev = gpu->dev; |
| 224 | |
| 225 | if (gpu->inactive) |
| 226 | return; |
| 227 | |
| 228 | DBG("%s: inactive!\n", gpu->name); |
| 229 | mutex_lock(&dev->struct_mutex); |
| 230 | if (!(msm_gpu_active(gpu) || gpu->inactive)) { |
| 231 | disable_axi(gpu); |
| 232 | disable_clk(gpu); |
| 233 | gpu->inactive = true; |
| 234 | } |
| 235 | mutex_unlock(&dev->struct_mutex); |
| 236 | } |
| 237 | |
| 238 | static void inactive_handler(unsigned long data) |
| 239 | { |
| 240 | struct msm_gpu *gpu = (struct msm_gpu *)data; |
| 241 | struct msm_drm_private *priv = gpu->dev->dev_private; |
| 242 | |
| 243 | queue_work(priv->wq, &gpu->inactive_work); |
| 244 | } |
| 245 | |
| 246 | /* cancel inactive timer and make sure we are awake: */ |
| 247 | static void inactive_cancel(struct msm_gpu *gpu) |
| 248 | { |
| 249 | DBG("%s", gpu->name); |
| 250 | del_timer(&gpu->inactive_timer); |
| 251 | if (gpu->inactive) { |
| 252 | enable_clk(gpu); |
| 253 | enable_axi(gpu); |
| 254 | gpu->inactive = false; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static void inactive_start(struct msm_gpu *gpu) |
| 259 | { |
| 260 | DBG("%s", gpu->name); |
| 261 | mod_timer(&gpu->inactive_timer, |
| 262 | round_jiffies_up(jiffies + DRM_MSM_INACTIVE_JIFFIES)); |
| 263 | } |
| 264 | |
| 265 | /* |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 266 | * Hangcheck detection for locked gpu: |
| 267 | */ |
| 268 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 269 | static void retire_submits(struct msm_gpu *gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 270 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 271 | static void recover_worker(struct work_struct *work) |
| 272 | { |
| 273 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work); |
| 274 | struct drm_device *dev = gpu->dev; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 275 | uint32_t fence = gpu->funcs->last_fence(gpu); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 276 | |
| 277 | dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name); |
| 278 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 279 | msm_update_fence(gpu->fctx, fence + 1); |
| 280 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 281 | mutex_lock(&dev->struct_mutex); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 282 | if (msm_gpu_active(gpu)) { |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 283 | struct msm_gem_submit *submit; |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 284 | |
| 285 | /* retire completed submits, plus the one that hung: */ |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 286 | retire_submits(gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 287 | |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 288 | inactive_cancel(gpu); |
| 289 | gpu->funcs->recover(gpu); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 290 | |
| 291 | /* replay the remaining submits after the one that hung: */ |
| 292 | list_for_each_entry(submit, &gpu->submit_list, node) { |
| 293 | gpu->funcs->submit(gpu, submit, NULL); |
| 294 | } |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 295 | } |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 296 | mutex_unlock(&dev->struct_mutex); |
| 297 | |
| 298 | msm_gpu_retire(gpu); |
| 299 | } |
| 300 | |
| 301 | static void hangcheck_timer_reset(struct msm_gpu *gpu) |
| 302 | { |
| 303 | DBG("%s", gpu->name); |
| 304 | mod_timer(&gpu->hangcheck_timer, |
| 305 | round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES)); |
| 306 | } |
| 307 | |
| 308 | static void hangcheck_handler(unsigned long data) |
| 309 | { |
| 310 | struct msm_gpu *gpu = (struct msm_gpu *)data; |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 311 | struct drm_device *dev = gpu->dev; |
| 312 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 313 | uint32_t fence = gpu->funcs->last_fence(gpu); |
| 314 | |
| 315 | if (fence != gpu->hangcheck_fence) { |
| 316 | /* some progress has been made.. ya! */ |
| 317 | gpu->hangcheck_fence = fence; |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame] | 318 | } else if (fence < gpu->fctx->last_fence) { |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 319 | /* no progress and not done.. hung! */ |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 320 | gpu->hangcheck_fence = fence; |
Rob Clark | 26791c4 | 2013-09-03 07:12:03 -0400 | [diff] [blame] | 321 | dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n", |
| 322 | gpu->name); |
| 323 | dev_err(dev->dev, "%s: completed fence: %u\n", |
| 324 | gpu->name, fence); |
| 325 | dev_err(dev->dev, "%s: submitted fence: %u\n", |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame] | 326 | gpu->name, gpu->fctx->last_fence); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 327 | queue_work(priv->wq, &gpu->recover_work); |
| 328 | } |
| 329 | |
| 330 | /* if still more pending work, reset the hangcheck timer: */ |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame] | 331 | if (gpu->fctx->last_fence > gpu->hangcheck_fence) |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 332 | hangcheck_timer_reset(gpu); |
Rob Clark | 6b8819c | 2013-09-11 17:14:30 -0400 | [diff] [blame] | 333 | |
| 334 | /* workaround for missing irq: */ |
| 335 | queue_work(priv->wq, &gpu->retire_work); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 339 | * Performance Counters: |
| 340 | */ |
| 341 | |
| 342 | /* called under perf_lock */ |
| 343 | static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs) |
| 344 | { |
| 345 | uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)]; |
| 346 | int i, n = min(ncntrs, gpu->num_perfcntrs); |
| 347 | |
| 348 | /* read current values: */ |
| 349 | for (i = 0; i < gpu->num_perfcntrs; i++) |
| 350 | current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg); |
| 351 | |
| 352 | /* update cntrs: */ |
| 353 | for (i = 0; i < n; i++) |
| 354 | cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i]; |
| 355 | |
| 356 | /* save current values: */ |
| 357 | for (i = 0; i < gpu->num_perfcntrs; i++) |
| 358 | gpu->last_cntrs[i] = current_cntrs[i]; |
| 359 | |
| 360 | return n; |
| 361 | } |
| 362 | |
| 363 | static void update_sw_cntrs(struct msm_gpu *gpu) |
| 364 | { |
| 365 | ktime_t time; |
| 366 | uint32_t elapsed; |
| 367 | unsigned long flags; |
| 368 | |
| 369 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 370 | if (!gpu->perfcntr_active) |
| 371 | goto out; |
| 372 | |
| 373 | time = ktime_get(); |
| 374 | elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time)); |
| 375 | |
| 376 | gpu->totaltime += elapsed; |
| 377 | if (gpu->last_sample.active) |
| 378 | gpu->activetime += elapsed; |
| 379 | |
| 380 | gpu->last_sample.active = msm_gpu_active(gpu); |
| 381 | gpu->last_sample.time = time; |
| 382 | |
| 383 | out: |
| 384 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 385 | } |
| 386 | |
| 387 | void msm_gpu_perfcntr_start(struct msm_gpu *gpu) |
| 388 | { |
| 389 | unsigned long flags; |
| 390 | |
| 391 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 392 | /* we could dynamically enable/disable perfcntr registers too.. */ |
| 393 | gpu->last_sample.active = msm_gpu_active(gpu); |
| 394 | gpu->last_sample.time = ktime_get(); |
| 395 | gpu->activetime = gpu->totaltime = 0; |
| 396 | gpu->perfcntr_active = true; |
| 397 | update_hw_cntrs(gpu, 0, NULL); |
| 398 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 399 | } |
| 400 | |
| 401 | void msm_gpu_perfcntr_stop(struct msm_gpu *gpu) |
| 402 | { |
| 403 | gpu->perfcntr_active = false; |
| 404 | } |
| 405 | |
| 406 | /* returns -errno or # of cntrs sampled */ |
| 407 | int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime, |
| 408 | uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs) |
| 409 | { |
| 410 | unsigned long flags; |
| 411 | int ret; |
| 412 | |
| 413 | spin_lock_irqsave(&gpu->perf_lock, flags); |
| 414 | |
| 415 | if (!gpu->perfcntr_active) { |
| 416 | ret = -EINVAL; |
| 417 | goto out; |
| 418 | } |
| 419 | |
| 420 | *activetime = gpu->activetime; |
| 421 | *totaltime = gpu->totaltime; |
| 422 | |
| 423 | gpu->activetime = gpu->totaltime = 0; |
| 424 | |
| 425 | ret = update_hw_cntrs(gpu, ncntrs, cntrs); |
| 426 | |
| 427 | out: |
| 428 | spin_unlock_irqrestore(&gpu->perf_lock, flags); |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | /* |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 434 | * Cmdstream submission/retirement: |
| 435 | */ |
| 436 | |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 437 | static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit) |
| 438 | { |
| 439 | int i; |
| 440 | |
| 441 | for (i = 0; i < submit->nr_bos; i++) { |
| 442 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
| 443 | /* move to inactive: */ |
| 444 | msm_gem_move_to_inactive(&msm_obj->base); |
| 445 | msm_gem_put_iova(&msm_obj->base, gpu->id); |
| 446 | drm_gem_object_unreference(&msm_obj->base); |
| 447 | } |
| 448 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 449 | fence_put(submit->fence); |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 450 | list_del(&submit->node); |
| 451 | kfree(submit); |
| 452 | } |
| 453 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 454 | static void retire_submits(struct msm_gpu *gpu) |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 455 | { |
| 456 | struct drm_device *dev = gpu->dev; |
| 457 | |
| 458 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 459 | |
| 460 | while (!list_empty(&gpu->submit_list)) { |
| 461 | struct msm_gem_submit *submit; |
| 462 | |
| 463 | submit = list_first_entry(&gpu->submit_list, |
| 464 | struct msm_gem_submit, node); |
| 465 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 466 | if (fence_is_signaled(submit->fence)) { |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 467 | retire_submit(gpu, submit); |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 468 | } else { |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 474 | static void retire_worker(struct work_struct *work) |
| 475 | { |
| 476 | struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work); |
| 477 | struct drm_device *dev = gpu->dev; |
| 478 | uint32_t fence = gpu->funcs->last_fence(gpu); |
| 479 | |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame] | 480 | msm_update_fence(gpu->fctx, fence); |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame] | 481 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 482 | mutex_lock(&dev->struct_mutex); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 483 | retire_submits(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 484 | mutex_unlock(&dev->struct_mutex); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 485 | |
| 486 | if (!msm_gpu_active(gpu)) |
| 487 | inactive_start(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /* call from irq handler to schedule work to retire bo's */ |
| 491 | void msm_gpu_retire(struct msm_gpu *gpu) |
| 492 | { |
| 493 | struct msm_drm_private *priv = gpu->dev->dev_private; |
| 494 | queue_work(priv->wq, &gpu->retire_work); |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 495 | update_sw_cntrs(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /* add bo's to gpu's ring, and kick gpu: */ |
| 499 | int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit, |
| 500 | struct msm_file_private *ctx) |
| 501 | { |
| 502 | struct drm_device *dev = gpu->dev; |
| 503 | struct msm_drm_private *priv = dev->dev_private; |
| 504 | int i, ret; |
| 505 | |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 506 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 507 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 508 | submit->fence = msm_fence_alloc(gpu->fctx); |
| 509 | if (IS_ERR(submit->fence)) { |
| 510 | ret = PTR_ERR(submit->fence); |
| 511 | submit->fence = NULL; |
| 512 | return ret; |
| 513 | } |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 514 | |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 515 | inactive_cancel(gpu); |
| 516 | |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 517 | list_add_tail(&submit->node, &gpu->submit_list); |
| 518 | |
Rob Clark | a7d3c95 | 2014-05-30 14:47:38 -0400 | [diff] [blame] | 519 | msm_rd_dump_submit(submit); |
| 520 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 521 | update_sw_cntrs(gpu); |
| 522 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 523 | for (i = 0; i < submit->nr_bos; i++) { |
| 524 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 525 | uint32_t iova; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 526 | |
| 527 | /* can't happen yet.. but when we add 2d support we'll have |
| 528 | * to deal w/ cross-ring synchronization: |
| 529 | */ |
| 530 | WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu)); |
| 531 | |
Rob Clark | 7d12a27 | 2016-03-16 16:07:38 -0400 | [diff] [blame] | 532 | /* submit takes a reference to the bo and iova until retired: */ |
| 533 | drm_gem_object_reference(&msm_obj->base); |
| 534 | msm_gem_get_iova_locked(&msm_obj->base, |
| 535 | submit->gpu->id, &iova); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 536 | |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 537 | if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE) |
| 538 | msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame^] | 539 | else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ) |
| 540 | msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 541 | } |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 542 | |
| 543 | ret = gpu->funcs->submit(gpu, submit, ctx); |
| 544 | priv->lastctx = ctx; |
| 545 | |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 546 | hangcheck_timer_reset(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 547 | |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * Init/Cleanup: |
| 553 | */ |
| 554 | |
| 555 | static irqreturn_t irq_handler(int irq, void *data) |
| 556 | { |
| 557 | struct msm_gpu *gpu = data; |
| 558 | return gpu->funcs->irq(gpu); |
| 559 | } |
| 560 | |
| 561 | static const char *clk_names[] = { |
| 562 | "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk", |
Rob Clark | de558cd | 2015-05-06 13:14:30 -0400 | [diff] [blame] | 563 | "alt_mem_iface_clk", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev, |
| 567 | struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs, |
| 568 | const char *name, const char *ioname, const char *irqname, int ringsz) |
| 569 | { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 570 | struct iommu_domain *iommu; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 571 | int i, ret; |
| 572 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 573 | if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs))) |
| 574 | gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs); |
| 575 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 576 | gpu->dev = drm; |
| 577 | gpu->funcs = funcs; |
| 578 | gpu->name = name; |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 579 | gpu->inactive = true; |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame] | 580 | gpu->fctx = msm_fence_context_alloc(drm, name); |
| 581 | if (IS_ERR(gpu->fctx)) { |
| 582 | ret = PTR_ERR(gpu->fctx); |
| 583 | gpu->fctx = NULL; |
| 584 | goto fail; |
| 585 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 586 | |
| 587 | INIT_LIST_HEAD(&gpu->active_list); |
| 588 | INIT_WORK(&gpu->retire_work, retire_worker); |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 589 | INIT_WORK(&gpu->inactive_work, inactive_worker); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 590 | INIT_WORK(&gpu->recover_work, recover_worker); |
| 591 | |
Rob Clark | 1a370be | 2015-06-07 13:46:04 -0400 | [diff] [blame] | 592 | INIT_LIST_HEAD(&gpu->submit_list); |
| 593 | |
Rob Clark | 37d77c3 | 2014-01-11 16:25:08 -0500 | [diff] [blame] | 594 | setup_timer(&gpu->inactive_timer, inactive_handler, |
| 595 | (unsigned long)gpu); |
Rob Clark | bd6f82d | 2013-08-24 14:20:38 -0400 | [diff] [blame] | 596 | setup_timer(&gpu->hangcheck_timer, hangcheck_handler, |
| 597 | (unsigned long)gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 598 | |
Rob Clark | 70c70f0 | 2014-05-30 14:49:43 -0400 | [diff] [blame] | 599 | spin_lock_init(&gpu->perf_lock); |
| 600 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 601 | BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks)); |
| 602 | |
| 603 | /* Map registers: */ |
| 604 | gpu->mmio = msm_ioremap(pdev, ioname, name); |
| 605 | if (IS_ERR(gpu->mmio)) { |
| 606 | ret = PTR_ERR(gpu->mmio); |
| 607 | goto fail; |
| 608 | } |
| 609 | |
| 610 | /* Get Interrupt: */ |
| 611 | gpu->irq = platform_get_irq_byname(pdev, irqname); |
| 612 | if (gpu->irq < 0) { |
| 613 | ret = gpu->irq; |
| 614 | dev_err(drm->dev, "failed to get irq: %d\n", ret); |
| 615 | goto fail; |
| 616 | } |
| 617 | |
| 618 | ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, |
| 619 | IRQF_TRIGGER_HIGH, gpu->name, gpu); |
| 620 | if (ret) { |
| 621 | dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret); |
| 622 | goto fail; |
| 623 | } |
| 624 | |
| 625 | /* Acquire clocks: */ |
| 626 | for (i = 0; i < ARRAY_SIZE(clk_names); i++) { |
| 627 | gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]); |
| 628 | DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]); |
| 629 | if (IS_ERR(gpu->grp_clks[i])) |
| 630 | gpu->grp_clks[i] = NULL; |
| 631 | } |
| 632 | |
| 633 | gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk"); |
| 634 | DBG("ebi1_clk: %p", gpu->ebi1_clk); |
| 635 | if (IS_ERR(gpu->ebi1_clk)) |
| 636 | gpu->ebi1_clk = NULL; |
| 637 | |
| 638 | /* Acquire regulators: */ |
| 639 | gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd"); |
| 640 | DBG("gpu_reg: %p", gpu->gpu_reg); |
| 641 | if (IS_ERR(gpu->gpu_reg)) |
| 642 | gpu->gpu_reg = NULL; |
| 643 | |
| 644 | gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx"); |
| 645 | DBG("gpu_cx: %p", gpu->gpu_cx); |
| 646 | if (IS_ERR(gpu->gpu_cx)) |
| 647 | gpu->gpu_cx = NULL; |
| 648 | |
| 649 | /* Setup IOMMU.. eventually we will (I think) do this once per context |
| 650 | * and have separate page tables per context. For now, to keep things |
| 651 | * simple and to get something working, just use a single address space: |
| 652 | */ |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 653 | iommu = iommu_domain_alloc(&platform_bus_type); |
| 654 | if (iommu) { |
| 655 | dev_info(drm->dev, "%s: using IOMMU\n", name); |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 656 | gpu->mmu = msm_iommu_new(&pdev->dev, iommu); |
Stephane Viau | 5e921b1 | 2015-09-15 08:41:46 -0400 | [diff] [blame] | 657 | if (IS_ERR(gpu->mmu)) { |
| 658 | ret = PTR_ERR(gpu->mmu); |
| 659 | dev_err(drm->dev, "failed to init iommu: %d\n", ret); |
| 660 | gpu->mmu = NULL; |
| 661 | iommu_domain_free(iommu); |
| 662 | goto fail; |
| 663 | } |
| 664 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 665 | } else { |
| 666 | dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 667 | } |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 668 | gpu->id = msm_register_mmu(drm, gpu->mmu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 669 | |
Rob Clark | a1ad352 | 2014-07-11 11:59:22 -0400 | [diff] [blame] | 670 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 671 | /* Create ringbuffer: */ |
Rob Clark | a1ad352 | 2014-07-11 11:59:22 -0400 | [diff] [blame] | 672 | mutex_lock(&drm->struct_mutex); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 673 | gpu->rb = msm_ringbuffer_new(gpu, ringsz); |
Rob Clark | a1ad352 | 2014-07-11 11:59:22 -0400 | [diff] [blame] | 674 | mutex_unlock(&drm->struct_mutex); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 675 | if (IS_ERR(gpu->rb)) { |
| 676 | ret = PTR_ERR(gpu->rb); |
| 677 | gpu->rb = NULL; |
| 678 | dev_err(drm->dev, "could not create ringbuffer: %d\n", ret); |
| 679 | goto fail; |
| 680 | } |
| 681 | |
Rob Clark | bf2b33af | 2013-11-15 09:03:15 -0500 | [diff] [blame] | 682 | bs_init(gpu); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 683 | |
| 684 | return 0; |
| 685 | |
| 686 | fail: |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | void msm_gpu_cleanup(struct msm_gpu *gpu) |
| 691 | { |
| 692 | DBG("%s", gpu->name); |
| 693 | |
| 694 | WARN_ON(!list_empty(&gpu->active_list)); |
| 695 | |
| 696 | bs_fini(gpu); |
| 697 | |
| 698 | if (gpu->rb) { |
| 699 | if (gpu->rb_iova) |
| 700 | msm_gem_put_iova(gpu->rb->bo, gpu->id); |
| 701 | msm_ringbuffer_destroy(gpu->rb); |
| 702 | } |
| 703 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 704 | if (gpu->mmu) |
| 705 | gpu->mmu->funcs->destroy(gpu->mmu); |
Rob Clark | ca762a8 | 2016-03-15 17:22:13 -0400 | [diff] [blame] | 706 | |
| 707 | if (gpu->fctx) |
| 708 | msm_fence_context_free(gpu->fctx); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 709 | } |