xref: /linux/drivers/gpu/drm/panthor/panthor_drv.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
1 // SPDX-License-Identifier: GPL-2.0 or MIT
2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
3 /* Copyright 2019 Linaro, Ltd., Rob Herring <robh@kernel.org> */
4 /* Copyright 2019 Collabora ltd. */
5 
6 #ifdef CONFIG_ARM_ARCH_TIMER
7 #include <asm/arch_timer.h>
8 #endif
9 
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/pagemap.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/time64.h>
17 
18 #include <drm/drm_auth.h>
19 #include <drm/drm_debugfs.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_exec.h>
22 #include <drm/drm_ioctl.h>
23 #include <drm/drm_print.h>
24 #include <drm/drm_syncobj.h>
25 #include <drm/drm_utils.h>
26 #include <drm/gpu_scheduler.h>
27 #include <drm/panthor_drm.h>
28 
29 #include "panthor_device.h"
30 #include "panthor_fw.h"
31 #include "panthor_gem.h"
32 #include "panthor_gpu.h"
33 #include "panthor_heap.h"
34 #include "panthor_mmu.h"
35 #include "panthor_regs.h"
36 #include "panthor_sched.h"
37 
38 /**
39  * DOC: user <-> kernel object copy helpers.
40  */
41 
42 /**
43  * panthor_set_uobj() - Copy kernel object to user object.
44  * @usr_ptr: Users pointer.
45  * @usr_size: Size of the user object.
46  * @min_size: Minimum size for this object.
47  * @kern_size: Size of the kernel object.
48  * @in: Address of the kernel object to copy.
49  *
50  * Helper automating kernel -> user object copies.
51  *
52  * Don't use this function directly, use PANTHOR_UOBJ_SET() instead.
53  *
54  * Return: 0 on success, a negative error code otherwise.
55  */
56 static int
57 panthor_set_uobj(u64 usr_ptr, u32 usr_size, u32 min_size, u32 kern_size, const void *in)
58 {
59 	/* User size shouldn't be smaller than the minimal object size. */
60 	if (usr_size < min_size)
61 		return -EINVAL;
62 
63 	if (copy_to_user(u64_to_user_ptr(usr_ptr), in, min_t(u32, usr_size, kern_size)))
64 		return -EFAULT;
65 
66 	/* When the kernel object is smaller than the user object, we fill the gap with
67 	 * zeros.
68 	 */
69 	if (usr_size > kern_size &&
70 	    clear_user(u64_to_user_ptr(usr_ptr + kern_size), usr_size - kern_size)) {
71 		return -EFAULT;
72 	}
73 
74 	return 0;
75 }
76 
77 /**
78  * panthor_get_uobj_array() - Copy a user object array into a kernel accessible object array.
79  * @in: The object array to copy.
80  * @min_stride: Minimum array stride.
81  * @obj_size: Kernel object size.
82  *
83  * Helper automating user -> kernel object copies.
84  *
85  * Don't use this function directly, use PANTHOR_UOBJ_GET_ARRAY() instead.
86  *
87  * Return: newly allocated object array or an ERR_PTR on error.
88  */
89 static void *
90 panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride,
91 		       u32 obj_size)
92 {
93 	int ret = 0;
94 	void *out_alloc;
95 
96 	if (!in->count)
97 		return NULL;
98 
99 	/* User stride must be at least the minimum object size, otherwise it might
100 	 * lack useful information.
101 	 */
102 	if (in->stride < min_stride)
103 		return ERR_PTR(-EINVAL);
104 
105 	out_alloc = kvmalloc_array(in->count, obj_size, GFP_KERNEL);
106 	if (!out_alloc)
107 		return ERR_PTR(-ENOMEM);
108 
109 	if (obj_size == in->stride) {
110 		/* Fast path when user/kernel have the same uAPI header version. */
111 		if (copy_from_user(out_alloc, u64_to_user_ptr(in->array),
112 				   (unsigned long)obj_size * in->count))
113 			ret = -EFAULT;
114 	} else {
115 		void __user *in_ptr = u64_to_user_ptr(in->array);
116 		void *out_ptr = out_alloc;
117 
118 		/* If the sizes differ, we need to copy elements one by one. */
119 		for (u32 i = 0; i < in->count; i++) {
120 			ret = copy_struct_from_user(out_ptr, obj_size, in_ptr, in->stride);
121 			if (ret)
122 				break;
123 
124 			out_ptr += obj_size;
125 			in_ptr += in->stride;
126 		}
127 	}
128 
129 	if (ret) {
130 		kvfree(out_alloc);
131 		return ERR_PTR(ret);
132 	}
133 
134 	return out_alloc;
135 }
136 
137 /**
138  * PANTHOR_UOBJ_MIN_SIZE_INTERNAL() - Get the minimum user object size
139  * @_typename: Object type.
140  * @_last_mandatory_field: Last mandatory field.
141  *
142  * Get the minimum user object size based on the last mandatory field name,
143  * A.K.A, the name of the last field of the structure at the time this
144  * structure was added to the uAPI.
145  *
146  * Don't use directly, use PANTHOR_UOBJ_DECL() instead.
147  */
148 #define PANTHOR_UOBJ_MIN_SIZE_INTERNAL(_typename, _last_mandatory_field) \
149 	(offsetof(_typename, _last_mandatory_field) + \
150 	 sizeof(((_typename *)NULL)->_last_mandatory_field))
151 
152 /**
153  * PANTHOR_UOBJ_DECL() - Declare a new uAPI object whose subject to
154  * evolutions.
155  * @_typename: Object type.
156  * @_last_mandatory_field: Last mandatory field.
157  *
158  * Should be used to extend the PANTHOR_UOBJ_MIN_SIZE() list.
159  */
160 #define PANTHOR_UOBJ_DECL(_typename, _last_mandatory_field) \
161 	_typename : PANTHOR_UOBJ_MIN_SIZE_INTERNAL(_typename, _last_mandatory_field)
162 
163 /**
164  * PANTHOR_UOBJ_MIN_SIZE() - Get the minimum size of a given uAPI object
165  * @_obj_name: Object to get the minimum size of.
166  *
167  * Don't use this macro directly, it's automatically called by
168  * PANTHOR_UOBJ_{SET,GET_ARRAY}().
169  */
170 #define PANTHOR_UOBJ_MIN_SIZE(_obj_name) \
171 	_Generic(_obj_name, \
172 		 PANTHOR_UOBJ_DECL(struct drm_panthor_gpu_info, tiler_present), \
173 		 PANTHOR_UOBJ_DECL(struct drm_panthor_csif_info, pad), \
174 		 PANTHOR_UOBJ_DECL(struct drm_panthor_timestamp_info, current_timestamp), \
175 		 PANTHOR_UOBJ_DECL(struct drm_panthor_group_priorities_info, pad), \
176 		 PANTHOR_UOBJ_DECL(struct drm_panthor_sync_op, timeline_value), \
177 		 PANTHOR_UOBJ_DECL(struct drm_panthor_queue_submit, syncs), \
178 		 PANTHOR_UOBJ_DECL(struct drm_panthor_queue_create, ringbuf_size), \
179 		 PANTHOR_UOBJ_DECL(struct drm_panthor_vm_bind_op, syncs))
180 
181 /**
182  * PANTHOR_UOBJ_SET() - Copy a kernel object to a user object.
183  * @_dest_usr_ptr: User pointer to copy to.
184  * @_usr_size: Size of the user object.
185  * @_src_obj: Kernel object to copy (not a pointer).
186  *
187  * Return: 0 on success, a negative error code otherwise.
188  */
189 #define PANTHOR_UOBJ_SET(_dest_usr_ptr, _usr_size, _src_obj) \
190 	panthor_set_uobj(_dest_usr_ptr, _usr_size, \
191 			 PANTHOR_UOBJ_MIN_SIZE(_src_obj), \
192 			 sizeof(_src_obj), &(_src_obj))
193 
194 /**
195  * PANTHOR_UOBJ_GET_ARRAY() - Copy a user object array to a kernel accessible
196  * object array.
197  * @_dest_array: Local variable that will hold the newly allocated kernel
198  * object array.
199  * @_uobj_array: The drm_panthor_obj_array object describing the user object
200  * array.
201  *
202  * Return: 0 on success, a negative error code otherwise.
203  */
204 #define PANTHOR_UOBJ_GET_ARRAY(_dest_array, _uobj_array) \
205 	({ \
206 		typeof(_dest_array) _tmp; \
207 		_tmp = panthor_get_uobj_array(_uobj_array, \
208 					      PANTHOR_UOBJ_MIN_SIZE((_dest_array)[0]), \
209 					      sizeof((_dest_array)[0])); \
210 		if (!IS_ERR(_tmp)) \
211 			_dest_array = _tmp; \
212 		PTR_ERR_OR_ZERO(_tmp); \
213 	})
214 
215 /**
216  * struct panthor_sync_signal - Represent a synchronization object point to attach
217  * our job fence to.
218  *
219  * This structure is here to keep track of fences that are currently bound to
220  * a specific syncobj point.
221  *
222  * At the beginning of a job submission, the fence
223  * is retrieved from the syncobj itself, and can be NULL if no fence was attached
224  * to this point.
225  *
226  * At the end, it points to the fence of the last job that had a
227  * %DRM_PANTHOR_SYNC_OP_SIGNAL on this syncobj.
228  *
229  * With jobs being submitted in batches, the fence might change several times during
230  * the process, allowing one job to wait on a job that's part of the same submission
231  * but appears earlier in the drm_panthor_group_submit::queue_submits array.
232  */
233 struct panthor_sync_signal {
234 	/** @node: list_head to track signal ops within a submit operation */
235 	struct list_head node;
236 
237 	/** @handle: The syncobj handle. */
238 	u32 handle;
239 
240 	/**
241 	 * @point: The syncobj point.
242 	 *
243 	 * Zero for regular syncobjs, and non-zero for timeline syncobjs.
244 	 */
245 	u64 point;
246 
247 	/**
248 	 * @syncobj: The sync object pointed by @handle.
249 	 */
250 	struct drm_syncobj *syncobj;
251 
252 	/**
253 	 * @chain: Chain object used to link the new fence to an existing
254 	 * timeline syncobj.
255 	 *
256 	 * NULL for regular syncobj, non-NULL for timeline syncobjs.
257 	 */
258 	struct dma_fence_chain *chain;
259 
260 	/**
261 	 * @fence: The fence to assign to the syncobj or syncobj-point.
262 	 */
263 	struct dma_fence *fence;
264 };
265 
266 /**
267  * struct panthor_job_ctx - Job context
268  */
269 struct panthor_job_ctx {
270 	/** @job: The job that is about to be submitted to drm_sched. */
271 	struct drm_sched_job *job;
272 
273 	/** @syncops: Array of sync operations. */
274 	struct drm_panthor_sync_op *syncops;
275 
276 	/** @syncop_count: Number of sync operations. */
277 	u32 syncop_count;
278 };
279 
280 /**
281  * struct panthor_submit_ctx - Submission context
282  *
283  * Anything that's related to a submission (%DRM_IOCTL_PANTHOR_VM_BIND or
284  * %DRM_IOCTL_PANTHOR_GROUP_SUBMIT) is kept here, so we can automate the
285  * initialization and cleanup steps.
286  */
287 struct panthor_submit_ctx {
288 	/** @file: DRM file this submission happens on. */
289 	struct drm_file *file;
290 
291 	/**
292 	 * @signals: List of struct panthor_sync_signal.
293 	 *
294 	 * %DRM_PANTHOR_SYNC_OP_SIGNAL operations will be recorded here,
295 	 * and %DRM_PANTHOR_SYNC_OP_WAIT will first check if an entry
296 	 * matching the syncobj+point exists before calling
297 	 * drm_syncobj_find_fence(). This allows us to describe dependencies
298 	 * existing between jobs that are part of the same batch.
299 	 */
300 	struct list_head signals;
301 
302 	/** @jobs: Array of jobs. */
303 	struct panthor_job_ctx *jobs;
304 
305 	/** @job_count: Number of entries in the @jobs array. */
306 	u32 job_count;
307 
308 	/** @exec: drm_exec context used to acquire and prepare resv objects. */
309 	struct drm_exec exec;
310 };
311 
312 #define PANTHOR_SYNC_OP_FLAGS_MASK \
313 	(DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_MASK | DRM_PANTHOR_SYNC_OP_SIGNAL)
314 
315 static bool sync_op_is_signal(const struct drm_panthor_sync_op *sync_op)
316 {
317 	return !!(sync_op->flags & DRM_PANTHOR_SYNC_OP_SIGNAL);
318 }
319 
320 static bool sync_op_is_wait(const struct drm_panthor_sync_op *sync_op)
321 {
322 	/* Note that DRM_PANTHOR_SYNC_OP_WAIT == 0 */
323 	return !(sync_op->flags & DRM_PANTHOR_SYNC_OP_SIGNAL);
324 }
325 
326 /**
327  * panthor_check_sync_op() - Check drm_panthor_sync_op fields
328  * @sync_op: The sync operation to check.
329  *
330  * Return: 0 on success, -EINVAL otherwise.
331  */
332 static int
333 panthor_check_sync_op(const struct drm_panthor_sync_op *sync_op)
334 {
335 	u8 handle_type;
336 
337 	if (sync_op->flags & ~PANTHOR_SYNC_OP_FLAGS_MASK)
338 		return -EINVAL;
339 
340 	handle_type = sync_op->flags & DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_MASK;
341 	if (handle_type != DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ &&
342 	    handle_type != DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ)
343 		return -EINVAL;
344 
345 	if (handle_type == DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ &&
346 	    sync_op->timeline_value != 0)
347 		return -EINVAL;
348 
349 	return 0;
350 }
351 
352 /**
353  * panthor_sync_signal_free() - Release resources and free a panthor_sync_signal object
354  * @sig_sync: Signal object to free.
355  */
356 static void
357 panthor_sync_signal_free(struct panthor_sync_signal *sig_sync)
358 {
359 	if (!sig_sync)
360 		return;
361 
362 	drm_syncobj_put(sig_sync->syncobj);
363 	dma_fence_chain_free(sig_sync->chain);
364 	dma_fence_put(sig_sync->fence);
365 	kfree(sig_sync);
366 }
367 
368 /**
369  * panthor_submit_ctx_add_sync_signal() - Add a signal operation to a submit context
370  * @ctx: Context to add the signal operation to.
371  * @handle: Syncobj handle.
372  * @point: Syncobj point.
373  *
374  * Return: 0 on success, otherwise negative error value.
375  */
376 static int
377 panthor_submit_ctx_add_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
378 {
379 	struct panthor_sync_signal *sig_sync;
380 	struct dma_fence *cur_fence;
381 	int ret;
382 
383 	sig_sync = kzalloc(sizeof(*sig_sync), GFP_KERNEL);
384 	if (!sig_sync)
385 		return -ENOMEM;
386 
387 	sig_sync->handle = handle;
388 	sig_sync->point = point;
389 
390 	if (point > 0) {
391 		sig_sync->chain = dma_fence_chain_alloc();
392 		if (!sig_sync->chain) {
393 			ret = -ENOMEM;
394 			goto err_free_sig_sync;
395 		}
396 	}
397 
398 	sig_sync->syncobj = drm_syncobj_find(ctx->file, handle);
399 	if (!sig_sync->syncobj) {
400 		ret = -EINVAL;
401 		goto err_free_sig_sync;
402 	}
403 
404 	/* Retrieve the current fence attached to that point. It's
405 	 * perfectly fine to get a NULL fence here, it just means there's
406 	 * no fence attached to that point yet.
407 	 */
408 	if (!drm_syncobj_find_fence(ctx->file, handle, point, 0, &cur_fence))
409 		sig_sync->fence = cur_fence;
410 
411 	list_add_tail(&sig_sync->node, &ctx->signals);
412 
413 	return 0;
414 
415 err_free_sig_sync:
416 	panthor_sync_signal_free(sig_sync);
417 	return ret;
418 }
419 
420 /**
421  * panthor_submit_ctx_search_sync_signal() - Search an existing signal operation in a
422  * submit context.
423  * @ctx: Context to search the signal operation in.
424  * @handle: Syncobj handle.
425  * @point: Syncobj point.
426  *
427  * Return: A valid panthor_sync_signal object if found, NULL otherwise.
428  */
429 static struct panthor_sync_signal *
430 panthor_submit_ctx_search_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
431 {
432 	struct panthor_sync_signal *sig_sync;
433 
434 	list_for_each_entry(sig_sync, &ctx->signals, node) {
435 		if (handle == sig_sync->handle && point == sig_sync->point)
436 			return sig_sync;
437 	}
438 
439 	return NULL;
440 }
441 
442 /**
443  * panthor_submit_ctx_add_job() - Add a job to a submit context
444  * @ctx: Context to search the signal operation in.
445  * @idx: Index of the job in the context.
446  * @job: Job to add.
447  * @syncs: Sync operations provided by userspace.
448  *
449  * Return: 0 on success, a negative error code otherwise.
450  */
451 static int
452 panthor_submit_ctx_add_job(struct panthor_submit_ctx *ctx, u32 idx,
453 			   struct drm_sched_job *job,
454 			   const struct drm_panthor_obj_array *syncs)
455 {
456 	int ret;
457 
458 	ctx->jobs[idx].job = job;
459 
460 	ret = PANTHOR_UOBJ_GET_ARRAY(ctx->jobs[idx].syncops, syncs);
461 	if (ret)
462 		return ret;
463 
464 	ctx->jobs[idx].syncop_count = syncs->count;
465 	return 0;
466 }
467 
468 /**
469  * panthor_submit_ctx_get_sync_signal() - Search signal operation and add one if none was found.
470  * @ctx: Context to search the signal operation in.
471  * @handle: Syncobj handle.
472  * @point: Syncobj point.
473  *
474  * Return: 0 on success, a negative error code otherwise.
475  */
476 static int
477 panthor_submit_ctx_get_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
478 {
479 	struct panthor_sync_signal *sig_sync;
480 
481 	sig_sync = panthor_submit_ctx_search_sync_signal(ctx, handle, point);
482 	if (sig_sync)
483 		return 0;
484 
485 	return panthor_submit_ctx_add_sync_signal(ctx, handle, point);
486 }
487 
488 /**
489  * panthor_submit_ctx_update_job_sync_signal_fences() - Update fences
490  * on the signal operations specified by a job.
491  * @ctx: Context to search the signal operation in.
492  * @job_idx: Index of the job to operate on.
493  *
494  * Return: 0 on success, a negative error code otherwise.
495  */
496 static int
497 panthor_submit_ctx_update_job_sync_signal_fences(struct panthor_submit_ctx *ctx,
498 						 u32 job_idx)
499 {
500 	struct panthor_device *ptdev = container_of(ctx->file->minor->dev,
501 						    struct panthor_device,
502 						    base);
503 	struct dma_fence *done_fence = &ctx->jobs[job_idx].job->s_fence->finished;
504 	const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
505 	u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
506 
507 	for (u32 i = 0; i < sync_op_count; i++) {
508 		struct dma_fence *old_fence;
509 		struct panthor_sync_signal *sig_sync;
510 
511 		if (!sync_op_is_signal(&sync_ops[i]))
512 			continue;
513 
514 		sig_sync = panthor_submit_ctx_search_sync_signal(ctx, sync_ops[i].handle,
515 								 sync_ops[i].timeline_value);
516 		if (drm_WARN_ON(&ptdev->base, !sig_sync))
517 			return -EINVAL;
518 
519 		old_fence = sig_sync->fence;
520 		sig_sync->fence = dma_fence_get(done_fence);
521 		dma_fence_put(old_fence);
522 
523 		if (drm_WARN_ON(&ptdev->base, !sig_sync->fence))
524 			return -EINVAL;
525 	}
526 
527 	return 0;
528 }
529 
530 /**
531  * panthor_submit_ctx_collect_job_signal_ops() - Iterate over all job signal operations
532  * and add them to the context.
533  * @ctx: Context to search the signal operation in.
534  * @job_idx: Index of the job to operate on.
535  *
536  * Return: 0 on success, a negative error code otherwise.
537  */
538 static int
539 panthor_submit_ctx_collect_job_signal_ops(struct panthor_submit_ctx *ctx,
540 					  u32 job_idx)
541 {
542 	const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
543 	u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
544 
545 	for (u32 i = 0; i < sync_op_count; i++) {
546 		int ret;
547 
548 		if (!sync_op_is_signal(&sync_ops[i]))
549 			continue;
550 
551 		ret = panthor_check_sync_op(&sync_ops[i]);
552 		if (ret)
553 			return ret;
554 
555 		ret = panthor_submit_ctx_get_sync_signal(ctx,
556 							 sync_ops[i].handle,
557 							 sync_ops[i].timeline_value);
558 		if (ret)
559 			return ret;
560 	}
561 
562 	return 0;
563 }
564 
565 /**
566  * panthor_submit_ctx_push_fences() - Iterate over the signal array, and for each entry, push
567  * the currently assigned fence to the associated syncobj.
568  * @ctx: Context to push fences on.
569  *
570  * This is the last step of a submission procedure, and is done once we know the submission
571  * is effective and job fences are guaranteed to be signaled in finite time.
572  */
573 static void
574 panthor_submit_ctx_push_fences(struct panthor_submit_ctx *ctx)
575 {
576 	struct panthor_sync_signal *sig_sync;
577 
578 	list_for_each_entry(sig_sync, &ctx->signals, node) {
579 		if (sig_sync->chain) {
580 			drm_syncobj_add_point(sig_sync->syncobj, sig_sync->chain,
581 					      sig_sync->fence, sig_sync->point);
582 			sig_sync->chain = NULL;
583 		} else {
584 			drm_syncobj_replace_fence(sig_sync->syncobj, sig_sync->fence);
585 		}
586 	}
587 }
588 
589 /**
590  * panthor_submit_ctx_add_sync_deps_to_job() - Add sync wait operations as
591  * job dependencies.
592  * @ctx: Submit context.
593  * @job_idx: Index of the job to operate on.
594  *
595  * Return: 0 on success, a negative error code otherwise.
596  */
597 static int
598 panthor_submit_ctx_add_sync_deps_to_job(struct panthor_submit_ctx *ctx,
599 					u32 job_idx)
600 {
601 	struct panthor_device *ptdev = container_of(ctx->file->minor->dev,
602 						    struct panthor_device,
603 						    base);
604 	const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
605 	struct drm_sched_job *job = ctx->jobs[job_idx].job;
606 	u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
607 	int ret = 0;
608 
609 	for (u32 i = 0; i < sync_op_count; i++) {
610 		struct panthor_sync_signal *sig_sync;
611 		struct dma_fence *fence;
612 
613 		if (!sync_op_is_wait(&sync_ops[i]))
614 			continue;
615 
616 		ret = panthor_check_sync_op(&sync_ops[i]);
617 		if (ret)
618 			return ret;
619 
620 		sig_sync = panthor_submit_ctx_search_sync_signal(ctx, sync_ops[i].handle,
621 								 sync_ops[i].timeline_value);
622 		if (sig_sync) {
623 			if (drm_WARN_ON(&ptdev->base, !sig_sync->fence))
624 				return -EINVAL;
625 
626 			fence = dma_fence_get(sig_sync->fence);
627 		} else {
628 			ret = drm_syncobj_find_fence(ctx->file, sync_ops[i].handle,
629 						     sync_ops[i].timeline_value,
630 						     0, &fence);
631 			if (ret)
632 				return ret;
633 		}
634 
635 		ret = drm_sched_job_add_dependency(job, fence);
636 		if (ret)
637 			return ret;
638 	}
639 
640 	return 0;
641 }
642 
643 /**
644  * panthor_submit_ctx_collect_jobs_signal_ops() - Collect all signal operations
645  * and add them to the submit context.
646  * @ctx: Submit context.
647  *
648  * Return: 0 on success, a negative error code otherwise.
649  */
650 static int
651 panthor_submit_ctx_collect_jobs_signal_ops(struct panthor_submit_ctx *ctx)
652 {
653 	for (u32 i = 0; i < ctx->job_count; i++) {
654 		int ret;
655 
656 		ret = panthor_submit_ctx_collect_job_signal_ops(ctx, i);
657 		if (ret)
658 			return ret;
659 	}
660 
661 	return 0;
662 }
663 
664 /**
665  * panthor_submit_ctx_add_deps_and_arm_jobs() - Add jobs dependencies and arm jobs
666  * @ctx: Submit context.
667  *
668  * Must be called after the resv preparation has been taken care of.
669  *
670  * Return: 0 on success, a negative error code otherwise.
671  */
672 static int
673 panthor_submit_ctx_add_deps_and_arm_jobs(struct panthor_submit_ctx *ctx)
674 {
675 	for (u32 i = 0; i < ctx->job_count; i++) {
676 		int ret;
677 
678 		ret = panthor_submit_ctx_add_sync_deps_to_job(ctx, i);
679 		if (ret)
680 			return ret;
681 
682 		drm_sched_job_arm(ctx->jobs[i].job);
683 
684 		ret = panthor_submit_ctx_update_job_sync_signal_fences(ctx, i);
685 		if (ret)
686 			return ret;
687 	}
688 
689 	return 0;
690 }
691 
692 /**
693  * panthor_submit_ctx_push_jobs() - Push jobs to their scheduling entities.
694  * @ctx: Submit context.
695  * @upd_resvs: Callback used to update reservation objects that were previously
696  * preapred.
697  */
698 static void
699 panthor_submit_ctx_push_jobs(struct panthor_submit_ctx *ctx,
700 			     void (*upd_resvs)(struct drm_exec *, struct drm_sched_job *))
701 {
702 	for (u32 i = 0; i < ctx->job_count; i++) {
703 		upd_resvs(&ctx->exec, ctx->jobs[i].job);
704 		drm_sched_entity_push_job(ctx->jobs[i].job);
705 
706 		/* Job is owned by the scheduler now. */
707 		ctx->jobs[i].job = NULL;
708 	}
709 
710 	panthor_submit_ctx_push_fences(ctx);
711 }
712 
713 /**
714  * panthor_submit_ctx_init() - Initializes a submission context
715  * @ctx: Submit context to initialize.
716  * @file: drm_file this submission happens on.
717  * @job_count: Number of jobs that will be submitted.
718  *
719  * Return: 0 on success, a negative error code otherwise.
720  */
721 static int panthor_submit_ctx_init(struct panthor_submit_ctx *ctx,
722 				   struct drm_file *file, u32 job_count)
723 {
724 	ctx->jobs = kvmalloc_array(job_count, sizeof(*ctx->jobs),
725 				   GFP_KERNEL | __GFP_ZERO);
726 	if (!ctx->jobs)
727 		return -ENOMEM;
728 
729 	ctx->file = file;
730 	ctx->job_count = job_count;
731 	INIT_LIST_HEAD(&ctx->signals);
732 	drm_exec_init(&ctx->exec,
733 		      DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES,
734 		      0);
735 	return 0;
736 }
737 
738 /**
739  * panthor_submit_ctx_cleanup() - Cleanup a submission context
740  * @ctx: Submit context to cleanup.
741  * @job_put: Job put callback.
742  */
743 static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
744 				       void (*job_put)(struct drm_sched_job *))
745 {
746 	struct panthor_sync_signal *sig_sync, *tmp;
747 	unsigned long i;
748 
749 	drm_exec_fini(&ctx->exec);
750 
751 	list_for_each_entry_safe(sig_sync, tmp, &ctx->signals, node)
752 		panthor_sync_signal_free(sig_sync);
753 
754 	for (i = 0; i < ctx->job_count; i++) {
755 		job_put(ctx->jobs[i].job);
756 		kvfree(ctx->jobs[i].syncops);
757 	}
758 
759 	kvfree(ctx->jobs);
760 }
761 
762 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
763 					struct drm_panthor_timestamp_info *arg)
764 {
765 	int ret;
766 
767 	ret = panthor_device_resume_and_get(ptdev);
768 	if (ret)
769 		return ret;
770 
771 #ifdef CONFIG_ARM_ARCH_TIMER
772 	arg->timestamp_frequency = arch_timer_get_cntfrq();
773 #else
774 	arg->timestamp_frequency = 0;
775 #endif
776 	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
777 	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
778 
779 	pm_runtime_put(ptdev->base.dev);
780 	return 0;
781 }
782 
783 static int group_priority_permit(struct drm_file *file,
784 				 u8 priority)
785 {
786 	/* Ensure that priority is valid */
787 	if (priority > PANTHOR_GROUP_PRIORITY_REALTIME)
788 		return -EINVAL;
789 
790 	/* Medium priority and below are always allowed */
791 	if (priority <= PANTHOR_GROUP_PRIORITY_MEDIUM)
792 		return 0;
793 
794 	/* Higher priorities require CAP_SYS_NICE or DRM_MASTER */
795 	if (capable(CAP_SYS_NICE) || drm_is_current_master(file))
796 		return 0;
797 
798 	return -EACCES;
799 }
800 
801 static void panthor_query_group_priorities_info(struct drm_file *file,
802 						struct drm_panthor_group_priorities_info *arg)
803 {
804 	int prio;
805 
806 	memset(arg, 0, sizeof(*arg));
807 	for (prio = PANTHOR_GROUP_PRIORITY_REALTIME; prio >= 0; prio--) {
808 		if (!group_priority_permit(file, prio))
809 			arg->allowed_mask |= BIT(prio);
810 	}
811 }
812 
813 static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct drm_file *file)
814 {
815 	struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
816 	struct drm_panthor_dev_query *args = data;
817 	struct drm_panthor_timestamp_info timestamp_info;
818 	struct drm_panthor_group_priorities_info priorities_info;
819 	int ret;
820 
821 	if (!args->pointer) {
822 		switch (args->type) {
823 		case DRM_PANTHOR_DEV_QUERY_GPU_INFO:
824 			args->size = sizeof(ptdev->gpu_info);
825 			return 0;
826 
827 		case DRM_PANTHOR_DEV_QUERY_CSIF_INFO:
828 			args->size = sizeof(ptdev->csif_info);
829 			return 0;
830 
831 		case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
832 			args->size = sizeof(timestamp_info);
833 			return 0;
834 
835 		case DRM_PANTHOR_DEV_QUERY_GROUP_PRIORITIES_INFO:
836 			args->size = sizeof(priorities_info);
837 			return 0;
838 
839 		default:
840 			return -EINVAL;
841 		}
842 	}
843 
844 	switch (args->type) {
845 	case DRM_PANTHOR_DEV_QUERY_GPU_INFO:
846 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->gpu_info);
847 
848 	case DRM_PANTHOR_DEV_QUERY_CSIF_INFO:
849 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
850 
851 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
852 		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
853 
854 		if (ret)
855 			return ret;
856 
857 		return PANTHOR_UOBJ_SET(args->pointer, args->size, timestamp_info);
858 
859 	case DRM_PANTHOR_DEV_QUERY_GROUP_PRIORITIES_INFO:
860 		panthor_query_group_priorities_info(file, &priorities_info);
861 		return PANTHOR_UOBJ_SET(args->pointer, args->size, priorities_info);
862 
863 	default:
864 		return -EINVAL;
865 	}
866 }
867 
868 #define PANTHOR_VM_CREATE_FLAGS			0
869 
870 static int panthor_ioctl_vm_create(struct drm_device *ddev, void *data,
871 				   struct drm_file *file)
872 {
873 	struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
874 	struct panthor_file *pfile = file->driver_priv;
875 	struct drm_panthor_vm_create *args = data;
876 	int cookie, ret;
877 
878 	if (!drm_dev_enter(ddev, &cookie))
879 		return -ENODEV;
880 
881 	ret = panthor_vm_pool_create_vm(ptdev, pfile->vms,  args);
882 	if (ret >= 0) {
883 		args->id = ret;
884 		ret = 0;
885 	}
886 
887 	drm_dev_exit(cookie);
888 	return ret;
889 }
890 
891 static int panthor_ioctl_vm_destroy(struct drm_device *ddev, void *data,
892 				    struct drm_file *file)
893 {
894 	struct panthor_file *pfile = file->driver_priv;
895 	struct drm_panthor_vm_destroy *args = data;
896 
897 	if (args->pad)
898 		return -EINVAL;
899 
900 	return panthor_vm_pool_destroy_vm(pfile->vms, args->id);
901 }
902 
903 #define PANTHOR_BO_FLAGS		DRM_PANTHOR_BO_NO_MMAP
904 
905 static int panthor_ioctl_bo_create(struct drm_device *ddev, void *data,
906 				   struct drm_file *file)
907 {
908 	struct panthor_file *pfile = file->driver_priv;
909 	struct drm_panthor_bo_create *args = data;
910 	struct panthor_vm *vm = NULL;
911 	int cookie, ret;
912 
913 	if (!drm_dev_enter(ddev, &cookie))
914 		return -ENODEV;
915 
916 	if (!args->size || args->pad ||
917 	    (args->flags & ~PANTHOR_BO_FLAGS)) {
918 		ret = -EINVAL;
919 		goto out_dev_exit;
920 	}
921 
922 	if (args->exclusive_vm_id) {
923 		vm = panthor_vm_pool_get_vm(pfile->vms, args->exclusive_vm_id);
924 		if (!vm) {
925 			ret = -EINVAL;
926 			goto out_dev_exit;
927 		}
928 	}
929 
930 	ret = panthor_gem_create_with_handle(file, ddev, vm, &args->size,
931 					     args->flags, &args->handle);
932 
933 	panthor_vm_put(vm);
934 
935 out_dev_exit:
936 	drm_dev_exit(cookie);
937 	return ret;
938 }
939 
940 static int panthor_ioctl_bo_mmap_offset(struct drm_device *ddev, void *data,
941 					struct drm_file *file)
942 {
943 	struct drm_panthor_bo_mmap_offset *args = data;
944 	struct panthor_gem_object *bo;
945 	struct drm_gem_object *obj;
946 	int ret;
947 
948 	if (args->pad)
949 		return -EINVAL;
950 
951 	obj = drm_gem_object_lookup(file, args->handle);
952 	if (!obj)
953 		return -ENOENT;
954 
955 	bo = to_panthor_bo(obj);
956 	if (bo->flags & DRM_PANTHOR_BO_NO_MMAP) {
957 		ret = -EPERM;
958 		goto out;
959 	}
960 
961 	ret = drm_gem_create_mmap_offset(obj);
962 	if (ret)
963 		goto out;
964 
965 	args->offset = drm_vma_node_offset_addr(&obj->vma_node);
966 
967 out:
968 	drm_gem_object_put(obj);
969 	return ret;
970 }
971 
972 static int panthor_ioctl_group_submit(struct drm_device *ddev, void *data,
973 				      struct drm_file *file)
974 {
975 	struct panthor_file *pfile = file->driver_priv;
976 	struct drm_panthor_group_submit *args = data;
977 	struct drm_panthor_queue_submit *jobs_args;
978 	struct panthor_submit_ctx ctx;
979 	int ret = 0, cookie;
980 
981 	if (args->pad)
982 		return -EINVAL;
983 
984 	if (!drm_dev_enter(ddev, &cookie))
985 		return -ENODEV;
986 
987 	ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->queue_submits);
988 	if (ret)
989 		goto out_dev_exit;
990 
991 	ret = panthor_submit_ctx_init(&ctx, file, args->queue_submits.count);
992 	if (ret)
993 		goto out_free_jobs_args;
994 
995 	/* Create jobs and attach sync operations */
996 	for (u32 i = 0; i < args->queue_submits.count; i++) {
997 		const struct drm_panthor_queue_submit *qsubmit = &jobs_args[i];
998 		struct drm_sched_job *job;
999 
1000 		job = panthor_job_create(pfile, args->group_handle, qsubmit,
1001 					 file->client_id);
1002 		if (IS_ERR(job)) {
1003 			ret = PTR_ERR(job);
1004 			goto out_cleanup_submit_ctx;
1005 		}
1006 
1007 		ret = panthor_submit_ctx_add_job(&ctx, i, job, &qsubmit->syncs);
1008 		if (ret)
1009 			goto out_cleanup_submit_ctx;
1010 	}
1011 
1012 	/*
1013 	 * Collect signal operations on all jobs, such that each job can pick
1014 	 * from it for its dependencies and update the fence to signal when the
1015 	 * job is submitted.
1016 	 */
1017 	ret = panthor_submit_ctx_collect_jobs_signal_ops(&ctx);
1018 	if (ret)
1019 		goto out_cleanup_submit_ctx;
1020 
1021 	/*
1022 	 * We acquire/prepare revs on all jobs before proceeding with the
1023 	 * dependency registration.
1024 	 *
1025 	 * This is solving two problems:
1026 	 * 1. drm_sched_job_arm() and drm_sched_entity_push_job() must be
1027 	 *    protected by a lock to make sure no concurrent access to the same
1028 	 *    entity get interleaved, which would mess up with the fence seqno
1029 	 *    ordering. Luckily, one of the resv being acquired is the VM resv,
1030 	 *    and a scheduling entity is only bound to a single VM. As soon as
1031 	 *    we acquire the VM resv, we should be safe.
1032 	 * 2. Jobs might depend on fences that were issued by previous jobs in
1033 	 *    the same batch, so we can't add dependencies on all jobs before
1034 	 *    arming previous jobs and registering the fence to the signal
1035 	 *    array, otherwise we might miss dependencies, or point to an
1036 	 *    outdated fence.
1037 	 */
1038 	if (args->queue_submits.count > 0) {
1039 		/* All jobs target the same group, so they also point to the same VM. */
1040 		struct panthor_vm *vm = panthor_job_vm(ctx.jobs[0].job);
1041 
1042 		drm_exec_until_all_locked(&ctx.exec) {
1043 			ret = panthor_vm_prepare_mapped_bos_resvs(&ctx.exec, vm,
1044 								  args->queue_submits.count);
1045 		}
1046 
1047 		if (ret)
1048 			goto out_cleanup_submit_ctx;
1049 	}
1050 
1051 	/*
1052 	 * Now that resvs are locked/prepared, we can iterate over each job to
1053 	 * add the dependencies, arm the job fence, register the job fence to
1054 	 * the signal array.
1055 	 */
1056 	ret = panthor_submit_ctx_add_deps_and_arm_jobs(&ctx);
1057 	if (ret)
1058 		goto out_cleanup_submit_ctx;
1059 
1060 	/* Nothing can fail after that point, so we can make our job fences
1061 	 * visible to the outside world. Push jobs and set the job fences to
1062 	 * the resv slots we reserved.  This also pushes the fences to the
1063 	 * syncobjs that are part of the signal array.
1064 	 */
1065 	panthor_submit_ctx_push_jobs(&ctx, panthor_job_update_resvs);
1066 
1067 out_cleanup_submit_ctx:
1068 	panthor_submit_ctx_cleanup(&ctx, panthor_job_put);
1069 
1070 out_free_jobs_args:
1071 	kvfree(jobs_args);
1072 
1073 out_dev_exit:
1074 	drm_dev_exit(cookie);
1075 	return ret;
1076 }
1077 
1078 static int panthor_ioctl_group_destroy(struct drm_device *ddev, void *data,
1079 				       struct drm_file *file)
1080 {
1081 	struct panthor_file *pfile = file->driver_priv;
1082 	struct drm_panthor_group_destroy *args = data;
1083 
1084 	if (args->pad)
1085 		return -EINVAL;
1086 
1087 	return panthor_group_destroy(pfile, args->group_handle);
1088 }
1089 
1090 static int panthor_ioctl_group_create(struct drm_device *ddev, void *data,
1091 				      struct drm_file *file)
1092 {
1093 	struct panthor_file *pfile = file->driver_priv;
1094 	struct drm_panthor_group_create *args = data;
1095 	struct drm_panthor_queue_create *queue_args;
1096 	int ret;
1097 
1098 	if (!args->queues.count || args->queues.count > MAX_CS_PER_CSG)
1099 		return -EINVAL;
1100 
1101 	ret = PANTHOR_UOBJ_GET_ARRAY(queue_args, &args->queues);
1102 	if (ret)
1103 		return ret;
1104 
1105 	ret = group_priority_permit(file, args->priority);
1106 	if (ret)
1107 		goto out;
1108 
1109 	ret = panthor_group_create(pfile, args, queue_args, file->client_id);
1110 	if (ret < 0)
1111 		goto out;
1112 	args->group_handle = ret;
1113 	ret = 0;
1114 
1115 out:
1116 	kvfree(queue_args);
1117 	return ret;
1118 }
1119 
1120 static int panthor_ioctl_group_get_state(struct drm_device *ddev, void *data,
1121 					 struct drm_file *file)
1122 {
1123 	struct panthor_file *pfile = file->driver_priv;
1124 	struct drm_panthor_group_get_state *args = data;
1125 
1126 	return panthor_group_get_state(pfile, args);
1127 }
1128 
1129 static int panthor_ioctl_tiler_heap_create(struct drm_device *ddev, void *data,
1130 					   struct drm_file *file)
1131 {
1132 	struct panthor_file *pfile = file->driver_priv;
1133 	struct drm_panthor_tiler_heap_create *args = data;
1134 	struct panthor_heap_pool *pool;
1135 	struct panthor_vm *vm;
1136 	int ret;
1137 
1138 	vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1139 	if (!vm)
1140 		return -EINVAL;
1141 
1142 	pool = panthor_vm_get_heap_pool(vm, true);
1143 	if (IS_ERR(pool)) {
1144 		ret = PTR_ERR(pool);
1145 		goto out_put_vm;
1146 	}
1147 
1148 	ret = panthor_heap_create(pool,
1149 				  args->initial_chunk_count,
1150 				  args->chunk_size,
1151 				  args->max_chunks,
1152 				  args->target_in_flight,
1153 				  &args->tiler_heap_ctx_gpu_va,
1154 				  &args->first_heap_chunk_gpu_va);
1155 	if (ret < 0)
1156 		goto out_put_heap_pool;
1157 
1158 	/* Heap pools are per-VM. We combine the VM and HEAP id to make
1159 	 * a unique heap handle.
1160 	 */
1161 	args->handle = (args->vm_id << 16) | ret;
1162 	ret = 0;
1163 
1164 out_put_heap_pool:
1165 	panthor_heap_pool_put(pool);
1166 
1167 out_put_vm:
1168 	panthor_vm_put(vm);
1169 	return ret;
1170 }
1171 
1172 static int panthor_ioctl_tiler_heap_destroy(struct drm_device *ddev, void *data,
1173 					    struct drm_file *file)
1174 {
1175 	struct panthor_file *pfile = file->driver_priv;
1176 	struct drm_panthor_tiler_heap_destroy *args = data;
1177 	struct panthor_heap_pool *pool;
1178 	struct panthor_vm *vm;
1179 	int ret;
1180 
1181 	if (args->pad)
1182 		return -EINVAL;
1183 
1184 	vm = panthor_vm_pool_get_vm(pfile->vms, args->handle >> 16);
1185 	if (!vm)
1186 		return -EINVAL;
1187 
1188 	pool = panthor_vm_get_heap_pool(vm, false);
1189 	if (IS_ERR(pool)) {
1190 		ret = PTR_ERR(pool);
1191 		goto out_put_vm;
1192 	}
1193 
1194 	ret = panthor_heap_destroy(pool, args->handle & GENMASK(15, 0));
1195 	panthor_heap_pool_put(pool);
1196 
1197 out_put_vm:
1198 	panthor_vm_put(vm);
1199 	return ret;
1200 }
1201 
1202 static int panthor_ioctl_vm_bind_async(struct drm_device *ddev,
1203 				       struct drm_panthor_vm_bind *args,
1204 				       struct drm_file *file)
1205 {
1206 	struct panthor_file *pfile = file->driver_priv;
1207 	struct drm_panthor_vm_bind_op *jobs_args;
1208 	struct panthor_submit_ctx ctx;
1209 	struct panthor_vm *vm;
1210 	int ret = 0;
1211 
1212 	vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1213 	if (!vm)
1214 		return -EINVAL;
1215 
1216 	ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->ops);
1217 	if (ret)
1218 		goto out_put_vm;
1219 
1220 	ret = panthor_submit_ctx_init(&ctx, file, args->ops.count);
1221 	if (ret)
1222 		goto out_free_jobs_args;
1223 
1224 	for (u32 i = 0; i < args->ops.count; i++) {
1225 		struct drm_panthor_vm_bind_op *op = &jobs_args[i];
1226 		struct drm_sched_job *job;
1227 
1228 		job = panthor_vm_bind_job_create(file, vm, op);
1229 		if (IS_ERR(job)) {
1230 			ret = PTR_ERR(job);
1231 			goto out_cleanup_submit_ctx;
1232 		}
1233 
1234 		ret = panthor_submit_ctx_add_job(&ctx, i, job, &op->syncs);
1235 		if (ret)
1236 			goto out_cleanup_submit_ctx;
1237 	}
1238 
1239 	ret = panthor_submit_ctx_collect_jobs_signal_ops(&ctx);
1240 	if (ret)
1241 		goto out_cleanup_submit_ctx;
1242 
1243 	/* Prepare reservation objects for each VM_BIND job. */
1244 	drm_exec_until_all_locked(&ctx.exec) {
1245 		for (u32 i = 0; i < ctx.job_count; i++) {
1246 			ret = panthor_vm_bind_job_prepare_resvs(&ctx.exec, ctx.jobs[i].job);
1247 			drm_exec_retry_on_contention(&ctx.exec);
1248 			if (ret)
1249 				goto out_cleanup_submit_ctx;
1250 		}
1251 	}
1252 
1253 	ret = panthor_submit_ctx_add_deps_and_arm_jobs(&ctx);
1254 	if (ret)
1255 		goto out_cleanup_submit_ctx;
1256 
1257 	/* Nothing can fail after that point. */
1258 	panthor_submit_ctx_push_jobs(&ctx, panthor_vm_bind_job_update_resvs);
1259 
1260 out_cleanup_submit_ctx:
1261 	panthor_submit_ctx_cleanup(&ctx, panthor_vm_bind_job_put);
1262 
1263 out_free_jobs_args:
1264 	kvfree(jobs_args);
1265 
1266 out_put_vm:
1267 	panthor_vm_put(vm);
1268 	return ret;
1269 }
1270 
1271 static int panthor_ioctl_vm_bind_sync(struct drm_device *ddev,
1272 				      struct drm_panthor_vm_bind *args,
1273 				      struct drm_file *file)
1274 {
1275 	struct panthor_file *pfile = file->driver_priv;
1276 	struct drm_panthor_vm_bind_op *jobs_args;
1277 	struct panthor_vm *vm;
1278 	int ret;
1279 
1280 	vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1281 	if (!vm)
1282 		return -EINVAL;
1283 
1284 	ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->ops);
1285 	if (ret)
1286 		goto out_put_vm;
1287 
1288 	for (u32 i = 0; i < args->ops.count; i++) {
1289 		ret = panthor_vm_bind_exec_sync_op(file, vm, &jobs_args[i]);
1290 		if (ret) {
1291 			/* Update ops.count so the user knows where things failed. */
1292 			args->ops.count = i;
1293 			break;
1294 		}
1295 	}
1296 
1297 	kvfree(jobs_args);
1298 
1299 out_put_vm:
1300 	panthor_vm_put(vm);
1301 	return ret;
1302 }
1303 
1304 #define PANTHOR_VM_BIND_FLAGS DRM_PANTHOR_VM_BIND_ASYNC
1305 
1306 static int panthor_ioctl_vm_bind(struct drm_device *ddev, void *data,
1307 				 struct drm_file *file)
1308 {
1309 	struct drm_panthor_vm_bind *args = data;
1310 	int cookie, ret;
1311 
1312 	if (!drm_dev_enter(ddev, &cookie))
1313 		return -ENODEV;
1314 
1315 	if (args->flags & DRM_PANTHOR_VM_BIND_ASYNC)
1316 		ret = panthor_ioctl_vm_bind_async(ddev, args, file);
1317 	else
1318 		ret = panthor_ioctl_vm_bind_sync(ddev, args, file);
1319 
1320 	drm_dev_exit(cookie);
1321 	return ret;
1322 }
1323 
1324 static int panthor_ioctl_vm_get_state(struct drm_device *ddev, void *data,
1325 				      struct drm_file *file)
1326 {
1327 	struct panthor_file *pfile = file->driver_priv;
1328 	struct drm_panthor_vm_get_state *args = data;
1329 	struct panthor_vm *vm;
1330 
1331 	vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1332 	if (!vm)
1333 		return -EINVAL;
1334 
1335 	if (panthor_vm_is_unusable(vm))
1336 		args->state = DRM_PANTHOR_VM_STATE_UNUSABLE;
1337 	else
1338 		args->state = DRM_PANTHOR_VM_STATE_USABLE;
1339 
1340 	panthor_vm_put(vm);
1341 	return 0;
1342 }
1343 
1344 static int panthor_ioctl_bo_set_label(struct drm_device *ddev, void *data,
1345 				      struct drm_file *file)
1346 {
1347 	struct drm_panthor_bo_set_label *args = data;
1348 	struct drm_gem_object *obj;
1349 	const char *label = NULL;
1350 	int ret = 0;
1351 
1352 	if (args->pad)
1353 		return -EINVAL;
1354 
1355 	obj = drm_gem_object_lookup(file, args->handle);
1356 	if (!obj)
1357 		return -ENOENT;
1358 
1359 	if (args->label) {
1360 		label = strndup_user((const char __user *)(uintptr_t)args->label,
1361 				     PANTHOR_BO_LABEL_MAXLEN);
1362 		if (IS_ERR(label)) {
1363 			ret = PTR_ERR(label);
1364 			if (ret == -EINVAL)
1365 				ret = -E2BIG;
1366 			goto err_put_obj;
1367 		}
1368 	}
1369 
1370 	/*
1371 	 * We treat passing a label of length 0 and passing a NULL label
1372 	 * differently, because even though they might seem conceptually
1373 	 * similar, future uses of the BO label might expect a different
1374 	 * behaviour in each case.
1375 	 */
1376 	panthor_gem_bo_set_label(obj, label);
1377 
1378 err_put_obj:
1379 	drm_gem_object_put(obj);
1380 
1381 	return ret;
1382 }
1383 
1384 static int panthor_ioctl_set_user_mmio_offset(struct drm_device *ddev,
1385 					      void *data, struct drm_file *file)
1386 {
1387 	struct drm_panthor_set_user_mmio_offset *args = data;
1388 	struct panthor_file *pfile = file->driver_priv;
1389 
1390 	if (args->offset != DRM_PANTHOR_USER_MMIO_OFFSET_32BIT &&
1391 	    args->offset != DRM_PANTHOR_USER_MMIO_OFFSET_64BIT)
1392 		return -EINVAL;
1393 
1394 	WRITE_ONCE(pfile->user_mmio.offset, args->offset);
1395 	return 0;
1396 }
1397 
1398 static int
1399 panthor_open(struct drm_device *ddev, struct drm_file *file)
1400 {
1401 	struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
1402 	struct panthor_file *pfile;
1403 	int ret;
1404 
1405 	pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
1406 	if (!pfile)
1407 		return -ENOMEM;
1408 
1409 	pfile->ptdev = ptdev;
1410 	pfile->user_mmio.offset = DRM_PANTHOR_USER_MMIO_OFFSET;
1411 
1412 #ifdef CONFIG_ARM64
1413 	/*
1414 	 * With 32-bit systems being limited by the 32-bit representation of
1415 	 * mmap2's pgoffset field, we need to make the MMIO offset arch
1416 	 * specific.
1417 	 */
1418 	if (test_tsk_thread_flag(current, TIF_32BIT))
1419 		pfile->user_mmio.offset = DRM_PANTHOR_USER_MMIO_OFFSET_32BIT;
1420 #endif
1421 
1422 
1423 	ret = panthor_vm_pool_create(pfile);
1424 	if (ret)
1425 		goto err_free_file;
1426 
1427 	ret = panthor_group_pool_create(pfile);
1428 	if (ret)
1429 		goto err_destroy_vm_pool;
1430 
1431 	file->driver_priv = pfile;
1432 	return 0;
1433 
1434 err_destroy_vm_pool:
1435 	panthor_vm_pool_destroy(pfile);
1436 
1437 err_free_file:
1438 	kfree(pfile);
1439 	return ret;
1440 }
1441 
1442 static void
1443 panthor_postclose(struct drm_device *ddev, struct drm_file *file)
1444 {
1445 	struct panthor_file *pfile = file->driver_priv;
1446 
1447 	panthor_group_pool_destroy(pfile);
1448 	panthor_vm_pool_destroy(pfile);
1449 
1450 	kfree(pfile);
1451 }
1452 
1453 static const struct drm_ioctl_desc panthor_drm_driver_ioctls[] = {
1454 #define PANTHOR_IOCTL(n, func, flags) \
1455 	DRM_IOCTL_DEF_DRV(PANTHOR_##n, panthor_ioctl_##func, flags)
1456 
1457 	PANTHOR_IOCTL(DEV_QUERY, dev_query, DRM_RENDER_ALLOW),
1458 	PANTHOR_IOCTL(VM_CREATE, vm_create, DRM_RENDER_ALLOW),
1459 	PANTHOR_IOCTL(VM_DESTROY, vm_destroy, DRM_RENDER_ALLOW),
1460 	PANTHOR_IOCTL(VM_BIND, vm_bind, DRM_RENDER_ALLOW),
1461 	PANTHOR_IOCTL(VM_GET_STATE, vm_get_state, DRM_RENDER_ALLOW),
1462 	PANTHOR_IOCTL(BO_CREATE, bo_create, DRM_RENDER_ALLOW),
1463 	PANTHOR_IOCTL(BO_MMAP_OFFSET, bo_mmap_offset, DRM_RENDER_ALLOW),
1464 	PANTHOR_IOCTL(GROUP_CREATE, group_create, DRM_RENDER_ALLOW),
1465 	PANTHOR_IOCTL(GROUP_DESTROY, group_destroy, DRM_RENDER_ALLOW),
1466 	PANTHOR_IOCTL(GROUP_GET_STATE, group_get_state, DRM_RENDER_ALLOW),
1467 	PANTHOR_IOCTL(TILER_HEAP_CREATE, tiler_heap_create, DRM_RENDER_ALLOW),
1468 	PANTHOR_IOCTL(TILER_HEAP_DESTROY, tiler_heap_destroy, DRM_RENDER_ALLOW),
1469 	PANTHOR_IOCTL(GROUP_SUBMIT, group_submit, DRM_RENDER_ALLOW),
1470 	PANTHOR_IOCTL(BO_SET_LABEL, bo_set_label, DRM_RENDER_ALLOW),
1471 	PANTHOR_IOCTL(SET_USER_MMIO_OFFSET, set_user_mmio_offset, DRM_RENDER_ALLOW),
1472 };
1473 
1474 static int panthor_mmap(struct file *filp, struct vm_area_struct *vma)
1475 {
1476 	struct drm_file *file = filp->private_data;
1477 	struct panthor_file *pfile = file->driver_priv;
1478 	struct panthor_device *ptdev = pfile->ptdev;
1479 	u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT;
1480 	u64 user_mmio_offset;
1481 	int ret, cookie;
1482 
1483 	if (!drm_dev_enter(file->minor->dev, &cookie))
1484 		return -ENODEV;
1485 
1486 	/* Adjust the user MMIO offset to match the offset used kernel side.
1487 	 * We use a local variable with a READ_ONCE() here to make sure
1488 	 * the user_mmio_offset we use for the is_user_mmio_mapping() check
1489 	 * hasn't changed when we do the offset adjustment.
1490 	 */
1491 	user_mmio_offset = READ_ONCE(pfile->user_mmio.offset);
1492 	if (offset >= user_mmio_offset) {
1493 		offset -= user_mmio_offset;
1494 		offset += DRM_PANTHOR_USER_MMIO_OFFSET;
1495 		vma->vm_pgoff = offset >> PAGE_SHIFT;
1496 		ret = panthor_device_mmap_io(ptdev, vma);
1497 	} else {
1498 		ret = drm_gem_mmap(filp, vma);
1499 	}
1500 
1501 	drm_dev_exit(cookie);
1502 	return ret;
1503 }
1504 
1505 static void panthor_gpu_show_fdinfo(struct panthor_device *ptdev,
1506 				    struct panthor_file *pfile,
1507 				    struct drm_printer *p)
1508 {
1509 	if (ptdev->profile_mask & PANTHOR_DEVICE_PROFILING_ALL)
1510 		panthor_fdinfo_gather_group_samples(pfile);
1511 
1512 	if (ptdev->profile_mask & PANTHOR_DEVICE_PROFILING_TIMESTAMP) {
1513 #ifdef CONFIG_ARM_ARCH_TIMER
1514 		drm_printf(p, "drm-engine-panthor:\t%llu ns\n",
1515 			   DIV_ROUND_UP_ULL((pfile->stats.time * NSEC_PER_SEC),
1516 					    arch_timer_get_cntfrq()));
1517 #endif
1518 	}
1519 	if (ptdev->profile_mask & PANTHOR_DEVICE_PROFILING_CYCLES)
1520 		drm_printf(p, "drm-cycles-panthor:\t%llu\n", pfile->stats.cycles);
1521 
1522 	drm_printf(p, "drm-maxfreq-panthor:\t%lu Hz\n", ptdev->fast_rate);
1523 	drm_printf(p, "drm-curfreq-panthor:\t%lu Hz\n", ptdev->current_frequency);
1524 }
1525 
1526 static void panthor_show_internal_memory_stats(struct drm_printer *p, struct drm_file *file)
1527 {
1528 	char *drv_name = file->minor->dev->driver->name;
1529 	struct panthor_file *pfile = file->driver_priv;
1530 	struct drm_memory_stats stats = {0};
1531 
1532 	panthor_fdinfo_gather_group_mem_info(pfile, &stats);
1533 	panthor_vm_heaps_sizes(pfile, &stats);
1534 
1535 	drm_fdinfo_print_size(p, drv_name, "resident", "memory", stats.resident);
1536 	drm_fdinfo_print_size(p, drv_name, "active", "memory", stats.active);
1537 }
1538 
1539 static void panthor_show_fdinfo(struct drm_printer *p, struct drm_file *file)
1540 {
1541 	struct drm_device *dev = file->minor->dev;
1542 	struct panthor_device *ptdev = container_of(dev, struct panthor_device, base);
1543 
1544 	panthor_gpu_show_fdinfo(ptdev, file->driver_priv, p);
1545 	panthor_show_internal_memory_stats(p, file);
1546 
1547 	drm_show_memory_stats(p, file);
1548 }
1549 
1550 static const struct file_operations panthor_drm_driver_fops = {
1551 	.owner = THIS_MODULE,
1552 	.open = drm_open,
1553 	.release = drm_release,
1554 	.unlocked_ioctl = drm_ioctl,
1555 	.compat_ioctl = drm_compat_ioctl,
1556 	.poll = drm_poll,
1557 	.read = drm_read,
1558 	.llseek = noop_llseek,
1559 	.mmap = panthor_mmap,
1560 	.show_fdinfo = drm_show_fdinfo,
1561 	.fop_flags = FOP_UNSIGNED_OFFSET,
1562 };
1563 
1564 #ifdef CONFIG_DEBUG_FS
1565 static int panthor_gems_show(struct seq_file *m, void *data)
1566 {
1567 	struct drm_info_node *node = m->private;
1568 	struct drm_device *dev = node->minor->dev;
1569 	struct panthor_device *ptdev = container_of(dev, struct panthor_device, base);
1570 
1571 	panthor_gem_debugfs_print_bos(ptdev, m);
1572 
1573 	return 0;
1574 }
1575 
1576 static struct drm_info_list panthor_debugfs_list[] = {
1577 	{"gems", panthor_gems_show, 0, NULL},
1578 };
1579 
1580 static int panthor_gems_debugfs_init(struct drm_minor *minor)
1581 {
1582 	drm_debugfs_create_files(panthor_debugfs_list,
1583 				 ARRAY_SIZE(panthor_debugfs_list),
1584 				 minor->debugfs_root, minor);
1585 
1586 	return 0;
1587 }
1588 
1589 static void panthor_debugfs_init(struct drm_minor *minor)
1590 {
1591 	panthor_mmu_debugfs_init(minor);
1592 	panthor_gems_debugfs_init(minor);
1593 }
1594 #endif
1595 
1596 /*
1597  * PanCSF driver version:
1598  * - 1.0 - initial interface
1599  * - 1.1 - adds DEV_QUERY_TIMESTAMP_INFO query
1600  * - 1.2 - adds DEV_QUERY_GROUP_PRIORITIES_INFO query
1601  *       - adds PANTHOR_GROUP_PRIORITY_REALTIME priority
1602  * - 1.3 - adds DRM_PANTHOR_GROUP_STATE_INNOCENT flag
1603  * - 1.4 - adds DRM_IOCTL_PANTHOR_BO_SET_LABEL ioctl
1604  * - 1.5 - adds DRM_PANTHOR_SET_USER_MMIO_OFFSET ioctl
1605  */
1606 static const struct drm_driver panthor_drm_driver = {
1607 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
1608 			   DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA,
1609 	.open = panthor_open,
1610 	.postclose = panthor_postclose,
1611 	.show_fdinfo = panthor_show_fdinfo,
1612 	.ioctls = panthor_drm_driver_ioctls,
1613 	.num_ioctls = ARRAY_SIZE(panthor_drm_driver_ioctls),
1614 	.fops = &panthor_drm_driver_fops,
1615 	.name = "panthor",
1616 	.desc = "Panthor DRM driver",
1617 	.major = 1,
1618 	.minor = 5,
1619 
1620 	.gem_create_object = panthor_gem_create_object,
1621 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
1622 #ifdef CONFIG_DEBUG_FS
1623 	.debugfs_init = panthor_debugfs_init,
1624 #endif
1625 };
1626 
1627 static int panthor_probe(struct platform_device *pdev)
1628 {
1629 	struct panthor_device *ptdev;
1630 
1631 	ptdev = devm_drm_dev_alloc(&pdev->dev, &panthor_drm_driver,
1632 				   struct panthor_device, base);
1633 	if (IS_ERR(ptdev))
1634 		return -ENOMEM;
1635 
1636 	platform_set_drvdata(pdev, ptdev);
1637 
1638 	return panthor_device_init(ptdev);
1639 }
1640 
1641 static void panthor_remove(struct platform_device *pdev)
1642 {
1643 	struct panthor_device *ptdev = platform_get_drvdata(pdev);
1644 
1645 	panthor_device_unplug(ptdev);
1646 }
1647 
1648 static ssize_t profiling_show(struct device *dev,
1649 			      struct device_attribute *attr,
1650 			      char *buf)
1651 {
1652 	struct panthor_device *ptdev = dev_get_drvdata(dev);
1653 
1654 	return sysfs_emit(buf, "%d\n", ptdev->profile_mask);
1655 }
1656 
1657 static ssize_t profiling_store(struct device *dev,
1658 			       struct device_attribute *attr,
1659 			       const char *buf, size_t len)
1660 {
1661 	struct panthor_device *ptdev = dev_get_drvdata(dev);
1662 	u32 value;
1663 	int err;
1664 
1665 	err = kstrtou32(buf, 0, &value);
1666 	if (err)
1667 		return err;
1668 
1669 	if ((value & ~PANTHOR_DEVICE_PROFILING_ALL) != 0)
1670 		return -EINVAL;
1671 
1672 	ptdev->profile_mask = value;
1673 
1674 	return len;
1675 }
1676 
1677 static DEVICE_ATTR_RW(profiling);
1678 
1679 static struct attribute *panthor_attrs[] = {
1680 	&dev_attr_profiling.attr,
1681 	NULL,
1682 };
1683 
1684 ATTRIBUTE_GROUPS(panthor);
1685 
1686 static const struct panthor_soc_data soc_data_mediatek_mt8196 = {
1687 	.asn_hash_enable = true,
1688 	.asn_hash = { 0xb, 0xe, 0x0, },
1689 };
1690 
1691 static const struct of_device_id dt_match[] = {
1692 	{ .compatible = "mediatek,mt8196-mali", .data = &soc_data_mediatek_mt8196, },
1693 	{ .compatible = "rockchip,rk3588-mali" },
1694 	{ .compatible = "arm,mali-valhall-csf" },
1695 	{}
1696 };
1697 MODULE_DEVICE_TABLE(of, dt_match);
1698 
1699 static DEFINE_RUNTIME_DEV_PM_OPS(panthor_pm_ops,
1700 				 panthor_device_suspend,
1701 				 panthor_device_resume,
1702 				 NULL);
1703 
1704 static struct platform_driver panthor_driver = {
1705 	.probe = panthor_probe,
1706 	.remove = panthor_remove,
1707 	.driver = {
1708 		.name = "panthor",
1709 		.pm = pm_ptr(&panthor_pm_ops),
1710 		.of_match_table = dt_match,
1711 		.dev_groups = panthor_groups,
1712 	},
1713 };
1714 
1715 /*
1716  * Workqueue used to cleanup stuff.
1717  *
1718  * We create a dedicated workqueue so we can drain on unplug and
1719  * make sure all resources are freed before the module is unloaded.
1720  */
1721 struct workqueue_struct *panthor_cleanup_wq;
1722 
1723 static int __init panthor_init(void)
1724 {
1725 	int ret;
1726 
1727 	ret = panthor_mmu_pt_cache_init();
1728 	if (ret)
1729 		return ret;
1730 
1731 	panthor_cleanup_wq = alloc_workqueue("panthor-cleanup", WQ_UNBOUND, 0);
1732 	if (!panthor_cleanup_wq) {
1733 		pr_err("panthor: Failed to allocate the workqueues");
1734 		ret = -ENOMEM;
1735 		goto err_mmu_pt_cache_fini;
1736 	}
1737 
1738 	ret = platform_driver_register(&panthor_driver);
1739 	if (ret)
1740 		goto err_destroy_cleanup_wq;
1741 
1742 	return 0;
1743 
1744 err_destroy_cleanup_wq:
1745 	destroy_workqueue(panthor_cleanup_wq);
1746 
1747 err_mmu_pt_cache_fini:
1748 	panthor_mmu_pt_cache_fini();
1749 	return ret;
1750 }
1751 module_init(panthor_init);
1752 
1753 static void __exit panthor_exit(void)
1754 {
1755 	platform_driver_unregister(&panthor_driver);
1756 	destroy_workqueue(panthor_cleanup_wq);
1757 	panthor_mmu_pt_cache_fini();
1758 }
1759 module_exit(panthor_exit);
1760 
1761 MODULE_AUTHOR("Panthor Project Developers");
1762 MODULE_DESCRIPTION("Panthor DRM Driver");
1763 MODULE_LICENSE("Dual MIT/GPL");
1764