Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2014 Broadcom |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/platform_device.h> |
Eric Anholt | 001bdb5 | 2016-02-05 17:41:49 -0800 | [diff] [blame^] | 26 | #include <linux/pm_runtime.h> |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 27 | #include <linux/device.h> |
| 28 | #include <linux/io.h> |
| 29 | |
| 30 | #include "uapi/drm/vc4_drm.h" |
| 31 | #include "vc4_drv.h" |
| 32 | #include "vc4_regs.h" |
| 33 | #include "vc4_trace.h" |
| 34 | |
| 35 | static void |
| 36 | vc4_queue_hangcheck(struct drm_device *dev) |
| 37 | { |
| 38 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 39 | |
| 40 | mod_timer(&vc4->hangcheck.timer, |
| 41 | round_jiffies_up(jiffies + msecs_to_jiffies(100))); |
| 42 | } |
| 43 | |
Eric Anholt | 2146136 | 2015-10-30 10:09:02 -0700 | [diff] [blame] | 44 | struct vc4_hang_state { |
| 45 | struct drm_vc4_get_hang_state user_state; |
| 46 | |
| 47 | u32 bo_count; |
| 48 | struct drm_gem_object **bo; |
| 49 | }; |
| 50 | |
| 51 | static void |
| 52 | vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state) |
| 53 | { |
| 54 | unsigned int i; |
| 55 | |
| 56 | mutex_lock(&dev->struct_mutex); |
| 57 | for (i = 0; i < state->user_state.bo_count; i++) |
| 58 | drm_gem_object_unreference(state->bo[i]); |
| 59 | mutex_unlock(&dev->struct_mutex); |
| 60 | |
| 61 | kfree(state); |
| 62 | } |
| 63 | |
| 64 | int |
| 65 | vc4_get_hang_state_ioctl(struct drm_device *dev, void *data, |
| 66 | struct drm_file *file_priv) |
| 67 | { |
| 68 | struct drm_vc4_get_hang_state *get_state = data; |
| 69 | struct drm_vc4_get_hang_state_bo *bo_state; |
| 70 | struct vc4_hang_state *kernel_state; |
| 71 | struct drm_vc4_get_hang_state *state; |
| 72 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 73 | unsigned long irqflags; |
| 74 | u32 i; |
Dan Carpenter | 65c4777 | 2015-12-17 15:36:28 +0300 | [diff] [blame] | 75 | int ret = 0; |
Eric Anholt | 2146136 | 2015-10-30 10:09:02 -0700 | [diff] [blame] | 76 | |
| 77 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 78 | kernel_state = vc4->hang_state; |
| 79 | if (!kernel_state) { |
| 80 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 81 | return -ENOENT; |
| 82 | } |
| 83 | state = &kernel_state->user_state; |
| 84 | |
| 85 | /* If the user's array isn't big enough, just return the |
| 86 | * required array size. |
| 87 | */ |
| 88 | if (get_state->bo_count < state->bo_count) { |
| 89 | get_state->bo_count = state->bo_count; |
| 90 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | vc4->hang_state = NULL; |
| 95 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 96 | |
| 97 | /* Save the user's BO pointer, so we don't stomp it with the memcpy. */ |
| 98 | state->bo = get_state->bo; |
| 99 | memcpy(get_state, state, sizeof(*state)); |
| 100 | |
| 101 | bo_state = kcalloc(state->bo_count, sizeof(*bo_state), GFP_KERNEL); |
| 102 | if (!bo_state) { |
| 103 | ret = -ENOMEM; |
| 104 | goto err_free; |
| 105 | } |
| 106 | |
| 107 | for (i = 0; i < state->bo_count; i++) { |
| 108 | struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]); |
| 109 | u32 handle; |
| 110 | |
| 111 | ret = drm_gem_handle_create(file_priv, kernel_state->bo[i], |
| 112 | &handle); |
| 113 | |
| 114 | if (ret) { |
| 115 | state->bo_count = i - 1; |
| 116 | goto err; |
| 117 | } |
| 118 | bo_state[i].handle = handle; |
| 119 | bo_state[i].paddr = vc4_bo->base.paddr; |
| 120 | bo_state[i].size = vc4_bo->base.base.size; |
| 121 | } |
| 122 | |
Dan Carpenter | 65c4777 | 2015-12-17 15:36:28 +0300 | [diff] [blame] | 123 | if (copy_to_user((void __user *)(uintptr_t)get_state->bo, |
| 124 | bo_state, |
| 125 | state->bo_count * sizeof(*bo_state))) |
| 126 | ret = -EFAULT; |
| 127 | |
Eric Anholt | 2146136 | 2015-10-30 10:09:02 -0700 | [diff] [blame] | 128 | kfree(bo_state); |
| 129 | |
| 130 | err_free: |
| 131 | |
| 132 | vc4_free_hang_state(dev, kernel_state); |
| 133 | |
| 134 | err: |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | vc4_save_hang_state(struct drm_device *dev) |
| 140 | { |
| 141 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 142 | struct drm_vc4_get_hang_state *state; |
| 143 | struct vc4_hang_state *kernel_state; |
| 144 | struct vc4_exec_info *exec; |
| 145 | struct vc4_bo *bo; |
| 146 | unsigned long irqflags; |
| 147 | unsigned int i, unref_list_count; |
| 148 | |
Dan Carpenter | 7e5082f | 2015-12-17 15:39:08 +0300 | [diff] [blame] | 149 | kernel_state = kcalloc(1, sizeof(*kernel_state), GFP_KERNEL); |
Eric Anholt | 2146136 | 2015-10-30 10:09:02 -0700 | [diff] [blame] | 150 | if (!kernel_state) |
| 151 | return; |
| 152 | |
| 153 | state = &kernel_state->user_state; |
| 154 | |
| 155 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 156 | exec = vc4_first_job(vc4); |
| 157 | if (!exec) { |
| 158 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | unref_list_count = 0; |
| 163 | list_for_each_entry(bo, &exec->unref_list, unref_head) |
| 164 | unref_list_count++; |
| 165 | |
| 166 | state->bo_count = exec->bo_count + unref_list_count; |
| 167 | kernel_state->bo = kcalloc(state->bo_count, sizeof(*kernel_state->bo), |
| 168 | GFP_ATOMIC); |
| 169 | if (!kernel_state->bo) { |
| 170 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | for (i = 0; i < exec->bo_count; i++) { |
| 175 | drm_gem_object_reference(&exec->bo[i]->base); |
| 176 | kernel_state->bo[i] = &exec->bo[i]->base; |
| 177 | } |
| 178 | |
| 179 | list_for_each_entry(bo, &exec->unref_list, unref_head) { |
| 180 | drm_gem_object_reference(&bo->base.base); |
| 181 | kernel_state->bo[i] = &bo->base.base; |
| 182 | i++; |
| 183 | } |
| 184 | |
| 185 | state->start_bin = exec->ct0ca; |
| 186 | state->start_render = exec->ct1ca; |
| 187 | |
| 188 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 189 | |
| 190 | state->ct0ca = V3D_READ(V3D_CTNCA(0)); |
| 191 | state->ct0ea = V3D_READ(V3D_CTNEA(0)); |
| 192 | |
| 193 | state->ct1ca = V3D_READ(V3D_CTNCA(1)); |
| 194 | state->ct1ea = V3D_READ(V3D_CTNEA(1)); |
| 195 | |
| 196 | state->ct0cs = V3D_READ(V3D_CTNCS(0)); |
| 197 | state->ct1cs = V3D_READ(V3D_CTNCS(1)); |
| 198 | |
| 199 | state->ct0ra0 = V3D_READ(V3D_CT00RA0); |
| 200 | state->ct1ra0 = V3D_READ(V3D_CT01RA0); |
| 201 | |
| 202 | state->bpca = V3D_READ(V3D_BPCA); |
| 203 | state->bpcs = V3D_READ(V3D_BPCS); |
| 204 | state->bpoa = V3D_READ(V3D_BPOA); |
| 205 | state->bpos = V3D_READ(V3D_BPOS); |
| 206 | |
| 207 | state->vpmbase = V3D_READ(V3D_VPMBASE); |
| 208 | |
| 209 | state->dbge = V3D_READ(V3D_DBGE); |
| 210 | state->fdbgo = V3D_READ(V3D_FDBGO); |
| 211 | state->fdbgb = V3D_READ(V3D_FDBGB); |
| 212 | state->fdbgr = V3D_READ(V3D_FDBGR); |
| 213 | state->fdbgs = V3D_READ(V3D_FDBGS); |
| 214 | state->errstat = V3D_READ(V3D_ERRSTAT); |
| 215 | |
| 216 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 217 | if (vc4->hang_state) { |
| 218 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 219 | vc4_free_hang_state(dev, kernel_state); |
| 220 | } else { |
| 221 | vc4->hang_state = kernel_state; |
| 222 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 223 | } |
| 224 | } |
| 225 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 226 | static void |
| 227 | vc4_reset(struct drm_device *dev) |
| 228 | { |
| 229 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 230 | |
| 231 | DRM_INFO("Resetting GPU.\n"); |
| 232 | vc4_v3d_set_power(vc4, false); |
| 233 | vc4_v3d_set_power(vc4, true); |
| 234 | |
| 235 | vc4_irq_reset(dev); |
| 236 | |
| 237 | /* Rearm the hangcheck -- another job might have been waiting |
| 238 | * for our hung one to get kicked off, and vc4_irq_reset() |
| 239 | * would have started it. |
| 240 | */ |
| 241 | vc4_queue_hangcheck(dev); |
| 242 | } |
| 243 | |
| 244 | static void |
| 245 | vc4_reset_work(struct work_struct *work) |
| 246 | { |
| 247 | struct vc4_dev *vc4 = |
| 248 | container_of(work, struct vc4_dev, hangcheck.reset_work); |
| 249 | |
Eric Anholt | 2146136 | 2015-10-30 10:09:02 -0700 | [diff] [blame] | 250 | vc4_save_hang_state(vc4->dev); |
| 251 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 252 | vc4_reset(vc4->dev); |
| 253 | } |
| 254 | |
| 255 | static void |
| 256 | vc4_hangcheck_elapsed(unsigned long data) |
| 257 | { |
| 258 | struct drm_device *dev = (struct drm_device *)data; |
| 259 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 260 | uint32_t ct0ca, ct1ca; |
Eric Anholt | c4ce60d | 2016-02-08 11:19:14 -0800 | [diff] [blame] | 261 | unsigned long irqflags; |
| 262 | struct vc4_exec_info *exec; |
| 263 | |
| 264 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 265 | exec = vc4_first_job(vc4); |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 266 | |
| 267 | /* If idle, we can stop watching for hangs. */ |
Eric Anholt | c4ce60d | 2016-02-08 11:19:14 -0800 | [diff] [blame] | 268 | if (!exec) { |
| 269 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 270 | return; |
Eric Anholt | c4ce60d | 2016-02-08 11:19:14 -0800 | [diff] [blame] | 271 | } |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 272 | |
| 273 | ct0ca = V3D_READ(V3D_CTNCA(0)); |
| 274 | ct1ca = V3D_READ(V3D_CTNCA(1)); |
| 275 | |
| 276 | /* If we've made any progress in execution, rearm the timer |
| 277 | * and wait. |
| 278 | */ |
Eric Anholt | c4ce60d | 2016-02-08 11:19:14 -0800 | [diff] [blame] | 279 | if (ct0ca != exec->last_ct0ca || ct1ca != exec->last_ct1ca) { |
| 280 | exec->last_ct0ca = ct0ca; |
| 281 | exec->last_ct1ca = ct1ca; |
| 282 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 283 | vc4_queue_hangcheck(dev); |
| 284 | return; |
| 285 | } |
| 286 | |
Eric Anholt | c4ce60d | 2016-02-08 11:19:14 -0800 | [diff] [blame] | 287 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 288 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 289 | /* We've gone too long with no progress, reset. This has to |
| 290 | * be done from a work struct, since resetting can sleep and |
| 291 | * this timer hook isn't allowed to. |
| 292 | */ |
| 293 | schedule_work(&vc4->hangcheck.reset_work); |
| 294 | } |
| 295 | |
| 296 | static void |
| 297 | submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end) |
| 298 | { |
| 299 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 300 | |
| 301 | /* Set the current and end address of the control list. |
| 302 | * Writing the end register is what starts the job. |
| 303 | */ |
| 304 | V3D_WRITE(V3D_CTNCA(thread), start); |
| 305 | V3D_WRITE(V3D_CTNEA(thread), end); |
| 306 | } |
| 307 | |
| 308 | int |
| 309 | vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns, |
| 310 | bool interruptible) |
| 311 | { |
| 312 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 313 | int ret = 0; |
| 314 | unsigned long timeout_expire; |
| 315 | DEFINE_WAIT(wait); |
| 316 | |
| 317 | if (vc4->finished_seqno >= seqno) |
| 318 | return 0; |
| 319 | |
| 320 | if (timeout_ns == 0) |
| 321 | return -ETIME; |
| 322 | |
| 323 | timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns); |
| 324 | |
| 325 | trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns); |
| 326 | for (;;) { |
| 327 | prepare_to_wait(&vc4->job_wait_queue, &wait, |
| 328 | interruptible ? TASK_INTERRUPTIBLE : |
| 329 | TASK_UNINTERRUPTIBLE); |
| 330 | |
| 331 | if (interruptible && signal_pending(current)) { |
| 332 | ret = -ERESTARTSYS; |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (vc4->finished_seqno >= seqno) |
| 337 | break; |
| 338 | |
| 339 | if (timeout_ns != ~0ull) { |
| 340 | if (time_after_eq(jiffies, timeout_expire)) { |
| 341 | ret = -ETIME; |
| 342 | break; |
| 343 | } |
| 344 | schedule_timeout(timeout_expire - jiffies); |
| 345 | } else { |
| 346 | schedule(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | finish_wait(&vc4->job_wait_queue, &wait); |
| 351 | trace_vc4_wait_for_seqno_end(dev, seqno); |
| 352 | |
Eric Anholt | 13cf890 | 2016-01-25 14:32:41 -0800 | [diff] [blame] | 353 | return ret; |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static void |
| 357 | vc4_flush_caches(struct drm_device *dev) |
| 358 | { |
| 359 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 360 | |
| 361 | /* Flush the GPU L2 caches. These caches sit on top of system |
| 362 | * L3 (the 128kb or so shared with the CPU), and are |
| 363 | * non-allocating in the L3. |
| 364 | */ |
| 365 | V3D_WRITE(V3D_L2CACTL, |
| 366 | V3D_L2CACTL_L2CCLR); |
| 367 | |
| 368 | V3D_WRITE(V3D_SLCACTL, |
| 369 | VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) | |
| 370 | VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) | |
| 371 | VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) | |
| 372 | VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC)); |
| 373 | } |
| 374 | |
| 375 | /* Sets the registers for the next job to be actually be executed in |
| 376 | * the hardware. |
| 377 | * |
| 378 | * The job_lock should be held during this. |
| 379 | */ |
| 380 | void |
| 381 | vc4_submit_next_job(struct drm_device *dev) |
| 382 | { |
| 383 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 384 | struct vc4_exec_info *exec = vc4_first_job(vc4); |
| 385 | |
| 386 | if (!exec) |
| 387 | return; |
| 388 | |
| 389 | vc4_flush_caches(dev); |
| 390 | |
| 391 | /* Disable the binner's pre-loaded overflow memory address */ |
| 392 | V3D_WRITE(V3D_BPOA, 0); |
| 393 | V3D_WRITE(V3D_BPOS, 0); |
| 394 | |
| 395 | if (exec->ct0ca != exec->ct0ea) |
| 396 | submit_cl(dev, 0, exec->ct0ca, exec->ct0ea); |
| 397 | submit_cl(dev, 1, exec->ct1ca, exec->ct1ea); |
| 398 | } |
| 399 | |
| 400 | static void |
| 401 | vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno) |
| 402 | { |
| 403 | struct vc4_bo *bo; |
| 404 | unsigned i; |
| 405 | |
| 406 | for (i = 0; i < exec->bo_count; i++) { |
| 407 | bo = to_vc4_bo(&exec->bo[i]->base); |
| 408 | bo->seqno = seqno; |
| 409 | } |
| 410 | |
| 411 | list_for_each_entry(bo, &exec->unref_list, unref_head) { |
| 412 | bo->seqno = seqno; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* Queues a struct vc4_exec_info for execution. If no job is |
| 417 | * currently executing, then submits it. |
| 418 | * |
| 419 | * Unlike most GPUs, our hardware only handles one command list at a |
| 420 | * time. To queue multiple jobs at once, we'd need to edit the |
| 421 | * previous command list to have a jump to the new one at the end, and |
| 422 | * then bump the end address. That's a change for a later date, |
| 423 | * though. |
| 424 | */ |
| 425 | static void |
| 426 | vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec) |
| 427 | { |
| 428 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 429 | uint64_t seqno; |
| 430 | unsigned long irqflags; |
| 431 | |
| 432 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 433 | |
| 434 | seqno = ++vc4->emit_seqno; |
| 435 | exec->seqno = seqno; |
| 436 | vc4_update_bo_seqnos(exec, seqno); |
| 437 | |
| 438 | list_add_tail(&exec->head, &vc4->job_list); |
| 439 | |
| 440 | /* If no job was executing, kick ours off. Otherwise, it'll |
| 441 | * get started when the previous job's frame done interrupt |
| 442 | * occurs. |
| 443 | */ |
| 444 | if (vc4_first_job(vc4) == exec) { |
| 445 | vc4_submit_next_job(dev); |
| 446 | vc4_queue_hangcheck(dev); |
| 447 | } |
| 448 | |
| 449 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Looks up a bunch of GEM handles for BOs and stores the array for |
| 454 | * use in the command validator that actually writes relocated |
| 455 | * addresses pointing to them. |
| 456 | */ |
| 457 | static int |
| 458 | vc4_cl_lookup_bos(struct drm_device *dev, |
| 459 | struct drm_file *file_priv, |
| 460 | struct vc4_exec_info *exec) |
| 461 | { |
| 462 | struct drm_vc4_submit_cl *args = exec->args; |
| 463 | uint32_t *handles; |
| 464 | int ret = 0; |
| 465 | int i; |
| 466 | |
| 467 | exec->bo_count = args->bo_handle_count; |
| 468 | |
| 469 | if (!exec->bo_count) { |
| 470 | /* See comment on bo_index for why we have to check |
| 471 | * this. |
| 472 | */ |
| 473 | DRM_ERROR("Rendering requires BOs to validate\n"); |
| 474 | return -EINVAL; |
| 475 | } |
| 476 | |
| 477 | exec->bo = kcalloc(exec->bo_count, sizeof(struct drm_gem_cma_object *), |
| 478 | GFP_KERNEL); |
| 479 | if (!exec->bo) { |
| 480 | DRM_ERROR("Failed to allocate validated BO pointers\n"); |
| 481 | return -ENOMEM; |
| 482 | } |
| 483 | |
| 484 | handles = drm_malloc_ab(exec->bo_count, sizeof(uint32_t)); |
| 485 | if (!handles) { |
| 486 | DRM_ERROR("Failed to allocate incoming GEM handles\n"); |
| 487 | goto fail; |
| 488 | } |
| 489 | |
| 490 | ret = copy_from_user(handles, |
| 491 | (void __user *)(uintptr_t)args->bo_handles, |
| 492 | exec->bo_count * sizeof(uint32_t)); |
| 493 | if (ret) { |
| 494 | DRM_ERROR("Failed to copy in GEM handles\n"); |
| 495 | goto fail; |
| 496 | } |
| 497 | |
| 498 | spin_lock(&file_priv->table_lock); |
| 499 | for (i = 0; i < exec->bo_count; i++) { |
| 500 | struct drm_gem_object *bo = idr_find(&file_priv->object_idr, |
| 501 | handles[i]); |
| 502 | if (!bo) { |
| 503 | DRM_ERROR("Failed to look up GEM BO %d: %d\n", |
| 504 | i, handles[i]); |
| 505 | ret = -EINVAL; |
| 506 | spin_unlock(&file_priv->table_lock); |
| 507 | goto fail; |
| 508 | } |
| 509 | drm_gem_object_reference(bo); |
| 510 | exec->bo[i] = (struct drm_gem_cma_object *)bo; |
| 511 | } |
| 512 | spin_unlock(&file_priv->table_lock); |
| 513 | |
| 514 | fail: |
| 515 | kfree(handles); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int |
| 520 | vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec) |
| 521 | { |
| 522 | struct drm_vc4_submit_cl *args = exec->args; |
| 523 | void *temp = NULL; |
| 524 | void *bin; |
| 525 | int ret = 0; |
| 526 | uint32_t bin_offset = 0; |
| 527 | uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size, |
| 528 | 16); |
| 529 | uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size; |
| 530 | uint32_t exec_size = uniforms_offset + args->uniforms_size; |
| 531 | uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) * |
| 532 | args->shader_rec_count); |
| 533 | struct vc4_bo *bo; |
| 534 | |
| 535 | if (uniforms_offset < shader_rec_offset || |
| 536 | exec_size < uniforms_offset || |
| 537 | args->shader_rec_count >= (UINT_MAX / |
| 538 | sizeof(struct vc4_shader_state)) || |
| 539 | temp_size < exec_size) { |
| 540 | DRM_ERROR("overflow in exec arguments\n"); |
| 541 | goto fail; |
| 542 | } |
| 543 | |
| 544 | /* Allocate space where we'll store the copied in user command lists |
| 545 | * and shader records. |
| 546 | * |
| 547 | * We don't just copy directly into the BOs because we need to |
| 548 | * read the contents back for validation, and I think the |
| 549 | * bo->vaddr is uncached access. |
| 550 | */ |
| 551 | temp = kmalloc(temp_size, GFP_KERNEL); |
| 552 | if (!temp) { |
| 553 | DRM_ERROR("Failed to allocate storage for copying " |
| 554 | "in bin/render CLs.\n"); |
| 555 | ret = -ENOMEM; |
| 556 | goto fail; |
| 557 | } |
| 558 | bin = temp + bin_offset; |
| 559 | exec->shader_rec_u = temp + shader_rec_offset; |
| 560 | exec->uniforms_u = temp + uniforms_offset; |
| 561 | exec->shader_state = temp + exec_size; |
| 562 | exec->shader_state_size = args->shader_rec_count; |
| 563 | |
Dan Carpenter | 65c4777 | 2015-12-17 15:36:28 +0300 | [diff] [blame] | 564 | if (copy_from_user(bin, |
| 565 | (void __user *)(uintptr_t)args->bin_cl, |
| 566 | args->bin_cl_size)) { |
| 567 | ret = -EFAULT; |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 568 | goto fail; |
| 569 | } |
| 570 | |
Dan Carpenter | 65c4777 | 2015-12-17 15:36:28 +0300 | [diff] [blame] | 571 | if (copy_from_user(exec->shader_rec_u, |
| 572 | (void __user *)(uintptr_t)args->shader_rec, |
| 573 | args->shader_rec_size)) { |
| 574 | ret = -EFAULT; |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 575 | goto fail; |
| 576 | } |
| 577 | |
Dan Carpenter | 65c4777 | 2015-12-17 15:36:28 +0300 | [diff] [blame] | 578 | if (copy_from_user(exec->uniforms_u, |
| 579 | (void __user *)(uintptr_t)args->uniforms, |
| 580 | args->uniforms_size)) { |
| 581 | ret = -EFAULT; |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 582 | goto fail; |
| 583 | } |
| 584 | |
| 585 | bo = vc4_bo_create(dev, exec_size, true); |
Eric Anholt | 2c68f1f | 2016-01-25 14:13:12 -0800 | [diff] [blame] | 586 | if (IS_ERR(bo)) { |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 587 | DRM_ERROR("Couldn't allocate BO for binning\n"); |
Eric Anholt | 2c68f1f | 2016-01-25 14:13:12 -0800 | [diff] [blame] | 588 | ret = PTR_ERR(bo); |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 589 | goto fail; |
| 590 | } |
| 591 | exec->exec_bo = &bo->base; |
| 592 | |
| 593 | list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head, |
| 594 | &exec->unref_list); |
| 595 | |
| 596 | exec->ct0ca = exec->exec_bo->paddr + bin_offset; |
| 597 | |
| 598 | exec->bin_u = bin; |
| 599 | |
| 600 | exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset; |
| 601 | exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset; |
| 602 | exec->shader_rec_size = args->shader_rec_size; |
| 603 | |
| 604 | exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset; |
| 605 | exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset; |
| 606 | exec->uniforms_size = args->uniforms_size; |
| 607 | |
| 608 | ret = vc4_validate_bin_cl(dev, |
| 609 | exec->exec_bo->vaddr + bin_offset, |
| 610 | bin, |
| 611 | exec); |
| 612 | if (ret) |
| 613 | goto fail; |
| 614 | |
| 615 | ret = vc4_validate_shader_recs(dev, exec); |
| 616 | |
| 617 | fail: |
| 618 | kfree(temp); |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | static void |
| 623 | vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec) |
| 624 | { |
Eric Anholt | 001bdb5 | 2016-02-05 17:41:49 -0800 | [diff] [blame^] | 625 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 626 | unsigned i; |
| 627 | |
| 628 | /* Need the struct lock for drm_gem_object_unreference(). */ |
| 629 | mutex_lock(&dev->struct_mutex); |
| 630 | if (exec->bo) { |
| 631 | for (i = 0; i < exec->bo_count; i++) |
| 632 | drm_gem_object_unreference(&exec->bo[i]->base); |
| 633 | kfree(exec->bo); |
| 634 | } |
| 635 | |
| 636 | while (!list_empty(&exec->unref_list)) { |
| 637 | struct vc4_bo *bo = list_first_entry(&exec->unref_list, |
| 638 | struct vc4_bo, unref_head); |
| 639 | list_del(&bo->unref_head); |
| 640 | drm_gem_object_unreference(&bo->base.base); |
| 641 | } |
| 642 | mutex_unlock(&dev->struct_mutex); |
| 643 | |
Eric Anholt | 001bdb5 | 2016-02-05 17:41:49 -0800 | [diff] [blame^] | 644 | pm_runtime_put(&vc4->v3d->pdev->dev); |
| 645 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 646 | kfree(exec); |
| 647 | } |
| 648 | |
| 649 | void |
| 650 | vc4_job_handle_completed(struct vc4_dev *vc4) |
| 651 | { |
| 652 | unsigned long irqflags; |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 653 | struct vc4_seqno_cb *cb, *cb_temp; |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 654 | |
| 655 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 656 | while (!list_empty(&vc4->job_done_list)) { |
| 657 | struct vc4_exec_info *exec = |
| 658 | list_first_entry(&vc4->job_done_list, |
| 659 | struct vc4_exec_info, head); |
| 660 | list_del(&exec->head); |
| 661 | |
| 662 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 663 | vc4_complete_exec(vc4->dev, exec); |
| 664 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 665 | } |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 666 | |
| 667 | list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) { |
| 668 | if (cb->seqno <= vc4->finished_seqno) { |
| 669 | list_del_init(&cb->work.entry); |
| 670 | schedule_work(&cb->work); |
| 671 | } |
| 672 | } |
| 673 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 674 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 675 | } |
| 676 | |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 677 | static void vc4_seqno_cb_work(struct work_struct *work) |
| 678 | { |
| 679 | struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work); |
| 680 | |
| 681 | cb->func(cb); |
| 682 | } |
| 683 | |
| 684 | int vc4_queue_seqno_cb(struct drm_device *dev, |
| 685 | struct vc4_seqno_cb *cb, uint64_t seqno, |
| 686 | void (*func)(struct vc4_seqno_cb *cb)) |
| 687 | { |
| 688 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 689 | int ret = 0; |
| 690 | unsigned long irqflags; |
| 691 | |
| 692 | cb->func = func; |
| 693 | INIT_WORK(&cb->work, vc4_seqno_cb_work); |
| 694 | |
| 695 | spin_lock_irqsave(&vc4->job_lock, irqflags); |
| 696 | if (seqno > vc4->finished_seqno) { |
| 697 | cb->seqno = seqno; |
| 698 | list_add_tail(&cb->work.entry, &vc4->seqno_cb_list); |
| 699 | } else { |
| 700 | schedule_work(&cb->work); |
| 701 | } |
| 702 | spin_unlock_irqrestore(&vc4->job_lock, irqflags); |
| 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 707 | /* Scheduled when any job has been completed, this walks the list of |
| 708 | * jobs that had completed and unrefs their BOs and frees their exec |
| 709 | * structs. |
| 710 | */ |
| 711 | static void |
| 712 | vc4_job_done_work(struct work_struct *work) |
| 713 | { |
| 714 | struct vc4_dev *vc4 = |
| 715 | container_of(work, struct vc4_dev, job_done_work); |
| 716 | |
| 717 | vc4_job_handle_completed(vc4); |
| 718 | } |
| 719 | |
| 720 | static int |
| 721 | vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev, |
| 722 | uint64_t seqno, |
| 723 | uint64_t *timeout_ns) |
| 724 | { |
| 725 | unsigned long start = jiffies; |
| 726 | int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true); |
| 727 | |
| 728 | if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) { |
| 729 | uint64_t delta = jiffies_to_nsecs(jiffies - start); |
| 730 | |
| 731 | if (*timeout_ns >= delta) |
| 732 | *timeout_ns -= delta; |
| 733 | } |
| 734 | |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | int |
| 739 | vc4_wait_seqno_ioctl(struct drm_device *dev, void *data, |
| 740 | struct drm_file *file_priv) |
| 741 | { |
| 742 | struct drm_vc4_wait_seqno *args = data; |
| 743 | |
| 744 | return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno, |
| 745 | &args->timeout_ns); |
| 746 | } |
| 747 | |
| 748 | int |
| 749 | vc4_wait_bo_ioctl(struct drm_device *dev, void *data, |
| 750 | struct drm_file *file_priv) |
| 751 | { |
| 752 | int ret; |
| 753 | struct drm_vc4_wait_bo *args = data; |
| 754 | struct drm_gem_object *gem_obj; |
| 755 | struct vc4_bo *bo; |
| 756 | |
Eric Anholt | e001523 | 2016-01-25 13:05:00 -0800 | [diff] [blame] | 757 | if (args->pad != 0) |
| 758 | return -EINVAL; |
| 759 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 760 | gem_obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 761 | if (!gem_obj) { |
| 762 | DRM_ERROR("Failed to look up GEM BO %d\n", args->handle); |
| 763 | return -EINVAL; |
| 764 | } |
| 765 | bo = to_vc4_bo(gem_obj); |
| 766 | |
| 767 | ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno, |
| 768 | &args->timeout_ns); |
| 769 | |
| 770 | drm_gem_object_unreference_unlocked(gem_obj); |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Submits a command list to the VC4. |
| 776 | * |
| 777 | * This is what is called batchbuffer emitting on other hardware. |
| 778 | */ |
| 779 | int |
| 780 | vc4_submit_cl_ioctl(struct drm_device *dev, void *data, |
| 781 | struct drm_file *file_priv) |
| 782 | { |
| 783 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 784 | struct drm_vc4_submit_cl *args = data; |
| 785 | struct vc4_exec_info *exec; |
| 786 | int ret; |
| 787 | |
| 788 | if ((args->flags & ~VC4_SUBMIT_CL_USE_CLEAR_COLOR) != 0) { |
| 789 | DRM_ERROR("Unknown flags: 0x%02x\n", args->flags); |
| 790 | return -EINVAL; |
| 791 | } |
| 792 | |
| 793 | exec = kcalloc(1, sizeof(*exec), GFP_KERNEL); |
| 794 | if (!exec) { |
| 795 | DRM_ERROR("malloc failure on exec struct\n"); |
| 796 | return -ENOMEM; |
| 797 | } |
| 798 | |
Eric Anholt | 001bdb5 | 2016-02-05 17:41:49 -0800 | [diff] [blame^] | 799 | ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev); |
| 800 | if (ret < 0) { |
| 801 | kfree(exec); |
| 802 | return ret; |
| 803 | } |
| 804 | |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 805 | exec->args = args; |
| 806 | INIT_LIST_HEAD(&exec->unref_list); |
| 807 | |
| 808 | ret = vc4_cl_lookup_bos(dev, file_priv, exec); |
| 809 | if (ret) |
| 810 | goto fail; |
| 811 | |
| 812 | if (exec->args->bin_cl_size != 0) { |
| 813 | ret = vc4_get_bcl(dev, exec); |
| 814 | if (ret) |
| 815 | goto fail; |
| 816 | } else { |
| 817 | exec->ct0ca = 0; |
| 818 | exec->ct0ea = 0; |
| 819 | } |
| 820 | |
| 821 | ret = vc4_get_rcl(dev, exec); |
| 822 | if (ret) |
| 823 | goto fail; |
| 824 | |
| 825 | /* Clear this out of the struct we'll be putting in the queue, |
| 826 | * since it's part of our stack. |
| 827 | */ |
| 828 | exec->args = NULL; |
| 829 | |
| 830 | vc4_queue_submit(dev, exec); |
| 831 | |
| 832 | /* Return the seqno for our job. */ |
| 833 | args->seqno = vc4->emit_seqno; |
| 834 | |
| 835 | return 0; |
| 836 | |
| 837 | fail: |
| 838 | vc4_complete_exec(vc4->dev, exec); |
| 839 | |
| 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | void |
| 844 | vc4_gem_init(struct drm_device *dev) |
| 845 | { |
| 846 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 847 | |
| 848 | INIT_LIST_HEAD(&vc4->job_list); |
| 849 | INIT_LIST_HEAD(&vc4->job_done_list); |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 850 | INIT_LIST_HEAD(&vc4->seqno_cb_list); |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 851 | spin_lock_init(&vc4->job_lock); |
| 852 | |
| 853 | INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work); |
| 854 | setup_timer(&vc4->hangcheck.timer, |
| 855 | vc4_hangcheck_elapsed, |
| 856 | (unsigned long)dev); |
| 857 | |
| 858 | INIT_WORK(&vc4->job_done_work, vc4_job_done_work); |
| 859 | } |
| 860 | |
| 861 | void |
| 862 | vc4_gem_destroy(struct drm_device *dev) |
| 863 | { |
| 864 | struct vc4_dev *vc4 = to_vc4_dev(dev); |
| 865 | |
| 866 | /* Waiting for exec to finish would need to be done before |
| 867 | * unregistering V3D. |
| 868 | */ |
| 869 | WARN_ON(vc4->emit_seqno != vc4->finished_seqno); |
| 870 | |
| 871 | /* V3D should already have disabled its interrupt and cleared |
| 872 | * the overflow allocation registers. Now free the object. |
| 873 | */ |
| 874 | if (vc4->overflow_mem) { |
| 875 | drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base); |
| 876 | vc4->overflow_mem = NULL; |
| 877 | } |
| 878 | |
| 879 | vc4_bo_cache_destroy(dev); |
Eric Anholt | 2146136 | 2015-10-30 10:09:02 -0700 | [diff] [blame] | 880 | |
| 881 | if (vc4->hang_state) |
| 882 | vc4_free_hang_state(dev, vc4->hang_state); |
Eric Anholt | d5b1a78 | 2015-11-30 12:13:37 -0800 | [diff] [blame] | 883 | } |