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