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