Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Advanced Micro Devices, Inc. |
| 3 | * Copyright 2008 Red Hat Inc. |
| 4 | * Copyright 2009 Jerome Glisse. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: Dave Airlie |
| 25 | * Alex Deucher |
| 26 | * Jerome Glisse |
| 27 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 28 | #include <linux/dma-fence-array.h> |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 29 | #include <linux/interval_tree_generic.h> |
Felix Kuehling | 0220844 | 2017-08-25 20:40:26 -0400 | [diff] [blame] | 30 | #include <linux/idr.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 31 | #include <drm/drmP.h> |
| 32 | #include <drm/amdgpu_drm.h> |
| 33 | #include "amdgpu.h" |
| 34 | #include "amdgpu_trace.h" |
| 35 | |
| 36 | /* |
| 37 | * GPUVM |
| 38 | * GPUVM is similar to the legacy gart on older asics, however |
| 39 | * rather than there being a single global gart table |
| 40 | * for the entire GPU, there are multiple VM page tables active |
| 41 | * at any given time. The VM page tables can contain a mix |
| 42 | * vram pages and system memory pages and system memory pages |
| 43 | * can be mapped as snooped (cached system pages) or unsnooped |
| 44 | * (uncached system pages). |
| 45 | * Each VM has an ID associated with it and there is a page table |
| 46 | * associated with each VMID. When execting a command buffer, |
| 47 | * the kernel tells the the ring what VMID to use for that command |
| 48 | * buffer. VMIDs are allocated dynamically as commands are submitted. |
| 49 | * The userspace drivers maintain their own address space and the kernel |
| 50 | * sets up their pages tables accordingly when they submit their |
| 51 | * command buffers and a VMID is assigned. |
| 52 | * Cayman/Trinity support up to 8 active VMs at any given time; |
| 53 | * SI supports 16. |
| 54 | */ |
| 55 | |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 56 | #define START(node) ((node)->start) |
| 57 | #define LAST(node) ((node)->last) |
| 58 | |
| 59 | INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last, |
| 60 | START, LAST, static, amdgpu_vm_it) |
| 61 | |
| 62 | #undef START |
| 63 | #undef LAST |
| 64 | |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 65 | /* Local structure. Encapsulate some VM table update parameters to reduce |
| 66 | * the number of function parameters |
| 67 | */ |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 68 | struct amdgpu_pte_update_params { |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 69 | /* amdgpu device we do this update for */ |
| 70 | struct amdgpu_device *adev; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 71 | /* optional amdgpu_vm we do this update for */ |
| 72 | struct amdgpu_vm *vm; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 73 | /* address where to copy page table entries from */ |
| 74 | uint64_t src; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 75 | /* indirect buffer to fill with commands */ |
| 76 | struct amdgpu_ib *ib; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 77 | /* Function which actually does the update */ |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 78 | void (*func)(struct amdgpu_pte_update_params *params, |
| 79 | struct amdgpu_bo *bo, uint64_t pe, |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 80 | uint64_t addr, unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 81 | uint64_t flags); |
Harish Kasiviswanathan | b4d4251 | 2017-05-11 19:47:22 -0400 | [diff] [blame] | 82 | /* The next two are used during VM update by CPU |
| 83 | * DMA addresses to use for mapping |
| 84 | * Kernel pointer of PD/PT BO that needs to be updated |
| 85 | */ |
| 86 | dma_addr_t *pages_addr; |
| 87 | void *kptr; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 90 | /* Helper to disable partial resident texture feature from a fence callback */ |
| 91 | struct amdgpu_prt_cb { |
| 92 | struct amdgpu_device *adev; |
| 93 | struct dma_fence_cb cb; |
| 94 | }; |
| 95 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 96 | /** |
Christian König | 5078314 | 2017-11-27 14:01:51 +0100 | [diff] [blame] | 97 | * amdgpu_vm_level_shift - return the addr shift for each level |
| 98 | * |
| 99 | * @adev: amdgpu_device pointer |
| 100 | * |
| 101 | * Returns the number of bits the pfn needs to be right shifted for a level. |
| 102 | */ |
| 103 | static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev, |
| 104 | unsigned level) |
| 105 | { |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 106 | unsigned shift = 0xff; |
| 107 | |
| 108 | switch (level) { |
| 109 | case AMDGPU_VM_PDB2: |
| 110 | case AMDGPU_VM_PDB1: |
| 111 | case AMDGPU_VM_PDB0: |
| 112 | shift = 9 * (AMDGPU_VM_PDB0 - level) + |
Christian König | 5078314 | 2017-11-27 14:01:51 +0100 | [diff] [blame] | 113 | adev->vm_manager.block_size; |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 114 | break; |
| 115 | case AMDGPU_VM_PTB: |
| 116 | shift = 0; |
| 117 | break; |
| 118 | default: |
| 119 | dev_err(adev->dev, "the level%d isn't supported.\n", level); |
| 120 | } |
| 121 | |
| 122 | return shift; |
Christian König | 5078314 | 2017-11-27 14:01:51 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 126 | * amdgpu_vm_num_entries - return the number of entries in a PD/PT |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 127 | * |
| 128 | * @adev: amdgpu_device pointer |
| 129 | * |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 130 | * Calculate the number of entries in a page directory or page table. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 131 | */ |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 132 | static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev, |
| 133 | unsigned level) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 134 | { |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 135 | unsigned shift = amdgpu_vm_level_shift(adev, |
| 136 | adev->vm_manager.root_level); |
Christian König | 0410c5e | 2017-11-20 14:29:01 +0100 | [diff] [blame] | 137 | |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 138 | if (level == adev->vm_manager.root_level) |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 139 | /* For the root directory */ |
Christian König | 0410c5e | 2017-11-20 14:29:01 +0100 | [diff] [blame] | 140 | return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift; |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 141 | else if (level != AMDGPU_VM_PTB) |
Christian König | 0410c5e | 2017-11-20 14:29:01 +0100 | [diff] [blame] | 142 | /* Everything in between */ |
| 143 | return 512; |
| 144 | else |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 145 | /* For the page tables on the leaves */ |
Zhang, Jerry | 36b32a6 | 2017-03-29 16:08:32 +0800 | [diff] [blame] | 146 | return AMDGPU_VM_PTE_COUNT(adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 150 | * amdgpu_vm_bo_size - returns the size of the BOs in bytes |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 151 | * |
| 152 | * @adev: amdgpu_device pointer |
| 153 | * |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 154 | * Calculate the size of the BO for a page directory or page table in bytes. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 155 | */ |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 156 | static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 157 | { |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame] | 158 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 162 | * amdgpu_vm_get_pd_bo - add the VM PD to a validation list |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 163 | * |
| 164 | * @vm: vm providing the BOs |
Christian König | 3c0eea6 | 2015-12-11 14:39:05 +0100 | [diff] [blame] | 165 | * @validated: head of validation list |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 166 | * @entry: entry to add |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 167 | * |
| 168 | * Add the page directory to the list of BOs to |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 169 | * validate for command submission. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 170 | */ |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 171 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
| 172 | struct list_head *validated, |
| 173 | struct amdgpu_bo_list_entry *entry) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 174 | { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 175 | entry->robj = vm->root.base.bo; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 176 | entry->priority = 0; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 177 | entry->tv.bo = &entry->robj->tbo; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 178 | entry->tv.shared = true; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 179 | entry->user_pages = NULL; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 180 | list_add(&entry->tv.head, validated); |
| 181 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 182 | |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 183 | /** |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 184 | * amdgpu_vm_validate_pt_bos - validate the page table BOs |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 185 | * |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 186 | * @adev: amdgpu device pointer |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 187 | * @vm: vm providing the BOs |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 188 | * @validate: callback to do the validation |
| 189 | * @param: parameter for the validation callback |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 190 | * |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 191 | * Validate the page table BOs on command submission if neccessary. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 192 | */ |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 193 | int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
| 194 | int (*validate)(void *p, struct amdgpu_bo *bo), |
| 195 | void *param) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 196 | { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 197 | struct ttm_bo_global *glob = adev->mman.bdev.glob; |
| 198 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 199 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 200 | spin_lock(&vm->status_lock); |
| 201 | while (!list_empty(&vm->evicted)) { |
| 202 | struct amdgpu_vm_bo_base *bo_base; |
| 203 | struct amdgpu_bo *bo; |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 204 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 205 | bo_base = list_first_entry(&vm->evicted, |
| 206 | struct amdgpu_vm_bo_base, |
| 207 | vm_status); |
| 208 | spin_unlock(&vm->status_lock); |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 209 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 210 | bo = bo_base->bo; |
| 211 | BUG_ON(!bo); |
| 212 | if (bo->parent) { |
| 213 | r = validate(param, bo); |
| 214 | if (r) |
| 215 | return r; |
Christian König | 34d7be5 | 2017-08-24 12:32:55 +0200 | [diff] [blame] | 216 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 217 | spin_lock(&glob->lru_lock); |
| 218 | ttm_bo_move_to_lru_tail(&bo->tbo); |
| 219 | if (bo->shadow) |
| 220 | ttm_bo_move_to_lru_tail(&bo->shadow->tbo); |
| 221 | spin_unlock(&glob->lru_lock); |
| 222 | } |
| 223 | |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 224 | if (bo->tbo.type == ttm_bo_type_kernel && |
| 225 | vm->use_cpu_for_update) { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 226 | r = amdgpu_bo_kmap(bo, NULL); |
| 227 | if (r) |
| 228 | return r; |
| 229 | } |
| 230 | |
| 231 | spin_lock(&vm->status_lock); |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 232 | if (bo->tbo.type != ttm_bo_type_kernel) |
| 233 | list_move(&bo_base->vm_status, &vm->moved); |
| 234 | else |
| 235 | list_move(&bo_base->vm_status, &vm->relocated); |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 236 | } |
| 237 | spin_unlock(&vm->status_lock); |
Christian König | 34d7be5 | 2017-08-24 12:32:55 +0200 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * amdgpu_vm_ready - check VM is ready for updates |
| 244 | * |
Christian König | 34d7be5 | 2017-08-24 12:32:55 +0200 | [diff] [blame] | 245 | * @vm: VM to check |
| 246 | * |
| 247 | * Check if all VM PDs/PTs are ready for updates |
| 248 | */ |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 249 | bool amdgpu_vm_ready(struct amdgpu_vm *vm) |
Christian König | 34d7be5 | 2017-08-24 12:32:55 +0200 | [diff] [blame] | 250 | { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 251 | bool ready; |
Christian König | 34d7be5 | 2017-08-24 12:32:55 +0200 | [diff] [blame] | 252 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 253 | spin_lock(&vm->status_lock); |
| 254 | ready = list_empty(&vm->evicted); |
| 255 | spin_unlock(&vm->status_lock); |
| 256 | |
| 257 | return ready; |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 261 | * amdgpu_vm_clear_bo - initially clear the PDs/PTs |
| 262 | * |
| 263 | * @adev: amdgpu_device pointer |
| 264 | * @bo: BO to clear |
| 265 | * @level: level this BO is at |
| 266 | * |
| 267 | * Root PD needs to be reserved when calling this. |
| 268 | */ |
| 269 | static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 270 | struct amdgpu_vm *vm, struct amdgpu_bo *bo, |
| 271 | unsigned level, bool pte_support_ats) |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 272 | { |
| 273 | struct ttm_operation_ctx ctx = { true, false }; |
| 274 | struct dma_fence *fence = NULL; |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 275 | unsigned entries, ats_entries; |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 276 | struct amdgpu_ring *ring; |
| 277 | struct amdgpu_job *job; |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 278 | uint64_t addr; |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 279 | int r; |
| 280 | |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 281 | addr = amdgpu_bo_gpu_offset(bo); |
| 282 | entries = amdgpu_bo_size(bo) / 8; |
| 283 | |
| 284 | if (pte_support_ats) { |
| 285 | if (level == adev->vm_manager.root_level) { |
| 286 | ats_entries = amdgpu_vm_level_shift(adev, level); |
| 287 | ats_entries += AMDGPU_GPU_PAGE_SHIFT; |
| 288 | ats_entries = AMDGPU_VA_HOLE_START >> ats_entries; |
| 289 | ats_entries = min(ats_entries, entries); |
| 290 | entries -= ats_entries; |
| 291 | } else { |
| 292 | ats_entries = entries; |
| 293 | entries = 0; |
| 294 | } |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 295 | } else { |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 296 | ats_entries = 0; |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
| 300 | |
| 301 | r = reservation_object_reserve_shared(bo->tbo.resv); |
| 302 | if (r) |
| 303 | return r; |
| 304 | |
| 305 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); |
| 306 | if (r) |
| 307 | goto error; |
| 308 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 309 | r = amdgpu_job_alloc_with_ib(adev, 64, &job); |
| 310 | if (r) |
| 311 | goto error; |
| 312 | |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 313 | if (ats_entries) { |
| 314 | uint64_t ats_value; |
| 315 | |
| 316 | ats_value = AMDGPU_PTE_DEFAULT_ATC; |
| 317 | if (level != AMDGPU_VM_PTB) |
| 318 | ats_value |= AMDGPU_PDE_PTE; |
| 319 | |
| 320 | amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0, |
| 321 | ats_entries, 0, ats_value); |
| 322 | addr += ats_entries * 8; |
| 323 | } |
| 324 | |
| 325 | if (entries) |
| 326 | amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0, |
| 327 | entries, 0, 0); |
| 328 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 329 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
| 330 | |
| 331 | WARN_ON(job->ibs[0].length_dw > 64); |
Christian König | 29e8357 | 2018-02-04 19:36:52 +0100 | [diff] [blame] | 332 | r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv, |
| 333 | AMDGPU_FENCE_OWNER_UNDEFINED, false); |
| 334 | if (r) |
| 335 | goto error_free; |
| 336 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 337 | r = amdgpu_job_submit(job, ring, &vm->entity, |
| 338 | AMDGPU_FENCE_OWNER_UNDEFINED, &fence); |
| 339 | if (r) |
| 340 | goto error_free; |
| 341 | |
| 342 | amdgpu_bo_fence(bo, fence, true); |
| 343 | dma_fence_put(fence); |
Christian König | e61736d | 2018-02-02 21:05:40 +0100 | [diff] [blame] | 344 | |
| 345 | if (bo->shadow) |
| 346 | return amdgpu_vm_clear_bo(adev, vm, bo->shadow, |
| 347 | level, pte_support_ats); |
| 348 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 349 | return 0; |
| 350 | |
| 351 | error_free: |
| 352 | amdgpu_job_free(job); |
| 353 | |
| 354 | error: |
| 355 | return r; |
| 356 | } |
| 357 | |
| 358 | /** |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 359 | * amdgpu_vm_alloc_levels - allocate the PD/PT levels |
| 360 | * |
| 361 | * @adev: amdgpu_device pointer |
| 362 | * @vm: requested vm |
| 363 | * @saddr: start of the address range |
| 364 | * @eaddr: end of the address range |
| 365 | * |
| 366 | * Make sure the page directories and page tables are allocated |
| 367 | */ |
| 368 | static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev, |
| 369 | struct amdgpu_vm *vm, |
| 370 | struct amdgpu_vm_pt *parent, |
| 371 | uint64_t saddr, uint64_t eaddr, |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 372 | unsigned level, bool ats) |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 373 | { |
Christian König | 5078314 | 2017-11-27 14:01:51 +0100 | [diff] [blame] | 374 | unsigned shift = amdgpu_vm_level_shift(adev, level); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 375 | unsigned pt_idx, from, to; |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 376 | u64 flags; |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 377 | int r; |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 378 | |
| 379 | if (!parent->entries) { |
| 380 | unsigned num_entries = amdgpu_vm_num_entries(adev, level); |
| 381 | |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 382 | parent->entries = kvmalloc_array(num_entries, |
| 383 | sizeof(struct amdgpu_vm_pt), |
| 384 | GFP_KERNEL | __GFP_ZERO); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 385 | if (!parent->entries) |
| 386 | return -ENOMEM; |
| 387 | memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt)); |
| 388 | } |
| 389 | |
Felix Kuehling | 1866bac | 2017-03-28 20:36:12 -0400 | [diff] [blame] | 390 | from = saddr >> shift; |
| 391 | to = eaddr >> shift; |
| 392 | if (from >= amdgpu_vm_num_entries(adev, level) || |
| 393 | to >= amdgpu_vm_num_entries(adev, level)) |
| 394 | return -EINVAL; |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 395 | |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 396 | ++level; |
Felix Kuehling | 1866bac | 2017-03-28 20:36:12 -0400 | [diff] [blame] | 397 | saddr = saddr & ((1 << shift) - 1); |
| 398 | eaddr = eaddr & ((1 << shift) - 1); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 399 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 400 | flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 401 | if (vm->use_cpu_for_update) |
| 402 | flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; |
| 403 | else |
| 404 | flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
| 405 | AMDGPU_GEM_CREATE_SHADOW); |
| 406 | |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 407 | /* walk over the address space and allocate the page tables */ |
| 408 | for (pt_idx = from; pt_idx <= to; ++pt_idx) { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 409 | struct reservation_object *resv = vm->root.base.bo->tbo.resv; |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 410 | struct amdgpu_vm_pt *entry = &parent->entries[pt_idx]; |
| 411 | struct amdgpu_bo *pt; |
| 412 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 413 | if (!entry->base.bo) { |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 414 | r = amdgpu_bo_create(adev, |
| 415 | amdgpu_vm_bo_size(adev, level), |
Christian König | eab3de2 | 2018-03-14 14:48:17 -0500 | [diff] [blame] | 416 | AMDGPU_GPU_PAGE_SIZE, |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 417 | AMDGPU_GEM_DOMAIN_VRAM, flags, |
Christian König | eab3de2 | 2018-03-14 14:48:17 -0500 | [diff] [blame] | 418 | ttm_bo_type_kernel, resv, &pt); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 419 | if (r) |
| 420 | return r; |
| 421 | |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 422 | r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats); |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 423 | if (r) { |
Christian König | e5197a4 | 2018-02-02 21:00:44 +0100 | [diff] [blame] | 424 | amdgpu_bo_unref(&pt->shadow); |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 425 | amdgpu_bo_unref(&pt); |
| 426 | return r; |
| 427 | } |
| 428 | |
Christian König | 0a096fb | 2017-07-12 10:01:48 +0200 | [diff] [blame] | 429 | if (vm->use_cpu_for_update) { |
| 430 | r = amdgpu_bo_kmap(pt, NULL); |
| 431 | if (r) { |
Christian König | e5197a4 | 2018-02-02 21:00:44 +0100 | [diff] [blame] | 432 | amdgpu_bo_unref(&pt->shadow); |
Christian König | 0a096fb | 2017-07-12 10:01:48 +0200 | [diff] [blame] | 433 | amdgpu_bo_unref(&pt); |
| 434 | return r; |
| 435 | } |
| 436 | } |
| 437 | |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 438 | /* Keep a reference to the root directory to avoid |
| 439 | * freeing them up in the wrong order. |
| 440 | */ |
Christian König | 0f2fc43 | 2017-08-31 10:46:20 +0200 | [diff] [blame] | 441 | pt->parent = amdgpu_bo_ref(parent->base.bo); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 442 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 443 | entry->base.vm = vm; |
| 444 | entry->base.bo = pt; |
| 445 | list_add_tail(&entry->base.bo_list, &pt->va); |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 446 | spin_lock(&vm->status_lock); |
| 447 | list_add(&entry->base.vm_status, &vm->relocated); |
| 448 | spin_unlock(&vm->status_lock); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 451 | if (level < AMDGPU_VM_PTB) { |
Felix Kuehling | 1866bac | 2017-03-28 20:36:12 -0400 | [diff] [blame] | 452 | uint64_t sub_saddr = (pt_idx == from) ? saddr : 0; |
| 453 | uint64_t sub_eaddr = (pt_idx == to) ? eaddr : |
| 454 | ((1 << shift) - 1); |
| 455 | r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr, |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 456 | sub_eaddr, level, ats); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 457 | if (r) |
| 458 | return r; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 465 | /** |
| 466 | * amdgpu_vm_alloc_pts - Allocate page tables. |
| 467 | * |
| 468 | * @adev: amdgpu_device pointer |
| 469 | * @vm: VM to allocate page tables for |
| 470 | * @saddr: Start address which needs to be allocated |
| 471 | * @size: Size from start address we need. |
| 472 | * |
| 473 | * Make sure the page tables are allocated. |
| 474 | */ |
| 475 | int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, |
| 476 | struct amdgpu_vm *vm, |
| 477 | uint64_t saddr, uint64_t size) |
| 478 | { |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 479 | uint64_t eaddr; |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 480 | bool ats = false; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 481 | |
| 482 | /* validate the parameters */ |
| 483 | if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | eaddr = saddr + size - 1; |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 487 | |
| 488 | if (vm->pte_support_ats) |
| 489 | ats = saddr < AMDGPU_VA_HOLE_START; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 490 | |
| 491 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 492 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 493 | |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 494 | if (eaddr >= adev->vm_manager.max_pfn) { |
| 495 | dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n", |
| 496 | eaddr, adev->vm_manager.max_pfn); |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 500 | return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 501 | adev->vm_manager.root_level, ats); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Christian König | 641e940 | 2017-04-03 13:59:25 +0200 | [diff] [blame] | 504 | /** |
Alex Xie | e59c020 | 2017-06-01 09:42:59 -0400 | [diff] [blame] | 505 | * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug |
| 506 | * |
| 507 | * @adev: amdgpu_device pointer |
| 508 | */ |
| 509 | void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev) |
| 510 | { |
| 511 | const struct amdgpu_ip_block *ip_block; |
| 512 | bool has_compute_vm_bug; |
| 513 | struct amdgpu_ring *ring; |
| 514 | int i; |
| 515 | |
| 516 | has_compute_vm_bug = false; |
| 517 | |
Alex Deucher | 2990a1f | 2017-12-15 16:18:00 -0500 | [diff] [blame] | 518 | ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); |
Alex Xie | e59c020 | 2017-06-01 09:42:59 -0400 | [diff] [blame] | 519 | if (ip_block) { |
| 520 | /* Compute has a VM bug for GFX version < 7. |
| 521 | Compute has a VM bug for GFX 8 MEC firmware version < 673.*/ |
| 522 | if (ip_block->version->major <= 7) |
| 523 | has_compute_vm_bug = true; |
| 524 | else if (ip_block->version->major == 8) |
| 525 | if (adev->gfx.mec_fw_version < 673) |
| 526 | has_compute_vm_bug = true; |
| 527 | } |
| 528 | |
| 529 | for (i = 0; i < adev->num_rings; i++) { |
| 530 | ring = adev->rings[i]; |
| 531 | if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) |
| 532 | /* only compute rings */ |
| 533 | ring->has_compute_vm_bug = has_compute_vm_bug; |
| 534 | else |
| 535 | ring->has_compute_vm_bug = false; |
| 536 | } |
| 537 | } |
| 538 | |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 539 | bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring, |
| 540 | struct amdgpu_job *job) |
| 541 | { |
| 542 | struct amdgpu_device *adev = ring->adev; |
| 543 | unsigned vmhub = ring->funcs->vmhub; |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 544 | struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
| 545 | struct amdgpu_vmid *id; |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 546 | bool gds_switch_needed; |
Alex Xie | e59c020 | 2017-06-01 09:42:59 -0400 | [diff] [blame] | 547 | bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug; |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 548 | |
Christian König | c4f46f2 | 2017-12-18 17:08:25 +0100 | [diff] [blame] | 549 | if (job->vmid == 0) |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 550 | return false; |
Christian König | c4f46f2 | 2017-12-18 17:08:25 +0100 | [diff] [blame] | 551 | id = &id_mgr->ids[job->vmid]; |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 552 | gds_switch_needed = ring->funcs->emit_gds_switch && ( |
| 553 | id->gds_base != job->gds_base || |
| 554 | id->gds_size != job->gds_size || |
| 555 | id->gws_base != job->gws_base || |
| 556 | id->gws_size != job->gws_size || |
| 557 | id->oa_base != job->oa_base || |
| 558 | id->oa_size != job->oa_size); |
| 559 | |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 560 | if (amdgpu_vmid_had_gpu_reset(adev, id)) |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 561 | return true; |
Alex Xie | bb37b67 | 2017-05-30 23:50:10 -0400 | [diff] [blame] | 562 | |
| 563 | return vm_flush_needed || gds_switch_needed; |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 564 | } |
| 565 | |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 566 | static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev) |
| 567 | { |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame] | 568 | return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size); |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 571 | /** |
| 572 | * amdgpu_vm_flush - hardware flush the vm |
| 573 | * |
| 574 | * @ring: ring to use for flush |
Christian König | c4f46f2 | 2017-12-18 17:08:25 +0100 | [diff] [blame] | 575 | * @vmid: vmid number to use |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 576 | * @pd_addr: address of the page directory |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 577 | * |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 578 | * Emit a VM flush when it is necessary. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 579 | */ |
Monk Liu | 8fdf074 | 2017-06-06 17:25:13 +0800 | [diff] [blame] | 580 | int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 581 | { |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 582 | struct amdgpu_device *adev = ring->adev; |
Christian König | 7645670 | 2017-04-06 17:52:39 +0200 | [diff] [blame] | 583 | unsigned vmhub = ring->funcs->vmhub; |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 584 | struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
Christian König | c4f46f2 | 2017-12-18 17:08:25 +0100 | [diff] [blame] | 585 | struct amdgpu_vmid *id = &id_mgr->ids[job->vmid]; |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 586 | bool gds_switch_needed = ring->funcs->emit_gds_switch && ( |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 587 | id->gds_base != job->gds_base || |
| 588 | id->gds_size != job->gds_size || |
| 589 | id->gws_base != job->gws_base || |
| 590 | id->gws_size != job->gws_size || |
| 591 | id->oa_base != job->oa_base || |
| 592 | id->oa_size != job->oa_size); |
Flora Cui | de37e68 | 2017-05-18 13:56:22 +0800 | [diff] [blame] | 593 | bool vm_flush_needed = job->vm_needs_flush; |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 594 | bool pasid_mapping_needed = id->pasid != job->pasid || |
| 595 | !id->pasid_mapping || |
| 596 | !dma_fence_is_signaled(id->pasid_mapping); |
| 597 | struct dma_fence *fence = NULL; |
Christian König | c0e5193 | 2017-04-03 14:16:07 +0200 | [diff] [blame] | 598 | unsigned patch_offset = 0; |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 599 | int r; |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 600 | |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 601 | if (amdgpu_vmid_had_gpu_reset(adev, id)) { |
Christian König | f7d015b | 2017-04-03 14:28:26 +0200 | [diff] [blame] | 602 | gds_switch_needed = true; |
| 603 | vm_flush_needed = true; |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 604 | pasid_mapping_needed = true; |
Christian König | f7d015b | 2017-04-03 14:28:26 +0200 | [diff] [blame] | 605 | } |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 606 | |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 607 | gds_switch_needed &= !!ring->funcs->emit_gds_switch; |
| 608 | vm_flush_needed &= !!ring->funcs->emit_vm_flush; |
| 609 | pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping && |
| 610 | ring->funcs->emit_wreg; |
| 611 | |
Monk Liu | 8fdf074 | 2017-06-06 17:25:13 +0800 | [diff] [blame] | 612 | if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync) |
Christian König | f7d015b | 2017-04-03 14:28:26 +0200 | [diff] [blame] | 613 | return 0; |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 614 | |
Christian König | c0e5193 | 2017-04-03 14:16:07 +0200 | [diff] [blame] | 615 | if (ring->funcs->init_cond_exec) |
| 616 | patch_offset = amdgpu_ring_init_cond_exec(ring); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 617 | |
Monk Liu | 8fdf074 | 2017-06-06 17:25:13 +0800 | [diff] [blame] | 618 | if (need_pipe_sync) |
| 619 | amdgpu_ring_emit_pipeline_sync(ring); |
| 620 | |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 621 | if (vm_flush_needed) { |
Christian König | c4f46f2 | 2017-12-18 17:08:25 +0100 | [diff] [blame] | 622 | trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr); |
Christian König | c633c00 | 2018-02-04 10:32:35 +0100 | [diff] [blame] | 623 | amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr); |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 624 | } |
Monk Liu | e9d672b | 2017-03-15 12:18:57 +0800 | [diff] [blame] | 625 | |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 626 | if (pasid_mapping_needed) |
| 627 | amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid); |
| 628 | |
| 629 | if (vm_flush_needed || pasid_mapping_needed) { |
Christian König | c0e5193 | 2017-04-03 14:16:07 +0200 | [diff] [blame] | 630 | r = amdgpu_fence_emit(ring, &fence); |
| 631 | if (r) |
| 632 | return r; |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 633 | } |
Monk Liu | e9d672b | 2017-03-15 12:18:57 +0800 | [diff] [blame] | 634 | |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 635 | if (vm_flush_needed) { |
Christian König | 7645670 | 2017-04-06 17:52:39 +0200 | [diff] [blame] | 636 | mutex_lock(&id_mgr->lock); |
Christian König | c0e5193 | 2017-04-03 14:16:07 +0200 | [diff] [blame] | 637 | dma_fence_put(id->last_flush); |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 638 | id->last_flush = dma_fence_get(fence); |
| 639 | id->current_gpu_reset_count = |
| 640 | atomic_read(&adev->gpu_reset_counter); |
Christian König | 7645670 | 2017-04-06 17:52:39 +0200 | [diff] [blame] | 641 | mutex_unlock(&id_mgr->lock); |
Christian König | c0e5193 | 2017-04-03 14:16:07 +0200 | [diff] [blame] | 642 | } |
Monk Liu | e9d672b | 2017-03-15 12:18:57 +0800 | [diff] [blame] | 643 | |
Christian König | b3cd285 | 2018-02-05 17:38:01 +0100 | [diff] [blame] | 644 | if (pasid_mapping_needed) { |
| 645 | id->pasid = job->pasid; |
| 646 | dma_fence_put(id->pasid_mapping); |
| 647 | id->pasid_mapping = dma_fence_get(fence); |
| 648 | } |
| 649 | dma_fence_put(fence); |
| 650 | |
Chunming Zhou | 7c4378f | 2017-05-11 18:22:17 +0800 | [diff] [blame] | 651 | if (ring->funcs->emit_gds_switch && gds_switch_needed) { |
Christian König | c0e5193 | 2017-04-03 14:16:07 +0200 | [diff] [blame] | 652 | id->gds_base = job->gds_base; |
| 653 | id->gds_size = job->gds_size; |
| 654 | id->gws_base = job->gws_base; |
| 655 | id->gws_size = job->gws_size; |
| 656 | id->oa_base = job->oa_base; |
| 657 | id->oa_size = job->oa_size; |
Christian König | c4f46f2 | 2017-12-18 17:08:25 +0100 | [diff] [blame] | 658 | amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, |
Christian König | c0e5193 | 2017-04-03 14:16:07 +0200 | [diff] [blame] | 659 | job->gds_size, job->gws_base, |
| 660 | job->gws_size, job->oa_base, |
| 661 | job->oa_size); |
| 662 | } |
| 663 | |
| 664 | if (ring->funcs->patch_cond_exec) |
| 665 | amdgpu_ring_patch_cond_exec(ring, patch_offset); |
| 666 | |
| 667 | /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ |
| 668 | if (ring->funcs->emit_switch_buffer) { |
| 669 | amdgpu_ring_emit_switch_buffer(ring); |
| 670 | amdgpu_ring_emit_switch_buffer(ring); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 671 | } |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 672 | return 0; |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 676 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo |
| 677 | * |
| 678 | * @vm: requested vm |
| 679 | * @bo: requested buffer object |
| 680 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 681 | * Find @bo inside the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 682 | * Search inside the @bos vm list for the requested vm |
| 683 | * Returns the found bo_va or NULL if none is found |
| 684 | * |
| 685 | * Object has to be reserved! |
| 686 | */ |
| 687 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, |
| 688 | struct amdgpu_bo *bo) |
| 689 | { |
| 690 | struct amdgpu_bo_va *bo_va; |
| 691 | |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 692 | list_for_each_entry(bo_va, &bo->va, base.bo_list) { |
| 693 | if (bo_va->base.vm == vm) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 694 | return bo_va; |
| 695 | } |
| 696 | } |
| 697 | return NULL; |
| 698 | } |
| 699 | |
| 700 | /** |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 701 | * amdgpu_vm_do_set_ptes - helper to call the right asic function |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 702 | * |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 703 | * @params: see amdgpu_pte_update_params definition |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 704 | * @bo: PD/PT to update |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 705 | * @pe: addr of the page entry |
| 706 | * @addr: dst addr to write into pe |
| 707 | * @count: number of page entries to update |
| 708 | * @incr: increase next addr by incr bytes |
| 709 | * @flags: hw access flags |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 710 | * |
| 711 | * Traces the parameters and calls the right asic functions |
| 712 | * to setup the page table using the DMA. |
| 713 | */ |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 714 | static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 715 | struct amdgpu_bo *bo, |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 716 | uint64_t pe, uint64_t addr, |
| 717 | unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 718 | uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 719 | { |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 720 | pe += amdgpu_bo_gpu_offset(bo); |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 721 | trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 722 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 723 | if (count < 3) { |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 724 | amdgpu_vm_write_pte(params->adev, params->ib, pe, |
| 725 | addr | flags, count, incr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 726 | |
| 727 | } else { |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 728 | amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 729 | count, incr, flags); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /** |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 734 | * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART |
| 735 | * |
| 736 | * @params: see amdgpu_pte_update_params definition |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 737 | * @bo: PD/PT to update |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 738 | * @pe: addr of the page entry |
| 739 | * @addr: dst addr to write into pe |
| 740 | * @count: number of page entries to update |
| 741 | * @incr: increase next addr by incr bytes |
| 742 | * @flags: hw access flags |
| 743 | * |
| 744 | * Traces the parameters and calls the DMA function to copy the PTEs. |
| 745 | */ |
| 746 | static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 747 | struct amdgpu_bo *bo, |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 748 | uint64_t pe, uint64_t addr, |
| 749 | unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 750 | uint64_t flags) |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 751 | { |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 752 | uint64_t src = (params->src + (addr >> 12) * 8); |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 753 | |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 754 | pe += amdgpu_bo_gpu_offset(bo); |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 755 | trace_amdgpu_vm_copy_ptes(pe, src, count); |
| 756 | |
| 757 | amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count); |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /** |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 761 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 762 | * |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 763 | * @pages_addr: optional DMA address to use for lookup |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 764 | * @addr: the unmapped addr |
| 765 | * |
| 766 | * Look up the physical address of the page that the pte resolves |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 767 | * to and return the pointer for the page table entry. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 768 | */ |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 769 | static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 770 | { |
| 771 | uint64_t result; |
| 772 | |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 773 | /* page table offset */ |
| 774 | result = pages_addr[addr >> PAGE_SHIFT]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 775 | |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 776 | /* in case cpu page size != gpu page size*/ |
| 777 | result |= addr & (~PAGE_MASK); |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 778 | |
| 779 | result &= 0xFFFFFFFFFFFFF000ULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 780 | |
| 781 | return result; |
| 782 | } |
| 783 | |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 784 | /** |
| 785 | * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU |
| 786 | * |
| 787 | * @params: see amdgpu_pte_update_params definition |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 788 | * @bo: PD/PT to update |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 789 | * @pe: kmap addr of the page entry |
| 790 | * @addr: dst addr to write into pe |
| 791 | * @count: number of page entries to update |
| 792 | * @incr: increase next addr by incr bytes |
| 793 | * @flags: hw access flags |
| 794 | * |
| 795 | * Write count number of PT/PD entries directly. |
| 796 | */ |
| 797 | static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 798 | struct amdgpu_bo *bo, |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 799 | uint64_t pe, uint64_t addr, |
| 800 | unsigned count, uint32_t incr, |
| 801 | uint64_t flags) |
| 802 | { |
| 803 | unsigned int i; |
Harish Kasiviswanathan | b4d4251 | 2017-05-11 19:47:22 -0400 | [diff] [blame] | 804 | uint64_t value; |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 805 | |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 806 | pe += (unsigned long)amdgpu_bo_kptr(bo); |
| 807 | |
Christian König | 03918b3 | 2017-07-11 17:15:37 +0200 | [diff] [blame] | 808 | trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags); |
| 809 | |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 810 | for (i = 0; i < count; i++) { |
Harish Kasiviswanathan | b4d4251 | 2017-05-11 19:47:22 -0400 | [diff] [blame] | 811 | value = params->pages_addr ? |
| 812 | amdgpu_vm_map_gart(params->pages_addr, addr) : |
| 813 | addr; |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 814 | amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe, |
| 815 | i, value, flags); |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 816 | addr += incr; |
| 817 | } |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 818 | } |
| 819 | |
Christian König | a33cab7 | 2017-07-11 17:13:00 +0200 | [diff] [blame] | 820 | static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
| 821 | void *owner) |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 822 | { |
| 823 | struct amdgpu_sync sync; |
| 824 | int r; |
| 825 | |
| 826 | amdgpu_sync_create(&sync); |
Andres Rodriguez | 177ae09 | 2017-09-15 20:44:06 -0400 | [diff] [blame] | 827 | amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false); |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 828 | r = amdgpu_sync_wait(&sync, true); |
| 829 | amdgpu_sync_free(&sync); |
| 830 | |
| 831 | return r; |
| 832 | } |
| 833 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 834 | /* |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 835 | * amdgpu_vm_update_pde - update a single level in the hierarchy |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 836 | * |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 837 | * @param: parameters for the update |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 838 | * @vm: requested vm |
Christian König | 194d216 | 2016-10-12 15:13:52 +0200 | [diff] [blame] | 839 | * @parent: parent directory |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 840 | * @entry: entry to update |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 841 | * |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 842 | * Makes sure the requested entry in parent is up to date. |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 843 | */ |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 844 | static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params, |
| 845 | struct amdgpu_vm *vm, |
| 846 | struct amdgpu_vm_pt *parent, |
| 847 | struct amdgpu_vm_pt *entry) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 848 | { |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 849 | struct amdgpu_bo *bo = parent->base.bo, *pbo; |
Christian König | 3de676d | 2017-11-29 13:27:26 +0100 | [diff] [blame] | 850 | uint64_t pde, pt, flags; |
| 851 | unsigned level; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 852 | |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 853 | /* Don't update huge pages here */ |
| 854 | if (entry->huge) |
| 855 | return; |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 856 | |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 857 | for (level = 0, pbo = bo->parent; pbo; ++level) |
Christian König | 3de676d | 2017-11-29 13:27:26 +0100 | [diff] [blame] | 858 | pbo = pbo->parent; |
| 859 | |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 860 | level += params->adev->vm_manager.root_level; |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 861 | pt = amdgpu_bo_gpu_offset(entry->base.bo); |
Christian König | 3de676d | 2017-11-29 13:27:26 +0100 | [diff] [blame] | 862 | flags = AMDGPU_PTE_VALID; |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 863 | amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags); |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 864 | pde = (entry - parent->entries) * 8; |
| 865 | if (bo->shadow) |
| 866 | params->func(params, bo->shadow, pde, pt, 1, 0, flags); |
| 867 | params->func(params, bo, pde, pt, 1, 0, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 868 | } |
| 869 | |
Christian König | 194d216 | 2016-10-12 15:13:52 +0200 | [diff] [blame] | 870 | /* |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 871 | * amdgpu_vm_invalidate_level - mark all PD levels as invalid |
| 872 | * |
| 873 | * @parent: parent PD |
| 874 | * |
| 875 | * Mark all PD level as invalid after an error. |
| 876 | */ |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 877 | static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev, |
| 878 | struct amdgpu_vm *vm, |
| 879 | struct amdgpu_vm_pt *parent, |
| 880 | unsigned level) |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 881 | { |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 882 | unsigned pt_idx, num_entries; |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 883 | |
| 884 | /* |
| 885 | * Recurse into the subdirectories. This recursion is harmless because |
| 886 | * we only have a maximum of 5 layers. |
| 887 | */ |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 888 | num_entries = amdgpu_vm_num_entries(adev, level); |
| 889 | for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) { |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 890 | struct amdgpu_vm_pt *entry = &parent->entries[pt_idx]; |
| 891 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 892 | if (!entry->base.bo) |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 893 | continue; |
| 894 | |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 895 | spin_lock(&vm->status_lock); |
Christian König | 481c2e9 | 2017-09-01 14:46:19 +0200 | [diff] [blame] | 896 | if (list_empty(&entry->base.vm_status)) |
| 897 | list_add(&entry->base.vm_status, &vm->relocated); |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 898 | spin_unlock(&vm->status_lock); |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 899 | amdgpu_vm_invalidate_level(adev, vm, entry, level + 1); |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | /* |
Christian König | 194d216 | 2016-10-12 15:13:52 +0200 | [diff] [blame] | 904 | * amdgpu_vm_update_directories - make sure that all directories are valid |
| 905 | * |
| 906 | * @adev: amdgpu_device pointer |
| 907 | * @vm: requested vm |
| 908 | * |
| 909 | * Makes sure all directories are up to date. |
| 910 | * Returns 0 for success, error for failure. |
| 911 | */ |
| 912 | int amdgpu_vm_update_directories(struct amdgpu_device *adev, |
| 913 | struct amdgpu_vm *vm) |
| 914 | { |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 915 | struct amdgpu_pte_update_params params; |
| 916 | struct amdgpu_job *job; |
| 917 | unsigned ndw = 0; |
Dan Carpenter | 78aa02c | 2017-09-30 11:14:13 +0300 | [diff] [blame] | 918 | int r = 0; |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 919 | |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 920 | if (list_empty(&vm->relocated)) |
| 921 | return 0; |
| 922 | |
| 923 | restart: |
| 924 | memset(¶ms, 0, sizeof(params)); |
| 925 | params.adev = adev; |
| 926 | |
| 927 | if (vm->use_cpu_for_update) { |
| 928 | r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM); |
| 929 | if (unlikely(r)) |
| 930 | return r; |
| 931 | |
| 932 | params.func = amdgpu_vm_cpu_set_ptes; |
| 933 | } else { |
| 934 | ndw = 512 * 8; |
| 935 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
| 936 | if (r) |
| 937 | return r; |
| 938 | |
| 939 | params.ib = &job->ibs[0]; |
| 940 | params.func = amdgpu_vm_do_set_ptes; |
| 941 | } |
| 942 | |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 943 | spin_lock(&vm->status_lock); |
| 944 | while (!list_empty(&vm->relocated)) { |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 945 | struct amdgpu_vm_bo_base *bo_base, *parent; |
| 946 | struct amdgpu_vm_pt *pt, *entry; |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 947 | struct amdgpu_bo *bo; |
| 948 | |
| 949 | bo_base = list_first_entry(&vm->relocated, |
| 950 | struct amdgpu_vm_bo_base, |
| 951 | vm_status); |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 952 | list_del_init(&bo_base->vm_status); |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 953 | spin_unlock(&vm->status_lock); |
| 954 | |
| 955 | bo = bo_base->bo->parent; |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 956 | if (!bo) { |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 957 | spin_lock(&vm->status_lock); |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 958 | continue; |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 959 | } |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 960 | |
| 961 | parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base, |
| 962 | bo_list); |
| 963 | pt = container_of(parent, struct amdgpu_vm_pt, base); |
| 964 | entry = container_of(bo_base, struct amdgpu_vm_pt, base); |
| 965 | |
| 966 | amdgpu_vm_update_pde(¶ms, vm, pt, entry); |
| 967 | |
| 968 | spin_lock(&vm->status_lock); |
| 969 | if (!vm->use_cpu_for_update && |
| 970 | (ndw - params.ib->length_dw) < 32) |
| 971 | break; |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 972 | } |
| 973 | spin_unlock(&vm->status_lock); |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 974 | |
Christian König | 68c6230 | 2017-07-11 17:23:29 +0200 | [diff] [blame] | 975 | if (vm->use_cpu_for_update) { |
| 976 | /* Flush HDP */ |
| 977 | mb(); |
Christian König | 6988256 | 2018-01-19 14:17:40 +0100 | [diff] [blame] | 978 | amdgpu_asic_flush_hdp(adev, NULL); |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 979 | } else if (params.ib->length_dw == 0) { |
| 980 | amdgpu_job_free(job); |
| 981 | } else { |
| 982 | struct amdgpu_bo *root = vm->root.base.bo; |
| 983 | struct amdgpu_ring *ring; |
| 984 | struct dma_fence *fence; |
| 985 | |
| 986 | ring = container_of(vm->entity.sched, struct amdgpu_ring, |
| 987 | sched); |
| 988 | |
| 989 | amdgpu_ring_pad_ib(ring, params.ib); |
| 990 | amdgpu_sync_resv(adev, &job->sync, root->tbo.resv, |
| 991 | AMDGPU_FENCE_OWNER_VM, false); |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 992 | WARN_ON(params.ib->length_dw > ndw); |
| 993 | r = amdgpu_job_submit(job, ring, &vm->entity, |
| 994 | AMDGPU_FENCE_OWNER_VM, &fence); |
| 995 | if (r) |
| 996 | goto error; |
| 997 | |
| 998 | amdgpu_bo_fence(root, fence, true); |
| 999 | dma_fence_put(vm->last_update); |
| 1000 | vm->last_update = fence; |
Christian König | 68c6230 | 2017-07-11 17:23:29 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 1003 | if (!list_empty(&vm->relocated)) |
| 1004 | goto restart; |
| 1005 | |
| 1006 | return 0; |
| 1007 | |
| 1008 | error: |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 1009 | amdgpu_vm_invalidate_level(adev, vm, &vm->root, |
| 1010 | adev->vm_manager.root_level); |
Christian König | 6989f24 | 2017-11-30 19:08:05 +0100 | [diff] [blame] | 1011 | amdgpu_job_free(job); |
Christian König | 92456b9 | 2017-05-12 16:09:26 +0200 | [diff] [blame] | 1012 | return r; |
Christian König | 194d216 | 2016-10-12 15:13:52 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1015 | /** |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1016 | * amdgpu_vm_find_entry - find the entry for an address |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1017 | * |
| 1018 | * @p: see amdgpu_pte_update_params definition |
| 1019 | * @addr: virtual address in question |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1020 | * @entry: resulting entry or NULL |
| 1021 | * @parent: parent entry |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1022 | * |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1023 | * Find the vm_pt entry and it's parent for the given address. |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1024 | */ |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1025 | void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr, |
| 1026 | struct amdgpu_vm_pt **entry, |
| 1027 | struct amdgpu_vm_pt **parent) |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1028 | { |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 1029 | unsigned level = p->adev->vm_manager.root_level; |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1030 | |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1031 | *parent = NULL; |
| 1032 | *entry = &p->vm->root; |
| 1033 | while ((*entry)->entries) { |
Christian König | e3a1b32 | 2017-12-01 13:28:46 +0100 | [diff] [blame] | 1034 | unsigned shift = amdgpu_vm_level_shift(p->adev, level++); |
Christian König | 5078314 | 2017-11-27 14:01:51 +0100 | [diff] [blame] | 1035 | |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1036 | *parent = *entry; |
Christian König | e3a1b32 | 2017-12-01 13:28:46 +0100 | [diff] [blame] | 1037 | *entry = &(*entry)->entries[addr >> shift]; |
| 1038 | addr &= (1ULL << shift) - 1; |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 1041 | if (level != AMDGPU_VM_PTB) |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1042 | *entry = NULL; |
| 1043 | } |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1044 | |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1045 | /** |
| 1046 | * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages |
| 1047 | * |
| 1048 | * @p: see amdgpu_pte_update_params definition |
| 1049 | * @entry: vm_pt entry to check |
| 1050 | * @parent: parent entry |
| 1051 | * @nptes: number of PTEs updated with this operation |
| 1052 | * @dst: destination address where the PTEs should point to |
| 1053 | * @flags: access flags fro the PTEs |
| 1054 | * |
| 1055 | * Check if we can update the PD with a huge page. |
| 1056 | */ |
Christian König | ec5207c | 2017-08-03 19:24:06 +0200 | [diff] [blame] | 1057 | static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p, |
| 1058 | struct amdgpu_vm_pt *entry, |
| 1059 | struct amdgpu_vm_pt *parent, |
| 1060 | unsigned nptes, uint64_t dst, |
| 1061 | uint64_t flags) |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1062 | { |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 1063 | uint64_t pde; |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1064 | |
| 1065 | /* In the case of a mixed PT the PDE must point to it*/ |
Christian König | 3cc1d3e | 2017-12-21 15:47:28 +0100 | [diff] [blame] | 1066 | if (p->adev->asic_type >= CHIP_VEGA10 && !p->src && |
| 1067 | nptes == AMDGPU_VM_PTE_COUNT(p->adev)) { |
Christian König | 4ab4016 | 2017-08-03 20:30:50 +0200 | [diff] [blame] | 1068 | /* Set the huge page flag to stop scanning at this PDE */ |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1069 | flags |= AMDGPU_PDE_PTE; |
| 1070 | } |
| 1071 | |
Christian König | 3cc1d3e | 2017-12-21 15:47:28 +0100 | [diff] [blame] | 1072 | if (!(flags & AMDGPU_PDE_PTE)) { |
| 1073 | if (entry->huge) { |
| 1074 | /* Add the entry to the relocated list to update it. */ |
| 1075 | entry->huge = false; |
| 1076 | spin_lock(&p->vm->status_lock); |
| 1077 | list_move(&entry->base.vm_status, &p->vm->relocated); |
| 1078 | spin_unlock(&p->vm->status_lock); |
| 1079 | } |
Christian König | ec5207c | 2017-08-03 19:24:06 +0200 | [diff] [blame] | 1080 | return; |
Christian König | 3cc1d3e | 2017-12-21 15:47:28 +0100 | [diff] [blame] | 1081 | } |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1082 | |
Christian König | 3cc1d3e | 2017-12-21 15:47:28 +0100 | [diff] [blame] | 1083 | entry->huge = true; |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 1084 | amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags); |
Christian König | 3de676d | 2017-11-29 13:27:26 +0100 | [diff] [blame] | 1085 | |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 1086 | pde = (entry - parent->entries) * 8; |
| 1087 | if (parent->base.bo->shadow) |
| 1088 | p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags); |
| 1089 | p->func(p, parent->base.bo, pde, dst, 1, 0, flags); |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /** |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1093 | * amdgpu_vm_update_ptes - make sure that page tables are valid |
| 1094 | * |
| 1095 | * @params: see amdgpu_pte_update_params definition |
| 1096 | * @vm: requested vm |
| 1097 | * @start: start of GPU address range |
| 1098 | * @end: end of GPU address range |
| 1099 | * @dst: destination address to map to, the next dst inside the function |
| 1100 | * @flags: mapping flags |
| 1101 | * |
| 1102 | * Update the page tables in the range @start - @end. |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1103 | * Returns 0 for success, -EINVAL for failure. |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1104 | */ |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1105 | static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1106 | uint64_t start, uint64_t end, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1107 | uint64_t dst, uint64_t flags) |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1108 | { |
Zhang, Jerry | 36b32a6 | 2017-03-29 16:08:32 +0800 | [diff] [blame] | 1109 | struct amdgpu_device *adev = params->adev; |
| 1110 | const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1; |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1111 | |
Christian König | 301654a | 2017-05-16 14:30:27 +0200 | [diff] [blame] | 1112 | uint64_t addr, pe_start; |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1113 | struct amdgpu_bo *pt; |
Christian König | 301654a | 2017-05-16 14:30:27 +0200 | [diff] [blame] | 1114 | unsigned nptes; |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1115 | |
| 1116 | /* walk over the address space and update the page tables */ |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1117 | for (addr = start; addr < end; addr += nptes, |
| 1118 | dst += nptes * AMDGPU_GPU_PAGE_SIZE) { |
| 1119 | struct amdgpu_vm_pt *entry, *parent; |
| 1120 | |
| 1121 | amdgpu_vm_get_entry(params, addr, &entry, &parent); |
| 1122 | if (!entry) |
| 1123 | return -ENOENT; |
Christian König | 4e2cb64 | 2016-10-25 15:52:28 +0200 | [diff] [blame] | 1124 | |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1125 | if ((addr & ~mask) == (end & ~mask)) |
| 1126 | nptes = end - addr; |
| 1127 | else |
Zhang, Jerry | 36b32a6 | 2017-03-29 16:08:32 +0800 | [diff] [blame] | 1128 | nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask); |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1129 | |
Christian König | ec5207c | 2017-08-03 19:24:06 +0200 | [diff] [blame] | 1130 | amdgpu_vm_handle_huge_pages(params, entry, parent, |
| 1131 | nptes, dst, flags); |
Christian König | 4ab4016 | 2017-08-03 20:30:50 +0200 | [diff] [blame] | 1132 | /* We don't need to update PTEs for huge pages */ |
Christian König | 78eb2f0 | 2017-11-30 15:41:28 +0100 | [diff] [blame] | 1133 | if (entry->huge) |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 1134 | continue; |
| 1135 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 1136 | pt = entry->base.bo; |
Christian König | 373ac64 | 2018-01-16 16:54:25 +0100 | [diff] [blame] | 1137 | pe_start = (addr & mask) * 8; |
| 1138 | if (pt->shadow) |
| 1139 | params->func(params, pt->shadow, pe_start, dst, nptes, |
| 1140 | AMDGPU_GPU_PAGE_SIZE, flags); |
| 1141 | params->func(params, pt, pe_start, dst, nptes, |
Christian König | 301654a | 2017-05-16 14:30:27 +0200 | [diff] [blame] | 1142 | AMDGPU_GPU_PAGE_SIZE, flags); |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1145 | return 0; |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /* |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1149 | * amdgpu_vm_frag_ptes - add fragment information to PTEs |
| 1150 | * |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1151 | * @params: see amdgpu_pte_update_params definition |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1152 | * @vm: requested vm |
| 1153 | * @start: first PTE to handle |
| 1154 | * @end: last PTE to handle |
| 1155 | * @dst: addr those PTEs should point to |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1156 | * @flags: hw mapping flags |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1157 | * Returns 0 for success, -EINVAL for failure. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1158 | */ |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1159 | static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 1160 | uint64_t start, uint64_t end, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1161 | uint64_t dst, uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1162 | { |
| 1163 | /** |
| 1164 | * The MC L1 TLB supports variable sized pages, based on a fragment |
| 1165 | * field in the PTE. When this field is set to a non-zero value, page |
| 1166 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE |
| 1167 | * flags are considered valid for all PTEs within the fragment range |
| 1168 | * and corresponding mappings are assumed to be physically contiguous. |
| 1169 | * |
| 1170 | * The L1 TLB can store a single PTE for the whole fragment, |
| 1171 | * significantly increasing the space available for translation |
| 1172 | * caching. This leads to large improvements in throughput when the |
| 1173 | * TLB is under pressure. |
| 1174 | * |
| 1175 | * The L2 TLB distributes small and large fragments into two |
| 1176 | * asymmetric partitions. The large fragment cache is significantly |
| 1177 | * larger. Thus, we try to use large fragments wherever possible. |
| 1178 | * Userspace can support this by aligning virtual base address and |
| 1179 | * allocation size to the fragment size. |
| 1180 | */ |
Roger He | 6849d47 | 2017-08-30 13:01:19 +0800 | [diff] [blame] | 1181 | unsigned max_frag = params->adev->vm_manager.fragment_size; |
| 1182 | int r; |
Christian König | 31f6c1f | 2016-01-26 12:37:49 +0100 | [diff] [blame] | 1183 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1184 | /* system pages are non continuously */ |
Roger He | 6849d47 | 2017-08-30 13:01:19 +0800 | [diff] [blame] | 1185 | if (params->src || !(flags & AMDGPU_PTE_VALID)) |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1186 | return amdgpu_vm_update_ptes(params, start, end, dst, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1187 | |
Roger He | 6849d47 | 2017-08-30 13:01:19 +0800 | [diff] [blame] | 1188 | while (start != end) { |
| 1189 | uint64_t frag_flags, frag_end; |
| 1190 | unsigned frag; |
| 1191 | |
| 1192 | /* This intentionally wraps around if no bit is set */ |
| 1193 | frag = min((unsigned)ffs(start) - 1, |
| 1194 | (unsigned)fls64(end - start) - 1); |
| 1195 | if (frag >= max_frag) { |
| 1196 | frag_flags = AMDGPU_PTE_FRAG(max_frag); |
| 1197 | frag_end = end & ~((1ULL << max_frag) - 1); |
| 1198 | } else { |
| 1199 | frag_flags = AMDGPU_PTE_FRAG(frag); |
| 1200 | frag_end = start + (1 << frag); |
| 1201 | } |
| 1202 | |
| 1203 | r = amdgpu_vm_update_ptes(params, start, frag_end, dst, |
| 1204 | flags | frag_flags); |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1205 | if (r) |
| 1206 | return r; |
Roger He | 6849d47 | 2017-08-30 13:01:19 +0800 | [diff] [blame] | 1207 | |
| 1208 | dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE; |
| 1209 | start = frag_end; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1210 | } |
| 1211 | |
Roger He | 6849d47 | 2017-08-30 13:01:19 +0800 | [diff] [blame] | 1212 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1216 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table |
| 1217 | * |
| 1218 | * @adev: amdgpu_device pointer |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1219 | * @exclusive: fence we need to sync to |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 1220 | * @pages_addr: DMA addresses to use for mapping |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1221 | * @vm: requested vm |
| 1222 | * @start: start of mapped range |
| 1223 | * @last: last mapped entry |
| 1224 | * @flags: flags for the entries |
| 1225 | * @addr: addr to set the area to |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1226 | * @fence: optional resulting fence |
| 1227 | * |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1228 | * Fill in the page table entries between @start and @last. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1229 | * Returns 0 for success, -EINVAL for failure. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1230 | */ |
| 1231 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1232 | struct dma_fence *exclusive, |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 1233 | dma_addr_t *pages_addr, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1234 | struct amdgpu_vm *vm, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1235 | uint64_t start, uint64_t last, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1236 | uint64_t flags, uint64_t addr, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1237 | struct dma_fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1238 | { |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1239 | struct amdgpu_ring *ring; |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 1240 | void *owner = AMDGPU_FENCE_OWNER_VM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1241 | unsigned nptes, ncmds, ndw; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1242 | struct amdgpu_job *job; |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1243 | struct amdgpu_pte_update_params params; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1244 | struct dma_fence *f = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1245 | int r; |
| 1246 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1247 | memset(¶ms, 0, sizeof(params)); |
| 1248 | params.adev = adev; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 1249 | params.vm = vm; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1250 | |
Christian König | a33cab7 | 2017-07-11 17:13:00 +0200 | [diff] [blame] | 1251 | /* sync to everything on unmapping */ |
| 1252 | if (!(flags & AMDGPU_PTE_VALID)) |
| 1253 | owner = AMDGPU_FENCE_OWNER_UNDEFINED; |
| 1254 | |
Harish Kasiviswanathan | b4d4251 | 2017-05-11 19:47:22 -0400 | [diff] [blame] | 1255 | if (vm->use_cpu_for_update) { |
| 1256 | /* params.src is used as flag to indicate system Memory */ |
| 1257 | if (pages_addr) |
| 1258 | params.src = ~0; |
| 1259 | |
| 1260 | /* Wait for PT BOs to be free. PTs share the same resv. object |
| 1261 | * as the root PD BO |
| 1262 | */ |
Christian König | a33cab7 | 2017-07-11 17:13:00 +0200 | [diff] [blame] | 1263 | r = amdgpu_vm_wait_pd(adev, vm, owner); |
Harish Kasiviswanathan | b4d4251 | 2017-05-11 19:47:22 -0400 | [diff] [blame] | 1264 | if (unlikely(r)) |
| 1265 | return r; |
| 1266 | |
| 1267 | params.func = amdgpu_vm_cpu_set_ptes; |
| 1268 | params.pages_addr = pages_addr; |
Harish Kasiviswanathan | b4d4251 | 2017-05-11 19:47:22 -0400 | [diff] [blame] | 1269 | return amdgpu_vm_frag_ptes(¶ms, start, last + 1, |
| 1270 | addr, flags); |
| 1271 | } |
| 1272 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1273 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 1274 | |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1275 | nptes = last - start + 1; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1276 | |
| 1277 | /* |
Bas Nieuwenhuizen | 8620952 | 2017-09-07 13:23:21 +0200 | [diff] [blame] | 1278 | * reserve space for two commands every (1 << BLOCK_SIZE) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1279 | * entries or 2k dwords (whatever is smaller) |
Bas Nieuwenhuizen | 8620952 | 2017-09-07 13:23:21 +0200 | [diff] [blame] | 1280 | * |
| 1281 | * The second command is for the shadow pagetables. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1282 | */ |
Emily Deng | 104bd2c | 2017-12-29 13:13:08 +0800 | [diff] [blame] | 1283 | if (vm->root.base.bo->shadow) |
| 1284 | ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2; |
| 1285 | else |
| 1286 | ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1287 | |
| 1288 | /* padding, etc. */ |
| 1289 | ndw = 64; |
| 1290 | |
Christian König | 570144c | 2017-08-30 15:38:45 +0200 | [diff] [blame] | 1291 | if (pages_addr) { |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1292 | /* copy commands needed */ |
Yong Zhao | e6d9219 | 2017-09-19 12:58:15 -0400 | [diff] [blame] | 1293 | ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1294 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1295 | /* and also PTEs */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1296 | ndw += nptes * 2; |
| 1297 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1298 | params.func = amdgpu_vm_do_copy_ptes; |
| 1299 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1300 | } else { |
| 1301 | /* set page commands needed */ |
Christian König | 44e1bae | 2018-01-24 19:58:45 +0100 | [diff] [blame] | 1302 | ndw += ncmds * 10; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1303 | |
Roger He | 6849d47 | 2017-08-30 13:01:19 +0800 | [diff] [blame] | 1304 | /* extra commands for begin/end fragments */ |
Christian König | 44e1bae | 2018-01-24 19:58:45 +0100 | [diff] [blame] | 1305 | ndw += 2 * 10 * adev->vm_manager.fragment_size; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1306 | |
| 1307 | params.func = amdgpu_vm_do_set_ptes; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1308 | } |
| 1309 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1310 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
| 1311 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1312 | return r; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1313 | |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1314 | params.ib = &job->ibs[0]; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 1315 | |
Christian König | 570144c | 2017-08-30 15:38:45 +0200 | [diff] [blame] | 1316 | if (pages_addr) { |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1317 | uint64_t *pte; |
| 1318 | unsigned i; |
| 1319 | |
| 1320 | /* Put the PTEs at the end of the IB. */ |
| 1321 | i = ndw - nptes * 2; |
| 1322 | pte= (uint64_t *)&(job->ibs->ptr[i]); |
| 1323 | params.src = job->ibs->gpu_addr + i * 4; |
| 1324 | |
| 1325 | for (i = 0; i < nptes; ++i) { |
| 1326 | pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i * |
| 1327 | AMDGPU_GPU_PAGE_SIZE); |
| 1328 | pte[i] |= flags; |
| 1329 | } |
Christian König | d7a4ac6 | 2016-09-25 11:54:00 +0200 | [diff] [blame] | 1330 | addr = 0; |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1331 | } |
| 1332 | |
Andrey Grodzovsky | cebb52b | 2017-11-13 14:47:52 -0500 | [diff] [blame] | 1333 | r = amdgpu_sync_fence(adev, &job->sync, exclusive, false); |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1334 | if (r) |
| 1335 | goto error_free; |
| 1336 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 1337 | r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv, |
Andres Rodriguez | 177ae09 | 2017-09-15 20:44:06 -0400 | [diff] [blame] | 1338 | owner, false); |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 1339 | if (r) |
| 1340 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1341 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 1342 | r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv); |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 1343 | if (r) |
| 1344 | goto error_free; |
| 1345 | |
Harish Kasiviswanathan | cc28c4e | 2017-05-11 22:39:31 -0400 | [diff] [blame] | 1346 | r = amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
| 1347 | if (r) |
| 1348 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1349 | |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1350 | amdgpu_ring_pad_ib(ring, params.ib); |
| 1351 | WARN_ON(params.ib->length_dw > ndw); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1352 | r = amdgpu_job_submit(job, ring, &vm->entity, |
| 1353 | AMDGPU_FENCE_OWNER_VM, &f); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 1354 | if (r) |
| 1355 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1356 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 1357 | amdgpu_bo_fence(vm->root.base.bo, f, true); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1358 | dma_fence_put(*fence); |
| 1359 | *fence = f; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1360 | return 0; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 1361 | |
| 1362 | error_free: |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1363 | amdgpu_job_free(job); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 1364 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | /** |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1368 | * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks |
| 1369 | * |
| 1370 | * @adev: amdgpu_device pointer |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1371 | * @exclusive: fence we need to sync to |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1372 | * @pages_addr: DMA addresses to use for mapping |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1373 | * @vm: requested vm |
| 1374 | * @mapping: mapped range and flags to use for the update |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1375 | * @flags: HW flags for the mapping |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1376 | * @nodes: array of drm_mm_nodes with the MC addresses |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1377 | * @fence: optional resulting fence |
| 1378 | * |
| 1379 | * Split the mapping into smaller chunks so that each update fits |
| 1380 | * into a SDMA IB. |
| 1381 | * Returns 0 for success, -EINVAL for failure. |
| 1382 | */ |
| 1383 | static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1384 | struct dma_fence *exclusive, |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1385 | dma_addr_t *pages_addr, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1386 | struct amdgpu_vm *vm, |
| 1387 | struct amdgpu_bo_va_mapping *mapping, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1388 | uint64_t flags, |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1389 | struct drm_mm_node *nodes, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1390 | struct dma_fence **fence) |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1391 | { |
Christian König | 9fc8fc7 | 2017-09-18 13:58:30 +0200 | [diff] [blame] | 1392 | unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size; |
Christian König | 570144c | 2017-08-30 15:38:45 +0200 | [diff] [blame] | 1393 | uint64_t pfn, start = mapping->start; |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1394 | int r; |
| 1395 | |
| 1396 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here |
| 1397 | * but in case of something, we filter the flags in first place |
| 1398 | */ |
| 1399 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) |
| 1400 | flags &= ~AMDGPU_PTE_READABLE; |
| 1401 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) |
| 1402 | flags &= ~AMDGPU_PTE_WRITEABLE; |
| 1403 | |
Alex Xie | 15b31c5 | 2017-03-03 16:47:11 -0500 | [diff] [blame] | 1404 | flags &= ~AMDGPU_PTE_EXECUTABLE; |
| 1405 | flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE; |
| 1406 | |
Alex Xie | b0fd18b | 2017-03-03 16:49:39 -0500 | [diff] [blame] | 1407 | flags &= ~AMDGPU_PTE_MTYPE_MASK; |
| 1408 | flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK); |
| 1409 | |
Zhang, Jerry | d0766e9 | 2017-04-19 09:53:29 +0800 | [diff] [blame] | 1410 | if ((mapping->flags & AMDGPU_PTE_PRT) && |
| 1411 | (adev->asic_type >= CHIP_VEGA10)) { |
| 1412 | flags |= AMDGPU_PTE_PRT; |
| 1413 | flags &= ~AMDGPU_PTE_VALID; |
| 1414 | } |
| 1415 | |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1416 | trace_amdgpu_vm_bo_update(mapping); |
| 1417 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1418 | pfn = mapping->offset >> PAGE_SHIFT; |
| 1419 | if (nodes) { |
| 1420 | while (pfn >= nodes->size) { |
| 1421 | pfn -= nodes->size; |
| 1422 | ++nodes; |
| 1423 | } |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 1424 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1425 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1426 | do { |
Christian König | 9fc8fc7 | 2017-09-18 13:58:30 +0200 | [diff] [blame] | 1427 | dma_addr_t *dma_addr = NULL; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1428 | uint64_t max_entries; |
| 1429 | uint64_t addr, last; |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1430 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1431 | if (nodes) { |
| 1432 | addr = nodes->start << PAGE_SHIFT; |
| 1433 | max_entries = (nodes->size - pfn) * |
| 1434 | (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); |
| 1435 | } else { |
| 1436 | addr = 0; |
| 1437 | max_entries = S64_MAX; |
| 1438 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1439 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1440 | if (pages_addr) { |
Christian König | 9fc8fc7 | 2017-09-18 13:58:30 +0200 | [diff] [blame] | 1441 | uint64_t count; |
| 1442 | |
Christian König | 457e0fe | 2017-08-22 12:50:46 +0200 | [diff] [blame] | 1443 | max_entries = min(max_entries, 16ull * 1024ull); |
Christian König | 9fc8fc7 | 2017-09-18 13:58:30 +0200 | [diff] [blame] | 1444 | for (count = 1; count < max_entries; ++count) { |
| 1445 | uint64_t idx = pfn + count; |
| 1446 | |
| 1447 | if (pages_addr[idx] != |
| 1448 | (pages_addr[idx - 1] + PAGE_SIZE)) |
| 1449 | break; |
| 1450 | } |
| 1451 | |
| 1452 | if (count < min_linear_pages) { |
| 1453 | addr = pfn << PAGE_SHIFT; |
| 1454 | dma_addr = pages_addr; |
| 1455 | } else { |
| 1456 | addr = pages_addr[pfn]; |
| 1457 | max_entries = count; |
| 1458 | } |
| 1459 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1460 | } else if (flags & AMDGPU_PTE_VALID) { |
| 1461 | addr += adev->vm_manager.vram_base_offset; |
Christian König | 9fc8fc7 | 2017-09-18 13:58:30 +0200 | [diff] [blame] | 1462 | addr += pfn << PAGE_SHIFT; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1463 | } |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1464 | |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 1465 | last = min((uint64_t)mapping->last, start + max_entries - 1); |
Christian König | 9fc8fc7 | 2017-09-18 13:58:30 +0200 | [diff] [blame] | 1466 | r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1467 | start, last, flags, addr, |
| 1468 | fence); |
| 1469 | if (r) |
| 1470 | return r; |
| 1471 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1472 | pfn += last - start + 1; |
| 1473 | if (nodes && nodes->size == pfn) { |
| 1474 | pfn = 0; |
| 1475 | ++nodes; |
| 1476 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1477 | start = last + 1; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1478 | |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 1479 | } while (unlikely(start != mapping->last + 1)); |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1480 | |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1485 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table |
| 1486 | * |
| 1487 | * @adev: amdgpu_device pointer |
| 1488 | * @bo_va: requested BO and VM object |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1489 | * @clear: if true clear the entries |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1490 | * |
| 1491 | * Fill in the page table entries for @bo_va. |
| 1492 | * Returns 0 for success, -EINVAL for failure. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1493 | */ |
| 1494 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, |
| 1495 | struct amdgpu_bo_va *bo_va, |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1496 | bool clear) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1497 | { |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1498 | struct amdgpu_bo *bo = bo_va->base.bo; |
| 1499 | struct amdgpu_vm *vm = bo_va->base.vm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1500 | struct amdgpu_bo_va_mapping *mapping; |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1501 | dma_addr_t *pages_addr = NULL; |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1502 | struct ttm_mem_reg *mem; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1503 | struct drm_mm_node *nodes; |
Christian König | 4e55eb3 | 2017-09-11 16:54:59 +0200 | [diff] [blame] | 1504 | struct dma_fence *exclusive, **last_update; |
Christian König | 457e0fe | 2017-08-22 12:50:46 +0200 | [diff] [blame] | 1505 | uint64_t flags; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1506 | int r; |
| 1507 | |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1508 | if (clear || !bo_va->base.bo) { |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1509 | mem = NULL; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1510 | nodes = NULL; |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1511 | exclusive = NULL; |
| 1512 | } else { |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1513 | struct ttm_dma_tt *ttm; |
| 1514 | |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1515 | mem = &bo_va->base.bo->tbo.mem; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1516 | nodes = mem->mm_node; |
| 1517 | if (mem->mem_type == TTM_PL_TT) { |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1518 | ttm = container_of(bo_va->base.bo->tbo.ttm, |
| 1519 | struct ttm_dma_tt, ttm); |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1520 | pages_addr = ttm->dma_address; |
Christian König | 9ab2146 | 2015-11-30 14:19:26 +0100 | [diff] [blame] | 1521 | } |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1522 | exclusive = reservation_object_get_excl(bo->tbo.resv); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1523 | } |
| 1524 | |
Christian König | 457e0fe | 2017-08-22 12:50:46 +0200 | [diff] [blame] | 1525 | if (bo) |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1526 | flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem); |
Christian König | 457e0fe | 2017-08-22 12:50:46 +0200 | [diff] [blame] | 1527 | else |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1528 | flags = 0x0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1529 | |
Christian König | 4e55eb3 | 2017-09-11 16:54:59 +0200 | [diff] [blame] | 1530 | if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv)) |
| 1531 | last_update = &vm->last_update; |
| 1532 | else |
| 1533 | last_update = &bo_va->last_pt_update; |
| 1534 | |
Christian König | 3d7d4d3 | 2017-08-23 16:13:33 +0200 | [diff] [blame] | 1535 | if (!clear && bo_va->base.moved) { |
| 1536 | bo_va->base.moved = false; |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1537 | list_splice_init(&bo_va->valids, &bo_va->invalids); |
Christian König | 3d7d4d3 | 2017-08-23 16:13:33 +0200 | [diff] [blame] | 1538 | |
Christian König | cb7b6ec | 2017-08-15 17:08:12 +0200 | [diff] [blame] | 1539 | } else if (bo_va->cleared != clear) { |
| 1540 | list_splice_init(&bo_va->valids, &bo_va->invalids); |
Christian König | 3d7d4d3 | 2017-08-23 16:13:33 +0200 | [diff] [blame] | 1541 | } |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1542 | |
| 1543 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
Christian König | 457e0fe | 2017-08-22 12:50:46 +0200 | [diff] [blame] | 1544 | r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm, |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1545 | mapping, flags, nodes, |
Christian König | 4e55eb3 | 2017-09-11 16:54:59 +0200 | [diff] [blame] | 1546 | last_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1547 | if (r) |
| 1548 | return r; |
| 1549 | } |
| 1550 | |
Christian König | 68c6230 | 2017-07-11 17:23:29 +0200 | [diff] [blame] | 1551 | if (vm->use_cpu_for_update) { |
| 1552 | /* Flush HDP */ |
| 1553 | mb(); |
Christian König | 6988256 | 2018-01-19 14:17:40 +0100 | [diff] [blame] | 1554 | amdgpu_asic_flush_hdp(adev, NULL); |
Christian König | 68c6230 | 2017-07-11 17:23:29 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
Christian König | cb7b6ec | 2017-08-15 17:08:12 +0200 | [diff] [blame] | 1557 | spin_lock(&vm->status_lock); |
| 1558 | list_del_init(&bo_va->base.vm_status); |
| 1559 | spin_unlock(&vm->status_lock); |
| 1560 | |
| 1561 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
| 1562 | bo_va->cleared = clear; |
| 1563 | |
| 1564 | if (trace_amdgpu_vm_bo_mapping_enabled()) { |
| 1565 | list_for_each_entry(mapping, &bo_va->valids, list) |
| 1566 | trace_amdgpu_vm_bo_mapping(mapping); |
| 1567 | } |
| 1568 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | /** |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1573 | * amdgpu_vm_update_prt_state - update the global PRT state |
| 1574 | */ |
| 1575 | static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) |
| 1576 | { |
| 1577 | unsigned long flags; |
| 1578 | bool enable; |
| 1579 | |
| 1580 | spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1581 | enable = !!atomic_read(&adev->vm_manager.num_prt_users); |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 1582 | adev->gmc.gmc_funcs->set_prt(adev, enable); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1583 | spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); |
| 1584 | } |
| 1585 | |
| 1586 | /** |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1587 | * amdgpu_vm_prt_get - add a PRT user |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1588 | */ |
| 1589 | static void amdgpu_vm_prt_get(struct amdgpu_device *adev) |
| 1590 | { |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 1591 | if (!adev->gmc.gmc_funcs->set_prt) |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1592 | return; |
| 1593 | |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1594 | if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) |
| 1595 | amdgpu_vm_update_prt_state(adev); |
| 1596 | } |
| 1597 | |
| 1598 | /** |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1599 | * amdgpu_vm_prt_put - drop a PRT user |
| 1600 | */ |
| 1601 | static void amdgpu_vm_prt_put(struct amdgpu_device *adev) |
| 1602 | { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1603 | if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1604 | amdgpu_vm_update_prt_state(adev); |
| 1605 | } |
| 1606 | |
| 1607 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1608 | * amdgpu_vm_prt_cb - callback for updating the PRT status |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1609 | */ |
| 1610 | static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) |
| 1611 | { |
| 1612 | struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); |
| 1613 | |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1614 | amdgpu_vm_prt_put(cb->adev); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1615 | kfree(cb); |
| 1616 | } |
| 1617 | |
| 1618 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1619 | * amdgpu_vm_add_prt_cb - add callback for updating the PRT status |
| 1620 | */ |
| 1621 | static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, |
| 1622 | struct dma_fence *fence) |
| 1623 | { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1624 | struct amdgpu_prt_cb *cb; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1625 | |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 1626 | if (!adev->gmc.gmc_funcs->set_prt) |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1627 | return; |
| 1628 | |
| 1629 | cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1630 | if (!cb) { |
| 1631 | /* Last resort when we are OOM */ |
| 1632 | if (fence) |
| 1633 | dma_fence_wait(fence, false); |
| 1634 | |
Dan Carpenter | 486a68f | 2017-04-03 21:41:39 +0300 | [diff] [blame] | 1635 | amdgpu_vm_prt_put(adev); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1636 | } else { |
| 1637 | cb->adev = adev; |
| 1638 | if (!fence || dma_fence_add_callback(fence, &cb->cb, |
| 1639 | amdgpu_vm_prt_cb)) |
| 1640 | amdgpu_vm_prt_cb(fence, &cb->cb); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /** |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1645 | * amdgpu_vm_free_mapping - free a mapping |
| 1646 | * |
| 1647 | * @adev: amdgpu_device pointer |
| 1648 | * @vm: requested vm |
| 1649 | * @mapping: mapping to be freed |
| 1650 | * @fence: fence of the unmap operation |
| 1651 | * |
| 1652 | * Free a mapping and make sure we decrease the PRT usage count if applicable. |
| 1653 | */ |
| 1654 | static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, |
| 1655 | struct amdgpu_vm *vm, |
| 1656 | struct amdgpu_bo_va_mapping *mapping, |
| 1657 | struct dma_fence *fence) |
| 1658 | { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1659 | if (mapping->flags & AMDGPU_PTE_PRT) |
| 1660 | amdgpu_vm_add_prt_cb(adev, fence); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1661 | kfree(mapping); |
| 1662 | } |
| 1663 | |
| 1664 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1665 | * amdgpu_vm_prt_fini - finish all prt mappings |
| 1666 | * |
| 1667 | * @adev: amdgpu_device pointer |
| 1668 | * @vm: requested vm |
| 1669 | * |
| 1670 | * Register a cleanup callback to disable PRT support after VM dies. |
| 1671 | */ |
| 1672 | static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1673 | { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 1674 | struct reservation_object *resv = vm->root.base.bo->tbo.resv; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1675 | struct dma_fence *excl, **shared; |
| 1676 | unsigned i, shared_count; |
| 1677 | int r; |
| 1678 | |
| 1679 | r = reservation_object_get_fences_rcu(resv, &excl, |
| 1680 | &shared_count, &shared); |
| 1681 | if (r) { |
| 1682 | /* Not enough memory to grab the fence list, as last resort |
| 1683 | * block for all the fences to complete. |
| 1684 | */ |
| 1685 | reservation_object_wait_timeout_rcu(resv, true, false, |
| 1686 | MAX_SCHEDULE_TIMEOUT); |
| 1687 | return; |
| 1688 | } |
| 1689 | |
| 1690 | /* Add a callback for each fence in the reservation object */ |
| 1691 | amdgpu_vm_prt_get(adev); |
| 1692 | amdgpu_vm_add_prt_cb(adev, excl); |
| 1693 | |
| 1694 | for (i = 0; i < shared_count; ++i) { |
| 1695 | amdgpu_vm_prt_get(adev); |
| 1696 | amdgpu_vm_add_prt_cb(adev, shared[i]); |
| 1697 | } |
| 1698 | |
| 1699 | kfree(shared); |
| 1700 | } |
| 1701 | |
| 1702 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1703 | * amdgpu_vm_clear_freed - clear freed BOs in the PT |
| 1704 | * |
| 1705 | * @adev: amdgpu_device pointer |
| 1706 | * @vm: requested vm |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1707 | * @fence: optional resulting fence (unchanged if no work needed to be done |
| 1708 | * or if an error occurred) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1709 | * |
| 1710 | * Make sure all freed BOs are cleared in the PT. |
| 1711 | * Returns 0 for success. |
| 1712 | * |
| 1713 | * PTs have to be reserved and mutex must be locked! |
| 1714 | */ |
| 1715 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1716 | struct amdgpu_vm *vm, |
| 1717 | struct dma_fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1718 | { |
| 1719 | struct amdgpu_bo_va_mapping *mapping; |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 1720 | uint64_t init_pte_value = 0; |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1721 | struct dma_fence *f = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1722 | int r; |
| 1723 | |
| 1724 | while (!list_empty(&vm->freed)) { |
| 1725 | mapping = list_first_entry(&vm->freed, |
| 1726 | struct amdgpu_bo_va_mapping, list); |
| 1727 | list_del(&mapping->list); |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 1728 | |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 1729 | if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START) |
Yong Zhao | 6d16dac | 2017-08-31 15:55:00 -0400 | [diff] [blame] | 1730 | init_pte_value = AMDGPU_PTE_DEFAULT_ATC; |
Yong Zhao | 51ac7ee | 2017-07-27 12:48:22 -0400 | [diff] [blame] | 1731 | |
Christian König | 570144c | 2017-08-30 15:38:45 +0200 | [diff] [blame] | 1732 | r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm, |
Christian König | fc6aa33 | 2017-04-19 14:41:19 +0200 | [diff] [blame] | 1733 | mapping->start, mapping->last, |
Yong Zhao | 51ac7ee | 2017-07-27 12:48:22 -0400 | [diff] [blame] | 1734 | init_pte_value, 0, &f); |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1735 | amdgpu_vm_free_mapping(adev, vm, mapping, f); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1736 | if (r) { |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1737 | dma_fence_put(f); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1738 | return r; |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1739 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1740 | } |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1741 | |
| 1742 | if (fence && f) { |
| 1743 | dma_fence_put(*fence); |
| 1744 | *fence = f; |
| 1745 | } else { |
| 1746 | dma_fence_put(f); |
| 1747 | } |
| 1748 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1749 | return 0; |
| 1750 | |
| 1751 | } |
| 1752 | |
| 1753 | /** |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1754 | * amdgpu_vm_handle_moved - handle moved BOs in the PT |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1755 | * |
| 1756 | * @adev: amdgpu_device pointer |
| 1757 | * @vm: requested vm |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1758 | * @sync: sync object to add fences to |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1759 | * |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1760 | * Make sure all BOs which are moved are updated in the PTs. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1761 | * Returns 0 for success. |
| 1762 | * |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1763 | * PTs have to be reserved! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1764 | */ |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1765 | int amdgpu_vm_handle_moved(struct amdgpu_device *adev, |
Christian König | 4e55eb3 | 2017-09-11 16:54:59 +0200 | [diff] [blame] | 1766 | struct amdgpu_vm *vm) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1767 | { |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1768 | bool clear; |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 1769 | int r = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1770 | |
| 1771 | spin_lock(&vm->status_lock); |
Christian König | 27c7b9a | 2017-08-01 11:27:36 +0200 | [diff] [blame] | 1772 | while (!list_empty(&vm->moved)) { |
Christian König | 4e55eb3 | 2017-09-11 16:54:59 +0200 | [diff] [blame] | 1773 | struct amdgpu_bo_va *bo_va; |
Christian König | ec363e0 | 2017-09-01 20:34:27 +0200 | [diff] [blame] | 1774 | struct reservation_object *resv; |
Christian König | 4e55eb3 | 2017-09-11 16:54:59 +0200 | [diff] [blame] | 1775 | |
Christian König | 27c7b9a | 2017-08-01 11:27:36 +0200 | [diff] [blame] | 1776 | bo_va = list_first_entry(&vm->moved, |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1777 | struct amdgpu_bo_va, base.vm_status); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1778 | spin_unlock(&vm->status_lock); |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1779 | |
Christian König | ec363e0 | 2017-09-01 20:34:27 +0200 | [diff] [blame] | 1780 | resv = bo_va->base.bo->tbo.resv; |
| 1781 | |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1782 | /* Per VM BOs never need to bo cleared in the page tables */ |
Christian König | ec363e0 | 2017-09-01 20:34:27 +0200 | [diff] [blame] | 1783 | if (resv == vm->root.base.bo->tbo.resv) |
| 1784 | clear = false; |
| 1785 | /* Try to reserve the BO to avoid clearing its ptes */ |
Christian König | 9b8cad2 | 2018-01-03 13:36:22 +0100 | [diff] [blame] | 1786 | else if (!amdgpu_vm_debug && reservation_object_trylock(resv)) |
Christian König | ec363e0 | 2017-09-01 20:34:27 +0200 | [diff] [blame] | 1787 | clear = false; |
| 1788 | /* Somebody else is using the BO right now */ |
| 1789 | else |
| 1790 | clear = true; |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1791 | |
| 1792 | r = amdgpu_vm_bo_update(adev, bo_va, clear); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1793 | if (r) |
| 1794 | return r; |
| 1795 | |
Christian König | ec363e0 | 2017-09-01 20:34:27 +0200 | [diff] [blame] | 1796 | if (!clear && resv != vm->root.base.bo->tbo.resv) |
| 1797 | reservation_object_unlock(resv); |
| 1798 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1799 | spin_lock(&vm->status_lock); |
| 1800 | } |
| 1801 | spin_unlock(&vm->status_lock); |
| 1802 | |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 1803 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /** |
| 1807 | * amdgpu_vm_bo_add - add a bo to a specific vm |
| 1808 | * |
| 1809 | * @adev: amdgpu_device pointer |
| 1810 | * @vm: requested vm |
| 1811 | * @bo: amdgpu buffer object |
| 1812 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1813 | * Add @bo into the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1814 | * Add @bo to the list of bos associated with the vm |
| 1815 | * Returns newly added bo_va or NULL for failure |
| 1816 | * |
| 1817 | * Object has to be reserved! |
| 1818 | */ |
| 1819 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, |
| 1820 | struct amdgpu_vm *vm, |
| 1821 | struct amdgpu_bo *bo) |
| 1822 | { |
| 1823 | struct amdgpu_bo_va *bo_va; |
| 1824 | |
| 1825 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); |
| 1826 | if (bo_va == NULL) { |
| 1827 | return NULL; |
| 1828 | } |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1829 | bo_va->base.vm = vm; |
| 1830 | bo_va->base.bo = bo; |
| 1831 | INIT_LIST_HEAD(&bo_va->base.bo_list); |
| 1832 | INIT_LIST_HEAD(&bo_va->base.vm_status); |
| 1833 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1834 | bo_va->ref_count = 1; |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1835 | INIT_LIST_HEAD(&bo_va->valids); |
| 1836 | INIT_LIST_HEAD(&bo_va->invalids); |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1837 | |
Christian König | 727ffdf | 2017-12-22 17:13:03 +0100 | [diff] [blame] | 1838 | if (!bo) |
| 1839 | return bo_va; |
| 1840 | |
| 1841 | list_add_tail(&bo_va->base.bo_list, &bo->va); |
| 1842 | |
| 1843 | if (bo->tbo.resv != vm->root.base.bo->tbo.resv) |
| 1844 | return bo_va; |
| 1845 | |
| 1846 | if (bo->preferred_domains & |
| 1847 | amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)) |
| 1848 | return bo_va; |
| 1849 | |
| 1850 | /* |
| 1851 | * We checked all the prerequisites, but it looks like this per VM BO |
| 1852 | * is currently evicted. add the BO to the evicted list to make sure it |
| 1853 | * is validated on next VM use to avoid fault. |
| 1854 | * */ |
| 1855 | spin_lock(&vm->status_lock); |
| 1856 | list_move_tail(&bo_va->base.vm_status, &vm->evicted); |
| 1857 | spin_unlock(&vm->status_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1858 | |
| 1859 | return bo_va; |
| 1860 | } |
| 1861 | |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1862 | |
| 1863 | /** |
| 1864 | * amdgpu_vm_bo_insert_mapping - insert a new mapping |
| 1865 | * |
| 1866 | * @adev: amdgpu_device pointer |
| 1867 | * @bo_va: bo_va to store the address |
| 1868 | * @mapping: the mapping to insert |
| 1869 | * |
| 1870 | * Insert a new mapping into all structures. |
| 1871 | */ |
| 1872 | static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, |
| 1873 | struct amdgpu_bo_va *bo_va, |
| 1874 | struct amdgpu_bo_va_mapping *mapping) |
| 1875 | { |
| 1876 | struct amdgpu_vm *vm = bo_va->base.vm; |
| 1877 | struct amdgpu_bo *bo = bo_va->base.bo; |
| 1878 | |
Christian König | aebc5e6 | 2017-09-06 16:55:16 +0200 | [diff] [blame] | 1879 | mapping->bo_va = bo_va; |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1880 | list_add(&mapping->list, &bo_va->invalids); |
| 1881 | amdgpu_vm_it_insert(mapping, &vm->va); |
| 1882 | |
| 1883 | if (mapping->flags & AMDGPU_PTE_PRT) |
| 1884 | amdgpu_vm_prt_get(adev); |
| 1885 | |
| 1886 | if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) { |
| 1887 | spin_lock(&vm->status_lock); |
Christian König | 481c2e9 | 2017-09-01 14:46:19 +0200 | [diff] [blame] | 1888 | if (list_empty(&bo_va->base.vm_status)) |
| 1889 | list_add(&bo_va->base.vm_status, &vm->moved); |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1890 | spin_unlock(&vm->status_lock); |
| 1891 | } |
| 1892 | trace_amdgpu_vm_bo_map(bo_va, mapping); |
| 1893 | } |
| 1894 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1895 | /** |
| 1896 | * amdgpu_vm_bo_map - map bo inside a vm |
| 1897 | * |
| 1898 | * @adev: amdgpu_device pointer |
| 1899 | * @bo_va: bo_va to store the address |
| 1900 | * @saddr: where to map the BO |
| 1901 | * @offset: requested offset in the BO |
| 1902 | * @flags: attributes of pages (read/write/valid/etc.) |
| 1903 | * |
| 1904 | * Add a mapping of the BO at the specefied addr into the VM. |
| 1905 | * Returns 0 for success, error for failure. |
| 1906 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1907 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1908 | */ |
| 1909 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, |
| 1910 | struct amdgpu_bo_va *bo_va, |
| 1911 | uint64_t saddr, uint64_t offset, |
Christian König | 268c300 | 2017-01-18 14:49:43 +0100 | [diff] [blame] | 1912 | uint64_t size, uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1913 | { |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 1914 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1915 | struct amdgpu_bo *bo = bo_va->base.bo; |
| 1916 | struct amdgpu_vm *vm = bo_va->base.vm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1917 | uint64_t eaddr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1918 | |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1919 | /* validate the parameters */ |
| 1920 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1921 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1922 | return -EINVAL; |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1923 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1924 | /* make sure object fit at this offset */ |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1925 | eaddr = saddr + size - 1; |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1926 | if (saddr >= eaddr || |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1927 | (bo && offset + size > amdgpu_bo_size(bo))) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1928 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1929 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1930 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1931 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1932 | |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 1933 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
| 1934 | if (tmp) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1935 | /* bo and tmp overlap, invalid addr */ |
| 1936 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1937 | "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr, |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 1938 | tmp->start, tmp->last + 1); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 1939 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 1943 | if (!mapping) |
| 1944 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1945 | |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 1946 | mapping->start = saddr; |
| 1947 | mapping->last = eaddr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1948 | mapping->offset = offset; |
| 1949 | mapping->flags = flags; |
| 1950 | |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 1951 | amdgpu_vm_bo_insert_map(adev, bo_va, mapping); |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1952 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1953 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | /** |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 1957 | * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings |
| 1958 | * |
| 1959 | * @adev: amdgpu_device pointer |
| 1960 | * @bo_va: bo_va to store the address |
| 1961 | * @saddr: where to map the BO |
| 1962 | * @offset: requested offset in the BO |
| 1963 | * @flags: attributes of pages (read/write/valid/etc.) |
| 1964 | * |
| 1965 | * Add a mapping of the BO at the specefied addr into the VM. Replace existing |
| 1966 | * mappings as we do so. |
| 1967 | * Returns 0 for success, error for failure. |
| 1968 | * |
| 1969 | * Object has to be reserved and unreserved outside! |
| 1970 | */ |
| 1971 | int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, |
| 1972 | struct amdgpu_bo_va *bo_va, |
| 1973 | uint64_t saddr, uint64_t offset, |
| 1974 | uint64_t size, uint64_t flags) |
| 1975 | { |
| 1976 | struct amdgpu_bo_va_mapping *mapping; |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1977 | struct amdgpu_bo *bo = bo_va->base.bo; |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 1978 | uint64_t eaddr; |
| 1979 | int r; |
| 1980 | |
| 1981 | /* validate the parameters */ |
| 1982 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || |
| 1983 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
| 1984 | return -EINVAL; |
| 1985 | |
| 1986 | /* make sure object fit at this offset */ |
| 1987 | eaddr = saddr + size - 1; |
| 1988 | if (saddr >= eaddr || |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1989 | (bo && offset + size > amdgpu_bo_size(bo))) |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 1990 | return -EINVAL; |
| 1991 | |
| 1992 | /* Allocate all the needed memory */ |
| 1993 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); |
| 1994 | if (!mapping) |
| 1995 | return -ENOMEM; |
| 1996 | |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 1997 | r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size); |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 1998 | if (r) { |
| 1999 | kfree(mapping); |
| 2000 | return r; |
| 2001 | } |
| 2002 | |
| 2003 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 2004 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 2005 | |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2006 | mapping->start = saddr; |
| 2007 | mapping->last = eaddr; |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 2008 | mapping->offset = offset; |
| 2009 | mapping->flags = flags; |
| 2010 | |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 2011 | amdgpu_vm_bo_insert_map(adev, bo_va, mapping); |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 2012 | |
| 2013 | return 0; |
| 2014 | } |
| 2015 | |
| 2016 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2017 | * amdgpu_vm_bo_unmap - remove bo mapping from vm |
| 2018 | * |
| 2019 | * @adev: amdgpu_device pointer |
| 2020 | * @bo_va: bo_va to remove the address from |
| 2021 | * @saddr: where to the BO is mapped |
| 2022 | * |
| 2023 | * Remove a mapping of the BO at the specefied addr from the VM. |
| 2024 | * Returns 0 for success, error for failure. |
| 2025 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 2026 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2027 | */ |
| 2028 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, |
| 2029 | struct amdgpu_bo_va *bo_va, |
| 2030 | uint64_t saddr) |
| 2031 | { |
| 2032 | struct amdgpu_bo_va_mapping *mapping; |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2033 | struct amdgpu_vm *vm = bo_va->base.vm; |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2034 | bool valid = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2035 | |
Christian König | 6c7fc50 | 2015-06-05 20:56:17 +0200 | [diff] [blame] | 2036 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 2037 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2038 | list_for_each_entry(mapping, &bo_va->valids, list) { |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2039 | if (mapping->start == saddr) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2040 | break; |
| 2041 | } |
| 2042 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2043 | if (&mapping->list == &bo_va->valids) { |
| 2044 | valid = false; |
| 2045 | |
| 2046 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2047 | if (mapping->start == saddr) |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2048 | break; |
| 2049 | } |
| 2050 | |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 2051 | if (&mapping->list == &bo_va->invalids) |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2052 | return -ENOENT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2053 | } |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 2054 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2055 | list_del(&mapping->list); |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2056 | amdgpu_vm_it_remove(mapping, &vm->va); |
Christian König | aebc5e6 | 2017-09-06 16:55:16 +0200 | [diff] [blame] | 2057 | mapping->bo_va = NULL; |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 2058 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2059 | |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 2060 | if (valid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2061 | list_add(&mapping->list, &vm->freed); |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 2062 | else |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 2063 | amdgpu_vm_free_mapping(adev, vm, mapping, |
| 2064 | bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2065 | |
| 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2070 | * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range |
| 2071 | * |
| 2072 | * @adev: amdgpu_device pointer |
| 2073 | * @vm: VM structure to use |
| 2074 | * @saddr: start of the range |
| 2075 | * @size: size of the range |
| 2076 | * |
| 2077 | * Remove all mappings in a range, split them as appropriate. |
| 2078 | * Returns 0 for success, error for failure. |
| 2079 | */ |
| 2080 | int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, |
| 2081 | struct amdgpu_vm *vm, |
| 2082 | uint64_t saddr, uint64_t size) |
| 2083 | { |
| 2084 | struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2085 | LIST_HEAD(removed); |
| 2086 | uint64_t eaddr; |
| 2087 | |
| 2088 | eaddr = saddr + size - 1; |
| 2089 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 2090 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 2091 | |
| 2092 | /* Allocate all the needed memory */ |
| 2093 | before = kzalloc(sizeof(*before), GFP_KERNEL); |
| 2094 | if (!before) |
| 2095 | return -ENOMEM; |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 2096 | INIT_LIST_HEAD(&before->list); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2097 | |
| 2098 | after = kzalloc(sizeof(*after), GFP_KERNEL); |
| 2099 | if (!after) { |
| 2100 | kfree(before); |
| 2101 | return -ENOMEM; |
| 2102 | } |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 2103 | INIT_LIST_HEAD(&after->list); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2104 | |
| 2105 | /* Now gather all removed mappings */ |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2106 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
| 2107 | while (tmp) { |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2108 | /* Remember mapping split at the start */ |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2109 | if (tmp->start < saddr) { |
| 2110 | before->start = tmp->start; |
| 2111 | before->last = saddr - 1; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2112 | before->offset = tmp->offset; |
| 2113 | before->flags = tmp->flags; |
| 2114 | list_add(&before->list, &tmp->list); |
| 2115 | } |
| 2116 | |
| 2117 | /* Remember mapping split at the end */ |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2118 | if (tmp->last > eaddr) { |
| 2119 | after->start = eaddr + 1; |
| 2120 | after->last = tmp->last; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2121 | after->offset = tmp->offset; |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2122 | after->offset += after->start - tmp->start; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2123 | after->flags = tmp->flags; |
| 2124 | list_add(&after->list, &tmp->list); |
| 2125 | } |
| 2126 | |
| 2127 | list_del(&tmp->list); |
| 2128 | list_add(&tmp->list, &removed); |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2129 | |
| 2130 | tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | /* And free them up */ |
| 2134 | list_for_each_entry_safe(tmp, next, &removed, list) { |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2135 | amdgpu_vm_it_remove(tmp, &vm->va); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2136 | list_del(&tmp->list); |
| 2137 | |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2138 | if (tmp->start < saddr) |
| 2139 | tmp->start = saddr; |
| 2140 | if (tmp->last > eaddr) |
| 2141 | tmp->last = eaddr; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2142 | |
Christian König | aebc5e6 | 2017-09-06 16:55:16 +0200 | [diff] [blame] | 2143 | tmp->bo_va = NULL; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2144 | list_add(&tmp->list, &vm->freed); |
| 2145 | trace_amdgpu_vm_bo_unmap(NULL, tmp); |
| 2146 | } |
| 2147 | |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 2148 | /* Insert partial mapping before the range */ |
| 2149 | if (!list_empty(&before->list)) { |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2150 | amdgpu_vm_it_insert(before, &vm->va); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2151 | if (before->flags & AMDGPU_PTE_PRT) |
| 2152 | amdgpu_vm_prt_get(adev); |
| 2153 | } else { |
| 2154 | kfree(before); |
| 2155 | } |
| 2156 | |
| 2157 | /* Insert partial mapping after the range */ |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 2158 | if (!list_empty(&after->list)) { |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2159 | amdgpu_vm_it_insert(after, &vm->va); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 2160 | if (after->flags & AMDGPU_PTE_PRT) |
| 2161 | amdgpu_vm_prt_get(adev); |
| 2162 | } else { |
| 2163 | kfree(after); |
| 2164 | } |
| 2165 | |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | /** |
Christian König | aebc5e6 | 2017-09-06 16:55:16 +0200 | [diff] [blame] | 2170 | * amdgpu_vm_bo_lookup_mapping - find mapping by address |
| 2171 | * |
| 2172 | * @vm: the requested VM |
| 2173 | * |
| 2174 | * Find a mapping by it's address. |
| 2175 | */ |
| 2176 | struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, |
| 2177 | uint64_t addr) |
| 2178 | { |
| 2179 | return amdgpu_vm_it_iter_first(&vm->va, addr, addr); |
| 2180 | } |
| 2181 | |
| 2182 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2183 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm |
| 2184 | * |
| 2185 | * @adev: amdgpu_device pointer |
| 2186 | * @bo_va: requested bo_va |
| 2187 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 2188 | * Remove @bo_va->bo from the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2189 | * |
| 2190 | * Object have to be reserved! |
| 2191 | */ |
| 2192 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, |
| 2193 | struct amdgpu_bo_va *bo_va) |
| 2194 | { |
| 2195 | struct amdgpu_bo_va_mapping *mapping, *next; |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2196 | struct amdgpu_vm *vm = bo_va->base.vm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2197 | |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2198 | list_del(&bo_va->base.bo_list); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2199 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2200 | spin_lock(&vm->status_lock); |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2201 | list_del(&bo_va->base.vm_status); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2202 | spin_unlock(&vm->status_lock); |
| 2203 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2204 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2205 | list_del(&mapping->list); |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2206 | amdgpu_vm_it_remove(mapping, &vm->va); |
Christian König | aebc5e6 | 2017-09-06 16:55:16 +0200 | [diff] [blame] | 2207 | mapping->bo_va = NULL; |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 2208 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2209 | list_add(&mapping->list, &vm->freed); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2210 | } |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2211 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { |
| 2212 | list_del(&mapping->list); |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2213 | amdgpu_vm_it_remove(mapping, &vm->va); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 2214 | amdgpu_vm_free_mapping(adev, vm, mapping, |
| 2215 | bo_va->last_pt_update); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 2216 | } |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 2217 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2218 | dma_fence_put(bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2219 | kfree(bo_va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | /** |
| 2223 | * amdgpu_vm_bo_invalidate - mark the bo as invalid |
| 2224 | * |
| 2225 | * @adev: amdgpu_device pointer |
| 2226 | * @vm: requested vm |
| 2227 | * @bo: amdgpu buffer object |
| 2228 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 2229 | * Mark @bo as invalid. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2230 | */ |
| 2231 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2232 | struct amdgpu_bo *bo, bool evicted) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2233 | { |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2234 | struct amdgpu_vm_bo_base *bo_base; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2235 | |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2236 | list_for_each_entry(bo_base, &bo->va, bo_list) { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2237 | struct amdgpu_vm *vm = bo_base->vm; |
| 2238 | |
Christian König | 3d7d4d3 | 2017-08-23 16:13:33 +0200 | [diff] [blame] | 2239 | bo_base->moved = true; |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2240 | if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) { |
| 2241 | spin_lock(&bo_base->vm->status_lock); |
Christian König | 73fb16e | 2017-08-16 11:13:48 +0200 | [diff] [blame] | 2242 | if (bo->tbo.type == ttm_bo_type_kernel) |
| 2243 | list_move(&bo_base->vm_status, &vm->evicted); |
| 2244 | else |
| 2245 | list_move_tail(&bo_base->vm_status, |
| 2246 | &vm->evicted); |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2247 | spin_unlock(&bo_base->vm->status_lock); |
| 2248 | continue; |
| 2249 | } |
| 2250 | |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 2251 | if (bo->tbo.type == ttm_bo_type_kernel) { |
| 2252 | spin_lock(&bo_base->vm->status_lock); |
| 2253 | if (list_empty(&bo_base->vm_status)) |
| 2254 | list_add(&bo_base->vm_status, &vm->relocated); |
| 2255 | spin_unlock(&bo_base->vm->status_lock); |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2256 | continue; |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 2257 | } |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2258 | |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2259 | spin_lock(&bo_base->vm->status_lock); |
| 2260 | if (list_empty(&bo_base->vm_status)) |
Christian König | 481c2e9 | 2017-09-01 14:46:19 +0200 | [diff] [blame] | 2261 | list_add(&bo_base->vm_status, &vm->moved); |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 2262 | spin_unlock(&bo_base->vm->status_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2263 | } |
| 2264 | } |
| 2265 | |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 2266 | static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) |
| 2267 | { |
| 2268 | /* Total bits covered by PD + PTs */ |
| 2269 | unsigned bits = ilog2(vm_size) + 18; |
| 2270 | |
| 2271 | /* Make sure the PD is 4K in size up to 8GB address space. |
| 2272 | Above that split equal between PD and PTs */ |
| 2273 | if (vm_size <= 8) |
| 2274 | return (bits - 9); |
| 2275 | else |
| 2276 | return ((bits + 3) / 2); |
| 2277 | } |
| 2278 | |
| 2279 | /** |
Roger He | d07f14b | 2017-08-15 16:05:59 +0800 | [diff] [blame] | 2280 | * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 2281 | * |
| 2282 | * @adev: amdgpu_device pointer |
| 2283 | * @vm_size: the default vm size if it's set auto |
| 2284 | */ |
Christian König | fdd5faa | 2017-11-04 16:51:44 +0100 | [diff] [blame] | 2285 | void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size, |
Christian König | f336812 | 2017-11-23 12:57:18 +0100 | [diff] [blame] | 2286 | uint32_t fragment_size_default, unsigned max_level, |
| 2287 | unsigned max_bits) |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 2288 | { |
Christian König | 36539dc | 2017-11-23 11:16:05 +0100 | [diff] [blame] | 2289 | uint64_t tmp; |
| 2290 | |
| 2291 | /* adjust vm size first */ |
Christian König | f336812 | 2017-11-23 12:57:18 +0100 | [diff] [blame] | 2292 | if (amdgpu_vm_size != -1) { |
| 2293 | unsigned max_size = 1 << (max_bits - 30); |
| 2294 | |
Christian König | fdd5faa | 2017-11-04 16:51:44 +0100 | [diff] [blame] | 2295 | vm_size = amdgpu_vm_size; |
Christian König | f336812 | 2017-11-23 12:57:18 +0100 | [diff] [blame] | 2296 | if (vm_size > max_size) { |
| 2297 | dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", |
| 2298 | amdgpu_vm_size, max_size); |
| 2299 | vm_size = max_size; |
| 2300 | } |
| 2301 | } |
Christian König | fdd5faa | 2017-11-04 16:51:44 +0100 | [diff] [blame] | 2302 | |
| 2303 | adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; |
Christian König | 36539dc | 2017-11-23 11:16:05 +0100 | [diff] [blame] | 2304 | |
| 2305 | tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); |
Christian König | 9748912 | 2017-11-27 16:22:05 +0100 | [diff] [blame] | 2306 | if (amdgpu_vm_block_size != -1) |
| 2307 | tmp >>= amdgpu_vm_block_size - 9; |
Christian König | 36539dc | 2017-11-23 11:16:05 +0100 | [diff] [blame] | 2308 | tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; |
| 2309 | adev->vm_manager.num_level = min(max_level, (unsigned)tmp); |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 2310 | switch (adev->vm_manager.num_level) { |
| 2311 | case 3: |
| 2312 | adev->vm_manager.root_level = AMDGPU_VM_PDB2; |
| 2313 | break; |
| 2314 | case 2: |
| 2315 | adev->vm_manager.root_level = AMDGPU_VM_PDB1; |
| 2316 | break; |
| 2317 | case 1: |
| 2318 | adev->vm_manager.root_level = AMDGPU_VM_PDB0; |
| 2319 | break; |
| 2320 | default: |
| 2321 | dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); |
| 2322 | } |
Christian König | b38f41e | 2017-11-22 17:00:35 +0100 | [diff] [blame] | 2323 | /* block size depends on vm size and hw setup*/ |
Christian König | 9748912 | 2017-11-27 16:22:05 +0100 | [diff] [blame] | 2324 | if (amdgpu_vm_block_size != -1) |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 2325 | adev->vm_manager.block_size = |
Christian König | 9748912 | 2017-11-27 16:22:05 +0100 | [diff] [blame] | 2326 | min((unsigned)amdgpu_vm_block_size, max_bits |
| 2327 | - AMDGPU_GPU_PAGE_SHIFT |
| 2328 | - 9 * adev->vm_manager.num_level); |
| 2329 | else if (adev->vm_manager.num_level > 1) |
| 2330 | adev->vm_manager.block_size = 9; |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 2331 | else |
Christian König | 9748912 | 2017-11-27 16:22:05 +0100 | [diff] [blame] | 2332 | adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 2333 | |
Christian König | b38f41e | 2017-11-22 17:00:35 +0100 | [diff] [blame] | 2334 | if (amdgpu_vm_fragment_size == -1) |
| 2335 | adev->vm_manager.fragment_size = fragment_size_default; |
| 2336 | else |
| 2337 | adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; |
Roger He | d07f14b | 2017-08-15 16:05:59 +0800 | [diff] [blame] | 2338 | |
Christian König | 36539dc | 2017-11-23 11:16:05 +0100 | [diff] [blame] | 2339 | DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", |
| 2340 | vm_size, adev->vm_manager.num_level + 1, |
| 2341 | adev->vm_manager.block_size, |
Christian König | fdd5faa | 2017-11-04 16:51:44 +0100 | [diff] [blame] | 2342 | adev->vm_manager.fragment_size); |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 2343 | } |
| 2344 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2345 | /** |
| 2346 | * amdgpu_vm_init - initialize a vm instance |
| 2347 | * |
| 2348 | * @adev: amdgpu_device pointer |
| 2349 | * @vm: requested vm |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 2350 | * @vm_context: Indicates if it GFX or Compute context |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2351 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 2352 | * Init @vm fields. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2353 | */ |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 2354 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
Felix Kuehling | 0220844 | 2017-08-25 20:40:26 -0400 | [diff] [blame] | 2355 | int vm_context, unsigned int pasid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2356 | { |
| 2357 | const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, |
Zhang, Jerry | 36b32a6 | 2017-03-29 16:08:32 +0800 | [diff] [blame] | 2358 | AMDGPU_VM_PTE_COUNT(adev) * 8); |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2359 | unsigned ring_instance; |
| 2360 | struct amdgpu_ring *ring; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 2361 | struct drm_sched_rq *rq; |
Christian König | d3aab67 | 2018-01-24 14:57:02 +0100 | [diff] [blame] | 2362 | unsigned long size; |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2363 | uint64_t flags; |
Chunming Zhou | 36bbf3b | 2017-04-20 16:17:34 +0800 | [diff] [blame] | 2364 | int r, i; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2365 | |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 2366 | vm->va = RB_ROOT_CACHED; |
Chunming Zhou | 36bbf3b | 2017-04-20 16:17:34 +0800 | [diff] [blame] | 2367 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
| 2368 | vm->reserved_vmid[i] = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2369 | spin_lock_init(&vm->status_lock); |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2370 | INIT_LIST_HEAD(&vm->evicted); |
Christian König | ea09729 | 2017-08-09 14:15:46 +0200 | [diff] [blame] | 2371 | INIT_LIST_HEAD(&vm->relocated); |
Christian König | 27c7b9a | 2017-08-01 11:27:36 +0200 | [diff] [blame] | 2372 | INIT_LIST_HEAD(&vm->moved); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2373 | INIT_LIST_HEAD(&vm->freed); |
Christian König | 2025021 | 2016-03-08 17:58:35 +0100 | [diff] [blame] | 2374 | |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2375 | /* create scheduler entity for page table updates */ |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2376 | |
| 2377 | ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring); |
| 2378 | ring_instance %= adev->vm_manager.vm_pte_num_rings; |
| 2379 | ring = adev->vm_manager.vm_pte_rings[ring_instance]; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 2380 | rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL]; |
| 2381 | r = drm_sched_entity_init(&ring->sched, &vm->entity, |
Monk Liu | b3eebe3 | 2017-10-23 12:23:29 +0800 | [diff] [blame] | 2382 | rq, amdgpu_sched_jobs, NULL); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2383 | if (r) |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2384 | return r; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2385 | |
Yong Zhao | 51ac7ee | 2017-07-27 12:48:22 -0400 | [diff] [blame] | 2386 | vm->pte_support_ats = false; |
| 2387 | |
| 2388 | if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) { |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 2389 | vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & |
| 2390 | AMDGPU_VM_USE_CPU_FOR_COMPUTE); |
Yong Zhao | 51ac7ee | 2017-07-27 12:48:22 -0400 | [diff] [blame] | 2391 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2392 | if (adev->asic_type == CHIP_RAVEN) |
Yong Zhao | 51ac7ee | 2017-07-27 12:48:22 -0400 | [diff] [blame] | 2393 | vm->pte_support_ats = true; |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2394 | } else { |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 2395 | vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & |
| 2396 | AMDGPU_VM_USE_CPU_FOR_GFX); |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2397 | } |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 2398 | DRM_DEBUG_DRIVER("VM update mode is %s\n", |
| 2399 | vm->use_cpu_for_update ? "CPU" : "SDMA"); |
| 2400 | WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)), |
| 2401 | "CPU update of VM recommended only for large BAR system\n"); |
Christian König | d588451 | 2017-09-08 14:09:41 +0200 | [diff] [blame] | 2402 | vm->last_update = NULL; |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 2403 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2404 | flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
Harish Kasiviswanathan | 3c82417 | 2017-05-11 15:50:08 -0400 | [diff] [blame] | 2405 | if (vm->use_cpu_for_update) |
| 2406 | flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; |
| 2407 | else |
| 2408 | flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
| 2409 | AMDGPU_GEM_CREATE_SHADOW); |
| 2410 | |
Christian König | d3aab67 | 2018-01-24 14:57:02 +0100 | [diff] [blame] | 2411 | size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level); |
Christian König | eab3de2 | 2018-03-14 14:48:17 -0500 | [diff] [blame] | 2412 | r = amdgpu_bo_create(adev, size, align, AMDGPU_GEM_DOMAIN_VRAM, flags, |
| 2413 | ttm_bo_type_kernel, NULL, &vm->root.base.bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2414 | if (r) |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2415 | goto error_free_sched_entity; |
| 2416 | |
Christian König | d3aab67 | 2018-01-24 14:57:02 +0100 | [diff] [blame] | 2417 | r = amdgpu_bo_reserve(vm->root.base.bo, true); |
| 2418 | if (r) |
| 2419 | goto error_free_root; |
| 2420 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2421 | r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo, |
Christian König | 4584312 | 2018-01-25 18:36:15 +0100 | [diff] [blame] | 2422 | adev->vm_manager.root_level, |
| 2423 | vm->pte_support_ats); |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2424 | if (r) |
| 2425 | goto error_unreserve; |
| 2426 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2427 | vm->root.base.vm = vm; |
| 2428 | list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va); |
Christian König | d3aab67 | 2018-01-24 14:57:02 +0100 | [diff] [blame] | 2429 | list_add_tail(&vm->root.base.vm_status, &vm->evicted); |
| 2430 | amdgpu_bo_unreserve(vm->root.base.bo); |
Christian König | 0a096fb | 2017-07-12 10:01:48 +0200 | [diff] [blame] | 2431 | |
Felix Kuehling | 0220844 | 2017-08-25 20:40:26 -0400 | [diff] [blame] | 2432 | if (pasid) { |
| 2433 | unsigned long flags; |
| 2434 | |
| 2435 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); |
| 2436 | r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, |
| 2437 | GFP_ATOMIC); |
| 2438 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); |
| 2439 | if (r < 0) |
| 2440 | goto error_free_root; |
| 2441 | |
| 2442 | vm->pasid = pasid; |
| 2443 | } |
| 2444 | |
Felix Kuehling | a2f1482 | 2017-08-26 02:43:06 -0400 | [diff] [blame] | 2445 | INIT_KFIFO(vm->faults); |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2446 | vm->fault_credit = 16; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2447 | |
| 2448 | return 0; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2449 | |
Christian König | 13307f7 | 2018-01-24 17:19:04 +0100 | [diff] [blame] | 2450 | error_unreserve: |
| 2451 | amdgpu_bo_unreserve(vm->root.base.bo); |
| 2452 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 2453 | error_free_root: |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 2454 | amdgpu_bo_unref(&vm->root.base.bo->shadow); |
| 2455 | amdgpu_bo_unref(&vm->root.base.bo); |
| 2456 | vm->root.base.bo = NULL; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2457 | |
| 2458 | error_free_sched_entity: |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 2459 | drm_sched_entity_fini(&ring->sched, &vm->entity); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2460 | |
| 2461 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | /** |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2465 | * amdgpu_vm_free_levels - free PD/PT levels |
| 2466 | * |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 2467 | * @adev: amdgpu device structure |
| 2468 | * @parent: PD/PT starting level to free |
| 2469 | * @level: level of parent structure |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2470 | * |
| 2471 | * Free the page directory or page table level and all sub levels. |
| 2472 | */ |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 2473 | static void amdgpu_vm_free_levels(struct amdgpu_device *adev, |
| 2474 | struct amdgpu_vm_pt *parent, |
| 2475 | unsigned level) |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2476 | { |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 2477 | unsigned i, num_entries = amdgpu_vm_num_entries(adev, level); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2478 | |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 2479 | if (parent->base.bo) { |
| 2480 | list_del(&parent->base.bo_list); |
| 2481 | list_del(&parent->base.vm_status); |
| 2482 | amdgpu_bo_unref(&parent->base.bo->shadow); |
| 2483 | amdgpu_bo_unref(&parent->base.bo); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2484 | } |
| 2485 | |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 2486 | if (parent->entries) |
| 2487 | for (i = 0; i < num_entries; i++) |
| 2488 | amdgpu_vm_free_levels(adev, &parent->entries[i], |
| 2489 | level + 1); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2490 | |
Christian König | 8f19cd7 | 2017-11-30 15:28:03 +0100 | [diff] [blame] | 2491 | kvfree(parent->entries); |
Christian König | f566ceb | 2016-10-27 20:04:38 +0200 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2495 | * amdgpu_vm_fini - tear down a vm instance |
| 2496 | * |
| 2497 | * @adev: amdgpu_device pointer |
| 2498 | * @vm: requested vm |
| 2499 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 2500 | * Tear down @vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2501 | * Unbind the VM and remove all bos from the vm bo list |
| 2502 | */ |
| 2503 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 2504 | { |
| 2505 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 2506 | bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; |
Christian König | 2642cf1 | 2017-10-13 17:24:31 +0200 | [diff] [blame] | 2507 | struct amdgpu_bo *root; |
Felix Kuehling | a2f1482 | 2017-08-26 02:43:06 -0400 | [diff] [blame] | 2508 | u64 fault; |
Christian König | 2642cf1 | 2017-10-13 17:24:31 +0200 | [diff] [blame] | 2509 | int i, r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2510 | |
Felix Kuehling | a2f1482 | 2017-08-26 02:43:06 -0400 | [diff] [blame] | 2511 | /* Clear pending page faults from IH when the VM is destroyed */ |
| 2512 | while (kfifo_get(&vm->faults, &fault)) |
| 2513 | amdgpu_ih_clear_fault(adev, fault); |
| 2514 | |
Felix Kuehling | 0220844 | 2017-08-25 20:40:26 -0400 | [diff] [blame] | 2515 | if (vm->pasid) { |
| 2516 | unsigned long flags; |
| 2517 | |
| 2518 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); |
| 2519 | idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); |
| 2520 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); |
| 2521 | } |
| 2522 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 2523 | drm_sched_entity_fini(vm->entity.sched, &vm->entity); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 2524 | |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 2525 | if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2526 | dev_err(adev->dev, "still active bo inside vm\n"); |
| 2527 | } |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 2528 | rbtree_postorder_for_each_entry_safe(mapping, tmp, |
| 2529 | &vm->va.rb_root, rb) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2530 | list_del(&mapping->list); |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 2531 | amdgpu_vm_it_remove(mapping, &vm->va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2532 | kfree(mapping); |
| 2533 | } |
| 2534 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 2535 | if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 2536 | amdgpu_vm_prt_fini(adev, vm); |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 2537 | prt_fini_needed = false; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 2538 | } |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 2539 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2540 | list_del(&mapping->list); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 2541 | amdgpu_vm_free_mapping(adev, vm, mapping, NULL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2542 | } |
| 2543 | |
Christian König | 2642cf1 | 2017-10-13 17:24:31 +0200 | [diff] [blame] | 2544 | root = amdgpu_bo_ref(vm->root.base.bo); |
| 2545 | r = amdgpu_bo_reserve(root, true); |
| 2546 | if (r) { |
| 2547 | dev_err(adev->dev, "Leaking page tables because BO reservation failed\n"); |
| 2548 | } else { |
Chunming Zhou | 196f748 | 2017-12-13 14:22:54 +0800 | [diff] [blame] | 2549 | amdgpu_vm_free_levels(adev, &vm->root, |
| 2550 | adev->vm_manager.root_level); |
Christian König | 2642cf1 | 2017-10-13 17:24:31 +0200 | [diff] [blame] | 2551 | amdgpu_bo_unreserve(root); |
| 2552 | } |
| 2553 | amdgpu_bo_unref(&root); |
Christian König | d588451 | 2017-09-08 14:09:41 +0200 | [diff] [blame] | 2554 | dma_fence_put(vm->last_update); |
Chunming Zhou | 1e9ef26 | 2017-04-20 16:18:48 +0800 | [diff] [blame] | 2555 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 2556 | amdgpu_vmid_free_reserved(adev, vm, i); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2557 | } |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2558 | |
| 2559 | /** |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2560 | * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID |
| 2561 | * |
| 2562 | * @adev: amdgpu_device pointer |
| 2563 | * @pasid: PASID do identify the VM |
| 2564 | * |
| 2565 | * This function is expected to be called in interrupt context. Returns |
| 2566 | * true if there was fault credit, false otherwise |
| 2567 | */ |
| 2568 | bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev, |
| 2569 | unsigned int pasid) |
| 2570 | { |
| 2571 | struct amdgpu_vm *vm; |
| 2572 | |
| 2573 | spin_lock(&adev->vm_manager.pasid_lock); |
| 2574 | vm = idr_find(&adev->vm_manager.pasid_idr, pasid); |
Christian König | d958939 | 2018-01-09 19:18:59 +0100 | [diff] [blame] | 2575 | if (!vm) { |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2576 | /* VM not found, can't track fault credit */ |
Christian König | d958939 | 2018-01-09 19:18:59 +0100 | [diff] [blame] | 2577 | spin_unlock(&adev->vm_manager.pasid_lock); |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2578 | return true; |
Christian König | d958939 | 2018-01-09 19:18:59 +0100 | [diff] [blame] | 2579 | } |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2580 | |
| 2581 | /* No lock needed. only accessed by IRQ handler */ |
Christian König | d958939 | 2018-01-09 19:18:59 +0100 | [diff] [blame] | 2582 | if (!vm->fault_credit) { |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2583 | /* Too many faults in this VM */ |
Christian König | d958939 | 2018-01-09 19:18:59 +0100 | [diff] [blame] | 2584 | spin_unlock(&adev->vm_manager.pasid_lock); |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2585 | return false; |
Christian König | d958939 | 2018-01-09 19:18:59 +0100 | [diff] [blame] | 2586 | } |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2587 | |
| 2588 | vm->fault_credit--; |
Christian König | d958939 | 2018-01-09 19:18:59 +0100 | [diff] [blame] | 2589 | spin_unlock(&adev->vm_manager.pasid_lock); |
Felix Kuehling | c98171c | 2017-09-21 16:26:41 -0400 | [diff] [blame] | 2590 | return true; |
| 2591 | } |
| 2592 | |
| 2593 | /** |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2594 | * amdgpu_vm_manager_init - init the VM manager |
| 2595 | * |
| 2596 | * @adev: amdgpu_device pointer |
| 2597 | * |
| 2598 | * Initialize the VM manager structures |
| 2599 | */ |
| 2600 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) |
| 2601 | { |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 2602 | unsigned i; |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2603 | |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 2604 | amdgpu_vmid_mgr_init(adev); |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2605 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2606 | adev->vm_manager.fence_context = |
| 2607 | dma_fence_context_alloc(AMDGPU_MAX_RINGS); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 2608 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 2609 | adev->vm_manager.seqno[i] = 0; |
| 2610 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2611 | atomic_set(&adev->vm_manager.vm_pte_next_ring, 0); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 2612 | spin_lock_init(&adev->vm_manager.prt_lock); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 2613 | atomic_set(&adev->vm_manager.num_prt_users, 0); |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 2614 | |
| 2615 | /* If not overridden by the user, by default, only in large BAR systems |
| 2616 | * Compute VM tables will be updated by CPU |
| 2617 | */ |
| 2618 | #ifdef CONFIG_X86_64 |
| 2619 | if (amdgpu_vm_update_mode == -1) { |
| 2620 | if (amdgpu_vm_is_large_bar(adev)) |
| 2621 | adev->vm_manager.vm_update_mode = |
| 2622 | AMDGPU_VM_USE_CPU_FOR_COMPUTE; |
| 2623 | else |
| 2624 | adev->vm_manager.vm_update_mode = 0; |
| 2625 | } else |
| 2626 | adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; |
| 2627 | #else |
| 2628 | adev->vm_manager.vm_update_mode = 0; |
| 2629 | #endif |
| 2630 | |
Felix Kuehling | 0220844 | 2017-08-25 20:40:26 -0400 | [diff] [blame] | 2631 | idr_init(&adev->vm_manager.pasid_idr); |
| 2632 | spin_lock_init(&adev->vm_manager.pasid_lock); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | /** |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2636 | * amdgpu_vm_manager_fini - cleanup VM manager |
| 2637 | * |
| 2638 | * @adev: amdgpu_device pointer |
| 2639 | * |
| 2640 | * Cleanup the VM manager and free resources. |
| 2641 | */ |
| 2642 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) |
| 2643 | { |
Felix Kuehling | 0220844 | 2017-08-25 20:40:26 -0400 | [diff] [blame] | 2644 | WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr)); |
| 2645 | idr_destroy(&adev->vm_manager.pasid_idr); |
| 2646 | |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 2647 | amdgpu_vmid_mgr_fini(adev); |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2648 | } |
Chunming Zhou | cfbcacf | 2017-04-24 11:09:04 +0800 | [diff] [blame] | 2649 | |
| 2650 | int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) |
| 2651 | { |
| 2652 | union drm_amdgpu_vm *args = data; |
Chunming Zhou | 1e9ef26 | 2017-04-20 16:18:48 +0800 | [diff] [blame] | 2653 | struct amdgpu_device *adev = dev->dev_private; |
| 2654 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
| 2655 | int r; |
Chunming Zhou | cfbcacf | 2017-04-24 11:09:04 +0800 | [diff] [blame] | 2656 | |
| 2657 | switch (args->in.op) { |
| 2658 | case AMDGPU_VM_OP_RESERVE_VMID: |
Chunming Zhou | 1e9ef26 | 2017-04-20 16:18:48 +0800 | [diff] [blame] | 2659 | /* current, we only have requirement to reserve vmid from gfxhub */ |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 2660 | r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB); |
Chunming Zhou | 1e9ef26 | 2017-04-20 16:18:48 +0800 | [diff] [blame] | 2661 | if (r) |
| 2662 | return r; |
| 2663 | break; |
Chunming Zhou | cfbcacf | 2017-04-24 11:09:04 +0800 | [diff] [blame] | 2664 | case AMDGPU_VM_OP_UNRESERVE_VMID: |
Christian König | 620f774 | 2017-12-18 16:53:03 +0100 | [diff] [blame] | 2665 | amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB); |
Chunming Zhou | cfbcacf | 2017-04-24 11:09:04 +0800 | [diff] [blame] | 2666 | break; |
| 2667 | default: |
| 2668 | return -EINVAL; |
| 2669 | } |
| 2670 | |
| 2671 | return 0; |
| 2672 | } |