blob: 8ade9886a5a7d157620b741bbbda75aadd13e371 [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
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 */
53static 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;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +020059 u64 paddr = 0, total_size, num_pgs, i;
60 u32 num_curr_pgs, page_size, page_shift;
61 int handle, rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +020062 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,
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +020076 "failed to allocate %llu huge contiguous pages\n",
Omer Shpigelman0feaf862019-02-16 00:39:22 +020077 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
Omer Shpigelman4eb1d122019-03-07 15:47:19 +020096 phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL);
Omer Shpigelman0feaf862019-02-16 00:39:22 +020097 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,
Oded Gabbaycab8e3e2019-03-27 09:44:28 +0200112 "Failed to allocate device memory (out of memory)\n");
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200113 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,
Wei Yongjun668ae722019-02-22 05:46:01 +0000123 GFP_ATOMIC);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200124 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
144idr_err:
145page_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
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200151 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200152pages_arr_err:
153 kfree(phys_pg_pack);
154pages_pack_err:
155 if (contiguous)
156 gen_pool_free(vm->dram_pg_pool, paddr, total_size);
157
158 return rc;
159}
160
161/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300162 * dma_map_host_va - DMA mapping of the given host virtual address.
163 * @hdev: habanalabs device structure
164 * @addr: the host virtual address of the memory area
165 * @size: the size of the memory area
166 * @p_userptr: pointer to result userptr structure
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200167 *
168 * This function does the following:
169 * - Allocate userptr structure
170 * - Pin the given host memory using the userptr structure
171 * - Perform DMA mapping to have the DMA addresses of the pages
172 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300173static int dma_map_host_va(struct hl_device *hdev, u64 addr, u64 size,
174 struct hl_userptr **p_userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200175{
176 struct hl_userptr *userptr;
177 int rc;
178
179 userptr = kzalloc(sizeof(*userptr), GFP_KERNEL);
180 if (!userptr) {
181 rc = -ENOMEM;
182 goto userptr_err;
183 }
184
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300185 rc = hl_pin_host_memory(hdev, addr, size, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200186 if (rc) {
187 dev_err(hdev->dev, "Failed to pin host memory\n");
188 goto pin_err;
189 }
190
191 rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl,
192 userptr->sgt->nents, DMA_BIDIRECTIONAL);
193 if (rc) {
194 dev_err(hdev->dev, "failed to map sgt with DMA region\n");
195 goto dma_map_err;
196 }
197
198 userptr->dma_mapped = true;
199 userptr->dir = DMA_BIDIRECTIONAL;
200 userptr->vm_type = VM_TYPE_USERPTR;
201
202 *p_userptr = userptr;
203
204 return 0;
205
206dma_map_err:
207 hl_unpin_host_memory(hdev, userptr);
208pin_err:
209 kfree(userptr);
210userptr_err:
211
212 return rc;
213}
214
215/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300216 * dma_unmap_host_va - DMA unmapping of the given host virtual address.
217 * @hdev: habanalabs device structure
218 * @userptr: userptr to free
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200219 *
220 * This function does the following:
221 * - Unpins the physical pages
222 * - Frees the userptr structure
223 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300224static void dma_unmap_host_va(struct hl_device *hdev,
225 struct hl_userptr *userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200226{
227 hl_unpin_host_memory(hdev, userptr);
228 kfree(userptr);
229}
230
231/*
232 * dram_pg_pool_do_release - free DRAM pages pool
233 *
234 * @ref : pointer to reference object
235 *
236 * This function does the following:
237 * - Frees the idr structure of physical pages handles
238 * - Frees the generic pool of DRAM physical pages
239 */
240static void dram_pg_pool_do_release(struct kref *ref)
241{
242 struct hl_vm *vm = container_of(ref, struct hl_vm,
243 dram_pg_pool_refcount);
244
245 /*
246 * free the idr here as only here we know for sure that there are no
247 * allocated physical pages and hence there are no handles in use
248 */
249 idr_destroy(&vm->phys_pg_pack_handles);
250 gen_pool_destroy(vm->dram_pg_pool);
251}
252
253/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300254 * free_phys_pg_pack - free physical page pack
255 * @hdev: habanalabs device structure
256 * @phys_pg_pack: physical page pack to free
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200257 *
258 * This function does the following:
259 * - For DRAM memory only, iterate over the pack and free each physical block
260 * structure by returning it to the general pool
261 * - Free the hl_vm_phys_pg_pack structure
262 */
263static void free_phys_pg_pack(struct hl_device *hdev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300264 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200265{
266 struct hl_vm *vm = &hdev->vm;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200267 u64 i;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200268
269 if (!phys_pg_pack->created_from_userptr) {
270 if (phys_pg_pack->contiguous) {
271 gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[0],
272 phys_pg_pack->total_size);
273
274 for (i = 0; i < phys_pg_pack->npages ; i++)
275 kref_put(&vm->dram_pg_pool_refcount,
276 dram_pg_pool_do_release);
277 } else {
278 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
279 gen_pool_free(vm->dram_pg_pool,
280 phys_pg_pack->pages[i],
281 phys_pg_pack->page_size);
282 kref_put(&vm->dram_pg_pool_refcount,
283 dram_pg_pool_do_release);
284 }
285 }
286 }
287
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200288 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200289 kfree(phys_pg_pack);
290}
291
292/*
293 * free_device_memory - free device memory
294 *
295 * @ctx : current context
296 * @handle : handle of the memory chunk to free
297 *
298 * This function does the following:
299 * - Free the device memory related to the given handle
300 */
301static int free_device_memory(struct hl_ctx *ctx, u32 handle)
302{
303 struct hl_device *hdev = ctx->hdev;
304 struct hl_vm *vm = &hdev->vm;
305 struct hl_vm_phys_pg_pack *phys_pg_pack;
306
307 spin_lock(&vm->idr_lock);
308 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
309 if (phys_pg_pack) {
310 if (atomic_read(&phys_pg_pack->mapping_cnt) > 0) {
311 dev_err(hdev->dev, "handle %u is mapped, cannot free\n",
312 handle);
313 spin_unlock(&vm->idr_lock);
314 return -EINVAL;
315 }
316
317 /*
318 * must remove from idr before the freeing of the physical
319 * pages as the refcount of the pool is also the trigger of the
320 * idr destroy
321 */
322 idr_remove(&vm->phys_pg_pack_handles, handle);
323 spin_unlock(&vm->idr_lock);
324
325 atomic64_sub(phys_pg_pack->total_size, &ctx->dram_phys_mem);
326 atomic64_sub(phys_pg_pack->total_size, &hdev->dram_used_mem);
327
328 free_phys_pg_pack(hdev, phys_pg_pack);
329 } else {
330 spin_unlock(&vm->idr_lock);
331 dev_err(hdev->dev,
332 "free device memory failed, no match for handle %u\n",
333 handle);
334 return -EINVAL;
335 }
336
337 return 0;
338}
339
340/*
341 * clear_va_list_locked - free virtual addresses list
342 *
343 * @hdev : habanalabs device structure
344 * @va_list : list of virtual addresses to free
345 *
346 * This function does the following:
347 * - Iterate over the list and free each virtual addresses block
348 *
349 * This function should be called only when va_list lock is taken
350 */
351static void clear_va_list_locked(struct hl_device *hdev,
352 struct list_head *va_list)
353{
354 struct hl_vm_va_block *va_block, *tmp;
355
356 list_for_each_entry_safe(va_block, tmp, va_list, node) {
357 list_del(&va_block->node);
358 kfree(va_block);
359 }
360}
361
362/*
363 * print_va_list_locked - print virtual addresses list
364 *
365 * @hdev : habanalabs device structure
366 * @va_list : list of virtual addresses to print
367 *
368 * This function does the following:
369 * - Iterate over the list and print each virtual addresses block
370 *
371 * This function should be called only when va_list lock is taken
372 */
373static void print_va_list_locked(struct hl_device *hdev,
374 struct list_head *va_list)
375{
376#if HL_MMU_DEBUG
377 struct hl_vm_va_block *va_block;
378
379 dev_dbg(hdev->dev, "print va list:\n");
380
381 list_for_each_entry(va_block, va_list, node)
382 dev_dbg(hdev->dev,
383 "va block, start: 0x%llx, end: 0x%llx, size: %llu\n",
384 va_block->start, va_block->end, va_block->size);
385#endif
386}
387
388/*
389 * merge_va_blocks_locked - merge a virtual block if possible
390 *
391 * @hdev : pointer to the habanalabs device structure
392 * @va_list : pointer to the virtual addresses block list
393 * @va_block : virtual block to merge with adjacent blocks
394 *
395 * This function does the following:
396 * - Merge the given blocks with the adjacent blocks if their virtual ranges
397 * create a contiguous virtual range
398 *
399 * This Function should be called only when va_list lock is taken
400 */
401static void merge_va_blocks_locked(struct hl_device *hdev,
402 struct list_head *va_list, struct hl_vm_va_block *va_block)
403{
404 struct hl_vm_va_block *prev, *next;
405
406 prev = list_prev_entry(va_block, node);
407 if (&prev->node != va_list && prev->end + 1 == va_block->start) {
408 prev->end = va_block->end;
409 prev->size = prev->end - prev->start;
410 list_del(&va_block->node);
411 kfree(va_block);
412 va_block = prev;
413 }
414
415 next = list_next_entry(va_block, node);
416 if (&next->node != va_list && va_block->end + 1 == next->start) {
417 next->start = va_block->start;
418 next->size = next->end - next->start;
419 list_del(&va_block->node);
420 kfree(va_block);
421 }
422}
423
424/*
425 * add_va_block_locked - add a virtual block to the virtual addresses list
426 *
427 * @hdev : pointer to the habanalabs device structure
428 * @va_list : pointer to the virtual addresses block list
429 * @start : start virtual address
430 * @end : end virtual address
431 *
432 * This function does the following:
433 * - Add the given block to the virtual blocks list and merge with other
434 * blocks if a contiguous virtual block can be created
435 *
436 * This Function should be called only when va_list lock is taken
437 */
438static int add_va_block_locked(struct hl_device *hdev,
439 struct list_head *va_list, u64 start, u64 end)
440{
441 struct hl_vm_va_block *va_block, *res = NULL;
442 u64 size = end - start;
443
444 print_va_list_locked(hdev, va_list);
445
446 list_for_each_entry(va_block, va_list, node) {
447 /* TODO: remove upon matureness */
448 if (hl_mem_area_crosses_range(start, size, va_block->start,
449 va_block->end)) {
450 dev_err(hdev->dev,
451 "block crossing ranges at start 0x%llx, end 0x%llx\n",
452 va_block->start, va_block->end);
453 return -EINVAL;
454 }
455
456 if (va_block->end < start)
457 res = va_block;
458 }
459
460 va_block = kmalloc(sizeof(*va_block), GFP_KERNEL);
461 if (!va_block)
462 return -ENOMEM;
463
464 va_block->start = start;
465 va_block->end = end;
466 va_block->size = size;
467
468 if (!res)
469 list_add(&va_block->node, va_list);
470 else
471 list_add(&va_block->node, &res->node);
472
473 merge_va_blocks_locked(hdev, va_list, va_block);
474
475 print_va_list_locked(hdev, va_list);
476
477 return 0;
478}
479
480/*
481 * add_va_block - wrapper for add_va_block_locked
482 *
483 * @hdev : pointer to the habanalabs device structure
484 * @va_list : pointer to the virtual addresses block list
485 * @start : start virtual address
486 * @end : end virtual address
487 *
488 * This function does the following:
489 * - Takes the list lock and calls add_va_block_locked
490 */
491static inline int add_va_block(struct hl_device *hdev,
492 struct hl_va_range *va_range, u64 start, u64 end)
493{
494 int rc;
495
496 mutex_lock(&va_range->lock);
497 rc = add_va_block_locked(hdev, &va_range->list, start, end);
498 mutex_unlock(&va_range->lock);
499
500 return rc;
501}
502
503/*
504 * get_va_block - get a virtual block with the requested size
505 *
506 * @hdev : pointer to the habanalabs device structure
507 * @va_range : pointer to the virtual addresses range
508 * @size : requested block size
509 * @hint_addr : hint for request address by the user
510 * @is_userptr : is host or DRAM memory
511 *
512 * This function does the following:
513 * - Iterate on the virtual block list to find a suitable virtual block for the
514 * requested size
515 * - Reserve the requested block and update the list
516 * - Return the start address of the virtual block
517 */
518static u64 get_va_block(struct hl_device *hdev,
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200519 struct hl_va_range *va_range, u64 size, u64 hint_addr,
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200520 bool is_userptr)
521{
522 struct hl_vm_va_block *va_block, *new_va_block = NULL;
523 u64 valid_start, valid_size, prev_start, prev_end, page_mask,
524 res_valid_start = 0, res_valid_size = 0;
525 u32 page_size;
526 bool add_prev = false;
527
528 if (is_userptr) {
529 /*
530 * We cannot know if the user allocated memory with huge pages
531 * or not, hence we continue with the biggest possible
532 * granularity.
533 */
534 page_size = PAGE_SIZE_2MB;
535 page_mask = PAGE_MASK_2MB;
536 } else {
537 page_size = hdev->asic_prop.dram_page_size;
538 page_mask = ~((u64)page_size - 1);
539 }
540
541 mutex_lock(&va_range->lock);
542
543 print_va_list_locked(hdev, &va_range->list);
544
545 list_for_each_entry(va_block, &va_range->list, node) {
546 /* calc the first possible aligned addr */
547 valid_start = va_block->start;
548
549
550 if (valid_start & (page_size - 1)) {
551 valid_start &= page_mask;
552 valid_start += page_size;
553 if (valid_start > va_block->end)
554 continue;
555 }
556
557 valid_size = va_block->end - valid_start;
558
559 if (valid_size >= size &&
560 (!new_va_block || valid_size < res_valid_size)) {
561
562 new_va_block = va_block;
563 res_valid_start = valid_start;
564 res_valid_size = valid_size;
565 }
566
567 if (hint_addr && hint_addr >= valid_start &&
568 ((hint_addr + size) <= va_block->end)) {
569 new_va_block = va_block;
570 res_valid_start = hint_addr;
571 res_valid_size = valid_size;
572 break;
573 }
574 }
575
576 if (!new_va_block) {
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200577 dev_err(hdev->dev, "no available va block for size %llu\n",
578 size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200579 goto out;
580 }
581
582 if (res_valid_start > new_va_block->start) {
583 prev_start = new_va_block->start;
584 prev_end = res_valid_start - 1;
585
586 new_va_block->start = res_valid_start;
587 new_va_block->size = res_valid_size;
588
589 add_prev = true;
590 }
591
592 if (new_va_block->size > size) {
593 new_va_block->start += size;
594 new_va_block->size = new_va_block->end - new_va_block->start;
595 } else {
596 list_del(&new_va_block->node);
597 kfree(new_va_block);
598 }
599
600 if (add_prev)
601 add_va_block_locked(hdev, &va_range->list, prev_start,
602 prev_end);
603
604 print_va_list_locked(hdev, &va_range->list);
605out:
606 mutex_unlock(&va_range->lock);
607
608 return res_valid_start;
609}
610
611/*
612 * get_sg_info - get number of pages and the DMA address from SG list
613 *
614 * @sg : the SG list
615 * @dma_addr : pointer to DMA address to return
616 *
617 * Calculate the number of consecutive pages described by the SG list. Take the
618 * offset of the address in the first page, add to it the length and round it up
619 * to the number of needed pages.
620 */
621static u32 get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
622{
623 *dma_addr = sg_dma_address(sg);
624
625 return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
626 (PAGE_SIZE - 1)) >> PAGE_SHIFT;
627}
628
629/*
630 * init_phys_pg_pack_from_userptr - initialize physical page pack from host
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300631 * memory
632 * @asid: current context ASID
633 * @userptr: userptr to initialize from
634 * @pphys_pg_pack: result pointer
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200635 *
636 * This function does the following:
637 * - Pin the physical pages related to the given virtual block
638 * - Create a physical page pack from the physical pages related to the given
639 * virtual block
640 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300641static int init_phys_pg_pack_from_userptr(u32 asid, struct hl_userptr *userptr,
642 struct hl_vm_phys_pg_pack **pphys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200643{
644 struct hl_vm_phys_pg_pack *phys_pg_pack;
645 struct scatterlist *sg;
646 dma_addr_t dma_addr;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200647 u64 page_mask, total_npages;
648 u32 npages, page_size = PAGE_SIZE;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200649 bool first = true, is_huge_page_opt = true;
650 int rc, i, j;
651
652 phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL);
653 if (!phys_pg_pack)
654 return -ENOMEM;
655
656 phys_pg_pack->vm_type = userptr->vm_type;
657 phys_pg_pack->created_from_userptr = true;
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300658 phys_pg_pack->asid = asid;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200659 atomic_set(&phys_pg_pack->mapping_cnt, 1);
660
661 /* Only if all dma_addrs are aligned to 2MB and their
662 * sizes is at least 2MB, we can use huge page mapping.
663 * We limit the 2MB optimization to this condition,
664 * since later on we acquire the related VA range as one
665 * consecutive block.
666 */
667 total_npages = 0;
668 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
669 npages = get_sg_info(sg, &dma_addr);
670
671 total_npages += npages;
672
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200673 if ((npages % PGS_IN_2MB_PAGE) ||
674 (dma_addr & (PAGE_SIZE_2MB - 1)))
675 is_huge_page_opt = false;
676 }
677
678 if (is_huge_page_opt) {
679 page_size = PAGE_SIZE_2MB;
680 total_npages /= PGS_IN_2MB_PAGE;
681 }
682
683 page_mask = ~(((u64) page_size) - 1);
684
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200685 phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64),
686 GFP_KERNEL);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200687 if (!phys_pg_pack->pages) {
688 rc = -ENOMEM;
689 goto page_pack_arr_mem_err;
690 }
691
692 phys_pg_pack->npages = total_npages;
693 phys_pg_pack->page_size = page_size;
694 phys_pg_pack->total_size = total_npages * page_size;
695
696 j = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200697 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
698 npages = get_sg_info(sg, &dma_addr);
699
700 /* align down to physical page size and save the offset */
701 if (first) {
702 first = false;
703 phys_pg_pack->offset = dma_addr & (page_size - 1);
704 dma_addr &= page_mask;
705 }
706
707 while (npages) {
708 phys_pg_pack->pages[j++] = dma_addr;
709 dma_addr += page_size;
710
711 if (is_huge_page_opt)
712 npages -= PGS_IN_2MB_PAGE;
713 else
714 npages--;
715 }
716 }
717
718 *pphys_pg_pack = phys_pg_pack;
719
720 return 0;
721
722page_pack_arr_mem_err:
723 kfree(phys_pg_pack);
724
725 return rc;
726}
727
728/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300729 * map_phys_pg_pack - maps the physical page pack.
730 * @ctx: current context
731 * @vaddr: start address of the virtual area to map from
732 * @phys_pg_pack: the pack of physical pages to map to
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200733 *
734 * This function does the following:
735 * - Maps each chunk of virtual memory to matching physical chunk
736 * - Stores number of successful mappings in the given argument
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300737 * - Returns 0 on success, error code otherwise
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200738 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300739static int map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
740 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200741{
742 struct hl_device *hdev = ctx->hdev;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200743 u64 next_vaddr = vaddr, paddr, mapped_pg_cnt = 0, i;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200744 u32 page_size = phys_pg_pack->page_size;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200745 int rc = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200746
747 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
748 paddr = phys_pg_pack->pages[i];
749
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200750 rc = hl_mmu_map(ctx, next_vaddr, paddr, page_size);
751 if (rc) {
752 dev_err(hdev->dev,
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200753 "map failed for handle %u, npages: %llu, mapped: %llu",
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200754 phys_pg_pack->handle, phys_pg_pack->npages,
755 mapped_pg_cnt);
756 goto err;
757 }
758
759 mapped_pg_cnt++;
760 next_vaddr += page_size;
761 }
762
763 return 0;
764
765err:
766 next_vaddr = vaddr;
767 for (i = 0 ; i < mapped_pg_cnt ; i++) {
768 if (hl_mmu_unmap(ctx, next_vaddr, page_size))
769 dev_warn_ratelimited(hdev->dev,
770 "failed to unmap handle %u, va: 0x%llx, pa: 0x%llx, page size: %u\n",
771 phys_pg_pack->handle, next_vaddr,
772 phys_pg_pack->pages[i], page_size);
773
774 next_vaddr += page_size;
775 }
776
777 return rc;
778}
779
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300780/*
781 * unmap_phys_pg_pack - unmaps the physical page pack
782 * @ctx: current context
783 * @vaddr: start address of the virtual area to unmap
784 * @phys_pg_pack: the pack of physical pages to unmap
785 */
786static void unmap_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
787 struct hl_vm_phys_pg_pack *phys_pg_pack)
788{
789 struct hl_device *hdev = ctx->hdev;
790 u64 next_vaddr, i;
791 u32 page_size;
792
793 page_size = phys_pg_pack->page_size;
794 next_vaddr = vaddr;
795
796 for (i = 0 ; i < phys_pg_pack->npages ; i++, next_vaddr += page_size) {
797 if (hl_mmu_unmap(ctx, next_vaddr, page_size))
798 dev_warn_ratelimited(hdev->dev,
799 "unmap failed for vaddr: 0x%llx\n", next_vaddr);
800
801 /*
802 * unmapping on Palladium can be really long, so avoid a CPU
803 * soft lockup bug by sleeping a little between unmapping pages
804 */
805 if (hdev->pldm)
806 usleep_range(500, 1000);
807 }
808}
809
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200810static int get_paddr_from_handle(struct hl_ctx *ctx, struct hl_mem_in *args,
811 u64 *paddr)
812{
813 struct hl_device *hdev = ctx->hdev;
814 struct hl_vm *vm = &hdev->vm;
815 struct hl_vm_phys_pg_pack *phys_pg_pack;
816 u32 handle;
817
818 handle = lower_32_bits(args->map_device.handle);
819 spin_lock(&vm->idr_lock);
820 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
821 if (!phys_pg_pack) {
822 spin_unlock(&vm->idr_lock);
823 dev_err(hdev->dev, "no match for handle %u\n", handle);
824 return -EINVAL;
825 }
826
827 *paddr = phys_pg_pack->pages[0];
828
829 spin_unlock(&vm->idr_lock);
830
831 return 0;
832}
833
834/*
835 * map_device_va - map the given memory
836 *
837 * @ctx : current context
838 * @args : host parameters with handle/host virtual address
839 * @device_addr : pointer to result device virtual address
840 *
841 * This function does the following:
842 * - If given a physical device memory handle, map to a device virtual block
843 * and return the start address of this block
844 * - If given a host virtual address and size, find the related physical pages,
845 * map a device virtual block to this pages and return the start address of
846 * this block
847 */
848static int map_device_va(struct hl_ctx *ctx, struct hl_mem_in *args,
849 u64 *device_addr)
850{
851 struct hl_device *hdev = ctx->hdev;
852 struct hl_vm *vm = &hdev->vm;
853 struct hl_vm_phys_pg_pack *phys_pg_pack;
854 struct hl_userptr *userptr = NULL;
855 struct hl_vm_hash_node *hnode;
856 enum vm_type_t *vm_type;
857 u64 ret_vaddr, hint_addr;
858 u32 handle = 0;
859 int rc;
860 bool is_userptr = args->flags & HL_MEM_USERPTR;
861
862 /* Assume failure */
863 *device_addr = 0;
864
865 if (is_userptr) {
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300866 u64 addr = args->map_host.host_virt_addr,
867 size = args->map_host.mem_size;
868
869 rc = dma_map_host_va(hdev, addr, size, &userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200870 if (rc) {
871 dev_err(hdev->dev, "failed to get userptr from va\n");
872 return rc;
873 }
874
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300875 rc = init_phys_pg_pack_from_userptr(ctx->asid, userptr,
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200876 &phys_pg_pack);
877 if (rc) {
878 dev_err(hdev->dev,
879 "unable to init page pack for vaddr 0x%llx\n",
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300880 addr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200881 goto init_page_pack_err;
882 }
883
884 vm_type = (enum vm_type_t *) userptr;
885 hint_addr = args->map_host.hint_addr;
886 } else {
887 handle = lower_32_bits(args->map_device.handle);
888
889 spin_lock(&vm->idr_lock);
890 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
891 if (!phys_pg_pack) {
892 spin_unlock(&vm->idr_lock);
893 dev_err(hdev->dev,
894 "no match for handle %u\n", handle);
895 return -EINVAL;
896 }
897
898 /* increment now to avoid freeing device memory while mapping */
899 atomic_inc(&phys_pg_pack->mapping_cnt);
900
901 spin_unlock(&vm->idr_lock);
902
903 vm_type = (enum vm_type_t *) phys_pg_pack;
904
905 hint_addr = args->map_device.hint_addr;
906 }
907
908 /*
909 * relevant for mapping device physical memory only, as host memory is
910 * implicitly shared
911 */
912 if (!is_userptr && !(phys_pg_pack->flags & HL_MEM_SHARED) &&
913 phys_pg_pack->asid != ctx->asid) {
914 dev_err(hdev->dev,
915 "Failed to map memory, handle %u is not shared\n",
916 handle);
917 rc = -EPERM;
918 goto shared_err;
919 }
920
921 hnode = kzalloc(sizeof(*hnode), GFP_KERNEL);
922 if (!hnode) {
923 rc = -ENOMEM;
924 goto hnode_err;
925 }
926
927 ret_vaddr = get_va_block(hdev,
928 is_userptr ? &ctx->host_va_range : &ctx->dram_va_range,
929 phys_pg_pack->total_size, hint_addr, is_userptr);
930 if (!ret_vaddr) {
931 dev_err(hdev->dev, "no available va block for handle %u\n",
932 handle);
933 rc = -ENOMEM;
934 goto va_block_err;
935 }
936
937 mutex_lock(&ctx->mmu_lock);
938
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300939 rc = map_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200940 if (rc) {
941 mutex_unlock(&ctx->mmu_lock);
942 dev_err(hdev->dev, "mapping page pack failed for handle %u\n",
943 handle);
944 goto map_err;
945 }
946
Omer Shpigelman27ca384c2019-02-28 10:46:11 +0200947 hdev->asic_funcs->mmu_invalidate_cache(hdev, false);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200948
949 mutex_unlock(&ctx->mmu_lock);
950
951 ret_vaddr += phys_pg_pack->offset;
952
953 hnode->ptr = vm_type;
954 hnode->vaddr = ret_vaddr;
955
956 mutex_lock(&ctx->mem_hash_lock);
957 hash_add(ctx->mem_hash, &hnode->node, ret_vaddr);
958 mutex_unlock(&ctx->mem_hash_lock);
959
960 *device_addr = ret_vaddr;
961
962 if (is_userptr)
963 free_phys_pg_pack(hdev, phys_pg_pack);
964
965 return 0;
966
967map_err:
968 if (add_va_block(hdev,
969 is_userptr ? &ctx->host_va_range : &ctx->dram_va_range,
970 ret_vaddr,
971 ret_vaddr + phys_pg_pack->total_size - 1))
972 dev_warn(hdev->dev,
973 "release va block failed for handle 0x%x, vaddr: 0x%llx\n",
974 handle, ret_vaddr);
975
976va_block_err:
977 kfree(hnode);
978hnode_err:
979shared_err:
980 atomic_dec(&phys_pg_pack->mapping_cnt);
981 if (is_userptr)
982 free_phys_pg_pack(hdev, phys_pg_pack);
983init_page_pack_err:
984 if (is_userptr)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300985 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200986
987 return rc;
988}
989
990/*
991 * unmap_device_va - unmap the given device virtual address
992 *
993 * @ctx : current context
994 * @vaddr : device virtual address to unmap
995 *
996 * This function does the following:
997 * - Unmap the physical pages related to the given virtual address
998 * - return the device virtual block to the virtual block list
999 */
1000static int unmap_device_va(struct hl_ctx *ctx, u64 vaddr)
1001{
1002 struct hl_device *hdev = ctx->hdev;
1003 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
1004 struct hl_vm_hash_node *hnode = NULL;
1005 struct hl_userptr *userptr = NULL;
1006 enum vm_type_t *vm_type;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001007 bool is_userptr;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +02001008 int rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001009
1010 /* protect from double entrance */
1011 mutex_lock(&ctx->mem_hash_lock);
1012 hash_for_each_possible(ctx->mem_hash, hnode, node, (unsigned long)vaddr)
1013 if (vaddr == hnode->vaddr)
1014 break;
1015
1016 if (!hnode) {
1017 mutex_unlock(&ctx->mem_hash_lock);
1018 dev_err(hdev->dev,
1019 "unmap failed, no mem hnode for vaddr 0x%llx\n",
1020 vaddr);
1021 return -EINVAL;
1022 }
1023
1024 hash_del(&hnode->node);
1025 mutex_unlock(&ctx->mem_hash_lock);
1026
1027 vm_type = hnode->ptr;
1028
1029 if (*vm_type == VM_TYPE_USERPTR) {
1030 is_userptr = true;
1031 userptr = hnode->ptr;
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001032 rc = init_phys_pg_pack_from_userptr(ctx->asid, userptr,
1033 &phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001034 if (rc) {
1035 dev_err(hdev->dev,
1036 "unable to init page pack for vaddr 0x%llx\n",
1037 vaddr);
1038 goto vm_type_err;
1039 }
1040 } else if (*vm_type == VM_TYPE_PHYS_PACK) {
1041 is_userptr = false;
1042 phys_pg_pack = hnode->ptr;
1043 } else {
1044 dev_warn(hdev->dev,
1045 "unmap failed, unknown vm desc for vaddr 0x%llx\n",
1046 vaddr);
1047 rc = -EFAULT;
1048 goto vm_type_err;
1049 }
1050
1051 if (atomic_read(&phys_pg_pack->mapping_cnt) == 0) {
1052 dev_err(hdev->dev, "vaddr 0x%llx is not mapped\n", vaddr);
1053 rc = -EINVAL;
1054 goto mapping_cnt_err;
1055 }
1056
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001057 vaddr &= ~(((u64) phys_pg_pack->page_size) - 1);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001058
1059 mutex_lock(&ctx->mmu_lock);
1060
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001061 unmap_phys_pg_pack(ctx, vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001062
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001063 hdev->asic_funcs->mmu_invalidate_cache(hdev, true);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001064
1065 mutex_unlock(&ctx->mmu_lock);
1066
1067 if (add_va_block(hdev,
1068 is_userptr ? &ctx->host_va_range : &ctx->dram_va_range,
1069 vaddr,
1070 vaddr + phys_pg_pack->total_size - 1))
1071 dev_warn(hdev->dev, "add va block failed for vaddr: 0x%llx\n",
1072 vaddr);
1073
1074 atomic_dec(&phys_pg_pack->mapping_cnt);
1075 kfree(hnode);
1076
1077 if (is_userptr) {
1078 free_phys_pg_pack(hdev, phys_pg_pack);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001079 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001080 }
1081
1082 return 0;
1083
1084mapping_cnt_err:
1085 if (is_userptr)
1086 free_phys_pg_pack(hdev, phys_pg_pack);
1087vm_type_err:
1088 mutex_lock(&ctx->mem_hash_lock);
1089 hash_add(ctx->mem_hash, &hnode->node, vaddr);
1090 mutex_unlock(&ctx->mem_hash_lock);
1091
1092 return rc;
1093}
1094
Oded Gabbay54303a12019-04-04 14:42:26 +03001095static int mem_ioctl_no_mmu(struct hl_fpriv *hpriv, union hl_mem_args *args)
1096{
1097 struct hl_device *hdev = hpriv->hdev;
1098 struct hl_ctx *ctx = hpriv->ctx;
1099 u64 device_addr = 0;
1100 u32 handle = 0;
1101 int rc;
1102
1103 switch (args->in.op) {
1104 case HL_MEM_OP_ALLOC:
1105 if (args->in.alloc.mem_size == 0) {
1106 dev_err(hdev->dev,
1107 "alloc size must be larger than 0\n");
1108 rc = -EINVAL;
1109 goto out;
1110 }
1111
1112 /* Force contiguous as there are no real MMU
1113 * translations to overcome physical memory gaps
1114 */
1115 args->in.flags |= HL_MEM_CONTIGUOUS;
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 rc = free_device_memory(ctx, args->in.free.handle);
1124 break;
1125
1126 case HL_MEM_OP_MAP:
1127 if (args->in.flags & HL_MEM_USERPTR) {
1128 device_addr = args->in.map_host.host_virt_addr;
1129 rc = 0;
1130 } else {
1131 rc = get_paddr_from_handle(ctx, &args->in,
1132 &device_addr);
1133 }
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 = 0;
1141 break;
1142
1143 default:
1144 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1145 rc = -ENOTTY;
1146 break;
1147 }
1148
1149out:
1150 return rc;
1151}
1152
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001153int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
1154{
1155 union hl_mem_args *args = data;
1156 struct hl_device *hdev = hpriv->hdev;
1157 struct hl_ctx *ctx = hpriv->ctx;
1158 u64 device_addr = 0;
1159 u32 handle = 0;
1160 int rc;
1161
1162 if (hl_device_disabled_or_in_reset(hdev)) {
1163 dev_warn_ratelimited(hdev->dev,
Oded Gabbay3f5398c2019-04-06 15:41:35 +03001164 "Device is %s. Can't execute MEMORY IOCTL\n",
1165 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001166 return -EBUSY;
1167 }
1168
Oded Gabbay54303a12019-04-04 14:42:26 +03001169 if (!hdev->mmu_enable)
1170 return mem_ioctl_no_mmu(hpriv, args);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001171
Oded Gabbay54303a12019-04-04 14:42:26 +03001172 switch (args->in.op) {
1173 case HL_MEM_OP_ALLOC:
1174 if (!hdev->dram_supports_virtual_memory) {
1175 dev_err(hdev->dev, "DRAM alloc is not supported\n");
1176 rc = -EINVAL;
1177 goto out;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001178 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001179
Oded Gabbay54303a12019-04-04 14:42:26 +03001180 if (args->in.alloc.mem_size == 0) {
1181 dev_err(hdev->dev,
1182 "alloc size must be larger than 0\n");
1183 rc = -EINVAL;
1184 goto out;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001185 }
Oded Gabbay54303a12019-04-04 14:42:26 +03001186 rc = alloc_device_memory(ctx, &args->in, &handle);
1187
1188 memset(args, 0, sizeof(*args));
1189 args->out.handle = (__u64) handle;
1190 break;
1191
1192 case HL_MEM_OP_FREE:
1193 rc = free_device_memory(ctx, args->in.free.handle);
1194 break;
1195
1196 case HL_MEM_OP_MAP:
1197 rc = map_device_va(ctx, &args->in, &device_addr);
1198
1199 memset(args, 0, sizeof(*args));
1200 args->out.device_virt_addr = device_addr;
1201 break;
1202
1203 case HL_MEM_OP_UNMAP:
1204 rc = unmap_device_va(ctx,
1205 args->in.unmap.device_virt_addr);
1206 break;
1207
1208 default:
1209 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1210 rc = -ENOTTY;
1211 break;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001212 }
1213
1214out:
1215 return rc;
1216}
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001217
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001218static int get_user_memory(struct hl_device *hdev, u64 addr, u64 size,
1219 u32 npages, u64 start, u32 offset,
1220 struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001221{
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001222 int rc;
1223
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001224 if (!access_ok((void __user *) (uintptr_t) addr, size)) {
Oded Gabbay230afe72019-02-27 00:19:18 +02001225 dev_err(hdev->dev, "user pointer is invalid - 0x%llx\n", addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001226 return -EFAULT;
1227 }
1228
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001229 userptr->vec = frame_vector_create(npages);
1230 if (!userptr->vec) {
1231 dev_err(hdev->dev, "Failed to create frame vector\n");
1232 return -ENOMEM;
1233 }
1234
1235 rc = get_vaddr_frames(start, npages, FOLL_FORCE | FOLL_WRITE,
1236 userptr->vec);
1237
1238 if (rc != npages) {
1239 dev_err(hdev->dev,
1240 "Failed to map host memory, user ptr probably wrong\n");
1241 if (rc < 0)
1242 goto destroy_framevec;
1243 rc = -EFAULT;
1244 goto put_framevec;
1245 }
1246
1247 if (frame_vector_to_pages(userptr->vec) < 0) {
1248 dev_err(hdev->dev,
1249 "Failed to translate frame vector to pages\n");
1250 rc = -EFAULT;
1251 goto put_framevec;
1252 }
1253
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001254 rc = sg_alloc_table_from_pages(userptr->sgt,
1255 frame_vector_pages(userptr->vec),
1256 npages, offset, size, GFP_ATOMIC);
1257 if (rc < 0) {
1258 dev_err(hdev->dev, "failed to create SG table from pages\n");
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001259 goto put_framevec;
1260 }
1261
1262 return 0;
1263
1264put_framevec:
1265 put_vaddr_frames(userptr->vec);
1266destroy_framevec:
1267 frame_vector_destroy(userptr->vec);
1268 return rc;
1269}
1270
1271/*
1272 * hl_pin_host_memory - pins a chunk of host memory.
1273 * @hdev: pointer to the habanalabs device structure
1274 * @addr: the host virtual address of the memory area
1275 * @size: the size of the memory area
1276 * @userptr: pointer to hl_userptr structure
1277 *
1278 * This function does the following:
1279 * - Pins the physical pages
1280 * - Create an SG list from those pages
1281 */
1282int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
1283 struct hl_userptr *userptr)
1284{
1285 u64 start, end;
1286 u32 npages, offset;
1287 int rc;
1288
1289 if (!size) {
1290 dev_err(hdev->dev, "size to pin is invalid - %llu\n", size);
1291 return -EINVAL;
1292 }
1293
1294 /*
1295 * If the combination of the address and size requested for this memory
1296 * region causes an integer overflow, return error.
1297 */
1298 if (((addr + size) < addr) ||
1299 PAGE_ALIGN(addr + size) < (addr + size)) {
1300 dev_err(hdev->dev,
1301 "user pointer 0x%llx + %llu causes integer overflow\n",
1302 addr, size);
1303 return -EINVAL;
1304 }
1305
1306 /*
1307 * This function can be called also from data path, hence use atomic
1308 * always as it is not a big allocation.
1309 */
1310 userptr->sgt = kzalloc(sizeof(*userptr->sgt), GFP_ATOMIC);
1311 if (!userptr->sgt)
1312 return -ENOMEM;
1313
1314 start = addr & PAGE_MASK;
1315 offset = addr & ~PAGE_MASK;
1316 end = PAGE_ALIGN(addr + size);
1317 npages = (end - start) >> PAGE_SHIFT;
1318
1319 userptr->size = size;
1320 userptr->addr = addr;
1321 userptr->dma_mapped = false;
1322 INIT_LIST_HEAD(&userptr->job_node);
1323
1324 rc = get_user_memory(hdev, addr, size, npages, start, offset,
1325 userptr);
1326 if (rc) {
1327 dev_err(hdev->dev,
1328 "failed to get user memory for address 0x%llx\n",
1329 addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001330 goto free_sgt;
1331 }
1332
Oded Gabbayc2164772019-02-16 00:39:24 +02001333 hl_debugfs_add_userptr(hdev, userptr);
1334
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001335 return 0;
1336
1337free_sgt:
1338 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001339 return rc;
1340}
1341
1342/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001343 * hl_unpin_host_memory - unpins a chunk of host memory.
1344 * @hdev: pointer to the habanalabs device structure
1345 * @userptr: pointer to hl_userptr structure
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001346 *
1347 * This function does the following:
1348 * - Unpins the physical pages related to the host memory
1349 * - Free the SG list
1350 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001351void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001352{
1353 struct page **pages;
1354
Oded Gabbayc2164772019-02-16 00:39:24 +02001355 hl_debugfs_remove_userptr(hdev, userptr);
1356
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001357 if (userptr->dma_mapped)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001358 hdev->asic_funcs->hl_dma_unmap_sg(hdev, userptr->sgt->sgl,
1359 userptr->sgt->nents,
1360 userptr->dir);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001361
1362 pages = frame_vector_pages(userptr->vec);
1363 if (!IS_ERR(pages)) {
1364 int i;
1365
1366 for (i = 0; i < frame_vector_count(userptr->vec); i++)
1367 set_page_dirty_lock(pages[i]);
1368 }
1369 put_vaddr_frames(userptr->vec);
1370 frame_vector_destroy(userptr->vec);
1371
1372 list_del(&userptr->job_node);
1373
1374 sg_free_table(userptr->sgt);
1375 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001376}
1377
1378/*
1379 * hl_userptr_delete_list - clear userptr list
1380 *
1381 * @hdev : pointer to the habanalabs device structure
1382 * @userptr_list : pointer to the list to clear
1383 *
1384 * This function does the following:
1385 * - Iterates over the list and unpins the host memory and frees the userptr
1386 * structure.
1387 */
1388void hl_userptr_delete_list(struct hl_device *hdev,
1389 struct list_head *userptr_list)
1390{
1391 struct hl_userptr *userptr, *tmp;
1392
1393 list_for_each_entry_safe(userptr, tmp, userptr_list, job_node) {
1394 hl_unpin_host_memory(hdev, userptr);
1395 kfree(userptr);
1396 }
1397
1398 INIT_LIST_HEAD(userptr_list);
1399}
1400
1401/*
1402 * hl_userptr_is_pinned - returns whether the given userptr is pinned
1403 *
1404 * @hdev : pointer to the habanalabs device structure
1405 * @userptr_list : pointer to the list to clear
1406 * @userptr : pointer to userptr to check
1407 *
1408 * This function does the following:
1409 * - Iterates over the list and checks if the given userptr is in it, means is
1410 * pinned. If so, returns true, otherwise returns false.
1411 */
1412bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr,
1413 u32 size, struct list_head *userptr_list,
1414 struct hl_userptr **userptr)
1415{
1416 list_for_each_entry((*userptr), userptr_list, job_node) {
1417 if ((addr == (*userptr)->addr) && (size == (*userptr)->size))
1418 return true;
1419 }
1420
1421 return false;
1422}
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001423
1424/*
1425 * hl_va_range_init - initialize virtual addresses range
1426 *
1427 * @hdev : pointer to the habanalabs device structure
1428 * @va_range : pointer to the range to initialize
1429 * @start : range start address
1430 * @end : range end address
1431 *
1432 * This function does the following:
1433 * - Initializes the virtual addresses list of the given range with the given
1434 * addresses.
1435 */
1436static int hl_va_range_init(struct hl_device *hdev,
1437 struct hl_va_range *va_range, u64 start, u64 end)
1438{
1439 int rc;
1440
1441 INIT_LIST_HEAD(&va_range->list);
1442
1443 /* PAGE_SIZE alignment */
1444
1445 if (start & (PAGE_SIZE - 1)) {
1446 start &= PAGE_MASK;
1447 start += PAGE_SIZE;
1448 }
1449
1450 if (end & (PAGE_SIZE - 1))
1451 end &= PAGE_MASK;
1452
1453 if (start >= end) {
1454 dev_err(hdev->dev, "too small vm range for va list\n");
1455 return -EFAULT;
1456 }
1457
1458 rc = add_va_block(hdev, va_range, start, end);
1459
1460 if (rc) {
1461 dev_err(hdev->dev, "Failed to init host va list\n");
1462 return rc;
1463 }
1464
1465 va_range->start_addr = start;
1466 va_range->end_addr = end;
1467
1468 return 0;
1469}
1470
1471/*
1472 * hl_vm_ctx_init_with_ranges - initialize virtual memory for context
1473 *
1474 * @ctx : pointer to the habanalabs context structure
1475 * @host_range_start : host virtual addresses range start
1476 * @host_range_end : host virtual addresses range end
1477 * @dram_range_start : dram virtual addresses range start
1478 * @dram_range_end : dram virtual addresses range end
1479 *
1480 * This function initializes the following:
1481 * - MMU for context
1482 * - Virtual address to area descriptor hashtable
1483 * - Virtual block list of available virtual memory
1484 */
Oded Gabbay5e6e0232019-02-27 12:15:16 +02001485static int hl_vm_ctx_init_with_ranges(struct hl_ctx *ctx, u64 host_range_start,
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001486 u64 host_range_end, u64 dram_range_start,
1487 u64 dram_range_end)
1488{
1489 struct hl_device *hdev = ctx->hdev;
1490 int rc;
1491
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001492 rc = hl_mmu_ctx_init(ctx);
1493 if (rc) {
1494 dev_err(hdev->dev, "failed to init context %d\n", ctx->asid);
1495 return rc;
1496 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001497
1498 mutex_init(&ctx->mem_hash_lock);
1499 hash_init(ctx->mem_hash);
1500
1501 mutex_init(&ctx->host_va_range.lock);
1502
1503 rc = hl_va_range_init(hdev, &ctx->host_va_range, host_range_start,
1504 host_range_end);
1505 if (rc) {
1506 dev_err(hdev->dev, "failed to init host vm range\n");
1507 goto host_vm_err;
1508 }
1509
1510 mutex_init(&ctx->dram_va_range.lock);
1511
1512 rc = hl_va_range_init(hdev, &ctx->dram_va_range, dram_range_start,
1513 dram_range_end);
1514 if (rc) {
1515 dev_err(hdev->dev, "failed to init dram vm range\n");
1516 goto dram_vm_err;
1517 }
1518
Oded Gabbayc2164772019-02-16 00:39:24 +02001519 hl_debugfs_add_ctx_mem_hash(hdev, ctx);
1520
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001521 return 0;
1522
1523dram_vm_err:
1524 mutex_destroy(&ctx->dram_va_range.lock);
1525
1526 mutex_lock(&ctx->host_va_range.lock);
1527 clear_va_list_locked(hdev, &ctx->host_va_range.list);
1528 mutex_unlock(&ctx->host_va_range.lock);
1529host_vm_err:
1530 mutex_destroy(&ctx->host_va_range.lock);
1531 mutex_destroy(&ctx->mem_hash_lock);
1532 hl_mmu_ctx_fini(ctx);
1533
1534 return rc;
1535}
1536
1537int hl_vm_ctx_init(struct hl_ctx *ctx)
1538{
1539 struct asic_fixed_properties *prop = &ctx->hdev->asic_prop;
1540 u64 host_range_start, host_range_end, dram_range_start,
1541 dram_range_end;
1542
1543 atomic64_set(&ctx->dram_phys_mem, 0);
1544
1545 /*
1546 * - If MMU is enabled, init the ranges as usual.
1547 * - If MMU is disabled, in case of host mapping, the returned address
1548 * is the given one.
1549 * In case of DRAM mapping, the returned address is the physical
1550 * address of the memory related to the given handle.
1551 */
1552 if (ctx->hdev->mmu_enable) {
1553 dram_range_start = prop->va_space_dram_start_address;
1554 dram_range_end = prop->va_space_dram_end_address;
1555 host_range_start = prop->va_space_host_start_address;
1556 host_range_end = prop->va_space_host_end_address;
1557 } else {
1558 dram_range_start = prop->dram_user_base_address;
1559 dram_range_end = prop->dram_end_address;
1560 host_range_start = prop->dram_user_base_address;
1561 host_range_end = prop->dram_end_address;
1562 }
1563
1564 return hl_vm_ctx_init_with_ranges(ctx, host_range_start, host_range_end,
1565 dram_range_start, dram_range_end);
1566}
1567
1568/*
1569 * hl_va_range_fini - clear a virtual addresses range
1570 *
1571 * @hdev : pointer to the habanalabs structure
1572 * va_range : pointer to virtual addresses range
1573 *
1574 * This function initializes the following:
1575 * - Checks that the given range contains the whole initial range
1576 * - Frees the virtual addresses block list and its lock
1577 */
1578static void hl_va_range_fini(struct hl_device *hdev,
1579 struct hl_va_range *va_range)
1580{
1581 struct hl_vm_va_block *va_block;
1582
1583 if (list_empty(&va_range->list)) {
1584 dev_warn(hdev->dev,
1585 "va list should not be empty on cleanup!\n");
1586 goto out;
1587 }
1588
1589 if (!list_is_singular(&va_range->list)) {
1590 dev_warn(hdev->dev,
1591 "va list should not contain multiple blocks on cleanup!\n");
1592 goto free_va_list;
1593 }
1594
1595 va_block = list_first_entry(&va_range->list, typeof(*va_block), node);
1596
1597 if (va_block->start != va_range->start_addr ||
1598 va_block->end != va_range->end_addr) {
1599 dev_warn(hdev->dev,
1600 "wrong va block on cleanup, from 0x%llx to 0x%llx\n",
1601 va_block->start, va_block->end);
1602 goto free_va_list;
1603 }
1604
1605free_va_list:
1606 mutex_lock(&va_range->lock);
1607 clear_va_list_locked(hdev, &va_range->list);
1608 mutex_unlock(&va_range->lock);
1609
1610out:
1611 mutex_destroy(&va_range->lock);
1612}
1613
1614/*
1615 * hl_vm_ctx_fini - virtual memory teardown of context
1616 *
1617 * @ctx : pointer to the habanalabs context structure
1618 *
1619 * This function perform teardown the following:
1620 * - Virtual block list of available virtual memory
1621 * - Virtual address to area descriptor hashtable
1622 * - MMU for context
1623 *
1624 * In addition this function does the following:
1625 * - Unmaps the existing hashtable nodes if the hashtable is not empty. The
1626 * hashtable should be empty as no valid mappings should exist at this
1627 * point.
1628 * - Frees any existing physical page list from the idr which relates to the
1629 * current context asid.
1630 * - This function checks the virtual block list for correctness. At this point
1631 * the list should contain one element which describes the whole virtual
1632 * memory range of the context. Otherwise, a warning is printed.
1633 */
1634void hl_vm_ctx_fini(struct hl_ctx *ctx)
1635{
1636 struct hl_device *hdev = ctx->hdev;
1637 struct hl_vm *vm = &hdev->vm;
1638 struct hl_vm_phys_pg_pack *phys_pg_list;
1639 struct hl_vm_hash_node *hnode;
1640 struct hlist_node *tmp_node;
1641 int i;
1642
Oded Gabbayc2164772019-02-16 00:39:24 +02001643 hl_debugfs_remove_ctx_mem_hash(hdev, ctx);
1644
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001645 if (!hash_empty(ctx->mem_hash))
1646 dev_notice(hdev->dev, "ctx is freed while it has va in use\n");
1647
1648 hash_for_each_safe(ctx->mem_hash, i, tmp_node, hnode, node) {
1649 dev_dbg(hdev->dev,
1650 "hl_mem_hash_node of vaddr 0x%llx of asid %d is still alive\n",
1651 hnode->vaddr, ctx->asid);
1652 unmap_device_va(ctx, hnode->vaddr);
1653 }
1654
1655 spin_lock(&vm->idr_lock);
1656 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_list, i)
1657 if (phys_pg_list->asid == ctx->asid) {
1658 dev_dbg(hdev->dev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001659 "page list 0x%px of asid %d is still alive\n",
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001660 phys_pg_list, ctx->asid);
Tomer Tayarc8113752019-08-04 07:03:41 +00001661 atomic64_sub(phys_pg_list->total_size,
1662 &hdev->dram_used_mem);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001663 free_phys_pg_pack(hdev, phys_pg_list);
1664 idr_remove(&vm->phys_pg_pack_handles, i);
1665 }
1666 spin_unlock(&vm->idr_lock);
1667
1668 hl_va_range_fini(hdev, &ctx->dram_va_range);
1669 hl_va_range_fini(hdev, &ctx->host_va_range);
1670
1671 mutex_destroy(&ctx->mem_hash_lock);
1672 hl_mmu_ctx_fini(ctx);
1673}
1674
1675/*
1676 * hl_vm_init - initialize virtual memory module
1677 *
1678 * @hdev : pointer to the habanalabs device structure
1679 *
1680 * This function initializes the following:
1681 * - MMU module
1682 * - DRAM physical pages pool of 2MB
1683 * - Idr for device memory allocation handles
1684 */
1685int hl_vm_init(struct hl_device *hdev)
1686{
1687 struct asic_fixed_properties *prop = &hdev->asic_prop;
1688 struct hl_vm *vm = &hdev->vm;
1689 int rc;
1690
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001691 vm->dram_pg_pool = gen_pool_create(__ffs(prop->dram_page_size), -1);
1692 if (!vm->dram_pg_pool) {
1693 dev_err(hdev->dev, "Failed to create dram page pool\n");
Oded Gabbay37d68ce2019-05-29 14:43:04 +03001694 return -ENOMEM;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001695 }
1696
1697 kref_init(&vm->dram_pg_pool_refcount);
1698
1699 rc = gen_pool_add(vm->dram_pg_pool, prop->dram_user_base_address,
1700 prop->dram_end_address - prop->dram_user_base_address,
1701 -1);
1702
1703 if (rc) {
1704 dev_err(hdev->dev,
1705 "Failed to add memory to dram page pool %d\n", rc);
1706 goto pool_add_err;
1707 }
1708
1709 spin_lock_init(&vm->idr_lock);
1710 idr_init(&vm->phys_pg_pack_handles);
1711
1712 atomic64_set(&hdev->dram_used_mem, 0);
1713
1714 vm->init_done = true;
1715
1716 return 0;
1717
1718pool_add_err:
1719 gen_pool_destroy(vm->dram_pg_pool);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001720
1721 return rc;
1722}
1723
1724/*
1725 * hl_vm_fini - virtual memory module teardown
1726 *
1727 * @hdev : pointer to the habanalabs device structure
1728 *
1729 * This function perform teardown to the following:
1730 * - Idr for device memory allocation handles
1731 * - DRAM physical pages pool of 2MB
1732 * - MMU module
1733 */
1734void hl_vm_fini(struct hl_device *hdev)
1735{
1736 struct hl_vm *vm = &hdev->vm;
1737
1738 if (!vm->init_done)
1739 return;
1740
1741 /*
1742 * At this point all the contexts should be freed and hence no DRAM
1743 * memory should be in use. Hence the DRAM pool should be freed here.
1744 */
1745 if (kref_put(&vm->dram_pg_pool_refcount, dram_pg_pool_do_release) != 1)
1746 dev_warn(hdev->dev, "dram_pg_pool was not destroyed on %s\n",
1747 __func__);
1748
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001749 vm->init_done = false;
1750}