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>
14 #include <linux/genalloc.h>
16 static int cb_map_mem(struct hl_ctx *ctx, struct hl_cb *cb)
18 struct hl_device *hdev = ctx->hdev;
19 struct asic_fixed_properties *prop = &hdev->asic_prop;
20 struct hl_vm_va_block *va_block, *tmp;
23 u32 page_size = prop->pmmu.page_size;
27 if (!hdev->supports_cb_mapping) {
28 dev_err_ratelimited(hdev->dev,
29 "Cannot map CB because no VA range is allocated for CB mapping\n");
33 if (!hdev->mmu_enable) {
34 dev_err_ratelimited(hdev->dev,
35 "Cannot map CB because MMU is disabled\n");
39 INIT_LIST_HEAD(&cb->va_block_list);
41 for (bus_addr = cb->bus_address;
42 bus_addr < cb->bus_address + cb->size;
43 bus_addr += page_size) {
45 virt_addr = (u64) gen_pool_alloc(ctx->cb_va_pool, page_size);
48 "Failed to allocate device virtual address for CB\n");
50 goto err_va_pool_free;
53 va_block = kzalloc(sizeof(*va_block), GFP_KERNEL);
56 gen_pool_free(ctx->cb_va_pool, virt_addr, page_size);
57 goto err_va_pool_free;
60 va_block->start = virt_addr;
61 va_block->end = virt_addr + page_size;
62 va_block->size = page_size;
63 list_add_tail(&va_block->node, &cb->va_block_list);
66 mutex_lock(&ctx->mmu_lock);
68 bus_addr = cb->bus_address;
70 list_for_each_entry(va_block, &cb->va_block_list, node) {
71 rc = hl_mmu_map(ctx, va_block->start, bus_addr, va_block->size,
72 list_is_last(&va_block->node,
75 dev_err(hdev->dev, "Failed to map VA %#llx to CB\n",
80 bus_addr += va_block->size;
81 offset += va_block->size;
84 hdev->asic_funcs->mmu_invalidate_cache(hdev, false, VM_TYPE_USERPTR);
86 mutex_unlock(&ctx->mmu_lock);
88 cb->is_mmu_mapped = true;
93 list_for_each_entry(va_block, &cb->va_block_list, node) {
96 hl_mmu_unmap(ctx, va_block->start, va_block->size,
97 offset <= va_block->size);
98 offset -= va_block->size;
101 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
103 mutex_unlock(&ctx->mmu_lock);
106 list_for_each_entry_safe(va_block, tmp, &cb->va_block_list, node) {
107 gen_pool_free(ctx->cb_va_pool, va_block->start, va_block->size);
108 list_del(&va_block->node);
115 static void cb_unmap_mem(struct hl_ctx *ctx, struct hl_cb *cb)
117 struct hl_device *hdev = ctx->hdev;
118 struct hl_vm_va_block *va_block, *tmp;
120 mutex_lock(&ctx->mmu_lock);
122 list_for_each_entry(va_block, &cb->va_block_list, node)
123 if (hl_mmu_unmap(ctx, va_block->start, va_block->size,
124 list_is_last(&va_block->node,
125 &cb->va_block_list)))
126 dev_warn_ratelimited(hdev->dev,
127 "Failed to unmap CB's va 0x%llx\n",
130 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
132 mutex_unlock(&ctx->mmu_lock);
134 list_for_each_entry_safe(va_block, tmp, &cb->va_block_list, node) {
135 gen_pool_free(ctx->cb_va_pool, va_block->start, va_block->size);
136 list_del(&va_block->node);
141 static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)
144 gen_pool_free(hdev->internal_cb_pool,
145 cb->kernel_address, cb->size);
147 hdev->asic_funcs->asic_dma_free_coherent(hdev, cb->size,
148 (void *) (uintptr_t) cb->kernel_address,
154 static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb)
157 spin_lock(&hdev->cb_pool_lock);
158 list_add(&cb->pool_list, &hdev->cb_pool);
159 spin_unlock(&hdev->cb_pool_lock);
165 static void cb_release(struct kref *ref)
167 struct hl_device *hdev;
170 cb = container_of(ref, struct hl_cb, refcount);
173 hl_debugfs_remove_cb(cb);
175 if (cb->is_mmu_mapped)
176 cb_unmap_mem(cb->ctx, cb);
180 cb_do_release(hdev, cb);
183 static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,
184 int ctx_id, bool internal_cb)
191 * We use of GFP_ATOMIC here because this function can be called from
192 * the latency-sensitive code path for command submission. Due to H/W
193 * limitations in some of the ASICs, the kernel must copy the user CB
194 * that is designated for an external queue and actually enqueue
195 * the kernel's copy. Hence, we must never sleep in this code section
196 * and must use GFP_ATOMIC for all memory allocations.
198 if (ctx_id == HL_KERNEL_ASID_ID)
199 cb = kzalloc(sizeof(*cb), GFP_ATOMIC);
201 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
207 p = (void *) gen_pool_alloc(hdev->internal_cb_pool, cb_size);
213 cb_offset = p - hdev->internal_cb_pool_virt_addr;
214 cb->is_internal = true;
215 cb->bus_address = hdev->internal_cb_va_base + cb_offset;
216 } else if (ctx_id == HL_KERNEL_ASID_ID) {
217 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
218 &cb->bus_address, GFP_ATOMIC);
220 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
222 GFP_USER | __GFP_ZERO);
227 "failed to allocate %d of dma memory for CB\n",
233 cb->kernel_address = (u64) (uintptr_t) p;
239 int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
240 struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
241 bool map_cb, u64 *handle)
244 bool alloc_new_cb = true;
245 int rc, ctx_id = ctx->asid;
248 * Can't use generic function to check this because of special case
249 * where we create a CB as part of the reset process
251 if ((hdev->disabled) || ((atomic_read(&hdev->in_reset)) &&
252 (ctx_id != HL_KERNEL_ASID_ID))) {
253 dev_warn_ratelimited(hdev->dev,
254 "Device is disabled or in reset. Can't create new CBs\n");
259 if (cb_size > SZ_2M) {
260 dev_err(hdev->dev, "CB size %d must be less than %d\n",
267 /* Minimum allocation must be PAGE SIZE */
268 if (cb_size < PAGE_SIZE)
271 if (ctx_id == HL_KERNEL_ASID_ID &&
272 cb_size <= hdev->asic_prop.cb_pool_cb_size) {
274 spin_lock(&hdev->cb_pool_lock);
275 if (!list_empty(&hdev->cb_pool)) {
276 cb = list_first_entry(&hdev->cb_pool,
277 typeof(*cb), pool_list);
278 list_del(&cb->pool_list);
279 spin_unlock(&hdev->cb_pool_lock);
280 alloc_new_cb = false;
282 spin_unlock(&hdev->cb_pool_lock);
283 dev_dbg(hdev->dev, "CB pool is empty\n");
289 cb = hl_cb_alloc(hdev, cb_size, ctx_id, internal_cb);
298 hl_ctx_get(hdev, cb->ctx);
301 if (ctx_id == HL_KERNEL_ASID_ID) {
303 "CB mapping is not supported for kernel context\n");
308 rc = cb_map_mem(ctx, cb);
313 spin_lock(&mgr->cb_lock);
314 rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);
315 spin_unlock(&mgr->cb_lock);
318 dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
324 kref_init(&cb->refcount);
325 spin_lock_init(&cb->lock);
328 * idr is 32-bit so we can safely OR it with a mask that is above
331 *handle = cb->id | HL_MMAP_TYPE_CB;
332 *handle <<= PAGE_SHIFT;
334 hl_debugfs_add_cb(cb);
339 if (cb->is_mmu_mapped)
340 cb_unmap_mem(cb->ctx, cb);
343 cb_do_release(hdev, cb);
350 int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle)
357 * handle was given to user to do mmap, I need to shift it back to
358 * how the idr module gave it to me
360 cb_handle >>= PAGE_SHIFT;
361 handle = (u32) cb_handle;
363 spin_lock(&mgr->cb_lock);
365 cb = idr_find(&mgr->cb_handles, handle);
367 idr_remove(&mgr->cb_handles, handle);
368 spin_unlock(&mgr->cb_lock);
369 kref_put(&cb->refcount, cb_release);
371 spin_unlock(&mgr->cb_lock);
373 "CB destroy failed, no match to handle 0x%x\n", handle);
380 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
382 union hl_cb_args *args = data;
383 struct hl_device *hdev = hpriv->hdev;
387 if (hl_device_disabled_or_in_reset(hdev)) {
388 dev_warn_ratelimited(hdev->dev,
389 "Device is %s. Can't execute CB IOCTL\n",
390 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
394 switch (args->in.op) {
395 case HL_CB_OP_CREATE:
396 if (args->in.cb_size > HL_MAX_CB_SIZE) {
398 "User requested CB size %d must be less than %d\n",
399 args->in.cb_size, HL_MAX_CB_SIZE);
402 rc = hl_cb_create(hdev, &hpriv->cb_mgr, hpriv->ctx,
403 args->in.cb_size, false,
404 !!(args->in.flags & HL_CB_FLAGS_MAP),
408 memset(args, 0, sizeof(*args));
409 args->out.cb_handle = handle;
412 case HL_CB_OP_DESTROY:
413 rc = hl_cb_destroy(hdev, &hpriv->cb_mgr,
425 static void cb_vm_close(struct vm_area_struct *vma)
427 struct hl_cb *cb = (struct hl_cb *) vma->vm_private_data;
430 new_mmap_size = cb->mmap_size - (vma->vm_end - vma->vm_start);
432 if (new_mmap_size > 0) {
433 cb->mmap_size = new_mmap_size;
437 spin_lock(&cb->lock);
439 spin_unlock(&cb->lock);
442 vma->vm_private_data = NULL;
445 static const struct vm_operations_struct cb_vm_ops = {
449 int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
451 struct hl_device *hdev = hpriv->hdev;
453 u32 handle, user_cb_size;
456 /* We use the page offset to hold the idr and thus we need to clear
457 * it before doing the mmap itself
459 handle = vma->vm_pgoff;
462 /* reference was taken here */
463 cb = hl_cb_get(hdev, &hpriv->cb_mgr, handle);
466 "CB mmap failed, no match to handle 0x%x\n", handle);
470 /* Validation check */
471 user_cb_size = vma->vm_end - vma->vm_start;
472 if (user_cb_size != ALIGN(cb->size, PAGE_SIZE)) {
474 "CB mmap failed, mmap size 0x%lx != 0x%x cb size\n",
475 vma->vm_end - vma->vm_start, cb->size);
480 if (!access_ok((void __user *) (uintptr_t) vma->vm_start,
483 "user pointer is invalid - 0x%lx\n",
490 spin_lock(&cb->lock);
494 "CB mmap failed, CB already mmaped to user\n");
501 spin_unlock(&cb->lock);
503 vma->vm_ops = &cb_vm_ops;
506 * Note: We're transferring the cb reference to
507 * vma->vm_private_data here.
510 vma->vm_private_data = cb;
512 rc = hdev->asic_funcs->cb_mmap(hdev, vma, (void *) cb->kernel_address,
513 cb->bus_address, cb->size);
515 spin_lock(&cb->lock);
520 cb->mmap_size = cb->size;
525 spin_unlock(&cb->lock);
531 struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
536 spin_lock(&mgr->cb_lock);
537 cb = idr_find(&mgr->cb_handles, handle);
540 spin_unlock(&mgr->cb_lock);
542 "CB get failed, no match to handle 0x%x\n", handle);
546 kref_get(&cb->refcount);
548 spin_unlock(&mgr->cb_lock);
554 void hl_cb_put(struct hl_cb *cb)
556 kref_put(&cb->refcount, cb_release);
559 void hl_cb_mgr_init(struct hl_cb_mgr *mgr)
561 spin_lock_init(&mgr->cb_lock);
562 idr_init(&mgr->cb_handles);
565 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr)
571 idp = &mgr->cb_handles;
573 idr_for_each_entry(idp, cb, id) {
574 if (kref_put(&cb->refcount, cb_release) != 1)
576 "CB %d for CTX ID %d is still alive\n",
580 idr_destroy(&mgr->cb_handles);
583 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
590 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx, cb_size,
591 internal_cb, false, &cb_handle);
594 "Failed to allocate CB for the kernel driver %d\n", rc);
598 cb_handle >>= PAGE_SHIFT;
599 cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr, (u32) cb_handle);
600 /* hl_cb_get should never fail here so use kernel WARN */
601 WARN(!cb, "Kernel CB handle invalid 0x%x\n", (u32) cb_handle);
608 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb_handle << PAGE_SHIFT);
613 int hl_cb_pool_init(struct hl_device *hdev)
618 INIT_LIST_HEAD(&hdev->cb_pool);
619 spin_lock_init(&hdev->cb_pool_lock);
621 for (i = 0 ; i < hdev->asic_prop.cb_pool_cb_cnt ; i++) {
622 cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size,
623 HL_KERNEL_ASID_ID, false);
626 list_add(&cb->pool_list, &hdev->cb_pool);
628 hl_cb_pool_fini(hdev);
636 int hl_cb_pool_fini(struct hl_device *hdev)
638 struct hl_cb *cb, *tmp;
640 list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) {
641 list_del(&cb->pool_list);
648 int hl_cb_va_pool_init(struct hl_ctx *ctx)
650 struct hl_device *hdev = ctx->hdev;
651 struct asic_fixed_properties *prop = &hdev->asic_prop;
654 if (!hdev->supports_cb_mapping)
657 ctx->cb_va_pool = gen_pool_create(__ffs(prop->pmmu.page_size), -1);
658 if (!ctx->cb_va_pool) {
660 "Failed to create VA gen pool for CB mapping\n");
664 rc = gen_pool_add(ctx->cb_va_pool, prop->cb_va_start_addr,
665 prop->cb_va_end_addr - prop->cb_va_start_addr, -1);
668 "Failed to add memory to VA gen pool for CB mapping\n");
669 goto err_pool_destroy;
675 gen_pool_destroy(ctx->cb_va_pool);
680 void hl_cb_va_pool_fini(struct hl_ctx *ctx)
682 struct hl_device *hdev = ctx->hdev;
684 if (!hdev->supports_cb_mapping)
687 gen_pool_destroy(ctx->cb_va_pool);