2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
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.
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
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/>.
18 #include <linux/sync_file.h>
25 * Cmdstream submission:
28 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
29 #define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */
30 #define BO_LOCKED 0x4000
31 #define BO_PINNED 0x2000
33 static struct msm_gem_submit *submit_create(struct drm_device *dev,
34 struct msm_gpu *gpu, int nr_bos, int nr_cmds)
36 struct msm_gem_submit *submit;
37 int sz = sizeof(*submit) + (nr_bos * sizeof(submit->bos[0])) +
38 (nr_cmds * sizeof(*submit->cmd));
40 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
47 submit->pid = get_pid(task_pid(current));
48 submit->cmd = (void *)&submit->bos[nr_bos];
50 /* initially, until copy_from_user() and bo lookup succeeds: */
54 INIT_LIST_HEAD(&submit->node);
55 INIT_LIST_HEAD(&submit->bo_list);
56 ww_acquire_init(&submit->ticket, &reservation_ww_class);
61 void msm_gem_submit_free(struct msm_gem_submit *submit)
63 dma_fence_put(submit->fence);
64 list_del(&submit->node);
69 static inline unsigned long __must_check
70 copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
72 if (access_ok(VERIFY_READ, from, n))
73 return __copy_from_user_inatomic(to, from, n);
77 static int submit_lookup_objects(struct msm_gem_submit *submit,
78 struct drm_msm_gem_submit *args, struct drm_file *file)
83 spin_lock(&file->table_lock);
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 =
91 u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
93 /* make sure we don't have garbage flags, in case we hit
94 * error path before flags is initialized:
96 submit->bos[i].flags = 0;
98 ret = copy_from_user_inatomic(&submit_bo, userptr, sizeof(submit_bo));
101 spin_unlock(&file->table_lock);
102 ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
105 spin_lock(&file->table_lock);
109 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
110 DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
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;
119 /* normally use drm_gem_object_lookup(), but for bulk lookup
120 * all under single table_lock just hit object_idr directly:
122 obj = idr_find(&file->object_idr, submit_bo.handle);
124 DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
129 msm_obj = to_msm_bo(obj);
131 if (!list_empty(&msm_obj->submit_entry)) {
132 DRM_ERROR("handle %u at index %u already on submit list\n",
133 submit_bo.handle, i);
138 drm_gem_object_reference(obj);
140 submit->bos[i].obj = msm_obj;
142 list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
147 spin_unlock(&file->table_lock);
155 static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
157 struct msm_gem_object *msm_obj = submit->bos[i].obj;
159 if (submit->bos[i].flags & BO_PINNED)
160 msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
162 if (submit->bos[i].flags & BO_LOCKED)
163 ww_mutex_unlock(&msm_obj->resv->lock);
165 if (!(submit->bos[i].flags & BO_VALID))
166 submit->bos[i].iova = 0;
168 submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
171 /* This is where we make sure all the bo's are reserved and pin'd: */
172 static int submit_lock_objects(struct msm_gem_submit *submit)
174 int contended, slow_locked = -1, i, ret = 0;
177 for (i = 0; i < submit->nr_bos; i++) {
178 struct msm_gem_object *msm_obj = submit->bos[i].obj;
180 if (slow_locked == i)
185 if (!(submit->bos[i].flags & BO_LOCKED)) {
186 ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
190 submit->bos[i].flags |= BO_LOCKED;
194 ww_acquire_done(&submit->ticket);
200 submit_unlock_unpin_bo(submit, i);
203 submit_unlock_unpin_bo(submit, slow_locked);
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,
211 submit->bos[contended].flags |= BO_LOCKED;
212 slow_locked = contended;
220 static int submit_fence_sync(struct msm_gem_submit *submit)
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;
228 ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write);
236 static int submit_pin_objects(struct msm_gem_submit *submit)
240 submit->valid = true;
242 for (i = 0; i < submit->nr_bos; i++) {
243 struct msm_gem_object *msm_obj = submit->bos[i].obj;
246 /* if locking succeeded, pin bo: */
247 ret = msm_gem_get_iova_locked(&msm_obj->base,
248 submit->gpu->id, &iova);
253 submit->bos[i].flags |= BO_PINNED;
255 if (iova == submit->bos[i].iova) {
256 submit->bos[i].flags |= BO_VALID;
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;
268 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
269 struct msm_gem_object **obj, uint64_t *iova, bool *valid)
271 if (idx >= submit->nr_bos) {
272 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
273 idx, submit->nr_bos);
278 *obj = submit->bos[idx].obj;
280 *iova = submit->bos[idx].iova;
282 *valid = !!(submit->bos[idx].flags & BO_VALID);
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)
291 uint32_t i, last_offset = 0;
296 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
300 /* For now, just map the entire thing. Eventually we probably
301 * to do it page-by-page, w/ kmap() if not vmap()d..
303 ptr = msm_gem_get_vaddr_locked(&obj->base);
307 DBG("failed to map: %d", ret);
311 for (i = 0; i < nr_relocs; i++) {
312 struct drm_msm_gem_submit_reloc submit_reloc;
313 void __user *userptr =
314 u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
319 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
323 if (submit_reloc.submit_offset % 4) {
324 DRM_ERROR("non-aligned reloc offset: %u\n",
325 submit_reloc.submit_offset);
329 /* offset in dwords: */
330 off = submit_reloc.submit_offset / 4;
332 if ((off >= (obj->base.size / 4)) ||
333 (off < last_offset)) {
334 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
338 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
345 iova += submit_reloc.reloc_offset;
347 if (submit_reloc.shift < 0)
348 iova >>= -submit_reloc.shift;
350 iova <<= submit_reloc.shift;
352 ptr[off] = iova | submit_reloc.or;
357 msm_gem_put_vaddr_locked(&obj->base);
362 static void submit_cleanup(struct msm_gem_submit *submit)
366 for (i = 0; i < submit->nr_bos; i++) {
367 struct msm_gem_object *msm_obj = submit->bos[i].obj;
368 submit_unlock_unpin_bo(submit, i);
369 list_del_init(&msm_obj->submit_entry);
370 drm_gem_object_unreference(&msm_obj->base);
373 ww_acquire_fini(&submit->ticket);
376 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
377 struct drm_file *file)
379 struct msm_drm_private *priv = dev->dev_private;
380 struct drm_msm_gem_submit *args = data;
381 struct msm_file_private *ctx = file->driver_priv;
382 struct msm_gem_submit *submit;
383 struct msm_gpu *gpu = priv->gpu;
384 struct dma_fence *in_fence = NULL;
385 struct sync_file *sync_file = NULL;
386 int out_fence_fd = -1;
393 /* for now, we just have 3d pipe.. eventually this would need to
394 * be more clever to dispatch to appropriate gpu module:
396 if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
399 if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
402 ret = mutex_lock_interruptible(&dev->struct_mutex);
406 if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
407 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
408 if (out_fence_fd < 0) {
413 priv->struct_mutex_task = current;
415 submit = submit_create(dev, gpu, args->nr_bos, args->nr_cmds);
421 ret = submit_lookup_objects(submit, args, file);
425 ret = submit_lock_objects(submit);
429 if (args->flags & MSM_SUBMIT_FENCE_FD_IN) {
430 in_fence = sync_file_get_fence(args->fence_fd);
437 /* TODO if we get an array-fence due to userspace merging multiple
438 * fences, we need a way to determine if all the backing fences
439 * are from our own context..
442 if (in_fence->context != gpu->fctx->context) {
443 ret = dma_fence_wait(in_fence, true);
450 if (!(args->fence & MSM_SUBMIT_NO_IMPLICIT)) {
451 ret = submit_fence_sync(submit);
456 ret = submit_pin_objects(submit);
460 for (i = 0; i < args->nr_cmds; i++) {
461 struct drm_msm_gem_submit_cmd submit_cmd;
462 void __user *userptr =
463 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
464 struct msm_gem_object *msm_obj;
467 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
473 /* validate input from userspace: */
474 switch (submit_cmd.type) {
475 case MSM_SUBMIT_CMD_BUF:
476 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
477 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
480 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
485 ret = submit_bo(submit, submit_cmd.submit_idx,
486 &msm_obj, &iova, NULL);
490 if (submit_cmd.size % 4) {
491 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
497 if ((submit_cmd.size + submit_cmd.submit_offset) >=
498 msm_obj->base.size) {
499 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
504 submit->cmd[i].type = submit_cmd.type;
505 submit->cmd[i].size = submit_cmd.size / 4;
506 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
507 submit->cmd[i].idx = submit_cmd.submit_idx;
512 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
513 submit_cmd.nr_relocs, submit_cmd.relocs);
520 submit->fence = msm_fence_alloc(gpu->fctx);
521 if (IS_ERR(submit->fence)) {
522 ret = PTR_ERR(submit->fence);
523 submit->fence = NULL;
527 if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
528 sync_file = sync_file_create(submit->fence);
535 msm_gpu_submit(gpu, submit, ctx);
537 args->fence = submit->fence->seqno;
539 if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
540 fd_install(out_fence_fd, sync_file->file);
541 args->fence_fd = out_fence_fd;
546 dma_fence_put(in_fence);
547 submit_cleanup(submit);
549 msm_gem_submit_free(submit);
551 if (ret && (out_fence_fd >= 0))
552 put_unused_fd(out_fence_fd);
553 priv->struct_mutex_task = NULL;
554 mutex_unlock(&dev->struct_mutex);