Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | // SPDX-License-Identifier: GPL-2.0-only OR MIT /* Copyright (c) 2023 Imagination Technologies Ltd. */ #include "pvr_cccb.h" #include "pvr_context.h" #include "pvr_device.h" #include "pvr_drv.h" #include "pvr_gem.h" #include "pvr_job.h" #include "pvr_power.h" #include "pvr_rogue_fwif.h" #include "pvr_rogue_fwif_common.h" #include "pvr_rogue_fwif_resetframework.h" #include "pvr_stream.h" #include "pvr_stream_defs.h" #include "pvr_vm.h" #include <drm/drm_auth.h> #include <drm/drm_managed.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/types.h> #include <linux/xarray.h> static int remap_priority(struct pvr_file *pvr_file, s32 uapi_priority, enum pvr_context_priority *priority_out) { switch (uapi_priority) { case DRM_PVR_CTX_PRIORITY_LOW: *priority_out = PVR_CTX_PRIORITY_LOW; break; case DRM_PVR_CTX_PRIORITY_NORMAL: *priority_out = PVR_CTX_PRIORITY_MEDIUM; break; case DRM_PVR_CTX_PRIORITY_HIGH: if (!capable(CAP_SYS_NICE) && !drm_is_current_master(from_pvr_file(pvr_file))) return -EACCES; *priority_out = PVR_CTX_PRIORITY_HIGH; break; default: return -EINVAL; } return 0; } static int get_fw_obj_size(enum drm_pvr_ctx_type type) { switch (type) { case DRM_PVR_CTX_TYPE_RENDER: return sizeof(struct rogue_fwif_fwrendercontext); case DRM_PVR_CTX_TYPE_COMPUTE: return sizeof(struct rogue_fwif_fwcomputecontext); case DRM_PVR_CTX_TYPE_TRANSFER_FRAG: return sizeof(struct rogue_fwif_fwtransfercontext); } return -EINVAL; } static int process_static_context_state(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_defs *cmd_defs, u64 stream_user_ptr, u32 stream_size, void *dest) { void *stream; int err; stream = kzalloc(stream_size, GFP_KERNEL); if (!stream) return -ENOMEM; if (copy_from_user(stream, u64_to_user_ptr(stream_user_ptr), stream_size)) { err = -EFAULT; goto err_free; } err = pvr_stream_process(pvr_dev, cmd_defs, stream, stream_size, dest); if (err) goto err_free; kfree(stream); return 0; err_free: kfree(stream); return err; } static int init_render_fw_objs(struct pvr_context *ctx, struct drm_pvr_ioctl_create_context_args *args, void *fw_ctx_map) { struct rogue_fwif_static_rendercontext_state *static_rendercontext_state; struct rogue_fwif_fwrendercontext *fw_render_context = fw_ctx_map; if (!args->static_context_state_len) return -EINVAL; static_rendercontext_state = &fw_render_context->static_render_context_state; /* Copy static render context state from userspace. */ return process_static_context_state(ctx->pvr_dev, &pvr_static_render_context_state_stream, args->static_context_state, args->static_context_state_len, &static_rendercontext_state->ctxswitch_regs[0]); } static int init_compute_fw_objs(struct pvr_context *ctx, struct drm_pvr_ioctl_create_context_args *args, void *fw_ctx_map) { struct rogue_fwif_fwcomputecontext *fw_compute_context = fw_ctx_map; struct rogue_fwif_cdm_registers_cswitch *ctxswitch_regs; if (!args->static_context_state_len) return -EINVAL; ctxswitch_regs = &fw_compute_context->static_compute_context_state.ctxswitch_regs; /* Copy static render context state from userspace. */ return process_static_context_state(ctx->pvr_dev, &pvr_static_compute_context_state_stream, args->static_context_state, args->static_context_state_len, ctxswitch_regs); } static int init_transfer_fw_objs(struct pvr_context *ctx, struct drm_pvr_ioctl_create_context_args *args, void *fw_ctx_map) { if (args->static_context_state_len) return -EINVAL; return 0; } static int init_fw_objs(struct pvr_context *ctx, struct drm_pvr_ioctl_create_context_args *args, void *fw_ctx_map) { switch (ctx->type) { case DRM_PVR_CTX_TYPE_RENDER: return init_render_fw_objs(ctx, args, fw_ctx_map); case DRM_PVR_CTX_TYPE_COMPUTE: return init_compute_fw_objs(ctx, args, fw_ctx_map); case DRM_PVR_CTX_TYPE_TRANSFER_FRAG: return init_transfer_fw_objs(ctx, args, fw_ctx_map); } return -EINVAL; } static void ctx_fw_data_init(void *cpu_ptr, void *priv) { struct pvr_context *ctx = priv; memcpy(cpu_ptr, ctx->data, ctx->data_size); } /** * pvr_context_destroy_queues() - Destroy all queues attached to a context. * @ctx: Context to destroy queues on. * * Should be called when the last reference to a context object is dropped. * It releases all resources attached to the queues bound to this context. */ static void pvr_context_destroy_queues(struct pvr_context *ctx) { switch (ctx->type) { case DRM_PVR_CTX_TYPE_RENDER: pvr_queue_destroy(ctx->queues.fragment); pvr_queue_destroy(ctx->queues.geometry); break; case DRM_PVR_CTX_TYPE_COMPUTE: pvr_queue_destroy(ctx->queues.compute); break; case DRM_PVR_CTX_TYPE_TRANSFER_FRAG: pvr_queue_destroy(ctx->queues.transfer); break; } } /** * pvr_context_create_queues() - Create all queues attached to a context. * @ctx: Context to create queues on. * @args: Context creation arguments passed by userspace. * @fw_ctx_map: CPU mapping of the FW context object. * * Return: * * 0 on success, or * * A negative error code otherwise. */ static int pvr_context_create_queues(struct pvr_context *ctx, struct drm_pvr_ioctl_create_context_args *args, void *fw_ctx_map) { int err; switch (ctx->type) { case DRM_PVR_CTX_TYPE_RENDER: ctx->queues.geometry = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_GEOMETRY, args, fw_ctx_map); if (IS_ERR(ctx->queues.geometry)) { err = PTR_ERR(ctx->queues.geometry); ctx->queues.geometry = NULL; goto err_destroy_queues; } ctx->queues.fragment = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_FRAGMENT, args, fw_ctx_map); if (IS_ERR(ctx->queues.fragment)) { err = PTR_ERR(ctx->queues.fragment); ctx->queues.fragment = NULL; goto err_destroy_queues; } return 0; case DRM_PVR_CTX_TYPE_COMPUTE: ctx->queues.compute = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_COMPUTE, args, fw_ctx_map); if (IS_ERR(ctx->queues.compute)) { err = PTR_ERR(ctx->queues.compute); ctx->queues.compute = NULL; goto err_destroy_queues; } return 0; case DRM_PVR_CTX_TYPE_TRANSFER_FRAG: ctx->queues.transfer = pvr_queue_create(ctx, DRM_PVR_JOB_TYPE_TRANSFER_FRAG, args, fw_ctx_map); if (IS_ERR(ctx->queues.transfer)) { err = PTR_ERR(ctx->queues.transfer); ctx->queues.transfer = NULL; goto err_destroy_queues; } return 0; } return -EINVAL; err_destroy_queues: pvr_context_destroy_queues(ctx); return err; } /** * pvr_context_kill_queues() - Kill queues attached to context. * @ctx: Context to kill queues on. * * Killing the queues implies making them unusable for future jobs, while still * letting the currently submitted jobs a chance to finish. Queue resources will * stay around until pvr_context_destroy_queues() is called. */ static void pvr_context_kill_queues(struct pvr_context *ctx) { switch (ctx->type) { case DRM_PVR_CTX_TYPE_RENDER: pvr_queue_kill(ctx->queues.fragment); pvr_queue_kill(ctx->queues.geometry); break; case DRM_PVR_CTX_TYPE_COMPUTE: pvr_queue_kill(ctx->queues.compute); break; case DRM_PVR_CTX_TYPE_TRANSFER_FRAG: pvr_queue_kill(ctx->queues.transfer); break; } } /** * pvr_context_create() - Create a context. * @pvr_file: File to attach the created context to. * @args: Context creation arguments. * * Return: * * 0 on success, or * * A negative error code on failure. */ int pvr_context_create(struct pvr_file *pvr_file, struct drm_pvr_ioctl_create_context_args *args) { struct pvr_device *pvr_dev = pvr_file->pvr_dev; struct pvr_context *ctx; int ctx_size; int err; /* Context creation flags are currently unused and must be zero. */ if (args->flags) return -EINVAL; ctx_size = get_fw_obj_size(args->type); if (ctx_size < 0) return ctx_size; ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; ctx->data_size = ctx_size; ctx->type = args->type; ctx->flags = args->flags; ctx->pvr_dev = pvr_dev; kref_init(&ctx->ref_count); err = remap_priority(pvr_file, args->priority, &ctx->priority); if (err) goto err_free_ctx; ctx->vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle); if (IS_ERR(ctx->vm_ctx)) { err = PTR_ERR(ctx->vm_ctx); goto err_free_ctx; } ctx->data = kzalloc(ctx_size, GFP_KERNEL); if (!ctx->data) { err = -ENOMEM; goto err_put_vm; } err = pvr_context_create_queues(ctx, args, ctx->data); if (err) goto err_free_ctx_data; err = init_fw_objs(ctx, args, ctx->data); if (err) goto err_destroy_queues; err = pvr_fw_object_create(pvr_dev, ctx_size, PVR_BO_FW_FLAGS_DEVICE_UNCACHED, ctx_fw_data_init, ctx, &ctx->fw_obj); if (err) goto err_free_ctx_data; err = xa_alloc(&pvr_dev->ctx_ids, &ctx->ctx_id, ctx, xa_limit_32b, GFP_KERNEL); if (err) goto err_destroy_fw_obj; err = xa_alloc(&pvr_file->ctx_handles, &args->handle, ctx, xa_limit_32b, GFP_KERNEL); if (err) { /* * It's possible that another thread could have taken a reference on the context at * this point as it is in the ctx_ids xarray. Therefore instead of directly * destroying the context, drop a reference instead. */ pvr_context_put(ctx); return err; } return 0; err_destroy_fw_obj: pvr_fw_object_destroy(ctx->fw_obj); err_destroy_queues: pvr_context_destroy_queues(ctx); err_free_ctx_data: kfree(ctx->data); err_put_vm: pvr_vm_context_put(ctx->vm_ctx); err_free_ctx: kfree(ctx); return err; } static void pvr_context_release(struct kref *ref_count) { struct pvr_context *ctx = container_of(ref_count, struct pvr_context, ref_count); struct pvr_device *pvr_dev = ctx->pvr_dev; xa_erase(&pvr_dev->ctx_ids, ctx->ctx_id); pvr_context_destroy_queues(ctx); pvr_fw_object_destroy(ctx->fw_obj); kfree(ctx->data); pvr_vm_context_put(ctx->vm_ctx); kfree(ctx); } /** * pvr_context_put() - Release reference on context * @ctx: Target context. */ void pvr_context_put(struct pvr_context *ctx) { if (ctx) kref_put(&ctx->ref_count, pvr_context_release); } /** * pvr_context_destroy() - Destroy context * @pvr_file: Pointer to pvr_file structure. * @handle: Userspace context handle. * * Removes context from context list and drops initial reference. Context will * then be destroyed once all outstanding references are dropped. * * Return: * * 0 on success, or * * -%EINVAL if context not in context list. */ int pvr_context_destroy(struct pvr_file *pvr_file, u32 handle) { struct pvr_context *ctx = xa_erase(&pvr_file->ctx_handles, handle); if (!ctx) return -EINVAL; /* Make sure nothing can be queued to the queues after that point. */ pvr_context_kill_queues(ctx); /* Release the reference held by the handle set. */ pvr_context_put(ctx); return 0; } /** * pvr_destroy_contexts_for_file: Destroy any contexts associated with the given file * @pvr_file: Pointer to pvr_file structure. * * Removes all contexts associated with @pvr_file from the device context list and drops initial * references. Contexts will then be destroyed once all outstanding references are dropped. */ void pvr_destroy_contexts_for_file(struct pvr_file *pvr_file) { struct pvr_context *ctx; unsigned long handle; xa_for_each(&pvr_file->ctx_handles, handle, ctx) pvr_context_destroy(pvr_file, handle); } /** * pvr_context_device_init() - Device level initialization for queue related resources. * @pvr_dev: The device to initialize. */ void pvr_context_device_init(struct pvr_device *pvr_dev) { xa_init_flags(&pvr_dev->ctx_ids, XA_FLAGS_ALLOC1); } /** * pvr_context_device_fini() - Device level cleanup for queue related resources. * @pvr_dev: The device to cleanup. */ void pvr_context_device_fini(struct pvr_device *pvr_dev) { WARN_ON(!xa_empty(&pvr_dev->ctx_ids)); xa_destroy(&pvr_dev->ctx_ids); } |