Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2016-2019 HabanaLabs, Ltd. |
| 5 | * All Rights Reserved. |
| 6 | */ |
| 7 | |
| 8 | #include <uapi/misc/habanalabs.h> |
| 9 | #include "habanalabs.h" |
| 10 | |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/slab.h> |
Ofir Bitton | bf6d109 | 2020-07-30 10:00:10 +0300 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 14 | #include <linux/genalloc.h> |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 15 | |
| 16 | static void cb_fini(struct hl_device *hdev, struct hl_cb *cb) |
| 17 | { |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 18 | if (cb->is_internal) |
| 19 | gen_pool_free(hdev->internal_cb_pool, |
| 20 | cb->kernel_address, cb->size); |
| 21 | else |
| 22 | hdev->asic_funcs->asic_dma_free_coherent(hdev, cb->size, |
| 23 | (void *) (uintptr_t) cb->kernel_address, |
| 24 | cb->bus_address); |
| 25 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 26 | kfree(cb); |
| 27 | } |
| 28 | |
| 29 | static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb) |
| 30 | { |
| 31 | if (cb->is_pool) { |
| 32 | spin_lock(&hdev->cb_pool_lock); |
| 33 | list_add(&cb->pool_list, &hdev->cb_pool); |
| 34 | spin_unlock(&hdev->cb_pool_lock); |
| 35 | } else { |
| 36 | cb_fini(hdev, cb); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static void cb_release(struct kref *ref) |
| 41 | { |
| 42 | struct hl_device *hdev; |
| 43 | struct hl_cb *cb; |
| 44 | |
| 45 | cb = container_of(ref, struct hl_cb, refcount); |
| 46 | hdev = cb->hdev; |
| 47 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 48 | hl_debugfs_remove_cb(cb); |
| 49 | |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 50 | hl_ctx_put(cb->ctx); |
| 51 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 52 | cb_do_release(hdev, cb); |
| 53 | } |
| 54 | |
| 55 | static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size, |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 56 | int ctx_id, bool internal_cb) |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 57 | { |
| 58 | struct hl_cb *cb; |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 59 | u32 cb_offset; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 60 | void *p; |
| 61 | |
| 62 | /* |
| 63 | * We use of GFP_ATOMIC here because this function can be called from |
| 64 | * the latency-sensitive code path for command submission. Due to H/W |
| 65 | * limitations in some of the ASICs, the kernel must copy the user CB |
| 66 | * that is designated for an external queue and actually enqueue |
| 67 | * the kernel's copy. Hence, we must never sleep in this code section |
| 68 | * and must use GFP_ATOMIC for all memory allocations. |
| 69 | */ |
| 70 | if (ctx_id == HL_KERNEL_ASID_ID) |
| 71 | cb = kzalloc(sizeof(*cb), GFP_ATOMIC); |
| 72 | else |
| 73 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
| 74 | |
| 75 | if (!cb) |
| 76 | return NULL; |
| 77 | |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 78 | if (internal_cb) { |
| 79 | p = (void *) gen_pool_alloc(hdev->internal_cb_pool, cb_size); |
| 80 | if (!p) { |
| 81 | kfree(cb); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | cb_offset = p - hdev->internal_cb_pool_virt_addr; |
| 86 | cb->is_internal = true; |
| 87 | cb->bus_address = hdev->internal_cb_va_base + cb_offset; |
| 88 | } else if (ctx_id == HL_KERNEL_ASID_ID) { |
Oded Gabbay | d9c3aa8 | 2019-05-01 11:47:04 +0300 | [diff] [blame] | 89 | p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size, |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 90 | &cb->bus_address, GFP_ATOMIC); |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 91 | } else { |
Oded Gabbay | d9c3aa8 | 2019-05-01 11:47:04 +0300 | [diff] [blame] | 92 | p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size, |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 93 | &cb->bus_address, |
| 94 | GFP_USER | __GFP_ZERO); |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 95 | } |
| 96 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 97 | if (!p) { |
| 98 | dev_err(hdev->dev, |
| 99 | "failed to allocate %d of dma memory for CB\n", |
| 100 | cb_size); |
| 101 | kfree(cb); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | cb->kernel_address = (u64) (uintptr_t) p; |
| 106 | cb->size = cb_size; |
| 107 | |
| 108 | return cb; |
| 109 | } |
| 110 | |
| 111 | int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr, |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 112 | struct hl_ctx *ctx, u32 cb_size, bool internal_cb, |
| 113 | u64 *handle) |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 114 | { |
| 115 | struct hl_cb *cb; |
| 116 | bool alloc_new_cb = true; |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 117 | int rc, ctx_id = ctx->asid; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 118 | |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 119 | /* |
| 120 | * Can't use generic function to check this because of special case |
| 121 | * where we create a CB as part of the reset process |
| 122 | */ |
| 123 | if ((hdev->disabled) || ((atomic_read(&hdev->in_reset)) && |
| 124 | (ctx_id != HL_KERNEL_ASID_ID))) { |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 125 | dev_warn_ratelimited(hdev->dev, |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 126 | "Device is disabled or in reset. Can't create new CBs\n"); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 127 | rc = -EBUSY; |
| 128 | goto out_err; |
| 129 | } |
| 130 | |
Oded Gabbay | 39b4251 | 2020-04-17 12:12:13 +0300 | [diff] [blame] | 131 | if (cb_size > SZ_2M) { |
| 132 | dev_err(hdev->dev, "CB size %d must be less than %d\n", |
| 133 | cb_size, SZ_2M); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 134 | rc = -EINVAL; |
| 135 | goto out_err; |
| 136 | } |
| 137 | |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 138 | if (!internal_cb) { |
| 139 | /* Minimum allocation must be PAGE SIZE */ |
| 140 | if (cb_size < PAGE_SIZE) |
| 141 | cb_size = PAGE_SIZE; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 142 | |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 143 | if (ctx_id == HL_KERNEL_ASID_ID && |
| 144 | cb_size <= hdev->asic_prop.cb_pool_cb_size) { |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 145 | |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 146 | spin_lock(&hdev->cb_pool_lock); |
| 147 | if (!list_empty(&hdev->cb_pool)) { |
| 148 | cb = list_first_entry(&hdev->cb_pool, |
| 149 | typeof(*cb), pool_list); |
| 150 | list_del(&cb->pool_list); |
| 151 | spin_unlock(&hdev->cb_pool_lock); |
| 152 | alloc_new_cb = false; |
| 153 | } else { |
| 154 | spin_unlock(&hdev->cb_pool_lock); |
| 155 | dev_dbg(hdev->dev, "CB pool is empty\n"); |
| 156 | } |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | if (alloc_new_cb) { |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 161 | cb = hl_cb_alloc(hdev, cb_size, ctx_id, internal_cb); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 162 | if (!cb) { |
| 163 | rc = -ENOMEM; |
| 164 | goto out_err; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | cb->hdev = hdev; |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 169 | cb->ctx = ctx; |
| 170 | hl_ctx_get(hdev, cb->ctx); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 171 | |
| 172 | spin_lock(&mgr->cb_lock); |
| 173 | rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC); |
| 174 | spin_unlock(&mgr->cb_lock); |
| 175 | |
| 176 | if (rc < 0) { |
| 177 | dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n"); |
| 178 | goto release_cb; |
| 179 | } |
| 180 | |
Oded Gabbay | f5b9c8c | 2020-08-12 10:11:20 +0300 | [diff] [blame] | 181 | cb->id = (u64) rc; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 182 | |
| 183 | kref_init(&cb->refcount); |
| 184 | spin_lock_init(&cb->lock); |
| 185 | |
| 186 | /* |
| 187 | * idr is 32-bit so we can safely OR it with a mask that is above |
| 188 | * 32 bit |
| 189 | */ |
Oded Gabbay | 3174ac9 | 2020-08-29 11:51:39 +0300 | [diff] [blame] | 190 | *handle = cb->id | HL_MMAP_TYPE_CB; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 191 | *handle <<= PAGE_SHIFT; |
| 192 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 193 | hl_debugfs_add_cb(cb); |
| 194 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 195 | return 0; |
| 196 | |
| 197 | release_cb: |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 198 | hl_ctx_put(cb->ctx); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 199 | cb_do_release(hdev, cb); |
| 200 | out_err: |
| 201 | *handle = 0; |
| 202 | |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle) |
| 207 | { |
| 208 | struct hl_cb *cb; |
| 209 | u32 handle; |
| 210 | int rc = 0; |
| 211 | |
| 212 | /* |
| 213 | * handle was given to user to do mmap, I need to shift it back to |
| 214 | * how the idr module gave it to me |
| 215 | */ |
| 216 | cb_handle >>= PAGE_SHIFT; |
| 217 | handle = (u32) cb_handle; |
| 218 | |
| 219 | spin_lock(&mgr->cb_lock); |
| 220 | |
| 221 | cb = idr_find(&mgr->cb_handles, handle); |
| 222 | if (cb) { |
| 223 | idr_remove(&mgr->cb_handles, handle); |
| 224 | spin_unlock(&mgr->cb_lock); |
| 225 | kref_put(&cb->refcount, cb_release); |
| 226 | } else { |
| 227 | spin_unlock(&mgr->cb_lock); |
| 228 | dev_err(hdev->dev, |
| 229 | "CB destroy failed, no match to handle 0x%x\n", handle); |
| 230 | rc = -EINVAL; |
| 231 | } |
| 232 | |
| 233 | return rc; |
| 234 | } |
| 235 | |
| 236 | int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data) |
| 237 | { |
| 238 | union hl_cb_args *args = data; |
| 239 | struct hl_device *hdev = hpriv->hdev; |
Oded Gabbay | 39b4251 | 2020-04-17 12:12:13 +0300 | [diff] [blame] | 240 | u64 handle = 0; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 241 | int rc; |
| 242 | |
Oded Gabbay | 3f5398c | 2019-04-06 15:41:35 +0300 | [diff] [blame] | 243 | if (hl_device_disabled_or_in_reset(hdev)) { |
| 244 | dev_warn_ratelimited(hdev->dev, |
| 245 | "Device is %s. Can't execute CB IOCTL\n", |
| 246 | atomic_read(&hdev->in_reset) ? "in_reset" : "disabled"); |
| 247 | return -EBUSY; |
| 248 | } |
| 249 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 250 | switch (args->in.op) { |
| 251 | case HL_CB_OP_CREATE: |
Oded Gabbay | 39b4251 | 2020-04-17 12:12:13 +0300 | [diff] [blame] | 252 | if (args->in.cb_size > HL_MAX_CB_SIZE) { |
| 253 | dev_err(hdev->dev, |
| 254 | "User requested CB size %d must be less than %d\n", |
| 255 | args->in.cb_size, HL_MAX_CB_SIZE); |
| 256 | rc = -EINVAL; |
| 257 | } else { |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 258 | rc = hl_cb_create(hdev, &hpriv->cb_mgr, hpriv->ctx, |
| 259 | args->in.cb_size, false, &handle); |
Oded Gabbay | 39b4251 | 2020-04-17 12:12:13 +0300 | [diff] [blame] | 260 | } |
| 261 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 262 | memset(args, 0, sizeof(*args)); |
| 263 | args->out.cb_handle = handle; |
| 264 | break; |
Oded Gabbay | 39b4251 | 2020-04-17 12:12:13 +0300 | [diff] [blame] | 265 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 266 | case HL_CB_OP_DESTROY: |
| 267 | rc = hl_cb_destroy(hdev, &hpriv->cb_mgr, |
| 268 | args->in.cb_handle); |
| 269 | break; |
Oded Gabbay | 39b4251 | 2020-04-17 12:12:13 +0300 | [diff] [blame] | 270 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 271 | default: |
| 272 | rc = -ENOTTY; |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | return rc; |
| 277 | } |
| 278 | |
| 279 | static void cb_vm_close(struct vm_area_struct *vma) |
| 280 | { |
| 281 | struct hl_cb *cb = (struct hl_cb *) vma->vm_private_data; |
Oded Gabbay | 9e28c17 | 2019-02-28 10:46:19 +0200 | [diff] [blame] | 282 | long new_mmap_size; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 283 | |
Oded Gabbay | 9e28c17 | 2019-02-28 10:46:19 +0200 | [diff] [blame] | 284 | new_mmap_size = cb->mmap_size - (vma->vm_end - vma->vm_start); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 285 | |
Oded Gabbay | 9e28c17 | 2019-02-28 10:46:19 +0200 | [diff] [blame] | 286 | if (new_mmap_size > 0) { |
| 287 | cb->mmap_size = new_mmap_size; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 288 | return; |
Oded Gabbay | 9e28c17 | 2019-02-28 10:46:19 +0200 | [diff] [blame] | 289 | } |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 290 | |
| 291 | spin_lock(&cb->lock); |
| 292 | cb->mmap = false; |
| 293 | spin_unlock(&cb->lock); |
| 294 | |
| 295 | hl_cb_put(cb); |
| 296 | vma->vm_private_data = NULL; |
| 297 | } |
| 298 | |
| 299 | static const struct vm_operations_struct cb_vm_ops = { |
| 300 | .close = cb_vm_close |
| 301 | }; |
| 302 | |
| 303 | int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma) |
| 304 | { |
| 305 | struct hl_device *hdev = hpriv->hdev; |
| 306 | struct hl_cb *cb; |
Ofir Bitton | bf6d109 | 2020-07-30 10:00:10 +0300 | [diff] [blame] | 307 | u32 handle, user_cb_size; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 308 | int rc; |
| 309 | |
Oded Gabbay | c5e0ec6 | 2020-08-29 11:55:15 +0300 | [diff] [blame] | 310 | /* We use the page offset to hold the idr and thus we need to clear |
| 311 | * it before doing the mmap itself |
| 312 | */ |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 313 | handle = vma->vm_pgoff; |
Oded Gabbay | c5e0ec6 | 2020-08-29 11:55:15 +0300 | [diff] [blame] | 314 | vma->vm_pgoff = 0; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 315 | |
| 316 | /* reference was taken here */ |
| 317 | cb = hl_cb_get(hdev, &hpriv->cb_mgr, handle); |
| 318 | if (!cb) { |
| 319 | dev_err(hdev->dev, |
Dotan Barak | 0a62c39 | 2020-04-28 08:43:19 +0300 | [diff] [blame] | 320 | "CB mmap failed, no match to handle 0x%x\n", handle); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | } |
| 323 | |
| 324 | /* Validation check */ |
Ofir Bitton | bf6d109 | 2020-07-30 10:00:10 +0300 | [diff] [blame] | 325 | user_cb_size = vma->vm_end - vma->vm_start; |
| 326 | if (user_cb_size != ALIGN(cb->size, PAGE_SIZE)) { |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 327 | dev_err(hdev->dev, |
| 328 | "CB mmap failed, mmap size 0x%lx != 0x%x cb size\n", |
| 329 | vma->vm_end - vma->vm_start, cb->size); |
| 330 | rc = -EINVAL; |
| 331 | goto put_cb; |
| 332 | } |
| 333 | |
Ofir Bitton | bf6d109 | 2020-07-30 10:00:10 +0300 | [diff] [blame] | 334 | if (!access_ok((void __user *) (uintptr_t) vma->vm_start, |
| 335 | user_cb_size)) { |
| 336 | dev_err(hdev->dev, |
| 337 | "user pointer is invalid - 0x%lx\n", |
| 338 | vma->vm_start); |
| 339 | |
| 340 | rc = -EINVAL; |
| 341 | goto put_cb; |
| 342 | } |
| 343 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 344 | spin_lock(&cb->lock); |
| 345 | |
| 346 | if (cb->mmap) { |
| 347 | dev_err(hdev->dev, |
| 348 | "CB mmap failed, CB already mmaped to user\n"); |
| 349 | rc = -EINVAL; |
| 350 | goto release_lock; |
| 351 | } |
| 352 | |
| 353 | cb->mmap = true; |
| 354 | |
| 355 | spin_unlock(&cb->lock); |
| 356 | |
| 357 | vma->vm_ops = &cb_vm_ops; |
| 358 | |
| 359 | /* |
| 360 | * Note: We're transferring the cb reference to |
| 361 | * vma->vm_private_data here. |
| 362 | */ |
| 363 | |
| 364 | vma->vm_private_data = cb; |
| 365 | |
Hillf Danton | 0db5753 | 2020-08-23 07:32:42 +0800 | [diff] [blame] | 366 | rc = hdev->asic_funcs->cb_mmap(hdev, vma, (void *) cb->kernel_address, |
| 367 | cb->bus_address, cb->size); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 368 | if (rc) { |
| 369 | spin_lock(&cb->lock); |
| 370 | cb->mmap = false; |
| 371 | goto release_lock; |
| 372 | } |
| 373 | |
| 374 | cb->mmap_size = cb->size; |
| 375 | |
| 376 | return 0; |
| 377 | |
| 378 | release_lock: |
| 379 | spin_unlock(&cb->lock); |
| 380 | put_cb: |
| 381 | hl_cb_put(cb); |
| 382 | return rc; |
| 383 | } |
| 384 | |
| 385 | struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr, |
| 386 | u32 handle) |
| 387 | { |
| 388 | struct hl_cb *cb; |
| 389 | |
| 390 | spin_lock(&mgr->cb_lock); |
| 391 | cb = idr_find(&mgr->cb_handles, handle); |
| 392 | |
| 393 | if (!cb) { |
| 394 | spin_unlock(&mgr->cb_lock); |
| 395 | dev_warn(hdev->dev, |
Dotan Barak | 0a62c39 | 2020-04-28 08:43:19 +0300 | [diff] [blame] | 396 | "CB get failed, no match to handle 0x%x\n", handle); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | kref_get(&cb->refcount); |
| 401 | |
| 402 | spin_unlock(&mgr->cb_lock); |
| 403 | |
| 404 | return cb; |
| 405 | |
| 406 | } |
| 407 | |
| 408 | void hl_cb_put(struct hl_cb *cb) |
| 409 | { |
| 410 | kref_put(&cb->refcount, cb_release); |
| 411 | } |
| 412 | |
| 413 | void hl_cb_mgr_init(struct hl_cb_mgr *mgr) |
| 414 | { |
| 415 | spin_lock_init(&mgr->cb_lock); |
| 416 | idr_init(&mgr->cb_handles); |
| 417 | } |
| 418 | |
| 419 | void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr) |
| 420 | { |
| 421 | struct hl_cb *cb; |
| 422 | struct idr *idp; |
| 423 | u32 id; |
| 424 | |
| 425 | idp = &mgr->cb_handles; |
| 426 | |
| 427 | idr_for_each_entry(idp, cb, id) { |
| 428 | if (kref_put(&cb->refcount, cb_release) != 1) |
| 429 | dev_err(hdev->dev, |
| 430 | "CB %d for CTX ID %d is still alive\n", |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 431 | id, cb->ctx->asid); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | idr_destroy(&mgr->cb_handles); |
| 435 | } |
| 436 | |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 437 | struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size, |
| 438 | bool internal_cb) |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 439 | { |
| 440 | u64 cb_handle; |
| 441 | struct hl_cb *cb; |
| 442 | int rc; |
| 443 | |
Tomer Tayar | fa8641a1 | 2020-09-07 17:36:41 +0300 | [diff] [blame^] | 444 | rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx, cb_size, |
| 445 | internal_cb, &cb_handle); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 446 | if (rc) { |
Oded Gabbay | 4c172bb | 2019-08-30 16:59:33 +0300 | [diff] [blame] | 447 | dev_err(hdev->dev, |
| 448 | "Failed to allocate CB for the kernel driver %d\n", rc); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 449 | return NULL; |
| 450 | } |
| 451 | |
| 452 | cb_handle >>= PAGE_SHIFT; |
| 453 | cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr, (u32) cb_handle); |
| 454 | /* hl_cb_get should never fail here so use kernel WARN */ |
| 455 | WARN(!cb, "Kernel CB handle invalid 0x%x\n", (u32) cb_handle); |
| 456 | if (!cb) |
| 457 | goto destroy_cb; |
| 458 | |
| 459 | return cb; |
| 460 | |
| 461 | destroy_cb: |
| 462 | hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb_handle << PAGE_SHIFT); |
| 463 | |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | int hl_cb_pool_init(struct hl_device *hdev) |
| 468 | { |
| 469 | struct hl_cb *cb; |
| 470 | int i; |
| 471 | |
| 472 | INIT_LIST_HEAD(&hdev->cb_pool); |
| 473 | spin_lock_init(&hdev->cb_pool_lock); |
| 474 | |
| 475 | for (i = 0 ; i < hdev->asic_prop.cb_pool_cb_cnt ; i++) { |
| 476 | cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size, |
Ofir Bitton | a04b7cd | 2020-07-13 13:36:55 +0300 | [diff] [blame] | 477 | HL_KERNEL_ASID_ID, false); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 478 | if (cb) { |
| 479 | cb->is_pool = true; |
| 480 | list_add(&cb->pool_list, &hdev->cb_pool); |
| 481 | } else { |
| 482 | hl_cb_pool_fini(hdev); |
| 483 | return -ENOMEM; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | int hl_cb_pool_fini(struct hl_device *hdev) |
| 491 | { |
| 492 | struct hl_cb *cb, *tmp; |
| 493 | |
| 494 | list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) { |
| 495 | list_del(&cb->pool_list); |
| 496 | cb_fini(hdev, cb); |
| 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |