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/genalloc.h>
15 static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)
18 gen_pool_free(hdev->internal_cb_pool,
19 cb->kernel_address, cb->size);
21 hdev->asic_funcs->asic_dma_free_coherent(hdev, cb->size,
22 (void *) (uintptr_t) cb->kernel_address,
28 static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb)
31 spin_lock(&hdev->cb_pool_lock);
32 list_add(&cb->pool_list, &hdev->cb_pool);
33 spin_unlock(&hdev->cb_pool_lock);
39 static void cb_release(struct kref *ref)
41 struct hl_device *hdev;
44 cb = container_of(ref, struct hl_cb, refcount);
47 hl_debugfs_remove_cb(cb);
49 cb_do_release(hdev, cb);
52 static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,
53 int ctx_id, bool internal_cb)
60 * We use of GFP_ATOMIC here because this function can be called from
61 * the latency-sensitive code path for command submission. Due to H/W
62 * limitations in some of the ASICs, the kernel must copy the user CB
63 * that is designated for an external queue and actually enqueue
64 * the kernel's copy. Hence, we must never sleep in this code section
65 * and must use GFP_ATOMIC for all memory allocations.
67 if (ctx_id == HL_KERNEL_ASID_ID)
68 cb = kzalloc(sizeof(*cb), GFP_ATOMIC);
70 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
76 p = (void *) gen_pool_alloc(hdev->internal_cb_pool, cb_size);
82 cb_offset = p - hdev->internal_cb_pool_virt_addr;
83 cb->is_internal = true;
84 cb->bus_address = hdev->internal_cb_va_base + cb_offset;
85 } else if (ctx_id == HL_KERNEL_ASID_ID) {
86 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
87 &cb->bus_address, GFP_ATOMIC);
89 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
91 GFP_USER | __GFP_ZERO);
96 "failed to allocate %d of dma memory for CB\n",
102 cb->kernel_address = (u64) (uintptr_t) p;
108 int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
109 u32 cb_size, u64 *handle, int ctx_id, bool internal_cb)
112 bool alloc_new_cb = true;
116 * Can't use generic function to check this because of special case
117 * where we create a CB as part of the reset process
119 if ((hdev->disabled) || ((atomic_read(&hdev->in_reset)) &&
120 (ctx_id != HL_KERNEL_ASID_ID))) {
121 dev_warn_ratelimited(hdev->dev,
122 "Device is disabled or in reset. Can't create new CBs\n");
127 if (cb_size > SZ_2M) {
128 dev_err(hdev->dev, "CB size %d must be less than %d\n",
135 /* Minimum allocation must be PAGE SIZE */
136 if (cb_size < PAGE_SIZE)
139 if (ctx_id == HL_KERNEL_ASID_ID &&
140 cb_size <= hdev->asic_prop.cb_pool_cb_size) {
142 spin_lock(&hdev->cb_pool_lock);
143 if (!list_empty(&hdev->cb_pool)) {
144 cb = list_first_entry(&hdev->cb_pool,
145 typeof(*cb), pool_list);
146 list_del(&cb->pool_list);
147 spin_unlock(&hdev->cb_pool_lock);
148 alloc_new_cb = false;
150 spin_unlock(&hdev->cb_pool_lock);
151 dev_dbg(hdev->dev, "CB pool is empty\n");
157 cb = hl_cb_alloc(hdev, cb_size, ctx_id, internal_cb);
167 spin_lock(&mgr->cb_lock);
168 rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);
169 spin_unlock(&mgr->cb_lock);
172 dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
178 kref_init(&cb->refcount);
179 spin_lock_init(&cb->lock);
182 * idr is 32-bit so we can safely OR it with a mask that is above
185 *handle = cb->id | HL_MMAP_CB_MASK;
186 *handle <<= PAGE_SHIFT;
188 hl_debugfs_add_cb(cb);
193 cb_do_release(hdev, cb);
200 int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle)
207 * handle was given to user to do mmap, I need to shift it back to
208 * how the idr module gave it to me
210 cb_handle >>= PAGE_SHIFT;
211 handle = (u32) cb_handle;
213 spin_lock(&mgr->cb_lock);
215 cb = idr_find(&mgr->cb_handles, handle);
217 idr_remove(&mgr->cb_handles, handle);
218 spin_unlock(&mgr->cb_lock);
219 kref_put(&cb->refcount, cb_release);
221 spin_unlock(&mgr->cb_lock);
223 "CB destroy failed, no match to handle 0x%x\n", handle);
230 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
232 union hl_cb_args *args = data;
233 struct hl_device *hdev = hpriv->hdev;
237 if (hl_device_disabled_or_in_reset(hdev)) {
238 dev_warn_ratelimited(hdev->dev,
239 "Device is %s. Can't execute CB IOCTL\n",
240 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
244 switch (args->in.op) {
245 case HL_CB_OP_CREATE:
246 if (args->in.cb_size > HL_MAX_CB_SIZE) {
248 "User requested CB size %d must be less than %d\n",
249 args->in.cb_size, HL_MAX_CB_SIZE);
252 rc = hl_cb_create(hdev, &hpriv->cb_mgr,
253 args->in.cb_size, &handle,
254 hpriv->ctx->asid, false);
257 memset(args, 0, sizeof(*args));
258 args->out.cb_handle = handle;
261 case HL_CB_OP_DESTROY:
262 rc = hl_cb_destroy(hdev, &hpriv->cb_mgr,
274 static void cb_vm_close(struct vm_area_struct *vma)
276 struct hl_cb *cb = (struct hl_cb *) vma->vm_private_data;
279 new_mmap_size = cb->mmap_size - (vma->vm_end - vma->vm_start);
281 if (new_mmap_size > 0) {
282 cb->mmap_size = new_mmap_size;
286 spin_lock(&cb->lock);
288 spin_unlock(&cb->lock);
291 vma->vm_private_data = NULL;
294 static const struct vm_operations_struct cb_vm_ops = {
298 int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
300 struct hl_device *hdev = hpriv->hdev;
306 handle = vma->vm_pgoff;
308 /* reference was taken here */
309 cb = hl_cb_get(hdev, &hpriv->cb_mgr, handle);
312 "CB mmap failed, no match to handle 0x%x\n", handle);
316 /* Validation check */
317 if ((vma->vm_end - vma->vm_start) != ALIGN(cb->size, PAGE_SIZE)) {
319 "CB mmap failed, mmap size 0x%lx != 0x%x cb size\n",
320 vma->vm_end - vma->vm_start, cb->size);
325 spin_lock(&cb->lock);
329 "CB mmap failed, CB already mmaped to user\n");
336 spin_unlock(&cb->lock);
338 vma->vm_ops = &cb_vm_ops;
341 * Note: We're transferring the cb reference to
342 * vma->vm_private_data here.
345 vma->vm_private_data = cb;
347 /* Calculate address for CB */
348 address = virt_to_phys((void *) (uintptr_t) cb->kernel_address);
350 rc = hdev->asic_funcs->cb_mmap(hdev, vma, cb->kernel_address,
354 spin_lock(&cb->lock);
359 cb->mmap_size = cb->size;
364 spin_unlock(&cb->lock);
370 struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
375 spin_lock(&mgr->cb_lock);
376 cb = idr_find(&mgr->cb_handles, handle);
379 spin_unlock(&mgr->cb_lock);
381 "CB get failed, no match to handle 0x%x\n", handle);
385 kref_get(&cb->refcount);
387 spin_unlock(&mgr->cb_lock);
393 void hl_cb_put(struct hl_cb *cb)
395 kref_put(&cb->refcount, cb_release);
398 void hl_cb_mgr_init(struct hl_cb_mgr *mgr)
400 spin_lock_init(&mgr->cb_lock);
401 idr_init(&mgr->cb_handles);
404 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr)
410 idp = &mgr->cb_handles;
412 idr_for_each_entry(idp, cb, id) {
413 if (kref_put(&cb->refcount, cb_release) != 1)
415 "CB %d for CTX ID %d is still alive\n",
419 idr_destroy(&mgr->cb_handles);
422 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
429 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, cb_size, &cb_handle,
430 HL_KERNEL_ASID_ID, internal_cb);
433 "Failed to allocate CB for the kernel driver %d\n", rc);
437 cb_handle >>= PAGE_SHIFT;
438 cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr, (u32) cb_handle);
439 /* hl_cb_get should never fail here so use kernel WARN */
440 WARN(!cb, "Kernel CB handle invalid 0x%x\n", (u32) cb_handle);
447 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb_handle << PAGE_SHIFT);
452 int hl_cb_pool_init(struct hl_device *hdev)
457 INIT_LIST_HEAD(&hdev->cb_pool);
458 spin_lock_init(&hdev->cb_pool_lock);
460 for (i = 0 ; i < hdev->asic_prop.cb_pool_cb_cnt ; i++) {
461 cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size,
462 HL_KERNEL_ASID_ID, false);
465 list_add(&cb->pool_list, &hdev->cb_pool);
467 hl_cb_pool_fini(hdev);
475 int hl_cb_pool_fini(struct hl_device *hdev)
477 struct hl_cb *cb, *tmp;
479 list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) {
480 list_del(&cb->pool_list);