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