Lines Matching refs:ctx
44 * @ctx : current context
52 static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
55 struct hl_device *hdev = ctx->hdev;
93 phys_pg_pack->asid = ctx->asid;
141 atomic64_add(phys_pg_pack->total_size, &ctx->dram_phys_mem);
299 * @ctx : current context
305 static int free_device_memory(struct hl_ctx *ctx, u32 handle)
307 struct hl_device *hdev = ctx->hdev;
329 atomic64_sub(phys_pg_pack->total_size, &ctx->dram_phys_mem);
624 * @ctx: current context
633 static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx,
642 huge_page_size = ctx->hdev->asic_prop.pmmu_huge.page_size;
653 phys_pg_pack->asid = ctx->asid;
725 * @ctx: current context
734 static int map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
737 struct hl_device *hdev = ctx->hdev;
745 rc = hl_mmu_map(ctx, next_vaddr, paddr, page_size,
764 if (hl_mmu_unmap(ctx, next_vaddr, page_size,
779 * @ctx: current context
783 static void unmap_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
786 struct hl_device *hdev = ctx->hdev;
794 if (hl_mmu_unmap(ctx, next_vaddr, page_size,
808 static int get_paddr_from_handle(struct hl_ctx *ctx, struct hl_mem_in *args,
811 struct hl_device *hdev = ctx->hdev;
835 * @ctx : current context
846 static int map_device_va(struct hl_ctx *ctx, struct hl_mem_in *args,
849 struct hl_device *hdev = ctx->hdev;
876 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
891 va_range = ctx->host_va_range;
906 va_range = ctx->host_huge_va_range;
931 va_range = ctx->dram_va_range;
940 phys_pg_pack->asid != ctx->asid) {
963 mutex_lock(&ctx->mmu_lock);
965 rc = map_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack);
967 mutex_unlock(&ctx->mmu_lock);
975 mutex_unlock(&ctx->mmu_lock);
989 mutex_lock(&ctx->mem_hash_lock);
990 hash_add(ctx->mem_hash, &hnode->node, ret_vaddr);
991 mutex_unlock(&ctx->mem_hash_lock);
1024 * @ctx : current context
1032 static int unmap_device_va(struct hl_ctx *ctx, u64 vaddr, bool ctx_free)
1034 struct hl_device *hdev = ctx->hdev;
1044 mutex_lock(&ctx->mem_hash_lock);
1045 hash_for_each_possible(ctx->mem_hash, hnode, node, (unsigned long)vaddr)
1050 mutex_unlock(&ctx->mem_hash_lock);
1058 mutex_unlock(&ctx->mem_hash_lock);
1065 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
1076 va_range = ctx->host_va_range;
1078 va_range = ctx->host_huge_va_range;
1081 va_range = ctx->dram_va_range;
1099 mutex_lock(&ctx->mmu_lock);
1101 unmap_phys_pg_pack(ctx, vaddr, phys_pg_pack);
1112 mutex_unlock(&ctx->mmu_lock);
1153 mutex_lock(&ctx->mem_hash_lock);
1154 hash_add(ctx->mem_hash, &hnode->node, vaddr);
1155 mutex_unlock(&ctx->mem_hash_lock);
1163 struct hl_ctx *ctx = hpriv->ctx;
1181 rc = alloc_device_memory(ctx, &args->in, &handle);
1188 rc = free_device_memory(ctx, args->in.free.handle);
1196 rc = get_paddr_from_handle(ctx, &args->in,
1222 struct hl_ctx *ctx = hpriv->ctx;
1251 rc = alloc_device_memory(ctx, &args->in, &handle);
1258 rc = free_device_memory(ctx, args->in.free.handle);
1262 rc = map_device_va(ctx, &args->in, &device_addr);
1269 rc = unmap_device_va(ctx, args->in.unmap.device_virt_addr,
1556 * @ctx: pointer to the habanalabs context structure
1571 static int vm_ctx_init_with_ranges(struct hl_ctx *ctx,
1579 struct hl_device *hdev = ctx->hdev;
1582 ctx->host_va_range = kzalloc(sizeof(*ctx->host_va_range), GFP_KERNEL);
1583 if (!ctx->host_va_range)
1586 ctx->host_huge_va_range = kzalloc(sizeof(*ctx->host_huge_va_range),
1588 if (!ctx->host_huge_va_range) {
1593 ctx->dram_va_range = kzalloc(sizeof(*ctx->dram_va_range), GFP_KERNEL);
1594 if (!ctx->dram_va_range) {
1599 rc = hl_mmu_ctx_init(ctx);
1601 dev_err(hdev->dev, "failed to init context %d\n", ctx->asid);
1605 mutex_init(&ctx->mem_hash_lock);
1606 hash_init(ctx->mem_hash);
1608 mutex_init(&ctx->host_va_range->lock);
1610 rc = va_range_init(hdev, ctx->host_va_range, host_range_start,
1618 mutex_init(&ctx->host_huge_va_range->lock);
1620 rc = va_range_init(hdev, ctx->host_huge_va_range,
1629 kfree(ctx->host_huge_va_range);
1630 ctx->host_huge_va_range = ctx->host_va_range;
1633 mutex_init(&ctx->dram_va_range->lock);
1635 rc = va_range_init(hdev, ctx->dram_va_range, dram_range_start,
1642 hl_debugfs_add_ctx_mem_hash(hdev, ctx);
1647 mutex_destroy(&ctx->dram_va_range->lock);
1650 mutex_lock(&ctx->host_huge_va_range->lock);
1651 clear_va_list_locked(hdev, &ctx->host_huge_va_range->list);
1652 mutex_unlock(&ctx->host_huge_va_range->lock);
1656 mutex_destroy(&ctx->host_huge_va_range->lock);
1657 mutex_lock(&ctx->host_va_range->lock);
1658 clear_va_list_locked(hdev, &ctx->host_va_range->list);
1659 mutex_unlock(&ctx->host_va_range->lock);
1661 mutex_destroy(&ctx->host_va_range->lock);
1662 mutex_destroy(&ctx->mem_hash_lock);
1663 hl_mmu_ctx_fini(ctx);
1665 kfree(ctx->dram_va_range);
1667 kfree(ctx->host_huge_va_range);
1669 kfree(ctx->host_va_range);
1674 int hl_vm_ctx_init(struct hl_ctx *ctx)
1676 struct asic_fixed_properties *prop = &ctx->hdev->asic_prop;
1680 atomic64_set(&ctx->dram_phys_mem, 0);
1689 if (ctx->hdev->mmu_enable) {
1705 return vm_ctx_init_with_ranges(ctx, host_range_start, host_range_end,
1715 * @ctx : pointer to the habanalabs context structure
1732 void hl_vm_ctx_fini(struct hl_ctx *ctx)
1734 struct hl_device *hdev = ctx->hdev;
1741 hl_debugfs_remove_ctx_mem_hash(hdev, ctx);
1747 if (!hdev->hard_reset_pending && !hash_empty(ctx->mem_hash))
1751 hash_for_each_safe(ctx->mem_hash, i, tmp_node, hnode, node) {
1754 hnode->vaddr, ctx->asid);
1755 unmap_device_va(ctx, hnode->vaddr, true);
1764 if (phys_pg_list->asid == ctx->asid) {
1767 phys_pg_list, ctx->asid);
1775 va_range_fini(hdev, ctx->dram_va_range);
1777 va_range_fini(hdev, ctx->host_huge_va_range);
1778 va_range_fini(hdev, ctx->host_va_range);
1780 mutex_destroy(&ctx->mem_hash_lock);
1781 hl_mmu_ctx_fini(ctx);