Lines Matching full:cb

17 static int cb_map_mem(struct hl_ctx *ctx, struct hl_cb *cb)  in cb_map_mem()  argument
26 "Mapping a CB to the device's MMU is not supported\n"); in cb_map_mem()
32 "Cannot map CB because MMU is disabled\n"); in cb_map_mem()
36 if (cb->is_mmu_mapped) in cb_map_mem()
39 cb->roundup_size = roundup(cb->size, page_size); in cb_map_mem()
41 cb->virtual_addr = (u64) gen_pool_alloc(ctx->cb_va_pool, cb->roundup_size); in cb_map_mem()
42 if (!cb->virtual_addr) { in cb_map_mem()
43 dev_err(hdev->dev, "Failed to allocate device virtual address for CB\n"); in cb_map_mem()
48 rc = hl_mmu_map_contiguous(ctx, cb->virtual_addr, cb->bus_address, cb->roundup_size); in cb_map_mem()
50 dev_err(hdev->dev, "Failed to map VA %#llx to CB\n", cb->virtual_addr); in cb_map_mem()
56 cb->is_mmu_mapped = true; in cb_map_mem()
61 gen_pool_free(ctx->cb_va_pool, cb->virtual_addr, cb->roundup_size); in cb_map_mem()
65 static void cb_unmap_mem(struct hl_ctx *ctx, struct hl_cb *cb) in cb_unmap_mem() argument
70 hl_mmu_unmap_contiguous(ctx, cb->virtual_addr, cb->roundup_size); in cb_unmap_mem()
74 gen_pool_free(ctx->cb_va_pool, cb->virtual_addr, cb->roundup_size); in cb_unmap_mem()
77 static void cb_fini(struct hl_device *hdev, struct hl_cb *cb) in cb_fini() argument
79 if (cb->is_internal) in cb_fini()
81 (uintptr_t)cb->kernel_address, cb->size); in cb_fini()
83 hl_asic_dma_free_coherent(hdev, cb->size, cb->kernel_address, cb->bus_address); in cb_fini()
85 kfree(cb); in cb_fini()
88 static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb) in cb_do_release() argument
90 if (cb->is_pool) { in cb_do_release()
92 list_add(&cb->pool_list, &hdev->cb_pool); in cb_do_release()
95 cb_fini(hdev, cb); in cb_do_release()
102 struct hl_cb *cb = NULL; in hl_cb_alloc() local
109 * limitations in some of the ASICs, the kernel must copy the user CB in hl_cb_alloc()
115 cb = kzalloc(sizeof(*cb), GFP_ATOMIC); in hl_cb_alloc()
117 if (!cb) in hl_cb_alloc()
118 cb = kzalloc(sizeof(*cb), GFP_KERNEL); in hl_cb_alloc()
120 if (!cb) in hl_cb_alloc()
126 kfree(cb); in hl_cb_alloc()
131 cb->is_internal = true; in hl_cb_alloc()
132 cb->bus_address = hdev->internal_cb_va_base + cb_offset; in hl_cb_alloc()
134 p = hl_asic_dma_alloc_coherent(hdev, cb_size, &cb->bus_address, GFP_ATOMIC); in hl_cb_alloc()
136 p = hl_asic_dma_alloc_coherent(hdev, cb_size, &cb->bus_address, GFP_KERNEL); in hl_cb_alloc()
138 p = hl_asic_dma_alloc_coherent(hdev, cb_size, &cb->bus_address, in hl_cb_alloc()
144 "failed to allocate %d of dma memory for CB\n", in hl_cb_alloc()
146 kfree(cb); in hl_cb_alloc()
150 cb->kernel_address = p; in hl_cb_alloc()
151 cb->size = cb_size; in hl_cb_alloc()
153 return cb; in hl_cb_alloc()
166 struct hl_cb *cb = buf->private; in hl_cb_mmap_mem_release() local
168 hl_debugfs_remove_cb(cb); in hl_cb_mmap_mem_release()
170 if (cb->is_mmu_mapped) in hl_cb_mmap_mem_release()
171 cb_unmap_mem(cb->ctx, cb); in hl_cb_mmap_mem_release()
173 hl_ctx_put(cb->ctx); in hl_cb_mmap_mem_release()
175 cb_do_release(cb->hdev, cb); in hl_cb_mmap_mem_release()
181 struct hl_cb *cb; in hl_cb_mmap_mem_alloc() local
195 cb = list_first_entry(&cb_args->hdev->cb_pool, in hl_cb_mmap_mem_alloc()
196 typeof(*cb), pool_list); in hl_cb_mmap_mem_alloc()
197 list_del(&cb->pool_list); in hl_cb_mmap_mem_alloc()
202 dev_dbg(cb_args->hdev->dev, "CB pool is empty\n"); in hl_cb_mmap_mem_alloc()
208 cb = hl_cb_alloc(cb_args->hdev, cb_args->cb_size, ctx_id, cb_args->internal_cb); in hl_cb_mmap_mem_alloc()
209 if (!cb) in hl_cb_mmap_mem_alloc()
213 cb->hdev = cb_args->hdev; in hl_cb_mmap_mem_alloc()
214 cb->ctx = cb_args->ctx; in hl_cb_mmap_mem_alloc()
215 cb->buf = buf; in hl_cb_mmap_mem_alloc()
216 cb->buf->mappable_size = cb->size; in hl_cb_mmap_mem_alloc()
217 cb->buf->private = cb; in hl_cb_mmap_mem_alloc()
219 hl_ctx_get(cb->ctx); in hl_cb_mmap_mem_alloc()
224 "CB mapping is not supported for kernel context\n"); in hl_cb_mmap_mem_alloc()
229 rc = cb_map_mem(cb_args->ctx, cb); in hl_cb_mmap_mem_alloc()
234 hl_debugfs_add_cb(cb); in hl_cb_mmap_mem_alloc()
239 hl_ctx_put(cb->ctx); in hl_cb_mmap_mem_alloc()
240 cb_do_release(cb_args->hdev, cb); in hl_cb_mmap_mem_alloc()
248 struct hl_cb *cb = buf->private; in hl_cb_mmap() local
250 return cb->hdev->asic_funcs->mmap(cb->hdev, vma, cb->kernel_address, in hl_cb_mmap()
251 cb->bus_address, cb->size); in hl_cb_mmap()
255 .topic = "CB",
283 dev_err(hdev->dev, "CB size %d must be less than %d\n", in hl_cb_create()
308 dev_dbg(mmg->dev, "CB 0x%llx is destroyed while still in use\n", cb_handle); in hl_cb_destroy()
316 struct hl_cb *cb; in hl_cb_info() local
319 cb = hl_cb_get(mmg, handle); in hl_cb_info()
320 if (!cb) { in hl_cb_info()
322 "CB info failed, no match to handle 0x%llx\n", handle); in hl_cb_info()
327 if (cb->is_mmu_mapped) { in hl_cb_info()
328 *device_va = cb->virtual_addr; in hl_cb_info()
330 dev_err(mmg->dev, "CB is not mapped to the device's MMU\n"); in hl_cb_info()
335 *usage_cnt = atomic_read(&cb->cs_cnt); in hl_cb_info()
339 hl_cb_put(cb); in hl_cb_info()
354 "Device is %s. Can't execute CB IOCTL\n", in hl_cb_ioctl()
363 "User requested CB size %d must be less than %d\n", in hl_cb_ioctl()
417 void hl_cb_put(struct hl_cb *cb) in hl_cb_put() argument
419 hl_mmap_mem_buf_put(cb->buf); in hl_cb_put()
426 struct hl_cb *cb; in hl_cb_kernel_create() local
433 "Failed to allocate CB for the kernel driver %d\n", rc); in hl_cb_kernel_create()
437 cb = hl_cb_get(&hdev->kernel_mem_mgr, cb_handle); in hl_cb_kernel_create()
439 if (!cb) { in hl_cb_kernel_create()
440 dev_crit(hdev->dev, "Kernel CB handle invalid 0x%x\n", in hl_cb_kernel_create()
445 return cb; in hl_cb_kernel_create()
455 struct hl_cb *cb; in hl_cb_pool_init() local
462 cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size, in hl_cb_pool_init()
464 if (cb) { in hl_cb_pool_init()
465 cb->is_pool = true; in hl_cb_pool_init()
466 list_add(&cb->pool_list, &hdev->cb_pool); in hl_cb_pool_init()
478 struct hl_cb *cb, *tmp; in hl_cb_pool_fini() local
480 list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) { in hl_cb_pool_fini()
481 list_del(&cb->pool_list); in hl_cb_pool_fini()
482 cb_fini(hdev, cb); in hl_cb_pool_fini()
500 "Failed to create VA gen pool for CB mapping\n"); in hl_cb_va_pool_init()
513 "Failed to add memory to VA gen pool for CB mapping\n"); in hl_cb_va_pool_init()