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