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