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 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 28 | #include <linux/dma-fence-array.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 29 | #include <drm/drmP.h> |
| 30 | #include <drm/amdgpu_drm.h> |
| 31 | #include "amdgpu.h" |
| 32 | #include "amdgpu_trace.h" |
| 33 | |
| 34 | /* |
| 35 | * GPUVM |
| 36 | * GPUVM is similar to the legacy gart on older asics, however |
| 37 | * rather than there being a single global gart table |
| 38 | * for the entire GPU, there are multiple VM page tables active |
| 39 | * at any given time. The VM page tables can contain a mix |
| 40 | * vram pages and system memory pages and system memory pages |
| 41 | * can be mapped as snooped (cached system pages) or unsnooped |
| 42 | * (uncached system pages). |
| 43 | * Each VM has an ID associated with it and there is a page table |
| 44 | * associated with each VMID. When execting a command buffer, |
| 45 | * the kernel tells the the ring what VMID to use for that command |
| 46 | * buffer. VMIDs are allocated dynamically as commands are submitted. |
| 47 | * The userspace drivers maintain their own address space and the kernel |
| 48 | * sets up their pages tables accordingly when they submit their |
| 49 | * command buffers and a VMID is assigned. |
| 50 | * Cayman/Trinity support up to 8 active VMs at any given time; |
| 51 | * SI supports 16. |
| 52 | */ |
| 53 | |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 54 | /* Local structure. Encapsulate some VM table update parameters to reduce |
| 55 | * the number of function parameters |
| 56 | */ |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 57 | struct amdgpu_pte_update_params { |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 58 | /* amdgpu device we do this update for */ |
| 59 | struct amdgpu_device *adev; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 60 | /* optional amdgpu_vm we do this update for */ |
| 61 | struct amdgpu_vm *vm; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 62 | /* address where to copy page table entries from */ |
| 63 | uint64_t src; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 64 | /* indirect buffer to fill with commands */ |
| 65 | struct amdgpu_ib *ib; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 66 | /* Function which actually does the update */ |
| 67 | void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe, |
| 68 | uint64_t addr, unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 69 | uint64_t flags); |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 70 | /* indicate update pt or its shadow */ |
| 71 | bool shadow; |
Harish Kasiviswanathan | f4833c4 | 2016-04-21 10:40:18 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 74 | /* Helper to disable partial resident texture feature from a fence callback */ |
| 75 | struct amdgpu_prt_cb { |
| 76 | struct amdgpu_device *adev; |
| 77 | struct dma_fence_cb cb; |
| 78 | }; |
| 79 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 80 | /** |
| 81 | * amdgpu_vm_num_pde - return the number of page directory entries |
| 82 | * |
| 83 | * @adev: amdgpu_device pointer |
| 84 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 85 | * Calculate the number of page directory entries. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 86 | */ |
| 87 | static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev) |
| 88 | { |
| 89 | return adev->vm_manager.max_pfn >> amdgpu_vm_block_size; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * amdgpu_vm_directory_size - returns the size of the page directory in bytes |
| 94 | * |
| 95 | * @adev: amdgpu_device pointer |
| 96 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 97 | * Calculate the size of the page directory in bytes. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 98 | */ |
| 99 | static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev) |
| 100 | { |
| 101 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8); |
| 102 | } |
| 103 | |
| 104 | /** |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 105 | * 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] | 106 | * |
| 107 | * @vm: vm providing the BOs |
Christian König | 3c0eea6 | 2015-12-11 14:39:05 +0100 | [diff] [blame] | 108 | * @validated: head of validation list |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 109 | * @entry: entry to add |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 110 | * |
| 111 | * Add the page directory to the list of BOs to |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 112 | * validate for command submission. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 113 | */ |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 114 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
| 115 | struct list_head *validated, |
| 116 | struct amdgpu_bo_list_entry *entry) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 117 | { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 118 | entry->robj = vm->root.bo; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 119 | entry->priority = 0; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 120 | entry->tv.bo = &entry->robj->tbo; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 121 | entry->tv.shared = true; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 122 | entry->user_pages = NULL; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 123 | list_add(&entry->tv.head, validated); |
| 124 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 125 | |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 126 | /** |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 127 | * amdgpu_vm_validate_pt_bos - validate the page table BOs |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 128 | * |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 129 | * @adev: amdgpu device pointer |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 130 | * @vm: vm providing the BOs |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 131 | * @validate: callback to do the validation |
| 132 | * @param: parameter for the validation callback |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 133 | * |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 134 | * Validate the page table BOs on command submission if neccessary. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 135 | */ |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 136 | int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
| 137 | int (*validate)(void *p, struct amdgpu_bo *bo), |
| 138 | void *param) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 139 | { |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 140 | uint64_t num_evictions; |
Christian König | ee1782c | 2015-12-11 21:01:23 +0100 | [diff] [blame] | 141 | unsigned i; |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 142 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 143 | |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 144 | /* We only need to validate the page tables |
| 145 | * if they aren't already valid. |
| 146 | */ |
| 147 | num_evictions = atomic64_read(&adev->num_evictions); |
| 148 | if (num_evictions == vm->last_eviction_counter) |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 149 | return 0; |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 150 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 151 | /* add the vm page table to the list */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 152 | for (i = 0; i <= vm->root.last_entry_used; ++i) { |
| 153 | struct amdgpu_bo *bo = vm->root.entries[i].bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 154 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 155 | if (!bo) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 156 | continue; |
| 157 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 158 | r = validate(param, bo); |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 159 | if (r) |
| 160 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 161 | } |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 162 | |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 163 | return 0; |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail |
| 168 | * |
| 169 | * @adev: amdgpu device instance |
| 170 | * @vm: vm providing the BOs |
| 171 | * |
| 172 | * Move the PT BOs to the tail of the LRU. |
| 173 | */ |
| 174 | void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev, |
| 175 | struct amdgpu_vm *vm) |
| 176 | { |
| 177 | struct ttm_bo_global *glob = adev->mman.bdev.glob; |
| 178 | unsigned i; |
| 179 | |
| 180 | spin_lock(&glob->lru_lock); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 181 | for (i = 0; i <= vm->root.last_entry_used; ++i) { |
| 182 | struct amdgpu_bo *bo = vm->root.entries[i].bo; |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 183 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 184 | if (!bo) |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 185 | continue; |
| 186 | |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 187 | ttm_bo_move_to_lru_tail(&bo->tbo); |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 188 | } |
| 189 | spin_unlock(&glob->lru_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 192 | /** |
| 193 | * amdgpu_vm_alloc_pts - Allocate page tables. |
| 194 | * |
| 195 | * @adev: amdgpu_device pointer |
| 196 | * @vm: VM to allocate page tables for |
| 197 | * @saddr: Start address which needs to be allocated |
| 198 | * @size: Size from start address we need. |
| 199 | * |
| 200 | * Make sure the page tables are allocated. |
| 201 | */ |
| 202 | int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, |
| 203 | struct amdgpu_vm *vm, |
| 204 | uint64_t saddr, uint64_t size) |
| 205 | { |
| 206 | unsigned last_pfn, pt_idx; |
| 207 | uint64_t eaddr; |
| 208 | int r; |
| 209 | |
| 210 | /* validate the parameters */ |
| 211 | if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | eaddr = saddr + size - 1; |
| 215 | last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE; |
| 216 | if (last_pfn >= adev->vm_manager.max_pfn) { |
| 217 | dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n", |
| 218 | last_pfn, adev->vm_manager.max_pfn); |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 223 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 224 | |
| 225 | saddr >>= amdgpu_vm_block_size; |
| 226 | eaddr >>= amdgpu_vm_block_size; |
| 227 | |
| 228 | BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev)); |
| 229 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 230 | if (eaddr > vm->root.last_entry_used) |
| 231 | vm->root.last_entry_used = eaddr; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 232 | |
| 233 | /* walk over the address space and allocate the page tables */ |
| 234 | for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 235 | struct reservation_object *resv = vm->root.bo->tbo.resv; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 236 | struct amdgpu_bo *pt; |
| 237 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 238 | if (vm->root.entries[pt_idx].bo) |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 239 | continue; |
| 240 | |
| 241 | r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8, |
| 242 | AMDGPU_GPU_PAGE_SIZE, true, |
| 243 | AMDGPU_GEM_DOMAIN_VRAM, |
| 244 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
| 245 | AMDGPU_GEM_CREATE_SHADOW | |
| 246 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | |
| 247 | AMDGPU_GEM_CREATE_VRAM_CLEARED, |
| 248 | NULL, resv, &pt); |
| 249 | if (r) |
| 250 | return r; |
| 251 | |
| 252 | /* Keep a reference to the page table to avoid freeing |
| 253 | * them up in the wrong order. |
| 254 | */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 255 | pt->parent = amdgpu_bo_ref(vm->root.bo); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 256 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 257 | vm->root.entries[pt_idx].bo = pt; |
| 258 | vm->root.entries[pt_idx].addr = 0; |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Chunming Zhou | 192b7dc | 2016-06-29 14:01:15 +0800 | [diff] [blame] | 264 | static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev, |
| 265 | struct amdgpu_vm_id *id) |
| 266 | { |
| 267 | return id->current_gpu_reset_count != |
| 268 | atomic_read(&adev->gpu_reset_counter) ? true : false; |
| 269 | } |
| 270 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 271 | /** |
| 272 | * amdgpu_vm_grab_id - allocate the next free VMID |
| 273 | * |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 274 | * @vm: vm to allocate id for |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 275 | * @ring: ring we want to submit job to |
| 276 | * @sync: sync object where we add dependencies |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 277 | * @fence: fence protecting ID from reuse |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 278 | * |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 279 | * 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] | 280 | */ |
Christian König | 7f8a529 | 2015-07-20 16:09:40 +0200 | [diff] [blame] | 281 | int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 282 | struct amdgpu_sync *sync, struct dma_fence *fence, |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 283 | struct amdgpu_job *job) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 284 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 285 | struct amdgpu_device *adev = ring->adev; |
Christian König | 090b767 | 2016-07-08 10:21:02 +0200 | [diff] [blame] | 286 | uint64_t fence_context = adev->fence_context + ring->idx; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 287 | struct dma_fence *updates = sync->last_vm_update; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 288 | struct amdgpu_vm_id *id, *idle; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 289 | struct dma_fence **fences; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 290 | unsigned i; |
| 291 | int r = 0; |
| 292 | |
| 293 | fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids, |
| 294 | GFP_KERNEL); |
| 295 | if (!fences) |
| 296 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 297 | |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 298 | mutex_lock(&adev->vm_manager.lock); |
| 299 | |
Christian König | 36fd7c5 | 2016-05-23 15:30:08 +0200 | [diff] [blame] | 300 | /* Check if we have an idle VMID */ |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 301 | i = 0; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 302 | list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) { |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 303 | fences[i] = amdgpu_sync_peek_fence(&idle->active, ring); |
| 304 | if (!fences[i]) |
Christian König | 36fd7c5 | 2016-05-23 15:30:08 +0200 | [diff] [blame] | 305 | break; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 306 | ++i; |
Christian König | 36fd7c5 | 2016-05-23 15:30:08 +0200 | [diff] [blame] | 307 | } |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 308 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 309 | /* If we can't find a idle VMID to use, wait till one becomes available */ |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 310 | if (&idle->list == &adev->vm_manager.ids_lru) { |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 311 | u64 fence_context = adev->vm_manager.fence_context + ring->idx; |
| 312 | unsigned seqno = ++adev->vm_manager.seqno[ring->idx]; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 313 | struct dma_fence_array *array; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 314 | unsigned j; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 315 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 316 | for (j = 0; j < i; ++j) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 317 | dma_fence_get(fences[j]); |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 318 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 319 | array = dma_fence_array_create(i, fences, fence_context, |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 320 | seqno, true); |
| 321 | if (!array) { |
| 322 | for (j = 0; j < i; ++j) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 323 | dma_fence_put(fences[j]); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 324 | kfree(fences); |
| 325 | r = -ENOMEM; |
| 326 | goto error; |
| 327 | } |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 328 | |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 329 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 330 | r = amdgpu_sync_fence(ring->adev, sync, &array->base); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 331 | dma_fence_put(&array->base); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 332 | if (r) |
| 333 | goto error; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 334 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 335 | mutex_unlock(&adev->vm_manager.lock); |
| 336 | return 0; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 337 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 338 | } |
| 339 | kfree(fences); |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 340 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 341 | job->vm_needs_flush = true; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 342 | /* Check if we can use a VMID already assigned to this VM */ |
| 343 | i = ring->idx; |
| 344 | do { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 345 | struct dma_fence *flushed; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 346 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 347 | id = vm->ids[i++]; |
| 348 | if (i == AMDGPU_MAX_RINGS) |
| 349 | i = 0; |
| 350 | |
| 351 | /* Check all the prerequisites to using this VMID */ |
| 352 | if (!id) |
| 353 | continue; |
Chunming Zhou | 192b7dc | 2016-06-29 14:01:15 +0800 | [diff] [blame] | 354 | if (amdgpu_vm_is_gpu_reset(adev, id)) |
Chunming Zhou | 6adb051 | 2016-06-27 17:06:01 +0800 | [diff] [blame] | 355 | continue; |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 356 | |
| 357 | if (atomic64_read(&id->owner) != vm->client_id) |
| 358 | continue; |
| 359 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 360 | if (job->vm_pd_addr != id->pd_gpu_addr) |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 361 | continue; |
| 362 | |
Christian König | 090b767 | 2016-07-08 10:21:02 +0200 | [diff] [blame] | 363 | if (!id->last_flush) |
| 364 | continue; |
| 365 | |
| 366 | if (id->last_flush->context != fence_context && |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 367 | !dma_fence_is_signaled(id->last_flush)) |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 368 | continue; |
| 369 | |
| 370 | flushed = id->flushed_updates; |
| 371 | if (updates && |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 372 | (!flushed || dma_fence_is_later(updates, flushed))) |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 373 | continue; |
| 374 | |
Christian König | 3dab83b | 2016-06-01 13:31:17 +0200 | [diff] [blame] | 375 | /* Good we can use this VMID. Remember this submission as |
| 376 | * user of the VMID. |
| 377 | */ |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 378 | r = amdgpu_sync_fence(ring->adev, &id->active, fence); |
| 379 | if (r) |
| 380 | goto error; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 381 | |
Chunming Zhou | 6adb051 | 2016-06-27 17:06:01 +0800 | [diff] [blame] | 382 | id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 383 | list_move_tail(&id->list, &adev->vm_manager.ids_lru); |
| 384 | vm->ids[ring->idx] = id; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 385 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 386 | job->vm_id = id - adev->vm_manager.ids; |
| 387 | job->vm_needs_flush = false; |
Christian König | 0c0fdf1 | 2016-07-08 10:48:24 +0200 | [diff] [blame] | 388 | trace_amdgpu_vm_grab_id(vm, ring->idx, job); |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 389 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 390 | mutex_unlock(&adev->vm_manager.lock); |
| 391 | return 0; |
Christian König | 8d76001e | 2016-05-23 16:00:32 +0200 | [diff] [blame] | 392 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 393 | } while (i != ring->idx); |
Chunming Zhou | 8e9fbeb | 2016-03-17 11:41:37 +0800 | [diff] [blame] | 394 | |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 395 | /* Still no ID to use? Then use the idle one found earlier */ |
| 396 | id = idle; |
| 397 | |
| 398 | /* Remember this submission as user of the VMID */ |
| 399 | r = amdgpu_sync_fence(ring->adev, &id->active, fence); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 400 | if (r) |
| 401 | goto error; |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 402 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 403 | dma_fence_put(id->first); |
| 404 | id->first = dma_fence_get(fence); |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 405 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 406 | dma_fence_put(id->last_flush); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 407 | id->last_flush = NULL; |
| 408 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 409 | dma_fence_put(id->flushed_updates); |
| 410 | id->flushed_updates = dma_fence_get(updates); |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 411 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 412 | id->pd_gpu_addr = job->vm_pd_addr; |
Chunming Zhou | b46b8a8 | 2016-06-27 17:04:23 +0800 | [diff] [blame] | 413 | id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 414 | list_move_tail(&id->list, &adev->vm_manager.ids_lru); |
Christian König | 0ea54b9 | 2016-05-04 10:20:01 +0200 | [diff] [blame] | 415 | atomic64_set(&id->owner, vm->client_id); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 416 | vm->ids[ring->idx] = id; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 417 | |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 418 | job->vm_id = id - adev->vm_manager.ids; |
Christian König | 0c0fdf1 | 2016-07-08 10:48:24 +0200 | [diff] [blame] | 419 | trace_amdgpu_vm_grab_id(vm, ring->idx, job); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 420 | |
| 421 | error: |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame] | 422 | mutex_unlock(&adev->vm_manager.lock); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 423 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 424 | } |
| 425 | |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 426 | static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring) |
| 427 | { |
| 428 | struct amdgpu_device *adev = ring->adev; |
Alex Deucher | a125510 | 2016-10-13 17:41:13 -0400 | [diff] [blame] | 429 | const struct amdgpu_ip_block *ip_block; |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 430 | |
Christian König | 21cd942 | 2016-10-05 15:36:39 +0200 | [diff] [blame] | 431 | if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE) |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 432 | /* only compute rings */ |
| 433 | return false; |
| 434 | |
| 435 | ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); |
| 436 | if (!ip_block) |
| 437 | return false; |
| 438 | |
Alex Deucher | a125510 | 2016-10-13 17:41:13 -0400 | [diff] [blame] | 439 | if (ip_block->version->major <= 7) { |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 440 | /* gfx7 has no workaround */ |
| 441 | return true; |
Alex Deucher | a125510 | 2016-10-13 17:41:13 -0400 | [diff] [blame] | 442 | } else if (ip_block->version->major == 8) { |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 443 | if (adev->gfx.mec_fw_version >= 673) |
| 444 | /* gfx8 is fixed in MEC firmware 673 */ |
| 445 | return false; |
| 446 | else |
| 447 | return true; |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 452 | static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr) |
| 453 | { |
| 454 | u64 addr = mc_addr; |
| 455 | |
| 456 | if (adev->mc.mc_funcs && adev->mc.mc_funcs->adjust_mc_addr) |
| 457 | addr = adev->mc.mc_funcs->adjust_mc_addr(adev, addr); |
| 458 | |
| 459 | return addr; |
| 460 | } |
| 461 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 462 | /** |
| 463 | * amdgpu_vm_flush - hardware flush the vm |
| 464 | * |
| 465 | * @ring: ring to use for flush |
Christian König | cffadc8 | 2016-03-01 13:34:49 +0100 | [diff] [blame] | 466 | * @vm_id: vmid number to use |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 467 | * @pd_addr: address of the page directory |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 468 | * |
Christian König | 4ff37a8 | 2016-02-26 16:18:26 +0100 | [diff] [blame] | 469 | * Emit a VM flush when it is necessary. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 470 | */ |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 471 | int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 472 | { |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 473 | struct amdgpu_device *adev = ring->adev; |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 474 | struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id]; |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 475 | bool gds_switch_needed = ring->funcs->emit_gds_switch && ( |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 476 | id->gds_base != job->gds_base || |
| 477 | id->gds_size != job->gds_size || |
| 478 | id->gws_base != job->gws_base || |
| 479 | id->gws_size != job->gws_size || |
| 480 | id->oa_base != job->oa_base || |
| 481 | id->oa_size != job->oa_size); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 482 | int r; |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 483 | |
| 484 | if (ring->funcs->emit_pipeline_sync && ( |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 485 | job->vm_needs_flush || gds_switch_needed || |
Alex Deucher | 93dcc37 | 2016-06-17 17:05:15 -0400 | [diff] [blame] | 486 | amdgpu_vm_ring_has_compute_vm_bug(ring))) |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 487 | amdgpu_ring_emit_pipeline_sync(ring); |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 488 | |
Chunming Zhou | aa1c890 | 2016-06-30 13:56:02 +0800 | [diff] [blame] | 489 | if (ring->funcs->emit_vm_flush && (job->vm_needs_flush || |
| 490 | amdgpu_vm_is_gpu_reset(adev, id))) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 491 | struct dma_fence *fence; |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 492 | u64 pd_addr = amdgpu_vm_adjust_mc_addr(adev, job->vm_pd_addr); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 493 | |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 494 | trace_amdgpu_vm_flush(pd_addr, ring->idx, job->vm_id); |
| 495 | amdgpu_ring_emit_vm_flush(ring, job->vm_id, pd_addr); |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 496 | |
Christian König | 3dab83b | 2016-06-01 13:31:17 +0200 | [diff] [blame] | 497 | r = amdgpu_fence_emit(ring, &fence); |
| 498 | if (r) |
| 499 | return r; |
| 500 | |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 501 | mutex_lock(&adev->vm_manager.lock); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 502 | dma_fence_put(id->last_flush); |
Christian König | 3dab83b | 2016-06-01 13:31:17 +0200 | [diff] [blame] | 503 | id->last_flush = fence; |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 504 | mutex_unlock(&adev->vm_manager.lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 505 | } |
Christian König | cffadc8 | 2016-03-01 13:34:49 +0100 | [diff] [blame] | 506 | |
Christian König | d564a06 | 2016-03-01 15:51:53 +0100 | [diff] [blame] | 507 | if (gds_switch_needed) { |
Chunming Zhou | fd53be3 | 2016-07-01 17:59:01 +0800 | [diff] [blame] | 508 | id->gds_base = job->gds_base; |
| 509 | id->gds_size = job->gds_size; |
| 510 | id->gws_base = job->gws_base; |
| 511 | id->gws_size = job->gws_size; |
| 512 | id->oa_base = job->oa_base; |
| 513 | id->oa_size = job->oa_size; |
| 514 | amdgpu_ring_emit_gds_switch(ring, job->vm_id, |
| 515 | job->gds_base, job->gds_size, |
| 516 | job->gws_base, job->gws_size, |
| 517 | job->oa_base, job->oa_size); |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 518 | } |
Christian König | 41d9eb2 | 2016-03-01 16:46:18 +0100 | [diff] [blame] | 519 | |
| 520 | return 0; |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * amdgpu_vm_reset_id - reset VMID to zero |
| 525 | * |
| 526 | * @adev: amdgpu device structure |
| 527 | * @vm_id: vmid number to use |
| 528 | * |
| 529 | * Reset saved GDW, GWS and OA to force switch on next flush. |
| 530 | */ |
| 531 | void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id) |
| 532 | { |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 533 | struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id]; |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 534 | |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 535 | id->gds_base = 0; |
| 536 | id->gds_size = 0; |
| 537 | id->gws_base = 0; |
| 538 | id->gws_size = 0; |
| 539 | id->oa_base = 0; |
| 540 | id->oa_size = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 544 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo |
| 545 | * |
| 546 | * @vm: requested vm |
| 547 | * @bo: requested buffer object |
| 548 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 549 | * Find @bo inside the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 550 | * Search inside the @bos vm list for the requested vm |
| 551 | * Returns the found bo_va or NULL if none is found |
| 552 | * |
| 553 | * Object has to be reserved! |
| 554 | */ |
| 555 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, |
| 556 | struct amdgpu_bo *bo) |
| 557 | { |
| 558 | struct amdgpu_bo_va *bo_va; |
| 559 | |
| 560 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
| 561 | if (bo_va->vm == vm) { |
| 562 | return bo_va; |
| 563 | } |
| 564 | } |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | /** |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 569 | * amdgpu_vm_do_set_ptes - helper to call the right asic function |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 570 | * |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 571 | * @params: see amdgpu_pte_update_params definition |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 572 | * @pe: addr of the page entry |
| 573 | * @addr: dst addr to write into pe |
| 574 | * @count: number of page entries to update |
| 575 | * @incr: increase next addr by incr bytes |
| 576 | * @flags: hw access flags |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 577 | * |
| 578 | * Traces the parameters and calls the right asic functions |
| 579 | * to setup the page table using the DMA. |
| 580 | */ |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 581 | static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params, |
| 582 | uint64_t pe, uint64_t addr, |
| 583 | unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 584 | uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 585 | { |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 586 | trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 587 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 588 | if (count < 3) { |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 589 | amdgpu_vm_write_pte(params->adev, params->ib, pe, |
| 590 | addr | flags, count, incr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 591 | |
| 592 | } else { |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 593 | amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 594 | count, incr, flags); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /** |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 599 | * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART |
| 600 | * |
| 601 | * @params: see amdgpu_pte_update_params definition |
| 602 | * @pe: addr of the page entry |
| 603 | * @addr: dst addr to write into pe |
| 604 | * @count: number of page entries to update |
| 605 | * @incr: increase next addr by incr bytes |
| 606 | * @flags: hw access flags |
| 607 | * |
| 608 | * Traces the parameters and calls the DMA function to copy the PTEs. |
| 609 | */ |
| 610 | static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params, |
| 611 | uint64_t pe, uint64_t addr, |
| 612 | unsigned count, uint32_t incr, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 613 | uint64_t flags) |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 614 | { |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 615 | uint64_t src = (params->src + (addr >> 12) * 8); |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 616 | |
Christian König | ec2f05f | 2016-09-25 16:11:52 +0200 | [diff] [blame] | 617 | |
| 618 | trace_amdgpu_vm_copy_ptes(pe, src, count); |
| 619 | |
| 620 | amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count); |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 624 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 625 | * |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 626 | * @pages_addr: optional DMA address to use for lookup |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 627 | * @addr: the unmapped addr |
| 628 | * |
| 629 | * 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] | 630 | * to and return the pointer for the page table entry. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 631 | */ |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 632 | static 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] | 633 | { |
| 634 | uint64_t result; |
| 635 | |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 636 | /* page table offset */ |
| 637 | result = pages_addr[addr >> PAGE_SHIFT]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 638 | |
Christian König | de9ea7b | 2016-08-12 11:33:30 +0200 | [diff] [blame] | 639 | /* in case cpu page size != gpu page size*/ |
| 640 | result |= addr & (~PAGE_MASK); |
Christian König | b07c9d2 | 2015-11-30 13:26:07 +0100 | [diff] [blame] | 641 | |
| 642 | result &= 0xFFFFFFFFFFFFF000ULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 643 | |
| 644 | return result; |
| 645 | } |
| 646 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 647 | /* |
| 648 | * amdgpu_vm_update_pdes - make sure that page directory is valid |
| 649 | * |
| 650 | * @adev: amdgpu_device pointer |
| 651 | * @vm: requested vm |
| 652 | * @start: start of GPU address range |
| 653 | * @end: end of GPU address range |
| 654 | * |
| 655 | * Allocates new page tables if necessary |
| 656 | * and updates the page directory. |
| 657 | * Returns 0 for success, error for failure. |
| 658 | */ |
| 659 | int amdgpu_vm_update_page_directory(struct amdgpu_device *adev, |
| 660 | struct amdgpu_vm *vm) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 661 | { |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 662 | struct amdgpu_bo *shadow; |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 663 | struct amdgpu_ring *ring; |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 664 | uint64_t pd_addr, shadow_addr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 665 | uint32_t incr = AMDGPU_VM_PTE_COUNT * 8; |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 666 | uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 667 | unsigned count = 0, pt_idx, ndw; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 668 | struct amdgpu_job *job; |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 669 | struct amdgpu_pte_update_params params; |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 670 | struct dma_fence *fence = NULL; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 671 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 672 | int r; |
| 673 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 674 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 675 | shadow = vm->root.bo->shadow; |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 676 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 677 | /* padding, etc. */ |
| 678 | ndw = 64; |
| 679 | |
| 680 | /* assume the worst case */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 681 | ndw += vm->root.last_entry_used * 6; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 682 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 683 | pd_addr = amdgpu_bo_gpu_offset(vm->root.bo); |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 684 | if (shadow) { |
| 685 | r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem); |
| 686 | if (r) |
| 687 | return r; |
| 688 | shadow_addr = amdgpu_bo_gpu_offset(shadow); |
| 689 | ndw *= 2; |
| 690 | } else { |
| 691 | shadow_addr = 0; |
| 692 | } |
| 693 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 694 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
| 695 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 696 | return r; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 697 | |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 698 | memset(¶ms, 0, sizeof(params)); |
| 699 | params.adev = adev; |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 700 | params.ib = &job->ibs[0]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 701 | |
| 702 | /* walk over the address space and update the page directory */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 703 | for (pt_idx = 0; pt_idx <= vm->root.last_entry_used; ++pt_idx) { |
| 704 | struct amdgpu_bo *bo = vm->root.entries[pt_idx].bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 705 | uint64_t pde, pt; |
| 706 | |
| 707 | if (bo == NULL) |
| 708 | continue; |
| 709 | |
Christian König | 0fc8683 | 2016-09-16 11:46:23 +0200 | [diff] [blame] | 710 | if (bo->shadow) { |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 711 | struct amdgpu_bo *pt_shadow = bo->shadow; |
Christian König | 0fc8683 | 2016-09-16 11:46:23 +0200 | [diff] [blame] | 712 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 713 | r = amdgpu_ttm_bind(&pt_shadow->tbo, |
| 714 | &pt_shadow->tbo.mem); |
Christian König | 0fc8683 | 2016-09-16 11:46:23 +0200 | [diff] [blame] | 715 | if (r) |
| 716 | return r; |
| 717 | } |
| 718 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 719 | pt = amdgpu_bo_gpu_offset(bo); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 720 | if (vm->root.entries[pt_idx].addr == pt) |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 721 | continue; |
| 722 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 723 | vm->root.entries[pt_idx].addr = pt; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 724 | |
| 725 | pde = pd_addr + pt_idx * 8; |
| 726 | if (((last_pde + 8 * count) != pde) || |
Christian König | 96105e5 | 2016-08-12 12:59:59 +0200 | [diff] [blame] | 727 | ((last_pt + incr * count) != pt) || |
| 728 | (count == AMDGPU_VM_MAX_UPDATE_SIZE)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 729 | |
| 730 | if (count) { |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 731 | uint64_t pt_addr = |
| 732 | amdgpu_vm_adjust_mc_addr(adev, last_pt); |
| 733 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 734 | if (shadow) |
| 735 | amdgpu_vm_do_set_ptes(¶ms, |
| 736 | last_shadow, |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 737 | pt_addr, count, |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 738 | incr, |
| 739 | AMDGPU_PTE_VALID); |
| 740 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 741 | amdgpu_vm_do_set_ptes(¶ms, last_pde, |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 742 | pt_addr, count, incr, |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 743 | AMDGPU_PTE_VALID); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | count = 1; |
| 747 | last_pde = pde; |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 748 | last_shadow = shadow_addr + pt_idx * 8; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 749 | last_pt = pt; |
| 750 | } else { |
| 751 | ++count; |
| 752 | } |
| 753 | } |
| 754 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 755 | if (count) { |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 756 | uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt); |
| 757 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 758 | if (vm->root.bo->shadow) |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 759 | amdgpu_vm_do_set_ptes(¶ms, last_shadow, pt_addr, |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 760 | count, incr, AMDGPU_PTE_VALID); |
| 761 | |
Alex Xie | e60f8db | 2017-03-09 11:36:26 -0500 | [diff] [blame] | 762 | amdgpu_vm_do_set_ptes(¶ms, last_pde, pt_addr, |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 763 | count, incr, AMDGPU_PTE_VALID); |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 764 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 765 | |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 766 | if (params.ib->length_dw == 0) { |
| 767 | amdgpu_job_free(job); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | amdgpu_ring_pad_ib(ring, params.ib); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 772 | amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv, |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 773 | AMDGPU_FENCE_OWNER_VM); |
| 774 | if (shadow) |
| 775 | amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv, |
| 776 | AMDGPU_FENCE_OWNER_VM); |
| 777 | |
| 778 | WARN_ON(params.ib->length_dw > ndw); |
| 779 | r = amdgpu_job_submit(job, ring, &vm->entity, |
| 780 | AMDGPU_FENCE_OWNER_VM, &fence); |
| 781 | if (r) |
| 782 | goto error_free; |
| 783 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 784 | amdgpu_bo_fence(vm->root.bo, fence, true); |
Christian König | a24960f | 2016-10-12 13:20:52 +0200 | [diff] [blame] | 785 | dma_fence_put(vm->last_dir_update); |
| 786 | vm->last_dir_update = dma_fence_get(fence); |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 787 | dma_fence_put(fence); |
Christian König | f8991ba | 2016-09-16 15:36:49 +0200 | [diff] [blame] | 788 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 789 | return 0; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 790 | |
| 791 | error_free: |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 792 | amdgpu_job_free(job); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 793 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /** |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 797 | * amdgpu_vm_update_ptes - make sure that page tables are valid |
| 798 | * |
| 799 | * @params: see amdgpu_pte_update_params definition |
| 800 | * @vm: requested vm |
| 801 | * @start: start of GPU address range |
| 802 | * @end: end of GPU address range |
| 803 | * @dst: destination address to map to, the next dst inside the function |
| 804 | * @flags: mapping flags |
| 805 | * |
| 806 | * Update the page tables in the range @start - @end. |
| 807 | */ |
| 808 | static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 809 | uint64_t start, uint64_t end, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 810 | uint64_t dst, uint64_t flags) |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 811 | { |
| 812 | const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1; |
| 813 | |
| 814 | uint64_t cur_pe_start, cur_nptes, cur_dst; |
| 815 | uint64_t addr; /* next GPU address to be updated */ |
| 816 | uint64_t pt_idx; |
| 817 | struct amdgpu_bo *pt; |
| 818 | unsigned nptes; /* next number of ptes to be updated */ |
| 819 | uint64_t next_pe_start; |
| 820 | |
| 821 | /* initialize the variables */ |
| 822 | addr = start; |
| 823 | pt_idx = addr >> amdgpu_vm_block_size; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 824 | pt = params->vm->root.entries[pt_idx].bo; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 825 | if (params->shadow) { |
| 826 | if (!pt->shadow) |
| 827 | return; |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 828 | pt = pt->shadow; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 829 | } |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 830 | if ((addr & ~mask) == (end & ~mask)) |
| 831 | nptes = end - addr; |
| 832 | else |
| 833 | nptes = AMDGPU_VM_PTE_COUNT - (addr & mask); |
| 834 | |
| 835 | cur_pe_start = amdgpu_bo_gpu_offset(pt); |
| 836 | cur_pe_start += (addr & mask) * 8; |
| 837 | cur_nptes = nptes; |
| 838 | cur_dst = dst; |
| 839 | |
| 840 | /* for next ptb*/ |
| 841 | addr += nptes; |
| 842 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; |
| 843 | |
| 844 | /* walk over the address space and update the page tables */ |
| 845 | while (addr < end) { |
| 846 | pt_idx = addr >> amdgpu_vm_block_size; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 847 | pt = params->vm->root.entries[pt_idx].bo; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 848 | if (params->shadow) { |
| 849 | if (!pt->shadow) |
| 850 | return; |
Christian König | 914b4dc | 2016-09-28 12:27:37 +0200 | [diff] [blame] | 851 | pt = pt->shadow; |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 852 | } |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 853 | |
| 854 | if ((addr & ~mask) == (end & ~mask)) |
| 855 | nptes = end - addr; |
| 856 | else |
| 857 | nptes = AMDGPU_VM_PTE_COUNT - (addr & mask); |
| 858 | |
| 859 | next_pe_start = amdgpu_bo_gpu_offset(pt); |
| 860 | next_pe_start += (addr & mask) * 8; |
| 861 | |
Christian König | 96105e5 | 2016-08-12 12:59:59 +0200 | [diff] [blame] | 862 | if ((cur_pe_start + 8 * cur_nptes) == next_pe_start && |
| 863 | ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) { |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 864 | /* The next ptb is consecutive to current ptb. |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 865 | * Don't call the update function now. |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 866 | * Will update two ptbs together in future. |
| 867 | */ |
| 868 | cur_nptes += nptes; |
| 869 | } else { |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 870 | params->func(params, cur_pe_start, cur_dst, cur_nptes, |
| 871 | AMDGPU_GPU_PAGE_SIZE, flags); |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 872 | |
| 873 | cur_pe_start = next_pe_start; |
| 874 | cur_nptes = nptes; |
| 875 | cur_dst = dst; |
| 876 | } |
| 877 | |
| 878 | /* for next ptb*/ |
| 879 | addr += nptes; |
| 880 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; |
| 881 | } |
| 882 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 883 | params->func(params, cur_pe_start, cur_dst, cur_nptes, |
| 884 | AMDGPU_GPU_PAGE_SIZE, flags); |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 888 | * amdgpu_vm_frag_ptes - add fragment information to PTEs |
| 889 | * |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 890 | * @params: see amdgpu_pte_update_params definition |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 891 | * @vm: requested vm |
| 892 | * @start: first PTE to handle |
| 893 | * @end: last PTE to handle |
| 894 | * @dst: addr those PTEs should point to |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 895 | * @flags: hw mapping flags |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 896 | */ |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 897 | static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 898 | uint64_t start, uint64_t end, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 899 | uint64_t dst, uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 900 | { |
| 901 | /** |
| 902 | * The MC L1 TLB supports variable sized pages, based on a fragment |
| 903 | * field in the PTE. When this field is set to a non-zero value, page |
| 904 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE |
| 905 | * flags are considered valid for all PTEs within the fragment range |
| 906 | * and corresponding mappings are assumed to be physically contiguous. |
| 907 | * |
| 908 | * The L1 TLB can store a single PTE for the whole fragment, |
| 909 | * significantly increasing the space available for translation |
| 910 | * caching. This leads to large improvements in throughput when the |
| 911 | * TLB is under pressure. |
| 912 | * |
| 913 | * The L2 TLB distributes small and large fragments into two |
| 914 | * asymmetric partitions. The large fragment cache is significantly |
| 915 | * larger. Thus, we try to use large fragments wherever possible. |
| 916 | * Userspace can support this by aligning virtual base address and |
| 917 | * allocation size to the fragment size. |
| 918 | */ |
| 919 | |
Christian König | 8036617 | 2016-10-04 13:39:43 +0200 | [diff] [blame] | 920 | /* SI and newer are optimized for 64KB */ |
| 921 | uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG); |
| 922 | uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 923 | |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 924 | uint64_t frag_start = ALIGN(start, frag_align); |
| 925 | uint64_t frag_end = end & ~(frag_align - 1); |
Christian König | 31f6c1f | 2016-01-26 12:37:49 +0100 | [diff] [blame] | 926 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 927 | /* system pages are non continuously */ |
Christian König | b7fc2cb | 2016-08-11 16:44:15 +0200 | [diff] [blame] | 928 | if (params->src || !(flags & AMDGPU_PTE_VALID) || |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 929 | (frag_start >= frag_end)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 930 | |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 931 | amdgpu_vm_update_ptes(params, start, end, dst, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | |
| 935 | /* handle the 4K area at the beginning */ |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 936 | if (start != frag_start) { |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 937 | amdgpu_vm_update_ptes(params, start, frag_start, |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 938 | dst, flags); |
| 939 | dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* handle the area in the middle */ |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 943 | amdgpu_vm_update_ptes(params, frag_start, frag_end, dst, |
Christian König | 8036617 | 2016-10-04 13:39:43 +0200 | [diff] [blame] | 944 | flags | frag_flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 945 | |
| 946 | /* handle the 4K area at the end */ |
Christian König | 92696dd | 2016-08-05 13:56:35 +0200 | [diff] [blame] | 947 | if (frag_end != end) { |
| 948 | dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 949 | amdgpu_vm_update_ptes(params, frag_end, end, dst, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
| 953 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 954 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table |
| 955 | * |
| 956 | * @adev: amdgpu_device pointer |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 957 | * @exclusive: fence we need to sync to |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 958 | * @src: address where to copy page table entries from |
| 959 | * @pages_addr: DMA addresses to use for mapping |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 960 | * @vm: requested vm |
| 961 | * @start: start of mapped range |
| 962 | * @last: last mapped entry |
| 963 | * @flags: flags for the entries |
| 964 | * @addr: addr to set the area to |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 965 | * @fence: optional resulting fence |
| 966 | * |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 967 | * Fill in the page table entries between @start and @last. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 968 | * Returns 0 for success, -EINVAL for failure. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 969 | */ |
| 970 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 971 | struct dma_fence *exclusive, |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 972 | uint64_t src, |
| 973 | dma_addr_t *pages_addr, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 974 | struct amdgpu_vm *vm, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 975 | uint64_t start, uint64_t last, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 976 | uint64_t flags, uint64_t addr, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 977 | struct dma_fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 978 | { |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 979 | struct amdgpu_ring *ring; |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 980 | void *owner = AMDGPU_FENCE_OWNER_VM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 981 | unsigned nptes, ncmds, ndw; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 982 | struct amdgpu_job *job; |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 983 | struct amdgpu_pte_update_params params; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 984 | struct dma_fence *f = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 985 | int r; |
| 986 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 987 | memset(¶ms, 0, sizeof(params)); |
| 988 | params.adev = adev; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 989 | params.vm = vm; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 990 | params.src = src; |
| 991 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 992 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
Christian König | 27c5f36 | 2016-08-04 15:02:49 +0200 | [diff] [blame] | 993 | |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 994 | /* sync to everything on unmapping */ |
| 995 | if (!(flags & AMDGPU_PTE_VALID)) |
| 996 | owner = AMDGPU_FENCE_OWNER_UNDEFINED; |
| 997 | |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 998 | nptes = last - start + 1; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 999 | |
| 1000 | /* |
| 1001 | * reserve space for one command every (1 << BLOCK_SIZE) |
| 1002 | * entries or 2k dwords (whatever is smaller) |
| 1003 | */ |
| 1004 | ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1; |
| 1005 | |
| 1006 | /* padding, etc. */ |
| 1007 | ndw = 64; |
| 1008 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1009 | if (src) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1010 | /* only copy commands needed */ |
| 1011 | ndw += ncmds * 7; |
| 1012 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1013 | params.func = amdgpu_vm_do_copy_ptes; |
| 1014 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1015 | } else if (pages_addr) { |
| 1016 | /* copy commands needed */ |
| 1017 | ndw += ncmds * 7; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1018 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1019 | /* and also PTEs */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1020 | ndw += nptes * 2; |
| 1021 | |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1022 | params.func = amdgpu_vm_do_copy_ptes; |
| 1023 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1024 | } else { |
| 1025 | /* set page commands needed */ |
| 1026 | ndw += ncmds * 10; |
| 1027 | |
| 1028 | /* two extra commands for begin/end of fragment */ |
| 1029 | ndw += 2 * 10; |
Christian König | afef8b8 | 2016-08-12 13:29:18 +0200 | [diff] [blame] | 1030 | |
| 1031 | params.func = amdgpu_vm_do_set_ptes; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1032 | } |
| 1033 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1034 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
| 1035 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1036 | return r; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1037 | |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1038 | params.ib = &job->ibs[0]; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 1039 | |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1040 | if (!src && pages_addr) { |
| 1041 | uint64_t *pte; |
| 1042 | unsigned i; |
| 1043 | |
| 1044 | /* Put the PTEs at the end of the IB. */ |
| 1045 | i = ndw - nptes * 2; |
| 1046 | pte= (uint64_t *)&(job->ibs->ptr[i]); |
| 1047 | params.src = job->ibs->gpu_addr + i * 4; |
| 1048 | |
| 1049 | for (i = 0; i < nptes; ++i) { |
| 1050 | pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i * |
| 1051 | AMDGPU_GPU_PAGE_SIZE); |
| 1052 | pte[i] |= flags; |
| 1053 | } |
Christian König | d7a4ac6 | 2016-09-25 11:54:00 +0200 | [diff] [blame] | 1054 | addr = 0; |
Christian König | b0456f9 | 2016-08-11 14:06:54 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1057 | r = amdgpu_sync_fence(adev, &job->sync, exclusive); |
| 1058 | if (r) |
| 1059 | goto error_free; |
| 1060 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1061 | r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv, |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 1062 | owner); |
| 1063 | if (r) |
| 1064 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1065 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1066 | r = reservation_object_reserve_shared(vm->root.bo->tbo.resv); |
Christian König | a1e08d3 | 2016-01-26 11:40:46 +0100 | [diff] [blame] | 1067 | if (r) |
| 1068 | goto error_free; |
| 1069 | |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 1070 | params.shadow = true; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 1071 | amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
Chunming Zhou | 4c7e885 | 2016-08-15 11:46:21 +0800 | [diff] [blame] | 1072 | params.shadow = false; |
Christian König | 49ac8a2 | 2016-10-13 15:09:08 +0200 | [diff] [blame] | 1073 | amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1074 | |
Christian König | 29efc4f | 2016-08-04 14:52:50 +0200 | [diff] [blame] | 1075 | amdgpu_ring_pad_ib(ring, params.ib); |
| 1076 | WARN_ON(params.ib->length_dw > ndw); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1077 | r = amdgpu_job_submit(job, ring, &vm->entity, |
| 1078 | AMDGPU_FENCE_OWNER_VM, &f); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 1079 | if (r) |
| 1080 | goto error_free; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1081 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1082 | amdgpu_bo_fence(vm->root.bo, f, true); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1083 | dma_fence_put(*fence); |
| 1084 | *fence = f; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1085 | return 0; |
Chunming Zhou | d5fc5e8 | 2015-07-21 16:52:10 +0800 | [diff] [blame] | 1086 | |
| 1087 | error_free: |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1088 | amdgpu_job_free(job); |
Chunming Zhou | 4af9f07 | 2015-08-03 12:57:31 +0800 | [diff] [blame] | 1089 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /** |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1093 | * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks |
| 1094 | * |
| 1095 | * @adev: amdgpu_device pointer |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1096 | * @exclusive: fence we need to sync to |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1097 | * @gtt_flags: flags as they are used for GTT |
| 1098 | * @pages_addr: DMA addresses to use for mapping |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1099 | * @vm: requested vm |
| 1100 | * @mapping: mapped range and flags to use for the update |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1101 | * @flags: HW flags for the mapping |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1102 | * @nodes: array of drm_mm_nodes with the MC addresses |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1103 | * @fence: optional resulting fence |
| 1104 | * |
| 1105 | * Split the mapping into smaller chunks so that each update fits |
| 1106 | * into a SDMA IB. |
| 1107 | * Returns 0 for success, -EINVAL for failure. |
| 1108 | */ |
| 1109 | static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1110 | struct dma_fence *exclusive, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1111 | uint64_t gtt_flags, |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1112 | dma_addr_t *pages_addr, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1113 | struct amdgpu_vm *vm, |
| 1114 | struct amdgpu_bo_va_mapping *mapping, |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1115 | uint64_t flags, |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1116 | struct drm_mm_node *nodes, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1117 | struct dma_fence **fence) |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1118 | { |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1119 | uint64_t pfn, src = 0, start = mapping->it.start; |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1120 | int r; |
| 1121 | |
| 1122 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here |
| 1123 | * but in case of something, we filter the flags in first place |
| 1124 | */ |
| 1125 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) |
| 1126 | flags &= ~AMDGPU_PTE_READABLE; |
| 1127 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) |
| 1128 | flags &= ~AMDGPU_PTE_WRITEABLE; |
| 1129 | |
Alex Xie | 15b31c5 | 2017-03-03 16:47:11 -0500 | [diff] [blame] | 1130 | flags &= ~AMDGPU_PTE_EXECUTABLE; |
| 1131 | flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE; |
| 1132 | |
Alex Xie | b0fd18b | 2017-03-03 16:49:39 -0500 | [diff] [blame] | 1133 | flags &= ~AMDGPU_PTE_MTYPE_MASK; |
| 1134 | flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK); |
| 1135 | |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1136 | trace_amdgpu_vm_bo_update(mapping); |
| 1137 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1138 | pfn = mapping->offset >> PAGE_SHIFT; |
| 1139 | if (nodes) { |
| 1140 | while (pfn >= nodes->size) { |
| 1141 | pfn -= nodes->size; |
| 1142 | ++nodes; |
| 1143 | } |
Christian König | fa3ab3c | 2016-03-18 21:00:35 +0100 | [diff] [blame] | 1144 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1145 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1146 | do { |
| 1147 | uint64_t max_entries; |
| 1148 | uint64_t addr, last; |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1149 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1150 | if (nodes) { |
| 1151 | addr = nodes->start << PAGE_SHIFT; |
| 1152 | max_entries = (nodes->size - pfn) * |
| 1153 | (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); |
| 1154 | } else { |
| 1155 | addr = 0; |
| 1156 | max_entries = S64_MAX; |
| 1157 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1158 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1159 | if (pages_addr) { |
| 1160 | if (flags == gtt_flags) |
| 1161 | src = adev->gart.table_addr + |
| 1162 | (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8; |
| 1163 | else |
| 1164 | max_entries = min(max_entries, 16ull * 1024ull); |
| 1165 | addr = 0; |
| 1166 | } else if (flags & AMDGPU_PTE_VALID) { |
| 1167 | addr += adev->vm_manager.vram_base_offset; |
| 1168 | } |
| 1169 | addr += pfn << PAGE_SHIFT; |
| 1170 | |
| 1171 | last = min((uint64_t)mapping->it.last, start + max_entries - 1); |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1172 | r = amdgpu_vm_bo_update_mapping(adev, exclusive, |
| 1173 | src, pages_addr, vm, |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1174 | start, last, flags, addr, |
| 1175 | fence); |
| 1176 | if (r) |
| 1177 | return r; |
| 1178 | |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1179 | pfn += last - start + 1; |
| 1180 | if (nodes && nodes->size == pfn) { |
| 1181 | pfn = 0; |
| 1182 | ++nodes; |
| 1183 | } |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1184 | start = last + 1; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1185 | |
| 1186 | } while (unlikely(start != mapping->it.last + 1)); |
Christian König | a14faa6 | 2016-01-25 14:27:31 +0100 | [diff] [blame] | 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1192 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table |
| 1193 | * |
| 1194 | * @adev: amdgpu_device pointer |
| 1195 | * @bo_va: requested BO and VM object |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1196 | * @clear: if true clear the entries |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1197 | * |
| 1198 | * Fill in the page table entries for @bo_va. |
| 1199 | * Returns 0 for success, -EINVAL for failure. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1200 | */ |
| 1201 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, |
| 1202 | struct amdgpu_bo_va *bo_va, |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1203 | bool clear) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1204 | { |
| 1205 | struct amdgpu_vm *vm = bo_va->vm; |
| 1206 | struct amdgpu_bo_va_mapping *mapping; |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1207 | dma_addr_t *pages_addr = NULL; |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1208 | uint64_t gtt_flags, flags; |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1209 | struct ttm_mem_reg *mem; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1210 | struct drm_mm_node *nodes; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1211 | struct dma_fence *exclusive; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1212 | int r; |
| 1213 | |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1214 | if (clear || !bo_va->bo) { |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1215 | mem = NULL; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1216 | nodes = NULL; |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1217 | exclusive = NULL; |
| 1218 | } else { |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1219 | struct ttm_dma_tt *ttm; |
| 1220 | |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1221 | mem = &bo_va->bo->tbo.mem; |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1222 | nodes = mem->mm_node; |
| 1223 | if (mem->mem_type == TTM_PL_TT) { |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1224 | ttm = container_of(bo_va->bo->tbo.ttm, struct |
| 1225 | ttm_dma_tt, ttm); |
| 1226 | pages_addr = ttm->dma_address; |
Christian König | 9ab2146 | 2015-11-30 14:19:26 +0100 | [diff] [blame] | 1227 | } |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1228 | exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1229 | } |
| 1230 | |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1231 | if (bo_va->bo) { |
| 1232 | flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem); |
| 1233 | gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) && |
| 1234 | adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ? |
| 1235 | flags : 0; |
| 1236 | } else { |
| 1237 | flags = 0x0; |
| 1238 | gtt_flags = ~0x0; |
| 1239 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1240 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1241 | spin_lock(&vm->status_lock); |
| 1242 | if (!list_empty(&bo_va->vm_status)) |
| 1243 | list_splice_init(&bo_va->valids, &bo_va->invalids); |
| 1244 | spin_unlock(&vm->status_lock); |
| 1245 | |
| 1246 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1247 | r = amdgpu_vm_bo_split_mapping(adev, exclusive, |
| 1248 | gtt_flags, pages_addr, vm, |
Christian König | 63e0ba4 | 2016-08-16 17:38:37 +0200 | [diff] [blame] | 1249 | mapping, flags, nodes, |
Christian König | 8358dce | 2016-03-30 10:50:25 +0200 | [diff] [blame] | 1250 | &bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1251 | if (r) |
| 1252 | return r; |
| 1253 | } |
| 1254 | |
Christian König | d6c10f6 | 2015-09-28 12:00:23 +0200 | [diff] [blame] | 1255 | if (trace_amdgpu_vm_bo_mapping_enabled()) { |
| 1256 | list_for_each_entry(mapping, &bo_va->valids, list) |
| 1257 | trace_amdgpu_vm_bo_mapping(mapping); |
| 1258 | |
| 1259 | list_for_each_entry(mapping, &bo_va->invalids, list) |
| 1260 | trace_amdgpu_vm_bo_mapping(mapping); |
| 1261 | } |
| 1262 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1263 | spin_lock(&vm->status_lock); |
monk.liu | 6d1d0ef | 2015-08-14 13:36:41 +0800 | [diff] [blame] | 1264 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1265 | list_del_init(&bo_va->vm_status); |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1266 | if (clear) |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1267 | list_add(&bo_va->vm_status, &vm->cleared); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1268 | spin_unlock(&vm->status_lock); |
| 1269 | |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1274 | * amdgpu_vm_update_prt_state - update the global PRT state |
| 1275 | */ |
| 1276 | static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) |
| 1277 | { |
| 1278 | unsigned long flags; |
| 1279 | bool enable; |
| 1280 | |
| 1281 | spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1282 | enable = !!atomic_read(&adev->vm_manager.num_prt_users); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1283 | adev->gart.gart_funcs->set_prt(adev, enable); |
| 1284 | spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); |
| 1285 | } |
| 1286 | |
| 1287 | /** |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1288 | * amdgpu_vm_prt_get - add a PRT user |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1289 | */ |
| 1290 | static void amdgpu_vm_prt_get(struct amdgpu_device *adev) |
| 1291 | { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1292 | if (!adev->gart.gart_funcs->set_prt) |
| 1293 | return; |
| 1294 | |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1295 | if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) |
| 1296 | amdgpu_vm_update_prt_state(adev); |
| 1297 | } |
| 1298 | |
| 1299 | /** |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1300 | * amdgpu_vm_prt_put - drop a PRT user |
| 1301 | */ |
| 1302 | static void amdgpu_vm_prt_put(struct amdgpu_device *adev) |
| 1303 | { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1304 | if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1305 | amdgpu_vm_update_prt_state(adev); |
| 1306 | } |
| 1307 | |
| 1308 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1309 | * amdgpu_vm_prt_cb - callback for updating the PRT status |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1310 | */ |
| 1311 | static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) |
| 1312 | { |
| 1313 | struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); |
| 1314 | |
Christian König | 0b15f2f | 2017-02-14 15:47:03 +0100 | [diff] [blame] | 1315 | amdgpu_vm_prt_put(cb->adev); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1316 | kfree(cb); |
| 1317 | } |
| 1318 | |
| 1319 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1320 | * amdgpu_vm_add_prt_cb - add callback for updating the PRT status |
| 1321 | */ |
| 1322 | static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, |
| 1323 | struct dma_fence *fence) |
| 1324 | { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1325 | struct amdgpu_prt_cb *cb; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1326 | |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1327 | if (!adev->gart.gart_funcs->set_prt) |
| 1328 | return; |
| 1329 | |
| 1330 | cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1331 | if (!cb) { |
| 1332 | /* Last resort when we are OOM */ |
| 1333 | if (fence) |
| 1334 | dma_fence_wait(fence, false); |
| 1335 | |
| 1336 | amdgpu_vm_prt_put(cb->adev); |
| 1337 | } else { |
| 1338 | cb->adev = adev; |
| 1339 | if (!fence || dma_fence_add_callback(fence, &cb->cb, |
| 1340 | amdgpu_vm_prt_cb)) |
| 1341 | amdgpu_vm_prt_cb(fence, &cb->cb); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /** |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1346 | * amdgpu_vm_free_mapping - free a mapping |
| 1347 | * |
| 1348 | * @adev: amdgpu_device pointer |
| 1349 | * @vm: requested vm |
| 1350 | * @mapping: mapping to be freed |
| 1351 | * @fence: fence of the unmap operation |
| 1352 | * |
| 1353 | * Free a mapping and make sure we decrease the PRT usage count if applicable. |
| 1354 | */ |
| 1355 | static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, |
| 1356 | struct amdgpu_vm *vm, |
| 1357 | struct amdgpu_bo_va_mapping *mapping, |
| 1358 | struct dma_fence *fence) |
| 1359 | { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1360 | if (mapping->flags & AMDGPU_PTE_PRT) |
| 1361 | amdgpu_vm_add_prt_cb(adev, fence); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1362 | kfree(mapping); |
| 1363 | } |
| 1364 | |
| 1365 | /** |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1366 | * amdgpu_vm_prt_fini - finish all prt mappings |
| 1367 | * |
| 1368 | * @adev: amdgpu_device pointer |
| 1369 | * @vm: requested vm |
| 1370 | * |
| 1371 | * Register a cleanup callback to disable PRT support after VM dies. |
| 1372 | */ |
| 1373 | static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1374 | { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1375 | struct reservation_object *resv = vm->root.bo->tbo.resv; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1376 | struct dma_fence *excl, **shared; |
| 1377 | unsigned i, shared_count; |
| 1378 | int r; |
| 1379 | |
| 1380 | r = reservation_object_get_fences_rcu(resv, &excl, |
| 1381 | &shared_count, &shared); |
| 1382 | if (r) { |
| 1383 | /* Not enough memory to grab the fence list, as last resort |
| 1384 | * block for all the fences to complete. |
| 1385 | */ |
| 1386 | reservation_object_wait_timeout_rcu(resv, true, false, |
| 1387 | MAX_SCHEDULE_TIMEOUT); |
| 1388 | return; |
| 1389 | } |
| 1390 | |
| 1391 | /* Add a callback for each fence in the reservation object */ |
| 1392 | amdgpu_vm_prt_get(adev); |
| 1393 | amdgpu_vm_add_prt_cb(adev, excl); |
| 1394 | |
| 1395 | for (i = 0; i < shared_count; ++i) { |
| 1396 | amdgpu_vm_prt_get(adev); |
| 1397 | amdgpu_vm_add_prt_cb(adev, shared[i]); |
| 1398 | } |
| 1399 | |
| 1400 | kfree(shared); |
| 1401 | } |
| 1402 | |
| 1403 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1404 | * amdgpu_vm_clear_freed - clear freed BOs in the PT |
| 1405 | * |
| 1406 | * @adev: amdgpu_device pointer |
| 1407 | * @vm: requested vm |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1408 | * @fence: optional resulting fence (unchanged if no work needed to be done |
| 1409 | * or if an error occurred) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1410 | * |
| 1411 | * Make sure all freed BOs are cleared in the PT. |
| 1412 | * Returns 0 for success. |
| 1413 | * |
| 1414 | * PTs have to be reserved and mutex must be locked! |
| 1415 | */ |
| 1416 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1417 | struct amdgpu_vm *vm, |
| 1418 | struct dma_fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1419 | { |
| 1420 | struct amdgpu_bo_va_mapping *mapping; |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1421 | struct dma_fence *f = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1422 | int r; |
| 1423 | |
| 1424 | while (!list_empty(&vm->freed)) { |
| 1425 | mapping = list_first_entry(&vm->freed, |
| 1426 | struct amdgpu_bo_va_mapping, list); |
| 1427 | list_del(&mapping->list); |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 1428 | |
Christian König | 3cabaa5 | 2016-06-06 10:17:58 +0200 | [diff] [blame] | 1429 | r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping, |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1430 | 0, 0, &f); |
| 1431 | amdgpu_vm_free_mapping(adev, vm, mapping, f); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1432 | if (r) { |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1433 | dma_fence_put(f); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1434 | return r; |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1435 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1436 | } |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 1437 | |
| 1438 | if (fence && f) { |
| 1439 | dma_fence_put(*fence); |
| 1440 | *fence = f; |
| 1441 | } else { |
| 1442 | dma_fence_put(f); |
| 1443 | } |
| 1444 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1445 | return 0; |
| 1446 | |
| 1447 | } |
| 1448 | |
| 1449 | /** |
| 1450 | * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT |
| 1451 | * |
| 1452 | * @adev: amdgpu_device pointer |
| 1453 | * @vm: requested vm |
| 1454 | * |
| 1455 | * Make sure all invalidated BOs are cleared in the PT. |
| 1456 | * Returns 0 for success. |
| 1457 | * |
| 1458 | * PTs have to be reserved and mutex must be locked! |
| 1459 | */ |
| 1460 | int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 1461 | struct amdgpu_vm *vm, struct amdgpu_sync *sync) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1462 | { |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 1463 | struct amdgpu_bo_va *bo_va = NULL; |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 1464 | int r = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1465 | |
| 1466 | spin_lock(&vm->status_lock); |
| 1467 | while (!list_empty(&vm->invalidated)) { |
| 1468 | bo_va = list_first_entry(&vm->invalidated, |
| 1469 | struct amdgpu_bo_va, vm_status); |
| 1470 | spin_unlock(&vm->status_lock); |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1471 | |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 1472 | r = amdgpu_vm_bo_update(adev, bo_va, true); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1473 | if (r) |
| 1474 | return r; |
| 1475 | |
| 1476 | spin_lock(&vm->status_lock); |
| 1477 | } |
| 1478 | spin_unlock(&vm->status_lock); |
| 1479 | |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 1480 | if (bo_va) |
Chunming Zhou | bb1e38a4 | 2015-08-03 18:19:38 +0800 | [diff] [blame] | 1481 | r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update); |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 1482 | |
| 1483 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * amdgpu_vm_bo_add - add a bo to a specific vm |
| 1488 | * |
| 1489 | * @adev: amdgpu_device pointer |
| 1490 | * @vm: requested vm |
| 1491 | * @bo: amdgpu buffer object |
| 1492 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1493 | * Add @bo into the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1494 | * Add @bo to the list of bos associated with the vm |
| 1495 | * Returns newly added bo_va or NULL for failure |
| 1496 | * |
| 1497 | * Object has to be reserved! |
| 1498 | */ |
| 1499 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, |
| 1500 | struct amdgpu_vm *vm, |
| 1501 | struct amdgpu_bo *bo) |
| 1502 | { |
| 1503 | struct amdgpu_bo_va *bo_va; |
| 1504 | |
| 1505 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); |
| 1506 | if (bo_va == NULL) { |
| 1507 | return NULL; |
| 1508 | } |
| 1509 | bo_va->vm = vm; |
| 1510 | bo_va->bo = bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1511 | bo_va->ref_count = 1; |
| 1512 | INIT_LIST_HEAD(&bo_va->bo_list); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1513 | INIT_LIST_HEAD(&bo_va->valids); |
| 1514 | INIT_LIST_HEAD(&bo_va->invalids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1515 | INIT_LIST_HEAD(&bo_va->vm_status); |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1516 | |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1517 | if (bo) |
| 1518 | list_add_tail(&bo_va->bo_list, &bo->va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1519 | |
| 1520 | return bo_va; |
| 1521 | } |
| 1522 | |
| 1523 | /** |
| 1524 | * amdgpu_vm_bo_map - map bo inside a vm |
| 1525 | * |
| 1526 | * @adev: amdgpu_device pointer |
| 1527 | * @bo_va: bo_va to store the address |
| 1528 | * @saddr: where to map the BO |
| 1529 | * @offset: requested offset in the BO |
| 1530 | * @flags: attributes of pages (read/write/valid/etc.) |
| 1531 | * |
| 1532 | * Add a mapping of the BO at the specefied addr into the VM. |
| 1533 | * Returns 0 for success, error for failure. |
| 1534 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1535 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1536 | */ |
| 1537 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, |
| 1538 | struct amdgpu_bo_va *bo_va, |
| 1539 | uint64_t saddr, uint64_t offset, |
Christian König | 268c300 | 2017-01-18 14:49:43 +0100 | [diff] [blame] | 1540 | uint64_t size, uint64_t flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1541 | { |
| 1542 | struct amdgpu_bo_va_mapping *mapping; |
| 1543 | struct amdgpu_vm *vm = bo_va->vm; |
| 1544 | struct interval_tree_node *it; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1545 | uint64_t eaddr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1546 | |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1547 | /* validate the parameters */ |
| 1548 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1549 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1550 | return -EINVAL; |
Christian König | 0be52de | 2015-05-18 14:37:27 +0200 | [diff] [blame] | 1551 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1552 | /* make sure object fit at this offset */ |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1553 | eaddr = saddr + size - 1; |
Christian König | a5f6b5b | 2017-01-30 11:01:38 +0100 | [diff] [blame] | 1554 | if (saddr >= eaddr || |
| 1555 | (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo))) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1556 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1557 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1558 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1559 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1560 | |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1561 | it = interval_tree_iter_first(&vm->va, saddr, eaddr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1562 | if (it) { |
| 1563 | struct amdgpu_bo_va_mapping *tmp; |
| 1564 | tmp = container_of(it, struct amdgpu_bo_va_mapping, it); |
| 1565 | /* bo and tmp overlap, invalid addr */ |
| 1566 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " |
| 1567 | "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr, |
| 1568 | tmp->it.start, tmp->it.last + 1); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 1569 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 1573 | if (!mapping) |
| 1574 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1575 | |
| 1576 | INIT_LIST_HEAD(&mapping->list); |
| 1577 | mapping->it.start = saddr; |
Felix Kuehling | 005ae95 | 2015-11-23 17:43:48 -0500 | [diff] [blame] | 1578 | mapping->it.last = eaddr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1579 | mapping->offset = offset; |
| 1580 | mapping->flags = flags; |
| 1581 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1582 | list_add(&mapping->list, &bo_va->invalids); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1583 | interval_tree_insert(&mapping->it, &vm->va); |
| 1584 | |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1585 | if (flags & AMDGPU_PTE_PRT) |
| 1586 | amdgpu_vm_prt_get(adev); |
| 1587 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1588 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /** |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 1592 | * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings |
| 1593 | * |
| 1594 | * @adev: amdgpu_device pointer |
| 1595 | * @bo_va: bo_va to store the address |
| 1596 | * @saddr: where to map the BO |
| 1597 | * @offset: requested offset in the BO |
| 1598 | * @flags: attributes of pages (read/write/valid/etc.) |
| 1599 | * |
| 1600 | * Add a mapping of the BO at the specefied addr into the VM. Replace existing |
| 1601 | * mappings as we do so. |
| 1602 | * Returns 0 for success, error for failure. |
| 1603 | * |
| 1604 | * Object has to be reserved and unreserved outside! |
| 1605 | */ |
| 1606 | int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, |
| 1607 | struct amdgpu_bo_va *bo_va, |
| 1608 | uint64_t saddr, uint64_t offset, |
| 1609 | uint64_t size, uint64_t flags) |
| 1610 | { |
| 1611 | struct amdgpu_bo_va_mapping *mapping; |
| 1612 | struct amdgpu_vm *vm = bo_va->vm; |
| 1613 | uint64_t eaddr; |
| 1614 | int r; |
| 1615 | |
| 1616 | /* validate the parameters */ |
| 1617 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || |
| 1618 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
| 1619 | return -EINVAL; |
| 1620 | |
| 1621 | /* make sure object fit at this offset */ |
| 1622 | eaddr = saddr + size - 1; |
| 1623 | if (saddr >= eaddr || |
| 1624 | (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo))) |
| 1625 | return -EINVAL; |
| 1626 | |
| 1627 | /* Allocate all the needed memory */ |
| 1628 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); |
| 1629 | if (!mapping) |
| 1630 | return -ENOMEM; |
| 1631 | |
| 1632 | r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size); |
| 1633 | if (r) { |
| 1634 | kfree(mapping); |
| 1635 | return r; |
| 1636 | } |
| 1637 | |
| 1638 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1639 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1640 | |
| 1641 | mapping->it.start = saddr; |
| 1642 | mapping->it.last = eaddr; |
| 1643 | mapping->offset = offset; |
| 1644 | mapping->flags = flags; |
| 1645 | |
| 1646 | list_add(&mapping->list, &bo_va->invalids); |
| 1647 | interval_tree_insert(&mapping->it, &vm->va); |
| 1648 | |
| 1649 | if (flags & AMDGPU_PTE_PRT) |
| 1650 | amdgpu_vm_prt_get(adev); |
| 1651 | |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1656 | * amdgpu_vm_bo_unmap - remove bo mapping from vm |
| 1657 | * |
| 1658 | * @adev: amdgpu_device pointer |
| 1659 | * @bo_va: bo_va to remove the address from |
| 1660 | * @saddr: where to the BO is mapped |
| 1661 | * |
| 1662 | * Remove a mapping of the BO at the specefied addr from the VM. |
| 1663 | * Returns 0 for success, error for failure. |
| 1664 | * |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 1665 | * Object has to be reserved and unreserved outside! |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1666 | */ |
| 1667 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, |
| 1668 | struct amdgpu_bo_va *bo_va, |
| 1669 | uint64_t saddr) |
| 1670 | { |
| 1671 | struct amdgpu_bo_va_mapping *mapping; |
| 1672 | struct amdgpu_vm *vm = bo_va->vm; |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1673 | bool valid = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1674 | |
Christian König | 6c7fc50 | 2015-06-05 20:56:17 +0200 | [diff] [blame] | 1675 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1676 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1677 | list_for_each_entry(mapping, &bo_va->valids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1678 | if (mapping->it.start == saddr) |
| 1679 | break; |
| 1680 | } |
| 1681 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1682 | if (&mapping->list == &bo_va->valids) { |
| 1683 | valid = false; |
| 1684 | |
| 1685 | list_for_each_entry(mapping, &bo_va->invalids, list) { |
| 1686 | if (mapping->it.start == saddr) |
| 1687 | break; |
| 1688 | } |
| 1689 | |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1690 | if (&mapping->list == &bo_va->invalids) |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1691 | return -ENOENT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1692 | } |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1693 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1694 | list_del(&mapping->list); |
| 1695 | interval_tree_remove(&mapping->it, &vm->va); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1696 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1697 | |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 1698 | if (valid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1699 | list_add(&mapping->list, &vm->freed); |
Christian König | e17841b | 2016-03-08 17:52:01 +0100 | [diff] [blame] | 1700 | else |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1701 | amdgpu_vm_free_mapping(adev, vm, mapping, |
| 1702 | bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | /** |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1708 | * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range |
| 1709 | * |
| 1710 | * @adev: amdgpu_device pointer |
| 1711 | * @vm: VM structure to use |
| 1712 | * @saddr: start of the range |
| 1713 | * @size: size of the range |
| 1714 | * |
| 1715 | * Remove all mappings in a range, split them as appropriate. |
| 1716 | * Returns 0 for success, error for failure. |
| 1717 | */ |
| 1718 | int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, |
| 1719 | struct amdgpu_vm *vm, |
| 1720 | uint64_t saddr, uint64_t size) |
| 1721 | { |
| 1722 | struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; |
| 1723 | struct interval_tree_node *it; |
| 1724 | LIST_HEAD(removed); |
| 1725 | uint64_t eaddr; |
| 1726 | |
| 1727 | eaddr = saddr + size - 1; |
| 1728 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1729 | eaddr /= AMDGPU_GPU_PAGE_SIZE; |
| 1730 | |
| 1731 | /* Allocate all the needed memory */ |
| 1732 | before = kzalloc(sizeof(*before), GFP_KERNEL); |
| 1733 | if (!before) |
| 1734 | return -ENOMEM; |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1735 | INIT_LIST_HEAD(&before->list); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1736 | |
| 1737 | after = kzalloc(sizeof(*after), GFP_KERNEL); |
| 1738 | if (!after) { |
| 1739 | kfree(before); |
| 1740 | return -ENOMEM; |
| 1741 | } |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1742 | INIT_LIST_HEAD(&after->list); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1743 | |
| 1744 | /* Now gather all removed mappings */ |
| 1745 | it = interval_tree_iter_first(&vm->va, saddr, eaddr); |
| 1746 | while (it) { |
| 1747 | tmp = container_of(it, struct amdgpu_bo_va_mapping, it); |
| 1748 | it = interval_tree_iter_next(it, saddr, eaddr); |
| 1749 | |
| 1750 | /* Remember mapping split at the start */ |
| 1751 | if (tmp->it.start < saddr) { |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1752 | before->it.start = tmp->it.start; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1753 | before->it.last = saddr - 1; |
| 1754 | before->offset = tmp->offset; |
| 1755 | before->flags = tmp->flags; |
| 1756 | list_add(&before->list, &tmp->list); |
| 1757 | } |
| 1758 | |
| 1759 | /* Remember mapping split at the end */ |
| 1760 | if (tmp->it.last > eaddr) { |
| 1761 | after->it.start = eaddr + 1; |
| 1762 | after->it.last = tmp->it.last; |
| 1763 | after->offset = tmp->offset; |
| 1764 | after->offset += after->it.start - tmp->it.start; |
| 1765 | after->flags = tmp->flags; |
| 1766 | list_add(&after->list, &tmp->list); |
| 1767 | } |
| 1768 | |
| 1769 | list_del(&tmp->list); |
| 1770 | list_add(&tmp->list, &removed); |
| 1771 | } |
| 1772 | |
| 1773 | /* And free them up */ |
| 1774 | list_for_each_entry_safe(tmp, next, &removed, list) { |
| 1775 | interval_tree_remove(&tmp->it, &vm->va); |
| 1776 | list_del(&tmp->list); |
| 1777 | |
| 1778 | if (tmp->it.start < saddr) |
| 1779 | tmp->it.start = saddr; |
| 1780 | if (tmp->it.last > eaddr) |
| 1781 | tmp->it.last = eaddr; |
| 1782 | |
| 1783 | list_add(&tmp->list, &vm->freed); |
| 1784 | trace_amdgpu_vm_bo_unmap(NULL, tmp); |
| 1785 | } |
| 1786 | |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1787 | /* Insert partial mapping before the range */ |
| 1788 | if (!list_empty(&before->list)) { |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1789 | interval_tree_insert(&before->it, &vm->va); |
| 1790 | if (before->flags & AMDGPU_PTE_PRT) |
| 1791 | amdgpu_vm_prt_get(adev); |
| 1792 | } else { |
| 1793 | kfree(before); |
| 1794 | } |
| 1795 | |
| 1796 | /* Insert partial mapping after the range */ |
Junwei Zhang | 27f6d61 | 2017-03-16 16:09:24 +0800 | [diff] [blame] | 1797 | if (!list_empty(&after->list)) { |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 1798 | interval_tree_insert(&after->it, &vm->va); |
| 1799 | if (after->flags & AMDGPU_PTE_PRT) |
| 1800 | amdgpu_vm_prt_get(adev); |
| 1801 | } else { |
| 1802 | kfree(after); |
| 1803 | } |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1809 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm |
| 1810 | * |
| 1811 | * @adev: amdgpu_device pointer |
| 1812 | * @bo_va: requested bo_va |
| 1813 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1814 | * Remove @bo_va->bo from the requested vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1815 | * |
| 1816 | * Object have to be reserved! |
| 1817 | */ |
| 1818 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, |
| 1819 | struct amdgpu_bo_va *bo_va) |
| 1820 | { |
| 1821 | struct amdgpu_bo_va_mapping *mapping, *next; |
| 1822 | struct amdgpu_vm *vm = bo_va->vm; |
| 1823 | |
| 1824 | list_del(&bo_va->bo_list); |
| 1825 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1826 | spin_lock(&vm->status_lock); |
| 1827 | list_del(&bo_va->vm_status); |
| 1828 | spin_unlock(&vm->status_lock); |
| 1829 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1830 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1831 | list_del(&mapping->list); |
| 1832 | interval_tree_remove(&mapping->it, &vm->va); |
Christian König | 93e3e43 | 2015-06-09 16:58:33 +0200 | [diff] [blame] | 1833 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1834 | list_add(&mapping->list, &vm->freed); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1835 | } |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1836 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { |
| 1837 | list_del(&mapping->list); |
| 1838 | interval_tree_remove(&mapping->it, &vm->va); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1839 | amdgpu_vm_free_mapping(adev, vm, mapping, |
| 1840 | bo_va->last_pt_update); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1841 | } |
Christian König | 32b41ac | 2016-03-08 18:03:27 +0100 | [diff] [blame] | 1842 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1843 | dma_fence_put(bo_va->last_pt_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1844 | kfree(bo_va); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | /** |
| 1848 | * amdgpu_vm_bo_invalidate - mark the bo as invalid |
| 1849 | * |
| 1850 | * @adev: amdgpu_device pointer |
| 1851 | * @vm: requested vm |
| 1852 | * @bo: amdgpu buffer object |
| 1853 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1854 | * Mark @bo as invalid. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1855 | */ |
| 1856 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, |
| 1857 | struct amdgpu_bo *bo) |
| 1858 | { |
| 1859 | struct amdgpu_bo_va *bo_va; |
| 1860 | |
| 1861 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1862 | spin_lock(&bo_va->vm->status_lock); |
| 1863 | if (list_empty(&bo_va->vm_status)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1864 | list_add(&bo_va->vm_status, &bo_va->vm->invalidated); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1865 | spin_unlock(&bo_va->vm->status_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /** |
| 1870 | * amdgpu_vm_init - initialize a vm instance |
| 1871 | * |
| 1872 | * @adev: amdgpu_device pointer |
| 1873 | * @vm: requested vm |
| 1874 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1875 | * Init @vm fields. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1876 | */ |
| 1877 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1878 | { |
| 1879 | const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, |
| 1880 | AMDGPU_VM_PTE_COUNT * 8); |
Michel Dänzer | 9571e1d | 2016-01-19 17:59:46 +0900 | [diff] [blame] | 1881 | unsigned pd_size, pd_entries; |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1882 | unsigned ring_instance; |
| 1883 | struct amdgpu_ring *ring; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1884 | struct amd_sched_rq *rq; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1885 | int i, r; |
| 1886 | |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 1887 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 1888 | vm->ids[i] = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1889 | vm->va = RB_ROOT; |
Chunming Zhou | 031e298 | 2016-04-25 10:19:13 +0800 | [diff] [blame] | 1890 | vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1891 | spin_lock_init(&vm->status_lock); |
| 1892 | INIT_LIST_HEAD(&vm->invalidated); |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1893 | INIT_LIST_HEAD(&vm->cleared); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1894 | INIT_LIST_HEAD(&vm->freed); |
Christian König | 2025021 | 2016-03-08 17:58:35 +0100 | [diff] [blame] | 1895 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1896 | pd_size = amdgpu_vm_directory_size(adev); |
| 1897 | pd_entries = amdgpu_vm_num_pdes(adev); |
| 1898 | |
| 1899 | /* allocate page table array */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1900 | vm->root.entries = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt)); |
| 1901 | if (vm->root.entries == NULL) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1902 | DRM_ERROR("Cannot allocate memory for page table array\n"); |
| 1903 | return -ENOMEM; |
| 1904 | } |
| 1905 | |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1906 | /* create scheduler entity for page table updates */ |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1907 | |
| 1908 | ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring); |
| 1909 | ring_instance %= adev->vm_manager.vm_pte_num_rings; |
| 1910 | ring = adev->vm_manager.vm_pte_rings[ring_instance]; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1911 | rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL]; |
| 1912 | r = amd_sched_entity_init(&ring->sched, &vm->entity, |
| 1913 | rq, amdgpu_sched_jobs); |
| 1914 | if (r) |
Chunming Zhou | 64827ad | 2016-07-28 17:20:32 +0800 | [diff] [blame] | 1915 | goto err; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1916 | |
Christian König | a24960f | 2016-10-12 13:20:52 +0200 | [diff] [blame] | 1917 | vm->last_dir_update = NULL; |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 1918 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1919 | r = amdgpu_bo_create(adev, pd_size, align, true, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 1920 | AMDGPU_GEM_DOMAIN_VRAM, |
Chunming Zhou | 1baa439 | 2016-08-04 13:59:32 +0800 | [diff] [blame] | 1921 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 1922 | AMDGPU_GEM_CREATE_SHADOW | |
Christian König | 617859e | 2016-11-17 15:40:02 +0100 | [diff] [blame] | 1923 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | |
| 1924 | AMDGPU_GEM_CREATE_VRAM_CLEARED, |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1925 | NULL, NULL, &vm->root.bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1926 | if (r) |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1927 | goto error_free_sched_entity; |
| 1928 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1929 | r = amdgpu_bo_reserve(vm->root.bo, false); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1930 | if (r) |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1931 | goto error_free_root; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1932 | |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 1933 | vm->last_eviction_counter = atomic64_read(&adev->num_evictions); |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1934 | amdgpu_bo_unreserve(vm->root.bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1935 | |
| 1936 | return 0; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1937 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1938 | error_free_root: |
| 1939 | amdgpu_bo_unref(&vm->root.bo->shadow); |
| 1940 | amdgpu_bo_unref(&vm->root.bo); |
| 1941 | vm->root.bo = NULL; |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1942 | |
| 1943 | error_free_sched_entity: |
| 1944 | amd_sched_entity_fini(&ring->sched, &vm->entity); |
| 1945 | |
Chunming Zhou | 64827ad | 2016-07-28 17:20:32 +0800 | [diff] [blame] | 1946 | err: |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1947 | drm_free_large(vm->root.entries); |
Chunming Zhou | 64827ad | 2016-07-28 17:20:32 +0800 | [diff] [blame] | 1948 | |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1949 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * amdgpu_vm_fini - tear down a vm instance |
| 1954 | * |
| 1955 | * @adev: amdgpu_device pointer |
| 1956 | * @vm: requested vm |
| 1957 | * |
Christian König | 8843dbb | 2016-01-26 12:17:11 +0100 | [diff] [blame] | 1958 | * Tear down @vm. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1959 | * Unbind the VM and remove all bos from the vm bo list |
| 1960 | */ |
| 1961 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) |
| 1962 | { |
| 1963 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1964 | bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1965 | int i; |
| 1966 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1967 | amd_sched_entity_fini(vm->entity.sched, &vm->entity); |
Christian König | 2bd9ccf | 2016-02-01 12:53:58 +0100 | [diff] [blame] | 1968 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1969 | if (!RB_EMPTY_ROOT(&vm->va)) { |
| 1970 | dev_err(adev->dev, "still active bo inside vm\n"); |
| 1971 | } |
| 1972 | rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) { |
| 1973 | list_del(&mapping->list); |
| 1974 | interval_tree_remove(&mapping->it, &vm->va); |
| 1975 | kfree(mapping); |
| 1976 | } |
| 1977 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1978 | if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1979 | amdgpu_vm_prt_fini(adev, vm); |
Christian König | 4388fc2 | 2017-03-13 10:13:36 +0100 | [diff] [blame] | 1980 | prt_fini_needed = false; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1981 | } |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 1982 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1983 | list_del(&mapping->list); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 1984 | amdgpu_vm_free_mapping(adev, vm, mapping, NULL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1985 | } |
| 1986 | |
Chunming Zhou | 1baa439 | 2016-08-04 13:59:32 +0800 | [diff] [blame] | 1987 | for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) { |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1988 | struct amdgpu_bo *pt = vm->root.entries[i].bo; |
Christian König | 2698f62 | 2016-09-16 13:06:09 +0200 | [diff] [blame] | 1989 | |
| 1990 | if (!pt) |
| 1991 | continue; |
| 1992 | |
| 1993 | amdgpu_bo_unref(&pt->shadow); |
| 1994 | amdgpu_bo_unref(&pt); |
Chunming Zhou | 1baa439 | 2016-08-04 13:59:32 +0800 | [diff] [blame] | 1995 | } |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1996 | drm_free_large(vm->root.entries); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1997 | |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame^] | 1998 | amdgpu_bo_unref(&vm->root.bo->shadow); |
| 1999 | amdgpu_bo_unref(&vm->root.bo); |
Christian König | a24960f | 2016-10-12 13:20:52 +0200 | [diff] [blame] | 2000 | dma_fence_put(vm->last_dir_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2001 | } |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2002 | |
| 2003 | /** |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2004 | * amdgpu_vm_manager_init - init the VM manager |
| 2005 | * |
| 2006 | * @adev: amdgpu_device pointer |
| 2007 | * |
| 2008 | * Initialize the VM manager structures |
| 2009 | */ |
| 2010 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) |
| 2011 | { |
| 2012 | unsigned i; |
| 2013 | |
| 2014 | INIT_LIST_HEAD(&adev->vm_manager.ids_lru); |
| 2015 | |
| 2016 | /* skip over VMID 0, since it is the system VM */ |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 2017 | for (i = 1; i < adev->vm_manager.num_ids; ++i) { |
| 2018 | amdgpu_vm_reset_id(adev, i); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 2019 | amdgpu_sync_create(&adev->vm_manager.ids[i].active); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2020 | list_add_tail(&adev->vm_manager.ids[i].list, |
| 2021 | &adev->vm_manager.ids_lru); |
Christian König | 971fe9a9 | 2016-03-01 15:09:25 +0100 | [diff] [blame] | 2022 | } |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2023 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2024 | adev->vm_manager.fence_context = |
| 2025 | dma_fence_context_alloc(AMDGPU_MAX_RINGS); |
Christian König | 1fbb2e9 | 2016-06-01 10:47:36 +0200 | [diff] [blame] | 2026 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 2027 | adev->vm_manager.seqno[i] = 0; |
| 2028 | |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 2029 | atomic_set(&adev->vm_manager.vm_pte_next_ring, 0); |
Christian König | b1c8a81 | 2016-05-04 10:34:03 +0200 | [diff] [blame] | 2030 | atomic64_set(&adev->vm_manager.client_counter, 0); |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 2031 | spin_lock_init(&adev->vm_manager.prt_lock); |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 2032 | atomic_set(&adev->vm_manager.num_prt_users, 0); |
Christian König | a9a78b3 | 2016-01-21 10:19:11 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | /** |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2036 | * amdgpu_vm_manager_fini - cleanup VM manager |
| 2037 | * |
| 2038 | * @adev: amdgpu_device pointer |
| 2039 | * |
| 2040 | * Cleanup the VM manager and free resources. |
| 2041 | */ |
| 2042 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) |
| 2043 | { |
| 2044 | unsigned i; |
| 2045 | |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 2046 | for (i = 0; i < AMDGPU_NUM_VM; ++i) { |
| 2047 | struct amdgpu_vm_id *id = &adev->vm_manager.ids[i]; |
| 2048 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2049 | dma_fence_put(adev->vm_manager.ids[i].first); |
Christian König | 832a902 | 2016-02-15 12:33:02 +0100 | [diff] [blame] | 2050 | amdgpu_sync_free(&adev->vm_manager.ids[i].active); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 2051 | dma_fence_put(id->flushed_updates); |
Dave Airlie | 7b624ad | 2016-11-07 09:37:09 +1000 | [diff] [blame] | 2052 | dma_fence_put(id->last_flush); |
Christian König | bcb1ba3 | 2016-03-08 15:40:11 +0100 | [diff] [blame] | 2053 | } |
Christian König | ea89f8c | 2015-11-15 20:52:06 +0100 | [diff] [blame] | 2054 | } |