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