1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include <uapi/misc/habanalabs.h>
9 #include "habanalabs.h"
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
15 static int cb_map_mem(struct hl_ctx *ctx, struct hl_cb *cb)
17 struct hl_device *hdev = ctx->hdev;
18 struct asic_fixed_properties *prop = &hdev->asic_prop;
19 struct hl_vm_va_block *va_block, *tmp;
22 u32 page_size = prop->pmmu.page_size;
26 if (!hdev->supports_cb_mapping) {
27 dev_err_ratelimited(hdev->dev,
28 "Cannot map CB because no VA range is allocated for CB mapping\n");
32 if (!hdev->mmu_enable) {
33 dev_err_ratelimited(hdev->dev,
34 "Cannot map CB because MMU is disabled\n");
38 INIT_LIST_HEAD(&cb->va_block_list);
40 for (bus_addr = cb->bus_address;
41 bus_addr < cb->bus_address + cb->size;
42 bus_addr += page_size) {
44 virt_addr = (u64) gen_pool_alloc(ctx->cb_va_pool, page_size);
47 "Failed to allocate device virtual address for CB\n");
49 goto err_va_pool_free;
52 va_block = kzalloc(sizeof(*va_block), GFP_KERNEL);
55 gen_pool_free(ctx->cb_va_pool, virt_addr, page_size);
56 goto err_va_pool_free;
59 va_block->start = virt_addr;
60 va_block->end = virt_addr + page_size;
61 va_block->size = page_size;
62 list_add_tail(&va_block->node, &cb->va_block_list);
65 mutex_lock(&ctx->mmu_lock);
67 bus_addr = cb->bus_address;
69 list_for_each_entry(va_block, &cb->va_block_list, node) {
70 rc = hl_mmu_map_page(ctx, va_block->start, bus_addr,
71 va_block->size, list_is_last(&va_block->node,
74 dev_err(hdev->dev, "Failed to map VA %#llx to CB\n",
79 bus_addr += va_block->size;
80 offset += va_block->size;
83 hdev->asic_funcs->mmu_invalidate_cache(hdev, false, VM_TYPE_USERPTR);
85 mutex_unlock(&ctx->mmu_lock);
87 cb->is_mmu_mapped = true;
92 list_for_each_entry(va_block, &cb->va_block_list, node) {
95 hl_mmu_unmap_page(ctx, va_block->start, va_block->size,
96 offset <= va_block->size);
97 offset -= va_block->size;
100 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
102 mutex_unlock(&ctx->mmu_lock);
105 list_for_each_entry_safe(va_block, tmp, &cb->va_block_list, node) {
106 gen_pool_free(ctx->cb_va_pool, va_block->start, va_block->size);
107 list_del(&va_block->node);
114 static void cb_unmap_mem(struct hl_ctx *ctx, struct hl_cb *cb)
116 struct hl_device *hdev = ctx->hdev;
117 struct hl_vm_va_block *va_block, *tmp;
119 mutex_lock(&ctx->mmu_lock);
121 list_for_each_entry(va_block, &cb->va_block_list, node)
122 if (hl_mmu_unmap_page(ctx, va_block->start, va_block->size,
123 list_is_last(&va_block->node,
124 &cb->va_block_list)))
125 dev_warn_ratelimited(hdev->dev,
126 "Failed to unmap CB's va 0x%llx\n",
129 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
131 mutex_unlock(&ctx->mmu_lock);
133 list_for_each_entry_safe(va_block, tmp, &cb->va_block_list, node) {
134 gen_pool_free(ctx->cb_va_pool, va_block->start, va_block->size);
135 list_del(&va_block->node);
140 static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)
143 gen_pool_free(hdev->internal_cb_pool,
144 (uintptr_t)cb->kernel_address, cb->size);
146 hdev->asic_funcs->asic_dma_free_coherent(hdev, cb->size,
147 cb->kernel_address, cb->bus_address);
152 static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb)
155 spin_lock(&hdev->cb_pool_lock);
156 list_add(&cb->pool_list, &hdev->cb_pool);
157 spin_unlock(&hdev->cb_pool_lock);
163 static void cb_release(struct kref *ref)
165 struct hl_device *hdev;
168 cb = container_of(ref, struct hl_cb, refcount);
171 hl_debugfs_remove_cb(cb);
173 if (cb->is_mmu_mapped)
174 cb_unmap_mem(cb->ctx, cb);
178 cb_do_release(hdev, cb);
181 static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,
182 int ctx_id, bool internal_cb)
184 struct hl_cb *cb = NULL;
189 * We use of GFP_ATOMIC here because this function can be called from
190 * the latency-sensitive code path for command submission. Due to H/W
191 * limitations in some of the ASICs, the kernel must copy the user CB
192 * that is designated for an external queue and actually enqueue
193 * the kernel's copy. Hence, we must never sleep in this code section
194 * and must use GFP_ATOMIC for all memory allocations.
196 if (ctx_id == HL_KERNEL_ASID_ID && !hdev->disabled)
197 cb = kzalloc(sizeof(*cb), GFP_ATOMIC);
200 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
206 p = (void *) gen_pool_alloc(hdev->internal_cb_pool, cb_size);
212 cb_offset = p - hdev->internal_cb_pool_virt_addr;
213 cb->is_internal = true;
214 cb->bus_address = hdev->internal_cb_va_base + cb_offset;
215 } else if (ctx_id == HL_KERNEL_ASID_ID) {
216 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
217 &cb->bus_address, GFP_ATOMIC);
219 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev,
220 cb_size, &cb->bus_address, GFP_KERNEL);
222 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
224 GFP_USER | __GFP_ZERO);
229 "failed to allocate %d of dma memory for CB\n",
235 cb->kernel_address = p;
241 int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
242 struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
243 bool map_cb, u64 *handle)
246 bool alloc_new_cb = true;
247 int rc, ctx_id = ctx->asid;
250 * Can't use generic function to check this because of special case
251 * where we create a CB as part of the reset process
253 if ((hdev->disabled) || ((atomic_read(&hdev->in_reset)) &&
254 (ctx_id != HL_KERNEL_ASID_ID))) {
255 dev_warn_ratelimited(hdev->dev,
256 "Device is disabled or in reset. Can't create new CBs\n");
261 if (cb_size > SZ_2M) {
262 dev_err(hdev->dev, "CB size %d must be less than %d\n",
269 /* Minimum allocation must be PAGE SIZE */
270 if (cb_size < PAGE_SIZE)
273 if (ctx_id == HL_KERNEL_ASID_ID &&
274 cb_size <= hdev->asic_prop.cb_pool_cb_size) {
276 spin_lock(&hdev->cb_pool_lock);
277 if (!list_empty(&hdev->cb_pool)) {
278 cb = list_first_entry(&hdev->cb_pool,
279 typeof(*cb), pool_list);
280 list_del(&cb->pool_list);
281 spin_unlock(&hdev->cb_pool_lock);
282 alloc_new_cb = false;
284 spin_unlock(&hdev->cb_pool_lock);
285 dev_dbg(hdev->dev, "CB pool is empty\n");
291 cb = hl_cb_alloc(hdev, cb_size, ctx_id, internal_cb);
300 hl_ctx_get(hdev, cb->ctx);
303 if (ctx_id == HL_KERNEL_ASID_ID) {
305 "CB mapping is not supported for kernel context\n");
310 rc = cb_map_mem(ctx, cb);
315 spin_lock(&mgr->cb_lock);
316 rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);
318 rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_KERNEL);
319 spin_unlock(&mgr->cb_lock);
322 dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
328 kref_init(&cb->refcount);
329 spin_lock_init(&cb->lock);
332 * idr is 32-bit so we can safely OR it with a mask that is above
335 *handle = cb->id | HL_MMAP_TYPE_CB;
336 *handle <<= PAGE_SHIFT;
338 hl_debugfs_add_cb(cb);
343 if (cb->is_mmu_mapped)
344 cb_unmap_mem(cb->ctx, cb);
347 cb_do_release(hdev, cb);
354 int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle)
361 * handle was given to user to do mmap, I need to shift it back to
362 * how the idr module gave it to me
364 cb_handle >>= PAGE_SHIFT;
365 handle = (u32) cb_handle;
367 spin_lock(&mgr->cb_lock);
369 cb = idr_find(&mgr->cb_handles, handle);
371 idr_remove(&mgr->cb_handles, handle);
372 spin_unlock(&mgr->cb_lock);
373 kref_put(&cb->refcount, cb_release);
375 spin_unlock(&mgr->cb_lock);
377 "CB destroy failed, no match to handle 0x%x\n", handle);
384 static int hl_cb_info(struct hl_device *hdev, struct hl_cb_mgr *mgr,
385 u64 cb_handle, u32 *usage_cnt)
391 /* The CB handle was given to user to do mmap, so need to shift it back
392 * to the value which was allocated by the IDR module.
394 cb_handle >>= PAGE_SHIFT;
395 handle = (u32) cb_handle;
397 spin_lock(&mgr->cb_lock);
399 cb = idr_find(&mgr->cb_handles, handle);
402 "CB info failed, no match to handle 0x%x\n", handle);
407 *usage_cnt = atomic_read(&cb->cs_cnt);
410 spin_unlock(&mgr->cb_lock);
414 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
416 union hl_cb_args *args = data;
417 struct hl_device *hdev = hpriv->hdev;
418 enum hl_device_status status;
423 if (!hl_device_operational(hdev, &status)) {
424 dev_warn_ratelimited(hdev->dev,
425 "Device is %s. Can't execute CB IOCTL\n",
426 hdev->status[status]);
430 switch (args->in.op) {
431 case HL_CB_OP_CREATE:
432 if (args->in.cb_size > HL_MAX_CB_SIZE) {
434 "User requested CB size %d must be less than %d\n",
435 args->in.cb_size, HL_MAX_CB_SIZE);
438 rc = hl_cb_create(hdev, &hpriv->cb_mgr, hpriv->ctx,
439 args->in.cb_size, false,
440 !!(args->in.flags & HL_CB_FLAGS_MAP),
444 memset(args, 0, sizeof(*args));
445 args->out.cb_handle = handle;
448 case HL_CB_OP_DESTROY:
449 rc = hl_cb_destroy(hdev, &hpriv->cb_mgr,
454 rc = hl_cb_info(hdev, &hpriv->cb_mgr, args->in.cb_handle,
456 memset(args, 0, sizeof(*args));
457 args->out.usage_cnt = usage_cnt;
468 static void cb_vm_close(struct vm_area_struct *vma)
470 struct hl_cb *cb = (struct hl_cb *) vma->vm_private_data;
473 new_mmap_size = cb->mmap_size - (vma->vm_end - vma->vm_start);
475 if (new_mmap_size > 0) {
476 cb->mmap_size = new_mmap_size;
480 spin_lock(&cb->lock);
482 spin_unlock(&cb->lock);
485 vma->vm_private_data = NULL;
488 static const struct vm_operations_struct cb_vm_ops = {
492 int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
494 struct hl_device *hdev = hpriv->hdev;
496 u32 handle, user_cb_size;
499 /* We use the page offset to hold the idr and thus we need to clear
500 * it before doing the mmap itself
502 handle = vma->vm_pgoff;
505 /* reference was taken here */
506 cb = hl_cb_get(hdev, &hpriv->cb_mgr, handle);
509 "CB mmap failed, no match to handle 0x%x\n", handle);
513 /* Validation check */
514 user_cb_size = vma->vm_end - vma->vm_start;
515 if (user_cb_size != ALIGN(cb->size, PAGE_SIZE)) {
517 "CB mmap failed, mmap size 0x%lx != 0x%x cb size\n",
518 vma->vm_end - vma->vm_start, cb->size);
523 if (!access_ok((void __user *) (uintptr_t) vma->vm_start,
526 "user pointer is invalid - 0x%lx\n",
533 spin_lock(&cb->lock);
537 "CB mmap failed, CB already mmaped to user\n");
544 spin_unlock(&cb->lock);
546 vma->vm_ops = &cb_vm_ops;
549 * Note: We're transferring the cb reference to
550 * vma->vm_private_data here.
553 vma->vm_private_data = cb;
555 rc = hdev->asic_funcs->cb_mmap(hdev, vma, cb->kernel_address,
556 cb->bus_address, cb->size);
558 spin_lock(&cb->lock);
563 cb->mmap_size = cb->size;
564 vma->vm_pgoff = handle;
569 spin_unlock(&cb->lock);
575 struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
580 spin_lock(&mgr->cb_lock);
581 cb = idr_find(&mgr->cb_handles, handle);
584 spin_unlock(&mgr->cb_lock);
586 "CB get failed, no match to handle 0x%x\n", handle);
590 kref_get(&cb->refcount);
592 spin_unlock(&mgr->cb_lock);
598 void hl_cb_put(struct hl_cb *cb)
600 kref_put(&cb->refcount, cb_release);
603 void hl_cb_mgr_init(struct hl_cb_mgr *mgr)
605 spin_lock_init(&mgr->cb_lock);
606 idr_init(&mgr->cb_handles);
609 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr)
615 idp = &mgr->cb_handles;
617 idr_for_each_entry(idp, cb, id) {
618 if (kref_put(&cb->refcount, cb_release) != 1)
620 "CB %d for CTX ID %d is still alive\n",
624 idr_destroy(&mgr->cb_handles);
627 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
634 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx, cb_size,
635 internal_cb, false, &cb_handle);
638 "Failed to allocate CB for the kernel driver %d\n", rc);
642 cb_handle >>= PAGE_SHIFT;
643 cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr, (u32) cb_handle);
644 /* hl_cb_get should never fail here */
646 dev_crit(hdev->dev, "Kernel CB handle invalid 0x%x\n",
654 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb_handle << PAGE_SHIFT);
659 int hl_cb_pool_init(struct hl_device *hdev)
664 INIT_LIST_HEAD(&hdev->cb_pool);
665 spin_lock_init(&hdev->cb_pool_lock);
667 for (i = 0 ; i < hdev->asic_prop.cb_pool_cb_cnt ; i++) {
668 cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size,
669 HL_KERNEL_ASID_ID, false);
672 list_add(&cb->pool_list, &hdev->cb_pool);
674 hl_cb_pool_fini(hdev);
682 int hl_cb_pool_fini(struct hl_device *hdev)
684 struct hl_cb *cb, *tmp;
686 list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) {
687 list_del(&cb->pool_list);
694 int hl_cb_va_pool_init(struct hl_ctx *ctx)
696 struct hl_device *hdev = ctx->hdev;
697 struct asic_fixed_properties *prop = &hdev->asic_prop;
700 if (!hdev->supports_cb_mapping)
703 ctx->cb_va_pool = gen_pool_create(__ffs(prop->pmmu.page_size), -1);
704 if (!ctx->cb_va_pool) {
706 "Failed to create VA gen pool for CB mapping\n");
710 rc = gen_pool_add(ctx->cb_va_pool, prop->cb_va_start_addr,
711 prop->cb_va_end_addr - prop->cb_va_start_addr, -1);
714 "Failed to add memory to VA gen pool for CB mapping\n");
715 goto err_pool_destroy;
721 gen_pool_destroy(ctx->cb_va_pool);
726 void hl_cb_va_pool_fini(struct hl_ctx *ctx)
728 struct hl_device *hdev = ctx->hdev;
730 if (!hdev->supports_cb_mapping)
733 gen_pool_destroy(ctx->cb_va_pool);