1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
5 #include "pvr_context.h"
6 #include "pvr_device.h"
10 #include "pvr_power.h"
11 #include "pvr_rogue_fwif.h"
12 #include "pvr_rogue_fwif_common.h"
13 #include "pvr_rogue_fwif_resetframework.h"
14 #include "pvr_stream.h"
15 #include "pvr_stream_defs.h"
18 #include <drm/drm_auth.h>
19 #include <drm/drm_managed.h>
21 #include <linux/bug.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/xarray.h>
33 remap_priority(struct pvr_file *pvr_file, s32 uapi_priority,
34 enum pvr_context_priority *priority_out)
36 switch (uapi_priority) {
37 case DRM_PVR_CTX_PRIORITY_LOW:
38 *priority_out = PVR_CTX_PRIORITY_LOW;
40 case DRM_PVR_CTX_PRIORITY_NORMAL:
41 *priority_out = PVR_CTX_PRIORITY_MEDIUM;
43 case DRM_PVR_CTX_PRIORITY_HIGH:
44 if (!capable(CAP_SYS_NICE) && !drm_is_current_master(from_pvr_file(pvr_file)))
46 *priority_out = PVR_CTX_PRIORITY_HIGH;
55 static int get_fw_obj_size(enum drm_pvr_ctx_type type)
58 case DRM_PVR_CTX_TYPE_RENDER:
59 return sizeof(struct rogue_fwif_fwrendercontext);
60 case DRM_PVR_CTX_TYPE_COMPUTE:
61 return sizeof(struct rogue_fwif_fwcomputecontext);
62 case DRM_PVR_CTX_TYPE_TRANSFER_FRAG:
63 return sizeof(struct rogue_fwif_fwtransfercontext);
70 process_static_context_state(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_defs *cmd_defs,
71 u64 stream_user_ptr, u32 stream_size, void *dest)
76 stream = memdup_user(u64_to_user_ptr(stream_user_ptr), stream_size);
78 return PTR_ERR(stream);
80 err = pvr_stream_process(pvr_dev, cmd_defs, stream, stream_size, dest);
87 static int init_render_fw_objs(struct pvr_context *ctx,
88 struct drm_pvr_ioctl_create_context_args *args,
91 struct rogue_fwif_static_rendercontext_state *static_rendercontext_state;
92 struct rogue_fwif_fwrendercontext *fw_render_context = fw_ctx_map;
94 if (!args->static_context_state_len)
97 static_rendercontext_state = &fw_render_context->static_render_context_state;
99 /* Copy static render context state from userspace. */
100 return process_static_context_state(ctx->pvr_dev,
101 &pvr_static_render_context_state_stream,
102 args->static_context_state,
103 args->static_context_state_len,
104 &static_rendercontext_state->ctxswitch_regs[0]);
107 static int init_compute_fw_objs(struct pvr_context *ctx,
108 struct drm_pvr_ioctl_create_context_args *args,
111 struct rogue_fwif_fwcomputecontext *fw_compute_context = fw_ctx_map;
112 struct rogue_fwif_cdm_registers_cswitch *ctxswitch_regs;
114 if (!args->static_context_state_len)
117 ctxswitch_regs = &fw_compute_context->static_compute_context_state.ctxswitch_regs;
119 /* Copy static render context state from userspace. */
120 return process_static_context_state(ctx->pvr_dev,
121 &pvr_static_compute_context_state_stream,
122 args->static_context_state,
123 args->static_context_state_len,
127 static int init_transfer_fw_objs(struct pvr_context *ctx,
128 struct drm_pvr_ioctl_create_context_args *args,
131 if (args->static_context_state_len)
137 static int init_fw_objs(struct pvr_context *ctx,
138 struct drm_pvr_ioctl_create_context_args *args,
142 case DRM_PVR_CTX_TYPE_RENDER:
143 return init_render_fw_objs(ctx, args, fw_ctx_map);
144 case DRM_PVR_CTX_TYPE_COMPUTE:
145 return init_compute_fw_objs(ctx, args, fw_ctx_map);
146 case DRM_PVR_CTX_TYPE_TRANSFER_FRAG:
147 return init_transfer_fw_objs(ctx, args, fw_ctx_map);
154 ctx_fw_data_init(void *cpu_ptr, void *priv)
156 struct pvr_context *ctx = priv;
158 memcpy(cpu_ptr, ctx->data, ctx->data_size);
162 * pvr_context_destroy_queues() - Destroy all queues attached to a context.
163 * @ctx: Context to destroy queues on.
165 * Should be called when the last reference to a context object is dropped.
166 * It releases all resources attached to the queues bound to this context.
168 static void pvr_context_destroy_queues(struct pvr_context *ctx)
171 case DRM_PVR_CTX_TYPE_RENDER:
172 pvr_queue_destroy(ctx->queues.fragment);
173 pvr_queue_destroy(ctx->queues.geometry);
175 case DRM_PVR_CTX_TYPE_COMPUTE:
176 pvr_queue_destroy(ctx->queues.compute);
178 case DRM_PVR_CTX_TYPE_TRANSFER_FRAG:
179 pvr_queue_destroy(ctx->queues.transfer);
185 * pvr_context_create_queues() - Create all queues attached to a context.
186 * @ctx: Context to create queues on.
187 * @args: Context creation arguments passed by userspace.
188 * @fw_ctx_map: CPU mapping of the FW context object.
192 * * A negative error code otherwise.
194 static int pvr_context_create_queues(struct pvr_context *ctx,
195 struct drm_pvr_ioctl_create_context_args *args,
201 case DRM_PVR_CTX_TYPE_RENDER:
202 ctx->queues.geometry = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_GEOMETRY,
204 if (IS_ERR(ctx->queues.geometry)) {
205 err = PTR_ERR(ctx->queues.geometry);
206 ctx->queues.geometry = NULL;
207 goto err_destroy_queues;
210 ctx->queues.fragment = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_FRAGMENT,
212 if (IS_ERR(ctx->queues.fragment)) {
213 err = PTR_ERR(ctx->queues.fragment);
214 ctx->queues.fragment = NULL;
215 goto err_destroy_queues;
219 case DRM_PVR_CTX_TYPE_COMPUTE:
220 ctx->queues.compute = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_COMPUTE,
222 if (IS_ERR(ctx->queues.compute)) {
223 err = PTR_ERR(ctx->queues.compute);
224 ctx->queues.compute = NULL;
225 goto err_destroy_queues;
229 case DRM_PVR_CTX_TYPE_TRANSFER_FRAG:
230 ctx->queues.transfer = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_TRANSFER_FRAG,
232 if (IS_ERR(ctx->queues.transfer)) {
233 err = PTR_ERR(ctx->queues.transfer);
234 ctx->queues.transfer = NULL;
235 goto err_destroy_queues;
243 pvr_context_destroy_queues(ctx);
248 * pvr_context_kill_queues() - Kill queues attached to context.
249 * @ctx: Context to kill queues on.
251 * Killing the queues implies making them unusable for future jobs, while still
252 * letting the currently submitted jobs a chance to finish. Queue resources will
253 * stay around until pvr_context_destroy_queues() is called.
255 static void pvr_context_kill_queues(struct pvr_context *ctx)
258 case DRM_PVR_CTX_TYPE_RENDER:
259 pvr_queue_kill(ctx->queues.fragment);
260 pvr_queue_kill(ctx->queues.geometry);
262 case DRM_PVR_CTX_TYPE_COMPUTE:
263 pvr_queue_kill(ctx->queues.compute);
265 case DRM_PVR_CTX_TYPE_TRANSFER_FRAG:
266 pvr_queue_kill(ctx->queues.transfer);
272 * pvr_context_create() - Create a context.
273 * @pvr_file: File to attach the created context to.
274 * @args: Context creation arguments.
278 * * A negative error code on failure.
280 int pvr_context_create(struct pvr_file *pvr_file, struct drm_pvr_ioctl_create_context_args *args)
282 struct pvr_device *pvr_dev = pvr_file->pvr_dev;
283 struct pvr_context *ctx;
287 /* Context creation flags are currently unused and must be zero. */
291 ctx_size = get_fw_obj_size(args->type);
295 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
299 ctx->data_size = ctx_size;
300 ctx->type = args->type;
301 ctx->flags = args->flags;
302 ctx->pvr_dev = pvr_dev;
303 kref_init(&ctx->ref_count);
305 err = remap_priority(pvr_file, args->priority, &ctx->priority);
309 ctx->vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
310 if (IS_ERR(ctx->vm_ctx)) {
311 err = PTR_ERR(ctx->vm_ctx);
315 ctx->data = kzalloc(ctx_size, GFP_KERNEL);
321 err = pvr_context_create_queues(ctx, args, ctx->data);
323 goto err_free_ctx_data;
325 err = init_fw_objs(ctx, args, ctx->data);
327 goto err_destroy_queues;
329 err = pvr_fw_object_create(pvr_dev, ctx_size, PVR_BO_FW_FLAGS_DEVICE_UNCACHED,
330 ctx_fw_data_init, ctx, &ctx->fw_obj);
332 goto err_free_ctx_data;
334 err = xa_alloc(&pvr_dev->ctx_ids, &ctx->ctx_id, ctx, xa_limit_32b, GFP_KERNEL);
336 goto err_destroy_fw_obj;
338 err = xa_alloc(&pvr_file->ctx_handles, &args->handle, ctx, xa_limit_32b, GFP_KERNEL);
341 * It's possible that another thread could have taken a reference on the context at
342 * this point as it is in the ctx_ids xarray. Therefore instead of directly
343 * destroying the context, drop a reference instead.
345 pvr_context_put(ctx);
349 spin_lock(&pvr_dev->ctx_list_lock);
350 list_add_tail(&ctx->file_link, &pvr_file->contexts);
351 spin_unlock(&pvr_dev->ctx_list_lock);
356 pvr_fw_object_destroy(ctx->fw_obj);
359 pvr_context_destroy_queues(ctx);
365 pvr_vm_context_put(ctx->vm_ctx);
373 pvr_context_release(struct kref *ref_count)
375 struct pvr_context *ctx =
376 container_of(ref_count, struct pvr_context, ref_count);
377 struct pvr_device *pvr_dev = ctx->pvr_dev;
379 WARN_ON(in_interrupt());
380 spin_lock(&pvr_dev->ctx_list_lock);
381 list_del(&ctx->file_link);
382 spin_unlock(&pvr_dev->ctx_list_lock);
384 xa_erase(&pvr_dev->ctx_ids, ctx->ctx_id);
385 pvr_context_destroy_queues(ctx);
386 pvr_fw_object_destroy(ctx->fw_obj);
388 pvr_vm_context_put(ctx->vm_ctx);
393 * pvr_context_put() - Release reference on context
394 * @ctx: Target context.
397 pvr_context_put(struct pvr_context *ctx)
400 kref_put(&ctx->ref_count, pvr_context_release);
404 * pvr_context_destroy() - Destroy context
405 * @pvr_file: Pointer to pvr_file structure.
406 * @handle: Userspace context handle.
408 * Removes context from context list and drops initial reference. Context will
409 * then be destroyed once all outstanding references are dropped.
413 * * -%EINVAL if context not in context list.
416 pvr_context_destroy(struct pvr_file *pvr_file, u32 handle)
418 struct pvr_context *ctx = xa_erase(&pvr_file->ctx_handles, handle);
423 /* Make sure nothing can be queued to the queues after that point. */
424 pvr_context_kill_queues(ctx);
426 /* Release the reference held by the handle set. */
427 pvr_context_put(ctx);
433 * pvr_destroy_contexts_for_file: Destroy any contexts associated with the given file
434 * @pvr_file: Pointer to pvr_file structure.
436 * Removes all contexts associated with @pvr_file from the device context list and drops initial
437 * references. Contexts will then be destroyed once all outstanding references are dropped.
439 void pvr_destroy_contexts_for_file(struct pvr_file *pvr_file)
441 struct pvr_device *pvr_dev = pvr_file->pvr_dev;
442 struct pvr_context *ctx;
443 unsigned long handle;
445 xa_for_each(&pvr_file->ctx_handles, handle, ctx)
446 pvr_context_destroy(pvr_file, handle);
448 spin_lock(&pvr_dev->ctx_list_lock);
449 ctx = list_first_entry(&pvr_file->contexts, struct pvr_context, file_link);
451 while (!list_entry_is_head(ctx, &pvr_file->contexts, file_link)) {
452 list_del_init(&ctx->file_link);
454 if (pvr_context_get_if_referenced(ctx)) {
455 spin_unlock(&pvr_dev->ctx_list_lock);
457 pvr_vm_unmap_all(ctx->vm_ctx);
459 pvr_context_put(ctx);
460 spin_lock(&pvr_dev->ctx_list_lock);
462 ctx = list_first_entry(&pvr_file->contexts, struct pvr_context, file_link);
464 spin_unlock(&pvr_dev->ctx_list_lock);
468 * pvr_context_device_init() - Device level initialization for queue related resources.
469 * @pvr_dev: The device to initialize.
471 void pvr_context_device_init(struct pvr_device *pvr_dev)
473 xa_init_flags(&pvr_dev->ctx_ids, XA_FLAGS_ALLOC1);
474 spin_lock_init(&pvr_dev->ctx_list_lock);
478 * pvr_context_device_fini() - Device level cleanup for queue related resources.
479 * @pvr_dev: The device to cleanup.
481 void pvr_context_device_fini(struct pvr_device *pvr_dev)
483 WARN_ON(!xa_empty(&pvr_dev->ctx_ids));
484 xa_destroy(&pvr_dev->ctx_ids);