blob: 0cb556fb4a8b308b8935bcf9da096a050548cdb8 [file] [log] [blame]
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001// 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 Bittonbf6d1092020-07-30 10:00:10 +030013#include <linux/uaccess.h>
Ofir Bittona04b7cd2020-07-13 13:36:55 +030014#include <linux/genalloc.h>
Oded Gabbaybe5d9262019-02-16 00:39:15 +020015
16static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)
17{
Ofir Bittona04b7cd2020-07-13 13:36:55 +030018 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 Gabbaybe5d9262019-02-16 00:39:15 +020026 kfree(cb);
27}
28
29static 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
40static 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 Gabbayc2164772019-02-16 00:39:24 +020048 hl_debugfs_remove_cb(cb);
49
Tomer Tayarfa8641a12020-09-07 17:36:41 +030050 hl_ctx_put(cb->ctx);
51
Oded Gabbaybe5d9262019-02-16 00:39:15 +020052 cb_do_release(hdev, cb);
53}
54
55static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,
Ofir Bittona04b7cd2020-07-13 13:36:55 +030056 int ctx_id, bool internal_cb)
Oded Gabbaybe5d9262019-02-16 00:39:15 +020057{
58 struct hl_cb *cb;
Ofir Bittona04b7cd2020-07-13 13:36:55 +030059 u32 cb_offset;
Oded Gabbaybe5d9262019-02-16 00:39:15 +020060 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 Bittona04b7cd2020-07-13 13:36:55 +030078 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 Gabbayd9c3aa82019-05-01 11:47:04 +030089 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
Oded Gabbaybe5d9262019-02-16 00:39:15 +020090 &cb->bus_address, GFP_ATOMIC);
Ofir Bittona04b7cd2020-07-13 13:36:55 +030091 } else {
Oded Gabbayd9c3aa82019-05-01 11:47:04 +030092 p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, cb_size,
Oded Gabbaybe5d9262019-02-16 00:39:15 +020093 &cb->bus_address,
94 GFP_USER | __GFP_ZERO);
Ofir Bittona04b7cd2020-07-13 13:36:55 +030095 }
96
Oded Gabbaybe5d9262019-02-16 00:39:15 +020097 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
111int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
Tomer Tayarfa8641a12020-09-07 17:36:41 +0300112 struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
113 u64 *handle)
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200114{
115 struct hl_cb *cb;
116 bool alloc_new_cb = true;
Tomer Tayarfa8641a12020-09-07 17:36:41 +0300117 int rc, ctx_id = ctx->asid;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200118
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200119 /*
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 Gabbaybe5d9262019-02-16 00:39:15 +0200125 dev_warn_ratelimited(hdev->dev,
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200126 "Device is disabled or in reset. Can't create new CBs\n");
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200127 rc = -EBUSY;
128 goto out_err;
129 }
130
Oded Gabbay39b42512020-04-17 12:12:13 +0300131 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 Gabbaybe5d9262019-02-16 00:39:15 +0200134 rc = -EINVAL;
135 goto out_err;
136 }
137
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300138 if (!internal_cb) {
139 /* Minimum allocation must be PAGE SIZE */
140 if (cb_size < PAGE_SIZE)
141 cb_size = PAGE_SIZE;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200142
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300143 if (ctx_id == HL_KERNEL_ASID_ID &&
144 cb_size <= hdev->asic_prop.cb_pool_cb_size) {
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200145
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300146 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 Gabbaybe5d9262019-02-16 00:39:15 +0200157 }
158 }
159
160 if (alloc_new_cb) {
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300161 cb = hl_cb_alloc(hdev, cb_size, ctx_id, internal_cb);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200162 if (!cb) {
163 rc = -ENOMEM;
164 goto out_err;
165 }
166 }
167
168 cb->hdev = hdev;
Tomer Tayarfa8641a12020-09-07 17:36:41 +0300169 cb->ctx = ctx;
170 hl_ctx_get(hdev, cb->ctx);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200171
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 Gabbayf5b9c8c2020-08-12 10:11:20 +0300181 cb->id = (u64) rc;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200182
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 Gabbay3174ac92020-08-29 11:51:39 +0300190 *handle = cb->id | HL_MMAP_TYPE_CB;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200191 *handle <<= PAGE_SHIFT;
192
Oded Gabbayc2164772019-02-16 00:39:24 +0200193 hl_debugfs_add_cb(cb);
194
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200195 return 0;
196
197release_cb:
Tomer Tayarfa8641a12020-09-07 17:36:41 +0300198 hl_ctx_put(cb->ctx);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200199 cb_do_release(hdev, cb);
200out_err:
201 *handle = 0;
202
203 return rc;
204}
205
206int 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
236int 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 Gabbay39b42512020-04-17 12:12:13 +0300240 u64 handle = 0;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200241 int rc;
242
Oded Gabbay3f5398c2019-04-06 15:41:35 +0300243 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 Gabbaybe5d9262019-02-16 00:39:15 +0200250 switch (args->in.op) {
251 case HL_CB_OP_CREATE:
Oded Gabbay39b42512020-04-17 12:12:13 +0300252 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 Tayarfa8641a12020-09-07 17:36:41 +0300258 rc = hl_cb_create(hdev, &hpriv->cb_mgr, hpriv->ctx,
259 args->in.cb_size, false, &handle);
Oded Gabbay39b42512020-04-17 12:12:13 +0300260 }
261
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200262 memset(args, 0, sizeof(*args));
263 args->out.cb_handle = handle;
264 break;
Oded Gabbay39b42512020-04-17 12:12:13 +0300265
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200266 case HL_CB_OP_DESTROY:
267 rc = hl_cb_destroy(hdev, &hpriv->cb_mgr,
268 args->in.cb_handle);
269 break;
Oded Gabbay39b42512020-04-17 12:12:13 +0300270
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200271 default:
272 rc = -ENOTTY;
273 break;
274 }
275
276 return rc;
277}
278
279static void cb_vm_close(struct vm_area_struct *vma)
280{
281 struct hl_cb *cb = (struct hl_cb *) vma->vm_private_data;
Oded Gabbay9e28c172019-02-28 10:46:19 +0200282 long new_mmap_size;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200283
Oded Gabbay9e28c172019-02-28 10:46:19 +0200284 new_mmap_size = cb->mmap_size - (vma->vm_end - vma->vm_start);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200285
Oded Gabbay9e28c172019-02-28 10:46:19 +0200286 if (new_mmap_size > 0) {
287 cb->mmap_size = new_mmap_size;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200288 return;
Oded Gabbay9e28c172019-02-28 10:46:19 +0200289 }
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200290
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
299static const struct vm_operations_struct cb_vm_ops = {
300 .close = cb_vm_close
301};
302
303int 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 Bittonbf6d1092020-07-30 10:00:10 +0300307 u32 handle, user_cb_size;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200308 int rc;
309
Oded Gabbayc5e0ec62020-08-29 11:55:15 +0300310 /* We use the page offset to hold the idr and thus we need to clear
311 * it before doing the mmap itself
312 */
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200313 handle = vma->vm_pgoff;
Oded Gabbayc5e0ec62020-08-29 11:55:15 +0300314 vma->vm_pgoff = 0;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200315
316 /* reference was taken here */
317 cb = hl_cb_get(hdev, &hpriv->cb_mgr, handle);
318 if (!cb) {
319 dev_err(hdev->dev,
Dotan Barak0a62c392020-04-28 08:43:19 +0300320 "CB mmap failed, no match to handle 0x%x\n", handle);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200321 return -EINVAL;
322 }
323
324 /* Validation check */
Ofir Bittonbf6d1092020-07-30 10:00:10 +0300325 user_cb_size = vma->vm_end - vma->vm_start;
326 if (user_cb_size != ALIGN(cb->size, PAGE_SIZE)) {
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200327 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 Bittonbf6d1092020-07-30 10:00:10 +0300334 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 Gabbaybe5d9262019-02-16 00:39:15 +0200344 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 Danton0db57532020-08-23 07:32:42 +0800366 rc = hdev->asic_funcs->cb_mmap(hdev, vma, (void *) cb->kernel_address,
367 cb->bus_address, cb->size);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200368 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
378release_lock:
379 spin_unlock(&cb->lock);
380put_cb:
381 hl_cb_put(cb);
382 return rc;
383}
384
385struct 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 Barak0a62c392020-04-28 08:43:19 +0300396 "CB get failed, no match to handle 0x%x\n", handle);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200397 return NULL;
398 }
399
400 kref_get(&cb->refcount);
401
402 spin_unlock(&mgr->cb_lock);
403
404 return cb;
405
406}
407
408void hl_cb_put(struct hl_cb *cb)
409{
410 kref_put(&cb->refcount, cb_release);
411}
412
413void 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
419void 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 Tayarfa8641a12020-09-07 17:36:41 +0300431 id, cb->ctx->asid);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200432 }
433
434 idr_destroy(&mgr->cb_handles);
435}
436
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300437struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
438 bool internal_cb)
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200439{
440 u64 cb_handle;
441 struct hl_cb *cb;
442 int rc;
443
Tomer Tayarfa8641a12020-09-07 17:36:41 +0300444 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx, cb_size,
445 internal_cb, &cb_handle);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200446 if (rc) {
Oded Gabbay4c172bb2019-08-30 16:59:33 +0300447 dev_err(hdev->dev,
448 "Failed to allocate CB for the kernel driver %d\n", rc);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200449 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
461destroy_cb:
462 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb_handle << PAGE_SHIFT);
463
464 return NULL;
465}
466
467int 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 Bittona04b7cd2020-07-13 13:36:55 +0300477 HL_KERNEL_ASID_ID, false);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200478 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
490int 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}