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> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 29 | #include <drm/drmP.h> |
| 30 | #include <drm/amdgpu_drm.h> |
| 31 | #include "amdgpu.h" |
| 32 | #include "amdgpu_trace.h" |
| 33 | |
| 34 | /* |
| 35 | * GPUVM |
| 36 | * GPUVM is similar to the legacy gart on older asics, however |
| 37 | * rather than there being a single global gart table |
| 38 | * for the entire GPU, there are multiple VM page tables active |
| 39 | * at any given time. The VM page tables can contain a mix |
| 40 | * vram pages and system memory pages and system memory pages |
| 41 | * can be mapped as snooped (cached system pages) or unsnooped |
| 42 | * (uncached system pages). |
| 43 | * Each VM has an ID associated with it and there is a page table |
| 44 | * associated with each VMID. When execting a command buffer, |
| 45 | * the kernel tells the the ring what VMID to use for that command |
| 46 | * buffer. VMIDs are allocated dynamically as commands are submitted. |
| 47 | * The userspace drivers maintain their own address space and the kernel |
| 48 | * sets up their pages tables accordingly when they submit their |
| 49 | * command buffers and a VMID is assigned. |
| 50 | * Cayman/Trinity support up to 8 active VMs at any given time; |
| 51 | * SI supports 16. |
| 52 | */ |
| 53 | |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 54 | /* Local structure. Encapsulate some VM table update parameters to reduce |
| 55 | * the number of function parameters |
| 56 | */ |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 57 | struct amdgpu_pte_update_params { |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 58 | /* amdgpu device we do this update for */ |
| 59 | struct amdgpu_device *adev; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 60 | /* optional amdgpu_vm we do this update for */ |
| 61 | struct amdgpu_vm *vm; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 62 | /* address where to copy page table entries from */ |
| 63 | uint64_t src; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 64 | /* indirect buffer to fill with commands */ |
| 65 | struct amdgpu_ib *ib; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 66 | /* Function which actually does the update */ |
| 67 | void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe, |
| 68 | uint64_t addr, unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 69 | uint64_t flags); |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 70 | /* indicate update pt or its shadow */ |
| 71 | bool shadow; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 74 | /* Helper to disable partial resident texture feature from a fence callback */ |
| 75 | struct amdgpu_prt_cb { |
| 76 | struct amdgpu_device *adev; |
| 77 | struct dma_fence_cb cb; |
| 78 | }; |
| 79 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 80 | /** |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 81 | * 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] | 82 | * |
| 83 | * @adev: amdgpu_device pointer |
| 84 | * |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 85 | * Calculate the number of entries in a page directory or page table. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 86 | */ |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 87 | static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev, |
| 88 | unsigned level) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 89 | { |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 90 | if (level == 0) |
| 91 | /* For the root directory */ |
| 92 | return adev->vm_manager.max_pfn >> |
| 93 | (amdgpu_vm_block_size * adev->vm_manager.num_level); |
| 94 | else if (level == adev->vm_manager.num_level) |
| 95 | /* For the page tables on the leaves */ |
| 96 | return AMDGPU_VM_PTE_COUNT; |
| 97 | else |
| 98 | /* Everything in between */ |
| 99 | return 1 << amdgpu_vm_block_size; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 103 | * amdgpu_vm_bo_size - returns the size of the BOs in bytes |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 104 | * |
| 105 | * @adev: amdgpu_device pointer |
| 106 | * |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 107 | * 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] | 108 | */ |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 109 | static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 110 | { |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 111 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 115 | * 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] | 116 | * |
| 117 | * @vm: vm providing the BOs |
Christian König | 3c0eea6 | 2015-12-11 14:39:05 +0100 | [diff] [blame] | 118 | * @validated: head of validation list |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 119 | * @entry: entry to add |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 120 | * |
| 121 | * Add the page directory to the list of BOs to |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 122 | * validate for command submission. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 123 | */ |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 124 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
| 125 | struct list_head *validated, |
| 126 | struct amdgpu_bo_list_entry *entry) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 127 | { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 128 | entry->robj = vm->root.bo; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 129 | entry->priority = 0; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 130 | entry->tv.bo = &entry->robj->tbo; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 131 | entry->tv.shared = true; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 132 | entry->user_pages = NULL; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 133 | list_add(&entry->tv.head, validated); |
| 134 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 135 | |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 136 | /** |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 137 | * amdgpu_vm_validate_pt_bos - validate the page table BOs |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 138 | * |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 139 | * @adev: amdgpu device pointer |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 140 | * @vm: vm providing the BOs |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 141 | * @validate: callback to do the validation |
| 142 | * @param: parameter for the validation callback |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 143 | * |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 144 | * Validate the page table BOs on command submission if neccessary. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 145 | */ |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 146 | int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
| 147 | int (*validate)(void *p, struct amdgpu_bo *bo), |
| 148 | void *param) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 149 | { |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 150 | uint64_t num_evictions; |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 151 | unsigned i; |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 152 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 153 | |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 154 | /* We only need to validate the page tables |
| 155 | * if they aren't already valid. |
| 156 | */ |
| 157 | num_evictions = atomic64_read(&adev->num_evictions); |
| 158 | if (num_evictions == vm->last_eviction_counter) |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 159 | return 0; |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 160 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 161 | /* add the vm page table to the list */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 162 | for (i = 0; i <= vm->root.last_entry_used; ++i) { |
| 163 | struct amdgpu_bo *bo = vm->root.entries[i].bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 164 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 165 | if (!bo) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 166 | continue; |
| 167 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 168 | r = validate(param, bo); |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 169 | if (r) |
| 170 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 171 | } |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 172 | |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 173 | return 0; |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail |
| 178 | * |
| 179 | * @adev: amdgpu device instance |
| 180 | * @vm: vm providing the BOs |
| 181 | * |
| 182 | * Move the PT BOs to the tail of the LRU. |
| 183 | */ |
| 184 | void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev, |
| 185 | struct amdgpu_vm *vm) |
| 186 | { |
| 187 | struct ttm_bo_global *glob = adev->mman.bdev.glob; |
| 188 | unsigned i; |
| 189 | |
| 190 | spin_lock(&glob->lru_lock); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 191 | for (i = 0; i <= vm->root.last_entry_used; ++i) { |
| 192 | struct amdgpu_bo *bo = vm->root.entries[i].bo; |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 193 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 194 | if (!bo) |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 195 | continue; |
| 196 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 197 | ttm_bo_move_to_lru_tail(&bo->tbo); |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 198 | } |
| 199 | spin_unlock(&glob->lru_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 202 | /** |
| 203 | * amdgpu_vm_alloc_pts - Allocate page tables. |
| 204 | * |
| 205 | * @adev: amdgpu_device pointer |
| 206 | * @vm: VM to allocate page tables for |
| 207 | * @saddr: Start address which needs to be allocated |
| 208 | * @size: Size from start address we need. |
| 209 | * |
| 210 | * Make sure the page tables are allocated. |
| 211 | */ |
| 212 | int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, |
| 213 | struct amdgpu_vm *vm, |
| 214 | uint64_t saddr, uint64_t size) |
| 215 | { |
| 216 | unsigned last_pfn, pt_idx; |
| 217 | uint64_t eaddr; |
| 218 | int r; |
| 219 | |
| 220 | /* validate the parameters */ |
| 221 | if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | eaddr = saddr + size - 1; |
| 225 | last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE; |
| 226 | if (last_pfn >= adev->vm_manager.max_pfn) { |
| 227 | dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n", |
| 228 | last_pfn, adev->vm_manager.max_pfn); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 233 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 234 | |
| 235 | saddr >>= amdgpu_vm_block_size; |
| 236 | eaddr >>= amdgpu_vm_block_size; |
| 237 | |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 238 | BUG_ON(eaddr >= amdgpu_vm_num_entries(adev, 0)); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 239 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 240 | if (eaddr > vm->root.last_entry_used) |
| 241 | vm->root.last_entry_used = eaddr; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 242 | |
| 243 | /* walk over the address space and allocate the page tables */ |
| 244 | for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 245 | struct reservation_object *resv = vm->root.bo->tbo.resv; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 246 | struct amdgpu_bo *pt; |
| 247 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 248 | if (vm->root.entries[pt_idx].bo) |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 249 | continue; |
| 250 | |
| 251 | r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8, |
| 252 | AMDGPU_GPU_PAGE_SIZE, true, |
| 253 | AMDGPU_GEM_DOMAIN_VRAM, |
| 254 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
| 255 | AMDGPU_GEM_CREATE_SHADOW | |
| 256 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | |
| 257 | AMDGPU_GEM_CREATE_VRAM_CLEARED, |
| 258 | NULL, resv, &pt); |
| 259 | if (r) |
| 260 | return r; |
| 261 | |
| 262 | /* Keep a reference to the page table to avoid freeing |
| 263 | * them up in the wrong order. |
| 264 | */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 265 | pt->parent = amdgpu_bo_ref(vm->root.bo); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 266 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 267 | vm->root.entries[pt_idx].bo = pt; |
| 268 | vm->root.entries[pt_idx].addr = 0; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Chunming Zhou | 192b7dc | 2016-06-29 14:01:15 +0800 | [diff] [blame] | 274 | static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev, |
| 275 | struct amdgpu_vm_id *id) |
| 276 | { |
| 277 | return id->current_gpu_reset_count != |
| 278 | atomic_read(&adev->gpu_reset_counter) ? true : false; |
| 279 | } |
| 280 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 281 | /** |
| 282 | * amdgpu_vm_grab_id - allocate the next free VMID |
| 283 | * |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 284 | * @vm: vm to allocate id for |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 285 | * @ring: ring we want to submit job to |
| 286 | * @sync: sync object where we add dependencies |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 287 | * @fence: fence protecting ID from reuse |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 288 | * |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 289 | * 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] | 290 | */ |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 291 | 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] | 292 | struct amdgpu_sync *sync, struct dma_fence *fence, |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 293 | struct amdgpu_job *job) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 294 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 295 | struct amdgpu_device *adev = ring->adev; |
Christian König | 090b767 | 2016-07-08 10:21:02 +0200 | [diff] [blame] | 296 | uint64_t fence_context = adev->fence_context + ring->idx; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 297 | struct dma_fence *updates = sync->last_vm_update; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 298 | struct amdgpu_vm_id *id, *idle; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 299 | struct dma_fence **fences; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 300 | unsigned i; |
| 301 | int r = 0; |
| 302 | |
| 303 | fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids, |
| 304 | GFP_KERNEL); |
| 305 | if (!fences) |
| 306 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 307 | |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 308 | mutex_lock(&adev->vm_manager.lock); |
| 309 | |
Christian König | 36fd7c5 | 2016-05-23 15:30:08 +0200 | [diff] [blame] | 310 | /* Check if we have an idle VMID */ |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 311 | i = 0; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 312 | list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) { |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 313 | fences[i] = amdgpu_sync_peek_fence(&idle->active, ring); |
| 314 | if (!fences[i]) |
Christian König | 36fd7c5 | 2016-05-23 15:30:08 +0200 | [diff] [blame] | 315 | break; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 316 | ++i; |
Christian König | 36fd7c5 | 2016-05-23 15:30:08 +0200 | [diff] [blame] | 317 | } |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 318 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 319 | /* If we can't find a idle VMID to use, wait till one becomes available */ |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 320 | if (&idle->list == &adev->vm_manager.ids_lru) { |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 321 | u64 fence_context = adev->vm_manager.fence_context + ring->idx; |
| 322 | unsigned seqno = ++adev->vm_manager.seqno[ring->idx]; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 323 | struct dma_fence_array *array; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 324 | unsigned j; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 325 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 326 | for (j = 0; j < i; ++j) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 327 | dma_fence_get(fences[j]); |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 328 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 329 | array = dma_fence_array_create(i, fences, fence_context, |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 330 | seqno, true); |
| 331 | if (!array) { |
| 332 | for (j = 0; j < i; ++j) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 333 | dma_fence_put(fences[j]); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 334 | kfree(fences); |
| 335 | r = -ENOMEM; |
| 336 | goto error; |
| 337 | } |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 338 | |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 339 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 340 | r = amdgpu_sync_fence(ring->adev, sync, &array->base); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 341 | dma_fence_put(&array->base); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 342 | if (r) |
| 343 | goto error; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 344 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 345 | mutex_unlock(&adev->vm_manager.lock); |
| 346 | return 0; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 347 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 348 | } |
| 349 | kfree(fences); |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 350 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 351 | job->vm_needs_flush = true; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 352 | /* Check if we can use a VMID already assigned to this VM */ |
| 353 | i = ring->idx; |
| 354 | do { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 355 | struct dma_fence *flushed; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 356 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 357 | id = vm->ids[i++]; |
| 358 | if (i == AMDGPU_MAX_RINGS) |
| 359 | i = 0; |
| 360 | |
| 361 | /* Check all the prerequisites to using this VMID */ |
| 362 | if (!id) |
| 363 | continue; |
Chunming Zhou | 192b7dc | 2016-06-29 14:01:15 +0800 | [diff] [blame] | 364 | if (amdgpu_vm_is_gpu_reset(adev, id)) |
Chunming Zhou | 6adb051 | 2016-06-27 17:06:01 +0800 | [diff] [blame] | 365 | continue; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 366 | |
| 367 | if (atomic64_read(&id->owner) != vm->client_id) |
| 368 | continue; |
| 369 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 370 | if (job->vm_pd_addr != id->pd_gpu_addr) |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 371 | continue; |
| 372 | |
Christian König | 090b767 | 2016-07-08 10:21:02 +0200 | [diff] [blame] | 373 | if (!id->last_flush) |
| 374 | continue; |
| 375 | |
| 376 | if (id->last_flush->context != fence_context && |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 377 | !dma_fence_is_signaled(id->last_flush)) |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 378 | continue; |
| 379 | |
| 380 | flushed = id->flushed_updates; |
| 381 | if (updates && |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 382 | (!flushed || dma_fence_is_later(updates, flushed))) |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 383 | continue; |
| 384 | |
Christian König | 3dab83b | 2016-06-01 13:31:17 +0200 | [diff] [blame] | 385 | /* Good we can use this VMID. Remember this submission as |
| 386 | * user of the VMID. |
| 387 | */ |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 388 | r = amdgpu_sync_fence(ring->adev, &id->active, fence); |
| 389 | if (r) |
| 390 | goto error; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 391 | |
Chunming Zhou | 6adb051 | 2016-06-27 17:06:01 +0800 | [diff] [blame] | 392 | id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 393 | list_move_tail(&id->list, &adev->vm_manager.ids_lru); |
| 394 | vm->ids[ring->idx] = id; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 395 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 396 | job->vm_id = id - adev->vm_manager.ids; |
| 397 | job->vm_needs_flush = false; |
Christian König | 0c0fdf1 | 2016-07-08 10:48:24 +0200 | [diff] [blame] | 398 | trace_amdgpu_vm_grab_id(vm, ring->idx, job); |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 399 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 400 | mutex_unlock(&adev->vm_manager.lock); |
| 401 | return 0; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 402 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 403 | } while (i != ring->idx); |
Chunming Zhou | 8e9fbeb | 2016-03-17 11:41:37 +0800 | [diff] [blame] | 404 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 405 | /* Still no ID to use? Then use the idle one found earlier */ |
| 406 | id = idle; |
| 407 | |
| 408 | /* Remember this submission as user of the VMID */ |
| 409 | r = amdgpu_sync_fence(ring->adev, &id->active, fence); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 410 | if (r) |
| 411 | goto error; |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 412 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 413 | dma_fence_put(id->first); |
| 414 | id->first = dma_fence_get(fence); |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 415 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 416 | dma_fence_put(id->last_flush); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 417 | id->last_flush = NULL; |
| 418 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 419 | dma_fence_put(id->flushed_updates); |
| 420 | id->flushed_updates = dma_fence_get(updates); |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 421 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 422 | id->pd_gpu_addr = job->vm_pd_addr; |
Chunming Zhou | b46b8a8 | 2016-06-27 17:04:23 +0800 | [diff] [blame] | 423 | id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 424 | list_move_tail(&id->list, &adev->vm_manager.ids_lru); |
Christian König | 0ea54b9 | 2016-05-04 10:20:01 +0200 | [diff] [blame] | 425 | atomic64_set(&id->owner, vm->client_id); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 426 | vm->ids[ring->idx] = id; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 427 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 428 | job->vm_id = id - adev->vm_manager.ids; |
Christian König | 0c0fdf1 | 2016-07-08 10:48:24 +0200 | [diff] [blame] | 429 | trace_amdgpu_vm_grab_id(vm, ring->idx, job); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 430 | |
| 431 | error: |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 432 | mutex_unlock(&adev->vm_manager.lock); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 433 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 436 | static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring) |
| 437 | { |
| 438 | struct amdgpu_device *adev = ring->adev; |
Alex Deucher | a125510 | 2016-10-13 17:41:13 -0400 | [diff] [blame] | 439 | const struct amdgpu_ip_block *ip_block; |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 440 | |
Christian König | 21cd942 | 2016-10-05 15:36:39 +0200 | [diff] [blame] | 441 | if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE) |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 442 | /* only compute rings */ |
| 443 | return false; |
| 444 | |
| 445 | ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); |
| 446 | if (!ip_block) |
| 447 | return false; |
| 448 | |
Alex Deucher | a125510 | 2016-10-13 17:41:13 -0400 | [diff] [blame] | 449 | if (ip_block->version->major <= 7) { |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 450 | /* gfx7 has no workaround */ |
| 451 | return true; |
Alex Deucher | a125510 | 2016-10-13 17:41:13 -0400 | [diff] [blame] | 452 | } else if (ip_block->version->major == 8) { |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 453 | if (adev->gfx.mec_fw_version >= 673) |
| 454 | /* gfx8 is fixed in MEC firmware 673 */ |
| 455 | return false; |
| 456 | else |
| 457 | return true; |
| 458 | } |
| 459 | return false; |
| 460 | } |
| 461 | |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 462 | static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr) |
| 463 | { |
| 464 | u64 addr = mc_addr; |
| 465 | |
| 466 | if (adev->mc.mc_funcs && adev->mc.mc_funcs->adjust_mc_addr) |
| 467 | addr = adev->mc.mc_funcs->adjust_mc_addr(adev, addr); |
| 468 | |
| 469 | return addr; |
| 470 | } |
| 471 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 472 | /** |
| 473 | * amdgpu_vm_flush - hardware flush the vm |
| 474 | * |
| 475 | * @ring: ring to use for flush |
Christian König | cffadc8 | 2016-03-01 13:34:49 +0100 | [diff] [blame] | 476 | * @vm_id: vmid number to use |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 477 | * @pd_addr: address of the page directory |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 478 | * |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 479 | * Emit a VM flush when it is necessary. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 480 | */ |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 481 | int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 482 | { |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 483 | struct amdgpu_device *adev = ring->adev; |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 484 | struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id]; |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 485 | bool gds_switch_needed = ring->funcs->emit_gds_switch && ( |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 486 | id->gds_base != job->gds_base || |
| 487 | id->gds_size != job->gds_size || |
| 488 | id->gws_base != job->gws_base || |
| 489 | id->gws_size != job->gws_size || |
| 490 | id->oa_base != job->oa_base || |
| 491 | id->oa_size != job->oa_size); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 492 | int r; |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 493 | |
| 494 | if (ring->funcs->emit_pipeline_sync && ( |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 495 | job->vm_needs_flush || gds_switch_needed || |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 496 | amdgpu_vm_ring_has_compute_vm_bug(ring))) |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 497 | amdgpu_ring_emit_pipeline_sync(ring); |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 498 | |
Chunming Zhou | aa1c890 | 2016-06-30 13:56:02 +0800 | [diff] [blame] | 499 | if (ring->funcs->emit_vm_flush && (job->vm_needs_flush || |
| 500 | amdgpu_vm_is_gpu_reset(adev, id))) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 501 | struct dma_fence *fence; |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 502 | u64 pd_addr = amdgpu_vm_adjust_mc_addr(adev, job->vm_pd_addr); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 503 | |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 504 | trace_amdgpu_vm_flush(pd_addr, ring->idx, job->vm_id); |
| 505 | amdgpu_ring_emit_vm_flush(ring, job->vm_id, pd_addr); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 506 | |
Christian König | 3dab83b | 2016-06-01 13:31:17 +0200 | [diff] [blame] | 507 | r = amdgpu_fence_emit(ring, &fence); |
| 508 | if (r) |
| 509 | return r; |
| 510 | |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 511 | mutex_lock(&adev->vm_manager.lock); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 512 | dma_fence_put(id->last_flush); |
Christian König | 3dab83b | 2016-06-01 13:31:17 +0200 | [diff] [blame] | 513 | id->last_flush = fence; |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 514 | mutex_unlock(&adev->vm_manager.lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 515 | } |
Christian König | cffadc8 | 2016-03-01 13:34:49 +0100 | [diff] [blame] | 516 | |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 517 | if (gds_switch_needed) { |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 518 | id->gds_base = job->gds_base; |
| 519 | id->gds_size = job->gds_size; |
| 520 | id->gws_base = job->gws_base; |
| 521 | id->gws_size = job->gws_size; |
| 522 | id->oa_base = job->oa_base; |
| 523 | id->oa_size = job->oa_size; |
| 524 | amdgpu_ring_emit_gds_switch(ring, job->vm_id, |
| 525 | job->gds_base, job->gds_size, |
| 526 | job->gws_base, job->gws_size, |
| 527 | job->oa_base, job->oa_size); |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 528 | } |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 529 | |
| 530 | return 0; |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
| 534 | * amdgpu_vm_reset_id - reset VMID to zero |
| 535 | * |
| 536 | * @adev: amdgpu device structure |
| 537 | * @vm_id: vmid number to use |
| 538 | * |
| 539 | * Reset saved GDW, GWS and OA to force switch on next flush. |
| 540 | */ |
| 541 | void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id) |
| 542 | { |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 543 | struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id]; |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 544 | |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 545 | id->gds_base = 0; |
| 546 | id->gds_size = 0; |
| 547 | id->gws_base = 0; |
| 548 | id->gws_size = 0; |
| 549 | id->oa_base = 0; |
| 550 | id->oa_size = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 554 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo |
| 555 | * |
| 556 | * @vm: requested vm |
| 557 | * @bo: requested buffer object |
| 558 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 559 | * Find @bo inside the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 560 | * Search inside the @bos vm list for the requested vm |
| 561 | * Returns the found bo_va or NULL if none is found |
| 562 | * |
| 563 | * Object has to be reserved! |
| 564 | */ |
| 565 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, |
| 566 | struct amdgpu_bo *bo) |
| 567 | { |
| 568 | struct amdgpu_bo_va *bo_va; |
| 569 | |
| 570 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
| 571 | if (bo_va->vm == vm) { |
| 572 | return bo_va; |
| 573 | } |
| 574 | } |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | /** |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 579 | * amdgpu_vm_do_set_ptes - helper to call the right asic function |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 580 | * |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 581 | * @params: see amdgpu_pte_update_params definition |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 582 | * @pe: addr of the page entry |
| 583 | * @addr: dst addr to write into pe |
| 584 | * @count: number of page entries to update |
| 585 | * @incr: increase next addr by incr bytes |
| 586 | * @flags: hw access flags |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 587 | * |
| 588 | * Traces the parameters and calls the right asic functions |
| 589 | * to setup the page table using the DMA. |
| 590 | */ |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 591 | static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params, |
| 592 | uint64_t pe, uint64_t addr, |
| 593 | unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 594 | uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 595 | { |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 596 | trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 597 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 598 | if (count < 3) { |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 599 | amdgpu_vm_write_pte(params->adev, params->ib, pe, |
| 600 | addr | flags, count, incr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 601 | |
| 602 | } else { |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 603 | amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 604 | count, incr, flags); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /** |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 609 | * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART |
| 610 | * |
| 611 | * @params: see amdgpu_pte_update_params definition |
| 612 | * @pe: addr of the page entry |
| 613 | * @addr: dst addr to write into pe |
| 614 | * @count: number of page entries to update |
| 615 | * @incr: increase next addr by incr bytes |
| 616 | * @flags: hw access flags |
| 617 | * |
| 618 | * Traces the parameters and calls the DMA function to copy the PTEs. |
| 619 | */ |
| 620 | static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params, |
| 621 | uint64_t pe, uint64_t addr, |
| 622 | unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 623 | uint64_t flags) |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 624 | { |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 625 | uint64_t src = (params->src + (addr >> 12) * 8); |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 626 | |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 627 | |
| 628 | trace_amdgpu_vm_copy_ptes(pe, src, count); |
| 629 | |
| 630 | amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count); |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 634 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 635 | * |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 636 | * @pages_addr: optional DMA address to use for lookup |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 637 | * @addr: the unmapped addr |
| 638 | * |
| 639 | * 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] | 640 | * to and return the pointer for the page table entry. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 641 | */ |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 642 | 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] | 643 | { |
| 644 | uint64_t result; |
| 645 | |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 646 | /* page table offset */ |
| 647 | result = pages_addr[addr >> PAGE_SHIFT]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 648 | |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 649 | /* in case cpu page size != gpu page size*/ |
| 650 | result |= addr & (~PAGE_MASK); |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 651 | |
| 652 | result &= 0xFFFFFFFFFFFFF000ULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 653 | |
| 654 | return result; |
| 655 | } |
| 656 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 657 | /* |
| 658 | * amdgpu_vm_update_pdes - make sure that page directory is valid |
| 659 | * |
| 660 | * @adev: amdgpu_device pointer |
| 661 | * @vm: requested vm |
| 662 | * @start: start of GPU address range |
| 663 | * @end: end of GPU address range |
| 664 | * |
| 665 | * Allocates new page tables if necessary |
| 666 | * and updates the page directory. |
| 667 | * Returns 0 for success, error for failure. |
| 668 | */ |
| 669 | int amdgpu_vm_update_page_directory(struct amdgpu_device *adev, |
| 670 | struct amdgpu_vm *vm) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 671 | { |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 672 | struct amdgpu_bo *shadow; |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 673 | struct amdgpu_ring *ring; |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 674 | uint64_t pd_addr, shadow_addr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 675 | uint32_t incr = AMDGPU_VM_PTE_COUNT * 8; |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 676 | uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 677 | unsigned count = 0, pt_idx, ndw; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 678 | struct amdgpu_job *job; |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 679 | struct amdgpu_pte_update_params params; |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 680 | struct dma_fence *fence = NULL; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 681 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 682 | int r; |
| 683 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 684 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 685 | shadow = vm->root.bo->shadow; |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 686 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 687 | /* padding, etc. */ |
| 688 | ndw = 64; |
| 689 | |
| 690 | /* assume the worst case */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 691 | ndw += vm->root.last_entry_used * 6; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 692 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 693 | pd_addr = amdgpu_bo_gpu_offset(vm->root.bo); |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 694 | if (shadow) { |
| 695 | r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem); |
| 696 | if (r) |
| 697 | return r; |
| 698 | shadow_addr = amdgpu_bo_gpu_offset(shadow); |
| 699 | ndw *= 2; |
| 700 | } else { |
| 701 | shadow_addr = 0; |
| 702 | } |
| 703 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 704 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
| 705 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 706 | return r; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 707 | |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 708 | memset(¶ms, 0, sizeof(params)); |
| 709 | params.adev = adev; |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 710 | params.ib = &job->ibs[0]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 711 | |
| 712 | /* walk over the address space and update the page directory */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 713 | for (pt_idx = 0; pt_idx <= vm->root.last_entry_used; ++pt_idx) { |
| 714 | struct amdgpu_bo *bo = vm->root.entries[pt_idx].bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 715 | uint64_t pde, pt; |
| 716 | |
| 717 | if (bo == NULL) |
| 718 | continue; |
| 719 | |
Christian König | 0fc8683 | 2016-09-16 11:46:23 +0200 | [diff] [blame] | 720 | if (bo->shadow) { |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 721 | struct amdgpu_bo *pt_shadow = bo->shadow; |
Christian König | 0fc8683 | 2016-09-16 11:46:23 +0200 | [diff] [blame] | 722 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 723 | r = amdgpu_ttm_bind(&pt_shadow->tbo, |
| 724 | &pt_shadow->tbo.mem); |
Christian König | 0fc8683 | 2016-09-16 11:46:23 +0200 | [diff] [blame] | 725 | if (r) |
| 726 | return r; |
| 727 | } |
| 728 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 729 | pt = amdgpu_bo_gpu_offset(bo); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 730 | if (vm->root.entries[pt_idx].addr == pt) |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 731 | continue; |
| 732 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 733 | vm->root.entries[pt_idx].addr = pt; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 734 | |
| 735 | pde = pd_addr + pt_idx * 8; |
| 736 | if (((last_pde + 8 * count) != pde) || |
Christian König | 96105e5 | 2016-08-12 12:59:59 +0200 | [diff] [blame] | 737 | ((last_pt + incr * count) != pt) || |
| 738 | (count == AMDGPU_VM_MAX_UPDATE_SIZE)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 739 | |
| 740 | if (count) { |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 741 | uint64_t pt_addr = |
| 742 | amdgpu_vm_adjust_mc_addr(adev, last_pt); |
| 743 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 744 | if (shadow) |
| 745 | amdgpu_vm_do_set_ptes(¶ms, |
| 746 | last_shadow, |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 747 | pt_addr, count, |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 748 | incr, |
| 749 | AMDGPU_PTE_VALID); |
| 750 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 751 | amdgpu_vm_do_set_ptes(¶ms, last_pde, |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 752 | pt_addr, count, incr, |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 753 | AMDGPU_PTE_VALID); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | count = 1; |
| 757 | last_pde = pde; |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 758 | last_shadow = shadow_addr + pt_idx * 8; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 759 | last_pt = pt; |
| 760 | } else { |
| 761 | ++count; |
| 762 | } |
| 763 | } |
| 764 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 765 | if (count) { |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 766 | uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt); |
| 767 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 768 | if (vm->root.bo->shadow) |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 769 | amdgpu_vm_do_set_ptes(¶ms, last_shadow, pt_addr, |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 770 | count, incr, AMDGPU_PTE_VALID); |
| 771 | |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 772 | amdgpu_vm_do_set_ptes(¶ms, last_pde, pt_addr, |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 773 | count, incr, AMDGPU_PTE_VALID); |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 774 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 775 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 776 | if (params.ib->length_dw == 0) { |
| 777 | amdgpu_job_free(job); |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | amdgpu_ring_pad_ib(ring, params.ib); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 782 | amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv, |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 783 | AMDGPU_FENCE_OWNER_VM); |
| 784 | if (shadow) |
| 785 | amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv, |
| 786 | AMDGPU_FENCE_OWNER_VM); |
| 787 | |
| 788 | WARN_ON(params.ib->length_dw > ndw); |
| 789 | r = amdgpu_job_submit(job, ring, &vm->entity, |
| 790 | AMDGPU_FENCE_OWNER_VM, &fence); |
| 791 | if (r) |
| 792 | goto error_free; |
| 793 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 794 | amdgpu_bo_fence(vm->root.bo, fence, true); |
Christian König | a24960f | 2016-10-12 13:20:52 +0200 | [diff] [blame] | 795 | dma_fence_put(vm->last_dir_update); |
| 796 | vm->last_dir_update = dma_fence_get(fence); |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 797 | dma_fence_put(fence); |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 798 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 799 | return 0; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 800 | |
| 801 | error_free: |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 802 | amdgpu_job_free(job); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 803 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /** |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 807 | * amdgpu_vm_update_ptes - make sure that page tables are valid |
| 808 | * |
| 809 | * @params: see amdgpu_pte_update_params definition |
| 810 | * @vm: requested vm |
| 811 | * @start: start of GPU address range |
| 812 | * @end: end of GPU address range |
| 813 | * @dst: destination address to map to, the next dst inside the function |
| 814 | * @flags: mapping flags |
| 815 | * |
| 816 | * Update the page tables in the range @start - @end. |
| 817 | */ |
| 818 | static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 819 | uint64_t start, uint64_t end, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 820 | uint64_t dst, uint64_t flags) |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 821 | { |
| 822 | const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1; |
| 823 | |
| 824 | uint64_t cur_pe_start, cur_nptes, cur_dst; |
| 825 | uint64_t addr; /* next GPU address to be updated */ |
| 826 | uint64_t pt_idx; |
| 827 | struct amdgpu_bo *pt; |
| 828 | unsigned nptes; /* next number of ptes to be updated */ |
| 829 | uint64_t next_pe_start; |
| 830 | |
| 831 | /* initialize the variables */ |
| 832 | addr = start; |
| 833 | pt_idx = addr >> amdgpu_vm_block_size; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 834 | pt = params->vm->root.entries[pt_idx].bo; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 835 | if (params->shadow) { |
| 836 | if (!pt->shadow) |
| 837 | return; |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 838 | pt = pt->shadow; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 839 | } |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 840 | if ((addr & ~mask) == (end & ~mask)) |
| 841 | nptes = end - addr; |
| 842 | else |
| 843 | nptes = AMDGPU_VM_PTE_COUNT - (addr & mask); |
| 844 | |
| 845 | cur_pe_start = amdgpu_bo_gpu_offset(pt); |
| 846 | cur_pe_start += (addr & mask) * 8; |
| 847 | cur_nptes = nptes; |
| 848 | cur_dst = dst; |
| 849 | |
| 850 | /* for next ptb*/ |
| 851 | addr += nptes; |
| 852 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; |
| 853 | |
| 854 | /* walk over the address space and update the page tables */ |
| 855 | while (addr < end) { |
| 856 | pt_idx = addr >> amdgpu_vm_block_size; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 857 | pt = params->vm->root.entries[pt_idx].bo; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 858 | if (params->shadow) { |
| 859 | if (!pt->shadow) |
| 860 | return; |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 861 | pt = pt->shadow; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 862 | } |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 863 | |
| 864 | if ((addr & ~mask) == (end & ~mask)) |
| 865 | nptes = end - addr; |
| 866 | else |
| 867 | nptes = AMDGPU_VM_PTE_COUNT - (addr & mask); |
| 868 | |
| 869 | next_pe_start = amdgpu_bo_gpu_offset(pt); |
| 870 | next_pe_start += (addr & mask) * 8; |
| 871 | |
Christian König | 96105e5 | 2016-08-12 12:59:59 +0200 | [diff] [blame] | 872 | if ((cur_pe_start + 8 * cur_nptes) == next_pe_start && |
| 873 | ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) { |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 874 | /* The next ptb is consecutive to current ptb. |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 875 | * Don't call the update function now. |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 876 | * Will update two ptbs together in future. |
| 877 | */ |
| 878 | cur_nptes += nptes; |
| 879 | } else { |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 880 | params->func(params, cur_pe_start, cur_dst, cur_nptes, |
| 881 | AMDGPU_GPU_PAGE_SIZE, flags); |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 882 | |
| 883 | cur_pe_start = next_pe_start; |
| 884 | cur_nptes = nptes; |
| 885 | cur_dst = dst; |
| 886 | } |
| 887 | |
| 888 | /* for next ptb*/ |
| 889 | addr += nptes; |
| 890 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; |
| 891 | } |
| 892 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 893 | params->func(params, cur_pe_start, cur_dst, cur_nptes, |
| 894 | AMDGPU_GPU_PAGE_SIZE, flags); |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 898 | * amdgpu_vm_frag_ptes - add fragment information to PTEs |
| 899 | * |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 900 | * @params: see amdgpu_pte_update_params definition |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 901 | * @vm: requested vm |
| 902 | * @start: first PTE to handle |
| 903 | * @end: last PTE to handle |
| 904 | * @dst: addr those PTEs should point to |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 905 | * @flags: hw mapping flags |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 906 | */ |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 907 | static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 908 | uint64_t start, uint64_t end, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 909 | uint64_t dst, uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 910 | { |
| 911 | /** |
| 912 | * The MC L1 TLB supports variable sized pages, based on a fragment |
| 913 | * field in the PTE. When this field is set to a non-zero value, page |
| 914 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE |
| 915 | * flags are considered valid for all PTEs within the fragment range |
| 916 | * and corresponding mappings are assumed to be physically contiguous. |
| 917 | * |
| 918 | * The L1 TLB can store a single PTE for the whole fragment, |
| 919 | * significantly increasing the space available for translation |
| 920 | * caching. This leads to large improvements in throughput when the |
| 921 | * TLB is under pressure. |
| 922 | * |
| 923 | * The L2 TLB distributes small and large fragments into two |
| 924 | * asymmetric partitions. The large fragment cache is significantly |
| 925 | * larger. Thus, we try to use large fragments wherever possible. |
| 926 | * Userspace can support this by aligning virtual base address and |
| 927 | * allocation size to the fragment size. |
| 928 | */ |
| 929 | |
Christian König | 8036617 | 2016-10-04 13:39:43 +0200 | [diff] [blame] | 930 | /* SI and newer are optimized for 64KB */ |
| 931 | uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG); |
| 932 | uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 933 | |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 934 | uint64_t frag_start = ALIGN(start, frag_align); |
| 935 | uint64_t frag_end = end & ~(frag_align - 1); |
Christian König | 31f6c1f | 2016-01-26 12:37:49 +0100 | [diff] [blame] | 936 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 937 | /* system pages are non continuously */ |
Christian König | b7fc2cb | 2016-08-11 16:44:15 +0200 | [diff] [blame] | 938 | if (params->src || !(flags & AMDGPU_PTE_VALID) || |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 939 | (frag_start >= frag_end)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 940 | |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 941 | amdgpu_vm_update_ptes(params, start, end, dst, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | |
| 945 | /* handle the 4K area at the beginning */ |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 946 | if (start != frag_start) { |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 947 | amdgpu_vm_update_ptes(params, start, frag_start, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 948 | dst, flags); |
| 949 | dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* handle the area in the middle */ |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 953 | amdgpu_vm_update_ptes(params, frag_start, frag_end, dst, |
Christian König | 8036617 | 2016-10-04 13:39:43 +0200 | [diff] [blame] | 954 | flags | frag_flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 955 | |
| 956 | /* handle the 4K area at the end */ |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 957 | if (frag_end != end) { |
| 958 | dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 959 | amdgpu_vm_update_ptes(params, frag_end, end, dst, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
| 963 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 964 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table |
| 965 | * |
| 966 | * @adev: amdgpu_device pointer |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 967 | * @exclusive: fence we need to sync to |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 968 | * @src: address where to copy page table entries from |
| 969 | * @pages_addr: DMA addresses to use for mapping |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 970 | * @vm: requested vm |
| 971 | * @start: start of mapped range |
| 972 | * @last: last mapped entry |
| 973 | * @flags: flags for the entries |
| 974 | * @addr: addr to set the area to |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 975 | * @fence: optional resulting fence |
| 976 | * |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 977 | * Fill in the page table entries between @start and @last. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 978 | * Returns 0 for success, -EINVAL for failure. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 979 | */ |
| 980 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 981 | struct dma_fence *exclusive, |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 982 | uint64_t src, |
| 983 | dma_addr_t *pages_addr, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 984 | struct amdgpu_vm *vm, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 985 | uint64_t start, uint64_t last, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 986 | uint64_t flags, uint64_t addr, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 987 | struct dma_fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 988 | { |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 989 | struct amdgpu_ring *ring; |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 990 | void *owner = AMDGPU_FENCE_OWNER_VM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 991 | unsigned nptes, ncmds, ndw; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 992 | struct amdgpu_job *job; |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 993 | struct amdgpu_pte_update_params params; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 994 | struct dma_fence *f = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 995 | int r; |
| 996 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 997 | memset(¶ms, 0, sizeof(params)); |
| 998 | params.adev = adev; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 999 | params.vm = vm; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1000 | params.src = src; |
| 1001 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1002 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 1003 | |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 1004 | /* sync to everything on unmapping */ |
| 1005 | if (!(flags & AMDGPU_PTE_VALID)) |
| 1006 | owner = AMDGPU_FENCE_OWNER_UNDEFINED; |
| 1007 | |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1008 | nptes = last - start + 1; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1009 | |
| 1010 | /* |
| 1011 | * reserve space for one command every (1 << BLOCK_SIZE) |
| 1012 | * entries or 2k dwords (whatever is smaller) |
| 1013 | */ |
| 1014 | ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1; |
| 1015 | |
| 1016 | /* padding, etc. */ |
| 1017 | ndw = 64; |
| 1018 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1019 | if (src) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1020 | /* only copy commands needed */ |
| 1021 | ndw += ncmds * 7; |
| 1022 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1023 | params.func = amdgpu_vm_do_copy_ptes; |
| 1024 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1025 | } else if (pages_addr) { |
| 1026 | /* copy commands needed */ |
| 1027 | ndw += ncmds * 7; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1028 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1029 | /* and also PTEs */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1030 | ndw += nptes * 2; |
| 1031 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1032 | params.func = amdgpu_vm_do_copy_ptes; |
| 1033 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1034 | } else { |
| 1035 | /* set page commands needed */ |
| 1036 | ndw += ncmds * 10; |
| 1037 | |
| 1038 | /* two extra commands for begin/end of fragment */ |
| 1039 | ndw += 2 * 10; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1040 | |
| 1041 | params.func = amdgpu_vm_do_set_ptes; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1042 | } |
| 1043 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1044 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
| 1045 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1046 | return r; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1047 | |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1048 | params.ib = &job->ibs[0]; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 1049 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1050 | if (!src && pages_addr) { |
| 1051 | uint64_t *pte; |
| 1052 | unsigned i; |
| 1053 | |
| 1054 | /* Put the PTEs at the end of the IB. */ |
| 1055 | i = ndw - nptes * 2; |
| 1056 | pte= (uint64_t *)&(job->ibs->ptr[i]); |
| 1057 | params.src = job->ibs->gpu_addr + i * 4; |
| 1058 | |
| 1059 | for (i = 0; i < nptes; ++i) { |
| 1060 | pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i * |
| 1061 | AMDGPU_GPU_PAGE_SIZE); |
| 1062 | pte[i] |= flags; |
| 1063 | } |
Christian König | d7a4ac6 | 2016-09-25 11:54:00 +0200 | [diff] [blame] | 1064 | addr = 0; |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1067 | r = amdgpu_sync_fence(adev, &job->sync, exclusive); |
| 1068 | if (r) |
| 1069 | goto error_free; |
| 1070 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1071 | 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] | 1072 | owner); |
| 1073 | if (r) |
| 1074 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1075 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1076 | r = reservation_object_reserve_shared(vm->root.bo->tbo.resv); |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 1077 | if (r) |
| 1078 | goto error_free; |
| 1079 | |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 1080 | params.shadow = true; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 1081 | amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 1082 | params.shadow = false; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 1083 | amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1084 | |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1085 | amdgpu_ring_pad_ib(ring, params.ib); |
| 1086 | WARN_ON(params.ib->length_dw > ndw); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1087 | r = amdgpu_job_submit(job, ring, &vm->entity, |
| 1088 | AMDGPU_FENCE_OWNER_VM, &f); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 1089 | if (r) |
| 1090 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1091 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1092 | amdgpu_bo_fence(vm->root.bo, f, true); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1093 | dma_fence_put(*fence); |
| 1094 | *fence = f; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1095 | return 0; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 1096 | |
| 1097 | error_free: |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1098 | amdgpu_job_free(job); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 1099 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /** |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1103 | * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks |
| 1104 | * |
| 1105 | * @adev: amdgpu_device pointer |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1106 | * @exclusive: fence we need to sync to |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1107 | * @gtt_flags: flags as they are used for GTT |
| 1108 | * @pages_addr: DMA addresses to use for mapping |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1109 | * @vm: requested vm |
| 1110 | * @mapping: mapped range and flags to use for the update |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1111 | * @flags: HW flags for the mapping |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1112 | * @nodes: array of drm_mm_nodes with the MC addresses |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1113 | * @fence: optional resulting fence |
| 1114 | * |
| 1115 | * Split the mapping into smaller chunks so that each update fits |
| 1116 | * into a SDMA IB. |
| 1117 | * Returns 0 for success, -EINVAL for failure. |
| 1118 | */ |
| 1119 | static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1120 | struct dma_fence *exclusive, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1121 | uint64_t gtt_flags, |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1122 | dma_addr_t *pages_addr, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1123 | struct amdgpu_vm *vm, |
| 1124 | struct amdgpu_bo_va_mapping *mapping, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1125 | uint64_t flags, |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1126 | struct drm_mm_node *nodes, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1127 | struct dma_fence **fence) |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1128 | { |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1129 | uint64_t pfn, src = 0, start = mapping->it.start; |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1130 | int r; |
| 1131 | |
| 1132 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here |
| 1133 | * but in case of something, we filter the flags in first place |
| 1134 | */ |
| 1135 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) |
| 1136 | flags &= ~AMDGPU_PTE_READABLE; |
| 1137 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) |
| 1138 | flags &= ~AMDGPU_PTE_WRITEABLE; |
| 1139 | |
Alex Xie | 15b31c5 | 2017-03-03 16:47:11 -0500 | [diff] [blame] | 1140 | flags &= ~AMDGPU_PTE_EXECUTABLE; |
| 1141 | flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE; |
| 1142 | |
Alex Xie | b0fd18b | 2017-03-03 16:49:39 -0500 | [diff] [blame] | 1143 | flags &= ~AMDGPU_PTE_MTYPE_MASK; |
| 1144 | flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK); |
| 1145 | |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1146 | trace_amdgpu_vm_bo_update(mapping); |
| 1147 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1148 | pfn = mapping->offset >> PAGE_SHIFT; |
| 1149 | if (nodes) { |
| 1150 | while (pfn >= nodes->size) { |
| 1151 | pfn -= nodes->size; |
| 1152 | ++nodes; |
| 1153 | } |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 1154 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1155 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1156 | do { |
| 1157 | uint64_t max_entries; |
| 1158 | uint64_t addr, last; |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1159 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1160 | if (nodes) { |
| 1161 | addr = nodes->start << PAGE_SHIFT; |
| 1162 | max_entries = (nodes->size - pfn) * |
| 1163 | (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); |
| 1164 | } else { |
| 1165 | addr = 0; |
| 1166 | max_entries = S64_MAX; |
| 1167 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1168 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1169 | if (pages_addr) { |
| 1170 | if (flags == gtt_flags) |
| 1171 | src = adev->gart.table_addr + |
| 1172 | (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8; |
| 1173 | else |
| 1174 | max_entries = min(max_entries, 16ull * 1024ull); |
| 1175 | addr = 0; |
| 1176 | } else if (flags & AMDGPU_PTE_VALID) { |
| 1177 | addr += adev->vm_manager.vram_base_offset; |
| 1178 | } |
| 1179 | addr += pfn << PAGE_SHIFT; |
| 1180 | |
| 1181 | last = min((uint64_t)mapping->it.last, start + max_entries - 1); |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1182 | r = amdgpu_vm_bo_update_mapping(adev, exclusive, |
| 1183 | src, pages_addr, vm, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1184 | start, last, flags, addr, |
| 1185 | fence); |
| 1186 | if (r) |
| 1187 | return r; |
| 1188 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1189 | pfn += last - start + 1; |
| 1190 | if (nodes && nodes->size == pfn) { |
| 1191 | pfn = 0; |
| 1192 | ++nodes; |
| 1193 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1194 | start = last + 1; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1195 | |
| 1196 | } while (unlikely(start != mapping->it.last + 1)); |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1202 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table |
| 1203 | * |
| 1204 | * @adev: amdgpu_device pointer |
| 1205 | * @bo_va: requested BO and VM object |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1206 | * @clear: if true clear the entries |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1207 | * |
| 1208 | * Fill in the page table entries for @bo_va. |
| 1209 | * Returns 0 for success, -EINVAL for failure. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1210 | */ |
| 1211 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, |
| 1212 | struct amdgpu_bo_va *bo_va, |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1213 | bool clear) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1214 | { |
| 1215 | struct amdgpu_vm *vm = bo_va->vm; |
| 1216 | struct amdgpu_bo_va_mapping *mapping; |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1217 | dma_addr_t *pages_addr = NULL; |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1218 | uint64_t gtt_flags, flags; |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1219 | struct ttm_mem_reg *mem; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1220 | struct drm_mm_node *nodes; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1221 | struct dma_fence *exclusive; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1222 | int r; |
| 1223 | |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1224 | if (clear || !bo_va->bo) { |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1225 | mem = NULL; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1226 | nodes = NULL; |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1227 | exclusive = NULL; |
| 1228 | } else { |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1229 | struct ttm_dma_tt *ttm; |
| 1230 | |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1231 | mem = &bo_va->bo->tbo.mem; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1232 | nodes = mem->mm_node; |
| 1233 | if (mem->mem_type == TTM_PL_TT) { |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1234 | ttm = container_of(bo_va->bo->tbo.ttm, struct |
| 1235 | ttm_dma_tt, ttm); |
| 1236 | pages_addr = ttm->dma_address; |
Christian König | 9ab2146 | 2015-11-30 14:19:26 +0100 | [diff] [blame] | 1237 | } |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1238 | exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1239 | } |
| 1240 | |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1241 | if (bo_va->bo) { |
| 1242 | flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem); |
| 1243 | gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) && |
| 1244 | adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ? |
| 1245 | flags : 0; |
| 1246 | } else { |
| 1247 | flags = 0x0; |
| 1248 | gtt_flags = ~0x0; |
| 1249 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1250 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1251 | spin_lock(&vm->status_lock); |
| 1252 | if (!list_empty(&bo_va->vm_status)) |
| 1253 | list_splice_init(&bo_va->valids, &bo_va->invalids); |
| 1254 | spin_unlock(&vm->status_lock); |
| 1255 | |
| 1256 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1257 | r = amdgpu_vm_bo_split_mapping(adev, exclusive, |
| 1258 | gtt_flags, pages_addr, vm, |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1259 | mapping, flags, nodes, |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1260 | &bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1261 | if (r) |
| 1262 | return r; |
| 1263 | } |
| 1264 | |
Christian König | d6c10f6 | 2015-09-28 12:00:23 +0200 | [diff] [blame] | 1265 | if (trace_amdgpu_vm_bo_mapping_enabled()) { |
| 1266 | list_for_each_entry(mapping, &bo_va->valids, list) |
| 1267 | trace_amdgpu_vm_bo_mapping(mapping); |
| 1268 | |
| 1269 | list_for_each_entry(mapping, &bo_va->invalids, list) |
| 1270 | trace_amdgpu_vm_bo_mapping(mapping); |
| 1271 | } |
| 1272 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1273 | spin_lock(&vm->status_lock); |
monk.liu | 6d1d0ef | 2015-08-14 13:36:41 +0800 | [diff] [blame] | 1274 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1275 | list_del_init(&bo_va->vm_status); |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1276 | if (clear) |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1277 | list_add(&bo_va->vm_status, &vm->cleared); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1278 | spin_unlock(&vm->status_lock); |
| 1279 | |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | /** |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1284 | * amdgpu_vm_update_prt_state - update the global PRT state |
| 1285 | */ |
| 1286 | static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) |
| 1287 | { |
| 1288 | unsigned long flags; |
| 1289 | bool enable; |
| 1290 | |
| 1291 | spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1292 | enable = !!atomic_read(&adev->vm_manager.num_prt_users); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1293 | adev->gart.gart_funcs->set_prt(adev, enable); |
| 1294 | spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); |
| 1295 | } |
| 1296 | |
| 1297 | /** |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1298 | * amdgpu_vm_prt_get - add a PRT user |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1299 | */ |
| 1300 | static void amdgpu_vm_prt_get(struct amdgpu_device *adev) |
| 1301 | { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1302 | if (!adev->gart.gart_funcs->set_prt) |
| 1303 | return; |
| 1304 | |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1305 | if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) |
| 1306 | amdgpu_vm_update_prt_state(adev); |
| 1307 | } |
| 1308 | |
| 1309 | /** |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1310 | * amdgpu_vm_prt_put - drop a PRT user |
| 1311 | */ |
| 1312 | static void amdgpu_vm_prt_put(struct amdgpu_device *adev) |
| 1313 | { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1314 | if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1315 | amdgpu_vm_update_prt_state(adev); |
| 1316 | } |
| 1317 | |
| 1318 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1319 | * amdgpu_vm_prt_cb - callback for updating the PRT status |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1320 | */ |
| 1321 | static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) |
| 1322 | { |
| 1323 | struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); |
| 1324 | |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1325 | amdgpu_vm_prt_put(cb->adev); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1326 | kfree(cb); |
| 1327 | } |
| 1328 | |
| 1329 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1330 | * amdgpu_vm_add_prt_cb - add callback for updating the PRT status |
| 1331 | */ |
| 1332 | static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, |
| 1333 | struct dma_fence *fence) |
| 1334 | { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1335 | struct amdgpu_prt_cb *cb; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1336 | |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1337 | if (!adev->gart.gart_funcs->set_prt) |
| 1338 | return; |
| 1339 | |
| 1340 | cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1341 | if (!cb) { |
| 1342 | /* Last resort when we are OOM */ |
| 1343 | if (fence) |
| 1344 | dma_fence_wait(fence, false); |
| 1345 | |
| 1346 | amdgpu_vm_prt_put(cb->adev); |
| 1347 | } else { |
| 1348 | cb->adev = adev; |
| 1349 | if (!fence || dma_fence_add_callback(fence, &cb->cb, |
| 1350 | amdgpu_vm_prt_cb)) |
| 1351 | amdgpu_vm_prt_cb(fence, &cb->cb); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /** |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1356 | * amdgpu_vm_free_mapping - free a mapping |
| 1357 | * |
| 1358 | * @adev: amdgpu_device pointer |
| 1359 | * @vm: requested vm |
| 1360 | * @mapping: mapping to be freed |
| 1361 | * @fence: fence of the unmap operation |
| 1362 | * |
| 1363 | * Free a mapping and make sure we decrease the PRT usage count if applicable. |
| 1364 | */ |
| 1365 | static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, |
| 1366 | struct amdgpu_vm *vm, |
| 1367 | struct amdgpu_bo_va_mapping *mapping, |
| 1368 | struct dma_fence *fence) |
| 1369 | { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1370 | if (mapping->flags & AMDGPU_PTE_PRT) |
| 1371 | amdgpu_vm_add_prt_cb(adev, fence); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1372 | kfree(mapping); |
| 1373 | } |
| 1374 | |
| 1375 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1376 | * amdgpu_vm_prt_fini - finish all prt mappings |
| 1377 | * |
| 1378 | * @adev: amdgpu_device pointer |
| 1379 | * @vm: requested vm |
| 1380 | * |
| 1381 | * Register a cleanup callback to disable PRT support after VM dies. |
| 1382 | */ |
| 1383 | static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1384 | { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1385 | struct reservation_object *resv = vm->root.bo->tbo.resv; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1386 | struct dma_fence *excl, **shared; |
| 1387 | unsigned i, shared_count; |
| 1388 | int r; |
| 1389 | |
| 1390 | r = reservation_object_get_fences_rcu(resv, &excl, |
| 1391 | &shared_count, &shared); |
| 1392 | if (r) { |
| 1393 | /* Not enough memory to grab the fence list, as last resort |
| 1394 | * block for all the fences to complete. |
| 1395 | */ |
| 1396 | reservation_object_wait_timeout_rcu(resv, true, false, |
| 1397 | MAX_SCHEDULE_TIMEOUT); |
| 1398 | return; |
| 1399 | } |
| 1400 | |
| 1401 | /* Add a callback for each fence in the reservation object */ |
| 1402 | amdgpu_vm_prt_get(adev); |
| 1403 | amdgpu_vm_add_prt_cb(adev, excl); |
| 1404 | |
| 1405 | for (i = 0; i < shared_count; ++i) { |
| 1406 | amdgpu_vm_prt_get(adev); |
| 1407 | amdgpu_vm_add_prt_cb(adev, shared[i]); |
| 1408 | } |
| 1409 | |
| 1410 | kfree(shared); |
| 1411 | } |
| 1412 | |
| 1413 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1414 | * amdgpu_vm_clear_freed - clear freed BOs in the PT |
| 1415 | * |
| 1416 | * @adev: amdgpu_device pointer |
| 1417 | * @vm: requested vm |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1418 | * @fence: optional resulting fence (unchanged if no work needed to be done |
| 1419 | * or if an error occurred) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1420 | * |
| 1421 | * Make sure all freed BOs are cleared in the PT. |
| 1422 | * Returns 0 for success. |
| 1423 | * |
| 1424 | * PTs have to be reserved and mutex must be locked! |
| 1425 | */ |
| 1426 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1427 | struct amdgpu_vm *vm, |
| 1428 | struct dma_fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1429 | { |
| 1430 | struct amdgpu_bo_va_mapping *mapping; |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1431 | struct dma_fence *f = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1432 | int r; |
| 1433 | |
| 1434 | while (!list_empty(&vm->freed)) { |
| 1435 | mapping = list_first_entry(&vm->freed, |
| 1436 | struct amdgpu_bo_va_mapping, list); |
| 1437 | list_del(&mapping->list); |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 1438 | |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1439 | r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping, |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1440 | 0, 0, &f); |
| 1441 | amdgpu_vm_free_mapping(adev, vm, mapping, f); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1442 | if (r) { |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1443 | dma_fence_put(f); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1444 | return r; |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1445 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1446 | } |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1447 | |
| 1448 | if (fence && f) { |
| 1449 | dma_fence_put(*fence); |
| 1450 | *fence = f; |
| 1451 | } else { |
| 1452 | dma_fence_put(f); |
| 1453 | } |
| 1454 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1455 | return 0; |
| 1456 | |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT |
| 1461 | * |
| 1462 | * @adev: amdgpu_device pointer |
| 1463 | * @vm: requested vm |
| 1464 | * |
| 1465 | * Make sure all invalidated BOs are cleared in the PT. |
| 1466 | * Returns 0 for success. |
| 1467 | * |
| 1468 | * PTs have to be reserved and mutex must be locked! |
| 1469 | */ |
| 1470 | int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 1471 | struct amdgpu_vm *vm, struct amdgpu_sync *sync) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1472 | { |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 1473 | struct amdgpu_bo_va *bo_va = NULL; |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 1474 | int r = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1475 | |
| 1476 | spin_lock(&vm->status_lock); |
| 1477 | while (!list_empty(&vm->invalidated)) { |
| 1478 | bo_va = list_first_entry(&vm->invalidated, |
| 1479 | struct amdgpu_bo_va, vm_status); |
| 1480 | spin_unlock(&vm->status_lock); |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1481 | |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1482 | r = amdgpu_vm_bo_update(adev, bo_va, true); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1483 | if (r) |
| 1484 | return r; |
| 1485 | |
| 1486 | spin_lock(&vm->status_lock); |
| 1487 | } |
| 1488 | spin_unlock(&vm->status_lock); |
| 1489 | |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 1490 | if (bo_va) |
Chunming Zhou | bb1e38a4 | 2015-08-03 18:19:38 +0800 | [diff] [blame] | 1491 | r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update); |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 1492 | |
| 1493 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | /** |
| 1497 | * amdgpu_vm_bo_add - add a bo to a specific vm |
| 1498 | * |
| 1499 | * @adev: amdgpu_device pointer |
| 1500 | * @vm: requested vm |
| 1501 | * @bo: amdgpu buffer object |
| 1502 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1503 | * Add @bo into the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1504 | * Add @bo to the list of bos associated with the vm |
| 1505 | * Returns newly added bo_va or NULL for failure |
| 1506 | * |
| 1507 | * Object has to be reserved! |
| 1508 | */ |
| 1509 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, |
| 1510 | struct amdgpu_vm *vm, |
| 1511 | struct amdgpu_bo *bo) |
| 1512 | { |
| 1513 | struct amdgpu_bo_va *bo_va; |
| 1514 | |
| 1515 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); |
| 1516 | if (bo_va == NULL) { |
| 1517 | return NULL; |
| 1518 | } |
| 1519 | bo_va->vm = vm; |
| 1520 | bo_va->bo = bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1521 | bo_va->ref_count = 1; |
| 1522 | INIT_LIST_HEAD(&bo_va->bo_list); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1523 | INIT_LIST_HEAD(&bo_va->valids); |
| 1524 | INIT_LIST_HEAD(&bo_va->invalids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1525 | INIT_LIST_HEAD(&bo_va->vm_status); |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1526 | |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1527 | if (bo) |
| 1528 | list_add_tail(&bo_va->bo_list, &bo->va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1529 | |
| 1530 | return bo_va; |
| 1531 | } |
| 1532 | |
| 1533 | /** |
| 1534 | * amdgpu_vm_bo_map - map bo inside a vm |
| 1535 | * |
| 1536 | * @adev: amdgpu_device pointer |
| 1537 | * @bo_va: bo_va to store the address |
| 1538 | * @saddr: where to map the BO |
| 1539 | * @offset: requested offset in the BO |
| 1540 | * @flags: attributes of pages (read/write/valid/etc.) |
| 1541 | * |
| 1542 | * Add a mapping of the BO at the specefied addr into the VM. |
| 1543 | * Returns 0 for success, error for failure. |
| 1544 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1545 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1546 | */ |
| 1547 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, |
| 1548 | struct amdgpu_bo_va *bo_va, |
| 1549 | uint64_t saddr, uint64_t offset, |
Christian König | 268c300 | 2017-01-18 14:49:43 +0100 | [diff] [blame] | 1550 | uint64_t size, uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1551 | { |
| 1552 | struct amdgpu_bo_va_mapping *mapping; |
| 1553 | struct amdgpu_vm *vm = bo_va->vm; |
| 1554 | struct interval_tree_node *it; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1555 | uint64_t eaddr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1556 | |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1557 | /* validate the parameters */ |
| 1558 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1559 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1560 | return -EINVAL; |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1561 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1562 | /* make sure object fit at this offset */ |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1563 | eaddr = saddr + size - 1; |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1564 | if (saddr >= eaddr || |
| 1565 | (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo))) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1566 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1567 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1568 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1569 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1570 | |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1571 | it = interval_tree_iter_first(&vm->va, saddr, eaddr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1572 | if (it) { |
| 1573 | struct amdgpu_bo_va_mapping *tmp; |
| 1574 | tmp = container_of(it, struct amdgpu_bo_va_mapping, it); |
| 1575 | /* bo and tmp overlap, invalid addr */ |
| 1576 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " |
| 1577 | "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr, |
| 1578 | tmp->it.start, tmp->it.last + 1); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 1579 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 1583 | if (!mapping) |
| 1584 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1585 | |
| 1586 | INIT_LIST_HEAD(&mapping->list); |
| 1587 | mapping->it.start = saddr; |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1588 | mapping->it.last = eaddr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1589 | mapping->offset = offset; |
| 1590 | mapping->flags = flags; |
| 1591 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1592 | list_add(&mapping->list, &bo_va->invalids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1593 | interval_tree_insert(&mapping->it, &vm->va); |
| 1594 | |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1595 | if (flags & AMDGPU_PTE_PRT) |
| 1596 | amdgpu_vm_prt_get(adev); |
| 1597 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1598 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /** |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 1602 | * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings |
| 1603 | * |
| 1604 | * @adev: amdgpu_device pointer |
| 1605 | * @bo_va: bo_va to store the address |
| 1606 | * @saddr: where to map the BO |
| 1607 | * @offset: requested offset in the BO |
| 1608 | * @flags: attributes of pages (read/write/valid/etc.) |
| 1609 | * |
| 1610 | * Add a mapping of the BO at the specefied addr into the VM. Replace existing |
| 1611 | * mappings as we do so. |
| 1612 | * Returns 0 for success, error for failure. |
| 1613 | * |
| 1614 | * Object has to be reserved and unreserved outside! |
| 1615 | */ |
| 1616 | int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, |
| 1617 | struct amdgpu_bo_va *bo_va, |
| 1618 | uint64_t saddr, uint64_t offset, |
| 1619 | uint64_t size, uint64_t flags) |
| 1620 | { |
| 1621 | struct amdgpu_bo_va_mapping *mapping; |
| 1622 | struct amdgpu_vm *vm = bo_va->vm; |
| 1623 | uint64_t eaddr; |
| 1624 | int r; |
| 1625 | |
| 1626 | /* validate the parameters */ |
| 1627 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || |
| 1628 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
| 1629 | return -EINVAL; |
| 1630 | |
| 1631 | /* make sure object fit at this offset */ |
| 1632 | eaddr = saddr + size - 1; |
| 1633 | if (saddr >= eaddr || |
| 1634 | (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo))) |
| 1635 | return -EINVAL; |
| 1636 | |
| 1637 | /* Allocate all the needed memory */ |
| 1638 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); |
| 1639 | if (!mapping) |
| 1640 | return -ENOMEM; |
| 1641 | |
| 1642 | r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size); |
| 1643 | if (r) { |
| 1644 | kfree(mapping); |
| 1645 | return r; |
| 1646 | } |
| 1647 | |
| 1648 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1649 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1650 | |
| 1651 | mapping->it.start = saddr; |
| 1652 | mapping->it.last = eaddr; |
| 1653 | mapping->offset = offset; |
| 1654 | mapping->flags = flags; |
| 1655 | |
| 1656 | list_add(&mapping->list, &bo_va->invalids); |
| 1657 | interval_tree_insert(&mapping->it, &vm->va); |
| 1658 | |
| 1659 | if (flags & AMDGPU_PTE_PRT) |
| 1660 | amdgpu_vm_prt_get(adev); |
| 1661 | |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1666 | * amdgpu_vm_bo_unmap - remove bo mapping from vm |
| 1667 | * |
| 1668 | * @adev: amdgpu_device pointer |
| 1669 | * @bo_va: bo_va to remove the address from |
| 1670 | * @saddr: where to the BO is mapped |
| 1671 | * |
| 1672 | * Remove a mapping of the BO at the specefied addr from the VM. |
| 1673 | * Returns 0 for success, error for failure. |
| 1674 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1675 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1676 | */ |
| 1677 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, |
| 1678 | struct amdgpu_bo_va *bo_va, |
| 1679 | uint64_t saddr) |
| 1680 | { |
| 1681 | struct amdgpu_bo_va_mapping *mapping; |
| 1682 | struct amdgpu_vm *vm = bo_va->vm; |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1683 | bool valid = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1684 | |
Christian König | 6c7fc50 | 2015-06-05 20:56:17 +0200 | [diff] [blame] | 1685 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1686 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1687 | list_for_each_entry(mapping, &bo_va->valids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1688 | if (mapping->it.start == saddr) |
| 1689 | break; |
| 1690 | } |
| 1691 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1692 | if (&mapping->list == &bo_va->valids) { |
| 1693 | valid = false; |
| 1694 | |
| 1695 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
| 1696 | if (mapping->it.start == saddr) |
| 1697 | break; |
| 1698 | } |
| 1699 | |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1700 | if (&mapping->list == &bo_va->invalids) |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1701 | return -ENOENT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1702 | } |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1703 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1704 | list_del(&mapping->list); |
| 1705 | interval_tree_remove(&mapping->it, &vm->va); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1706 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1707 | |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 1708 | if (valid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1709 | list_add(&mapping->list, &vm->freed); |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 1710 | else |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1711 | amdgpu_vm_free_mapping(adev, vm, mapping, |
| 1712 | bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1713 | |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | /** |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1718 | * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range |
| 1719 | * |
| 1720 | * @adev: amdgpu_device pointer |
| 1721 | * @vm: VM structure to use |
| 1722 | * @saddr: start of the range |
| 1723 | * @size: size of the range |
| 1724 | * |
| 1725 | * Remove all mappings in a range, split them as appropriate. |
| 1726 | * Returns 0 for success, error for failure. |
| 1727 | */ |
| 1728 | int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, |
| 1729 | struct amdgpu_vm *vm, |
| 1730 | uint64_t saddr, uint64_t size) |
| 1731 | { |
| 1732 | struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; |
| 1733 | struct interval_tree_node *it; |
| 1734 | LIST_HEAD(removed); |
| 1735 | uint64_t eaddr; |
| 1736 | |
| 1737 | eaddr = saddr + size - 1; |
| 1738 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1739 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1740 | |
| 1741 | /* Allocate all the needed memory */ |
| 1742 | before = kzalloc(sizeof(*before), GFP_KERNEL); |
| 1743 | if (!before) |
| 1744 | return -ENOMEM; |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1745 | INIT_LIST_HEAD(&before->list); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1746 | |
| 1747 | after = kzalloc(sizeof(*after), GFP_KERNEL); |
| 1748 | if (!after) { |
| 1749 | kfree(before); |
| 1750 | return -ENOMEM; |
| 1751 | } |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1752 | INIT_LIST_HEAD(&after->list); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1753 | |
| 1754 | /* Now gather all removed mappings */ |
| 1755 | it = interval_tree_iter_first(&vm->va, saddr, eaddr); |
| 1756 | while (it) { |
| 1757 | tmp = container_of(it, struct amdgpu_bo_va_mapping, it); |
| 1758 | it = interval_tree_iter_next(it, saddr, eaddr); |
| 1759 | |
| 1760 | /* Remember mapping split at the start */ |
| 1761 | if (tmp->it.start < saddr) { |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1762 | before->it.start = tmp->it.start; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1763 | before->it.last = saddr - 1; |
| 1764 | before->offset = tmp->offset; |
| 1765 | before->flags = tmp->flags; |
| 1766 | list_add(&before->list, &tmp->list); |
| 1767 | } |
| 1768 | |
| 1769 | /* Remember mapping split at the end */ |
| 1770 | if (tmp->it.last > eaddr) { |
| 1771 | after->it.start = eaddr + 1; |
| 1772 | after->it.last = tmp->it.last; |
| 1773 | after->offset = tmp->offset; |
| 1774 | after->offset += after->it.start - tmp->it.start; |
| 1775 | after->flags = tmp->flags; |
| 1776 | list_add(&after->list, &tmp->list); |
| 1777 | } |
| 1778 | |
| 1779 | list_del(&tmp->list); |
| 1780 | list_add(&tmp->list, &removed); |
| 1781 | } |
| 1782 | |
| 1783 | /* And free them up */ |
| 1784 | list_for_each_entry_safe(tmp, next, &removed, list) { |
| 1785 | interval_tree_remove(&tmp->it, &vm->va); |
| 1786 | list_del(&tmp->list); |
| 1787 | |
| 1788 | if (tmp->it.start < saddr) |
| 1789 | tmp->it.start = saddr; |
| 1790 | if (tmp->it.last > eaddr) |
| 1791 | tmp->it.last = eaddr; |
| 1792 | |
| 1793 | list_add(&tmp->list, &vm->freed); |
| 1794 | trace_amdgpu_vm_bo_unmap(NULL, tmp); |
| 1795 | } |
| 1796 | |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1797 | /* Insert partial mapping before the range */ |
| 1798 | if (!list_empty(&before->list)) { |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1799 | interval_tree_insert(&before->it, &vm->va); |
| 1800 | if (before->flags & AMDGPU_PTE_PRT) |
| 1801 | amdgpu_vm_prt_get(adev); |
| 1802 | } else { |
| 1803 | kfree(before); |
| 1804 | } |
| 1805 | |
| 1806 | /* Insert partial mapping after the range */ |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1807 | if (!list_empty(&after->list)) { |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1808 | interval_tree_insert(&after->it, &vm->va); |
| 1809 | if (after->flags & AMDGPU_PTE_PRT) |
| 1810 | amdgpu_vm_prt_get(adev); |
| 1811 | } else { |
| 1812 | kfree(after); |
| 1813 | } |
| 1814 | |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
| 1818 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1819 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm |
| 1820 | * |
| 1821 | * @adev: amdgpu_device pointer |
| 1822 | * @bo_va: requested bo_va |
| 1823 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1824 | * Remove @bo_va->bo from the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1825 | * |
| 1826 | * Object have to be reserved! |
| 1827 | */ |
| 1828 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, |
| 1829 | struct amdgpu_bo_va *bo_va) |
| 1830 | { |
| 1831 | struct amdgpu_bo_va_mapping *mapping, *next; |
| 1832 | struct amdgpu_vm *vm = bo_va->vm; |
| 1833 | |
| 1834 | list_del(&bo_va->bo_list); |
| 1835 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1836 | spin_lock(&vm->status_lock); |
| 1837 | list_del(&bo_va->vm_status); |
| 1838 | spin_unlock(&vm->status_lock); |
| 1839 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1840 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1841 | list_del(&mapping->list); |
| 1842 | interval_tree_remove(&mapping->it, &vm->va); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1843 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1844 | list_add(&mapping->list, &vm->freed); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1845 | } |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1846 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { |
| 1847 | list_del(&mapping->list); |
| 1848 | interval_tree_remove(&mapping->it, &vm->va); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1849 | amdgpu_vm_free_mapping(adev, vm, mapping, |
| 1850 | bo_va->last_pt_update); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1851 | } |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1852 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1853 | dma_fence_put(bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1854 | kfree(bo_va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * amdgpu_vm_bo_invalidate - mark the bo as invalid |
| 1859 | * |
| 1860 | * @adev: amdgpu_device pointer |
| 1861 | * @vm: requested vm |
| 1862 | * @bo: amdgpu buffer object |
| 1863 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1864 | * Mark @bo as invalid. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1865 | */ |
| 1866 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, |
| 1867 | struct amdgpu_bo *bo) |
| 1868 | { |
| 1869 | struct amdgpu_bo_va *bo_va; |
| 1870 | |
| 1871 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1872 | spin_lock(&bo_va->vm->status_lock); |
| 1873 | if (list_empty(&bo_va->vm_status)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1874 | list_add(&bo_va->vm_status, &bo_va->vm->invalidated); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1875 | spin_unlock(&bo_va->vm->status_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | /** |
| 1880 | * amdgpu_vm_init - initialize a vm instance |
| 1881 | * |
| 1882 | * @adev: amdgpu_device pointer |
| 1883 | * @vm: requested vm |
| 1884 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1885 | * Init @vm fields. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1886 | */ |
| 1887 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1888 | { |
| 1889 | const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, |
| 1890 | AMDGPU_VM_PTE_COUNT * 8); |
Michel Dänzer | 9571e1d | 2016-01-19 17:59:46 +0900 | [diff] [blame] | 1891 | unsigned pd_size, pd_entries; |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1892 | unsigned ring_instance; |
| 1893 | struct amdgpu_ring *ring; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1894 | struct amd_sched_rq *rq; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1895 | int i, r; |
| 1896 | |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 1897 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 1898 | vm->ids[i] = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1899 | vm->va = RB_ROOT; |
Chunming Zhou | 031e298 | 2016-04-25 10:19:13 +0800 | [diff] [blame] | 1900 | vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1901 | spin_lock_init(&vm->status_lock); |
| 1902 | INIT_LIST_HEAD(&vm->invalidated); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1903 | INIT_LIST_HEAD(&vm->cleared); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1904 | INIT_LIST_HEAD(&vm->freed); |
Christian König | 2025021 | 2016-03-08 17:58:35 +0100 | [diff] [blame] | 1905 | |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 1906 | pd_size = amdgpu_vm_bo_size(adev, 0); |
| 1907 | pd_entries = amdgpu_vm_num_entries(adev, 0); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1908 | |
| 1909 | /* allocate page table array */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1910 | vm->root.entries = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt)); |
| 1911 | if (vm->root.entries == NULL) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1912 | DRM_ERROR("Cannot allocate memory for page table array\n"); |
| 1913 | return -ENOMEM; |
| 1914 | } |
| 1915 | |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1916 | /* create scheduler entity for page table updates */ |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1917 | |
| 1918 | ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring); |
| 1919 | ring_instance %= adev->vm_manager.vm_pte_num_rings; |
| 1920 | ring = adev->vm_manager.vm_pte_rings[ring_instance]; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1921 | rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL]; |
| 1922 | r = amd_sched_entity_init(&ring->sched, &vm->entity, |
| 1923 | rq, amdgpu_sched_jobs); |
| 1924 | if (r) |
Chunming Zhou | 64827ad | 2016-07-28 17:20:32 +0800 | [diff] [blame] | 1925 | goto err; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1926 | |
Christian König | a24960f | 2016-10-12 13:20:52 +0200 | [diff] [blame] | 1927 | vm->last_dir_update = NULL; |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 1928 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1929 | r = amdgpu_bo_create(adev, pd_size, align, true, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 1930 | AMDGPU_GEM_DOMAIN_VRAM, |
Chunming Zhou | 1baa439 | 2016-08-04 13:59:32 +0800 | [diff] [blame] | 1931 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 1932 | AMDGPU_GEM_CREATE_SHADOW | |
Christian König | 617859e | 2016-11-17 15:40:02 +0100 | [diff] [blame] | 1933 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | |
| 1934 | AMDGPU_GEM_CREATE_VRAM_CLEARED, |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1935 | NULL, NULL, &vm->root.bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1936 | if (r) |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1937 | goto error_free_sched_entity; |
| 1938 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1939 | r = amdgpu_bo_reserve(vm->root.bo, false); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1940 | if (r) |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1941 | goto error_free_root; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1942 | |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 1943 | vm->last_eviction_counter = atomic64_read(&adev->num_evictions); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1944 | amdgpu_bo_unreserve(vm->root.bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1945 | |
| 1946 | return 0; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1947 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1948 | error_free_root: |
| 1949 | amdgpu_bo_unref(&vm->root.bo->shadow); |
| 1950 | amdgpu_bo_unref(&vm->root.bo); |
| 1951 | vm->root.bo = NULL; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1952 | |
| 1953 | error_free_sched_entity: |
| 1954 | amd_sched_entity_fini(&ring->sched, &vm->entity); |
| 1955 | |
Chunming Zhou | 64827ad | 2016-07-28 17:20:32 +0800 | [diff] [blame] | 1956 | err: |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1957 | drm_free_large(vm->root.entries); |
Chunming Zhou | 64827ad | 2016-07-28 17:20:32 +0800 | [diff] [blame] | 1958 | |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1959 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /** |
| 1963 | * amdgpu_vm_fini - tear down a vm instance |
| 1964 | * |
| 1965 | * @adev: amdgpu_device pointer |
| 1966 | * @vm: requested vm |
| 1967 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1968 | * Tear down @vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1969 | * Unbind the VM and remove all bos from the vm bo list |
| 1970 | */ |
| 1971 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1972 | { |
| 1973 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1974 | bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1975 | int i; |
| 1976 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1977 | amd_sched_entity_fini(vm->entity.sched, &vm->entity); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1978 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1979 | if (!RB_EMPTY_ROOT(&vm->va)) { |
| 1980 | dev_err(adev->dev, "still active bo inside vm\n"); |
| 1981 | } |
| 1982 | rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) { |
| 1983 | list_del(&mapping->list); |
| 1984 | interval_tree_remove(&mapping->it, &vm->va); |
| 1985 | kfree(mapping); |
| 1986 | } |
| 1987 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1988 | if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1989 | amdgpu_vm_prt_fini(adev, vm); |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1990 | prt_fini_needed = false; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1991 | } |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1992 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1993 | list_del(&mapping->list); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1994 | amdgpu_vm_free_mapping(adev, vm, mapping, NULL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1995 | } |
| 1996 | |
Christian König | 72a7ec5 | 2016-10-19 11:03:57 +0200 | [diff] [blame^] | 1997 | for (i = 0; i < amdgpu_vm_num_entries(adev, 0); i++) { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 1998 | struct amdgpu_bo *pt = vm->root.entries[i].bo; |
Christian König | 2698f62 | 2016-09-16 13:06:09 +0200 | [diff] [blame] | 1999 | |
| 2000 | if (!pt) |
| 2001 | continue; |
| 2002 | |
| 2003 | amdgpu_bo_unref(&pt->shadow); |
| 2004 | amdgpu_bo_unref(&pt); |
Chunming Zhou | 1baa439 | 2016-08-04 13:59:32 +0800 | [diff] [blame] | 2005 | } |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 2006 | drm_free_large(vm->root.entries); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2007 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 2008 | amdgpu_bo_unref(&vm->root.bo->shadow); |
| 2009 | amdgpu_bo_unref(&vm->root.bo); |
Christian König | a24960f | 2016-10-12 13:20:52 +0200 | [diff] [blame] | 2010 | dma_fence_put(vm->last_dir_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2011 | } |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2012 | |
| 2013 | /** |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2014 | * amdgpu_vm_manager_init - init the VM manager |
| 2015 | * |
| 2016 | * @adev: amdgpu_device pointer |
| 2017 | * |
| 2018 | * Initialize the VM manager structures |
| 2019 | */ |
| 2020 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) |
| 2021 | { |
| 2022 | unsigned i; |
| 2023 | |
| 2024 | INIT_LIST_HEAD(&adev->vm_manager.ids_lru); |
| 2025 | |
| 2026 | /* skip over VMID 0, since it is the system VM */ |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 2027 | for (i = 1; i < adev->vm_manager.num_ids; ++i) { |
| 2028 | amdgpu_vm_reset_id(adev, i); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 2029 | amdgpu_sync_create(&adev->vm_manager.ids[i].active); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2030 | list_add_tail(&adev->vm_manager.ids[i].list, |
| 2031 | &adev->vm_manager.ids_lru); |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 2032 | } |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2033 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2034 | adev->vm_manager.fence_context = |
| 2035 | dma_fence_context_alloc(AMDGPU_MAX_RINGS); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 2036 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 2037 | adev->vm_manager.seqno[i] = 0; |
| 2038 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2039 | atomic_set(&adev->vm_manager.vm_pte_next_ring, 0); |
Christian König | b1c8a81 | 2016-05-04 10:34:03 +0200 | [diff] [blame] | 2040 | atomic64_set(&adev->vm_manager.client_counter, 0); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 2041 | spin_lock_init(&adev->vm_manager.prt_lock); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 2042 | atomic_set(&adev->vm_manager.num_prt_users, 0); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | /** |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2046 | * amdgpu_vm_manager_fini - cleanup VM manager |
| 2047 | * |
| 2048 | * @adev: amdgpu_device pointer |
| 2049 | * |
| 2050 | * Cleanup the VM manager and free resources. |
| 2051 | */ |
| 2052 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) |
| 2053 | { |
| 2054 | unsigned i; |
| 2055 | |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 2056 | for (i = 0; i < AMDGPU_NUM_VM; ++i) { |
| 2057 | struct amdgpu_vm_id *id = &adev->vm_manager.ids[i]; |
| 2058 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2059 | dma_fence_put(adev->vm_manager.ids[i].first); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 2060 | amdgpu_sync_free(&adev->vm_manager.ids[i].active); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2061 | dma_fence_put(id->flushed_updates); |
Dave Airlie | 7b624ad | 2016-11-07 09:37:09 +1000 | [diff] [blame] | 2062 | dma_fence_put(id->last_flush); |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 2063 | } |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2064 | } |