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