blob: d54fdd04be5b4de7abb0aa68291c9c4a751b68ea [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 }
84 }
85
86 phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL);
87 if (!phys_pg_pack) {
88 rc = -ENOMEM;
89 goto pages_pack_err;
90 }
91
92 phys_pg_pack->vm_type = VM_TYPE_PHYS_PACK;
93 phys_pg_pack->asid = ctx->asid;
94 phys_pg_pack->npages = num_pgs;
95 phys_pg_pack->page_size = page_size;
96 phys_pg_pack->total_size = total_size;
97 phys_pg_pack->flags = args->flags;
98 phys_pg_pack->contiguous = contiguous;
99
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200100 phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL);
Ofir Bitton08391522020-08-11 08:57:45 +0300101 if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200102 rc = -ENOMEM;
103 goto pages_arr_err;
104 }
105
106 if (phys_pg_pack->contiguous) {
107 for (i = 0 ; i < num_pgs ; i++)
108 phys_pg_pack->pages[i] = paddr + i * page_size;
109 } else {
110 for (i = 0 ; i < num_pgs ; i++) {
111 phys_pg_pack->pages[i] = (u64) gen_pool_alloc(
112 vm->dram_pg_pool,
113 page_size);
114 if (!phys_pg_pack->pages[i]) {
115 dev_err(hdev->dev,
Oded Gabbaycab8e3e2019-03-27 09:44:28 +0200116 "Failed to allocate device memory (out of memory)\n");
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200117 rc = -ENOMEM;
118 goto page_err;
119 }
120
121 num_curr_pgs++;
122 }
123 }
124
125 spin_lock(&vm->idr_lock);
126 handle = idr_alloc(&vm->phys_pg_pack_handles, phys_pg_pack, 1, 0,
Ofir Bittond5eb8372021-02-14 15:35:56 +0200127 GFP_KERNEL);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200128 spin_unlock(&vm->idr_lock);
129
130 if (handle < 0) {
131 dev_err(hdev->dev, "Failed to get handle for page\n");
132 rc = -EFAULT;
133 goto idr_err;
134 }
135
136 for (i = 0 ; i < num_pgs ; i++)
137 kref_get(&vm->dram_pg_pool_refcount);
138
139 phys_pg_pack->handle = handle;
140
141 atomic64_add(phys_pg_pack->total_size, &ctx->dram_phys_mem);
142 atomic64_add(phys_pg_pack->total_size, &hdev->dram_used_mem);
143
144 *ret_handle = handle;
145
146 return 0;
147
148idr_err:
149page_err:
150 if (!phys_pg_pack->contiguous)
151 for (i = 0 ; i < num_curr_pgs ; i++)
152 gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[i],
153 page_size);
154
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200155 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200156pages_arr_err:
157 kfree(phys_pg_pack);
158pages_pack_err:
159 if (contiguous)
160 gen_pool_free(vm->dram_pg_pool, paddr, total_size);
161
162 return rc;
163}
164
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200165/**
166 * dma_map_host_va() - DMA mapping of the given host virtual address.
167 * @hdev: habanalabs device structure.
168 * @addr: the host virtual address of the memory area.
169 * @size: the size of the memory area.
170 * @p_userptr: pointer to result userptr structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200171 *
172 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200173 * - Allocate userptr structure.
174 * - Pin the given host memory using the userptr structure.
175 * - Perform DMA mapping to have the DMA addresses of the pages.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200176 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300177static int dma_map_host_va(struct hl_device *hdev, u64 addr, u64 size,
178 struct hl_userptr **p_userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200179{
180 struct hl_userptr *userptr;
181 int rc;
182
183 userptr = kzalloc(sizeof(*userptr), GFP_KERNEL);
184 if (!userptr) {
185 rc = -ENOMEM;
186 goto userptr_err;
187 }
188
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300189 rc = hl_pin_host_memory(hdev, addr, size, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200190 if (rc) {
191 dev_err(hdev->dev, "Failed to pin host memory\n");
192 goto pin_err;
193 }
194
195 rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl,
196 userptr->sgt->nents, DMA_BIDIRECTIONAL);
197 if (rc) {
198 dev_err(hdev->dev, "failed to map sgt with DMA region\n");
199 goto dma_map_err;
200 }
201
202 userptr->dma_mapped = true;
203 userptr->dir = DMA_BIDIRECTIONAL;
204 userptr->vm_type = VM_TYPE_USERPTR;
205
206 *p_userptr = userptr;
207
208 return 0;
209
210dma_map_err:
211 hl_unpin_host_memory(hdev, userptr);
212pin_err:
213 kfree(userptr);
214userptr_err:
215
216 return rc;
217}
218
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200219/**
220 * dma_unmap_host_va() - DMA unmapping of the given host virtual address.
221 * @hdev: habanalabs device structure.
222 * @userptr: userptr to free.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200223 *
224 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200225 * - Unpins the physical pages.
226 * - Frees the userptr structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200227 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300228static void dma_unmap_host_va(struct hl_device *hdev,
229 struct hl_userptr *userptr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200230{
231 hl_unpin_host_memory(hdev, userptr);
232 kfree(userptr);
233}
234
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200235/**
236 * dram_pg_pool_do_release() - free DRAM pages pool
237 * @ref: pointer to reference object.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200238 *
239 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200240 * - Frees the idr structure of physical pages handles.
241 * - Frees the generic pool of DRAM physical pages.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200242 */
243static void dram_pg_pool_do_release(struct kref *ref)
244{
245 struct hl_vm *vm = container_of(ref, struct hl_vm,
246 dram_pg_pool_refcount);
247
248 /*
249 * free the idr here as only here we know for sure that there are no
250 * allocated physical pages and hence there are no handles in use
251 */
252 idr_destroy(&vm->phys_pg_pack_handles);
253 gen_pool_destroy(vm->dram_pg_pool);
254}
255
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200256/**
257 * free_phys_pg_pack() - free physical page pack.
258 * @hdev: habanalabs device structure.
259 * @phys_pg_pack: physical page pack to free.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200260 *
261 * This function does the following:
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200262 * - For DRAM memory only
263 * - iterate over the pack, scrub and free each physical block structure by
264 * returning it to the general pool.
265 * In case of error during scrubbing, initiate hard reset.
266 * Once hard reset is triggered, scrubbing is bypassed while freeing the
267 * memory continues.
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200268 * - Free the hl_vm_phys_pg_pack structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200269 */
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200270static int free_phys_pg_pack(struct hl_device *hdev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300271 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200272{
273 struct hl_vm *vm = &hdev->vm;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200274 u64 i;
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200275 int rc = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200276
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200277 if (phys_pg_pack->created_from_userptr)
278 goto end;
279
280 if (phys_pg_pack->contiguous) {
281 if (hdev->memory_scrub && !hdev->disabled) {
282 rc = hdev->asic_funcs->scrub_device_mem(hdev,
283 phys_pg_pack->pages[0],
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200284 phys_pg_pack->total_size);
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200285 if (rc)
286 dev_err(hdev->dev,
287 "Failed to scrub contiguous device memory\n");
288 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200289
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200290 gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[0],
291 phys_pg_pack->total_size);
292
293 for (i = 0; i < phys_pg_pack->npages ; i++)
294 kref_put(&vm->dram_pg_pool_refcount,
295 dram_pg_pool_do_release);
296 } else {
297 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
298 if (hdev->memory_scrub && !hdev->disabled && rc == 0) {
299 rc = hdev->asic_funcs->scrub_device_mem(
300 hdev,
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200301 phys_pg_pack->pages[i],
302 phys_pg_pack->page_size);
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200303 if (rc)
304 dev_err(hdev->dev,
305 "Failed to scrub device memory\n");
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200306 }
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200307 gen_pool_free(vm->dram_pg_pool,
308 phys_pg_pack->pages[i],
309 phys_pg_pack->page_size);
310 kref_put(&vm->dram_pg_pool_refcount,
311 dram_pg_pool_do_release);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200312 }
313 }
314
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200315 if (rc && !hdev->disabled)
316 hl_device_reset(hdev, HL_RESET_HARD);
317
318end:
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200319 kvfree(phys_pg_pack->pages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200320 kfree(phys_pg_pack);
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200321
322 return rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200323}
324
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200325/**
326 * free_device_memory() - free device memory.
327 * @ctx: pointer to the context structure.
Omer Shpigelmanf19040c2020-12-09 13:34:11 +0200328 * @args: host parameters containing the requested size.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200329 *
330 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200331 * - Free the device memory related to the given handle.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200332 */
Omer Shpigelmanf19040c2020-12-09 13:34:11 +0200333static int free_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200334{
335 struct hl_device *hdev = ctx->hdev;
336 struct hl_vm *vm = &hdev->vm;
337 struct hl_vm_phys_pg_pack *phys_pg_pack;
Omer Shpigelmanf19040c2020-12-09 13:34:11 +0200338 u32 handle = args->free.handle;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200339
340 spin_lock(&vm->idr_lock);
341 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
342 if (phys_pg_pack) {
343 if (atomic_read(&phys_pg_pack->mapping_cnt) > 0) {
344 dev_err(hdev->dev, "handle %u is mapped, cannot free\n",
345 handle);
346 spin_unlock(&vm->idr_lock);
347 return -EINVAL;
348 }
349
350 /*
351 * must remove from idr before the freeing of the physical
352 * pages as the refcount of the pool is also the trigger of the
353 * idr destroy
354 */
355 idr_remove(&vm->phys_pg_pack_handles, handle);
356 spin_unlock(&vm->idr_lock);
357
358 atomic64_sub(phys_pg_pack->total_size, &ctx->dram_phys_mem);
359 atomic64_sub(phys_pg_pack->total_size, &hdev->dram_used_mem);
360
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +0200361 return free_phys_pg_pack(hdev, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200362 } else {
363 spin_unlock(&vm->idr_lock);
364 dev_err(hdev->dev,
365 "free device memory failed, no match for handle %u\n",
366 handle);
367 return -EINVAL;
368 }
369
370 return 0;
371}
372
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200373/**
374 * clear_va_list_locked() - free virtual addresses list.
375 * @hdev: habanalabs device structure.
376 * @va_list: list of virtual addresses to free.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200377 *
378 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200379 * - Iterate over the list and free each virtual addresses block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200380 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200381 * This function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200382 */
383static void clear_va_list_locked(struct hl_device *hdev,
384 struct list_head *va_list)
385{
386 struct hl_vm_va_block *va_block, *tmp;
387
388 list_for_each_entry_safe(va_block, tmp, va_list, node) {
389 list_del(&va_block->node);
390 kfree(va_block);
391 }
392}
393
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200394/**
395 * print_va_list_locked() - print virtual addresses list.
396 * @hdev: habanalabs device structure.
397 * @va_list: list of virtual addresses to print.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200398 *
399 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200400 * - Iterate over the list and print each virtual addresses block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200401 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200402 * This function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200403 */
404static void print_va_list_locked(struct hl_device *hdev,
405 struct list_head *va_list)
406{
407#if HL_MMU_DEBUG
408 struct hl_vm_va_block *va_block;
409
410 dev_dbg(hdev->dev, "print va list:\n");
411
412 list_for_each_entry(va_block, va_list, node)
413 dev_dbg(hdev->dev,
414 "va block, start: 0x%llx, end: 0x%llx, size: %llu\n",
415 va_block->start, va_block->end, va_block->size);
416#endif
417}
418
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200419/**
420 * merge_va_blocks_locked() - merge a virtual block if possible.
421 * @hdev: pointer to the habanalabs device structure.
422 * @va_list: pointer to the virtual addresses block list.
423 * @va_block: virtual block to merge with adjacent blocks.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200424 *
425 * This function does the following:
426 * - Merge the given blocks with the adjacent blocks if their virtual ranges
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200427 * create a contiguous virtual range.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200428 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200429 * This Function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200430 */
431static void merge_va_blocks_locked(struct hl_device *hdev,
432 struct list_head *va_list, struct hl_vm_va_block *va_block)
433{
434 struct hl_vm_va_block *prev, *next;
435
436 prev = list_prev_entry(va_block, node);
437 if (&prev->node != va_list && prev->end + 1 == va_block->start) {
438 prev->end = va_block->end;
439 prev->size = prev->end - prev->start;
440 list_del(&va_block->node);
441 kfree(va_block);
442 va_block = prev;
443 }
444
445 next = list_next_entry(va_block, node);
446 if (&next->node != va_list && va_block->end + 1 == next->start) {
447 next->start = va_block->start;
448 next->size = next->end - next->start;
449 list_del(&va_block->node);
450 kfree(va_block);
451 }
452}
453
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200454/**
455 * add_va_block_locked() - add a virtual block to the virtual addresses list.
456 * @hdev: pointer to the habanalabs device structure.
457 * @va_list: pointer to the virtual addresses block list.
458 * @start: start virtual address.
459 * @end: end virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200460 *
461 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200462 * - Add the given block to the virtual blocks list and merge with other blocks
463 * if a contiguous virtual block can be created.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200464 *
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200465 * This Function should be called only when va_list lock is taken.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200466 */
467static int add_va_block_locked(struct hl_device *hdev,
468 struct list_head *va_list, u64 start, u64 end)
469{
470 struct hl_vm_va_block *va_block, *res = NULL;
471 u64 size = end - start;
472
473 print_va_list_locked(hdev, va_list);
474
475 list_for_each_entry(va_block, va_list, node) {
476 /* TODO: remove upon matureness */
477 if (hl_mem_area_crosses_range(start, size, va_block->start,
478 va_block->end)) {
479 dev_err(hdev->dev,
480 "block crossing ranges at start 0x%llx, end 0x%llx\n",
481 va_block->start, va_block->end);
482 return -EINVAL;
483 }
484
485 if (va_block->end < start)
486 res = va_block;
487 }
488
489 va_block = kmalloc(sizeof(*va_block), GFP_KERNEL);
490 if (!va_block)
491 return -ENOMEM;
492
493 va_block->start = start;
494 va_block->end = end;
495 va_block->size = size;
496
497 if (!res)
498 list_add(&va_block->node, va_list);
499 else
500 list_add(&va_block->node, &res->node);
501
502 merge_va_blocks_locked(hdev, va_list, va_block);
503
504 print_va_list_locked(hdev, va_list);
505
506 return 0;
507}
508
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200509/**
510 * add_va_block() - wrapper for add_va_block_locked.
511 * @hdev: pointer to the habanalabs device structure.
512 * @va_list: pointer to the virtual addresses block list.
513 * @start: start virtual address.
514 * @end: end virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200515 *
516 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200517 * - Takes the list lock and calls add_va_block_locked.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200518 */
519static inline int add_va_block(struct hl_device *hdev,
520 struct hl_va_range *va_range, u64 start, u64 end)
521{
522 int rc;
523
524 mutex_lock(&va_range->lock);
525 rc = add_va_block_locked(hdev, &va_range->list, start, end);
526 mutex_unlock(&va_range->lock);
527
528 return rc;
529}
530
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200531/**
farah kassabri1ae32b92021-01-31 18:56:03 +0200532 * is_hint_crossing_range() - check if hint address crossing specified reserved
533 * range.
534 */
535static inline bool is_hint_crossing_range(enum hl_va_range_type range_type,
536 u64 start_addr, u32 size, struct asic_fixed_properties *prop) {
537 bool range_cross;
538
539 if (range_type == HL_VA_RANGE_TYPE_DRAM)
540 range_cross =
541 hl_mem_area_crosses_range(start_addr, size,
542 prop->hints_dram_reserved_va_range.start_addr,
543 prop->hints_dram_reserved_va_range.end_addr);
544 else if (range_type == HL_VA_RANGE_TYPE_HOST)
545 range_cross =
546 hl_mem_area_crosses_range(start_addr, size,
547 prop->hints_host_reserved_va_range.start_addr,
548 prop->hints_host_reserved_va_range.end_addr);
549 else
550 range_cross =
551 hl_mem_area_crosses_range(start_addr, size,
552 prop->hints_host_hpage_reserved_va_range.start_addr,
553 prop->hints_host_hpage_reserved_va_range.end_addr);
554
555 return range_cross;
556}
557
558/**
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300559 * get_va_block() - get a virtual block for the given size and alignment.
farah kassabri8d79ce12021-01-11 10:10:00 +0200560 *
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300561 * @hdev: pointer to the habanalabs device structure.
562 * @va_range: pointer to the virtual addresses range.
563 * @size: requested block size.
564 * @hint_addr: hint for requested address by the user.
565 * @va_block_align: required alignment of the virtual block start address.
farah kassabri1ae32b92021-01-31 18:56:03 +0200566 * @range_type: va range type (host, dram)
Yuri Nudelman486e1972021-06-03 17:51:58 +0300567 * @flags: additional memory flags, currently only uses HL_MEM_FORCE_HINT
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200568 *
569 * This function does the following:
570 * - Iterate on the virtual block list to find a suitable virtual block for the
farah kassabri8d79ce12021-01-11 10:10:00 +0200571 * given size, hint address and alignment.
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300572 * - Reserve the requested block and update the list.
573 * - Return the start address of the virtual block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200574 */
farah kassabri8d79ce12021-01-11 10:10:00 +0200575static u64 get_va_block(struct hl_device *hdev,
Moti Haimovskib19dc672020-11-18 20:15:29 +0200576 struct hl_va_range *va_range,
farah kassabri1ae32b92021-01-31 18:56:03 +0200577 u64 size, u64 hint_addr, u32 va_block_align,
Yuri Nudelman486e1972021-06-03 17:51:58 +0300578 enum hl_va_range_type range_type,
579 u32 flags)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200580{
581 struct hl_vm_va_block *va_block, *new_va_block = NULL;
farah kassabri1ae32b92021-01-31 18:56:03 +0200582 struct asic_fixed_properties *prop = &hdev->asic_prop;
farah kassabri8d79ce12021-01-11 10:10:00 +0200583 u64 tmp_hint_addr, valid_start, valid_size, prev_start, prev_end,
farah kassabri1ae32b92021-01-31 18:56:03 +0200584 align_mask, reserved_valid_start = 0, reserved_valid_size = 0,
585 dram_hint_mask = prop->dram_hints_align_mask;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200586 bool add_prev = false;
farah kassabri8d79ce12021-01-11 10:10:00 +0200587 bool is_align_pow_2 = is_power_of_2(va_range->page_size);
farah kassabri1ae32b92021-01-31 18:56:03 +0200588 bool is_hint_dram_addr = hl_is_dram_va(hdev, hint_addr);
Yuri Nudelman486e1972021-06-03 17:51:58 +0300589 bool force_hint = flags & HL_MEM_FORCE_HINT;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200590
farah kassabri8d79ce12021-01-11 10:10:00 +0200591 if (is_align_pow_2)
592 align_mask = ~((u64)va_block_align - 1);
593 else
594 /*
595 * with non-power-of-2 range we work only with page granularity
596 * and the start address is page aligned,
597 * so no need for alignment checking.
598 */
599 size = DIV_ROUND_UP_ULL(size, va_range->page_size) *
600 va_range->page_size;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000601
farah kassabri1ae32b92021-01-31 18:56:03 +0200602 tmp_hint_addr = hint_addr & ~dram_hint_mask;
farah kassabri8d79ce12021-01-11 10:10:00 +0200603
604 /* Check if we need to ignore hint address */
605 if ((is_align_pow_2 && (hint_addr & (va_block_align - 1))) ||
farah kassabri1ae32b92021-01-31 18:56:03 +0200606 (!is_align_pow_2 && is_hint_dram_addr &&
607 do_div(tmp_hint_addr, va_range->page_size))) {
Oded Gabbayb8e785c2021-04-26 18:32:25 +0300608
Yuri Nudelman486e1972021-06-03 17:51:58 +0300609 if (force_hint) {
610 /* Hint must be repected, so here we just fail.
611 */
612 dev_err(hdev->dev,
613 "Hint address 0x%llx is not page aligned - cannot be respected\n",
614 hint_addr);
615 return 0;
616 }
617
Oded Gabbayb8e785c2021-04-26 18:32:25 +0300618 dev_dbg(hdev->dev,
619 "Hint address 0x%llx will be ignored because it is not aligned\n",
620 hint_addr);
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300621 hint_addr = 0;
farah kassabri8d79ce12021-01-11 10:10:00 +0200622 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200623
624 mutex_lock(&va_range->lock);
625
626 print_va_list_locked(hdev, &va_range->list);
627
628 list_for_each_entry(va_block, &va_range->list, node) {
farah kassabri8d79ce12021-01-11 10:10:00 +0200629 /* Calc the first possible aligned addr */
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200630 valid_start = va_block->start;
631
farah kassabri8d79ce12021-01-11 10:10:00 +0200632 if (is_align_pow_2 && (valid_start & (va_block_align - 1))) {
Omer Shpigelman7c52fb02020-06-28 21:15:53 +0300633 valid_start &= align_mask;
634 valid_start += va_block_align;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200635 if (valid_start > va_block->end)
636 continue;
637 }
638
639 valid_size = va_block->end - valid_start;
farah kassabri8d79ce12021-01-11 10:10:00 +0200640 if (valid_size < size)
641 continue;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200642
farah kassabri1ae32b92021-01-31 18:56:03 +0200643 /*
644 * In case hint address is 0, and arc_hints_range_reservation
645 * property enabled, then avoid allocating va blocks from the
646 * range reserved for hint addresses
647 */
648 if (prop->hints_range_reservation && !hint_addr)
649 if (is_hint_crossing_range(range_type, valid_start,
650 size, prop))
651 continue;
652
farah kassabri8d79ce12021-01-11 10:10:00 +0200653 /* Pick the minimal length block which has the required size */
654 if (!new_va_block || (valid_size < reserved_valid_size)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200655 new_va_block = va_block;
Moti Haimovskib19dc672020-11-18 20:15:29 +0200656 reserved_valid_start = valid_start;
657 reserved_valid_size = valid_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200658 }
659
660 if (hint_addr && hint_addr >= valid_start &&
Moti Haimovskib19dc672020-11-18 20:15:29 +0200661 (hint_addr + size) <= va_block->end) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200662 new_va_block = va_block;
Moti Haimovskib19dc672020-11-18 20:15:29 +0200663 reserved_valid_start = hint_addr;
664 reserved_valid_size = valid_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200665 break;
666 }
667 }
668
669 if (!new_va_block) {
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200670 dev_err(hdev->dev, "no available va block for size %llu\n",
farah kassabri8d79ce12021-01-11 10:10:00 +0200671 size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200672 goto out;
673 }
674
Yuri Nudelman486e1972021-06-03 17:51:58 +0300675 if (force_hint && reserved_valid_start != hint_addr) {
676 /* Hint address must be respected. If we are here - this means
677 * we could not respect it.
678 */
679 dev_err(hdev->dev,
680 "Hint address 0x%llx could not be respected\n",
681 hint_addr);
682 reserved_valid_start = 0;
683 goto out;
684 }
685
farah kassabri8d79ce12021-01-11 10:10:00 +0200686 /*
687 * Check if there is some leftover range due to reserving the new
688 * va block, then return it to the main virtual addresses list.
689 */
Moti Haimovskib19dc672020-11-18 20:15:29 +0200690 if (reserved_valid_start > new_va_block->start) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200691 prev_start = new_va_block->start;
Moti Haimovskib19dc672020-11-18 20:15:29 +0200692 prev_end = reserved_valid_start - 1;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200693
Moti Haimovskib19dc672020-11-18 20:15:29 +0200694 new_va_block->start = reserved_valid_start;
695 new_va_block->size = reserved_valid_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200696
697 add_prev = true;
698 }
699
700 if (new_va_block->size > size) {
701 new_va_block->start += size;
702 new_va_block->size = new_va_block->end - new_va_block->start;
703 } else {
704 list_del(&new_va_block->node);
705 kfree(new_va_block);
706 }
707
708 if (add_prev)
709 add_va_block_locked(hdev, &va_range->list, prev_start,
710 prev_end);
711
712 print_va_list_locked(hdev, &va_range->list);
713out:
714 mutex_unlock(&va_range->lock);
715
Moti Haimovskib19dc672020-11-18 20:15:29 +0200716 return reserved_valid_start;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200717}
718
719/*
Ofir Bittonbe91b912020-10-22 15:04:10 +0300720 * hl_reserve_va_block() - reserve a virtual block of a given size.
721 * @hdev: pointer to the habanalabs device structure.
722 * @ctx: current context
723 * @type: virtual addresses range type.
724 * @size: requested block size.
Ofir Bitton412c41f2020-11-04 15:18:55 +0200725 * @alignment: required alignment in bytes of the virtual block start address,
726 * 0 means no alignment.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300727 *
728 * This function does the following:
729 * - Iterate on the virtual block list to find a suitable virtual block for the
Ofir Bitton412c41f2020-11-04 15:18:55 +0200730 * given size and alignment.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300731 * - Reserve the requested block and update the list.
732 * - Return the start address of the virtual block.
733 */
734u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
Ofir Bitton412c41f2020-11-04 15:18:55 +0200735 enum hl_va_range_type type, u32 size, u32 alignment)
Ofir Bittonbe91b912020-10-22 15:04:10 +0300736{
737 return get_va_block(hdev, ctx->va_range[type], size, 0,
Yuri Nudelman486e1972021-06-03 17:51:58 +0300738 max(alignment, ctx->va_range[type]->page_size),
739 type, 0);
Ofir Bittonbe91b912020-10-22 15:04:10 +0300740}
741
742/**
743 * hl_get_va_range_type() - get va_range type for the given address and size.
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200744 * @address: the start address of the area we want to validate.
745 * @size: the size in bytes of the area we want to validate.
746 * @type: returned va_range type.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300747 *
748 * Return: true if the area is inside a valid range, false otherwise.
749 */
750static int hl_get_va_range_type(struct hl_ctx *ctx, u64 address, u64 size,
751 enum hl_va_range_type *type)
752{
753 int i;
754
755 for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX; i++) {
756 if (hl_mem_area_inside_range(address, size,
757 ctx->va_range[i]->start_addr,
758 ctx->va_range[i]->end_addr)) {
759 *type = i;
760 return 0;
761 }
762 }
763
764 return -EINVAL;
765}
766
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200767/**
768 * hl_unreserve_va_block() - wrapper for add_va_block to unreserve a va block.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300769 * @hdev: pointer to the habanalabs device structure
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200770 * @ctx: pointer to the context structure.
771 * @start: start virtual address.
772 * @end: end virtual address.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300773 *
774 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200775 * - Takes the list lock and calls add_va_block_locked.
Ofir Bittonbe91b912020-10-22 15:04:10 +0300776 */
777int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
778 u64 start_addr, u64 size)
779{
780 enum hl_va_range_type type;
781 int rc;
782
783 rc = hl_get_va_range_type(ctx, start_addr, size, &type);
784 if (rc) {
785 dev_err(hdev->dev,
786 "cannot find va_range for va %#llx size %llu",
787 start_addr, size);
788 return rc;
789 }
790
791 rc = add_va_block(hdev, ctx->va_range[type], start_addr,
792 start_addr + size - 1);
793 if (rc)
794 dev_warn(hdev->dev,
795 "add va block failed for vaddr: 0x%llx\n", start_addr);
796
797 return rc;
798}
799
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200800/**
801 * get_sg_info() - get number of pages and the DMA address from SG list.
802 * @sg: the SG list.
803 * @dma_addr: pointer to DMA address to return.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200804 *
805 * Calculate the number of consecutive pages described by the SG list. Take the
806 * offset of the address in the first page, add to it the length and round it up
807 * to the number of needed pages.
808 */
809static u32 get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
810{
811 *dma_addr = sg_dma_address(sg);
812
813 return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
814 (PAGE_SIZE - 1)) >> PAGE_SHIFT;
815}
816
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200817/**
818 * init_phys_pg_pack_from_userptr() - initialize physical page pack from host
819 * memory
820 * @ctx: pointer to the context structure.
821 * @userptr: userptr to initialize from.
822 * @pphys_pg_pack: result pointer.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200823 *
824 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200825 * - Pin the physical pages related to the given virtual block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200826 * - Create a physical page pack from the physical pages related to the given
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200827 * virtual block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200828 */
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000829static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx,
830 struct hl_userptr *userptr,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300831 struct hl_vm_phys_pg_pack **pphys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200832{
833 struct hl_vm_phys_pg_pack *phys_pg_pack;
834 struct scatterlist *sg;
835 dma_addr_t dma_addr;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200836 u64 page_mask, total_npages;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000837 u32 npages, page_size = PAGE_SIZE,
Omer Shpigelman64a7e292020-01-05 09:05:45 +0000838 huge_page_size = ctx->hdev->asic_prop.pmmu_huge.page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200839 bool first = true, is_huge_page_opt = true;
840 int rc, i, j;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000841 u32 pgs_in_huge_page = huge_page_size >> __ffs(page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200842
843 phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL);
844 if (!phys_pg_pack)
845 return -ENOMEM;
846
847 phys_pg_pack->vm_type = userptr->vm_type;
848 phys_pg_pack->created_from_userptr = true;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000849 phys_pg_pack->asid = ctx->asid;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200850 atomic_set(&phys_pg_pack->mapping_cnt, 1);
851
852 /* Only if all dma_addrs are aligned to 2MB and their
853 * sizes is at least 2MB, we can use huge page mapping.
854 * We limit the 2MB optimization to this condition,
855 * since later on we acquire the related VA range as one
856 * consecutive block.
857 */
858 total_npages = 0;
859 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
860 npages = get_sg_info(sg, &dma_addr);
861
862 total_npages += npages;
863
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000864 if ((npages % pgs_in_huge_page) ||
865 (dma_addr & (huge_page_size - 1)))
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200866 is_huge_page_opt = false;
867 }
868
869 if (is_huge_page_opt) {
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000870 page_size = huge_page_size;
871 do_div(total_npages, pgs_in_huge_page);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200872 }
873
874 page_mask = ~(((u64) page_size) - 1);
875
Omer Shpigelman4eb1d122019-03-07 15:47:19 +0200876 phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64),
877 GFP_KERNEL);
Ofir Bitton08391522020-08-11 08:57:45 +0300878 if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200879 rc = -ENOMEM;
880 goto page_pack_arr_mem_err;
881 }
882
883 phys_pg_pack->npages = total_npages;
884 phys_pg_pack->page_size = page_size;
885 phys_pg_pack->total_size = total_npages * page_size;
886
887 j = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200888 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) {
889 npages = get_sg_info(sg, &dma_addr);
890
891 /* align down to physical page size and save the offset */
892 if (first) {
893 first = false;
894 phys_pg_pack->offset = dma_addr & (page_size - 1);
895 dma_addr &= page_mask;
896 }
897
898 while (npages) {
899 phys_pg_pack->pages[j++] = dma_addr;
900 dma_addr += page_size;
901
902 if (is_huge_page_opt)
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000903 npages -= pgs_in_huge_page;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200904 else
905 npages--;
906 }
907 }
908
909 *pphys_pg_pack = phys_pg_pack;
910
911 return 0;
912
913page_pack_arr_mem_err:
914 kfree(phys_pg_pack);
915
916 return rc;
917}
918
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200919/**
920 * map_phys_pg_pack() - maps the physical page pack..
921 * @ctx: pointer to the context structure.
922 * @vaddr: start address of the virtual area to map from.
923 * @phys_pg_pack: the pack of physical pages to map to.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200924 *
925 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200926 * - Maps each chunk of virtual memory to matching physical chunk.
927 * - Stores number of successful mappings in the given argument.
928 * - Returns 0 on success, error code otherwise.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200929 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300930static int map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
931 struct hl_vm_phys_pg_pack *phys_pg_pack)
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200932{
933 struct hl_device *hdev = ctx->hdev;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200934 u64 next_vaddr = vaddr, paddr, mapped_pg_cnt = 0, i;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200935 u32 page_size = phys_pg_pack->page_size;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200936 int rc = 0;
farah kassabri2f6274e2021-03-11 11:24:57 +0200937 bool is_host_addr;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200938
939 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
940 paddr = phys_pg_pack->pages[i];
941
Ofir Bitton5c054872020-10-22 15:13:10 +0300942 rc = hl_mmu_map_page(ctx, next_vaddr, paddr, page_size,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +0200943 (i + 1) == phys_pg_pack->npages);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200944 if (rc) {
945 dev_err(hdev->dev,
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200946 "map failed for handle %u, npages: %llu, mapped: %llu",
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200947 phys_pg_pack->handle, phys_pg_pack->npages,
948 mapped_pg_cnt);
949 goto err;
950 }
951
952 mapped_pg_cnt++;
953 next_vaddr += page_size;
954 }
955
956 return 0;
957
958err:
farah kassabri2f6274e2021-03-11 11:24:57 +0200959 is_host_addr = !hl_is_dram_va(hdev, vaddr);
960
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200961 next_vaddr = vaddr;
962 for (i = 0 ; i < mapped_pg_cnt ; i++) {
Ofir Bitton5c054872020-10-22 15:13:10 +0300963 if (hl_mmu_unmap_page(ctx, next_vaddr, page_size,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +0200964 (i + 1) == mapped_pg_cnt))
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200965 dev_warn_ratelimited(hdev->dev,
966 "failed to unmap handle %u, va: 0x%llx, pa: 0x%llx, page size: %u\n",
967 phys_pg_pack->handle, next_vaddr,
968 phys_pg_pack->pages[i], page_size);
969
970 next_vaddr += page_size;
farah kassabri2f6274e2021-03-11 11:24:57 +0200971
972 /*
973 * unmapping on Palladium can be really long, so avoid a CPU
974 * soft lockup bug by sleeping a little between unmapping pages
975 *
976 * In addition, on host num of pages could be huge,
977 * because page size could be 4KB, so when unmapping host
978 * pages sleep every 32K pages to avoid soft lockup
979 */
980 if (hdev->pldm || (is_host_addr && (i & 0x7FFF) == 0))
981 usleep_range(50, 200);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200982 }
983
984 return rc;
985}
986
Omer Shpigelman3b762f52020-12-09 13:28:46 +0200987/**
988 * unmap_phys_pg_pack() - unmaps the physical page pack.
989 * @ctx: pointer to the context structure.
990 * @vaddr: start address of the virtual area to unmap.
991 * @phys_pg_pack: the pack of physical pages to unmap.
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300992 */
993static void unmap_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr,
994 struct hl_vm_phys_pg_pack *phys_pg_pack)
995{
996 struct hl_device *hdev = ctx->hdev;
997 u64 next_vaddr, i;
Oded Gabbay94883072021-01-11 17:49:30 +0200998 bool is_host_addr;
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +0300999 u32 page_size;
1000
Oded Gabbay94883072021-01-11 17:49:30 +02001001 is_host_addr = !hl_is_dram_va(hdev, vaddr);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001002 page_size = phys_pg_pack->page_size;
1003 next_vaddr = vaddr;
1004
1005 for (i = 0 ; i < phys_pg_pack->npages ; i++, next_vaddr += page_size) {
Ofir Bitton5c054872020-10-22 15:13:10 +03001006 if (hl_mmu_unmap_page(ctx, next_vaddr, page_size,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +02001007 (i + 1) == phys_pg_pack->npages))
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001008 dev_warn_ratelimited(hdev->dev,
1009 "unmap failed for vaddr: 0x%llx\n", next_vaddr);
1010
1011 /*
1012 * unmapping on Palladium can be really long, so avoid a CPU
1013 * soft lockup bug by sleeping a little between unmapping pages
Oded Gabbay94883072021-01-11 17:49:30 +02001014 *
farah kassabri2f6274e2021-03-11 11:24:57 +02001015 * In addition, on host num of pages could be huge,
1016 * because page size could be 4KB, so when unmapping host
1017 * pages sleep every 32K pages to avoid soft lockup
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001018 */
Oded Gabbay94883072021-01-11 17:49:30 +02001019 if (hdev->pldm || (is_host_addr && (i & 0x7FFF) == 0))
1020 usleep_range(50, 200);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001021 }
1022}
1023
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001024static int get_paddr_from_handle(struct hl_ctx *ctx, struct hl_mem_in *args,
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001025 u64 *paddr)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001026{
1027 struct hl_device *hdev = ctx->hdev;
1028 struct hl_vm *vm = &hdev->vm;
1029 struct hl_vm_phys_pg_pack *phys_pg_pack;
1030 u32 handle;
1031
1032 handle = lower_32_bits(args->map_device.handle);
1033 spin_lock(&vm->idr_lock);
1034 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
1035 if (!phys_pg_pack) {
1036 spin_unlock(&vm->idr_lock);
1037 dev_err(hdev->dev, "no match for handle %u\n", handle);
1038 return -EINVAL;
1039 }
1040
1041 *paddr = phys_pg_pack->pages[0];
1042
1043 spin_unlock(&vm->idr_lock);
1044
1045 return 0;
1046}
1047
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001048/**
1049 * map_device_va() - map the given memory.
1050 * @ctx: pointer to the context structure.
1051 * @args: host parameters with handle/host virtual address.
1052 * @device_addr: pointer to result device virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001053 *
1054 * This function does the following:
1055 * - If given a physical device memory handle, map to a device virtual block
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001056 * and return the start address of this block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001057 * - If given a host virtual address and size, find the related physical pages,
1058 * map a device virtual block to this pages and return the start address of
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001059 * this block.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001060 */
1061static int map_device_va(struct hl_ctx *ctx, struct hl_mem_in *args,
1062 u64 *device_addr)
1063{
1064 struct hl_device *hdev = ctx->hdev;
1065 struct hl_vm *vm = &hdev->vm;
1066 struct hl_vm_phys_pg_pack *phys_pg_pack;
1067 struct hl_userptr *userptr = NULL;
1068 struct hl_vm_hash_node *hnode;
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001069 struct hl_va_range *va_range;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001070 enum vm_type_t *vm_type;
1071 u64 ret_vaddr, hint_addr;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001072 u32 handle = 0, va_block_align;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001073 int rc;
1074 bool is_userptr = args->flags & HL_MEM_USERPTR;
farah kassabri1ae32b92021-01-31 18:56:03 +02001075 enum hl_va_range_type va_range_type = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001076
1077 /* Assume failure */
1078 *device_addr = 0;
1079
1080 if (is_userptr) {
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001081 u64 addr = args->map_host.host_virt_addr,
1082 size = args->map_host.mem_size;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001083 u32 page_size = hdev->asic_prop.pmmu.page_size,
1084 huge_page_size = hdev->asic_prop.pmmu_huge.page_size;
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001085
1086 rc = dma_map_host_va(hdev, addr, size, &userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001087 if (rc) {
1088 dev_err(hdev->dev, "failed to get userptr from va\n");
1089 return rc;
1090 }
1091
Omer Shpigelman54bb6742019-11-14 18:23:55 +00001092 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001093 &phys_pg_pack);
1094 if (rc) {
1095 dev_err(hdev->dev,
1096 "unable to init page pack for vaddr 0x%llx\n",
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001097 addr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001098 goto init_page_pack_err;
1099 }
1100
1101 vm_type = (enum vm_type_t *) userptr;
1102 hint_addr = args->map_host.hint_addr;
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001103 handle = phys_pg_pack->handle;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001104
1105 /* get required alignment */
1106 if (phys_pg_pack->page_size == page_size) {
Ofir Bitton784b9162020-10-22 11:05:55 +03001107 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST];
farah kassabri1ae32b92021-01-31 18:56:03 +02001108 va_range_type = HL_VA_RANGE_TYPE_HOST;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001109 /*
1110 * huge page alignment may be needed in case of regular
1111 * page mapping, depending on the host VA alignment
1112 */
1113 if (addr & (huge_page_size - 1))
1114 va_block_align = page_size;
1115 else
1116 va_block_align = huge_page_size;
1117 } else {
1118 /*
1119 * huge page alignment is needed in case of huge page
1120 * mapping
1121 */
Ofir Bitton784b9162020-10-22 11:05:55 +03001122 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE];
farah kassabri1ae32b92021-01-31 18:56:03 +02001123 va_range_type = HL_VA_RANGE_TYPE_HOST_HUGE;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001124 va_block_align = huge_page_size;
1125 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001126 } else {
1127 handle = lower_32_bits(args->map_device.handle);
1128
1129 spin_lock(&vm->idr_lock);
1130 phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle);
1131 if (!phys_pg_pack) {
1132 spin_unlock(&vm->idr_lock);
1133 dev_err(hdev->dev,
1134 "no match for handle %u\n", handle);
1135 return -EINVAL;
1136 }
1137
1138 /* increment now to avoid freeing device memory while mapping */
1139 atomic_inc(&phys_pg_pack->mapping_cnt);
1140
1141 spin_unlock(&vm->idr_lock);
1142
1143 vm_type = (enum vm_type_t *) phys_pg_pack;
1144
1145 hint_addr = args->map_device.hint_addr;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001146
Moti Haimovskib19dc672020-11-18 20:15:29 +02001147 /* DRAM VA alignment is the same as the MMU page size */
Ofir Bitton784b9162020-10-22 11:05:55 +03001148 va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM];
farah kassabri1ae32b92021-01-31 18:56:03 +02001149 va_range_type = HL_VA_RANGE_TYPE_DRAM;
Omer Shpigelman7c52fb02020-06-28 21:15:53 +03001150 va_block_align = hdev->asic_prop.dmmu.page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001151 }
1152
1153 /*
1154 * relevant for mapping device physical memory only, as host memory is
1155 * implicitly shared
1156 */
1157 if (!is_userptr && !(phys_pg_pack->flags & HL_MEM_SHARED) &&
1158 phys_pg_pack->asid != ctx->asid) {
1159 dev_err(hdev->dev,
1160 "Failed to map memory, handle %u is not shared\n",
1161 handle);
1162 rc = -EPERM;
1163 goto shared_err;
1164 }
1165
1166 hnode = kzalloc(sizeof(*hnode), GFP_KERNEL);
1167 if (!hnode) {
1168 rc = -ENOMEM;
1169 goto hnode_err;
1170 }
1171
Yuri Nudelman486e1972021-06-03 17:51:58 +03001172 if (hint_addr && phys_pg_pack->offset) {
1173 if (args->flags & HL_MEM_FORCE_HINT) {
1174 /* If hint must be repected, since we can't - just fail.
1175 */
1176 dev_err(hdev->dev,
1177 "Hint address 0x%llx cannot be respected because source memory is not aligned 0x%x\n",
1178 hint_addr, phys_pg_pack->offset);
1179 rc = -EINVAL;
1180 goto va_block_err;
1181 }
1182 dev_dbg(hdev->dev,
1183 "Hint address 0x%llx will be ignored because source memory is not aligned 0x%x\n",
1184 hint_addr, phys_pg_pack->offset);
1185 }
1186
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001187 ret_vaddr = get_va_block(hdev, va_range, phys_pg_pack->total_size,
farah kassabri1ae32b92021-01-31 18:56:03 +02001188 hint_addr, va_block_align,
Yuri Nudelman486e1972021-06-03 17:51:58 +03001189 va_range_type, args->flags);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001190 if (!ret_vaddr) {
1191 dev_err(hdev->dev, "no available va block for handle %u\n",
1192 handle);
1193 rc = -ENOMEM;
1194 goto va_block_err;
1195 }
1196
1197 mutex_lock(&ctx->mmu_lock);
1198
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001199 rc = map_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001200 if (rc) {
1201 mutex_unlock(&ctx->mmu_lock);
1202 dev_err(hdev->dev, "mapping page pack failed for handle %u\n",
1203 handle);
1204 goto map_err;
1205 }
1206
Alon Mizrahi08c03a12021-04-08 15:30:59 +03001207 rc = hdev->asic_funcs->mmu_invalidate_cache_range(hdev, false,
1208 *vm_type, ctx->asid, ret_vaddr, phys_pg_pack->total_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001209
1210 mutex_unlock(&ctx->mmu_lock);
1211
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001212 if (rc) {
1213 dev_err(hdev->dev,
1214 "mapping handle %u failed due to MMU cache invalidation\n",
1215 handle);
1216 goto map_err;
1217 }
1218
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001219 ret_vaddr += phys_pg_pack->offset;
1220
1221 hnode->ptr = vm_type;
1222 hnode->vaddr = ret_vaddr;
1223
1224 mutex_lock(&ctx->mem_hash_lock);
1225 hash_add(ctx->mem_hash, &hnode->node, ret_vaddr);
1226 mutex_unlock(&ctx->mem_hash_lock);
1227
1228 *device_addr = ret_vaddr;
1229
1230 if (is_userptr)
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +02001231 rc = free_phys_pg_pack(hdev, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001232
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +02001233 return rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001234
1235map_err:
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001236 if (add_va_block(hdev, va_range, ret_vaddr,
1237 ret_vaddr + phys_pg_pack->total_size - 1))
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001238 dev_warn(hdev->dev,
1239 "release va block failed for handle 0x%x, vaddr: 0x%llx\n",
1240 handle, ret_vaddr);
1241
1242va_block_err:
1243 kfree(hnode);
1244hnode_err:
1245shared_err:
1246 atomic_dec(&phys_pg_pack->mapping_cnt);
1247 if (is_userptr)
1248 free_phys_pg_pack(hdev, phys_pg_pack);
1249init_page_pack_err:
1250 if (is_userptr)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001251 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001252
1253 return rc;
1254}
1255
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001256/**
1257 * unmap_device_va() - unmap the given device virtual address.
1258 * @ctx: pointer to the context structure.
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001259 * @args: host parameters with device virtual address to unmap.
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001260 * @ctx_free: true if in context free flow, false otherwise.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001261 *
1262 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001263 * - unmap the physical pages related to the given virtual address.
1264 * - return the device virtual block to the virtual block list.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001265 */
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001266static int unmap_device_va(struct hl_ctx *ctx, struct hl_mem_in *args,
1267 bool ctx_free)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001268{
1269 struct hl_device *hdev = ctx->hdev;
Moti Haimovskib19dc672020-11-18 20:15:29 +02001270 struct asic_fixed_properties *prop = &hdev->asic_prop;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001271 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
1272 struct hl_vm_hash_node *hnode = NULL;
1273 struct hl_userptr *userptr = NULL;
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001274 struct hl_va_range *va_range;
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001275 u64 vaddr = args->unmap.device_virt_addr;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001276 enum vm_type_t *vm_type;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001277 bool is_userptr;
Tomer Tayarc68f1ba2020-06-01 09:56:47 +03001278 int rc = 0;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001279
1280 /* protect from double entrance */
1281 mutex_lock(&ctx->mem_hash_lock);
1282 hash_for_each_possible(ctx->mem_hash, hnode, node, (unsigned long)vaddr)
1283 if (vaddr == hnode->vaddr)
1284 break;
1285
1286 if (!hnode) {
1287 mutex_unlock(&ctx->mem_hash_lock);
1288 dev_err(hdev->dev,
1289 "unmap failed, no mem hnode for vaddr 0x%llx\n",
1290 vaddr);
1291 return -EINVAL;
1292 }
1293
1294 hash_del(&hnode->node);
1295 mutex_unlock(&ctx->mem_hash_lock);
1296
1297 vm_type = hnode->ptr;
1298
1299 if (*vm_type == VM_TYPE_USERPTR) {
1300 is_userptr = true;
1301 userptr = hnode->ptr;
Omer Shpigelman54bb6742019-11-14 18:23:55 +00001302 rc = init_phys_pg_pack_from_userptr(ctx, userptr,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001303 &phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001304 if (rc) {
1305 dev_err(hdev->dev,
1306 "unable to init page pack for vaddr 0x%llx\n",
1307 vaddr);
1308 goto vm_type_err;
1309 }
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001310
1311 if (phys_pg_pack->page_size ==
1312 hdev->asic_prop.pmmu.page_size)
Ofir Bitton784b9162020-10-22 11:05:55 +03001313 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST];
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001314 else
Ofir Bitton784b9162020-10-22 11:05:55 +03001315 va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE];
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001316 } else if (*vm_type == VM_TYPE_PHYS_PACK) {
1317 is_userptr = false;
Ofir Bitton784b9162020-10-22 11:05:55 +03001318 va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM];
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001319 phys_pg_pack = hnode->ptr;
1320 } else {
1321 dev_warn(hdev->dev,
1322 "unmap failed, unknown vm desc for vaddr 0x%llx\n",
1323 vaddr);
1324 rc = -EFAULT;
1325 goto vm_type_err;
1326 }
1327
1328 if (atomic_read(&phys_pg_pack->mapping_cnt) == 0) {
1329 dev_err(hdev->dev, "vaddr 0x%llx is not mapped\n", vaddr);
1330 rc = -EINVAL;
1331 goto mapping_cnt_err;
1332 }
1333
Moti Haimovskib19dc672020-11-18 20:15:29 +02001334 if (!is_userptr && !is_power_of_2(phys_pg_pack->page_size))
1335 vaddr = prop->dram_base_address +
1336 DIV_ROUND_DOWN_ULL(vaddr - prop->dram_base_address,
1337 phys_pg_pack->page_size) *
1338 phys_pg_pack->page_size;
1339 else
1340 vaddr &= ~(((u64) phys_pg_pack->page_size) - 1);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001341
1342 mutex_lock(&ctx->mmu_lock);
1343
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001344 unmap_phys_pg_pack(ctx, vaddr, phys_pg_pack);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001345
Omer Shpigelmanbea84c42019-11-14 18:23:58 +00001346 /*
1347 * During context free this function is called in a loop to clean all
1348 * the context mappings. Hence the cache invalidation can be called once
1349 * at the loop end rather than for each iteration
1350 */
1351 if (!ctx_free)
Alon Mizrahi08c03a12021-04-08 15:30:59 +03001352 rc = hdev->asic_funcs->mmu_invalidate_cache_range(hdev, true,
1353 *vm_type, ctx->asid, vaddr,
1354 phys_pg_pack->total_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001355
1356 mutex_unlock(&ctx->mmu_lock);
1357
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001358 /*
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001359 * If the context is closing we don't need to check for the MMU cache
1360 * invalidation return code and update the VA free list as in this flow
1361 * we invalidate the MMU cache outside of this unmap function and the VA
1362 * free list will be freed anyway.
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001363 */
1364 if (!ctx_free) {
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001365 int tmp_rc;
1366
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001367 if (rc)
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001368 dev_err(hdev->dev,
1369 "unmapping vaddr 0x%llx failed due to MMU cache invalidation\n",
1370 vaddr);
1371
1372 tmp_rc = add_va_block(hdev, va_range, vaddr,
1373 vaddr + phys_pg_pack->total_size - 1);
1374 if (tmp_rc) {
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001375 dev_warn(hdev->dev,
1376 "add va block failed for vaddr: 0x%llx\n",
1377 vaddr);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001378 if (!rc)
1379 rc = tmp_rc;
1380 }
Omer Shpigelman71c5e552019-11-14 18:23:57 +00001381 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001382
1383 atomic_dec(&phys_pg_pack->mapping_cnt);
1384 kfree(hnode);
1385
1386 if (is_userptr) {
Bharat Jauharid4b1e5d2021-03-18 12:11:19 +02001387 rc = free_phys_pg_pack(hdev, phys_pg_pack);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001388 dma_unmap_host_va(hdev, userptr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001389 }
1390
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001391 return rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001392
1393mapping_cnt_err:
1394 if (is_userptr)
1395 free_phys_pg_pack(hdev, phys_pg_pack);
1396vm_type_err:
1397 mutex_lock(&ctx->mem_hash_lock);
1398 hash_add(ctx->mem_hash, &hnode->node, vaddr);
1399 mutex_unlock(&ctx->mem_hash_lock);
1400
1401 return rc;
1402}
1403
Oded Gabbay6df50d22021-02-05 16:04:34 +02001404static int map_block(struct hl_device *hdev, u64 address, u64 *handle,
1405 u32 *size)
Ofir Bittond00697f2021-01-05 12:55:06 +02001406{
1407 u32 block_id = 0;
1408 int rc;
1409
Oded Gabbay6df50d22021-02-05 16:04:34 +02001410 rc = hdev->asic_funcs->get_hw_block_id(hdev, address, size, &block_id);
Ofir Bittond00697f2021-01-05 12:55:06 +02001411
1412 *handle = block_id | HL_MMAP_TYPE_BLOCK;
1413 *handle <<= PAGE_SHIFT;
1414
1415 return rc;
1416}
1417
1418static void hw_block_vm_close(struct vm_area_struct *vma)
1419{
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001420 struct hl_vm_hw_block_list_node *lnode =
1421 (struct hl_vm_hw_block_list_node *) vma->vm_private_data;
1422 struct hl_ctx *ctx = lnode->ctx;
Ofir Bittond00697f2021-01-05 12:55:06 +02001423
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001424 mutex_lock(&ctx->hw_block_list_lock);
1425 list_del(&lnode->node);
1426 mutex_unlock(&ctx->hw_block_list_lock);
Ofir Bittond00697f2021-01-05 12:55:06 +02001427 hl_ctx_put(ctx);
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001428 kfree(lnode);
Ofir Bittond00697f2021-01-05 12:55:06 +02001429 vma->vm_private_data = NULL;
1430}
1431
1432static const struct vm_operations_struct hw_block_vm_ops = {
1433 .close = hw_block_vm_close
1434};
1435
1436/**
1437 * hl_hw_block_mmap() - mmap a hw block to user.
1438 * @hpriv: pointer to the private data of the fd
1439 * @vma: pointer to vm_area_struct of the process
1440 *
1441 * Driver increments context reference for every HW block mapped in order
1442 * to prevent user from closing FD without unmapping first
1443 */
1444int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
1445{
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001446 struct hl_vm_hw_block_list_node *lnode;
Ofir Bittond00697f2021-01-05 12:55:06 +02001447 struct hl_device *hdev = hpriv->hdev;
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001448 struct hl_ctx *ctx = hpriv->ctx;
Ofir Bittond00697f2021-01-05 12:55:06 +02001449 u32 block_id, block_size;
1450 int rc;
1451
1452 /* We use the page offset to hold the block id and thus we need to clear
1453 * it before doing the mmap itself
1454 */
1455 block_id = vma->vm_pgoff;
1456 vma->vm_pgoff = 0;
1457
1458 /* Driver only allows mapping of a complete HW block */
1459 block_size = vma->vm_end - vma->vm_start;
1460
Ofir Bittond00697f2021-01-05 12:55:06 +02001461 if (!access_ok((void __user *) (uintptr_t) vma->vm_start, block_size)) {
Ofir Bittond00697f2021-01-05 12:55:06 +02001462 dev_err(hdev->dev,
1463 "user pointer is invalid - 0x%lx\n",
1464 vma->vm_start);
1465
1466 return -EINVAL;
1467 }
1468
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001469 lnode = kzalloc(sizeof(*lnode), GFP_KERNEL);
1470 if (!lnode)
1471 return -ENOMEM;
Ofir Bittond00697f2021-01-05 12:55:06 +02001472
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001473 vma->vm_ops = &hw_block_vm_ops;
1474 vma->vm_private_data = lnode;
1475
1476 hl_ctx_get(hdev, ctx);
Ofir Bittond00697f2021-01-05 12:55:06 +02001477
1478 rc = hdev->asic_funcs->hw_block_mmap(hdev, vma, block_id, block_size);
1479 if (rc) {
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001480 hl_ctx_put(ctx);
1481 kfree(lnode);
Ofir Bittond00697f2021-01-05 12:55:06 +02001482 return rc;
1483 }
1484
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001485 lnode->ctx = ctx;
1486 lnode->vaddr = vma->vm_start;
1487 lnode->size = block_size;
1488 lnode->id = block_id;
1489
1490 mutex_lock(&ctx->hw_block_list_lock);
1491 list_add_tail(&lnode->node, &ctx->hw_block_mem_list);
1492 mutex_unlock(&ctx->hw_block_list_lock);
1493
Ofir Bittond00697f2021-01-05 12:55:06 +02001494 vma->vm_pgoff = block_id;
1495
1496 return 0;
1497}
1498
Oded Gabbay54303a12019-04-04 14:42:26 +03001499static int mem_ioctl_no_mmu(struct hl_fpriv *hpriv, union hl_mem_args *args)
1500{
1501 struct hl_device *hdev = hpriv->hdev;
1502 struct hl_ctx *ctx = hpriv->ctx;
Ofir Bittond00697f2021-01-05 12:55:06 +02001503 u64 block_handle, device_addr = 0;
Oded Gabbay6df50d22021-02-05 16:04:34 +02001504 u32 handle = 0, block_size;
Oded Gabbay54303a12019-04-04 14:42:26 +03001505 int rc;
1506
1507 switch (args->in.op) {
1508 case HL_MEM_OP_ALLOC:
1509 if (args->in.alloc.mem_size == 0) {
1510 dev_err(hdev->dev,
1511 "alloc size must be larger than 0\n");
1512 rc = -EINVAL;
1513 goto out;
1514 }
1515
1516 /* Force contiguous as there are no real MMU
1517 * translations to overcome physical memory gaps
1518 */
1519 args->in.flags |= HL_MEM_CONTIGUOUS;
1520 rc = alloc_device_memory(ctx, &args->in, &handle);
1521
1522 memset(args, 0, sizeof(*args));
1523 args->out.handle = (__u64) handle;
1524 break;
1525
1526 case HL_MEM_OP_FREE:
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001527 rc = free_device_memory(ctx, &args->in);
Oded Gabbay54303a12019-04-04 14:42:26 +03001528 break;
1529
1530 case HL_MEM_OP_MAP:
1531 if (args->in.flags & HL_MEM_USERPTR) {
1532 device_addr = args->in.map_host.host_virt_addr;
1533 rc = 0;
1534 } else {
1535 rc = get_paddr_from_handle(ctx, &args->in,
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001536 &device_addr);
Oded Gabbay54303a12019-04-04 14:42:26 +03001537 }
1538
1539 memset(args, 0, sizeof(*args));
1540 args->out.device_virt_addr = device_addr;
1541 break;
1542
1543 case HL_MEM_OP_UNMAP:
1544 rc = 0;
1545 break;
1546
Ofir Bittond00697f2021-01-05 12:55:06 +02001547 case HL_MEM_OP_MAP_BLOCK:
1548 rc = map_block(hdev, args->in.map_block.block_addr,
Oded Gabbay6df50d22021-02-05 16:04:34 +02001549 &block_handle, &block_size);
1550 args->out.block_handle = block_handle;
1551 args->out.block_size = block_size;
Ofir Bittond00697f2021-01-05 12:55:06 +02001552 break;
1553
Oded Gabbay54303a12019-04-04 14:42:26 +03001554 default:
1555 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1556 rc = -ENOTTY;
1557 break;
1558 }
1559
1560out:
1561 return rc;
1562}
1563
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001564int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
1565{
Ofir Bitton66a76402020-10-05 14:40:10 +03001566 enum hl_device_status status;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001567 union hl_mem_args *args = data;
1568 struct hl_device *hdev = hpriv->hdev;
1569 struct hl_ctx *ctx = hpriv->ctx;
Ofir Bittond00697f2021-01-05 12:55:06 +02001570 u64 block_handle, device_addr = 0;
Oded Gabbay6df50d22021-02-05 16:04:34 +02001571 u32 handle = 0, block_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001572 int rc;
1573
Ofir Bitton66a76402020-10-05 14:40:10 +03001574 if (!hl_device_operational(hdev, &status)) {
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001575 dev_warn_ratelimited(hdev->dev,
Oded Gabbay3f5398c2019-04-06 15:41:35 +03001576 "Device is %s. Can't execute MEMORY IOCTL\n",
Ofir Bitton66a76402020-10-05 14:40:10 +03001577 hdev->status[status]);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001578 return -EBUSY;
1579 }
1580
Oded Gabbay54303a12019-04-04 14:42:26 +03001581 if (!hdev->mmu_enable)
1582 return mem_ioctl_no_mmu(hpriv, args);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001583
Oded Gabbay54303a12019-04-04 14:42:26 +03001584 switch (args->in.op) {
1585 case HL_MEM_OP_ALLOC:
Oded Gabbay54303a12019-04-04 14:42:26 +03001586 if (args->in.alloc.mem_size == 0) {
1587 dev_err(hdev->dev,
1588 "alloc size must be larger than 0\n");
1589 rc = -EINVAL;
1590 goto out;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001591 }
Oded Gabbay3e622992020-10-18 15:32:23 +03001592
1593 /* If DRAM does not support virtual memory the driver won't
1594 * handle the allocation/freeing of that memory. However, for
1595 * system administration/monitoring purposes, the driver will
1596 * keep track of the amount of DRAM memory that is allocated
1597 * and freed by the user. Because this code totally relies on
1598 * the user's input, the driver can't ensure the validity
1599 * of this accounting.
1600 */
Oded Gabbay7f070c92020-11-09 09:48:31 +02001601 if (!hdev->asic_prop.dram_supports_virtual_memory) {
Oded Gabbay3e622992020-10-18 15:32:23 +03001602 atomic64_add(args->in.alloc.mem_size,
1603 &ctx->dram_phys_mem);
1604 atomic64_add(args->in.alloc.mem_size,
1605 &hdev->dram_used_mem);
1606
1607 dev_dbg(hdev->dev, "DRAM alloc is not supported\n");
1608 rc = 0;
1609
1610 memset(args, 0, sizeof(*args));
1611 args->out.handle = 0;
1612 goto out;
1613 }
1614
Oded Gabbay54303a12019-04-04 14:42:26 +03001615 rc = alloc_device_memory(ctx, &args->in, &handle);
1616
1617 memset(args, 0, sizeof(*args));
1618 args->out.handle = (__u64) handle;
1619 break;
1620
1621 case HL_MEM_OP_FREE:
Oded Gabbay3e622992020-10-18 15:32:23 +03001622 /* If DRAM does not support virtual memory the driver won't
1623 * handle the allocation/freeing of that memory. However, for
1624 * system administration/monitoring purposes, the driver will
1625 * keep track of the amount of DRAM memory that is allocated
1626 * and freed by the user. Because this code totally relies on
1627 * the user's input, the driver can't ensure the validity
1628 * of this accounting.
1629 */
Oded Gabbay7f070c92020-11-09 09:48:31 +02001630 if (!hdev->asic_prop.dram_supports_virtual_memory) {
Oded Gabbay3e622992020-10-18 15:32:23 +03001631 atomic64_sub(args->in.alloc.mem_size,
1632 &ctx->dram_phys_mem);
1633 atomic64_sub(args->in.alloc.mem_size,
1634 &hdev->dram_used_mem);
1635
1636 dev_dbg(hdev->dev, "DRAM alloc is not supported\n");
1637 rc = 0;
1638
1639 goto out;
1640 }
1641
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001642 rc = free_device_memory(ctx, &args->in);
Oded Gabbay54303a12019-04-04 14:42:26 +03001643 break;
1644
1645 case HL_MEM_OP_MAP:
1646 rc = map_device_va(ctx, &args->in, &device_addr);
1647
1648 memset(args, 0, sizeof(*args));
1649 args->out.device_virt_addr = device_addr;
1650 break;
1651
1652 case HL_MEM_OP_UNMAP:
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02001653 rc = unmap_device_va(ctx, &args->in, false);
Oded Gabbay54303a12019-04-04 14:42:26 +03001654 break;
1655
Ofir Bittond00697f2021-01-05 12:55:06 +02001656 case HL_MEM_OP_MAP_BLOCK:
1657 rc = map_block(hdev, args->in.map_block.block_addr,
Oded Gabbay6df50d22021-02-05 16:04:34 +02001658 &block_handle, &block_size);
1659 args->out.block_handle = block_handle;
1660 args->out.block_size = block_size;
Oded Gabbay54303a12019-04-04 14:42:26 +03001661 break;
1662
1663 default:
1664 dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n");
1665 rc = -ENOTTY;
1666 break;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001667 }
1668
1669out:
1670 return rc;
1671}
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001672
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001673static int get_user_memory(struct hl_device *hdev, u64 addr, u64 size,
1674 u32 npages, u64 start, u32 offset,
1675 struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001676{
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001677 int rc;
1678
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001679 if (!access_ok((void __user *) (uintptr_t) addr, size)) {
Oded Gabbay230afe72019-02-27 00:19:18 +02001680 dev_err(hdev->dev, "user pointer is invalid - 0x%llx\n", addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001681 return -EFAULT;
1682 }
1683
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001684 userptr->pages = kvmalloc_array(npages, sizeof(*userptr->pages),
1685 GFP_KERNEL);
1686 if (!userptr->pages)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001687 return -ENOMEM;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001688
Daniel Vetterd88a0c12020-11-27 17:41:18 +01001689 rc = pin_user_pages_fast(start, npages,
1690 FOLL_FORCE | FOLL_WRITE | FOLL_LONGTERM,
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001691 userptr->pages);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001692
1693 if (rc != npages) {
1694 dev_err(hdev->dev,
Tomer Tayarf5d6e392021-06-10 20:48:39 +03001695 "Failed (%d) to pin host memory with user ptr 0x%llx, size 0x%llx, npages %d\n",
1696 rc, addr, size, npages);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001697 if (rc < 0)
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001698 goto destroy_pages;
1699 npages = rc;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001700 rc = -EFAULT;
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001701 goto put_pages;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001702 }
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001703 userptr->npages = npages;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001704
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001705 rc = sg_alloc_table_from_pages(userptr->sgt,
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001706 userptr->pages,
Ofir Bittond5eb8372021-02-14 15:35:56 +02001707 npages, offset, size, GFP_KERNEL);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001708 if (rc < 0) {
1709 dev_err(hdev->dev, "failed to create SG table from pages\n");
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001710 goto put_pages;
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001711 }
1712
1713 return 0;
1714
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001715put_pages:
1716 unpin_user_pages(userptr->pages, npages);
1717destroy_pages:
1718 kvfree(userptr->pages);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001719 return rc;
1720}
1721
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001722/**
1723 * hl_pin_host_memory() - pins a chunk of host memory.
1724 * @hdev: pointer to the habanalabs device structure.
1725 * @addr: the host virtual address of the memory area.
1726 * @size: the size of the memory area.
1727 * @userptr: pointer to hl_userptr structure.
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001728 *
1729 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001730 * - Pins the physical pages.
1731 * - Create an SG list from those pages.
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001732 */
1733int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
1734 struct hl_userptr *userptr)
1735{
1736 u64 start, end;
1737 u32 npages, offset;
1738 int rc;
1739
1740 if (!size) {
1741 dev_err(hdev->dev, "size to pin is invalid - %llu\n", size);
1742 return -EINVAL;
1743 }
1744
1745 /*
1746 * If the combination of the address and size requested for this memory
1747 * region causes an integer overflow, return error.
1748 */
1749 if (((addr + size) < addr) ||
1750 PAGE_ALIGN(addr + size) < (addr + size)) {
1751 dev_err(hdev->dev,
1752 "user pointer 0x%llx + %llu causes integer overflow\n",
1753 addr, size);
1754 return -EINVAL;
1755 }
1756
Ofir Bittond5eb8372021-02-14 15:35:56 +02001757 userptr->sgt = kzalloc(sizeof(*userptr->sgt), GFP_KERNEL);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001758 if (!userptr->sgt)
1759 return -ENOMEM;
1760
1761 start = addr & PAGE_MASK;
1762 offset = addr & ~PAGE_MASK;
1763 end = PAGE_ALIGN(addr + size);
1764 npages = (end - start) >> PAGE_SHIFT;
1765
1766 userptr->size = size;
1767 userptr->addr = addr;
1768 userptr->dma_mapped = false;
1769 INIT_LIST_HEAD(&userptr->job_node);
1770
1771 rc = get_user_memory(hdev, addr, size, npages, start, offset,
1772 userptr);
1773 if (rc) {
1774 dev_err(hdev->dev,
1775 "failed to get user memory for address 0x%llx\n",
1776 addr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001777 goto free_sgt;
1778 }
1779
Oded Gabbayc2164772019-02-16 00:39:24 +02001780 hl_debugfs_add_userptr(hdev, userptr);
1781
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001782 return 0;
1783
1784free_sgt:
1785 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001786 return rc;
1787}
1788
1789/*
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001790 * hl_unpin_host_memory - unpins a chunk of host memory.
1791 * @hdev: pointer to the habanalabs device structure
1792 * @userptr: pointer to hl_userptr structure
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001793 *
1794 * This function does the following:
1795 * - Unpins the physical pages related to the host memory
1796 * - Free the SG list
1797 */
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001798void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr)
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001799{
Oded Gabbayc2164772019-02-16 00:39:24 +02001800 hl_debugfs_remove_userptr(hdev, userptr);
1801
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001802 if (userptr->dma_mapped)
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001803 hdev->asic_funcs->hl_dma_unmap_sg(hdev, userptr->sgt->sgl,
1804 userptr->sgt->nents,
1805 userptr->dir);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001806
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001807 unpin_user_pages_dirty_lock(userptr->pages, userptr->npages, true);
1808 kvfree(userptr->pages);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001809
1810 list_del(&userptr->job_node);
1811
1812 sg_free_table(userptr->sgt);
1813 kfree(userptr->sgt);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001814}
1815
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001816/**
1817 * hl_userptr_delete_list() - clear userptr list.
1818 * @hdev: pointer to the habanalabs device structure.
1819 * @userptr_list: pointer to the list to clear.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001820 *
1821 * This function does the following:
1822 * - Iterates over the list and unpins the host memory and frees the userptr
1823 * structure.
1824 */
1825void hl_userptr_delete_list(struct hl_device *hdev,
1826 struct list_head *userptr_list)
1827{
1828 struct hl_userptr *userptr, *tmp;
1829
1830 list_for_each_entry_safe(userptr, tmp, userptr_list, job_node) {
1831 hl_unpin_host_memory(hdev, userptr);
1832 kfree(userptr);
1833 }
1834
1835 INIT_LIST_HEAD(userptr_list);
1836}
1837
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001838/**
1839 * hl_userptr_is_pinned() - returns whether the given userptr is pinned.
1840 * @hdev: pointer to the habanalabs device structure.
1841 * @userptr_list: pointer to the list to clear.
1842 * @userptr: pointer to userptr to check.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001843 *
1844 * This function does the following:
1845 * - Iterates over the list and checks if the given userptr is in it, means is
1846 * pinned. If so, returns true, otherwise returns false.
1847 */
1848bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr,
1849 u32 size, struct list_head *userptr_list,
1850 struct hl_userptr **userptr)
1851{
1852 list_for_each_entry((*userptr), userptr_list, job_node) {
1853 if ((addr == (*userptr)->addr) && (size == (*userptr)->size))
1854 return true;
1855 }
1856
1857 return false;
1858}
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001859
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001860/**
1861 * va_range_init() - initialize virtual addresses range.
1862 * @hdev: pointer to the habanalabs device structure.
1863 * @va_range: pointer to the range to initialize.
1864 * @start: range start address.
1865 * @end: range end address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001866 *
1867 * This function does the following:
1868 * - Initializes the virtual addresses list of the given range with the given
1869 * addresses.
1870 */
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001871static int va_range_init(struct hl_device *hdev, struct hl_va_range *va_range,
Ofir Bitton784b9162020-10-22 11:05:55 +03001872 u64 start, u64 end, u32 page_size)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001873{
1874 int rc;
1875
1876 INIT_LIST_HEAD(&va_range->list);
1877
Moti Haimovskib19dc672020-11-18 20:15:29 +02001878 /*
1879 * PAGE_SIZE alignment
1880 * it is the callers responsibility to align the addresses if the
1881 * page size is not a power of 2
1882 */
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001883
Moti Haimovskib19dc672020-11-18 20:15:29 +02001884 if (is_power_of_2(page_size)) {
1885 if (start & (PAGE_SIZE - 1)) {
1886 start &= PAGE_MASK;
1887 start += PAGE_SIZE;
1888 }
1889
1890 if (end & (PAGE_SIZE - 1))
1891 end &= PAGE_MASK;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001892 }
1893
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001894 if (start >= end) {
1895 dev_err(hdev->dev, "too small vm range for va list\n");
1896 return -EFAULT;
1897 }
1898
1899 rc = add_va_block(hdev, va_range, start, end);
1900
1901 if (rc) {
1902 dev_err(hdev->dev, "Failed to init host va list\n");
1903 return rc;
1904 }
1905
1906 va_range->start_addr = start;
1907 va_range->end_addr = end;
Ofir Bitton784b9162020-10-22 11:05:55 +03001908 va_range->page_size = page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001909
1910 return 0;
1911}
1912
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001913/**
1914 * va_range_fini() - clear a virtual addresses range.
1915 * @hdev: pointer to the habanalabs structure.
1916 * va_range: pointer to virtual addresses rang.e
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001917 *
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001918 * This function does the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001919 * - Frees the virtual addresses block list and its lock.
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001920 */
Ofir Bitton784b9162020-10-22 11:05:55 +03001921static void va_range_fini(struct hl_device *hdev, struct hl_va_range *va_range)
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001922{
1923 mutex_lock(&va_range->lock);
1924 clear_va_list_locked(hdev, &va_range->list);
1925 mutex_unlock(&va_range->lock);
1926
1927 mutex_destroy(&va_range->lock);
1928 kfree(va_range);
1929}
1930
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001931/**
1932 * vm_ctx_init_with_ranges() - initialize virtual memory for context.
1933 * @ctx: pointer to the habanalabs context structure.
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001934 * @host_range_start: host virtual addresses range start.
1935 * @host_range_end: host virtual addresses range end.
1936 * @host_huge_range_start: host virtual addresses range start for memory
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001937 * allocated with huge pages.
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001938 * @host_huge_range_end: host virtual addresses range end for memory allocated
1939 * with huge pages.
1940 * @dram_range_start: dram virtual addresses range start.
1941 * @dram_range_end: dram virtual addresses range end.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001942 *
1943 * This function initializes the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02001944 * - MMU for context.
1945 * - Virtual address to area descriptor hashtable.
1946 * - Virtual block list of available virtual memory.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001947 */
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001948static int vm_ctx_init_with_ranges(struct hl_ctx *ctx,
1949 u64 host_range_start,
1950 u64 host_range_end,
Ofir Bitton784b9162020-10-22 11:05:55 +03001951 u32 host_page_size,
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001952 u64 host_huge_range_start,
1953 u64 host_huge_range_end,
Ofir Bitton784b9162020-10-22 11:05:55 +03001954 u32 host_huge_page_size,
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001955 u64 dram_range_start,
Ofir Bitton784b9162020-10-22 11:05:55 +03001956 u64 dram_range_end,
1957 u32 dram_page_size)
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001958{
1959 struct hl_device *hdev = ctx->hdev;
Ofir Bitton784b9162020-10-22 11:05:55 +03001960 int i, rc;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001961
Ofir Bitton784b9162020-10-22 11:05:55 +03001962 for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++) {
1963 ctx->va_range[i] =
1964 kzalloc(sizeof(struct hl_va_range), GFP_KERNEL);
1965 if (!ctx->va_range[i]) {
1966 rc = -ENOMEM;
1967 goto free_va_range;
1968 }
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001969 }
1970
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001971 rc = hl_mmu_ctx_init(ctx);
1972 if (rc) {
1973 dev_err(hdev->dev, "failed to init context %d\n", ctx->asid);
Ofir Bitton784b9162020-10-22 11:05:55 +03001974 goto free_va_range;
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001975 }
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001976
1977 mutex_init(&ctx->mem_hash_lock);
1978 hash_init(ctx->mem_hash);
1979
Ofir Bitton784b9162020-10-22 11:05:55 +03001980 mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001981
Ofir Bitton784b9162020-10-22 11:05:55 +03001982 rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST],
1983 host_range_start, host_range_end, host_page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001984 if (rc) {
1985 dev_err(hdev->dev, "failed to init host vm range\n");
Ofir Bitton784b9162020-10-22 11:05:55 +03001986 goto mmu_ctx_fini;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001987 }
1988
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001989 if (hdev->pmmu_huge_range) {
Ofir Bitton784b9162020-10-22 11:05:55 +03001990 mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001991
Ofir Bitton784b9162020-10-22 11:05:55 +03001992 rc = va_range_init(hdev,
1993 ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE],
1994 host_huge_range_start, host_huge_range_end,
1995 host_huge_page_size);
Omer Shpigelman64a7e292020-01-05 09:05:45 +00001996 if (rc) {
1997 dev_err(hdev->dev,
1998 "failed to init host huge vm range\n");
Ofir Bitton784b9162020-10-22 11:05:55 +03001999 goto clear_host_va_range;
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002000 }
2001 } else {
Ofir Bitton8e718f22020-11-26 13:01:11 +02002002 kfree(ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]);
Ofir Bitton784b9162020-10-22 11:05:55 +03002003 ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] =
2004 ctx->va_range[HL_VA_RANGE_TYPE_HOST];
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002005 }
2006
Ofir Bitton784b9162020-10-22 11:05:55 +03002007 mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock);
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002008
Ofir Bitton784b9162020-10-22 11:05:55 +03002009 rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM],
2010 dram_range_start, dram_range_end, dram_page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002011 if (rc) {
2012 dev_err(hdev->dev, "failed to init dram vm range\n");
Ofir Bitton784b9162020-10-22 11:05:55 +03002013 goto clear_host_huge_va_range;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002014 }
2015
Oded Gabbayc2164772019-02-16 00:39:24 +02002016 hl_debugfs_add_ctx_mem_hash(hdev, ctx);
2017
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002018 return 0;
2019
Ofir Bitton784b9162020-10-22 11:05:55 +03002020clear_host_huge_va_range:
2021 mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002022
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002023 if (hdev->pmmu_huge_range) {
Ofir Bitton784b9162020-10-22 11:05:55 +03002024 mutex_lock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
2025 clear_va_list_locked(hdev,
2026 &ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->list);
2027 mutex_unlock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002028 }
Ofir Bitton784b9162020-10-22 11:05:55 +03002029clear_host_va_range:
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002030 if (hdev->pmmu_huge_range)
Ofir Bitton784b9162020-10-22 11:05:55 +03002031 mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock);
2032 mutex_lock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
2033 clear_va_list_locked(hdev, &ctx->va_range[HL_VA_RANGE_TYPE_HOST]->list);
2034 mutex_unlock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
2035mmu_ctx_fini:
2036 mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002037 mutex_destroy(&ctx->mem_hash_lock);
2038 hl_mmu_ctx_fini(ctx);
Ofir Bitton784b9162020-10-22 11:05:55 +03002039free_va_range:
2040 for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++)
2041 kfree(ctx->va_range[i]);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002042
2043 return rc;
2044}
2045
2046int hl_vm_ctx_init(struct hl_ctx *ctx)
2047{
2048 struct asic_fixed_properties *prop = &ctx->hdev->asic_prop;
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002049 u64 host_range_start, host_range_end, host_huge_range_start,
2050 host_huge_range_end, dram_range_start, dram_range_end;
Ofir Bitton784b9162020-10-22 11:05:55 +03002051 u32 host_page_size, host_huge_page_size, dram_page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002052
2053 atomic64_set(&ctx->dram_phys_mem, 0);
2054
2055 /*
2056 * - If MMU is enabled, init the ranges as usual.
2057 * - If MMU is disabled, in case of host mapping, the returned address
2058 * is the given one.
2059 * In case of DRAM mapping, the returned address is the physical
2060 * address of the memory related to the given handle.
2061 */
Oded Gabbayf3a965c2020-10-04 23:00:39 +03002062 if (!ctx->hdev->mmu_enable)
2063 return 0;
2064
2065 dram_range_start = prop->dmmu.start_addr;
2066 dram_range_end = prop->dmmu.end_addr;
Moti Haimovskib19dc672020-11-18 20:15:29 +02002067 dram_page_size = prop->dram_page_size ?
2068 prop->dram_page_size : prop->dmmu.page_size;
Oded Gabbayf3a965c2020-10-04 23:00:39 +03002069 host_range_start = prop->pmmu.start_addr;
2070 host_range_end = prop->pmmu.end_addr;
Ofir Bitton784b9162020-10-22 11:05:55 +03002071 host_page_size = prop->pmmu.page_size;
Oded Gabbayf3a965c2020-10-04 23:00:39 +03002072 host_huge_range_start = prop->pmmu_huge.start_addr;
2073 host_huge_range_end = prop->pmmu_huge.end_addr;
Ofir Bitton784b9162020-10-22 11:05:55 +03002074 host_huge_page_size = prop->pmmu_huge.page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002075
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002076 return vm_ctx_init_with_ranges(ctx, host_range_start, host_range_end,
Ofir Bitton784b9162020-10-22 11:05:55 +03002077 host_page_size, host_huge_range_start,
2078 host_huge_range_end, host_huge_page_size,
2079 dram_range_start, dram_range_end, dram_page_size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002080}
2081
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002082/**
2083 * hl_vm_ctx_fini() - virtual memory teardown of context.
2084 * @ctx: pointer to the habanalabs context structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002085 *
2086 * This function perform teardown the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002087 * - Virtual block list of available virtual memory.
2088 * - Virtual address to area descriptor hashtable.
2089 * - MMU for context.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002090 *
2091 * In addition this function does the following:
2092 * - Unmaps the existing hashtable nodes if the hashtable is not empty. The
2093 * hashtable should be empty as no valid mappings should exist at this
2094 * point.
2095 * - Frees any existing physical page list from the idr which relates to the
2096 * current context asid.
2097 * - This function checks the virtual block list for correctness. At this point
2098 * the list should contain one element which describes the whole virtual
2099 * memory range of the context. Otherwise, a warning is printed.
2100 */
2101void hl_vm_ctx_fini(struct hl_ctx *ctx)
2102{
2103 struct hl_device *hdev = ctx->hdev;
2104 struct hl_vm *vm = &hdev->vm;
2105 struct hl_vm_phys_pg_pack *phys_pg_list;
2106 struct hl_vm_hash_node *hnode;
2107 struct hlist_node *tmp_node;
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02002108 struct hl_mem_in args;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002109 int i;
2110
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002111 if (!hdev->mmu_enable)
Oded Gabbayf3a965c2020-10-04 23:00:39 +03002112 return;
2113
Oded Gabbayc2164772019-02-16 00:39:24 +02002114 hl_debugfs_remove_ctx_mem_hash(hdev, ctx);
2115
Omer Shpigelmane604f552019-11-14 18:23:59 +00002116 /*
2117 * Clearly something went wrong on hard reset so no point in printing
2118 * another side effect error
2119 */
2120 if (!hdev->hard_reset_pending && !hash_empty(ctx->mem_hash))
2121 dev_notice(hdev->dev,
Oded Gabbay0eab4f82020-06-22 09:52:22 +03002122 "user released device without removing its memory mappings\n");
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002123
2124 hash_for_each_safe(ctx->mem_hash, i, tmp_node, hnode, node) {
2125 dev_dbg(hdev->dev,
2126 "hl_mem_hash_node of vaddr 0x%llx of asid %d is still alive\n",
2127 hnode->vaddr, ctx->asid);
Omer Shpigelmanf19040c2020-12-09 13:34:11 +02002128 args.unmap.device_virt_addr = hnode->vaddr;
2129 unmap_device_va(ctx, &args, true);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002130 }
2131
Ohad Sharabicb6ef0e2020-11-26 09:39:26 +02002132 mutex_lock(&ctx->mmu_lock);
2133
Omer Shpigelmanbea84c42019-11-14 18:23:58 +00002134 /* invalidate the cache once after the unmapping loop */
2135 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
2136 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_PHYS_PACK);
2137
Ohad Sharabicb6ef0e2020-11-26 09:39:26 +02002138 mutex_unlock(&ctx->mmu_lock);
2139
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002140 spin_lock(&vm->idr_lock);
2141 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_list, i)
2142 if (phys_pg_list->asid == ctx->asid) {
2143 dev_dbg(hdev->dev,
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03002144 "page list 0x%px of asid %d is still alive\n",
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002145 phys_pg_list, ctx->asid);
Tomer Tayarc8113752019-08-04 07:03:41 +00002146 atomic64_sub(phys_pg_list->total_size,
2147 &hdev->dram_used_mem);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002148 free_phys_pg_pack(hdev, phys_pg_list);
2149 idr_remove(&vm->phys_pg_pack_handles, i);
2150 }
2151 spin_unlock(&vm->idr_lock);
2152
Ofir Bitton784b9162020-10-22 11:05:55 +03002153 va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM]);
Ofir Bitton8e718f22020-11-26 13:01:11 +02002154 va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST]);
2155
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002156 if (hdev->pmmu_huge_range)
Ofir Bitton784b9162020-10-22 11:05:55 +03002157 va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002158
2159 mutex_destroy(&ctx->mem_hash_lock);
2160 hl_mmu_ctx_fini(ctx);
Oded Gabbay3e622992020-10-18 15:32:23 +03002161
2162 /* In this case we need to clear the global accounting of DRAM usage
2163 * because the user notifies us on allocations. If the user is no more,
2164 * all DRAM is available
2165 */
Ofir Bitton8e39e752020-11-12 11:03:32 +02002166 if (ctx->asid != HL_KERNEL_ASID_ID &&
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002167 !hdev->asic_prop.dram_supports_virtual_memory)
2168 atomic64_set(&hdev->dram_used_mem, 0);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002169}
2170
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002171/**
2172 * hl_vm_init() - initialize virtual memory module.
2173 * @hdev: pointer to the habanalabs device structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002174 *
2175 * This function initializes the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002176 * - MMU module.
2177 * - DRAM physical pages pool of 2MB.
2178 * - Idr for device memory allocation handles.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002179 */
2180int hl_vm_init(struct hl_device *hdev)
2181{
2182 struct asic_fixed_properties *prop = &hdev->asic_prop;
2183 struct hl_vm *vm = &hdev->vm;
2184 int rc;
2185
Moti Haimovskib19dc672020-11-18 20:15:29 +02002186 if (is_power_of_2(prop->dram_page_size))
2187 vm->dram_pg_pool =
2188 gen_pool_create(__ffs(prop->dram_page_size), -1);
2189 else
2190 vm->dram_pg_pool =
2191 gen_pool_create(__ffs(DRAM_POOL_PAGE_SIZE), -1);
2192
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002193 if (!vm->dram_pg_pool) {
2194 dev_err(hdev->dev, "Failed to create dram page pool\n");
Oded Gabbay37d68ce2019-05-29 14:43:04 +03002195 return -ENOMEM;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002196 }
2197
2198 kref_init(&vm->dram_pg_pool_refcount);
2199
2200 rc = gen_pool_add(vm->dram_pg_pool, prop->dram_user_base_address,
2201 prop->dram_end_address - prop->dram_user_base_address,
2202 -1);
2203
2204 if (rc) {
2205 dev_err(hdev->dev,
2206 "Failed to add memory to dram page pool %d\n", rc);
2207 goto pool_add_err;
2208 }
2209
2210 spin_lock_init(&vm->idr_lock);
2211 idr_init(&vm->phys_pg_pack_handles);
2212
2213 atomic64_set(&hdev->dram_used_mem, 0);
2214
2215 vm->init_done = true;
2216
2217 return 0;
2218
2219pool_add_err:
2220 gen_pool_destroy(vm->dram_pg_pool);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002221
2222 return rc;
2223}
2224
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002225/**
2226 * hl_vm_fini() - virtual memory module teardown.
2227 * @hdev: pointer to the habanalabs device structure.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002228 *
2229 * This function perform teardown to the following:
Omer Shpigelman3b762f52020-12-09 13:28:46 +02002230 * - Idr for device memory allocation handles.
2231 * - DRAM physical pages pool of 2MB.
2232 * - MMU module.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002233 */
2234void hl_vm_fini(struct hl_device *hdev)
2235{
2236 struct hl_vm *vm = &hdev->vm;
2237
2238 if (!vm->init_done)
2239 return;
2240
2241 /*
2242 * At this point all the contexts should be freed and hence no DRAM
2243 * memory should be in use. Hence the DRAM pool should be freed here.
2244 */
2245 if (kref_put(&vm->dram_pg_pool_refcount, dram_pg_pool_do_release) != 1)
2246 dev_warn(hdev->dev, "dram_pg_pool was not destroyed on %s\n",
2247 __func__);
2248
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002249 vm->init_done = false;
2250}
Sagiv Ozeria4371c12021-02-23 11:01:08 +02002251
2252/**
2253 * hl_hw_block_mem_init() - HW block memory initialization.
2254 * @ctx: pointer to the habanalabs context structure.
2255 *
2256 * This function initializes the HW block virtual mapped addresses list and
2257 * it's lock.
2258 */
2259void hl_hw_block_mem_init(struct hl_ctx *ctx)
2260{
2261 mutex_init(&ctx->hw_block_list_lock);
2262 INIT_LIST_HEAD(&ctx->hw_block_mem_list);
2263}
2264
2265/**
2266 * hl_hw_block_mem_fini() - HW block memory teardown.
2267 * @ctx: pointer to the habanalabs context structure.
2268 *
2269 * This function clears the HW block virtual mapped addresses list and destroys
2270 * it's lock.
2271 */
2272void hl_hw_block_mem_fini(struct hl_ctx *ctx)
2273{
2274 struct hl_vm_hw_block_list_node *lnode, *tmp;
2275
2276 if (!list_empty(&ctx->hw_block_mem_list))
2277 dev_crit(ctx->hdev->dev, "HW block mem list isn't empty\n");
2278
2279 list_for_each_entry_safe(lnode, tmp, &ctx->hw_block_mem_list, node) {
2280 list_del(&lnode->node);
2281 kfree(lnode);
2282 }
2283
2284 mutex_destroy(&ctx->hw_block_list_lock);
2285}