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