blob: 36de0d05d3a2d2a57be8b4ee0f264ae711e23967 [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"
Greg Kroah-Hartman7b16a152020-07-28 19:18:51 +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
Omer Shpigelman0feaf862019-02-16 00:39:22 +020015#define HL_MMU_DEBUG 0
16
Moti Haimovskib19dc672020-11-18 20:15:29 +020017/* use small pages for supporting non-pow2 (32M/40M/48M) DRAM phys page sizes */
18#define DRAM_POOL_PAGE_SIZE SZ_8M
19
Omer Shpigelman0feaf862019-02-16 00:39:22 +020020/*
21 * The va ranges in context object contain a list with the available chunks of
22 * device virtual memory.
23 * There is one range for host allocations and one for DRAM allocations.
24 *
25 * On initialization each range contains one chunk of all of its available
26 * virtual range which is a half of the total device virtual range.
27 *
28 * On each mapping of physical pages, a suitable virtual range chunk (with a
29 * minimum size) is selected from the list. If the chunk size equals the
30 * requested size, the chunk is returned. Otherwise, the chunk is split into
31 * two chunks - one to return as result and a remainder to stay in the list.
32 *
33 * On each Unmapping of a virtual address, the relevant virtual chunk is
34 * returned to the list. The chunk is added to the list and if its edges match
35 * the edges of the adjacent chunks (means a contiguous chunk can be created),
36 * the chunks are merged.
37 *
38 * On finish, the list is checked to have only one chunk of all the relevant
39 * virtual range (which is a half of the device total virtual range).
40 * If not (means not all mappings were unmapped), a warning is printed.
41 */
42
43/*
Omer Shpigelman3b762f52020-12-09 13:28:46 +020044 * alloc_device_memory() - allocate device memory.
45 * @ctx: pointer to the context structure.
46 * @args: host parameters containing the requested size.
47 * @ret_handle: result handle.
Omer Shpigelman0feaf862019-02-16 00:39:22 +020048 *
49 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +020050 * - Allocate the requested size rounded up to 'dram_page_size' pages.
51 * - Return unique handle for later map/unmap/free.
Omer Shpigelman0feaf862019-02-16 00:39:22 +020052 */
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;
Moti Haimovskib19dc672020-11-18 20:15:29 +020060 u32 num_curr_pgs, page_size;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +020061 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;
Moti Haimovskib19dc672020-11-18 20:15:29 +020066 num_pgs = DIV_ROUND_UP_ULL(args->alloc.mem_size, page_size);
67 total_size = num_pgs * page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +020068
Ofir Bitton08391522020-08-11 08:57:45 +030069 if (!total_size) {
70 dev_err(hdev->dev, "Cannot allocate 0 bytes\n");
71 return -EINVAL;
72 }
73
Omer Shpigelman0feaf862019-02-16 00:39:22 +020074 contiguous = args->flags & HL_MEM_CONTIGUOUS;
75
76 if (contiguous) {
77 paddr = (u64) gen_pool_alloc(vm->dram_pg_pool, total_size);
78 if (!paddr) {
79 dev_err(hdev->dev,
Oded Gabbayfc6121e2020-09-23 14:07:32 +030080 "failed to allocate %llu contiguous pages with total size of %llu\n",
81 num_pgs, total_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +020082 return -ENOMEM;
83 }
farah kassabri03df1362020-05-06 11:17:38 +030084
85 if (hdev->memory_scrub) {
86 rc = hdev->asic_funcs->scrub_device_mem(hdev, paddr,
87 total_size);
88 if (rc) {
89 dev_err(hdev->dev,
90 "Failed to scrub contiguous device memory\n");
91 goto pages_pack_err;
92 }
93 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +020094 }
95
96 phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL);
97 if (!phys_pg_pack) {
98 rc = -ENOMEM;
99 goto pages_pack_err;
100 }
101
102 phys_pg_pack->vm_type = VM_TYPE_PHYS_PACK;
103 phys_pg_pack->asid = ctx->asid;
104 phys_pg_pack->npages = num_pgs;
105 phys_pg_pack->page_size = page_size;
106 phys_pg_pack->total_size = total_size;
107 phys_pg_pack->flags = args->flags;
108 phys_pg_pack->contiguous = contiguous;
109
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200110 phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL);
Ofir Bitton08391522020-08-11 08:57:45 +0300111 if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200112 rc = -ENOMEM;
113 goto pages_arr_err;
114 }
115
116 if (phys_pg_pack->contiguous) {
117 for (i = 0 ; i < num_pgs ; i++)
118 phys_pg_pack->pages[i] = paddr + i * page_size;
119 } else {
120 for (i = 0 ; i < num_pgs ; i++) {
121 phys_pg_pack->pages[i] = (u64) gen_pool_alloc(
122 vm->dram_pg_pool,
123 page_size);
124 if (!phys_pg_pack->pages[i]) {
125 dev_err(hdev->dev,
Oded Gabbaycab8e3e2019-03-27 09:44:28 +0200126 "Failed to allocate device memory (out of memory)\n");
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200127 rc = -ENOMEM;
128 goto page_err;
129 }
130
farah kassabri03df1362020-05-06 11:17:38 +0300131 if (hdev->memory_scrub) {
132 rc = hdev->asic_funcs->scrub_device_mem(hdev,
133 phys_pg_pack->pages[i],
134 page_size);
135 if (rc) {
136 dev_err(hdev->dev,
137 "Failed to scrub device memory\n");
138 goto page_err;
139 }
140 }
141
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200142 num_curr_pgs++;
143 }
144 }
145
146 spin_lock(&vm->idr_lock);
147 handle = idr_alloc(&vm->phys_pg_pack_handles, phys_pg_pack, 1, 0,
Wei Yongjun668ae722019-02-22 05:46:01 +0000148 GFP_ATOMIC);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200149 spin_unlock(&vm->idr_lock);
150
151 if (handle < 0) {
152 dev_err(hdev->dev, "Failed to get handle for page\n");
153 rc = -EFAULT;
154 goto idr_err;
155 }
156
157 for (i = 0 ; i < num_pgs ; i++)
158 kref_get(&vm->dram_pg_pool_refcount);
159
160 phys_pg_pack->handle = handle;
161
162 atomic64_add(phys_pg_pack->total_size, &ctx->dram_phys_mem);
163 atomic64_add(phys_pg_pack->total_size, &hdev->dram_used_mem);
164
165 *ret_handle = handle;
166
167 return 0;
168
169idr_err:
170page_err:
171 if (!phys_pg_pack->contiguous)
172 for (i = 0 ; i < num_curr_pgs ; i++)
173 gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[i],
174 page_size);
175
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200176 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200177pages_arr_err:
178 kfree(phys_pg_pack);
179pages_pack_err:
180 if (contiguous)
181 gen_pool_free(vm->dram_pg_pool, paddr, total_size);
182
183 return rc;
184}
185
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200186/**
187 * dma_map_host_va() - DMA mapping of the given host virtual address.
188 * @hdev: habanalabs device structure.
189 * @addr: the host virtual address of the memory area.
190 * @size: the size of the memory area.
191 * @p_userptr: pointer to result userptr structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200192 *
193 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200194 * - Allocate userptr structure.
195 * - Pin the given host memory using the userptr structure.
196 * - Perform DMA mapping to have the DMA addresses of the pages.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200197 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300198static int dma_map_host_va(struct hl_device *hdev, u64 addr, u64 size,
199 struct hl_userptr **p_userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200200{
201 struct hl_userptr *userptr;
202 int rc;
203
204 userptr = kzalloc(sizeof(*userptr), GFP_KERNEL);
205 if (!userptr) {
206 rc = -ENOMEM;
207 goto userptr_err;
208 }
209
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300210 rc = hl_pin_host_memory(hdev, addr, size, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200211 if (rc) {
212 dev_err(hdev->dev, "Failed to pin host memory\n");
213 goto pin_err;
214 }
215
216 rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl,
217 userptr->sgt->nents, DMA_BIDIRECTIONAL);
218 if (rc) {
219 dev_err(hdev->dev, "failed to map sgt with DMA region\n");
220 goto dma_map_err;
221 }
222
223 userptr->dma_mapped = true;
224 userptr->dir = DMA_BIDIRECTIONAL;
225 userptr->vm_type = VM_TYPE_USERPTR;
226
227 *p_userptr = userptr;
228
229 return 0;
230
231dma_map_err:
232 hl_unpin_host_memory(hdev, userptr);
233pin_err:
234 kfree(userptr);
235userptr_err:
236
237 return rc;
238}
239
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200240/**
241 * dma_unmap_host_va() - DMA unmapping of the given host virtual address.
242 * @hdev: habanalabs device structure.
243 * @userptr: userptr to free.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200244 *
245 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200246 * - Unpins the physical pages.
247 * - Frees the userptr structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200248 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300249static void dma_unmap_host_va(struct hl_device *hdev,
250 struct hl_userptr *userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200251{
252 hl_unpin_host_memory(hdev, userptr);
253 kfree(userptr);
254}
255
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200256/**
257 * dram_pg_pool_do_release() - free DRAM pages pool
258 * @ref: pointer to reference object.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200259 *
260 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200261 * - Frees the idr structure of physical pages handles.
262 * - Frees the generic pool of DRAM physical pages.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200263 */
264static void dram_pg_pool_do_release(struct kref *ref)
265{
266 struct hl_vm *vm = container_of(ref, struct hl_vm,
267 dram_pg_pool_refcount);
268
269 /*
270 * free the idr here as only here we know for sure that there are no
271 * allocated physical pages and hence there are no handles in use
272 */
273 idr_destroy(&vm->phys_pg_pack_handles);
274 gen_pool_destroy(vm->dram_pg_pool);
275}
276
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200277/**
278 * free_phys_pg_pack() - free physical page pack.
279 * @hdev: habanalabs device structure.
280 * @phys_pg_pack: physical page pack to free.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200281 *
282 * This function does the following:
283 * - For DRAM memory only, iterate over the pack and free each physical block
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200284 * structure by returning it to the general pool.
285 * - Free the hl_vm_phys_pg_pack structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200286 */
287static void free_phys_pg_pack(struct hl_device *hdev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300288 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200289{
290 struct hl_vm *vm = &hdev->vm;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200291 u64 i;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200292
293 if (!phys_pg_pack->created_from_userptr) {
294 if (phys_pg_pack->contiguous) {
295 gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[0],
296 phys_pg_pack->total_size);
297
298 for (i = 0; i < phys_pg_pack->npages ; i++)
299 kref_put(&vm->dram_pg_pool_refcount,
300 dram_pg_pool_do_release);
301 } else {
302 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
303 gen_pool_free(vm->dram_pg_pool,
304 phys_pg_pack->pages[i],
305 phys_pg_pack->page_size);
306 kref_put(&vm->dram_pg_pool_refcount,
307 dram_pg_pool_do_release);
308 }
309 }
310 }
311
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200312 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200313 kfree(phys_pg_pack);
314}
315
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200316/**
317 * free_device_memory() - free device memory.
318 * @ctx: pointer to the context structure.
Omer Shpigelmanf19040c2020-12-09 13:34:11 +0200319 * @args: host parameters containing the requested size.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200320 *
321 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200322 * - Free the device memory related to the given handle.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200323 */
Omer Shpigelmanf19040c2020-12-09 13:34:11 +0200324static int free_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200325{
326 struct hl_device *hdev = ctx->hdev;
327 struct hl_vm *vm = &hdev->vm;
328 struct hl_vm_phys_pg_pack *phys_pg_pack;
Omer Shpigelmanf19040c2020-12-09 13:34:11 +0200329 u32 handle = args->free.handle;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200330
331 spin_lock(&vm->idr_lock);
332 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
333 if (phys_pg_pack) {
334 if (atomic_read(&phys_pg_pack->mapping_cnt) > 0) {
335 dev_err(hdev->dev, "handle %u is mapped, cannot free\n",
336 handle);
337 spin_unlock(&vm->idr_lock);
338 return -EINVAL;
339 }
340
341 /*
342 * must remove from idr before the freeing of the physical
343 * pages as the refcount of the pool is also the trigger of the
344 * idr destroy
345 */
346 idr_remove(&vm->phys_pg_pack_handles, handle);
347 spin_unlock(&vm->idr_lock);
348
349 atomic64_sub(phys_pg_pack->total_size, &ctx->dram_phys_mem);
350 atomic64_sub(phys_pg_pack->total_size, &hdev->dram_used_mem);
351
352 free_phys_pg_pack(hdev, phys_pg_pack);
353 } else {
354 spin_unlock(&vm->idr_lock);
355 dev_err(hdev->dev,
356 "free device memory failed, no match for handle %u\n",
357 handle);
358 return -EINVAL;
359 }
360
361 return 0;
362}
363
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200364/**
365 * clear_va_list_locked() - free virtual addresses list.
366 * @hdev: habanalabs device structure.
367 * @va_list: list of virtual addresses to free.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200368 *
369 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200370 * - Iterate over the list and free each virtual addresses block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200371 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200372 * This function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200373 */
374static void clear_va_list_locked(struct hl_device *hdev,
375 struct list_head *va_list)
376{
377 struct hl_vm_va_block *va_block, *tmp;
378
379 list_for_each_entry_safe(va_block, tmp, va_list, node) {
380 list_del(&va_block->node);
381 kfree(va_block);
382 }
383}
384
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200385/**
386 * print_va_list_locked() - print virtual addresses list.
387 * @hdev: habanalabs device structure.
388 * @va_list: list of virtual addresses to print.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200389 *
390 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200391 * - Iterate over the list and print each virtual addresses block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200392 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200393 * This function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200394 */
395static void print_va_list_locked(struct hl_device *hdev,
396 struct list_head *va_list)
397{
398#if HL_MMU_DEBUG
399 struct hl_vm_va_block *va_block;
400
401 dev_dbg(hdev->dev, "print va list:\n");
402
403 list_for_each_entry(va_block, va_list, node)
404 dev_dbg(hdev->dev,
405 "va block, start: 0x%llx, end: 0x%llx, size: %llu\n",
406 va_block->start, va_block->end, va_block->size);
407#endif
408}
409
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200410/**
411 * merge_va_blocks_locked() - merge a virtual block if possible.
412 * @hdev: pointer to the habanalabs device structure.
413 * @va_list: pointer to the virtual addresses block list.
414 * @va_block: virtual block to merge with adjacent blocks.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200415 *
416 * This function does the following:
417 * - Merge the given blocks with the adjacent blocks if their virtual ranges
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200418 * create a contiguous virtual range.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200419 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200420 * This Function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200421 */
422static void merge_va_blocks_locked(struct hl_device *hdev,
423 struct list_head *va_list, struct hl_vm_va_block *va_block)
424{
425 struct hl_vm_va_block *prev, *next;
426
427 prev = list_prev_entry(va_block, node);
428 if (&prev->node != va_list && prev->end + 1 == va_block->start) {
429 prev->end = va_block->end;
430 prev->size = prev->end - prev->start;
431 list_del(&va_block->node);
432 kfree(va_block);
433 va_block = prev;
434 }
435
436 next = list_next_entry(va_block, node);
437 if (&next->node != va_list && va_block->end + 1 == next->start) {
438 next->start = va_block->start;
439 next->size = next->end - next->start;
440 list_del(&va_block->node);
441 kfree(va_block);
442 }
443}
444
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200445/**
446 * add_va_block_locked() - add a virtual block to the virtual addresses list.
447 * @hdev: pointer to the habanalabs device structure.
448 * @va_list: pointer to the virtual addresses block list.
449 * @start: start virtual address.
450 * @end: end virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200451 *
452 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200453 * - Add the given block to the virtual blocks list and merge with other blocks
454 * if a contiguous virtual block can be created.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200455 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200456 * This Function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200457 */
458static int add_va_block_locked(struct hl_device *hdev,
459 struct list_head *va_list, u64 start, u64 end)
460{
461 struct hl_vm_va_block *va_block, *res = NULL;
462 u64 size = end - start;
463
464 print_va_list_locked(hdev, va_list);
465
466 list_for_each_entry(va_block, va_list, node) {
467 /* TODO: remove upon matureness */
468 if (hl_mem_area_crosses_range(start, size, va_block->start,
469 va_block->end)) {
470 dev_err(hdev->dev,
471 "block crossing ranges at start 0x%llx, end 0x%llx\n",
472 va_block->start, va_block->end);
473 return -EINVAL;
474 }
475
476 if (va_block->end < start)
477 res = va_block;
478 }
479
480 va_block = kmalloc(sizeof(*va_block), GFP_KERNEL);
481 if (!va_block)
482 return -ENOMEM;
483
484 va_block->start = start;
485 va_block->end = end;
486 va_block->size = size;
487
488 if (!res)
489 list_add(&va_block->node, va_list);
490 else
491 list_add(&va_block->node, &res->node);
492
493 merge_va_blocks_locked(hdev, va_list, va_block);
494
495 print_va_list_locked(hdev, va_list);
496
497 return 0;
498}
499
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200500/**
501 * add_va_block() - wrapper for add_va_block_locked.
502 * @hdev: pointer to the habanalabs device structure.
503 * @va_list: pointer to the virtual addresses block list.
504 * @start: start virtual address.
505 * @end: end virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200506 *
507 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200508 * - Takes the list lock and calls add_va_block_locked.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200509 */
510static inline int add_va_block(struct hl_device *hdev,
511 struct hl_va_range *va_range, u64 start, u64 end)
512{
513 int rc;
514
515 mutex_lock(&va_range->lock);
516 rc = add_va_block_locked(hdev, &va_range->list, start, end);
517 mutex_unlock(&va_range->lock);
518
519 return rc;
520}
521
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200522/**
farah kassabri8d79ce12021-01-11 10:10:00 +0200523 * get_va_block() - get a virtual block for the given size and alignment.
524 *
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300525 * @hdev: pointer to the habanalabs device structure.
526 * @va_range: pointer to the virtual addresses range.
527 * @size: requested block size.
528 * @hint_addr: hint for requested address by the user.
529 * @va_block_align: required alignment of the virtual block start address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200530 *
531 * This function does the following:
532 * - Iterate on the virtual block list to find a suitable virtual block for the
farah kassabri8d79ce12021-01-11 10:10:00 +0200533 * given size, hint address and alignment.
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300534 * - Reserve the requested block and update the list.
535 * - Return the start address of the virtual block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200536 */
farah kassabri8d79ce12021-01-11 10:10:00 +0200537static u64 get_va_block(struct hl_device *hdev,
Moti Haimovskib19dc672020-11-18 20:15:29 +0200538 struct hl_va_range *va_range,
539 u64 size, u64 hint_addr, u32 va_block_align)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200540{
541 struct hl_vm_va_block *va_block, *new_va_block = NULL;
farah kassabri8d79ce12021-01-11 10:10:00 +0200542 u64 tmp_hint_addr, valid_start, valid_size, prev_start, prev_end,
543 align_mask, reserved_valid_start = 0, reserved_valid_size = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200544 bool add_prev = false;
farah kassabri8d79ce12021-01-11 10:10:00 +0200545 bool is_align_pow_2 = is_power_of_2(va_range->page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200546
farah kassabri8d79ce12021-01-11 10:10:00 +0200547 if (is_align_pow_2)
548 align_mask = ~((u64)va_block_align - 1);
549 else
550 /*
551 * with non-power-of-2 range we work only with page granularity
552 * and the start address is page aligned,
553 * so no need for alignment checking.
554 */
555 size = DIV_ROUND_UP_ULL(size, va_range->page_size) *
556 va_range->page_size;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000557
farah kassabri8d79ce12021-01-11 10:10:00 +0200558 tmp_hint_addr = hint_addr;
559
560 /* Check if we need to ignore hint address */
561 if ((is_align_pow_2 && (hint_addr & (va_block_align - 1))) ||
562 (!is_align_pow_2 &&
563 do_div(tmp_hint_addr, va_range->page_size))) {
564 dev_info(hdev->dev, "Hint address 0x%llx will be ignored\n",
565 hint_addr);
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300566 hint_addr = 0;
farah kassabri8d79ce12021-01-11 10:10:00 +0200567 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200568
569 mutex_lock(&va_range->lock);
570
571 print_va_list_locked(hdev, &va_range->list);
572
573 list_for_each_entry(va_block, &va_range->list, node) {
farah kassabri8d79ce12021-01-11 10:10:00 +0200574 /* Calc the first possible aligned addr */
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200575 valid_start = va_block->start;
576
farah kassabri8d79ce12021-01-11 10:10:00 +0200577 if (is_align_pow_2 && (valid_start & (va_block_align - 1))) {
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300578 valid_start &= align_mask;
579 valid_start += va_block_align;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200580 if (valid_start > va_block->end)
581 continue;
582 }
583
584 valid_size = va_block->end - valid_start;
farah kassabri8d79ce12021-01-11 10:10:00 +0200585 if (valid_size < size)
586 continue;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200587
farah kassabri8d79ce12021-01-11 10:10:00 +0200588 /* Pick the minimal length block which has the required size */
589 if (!new_va_block || (valid_size < reserved_valid_size)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200590 new_va_block = va_block;
Moti Haimovskib19dc672020-11-18 20:15:29 +0200591 reserved_valid_start = valid_start;
592 reserved_valid_size = valid_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200593 }
594
595 if (hint_addr && hint_addr >= valid_start &&
Moti Haimovskib19dc672020-11-18 20:15:29 +0200596 (hint_addr + size) <= va_block->end) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200597 new_va_block = va_block;
Moti Haimovskib19dc672020-11-18 20:15:29 +0200598 reserved_valid_start = hint_addr;
599 reserved_valid_size = valid_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200600 break;
601 }
602 }
603
604 if (!new_va_block) {
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200605 dev_err(hdev->dev, "no available va block for size %llu\n",
farah kassabri8d79ce12021-01-11 10:10:00 +0200606 size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200607 goto out;
608 }
609
farah kassabri8d79ce12021-01-11 10:10:00 +0200610 /*
611 * Check if there is some leftover range due to reserving the new
612 * va block, then return it to the main virtual addresses list.
613 */
Moti Haimovskib19dc672020-11-18 20:15:29 +0200614 if (reserved_valid_start > new_va_block->start) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200615 prev_start = new_va_block->start;
Moti Haimovskib19dc672020-11-18 20:15:29 +0200616 prev_end = reserved_valid_start - 1;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200617
Moti Haimovskib19dc672020-11-18 20:15:29 +0200618 new_va_block->start = reserved_valid_start;
619 new_va_block->size = reserved_valid_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200620
621 add_prev = true;
622 }
623
624 if (new_va_block->size > size) {
625 new_va_block->start += size;
626 new_va_block->size = new_va_block->end - new_va_block->start;
627 } else {
628 list_del(&new_va_block->node);
629 kfree(new_va_block);
630 }
631
632 if (add_prev)
633 add_va_block_locked(hdev, &va_range->list, prev_start,
634 prev_end);
635
636 print_va_list_locked(hdev, &va_range->list);
637out:
638 mutex_unlock(&va_range->lock);
639
Moti Haimovskib19dc672020-11-18 20:15:29 +0200640 return reserved_valid_start;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200641}
642
Moti Haimovskib19dc672020-11-18 20:15:29 +0200643/*
Ofir Bittonbe91b912020-10-22 15:04:10 +0300644 * hl_reserve_va_block() - reserve a virtual block of a given size.
645 * @hdev: pointer to the habanalabs device structure.
646 * @ctx: current context
647 * @type: virtual addresses range type.
648 * @size: requested block size.
Ofir Bitton412c41f2020-11-04 15:18:55 +0200649 * @alignment: required alignment in bytes of the virtual block start address,
650 * 0 means no alignment.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300651 *
652 * This function does the following:
653 * - Iterate on the virtual block list to find a suitable virtual block for the
Ofir Bitton412c41f2020-11-04 15:18:55 +0200654 * given size and alignment.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300655 * - Reserve the requested block and update the list.
656 * - Return the start address of the virtual block.
657 */
658u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
Ofir Bitton412c41f2020-11-04 15:18:55 +0200659 enum hl_va_range_type type, u32 size, u32 alignment)
Ofir Bittonbe91b912020-10-22 15:04:10 +0300660{
661 return get_va_block(hdev, ctx->va_range[type], size, 0,
Ofir Bitton412c41f2020-11-04 15:18:55 +0200662 max(alignment, ctx->va_range[type]->page_size));
Ofir Bittonbe91b912020-10-22 15:04:10 +0300663}
664
665/**
666 * hl_get_va_range_type() - get va_range type for the given address and size.
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200667 * @address: the start address of the area we want to validate.
668 * @size: the size in bytes of the area we want to validate.
669 * @type: returned va_range type.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300670 *
671 * Return: true if the area is inside a valid range, false otherwise.
672 */
673static int hl_get_va_range_type(struct hl_ctx *ctx, u64 address, u64 size,
674 enum hl_va_range_type *type)
675{
676 int i;
677
678 for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX; i++) {
679 if (hl_mem_area_inside_range(address, size,
680 ctx->va_range[i]->start_addr,
681 ctx->va_range[i]->end_addr)) {
682 *type = i;
683 return 0;
684 }
685 }
686
687 return -EINVAL;
688}
689
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200690/**
691 * hl_unreserve_va_block() - wrapper for add_va_block to unreserve a va block.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300692 * @hdev: pointer to the habanalabs device structure
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200693 * @ctx: pointer to the context structure.
694 * @start: start virtual address.
695 * @end: end virtual address.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300696 *
697 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200698 * - Takes the list lock and calls add_va_block_locked.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300699 */
700int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
701 u64 start_addr, u64 size)
702{
703 enum hl_va_range_type type;
704 int rc;
705
706 rc = hl_get_va_range_type(ctx, start_addr, size, &type);
707 if (rc) {
708 dev_err(hdev->dev,
709 "cannot find va_range for va %#llx size %llu",
710 start_addr, size);
711 return rc;
712 }
713
714 rc = add_va_block(hdev, ctx->va_range[type], start_addr,
715 start_addr + size - 1);
716 if (rc)
717 dev_warn(hdev->dev,
718 "add va block failed for vaddr: 0x%llx\n", start_addr);
719
720 return rc;
721}
722
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200723/**
724 * get_sg_info() - get number of pages and the DMA address from SG list.
725 * @sg: the SG list.
726 * @dma_addr: pointer to DMA address to return.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200727 *
728 * Calculate the number of consecutive pages described by the SG list. Take the
729 * offset of the address in the first page, add to it the length and round it up
730 * to the number of needed pages.
731 */
732static u32 get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
733{
734 *dma_addr = sg_dma_address(sg);
735
736 return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
737 (PAGE_SIZE - 1)) >> PAGE_SHIFT;
738}
739
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200740/**
741 * init_phys_pg_pack_from_userptr() - initialize physical page pack from host
742 * memory
743 * @ctx: pointer to the context structure.
744 * @userptr: userptr to initialize from.
745 * @pphys_pg_pack: result pointer.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200746 *
747 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200748 * - Pin the physical pages related to the given virtual block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200749 * - Create a physical page pack from the physical pages related to the given
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200750 * virtual block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200751 */
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000752static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx,
753 struct hl_userptr *userptr,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300754 struct hl_vm_phys_pg_pack **pphys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200755{
756 struct hl_vm_phys_pg_pack *phys_pg_pack;
757 struct scatterlist *sg;
758 dma_addr_t dma_addr;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200759 u64 page_mask, total_npages;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000760 u32 npages, page_size = PAGE_SIZE,
Omer Shpigelman64a7e292020-01-05 09:05:45 +0000761 huge_page_size = ctx->hdev->asic_prop.pmmu_huge.page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200762 bool first = true, is_huge_page_opt = true;
763 int rc, i, j;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000764 u32 pgs_in_huge_page = huge_page_size >> __ffs(page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200765
766 phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL);
767 if (!phys_pg_pack)
768 return -ENOMEM;
769
770 phys_pg_pack->vm_type = userptr->vm_type;
771 phys_pg_pack->created_from_userptr = true;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000772 phys_pg_pack->asid = ctx->asid;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200773 atomic_set(&phys_pg_pack->mapping_cnt, 1);
774
775 /* Only if all dma_addrs are aligned to 2MB and their
776 * sizes is at least 2MB, we can use huge page mapping.
777 * We limit the 2MB optimization to this condition,
778 * since later on we acquire the related VA range as one
779 * consecutive block.
780 */
781 total_npages = 0;
782 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
783 npages = get_sg_info(sg, &dma_addr);
784
785 total_npages += npages;
786
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000787 if ((npages % pgs_in_huge_page) ||
788 (dma_addr & (huge_page_size - 1)))
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200789 is_huge_page_opt = false;
790 }
791
792 if (is_huge_page_opt) {
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000793 page_size = huge_page_size;
794 do_div(total_npages, pgs_in_huge_page);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200795 }
796
797 page_mask = ~(((u64) page_size) - 1);
798
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200799 phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64),
800 GFP_KERNEL);
Ofir Bitton08391522020-08-11 08:57:45 +0300801 if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200802 rc = -ENOMEM;
803 goto page_pack_arr_mem_err;
804 }
805
806 phys_pg_pack->npages = total_npages;
807 phys_pg_pack->page_size = page_size;
808 phys_pg_pack->total_size = total_npages * page_size;
809
810 j = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200811 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
812 npages = get_sg_info(sg, &dma_addr);
813
814 /* align down to physical page size and save the offset */
815 if (first) {
816 first = false;
817 phys_pg_pack->offset = dma_addr & (page_size - 1);
818 dma_addr &= page_mask;
819 }
820
821 while (npages) {
822 phys_pg_pack->pages[j++] = dma_addr;
823 dma_addr += page_size;
824
825 if (is_huge_page_opt)
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000826 npages -= pgs_in_huge_page;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200827 else
828 npages--;
829 }
830 }
831
832 *pphys_pg_pack = phys_pg_pack;
833
834 return 0;
835
836page_pack_arr_mem_err:
837 kfree(phys_pg_pack);
838
839 return rc;
840}
841
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200842/**
843 * map_phys_pg_pack() - maps the physical page pack..
844 * @ctx: pointer to the context structure.
845 * @vaddr: start address of the virtual area to map from.
846 * @phys_pg_pack: the pack of physical pages to map to.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200847 *
848 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200849 * - Maps each chunk of virtual memory to matching physical chunk.
850 * - Stores number of successful mappings in the given argument.
851 * - Returns 0 on success, error code otherwise.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200852 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300853static int map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
854 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200855{
856 struct hl_device *hdev = ctx->hdev;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200857 u64 next_vaddr = vaddr, paddr, mapped_pg_cnt = 0, i;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200858 u32 page_size = phys_pg_pack->page_size;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200859 int rc = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200860
861 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
862 paddr = phys_pg_pack->pages[i];
863
Ofir Bitton5c054872020-10-22 15:13:10 +0300864 rc = hl_mmu_map_page(ctx, next_vaddr, paddr, page_size,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +0200865 (i + 1) == phys_pg_pack->npages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200866 if (rc) {
867 dev_err(hdev->dev,
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200868 "map failed for handle %u, npages: %llu, mapped: %llu",
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200869 phys_pg_pack->handle, phys_pg_pack->npages,
870 mapped_pg_cnt);
871 goto err;
872 }
873
874 mapped_pg_cnt++;
875 next_vaddr += page_size;
876 }
877
878 return 0;
879
880err:
881 next_vaddr = vaddr;
882 for (i = 0 ; i < mapped_pg_cnt ; i++) {
Ofir Bitton5c054872020-10-22 15:13:10 +0300883 if (hl_mmu_unmap_page(ctx, next_vaddr, page_size,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +0200884 (i + 1) == mapped_pg_cnt))
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200885 dev_warn_ratelimited(hdev->dev,
886 "failed to unmap handle %u, va: 0x%llx, pa: 0x%llx, page size: %u\n",
887 phys_pg_pack->handle, next_vaddr,
888 phys_pg_pack->pages[i], page_size);
889
890 next_vaddr += page_size;
891 }
892
893 return rc;
894}
895
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200896/**
897 * unmap_phys_pg_pack() - unmaps the physical page pack.
898 * @ctx: pointer to the context structure.
899 * @vaddr: start address of the virtual area to unmap.
900 * @phys_pg_pack: the pack of physical pages to unmap.
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300901 */
902static void unmap_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
903 struct hl_vm_phys_pg_pack *phys_pg_pack)
904{
905 struct hl_device *hdev = ctx->hdev;
906 u64 next_vaddr, i;
Oded Gabbay94883072021-01-11 17:49:30 +0200907 bool is_host_addr;
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300908 u32 page_size;
909
Oded Gabbay94883072021-01-11 17:49:30 +0200910 is_host_addr = !hl_is_dram_va(hdev, vaddr);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300911 page_size = phys_pg_pack->page_size;
912 next_vaddr = vaddr;
913
914 for (i = 0 ; i < phys_pg_pack->npages ; i++, next_vaddr += page_size) {
Ofir Bitton5c054872020-10-22 15:13:10 +0300915 if (hl_mmu_unmap_page(ctx, next_vaddr, page_size,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +0200916 (i + 1) == phys_pg_pack->npages))
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300917 dev_warn_ratelimited(hdev->dev,
918 "unmap failed for vaddr: 0x%llx\n", next_vaddr);
919
920 /*
921 * unmapping on Palladium can be really long, so avoid a CPU
922 * soft lockup bug by sleeping a little between unmapping pages
Oded Gabbay94883072021-01-11 17:49:30 +0200923 *
924 * In addition, when unmapping host memory we pass through
925 * the Linux kernel to unpin the pages and that takes a long
926 * time. Therefore, sleep every 32K pages to avoid soft lockup
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300927 */
Oded Gabbay94883072021-01-11 17:49:30 +0200928 if (hdev->pldm || (is_host_addr && (i & 0x7FFF) == 0))
929 usleep_range(50, 200);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300930 }
931}
932
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200933static int get_paddr_from_handle(struct hl_ctx *ctx, struct hl_mem_in *args,
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200934 u64 *paddr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200935{
936 struct hl_device *hdev = ctx->hdev;
937 struct hl_vm *vm = &hdev->vm;
938 struct hl_vm_phys_pg_pack *phys_pg_pack;
939 u32 handle;
940
941 handle = lower_32_bits(args->map_device.handle);
942 spin_lock(&vm->idr_lock);
943 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
944 if (!phys_pg_pack) {
945 spin_unlock(&vm->idr_lock);
946 dev_err(hdev->dev, "no match for handle %u\n", handle);
947 return -EINVAL;
948 }
949
950 *paddr = phys_pg_pack->pages[0];
951
952 spin_unlock(&vm->idr_lock);
953
954 return 0;
955}
956
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200957/**
958 * map_device_va() - map the given memory.
959 * @ctx: pointer to the context structure.
960 * @args: host parameters with handle/host virtual address.
961 * @device_addr: pointer to result device virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200962 *
963 * This function does the following:
964 * - If given a physical device memory handle, map to a device virtual block
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200965 * and return the start address of this block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200966 * - If given a host virtual address and size, find the related physical pages,
967 * map a device virtual block to this pages and return the start address of
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200968 * this block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200969 */
970static int map_device_va(struct hl_ctx *ctx, struct hl_mem_in *args,
971 u64 *device_addr)
972{
973 struct hl_device *hdev = ctx->hdev;
974 struct hl_vm *vm = &hdev->vm;
975 struct hl_vm_phys_pg_pack *phys_pg_pack;
976 struct hl_userptr *userptr = NULL;
977 struct hl_vm_hash_node *hnode;
Omer Shpigelman64a7e292020-01-05 09:05:45 +0000978 struct hl_va_range *va_range;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200979 enum vm_type_t *vm_type;
980 u64 ret_vaddr, hint_addr;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300981 u32 handle = 0, va_block_align;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200982 int rc;
983 bool is_userptr = args->flags & HL_MEM_USERPTR;
984
985 /* Assume failure */
986 *device_addr = 0;
987
988 if (is_userptr) {
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300989 u64 addr = args->map_host.host_virt_addr,
990 size = args->map_host.mem_size;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300991 u32 page_size = hdev->asic_prop.pmmu.page_size,
992 huge_page_size = hdev->asic_prop.pmmu_huge.page_size;
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300993
994 rc = dma_map_host_va(hdev, addr, size, &userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200995 if (rc) {
996 dev_err(hdev->dev, "failed to get userptr from va\n");
997 return rc;
998 }
999
Omer Shpigelman54bb6742019-11-14 18:23:55 +00001000 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001001 &phys_pg_pack);
1002 if (rc) {
1003 dev_err(hdev->dev,
1004 "unable to init page pack for vaddr 0x%llx\n",
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001005 addr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001006 goto init_page_pack_err;
1007 }
1008
1009 vm_type = (enum vm_type_t *) userptr;
1010 hint_addr = args->map_host.hint_addr;
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001011 handle = phys_pg_pack->handle;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001012
1013 /* get required alignment */
1014 if (phys_pg_pack->page_size == page_size) {
Ofir Bitton784b9162020-10-22 11:05:55 +03001015 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST];
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001016
1017 /*
1018 * huge page alignment may be needed in case of regular
1019 * page mapping, depending on the host VA alignment
1020 */
1021 if (addr & (huge_page_size - 1))
1022 va_block_align = page_size;
1023 else
1024 va_block_align = huge_page_size;
1025 } else {
1026 /*
1027 * huge page alignment is needed in case of huge page
1028 * mapping
1029 */
Ofir Bitton784b9162020-10-22 11:05:55 +03001030 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE];
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001031 va_block_align = huge_page_size;
1032 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001033 } else {
1034 handle = lower_32_bits(args->map_device.handle);
1035
1036 spin_lock(&vm->idr_lock);
1037 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
1038 if (!phys_pg_pack) {
1039 spin_unlock(&vm->idr_lock);
1040 dev_err(hdev->dev,
1041 "no match for handle %u\n", handle);
1042 return -EINVAL;
1043 }
1044
1045 /* increment now to avoid freeing device memory while mapping */
1046 atomic_inc(&phys_pg_pack->mapping_cnt);
1047
1048 spin_unlock(&vm->idr_lock);
1049
1050 vm_type = (enum vm_type_t *) phys_pg_pack;
1051
1052 hint_addr = args->map_device.hint_addr;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001053
Moti Haimovskib19dc672020-11-18 20:15:29 +02001054 /* DRAM VA alignment is the same as the MMU page size */
Ofir Bitton784b9162020-10-22 11:05:55 +03001055 va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM];
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001056 va_block_align = hdev->asic_prop.dmmu.page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001057 }
1058
1059 /*
1060 * relevant for mapping device physical memory only, as host memory is
1061 * implicitly shared
1062 */
1063 if (!is_userptr && !(phys_pg_pack->flags & HL_MEM_SHARED) &&
1064 phys_pg_pack->asid != ctx->asid) {
1065 dev_err(hdev->dev,
1066 "Failed to map memory, handle %u is not shared\n",
1067 handle);
1068 rc = -EPERM;
1069 goto shared_err;
1070 }
1071
1072 hnode = kzalloc(sizeof(*hnode), GFP_KERNEL);
1073 if (!hnode) {
1074 rc = -ENOMEM;
1075 goto hnode_err;
1076 }
1077
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001078 ret_vaddr = get_va_block(hdev, va_range, phys_pg_pack->total_size,
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001079 hint_addr, va_block_align);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001080 if (!ret_vaddr) {
1081 dev_err(hdev->dev, "no available va block for handle %u\n",
1082 handle);
1083 rc = -ENOMEM;
1084 goto va_block_err;
1085 }
1086
1087 mutex_lock(&ctx->mmu_lock);
1088
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001089 rc = map_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001090 if (rc) {
1091 mutex_unlock(&ctx->mmu_lock);
1092 dev_err(hdev->dev, "mapping page pack failed for handle %u\n",
1093 handle);
1094 goto map_err;
1095 }
1096
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001097 rc = hdev->asic_funcs->mmu_invalidate_cache(hdev, false, *vm_type);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001098
1099 mutex_unlock(&ctx->mmu_lock);
1100
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001101 if (rc) {
1102 dev_err(hdev->dev,
1103 "mapping handle %u failed due to MMU cache invalidation\n",
1104 handle);
1105 goto map_err;
1106 }
1107
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001108 ret_vaddr += phys_pg_pack->offset;
1109
1110 hnode->ptr = vm_type;
1111 hnode->vaddr = ret_vaddr;
1112
1113 mutex_lock(&ctx->mem_hash_lock);
1114 hash_add(ctx->mem_hash, &hnode->node, ret_vaddr);
1115 mutex_unlock(&ctx->mem_hash_lock);
1116
1117 *device_addr = ret_vaddr;
1118
1119 if (is_userptr)
1120 free_phys_pg_pack(hdev, phys_pg_pack);
1121
1122 return 0;
1123
1124map_err:
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001125 if (add_va_block(hdev, va_range, ret_vaddr,
1126 ret_vaddr + phys_pg_pack->total_size - 1))
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001127 dev_warn(hdev->dev,
1128 "release va block failed for handle 0x%x, vaddr: 0x%llx\n",
1129 handle, ret_vaddr);
1130
1131va_block_err:
1132 kfree(hnode);
1133hnode_err:
1134shared_err:
1135 atomic_dec(&phys_pg_pack->mapping_cnt);
1136 if (is_userptr)
1137 free_phys_pg_pack(hdev, phys_pg_pack);
1138init_page_pack_err:
1139 if (is_userptr)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001140 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001141
1142 return rc;
1143}
1144
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001145/**
1146 * unmap_device_va() - unmap the given device virtual address.
1147 * @ctx: pointer to the context structure.
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001148 * @args: host parameters with device virtual address to unmap.
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001149 * @ctx_free: true if in context free flow, false otherwise.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001150 *
1151 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001152 * - unmap the physical pages related to the given virtual address.
1153 * - return the device virtual block to the virtual block list.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001154 */
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001155static int unmap_device_va(struct hl_ctx *ctx, struct hl_mem_in *args,
1156 bool ctx_free)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001157{
1158 struct hl_device *hdev = ctx->hdev;
Moti Haimovskib19dc672020-11-18 20:15:29 +02001159 struct asic_fixed_properties *prop = &hdev->asic_prop;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001160 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
1161 struct hl_vm_hash_node *hnode = NULL;
1162 struct hl_userptr *userptr = NULL;
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001163 struct hl_va_range *va_range;
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001164 u64 vaddr = args->unmap.device_virt_addr;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001165 enum vm_type_t *vm_type;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001166 bool is_userptr;
Tomer Tayarc68f1ba2020-06-01 09:56:47 +03001167 int rc = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001168
1169 /* protect from double entrance */
1170 mutex_lock(&ctx->mem_hash_lock);
1171 hash_for_each_possible(ctx->mem_hash, hnode, node, (unsigned long)vaddr)
1172 if (vaddr == hnode->vaddr)
1173 break;
1174
1175 if (!hnode) {
1176 mutex_unlock(&ctx->mem_hash_lock);
1177 dev_err(hdev->dev,
1178 "unmap failed, no mem hnode for vaddr 0x%llx\n",
1179 vaddr);
1180 return -EINVAL;
1181 }
1182
1183 hash_del(&hnode->node);
1184 mutex_unlock(&ctx->mem_hash_lock);
1185
1186 vm_type = hnode->ptr;
1187
1188 if (*vm_type == VM_TYPE_USERPTR) {
1189 is_userptr = true;
1190 userptr = hnode->ptr;
Omer Shpigelman54bb6742019-11-14 18:23:55 +00001191 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001192 &phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001193 if (rc) {
1194 dev_err(hdev->dev,
1195 "unable to init page pack for vaddr 0x%llx\n",
1196 vaddr);
1197 goto vm_type_err;
1198 }
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001199
1200 if (phys_pg_pack->page_size ==
1201 hdev->asic_prop.pmmu.page_size)
Ofir Bitton784b9162020-10-22 11:05:55 +03001202 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST];
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001203 else
Ofir Bitton784b9162020-10-22 11:05:55 +03001204 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE];
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001205 } else if (*vm_type == VM_TYPE_PHYS_PACK) {
1206 is_userptr = false;
Ofir Bitton784b9162020-10-22 11:05:55 +03001207 va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM];
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001208 phys_pg_pack = hnode->ptr;
1209 } else {
1210 dev_warn(hdev->dev,
1211 "unmap failed, unknown vm desc for vaddr 0x%llx\n",
1212 vaddr);
1213 rc = -EFAULT;
1214 goto vm_type_err;
1215 }
1216
1217 if (atomic_read(&phys_pg_pack->mapping_cnt) == 0) {
1218 dev_err(hdev->dev, "vaddr 0x%llx is not mapped\n", vaddr);
1219 rc = -EINVAL;
1220 goto mapping_cnt_err;
1221 }
1222
Moti Haimovskib19dc672020-11-18 20:15:29 +02001223 if (!is_userptr && !is_power_of_2(phys_pg_pack->page_size))
1224 vaddr = prop->dram_base_address +
1225 DIV_ROUND_DOWN_ULL(vaddr - prop->dram_base_address,
1226 phys_pg_pack->page_size) *
1227 phys_pg_pack->page_size;
1228 else
1229 vaddr &= ~(((u64) phys_pg_pack->page_size) - 1);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001230
1231 mutex_lock(&ctx->mmu_lock);
1232
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001233 unmap_phys_pg_pack(ctx, vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001234
Omer Shpigelmanbea84c42019-11-14 18:23:58 +00001235 /*
1236 * During context free this function is called in a loop to clean all
1237 * the context mappings. Hence the cache invalidation can be called once
1238 * at the loop end rather than for each iteration
1239 */
1240 if (!ctx_free)
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001241 rc = hdev->asic_funcs->mmu_invalidate_cache(hdev, true,
1242 *vm_type);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001243
1244 mutex_unlock(&ctx->mmu_lock);
1245
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001246 /*
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001247 * If the context is closing we don't need to check for the MMU cache
1248 * invalidation return code and update the VA free list as in this flow
1249 * we invalidate the MMU cache outside of this unmap function and the VA
1250 * free list will be freed anyway.
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001251 */
1252 if (!ctx_free) {
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001253 int tmp_rc;
1254
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001255 if (rc)
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001256 dev_err(hdev->dev,
1257 "unmapping vaddr 0x%llx failed due to MMU cache invalidation\n",
1258 vaddr);
1259
1260 tmp_rc = add_va_block(hdev, va_range, vaddr,
1261 vaddr + phys_pg_pack->total_size - 1);
1262 if (tmp_rc) {
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001263 dev_warn(hdev->dev,
1264 "add va block failed for vaddr: 0x%llx\n",
1265 vaddr);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001266 if (!rc)
1267 rc = tmp_rc;
1268 }
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001269 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001270
1271 atomic_dec(&phys_pg_pack->mapping_cnt);
1272 kfree(hnode);
1273
1274 if (is_userptr) {
1275 free_phys_pg_pack(hdev, phys_pg_pack);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001276 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001277 }
1278
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001279 return rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001280
1281mapping_cnt_err:
1282 if (is_userptr)
1283 free_phys_pg_pack(hdev, phys_pg_pack);
1284vm_type_err:
1285 mutex_lock(&ctx->mem_hash_lock);
1286 hash_add(ctx->mem_hash, &hnode->node, vaddr);
1287 mutex_unlock(&ctx->mem_hash_lock);
1288
1289 return rc;
1290}
1291
Oded Gabbay54303a12019-04-04 14:42:26 +03001292static int mem_ioctl_no_mmu(struct hl_fpriv *hpriv, union hl_mem_args *args)
1293{
1294 struct hl_device *hdev = hpriv->hdev;
1295 struct hl_ctx *ctx = hpriv->ctx;
1296 u64 device_addr = 0;
1297 u32 handle = 0;
1298 int rc;
1299
1300 switch (args->in.op) {
1301 case HL_MEM_OP_ALLOC:
1302 if (args->in.alloc.mem_size == 0) {
1303 dev_err(hdev->dev,
1304 "alloc size must be larger than 0\n");
1305 rc = -EINVAL;
1306 goto out;
1307 }
1308
1309 /* Force contiguous as there are no real MMU
1310 * translations to overcome physical memory gaps
1311 */
1312 args->in.flags |= HL_MEM_CONTIGUOUS;
1313 rc = alloc_device_memory(ctx, &args->in, &handle);
1314
1315 memset(args, 0, sizeof(*args));
1316 args->out.handle = (__u64) handle;
1317 break;
1318
1319 case HL_MEM_OP_FREE:
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001320 rc = free_device_memory(ctx, &args->in);
Oded Gabbay54303a12019-04-04 14:42:26 +03001321 break;
1322
1323 case HL_MEM_OP_MAP:
1324 if (args->in.flags & HL_MEM_USERPTR) {
1325 device_addr = args->in.map_host.host_virt_addr;
1326 rc = 0;
1327 } else {
1328 rc = get_paddr_from_handle(ctx, &args->in,
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001329 &device_addr);
Oded Gabbay54303a12019-04-04 14:42:26 +03001330 }
1331
1332 memset(args, 0, sizeof(*args));
1333 args->out.device_virt_addr = device_addr;
1334 break;
1335
1336 case HL_MEM_OP_UNMAP:
1337 rc = 0;
1338 break;
1339
1340 default:
1341 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1342 rc = -ENOTTY;
1343 break;
1344 }
1345
1346out:
1347 return rc;
1348}
1349
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001350int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
1351{
Ofir Bitton66a76402020-10-05 14:40:10 +03001352 enum hl_device_status status;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001353 union hl_mem_args *args = data;
1354 struct hl_device *hdev = hpriv->hdev;
1355 struct hl_ctx *ctx = hpriv->ctx;
1356 u64 device_addr = 0;
1357 u32 handle = 0;
1358 int rc;
1359
Ofir Bitton66a76402020-10-05 14:40:10 +03001360 if (!hl_device_operational(hdev, &status)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001361 dev_warn_ratelimited(hdev->dev,
Oded Gabbay3f5398c2019-04-06 15:41:35 +03001362 "Device is %s. Can't execute MEMORY IOCTL\n",
Ofir Bitton66a76402020-10-05 14:40:10 +03001363 hdev->status[status]);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001364 return -EBUSY;
1365 }
1366
Oded Gabbay54303a12019-04-04 14:42:26 +03001367 if (!hdev->mmu_enable)
1368 return mem_ioctl_no_mmu(hpriv, args);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001369
Oded Gabbay54303a12019-04-04 14:42:26 +03001370 switch (args->in.op) {
1371 case HL_MEM_OP_ALLOC:
Oded Gabbay54303a12019-04-04 14:42:26 +03001372 if (args->in.alloc.mem_size == 0) {
1373 dev_err(hdev->dev,
1374 "alloc size must be larger than 0\n");
1375 rc = -EINVAL;
1376 goto out;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001377 }
Oded Gabbay3e622992020-10-18 15:32:23 +03001378
1379 /* If DRAM does not support virtual memory the driver won't
1380 * handle the allocation/freeing of that memory. However, for
1381 * system administration/monitoring purposes, the driver will
1382 * keep track of the amount of DRAM memory that is allocated
1383 * and freed by the user. Because this code totally relies on
1384 * the user's input, the driver can't ensure the validity
1385 * of this accounting.
1386 */
Oded Gabbay7f070c92020-11-09 09:48:31 +02001387 if (!hdev->asic_prop.dram_supports_virtual_memory) {
Oded Gabbay3e622992020-10-18 15:32:23 +03001388 atomic64_add(args->in.alloc.mem_size,
1389 &ctx->dram_phys_mem);
1390 atomic64_add(args->in.alloc.mem_size,
1391 &hdev->dram_used_mem);
1392
1393 dev_dbg(hdev->dev, "DRAM alloc is not supported\n");
1394 rc = 0;
1395
1396 memset(args, 0, sizeof(*args));
1397 args->out.handle = 0;
1398 goto out;
1399 }
1400
Oded Gabbay54303a12019-04-04 14:42:26 +03001401 rc = alloc_device_memory(ctx, &args->in, &handle);
1402
1403 memset(args, 0, sizeof(*args));
1404 args->out.handle = (__u64) handle;
1405 break;
1406
1407 case HL_MEM_OP_FREE:
Oded Gabbay3e622992020-10-18 15:32:23 +03001408 /* If DRAM does not support virtual memory the driver won't
1409 * handle the allocation/freeing of that memory. However, for
1410 * system administration/monitoring purposes, the driver will
1411 * keep track of the amount of DRAM memory that is allocated
1412 * and freed by the user. Because this code totally relies on
1413 * the user's input, the driver can't ensure the validity
1414 * of this accounting.
1415 */
Oded Gabbay7f070c92020-11-09 09:48:31 +02001416 if (!hdev->asic_prop.dram_supports_virtual_memory) {
Oded Gabbay3e622992020-10-18 15:32:23 +03001417 atomic64_sub(args->in.alloc.mem_size,
1418 &ctx->dram_phys_mem);
1419 atomic64_sub(args->in.alloc.mem_size,
1420 &hdev->dram_used_mem);
1421
1422 dev_dbg(hdev->dev, "DRAM alloc is not supported\n");
1423 rc = 0;
1424
1425 goto out;
1426 }
1427
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001428 rc = free_device_memory(ctx, &args->in);
Oded Gabbay54303a12019-04-04 14:42:26 +03001429 break;
1430
1431 case HL_MEM_OP_MAP:
1432 rc = map_device_va(ctx, &args->in, &device_addr);
1433
1434 memset(args, 0, sizeof(*args));
1435 args->out.device_virt_addr = device_addr;
1436 break;
1437
1438 case HL_MEM_OP_UNMAP:
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001439 rc = unmap_device_va(ctx, &args->in, false);
Oded Gabbay54303a12019-04-04 14:42:26 +03001440 break;
1441
1442 default:
1443 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1444 rc = -ENOTTY;
1445 break;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001446 }
1447
1448out:
1449 return rc;
1450}
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001451
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001452static int get_user_memory(struct hl_device *hdev, u64 addr, u64 size,
1453 u32 npages, u64 start, u32 offset,
1454 struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001455{
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001456 int rc;
1457
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001458 if (!access_ok((void __user *) (uintptr_t) addr, size)) {
Oded Gabbay230afe72019-02-27 00:19:18 +02001459 dev_err(hdev->dev, "user pointer is invalid - 0x%llx\n", addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001460 return -EFAULT;
1461 }
1462
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001463 userptr->vec = frame_vector_create(npages);
1464 if (!userptr->vec) {
1465 dev_err(hdev->dev, "Failed to create frame vector\n");
1466 return -ENOMEM;
1467 }
1468
1469 rc = get_vaddr_frames(start, npages, FOLL_FORCE | FOLL_WRITE,
1470 userptr->vec);
1471
1472 if (rc != npages) {
1473 dev_err(hdev->dev,
1474 "Failed to map host memory, user ptr probably wrong\n");
1475 if (rc < 0)
1476 goto destroy_framevec;
1477 rc = -EFAULT;
1478 goto put_framevec;
1479 }
1480
1481 if (frame_vector_to_pages(userptr->vec) < 0) {
1482 dev_err(hdev->dev,
1483 "Failed to translate frame vector to pages\n");
1484 rc = -EFAULT;
1485 goto put_framevec;
1486 }
1487
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001488 rc = sg_alloc_table_from_pages(userptr->sgt,
1489 frame_vector_pages(userptr->vec),
1490 npages, offset, size, GFP_ATOMIC);
1491 if (rc < 0) {
1492 dev_err(hdev->dev, "failed to create SG table from pages\n");
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001493 goto put_framevec;
1494 }
1495
1496 return 0;
1497
1498put_framevec:
1499 put_vaddr_frames(userptr->vec);
1500destroy_framevec:
1501 frame_vector_destroy(userptr->vec);
1502 return rc;
1503}
1504
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001505/**
1506 * hl_pin_host_memory() - pins a chunk of host memory.
1507 * @hdev: pointer to the habanalabs device structure.
1508 * @addr: the host virtual address of the memory area.
1509 * @size: the size of the memory area.
1510 * @userptr: pointer to hl_userptr structure.
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001511 *
1512 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001513 * - Pins the physical pages.
1514 * - Create an SG list from those pages.
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001515 */
1516int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
1517 struct hl_userptr *userptr)
1518{
1519 u64 start, end;
1520 u32 npages, offset;
1521 int rc;
1522
1523 if (!size) {
1524 dev_err(hdev->dev, "size to pin is invalid - %llu\n", size);
1525 return -EINVAL;
1526 }
1527
1528 /*
1529 * If the combination of the address and size requested for this memory
1530 * region causes an integer overflow, return error.
1531 */
1532 if (((addr + size) < addr) ||
1533 PAGE_ALIGN(addr + size) < (addr + size)) {
1534 dev_err(hdev->dev,
1535 "user pointer 0x%llx + %llu causes integer overflow\n",
1536 addr, size);
1537 return -EINVAL;
1538 }
1539
1540 /*
1541 * This function can be called also from data path, hence use atomic
1542 * always as it is not a big allocation.
1543 */
1544 userptr->sgt = kzalloc(sizeof(*userptr->sgt), GFP_ATOMIC);
1545 if (!userptr->sgt)
1546 return -ENOMEM;
1547
1548 start = addr & PAGE_MASK;
1549 offset = addr & ~PAGE_MASK;
1550 end = PAGE_ALIGN(addr + size);
1551 npages = (end - start) >> PAGE_SHIFT;
1552
1553 userptr->size = size;
1554 userptr->addr = addr;
1555 userptr->dma_mapped = false;
1556 INIT_LIST_HEAD(&userptr->job_node);
1557
1558 rc = get_user_memory(hdev, addr, size, npages, start, offset,
1559 userptr);
1560 if (rc) {
1561 dev_err(hdev->dev,
1562 "failed to get user memory for address 0x%llx\n",
1563 addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001564 goto free_sgt;
1565 }
1566
Oded Gabbayc2164772019-02-16 00:39:24 +02001567 hl_debugfs_add_userptr(hdev, userptr);
1568
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001569 return 0;
1570
1571free_sgt:
1572 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001573 return rc;
1574}
1575
1576/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001577 * hl_unpin_host_memory - unpins a chunk of host memory.
1578 * @hdev: pointer to the habanalabs device structure
1579 * @userptr: pointer to hl_userptr structure
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001580 *
1581 * This function does the following:
1582 * - Unpins the physical pages related to the host memory
1583 * - Free the SG list
1584 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001585void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001586{
1587 struct page **pages;
1588
Oded Gabbayc2164772019-02-16 00:39:24 +02001589 hl_debugfs_remove_userptr(hdev, userptr);
1590
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001591 if (userptr->dma_mapped)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001592 hdev->asic_funcs->hl_dma_unmap_sg(hdev, userptr->sgt->sgl,
1593 userptr->sgt->nents,
1594 userptr->dir);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001595
1596 pages = frame_vector_pages(userptr->vec);
1597 if (!IS_ERR(pages)) {
1598 int i;
1599
1600 for (i = 0; i < frame_vector_count(userptr->vec); i++)
1601 set_page_dirty_lock(pages[i]);
1602 }
1603 put_vaddr_frames(userptr->vec);
1604 frame_vector_destroy(userptr->vec);
1605
1606 list_del(&userptr->job_node);
1607
1608 sg_free_table(userptr->sgt);
1609 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001610}
1611
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001612/**
1613 * hl_userptr_delete_list() - clear userptr list.
1614 * @hdev: pointer to the habanalabs device structure.
1615 * @userptr_list: pointer to the list to clear.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001616 *
1617 * This function does the following:
1618 * - Iterates over the list and unpins the host memory and frees the userptr
1619 * structure.
1620 */
1621void hl_userptr_delete_list(struct hl_device *hdev,
1622 struct list_head *userptr_list)
1623{
1624 struct hl_userptr *userptr, *tmp;
1625
1626 list_for_each_entry_safe(userptr, tmp, userptr_list, job_node) {
1627 hl_unpin_host_memory(hdev, userptr);
1628 kfree(userptr);
1629 }
1630
1631 INIT_LIST_HEAD(userptr_list);
1632}
1633
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001634/**
1635 * hl_userptr_is_pinned() - returns whether the given userptr is pinned.
1636 * @hdev: pointer to the habanalabs device structure.
1637 * @userptr_list: pointer to the list to clear.
1638 * @userptr: pointer to userptr to check.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001639 *
1640 * This function does the following:
1641 * - Iterates over the list and checks if the given userptr is in it, means is
1642 * pinned. If so, returns true, otherwise returns false.
1643 */
1644bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr,
1645 u32 size, struct list_head *userptr_list,
1646 struct hl_userptr **userptr)
1647{
1648 list_for_each_entry((*userptr), userptr_list, job_node) {
1649 if ((addr == (*userptr)->addr) && (size == (*userptr)->size))
1650 return true;
1651 }
1652
1653 return false;
1654}
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001655
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001656/**
1657 * va_range_init() - initialize virtual addresses range.
1658 * @hdev: pointer to the habanalabs device structure.
1659 * @va_range: pointer to the range to initialize.
1660 * @start: range start address.
1661 * @end: range end address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001662 *
1663 * This function does the following:
1664 * - Initializes the virtual addresses list of the given range with the given
1665 * addresses.
1666 */
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001667static int va_range_init(struct hl_device *hdev, struct hl_va_range *va_range,
Ofir Bitton784b9162020-10-22 11:05:55 +03001668 u64 start, u64 end, u32 page_size)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001669{
1670 int rc;
1671
1672 INIT_LIST_HEAD(&va_range->list);
1673
Moti Haimovskib19dc672020-11-18 20:15:29 +02001674 /*
1675 * PAGE_SIZE alignment
1676 * it is the callers responsibility to align the addresses if the
1677 * page size is not a power of 2
1678 */
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001679
Moti Haimovskib19dc672020-11-18 20:15:29 +02001680 if (is_power_of_2(page_size)) {
1681 if (start & (PAGE_SIZE - 1)) {
1682 start &= PAGE_MASK;
1683 start += PAGE_SIZE;
1684 }
1685
1686 if (end & (PAGE_SIZE - 1))
1687 end &= PAGE_MASK;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001688 }
1689
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001690 if (start >= end) {
1691 dev_err(hdev->dev, "too small vm range for va list\n");
1692 return -EFAULT;
1693 }
1694
1695 rc = add_va_block(hdev, va_range, start, end);
1696
1697 if (rc) {
1698 dev_err(hdev->dev, "Failed to init host va list\n");
1699 return rc;
1700 }
1701
1702 va_range->start_addr = start;
1703 va_range->end_addr = end;
Ofir Bitton784b9162020-10-22 11:05:55 +03001704 va_range->page_size = page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001705
1706 return 0;
1707}
1708
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001709/**
1710 * va_range_fini() - clear a virtual addresses range.
1711 * @hdev: pointer to the habanalabs structure.
1712 * va_range: pointer to virtual addresses rang.e
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001713 *
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001714 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001715 * - Frees the virtual addresses block list and its lock.
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001716 */
Ofir Bitton784b9162020-10-22 11:05:55 +03001717static void va_range_fini(struct hl_device *hdev, struct hl_va_range *va_range)
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001718{
1719 mutex_lock(&va_range->lock);
1720 clear_va_list_locked(hdev, &va_range->list);
1721 mutex_unlock(&va_range->lock);
1722
1723 mutex_destroy(&va_range->lock);
1724 kfree(va_range);
1725}
1726
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001727/**
1728 * vm_ctx_init_with_ranges() - initialize virtual memory for context.
1729 * @ctx: pointer to the habanalabs context structure.
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001730 * @host_range_start: host virtual addresses range start.
1731 * @host_range_end: host virtual addresses range end.
1732 * @host_huge_range_start: host virtual addresses range start for memory
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001733 * allocated with huge pages.
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001734 * @host_huge_range_end: host virtual addresses range end for memory allocated
1735 * with huge pages.
1736 * @dram_range_start: dram virtual addresses range start.
1737 * @dram_range_end: dram virtual addresses range end.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001738 *
1739 * This function initializes the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001740 * - MMU for context.
1741 * - Virtual address to area descriptor hashtable.
1742 * - Virtual block list of available virtual memory.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001743 */
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001744static int vm_ctx_init_with_ranges(struct hl_ctx *ctx,
1745 u64 host_range_start,
1746 u64 host_range_end,
Ofir Bitton784b9162020-10-22 11:05:55 +03001747 u32 host_page_size,
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001748 u64 host_huge_range_start,
1749 u64 host_huge_range_end,
Ofir Bitton784b9162020-10-22 11:05:55 +03001750 u32 host_huge_page_size,
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001751 u64 dram_range_start,
Ofir Bitton784b9162020-10-22 11:05:55 +03001752 u64 dram_range_end,
1753 u32 dram_page_size)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001754{
1755 struct hl_device *hdev = ctx->hdev;
Ofir Bitton784b9162020-10-22 11:05:55 +03001756 int i, rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001757
Ofir Bitton784b9162020-10-22 11:05:55 +03001758 for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++) {
1759 ctx->va_range[i] =
1760 kzalloc(sizeof(struct hl_va_range), GFP_KERNEL);
1761 if (!ctx->va_range[i]) {
1762 rc = -ENOMEM;
1763 goto free_va_range;
1764 }
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001765 }
1766
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001767 rc = hl_mmu_ctx_init(ctx);
1768 if (rc) {
1769 dev_err(hdev->dev, "failed to init context %d\n", ctx->asid);
Ofir Bitton784b9162020-10-22 11:05:55 +03001770 goto free_va_range;
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001771 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001772
1773 mutex_init(&ctx->mem_hash_lock);
1774 hash_init(ctx->mem_hash);
1775
Ofir Bitton784b9162020-10-22 11:05:55 +03001776 mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001777
Ofir Bitton784b9162020-10-22 11:05:55 +03001778 rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST],
1779 host_range_start, host_range_end, host_page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001780 if (rc) {
1781 dev_err(hdev->dev, "failed to init host vm range\n");
Ofir Bitton784b9162020-10-22 11:05:55 +03001782 goto mmu_ctx_fini;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001783 }
1784
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001785 if (hdev->pmmu_huge_range) {
Ofir Bitton784b9162020-10-22 11:05:55 +03001786 mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001787
Ofir Bitton784b9162020-10-22 11:05:55 +03001788 rc = va_range_init(hdev,
1789 ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE],
1790 host_huge_range_start, host_huge_range_end,
1791 host_huge_page_size);
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001792 if (rc) {
1793 dev_err(hdev->dev,
1794 "failed to init host huge vm range\n");
Ofir Bitton784b9162020-10-22 11:05:55 +03001795 goto clear_host_va_range;
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001796 }
1797 } else {
Ofir Bitton8e718f22020-11-26 13:01:11 +02001798 kfree(ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]);
Ofir Bitton784b9162020-10-22 11:05:55 +03001799 ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] =
1800 ctx->va_range[HL_VA_RANGE_TYPE_HOST];
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001801 }
1802
Ofir Bitton784b9162020-10-22 11:05:55 +03001803 mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock);
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001804
Ofir Bitton784b9162020-10-22 11:05:55 +03001805 rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM],
1806 dram_range_start, dram_range_end, dram_page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001807 if (rc) {
1808 dev_err(hdev->dev, "failed to init dram vm range\n");
Ofir Bitton784b9162020-10-22 11:05:55 +03001809 goto clear_host_huge_va_range;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001810 }
1811
Oded Gabbayc2164772019-02-16 00:39:24 +02001812 hl_debugfs_add_ctx_mem_hash(hdev, ctx);
1813
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001814 return 0;
1815
Ofir Bitton784b9162020-10-22 11:05:55 +03001816clear_host_huge_va_range:
1817 mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001818
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001819 if (hdev->pmmu_huge_range) {
Ofir Bitton784b9162020-10-22 11:05:55 +03001820 mutex_lock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
1821 clear_va_list_locked(hdev,
1822 &ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->list);
1823 mutex_unlock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001824 }
Ofir Bitton784b9162020-10-22 11:05:55 +03001825clear_host_va_range:
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001826 if (hdev->pmmu_huge_range)
Ofir Bitton784b9162020-10-22 11:05:55 +03001827 mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
1828 mutex_lock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
1829 clear_va_list_locked(hdev, &ctx->va_range[HL_VA_RANGE_TYPE_HOST]->list);
1830 mutex_unlock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
1831mmu_ctx_fini:
1832 mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001833 mutex_destroy(&ctx->mem_hash_lock);
1834 hl_mmu_ctx_fini(ctx);
Ofir Bitton784b9162020-10-22 11:05:55 +03001835free_va_range:
1836 for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++)
1837 kfree(ctx->va_range[i]);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001838
1839 return rc;
1840}
1841
1842int hl_vm_ctx_init(struct hl_ctx *ctx)
1843{
1844 struct asic_fixed_properties *prop = &ctx->hdev->asic_prop;
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001845 u64 host_range_start, host_range_end, host_huge_range_start,
1846 host_huge_range_end, dram_range_start, dram_range_end;
Ofir Bitton784b9162020-10-22 11:05:55 +03001847 u32 host_page_size, host_huge_page_size, dram_page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001848
1849 atomic64_set(&ctx->dram_phys_mem, 0);
1850
1851 /*
1852 * - If MMU is enabled, init the ranges as usual.
1853 * - If MMU is disabled, in case of host mapping, the returned address
1854 * is the given one.
1855 * In case of DRAM mapping, the returned address is the physical
1856 * address of the memory related to the given handle.
1857 */
Oded Gabbayf3a965c2020-10-04 23:00:39 +03001858 if (!ctx->hdev->mmu_enable)
1859 return 0;
1860
1861 dram_range_start = prop->dmmu.start_addr;
1862 dram_range_end = prop->dmmu.end_addr;
Moti Haimovskib19dc672020-11-18 20:15:29 +02001863 dram_page_size = prop->dram_page_size ?
1864 prop->dram_page_size : prop->dmmu.page_size;
Oded Gabbayf3a965c2020-10-04 23:00:39 +03001865 host_range_start = prop->pmmu.start_addr;
1866 host_range_end = prop->pmmu.end_addr;
Ofir Bitton784b9162020-10-22 11:05:55 +03001867 host_page_size = prop->pmmu.page_size;
Oded Gabbayf3a965c2020-10-04 23:00:39 +03001868 host_huge_range_start = prop->pmmu_huge.start_addr;
1869 host_huge_range_end = prop->pmmu_huge.end_addr;
Ofir Bitton784b9162020-10-22 11:05:55 +03001870 host_huge_page_size = prop->pmmu_huge.page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001871
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001872 return vm_ctx_init_with_ranges(ctx, host_range_start, host_range_end,
Ofir Bitton784b9162020-10-22 11:05:55 +03001873 host_page_size, host_huge_range_start,
1874 host_huge_range_end, host_huge_page_size,
1875 dram_range_start, dram_range_end, dram_page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001876}
1877
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001878/**
1879 * hl_vm_ctx_fini() - virtual memory teardown of context.
1880 * @ctx: pointer to the habanalabs context structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001881 *
1882 * This function perform teardown the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001883 * - Virtual block list of available virtual memory.
1884 * - Virtual address to area descriptor hashtable.
1885 * - MMU for context.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001886 *
1887 * In addition this function does the following:
1888 * - Unmaps the existing hashtable nodes if the hashtable is not empty. The
1889 * hashtable should be empty as no valid mappings should exist at this
1890 * point.
1891 * - Frees any existing physical page list from the idr which relates to the
1892 * current context asid.
1893 * - This function checks the virtual block list for correctness. At this point
1894 * the list should contain one element which describes the whole virtual
1895 * memory range of the context. Otherwise, a warning is printed.
1896 */
1897void hl_vm_ctx_fini(struct hl_ctx *ctx)
1898{
1899 struct hl_device *hdev = ctx->hdev;
1900 struct hl_vm *vm = &hdev->vm;
1901 struct hl_vm_phys_pg_pack *phys_pg_list;
1902 struct hl_vm_hash_node *hnode;
1903 struct hlist_node *tmp_node;
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001904 struct hl_mem_in args;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001905 int i;
1906
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001907 if (!hdev->mmu_enable)
Oded Gabbayf3a965c2020-10-04 23:00:39 +03001908 return;
1909
Oded Gabbayc2164772019-02-16 00:39:24 +02001910 hl_debugfs_remove_ctx_mem_hash(hdev, ctx);
1911
Omer Shpigelmane604f552019-11-14 18:23:59 +00001912 /*
1913 * Clearly something went wrong on hard reset so no point in printing
1914 * another side effect error
1915 */
1916 if (!hdev->hard_reset_pending && !hash_empty(ctx->mem_hash))
1917 dev_notice(hdev->dev,
Oded Gabbay0eab4f82020-06-22 09:52:22 +03001918 "user released device without removing its memory mappings\n");
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001919
1920 hash_for_each_safe(ctx->mem_hash, i, tmp_node, hnode, node) {
1921 dev_dbg(hdev->dev,
1922 "hl_mem_hash_node of vaddr 0x%llx of asid %d is still alive\n",
1923 hnode->vaddr, ctx->asid);
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001924 args.unmap.device_virt_addr = hnode->vaddr;
1925 unmap_device_va(ctx, &args, true);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001926 }
1927
Ohad Sharabicb6ef0e2020-11-26 09:39:26 +02001928 mutex_lock(&ctx->mmu_lock);
1929
Omer Shpigelmanbea84c42019-11-14 18:23:58 +00001930 /* invalidate the cache once after the unmapping loop */
1931 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
1932 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_PHYS_PACK);
1933
Ohad Sharabicb6ef0e2020-11-26 09:39:26 +02001934 mutex_unlock(&ctx->mmu_lock);
1935
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001936 spin_lock(&vm->idr_lock);
1937 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_list, i)
1938 if (phys_pg_list->asid == ctx->asid) {
1939 dev_dbg(hdev->dev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001940 "page list 0x%px of asid %d is still alive\n",
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001941 phys_pg_list, ctx->asid);
Tomer Tayarc8113752019-08-04 07:03:41 +00001942 atomic64_sub(phys_pg_list->total_size,
1943 &hdev->dram_used_mem);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001944 free_phys_pg_pack(hdev, phys_pg_list);
1945 idr_remove(&vm->phys_pg_pack_handles, i);
1946 }
1947 spin_unlock(&vm->idr_lock);
1948
Ofir Bitton784b9162020-10-22 11:05:55 +03001949 va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM]);
Ofir Bitton8e718f22020-11-26 13:01:11 +02001950 va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST]);
1951
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001952 if (hdev->pmmu_huge_range)
Ofir Bitton784b9162020-10-22 11:05:55 +03001953 va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001954
1955 mutex_destroy(&ctx->mem_hash_lock);
1956 hl_mmu_ctx_fini(ctx);
Oded Gabbay3e622992020-10-18 15:32:23 +03001957
1958 /* In this case we need to clear the global accounting of DRAM usage
1959 * because the user notifies us on allocations. If the user is no more,
1960 * all DRAM is available
1961 */
Ofir Bitton8e39e752020-11-12 11:03:32 +02001962 if (ctx->asid != HL_KERNEL_ASID_ID &&
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001963 !hdev->asic_prop.dram_supports_virtual_memory)
1964 atomic64_set(&hdev->dram_used_mem, 0);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001965}
1966
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001967/**
1968 * hl_vm_init() - initialize virtual memory module.
1969 * @hdev: pointer to the habanalabs device structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001970 *
1971 * This function initializes the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001972 * - MMU module.
1973 * - DRAM physical pages pool of 2MB.
1974 * - Idr for device memory allocation handles.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001975 */
1976int hl_vm_init(struct hl_device *hdev)
1977{
1978 struct asic_fixed_properties *prop = &hdev->asic_prop;
1979 struct hl_vm *vm = &hdev->vm;
1980 int rc;
1981
Moti Haimovskib19dc672020-11-18 20:15:29 +02001982 if (is_power_of_2(prop->dram_page_size))
1983 vm->dram_pg_pool =
1984 gen_pool_create(__ffs(prop->dram_page_size), -1);
1985 else
1986 vm->dram_pg_pool =
1987 gen_pool_create(__ffs(DRAM_POOL_PAGE_SIZE), -1);
1988
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001989 if (!vm->dram_pg_pool) {
1990 dev_err(hdev->dev, "Failed to create dram page pool\n");
Oded Gabbay37d68ce2019-05-29 14:43:04 +03001991 return -ENOMEM;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001992 }
1993
1994 kref_init(&vm->dram_pg_pool_refcount);
1995
1996 rc = gen_pool_add(vm->dram_pg_pool, prop->dram_user_base_address,
1997 prop->dram_end_address - prop->dram_user_base_address,
1998 -1);
1999
2000 if (rc) {
2001 dev_err(hdev->dev,
2002 "Failed to add memory to dram page pool %d\n", rc);
2003 goto pool_add_err;
2004 }
2005
2006 spin_lock_init(&vm->idr_lock);
2007 idr_init(&vm->phys_pg_pack_handles);
2008
2009 atomic64_set(&hdev->dram_used_mem, 0);
2010
2011 vm->init_done = true;
2012
2013 return 0;
2014
2015pool_add_err:
2016 gen_pool_destroy(vm->dram_pg_pool);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002017
2018 return rc;
2019}
2020
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002021/**
2022 * hl_vm_fini() - virtual memory module teardown.
2023 * @hdev: pointer to the habanalabs device structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002024 *
2025 * This function perform teardown to the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002026 * - Idr for device memory allocation handles.
2027 * - DRAM physical pages pool of 2MB.
2028 * - MMU module.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002029 */
2030void hl_vm_fini(struct hl_device *hdev)
2031{
2032 struct hl_vm *vm = &hdev->vm;
2033
2034 if (!vm->init_done)
2035 return;
2036
2037 /*
2038 * At this point all the contexts should be freed and hence no DRAM
2039 * memory should be in use. Hence the DRAM pool should be freed here.
2040 */
2041 if (kref_put(&vm->dram_pg_pool_refcount, dram_pg_pool_do_release) != 1)
2042 dev_warn(hdev->dev, "dram_pg_pool was not destroyed on %s\n",
2043 __func__);
2044
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002045 vm->init_done = false;
2046}