1 1.12 riastrad /* $NetBSD: amdgpu_vm.c,v 1.12 2021/12/19 12:38:24 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2008 Advanced Micro Devices, Inc. 5 1.1 riastrad * Copyright 2008 Red Hat Inc. 6 1.1 riastrad * Copyright 2009 Jerome Glisse. 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 10 1.1 riastrad * to deal in the Software without restriction, including without limitation 11 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 13 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice shall be included in 16 1.1 riastrad * all copies or substantial portions of the Software. 17 1.1 riastrad * 18 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 25 1.1 riastrad * 26 1.1 riastrad * Authors: Dave Airlie 27 1.1 riastrad * Alex Deucher 28 1.1 riastrad * Jerome Glisse 29 1.1 riastrad */ 30 1.1 riastrad #include <sys/cdefs.h> 31 1.12 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_vm.c,v 1.12 2021/12/19 12:38:24 riastradh Exp $"); 32 1.5 riastrad 33 1.5 riastrad #include <linux/dma-fence-array.h> 34 1.5 riastrad #include <linux/interval_tree_generic.h> 35 1.5 riastrad #include <linux/idr.h> 36 1.1 riastrad 37 1.1 riastrad #include <drm/amdgpu_drm.h> 38 1.1 riastrad #include "amdgpu.h" 39 1.1 riastrad #include "amdgpu_trace.h" 40 1.5 riastrad #include "amdgpu_amdkfd.h" 41 1.5 riastrad #include "amdgpu_gmc.h" 42 1.5 riastrad #include "amdgpu_xgmi.h" 43 1.1 riastrad 44 1.4 riastrad #include <linux/nbsd-namespace.h> 45 1.5 riastrad /** 46 1.5 riastrad * DOC: GPUVM 47 1.5 riastrad * 48 1.1 riastrad * GPUVM is similar to the legacy gart on older asics, however 49 1.1 riastrad * rather than there being a single global gart table 50 1.1 riastrad * for the entire GPU, there are multiple VM page tables active 51 1.1 riastrad * at any given time. The VM page tables can contain a mix 52 1.1 riastrad * vram pages and system memory pages and system memory pages 53 1.1 riastrad * can be mapped as snooped (cached system pages) or unsnooped 54 1.1 riastrad * (uncached system pages). 55 1.1 riastrad * Each VM has an ID associated with it and there is a page table 56 1.1 riastrad * associated with each VMID. When execting a command buffer, 57 1.1 riastrad * the kernel tells the the ring what VMID to use for that command 58 1.1 riastrad * buffer. VMIDs are allocated dynamically as commands are submitted. 59 1.1 riastrad * The userspace drivers maintain their own address space and the kernel 60 1.1 riastrad * sets up their pages tables accordingly when they submit their 61 1.1 riastrad * command buffers and a VMID is assigned. 62 1.1 riastrad * Cayman/Trinity support up to 8 active VMs at any given time; 63 1.1 riastrad * SI supports 16. 64 1.1 riastrad */ 65 1.1 riastrad 66 1.5 riastrad #define START(node) ((node)->start) 67 1.5 riastrad #define LAST(node) ((node)->last) 68 1.5 riastrad 69 1.5 riastrad INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last, 70 1.5 riastrad START, LAST, static, amdgpu_vm_it) 71 1.5 riastrad 72 1.5 riastrad #undef START 73 1.5 riastrad #undef LAST 74 1.5 riastrad 75 1.5 riastrad /** 76 1.5 riastrad * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback 77 1.5 riastrad */ 78 1.5 riastrad struct amdgpu_prt_cb { 79 1.5 riastrad 80 1.5 riastrad /** 81 1.5 riastrad * @adev: amdgpu device 82 1.5 riastrad */ 83 1.5 riastrad struct amdgpu_device *adev; 84 1.5 riastrad 85 1.5 riastrad /** 86 1.5 riastrad * @cb: callback 87 1.5 riastrad */ 88 1.5 riastrad struct dma_fence_cb cb; 89 1.5 riastrad }; 90 1.5 riastrad 91 1.5 riastrad /** 92 1.5 riastrad * vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS 93 1.5 riastrad * happens while holding this lock anywhere to prevent deadlocks when 94 1.5 riastrad * an MMU notifier runs in reclaim-FS context. 95 1.5 riastrad */ 96 1.5 riastrad static inline void amdgpu_vm_eviction_lock(struct amdgpu_vm *vm) 97 1.5 riastrad { 98 1.5 riastrad mutex_lock(&vm->eviction_lock); 99 1.5 riastrad vm->saved_flags = memalloc_nofs_save(); 100 1.5 riastrad } 101 1.5 riastrad 102 1.5 riastrad static inline int amdgpu_vm_eviction_trylock(struct amdgpu_vm *vm) 103 1.5 riastrad { 104 1.5 riastrad if (mutex_trylock(&vm->eviction_lock)) { 105 1.5 riastrad vm->saved_flags = memalloc_nofs_save(); 106 1.5 riastrad return 1; 107 1.5 riastrad } 108 1.5 riastrad return 0; 109 1.5 riastrad } 110 1.5 riastrad 111 1.5 riastrad static inline void amdgpu_vm_eviction_unlock(struct amdgpu_vm *vm) 112 1.5 riastrad { 113 1.5 riastrad memalloc_nofs_restore(vm->saved_flags); 114 1.5 riastrad mutex_unlock(&vm->eviction_lock); 115 1.5 riastrad } 116 1.5 riastrad 117 1.1 riastrad /** 118 1.5 riastrad * amdgpu_vm_level_shift - return the addr shift for each level 119 1.1 riastrad * 120 1.1 riastrad * @adev: amdgpu_device pointer 121 1.5 riastrad * @level: VMPT level 122 1.1 riastrad * 123 1.5 riastrad * Returns: 124 1.5 riastrad * The number of bits the pfn needs to be right shifted for a level. 125 1.1 riastrad */ 126 1.5 riastrad static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev, 127 1.5 riastrad unsigned level) 128 1.1 riastrad { 129 1.5 riastrad unsigned shift = 0xff; 130 1.5 riastrad 131 1.5 riastrad switch (level) { 132 1.5 riastrad case AMDGPU_VM_PDB2: 133 1.5 riastrad case AMDGPU_VM_PDB1: 134 1.5 riastrad case AMDGPU_VM_PDB0: 135 1.5 riastrad shift = 9 * (AMDGPU_VM_PDB0 - level) + 136 1.5 riastrad adev->vm_manager.block_size; 137 1.5 riastrad break; 138 1.5 riastrad case AMDGPU_VM_PTB: 139 1.5 riastrad shift = 0; 140 1.5 riastrad break; 141 1.5 riastrad default: 142 1.5 riastrad dev_err(adev->dev, "the level%d isn't supported.\n", level); 143 1.5 riastrad } 144 1.5 riastrad 145 1.5 riastrad return shift; 146 1.1 riastrad } 147 1.1 riastrad 148 1.1 riastrad /** 149 1.5 riastrad * amdgpu_vm_num_entries - return the number of entries in a PD/PT 150 1.1 riastrad * 151 1.1 riastrad * @adev: amdgpu_device pointer 152 1.5 riastrad * @level: VMPT level 153 1.1 riastrad * 154 1.5 riastrad * Returns: 155 1.5 riastrad * The number of entries in a page directory or page table. 156 1.1 riastrad */ 157 1.5 riastrad static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev, 158 1.5 riastrad unsigned level) 159 1.1 riastrad { 160 1.5 riastrad unsigned shift = amdgpu_vm_level_shift(adev, 161 1.5 riastrad adev->vm_manager.root_level); 162 1.5 riastrad 163 1.5 riastrad if (level == adev->vm_manager.root_level) 164 1.5 riastrad /* For the root directory */ 165 1.5 riastrad return round_up(adev->vm_manager.max_pfn, 1ULL << shift) 166 1.5 riastrad >> shift; 167 1.5 riastrad else if (level != AMDGPU_VM_PTB) 168 1.5 riastrad /* Everything in between */ 169 1.5 riastrad return 512; 170 1.5 riastrad else 171 1.5 riastrad /* For the page tables on the leaves */ 172 1.5 riastrad return AMDGPU_VM_PTE_COUNT(adev); 173 1.1 riastrad } 174 1.1 riastrad 175 1.1 riastrad /** 176 1.5 riastrad * amdgpu_vm_num_ats_entries - return the number of ATS entries in the root PD 177 1.1 riastrad * 178 1.5 riastrad * @adev: amdgpu_device pointer 179 1.1 riastrad * 180 1.5 riastrad * Returns: 181 1.5 riastrad * The number of entries in the root page directory which needs the ATS setting. 182 1.1 riastrad */ 183 1.5 riastrad static unsigned amdgpu_vm_num_ats_entries(struct amdgpu_device *adev) 184 1.5 riastrad { 185 1.5 riastrad unsigned shift; 186 1.5 riastrad 187 1.5 riastrad shift = amdgpu_vm_level_shift(adev, adev->vm_manager.root_level); 188 1.5 riastrad return AMDGPU_GMC_HOLE_START >> (shift + AMDGPU_GPU_PAGE_SHIFT); 189 1.5 riastrad } 190 1.1 riastrad 191 1.5 riastrad /** 192 1.5 riastrad * amdgpu_vm_entries_mask - the mask to get the entry number of a PD/PT 193 1.5 riastrad * 194 1.5 riastrad * @adev: amdgpu_device pointer 195 1.5 riastrad * @level: VMPT level 196 1.5 riastrad * 197 1.5 riastrad * Returns: 198 1.5 riastrad * The mask to extract the entry number of a PD/PT from an address. 199 1.5 riastrad */ 200 1.5 riastrad static uint32_t amdgpu_vm_entries_mask(struct amdgpu_device *adev, 201 1.5 riastrad unsigned int level) 202 1.5 riastrad { 203 1.5 riastrad if (level <= adev->vm_manager.root_level) 204 1.5 riastrad return 0xffffffff; 205 1.5 riastrad else if (level != AMDGPU_VM_PTB) 206 1.5 riastrad return 0x1ff; 207 1.5 riastrad else 208 1.5 riastrad return AMDGPU_VM_PTE_COUNT(adev) - 1; 209 1.5 riastrad } 210 1.1 riastrad 211 1.5 riastrad /** 212 1.5 riastrad * amdgpu_vm_bo_size - returns the size of the BOs in bytes 213 1.5 riastrad * 214 1.5 riastrad * @adev: amdgpu_device pointer 215 1.5 riastrad * @level: VMPT level 216 1.5 riastrad * 217 1.5 riastrad * Returns: 218 1.5 riastrad * The size of the BO for a page directory or page table in bytes. 219 1.5 riastrad */ 220 1.5 riastrad static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level) 221 1.5 riastrad { 222 1.5 riastrad return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8); 223 1.5 riastrad } 224 1.1 riastrad 225 1.5 riastrad /** 226 1.5 riastrad * amdgpu_vm_bo_evicted - vm_bo is evicted 227 1.5 riastrad * 228 1.5 riastrad * @vm_bo: vm_bo which is evicted 229 1.5 riastrad * 230 1.5 riastrad * State for PDs/PTs and per VM BOs which are not at the location they should 231 1.5 riastrad * be. 232 1.5 riastrad */ 233 1.5 riastrad static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo) 234 1.5 riastrad { 235 1.5 riastrad struct amdgpu_vm *vm = vm_bo->vm; 236 1.5 riastrad struct amdgpu_bo *bo = vm_bo->bo; 237 1.1 riastrad 238 1.5 riastrad vm_bo->moved = true; 239 1.5 riastrad if (bo->tbo.type == ttm_bo_type_kernel) 240 1.5 riastrad list_move(&vm_bo->vm_status, &vm->evicted); 241 1.5 riastrad else 242 1.5 riastrad list_move_tail(&vm_bo->vm_status, &vm->evicted); 243 1.1 riastrad } 244 1.1 riastrad 245 1.1 riastrad /** 246 1.5 riastrad * amdgpu_vm_bo_relocated - vm_bo is reloacted 247 1.1 riastrad * 248 1.5 riastrad * @vm_bo: vm_bo which is relocated 249 1.5 riastrad * 250 1.5 riastrad * State for PDs/PTs which needs to update their parent PD. 251 1.5 riastrad */ 252 1.5 riastrad static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo) 253 1.5 riastrad { 254 1.5 riastrad list_move(&vm_bo->vm_status, &vm_bo->vm->relocated); 255 1.5 riastrad } 256 1.5 riastrad 257 1.5 riastrad /** 258 1.5 riastrad * amdgpu_vm_bo_moved - vm_bo is moved 259 1.1 riastrad * 260 1.5 riastrad * @vm_bo: vm_bo which is moved 261 1.1 riastrad * 262 1.5 riastrad * State for per VM BOs which are moved, but that change is not yet reflected 263 1.5 riastrad * in the page tables. 264 1.1 riastrad */ 265 1.5 riastrad static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo) 266 1.1 riastrad { 267 1.5 riastrad list_move(&vm_bo->vm_status, &vm_bo->vm->moved); 268 1.5 riastrad } 269 1.1 riastrad 270 1.5 riastrad /** 271 1.5 riastrad * amdgpu_vm_bo_idle - vm_bo is idle 272 1.5 riastrad * 273 1.5 riastrad * @vm_bo: vm_bo which is now idle 274 1.5 riastrad * 275 1.5 riastrad * State for PDs/PTs and per VM BOs which have gone through the state machine 276 1.5 riastrad * and are now idle. 277 1.5 riastrad */ 278 1.5 riastrad static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo) 279 1.5 riastrad { 280 1.5 riastrad list_move(&vm_bo->vm_status, &vm_bo->vm->idle); 281 1.5 riastrad vm_bo->moved = false; 282 1.5 riastrad } 283 1.1 riastrad 284 1.5 riastrad /** 285 1.5 riastrad * amdgpu_vm_bo_invalidated - vm_bo is invalidated 286 1.5 riastrad * 287 1.5 riastrad * @vm_bo: vm_bo which is now invalidated 288 1.5 riastrad * 289 1.5 riastrad * State for normal BOs which are invalidated and that change not yet reflected 290 1.5 riastrad * in the PTs. 291 1.5 riastrad */ 292 1.5 riastrad static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo) 293 1.5 riastrad { 294 1.5 riastrad spin_lock(&vm_bo->vm->invalidated_lock); 295 1.5 riastrad list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated); 296 1.5 riastrad spin_unlock(&vm_bo->vm->invalidated_lock); 297 1.5 riastrad } 298 1.1 riastrad 299 1.5 riastrad /** 300 1.5 riastrad * amdgpu_vm_bo_done - vm_bo is done 301 1.5 riastrad * 302 1.5 riastrad * @vm_bo: vm_bo which is now done 303 1.5 riastrad * 304 1.5 riastrad * State for normal BOs which are invalidated and that change has been updated 305 1.5 riastrad * in the PTs. 306 1.5 riastrad */ 307 1.5 riastrad static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo) 308 1.5 riastrad { 309 1.5 riastrad spin_lock(&vm_bo->vm->invalidated_lock); 310 1.5 riastrad list_del_init(&vm_bo->vm_status); 311 1.5 riastrad spin_unlock(&vm_bo->vm->invalidated_lock); 312 1.5 riastrad } 313 1.1 riastrad 314 1.5 riastrad /** 315 1.5 riastrad * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm 316 1.5 riastrad * 317 1.5 riastrad * @base: base structure for tracking BO usage in a VM 318 1.5 riastrad * @vm: vm to which bo is to be added 319 1.5 riastrad * @bo: amdgpu buffer object 320 1.5 riastrad * 321 1.5 riastrad * Initialize a bo_va_base structure and add it to the appropriate lists 322 1.5 riastrad * 323 1.5 riastrad */ 324 1.5 riastrad static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base, 325 1.5 riastrad struct amdgpu_vm *vm, 326 1.5 riastrad struct amdgpu_bo *bo) 327 1.5 riastrad { 328 1.5 riastrad base->vm = vm; 329 1.5 riastrad base->bo = bo; 330 1.5 riastrad base->next = NULL; 331 1.5 riastrad INIT_LIST_HEAD(&base->vm_status); 332 1.1 riastrad 333 1.5 riastrad if (!bo) 334 1.5 riastrad return; 335 1.5 riastrad base->next = bo->vm_bo; 336 1.5 riastrad bo->vm_bo = base; 337 1.1 riastrad 338 1.5 riastrad if (bo->tbo.base.resv != vm->root.base.bo->tbo.base.resv) 339 1.5 riastrad return; 340 1.1 riastrad 341 1.5 riastrad vm->bulk_moveable = false; 342 1.5 riastrad if (bo->tbo.type == ttm_bo_type_kernel && bo->parent) 343 1.5 riastrad amdgpu_vm_bo_relocated(base); 344 1.5 riastrad else 345 1.5 riastrad amdgpu_vm_bo_idle(base); 346 1.1 riastrad 347 1.5 riastrad if (bo->preferred_domains & 348 1.5 riastrad amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)) 349 1.5 riastrad return; 350 1.1 riastrad 351 1.5 riastrad /* 352 1.5 riastrad * we checked all the prerequisites, but it looks like this per vm bo 353 1.5 riastrad * is currently evicted. add the bo to the evicted list to make sure it 354 1.5 riastrad * is validated on next vm use to avoid fault. 355 1.5 riastrad * */ 356 1.5 riastrad amdgpu_vm_bo_evicted(base); 357 1.1 riastrad } 358 1.1 riastrad 359 1.1 riastrad /** 360 1.5 riastrad * amdgpu_vm_pt_parent - get the parent page directory 361 1.1 riastrad * 362 1.5 riastrad * @pt: child page table 363 1.1 riastrad * 364 1.5 riastrad * Helper to get the parent entry for the child page table. NULL if we are at 365 1.5 riastrad * the root page directory. 366 1.1 riastrad */ 367 1.5 riastrad static struct amdgpu_vm_pt *amdgpu_vm_pt_parent(struct amdgpu_vm_pt *pt) 368 1.5 riastrad { 369 1.5 riastrad struct amdgpu_bo *parent = pt->base.bo->parent; 370 1.5 riastrad 371 1.5 riastrad if (!parent) 372 1.5 riastrad return NULL; 373 1.1 riastrad 374 1.5 riastrad return container_of(parent->vm_bo, struct amdgpu_vm_pt, base); 375 1.1 riastrad } 376 1.1 riastrad 377 1.5 riastrad /* 378 1.5 riastrad * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt 379 1.5 riastrad */ 380 1.5 riastrad struct amdgpu_vm_pt_cursor { 381 1.5 riastrad uint64_t pfn; 382 1.5 riastrad struct amdgpu_vm_pt *parent; 383 1.5 riastrad struct amdgpu_vm_pt *entry; 384 1.5 riastrad unsigned level; 385 1.5 riastrad }; 386 1.5 riastrad 387 1.1 riastrad /** 388 1.5 riastrad * amdgpu_vm_pt_start - start PD/PT walk 389 1.1 riastrad * 390 1.1 riastrad * @adev: amdgpu_device pointer 391 1.5 riastrad * @vm: amdgpu_vm structure 392 1.5 riastrad * @start: start address of the walk 393 1.5 riastrad * @cursor: state to initialize 394 1.1 riastrad * 395 1.5 riastrad * Initialize a amdgpu_vm_pt_cursor to start a walk. 396 1.1 riastrad */ 397 1.5 riastrad static void amdgpu_vm_pt_start(struct amdgpu_device *adev, 398 1.5 riastrad struct amdgpu_vm *vm, uint64_t start, 399 1.5 riastrad struct amdgpu_vm_pt_cursor *cursor) 400 1.1 riastrad { 401 1.5 riastrad cursor->pfn = start; 402 1.5 riastrad cursor->parent = NULL; 403 1.5 riastrad cursor->entry = &vm->root; 404 1.5 riastrad cursor->level = adev->vm_manager.root_level; 405 1.1 riastrad } 406 1.1 riastrad 407 1.1 riastrad /** 408 1.5 riastrad * amdgpu_vm_pt_descendant - go to child node 409 1.1 riastrad * 410 1.5 riastrad * @adev: amdgpu_device pointer 411 1.5 riastrad * @cursor: current state 412 1.1 riastrad * 413 1.5 riastrad * Walk to the child node of the current node. 414 1.5 riastrad * Returns: 415 1.5 riastrad * True if the walk was possible, false otherwise. 416 1.1 riastrad */ 417 1.5 riastrad static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev, 418 1.5 riastrad struct amdgpu_vm_pt_cursor *cursor) 419 1.1 riastrad { 420 1.5 riastrad unsigned mask, shift, idx; 421 1.5 riastrad 422 1.5 riastrad if (!cursor->entry->entries) 423 1.5 riastrad return false; 424 1.5 riastrad 425 1.5 riastrad BUG_ON(!cursor->entry->base.bo); 426 1.5 riastrad mask = amdgpu_vm_entries_mask(adev, cursor->level); 427 1.5 riastrad shift = amdgpu_vm_level_shift(adev, cursor->level); 428 1.1 riastrad 429 1.5 riastrad ++cursor->level; 430 1.5 riastrad idx = (cursor->pfn >> shift) & mask; 431 1.5 riastrad cursor->parent = cursor->entry; 432 1.5 riastrad cursor->entry = &cursor->entry->entries[idx]; 433 1.5 riastrad return true; 434 1.1 riastrad } 435 1.1 riastrad 436 1.1 riastrad /** 437 1.5 riastrad * amdgpu_vm_pt_sibling - go to sibling node 438 1.1 riastrad * 439 1.1 riastrad * @adev: amdgpu_device pointer 440 1.5 riastrad * @cursor: current state 441 1.1 riastrad * 442 1.5 riastrad * Walk to the sibling node of the current node. 443 1.5 riastrad * Returns: 444 1.5 riastrad * True if the walk was possible, false otherwise. 445 1.1 riastrad */ 446 1.5 riastrad static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev, 447 1.5 riastrad struct amdgpu_vm_pt_cursor *cursor) 448 1.1 riastrad { 449 1.5 riastrad unsigned shift, num_entries; 450 1.1 riastrad 451 1.5 riastrad /* Root doesn't have a sibling */ 452 1.5 riastrad if (!cursor->parent) 453 1.5 riastrad return false; 454 1.1 riastrad 455 1.5 riastrad /* Go to our parents and see if we got a sibling */ 456 1.5 riastrad shift = amdgpu_vm_level_shift(adev, cursor->level - 1); 457 1.5 riastrad num_entries = amdgpu_vm_num_entries(adev, cursor->level - 1); 458 1.1 riastrad 459 1.5 riastrad if (cursor->entry == &cursor->parent->entries[num_entries - 1]) 460 1.5 riastrad return false; 461 1.5 riastrad 462 1.5 riastrad cursor->pfn += 1ULL << shift; 463 1.5 riastrad cursor->pfn &= ~((1ULL << shift) - 1); 464 1.5 riastrad ++cursor->entry; 465 1.5 riastrad return true; 466 1.1 riastrad } 467 1.1 riastrad 468 1.5 riastrad /** 469 1.5 riastrad * amdgpu_vm_pt_ancestor - go to parent node 470 1.5 riastrad * 471 1.5 riastrad * @cursor: current state 472 1.5 riastrad * 473 1.5 riastrad * Walk to the parent node of the current node. 474 1.5 riastrad * Returns: 475 1.5 riastrad * True if the walk was possible, false otherwise. 476 1.5 riastrad */ 477 1.5 riastrad static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor) 478 1.1 riastrad { 479 1.5 riastrad if (!cursor->parent) 480 1.5 riastrad return false; 481 1.5 riastrad 482 1.5 riastrad --cursor->level; 483 1.5 riastrad cursor->entry = cursor->parent; 484 1.5 riastrad cursor->parent = amdgpu_vm_pt_parent(cursor->parent); 485 1.5 riastrad return true; 486 1.1 riastrad } 487 1.1 riastrad 488 1.1 riastrad /** 489 1.5 riastrad * amdgpu_vm_pt_next - get next PD/PT in hieratchy 490 1.1 riastrad * 491 1.1 riastrad * @adev: amdgpu_device pointer 492 1.5 riastrad * @cursor: current state 493 1.1 riastrad * 494 1.5 riastrad * Walk the PD/PT tree to the next node. 495 1.1 riastrad */ 496 1.5 riastrad static void amdgpu_vm_pt_next(struct amdgpu_device *adev, 497 1.5 riastrad struct amdgpu_vm_pt_cursor *cursor) 498 1.1 riastrad { 499 1.5 riastrad /* First try a newborn child */ 500 1.5 riastrad if (amdgpu_vm_pt_descendant(adev, cursor)) 501 1.5 riastrad return; 502 1.1 riastrad 503 1.5 riastrad /* If that didn't worked try to find a sibling */ 504 1.5 riastrad while (!amdgpu_vm_pt_sibling(adev, cursor)) { 505 1.5 riastrad /* No sibling, go to our parents and grandparents */ 506 1.5 riastrad if (!amdgpu_vm_pt_ancestor(cursor)) { 507 1.5 riastrad cursor->pfn = ~0ll; 508 1.5 riastrad return; 509 1.5 riastrad } 510 1.5 riastrad } 511 1.5 riastrad } 512 1.1 riastrad 513 1.5 riastrad /** 514 1.5 riastrad * amdgpu_vm_pt_first_dfs - start a deep first search 515 1.5 riastrad * 516 1.5 riastrad * @adev: amdgpu_device structure 517 1.5 riastrad * @vm: amdgpu_vm structure 518 1.5 riastrad * @start: optional cursor to start with 519 1.5 riastrad * @cursor: state to initialize 520 1.5 riastrad * 521 1.5 riastrad * Starts a deep first traversal of the PD/PT tree. 522 1.5 riastrad */ 523 1.5 riastrad static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev, 524 1.5 riastrad struct amdgpu_vm *vm, 525 1.5 riastrad struct amdgpu_vm_pt_cursor *start, 526 1.5 riastrad struct amdgpu_vm_pt_cursor *cursor) 527 1.5 riastrad { 528 1.5 riastrad if (start) 529 1.5 riastrad *cursor = *start; 530 1.5 riastrad else 531 1.5 riastrad amdgpu_vm_pt_start(adev, vm, 0, cursor); 532 1.5 riastrad while (amdgpu_vm_pt_descendant(adev, cursor)); 533 1.5 riastrad } 534 1.1 riastrad 535 1.5 riastrad /** 536 1.5 riastrad * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue 537 1.5 riastrad * 538 1.5 riastrad * @start: starting point for the search 539 1.5 riastrad * @entry: current entry 540 1.5 riastrad * 541 1.5 riastrad * Returns: 542 1.5 riastrad * True when the search should continue, false otherwise. 543 1.5 riastrad */ 544 1.5 riastrad static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start, 545 1.5 riastrad struct amdgpu_vm_pt *entry) 546 1.5 riastrad { 547 1.5 riastrad return entry && (!start || entry != start->entry); 548 1.5 riastrad } 549 1.1 riastrad 550 1.5 riastrad /** 551 1.5 riastrad * amdgpu_vm_pt_next_dfs - get the next node for a deep first search 552 1.5 riastrad * 553 1.5 riastrad * @adev: amdgpu_device structure 554 1.5 riastrad * @cursor: current state 555 1.5 riastrad * 556 1.5 riastrad * Move the cursor to the next node in a deep first search. 557 1.5 riastrad */ 558 1.5 riastrad static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev, 559 1.5 riastrad struct amdgpu_vm_pt_cursor *cursor) 560 1.5 riastrad { 561 1.5 riastrad if (!cursor->entry) 562 1.5 riastrad return; 563 1.1 riastrad 564 1.5 riastrad if (!cursor->parent) 565 1.5 riastrad cursor->entry = NULL; 566 1.5 riastrad else if (amdgpu_vm_pt_sibling(adev, cursor)) 567 1.5 riastrad while (amdgpu_vm_pt_descendant(adev, cursor)); 568 1.5 riastrad else 569 1.5 riastrad amdgpu_vm_pt_ancestor(cursor); 570 1.5 riastrad } 571 1.1 riastrad 572 1.5 riastrad /* 573 1.5 riastrad * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs 574 1.5 riastrad */ 575 1.5 riastrad #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) \ 576 1.5 riastrad for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)), \ 577 1.5 riastrad (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\ 578 1.5 riastrad amdgpu_vm_pt_continue_dfs((start), (entry)); \ 579 1.5 riastrad (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor))) 580 1.1 riastrad 581 1.5 riastrad /** 582 1.5 riastrad * amdgpu_vm_get_pd_bo - add the VM PD to a validation list 583 1.5 riastrad * 584 1.5 riastrad * @vm: vm providing the BOs 585 1.5 riastrad * @validated: head of validation list 586 1.5 riastrad * @entry: entry to add 587 1.5 riastrad * 588 1.5 riastrad * Add the page directory to the list of BOs to 589 1.5 riastrad * validate for command submission. 590 1.5 riastrad */ 591 1.5 riastrad void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, 592 1.5 riastrad struct list_head *validated, 593 1.5 riastrad struct amdgpu_bo_list_entry *entry) 594 1.5 riastrad { 595 1.5 riastrad entry->priority = 0; 596 1.5 riastrad entry->tv.bo = &vm->root.base.bo->tbo; 597 1.5 riastrad /* One for TTM and one for the CS job */ 598 1.5 riastrad entry->tv.num_shared = 2; 599 1.5 riastrad entry->user_pages = NULL; 600 1.5 riastrad list_add(&entry->tv.head, validated); 601 1.1 riastrad } 602 1.1 riastrad 603 1.1 riastrad /** 604 1.5 riastrad * amdgpu_vm_del_from_lru_notify - update bulk_moveable flag 605 1.1 riastrad * 606 1.5 riastrad * @bo: BO which was removed from the LRU 607 1.1 riastrad * 608 1.5 riastrad * Make sure the bulk_moveable flag is updated when a BO is removed from the 609 1.5 riastrad * LRU. 610 1.1 riastrad */ 611 1.5 riastrad void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo) 612 1.1 riastrad { 613 1.5 riastrad struct amdgpu_bo *abo; 614 1.5 riastrad struct amdgpu_vm_bo_base *bo_base; 615 1.5 riastrad 616 1.5 riastrad if (!amdgpu_bo_is_amdgpu_bo(bo)) 617 1.5 riastrad return; 618 1.5 riastrad 619 1.5 riastrad if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) 620 1.5 riastrad return; 621 1.1 riastrad 622 1.5 riastrad abo = ttm_to_amdgpu_bo(bo); 623 1.5 riastrad if (!abo->parent) 624 1.5 riastrad return; 625 1.5 riastrad for (bo_base = abo->vm_bo; bo_base; bo_base = bo_base->next) { 626 1.5 riastrad struct amdgpu_vm *vm = bo_base->vm; 627 1.1 riastrad 628 1.5 riastrad if (abo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) 629 1.5 riastrad vm->bulk_moveable = false; 630 1.5 riastrad } 631 1.1 riastrad 632 1.1 riastrad } 633 1.1 riastrad /** 634 1.5 riastrad * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU 635 1.1 riastrad * 636 1.5 riastrad * @adev: amdgpu device pointer 637 1.5 riastrad * @vm: vm providing the BOs 638 1.1 riastrad * 639 1.5 riastrad * Move all BOs to the end of LRU and remember their positions to put them 640 1.5 riastrad * together. 641 1.5 riastrad */ 642 1.5 riastrad void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev, 643 1.5 riastrad struct amdgpu_vm *vm) 644 1.5 riastrad { 645 1.5 riastrad struct amdgpu_vm_bo_base *bo_base; 646 1.1 riastrad 647 1.5 riastrad if (vm->bulk_moveable) { 648 1.5 riastrad spin_lock(&ttm_bo_glob.lru_lock); 649 1.5 riastrad ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move); 650 1.5 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 651 1.5 riastrad return; 652 1.5 riastrad } 653 1.1 riastrad 654 1.5 riastrad memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move)); 655 1.1 riastrad 656 1.5 riastrad spin_lock(&ttm_bo_glob.lru_lock); 657 1.5 riastrad list_for_each_entry(bo_base, &vm->idle, vm_status) { 658 1.5 riastrad struct amdgpu_bo *bo = bo_base->bo; 659 1.1 riastrad 660 1.5 riastrad if (!bo->parent) 661 1.5 riastrad continue; 662 1.1 riastrad 663 1.5 riastrad ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move); 664 1.5 riastrad if (bo->shadow) 665 1.5 riastrad ttm_bo_move_to_lru_tail(&bo->shadow->tbo, 666 1.5 riastrad &vm->lru_bulk_move); 667 1.5 riastrad } 668 1.5 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 669 1.1 riastrad 670 1.5 riastrad vm->bulk_moveable = true; 671 1.5 riastrad } 672 1.1 riastrad 673 1.5 riastrad /** 674 1.5 riastrad * amdgpu_vm_validate_pt_bos - validate the page table BOs 675 1.5 riastrad * 676 1.5 riastrad * @adev: amdgpu device pointer 677 1.5 riastrad * @vm: vm providing the BOs 678 1.5 riastrad * @validate: callback to do the validation 679 1.5 riastrad * @param: parameter for the validation callback 680 1.5 riastrad * 681 1.5 riastrad * Validate the page table BOs on command submission if neccessary. 682 1.5 riastrad * 683 1.5 riastrad * Returns: 684 1.5 riastrad * Validation result. 685 1.5 riastrad */ 686 1.5 riastrad int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, 687 1.5 riastrad int (*validate)(void *p, struct amdgpu_bo *bo), 688 1.5 riastrad void *param) 689 1.5 riastrad { 690 1.5 riastrad struct amdgpu_vm_bo_base *bo_base, *tmp; 691 1.5 riastrad int r; 692 1.1 riastrad 693 1.5 riastrad vm->bulk_moveable &= list_empty(&vm->evicted); 694 1.1 riastrad 695 1.5 riastrad list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) { 696 1.5 riastrad struct amdgpu_bo *bo = bo_base->bo; 697 1.1 riastrad 698 1.5 riastrad r = validate(param, bo); 699 1.5 riastrad if (r) 700 1.5 riastrad return r; 701 1.1 riastrad 702 1.5 riastrad if (bo->tbo.type != ttm_bo_type_kernel) { 703 1.5 riastrad amdgpu_vm_bo_moved(bo_base); 704 1.1 riastrad } else { 705 1.5 riastrad vm->update_funcs->map_table(bo); 706 1.5 riastrad if (bo->parent) 707 1.5 riastrad amdgpu_vm_bo_relocated(bo_base); 708 1.5 riastrad else 709 1.5 riastrad amdgpu_vm_bo_idle(bo_base); 710 1.1 riastrad } 711 1.1 riastrad } 712 1.1 riastrad 713 1.5 riastrad amdgpu_vm_eviction_lock(vm); 714 1.5 riastrad vm->evicting = false; 715 1.5 riastrad amdgpu_vm_eviction_unlock(vm); 716 1.1 riastrad 717 1.1 riastrad return 0; 718 1.1 riastrad } 719 1.1 riastrad 720 1.1 riastrad /** 721 1.5 riastrad * amdgpu_vm_ready - check VM is ready for updates 722 1.1 riastrad * 723 1.5 riastrad * @vm: VM to check 724 1.5 riastrad * 725 1.5 riastrad * Check if all VM PDs/PTs are ready for updates 726 1.1 riastrad * 727 1.5 riastrad * Returns: 728 1.5 riastrad * True if eviction list is empty. 729 1.1 riastrad */ 730 1.5 riastrad bool amdgpu_vm_ready(struct amdgpu_vm *vm) 731 1.1 riastrad { 732 1.5 riastrad return list_empty(&vm->evicted); 733 1.1 riastrad } 734 1.1 riastrad 735 1.1 riastrad /** 736 1.5 riastrad * amdgpu_vm_clear_bo - initially clear the PDs/PTs 737 1.1 riastrad * 738 1.1 riastrad * @adev: amdgpu_device pointer 739 1.5 riastrad * @vm: VM to clear BO from 740 1.5 riastrad * @bo: BO to clear 741 1.5 riastrad * @direct: use a direct update 742 1.1 riastrad * 743 1.5 riastrad * Root PD needs to be reserved when calling this. 744 1.1 riastrad * 745 1.5 riastrad * Returns: 746 1.5 riastrad * 0 on success, errno otherwise. 747 1.1 riastrad */ 748 1.5 riastrad static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, 749 1.5 riastrad struct amdgpu_vm *vm, 750 1.5 riastrad struct amdgpu_bo *bo, 751 1.5 riastrad bool direct) 752 1.5 riastrad { 753 1.5 riastrad struct ttm_operation_ctx ctx = { true, false }; 754 1.5 riastrad unsigned level = adev->vm_manager.root_level; 755 1.5 riastrad struct amdgpu_vm_update_params params; 756 1.5 riastrad struct amdgpu_bo *ancestor = bo; 757 1.5 riastrad unsigned entries, ats_entries; 758 1.1 riastrad uint64_t addr; 759 1.5 riastrad int r; 760 1.5 riastrad 761 1.5 riastrad /* Figure out our place in the hierarchy */ 762 1.5 riastrad if (ancestor->parent) { 763 1.5 riastrad ++level; 764 1.5 riastrad while (ancestor->parent->parent) { 765 1.5 riastrad ++level; 766 1.5 riastrad ancestor = ancestor->parent; 767 1.5 riastrad } 768 1.5 riastrad } 769 1.5 riastrad 770 1.5 riastrad entries = amdgpu_bo_size(bo) / 8; 771 1.5 riastrad if (!vm->pte_support_ats) { 772 1.5 riastrad ats_entries = 0; 773 1.5 riastrad 774 1.5 riastrad } else if (!bo->parent) { 775 1.5 riastrad ats_entries = amdgpu_vm_num_ats_entries(adev); 776 1.5 riastrad ats_entries = min(ats_entries, entries); 777 1.5 riastrad entries -= ats_entries; 778 1.5 riastrad 779 1.5 riastrad } else { 780 1.5 riastrad struct amdgpu_vm_pt *pt; 781 1.1 riastrad 782 1.5 riastrad pt = container_of(ancestor->vm_bo, struct amdgpu_vm_pt, base); 783 1.5 riastrad ats_entries = amdgpu_vm_num_ats_entries(adev); 784 1.5 riastrad if ((pt - vm->root.entries) >= ats_entries) { 785 1.5 riastrad ats_entries = 0; 786 1.5 riastrad } else { 787 1.5 riastrad ats_entries = entries; 788 1.5 riastrad entries = 0; 789 1.5 riastrad } 790 1.5 riastrad } 791 1.1 riastrad 792 1.5 riastrad r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 793 1.5 riastrad if (r) 794 1.5 riastrad return r; 795 1.1 riastrad 796 1.5 riastrad if (bo->shadow) { 797 1.5 riastrad r = ttm_bo_validate(&bo->shadow->tbo, &bo->shadow->placement, 798 1.5 riastrad &ctx); 799 1.1 riastrad if (r) 800 1.1 riastrad return r; 801 1.5 riastrad } 802 1.5 riastrad 803 1.5 riastrad r = vm->update_funcs->map_table(bo); 804 1.5 riastrad if (r) 805 1.5 riastrad return r; 806 1.5 riastrad 807 1.5 riastrad memset(¶ms, 0, sizeof(params)); 808 1.5 riastrad params.adev = adev; 809 1.5 riastrad params.vm = vm; 810 1.5 riastrad params.direct = direct; 811 1.5 riastrad 812 1.5 riastrad r = vm->update_funcs->prepare(¶ms, AMDGPU_FENCE_OWNER_KFD, NULL); 813 1.5 riastrad if (r) 814 1.5 riastrad return r; 815 1.5 riastrad 816 1.5 riastrad addr = 0; 817 1.5 riastrad if (ats_entries) { 818 1.5 riastrad uint64_t value = 0, flags; 819 1.5 riastrad 820 1.5 riastrad flags = AMDGPU_PTE_DEFAULT_ATC; 821 1.5 riastrad if (level != AMDGPU_VM_PTB) { 822 1.5 riastrad /* Handle leaf PDEs as PTEs */ 823 1.5 riastrad flags |= AMDGPU_PDE_PTE; 824 1.5 riastrad amdgpu_gmc_get_vm_pde(adev, level, &value, &flags); 825 1.5 riastrad } 826 1.1 riastrad 827 1.5 riastrad r = vm->update_funcs->update(¶ms, bo, addr, 0, ats_entries, 828 1.5 riastrad value, flags); 829 1.5 riastrad if (r) 830 1.5 riastrad return r; 831 1.1 riastrad 832 1.5 riastrad addr += ats_entries * 8; 833 1.5 riastrad } 834 1.1 riastrad 835 1.5 riastrad if (entries) { 836 1.5 riastrad uint64_t value = 0, flags = 0; 837 1.1 riastrad 838 1.5 riastrad if (adev->asic_type >= CHIP_VEGA10) { 839 1.5 riastrad if (level != AMDGPU_VM_PTB) { 840 1.5 riastrad /* Handle leaf PDEs as PTEs */ 841 1.5 riastrad flags |= AMDGPU_PDE_PTE; 842 1.5 riastrad amdgpu_gmc_get_vm_pde(adev, level, 843 1.5 riastrad &value, &flags); 844 1.5 riastrad } else { 845 1.5 riastrad /* Workaround for fault priority problem on GMC9 */ 846 1.5 riastrad flags = AMDGPU_PTE_EXECUTABLE; 847 1.1 riastrad } 848 1.1 riastrad } 849 1.1 riastrad 850 1.5 riastrad r = vm->update_funcs->update(¶ms, bo, addr, 0, entries, 851 1.5 riastrad value, flags); 852 1.5 riastrad if (r) 853 1.5 riastrad return r; 854 1.1 riastrad } 855 1.1 riastrad 856 1.5 riastrad return vm->update_funcs->commit(¶ms, NULL); 857 1.5 riastrad } 858 1.5 riastrad 859 1.5 riastrad /** 860 1.5 riastrad * amdgpu_vm_bo_param - fill in parameters for PD/PT allocation 861 1.5 riastrad * 862 1.5 riastrad * @adev: amdgpu_device pointer 863 1.5 riastrad * @vm: requesting vm 864 1.5 riastrad * @level: the page table level 865 1.5 riastrad * @direct: use a direct update 866 1.5 riastrad * @bp: resulting BO allocation parameters 867 1.5 riastrad */ 868 1.5 riastrad static void amdgpu_vm_bo_param(struct amdgpu_device *adev, struct amdgpu_vm *vm, 869 1.5 riastrad int level, bool direct, 870 1.5 riastrad struct amdgpu_bo_param *bp) 871 1.5 riastrad { 872 1.5 riastrad memset(bp, 0, sizeof(*bp)); 873 1.1 riastrad 874 1.5 riastrad bp->size = amdgpu_vm_bo_size(adev, level); 875 1.5 riastrad bp->byte_align = AMDGPU_GPU_PAGE_SIZE; 876 1.5 riastrad bp->domain = AMDGPU_GEM_DOMAIN_VRAM; 877 1.5 riastrad bp->domain = amdgpu_bo_get_preferred_pin_domain(adev, bp->domain); 878 1.5 riastrad bp->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | 879 1.5 riastrad AMDGPU_GEM_CREATE_CPU_GTT_USWC; 880 1.5 riastrad if (vm->use_cpu_for_update) 881 1.5 riastrad bp->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 882 1.5 riastrad else if (!vm->root.base.bo || vm->root.base.bo->shadow) 883 1.5 riastrad bp->flags |= AMDGPU_GEM_CREATE_SHADOW; 884 1.5 riastrad bp->type = ttm_bo_type_kernel; 885 1.5 riastrad bp->no_wait_gpu = direct; 886 1.5 riastrad if (vm->root.base.bo) 887 1.5 riastrad bp->resv = vm->root.base.bo->tbo.base.resv; 888 1.1 riastrad } 889 1.1 riastrad 890 1.1 riastrad /** 891 1.5 riastrad * amdgpu_vm_alloc_pts - Allocate a specific page table 892 1.1 riastrad * 893 1.1 riastrad * @adev: amdgpu_device pointer 894 1.5 riastrad * @vm: VM to allocate page tables for 895 1.5 riastrad * @cursor: Which page table to allocate 896 1.5 riastrad * @direct: use a direct update 897 1.1 riastrad * 898 1.5 riastrad * Make sure a specific page table or directory is allocated. 899 1.1 riastrad * 900 1.5 riastrad * Returns: 901 1.5 riastrad * 1 if page table needed to be allocated, 0 if page table was already 902 1.5 riastrad * allocated, negative errno if an error occurred. 903 1.1 riastrad */ 904 1.5 riastrad static int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, 905 1.5 riastrad struct amdgpu_vm *vm, 906 1.5 riastrad struct amdgpu_vm_pt_cursor *cursor, 907 1.5 riastrad bool direct) 908 1.5 riastrad { 909 1.5 riastrad struct amdgpu_vm_pt *entry = cursor->entry; 910 1.5 riastrad struct amdgpu_bo_param bp; 911 1.5 riastrad struct amdgpu_bo *pt; 912 1.1 riastrad int r; 913 1.1 riastrad 914 1.5 riastrad if (cursor->level < AMDGPU_VM_PTB && !entry->entries) { 915 1.5 riastrad unsigned num_entries; 916 1.1 riastrad 917 1.5 riastrad num_entries = amdgpu_vm_num_entries(adev, cursor->level); 918 1.5 riastrad entry->entries = kvmalloc_array(num_entries, 919 1.5 riastrad sizeof(*entry->entries), 920 1.5 riastrad GFP_KERNEL | __GFP_ZERO); 921 1.5 riastrad if (!entry->entries) 922 1.5 riastrad return -ENOMEM; 923 1.5 riastrad } 924 1.1 riastrad 925 1.5 riastrad if (entry->base.bo) 926 1.5 riastrad return 0; 927 1.1 riastrad 928 1.5 riastrad amdgpu_vm_bo_param(adev, vm, cursor->level, direct, &bp); 929 1.1 riastrad 930 1.5 riastrad r = amdgpu_bo_create(adev, &bp, &pt); 931 1.5 riastrad if (r) 932 1.5 riastrad return r; 933 1.1 riastrad 934 1.5 riastrad /* Keep a reference to the root directory to avoid 935 1.5 riastrad * freeing them up in the wrong order. 936 1.5 riastrad */ 937 1.5 riastrad pt->parent = amdgpu_bo_ref(cursor->parent->base.bo); 938 1.5 riastrad amdgpu_vm_bo_base_init(&entry->base, vm, pt); 939 1.1 riastrad 940 1.5 riastrad r = amdgpu_vm_clear_bo(adev, vm, pt, direct); 941 1.5 riastrad if (r) 942 1.5 riastrad goto error_free_pt; 943 1.1 riastrad 944 1.5 riastrad return 0; 945 1.1 riastrad 946 1.5 riastrad error_free_pt: 947 1.5 riastrad amdgpu_bo_unref(&pt->shadow); 948 1.5 riastrad amdgpu_bo_unref(&pt); 949 1.5 riastrad return r; 950 1.5 riastrad } 951 1.1 riastrad 952 1.5 riastrad /** 953 1.5 riastrad * amdgpu_vm_free_table - fre one PD/PT 954 1.5 riastrad * 955 1.5 riastrad * @entry: PDE to free 956 1.5 riastrad */ 957 1.5 riastrad static void amdgpu_vm_free_table(struct amdgpu_vm_pt *entry) 958 1.5 riastrad { 959 1.5 riastrad if (entry->base.bo) { 960 1.5 riastrad entry->base.bo->vm_bo = NULL; 961 1.5 riastrad list_del(&entry->base.vm_status); 962 1.5 riastrad amdgpu_bo_unref(&entry->base.bo->shadow); 963 1.5 riastrad amdgpu_bo_unref(&entry->base.bo); 964 1.1 riastrad } 965 1.5 riastrad kvfree(entry->entries); 966 1.5 riastrad entry->entries = NULL; 967 1.5 riastrad } 968 1.5 riastrad 969 1.5 riastrad /** 970 1.5 riastrad * amdgpu_vm_free_pts - free PD/PT levels 971 1.5 riastrad * 972 1.5 riastrad * @adev: amdgpu device structure 973 1.5 riastrad * @vm: amdgpu vm structure 974 1.5 riastrad * @start: optional cursor where to start freeing PDs/PTs 975 1.5 riastrad * 976 1.5 riastrad * Free the page directory or page table level and all sub levels. 977 1.5 riastrad */ 978 1.5 riastrad static void amdgpu_vm_free_pts(struct amdgpu_device *adev, 979 1.5 riastrad struct amdgpu_vm *vm, 980 1.5 riastrad struct amdgpu_vm_pt_cursor *start) 981 1.5 riastrad { 982 1.5 riastrad struct amdgpu_vm_pt_cursor cursor; 983 1.5 riastrad struct amdgpu_vm_pt *entry; 984 1.1 riastrad 985 1.5 riastrad vm->bulk_moveable = false; 986 1.1 riastrad 987 1.5 riastrad for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) 988 1.5 riastrad amdgpu_vm_free_table(entry); 989 1.1 riastrad 990 1.5 riastrad if (start) 991 1.5 riastrad amdgpu_vm_free_table(start->entry); 992 1.5 riastrad } 993 1.1 riastrad 994 1.5 riastrad /** 995 1.5 riastrad * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug 996 1.5 riastrad * 997 1.5 riastrad * @adev: amdgpu_device pointer 998 1.5 riastrad */ 999 1.5 riastrad void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev) 1000 1.5 riastrad { 1001 1.5 riastrad const struct amdgpu_ip_block *ip_block; 1002 1.5 riastrad bool has_compute_vm_bug; 1003 1.5 riastrad struct amdgpu_ring *ring; 1004 1.5 riastrad int i; 1005 1.1 riastrad 1006 1.5 riastrad has_compute_vm_bug = false; 1007 1.1 riastrad 1008 1.5 riastrad ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); 1009 1.5 riastrad if (ip_block) { 1010 1.5 riastrad /* Compute has a VM bug for GFX version < 7. 1011 1.5 riastrad Compute has a VM bug for GFX 8 MEC firmware version < 673.*/ 1012 1.5 riastrad if (ip_block->version->major <= 7) 1013 1.5 riastrad has_compute_vm_bug = true; 1014 1.5 riastrad else if (ip_block->version->major == 8) 1015 1.5 riastrad if (adev->gfx.mec_fw_version < 673) 1016 1.5 riastrad has_compute_vm_bug = true; 1017 1.5 riastrad } 1018 1.5 riastrad 1019 1.5 riastrad for (i = 0; i < adev->num_rings; i++) { 1020 1.5 riastrad ring = adev->rings[i]; 1021 1.5 riastrad if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) 1022 1.5 riastrad /* only compute rings */ 1023 1.5 riastrad ring->has_compute_vm_bug = has_compute_vm_bug; 1024 1.5 riastrad else 1025 1.5 riastrad ring->has_compute_vm_bug = false; 1026 1.1 riastrad } 1027 1.5 riastrad } 1028 1.1 riastrad 1029 1.5 riastrad /** 1030 1.5 riastrad * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job. 1031 1.5 riastrad * 1032 1.5 riastrad * @ring: ring on which the job will be submitted 1033 1.5 riastrad * @job: job to submit 1034 1.5 riastrad * 1035 1.5 riastrad * Returns: 1036 1.5 riastrad * True if sync is needed. 1037 1.5 riastrad */ 1038 1.5 riastrad bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring, 1039 1.5 riastrad struct amdgpu_job *job) 1040 1.5 riastrad { 1041 1.5 riastrad struct amdgpu_device *adev = ring->adev; 1042 1.5 riastrad unsigned vmhub = ring->funcs->vmhub; 1043 1.5 riastrad struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; 1044 1.5 riastrad struct amdgpu_vmid *id; 1045 1.5 riastrad bool gds_switch_needed; 1046 1.5 riastrad bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug; 1047 1.5 riastrad 1048 1.5 riastrad if (job->vmid == 0) 1049 1.5 riastrad return false; 1050 1.5 riastrad id = &id_mgr->ids[job->vmid]; 1051 1.5 riastrad gds_switch_needed = ring->funcs->emit_gds_switch && ( 1052 1.5 riastrad id->gds_base != job->gds_base || 1053 1.5 riastrad id->gds_size != job->gds_size || 1054 1.5 riastrad id->gws_base != job->gws_base || 1055 1.5 riastrad id->gws_size != job->gws_size || 1056 1.5 riastrad id->oa_base != job->oa_base || 1057 1.5 riastrad id->oa_size != job->oa_size); 1058 1.1 riastrad 1059 1.5 riastrad if (amdgpu_vmid_had_gpu_reset(adev, id)) 1060 1.5 riastrad return true; 1061 1.1 riastrad 1062 1.5 riastrad return vm_flush_needed || gds_switch_needed; 1063 1.1 riastrad } 1064 1.1 riastrad 1065 1.1 riastrad /** 1066 1.5 riastrad * amdgpu_vm_flush - hardware flush the vm 1067 1.1 riastrad * 1068 1.5 riastrad * @ring: ring to use for flush 1069 1.5 riastrad * @job: related job 1070 1.5 riastrad * @need_pipe_sync: is pipe sync needed 1071 1.1 riastrad * 1072 1.5 riastrad * Emit a VM flush when it is necessary. 1073 1.1 riastrad * 1074 1.5 riastrad * Returns: 1075 1.5 riastrad * 0 on success, errno otherwise. 1076 1.1 riastrad */ 1077 1.5 riastrad int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, 1078 1.5 riastrad bool need_pipe_sync) 1079 1.1 riastrad { 1080 1.5 riastrad struct amdgpu_device *adev = ring->adev; 1081 1.5 riastrad unsigned vmhub = ring->funcs->vmhub; 1082 1.5 riastrad struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; 1083 1.5 riastrad struct amdgpu_vmid *id = &id_mgr->ids[job->vmid]; 1084 1.5 riastrad bool gds_switch_needed = ring->funcs->emit_gds_switch && ( 1085 1.5 riastrad id->gds_base != job->gds_base || 1086 1.5 riastrad id->gds_size != job->gds_size || 1087 1.5 riastrad id->gws_base != job->gws_base || 1088 1.5 riastrad id->gws_size != job->gws_size || 1089 1.5 riastrad id->oa_base != job->oa_base || 1090 1.5 riastrad id->oa_size != job->oa_size); 1091 1.5 riastrad bool vm_flush_needed = job->vm_needs_flush; 1092 1.5 riastrad struct dma_fence *fence = NULL; 1093 1.5 riastrad bool pasid_mapping_needed = false; 1094 1.5 riastrad unsigned patch_offset = 0; 1095 1.1 riastrad int r; 1096 1.1 riastrad 1097 1.5 riastrad if (amdgpu_vmid_had_gpu_reset(adev, id)) { 1098 1.5 riastrad gds_switch_needed = true; 1099 1.5 riastrad vm_flush_needed = true; 1100 1.5 riastrad pasid_mapping_needed = true; 1101 1.5 riastrad } 1102 1.5 riastrad 1103 1.5 riastrad mutex_lock(&id_mgr->lock); 1104 1.5 riastrad if (id->pasid != job->pasid || !id->pasid_mapping || 1105 1.5 riastrad !dma_fence_is_signaled(id->pasid_mapping)) 1106 1.5 riastrad pasid_mapping_needed = true; 1107 1.5 riastrad mutex_unlock(&id_mgr->lock); 1108 1.5 riastrad 1109 1.5 riastrad gds_switch_needed &= !!ring->funcs->emit_gds_switch; 1110 1.5 riastrad vm_flush_needed &= !!ring->funcs->emit_vm_flush && 1111 1.5 riastrad job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET; 1112 1.5 riastrad pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping && 1113 1.5 riastrad ring->funcs->emit_wreg; 1114 1.5 riastrad 1115 1.5 riastrad if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync) 1116 1.5 riastrad return 0; 1117 1.5 riastrad 1118 1.5 riastrad if (ring->funcs->init_cond_exec) 1119 1.5 riastrad patch_offset = amdgpu_ring_init_cond_exec(ring); 1120 1.5 riastrad 1121 1.5 riastrad if (need_pipe_sync) 1122 1.5 riastrad amdgpu_ring_emit_pipeline_sync(ring); 1123 1.5 riastrad 1124 1.5 riastrad if (vm_flush_needed) { 1125 1.5 riastrad trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr); 1126 1.5 riastrad amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr); 1127 1.1 riastrad } 1128 1.1 riastrad 1129 1.5 riastrad if (pasid_mapping_needed) 1130 1.5 riastrad amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid); 1131 1.1 riastrad 1132 1.5 riastrad if (vm_flush_needed || pasid_mapping_needed) { 1133 1.5 riastrad r = amdgpu_fence_emit(ring, &fence, 0); 1134 1.1 riastrad if (r) 1135 1.1 riastrad return r; 1136 1.1 riastrad } 1137 1.1 riastrad 1138 1.5 riastrad if (vm_flush_needed) { 1139 1.5 riastrad mutex_lock(&id_mgr->lock); 1140 1.5 riastrad dma_fence_put(id->last_flush); 1141 1.5 riastrad id->last_flush = dma_fence_get(fence); 1142 1.5 riastrad id->current_gpu_reset_count = 1143 1.5 riastrad atomic_read(&adev->gpu_reset_counter); 1144 1.5 riastrad mutex_unlock(&id_mgr->lock); 1145 1.5 riastrad } 1146 1.5 riastrad 1147 1.5 riastrad if (pasid_mapping_needed) { 1148 1.5 riastrad mutex_lock(&id_mgr->lock); 1149 1.5 riastrad id->pasid = job->pasid; 1150 1.5 riastrad dma_fence_put(id->pasid_mapping); 1151 1.5 riastrad id->pasid_mapping = dma_fence_get(fence); 1152 1.5 riastrad mutex_unlock(&id_mgr->lock); 1153 1.5 riastrad } 1154 1.5 riastrad dma_fence_put(fence); 1155 1.5 riastrad 1156 1.5 riastrad if (ring->funcs->emit_gds_switch && gds_switch_needed) { 1157 1.5 riastrad id->gds_base = job->gds_base; 1158 1.5 riastrad id->gds_size = job->gds_size; 1159 1.5 riastrad id->gws_base = job->gws_base; 1160 1.5 riastrad id->gws_size = job->gws_size; 1161 1.5 riastrad id->oa_base = job->oa_base; 1162 1.5 riastrad id->oa_size = job->oa_size; 1163 1.5 riastrad amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, 1164 1.5 riastrad job->gds_size, job->gws_base, 1165 1.5 riastrad job->gws_size, job->oa_base, 1166 1.5 riastrad job->oa_size); 1167 1.5 riastrad } 1168 1.5 riastrad 1169 1.5 riastrad if (ring->funcs->patch_cond_exec) 1170 1.5 riastrad amdgpu_ring_patch_cond_exec(ring, patch_offset); 1171 1.5 riastrad 1172 1.5 riastrad /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ 1173 1.5 riastrad if (ring->funcs->emit_switch_buffer) { 1174 1.5 riastrad amdgpu_ring_emit_switch_buffer(ring); 1175 1.5 riastrad amdgpu_ring_emit_switch_buffer(ring); 1176 1.1 riastrad } 1177 1.1 riastrad return 0; 1178 1.1 riastrad } 1179 1.1 riastrad 1180 1.1 riastrad /** 1181 1.5 riastrad * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo 1182 1.1 riastrad * 1183 1.1 riastrad * @vm: requested vm 1184 1.5 riastrad * @bo: requested buffer object 1185 1.1 riastrad * 1186 1.5 riastrad * Find @bo inside the requested vm. 1187 1.5 riastrad * Search inside the @bos vm list for the requested vm 1188 1.5 riastrad * Returns the found bo_va or NULL if none is found 1189 1.5 riastrad * 1190 1.5 riastrad * Object has to be reserved! 1191 1.1 riastrad * 1192 1.5 riastrad * Returns: 1193 1.5 riastrad * Found bo_va or NULL. 1194 1.1 riastrad */ 1195 1.5 riastrad struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, 1196 1.5 riastrad struct amdgpu_bo *bo) 1197 1.1 riastrad { 1198 1.5 riastrad struct amdgpu_vm_bo_base *base; 1199 1.1 riastrad 1200 1.5 riastrad for (base = bo->vm_bo; base; base = base->next) { 1201 1.5 riastrad if (base->vm != vm) 1202 1.5 riastrad continue; 1203 1.1 riastrad 1204 1.5 riastrad return container_of(base, struct amdgpu_bo_va, base); 1205 1.1 riastrad } 1206 1.5 riastrad return NULL; 1207 1.1 riastrad } 1208 1.1 riastrad 1209 1.1 riastrad /** 1210 1.5 riastrad * amdgpu_vm_map_gart - Resolve gart mapping of addr 1211 1.1 riastrad * 1212 1.5 riastrad * @pages_addr: optional DMA address to use for lookup 1213 1.5 riastrad * @addr: the unmapped addr 1214 1.1 riastrad * 1215 1.5 riastrad * Look up the physical address of the page that the pte resolves 1216 1.5 riastrad * to. 1217 1.1 riastrad * 1218 1.5 riastrad * Returns: 1219 1.5 riastrad * The pointer for the page table entry. 1220 1.1 riastrad */ 1221 1.7 riastrad #ifdef __NetBSD__ 1222 1.7 riastrad uint64_t amdgpu_vm_map_gart(const bus_dma_segment_t *pages_addr, uint64_t addr) 1223 1.7 riastrad #else 1224 1.5 riastrad uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) 1225 1.7 riastrad #endif 1226 1.1 riastrad { 1227 1.5 riastrad uint64_t result; 1228 1.1 riastrad 1229 1.5 riastrad /* page table offset */ 1230 1.7 riastrad #ifdef __NetBSD__ 1231 1.7 riastrad KASSERT(pages_addr[addr >> PAGE_SHIFT].ds_len == PAGE_SIZE); 1232 1.7 riastrad result = pages_addr[addr >> PAGE_SHIFT].ds_addr; 1233 1.7 riastrad #else 1234 1.5 riastrad result = pages_addr[addr >> PAGE_SHIFT]; 1235 1.7 riastrad #endif 1236 1.1 riastrad 1237 1.5 riastrad /* in case cpu page size != gpu page size*/ 1238 1.5 riastrad result |= addr & (~PAGE_MASK); 1239 1.1 riastrad 1240 1.5 riastrad result &= 0xFFFFFFFFFFFFF000ULL; 1241 1.1 riastrad 1242 1.5 riastrad return result; 1243 1.1 riastrad } 1244 1.1 riastrad 1245 1.1 riastrad /** 1246 1.5 riastrad * amdgpu_vm_update_pde - update a single level in the hierarchy 1247 1.1 riastrad * 1248 1.5 riastrad * @params: parameters for the update 1249 1.1 riastrad * @vm: requested vm 1250 1.5 riastrad * @entry: entry to update 1251 1.5 riastrad * 1252 1.5 riastrad * Makes sure the requested entry in parent is up to date. 1253 1.5 riastrad */ 1254 1.5 riastrad static int amdgpu_vm_update_pde(struct amdgpu_vm_update_params *params, 1255 1.5 riastrad struct amdgpu_vm *vm, 1256 1.5 riastrad struct amdgpu_vm_pt *entry) 1257 1.5 riastrad { 1258 1.5 riastrad struct amdgpu_vm_pt *parent = amdgpu_vm_pt_parent(entry); 1259 1.5 riastrad struct amdgpu_bo *bo = parent->base.bo, *pbo; 1260 1.5 riastrad uint64_t pde, pt, flags; 1261 1.5 riastrad unsigned level; 1262 1.5 riastrad 1263 1.5 riastrad for (level = 0, pbo = bo->parent; pbo; ++level) 1264 1.5 riastrad pbo = pbo->parent; 1265 1.5 riastrad 1266 1.5 riastrad level += params->adev->vm_manager.root_level; 1267 1.5 riastrad amdgpu_gmc_get_pde_for_bo(entry->base.bo, level, &pt, &flags); 1268 1.5 riastrad pde = (entry - parent->entries) * 8; 1269 1.5 riastrad return vm->update_funcs->update(params, bo, pde, pt, 1, 0, flags); 1270 1.5 riastrad } 1271 1.5 riastrad 1272 1.5 riastrad /** 1273 1.5 riastrad * amdgpu_vm_invalidate_pds - mark all PDs as invalid 1274 1.1 riastrad * 1275 1.5 riastrad * @adev: amdgpu_device pointer 1276 1.5 riastrad * @vm: related vm 1277 1.1 riastrad * 1278 1.5 riastrad * Mark all PD level as invalid after an error. 1279 1.1 riastrad */ 1280 1.5 riastrad static void amdgpu_vm_invalidate_pds(struct amdgpu_device *adev, 1281 1.5 riastrad struct amdgpu_vm *vm) 1282 1.1 riastrad { 1283 1.5 riastrad struct amdgpu_vm_pt_cursor cursor; 1284 1.5 riastrad struct amdgpu_vm_pt *entry; 1285 1.1 riastrad 1286 1.5 riastrad for_each_amdgpu_vm_pt_dfs_safe(adev, vm, NULL, cursor, entry) 1287 1.5 riastrad if (entry->base.bo && !entry->base.moved) 1288 1.5 riastrad amdgpu_vm_bo_relocated(&entry->base); 1289 1.1 riastrad } 1290 1.1 riastrad 1291 1.1 riastrad /** 1292 1.5 riastrad * amdgpu_vm_update_pdes - make sure that all directories are valid 1293 1.1 riastrad * 1294 1.1 riastrad * @adev: amdgpu_device pointer 1295 1.5 riastrad * @vm: requested vm 1296 1.5 riastrad * @direct: submit directly to the paging queue 1297 1.1 riastrad * 1298 1.5 riastrad * Makes sure all directories are up to date. 1299 1.1 riastrad * 1300 1.5 riastrad * Returns: 1301 1.5 riastrad * 0 for success, error for failure. 1302 1.1 riastrad */ 1303 1.5 riastrad int amdgpu_vm_update_pdes(struct amdgpu_device *adev, 1304 1.5 riastrad struct amdgpu_vm *vm, bool direct) 1305 1.1 riastrad { 1306 1.5 riastrad struct amdgpu_vm_update_params params; 1307 1.1 riastrad int r; 1308 1.1 riastrad 1309 1.5 riastrad if (list_empty(&vm->relocated)) 1310 1.5 riastrad return 0; 1311 1.5 riastrad 1312 1.5 riastrad memset(¶ms, 0, sizeof(params)); 1313 1.5 riastrad params.adev = adev; 1314 1.5 riastrad params.vm = vm; 1315 1.5 riastrad params.direct = direct; 1316 1.5 riastrad 1317 1.5 riastrad r = vm->update_funcs->prepare(¶ms, AMDGPU_FENCE_OWNER_VM, NULL); 1318 1.5 riastrad if (r) 1319 1.5 riastrad return r; 1320 1.5 riastrad 1321 1.5 riastrad while (!list_empty(&vm->relocated)) { 1322 1.5 riastrad struct amdgpu_vm_pt *entry; 1323 1.5 riastrad 1324 1.5 riastrad entry = list_first_entry(&vm->relocated, struct amdgpu_vm_pt, 1325 1.5 riastrad base.vm_status); 1326 1.5 riastrad amdgpu_vm_bo_idle(&entry->base); 1327 1.5 riastrad 1328 1.5 riastrad r = amdgpu_vm_update_pde(¶ms, vm, entry); 1329 1.5 riastrad if (r) 1330 1.5 riastrad goto error; 1331 1.5 riastrad } 1332 1.5 riastrad 1333 1.5 riastrad r = vm->update_funcs->commit(¶ms, &vm->last_update); 1334 1.5 riastrad if (r) 1335 1.5 riastrad goto error; 1336 1.5 riastrad return 0; 1337 1.5 riastrad 1338 1.5 riastrad error: 1339 1.5 riastrad amdgpu_vm_invalidate_pds(adev, vm); 1340 1.5 riastrad return r; 1341 1.5 riastrad } 1342 1.5 riastrad 1343 1.5 riastrad /* 1344 1.5 riastrad * amdgpu_vm_update_flags - figure out flags for PTE updates 1345 1.5 riastrad * 1346 1.5 riastrad * Make sure to set the right flags for the PTEs at the desired level. 1347 1.5 riastrad */ 1348 1.5 riastrad static void amdgpu_vm_update_flags(struct amdgpu_vm_update_params *params, 1349 1.5 riastrad struct amdgpu_bo *bo, unsigned level, 1350 1.5 riastrad uint64_t pe, uint64_t addr, 1351 1.5 riastrad unsigned count, uint32_t incr, 1352 1.5 riastrad uint64_t flags) 1353 1.5 riastrad 1354 1.5 riastrad { 1355 1.5 riastrad if (level != AMDGPU_VM_PTB) { 1356 1.5 riastrad flags |= AMDGPU_PDE_PTE; 1357 1.5 riastrad amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags); 1358 1.5 riastrad 1359 1.5 riastrad } else if (params->adev->asic_type >= CHIP_VEGA10 && 1360 1.5 riastrad !(flags & AMDGPU_PTE_VALID) && 1361 1.5 riastrad !(flags & AMDGPU_PTE_PRT)) { 1362 1.5 riastrad 1363 1.5 riastrad /* Workaround for fault priority problem on GMC9 */ 1364 1.5 riastrad flags |= AMDGPU_PTE_EXECUTABLE; 1365 1.5 riastrad } 1366 1.5 riastrad 1367 1.5 riastrad params->vm->update_funcs->update(params, bo, pe, addr, count, incr, 1368 1.5 riastrad flags); 1369 1.5 riastrad } 1370 1.5 riastrad 1371 1.5 riastrad /** 1372 1.5 riastrad * amdgpu_vm_fragment - get fragment for PTEs 1373 1.5 riastrad * 1374 1.5 riastrad * @params: see amdgpu_vm_update_params definition 1375 1.5 riastrad * @start: first PTE to handle 1376 1.5 riastrad * @end: last PTE to handle 1377 1.5 riastrad * @flags: hw mapping flags 1378 1.5 riastrad * @frag: resulting fragment size 1379 1.5 riastrad * @frag_end: end of this fragment 1380 1.5 riastrad * 1381 1.5 riastrad * Returns the first possible fragment for the start and end address. 1382 1.5 riastrad */ 1383 1.5 riastrad static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params, 1384 1.5 riastrad uint64_t start, uint64_t end, uint64_t flags, 1385 1.5 riastrad unsigned int *frag, uint64_t *frag_end) 1386 1.5 riastrad { 1387 1.5 riastrad /** 1388 1.5 riastrad * The MC L1 TLB supports variable sized pages, based on a fragment 1389 1.5 riastrad * field in the PTE. When this field is set to a non-zero value, page 1390 1.5 riastrad * granularity is increased from 4KB to (1 << (12 + frag)). The PTE 1391 1.5 riastrad * flags are considered valid for all PTEs within the fragment range 1392 1.5 riastrad * and corresponding mappings are assumed to be physically contiguous. 1393 1.5 riastrad * 1394 1.5 riastrad * The L1 TLB can store a single PTE for the whole fragment, 1395 1.5 riastrad * significantly increasing the space available for translation 1396 1.5 riastrad * caching. This leads to large improvements in throughput when the 1397 1.5 riastrad * TLB is under pressure. 1398 1.5 riastrad * 1399 1.5 riastrad * The L2 TLB distributes small and large fragments into two 1400 1.5 riastrad * asymmetric partitions. The large fragment cache is significantly 1401 1.5 riastrad * larger. Thus, we try to use large fragments wherever possible. 1402 1.5 riastrad * Userspace can support this by aligning virtual base address and 1403 1.5 riastrad * allocation size to the fragment size. 1404 1.5 riastrad * 1405 1.5 riastrad * Starting with Vega10 the fragment size only controls the L1. The L2 1406 1.5 riastrad * is now directly feed with small/huge/giant pages from the walker. 1407 1.5 riastrad */ 1408 1.5 riastrad unsigned max_frag; 1409 1.5 riastrad 1410 1.5 riastrad if (params->adev->asic_type < CHIP_VEGA10) 1411 1.5 riastrad max_frag = params->adev->vm_manager.fragment_size; 1412 1.5 riastrad else 1413 1.5 riastrad max_frag = 31; 1414 1.5 riastrad 1415 1.5 riastrad /* system pages are non continuously */ 1416 1.5 riastrad if (params->pages_addr) { 1417 1.5 riastrad *frag = 0; 1418 1.5 riastrad *frag_end = end; 1419 1.5 riastrad return; 1420 1.5 riastrad } 1421 1.5 riastrad 1422 1.5 riastrad /* This intentionally wraps around if no bit is set */ 1423 1.5 riastrad *frag = min((unsigned)ffs(start) - 1, (unsigned)fls64(end - start) - 1); 1424 1.5 riastrad if (*frag >= max_frag) { 1425 1.5 riastrad *frag = max_frag; 1426 1.5 riastrad *frag_end = end & ~((1ULL << max_frag) - 1); 1427 1.5 riastrad } else { 1428 1.5 riastrad *frag_end = start + (1 << *frag); 1429 1.5 riastrad } 1430 1.5 riastrad } 1431 1.5 riastrad 1432 1.5 riastrad /** 1433 1.5 riastrad * amdgpu_vm_update_ptes - make sure that page tables are valid 1434 1.5 riastrad * 1435 1.5 riastrad * @params: see amdgpu_vm_update_params definition 1436 1.5 riastrad * @start: start of GPU address range 1437 1.5 riastrad * @end: end of GPU address range 1438 1.5 riastrad * @dst: destination address to map to, the next dst inside the function 1439 1.5 riastrad * @flags: mapping flags 1440 1.5 riastrad * 1441 1.5 riastrad * Update the page tables in the range @start - @end. 1442 1.5 riastrad * 1443 1.5 riastrad * Returns: 1444 1.5 riastrad * 0 for success, -EINVAL for failure. 1445 1.5 riastrad */ 1446 1.5 riastrad static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params, 1447 1.5 riastrad uint64_t start, uint64_t end, 1448 1.5 riastrad uint64_t dst, uint64_t flags) 1449 1.5 riastrad { 1450 1.5 riastrad struct amdgpu_device *adev = params->adev; 1451 1.5 riastrad struct amdgpu_vm_pt_cursor cursor; 1452 1.5 riastrad uint64_t frag_start = start, frag_end; 1453 1.5 riastrad unsigned int frag; 1454 1.5 riastrad int r; 1455 1.5 riastrad 1456 1.5 riastrad /* figure out the initial fragment */ 1457 1.5 riastrad amdgpu_vm_fragment(params, frag_start, end, flags, &frag, &frag_end); 1458 1.5 riastrad 1459 1.5 riastrad /* walk over the address space and update the PTs */ 1460 1.5 riastrad amdgpu_vm_pt_start(adev, params->vm, start, &cursor); 1461 1.5 riastrad while (cursor.pfn < end) { 1462 1.5 riastrad unsigned shift, parent_shift, mask; 1463 1.5 riastrad uint64_t incr, entry_end, pe_start; 1464 1.5 riastrad struct amdgpu_bo *pt; 1465 1.5 riastrad 1466 1.5 riastrad /* make sure that the page tables covering the address range are 1467 1.5 riastrad * actually allocated 1468 1.5 riastrad */ 1469 1.5 riastrad r = amdgpu_vm_alloc_pts(params->adev, params->vm, &cursor, 1470 1.5 riastrad params->direct); 1471 1.5 riastrad if (r) 1472 1.5 riastrad return r; 1473 1.5 riastrad 1474 1.5 riastrad pt = cursor.entry->base.bo; 1475 1.5 riastrad 1476 1.5 riastrad /* The root level can't be a huge page */ 1477 1.5 riastrad if (cursor.level == adev->vm_manager.root_level) { 1478 1.5 riastrad if (!amdgpu_vm_pt_descendant(adev, &cursor)) 1479 1.5 riastrad return -ENOENT; 1480 1.5 riastrad continue; 1481 1.5 riastrad } 1482 1.5 riastrad 1483 1.5 riastrad shift = amdgpu_vm_level_shift(adev, cursor.level); 1484 1.5 riastrad parent_shift = amdgpu_vm_level_shift(adev, cursor.level - 1); 1485 1.5 riastrad if (adev->asic_type < CHIP_VEGA10 && 1486 1.5 riastrad (flags & AMDGPU_PTE_VALID)) { 1487 1.5 riastrad /* No huge page support before GMC v9 */ 1488 1.5 riastrad if (cursor.level != AMDGPU_VM_PTB) { 1489 1.5 riastrad if (!amdgpu_vm_pt_descendant(adev, &cursor)) 1490 1.5 riastrad return -ENOENT; 1491 1.5 riastrad continue; 1492 1.5 riastrad } 1493 1.5 riastrad } else if (frag < shift) { 1494 1.5 riastrad /* We can't use this level when the fragment size is 1495 1.5 riastrad * smaller than the address shift. Go to the next 1496 1.5 riastrad * child entry and try again. 1497 1.5 riastrad */ 1498 1.5 riastrad if (!amdgpu_vm_pt_descendant(adev, &cursor)) 1499 1.5 riastrad return -ENOENT; 1500 1.5 riastrad continue; 1501 1.5 riastrad } else if (frag >= parent_shift && 1502 1.5 riastrad cursor.level - 1 != adev->vm_manager.root_level) { 1503 1.5 riastrad /* If the fragment size is even larger than the parent 1504 1.5 riastrad * shift we should go up one level and check it again 1505 1.5 riastrad * unless one level up is the root level. 1506 1.5 riastrad */ 1507 1.5 riastrad if (!amdgpu_vm_pt_ancestor(&cursor)) 1508 1.5 riastrad return -ENOENT; 1509 1.5 riastrad continue; 1510 1.5 riastrad } 1511 1.5 riastrad 1512 1.5 riastrad /* Looks good so far, calculate parameters for the update */ 1513 1.5 riastrad incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift; 1514 1.5 riastrad mask = amdgpu_vm_entries_mask(adev, cursor.level); 1515 1.5 riastrad pe_start = ((cursor.pfn >> shift) & mask) * 8; 1516 1.5 riastrad entry_end = (uint64_t)(mask + 1) << shift; 1517 1.5 riastrad entry_end += cursor.pfn & ~(entry_end - 1); 1518 1.5 riastrad entry_end = min(entry_end, end); 1519 1.5 riastrad 1520 1.5 riastrad do { 1521 1.5 riastrad uint64_t upd_end = min(entry_end, frag_end); 1522 1.5 riastrad unsigned nptes = (upd_end - frag_start) >> shift; 1523 1.5 riastrad 1524 1.5 riastrad amdgpu_vm_update_flags(params, pt, cursor.level, 1525 1.5 riastrad pe_start, dst, nptes, incr, 1526 1.5 riastrad flags | AMDGPU_PTE_FRAG(frag)); 1527 1.5 riastrad 1528 1.5 riastrad pe_start += nptes * 8; 1529 1.5 riastrad dst += (uint64_t)nptes * AMDGPU_GPU_PAGE_SIZE << shift; 1530 1.5 riastrad 1531 1.5 riastrad frag_start = upd_end; 1532 1.5 riastrad if (frag_start >= frag_end) { 1533 1.5 riastrad /* figure out the next fragment */ 1534 1.5 riastrad amdgpu_vm_fragment(params, frag_start, end, 1535 1.5 riastrad flags, &frag, &frag_end); 1536 1.5 riastrad if (frag < shift) 1537 1.5 riastrad break; 1538 1.5 riastrad } 1539 1.5 riastrad } while (frag_start < entry_end); 1540 1.5 riastrad 1541 1.5 riastrad if (amdgpu_vm_pt_descendant(adev, &cursor)) { 1542 1.5 riastrad /* Free all child entries. 1543 1.5 riastrad * Update the tables with the flags and addresses and free up subsequent 1544 1.5 riastrad * tables in the case of huge pages or freed up areas. 1545 1.5 riastrad * This is the maximum you can free, because all other page tables are not 1546 1.5 riastrad * completely covered by the range and so potentially still in use. 1547 1.5 riastrad */ 1548 1.5 riastrad while (cursor.pfn < frag_start) { 1549 1.5 riastrad amdgpu_vm_free_pts(adev, params->vm, &cursor); 1550 1.5 riastrad amdgpu_vm_pt_next(adev, &cursor); 1551 1.5 riastrad } 1552 1.5 riastrad 1553 1.5 riastrad } else if (frag >= shift) { 1554 1.5 riastrad /* or just move on to the next on the same level. */ 1555 1.5 riastrad amdgpu_vm_pt_next(adev, &cursor); 1556 1.5 riastrad } 1557 1.5 riastrad } 1558 1.5 riastrad 1559 1.5 riastrad return 0; 1560 1.5 riastrad } 1561 1.5 riastrad 1562 1.5 riastrad /** 1563 1.5 riastrad * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table 1564 1.5 riastrad * 1565 1.5 riastrad * @adev: amdgpu_device pointer 1566 1.5 riastrad * @vm: requested vm 1567 1.5 riastrad * @direct: direct submission in a page fault 1568 1.5 riastrad * @exclusive: fence we need to sync to 1569 1.5 riastrad * @start: start of mapped range 1570 1.5 riastrad * @last: last mapped entry 1571 1.5 riastrad * @flags: flags for the entries 1572 1.5 riastrad * @addr: addr to set the area to 1573 1.5 riastrad * @pages_addr: DMA addresses to use for mapping 1574 1.5 riastrad * @fence: optional resulting fence 1575 1.5 riastrad * 1576 1.5 riastrad * Fill in the page table entries between @start and @last. 1577 1.5 riastrad * 1578 1.5 riastrad * Returns: 1579 1.5 riastrad * 0 for success, -EINVAL for failure. 1580 1.5 riastrad */ 1581 1.5 riastrad static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, 1582 1.5 riastrad struct amdgpu_vm *vm, bool direct, 1583 1.5 riastrad struct dma_fence *exclusive, 1584 1.5 riastrad uint64_t start, uint64_t last, 1585 1.5 riastrad uint64_t flags, uint64_t addr, 1586 1.7 riastrad #ifdef __NetBSD__ 1587 1.7 riastrad bus_dma_segment_t *pages_addr, 1588 1.7 riastrad #else 1589 1.5 riastrad dma_addr_t *pages_addr, 1590 1.7 riastrad #endif 1591 1.5 riastrad struct dma_fence **fence) 1592 1.5 riastrad { 1593 1.5 riastrad struct amdgpu_vm_update_params params; 1594 1.5 riastrad void *owner = AMDGPU_FENCE_OWNER_VM; 1595 1.5 riastrad int r; 1596 1.5 riastrad 1597 1.5 riastrad memset(¶ms, 0, sizeof(params)); 1598 1.5 riastrad params.adev = adev; 1599 1.5 riastrad params.vm = vm; 1600 1.5 riastrad params.direct = direct; 1601 1.5 riastrad params.pages_addr = pages_addr; 1602 1.5 riastrad 1603 1.5 riastrad /* sync to everything except eviction fences on unmapping */ 1604 1.5 riastrad if (!(flags & AMDGPU_PTE_VALID)) 1605 1.5 riastrad owner = AMDGPU_FENCE_OWNER_KFD; 1606 1.5 riastrad 1607 1.5 riastrad amdgpu_vm_eviction_lock(vm); 1608 1.5 riastrad if (vm->evicting) { 1609 1.5 riastrad r = -EBUSY; 1610 1.5 riastrad goto error_unlock; 1611 1.5 riastrad } 1612 1.5 riastrad 1613 1.5 riastrad r = vm->update_funcs->prepare(¶ms, owner, exclusive); 1614 1.5 riastrad if (r) 1615 1.5 riastrad goto error_unlock; 1616 1.5 riastrad 1617 1.5 riastrad r = amdgpu_vm_update_ptes(¶ms, start, last + 1, addr, flags); 1618 1.5 riastrad if (r) 1619 1.5 riastrad goto error_unlock; 1620 1.5 riastrad 1621 1.5 riastrad r = vm->update_funcs->commit(¶ms, fence); 1622 1.5 riastrad 1623 1.5 riastrad error_unlock: 1624 1.5 riastrad amdgpu_vm_eviction_unlock(vm); 1625 1.5 riastrad return r; 1626 1.5 riastrad } 1627 1.5 riastrad 1628 1.5 riastrad /** 1629 1.5 riastrad * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks 1630 1.5 riastrad * 1631 1.5 riastrad * @adev: amdgpu_device pointer 1632 1.5 riastrad * @exclusive: fence we need to sync to 1633 1.5 riastrad * @pages_addr: DMA addresses to use for mapping 1634 1.5 riastrad * @vm: requested vm 1635 1.5 riastrad * @mapping: mapped range and flags to use for the update 1636 1.5 riastrad * @flags: HW flags for the mapping 1637 1.5 riastrad * @bo_adev: amdgpu_device pointer that bo actually been allocated 1638 1.5 riastrad * @nodes: array of drm_mm_nodes with the MC addresses 1639 1.5 riastrad * @fence: optional resulting fence 1640 1.5 riastrad * 1641 1.5 riastrad * Split the mapping into smaller chunks so that each update fits 1642 1.5 riastrad * into a SDMA IB. 1643 1.5 riastrad * 1644 1.5 riastrad * Returns: 1645 1.5 riastrad * 0 for success, -EINVAL for failure. 1646 1.5 riastrad */ 1647 1.5 riastrad static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, 1648 1.5 riastrad struct dma_fence *exclusive, 1649 1.7 riastrad #ifdef __NetBSD__ 1650 1.7 riastrad bus_dma_segment_t *pages_addr, 1651 1.7 riastrad #else 1652 1.5 riastrad dma_addr_t *pages_addr, 1653 1.7 riastrad #endif 1654 1.5 riastrad struct amdgpu_vm *vm, 1655 1.5 riastrad struct amdgpu_bo_va_mapping *mapping, 1656 1.5 riastrad uint64_t flags, 1657 1.5 riastrad struct amdgpu_device *bo_adev, 1658 1.5 riastrad struct drm_mm_node *nodes, 1659 1.5 riastrad struct dma_fence **fence) 1660 1.5 riastrad { 1661 1.5 riastrad unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size; 1662 1.5 riastrad uint64_t pfn, start = mapping->start; 1663 1.5 riastrad int r; 1664 1.5 riastrad 1665 1.5 riastrad /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here 1666 1.5 riastrad * but in case of something, we filter the flags in first place 1667 1.5 riastrad */ 1668 1.5 riastrad if (!(mapping->flags & AMDGPU_PTE_READABLE)) 1669 1.5 riastrad flags &= ~AMDGPU_PTE_READABLE; 1670 1.5 riastrad if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) 1671 1.5 riastrad flags &= ~AMDGPU_PTE_WRITEABLE; 1672 1.5 riastrad 1673 1.5 riastrad /* Apply ASIC specific mapping flags */ 1674 1.5 riastrad amdgpu_gmc_get_vm_pte(adev, mapping, &flags); 1675 1.5 riastrad 1676 1.5 riastrad trace_amdgpu_vm_bo_update(mapping); 1677 1.5 riastrad 1678 1.5 riastrad pfn = mapping->offset >> PAGE_SHIFT; 1679 1.5 riastrad if (nodes) { 1680 1.5 riastrad while (pfn >= nodes->size) { 1681 1.5 riastrad pfn -= nodes->size; 1682 1.5 riastrad ++nodes; 1683 1.5 riastrad } 1684 1.5 riastrad } 1685 1.5 riastrad 1686 1.5 riastrad do { 1687 1.7 riastrad #ifdef __NetBSD__ 1688 1.7 riastrad bus_dma_segment_t *dma_addr = NULL; 1689 1.7 riastrad #else 1690 1.5 riastrad dma_addr_t *dma_addr = NULL; 1691 1.7 riastrad #endif 1692 1.5 riastrad uint64_t max_entries; 1693 1.5 riastrad uint64_t addr, last; 1694 1.5 riastrad 1695 1.5 riastrad if (nodes) { 1696 1.5 riastrad addr = nodes->start << PAGE_SHIFT; 1697 1.5 riastrad max_entries = (nodes->size - pfn) * 1698 1.5 riastrad AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1699 1.5 riastrad } else { 1700 1.5 riastrad addr = 0; 1701 1.5 riastrad max_entries = S64_MAX; 1702 1.5 riastrad } 1703 1.5 riastrad 1704 1.5 riastrad if (pages_addr) { 1705 1.5 riastrad uint64_t count; 1706 1.5 riastrad 1707 1.5 riastrad for (count = 1; 1708 1.5 riastrad count < max_entries / AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1709 1.5 riastrad ++count) { 1710 1.5 riastrad uint64_t idx = pfn + count; 1711 1.5 riastrad 1712 1.7 riastrad #ifdef __NetBSD__ 1713 1.7 riastrad KASSERT(pages_addr[idx].ds_len == PAGE_SIZE); 1714 1.7 riastrad if (pages_addr[idx].ds_addr != 1715 1.7 riastrad pages_addr[idx - 1].ds_addr + PAGE_SIZE) 1716 1.7 riastrad break; 1717 1.7 riastrad #else 1718 1.5 riastrad if (pages_addr[idx] != 1719 1.5 riastrad (pages_addr[idx - 1] + PAGE_SIZE)) 1720 1.5 riastrad break; 1721 1.7 riastrad #endif 1722 1.5 riastrad } 1723 1.5 riastrad 1724 1.5 riastrad if (count < min_linear_pages) { 1725 1.5 riastrad addr = pfn << PAGE_SHIFT; 1726 1.5 riastrad dma_addr = pages_addr; 1727 1.5 riastrad } else { 1728 1.7 riastrad #ifdef __NetBSD__ 1729 1.7 riastrad KASSERT(pages_addr[pfn].ds_len == PAGE_SIZE); 1730 1.7 riastrad addr = pages_addr[pfn].ds_addr; 1731 1.7 riastrad #else 1732 1.5 riastrad addr = pages_addr[pfn]; 1733 1.7 riastrad #endif 1734 1.5 riastrad max_entries = count * 1735 1.5 riastrad AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1736 1.5 riastrad } 1737 1.5 riastrad 1738 1.5 riastrad } else if (flags & AMDGPU_PTE_VALID) { 1739 1.5 riastrad addr += bo_adev->vm_manager.vram_base_offset; 1740 1.5 riastrad addr += pfn << PAGE_SHIFT; 1741 1.5 riastrad } 1742 1.5 riastrad 1743 1.5 riastrad last = min((uint64_t)mapping->last, start + max_entries - 1); 1744 1.5 riastrad r = amdgpu_vm_bo_update_mapping(adev, vm, false, exclusive, 1745 1.5 riastrad start, last, flags, addr, 1746 1.5 riastrad dma_addr, fence); 1747 1.5 riastrad if (r) 1748 1.5 riastrad return r; 1749 1.5 riastrad 1750 1.5 riastrad pfn += (last - start + 1) / AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1751 1.5 riastrad if (nodes && nodes->size == pfn) { 1752 1.5 riastrad pfn = 0; 1753 1.5 riastrad ++nodes; 1754 1.5 riastrad } 1755 1.5 riastrad start = last + 1; 1756 1.5 riastrad 1757 1.5 riastrad } while (unlikely(start != mapping->last + 1)); 1758 1.5 riastrad 1759 1.5 riastrad return 0; 1760 1.5 riastrad } 1761 1.5 riastrad 1762 1.5 riastrad /** 1763 1.5 riastrad * amdgpu_vm_bo_update - update all BO mappings in the vm page table 1764 1.5 riastrad * 1765 1.5 riastrad * @adev: amdgpu_device pointer 1766 1.5 riastrad * @bo_va: requested BO and VM object 1767 1.5 riastrad * @clear: if true clear the entries 1768 1.5 riastrad * 1769 1.5 riastrad * Fill in the page table entries for @bo_va. 1770 1.5 riastrad * 1771 1.5 riastrad * Returns: 1772 1.5 riastrad * 0 for success, -EINVAL for failure. 1773 1.5 riastrad */ 1774 1.5 riastrad int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, 1775 1.5 riastrad bool clear) 1776 1.5 riastrad { 1777 1.5 riastrad struct amdgpu_bo *bo = bo_va->base.bo; 1778 1.5 riastrad struct amdgpu_vm *vm = bo_va->base.vm; 1779 1.5 riastrad struct amdgpu_bo_va_mapping *mapping; 1780 1.7 riastrad #ifdef __NetBSD__ 1781 1.7 riastrad bus_dma_segment_t *pages_addr = NULL; 1782 1.7 riastrad #else 1783 1.5 riastrad dma_addr_t *pages_addr = NULL; 1784 1.7 riastrad #endif 1785 1.5 riastrad struct ttm_mem_reg *mem; 1786 1.5 riastrad struct drm_mm_node *nodes; 1787 1.5 riastrad struct dma_fence *exclusive, **last_update; 1788 1.5 riastrad uint64_t flags; 1789 1.5 riastrad struct amdgpu_device *bo_adev = adev; 1790 1.5 riastrad int r; 1791 1.5 riastrad 1792 1.5 riastrad if (clear || !bo) { 1793 1.5 riastrad mem = NULL; 1794 1.5 riastrad nodes = NULL; 1795 1.5 riastrad exclusive = NULL; 1796 1.5 riastrad } else { 1797 1.5 riastrad struct ttm_dma_tt *ttm; 1798 1.5 riastrad 1799 1.5 riastrad mem = &bo->tbo.mem; 1800 1.5 riastrad nodes = mem->mm_node; 1801 1.5 riastrad if (mem->mem_type == TTM_PL_TT) { 1802 1.5 riastrad ttm = container_of(bo->tbo.ttm, struct ttm_dma_tt, ttm); 1803 1.7 riastrad #ifdef __NetBSD__ 1804 1.7 riastrad pages_addr = ttm->dma_address->dm_segs; 1805 1.7 riastrad #else 1806 1.5 riastrad pages_addr = ttm->dma_address; 1807 1.7 riastrad #endif 1808 1.5 riastrad } 1809 1.5 riastrad exclusive = bo->tbo.moving; 1810 1.5 riastrad } 1811 1.5 riastrad 1812 1.5 riastrad if (bo) { 1813 1.5 riastrad flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem); 1814 1.5 riastrad bo_adev = amdgpu_ttm_adev(bo->tbo.bdev); 1815 1.5 riastrad } else { 1816 1.5 riastrad flags = 0x0; 1817 1.5 riastrad } 1818 1.5 riastrad 1819 1.5 riastrad if (clear || (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv)) 1820 1.5 riastrad last_update = &vm->last_update; 1821 1.5 riastrad else 1822 1.5 riastrad last_update = &bo_va->last_pt_update; 1823 1.5 riastrad 1824 1.5 riastrad if (!clear && bo_va->base.moved) { 1825 1.5 riastrad bo_va->base.moved = false; 1826 1.5 riastrad list_splice_init(&bo_va->valids, &bo_va->invalids); 1827 1.5 riastrad 1828 1.5 riastrad } else if (bo_va->cleared != clear) { 1829 1.5 riastrad list_splice_init(&bo_va->valids, &bo_va->invalids); 1830 1.5 riastrad } 1831 1.5 riastrad 1832 1.5 riastrad list_for_each_entry(mapping, &bo_va->invalids, list) { 1833 1.5 riastrad r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm, 1834 1.5 riastrad mapping, flags, bo_adev, nodes, 1835 1.5 riastrad last_update); 1836 1.5 riastrad if (r) 1837 1.5 riastrad return r; 1838 1.5 riastrad } 1839 1.5 riastrad 1840 1.5 riastrad /* If the BO is not in its preferred location add it back to 1841 1.5 riastrad * the evicted list so that it gets validated again on the 1842 1.5 riastrad * next command submission. 1843 1.5 riastrad */ 1844 1.5 riastrad if (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) { 1845 1.5 riastrad uint32_t mem_type = bo->tbo.mem.mem_type; 1846 1.5 riastrad 1847 1.5 riastrad if (!(bo->preferred_domains & 1848 1.5 riastrad amdgpu_mem_type_to_domain(mem_type))) 1849 1.5 riastrad amdgpu_vm_bo_evicted(&bo_va->base); 1850 1.5 riastrad else 1851 1.5 riastrad amdgpu_vm_bo_idle(&bo_va->base); 1852 1.5 riastrad } else { 1853 1.5 riastrad amdgpu_vm_bo_done(&bo_va->base); 1854 1.5 riastrad } 1855 1.5 riastrad 1856 1.5 riastrad list_splice_init(&bo_va->invalids, &bo_va->valids); 1857 1.5 riastrad bo_va->cleared = clear; 1858 1.5 riastrad 1859 1.5 riastrad if (trace_amdgpu_vm_bo_mapping_enabled()) { 1860 1.5 riastrad list_for_each_entry(mapping, &bo_va->valids, list) 1861 1.5 riastrad trace_amdgpu_vm_bo_mapping(mapping); 1862 1.5 riastrad } 1863 1.5 riastrad 1864 1.5 riastrad return 0; 1865 1.5 riastrad } 1866 1.5 riastrad 1867 1.5 riastrad /** 1868 1.5 riastrad * amdgpu_vm_update_prt_state - update the global PRT state 1869 1.5 riastrad * 1870 1.5 riastrad * @adev: amdgpu_device pointer 1871 1.5 riastrad */ 1872 1.5 riastrad static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) 1873 1.5 riastrad { 1874 1.5 riastrad unsigned long flags; 1875 1.5 riastrad bool enable; 1876 1.5 riastrad 1877 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); 1878 1.5 riastrad enable = !!atomic_read(&adev->vm_manager.num_prt_users); 1879 1.5 riastrad adev->gmc.gmc_funcs->set_prt(adev, enable); 1880 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); 1881 1.5 riastrad } 1882 1.5 riastrad 1883 1.5 riastrad /** 1884 1.5 riastrad * amdgpu_vm_prt_get - add a PRT user 1885 1.5 riastrad * 1886 1.5 riastrad * @adev: amdgpu_device pointer 1887 1.5 riastrad */ 1888 1.5 riastrad static void amdgpu_vm_prt_get(struct amdgpu_device *adev) 1889 1.5 riastrad { 1890 1.5 riastrad if (!adev->gmc.gmc_funcs->set_prt) 1891 1.5 riastrad return; 1892 1.5 riastrad 1893 1.5 riastrad if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) 1894 1.5 riastrad amdgpu_vm_update_prt_state(adev); 1895 1.5 riastrad } 1896 1.5 riastrad 1897 1.5 riastrad /** 1898 1.5 riastrad * amdgpu_vm_prt_put - drop a PRT user 1899 1.5 riastrad * 1900 1.5 riastrad * @adev: amdgpu_device pointer 1901 1.5 riastrad */ 1902 1.5 riastrad static void amdgpu_vm_prt_put(struct amdgpu_device *adev) 1903 1.5 riastrad { 1904 1.5 riastrad if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) 1905 1.5 riastrad amdgpu_vm_update_prt_state(adev); 1906 1.5 riastrad } 1907 1.5 riastrad 1908 1.5 riastrad /** 1909 1.5 riastrad * amdgpu_vm_prt_cb - callback for updating the PRT status 1910 1.5 riastrad * 1911 1.5 riastrad * @fence: fence for the callback 1912 1.5 riastrad * @_cb: the callback function 1913 1.5 riastrad */ 1914 1.5 riastrad static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) 1915 1.5 riastrad { 1916 1.5 riastrad struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); 1917 1.5 riastrad 1918 1.5 riastrad amdgpu_vm_prt_put(cb->adev); 1919 1.5 riastrad kfree(cb); 1920 1.5 riastrad } 1921 1.5 riastrad 1922 1.5 riastrad /** 1923 1.5 riastrad * amdgpu_vm_add_prt_cb - add callback for updating the PRT status 1924 1.5 riastrad * 1925 1.5 riastrad * @adev: amdgpu_device pointer 1926 1.5 riastrad * @fence: fence for the callback 1927 1.5 riastrad */ 1928 1.5 riastrad static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, 1929 1.5 riastrad struct dma_fence *fence) 1930 1.5 riastrad { 1931 1.5 riastrad struct amdgpu_prt_cb *cb; 1932 1.5 riastrad 1933 1.5 riastrad if (!adev->gmc.gmc_funcs->set_prt) 1934 1.5 riastrad return; 1935 1.5 riastrad 1936 1.5 riastrad cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); 1937 1.5 riastrad if (!cb) { 1938 1.5 riastrad /* Last resort when we are OOM */ 1939 1.5 riastrad if (fence) 1940 1.5 riastrad dma_fence_wait(fence, false); 1941 1.5 riastrad 1942 1.5 riastrad amdgpu_vm_prt_put(adev); 1943 1.5 riastrad } else { 1944 1.5 riastrad cb->adev = adev; 1945 1.5 riastrad if (!fence || dma_fence_add_callback(fence, &cb->cb, 1946 1.5 riastrad amdgpu_vm_prt_cb)) 1947 1.5 riastrad amdgpu_vm_prt_cb(fence, &cb->cb); 1948 1.5 riastrad } 1949 1.5 riastrad } 1950 1.5 riastrad 1951 1.5 riastrad /** 1952 1.5 riastrad * amdgpu_vm_free_mapping - free a mapping 1953 1.5 riastrad * 1954 1.5 riastrad * @adev: amdgpu_device pointer 1955 1.5 riastrad * @vm: requested vm 1956 1.5 riastrad * @mapping: mapping to be freed 1957 1.5 riastrad * @fence: fence of the unmap operation 1958 1.5 riastrad * 1959 1.5 riastrad * Free a mapping and make sure we decrease the PRT usage count if applicable. 1960 1.5 riastrad */ 1961 1.5 riastrad static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, 1962 1.5 riastrad struct amdgpu_vm *vm, 1963 1.5 riastrad struct amdgpu_bo_va_mapping *mapping, 1964 1.5 riastrad struct dma_fence *fence) 1965 1.5 riastrad { 1966 1.5 riastrad if (mapping->flags & AMDGPU_PTE_PRT) 1967 1.5 riastrad amdgpu_vm_add_prt_cb(adev, fence); 1968 1.5 riastrad kfree(mapping); 1969 1.5 riastrad } 1970 1.5 riastrad 1971 1.5 riastrad /** 1972 1.5 riastrad * amdgpu_vm_prt_fini - finish all prt mappings 1973 1.5 riastrad * 1974 1.5 riastrad * @adev: amdgpu_device pointer 1975 1.5 riastrad * @vm: requested vm 1976 1.5 riastrad * 1977 1.5 riastrad * Register a cleanup callback to disable PRT support after VM dies. 1978 1.5 riastrad */ 1979 1.5 riastrad static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 1980 1.5 riastrad { 1981 1.5 riastrad struct dma_resv *resv = vm->root.base.bo->tbo.base.resv; 1982 1.5 riastrad struct dma_fence *excl, **shared; 1983 1.5 riastrad unsigned i, shared_count; 1984 1.5 riastrad int r; 1985 1.5 riastrad 1986 1.5 riastrad r = dma_resv_get_fences_rcu(resv, &excl, 1987 1.5 riastrad &shared_count, &shared); 1988 1.5 riastrad if (r) { 1989 1.5 riastrad /* Not enough memory to grab the fence list, as last resort 1990 1.5 riastrad * block for all the fences to complete. 1991 1.5 riastrad */ 1992 1.5 riastrad dma_resv_wait_timeout_rcu(resv, true, false, 1993 1.5 riastrad MAX_SCHEDULE_TIMEOUT); 1994 1.5 riastrad return; 1995 1.5 riastrad } 1996 1.5 riastrad 1997 1.5 riastrad /* Add a callback for each fence in the reservation object */ 1998 1.5 riastrad amdgpu_vm_prt_get(adev); 1999 1.5 riastrad amdgpu_vm_add_prt_cb(adev, excl); 2000 1.5 riastrad 2001 1.5 riastrad for (i = 0; i < shared_count; ++i) { 2002 1.5 riastrad amdgpu_vm_prt_get(adev); 2003 1.5 riastrad amdgpu_vm_add_prt_cb(adev, shared[i]); 2004 1.5 riastrad } 2005 1.5 riastrad 2006 1.5 riastrad kfree(shared); 2007 1.5 riastrad } 2008 1.5 riastrad 2009 1.5 riastrad /** 2010 1.5 riastrad * amdgpu_vm_clear_freed - clear freed BOs in the PT 2011 1.5 riastrad * 2012 1.5 riastrad * @adev: amdgpu_device pointer 2013 1.5 riastrad * @vm: requested vm 2014 1.5 riastrad * @fence: optional resulting fence (unchanged if no work needed to be done 2015 1.5 riastrad * or if an error occurred) 2016 1.5 riastrad * 2017 1.5 riastrad * Make sure all freed BOs are cleared in the PT. 2018 1.5 riastrad * PTs have to be reserved and mutex must be locked! 2019 1.5 riastrad * 2020 1.5 riastrad * Returns: 2021 1.5 riastrad * 0 for success. 2022 1.5 riastrad * 2023 1.5 riastrad */ 2024 1.5 riastrad int amdgpu_vm_clear_freed(struct amdgpu_device *adev, 2025 1.5 riastrad struct amdgpu_vm *vm, 2026 1.5 riastrad struct dma_fence **fence) 2027 1.5 riastrad { 2028 1.5 riastrad struct amdgpu_bo_va_mapping *mapping; 2029 1.5 riastrad uint64_t init_pte_value = 0; 2030 1.5 riastrad struct dma_fence *f = NULL; 2031 1.5 riastrad int r; 2032 1.5 riastrad 2033 1.5 riastrad while (!list_empty(&vm->freed)) { 2034 1.5 riastrad mapping = list_first_entry(&vm->freed, 2035 1.5 riastrad struct amdgpu_bo_va_mapping, list); 2036 1.5 riastrad list_del(&mapping->list); 2037 1.5 riastrad 2038 1.5 riastrad if (vm->pte_support_ats && 2039 1.5 riastrad mapping->start < AMDGPU_GMC_HOLE_START) 2040 1.5 riastrad init_pte_value = AMDGPU_PTE_DEFAULT_ATC; 2041 1.5 riastrad 2042 1.5 riastrad r = amdgpu_vm_bo_update_mapping(adev, vm, false, NULL, 2043 1.5 riastrad mapping->start, mapping->last, 2044 1.5 riastrad init_pte_value, 0, NULL, &f); 2045 1.5 riastrad amdgpu_vm_free_mapping(adev, vm, mapping, f); 2046 1.5 riastrad if (r) { 2047 1.5 riastrad dma_fence_put(f); 2048 1.5 riastrad return r; 2049 1.5 riastrad } 2050 1.5 riastrad } 2051 1.5 riastrad 2052 1.5 riastrad if (fence && f) { 2053 1.5 riastrad dma_fence_put(*fence); 2054 1.5 riastrad *fence = f; 2055 1.5 riastrad } else { 2056 1.5 riastrad dma_fence_put(f); 2057 1.5 riastrad } 2058 1.5 riastrad 2059 1.5 riastrad return 0; 2060 1.5 riastrad 2061 1.5 riastrad } 2062 1.5 riastrad 2063 1.5 riastrad /** 2064 1.5 riastrad * amdgpu_vm_handle_moved - handle moved BOs in the PT 2065 1.5 riastrad * 2066 1.5 riastrad * @adev: amdgpu_device pointer 2067 1.5 riastrad * @vm: requested vm 2068 1.5 riastrad * 2069 1.5 riastrad * Make sure all BOs which are moved are updated in the PTs. 2070 1.5 riastrad * 2071 1.5 riastrad * Returns: 2072 1.5 riastrad * 0 for success. 2073 1.5 riastrad * 2074 1.5 riastrad * PTs have to be reserved! 2075 1.5 riastrad */ 2076 1.5 riastrad int amdgpu_vm_handle_moved(struct amdgpu_device *adev, 2077 1.5 riastrad struct amdgpu_vm *vm) 2078 1.5 riastrad { 2079 1.5 riastrad struct amdgpu_bo_va *bo_va, *tmp; 2080 1.5 riastrad struct dma_resv *resv; 2081 1.5 riastrad bool clear; 2082 1.5 riastrad int r; 2083 1.5 riastrad 2084 1.5 riastrad list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 2085 1.5 riastrad /* Per VM BOs never need to bo cleared in the page tables */ 2086 1.5 riastrad r = amdgpu_vm_bo_update(adev, bo_va, false); 2087 1.5 riastrad if (r) 2088 1.5 riastrad return r; 2089 1.5 riastrad } 2090 1.5 riastrad 2091 1.5 riastrad spin_lock(&vm->invalidated_lock); 2092 1.5 riastrad while (!list_empty(&vm->invalidated)) { 2093 1.5 riastrad bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, 2094 1.5 riastrad base.vm_status); 2095 1.5 riastrad resv = bo_va->base.bo->tbo.base.resv; 2096 1.5 riastrad spin_unlock(&vm->invalidated_lock); 2097 1.5 riastrad 2098 1.5 riastrad /* Try to reserve the BO to avoid clearing its ptes */ 2099 1.5 riastrad if (!amdgpu_vm_debug && dma_resv_trylock(resv)) 2100 1.5 riastrad clear = false; 2101 1.5 riastrad /* Somebody else is using the BO right now */ 2102 1.5 riastrad else 2103 1.5 riastrad clear = true; 2104 1.5 riastrad 2105 1.5 riastrad r = amdgpu_vm_bo_update(adev, bo_va, clear); 2106 1.5 riastrad if (r) 2107 1.5 riastrad return r; 2108 1.5 riastrad 2109 1.5 riastrad if (!clear) 2110 1.5 riastrad dma_resv_unlock(resv); 2111 1.5 riastrad spin_lock(&vm->invalidated_lock); 2112 1.5 riastrad } 2113 1.5 riastrad spin_unlock(&vm->invalidated_lock); 2114 1.5 riastrad 2115 1.5 riastrad return 0; 2116 1.5 riastrad } 2117 1.5 riastrad 2118 1.5 riastrad /** 2119 1.5 riastrad * amdgpu_vm_bo_add - add a bo to a specific vm 2120 1.5 riastrad * 2121 1.5 riastrad * @adev: amdgpu_device pointer 2122 1.5 riastrad * @vm: requested vm 2123 1.5 riastrad * @bo: amdgpu buffer object 2124 1.5 riastrad * 2125 1.5 riastrad * Add @bo into the requested vm. 2126 1.5 riastrad * Add @bo to the list of bos associated with the vm 2127 1.5 riastrad * 2128 1.5 riastrad * Returns: 2129 1.5 riastrad * Newly added bo_va or NULL for failure 2130 1.5 riastrad * 2131 1.5 riastrad * Object has to be reserved! 2132 1.5 riastrad */ 2133 1.5 riastrad struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, 2134 1.5 riastrad struct amdgpu_vm *vm, 2135 1.5 riastrad struct amdgpu_bo *bo) 2136 1.5 riastrad { 2137 1.5 riastrad struct amdgpu_bo_va *bo_va; 2138 1.5 riastrad 2139 1.5 riastrad bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); 2140 1.5 riastrad if (bo_va == NULL) { 2141 1.5 riastrad return NULL; 2142 1.5 riastrad } 2143 1.5 riastrad amdgpu_vm_bo_base_init(&bo_va->base, vm, bo); 2144 1.5 riastrad 2145 1.5 riastrad bo_va->ref_count = 1; 2146 1.5 riastrad INIT_LIST_HEAD(&bo_va->valids); 2147 1.5 riastrad INIT_LIST_HEAD(&bo_va->invalids); 2148 1.5 riastrad 2149 1.5 riastrad if (bo && amdgpu_xgmi_same_hive(adev, amdgpu_ttm_adev(bo->tbo.bdev)) && 2150 1.5 riastrad (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM)) { 2151 1.5 riastrad bo_va->is_xgmi = true; 2152 1.5 riastrad mutex_lock(&adev->vm_manager.lock_pstate); 2153 1.5 riastrad /* Power up XGMI if it can be potentially used */ 2154 1.5 riastrad if (++adev->vm_manager.xgmi_map_counter == 1) 2155 1.5 riastrad amdgpu_xgmi_set_pstate(adev, 1); 2156 1.5 riastrad mutex_unlock(&adev->vm_manager.lock_pstate); 2157 1.5 riastrad } 2158 1.5 riastrad 2159 1.5 riastrad return bo_va; 2160 1.5 riastrad } 2161 1.5 riastrad 2162 1.5 riastrad 2163 1.5 riastrad /** 2164 1.5 riastrad * amdgpu_vm_bo_insert_mapping - insert a new mapping 2165 1.5 riastrad * 2166 1.5 riastrad * @adev: amdgpu_device pointer 2167 1.5 riastrad * @bo_va: bo_va to store the address 2168 1.5 riastrad * @mapping: the mapping to insert 2169 1.5 riastrad * 2170 1.5 riastrad * Insert a new mapping into all structures. 2171 1.5 riastrad */ 2172 1.5 riastrad static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, 2173 1.5 riastrad struct amdgpu_bo_va *bo_va, 2174 1.5 riastrad struct amdgpu_bo_va_mapping *mapping) 2175 1.5 riastrad { 2176 1.5 riastrad struct amdgpu_vm *vm = bo_va->base.vm; 2177 1.5 riastrad struct amdgpu_bo *bo = bo_va->base.bo; 2178 1.5 riastrad 2179 1.5 riastrad mapping->bo_va = bo_va; 2180 1.5 riastrad list_add(&mapping->list, &bo_va->invalids); 2181 1.5 riastrad amdgpu_vm_it_insert(mapping, &vm->va); 2182 1.5 riastrad 2183 1.5 riastrad if (mapping->flags & AMDGPU_PTE_PRT) 2184 1.5 riastrad amdgpu_vm_prt_get(adev); 2185 1.5 riastrad 2186 1.5 riastrad if (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv && 2187 1.5 riastrad !bo_va->base.moved) { 2188 1.5 riastrad list_move(&bo_va->base.vm_status, &vm->moved); 2189 1.5 riastrad } 2190 1.5 riastrad trace_amdgpu_vm_bo_map(bo_va, mapping); 2191 1.5 riastrad } 2192 1.5 riastrad 2193 1.5 riastrad /** 2194 1.5 riastrad * amdgpu_vm_bo_map - map bo inside a vm 2195 1.5 riastrad * 2196 1.5 riastrad * @adev: amdgpu_device pointer 2197 1.5 riastrad * @bo_va: bo_va to store the address 2198 1.5 riastrad * @saddr: where to map the BO 2199 1.5 riastrad * @offset: requested offset in the BO 2200 1.5 riastrad * @size: BO size in bytes 2201 1.5 riastrad * @flags: attributes of pages (read/write/valid/etc.) 2202 1.5 riastrad * 2203 1.5 riastrad * Add a mapping of the BO at the specefied addr into the VM. 2204 1.5 riastrad * 2205 1.5 riastrad * Returns: 2206 1.5 riastrad * 0 for success, error for failure. 2207 1.5 riastrad * 2208 1.5 riastrad * Object has to be reserved and unreserved outside! 2209 1.5 riastrad */ 2210 1.5 riastrad int amdgpu_vm_bo_map(struct amdgpu_device *adev, 2211 1.5 riastrad struct amdgpu_bo_va *bo_va, 2212 1.5 riastrad uint64_t saddr, uint64_t offset, 2213 1.5 riastrad uint64_t size, uint64_t flags) 2214 1.5 riastrad { 2215 1.5 riastrad struct amdgpu_bo_va_mapping *mapping, *tmp; 2216 1.5 riastrad struct amdgpu_bo *bo = bo_va->base.bo; 2217 1.5 riastrad struct amdgpu_vm *vm = bo_va->base.vm; 2218 1.5 riastrad uint64_t eaddr; 2219 1.5 riastrad 2220 1.1 riastrad /* validate the parameters */ 2221 1.1 riastrad if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || 2222 1.1 riastrad size == 0 || size & AMDGPU_GPU_PAGE_MASK) 2223 1.1 riastrad return -EINVAL; 2224 1.1 riastrad 2225 1.1 riastrad /* make sure object fit at this offset */ 2226 1.1 riastrad eaddr = saddr + size - 1; 2227 1.5 riastrad if (saddr >= eaddr || 2228 1.5 riastrad (bo && offset + size > amdgpu_bo_size(bo))) 2229 1.1 riastrad return -EINVAL; 2230 1.1 riastrad 2231 1.1 riastrad saddr /= AMDGPU_GPU_PAGE_SIZE; 2232 1.1 riastrad eaddr /= AMDGPU_GPU_PAGE_SIZE; 2233 1.1 riastrad 2234 1.5 riastrad tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 2235 1.5 riastrad if (tmp) { 2236 1.1 riastrad /* bo and tmp overlap, invalid addr */ 2237 1.3 riastrad dev_err(adev->dev, "bo %p va 0x%010"PRIx64"-0x%010"PRIx64" conflict with " 2238 1.6 riastrad "0x%010"PRIx64"-0x%010"PRIx64"\n", bo, saddr, eaddr, 2239 1.5 riastrad tmp->start, tmp->last + 1); 2240 1.5 riastrad return -EINVAL; 2241 1.1 riastrad } 2242 1.1 riastrad 2243 1.1 riastrad mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 2244 1.5 riastrad if (!mapping) 2245 1.5 riastrad return -ENOMEM; 2246 1.1 riastrad 2247 1.5 riastrad mapping->start = saddr; 2248 1.5 riastrad mapping->last = eaddr; 2249 1.1 riastrad mapping->offset = offset; 2250 1.1 riastrad mapping->flags = flags; 2251 1.1 riastrad 2252 1.5 riastrad amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 2253 1.1 riastrad 2254 1.5 riastrad return 0; 2255 1.5 riastrad } 2256 1.1 riastrad 2257 1.5 riastrad /** 2258 1.5 riastrad * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings 2259 1.5 riastrad * 2260 1.5 riastrad * @adev: amdgpu_device pointer 2261 1.5 riastrad * @bo_va: bo_va to store the address 2262 1.5 riastrad * @saddr: where to map the BO 2263 1.5 riastrad * @offset: requested offset in the BO 2264 1.5 riastrad * @size: BO size in bytes 2265 1.5 riastrad * @flags: attributes of pages (read/write/valid/etc.) 2266 1.5 riastrad * 2267 1.5 riastrad * Add a mapping of the BO at the specefied addr into the VM. Replace existing 2268 1.5 riastrad * mappings as we do so. 2269 1.5 riastrad * 2270 1.5 riastrad * Returns: 2271 1.5 riastrad * 0 for success, error for failure. 2272 1.5 riastrad * 2273 1.5 riastrad * Object has to be reserved and unreserved outside! 2274 1.5 riastrad */ 2275 1.5 riastrad int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, 2276 1.5 riastrad struct amdgpu_bo_va *bo_va, 2277 1.5 riastrad uint64_t saddr, uint64_t offset, 2278 1.5 riastrad uint64_t size, uint64_t flags) 2279 1.5 riastrad { 2280 1.5 riastrad struct amdgpu_bo_va_mapping *mapping; 2281 1.5 riastrad struct amdgpu_bo *bo = bo_va->base.bo; 2282 1.5 riastrad uint64_t eaddr; 2283 1.5 riastrad int r; 2284 1.1 riastrad 2285 1.5 riastrad /* validate the parameters */ 2286 1.5 riastrad if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || 2287 1.5 riastrad size == 0 || size & AMDGPU_GPU_PAGE_MASK) 2288 1.5 riastrad return -EINVAL; 2289 1.1 riastrad 2290 1.5 riastrad /* make sure object fit at this offset */ 2291 1.5 riastrad eaddr = saddr + size - 1; 2292 1.5 riastrad if (saddr >= eaddr || 2293 1.5 riastrad (bo && offset + size > amdgpu_bo_size(bo))) 2294 1.5 riastrad return -EINVAL; 2295 1.1 riastrad 2296 1.5 riastrad /* Allocate all the needed memory */ 2297 1.5 riastrad mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 2298 1.5 riastrad if (!mapping) 2299 1.5 riastrad return -ENOMEM; 2300 1.1 riastrad 2301 1.5 riastrad r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size); 2302 1.5 riastrad if (r) { 2303 1.5 riastrad kfree(mapping); 2304 1.5 riastrad return r; 2305 1.1 riastrad } 2306 1.1 riastrad 2307 1.5 riastrad saddr /= AMDGPU_GPU_PAGE_SIZE; 2308 1.5 riastrad eaddr /= AMDGPU_GPU_PAGE_SIZE; 2309 1.5 riastrad 2310 1.5 riastrad mapping->start = saddr; 2311 1.5 riastrad mapping->last = eaddr; 2312 1.5 riastrad mapping->offset = offset; 2313 1.5 riastrad mapping->flags = flags; 2314 1.1 riastrad 2315 1.5 riastrad amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 2316 1.1 riastrad 2317 1.5 riastrad return 0; 2318 1.1 riastrad } 2319 1.1 riastrad 2320 1.1 riastrad /** 2321 1.1 riastrad * amdgpu_vm_bo_unmap - remove bo mapping from vm 2322 1.1 riastrad * 2323 1.1 riastrad * @adev: amdgpu_device pointer 2324 1.1 riastrad * @bo_va: bo_va to remove the address from 2325 1.1 riastrad * @saddr: where to the BO is mapped 2326 1.1 riastrad * 2327 1.1 riastrad * Remove a mapping of the BO at the specefied addr from the VM. 2328 1.5 riastrad * 2329 1.5 riastrad * Returns: 2330 1.5 riastrad * 0 for success, error for failure. 2331 1.1 riastrad * 2332 1.1 riastrad * Object has to be reserved and unreserved outside! 2333 1.1 riastrad */ 2334 1.1 riastrad int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, 2335 1.1 riastrad struct amdgpu_bo_va *bo_va, 2336 1.1 riastrad uint64_t saddr) 2337 1.1 riastrad { 2338 1.1 riastrad struct amdgpu_bo_va_mapping *mapping; 2339 1.5 riastrad struct amdgpu_vm *vm = bo_va->base.vm; 2340 1.1 riastrad bool valid = true; 2341 1.1 riastrad 2342 1.1 riastrad saddr /= AMDGPU_GPU_PAGE_SIZE; 2343 1.5 riastrad 2344 1.1 riastrad list_for_each_entry(mapping, &bo_va->valids, list) { 2345 1.5 riastrad if (mapping->start == saddr) 2346 1.1 riastrad break; 2347 1.1 riastrad } 2348 1.1 riastrad 2349 1.1 riastrad if (&mapping->list == &bo_va->valids) { 2350 1.1 riastrad valid = false; 2351 1.1 riastrad 2352 1.1 riastrad list_for_each_entry(mapping, &bo_va->invalids, list) { 2353 1.5 riastrad if (mapping->start == saddr) 2354 1.1 riastrad break; 2355 1.1 riastrad } 2356 1.1 riastrad 2357 1.5 riastrad if (&mapping->list == &bo_va->invalids) 2358 1.1 riastrad return -ENOENT; 2359 1.1 riastrad } 2360 1.5 riastrad 2361 1.1 riastrad list_del(&mapping->list); 2362 1.5 riastrad amdgpu_vm_it_remove(mapping, &vm->va); 2363 1.5 riastrad mapping->bo_va = NULL; 2364 1.1 riastrad trace_amdgpu_vm_bo_unmap(bo_va, mapping); 2365 1.1 riastrad 2366 1.5 riastrad if (valid) 2367 1.1 riastrad list_add(&mapping->list, &vm->freed); 2368 1.5 riastrad else 2369 1.5 riastrad amdgpu_vm_free_mapping(adev, vm, mapping, 2370 1.5 riastrad bo_va->last_pt_update); 2371 1.5 riastrad 2372 1.5 riastrad return 0; 2373 1.5 riastrad } 2374 1.5 riastrad 2375 1.5 riastrad /** 2376 1.5 riastrad * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range 2377 1.5 riastrad * 2378 1.5 riastrad * @adev: amdgpu_device pointer 2379 1.5 riastrad * @vm: VM structure to use 2380 1.5 riastrad * @saddr: start of the range 2381 1.5 riastrad * @size: size of the range 2382 1.5 riastrad * 2383 1.5 riastrad * Remove all mappings in a range, split them as appropriate. 2384 1.5 riastrad * 2385 1.5 riastrad * Returns: 2386 1.5 riastrad * 0 for success, error for failure. 2387 1.5 riastrad */ 2388 1.5 riastrad int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, 2389 1.5 riastrad struct amdgpu_vm *vm, 2390 1.5 riastrad uint64_t saddr, uint64_t size) 2391 1.5 riastrad { 2392 1.5 riastrad struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; 2393 1.5 riastrad LIST_HEAD(removed); 2394 1.5 riastrad uint64_t eaddr; 2395 1.5 riastrad 2396 1.5 riastrad eaddr = saddr + size - 1; 2397 1.5 riastrad saddr /= AMDGPU_GPU_PAGE_SIZE; 2398 1.5 riastrad eaddr /= AMDGPU_GPU_PAGE_SIZE; 2399 1.5 riastrad 2400 1.5 riastrad /* Allocate all the needed memory */ 2401 1.5 riastrad before = kzalloc(sizeof(*before), GFP_KERNEL); 2402 1.5 riastrad if (!before) 2403 1.5 riastrad return -ENOMEM; 2404 1.5 riastrad INIT_LIST_HEAD(&before->list); 2405 1.5 riastrad 2406 1.5 riastrad after = kzalloc(sizeof(*after), GFP_KERNEL); 2407 1.5 riastrad if (!after) { 2408 1.5 riastrad kfree(before); 2409 1.5 riastrad return -ENOMEM; 2410 1.5 riastrad } 2411 1.5 riastrad INIT_LIST_HEAD(&after->list); 2412 1.5 riastrad 2413 1.5 riastrad /* Now gather all removed mappings */ 2414 1.5 riastrad tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 2415 1.5 riastrad while (tmp) { 2416 1.5 riastrad /* Remember mapping split at the start */ 2417 1.5 riastrad if (tmp->start < saddr) { 2418 1.5 riastrad before->start = tmp->start; 2419 1.5 riastrad before->last = saddr - 1; 2420 1.5 riastrad before->offset = tmp->offset; 2421 1.5 riastrad before->flags = tmp->flags; 2422 1.5 riastrad before->bo_va = tmp->bo_va; 2423 1.5 riastrad list_add(&before->list, &tmp->bo_va->invalids); 2424 1.5 riastrad } 2425 1.5 riastrad 2426 1.5 riastrad /* Remember mapping split at the end */ 2427 1.5 riastrad if (tmp->last > eaddr) { 2428 1.5 riastrad after->start = eaddr + 1; 2429 1.5 riastrad after->last = tmp->last; 2430 1.5 riastrad after->offset = tmp->offset; 2431 1.5 riastrad after->offset += after->start - tmp->start; 2432 1.5 riastrad after->flags = tmp->flags; 2433 1.5 riastrad after->bo_va = tmp->bo_va; 2434 1.5 riastrad list_add(&after->list, &tmp->bo_va->invalids); 2435 1.5 riastrad } 2436 1.5 riastrad 2437 1.5 riastrad list_del(&tmp->list); 2438 1.5 riastrad list_add(&tmp->list, &removed); 2439 1.5 riastrad 2440 1.6 riastrad tmp = amdgpu_vm_it_iter_next(&vm->va, tmp, saddr, eaddr); 2441 1.5 riastrad } 2442 1.5 riastrad 2443 1.5 riastrad /* And free them up */ 2444 1.5 riastrad list_for_each_entry_safe(tmp, next, &removed, list) { 2445 1.5 riastrad amdgpu_vm_it_remove(tmp, &vm->va); 2446 1.5 riastrad list_del(&tmp->list); 2447 1.5 riastrad 2448 1.5 riastrad if (tmp->start < saddr) 2449 1.5 riastrad tmp->start = saddr; 2450 1.5 riastrad if (tmp->last > eaddr) 2451 1.5 riastrad tmp->last = eaddr; 2452 1.5 riastrad 2453 1.5 riastrad tmp->bo_va = NULL; 2454 1.5 riastrad list_add(&tmp->list, &vm->freed); 2455 1.5 riastrad trace_amdgpu_vm_bo_unmap(NULL, tmp); 2456 1.5 riastrad } 2457 1.5 riastrad 2458 1.5 riastrad /* Insert partial mapping before the range */ 2459 1.5 riastrad if (!list_empty(&before->list)) { 2460 1.5 riastrad amdgpu_vm_it_insert(before, &vm->va); 2461 1.5 riastrad if (before->flags & AMDGPU_PTE_PRT) 2462 1.5 riastrad amdgpu_vm_prt_get(adev); 2463 1.5 riastrad } else { 2464 1.5 riastrad kfree(before); 2465 1.5 riastrad } 2466 1.5 riastrad 2467 1.5 riastrad /* Insert partial mapping after the range */ 2468 1.5 riastrad if (!list_empty(&after->list)) { 2469 1.5 riastrad amdgpu_vm_it_insert(after, &vm->va); 2470 1.5 riastrad if (after->flags & AMDGPU_PTE_PRT) 2471 1.5 riastrad amdgpu_vm_prt_get(adev); 2472 1.1 riastrad } else { 2473 1.5 riastrad kfree(after); 2474 1.5 riastrad } 2475 1.5 riastrad 2476 1.5 riastrad return 0; 2477 1.5 riastrad } 2478 1.5 riastrad 2479 1.5 riastrad /** 2480 1.5 riastrad * amdgpu_vm_bo_lookup_mapping - find mapping by address 2481 1.5 riastrad * 2482 1.5 riastrad * @vm: the requested VM 2483 1.5 riastrad * @addr: the address 2484 1.5 riastrad * 2485 1.5 riastrad * Find a mapping by it's address. 2486 1.5 riastrad * 2487 1.5 riastrad * Returns: 2488 1.5 riastrad * The amdgpu_bo_va_mapping matching for addr or NULL 2489 1.5 riastrad * 2490 1.5 riastrad */ 2491 1.5 riastrad struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, 2492 1.5 riastrad uint64_t addr) 2493 1.5 riastrad { 2494 1.5 riastrad return amdgpu_vm_it_iter_first(&vm->va, addr, addr); 2495 1.5 riastrad } 2496 1.5 riastrad 2497 1.5 riastrad /** 2498 1.5 riastrad * amdgpu_vm_bo_trace_cs - trace all reserved mappings 2499 1.5 riastrad * 2500 1.5 riastrad * @vm: the requested vm 2501 1.5 riastrad * @ticket: CS ticket 2502 1.5 riastrad * 2503 1.5 riastrad * Trace all mappings of BOs reserved during a command submission. 2504 1.5 riastrad */ 2505 1.5 riastrad void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket) 2506 1.5 riastrad { 2507 1.5 riastrad struct amdgpu_bo_va_mapping *mapping; 2508 1.5 riastrad 2509 1.5 riastrad if (!trace_amdgpu_vm_bo_cs_enabled()) 2510 1.5 riastrad return; 2511 1.5 riastrad 2512 1.5 riastrad for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping; 2513 1.6 riastrad mapping = amdgpu_vm_it_iter_next(&vm->va, mapping, 0, U64_MAX)) { 2514 1.5 riastrad if (mapping->bo_va && mapping->bo_va->base.bo) { 2515 1.5 riastrad struct amdgpu_bo *bo; 2516 1.5 riastrad 2517 1.5 riastrad bo = mapping->bo_va->base.bo; 2518 1.5 riastrad if (dma_resv_locking_ctx(bo->tbo.base.resv) != 2519 1.5 riastrad ticket) 2520 1.5 riastrad continue; 2521 1.5 riastrad } 2522 1.5 riastrad 2523 1.5 riastrad trace_amdgpu_vm_bo_cs(mapping); 2524 1.1 riastrad } 2525 1.1 riastrad } 2526 1.1 riastrad 2527 1.1 riastrad /** 2528 1.1 riastrad * amdgpu_vm_bo_rmv - remove a bo to a specific vm 2529 1.1 riastrad * 2530 1.1 riastrad * @adev: amdgpu_device pointer 2531 1.1 riastrad * @bo_va: requested bo_va 2532 1.1 riastrad * 2533 1.5 riastrad * Remove @bo_va->bo from the requested vm. 2534 1.1 riastrad * 2535 1.1 riastrad * Object have to be reserved! 2536 1.1 riastrad */ 2537 1.1 riastrad void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, 2538 1.1 riastrad struct amdgpu_bo_va *bo_va) 2539 1.1 riastrad { 2540 1.1 riastrad struct amdgpu_bo_va_mapping *mapping, *next; 2541 1.5 riastrad struct amdgpu_bo *bo = bo_va->base.bo; 2542 1.5 riastrad struct amdgpu_vm *vm = bo_va->base.vm; 2543 1.5 riastrad struct amdgpu_vm_bo_base **base; 2544 1.5 riastrad 2545 1.5 riastrad if (bo) { 2546 1.5 riastrad if (bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) 2547 1.5 riastrad vm->bulk_moveable = false; 2548 1.5 riastrad 2549 1.5 riastrad for (base = &bo_va->base.bo->vm_bo; *base; 2550 1.5 riastrad base = &(*base)->next) { 2551 1.5 riastrad if (*base != &bo_va->base) 2552 1.5 riastrad continue; 2553 1.1 riastrad 2554 1.5 riastrad *base = bo_va->base.next; 2555 1.5 riastrad break; 2556 1.5 riastrad } 2557 1.5 riastrad } 2558 1.1 riastrad 2559 1.5 riastrad spin_lock(&vm->invalidated_lock); 2560 1.5 riastrad list_del(&bo_va->base.vm_status); 2561 1.5 riastrad spin_unlock(&vm->invalidated_lock); 2562 1.1 riastrad 2563 1.1 riastrad list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { 2564 1.1 riastrad list_del(&mapping->list); 2565 1.5 riastrad amdgpu_vm_it_remove(mapping, &vm->va); 2566 1.5 riastrad mapping->bo_va = NULL; 2567 1.1 riastrad trace_amdgpu_vm_bo_unmap(bo_va, mapping); 2568 1.1 riastrad list_add(&mapping->list, &vm->freed); 2569 1.1 riastrad } 2570 1.1 riastrad list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { 2571 1.1 riastrad list_del(&mapping->list); 2572 1.5 riastrad amdgpu_vm_it_remove(mapping, &vm->va); 2573 1.5 riastrad amdgpu_vm_free_mapping(adev, vm, mapping, 2574 1.5 riastrad bo_va->last_pt_update); 2575 1.5 riastrad } 2576 1.5 riastrad 2577 1.5 riastrad dma_fence_put(bo_va->last_pt_update); 2578 1.5 riastrad 2579 1.5 riastrad if (bo && bo_va->is_xgmi) { 2580 1.5 riastrad mutex_lock(&adev->vm_manager.lock_pstate); 2581 1.5 riastrad if (--adev->vm_manager.xgmi_map_counter == 0) 2582 1.5 riastrad amdgpu_xgmi_set_pstate(adev, 0); 2583 1.5 riastrad mutex_unlock(&adev->vm_manager.lock_pstate); 2584 1.1 riastrad } 2585 1.5 riastrad 2586 1.1 riastrad kfree(bo_va); 2587 1.1 riastrad } 2588 1.1 riastrad 2589 1.1 riastrad /** 2590 1.5 riastrad * amdgpu_vm_evictable - check if we can evict a VM 2591 1.5 riastrad * 2592 1.5 riastrad * @bo: A page table of the VM. 2593 1.5 riastrad * 2594 1.5 riastrad * Check if it is possible to evict a VM. 2595 1.5 riastrad */ 2596 1.5 riastrad bool amdgpu_vm_evictable(struct amdgpu_bo *bo) 2597 1.5 riastrad { 2598 1.5 riastrad struct amdgpu_vm_bo_base *bo_base = bo->vm_bo; 2599 1.5 riastrad 2600 1.5 riastrad /* Page tables of a destroyed VM can go away immediately */ 2601 1.5 riastrad if (!bo_base || !bo_base->vm) 2602 1.5 riastrad return true; 2603 1.5 riastrad 2604 1.5 riastrad /* Don't evict VM page tables while they are busy */ 2605 1.5 riastrad if (!dma_resv_test_signaled_rcu(bo->tbo.base.resv, true)) 2606 1.5 riastrad return false; 2607 1.5 riastrad 2608 1.5 riastrad /* Try to block ongoing updates */ 2609 1.5 riastrad if (!amdgpu_vm_eviction_trylock(bo_base->vm)) 2610 1.5 riastrad return false; 2611 1.5 riastrad 2612 1.5 riastrad /* Don't evict VM page tables while they are updated */ 2613 1.5 riastrad if (!dma_fence_is_signaled(bo_base->vm->last_direct) || 2614 1.5 riastrad !dma_fence_is_signaled(bo_base->vm->last_delayed)) { 2615 1.5 riastrad amdgpu_vm_eviction_unlock(bo_base->vm); 2616 1.5 riastrad return false; 2617 1.5 riastrad } 2618 1.5 riastrad 2619 1.5 riastrad bo_base->vm->evicting = true; 2620 1.5 riastrad amdgpu_vm_eviction_unlock(bo_base->vm); 2621 1.5 riastrad return true; 2622 1.5 riastrad } 2623 1.5 riastrad 2624 1.5 riastrad /** 2625 1.1 riastrad * amdgpu_vm_bo_invalidate - mark the bo as invalid 2626 1.1 riastrad * 2627 1.1 riastrad * @adev: amdgpu_device pointer 2628 1.1 riastrad * @bo: amdgpu buffer object 2629 1.5 riastrad * @evicted: is the BO evicted 2630 1.1 riastrad * 2631 1.5 riastrad * Mark @bo as invalid. 2632 1.1 riastrad */ 2633 1.1 riastrad void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, 2634 1.5 riastrad struct amdgpu_bo *bo, bool evicted) 2635 1.1 riastrad { 2636 1.5 riastrad struct amdgpu_vm_bo_base *bo_base; 2637 1.5 riastrad 2638 1.5 riastrad /* shadow bo doesn't have bo base, its validation needs its parent */ 2639 1.5 riastrad if (bo->parent && bo->parent->shadow == bo) 2640 1.5 riastrad bo = bo->parent; 2641 1.5 riastrad 2642 1.5 riastrad for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { 2643 1.5 riastrad struct amdgpu_vm *vm = bo_base->vm; 2644 1.5 riastrad 2645 1.5 riastrad if (evicted && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) { 2646 1.5 riastrad amdgpu_vm_bo_evicted(bo_base); 2647 1.5 riastrad continue; 2648 1.5 riastrad } 2649 1.5 riastrad 2650 1.5 riastrad if (bo_base->moved) 2651 1.5 riastrad continue; 2652 1.5 riastrad bo_base->moved = true; 2653 1.1 riastrad 2654 1.5 riastrad if (bo->tbo.type == ttm_bo_type_kernel) 2655 1.5 riastrad amdgpu_vm_bo_relocated(bo_base); 2656 1.5 riastrad else if (bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) 2657 1.5 riastrad amdgpu_vm_bo_moved(bo_base); 2658 1.5 riastrad else 2659 1.5 riastrad amdgpu_vm_bo_invalidated(bo_base); 2660 1.1 riastrad } 2661 1.1 riastrad } 2662 1.1 riastrad 2663 1.1 riastrad /** 2664 1.5 riastrad * amdgpu_vm_get_block_size - calculate VM page table size as power of two 2665 1.5 riastrad * 2666 1.5 riastrad * @vm_size: VM size 2667 1.5 riastrad * 2668 1.5 riastrad * Returns: 2669 1.5 riastrad * VM page table as power of two 2670 1.5 riastrad */ 2671 1.5 riastrad static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) 2672 1.5 riastrad { 2673 1.5 riastrad /* Total bits covered by PD + PTs */ 2674 1.5 riastrad unsigned bits = ilog2(vm_size) + 18; 2675 1.5 riastrad 2676 1.5 riastrad /* Make sure the PD is 4K in size up to 8GB address space. 2677 1.5 riastrad Above that split equal between PD and PTs */ 2678 1.5 riastrad if (vm_size <= 8) 2679 1.5 riastrad return (bits - 9); 2680 1.5 riastrad else 2681 1.5 riastrad return ((bits + 3) / 2); 2682 1.5 riastrad } 2683 1.5 riastrad 2684 1.5 riastrad /** 2685 1.5 riastrad * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size 2686 1.5 riastrad * 2687 1.5 riastrad * @adev: amdgpu_device pointer 2688 1.5 riastrad * @min_vm_size: the minimum vm size in GB if it's set auto 2689 1.5 riastrad * @fragment_size_default: Default PTE fragment size 2690 1.5 riastrad * @max_level: max VMPT level 2691 1.5 riastrad * @max_bits: max address space size in bits 2692 1.5 riastrad * 2693 1.5 riastrad */ 2694 1.5 riastrad void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size, 2695 1.5 riastrad uint32_t fragment_size_default, unsigned max_level, 2696 1.5 riastrad unsigned max_bits) 2697 1.5 riastrad { 2698 1.5 riastrad unsigned int max_size = 1 << (max_bits - 30); 2699 1.5 riastrad unsigned int vm_size; 2700 1.5 riastrad uint64_t tmp; 2701 1.5 riastrad 2702 1.5 riastrad /* adjust vm size first */ 2703 1.5 riastrad if (amdgpu_vm_size != -1) { 2704 1.5 riastrad vm_size = amdgpu_vm_size; 2705 1.5 riastrad if (vm_size > max_size) { 2706 1.5 riastrad dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", 2707 1.5 riastrad amdgpu_vm_size, max_size); 2708 1.5 riastrad vm_size = max_size; 2709 1.5 riastrad } 2710 1.5 riastrad } else { 2711 1.5 riastrad struct sysinfo si; 2712 1.5 riastrad unsigned int phys_ram_gb; 2713 1.5 riastrad 2714 1.5 riastrad /* Optimal VM size depends on the amount of physical 2715 1.5 riastrad * RAM available. Underlying requirements and 2716 1.5 riastrad * assumptions: 2717 1.5 riastrad * 2718 1.5 riastrad * - Need to map system memory and VRAM from all GPUs 2719 1.5 riastrad * - VRAM from other GPUs not known here 2720 1.5 riastrad * - Assume VRAM <= system memory 2721 1.5 riastrad * - On GFX8 and older, VM space can be segmented for 2722 1.5 riastrad * different MTYPEs 2723 1.5 riastrad * - Need to allow room for fragmentation, guard pages etc. 2724 1.5 riastrad * 2725 1.5 riastrad * This adds up to a rough guess of system memory x3. 2726 1.5 riastrad * Round up to power of two to maximize the available 2727 1.5 riastrad * VM size with the given page table size. 2728 1.5 riastrad */ 2729 1.5 riastrad si_meminfo(&si); 2730 1.5 riastrad phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit + 2731 1.5 riastrad (1 << 30) - 1) >> 30; 2732 1.5 riastrad vm_size = roundup_pow_of_two( 2733 1.5 riastrad min(max(phys_ram_gb * 3, min_vm_size), max_size)); 2734 1.5 riastrad } 2735 1.5 riastrad 2736 1.5 riastrad adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; 2737 1.5 riastrad 2738 1.5 riastrad tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); 2739 1.5 riastrad if (amdgpu_vm_block_size != -1) 2740 1.5 riastrad tmp >>= amdgpu_vm_block_size - 9; 2741 1.5 riastrad tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; 2742 1.5 riastrad adev->vm_manager.num_level = min(max_level, (unsigned)tmp); 2743 1.5 riastrad switch (adev->vm_manager.num_level) { 2744 1.5 riastrad case 3: 2745 1.5 riastrad adev->vm_manager.root_level = AMDGPU_VM_PDB2; 2746 1.5 riastrad break; 2747 1.5 riastrad case 2: 2748 1.5 riastrad adev->vm_manager.root_level = AMDGPU_VM_PDB1; 2749 1.5 riastrad break; 2750 1.5 riastrad case 1: 2751 1.5 riastrad adev->vm_manager.root_level = AMDGPU_VM_PDB0; 2752 1.5 riastrad break; 2753 1.5 riastrad default: 2754 1.5 riastrad dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); 2755 1.5 riastrad } 2756 1.5 riastrad /* block size depends on vm size and hw setup*/ 2757 1.5 riastrad if (amdgpu_vm_block_size != -1) 2758 1.5 riastrad adev->vm_manager.block_size = 2759 1.5 riastrad min((unsigned)amdgpu_vm_block_size, max_bits 2760 1.5 riastrad - AMDGPU_GPU_PAGE_SHIFT 2761 1.5 riastrad - 9 * adev->vm_manager.num_level); 2762 1.5 riastrad else if (adev->vm_manager.num_level > 1) 2763 1.5 riastrad adev->vm_manager.block_size = 9; 2764 1.5 riastrad else 2765 1.5 riastrad adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); 2766 1.5 riastrad 2767 1.5 riastrad if (amdgpu_vm_fragment_size == -1) 2768 1.5 riastrad adev->vm_manager.fragment_size = fragment_size_default; 2769 1.5 riastrad else 2770 1.5 riastrad adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; 2771 1.5 riastrad 2772 1.5 riastrad DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", 2773 1.5 riastrad vm_size, adev->vm_manager.num_level + 1, 2774 1.5 riastrad adev->vm_manager.block_size, 2775 1.5 riastrad adev->vm_manager.fragment_size); 2776 1.5 riastrad } 2777 1.5 riastrad 2778 1.5 riastrad /** 2779 1.5 riastrad * amdgpu_vm_wait_idle - wait for the VM to become idle 2780 1.5 riastrad * 2781 1.5 riastrad * @vm: VM object to wait for 2782 1.5 riastrad * @timeout: timeout to wait for VM to become idle 2783 1.5 riastrad */ 2784 1.5 riastrad long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout) 2785 1.5 riastrad { 2786 1.5 riastrad timeout = dma_resv_wait_timeout_rcu(vm->root.base.bo->tbo.base.resv, 2787 1.5 riastrad true, true, timeout); 2788 1.5 riastrad if (timeout <= 0) 2789 1.5 riastrad return timeout; 2790 1.5 riastrad 2791 1.5 riastrad timeout = dma_fence_wait_timeout(vm->last_direct, true, timeout); 2792 1.5 riastrad if (timeout <= 0) 2793 1.5 riastrad return timeout; 2794 1.5 riastrad 2795 1.5 riastrad return dma_fence_wait_timeout(vm->last_delayed, true, timeout); 2796 1.5 riastrad } 2797 1.5 riastrad 2798 1.5 riastrad /** 2799 1.1 riastrad * amdgpu_vm_init - initialize a vm instance 2800 1.1 riastrad * 2801 1.1 riastrad * @adev: amdgpu_device pointer 2802 1.1 riastrad * @vm: requested vm 2803 1.5 riastrad * @vm_context: Indicates if it GFX or Compute context 2804 1.5 riastrad * @pasid: Process address space identifier 2805 1.1 riastrad * 2806 1.5 riastrad * Init @vm fields. 2807 1.5 riastrad * 2808 1.5 riastrad * Returns: 2809 1.5 riastrad * 0 for success, error for failure. 2810 1.1 riastrad */ 2811 1.5 riastrad int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, 2812 1.5 riastrad int vm_context, unsigned int pasid) 2813 1.1 riastrad { 2814 1.5 riastrad struct amdgpu_bo_param bp; 2815 1.5 riastrad struct amdgpu_bo *root; 2816 1.5 riastrad int r, i; 2817 1.5 riastrad 2818 1.3 riastrad #ifdef __NetBSD__ 2819 1.6 riastrad amdgpu_vm_it_init(&vm->va); 2820 1.3 riastrad #else 2821 1.5 riastrad vm->va = RB_ROOT_CACHED; 2822 1.3 riastrad #endif 2823 1.5 riastrad for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 2824 1.5 riastrad vm->reserved_vmid[i] = NULL; 2825 1.5 riastrad INIT_LIST_HEAD(&vm->evicted); 2826 1.5 riastrad INIT_LIST_HEAD(&vm->relocated); 2827 1.5 riastrad INIT_LIST_HEAD(&vm->moved); 2828 1.5 riastrad INIT_LIST_HEAD(&vm->idle); 2829 1.1 riastrad INIT_LIST_HEAD(&vm->invalidated); 2830 1.5 riastrad spin_lock_init(&vm->invalidated_lock); 2831 1.1 riastrad INIT_LIST_HEAD(&vm->freed); 2832 1.5 riastrad 2833 1.5 riastrad 2834 1.5 riastrad /* create scheduler entities for page table updates */ 2835 1.5 riastrad r = drm_sched_entity_init(&vm->direct, DRM_SCHED_PRIORITY_NORMAL, 2836 1.5 riastrad adev->vm_manager.vm_pte_scheds, 2837 1.5 riastrad adev->vm_manager.vm_pte_num_scheds, NULL); 2838 1.5 riastrad if (r) 2839 1.8 riastrad goto error_free_destroylock; 2840 1.5 riastrad 2841 1.5 riastrad r = drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL, 2842 1.5 riastrad adev->vm_manager.vm_pte_scheds, 2843 1.5 riastrad adev->vm_manager.vm_pte_num_scheds, NULL); 2844 1.5 riastrad if (r) 2845 1.5 riastrad goto error_free_direct; 2846 1.5 riastrad 2847 1.5 riastrad vm->pte_support_ats = false; 2848 1.5 riastrad vm->is_compute_context = false; 2849 1.5 riastrad 2850 1.5 riastrad if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) { 2851 1.5 riastrad vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2852 1.5 riastrad AMDGPU_VM_USE_CPU_FOR_COMPUTE); 2853 1.5 riastrad 2854 1.5 riastrad if (adev->asic_type == CHIP_RAVEN) 2855 1.5 riastrad vm->pte_support_ats = true; 2856 1.5 riastrad } else { 2857 1.5 riastrad vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2858 1.5 riastrad AMDGPU_VM_USE_CPU_FOR_GFX); 2859 1.5 riastrad } 2860 1.5 riastrad DRM_DEBUG_DRIVER("VM update mode is %s\n", 2861 1.5 riastrad vm->use_cpu_for_update ? "CPU" : "SDMA"); 2862 1.5 riastrad WARN_ONCE((vm->use_cpu_for_update && 2863 1.5 riastrad !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2864 1.5 riastrad "CPU update of VM recommended only for large BAR system\n"); 2865 1.5 riastrad 2866 1.5 riastrad if (vm->use_cpu_for_update) 2867 1.5 riastrad vm->update_funcs = &amdgpu_vm_cpu_funcs; 2868 1.5 riastrad else 2869 1.5 riastrad vm->update_funcs = &amdgpu_vm_sdma_funcs; 2870 1.5 riastrad vm->last_update = NULL; 2871 1.5 riastrad vm->last_direct = dma_fence_get_stub(); 2872 1.5 riastrad vm->last_delayed = dma_fence_get_stub(); 2873 1.5 riastrad 2874 1.5 riastrad mutex_init(&vm->eviction_lock); 2875 1.5 riastrad vm->evicting = false; 2876 1.5 riastrad 2877 1.5 riastrad amdgpu_vm_bo_param(adev, vm, adev->vm_manager.root_level, false, &bp); 2878 1.5 riastrad if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) 2879 1.5 riastrad bp.flags &= ~AMDGPU_GEM_CREATE_SHADOW; 2880 1.5 riastrad r = amdgpu_bo_create(adev, &bp, &root); 2881 1.5 riastrad if (r) 2882 1.5 riastrad goto error_free_delayed; 2883 1.5 riastrad 2884 1.5 riastrad r = amdgpu_bo_reserve(root, true); 2885 1.5 riastrad if (r) 2886 1.5 riastrad goto error_free_root; 2887 1.5 riastrad 2888 1.5 riastrad r = dma_resv_reserve_shared(root->tbo.base.resv, 1); 2889 1.5 riastrad if (r) 2890 1.5 riastrad goto error_unreserve; 2891 1.5 riastrad 2892 1.5 riastrad amdgpu_vm_bo_base_init(&vm->root.base, vm, root); 2893 1.5 riastrad 2894 1.5 riastrad r = amdgpu_vm_clear_bo(adev, vm, root, false); 2895 1.5 riastrad if (r) 2896 1.5 riastrad goto error_unreserve; 2897 1.5 riastrad 2898 1.5 riastrad amdgpu_bo_unreserve(vm->root.base.bo); 2899 1.5 riastrad 2900 1.5 riastrad if (pasid) { 2901 1.5 riastrad unsigned long flags; 2902 1.5 riastrad 2903 1.9 riastrad idr_preload(GFP_ATOMIC); 2904 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 2905 1.5 riastrad r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, 2906 1.5 riastrad GFP_ATOMIC); 2907 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 2908 1.9 riastrad idr_preload_end(); 2909 1.5 riastrad if (r < 0) 2910 1.5 riastrad goto error_free_root; 2911 1.5 riastrad 2912 1.5 riastrad vm->pasid = pasid; 2913 1.5 riastrad } 2914 1.5 riastrad 2915 1.5 riastrad INIT_KFIFO(vm->faults); 2916 1.5 riastrad 2917 1.5 riastrad return 0; 2918 1.5 riastrad 2919 1.5 riastrad error_unreserve: 2920 1.5 riastrad amdgpu_bo_unreserve(vm->root.base.bo); 2921 1.5 riastrad 2922 1.5 riastrad error_free_root: 2923 1.5 riastrad amdgpu_bo_unref(&vm->root.base.bo->shadow); 2924 1.5 riastrad amdgpu_bo_unref(&vm->root.base.bo); 2925 1.5 riastrad vm->root.base.bo = NULL; 2926 1.5 riastrad 2927 1.5 riastrad error_free_delayed: 2928 1.5 riastrad dma_fence_put(vm->last_direct); 2929 1.5 riastrad dma_fence_put(vm->last_delayed); 2930 1.5 riastrad drm_sched_entity_destroy(&vm->delayed); 2931 1.8 riastrad mutex_destroy(&vm->eviction_lock); 2932 1.5 riastrad 2933 1.5 riastrad error_free_direct: 2934 1.5 riastrad drm_sched_entity_destroy(&vm->direct); 2935 1.5 riastrad 2936 1.8 riastrad error_free_destroylock: 2937 1.8 riastrad spin_lock_destroy(&vm->invalidated_lock); 2938 1.8 riastrad 2939 1.5 riastrad return r; 2940 1.5 riastrad } 2941 1.5 riastrad 2942 1.5 riastrad /** 2943 1.5 riastrad * amdgpu_vm_check_clean_reserved - check if a VM is clean 2944 1.5 riastrad * 2945 1.5 riastrad * @adev: amdgpu_device pointer 2946 1.5 riastrad * @vm: the VM to check 2947 1.5 riastrad * 2948 1.5 riastrad * check all entries of the root PD, if any subsequent PDs are allocated, 2949 1.5 riastrad * it means there are page table creating and filling, and is no a clean 2950 1.5 riastrad * VM 2951 1.5 riastrad * 2952 1.5 riastrad * Returns: 2953 1.5 riastrad * 0 if this VM is clean 2954 1.5 riastrad */ 2955 1.5 riastrad static int amdgpu_vm_check_clean_reserved(struct amdgpu_device *adev, 2956 1.5 riastrad struct amdgpu_vm *vm) 2957 1.5 riastrad { 2958 1.5 riastrad enum amdgpu_vm_level root = adev->vm_manager.root_level; 2959 1.5 riastrad unsigned int entries = amdgpu_vm_num_entries(adev, root); 2960 1.5 riastrad unsigned int i = 0; 2961 1.5 riastrad 2962 1.5 riastrad if (!(vm->root.entries)) 2963 1.5 riastrad return 0; 2964 1.5 riastrad 2965 1.5 riastrad for (i = 0; i < entries; i++) { 2966 1.5 riastrad if (vm->root.entries[i].base.bo) 2967 1.5 riastrad return -EINVAL; 2968 1.1 riastrad } 2969 1.1 riastrad 2970 1.5 riastrad return 0; 2971 1.5 riastrad } 2972 1.5 riastrad 2973 1.5 riastrad /** 2974 1.5 riastrad * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM 2975 1.5 riastrad * 2976 1.5 riastrad * @adev: amdgpu_device pointer 2977 1.5 riastrad * @vm: requested vm 2978 1.5 riastrad * @pasid: pasid to use 2979 1.5 riastrad * 2980 1.5 riastrad * This only works on GFX VMs that don't have any BOs added and no 2981 1.5 riastrad * page tables allocated yet. 2982 1.5 riastrad * 2983 1.5 riastrad * Changes the following VM parameters: 2984 1.5 riastrad * - use_cpu_for_update 2985 1.5 riastrad * - pte_supports_ats 2986 1.5 riastrad * - pasid (old PASID is released, because compute manages its own PASIDs) 2987 1.5 riastrad * 2988 1.5 riastrad * Reinitializes the page directory to reflect the changed ATS 2989 1.5 riastrad * setting. 2990 1.5 riastrad * 2991 1.5 riastrad * Returns: 2992 1.5 riastrad * 0 for success, -errno for errors. 2993 1.5 riastrad */ 2994 1.5 riastrad int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm, 2995 1.5 riastrad unsigned int pasid) 2996 1.5 riastrad { 2997 1.5 riastrad bool pte_support_ats = (adev->asic_type == CHIP_RAVEN); 2998 1.5 riastrad int r; 2999 1.1 riastrad 3000 1.5 riastrad r = amdgpu_bo_reserve(vm->root.base.bo, true); 3001 1.1 riastrad if (r) 3002 1.1 riastrad return r; 3003 1.5 riastrad 3004 1.5 riastrad /* Sanity checks */ 3005 1.5 riastrad r = amdgpu_vm_check_clean_reserved(adev, vm); 3006 1.5 riastrad if (r) 3007 1.5 riastrad goto unreserve_bo; 3008 1.5 riastrad 3009 1.5 riastrad if (pasid) { 3010 1.5 riastrad unsigned long flags; 3011 1.5 riastrad 3012 1.9 riastrad idr_preload(GFP_ATOMIC); 3013 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3014 1.5 riastrad r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, 3015 1.5 riastrad GFP_ATOMIC); 3016 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3017 1.9 riastrad idr_preload_end(); 3018 1.5 riastrad 3019 1.5 riastrad if (r == -ENOSPC) 3020 1.5 riastrad goto unreserve_bo; 3021 1.5 riastrad r = 0; 3022 1.5 riastrad } 3023 1.5 riastrad 3024 1.5 riastrad /* Check if PD needs to be reinitialized and do it before 3025 1.5 riastrad * changing any other state, in case it fails. 3026 1.5 riastrad */ 3027 1.5 riastrad if (pte_support_ats != vm->pte_support_ats) { 3028 1.5 riastrad vm->pte_support_ats = pte_support_ats; 3029 1.5 riastrad r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo, false); 3030 1.5 riastrad if (r) 3031 1.5 riastrad goto free_idr; 3032 1.5 riastrad } 3033 1.5 riastrad 3034 1.5 riastrad /* Update VM state */ 3035 1.5 riastrad vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 3036 1.5 riastrad AMDGPU_VM_USE_CPU_FOR_COMPUTE); 3037 1.5 riastrad DRM_DEBUG_DRIVER("VM update mode is %s\n", 3038 1.5 riastrad vm->use_cpu_for_update ? "CPU" : "SDMA"); 3039 1.5 riastrad WARN_ONCE((vm->use_cpu_for_update && 3040 1.5 riastrad !amdgpu_gmc_vram_full_visible(&adev->gmc)), 3041 1.5 riastrad "CPU update of VM recommended only for large BAR system\n"); 3042 1.5 riastrad 3043 1.5 riastrad if (vm->use_cpu_for_update) 3044 1.5 riastrad vm->update_funcs = &amdgpu_vm_cpu_funcs; 3045 1.5 riastrad else 3046 1.5 riastrad vm->update_funcs = &amdgpu_vm_sdma_funcs; 3047 1.5 riastrad dma_fence_put(vm->last_update); 3048 1.5 riastrad vm->last_update = NULL; 3049 1.5 riastrad vm->is_compute_context = true; 3050 1.5 riastrad 3051 1.5 riastrad if (vm->pasid) { 3052 1.5 riastrad unsigned long flags; 3053 1.5 riastrad 3054 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3055 1.5 riastrad idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); 3056 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3057 1.5 riastrad 3058 1.5 riastrad /* Free the original amdgpu allocated pasid 3059 1.5 riastrad * Will be replaced with kfd allocated pasid 3060 1.5 riastrad */ 3061 1.5 riastrad amdgpu_pasid_free(vm->pasid); 3062 1.5 riastrad vm->pasid = 0; 3063 1.1 riastrad } 3064 1.5 riastrad 3065 1.5 riastrad /* Free the shadow bo for compute VM */ 3066 1.5 riastrad amdgpu_bo_unref(&vm->root.base.bo->shadow); 3067 1.5 riastrad 3068 1.5 riastrad if (pasid) 3069 1.5 riastrad vm->pasid = pasid; 3070 1.5 riastrad 3071 1.5 riastrad goto unreserve_bo; 3072 1.5 riastrad 3073 1.5 riastrad free_idr: 3074 1.5 riastrad if (pasid) { 3075 1.5 riastrad unsigned long flags; 3076 1.5 riastrad 3077 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3078 1.5 riastrad idr_remove(&adev->vm_manager.pasid_idr, pasid); 3079 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3080 1.1 riastrad } 3081 1.5 riastrad unreserve_bo: 3082 1.5 riastrad amdgpu_bo_unreserve(vm->root.base.bo); 3083 1.5 riastrad return r; 3084 1.5 riastrad } 3085 1.5 riastrad 3086 1.5 riastrad /** 3087 1.5 riastrad * amdgpu_vm_release_compute - release a compute vm 3088 1.5 riastrad * @adev: amdgpu_device pointer 3089 1.5 riastrad * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute 3090 1.5 riastrad * 3091 1.5 riastrad * This is a correspondant of amdgpu_vm_make_compute. It decouples compute 3092 1.5 riastrad * pasid from vm. Compute should stop use of vm after this call. 3093 1.5 riastrad */ 3094 1.5 riastrad void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) 3095 1.5 riastrad { 3096 1.5 riastrad if (vm->pasid) { 3097 1.5 riastrad unsigned long flags; 3098 1.1 riastrad 3099 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3100 1.5 riastrad idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); 3101 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3102 1.5 riastrad } 3103 1.5 riastrad vm->pasid = 0; 3104 1.5 riastrad vm->is_compute_context = false; 3105 1.1 riastrad } 3106 1.1 riastrad 3107 1.1 riastrad /** 3108 1.1 riastrad * amdgpu_vm_fini - tear down a vm instance 3109 1.1 riastrad * 3110 1.1 riastrad * @adev: amdgpu_device pointer 3111 1.1 riastrad * @vm: requested vm 3112 1.1 riastrad * 3113 1.5 riastrad * Tear down @vm. 3114 1.1 riastrad * Unbind the VM and remove all bos from the vm bo list 3115 1.1 riastrad */ 3116 1.1 riastrad void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 3117 1.1 riastrad { 3118 1.1 riastrad struct amdgpu_bo_va_mapping *mapping, *tmp; 3119 1.5 riastrad bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; 3120 1.5 riastrad struct amdgpu_bo *root; 3121 1.1 riastrad int i; 3122 1.1 riastrad 3123 1.11 riastrad FINI_KFIFO(vm->faults); 3124 1.11 riastrad 3125 1.5 riastrad amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); 3126 1.5 riastrad 3127 1.5 riastrad root = amdgpu_bo_ref(vm->root.base.bo); 3128 1.5 riastrad amdgpu_bo_reserve(root, true); 3129 1.5 riastrad if (vm->pasid) { 3130 1.5 riastrad unsigned long flags; 3131 1.5 riastrad 3132 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3133 1.5 riastrad idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); 3134 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3135 1.5 riastrad vm->pasid = 0; 3136 1.5 riastrad } 3137 1.5 riastrad 3138 1.5 riastrad dma_fence_wait(vm->last_direct, false); 3139 1.5 riastrad dma_fence_put(vm->last_direct); 3140 1.5 riastrad dma_fence_wait(vm->last_delayed, false); 3141 1.5 riastrad dma_fence_put(vm->last_delayed); 3142 1.5 riastrad 3143 1.5 riastrad list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { 3144 1.5 riastrad if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { 3145 1.5 riastrad amdgpu_vm_prt_fini(adev, vm); 3146 1.5 riastrad prt_fini_needed = false; 3147 1.5 riastrad } 3148 1.5 riastrad 3149 1.5 riastrad list_del(&mapping->list); 3150 1.5 riastrad amdgpu_vm_free_mapping(adev, vm, mapping, NULL); 3151 1.5 riastrad } 3152 1.5 riastrad 3153 1.5 riastrad amdgpu_vm_free_pts(adev, vm, NULL); 3154 1.5 riastrad amdgpu_bo_unreserve(root); 3155 1.5 riastrad amdgpu_bo_unref(&root); 3156 1.5 riastrad WARN_ON(vm->root.base.bo); 3157 1.5 riastrad 3158 1.5 riastrad drm_sched_entity_destroy(&vm->direct); 3159 1.5 riastrad drm_sched_entity_destroy(&vm->delayed); 3160 1.5 riastrad 3161 1.5 riastrad if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { 3162 1.1 riastrad dev_err(adev->dev, "still active bo inside vm\n"); 3163 1.1 riastrad } 3164 1.5 riastrad rbtree_postorder_for_each_entry_safe(mapping, tmp, 3165 1.5 riastrad &vm->va.rb_root, rb) { 3166 1.5 riastrad /* Don't remove the mapping here, we don't want to trigger a 3167 1.5 riastrad * rebalance and the tree is about to be destroyed anyway. 3168 1.5 riastrad */ 3169 1.1 riastrad list_del(&mapping->list); 3170 1.1 riastrad kfree(mapping); 3171 1.1 riastrad } 3172 1.1 riastrad 3173 1.5 riastrad dma_fence_put(vm->last_update); 3174 1.5 riastrad for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 3175 1.5 riastrad amdgpu_vmid_free_reserved(adev, vm, i); 3176 1.10 riastrad 3177 1.11 riastrad mutex_destroy(&vm->eviction_lock); 3178 1.10 riastrad spin_lock_destroy(&vm->invalidated_lock); 3179 1.5 riastrad } 3180 1.5 riastrad 3181 1.5 riastrad /** 3182 1.5 riastrad * amdgpu_vm_manager_init - init the VM manager 3183 1.5 riastrad * 3184 1.5 riastrad * @adev: amdgpu_device pointer 3185 1.5 riastrad * 3186 1.5 riastrad * Initialize the VM manager structures 3187 1.5 riastrad */ 3188 1.5 riastrad void amdgpu_vm_manager_init(struct amdgpu_device *adev) 3189 1.5 riastrad { 3190 1.5 riastrad unsigned i; 3191 1.5 riastrad 3192 1.5 riastrad amdgpu_vmid_mgr_init(adev); 3193 1.5 riastrad 3194 1.5 riastrad adev->vm_manager.fence_context = 3195 1.5 riastrad dma_fence_context_alloc(AMDGPU_MAX_RINGS); 3196 1.5 riastrad for (i = 0; i < AMDGPU_MAX_RINGS; ++i) 3197 1.5 riastrad adev->vm_manager.seqno[i] = 0; 3198 1.5 riastrad 3199 1.5 riastrad spin_lock_init(&adev->vm_manager.prt_lock); 3200 1.5 riastrad atomic_set(&adev->vm_manager.num_prt_users, 0); 3201 1.5 riastrad 3202 1.5 riastrad /* If not overridden by the user, by default, only in large BAR systems 3203 1.5 riastrad * Compute VM tables will be updated by CPU 3204 1.5 riastrad */ 3205 1.5 riastrad #ifdef CONFIG_X86_64 3206 1.5 riastrad if (amdgpu_vm_update_mode == -1) { 3207 1.5 riastrad if (amdgpu_gmc_vram_full_visible(&adev->gmc)) 3208 1.5 riastrad adev->vm_manager.vm_update_mode = 3209 1.5 riastrad AMDGPU_VM_USE_CPU_FOR_COMPUTE; 3210 1.5 riastrad else 3211 1.5 riastrad adev->vm_manager.vm_update_mode = 0; 3212 1.5 riastrad } else 3213 1.5 riastrad adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; 3214 1.5 riastrad #else 3215 1.5 riastrad adev->vm_manager.vm_update_mode = 0; 3216 1.5 riastrad #endif 3217 1.5 riastrad 3218 1.5 riastrad idr_init(&adev->vm_manager.pasid_idr); 3219 1.5 riastrad spin_lock_init(&adev->vm_manager.pasid_lock); 3220 1.1 riastrad 3221 1.5 riastrad adev->vm_manager.xgmi_map_counter = 0; 3222 1.5 riastrad mutex_init(&adev->vm_manager.lock_pstate); 3223 1.1 riastrad } 3224 1.1 riastrad 3225 1.1 riastrad /** 3226 1.1 riastrad * amdgpu_vm_manager_fini - cleanup VM manager 3227 1.1 riastrad * 3228 1.1 riastrad * @adev: amdgpu_device pointer 3229 1.1 riastrad * 3230 1.1 riastrad * Cleanup the VM manager and free resources. 3231 1.1 riastrad */ 3232 1.1 riastrad void amdgpu_vm_manager_fini(struct amdgpu_device *adev) 3233 1.1 riastrad { 3234 1.10 riastrad mutex_destroy(&adev->vm_manager.lock_pstate); 3235 1.10 riastrad spin_lock_destroy(&adev->vm_manager.pasid_lock); 3236 1.5 riastrad WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr)); 3237 1.5 riastrad idr_destroy(&adev->vm_manager.pasid_idr); 3238 1.5 riastrad 3239 1.5 riastrad amdgpu_vmid_mgr_fini(adev); 3240 1.5 riastrad } 3241 1.5 riastrad 3242 1.5 riastrad /** 3243 1.5 riastrad * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs. 3244 1.5 riastrad * 3245 1.5 riastrad * @dev: drm device pointer 3246 1.5 riastrad * @data: drm_amdgpu_vm 3247 1.5 riastrad * @filp: drm file pointer 3248 1.5 riastrad * 3249 1.5 riastrad * Returns: 3250 1.5 riastrad * 0 for success, -errno for errors. 3251 1.5 riastrad */ 3252 1.5 riastrad int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 3253 1.5 riastrad { 3254 1.5 riastrad union drm_amdgpu_vm *args = data; 3255 1.5 riastrad struct amdgpu_device *adev = dev->dev_private; 3256 1.5 riastrad struct amdgpu_fpriv *fpriv = filp->driver_priv; 3257 1.5 riastrad int r; 3258 1.5 riastrad 3259 1.5 riastrad switch (args->in.op) { 3260 1.5 riastrad case AMDGPU_VM_OP_RESERVE_VMID: 3261 1.5 riastrad /* We only have requirement to reserve vmid from gfxhub */ 3262 1.5 riastrad r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, 3263 1.5 riastrad AMDGPU_GFXHUB_0); 3264 1.5 riastrad if (r) 3265 1.5 riastrad return r; 3266 1.5 riastrad break; 3267 1.5 riastrad case AMDGPU_VM_OP_UNRESERVE_VMID: 3268 1.5 riastrad amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0); 3269 1.5 riastrad break; 3270 1.5 riastrad default: 3271 1.5 riastrad return -EINVAL; 3272 1.5 riastrad } 3273 1.5 riastrad 3274 1.5 riastrad return 0; 3275 1.5 riastrad } 3276 1.5 riastrad 3277 1.5 riastrad /** 3278 1.5 riastrad * amdgpu_vm_get_task_info - Extracts task info for a PASID. 3279 1.5 riastrad * 3280 1.5 riastrad * @adev: drm device pointer 3281 1.5 riastrad * @pasid: PASID identifier for VM 3282 1.5 riastrad * @task_info: task_info to fill. 3283 1.5 riastrad */ 3284 1.5 riastrad void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid, 3285 1.5 riastrad struct amdgpu_task_info *task_info) 3286 1.5 riastrad { 3287 1.5 riastrad struct amdgpu_vm *vm; 3288 1.5 riastrad unsigned long flags; 3289 1.5 riastrad 3290 1.5 riastrad spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3291 1.5 riastrad 3292 1.5 riastrad vm = idr_find(&adev->vm_manager.pasid_idr, pasid); 3293 1.5 riastrad if (vm) 3294 1.5 riastrad *task_info = vm->task_info; 3295 1.5 riastrad 3296 1.5 riastrad spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3297 1.5 riastrad } 3298 1.5 riastrad 3299 1.5 riastrad /** 3300 1.5 riastrad * amdgpu_vm_set_task_info - Sets VMs task info. 3301 1.5 riastrad * 3302 1.5 riastrad * @vm: vm for which to set the info 3303 1.5 riastrad */ 3304 1.5 riastrad void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) 3305 1.5 riastrad { 3306 1.5 riastrad if (vm->task_info.pid) 3307 1.5 riastrad return; 3308 1.5 riastrad 3309 1.12 riastrad #ifdef __NetBSD__ 3310 1.12 riastrad vm->task_info.pid = curlwp->l_proc->p_pid; 3311 1.12 riastrad vm->task_info.tgid = curlwp->l_lid; 3312 1.12 riastrad strlcpy(vm->task_info.process_name, curlwp->l_proc->p_comm, 3313 1.12 riastrad sizeof vm->task_info.process_name); 3314 1.12 riastrad if (curlwp->l_name) 3315 1.12 riastrad strlcpy(vm->task_info.task_name, curlwp->l_name, 3316 1.12 riastrad sizeof vm->task_info.task_name); 3317 1.12 riastrad #else 3318 1.5 riastrad vm->task_info.pid = current->pid; 3319 1.5 riastrad get_task_comm(vm->task_info.task_name, current); 3320 1.5 riastrad 3321 1.5 riastrad if (current->group_leader->mm != current->mm) 3322 1.5 riastrad return; 3323 1.5 riastrad 3324 1.5 riastrad vm->task_info.tgid = current->group_leader->pid; 3325 1.5 riastrad get_task_comm(vm->task_info.process_name, current->group_leader); 3326 1.6 riastrad #endif 3327 1.5 riastrad } 3328 1.5 riastrad 3329 1.5 riastrad /** 3330 1.5 riastrad * amdgpu_vm_handle_fault - graceful handling of VM faults. 3331 1.5 riastrad * @adev: amdgpu device pointer 3332 1.5 riastrad * @pasid: PASID of the VM 3333 1.5 riastrad * @addr: Address of the fault 3334 1.5 riastrad * 3335 1.5 riastrad * Try to gracefully handle a VM fault. Return true if the fault was handled and 3336 1.5 riastrad * shouldn't be reported any more. 3337 1.5 riastrad */ 3338 1.5 riastrad bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, unsigned int pasid, 3339 1.5 riastrad uint64_t addr) 3340 1.5 riastrad { 3341 1.5 riastrad struct amdgpu_bo *root; 3342 1.5 riastrad uint64_t value, flags; 3343 1.5 riastrad struct amdgpu_vm *vm; 3344 1.5 riastrad long r; 3345 1.5 riastrad 3346 1.5 riastrad spin_lock(&adev->vm_manager.pasid_lock); 3347 1.5 riastrad vm = idr_find(&adev->vm_manager.pasid_idr, pasid); 3348 1.5 riastrad if (vm) 3349 1.5 riastrad root = amdgpu_bo_ref(vm->root.base.bo); 3350 1.5 riastrad else 3351 1.5 riastrad root = NULL; 3352 1.5 riastrad spin_unlock(&adev->vm_manager.pasid_lock); 3353 1.5 riastrad 3354 1.5 riastrad if (!root) 3355 1.5 riastrad return false; 3356 1.5 riastrad 3357 1.5 riastrad r = amdgpu_bo_reserve(root, true); 3358 1.5 riastrad if (r) 3359 1.5 riastrad goto error_unref; 3360 1.5 riastrad 3361 1.5 riastrad /* Double check that the VM still exists */ 3362 1.5 riastrad spin_lock(&adev->vm_manager.pasid_lock); 3363 1.5 riastrad vm = idr_find(&adev->vm_manager.pasid_idr, pasid); 3364 1.5 riastrad if (vm && vm->root.base.bo != root) 3365 1.5 riastrad vm = NULL; 3366 1.5 riastrad spin_unlock(&adev->vm_manager.pasid_lock); 3367 1.5 riastrad if (!vm) 3368 1.5 riastrad goto error_unlock; 3369 1.5 riastrad 3370 1.5 riastrad addr /= AMDGPU_GPU_PAGE_SIZE; 3371 1.5 riastrad flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED | 3372 1.5 riastrad AMDGPU_PTE_SYSTEM; 3373 1.5 riastrad 3374 1.5 riastrad if (vm->is_compute_context) { 3375 1.5 riastrad /* Intentionally setting invalid PTE flag 3376 1.5 riastrad * combination to force a no-retry-fault 3377 1.5 riastrad */ 3378 1.5 riastrad flags = AMDGPU_PTE_EXECUTABLE | AMDGPU_PDE_PTE | 3379 1.5 riastrad AMDGPU_PTE_TF; 3380 1.5 riastrad value = 0; 3381 1.5 riastrad 3382 1.5 riastrad } else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) { 3383 1.5 riastrad /* Redirect the access to the dummy page */ 3384 1.5 riastrad value = adev->dummy_page_addr; 3385 1.5 riastrad flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE | 3386 1.5 riastrad AMDGPU_PTE_WRITEABLE; 3387 1.5 riastrad 3388 1.5 riastrad } else { 3389 1.5 riastrad /* Let the hw retry silently on the PTE */ 3390 1.5 riastrad value = 0; 3391 1.5 riastrad } 3392 1.5 riastrad 3393 1.5 riastrad r = amdgpu_vm_bo_update_mapping(adev, vm, true, NULL, addr, addr + 1, 3394 1.5 riastrad flags, value, NULL, NULL); 3395 1.5 riastrad if (r) 3396 1.5 riastrad goto error_unlock; 3397 1.5 riastrad 3398 1.5 riastrad r = amdgpu_vm_update_pdes(adev, vm, true); 3399 1.5 riastrad 3400 1.5 riastrad error_unlock: 3401 1.5 riastrad amdgpu_bo_unreserve(root); 3402 1.5 riastrad if (r < 0) 3403 1.5 riastrad DRM_ERROR("Can't handle page fault (%ld)\n", r); 3404 1.5 riastrad 3405 1.5 riastrad error_unref: 3406 1.5 riastrad amdgpu_bo_unref(&root); 3407 1.1 riastrad 3408 1.5 riastrad return false; 3409 1.1 riastrad } 3410