blob: e6412e13145e04bdfbb7890daa7f34748c7b2f2e [file] [log] [blame]
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
Omer Shpigelman0feaf862019-02-16 00:39:22 +02008#include <uapi/misc/habanalabs.h>
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02009#include "habanalabs.h"
Omer Shpigelman0feaf862019-02-16 00:39:22 +020010#include "include/hw_ip/mmu/mmu_general.h"
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020011
12#include <linux/uaccess.h>
13#include <linux/slab.h>
Omer Shpigelman0feaf862019-02-16 00:39:22 +020014#include <linux/genalloc.h>
15
Omer Shpigelman0feaf862019-02-16 00:39:22 +020016#define HL_MMU_DEBUG 0
17
18/*
19 * The va ranges in context object contain a list with the available chunks of
20 * device virtual memory.
21 * There is one range for host allocations and one for DRAM allocations.
22 *
23 * On initialization each range contains one chunk of all of its available
24 * virtual range which is a half of the total device virtual range.
25 *
26 * On each mapping of physical pages, a suitable virtual range chunk (with a
27 * minimum size) is selected from the list. If the chunk size equals the
28 * requested size, the chunk is returned. Otherwise, the chunk is split into
29 * two chunks - one to return as result and a remainder to stay in the list.
30 *
31 * On each Unmapping of a virtual address, the relevant virtual chunk is
32 * returned to the list. The chunk is added to the list and if its edges match
33 * the edges of the adjacent chunks (means a contiguous chunk can be created),
34 * the chunks are merged.
35 *
36 * On finish, the list is checked to have only one chunk of all the relevant
37 * virtual range (which is a half of the device total virtual range).
38 * If not (means not all mappings were unmapped), a warning is printed.
39 */
40
41/*
42 * alloc_device_memory - allocate device memory
43 *
44 * @ctx : current context
45 * @args : host parameters containing the requested size
46 * @ret_handle : result handle
47 *
48 * This function does the following:
49 * - Allocate the requested size rounded up to 2MB pages
50 * - Return unique handle
51 */
52static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
53 u32 *ret_handle)
54{
55 struct hl_device *hdev = ctx->hdev;
56 struct hl_vm *vm = &hdev->vm;
57 struct hl_vm_phys_pg_pack *phys_pg_pack;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +020058 u64 paddr = 0, total_size, num_pgs, i;
59 u32 num_curr_pgs, page_size, page_shift;
60 int handle, rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +020061 bool contiguous;
62
63 num_curr_pgs = 0;
64 page_size = hdev->asic_prop.dram_page_size;
65 page_shift = __ffs(page_size);
66 num_pgs = (args->alloc.mem_size + (page_size - 1)) >> page_shift;
67 total_size = num_pgs << page_shift;
68
69 contiguous = args->flags & HL_MEM_CONTIGUOUS;
70
71 if (contiguous) {
72 paddr = (u64) gen_pool_alloc(vm->dram_pg_pool, total_size);
73 if (!paddr) {
74 dev_err(hdev->dev,
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +020075 "failed to allocate %llu huge contiguous pages\n",
Omer Shpigelman0feaf862019-02-16 00:39:22 +020076 num_pgs);
77 return -ENOMEM;
78 }
79 }
80
81 phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL);
82 if (!phys_pg_pack) {
83 rc = -ENOMEM;
84 goto pages_pack_err;
85 }
86
87 phys_pg_pack->vm_type = VM_TYPE_PHYS_PACK;
88 phys_pg_pack->asid = ctx->asid;
89 phys_pg_pack->npages = num_pgs;
90 phys_pg_pack->page_size = page_size;
91 phys_pg_pack->total_size = total_size;
92 phys_pg_pack->flags = args->flags;
93 phys_pg_pack->contiguous = contiguous;
94
Omer Shpigelman4eb1d122019-03-07 15:47:19 +020095 phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL);
Omer Shpigelman0feaf862019-02-16 00:39:22 +020096 if (!phys_pg_pack->pages) {
97 rc = -ENOMEM;
98 goto pages_arr_err;
99 }
100
101 if (phys_pg_pack->contiguous) {
102 for (i = 0 ; i < num_pgs ; i++)
103 phys_pg_pack->pages[i] = paddr + i * page_size;
104 } else {
105 for (i = 0 ; i < num_pgs ; i++) {
106 phys_pg_pack->pages[i] = (u64) gen_pool_alloc(
107 vm->dram_pg_pool,
108 page_size);
109 if (!phys_pg_pack->pages[i]) {
110 dev_err(hdev->dev,
Oded Gabbaycab8e3e2019-03-27 09:44:28 +0200111 "Failed to allocate device memory (out of memory)\n");
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200112 rc = -ENOMEM;
113 goto page_err;
114 }
115
116 num_curr_pgs++;
117 }
118 }
119
120 spin_lock(&vm->idr_lock);
121 handle = idr_alloc(&vm->phys_pg_pack_handles, phys_pg_pack, 1, 0,
Wei Yongjun668ae722019-02-22 05:46:01 +0000122 GFP_ATOMIC);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200123 spin_unlock(&vm->idr_lock);
124
125 if (handle < 0) {
126 dev_err(hdev->dev, "Failed to get handle for page\n");
127 rc = -EFAULT;
128 goto idr_err;
129 }
130
131 for (i = 0 ; i < num_pgs ; i++)
132 kref_get(&vm->dram_pg_pool_refcount);
133
134 phys_pg_pack->handle = handle;
135
136 atomic64_add(phys_pg_pack->total_size, &ctx->dram_phys_mem);
137 atomic64_add(phys_pg_pack->total_size, &hdev->dram_used_mem);
138
139 *ret_handle = handle;
140
141 return 0;
142
143idr_err:
144page_err:
145 if (!phys_pg_pack->contiguous)
146 for (i = 0 ; i < num_curr_pgs ; i++)
147 gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[i],
148 page_size);
149
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200150 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200151pages_arr_err:
152 kfree(phys_pg_pack);
153pages_pack_err:
154 if (contiguous)
155 gen_pool_free(vm->dram_pg_pool, paddr, total_size);
156
157 return rc;
158}
159
160/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300161 * dma_map_host_va - DMA mapping of the given host virtual address.
162 * @hdev: habanalabs device structure
163 * @addr: the host virtual address of the memory area
164 * @size: the size of the memory area
165 * @p_userptr: pointer to result userptr structure
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200166 *
167 * This function does the following:
168 * - Allocate userptr structure
169 * - Pin the given host memory using the userptr structure
170 * - Perform DMA mapping to have the DMA addresses of the pages
171 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300172static int dma_map_host_va(struct hl_device *hdev, u64 addr, u64 size,
173 struct hl_userptr **p_userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200174{
175 struct hl_userptr *userptr;
176 int rc;
177
178 userptr = kzalloc(sizeof(*userptr), GFP_KERNEL);
179 if (!userptr) {
180 rc = -ENOMEM;
181 goto userptr_err;
182 }
183
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300184 rc = hl_pin_host_memory(hdev, addr, size, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200185 if (rc) {
186 dev_err(hdev->dev, "Failed to pin host memory\n");
187 goto pin_err;
188 }
189
190 rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl,
191 userptr->sgt->nents, DMA_BIDIRECTIONAL);
192 if (rc) {
193 dev_err(hdev->dev, "failed to map sgt with DMA region\n");
194 goto dma_map_err;
195 }
196
197 userptr->dma_mapped = true;
198 userptr->dir = DMA_BIDIRECTIONAL;
199 userptr->vm_type = VM_TYPE_USERPTR;
200
201 *p_userptr = userptr;
202
203 return 0;
204
205dma_map_err:
206 hl_unpin_host_memory(hdev, userptr);
207pin_err:
208 kfree(userptr);
209userptr_err:
210
211 return rc;
212}
213
214/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300215 * dma_unmap_host_va - DMA unmapping of the given host virtual address.
216 * @hdev: habanalabs device structure
217 * @userptr: userptr to free
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200218 *
219 * This function does the following:
220 * - Unpins the physical pages
221 * - Frees the userptr structure
222 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300223static void dma_unmap_host_va(struct hl_device *hdev,
224 struct hl_userptr *userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200225{
226 hl_unpin_host_memory(hdev, userptr);
227 kfree(userptr);
228}
229
230/*
231 * dram_pg_pool_do_release - free DRAM pages pool
232 *
233 * @ref : pointer to reference object
234 *
235 * This function does the following:
236 * - Frees the idr structure of physical pages handles
237 * - Frees the generic pool of DRAM physical pages
238 */
239static void dram_pg_pool_do_release(struct kref *ref)
240{
241 struct hl_vm *vm = container_of(ref, struct hl_vm,
242 dram_pg_pool_refcount);
243
244 /*
245 * free the idr here as only here we know for sure that there are no
246 * allocated physical pages and hence there are no handles in use
247 */
248 idr_destroy(&vm->phys_pg_pack_handles);
249 gen_pool_destroy(vm->dram_pg_pool);
250}
251
252/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300253 * free_phys_pg_pack - free physical page pack
254 * @hdev: habanalabs device structure
255 * @phys_pg_pack: physical page pack to free
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200256 *
257 * This function does the following:
258 * - For DRAM memory only, iterate over the pack and free each physical block
259 * structure by returning it to the general pool
260 * - Free the hl_vm_phys_pg_pack structure
261 */
262static void free_phys_pg_pack(struct hl_device *hdev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300263 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200264{
265 struct hl_vm *vm = &hdev->vm;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200266 u64 i;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200267
268 if (!phys_pg_pack->created_from_userptr) {
269 if (phys_pg_pack->contiguous) {
270 gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[0],
271 phys_pg_pack->total_size);
272
273 for (i = 0; i < phys_pg_pack->npages ; i++)
274 kref_put(&vm->dram_pg_pool_refcount,
275 dram_pg_pool_do_release);
276 } else {
277 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
278 gen_pool_free(vm->dram_pg_pool,
279 phys_pg_pack->pages[i],
280 phys_pg_pack->page_size);
281 kref_put(&vm->dram_pg_pool_refcount,
282 dram_pg_pool_do_release);
283 }
284 }
285 }
286
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200287 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200288 kfree(phys_pg_pack);
289}
290
291/*
292 * free_device_memory - free device memory
293 *
294 * @ctx : current context
295 * @handle : handle of the memory chunk to free
296 *
297 * This function does the following:
298 * - Free the device memory related to the given handle
299 */
300static int free_device_memory(struct hl_ctx *ctx, u32 handle)
301{
302 struct hl_device *hdev = ctx->hdev;
303 struct hl_vm *vm = &hdev->vm;
304 struct hl_vm_phys_pg_pack *phys_pg_pack;
305
306 spin_lock(&vm->idr_lock);
307 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
308 if (phys_pg_pack) {
309 if (atomic_read(&phys_pg_pack->mapping_cnt) > 0) {
310 dev_err(hdev->dev, "handle %u is mapped, cannot free\n",
311 handle);
312 spin_unlock(&vm->idr_lock);
313 return -EINVAL;
314 }
315
316 /*
317 * must remove from idr before the freeing of the physical
318 * pages as the refcount of the pool is also the trigger of the
319 * idr destroy
320 */
321 idr_remove(&vm->phys_pg_pack_handles, handle);
322 spin_unlock(&vm->idr_lock);
323
324 atomic64_sub(phys_pg_pack->total_size, &ctx->dram_phys_mem);
325 atomic64_sub(phys_pg_pack->total_size, &hdev->dram_used_mem);
326
327 free_phys_pg_pack(hdev, phys_pg_pack);
328 } else {
329 spin_unlock(&vm->idr_lock);
330 dev_err(hdev->dev,
331 "free device memory failed, no match for handle %u\n",
332 handle);
333 return -EINVAL;
334 }
335
336 return 0;
337}
338
339/*
340 * clear_va_list_locked - free virtual addresses list
341 *
342 * @hdev : habanalabs device structure
343 * @va_list : list of virtual addresses to free
344 *
345 * This function does the following:
346 * - Iterate over the list and free each virtual addresses block
347 *
348 * This function should be called only when va_list lock is taken
349 */
350static void clear_va_list_locked(struct hl_device *hdev,
351 struct list_head *va_list)
352{
353 struct hl_vm_va_block *va_block, *tmp;
354
355 list_for_each_entry_safe(va_block, tmp, va_list, node) {
356 list_del(&va_block->node);
357 kfree(va_block);
358 }
359}
360
361/*
362 * print_va_list_locked - print virtual addresses list
363 *
364 * @hdev : habanalabs device structure
365 * @va_list : list of virtual addresses to print
366 *
367 * This function does the following:
368 * - Iterate over the list and print each virtual addresses block
369 *
370 * This function should be called only when va_list lock is taken
371 */
372static void print_va_list_locked(struct hl_device *hdev,
373 struct list_head *va_list)
374{
375#if HL_MMU_DEBUG
376 struct hl_vm_va_block *va_block;
377
378 dev_dbg(hdev->dev, "print va list:\n");
379
380 list_for_each_entry(va_block, va_list, node)
381 dev_dbg(hdev->dev,
382 "va block, start: 0x%llx, end: 0x%llx, size: %llu\n",
383 va_block->start, va_block->end, va_block->size);
384#endif
385}
386
387/*
388 * merge_va_blocks_locked - merge a virtual block if possible
389 *
390 * @hdev : pointer to the habanalabs device structure
391 * @va_list : pointer to the virtual addresses block list
392 * @va_block : virtual block to merge with adjacent blocks
393 *
394 * This function does the following:
395 * - Merge the given blocks with the adjacent blocks if their virtual ranges
396 * create a contiguous virtual range
397 *
398 * This Function should be called only when va_list lock is taken
399 */
400static void merge_va_blocks_locked(struct hl_device *hdev,
401 struct list_head *va_list, struct hl_vm_va_block *va_block)
402{
403 struct hl_vm_va_block *prev, *next;
404
405 prev = list_prev_entry(va_block, node);
406 if (&prev->node != va_list && prev->end + 1 == va_block->start) {
407 prev->end = va_block->end;
408 prev->size = prev->end - prev->start;
409 list_del(&va_block->node);
410 kfree(va_block);
411 va_block = prev;
412 }
413
414 next = list_next_entry(va_block, node);
415 if (&next->node != va_list && va_block->end + 1 == next->start) {
416 next->start = va_block->start;
417 next->size = next->end - next->start;
418 list_del(&va_block->node);
419 kfree(va_block);
420 }
421}
422
423/*
424 * add_va_block_locked - add a virtual block to the virtual addresses list
425 *
426 * @hdev : pointer to the habanalabs device structure
427 * @va_list : pointer to the virtual addresses block list
428 * @start : start virtual address
429 * @end : end virtual address
430 *
431 * This function does the following:
432 * - Add the given block to the virtual blocks list and merge with other
433 * blocks if a contiguous virtual block can be created
434 *
435 * This Function should be called only when va_list lock is taken
436 */
437static int add_va_block_locked(struct hl_device *hdev,
438 struct list_head *va_list, u64 start, u64 end)
439{
440 struct hl_vm_va_block *va_block, *res = NULL;
441 u64 size = end - start;
442
443 print_va_list_locked(hdev, va_list);
444
445 list_for_each_entry(va_block, va_list, node) {
446 /* TODO: remove upon matureness */
447 if (hl_mem_area_crosses_range(start, size, va_block->start,
448 va_block->end)) {
449 dev_err(hdev->dev,
450 "block crossing ranges at start 0x%llx, end 0x%llx\n",
451 va_block->start, va_block->end);
452 return -EINVAL;
453 }
454
455 if (va_block->end < start)
456 res = va_block;
457 }
458
459 va_block = kmalloc(sizeof(*va_block), GFP_KERNEL);
460 if (!va_block)
461 return -ENOMEM;
462
463 va_block->start = start;
464 va_block->end = end;
465 va_block->size = size;
466
467 if (!res)
468 list_add(&va_block->node, va_list);
469 else
470 list_add(&va_block->node, &res->node);
471
472 merge_va_blocks_locked(hdev, va_list, va_block);
473
474 print_va_list_locked(hdev, va_list);
475
476 return 0;
477}
478
479/*
480 * add_va_block - wrapper for add_va_block_locked
481 *
482 * @hdev : pointer to the habanalabs device structure
483 * @va_list : pointer to the virtual addresses block list
484 * @start : start virtual address
485 * @end : end virtual address
486 *
487 * This function does the following:
488 * - Takes the list lock and calls add_va_block_locked
489 */
490static inline int add_va_block(struct hl_device *hdev,
491 struct hl_va_range *va_range, u64 start, u64 end)
492{
493 int rc;
494
495 mutex_lock(&va_range->lock);
496 rc = add_va_block_locked(hdev, &va_range->list, start, end);
497 mutex_unlock(&va_range->lock);
498
499 return rc;
500}
501
502/*
503 * get_va_block - get a virtual block with the requested size
504 *
505 * @hdev : pointer to the habanalabs device structure
506 * @va_range : pointer to the virtual addresses range
507 * @size : requested block size
508 * @hint_addr : hint for request address by the user
509 * @is_userptr : is host or DRAM memory
510 *
511 * This function does the following:
512 * - Iterate on the virtual block list to find a suitable virtual block for the
513 * requested size
514 * - Reserve the requested block and update the list
515 * - Return the start address of the virtual block
516 */
517static u64 get_va_block(struct hl_device *hdev,
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000518 struct hl_va_range *va_range, u64 size, u64 hint_addr,
519 bool is_userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200520{
521 struct hl_vm_va_block *va_block, *new_va_block = NULL;
522 u64 valid_start, valid_size, prev_start, prev_end, page_mask,
523 res_valid_start = 0, res_valid_size = 0;
524 u32 page_size;
525 bool add_prev = false;
526
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000527 if (is_userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200528 /*
529 * We cannot know if the user allocated memory with huge pages
530 * or not, hence we continue with the biggest possible
531 * granularity.
532 */
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000533 page_size = hdev->asic_prop.pmmu.huge_page_size;
534 else
535 page_size = hdev->asic_prop.dmmu.page_size;
536
537 page_mask = ~((u64)page_size - 1);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200538
539 mutex_lock(&va_range->lock);
540
541 print_va_list_locked(hdev, &va_range->list);
542
543 list_for_each_entry(va_block, &va_range->list, node) {
544 /* calc the first possible aligned addr */
545 valid_start = va_block->start;
546
547
548 if (valid_start & (page_size - 1)) {
549 valid_start &= page_mask;
550 valid_start += page_size;
551 if (valid_start > va_block->end)
552 continue;
553 }
554
555 valid_size = va_block->end - valid_start;
556
557 if (valid_size >= size &&
558 (!new_va_block || valid_size < res_valid_size)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200559 new_va_block = va_block;
560 res_valid_start = valid_start;
561 res_valid_size = valid_size;
562 }
563
564 if (hint_addr && hint_addr >= valid_start &&
565 ((hint_addr + size) <= va_block->end)) {
566 new_va_block = va_block;
567 res_valid_start = hint_addr;
568 res_valid_size = valid_size;
569 break;
570 }
571 }
572
573 if (!new_va_block) {
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200574 dev_err(hdev->dev, "no available va block for size %llu\n",
575 size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200576 goto out;
577 }
578
579 if (res_valid_start > new_va_block->start) {
580 prev_start = new_va_block->start;
581 prev_end = res_valid_start - 1;
582
583 new_va_block->start = res_valid_start;
584 new_va_block->size = res_valid_size;
585
586 add_prev = true;
587 }
588
589 if (new_va_block->size > size) {
590 new_va_block->start += size;
591 new_va_block->size = new_va_block->end - new_va_block->start;
592 } else {
593 list_del(&new_va_block->node);
594 kfree(new_va_block);
595 }
596
597 if (add_prev)
598 add_va_block_locked(hdev, &va_range->list, prev_start,
599 prev_end);
600
601 print_va_list_locked(hdev, &va_range->list);
602out:
603 mutex_unlock(&va_range->lock);
604
605 return res_valid_start;
606}
607
608/*
609 * get_sg_info - get number of pages and the DMA address from SG list
610 *
611 * @sg : the SG list
612 * @dma_addr : pointer to DMA address to return
613 *
614 * Calculate the number of consecutive pages described by the SG list. Take the
615 * offset of the address in the first page, add to it the length and round it up
616 * to the number of needed pages.
617 */
618static u32 get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
619{
620 *dma_addr = sg_dma_address(sg);
621
622 return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
623 (PAGE_SIZE - 1)) >> PAGE_SHIFT;
624}
625
626/*
627 * init_phys_pg_pack_from_userptr - initialize physical page pack from host
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300628 * memory
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000629 * @ctx: current context
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300630 * @userptr: userptr to initialize from
631 * @pphys_pg_pack: result pointer
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200632 *
633 * This function does the following:
634 * - Pin the physical pages related to the given virtual block
635 * - Create a physical page pack from the physical pages related to the given
636 * virtual block
637 */
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000638static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx,
639 struct hl_userptr *userptr,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300640 struct hl_vm_phys_pg_pack **pphys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200641{
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000642 struct hl_mmu_properties *mmu_prop = &ctx->hdev->asic_prop.pmmu;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200643 struct hl_vm_phys_pg_pack *phys_pg_pack;
644 struct scatterlist *sg;
645 dma_addr_t dma_addr;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200646 u64 page_mask, total_npages;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000647 u32 npages, page_size = PAGE_SIZE,
648 huge_page_size = mmu_prop->huge_page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200649 bool first = true, is_huge_page_opt = true;
650 int rc, i, j;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000651 u32 pgs_in_huge_page = huge_page_size >> __ffs(page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200652
653 phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL);
654 if (!phys_pg_pack)
655 return -ENOMEM;
656
657 phys_pg_pack->vm_type = userptr->vm_type;
658 phys_pg_pack->created_from_userptr = true;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000659 phys_pg_pack->asid = ctx->asid;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200660 atomic_set(&phys_pg_pack->mapping_cnt, 1);
661
662 /* Only if all dma_addrs are aligned to 2MB and their
663 * sizes is at least 2MB, we can use huge page mapping.
664 * We limit the 2MB optimization to this condition,
665 * since later on we acquire the related VA range as one
666 * consecutive block.
667 */
668 total_npages = 0;
669 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
670 npages = get_sg_info(sg, &dma_addr);
671
672 total_npages += npages;
673
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000674 if ((npages % pgs_in_huge_page) ||
675 (dma_addr & (huge_page_size - 1)))
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200676 is_huge_page_opt = false;
677 }
678
679 if (is_huge_page_opt) {
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000680 page_size = huge_page_size;
681 do_div(total_npages, pgs_in_huge_page);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200682 }
683
684 page_mask = ~(((u64) page_size) - 1);
685
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200686 phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64),
687 GFP_KERNEL);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200688 if (!phys_pg_pack->pages) {
689 rc = -ENOMEM;
690 goto page_pack_arr_mem_err;
691 }
692
693 phys_pg_pack->npages = total_npages;
694 phys_pg_pack->page_size = page_size;
695 phys_pg_pack->total_size = total_npages * page_size;
696
697 j = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200698 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
699 npages = get_sg_info(sg, &dma_addr);
700
701 /* align down to physical page size and save the offset */
702 if (first) {
703 first = false;
704 phys_pg_pack->offset = dma_addr & (page_size - 1);
705 dma_addr &= page_mask;
706 }
707
708 while (npages) {
709 phys_pg_pack->pages[j++] = dma_addr;
710 dma_addr += page_size;
711
712 if (is_huge_page_opt)
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000713 npages -= pgs_in_huge_page;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200714 else
715 npages--;
716 }
717 }
718
719 *pphys_pg_pack = phys_pg_pack;
720
721 return 0;
722
723page_pack_arr_mem_err:
724 kfree(phys_pg_pack);
725
726 return rc;
727}
728
729/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300730 * map_phys_pg_pack - maps the physical page pack.
731 * @ctx: current context
732 * @vaddr: start address of the virtual area to map from
733 * @phys_pg_pack: the pack of physical pages to map to
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200734 *
735 * This function does the following:
736 * - Maps each chunk of virtual memory to matching physical chunk
737 * - Stores number of successful mappings in the given argument
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300738 * - Returns 0 on success, error code otherwise
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200739 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300740static int map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
741 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200742{
743 struct hl_device *hdev = ctx->hdev;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200744 u64 next_vaddr = vaddr, paddr, mapped_pg_cnt = 0, i;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200745 u32 page_size = phys_pg_pack->page_size;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200746 int rc = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200747
748 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
749 paddr = phys_pg_pack->pages[i];
750
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200751 rc = hl_mmu_map(ctx, next_vaddr, paddr, page_size);
752 if (rc) {
753 dev_err(hdev->dev,
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200754 "map failed for handle %u, npages: %llu, mapped: %llu",
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200755 phys_pg_pack->handle, phys_pg_pack->npages,
756 mapped_pg_cnt);
757 goto err;
758 }
759
760 mapped_pg_cnt++;
761 next_vaddr += page_size;
762 }
763
764 return 0;
765
766err:
767 next_vaddr = vaddr;
768 for (i = 0 ; i < mapped_pg_cnt ; i++) {
769 if (hl_mmu_unmap(ctx, next_vaddr, page_size))
770 dev_warn_ratelimited(hdev->dev,
771 "failed to unmap handle %u, va: 0x%llx, pa: 0x%llx, page size: %u\n",
772 phys_pg_pack->handle, next_vaddr,
773 phys_pg_pack->pages[i], page_size);
774
775 next_vaddr += page_size;
776 }
777
778 return rc;
779}
780
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300781/*
782 * unmap_phys_pg_pack - unmaps the physical page pack
783 * @ctx: current context
784 * @vaddr: start address of the virtual area to unmap
785 * @phys_pg_pack: the pack of physical pages to unmap
786 */
787static void unmap_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
788 struct hl_vm_phys_pg_pack *phys_pg_pack)
789{
790 struct hl_device *hdev = ctx->hdev;
791 u64 next_vaddr, i;
792 u32 page_size;
793
794 page_size = phys_pg_pack->page_size;
795 next_vaddr = vaddr;
796
797 for (i = 0 ; i < phys_pg_pack->npages ; i++, next_vaddr += page_size) {
798 if (hl_mmu_unmap(ctx, next_vaddr, page_size))
799 dev_warn_ratelimited(hdev->dev,
800 "unmap failed for vaddr: 0x%llx\n", next_vaddr);
801
802 /*
803 * unmapping on Palladium can be really long, so avoid a CPU
804 * soft lockup bug by sleeping a little between unmapping pages
805 */
806 if (hdev->pldm)
807 usleep_range(500, 1000);
808 }
809}
810
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200811static int get_paddr_from_handle(struct hl_ctx *ctx, struct hl_mem_in *args,
812 u64 *paddr)
813{
814 struct hl_device *hdev = ctx->hdev;
815 struct hl_vm *vm = &hdev->vm;
816 struct hl_vm_phys_pg_pack *phys_pg_pack;
817 u32 handle;
818
819 handle = lower_32_bits(args->map_device.handle);
820 spin_lock(&vm->idr_lock);
821 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
822 if (!phys_pg_pack) {
823 spin_unlock(&vm->idr_lock);
824 dev_err(hdev->dev, "no match for handle %u\n", handle);
825 return -EINVAL;
826 }
827
828 *paddr = phys_pg_pack->pages[0];
829
830 spin_unlock(&vm->idr_lock);
831
832 return 0;
833}
834
835/*
836 * map_device_va - map the given memory
837 *
838 * @ctx : current context
839 * @args : host parameters with handle/host virtual address
840 * @device_addr : pointer to result device virtual address
841 *
842 * This function does the following:
843 * - If given a physical device memory handle, map to a device virtual block
844 * and return the start address of this block
845 * - If given a host virtual address and size, find the related physical pages,
846 * map a device virtual block to this pages and return the start address of
847 * this block
848 */
849static int map_device_va(struct hl_ctx *ctx, struct hl_mem_in *args,
850 u64 *device_addr)
851{
852 struct hl_device *hdev = ctx->hdev;
853 struct hl_vm *vm = &hdev->vm;
854 struct hl_vm_phys_pg_pack *phys_pg_pack;
855 struct hl_userptr *userptr = NULL;
856 struct hl_vm_hash_node *hnode;
857 enum vm_type_t *vm_type;
858 u64 ret_vaddr, hint_addr;
859 u32 handle = 0;
860 int rc;
861 bool is_userptr = args->flags & HL_MEM_USERPTR;
862
863 /* Assume failure */
864 *device_addr = 0;
865
866 if (is_userptr) {
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300867 u64 addr = args->map_host.host_virt_addr,
868 size = args->map_host.mem_size;
869
870 rc = dma_map_host_va(hdev, addr, size, &userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200871 if (rc) {
872 dev_err(hdev->dev, "failed to get userptr from va\n");
873 return rc;
874 }
875
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000876 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200877 &phys_pg_pack);
878 if (rc) {
879 dev_err(hdev->dev,
880 "unable to init page pack for vaddr 0x%llx\n",
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300881 addr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200882 goto init_page_pack_err;
883 }
884
885 vm_type = (enum vm_type_t *) userptr;
886 hint_addr = args->map_host.hint_addr;
887 } else {
888 handle = lower_32_bits(args->map_device.handle);
889
890 spin_lock(&vm->idr_lock);
891 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
892 if (!phys_pg_pack) {
893 spin_unlock(&vm->idr_lock);
894 dev_err(hdev->dev,
895 "no match for handle %u\n", handle);
896 return -EINVAL;
897 }
898
899 /* increment now to avoid freeing device memory while mapping */
900 atomic_inc(&phys_pg_pack->mapping_cnt);
901
902 spin_unlock(&vm->idr_lock);
903
904 vm_type = (enum vm_type_t *) phys_pg_pack;
905
906 hint_addr = args->map_device.hint_addr;
907 }
908
909 /*
910 * relevant for mapping device physical memory only, as host memory is
911 * implicitly shared
912 */
913 if (!is_userptr && !(phys_pg_pack->flags & HL_MEM_SHARED) &&
914 phys_pg_pack->asid != ctx->asid) {
915 dev_err(hdev->dev,
916 "Failed to map memory, handle %u is not shared\n",
917 handle);
918 rc = -EPERM;
919 goto shared_err;
920 }
921
922 hnode = kzalloc(sizeof(*hnode), GFP_KERNEL);
923 if (!hnode) {
924 rc = -ENOMEM;
925 goto hnode_err;
926 }
927
928 ret_vaddr = get_va_block(hdev,
929 is_userptr ? &ctx->host_va_range : &ctx->dram_va_range,
930 phys_pg_pack->total_size, hint_addr, is_userptr);
931 if (!ret_vaddr) {
932 dev_err(hdev->dev, "no available va block for handle %u\n",
933 handle);
934 rc = -ENOMEM;
935 goto va_block_err;
936 }
937
938 mutex_lock(&ctx->mmu_lock);
939
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300940 rc = map_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200941 if (rc) {
942 mutex_unlock(&ctx->mmu_lock);
943 dev_err(hdev->dev, "mapping page pack failed for handle %u\n",
944 handle);
945 goto map_err;
946 }
947
Omer Shpigelman7b6e4ea2019-11-14 18:23:53 +0000948 hdev->asic_funcs->mmu_invalidate_cache(hdev, false, *vm_type);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200949
950 mutex_unlock(&ctx->mmu_lock);
951
952 ret_vaddr += phys_pg_pack->offset;
953
954 hnode->ptr = vm_type;
955 hnode->vaddr = ret_vaddr;
956
957 mutex_lock(&ctx->mem_hash_lock);
958 hash_add(ctx->mem_hash, &hnode->node, ret_vaddr);
959 mutex_unlock(&ctx->mem_hash_lock);
960
961 *device_addr = ret_vaddr;
962
963 if (is_userptr)
964 free_phys_pg_pack(hdev, phys_pg_pack);
965
966 return 0;
967
968map_err:
969 if (add_va_block(hdev,
970 is_userptr ? &ctx->host_va_range : &ctx->dram_va_range,
971 ret_vaddr,
972 ret_vaddr + phys_pg_pack->total_size - 1))
973 dev_warn(hdev->dev,
974 "release va block failed for handle 0x%x, vaddr: 0x%llx\n",
975 handle, ret_vaddr);
976
977va_block_err:
978 kfree(hnode);
979hnode_err:
980shared_err:
981 atomic_dec(&phys_pg_pack->mapping_cnt);
982 if (is_userptr)
983 free_phys_pg_pack(hdev, phys_pg_pack);
984init_page_pack_err:
985 if (is_userptr)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300986 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200987
988 return rc;
989}
990
991/*
992 * unmap_device_va - unmap the given device virtual address
993 *
994 * @ctx : current context
995 * @vaddr : device virtual address to unmap
Omer Shpigelman71c5e552019-11-14 18:23:57 +0000996 * @ctx_free : true if in context free flow, false otherwise.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200997 *
998 * This function does the following:
999 * - Unmap the physical pages related to the given virtual address
1000 * - return the device virtual block to the virtual block list
1001 */
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001002static int unmap_device_va(struct hl_ctx *ctx, u64 vaddr, bool ctx_free)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001003{
1004 struct hl_device *hdev = ctx->hdev;
1005 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
1006 struct hl_vm_hash_node *hnode = NULL;
1007 struct hl_userptr *userptr = NULL;
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001008 struct hl_va_range *va_range;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001009 enum vm_type_t *vm_type;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001010 bool is_userptr;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +02001011 int rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001012
1013 /* protect from double entrance */
1014 mutex_lock(&ctx->mem_hash_lock);
1015 hash_for_each_possible(ctx->mem_hash, hnode, node, (unsigned long)vaddr)
1016 if (vaddr == hnode->vaddr)
1017 break;
1018
1019 if (!hnode) {
1020 mutex_unlock(&ctx->mem_hash_lock);
1021 dev_err(hdev->dev,
1022 "unmap failed, no mem hnode for vaddr 0x%llx\n",
1023 vaddr);
1024 return -EINVAL;
1025 }
1026
1027 hash_del(&hnode->node);
1028 mutex_unlock(&ctx->mem_hash_lock);
1029
1030 vm_type = hnode->ptr;
1031
1032 if (*vm_type == VM_TYPE_USERPTR) {
1033 is_userptr = true;
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001034 va_range = &ctx->host_va_range;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001035 userptr = hnode->ptr;
Omer Shpigelman54bb6742019-11-14 18:23:55 +00001036 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001037 &phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001038 if (rc) {
1039 dev_err(hdev->dev,
1040 "unable to init page pack for vaddr 0x%llx\n",
1041 vaddr);
1042 goto vm_type_err;
1043 }
1044 } else if (*vm_type == VM_TYPE_PHYS_PACK) {
1045 is_userptr = false;
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001046 va_range = &ctx->dram_va_range;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001047 phys_pg_pack = hnode->ptr;
1048 } else {
1049 dev_warn(hdev->dev,
1050 "unmap failed, unknown vm desc for vaddr 0x%llx\n",
1051 vaddr);
1052 rc = -EFAULT;
1053 goto vm_type_err;
1054 }
1055
1056 if (atomic_read(&phys_pg_pack->mapping_cnt) == 0) {
1057 dev_err(hdev->dev, "vaddr 0x%llx is not mapped\n", vaddr);
1058 rc = -EINVAL;
1059 goto mapping_cnt_err;
1060 }
1061
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001062 vaddr &= ~(((u64) phys_pg_pack->page_size) - 1);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001063
1064 mutex_lock(&ctx->mmu_lock);
1065
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001066 unmap_phys_pg_pack(ctx, vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001067
Omer Shpigelman7b6e4ea2019-11-14 18:23:53 +00001068 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, *vm_type);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001069
1070 mutex_unlock(&ctx->mmu_lock);
1071
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001072 /*
1073 * No point in maintaining the free VA block list if the context is
1074 * closing as the list will be freed anyway
1075 */
1076 if (!ctx_free) {
1077 rc = add_va_block(hdev, va_range, vaddr,
1078 vaddr + phys_pg_pack->total_size - 1);
1079 if (rc)
1080 dev_warn(hdev->dev,
1081 "add va block failed for vaddr: 0x%llx\n",
1082 vaddr);
1083 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001084
1085 atomic_dec(&phys_pg_pack->mapping_cnt);
1086 kfree(hnode);
1087
1088 if (is_userptr) {
1089 free_phys_pg_pack(hdev, phys_pg_pack);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001090 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001091 }
1092
1093 return 0;
1094
1095mapping_cnt_err:
1096 if (is_userptr)
1097 free_phys_pg_pack(hdev, phys_pg_pack);
1098vm_type_err:
1099 mutex_lock(&ctx->mem_hash_lock);
1100 hash_add(ctx->mem_hash, &hnode->node, vaddr);
1101 mutex_unlock(&ctx->mem_hash_lock);
1102
1103 return rc;
1104}
1105
Oded Gabbay54303a12019-04-04 14:42:26 +03001106static int mem_ioctl_no_mmu(struct hl_fpriv *hpriv, union hl_mem_args *args)
1107{
1108 struct hl_device *hdev = hpriv->hdev;
1109 struct hl_ctx *ctx = hpriv->ctx;
1110 u64 device_addr = 0;
1111 u32 handle = 0;
1112 int rc;
1113
1114 switch (args->in.op) {
1115 case HL_MEM_OP_ALLOC:
1116 if (args->in.alloc.mem_size == 0) {
1117 dev_err(hdev->dev,
1118 "alloc size must be larger than 0\n");
1119 rc = -EINVAL;
1120 goto out;
1121 }
1122
1123 /* Force contiguous as there are no real MMU
1124 * translations to overcome physical memory gaps
1125 */
1126 args->in.flags |= HL_MEM_CONTIGUOUS;
1127 rc = alloc_device_memory(ctx, &args->in, &handle);
1128
1129 memset(args, 0, sizeof(*args));
1130 args->out.handle = (__u64) handle;
1131 break;
1132
1133 case HL_MEM_OP_FREE:
1134 rc = free_device_memory(ctx, args->in.free.handle);
1135 break;
1136
1137 case HL_MEM_OP_MAP:
1138 if (args->in.flags & HL_MEM_USERPTR) {
1139 device_addr = args->in.map_host.host_virt_addr;
1140 rc = 0;
1141 } else {
1142 rc = get_paddr_from_handle(ctx, &args->in,
1143 &device_addr);
1144 }
1145
1146 memset(args, 0, sizeof(*args));
1147 args->out.device_virt_addr = device_addr;
1148 break;
1149
1150 case HL_MEM_OP_UNMAP:
1151 rc = 0;
1152 break;
1153
1154 default:
1155 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1156 rc = -ENOTTY;
1157 break;
1158 }
1159
1160out:
1161 return rc;
1162}
1163
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001164int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
1165{
1166 union hl_mem_args *args = data;
1167 struct hl_device *hdev = hpriv->hdev;
1168 struct hl_ctx *ctx = hpriv->ctx;
1169 u64 device_addr = 0;
1170 u32 handle = 0;
1171 int rc;
1172
1173 if (hl_device_disabled_or_in_reset(hdev)) {
1174 dev_warn_ratelimited(hdev->dev,
Oded Gabbay3f5398c2019-04-06 15:41:35 +03001175 "Device is %s. Can't execute MEMORY IOCTL\n",
1176 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001177 return -EBUSY;
1178 }
1179
Oded Gabbay54303a12019-04-04 14:42:26 +03001180 if (!hdev->mmu_enable)
1181 return mem_ioctl_no_mmu(hpriv, args);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001182
Oded Gabbay54303a12019-04-04 14:42:26 +03001183 switch (args->in.op) {
1184 case HL_MEM_OP_ALLOC:
1185 if (!hdev->dram_supports_virtual_memory) {
1186 dev_err(hdev->dev, "DRAM alloc is not supported\n");
1187 rc = -EINVAL;
1188 goto out;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001189 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001190
Oded Gabbay54303a12019-04-04 14:42:26 +03001191 if (args->in.alloc.mem_size == 0) {
1192 dev_err(hdev->dev,
1193 "alloc size must be larger than 0\n");
1194 rc = -EINVAL;
1195 goto out;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001196 }
Oded Gabbay54303a12019-04-04 14:42:26 +03001197 rc = alloc_device_memory(ctx, &args->in, &handle);
1198
1199 memset(args, 0, sizeof(*args));
1200 args->out.handle = (__u64) handle;
1201 break;
1202
1203 case HL_MEM_OP_FREE:
1204 rc = free_device_memory(ctx, args->in.free.handle);
1205 break;
1206
1207 case HL_MEM_OP_MAP:
1208 rc = map_device_va(ctx, &args->in, &device_addr);
1209
1210 memset(args, 0, sizeof(*args));
1211 args->out.device_virt_addr = device_addr;
1212 break;
1213
1214 case HL_MEM_OP_UNMAP:
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001215 rc = unmap_device_va(ctx, args->in.unmap.device_virt_addr,
1216 false);
Oded Gabbay54303a12019-04-04 14:42:26 +03001217 break;
1218
1219 default:
1220 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1221 rc = -ENOTTY;
1222 break;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001223 }
1224
1225out:
1226 return rc;
1227}
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001228
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001229static int get_user_memory(struct hl_device *hdev, u64 addr, u64 size,
1230 u32 npages, u64 start, u32 offset,
1231 struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001232{
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001233 int rc;
1234
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001235 if (!access_ok((void __user *) (uintptr_t) addr, size)) {
Oded Gabbay230afe72019-02-27 00:19:18 +02001236 dev_err(hdev->dev, "user pointer is invalid - 0x%llx\n", addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001237 return -EFAULT;
1238 }
1239
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001240 userptr->vec = frame_vector_create(npages);
1241 if (!userptr->vec) {
1242 dev_err(hdev->dev, "Failed to create frame vector\n");
1243 return -ENOMEM;
1244 }
1245
1246 rc = get_vaddr_frames(start, npages, FOLL_FORCE | FOLL_WRITE,
1247 userptr->vec);
1248
1249 if (rc != npages) {
1250 dev_err(hdev->dev,
1251 "Failed to map host memory, user ptr probably wrong\n");
1252 if (rc < 0)
1253 goto destroy_framevec;
1254 rc = -EFAULT;
1255 goto put_framevec;
1256 }
1257
1258 if (frame_vector_to_pages(userptr->vec) < 0) {
1259 dev_err(hdev->dev,
1260 "Failed to translate frame vector to pages\n");
1261 rc = -EFAULT;
1262 goto put_framevec;
1263 }
1264
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001265 rc = sg_alloc_table_from_pages(userptr->sgt,
1266 frame_vector_pages(userptr->vec),
1267 npages, offset, size, GFP_ATOMIC);
1268 if (rc < 0) {
1269 dev_err(hdev->dev, "failed to create SG table from pages\n");
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001270 goto put_framevec;
1271 }
1272
1273 return 0;
1274
1275put_framevec:
1276 put_vaddr_frames(userptr->vec);
1277destroy_framevec:
1278 frame_vector_destroy(userptr->vec);
1279 return rc;
1280}
1281
1282/*
1283 * hl_pin_host_memory - pins a chunk of host memory.
1284 * @hdev: pointer to the habanalabs device structure
1285 * @addr: the host virtual address of the memory area
1286 * @size: the size of the memory area
1287 * @userptr: pointer to hl_userptr structure
1288 *
1289 * This function does the following:
1290 * - Pins the physical pages
1291 * - Create an SG list from those pages
1292 */
1293int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
1294 struct hl_userptr *userptr)
1295{
1296 u64 start, end;
1297 u32 npages, offset;
1298 int rc;
1299
1300 if (!size) {
1301 dev_err(hdev->dev, "size to pin is invalid - %llu\n", size);
1302 return -EINVAL;
1303 }
1304
1305 /*
1306 * If the combination of the address and size requested for this memory
1307 * region causes an integer overflow, return error.
1308 */
1309 if (((addr + size) < addr) ||
1310 PAGE_ALIGN(addr + size) < (addr + size)) {
1311 dev_err(hdev->dev,
1312 "user pointer 0x%llx + %llu causes integer overflow\n",
1313 addr, size);
1314 return -EINVAL;
1315 }
1316
1317 /*
1318 * This function can be called also from data path, hence use atomic
1319 * always as it is not a big allocation.
1320 */
1321 userptr->sgt = kzalloc(sizeof(*userptr->sgt), GFP_ATOMIC);
1322 if (!userptr->sgt)
1323 return -ENOMEM;
1324
1325 start = addr & PAGE_MASK;
1326 offset = addr & ~PAGE_MASK;
1327 end = PAGE_ALIGN(addr + size);
1328 npages = (end - start) >> PAGE_SHIFT;
1329
1330 userptr->size = size;
1331 userptr->addr = addr;
1332 userptr->dma_mapped = false;
1333 INIT_LIST_HEAD(&userptr->job_node);
1334
1335 rc = get_user_memory(hdev, addr, size, npages, start, offset,
1336 userptr);
1337 if (rc) {
1338 dev_err(hdev->dev,
1339 "failed to get user memory for address 0x%llx\n",
1340 addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001341 goto free_sgt;
1342 }
1343
Oded Gabbayc2164772019-02-16 00:39:24 +02001344 hl_debugfs_add_userptr(hdev, userptr);
1345
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001346 return 0;
1347
1348free_sgt:
1349 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001350 return rc;
1351}
1352
1353/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001354 * hl_unpin_host_memory - unpins a chunk of host memory.
1355 * @hdev: pointer to the habanalabs device structure
1356 * @userptr: pointer to hl_userptr structure
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001357 *
1358 * This function does the following:
1359 * - Unpins the physical pages related to the host memory
1360 * - Free the SG list
1361 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001362void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001363{
1364 struct page **pages;
1365
Oded Gabbayc2164772019-02-16 00:39:24 +02001366 hl_debugfs_remove_userptr(hdev, userptr);
1367
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001368 if (userptr->dma_mapped)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001369 hdev->asic_funcs->hl_dma_unmap_sg(hdev, userptr->sgt->sgl,
1370 userptr->sgt->nents,
1371 userptr->dir);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001372
1373 pages = frame_vector_pages(userptr->vec);
1374 if (!IS_ERR(pages)) {
1375 int i;
1376
1377 for (i = 0; i < frame_vector_count(userptr->vec); i++)
1378 set_page_dirty_lock(pages[i]);
1379 }
1380 put_vaddr_frames(userptr->vec);
1381 frame_vector_destroy(userptr->vec);
1382
1383 list_del(&userptr->job_node);
1384
1385 sg_free_table(userptr->sgt);
1386 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001387}
1388
1389/*
1390 * hl_userptr_delete_list - clear userptr list
1391 *
1392 * @hdev : pointer to the habanalabs device structure
1393 * @userptr_list : pointer to the list to clear
1394 *
1395 * This function does the following:
1396 * - Iterates over the list and unpins the host memory and frees the userptr
1397 * structure.
1398 */
1399void hl_userptr_delete_list(struct hl_device *hdev,
1400 struct list_head *userptr_list)
1401{
1402 struct hl_userptr *userptr, *tmp;
1403
1404 list_for_each_entry_safe(userptr, tmp, userptr_list, job_node) {
1405 hl_unpin_host_memory(hdev, userptr);
1406 kfree(userptr);
1407 }
1408
1409 INIT_LIST_HEAD(userptr_list);
1410}
1411
1412/*
1413 * hl_userptr_is_pinned - returns whether the given userptr is pinned
1414 *
1415 * @hdev : pointer to the habanalabs device structure
1416 * @userptr_list : pointer to the list to clear
1417 * @userptr : pointer to userptr to check
1418 *
1419 * This function does the following:
1420 * - Iterates over the list and checks if the given userptr is in it, means is
1421 * pinned. If so, returns true, otherwise returns false.
1422 */
1423bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr,
1424 u32 size, struct list_head *userptr_list,
1425 struct hl_userptr **userptr)
1426{
1427 list_for_each_entry((*userptr), userptr_list, job_node) {
1428 if ((addr == (*userptr)->addr) && (size == (*userptr)->size))
1429 return true;
1430 }
1431
1432 return false;
1433}
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001434
1435/*
1436 * hl_va_range_init - initialize virtual addresses range
1437 *
1438 * @hdev : pointer to the habanalabs device structure
1439 * @va_range : pointer to the range to initialize
1440 * @start : range start address
1441 * @end : range end address
1442 *
1443 * This function does the following:
1444 * - Initializes the virtual addresses list of the given range with the given
1445 * addresses.
1446 */
1447static int hl_va_range_init(struct hl_device *hdev,
1448 struct hl_va_range *va_range, u64 start, u64 end)
1449{
1450 int rc;
1451
1452 INIT_LIST_HEAD(&va_range->list);
1453
1454 /* PAGE_SIZE alignment */
1455
1456 if (start & (PAGE_SIZE - 1)) {
1457 start &= PAGE_MASK;
1458 start += PAGE_SIZE;
1459 }
1460
1461 if (end & (PAGE_SIZE - 1))
1462 end &= PAGE_MASK;
1463
1464 if (start >= end) {
1465 dev_err(hdev->dev, "too small vm range for va list\n");
1466 return -EFAULT;
1467 }
1468
1469 rc = add_va_block(hdev, va_range, start, end);
1470
1471 if (rc) {
1472 dev_err(hdev->dev, "Failed to init host va list\n");
1473 return rc;
1474 }
1475
1476 va_range->start_addr = start;
1477 va_range->end_addr = end;
1478
1479 return 0;
1480}
1481
1482/*
1483 * hl_vm_ctx_init_with_ranges - initialize virtual memory for context
1484 *
1485 * @ctx : pointer to the habanalabs context structure
1486 * @host_range_start : host virtual addresses range start
1487 * @host_range_end : host virtual addresses range end
1488 * @dram_range_start : dram virtual addresses range start
1489 * @dram_range_end : dram virtual addresses range end
1490 *
1491 * This function initializes the following:
1492 * - MMU for context
1493 * - Virtual address to area descriptor hashtable
1494 * - Virtual block list of available virtual memory
1495 */
Oded Gabbay5e6e0232019-02-27 12:15:16 +02001496static int hl_vm_ctx_init_with_ranges(struct hl_ctx *ctx, u64 host_range_start,
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001497 u64 host_range_end, u64 dram_range_start,
1498 u64 dram_range_end)
1499{
1500 struct hl_device *hdev = ctx->hdev;
1501 int rc;
1502
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001503 rc = hl_mmu_ctx_init(ctx);
1504 if (rc) {
1505 dev_err(hdev->dev, "failed to init context %d\n", ctx->asid);
1506 return rc;
1507 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001508
1509 mutex_init(&ctx->mem_hash_lock);
1510 hash_init(ctx->mem_hash);
1511
1512 mutex_init(&ctx->host_va_range.lock);
1513
1514 rc = hl_va_range_init(hdev, &ctx->host_va_range, host_range_start,
1515 host_range_end);
1516 if (rc) {
1517 dev_err(hdev->dev, "failed to init host vm range\n");
1518 goto host_vm_err;
1519 }
1520
1521 mutex_init(&ctx->dram_va_range.lock);
1522
1523 rc = hl_va_range_init(hdev, &ctx->dram_va_range, dram_range_start,
1524 dram_range_end);
1525 if (rc) {
1526 dev_err(hdev->dev, "failed to init dram vm range\n");
1527 goto dram_vm_err;
1528 }
1529
Oded Gabbayc2164772019-02-16 00:39:24 +02001530 hl_debugfs_add_ctx_mem_hash(hdev, ctx);
1531
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001532 return 0;
1533
1534dram_vm_err:
1535 mutex_destroy(&ctx->dram_va_range.lock);
1536
1537 mutex_lock(&ctx->host_va_range.lock);
1538 clear_va_list_locked(hdev, &ctx->host_va_range.list);
1539 mutex_unlock(&ctx->host_va_range.lock);
1540host_vm_err:
1541 mutex_destroy(&ctx->host_va_range.lock);
1542 mutex_destroy(&ctx->mem_hash_lock);
1543 hl_mmu_ctx_fini(ctx);
1544
1545 return rc;
1546}
1547
1548int hl_vm_ctx_init(struct hl_ctx *ctx)
1549{
1550 struct asic_fixed_properties *prop = &ctx->hdev->asic_prop;
1551 u64 host_range_start, host_range_end, dram_range_start,
1552 dram_range_end;
1553
1554 atomic64_set(&ctx->dram_phys_mem, 0);
1555
1556 /*
1557 * - If MMU is enabled, init the ranges as usual.
1558 * - If MMU is disabled, in case of host mapping, the returned address
1559 * is the given one.
1560 * In case of DRAM mapping, the returned address is the physical
1561 * address of the memory related to the given handle.
1562 */
1563 if (ctx->hdev->mmu_enable) {
1564 dram_range_start = prop->va_space_dram_start_address;
1565 dram_range_end = prop->va_space_dram_end_address;
1566 host_range_start = prop->va_space_host_start_address;
1567 host_range_end = prop->va_space_host_end_address;
1568 } else {
1569 dram_range_start = prop->dram_user_base_address;
1570 dram_range_end = prop->dram_end_address;
1571 host_range_start = prop->dram_user_base_address;
1572 host_range_end = prop->dram_end_address;
1573 }
1574
1575 return hl_vm_ctx_init_with_ranges(ctx, host_range_start, host_range_end,
1576 dram_range_start, dram_range_end);
1577}
1578
1579/*
1580 * hl_va_range_fini - clear a virtual addresses range
1581 *
1582 * @hdev : pointer to the habanalabs structure
1583 * va_range : pointer to virtual addresses range
1584 *
1585 * This function initializes the following:
1586 * - Checks that the given range contains the whole initial range
1587 * - Frees the virtual addresses block list and its lock
1588 */
1589static void hl_va_range_fini(struct hl_device *hdev,
1590 struct hl_va_range *va_range)
1591{
1592 struct hl_vm_va_block *va_block;
1593
1594 if (list_empty(&va_range->list)) {
1595 dev_warn(hdev->dev,
1596 "va list should not be empty on cleanup!\n");
1597 goto out;
1598 }
1599
1600 if (!list_is_singular(&va_range->list)) {
1601 dev_warn(hdev->dev,
1602 "va list should not contain multiple blocks on cleanup!\n");
1603 goto free_va_list;
1604 }
1605
1606 va_block = list_first_entry(&va_range->list, typeof(*va_block), node);
1607
1608 if (va_block->start != va_range->start_addr ||
1609 va_block->end != va_range->end_addr) {
1610 dev_warn(hdev->dev,
1611 "wrong va block on cleanup, from 0x%llx to 0x%llx\n",
1612 va_block->start, va_block->end);
1613 goto free_va_list;
1614 }
1615
1616free_va_list:
1617 mutex_lock(&va_range->lock);
1618 clear_va_list_locked(hdev, &va_range->list);
1619 mutex_unlock(&va_range->lock);
1620
1621out:
1622 mutex_destroy(&va_range->lock);
1623}
1624
1625/*
1626 * hl_vm_ctx_fini - virtual memory teardown of context
1627 *
1628 * @ctx : pointer to the habanalabs context structure
1629 *
1630 * This function perform teardown the following:
1631 * - Virtual block list of available virtual memory
1632 * - Virtual address to area descriptor hashtable
1633 * - MMU for context
1634 *
1635 * In addition this function does the following:
1636 * - Unmaps the existing hashtable nodes if the hashtable is not empty. The
1637 * hashtable should be empty as no valid mappings should exist at this
1638 * point.
1639 * - Frees any existing physical page list from the idr which relates to the
1640 * current context asid.
1641 * - This function checks the virtual block list for correctness. At this point
1642 * the list should contain one element which describes the whole virtual
1643 * memory range of the context. Otherwise, a warning is printed.
1644 */
1645void hl_vm_ctx_fini(struct hl_ctx *ctx)
1646{
1647 struct hl_device *hdev = ctx->hdev;
1648 struct hl_vm *vm = &hdev->vm;
1649 struct hl_vm_phys_pg_pack *phys_pg_list;
1650 struct hl_vm_hash_node *hnode;
1651 struct hlist_node *tmp_node;
1652 int i;
1653
Oded Gabbayc2164772019-02-16 00:39:24 +02001654 hl_debugfs_remove_ctx_mem_hash(hdev, ctx);
1655
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001656 if (!hash_empty(ctx->mem_hash))
1657 dev_notice(hdev->dev, "ctx is freed while it has va in use\n");
1658
1659 hash_for_each_safe(ctx->mem_hash, i, tmp_node, hnode, node) {
1660 dev_dbg(hdev->dev,
1661 "hl_mem_hash_node of vaddr 0x%llx of asid %d is still alive\n",
1662 hnode->vaddr, ctx->asid);
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001663 unmap_device_va(ctx, hnode->vaddr, true);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001664 }
1665
1666 spin_lock(&vm->idr_lock);
1667 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_list, i)
1668 if (phys_pg_list->asid == ctx->asid) {
1669 dev_dbg(hdev->dev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001670 "page list 0x%px of asid %d is still alive\n",
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001671 phys_pg_list, ctx->asid);
Tomer Tayarc8113752019-08-04 07:03:41 +00001672 atomic64_sub(phys_pg_list->total_size,
1673 &hdev->dram_used_mem);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001674 free_phys_pg_pack(hdev, phys_pg_list);
1675 idr_remove(&vm->phys_pg_pack_handles, i);
1676 }
1677 spin_unlock(&vm->idr_lock);
1678
1679 hl_va_range_fini(hdev, &ctx->dram_va_range);
1680 hl_va_range_fini(hdev, &ctx->host_va_range);
1681
1682 mutex_destroy(&ctx->mem_hash_lock);
1683 hl_mmu_ctx_fini(ctx);
1684}
1685
1686/*
1687 * hl_vm_init - initialize virtual memory module
1688 *
1689 * @hdev : pointer to the habanalabs device structure
1690 *
1691 * This function initializes the following:
1692 * - MMU module
1693 * - DRAM physical pages pool of 2MB
1694 * - Idr for device memory allocation handles
1695 */
1696int hl_vm_init(struct hl_device *hdev)
1697{
1698 struct asic_fixed_properties *prop = &hdev->asic_prop;
1699 struct hl_vm *vm = &hdev->vm;
1700 int rc;
1701
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001702 vm->dram_pg_pool = gen_pool_create(__ffs(prop->dram_page_size), -1);
1703 if (!vm->dram_pg_pool) {
1704 dev_err(hdev->dev, "Failed to create dram page pool\n");
Oded Gabbay37d68ce2019-05-29 14:43:04 +03001705 return -ENOMEM;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001706 }
1707
1708 kref_init(&vm->dram_pg_pool_refcount);
1709
1710 rc = gen_pool_add(vm->dram_pg_pool, prop->dram_user_base_address,
1711 prop->dram_end_address - prop->dram_user_base_address,
1712 -1);
1713
1714 if (rc) {
1715 dev_err(hdev->dev,
1716 "Failed to add memory to dram page pool %d\n", rc);
1717 goto pool_add_err;
1718 }
1719
1720 spin_lock_init(&vm->idr_lock);
1721 idr_init(&vm->phys_pg_pack_handles);
1722
1723 atomic64_set(&hdev->dram_used_mem, 0);
1724
1725 vm->init_done = true;
1726
1727 return 0;
1728
1729pool_add_err:
1730 gen_pool_destroy(vm->dram_pg_pool);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001731
1732 return rc;
1733}
1734
1735/*
1736 * hl_vm_fini - virtual memory module teardown
1737 *
1738 * @hdev : pointer to the habanalabs device structure
1739 *
1740 * This function perform teardown to the following:
1741 * - Idr for device memory allocation handles
1742 * - DRAM physical pages pool of 2MB
1743 * - MMU module
1744 */
1745void hl_vm_fini(struct hl_device *hdev)
1746{
1747 struct hl_vm *vm = &hdev->vm;
1748
1749 if (!vm->init_done)
1750 return;
1751
1752 /*
1753 * At this point all the contexts should be freed and hence no DRAM
1754 * memory should be in use. Hence the DRAM pool should be freed here.
1755 */
1756 if (kref_put(&vm->dram_pg_pool_refcount, dram_pg_pool_do_release) != 1)
1757 dev_warn(hdev->dev, "dram_pg_pool was not destroyed on %s\n",
1758 __func__);
1759
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001760 vm->init_done = false;
1761}