/** * struct panthor_as_slot - Address space slot
*/ struct panthor_as_slot { /** @vm: VM bound to this slot. NULL is no VM is bound. */ struct panthor_vm *vm;
};
/** * struct panthor_mmu - MMU related data
*/ struct panthor_mmu { /** @irq: The MMU irq. */ struct panthor_irq irq;
/** * @as: Address space related fields. * * The GPU has a limited number of address spaces (AS) slots, forcing * us to re-assign them to re-assign slots on-demand.
*/ struct { /** @as.slots_lock: Lock protecting access to all other AS fields. */ struct mutex slots_lock;
/** @as.alloc_mask: Bitmask encoding the allocated slots. */ unsignedlong alloc_mask;
/** @as.faulty_mask: Bitmask encoding the faulty slots. */ unsignedlong faulty_mask;
/** @as.slots: VMs currently bound to the AS slots. */ struct panthor_as_slot slots[MAX_AS_SLOTS];
/** * @as.lru_list: List of least recently used VMs. * * We use this list to pick a VM to evict when all slots are * used. * * There should be no more active VMs than there are AS slots, * so this LRU is just here to keep VMs bound until there's * a need to release a slot, thus avoid unnecessary TLB/cache * flushes.
*/ struct list_head lru_list;
} as;
/** @vm.list: List containing all VMs. */ struct list_head list;
/** @vm.reset_in_progress: True if a reset is in progress. */ bool reset_in_progress;
/** @vm.wq: Workqueue used for the VM_BIND queues. */ struct workqueue_struct *wq;
} vm;
};
/** * struct panthor_vm_pool - VM pool object
*/ struct panthor_vm_pool { /** @xa: Array used for VM handle tracking. */ struct xarray xa;
};
/** * struct panthor_vma - GPU mapping object * * This is used to track GEM mappings in GPU space.
*/ struct panthor_vma { /** @base: Inherits from drm_gpuva. */ struct drm_gpuva base;
/** @node: Used to implement deferred release of VMAs. */ struct list_head node;
/** * @flags: Combination of drm_panthor_vm_bind_op_flags. * * Only map related flags are accepted.
*/
u32 flags;
};
/** * struct panthor_vm_op_ctx - VM operation context * * With VM operations potentially taking place in a dma-signaling path, we * need to make sure everything that might require resource allocation is * pre-allocated upfront. This is what this operation context is far. * * We also collect resources that have been freed, so we can release them * asynchronously, and let the VM_BIND scheduler process the next VM_BIND * request.
*/ struct panthor_vm_op_ctx { /** @rsvd_page_tables: Pages reserved for the MMU page table update. */ struct { /** @rsvd_page_tables.count: Number of pages reserved. */
u32 count;
/** @rsvd_page_tables.ptr: Point to the first unused page in the @pages table. */
u32 ptr;
/** * @rsvd_page_tables.pages: Array of pages to be used for an MMU page table update. * * After an VM operation, there might be free pages left in this array. * They should be returned to the pt_cache as part of the op_ctx cleanup.
*/ void **pages;
} rsvd_page_tables;
/** * @preallocated_vmas: Pre-allocated VMAs to handle the remap case. * * Partial unmap requests or map requests overlapping existing mappings will * trigger a remap call, which need to register up to three panthor_vma objects * (one for the new mapping, and two for the previous and next mappings).
*/ struct panthor_vma *preallocated_vmas[3];
/** @flags: Combination of drm_panthor_vm_bind_op_flags. */
u32 flags;
/** @va: Virtual range targeted by the VM operation. */ struct { /** @va.addr: Start address. */
u64 addr;
/** @va.range: Range size. */
u64 range;
} va;
/** * @returned_vmas: List of panthor_vma objects returned after a VM operation. * * For unmap operations, this will contain all VMAs that were covered by the * specified VA range. * * For map operations, this will contain all VMAs that previously mapped to * the specified VA range. * * Those VMAs, and the resources they point to will be released as part of * the op_ctx cleanup operation.
*/ struct list_head returned_vmas;
/** @map: Fields specific to a map operation. */ struct { /** @map.vm_bo: Buffer object to map. */ struct drm_gpuvm_bo *vm_bo;
/** @map.bo_offset: Offset in the buffer object. */
u64 bo_offset;
/** * @map.sgt: sg-table pointing to pages backing the GEM object. * * This is gathered at job creation time, such that we don't have * to allocate in ::run_job().
*/ struct sg_table *sgt;
/** * @map.new_vma: The new VMA object that will be inserted to the VA tree.
*/ struct panthor_vma *new_vma;
} map;
};
/** * struct panthor_vm - VM object * * A VM is an object representing a GPU (or MCU) virtual address space. * It embeds the MMU page table for this address space, a tree containing * all the virtual mappings of GEM objects, and other things needed to manage * the VM. * * Except for the MCU VM, which is managed by the kernel, all other VMs are * created by userspace and mostly managed by userspace, using the * %DRM_IOCTL_PANTHOR_VM_BIND ioctl. * * A portion of the virtual address space is reserved for kernel objects, * like heap chunks, and userspace gets to decide how much of the virtual * address space is left to the kernel (half of the virtual address space * by default).
*/ struct panthor_vm { /** * @base: Inherit from drm_gpuvm. * * We delegate all the VA management to the common drm_gpuvm framework * and only implement hooks to update the MMU page table.
*/ struct drm_gpuvm base;
/** * @sched: Scheduler used for asynchronous VM_BIND request. * * We use a 1:1 scheduler here.
*/ struct drm_gpu_scheduler sched;
/** * @entity: Scheduling entity representing the VM_BIND queue. * * There's currently one bind queue per VM. It doesn't make sense to * allow more given the VM operations are serialized anyway.
*/ struct drm_sched_entity entity;
/** * @op_lock: Lock used to serialize operations on a VM. * * The serialization of jobs queued to the VM_BIND queue is already * taken care of by drm_sched, but we need to serialize synchronous * and asynchronous VM_BIND request. This is what this lock is for.
*/ struct mutex op_lock;
/** * @op_ctx: The context attached to the currently executing VM operation. * * NULL when no operation is in progress.
*/ struct panthor_vm_op_ctx *op_ctx;
/** * @mm: Memory management object representing the auto-VA/kernel-VA. * * Used to auto-allocate VA space for kernel-managed objects (tiler * heaps, ...). * * For the MCU VM, this is managing the VA range that's used to map * all shared interfaces. * * For user VMs, the range is specified by userspace, and must not * exceed half of the VA space addressable.
*/ struct drm_mm mm;
/** @kernel_auto_va: Automatic VA-range for kernel BOs. */ struct { /** @kernel_auto_va.start: Start of the automatic VA-range for kernel BOs. */
u64 start;
/** @kernel_auto_va.size: Size of the automatic VA-range for kernel BOs. */
u64 end;
} kernel_auto_va;
/** @as: Address space related fields. */ struct { /** * @as.id: ID of the address space this VM is bound to. * * A value of -1 means the VM is inactive/not bound.
*/ int id;
/** @as.active_cnt: Number of active users of this VM. */
refcount_t active_cnt;
/** * @as.lru_node: Used to instead the VM in the panthor_mmu::as::lru_list. * * Active VMs should not be inserted in the LRU list.
*/ struct list_head lru_node;
} as;
/** * @heaps: Tiler heap related fields.
*/ struct { /** * @heaps.pool: The heap pool attached to this VM. * * Will stay NULL until someone creates a heap context on this VM.
*/ struct panthor_heap_pool *pool;
/** @heaps.lock: Lock used to protect access to @pool. */ struct mutex lock;
} heaps;
/** @node: Used to insert the VM in the panthor_mmu::vm::list. */ struct list_head node;
/** @for_mcu: True if this is the MCU VM. */ bool for_mcu;
/** * @destroyed: True if the VM was destroyed. * * No further bind requests should be queued to a destroyed VM.
*/ bool destroyed;
/** * @unusable: True if the VM has turned unusable because something * bad happened during an asynchronous request. * * We don't try to recover from such failures, because this implies * informing userspace about the specific operation that failed, and * hoping the userspace driver can replay things from there. This all * sounds very complicated for little gain. * * Instead, we should just flag the VM as unusable, and fail any * further request targeting this VM. * * We also provide a way to query a VM state, so userspace can destroy * it and create a new one. * * As an analogy, this would be mapped to a VK_ERROR_DEVICE_LOST * situation, where the logical device needs to be re-created.
*/ bool unusable;
/** * @unhandled_fault: Unhandled fault happened. * * This should be reported to the scheduler, and the queue/group be * flagged as faulty as a result.
*/ bool unhandled_fault;
};
/* * @pt_cache: Cache used to allocate MMU page tables. * * The pre-allocation pattern forces us to over-allocate to plan for * the worst case scenario, and return the pages we didn't use. * * Having a kmem_cache allows us to speed allocations.
*/ staticstruct kmem_cache *pt_cache;
/** * alloc_pt() - Custom page table allocator * @cookie: Cookie passed at page table allocation time. * @size: Size of the page table. This size should be fixed, * and determined at creation time based on the granule size. * @gfp: GFP flags. * * We want a custom allocator so we can use a cache for page table * allocations and amortize the cost of the over-reservation that's * done to allow asynchronous VM operations. * * Return: non-NULL on success, NULL if the allocation failed for any * reason.
*/ staticvoid *alloc_pt(void *cookie, size_t size, gfp_t gfp)
{ struct panthor_vm *vm = cookie; void *page;
/* Allocation of the root page table happening during init. */ if (unlikely(!vm->root_page_table)) { struct page *p;
/* We're not supposed to have anything bigger than 4k here, because we picked a * 4k granule size at init time.
*/ if (drm_WARN_ON(&vm->ptdev->base, size != SZ_4K)) return NULL;
/* We must have some op_ctx attached to the VM and it must have at least one * free page.
*/ if (drm_WARN_ON(&vm->ptdev->base, !vm->op_ctx) ||
drm_WARN_ON(&vm->ptdev->base,
vm->op_ctx->rsvd_page_tables.ptr >= vm->op_ctx->rsvd_page_tables.count)) return NULL;
/* Page table entries don't use virtual addresses, which trips out * kmemleak. kmemleak_alloc_phys() might work, but physical addresses * are mixed with other fields, and I fear kmemleak won't detect that * either. * * Let's just ignore memory passed to the page-table driver for now.
*/
kmemleak_ignore(page); return page;
}
/** * free_pt() - Custom page table free function * @cookie: Cookie passed at page table allocation time. * @data: Page table to free. * @size: Size of the page table. This size should be fixed, * and determined at creation time based on the granule size.
*/ staticvoid free_pt(void *cookie, void *data, size_t size)
{ struct panthor_vm *vm = cookie;
/* Wait for the MMU status to indicate there is no active command, in * case one is pending.
*/
ret = gpu_read_relaxed_poll_timeout_atomic(ptdev, AS_STATUS(as_nr), val,
!(val & AS_STATUS_AS_ACTIVE),
10, 100000);
if (ret) {
panthor_device_schedule_reset(ptdev);
drm_err(&ptdev->base, "AS_ACTIVE bit stuck\n");
}
/* write AS_COMMAND when MMU is ready to accept another command */
status = wait_ready(ptdev, as_nr); if (!status)
gpu_write(ptdev, AS_COMMAND(as_nr), cmd);
/* * The locked region is a naturally aligned power of 2 block encoded as * log2 minus(1). * Calculate the desired start/end and look for the highest bit which * differs. The smallest naturally aligned block must include this bit * change, the desired region starts with this bit (and subsequent bits) * zeroed and ends with the bit (and subsequent bits) set to one.
*/
region_width = max(fls64(region_start ^ (region_end - 1)),
const_ilog2(AS_LOCK_REGION_MIN_SIZE)) - 1;
/* * Mask off the low bits of region_start (which would be ignored by * the hardware anyway)
*/
region_start &= GENMASK_ULL(63, region_width);
region = region_width | region_start;
/* Lock the region that needs to be updated */
gpu_write64(ptdev, AS_LOCKADDR(as_nr), region);
write_cmd(ptdev, as_nr, AS_COMMAND_LOCK);
}
/** * panthor_vm_has_unhandled_faults() - Check if a VM has unhandled faults * @vm: VM to check. * * Return: true if the VM has unhandled faults, false otherwise.
*/ bool panthor_vm_has_unhandled_faults(struct panthor_vm *vm)
{ return vm->unhandled_fault;
}
/** * panthor_vm_is_unusable() - Check if the VM is still usable * @vm: VM to check. * * Return: true if the VM is unusable, false otherwise.
*/ bool panthor_vm_is_unusable(struct panthor_vm *vm)
{ return vm->unusable;
}
/** * panthor_vm_active() - Flag a VM as active * @vm: VM to flag as active. * * Assigns an address space to a VM so it can be used by the GPU/MCU. * * Return: 0 on success, a negative error code otherwise.
*/ int panthor_vm_active(struct panthor_vm *vm)
{ struct panthor_device *ptdev = vm->ptdev;
u32 va_bits = GPU_MMU_FEATURES_VA_BITS(ptdev->gpu_info.mmu_features); struct io_pgtable_cfg *cfg = &io_pgtable_ops_to_pgtable(vm->pgtbl_ops)->cfg; int ret = 0, as, cookie;
u64 transtab, transcfg;
if (!drm_dev_enter(&ptdev->base, &cookie)) return -ENODEV;
if (refcount_inc_not_zero(&vm->as.active_cnt)) goto out_dev_exit;
mutex_lock(&ptdev->mmu->as.slots_lock);
if (refcount_inc_not_zero(&vm->as.active_cnt)) goto out_unlock;
as = vm->as.id; if (as >= 0) { /* Unhandled pagefault on this AS, the MMU was disabled. We need to * re-enable the MMU after clearing+unmasking the AS interrupts.
*/ if (ptdev->mmu->as.faulty_mask & panthor_mmu_as_fault_mask(ptdev, as)) goto out_enable_as;
goto out_make_active;
}
/* Check for a free AS */ if (vm->for_mcu) {
drm_WARN_ON(&ptdev->base, ptdev->mmu->as.alloc_mask & BIT(0));
as = 0;
} else {
as = ffz(ptdev->mmu->as.alloc_mask | BIT(0));
}
if (!(BIT(as) & ptdev->gpu_info.as_present)) { struct panthor_vm *lru_vm;
lru_vm = list_first_entry_or_null(&ptdev->mmu->as.lru_list, struct panthor_vm,
as.lru_node); if (drm_WARN_ON(&ptdev->base, !lru_vm)) {
ret = -EBUSY; goto out_unlock;
}
drm_WARN_ON(&ptdev->base, refcount_read(&lru_vm->as.active_cnt));
as = lru_vm->as.id;
panthor_vm_release_as_locked(lru_vm);
}
/* Assign the free or reclaimed AS to the FD */
vm->as.id = as;
set_bit(as, &ptdev->mmu->as.alloc_mask);
ptdev->mmu->as.slots[as].vm = vm;
/** * panthor_vm_idle() - Flag a VM idle * @vm: VM to flag as idle. * * When we know the GPU is done with the VM (no more jobs to process), * we can relinquish the AS slot attached to this VM, if any. * * We don't release the slot immediately, but instead place the VM in * the LRU list, so it can be evicted if another VM needs an AS slot. * This way, VMs keep attached to the AS they were given until we run * out of free slot, limiting the number of MMU operations (TLB flush * and other AS updates).
*/ void panthor_vm_idle(struct panthor_vm *vm)
{ struct panthor_device *ptdev = vm->ptdev;
if (!refcount_dec_and_mutex_lock(&vm->as.active_cnt, &ptdev->mmu->as.slots_lock)) return;
if (!drm_WARN_ON(&ptdev->base, vm->as.id == -1 || !list_empty(&vm->as.lru_node)))
list_add_tail(&vm->as.lru_node, &ptdev->mmu->as.lru_list);
/** * panthor_vm_as() - Get the AS slot attached to a VM * @vm: VM to get the AS slot of. * * Return: -1 if the VM is not assigned an AS slot yet, >= 0 otherwise.
*/ int panthor_vm_as(struct panthor_vm *vm)
{ return vm->as.id;
}
static size_t get_pgsize(u64 addr, size_t size, size_t *count)
{ /* * io-pgtable only operates on multiple pages within a single table * entry, so we need to split at boundaries of the table size, i.e. * the next block size up. The distance from address A to the next * boundary of block size B is logically B - A % B, but in unsigned * two's complement where B is a power of two we get the equivalence * B - A % B == (B - A) % B == (n * B - A) % B, and choose n = 0 :)
*/
size_t blk_offset = -addr % SZ_2M;
ret = ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot,
GFP_KERNEL, &mapped);
iova += mapped;
paddr += mapped;
len -= mapped;
if (drm_WARN_ON(&ptdev->base, !ret && !mapped))
ret = -ENOMEM;
if (ret) { /* If something failed, unmap what we've already mapped before * returning. The unmap call is not supposed to fail.
*/
drm_WARN_ON(&ptdev->base,
panthor_vm_unmap_pages(vm, start_iova,
iova - start_iova)); return ret;
}
}
/** * panthor_vm_alloc_va() - Allocate a region in the auto-va space * @vm: VM to allocate a region on. * @va: start of the VA range. Can be PANTHOR_VM_KERNEL_AUTO_VA if the user * wants the VA to be automatically allocated from the auto-VA range. * @size: size of the VA range. * @va_node: drm_mm_node to initialize. Must be zero-initialized. * * Some GPU objects, like heap chunks, are fully managed by the kernel and * need to be mapped to the userspace VM, in the region reserved for kernel * objects. * * This function takes care of allocating a region in the kernel auto-VA space. * * Return: 0 on success, an error code otherwise.
*/ int
panthor_vm_alloc_va(struct panthor_vm *vm, u64 va, u64 size, struct drm_mm_node *va_node)
{
ssize_t vm_pgsz = panthor_vm_page_size(vm); int ret;
if (!size || !IS_ALIGNED(size, vm_pgsz)) return -EINVAL;
if (va != PANTHOR_VM_KERNEL_AUTO_VA && !IS_ALIGNED(va, vm_pgsz)) return -EINVAL;
/** * panthor_vm_free_va() - Free a region allocated with panthor_vm_alloc_va() * @vm: VM to free the region on. * @va_node: Memory node representing the region to free.
*/ void panthor_vm_free_va(struct panthor_vm *vm, struct drm_mm_node *va_node)
{
mutex_lock(&vm->mm_lock);
drm_mm_remove_node(va_node);
mutex_unlock(&vm->mm_lock);
}
/* We must retain the GEM before calling drm_gpuvm_bo_put(), * otherwise the mutex might be destroyed while we hold it. * Same goes for the VM, since we take the VM resv lock.
*/
drm_gem_object_get(&bo->base.base);
drm_gpuvm_get(vm);
/* We take the resv lock to protect against concurrent accesses to the * gpuvm evicted/extobj lists that are modified in * drm_gpuvm_bo_destroy(), which is called if drm_gpuvm_bo_put() * releases sthe last vm_bo reference. * We take the BO GPUVA list lock to protect the vm_bo removal from the * GEM vm_bo list.
*/
dma_resv_lock(drm_gpuvm_resv(vm), NULL);
mutex_lock(&bo->gpuva_list_lock);
unpin = drm_gpuvm_bo_put(vm_bo);
mutex_unlock(&bo->gpuva_list_lock);
dma_resv_unlock(drm_gpuvm_resv(vm));
/* If the vm_bo object was destroyed, release the pin reference that * was hold by this object.
*/ if (unpin && !drm_gem_is_imported(&bo->base.base))
drm_gem_shmem_unpin(&bo->base);
switch (op_ctx->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) { case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP: /* One VMA for the new mapping, and two more VMAs for the remap case * which might contain both a prev and next VA.
*/
vma_count = 3; break;
case DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP: /* Two VMAs can be needed for an unmap, as an unmap can happen * in the middle of a drm_gpuva, requiring a remap with both * prev & next VA. Or an unmap can span more than one drm_gpuva * where the first and last ones are covered partially, requring * a remap for the first with a prev VA and remap for the last * with a next VA.
*/
vma_count = 2; break;
default: return 0;
}
for (u32 i = 0; i < vma_count; i++) { struct panthor_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
/* Make sure the VA and size are in-bounds. */ if (size > bo->base.base.size || offset > bo->base.base.size - size) return -EINVAL;
/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ if (bo->exclusive_vm_root_gem &&
bo->exclusive_vm_root_gem != panthor_vm_root_gem(vm)) return -EINVAL;
ret = panthor_vm_op_ctx_prealloc_vmas(op_ctx); if (ret) goto err_cleanup;
if (!drm_gem_is_imported(&bo->base.base)) { /* Pre-reserve the BO pages, so the map operation doesn't have to * allocate.
*/
ret = drm_gem_shmem_pin(&bo->base); if (ret) goto err_cleanup;
}
sgt = drm_gem_shmem_get_pages_sgt(&bo->base); if (IS_ERR(sgt)) { if (!drm_gem_is_imported(&bo->base.base))
drm_gem_shmem_unpin(&bo->base);
ret = PTR_ERR(sgt); goto err_cleanup;
}
op_ctx->map.sgt = sgt;
preallocated_vm_bo = drm_gpuvm_bo_create(&vm->base, &bo->base.base); if (!preallocated_vm_bo) { if (!drm_gem_is_imported(&bo->base.base))
drm_gem_shmem_unpin(&bo->base);
ret = -ENOMEM; goto err_cleanup;
}
/* drm_gpuvm_bo_obtain_prealloc() will call drm_gpuvm_bo_put() on our * pre-allocated BO if the <BO,VM> association exists. Given we * only have one ref on preallocated_vm_bo, drm_gpuvm_bo_destroy() will * be called immediately, and we have to hold the VM resv lock when * calling this function.
*/
dma_resv_lock(panthor_vm_resv(vm), NULL);
mutex_lock(&bo->gpuva_list_lock);
op_ctx->map.vm_bo = drm_gpuvm_bo_obtain_prealloc(preallocated_vm_bo);
mutex_unlock(&bo->gpuva_list_lock);
dma_resv_unlock(panthor_vm_resv(vm));
/* If the a vm_bo for this <VM,BO> combination exists, it already * retains a pin ref, and we can release the one we took earlier. * * If our pre-allocated vm_bo is picked, it now retains the pin ref, * which will be released in panthor_vm_bo_put().
*/ if (preallocated_vm_bo != op_ctx->map.vm_bo &&
!drm_gem_is_imported(&bo->base.base))
drm_gem_shmem_unpin(&bo->base);
op_ctx->map.bo_offset = offset;
/* L1, L2 and L3 page tables. * We could optimize L3 allocation by iterating over the sgt and merging * 2M contiguous blocks, but it's simpler to over-provision and return * the pages if they're not used.
*/
pt_count = ((ALIGN(va + size, 1ull << 39) - ALIGN_DOWN(va, 1ull << 39)) >> 39) +
((ALIGN(va + size, 1ull << 30) - ALIGN_DOWN(va, 1ull << 30)) >> 30) +
((ALIGN(va + size, 1ull << 21) - ALIGN_DOWN(va, 1ull << 21)) >> 21);
op_ctx->rsvd_page_tables.pages = kcalloc(pt_count, sizeof(*op_ctx->rsvd_page_tables.pages),
GFP_KERNEL); if (!op_ctx->rsvd_page_tables.pages) {
ret = -ENOMEM; goto err_cleanup;
}
ret = kmem_cache_alloc_bulk(pt_cache, GFP_KERNEL, pt_count,
op_ctx->rsvd_page_tables.pages);
op_ctx->rsvd_page_tables.count = ret; if (ret != pt_count) {
ret = -ENOMEM; goto err_cleanup;
}
/* Insert BO into the extobj list last, when we know nothing can fail. */
dma_resv_lock(panthor_vm_resv(vm), NULL);
drm_gpuvm_bo_extobj_add(op_ctx->map.vm_bo);
dma_resv_unlock(panthor_vm_resv(vm));
/** * panthor_vm_get_bo_for_va() - Get the GEM object mapped at a virtual address * @vm: VM to look into. * @va: Virtual address to search for. * @bo_offset: Offset of the GEM object mapped at this virtual address. * Only valid on success. * * The object returned by this function might no longer be mapped when the * function returns. It's the caller responsibility to ensure there's no * concurrent map/unmap operations making the returned value invalid, or * make sure it doesn't matter if the object is no longer mapped. * * Return: A valid pointer on success, an ERR_PTR() otherwise.
*/ struct panthor_gem_object *
panthor_vm_get_bo_for_va(struct panthor_vm *vm, u64 va, u64 *bo_offset)
{ struct panthor_gem_object *bo = ERR_PTR(-ENOENT); struct drm_gpuva *gpuva; struct panthor_vma *vma;
/* Take the VM lock to prevent concurrent map/unmap operations. */
mutex_lock(&vm->op_lock);
gpuva = drm_gpuva_find_first(&vm->base, va, 1);
vma = gpuva ? container_of(gpuva, struct panthor_vma, base) : NULL; if (vma && vma->base.gem.obj) {
drm_gem_object_get(vma->base.gem.obj);
bo = to_panthor_bo(vma->base.gem.obj);
*bo_offset = vma->base.gem.offset + (va - vma->base.va.addr);
}
mutex_unlock(&vm->op_lock);
/* Make sure we have a minimum amount of VA space for kernel objects. */ if (full_va_range < PANTHOR_VM_MIN_KERNEL_VA_SIZE) return 0;
if (args->user_va_range) { /* Use the user provided value if != 0. */
user_va_range = args->user_va_range;
} elseif (TASK_SIZE_OF(current) < full_va_range) { /* If the task VM size is smaller than the GPU VA range, pick this * as our default user VA range, so userspace can CPU/GPU map buffers * at the same address.
*/
user_va_range = TASK_SIZE_OF(current);
} else { /* If the GPU VA range is smaller than the task VM size, we * just have to live with the fact we won't be able to map * all buffers at the same GPU/CPU address. * * If the GPU VA range is bigger than 4G (more than 32-bit of * VA), we split the range in two, and assign half of it to * the user and the other half to the kernel, if it's not, we * keep the kernel VA space as small as possible.
*/
user_va_range = full_va_range > SZ_4G ?
full_va_range / 2 :
full_va_range - PANTHOR_VM_MIN_KERNEL_VA_SIZE;
}
/* Pick a kernel VA range that's a power of two, to have a clear split. */
*kernel_va_range = rounddown_pow_of_two(full_va_range - user_va_range);
*kernel_va_start = full_va_range - *kernel_va_range; return 0;
}
/* * Only 32 VMs per open file. If that becomes a limiting factor, we can * increase this number.
*/ #define PANTHOR_MAX_VMS_PER_FILE 32
/** * panthor_vm_pool_create_vm() - Create a VM * @ptdev: The panthor device * @pool: The VM to create this VM on. * @args: VM creation args. * * Return: a positive VM ID on success, a negative error code otherwise.
*/ int panthor_vm_pool_create_vm(struct panthor_device *ptdev, struct panthor_vm_pool *pool, struct drm_panthor_vm_create *args)
{
u64 kernel_va_start, kernel_va_range; struct panthor_vm *vm; int ret;
u32 id;
ret = panthor_vm_create_check_args(ptdev, args, &kernel_va_start, &kernel_va_range); if (ret) return ret;
vm = panthor_vm_create(ptdev, false, kernel_va_start, kernel_va_range,
kernel_va_start, kernel_va_range); if (IS_ERR(vm)) return PTR_ERR(vm);
ret = xa_alloc(&pool->xa, &id, vm,
XA_LIMIT(1, PANTHOR_MAX_VMS_PER_FILE), GFP_KERNEL);
/** * panthor_vm_pool_destroy_vm() - Destroy a VM. * @pool: VM pool. * @handle: VM handle. * * This function doesn't free the VM object or its resources, it just kills * all mappings, and makes sure nothing can be mapped after that point. * * If there was any active jobs at the time this function is called, these * jobs should experience page faults and be killed as a result. * * The VM resources are freed when the last reference on the VM object is * dropped. * * Return: %0 for success, negative errno value for failure
*/ int panthor_vm_pool_destroy_vm(struct panthor_vm_pool *pool, u32 handle)
{ struct panthor_vm *vm;
vm = xa_erase(&pool->xa, handle);
panthor_vm_destroy(vm);
return vm ? 0 : -EINVAL;
}
/** * panthor_vm_pool_get_vm() - Retrieve VM object bound to a VM handle * @pool: VM pool to check. * @handle: Handle of the VM to retrieve. * * Return: A valid pointer if the VM exists, NULL otherwise.
*/ struct panthor_vm *
panthor_vm_pool_get_vm(struct panthor_vm_pool *pool, u32 handle)
{ struct panthor_vm *vm;
xa_lock(&pool->xa);
vm = panthor_vm_get(xa_load(&pool->xa, handle));
xa_unlock(&pool->xa);
return vm;
}
/** * panthor_vm_pool_destroy() - Destroy a VM pool. * @pfile: File. * * Destroy all VMs in the pool, and release the pool resources. * * Note that VMs can outlive the pool they were created from if other * objects hold a reference to there VMs.
*/ void panthor_vm_pool_destroy(struct panthor_file *pfile)
{ struct panthor_vm *vm; unsignedlong i;
if (!pfile->vms) return;
xa_for_each(&pfile->vms->xa, i, vm)
panthor_vm_destroy(vm);
xa_destroy(&pfile->vms->xa);
kfree(pfile->vms);
}
/** * panthor_vm_pool_create() - Create a VM pool * @pfile: File. * * Return: 0 on success, a negative error code otherwise.
*/ int panthor_vm_pool_create(struct panthor_file *pfile)
{
pfile->vms = kzalloc(sizeof(*pfile->vms), GFP_KERNEL); if (!pfile->vms) return -ENOMEM;
/* terminal fault, print info about the fault */
drm_err(&ptdev->base, "Unhandled Page fault in AS%d at VA 0x%016llX\n" "raw fault status: 0x%X\n" "decoded fault status: %s\n" "exception type 0x%X: %s\n" "access type 0x%X: %s\n" "source id 0x%X\n",
as, addr,
fault_status,
(fault_status & (1 << 10) ? "DECODER FAULT" : "SLAVE FAULT"),
exception_type, panthor_exception_name(ptdev, exception_type),
access_type, access_type_name(ptdev, fault_status),
source_id);
/* We don't handle VM faults at the moment, so let's just clear the * interrupt and let the writer/reader crash. * Note that COMPLETED irqs are never cleared, but this is fine * because they are always masked.
*/
gpu_write(ptdev, MMU_INT_CLEAR, mask);
/* Ignore MMU interrupts on this AS until it's been * re-enabled.
*/
ptdev->mmu->irq.mask = new_int_mask;
if (ptdev->mmu->as.slots[as].vm)
ptdev->mmu->as.slots[as].vm->unhandled_fault = true;
/* Disable the MMU to kill jobs on this AS. */
panthor_mmu_as_disable(ptdev, as);
mutex_unlock(&ptdev->mmu->as.slots_lock);
status &= ~mask;
has_unhandled_faults = true;
}
if (has_unhandled_faults)
panthor_sched_report_mmu_fault(ptdev);
}
PANTHOR_IRQ_HANDLER(mmu, MMU, panthor_mmu_irq_handler);
/** * panthor_mmu_suspend() - Suspend the MMU logic * @ptdev: Device. * * All we do here is de-assign the AS slots on all active VMs, so things * get flushed to the main memory, and no further access to these VMs are * possible. * * We also suspend the MMU IRQ.
*/ void panthor_mmu_suspend(struct panthor_device *ptdev)
{
mutex_lock(&ptdev->mmu->as.slots_lock); for (u32 i = 0; i < ARRAY_SIZE(ptdev->mmu->as.slots); i++) { struct panthor_vm *vm = ptdev->mmu->as.slots[i].vm;
if (vm) {
drm_WARN_ON(&ptdev->base, panthor_mmu_as_disable(ptdev, i));
panthor_vm_release_as_locked(vm);
}
}
mutex_unlock(&ptdev->mmu->as.slots_lock);
panthor_mmu_irq_suspend(&ptdev->mmu->irq);
}
/** * panthor_mmu_resume() - Resume the MMU logic * @ptdev: Device. * * Resume the IRQ. * * We don't re-enable previously active VMs. We assume other parts of the * driver will call panthor_vm_active() on the VMs they intend to use.
*/ void panthor_mmu_resume(struct panthor_device *ptdev)
{
mutex_lock(&ptdev->mmu->as.slots_lock);
ptdev->mmu->as.alloc_mask = 0;
ptdev->mmu->as.faulty_mask = 0;
mutex_unlock(&ptdev->mmu->as.slots_lock);
/** * panthor_mmu_pre_reset() - Prepare for a reset * @ptdev: Device. * * Suspend the IRQ, and make sure all VM_BIND queues are stopped, so we * don't get asked to do a VM operation while the GPU is down. * * We don't cleanly shutdown the AS slots here, because the reset might * come from an AS_ACTIVE_BIT stuck situation.
*/ void panthor_mmu_pre_reset(struct panthor_device *ptdev)
{ struct panthor_vm *vm;
/** * panthor_mmu_post_reset() - Restore things after a reset * @ptdev: Device. * * Put the MMU logic back in action after a reset. That implies resuming the * IRQ and re-enabling the VM_BIND queues.
*/ void panthor_mmu_post_reset(struct panthor_device *ptdev)
{ struct panthor_vm *vm;
mutex_lock(&ptdev->mmu->as.slots_lock);
/* Now that the reset is effective, we can assume that none of the * AS slots are setup, and clear the faulty flags too.
*/
ptdev->mmu->as.alloc_mask = 0;
ptdev->mmu->as.faulty_mask = 0;
for (u32 i = 0; i < ARRAY_SIZE(ptdev->mmu->as.slots); i++) { struct panthor_vm *vm = ptdev->mmu->as.slots[i].vm;
mutex_lock(&vm->heaps.lock); if (drm_WARN_ON(&ptdev->base, vm->heaps.pool))
panthor_heap_pool_destroy(vm->heaps.pool);
mutex_unlock(&vm->heaps.lock);
mutex_destroy(&vm->heaps.lock);
mutex_lock(&ptdev->mmu->vm.lock);
list_del(&vm->node); /* Restore the scheduler state so we can call drm_sched_entity_destroy() * and drm_sched_fini(). If get there, that means we have no job left * and no new jobs can be queued, so we can start the scheduler without * risking interfering with the reset.
*/ if (ptdev->mmu->vm.reset_in_progress)
panthor_vm_start(vm);
mutex_unlock(&ptdev->mmu->vm.lock);
/** * panthor_vm_put() - Release a reference on a VM * @vm: VM to release the reference on. Can be NULL.
*/ void panthor_vm_put(struct panthor_vm *vm)
{
drm_gpuvm_put(vm ? &vm->base : NULL);
}
/** * panthor_vm_get() - Get a VM reference * @vm: VM to get the reference on. Can be NULL. * * Return: @vm value.
*/ struct panthor_vm *panthor_vm_get(struct panthor_vm *vm)
{ if (vm)
drm_gpuvm_get(&vm->base);
return vm;
}
/** * panthor_vm_get_heap_pool() - Get the heap pool attached to a VM * @vm: VM to query the heap pool on. * @create: True if the heap pool should be created when it doesn't exist. * * Heap pools are per-VM. This function allows one to retrieve the heap pool * attached to a VM. * * If no heap pool exists yet, and @create is true, we create one. * * The returned panthor_heap_pool should be released with panthor_heap_pool_put(). * * Return: A valid pointer on success, an ERR_PTR() otherwise.
*/ struct panthor_heap_pool *panthor_vm_get_heap_pool(struct panthor_vm *vm, bool create)
{ struct panthor_heap_pool *pool;
mutex_lock(&vm->heaps.lock); if (!vm->heaps.pool && create) { if (vm->destroyed)
pool = ERR_PTR(-EINVAL); else
pool = panthor_heap_pool_create(vm->ptdev, vm);
if (!IS_ERR(pool))
vm->heaps.pool = panthor_heap_pool_get(pool);
} else {
pool = panthor_heap_pool_get(vm->heaps.pool); if (!pool)
pool = ERR_PTR(-ENOENT);
}
mutex_unlock(&vm->heaps.lock);
return pool;
}
/** * panthor_vm_heaps_sizes() - Calculate size of all heap chunks across all * heaps over all the heap pools in a VM * @pfile: File. * @stats: Memory stats to be updated. * * Calculate all heap chunk sizes in all heap pools bound to a VM. If the VM * is active, record the size as active as well.
*/ void panthor_vm_heaps_sizes(struct panthor_file *pfile, struct drm_memory_stats *stats)
{ struct panthor_vm *vm; unsignedlong i;
for (i = 0; i < 8; i++) {
u8 in_attr = mair >> (8 * i), out_attr;
u8 outer = in_attr >> 4, inner = in_attr & 0xf;
/* For caching to be enabled, inner and outer caching policy * have to be both write-back, if one of them is write-through * or non-cacheable, we just choose non-cacheable. Device * memory is also translated to non-cacheable.
*/ if (!(outer & 3) || !(outer & 4) || !(inner & 4)) {
out_attr = AS_MEMATTR_AARCH64_INNER_OUTER_NC |
AS_MEMATTR_AARCH64_SH_MIDGARD_INNER |
AS_MEMATTR_AARCH64_INNER_ALLOC_EXPL(false, false);
} else {
out_attr = AS_MEMATTR_AARCH64_INNER_OUTER_WB |
AS_MEMATTR_AARCH64_INNER_ALLOC_EXPL(inner & 1, inner & 2); /* Use SH_MIDGARD_INNER mode when device isn't coherent, * so SH_IS, which is used when IOMMU_CACHE is set, maps * to Mali's internal-shareable mode. As per the Mali * Spec, inner and outer-shareable modes aren't allowed * for WB memory when coherency is disabled. * Use SH_CPU_INNER mode when coherency is enabled, so * that SH_IS actually maps to the standard definition of * inner-shareable.
*/ if (!coherent)
out_attr |= AS_MEMATTR_AARCH64_SH_MIDGARD_INNER; else
out_attr |= AS_MEMATTR_AARCH64_SH_CPU_INNER;
}
/* drm_gpuva_unlink() release the vm_bo, but we manually retained it * when entering this function, so we can implement deferred VMA * destruction. Re-assign it here.
*/
vma->base.vm_bo = vm_bo;
list_add_tail(&vma->node, &vm->op_ctx->returned_vmas);
}
ret = panthor_vm_map_pages(vm, op->map.va.addr, flags_to_prot(vma->flags),
op_ctx->map.sgt, op->map.gem.offset,
op->map.va.range); if (ret) return ret;
/* Ref owned by the mapping now, clear the obj field so we don't release the * pinning/obj ref behind GPUVA's back.
*/
drm_gpuva_map(&vm->base, &vma->base, &op->map);
panthor_vma_link(vm, vma, op_ctx->map.vm_bo);
op_ctx->map.vm_bo = NULL; return 0;
}
if (prev_vma) { /* panthor_vma_link() transfers the vm_bo ownership to * the VMA object. Since the vm_bo we're passing is still * owned by the old mapping which will be released when this * mapping is destroyed, we need to grab a ref here.
*/
panthor_vma_link(vm, prev_vma,
drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo));
}
if (next_vma) {
panthor_vma_link(vm, next_vma,
drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo));
}
/** * panthor_vm_resv() - Get the dma_resv object attached to a VM. * @vm: VM to get the dma_resv of. * * Return: A dma_resv object.
*/ struct dma_resv *panthor_vm_resv(struct panthor_vm *vm)
{ return drm_gpuvm_resv(&vm->base);
}
struct drm_gem_object *panthor_vm_root_gem(struct panthor_vm *vm)
{ if (!vm) return NULL;
/* Not only we report an error whose result is propagated to the * drm_sched finished fence, but we also flag the VM as unusable, because * a failure in the async VM_BIND results in an inconsistent state. VM needs * to be destroyed and recreated.
*/
cookie = dma_fence_begin_signalling();
ret = panthor_vm_exec_op(job->vm, &job->ctx, true);
dma_fence_end_signalling(cookie);
/* Do the heavy cleanups asynchronously, so we're out of the * dma-signaling path and can acquire dma-resv locks safely.
*/
queue_work(panthor_cleanup_wq, &job->cleanup_op_ctx_work);
}
staticenum drm_gpu_sched_stat
panthor_vm_bind_timedout_job(struct drm_sched_job *sched_job)
{
WARN(1, "VM_BIND ops are synchronous for now, there should be no timeout!"); return DRM_GPU_SCHED_STAT_RESET;
}
/* If a reset is in progress, stop the scheduler. */ if (ptdev->mmu->vm.reset_in_progress)
panthor_vm_stop(vm);
mutex_unlock(&ptdev->mmu->vm.lock);
/* We intentionally leave the reserved range to zero, because we want kernel VMAs * to be handled the same way user VMAs are.
*/
drm_gpuvm_init(&vm->base, for_mcu ? "panthor-MCU-VM" : "panthor-GPU-VM",
DRM_GPUVM_RESV_PROTECTED, &ptdev->base, dummy_gem,
--> --------------------
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.