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 | } |
farah kassabri | 03df136 | 2020-05-06 11:17:38 +0300 | [diff] [blame] | 84 | |
| 85 | if (hdev->memory_scrub) { |
| 86 | rc = hdev->asic_funcs->scrub_device_mem(hdev, paddr, |
| 87 | total_size); |
| 88 | if (rc) { |
| 89 | dev_err(hdev->dev, |
| 90 | "Failed to scrub contiguous device memory\n"); |
| 91 | goto pages_pack_err; |
| 92 | } |
| 93 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL); |
| 97 | if (!phys_pg_pack) { |
| 98 | rc = -ENOMEM; |
| 99 | goto pages_pack_err; |
| 100 | } |
| 101 | |
| 102 | phys_pg_pack->vm_type = VM_TYPE_PHYS_PACK; |
| 103 | phys_pg_pack->asid = ctx->asid; |
| 104 | phys_pg_pack->npages = num_pgs; |
| 105 | phys_pg_pack->page_size = page_size; |
| 106 | phys_pg_pack->total_size = total_size; |
| 107 | phys_pg_pack->flags = args->flags; |
| 108 | phys_pg_pack->contiguous = contiguous; |
| 109 | |
Omer Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 110 | phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL); |
Ofir Bitton | 0839152 | 2020-08-11 08:57:45 +0300 | [diff] [blame] | 111 | if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 112 | rc = -ENOMEM; |
| 113 | goto pages_arr_err; |
| 114 | } |
| 115 | |
| 116 | if (phys_pg_pack->contiguous) { |
| 117 | for (i = 0 ; i < num_pgs ; i++) |
| 118 | phys_pg_pack->pages[i] = paddr + i * page_size; |
| 119 | } else { |
| 120 | for (i = 0 ; i < num_pgs ; i++) { |
| 121 | phys_pg_pack->pages[i] = (u64) gen_pool_alloc( |
| 122 | vm->dram_pg_pool, |
| 123 | page_size); |
| 124 | if (!phys_pg_pack->pages[i]) { |
| 125 | dev_err(hdev->dev, |
Oded Gabbay | cab8e3e | 2019-03-27 09:44:28 +0200 | [diff] [blame] | 126 | "Failed to allocate device memory (out of memory)\n"); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 127 | rc = -ENOMEM; |
| 128 | goto page_err; |
| 129 | } |
| 130 | |
farah kassabri | 03df136 | 2020-05-06 11:17:38 +0300 | [diff] [blame] | 131 | if (hdev->memory_scrub) { |
| 132 | rc = hdev->asic_funcs->scrub_device_mem(hdev, |
| 133 | phys_pg_pack->pages[i], |
| 134 | page_size); |
| 135 | if (rc) { |
| 136 | dev_err(hdev->dev, |
| 137 | "Failed to scrub device memory\n"); |
| 138 | goto page_err; |
| 139 | } |
| 140 | } |
| 141 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 142 | num_curr_pgs++; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | spin_lock(&vm->idr_lock); |
| 147 | handle = idr_alloc(&vm->phys_pg_pack_handles, phys_pg_pack, 1, 0, |
Wei Yongjun | 668ae72 | 2019-02-22 05:46:01 +0000 | [diff] [blame] | 148 | GFP_ATOMIC); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 149 | spin_unlock(&vm->idr_lock); |
| 150 | |
| 151 | if (handle < 0) { |
| 152 | dev_err(hdev->dev, "Failed to get handle for page\n"); |
| 153 | rc = -EFAULT; |
| 154 | goto idr_err; |
| 155 | } |
| 156 | |
| 157 | for (i = 0 ; i < num_pgs ; i++) |
| 158 | kref_get(&vm->dram_pg_pool_refcount); |
| 159 | |
| 160 | phys_pg_pack->handle = handle; |
| 161 | |
| 162 | atomic64_add(phys_pg_pack->total_size, &ctx->dram_phys_mem); |
| 163 | atomic64_add(phys_pg_pack->total_size, &hdev->dram_used_mem); |
| 164 | |
| 165 | *ret_handle = handle; |
| 166 | |
| 167 | return 0; |
| 168 | |
| 169 | idr_err: |
| 170 | page_err: |
| 171 | if (!phys_pg_pack->contiguous) |
| 172 | for (i = 0 ; i < num_curr_pgs ; i++) |
| 173 | gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[i], |
| 174 | page_size); |
| 175 | |
Omer Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 176 | kvfree(phys_pg_pack->pages); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 177 | pages_arr_err: |
| 178 | kfree(phys_pg_pack); |
| 179 | pages_pack_err: |
| 180 | if (contiguous) |
| 181 | gen_pool_free(vm->dram_pg_pool, paddr, total_size); |
| 182 | |
| 183 | return rc; |
| 184 | } |
| 185 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 186 | /** |
| 187 | * dma_map_host_va() - DMA mapping of the given host virtual address. |
| 188 | * @hdev: habanalabs device structure. |
| 189 | * @addr: the host virtual address of the memory area. |
| 190 | * @size: the size of the memory area. |
| 191 | * @p_userptr: pointer to result userptr structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 192 | * |
| 193 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 194 | * - Allocate userptr structure. |
| 195 | * - Pin the given host memory using the userptr structure. |
| 196 | * - Perform DMA mapping to have the DMA addresses of the pages. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 197 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 198 | static int dma_map_host_va(struct hl_device *hdev, u64 addr, u64 size, |
| 199 | struct hl_userptr **p_userptr) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 200 | { |
| 201 | struct hl_userptr *userptr; |
| 202 | int rc; |
| 203 | |
| 204 | userptr = kzalloc(sizeof(*userptr), GFP_KERNEL); |
| 205 | if (!userptr) { |
| 206 | rc = -ENOMEM; |
| 207 | goto userptr_err; |
| 208 | } |
| 209 | |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 210 | rc = hl_pin_host_memory(hdev, addr, size, userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 211 | if (rc) { |
| 212 | dev_err(hdev->dev, "Failed to pin host memory\n"); |
| 213 | goto pin_err; |
| 214 | } |
| 215 | |
| 216 | rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl, |
| 217 | userptr->sgt->nents, DMA_BIDIRECTIONAL); |
| 218 | if (rc) { |
| 219 | dev_err(hdev->dev, "failed to map sgt with DMA region\n"); |
| 220 | goto dma_map_err; |
| 221 | } |
| 222 | |
| 223 | userptr->dma_mapped = true; |
| 224 | userptr->dir = DMA_BIDIRECTIONAL; |
| 225 | userptr->vm_type = VM_TYPE_USERPTR; |
| 226 | |
| 227 | *p_userptr = userptr; |
| 228 | |
| 229 | return 0; |
| 230 | |
| 231 | dma_map_err: |
| 232 | hl_unpin_host_memory(hdev, userptr); |
| 233 | pin_err: |
| 234 | kfree(userptr); |
| 235 | userptr_err: |
| 236 | |
| 237 | return rc; |
| 238 | } |
| 239 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 240 | /** |
| 241 | * dma_unmap_host_va() - DMA unmapping of the given host virtual address. |
| 242 | * @hdev: habanalabs device structure. |
| 243 | * @userptr: userptr to free. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 244 | * |
| 245 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 246 | * - Unpins the physical pages. |
| 247 | * - Frees the userptr structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 248 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 249 | static void dma_unmap_host_va(struct hl_device *hdev, |
| 250 | struct hl_userptr *userptr) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 251 | { |
| 252 | hl_unpin_host_memory(hdev, userptr); |
| 253 | kfree(userptr); |
| 254 | } |
| 255 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 256 | /** |
| 257 | * dram_pg_pool_do_release() - free DRAM pages pool |
| 258 | * @ref: pointer to reference object. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 259 | * |
| 260 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 261 | * - Frees the idr structure of physical pages handles. |
| 262 | * - Frees the generic pool of DRAM physical pages. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 263 | */ |
| 264 | static void dram_pg_pool_do_release(struct kref *ref) |
| 265 | { |
| 266 | struct hl_vm *vm = container_of(ref, struct hl_vm, |
| 267 | dram_pg_pool_refcount); |
| 268 | |
| 269 | /* |
| 270 | * free the idr here as only here we know for sure that there are no |
| 271 | * allocated physical pages and hence there are no handles in use |
| 272 | */ |
| 273 | idr_destroy(&vm->phys_pg_pack_handles); |
| 274 | gen_pool_destroy(vm->dram_pg_pool); |
| 275 | } |
| 276 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 277 | /** |
| 278 | * free_phys_pg_pack() - free physical page pack. |
| 279 | * @hdev: habanalabs device structure. |
| 280 | * @phys_pg_pack: physical page pack to free. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 281 | * |
| 282 | * This function does the following: |
| 283 | * - For DRAM memory only, iterate over the pack and free each physical block |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 284 | * structure by returning it to the general pool. |
| 285 | * - Free the hl_vm_phys_pg_pack structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 286 | */ |
| 287 | static void free_phys_pg_pack(struct hl_device *hdev, |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 288 | struct hl_vm_phys_pg_pack *phys_pg_pack) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 289 | { |
| 290 | struct hl_vm *vm = &hdev->vm; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 291 | u64 i; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 292 | |
| 293 | if (!phys_pg_pack->created_from_userptr) { |
| 294 | if (phys_pg_pack->contiguous) { |
| 295 | gen_pool_free(vm->dram_pg_pool, phys_pg_pack->pages[0], |
| 296 | phys_pg_pack->total_size); |
| 297 | |
| 298 | for (i = 0; i < phys_pg_pack->npages ; i++) |
| 299 | kref_put(&vm->dram_pg_pool_refcount, |
| 300 | dram_pg_pool_do_release); |
| 301 | } else { |
| 302 | for (i = 0 ; i < phys_pg_pack->npages ; i++) { |
| 303 | gen_pool_free(vm->dram_pg_pool, |
| 304 | phys_pg_pack->pages[i], |
| 305 | phys_pg_pack->page_size); |
| 306 | kref_put(&vm->dram_pg_pool_refcount, |
| 307 | dram_pg_pool_do_release); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
Omer Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 312 | kvfree(phys_pg_pack->pages); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 313 | kfree(phys_pg_pack); |
| 314 | } |
| 315 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 316 | /** |
| 317 | * free_device_memory() - free device memory. |
| 318 | * @ctx: pointer to the context structure. |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 319 | * @args: host parameters containing the requested size. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 320 | * |
| 321 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 322 | * - Free the device memory related to the given handle. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 323 | */ |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 324 | 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] | 325 | { |
| 326 | struct hl_device *hdev = ctx->hdev; |
| 327 | struct hl_vm *vm = &hdev->vm; |
| 328 | struct hl_vm_phys_pg_pack *phys_pg_pack; |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 329 | u32 handle = args->free.handle; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 330 | |
| 331 | spin_lock(&vm->idr_lock); |
| 332 | phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle); |
| 333 | if (phys_pg_pack) { |
| 334 | if (atomic_read(&phys_pg_pack->mapping_cnt) > 0) { |
| 335 | dev_err(hdev->dev, "handle %u is mapped, cannot free\n", |
| 336 | handle); |
| 337 | spin_unlock(&vm->idr_lock); |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * must remove from idr before the freeing of the physical |
| 343 | * pages as the refcount of the pool is also the trigger of the |
| 344 | * idr destroy |
| 345 | */ |
| 346 | idr_remove(&vm->phys_pg_pack_handles, handle); |
| 347 | spin_unlock(&vm->idr_lock); |
| 348 | |
| 349 | atomic64_sub(phys_pg_pack->total_size, &ctx->dram_phys_mem); |
| 350 | atomic64_sub(phys_pg_pack->total_size, &hdev->dram_used_mem); |
| 351 | |
| 352 | free_phys_pg_pack(hdev, phys_pg_pack); |
| 353 | } else { |
| 354 | spin_unlock(&vm->idr_lock); |
| 355 | dev_err(hdev->dev, |
| 356 | "free device memory failed, no match for handle %u\n", |
| 357 | handle); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 364 | /** |
| 365 | * clear_va_list_locked() - free virtual addresses list. |
| 366 | * @hdev: habanalabs device structure. |
| 367 | * @va_list: list of virtual addresses to free. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 368 | * |
| 369 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 370 | * - Iterate over the list and free each virtual addresses block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 371 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 372 | * This function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 373 | */ |
| 374 | static void clear_va_list_locked(struct hl_device *hdev, |
| 375 | struct list_head *va_list) |
| 376 | { |
| 377 | struct hl_vm_va_block *va_block, *tmp; |
| 378 | |
| 379 | list_for_each_entry_safe(va_block, tmp, va_list, node) { |
| 380 | list_del(&va_block->node); |
| 381 | kfree(va_block); |
| 382 | } |
| 383 | } |
| 384 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 385 | /** |
| 386 | * print_va_list_locked() - print virtual addresses list. |
| 387 | * @hdev: habanalabs device structure. |
| 388 | * @va_list: list of virtual addresses to print. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 389 | * |
| 390 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 391 | * - Iterate over the list and print each virtual addresses block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 392 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 393 | * This function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 394 | */ |
| 395 | static void print_va_list_locked(struct hl_device *hdev, |
| 396 | struct list_head *va_list) |
| 397 | { |
| 398 | #if HL_MMU_DEBUG |
| 399 | struct hl_vm_va_block *va_block; |
| 400 | |
| 401 | dev_dbg(hdev->dev, "print va list:\n"); |
| 402 | |
| 403 | list_for_each_entry(va_block, va_list, node) |
| 404 | dev_dbg(hdev->dev, |
| 405 | "va block, start: 0x%llx, end: 0x%llx, size: %llu\n", |
| 406 | va_block->start, va_block->end, va_block->size); |
| 407 | #endif |
| 408 | } |
| 409 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 410 | /** |
| 411 | * merge_va_blocks_locked() - merge a virtual block if possible. |
| 412 | * @hdev: pointer to the habanalabs device structure. |
| 413 | * @va_list: pointer to the virtual addresses block list. |
| 414 | * @va_block: virtual block to merge with adjacent blocks. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 415 | * |
| 416 | * This function does the following: |
| 417 | * - Merge the given blocks with the adjacent blocks if their virtual ranges |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 418 | * create a contiguous virtual range. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 419 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 420 | * This Function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 421 | */ |
| 422 | static void merge_va_blocks_locked(struct hl_device *hdev, |
| 423 | struct list_head *va_list, struct hl_vm_va_block *va_block) |
| 424 | { |
| 425 | struct hl_vm_va_block *prev, *next; |
| 426 | |
| 427 | prev = list_prev_entry(va_block, node); |
| 428 | if (&prev->node != va_list && prev->end + 1 == va_block->start) { |
| 429 | prev->end = va_block->end; |
| 430 | prev->size = prev->end - prev->start; |
| 431 | list_del(&va_block->node); |
| 432 | kfree(va_block); |
| 433 | va_block = prev; |
| 434 | } |
| 435 | |
| 436 | next = list_next_entry(va_block, node); |
| 437 | if (&next->node != va_list && va_block->end + 1 == next->start) { |
| 438 | next->start = va_block->start; |
| 439 | next->size = next->end - next->start; |
| 440 | list_del(&va_block->node); |
| 441 | kfree(va_block); |
| 442 | } |
| 443 | } |
| 444 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 445 | /** |
| 446 | * add_va_block_locked() - add a virtual block to the virtual addresses list. |
| 447 | * @hdev: pointer to the habanalabs device structure. |
| 448 | * @va_list: pointer to the virtual addresses block list. |
| 449 | * @start: start virtual address. |
| 450 | * @end: end virtual address. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 451 | * |
| 452 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 453 | * - Add the given block to the virtual blocks list and merge with other blocks |
| 454 | * if a contiguous virtual block can be created. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 455 | * |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 456 | * This Function should be called only when va_list lock is taken. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 457 | */ |
| 458 | static int add_va_block_locked(struct hl_device *hdev, |
| 459 | struct list_head *va_list, u64 start, u64 end) |
| 460 | { |
| 461 | struct hl_vm_va_block *va_block, *res = NULL; |
| 462 | u64 size = end - start; |
| 463 | |
| 464 | print_va_list_locked(hdev, va_list); |
| 465 | |
| 466 | list_for_each_entry(va_block, va_list, node) { |
| 467 | /* TODO: remove upon matureness */ |
| 468 | if (hl_mem_area_crosses_range(start, size, va_block->start, |
| 469 | va_block->end)) { |
| 470 | dev_err(hdev->dev, |
| 471 | "block crossing ranges at start 0x%llx, end 0x%llx\n", |
| 472 | va_block->start, va_block->end); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | if (va_block->end < start) |
| 477 | res = va_block; |
| 478 | } |
| 479 | |
| 480 | va_block = kmalloc(sizeof(*va_block), GFP_KERNEL); |
| 481 | if (!va_block) |
| 482 | return -ENOMEM; |
| 483 | |
| 484 | va_block->start = start; |
| 485 | va_block->end = end; |
| 486 | va_block->size = size; |
| 487 | |
| 488 | if (!res) |
| 489 | list_add(&va_block->node, va_list); |
| 490 | else |
| 491 | list_add(&va_block->node, &res->node); |
| 492 | |
| 493 | merge_va_blocks_locked(hdev, va_list, va_block); |
| 494 | |
| 495 | print_va_list_locked(hdev, va_list); |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 500 | /** |
| 501 | * add_va_block() - wrapper for add_va_block_locked. |
| 502 | * @hdev: pointer to the habanalabs device structure. |
| 503 | * @va_list: pointer to the virtual addresses block list. |
| 504 | * @start: start virtual address. |
| 505 | * @end: end virtual address. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 506 | * |
| 507 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 508 | * - Takes the list lock and calls add_va_block_locked. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 509 | */ |
| 510 | static inline int add_va_block(struct hl_device *hdev, |
| 511 | struct hl_va_range *va_range, u64 start, u64 end) |
| 512 | { |
| 513 | int rc; |
| 514 | |
| 515 | mutex_lock(&va_range->lock); |
| 516 | rc = add_va_block_locked(hdev, &va_range->list, start, end); |
| 517 | mutex_unlock(&va_range->lock); |
| 518 | |
| 519 | return rc; |
| 520 | } |
| 521 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 522 | /** |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 523 | * get_va_block() - get a virtual block for the given size and alignment. |
| 524 | * |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 525 | * @hdev: pointer to the habanalabs device structure. |
| 526 | * @va_range: pointer to the virtual addresses range. |
| 527 | * @size: requested block size. |
| 528 | * @hint_addr: hint for requested address by the user. |
| 529 | * @va_block_align: required alignment of the virtual block start address. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 530 | * |
| 531 | * This function does the following: |
| 532 | * - 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^] | 533 | * given size, hint address and alignment. |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 534 | * - Reserve the requested block and update the list. |
| 535 | * - Return the start address of the virtual block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 536 | */ |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 537 | static u64 get_va_block(struct hl_device *hdev, |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 538 | struct hl_va_range *va_range, |
| 539 | u64 size, u64 hint_addr, u32 va_block_align) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 540 | { |
| 541 | struct hl_vm_va_block *va_block, *new_va_block = NULL; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 542 | u64 tmp_hint_addr, valid_start, valid_size, prev_start, prev_end, |
| 543 | align_mask, reserved_valid_start = 0, reserved_valid_size = 0; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 544 | bool add_prev = false; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 545 | bool is_align_pow_2 = is_power_of_2(va_range->page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 546 | |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 547 | if (is_align_pow_2) |
| 548 | align_mask = ~((u64)va_block_align - 1); |
| 549 | else |
| 550 | /* |
| 551 | * with non-power-of-2 range we work only with page granularity |
| 552 | * and the start address is page aligned, |
| 553 | * so no need for alignment checking. |
| 554 | */ |
| 555 | size = DIV_ROUND_UP_ULL(size, va_range->page_size) * |
| 556 | va_range->page_size; |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 557 | |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 558 | tmp_hint_addr = hint_addr; |
| 559 | |
| 560 | /* Check if we need to ignore hint address */ |
| 561 | if ((is_align_pow_2 && (hint_addr & (va_block_align - 1))) || |
| 562 | (!is_align_pow_2 && |
| 563 | do_div(tmp_hint_addr, va_range->page_size))) { |
| 564 | dev_info(hdev->dev, "Hint address 0x%llx will be ignored\n", |
| 565 | hint_addr); |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 566 | hint_addr = 0; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 567 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 568 | |
| 569 | mutex_lock(&va_range->lock); |
| 570 | |
| 571 | print_va_list_locked(hdev, &va_range->list); |
| 572 | |
| 573 | list_for_each_entry(va_block, &va_range->list, node) { |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 574 | /* Calc the first possible aligned addr */ |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 575 | valid_start = va_block->start; |
| 576 | |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 577 | if (is_align_pow_2 && (valid_start & (va_block_align - 1))) { |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 578 | valid_start &= align_mask; |
| 579 | valid_start += va_block_align; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 580 | if (valid_start > va_block->end) |
| 581 | continue; |
| 582 | } |
| 583 | |
| 584 | valid_size = va_block->end - valid_start; |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 585 | if (valid_size < size) |
| 586 | continue; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 587 | |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 588 | /* Pick the minimal length block which has the required size */ |
| 589 | if (!new_va_block || (valid_size < reserved_valid_size)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 590 | new_va_block = va_block; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 591 | reserved_valid_start = valid_start; |
| 592 | reserved_valid_size = valid_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | if (hint_addr && hint_addr >= valid_start && |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 596 | (hint_addr + size) <= va_block->end) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 597 | new_va_block = va_block; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 598 | reserved_valid_start = hint_addr; |
| 599 | reserved_valid_size = valid_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | if (!new_va_block) { |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 605 | dev_err(hdev->dev, "no available va block for size %llu\n", |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 606 | size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 607 | goto out; |
| 608 | } |
| 609 | |
farah kassabri | 8d79ce1 | 2021-01-11 10:10:00 +0200 | [diff] [blame^] | 610 | /* |
| 611 | * Check if there is some leftover range due to reserving the new |
| 612 | * va block, then return it to the main virtual addresses list. |
| 613 | */ |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 614 | if (reserved_valid_start > new_va_block->start) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 615 | prev_start = new_va_block->start; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 616 | prev_end = reserved_valid_start - 1; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 617 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 618 | new_va_block->start = reserved_valid_start; |
| 619 | new_va_block->size = reserved_valid_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 620 | |
| 621 | add_prev = true; |
| 622 | } |
| 623 | |
| 624 | if (new_va_block->size > size) { |
| 625 | new_va_block->start += size; |
| 626 | new_va_block->size = new_va_block->end - new_va_block->start; |
| 627 | } else { |
| 628 | list_del(&new_va_block->node); |
| 629 | kfree(new_va_block); |
| 630 | } |
| 631 | |
| 632 | if (add_prev) |
| 633 | add_va_block_locked(hdev, &va_range->list, prev_start, |
| 634 | prev_end); |
| 635 | |
| 636 | print_va_list_locked(hdev, &va_range->list); |
| 637 | out: |
| 638 | mutex_unlock(&va_range->lock); |
| 639 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 640 | return reserved_valid_start; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 643 | /* |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 644 | * hl_reserve_va_block() - reserve a virtual block of a given size. |
| 645 | * @hdev: pointer to the habanalabs device structure. |
| 646 | * @ctx: current context |
| 647 | * @type: virtual addresses range type. |
| 648 | * @size: requested block size. |
Ofir Bitton | 412c41f | 2020-11-04 15:18:55 +0200 | [diff] [blame] | 649 | * @alignment: required alignment in bytes of the virtual block start address, |
| 650 | * 0 means no alignment. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 651 | * |
| 652 | * This function does the following: |
| 653 | * - 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] | 654 | * given size and alignment. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 655 | * - Reserve the requested block and update the list. |
| 656 | * - Return the start address of the virtual block. |
| 657 | */ |
| 658 | 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] | 659 | enum hl_va_range_type type, u32 size, u32 alignment) |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 660 | { |
| 661 | return get_va_block(hdev, ctx->va_range[type], size, 0, |
Ofir Bitton | 412c41f | 2020-11-04 15:18:55 +0200 | [diff] [blame] | 662 | max(alignment, ctx->va_range[type]->page_size)); |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
| 666 | * 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] | 667 | * @address: the start address of the area we want to validate. |
| 668 | * @size: the size in bytes of the area we want to validate. |
| 669 | * @type: returned va_range type. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 670 | * |
| 671 | * Return: true if the area is inside a valid range, false otherwise. |
| 672 | */ |
| 673 | static int hl_get_va_range_type(struct hl_ctx *ctx, u64 address, u64 size, |
| 674 | enum hl_va_range_type *type) |
| 675 | { |
| 676 | int i; |
| 677 | |
| 678 | for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX; i++) { |
| 679 | if (hl_mem_area_inside_range(address, size, |
| 680 | ctx->va_range[i]->start_addr, |
| 681 | ctx->va_range[i]->end_addr)) { |
| 682 | *type = i; |
| 683 | return 0; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 690 | /** |
| 691 | * 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] | 692 | * @hdev: pointer to the habanalabs device structure |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 693 | * @ctx: pointer to the context structure. |
| 694 | * @start: start virtual address. |
| 695 | * @end: end virtual address. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 696 | * |
| 697 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 698 | * - Takes the list lock and calls add_va_block_locked. |
Ofir Bitton | be91b91 | 2020-10-22 15:04:10 +0300 | [diff] [blame] | 699 | */ |
| 700 | int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx, |
| 701 | u64 start_addr, u64 size) |
| 702 | { |
| 703 | enum hl_va_range_type type; |
| 704 | int rc; |
| 705 | |
| 706 | rc = hl_get_va_range_type(ctx, start_addr, size, &type); |
| 707 | if (rc) { |
| 708 | dev_err(hdev->dev, |
| 709 | "cannot find va_range for va %#llx size %llu", |
| 710 | start_addr, size); |
| 711 | return rc; |
| 712 | } |
| 713 | |
| 714 | rc = add_va_block(hdev, ctx->va_range[type], start_addr, |
| 715 | start_addr + size - 1); |
| 716 | if (rc) |
| 717 | dev_warn(hdev->dev, |
| 718 | "add va block failed for vaddr: 0x%llx\n", start_addr); |
| 719 | |
| 720 | return rc; |
| 721 | } |
| 722 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 723 | /** |
| 724 | * get_sg_info() - get number of pages and the DMA address from SG list. |
| 725 | * @sg: the SG list. |
| 726 | * @dma_addr: pointer to DMA address to return. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 727 | * |
| 728 | * Calculate the number of consecutive pages described by the SG list. Take the |
| 729 | * offset of the address in the first page, add to it the length and round it up |
| 730 | * to the number of needed pages. |
| 731 | */ |
| 732 | static u32 get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr) |
| 733 | { |
| 734 | *dma_addr = sg_dma_address(sg); |
| 735 | |
| 736 | return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) + |
| 737 | (PAGE_SIZE - 1)) >> PAGE_SHIFT; |
| 738 | } |
| 739 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 740 | /** |
| 741 | * init_phys_pg_pack_from_userptr() - initialize physical page pack from host |
| 742 | * memory |
| 743 | * @ctx: pointer to the context structure. |
| 744 | * @userptr: userptr to initialize from. |
| 745 | * @pphys_pg_pack: result pointer. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 746 | * |
| 747 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 748 | * - Pin the physical pages related to the given virtual block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 749 | * - 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] | 750 | * virtual block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 751 | */ |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 752 | static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx, |
| 753 | struct hl_userptr *userptr, |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 754 | struct hl_vm_phys_pg_pack **pphys_pg_pack) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 755 | { |
| 756 | struct hl_vm_phys_pg_pack *phys_pg_pack; |
| 757 | struct scatterlist *sg; |
| 758 | dma_addr_t dma_addr; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 759 | u64 page_mask, total_npages; |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 760 | u32 npages, page_size = PAGE_SIZE, |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 761 | huge_page_size = ctx->hdev->asic_prop.pmmu_huge.page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 762 | bool first = true, is_huge_page_opt = true; |
| 763 | int rc, i, j; |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 764 | u32 pgs_in_huge_page = huge_page_size >> __ffs(page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 765 | |
| 766 | phys_pg_pack = kzalloc(sizeof(*phys_pg_pack), GFP_KERNEL); |
| 767 | if (!phys_pg_pack) |
| 768 | return -ENOMEM; |
| 769 | |
| 770 | phys_pg_pack->vm_type = userptr->vm_type; |
| 771 | phys_pg_pack->created_from_userptr = true; |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 772 | phys_pg_pack->asid = ctx->asid; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 773 | atomic_set(&phys_pg_pack->mapping_cnt, 1); |
| 774 | |
| 775 | /* Only if all dma_addrs are aligned to 2MB and their |
| 776 | * sizes is at least 2MB, we can use huge page mapping. |
| 777 | * We limit the 2MB optimization to this condition, |
| 778 | * since later on we acquire the related VA range as one |
| 779 | * consecutive block. |
| 780 | */ |
| 781 | total_npages = 0; |
| 782 | for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) { |
| 783 | npages = get_sg_info(sg, &dma_addr); |
| 784 | |
| 785 | total_npages += npages; |
| 786 | |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 787 | if ((npages % pgs_in_huge_page) || |
| 788 | (dma_addr & (huge_page_size - 1))) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 789 | is_huge_page_opt = false; |
| 790 | } |
| 791 | |
| 792 | if (is_huge_page_opt) { |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 793 | page_size = huge_page_size; |
| 794 | do_div(total_npages, pgs_in_huge_page); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | page_mask = ~(((u64) page_size) - 1); |
| 798 | |
Omer Shpigelman | 4eb1d12 | 2019-03-07 15:47:19 +0200 | [diff] [blame] | 799 | phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64), |
| 800 | GFP_KERNEL); |
Ofir Bitton | 0839152 | 2020-08-11 08:57:45 +0300 | [diff] [blame] | 801 | if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 802 | rc = -ENOMEM; |
| 803 | goto page_pack_arr_mem_err; |
| 804 | } |
| 805 | |
| 806 | phys_pg_pack->npages = total_npages; |
| 807 | phys_pg_pack->page_size = page_size; |
| 808 | phys_pg_pack->total_size = total_npages * page_size; |
| 809 | |
| 810 | j = 0; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 811 | for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents, i) { |
| 812 | npages = get_sg_info(sg, &dma_addr); |
| 813 | |
| 814 | /* align down to physical page size and save the offset */ |
| 815 | if (first) { |
| 816 | first = false; |
| 817 | phys_pg_pack->offset = dma_addr & (page_size - 1); |
| 818 | dma_addr &= page_mask; |
| 819 | } |
| 820 | |
| 821 | while (npages) { |
| 822 | phys_pg_pack->pages[j++] = dma_addr; |
| 823 | dma_addr += page_size; |
| 824 | |
| 825 | if (is_huge_page_opt) |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 826 | npages -= pgs_in_huge_page; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 827 | else |
| 828 | npages--; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | *pphys_pg_pack = phys_pg_pack; |
| 833 | |
| 834 | return 0; |
| 835 | |
| 836 | page_pack_arr_mem_err: |
| 837 | kfree(phys_pg_pack); |
| 838 | |
| 839 | return rc; |
| 840 | } |
| 841 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 842 | /** |
| 843 | * map_phys_pg_pack() - maps the physical page pack.. |
| 844 | * @ctx: pointer to the context structure. |
| 845 | * @vaddr: start address of the virtual area to map from. |
| 846 | * @phys_pg_pack: the pack of physical pages to map to. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 847 | * |
| 848 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 849 | * - Maps each chunk of virtual memory to matching physical chunk. |
| 850 | * - Stores number of successful mappings in the given argument. |
| 851 | * - Returns 0 on success, error code otherwise. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 852 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 853 | static int map_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr, |
| 854 | struct hl_vm_phys_pg_pack *phys_pg_pack) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 855 | { |
| 856 | struct hl_device *hdev = ctx->hdev; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 857 | u64 next_vaddr = vaddr, paddr, mapped_pg_cnt = 0, i; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 858 | u32 page_size = phys_pg_pack->page_size; |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 859 | int rc = 0; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 860 | |
| 861 | for (i = 0 ; i < phys_pg_pack->npages ; i++) { |
| 862 | paddr = phys_pg_pack->pages[i]; |
| 863 | |
Ofir Bitton | 5c05487 | 2020-10-22 15:13:10 +0300 | [diff] [blame] | 864 | rc = hl_mmu_map_page(ctx, next_vaddr, paddr, page_size, |
Pawel Piskorski | 7fc40bc | 2019-12-06 17:32:38 +0200 | [diff] [blame] | 865 | (i + 1) == phys_pg_pack->npages); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 866 | if (rc) { |
| 867 | dev_err(hdev->dev, |
Omer Shpigelman | bfb1ce1 | 2019-03-05 10:59:16 +0200 | [diff] [blame] | 868 | "map failed for handle %u, npages: %llu, mapped: %llu", |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 869 | phys_pg_pack->handle, phys_pg_pack->npages, |
| 870 | mapped_pg_cnt); |
| 871 | goto err; |
| 872 | } |
| 873 | |
| 874 | mapped_pg_cnt++; |
| 875 | next_vaddr += page_size; |
| 876 | } |
| 877 | |
| 878 | return 0; |
| 879 | |
| 880 | err: |
| 881 | next_vaddr = vaddr; |
| 882 | for (i = 0 ; i < mapped_pg_cnt ; i++) { |
Ofir Bitton | 5c05487 | 2020-10-22 15:13:10 +0300 | [diff] [blame] | 883 | if (hl_mmu_unmap_page(ctx, next_vaddr, page_size, |
Pawel Piskorski | 7fc40bc | 2019-12-06 17:32:38 +0200 | [diff] [blame] | 884 | (i + 1) == mapped_pg_cnt)) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 885 | dev_warn_ratelimited(hdev->dev, |
| 886 | "failed to unmap handle %u, va: 0x%llx, pa: 0x%llx, page size: %u\n", |
| 887 | phys_pg_pack->handle, next_vaddr, |
| 888 | phys_pg_pack->pages[i], page_size); |
| 889 | |
| 890 | next_vaddr += page_size; |
| 891 | } |
| 892 | |
| 893 | return rc; |
| 894 | } |
| 895 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 896 | /** |
| 897 | * unmap_phys_pg_pack() - unmaps the physical page pack. |
| 898 | * @ctx: pointer to the context structure. |
| 899 | * @vaddr: start address of the virtual area to unmap. |
| 900 | * @phys_pg_pack: the pack of physical pages to unmap. |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 901 | */ |
| 902 | static void unmap_phys_pg_pack(struct hl_ctx *ctx, u64 vaddr, |
| 903 | struct hl_vm_phys_pg_pack *phys_pg_pack) |
| 904 | { |
| 905 | struct hl_device *hdev = ctx->hdev; |
| 906 | u64 next_vaddr, i; |
Oded Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 907 | bool is_host_addr; |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 908 | u32 page_size; |
| 909 | |
Oded Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 910 | is_host_addr = !hl_is_dram_va(hdev, vaddr); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 911 | page_size = phys_pg_pack->page_size; |
| 912 | next_vaddr = vaddr; |
| 913 | |
| 914 | for (i = 0 ; i < phys_pg_pack->npages ; i++, next_vaddr += page_size) { |
Ofir Bitton | 5c05487 | 2020-10-22 15:13:10 +0300 | [diff] [blame] | 915 | if (hl_mmu_unmap_page(ctx, next_vaddr, page_size, |
Pawel Piskorski | 7fc40bc | 2019-12-06 17:32:38 +0200 | [diff] [blame] | 916 | (i + 1) == phys_pg_pack->npages)) |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 917 | dev_warn_ratelimited(hdev->dev, |
| 918 | "unmap failed for vaddr: 0x%llx\n", next_vaddr); |
| 919 | |
| 920 | /* |
| 921 | * unmapping on Palladium can be really long, so avoid a CPU |
| 922 | * soft lockup bug by sleeping a little between unmapping pages |
Oded Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 923 | * |
| 924 | * In addition, when unmapping host memory we pass through |
| 925 | * the Linux kernel to unpin the pages and that takes a long |
| 926 | * time. Therefore, sleep every 32K pages to avoid soft lockup |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 927 | */ |
Oded Gabbay | 9488307 | 2021-01-11 17:49:30 +0200 | [diff] [blame] | 928 | if (hdev->pldm || (is_host_addr && (i & 0x7FFF) == 0)) |
| 929 | usleep_range(50, 200); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 933 | 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] | 934 | u64 *paddr) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 935 | { |
| 936 | struct hl_device *hdev = ctx->hdev; |
| 937 | struct hl_vm *vm = &hdev->vm; |
| 938 | struct hl_vm_phys_pg_pack *phys_pg_pack; |
| 939 | u32 handle; |
| 940 | |
| 941 | handle = lower_32_bits(args->map_device.handle); |
| 942 | spin_lock(&vm->idr_lock); |
| 943 | phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle); |
| 944 | if (!phys_pg_pack) { |
| 945 | spin_unlock(&vm->idr_lock); |
| 946 | dev_err(hdev->dev, "no match for handle %u\n", handle); |
| 947 | return -EINVAL; |
| 948 | } |
| 949 | |
| 950 | *paddr = phys_pg_pack->pages[0]; |
| 951 | |
| 952 | spin_unlock(&vm->idr_lock); |
| 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 957 | /** |
| 958 | * map_device_va() - map the given memory. |
| 959 | * @ctx: pointer to the context structure. |
| 960 | * @args: host parameters with handle/host virtual address. |
| 961 | * @device_addr: pointer to result device virtual address. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 962 | * |
| 963 | * This function does the following: |
| 964 | * - 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] | 965 | * and return the start address of this block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 966 | * - If given a host virtual address and size, find the related physical pages, |
| 967 | * map a device virtual block to this pages and return the start address of |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 968 | * this block. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 969 | */ |
| 970 | static int map_device_va(struct hl_ctx *ctx, struct hl_mem_in *args, |
| 971 | u64 *device_addr) |
| 972 | { |
| 973 | struct hl_device *hdev = ctx->hdev; |
| 974 | struct hl_vm *vm = &hdev->vm; |
| 975 | struct hl_vm_phys_pg_pack *phys_pg_pack; |
| 976 | struct hl_userptr *userptr = NULL; |
| 977 | struct hl_vm_hash_node *hnode; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 978 | struct hl_va_range *va_range; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 979 | enum vm_type_t *vm_type; |
| 980 | u64 ret_vaddr, hint_addr; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 981 | u32 handle = 0, va_block_align; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 982 | int rc; |
| 983 | bool is_userptr = args->flags & HL_MEM_USERPTR; |
| 984 | |
| 985 | /* Assume failure */ |
| 986 | *device_addr = 0; |
| 987 | |
| 988 | if (is_userptr) { |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 989 | u64 addr = args->map_host.host_virt_addr, |
| 990 | size = args->map_host.mem_size; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 991 | u32 page_size = hdev->asic_prop.pmmu.page_size, |
| 992 | huge_page_size = hdev->asic_prop.pmmu_huge.page_size; |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 993 | |
| 994 | rc = dma_map_host_va(hdev, addr, size, &userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 995 | if (rc) { |
| 996 | dev_err(hdev->dev, "failed to get userptr from va\n"); |
| 997 | return rc; |
| 998 | } |
| 999 | |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 1000 | rc = init_phys_pg_pack_from_userptr(ctx, userptr, |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1001 | &phys_pg_pack); |
| 1002 | if (rc) { |
| 1003 | dev_err(hdev->dev, |
| 1004 | "unable to init page pack for vaddr 0x%llx\n", |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1005 | addr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1006 | goto init_page_pack_err; |
| 1007 | } |
| 1008 | |
| 1009 | vm_type = (enum vm_type_t *) userptr; |
| 1010 | hint_addr = args->map_host.hint_addr; |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1011 | handle = phys_pg_pack->handle; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1012 | |
| 1013 | /* get required alignment */ |
| 1014 | if (phys_pg_pack->page_size == page_size) { |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1015 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST]; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1016 | |
| 1017 | /* |
| 1018 | * huge page alignment may be needed in case of regular |
| 1019 | * page mapping, depending on the host VA alignment |
| 1020 | */ |
| 1021 | if (addr & (huge_page_size - 1)) |
| 1022 | va_block_align = page_size; |
| 1023 | else |
| 1024 | va_block_align = huge_page_size; |
| 1025 | } else { |
| 1026 | /* |
| 1027 | * huge page alignment is needed in case of huge page |
| 1028 | * mapping |
| 1029 | */ |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1030 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1031 | va_block_align = huge_page_size; |
| 1032 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1033 | } else { |
| 1034 | handle = lower_32_bits(args->map_device.handle); |
| 1035 | |
| 1036 | spin_lock(&vm->idr_lock); |
| 1037 | phys_pg_pack = idr_find(&vm->phys_pg_pack_handles, handle); |
| 1038 | if (!phys_pg_pack) { |
| 1039 | spin_unlock(&vm->idr_lock); |
| 1040 | dev_err(hdev->dev, |
| 1041 | "no match for handle %u\n", handle); |
| 1042 | return -EINVAL; |
| 1043 | } |
| 1044 | |
| 1045 | /* increment now to avoid freeing device memory while mapping */ |
| 1046 | atomic_inc(&phys_pg_pack->mapping_cnt); |
| 1047 | |
| 1048 | spin_unlock(&vm->idr_lock); |
| 1049 | |
| 1050 | vm_type = (enum vm_type_t *) phys_pg_pack; |
| 1051 | |
| 1052 | hint_addr = args->map_device.hint_addr; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1053 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1054 | /* DRAM VA alignment is the same as the MMU page size */ |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1055 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM]; |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1056 | va_block_align = hdev->asic_prop.dmmu.page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * relevant for mapping device physical memory only, as host memory is |
| 1061 | * implicitly shared |
| 1062 | */ |
| 1063 | if (!is_userptr && !(phys_pg_pack->flags & HL_MEM_SHARED) && |
| 1064 | phys_pg_pack->asid != ctx->asid) { |
| 1065 | dev_err(hdev->dev, |
| 1066 | "Failed to map memory, handle %u is not shared\n", |
| 1067 | handle); |
| 1068 | rc = -EPERM; |
| 1069 | goto shared_err; |
| 1070 | } |
| 1071 | |
| 1072 | hnode = kzalloc(sizeof(*hnode), GFP_KERNEL); |
| 1073 | if (!hnode) { |
| 1074 | rc = -ENOMEM; |
| 1075 | goto hnode_err; |
| 1076 | } |
| 1077 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1078 | ret_vaddr = get_va_block(hdev, va_range, phys_pg_pack->total_size, |
Omer Shpigelman | 7c52fb0 | 2020-06-28 21:15:53 +0300 | [diff] [blame] | 1079 | hint_addr, va_block_align); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1080 | if (!ret_vaddr) { |
| 1081 | dev_err(hdev->dev, "no available va block for handle %u\n", |
| 1082 | handle); |
| 1083 | rc = -ENOMEM; |
| 1084 | goto va_block_err; |
| 1085 | } |
| 1086 | |
| 1087 | mutex_lock(&ctx->mmu_lock); |
| 1088 | |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1089 | rc = map_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1090 | if (rc) { |
| 1091 | mutex_unlock(&ctx->mmu_lock); |
| 1092 | dev_err(hdev->dev, "mapping page pack failed for handle %u\n", |
| 1093 | handle); |
| 1094 | goto map_err; |
| 1095 | } |
| 1096 | |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1097 | rc = hdev->asic_funcs->mmu_invalidate_cache(hdev, false, *vm_type); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1098 | |
| 1099 | mutex_unlock(&ctx->mmu_lock); |
| 1100 | |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1101 | if (rc) { |
| 1102 | dev_err(hdev->dev, |
| 1103 | "mapping handle %u failed due to MMU cache invalidation\n", |
| 1104 | handle); |
| 1105 | goto map_err; |
| 1106 | } |
| 1107 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1108 | ret_vaddr += phys_pg_pack->offset; |
| 1109 | |
| 1110 | hnode->ptr = vm_type; |
| 1111 | hnode->vaddr = ret_vaddr; |
| 1112 | |
| 1113 | mutex_lock(&ctx->mem_hash_lock); |
| 1114 | hash_add(ctx->mem_hash, &hnode->node, ret_vaddr); |
| 1115 | mutex_unlock(&ctx->mem_hash_lock); |
| 1116 | |
| 1117 | *device_addr = ret_vaddr; |
| 1118 | |
| 1119 | if (is_userptr) |
| 1120 | free_phys_pg_pack(hdev, phys_pg_pack); |
| 1121 | |
| 1122 | return 0; |
| 1123 | |
| 1124 | map_err: |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1125 | if (add_va_block(hdev, va_range, ret_vaddr, |
| 1126 | ret_vaddr + phys_pg_pack->total_size - 1)) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1127 | dev_warn(hdev->dev, |
| 1128 | "release va block failed for handle 0x%x, vaddr: 0x%llx\n", |
| 1129 | handle, ret_vaddr); |
| 1130 | |
| 1131 | va_block_err: |
| 1132 | kfree(hnode); |
| 1133 | hnode_err: |
| 1134 | shared_err: |
| 1135 | atomic_dec(&phys_pg_pack->mapping_cnt); |
| 1136 | if (is_userptr) |
| 1137 | free_phys_pg_pack(hdev, phys_pg_pack); |
| 1138 | init_page_pack_err: |
| 1139 | if (is_userptr) |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1140 | dma_unmap_host_va(hdev, userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1141 | |
| 1142 | return rc; |
| 1143 | } |
| 1144 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1145 | /** |
| 1146 | * unmap_device_va() - unmap the given device virtual address. |
| 1147 | * @ctx: pointer to the context structure. |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1148 | * @args: host parameters with device virtual address to unmap. |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1149 | * @ctx_free: true if in context free flow, false otherwise. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1150 | * |
| 1151 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1152 | * - unmap the physical pages related to the given virtual address. |
| 1153 | * - return the device virtual block to the virtual block list. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1154 | */ |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1155 | static int unmap_device_va(struct hl_ctx *ctx, struct hl_mem_in *args, |
| 1156 | bool ctx_free) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1157 | { |
| 1158 | struct hl_device *hdev = ctx->hdev; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1159 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1160 | struct hl_vm_phys_pg_pack *phys_pg_pack = NULL; |
| 1161 | struct hl_vm_hash_node *hnode = NULL; |
| 1162 | struct hl_userptr *userptr = NULL; |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1163 | struct hl_va_range *va_range; |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1164 | u64 vaddr = args->unmap.device_virt_addr; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1165 | enum vm_type_t *vm_type; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1166 | bool is_userptr; |
Tomer Tayar | c68f1ba | 2020-06-01 09:56:47 +0300 | [diff] [blame] | 1167 | int rc = 0; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1168 | |
| 1169 | /* protect from double entrance */ |
| 1170 | mutex_lock(&ctx->mem_hash_lock); |
| 1171 | hash_for_each_possible(ctx->mem_hash, hnode, node, (unsigned long)vaddr) |
| 1172 | if (vaddr == hnode->vaddr) |
| 1173 | break; |
| 1174 | |
| 1175 | if (!hnode) { |
| 1176 | mutex_unlock(&ctx->mem_hash_lock); |
| 1177 | dev_err(hdev->dev, |
| 1178 | "unmap failed, no mem hnode for vaddr 0x%llx\n", |
| 1179 | vaddr); |
| 1180 | return -EINVAL; |
| 1181 | } |
| 1182 | |
| 1183 | hash_del(&hnode->node); |
| 1184 | mutex_unlock(&ctx->mem_hash_lock); |
| 1185 | |
| 1186 | vm_type = hnode->ptr; |
| 1187 | |
| 1188 | if (*vm_type == VM_TYPE_USERPTR) { |
| 1189 | is_userptr = true; |
| 1190 | userptr = hnode->ptr; |
Omer Shpigelman | 54bb674 | 2019-11-14 18:23:55 +0000 | [diff] [blame] | 1191 | rc = init_phys_pg_pack_from_userptr(ctx, userptr, |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1192 | &phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1193 | if (rc) { |
| 1194 | dev_err(hdev->dev, |
| 1195 | "unable to init page pack for vaddr 0x%llx\n", |
| 1196 | vaddr); |
| 1197 | goto vm_type_err; |
| 1198 | } |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (phys_pg_pack->page_size == |
| 1201 | hdev->asic_prop.pmmu.page_size) |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1202 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST]; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1203 | else |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1204 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1205 | } else if (*vm_type == VM_TYPE_PHYS_PACK) { |
| 1206 | is_userptr = false; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1207 | va_range = ctx->va_range[HL_VA_RANGE_TYPE_DRAM]; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1208 | phys_pg_pack = hnode->ptr; |
| 1209 | } else { |
| 1210 | dev_warn(hdev->dev, |
| 1211 | "unmap failed, unknown vm desc for vaddr 0x%llx\n", |
| 1212 | vaddr); |
| 1213 | rc = -EFAULT; |
| 1214 | goto vm_type_err; |
| 1215 | } |
| 1216 | |
| 1217 | if (atomic_read(&phys_pg_pack->mapping_cnt) == 0) { |
| 1218 | dev_err(hdev->dev, "vaddr 0x%llx is not mapped\n", vaddr); |
| 1219 | rc = -EINVAL; |
| 1220 | goto mapping_cnt_err; |
| 1221 | } |
| 1222 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1223 | if (!is_userptr && !is_power_of_2(phys_pg_pack->page_size)) |
| 1224 | vaddr = prop->dram_base_address + |
| 1225 | DIV_ROUND_DOWN_ULL(vaddr - prop->dram_base_address, |
| 1226 | phys_pg_pack->page_size) * |
| 1227 | phys_pg_pack->page_size; |
| 1228 | else |
| 1229 | vaddr &= ~(((u64) phys_pg_pack->page_size) - 1); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1230 | |
| 1231 | mutex_lock(&ctx->mmu_lock); |
| 1232 | |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1233 | unmap_phys_pg_pack(ctx, vaddr, phys_pg_pack); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1234 | |
Omer Shpigelman | bea84c4 | 2019-11-14 18:23:58 +0000 | [diff] [blame] | 1235 | /* |
| 1236 | * During context free this function is called in a loop to clean all |
| 1237 | * the context mappings. Hence the cache invalidation can be called once |
| 1238 | * at the loop end rather than for each iteration |
| 1239 | */ |
| 1240 | if (!ctx_free) |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1241 | rc = hdev->asic_funcs->mmu_invalidate_cache(hdev, true, |
| 1242 | *vm_type); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1243 | |
| 1244 | mutex_unlock(&ctx->mmu_lock); |
| 1245 | |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1246 | /* |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1247 | * If the context is closing we don't need to check for the MMU cache |
| 1248 | * invalidation return code and update the VA free list as in this flow |
| 1249 | * we invalidate the MMU cache outside of this unmap function and the VA |
| 1250 | * free list will be freed anyway. |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1251 | */ |
| 1252 | if (!ctx_free) { |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1253 | int tmp_rc; |
| 1254 | |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1255 | if (rc) |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1256 | dev_err(hdev->dev, |
| 1257 | "unmapping vaddr 0x%llx failed due to MMU cache invalidation\n", |
| 1258 | vaddr); |
| 1259 | |
| 1260 | tmp_rc = add_va_block(hdev, va_range, vaddr, |
| 1261 | vaddr + phys_pg_pack->total_size - 1); |
| 1262 | if (tmp_rc) { |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1263 | dev_warn(hdev->dev, |
| 1264 | "add va block failed for vaddr: 0x%llx\n", |
| 1265 | vaddr); |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1266 | if (!rc) |
| 1267 | rc = tmp_rc; |
| 1268 | } |
Omer Shpigelman | 71c5e55 | 2019-11-14 18:23:57 +0000 | [diff] [blame] | 1269 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1270 | |
| 1271 | atomic_dec(&phys_pg_pack->mapping_cnt); |
| 1272 | kfree(hnode); |
| 1273 | |
| 1274 | if (is_userptr) { |
| 1275 | free_phys_pg_pack(hdev, phys_pg_pack); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1276 | dma_unmap_host_va(hdev, userptr); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Omer Shpigelman | 8ff5f4f | 2020-05-24 23:06:59 +0300 | [diff] [blame] | 1279 | return rc; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1280 | |
| 1281 | mapping_cnt_err: |
| 1282 | if (is_userptr) |
| 1283 | free_phys_pg_pack(hdev, phys_pg_pack); |
| 1284 | vm_type_err: |
| 1285 | mutex_lock(&ctx->mem_hash_lock); |
| 1286 | hash_add(ctx->mem_hash, &hnode->node, vaddr); |
| 1287 | mutex_unlock(&ctx->mem_hash_lock); |
| 1288 | |
| 1289 | return rc; |
| 1290 | } |
| 1291 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1292 | static int mem_ioctl_no_mmu(struct hl_fpriv *hpriv, union hl_mem_args *args) |
| 1293 | { |
| 1294 | struct hl_device *hdev = hpriv->hdev; |
| 1295 | struct hl_ctx *ctx = hpriv->ctx; |
| 1296 | u64 device_addr = 0; |
| 1297 | u32 handle = 0; |
| 1298 | int rc; |
| 1299 | |
| 1300 | switch (args->in.op) { |
| 1301 | case HL_MEM_OP_ALLOC: |
| 1302 | if (args->in.alloc.mem_size == 0) { |
| 1303 | dev_err(hdev->dev, |
| 1304 | "alloc size must be larger than 0\n"); |
| 1305 | rc = -EINVAL; |
| 1306 | goto out; |
| 1307 | } |
| 1308 | |
| 1309 | /* Force contiguous as there are no real MMU |
| 1310 | * translations to overcome physical memory gaps |
| 1311 | */ |
| 1312 | args->in.flags |= HL_MEM_CONTIGUOUS; |
| 1313 | rc = alloc_device_memory(ctx, &args->in, &handle); |
| 1314 | |
| 1315 | memset(args, 0, sizeof(*args)); |
| 1316 | args->out.handle = (__u64) handle; |
| 1317 | break; |
| 1318 | |
| 1319 | case HL_MEM_OP_FREE: |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1320 | rc = free_device_memory(ctx, &args->in); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1321 | break; |
| 1322 | |
| 1323 | case HL_MEM_OP_MAP: |
| 1324 | if (args->in.flags & HL_MEM_USERPTR) { |
| 1325 | device_addr = args->in.map_host.host_virt_addr; |
| 1326 | rc = 0; |
| 1327 | } else { |
| 1328 | rc = get_paddr_from_handle(ctx, &args->in, |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1329 | &device_addr); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | memset(args, 0, sizeof(*args)); |
| 1333 | args->out.device_virt_addr = device_addr; |
| 1334 | break; |
| 1335 | |
| 1336 | case HL_MEM_OP_UNMAP: |
| 1337 | rc = 0; |
| 1338 | break; |
| 1339 | |
| 1340 | default: |
| 1341 | dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n"); |
| 1342 | rc = -ENOTTY; |
| 1343 | break; |
| 1344 | } |
| 1345 | |
| 1346 | out: |
| 1347 | return rc; |
| 1348 | } |
| 1349 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1350 | int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data) |
| 1351 | { |
Ofir Bitton | 66a7640 | 2020-10-05 14:40:10 +0300 | [diff] [blame] | 1352 | enum hl_device_status status; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1353 | union hl_mem_args *args = data; |
| 1354 | struct hl_device *hdev = hpriv->hdev; |
| 1355 | struct hl_ctx *ctx = hpriv->ctx; |
| 1356 | u64 device_addr = 0; |
| 1357 | u32 handle = 0; |
| 1358 | int rc; |
| 1359 | |
Ofir Bitton | 66a7640 | 2020-10-05 14:40:10 +0300 | [diff] [blame] | 1360 | if (!hl_device_operational(hdev, &status)) { |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1361 | dev_warn_ratelimited(hdev->dev, |
Oded Gabbay | 3f5398c | 2019-04-06 15:41:35 +0300 | [diff] [blame] | 1362 | "Device is %s. Can't execute MEMORY IOCTL\n", |
Ofir Bitton | 66a7640 | 2020-10-05 14:40:10 +0300 | [diff] [blame] | 1363 | hdev->status[status]); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1364 | return -EBUSY; |
| 1365 | } |
| 1366 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1367 | if (!hdev->mmu_enable) |
| 1368 | return mem_ioctl_no_mmu(hpriv, args); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1369 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1370 | switch (args->in.op) { |
| 1371 | case HL_MEM_OP_ALLOC: |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1372 | if (args->in.alloc.mem_size == 0) { |
| 1373 | dev_err(hdev->dev, |
| 1374 | "alloc size must be larger than 0\n"); |
| 1375 | rc = -EINVAL; |
| 1376 | goto out; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1377 | } |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1378 | |
| 1379 | /* If DRAM does not support virtual memory the driver won't |
| 1380 | * handle the allocation/freeing of that memory. However, for |
| 1381 | * system administration/monitoring purposes, the driver will |
| 1382 | * keep track of the amount of DRAM memory that is allocated |
| 1383 | * and freed by the user. Because this code totally relies on |
| 1384 | * the user's input, the driver can't ensure the validity |
| 1385 | * of this accounting. |
| 1386 | */ |
Oded Gabbay | 7f070c9 | 2020-11-09 09:48:31 +0200 | [diff] [blame] | 1387 | if (!hdev->asic_prop.dram_supports_virtual_memory) { |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1388 | atomic64_add(args->in.alloc.mem_size, |
| 1389 | &ctx->dram_phys_mem); |
| 1390 | atomic64_add(args->in.alloc.mem_size, |
| 1391 | &hdev->dram_used_mem); |
| 1392 | |
| 1393 | dev_dbg(hdev->dev, "DRAM alloc is not supported\n"); |
| 1394 | rc = 0; |
| 1395 | |
| 1396 | memset(args, 0, sizeof(*args)); |
| 1397 | args->out.handle = 0; |
| 1398 | goto out; |
| 1399 | } |
| 1400 | |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1401 | rc = alloc_device_memory(ctx, &args->in, &handle); |
| 1402 | |
| 1403 | memset(args, 0, sizeof(*args)); |
| 1404 | args->out.handle = (__u64) handle; |
| 1405 | break; |
| 1406 | |
| 1407 | case HL_MEM_OP_FREE: |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1408 | /* If DRAM does not support virtual memory the driver won't |
| 1409 | * handle the allocation/freeing of that memory. However, for |
| 1410 | * system administration/monitoring purposes, the driver will |
| 1411 | * keep track of the amount of DRAM memory that is allocated |
| 1412 | * and freed by the user. Because this code totally relies on |
| 1413 | * the user's input, the driver can't ensure the validity |
| 1414 | * of this accounting. |
| 1415 | */ |
Oded Gabbay | 7f070c9 | 2020-11-09 09:48:31 +0200 | [diff] [blame] | 1416 | if (!hdev->asic_prop.dram_supports_virtual_memory) { |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1417 | atomic64_sub(args->in.alloc.mem_size, |
| 1418 | &ctx->dram_phys_mem); |
| 1419 | atomic64_sub(args->in.alloc.mem_size, |
| 1420 | &hdev->dram_used_mem); |
| 1421 | |
| 1422 | dev_dbg(hdev->dev, "DRAM alloc is not supported\n"); |
| 1423 | rc = 0; |
| 1424 | |
| 1425 | goto out; |
| 1426 | } |
| 1427 | |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1428 | rc = free_device_memory(ctx, &args->in); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1429 | break; |
| 1430 | |
| 1431 | case HL_MEM_OP_MAP: |
| 1432 | rc = map_device_va(ctx, &args->in, &device_addr); |
| 1433 | |
| 1434 | memset(args, 0, sizeof(*args)); |
| 1435 | args->out.device_virt_addr = device_addr; |
| 1436 | break; |
| 1437 | |
| 1438 | case HL_MEM_OP_UNMAP: |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1439 | rc = unmap_device_va(ctx, &args->in, false); |
Oded Gabbay | 54303a1 | 2019-04-04 14:42:26 +0300 | [diff] [blame] | 1440 | break; |
| 1441 | |
| 1442 | default: |
| 1443 | dev_err(hdev->dev, "Unknown opcode for memory IOCTL\n"); |
| 1444 | rc = -ENOTTY; |
| 1445 | break; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | out: |
| 1449 | return rc; |
| 1450 | } |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1451 | |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1452 | static int get_user_memory(struct hl_device *hdev, u64 addr, u64 size, |
| 1453 | u32 npages, u64 start, u32 offset, |
| 1454 | struct hl_userptr *userptr) |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1455 | { |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1456 | int rc; |
| 1457 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1458 | if (!access_ok((void __user *) (uintptr_t) addr, size)) { |
Oded Gabbay | 230afe7 | 2019-02-27 00:19:18 +0200 | [diff] [blame] | 1459 | dev_err(hdev->dev, "user pointer is invalid - 0x%llx\n", addr); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1460 | return -EFAULT; |
| 1461 | } |
| 1462 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1463 | userptr->vec = frame_vector_create(npages); |
| 1464 | if (!userptr->vec) { |
| 1465 | dev_err(hdev->dev, "Failed to create frame vector\n"); |
| 1466 | return -ENOMEM; |
| 1467 | } |
| 1468 | |
| 1469 | rc = get_vaddr_frames(start, npages, FOLL_FORCE | FOLL_WRITE, |
| 1470 | userptr->vec); |
| 1471 | |
| 1472 | if (rc != npages) { |
| 1473 | dev_err(hdev->dev, |
| 1474 | "Failed to map host memory, user ptr probably wrong\n"); |
| 1475 | if (rc < 0) |
| 1476 | goto destroy_framevec; |
| 1477 | rc = -EFAULT; |
| 1478 | goto put_framevec; |
| 1479 | } |
| 1480 | |
| 1481 | if (frame_vector_to_pages(userptr->vec) < 0) { |
| 1482 | dev_err(hdev->dev, |
| 1483 | "Failed to translate frame vector to pages\n"); |
| 1484 | rc = -EFAULT; |
| 1485 | goto put_framevec; |
| 1486 | } |
| 1487 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1488 | rc = sg_alloc_table_from_pages(userptr->sgt, |
| 1489 | frame_vector_pages(userptr->vec), |
| 1490 | npages, offset, size, GFP_ATOMIC); |
| 1491 | if (rc < 0) { |
| 1492 | dev_err(hdev->dev, "failed to create SG table from pages\n"); |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1493 | goto put_framevec; |
| 1494 | } |
| 1495 | |
| 1496 | return 0; |
| 1497 | |
| 1498 | put_framevec: |
| 1499 | put_vaddr_frames(userptr->vec); |
| 1500 | destroy_framevec: |
| 1501 | frame_vector_destroy(userptr->vec); |
| 1502 | return rc; |
| 1503 | } |
| 1504 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1505 | /** |
| 1506 | * hl_pin_host_memory() - pins a chunk of host memory. |
| 1507 | * @hdev: pointer to the habanalabs device structure. |
| 1508 | * @addr: the host virtual address of the memory area. |
| 1509 | * @size: the size of the memory area. |
| 1510 | * @userptr: pointer to hl_userptr structure. |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1511 | * |
| 1512 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1513 | * - Pins the physical pages. |
| 1514 | * - Create an SG list from those pages. |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1515 | */ |
| 1516 | int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size, |
| 1517 | struct hl_userptr *userptr) |
| 1518 | { |
| 1519 | u64 start, end; |
| 1520 | u32 npages, offset; |
| 1521 | int rc; |
| 1522 | |
| 1523 | if (!size) { |
| 1524 | dev_err(hdev->dev, "size to pin is invalid - %llu\n", size); |
| 1525 | return -EINVAL; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * If the combination of the address and size requested for this memory |
| 1530 | * region causes an integer overflow, return error. |
| 1531 | */ |
| 1532 | if (((addr + size) < addr) || |
| 1533 | PAGE_ALIGN(addr + size) < (addr + size)) { |
| 1534 | dev_err(hdev->dev, |
| 1535 | "user pointer 0x%llx + %llu causes integer overflow\n", |
| 1536 | addr, size); |
| 1537 | return -EINVAL; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * This function can be called also from data path, hence use atomic |
| 1542 | * always as it is not a big allocation. |
| 1543 | */ |
| 1544 | userptr->sgt = kzalloc(sizeof(*userptr->sgt), GFP_ATOMIC); |
| 1545 | if (!userptr->sgt) |
| 1546 | return -ENOMEM; |
| 1547 | |
| 1548 | start = addr & PAGE_MASK; |
| 1549 | offset = addr & ~PAGE_MASK; |
| 1550 | end = PAGE_ALIGN(addr + size); |
| 1551 | npages = (end - start) >> PAGE_SHIFT; |
| 1552 | |
| 1553 | userptr->size = size; |
| 1554 | userptr->addr = addr; |
| 1555 | userptr->dma_mapped = false; |
| 1556 | INIT_LIST_HEAD(&userptr->job_node); |
| 1557 | |
| 1558 | rc = get_user_memory(hdev, addr, size, npages, start, offset, |
| 1559 | userptr); |
| 1560 | if (rc) { |
| 1561 | dev_err(hdev->dev, |
| 1562 | "failed to get user memory for address 0x%llx\n", |
| 1563 | addr); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1564 | goto free_sgt; |
| 1565 | } |
| 1566 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 1567 | hl_debugfs_add_userptr(hdev, userptr); |
| 1568 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1569 | return 0; |
| 1570 | |
| 1571 | free_sgt: |
| 1572 | kfree(userptr->sgt); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1573 | return rc; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1577 | * hl_unpin_host_memory - unpins a chunk of host memory. |
| 1578 | * @hdev: pointer to the habanalabs device structure |
| 1579 | * @userptr: pointer to hl_userptr structure |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1580 | * |
| 1581 | * This function does the following: |
| 1582 | * - Unpins the physical pages related to the host memory |
| 1583 | * - Free the SG list |
| 1584 | */ |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1585 | 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] | 1586 | { |
| 1587 | struct page **pages; |
| 1588 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 1589 | hl_debugfs_remove_userptr(hdev, userptr); |
| 1590 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1591 | if (userptr->dma_mapped) |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1592 | hdev->asic_funcs->hl_dma_unmap_sg(hdev, userptr->sgt->sgl, |
| 1593 | userptr->sgt->nents, |
| 1594 | userptr->dir); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1595 | |
| 1596 | pages = frame_vector_pages(userptr->vec); |
| 1597 | if (!IS_ERR(pages)) { |
| 1598 | int i; |
| 1599 | |
| 1600 | for (i = 0; i < frame_vector_count(userptr->vec); i++) |
| 1601 | set_page_dirty_lock(pages[i]); |
| 1602 | } |
| 1603 | put_vaddr_frames(userptr->vec); |
| 1604 | frame_vector_destroy(userptr->vec); |
| 1605 | |
| 1606 | list_del(&userptr->job_node); |
| 1607 | |
| 1608 | sg_free_table(userptr->sgt); |
| 1609 | kfree(userptr->sgt); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1610 | } |
| 1611 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1612 | /** |
| 1613 | * hl_userptr_delete_list() - clear userptr list. |
| 1614 | * @hdev: pointer to the habanalabs device structure. |
| 1615 | * @userptr_list: pointer to the list to clear. |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1616 | * |
| 1617 | * This function does the following: |
| 1618 | * - Iterates over the list and unpins the host memory and frees the userptr |
| 1619 | * structure. |
| 1620 | */ |
| 1621 | void hl_userptr_delete_list(struct hl_device *hdev, |
| 1622 | struct list_head *userptr_list) |
| 1623 | { |
| 1624 | struct hl_userptr *userptr, *tmp; |
| 1625 | |
| 1626 | list_for_each_entry_safe(userptr, tmp, userptr_list, job_node) { |
| 1627 | hl_unpin_host_memory(hdev, userptr); |
| 1628 | kfree(userptr); |
| 1629 | } |
| 1630 | |
| 1631 | INIT_LIST_HEAD(userptr_list); |
| 1632 | } |
| 1633 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1634 | /** |
| 1635 | * hl_userptr_is_pinned() - returns whether the given userptr is pinned. |
| 1636 | * @hdev: pointer to the habanalabs device structure. |
| 1637 | * @userptr_list: pointer to the list to clear. |
| 1638 | * @userptr: pointer to userptr to check. |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 1639 | * |
| 1640 | * This function does the following: |
| 1641 | * - Iterates over the list and checks if the given userptr is in it, means is |
| 1642 | * pinned. If so, returns true, otherwise returns false. |
| 1643 | */ |
| 1644 | bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, |
| 1645 | u32 size, struct list_head *userptr_list, |
| 1646 | struct hl_userptr **userptr) |
| 1647 | { |
| 1648 | list_for_each_entry((*userptr), userptr_list, job_node) { |
| 1649 | if ((addr == (*userptr)->addr) && (size == (*userptr)->size)) |
| 1650 | return true; |
| 1651 | } |
| 1652 | |
| 1653 | return false; |
| 1654 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1655 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1656 | /** |
| 1657 | * va_range_init() - initialize virtual addresses range. |
| 1658 | * @hdev: pointer to the habanalabs device structure. |
| 1659 | * @va_range: pointer to the range to initialize. |
| 1660 | * @start: range start address. |
| 1661 | * @end: range end address. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1662 | * |
| 1663 | * This function does the following: |
| 1664 | * - Initializes the virtual addresses list of the given range with the given |
| 1665 | * addresses. |
| 1666 | */ |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1667 | 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] | 1668 | u64 start, u64 end, u32 page_size) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1669 | { |
| 1670 | int rc; |
| 1671 | |
| 1672 | INIT_LIST_HEAD(&va_range->list); |
| 1673 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1674 | /* |
| 1675 | * PAGE_SIZE alignment |
| 1676 | * it is the callers responsibility to align the addresses if the |
| 1677 | * page size is not a power of 2 |
| 1678 | */ |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1679 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1680 | if (is_power_of_2(page_size)) { |
| 1681 | if (start & (PAGE_SIZE - 1)) { |
| 1682 | start &= PAGE_MASK; |
| 1683 | start += PAGE_SIZE; |
| 1684 | } |
| 1685 | |
| 1686 | if (end & (PAGE_SIZE - 1)) |
| 1687 | end &= PAGE_MASK; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1688 | } |
| 1689 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1690 | if (start >= end) { |
| 1691 | dev_err(hdev->dev, "too small vm range for va list\n"); |
| 1692 | return -EFAULT; |
| 1693 | } |
| 1694 | |
| 1695 | rc = add_va_block(hdev, va_range, start, end); |
| 1696 | |
| 1697 | if (rc) { |
| 1698 | dev_err(hdev->dev, "Failed to init host va list\n"); |
| 1699 | return rc; |
| 1700 | } |
| 1701 | |
| 1702 | va_range->start_addr = start; |
| 1703 | va_range->end_addr = end; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1704 | va_range->page_size = page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1705 | |
| 1706 | return 0; |
| 1707 | } |
| 1708 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1709 | /** |
| 1710 | * va_range_fini() - clear a virtual addresses range. |
| 1711 | * @hdev: pointer to the habanalabs structure. |
| 1712 | * va_range: pointer to virtual addresses rang.e |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1713 | * |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1714 | * This function does the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1715 | * - Frees the virtual addresses block list and its lock. |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1716 | */ |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1717 | 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] | 1718 | { |
| 1719 | mutex_lock(&va_range->lock); |
| 1720 | clear_va_list_locked(hdev, &va_range->list); |
| 1721 | mutex_unlock(&va_range->lock); |
| 1722 | |
| 1723 | mutex_destroy(&va_range->lock); |
| 1724 | kfree(va_range); |
| 1725 | } |
| 1726 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1727 | /** |
| 1728 | * vm_ctx_init_with_ranges() - initialize virtual memory for context. |
| 1729 | * @ctx: pointer to the habanalabs context structure. |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1730 | * @host_range_start: host virtual addresses range start. |
| 1731 | * @host_range_end: host virtual addresses range end. |
| 1732 | * @host_huge_range_start: host virtual addresses range start for memory |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1733 | * allocated with huge pages. |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1734 | * @host_huge_range_end: host virtual addresses range end for memory allocated |
| 1735 | * with huge pages. |
| 1736 | * @dram_range_start: dram virtual addresses range start. |
| 1737 | * @dram_range_end: dram virtual addresses range end. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1738 | * |
| 1739 | * This function initializes the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1740 | * - MMU for context. |
| 1741 | * - Virtual address to area descriptor hashtable. |
| 1742 | * - Virtual block list of available virtual memory. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1743 | */ |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1744 | static int vm_ctx_init_with_ranges(struct hl_ctx *ctx, |
| 1745 | u64 host_range_start, |
| 1746 | u64 host_range_end, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1747 | u32 host_page_size, |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1748 | u64 host_huge_range_start, |
| 1749 | u64 host_huge_range_end, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1750 | u32 host_huge_page_size, |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1751 | u64 dram_range_start, |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1752 | u64 dram_range_end, |
| 1753 | u32 dram_page_size) |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1754 | { |
| 1755 | struct hl_device *hdev = ctx->hdev; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1756 | int i, rc; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1757 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1758 | for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++) { |
| 1759 | ctx->va_range[i] = |
| 1760 | kzalloc(sizeof(struct hl_va_range), GFP_KERNEL); |
| 1761 | if (!ctx->va_range[i]) { |
| 1762 | rc = -ENOMEM; |
| 1763 | goto free_va_range; |
| 1764 | } |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Omer Shpigelman | 27ca384c | 2019-02-28 10:46:11 +0200 | [diff] [blame] | 1767 | rc = hl_mmu_ctx_init(ctx); |
| 1768 | if (rc) { |
| 1769 | dev_err(hdev->dev, "failed to init context %d\n", ctx->asid); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1770 | goto free_va_range; |
Omer Shpigelman | 27ca384c | 2019-02-28 10:46:11 +0200 | [diff] [blame] | 1771 | } |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1772 | |
| 1773 | mutex_init(&ctx->mem_hash_lock); |
| 1774 | hash_init(ctx->mem_hash); |
| 1775 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1776 | mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1777 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1778 | rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST], |
| 1779 | host_range_start, host_range_end, host_page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1780 | if (rc) { |
| 1781 | dev_err(hdev->dev, "failed to init host vm range\n"); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1782 | goto mmu_ctx_fini; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1783 | } |
| 1784 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1785 | if (hdev->pmmu_huge_range) { |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1786 | mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1787 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1788 | rc = va_range_init(hdev, |
| 1789 | ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE], |
| 1790 | host_huge_range_start, host_huge_range_end, |
| 1791 | host_huge_page_size); |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1792 | if (rc) { |
| 1793 | dev_err(hdev->dev, |
| 1794 | "failed to init host huge vm range\n"); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1795 | goto clear_host_va_range; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1796 | } |
| 1797 | } else { |
Ofir Bitton | 8e718f2 | 2020-11-26 13:01:11 +0200 | [diff] [blame] | 1798 | kfree(ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1799 | ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] = |
| 1800 | ctx->va_range[HL_VA_RANGE_TYPE_HOST]; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1803 | mutex_init(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock); |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1804 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1805 | rc = va_range_init(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM], |
| 1806 | dram_range_start, dram_range_end, dram_page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1807 | if (rc) { |
| 1808 | dev_err(hdev->dev, "failed to init dram vm range\n"); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1809 | goto clear_host_huge_va_range; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1810 | } |
| 1811 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 1812 | hl_debugfs_add_ctx_mem_hash(hdev, ctx); |
| 1813 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1814 | return 0; |
| 1815 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1816 | clear_host_huge_va_range: |
| 1817 | mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_DRAM]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1818 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1819 | if (hdev->pmmu_huge_range) { |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1820 | mutex_lock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock); |
| 1821 | clear_va_list_locked(hdev, |
| 1822 | &ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->list); |
| 1823 | mutex_unlock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock); |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1824 | } |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1825 | clear_host_va_range: |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1826 | if (hdev->pmmu_huge_range) |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1827 | mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]->lock); |
| 1828 | mutex_lock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock); |
| 1829 | clear_va_list_locked(hdev, &ctx->va_range[HL_VA_RANGE_TYPE_HOST]->list); |
| 1830 | mutex_unlock(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock); |
| 1831 | mmu_ctx_fini: |
| 1832 | mutex_destroy(&ctx->va_range[HL_VA_RANGE_TYPE_HOST]->lock); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1833 | mutex_destroy(&ctx->mem_hash_lock); |
| 1834 | hl_mmu_ctx_fini(ctx); |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1835 | free_va_range: |
| 1836 | for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++) |
| 1837 | kfree(ctx->va_range[i]); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1838 | |
| 1839 | return rc; |
| 1840 | } |
| 1841 | |
| 1842 | int hl_vm_ctx_init(struct hl_ctx *ctx) |
| 1843 | { |
| 1844 | struct asic_fixed_properties *prop = &ctx->hdev->asic_prop; |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1845 | u64 host_range_start, host_range_end, host_huge_range_start, |
| 1846 | host_huge_range_end, dram_range_start, dram_range_end; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1847 | u32 host_page_size, host_huge_page_size, dram_page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1848 | |
| 1849 | atomic64_set(&ctx->dram_phys_mem, 0); |
| 1850 | |
| 1851 | /* |
| 1852 | * - If MMU is enabled, init the ranges as usual. |
| 1853 | * - If MMU is disabled, in case of host mapping, the returned address |
| 1854 | * is the given one. |
| 1855 | * In case of DRAM mapping, the returned address is the physical |
| 1856 | * address of the memory related to the given handle. |
| 1857 | */ |
Oded Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 1858 | if (!ctx->hdev->mmu_enable) |
| 1859 | return 0; |
| 1860 | |
| 1861 | dram_range_start = prop->dmmu.start_addr; |
| 1862 | dram_range_end = prop->dmmu.end_addr; |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1863 | dram_page_size = prop->dram_page_size ? |
| 1864 | prop->dram_page_size : prop->dmmu.page_size; |
Oded Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 1865 | host_range_start = prop->pmmu.start_addr; |
| 1866 | host_range_end = prop->pmmu.end_addr; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1867 | host_page_size = prop->pmmu.page_size; |
Oded Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 1868 | host_huge_range_start = prop->pmmu_huge.start_addr; |
| 1869 | host_huge_range_end = prop->pmmu_huge.end_addr; |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1870 | host_huge_page_size = prop->pmmu_huge.page_size; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1871 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1872 | 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] | 1873 | host_page_size, host_huge_range_start, |
| 1874 | host_huge_range_end, host_huge_page_size, |
| 1875 | dram_range_start, dram_range_end, dram_page_size); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1878 | /** |
| 1879 | * hl_vm_ctx_fini() - virtual memory teardown of context. |
| 1880 | * @ctx: pointer to the habanalabs context structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1881 | * |
| 1882 | * This function perform teardown the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1883 | * - Virtual block list of available virtual memory. |
| 1884 | * - Virtual address to area descriptor hashtable. |
| 1885 | * - MMU for context. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1886 | * |
| 1887 | * In addition this function does the following: |
| 1888 | * - Unmaps the existing hashtable nodes if the hashtable is not empty. The |
| 1889 | * hashtable should be empty as no valid mappings should exist at this |
| 1890 | * point. |
| 1891 | * - Frees any existing physical page list from the idr which relates to the |
| 1892 | * current context asid. |
| 1893 | * - This function checks the virtual block list for correctness. At this point |
| 1894 | * the list should contain one element which describes the whole virtual |
| 1895 | * memory range of the context. Otherwise, a warning is printed. |
| 1896 | */ |
| 1897 | void hl_vm_ctx_fini(struct hl_ctx *ctx) |
| 1898 | { |
| 1899 | struct hl_device *hdev = ctx->hdev; |
| 1900 | struct hl_vm *vm = &hdev->vm; |
| 1901 | struct hl_vm_phys_pg_pack *phys_pg_list; |
| 1902 | struct hl_vm_hash_node *hnode; |
| 1903 | struct hlist_node *tmp_node; |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1904 | struct hl_mem_in args; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1905 | int i; |
| 1906 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1907 | if (!hdev->mmu_enable) |
Oded Gabbay | f3a965c | 2020-10-04 23:00:39 +0300 | [diff] [blame] | 1908 | return; |
| 1909 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 1910 | hl_debugfs_remove_ctx_mem_hash(hdev, ctx); |
| 1911 | |
Omer Shpigelman | e604f55 | 2019-11-14 18:23:59 +0000 | [diff] [blame] | 1912 | /* |
| 1913 | * Clearly something went wrong on hard reset so no point in printing |
| 1914 | * another side effect error |
| 1915 | */ |
| 1916 | if (!hdev->hard_reset_pending && !hash_empty(ctx->mem_hash)) |
| 1917 | dev_notice(hdev->dev, |
Oded Gabbay | 0eab4f8 | 2020-06-22 09:52:22 +0300 | [diff] [blame] | 1918 | "user released device without removing its memory mappings\n"); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1919 | |
| 1920 | hash_for_each_safe(ctx->mem_hash, i, tmp_node, hnode, node) { |
| 1921 | dev_dbg(hdev->dev, |
| 1922 | "hl_mem_hash_node of vaddr 0x%llx of asid %d is still alive\n", |
| 1923 | hnode->vaddr, ctx->asid); |
Omer Shpigelman | f19040c | 2020-12-09 13:34:11 +0200 | [diff] [blame] | 1924 | args.unmap.device_virt_addr = hnode->vaddr; |
| 1925 | unmap_device_va(ctx, &args, true); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1926 | } |
| 1927 | |
Ohad Sharabi | cb6ef0e | 2020-11-26 09:39:26 +0200 | [diff] [blame] | 1928 | mutex_lock(&ctx->mmu_lock); |
| 1929 | |
Omer Shpigelman | bea84c4 | 2019-11-14 18:23:58 +0000 | [diff] [blame] | 1930 | /* invalidate the cache once after the unmapping loop */ |
| 1931 | hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR); |
| 1932 | hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_PHYS_PACK); |
| 1933 | |
Ohad Sharabi | cb6ef0e | 2020-11-26 09:39:26 +0200 | [diff] [blame] | 1934 | mutex_unlock(&ctx->mmu_lock); |
| 1935 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1936 | spin_lock(&vm->idr_lock); |
| 1937 | idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_list, i) |
| 1938 | if (phys_pg_list->asid == ctx->asid) { |
| 1939 | dev_dbg(hdev->dev, |
Omer Shpigelman | 7f74d4d | 2019-08-12 11:48:46 +0300 | [diff] [blame] | 1940 | "page list 0x%px of asid %d is still alive\n", |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1941 | phys_pg_list, ctx->asid); |
Tomer Tayar | c811375 | 2019-08-04 07:03:41 +0000 | [diff] [blame] | 1942 | atomic64_sub(phys_pg_list->total_size, |
| 1943 | &hdev->dram_used_mem); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1944 | free_phys_pg_pack(hdev, phys_pg_list); |
| 1945 | idr_remove(&vm->phys_pg_pack_handles, i); |
| 1946 | } |
| 1947 | spin_unlock(&vm->idr_lock); |
| 1948 | |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1949 | va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM]); |
Ofir Bitton | 8e718f2 | 2020-11-26 13:01:11 +0200 | [diff] [blame] | 1950 | va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST]); |
| 1951 | |
Omer Shpigelman | 64a7e29 | 2020-01-05 09:05:45 +0000 | [diff] [blame] | 1952 | if (hdev->pmmu_huge_range) |
Ofir Bitton | 784b916 | 2020-10-22 11:05:55 +0300 | [diff] [blame] | 1953 | 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] | 1954 | |
| 1955 | mutex_destroy(&ctx->mem_hash_lock); |
| 1956 | hl_mmu_ctx_fini(ctx); |
Oded Gabbay | 3e62299 | 2020-10-18 15:32:23 +0300 | [diff] [blame] | 1957 | |
| 1958 | /* In this case we need to clear the global accounting of DRAM usage |
| 1959 | * because the user notifies us on allocations. If the user is no more, |
| 1960 | * all DRAM is available |
| 1961 | */ |
Ofir Bitton | 8e39e75 | 2020-11-12 11:03:32 +0200 | [diff] [blame] | 1962 | if (ctx->asid != HL_KERNEL_ASID_ID && |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1963 | !hdev->asic_prop.dram_supports_virtual_memory) |
| 1964 | atomic64_set(&hdev->dram_used_mem, 0); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1967 | /** |
| 1968 | * hl_vm_init() - initialize virtual memory module. |
| 1969 | * @hdev: pointer to the habanalabs device structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1970 | * |
| 1971 | * This function initializes the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 1972 | * - MMU module. |
| 1973 | * - DRAM physical pages pool of 2MB. |
| 1974 | * - Idr for device memory allocation handles. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1975 | */ |
| 1976 | int hl_vm_init(struct hl_device *hdev) |
| 1977 | { |
| 1978 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
| 1979 | struct hl_vm *vm = &hdev->vm; |
| 1980 | int rc; |
| 1981 | |
Moti Haimovski | b19dc67 | 2020-11-18 20:15:29 +0200 | [diff] [blame] | 1982 | if (is_power_of_2(prop->dram_page_size)) |
| 1983 | vm->dram_pg_pool = |
| 1984 | gen_pool_create(__ffs(prop->dram_page_size), -1); |
| 1985 | else |
| 1986 | vm->dram_pg_pool = |
| 1987 | gen_pool_create(__ffs(DRAM_POOL_PAGE_SIZE), -1); |
| 1988 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1989 | if (!vm->dram_pg_pool) { |
| 1990 | dev_err(hdev->dev, "Failed to create dram page pool\n"); |
Oded Gabbay | 37d68ce | 2019-05-29 14:43:04 +0300 | [diff] [blame] | 1991 | return -ENOMEM; |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | kref_init(&vm->dram_pg_pool_refcount); |
| 1995 | |
| 1996 | rc = gen_pool_add(vm->dram_pg_pool, prop->dram_user_base_address, |
| 1997 | prop->dram_end_address - prop->dram_user_base_address, |
| 1998 | -1); |
| 1999 | |
| 2000 | if (rc) { |
| 2001 | dev_err(hdev->dev, |
| 2002 | "Failed to add memory to dram page pool %d\n", rc); |
| 2003 | goto pool_add_err; |
| 2004 | } |
| 2005 | |
| 2006 | spin_lock_init(&vm->idr_lock); |
| 2007 | idr_init(&vm->phys_pg_pack_handles); |
| 2008 | |
| 2009 | atomic64_set(&hdev->dram_used_mem, 0); |
| 2010 | |
| 2011 | vm->init_done = true; |
| 2012 | |
| 2013 | return 0; |
| 2014 | |
| 2015 | pool_add_err: |
| 2016 | gen_pool_destroy(vm->dram_pg_pool); |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2017 | |
| 2018 | return rc; |
| 2019 | } |
| 2020 | |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2021 | /** |
| 2022 | * hl_vm_fini() - virtual memory module teardown. |
| 2023 | * @hdev: pointer to the habanalabs device structure. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2024 | * |
| 2025 | * This function perform teardown to the following: |
Omer Shpigelman | 3b762f5 | 2020-12-09 13:28:46 +0200 | [diff] [blame] | 2026 | * - Idr for device memory allocation handles. |
| 2027 | * - DRAM physical pages pool of 2MB. |
| 2028 | * - MMU module. |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2029 | */ |
| 2030 | void hl_vm_fini(struct hl_device *hdev) |
| 2031 | { |
| 2032 | struct hl_vm *vm = &hdev->vm; |
| 2033 | |
| 2034 | if (!vm->init_done) |
| 2035 | return; |
| 2036 | |
| 2037 | /* |
| 2038 | * At this point all the contexts should be freed and hence no DRAM |
| 2039 | * memory should be in use. Hence the DRAM pool should be freed here. |
| 2040 | */ |
| 2041 | if (kref_put(&vm->dram_pg_pool_refcount, dram_pg_pool_do_release) != 1) |
| 2042 | dev_warn(hdev->dev, "dram_pg_pool was not destroyed on %s\n", |
| 2043 | __func__); |
| 2044 | |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 2045 | vm->init_done = false; |
| 2046 | } |