1 // SPDX-License-Identifier: GPL-2.0 or MIT
4 /* Copyright 2019 Collabora ltd. */
6 #include <linux/list.h>
7 #include <linux/module.h>
8 #include <linux/of_platform.h>
9 #include <linux/pagemap.h>
10 #include <linux/platform_device.h>
11 #include <linux/pm_runtime.h>
13 #include <drm/drm_debugfs.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_exec.h>
16 #include <drm/drm_ioctl.h>
17 #include <drm/drm_syncobj.h>
18 #include <drm/drm_utils.h>
19 #include <drm/gpu_scheduler.h>
20 #include <drm/panthor_drm.h>
22 #include "panthor_device.h"
23 #include "panthor_fw.h"
24 #include "panthor_gem.h"
25 #include "panthor_gpu.h"
26 #include "panthor_heap.h"
27 #include "panthor_mmu.h"
28 #include "panthor_regs.h"
29 #include "panthor_sched.h"
32 * DOC: user <-> kernel object copy helpers.
36 * panthor_set_uobj() - Copy kernel object to user object.
37 * @usr_ptr: Users pointer.
38 * @usr_size: Size of the user object.
39 * @min_size: Minimum size for this object.
40 * @kern_size: Size of the kernel object.
41 * @in: Address of the kernel object to copy.
43 * Helper automating kernel -> user object copies.
45 * Don't use this function directly, use PANTHOR_UOBJ_SET() instead.
47 * Return: 0 on success, a negative error code otherwise.
50 panthor_set_uobj(u64 usr_ptr, u32 usr_size, u32 min_size, u32 kern_size, const void *in)
52 /* User size shouldn't be smaller than the minimal object size. */
53 if (usr_size < min_size)
56 if (copy_to_user(u64_to_user_ptr(usr_ptr), in, min_t(u32, usr_size, kern_size)))
59 /* When the kernel object is smaller than the user object, we fill the gap with
62 if (usr_size > kern_size &&
63 clear_user(u64_to_user_ptr(usr_ptr + kern_size), usr_size - kern_size)) {
71 * panthor_get_uobj_array() - Copy a user object array into a kernel accessible object array.
72 * @in: The object array to copy.
73 * @min_stride: Minimum array stride.
74 * @obj_size: Kernel object size.
76 * Helper automating user -> kernel object copies.
78 * Don't use this function directly, use PANTHOR_UOBJ_GET_ARRAY() instead.
80 * Return: newly allocated object array or an ERR_PTR on error.
83 panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride,
89 /* User stride must be at least the minimum object size, otherwise it might
90 * lack useful information.
92 if (in->stride < min_stride)
93 return ERR_PTR(-EINVAL);
98 out_alloc = kvmalloc_array(in->count, obj_size, GFP_KERNEL);
100 return ERR_PTR(-ENOMEM);
102 if (obj_size == in->stride) {
103 /* Fast path when user/kernel have the same uAPI header version. */
104 if (copy_from_user(out_alloc, u64_to_user_ptr(in->array),
105 (unsigned long)obj_size * in->count))
108 void __user *in_ptr = u64_to_user_ptr(in->array);
109 void *out_ptr = out_alloc;
111 /* If the sizes differ, we need to copy elements one by one. */
112 for (u32 i = 0; i < in->count; i++) {
113 ret = copy_struct_from_user(out_ptr, obj_size, in_ptr, in->stride);
118 in_ptr += in->stride;
131 * PANTHOR_UOBJ_MIN_SIZE_INTERNAL() - Get the minimum user object size
132 * @_typename: Object type.
133 * @_last_mandatory_field: Last mandatory field.
135 * Get the minimum user object size based on the last mandatory field name,
136 * A.K.A, the name of the last field of the structure at the time this
137 * structure was added to the uAPI.
139 * Don't use directly, use PANTHOR_UOBJ_DECL() instead.
141 #define PANTHOR_UOBJ_MIN_SIZE_INTERNAL(_typename, _last_mandatory_field) \
142 (offsetof(_typename, _last_mandatory_field) + \
143 sizeof(((_typename *)NULL)->_last_mandatory_field))
146 * PANTHOR_UOBJ_DECL() - Declare a new uAPI object whose subject to
148 * @_typename: Object type.
149 * @_last_mandatory_field: Last mandatory field.
151 * Should be used to extend the PANTHOR_UOBJ_MIN_SIZE() list.
153 #define PANTHOR_UOBJ_DECL(_typename, _last_mandatory_field) \
154 _typename : PANTHOR_UOBJ_MIN_SIZE_INTERNAL(_typename, _last_mandatory_field)
157 * PANTHOR_UOBJ_MIN_SIZE() - Get the minimum size of a given uAPI object
158 * @_obj_name: Object to get the minimum size of.
160 * Don't use this macro directly, it's automatically called by
161 * PANTHOR_UOBJ_{SET,GET_ARRAY}().
163 #define PANTHOR_UOBJ_MIN_SIZE(_obj_name) \
164 _Generic(_obj_name, \
165 PANTHOR_UOBJ_DECL(struct drm_panthor_gpu_info, tiler_present), \
166 PANTHOR_UOBJ_DECL(struct drm_panthor_csif_info, pad), \
167 PANTHOR_UOBJ_DECL(struct drm_panthor_sync_op, timeline_value), \
168 PANTHOR_UOBJ_DECL(struct drm_panthor_queue_submit, syncs), \
169 PANTHOR_UOBJ_DECL(struct drm_panthor_queue_create, ringbuf_size), \
170 PANTHOR_UOBJ_DECL(struct drm_panthor_vm_bind_op, syncs))
173 * PANTHOR_UOBJ_SET() - Copy a kernel object to a user object.
174 * @_dest_usr_ptr: User pointer to copy to.
175 * @_usr_size: Size of the user object.
176 * @_src_obj: Kernel object to copy (not a pointer).
178 * Return: 0 on success, a negative error code otherwise.
180 #define PANTHOR_UOBJ_SET(_dest_usr_ptr, _usr_size, _src_obj) \
181 panthor_set_uobj(_dest_usr_ptr, _usr_size, \
182 PANTHOR_UOBJ_MIN_SIZE(_src_obj), \
183 sizeof(_src_obj), &(_src_obj))
186 * PANTHOR_UOBJ_GET_ARRAY() - Copy a user object array to a kernel accessible
188 * @_dest_array: Local variable that will hold the newly allocated kernel
190 * @_uobj_array: The drm_panthor_obj_array object describing the user object
193 * Return: 0 on success, a negative error code otherwise.
195 #define PANTHOR_UOBJ_GET_ARRAY(_dest_array, _uobj_array) \
197 typeof(_dest_array) _tmp; \
198 _tmp = panthor_get_uobj_array(_uobj_array, \
199 PANTHOR_UOBJ_MIN_SIZE((_dest_array)[0]), \
200 sizeof((_dest_array)[0])); \
202 _dest_array = _tmp; \
203 PTR_ERR_OR_ZERO(_tmp); \
207 * struct panthor_sync_signal - Represent a synchronization object point to attach
210 * This structure is here to keep track of fences that are currently bound to
211 * a specific syncobj point.
213 * At the beginning of a job submission, the fence
214 * is retrieved from the syncobj itself, and can be NULL if no fence was attached
217 * At the end, it points to the fence of the last job that had a
218 * %DRM_PANTHOR_SYNC_OP_SIGNAL on this syncobj.
220 * With jobs being submitted in batches, the fence might change several times during
221 * the process, allowing one job to wait on a job that's part of the same submission
222 * but appears earlier in the drm_panthor_group_submit::queue_submits array.
224 struct panthor_sync_signal {
225 /** @node: list_head to track signal ops within a submit operation */
226 struct list_head node;
228 /** @handle: The syncobj handle. */
232 * @point: The syncobj point.
234 * Zero for regular syncobjs, and non-zero for timeline syncobjs.
239 * @syncobj: The sync object pointed by @handle.
241 struct drm_syncobj *syncobj;
244 * @chain: Chain object used to link the new fence to an existing
247 * NULL for regular syncobj, non-NULL for timeline syncobjs.
249 struct dma_fence_chain *chain;
252 * @fence: The fence to assign to the syncobj or syncobj-point.
254 struct dma_fence *fence;
258 * struct panthor_job_ctx - Job context
260 struct panthor_job_ctx {
261 /** @job: The job that is about to be submitted to drm_sched. */
262 struct drm_sched_job *job;
264 /** @syncops: Array of sync operations. */
265 struct drm_panthor_sync_op *syncops;
267 /** @syncop_count: Number of sync operations. */
272 * struct panthor_submit_ctx - Submission context
274 * Anything that's related to a submission (%DRM_IOCTL_PANTHOR_VM_BIND or
275 * %DRM_IOCTL_PANTHOR_GROUP_SUBMIT) is kept here, so we can automate the
276 * initialization and cleanup steps.
278 struct panthor_submit_ctx {
279 /** @file: DRM file this submission happens on. */
280 struct drm_file *file;
283 * @signals: List of struct panthor_sync_signal.
285 * %DRM_PANTHOR_SYNC_OP_SIGNAL operations will be recorded here,
286 * and %DRM_PANTHOR_SYNC_OP_WAIT will first check if an entry
287 * matching the syncobj+point exists before calling
288 * drm_syncobj_find_fence(). This allows us to describe dependencies
289 * existing between jobs that are part of the same batch.
291 struct list_head signals;
293 /** @jobs: Array of jobs. */
294 struct panthor_job_ctx *jobs;
296 /** @job_count: Number of entries in the @jobs array. */
299 /** @exec: drm_exec context used to acquire and prepare resv objects. */
300 struct drm_exec exec;
303 #define PANTHOR_SYNC_OP_FLAGS_MASK \
304 (DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_MASK | DRM_PANTHOR_SYNC_OP_SIGNAL)
306 static bool sync_op_is_signal(const struct drm_panthor_sync_op *sync_op)
308 return !!(sync_op->flags & DRM_PANTHOR_SYNC_OP_SIGNAL);
311 static bool sync_op_is_wait(const struct drm_panthor_sync_op *sync_op)
313 /* Note that DRM_PANTHOR_SYNC_OP_WAIT == 0 */
314 return !(sync_op->flags & DRM_PANTHOR_SYNC_OP_SIGNAL);
318 * panthor_check_sync_op() - Check drm_panthor_sync_op fields
319 * @sync_op: The sync operation to check.
321 * Return: 0 on success, -EINVAL otherwise.
324 panthor_check_sync_op(const struct drm_panthor_sync_op *sync_op)
328 if (sync_op->flags & ~PANTHOR_SYNC_OP_FLAGS_MASK)
331 handle_type = sync_op->flags & DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_MASK;
332 if (handle_type != DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ &&
333 handle_type != DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ)
336 if (handle_type == DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ &&
337 sync_op->timeline_value != 0)
344 * panthor_sync_signal_free() - Release resources and free a panthor_sync_signal object
345 * @sig_sync: Signal object to free.
348 panthor_sync_signal_free(struct panthor_sync_signal *sig_sync)
353 drm_syncobj_put(sig_sync->syncobj);
354 dma_fence_chain_free(sig_sync->chain);
355 dma_fence_put(sig_sync->fence);
360 * panthor_submit_ctx_add_sync_signal() - Add a signal operation to a submit context
361 * @ctx: Context to add the signal operation to.
362 * @handle: Syncobj handle.
363 * @point: Syncobj point.
365 * Return: 0 on success, otherwise negative error value.
368 panthor_submit_ctx_add_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
370 struct panthor_sync_signal *sig_sync;
371 struct dma_fence *cur_fence;
374 sig_sync = kzalloc(sizeof(*sig_sync), GFP_KERNEL);
378 sig_sync->handle = handle;
379 sig_sync->point = point;
382 sig_sync->chain = dma_fence_chain_alloc();
383 if (!sig_sync->chain) {
385 goto err_free_sig_sync;
389 sig_sync->syncobj = drm_syncobj_find(ctx->file, handle);
390 if (!sig_sync->syncobj) {
392 goto err_free_sig_sync;
395 /* Retrieve the current fence attached to that point. It's
396 * perfectly fine to get a NULL fence here, it just means there's
397 * no fence attached to that point yet.
399 if (!drm_syncobj_find_fence(ctx->file, handle, point, 0, &cur_fence))
400 sig_sync->fence = cur_fence;
402 list_add_tail(&sig_sync->node, &ctx->signals);
407 panthor_sync_signal_free(sig_sync);
412 * panthor_submit_ctx_search_sync_signal() - Search an existing signal operation in a
414 * @ctx: Context to search the signal operation in.
415 * @handle: Syncobj handle.
416 * @point: Syncobj point.
418 * Return: A valid panthor_sync_signal object if found, NULL otherwise.
420 static struct panthor_sync_signal *
421 panthor_submit_ctx_search_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
423 struct panthor_sync_signal *sig_sync;
425 list_for_each_entry(sig_sync, &ctx->signals, node) {
426 if (handle == sig_sync->handle && point == sig_sync->point)
434 * panthor_submit_ctx_add_job() - Add a job to a submit context
435 * @ctx: Context to search the signal operation in.
436 * @idx: Index of the job in the context.
438 * @syncs: Sync operations provided by userspace.
440 * Return: 0 on success, a negative error code otherwise.
443 panthor_submit_ctx_add_job(struct panthor_submit_ctx *ctx, u32 idx,
444 struct drm_sched_job *job,
445 const struct drm_panthor_obj_array *syncs)
449 ctx->jobs[idx].job = job;
451 ret = PANTHOR_UOBJ_GET_ARRAY(ctx->jobs[idx].syncops, syncs);
455 ctx->jobs[idx].syncop_count = syncs->count;
460 * panthor_submit_ctx_get_sync_signal() - Search signal operation and add one if none was found.
461 * @ctx: Context to search the signal operation in.
462 * @handle: Syncobj handle.
463 * @point: Syncobj point.
465 * Return: 0 on success, a negative error code otherwise.
468 panthor_submit_ctx_get_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
470 struct panthor_sync_signal *sig_sync;
472 sig_sync = panthor_submit_ctx_search_sync_signal(ctx, handle, point);
476 return panthor_submit_ctx_add_sync_signal(ctx, handle, point);
480 * panthor_submit_ctx_update_job_sync_signal_fences() - Update fences
481 * on the signal operations specified by a job.
482 * @ctx: Context to search the signal operation in.
483 * @job_idx: Index of the job to operate on.
485 * Return: 0 on success, a negative error code otherwise.
488 panthor_submit_ctx_update_job_sync_signal_fences(struct panthor_submit_ctx *ctx,
491 struct panthor_device *ptdev = container_of(ctx->file->minor->dev,
492 struct panthor_device,
494 struct dma_fence *done_fence = &ctx->jobs[job_idx].job->s_fence->finished;
495 const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
496 u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
498 for (u32 i = 0; i < sync_op_count; i++) {
499 struct dma_fence *old_fence;
500 struct panthor_sync_signal *sig_sync;
502 if (!sync_op_is_signal(&sync_ops[i]))
505 sig_sync = panthor_submit_ctx_search_sync_signal(ctx, sync_ops[i].handle,
506 sync_ops[i].timeline_value);
507 if (drm_WARN_ON(&ptdev->base, !sig_sync))
510 old_fence = sig_sync->fence;
511 sig_sync->fence = dma_fence_get(done_fence);
512 dma_fence_put(old_fence);
514 if (drm_WARN_ON(&ptdev->base, !sig_sync->fence))
522 * panthor_submit_ctx_collect_job_signal_ops() - Iterate over all job signal operations
523 * and add them to the context.
524 * @ctx: Context to search the signal operation in.
525 * @job_idx: Index of the job to operate on.
527 * Return: 0 on success, a negative error code otherwise.
530 panthor_submit_ctx_collect_job_signal_ops(struct panthor_submit_ctx *ctx,
533 const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
534 u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
536 for (u32 i = 0; i < sync_op_count; i++) {
539 if (!sync_op_is_signal(&sync_ops[i]))
542 ret = panthor_check_sync_op(&sync_ops[i]);
546 ret = panthor_submit_ctx_get_sync_signal(ctx,
548 sync_ops[i].timeline_value);
557 * panthor_submit_ctx_push_fences() - Iterate over the signal array, and for each entry, push
558 * the currently assigned fence to the associated syncobj.
559 * @ctx: Context to push fences on.
561 * This is the last step of a submission procedure, and is done once we know the submission
562 * is effective and job fences are guaranteed to be signaled in finite time.
565 panthor_submit_ctx_push_fences(struct panthor_submit_ctx *ctx)
567 struct panthor_sync_signal *sig_sync;
569 list_for_each_entry(sig_sync, &ctx->signals, node) {
570 if (sig_sync->chain) {
571 drm_syncobj_add_point(sig_sync->syncobj, sig_sync->chain,
572 sig_sync->fence, sig_sync->point);
573 sig_sync->chain = NULL;
575 drm_syncobj_replace_fence(sig_sync->syncobj, sig_sync->fence);
581 * panthor_submit_ctx_add_sync_deps_to_job() - Add sync wait operations as
583 * @ctx: Submit context.
584 * @job_idx: Index of the job to operate on.
586 * Return: 0 on success, a negative error code otherwise.
589 panthor_submit_ctx_add_sync_deps_to_job(struct panthor_submit_ctx *ctx,
592 struct panthor_device *ptdev = container_of(ctx->file->minor->dev,
593 struct panthor_device,
595 const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
596 struct drm_sched_job *job = ctx->jobs[job_idx].job;
597 u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
600 for (u32 i = 0; i < sync_op_count; i++) {
601 struct panthor_sync_signal *sig_sync;
602 struct dma_fence *fence;
604 if (!sync_op_is_wait(&sync_ops[i]))
607 ret = panthor_check_sync_op(&sync_ops[i]);
611 sig_sync = panthor_submit_ctx_search_sync_signal(ctx, sync_ops[i].handle,
612 sync_ops[i].timeline_value);
614 if (drm_WARN_ON(&ptdev->base, !sig_sync->fence))
617 fence = dma_fence_get(sig_sync->fence);
619 ret = drm_syncobj_find_fence(ctx->file, sync_ops[i].handle,
620 sync_ops[i].timeline_value,
626 ret = drm_sched_job_add_dependency(job, fence);
635 * panthor_submit_ctx_collect_jobs_signal_ops() - Collect all signal operations
636 * and add them to the submit context.
637 * @ctx: Submit context.
639 * Return: 0 on success, a negative error code otherwise.
642 panthor_submit_ctx_collect_jobs_signal_ops(struct panthor_submit_ctx *ctx)
644 for (u32 i = 0; i < ctx->job_count; i++) {
647 ret = panthor_submit_ctx_collect_job_signal_ops(ctx, i);
656 * panthor_submit_ctx_add_deps_and_arm_jobs() - Add jobs dependencies and arm jobs
657 * @ctx: Submit context.
659 * Must be called after the resv preparation has been taken care of.
661 * Return: 0 on success, a negative error code otherwise.
664 panthor_submit_ctx_add_deps_and_arm_jobs(struct panthor_submit_ctx *ctx)
666 for (u32 i = 0; i < ctx->job_count; i++) {
669 ret = panthor_submit_ctx_add_sync_deps_to_job(ctx, i);
673 drm_sched_job_arm(ctx->jobs[i].job);
675 ret = panthor_submit_ctx_update_job_sync_signal_fences(ctx, i);
684 * panthor_submit_ctx_push_jobs() - Push jobs to their scheduling entities.
685 * @ctx: Submit context.
686 * @upd_resvs: Callback used to update reservation objects that were previously
690 panthor_submit_ctx_push_jobs(struct panthor_submit_ctx *ctx,
691 void (*upd_resvs)(struct drm_exec *, struct drm_sched_job *))
693 for (u32 i = 0; i < ctx->job_count; i++) {
694 upd_resvs(&ctx->exec, ctx->jobs[i].job);
695 drm_sched_entity_push_job(ctx->jobs[i].job);
697 /* Job is owned by the scheduler now. */
698 ctx->jobs[i].job = NULL;
701 panthor_submit_ctx_push_fences(ctx);
705 * panthor_submit_ctx_init() - Initializes a submission context
706 * @ctx: Submit context to initialize.
707 * @file: drm_file this submission happens on.
708 * @job_count: Number of jobs that will be submitted.
710 * Return: 0 on success, a negative error code otherwise.
712 static int panthor_submit_ctx_init(struct panthor_submit_ctx *ctx,
713 struct drm_file *file, u32 job_count)
715 ctx->jobs = kvmalloc_array(job_count, sizeof(*ctx->jobs),
716 GFP_KERNEL | __GFP_ZERO);
721 ctx->job_count = job_count;
722 INIT_LIST_HEAD(&ctx->signals);
723 drm_exec_init(&ctx->exec,
724 DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES,
730 * panthor_submit_ctx_cleanup() - Cleanup a submission context
731 * @ctx: Submit context to cleanup.
732 * @job_put: Job put callback.
734 static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
735 void (*job_put)(struct drm_sched_job *))
737 struct panthor_sync_signal *sig_sync, *tmp;
740 drm_exec_fini(&ctx->exec);
742 list_for_each_entry_safe(sig_sync, tmp, &ctx->signals, node)
743 panthor_sync_signal_free(sig_sync);
745 for (i = 0; i < ctx->job_count; i++) {
746 job_put(ctx->jobs[i].job);
747 kvfree(ctx->jobs[i].syncops);
753 static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct drm_file *file)
755 struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
756 struct drm_panthor_dev_query *args = data;
758 if (!args->pointer) {
759 switch (args->type) {
760 case DRM_PANTHOR_DEV_QUERY_GPU_INFO:
761 args->size = sizeof(ptdev->gpu_info);
764 case DRM_PANTHOR_DEV_QUERY_CSIF_INFO:
765 args->size = sizeof(ptdev->csif_info);
773 switch (args->type) {
774 case DRM_PANTHOR_DEV_QUERY_GPU_INFO:
775 return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->gpu_info);
777 case DRM_PANTHOR_DEV_QUERY_CSIF_INFO:
778 return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
785 #define PANTHOR_VM_CREATE_FLAGS 0
787 static int panthor_ioctl_vm_create(struct drm_device *ddev, void *data,
788 struct drm_file *file)
790 struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
791 struct panthor_file *pfile = file->driver_priv;
792 struct drm_panthor_vm_create *args = data;
795 if (!drm_dev_enter(ddev, &cookie))
798 ret = panthor_vm_pool_create_vm(ptdev, pfile->vms, args);
804 drm_dev_exit(cookie);
808 static int panthor_ioctl_vm_destroy(struct drm_device *ddev, void *data,
809 struct drm_file *file)
811 struct panthor_file *pfile = file->driver_priv;
812 struct drm_panthor_vm_destroy *args = data;
817 return panthor_vm_pool_destroy_vm(pfile->vms, args->id);
820 #define PANTHOR_BO_FLAGS DRM_PANTHOR_BO_NO_MMAP
822 static int panthor_ioctl_bo_create(struct drm_device *ddev, void *data,
823 struct drm_file *file)
825 struct panthor_file *pfile = file->driver_priv;
826 struct drm_panthor_bo_create *args = data;
827 struct panthor_vm *vm = NULL;
830 if (!drm_dev_enter(ddev, &cookie))
833 if (!args->size || args->pad ||
834 (args->flags & ~PANTHOR_BO_FLAGS)) {
839 if (args->exclusive_vm_id) {
840 vm = panthor_vm_pool_get_vm(pfile->vms, args->exclusive_vm_id);
847 ret = panthor_gem_create_with_handle(file, ddev, vm, &args->size,
848 args->flags, &args->handle);
853 drm_dev_exit(cookie);
857 static int panthor_ioctl_bo_mmap_offset(struct drm_device *ddev, void *data,
858 struct drm_file *file)
860 struct drm_panthor_bo_mmap_offset *args = data;
861 struct drm_gem_object *obj;
867 obj = drm_gem_object_lookup(file, args->handle);
871 ret = drm_gem_create_mmap_offset(obj);
875 args->offset = drm_vma_node_offset_addr(&obj->vma_node);
878 drm_gem_object_put(obj);
882 static int panthor_ioctl_group_submit(struct drm_device *ddev, void *data,
883 struct drm_file *file)
885 struct panthor_file *pfile = file->driver_priv;
886 struct drm_panthor_group_submit *args = data;
887 struct drm_panthor_queue_submit *jobs_args;
888 struct panthor_submit_ctx ctx;
894 if (!drm_dev_enter(ddev, &cookie))
897 ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->queue_submits);
901 ret = panthor_submit_ctx_init(&ctx, file, args->queue_submits.count);
903 goto out_free_jobs_args;
905 /* Create jobs and attach sync operations */
906 for (u32 i = 0; i < args->queue_submits.count; i++) {
907 const struct drm_panthor_queue_submit *qsubmit = &jobs_args[i];
908 struct drm_sched_job *job;
910 job = panthor_job_create(pfile, args->group_handle, qsubmit);
913 goto out_cleanup_submit_ctx;
916 ret = panthor_submit_ctx_add_job(&ctx, i, job, &qsubmit->syncs);
918 goto out_cleanup_submit_ctx;
922 * Collect signal operations on all jobs, such that each job can pick
923 * from it for its dependencies and update the fence to signal when the
926 ret = panthor_submit_ctx_collect_jobs_signal_ops(&ctx);
928 goto out_cleanup_submit_ctx;
931 * We acquire/prepare revs on all jobs before proceeding with the
932 * dependency registration.
934 * This is solving two problems:
935 * 1. drm_sched_job_arm() and drm_sched_entity_push_job() must be
936 * protected by a lock to make sure no concurrent access to the same
937 * entity get interleaved, which would mess up with the fence seqno
938 * ordering. Luckily, one of the resv being acquired is the VM resv,
939 * and a scheduling entity is only bound to a single VM. As soon as
940 * we acquire the VM resv, we should be safe.
941 * 2. Jobs might depend on fences that were issued by previous jobs in
942 * the same batch, so we can't add dependencies on all jobs before
943 * arming previous jobs and registering the fence to the signal
944 * array, otherwise we might miss dependencies, or point to an
947 if (args->queue_submits.count > 0) {
948 /* All jobs target the same group, so they also point to the same VM. */
949 struct panthor_vm *vm = panthor_job_vm(ctx.jobs[0].job);
951 drm_exec_until_all_locked(&ctx.exec) {
952 ret = panthor_vm_prepare_mapped_bos_resvs(&ctx.exec, vm,
953 args->queue_submits.count);
957 goto out_cleanup_submit_ctx;
961 * Now that resvs are locked/prepared, we can iterate over each job to
962 * add the dependencies, arm the job fence, register the job fence to
965 ret = panthor_submit_ctx_add_deps_and_arm_jobs(&ctx);
967 goto out_cleanup_submit_ctx;
969 /* Nothing can fail after that point, so we can make our job fences
970 * visible to the outside world. Push jobs and set the job fences to
971 * the resv slots we reserved. This also pushes the fences to the
972 * syncobjs that are part of the signal array.
974 panthor_submit_ctx_push_jobs(&ctx, panthor_job_update_resvs);
976 out_cleanup_submit_ctx:
977 panthor_submit_ctx_cleanup(&ctx, panthor_job_put);
983 drm_dev_exit(cookie);
987 static int panthor_ioctl_group_destroy(struct drm_device *ddev, void *data,
988 struct drm_file *file)
990 struct panthor_file *pfile = file->driver_priv;
991 struct drm_panthor_group_destroy *args = data;
996 return panthor_group_destroy(pfile, args->group_handle);
999 static int panthor_ioctl_group_create(struct drm_device *ddev, void *data,
1000 struct drm_file *file)
1002 struct panthor_file *pfile = file->driver_priv;
1003 struct drm_panthor_group_create *args = data;
1004 struct drm_panthor_queue_create *queue_args;
1007 if (!args->queues.count)
1010 ret = PANTHOR_UOBJ_GET_ARRAY(queue_args, &args->queues);
1014 ret = panthor_group_create(pfile, args, queue_args);
1016 args->group_handle = ret;
1024 static int panthor_ioctl_group_get_state(struct drm_device *ddev, void *data,
1025 struct drm_file *file)
1027 struct panthor_file *pfile = file->driver_priv;
1028 struct drm_panthor_group_get_state *args = data;
1030 return panthor_group_get_state(pfile, args);
1033 static int panthor_ioctl_tiler_heap_create(struct drm_device *ddev, void *data,
1034 struct drm_file *file)
1036 struct panthor_file *pfile = file->driver_priv;
1037 struct drm_panthor_tiler_heap_create *args = data;
1038 struct panthor_heap_pool *pool;
1039 struct panthor_vm *vm;
1042 vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1046 pool = panthor_vm_get_heap_pool(vm, true);
1048 ret = PTR_ERR(pool);
1052 ret = panthor_heap_create(pool,
1053 args->initial_chunk_count,
1056 args->target_in_flight,
1057 &args->tiler_heap_ctx_gpu_va,
1058 &args->first_heap_chunk_gpu_va);
1060 goto out_put_heap_pool;
1062 /* Heap pools are per-VM. We combine the VM and HEAP id to make
1063 * a unique heap handle.
1065 args->handle = (args->vm_id << 16) | ret;
1069 panthor_heap_pool_put(pool);
1076 static int panthor_ioctl_tiler_heap_destroy(struct drm_device *ddev, void *data,
1077 struct drm_file *file)
1079 struct panthor_file *pfile = file->driver_priv;
1080 struct drm_panthor_tiler_heap_destroy *args = data;
1081 struct panthor_heap_pool *pool;
1082 struct panthor_vm *vm;
1088 vm = panthor_vm_pool_get_vm(pfile->vms, args->handle >> 16);
1092 pool = panthor_vm_get_heap_pool(vm, false);
1094 ret = PTR_ERR(pool);
1098 ret = panthor_heap_destroy(pool, args->handle & GENMASK(15, 0));
1099 panthor_heap_pool_put(pool);
1106 static int panthor_ioctl_vm_bind_async(struct drm_device *ddev,
1107 struct drm_panthor_vm_bind *args,
1108 struct drm_file *file)
1110 struct panthor_file *pfile = file->driver_priv;
1111 struct drm_panthor_vm_bind_op *jobs_args;
1112 struct panthor_submit_ctx ctx;
1113 struct panthor_vm *vm;
1116 vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1120 ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->ops);
1124 ret = panthor_submit_ctx_init(&ctx, file, args->ops.count);
1126 goto out_free_jobs_args;
1128 for (u32 i = 0; i < args->ops.count; i++) {
1129 struct drm_panthor_vm_bind_op *op = &jobs_args[i];
1130 struct drm_sched_job *job;
1132 job = panthor_vm_bind_job_create(file, vm, op);
1135 goto out_cleanup_submit_ctx;
1138 ret = panthor_submit_ctx_add_job(&ctx, i, job, &op->syncs);
1140 goto out_cleanup_submit_ctx;
1143 ret = panthor_submit_ctx_collect_jobs_signal_ops(&ctx);
1145 goto out_cleanup_submit_ctx;
1147 /* Prepare reservation objects for each VM_BIND job. */
1148 drm_exec_until_all_locked(&ctx.exec) {
1149 for (u32 i = 0; i < ctx.job_count; i++) {
1150 ret = panthor_vm_bind_job_prepare_resvs(&ctx.exec, ctx.jobs[i].job);
1151 drm_exec_retry_on_contention(&ctx.exec);
1153 goto out_cleanup_submit_ctx;
1157 ret = panthor_submit_ctx_add_deps_and_arm_jobs(&ctx);
1159 goto out_cleanup_submit_ctx;
1161 /* Nothing can fail after that point. */
1162 panthor_submit_ctx_push_jobs(&ctx, panthor_vm_bind_job_update_resvs);
1164 out_cleanup_submit_ctx:
1165 panthor_submit_ctx_cleanup(&ctx, panthor_vm_bind_job_put);
1175 static int panthor_ioctl_vm_bind_sync(struct drm_device *ddev,
1176 struct drm_panthor_vm_bind *args,
1177 struct drm_file *file)
1179 struct panthor_file *pfile = file->driver_priv;
1180 struct drm_panthor_vm_bind_op *jobs_args;
1181 struct panthor_vm *vm;
1184 vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1188 ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->ops);
1192 for (u32 i = 0; i < args->ops.count; i++) {
1193 ret = panthor_vm_bind_exec_sync_op(file, vm, &jobs_args[i]);
1195 /* Update ops.count so the user knows where things failed. */
1196 args->ops.count = i;
1208 #define PANTHOR_VM_BIND_FLAGS DRM_PANTHOR_VM_BIND_ASYNC
1210 static int panthor_ioctl_vm_bind(struct drm_device *ddev, void *data,
1211 struct drm_file *file)
1213 struct drm_panthor_vm_bind *args = data;
1216 if (!drm_dev_enter(ddev, &cookie))
1219 if (args->flags & DRM_PANTHOR_VM_BIND_ASYNC)
1220 ret = panthor_ioctl_vm_bind_async(ddev, args, file);
1222 ret = panthor_ioctl_vm_bind_sync(ddev, args, file);
1224 drm_dev_exit(cookie);
1228 static int panthor_ioctl_vm_get_state(struct drm_device *ddev, void *data,
1229 struct drm_file *file)
1231 struct panthor_file *pfile = file->driver_priv;
1232 struct drm_panthor_vm_get_state *args = data;
1233 struct panthor_vm *vm;
1235 vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
1239 if (panthor_vm_is_unusable(vm))
1240 args->state = DRM_PANTHOR_VM_STATE_UNUSABLE;
1242 args->state = DRM_PANTHOR_VM_STATE_USABLE;
1249 panthor_open(struct drm_device *ddev, struct drm_file *file)
1251 struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
1252 struct panthor_file *pfile;
1255 if (!try_module_get(THIS_MODULE))
1258 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
1264 pfile->ptdev = ptdev;
1266 ret = panthor_vm_pool_create(pfile);
1270 ret = panthor_group_pool_create(pfile);
1272 goto err_destroy_vm_pool;
1274 file->driver_priv = pfile;
1277 err_destroy_vm_pool:
1278 panthor_vm_pool_destroy(pfile);
1284 module_put(THIS_MODULE);
1289 panthor_postclose(struct drm_device *ddev, struct drm_file *file)
1291 struct panthor_file *pfile = file->driver_priv;
1293 panthor_group_pool_destroy(pfile);
1294 panthor_vm_pool_destroy(pfile);
1297 module_put(THIS_MODULE);
1300 static const struct drm_ioctl_desc panthor_drm_driver_ioctls[] = {
1301 #define PANTHOR_IOCTL(n, func, flags) \
1302 DRM_IOCTL_DEF_DRV(PANTHOR_##n, panthor_ioctl_##func, flags)
1304 PANTHOR_IOCTL(DEV_QUERY, dev_query, DRM_RENDER_ALLOW),
1305 PANTHOR_IOCTL(VM_CREATE, vm_create, DRM_RENDER_ALLOW),
1306 PANTHOR_IOCTL(VM_DESTROY, vm_destroy, DRM_RENDER_ALLOW),
1307 PANTHOR_IOCTL(VM_BIND, vm_bind, DRM_RENDER_ALLOW),
1308 PANTHOR_IOCTL(VM_GET_STATE, vm_get_state, DRM_RENDER_ALLOW),
1309 PANTHOR_IOCTL(BO_CREATE, bo_create, DRM_RENDER_ALLOW),
1310 PANTHOR_IOCTL(BO_MMAP_OFFSET, bo_mmap_offset, DRM_RENDER_ALLOW),
1311 PANTHOR_IOCTL(GROUP_CREATE, group_create, DRM_RENDER_ALLOW),
1312 PANTHOR_IOCTL(GROUP_DESTROY, group_destroy, DRM_RENDER_ALLOW),
1313 PANTHOR_IOCTL(GROUP_GET_STATE, group_get_state, DRM_RENDER_ALLOW),
1314 PANTHOR_IOCTL(TILER_HEAP_CREATE, tiler_heap_create, DRM_RENDER_ALLOW),
1315 PANTHOR_IOCTL(TILER_HEAP_DESTROY, tiler_heap_destroy, DRM_RENDER_ALLOW),
1316 PANTHOR_IOCTL(GROUP_SUBMIT, group_submit, DRM_RENDER_ALLOW),
1319 static int panthor_mmap(struct file *filp, struct vm_area_struct *vma)
1321 struct drm_file *file = filp->private_data;
1322 struct panthor_file *pfile = file->driver_priv;
1323 struct panthor_device *ptdev = pfile->ptdev;
1324 u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT;
1327 if (!drm_dev_enter(file->minor->dev, &cookie))
1332 * With 32-bit systems being limited by the 32-bit representation of
1333 * mmap2's pgoffset field, we need to make the MMIO offset arch
1334 * specific. This converts a user MMIO offset into something the kernel
1335 * driver understands.
1337 if (test_tsk_thread_flag(current, TIF_32BIT) &&
1338 offset >= DRM_PANTHOR_USER_MMIO_OFFSET_32BIT) {
1339 offset += DRM_PANTHOR_USER_MMIO_OFFSET_64BIT -
1340 DRM_PANTHOR_USER_MMIO_OFFSET_32BIT;
1341 vma->vm_pgoff = offset >> PAGE_SHIFT;
1345 if (offset >= DRM_PANTHOR_USER_MMIO_OFFSET)
1346 ret = panthor_device_mmap_io(ptdev, vma);
1348 ret = drm_gem_mmap(filp, vma);
1350 drm_dev_exit(cookie);
1354 static const struct file_operations panthor_drm_driver_fops = {
1356 .release = drm_release,
1357 .unlocked_ioctl = drm_ioctl,
1358 .compat_ioctl = drm_compat_ioctl,
1361 .llseek = noop_llseek,
1362 .mmap = panthor_mmap,
1365 #ifdef CONFIG_DEBUG_FS
1366 static void panthor_debugfs_init(struct drm_minor *minor)
1368 panthor_mmu_debugfs_init(minor);
1373 * PanCSF driver version:
1374 * - 1.0 - initial interface
1376 static const struct drm_driver panthor_drm_driver = {
1377 .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
1378 DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA,
1379 .open = panthor_open,
1380 .postclose = panthor_postclose,
1381 .ioctls = panthor_drm_driver_ioctls,
1382 .num_ioctls = ARRAY_SIZE(panthor_drm_driver_ioctls),
1383 .fops = &panthor_drm_driver_fops,
1385 .desc = "Panthor DRM driver",
1390 .gem_create_object = panthor_gem_create_object,
1391 .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
1392 #ifdef CONFIG_DEBUG_FS
1393 .debugfs_init = panthor_debugfs_init,
1397 static int panthor_probe(struct platform_device *pdev)
1399 struct panthor_device *ptdev;
1401 ptdev = devm_drm_dev_alloc(&pdev->dev, &panthor_drm_driver,
1402 struct panthor_device, base);
1406 platform_set_drvdata(pdev, ptdev);
1408 return panthor_device_init(ptdev);
1411 static void panthor_remove(struct platform_device *pdev)
1413 struct panthor_device *ptdev = platform_get_drvdata(pdev);
1415 panthor_device_unplug(ptdev);
1418 static const struct of_device_id dt_match[] = {
1419 { .compatible = "rockchip,rk3588-mali" },
1420 { .compatible = "arm,mali-valhall-csf" },
1423 MODULE_DEVICE_TABLE(of, dt_match);
1425 static DEFINE_RUNTIME_DEV_PM_OPS(panthor_pm_ops,
1426 panthor_device_suspend,
1427 panthor_device_resume,
1430 static struct platform_driver panthor_driver = {
1431 .probe = panthor_probe,
1432 .remove_new = panthor_remove,
1435 .pm = pm_ptr(&panthor_pm_ops),
1436 .of_match_table = dt_match,
1441 * Workqueue used to cleanup stuff.
1443 * We create a dedicated workqueue so we can drain on unplug and
1444 * make sure all resources are freed before the module is unloaded.
1446 struct workqueue_struct *panthor_cleanup_wq;
1448 static int __init panthor_init(void)
1452 ret = panthor_mmu_pt_cache_init();
1456 panthor_cleanup_wq = alloc_workqueue("panthor-cleanup", WQ_UNBOUND, 0);
1457 if (!panthor_cleanup_wq) {
1458 pr_err("panthor: Failed to allocate the workqueues");
1460 goto err_mmu_pt_cache_fini;
1463 ret = platform_driver_register(&panthor_driver);
1465 goto err_destroy_cleanup_wq;
1469 err_destroy_cleanup_wq:
1470 destroy_workqueue(panthor_cleanup_wq);
1472 err_mmu_pt_cache_fini:
1473 panthor_mmu_pt_cache_fini();
1476 module_init(panthor_init);
1478 static void __exit panthor_exit(void)
1480 platform_driver_unregister(&panthor_driver);
1481 destroy_workqueue(panthor_cleanup_wq);
1482 panthor_mmu_pt_cache_fini();
1484 module_exit(panthor_exit);
1486 MODULE_AUTHOR("Panthor Project Developers");
1487 MODULE_DESCRIPTION("Panthor DRM Driver");
1488 MODULE_LICENSE("Dual MIT/GPL");