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