Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2016-2019 HabanaLabs, Ltd. |
| 5 | * All Rights Reserved. |
| 6 | */ |
| 7 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 8 | #include <uapi/misc/habanalabs.h> |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 9 | #include "habanalabs.h" |
Greg Kroah-Hartman | 7b16a15 | 2020-07-28 19:18:51 +0200 | [diff] [blame] | 10 | #include "../include/hw_ip/mmu/mmu_general.h" |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 11 | |
| 12 | #include <linux/uaccess.h> |
| 13 | #include <linux/slab.h> |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 14 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 15 | #define HL_MMU_DEBUG 0 |
| 16 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 17 | /* use small pages for supporting non-pow2 (32M/40M/48M) DRAM phys page sizes */ |
| 18 | #define DRAM_POOL_PAGE_SIZE SZ_8M |
| 19 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 20 | /* |
| 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 44 | * 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 48 | * |
| 49 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 50 | * - Allocate the requested size rounded up to 'dram_page_size' pages. |
| 51 | * - Return unique handle for later map/unmap/free. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 52 | */ |
| 53 | static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args, |
| 54 | u32 *ret_handle) |
| 55 | { |
| 56 | struct hl_device *hdev = ctx->hdev; |
| 57 | struct hl_vm *vm = &hdev->vm; |
| 58 | struct hl_vm_phys_pg_pack *phys_pg_pack; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 59 | u64 paddr = 0, total_size, num_pgs, i; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 60 | u32 num_curr_pgs, page_size; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 61 | int handle, rc; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 62 | bool contiguous; |
| 63 | |
| 64 | num_curr_pgs = 0; |
| 65 | page_size = hdev->asic_prop.dram_page_size; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 66 | num_pgs = DIV_ROUND_UP_ULL(args->alloc.mem_size, page_size); |
| 67 | total_size = num_pgs * page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 68 | |
Ofir Bitton | 0839152 | 2020-08-11 08:57:45 +0300 | [diff] [blame] | 69 | if (!total_size) { |
| 70 | dev_err(hdev->dev, "Cannot allocate 0 bytes\n"); |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 74 | 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 Gabbay | fc6121e | 2020-09-23 14:07:32 +0300 | [diff] [blame] | 80 | "failed to allocate %llu contiguous pages with total size of %llu\n", |
| 81 | num_pgs, total_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 82 | 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 Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 100 | phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL); |
Ofir Bitton | 0839152 | 2020-08-11 08:57:45 +0300 | [diff] [blame] | 101 | if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 102 | 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 Gabbay | cab8e3e | 2019-03-27 09:44:28 +0200 | [diff] [blame] | 116 | "Failed to allocate device memory (out of memory)\n"); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 117 | 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 Bitton | d5eb837 | 2021-02-14 15:35:56 +0200 | [diff] [blame] | 127 | GFP_KERNEL); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 128 | 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 | |
| 148 | idr_err: |
| 149 | page_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 Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 155 | kvfree(phys_pg_pack->pages); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 156 | pages_arr_err: |
| 157 | kfree(phys_pg_pack); |
| 158 | pages_pack_err: |
| 159 | if (contiguous) |
| 160 | gen_pool_free(vm->dram_pg_pool, paddr, total_size); |
| 161 | |
| 162 | return rc; |
| 163 | } |
| 164 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 165 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 171 | * |
| 172 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 173 | * - 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 176 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 177 | static int dma_map_host_va(struct hl_device *hdev, u64 addr, u64 size, |
| 178 | struct hl_userptr **p_userptr) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 179 | { |
| 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 Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 189 | rc = hl_pin_host_memory(hdev, addr, size, userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 190 | 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 | |
| 210 | dma_map_err: |
| 211 | hl_unpin_host_memory(hdev, userptr); |
| 212 | pin_err: |
| 213 | kfree(userptr); |
| 214 | userptr_err: |
| 215 | |
| 216 | return rc; |
| 217 | } |
| 218 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 219 | /** |
| 220 | * dma_unmap_host_va() - DMA unmapping of the given host virtual address. |
| 221 | * @hdev: habanalabs device structure. |
| 222 | * @userptr: userptr to free. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 223 | * |
| 224 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 225 | * - Unpins the physical pages. |
| 226 | * - Frees the userptr structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 227 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 228 | static void dma_unmap_host_va(struct hl_device *hdev, |
| 229 | struct hl_userptr *userptr) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 230 | { |
| 231 | hl_unpin_host_memory(hdev, userptr); |
| 232 | kfree(userptr); |
| 233 | } |
| 234 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 235 | /** |
| 236 | * dram_pg_pool_do_release() - free DRAM pages pool |
| 237 | * @ref: pointer to reference object. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 238 | * |
| 239 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 240 | * - Frees the idr structure of physical pages handles. |
| 241 | * - Frees the generic pool of DRAM physical pages. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 242 | */ |
| 243 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 256 | /** |
| 257 | * free_phys_pg_pack() - free physical page pack. |
| 258 | * @hdev: habanalabs device structure. |
| 259 | * @phys_pg_pack: physical page pack to free. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 260 | * |
| 261 | * This function does the following: |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 262 | * - 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 268 | * - Free the hl_vm_phys_pg_pack structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 269 | */ |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 270 | static int free_phys_pg_pack(struct hl_device *hdev, |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 271 | struct hl_vm_phys_pg_pack *phys_pg_pack) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 272 | { |
| 273 | struct hl_vm *vm = &hdev->vm; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 274 | u64 i; |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 275 | int rc = 0; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 276 | |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 277 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 284 | phys_pg_pack->total_size); |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 285 | if (rc) |
| 286 | dev_err(hdev->dev, |
| 287 | "Failed to scrub contiguous device memory\n"); |
| 288 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 289 | |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 290 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 301 | phys_pg_pack->pages[i], |
| 302 | phys_pg_pack->page_size); |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 303 | if (rc) |
| 304 | dev_err(hdev->dev, |
| 305 | "Failed to scrub device memory\n"); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 306 | } |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 307 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 315 | if (rc && !hdev->disabled) |
| 316 | hl_device_reset(hdev, HL_RESET_HARD); |
| 317 | |
| 318 | end: |
Omer Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 319 | kvfree(phys_pg_pack->pages); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 320 | kfree(phys_pg_pack); |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 321 | |
| 322 | return rc; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 325 | /** |
| 326 | * free_device_memory() - free device memory. |
| 327 | * @ctx: pointer to the context structure. |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 328 | * @args: host parameters containing the requested size. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 329 | * |
| 330 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 331 | * - Free the device memory related to the given handle. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 332 | */ |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 333 | static int free_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 334 | { |
| 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 Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 338 | u32 handle = args->free.handle; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 339 | |
| 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 Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 361 | return free_phys_pg_pack(hdev, phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 362 | } 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 373 | /** |
| 374 | * clear_va_list_locked() - free virtual addresses list. |
| 375 | * @hdev: habanalabs device structure. |
| 376 | * @va_list: list of virtual addresses to free. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 377 | * |
| 378 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 379 | * - Iterate over the list and free each virtual addresses block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 380 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 381 | * This function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 382 | */ |
| 383 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 394 | /** |
| 395 | * print_va_list_locked() - print virtual addresses list. |
| 396 | * @hdev: habanalabs device structure. |
| 397 | * @va_list: list of virtual addresses to print. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 398 | * |
| 399 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 400 | * - Iterate over the list and print each virtual addresses block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 401 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 402 | * This function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 403 | */ |
| 404 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 419 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 424 | * |
| 425 | * This function does the following: |
| 426 | * - Merge the given blocks with the adjacent blocks if their virtual ranges |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 427 | * create a contiguous virtual range. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 428 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 429 | * This Function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 430 | */ |
| 431 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 454 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 460 | * |
| 461 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 462 | * - Add the given block to the virtual blocks list and merge with other blocks |
| 463 | * if a contiguous virtual block can be created. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 464 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 465 | * This Function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 466 | */ |
| 467 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 509 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 515 | * |
| 516 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 517 | * - Takes the list lock and calls add_va_block_locked. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 518 | */ |
| 519 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 531 | /** |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 532 | * is_hint_crossing_range() - check if hint address crossing specified reserved |
| 533 | * range. |
| 534 | */ |
| 535 | static 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 Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 559 | * get_va_block() - get a virtual block for the given size and alignment. |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 560 | * |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 561 | * @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 kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 566 | * @range_type: va range type (host, dram) |
Yuri Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 567 | * @flags: additional memory flags, currently only uses HL_MEM_FORCE_HINT |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 568 | * |
| 569 | * This function does the following: |
| 570 | * - Iterate on the virtual block list to find a suitable virtual block for the |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 571 | * given size, hint address and alignment. |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 572 | * - Reserve the requested block and update the list. |
| 573 | * - Return the start address of the virtual block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 574 | */ |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 575 | static u64 get_va_block(struct hl_device *hdev, |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 576 | struct hl_va_range *va_range, |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 577 | u64 size, u64 hint_addr, u32 va_block_align, |
Yuri Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 578 | enum hl_va_range_type range_type, |
| 579 | u32 flags) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 580 | { |
| 581 | struct hl_vm_va_block *va_block, *new_va_block = NULL; |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 582 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 583 | u64 tmp_hint_addr, valid_start, valid_size, prev_start, prev_end, |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 584 | align_mask, reserved_valid_start = 0, reserved_valid_size = 0, |
| 585 | dram_hint_mask = prop->dram_hints_align_mask; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 586 | bool add_prev = false; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 587 | bool is_align_pow_2 = is_power_of_2(va_range->page_size); |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 588 | bool is_hint_dram_addr = hl_is_dram_va(hdev, hint_addr); |
Yuri Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 589 | bool force_hint = flags & HL_MEM_FORCE_HINT; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 590 | |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 591 | 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 Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 601 | |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 602 | tmp_hint_addr = hint_addr & ~dram_hint_mask; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 603 | |
| 604 | /* Check if we need to ignore hint address */ |
| 605 | if ((is_align_pow_2 && (hint_addr & (va_block_align - 1))) || |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 606 | (!is_align_pow_2 && is_hint_dram_addr && |
| 607 | do_div(tmp_hint_addr, va_range->page_size))) { |
Oded Gabbay | b8e785c | 2021-04-26 18:32:25 +0300 | [diff] [blame] | 608 | |
Yuri Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 609 | 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 Gabbay | b8e785c | 2021-04-26 18:32:25 +0300 | [diff] [blame] | 618 | dev_dbg(hdev->dev, |
| 619 | "Hint address 0x%llx will be ignored because it is not aligned\n", |
| 620 | hint_addr); |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 621 | hint_addr = 0; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 622 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 623 | |
| 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 kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 629 | /* Calc the first possible aligned addr */ |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 630 | valid_start = va_block->start; |
| 631 | |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 632 | if (is_align_pow_2 && (valid_start & (va_block_align - 1))) { |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 633 | valid_start &= align_mask; |
| 634 | valid_start += va_block_align; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 635 | if (valid_start > va_block->end) |
| 636 | continue; |
| 637 | } |
| 638 | |
| 639 | valid_size = va_block->end - valid_start; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 640 | if (valid_size < size) |
| 641 | continue; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 642 | |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 643 | /* |
| 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 kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 653 | /* Pick the minimal length block which has the required size */ |
| 654 | if (!new_va_block || (valid_size < reserved_valid_size)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 655 | new_va_block = va_block; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 656 | reserved_valid_start = valid_start; |
| 657 | reserved_valid_size = valid_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | if (hint_addr && hint_addr >= valid_start && |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 661 | (hint_addr + size) <= va_block->end) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 662 | new_va_block = va_block; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 663 | reserved_valid_start = hint_addr; |
| 664 | reserved_valid_size = valid_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 665 | break; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (!new_va_block) { |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 670 | dev_err(hdev->dev, "no available va block for size %llu\n", |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 671 | size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 672 | goto out; |
| 673 | } |
| 674 | |
Yuri Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 675 | 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 kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame] | 686 | /* |
| 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 Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 690 | if (reserved_valid_start > new_va_block->start) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 691 | prev_start = new_va_block->start; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 692 | prev_end = reserved_valid_start - 1; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 693 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 694 | new_va_block->start = reserved_valid_start; |
| 695 | new_va_block->size = reserved_valid_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 696 | |
| 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); |
| 713 | out: |
| 714 | mutex_unlock(&va_range->lock); |
| 715 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 716 | return reserved_valid_start; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 720 | * 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 Bitton | 412c41f | 2020-11-04 15:18:55 +0200 | [diff] [blame] | 725 | * @alignment: required alignment in bytes of the virtual block start address, |
| 726 | * 0 means no alignment. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 727 | * |
| 728 | * This function does the following: |
| 729 | * - Iterate on the virtual block list to find a suitable virtual block for the |
Ofir Bitton | 412c41f | 2020-11-04 15:18:55 +0200 | [diff] [blame] | 730 | * given size and alignment. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 731 | * - Reserve the requested block and update the list. |
| 732 | * - Return the start address of the virtual block. |
| 733 | */ |
| 734 | u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx, |
Ofir Bitton | 412c41f | 2020-11-04 15:18:55 +0200 | [diff] [blame] | 735 | enum hl_va_range_type type, u32 size, u32 alignment) |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 736 | { |
| 737 | return get_va_block(hdev, ctx->va_range[type], size, 0, |
Yuri Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 738 | max(alignment, ctx->va_range[type]->page_size), |
| 739 | type, 0); |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /** |
| 743 | * hl_get_va_range_type() - get va_range type for the given address and size. |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 744 | * @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 Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 747 | * |
| 748 | * Return: true if the area is inside a valid range, false otherwise. |
| 749 | */ |
| 750 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 767 | /** |
| 768 | * hl_unreserve_va_block() - wrapper for add_va_block to unreserve a va block. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 769 | * @hdev: pointer to the habanalabs device structure |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 770 | * @ctx: pointer to the context structure. |
| 771 | * @start: start virtual address. |
| 772 | * @end: end virtual address. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 773 | * |
| 774 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 775 | * - Takes the list lock and calls add_va_block_locked. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 776 | */ |
| 777 | int 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 800 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 804 | * |
| 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 | */ |
| 809 | static 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 817 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 823 | * |
| 824 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 825 | * - Pin the physical pages related to the given virtual block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 826 | * - Create a physical page pack from the physical pages related to the given |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 827 | * virtual block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 828 | */ |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 829 | static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx, |
| 830 | struct hl_userptr *userptr, |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 831 | struct hl_vm_phys_pg_pack **pphys_pg_pack) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 832 | { |
| 833 | struct hl_vm_phys_pg_pack *phys_pg_pack; |
| 834 | struct scatterlist *sg; |
| 835 | dma_addr_t dma_addr; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 836 | u64 page_mask, total_npages; |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 837 | u32 npages, page_size = PAGE_SIZE, |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 838 | huge_page_size = ctx->hdev->asic_prop.pmmu_huge.page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 839 | bool first = true, is_huge_page_opt = true; |
| 840 | int rc, i, j; |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 841 | u32 pgs_in_huge_page = huge_page_size >> __ffs(page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 842 | |
| 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 Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 849 | phys_pg_pack->asid = ctx->asid; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 850 | 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 Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 864 | if ((npages % pgs_in_huge_page) || |
| 865 | (dma_addr & (huge_page_size - 1))) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 866 | is_huge_page_opt = false; |
| 867 | } |
| 868 | |
| 869 | if (is_huge_page_opt) { |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 870 | page_size = huge_page_size; |
| 871 | do_div(total_npages, pgs_in_huge_page); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | page_mask = ~(((u64) page_size) - 1); |
| 875 | |
Omer Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 876 | phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64), |
| 877 | GFP_KERNEL); |
Ofir Bitton | 0839152 | 2020-08-11 08:57:45 +0300 | [diff] [blame] | 878 | if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 879 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 888 | 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 Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 903 | npages -= pgs_in_huge_page; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 904 | else |
| 905 | npages--; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | *pphys_pg_pack = phys_pg_pack; |
| 910 | |
| 911 | return 0; |
| 912 | |
| 913 | page_pack_arr_mem_err: |
| 914 | kfree(phys_pg_pack); |
| 915 | |
| 916 | return rc; |
| 917 | } |
| 918 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 919 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 924 | * |
| 925 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 926 | * - 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 929 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 930 | static int map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr, |
| 931 | struct hl_vm_phys_pg_pack *phys_pg_pack) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 932 | { |
| 933 | struct hl_device *hdev = ctx->hdev; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 934 | u64 next_vaddr = vaddr, paddr, mapped_pg_cnt = 0, i; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 935 | u32 page_size = phys_pg_pack->page_size; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 936 | int rc = 0; |
farah kassabri | 2f6274e | 2021-03-11 11:24:57 +0200 | [diff] [blame] | 937 | bool is_host_addr; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 938 | |
| 939 | for (i = 0 ; i < phys_pg_pack->npages ; i++) { |
| 940 | paddr = phys_pg_pack->pages[i]; |
| 941 | |
Ofir Bitton | 5c05487 | 2020-10-22 15:13:10 +0300 | [diff] [blame] | 942 | rc = hl_mmu_map_page(ctx, next_vaddr, paddr, page_size, |
Pawel Piskorski | 7fc40bc | 2019-12-06 17:32:38 +0200 | [diff] [blame] | 943 | (i + 1) == phys_pg_pack->npages); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 944 | if (rc) { |
| 945 | dev_err(hdev->dev, |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 946 | "map failed for handle %u, npages: %llu, mapped: %llu", |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 947 | 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 | |
| 958 | err: |
farah kassabri | 2f6274e | 2021-03-11 11:24:57 +0200 | [diff] [blame] | 959 | is_host_addr = !hl_is_dram_va(hdev, vaddr); |
| 960 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 961 | next_vaddr = vaddr; |
| 962 | for (i = 0 ; i < mapped_pg_cnt ; i++) { |
Ofir Bitton | 5c05487 | 2020-10-22 15:13:10 +0300 | [diff] [blame] | 963 | if (hl_mmu_unmap_page(ctx, next_vaddr, page_size, |
Pawel Piskorski | 7fc40bc | 2019-12-06 17:32:38 +0200 | [diff] [blame] | 964 | (i + 1) == mapped_pg_cnt)) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 965 | 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 kassabri | 2f6274e | 2021-03-11 11:24:57 +0200 | [diff] [blame] | 971 | |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | return rc; |
| 985 | } |
| 986 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 987 | /** |
| 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 Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 992 | */ |
| 993 | static 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 Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 998 | bool is_host_addr; |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 999 | u32 page_size; |
| 1000 | |
Oded Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 1001 | is_host_addr = !hl_is_dram_va(hdev, vaddr); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1002 | 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 Bitton | 5c05487 | 2020-10-22 15:13:10 +0300 | [diff] [blame] | 1006 | if (hl_mmu_unmap_page(ctx, next_vaddr, page_size, |
Pawel Piskorski | 7fc40bc | 2019-12-06 17:32:38 +0200 | [diff] [blame] | 1007 | (i + 1) == phys_pg_pack->npages)) |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1008 | 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 Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 1014 | * |
farah kassabri | 2f6274e | 2021-03-11 11:24:57 +0200 | [diff] [blame] | 1015 | * 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 Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1018 | */ |
Oded Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 1019 | if (hdev->pldm || (is_host_addr && (i & 0x7FFF) == 0)) |
| 1020 | usleep_range(50, 200); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1024 | static int get_paddr_from_handle(struct hl_ctx *ctx, struct hl_mem_in *args, |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1025 | u64 *paddr) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1026 | { |
| 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1048 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1053 | * |
| 1054 | * This function does the following: |
| 1055 | * - If given a physical device memory handle, map to a device virtual block |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1056 | * and return the start address of this block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1057 | * - 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1059 | * this block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1060 | */ |
| 1061 | static 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 Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1069 | struct hl_va_range *va_range; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1070 | enum vm_type_t *vm_type; |
| 1071 | u64 ret_vaddr, hint_addr; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1072 | u32 handle = 0, va_block_align; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1073 | int rc; |
| 1074 | bool is_userptr = args->flags & HL_MEM_USERPTR; |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 1075 | enum hl_va_range_type va_range_type = 0; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1076 | |
| 1077 | /* Assume failure */ |
| 1078 | *device_addr = 0; |
| 1079 | |
| 1080 | if (is_userptr) { |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1081 | u64 addr = args->map_host.host_virt_addr, |
| 1082 | size = args->map_host.mem_size; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1083 | u32 page_size = hdev->asic_prop.pmmu.page_size, |
| 1084 | huge_page_size = hdev->asic_prop.pmmu_huge.page_size; |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1085 | |
| 1086 | rc = dma_map_host_va(hdev, addr, size, &userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1087 | if (rc) { |
| 1088 | dev_err(hdev->dev, "failed to get userptr from va\n"); |
| 1089 | return rc; |
| 1090 | } |
| 1091 | |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 1092 | rc = init_phys_pg_pack_from_userptr(ctx, userptr, |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1093 | &phys_pg_pack); |
| 1094 | if (rc) { |
| 1095 | dev_err(hdev->dev, |
| 1096 | "unable to init page pack for vaddr 0x%llx\n", |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1097 | addr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1098 | goto init_page_pack_err; |
| 1099 | } |
| 1100 | |
| 1101 | vm_type = (enum vm_type_t *) userptr; |
| 1102 | hint_addr = args->map_host.hint_addr; |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1103 | handle = phys_pg_pack->handle; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1104 | |
| 1105 | /* get required alignment */ |
| 1106 | if (phys_pg_pack->page_size == page_size) { |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1107 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST]; |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 1108 | va_range_type = HL_VA_RANGE_TYPE_HOST; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1109 | /* |
| 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 Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1122 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]; |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 1123 | va_range_type = HL_VA_RANGE_TYPE_HOST_HUGE; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1124 | va_block_align = huge_page_size; |
| 1125 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1126 | } 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 Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1146 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1147 | /* DRAM VA alignment is the same as the MMU page size */ |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1148 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM]; |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 1149 | va_range_type = HL_VA_RANGE_TYPE_DRAM; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1150 | va_block_align = hdev->asic_prop.dmmu.page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1151 | } |
| 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 Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 1172 | 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 Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1187 | ret_vaddr = get_va_block(hdev, va_range, phys_pg_pack->total_size, |
farah kassabri | 1ae32b9 | 2021-01-31 18:56:03 +0200 | [diff] [blame] | 1188 | hint_addr, va_block_align, |
Yuri Nudelman | 486e197 | 2021-06-03 17:51:58 +0300 | [diff] [blame] | 1189 | va_range_type, args->flags); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1190 | 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 Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1199 | rc = map_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1200 | 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 Mizrahi | 08c03a1 | 2021-04-08 15:30:59 +0300 | [diff] [blame] | 1207 | rc = hdev->asic_funcs->mmu_invalidate_cache_range(hdev, false, |
| 1208 | *vm_type, ctx->asid, ret_vaddr, phys_pg_pack->total_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1209 | |
| 1210 | mutex_unlock(&ctx->mmu_lock); |
| 1211 | |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1212 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1219 | 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 Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 1231 | rc = free_phys_pg_pack(hdev, phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1232 | |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 1233 | return rc; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1234 | |
| 1235 | map_err: |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1236 | if (add_va_block(hdev, va_range, ret_vaddr, |
| 1237 | ret_vaddr + phys_pg_pack->total_size - 1)) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1238 | dev_warn(hdev->dev, |
| 1239 | "release va block failed for handle 0x%x, vaddr: 0x%llx\n", |
| 1240 | handle, ret_vaddr); |
| 1241 | |
| 1242 | va_block_err: |
| 1243 | kfree(hnode); |
| 1244 | hnode_err: |
| 1245 | shared_err: |
| 1246 | atomic_dec(&phys_pg_pack->mapping_cnt); |
| 1247 | if (is_userptr) |
| 1248 | free_phys_pg_pack(hdev, phys_pg_pack); |
| 1249 | init_page_pack_err: |
| 1250 | if (is_userptr) |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1251 | dma_unmap_host_va(hdev, userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1252 | |
| 1253 | return rc; |
| 1254 | } |
| 1255 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1256 | /** |
| 1257 | * unmap_device_va() - unmap the given device virtual address. |
| 1258 | * @ctx: pointer to the context structure. |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1259 | * @args: host parameters with device virtual address to unmap. |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1260 | * @ctx_free: true if in context free flow, false otherwise. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1261 | * |
| 1262 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1263 | * - unmap the physical pages related to the given virtual address. |
| 1264 | * - return the device virtual block to the virtual block list. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1265 | */ |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1266 | static int unmap_device_va(struct hl_ctx *ctx, struct hl_mem_in *args, |
| 1267 | bool ctx_free) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1268 | { |
| 1269 | struct hl_device *hdev = ctx->hdev; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1270 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1271 | 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 Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1274 | struct hl_va_range *va_range; |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1275 | u64 vaddr = args->unmap.device_virt_addr; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1276 | enum vm_type_t *vm_type; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1277 | bool is_userptr; |
Tomer Tayar | c68f1ba | 2020-06-01 09:56:47 +0300 | [diff] [blame] | 1278 | int rc = 0; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1279 | |
| 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 Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 1302 | rc = init_phys_pg_pack_from_userptr(ctx, userptr, |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1303 | &phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1304 | 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 Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1310 | |
| 1311 | if (phys_pg_pack->page_size == |
| 1312 | hdev->asic_prop.pmmu.page_size) |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1313 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST]; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1314 | else |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1315 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1316 | } else if (*vm_type == VM_TYPE_PHYS_PACK) { |
| 1317 | is_userptr = false; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1318 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM]; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1319 | 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 Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1334 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1341 | |
| 1342 | mutex_lock(&ctx->mmu_lock); |
| 1343 | |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1344 | unmap_phys_pg_pack(ctx, vaddr, phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1345 | |
Omer Shpigelman | bea84c4 | 2019-11-14 18:23:58 +0000 | [diff] [blame] | 1346 | /* |
| 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 Mizrahi | 08c03a1 | 2021-04-08 15:30:59 +0300 | [diff] [blame] | 1352 | rc = hdev->asic_funcs->mmu_invalidate_cache_range(hdev, true, |
| 1353 | *vm_type, ctx->asid, vaddr, |
| 1354 | phys_pg_pack->total_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1355 | |
| 1356 | mutex_unlock(&ctx->mmu_lock); |
| 1357 | |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1358 | /* |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1359 | * 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 Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1363 | */ |
| 1364 | if (!ctx_free) { |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1365 | int tmp_rc; |
| 1366 | |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1367 | if (rc) |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1368 | 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 Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1375 | dev_warn(hdev->dev, |
| 1376 | "add va block failed for vaddr: 0x%llx\n", |
| 1377 | vaddr); |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1378 | if (!rc) |
| 1379 | rc = tmp_rc; |
| 1380 | } |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1381 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1382 | |
| 1383 | atomic_dec(&phys_pg_pack->mapping_cnt); |
| 1384 | kfree(hnode); |
| 1385 | |
| 1386 | if (is_userptr) { |
Bharat Jauhari | d4b1e5d | 2021-03-18 12:11:19 +0200 | [diff] [blame] | 1387 | rc = free_phys_pg_pack(hdev, phys_pg_pack); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1388 | dma_unmap_host_va(hdev, userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1391 | return rc; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1392 | |
| 1393 | mapping_cnt_err: |
| 1394 | if (is_userptr) |
| 1395 | free_phys_pg_pack(hdev, phys_pg_pack); |
| 1396 | vm_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 Gabbay | 6df50d2 | 2021-02-05 16:04:34 +0200 | [diff] [blame] | 1404 | static int map_block(struct hl_device *hdev, u64 address, u64 *handle, |
| 1405 | u32 *size) |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1406 | { |
| 1407 | u32 block_id = 0; |
| 1408 | int rc; |
| 1409 | |
Oded Gabbay | 6df50d2 | 2021-02-05 16:04:34 +0200 | [diff] [blame] | 1410 | rc = hdev->asic_funcs->get_hw_block_id(hdev, address, size, &block_id); |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1411 | |
| 1412 | *handle = block_id | HL_MMAP_TYPE_BLOCK; |
| 1413 | *handle <<= PAGE_SHIFT; |
| 1414 | |
| 1415 | return rc; |
| 1416 | } |
| 1417 | |
| 1418 | static void hw_block_vm_close(struct vm_area_struct *vma) |
| 1419 | { |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1420 | 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 Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1423 | |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1424 | mutex_lock(&ctx->hw_block_list_lock); |
| 1425 | list_del(&lnode->node); |
| 1426 | mutex_unlock(&ctx->hw_block_list_lock); |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1427 | hl_ctx_put(ctx); |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1428 | kfree(lnode); |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1429 | vma->vm_private_data = NULL; |
| 1430 | } |
| 1431 | |
| 1432 | static 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 | */ |
| 1444 | int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma) |
| 1445 | { |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1446 | struct hl_vm_hw_block_list_node *lnode; |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1447 | struct hl_device *hdev = hpriv->hdev; |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1448 | struct hl_ctx *ctx = hpriv->ctx; |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1449 | 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 Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1461 | if (!access_ok((void __user *) (uintptr_t) vma->vm_start, block_size)) { |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1462 | dev_err(hdev->dev, |
| 1463 | "user pointer is invalid - 0x%lx\n", |
| 1464 | vma->vm_start); |
| 1465 | |
| 1466 | return -EINVAL; |
| 1467 | } |
| 1468 | |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1469 | lnode = kzalloc(sizeof(*lnode), GFP_KERNEL); |
| 1470 | if (!lnode) |
| 1471 | return -ENOMEM; |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1472 | |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1473 | vma->vm_ops = &hw_block_vm_ops; |
| 1474 | vma->vm_private_data = lnode; |
| 1475 | |
| 1476 | hl_ctx_get(hdev, ctx); |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1477 | |
| 1478 | rc = hdev->asic_funcs->hw_block_mmap(hdev, vma, block_id, block_size); |
| 1479 | if (rc) { |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1480 | hl_ctx_put(ctx); |
| 1481 | kfree(lnode); |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1482 | return rc; |
| 1483 | } |
| 1484 | |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 1485 | 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 Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1494 | vma->vm_pgoff = block_id; |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1499 | static 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 Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1503 | u64 block_handle, device_addr = 0; |
Oded Gabbay | 6df50d2 | 2021-02-05 16:04:34 +0200 | [diff] [blame] | 1504 | u32 handle = 0, block_size; |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1505 | 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 Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1527 | rc = free_device_memory(ctx, &args->in); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1528 | 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1536 | &device_addr); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1537 | } |
| 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 Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1547 | case HL_MEM_OP_MAP_BLOCK: |
| 1548 | rc = map_block(hdev, args->in.map_block.block_addr, |
Oded Gabbay | 6df50d2 | 2021-02-05 16:04:34 +0200 | [diff] [blame] | 1549 | &block_handle, &block_size); |
| 1550 | args->out.block_handle = block_handle; |
| 1551 | args->out.block_size = block_size; |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1552 | break; |
| 1553 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1554 | default: |
| 1555 | dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n"); |
| 1556 | rc = -ENOTTY; |
| 1557 | break; |
| 1558 | } |
| 1559 | |
| 1560 | out: |
| 1561 | return rc; |
| 1562 | } |
| 1563 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1564 | int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data) |
| 1565 | { |
Ofir Bitton | 66a7640 | 2020-10-05 14:40:10 +0300 | [diff] [blame] | 1566 | enum hl_device_status status; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1567 | union hl_mem_args *args = data; |
| 1568 | struct hl_device *hdev = hpriv->hdev; |
| 1569 | struct hl_ctx *ctx = hpriv->ctx; |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1570 | u64 block_handle, device_addr = 0; |
Oded Gabbay | 6df50d2 | 2021-02-05 16:04:34 +0200 | [diff] [blame] | 1571 | u32 handle = 0, block_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1572 | int rc; |
| 1573 | |
Ofir Bitton | 66a7640 | 2020-10-05 14:40:10 +0300 | [diff] [blame] | 1574 | if (!hl_device_operational(hdev, &status)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1575 | dev_warn_ratelimited(hdev->dev, |
Oded Gabbay | 3f5398c | 2019-04-06 15:41:35 +0300 | [diff] [blame] | 1576 | "Device is %s. Can't execute MEMORY IOCTL\n", |
Ofir Bitton | 66a7640 | 2020-10-05 14:40:10 +0300 | [diff] [blame] | 1577 | hdev->status[status]); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1578 | return -EBUSY; |
| 1579 | } |
| 1580 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1581 | if (!hdev->mmu_enable) |
| 1582 | return mem_ioctl_no_mmu(hpriv, args); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1583 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1584 | switch (args->in.op) { |
| 1585 | case HL_MEM_OP_ALLOC: |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1586 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1591 | } |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1592 | |
| 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 Gabbay | 7f070c9 | 2020-11-09 09:48:31 +0200 | [diff] [blame] | 1601 | if (!hdev->asic_prop.dram_supports_virtual_memory) { |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1602 | 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 Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1615 | 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 Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1622 | /* 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 Gabbay | 7f070c9 | 2020-11-09 09:48:31 +0200 | [diff] [blame] | 1630 | if (!hdev->asic_prop.dram_supports_virtual_memory) { |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1631 | 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 Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1642 | rc = free_device_memory(ctx, &args->in); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1643 | 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 Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1653 | rc = unmap_device_va(ctx, &args->in, false); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1654 | break; |
| 1655 | |
Ofir Bitton | d00697f | 2021-01-05 12:55:06 +0200 | [diff] [blame] | 1656 | case HL_MEM_OP_MAP_BLOCK: |
| 1657 | rc = map_block(hdev, args->in.map_block.block_addr, |
Oded Gabbay | 6df50d2 | 2021-02-05 16:04:34 +0200 | [diff] [blame] | 1658 | &block_handle, &block_size); |
| 1659 | args->out.block_handle = block_handle; |
| 1660 | args->out.block_size = block_size; |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1661 | break; |
| 1662 | |
| 1663 | default: |
| 1664 | dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n"); |
| 1665 | rc = -ENOTTY; |
| 1666 | break; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | out: |
| 1670 | return rc; |
| 1671 | } |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1672 | |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1673 | static 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 Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1676 | { |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1677 | int rc; |
| 1678 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1679 | if (!access_ok((void __user *) (uintptr_t) addr, size)) { |
Oded Gabbay | 230afe7 | 2019-02-27 00:19:18 +0200 | [diff] [blame] | 1680 | dev_err(hdev->dev, "user pointer is invalid - 0x%llx\n", addr); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1681 | return -EFAULT; |
| 1682 | } |
| 1683 | |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1684 | userptr->pages = kvmalloc_array(npages, sizeof(*userptr->pages), |
| 1685 | GFP_KERNEL); |
| 1686 | if (!userptr->pages) |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1687 | return -ENOMEM; |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1688 | |
Daniel Vetter | d88a0c1 | 2020-11-27 17:41:18 +0100 | [diff] [blame] | 1689 | rc = pin_user_pages_fast(start, npages, |
| 1690 | FOLL_FORCE | FOLL_WRITE | FOLL_LONGTERM, |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1691 | userptr->pages); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1692 | |
| 1693 | if (rc != npages) { |
| 1694 | dev_err(hdev->dev, |
Tomer Tayar | f5d6e39 | 2021-06-10 20:48:39 +0300 | [diff] [blame] | 1695 | "Failed (%d) to pin host memory with user ptr 0x%llx, size 0x%llx, npages %d\n", |
| 1696 | rc, addr, size, npages); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1697 | if (rc < 0) |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1698 | goto destroy_pages; |
| 1699 | npages = rc; |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1700 | rc = -EFAULT; |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1701 | goto put_pages; |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1702 | } |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1703 | userptr->npages = npages; |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1704 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1705 | rc = sg_alloc_table_from_pages(userptr->sgt, |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1706 | userptr->pages, |
Ofir Bitton | d5eb837 | 2021-02-14 15:35:56 +0200 | [diff] [blame] | 1707 | npages, offset, size, GFP_KERNEL); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1708 | if (rc < 0) { |
| 1709 | dev_err(hdev->dev, "failed to create SG table from pages\n"); |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1710 | goto put_pages; |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | return 0; |
| 1714 | |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1715 | put_pages: |
| 1716 | unpin_user_pages(userptr->pages, npages); |
| 1717 | destroy_pages: |
| 1718 | kvfree(userptr->pages); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1719 | return rc; |
| 1720 | } |
| 1721 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1722 | /** |
| 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 Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1728 | * |
| 1729 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1730 | * - Pins the physical pages. |
| 1731 | * - Create an SG list from those pages. |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1732 | */ |
| 1733 | int 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 Bitton | d5eb837 | 2021-02-14 15:35:56 +0200 | [diff] [blame] | 1757 | userptr->sgt = kzalloc(sizeof(*userptr->sgt), GFP_KERNEL); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1758 | 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 Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1777 | goto free_sgt; |
| 1778 | } |
| 1779 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 1780 | hl_debugfs_add_userptr(hdev, userptr); |
| 1781 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1782 | return 0; |
| 1783 | |
| 1784 | free_sgt: |
| 1785 | kfree(userptr->sgt); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1786 | return rc; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1790 | * 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 Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1793 | * |
| 1794 | * This function does the following: |
| 1795 | * - Unpins the physical pages related to the host memory |
| 1796 | * - Free the SG list |
| 1797 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1798 | void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr) |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1799 | { |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 1800 | hl_debugfs_remove_userptr(hdev, userptr); |
| 1801 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1802 | if (userptr->dma_mapped) |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1803 | hdev->asic_funcs->hl_dma_unmap_sg(hdev, userptr->sgt->sgl, |
| 1804 | userptr->sgt->nents, |
| 1805 | userptr->dir); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1806 | |
Daniel Vetter | d4cb192 | 2020-11-27 17:41:17 +0100 | [diff] [blame] | 1807 | unpin_user_pages_dirty_lock(userptr->pages, userptr->npages, true); |
| 1808 | kvfree(userptr->pages); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1809 | |
| 1810 | list_del(&userptr->job_node); |
| 1811 | |
| 1812 | sg_free_table(userptr->sgt); |
| 1813 | kfree(userptr->sgt); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1816 | /** |
| 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 Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1820 | * |
| 1821 | * This function does the following: |
| 1822 | * - Iterates over the list and unpins the host memory and frees the userptr |
| 1823 | * structure. |
| 1824 | */ |
| 1825 | void 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1838 | /** |
| 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 Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1843 | * |
| 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 | */ |
| 1848 | bool 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1859 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1860 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1866 | * |
| 1867 | * This function does the following: |
| 1868 | * - Initializes the virtual addresses list of the given range with the given |
| 1869 | * addresses. |
| 1870 | */ |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1871 | static int va_range_init(struct hl_device *hdev, struct hl_va_range *va_range, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1872 | u64 start, u64 end, u32 page_size) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1873 | { |
| 1874 | int rc; |
| 1875 | |
| 1876 | INIT_LIST_HEAD(&va_range->list); |
| 1877 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1878 | /* |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1883 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1884 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1894 | 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 Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1908 | va_range->page_size = page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1909 | |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1913 | /** |
| 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1917 | * |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1918 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1919 | * - Frees the virtual addresses block list and its lock. |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1920 | */ |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1921 | static void va_range_fini(struct hl_device *hdev, struct hl_va_range *va_range) |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1922 | { |
| 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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1931 | /** |
| 1932 | * vm_ctx_init_with_ranges() - initialize virtual memory for context. |
| 1933 | * @ctx: pointer to the habanalabs context structure. |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1934 | * @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 Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1937 | * allocated with huge pages. |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1938 | * @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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1942 | * |
| 1943 | * This function initializes the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1944 | * - MMU for context. |
| 1945 | * - Virtual address to area descriptor hashtable. |
| 1946 | * - Virtual block list of available virtual memory. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1947 | */ |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1948 | static int vm_ctx_init_with_ranges(struct hl_ctx *ctx, |
| 1949 | u64 host_range_start, |
| 1950 | u64 host_range_end, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1951 | u32 host_page_size, |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1952 | u64 host_huge_range_start, |
| 1953 | u64 host_huge_range_end, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1954 | u32 host_huge_page_size, |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1955 | u64 dram_range_start, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1956 | u64 dram_range_end, |
| 1957 | u32 dram_page_size) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1958 | { |
| 1959 | struct hl_device *hdev = ctx->hdev; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1960 | int i, rc; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1961 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1962 | 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 Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Omer Shpigelman | 27ca384c | 2019-02-28 10:46:11 +0200 | [diff] [blame] | 1971 | rc = hl_mmu_ctx_init(ctx); |
| 1972 | if (rc) { |
| 1973 | dev_err(hdev->dev, "failed to init context %d\n", ctx->asid); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1974 | goto free_va_range; |
Omer Shpigelman | 27ca384c | 2019-02-28 10:46:11 +0200 | [diff] [blame] | 1975 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1976 | |
| 1977 | mutex_init(&ctx->mem_hash_lock); |
| 1978 | hash_init(ctx->mem_hash); |
| 1979 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1980 | mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1981 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1982 | rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST], |
| 1983 | host_range_start, host_range_end, host_page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1984 | if (rc) { |
| 1985 | dev_err(hdev->dev, "failed to init host vm range\n"); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1986 | goto mmu_ctx_fini; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1987 | } |
| 1988 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1989 | if (hdev->pmmu_huge_range) { |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1990 | mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1991 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1992 | 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 Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1996 | if (rc) { |
| 1997 | dev_err(hdev->dev, |
| 1998 | "failed to init host huge vm range\n"); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1999 | goto clear_host_va_range; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } else { |
Ofir Bitton | 8e718f2 | 2020-11-26 13:01:11 +0200 | [diff] [blame] | 2002 | kfree(ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2003 | ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] = |
| 2004 | ctx->va_range[HL_VA_RANGE_TYPE_HOST]; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2007 | mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock); |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2008 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2009 | rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM], |
| 2010 | dram_range_start, dram_range_end, dram_page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2011 | if (rc) { |
| 2012 | dev_err(hdev->dev, "failed to init dram vm range\n"); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2013 | goto clear_host_huge_va_range; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2014 | } |
| 2015 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 2016 | hl_debugfs_add_ctx_mem_hash(hdev, ctx); |
| 2017 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2018 | return 0; |
| 2019 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2020 | clear_host_huge_va_range: |
| 2021 | mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2022 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2023 | if (hdev->pmmu_huge_range) { |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2024 | 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 Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2028 | } |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2029 | clear_host_va_range: |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2030 | if (hdev->pmmu_huge_range) |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2031 | 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); |
| 2035 | mmu_ctx_fini: |
| 2036 | mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2037 | mutex_destroy(&ctx->mem_hash_lock); |
| 2038 | hl_mmu_ctx_fini(ctx); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2039 | free_va_range: |
| 2040 | for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++) |
| 2041 | kfree(ctx->va_range[i]); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2042 | |
| 2043 | return rc; |
| 2044 | } |
| 2045 | |
| 2046 | int hl_vm_ctx_init(struct hl_ctx *ctx) |
| 2047 | { |
| 2048 | struct asic_fixed_properties *prop = &ctx->hdev->asic_prop; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2049 | u64 host_range_start, host_range_end, host_huge_range_start, |
| 2050 | host_huge_range_end, dram_range_start, dram_range_end; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2051 | u32 host_page_size, host_huge_page_size, dram_page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2052 | |
| 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 Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 2062 | 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 Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 2067 | dram_page_size = prop->dram_page_size ? |
| 2068 | prop->dram_page_size : prop->dmmu.page_size; |
Oded Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 2069 | host_range_start = prop->pmmu.start_addr; |
| 2070 | host_range_end = prop->pmmu.end_addr; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2071 | host_page_size = prop->pmmu.page_size; |
Oded Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 2072 | host_huge_range_start = prop->pmmu_huge.start_addr; |
| 2073 | host_huge_range_end = prop->pmmu_huge.end_addr; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2074 | host_huge_page_size = prop->pmmu_huge.page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2075 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2076 | return vm_ctx_init_with_ranges(ctx, host_range_start, host_range_end, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2077 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2080 | } |
| 2081 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2082 | /** |
| 2083 | * hl_vm_ctx_fini() - virtual memory teardown of context. |
| 2084 | * @ctx: pointer to the habanalabs context structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2085 | * |
| 2086 | * This function perform teardown the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2087 | * - Virtual block list of available virtual memory. |
| 2088 | * - Virtual address to area descriptor hashtable. |
| 2089 | * - MMU for context. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2090 | * |
| 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 | */ |
| 2101 | void 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 Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 2108 | struct hl_mem_in args; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2109 | int i; |
| 2110 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2111 | if (!hdev->mmu_enable) |
Oded Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 2112 | return; |
| 2113 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 2114 | hl_debugfs_remove_ctx_mem_hash(hdev, ctx); |
| 2115 | |
Omer Shpigelman | e604f55 | 2019-11-14 18:23:59 +0000 | [diff] [blame] | 2116 | /* |
| 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 Gabbay | 0eab4f8 | 2020-06-22 09:52:22 +0300 | [diff] [blame] | 2122 | "user released device without removing its memory mappings\n"); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2123 | |
| 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 Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 2128 | args.unmap.device_virt_addr = hnode->vaddr; |
| 2129 | unmap_device_va(ctx, &args, true); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2130 | } |
| 2131 | |
Ohad Sharabi | cb6ef0e | 2020-11-26 09:39:26 +0200 | [diff] [blame] | 2132 | mutex_lock(&ctx->mmu_lock); |
| 2133 | |
Omer Shpigelman | bea84c4 | 2019-11-14 18:23:58 +0000 | [diff] [blame] | 2134 | /* 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 Sharabi | cb6ef0e | 2020-11-26 09:39:26 +0200 | [diff] [blame] | 2138 | mutex_unlock(&ctx->mmu_lock); |
| 2139 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2140 | 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 Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 2144 | "page list 0x%px of asid %d is still alive\n", |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2145 | phys_pg_list, ctx->asid); |
Tomer Tayar | c811375 | 2019-08-04 07:03:41 +0000 | [diff] [blame] | 2146 | atomic64_sub(phys_pg_list->total_size, |
| 2147 | &hdev->dram_used_mem); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2148 | 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 Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2153 | va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM]); |
Ofir Bitton | 8e718f2 | 2020-11-26 13:01:11 +0200 | [diff] [blame] | 2154 | va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST]); |
| 2155 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 2156 | if (hdev->pmmu_huge_range) |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 2157 | va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2158 | |
| 2159 | mutex_destroy(&ctx->mem_hash_lock); |
| 2160 | hl_mmu_ctx_fini(ctx); |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 2161 | |
| 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 Bitton | 8e39e75 | 2020-11-12 11:03:32 +0200 | [diff] [blame] | 2166 | if (ctx->asid != HL_KERNEL_ASID_ID && |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2167 | !hdev->asic_prop.dram_supports_virtual_memory) |
| 2168 | atomic64_set(&hdev->dram_used_mem, 0); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2169 | } |
| 2170 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2171 | /** |
| 2172 | * hl_vm_init() - initialize virtual memory module. |
| 2173 | * @hdev: pointer to the habanalabs device structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2174 | * |
| 2175 | * This function initializes the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2176 | * - MMU module. |
| 2177 | * - DRAM physical pages pool of 2MB. |
| 2178 | * - Idr for device memory allocation handles. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2179 | */ |
| 2180 | int 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 Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 2186 | 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2193 | if (!vm->dram_pg_pool) { |
| 2194 | dev_err(hdev->dev, "Failed to create dram page pool\n"); |
Oded Gabbay | 37d68ce | 2019-05-29 14:43:04 +0300 | [diff] [blame] | 2195 | return -ENOMEM; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2196 | } |
| 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 | |
| 2219 | pool_add_err: |
| 2220 | gen_pool_destroy(vm->dram_pg_pool); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2221 | |
| 2222 | return rc; |
| 2223 | } |
| 2224 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2225 | /** |
| 2226 | * hl_vm_fini() - virtual memory module teardown. |
| 2227 | * @hdev: pointer to the habanalabs device structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2228 | * |
| 2229 | * This function perform teardown to the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2230 | * - Idr for device memory allocation handles. |
| 2231 | * - DRAM physical pages pool of 2MB. |
| 2232 | * - MMU module. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2233 | */ |
| 2234 | void 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 Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2249 | vm->init_done = false; |
| 2250 | } |
Sagiv Ozeri | a4371c1 | 2021-02-23 11:01:08 +0200 | [diff] [blame] | 2251 | |
| 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 | */ |
| 2259 | void 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 | */ |
| 2272 | void 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 | } |