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