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