1 /*
2  * Copyright (C) 2015 Etnaviv Project
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <linux/dma-fence-array.h>
18 #include <linux/reservation.h>
19 #include <linux/sync_file.h>
20 #include "etnaviv_cmdbuf.h"
21 #include "etnaviv_drv.h"
22 #include "etnaviv_gpu.h"
23 #include "etnaviv_gem.h"
24 #include "etnaviv_perfmon.h"
25 
26 /*
27  * Cmdstream submission:
28  */
29 
30 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE)
31 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */
32 #define BO_LOCKED   0x4000
33 #define BO_PINNED   0x2000
34 
35 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev,
36 		struct etnaviv_gpu *gpu, size_t nr)
37 {
38 	struct etnaviv_gem_submit *submit;
39 	size_t sz = size_vstruct(nr, sizeof(submit->bos[0]), sizeof(*submit));
40 
41 	submit = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
42 	if (submit) {
43 		submit->dev = dev;
44 		submit->gpu = gpu;
45 
46 		/* initially, until copy_from_user() and bo lookup succeeds: */
47 		submit->nr_bos = 0;
48 		submit->fence = NULL;
49 
50 		ww_acquire_init(&submit->ticket, &reservation_ww_class);
51 	}
52 
53 	return submit;
54 }
55 
56 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
57 	struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
58 	unsigned nr_bos)
59 {
60 	struct drm_etnaviv_gem_submit_bo *bo;
61 	unsigned i;
62 	int ret = 0;
63 
64 	spin_lock(&file->table_lock);
65 
66 	for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
67 		struct drm_gem_object *obj;
68 
69 		if (bo->flags & BO_INVALID_FLAGS) {
70 			DRM_ERROR("invalid flags: %x\n", bo->flags);
71 			ret = -EINVAL;
72 			goto out_unlock;
73 		}
74 
75 		submit->bos[i].flags = bo->flags;
76 
77 		/* normally use drm_gem_object_lookup(), but for bulk lookup
78 		 * all under single table_lock just hit object_idr directly:
79 		 */
80 		obj = idr_find(&file->object_idr, bo->handle);
81 		if (!obj) {
82 			DRM_ERROR("invalid handle %u at index %u\n",
83 				  bo->handle, i);
84 			ret = -EINVAL;
85 			goto out_unlock;
86 		}
87 
88 		/*
89 		 * Take a refcount on the object. The file table lock
90 		 * prevents the object_idr's refcount on this being dropped.
91 		 */
92 		drm_gem_object_get(obj);
93 
94 		submit->bos[i].obj = to_etnaviv_bo(obj);
95 	}
96 
97 out_unlock:
98 	submit->nr_bos = i;
99 	spin_unlock(&file->table_lock);
100 
101 	return ret;
102 }
103 
104 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
105 {
106 	if (submit->bos[i].flags & BO_LOCKED) {
107 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
108 
109 		ww_mutex_unlock(&etnaviv_obj->resv->lock);
110 		submit->bos[i].flags &= ~BO_LOCKED;
111 	}
112 }
113 
114 static int submit_lock_objects(struct etnaviv_gem_submit *submit)
115 {
116 	int contended, slow_locked = -1, i, ret = 0;
117 
118 retry:
119 	for (i = 0; i < submit->nr_bos; i++) {
120 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
121 
122 		if (slow_locked == i)
123 			slow_locked = -1;
124 
125 		contended = i;
126 
127 		if (!(submit->bos[i].flags & BO_LOCKED)) {
128 			ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock,
129 					&submit->ticket);
130 			if (ret == -EALREADY)
131 				DRM_ERROR("BO at index %u already on submit list\n",
132 					  i);
133 			if (ret)
134 				goto fail;
135 			submit->bos[i].flags |= BO_LOCKED;
136 		}
137 	}
138 
139 	ww_acquire_done(&submit->ticket);
140 
141 	return 0;
142 
143 fail:
144 	for (; i >= 0; i--)
145 		submit_unlock_object(submit, i);
146 
147 	if (slow_locked > 0)
148 		submit_unlock_object(submit, slow_locked);
149 
150 	if (ret == -EDEADLK) {
151 		struct etnaviv_gem_object *etnaviv_obj;
152 
153 		etnaviv_obj = submit->bos[contended].obj;
154 
155 		/* we lost out in a seqno race, lock and retry.. */
156 		ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock,
157 				&submit->ticket);
158 		if (!ret) {
159 			submit->bos[contended].flags |= BO_LOCKED;
160 			slow_locked = contended;
161 			goto retry;
162 		}
163 	}
164 
165 	return ret;
166 }
167 
168 static int submit_fence_sync(const struct etnaviv_gem_submit *submit)
169 {
170 	unsigned int context = submit->gpu->fence_context;
171 	int i, ret = 0;
172 
173 	for (i = 0; i < submit->nr_bos; i++) {
174 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
175 		bool write = submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE;
176 		bool explicit = !!(submit->flags & ETNA_SUBMIT_NO_IMPLICIT);
177 
178 		ret = etnaviv_gpu_fence_sync_obj(etnaviv_obj, context, write,
179 						 explicit);
180 		if (ret)
181 			break;
182 	}
183 
184 	return ret;
185 }
186 
187 static void submit_unpin_objects(struct etnaviv_gem_submit *submit)
188 {
189 	int i;
190 
191 	for (i = 0; i < submit->nr_bos; i++) {
192 		if (submit->bos[i].flags & BO_PINNED)
193 			etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
194 
195 		submit->bos[i].mapping = NULL;
196 		submit->bos[i].flags &= ~BO_PINNED;
197 	}
198 }
199 
200 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
201 {
202 	int i, ret = 0;
203 
204 	for (i = 0; i < submit->nr_bos; i++) {
205 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
206 		struct etnaviv_vram_mapping *mapping;
207 
208 		mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
209 						  submit->gpu);
210 		if (IS_ERR(mapping)) {
211 			ret = PTR_ERR(mapping);
212 			break;
213 		}
214 
215 		submit->bos[i].flags |= BO_PINNED;
216 		submit->bos[i].mapping = mapping;
217 	}
218 
219 	return ret;
220 }
221 
222 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
223 	struct etnaviv_gem_submit_bo **bo)
224 {
225 	if (idx >= submit->nr_bos) {
226 		DRM_ERROR("invalid buffer index: %u (out of %u)\n",
227 				idx, submit->nr_bos);
228 		return -EINVAL;
229 	}
230 
231 	*bo = &submit->bos[idx];
232 
233 	return 0;
234 }
235 
236 /* process the reloc's and patch up the cmdstream as needed: */
237 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
238 		u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
239 		u32 nr_relocs)
240 {
241 	u32 i, last_offset = 0;
242 	u32 *ptr = stream;
243 	int ret;
244 
245 	for (i = 0; i < nr_relocs; i++) {
246 		const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
247 		struct etnaviv_gem_submit_bo *bo;
248 		u32 off;
249 
250 		if (unlikely(r->flags)) {
251 			DRM_ERROR("invalid reloc flags\n");
252 			return -EINVAL;
253 		}
254 
255 		if (r->submit_offset % 4) {
256 			DRM_ERROR("non-aligned reloc offset: %u\n",
257 				  r->submit_offset);
258 			return -EINVAL;
259 		}
260 
261 		/* offset in dwords: */
262 		off = r->submit_offset / 4;
263 
264 		if ((off >= size ) ||
265 				(off < last_offset)) {
266 			DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
267 			return -EINVAL;
268 		}
269 
270 		ret = submit_bo(submit, r->reloc_idx, &bo);
271 		if (ret)
272 			return ret;
273 
274 		if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
275 			DRM_ERROR("relocation %u outside object\n", i);
276 			return -EINVAL;
277 		}
278 
279 		ptr[off] = bo->mapping->iova + r->reloc_offset;
280 
281 		last_offset = off;
282 	}
283 
284 	return 0;
285 }
286 
287 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
288 		struct etnaviv_cmdbuf *cmdbuf,
289 		const struct drm_etnaviv_gem_submit_pmr *pmrs,
290 		u32 nr_pms)
291 {
292 	u32 i;
293 
294 	for (i = 0; i < nr_pms; i++) {
295 		const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i;
296 		struct etnaviv_gem_submit_bo *bo;
297 		int ret;
298 
299 		ret = submit_bo(submit, r->read_idx, &bo);
300 		if (ret)
301 			return ret;
302 
303 		/* at offset 0 a sequence number gets stored used for userspace sync */
304 		if (r->read_offset == 0) {
305 			DRM_ERROR("perfmon request: offset is 0");
306 			return -EINVAL;
307 		}
308 
309 		if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
310 			DRM_ERROR("perfmon request: offset %u outside object", i);
311 			return -EINVAL;
312 		}
313 
314 		if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
315 			DRM_ERROR("perfmon request: flags are not valid");
316 			return -EINVAL;
317 		}
318 
319 		if (etnaviv_pm_req_validate(r, cmdbuf->exec_state)) {
320 			DRM_ERROR("perfmon request: domain or signal not valid");
321 			return -EINVAL;
322 		}
323 
324 		cmdbuf->pmrs[i].flags = r->flags;
325 		cmdbuf->pmrs[i].domain = r->domain;
326 		cmdbuf->pmrs[i].signal = r->signal;
327 		cmdbuf->pmrs[i].sequence = r->sequence;
328 		cmdbuf->pmrs[i].offset = r->read_offset;
329 		cmdbuf->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base);
330 	}
331 
332 	return 0;
333 }
334 
335 static void submit_cleanup(struct etnaviv_gem_submit *submit)
336 {
337 	unsigned i;
338 
339 	for (i = 0; i < submit->nr_bos; i++) {
340 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
341 
342 		submit_unlock_object(submit, i);
343 		drm_gem_object_put_unlocked(&etnaviv_obj->base);
344 	}
345 
346 	ww_acquire_fini(&submit->ticket);
347 	if (submit->fence)
348 		dma_fence_put(submit->fence);
349 	kfree(submit);
350 }
351 
352 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
353 		struct drm_file *file)
354 {
355 	struct etnaviv_drm_private *priv = dev->dev_private;
356 	struct drm_etnaviv_gem_submit *args = data;
357 	struct drm_etnaviv_gem_submit_reloc *relocs;
358 	struct drm_etnaviv_gem_submit_pmr *pmrs;
359 	struct drm_etnaviv_gem_submit_bo *bos;
360 	struct etnaviv_gem_submit *submit;
361 	struct etnaviv_cmdbuf *cmdbuf;
362 	struct etnaviv_gpu *gpu;
363 	struct dma_fence *in_fence = NULL;
364 	struct sync_file *sync_file = NULL;
365 	int out_fence_fd = -1;
366 	void *stream;
367 	int ret;
368 
369 	if (args->pipe >= ETNA_MAX_PIPES)
370 		return -EINVAL;
371 
372 	gpu = priv->gpu[args->pipe];
373 	if (!gpu)
374 		return -ENXIO;
375 
376 	if (args->stream_size % 4) {
377 		DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
378 			  args->stream_size);
379 		return -EINVAL;
380 	}
381 
382 	if (args->exec_state != ETNA_PIPE_3D &&
383 	    args->exec_state != ETNA_PIPE_2D &&
384 	    args->exec_state != ETNA_PIPE_VG) {
385 		DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
386 		return -EINVAL;
387 	}
388 
389 	if (args->flags & ~ETNA_SUBMIT_FLAGS) {
390 		DRM_ERROR("invalid flags: 0x%x\n", args->flags);
391 		return -EINVAL;
392 	}
393 
394 	/*
395 	 * Copy the command submission and bo array to kernel space in
396 	 * one go, and do this outside of any locks.
397 	 */
398 	bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
399 	relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
400 	pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
401 	stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
402 	cmdbuf = etnaviv_cmdbuf_new(gpu->cmdbuf_suballoc,
403 				    ALIGN(args->stream_size, 8) + 8,
404 				    args->nr_bos, args->nr_pmrs);
405 	if (!bos || !relocs || !pmrs || !stream || !cmdbuf) {
406 		ret = -ENOMEM;
407 		goto err_submit_cmds;
408 	}
409 
410 	cmdbuf->exec_state = args->exec_state;
411 	cmdbuf->ctx = file->driver_priv;
412 
413 	ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
414 			     args->nr_bos * sizeof(*bos));
415 	if (ret) {
416 		ret = -EFAULT;
417 		goto err_submit_cmds;
418 	}
419 
420 	ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
421 			     args->nr_relocs * sizeof(*relocs));
422 	if (ret) {
423 		ret = -EFAULT;
424 		goto err_submit_cmds;
425 	}
426 
427 	ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
428 			     args->nr_pmrs * sizeof(*pmrs));
429 	if (ret) {
430 		ret = -EFAULT;
431 		goto err_submit_cmds;
432 	}
433 	cmdbuf->nr_pmrs = args->nr_pmrs;
434 
435 	ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
436 			     args->stream_size);
437 	if (ret) {
438 		ret = -EFAULT;
439 		goto err_submit_cmds;
440 	}
441 
442 	if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
443 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
444 		if (out_fence_fd < 0) {
445 			ret = out_fence_fd;
446 			goto err_submit_cmds;
447 		}
448 	}
449 
450 	submit = submit_create(dev, gpu, args->nr_bos);
451 	if (!submit) {
452 		ret = -ENOMEM;
453 		goto err_submit_cmds;
454 	}
455 
456 	submit->flags = args->flags;
457 
458 	ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
459 	if (ret)
460 		goto err_submit_objects;
461 
462 	ret = submit_lock_objects(submit);
463 	if (ret)
464 		goto err_submit_objects;
465 
466 	if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
467 				      relocs, args->nr_relocs)) {
468 		ret = -EINVAL;
469 		goto err_submit_objects;
470 	}
471 
472 	if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
473 		in_fence = sync_file_get_fence(args->fence_fd);
474 		if (!in_fence) {
475 			ret = -EINVAL;
476 			goto err_submit_objects;
477 		}
478 
479 		/*
480 		 * Wait if the fence is from a foreign context, or if the fence
481 		 * array contains any fence from a foreign context.
482 		 */
483 		if (!dma_fence_match_context(in_fence, gpu->fence_context)) {
484 			ret = dma_fence_wait(in_fence, true);
485 			if (ret)
486 				goto err_submit_objects;
487 		}
488 	}
489 
490 	ret = submit_fence_sync(submit);
491 	if (ret)
492 		goto err_submit_objects;
493 
494 	ret = submit_pin_objects(submit);
495 	if (ret)
496 		goto out;
497 
498 	ret = submit_reloc(submit, stream, args->stream_size / 4,
499 			   relocs, args->nr_relocs);
500 	if (ret)
501 		goto out;
502 
503 	ret = submit_perfmon_validate(submit, cmdbuf, pmrs, args->nr_pmrs);
504 	if (ret)
505 		goto out;
506 
507 	memcpy(cmdbuf->vaddr, stream, args->stream_size);
508 	cmdbuf->user_size = ALIGN(args->stream_size, 8);
509 
510 	ret = etnaviv_gpu_submit(gpu, submit, cmdbuf);
511 	if (ret)
512 		goto out;
513 
514 	cmdbuf = NULL;
515 
516 	if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
517 		/*
518 		 * This can be improved: ideally we want to allocate the sync
519 		 * file before kicking off the GPU job and just attach the
520 		 * fence to the sync file here, eliminating the ENOMEM
521 		 * possibility at this stage.
522 		 */
523 		sync_file = sync_file_create(submit->fence);
524 		if (!sync_file) {
525 			ret = -ENOMEM;
526 			goto out;
527 		}
528 		fd_install(out_fence_fd, sync_file->file);
529 	}
530 
531 	args->fence_fd = out_fence_fd;
532 	args->fence = submit->fence->seqno;
533 
534 out:
535 	submit_unpin_objects(submit);
536 
537 	/*
538 	 * If we're returning -EAGAIN, it may be due to the userptr code
539 	 * wanting to run its workqueue outside of any locks. Flush our
540 	 * workqueue to ensure that it is run in a timely manner.
541 	 */
542 	if (ret == -EAGAIN)
543 		flush_workqueue(priv->wq);
544 
545 err_submit_objects:
546 	if (in_fence)
547 		dma_fence_put(in_fence);
548 	submit_cleanup(submit);
549 
550 err_submit_cmds:
551 	if (ret && (out_fence_fd >= 0))
552 		put_unused_fd(out_fence_fd);
553 	/* if we still own the cmdbuf */
554 	if (cmdbuf)
555 		etnaviv_cmdbuf_free(cmdbuf);
556 	if (stream)
557 		kvfree(stream);
558 	if (bos)
559 		kvfree(bos);
560 	if (relocs)
561 		kvfree(relocs);
562 	if (pmrs)
563 		kvfree(pmrs);
564 
565 	return ret;
566 }
567