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