/* * for ASIC that supports setting the allocation page size by user we will address * user's choice only if it is not 0 (as 0 means taking the default page size)
*/ if (prop->supports_user_set_page_size && args->alloc.page_size) {
psize = args->alloc.page_size;
if (!is_power_of_2(psize)) {
dev_err(hdev->dev, "user page size (%#llx) is not power of 2\n", psize); return -EINVAL;
}
} else {
psize = prop->device_mem_alloc_default_page_size;
}
*page_size = psize;
return 0;
}
/* * The va ranges in context object contain a list with the available chunks of * device virtual memory. * There is one range for host allocations and one for DRAM allocations. * * On initialization each range contains one chunk of all of its available * virtual range which is a half of the total device virtual range. * * On each mapping of physical pages, a suitable virtual range chunk (with a * minimum size) is selected from the list. If the chunk size equals the * requested size, the chunk is returned. Otherwise, the chunk is split into * two chunks - one to return as result and a remainder to stay in the list. * * On each Unmapping of a virtual address, the relevant virtual chunk is * returned to the list. The chunk is added to the list and if its edges match * the edges of the adjacent chunks (means a contiguous chunk can be created), * the chunks are merged. * * On finish, the list is checked to have only one chunk of all the relevant * virtual range (which is a half of the device total virtual range). * If not (means not all mappings were unmapped), a warning is printed.
*/
/* * alloc_device_memory() - allocate device memory. * @ctx: pointer to the context structure. * @args: host parameters containing the requested size. * @ret_handle: result handle. * * This function does the following: * - Allocate the requested size rounded up to 'dram_page_size' pages. * - Return unique handle for later map/unmap/free.
*/ staticint alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
u32 *ret_handle)
{ struct hl_device *hdev = ctx->hdev; struct hl_vm *vm = &hdev->vm; struct hl_vm_phys_pg_pack *phys_pg_pack;
u64 paddr = 0, total_size, num_pgs, i;
u32 num_curr_pgs, page_size; bool contiguous; int handle, rc;
num_curr_pgs = 0;
rc = set_alloc_page_size(hdev, args, &page_size); if (rc) return rc;
idr_err:
page_err: if (!phys_pg_pack->contiguous) for (i = 0 ; i < num_curr_pgs ; i++)
gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[i],
page_size);
kvfree(phys_pg_pack->pages);
pages_arr_err:
kfree(phys_pg_pack);
pages_pack_err: if (contiguous)
gen_pool_free(vm->dram_pg_pool, paddr, total_size);
return rc;
}
/** * dma_map_host_va() - DMA mapping of the given host virtual address. * @hdev: habanalabs device structure. * @addr: the host virtual address of the memory area. * @size: the size of the memory area. * @p_userptr: pointer to result userptr structure. * * This function does the following: * - Allocate userptr structure. * - Pin the given host memory using the userptr structure. * - Perform DMA mapping to have the DMA addresses of the pages.
*/ staticint dma_map_host_va(struct hl_device *hdev, u64 addr, u64 size, struct hl_userptr **p_userptr)
{ struct hl_userptr *userptr; int rc;
/** * dma_unmap_host_va() - DMA unmapping of the given host virtual address. * @hdev: habanalabs device structure. * @userptr: userptr to free. * * This function does the following: * - Unpins the physical pages. * - Frees the userptr structure.
*/ staticvoid dma_unmap_host_va(struct hl_device *hdev, struct hl_userptr *userptr)
{
hl_unpin_host_memory(hdev, userptr);
kfree(userptr);
}
/** * dram_pg_pool_do_release() - free DRAM pages pool * @ref: pointer to reference object. * * This function does the following: * - Frees the idr structure of physical pages handles. * - Frees the generic pool of DRAM physical pages.
*/ staticvoid dram_pg_pool_do_release(struct kref *ref)
{ struct hl_vm *vm = container_of(ref, struct hl_vm,
dram_pg_pool_refcount);
/* * free the idr here as only here we know for sure that there are no * allocated physical pages and hence there are no handles in use
*/
idr_destroy(&vm->phys_pg_pack_handles);
gen_pool_destroy(vm->dram_pg_pool);
}
/** * free_phys_pg_pack() - free physical page pack. * @hdev: habanalabs device structure. * @phys_pg_pack: physical page pack to free. * * This function does the following: * - For DRAM memory only * - iterate over the pack, free each physical block structure by * returning it to the general pool. * - Free the hl_vm_phys_pg_pack structure.
*/ staticvoid free_phys_pg_pack(struct hl_device *hdev, struct hl_vm_phys_pg_pack *phys_pg_pack)
{ struct hl_vm *vm = &hdev->vm;
u64 i;
if (phys_pg_pack->created_from_userptr) goto end;
if (phys_pg_pack->contiguous) {
gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[0],
phys_pg_pack->total_size);
for (i = 0; i < phys_pg_pack->npages ; i++)
kref_put(&vm->dram_pg_pool_refcount,
dram_pg_pool_do_release);
} else { for (i = 0 ; i < phys_pg_pack->npages ; i++) {
gen_pool_free(vm->dram_pg_pool,
phys_pg_pack->pages[i],
phys_pg_pack->page_size);
kref_put(&vm->dram_pg_pool_refcount,
dram_pg_pool_do_release);
}
}
/** * free_device_memory() - free device memory. * @ctx: pointer to the context structure. * @args: host parameters containing the requested size. * * This function does the following: * - Free the device memory related to the given handle.
*/ staticint free_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args)
{ struct hl_device *hdev = ctx->hdev; struct hl_vm *vm = &hdev->vm; struct hl_vm_phys_pg_pack *phys_pg_pack;
u32 handle = args->free.handle;
spin_lock(&vm->idr_lock);
phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle); if (!phys_pg_pack) {
spin_unlock(&vm->idr_lock);
dev_err(hdev->dev, "free device memory failed, no match for handle %u\n", handle); return -EINVAL;
}
if (atomic_read(&phys_pg_pack->mapping_cnt) > 0) {
spin_unlock(&vm->idr_lock);
dev_err(hdev->dev, "handle %u is mapped, cannot free\n", handle); return -EINVAL;
}
/* must remove from idr before the freeing of the physical pages as the refcount of the pool * is also the trigger of the idr destroy
*/
idr_remove(&vm->phys_pg_pack_handles, handle);
spin_unlock(&vm->idr_lock);
/** * clear_va_list_locked() - free virtual addresses list. * @hdev: habanalabs device structure. * @va_list: list of virtual addresses to free. * * This function does the following: * - Iterate over the list and free each virtual addresses block. * * This function should be called only when va_list lock is taken.
*/ staticvoid clear_va_list_locked(struct hl_device *hdev, struct list_head *va_list)
{ struct hl_vm_va_block *va_block, *tmp;
/** * print_va_list_locked() - print virtual addresses list. * @hdev: habanalabs device structure. * @va_list: list of virtual addresses to print. * * This function does the following: * - Iterate over the list and print each virtual addresses block. * * This function should be called only when va_list lock is taken.
*/ staticvoid print_va_list_locked(struct hl_device *hdev, struct list_head *va_list)
{ #if HL_MMU_DEBUG struct hl_vm_va_block *va_block;
/** * merge_va_blocks_locked() - merge a virtual block if possible. * @hdev: pointer to the habanalabs device structure. * @va_list: pointer to the virtual addresses block list. * @va_block: virtual block to merge with adjacent blocks. * * This function does the following: * - Merge the given blocks with the adjacent blocks if their virtual ranges * create a contiguous virtual range. * * This Function should be called only when va_list lock is taken.
*/ staticvoid merge_va_blocks_locked(struct hl_device *hdev, struct list_head *va_list, struct hl_vm_va_block *va_block)
{ struct hl_vm_va_block *prev, *next;
/** * add_va_block_locked() - add a virtual block to the virtual addresses list. * @hdev: pointer to the habanalabs device structure. * @va_list: pointer to the virtual addresses block list. * @start: start virtual address. * @end: end virtual address. * * This function does the following: * - Add the given block to the virtual blocks list and merge with other blocks * if a contiguous virtual block can be created. * * This Function should be called only when va_list lock is taken.
*/ staticint add_va_block_locked(struct hl_device *hdev, struct list_head *va_list, u64 start, u64 end)
{ struct hl_vm_va_block *va_block, *res = NULL;
u64 size = end - start + 1;
print_va_list_locked(hdev, va_list);
list_for_each_entry(va_block, va_list, node) { /* TODO: remove upon matureness */ if (hl_mem_area_crosses_range(start, size, va_block->start,
va_block->end)) {
dev_err(hdev->dev, "block crossing ranges at start 0x%llx, end 0x%llx\n",
va_block->start, va_block->end); return -EINVAL;
}
if (va_block->end < start)
res = va_block;
}
va_block = kmalloc(sizeof(*va_block), GFP_KERNEL); if (!va_block) return -ENOMEM;
if (!res)
list_add(&va_block->node, va_list); else
list_add(&va_block->node, &res->node);
merge_va_blocks_locked(hdev, va_list, va_block);
print_va_list_locked(hdev, va_list);
return 0;
}
/** * add_va_block() - wrapper for add_va_block_locked. * @hdev: pointer to the habanalabs device structure. * @va_range: pointer to the virtual addresses range object. * @start: start virtual address. * @end: end virtual address. * * This function does the following: * - Takes the list lock and calls add_va_block_locked.
*/ staticinlineint add_va_block(struct hl_device *hdev, struct hl_va_range *va_range, u64 start, u64 end)
{ int rc;
/** * get_va_block() - get a virtual block for the given size and alignment. * * @hdev: pointer to the habanalabs device structure. * @va_range: pointer to the virtual addresses range. * @size: requested block size. * @hint_addr: hint for requested address by the user. * @va_block_align: required alignment of the virtual block start address. * @range_type: va range type (host, dram) * @flags: additional memory flags, currently only uses HL_MEM_FORCE_HINT * * This function does the following: * - Iterate on the virtual block list to find a suitable virtual block for the * given size, hint address and alignment. * - Reserve the requested block and update the list. * - Return the start address of the virtual block.
*/ static u64 get_va_block(struct hl_device *hdev, struct hl_va_range *va_range,
u64 size, u64 hint_addr, u32 va_block_align, enum hl_va_range_type range_type,
u32 flags)
{ struct hl_vm_va_block *va_block, *new_va_block = NULL; struct asic_fixed_properties *prop = &hdev->asic_prop;
u64 tmp_hint_addr, valid_start, valid_size, prev_start, prev_end,
align_mask, reserved_valid_start = 0, reserved_valid_size = 0,
dram_hint_mask = prop->dram_hints_align_mask; bool add_prev = false; bool is_align_pow_2 = is_power_of_2(va_range->page_size); bool is_hint_dram_addr = hl_is_dram_va(hdev, hint_addr); bool force_hint = flags & HL_MEM_FORCE_HINT; int rc;
if (is_align_pow_2)
align_mask = ~((u64)va_block_align - 1); else /* * with non-power-of-2 range we work only with page granularity * and the start address is page aligned, * so no need for alignment checking.
*/
size = DIV_ROUND_UP_ULL(size, va_range->page_size) *
va_range->page_size;
tmp_hint_addr = hint_addr & ~dram_hint_mask;
/* Check if we need to ignore hint address */ if ((is_align_pow_2 && (hint_addr & (va_block_align - 1))) ||
(!is_align_pow_2 && is_hint_dram_addr &&
do_div(tmp_hint_addr, va_range->page_size))) {
if (force_hint) { /* Hint must be respected, so here we just fail */
dev_err(hdev->dev, "Hint address 0x%llx is not page aligned - cannot be respected\n",
hint_addr); return 0;
}
dev_dbg(hdev->dev, "Hint address 0x%llx will be ignored because it is not aligned\n",
hint_addr);
hint_addr = 0;
}
mutex_lock(&va_range->lock);
print_va_list_locked(hdev, &va_range->list);
list_for_each_entry(va_block, &va_range->list, node) { /* Calc the first possible aligned addr */
valid_start = va_block->start;
/* * In case hint address is 0, and hints_range_reservation * property enabled, then avoid allocating va blocks from the * range reserved for hint addresses
*/ if (prop->hints_range_reservation && !hint_addr) if (is_hint_crossing_range(range_type, valid_start,
size, prop)) continue;
/* Pick the minimal length block which has the required size */ if (!new_va_block || (valid_size < reserved_valid_size)) {
new_va_block = va_block;
reserved_valid_start = valid_start;
reserved_valid_size = valid_size;
}
if (!new_va_block) {
dev_err(hdev->dev, "no available va block for size %llu\n",
size); goto out;
}
if (force_hint && reserved_valid_start != hint_addr) { /* Hint address must be respected. If we are here - this means * we could not respect it.
*/
dev_err(hdev->dev, "Hint address 0x%llx could not be respected\n",
hint_addr);
reserved_valid_start = 0; goto out;
}
/* * Check if there is some leftover range due to reserving the new * va block, then return it to the main virtual addresses list.
*/ if (reserved_valid_start > new_va_block->start) {
prev_start = new_va_block->start;
prev_end = reserved_valid_start - 1;
/* * hl_reserve_va_block() - reserve a virtual block of a given size. * @hdev: pointer to the habanalabs device structure. * @ctx: current context * @type: virtual addresses range type. * @size: requested block size. * @alignment: required alignment in bytes of the virtual block start address, * 0 means no alignment. * * This function does the following: * - Iterate on the virtual block list to find a suitable virtual block for the * given size and alignment. * - Reserve the requested block and update the list. * - Return the start address of the virtual block.
*/
u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx, enum hl_va_range_type type, u64 size, u32 alignment)
{ return get_va_block(hdev, ctx->va_range[type], size, 0,
max(alignment, ctx->va_range[type]->page_size),
type, 0);
}
/** * hl_get_va_range_type() - get va_range type for the given address and size. * @ctx: context to fetch va_range from. * @address: the start address of the area we want to validate. * @size: the size in bytes of the area we want to validate. * @type: returned va_range type. * * Return: true if the area is inside a valid range, false otherwise.
*/ staticint hl_get_va_range_type(struct hl_ctx *ctx, u64 address, u64 size, enum hl_va_range_type *type)
{ int i;
for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX; i++) { if (hl_mem_area_inside_range(address, size,
ctx->va_range[i]->start_addr,
ctx->va_range[i]->end_addr)) {
*type = i; return 0;
}
}
return -EINVAL;
}
/** * hl_unreserve_va_block() - wrapper for add_va_block to unreserve a va block. * @hdev: pointer to the habanalabs device structure * @ctx: pointer to the context structure. * @start_addr: start virtual address. * @size: number of bytes to unreserve. * * This function does the following: * - Takes the list lock and calls add_va_block_locked.
*/ int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
u64 start_addr, u64 size)
{ enum hl_va_range_type type; int rc;
rc = hl_get_va_range_type(ctx, start_addr, size, &type); if (rc) {
dev_err(hdev->dev, "cannot find va_range for va %#llx size %llu",
start_addr, size); return rc;
}
rc = add_va_block(hdev, ctx->va_range[type], start_addr,
start_addr + size - 1); if (rc)
dev_warn(hdev->dev, "add va block failed for vaddr: 0x%llx\n", start_addr);
return rc;
}
/** * init_phys_pg_pack_from_userptr() - initialize physical page pack from host * memory * @ctx: pointer to the context structure. * @userptr: userptr to initialize from. * @pphys_pg_pack: result pointer. * @force_regular_page: tell the function to ignore huge page optimization, * even if possible. Needed for cases where the device VA * is allocated before we know the composition of the * physical pages * * This function does the following: * - Create a physical page pack from the physical pages related to the given * virtual block.
*/ staticint init_phys_pg_pack_from_userptr(struct hl_ctx *ctx, struct hl_userptr *userptr, struct hl_vm_phys_pg_pack **pphys_pg_pack, bool force_regular_page)
{
u32 npages, page_size = PAGE_SIZE,
huge_page_size = ctx->hdev->asic_prop.pmmu_huge.page_size;
u32 pgs_in_huge_page = huge_page_size >> __ffs(page_size); struct hl_vm_phys_pg_pack *phys_pg_pack; bool first = true, is_huge_page_opt;
u64 page_mask, total_npages; struct scatterlist *sg;
dma_addr_t dma_addr; int rc, i, j;
phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL); if (!phys_pg_pack) return -ENOMEM;
/* Only if all dma_addrs are aligned to 2MB and their * sizes is at least 2MB, we can use huge page mapping. * We limit the 2MB optimization to this condition, * since later on we acquire the related VA range as one * consecutive block.
*/
total_npages = 0;
for_each_sgtable_dma_sg(userptr->sgt, sg, i) {
npages = hl_get_sg_info(sg, &dma_addr);
/* align down to physical page size and save the offset */ if (first) {
first = false;
phys_pg_pack->offset = dma_addr & (page_size - 1);
dma_addr &= page_mask;
}
while (npages) {
phys_pg_pack->pages[j++] = dma_addr;
dma_addr += page_size;
if (is_huge_page_opt)
npages -= pgs_in_huge_page; else
npages--;
}
}
*pphys_pg_pack = phys_pg_pack;
return 0;
page_pack_arr_mem_err:
kfree(phys_pg_pack);
return rc;
}
/** * map_phys_pg_pack() - maps the physical page pack.. * @ctx: pointer to the context structure. * @vaddr: start address of the virtual area to map from. * @phys_pg_pack: the pack of physical pages to map to. * * This function does the following: * - Maps each chunk of virtual memory to matching physical chunk. * - Stores number of successful mappings in the given argument. * - Returns 0 on success, error code otherwise.
*/ staticint map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr, struct hl_vm_phys_pg_pack *phys_pg_pack)
{ struct hl_device *hdev = ctx->hdev;
u64 next_vaddr = vaddr, paddr, mapped_pg_cnt = 0, i;
u32 page_size = phys_pg_pack->page_size; int rc = 0; bool is_host_addr;
for (i = 0 ; i < phys_pg_pack->npages ; i++) {
paddr = phys_pg_pack->pages[i];
next_vaddr = vaddr; for (i = 0 ; i < mapped_pg_cnt ; i++) { if (hl_mmu_unmap_page(ctx, next_vaddr, page_size,
(i + 1) == mapped_pg_cnt))
dev_warn_ratelimited(hdev->dev, "failed to unmap handle %u, va: 0x%llx, pa: 0x%llx, page size: %u\n",
phys_pg_pack->handle, next_vaddr,
phys_pg_pack->pages[i], page_size);
next_vaddr += page_size;
/* * unmapping on Palladium can be really long, so avoid a CPU * soft lockup bug by sleeping a little between unmapping pages * * In addition, on host num of pages could be huge, * because page size could be 4KB, so when unmapping host * pages sleep every 32K pages to avoid soft lockup
*/ if (hdev->pldm || (is_host_addr && (i & 0x7FFF) == 0))
usleep_range(50, 200);
}
return rc;
}
/** * unmap_phys_pg_pack() - unmaps the physical page pack. * @ctx: pointer to the context structure. * @vaddr: start address of the virtual area to unmap. * @phys_pg_pack: the pack of physical pages to unmap.
*/ staticvoid unmap_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr, struct hl_vm_phys_pg_pack *phys_pg_pack)
{ struct hl_device *hdev = ctx->hdev;
u64 next_vaddr, i; bool is_host_addr;
u32 page_size;
for (i = 0 ; i < phys_pg_pack->npages ; i++, next_vaddr += page_size) { if (hl_mmu_unmap_page(ctx, next_vaddr, page_size,
(i + 1) == phys_pg_pack->npages))
dev_warn_ratelimited(hdev->dev, "unmap failed for vaddr: 0x%llx\n", next_vaddr);
/* * unmapping on Palladium can be really long, so avoid a CPU * soft lockup bug by sleeping a little between unmapping pages * * In addition, on host num of pages could be huge, * because page size could be 4KB, so when unmapping host * pages sleep every 32K pages to avoid soft lockup
*/ if (hdev->pldm || (is_host_addr && (i & 0x7FFF) == 0))
usleep_range(50, 200);
}
}
/** * map_device_va() - map the given memory. * @ctx: pointer to the context structure. * @args: host parameters with handle/host virtual address. * @device_addr: pointer to result device virtual address. * * This function does the following: * - If given a physical device memory handle, map to a device virtual block * and return the start address of this block. * - If given a host virtual address and size, find the related physical pages, * map a device virtual block to this pages and return the start address of * this block.
*/ staticint map_device_va(struct hl_ctx *ctx, struct hl_mem_in *args, u64 *device_addr)
{ struct hl_vm_phys_pg_pack *phys_pg_pack; enum hl_va_range_type va_range_type = 0; struct hl_device *hdev = ctx->hdev; struct hl_userptr *userptr = NULL;
u32 handle = 0, va_block_align; struct hl_vm_hash_node *hnode; struct hl_vm *vm = &hdev->vm; struct hl_va_range *va_range; bool is_userptr, do_prefetch;
u64 ret_vaddr, hint_addr; enum vm_type *vm_type; int rc;
/* get required alignment */ if (phys_pg_pack->page_size == page_size) {
va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST];
va_range_type = HL_VA_RANGE_TYPE_HOST; /* * huge page alignment may be needed in case of regular * page mapping, depending on the host VA alignment
*/ if (addr & (huge_page_size - 1))
va_block_align = page_size; else
va_block_align = huge_page_size;
} else { /* * huge page alignment is needed in case of huge page * mapping
*/
va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE];
va_range_type = HL_VA_RANGE_TYPE_HOST_HUGE;
va_block_align = huge_page_size;
}
} else {
handle = lower_32_bits(args->map_device.handle);
spin_lock(&vm->idr_lock);
phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle); if (!phys_pg_pack) {
spin_unlock(&vm->idr_lock);
dev_err(hdev->dev, "no match for handle %u\n", handle); return -EINVAL;
}
/* increment now to avoid freeing device memory while mapping */
atomic_inc(&phys_pg_pack->mapping_cnt);
spin_unlock(&vm->idr_lock);
vm_type = (enum vm_type *) phys_pg_pack;
hint_addr = args->map_device.hint_addr;
/* DRAM VA alignment is the same as the MMU page size */
va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM];
va_range_type = HL_VA_RANGE_TYPE_DRAM;
va_block_align = hdev->asic_prop.dmmu.page_size;
}
/* * relevant for mapping device physical memory only, as host memory is * implicitly shared
*/ if (!is_userptr && !(phys_pg_pack->flags & HL_MEM_SHARED) &&
phys_pg_pack->asid != ctx->asid) {
dev_err(hdev->dev, "Failed to map memory, handle %u is not shared\n",
handle);
rc = -EPERM; goto shared_err;
}
if (hint_addr && phys_pg_pack->offset) { if (args->flags & HL_MEM_FORCE_HINT) { /* Fail if hint must be respected but it can't be */
dev_err(hdev->dev, "Hint address 0x%llx cannot be respected because source memory is not aligned 0x%x\n",
hint_addr, phys_pg_pack->offset);
rc = -EINVAL; goto va_block_err;
}
dev_dbg(hdev->dev, "Hint address 0x%llx will be ignored because source memory is not aligned 0x%x\n",
hint_addr, phys_pg_pack->offset);
}
ret_vaddr = get_va_block(hdev, va_range, phys_pg_pack->total_size,
hint_addr, va_block_align,
va_range_type, args->flags); if (!ret_vaddr) {
dev_err(hdev->dev, "no available va block for handle %u\n",
handle);
rc = -ENOMEM; goto va_block_err;
}
/* * prefetch is done upon user's request. it is performed in WQ as and so can * be outside the MMU lock. the operation itself is already protected by the mmu lock
*/ if (do_prefetch) {
rc = hl_mmu_prefetch_cache_range(ctx, *vm_type, ctx->asid, ret_vaddr,
phys_pg_pack->total_size); if (rc) goto map_err;
}
if (is_userptr)
free_phys_pg_pack(hdev, phys_pg_pack);
return rc;
map_err: if (add_va_block(hdev, va_range, ret_vaddr,
ret_vaddr + phys_pg_pack->total_size - 1))
dev_warn(hdev->dev, "release va block failed for handle 0x%x, vaddr: 0x%llx\n",
handle, ret_vaddr);
va_block_err:
kfree(hnode);
hnode_err:
shared_err:
atomic_dec(&phys_pg_pack->mapping_cnt); if (is_userptr)
free_phys_pg_pack(hdev, phys_pg_pack);
init_page_pack_err: if (is_userptr)
dma_unmap_host_va(hdev, userptr);
return rc;
}
/* Should be called while the context's mem_hash_lock is taken */ staticstruct hl_vm_hash_node *get_vm_hash_node_locked(struct hl_ctx *ctx, u64 vaddr)
{ struct hl_vm_hash_node *hnode;
hash_for_each_possible(ctx->mem_hash, hnode, node, vaddr) if (vaddr == hnode->vaddr) return hnode;
return NULL;
}
/** * unmap_device_va() - unmap the given device virtual address. * @ctx: pointer to the context structure. * @args: host parameters with device virtual address to unmap. * @ctx_free: true if in context free flow, false otherwise. * * This function does the following: * - unmap the physical pages related to the given virtual address. * - return the device virtual block to the virtual block list.
*/ staticint unmap_device_va(struct hl_ctx *ctx, struct hl_mem_in *args, bool ctx_free)
{ struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
u64 vaddr = args->unmap.device_virt_addr; struct asic_fixed_properties *prop; struct hl_device *hdev = ctx->hdev; struct hl_userptr *userptr = NULL; struct hl_vm_hash_node *hnode; struct hl_va_range *va_range; enum vm_type *vm_type; bool is_userptr; int rc = 0;
prop = &hdev->asic_prop;
/* protect from double entrance */
mutex_lock(&ctx->mem_hash_lock);
hnode = get_vm_hash_node_locked(ctx, vaddr); if (!hnode) {
mutex_unlock(&ctx->mem_hash_lock);
dev_err(hdev->dev, "unmap failed, no mem hnode for vaddr 0x%llx\n", vaddr); return -EINVAL;
}
if (hnode->export_cnt) {
mutex_unlock(&ctx->mem_hash_lock);
dev_err(hdev->dev, "failed to unmap %#llx, memory is exported\n", vaddr); return -EINVAL;
}
/* * During context free this function is called in a loop to clean all * the context mappings. Hence the cache invalidation can be called once * at the loop end rather than for each iteration
*/ if (!ctx_free)
rc = hl_mmu_invalidate_cache_range(hdev, true, *vm_type, ctx->asid, vaddr,
phys_pg_pack->total_size);
mutex_unlock(&hdev->mmu_lock);
/* * If the context is closing we don't need to check for the MMU cache * invalidation return code and update the VA free list as in this flow * we invalidate the MMU cache outside of this unmap function and the VA * free list will be freed anyway.
*/ if (!ctx_free) { int tmp_rc;
tmp_rc = add_va_block(hdev, va_range, vaddr,
vaddr + phys_pg_pack->total_size - 1); if (tmp_rc) {
dev_warn(hdev->dev, "add va block failed for vaddr: 0x%llx\n",
vaddr); if (!rc)
rc = tmp_rc;
}
}
/** * hl_hw_block_mmap() - mmap a hw block to user. * @hpriv: pointer to the private data of the fd * @vma: pointer to vm_area_struct of the process * * Driver increments context reference for every HW block mapped in order * to prevent user from closing FD without unmapping first
*/ int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
{ struct hl_vm_hw_block_list_node *lnode; struct hl_device *hdev = hpriv->hdev; struct hl_ctx *ctx = hpriv->ctx;
u32 block_id, block_size; int rc;
/* We use the page offset to hold the block id and thus we need to clear * it before doing the mmap itself
*/
block_id = vma->vm_pgoff;
vma->vm_pgoff = 0;
/* Driver only allows mapping of a complete HW block */
block_size = vma->vm_end - vma->vm_start;
if (!access_ok((void __user *) (uintptr_t) vma->vm_start, block_size)) {
dev_err(hdev->dev, "user pointer is invalid - 0x%lx\n",
vma->vm_start);
return -EINVAL;
}
lnode = kzalloc(sizeof(*lnode), GFP_KERNEL); if (!lnode) return -ENOMEM;
/* Align max segment size to PAGE_SIZE to fit the minimal IOMMU mapping granularity */
dma_max_seg_size = ALIGN_DOWN(dma_get_max_seg_size(dev), PAGE_SIZE); if (dma_max_seg_size < PAGE_SIZE) {
dev_err_ratelimited(hdev->dev, "dma_max_seg_size %llu can't be smaller than PAGE_SIZE\n",
dma_max_seg_size); return ERR_PTR(-EINVAL);
}
sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); if (!sgt) return ERR_PTR(-ENOMEM);
/* Use the offset to move to the actual first page that is exported */ for (start_page = 0 ; start_page < npages ; ++start_page) { if (offset < page_size) break;
/* The offset value was validated so there can't be an underflow */
offset -= page_size;
}
/* Calculate the required number of entries for the SG table */
curr_page = start_page;
nents = 1;
left_size_to_export = exported_size;
left_size_in_page = page_size - offset;
left_size_in_dma_seg = dma_max_seg_size;
next_sg_entry = false;
if (!left_size_in_page) { /* left_size_to_export is not zero so there must be another page */ if (pages[curr_page] + page_size != pages[curr_page + 1])
next_sg_entry = true;
++curr_page;
left_size_in_page = page_size;
}
if (!left_size_in_dma_seg) {
next_sg_entry = true;
left_size_in_dma_seg = dma_max_seg_size;
}
if (next_sg_entry) {
++nents;
next_sg_entry = false;
}
}
if (!left_size_in_page) { /* left_size_to_export is not zero so there must be another page */ if (pages[curr_page] + page_size != pages[curr_page + 1]) {
device_address = pages[curr_page + 1];
next_sg_entry = true;
}
left_size_in_page = page_size;
}
if (!left_size_in_dma_seg) { /* * Skip setting a new device address if already moving to a page * which is not contiguous with the current page.
*/ if (!next_sg_entry) {
device_address += chunk_size;
next_sg_entry = true;
}
left_size_in_dma_seg = dma_max_seg_size;
}
if (next_sg_entry) {
next_sg_entry = false; break;
}
}
/* There should be nothing left to export exactly after looping over all SG elements */ if (left_size_to_export) {
dev_err(hdev->dev, "left size to export %#llx after initializing %u SG elements\n",
left_size_to_export, sgt->nents);
rc = -ENOMEM; goto err_unmap;
}
/* * Because we are not going to include a CPU list, we want to have some chance that other * users will detect this when going over SG table, by setting the orig_nents to 0 and using * only nents (length of DMA list).
*/
sgt->orig_nents = 0;
dev_dbg(hdev->dev, "prepared SG table with %u entries for importer %s\n",
nents, dev_name(dev));
for_each_sgtable_dma_sg(sgt, sg, i)
dev_dbg(hdev->dev, "SG entry %d: address %#llx, length %#x\n",
i, sg_dma_address(sg), sg_dma_len(sg));
return sgt;
err_unmap:
for_each_sgtable_dma_sg(sgt, sg, i) { if (!sg_dma_len(sg)) continue;
/* The memory behind the dma-buf has *always* resided on the device itself, i.e. it lives * only in the 'device' domain (after all, it maps a PCI bar address which points to the * device memory). * * Therefore, it was never in the 'CPU' domain and hence, there is no need to perform * a sync of the memory to the CPU's cache, as it never resided inside that cache.
*/
for_each_sgtable_dma_sg(sgt, sg, i)
dma_unmap_resource(attachment->dev, sg_dma_address(sg),
sg_dma_len(sg), dir,
DMA_ATTR_SKIP_CPU_SYNC);
/* Need to restore orig_nents because sg_free_table use that field */
sgt->orig_nents = sgt->nents;
sg_free_table(sgt);
kfree(sgt);
}
/* get the memory handle */
mutex_lock(&ctx->mem_hash_lock);
hnode = get_vm_hash_node_locked(ctx, addr); if (!hnode) {
mutex_unlock(&ctx->mem_hash_lock);
dev_dbg(hdev->dev, "map address %#llx not found\n", addr); return ERR_PTR(-EINVAL);
}
if (upper_32_bits(hnode->handle)) {
mutex_unlock(&ctx->mem_hash_lock);
dev_dbg(hdev->dev, "invalid handle %#llx for map address %#llx\n",
hnode->handle, addr); return ERR_PTR(-EINVAL);
}
/* * node found, increase export count so this memory cannot be unmapped * and the hash node cannot be deleted.
*/
hnode->export_cnt++;
mutex_unlock(&ctx->mem_hash_lock);
/* Get compute device file to enforce release order, such that all exported dma-buf will be * released first and only then the compute device. * Paired with fput() in hl_release_dmabuf().
*/
get_file(ctx->hpriv->file_priv->filp);
staticint validate_export_params_common(struct hl_device *hdev, u64 addr, u64 size, u64 offset)
{ if (!PAGE_ALIGNED(addr)) {
dev_dbg(hdev->dev, "exported device memory address 0x%llx should be aligned to PAGE_SIZE 0x%lx\n",
addr, PAGE_SIZE); return -EINVAL;
}
if (!size || !PAGE_ALIGNED(size)) {
dev_dbg(hdev->dev, "exported device memory size %llu should be a multiple of PAGE_SIZE %lu\n",
size, PAGE_SIZE); return -EINVAL;
}
if (!PAGE_ALIGNED(offset)) {
dev_dbg(hdev->dev, "exported device memory offset %llu should be a multiple of PAGE_SIZE %lu\n",
offset, PAGE_SIZE); return -EINVAL;
}
spin_lock(&vm->idr_lock);
phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, (u32) hnode->handle); if (!phys_pg_pack) {
spin_unlock(&vm->idr_lock);
dev_dbg(hdev->dev, "no match for handle 0x%x\n", (u32) hnode->handle); return ERR_PTR(-EINVAL);
}
spin_unlock(&vm->idr_lock);
if (phys_pg_pack->vm_type != VM_TYPE_PHYS_PACK) {
dev_dbg(hdev->dev, "handle 0x%llx does not represent DRAM memory\n", hnode->handle); return ERR_PTR(-EINVAL);
}
return phys_pg_pack;
}
/** * export_dmabuf_from_addr() - export a dma-buf object for the given memory * address and size. * @ctx: pointer to the context structure. * @addr: device address. * @size: size of device memory to export. * @offset: the offset into the buffer from which to start exporting * @flags: DMA-BUF file/FD flags. * @dmabuf_fd: pointer to result FD that represents the dma-buf object. * * Create and export a dma-buf object for an existing memory allocation inside * the device memory, and return a FD which is associated with the dma-buf * object. * * Return: 0 on success, non-zero for failure.
*/ staticint export_dmabuf_from_addr(struct hl_ctx *ctx, u64 addr, u64 size, u64 offset, int flags, int *dmabuf_fd)
{ struct hl_vm_phys_pg_pack *phys_pg_pack = NULL; struct hl_vm_hash_node *hnode = NULL; struct asic_fixed_properties *prop; struct hl_dmabuf_priv *hl_dmabuf; struct hl_device *hdev; int rc;
hdev = ctx->hdev;
prop = &hdev->asic_prop;
/* offset must be 0 in devices without virtual memory support */ if (!prop->dram_supports_virtual_memory && offset) {
dev_dbg(hdev->dev, "offset is not allowed in device without virtual memory\n"); return -EINVAL;
}
hl_dmabuf = kzalloc(sizeof(*hl_dmabuf), GFP_KERNEL); if (!hl_dmabuf) return -ENOMEM;
if (prop->dram_supports_virtual_memory) {
hnode = memhash_node_export_get(ctx, addr); if (IS_ERR(hnode)) {
rc = PTR_ERR(hnode); goto err_free_dmabuf_wrapper;
}
phys_pg_pack = get_phys_pg_pack_from_hash_node(hdev, hnode); if (IS_ERR(phys_pg_pack)) {
rc = PTR_ERR(phys_pg_pack); goto dec_memhash_export_cnt;
}
rc = validate_export_params(hdev, addr, size, offset, phys_pg_pack); if (rc) goto dec_memhash_export_cnt;
/** * allocate_timestamps_buffers() - allocate timestamps buffers * This function will allocate ts buffer that will later on be mapped to the user * in order to be able to read the timestamp. * in addition it'll allocate an extra buffer for registration management. * since we cannot fail during registration for out-of-memory situation, so * we'll prepare a pool which will be used as user interrupt nodes and instead * of dynamically allocating nodes while registration we'll pick the node from * this pool. in addition it'll add node to the mapping hash which will be used * to map user ts buffer to the internal kernel ts buffer. * @hpriv: pointer to the private data of the fd * @args: ioctl input * @handle: user timestamp buffer handle as an output
*/ staticint allocate_timestamps_buffers(struct hl_fpriv *hpriv, struct hl_mem_in *args, u64 *handle)
{ struct hl_mem_mgr *mmg = &hpriv->mem_mgr; struct hl_mmap_mem_buf *buf;
if (args->num_of_elements > TS_MAX_ELEMENTS_NUM) {
dev_err(mmg->dev, "Num of elements exceeds Max allowed number (0x%x > 0x%x)\n",
args->num_of_elements, TS_MAX_ELEMENTS_NUM); return -EINVAL;
}
buf = hl_mmap_mem_buf_alloc(mmg, &hl_ts_behavior, GFP_KERNEL, &args->num_of_elements); if (!buf) return -ENOMEM;
if (!hl_device_operational(hdev, &status)) {
dev_dbg_ratelimited(hdev->dev, "Device is %s. Can't execute MEMORY IOCTL\n",
hdev->status[status]); return -EBUSY;
}
switch (args->in.op) { case HL_MEM_OP_ALLOC: if (args->in.alloc.mem_size == 0) {
dev_err(hdev->dev, "alloc size must be larger than 0\n");
rc = -EINVAL; goto out;
}
/* If DRAM does not support virtual memory the driver won't * handle the allocation/freeing of that memory. However, for * system administration/monitoring purposes, the driver will * keep track of the amount of DRAM memory that is allocated * and freed by the user. Because this code totally relies on * the user's input, the driver can't ensure the validity * of this accounting.
*/ if (!hdev->asic_prop.dram_supports_virtual_memory) {
atomic64_add(args->in.alloc.mem_size,
&ctx->dram_phys_mem);
atomic64_add(args->in.alloc.mem_size,
&hdev->dram_used_mem);
dev_dbg(hdev->dev, "DRAM alloc is not supported\n");
rc = 0;
case HL_MEM_OP_FREE: /* If DRAM does not support virtual memory the driver won't * handle the allocation/freeing of that memory. However, for * system administration/monitoring purposes, the driver will * keep track of the amount of DRAM memory that is allocated * and freed by the user. Because this code totally relies on * the user's input, the driver can't ensure the validity * of this accounting.
*/ if (!hdev->asic_prop.dram_supports_virtual_memory) {
atomic64_sub(args->in.alloc.mem_size,
&ctx->dram_phys_mem);
atomic64_sub(args->in.alloc.mem_size,
&hdev->dram_used_mem);
dev_dbg(hdev->dev, "DRAM alloc is not supported\n");
rc = 0;
goto out;
}
rc = free_device_memory(ctx, &args->in); break;
case HL_MEM_OP_MAP:
rc = map_device_va(ctx, &args->in, &device_addr);
/** * hl_pin_host_memory() - pins a chunk of host memory. * @hdev: pointer to the habanalabs device structure. * @addr: the host virtual address of the memory area. * @size: the size of the memory area. * @userptr: pointer to hl_userptr structure. * * This function does the following: * - Pins the physical pages. * - Create an SG list from those pages.
*/ int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size, struct hl_userptr *userptr)
{
u64 start, end;
u32 npages, offset; int rc;
if (!size) {
dev_err(hdev->dev, "size to pin is invalid - %llu\n", size); return -EINVAL;
}
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.