1 // SPDX-License-Identifier: GPL-2.0 or MIT 2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */ 3 /* Copyright 2023 Collabora ltd. */ 4 5 #include <drm/drm_debugfs.h> 6 #include <drm/drm_drv.h> 7 #include <drm/drm_exec.h> 8 #include <drm/drm_gpuvm.h> 9 #include <drm/drm_managed.h> 10 #include <drm/gpu_scheduler.h> 11 #include <drm/panthor_drm.h> 12 13 #include <linux/atomic.h> 14 #include <linux/bitfield.h> 15 #include <linux/delay.h> 16 #include <linux/dma-mapping.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/iopoll.h> 20 #include <linux/io-pgtable.h> 21 #include <linux/iommu.h> 22 #include <linux/kmemleak.h> 23 #include <linux/platform_device.h> 24 #include <linux/pm_runtime.h> 25 #include <linux/rwsem.h> 26 #include <linux/sched.h> 27 #include <linux/shmem_fs.h> 28 #include <linux/sizes.h> 29 30 #include "panthor_device.h" 31 #include "panthor_gem.h" 32 #include "panthor_heap.h" 33 #include "panthor_mmu.h" 34 #include "panthor_regs.h" 35 #include "panthor_sched.h" 36 37 #define MAX_AS_SLOTS 32 38 39 struct panthor_vm; 40 41 /** 42 * struct panthor_as_slot - Address space slot 43 */ 44 struct panthor_as_slot { 45 /** @vm: VM bound to this slot. NULL is no VM is bound. */ 46 struct panthor_vm *vm; 47 }; 48 49 /** 50 * struct panthor_mmu - MMU related data 51 */ 52 struct panthor_mmu { 53 /** @irq: The MMU irq. */ 54 struct panthor_irq irq; 55 56 /** 57 * @as: Address space related fields. 58 * 59 * The GPU has a limited number of address spaces (AS) slots, forcing 60 * us to re-assign them to re-assign slots on-demand. 61 */ 62 struct { 63 /** @as.slots_lock: Lock protecting access to all other AS fields. */ 64 struct mutex slots_lock; 65 66 /** @as.alloc_mask: Bitmask encoding the allocated slots. */ 67 unsigned long alloc_mask; 68 69 /** @as.faulty_mask: Bitmask encoding the faulty slots. */ 70 unsigned long faulty_mask; 71 72 /** @as.slots: VMs currently bound to the AS slots. */ 73 struct panthor_as_slot slots[MAX_AS_SLOTS]; 74 75 /** 76 * @as.lru_list: List of least recently used VMs. 77 * 78 * We use this list to pick a VM to evict when all slots are 79 * used. 80 * 81 * There should be no more active VMs than there are AS slots, 82 * so this LRU is just here to keep VMs bound until there's 83 * a need to release a slot, thus avoid unnecessary TLB/cache 84 * flushes. 85 */ 86 struct list_head lru_list; 87 } as; 88 89 /** @vm: VMs management fields */ 90 struct { 91 /** @vm.lock: Lock protecting access to list. */ 92 struct mutex lock; 93 94 /** @vm.list: List containing all VMs. */ 95 struct list_head list; 96 97 /** @vm.reset_in_progress: True if a reset is in progress. */ 98 bool reset_in_progress; 99 100 /** @vm.wq: Workqueue used for the VM_BIND queues. */ 101 struct workqueue_struct *wq; 102 } vm; 103 }; 104 105 /** 106 * struct panthor_vm_pool - VM pool object 107 */ 108 struct panthor_vm_pool { 109 /** @xa: Array used for VM handle tracking. */ 110 struct xarray xa; 111 }; 112 113 /** 114 * struct panthor_vma - GPU mapping object 115 * 116 * This is used to track GEM mappings in GPU space. 117 */ 118 struct panthor_vma { 119 /** @base: Inherits from drm_gpuva. */ 120 struct drm_gpuva base; 121 122 /** @node: Used to implement deferred release of VMAs. */ 123 struct list_head node; 124 125 /** 126 * @flags: Combination of drm_panthor_vm_bind_op_flags. 127 * 128 * Only map related flags are accepted. 129 */ 130 u32 flags; 131 }; 132 133 /** 134 * struct panthor_vm_op_ctx - VM operation context 135 * 136 * With VM operations potentially taking place in a dma-signaling path, we 137 * need to make sure everything that might require resource allocation is 138 * pre-allocated upfront. This is what this operation context is far. 139 * 140 * We also collect resources that have been freed, so we can release them 141 * asynchronously, and let the VM_BIND scheduler process the next VM_BIND 142 * request. 143 */ 144 struct panthor_vm_op_ctx { 145 /** @rsvd_page_tables: Pages reserved for the MMU page table update. */ 146 struct { 147 /** @rsvd_page_tables.count: Number of pages reserved. */ 148 u32 count; 149 150 /** @rsvd_page_tables.ptr: Point to the first unused page in the @pages table. */ 151 u32 ptr; 152 153 /** 154 * @rsvd_page_tables.pages: Array of pages to be used for an MMU page table update. 155 * 156 * After an VM operation, there might be free pages left in this array. 157 * They should be returned to the pt_cache as part of the op_ctx cleanup. 158 */ 159 void **pages; 160 } rsvd_page_tables; 161 162 /** 163 * @preallocated_vmas: Pre-allocated VMAs to handle the remap case. 164 * 165 * Partial unmap requests or map requests overlapping existing mappings will 166 * trigger a remap call, which need to register up to three panthor_vma objects 167 * (one for the new mapping, and two for the previous and next mappings). 168 */ 169 struct panthor_vma *preallocated_vmas[3]; 170 171 /** @flags: Combination of drm_panthor_vm_bind_op_flags. */ 172 u32 flags; 173 174 /** @va: Virtual range targeted by the VM operation. */ 175 struct { 176 /** @va.addr: Start address. */ 177 u64 addr; 178 179 /** @va.range: Range size. */ 180 u64 range; 181 } va; 182 183 /** 184 * @returned_vmas: List of panthor_vma objects returned after a VM operation. 185 * 186 * For unmap operations, this will contain all VMAs that were covered by the 187 * specified VA range. 188 * 189 * For map operations, this will contain all VMAs that previously mapped to 190 * the specified VA range. 191 * 192 * Those VMAs, and the resources they point to will be released as part of 193 * the op_ctx cleanup operation. 194 */ 195 struct list_head returned_vmas; 196 197 /** @map: Fields specific to a map operation. */ 198 struct { 199 /** @map.vm_bo: Buffer object to map. */ 200 struct drm_gpuvm_bo *vm_bo; 201 202 /** @map.bo_offset: Offset in the buffer object. */ 203 u64 bo_offset; 204 205 /** 206 * @map.sgt: sg-table pointing to pages backing the GEM object. 207 * 208 * This is gathered at job creation time, such that we don't have 209 * to allocate in ::run_job(). 210 */ 211 struct sg_table *sgt; 212 213 /** 214 * @map.new_vma: The new VMA object that will be inserted to the VA tree. 215 */ 216 struct panthor_vma *new_vma; 217 } map; 218 }; 219 220 /** 221 * struct panthor_vm - VM object 222 * 223 * A VM is an object representing a GPU (or MCU) virtual address space. 224 * It embeds the MMU page table for this address space, a tree containing 225 * all the virtual mappings of GEM objects, and other things needed to manage 226 * the VM. 227 * 228 * Except for the MCU VM, which is managed by the kernel, all other VMs are 229 * created by userspace and mostly managed by userspace, using the 230 * %DRM_IOCTL_PANTHOR_VM_BIND ioctl. 231 * 232 * A portion of the virtual address space is reserved for kernel objects, 233 * like heap chunks, and userspace gets to decide how much of the virtual 234 * address space is left to the kernel (half of the virtual address space 235 * by default). 236 */ 237 struct panthor_vm { 238 /** 239 * @base: Inherit from drm_gpuvm. 240 * 241 * We delegate all the VA management to the common drm_gpuvm framework 242 * and only implement hooks to update the MMU page table. 243 */ 244 struct drm_gpuvm base; 245 246 /** 247 * @sched: Scheduler used for asynchronous VM_BIND request. 248 * 249 * We use a 1:1 scheduler here. 250 */ 251 struct drm_gpu_scheduler sched; 252 253 /** 254 * @entity: Scheduling entity representing the VM_BIND queue. 255 * 256 * There's currently one bind queue per VM. It doesn't make sense to 257 * allow more given the VM operations are serialized anyway. 258 */ 259 struct drm_sched_entity entity; 260 261 /** @ptdev: Device. */ 262 struct panthor_device *ptdev; 263 264 /** @memattr: Value to program to the AS_MEMATTR register. */ 265 u64 memattr; 266 267 /** @pgtbl_ops: Page table operations. */ 268 struct io_pgtable_ops *pgtbl_ops; 269 270 /** @root_page_table: Stores the root page table pointer. */ 271 void *root_page_table; 272 273 /** 274 * @op_lock: Lock used to serialize operations on a VM. 275 * 276 * The serialization of jobs queued to the VM_BIND queue is already 277 * taken care of by drm_sched, but we need to serialize synchronous 278 * and asynchronous VM_BIND request. This is what this lock is for. 279 */ 280 struct mutex op_lock; 281 282 /** 283 * @op_ctx: The context attached to the currently executing VM operation. 284 * 285 * NULL when no operation is in progress. 286 */ 287 struct panthor_vm_op_ctx *op_ctx; 288 289 /** 290 * @mm: Memory management object representing the auto-VA/kernel-VA. 291 * 292 * Used to auto-allocate VA space for kernel-managed objects (tiler 293 * heaps, ...). 294 * 295 * For the MCU VM, this is managing the VA range that's used to map 296 * all shared interfaces. 297 * 298 * For user VMs, the range is specified by userspace, and must not 299 * exceed half of the VA space addressable. 300 */ 301 struct drm_mm mm; 302 303 /** @mm_lock: Lock protecting the @mm field. */ 304 struct mutex mm_lock; 305 306 /** @kernel_auto_va: Automatic VA-range for kernel BOs. */ 307 struct { 308 /** @kernel_auto_va.start: Start of the automatic VA-range for kernel BOs. */ 309 u64 start; 310 311 /** @kernel_auto_va.size: Size of the automatic VA-range for kernel BOs. */ 312 u64 end; 313 } kernel_auto_va; 314 315 /** @as: Address space related fields. */ 316 struct { 317 /** 318 * @as.id: ID of the address space this VM is bound to. 319 * 320 * A value of -1 means the VM is inactive/not bound. 321 */ 322 int id; 323 324 /** @as.active_cnt: Number of active users of this VM. */ 325 refcount_t active_cnt; 326 327 /** 328 * @as.lru_node: Used to instead the VM in the panthor_mmu::as::lru_list. 329 * 330 * Active VMs should not be inserted in the LRU list. 331 */ 332 struct list_head lru_node; 333 } as; 334 335 /** 336 * @heaps: Tiler heap related fields. 337 */ 338 struct { 339 /** 340 * @heaps.pool: The heap pool attached to this VM. 341 * 342 * Will stay NULL until someone creates a heap context on this VM. 343 */ 344 struct panthor_heap_pool *pool; 345 346 /** @heaps.lock: Lock used to protect access to @pool. */ 347 struct mutex lock; 348 } heaps; 349 350 /** @node: Used to insert the VM in the panthor_mmu::vm::list. */ 351 struct list_head node; 352 353 /** @for_mcu: True if this is the MCU VM. */ 354 bool for_mcu; 355 356 /** 357 * @destroyed: True if the VM was destroyed. 358 * 359 * No further bind requests should be queued to a destroyed VM. 360 */ 361 bool destroyed; 362 363 /** 364 * @unusable: True if the VM has turned unusable because something 365 * bad happened during an asynchronous request. 366 * 367 * We don't try to recover from such failures, because this implies 368 * informing userspace about the specific operation that failed, and 369 * hoping the userspace driver can replay things from there. This all 370 * sounds very complicated for little gain. 371 * 372 * Instead, we should just flag the VM as unusable, and fail any 373 * further request targeting this VM. 374 * 375 * We also provide a way to query a VM state, so userspace can destroy 376 * it and create a new one. 377 * 378 * As an analogy, this would be mapped to a VK_ERROR_DEVICE_LOST 379 * situation, where the logical device needs to be re-created. 380 */ 381 bool unusable; 382 383 /** 384 * @unhandled_fault: Unhandled fault happened. 385 * 386 * This should be reported to the scheduler, and the queue/group be 387 * flagged as faulty as a result. 388 */ 389 bool unhandled_fault; 390 }; 391 392 /** 393 * struct panthor_vm_bind_job - VM bind job 394 */ 395 struct panthor_vm_bind_job { 396 /** @base: Inherit from drm_sched_job. */ 397 struct drm_sched_job base; 398 399 /** @refcount: Reference count. */ 400 struct kref refcount; 401 402 /** @cleanup_op_ctx_work: Work used to cleanup the VM operation context. */ 403 struct work_struct cleanup_op_ctx_work; 404 405 /** @vm: VM targeted by the VM operation. */ 406 struct panthor_vm *vm; 407 408 /** @ctx: Operation context. */ 409 struct panthor_vm_op_ctx ctx; 410 }; 411 412 /* 413 * @pt_cache: Cache used to allocate MMU page tables. 414 * 415 * The pre-allocation pattern forces us to over-allocate to plan for 416 * the worst case scenario, and return the pages we didn't use. 417 * 418 * Having a kmem_cache allows us to speed allocations. 419 */ 420 static struct kmem_cache *pt_cache; 421 422 /** 423 * alloc_pt() - Custom page table allocator 424 * @cookie: Cookie passed at page table allocation time. 425 * @size: Size of the page table. This size should be fixed, 426 * and determined at creation time based on the granule size. 427 * @gfp: GFP flags. 428 * 429 * We want a custom allocator so we can use a cache for page table 430 * allocations and amortize the cost of the over-reservation that's 431 * done to allow asynchronous VM operations. 432 * 433 * Return: non-NULL on success, NULL if the allocation failed for any 434 * reason. 435 */ 436 static void *alloc_pt(void *cookie, size_t size, gfp_t gfp) 437 { 438 struct panthor_vm *vm = cookie; 439 void *page; 440 441 /* Allocation of the root page table happening during init. */ 442 if (unlikely(!vm->root_page_table)) { 443 struct page *p; 444 445 drm_WARN_ON(&vm->ptdev->base, vm->op_ctx); 446 p = alloc_pages_node(dev_to_node(vm->ptdev->base.dev), 447 gfp | __GFP_ZERO, get_order(size)); 448 page = p ? page_address(p) : NULL; 449 vm->root_page_table = page; 450 return page; 451 } 452 453 /* We're not supposed to have anything bigger than 4k here, because we picked a 454 * 4k granule size at init time. 455 */ 456 if (drm_WARN_ON(&vm->ptdev->base, size != SZ_4K)) 457 return NULL; 458 459 /* We must have some op_ctx attached to the VM and it must have at least one 460 * free page. 461 */ 462 if (drm_WARN_ON(&vm->ptdev->base, !vm->op_ctx) || 463 drm_WARN_ON(&vm->ptdev->base, 464 vm->op_ctx->rsvd_page_tables.ptr >= vm->op_ctx->rsvd_page_tables.count)) 465 return NULL; 466 467 page = vm->op_ctx->rsvd_page_tables.pages[vm->op_ctx->rsvd_page_tables.ptr++]; 468 memset(page, 0, SZ_4K); 469 470 /* Page table entries don't use virtual addresses, which trips out 471 * kmemleak. kmemleak_alloc_phys() might work, but physical addresses 472 * are mixed with other fields, and I fear kmemleak won't detect that 473 * either. 474 * 475 * Let's just ignore memory passed to the page-table driver for now. 476 */ 477 kmemleak_ignore(page); 478 return page; 479 } 480 481 /** 482 * free_pt() - Custom page table free function 483 * @cookie: Cookie passed at page table allocation time. 484 * @data: Page table to free. 485 * @size: Size of the page table. This size should be fixed, 486 * and determined at creation time based on the granule size. 487 */ 488 static void free_pt(void *cookie, void *data, size_t size) 489 { 490 struct panthor_vm *vm = cookie; 491 492 if (unlikely(vm->root_page_table == data)) { 493 free_pages((unsigned long)data, get_order(size)); 494 vm->root_page_table = NULL; 495 return; 496 } 497 498 if (drm_WARN_ON(&vm->ptdev->base, size != SZ_4K)) 499 return; 500 501 /* Return the page to the pt_cache. */ 502 kmem_cache_free(pt_cache, data); 503 } 504 505 static int wait_ready(struct panthor_device *ptdev, u32 as_nr) 506 { 507 int ret; 508 u32 val; 509 510 /* Wait for the MMU status to indicate there is no active command, in 511 * case one is pending. 512 */ 513 ret = gpu_read_relaxed_poll_timeout_atomic(ptdev, AS_STATUS(as_nr), val, 514 !(val & AS_STATUS_AS_ACTIVE), 515 10, 100000); 516 517 if (ret) { 518 panthor_device_schedule_reset(ptdev); 519 drm_err(&ptdev->base, "AS_ACTIVE bit stuck\n"); 520 } 521 522 return ret; 523 } 524 525 static int write_cmd(struct panthor_device *ptdev, u32 as_nr, u32 cmd) 526 { 527 int status; 528 529 /* write AS_COMMAND when MMU is ready to accept another command */ 530 status = wait_ready(ptdev, as_nr); 531 if (!status) 532 gpu_write(ptdev, AS_COMMAND(as_nr), cmd); 533 534 return status; 535 } 536 537 static void lock_region(struct panthor_device *ptdev, u32 as_nr, 538 u64 region_start, u64 size) 539 { 540 u8 region_width; 541 u64 region; 542 u64 region_end = region_start + size; 543 544 if (!size) 545 return; 546 547 /* 548 * The locked region is a naturally aligned power of 2 block encoded as 549 * log2 minus(1). 550 * Calculate the desired start/end and look for the highest bit which 551 * differs. The smallest naturally aligned block must include this bit 552 * change, the desired region starts with this bit (and subsequent bits) 553 * zeroed and ends with the bit (and subsequent bits) set to one. 554 */ 555 region_width = max(fls64(region_start ^ (region_end - 1)), 556 const_ilog2(AS_LOCK_REGION_MIN_SIZE)) - 1; 557 558 /* 559 * Mask off the low bits of region_start (which would be ignored by 560 * the hardware anyway) 561 */ 562 region_start &= GENMASK_ULL(63, region_width); 563 564 region = region_width | region_start; 565 566 /* Lock the region that needs to be updated */ 567 gpu_write64(ptdev, AS_LOCKADDR(as_nr), region); 568 write_cmd(ptdev, as_nr, AS_COMMAND_LOCK); 569 } 570 571 static int mmu_hw_do_operation_locked(struct panthor_device *ptdev, int as_nr, 572 u64 iova, u64 size, u32 op) 573 { 574 lockdep_assert_held(&ptdev->mmu->as.slots_lock); 575 576 if (as_nr < 0) 577 return 0; 578 579 /* 580 * If the AS number is greater than zero, then we can be sure 581 * the device is up and running, so we don't need to explicitly 582 * power it up 583 */ 584 585 if (op != AS_COMMAND_UNLOCK) 586 lock_region(ptdev, as_nr, iova, size); 587 588 /* Run the MMU operation */ 589 write_cmd(ptdev, as_nr, op); 590 591 /* Wait for the flush to complete */ 592 return wait_ready(ptdev, as_nr); 593 } 594 595 static int mmu_hw_do_operation(struct panthor_vm *vm, 596 u64 iova, u64 size, u32 op) 597 { 598 struct panthor_device *ptdev = vm->ptdev; 599 int ret; 600 601 mutex_lock(&ptdev->mmu->as.slots_lock); 602 ret = mmu_hw_do_operation_locked(ptdev, vm->as.id, iova, size, op); 603 mutex_unlock(&ptdev->mmu->as.slots_lock); 604 605 return ret; 606 } 607 608 static int panthor_mmu_as_enable(struct panthor_device *ptdev, u32 as_nr, 609 u64 transtab, u64 transcfg, u64 memattr) 610 { 611 int ret; 612 613 ret = mmu_hw_do_operation_locked(ptdev, as_nr, 0, ~0ULL, AS_COMMAND_FLUSH_MEM); 614 if (ret) 615 return ret; 616 617 gpu_write64(ptdev, AS_TRANSTAB(as_nr), transtab); 618 gpu_write64(ptdev, AS_MEMATTR(as_nr), memattr); 619 gpu_write64(ptdev, AS_TRANSCFG(as_nr), transcfg); 620 621 return write_cmd(ptdev, as_nr, AS_COMMAND_UPDATE); 622 } 623 624 static int panthor_mmu_as_disable(struct panthor_device *ptdev, u32 as_nr) 625 { 626 int ret; 627 628 ret = mmu_hw_do_operation_locked(ptdev, as_nr, 0, ~0ULL, AS_COMMAND_FLUSH_MEM); 629 if (ret) 630 return ret; 631 632 gpu_write64(ptdev, AS_TRANSTAB(as_nr), 0); 633 gpu_write64(ptdev, AS_MEMATTR(as_nr), 0); 634 gpu_write64(ptdev, AS_TRANSCFG(as_nr), AS_TRANSCFG_ADRMODE_UNMAPPED); 635 636 return write_cmd(ptdev, as_nr, AS_COMMAND_UPDATE); 637 } 638 639 static u32 panthor_mmu_fault_mask(struct panthor_device *ptdev, u32 value) 640 { 641 /* Bits 16 to 31 mean REQ_COMPLETE. */ 642 return value & GENMASK(15, 0); 643 } 644 645 static u32 panthor_mmu_as_fault_mask(struct panthor_device *ptdev, u32 as) 646 { 647 return BIT(as); 648 } 649 650 /** 651 * panthor_vm_has_unhandled_faults() - Check if a VM has unhandled faults 652 * @vm: VM to check. 653 * 654 * Return: true if the VM has unhandled faults, false otherwise. 655 */ 656 bool panthor_vm_has_unhandled_faults(struct panthor_vm *vm) 657 { 658 return vm->unhandled_fault; 659 } 660 661 /** 662 * panthor_vm_is_unusable() - Check if the VM is still usable 663 * @vm: VM to check. 664 * 665 * Return: true if the VM is unusable, false otherwise. 666 */ 667 bool panthor_vm_is_unusable(struct panthor_vm *vm) 668 { 669 return vm->unusable; 670 } 671 672 static void panthor_vm_release_as_locked(struct panthor_vm *vm) 673 { 674 struct panthor_device *ptdev = vm->ptdev; 675 676 lockdep_assert_held(&ptdev->mmu->as.slots_lock); 677 678 if (drm_WARN_ON(&ptdev->base, vm->as.id < 0)) 679 return; 680 681 ptdev->mmu->as.slots[vm->as.id].vm = NULL; 682 clear_bit(vm->as.id, &ptdev->mmu->as.alloc_mask); 683 refcount_set(&vm->as.active_cnt, 0); 684 list_del_init(&vm->as.lru_node); 685 vm->as.id = -1; 686 } 687 688 /** 689 * panthor_vm_active() - Flag a VM as active 690 * @vm: VM to flag as active. 691 * 692 * Assigns an address space to a VM so it can be used by the GPU/MCU. 693 * 694 * Return: 0 on success, a negative error code otherwise. 695 */ 696 int panthor_vm_active(struct panthor_vm *vm) 697 { 698 struct panthor_device *ptdev = vm->ptdev; 699 u32 va_bits = GPU_MMU_FEATURES_VA_BITS(ptdev->gpu_info.mmu_features); 700 struct io_pgtable_cfg *cfg = &io_pgtable_ops_to_pgtable(vm->pgtbl_ops)->cfg; 701 int ret = 0, as, cookie; 702 u64 transtab, transcfg; 703 704 if (!drm_dev_enter(&ptdev->base, &cookie)) 705 return -ENODEV; 706 707 if (refcount_inc_not_zero(&vm->as.active_cnt)) 708 goto out_dev_exit; 709 710 mutex_lock(&ptdev->mmu->as.slots_lock); 711 712 if (refcount_inc_not_zero(&vm->as.active_cnt)) 713 goto out_unlock; 714 715 as = vm->as.id; 716 if (as >= 0) { 717 /* Unhandled pagefault on this AS, the MMU was disabled. We need to 718 * re-enable the MMU after clearing+unmasking the AS interrupts. 719 */ 720 if (ptdev->mmu->as.faulty_mask & panthor_mmu_as_fault_mask(ptdev, as)) 721 goto out_enable_as; 722 723 goto out_make_active; 724 } 725 726 /* Check for a free AS */ 727 if (vm->for_mcu) { 728 drm_WARN_ON(&ptdev->base, ptdev->mmu->as.alloc_mask & BIT(0)); 729 as = 0; 730 } else { 731 as = ffz(ptdev->mmu->as.alloc_mask | BIT(0)); 732 } 733 734 if (!(BIT(as) & ptdev->gpu_info.as_present)) { 735 struct panthor_vm *lru_vm; 736 737 lru_vm = list_first_entry_or_null(&ptdev->mmu->as.lru_list, 738 struct panthor_vm, 739 as.lru_node); 740 if (drm_WARN_ON(&ptdev->base, !lru_vm)) { 741 ret = -EBUSY; 742 goto out_unlock; 743 } 744 745 drm_WARN_ON(&ptdev->base, refcount_read(&lru_vm->as.active_cnt)); 746 as = lru_vm->as.id; 747 panthor_vm_release_as_locked(lru_vm); 748 } 749 750 /* Assign the free or reclaimed AS to the FD */ 751 vm->as.id = as; 752 set_bit(as, &ptdev->mmu->as.alloc_mask); 753 ptdev->mmu->as.slots[as].vm = vm; 754 755 out_enable_as: 756 transtab = cfg->arm_lpae_s1_cfg.ttbr; 757 transcfg = AS_TRANSCFG_PTW_MEMATTR_WB | 758 AS_TRANSCFG_PTW_RA | 759 AS_TRANSCFG_ADRMODE_AARCH64_4K | 760 AS_TRANSCFG_INA_BITS(55 - va_bits); 761 if (ptdev->coherent) 762 transcfg |= AS_TRANSCFG_PTW_SH_OS; 763 764 /* If the VM is re-activated, we clear the fault. */ 765 vm->unhandled_fault = false; 766 767 /* Unhandled pagefault on this AS, clear the fault and re-enable interrupts 768 * before enabling the AS. 769 */ 770 if (ptdev->mmu->as.faulty_mask & panthor_mmu_as_fault_mask(ptdev, as)) { 771 gpu_write(ptdev, MMU_INT_CLEAR, panthor_mmu_as_fault_mask(ptdev, as)); 772 ptdev->mmu->as.faulty_mask &= ~panthor_mmu_as_fault_mask(ptdev, as); 773 ptdev->mmu->irq.mask |= panthor_mmu_as_fault_mask(ptdev, as); 774 gpu_write(ptdev, MMU_INT_MASK, ~ptdev->mmu->as.faulty_mask); 775 } 776 777 ret = panthor_mmu_as_enable(vm->ptdev, vm->as.id, transtab, transcfg, vm->memattr); 778 779 out_make_active: 780 if (!ret) { 781 refcount_set(&vm->as.active_cnt, 1); 782 list_del_init(&vm->as.lru_node); 783 } 784 785 out_unlock: 786 mutex_unlock(&ptdev->mmu->as.slots_lock); 787 788 out_dev_exit: 789 drm_dev_exit(cookie); 790 return ret; 791 } 792 793 /** 794 * panthor_vm_idle() - Flag a VM idle 795 * @vm: VM to flag as idle. 796 * 797 * When we know the GPU is done with the VM (no more jobs to process), 798 * we can relinquish the AS slot attached to this VM, if any. 799 * 800 * We don't release the slot immediately, but instead place the VM in 801 * the LRU list, so it can be evicted if another VM needs an AS slot. 802 * This way, VMs keep attached to the AS they were given until we run 803 * out of free slot, limiting the number of MMU operations (TLB flush 804 * and other AS updates). 805 */ 806 void panthor_vm_idle(struct panthor_vm *vm) 807 { 808 struct panthor_device *ptdev = vm->ptdev; 809 810 if (!refcount_dec_and_mutex_lock(&vm->as.active_cnt, &ptdev->mmu->as.slots_lock)) 811 return; 812 813 if (!drm_WARN_ON(&ptdev->base, vm->as.id == -1 || !list_empty(&vm->as.lru_node))) 814 list_add_tail(&vm->as.lru_node, &ptdev->mmu->as.lru_list); 815 816 refcount_set(&vm->as.active_cnt, 0); 817 mutex_unlock(&ptdev->mmu->as.slots_lock); 818 } 819 820 u32 panthor_vm_page_size(struct panthor_vm *vm) 821 { 822 const struct io_pgtable *pgt = io_pgtable_ops_to_pgtable(vm->pgtbl_ops); 823 u32 pg_shift = ffs(pgt->cfg.pgsize_bitmap) - 1; 824 825 return 1u << pg_shift; 826 } 827 828 static void panthor_vm_stop(struct panthor_vm *vm) 829 { 830 drm_sched_stop(&vm->sched, NULL); 831 } 832 833 static void panthor_vm_start(struct panthor_vm *vm) 834 { 835 drm_sched_start(&vm->sched, 0); 836 } 837 838 /** 839 * panthor_vm_as() - Get the AS slot attached to a VM 840 * @vm: VM to get the AS slot of. 841 * 842 * Return: -1 if the VM is not assigned an AS slot yet, >= 0 otherwise. 843 */ 844 int panthor_vm_as(struct panthor_vm *vm) 845 { 846 return vm->as.id; 847 } 848 849 static size_t get_pgsize(u64 addr, size_t size, size_t *count) 850 { 851 /* 852 * io-pgtable only operates on multiple pages within a single table 853 * entry, so we need to split at boundaries of the table size, i.e. 854 * the next block size up. The distance from address A to the next 855 * boundary of block size B is logically B - A % B, but in unsigned 856 * two's complement where B is a power of two we get the equivalence 857 * B - A % B == (B - A) % B == (n * B - A) % B, and choose n = 0 :) 858 */ 859 size_t blk_offset = -addr % SZ_2M; 860 861 if (blk_offset || size < SZ_2M) { 862 *count = min_not_zero(blk_offset, size) / SZ_4K; 863 return SZ_4K; 864 } 865 blk_offset = -addr % SZ_1G ?: SZ_1G; 866 *count = min(blk_offset, size) / SZ_2M; 867 return SZ_2M; 868 } 869 870 static int panthor_vm_flush_range(struct panthor_vm *vm, u64 iova, u64 size) 871 { 872 struct panthor_device *ptdev = vm->ptdev; 873 int ret = 0, cookie; 874 875 if (vm->as.id < 0) 876 return 0; 877 878 /* If the device is unplugged, we just silently skip the flush. */ 879 if (!drm_dev_enter(&ptdev->base, &cookie)) 880 return 0; 881 882 ret = mmu_hw_do_operation(vm, iova, size, AS_COMMAND_FLUSH_PT); 883 884 drm_dev_exit(cookie); 885 return ret; 886 } 887 888 static int panthor_vm_unmap_pages(struct panthor_vm *vm, u64 iova, u64 size) 889 { 890 struct panthor_device *ptdev = vm->ptdev; 891 struct io_pgtable_ops *ops = vm->pgtbl_ops; 892 u64 offset = 0; 893 894 drm_dbg(&ptdev->base, "unmap: as=%d, iova=%llx, len=%llx", vm->as.id, iova, size); 895 896 while (offset < size) { 897 size_t unmapped_sz = 0, pgcount; 898 size_t pgsize = get_pgsize(iova + offset, size - offset, &pgcount); 899 900 unmapped_sz = ops->unmap_pages(ops, iova + offset, pgsize, pgcount, NULL); 901 902 if (drm_WARN_ON(&ptdev->base, unmapped_sz != pgsize * pgcount)) { 903 drm_err(&ptdev->base, "failed to unmap range %llx-%llx (requested range %llx-%llx)\n", 904 iova + offset + unmapped_sz, 905 iova + offset + pgsize * pgcount, 906 iova, iova + size); 907 panthor_vm_flush_range(vm, iova, offset + unmapped_sz); 908 return -EINVAL; 909 } 910 offset += unmapped_sz; 911 } 912 913 return panthor_vm_flush_range(vm, iova, size); 914 } 915 916 static int 917 panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, 918 struct sg_table *sgt, u64 offset, u64 size) 919 { 920 struct panthor_device *ptdev = vm->ptdev; 921 unsigned int count; 922 struct scatterlist *sgl; 923 struct io_pgtable_ops *ops = vm->pgtbl_ops; 924 u64 start_iova = iova; 925 int ret; 926 927 if (!size) 928 return 0; 929 930 for_each_sgtable_dma_sg(sgt, sgl, count) { 931 dma_addr_t paddr = sg_dma_address(sgl); 932 size_t len = sg_dma_len(sgl); 933 934 if (len <= offset) { 935 offset -= len; 936 continue; 937 } 938 939 paddr += offset; 940 len -= offset; 941 len = min_t(size_t, len, size); 942 size -= len; 943 944 drm_dbg(&ptdev->base, "map: as=%d, iova=%llx, paddr=%pad, len=%zx", 945 vm->as.id, iova, &paddr, len); 946 947 while (len) { 948 size_t pgcount, mapped = 0; 949 size_t pgsize = get_pgsize(iova | paddr, len, &pgcount); 950 951 ret = ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, 952 GFP_KERNEL, &mapped); 953 iova += mapped; 954 paddr += mapped; 955 len -= mapped; 956 957 if (drm_WARN_ON(&ptdev->base, !ret && !mapped)) 958 ret = -ENOMEM; 959 960 if (ret) { 961 /* If something failed, unmap what we've already mapped before 962 * returning. The unmap call is not supposed to fail. 963 */ 964 drm_WARN_ON(&ptdev->base, 965 panthor_vm_unmap_pages(vm, start_iova, 966 iova - start_iova)); 967 return ret; 968 } 969 } 970 971 if (!size) 972 break; 973 974 offset = 0; 975 } 976 977 return panthor_vm_flush_range(vm, start_iova, iova - start_iova); 978 } 979 980 static int flags_to_prot(u32 flags) 981 { 982 int prot = 0; 983 984 if (flags & DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC) 985 prot |= IOMMU_NOEXEC; 986 987 if (!(flags & DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED)) 988 prot |= IOMMU_CACHE; 989 990 if (flags & DRM_PANTHOR_VM_BIND_OP_MAP_READONLY) 991 prot |= IOMMU_READ; 992 else 993 prot |= IOMMU_READ | IOMMU_WRITE; 994 995 return prot; 996 } 997 998 /** 999 * panthor_vm_alloc_va() - Allocate a region in the auto-va space 1000 * @vm: VM to allocate a region on. 1001 * @va: start of the VA range. Can be PANTHOR_VM_KERNEL_AUTO_VA if the user 1002 * wants the VA to be automatically allocated from the auto-VA range. 1003 * @size: size of the VA range. 1004 * @va_node: drm_mm_node to initialize. Must be zero-initialized. 1005 * 1006 * Some GPU objects, like heap chunks, are fully managed by the kernel and 1007 * need to be mapped to the userspace VM, in the region reserved for kernel 1008 * objects. 1009 * 1010 * This function takes care of allocating a region in the kernel auto-VA space. 1011 * 1012 * Return: 0 on success, an error code otherwise. 1013 */ 1014 int 1015 panthor_vm_alloc_va(struct panthor_vm *vm, u64 va, u64 size, 1016 struct drm_mm_node *va_node) 1017 { 1018 ssize_t vm_pgsz = panthor_vm_page_size(vm); 1019 int ret; 1020 1021 if (!size || !IS_ALIGNED(size, vm_pgsz)) 1022 return -EINVAL; 1023 1024 if (va != PANTHOR_VM_KERNEL_AUTO_VA && !IS_ALIGNED(va, vm_pgsz)) 1025 return -EINVAL; 1026 1027 mutex_lock(&vm->mm_lock); 1028 if (va != PANTHOR_VM_KERNEL_AUTO_VA) { 1029 va_node->start = va; 1030 va_node->size = size; 1031 ret = drm_mm_reserve_node(&vm->mm, va_node); 1032 } else { 1033 ret = drm_mm_insert_node_in_range(&vm->mm, va_node, size, 1034 size >= SZ_2M ? SZ_2M : SZ_4K, 1035 0, vm->kernel_auto_va.start, 1036 vm->kernel_auto_va.end, 1037 DRM_MM_INSERT_BEST); 1038 } 1039 mutex_unlock(&vm->mm_lock); 1040 1041 return ret; 1042 } 1043 1044 /** 1045 * panthor_vm_free_va() - Free a region allocated with panthor_vm_alloc_va() 1046 * @vm: VM to free the region on. 1047 * @va_node: Memory node representing the region to free. 1048 */ 1049 void panthor_vm_free_va(struct panthor_vm *vm, struct drm_mm_node *va_node) 1050 { 1051 mutex_lock(&vm->mm_lock); 1052 drm_mm_remove_node(va_node); 1053 mutex_unlock(&vm->mm_lock); 1054 } 1055 1056 static void panthor_vm_bo_put(struct drm_gpuvm_bo *vm_bo) 1057 { 1058 struct panthor_gem_object *bo = to_panthor_bo(vm_bo->obj); 1059 struct drm_gpuvm *vm = vm_bo->vm; 1060 bool unpin; 1061 1062 /* We must retain the GEM before calling drm_gpuvm_bo_put(), 1063 * otherwise the mutex might be destroyed while we hold it. 1064 * Same goes for the VM, since we take the VM resv lock. 1065 */ 1066 drm_gem_object_get(&bo->base.base); 1067 drm_gpuvm_get(vm); 1068 1069 /* We take the resv lock to protect against concurrent accesses to the 1070 * gpuvm evicted/extobj lists that are modified in 1071 * drm_gpuvm_bo_destroy(), which is called if drm_gpuvm_bo_put() 1072 * releases sthe last vm_bo reference. 1073 * We take the BO GPUVA list lock to protect the vm_bo removal from the 1074 * GEM vm_bo list. 1075 */ 1076 dma_resv_lock(drm_gpuvm_resv(vm), NULL); 1077 mutex_lock(&bo->gpuva_list_lock); 1078 unpin = drm_gpuvm_bo_put(vm_bo); 1079 mutex_unlock(&bo->gpuva_list_lock); 1080 dma_resv_unlock(drm_gpuvm_resv(vm)); 1081 1082 /* If the vm_bo object was destroyed, release the pin reference that 1083 * was hold by this object. 1084 */ 1085 if (unpin && !drm_gem_is_imported(&bo->base.base)) 1086 drm_gem_shmem_unpin(&bo->base); 1087 1088 drm_gpuvm_put(vm); 1089 drm_gem_object_put(&bo->base.base); 1090 } 1091 1092 static void panthor_vm_cleanup_op_ctx(struct panthor_vm_op_ctx *op_ctx, 1093 struct panthor_vm *vm) 1094 { 1095 struct panthor_vma *vma, *tmp_vma; 1096 1097 u32 remaining_pt_count = op_ctx->rsvd_page_tables.count - 1098 op_ctx->rsvd_page_tables.ptr; 1099 1100 if (remaining_pt_count) { 1101 kmem_cache_free_bulk(pt_cache, remaining_pt_count, 1102 op_ctx->rsvd_page_tables.pages + 1103 op_ctx->rsvd_page_tables.ptr); 1104 } 1105 1106 kfree(op_ctx->rsvd_page_tables.pages); 1107 1108 if (op_ctx->map.vm_bo) 1109 panthor_vm_bo_put(op_ctx->map.vm_bo); 1110 1111 for (u32 i = 0; i < ARRAY_SIZE(op_ctx->preallocated_vmas); i++) 1112 kfree(op_ctx->preallocated_vmas[i]); 1113 1114 list_for_each_entry_safe(vma, tmp_vma, &op_ctx->returned_vmas, node) { 1115 list_del(&vma->node); 1116 panthor_vm_bo_put(vma->base.vm_bo); 1117 kfree(vma); 1118 } 1119 } 1120 1121 static struct panthor_vma * 1122 panthor_vm_op_ctx_get_vma(struct panthor_vm_op_ctx *op_ctx) 1123 { 1124 for (u32 i = 0; i < ARRAY_SIZE(op_ctx->preallocated_vmas); i++) { 1125 struct panthor_vma *vma = op_ctx->preallocated_vmas[i]; 1126 1127 if (vma) { 1128 op_ctx->preallocated_vmas[i] = NULL; 1129 return vma; 1130 } 1131 } 1132 1133 return NULL; 1134 } 1135 1136 static int 1137 panthor_vm_op_ctx_prealloc_vmas(struct panthor_vm_op_ctx *op_ctx) 1138 { 1139 u32 vma_count; 1140 1141 switch (op_ctx->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) { 1142 case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP: 1143 /* One VMA for the new mapping, and two more VMAs for the remap case 1144 * which might contain both a prev and next VA. 1145 */ 1146 vma_count = 3; 1147 break; 1148 1149 case DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP: 1150 /* Partial unmaps might trigger a remap with either a prev or a next VA, 1151 * but not both. 1152 */ 1153 vma_count = 1; 1154 break; 1155 1156 default: 1157 return 0; 1158 } 1159 1160 for (u32 i = 0; i < vma_count; i++) { 1161 struct panthor_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL); 1162 1163 if (!vma) 1164 return -ENOMEM; 1165 1166 op_ctx->preallocated_vmas[i] = vma; 1167 } 1168 1169 return 0; 1170 } 1171 1172 #define PANTHOR_VM_BIND_OP_MAP_FLAGS \ 1173 (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ 1174 DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ 1175 DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ 1176 DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) 1177 1178 static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, 1179 struct panthor_vm *vm, 1180 struct panthor_gem_object *bo, 1181 u64 offset, 1182 u64 size, u64 va, 1183 u32 flags) 1184 { 1185 struct drm_gpuvm_bo *preallocated_vm_bo; 1186 struct sg_table *sgt = NULL; 1187 u64 pt_count; 1188 int ret; 1189 1190 if (!bo) 1191 return -EINVAL; 1192 1193 if ((flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS) || 1194 (flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) != DRM_PANTHOR_VM_BIND_OP_TYPE_MAP) 1195 return -EINVAL; 1196 1197 /* Make sure the VA and size are aligned and in-bounds. */ 1198 if (size > bo->base.base.size || offset > bo->base.base.size - size) 1199 return -EINVAL; 1200 1201 /* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ 1202 if (bo->exclusive_vm_root_gem && 1203 bo->exclusive_vm_root_gem != panthor_vm_root_gem(vm)) 1204 return -EINVAL; 1205 1206 memset(op_ctx, 0, sizeof(*op_ctx)); 1207 INIT_LIST_HEAD(&op_ctx->returned_vmas); 1208 op_ctx->flags = flags; 1209 op_ctx->va.range = size; 1210 op_ctx->va.addr = va; 1211 1212 ret = panthor_vm_op_ctx_prealloc_vmas(op_ctx); 1213 if (ret) 1214 goto err_cleanup; 1215 1216 if (!drm_gem_is_imported(&bo->base.base)) { 1217 /* Pre-reserve the BO pages, so the map operation doesn't have to 1218 * allocate. 1219 */ 1220 ret = drm_gem_shmem_pin(&bo->base); 1221 if (ret) 1222 goto err_cleanup; 1223 } 1224 1225 sgt = drm_gem_shmem_get_pages_sgt(&bo->base); 1226 if (IS_ERR(sgt)) { 1227 if (!drm_gem_is_imported(&bo->base.base)) 1228 drm_gem_shmem_unpin(&bo->base); 1229 1230 ret = PTR_ERR(sgt); 1231 goto err_cleanup; 1232 } 1233 1234 op_ctx->map.sgt = sgt; 1235 1236 preallocated_vm_bo = drm_gpuvm_bo_create(&vm->base, &bo->base.base); 1237 if (!preallocated_vm_bo) { 1238 if (!drm_gem_is_imported(&bo->base.base)) 1239 drm_gem_shmem_unpin(&bo->base); 1240 1241 ret = -ENOMEM; 1242 goto err_cleanup; 1243 } 1244 1245 /* drm_gpuvm_bo_obtain_prealloc() will call drm_gpuvm_bo_put() on our 1246 * pre-allocated BO if the <BO,VM> association exists. Given we 1247 * only have one ref on preallocated_vm_bo, drm_gpuvm_bo_destroy() will 1248 * be called immediately, and we have to hold the VM resv lock when 1249 * calling this function. 1250 */ 1251 dma_resv_lock(panthor_vm_resv(vm), NULL); 1252 mutex_lock(&bo->gpuva_list_lock); 1253 op_ctx->map.vm_bo = drm_gpuvm_bo_obtain_prealloc(preallocated_vm_bo); 1254 mutex_unlock(&bo->gpuva_list_lock); 1255 dma_resv_unlock(panthor_vm_resv(vm)); 1256 1257 /* If the a vm_bo for this <VM,BO> combination exists, it already 1258 * retains a pin ref, and we can release the one we took earlier. 1259 * 1260 * If our pre-allocated vm_bo is picked, it now retains the pin ref, 1261 * which will be released in panthor_vm_bo_put(). 1262 */ 1263 if (preallocated_vm_bo != op_ctx->map.vm_bo && 1264 !drm_gem_is_imported(&bo->base.base)) 1265 drm_gem_shmem_unpin(&bo->base); 1266 1267 op_ctx->map.bo_offset = offset; 1268 1269 /* L1, L2 and L3 page tables. 1270 * We could optimize L3 allocation by iterating over the sgt and merging 1271 * 2M contiguous blocks, but it's simpler to over-provision and return 1272 * the pages if they're not used. 1273 */ 1274 pt_count = ((ALIGN(va + size, 1ull << 39) - ALIGN_DOWN(va, 1ull << 39)) >> 39) + 1275 ((ALIGN(va + size, 1ull << 30) - ALIGN_DOWN(va, 1ull << 30)) >> 30) + 1276 ((ALIGN(va + size, 1ull << 21) - ALIGN_DOWN(va, 1ull << 21)) >> 21); 1277 1278 op_ctx->rsvd_page_tables.pages = kcalloc(pt_count, 1279 sizeof(*op_ctx->rsvd_page_tables.pages), 1280 GFP_KERNEL); 1281 if (!op_ctx->rsvd_page_tables.pages) { 1282 ret = -ENOMEM; 1283 goto err_cleanup; 1284 } 1285 1286 ret = kmem_cache_alloc_bulk(pt_cache, GFP_KERNEL, pt_count, 1287 op_ctx->rsvd_page_tables.pages); 1288 op_ctx->rsvd_page_tables.count = ret; 1289 if (ret != pt_count) { 1290 ret = -ENOMEM; 1291 goto err_cleanup; 1292 } 1293 1294 /* Insert BO into the extobj list last, when we know nothing can fail. */ 1295 dma_resv_lock(panthor_vm_resv(vm), NULL); 1296 drm_gpuvm_bo_extobj_add(op_ctx->map.vm_bo); 1297 dma_resv_unlock(panthor_vm_resv(vm)); 1298 1299 return 0; 1300 1301 err_cleanup: 1302 panthor_vm_cleanup_op_ctx(op_ctx, vm); 1303 return ret; 1304 } 1305 1306 static int panthor_vm_prepare_unmap_op_ctx(struct panthor_vm_op_ctx *op_ctx, 1307 struct panthor_vm *vm, 1308 u64 va, u64 size) 1309 { 1310 u32 pt_count = 0; 1311 int ret; 1312 1313 memset(op_ctx, 0, sizeof(*op_ctx)); 1314 INIT_LIST_HEAD(&op_ctx->returned_vmas); 1315 op_ctx->va.range = size; 1316 op_ctx->va.addr = va; 1317 op_ctx->flags = DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP; 1318 1319 /* Pre-allocate L3 page tables to account for the split-2M-block 1320 * situation on unmap. 1321 */ 1322 if (va != ALIGN(va, SZ_2M)) 1323 pt_count++; 1324 1325 if (va + size != ALIGN(va + size, SZ_2M) && 1326 ALIGN(va + size, SZ_2M) != ALIGN(va, SZ_2M)) 1327 pt_count++; 1328 1329 ret = panthor_vm_op_ctx_prealloc_vmas(op_ctx); 1330 if (ret) 1331 goto err_cleanup; 1332 1333 if (pt_count) { 1334 op_ctx->rsvd_page_tables.pages = kcalloc(pt_count, 1335 sizeof(*op_ctx->rsvd_page_tables.pages), 1336 GFP_KERNEL); 1337 if (!op_ctx->rsvd_page_tables.pages) { 1338 ret = -ENOMEM; 1339 goto err_cleanup; 1340 } 1341 1342 ret = kmem_cache_alloc_bulk(pt_cache, GFP_KERNEL, pt_count, 1343 op_ctx->rsvd_page_tables.pages); 1344 if (ret != pt_count) { 1345 ret = -ENOMEM; 1346 goto err_cleanup; 1347 } 1348 op_ctx->rsvd_page_tables.count = pt_count; 1349 } 1350 1351 return 0; 1352 1353 err_cleanup: 1354 panthor_vm_cleanup_op_ctx(op_ctx, vm); 1355 return ret; 1356 } 1357 1358 static void panthor_vm_prepare_sync_only_op_ctx(struct panthor_vm_op_ctx *op_ctx, 1359 struct panthor_vm *vm) 1360 { 1361 memset(op_ctx, 0, sizeof(*op_ctx)); 1362 INIT_LIST_HEAD(&op_ctx->returned_vmas); 1363 op_ctx->flags = DRM_PANTHOR_VM_BIND_OP_TYPE_SYNC_ONLY; 1364 } 1365 1366 /** 1367 * panthor_vm_get_bo_for_va() - Get the GEM object mapped at a virtual address 1368 * @vm: VM to look into. 1369 * @va: Virtual address to search for. 1370 * @bo_offset: Offset of the GEM object mapped at this virtual address. 1371 * Only valid on success. 1372 * 1373 * The object returned by this function might no longer be mapped when the 1374 * function returns. It's the caller responsibility to ensure there's no 1375 * concurrent map/unmap operations making the returned value invalid, or 1376 * make sure it doesn't matter if the object is no longer mapped. 1377 * 1378 * Return: A valid pointer on success, an ERR_PTR() otherwise. 1379 */ 1380 struct panthor_gem_object * 1381 panthor_vm_get_bo_for_va(struct panthor_vm *vm, u64 va, u64 *bo_offset) 1382 { 1383 struct panthor_gem_object *bo = ERR_PTR(-ENOENT); 1384 struct drm_gpuva *gpuva; 1385 struct panthor_vma *vma; 1386 1387 /* Take the VM lock to prevent concurrent map/unmap operations. */ 1388 mutex_lock(&vm->op_lock); 1389 gpuva = drm_gpuva_find_first(&vm->base, va, 1); 1390 vma = gpuva ? container_of(gpuva, struct panthor_vma, base) : NULL; 1391 if (vma && vma->base.gem.obj) { 1392 drm_gem_object_get(vma->base.gem.obj); 1393 bo = to_panthor_bo(vma->base.gem.obj); 1394 *bo_offset = vma->base.gem.offset + (va - vma->base.va.addr); 1395 } 1396 mutex_unlock(&vm->op_lock); 1397 1398 return bo; 1399 } 1400 1401 #define PANTHOR_VM_MIN_KERNEL_VA_SIZE SZ_256M 1402 1403 static u64 1404 panthor_vm_create_get_user_va_range(const struct drm_panthor_vm_create *args, 1405 u64 full_va_range) 1406 { 1407 u64 user_va_range; 1408 1409 /* Make sure we have a minimum amount of VA space for kernel objects. */ 1410 if (full_va_range < PANTHOR_VM_MIN_KERNEL_VA_SIZE) 1411 return 0; 1412 1413 if (args->user_va_range) { 1414 /* Use the user provided value if != 0. */ 1415 user_va_range = args->user_va_range; 1416 } else if (TASK_SIZE_OF(current) < full_va_range) { 1417 /* If the task VM size is smaller than the GPU VA range, pick this 1418 * as our default user VA range, so userspace can CPU/GPU map buffers 1419 * at the same address. 1420 */ 1421 user_va_range = TASK_SIZE_OF(current); 1422 } else { 1423 /* If the GPU VA range is smaller than the task VM size, we 1424 * just have to live with the fact we won't be able to map 1425 * all buffers at the same GPU/CPU address. 1426 * 1427 * If the GPU VA range is bigger than 4G (more than 32-bit of 1428 * VA), we split the range in two, and assign half of it to 1429 * the user and the other half to the kernel, if it's not, we 1430 * keep the kernel VA space as small as possible. 1431 */ 1432 user_va_range = full_va_range > SZ_4G ? 1433 full_va_range / 2 : 1434 full_va_range - PANTHOR_VM_MIN_KERNEL_VA_SIZE; 1435 } 1436 1437 if (full_va_range - PANTHOR_VM_MIN_KERNEL_VA_SIZE < user_va_range) 1438 user_va_range = full_va_range - PANTHOR_VM_MIN_KERNEL_VA_SIZE; 1439 1440 return user_va_range; 1441 } 1442 1443 #define PANTHOR_VM_CREATE_FLAGS 0 1444 1445 static int 1446 panthor_vm_create_check_args(const struct panthor_device *ptdev, 1447 const struct drm_panthor_vm_create *args, 1448 u64 *kernel_va_start, u64 *kernel_va_range) 1449 { 1450 u32 va_bits = GPU_MMU_FEATURES_VA_BITS(ptdev->gpu_info.mmu_features); 1451 u64 full_va_range = 1ull << va_bits; 1452 u64 user_va_range; 1453 1454 if (args->flags & ~PANTHOR_VM_CREATE_FLAGS) 1455 return -EINVAL; 1456 1457 user_va_range = panthor_vm_create_get_user_va_range(args, full_va_range); 1458 if (!user_va_range || (args->user_va_range && args->user_va_range > user_va_range)) 1459 return -EINVAL; 1460 1461 /* Pick a kernel VA range that's a power of two, to have a clear split. */ 1462 *kernel_va_range = rounddown_pow_of_two(full_va_range - user_va_range); 1463 *kernel_va_start = full_va_range - *kernel_va_range; 1464 return 0; 1465 } 1466 1467 /* 1468 * Only 32 VMs per open file. If that becomes a limiting factor, we can 1469 * increase this number. 1470 */ 1471 #define PANTHOR_MAX_VMS_PER_FILE 32 1472 1473 /** 1474 * panthor_vm_pool_create_vm() - Create a VM 1475 * @ptdev: The panthor device 1476 * @pool: The VM to create this VM on. 1477 * @args: VM creation args. 1478 * 1479 * Return: a positive VM ID on success, a negative error code otherwise. 1480 */ 1481 int panthor_vm_pool_create_vm(struct panthor_device *ptdev, 1482 struct panthor_vm_pool *pool, 1483 struct drm_panthor_vm_create *args) 1484 { 1485 u64 kernel_va_start, kernel_va_range; 1486 struct panthor_vm *vm; 1487 int ret; 1488 u32 id; 1489 1490 ret = panthor_vm_create_check_args(ptdev, args, &kernel_va_start, &kernel_va_range); 1491 if (ret) 1492 return ret; 1493 1494 vm = panthor_vm_create(ptdev, false, kernel_va_start, kernel_va_range, 1495 kernel_va_start, kernel_va_range); 1496 if (IS_ERR(vm)) 1497 return PTR_ERR(vm); 1498 1499 ret = xa_alloc(&pool->xa, &id, vm, 1500 XA_LIMIT(1, PANTHOR_MAX_VMS_PER_FILE), GFP_KERNEL); 1501 1502 if (ret) { 1503 panthor_vm_put(vm); 1504 return ret; 1505 } 1506 1507 args->user_va_range = kernel_va_start; 1508 return id; 1509 } 1510 1511 static void panthor_vm_destroy(struct panthor_vm *vm) 1512 { 1513 if (!vm) 1514 return; 1515 1516 vm->destroyed = true; 1517 1518 mutex_lock(&vm->heaps.lock); 1519 panthor_heap_pool_destroy(vm->heaps.pool); 1520 vm->heaps.pool = NULL; 1521 mutex_unlock(&vm->heaps.lock); 1522 1523 drm_WARN_ON(&vm->ptdev->base, 1524 panthor_vm_unmap_range(vm, vm->base.mm_start, vm->base.mm_range)); 1525 panthor_vm_put(vm); 1526 } 1527 1528 /** 1529 * panthor_vm_pool_destroy_vm() - Destroy a VM. 1530 * @pool: VM pool. 1531 * @handle: VM handle. 1532 * 1533 * This function doesn't free the VM object or its resources, it just kills 1534 * all mappings, and makes sure nothing can be mapped after that point. 1535 * 1536 * If there was any active jobs at the time this function is called, these 1537 * jobs should experience page faults and be killed as a result. 1538 * 1539 * The VM resources are freed when the last reference on the VM object is 1540 * dropped. 1541 * 1542 * Return: %0 for success, negative errno value for failure 1543 */ 1544 int panthor_vm_pool_destroy_vm(struct panthor_vm_pool *pool, u32 handle) 1545 { 1546 struct panthor_vm *vm; 1547 1548 vm = xa_erase(&pool->xa, handle); 1549 1550 panthor_vm_destroy(vm); 1551 1552 return vm ? 0 : -EINVAL; 1553 } 1554 1555 /** 1556 * panthor_vm_pool_get_vm() - Retrieve VM object bound to a VM handle 1557 * @pool: VM pool to check. 1558 * @handle: Handle of the VM to retrieve. 1559 * 1560 * Return: A valid pointer if the VM exists, NULL otherwise. 1561 */ 1562 struct panthor_vm * 1563 panthor_vm_pool_get_vm(struct panthor_vm_pool *pool, u32 handle) 1564 { 1565 struct panthor_vm *vm; 1566 1567 xa_lock(&pool->xa); 1568 vm = panthor_vm_get(xa_load(&pool->xa, handle)); 1569 xa_unlock(&pool->xa); 1570 1571 return vm; 1572 } 1573 1574 /** 1575 * panthor_vm_pool_destroy() - Destroy a VM pool. 1576 * @pfile: File. 1577 * 1578 * Destroy all VMs in the pool, and release the pool resources. 1579 * 1580 * Note that VMs can outlive the pool they were created from if other 1581 * objects hold a reference to there VMs. 1582 */ 1583 void panthor_vm_pool_destroy(struct panthor_file *pfile) 1584 { 1585 struct panthor_vm *vm; 1586 unsigned long i; 1587 1588 if (!pfile->vms) 1589 return; 1590 1591 xa_for_each(&pfile->vms->xa, i, vm) 1592 panthor_vm_destroy(vm); 1593 1594 xa_destroy(&pfile->vms->xa); 1595 kfree(pfile->vms); 1596 } 1597 1598 /** 1599 * panthor_vm_pool_create() - Create a VM pool 1600 * @pfile: File. 1601 * 1602 * Return: 0 on success, a negative error code otherwise. 1603 */ 1604 int panthor_vm_pool_create(struct panthor_file *pfile) 1605 { 1606 pfile->vms = kzalloc(sizeof(*pfile->vms), GFP_KERNEL); 1607 if (!pfile->vms) 1608 return -ENOMEM; 1609 1610 xa_init_flags(&pfile->vms->xa, XA_FLAGS_ALLOC1); 1611 return 0; 1612 } 1613 1614 /* dummy TLB ops, the real TLB flush happens in panthor_vm_flush_range() */ 1615 static void mmu_tlb_flush_all(void *cookie) 1616 { 1617 } 1618 1619 static void mmu_tlb_flush_walk(unsigned long iova, size_t size, size_t granule, void *cookie) 1620 { 1621 } 1622 1623 static const struct iommu_flush_ops mmu_tlb_ops = { 1624 .tlb_flush_all = mmu_tlb_flush_all, 1625 .tlb_flush_walk = mmu_tlb_flush_walk, 1626 }; 1627 1628 static const char *access_type_name(struct panthor_device *ptdev, 1629 u32 fault_status) 1630 { 1631 switch (fault_status & AS_FAULTSTATUS_ACCESS_TYPE_MASK) { 1632 case AS_FAULTSTATUS_ACCESS_TYPE_ATOMIC: 1633 return "ATOMIC"; 1634 case AS_FAULTSTATUS_ACCESS_TYPE_READ: 1635 return "READ"; 1636 case AS_FAULTSTATUS_ACCESS_TYPE_WRITE: 1637 return "WRITE"; 1638 case AS_FAULTSTATUS_ACCESS_TYPE_EX: 1639 return "EXECUTE"; 1640 default: 1641 drm_WARN_ON(&ptdev->base, 1); 1642 return NULL; 1643 } 1644 } 1645 1646 static void panthor_mmu_irq_handler(struct panthor_device *ptdev, u32 status) 1647 { 1648 bool has_unhandled_faults = false; 1649 1650 status = panthor_mmu_fault_mask(ptdev, status); 1651 while (status) { 1652 u32 as = ffs(status | (status >> 16)) - 1; 1653 u32 mask = panthor_mmu_as_fault_mask(ptdev, as); 1654 u32 new_int_mask; 1655 u64 addr; 1656 u32 fault_status; 1657 u32 exception_type; 1658 u32 access_type; 1659 u32 source_id; 1660 1661 fault_status = gpu_read(ptdev, AS_FAULTSTATUS(as)); 1662 addr = gpu_read64(ptdev, AS_FAULTADDRESS(as)); 1663 1664 /* decode the fault status */ 1665 exception_type = fault_status & 0xFF; 1666 access_type = (fault_status >> 8) & 0x3; 1667 source_id = (fault_status >> 16); 1668 1669 mutex_lock(&ptdev->mmu->as.slots_lock); 1670 1671 ptdev->mmu->as.faulty_mask |= mask; 1672 new_int_mask = 1673 panthor_mmu_fault_mask(ptdev, ~ptdev->mmu->as.faulty_mask); 1674 1675 /* terminal fault, print info about the fault */ 1676 drm_err(&ptdev->base, 1677 "Unhandled Page fault in AS%d at VA 0x%016llX\n" 1678 "raw fault status: 0x%X\n" 1679 "decoded fault status: %s\n" 1680 "exception type 0x%X: %s\n" 1681 "access type 0x%X: %s\n" 1682 "source id 0x%X\n", 1683 as, addr, 1684 fault_status, 1685 (fault_status & (1 << 10) ? "DECODER FAULT" : "SLAVE FAULT"), 1686 exception_type, panthor_exception_name(ptdev, exception_type), 1687 access_type, access_type_name(ptdev, fault_status), 1688 source_id); 1689 1690 /* We don't handle VM faults at the moment, so let's just clear the 1691 * interrupt and let the writer/reader crash. 1692 * Note that COMPLETED irqs are never cleared, but this is fine 1693 * because they are always masked. 1694 */ 1695 gpu_write(ptdev, MMU_INT_CLEAR, mask); 1696 1697 /* Ignore MMU interrupts on this AS until it's been 1698 * re-enabled. 1699 */ 1700 ptdev->mmu->irq.mask = new_int_mask; 1701 1702 if (ptdev->mmu->as.slots[as].vm) 1703 ptdev->mmu->as.slots[as].vm->unhandled_fault = true; 1704 1705 /* Disable the MMU to kill jobs on this AS. */ 1706 panthor_mmu_as_disable(ptdev, as); 1707 mutex_unlock(&ptdev->mmu->as.slots_lock); 1708 1709 status &= ~mask; 1710 has_unhandled_faults = true; 1711 } 1712 1713 if (has_unhandled_faults) 1714 panthor_sched_report_mmu_fault(ptdev); 1715 } 1716 PANTHOR_IRQ_HANDLER(mmu, MMU, panthor_mmu_irq_handler); 1717 1718 /** 1719 * panthor_mmu_suspend() - Suspend the MMU logic 1720 * @ptdev: Device. 1721 * 1722 * All we do here is de-assign the AS slots on all active VMs, so things 1723 * get flushed to the main memory, and no further access to these VMs are 1724 * possible. 1725 * 1726 * We also suspend the MMU IRQ. 1727 */ 1728 void panthor_mmu_suspend(struct panthor_device *ptdev) 1729 { 1730 mutex_lock(&ptdev->mmu->as.slots_lock); 1731 for (u32 i = 0; i < ARRAY_SIZE(ptdev->mmu->as.slots); i++) { 1732 struct panthor_vm *vm = ptdev->mmu->as.slots[i].vm; 1733 1734 if (vm) { 1735 drm_WARN_ON(&ptdev->base, panthor_mmu_as_disable(ptdev, i)); 1736 panthor_vm_release_as_locked(vm); 1737 } 1738 } 1739 mutex_unlock(&ptdev->mmu->as.slots_lock); 1740 1741 panthor_mmu_irq_suspend(&ptdev->mmu->irq); 1742 } 1743 1744 /** 1745 * panthor_mmu_resume() - Resume the MMU logic 1746 * @ptdev: Device. 1747 * 1748 * Resume the IRQ. 1749 * 1750 * We don't re-enable previously active VMs. We assume other parts of the 1751 * driver will call panthor_vm_active() on the VMs they intend to use. 1752 */ 1753 void panthor_mmu_resume(struct panthor_device *ptdev) 1754 { 1755 mutex_lock(&ptdev->mmu->as.slots_lock); 1756 ptdev->mmu->as.alloc_mask = 0; 1757 ptdev->mmu->as.faulty_mask = 0; 1758 mutex_unlock(&ptdev->mmu->as.slots_lock); 1759 1760 panthor_mmu_irq_resume(&ptdev->mmu->irq, panthor_mmu_fault_mask(ptdev, ~0)); 1761 } 1762 1763 /** 1764 * panthor_mmu_pre_reset() - Prepare for a reset 1765 * @ptdev: Device. 1766 * 1767 * Suspend the IRQ, and make sure all VM_BIND queues are stopped, so we 1768 * don't get asked to do a VM operation while the GPU is down. 1769 * 1770 * We don't cleanly shutdown the AS slots here, because the reset might 1771 * come from an AS_ACTIVE_BIT stuck situation. 1772 */ 1773 void panthor_mmu_pre_reset(struct panthor_device *ptdev) 1774 { 1775 struct panthor_vm *vm; 1776 1777 panthor_mmu_irq_suspend(&ptdev->mmu->irq); 1778 1779 mutex_lock(&ptdev->mmu->vm.lock); 1780 ptdev->mmu->vm.reset_in_progress = true; 1781 list_for_each_entry(vm, &ptdev->mmu->vm.list, node) 1782 panthor_vm_stop(vm); 1783 mutex_unlock(&ptdev->mmu->vm.lock); 1784 } 1785 1786 /** 1787 * panthor_mmu_post_reset() - Restore things after a reset 1788 * @ptdev: Device. 1789 * 1790 * Put the MMU logic back in action after a reset. That implies resuming the 1791 * IRQ and re-enabling the VM_BIND queues. 1792 */ 1793 void panthor_mmu_post_reset(struct panthor_device *ptdev) 1794 { 1795 struct panthor_vm *vm; 1796 1797 mutex_lock(&ptdev->mmu->as.slots_lock); 1798 1799 /* Now that the reset is effective, we can assume that none of the 1800 * AS slots are setup, and clear the faulty flags too. 1801 */ 1802 ptdev->mmu->as.alloc_mask = 0; 1803 ptdev->mmu->as.faulty_mask = 0; 1804 1805 for (u32 i = 0; i < ARRAY_SIZE(ptdev->mmu->as.slots); i++) { 1806 struct panthor_vm *vm = ptdev->mmu->as.slots[i].vm; 1807 1808 if (vm) 1809 panthor_vm_release_as_locked(vm); 1810 } 1811 1812 mutex_unlock(&ptdev->mmu->as.slots_lock); 1813 1814 panthor_mmu_irq_resume(&ptdev->mmu->irq, panthor_mmu_fault_mask(ptdev, ~0)); 1815 1816 /* Restart the VM_BIND queues. */ 1817 mutex_lock(&ptdev->mmu->vm.lock); 1818 list_for_each_entry(vm, &ptdev->mmu->vm.list, node) { 1819 panthor_vm_start(vm); 1820 } 1821 ptdev->mmu->vm.reset_in_progress = false; 1822 mutex_unlock(&ptdev->mmu->vm.lock); 1823 } 1824 1825 static void panthor_vm_free(struct drm_gpuvm *gpuvm) 1826 { 1827 struct panthor_vm *vm = container_of(gpuvm, struct panthor_vm, base); 1828 struct panthor_device *ptdev = vm->ptdev; 1829 1830 mutex_lock(&vm->heaps.lock); 1831 if (drm_WARN_ON(&ptdev->base, vm->heaps.pool)) 1832 panthor_heap_pool_destroy(vm->heaps.pool); 1833 mutex_unlock(&vm->heaps.lock); 1834 mutex_destroy(&vm->heaps.lock); 1835 1836 mutex_lock(&ptdev->mmu->vm.lock); 1837 list_del(&vm->node); 1838 /* Restore the scheduler state so we can call drm_sched_entity_destroy() 1839 * and drm_sched_fini(). If get there, that means we have no job left 1840 * and no new jobs can be queued, so we can start the scheduler without 1841 * risking interfering with the reset. 1842 */ 1843 if (ptdev->mmu->vm.reset_in_progress) 1844 panthor_vm_start(vm); 1845 mutex_unlock(&ptdev->mmu->vm.lock); 1846 1847 drm_sched_entity_destroy(&vm->entity); 1848 drm_sched_fini(&vm->sched); 1849 1850 mutex_lock(&ptdev->mmu->as.slots_lock); 1851 if (vm->as.id >= 0) { 1852 int cookie; 1853 1854 if (drm_dev_enter(&ptdev->base, &cookie)) { 1855 panthor_mmu_as_disable(ptdev, vm->as.id); 1856 drm_dev_exit(cookie); 1857 } 1858 1859 ptdev->mmu->as.slots[vm->as.id].vm = NULL; 1860 clear_bit(vm->as.id, &ptdev->mmu->as.alloc_mask); 1861 list_del(&vm->as.lru_node); 1862 } 1863 mutex_unlock(&ptdev->mmu->as.slots_lock); 1864 1865 free_io_pgtable_ops(vm->pgtbl_ops); 1866 1867 drm_mm_takedown(&vm->mm); 1868 kfree(vm); 1869 } 1870 1871 /** 1872 * panthor_vm_put() - Release a reference on a VM 1873 * @vm: VM to release the reference on. Can be NULL. 1874 */ 1875 void panthor_vm_put(struct panthor_vm *vm) 1876 { 1877 drm_gpuvm_put(vm ? &vm->base : NULL); 1878 } 1879 1880 /** 1881 * panthor_vm_get() - Get a VM reference 1882 * @vm: VM to get the reference on. Can be NULL. 1883 * 1884 * Return: @vm value. 1885 */ 1886 struct panthor_vm *panthor_vm_get(struct panthor_vm *vm) 1887 { 1888 if (vm) 1889 drm_gpuvm_get(&vm->base); 1890 1891 return vm; 1892 } 1893 1894 /** 1895 * panthor_vm_get_heap_pool() - Get the heap pool attached to a VM 1896 * @vm: VM to query the heap pool on. 1897 * @create: True if the heap pool should be created when it doesn't exist. 1898 * 1899 * Heap pools are per-VM. This function allows one to retrieve the heap pool 1900 * attached to a VM. 1901 * 1902 * If no heap pool exists yet, and @create is true, we create one. 1903 * 1904 * The returned panthor_heap_pool should be released with panthor_heap_pool_put(). 1905 * 1906 * Return: A valid pointer on success, an ERR_PTR() otherwise. 1907 */ 1908 struct panthor_heap_pool *panthor_vm_get_heap_pool(struct panthor_vm *vm, bool create) 1909 { 1910 struct panthor_heap_pool *pool; 1911 1912 mutex_lock(&vm->heaps.lock); 1913 if (!vm->heaps.pool && create) { 1914 if (vm->destroyed) 1915 pool = ERR_PTR(-EINVAL); 1916 else 1917 pool = panthor_heap_pool_create(vm->ptdev, vm); 1918 1919 if (!IS_ERR(pool)) 1920 vm->heaps.pool = panthor_heap_pool_get(pool); 1921 } else { 1922 pool = panthor_heap_pool_get(vm->heaps.pool); 1923 if (!pool) 1924 pool = ERR_PTR(-ENOENT); 1925 } 1926 mutex_unlock(&vm->heaps.lock); 1927 1928 return pool; 1929 } 1930 1931 /** 1932 * panthor_vm_heaps_sizes() - Calculate size of all heap chunks across all 1933 * heaps over all the heap pools in a VM 1934 * @pfile: File. 1935 * @stats: Memory stats to be updated. 1936 * 1937 * Calculate all heap chunk sizes in all heap pools bound to a VM. If the VM 1938 * is active, record the size as active as well. 1939 */ 1940 void panthor_vm_heaps_sizes(struct panthor_file *pfile, struct drm_memory_stats *stats) 1941 { 1942 struct panthor_vm *vm; 1943 unsigned long i; 1944 1945 if (!pfile->vms) 1946 return; 1947 1948 xa_lock(&pfile->vms->xa); 1949 xa_for_each(&pfile->vms->xa, i, vm) { 1950 size_t size = panthor_heap_pool_size(vm->heaps.pool); 1951 stats->resident += size; 1952 if (vm->as.id >= 0) 1953 stats->active += size; 1954 } 1955 xa_unlock(&pfile->vms->xa); 1956 } 1957 1958 static u64 mair_to_memattr(u64 mair, bool coherent) 1959 { 1960 u64 memattr = 0; 1961 u32 i; 1962 1963 for (i = 0; i < 8; i++) { 1964 u8 in_attr = mair >> (8 * i), out_attr; 1965 u8 outer = in_attr >> 4, inner = in_attr & 0xf; 1966 1967 /* For caching to be enabled, inner and outer caching policy 1968 * have to be both write-back, if one of them is write-through 1969 * or non-cacheable, we just choose non-cacheable. Device 1970 * memory is also translated to non-cacheable. 1971 */ 1972 if (!(outer & 3) || !(outer & 4) || !(inner & 4)) { 1973 out_attr = AS_MEMATTR_AARCH64_INNER_OUTER_NC | 1974 AS_MEMATTR_AARCH64_SH_MIDGARD_INNER | 1975 AS_MEMATTR_AARCH64_INNER_ALLOC_EXPL(false, false); 1976 } else { 1977 out_attr = AS_MEMATTR_AARCH64_INNER_OUTER_WB | 1978 AS_MEMATTR_AARCH64_INNER_ALLOC_EXPL(inner & 1, inner & 2); 1979 /* Use SH_MIDGARD_INNER mode when device isn't coherent, 1980 * so SH_IS, which is used when IOMMU_CACHE is set, maps 1981 * to Mali's internal-shareable mode. As per the Mali 1982 * Spec, inner and outer-shareable modes aren't allowed 1983 * for WB memory when coherency is disabled. 1984 * Use SH_CPU_INNER mode when coherency is enabled, so 1985 * that SH_IS actually maps to the standard definition of 1986 * inner-shareable. 1987 */ 1988 if (!coherent) 1989 out_attr |= AS_MEMATTR_AARCH64_SH_MIDGARD_INNER; 1990 else 1991 out_attr |= AS_MEMATTR_AARCH64_SH_CPU_INNER; 1992 } 1993 1994 memattr |= (u64)out_attr << (8 * i); 1995 } 1996 1997 return memattr; 1998 } 1999 2000 static void panthor_vma_link(struct panthor_vm *vm, 2001 struct panthor_vma *vma, 2002 struct drm_gpuvm_bo *vm_bo) 2003 { 2004 struct panthor_gem_object *bo = to_panthor_bo(vma->base.gem.obj); 2005 2006 mutex_lock(&bo->gpuva_list_lock); 2007 drm_gpuva_link(&vma->base, vm_bo); 2008 drm_WARN_ON(&vm->ptdev->base, drm_gpuvm_bo_put(vm_bo)); 2009 mutex_unlock(&bo->gpuva_list_lock); 2010 } 2011 2012 static void panthor_vma_unlink(struct panthor_vm *vm, 2013 struct panthor_vma *vma) 2014 { 2015 struct panthor_gem_object *bo = to_panthor_bo(vma->base.gem.obj); 2016 struct drm_gpuvm_bo *vm_bo = drm_gpuvm_bo_get(vma->base.vm_bo); 2017 2018 mutex_lock(&bo->gpuva_list_lock); 2019 drm_gpuva_unlink(&vma->base); 2020 mutex_unlock(&bo->gpuva_list_lock); 2021 2022 /* drm_gpuva_unlink() release the vm_bo, but we manually retained it 2023 * when entering this function, so we can implement deferred VMA 2024 * destruction. Re-assign it here. 2025 */ 2026 vma->base.vm_bo = vm_bo; 2027 list_add_tail(&vma->node, &vm->op_ctx->returned_vmas); 2028 } 2029 2030 static void panthor_vma_init(struct panthor_vma *vma, u32 flags) 2031 { 2032 INIT_LIST_HEAD(&vma->node); 2033 vma->flags = flags; 2034 } 2035 2036 #define PANTHOR_VM_MAP_FLAGS \ 2037 (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ 2038 DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ 2039 DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED) 2040 2041 static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) 2042 { 2043 struct panthor_vm *vm = priv; 2044 struct panthor_vm_op_ctx *op_ctx = vm->op_ctx; 2045 struct panthor_vma *vma = panthor_vm_op_ctx_get_vma(op_ctx); 2046 int ret; 2047 2048 if (!vma) 2049 return -EINVAL; 2050 2051 panthor_vma_init(vma, op_ctx->flags & PANTHOR_VM_MAP_FLAGS); 2052 2053 ret = panthor_vm_map_pages(vm, op->map.va.addr, flags_to_prot(vma->flags), 2054 op_ctx->map.sgt, op->map.gem.offset, 2055 op->map.va.range); 2056 if (ret) 2057 return ret; 2058 2059 /* Ref owned by the mapping now, clear the obj field so we don't release the 2060 * pinning/obj ref behind GPUVA's back. 2061 */ 2062 drm_gpuva_map(&vm->base, &vma->base, &op->map); 2063 panthor_vma_link(vm, vma, op_ctx->map.vm_bo); 2064 op_ctx->map.vm_bo = NULL; 2065 return 0; 2066 } 2067 2068 static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, 2069 void *priv) 2070 { 2071 struct panthor_vma *unmap_vma = container_of(op->remap.unmap->va, struct panthor_vma, base); 2072 struct panthor_vm *vm = priv; 2073 struct panthor_vm_op_ctx *op_ctx = vm->op_ctx; 2074 struct panthor_vma *prev_vma = NULL, *next_vma = NULL; 2075 u64 unmap_start, unmap_range; 2076 int ret; 2077 2078 drm_gpuva_op_remap_to_unmap_range(&op->remap, &unmap_start, &unmap_range); 2079 ret = panthor_vm_unmap_pages(vm, unmap_start, unmap_range); 2080 if (ret) 2081 return ret; 2082 2083 if (op->remap.prev) { 2084 prev_vma = panthor_vm_op_ctx_get_vma(op_ctx); 2085 panthor_vma_init(prev_vma, unmap_vma->flags); 2086 } 2087 2088 if (op->remap.next) { 2089 next_vma = panthor_vm_op_ctx_get_vma(op_ctx); 2090 panthor_vma_init(next_vma, unmap_vma->flags); 2091 } 2092 2093 drm_gpuva_remap(prev_vma ? &prev_vma->base : NULL, 2094 next_vma ? &next_vma->base : NULL, 2095 &op->remap); 2096 2097 if (prev_vma) { 2098 /* panthor_vma_link() transfers the vm_bo ownership to 2099 * the VMA object. Since the vm_bo we're passing is still 2100 * owned by the old mapping which will be released when this 2101 * mapping is destroyed, we need to grab a ref here. 2102 */ 2103 panthor_vma_link(vm, prev_vma, 2104 drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo)); 2105 } 2106 2107 if (next_vma) { 2108 panthor_vma_link(vm, next_vma, 2109 drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo)); 2110 } 2111 2112 panthor_vma_unlink(vm, unmap_vma); 2113 return 0; 2114 } 2115 2116 static int panthor_gpuva_sm_step_unmap(struct drm_gpuva_op *op, 2117 void *priv) 2118 { 2119 struct panthor_vma *unmap_vma = container_of(op->unmap.va, struct panthor_vma, base); 2120 struct panthor_vm *vm = priv; 2121 int ret; 2122 2123 ret = panthor_vm_unmap_pages(vm, unmap_vma->base.va.addr, 2124 unmap_vma->base.va.range); 2125 if (drm_WARN_ON(&vm->ptdev->base, ret)) 2126 return ret; 2127 2128 drm_gpuva_unmap(&op->unmap); 2129 panthor_vma_unlink(vm, unmap_vma); 2130 return 0; 2131 } 2132 2133 static const struct drm_gpuvm_ops panthor_gpuvm_ops = { 2134 .vm_free = panthor_vm_free, 2135 .sm_step_map = panthor_gpuva_sm_step_map, 2136 .sm_step_remap = panthor_gpuva_sm_step_remap, 2137 .sm_step_unmap = panthor_gpuva_sm_step_unmap, 2138 }; 2139 2140 /** 2141 * panthor_vm_resv() - Get the dma_resv object attached to a VM. 2142 * @vm: VM to get the dma_resv of. 2143 * 2144 * Return: A dma_resv object. 2145 */ 2146 struct dma_resv *panthor_vm_resv(struct panthor_vm *vm) 2147 { 2148 return drm_gpuvm_resv(&vm->base); 2149 } 2150 2151 struct drm_gem_object *panthor_vm_root_gem(struct panthor_vm *vm) 2152 { 2153 if (!vm) 2154 return NULL; 2155 2156 return vm->base.r_obj; 2157 } 2158 2159 static int 2160 panthor_vm_exec_op(struct panthor_vm *vm, struct panthor_vm_op_ctx *op, 2161 bool flag_vm_unusable_on_failure) 2162 { 2163 u32 op_type = op->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK; 2164 int ret; 2165 2166 if (op_type == DRM_PANTHOR_VM_BIND_OP_TYPE_SYNC_ONLY) 2167 return 0; 2168 2169 mutex_lock(&vm->op_lock); 2170 vm->op_ctx = op; 2171 switch (op_type) { 2172 case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP: 2173 if (vm->unusable) { 2174 ret = -EINVAL; 2175 break; 2176 } 2177 2178 ret = drm_gpuvm_sm_map(&vm->base, vm, op->va.addr, op->va.range, 2179 op->map.vm_bo->obj, op->map.bo_offset); 2180 break; 2181 2182 case DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP: 2183 ret = drm_gpuvm_sm_unmap(&vm->base, vm, op->va.addr, op->va.range); 2184 break; 2185 2186 default: 2187 ret = -EINVAL; 2188 break; 2189 } 2190 2191 if (ret && flag_vm_unusable_on_failure) 2192 vm->unusable = true; 2193 2194 vm->op_ctx = NULL; 2195 mutex_unlock(&vm->op_lock); 2196 2197 return ret; 2198 } 2199 2200 static struct dma_fence * 2201 panthor_vm_bind_run_job(struct drm_sched_job *sched_job) 2202 { 2203 struct panthor_vm_bind_job *job = container_of(sched_job, struct panthor_vm_bind_job, base); 2204 bool cookie; 2205 int ret; 2206 2207 /* Not only we report an error whose result is propagated to the 2208 * drm_sched finished fence, but we also flag the VM as unusable, because 2209 * a failure in the async VM_BIND results in an inconsistent state. VM needs 2210 * to be destroyed and recreated. 2211 */ 2212 cookie = dma_fence_begin_signalling(); 2213 ret = panthor_vm_exec_op(job->vm, &job->ctx, true); 2214 dma_fence_end_signalling(cookie); 2215 2216 return ret ? ERR_PTR(ret) : NULL; 2217 } 2218 2219 static void panthor_vm_bind_job_release(struct kref *kref) 2220 { 2221 struct panthor_vm_bind_job *job = container_of(kref, struct panthor_vm_bind_job, refcount); 2222 2223 if (job->base.s_fence) 2224 drm_sched_job_cleanup(&job->base); 2225 2226 panthor_vm_cleanup_op_ctx(&job->ctx, job->vm); 2227 panthor_vm_put(job->vm); 2228 kfree(job); 2229 } 2230 2231 /** 2232 * panthor_vm_bind_job_put() - Release a VM_BIND job reference 2233 * @sched_job: Job to release the reference on. 2234 */ 2235 void panthor_vm_bind_job_put(struct drm_sched_job *sched_job) 2236 { 2237 struct panthor_vm_bind_job *job = 2238 container_of(sched_job, struct panthor_vm_bind_job, base); 2239 2240 if (sched_job) 2241 kref_put(&job->refcount, panthor_vm_bind_job_release); 2242 } 2243 2244 static void 2245 panthor_vm_bind_free_job(struct drm_sched_job *sched_job) 2246 { 2247 struct panthor_vm_bind_job *job = 2248 container_of(sched_job, struct panthor_vm_bind_job, base); 2249 2250 drm_sched_job_cleanup(sched_job); 2251 2252 /* Do the heavy cleanups asynchronously, so we're out of the 2253 * dma-signaling path and can acquire dma-resv locks safely. 2254 */ 2255 queue_work(panthor_cleanup_wq, &job->cleanup_op_ctx_work); 2256 } 2257 2258 static enum drm_gpu_sched_stat 2259 panthor_vm_bind_timedout_job(struct drm_sched_job *sched_job) 2260 { 2261 WARN(1, "VM_BIND ops are synchronous for now, there should be no timeout!"); 2262 return DRM_GPU_SCHED_STAT_RESET; 2263 } 2264 2265 static const struct drm_sched_backend_ops panthor_vm_bind_ops = { 2266 .run_job = panthor_vm_bind_run_job, 2267 .free_job = panthor_vm_bind_free_job, 2268 .timedout_job = panthor_vm_bind_timedout_job, 2269 }; 2270 2271 /** 2272 * panthor_vm_create() - Create a VM 2273 * @ptdev: Device. 2274 * @for_mcu: True if this is the FW MCU VM. 2275 * @kernel_va_start: Start of the range reserved for kernel BO mapping. 2276 * @kernel_va_size: Size of the range reserved for kernel BO mapping. 2277 * @auto_kernel_va_start: Start of the auto-VA kernel range. 2278 * @auto_kernel_va_size: Size of the auto-VA kernel range. 2279 * 2280 * Return: A valid pointer on success, an ERR_PTR() otherwise. 2281 */ 2282 struct panthor_vm * 2283 panthor_vm_create(struct panthor_device *ptdev, bool for_mcu, 2284 u64 kernel_va_start, u64 kernel_va_size, 2285 u64 auto_kernel_va_start, u64 auto_kernel_va_size) 2286 { 2287 u32 va_bits = GPU_MMU_FEATURES_VA_BITS(ptdev->gpu_info.mmu_features); 2288 u32 pa_bits = GPU_MMU_FEATURES_PA_BITS(ptdev->gpu_info.mmu_features); 2289 u64 full_va_range = 1ull << va_bits; 2290 struct drm_gem_object *dummy_gem; 2291 struct drm_gpu_scheduler *sched; 2292 const struct drm_sched_init_args sched_args = { 2293 .ops = &panthor_vm_bind_ops, 2294 .submit_wq = ptdev->mmu->vm.wq, 2295 .num_rqs = 1, 2296 .credit_limit = 1, 2297 /* Bind operations are synchronous for now, no timeout needed. */ 2298 .timeout = MAX_SCHEDULE_TIMEOUT, 2299 .name = "panthor-vm-bind", 2300 .dev = ptdev->base.dev, 2301 }; 2302 struct io_pgtable_cfg pgtbl_cfg; 2303 u64 mair, min_va, va_range; 2304 struct panthor_vm *vm; 2305 int ret; 2306 2307 vm = kzalloc(sizeof(*vm), GFP_KERNEL); 2308 if (!vm) 2309 return ERR_PTR(-ENOMEM); 2310 2311 /* We allocate a dummy GEM for the VM. */ 2312 dummy_gem = drm_gpuvm_resv_object_alloc(&ptdev->base); 2313 if (!dummy_gem) { 2314 ret = -ENOMEM; 2315 goto err_free_vm; 2316 } 2317 2318 mutex_init(&vm->heaps.lock); 2319 vm->for_mcu = for_mcu; 2320 vm->ptdev = ptdev; 2321 mutex_init(&vm->op_lock); 2322 2323 if (for_mcu) { 2324 /* CSF MCU is a cortex M7, and can only address 4G */ 2325 min_va = 0; 2326 va_range = SZ_4G; 2327 } else { 2328 min_va = 0; 2329 va_range = full_va_range; 2330 } 2331 2332 mutex_init(&vm->mm_lock); 2333 drm_mm_init(&vm->mm, kernel_va_start, kernel_va_size); 2334 vm->kernel_auto_va.start = auto_kernel_va_start; 2335 vm->kernel_auto_va.end = vm->kernel_auto_va.start + auto_kernel_va_size - 1; 2336 2337 INIT_LIST_HEAD(&vm->node); 2338 INIT_LIST_HEAD(&vm->as.lru_node); 2339 vm->as.id = -1; 2340 refcount_set(&vm->as.active_cnt, 0); 2341 2342 pgtbl_cfg = (struct io_pgtable_cfg) { 2343 .pgsize_bitmap = SZ_4K | SZ_2M, 2344 .ias = va_bits, 2345 .oas = pa_bits, 2346 .coherent_walk = ptdev->coherent, 2347 .tlb = &mmu_tlb_ops, 2348 .iommu_dev = ptdev->base.dev, 2349 .alloc = alloc_pt, 2350 .free = free_pt, 2351 }; 2352 2353 vm->pgtbl_ops = alloc_io_pgtable_ops(ARM_64_LPAE_S1, &pgtbl_cfg, vm); 2354 if (!vm->pgtbl_ops) { 2355 ret = -EINVAL; 2356 goto err_mm_takedown; 2357 } 2358 2359 ret = drm_sched_init(&vm->sched, &sched_args); 2360 if (ret) 2361 goto err_free_io_pgtable; 2362 2363 sched = &vm->sched; 2364 ret = drm_sched_entity_init(&vm->entity, 0, &sched, 1, NULL); 2365 if (ret) 2366 goto err_sched_fini; 2367 2368 mair = io_pgtable_ops_to_pgtable(vm->pgtbl_ops)->cfg.arm_lpae_s1_cfg.mair; 2369 vm->memattr = mair_to_memattr(mair, ptdev->coherent); 2370 2371 mutex_lock(&ptdev->mmu->vm.lock); 2372 list_add_tail(&vm->node, &ptdev->mmu->vm.list); 2373 2374 /* If a reset is in progress, stop the scheduler. */ 2375 if (ptdev->mmu->vm.reset_in_progress) 2376 panthor_vm_stop(vm); 2377 mutex_unlock(&ptdev->mmu->vm.lock); 2378 2379 /* We intentionally leave the reserved range to zero, because we want kernel VMAs 2380 * to be handled the same way user VMAs are. 2381 */ 2382 drm_gpuvm_init(&vm->base, for_mcu ? "panthor-MCU-VM" : "panthor-GPU-VM", 2383 DRM_GPUVM_RESV_PROTECTED, &ptdev->base, dummy_gem, 2384 min_va, va_range, 0, 0, &panthor_gpuvm_ops); 2385 drm_gem_object_put(dummy_gem); 2386 return vm; 2387 2388 err_sched_fini: 2389 drm_sched_fini(&vm->sched); 2390 2391 err_free_io_pgtable: 2392 free_io_pgtable_ops(vm->pgtbl_ops); 2393 2394 err_mm_takedown: 2395 drm_mm_takedown(&vm->mm); 2396 drm_gem_object_put(dummy_gem); 2397 2398 err_free_vm: 2399 kfree(vm); 2400 return ERR_PTR(ret); 2401 } 2402 2403 static int 2404 panthor_vm_bind_prepare_op_ctx(struct drm_file *file, 2405 struct panthor_vm *vm, 2406 const struct drm_panthor_vm_bind_op *op, 2407 struct panthor_vm_op_ctx *op_ctx) 2408 { 2409 ssize_t vm_pgsz = panthor_vm_page_size(vm); 2410 struct drm_gem_object *gem; 2411 int ret; 2412 2413 /* Aligned on page size. */ 2414 if (!IS_ALIGNED(op->va | op->size, vm_pgsz)) 2415 return -EINVAL; 2416 2417 switch (op->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) { 2418 case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP: 2419 gem = drm_gem_object_lookup(file, op->bo_handle); 2420 ret = panthor_vm_prepare_map_op_ctx(op_ctx, vm, 2421 gem ? to_panthor_bo(gem) : NULL, 2422 op->bo_offset, 2423 op->size, 2424 op->va, 2425 op->flags); 2426 drm_gem_object_put(gem); 2427 return ret; 2428 2429 case DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP: 2430 if (op->flags & ~DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) 2431 return -EINVAL; 2432 2433 if (op->bo_handle || op->bo_offset) 2434 return -EINVAL; 2435 2436 return panthor_vm_prepare_unmap_op_ctx(op_ctx, vm, op->va, op->size); 2437 2438 case DRM_PANTHOR_VM_BIND_OP_TYPE_SYNC_ONLY: 2439 if (op->flags & ~DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) 2440 return -EINVAL; 2441 2442 if (op->bo_handle || op->bo_offset) 2443 return -EINVAL; 2444 2445 if (op->va || op->size) 2446 return -EINVAL; 2447 2448 if (!op->syncs.count) 2449 return -EINVAL; 2450 2451 panthor_vm_prepare_sync_only_op_ctx(op_ctx, vm); 2452 return 0; 2453 2454 default: 2455 return -EINVAL; 2456 } 2457 } 2458 2459 static void panthor_vm_bind_job_cleanup_op_ctx_work(struct work_struct *work) 2460 { 2461 struct panthor_vm_bind_job *job = 2462 container_of(work, struct panthor_vm_bind_job, cleanup_op_ctx_work); 2463 2464 panthor_vm_bind_job_put(&job->base); 2465 } 2466 2467 /** 2468 * panthor_vm_bind_job_create() - Create a VM_BIND job 2469 * @file: File. 2470 * @vm: VM targeted by the VM_BIND job. 2471 * @op: VM operation data. 2472 * 2473 * Return: A valid pointer on success, an ERR_PTR() otherwise. 2474 */ 2475 struct drm_sched_job * 2476 panthor_vm_bind_job_create(struct drm_file *file, 2477 struct panthor_vm *vm, 2478 const struct drm_panthor_vm_bind_op *op) 2479 { 2480 struct panthor_vm_bind_job *job; 2481 int ret; 2482 2483 if (!vm) 2484 return ERR_PTR(-EINVAL); 2485 2486 if (vm->destroyed || vm->unusable) 2487 return ERR_PTR(-EINVAL); 2488 2489 job = kzalloc(sizeof(*job), GFP_KERNEL); 2490 if (!job) 2491 return ERR_PTR(-ENOMEM); 2492 2493 ret = panthor_vm_bind_prepare_op_ctx(file, vm, op, &job->ctx); 2494 if (ret) { 2495 kfree(job); 2496 return ERR_PTR(ret); 2497 } 2498 2499 INIT_WORK(&job->cleanup_op_ctx_work, panthor_vm_bind_job_cleanup_op_ctx_work); 2500 kref_init(&job->refcount); 2501 job->vm = panthor_vm_get(vm); 2502 2503 ret = drm_sched_job_init(&job->base, &vm->entity, 1, vm, file->client_id); 2504 if (ret) 2505 goto err_put_job; 2506 2507 return &job->base; 2508 2509 err_put_job: 2510 panthor_vm_bind_job_put(&job->base); 2511 return ERR_PTR(ret); 2512 } 2513 2514 /** 2515 * panthor_vm_bind_job_prepare_resvs() - Prepare VM_BIND job dma_resvs 2516 * @exec: The locking/preparation context. 2517 * @sched_job: The job to prepare resvs on. 2518 * 2519 * Locks and prepare the VM resv. 2520 * 2521 * If this is a map operation, locks and prepares the GEM resv. 2522 * 2523 * Return: 0 on success, a negative error code otherwise. 2524 */ 2525 int panthor_vm_bind_job_prepare_resvs(struct drm_exec *exec, 2526 struct drm_sched_job *sched_job) 2527 { 2528 struct panthor_vm_bind_job *job = container_of(sched_job, struct panthor_vm_bind_job, base); 2529 int ret; 2530 2531 /* Acquire the VM lock an reserve a slot for this VM bind job. */ 2532 ret = drm_gpuvm_prepare_vm(&job->vm->base, exec, 1); 2533 if (ret) 2534 return ret; 2535 2536 if (job->ctx.map.vm_bo) { 2537 /* Lock/prepare the GEM being mapped. */ 2538 ret = drm_exec_prepare_obj(exec, job->ctx.map.vm_bo->obj, 1); 2539 if (ret) 2540 return ret; 2541 } 2542 2543 return 0; 2544 } 2545 2546 /** 2547 * panthor_vm_bind_job_update_resvs() - Update the resv objects touched by a job 2548 * @exec: drm_exec context. 2549 * @sched_job: Job to update the resvs on. 2550 */ 2551 void panthor_vm_bind_job_update_resvs(struct drm_exec *exec, 2552 struct drm_sched_job *sched_job) 2553 { 2554 struct panthor_vm_bind_job *job = container_of(sched_job, struct panthor_vm_bind_job, base); 2555 2556 /* Explicit sync => we just register our job finished fence as bookkeep. */ 2557 drm_gpuvm_resv_add_fence(&job->vm->base, exec, 2558 &sched_job->s_fence->finished, 2559 DMA_RESV_USAGE_BOOKKEEP, 2560 DMA_RESV_USAGE_BOOKKEEP); 2561 } 2562 2563 void panthor_vm_update_resvs(struct panthor_vm *vm, struct drm_exec *exec, 2564 struct dma_fence *fence, 2565 enum dma_resv_usage private_usage, 2566 enum dma_resv_usage extobj_usage) 2567 { 2568 drm_gpuvm_resv_add_fence(&vm->base, exec, fence, private_usage, extobj_usage); 2569 } 2570 2571 /** 2572 * panthor_vm_bind_exec_sync_op() - Execute a VM_BIND operation synchronously. 2573 * @file: File. 2574 * @vm: VM targeted by the VM operation. 2575 * @op: Data describing the VM operation. 2576 * 2577 * Return: 0 on success, a negative error code otherwise. 2578 */ 2579 int panthor_vm_bind_exec_sync_op(struct drm_file *file, 2580 struct panthor_vm *vm, 2581 struct drm_panthor_vm_bind_op *op) 2582 { 2583 struct panthor_vm_op_ctx op_ctx; 2584 int ret; 2585 2586 /* No sync objects allowed on synchronous operations. */ 2587 if (op->syncs.count) 2588 return -EINVAL; 2589 2590 if (!op->size) 2591 return 0; 2592 2593 ret = panthor_vm_bind_prepare_op_ctx(file, vm, op, &op_ctx); 2594 if (ret) 2595 return ret; 2596 2597 ret = panthor_vm_exec_op(vm, &op_ctx, false); 2598 panthor_vm_cleanup_op_ctx(&op_ctx, vm); 2599 2600 return ret; 2601 } 2602 2603 /** 2604 * panthor_vm_map_bo_range() - Map a GEM object range to a VM 2605 * @vm: VM to map the GEM to. 2606 * @bo: GEM object to map. 2607 * @offset: Offset in the GEM object. 2608 * @size: Size to map. 2609 * @va: Virtual address to map the object to. 2610 * @flags: Combination of drm_panthor_vm_bind_op_flags flags. 2611 * Only map-related flags are valid. 2612 * 2613 * Internal use only. For userspace requests, use 2614 * panthor_vm_bind_exec_sync_op() instead. 2615 * 2616 * Return: 0 on success, a negative error code otherwise. 2617 */ 2618 int panthor_vm_map_bo_range(struct panthor_vm *vm, struct panthor_gem_object *bo, 2619 u64 offset, u64 size, u64 va, u32 flags) 2620 { 2621 struct panthor_vm_op_ctx op_ctx; 2622 int ret; 2623 2624 ret = panthor_vm_prepare_map_op_ctx(&op_ctx, vm, bo, offset, size, va, flags); 2625 if (ret) 2626 return ret; 2627 2628 ret = panthor_vm_exec_op(vm, &op_ctx, false); 2629 panthor_vm_cleanup_op_ctx(&op_ctx, vm); 2630 2631 return ret; 2632 } 2633 2634 /** 2635 * panthor_vm_unmap_range() - Unmap a portion of the VA space 2636 * @vm: VM to unmap the region from. 2637 * @va: Virtual address to unmap. Must be 4k aligned. 2638 * @size: Size of the region to unmap. Must be 4k aligned. 2639 * 2640 * Internal use only. For userspace requests, use 2641 * panthor_vm_bind_exec_sync_op() instead. 2642 * 2643 * Return: 0 on success, a negative error code otherwise. 2644 */ 2645 int panthor_vm_unmap_range(struct panthor_vm *vm, u64 va, u64 size) 2646 { 2647 struct panthor_vm_op_ctx op_ctx; 2648 int ret; 2649 2650 ret = panthor_vm_prepare_unmap_op_ctx(&op_ctx, vm, va, size); 2651 if (ret) 2652 return ret; 2653 2654 ret = panthor_vm_exec_op(vm, &op_ctx, false); 2655 panthor_vm_cleanup_op_ctx(&op_ctx, vm); 2656 2657 return ret; 2658 } 2659 2660 /** 2661 * panthor_vm_prepare_mapped_bos_resvs() - Prepare resvs on VM BOs. 2662 * @exec: Locking/preparation context. 2663 * @vm: VM targeted by the GPU job. 2664 * @slot_count: Number of slots to reserve. 2665 * 2666 * GPU jobs assume all BOs bound to the VM at the time the job is submitted 2667 * are available when the job is executed. In order to guarantee that, we 2668 * need to reserve a slot on all BOs mapped to a VM and update this slot with 2669 * the job fence after its submission. 2670 * 2671 * Return: 0 on success, a negative error code otherwise. 2672 */ 2673 int panthor_vm_prepare_mapped_bos_resvs(struct drm_exec *exec, struct panthor_vm *vm, 2674 u32 slot_count) 2675 { 2676 int ret; 2677 2678 /* Acquire the VM lock and reserve a slot for this GPU job. */ 2679 ret = drm_gpuvm_prepare_vm(&vm->base, exec, slot_count); 2680 if (ret) 2681 return ret; 2682 2683 return drm_gpuvm_prepare_objects(&vm->base, exec, slot_count); 2684 } 2685 2686 /** 2687 * panthor_mmu_unplug() - Unplug the MMU logic 2688 * @ptdev: Device. 2689 * 2690 * No access to the MMU regs should be done after this function is called. 2691 * We suspend the IRQ and disable all VMs to guarantee that. 2692 */ 2693 void panthor_mmu_unplug(struct panthor_device *ptdev) 2694 { 2695 if (!IS_ENABLED(CONFIG_PM) || pm_runtime_active(ptdev->base.dev)) 2696 panthor_mmu_irq_suspend(&ptdev->mmu->irq); 2697 2698 mutex_lock(&ptdev->mmu->as.slots_lock); 2699 for (u32 i = 0; i < ARRAY_SIZE(ptdev->mmu->as.slots); i++) { 2700 struct panthor_vm *vm = ptdev->mmu->as.slots[i].vm; 2701 2702 if (vm) { 2703 drm_WARN_ON(&ptdev->base, panthor_mmu_as_disable(ptdev, i)); 2704 panthor_vm_release_as_locked(vm); 2705 } 2706 } 2707 mutex_unlock(&ptdev->mmu->as.slots_lock); 2708 } 2709 2710 static void panthor_mmu_release_wq(struct drm_device *ddev, void *res) 2711 { 2712 destroy_workqueue(res); 2713 } 2714 2715 /** 2716 * panthor_mmu_init() - Initialize the MMU logic. 2717 * @ptdev: Device. 2718 * 2719 * Return: 0 on success, a negative error code otherwise. 2720 */ 2721 int panthor_mmu_init(struct panthor_device *ptdev) 2722 { 2723 u32 va_bits = GPU_MMU_FEATURES_VA_BITS(ptdev->gpu_info.mmu_features); 2724 struct panthor_mmu *mmu; 2725 int ret, irq; 2726 2727 mmu = drmm_kzalloc(&ptdev->base, sizeof(*mmu), GFP_KERNEL); 2728 if (!mmu) 2729 return -ENOMEM; 2730 2731 INIT_LIST_HEAD(&mmu->as.lru_list); 2732 2733 ret = drmm_mutex_init(&ptdev->base, &mmu->as.slots_lock); 2734 if (ret) 2735 return ret; 2736 2737 INIT_LIST_HEAD(&mmu->vm.list); 2738 ret = drmm_mutex_init(&ptdev->base, &mmu->vm.lock); 2739 if (ret) 2740 return ret; 2741 2742 ptdev->mmu = mmu; 2743 2744 irq = platform_get_irq_byname(to_platform_device(ptdev->base.dev), "mmu"); 2745 if (irq <= 0) 2746 return -ENODEV; 2747 2748 ret = panthor_request_mmu_irq(ptdev, &mmu->irq, irq, 2749 panthor_mmu_fault_mask(ptdev, ~0)); 2750 if (ret) 2751 return ret; 2752 2753 mmu->vm.wq = alloc_workqueue("panthor-vm-bind", WQ_UNBOUND, 0); 2754 if (!mmu->vm.wq) 2755 return -ENOMEM; 2756 2757 /* On 32-bit kernels, the VA space is limited by the io_pgtable_ops abstraction, 2758 * which passes iova as an unsigned long. Patch the mmu_features to reflect this 2759 * limitation. 2760 */ 2761 if (va_bits > BITS_PER_LONG) { 2762 ptdev->gpu_info.mmu_features &= ~GENMASK(7, 0); 2763 ptdev->gpu_info.mmu_features |= BITS_PER_LONG; 2764 } 2765 2766 return drmm_add_action_or_reset(&ptdev->base, panthor_mmu_release_wq, mmu->vm.wq); 2767 } 2768 2769 #ifdef CONFIG_DEBUG_FS 2770 static int show_vm_gpuvas(struct panthor_vm *vm, struct seq_file *m) 2771 { 2772 int ret; 2773 2774 mutex_lock(&vm->op_lock); 2775 ret = drm_debugfs_gpuva_info(m, &vm->base); 2776 mutex_unlock(&vm->op_lock); 2777 2778 return ret; 2779 } 2780 2781 static int show_each_vm(struct seq_file *m, void *arg) 2782 { 2783 struct drm_info_node *node = (struct drm_info_node *)m->private; 2784 struct drm_device *ddev = node->minor->dev; 2785 struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base); 2786 int (*show)(struct panthor_vm *, struct seq_file *) = node->info_ent->data; 2787 struct panthor_vm *vm; 2788 int ret = 0; 2789 2790 mutex_lock(&ptdev->mmu->vm.lock); 2791 list_for_each_entry(vm, &ptdev->mmu->vm.list, node) { 2792 ret = show(vm, m); 2793 if (ret < 0) 2794 break; 2795 2796 seq_puts(m, "\n"); 2797 } 2798 mutex_unlock(&ptdev->mmu->vm.lock); 2799 2800 return ret; 2801 } 2802 2803 static struct drm_info_list panthor_mmu_debugfs_list[] = { 2804 DRM_DEBUGFS_GPUVA_INFO(show_each_vm, show_vm_gpuvas), 2805 }; 2806 2807 /** 2808 * panthor_mmu_debugfs_init() - Initialize MMU debugfs entries 2809 * @minor: Minor. 2810 */ 2811 void panthor_mmu_debugfs_init(struct drm_minor *minor) 2812 { 2813 drm_debugfs_create_files(panthor_mmu_debugfs_list, 2814 ARRAY_SIZE(panthor_mmu_debugfs_list), 2815 minor->debugfs_root, minor); 2816 } 2817 #endif /* CONFIG_DEBUG_FS */ 2818 2819 /** 2820 * panthor_mmu_pt_cache_init() - Initialize the page table cache. 2821 * 2822 * Return: 0 on success, a negative error code otherwise. 2823 */ 2824 int panthor_mmu_pt_cache_init(void) 2825 { 2826 pt_cache = kmem_cache_create("panthor-mmu-pt", SZ_4K, SZ_4K, 0, NULL); 2827 if (!pt_cache) 2828 return -ENOMEM; 2829 2830 return 0; 2831 } 2832 2833 /** 2834 * panthor_mmu_pt_cache_fini() - Destroy the page table cache. 2835 */ 2836 void panthor_mmu_pt_cache_fini(void) 2837 { 2838 kmem_cache_destroy(pt_cache); 2839 } 2840