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