1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Etnaviv Project
4  */
5 
6 #include <drm/drm_file.h>
7 #include <linux/dma-fence-array.h>
8 #include <linux/file.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/dma-resv.h>
11 #include <linux/sync_file.h>
12 #include <linux/uaccess.h>
13 #include <linux/vmalloc.h>
14 
15 #include "etnaviv_cmdbuf.h"
16 #include "etnaviv_drv.h"
17 #include "etnaviv_gpu.h"
18 #include "etnaviv_gem.h"
19 #include "etnaviv_perfmon.h"
20 #include "etnaviv_sched.h"
21 
22 /*
23  * Cmdstream submission:
24  */
25 
26 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE)
27 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */
28 #define BO_LOCKED   0x4000
29 #define BO_PINNED   0x2000
30 
31 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev,
32 		struct etnaviv_gpu *gpu, size_t nr_bos, size_t nr_pmrs)
33 {
34 	struct etnaviv_gem_submit *submit;
35 	size_t sz = size_vstruct(nr_bos, sizeof(submit->bos[0]), sizeof(*submit));
36 
37 	submit = kzalloc(sz, GFP_KERNEL);
38 	if (!submit)
39 		return NULL;
40 
41 	submit->pmrs = kcalloc(nr_pmrs, sizeof(struct etnaviv_perfmon_request),
42 			       GFP_KERNEL);
43 	if (!submit->pmrs) {
44 		kfree(submit);
45 		return NULL;
46 	}
47 	submit->nr_pmrs = nr_pmrs;
48 
49 	submit->gpu = gpu;
50 	kref_init(&submit->refcount);
51 
52 	return submit;
53 }
54 
55 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
56 	struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
57 	unsigned nr_bos)
58 {
59 	struct drm_etnaviv_gem_submit_bo *bo;
60 	unsigned i;
61 	int ret = 0;
62 
63 	spin_lock(&file->table_lock);
64 
65 	for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
66 		struct drm_gem_object *obj;
67 
68 		if (bo->flags & BO_INVALID_FLAGS) {
69 			DRM_ERROR("invalid flags: %x\n", bo->flags);
70 			ret = -EINVAL;
71 			goto out_unlock;
72 		}
73 
74 		submit->bos[i].flags = bo->flags;
75 		if (submit->flags & ETNA_SUBMIT_SOFTPIN) {
76 			if (bo->presumed < ETNAVIV_SOFTPIN_START_ADDRESS) {
77 				DRM_ERROR("invalid softpin address\n");
78 				ret = -EINVAL;
79 				goto out_unlock;
80 			}
81 			submit->bos[i].va = bo->presumed;
82 		}
83 
84 		/* normally use drm_gem_object_lookup(), but for bulk lookup
85 		 * all under single table_lock just hit object_idr directly:
86 		 */
87 		obj = idr_find(&file->object_idr, bo->handle);
88 		if (!obj) {
89 			DRM_ERROR("invalid handle %u at index %u\n",
90 				  bo->handle, i);
91 			ret = -EINVAL;
92 			goto out_unlock;
93 		}
94 
95 		/*
96 		 * Take a refcount on the object. The file table lock
97 		 * prevents the object_idr's refcount on this being dropped.
98 		 */
99 		drm_gem_object_get(obj);
100 
101 		submit->bos[i].obj = to_etnaviv_bo(obj);
102 	}
103 
104 out_unlock:
105 	submit->nr_bos = i;
106 	spin_unlock(&file->table_lock);
107 
108 	return ret;
109 }
110 
111 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
112 {
113 	if (submit->bos[i].flags & BO_LOCKED) {
114 		struct drm_gem_object *obj = &submit->bos[i].obj->base;
115 
116 		dma_resv_unlock(obj->resv);
117 		submit->bos[i].flags &= ~BO_LOCKED;
118 	}
119 }
120 
121 static int submit_lock_objects(struct etnaviv_gem_submit *submit,
122 		struct ww_acquire_ctx *ticket)
123 {
124 	int contended, slow_locked = -1, i, ret = 0;
125 
126 retry:
127 	for (i = 0; i < submit->nr_bos; i++) {
128 		struct drm_gem_object *obj = &submit->bos[i].obj->base;
129 
130 		if (slow_locked == i)
131 			slow_locked = -1;
132 
133 		contended = i;
134 
135 		if (!(submit->bos[i].flags & BO_LOCKED)) {
136 			ret = dma_resv_lock_interruptible(obj->resv, ticket);
137 			if (ret == -EALREADY)
138 				DRM_ERROR("BO at index %u already on submit list\n",
139 					  i);
140 			if (ret)
141 				goto fail;
142 			submit->bos[i].flags |= BO_LOCKED;
143 		}
144 	}
145 
146 	ww_acquire_done(ticket);
147 
148 	return 0;
149 
150 fail:
151 	for (; i >= 0; i--)
152 		submit_unlock_object(submit, i);
153 
154 	if (slow_locked > 0)
155 		submit_unlock_object(submit, slow_locked);
156 
157 	if (ret == -EDEADLK) {
158 		struct drm_gem_object *obj;
159 
160 		obj = &submit->bos[contended].obj->base;
161 
162 		/* we lost out in a seqno race, lock and retry.. */
163 		ret = dma_resv_lock_slow_interruptible(obj->resv, ticket);
164 		if (!ret) {
165 			submit->bos[contended].flags |= BO_LOCKED;
166 			slow_locked = contended;
167 			goto retry;
168 		}
169 	}
170 
171 	return ret;
172 }
173 
174 static int submit_fence_sync(struct etnaviv_gem_submit *submit)
175 {
176 	int i, ret = 0;
177 
178 	for (i = 0; i < submit->nr_bos; i++) {
179 		struct etnaviv_gem_submit_bo *bo = &submit->bos[i];
180 		struct dma_resv *robj = bo->obj->base.resv;
181 
182 		if (!(bo->flags & ETNA_SUBMIT_BO_WRITE)) {
183 			ret = dma_resv_reserve_shared(robj, 1);
184 			if (ret)
185 				return ret;
186 		}
187 
188 		if (submit->flags & ETNA_SUBMIT_NO_IMPLICIT)
189 			continue;
190 
191 		if (bo->flags & ETNA_SUBMIT_BO_WRITE) {
192 			ret = dma_resv_get_fences(robj, true, &bo->nr_shared,
193 						  &bo->shared);
194 			if (ret)
195 				return ret;
196 		} else {
197 			bo->excl = dma_fence_get(dma_resv_excl_fence(robj));
198 		}
199 
200 	}
201 
202 	return ret;
203 }
204 
205 static void submit_attach_object_fences(struct etnaviv_gem_submit *submit)
206 {
207 	int i;
208 
209 	for (i = 0; i < submit->nr_bos; i++) {
210 		struct drm_gem_object *obj = &submit->bos[i].obj->base;
211 
212 		if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
213 			dma_resv_add_excl_fence(obj->resv,
214 							  submit->out_fence);
215 		else
216 			dma_resv_add_shared_fence(obj->resv,
217 							    submit->out_fence);
218 
219 		submit_unlock_object(submit, i);
220 	}
221 }
222 
223 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
224 {
225 	int i, ret = 0;
226 
227 	for (i = 0; i < submit->nr_bos; i++) {
228 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
229 		struct etnaviv_vram_mapping *mapping;
230 
231 		mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
232 						  submit->mmu_context,
233 						  submit->bos[i].va);
234 		if (IS_ERR(mapping)) {
235 			ret = PTR_ERR(mapping);
236 			break;
237 		}
238 
239 		if ((submit->flags & ETNA_SUBMIT_SOFTPIN) &&
240 		     submit->bos[i].va != mapping->iova) {
241 			etnaviv_gem_mapping_unreference(mapping);
242 			return -EINVAL;
243 		}
244 
245 		atomic_inc(&etnaviv_obj->gpu_active);
246 
247 		submit->bos[i].flags |= BO_PINNED;
248 		submit->bos[i].mapping = mapping;
249 	}
250 
251 	return ret;
252 }
253 
254 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
255 	struct etnaviv_gem_submit_bo **bo)
256 {
257 	if (idx >= submit->nr_bos) {
258 		DRM_ERROR("invalid buffer index: %u (out of %u)\n",
259 				idx, submit->nr_bos);
260 		return -EINVAL;
261 	}
262 
263 	*bo = &submit->bos[idx];
264 
265 	return 0;
266 }
267 
268 /* process the reloc's and patch up the cmdstream as needed: */
269 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
270 		u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
271 		u32 nr_relocs)
272 {
273 	u32 i, last_offset = 0;
274 	u32 *ptr = stream;
275 	int ret;
276 
277 	/* Submits using softpin don't blend with relocs */
278 	if ((submit->flags & ETNA_SUBMIT_SOFTPIN) && nr_relocs != 0)
279 		return -EINVAL;
280 
281 	for (i = 0; i < nr_relocs; i++) {
282 		const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
283 		struct etnaviv_gem_submit_bo *bo;
284 		u32 off;
285 
286 		if (unlikely(r->flags)) {
287 			DRM_ERROR("invalid reloc flags\n");
288 			return -EINVAL;
289 		}
290 
291 		if (r->submit_offset % 4) {
292 			DRM_ERROR("non-aligned reloc offset: %u\n",
293 				  r->submit_offset);
294 			return -EINVAL;
295 		}
296 
297 		/* offset in dwords: */
298 		off = r->submit_offset / 4;
299 
300 		if ((off >= size ) ||
301 				(off < last_offset)) {
302 			DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
303 			return -EINVAL;
304 		}
305 
306 		ret = submit_bo(submit, r->reloc_idx, &bo);
307 		if (ret)
308 			return ret;
309 
310 		if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
311 			DRM_ERROR("relocation %u outside object\n", i);
312 			return -EINVAL;
313 		}
314 
315 		ptr[off] = bo->mapping->iova + r->reloc_offset;
316 
317 		last_offset = off;
318 	}
319 
320 	return 0;
321 }
322 
323 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit,
324 		u32 exec_state, const struct drm_etnaviv_gem_submit_pmr *pmrs)
325 {
326 	u32 i;
327 
328 	for (i = 0; i < submit->nr_pmrs; i++) {
329 		const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i;
330 		struct etnaviv_gem_submit_bo *bo;
331 		int ret;
332 
333 		ret = submit_bo(submit, r->read_idx, &bo);
334 		if (ret)
335 			return ret;
336 
337 		/* at offset 0 a sequence number gets stored used for userspace sync */
338 		if (r->read_offset == 0) {
339 			DRM_ERROR("perfmon request: offset is 0");
340 			return -EINVAL;
341 		}
342 
343 		if (r->read_offset >= bo->obj->base.size - sizeof(u32)) {
344 			DRM_ERROR("perfmon request: offset %u outside object", i);
345 			return -EINVAL;
346 		}
347 
348 		if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) {
349 			DRM_ERROR("perfmon request: flags are not valid");
350 			return -EINVAL;
351 		}
352 
353 		if (etnaviv_pm_req_validate(r, exec_state)) {
354 			DRM_ERROR("perfmon request: domain or signal not valid");
355 			return -EINVAL;
356 		}
357 
358 		submit->pmrs[i].flags = r->flags;
359 		submit->pmrs[i].domain = r->domain;
360 		submit->pmrs[i].signal = r->signal;
361 		submit->pmrs[i].sequence = r->sequence;
362 		submit->pmrs[i].offset = r->read_offset;
363 		submit->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base);
364 	}
365 
366 	return 0;
367 }
368 
369 static void submit_cleanup(struct kref *kref)
370 {
371 	struct etnaviv_gem_submit *submit =
372 			container_of(kref, struct etnaviv_gem_submit, refcount);
373 	unsigned i;
374 
375 	if (submit->runtime_resumed)
376 		pm_runtime_put_autosuspend(submit->gpu->dev);
377 
378 	if (submit->cmdbuf.suballoc)
379 		etnaviv_cmdbuf_free(&submit->cmdbuf);
380 
381 	if (submit->mmu_context)
382 		etnaviv_iommu_context_put(submit->mmu_context);
383 
384 	if (submit->prev_mmu_context)
385 		etnaviv_iommu_context_put(submit->prev_mmu_context);
386 
387 	for (i = 0; i < submit->nr_bos; i++) {
388 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
389 
390 		/* unpin all objects */
391 		if (submit->bos[i].flags & BO_PINNED) {
392 			etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
393 			atomic_dec(&etnaviv_obj->gpu_active);
394 			submit->bos[i].mapping = NULL;
395 			submit->bos[i].flags &= ~BO_PINNED;
396 		}
397 
398 		/* if the GPU submit failed, objects might still be locked */
399 		submit_unlock_object(submit, i);
400 		drm_gem_object_put(&etnaviv_obj->base);
401 	}
402 
403 	wake_up_all(&submit->gpu->fence_event);
404 
405 	if (submit->in_fence)
406 		dma_fence_put(submit->in_fence);
407 	if (submit->out_fence) {
408 		/* first remove from IDR, so fence can not be found anymore */
409 		mutex_lock(&submit->gpu->fence_lock);
410 		idr_remove(&submit->gpu->fence_idr, submit->out_fence_id);
411 		mutex_unlock(&submit->gpu->fence_lock);
412 		dma_fence_put(submit->out_fence);
413 	}
414 	kfree(submit->pmrs);
415 	kfree(submit);
416 }
417 
418 void etnaviv_submit_put(struct etnaviv_gem_submit *submit)
419 {
420 	kref_put(&submit->refcount, submit_cleanup);
421 }
422 
423 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
424 		struct drm_file *file)
425 {
426 	struct etnaviv_file_private *ctx = file->driver_priv;
427 	struct etnaviv_drm_private *priv = dev->dev_private;
428 	struct drm_etnaviv_gem_submit *args = data;
429 	struct drm_etnaviv_gem_submit_reloc *relocs;
430 	struct drm_etnaviv_gem_submit_pmr *pmrs;
431 	struct drm_etnaviv_gem_submit_bo *bos;
432 	struct etnaviv_gem_submit *submit;
433 	struct etnaviv_gpu *gpu;
434 	struct sync_file *sync_file = NULL;
435 	struct ww_acquire_ctx ticket;
436 	int out_fence_fd = -1;
437 	void *stream;
438 	int ret;
439 
440 	if (args->pipe >= ETNA_MAX_PIPES)
441 		return -EINVAL;
442 
443 	gpu = priv->gpu[args->pipe];
444 	if (!gpu)
445 		return -ENXIO;
446 
447 	if (args->stream_size % 4) {
448 		DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
449 			  args->stream_size);
450 		return -EINVAL;
451 	}
452 
453 	if (args->exec_state != ETNA_PIPE_3D &&
454 	    args->exec_state != ETNA_PIPE_2D &&
455 	    args->exec_state != ETNA_PIPE_VG) {
456 		DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
457 		return -EINVAL;
458 	}
459 
460 	if (args->flags & ~ETNA_SUBMIT_FLAGS) {
461 		DRM_ERROR("invalid flags: 0x%x\n", args->flags);
462 		return -EINVAL;
463 	}
464 
465 	if ((args->flags & ETNA_SUBMIT_SOFTPIN) &&
466 	    priv->mmu_global->version != ETNAVIV_IOMMU_V2) {
467 		DRM_ERROR("softpin requested on incompatible MMU\n");
468 		return -EINVAL;
469 	}
470 
471 	if (args->stream_size > SZ_128K || args->nr_relocs > SZ_128K ||
472 	    args->nr_bos > SZ_128K || args->nr_pmrs > 128) {
473 		DRM_ERROR("submit arguments out of size limits\n");
474 		return -EINVAL;
475 	}
476 
477 	/*
478 	 * Copy the command submission and bo array to kernel space in
479 	 * one go, and do this outside of any locks.
480 	 */
481 	bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL);
482 	relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL);
483 	pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL);
484 	stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL);
485 	if (!bos || !relocs || !pmrs || !stream) {
486 		ret = -ENOMEM;
487 		goto err_submit_cmds;
488 	}
489 
490 	ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
491 			     args->nr_bos * sizeof(*bos));
492 	if (ret) {
493 		ret = -EFAULT;
494 		goto err_submit_cmds;
495 	}
496 
497 	ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
498 			     args->nr_relocs * sizeof(*relocs));
499 	if (ret) {
500 		ret = -EFAULT;
501 		goto err_submit_cmds;
502 	}
503 
504 	ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs),
505 			     args->nr_pmrs * sizeof(*pmrs));
506 	if (ret) {
507 		ret = -EFAULT;
508 		goto err_submit_cmds;
509 	}
510 
511 	ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
512 			     args->stream_size);
513 	if (ret) {
514 		ret = -EFAULT;
515 		goto err_submit_cmds;
516 	}
517 
518 	if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
519 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
520 		if (out_fence_fd < 0) {
521 			ret = out_fence_fd;
522 			goto err_submit_cmds;
523 		}
524 	}
525 
526 	ww_acquire_init(&ticket, &reservation_ww_class);
527 
528 	submit = submit_create(dev, gpu, args->nr_bos, args->nr_pmrs);
529 	if (!submit) {
530 		ret = -ENOMEM;
531 		goto err_submit_ww_acquire;
532 	}
533 
534 	ret = etnaviv_cmdbuf_init(priv->cmdbuf_suballoc, &submit->cmdbuf,
535 				  ALIGN(args->stream_size, 8) + 8);
536 	if (ret)
537 		goto err_submit_objects;
538 
539 	submit->ctx = file->driver_priv;
540 	submit->mmu_context = etnaviv_iommu_context_get(submit->ctx->mmu);
541 	submit->exec_state = args->exec_state;
542 	submit->flags = args->flags;
543 
544 	ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
545 	if (ret)
546 		goto err_submit_objects;
547 
548 	if ((priv->mmu_global->version != ETNAVIV_IOMMU_V2) &&
549 	    !etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
550 				      relocs, args->nr_relocs)) {
551 		ret = -EINVAL;
552 		goto err_submit_objects;
553 	}
554 
555 	if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) {
556 		submit->in_fence = sync_file_get_fence(args->fence_fd);
557 		if (!submit->in_fence) {
558 			ret = -EINVAL;
559 			goto err_submit_objects;
560 		}
561 	}
562 
563 	ret = submit_pin_objects(submit);
564 	if (ret)
565 		goto err_submit_objects;
566 
567 	ret = submit_reloc(submit, stream, args->stream_size / 4,
568 			   relocs, args->nr_relocs);
569 	if (ret)
570 		goto err_submit_objects;
571 
572 	ret = submit_perfmon_validate(submit, args->exec_state, pmrs);
573 	if (ret)
574 		goto err_submit_objects;
575 
576 	memcpy(submit->cmdbuf.vaddr, stream, args->stream_size);
577 
578 	ret = submit_lock_objects(submit, &ticket);
579 	if (ret)
580 		goto err_submit_objects;
581 
582 	ret = submit_fence_sync(submit);
583 	if (ret)
584 		goto err_submit_objects;
585 
586 	ret = etnaviv_sched_push_job(&ctx->sched_entity[args->pipe], submit);
587 	if (ret)
588 		goto err_submit_objects;
589 
590 	submit_attach_object_fences(submit);
591 
592 	if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
593 		/*
594 		 * This can be improved: ideally we want to allocate the sync
595 		 * file before kicking off the GPU job and just attach the
596 		 * fence to the sync file here, eliminating the ENOMEM
597 		 * possibility at this stage.
598 		 */
599 		sync_file = sync_file_create(submit->out_fence);
600 		if (!sync_file) {
601 			ret = -ENOMEM;
602 			goto err_submit_objects;
603 		}
604 		fd_install(out_fence_fd, sync_file->file);
605 	}
606 
607 	args->fence_fd = out_fence_fd;
608 	args->fence = submit->out_fence_id;
609 
610 err_submit_objects:
611 	etnaviv_submit_put(submit);
612 
613 err_submit_ww_acquire:
614 	ww_acquire_fini(&ticket);
615 
616 err_submit_cmds:
617 	if (ret && (out_fence_fd >= 0))
618 		put_unused_fd(out_fence_fd);
619 	kvfree(stream);
620 	kvfree(bos);
621 	kvfree(relocs);
622 	kvfree(pmrs);
623 
624 	return ret;
625 }
626