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