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