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_drv.h" |
| 19 | #include "msm_gpu.h" |
| 20 | #include "msm_gem.h" |
| 21 | |
| 22 | /* |
| 23 | * Cmdstream submission: |
| 24 | */ |
| 25 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 26 | /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */ |
Rob Clark | 340faef | 2016-03-14 13:56:37 -0400 | [diff] [blame^] | 27 | #define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */ |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 28 | #define BO_LOCKED 0x4000 |
| 29 | #define BO_PINNED 0x2000 |
| 30 | |
| 31 | static inline void __user *to_user_ptr(u64 address) |
| 32 | { |
| 33 | return (void __user *)(uintptr_t)address; |
| 34 | } |
| 35 | |
| 36 | static struct msm_gem_submit *submit_create(struct drm_device *dev, |
| 37 | struct msm_gpu *gpu, int nr) |
| 38 | { |
| 39 | struct msm_gem_submit *submit; |
| 40 | int sz = sizeof(*submit) + (nr * sizeof(submit->bos[0])); |
| 41 | |
| 42 | submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
| 43 | if (submit) { |
| 44 | submit->dev = dev; |
| 45 | submit->gpu = gpu; |
| 46 | |
| 47 | /* initially, until copy_from_user() and bo lookup succeeds: */ |
| 48 | submit->nr_bos = 0; |
| 49 | submit->nr_cmds = 0; |
| 50 | |
| 51 | INIT_LIST_HEAD(&submit->bo_list); |
| 52 | ww_acquire_init(&submit->ticket, &reservation_ww_class); |
| 53 | } |
| 54 | |
| 55 | return submit; |
| 56 | } |
| 57 | |
| 58 | static int submit_lookup_objects(struct msm_gem_submit *submit, |
| 59 | struct drm_msm_gem_submit *args, struct drm_file *file) |
| 60 | { |
| 61 | unsigned i; |
| 62 | int ret = 0; |
| 63 | |
| 64 | spin_lock(&file->table_lock); |
| 65 | |
| 66 | for (i = 0; i < args->nr_bos; i++) { |
| 67 | struct drm_msm_gem_submit_bo submit_bo; |
| 68 | struct drm_gem_object *obj; |
| 69 | struct msm_gem_object *msm_obj; |
| 70 | void __user *userptr = |
| 71 | to_user_ptr(args->bos + (i * sizeof(submit_bo))); |
| 72 | |
| 73 | ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo)); |
| 74 | if (ret) { |
| 75 | ret = -EFAULT; |
| 76 | goto out_unlock; |
| 77 | } |
| 78 | |
Rob Clark | 93ddb0d | 2014-03-03 09:42:33 -0500 | [diff] [blame] | 79 | if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 80 | DRM_ERROR("invalid flags: %x\n", submit_bo.flags); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 81 | ret = -EINVAL; |
| 82 | goto out_unlock; |
| 83 | } |
| 84 | |
| 85 | submit->bos[i].flags = submit_bo.flags; |
| 86 | /* in validate_objects() we figure out if this is true: */ |
| 87 | submit->bos[i].iova = submit_bo.presumed; |
| 88 | |
| 89 | /* normally use drm_gem_object_lookup(), but for bulk lookup |
| 90 | * all under single table_lock just hit object_idr directly: |
| 91 | */ |
| 92 | obj = idr_find(&file->object_idr, submit_bo.handle); |
| 93 | if (!obj) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 94 | DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 95 | ret = -EINVAL; |
| 96 | goto out_unlock; |
| 97 | } |
| 98 | |
| 99 | msm_obj = to_msm_bo(obj); |
| 100 | |
| 101 | if (!list_empty(&msm_obj->submit_entry)) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 102 | DRM_ERROR("handle %u at index %u already on submit list\n", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 103 | submit_bo.handle, i); |
| 104 | ret = -EINVAL; |
| 105 | goto out_unlock; |
| 106 | } |
| 107 | |
| 108 | drm_gem_object_reference(obj); |
| 109 | |
| 110 | submit->bos[i].obj = msm_obj; |
| 111 | |
| 112 | list_add_tail(&msm_obj->submit_entry, &submit->bo_list); |
| 113 | } |
| 114 | |
| 115 | out_unlock: |
| 116 | submit->nr_bos = i; |
| 117 | spin_unlock(&file->table_lock); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i) |
| 123 | { |
| 124 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
| 125 | |
| 126 | if (submit->bos[i].flags & BO_PINNED) |
| 127 | msm_gem_put_iova(&msm_obj->base, submit->gpu->id); |
| 128 | |
| 129 | if (submit->bos[i].flags & BO_LOCKED) |
| 130 | ww_mutex_unlock(&msm_obj->resv->lock); |
| 131 | |
| 132 | if (!(submit->bos[i].flags & BO_VALID)) |
| 133 | submit->bos[i].iova = 0; |
| 134 | |
| 135 | submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED); |
| 136 | } |
| 137 | |
| 138 | /* This is where we make sure all the bo's are reserved and pin'd: */ |
Rob Clark | 340faef | 2016-03-14 13:56:37 -0400 | [diff] [blame^] | 139 | static int submit_lock_objects(struct msm_gem_submit *submit) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 140 | { |
| 141 | int contended, slow_locked = -1, i, ret = 0; |
| 142 | |
| 143 | retry: |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 144 | for (i = 0; i < submit->nr_bos; i++) { |
| 145 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 146 | |
| 147 | if (slow_locked == i) |
| 148 | slow_locked = -1; |
| 149 | |
| 150 | contended = i; |
| 151 | |
| 152 | if (!(submit->bos[i].flags & BO_LOCKED)) { |
| 153 | ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock, |
| 154 | &submit->ticket); |
| 155 | if (ret) |
| 156 | goto fail; |
| 157 | submit->bos[i].flags |= BO_LOCKED; |
| 158 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | ww_acquire_done(&submit->ticket); |
| 162 | |
| 163 | return 0; |
| 164 | |
| 165 | fail: |
| 166 | for (; i >= 0; i--) |
| 167 | submit_unlock_unpin_bo(submit, i); |
| 168 | |
| 169 | if (slow_locked > 0) |
| 170 | submit_unlock_unpin_bo(submit, slow_locked); |
| 171 | |
| 172 | if (ret == -EDEADLK) { |
| 173 | struct msm_gem_object *msm_obj = submit->bos[contended].obj; |
| 174 | /* we lost out in a seqno race, lock and retry.. */ |
| 175 | ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock, |
| 176 | &submit->ticket); |
| 177 | if (!ret) { |
| 178 | submit->bos[contended].flags |= BO_LOCKED; |
| 179 | slow_locked = contended; |
| 180 | goto retry; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return ret; |
| 185 | } |
| 186 | |
Rob Clark | 340faef | 2016-03-14 13:56:37 -0400 | [diff] [blame^] | 187 | static int submit_pin_objects(struct msm_gem_submit *submit) |
| 188 | { |
| 189 | int i, ret = 0; |
| 190 | |
| 191 | submit->valid = true; |
| 192 | |
| 193 | for (i = 0; i < submit->nr_bos; i++) { |
| 194 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
| 195 | uint32_t iova; |
| 196 | |
| 197 | /* if locking succeeded, pin bo: */ |
| 198 | ret = msm_gem_get_iova_locked(&msm_obj->base, |
| 199 | submit->gpu->id, &iova); |
| 200 | |
| 201 | if (ret) |
| 202 | break; |
| 203 | |
| 204 | submit->bos[i].flags |= BO_PINNED; |
| 205 | |
| 206 | if (iova == submit->bos[i].iova) { |
| 207 | submit->bos[i].flags |= BO_VALID; |
| 208 | } else { |
| 209 | submit->bos[i].iova = iova; |
| 210 | /* iova changed, so address in cmdstream is not valid: */ |
| 211 | submit->bos[i].flags &= ~BO_VALID; |
| 212 | submit->valid = false; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 219 | static int submit_bo(struct msm_gem_submit *submit, uint32_t idx, |
| 220 | struct msm_gem_object **obj, uint32_t *iova, bool *valid) |
| 221 | { |
| 222 | if (idx >= submit->nr_bos) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 223 | DRM_ERROR("invalid buffer index: %u (out of %u)\n", |
| 224 | idx, submit->nr_bos); |
| 225 | return -EINVAL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (obj) |
| 229 | *obj = submit->bos[idx].obj; |
| 230 | if (iova) |
| 231 | *iova = submit->bos[idx].iova; |
| 232 | if (valid) |
| 233 | *valid = !!(submit->bos[idx].flags & BO_VALID); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* process the reloc's and patch up the cmdstream as needed: */ |
| 239 | static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj, |
| 240 | uint32_t offset, uint32_t nr_relocs, uint64_t relocs) |
| 241 | { |
| 242 | uint32_t i, last_offset = 0; |
| 243 | uint32_t *ptr; |
| 244 | int ret; |
| 245 | |
| 246 | if (offset % 4) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 247 | DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | /* For now, just map the entire thing. Eventually we probably |
| 252 | * to do it page-by-page, w/ kmap() if not vmap()d.. |
| 253 | */ |
Rob Clark | c2703b1 | 2014-02-06 19:19:20 -0500 | [diff] [blame] | 254 | ptr = msm_gem_vaddr_locked(&obj->base); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 255 | |
| 256 | if (IS_ERR(ptr)) { |
| 257 | ret = PTR_ERR(ptr); |
| 258 | DBG("failed to map: %d", ret); |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | for (i = 0; i < nr_relocs; i++) { |
| 263 | struct drm_msm_gem_submit_reloc submit_reloc; |
| 264 | void __user *userptr = |
| 265 | to_user_ptr(relocs + (i * sizeof(submit_reloc))); |
| 266 | uint32_t iova, off; |
| 267 | bool valid; |
| 268 | |
| 269 | ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc)); |
| 270 | if (ret) |
| 271 | return -EFAULT; |
| 272 | |
| 273 | if (submit_reloc.submit_offset % 4) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 274 | DRM_ERROR("non-aligned reloc offset: %u\n", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 275 | submit_reloc.submit_offset); |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
| 279 | /* offset in dwords: */ |
| 280 | off = submit_reloc.submit_offset / 4; |
| 281 | |
| 282 | if ((off >= (obj->base.size / 4)) || |
| 283 | (off < last_offset)) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 284 | DRM_ERROR("invalid offset %u at reloc %u\n", off, i); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | |
| 292 | if (valid) |
| 293 | continue; |
| 294 | |
| 295 | iova += submit_reloc.reloc_offset; |
| 296 | |
| 297 | if (submit_reloc.shift < 0) |
| 298 | iova >>= -submit_reloc.shift; |
| 299 | else |
| 300 | iova <<= submit_reloc.shift; |
| 301 | |
| 302 | ptr[off] = iova | submit_reloc.or; |
| 303 | |
| 304 | last_offset = off; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static void submit_cleanup(struct msm_gem_submit *submit, bool fail) |
| 311 | { |
| 312 | unsigned i; |
| 313 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 314 | for (i = 0; i < submit->nr_bos; i++) { |
| 315 | struct msm_gem_object *msm_obj = submit->bos[i].obj; |
| 316 | submit_unlock_unpin_bo(submit, i); |
| 317 | list_del_init(&msm_obj->submit_entry); |
| 318 | drm_gem_object_unreference(&msm_obj->base); |
| 319 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 320 | |
| 321 | ww_acquire_fini(&submit->ticket); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | int msm_ioctl_gem_submit(struct drm_device *dev, void *data, |
| 325 | struct drm_file *file) |
| 326 | { |
| 327 | struct msm_drm_private *priv = dev->dev_private; |
| 328 | struct drm_msm_gem_submit *args = data; |
| 329 | struct msm_file_private *ctx = file->driver_priv; |
| 330 | struct msm_gem_submit *submit; |
Rob Clark | c01a958 | 2016-02-03 13:12:31 -0500 | [diff] [blame] | 331 | struct msm_gpu *gpu = priv->gpu; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 332 | unsigned i; |
| 333 | int ret; |
| 334 | |
Rob Clark | c01a958 | 2016-02-03 13:12:31 -0500 | [diff] [blame] | 335 | if (!gpu) |
| 336 | return -ENXIO; |
| 337 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 338 | /* for now, we just have 3d pipe.. eventually this would need to |
| 339 | * be more clever to dispatch to appropriate gpu module: |
| 340 | */ |
| 341 | if (args->pipe != MSM_PIPE_3D0) |
| 342 | return -EINVAL; |
| 343 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 344 | if (args->nr_cmds > MAX_CMDS) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | submit = submit_create(dev, gpu, args->nr_bos); |
Rob Clark | 687f084 | 2016-02-03 13:24:35 -0500 | [diff] [blame] | 348 | if (!submit) |
| 349 | return -ENOMEM; |
| 350 | |
| 351 | mutex_lock(&dev->struct_mutex); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 352 | |
| 353 | ret = submit_lookup_objects(submit, args, file); |
| 354 | if (ret) |
| 355 | goto out; |
| 356 | |
Rob Clark | 340faef | 2016-03-14 13:56:37 -0400 | [diff] [blame^] | 357 | ret = submit_lock_objects(submit); |
| 358 | if (ret) |
| 359 | goto out; |
| 360 | |
| 361 | ret = submit_pin_objects(submit); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 362 | if (ret) |
| 363 | goto out; |
| 364 | |
| 365 | for (i = 0; i < args->nr_cmds; i++) { |
| 366 | struct drm_msm_gem_submit_cmd submit_cmd; |
| 367 | void __user *userptr = |
| 368 | to_user_ptr(args->cmds + (i * sizeof(submit_cmd))); |
| 369 | struct msm_gem_object *msm_obj; |
| 370 | uint32_t iova; |
| 371 | |
| 372 | ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd)); |
| 373 | if (ret) { |
| 374 | ret = -EFAULT; |
| 375 | goto out; |
| 376 | } |
| 377 | |
Rob Clark | 93ddb0d | 2014-03-03 09:42:33 -0500 | [diff] [blame] | 378 | /* validate input from userspace: */ |
| 379 | switch (submit_cmd.type) { |
| 380 | case MSM_SUBMIT_CMD_BUF: |
| 381 | case MSM_SUBMIT_CMD_IB_TARGET_BUF: |
| 382 | case MSM_SUBMIT_CMD_CTX_RESTORE_BUF: |
| 383 | break; |
| 384 | default: |
| 385 | DRM_ERROR("invalid type: %08x\n", submit_cmd.type); |
| 386 | ret = -EINVAL; |
| 387 | goto out; |
| 388 | } |
| 389 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 390 | ret = submit_bo(submit, submit_cmd.submit_idx, |
| 391 | &msm_obj, &iova, NULL); |
| 392 | if (ret) |
| 393 | goto out; |
| 394 | |
| 395 | if (submit_cmd.size % 4) { |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 396 | DRM_ERROR("non-aligned cmdstream buffer size: %u\n", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 397 | submit_cmd.size); |
| 398 | ret = -EINVAL; |
| 399 | goto out; |
| 400 | } |
| 401 | |
Rob Clark | 1987253 | 2013-09-06 15:36:40 -0400 | [diff] [blame] | 402 | if ((submit_cmd.size + submit_cmd.submit_offset) >= |
| 403 | msm_obj->base.size) { |
| 404 | DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 405 | ret = -EINVAL; |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | submit->cmd[i].type = submit_cmd.type; |
| 410 | submit->cmd[i].size = submit_cmd.size / 4; |
| 411 | submit->cmd[i].iova = iova + submit_cmd.submit_offset; |
Rob Clark | a7d3c95 | 2014-05-30 14:47:38 -0400 | [diff] [blame] | 412 | submit->cmd[i].idx = submit_cmd.submit_idx; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 413 | |
| 414 | if (submit->valid) |
| 415 | continue; |
| 416 | |
| 417 | ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset, |
| 418 | submit_cmd.nr_relocs, submit_cmd.relocs); |
| 419 | if (ret) |
| 420 | goto out; |
| 421 | } |
| 422 | |
| 423 | submit->nr_cmds = i; |
| 424 | |
| 425 | ret = msm_gpu_submit(gpu, submit, ctx); |
| 426 | |
| 427 | args->fence = submit->fence; |
| 428 | |
| 429 | out: |
Rob Clark | 687f084 | 2016-02-03 13:24:35 -0500 | [diff] [blame] | 430 | submit_cleanup(submit, !!ret); |
Rob Clark | c2703b1 | 2014-02-06 19:19:20 -0500 | [diff] [blame] | 431 | mutex_unlock(&dev->struct_mutex); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 432 | return ret; |
| 433 | } |