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 | */ |
| 28 | #include <drm/drmP.h> |
| 29 | #include <drm/amdgpu_drm.h> |
| 30 | #include "amdgpu.h" |
| 31 | #include "amdgpu_trace.h" |
| 32 | |
| 33 | /* |
| 34 | * GPUVM |
| 35 | * GPUVM is similar to the legacy gart on older asics, however |
| 36 | * rather than there being a single global gart table |
| 37 | * for the entire GPU, there are multiple VM page tables active |
| 38 | * at any given time. The VM page tables can contain a mix |
| 39 | * vram pages and system memory pages and system memory pages |
| 40 | * can be mapped as snooped (cached system pages) or unsnooped |
| 41 | * (uncached system pages). |
| 42 | * Each VM has an ID associated with it and there is a page table |
| 43 | * associated with each VMID. When execting a command buffer, |
| 44 | * the kernel tells the the ring what VMID to use for that command |
| 45 | * buffer. VMIDs are allocated dynamically as commands are submitted. |
| 46 | * The userspace drivers maintain their own address space and the kernel |
| 47 | * sets up their pages tables accordingly when they submit their |
| 48 | * command buffers and a VMID is assigned. |
| 49 | * Cayman/Trinity support up to 8 active VMs at any given time; |
| 50 | * SI supports 16. |
| 51 | */ |
| 52 | |
| 53 | /** |
| 54 | * amdgpu_vm_num_pde - return the number of page directory entries |
| 55 | * |
| 56 | * @adev: amdgpu_device pointer |
| 57 | * |
| 58 | * Calculate the number of page directory entries (cayman+). |
| 59 | */ |
| 60 | static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev) |
| 61 | { |
| 62 | return adev->vm_manager.max_pfn >> amdgpu_vm_block_size; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * amdgpu_vm_directory_size - returns the size of the page directory in bytes |
| 67 | * |
| 68 | * @adev: amdgpu_device pointer |
| 69 | * |
| 70 | * Calculate the size of the page directory in bytes (cayman+). |
| 71 | */ |
| 72 | static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev) |
| 73 | { |
| 74 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8); |
| 75 | } |
| 76 | |
| 77 | /** |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 78 | * 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] | 79 | * |
| 80 | * @vm: vm providing the BOs |
Christian König | 3c0eea6 | 2015-12-11 14:39:05 +0100 | [diff] [blame] | 81 | * @validated: head of validation list |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 82 | * @entry: entry to add |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 83 | * |
| 84 | * Add the page directory to the list of BOs to |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 85 | * validate for command submission. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 86 | */ |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 87 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
| 88 | struct list_head *validated, |
| 89 | struct amdgpu_bo_list_entry *entry) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 90 | { |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 91 | entry->robj = vm->page_directory; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 92 | entry->priority = 0; |
| 93 | entry->tv.bo = &vm->page_directory->tbo; |
| 94 | entry->tv.shared = true; |
| 95 | list_add(&entry->tv.head, validated); |
| 96 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 97 | |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 98 | /** |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 99 | * amdgpu_vm_get_bos - add the vm BOs to a duplicates list |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 100 | * |
| 101 | * @vm: vm providing the BOs |
Christian König | 3c0eea6 | 2015-12-11 14:39:05 +0100 | [diff] [blame] | 102 | * @duplicates: head of duplicates list |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 103 | * |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 104 | * Add the page directory to the BO duplicates list |
| 105 | * for command submission. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 106 | */ |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 107 | void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 108 | { |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 109 | unsigned i; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 110 | |
| 111 | /* add the vm page table to the list */ |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 112 | for (i = 0; i <= vm->max_pde_used; ++i) { |
| 113 | struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 114 | |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 115 | if (!entry->robj) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 116 | continue; |
| 117 | |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 118 | list_add(&entry->tv.head, duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 119 | } |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 120 | |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail |
| 125 | * |
| 126 | * @adev: amdgpu device instance |
| 127 | * @vm: vm providing the BOs |
| 128 | * |
| 129 | * Move the PT BOs to the tail of the LRU. |
| 130 | */ |
| 131 | void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev, |
| 132 | struct amdgpu_vm *vm) |
| 133 | { |
| 134 | struct ttm_bo_global *glob = adev->mman.bdev.glob; |
| 135 | unsigned i; |
| 136 | |
| 137 | spin_lock(&glob->lru_lock); |
| 138 | for (i = 0; i <= vm->max_pde_used; ++i) { |
| 139 | struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry; |
| 140 | |
| 141 | if (!entry->robj) |
| 142 | continue; |
| 143 | |
| 144 | ttm_bo_move_to_lru_tail(&entry->robj->tbo); |
| 145 | } |
| 146 | spin_unlock(&glob->lru_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * amdgpu_vm_grab_id - allocate the next free VMID |
| 151 | * |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 152 | * @vm: vm to allocate id for |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 153 | * @ring: ring we want to submit job to |
| 154 | * @sync: sync object where we add dependencies |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 155 | * @fence: fence protecting ID from reuse |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 156 | * |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 157 | * 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] | 158 | * |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 159 | * Global mutex must be locked! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 160 | */ |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 161 | int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring, |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 162 | struct amdgpu_sync *sync, struct fence *fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 163 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 164 | struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx]; |
| 165 | struct amdgpu_device *adev = ring->adev; |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 166 | struct amdgpu_vm_manager_id *id; |
| 167 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 168 | |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 169 | mutex_lock(&adev->vm_manager.lock); |
| 170 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 171 | /* check if the id is still valid */ |
Christian König | 1c16c0a | 2015-11-14 21:31:40 +0100 | [diff] [blame] | 172 | if (vm_id->id) { |
Christian König | 1c16c0a | 2015-11-14 21:31:40 +0100 | [diff] [blame] | 173 | long owner; |
| 174 | |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 175 | id = &adev->vm_manager.ids[vm_id->id]; |
| 176 | owner = atomic_long_read(&id->owner); |
Christian König | 1c16c0a | 2015-11-14 21:31:40 +0100 | [diff] [blame] | 177 | if (owner == (long)vm) { |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 178 | list_move_tail(&id->list, &adev->vm_manager.ids_lru); |
Christian König | 165e4e0 | 2016-01-07 18:15:22 +0100 | [diff] [blame] | 179 | trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 180 | |
| 181 | fence_put(id->active); |
| 182 | id->active = fence_get(fence); |
| 183 | |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 184 | mutex_unlock(&adev->vm_manager.lock); |
Christian König | 1c16c0a | 2015-11-14 21:31:40 +0100 | [diff] [blame] | 185 | return 0; |
| 186 | } |
Christian König | 39ff844 | 2015-09-28 12:01:20 +0200 | [diff] [blame] | 187 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 188 | |
| 189 | /* we definately need to flush */ |
| 190 | vm_id->pd_gpu_addr = ~0ll; |
| 191 | |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 192 | id = list_first_entry(&adev->vm_manager.ids_lru, |
| 193 | struct amdgpu_vm_manager_id, |
| 194 | list); |
| 195 | list_move_tail(&id->list, &adev->vm_manager.ids_lru); |
| 196 | atomic_long_set(&id->owner, (long)vm); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 197 | |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 198 | vm_id->id = id - adev->vm_manager.ids; |
| 199 | trace_amdgpu_vm_grab_id(vm, vm_id->id, ring->idx); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 200 | |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 201 | r = amdgpu_sync_fence(ring->adev, sync, id->active); |
| 202 | |
| 203 | if (!r) { |
| 204 | fence_put(id->active); |
| 205 | id->active = fence_get(fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 208 | mutex_unlock(&adev->vm_manager.lock); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 209 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * amdgpu_vm_flush - hardware flush the vm |
| 214 | * |
| 215 | * @ring: ring to use for flush |
| 216 | * @vm: vm we want to flush |
| 217 | * @updates: last vm update that we waited for |
| 218 | * |
| 219 | * Flush the vm (cayman+). |
| 220 | * |
| 221 | * Global and local mutex must be locked! |
| 222 | */ |
| 223 | void amdgpu_vm_flush(struct amdgpu_ring *ring, |
| 224 | struct amdgpu_vm *vm, |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 225 | struct fence *updates) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 226 | { |
| 227 | uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory); |
| 228 | struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx]; |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 229 | struct fence *flushed_updates = vm_id->flushed_updates; |
Christian König | b56c228 | 2015-10-29 17:01:19 +0100 | [diff] [blame] | 230 | bool is_later; |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 231 | |
Christian König | b56c228 | 2015-10-29 17:01:19 +0100 | [diff] [blame] | 232 | if (!flushed_updates) |
| 233 | is_later = true; |
| 234 | else if (!updates) |
| 235 | is_later = false; |
| 236 | else |
| 237 | is_later = fence_is_later(updates, flushed_updates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 238 | |
Christian König | b56c228 | 2015-10-29 17:01:19 +0100 | [diff] [blame] | 239 | if (pd_addr != vm_id->pd_gpu_addr || is_later) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 240 | trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id); |
Christian König | b56c228 | 2015-10-29 17:01:19 +0100 | [diff] [blame] | 241 | if (is_later) { |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 242 | vm_id->flushed_updates = fence_get(updates); |
| 243 | fence_put(flushed_updates); |
| 244 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 245 | vm_id->pd_gpu_addr = pd_addr; |
| 246 | amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 251 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo |
| 252 | * |
| 253 | * @vm: requested vm |
| 254 | * @bo: requested buffer object |
| 255 | * |
| 256 | * Find @bo inside the requested vm (cayman+). |
| 257 | * Search inside the @bos vm list for the requested vm |
| 258 | * Returns the found bo_va or NULL if none is found |
| 259 | * |
| 260 | * Object has to be reserved! |
| 261 | */ |
| 262 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, |
| 263 | struct amdgpu_bo *bo) |
| 264 | { |
| 265 | struct amdgpu_bo_va *bo_va; |
| 266 | |
| 267 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
| 268 | if (bo_va->vm == vm) { |
| 269 | return bo_va; |
| 270 | } |
| 271 | } |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * amdgpu_vm_update_pages - helper to call the right asic function |
| 277 | * |
| 278 | * @adev: amdgpu_device pointer |
| 279 | * @ib: indirect buffer to fill with commands |
| 280 | * @pe: addr of the page entry |
| 281 | * @addr: dst addr to write into pe |
| 282 | * @count: number of page entries to update |
| 283 | * @incr: increase next addr by incr bytes |
| 284 | * @flags: hw access flags |
| 285 | * @gtt_flags: GTT hw access flags |
| 286 | * |
| 287 | * Traces the parameters and calls the right asic functions |
| 288 | * to setup the page table using the DMA. |
| 289 | */ |
| 290 | static void amdgpu_vm_update_pages(struct amdgpu_device *adev, |
| 291 | struct amdgpu_ib *ib, |
| 292 | uint64_t pe, uint64_t addr, |
| 293 | unsigned count, uint32_t incr, |
| 294 | uint32_t flags, uint32_t gtt_flags) |
| 295 | { |
| 296 | trace_amdgpu_vm_set_page(pe, addr, count, incr, flags); |
| 297 | |
| 298 | if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) { |
| 299 | uint64_t src = adev->gart.table_addr + (addr >> 12) * 8; |
| 300 | amdgpu_vm_copy_pte(adev, ib, pe, src, count); |
| 301 | |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame^] | 302 | } else if (flags & AMDGPU_PTE_SYSTEM) { |
| 303 | dma_addr_t *pages_addr = adev->gart.pages_addr; |
| 304 | amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr, |
| 305 | count, incr, flags); |
| 306 | |
| 307 | } else if (count < 3) { |
| 308 | amdgpu_vm_write_pte(adev, ib, NULL, pe, addr, |
| 309 | count, incr, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 310 | |
| 311 | } else { |
| 312 | amdgpu_vm_set_pte_pde(adev, ib, pe, addr, |
| 313 | count, incr, flags); |
| 314 | } |
| 315 | } |
| 316 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 317 | int amdgpu_vm_free_job(struct amdgpu_job *job) |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 318 | { |
| 319 | int i; |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 320 | for (i = 0; i < job->num_ibs; i++) |
| 321 | amdgpu_ib_free(job->adev, &job->ibs[i]); |
| 322 | kfree(job->ibs); |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 326 | /** |
| 327 | * amdgpu_vm_clear_bo - initially clear the page dir/table |
| 328 | * |
| 329 | * @adev: amdgpu_device pointer |
| 330 | * @bo: bo to clear |
Chunming Zhou | ef9f0a8 | 2015-11-13 13:43:22 +0800 | [diff] [blame] | 331 | * |
| 332 | * need to reserve bo first before calling it. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 333 | */ |
| 334 | static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, |
| 335 | struct amdgpu_bo *bo) |
| 336 | { |
| 337 | struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring; |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 338 | struct fence *fence = NULL; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 339 | struct amdgpu_ib *ib; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 340 | unsigned entries; |
| 341 | uint64_t addr; |
| 342 | int r; |
| 343 | |
monk.liu | ca95261 | 2015-05-25 14:44:05 +0800 | [diff] [blame] | 344 | r = reservation_object_reserve_shared(bo->tbo.resv); |
| 345 | if (r) |
| 346 | return r; |
| 347 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 348 | r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); |
| 349 | if (r) |
Chunming Zhou | ef9f0a8 | 2015-11-13 13:43:22 +0800 | [diff] [blame] | 350 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 351 | |
| 352 | addr = amdgpu_bo_gpu_offset(bo); |
| 353 | entries = amdgpu_bo_size(bo) / 8; |
| 354 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 355 | ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); |
| 356 | if (!ib) |
Chunming Zhou | ef9f0a8 | 2015-11-13 13:43:22 +0800 | [diff] [blame] | 357 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 358 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 359 | r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 360 | if (r) |
| 361 | goto error_free; |
| 362 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 363 | ib->length_dw = 0; |
| 364 | |
| 365 | amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0); |
| 366 | amdgpu_vm_pad_ib(adev, ib); |
| 367 | WARN_ON(ib->length_dw > 64); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 368 | r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, |
| 369 | &amdgpu_vm_free_job, |
| 370 | AMDGPU_FENCE_OWNER_VM, |
| 371 | &fence); |
| 372 | if (!r) |
| 373 | amdgpu_bo_fence(bo, fence, true); |
Chunming Zhou | 281b422 | 2015-08-12 12:58:31 +0800 | [diff] [blame] | 374 | fence_put(fence); |
Chunming Zhou | cadf97b | 2016-01-15 11:25:00 +0800 | [diff] [blame] | 375 | return 0; |
Chunming Zhou | ef9f0a8 | 2015-11-13 13:43:22 +0800 | [diff] [blame] | 376 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 377 | error_free: |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 378 | amdgpu_ib_free(adev, ib); |
| 379 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 380 | |
Chunming Zhou | ef9f0a8 | 2015-11-13 13:43:22 +0800 | [diff] [blame] | 381 | error: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 382 | return r; |
| 383 | } |
| 384 | |
| 385 | /** |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame^] | 386 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 387 | * |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame^] | 388 | * @pages_addr: optional DMA address to use for lookup |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 389 | * @addr: the unmapped addr |
| 390 | * |
| 391 | * 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^] | 392 | * to and return the pointer for the page table entry. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 393 | */ |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame^] | 394 | 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] | 395 | { |
| 396 | uint64_t result; |
| 397 | |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame^] | 398 | if (pages_addr) { |
| 399 | /* page table offset */ |
| 400 | result = pages_addr[addr >> PAGE_SHIFT]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 401 | |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame^] | 402 | /* in case cpu page size != gpu page size*/ |
| 403 | result |= addr & (~PAGE_MASK); |
| 404 | |
| 405 | } else { |
| 406 | /* No mapping required */ |
| 407 | result = addr; |
| 408 | } |
| 409 | |
| 410 | result &= 0xFFFFFFFFFFFFF000ULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 411 | |
| 412 | return result; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * amdgpu_vm_update_pdes - make sure that page directory is valid |
| 417 | * |
| 418 | * @adev: amdgpu_device pointer |
| 419 | * @vm: requested vm |
| 420 | * @start: start of GPU address range |
| 421 | * @end: end of GPU address range |
| 422 | * |
| 423 | * Allocates new page tables if necessary |
| 424 | * and updates the page directory (cayman+). |
| 425 | * Returns 0 for success, error for failure. |
| 426 | * |
| 427 | * Global and local mutex must be locked! |
| 428 | */ |
| 429 | int amdgpu_vm_update_page_directory(struct amdgpu_device *adev, |
| 430 | struct amdgpu_vm *vm) |
| 431 | { |
| 432 | struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring; |
| 433 | struct amdgpu_bo *pd = vm->page_directory; |
| 434 | uint64_t pd_addr = amdgpu_bo_gpu_offset(pd); |
| 435 | uint32_t incr = AMDGPU_VM_PTE_COUNT * 8; |
| 436 | uint64_t last_pde = ~0, last_pt = ~0; |
| 437 | unsigned count = 0, pt_idx, ndw; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 438 | struct amdgpu_ib *ib; |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 439 | struct fence *fence = NULL; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 440 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 441 | int r; |
| 442 | |
| 443 | /* padding, etc. */ |
| 444 | ndw = 64; |
| 445 | |
| 446 | /* assume the worst case */ |
| 447 | ndw += vm->max_pde_used * 6; |
| 448 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 449 | ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); |
| 450 | if (!ib) |
| 451 | return -ENOMEM; |
| 452 | |
| 453 | r = amdgpu_ib_get(ring, NULL, ndw * 4, ib); |
Sudip Mukherjee | 7a57455 | 2015-10-08 19:28:01 +0530 | [diff] [blame] | 454 | if (r) { |
| 455 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 456 | return r; |
Sudip Mukherjee | 7a57455 | 2015-10-08 19:28:01 +0530 | [diff] [blame] | 457 | } |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 458 | ib->length_dw = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 459 | |
| 460 | /* walk over the address space and update the page directory */ |
| 461 | for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) { |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 462 | struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 463 | uint64_t pde, pt; |
| 464 | |
| 465 | if (bo == NULL) |
| 466 | continue; |
| 467 | |
| 468 | pt = amdgpu_bo_gpu_offset(bo); |
| 469 | if (vm->page_tables[pt_idx].addr == pt) |
| 470 | continue; |
| 471 | vm->page_tables[pt_idx].addr = pt; |
| 472 | |
| 473 | pde = pd_addr + pt_idx * 8; |
| 474 | if (((last_pde + 8 * count) != pde) || |
| 475 | ((last_pt + incr * count) != pt)) { |
| 476 | |
| 477 | if (count) { |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 478 | amdgpu_vm_update_pages(adev, ib, last_pde, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 479 | last_pt, count, incr, |
| 480 | AMDGPU_PTE_VALID, 0); |
| 481 | } |
| 482 | |
| 483 | count = 1; |
| 484 | last_pde = pde; |
| 485 | last_pt = pt; |
| 486 | } else { |
| 487 | ++count; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (count) |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 492 | amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 493 | incr, AMDGPU_PTE_VALID, 0); |
| 494 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 495 | if (ib->length_dw != 0) { |
| 496 | amdgpu_vm_pad_ib(adev, ib); |
| 497 | amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM); |
| 498 | WARN_ON(ib->length_dw > ndw); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 499 | r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, |
| 500 | &amdgpu_vm_free_job, |
| 501 | AMDGPU_FENCE_OWNER_VM, |
| 502 | &fence); |
| 503 | if (r) |
| 504 | goto error_free; |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 505 | |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 506 | amdgpu_bo_fence(pd, fence, true); |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 507 | fence_put(vm->page_directory_fence); |
| 508 | vm->page_directory_fence = fence_get(fence); |
Chunming Zhou | 281b422 | 2015-08-12 12:58:31 +0800 | [diff] [blame] | 509 | fence_put(fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 510 | } |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 511 | |
Chunming Zhou | cadf97b | 2016-01-15 11:25:00 +0800 | [diff] [blame] | 512 | if (ib->length_dw == 0) { |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 513 | amdgpu_ib_free(adev, ib); |
| 514 | kfree(ib); |
| 515 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 516 | |
| 517 | return 0; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 518 | |
| 519 | error_free: |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 520 | amdgpu_ib_free(adev, ib); |
| 521 | kfree(ib); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 522 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
| 526 | * amdgpu_vm_frag_ptes - add fragment information to PTEs |
| 527 | * |
| 528 | * @adev: amdgpu_device pointer |
| 529 | * @ib: IB for the update |
| 530 | * @pe_start: first PTE to handle |
| 531 | * @pe_end: last PTE to handle |
| 532 | * @addr: addr those PTEs should point to |
| 533 | * @flags: hw mapping flags |
| 534 | * @gtt_flags: GTT hw mapping flags |
| 535 | * |
| 536 | * Global and local mutex must be locked! |
| 537 | */ |
| 538 | static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev, |
| 539 | struct amdgpu_ib *ib, |
| 540 | uint64_t pe_start, uint64_t pe_end, |
| 541 | uint64_t addr, uint32_t flags, |
| 542 | uint32_t gtt_flags) |
| 543 | { |
| 544 | /** |
| 545 | * The MC L1 TLB supports variable sized pages, based on a fragment |
| 546 | * field in the PTE. When this field is set to a non-zero value, page |
| 547 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE |
| 548 | * flags are considered valid for all PTEs within the fragment range |
| 549 | * and corresponding mappings are assumed to be physically contiguous. |
| 550 | * |
| 551 | * The L1 TLB can store a single PTE for the whole fragment, |
| 552 | * significantly increasing the space available for translation |
| 553 | * caching. This leads to large improvements in throughput when the |
| 554 | * TLB is under pressure. |
| 555 | * |
| 556 | * The L2 TLB distributes small and large fragments into two |
| 557 | * asymmetric partitions. The large fragment cache is significantly |
| 558 | * larger. Thus, we try to use large fragments wherever possible. |
| 559 | * Userspace can support this by aligning virtual base address and |
| 560 | * allocation size to the fragment size. |
| 561 | */ |
| 562 | |
| 563 | /* SI and newer are optimized for 64KB */ |
| 564 | uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB; |
| 565 | uint64_t frag_align = 0x80; |
| 566 | |
| 567 | uint64_t frag_start = ALIGN(pe_start, frag_align); |
| 568 | uint64_t frag_end = pe_end & ~(frag_align - 1); |
| 569 | |
| 570 | unsigned count; |
| 571 | |
| 572 | /* system pages are non continuously */ |
| 573 | if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) || |
| 574 | (frag_start >= frag_end)) { |
| 575 | |
| 576 | count = (pe_end - pe_start) / 8; |
| 577 | amdgpu_vm_update_pages(adev, ib, pe_start, addr, count, |
| 578 | AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags); |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | /* handle the 4K area at the beginning */ |
| 583 | if (pe_start != frag_start) { |
| 584 | count = (frag_start - pe_start) / 8; |
| 585 | amdgpu_vm_update_pages(adev, ib, pe_start, addr, count, |
| 586 | AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags); |
| 587 | addr += AMDGPU_GPU_PAGE_SIZE * count; |
| 588 | } |
| 589 | |
| 590 | /* handle the area in the middle */ |
| 591 | count = (frag_end - frag_start) / 8; |
| 592 | amdgpu_vm_update_pages(adev, ib, frag_start, addr, count, |
| 593 | AMDGPU_GPU_PAGE_SIZE, flags | frag_flags, |
| 594 | gtt_flags); |
| 595 | |
| 596 | /* handle the 4K area at the end */ |
| 597 | if (frag_end != pe_end) { |
| 598 | addr += AMDGPU_GPU_PAGE_SIZE * count; |
| 599 | count = (pe_end - frag_end) / 8; |
| 600 | amdgpu_vm_update_pages(adev, ib, frag_end, addr, count, |
| 601 | AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * amdgpu_vm_update_ptes - make sure that page tables are valid |
| 607 | * |
| 608 | * @adev: amdgpu_device pointer |
| 609 | * @vm: requested vm |
| 610 | * @start: start of GPU address range |
| 611 | * @end: end of GPU address range |
| 612 | * @dst: destination address to map to |
| 613 | * @flags: mapping flags |
| 614 | * |
| 615 | * Update the page tables in the range @start - @end (cayman+). |
| 616 | * |
| 617 | * Global and local mutex must be locked! |
| 618 | */ |
| 619 | static int amdgpu_vm_update_ptes(struct amdgpu_device *adev, |
| 620 | struct amdgpu_vm *vm, |
| 621 | struct amdgpu_ib *ib, |
| 622 | uint64_t start, uint64_t end, |
| 623 | uint64_t dst, uint32_t flags, |
| 624 | uint32_t gtt_flags) |
| 625 | { |
| 626 | uint64_t mask = AMDGPU_VM_PTE_COUNT - 1; |
| 627 | uint64_t last_pte = ~0, last_dst = ~0; |
Christian König | a60c423 | 2015-09-01 15:33:25 +0200 | [diff] [blame] | 628 | void *owner = AMDGPU_FENCE_OWNER_VM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 629 | unsigned count = 0; |
| 630 | uint64_t addr; |
| 631 | |
Christian König | a60c423 | 2015-09-01 15:33:25 +0200 | [diff] [blame] | 632 | /* sync to everything on unmapping */ |
| 633 | if (!(flags & AMDGPU_PTE_VALID)) |
| 634 | owner = AMDGPU_FENCE_OWNER_UNDEFINED; |
| 635 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 636 | /* walk over the address space and update the page tables */ |
| 637 | for (addr = start; addr < end; ) { |
| 638 | uint64_t pt_idx = addr >> amdgpu_vm_block_size; |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 639 | struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 640 | unsigned nptes; |
| 641 | uint64_t pte; |
| 642 | int r; |
| 643 | |
Christian König | a60c423 | 2015-09-01 15:33:25 +0200 | [diff] [blame] | 644 | amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 645 | r = reservation_object_reserve_shared(pt->tbo.resv); |
| 646 | if (r) |
| 647 | return r; |
| 648 | |
| 649 | if ((addr & ~mask) == (end & ~mask)) |
| 650 | nptes = end - addr; |
| 651 | else |
| 652 | nptes = AMDGPU_VM_PTE_COUNT - (addr & mask); |
| 653 | |
| 654 | pte = amdgpu_bo_gpu_offset(pt); |
| 655 | pte += (addr & mask) * 8; |
| 656 | |
| 657 | if ((last_pte + 8 * count) != pte) { |
| 658 | |
| 659 | if (count) { |
| 660 | amdgpu_vm_frag_ptes(adev, ib, last_pte, |
| 661 | last_pte + 8 * count, |
| 662 | last_dst, flags, |
| 663 | gtt_flags); |
| 664 | } |
| 665 | |
| 666 | count = nptes; |
| 667 | last_pte = pte; |
| 668 | last_dst = dst; |
| 669 | } else { |
| 670 | count += nptes; |
| 671 | } |
| 672 | |
| 673 | addr += nptes; |
| 674 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; |
| 675 | } |
| 676 | |
| 677 | if (count) { |
| 678 | amdgpu_vm_frag_ptes(adev, ib, last_pte, |
| 679 | last_pte + 8 * count, |
| 680 | last_dst, flags, gtt_flags); |
| 681 | } |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 687 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table |
| 688 | * |
| 689 | * @adev: amdgpu_device pointer |
| 690 | * @vm: requested vm |
| 691 | * @mapping: mapped range and flags to use for the update |
| 692 | * @addr: addr to set the area to |
| 693 | * @gtt_flags: flags as they are used for GTT |
| 694 | * @fence: optional resulting fence |
| 695 | * |
| 696 | * Fill in the page table entries for @mapping. |
| 697 | * Returns 0 for success, -EINVAL for failure. |
| 698 | * |
| 699 | * Object have to be reserved and mutex must be locked! |
| 700 | */ |
| 701 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, |
| 702 | struct amdgpu_vm *vm, |
| 703 | struct amdgpu_bo_va_mapping *mapping, |
| 704 | uint64_t addr, uint32_t gtt_flags, |
Chunming Zhou | bb1e38a4 | 2015-08-03 18:19:38 +0800 | [diff] [blame] | 705 | struct fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 706 | { |
| 707 | struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring; |
| 708 | unsigned nptes, ncmds, ndw; |
| 709 | uint32_t flags = gtt_flags; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 710 | struct amdgpu_ib *ib; |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 711 | struct fence *f = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 712 | int r; |
| 713 | |
| 714 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here |
| 715 | * but in case of something, we filter the flags in first place |
| 716 | */ |
| 717 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) |
| 718 | flags &= ~AMDGPU_PTE_READABLE; |
| 719 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) |
| 720 | flags &= ~AMDGPU_PTE_WRITEABLE; |
| 721 | |
| 722 | trace_amdgpu_vm_bo_update(mapping); |
| 723 | |
| 724 | nptes = mapping->it.last - mapping->it.start + 1; |
| 725 | |
| 726 | /* |
| 727 | * reserve space for one command every (1 << BLOCK_SIZE) |
| 728 | * entries or 2k dwords (whatever is smaller) |
| 729 | */ |
| 730 | ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1; |
| 731 | |
| 732 | /* padding, etc. */ |
| 733 | ndw = 64; |
| 734 | |
| 735 | if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) { |
| 736 | /* only copy commands needed */ |
| 737 | ndw += ncmds * 7; |
| 738 | |
| 739 | } else if (flags & AMDGPU_PTE_SYSTEM) { |
| 740 | /* header for write data commands */ |
| 741 | ndw += ncmds * 4; |
| 742 | |
| 743 | /* body of write data command */ |
| 744 | ndw += nptes * 2; |
| 745 | |
| 746 | } else { |
| 747 | /* set page commands needed */ |
| 748 | ndw += ncmds * 10; |
| 749 | |
| 750 | /* two extra commands for begin/end of fragment */ |
| 751 | ndw += 2 * 10; |
| 752 | } |
| 753 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 754 | ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); |
| 755 | if (!ib) |
| 756 | return -ENOMEM; |
| 757 | |
| 758 | r = amdgpu_ib_get(ring, NULL, ndw * 4, ib); |
| 759 | if (r) { |
| 760 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 761 | return r; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | ib->length_dw = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 765 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 766 | r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 767 | mapping->it.last + 1, addr + mapping->offset, |
| 768 | flags, gtt_flags); |
| 769 | |
| 770 | if (r) { |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 771 | amdgpu_ib_free(adev, ib); |
| 772 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 773 | return r; |
| 774 | } |
| 775 | |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 776 | amdgpu_vm_pad_ib(adev, ib); |
| 777 | WARN_ON(ib->length_dw > ndw); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 778 | r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, |
| 779 | &amdgpu_vm_free_job, |
| 780 | AMDGPU_FENCE_OWNER_VM, |
| 781 | &f); |
| 782 | if (r) |
| 783 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 784 | |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 785 | amdgpu_bo_fence(vm->page_directory, f, true); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 786 | if (fence) { |
| 787 | fence_put(*fence); |
| 788 | *fence = fence_get(f); |
| 789 | } |
Chunming Zhou | 281b422 | 2015-08-12 12:58:31 +0800 | [diff] [blame] | 790 | fence_put(f); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 791 | return 0; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 792 | |
| 793 | error_free: |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 794 | amdgpu_ib_free(adev, ib); |
| 795 | kfree(ib); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 796 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /** |
| 800 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table |
| 801 | * |
| 802 | * @adev: amdgpu_device pointer |
| 803 | * @bo_va: requested BO and VM object |
| 804 | * @mem: ttm mem |
| 805 | * |
| 806 | * Fill in the page table entries for @bo_va. |
| 807 | * Returns 0 for success, -EINVAL for failure. |
| 808 | * |
| 809 | * Object have to be reserved and mutex must be locked! |
| 810 | */ |
| 811 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, |
| 812 | struct amdgpu_bo_va *bo_va, |
| 813 | struct ttm_mem_reg *mem) |
| 814 | { |
| 815 | struct amdgpu_vm *vm = bo_va->vm; |
| 816 | struct amdgpu_bo_va_mapping *mapping; |
| 817 | uint32_t flags; |
| 818 | uint64_t addr; |
| 819 | int r; |
| 820 | |
| 821 | if (mem) { |
Christian König | b7d698d | 2015-09-07 12:32:09 +0200 | [diff] [blame] | 822 | addr = (u64)mem->start << PAGE_SHIFT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 823 | if (mem->mem_type != TTM_PL_TT) |
| 824 | addr += adev->vm_manager.vram_base_offset; |
| 825 | } else { |
| 826 | addr = 0; |
| 827 | } |
| 828 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 829 | flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem); |
| 830 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 831 | spin_lock(&vm->status_lock); |
| 832 | if (!list_empty(&bo_va->vm_status)) |
| 833 | list_splice_init(&bo_va->valids, &bo_va->invalids); |
| 834 | spin_unlock(&vm->status_lock); |
| 835 | |
| 836 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 837 | r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr, |
| 838 | flags, &bo_va->last_pt_update); |
| 839 | if (r) |
| 840 | return r; |
| 841 | } |
| 842 | |
Christian König | d6c10f6 | 2015-09-28 12:00:23 +0200 | [diff] [blame] | 843 | if (trace_amdgpu_vm_bo_mapping_enabled()) { |
| 844 | list_for_each_entry(mapping, &bo_va->valids, list) |
| 845 | trace_amdgpu_vm_bo_mapping(mapping); |
| 846 | |
| 847 | list_for_each_entry(mapping, &bo_va->invalids, list) |
| 848 | trace_amdgpu_vm_bo_mapping(mapping); |
| 849 | } |
| 850 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 851 | spin_lock(&vm->status_lock); |
monk.liu | 6d1d0ef | 2015-08-14 13:36:41 +0800 | [diff] [blame] | 852 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 853 | list_del_init(&bo_va->vm_status); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 854 | if (!mem) |
| 855 | list_add(&bo_va->vm_status, &vm->cleared); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 856 | spin_unlock(&vm->status_lock); |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * amdgpu_vm_clear_freed - clear freed BOs in the PT |
| 863 | * |
| 864 | * @adev: amdgpu_device pointer |
| 865 | * @vm: requested vm |
| 866 | * |
| 867 | * Make sure all freed BOs are cleared in the PT. |
| 868 | * Returns 0 for success. |
| 869 | * |
| 870 | * PTs have to be reserved and mutex must be locked! |
| 871 | */ |
| 872 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, |
| 873 | struct amdgpu_vm *vm) |
| 874 | { |
| 875 | struct amdgpu_bo_va_mapping *mapping; |
| 876 | int r; |
| 877 | |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 878 | spin_lock(&vm->freed_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 879 | while (!list_empty(&vm->freed)) { |
| 880 | mapping = list_first_entry(&vm->freed, |
| 881 | struct amdgpu_bo_va_mapping, list); |
| 882 | list_del(&mapping->list); |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 883 | spin_unlock(&vm->freed_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 884 | r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL); |
| 885 | kfree(mapping); |
| 886 | if (r) |
| 887 | return r; |
| 888 | |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 889 | spin_lock(&vm->freed_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 890 | } |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 891 | spin_unlock(&vm->freed_lock); |
| 892 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 893 | return 0; |
| 894 | |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT |
| 899 | * |
| 900 | * @adev: amdgpu_device pointer |
| 901 | * @vm: requested vm |
| 902 | * |
| 903 | * Make sure all invalidated BOs are cleared in the PT. |
| 904 | * Returns 0 for success. |
| 905 | * |
| 906 | * PTs have to be reserved and mutex must be locked! |
| 907 | */ |
| 908 | int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 909 | struct amdgpu_vm *vm, struct amdgpu_sync *sync) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 910 | { |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 911 | struct amdgpu_bo_va *bo_va = NULL; |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 912 | int r = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 913 | |
| 914 | spin_lock(&vm->status_lock); |
| 915 | while (!list_empty(&vm->invalidated)) { |
| 916 | bo_va = list_first_entry(&vm->invalidated, |
| 917 | struct amdgpu_bo_va, vm_status); |
| 918 | spin_unlock(&vm->status_lock); |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 919 | mutex_lock(&bo_va->mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 920 | r = amdgpu_vm_bo_update(adev, bo_va, NULL); |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 921 | mutex_unlock(&bo_va->mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 922 | if (r) |
| 923 | return r; |
| 924 | |
| 925 | spin_lock(&vm->status_lock); |
| 926 | } |
| 927 | spin_unlock(&vm->status_lock); |
| 928 | |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 929 | if (bo_va) |
Chunming Zhou | bb1e38a4 | 2015-08-03 18:19:38 +0800 | [diff] [blame] | 930 | r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update); |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 931 | |
| 932 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /** |
| 936 | * amdgpu_vm_bo_add - add a bo to a specific vm |
| 937 | * |
| 938 | * @adev: amdgpu_device pointer |
| 939 | * @vm: requested vm |
| 940 | * @bo: amdgpu buffer object |
| 941 | * |
| 942 | * Add @bo into the requested vm (cayman+). |
| 943 | * Add @bo to the list of bos associated with the vm |
| 944 | * Returns newly added bo_va or NULL for failure |
| 945 | * |
| 946 | * Object has to be reserved! |
| 947 | */ |
| 948 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, |
| 949 | struct amdgpu_vm *vm, |
| 950 | struct amdgpu_bo *bo) |
| 951 | { |
| 952 | struct amdgpu_bo_va *bo_va; |
| 953 | |
| 954 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); |
| 955 | if (bo_va == NULL) { |
| 956 | return NULL; |
| 957 | } |
| 958 | bo_va->vm = vm; |
| 959 | bo_va->bo = bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 960 | bo_va->ref_count = 1; |
| 961 | INIT_LIST_HEAD(&bo_va->bo_list); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 962 | INIT_LIST_HEAD(&bo_va->valids); |
| 963 | INIT_LIST_HEAD(&bo_va->invalids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 964 | INIT_LIST_HEAD(&bo_va->vm_status); |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 965 | mutex_init(&bo_va->mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 966 | list_add_tail(&bo_va->bo_list, &bo->va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 967 | |
| 968 | return bo_va; |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * amdgpu_vm_bo_map - map bo inside a vm |
| 973 | * |
| 974 | * @adev: amdgpu_device pointer |
| 975 | * @bo_va: bo_va to store the address |
| 976 | * @saddr: where to map the BO |
| 977 | * @offset: requested offset in the BO |
| 978 | * @flags: attributes of pages (read/write/valid/etc.) |
| 979 | * |
| 980 | * Add a mapping of the BO at the specefied addr into the VM. |
| 981 | * Returns 0 for success, error for failure. |
| 982 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 983 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 984 | */ |
| 985 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, |
| 986 | struct amdgpu_bo_va *bo_va, |
| 987 | uint64_t saddr, uint64_t offset, |
| 988 | uint64_t size, uint32_t flags) |
| 989 | { |
| 990 | struct amdgpu_bo_va_mapping *mapping; |
| 991 | struct amdgpu_vm *vm = bo_va->vm; |
| 992 | struct interval_tree_node *it; |
| 993 | unsigned last_pfn, pt_idx; |
| 994 | uint64_t eaddr; |
| 995 | int r; |
| 996 | |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 997 | /* validate the parameters */ |
| 998 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 999 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1000 | return -EINVAL; |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1001 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1002 | /* make sure object fit at this offset */ |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1003 | eaddr = saddr + size - 1; |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1004 | if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1005 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1006 | |
| 1007 | last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE; |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1008 | if (last_pfn >= adev->vm_manager.max_pfn) { |
| 1009 | dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n", |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1010 | last_pfn, adev->vm_manager.max_pfn); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1011 | return -EINVAL; |
| 1012 | } |
| 1013 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1014 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1015 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1016 | |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1017 | spin_lock(&vm->it_lock); |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1018 | it = interval_tree_iter_first(&vm->va, saddr, eaddr); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1019 | spin_unlock(&vm->it_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1020 | if (it) { |
| 1021 | struct amdgpu_bo_va_mapping *tmp; |
| 1022 | tmp = container_of(it, struct amdgpu_bo_va_mapping, it); |
| 1023 | /* bo and tmp overlap, invalid addr */ |
| 1024 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " |
| 1025 | "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr, |
| 1026 | tmp->it.start, tmp->it.last + 1); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1027 | r = -EINVAL; |
Chunming Zhou | f48b265 | 2015-10-16 14:06:19 +0800 | [diff] [blame] | 1028 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); |
| 1032 | if (!mapping) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1033 | r = -ENOMEM; |
Chunming Zhou | f48b265 | 2015-10-16 14:06:19 +0800 | [diff] [blame] | 1034 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | INIT_LIST_HEAD(&mapping->list); |
| 1038 | mapping->it.start = saddr; |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1039 | mapping->it.last = eaddr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1040 | mapping->offset = offset; |
| 1041 | mapping->flags = flags; |
| 1042 | |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 1043 | mutex_lock(&bo_va->mutex); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1044 | list_add(&mapping->list, &bo_va->invalids); |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 1045 | mutex_unlock(&bo_va->mutex); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1046 | spin_lock(&vm->it_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1047 | interval_tree_insert(&mapping->it, &vm->va); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1048 | spin_unlock(&vm->it_lock); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1049 | trace_amdgpu_vm_bo_map(bo_va, mapping); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1050 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1051 | /* Make sure the page tables are allocated */ |
| 1052 | saddr >>= amdgpu_vm_block_size; |
| 1053 | eaddr >>= amdgpu_vm_block_size; |
| 1054 | |
| 1055 | BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev)); |
| 1056 | |
| 1057 | if (eaddr > vm->max_pde_used) |
| 1058 | vm->max_pde_used = eaddr; |
| 1059 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1060 | /* walk over the address space and allocate the page tables */ |
| 1061 | for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) { |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 1062 | struct reservation_object *resv = vm->page_directory->tbo.resv; |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 1063 | struct amdgpu_bo_list_entry *entry; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1064 | struct amdgpu_bo *pt; |
| 1065 | |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 1066 | entry = &vm->page_tables[pt_idx].entry; |
| 1067 | if (entry->robj) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1068 | continue; |
| 1069 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1070 | r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8, |
| 1071 | AMDGPU_GPU_PAGE_SIZE, true, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 1072 | AMDGPU_GEM_DOMAIN_VRAM, |
| 1073 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS, |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 1074 | NULL, resv, &pt); |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1075 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1076 | goto error_free; |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1077 | |
Christian König | 82b9c55 | 2015-11-27 16:49:00 +0100 | [diff] [blame] | 1078 | /* Keep a reference to the page table to avoid freeing |
| 1079 | * them up in the wrong order. |
| 1080 | */ |
| 1081 | pt->parent = amdgpu_bo_ref(vm->page_directory); |
| 1082 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1083 | r = amdgpu_vm_clear_bo(adev, pt); |
| 1084 | if (r) { |
| 1085 | amdgpu_bo_unref(&pt); |
| 1086 | goto error_free; |
| 1087 | } |
| 1088 | |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 1089 | entry->robj = pt; |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 1090 | entry->priority = 0; |
| 1091 | entry->tv.bo = &entry->robj->tbo; |
| 1092 | entry->tv.shared = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1093 | vm->page_tables[pt_idx].addr = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1096 | return 0; |
| 1097 | |
| 1098 | error_free: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1099 | list_del(&mapping->list); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1100 | spin_lock(&vm->it_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1101 | interval_tree_remove(&mapping->it, &vm->va); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1102 | spin_unlock(&vm->it_lock); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1103 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1104 | kfree(mapping); |
| 1105 | |
Chunming Zhou | f48b265 | 2015-10-16 14:06:19 +0800 | [diff] [blame] | 1106 | error: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1107 | return r; |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * amdgpu_vm_bo_unmap - remove bo mapping from vm |
| 1112 | * |
| 1113 | * @adev: amdgpu_device pointer |
| 1114 | * @bo_va: bo_va to remove the address from |
| 1115 | * @saddr: where to the BO is mapped |
| 1116 | * |
| 1117 | * Remove a mapping of the BO at the specefied addr from the VM. |
| 1118 | * Returns 0 for success, error for failure. |
| 1119 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1120 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1121 | */ |
| 1122 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, |
| 1123 | struct amdgpu_bo_va *bo_va, |
| 1124 | uint64_t saddr) |
| 1125 | { |
| 1126 | struct amdgpu_bo_va_mapping *mapping; |
| 1127 | struct amdgpu_vm *vm = bo_va->vm; |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1128 | bool valid = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1129 | |
Christian König | 6c7fc50 | 2015-06-05 20:56:17 +0200 | [diff] [blame] | 1130 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 1131 | mutex_lock(&bo_va->mutex); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1132 | list_for_each_entry(mapping, &bo_va->valids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1133 | if (mapping->it.start == saddr) |
| 1134 | break; |
| 1135 | } |
| 1136 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1137 | if (&mapping->list == &bo_va->valids) { |
| 1138 | valid = false; |
| 1139 | |
| 1140 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
| 1141 | if (mapping->it.start == saddr) |
| 1142 | break; |
| 1143 | } |
| 1144 | |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 1145 | if (&mapping->list == &bo_va->invalids) { |
| 1146 | mutex_unlock(&bo_va->mutex); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1147 | return -ENOENT; |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 1148 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1149 | } |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 1150 | mutex_unlock(&bo_va->mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1151 | list_del(&mapping->list); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1152 | spin_lock(&vm->it_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1153 | interval_tree_remove(&mapping->it, &vm->va); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1154 | spin_unlock(&vm->it_lock); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1155 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1156 | |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 1157 | if (valid) { |
| 1158 | spin_lock(&vm->freed_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1159 | list_add(&mapping->list, &vm->freed); |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 1160 | spin_unlock(&vm->freed_lock); |
| 1161 | } else { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1162 | kfree(mapping); |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 1163 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1164 | |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm |
| 1170 | * |
| 1171 | * @adev: amdgpu_device pointer |
| 1172 | * @bo_va: requested bo_va |
| 1173 | * |
| 1174 | * Remove @bo_va->bo from the requested vm (cayman+). |
| 1175 | * |
| 1176 | * Object have to be reserved! |
| 1177 | */ |
| 1178 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, |
| 1179 | struct amdgpu_bo_va *bo_va) |
| 1180 | { |
| 1181 | struct amdgpu_bo_va_mapping *mapping, *next; |
| 1182 | struct amdgpu_vm *vm = bo_va->vm; |
| 1183 | |
| 1184 | list_del(&bo_va->bo_list); |
| 1185 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1186 | spin_lock(&vm->status_lock); |
| 1187 | list_del(&bo_va->vm_status); |
| 1188 | spin_unlock(&vm->status_lock); |
| 1189 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1190 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1191 | list_del(&mapping->list); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1192 | spin_lock(&vm->it_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1193 | interval_tree_remove(&mapping->it, &vm->va); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1194 | spin_unlock(&vm->it_lock); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1195 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 1196 | spin_lock(&vm->freed_lock); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1197 | list_add(&mapping->list, &vm->freed); |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 1198 | spin_unlock(&vm->freed_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1199 | } |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1200 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { |
| 1201 | list_del(&mapping->list); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1202 | spin_lock(&vm->it_lock); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1203 | interval_tree_remove(&mapping->it, &vm->va); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1204 | spin_unlock(&vm->it_lock); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1205 | kfree(mapping); |
| 1206 | } |
Chunming Zhou | bb1e38a4 | 2015-08-03 18:19:38 +0800 | [diff] [blame] | 1207 | fence_put(bo_va->last_pt_update); |
Chunming Zhou | 69b576a | 2015-11-18 11:17:39 +0800 | [diff] [blame] | 1208 | mutex_destroy(&bo_va->mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1209 | kfree(bo_va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * amdgpu_vm_bo_invalidate - mark the bo as invalid |
| 1214 | * |
| 1215 | * @adev: amdgpu_device pointer |
| 1216 | * @vm: requested vm |
| 1217 | * @bo: amdgpu buffer object |
| 1218 | * |
| 1219 | * Mark @bo as invalid (cayman+). |
| 1220 | */ |
| 1221 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, |
| 1222 | struct amdgpu_bo *bo) |
| 1223 | { |
| 1224 | struct amdgpu_bo_va *bo_va; |
| 1225 | |
| 1226 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1227 | spin_lock(&bo_va->vm->status_lock); |
| 1228 | if (list_empty(&bo_va->vm_status)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1229 | list_add(&bo_va->vm_status, &bo_va->vm->invalidated); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1230 | spin_unlock(&bo_va->vm->status_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * amdgpu_vm_init - initialize a vm instance |
| 1236 | * |
| 1237 | * @adev: amdgpu_device pointer |
| 1238 | * @vm: requested vm |
| 1239 | * |
| 1240 | * Init @vm fields (cayman+). |
| 1241 | */ |
| 1242 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1243 | { |
| 1244 | const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, |
| 1245 | AMDGPU_VM_PTE_COUNT * 8); |
Michel Dänzer | 9571e1d | 2016-01-19 17:59:46 +0900 | [diff] [blame] | 1246 | unsigned pd_size, pd_entries; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1247 | int i, r; |
| 1248 | |
| 1249 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
| 1250 | vm->ids[i].id = 0; |
| 1251 | vm->ids[i].flushed_updates = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1252 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1253 | vm->va = RB_ROOT; |
| 1254 | spin_lock_init(&vm->status_lock); |
| 1255 | INIT_LIST_HEAD(&vm->invalidated); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1256 | INIT_LIST_HEAD(&vm->cleared); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1257 | INIT_LIST_HEAD(&vm->freed); |
Chunming Zhou | c25867d | 2015-11-13 13:32:01 +0800 | [diff] [blame] | 1258 | spin_lock_init(&vm->it_lock); |
jimqu | 81d75a3 | 2015-12-04 17:17:00 +0800 | [diff] [blame] | 1259 | spin_lock_init(&vm->freed_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1260 | pd_size = amdgpu_vm_directory_size(adev); |
| 1261 | pd_entries = amdgpu_vm_num_pdes(adev); |
| 1262 | |
| 1263 | /* allocate page table array */ |
Michel Dänzer | 9571e1d | 2016-01-19 17:59:46 +0900 | [diff] [blame] | 1264 | vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt)); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1265 | if (vm->page_tables == NULL) { |
| 1266 | DRM_ERROR("Cannot allocate memory for page table array\n"); |
| 1267 | return -ENOMEM; |
| 1268 | } |
| 1269 | |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 1270 | vm->page_directory_fence = NULL; |
| 1271 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1272 | r = amdgpu_bo_create(adev, pd_size, align, true, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 1273 | AMDGPU_GEM_DOMAIN_VRAM, |
| 1274 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 1275 | NULL, NULL, &vm->page_directory); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1276 | if (r) |
| 1277 | return r; |
Chunming Zhou | ef9f0a8 | 2015-11-13 13:43:22 +0800 | [diff] [blame] | 1278 | r = amdgpu_bo_reserve(vm->page_directory, false); |
| 1279 | if (r) { |
| 1280 | amdgpu_bo_unref(&vm->page_directory); |
| 1281 | vm->page_directory = NULL; |
| 1282 | return r; |
| 1283 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1284 | r = amdgpu_vm_clear_bo(adev, vm->page_directory); |
Chunming Zhou | ef9f0a8 | 2015-11-13 13:43:22 +0800 | [diff] [blame] | 1285 | amdgpu_bo_unreserve(vm->page_directory); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1286 | if (r) { |
| 1287 | amdgpu_bo_unref(&vm->page_directory); |
| 1288 | vm->page_directory = NULL; |
| 1289 | return r; |
| 1290 | } |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * amdgpu_vm_fini - tear down a vm instance |
| 1297 | * |
| 1298 | * @adev: amdgpu_device pointer |
| 1299 | * @vm: requested vm |
| 1300 | * |
| 1301 | * Tear down @vm (cayman+). |
| 1302 | * Unbind the VM and remove all bos from the vm bo list |
| 1303 | */ |
| 1304 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1305 | { |
| 1306 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
| 1307 | int i; |
| 1308 | |
| 1309 | if (!RB_EMPTY_ROOT(&vm->va)) { |
| 1310 | dev_err(adev->dev, "still active bo inside vm\n"); |
| 1311 | } |
| 1312 | rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) { |
| 1313 | list_del(&mapping->list); |
| 1314 | interval_tree_remove(&mapping->it, &vm->va); |
| 1315 | kfree(mapping); |
| 1316 | } |
| 1317 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { |
| 1318 | list_del(&mapping->list); |
| 1319 | kfree(mapping); |
| 1320 | } |
| 1321 | |
| 1322 | for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 1323 | amdgpu_bo_unref(&vm->page_tables[i].entry.robj); |
Michel Dänzer | 9571e1d | 2016-01-19 17:59:46 +0900 | [diff] [blame] | 1324 | drm_free_large(vm->page_tables); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1325 | |
| 1326 | amdgpu_bo_unref(&vm->page_directory); |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 1327 | fence_put(vm->page_directory_fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1328 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
Christian König | 1c16c0a | 2015-11-14 21:31:40 +0100 | [diff] [blame] | 1329 | unsigned id = vm->ids[i].id; |
| 1330 | |
| 1331 | atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner, |
| 1332 | (long)vm, 0); |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 1333 | fence_put(vm->ids[i].flushed_updates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1334 | } |
| 1335 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1336 | } |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 1337 | |
| 1338 | /** |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 1339 | * amdgpu_vm_manager_init - init the VM manager |
| 1340 | * |
| 1341 | * @adev: amdgpu_device pointer |
| 1342 | * |
| 1343 | * Initialize the VM manager structures |
| 1344 | */ |
| 1345 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) |
| 1346 | { |
| 1347 | unsigned i; |
| 1348 | |
| 1349 | INIT_LIST_HEAD(&adev->vm_manager.ids_lru); |
| 1350 | |
| 1351 | /* skip over VMID 0, since it is the system VM */ |
| 1352 | for (i = 1; i < adev->vm_manager.num_ids; ++i) |
| 1353 | list_add_tail(&adev->vm_manager.ids[i].list, |
| 1354 | &adev->vm_manager.ids_lru); |
| 1355 | } |
| 1356 | |
| 1357 | /** |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 1358 | * amdgpu_vm_manager_fini - cleanup VM manager |
| 1359 | * |
| 1360 | * @adev: amdgpu_device pointer |
| 1361 | * |
| 1362 | * Cleanup the VM manager and free resources. |
| 1363 | */ |
| 1364 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) |
| 1365 | { |
| 1366 | unsigned i; |
| 1367 | |
| 1368 | for (i = 0; i < AMDGPU_NUM_VM; ++i) |
Christian König | 1c16c0a | 2015-11-14 21:31:40 +0100 | [diff] [blame] | 1369 | fence_put(adev->vm_manager.ids[i].active); |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 1370 | } |