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