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