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