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