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/>.
23 * Cmdstream submission:
26 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
27 #define BO_VALID 0x8000
28 #define BO_LOCKED 0x4000
29 #define BO_PINNED 0x2000
31 static inline void __user *to_user_ptr(u64 address)
33 return (void __user *)(uintptr_t)address;
36 static struct msm_gem_submit *submit_create(struct drm_device *dev,
37 struct msm_gpu *gpu, int nr)
39 struct msm_gem_submit *submit;
40 int sz = sizeof(*submit) + (nr * sizeof(submit->bos[0]));
42 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
47 /* initially, until copy_from_user() and bo lookup succeeds: */
51 INIT_LIST_HEAD(&submit->bo_list);
52 ww_acquire_init(&submit->ticket, &reservation_ww_class);
58 static inline unsigned long __must_check
59 copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
61 if (access_ok(VERIFY_READ, from, n))
62 return __copy_from_user_inatomic(to, from, n);
66 static int submit_lookup_objects(struct msm_gem_submit *submit,
67 struct drm_msm_gem_submit *args, struct drm_file *file)
72 spin_lock(&file->table_lock);
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 =
80 to_user_ptr(args->bos + (i * sizeof(submit_bo)));
82 ret = copy_from_user_inatomic(&submit_bo, userptr, sizeof(submit_bo));
85 spin_unlock(&file->table_lock);
86 ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
89 spin_lock(&file->table_lock);
93 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
94 DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
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;
103 /* normally use drm_gem_object_lookup(), but for bulk lookup
104 * all under single table_lock just hit object_idr directly:
106 obj = idr_find(&file->object_idr, submit_bo.handle);
108 DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
113 msm_obj = to_msm_bo(obj);
115 if (!list_empty(&msm_obj->submit_entry)) {
116 DRM_ERROR("handle %u at index %u already on submit list\n",
117 submit_bo.handle, i);
122 drm_gem_object_reference(obj);
124 submit->bos[i].obj = msm_obj;
126 list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
131 spin_unlock(&file->table_lock);
139 static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
141 struct msm_gem_object *msm_obj = submit->bos[i].obj;
143 if (submit->bos[i].flags & BO_PINNED)
144 msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
146 if (submit->bos[i].flags & BO_LOCKED)
147 ww_mutex_unlock(&msm_obj->resv->lock);
149 if (!(submit->bos[i].flags & BO_VALID))
150 submit->bos[i].iova = 0;
152 submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
155 /* This is where we make sure all the bo's are reserved and pin'd: */
156 static int submit_validate_objects(struct msm_gem_submit *submit)
158 int contended, slow_locked = -1, i, ret = 0;
161 submit->valid = true;
163 for (i = 0; i < submit->nr_bos; i++) {
164 struct msm_gem_object *msm_obj = submit->bos[i].obj;
167 if (slow_locked == i)
172 if (!(submit->bos[i].flags & BO_LOCKED)) {
173 ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
177 submit->bos[i].flags |= BO_LOCKED;
181 /* if locking succeeded, pin bo: */
182 ret = msm_gem_get_iova_locked(&msm_obj->base,
183 submit->gpu->id, &iova);
185 /* this would break the logic in the fail path.. there is no
186 * reason for this to happen, but just to be on the safe side
187 * let's notice if this starts happening in the future:
189 WARN_ON(ret == -EDEADLK);
194 submit->bos[i].flags |= BO_PINNED;
196 if (iova == submit->bos[i].iova) {
197 submit->bos[i].flags |= BO_VALID;
199 submit->bos[i].iova = iova;
200 submit->bos[i].flags &= ~BO_VALID;
201 submit->valid = false;
205 ww_acquire_done(&submit->ticket);
211 submit_unlock_unpin_bo(submit, i);
214 submit_unlock_unpin_bo(submit, slow_locked);
216 if (ret == -EDEADLK) {
217 struct msm_gem_object *msm_obj = submit->bos[contended].obj;
218 /* we lost out in a seqno race, lock and retry.. */
219 ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
222 submit->bos[contended].flags |= BO_LOCKED;
223 slow_locked = contended;
231 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
232 struct msm_gem_object **obj, uint32_t *iova, bool *valid)
234 if (idx >= submit->nr_bos) {
235 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
236 idx, submit->nr_bos);
241 *obj = submit->bos[idx].obj;
243 *iova = submit->bos[idx].iova;
245 *valid = !!(submit->bos[idx].flags & BO_VALID);
250 /* process the reloc's and patch up the cmdstream as needed: */
251 static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
252 uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
254 uint32_t i, last_offset = 0;
259 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
263 /* For now, just map the entire thing. Eventually we probably
264 * to do it page-by-page, w/ kmap() if not vmap()d..
266 ptr = msm_gem_vaddr_locked(&obj->base);
270 DBG("failed to map: %d", ret);
274 for (i = 0; i < nr_relocs; i++) {
275 struct drm_msm_gem_submit_reloc submit_reloc;
276 void __user *userptr =
277 to_user_ptr(relocs + (i * sizeof(submit_reloc)));
281 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
285 if (submit_reloc.submit_offset % 4) {
286 DRM_ERROR("non-aligned reloc offset: %u\n",
287 submit_reloc.submit_offset);
291 /* offset in dwords: */
292 off = submit_reloc.submit_offset / 4;
294 if ((off >= (obj->base.size / 4)) ||
295 (off < last_offset)) {
296 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
300 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
307 iova += submit_reloc.reloc_offset;
309 if (submit_reloc.shift < 0)
310 iova >>= -submit_reloc.shift;
312 iova <<= submit_reloc.shift;
314 ptr[off] = iova | submit_reloc.or;
322 static void submit_cleanup(struct msm_gem_submit *submit, bool fail)
326 for (i = 0; i < submit->nr_bos; i++) {
327 struct msm_gem_object *msm_obj = submit->bos[i].obj;
328 submit_unlock_unpin_bo(submit, i);
329 list_del_init(&msm_obj->submit_entry);
330 drm_gem_object_unreference(&msm_obj->base);
333 ww_acquire_fini(&submit->ticket);
336 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
337 struct drm_file *file)
339 struct msm_drm_private *priv = dev->dev_private;
340 struct drm_msm_gem_submit *args = data;
341 struct msm_file_private *ctx = file->driver_priv;
342 struct msm_gem_submit *submit;
347 /* for now, we just have 3d pipe.. eventually this would need to
348 * be more clever to dispatch to appropriate gpu module:
350 if (args->pipe != MSM_PIPE_3D0)
355 if (args->nr_cmds > MAX_CMDS)
358 mutex_lock(&dev->struct_mutex);
360 submit = submit_create(dev, gpu, args->nr_bos);
366 ret = submit_lookup_objects(submit, args, file);
370 ret = submit_validate_objects(submit);
374 for (i = 0; i < args->nr_cmds; i++) {
375 struct drm_msm_gem_submit_cmd submit_cmd;
376 void __user *userptr =
377 to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
378 struct msm_gem_object *msm_obj;
381 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
387 /* validate input from userspace: */
388 switch (submit_cmd.type) {
389 case MSM_SUBMIT_CMD_BUF:
390 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
391 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
394 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
399 ret = submit_bo(submit, submit_cmd.submit_idx,
400 &msm_obj, &iova, NULL);
404 if (submit_cmd.size % 4) {
405 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
411 if ((submit_cmd.size + submit_cmd.submit_offset) >=
412 msm_obj->base.size) {
413 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
418 submit->cmd[i].type = submit_cmd.type;
419 submit->cmd[i].size = submit_cmd.size / 4;
420 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
421 submit->cmd[i].idx = submit_cmd.submit_idx;
426 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
427 submit_cmd.nr_relocs, submit_cmd.relocs);
434 ret = msm_gpu_submit(gpu, submit, ctx);
436 args->fence = submit->fence;
440 submit_cleanup(submit, !!ret);
441 mutex_unlock(&dev->struct_mutex);