1 // SPDX-License-Identifier: GPL-2.0 or MIT 2 /* Copyright 2023 Collabora ltd. */ 3 4 #include <drm/drm_drv.h> 5 #include <drm/drm_exec.h> 6 #include <drm/drm_gem_shmem_helper.h> 7 #include <drm/drm_managed.h> 8 #include <drm/drm_print.h> 9 #include <drm/gpu_scheduler.h> 10 #include <drm/panthor_drm.h> 11 12 #include <linux/build_bug.h> 13 #include <linux/cleanup.h> 14 #include <linux/clk.h> 15 #include <linux/delay.h> 16 #include <linux/dma-mapping.h> 17 #include <linux/dma-resv.h> 18 #include <linux/firmware.h> 19 #include <linux/interrupt.h> 20 #include <linux/io.h> 21 #include <linux/iopoll.h> 22 #include <linux/iosys-map.h> 23 #include <linux/module.h> 24 #include <linux/platform_device.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/rcupdate.h> 27 28 #include "panthor_devfreq.h" 29 #include "panthor_device.h" 30 #include "panthor_fw.h" 31 #include "panthor_gem.h" 32 #include "panthor_gpu.h" 33 #include "panthor_heap.h" 34 #include "panthor_mmu.h" 35 #include "panthor_regs.h" 36 #include "panthor_sched.h" 37 38 /** 39 * DOC: Scheduler 40 * 41 * Mali CSF hardware adopts a firmware-assisted scheduling model, where 42 * the firmware takes care of scheduling aspects, to some extent. 43 * 44 * The scheduling happens at the scheduling group level, each group 45 * contains 1 to N queues (N is FW/hardware dependent, and exposed 46 * through the firmware interface). Each queue is assigned a command 47 * stream ring buffer, which serves as a way to get jobs submitted to 48 * the GPU, among other things. 49 * 50 * The firmware can schedule a maximum of M groups (M is FW/hardware 51 * dependent, and exposed through the firmware interface). Passed 52 * this maximum number of groups, the kernel must take care of 53 * rotating the groups passed to the firmware so every group gets 54 * a chance to have his queues scheduled for execution. 55 * 56 * The current implementation only supports with kernel-mode queues. 57 * In other terms, userspace doesn't have access to the ring-buffer. 58 * Instead, userspace passes indirect command stream buffers that are 59 * called from the queue ring-buffer by the kernel using a pre-defined 60 * sequence of command stream instructions to ensure the userspace driver 61 * always gets consistent results (cache maintenance, 62 * synchronization, ...). 63 * 64 * We rely on the drm_gpu_scheduler framework to deal with job 65 * dependencies and submission. As any other driver dealing with a 66 * FW-scheduler, we use the 1:1 entity:scheduler mode, such that each 67 * entity has its own job scheduler. When a job is ready to be executed 68 * (all its dependencies are met), it is pushed to the appropriate 69 * queue ring-buffer, and the group is scheduled for execution if it 70 * wasn't already active. 71 * 72 * Kernel-side group scheduling is timeslice-based. When we have less 73 * groups than there are slots, the periodic tick is disabled and we 74 * just let the FW schedule the active groups. When there are more 75 * groups than slots, we let each group a chance to execute stuff for 76 * a given amount of time, and then re-evaluate and pick new groups 77 * to schedule. The group selection algorithm is based on 78 * priority+round-robin. 79 * 80 * Even though user-mode queues is out of the scope right now, the 81 * current design takes them into account by avoiding any guess on the 82 * group/queue state that would be based on information we wouldn't have 83 * if userspace was in charge of the ring-buffer. That's also one of the 84 * reason we don't do 'cooperative' scheduling (encoding FW group slot 85 * reservation as dma_fence that would be returned from the 86 * drm_gpu_scheduler::prepare_job() hook, and treating group rotation as 87 * a queue of waiters, ordered by job submission order). This approach 88 * would work for kernel-mode queues, but would make user-mode queues a 89 * lot more complicated to retrofit. 90 */ 91 92 #define JOB_TIMEOUT_MS 5000 93 94 #define MAX_CSG_PRIO 0xf 95 96 #define NUM_INSTRS_PER_CACHE_LINE (64 / sizeof(u64)) 97 #define MAX_INSTRS_PER_JOB 24 98 99 struct panthor_group; 100 101 /** 102 * struct panthor_csg_slot - Command stream group slot 103 * 104 * This represents a FW slot for a scheduling group. 105 */ 106 struct panthor_csg_slot { 107 /** @group: Scheduling group bound to this slot. */ 108 struct panthor_group *group; 109 110 /** @priority: Group priority. */ 111 u8 priority; 112 }; 113 114 /** 115 * enum panthor_csg_priority - Group priority 116 */ 117 enum panthor_csg_priority { 118 /** @PANTHOR_CSG_PRIORITY_LOW: Low priority group. */ 119 PANTHOR_CSG_PRIORITY_LOW = 0, 120 121 /** @PANTHOR_CSG_PRIORITY_MEDIUM: Medium priority group. */ 122 PANTHOR_CSG_PRIORITY_MEDIUM, 123 124 /** @PANTHOR_CSG_PRIORITY_HIGH: High priority group. */ 125 PANTHOR_CSG_PRIORITY_HIGH, 126 127 /** 128 * @PANTHOR_CSG_PRIORITY_RT: Real-time priority group. 129 * 130 * Real-time priority allows one to preempt scheduling of other 131 * non-real-time groups. When such a group becomes executable, 132 * it will evict the group with the lowest non-rt priority if 133 * there's no free group slot available. 134 */ 135 PANTHOR_CSG_PRIORITY_RT, 136 137 /** @PANTHOR_CSG_PRIORITY_COUNT: Number of priority levels. */ 138 PANTHOR_CSG_PRIORITY_COUNT, 139 }; 140 141 /** 142 * struct panthor_scheduler - Object used to manage the scheduler 143 */ 144 struct panthor_scheduler { 145 /** @ptdev: Device. */ 146 struct panthor_device *ptdev; 147 148 /** 149 * @wq: Workqueue used by our internal scheduler logic and 150 * drm_gpu_scheduler. 151 * 152 * Used for the scheduler tick, group update or other kind of FW 153 * event processing that can't be handled in the threaded interrupt 154 * path. Also passed to the drm_gpu_scheduler instances embedded 155 * in panthor_queue. 156 */ 157 struct workqueue_struct *wq; 158 159 /** 160 * @heap_alloc_wq: Workqueue used to schedule tiler_oom works. 161 * 162 * We have a queue dedicated to heap chunk allocation works to avoid 163 * blocking the rest of the scheduler if the allocation tries to 164 * reclaim memory. 165 */ 166 struct workqueue_struct *heap_alloc_wq; 167 168 /** @tick_work: Work executed on a scheduling tick. */ 169 struct delayed_work tick_work; 170 171 /** 172 * @sync_upd_work: Work used to process synchronization object updates. 173 * 174 * We use this work to unblock queues/groups that were waiting on a 175 * synchronization object. 176 */ 177 struct work_struct sync_upd_work; 178 179 /** 180 * @fw_events_work: Work used to process FW events outside the interrupt path. 181 * 182 * Even if the interrupt is threaded, we need any event processing 183 * that require taking the panthor_scheduler::lock to be processed 184 * outside the interrupt path so we don't block the tick logic when 185 * it calls panthor_fw_{csg,wait}_wait_acks(). Since most of the 186 * event processing requires taking this lock, we just delegate all 187 * FW event processing to the scheduler workqueue. 188 */ 189 struct work_struct fw_events_work; 190 191 /** 192 * @fw_events: Bitmask encoding pending FW events. 193 */ 194 atomic_t fw_events; 195 196 /** 197 * @resched_target: When the next tick should occur. 198 * 199 * Expressed in jiffies. 200 */ 201 u64 resched_target; 202 203 /** 204 * @last_tick: When the last tick occurred. 205 * 206 * Expressed in jiffies. 207 */ 208 u64 last_tick; 209 210 /** @tick_period: Tick period in jiffies. */ 211 u64 tick_period; 212 213 /** 214 * @lock: Lock protecting access to all the scheduler fields. 215 * 216 * Should be taken in the tick work, the irq handler, and anywhere the @groups 217 * fields are touched. 218 */ 219 struct mutex lock; 220 221 /** @groups: Various lists used to classify groups. */ 222 struct { 223 /** 224 * @runnable: Runnable group lists. 225 * 226 * When a group has queues that want to execute something, 227 * its panthor_group::run_node should be inserted here. 228 * 229 * One list per-priority. 230 */ 231 struct list_head runnable[PANTHOR_CSG_PRIORITY_COUNT]; 232 233 /** 234 * @idle: Idle group lists. 235 * 236 * When all queues of a group are idle (either because they 237 * have nothing to execute, or because they are blocked), the 238 * panthor_group::run_node field should be inserted here. 239 * 240 * One list per-priority. 241 */ 242 struct list_head idle[PANTHOR_CSG_PRIORITY_COUNT]; 243 244 /** 245 * @waiting: List of groups whose queues are blocked on a 246 * synchronization object. 247 * 248 * Insert panthor_group::wait_node here when a group is waiting 249 * for synchronization objects to be signaled. 250 * 251 * This list is evaluated in the @sync_upd_work work. 252 */ 253 struct list_head waiting; 254 } groups; 255 256 /** 257 * @csg_slots: FW command stream group slots. 258 */ 259 struct panthor_csg_slot csg_slots[MAX_CSGS]; 260 261 /** @csg_slot_count: Number of command stream group slots exposed by the FW. */ 262 u32 csg_slot_count; 263 264 /** @cs_slot_count: Number of command stream slot per group slot exposed by the FW. */ 265 u32 cs_slot_count; 266 267 /** @as_slot_count: Number of address space slots supported by the MMU. */ 268 u32 as_slot_count; 269 270 /** @used_csg_slot_count: Number of command stream group slot currently used. */ 271 u32 used_csg_slot_count; 272 273 /** @sb_slot_count: Number of scoreboard slots. */ 274 u32 sb_slot_count; 275 276 /** 277 * @might_have_idle_groups: True if an active group might have become idle. 278 * 279 * This will force a tick, so other runnable groups can be scheduled if one 280 * or more active groups became idle. 281 */ 282 bool might_have_idle_groups; 283 284 /** @pm: Power management related fields. */ 285 struct { 286 /** @has_ref: True if the scheduler owns a runtime PM reference. */ 287 bool has_ref; 288 } pm; 289 290 /** @reset: Reset related fields. */ 291 struct { 292 /** @lock: Lock protecting the other reset fields. */ 293 struct mutex lock; 294 295 /** 296 * @in_progress: True if a reset is in progress. 297 * 298 * Set to true in panthor_sched_pre_reset() and back to false in 299 * panthor_sched_post_reset(). 300 */ 301 atomic_t in_progress; 302 303 /** 304 * @stopped_groups: List containing all groups that were stopped 305 * before a reset. 306 * 307 * Insert panthor_group::run_node in the pre_reset path. 308 */ 309 struct list_head stopped_groups; 310 } reset; 311 }; 312 313 /** 314 * struct panthor_syncobj_32b - 32-bit FW synchronization object 315 */ 316 struct panthor_syncobj_32b { 317 /** @seqno: Sequence number. */ 318 u32 seqno; 319 320 /** 321 * @status: Status. 322 * 323 * Not zero on failure. 324 */ 325 u32 status; 326 }; 327 328 /** 329 * struct panthor_syncobj_64b - 64-bit FW synchronization object 330 */ 331 struct panthor_syncobj_64b { 332 /** @seqno: Sequence number. */ 333 u64 seqno; 334 335 /** 336 * @status: Status. 337 * 338 * Not zero on failure. 339 */ 340 u32 status; 341 342 /** @pad: MBZ. */ 343 u32 pad; 344 }; 345 346 /** 347 * struct panthor_queue - Execution queue 348 */ 349 struct panthor_queue { 350 /** @scheduler: DRM scheduler used for this queue. */ 351 struct drm_gpu_scheduler scheduler; 352 353 /** @entity: DRM scheduling entity used for this queue. */ 354 struct drm_sched_entity entity; 355 356 /** @name: DRM scheduler name for this queue. */ 357 char *name; 358 359 /** @timeout: Queue timeout related fields. */ 360 struct { 361 /** @timeout.work: Work executed when a queue timeout occurs. */ 362 struct delayed_work work; 363 364 /** 365 * @timeout.remaining: Time remaining before a queue timeout. 366 * 367 * When the timer is running, this value is set to MAX_SCHEDULE_TIMEOUT. 368 * When the timer is suspended, it's set to the time remaining when the 369 * timer was suspended. 370 */ 371 unsigned long remaining; 372 } timeout; 373 374 /** 375 * @doorbell_id: Doorbell assigned to this queue. 376 * 377 * Right now, all groups share the same doorbell, and the doorbell ID 378 * is assigned to group_slot + 1 when the group is assigned a slot. But 379 * we might decide to provide fine grained doorbell assignment at some 380 * point, so don't have to wake up all queues in a group every time one 381 * of them is updated. 382 */ 383 u8 doorbell_id; 384 385 /** 386 * @priority: Priority of the queue inside the group. 387 * 388 * Must be less than 16 (Only 4 bits available). 389 */ 390 u8 priority; 391 #define CSF_MAX_QUEUE_PRIO GENMASK(3, 0) 392 393 /** @ringbuf: Command stream ring-buffer. */ 394 struct panthor_kernel_bo *ringbuf; 395 396 /** @iface: Firmware interface. */ 397 struct { 398 /** @mem: FW memory allocated for this interface. */ 399 struct panthor_kernel_bo *mem; 400 401 /** @input: Input interface. */ 402 struct panthor_fw_ringbuf_input_iface *input; 403 404 /** @output: Output interface. */ 405 const struct panthor_fw_ringbuf_output_iface *output; 406 407 /** @input_fw_va: FW virtual address of the input interface buffer. */ 408 u32 input_fw_va; 409 410 /** @output_fw_va: FW virtual address of the output interface buffer. */ 411 u32 output_fw_va; 412 } iface; 413 414 /** 415 * @syncwait: Stores information about the synchronization object this 416 * queue is waiting on. 417 */ 418 struct { 419 /** @gpu_va: GPU address of the synchronization object. */ 420 u64 gpu_va; 421 422 /** @ref: Reference value to compare against. */ 423 u64 ref; 424 425 /** @gt: True if this is a greater-than test. */ 426 bool gt; 427 428 /** @sync64: True if this is a 64-bit sync object. */ 429 bool sync64; 430 431 /** @bo: Buffer object holding the synchronization object. */ 432 struct drm_gem_object *obj; 433 434 /** @offset: Offset of the synchronization object inside @bo. */ 435 u64 offset; 436 437 /** 438 * @kmap: Kernel mapping of the buffer object holding the 439 * synchronization object. 440 */ 441 void *kmap; 442 } syncwait; 443 444 /** @fence_ctx: Fence context fields. */ 445 struct { 446 /** @lock: Used to protect access to all fences allocated by this context. */ 447 spinlock_t lock; 448 449 /** 450 * @id: Fence context ID. 451 * 452 * Allocated with dma_fence_context_alloc(). 453 */ 454 u64 id; 455 456 /** @seqno: Sequence number of the last initialized fence. */ 457 atomic64_t seqno; 458 459 /** 460 * @last_fence: Fence of the last submitted job. 461 * 462 * We return this fence when we get an empty command stream. 463 * This way, we are guaranteed that all earlier jobs have completed 464 * when drm_sched_job::s_fence::finished without having to feed 465 * the CS ring buffer with a dummy job that only signals the fence. 466 */ 467 struct dma_fence *last_fence; 468 469 /** 470 * @in_flight_jobs: List containing all in-flight jobs. 471 * 472 * Used to keep track and signal panthor_job::done_fence when the 473 * synchronization object attached to the queue is signaled. 474 */ 475 struct list_head in_flight_jobs; 476 } fence_ctx; 477 478 /** @profiling: Job profiling data slots and access information. */ 479 struct { 480 /** @slots: Kernel BO holding the slots. */ 481 struct panthor_kernel_bo *slots; 482 483 /** @slot_count: Number of jobs ringbuffer can hold at once. */ 484 u32 slot_count; 485 486 /** @seqno: Index of the next available profiling information slot. */ 487 u32 seqno; 488 } profiling; 489 }; 490 491 /** 492 * enum panthor_group_state - Scheduling group state. 493 */ 494 enum panthor_group_state { 495 /** @PANTHOR_CS_GROUP_CREATED: Group was created, but not scheduled yet. */ 496 PANTHOR_CS_GROUP_CREATED, 497 498 /** @PANTHOR_CS_GROUP_ACTIVE: Group is currently scheduled. */ 499 PANTHOR_CS_GROUP_ACTIVE, 500 501 /** 502 * @PANTHOR_CS_GROUP_SUSPENDED: Group was scheduled at least once, but is 503 * inactive/suspended right now. 504 */ 505 PANTHOR_CS_GROUP_SUSPENDED, 506 507 /** 508 * @PANTHOR_CS_GROUP_TERMINATED: Group was terminated. 509 * 510 * Can no longer be scheduled. The only allowed action is a destruction. 511 */ 512 PANTHOR_CS_GROUP_TERMINATED, 513 514 /** 515 * @PANTHOR_CS_GROUP_UNKNOWN_STATE: Group is an unknown state. 516 * 517 * The FW returned an inconsistent state. The group is flagged unusable 518 * and can no longer be scheduled. The only allowed action is a 519 * destruction. 520 * 521 * When that happens, we also schedule a FW reset, to start from a fresh 522 * state. 523 */ 524 PANTHOR_CS_GROUP_UNKNOWN_STATE, 525 }; 526 527 /** 528 * struct panthor_group - Scheduling group object 529 */ 530 struct panthor_group { 531 /** @refcount: Reference count */ 532 struct kref refcount; 533 534 /** @ptdev: Device. */ 535 struct panthor_device *ptdev; 536 537 /** @vm: VM bound to the group. */ 538 struct panthor_vm *vm; 539 540 /** @compute_core_mask: Mask of shader cores that can be used for compute jobs. */ 541 u64 compute_core_mask; 542 543 /** @fragment_core_mask: Mask of shader cores that can be used for fragment jobs. */ 544 u64 fragment_core_mask; 545 546 /** @tiler_core_mask: Mask of tiler cores that can be used for tiler jobs. */ 547 u64 tiler_core_mask; 548 549 /** @max_compute_cores: Maximum number of shader cores used for compute jobs. */ 550 u8 max_compute_cores; 551 552 /** @max_fragment_cores: Maximum number of shader cores used for fragment jobs. */ 553 u8 max_fragment_cores; 554 555 /** @max_tiler_cores: Maximum number of tiler cores used for tiler jobs. */ 556 u8 max_tiler_cores; 557 558 /** @priority: Group priority (check panthor_csg_priority). */ 559 u8 priority; 560 561 /** @blocked_queues: Bitmask reflecting the blocked queues. */ 562 u32 blocked_queues; 563 564 /** @idle_queues: Bitmask reflecting the idle queues. */ 565 u32 idle_queues; 566 567 /** @fatal_lock: Lock used to protect access to fatal fields. */ 568 spinlock_t fatal_lock; 569 570 /** @fatal_queues: Bitmask reflecting the queues that hit a fatal exception. */ 571 u32 fatal_queues; 572 573 /** @tiler_oom: Mask of queues that have a tiler OOM event to process. */ 574 atomic_t tiler_oom; 575 576 /** @queue_count: Number of queues in this group. */ 577 u32 queue_count; 578 579 /** @queues: Queues owned by this group. */ 580 struct panthor_queue *queues[MAX_CS_PER_CSG]; 581 582 /** 583 * @csg_id: ID of the FW group slot. 584 * 585 * -1 when the group is not scheduled/active. 586 */ 587 int csg_id; 588 589 /** 590 * @destroyed: True when the group has been destroyed. 591 * 592 * If a group is destroyed it becomes useless: no further jobs can be submitted 593 * to its queues. We simply wait for all references to be dropped so we can 594 * release the group object. 595 */ 596 bool destroyed; 597 598 /** 599 * @timedout: True when a timeout occurred on any of the queues owned by 600 * this group. 601 * 602 * Timeouts can be reported by drm_sched or by the FW. If a reset is required, 603 * and the group can't be suspended, this also leads to a timeout. In any case, 604 * any timeout situation is unrecoverable, and the group becomes useless. We 605 * simply wait for all references to be dropped so we can release the group 606 * object. 607 */ 608 bool timedout; 609 610 /** 611 * @innocent: True when the group becomes unusable because the group suspension 612 * failed during a reset. 613 * 614 * Sometimes the FW was put in a bad state by other groups, causing the group 615 * suspension happening in the reset path to fail. In that case, we consider the 616 * group innocent. 617 */ 618 bool innocent; 619 620 /** 621 * @syncobjs: Pool of per-queue synchronization objects. 622 * 623 * One sync object per queue. The position of the sync object is 624 * determined by the queue index. 625 */ 626 struct panthor_kernel_bo *syncobjs; 627 628 /** @fdinfo: Per-file info exposed through /proc/<process>/fdinfo */ 629 struct { 630 /** @data: Total sampled values for jobs in queues from this group. */ 631 struct panthor_gpu_usage data; 632 633 /** 634 * @fdinfo.lock: Spinlock to govern concurrent access from drm file's fdinfo 635 * callback and job post-completion processing function 636 */ 637 spinlock_t lock; 638 639 /** @fdinfo.kbo_sizes: Aggregate size of private kernel BO's held by the group. */ 640 size_t kbo_sizes; 641 } fdinfo; 642 643 /** @task_info: Info of current->group_leader that created the group. */ 644 struct { 645 /** @task_info.pid: pid of current->group_leader */ 646 pid_t pid; 647 648 /** @task_info.comm: comm of current->group_leader */ 649 char comm[TASK_COMM_LEN]; 650 } task_info; 651 652 /** @state: Group state. */ 653 enum panthor_group_state state; 654 655 /** 656 * @suspend_buf: Suspend buffer. 657 * 658 * Stores the state of the group and its queues when a group is suspended. 659 * Used at resume time to restore the group in its previous state. 660 * 661 * The size of the suspend buffer is exposed through the FW interface. 662 */ 663 struct panthor_kernel_bo *suspend_buf; 664 665 /** 666 * @protm_suspend_buf: Protection mode suspend buffer. 667 * 668 * Stores the state of the group and its queues when a group that's in 669 * protection mode is suspended. 670 * 671 * Used at resume time to restore the group in its previous state. 672 * 673 * The size of the protection mode suspend buffer is exposed through the 674 * FW interface. 675 */ 676 struct panthor_kernel_bo *protm_suspend_buf; 677 678 /** @sync_upd_work: Work used to check/signal job fences. */ 679 struct work_struct sync_upd_work; 680 681 /** @tiler_oom_work: Work used to process tiler OOM events happening on this group. */ 682 struct work_struct tiler_oom_work; 683 684 /** @term_work: Work used to finish the group termination procedure. */ 685 struct work_struct term_work; 686 687 /** 688 * @release_work: Work used to release group resources. 689 * 690 * We need to postpone the group release to avoid a deadlock when 691 * the last ref is released in the tick work. 692 */ 693 struct work_struct release_work; 694 695 /** 696 * @run_node: Node used to insert the group in the 697 * panthor_group::groups::{runnable,idle} and 698 * panthor_group::reset.stopped_groups lists. 699 */ 700 struct list_head run_node; 701 702 /** 703 * @wait_node: Node used to insert the group in the 704 * panthor_group::groups::waiting list. 705 */ 706 struct list_head wait_node; 707 }; 708 709 struct panthor_job_profiling_data { 710 struct { 711 u64 before; 712 u64 after; 713 } cycles; 714 715 struct { 716 u64 before; 717 u64 after; 718 } time; 719 }; 720 721 /** 722 * group_queue_work() - Queue a group work 723 * @group: Group to queue the work for. 724 * @wname: Work name. 725 * 726 * Grabs a ref and queue a work item to the scheduler workqueue. If 727 * the work was already queued, we release the reference we grabbed. 728 * 729 * Work callbacks must release the reference we grabbed here. 730 */ 731 #define group_queue_work(group, wname) \ 732 do { \ 733 group_get(group); \ 734 if (!queue_work((group)->ptdev->scheduler->wq, &(group)->wname ## _work)) \ 735 group_put(group); \ 736 } while (0) 737 738 /** 739 * sched_queue_work() - Queue a scheduler work. 740 * @sched: Scheduler object. 741 * @wname: Work name. 742 * 743 * Conditionally queues a scheduler work if no reset is pending/in-progress. 744 */ 745 #define sched_queue_work(sched, wname) \ 746 do { \ 747 if (!atomic_read(&(sched)->reset.in_progress) && \ 748 !panthor_device_reset_is_pending((sched)->ptdev)) \ 749 queue_work((sched)->wq, &(sched)->wname ## _work); \ 750 } while (0) 751 752 /** 753 * sched_queue_delayed_work() - Queue a scheduler delayed work. 754 * @sched: Scheduler object. 755 * @wname: Work name. 756 * @delay: Work delay in jiffies. 757 * 758 * Conditionally queues a scheduler delayed work if no reset is 759 * pending/in-progress. 760 */ 761 #define sched_queue_delayed_work(sched, wname, delay) \ 762 do { \ 763 if (!atomic_read(&sched->reset.in_progress) && \ 764 !panthor_device_reset_is_pending((sched)->ptdev)) \ 765 mod_delayed_work((sched)->wq, &(sched)->wname ## _work, delay); \ 766 } while (0) 767 768 /* 769 * We currently set the maximum of groups per file to an arbitrary low value. 770 * But this can be updated if we need more. 771 */ 772 #define MAX_GROUPS_PER_POOL 128 773 774 /* 775 * Mark added on an entry of group pool Xarray to identify if the group has 776 * been fully initialized and can be accessed elsewhere in the driver code. 777 */ 778 #define GROUP_REGISTERED XA_MARK_1 779 780 /** 781 * struct panthor_group_pool - Group pool 782 * 783 * Each file get assigned a group pool. 784 */ 785 struct panthor_group_pool { 786 /** @xa: Xarray used to manage group handles. */ 787 struct xarray xa; 788 }; 789 790 /** 791 * struct panthor_job - Used to manage GPU job 792 */ 793 struct panthor_job { 794 /** @base: Inherit from drm_sched_job. */ 795 struct drm_sched_job base; 796 797 /** @refcount: Reference count. */ 798 struct kref refcount; 799 800 /** @group: Group of the queue this job will be pushed to. */ 801 struct panthor_group *group; 802 803 /** @queue_idx: Index of the queue inside @group. */ 804 u32 queue_idx; 805 806 /** @call_info: Information about the userspace command stream call. */ 807 struct { 808 /** @start: GPU address of the userspace command stream. */ 809 u64 start; 810 811 /** @size: Size of the userspace command stream. */ 812 u32 size; 813 814 /** 815 * @latest_flush: Flush ID at the time the userspace command 816 * stream was built. 817 * 818 * Needed for the flush reduction mechanism. 819 */ 820 u32 latest_flush; 821 } call_info; 822 823 /** @ringbuf: Position of this job is in the ring buffer. */ 824 struct { 825 /** @start: Start offset. */ 826 u64 start; 827 828 /** @end: End offset. */ 829 u64 end; 830 } ringbuf; 831 832 /** 833 * @node: Used to insert the job in the panthor_queue::fence_ctx::in_flight_jobs 834 * list. 835 */ 836 struct list_head node; 837 838 /** @done_fence: Fence signaled when the job is finished or cancelled. */ 839 struct dma_fence *done_fence; 840 841 /** @profiling: Job profiling information. */ 842 struct { 843 /** @mask: Current device job profiling enablement bitmask. */ 844 u32 mask; 845 846 /** @slot: Job index in the profiling slots BO. */ 847 u32 slot; 848 } profiling; 849 }; 850 851 static void 852 panthor_queue_put_syncwait_obj(struct panthor_queue *queue) 853 { 854 if (queue->syncwait.kmap) { 855 struct iosys_map map = IOSYS_MAP_INIT_VADDR(queue->syncwait.kmap); 856 857 drm_gem_vunmap(queue->syncwait.obj, &map); 858 queue->syncwait.kmap = NULL; 859 } 860 861 drm_gem_object_put(queue->syncwait.obj); 862 queue->syncwait.obj = NULL; 863 } 864 865 static void * 866 panthor_queue_get_syncwait_obj(struct panthor_group *group, struct panthor_queue *queue) 867 { 868 struct panthor_device *ptdev = group->ptdev; 869 struct panthor_gem_object *bo; 870 struct iosys_map map; 871 int ret; 872 873 if (queue->syncwait.kmap) { 874 bo = container_of(queue->syncwait.obj, 875 struct panthor_gem_object, base.base); 876 goto out_sync; 877 } 878 879 bo = panthor_vm_get_bo_for_va(group->vm, 880 queue->syncwait.gpu_va, 881 &queue->syncwait.offset); 882 if (drm_WARN_ON(&ptdev->base, IS_ERR_OR_NULL(bo))) 883 goto err_put_syncwait_obj; 884 885 queue->syncwait.obj = &bo->base.base; 886 ret = drm_gem_vmap(queue->syncwait.obj, &map); 887 if (drm_WARN_ON(&ptdev->base, ret)) 888 goto err_put_syncwait_obj; 889 890 queue->syncwait.kmap = map.vaddr; 891 if (drm_WARN_ON(&ptdev->base, !queue->syncwait.kmap)) 892 goto err_put_syncwait_obj; 893 894 out_sync: 895 /* Make sure the CPU caches are invalidated before the seqno is read. 896 * drm_gem_shmem_sync() is a NOP if map_wc=true, so no need to check 897 * it here. 898 */ 899 panthor_gem_sync(&bo->base.base, queue->syncwait.offset, 900 queue->syncwait.sync64 ? 901 sizeof(struct panthor_syncobj_64b) : 902 sizeof(struct panthor_syncobj_32b), 903 DRM_PANTHOR_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE); 904 905 return queue->syncwait.kmap + queue->syncwait.offset; 906 907 err_put_syncwait_obj: 908 panthor_queue_put_syncwait_obj(queue); 909 return NULL; 910 } 911 912 static void group_free_queue(struct panthor_group *group, struct panthor_queue *queue) 913 { 914 if (IS_ERR_OR_NULL(queue)) 915 return; 916 917 /* Disable the timeout before tearing down drm_sched components. */ 918 disable_delayed_work_sync(&queue->timeout.work); 919 920 if (queue->entity.fence_context) 921 drm_sched_entity_destroy(&queue->entity); 922 923 if (queue->scheduler.ops) 924 drm_sched_fini(&queue->scheduler); 925 926 kfree(queue->name); 927 928 panthor_queue_put_syncwait_obj(queue); 929 930 panthor_kernel_bo_destroy(queue->ringbuf); 931 panthor_kernel_bo_destroy(queue->iface.mem); 932 panthor_kernel_bo_destroy(queue->profiling.slots); 933 934 /* Release the last_fence we were holding, if any. */ 935 dma_fence_put(queue->fence_ctx.last_fence); 936 937 kfree(queue); 938 } 939 940 static void group_release_work(struct work_struct *work) 941 { 942 struct panthor_group *group = container_of(work, 943 struct panthor_group, 944 release_work); 945 u32 i; 946 947 /* dma-fences may still be accessing group->queues under rcu lock. */ 948 synchronize_rcu(); 949 950 for (i = 0; i < group->queue_count; i++) 951 group_free_queue(group, group->queues[i]); 952 953 panthor_kernel_bo_destroy(group->suspend_buf); 954 panthor_kernel_bo_destroy(group->protm_suspend_buf); 955 panthor_kernel_bo_destroy(group->syncobjs); 956 957 panthor_vm_put(group->vm); 958 kfree(group); 959 } 960 961 static void group_release(struct kref *kref) 962 { 963 struct panthor_group *group = container_of(kref, 964 struct panthor_group, 965 refcount); 966 struct panthor_device *ptdev = group->ptdev; 967 968 drm_WARN_ON(&ptdev->base, group->csg_id >= 0); 969 drm_WARN_ON(&ptdev->base, !list_empty(&group->run_node)); 970 drm_WARN_ON(&ptdev->base, !list_empty(&group->wait_node)); 971 972 queue_work(panthor_cleanup_wq, &group->release_work); 973 } 974 975 static void group_put(struct panthor_group *group) 976 { 977 if (group) 978 kref_put(&group->refcount, group_release); 979 } 980 981 static struct panthor_group * 982 group_get(struct panthor_group *group) 983 { 984 if (group) 985 kref_get(&group->refcount); 986 987 return group; 988 } 989 990 /** 991 * group_bind_locked() - Bind a group to a group slot 992 * @group: Group. 993 * @csg_id: Slot. 994 * 995 * Return: 0 on success, a negative error code otherwise. 996 */ 997 static int 998 group_bind_locked(struct panthor_group *group, u32 csg_id) 999 { 1000 struct panthor_device *ptdev = group->ptdev; 1001 struct panthor_csg_slot *csg_slot; 1002 int ret; 1003 1004 lockdep_assert_held(&ptdev->scheduler->lock); 1005 1006 if (drm_WARN_ON(&ptdev->base, group->csg_id != -1 || csg_id >= MAX_CSGS || 1007 ptdev->scheduler->csg_slots[csg_id].group)) 1008 return -EINVAL; 1009 1010 ret = panthor_vm_active(group->vm); 1011 if (ret) 1012 return ret; 1013 1014 csg_slot = &ptdev->scheduler->csg_slots[csg_id]; 1015 group_get(group); 1016 group->csg_id = csg_id; 1017 1018 /* Dummy doorbell allocation: doorbell is assigned to the group and 1019 * all queues use the same doorbell. 1020 * 1021 * TODO: Implement LRU-based doorbell assignment, so the most often 1022 * updated queues get their own doorbell, thus avoiding useless checks 1023 * on queues belonging to the same group that are rarely updated. 1024 */ 1025 for (u32 i = 0; i < group->queue_count; i++) 1026 group->queues[i]->doorbell_id = csg_id + 1; 1027 1028 csg_slot->group = group; 1029 1030 return 0; 1031 } 1032 1033 /** 1034 * group_unbind_locked() - Unbind a group from a slot. 1035 * @group: Group to unbind. 1036 * 1037 * Return: 0 on success, a negative error code otherwise. 1038 */ 1039 static int 1040 group_unbind_locked(struct panthor_group *group) 1041 { 1042 struct panthor_device *ptdev = group->ptdev; 1043 struct panthor_csg_slot *slot; 1044 1045 lockdep_assert_held(&ptdev->scheduler->lock); 1046 1047 if (drm_WARN_ON(&ptdev->base, group->csg_id < 0 || group->csg_id >= MAX_CSGS)) 1048 return -EINVAL; 1049 1050 if (drm_WARN_ON(&ptdev->base, group->state == PANTHOR_CS_GROUP_ACTIVE)) 1051 return -EINVAL; 1052 1053 slot = &ptdev->scheduler->csg_slots[group->csg_id]; 1054 panthor_vm_idle(group->vm); 1055 group->csg_id = -1; 1056 1057 /* Tiler OOM events will be re-issued next time the group is scheduled. */ 1058 atomic_set(&group->tiler_oom, 0); 1059 cancel_work(&group->tiler_oom_work); 1060 1061 for (u32 i = 0; i < group->queue_count; i++) 1062 group->queues[i]->doorbell_id = -1; 1063 1064 slot->group = NULL; 1065 1066 group_put(group); 1067 return 0; 1068 } 1069 1070 static bool 1071 group_is_idle(struct panthor_group *group) 1072 { 1073 u32 inactive_queues = group->idle_queues | group->blocked_queues; 1074 1075 return hweight32(inactive_queues) == group->queue_count; 1076 } 1077 1078 static bool 1079 group_can_run(struct panthor_group *group) 1080 { 1081 return group->state != PANTHOR_CS_GROUP_TERMINATED && 1082 group->state != PANTHOR_CS_GROUP_UNKNOWN_STATE && 1083 !group->destroyed && group->fatal_queues == 0 && 1084 !group->timedout; 1085 } 1086 1087 static bool 1088 queue_timeout_is_suspended(struct panthor_queue *queue) 1089 { 1090 /* When running, the remaining time is set to MAX_SCHEDULE_TIMEOUT. */ 1091 return queue->timeout.remaining != MAX_SCHEDULE_TIMEOUT; 1092 } 1093 1094 static void 1095 queue_reset_timeout_locked(struct panthor_queue *queue) 1096 { 1097 lockdep_assert_held(&queue->fence_ctx.lock); 1098 1099 if (!queue_timeout_is_suspended(queue)) { 1100 mod_delayed_work(queue->scheduler.timeout_wq, 1101 &queue->timeout.work, 1102 msecs_to_jiffies(JOB_TIMEOUT_MS)); 1103 } 1104 } 1105 1106 static void 1107 queue_suspend_timeout_locked(struct panthor_queue *queue) 1108 { 1109 unsigned long qtimeout, now; 1110 struct panthor_group *group; 1111 struct panthor_job *job; 1112 bool timer_was_active; 1113 1114 lockdep_assert_held(&queue->fence_ctx.lock); 1115 1116 /* Already suspended, nothing to do. */ 1117 if (queue_timeout_is_suspended(queue)) 1118 return; 1119 1120 job = list_first_entry_or_null(&queue->fence_ctx.in_flight_jobs, 1121 struct panthor_job, node); 1122 group = job ? job->group : NULL; 1123 1124 /* If the queue is blocked and the group is idle, we want the timer to 1125 * keep running because the group can't be unblocked by other queues, 1126 * so it has to come from an external source, and we want to timebox 1127 * this external signalling. 1128 */ 1129 if (group && group_can_run(group) && 1130 (group->blocked_queues & BIT(job->queue_idx)) && 1131 group_is_idle(group)) 1132 return; 1133 1134 now = jiffies; 1135 qtimeout = queue->timeout.work.timer.expires; 1136 1137 /* Cancel the timer. */ 1138 timer_was_active = cancel_delayed_work(&queue->timeout.work); 1139 if (!timer_was_active || !job) 1140 queue->timeout.remaining = msecs_to_jiffies(JOB_TIMEOUT_MS); 1141 else if (time_after(qtimeout, now)) 1142 queue->timeout.remaining = qtimeout - now; 1143 else 1144 queue->timeout.remaining = 0; 1145 1146 if (WARN_ON_ONCE(queue->timeout.remaining > msecs_to_jiffies(JOB_TIMEOUT_MS))) 1147 queue->timeout.remaining = msecs_to_jiffies(JOB_TIMEOUT_MS); 1148 } 1149 1150 static void 1151 queue_suspend_timeout(struct panthor_queue *queue) 1152 { 1153 spin_lock(&queue->fence_ctx.lock); 1154 queue_suspend_timeout_locked(queue); 1155 spin_unlock(&queue->fence_ctx.lock); 1156 } 1157 1158 static void 1159 queue_resume_timeout(struct panthor_queue *queue) 1160 { 1161 spin_lock(&queue->fence_ctx.lock); 1162 1163 if (queue_timeout_is_suspended(queue)) { 1164 mod_delayed_work(queue->scheduler.timeout_wq, 1165 &queue->timeout.work, 1166 queue->timeout.remaining); 1167 1168 queue->timeout.remaining = MAX_SCHEDULE_TIMEOUT; 1169 } 1170 1171 spin_unlock(&queue->fence_ctx.lock); 1172 } 1173 1174 /** 1175 * cs_slot_prog_locked() - Program a queue slot 1176 * @ptdev: Device. 1177 * @csg_id: Group slot ID. 1178 * @cs_id: Queue slot ID. 1179 * 1180 * Program a queue slot with the queue information so things can start being 1181 * executed on this queue. 1182 * 1183 * The group slot must have a group bound to it already (group_bind_locked()). 1184 */ 1185 static void 1186 cs_slot_prog_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id) 1187 { 1188 struct panthor_queue *queue = ptdev->scheduler->csg_slots[csg_id].group->queues[cs_id]; 1189 struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id); 1190 1191 lockdep_assert_held(&ptdev->scheduler->lock); 1192 1193 queue->iface.input->extract = queue->iface.output->extract; 1194 drm_WARN_ON(&ptdev->base, queue->iface.input->insert < queue->iface.input->extract); 1195 1196 cs_iface->input->ringbuf_base = panthor_kernel_bo_gpuva(queue->ringbuf); 1197 cs_iface->input->ringbuf_size = panthor_kernel_bo_size(queue->ringbuf); 1198 cs_iface->input->ringbuf_input = queue->iface.input_fw_va; 1199 cs_iface->input->ringbuf_output = queue->iface.output_fw_va; 1200 cs_iface->input->config = CS_CONFIG_PRIORITY(queue->priority) | 1201 CS_CONFIG_DOORBELL(queue->doorbell_id); 1202 cs_iface->input->ack_irq_mask = ~0; 1203 panthor_fw_update_reqs(cs_iface, req, 1204 CS_IDLE_SYNC_WAIT | 1205 CS_IDLE_EMPTY | 1206 CS_STATE_START, 1207 CS_IDLE_SYNC_WAIT | 1208 CS_IDLE_EMPTY | 1209 CS_STATE_MASK); 1210 if (queue->iface.input->insert != queue->iface.input->extract) 1211 queue_resume_timeout(queue); 1212 } 1213 1214 /** 1215 * cs_slot_reset_locked() - Reset a queue slot 1216 * @ptdev: Device. 1217 * @csg_id: Group slot. 1218 * @cs_id: Queue slot. 1219 * 1220 * Change the queue slot state to STOP and suspend the queue timeout if 1221 * the queue is not blocked. 1222 * 1223 * The group slot must have a group bound to it (group_bind_locked()). 1224 */ 1225 static int 1226 cs_slot_reset_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id) 1227 { 1228 struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id); 1229 struct panthor_group *group = ptdev->scheduler->csg_slots[csg_id].group; 1230 struct panthor_queue *queue = group->queues[cs_id]; 1231 1232 lockdep_assert_held(&ptdev->scheduler->lock); 1233 1234 panthor_fw_update_reqs(cs_iface, req, 1235 CS_STATE_STOP, 1236 CS_STATE_MASK); 1237 1238 queue_suspend_timeout(queue); 1239 1240 return 0; 1241 } 1242 1243 /** 1244 * csg_slot_sync_priority_locked() - Synchronize the group slot priority 1245 * @ptdev: Device. 1246 * @csg_id: Group slot ID. 1247 * 1248 * Group slot priority update happens asynchronously. When we receive a 1249 * %CSG_ENDPOINT_CONFIG, we know the update is effective, and can 1250 * reflect it to our panthor_csg_slot object. 1251 */ 1252 static void 1253 csg_slot_sync_priority_locked(struct panthor_device *ptdev, u32 csg_id) 1254 { 1255 struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id]; 1256 struct panthor_fw_csg_iface *csg_iface; 1257 u64 endpoint_req; 1258 1259 lockdep_assert_held(&ptdev->scheduler->lock); 1260 1261 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 1262 endpoint_req = panthor_fw_csg_endpoint_req_get(ptdev, csg_iface); 1263 csg_slot->priority = CSG_EP_REQ_PRIORITY_GET(endpoint_req); 1264 } 1265 1266 /** 1267 * cs_slot_sync_queue_state_locked() - Synchronize the queue slot priority 1268 * @ptdev: Device. 1269 * @csg_id: Group slot. 1270 * @cs_id: Queue slot. 1271 * 1272 * Queue state is updated on group suspend or STATUS_UPDATE event. 1273 */ 1274 static void 1275 cs_slot_sync_queue_state_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id) 1276 { 1277 struct panthor_group *group = ptdev->scheduler->csg_slots[csg_id].group; 1278 struct panthor_queue *queue = group->queues[cs_id]; 1279 struct panthor_fw_cs_iface *cs_iface = 1280 panthor_fw_get_cs_iface(group->ptdev, csg_id, cs_id); 1281 1282 u32 status_wait_cond; 1283 1284 switch (cs_iface->output->status_blocked_reason) { 1285 case CS_STATUS_BLOCKED_REASON_UNBLOCKED: 1286 if (queue->iface.input->insert == queue->iface.output->extract && 1287 cs_iface->output->status_scoreboards == 0) 1288 group->idle_queues |= BIT(cs_id); 1289 break; 1290 1291 case CS_STATUS_BLOCKED_REASON_SYNC_WAIT: 1292 if (list_empty(&group->wait_node)) { 1293 list_move_tail(&group->wait_node, 1294 &group->ptdev->scheduler->groups.waiting); 1295 } 1296 1297 /* The queue is only blocked if there's no deferred operation 1298 * pending, which can be checked through the scoreboard status. 1299 */ 1300 if (!cs_iface->output->status_scoreboards) 1301 group->blocked_queues |= BIT(cs_id); 1302 1303 queue->syncwait.gpu_va = cs_iface->output->status_wait_sync_ptr; 1304 queue->syncwait.ref = cs_iface->output->status_wait_sync_value; 1305 status_wait_cond = cs_iface->output->status_wait & CS_STATUS_WAIT_SYNC_COND_MASK; 1306 queue->syncwait.gt = status_wait_cond == CS_STATUS_WAIT_SYNC_COND_GT; 1307 if (cs_iface->output->status_wait & CS_STATUS_WAIT_SYNC_64B) { 1308 u64 sync_val_hi = cs_iface->output->status_wait_sync_value_hi; 1309 1310 queue->syncwait.sync64 = true; 1311 queue->syncwait.ref |= sync_val_hi << 32; 1312 } else { 1313 queue->syncwait.sync64 = false; 1314 } 1315 break; 1316 1317 default: 1318 /* Other reasons are not blocking. Consider the queue as runnable 1319 * in those cases. 1320 */ 1321 break; 1322 } 1323 } 1324 1325 static void 1326 csg_slot_sync_queues_state_locked(struct panthor_device *ptdev, u32 csg_id) 1327 { 1328 struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id]; 1329 struct panthor_group *group = csg_slot->group; 1330 u32 i; 1331 1332 lockdep_assert_held(&ptdev->scheduler->lock); 1333 1334 group->idle_queues = 0; 1335 group->blocked_queues = 0; 1336 1337 for (i = 0; i < group->queue_count; i++) { 1338 if (group->queues[i]) 1339 cs_slot_sync_queue_state_locked(ptdev, csg_id, i); 1340 } 1341 } 1342 1343 static void 1344 csg_slot_sync_state_locked(struct panthor_device *ptdev, u32 csg_id) 1345 { 1346 struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id]; 1347 struct panthor_fw_csg_iface *csg_iface; 1348 struct panthor_group *group; 1349 enum panthor_group_state new_state, old_state; 1350 u32 csg_state; 1351 1352 lockdep_assert_held(&ptdev->scheduler->lock); 1353 1354 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 1355 group = csg_slot->group; 1356 1357 if (!group) 1358 return; 1359 1360 old_state = group->state; 1361 csg_state = csg_iface->output->ack & CSG_STATE_MASK; 1362 switch (csg_state) { 1363 case CSG_STATE_START: 1364 case CSG_STATE_RESUME: 1365 new_state = PANTHOR_CS_GROUP_ACTIVE; 1366 break; 1367 case CSG_STATE_TERMINATE: 1368 new_state = PANTHOR_CS_GROUP_TERMINATED; 1369 break; 1370 case CSG_STATE_SUSPEND: 1371 new_state = PANTHOR_CS_GROUP_SUSPENDED; 1372 break; 1373 default: 1374 /* The unknown state might be caused by a FW state corruption, 1375 * which means the group metadata can't be trusted anymore, and 1376 * the SUSPEND operation might propagate the corruption to the 1377 * suspend buffers. Flag the group state as unknown to make 1378 * sure it's unusable after that point. 1379 */ 1380 drm_err(&ptdev->base, "Invalid state on CSG %d (state=%d)", 1381 csg_id, csg_state); 1382 new_state = PANTHOR_CS_GROUP_UNKNOWN_STATE; 1383 break; 1384 } 1385 1386 if (old_state == new_state) 1387 return; 1388 1389 /* The unknown state might be caused by a FW issue, reset the FW to 1390 * take a fresh start. 1391 */ 1392 if (new_state == PANTHOR_CS_GROUP_UNKNOWN_STATE) 1393 panthor_device_schedule_reset(ptdev); 1394 1395 if (new_state == PANTHOR_CS_GROUP_SUSPENDED) 1396 csg_slot_sync_queues_state_locked(ptdev, csg_id); 1397 1398 if (old_state == PANTHOR_CS_GROUP_ACTIVE) { 1399 u32 i; 1400 1401 /* Reset the queue slots so we start from a clean 1402 * state when starting/resuming a new group on this 1403 * CSG slot. No wait needed here, and no ringbell 1404 * either, since the CS slot will only be re-used 1405 * on the next CSG start operation. 1406 */ 1407 for (i = 0; i < group->queue_count; i++) { 1408 if (group->queues[i]) 1409 cs_slot_reset_locked(ptdev, csg_id, i); 1410 } 1411 } 1412 1413 group->state = new_state; 1414 } 1415 1416 static int 1417 csg_slot_prog_locked(struct panthor_device *ptdev, u32 csg_id, u32 priority) 1418 { 1419 struct panthor_fw_csg_iface *csg_iface; 1420 struct panthor_csg_slot *csg_slot; 1421 struct panthor_group *group; 1422 u32 queue_mask = 0, i; 1423 u64 endpoint_req; 1424 1425 lockdep_assert_held(&ptdev->scheduler->lock); 1426 1427 if (priority > MAX_CSG_PRIO) 1428 return -EINVAL; 1429 1430 if (drm_WARN_ON(&ptdev->base, csg_id >= MAX_CSGS)) 1431 return -EINVAL; 1432 1433 csg_slot = &ptdev->scheduler->csg_slots[csg_id]; 1434 group = csg_slot->group; 1435 if (!group || group->state == PANTHOR_CS_GROUP_ACTIVE) 1436 return 0; 1437 1438 csg_iface = panthor_fw_get_csg_iface(group->ptdev, csg_id); 1439 1440 for (i = 0; i < group->queue_count; i++) { 1441 if (group->queues[i]) { 1442 cs_slot_prog_locked(ptdev, csg_id, i); 1443 queue_mask |= BIT(i); 1444 } 1445 } 1446 1447 csg_iface->input->allow_compute = group->compute_core_mask; 1448 csg_iface->input->allow_fragment = group->fragment_core_mask; 1449 csg_iface->input->allow_other = group->tiler_core_mask; 1450 endpoint_req = CSG_EP_REQ_COMPUTE(group->max_compute_cores) | 1451 CSG_EP_REQ_FRAGMENT(group->max_fragment_cores) | 1452 CSG_EP_REQ_TILER(group->max_tiler_cores) | 1453 CSG_EP_REQ_PRIORITY(priority); 1454 panthor_fw_csg_endpoint_req_set(ptdev, csg_iface, endpoint_req); 1455 1456 csg_iface->input->config = panthor_vm_as(group->vm); 1457 1458 if (group->suspend_buf) 1459 csg_iface->input->suspend_buf = panthor_kernel_bo_gpuva(group->suspend_buf); 1460 else 1461 csg_iface->input->suspend_buf = 0; 1462 1463 if (group->protm_suspend_buf) { 1464 csg_iface->input->protm_suspend_buf = 1465 panthor_kernel_bo_gpuva(group->protm_suspend_buf); 1466 } else { 1467 csg_iface->input->protm_suspend_buf = 0; 1468 } 1469 1470 csg_iface->input->ack_irq_mask = ~0; 1471 panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, queue_mask); 1472 return 0; 1473 } 1474 1475 static void 1476 cs_slot_process_fatal_event_locked(struct panthor_device *ptdev, 1477 u32 csg_id, u32 cs_id) 1478 { 1479 struct panthor_scheduler *sched = ptdev->scheduler; 1480 struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id]; 1481 struct panthor_group *group = csg_slot->group; 1482 struct panthor_fw_cs_iface *cs_iface; 1483 u32 fatal; 1484 u64 info; 1485 1486 lockdep_assert_held(&sched->lock); 1487 1488 cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id); 1489 fatal = cs_iface->output->fatal; 1490 info = cs_iface->output->fatal_info; 1491 1492 if (group) { 1493 drm_warn(&ptdev->base, "CS_FATAL: pid=%d, comm=%s\n", 1494 group->task_info.pid, group->task_info.comm); 1495 1496 group->fatal_queues |= BIT(cs_id); 1497 } 1498 1499 if (CS_EXCEPTION_TYPE(fatal) == DRM_PANTHOR_EXCEPTION_CS_UNRECOVERABLE) { 1500 /* If this exception is unrecoverable, queue a reset, and make 1501 * sure we stop scheduling groups until the reset has happened. 1502 */ 1503 panthor_device_schedule_reset(ptdev); 1504 cancel_delayed_work(&sched->tick_work); 1505 } else { 1506 sched_queue_delayed_work(sched, tick, 0); 1507 } 1508 1509 drm_warn(&ptdev->base, 1510 "CSG slot %d CS slot: %d\n" 1511 "CS_FATAL.EXCEPTION_TYPE: 0x%x (%s)\n" 1512 "CS_FATAL.EXCEPTION_DATA: 0x%x\n" 1513 "CS_FATAL_INFO.EXCEPTION_DATA: 0x%llx\n", 1514 csg_id, cs_id, 1515 (unsigned int)CS_EXCEPTION_TYPE(fatal), 1516 panthor_exception_name(ptdev, CS_EXCEPTION_TYPE(fatal)), 1517 (unsigned int)CS_EXCEPTION_DATA(fatal), 1518 info); 1519 } 1520 1521 static void 1522 cs_slot_process_fault_event_locked(struct panthor_device *ptdev, 1523 u32 csg_id, u32 cs_id) 1524 { 1525 struct panthor_scheduler *sched = ptdev->scheduler; 1526 struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id]; 1527 struct panthor_group *group = csg_slot->group; 1528 struct panthor_queue *queue = group && cs_id < group->queue_count ? 1529 group->queues[cs_id] : NULL; 1530 struct panthor_fw_cs_iface *cs_iface; 1531 u32 fault; 1532 u64 info; 1533 1534 lockdep_assert_held(&sched->lock); 1535 1536 cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id); 1537 fault = cs_iface->output->fault; 1538 info = cs_iface->output->fault_info; 1539 1540 if (queue) { 1541 u64 cs_extract = queue->iface.output->extract; 1542 struct panthor_job *job; 1543 1544 spin_lock(&queue->fence_ctx.lock); 1545 list_for_each_entry(job, &queue->fence_ctx.in_flight_jobs, node) { 1546 if (cs_extract >= job->ringbuf.end) 1547 continue; 1548 1549 if (cs_extract < job->ringbuf.start) 1550 break; 1551 1552 dma_fence_set_error(job->done_fence, -EINVAL); 1553 } 1554 spin_unlock(&queue->fence_ctx.lock); 1555 } 1556 1557 if (group) { 1558 drm_warn(&ptdev->base, "CS_FAULT: pid=%d, comm=%s\n", 1559 group->task_info.pid, group->task_info.comm); 1560 } 1561 1562 drm_warn(&ptdev->base, 1563 "CSG slot %d CS slot: %d\n" 1564 "CS_FAULT.EXCEPTION_TYPE: 0x%x (%s)\n" 1565 "CS_FAULT.EXCEPTION_DATA: 0x%x\n" 1566 "CS_FAULT_INFO.EXCEPTION_DATA: 0x%llx\n", 1567 csg_id, cs_id, 1568 (unsigned int)CS_EXCEPTION_TYPE(fault), 1569 panthor_exception_name(ptdev, CS_EXCEPTION_TYPE(fault)), 1570 (unsigned int)CS_EXCEPTION_DATA(fault), 1571 info); 1572 } 1573 1574 static int group_process_tiler_oom(struct panthor_group *group, u32 cs_id) 1575 { 1576 struct panthor_device *ptdev = group->ptdev; 1577 struct panthor_scheduler *sched = ptdev->scheduler; 1578 u32 renderpasses_in_flight, pending_frag_count; 1579 struct panthor_heap_pool *heaps = NULL; 1580 u64 heap_address, new_chunk_va = 0; 1581 u32 vt_start, vt_end, frag_end; 1582 int ret, csg_id; 1583 1584 mutex_lock(&sched->lock); 1585 csg_id = group->csg_id; 1586 if (csg_id >= 0) { 1587 struct panthor_fw_cs_iface *cs_iface; 1588 1589 cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id); 1590 heaps = panthor_vm_get_heap_pool(group->vm, false); 1591 heap_address = cs_iface->output->heap_address; 1592 vt_start = cs_iface->output->heap_vt_start; 1593 vt_end = cs_iface->output->heap_vt_end; 1594 frag_end = cs_iface->output->heap_frag_end; 1595 renderpasses_in_flight = vt_start - frag_end; 1596 pending_frag_count = vt_end - frag_end; 1597 } 1598 mutex_unlock(&sched->lock); 1599 1600 /* The group got scheduled out, we stop here. We will get a new tiler OOM event 1601 * when it's scheduled again. 1602 */ 1603 if (unlikely(csg_id < 0)) 1604 return 0; 1605 1606 if (IS_ERR(heaps) || frag_end > vt_end || vt_end >= vt_start) { 1607 ret = -EINVAL; 1608 } else { 1609 /* We do the allocation without holding the scheduler lock to avoid 1610 * blocking the scheduling. 1611 */ 1612 ret = panthor_heap_grow(heaps, heap_address, 1613 renderpasses_in_flight, 1614 pending_frag_count, &new_chunk_va); 1615 } 1616 1617 /* If the heap context doesn't have memory for us, we want to let the 1618 * FW try to reclaim memory by waiting for fragment jobs to land or by 1619 * executing the tiler OOM exception handler, which is supposed to 1620 * implement incremental rendering. 1621 */ 1622 if (ret && ret != -ENOMEM) { 1623 drm_warn(&ptdev->base, "Failed to extend the tiler heap\n"); 1624 group->fatal_queues |= BIT(cs_id); 1625 sched_queue_delayed_work(sched, tick, 0); 1626 goto out_put_heap_pool; 1627 } 1628 1629 mutex_lock(&sched->lock); 1630 csg_id = group->csg_id; 1631 if (csg_id >= 0) { 1632 struct panthor_fw_csg_iface *csg_iface; 1633 struct panthor_fw_cs_iface *cs_iface; 1634 1635 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 1636 cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id); 1637 1638 cs_iface->input->heap_start = new_chunk_va; 1639 cs_iface->input->heap_end = new_chunk_va; 1640 panthor_fw_update_reqs(cs_iface, req, cs_iface->output->ack, CS_TILER_OOM); 1641 panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, BIT(cs_id)); 1642 panthor_fw_ring_csg_doorbells(ptdev, BIT(csg_id)); 1643 } 1644 mutex_unlock(&sched->lock); 1645 1646 /* We allocated a chunck, but couldn't link it to the heap 1647 * context because the group was scheduled out while we were 1648 * allocating memory. We need to return this chunk to the heap. 1649 */ 1650 if (unlikely(csg_id < 0 && new_chunk_va)) 1651 panthor_heap_return_chunk(heaps, heap_address, new_chunk_va); 1652 1653 ret = 0; 1654 1655 out_put_heap_pool: 1656 panthor_heap_pool_put(heaps); 1657 return ret; 1658 } 1659 1660 static void group_tiler_oom_work(struct work_struct *work) 1661 { 1662 struct panthor_group *group = 1663 container_of(work, struct panthor_group, tiler_oom_work); 1664 u32 tiler_oom = atomic_xchg(&group->tiler_oom, 0); 1665 1666 while (tiler_oom) { 1667 u32 cs_id = ffs(tiler_oom) - 1; 1668 1669 group_process_tiler_oom(group, cs_id); 1670 tiler_oom &= ~BIT(cs_id); 1671 } 1672 1673 group_put(group); 1674 } 1675 1676 static void 1677 cs_slot_process_tiler_oom_event_locked(struct panthor_device *ptdev, 1678 u32 csg_id, u32 cs_id) 1679 { 1680 struct panthor_scheduler *sched = ptdev->scheduler; 1681 struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id]; 1682 struct panthor_group *group = csg_slot->group; 1683 1684 lockdep_assert_held(&sched->lock); 1685 1686 if (drm_WARN_ON(&ptdev->base, !group)) 1687 return; 1688 1689 atomic_or(BIT(cs_id), &group->tiler_oom); 1690 1691 /* We don't use group_queue_work() here because we want to queue the 1692 * work item to the heap_alloc_wq. 1693 */ 1694 group_get(group); 1695 if (!queue_work(sched->heap_alloc_wq, &group->tiler_oom_work)) 1696 group_put(group); 1697 } 1698 1699 static bool cs_slot_process_irq_locked(struct panthor_device *ptdev, 1700 u32 csg_id, u32 cs_id) 1701 { 1702 struct panthor_fw_cs_iface *cs_iface; 1703 u32 req, ack, events; 1704 1705 lockdep_assert_held(&ptdev->scheduler->lock); 1706 1707 cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id); 1708 req = cs_iface->input->req; 1709 ack = cs_iface->output->ack; 1710 events = (req ^ ack) & CS_EVT_MASK; 1711 1712 if (events & CS_FATAL) 1713 cs_slot_process_fatal_event_locked(ptdev, csg_id, cs_id); 1714 1715 if (events & CS_FAULT) 1716 cs_slot_process_fault_event_locked(ptdev, csg_id, cs_id); 1717 1718 if (events & CS_TILER_OOM) 1719 cs_slot_process_tiler_oom_event_locked(ptdev, csg_id, cs_id); 1720 1721 /* We don't acknowledge the TILER_OOM event since its handling is 1722 * deferred to a separate work. 1723 */ 1724 panthor_fw_update_reqs(cs_iface, req, ack, CS_FATAL | CS_FAULT); 1725 1726 return (events & (CS_FAULT | CS_TILER_OOM)) != 0; 1727 } 1728 1729 static void csg_slot_process_idle_event_locked(struct panthor_device *ptdev, u32 csg_id) 1730 { 1731 struct panthor_scheduler *sched = ptdev->scheduler; 1732 1733 lockdep_assert_held(&sched->lock); 1734 1735 sched->might_have_idle_groups = true; 1736 1737 /* Schedule a tick so we can evict idle groups and schedule non-idle 1738 * ones. This will also update runtime PM and devfreq busy/idle states, 1739 * so the device can lower its frequency or get suspended. 1740 */ 1741 sched_queue_delayed_work(sched, tick, 0); 1742 } 1743 1744 static void csg_slot_sync_update_locked(struct panthor_device *ptdev, 1745 u32 csg_id) 1746 { 1747 struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id]; 1748 struct panthor_group *group = csg_slot->group; 1749 1750 lockdep_assert_held(&ptdev->scheduler->lock); 1751 1752 if (group) 1753 group_queue_work(group, sync_upd); 1754 1755 sched_queue_work(ptdev->scheduler, sync_upd); 1756 } 1757 1758 static void 1759 csg_slot_process_progress_timer_event_locked(struct panthor_device *ptdev, u32 csg_id) 1760 { 1761 struct panthor_scheduler *sched = ptdev->scheduler; 1762 struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id]; 1763 struct panthor_group *group = csg_slot->group; 1764 1765 lockdep_assert_held(&sched->lock); 1766 1767 group = csg_slot->group; 1768 if (!drm_WARN_ON(&ptdev->base, !group)) { 1769 drm_warn(&ptdev->base, "CSG_PROGRESS_TIMER_EVENT: pid=%d, comm=%s\n", 1770 group->task_info.pid, group->task_info.comm); 1771 1772 group->timedout = true; 1773 } 1774 1775 drm_warn(&ptdev->base, "CSG slot %d progress timeout\n", csg_id); 1776 1777 sched_queue_delayed_work(sched, tick, 0); 1778 } 1779 1780 static void sched_process_csg_irq_locked(struct panthor_device *ptdev, u32 csg_id) 1781 { 1782 u32 req, ack, cs_irq_req, cs_irq_ack, cs_irqs, csg_events; 1783 struct panthor_fw_csg_iface *csg_iface; 1784 u32 ring_cs_db_mask = 0; 1785 1786 lockdep_assert_held(&ptdev->scheduler->lock); 1787 1788 if (drm_WARN_ON(&ptdev->base, csg_id >= ptdev->scheduler->csg_slot_count)) 1789 return; 1790 1791 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 1792 req = READ_ONCE(csg_iface->input->req); 1793 ack = READ_ONCE(csg_iface->output->ack); 1794 cs_irq_req = READ_ONCE(csg_iface->output->cs_irq_req); 1795 cs_irq_ack = READ_ONCE(csg_iface->input->cs_irq_ack); 1796 csg_events = (req ^ ack) & CSG_EVT_MASK; 1797 1798 /* There may not be any pending CSG/CS interrupts to process */ 1799 if (req == ack && cs_irq_req == cs_irq_ack) 1800 return; 1801 1802 /* Immediately set IRQ_ACK bits to be same as the IRQ_REQ bits before 1803 * examining the CS_ACK & CS_REQ bits. This would ensure that Host 1804 * doesn't miss an interrupt for the CS in the race scenario where 1805 * whilst Host is servicing an interrupt for the CS, firmware sends 1806 * another interrupt for that CS. 1807 */ 1808 csg_iface->input->cs_irq_ack = cs_irq_req; 1809 1810 panthor_fw_update_reqs(csg_iface, req, ack, 1811 CSG_SYNC_UPDATE | 1812 CSG_IDLE | 1813 CSG_PROGRESS_TIMER_EVENT); 1814 1815 if (csg_events & CSG_IDLE) 1816 csg_slot_process_idle_event_locked(ptdev, csg_id); 1817 1818 if (csg_events & CSG_PROGRESS_TIMER_EVENT) 1819 csg_slot_process_progress_timer_event_locked(ptdev, csg_id); 1820 1821 cs_irqs = cs_irq_req ^ cs_irq_ack; 1822 while (cs_irqs) { 1823 u32 cs_id = ffs(cs_irqs) - 1; 1824 1825 if (cs_slot_process_irq_locked(ptdev, csg_id, cs_id)) 1826 ring_cs_db_mask |= BIT(cs_id); 1827 1828 cs_irqs &= ~BIT(cs_id); 1829 } 1830 1831 if (csg_events & CSG_SYNC_UPDATE) 1832 csg_slot_sync_update_locked(ptdev, csg_id); 1833 1834 if (ring_cs_db_mask) 1835 panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, ring_cs_db_mask); 1836 1837 panthor_fw_ring_csg_doorbells(ptdev, BIT(csg_id)); 1838 } 1839 1840 static void sched_process_idle_event_locked(struct panthor_device *ptdev) 1841 { 1842 struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev); 1843 1844 lockdep_assert_held(&ptdev->scheduler->lock); 1845 1846 /* Acknowledge the idle event and schedule a tick. */ 1847 panthor_fw_update_reqs(glb_iface, req, glb_iface->output->ack, GLB_IDLE); 1848 sched_queue_delayed_work(ptdev->scheduler, tick, 0); 1849 } 1850 1851 /** 1852 * sched_process_global_irq_locked() - Process the scheduling part of a global IRQ 1853 * @ptdev: Device. 1854 */ 1855 static void sched_process_global_irq_locked(struct panthor_device *ptdev) 1856 { 1857 struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev); 1858 u32 req, ack, evts; 1859 1860 lockdep_assert_held(&ptdev->scheduler->lock); 1861 1862 req = READ_ONCE(glb_iface->input->req); 1863 ack = READ_ONCE(glb_iface->output->ack); 1864 evts = (req ^ ack) & GLB_EVT_MASK; 1865 1866 if (evts & GLB_IDLE) 1867 sched_process_idle_event_locked(ptdev); 1868 } 1869 1870 static void process_fw_events_work(struct work_struct *work) 1871 { 1872 struct panthor_scheduler *sched = container_of(work, struct panthor_scheduler, 1873 fw_events_work); 1874 u32 events = atomic_xchg(&sched->fw_events, 0); 1875 struct panthor_device *ptdev = sched->ptdev; 1876 1877 mutex_lock(&sched->lock); 1878 1879 if (events & JOB_INT_GLOBAL_IF) { 1880 sched_process_global_irq_locked(ptdev); 1881 events &= ~JOB_INT_GLOBAL_IF; 1882 } 1883 1884 while (events) { 1885 u32 csg_id = ffs(events) - 1; 1886 1887 sched_process_csg_irq_locked(ptdev, csg_id); 1888 events &= ~BIT(csg_id); 1889 } 1890 1891 mutex_unlock(&sched->lock); 1892 } 1893 1894 /** 1895 * panthor_sched_report_fw_events() - Report FW events to the scheduler. 1896 */ 1897 void panthor_sched_report_fw_events(struct panthor_device *ptdev, u32 events) 1898 { 1899 if (!ptdev->scheduler) 1900 return; 1901 1902 atomic_or(events, &ptdev->scheduler->fw_events); 1903 sched_queue_work(ptdev->scheduler, fw_events); 1904 } 1905 1906 static const char *fence_get_driver_name(struct dma_fence *fence) 1907 { 1908 return "panthor"; 1909 } 1910 1911 static const char *queue_fence_get_timeline_name(struct dma_fence *fence) 1912 { 1913 return "queue-fence"; 1914 } 1915 1916 static const struct dma_fence_ops panthor_queue_fence_ops = { 1917 .get_driver_name = fence_get_driver_name, 1918 .get_timeline_name = queue_fence_get_timeline_name, 1919 }; 1920 1921 struct panthor_csg_slots_upd_ctx { 1922 u32 update_mask; 1923 u32 timedout_mask; 1924 struct { 1925 u32 value; 1926 u32 mask; 1927 } requests[MAX_CSGS]; 1928 }; 1929 1930 static void csgs_upd_ctx_init(struct panthor_csg_slots_upd_ctx *ctx) 1931 { 1932 memset(ctx, 0, sizeof(*ctx)); 1933 } 1934 1935 static void csgs_upd_ctx_queue_reqs(struct panthor_device *ptdev, 1936 struct panthor_csg_slots_upd_ctx *ctx, 1937 u32 csg_id, u32 value, u32 mask) 1938 { 1939 if (drm_WARN_ON(&ptdev->base, !mask) || 1940 drm_WARN_ON(&ptdev->base, csg_id >= ptdev->scheduler->csg_slot_count)) 1941 return; 1942 1943 ctx->requests[csg_id].value = (ctx->requests[csg_id].value & ~mask) | (value & mask); 1944 ctx->requests[csg_id].mask |= mask; 1945 ctx->update_mask |= BIT(csg_id); 1946 } 1947 1948 static int csgs_upd_ctx_apply_locked(struct panthor_device *ptdev, 1949 struct panthor_csg_slots_upd_ctx *ctx) 1950 { 1951 struct panthor_scheduler *sched = ptdev->scheduler; 1952 u32 update_slots = ctx->update_mask; 1953 1954 lockdep_assert_held(&sched->lock); 1955 1956 if (!ctx->update_mask) 1957 return 0; 1958 1959 while (update_slots) { 1960 struct panthor_fw_csg_iface *csg_iface; 1961 u32 csg_id = ffs(update_slots) - 1; 1962 1963 update_slots &= ~BIT(csg_id); 1964 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 1965 panthor_fw_update_reqs(csg_iface, req, 1966 ctx->requests[csg_id].value, 1967 ctx->requests[csg_id].mask); 1968 } 1969 1970 panthor_fw_ring_csg_doorbells(ptdev, ctx->update_mask); 1971 1972 update_slots = ctx->update_mask; 1973 while (update_slots) { 1974 struct panthor_fw_csg_iface *csg_iface; 1975 u32 csg_id = ffs(update_slots) - 1; 1976 u32 req_mask = ctx->requests[csg_id].mask, acked; 1977 int ret; 1978 1979 update_slots &= ~BIT(csg_id); 1980 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 1981 1982 ret = panthor_fw_csg_wait_acks(ptdev, csg_id, req_mask, &acked, 100); 1983 1984 if (acked & CSG_ENDPOINT_CONFIG) 1985 csg_slot_sync_priority_locked(ptdev, csg_id); 1986 1987 if (acked & CSG_STATE_MASK) 1988 csg_slot_sync_state_locked(ptdev, csg_id); 1989 1990 if (acked & CSG_STATUS_UPDATE) 1991 csg_slot_sync_queues_state_locked(ptdev, csg_id); 1992 1993 if (ret && acked != req_mask && 1994 ((csg_iface->input->req ^ csg_iface->output->ack) & req_mask) != 0) { 1995 drm_err(&ptdev->base, "CSG %d update request timedout", csg_id); 1996 ctx->timedout_mask |= BIT(csg_id); 1997 } 1998 } 1999 2000 if (ctx->timedout_mask) 2001 return -ETIMEDOUT; 2002 2003 return 0; 2004 } 2005 2006 struct panthor_sched_tick_ctx { 2007 struct list_head old_groups[PANTHOR_CSG_PRIORITY_COUNT]; 2008 struct list_head groups[PANTHOR_CSG_PRIORITY_COUNT]; 2009 u32 idle_group_count; 2010 u32 group_count; 2011 struct panthor_vm *vms[MAX_CS_PER_CSG]; 2012 u32 as_count; 2013 bool immediate_tick; 2014 bool stop_tick; 2015 u32 csg_upd_failed_mask; 2016 }; 2017 2018 static bool 2019 tick_ctx_is_full(const struct panthor_scheduler *sched, 2020 const struct panthor_sched_tick_ctx *ctx) 2021 { 2022 return ctx->group_count == sched->csg_slot_count; 2023 } 2024 2025 static void 2026 tick_ctx_pick_groups_from_list(const struct panthor_scheduler *sched, 2027 struct panthor_sched_tick_ctx *ctx, 2028 struct list_head *queue, 2029 bool skip_idle_groups, 2030 bool owned_by_tick_ctx) 2031 { 2032 struct panthor_group *group, *tmp; 2033 2034 if (tick_ctx_is_full(sched, ctx)) 2035 return; 2036 2037 list_for_each_entry_safe(group, tmp, queue, run_node) { 2038 u32 i; 2039 2040 if (!group_can_run(group)) 2041 continue; 2042 2043 if (skip_idle_groups && group_is_idle(group)) 2044 continue; 2045 2046 for (i = 0; i < ctx->as_count; i++) { 2047 if (ctx->vms[i] == group->vm) 2048 break; 2049 } 2050 2051 if (i == ctx->as_count && ctx->as_count == sched->as_slot_count) 2052 continue; 2053 2054 if (!owned_by_tick_ctx) 2055 group_get(group); 2056 2057 ctx->group_count++; 2058 2059 /* If we have more than one active group with the same priority, 2060 * we need to keep ticking to rotate the CSG priority. 2061 */ 2062 if (group_is_idle(group)) 2063 ctx->idle_group_count++; 2064 else if (!list_empty(&ctx->groups[group->priority])) 2065 ctx->stop_tick = false; 2066 2067 list_move_tail(&group->run_node, &ctx->groups[group->priority]); 2068 2069 if (i == ctx->as_count) 2070 ctx->vms[ctx->as_count++] = group->vm; 2071 2072 if (tick_ctx_is_full(sched, ctx)) 2073 return; 2074 } 2075 } 2076 2077 static void 2078 tick_ctx_insert_old_group(struct panthor_scheduler *sched, 2079 struct panthor_sched_tick_ctx *ctx, 2080 struct panthor_group *group) 2081 { 2082 struct panthor_csg_slot *csg_slot = &sched->csg_slots[group->csg_id]; 2083 struct panthor_group *other_group; 2084 2085 /* Class groups in descending priority order so we can easily rotate. */ 2086 list_for_each_entry(other_group, 2087 &ctx->old_groups[csg_slot->group->priority], 2088 run_node) { 2089 struct panthor_csg_slot *other_csg_slot = &sched->csg_slots[other_group->csg_id]; 2090 2091 /* Our group has a higher prio than the one we're testing against, 2092 * place it just before. 2093 */ 2094 if (csg_slot->priority > other_csg_slot->priority) { 2095 list_add_tail(&group->run_node, &other_group->run_node); 2096 return; 2097 } 2098 } 2099 2100 list_add_tail(&group->run_node, &ctx->old_groups[group->priority]); 2101 } 2102 2103 static void 2104 tick_ctx_init(struct panthor_scheduler *sched, 2105 struct panthor_sched_tick_ctx *ctx) 2106 { 2107 struct panthor_device *ptdev = sched->ptdev; 2108 struct panthor_csg_slots_upd_ctx upd_ctx; 2109 int ret; 2110 u32 i; 2111 2112 memset(ctx, 0, sizeof(*ctx)); 2113 csgs_upd_ctx_init(&upd_ctx); 2114 2115 ctx->stop_tick = true; 2116 for (i = 0; i < ARRAY_SIZE(ctx->groups); i++) { 2117 INIT_LIST_HEAD(&ctx->groups[i]); 2118 INIT_LIST_HEAD(&ctx->old_groups[i]); 2119 } 2120 2121 for (i = 0; i < sched->csg_slot_count; i++) { 2122 struct panthor_csg_slot *csg_slot = &sched->csg_slots[i]; 2123 struct panthor_group *group = csg_slot->group; 2124 struct panthor_fw_csg_iface *csg_iface; 2125 2126 if (!group) 2127 continue; 2128 2129 csg_iface = panthor_fw_get_csg_iface(ptdev, i); 2130 group_get(group); 2131 2132 /* If there was unhandled faults on the VM, force processing of 2133 * CSG IRQs, so we can flag the faulty queue. 2134 */ 2135 if (panthor_vm_has_unhandled_faults(group->vm)) { 2136 sched_process_csg_irq_locked(ptdev, i); 2137 2138 /* No fatal fault reported, flag all queues as faulty. */ 2139 if (!group->fatal_queues) 2140 group->fatal_queues |= GENMASK(group->queue_count - 1, 0); 2141 } 2142 2143 tick_ctx_insert_old_group(sched, ctx, group); 2144 csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, i, 2145 csg_iface->output->ack ^ CSG_STATUS_UPDATE, 2146 CSG_STATUS_UPDATE); 2147 } 2148 2149 ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx); 2150 if (ret) { 2151 panthor_device_schedule_reset(ptdev); 2152 ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask; 2153 } 2154 } 2155 2156 static void 2157 group_term_post_processing(struct panthor_group *group) 2158 { 2159 struct panthor_job *job, *tmp; 2160 LIST_HEAD(faulty_jobs); 2161 bool cookie; 2162 u32 i = 0; 2163 2164 if (drm_WARN_ON(&group->ptdev->base, group_can_run(group))) 2165 return; 2166 2167 cookie = dma_fence_begin_signalling(); 2168 for (i = 0; i < group->queue_count; i++) { 2169 struct panthor_queue *queue = group->queues[i]; 2170 struct panthor_syncobj_64b *syncobj; 2171 int err; 2172 2173 if (group->fatal_queues & BIT(i)) 2174 err = -EINVAL; 2175 else if (group->timedout) 2176 err = -ETIMEDOUT; 2177 else 2178 err = -ECANCELED; 2179 2180 if (!queue) 2181 continue; 2182 2183 spin_lock(&queue->fence_ctx.lock); 2184 list_for_each_entry_safe(job, tmp, &queue->fence_ctx.in_flight_jobs, node) { 2185 list_move_tail(&job->node, &faulty_jobs); 2186 dma_fence_set_error(job->done_fence, err); 2187 dma_fence_signal_locked(job->done_fence); 2188 } 2189 spin_unlock(&queue->fence_ctx.lock); 2190 2191 /* Manually update the syncobj seqno to unblock waiters. */ 2192 syncobj = group->syncobjs->kmap + (i * sizeof(*syncobj)); 2193 syncobj->status = ~0; 2194 syncobj->seqno = atomic64_read(&queue->fence_ctx.seqno); 2195 sched_queue_work(group->ptdev->scheduler, sync_upd); 2196 } 2197 dma_fence_end_signalling(cookie); 2198 2199 list_for_each_entry_safe(job, tmp, &faulty_jobs, node) { 2200 list_del_init(&job->node); 2201 panthor_job_put(&job->base); 2202 } 2203 } 2204 2205 static void group_term_work(struct work_struct *work) 2206 { 2207 struct panthor_group *group = 2208 container_of(work, struct panthor_group, term_work); 2209 2210 group_term_post_processing(group); 2211 group_put(group); 2212 } 2213 2214 static void 2215 tick_ctx_cleanup(struct panthor_scheduler *sched, 2216 struct panthor_sched_tick_ctx *ctx) 2217 { 2218 struct panthor_device *ptdev = sched->ptdev; 2219 struct panthor_group *group, *tmp; 2220 u32 i; 2221 2222 for (i = 0; i < ARRAY_SIZE(ctx->old_groups); i++) { 2223 list_for_each_entry_safe(group, tmp, &ctx->old_groups[i], run_node) { 2224 /* If everything went fine, we should only have groups 2225 * to be terminated in the old_groups lists. 2226 */ 2227 drm_WARN_ON(&ptdev->base, !ctx->csg_upd_failed_mask && 2228 group_can_run(group)); 2229 2230 if (!group_can_run(group)) { 2231 list_del_init(&group->run_node); 2232 list_del_init(&group->wait_node); 2233 group_queue_work(group, term); 2234 } else if (group->csg_id >= 0) { 2235 list_del_init(&group->run_node); 2236 } else { 2237 list_move(&group->run_node, 2238 group_is_idle(group) ? 2239 &sched->groups.idle[group->priority] : 2240 &sched->groups.runnable[group->priority]); 2241 } 2242 group_put(group); 2243 } 2244 } 2245 2246 for (i = 0; i < ARRAY_SIZE(ctx->groups); i++) { 2247 /* If everything went fine, the groups to schedule lists should 2248 * be empty. 2249 */ 2250 drm_WARN_ON(&ptdev->base, 2251 !ctx->csg_upd_failed_mask && !list_empty(&ctx->groups[i])); 2252 2253 list_for_each_entry_safe(group, tmp, &ctx->groups[i], run_node) { 2254 if (group->csg_id >= 0) { 2255 list_del_init(&group->run_node); 2256 } else { 2257 list_move(&group->run_node, 2258 group_is_idle(group) ? 2259 &sched->groups.idle[group->priority] : 2260 &sched->groups.runnable[group->priority]); 2261 } 2262 group_put(group); 2263 } 2264 } 2265 } 2266 2267 static void 2268 tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *ctx) 2269 { 2270 struct panthor_group *group, *tmp; 2271 struct panthor_device *ptdev = sched->ptdev; 2272 struct panthor_csg_slot *csg_slot; 2273 int prio, new_csg_prio = MAX_CSG_PRIO, i; 2274 u32 free_csg_slots = 0; 2275 struct panthor_csg_slots_upd_ctx upd_ctx; 2276 int ret; 2277 2278 csgs_upd_ctx_init(&upd_ctx); 2279 2280 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) { 2281 /* Suspend or terminate evicted groups. */ 2282 list_for_each_entry(group, &ctx->old_groups[prio], run_node) { 2283 bool term = !group_can_run(group); 2284 int csg_id = group->csg_id; 2285 2286 if (drm_WARN_ON(&ptdev->base, csg_id < 0)) 2287 continue; 2288 2289 csg_slot = &sched->csg_slots[csg_id]; 2290 csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id, 2291 term ? CSG_STATE_TERMINATE : CSG_STATE_SUSPEND, 2292 CSG_STATE_MASK); 2293 } 2294 2295 /* Update priorities on already running groups. */ 2296 list_for_each_entry(group, &ctx->groups[prio], run_node) { 2297 struct panthor_fw_csg_iface *csg_iface; 2298 int csg_id = group->csg_id; 2299 2300 if (csg_id < 0) { 2301 new_csg_prio--; 2302 continue; 2303 } 2304 2305 csg_slot = &sched->csg_slots[csg_id]; 2306 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 2307 if (csg_slot->priority == new_csg_prio) { 2308 new_csg_prio--; 2309 continue; 2310 } 2311 2312 panthor_fw_csg_endpoint_req_update(ptdev, csg_iface, 2313 CSG_EP_REQ_PRIORITY(new_csg_prio), 2314 CSG_EP_REQ_PRIORITY_MASK); 2315 csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id, 2316 csg_iface->output->ack ^ CSG_ENDPOINT_CONFIG, 2317 CSG_ENDPOINT_CONFIG); 2318 new_csg_prio--; 2319 } 2320 } 2321 2322 ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx); 2323 if (ret) { 2324 panthor_device_schedule_reset(ptdev); 2325 ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask; 2326 return; 2327 } 2328 2329 /* Unbind evicted groups. */ 2330 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) { 2331 list_for_each_entry(group, &ctx->old_groups[prio], run_node) { 2332 /* This group is gone. Process interrupts to clear 2333 * any pending interrupts before we start the new 2334 * group. 2335 */ 2336 if (group->csg_id >= 0) 2337 sched_process_csg_irq_locked(ptdev, group->csg_id); 2338 2339 group_unbind_locked(group); 2340 } 2341 } 2342 2343 for (i = 0; i < sched->csg_slot_count; i++) { 2344 if (!sched->csg_slots[i].group) 2345 free_csg_slots |= BIT(i); 2346 } 2347 2348 csgs_upd_ctx_init(&upd_ctx); 2349 new_csg_prio = MAX_CSG_PRIO; 2350 2351 /* Start new groups. */ 2352 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) { 2353 list_for_each_entry(group, &ctx->groups[prio], run_node) { 2354 int csg_id = group->csg_id; 2355 struct panthor_fw_csg_iface *csg_iface; 2356 2357 if (csg_id >= 0) { 2358 new_csg_prio--; 2359 continue; 2360 } 2361 2362 csg_id = ffs(free_csg_slots) - 1; 2363 if (drm_WARN_ON(&ptdev->base, csg_id < 0)) 2364 break; 2365 2366 csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id); 2367 csg_slot = &sched->csg_slots[csg_id]; 2368 group_bind_locked(group, csg_id); 2369 csg_slot_prog_locked(ptdev, csg_id, new_csg_prio--); 2370 csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id, 2371 group->state == PANTHOR_CS_GROUP_SUSPENDED ? 2372 CSG_STATE_RESUME : CSG_STATE_START, 2373 CSG_STATE_MASK); 2374 csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id, 2375 csg_iface->output->ack ^ CSG_ENDPOINT_CONFIG, 2376 CSG_ENDPOINT_CONFIG); 2377 free_csg_slots &= ~BIT(csg_id); 2378 } 2379 } 2380 2381 ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx); 2382 if (ret) { 2383 panthor_device_schedule_reset(ptdev); 2384 ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask; 2385 return; 2386 } 2387 2388 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) { 2389 list_for_each_entry_safe(group, tmp, &ctx->groups[prio], run_node) { 2390 list_del_init(&group->run_node); 2391 2392 /* If the group has been destroyed while we were 2393 * scheduling, ask for an immediate tick to 2394 * re-evaluate as soon as possible and get rid of 2395 * this dangling group. 2396 */ 2397 if (group->destroyed) 2398 ctx->immediate_tick = true; 2399 group_put(group); 2400 } 2401 2402 /* Return evicted groups to the idle or run queues. Groups 2403 * that can no longer be run (because they've been destroyed 2404 * or experienced an unrecoverable error) will be scheduled 2405 * for destruction in tick_ctx_cleanup(). 2406 */ 2407 list_for_each_entry_safe(group, tmp, &ctx->old_groups[prio], run_node) { 2408 if (!group_can_run(group)) 2409 continue; 2410 2411 if (group_is_idle(group)) 2412 list_move_tail(&group->run_node, &sched->groups.idle[prio]); 2413 else 2414 list_move_tail(&group->run_node, &sched->groups.runnable[prio]); 2415 group_put(group); 2416 } 2417 } 2418 2419 sched->used_csg_slot_count = ctx->group_count; 2420 sched->might_have_idle_groups = ctx->idle_group_count > 0; 2421 } 2422 2423 static u64 2424 tick_ctx_update_resched_target(struct panthor_scheduler *sched, 2425 const struct panthor_sched_tick_ctx *ctx) 2426 { 2427 u64 resched_target; 2428 2429 if (ctx->stop_tick) 2430 goto no_tick; 2431 2432 resched_target = sched->last_tick + sched->tick_period; 2433 2434 if (time_before64(sched->resched_target, sched->last_tick) || 2435 time_before64(resched_target, sched->resched_target)) 2436 sched->resched_target = resched_target; 2437 2438 return sched->resched_target - sched->last_tick; 2439 2440 no_tick: 2441 sched->resched_target = U64_MAX; 2442 return U64_MAX; 2443 } 2444 2445 static void tick_work(struct work_struct *work) 2446 { 2447 struct panthor_scheduler *sched = container_of(work, struct panthor_scheduler, 2448 tick_work.work); 2449 struct panthor_device *ptdev = sched->ptdev; 2450 struct panthor_sched_tick_ctx ctx; 2451 u64 resched_target = sched->resched_target; 2452 u64 remaining_jiffies = 0, resched_delay; 2453 u64 now = get_jiffies_64(); 2454 int prio, ret, cookie; 2455 bool full_tick; 2456 2457 if (!drm_dev_enter(&ptdev->base, &cookie)) 2458 return; 2459 2460 ret = panthor_device_resume_and_get(ptdev); 2461 if (drm_WARN_ON(&ptdev->base, ret)) 2462 goto out_dev_exit; 2463 2464 /* If the tick is stopped, calculate when the next tick would be */ 2465 if (resched_target == U64_MAX) 2466 resched_target = sched->last_tick + sched->tick_period; 2467 2468 if (time_before64(now, resched_target)) 2469 remaining_jiffies = resched_target - now; 2470 2471 full_tick = remaining_jiffies == 0; 2472 2473 mutex_lock(&sched->lock); 2474 if (panthor_device_reset_is_pending(sched->ptdev)) 2475 goto out_unlock; 2476 2477 tick_ctx_init(sched, &ctx); 2478 if (ctx.csg_upd_failed_mask) 2479 goto out_cleanup_ctx; 2480 2481 if (!full_tick) { 2482 /* Scheduling forced in the middle of a tick. Only RT groups 2483 * can preempt non-RT ones. Currently running RT groups can't be 2484 * preempted. 2485 */ 2486 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; 2487 prio >= 0 && !tick_ctx_is_full(sched, &ctx); 2488 prio--) { 2489 tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], 2490 true, true); 2491 if (prio == PANTHOR_CSG_PRIORITY_RT) { 2492 tick_ctx_pick_groups_from_list(sched, &ctx, 2493 &sched->groups.runnable[prio], 2494 true, false); 2495 } 2496 } 2497 } 2498 2499 /* First pick non-idle groups */ 2500 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; 2501 prio >= 0 && !tick_ctx_is_full(sched, &ctx); 2502 prio--) { 2503 struct panthor_group *old_highest_prio_group = 2504 list_first_entry_or_null(&ctx.old_groups[prio], 2505 struct panthor_group, run_node); 2506 2507 /* Pull out the group with the highest prio for rotation. */ 2508 if (old_highest_prio_group) 2509 list_del(&old_highest_prio_group->run_node); 2510 2511 /* Re-insert old active groups so they get a chance to run with higher prio. */ 2512 tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], true, true); 2513 2514 /* Fill the remaining slots with runnable groups. */ 2515 tick_ctx_pick_groups_from_list(sched, &ctx, &sched->groups.runnable[prio], 2516 true, false); 2517 2518 /* Re-insert the old group with the highest prio, and give it a chance to be 2519 * scheduled again (but with a lower prio) if there's room left. 2520 */ 2521 if (old_highest_prio_group) { 2522 list_add_tail(&old_highest_prio_group->run_node, &ctx.old_groups[prio]); 2523 tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], 2524 true, true); 2525 } 2526 } 2527 2528 /* If we have free CSG slots left, pick idle groups */ 2529 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; 2530 prio >= 0 && !tick_ctx_is_full(sched, &ctx); 2531 prio--) { 2532 /* Check the old_group queue first to avoid reprogramming the slots */ 2533 tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], false, true); 2534 tick_ctx_pick_groups_from_list(sched, &ctx, &sched->groups.idle[prio], 2535 false, false); 2536 } 2537 2538 tick_ctx_apply(sched, &ctx); 2539 if (ctx.csg_upd_failed_mask) 2540 goto out_cleanup_ctx; 2541 2542 if (ctx.idle_group_count == ctx.group_count) { 2543 panthor_devfreq_record_idle(sched->ptdev); 2544 if (sched->pm.has_ref) { 2545 pm_runtime_put_autosuspend(ptdev->base.dev); 2546 sched->pm.has_ref = false; 2547 } 2548 } else { 2549 panthor_devfreq_record_busy(sched->ptdev); 2550 if (!sched->pm.has_ref) { 2551 pm_runtime_get(ptdev->base.dev); 2552 sched->pm.has_ref = true; 2553 } 2554 } 2555 2556 sched->last_tick = now; 2557 resched_delay = tick_ctx_update_resched_target(sched, &ctx); 2558 if (ctx.immediate_tick) 2559 resched_delay = 0; 2560 2561 if (resched_delay != U64_MAX) 2562 sched_queue_delayed_work(sched, tick, resched_delay); 2563 2564 out_cleanup_ctx: 2565 tick_ctx_cleanup(sched, &ctx); 2566 2567 out_unlock: 2568 mutex_unlock(&sched->lock); 2569 pm_runtime_mark_last_busy(ptdev->base.dev); 2570 pm_runtime_put_autosuspend(ptdev->base.dev); 2571 2572 out_dev_exit: 2573 drm_dev_exit(cookie); 2574 } 2575 2576 static int panthor_queue_eval_syncwait(struct panthor_group *group, u8 queue_idx) 2577 { 2578 struct panthor_queue *queue = group->queues[queue_idx]; 2579 union { 2580 struct panthor_syncobj_64b sync64; 2581 struct panthor_syncobj_32b sync32; 2582 } *syncobj; 2583 bool result; 2584 u64 value; 2585 2586 syncobj = panthor_queue_get_syncwait_obj(group, queue); 2587 if (!syncobj) 2588 return -EINVAL; 2589 2590 value = queue->syncwait.sync64 ? 2591 syncobj->sync64.seqno : 2592 syncobj->sync32.seqno; 2593 2594 if (queue->syncwait.gt) 2595 result = value > queue->syncwait.ref; 2596 else 2597 result = value <= queue->syncwait.ref; 2598 2599 if (result) 2600 panthor_queue_put_syncwait_obj(queue); 2601 2602 return result; 2603 } 2604 2605 static void sync_upd_work(struct work_struct *work) 2606 { 2607 struct panthor_scheduler *sched = container_of(work, 2608 struct panthor_scheduler, 2609 sync_upd_work); 2610 struct panthor_group *group, *tmp; 2611 bool immediate_tick = false; 2612 2613 mutex_lock(&sched->lock); 2614 list_for_each_entry_safe(group, tmp, &sched->groups.waiting, wait_node) { 2615 u32 tested_queues = group->blocked_queues; 2616 u32 unblocked_queues = 0; 2617 2618 while (tested_queues) { 2619 u32 cs_id = ffs(tested_queues) - 1; 2620 int ret; 2621 2622 ret = panthor_queue_eval_syncwait(group, cs_id); 2623 drm_WARN_ON(&group->ptdev->base, ret < 0); 2624 if (ret) 2625 unblocked_queues |= BIT(cs_id); 2626 2627 tested_queues &= ~BIT(cs_id); 2628 } 2629 2630 if (unblocked_queues) { 2631 group->blocked_queues &= ~unblocked_queues; 2632 2633 if (group->csg_id < 0) { 2634 list_move(&group->run_node, 2635 &sched->groups.runnable[group->priority]); 2636 if (group->priority == PANTHOR_CSG_PRIORITY_RT) 2637 immediate_tick = true; 2638 } 2639 } 2640 2641 if (!group->blocked_queues) 2642 list_del_init(&group->wait_node); 2643 } 2644 mutex_unlock(&sched->lock); 2645 2646 if (immediate_tick) 2647 sched_queue_delayed_work(sched, tick, 0); 2648 } 2649 2650 static void sched_resume_tick(struct panthor_device *ptdev) 2651 { 2652 struct panthor_scheduler *sched = ptdev->scheduler; 2653 u64 delay_jiffies, now; 2654 2655 drm_WARN_ON(&ptdev->base, sched->resched_target != U64_MAX); 2656 2657 /* Scheduler tick was off, recalculate the resched_target based on the 2658 * last tick event, and queue the scheduler work. 2659 */ 2660 now = get_jiffies_64(); 2661 sched->resched_target = sched->last_tick + sched->tick_period; 2662 if (sched->used_csg_slot_count == sched->csg_slot_count && 2663 time_before64(now, sched->resched_target)) 2664 delay_jiffies = min_t(unsigned long, sched->resched_target - now, ULONG_MAX); 2665 else 2666 delay_jiffies = 0; 2667 2668 sched_queue_delayed_work(sched, tick, delay_jiffies); 2669 } 2670 2671 static void group_schedule_locked(struct panthor_group *group, u32 queue_mask) 2672 { 2673 struct panthor_device *ptdev = group->ptdev; 2674 struct panthor_scheduler *sched = ptdev->scheduler; 2675 struct list_head *queue = &sched->groups.runnable[group->priority]; 2676 bool was_idle; 2677 2678 if (!group_can_run(group)) 2679 return; 2680 2681 /* All updated queues are blocked, no need to wake up the scheduler. */ 2682 if ((queue_mask & group->blocked_queues) == queue_mask) 2683 return; 2684 2685 was_idle = group_is_idle(group); 2686 group->idle_queues &= ~queue_mask; 2687 2688 /* Don't mess up with the lists if we're in a middle of a reset. */ 2689 if (atomic_read(&sched->reset.in_progress)) 2690 return; 2691 2692 if (was_idle && !group_is_idle(group)) 2693 list_move_tail(&group->run_node, queue); 2694 2695 /* RT groups are preemptive. */ 2696 if (group->priority == PANTHOR_CSG_PRIORITY_RT) { 2697 sched_queue_delayed_work(sched, tick, 0); 2698 return; 2699 } 2700 2701 /* Some groups might be idle, force an immediate tick to 2702 * re-evaluate. 2703 */ 2704 if (sched->might_have_idle_groups) { 2705 sched_queue_delayed_work(sched, tick, 0); 2706 return; 2707 } 2708 2709 /* Scheduler is ticking, nothing to do. */ 2710 if (sched->resched_target != U64_MAX) { 2711 /* If there are free slots, force immediating ticking. */ 2712 if (sched->used_csg_slot_count < sched->csg_slot_count) 2713 sched_queue_delayed_work(sched, tick, 0); 2714 2715 return; 2716 } 2717 2718 /* Scheduler tick was off, recalculate the resched_target based on the 2719 * last tick event, and queue the scheduler work. 2720 */ 2721 sched_resume_tick(ptdev); 2722 } 2723 2724 static void queue_stop(struct panthor_queue *queue, 2725 struct panthor_job *bad_job) 2726 { 2727 disable_delayed_work_sync(&queue->timeout.work); 2728 drm_sched_stop(&queue->scheduler, bad_job ? &bad_job->base : NULL); 2729 } 2730 2731 static void queue_start(struct panthor_queue *queue) 2732 { 2733 struct panthor_job *job; 2734 2735 /* Re-assign the parent fences. */ 2736 list_for_each_entry(job, &queue->scheduler.pending_list, base.list) 2737 job->base.s_fence->parent = dma_fence_get(job->done_fence); 2738 2739 enable_delayed_work(&queue->timeout.work); 2740 drm_sched_start(&queue->scheduler, 0); 2741 } 2742 2743 static void panthor_group_stop(struct panthor_group *group) 2744 { 2745 struct panthor_scheduler *sched = group->ptdev->scheduler; 2746 2747 lockdep_assert_held(&sched->reset.lock); 2748 2749 for (u32 i = 0; i < group->queue_count; i++) 2750 queue_stop(group->queues[i], NULL); 2751 2752 group_get(group); 2753 list_move_tail(&group->run_node, &sched->reset.stopped_groups); 2754 } 2755 2756 static void panthor_group_start(struct panthor_group *group) 2757 { 2758 struct panthor_scheduler *sched = group->ptdev->scheduler; 2759 2760 lockdep_assert_held(&group->ptdev->scheduler->reset.lock); 2761 2762 for (u32 i = 0; i < group->queue_count; i++) 2763 queue_start(group->queues[i]); 2764 2765 if (group_can_run(group)) { 2766 list_move_tail(&group->run_node, 2767 group_is_idle(group) ? 2768 &sched->groups.idle[group->priority] : 2769 &sched->groups.runnable[group->priority]); 2770 } else { 2771 list_del_init(&group->run_node); 2772 list_del_init(&group->wait_node); 2773 group_queue_work(group, term); 2774 } 2775 2776 group_put(group); 2777 } 2778 2779 /** 2780 * panthor_sched_report_mmu_fault() - Report MMU faults to the scheduler. 2781 */ 2782 void panthor_sched_report_mmu_fault(struct panthor_device *ptdev) 2783 { 2784 /* Force a tick to immediately kill faulty groups. */ 2785 if (ptdev->scheduler) 2786 sched_queue_delayed_work(ptdev->scheduler, tick, 0); 2787 } 2788 2789 void panthor_sched_prepare_for_vm_destruction(struct panthor_device *ptdev) 2790 { 2791 /* FW can write out internal state, like the heap context, during CSG 2792 * suspend. It is therefore important that the scheduler has fully 2793 * evicted any pending and related groups before VM destruction can 2794 * safely continue. Failure to do so can lead to GPU page faults. 2795 * A controlled termination of a Panthor instance involves destroying 2796 * the group(s) before the VM. This means any relevant group eviction 2797 * has already been initiated by this point, and we just need to 2798 * ensure that any pending tick_work() has been completed. 2799 */ 2800 flush_work(&ptdev->scheduler->tick_work.work); 2801 } 2802 2803 void panthor_sched_resume(struct panthor_device *ptdev) 2804 { 2805 /* Force a tick to re-evaluate after a resume. */ 2806 sched_queue_delayed_work(ptdev->scheduler, tick, 0); 2807 } 2808 2809 void panthor_sched_suspend(struct panthor_device *ptdev) 2810 { 2811 struct panthor_scheduler *sched = ptdev->scheduler; 2812 struct panthor_csg_slots_upd_ctx upd_ctx; 2813 u32 suspended_slots; 2814 u32 i; 2815 2816 mutex_lock(&sched->lock); 2817 csgs_upd_ctx_init(&upd_ctx); 2818 for (i = 0; i < sched->csg_slot_count; i++) { 2819 struct panthor_csg_slot *csg_slot = &sched->csg_slots[i]; 2820 2821 if (csg_slot->group) { 2822 csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, i, 2823 group_can_run(csg_slot->group) ? 2824 CSG_STATE_SUSPEND : CSG_STATE_TERMINATE, 2825 CSG_STATE_MASK); 2826 } 2827 } 2828 2829 suspended_slots = upd_ctx.update_mask; 2830 2831 csgs_upd_ctx_apply_locked(ptdev, &upd_ctx); 2832 suspended_slots &= ~upd_ctx.timedout_mask; 2833 2834 if (upd_ctx.timedout_mask) { 2835 u32 slot_mask = upd_ctx.timedout_mask; 2836 2837 drm_err(&ptdev->base, "CSG suspend failed, escalating to termination"); 2838 csgs_upd_ctx_init(&upd_ctx); 2839 while (slot_mask) { 2840 u32 csg_id = ffs(slot_mask) - 1; 2841 struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id]; 2842 2843 /* If the group was still usable before that point, we consider 2844 * it innocent. 2845 */ 2846 if (group_can_run(csg_slot->group)) 2847 csg_slot->group->innocent = true; 2848 2849 /* We consider group suspension failures as fatal and flag the 2850 * group as unusable by setting timedout=true. 2851 */ 2852 csg_slot->group->timedout = true; 2853 2854 csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id, 2855 CSG_STATE_TERMINATE, 2856 CSG_STATE_MASK); 2857 slot_mask &= ~BIT(csg_id); 2858 } 2859 2860 csgs_upd_ctx_apply_locked(ptdev, &upd_ctx); 2861 2862 slot_mask = upd_ctx.timedout_mask; 2863 while (slot_mask) { 2864 u32 csg_id = ffs(slot_mask) - 1; 2865 struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id]; 2866 struct panthor_group *group = csg_slot->group; 2867 2868 /* Terminate command timedout, but the soft-reset will 2869 * automatically terminate all active groups, so let's 2870 * force the state to halted here. 2871 */ 2872 if (group->state != PANTHOR_CS_GROUP_TERMINATED) { 2873 group->state = PANTHOR_CS_GROUP_TERMINATED; 2874 2875 /* Reset the queue slots manually if the termination 2876 * request failed. 2877 */ 2878 for (i = 0; i < group->queue_count; i++) { 2879 if (group->queues[i]) 2880 cs_slot_reset_locked(ptdev, csg_id, i); 2881 } 2882 } 2883 slot_mask &= ~BIT(csg_id); 2884 } 2885 } 2886 2887 /* Flush L2 and LSC caches to make sure suspend state is up-to-date. 2888 * If the flush fails, flag all queues for termination. 2889 */ 2890 if (suspended_slots) { 2891 bool flush_caches_failed = false; 2892 u32 slot_mask = suspended_slots; 2893 2894 if (panthor_gpu_flush_caches(ptdev, CACHE_CLEAN, CACHE_CLEAN, 0)) 2895 flush_caches_failed = true; 2896 2897 while (slot_mask) { 2898 u32 csg_id = ffs(slot_mask) - 1; 2899 struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id]; 2900 2901 if (flush_caches_failed) 2902 csg_slot->group->state = PANTHOR_CS_GROUP_TERMINATED; 2903 else 2904 csg_slot_sync_update_locked(ptdev, csg_id); 2905 2906 slot_mask &= ~BIT(csg_id); 2907 } 2908 } 2909 2910 for (i = 0; i < sched->csg_slot_count; i++) { 2911 struct panthor_csg_slot *csg_slot = &sched->csg_slots[i]; 2912 struct panthor_group *group = csg_slot->group; 2913 2914 if (!group) 2915 continue; 2916 2917 group_get(group); 2918 2919 if (group->csg_id >= 0) 2920 sched_process_csg_irq_locked(ptdev, group->csg_id); 2921 2922 group_unbind_locked(group); 2923 2924 drm_WARN_ON(&group->ptdev->base, !list_empty(&group->run_node)); 2925 2926 if (group_can_run(group)) { 2927 list_add(&group->run_node, 2928 &sched->groups.idle[group->priority]); 2929 } else { 2930 /* We don't bother stopping the scheduler if the group is 2931 * faulty, the group termination work will finish the job. 2932 */ 2933 list_del_init(&group->wait_node); 2934 group_queue_work(group, term); 2935 } 2936 group_put(group); 2937 } 2938 mutex_unlock(&sched->lock); 2939 } 2940 2941 void panthor_sched_pre_reset(struct panthor_device *ptdev) 2942 { 2943 struct panthor_scheduler *sched = ptdev->scheduler; 2944 struct panthor_group *group, *group_tmp; 2945 u32 i; 2946 2947 mutex_lock(&sched->reset.lock); 2948 atomic_set(&sched->reset.in_progress, true); 2949 2950 /* Cancel all scheduler works. Once this is done, these works can't be 2951 * scheduled again until the reset operation is complete. 2952 */ 2953 cancel_work_sync(&sched->sync_upd_work); 2954 cancel_delayed_work_sync(&sched->tick_work); 2955 2956 panthor_sched_suspend(ptdev); 2957 2958 /* Stop all groups that might still accept jobs, so we don't get passed 2959 * new jobs while we're resetting. 2960 */ 2961 for (i = 0; i < ARRAY_SIZE(sched->groups.runnable); i++) { 2962 list_for_each_entry_safe(group, group_tmp, &sched->groups.runnable[i], run_node) 2963 panthor_group_stop(group); 2964 } 2965 2966 for (i = 0; i < ARRAY_SIZE(sched->groups.idle); i++) { 2967 list_for_each_entry_safe(group, group_tmp, &sched->groups.idle[i], run_node) 2968 panthor_group_stop(group); 2969 } 2970 2971 mutex_unlock(&sched->reset.lock); 2972 } 2973 2974 void panthor_sched_post_reset(struct panthor_device *ptdev, bool reset_failed) 2975 { 2976 struct panthor_scheduler *sched = ptdev->scheduler; 2977 struct panthor_group *group, *group_tmp; 2978 2979 mutex_lock(&sched->reset.lock); 2980 2981 list_for_each_entry_safe(group, group_tmp, &sched->reset.stopped_groups, run_node) { 2982 /* Consider all previously running group as terminated if the 2983 * reset failed. 2984 */ 2985 if (reset_failed) 2986 group->state = PANTHOR_CS_GROUP_TERMINATED; 2987 2988 panthor_group_start(group); 2989 } 2990 2991 /* We're done resetting the GPU, clear the reset.in_progress bit so we can 2992 * kick the scheduler. 2993 */ 2994 atomic_set(&sched->reset.in_progress, false); 2995 mutex_unlock(&sched->reset.lock); 2996 2997 /* No need to queue a tick and update syncs if the reset failed. */ 2998 if (!reset_failed) { 2999 sched_queue_delayed_work(sched, tick, 0); 3000 sched_queue_work(sched, sync_upd); 3001 } 3002 } 3003 3004 static void update_fdinfo_stats(struct panthor_job *job) 3005 { 3006 struct panthor_group *group = job->group; 3007 struct panthor_queue *queue = group->queues[job->queue_idx]; 3008 struct panthor_gpu_usage *fdinfo = &group->fdinfo.data; 3009 struct panthor_job_profiling_data *slots = queue->profiling.slots->kmap; 3010 struct panthor_job_profiling_data *data = &slots[job->profiling.slot]; 3011 3012 scoped_guard(spinlock, &group->fdinfo.lock) { 3013 if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_CYCLES) 3014 fdinfo->cycles += data->cycles.after - data->cycles.before; 3015 if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_TIMESTAMP) 3016 fdinfo->time += data->time.after - data->time.before; 3017 } 3018 } 3019 3020 void panthor_fdinfo_gather_group_samples(struct panthor_file *pfile) 3021 { 3022 struct panthor_group_pool *gpool = pfile->groups; 3023 struct panthor_group *group; 3024 unsigned long i; 3025 3026 if (IS_ERR_OR_NULL(gpool)) 3027 return; 3028 3029 xa_lock(&gpool->xa); 3030 xa_for_each_marked(&gpool->xa, i, group, GROUP_REGISTERED) { 3031 guard(spinlock)(&group->fdinfo.lock); 3032 pfile->stats.cycles += group->fdinfo.data.cycles; 3033 pfile->stats.time += group->fdinfo.data.time; 3034 group->fdinfo.data.cycles = 0; 3035 group->fdinfo.data.time = 0; 3036 } 3037 xa_unlock(&gpool->xa); 3038 } 3039 3040 static bool queue_check_job_completion(struct panthor_queue *queue) 3041 { 3042 struct panthor_syncobj_64b *syncobj = NULL; 3043 struct panthor_job *job, *job_tmp; 3044 bool cookie, progress = false; 3045 LIST_HEAD(done_jobs); 3046 3047 cookie = dma_fence_begin_signalling(); 3048 spin_lock(&queue->fence_ctx.lock); 3049 list_for_each_entry_safe(job, job_tmp, &queue->fence_ctx.in_flight_jobs, node) { 3050 if (!syncobj) { 3051 struct panthor_group *group = job->group; 3052 3053 syncobj = group->syncobjs->kmap + 3054 (job->queue_idx * sizeof(*syncobj)); 3055 } 3056 3057 if (syncobj->seqno < job->done_fence->seqno) 3058 break; 3059 3060 list_move_tail(&job->node, &done_jobs); 3061 dma_fence_signal_locked(job->done_fence); 3062 } 3063 3064 if (list_empty(&queue->fence_ctx.in_flight_jobs)) { 3065 /* If we have no job left, we cancel the timer, and reset remaining 3066 * time to its default so it can be restarted next time 3067 * queue_resume_timeout() is called. 3068 */ 3069 queue_suspend_timeout_locked(queue); 3070 3071 /* If there's no job pending, we consider it progress to avoid a 3072 * spurious timeout if the timeout handler and the sync update 3073 * handler raced. 3074 */ 3075 progress = true; 3076 } else if (!list_empty(&done_jobs)) { 3077 queue_reset_timeout_locked(queue); 3078 progress = true; 3079 } 3080 spin_unlock(&queue->fence_ctx.lock); 3081 dma_fence_end_signalling(cookie); 3082 3083 list_for_each_entry_safe(job, job_tmp, &done_jobs, node) { 3084 if (job->profiling.mask) 3085 update_fdinfo_stats(job); 3086 list_del_init(&job->node); 3087 panthor_job_put(&job->base); 3088 } 3089 3090 return progress; 3091 } 3092 3093 static void group_sync_upd_work(struct work_struct *work) 3094 { 3095 struct panthor_group *group = 3096 container_of(work, struct panthor_group, sync_upd_work); 3097 u32 queue_idx; 3098 bool cookie; 3099 3100 cookie = dma_fence_begin_signalling(); 3101 for (queue_idx = 0; queue_idx < group->queue_count; queue_idx++) { 3102 struct panthor_queue *queue = group->queues[queue_idx]; 3103 3104 if (!queue) 3105 continue; 3106 3107 queue_check_job_completion(queue); 3108 } 3109 dma_fence_end_signalling(cookie); 3110 3111 group_put(group); 3112 } 3113 3114 struct panthor_job_ringbuf_instrs { 3115 u64 buffer[MAX_INSTRS_PER_JOB]; 3116 u32 count; 3117 }; 3118 3119 struct panthor_job_instr { 3120 u32 profile_mask; 3121 u64 instr; 3122 }; 3123 3124 #define JOB_INSTR(__prof, __instr) \ 3125 { \ 3126 .profile_mask = __prof, \ 3127 .instr = __instr, \ 3128 } 3129 3130 static void 3131 copy_instrs_to_ringbuf(struct panthor_queue *queue, 3132 struct panthor_job *job, 3133 struct panthor_job_ringbuf_instrs *instrs) 3134 { 3135 u64 ringbuf_size = panthor_kernel_bo_size(queue->ringbuf); 3136 u64 start = job->ringbuf.start & (ringbuf_size - 1); 3137 u64 size, written; 3138 3139 /* 3140 * We need to write a whole slot, including any trailing zeroes 3141 * that may come at the end of it. Also, because instrs.buffer has 3142 * been zero-initialised, there's no need to pad it with 0's 3143 */ 3144 instrs->count = ALIGN(instrs->count, NUM_INSTRS_PER_CACHE_LINE); 3145 size = instrs->count * sizeof(u64); 3146 WARN_ON(size > ringbuf_size); 3147 written = min(ringbuf_size - start, size); 3148 3149 memcpy(queue->ringbuf->kmap + start, instrs->buffer, written); 3150 3151 if (written < size) 3152 memcpy(queue->ringbuf->kmap, 3153 &instrs->buffer[written / sizeof(u64)], 3154 size - written); 3155 } 3156 3157 struct panthor_job_cs_params { 3158 u32 profile_mask; 3159 u64 addr_reg; u64 val_reg; 3160 u64 cycle_reg; u64 time_reg; 3161 u64 sync_addr; u64 times_addr; 3162 u64 cs_start; u64 cs_size; 3163 u32 last_flush; u32 waitall_mask; 3164 }; 3165 3166 static void 3167 get_job_cs_params(struct panthor_job *job, struct panthor_job_cs_params *params) 3168 { 3169 struct panthor_group *group = job->group; 3170 struct panthor_queue *queue = group->queues[job->queue_idx]; 3171 struct panthor_device *ptdev = group->ptdev; 3172 struct panthor_scheduler *sched = ptdev->scheduler; 3173 3174 params->addr_reg = ptdev->csif_info.cs_reg_count - 3175 ptdev->csif_info.unpreserved_cs_reg_count; 3176 params->val_reg = params->addr_reg + 2; 3177 params->cycle_reg = params->addr_reg; 3178 params->time_reg = params->val_reg; 3179 3180 params->sync_addr = panthor_kernel_bo_gpuva(group->syncobjs) + 3181 job->queue_idx * sizeof(struct panthor_syncobj_64b); 3182 params->times_addr = panthor_kernel_bo_gpuva(queue->profiling.slots) + 3183 (job->profiling.slot * sizeof(struct panthor_job_profiling_data)); 3184 params->waitall_mask = GENMASK(sched->sb_slot_count - 1, 0); 3185 3186 params->cs_start = job->call_info.start; 3187 params->cs_size = job->call_info.size; 3188 params->last_flush = job->call_info.latest_flush; 3189 3190 params->profile_mask = job->profiling.mask; 3191 } 3192 3193 #define JOB_INSTR_ALWAYS(instr) \ 3194 JOB_INSTR(PANTHOR_DEVICE_PROFILING_DISABLED, (instr)) 3195 #define JOB_INSTR_TIMESTAMP(instr) \ 3196 JOB_INSTR(PANTHOR_DEVICE_PROFILING_TIMESTAMP, (instr)) 3197 #define JOB_INSTR_CYCLES(instr) \ 3198 JOB_INSTR(PANTHOR_DEVICE_PROFILING_CYCLES, (instr)) 3199 3200 static void 3201 prepare_job_instrs(const struct panthor_job_cs_params *params, 3202 struct panthor_job_ringbuf_instrs *instrs) 3203 { 3204 const struct panthor_job_instr instr_seq[] = { 3205 /* MOV32 rX+2, cs.latest_flush */ 3206 JOB_INSTR_ALWAYS((2ull << 56) | (params->val_reg << 48) | params->last_flush), 3207 /* FLUSH_CACHE2.clean_inv_all.no_wait.signal(0) rX+2 */ 3208 JOB_INSTR_ALWAYS((36ull << 56) | (0ull << 48) | (params->val_reg << 40) | 3209 (0 << 16) | 0x233), 3210 /* MOV48 rX:rX+1, cycles_offset */ 3211 JOB_INSTR_CYCLES((1ull << 56) | (params->cycle_reg << 48) | 3212 (params->times_addr + 3213 offsetof(struct panthor_job_profiling_data, cycles.before))), 3214 /* STORE_STATE cycles */ 3215 JOB_INSTR_CYCLES((40ull << 56) | (params->cycle_reg << 40) | (1ll << 32)), 3216 /* MOV48 rX:rX+1, time_offset */ 3217 JOB_INSTR_TIMESTAMP((1ull << 56) | (params->time_reg << 48) | 3218 (params->times_addr + 3219 offsetof(struct panthor_job_profiling_data, time.before))), 3220 /* STORE_STATE timer */ 3221 JOB_INSTR_TIMESTAMP((40ull << 56) | (params->time_reg << 40) | (0ll << 32)), 3222 /* MOV48 rX:rX+1, cs.start */ 3223 JOB_INSTR_ALWAYS((1ull << 56) | (params->addr_reg << 48) | params->cs_start), 3224 /* MOV32 rX+2, cs.size */ 3225 JOB_INSTR_ALWAYS((2ull << 56) | (params->val_reg << 48) | params->cs_size), 3226 /* WAIT(0) => waits for FLUSH_CACHE2 instruction */ 3227 JOB_INSTR_ALWAYS((3ull << 56) | (1 << 16)), 3228 /* CALL rX:rX+1, rX+2 */ 3229 JOB_INSTR_ALWAYS((32ull << 56) | (params->addr_reg << 40) | 3230 (params->val_reg << 32)), 3231 /* MOV48 rX:rX+1, cycles_offset */ 3232 JOB_INSTR_CYCLES((1ull << 56) | (params->cycle_reg << 48) | 3233 (params->times_addr + 3234 offsetof(struct panthor_job_profiling_data, cycles.after))), 3235 /* STORE_STATE cycles */ 3236 JOB_INSTR_CYCLES((40ull << 56) | (params->cycle_reg << 40) | (1ll << 32)), 3237 /* MOV48 rX:rX+1, time_offset */ 3238 JOB_INSTR_TIMESTAMP((1ull << 56) | (params->time_reg << 48) | 3239 (params->times_addr + 3240 offsetof(struct panthor_job_profiling_data, time.after))), 3241 /* STORE_STATE timer */ 3242 JOB_INSTR_TIMESTAMP((40ull << 56) | (params->time_reg << 40) | (0ll << 32)), 3243 /* MOV48 rX:rX+1, sync_addr */ 3244 JOB_INSTR_ALWAYS((1ull << 56) | (params->addr_reg << 48) | params->sync_addr), 3245 /* MOV48 rX+2, #1 */ 3246 JOB_INSTR_ALWAYS((1ull << 56) | (params->val_reg << 48) | 1), 3247 /* WAIT(all) */ 3248 JOB_INSTR_ALWAYS((3ull << 56) | (params->waitall_mask << 16)), 3249 /* SYNC_ADD64.system_scope.propage_err.nowait rX:rX+1, rX+2*/ 3250 JOB_INSTR_ALWAYS((51ull << 56) | (0ull << 48) | (params->addr_reg << 40) | 3251 (params->val_reg << 32) | (0 << 16) | 1), 3252 /* ERROR_BARRIER, so we can recover from faults at job boundaries. */ 3253 JOB_INSTR_ALWAYS((47ull << 56)), 3254 }; 3255 u32 pad; 3256 3257 instrs->count = 0; 3258 3259 /* NEED to be cacheline aligned to please the prefetcher. */ 3260 static_assert(sizeof(instrs->buffer) % 64 == 0, 3261 "panthor_job_ringbuf_instrs::buffer is not aligned on a cacheline"); 3262 3263 /* Make sure we have enough storage to store the whole sequence. */ 3264 static_assert(ALIGN(ARRAY_SIZE(instr_seq), NUM_INSTRS_PER_CACHE_LINE) == 3265 ARRAY_SIZE(instrs->buffer), 3266 "instr_seq vs panthor_job_ringbuf_instrs::buffer size mismatch"); 3267 3268 for (u32 i = 0; i < ARRAY_SIZE(instr_seq); i++) { 3269 /* If the profile mask of this instruction is not enabled, skip it. */ 3270 if (instr_seq[i].profile_mask && 3271 !(instr_seq[i].profile_mask & params->profile_mask)) 3272 continue; 3273 3274 instrs->buffer[instrs->count++] = instr_seq[i].instr; 3275 } 3276 3277 pad = ALIGN(instrs->count, NUM_INSTRS_PER_CACHE_LINE); 3278 memset(&instrs->buffer[instrs->count], 0, 3279 (pad - instrs->count) * sizeof(instrs->buffer[0])); 3280 instrs->count = pad; 3281 } 3282 3283 static u32 calc_job_credits(u32 profile_mask) 3284 { 3285 struct panthor_job_ringbuf_instrs instrs; 3286 struct panthor_job_cs_params params = { 3287 .profile_mask = profile_mask, 3288 }; 3289 3290 prepare_job_instrs(¶ms, &instrs); 3291 return instrs.count; 3292 } 3293 3294 static struct dma_fence * 3295 queue_run_job(struct drm_sched_job *sched_job) 3296 { 3297 struct panthor_job *job = container_of(sched_job, struct panthor_job, base); 3298 struct panthor_group *group = job->group; 3299 struct panthor_queue *queue = group->queues[job->queue_idx]; 3300 struct panthor_device *ptdev = group->ptdev; 3301 struct panthor_scheduler *sched = ptdev->scheduler; 3302 struct panthor_job_ringbuf_instrs instrs; 3303 struct panthor_job_cs_params cs_params; 3304 struct dma_fence *done_fence; 3305 int ret; 3306 3307 /* Stream size is zero, nothing to do except making sure all previously 3308 * submitted jobs are done before we signal the 3309 * drm_sched_job::s_fence::finished fence. 3310 */ 3311 if (!job->call_info.size) { 3312 job->done_fence = dma_fence_get(queue->fence_ctx.last_fence); 3313 return dma_fence_get(job->done_fence); 3314 } 3315 3316 ret = panthor_device_resume_and_get(ptdev); 3317 if (drm_WARN_ON(&ptdev->base, ret)) 3318 return ERR_PTR(ret); 3319 3320 mutex_lock(&sched->lock); 3321 if (!group_can_run(group)) { 3322 done_fence = ERR_PTR(-ECANCELED); 3323 goto out_unlock; 3324 } 3325 3326 dma_fence_init(job->done_fence, 3327 &panthor_queue_fence_ops, 3328 &queue->fence_ctx.lock, 3329 queue->fence_ctx.id, 3330 atomic64_inc_return(&queue->fence_ctx.seqno)); 3331 3332 job->profiling.slot = queue->profiling.seqno++; 3333 if (queue->profiling.seqno == queue->profiling.slot_count) 3334 queue->profiling.seqno = 0; 3335 3336 job->ringbuf.start = queue->iface.input->insert; 3337 3338 get_job_cs_params(job, &cs_params); 3339 prepare_job_instrs(&cs_params, &instrs); 3340 copy_instrs_to_ringbuf(queue, job, &instrs); 3341 3342 job->ringbuf.end = job->ringbuf.start + (instrs.count * sizeof(u64)); 3343 3344 panthor_job_get(&job->base); 3345 spin_lock(&queue->fence_ctx.lock); 3346 list_add_tail(&job->node, &queue->fence_ctx.in_flight_jobs); 3347 spin_unlock(&queue->fence_ctx.lock); 3348 3349 /* Make sure the ring buffer is updated before the INSERT 3350 * register. 3351 */ 3352 wmb(); 3353 3354 queue->iface.input->extract = queue->iface.output->extract; 3355 queue->iface.input->insert = job->ringbuf.end; 3356 3357 if (group->csg_id < 0) { 3358 group_schedule_locked(group, BIT(job->queue_idx)); 3359 } else { 3360 u32 queue_mask = BIT(job->queue_idx); 3361 bool resume_tick = group_is_idle(group) && 3362 (group->idle_queues & queue_mask) && 3363 !(group->blocked_queues & queue_mask) && 3364 sched->resched_target == U64_MAX; 3365 3366 /* We just added something to the queue, so it's no longer idle. */ 3367 group->idle_queues &= ~queue_mask; 3368 3369 if (resume_tick) 3370 sched_resume_tick(ptdev); 3371 3372 gpu_write(ptdev, CSF_DOORBELL(queue->doorbell_id), 1); 3373 if (!sched->pm.has_ref && 3374 !(group->blocked_queues & BIT(job->queue_idx))) { 3375 pm_runtime_get(ptdev->base.dev); 3376 sched->pm.has_ref = true; 3377 } 3378 queue_resume_timeout(queue); 3379 panthor_devfreq_record_busy(sched->ptdev); 3380 } 3381 3382 /* Update the last fence. */ 3383 dma_fence_put(queue->fence_ctx.last_fence); 3384 queue->fence_ctx.last_fence = dma_fence_get(job->done_fence); 3385 3386 done_fence = dma_fence_get(job->done_fence); 3387 3388 out_unlock: 3389 mutex_unlock(&sched->lock); 3390 pm_runtime_mark_last_busy(ptdev->base.dev); 3391 pm_runtime_put_autosuspend(ptdev->base.dev); 3392 3393 return done_fence; 3394 } 3395 3396 static enum drm_gpu_sched_stat 3397 queue_timedout_job(struct drm_sched_job *sched_job) 3398 { 3399 struct panthor_job *job = container_of(sched_job, struct panthor_job, base); 3400 struct panthor_group *group = job->group; 3401 struct panthor_device *ptdev = group->ptdev; 3402 struct panthor_scheduler *sched = ptdev->scheduler; 3403 struct panthor_queue *queue = group->queues[job->queue_idx]; 3404 3405 drm_warn(&ptdev->base, "job timeout: pid=%d, comm=%s, seqno=%llu\n", 3406 group->task_info.pid, group->task_info.comm, job->done_fence->seqno); 3407 3408 drm_WARN_ON(&ptdev->base, atomic_read(&sched->reset.in_progress)); 3409 3410 queue_stop(queue, job); 3411 3412 mutex_lock(&sched->lock); 3413 group->timedout = true; 3414 if (group->csg_id >= 0) { 3415 sched_queue_delayed_work(ptdev->scheduler, tick, 0); 3416 } else { 3417 /* Remove from the run queues, so the scheduler can't 3418 * pick the group on the next tick. 3419 */ 3420 list_del_init(&group->run_node); 3421 list_del_init(&group->wait_node); 3422 3423 group_queue_work(group, term); 3424 } 3425 mutex_unlock(&sched->lock); 3426 3427 queue_start(queue); 3428 return DRM_GPU_SCHED_STAT_RESET; 3429 } 3430 3431 static void queue_free_job(struct drm_sched_job *sched_job) 3432 { 3433 drm_sched_job_cleanup(sched_job); 3434 panthor_job_put(sched_job); 3435 } 3436 3437 static const struct drm_sched_backend_ops panthor_queue_sched_ops = { 3438 .run_job = queue_run_job, 3439 .timedout_job = queue_timedout_job, 3440 .free_job = queue_free_job, 3441 }; 3442 3443 static u32 calc_profiling_ringbuf_num_slots(struct panthor_device *ptdev, 3444 u32 cs_ringbuf_size) 3445 { 3446 u32 min_profiled_job_instrs = U32_MAX; 3447 u32 last_flag = fls(PANTHOR_DEVICE_PROFILING_ALL); 3448 3449 /* 3450 * We want to calculate the minimum size of a profiled job's CS, 3451 * because since they need additional instructions for the sampling 3452 * of performance metrics, they might take up further slots in 3453 * the queue's ringbuffer. This means we might not need as many job 3454 * slots for keeping track of their profiling information. What we 3455 * need is the maximum number of slots we should allocate to this end, 3456 * which matches the maximum number of profiled jobs we can place 3457 * simultaneously in the queue's ring buffer. 3458 * That has to be calculated separately for every single job profiling 3459 * flag, but not in the case job profiling is disabled, since unprofiled 3460 * jobs don't need to keep track of this at all. 3461 */ 3462 for (u32 i = 0; i < last_flag; i++) { 3463 min_profiled_job_instrs = 3464 min(min_profiled_job_instrs, calc_job_credits(BIT(i))); 3465 } 3466 3467 return DIV_ROUND_UP(cs_ringbuf_size, min_profiled_job_instrs * sizeof(u64)); 3468 } 3469 3470 static void queue_timeout_work(struct work_struct *work) 3471 { 3472 struct panthor_queue *queue = container_of(work, struct panthor_queue, 3473 timeout.work.work); 3474 bool progress; 3475 3476 progress = queue_check_job_completion(queue); 3477 if (!progress) 3478 drm_sched_fault(&queue->scheduler); 3479 } 3480 3481 static struct panthor_queue * 3482 group_create_queue(struct panthor_group *group, 3483 const struct drm_panthor_queue_create *args, 3484 u64 drm_client_id, u32 gid, u32 qid) 3485 { 3486 struct drm_sched_init_args sched_args = { 3487 .ops = &panthor_queue_sched_ops, 3488 .submit_wq = group->ptdev->scheduler->wq, 3489 .num_rqs = 1, 3490 /* 3491 * The credit limit argument tells us the total number of 3492 * instructions across all CS slots in the ringbuffer, with 3493 * some jobs requiring twice as many as others, depending on 3494 * their profiling status. 3495 */ 3496 .credit_limit = args->ringbuf_size / sizeof(u64), 3497 .timeout = MAX_SCHEDULE_TIMEOUT, 3498 .timeout_wq = group->ptdev->reset.wq, 3499 .dev = group->ptdev->base.dev, 3500 }; 3501 struct drm_gpu_scheduler *drm_sched; 3502 struct panthor_queue *queue; 3503 int ret; 3504 3505 if (args->pad[0] || args->pad[1] || args->pad[2]) 3506 return ERR_PTR(-EINVAL); 3507 3508 if (args->ringbuf_size < SZ_4K || args->ringbuf_size > SZ_64K || 3509 !is_power_of_2(args->ringbuf_size)) 3510 return ERR_PTR(-EINVAL); 3511 3512 if (args->priority > CSF_MAX_QUEUE_PRIO) 3513 return ERR_PTR(-EINVAL); 3514 3515 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 3516 if (!queue) 3517 return ERR_PTR(-ENOMEM); 3518 3519 queue->timeout.remaining = msecs_to_jiffies(JOB_TIMEOUT_MS); 3520 INIT_DELAYED_WORK(&queue->timeout.work, queue_timeout_work); 3521 queue->fence_ctx.id = dma_fence_context_alloc(1); 3522 spin_lock_init(&queue->fence_ctx.lock); 3523 INIT_LIST_HEAD(&queue->fence_ctx.in_flight_jobs); 3524 3525 queue->priority = args->priority; 3526 3527 queue->ringbuf = panthor_kernel_bo_create(group->ptdev, group->vm, 3528 args->ringbuf_size, 3529 DRM_PANTHOR_BO_NO_MMAP, 3530 DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | 3531 DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED, 3532 PANTHOR_VM_KERNEL_AUTO_VA, 3533 "CS ring buffer"); 3534 if (IS_ERR(queue->ringbuf)) { 3535 ret = PTR_ERR(queue->ringbuf); 3536 goto err_free_queue; 3537 } 3538 3539 ret = panthor_kernel_bo_vmap(queue->ringbuf); 3540 if (ret) 3541 goto err_free_queue; 3542 3543 queue->iface.mem = panthor_fw_alloc_queue_iface_mem(group->ptdev, 3544 &queue->iface.input, 3545 &queue->iface.output, 3546 &queue->iface.input_fw_va, 3547 &queue->iface.output_fw_va); 3548 if (IS_ERR(queue->iface.mem)) { 3549 ret = PTR_ERR(queue->iface.mem); 3550 goto err_free_queue; 3551 } 3552 3553 queue->profiling.slot_count = 3554 calc_profiling_ringbuf_num_slots(group->ptdev, args->ringbuf_size); 3555 3556 queue->profiling.slots = 3557 panthor_kernel_bo_create(group->ptdev, group->vm, 3558 queue->profiling.slot_count * 3559 sizeof(struct panthor_job_profiling_data), 3560 DRM_PANTHOR_BO_NO_MMAP, 3561 DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | 3562 DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED, 3563 PANTHOR_VM_KERNEL_AUTO_VA, 3564 "Group job stats"); 3565 3566 if (IS_ERR(queue->profiling.slots)) { 3567 ret = PTR_ERR(queue->profiling.slots); 3568 goto err_free_queue; 3569 } 3570 3571 ret = panthor_kernel_bo_vmap(queue->profiling.slots); 3572 if (ret) 3573 goto err_free_queue; 3574 3575 /* assign a unique name */ 3576 queue->name = kasprintf(GFP_KERNEL, "panthor-queue-%llu-%u-%u", drm_client_id, gid, qid); 3577 if (!queue->name) { 3578 ret = -ENOMEM; 3579 goto err_free_queue; 3580 } 3581 3582 sched_args.name = queue->name; 3583 3584 ret = drm_sched_init(&queue->scheduler, &sched_args); 3585 if (ret) 3586 goto err_free_queue; 3587 3588 drm_sched = &queue->scheduler; 3589 ret = drm_sched_entity_init(&queue->entity, 0, &drm_sched, 1, NULL); 3590 if (ret) 3591 goto err_free_queue; 3592 3593 return queue; 3594 3595 err_free_queue: 3596 group_free_queue(group, queue); 3597 return ERR_PTR(ret); 3598 } 3599 3600 static void group_init_task_info(struct panthor_group *group) 3601 { 3602 struct task_struct *task = current->group_leader; 3603 3604 group->task_info.pid = task->pid; 3605 get_task_comm(group->task_info.comm, task); 3606 } 3607 3608 static void add_group_kbo_sizes(struct panthor_device *ptdev, 3609 struct panthor_group *group) 3610 { 3611 struct panthor_queue *queue; 3612 int i; 3613 3614 if (drm_WARN_ON(&ptdev->base, IS_ERR_OR_NULL(group))) 3615 return; 3616 if (drm_WARN_ON(&ptdev->base, ptdev != group->ptdev)) 3617 return; 3618 3619 group->fdinfo.kbo_sizes += group->suspend_buf->obj->size; 3620 group->fdinfo.kbo_sizes += group->protm_suspend_buf->obj->size; 3621 group->fdinfo.kbo_sizes += group->syncobjs->obj->size; 3622 3623 for (i = 0; i < group->queue_count; i++) { 3624 queue = group->queues[i]; 3625 group->fdinfo.kbo_sizes += queue->ringbuf->obj->size; 3626 group->fdinfo.kbo_sizes += queue->iface.mem->obj->size; 3627 group->fdinfo.kbo_sizes += queue->profiling.slots->obj->size; 3628 } 3629 } 3630 3631 #define MAX_GROUPS_PER_POOL 128 3632 3633 int panthor_group_create(struct panthor_file *pfile, 3634 const struct drm_panthor_group_create *group_args, 3635 const struct drm_panthor_queue_create *queue_args, 3636 u64 drm_client_id) 3637 { 3638 struct panthor_device *ptdev = pfile->ptdev; 3639 struct panthor_group_pool *gpool = pfile->groups; 3640 struct panthor_scheduler *sched = ptdev->scheduler; 3641 struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, 0); 3642 struct panthor_group *group = NULL; 3643 u32 gid, i, suspend_size; 3644 int ret; 3645 3646 if (group_args->pad) 3647 return -EINVAL; 3648 3649 if (group_args->priority >= PANTHOR_CSG_PRIORITY_COUNT) 3650 return -EINVAL; 3651 3652 if ((group_args->compute_core_mask & ~ptdev->gpu_info.shader_present) || 3653 (group_args->fragment_core_mask & ~ptdev->gpu_info.shader_present) || 3654 (group_args->tiler_core_mask & ~ptdev->gpu_info.tiler_present)) 3655 return -EINVAL; 3656 3657 if (hweight64(group_args->compute_core_mask) < group_args->max_compute_cores || 3658 hweight64(group_args->fragment_core_mask) < group_args->max_fragment_cores || 3659 hweight64(group_args->tiler_core_mask) < group_args->max_tiler_cores) 3660 return -EINVAL; 3661 3662 group = kzalloc(sizeof(*group), GFP_KERNEL); 3663 if (!group) 3664 return -ENOMEM; 3665 3666 spin_lock_init(&group->fatal_lock); 3667 kref_init(&group->refcount); 3668 group->state = PANTHOR_CS_GROUP_CREATED; 3669 group->csg_id = -1; 3670 3671 group->ptdev = ptdev; 3672 group->max_compute_cores = group_args->max_compute_cores; 3673 group->compute_core_mask = group_args->compute_core_mask; 3674 group->max_fragment_cores = group_args->max_fragment_cores; 3675 group->fragment_core_mask = group_args->fragment_core_mask; 3676 group->max_tiler_cores = group_args->max_tiler_cores; 3677 group->tiler_core_mask = group_args->tiler_core_mask; 3678 group->priority = group_args->priority; 3679 3680 INIT_LIST_HEAD(&group->wait_node); 3681 INIT_LIST_HEAD(&group->run_node); 3682 INIT_WORK(&group->term_work, group_term_work); 3683 INIT_WORK(&group->sync_upd_work, group_sync_upd_work); 3684 INIT_WORK(&group->tiler_oom_work, group_tiler_oom_work); 3685 INIT_WORK(&group->release_work, group_release_work); 3686 3687 group->vm = panthor_vm_pool_get_vm(pfile->vms, group_args->vm_id); 3688 if (!group->vm) { 3689 ret = -EINVAL; 3690 goto err_put_group; 3691 } 3692 3693 suspend_size = csg_iface->control->suspend_size; 3694 group->suspend_buf = panthor_fw_alloc_suspend_buf_mem(ptdev, suspend_size); 3695 if (IS_ERR(group->suspend_buf)) { 3696 ret = PTR_ERR(group->suspend_buf); 3697 group->suspend_buf = NULL; 3698 goto err_put_group; 3699 } 3700 3701 suspend_size = csg_iface->control->protm_suspend_size; 3702 group->protm_suspend_buf = panthor_fw_alloc_suspend_buf_mem(ptdev, suspend_size); 3703 if (IS_ERR(group->protm_suspend_buf)) { 3704 ret = PTR_ERR(group->protm_suspend_buf); 3705 group->protm_suspend_buf = NULL; 3706 goto err_put_group; 3707 } 3708 3709 group->syncobjs = panthor_kernel_bo_create(ptdev, group->vm, 3710 group_args->queues.count * 3711 sizeof(struct panthor_syncobj_64b), 3712 DRM_PANTHOR_BO_NO_MMAP, 3713 DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | 3714 DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED, 3715 PANTHOR_VM_KERNEL_AUTO_VA, 3716 "Group sync objects"); 3717 if (IS_ERR(group->syncobjs)) { 3718 ret = PTR_ERR(group->syncobjs); 3719 goto err_put_group; 3720 } 3721 3722 ret = panthor_kernel_bo_vmap(group->syncobjs); 3723 if (ret) 3724 goto err_put_group; 3725 3726 memset(group->syncobjs->kmap, 0, 3727 group_args->queues.count * sizeof(struct panthor_syncobj_64b)); 3728 3729 ret = xa_alloc(&gpool->xa, &gid, group, XA_LIMIT(1, MAX_GROUPS_PER_POOL), GFP_KERNEL); 3730 if (ret) 3731 goto err_put_group; 3732 3733 for (i = 0; i < group_args->queues.count; i++) { 3734 group->queues[i] = group_create_queue(group, &queue_args[i], drm_client_id, gid, i); 3735 if (IS_ERR(group->queues[i])) { 3736 ret = PTR_ERR(group->queues[i]); 3737 group->queues[i] = NULL; 3738 goto err_erase_gid; 3739 } 3740 3741 group->queue_count++; 3742 } 3743 3744 group->idle_queues = GENMASK(group->queue_count - 1, 0); 3745 3746 mutex_lock(&sched->reset.lock); 3747 if (atomic_read(&sched->reset.in_progress)) { 3748 panthor_group_stop(group); 3749 } else { 3750 mutex_lock(&sched->lock); 3751 list_add_tail(&group->run_node, 3752 &sched->groups.idle[group->priority]); 3753 mutex_unlock(&sched->lock); 3754 } 3755 mutex_unlock(&sched->reset.lock); 3756 3757 add_group_kbo_sizes(group->ptdev, group); 3758 spin_lock_init(&group->fdinfo.lock); 3759 3760 group_init_task_info(group); 3761 3762 xa_set_mark(&gpool->xa, gid, GROUP_REGISTERED); 3763 3764 return gid; 3765 3766 err_erase_gid: 3767 xa_erase(&gpool->xa, gid); 3768 3769 err_put_group: 3770 group_put(group); 3771 return ret; 3772 } 3773 3774 int panthor_group_destroy(struct panthor_file *pfile, u32 group_handle) 3775 { 3776 struct panthor_group_pool *gpool = pfile->groups; 3777 struct panthor_device *ptdev = pfile->ptdev; 3778 struct panthor_scheduler *sched = ptdev->scheduler; 3779 struct panthor_group *group; 3780 3781 if (!xa_get_mark(&gpool->xa, group_handle, GROUP_REGISTERED)) 3782 return -EINVAL; 3783 3784 group = xa_erase(&gpool->xa, group_handle); 3785 if (!group) 3786 return -EINVAL; 3787 3788 mutex_lock(&sched->reset.lock); 3789 mutex_lock(&sched->lock); 3790 group->destroyed = true; 3791 if (group->csg_id >= 0) { 3792 sched_queue_delayed_work(sched, tick, 0); 3793 } else if (!atomic_read(&sched->reset.in_progress)) { 3794 /* Remove from the run queues, so the scheduler can't 3795 * pick the group on the next tick. 3796 */ 3797 list_del_init(&group->run_node); 3798 list_del_init(&group->wait_node); 3799 group_queue_work(group, term); 3800 } 3801 mutex_unlock(&sched->lock); 3802 mutex_unlock(&sched->reset.lock); 3803 3804 group_put(group); 3805 return 0; 3806 } 3807 3808 static struct panthor_group *group_from_handle(struct panthor_group_pool *pool, 3809 unsigned long group_handle) 3810 { 3811 struct panthor_group *group; 3812 3813 xa_lock(&pool->xa); 3814 group = group_get(xa_find(&pool->xa, &group_handle, group_handle, GROUP_REGISTERED)); 3815 xa_unlock(&pool->xa); 3816 3817 return group; 3818 } 3819 3820 int panthor_group_get_state(struct panthor_file *pfile, 3821 struct drm_panthor_group_get_state *get_state) 3822 { 3823 struct panthor_group_pool *gpool = pfile->groups; 3824 struct panthor_device *ptdev = pfile->ptdev; 3825 struct panthor_scheduler *sched = ptdev->scheduler; 3826 struct panthor_group *group; 3827 3828 if (get_state->pad) 3829 return -EINVAL; 3830 3831 group = group_from_handle(gpool, get_state->group_handle); 3832 if (!group) 3833 return -EINVAL; 3834 3835 memset(get_state, 0, sizeof(*get_state)); 3836 3837 mutex_lock(&sched->lock); 3838 if (group->timedout) 3839 get_state->state |= DRM_PANTHOR_GROUP_STATE_TIMEDOUT; 3840 if (group->fatal_queues) { 3841 get_state->state |= DRM_PANTHOR_GROUP_STATE_FATAL_FAULT; 3842 get_state->fatal_queues = group->fatal_queues; 3843 } 3844 if (group->innocent) 3845 get_state->state |= DRM_PANTHOR_GROUP_STATE_INNOCENT; 3846 mutex_unlock(&sched->lock); 3847 3848 group_put(group); 3849 return 0; 3850 } 3851 3852 int panthor_group_pool_create(struct panthor_file *pfile) 3853 { 3854 struct panthor_group_pool *gpool; 3855 3856 gpool = kzalloc(sizeof(*gpool), GFP_KERNEL); 3857 if (!gpool) 3858 return -ENOMEM; 3859 3860 xa_init_flags(&gpool->xa, XA_FLAGS_ALLOC1); 3861 pfile->groups = gpool; 3862 return 0; 3863 } 3864 3865 void panthor_group_pool_destroy(struct panthor_file *pfile) 3866 { 3867 struct panthor_group_pool *gpool = pfile->groups; 3868 struct panthor_group *group; 3869 unsigned long i; 3870 3871 if (IS_ERR_OR_NULL(gpool)) 3872 return; 3873 3874 xa_for_each(&gpool->xa, i, group) 3875 panthor_group_destroy(pfile, i); 3876 3877 xa_destroy(&gpool->xa); 3878 kfree(gpool); 3879 pfile->groups = NULL; 3880 } 3881 3882 /** 3883 * panthor_fdinfo_gather_group_mem_info() - Retrieve aggregate size of all private kernel BO's 3884 * belonging to all the groups owned by an open Panthor file 3885 * @pfile: File. 3886 * @stats: Memory statistics to be updated. 3887 * 3888 */ 3889 void 3890 panthor_fdinfo_gather_group_mem_info(struct panthor_file *pfile, 3891 struct drm_memory_stats *stats) 3892 { 3893 struct panthor_group_pool *gpool = pfile->groups; 3894 struct panthor_group *group; 3895 unsigned long i; 3896 3897 if (IS_ERR_OR_NULL(gpool)) 3898 return; 3899 3900 xa_lock(&gpool->xa); 3901 xa_for_each_marked(&gpool->xa, i, group, GROUP_REGISTERED) { 3902 stats->resident += group->fdinfo.kbo_sizes; 3903 if (group->csg_id >= 0) 3904 stats->active += group->fdinfo.kbo_sizes; 3905 } 3906 xa_unlock(&gpool->xa); 3907 } 3908 3909 static void job_release(struct kref *ref) 3910 { 3911 struct panthor_job *job = container_of(ref, struct panthor_job, refcount); 3912 3913 drm_WARN_ON(&job->group->ptdev->base, !list_empty(&job->node)); 3914 3915 if (job->base.s_fence) 3916 drm_sched_job_cleanup(&job->base); 3917 3918 if (job->done_fence && job->done_fence->ops) 3919 dma_fence_put(job->done_fence); 3920 else 3921 dma_fence_free(job->done_fence); 3922 3923 group_put(job->group); 3924 3925 kfree(job); 3926 } 3927 3928 struct drm_sched_job *panthor_job_get(struct drm_sched_job *sched_job) 3929 { 3930 if (sched_job) { 3931 struct panthor_job *job = container_of(sched_job, struct panthor_job, base); 3932 3933 kref_get(&job->refcount); 3934 } 3935 3936 return sched_job; 3937 } 3938 3939 void panthor_job_put(struct drm_sched_job *sched_job) 3940 { 3941 struct panthor_job *job = container_of(sched_job, struct panthor_job, base); 3942 3943 if (sched_job) 3944 kref_put(&job->refcount, job_release); 3945 } 3946 3947 struct panthor_vm *panthor_job_vm(struct drm_sched_job *sched_job) 3948 { 3949 struct panthor_job *job = container_of(sched_job, struct panthor_job, base); 3950 3951 return job->group->vm; 3952 } 3953 3954 struct drm_sched_job * 3955 panthor_job_create(struct panthor_file *pfile, 3956 u16 group_handle, 3957 const struct drm_panthor_queue_submit *qsubmit, 3958 u64 drm_client_id) 3959 { 3960 struct panthor_group_pool *gpool = pfile->groups; 3961 struct panthor_job *job; 3962 u32 credits; 3963 int ret; 3964 3965 if (qsubmit->pad) 3966 return ERR_PTR(-EINVAL); 3967 3968 /* If stream_addr is zero, so stream_size should be. */ 3969 if ((qsubmit->stream_size == 0) != (qsubmit->stream_addr == 0)) 3970 return ERR_PTR(-EINVAL); 3971 3972 /* Make sure the address is aligned on 64-byte (cacheline) and the size is 3973 * aligned on 8-byte (instruction size). 3974 */ 3975 if ((qsubmit->stream_addr & 63) || (qsubmit->stream_size & 7)) 3976 return ERR_PTR(-EINVAL); 3977 3978 /* bits 24:30 must be zero. */ 3979 if (qsubmit->latest_flush & GENMASK(30, 24)) 3980 return ERR_PTR(-EINVAL); 3981 3982 job = kzalloc(sizeof(*job), GFP_KERNEL); 3983 if (!job) 3984 return ERR_PTR(-ENOMEM); 3985 3986 kref_init(&job->refcount); 3987 job->queue_idx = qsubmit->queue_index; 3988 job->call_info.size = qsubmit->stream_size; 3989 job->call_info.start = qsubmit->stream_addr; 3990 job->call_info.latest_flush = qsubmit->latest_flush; 3991 INIT_LIST_HEAD(&job->node); 3992 3993 job->group = group_from_handle(gpool, group_handle); 3994 if (!job->group) { 3995 ret = -EINVAL; 3996 goto err_put_job; 3997 } 3998 3999 if (!group_can_run(job->group)) { 4000 ret = -EINVAL; 4001 goto err_put_job; 4002 } 4003 4004 if (job->queue_idx >= job->group->queue_count || 4005 !job->group->queues[job->queue_idx]) { 4006 ret = -EINVAL; 4007 goto err_put_job; 4008 } 4009 4010 /* Empty command streams don't need a fence, they'll pick the one from 4011 * the previously submitted job. 4012 */ 4013 if (job->call_info.size) { 4014 job->done_fence = kzalloc(sizeof(*job->done_fence), GFP_KERNEL); 4015 if (!job->done_fence) { 4016 ret = -ENOMEM; 4017 goto err_put_job; 4018 } 4019 } 4020 4021 job->profiling.mask = pfile->ptdev->profile_mask; 4022 credits = calc_job_credits(job->profiling.mask); 4023 if (credits == 0) { 4024 ret = -EINVAL; 4025 goto err_put_job; 4026 } 4027 4028 ret = drm_sched_job_init(&job->base, 4029 &job->group->queues[job->queue_idx]->entity, 4030 credits, job->group, drm_client_id); 4031 if (ret) 4032 goto err_put_job; 4033 4034 return &job->base; 4035 4036 err_put_job: 4037 panthor_job_put(&job->base); 4038 return ERR_PTR(ret); 4039 } 4040 4041 void panthor_job_update_resvs(struct drm_exec *exec, struct drm_sched_job *sched_job) 4042 { 4043 struct panthor_job *job = container_of(sched_job, struct panthor_job, base); 4044 4045 panthor_vm_update_resvs(job->group->vm, exec, &sched_job->s_fence->finished, 4046 DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP); 4047 } 4048 4049 void panthor_sched_unplug(struct panthor_device *ptdev) 4050 { 4051 struct panthor_scheduler *sched = ptdev->scheduler; 4052 4053 disable_delayed_work_sync(&sched->tick_work); 4054 disable_work_sync(&sched->fw_events_work); 4055 disable_work_sync(&sched->sync_upd_work); 4056 4057 mutex_lock(&sched->lock); 4058 if (sched->pm.has_ref) { 4059 pm_runtime_put(ptdev->base.dev); 4060 sched->pm.has_ref = false; 4061 } 4062 mutex_unlock(&sched->lock); 4063 } 4064 4065 static void panthor_sched_fini(struct drm_device *ddev, void *res) 4066 { 4067 struct panthor_scheduler *sched = res; 4068 int prio; 4069 4070 if (!sched || !sched->csg_slot_count) 4071 return; 4072 4073 if (sched->wq) 4074 destroy_workqueue(sched->wq); 4075 4076 if (sched->heap_alloc_wq) 4077 destroy_workqueue(sched->heap_alloc_wq); 4078 4079 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) { 4080 drm_WARN_ON(ddev, !list_empty(&sched->groups.runnable[prio])); 4081 drm_WARN_ON(ddev, !list_empty(&sched->groups.idle[prio])); 4082 } 4083 4084 drm_WARN_ON(ddev, !list_empty(&sched->groups.waiting)); 4085 } 4086 4087 int panthor_sched_init(struct panthor_device *ptdev) 4088 { 4089 struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev); 4090 struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, 0); 4091 struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, 0, 0); 4092 struct panthor_scheduler *sched; 4093 u32 gpu_as_count, num_groups; 4094 int prio, ret; 4095 4096 sched = drmm_kzalloc(&ptdev->base, sizeof(*sched), GFP_KERNEL); 4097 if (!sched) 4098 return -ENOMEM; 4099 4100 /* The highest bit in JOB_INT_* is reserved for globabl IRQs. That 4101 * leaves 31 bits for CSG IRQs, hence the MAX_CSGS clamp here. 4102 */ 4103 num_groups = min_t(u32, MAX_CSGS, glb_iface->control->group_num); 4104 4105 /* The FW-side scheduler might deadlock if two groups with the same 4106 * priority try to access a set of resources that overlaps, with part 4107 * of the resources being allocated to one group and the other part to 4108 * the other group, both groups waiting for the remaining resources to 4109 * be allocated. To avoid that, it is recommended to assign each CSG a 4110 * different priority. In theory we could allow several groups to have 4111 * the same CSG priority if they don't request the same resources, but 4112 * that makes the scheduling logic more complicated, so let's clamp 4113 * the number of CSG slots to MAX_CSG_PRIO + 1 for now. 4114 */ 4115 num_groups = min_t(u32, MAX_CSG_PRIO + 1, num_groups); 4116 4117 /* We need at least one AS for the MCU and one for the GPU contexts. */ 4118 gpu_as_count = hweight32(ptdev->gpu_info.as_present & GENMASK(31, 1)); 4119 if (!gpu_as_count) { 4120 drm_err(&ptdev->base, "Not enough AS (%d, expected at least 2)", 4121 gpu_as_count + 1); 4122 return -EINVAL; 4123 } 4124 4125 sched->ptdev = ptdev; 4126 sched->sb_slot_count = CS_FEATURES_SCOREBOARDS(cs_iface->control->features); 4127 sched->csg_slot_count = num_groups; 4128 sched->cs_slot_count = csg_iface->control->stream_num; 4129 sched->as_slot_count = gpu_as_count; 4130 ptdev->csif_info.csg_slot_count = sched->csg_slot_count; 4131 ptdev->csif_info.cs_slot_count = sched->cs_slot_count; 4132 ptdev->csif_info.scoreboard_slot_count = sched->sb_slot_count; 4133 4134 sched->last_tick = 0; 4135 sched->resched_target = U64_MAX; 4136 sched->tick_period = msecs_to_jiffies(10); 4137 INIT_DELAYED_WORK(&sched->tick_work, tick_work); 4138 INIT_WORK(&sched->sync_upd_work, sync_upd_work); 4139 INIT_WORK(&sched->fw_events_work, process_fw_events_work); 4140 4141 ret = drmm_mutex_init(&ptdev->base, &sched->lock); 4142 if (ret) 4143 return ret; 4144 4145 for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) { 4146 INIT_LIST_HEAD(&sched->groups.runnable[prio]); 4147 INIT_LIST_HEAD(&sched->groups.idle[prio]); 4148 } 4149 INIT_LIST_HEAD(&sched->groups.waiting); 4150 4151 ret = drmm_mutex_init(&ptdev->base, &sched->reset.lock); 4152 if (ret) 4153 return ret; 4154 4155 INIT_LIST_HEAD(&sched->reset.stopped_groups); 4156 4157 /* sched->heap_alloc_wq will be used for heap chunk allocation on 4158 * tiler OOM events, which means we can't use the same workqueue for 4159 * the scheduler because works queued by the scheduler are in 4160 * the dma-signalling path. Allocate a dedicated heap_alloc_wq to 4161 * work around this limitation. 4162 * 4163 * FIXME: Ultimately, what we need is a failable/non-blocking GEM 4164 * allocation path that we can call when a heap OOM is reported. The 4165 * FW is smart enough to fall back on other methods if the kernel can't 4166 * allocate memory, and fail the tiling job if none of these 4167 * countermeasures worked. 4168 * 4169 * Set WQ_MEM_RECLAIM on sched->wq to unblock the situation when the 4170 * system is running out of memory. 4171 */ 4172 sched->heap_alloc_wq = alloc_workqueue("panthor-heap-alloc", WQ_UNBOUND, 0); 4173 sched->wq = alloc_workqueue("panthor-csf-sched", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); 4174 if (!sched->wq || !sched->heap_alloc_wq) { 4175 panthor_sched_fini(&ptdev->base, sched); 4176 drm_err(&ptdev->base, "Failed to allocate the workqueues"); 4177 return -ENOMEM; 4178 } 4179 4180 ret = drmm_add_action_or_reset(&ptdev->base, panthor_sched_fini, sched); 4181 if (ret) 4182 return ret; 4183 4184 ptdev->scheduler = sched; 4185 return 0; 4186 } 4187