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