blob: b83e0fa1f269ff2eba40aec5bc0686b486bca943 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence-array.h>
Christian Königa9f87f62017-03-30 14:03:59 +020029#include <linux/interval_tree_generic.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030#include <drm/drmP.h>
31#include <drm/amdgpu_drm.h>
32#include "amdgpu.h"
33#include "amdgpu_trace.h"
34
35/*
36 * GPUVM
37 * GPUVM is similar to the legacy gart on older asics, however
38 * rather than there being a single global gart table
39 * for the entire GPU, there are multiple VM page tables active
40 * at any given time. The VM page tables can contain a mix
41 * vram pages and system memory pages and system memory pages
42 * can be mapped as snooped (cached system pages) or unsnooped
43 * (uncached system pages).
44 * Each VM has an ID associated with it and there is a page table
45 * associated with each VMID. When execting a command buffer,
46 * the kernel tells the the ring what VMID to use for that command
47 * buffer. VMIDs are allocated dynamically as commands are submitted.
48 * The userspace drivers maintain their own address space and the kernel
49 * sets up their pages tables accordingly when they submit their
50 * command buffers and a VMID is assigned.
51 * Cayman/Trinity support up to 8 active VMs at any given time;
52 * SI supports 16.
53 */
54
Christian Königa9f87f62017-03-30 14:03:59 +020055#define START(node) ((node)->start)
56#define LAST(node) ((node)->last)
57
58INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
59 START, LAST, static, amdgpu_vm_it)
60
61#undef START
62#undef LAST
63
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040064/* Local structure. Encapsulate some VM table update parameters to reduce
65 * the number of function parameters
66 */
Christian König29efc4f2016-08-04 14:52:50 +020067struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020068 /* amdgpu device we do this update for */
69 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +020070 /* optional amdgpu_vm we do this update for */
71 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040072 /* address where to copy page table entries from */
73 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040074 /* indirect buffer to fill with commands */
75 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020076 /* Function which actually does the update */
77 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
78 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080079 uint64_t flags);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -040080 /* The next two are used during VM update by CPU
81 * DMA addresses to use for mapping
82 * Kernel pointer of PD/PT BO that needs to be updated
83 */
84 dma_addr_t *pages_addr;
85 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040086};
87
Christian König284710f2017-01-30 11:09:31 +010088/* Helper to disable partial resident texture feature from a fence callback */
89struct amdgpu_prt_cb {
90 struct amdgpu_device *adev;
91 struct dma_fence_cb cb;
92};
93
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094/**
Christian König72a7ec52016-10-19 11:03:57 +020095 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 *
97 * @adev: amdgpu_device pointer
98 *
Christian König72a7ec52016-10-19 11:03:57 +020099 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100 */
Christian König72a7ec52016-10-19 11:03:57 +0200101static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
102 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103{
Christian König72a7ec52016-10-19 11:03:57 +0200104 if (level == 0)
105 /* For the root directory */
106 return adev->vm_manager.max_pfn >>
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800107 (adev->vm_manager.block_size *
108 adev->vm_manager.num_level);
Christian König72a7ec52016-10-19 11:03:57 +0200109 else if (level == adev->vm_manager.num_level)
110 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800111 return AMDGPU_VM_PTE_COUNT(adev);
Christian König72a7ec52016-10-19 11:03:57 +0200112 else
113 /* Everything in between */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800114 return 1 << adev->vm_manager.block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115}
116
117/**
Christian König72a7ec52016-10-19 11:03:57 +0200118 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 *
120 * @adev: amdgpu_device pointer
121 *
Christian König72a7ec52016-10-19 11:03:57 +0200122 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 */
Christian König72a7ec52016-10-19 11:03:57 +0200124static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125{
Christian König72a7ec52016-10-19 11:03:57 +0200126 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127}
128
129/**
Christian König56467eb2015-12-11 15:16:32 +0100130 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 *
132 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100133 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100134 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 *
136 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100137 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 */
Christian König56467eb2015-12-11 15:16:32 +0100139void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
140 struct list_head *validated,
141 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142{
Christian König3f3333f2017-08-03 14:02:13 +0200143 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100144 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200145 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100146 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100147 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100148 list_add(&entry->tv.head, validated);
149}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150
Christian König56467eb2015-12-11 15:16:32 +0100151/**
Christian Königf7da30d2016-09-28 12:03:04 +0200152 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100153 *
Christian König5a712a82016-06-21 16:28:15 +0200154 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100155 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200156 * @validate: callback to do the validation
157 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158 *
Christian Königf7da30d2016-09-28 12:03:04 +0200159 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 */
Christian Königf7da30d2016-09-28 12:03:04 +0200161int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
162 int (*validate)(void *p, struct amdgpu_bo *bo),
163 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164{
Christian König3f3333f2017-08-03 14:02:13 +0200165 struct ttm_bo_global *glob = adev->mman.bdev.glob;
166 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167
Christian König3f3333f2017-08-03 14:02:13 +0200168 spin_lock(&vm->status_lock);
169 while (!list_empty(&vm->evicted)) {
170 struct amdgpu_vm_bo_base *bo_base;
171 struct amdgpu_bo *bo;
Christian König5a712a82016-06-21 16:28:15 +0200172
Christian König3f3333f2017-08-03 14:02:13 +0200173 bo_base = list_first_entry(&vm->evicted,
174 struct amdgpu_vm_bo_base,
175 vm_status);
176 spin_unlock(&vm->status_lock);
Christian Königeceb8a12016-01-11 15:35:21 +0100177
Christian König3f3333f2017-08-03 14:02:13 +0200178 bo = bo_base->bo;
179 BUG_ON(!bo);
180 if (bo->parent) {
181 r = validate(param, bo);
182 if (r)
183 return r;
Christian König34d7be52017-08-24 12:32:55 +0200184
Christian König3f3333f2017-08-03 14:02:13 +0200185 spin_lock(&glob->lru_lock);
186 ttm_bo_move_to_lru_tail(&bo->tbo);
187 if (bo->shadow)
188 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
189 spin_unlock(&glob->lru_lock);
190 }
191
Christian König73fb16e2017-08-16 11:13:48 +0200192 if (bo->tbo.type == ttm_bo_type_kernel &&
193 vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +0200194 r = amdgpu_bo_kmap(bo, NULL);
195 if (r)
196 return r;
197 }
198
199 spin_lock(&vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200200 if (bo->tbo.type != ttm_bo_type_kernel)
201 list_move(&bo_base->vm_status, &vm->moved);
202 else
203 list_move(&bo_base->vm_status, &vm->relocated);
Christian König3f3333f2017-08-03 14:02:13 +0200204 }
205 spin_unlock(&vm->status_lock);
Christian König34d7be52017-08-24 12:32:55 +0200206
207 return 0;
208}
209
210/**
211 * amdgpu_vm_ready - check VM is ready for updates
212 *
Christian König34d7be52017-08-24 12:32:55 +0200213 * @vm: VM to check
214 *
215 * Check if all VM PDs/PTs are ready for updates
216 */
Christian König3f3333f2017-08-03 14:02:13 +0200217bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200218{
Christian König3f3333f2017-08-03 14:02:13 +0200219 bool ready;
Christian König34d7be52017-08-24 12:32:55 +0200220
Christian König3f3333f2017-08-03 14:02:13 +0200221 spin_lock(&vm->status_lock);
222 ready = list_empty(&vm->evicted);
223 spin_unlock(&vm->status_lock);
224
225 return ready;
Christian König34d7be52017-08-24 12:32:55 +0200226}
227
228/**
Christian Königf566ceb2016-10-27 20:04:38 +0200229 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
230 *
231 * @adev: amdgpu_device pointer
232 * @vm: requested vm
233 * @saddr: start of the address range
234 * @eaddr: end of the address range
235 *
236 * Make sure the page directories and page tables are allocated
237 */
238static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
239 struct amdgpu_vm *vm,
240 struct amdgpu_vm_pt *parent,
241 uint64_t saddr, uint64_t eaddr,
242 unsigned level)
243{
244 unsigned shift = (adev->vm_manager.num_level - level) *
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800245 adev->vm_manager.block_size;
Christian Königf566ceb2016-10-27 20:04:38 +0200246 unsigned pt_idx, from, to;
247 int r;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400248 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400249 uint64_t init_value = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200250
251 if (!parent->entries) {
252 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
253
Michal Hocko20981052017-05-17 14:23:12 +0200254 parent->entries = kvmalloc_array(num_entries,
255 sizeof(struct amdgpu_vm_pt),
256 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200257 if (!parent->entries)
258 return -ENOMEM;
259 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
260 }
261
Felix Kuehling1866bac2017-03-28 20:36:12 -0400262 from = saddr >> shift;
263 to = eaddr >> shift;
264 if (from >= amdgpu_vm_num_entries(adev, level) ||
265 to >= amdgpu_vm_num_entries(adev, level))
266 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200267
268 if (to > parent->last_entry_used)
269 parent->last_entry_used = to;
270
271 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400272 saddr = saddr & ((1 << shift) - 1);
273 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200274
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400275 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
276 AMDGPU_GEM_CREATE_VRAM_CLEARED;
277 if (vm->use_cpu_for_update)
278 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
279 else
280 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
281 AMDGPU_GEM_CREATE_SHADOW);
282
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400283 if (vm->pte_support_ats) {
284 init_value = AMDGPU_PTE_SYSTEM;
285 if (level != adev->vm_manager.num_level - 1)
286 init_value |= AMDGPU_PDE_PTE;
287 }
288
Christian Königf566ceb2016-10-27 20:04:38 +0200289 /* walk over the address space and allocate the page tables */
290 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200291 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200292 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
293 struct amdgpu_bo *pt;
294
Christian König3f3333f2017-08-03 14:02:13 +0200295 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200296 r = amdgpu_bo_create(adev,
297 amdgpu_vm_bo_size(adev, level),
298 AMDGPU_GPU_PAGE_SIZE, true,
299 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400300 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400301 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200302 if (r)
303 return r;
304
Christian König0a096fb2017-07-12 10:01:48 +0200305 if (vm->use_cpu_for_update) {
306 r = amdgpu_bo_kmap(pt, NULL);
307 if (r) {
308 amdgpu_bo_unref(&pt);
309 return r;
310 }
311 }
312
Christian Königf566ceb2016-10-27 20:04:38 +0200313 /* Keep a reference to the root directory to avoid
314 * freeing them up in the wrong order.
315 */
Christian König0f2fc432017-08-31 10:46:20 +0200316 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200317
Christian König3f3333f2017-08-03 14:02:13 +0200318 entry->base.vm = vm;
319 entry->base.bo = pt;
320 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200321 spin_lock(&vm->status_lock);
322 list_add(&entry->base.vm_status, &vm->relocated);
323 spin_unlock(&vm->status_lock);
Christian König0f2fc432017-08-31 10:46:20 +0200324 entry->addr = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200325 }
326
327 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400328 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
329 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
330 ((1 << shift) - 1);
331 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
332 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200333 if (r)
334 return r;
335 }
336 }
337
338 return 0;
339}
340
Christian König663e4572017-03-13 10:13:37 +0100341/**
342 * amdgpu_vm_alloc_pts - Allocate page tables.
343 *
344 * @adev: amdgpu_device pointer
345 * @vm: VM to allocate page tables for
346 * @saddr: Start address which needs to be allocated
347 * @size: Size from start address we need.
348 *
349 * Make sure the page tables are allocated.
350 */
351int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
352 struct amdgpu_vm *vm,
353 uint64_t saddr, uint64_t size)
354{
Felix Kuehling22770e52017-03-28 20:24:53 -0400355 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100356 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100357
358 /* validate the parameters */
359 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
360 return -EINVAL;
361
362 eaddr = saddr + size - 1;
363 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
364 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400365 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100366 last_pfn, adev->vm_manager.max_pfn);
367 return -EINVAL;
368 }
369
370 saddr /= AMDGPU_GPU_PAGE_SIZE;
371 eaddr /= AMDGPU_GPU_PAGE_SIZE;
372
Christian Königf566ceb2016-10-27 20:04:38 +0200373 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100374}
375
Christian König641e9402017-04-03 13:59:25 +0200376/**
377 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
378 *
379 * @adev: amdgpu_device pointer
380 * @id: VMID structure
381 *
382 * Check if GPU reset occured since last use of the VMID.
383 */
384static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
385 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800386{
387 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200388 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800389}
390
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800391static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
392{
393 return !!vm->reserved_vmid[vmhub];
394}
395
396/* idr_mgr->lock must be held */
397static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
398 struct amdgpu_ring *ring,
399 struct amdgpu_sync *sync,
400 struct dma_fence *fence,
401 struct amdgpu_job *job)
402{
403 struct amdgpu_device *adev = ring->adev;
404 unsigned vmhub = ring->funcs->vmhub;
405 uint64_t fence_context = adev->fence_context + ring->idx;
406 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
407 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
408 struct dma_fence *updates = sync->last_vm_update;
409 int r = 0;
410 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200411 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800412
413 flushed = id->flushed_updates;
414 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
415 (atomic64_read(&id->owner) != vm->client_id) ||
416 (job->vm_pd_addr != id->pd_gpu_addr) ||
417 (updates && (!flushed || updates->context != flushed->context ||
418 dma_fence_is_later(updates, flushed))) ||
419 (!id->last_flush || (id->last_flush->context != fence_context &&
420 !dma_fence_is_signaled(id->last_flush)))) {
421 needs_flush = true;
422 /* to prevent one context starved by another context */
423 id->pd_gpu_addr = 0;
424 tmp = amdgpu_sync_peek_fence(&id->active, ring);
425 if (tmp) {
426 r = amdgpu_sync_fence(adev, sync, tmp);
427 return r;
428 }
429 }
430
431 /* Good we can use this VMID. Remember this submission as
432 * user of the VMID.
433 */
434 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
435 if (r)
436 goto out;
437
438 if (updates && (!flushed || updates->context != flushed->context ||
439 dma_fence_is_later(updates, flushed))) {
440 dma_fence_put(id->flushed_updates);
441 id->flushed_updates = dma_fence_get(updates);
442 }
443 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800444 atomic64_set(&id->owner, vm->client_id);
445 job->vm_needs_flush = needs_flush;
446 if (needs_flush) {
447 dma_fence_put(id->last_flush);
448 id->last_flush = NULL;
449 }
450 job->vm_id = id - id_mgr->ids;
451 trace_amdgpu_vm_grab_id(vm, ring, job);
452out:
453 return r;
454}
455
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400456/**
457 * amdgpu_vm_grab_id - allocate the next free VMID
458 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200460 * @ring: ring we want to submit job to
461 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100462 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 *
Christian König7f8a5292015-07-20 16:09:40 +0200464 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400465 */
Christian König7f8a5292015-07-20 16:09:40 +0200466int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100467 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800468 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200471 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200472 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200473 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100474 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200475 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100476 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200477 unsigned i;
478 int r = 0;
479
Christian König76456702017-04-06 17:52:39 +0200480 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800481 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
482 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
483 mutex_unlock(&id_mgr->lock);
484 return r;
485 }
486 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
487 if (!fences) {
488 mutex_unlock(&id_mgr->lock);
489 return -ENOMEM;
490 }
Christian König36fd7c52016-05-23 15:30:08 +0200491 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200492 i = 0;
Christian König76456702017-04-06 17:52:39 +0200493 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200494 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
495 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200496 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200497 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200498 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100499
Christian König1fbb2e92016-06-01 10:47:36 +0200500 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200501 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200502 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
503 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100504 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200505 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200506
Christian König1fbb2e92016-06-01 10:47:36 +0200507 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100508 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200509
Chris Wilsonf54d1862016-10-25 13:00:45 +0100510 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200511 seqno, true);
512 if (!array) {
513 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100514 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200515 kfree(fences);
516 r = -ENOMEM;
517 goto error;
518 }
Christian König8d76001e2016-05-23 16:00:32 +0200519
Christian König8d76001e2016-05-23 16:00:32 +0200520
Christian König1fbb2e92016-06-01 10:47:36 +0200521 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100522 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200523 if (r)
524 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200525
Christian König76456702017-04-06 17:52:39 +0200526 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200527 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200528
Christian König1fbb2e92016-06-01 10:47:36 +0200529 }
530 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200531
Christian König6f1ceab2017-07-11 16:59:21 +0200532 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200533 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200534 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100535 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200536 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200537
Christian König1fbb2e92016-06-01 10:47:36 +0200538 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200539 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800540 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200541
542 if (atomic64_read(&id->owner) != vm->client_id)
543 continue;
544
Chunming Zhoufd53be32016-07-01 17:59:01 +0800545 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200546 continue;
547
Christian König87c910d2017-03-30 16:56:20 +0200548 if (!id->last_flush ||
549 (id->last_flush->context != fence_context &&
550 !dma_fence_is_signaled(id->last_flush)))
551 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200552
553 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200554 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
555 needs_flush = true;
556
557 /* Concurrent flushes are only possible starting with Vega10 */
558 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200559 continue;
560
Christian König3dab83b2016-06-01 13:31:17 +0200561 /* Good we can use this VMID. Remember this submission as
562 * user of the VMID.
563 */
Christian König1fbb2e92016-06-01 10:47:36 +0200564 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
565 if (r)
566 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200567
Christian König87c910d2017-03-30 16:56:20 +0200568 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
569 dma_fence_put(id->flushed_updates);
570 id->flushed_updates = dma_fence_get(updates);
571 }
Christian König8d76001e2016-05-23 16:00:32 +0200572
Christian König87c910d2017-03-30 16:56:20 +0200573 if (needs_flush)
574 goto needs_flush;
575 else
576 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200577
Christian König4f618e72017-04-06 15:18:21 +0200578 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800579
Christian König1fbb2e92016-06-01 10:47:36 +0200580 /* Still no ID to use? Then use the idle one found earlier */
581 id = idle;
582
583 /* Remember this submission as user of the VMID */
584 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100585 if (r)
586 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100587
Christian König87c910d2017-03-30 16:56:20 +0200588 id->pd_gpu_addr = job->vm_pd_addr;
589 dma_fence_put(id->flushed_updates);
590 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200591 atomic64_set(&id->owner, vm->client_id);
592
593needs_flush:
594 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100595 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100596 id->last_flush = NULL;
597
Christian König87c910d2017-03-30 16:56:20 +0200598no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200599 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600
Christian König76456702017-04-06 17:52:39 +0200601 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200602 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100603
604error:
Christian König76456702017-04-06 17:52:39 +0200605 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100606 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400607}
608
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800609static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
610 struct amdgpu_vm *vm,
611 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400612{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800613 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400614
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800615 mutex_lock(&id_mgr->lock);
616 if (vm->reserved_vmid[vmhub]) {
617 list_add(&vm->reserved_vmid[vmhub]->list,
618 &id_mgr->ids_lru);
619 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800620 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400621 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800622 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400623}
624
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800625static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
626 struct amdgpu_vm *vm,
627 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500628{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800629 struct amdgpu_vm_id_manager *id_mgr;
630 struct amdgpu_vm_id *idle;
631 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500632
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800633 id_mgr = &adev->vm_manager.id_mgr[vmhub];
634 mutex_lock(&id_mgr->lock);
635 if (vm->reserved_vmid[vmhub])
636 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800637 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
638 AMDGPU_VM_MAX_RESERVED_VMID) {
639 DRM_ERROR("Over limitation of reserved vmid\n");
640 atomic_dec(&id_mgr->reserved_vmid_num);
641 r = -EINVAL;
642 goto unlock;
643 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800644 /* Select the first entry VMID */
645 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
646 list_del_init(&idle->list);
647 vm->reserved_vmid[vmhub] = idle;
648 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500649
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800650 return 0;
651unlock:
652 mutex_unlock(&id_mgr->lock);
653 return r;
654}
655
Alex Xiee59c0202017-06-01 09:42:59 -0400656/**
657 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
658 *
659 * @adev: amdgpu_device pointer
660 */
661void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
662{
663 const struct amdgpu_ip_block *ip_block;
664 bool has_compute_vm_bug;
665 struct amdgpu_ring *ring;
666 int i;
667
668 has_compute_vm_bug = false;
669
670 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
671 if (ip_block) {
672 /* Compute has a VM bug for GFX version < 7.
673 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
674 if (ip_block->version->major <= 7)
675 has_compute_vm_bug = true;
676 else if (ip_block->version->major == 8)
677 if (adev->gfx.mec_fw_version < 673)
678 has_compute_vm_bug = true;
679 }
680
681 for (i = 0; i < adev->num_rings; i++) {
682 ring = adev->rings[i];
683 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
684 /* only compute rings */
685 ring->has_compute_vm_bug = has_compute_vm_bug;
686 else
687 ring->has_compute_vm_bug = false;
688 }
689}
690
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400691bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
692 struct amdgpu_job *job)
693{
694 struct amdgpu_device *adev = ring->adev;
695 unsigned vmhub = ring->funcs->vmhub;
696 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
697 struct amdgpu_vm_id *id;
698 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400699 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400700
701 if (job->vm_id == 0)
702 return false;
703 id = &id_mgr->ids[job->vm_id];
704 gds_switch_needed = ring->funcs->emit_gds_switch && (
705 id->gds_base != job->gds_base ||
706 id->gds_size != job->gds_size ||
707 id->gws_base != job->gws_base ||
708 id->gws_size != job->gws_size ||
709 id->oa_base != job->oa_base ||
710 id->oa_size != job->oa_size);
711
712 if (amdgpu_vm_had_gpu_reset(adev, id))
713 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400714
715 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400716}
717
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400718static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
719{
720 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500721}
722
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723/**
724 * amdgpu_vm_flush - hardware flush the vm
725 *
726 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100727 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100728 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 *
Christian König4ff37a82016-02-26 16:18:26 +0100730 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800732int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400733{
Christian König971fe9a92016-03-01 15:09:25 +0100734 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200735 unsigned vmhub = ring->funcs->vmhub;
736 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
737 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100738 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800739 id->gds_base != job->gds_base ||
740 id->gds_size != job->gds_size ||
741 id->gws_base != job->gws_base ||
742 id->gws_size != job->gws_size ||
743 id->oa_base != job->oa_base ||
744 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800745 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200746 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100747 int r;
Christian Königd564a062016-03-01 15:51:53 +0100748
Christian Königf7d015b2017-04-03 14:28:26 +0200749 if (amdgpu_vm_had_gpu_reset(adev, id)) {
750 gds_switch_needed = true;
751 vm_flush_needed = true;
752 }
Christian König971fe9a92016-03-01 15:09:25 +0100753
Monk Liu8fdf0742017-06-06 17:25:13 +0800754 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200755 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100756
Christian Königc0e51932017-04-03 14:16:07 +0200757 if (ring->funcs->init_cond_exec)
758 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100759
Monk Liu8fdf0742017-06-06 17:25:13 +0800760 if (need_pipe_sync)
761 amdgpu_ring_emit_pipeline_sync(ring);
762
Christian Königf7d015b2017-04-03 14:28:26 +0200763 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200764 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800765
Christian König9a94f5a2017-05-12 14:46:23 +0200766 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
767 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800768
Christian Königc0e51932017-04-03 14:16:07 +0200769 r = amdgpu_fence_emit(ring, &fence);
770 if (r)
771 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800772
Christian König76456702017-04-06 17:52:39 +0200773 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200774 dma_fence_put(id->last_flush);
775 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800776 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200777 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200778 }
Monk Liue9d672b2017-03-15 12:18:57 +0800779
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800780 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200781 id->gds_base = job->gds_base;
782 id->gds_size = job->gds_size;
783 id->gws_base = job->gws_base;
784 id->gws_size = job->gws_size;
785 id->oa_base = job->oa_base;
786 id->oa_size = job->oa_size;
787 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
788 job->gds_size, job->gws_base,
789 job->gws_size, job->oa_base,
790 job->oa_size);
791 }
792
793 if (ring->funcs->patch_cond_exec)
794 amdgpu_ring_patch_cond_exec(ring, patch_offset);
795
796 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
797 if (ring->funcs->emit_switch_buffer) {
798 amdgpu_ring_emit_switch_buffer(ring);
799 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400800 }
Christian König41d9eb22016-03-01 16:46:18 +0100801 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100802}
803
804/**
805 * amdgpu_vm_reset_id - reset VMID to zero
806 *
807 * @adev: amdgpu device structure
808 * @vm_id: vmid number to use
809 *
810 * Reset saved GDW, GWS and OA to force switch on next flush.
811 */
Christian König76456702017-04-06 17:52:39 +0200812void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
813 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100814{
Christian König76456702017-04-06 17:52:39 +0200815 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
816 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100817
Christian Königb3c85a02017-05-10 20:06:58 +0200818 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100819 id->gds_base = 0;
820 id->gds_size = 0;
821 id->gws_base = 0;
822 id->gws_size = 0;
823 id->oa_base = 0;
824 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400825}
826
827/**
Christian Königb3c85a02017-05-10 20:06:58 +0200828 * amdgpu_vm_reset_all_id - reset VMID to zero
829 *
830 * @adev: amdgpu device structure
831 *
832 * Reset VMID to force flush on next use
833 */
834void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
835{
836 unsigned i, j;
837
838 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
839 struct amdgpu_vm_id_manager *id_mgr =
840 &adev->vm_manager.id_mgr[i];
841
842 for (j = 1; j < id_mgr->num_ids; ++j)
843 amdgpu_vm_reset_id(adev, i, j);
844 }
845}
846
847/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
849 *
850 * @vm: requested vm
851 * @bo: requested buffer object
852 *
Christian König8843dbb2016-01-26 12:17:11 +0100853 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854 * Search inside the @bos vm list for the requested vm
855 * Returns the found bo_va or NULL if none is found
856 *
857 * Object has to be reserved!
858 */
859struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
860 struct amdgpu_bo *bo)
861{
862 struct amdgpu_bo_va *bo_va;
863
Christian Königec681542017-08-01 10:51:43 +0200864 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
865 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400866 return bo_va;
867 }
868 }
869 return NULL;
870}
871
872/**
Christian Königafef8b82016-08-12 13:29:18 +0200873 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 *
Christian König29efc4f2016-08-04 14:52:50 +0200875 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400876 * @pe: addr of the page entry
877 * @addr: dst addr to write into pe
878 * @count: number of page entries to update
879 * @incr: increase next addr by incr bytes
880 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400881 *
882 * Traces the parameters and calls the right asic functions
883 * to setup the page table using the DMA.
884 */
Christian Königafef8b82016-08-12 13:29:18 +0200885static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
886 uint64_t pe, uint64_t addr,
887 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800888 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889{
Christian Königec2f05f2016-09-25 16:11:52 +0200890 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400891
Christian Königafef8b82016-08-12 13:29:18 +0200892 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200893 amdgpu_vm_write_pte(params->adev, params->ib, pe,
894 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895
896 } else {
Christian König27c5f362016-08-04 15:02:49 +0200897 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400898 count, incr, flags);
899 }
900}
901
902/**
Christian Königafef8b82016-08-12 13:29:18 +0200903 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
904 *
905 * @params: see amdgpu_pte_update_params definition
906 * @pe: addr of the page entry
907 * @addr: dst addr to write into pe
908 * @count: number of page entries to update
909 * @incr: increase next addr by incr bytes
910 * @flags: hw access flags
911 *
912 * Traces the parameters and calls the DMA function to copy the PTEs.
913 */
914static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
915 uint64_t pe, uint64_t addr,
916 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800917 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200918{
Christian Königec2f05f2016-09-25 16:11:52 +0200919 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200920
Christian Königec2f05f2016-09-25 16:11:52 +0200921
922 trace_amdgpu_vm_copy_ptes(pe, src, count);
923
924 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200925}
926
927/**
Christian Königb07c9d22015-11-30 13:26:07 +0100928 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 *
Christian Königb07c9d22015-11-30 13:26:07 +0100930 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931 * @addr: the unmapped addr
932 *
933 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100934 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200936static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937{
938 uint64_t result;
939
Christian Königde9ea7b2016-08-12 11:33:30 +0200940 /* page table offset */
941 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942
Christian Königde9ea7b2016-08-12 11:33:30 +0200943 /* in case cpu page size != gpu page size*/
944 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100945
946 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947
948 return result;
949}
950
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400951/**
952 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
953 *
954 * @params: see amdgpu_pte_update_params definition
955 * @pe: kmap addr of the page entry
956 * @addr: dst addr to write into pe
957 * @count: number of page entries to update
958 * @incr: increase next addr by incr bytes
959 * @flags: hw access flags
960 *
961 * Write count number of PT/PD entries directly.
962 */
963static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
964 uint64_t pe, uint64_t addr,
965 unsigned count, uint32_t incr,
966 uint64_t flags)
967{
968 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400969 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400970
Christian König03918b32017-07-11 17:15:37 +0200971 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
972
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400973 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400974 value = params->pages_addr ?
975 amdgpu_vm_map_gart(params->pages_addr, addr) :
976 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -0400977 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400978 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400979 addr += incr;
980 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400981}
982
Christian Königa33cab72017-07-11 17:13:00 +0200983static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
984 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400985{
986 struct amdgpu_sync sync;
987 int r;
988
989 amdgpu_sync_create(&sync);
Christian König3f3333f2017-08-03 14:02:13 +0200990 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400991 r = amdgpu_sync_wait(&sync, true);
992 amdgpu_sync_free(&sync);
993
994 return r;
995}
996
Christian Königf8991ba2016-09-16 15:36:49 +0200997/*
Christian König194d2162016-10-12 15:13:52 +0200998 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200999 *
1000 * @adev: amdgpu_device pointer
1001 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +02001002 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +02001003 *
Christian König194d2162016-10-12 15:13:52 +02001004 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +02001005 * Returns 0 for success, error for failure.
1006 */
Christian König194d2162016-10-12 15:13:52 +02001007static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1008 struct amdgpu_vm *vm,
Christian Königea097292017-08-09 14:15:46 +02001009 struct amdgpu_vm_pt *parent)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010{
Christian Königf8991ba2016-09-16 15:36:49 +02001011 struct amdgpu_bo *shadow;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001012 struct amdgpu_ring *ring = NULL;
1013 uint64_t pd_addr, shadow_addr = 0;
Christian Königf8991ba2016-09-16 15:36:49 +02001014 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001015 unsigned count = 0, pt_idx, ndw = 0;
Christian Königd71518b2016-02-01 12:20:25 +01001016 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001017 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +10001018 struct dma_fence *fence = NULL;
Christian Königea097292017-08-09 14:15:46 +02001019 uint32_t incr;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001020
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001021 int r;
1022
Christian König194d2162016-10-12 15:13:52 +02001023 if (!parent->entries)
1024 return 0;
Christian Königd71518b2016-02-01 12:20:25 +01001025
Christian König27c5f362016-08-04 15:02:49 +02001026 memset(&params, 0, sizeof(params));
1027 params.adev = adev;
Christian König3f3333f2017-08-03 14:02:13 +02001028 shadow = parent->base.bo->shadow;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001029
Alex Deucher69277982017-07-13 15:37:11 -04001030 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02001031 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Christian Königa33cab72017-07-11 17:13:00 +02001032 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001033 if (unlikely(r))
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001034 return r;
Christian König0a096fb2017-07-12 10:01:48 +02001035
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001036 params.func = amdgpu_vm_cpu_set_ptes;
1037 } else {
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001038 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1039 sched);
1040
1041 /* padding, etc. */
1042 ndw = 64;
1043
1044 /* assume the worst case */
1045 ndw += parent->last_entry_used * 6;
1046
Christian König3f3333f2017-08-03 14:02:13 +02001047 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001048
1049 if (shadow) {
1050 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1051 ndw *= 2;
1052 } else {
1053 shadow_addr = 0;
1054 }
1055
1056 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1057 if (r)
1058 return r;
1059
1060 params.ib = &job->ibs[0];
1061 params.func = amdgpu_vm_do_set_ptes;
1062 }
1063
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001064
Christian König194d2162016-10-12 15:13:52 +02001065 /* walk over the address space and update the directory */
1066 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
Christian Königea097292017-08-09 14:15:46 +02001067 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1068 struct amdgpu_bo *bo = entry->base.bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001069 uint64_t pde, pt;
1070
1071 if (bo == NULL)
1072 continue;
1073
Christian Königea097292017-08-09 14:15:46 +02001074 spin_lock(&vm->status_lock);
1075 list_del_init(&entry->base.vm_status);
1076 spin_unlock(&vm->status_lock);
1077
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001078 pt = amdgpu_bo_gpu_offset(bo);
Christian König53e2e912017-05-15 15:19:10 +02001079 pt = amdgpu_gart_get_vm_pde(adev, pt);
Christian König4ab40162017-08-03 20:30:50 +02001080 /* Don't update huge pages here */
1081 if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
1082 parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
Christian Königf8991ba2016-09-16 15:36:49 +02001083 continue;
1084
Christian König4ab40162017-08-03 20:30:50 +02001085 parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001086
1087 pde = pd_addr + pt_idx * 8;
Christian Königea097292017-08-09 14:15:46 +02001088 incr = amdgpu_bo_size(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001089 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001090 ((last_pt + incr * count) != pt) ||
1091 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001092
1093 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +02001094 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001095 params.func(&params,
1096 last_shadow,
1097 last_pt, count,
1098 incr,
1099 AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001100
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001101 params.func(&params, last_pde,
1102 last_pt, count, incr,
1103 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001104 }
1105
1106 count = 1;
1107 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001108 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001109 last_pt = pt;
1110 } else {
1111 ++count;
1112 }
1113 }
1114
Christian Königf8991ba2016-09-16 15:36:49 +02001115 if (count) {
Christian König3f3333f2017-08-03 14:02:13 +02001116 if (vm->root.base.bo->shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001117 params.func(&params, last_shadow, last_pt,
1118 count, incr, AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001119
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001120 params.func(&params, last_pde, last_pt,
1121 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001122 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001123
Christian König0a096fb2017-07-12 10:01:48 +02001124 if (!vm->use_cpu_for_update) {
1125 if (params.ib->length_dw == 0) {
1126 amdgpu_job_free(job);
1127 } else {
1128 amdgpu_ring_pad_ib(ring, params.ib);
Christian König3f3333f2017-08-03 14:02:13 +02001129 amdgpu_sync_resv(adev, &job->sync,
1130 parent->base.bo->tbo.resv,
Christian König194d2162016-10-12 15:13:52 +02001131 AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001132 if (shadow)
1133 amdgpu_sync_resv(adev, &job->sync,
1134 shadow->tbo.resv,
1135 AMDGPU_FENCE_OWNER_VM);
Christian Königf8991ba2016-09-16 15:36:49 +02001136
Christian König0a096fb2017-07-12 10:01:48 +02001137 WARN_ON(params.ib->length_dw > ndw);
1138 r = amdgpu_job_submit(job, ring, &vm->entity,
1139 AMDGPU_FENCE_OWNER_VM, &fence);
1140 if (r)
1141 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001142
Christian König3f3333f2017-08-03 14:02:13 +02001143 amdgpu_bo_fence(parent->base.bo, fence, true);
Christian König0a096fb2017-07-12 10:01:48 +02001144 dma_fence_put(vm->last_dir_update);
1145 vm->last_dir_update = dma_fence_get(fence);
1146 dma_fence_put(fence);
1147 }
Christian König194d2162016-10-12 15:13:52 +02001148 }
Christian Königf8991ba2016-09-16 15:36:49 +02001149
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001151
1152error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001153 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001154 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001155}
1156
Christian König194d2162016-10-12 15:13:52 +02001157/*
Christian König92456b92017-05-12 16:09:26 +02001158 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1159 *
1160 * @parent: parent PD
1161 *
1162 * Mark all PD level as invalid after an error.
1163 */
Christian Königea097292017-08-09 14:15:46 +02001164static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
1165 struct amdgpu_vm_pt *parent)
Christian König92456b92017-05-12 16:09:26 +02001166{
1167 unsigned pt_idx;
1168
1169 /*
1170 * Recurse into the subdirectories. This recursion is harmless because
1171 * we only have a maximum of 5 layers.
1172 */
1173 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1174 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1175
Christian König3f3333f2017-08-03 14:02:13 +02001176 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001177 continue;
1178
1179 entry->addr = ~0ULL;
Christian Königea097292017-08-09 14:15:46 +02001180 spin_lock(&vm->status_lock);
1181 list_move(&entry->base.vm_status, &vm->relocated);
1182 spin_unlock(&vm->status_lock);
1183 amdgpu_vm_invalidate_level(vm, entry);
Christian König92456b92017-05-12 16:09:26 +02001184 }
1185}
1186
1187/*
Christian König194d2162016-10-12 15:13:52 +02001188 * amdgpu_vm_update_directories - make sure that all directories are valid
1189 *
1190 * @adev: amdgpu_device pointer
1191 * @vm: requested vm
1192 *
1193 * Makes sure all directories are up to date.
1194 * Returns 0 for success, error for failure.
1195 */
1196int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1197 struct amdgpu_vm *vm)
1198{
Christian König92456b92017-05-12 16:09:26 +02001199 int r;
1200
Christian Königea097292017-08-09 14:15:46 +02001201 spin_lock(&vm->status_lock);
1202 while (!list_empty(&vm->relocated)) {
1203 struct amdgpu_vm_bo_base *bo_base;
1204 struct amdgpu_bo *bo;
1205
1206 bo_base = list_first_entry(&vm->relocated,
1207 struct amdgpu_vm_bo_base,
1208 vm_status);
1209 spin_unlock(&vm->status_lock);
1210
1211 bo = bo_base->bo->parent;
1212 if (bo) {
1213 struct amdgpu_vm_bo_base *parent;
1214 struct amdgpu_vm_pt *pt;
1215
1216 parent = list_first_entry(&bo->va,
1217 struct amdgpu_vm_bo_base,
1218 bo_list);
1219 pt = container_of(parent, struct amdgpu_vm_pt, base);
1220
1221 r = amdgpu_vm_update_level(adev, vm, pt);
1222 if (r) {
1223 amdgpu_vm_invalidate_level(vm, &vm->root);
1224 return r;
1225 }
1226 spin_lock(&vm->status_lock);
1227 } else {
1228 spin_lock(&vm->status_lock);
1229 list_del_init(&bo_base->vm_status);
1230 }
1231 }
1232 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +02001233
Christian König68c62302017-07-11 17:23:29 +02001234 if (vm->use_cpu_for_update) {
1235 /* Flush HDP */
1236 mb();
1237 amdgpu_gart_flush_gpu_tlb(adev, 0);
1238 }
1239
Christian König92456b92017-05-12 16:09:26 +02001240 return r;
Christian König194d2162016-10-12 15:13:52 +02001241}
1242
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001243/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001244 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001245 *
1246 * @p: see amdgpu_pte_update_params definition
1247 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001248 * @entry: resulting entry or NULL
1249 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001250 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001251 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001252 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001253void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1254 struct amdgpu_vm_pt **entry,
1255 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001256{
Christian König4e2cb642016-10-25 15:52:28 +02001257 unsigned idx, level = p->adev->vm_manager.num_level;
1258
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001259 *parent = NULL;
1260 *entry = &p->vm->root;
1261 while ((*entry)->entries) {
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001262 idx = addr >> (p->adev->vm_manager.block_size * level--);
Christian König3f3333f2017-08-03 14:02:13 +02001263 idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001264 *parent = *entry;
1265 *entry = &(*entry)->entries[idx];
Christian König4e2cb642016-10-25 15:52:28 +02001266 }
1267
1268 if (level)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001269 *entry = NULL;
1270}
Christian König4e2cb642016-10-25 15:52:28 +02001271
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001272/**
1273 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1274 *
1275 * @p: see amdgpu_pte_update_params definition
1276 * @entry: vm_pt entry to check
1277 * @parent: parent entry
1278 * @nptes: number of PTEs updated with this operation
1279 * @dst: destination address where the PTEs should point to
1280 * @flags: access flags fro the PTEs
1281 *
1282 * Check if we can update the PD with a huge page.
1283 */
Christian Königec5207c2017-08-03 19:24:06 +02001284static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1285 struct amdgpu_vm_pt *entry,
1286 struct amdgpu_vm_pt *parent,
1287 unsigned nptes, uint64_t dst,
1288 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001289{
1290 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1291 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001292
1293 /* In the case of a mixed PT the PDE must point to it*/
1294 if (p->adev->asic_type < CHIP_VEGA10 ||
1295 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehling38a87912017-08-17 16:37:49 -04001296 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001297 !(flags & AMDGPU_PTE_VALID)) {
1298
Christian König3f3333f2017-08-03 14:02:13 +02001299 dst = amdgpu_bo_gpu_offset(entry->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001300 dst = amdgpu_gart_get_vm_pde(p->adev, dst);
1301 flags = AMDGPU_PTE_VALID;
1302 } else {
Christian König4ab40162017-08-03 20:30:50 +02001303 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001304 flags |= AMDGPU_PDE_PTE;
1305 }
1306
Christian König4ab40162017-08-03 20:30:50 +02001307 if (entry->addr == (dst | flags))
Christian Königec5207c2017-08-03 19:24:06 +02001308 return;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001309
Christian König4ab40162017-08-03 20:30:50 +02001310 entry->addr = (dst | flags);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001311
1312 if (use_cpu_update) {
Felix Kuehling38a87912017-08-17 16:37:49 -04001313 /* In case a huge page is replaced with a system
1314 * memory mapping, p->pages_addr != NULL and
1315 * amdgpu_vm_cpu_set_ptes would try to translate dst
1316 * through amdgpu_vm_map_gart. But dst is already a
1317 * GPU address (of the page table). Disable
1318 * amdgpu_vm_map_gart temporarily.
1319 */
1320 dma_addr_t *tmp;
1321
1322 tmp = p->pages_addr;
1323 p->pages_addr = NULL;
1324
Christian König3f3333f2017-08-03 14:02:13 +02001325 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001326 pde = pd_addr + (entry - parent->entries) * 8;
1327 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehling38a87912017-08-17 16:37:49 -04001328
1329 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001330 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001331 if (parent->base.bo->shadow) {
1332 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001333 pde = pd_addr + (entry - parent->entries) * 8;
1334 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1335 }
Christian König3f3333f2017-08-03 14:02:13 +02001336 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001337 pde = pd_addr + (entry - parent->entries) * 8;
1338 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1339 }
Christian König4e2cb642016-10-25 15:52:28 +02001340}
1341
1342/**
Christian König92696dd2016-08-05 13:56:35 +02001343 * amdgpu_vm_update_ptes - make sure that page tables are valid
1344 *
1345 * @params: see amdgpu_pte_update_params definition
1346 * @vm: requested vm
1347 * @start: start of GPU address range
1348 * @end: end of GPU address range
1349 * @dst: destination address to map to, the next dst inside the function
1350 * @flags: mapping flags
1351 *
1352 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001353 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001354 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001355static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001356 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001357 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001358{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001359 struct amdgpu_device *adev = params->adev;
1360 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001361
Christian König301654a2017-05-16 14:30:27 +02001362 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001363 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001364 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001365 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001366
1367 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001368 for (addr = start; addr < end; addr += nptes,
1369 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1370 struct amdgpu_vm_pt *entry, *parent;
1371
1372 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1373 if (!entry)
1374 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001375
Christian König92696dd2016-08-05 13:56:35 +02001376 if ((addr & ~mask) == (end & ~mask))
1377 nptes = end - addr;
1378 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001379 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001380
Christian Königec5207c2017-08-03 19:24:06 +02001381 amdgpu_vm_handle_huge_pages(params, entry, parent,
1382 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001383 /* We don't need to update PTEs for huge pages */
1384 if (entry->addr & AMDGPU_PDE_PTE)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001385 continue;
1386
Christian König3f3333f2017-08-03 14:02:13 +02001387 pt = entry->base.bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001388 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001389 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001390 } else {
1391 if (pt->shadow) {
1392 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1393 pe_start += (addr & mask) * 8;
1394 params->func(params, pe_start, dst, nptes,
1395 AMDGPU_GPU_PAGE_SIZE, flags);
1396 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001397 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001398 }
Christian König92696dd2016-08-05 13:56:35 +02001399
Christian König301654a2017-05-16 14:30:27 +02001400 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001401 params->func(params, pe_start, dst, nptes,
1402 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001403 }
1404
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001405 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001406}
1407
1408/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001409 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1410 *
Christian König29efc4f2016-08-04 14:52:50 +02001411 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001412 * @vm: requested vm
1413 * @start: first PTE to handle
1414 * @end: last PTE to handle
1415 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001416 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001417 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001419static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001420 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001421 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422{
1423 /**
1424 * The MC L1 TLB supports variable sized pages, based on a fragment
1425 * field in the PTE. When this field is set to a non-zero value, page
1426 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1427 * flags are considered valid for all PTEs within the fragment range
1428 * and corresponding mappings are assumed to be physically contiguous.
1429 *
1430 * The L1 TLB can store a single PTE for the whole fragment,
1431 * significantly increasing the space available for translation
1432 * caching. This leads to large improvements in throughput when the
1433 * TLB is under pressure.
1434 *
1435 * The L2 TLB distributes small and large fragments into two
1436 * asymmetric partitions. The large fragment cache is significantly
1437 * larger. Thus, we try to use large fragments wherever possible.
1438 * Userspace can support this by aligning virtual base address and
1439 * allocation size to the fragment size.
1440 */
Roger He6849d472017-08-30 13:01:19 +08001441 unsigned max_frag = params->adev->vm_manager.fragment_size;
1442 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001443
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001445 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001446 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447
Roger He6849d472017-08-30 13:01:19 +08001448 while (start != end) {
1449 uint64_t frag_flags, frag_end;
1450 unsigned frag;
1451
1452 /* This intentionally wraps around if no bit is set */
1453 frag = min((unsigned)ffs(start) - 1,
1454 (unsigned)fls64(end - start) - 1);
1455 if (frag >= max_frag) {
1456 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1457 frag_end = end & ~((1ULL << max_frag) - 1);
1458 } else {
1459 frag_flags = AMDGPU_PTE_FRAG(frag);
1460 frag_end = start + (1 << frag);
1461 }
1462
1463 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1464 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001465 if (r)
1466 return r;
Roger He6849d472017-08-30 13:01:19 +08001467
1468 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1469 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470 }
1471
Roger He6849d472017-08-30 13:01:19 +08001472 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001473}
1474
1475/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1477 *
1478 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001479 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001480 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001481 * @vm: requested vm
1482 * @start: start of mapped range
1483 * @last: last mapped entry
1484 * @flags: flags for the entries
1485 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001486 * @fence: optional resulting fence
1487 *
Christian Königa14faa62016-01-25 14:27:31 +01001488 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001490 */
1491static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001492 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001493 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001495 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001496 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001497 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001498{
Christian König2d55e452016-02-08 17:37:38 +01001499 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001500 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001501 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001502 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001503 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001504 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505 int r;
1506
Christian Königafef8b82016-08-12 13:29:18 +02001507 memset(&params, 0, sizeof(params));
1508 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001509 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001510
Christian Königa33cab72017-07-11 17:13:00 +02001511 /* sync to everything on unmapping */
1512 if (!(flags & AMDGPU_PTE_VALID))
1513 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1514
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001515 if (vm->use_cpu_for_update) {
1516 /* params.src is used as flag to indicate system Memory */
1517 if (pages_addr)
1518 params.src = ~0;
1519
1520 /* Wait for PT BOs to be free. PTs share the same resv. object
1521 * as the root PD BO
1522 */
Christian Königa33cab72017-07-11 17:13:00 +02001523 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001524 if (unlikely(r))
1525 return r;
1526
1527 params.func = amdgpu_vm_cpu_set_ptes;
1528 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001529 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1530 addr, flags);
1531 }
1532
Christian König2d55e452016-02-08 17:37:38 +01001533 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001534
Christian Königa14faa62016-01-25 14:27:31 +01001535 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536
1537 /*
1538 * reserve space for one command every (1 << BLOCK_SIZE)
1539 * entries or 2k dwords (whatever is smaller)
1540 */
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001541 ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542
1543 /* padding, etc. */
1544 ndw = 64;
1545
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001546 /* one PDE write for each huge page */
1547 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1548
Christian König570144c2017-08-30 15:38:45 +02001549 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001550 /* copy commands needed */
1551 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552
Christian Königb0456f92016-08-11 14:06:54 +02001553 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001554 ndw += nptes * 2;
1555
Christian Königafef8b82016-08-12 13:29:18 +02001556 params.func = amdgpu_vm_do_copy_ptes;
1557
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001558 } else {
1559 /* set page commands needed */
1560 ndw += ncmds * 10;
1561
Roger He6849d472017-08-30 13:01:19 +08001562 /* extra commands for begin/end fragments */
1563 ndw += 2 * 10 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001564
1565 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 }
1567
Christian Königd71518b2016-02-01 12:20:25 +01001568 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1569 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001570 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001571
Christian König29efc4f2016-08-04 14:52:50 +02001572 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001573
Christian König570144c2017-08-30 15:38:45 +02001574 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001575 uint64_t *pte;
1576 unsigned i;
1577
1578 /* Put the PTEs at the end of the IB. */
1579 i = ndw - nptes * 2;
1580 pte= (uint64_t *)&(job->ibs->ptr[i]);
1581 params.src = job->ibs->gpu_addr + i * 4;
1582
1583 for (i = 0; i < nptes; ++i) {
1584 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1585 AMDGPU_GPU_PAGE_SIZE);
1586 pte[i] |= flags;
1587 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001588 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001589 }
1590
Christian König3cabaa52016-06-06 10:17:58 +02001591 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1592 if (r)
1593 goto error_free;
1594
Christian König3f3333f2017-08-03 14:02:13 +02001595 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001596 owner);
1597 if (r)
1598 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599
Christian König3f3333f2017-08-03 14:02:13 +02001600 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001601 if (r)
1602 goto error_free;
1603
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001604 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1605 if (r)
1606 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607
Christian König29efc4f2016-08-04 14:52:50 +02001608 amdgpu_ring_pad_ib(ring, params.ib);
1609 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001610 r = amdgpu_job_submit(job, ring, &vm->entity,
1611 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001612 if (r)
1613 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001614
Christian König3f3333f2017-08-03 14:02:13 +02001615 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001616 dma_fence_put(*fence);
1617 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001618 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001619
1620error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001621 amdgpu_job_free(job);
Christian Königea097292017-08-09 14:15:46 +02001622 amdgpu_vm_invalidate_level(vm, &vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001623 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001624}
1625
1626/**
Christian Königa14faa62016-01-25 14:27:31 +01001627 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1628 *
1629 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001630 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001631 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001632 * @vm: requested vm
1633 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001634 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001635 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001636 * @fence: optional resulting fence
1637 *
1638 * Split the mapping into smaller chunks so that each update fits
1639 * into a SDMA IB.
1640 * Returns 0 for success, -EINVAL for failure.
1641 */
1642static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001643 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001644 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001645 struct amdgpu_vm *vm,
1646 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001647 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001648 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001649 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001650{
Christian König570144c2017-08-30 15:38:45 +02001651 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001652 int r;
1653
1654 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1655 * but in case of something, we filter the flags in first place
1656 */
1657 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1658 flags &= ~AMDGPU_PTE_READABLE;
1659 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1660 flags &= ~AMDGPU_PTE_WRITEABLE;
1661
Alex Xie15b31c52017-03-03 16:47:11 -05001662 flags &= ~AMDGPU_PTE_EXECUTABLE;
1663 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1664
Alex Xieb0fd18b2017-03-03 16:49:39 -05001665 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1666 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1667
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001668 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1669 (adev->asic_type >= CHIP_VEGA10)) {
1670 flags |= AMDGPU_PTE_PRT;
1671 flags &= ~AMDGPU_PTE_VALID;
1672 }
1673
Christian Königa14faa62016-01-25 14:27:31 +01001674 trace_amdgpu_vm_bo_update(mapping);
1675
Christian König63e0ba42016-08-16 17:38:37 +02001676 pfn = mapping->offset >> PAGE_SHIFT;
1677 if (nodes) {
1678 while (pfn >= nodes->size) {
1679 pfn -= nodes->size;
1680 ++nodes;
1681 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001682 }
Christian Königa14faa62016-01-25 14:27:31 +01001683
Christian König63e0ba42016-08-16 17:38:37 +02001684 do {
1685 uint64_t max_entries;
1686 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001687
Christian König63e0ba42016-08-16 17:38:37 +02001688 if (nodes) {
1689 addr = nodes->start << PAGE_SHIFT;
1690 max_entries = (nodes->size - pfn) *
1691 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1692 } else {
1693 addr = 0;
1694 max_entries = S64_MAX;
1695 }
Christian Königa14faa62016-01-25 14:27:31 +01001696
Christian König63e0ba42016-08-16 17:38:37 +02001697 if (pages_addr) {
Christian Königfebb84a2017-08-22 12:50:46 +02001698 max_entries = min(max_entries, 16ull * 1024ull);
Christian König63e0ba42016-08-16 17:38:37 +02001699 addr = 0;
1700 } else if (flags & AMDGPU_PTE_VALID) {
1701 addr += adev->vm_manager.vram_base_offset;
1702 }
1703 addr += pfn << PAGE_SHIFT;
1704
Christian Königa9f87f62017-03-30 14:03:59 +02001705 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König570144c2017-08-30 15:38:45 +02001706 r = amdgpu_vm_bo_update_mapping(adev, exclusive, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001707 start, last, flags, addr,
1708 fence);
1709 if (r)
1710 return r;
1711
Christian König63e0ba42016-08-16 17:38:37 +02001712 pfn += last - start + 1;
1713 if (nodes && nodes->size == pfn) {
1714 pfn = 0;
1715 ++nodes;
1716 }
Christian Königa14faa62016-01-25 14:27:31 +01001717 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001718
Christian Königa9f87f62017-03-30 14:03:59 +02001719 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001720
1721 return 0;
1722}
1723
1724/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001725 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1726 *
1727 * @adev: amdgpu_device pointer
1728 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001729 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730 *
1731 * Fill in the page table entries for @bo_va.
1732 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001733 */
1734int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1735 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001736 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737{
Christian Königec681542017-08-01 10:51:43 +02001738 struct amdgpu_bo *bo = bo_va->base.bo;
1739 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001741 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001742 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001743 struct drm_mm_node *nodes;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001744 struct dma_fence *exclusive;
Christian Königfebb84a2017-08-22 12:50:46 +02001745 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001746 int r;
1747
Christian Königec681542017-08-01 10:51:43 +02001748 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001749 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001750 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001751 exclusive = NULL;
1752 } else {
Christian König8358dce2016-03-30 10:50:25 +02001753 struct ttm_dma_tt *ttm;
1754
Christian Königec681542017-08-01 10:51:43 +02001755 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001756 nodes = mem->mm_node;
1757 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001758 ttm = container_of(bo_va->base.bo->tbo.ttm,
1759 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001760 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001761 }
Christian Königec681542017-08-01 10:51:43 +02001762 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001763 }
1764
Christian Königfebb84a2017-08-22 12:50:46 +02001765 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001766 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian Königfebb84a2017-08-22 12:50:46 +02001767 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001768 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001769
Christian König3d7d4d32017-08-23 16:13:33 +02001770 if (!clear && bo_va->base.moved) {
1771 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001772 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001773
Christian Königcb7b6ec2017-08-15 17:08:12 +02001774 } else if (bo_va->cleared != clear) {
1775 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001776 }
Christian König7fc11952015-07-30 11:53:42 +02001777
1778 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königfebb84a2017-08-22 12:50:46 +02001779 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001780 mapping, flags, nodes,
Christian König8358dce2016-03-30 10:50:25 +02001781 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001782 if (r)
1783 return r;
1784 }
1785
Christian König68c62302017-07-11 17:23:29 +02001786 if (vm->use_cpu_for_update) {
1787 /* Flush HDP */
1788 mb();
1789 amdgpu_gart_flush_gpu_tlb(adev, 0);
1790 }
1791
Christian Königcb7b6ec2017-08-15 17:08:12 +02001792 spin_lock(&vm->status_lock);
1793 list_del_init(&bo_va->base.vm_status);
1794 spin_unlock(&vm->status_lock);
1795
1796 list_splice_init(&bo_va->invalids, &bo_va->valids);
1797 bo_va->cleared = clear;
1798
1799 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1800 list_for_each_entry(mapping, &bo_va->valids, list)
1801 trace_amdgpu_vm_bo_mapping(mapping);
1802 }
1803
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001804 return 0;
1805}
1806
1807/**
Christian König284710f2017-01-30 11:09:31 +01001808 * amdgpu_vm_update_prt_state - update the global PRT state
1809 */
1810static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1811{
1812 unsigned long flags;
1813 bool enable;
1814
1815 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001816 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001817 adev->gart.gart_funcs->set_prt(adev, enable);
1818 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1819}
1820
1821/**
Christian König4388fc22017-03-13 10:13:36 +01001822 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001823 */
1824static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1825{
Christian König4388fc22017-03-13 10:13:36 +01001826 if (!adev->gart.gart_funcs->set_prt)
1827 return;
1828
Christian König451bc8e2017-02-14 16:02:52 +01001829 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1830 amdgpu_vm_update_prt_state(adev);
1831}
1832
1833/**
Christian König0b15f2f2017-02-14 15:47:03 +01001834 * amdgpu_vm_prt_put - drop a PRT user
1835 */
1836static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1837{
Christian König451bc8e2017-02-14 16:02:52 +01001838 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001839 amdgpu_vm_update_prt_state(adev);
1840}
1841
1842/**
Christian König451bc8e2017-02-14 16:02:52 +01001843 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001844 */
1845static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1846{
1847 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1848
Christian König0b15f2f2017-02-14 15:47:03 +01001849 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001850 kfree(cb);
1851}
1852
1853/**
Christian König451bc8e2017-02-14 16:02:52 +01001854 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1855 */
1856static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1857 struct dma_fence *fence)
1858{
Christian König4388fc22017-03-13 10:13:36 +01001859 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001860
Christian König4388fc22017-03-13 10:13:36 +01001861 if (!adev->gart.gart_funcs->set_prt)
1862 return;
1863
1864 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001865 if (!cb) {
1866 /* Last resort when we are OOM */
1867 if (fence)
1868 dma_fence_wait(fence, false);
1869
Dan Carpenter486a68f2017-04-03 21:41:39 +03001870 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001871 } else {
1872 cb->adev = adev;
1873 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1874 amdgpu_vm_prt_cb))
1875 amdgpu_vm_prt_cb(fence, &cb->cb);
1876 }
1877}
1878
1879/**
Christian König284710f2017-01-30 11:09:31 +01001880 * amdgpu_vm_free_mapping - free a mapping
1881 *
1882 * @adev: amdgpu_device pointer
1883 * @vm: requested vm
1884 * @mapping: mapping to be freed
1885 * @fence: fence of the unmap operation
1886 *
1887 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1888 */
1889static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1890 struct amdgpu_vm *vm,
1891 struct amdgpu_bo_va_mapping *mapping,
1892 struct dma_fence *fence)
1893{
Christian König451bc8e2017-02-14 16:02:52 +01001894 if (mapping->flags & AMDGPU_PTE_PRT)
1895 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001896 kfree(mapping);
1897}
1898
1899/**
Christian König451bc8e2017-02-14 16:02:52 +01001900 * amdgpu_vm_prt_fini - finish all prt mappings
1901 *
1902 * @adev: amdgpu_device pointer
1903 * @vm: requested vm
1904 *
1905 * Register a cleanup callback to disable PRT support after VM dies.
1906 */
1907static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1908{
Christian König3f3333f2017-08-03 14:02:13 +02001909 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001910 struct dma_fence *excl, **shared;
1911 unsigned i, shared_count;
1912 int r;
1913
1914 r = reservation_object_get_fences_rcu(resv, &excl,
1915 &shared_count, &shared);
1916 if (r) {
1917 /* Not enough memory to grab the fence list, as last resort
1918 * block for all the fences to complete.
1919 */
1920 reservation_object_wait_timeout_rcu(resv, true, false,
1921 MAX_SCHEDULE_TIMEOUT);
1922 return;
1923 }
1924
1925 /* Add a callback for each fence in the reservation object */
1926 amdgpu_vm_prt_get(adev);
1927 amdgpu_vm_add_prt_cb(adev, excl);
1928
1929 for (i = 0; i < shared_count; ++i) {
1930 amdgpu_vm_prt_get(adev);
1931 amdgpu_vm_add_prt_cb(adev, shared[i]);
1932 }
1933
1934 kfree(shared);
1935}
1936
1937/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001938 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1939 *
1940 * @adev: amdgpu_device pointer
1941 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001942 * @fence: optional resulting fence (unchanged if no work needed to be done
1943 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001944 *
1945 * Make sure all freed BOs are cleared in the PT.
1946 * Returns 0 for success.
1947 *
1948 * PTs have to be reserved and mutex must be locked!
1949 */
1950int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001951 struct amdgpu_vm *vm,
1952 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001953{
1954 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001955 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001956 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001957 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958
1959 while (!list_empty(&vm->freed)) {
1960 mapping = list_first_entry(&vm->freed,
1961 struct amdgpu_bo_va_mapping, list);
1962 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001963
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001964 if (vm->pte_support_ats)
1965 init_pte_value = AMDGPU_PTE_SYSTEM;
1966
Christian König570144c2017-08-30 15:38:45 +02001967 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001968 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001969 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001970 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001971 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001972 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001973 return r;
Christian König284710f2017-01-30 11:09:31 +01001974 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001976
1977 if (fence && f) {
1978 dma_fence_put(*fence);
1979 *fence = f;
1980 } else {
1981 dma_fence_put(f);
1982 }
1983
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 return 0;
1985
1986}
1987
1988/**
Christian König73fb16e2017-08-16 11:13:48 +02001989 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001990 *
1991 * @adev: amdgpu_device pointer
1992 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02001993 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001994 *
Christian König73fb16e2017-08-16 11:13:48 +02001995 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001996 * Returns 0 for success.
1997 *
Christian König73fb16e2017-08-16 11:13:48 +02001998 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001999 */
Christian König73fb16e2017-08-16 11:13:48 +02002000int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
2001 struct amdgpu_vm *vm,
2002 struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002003{
monk.liucfe2c972015-05-26 15:01:54 +08002004 struct amdgpu_bo_va *bo_va = NULL;
Christian König73fb16e2017-08-16 11:13:48 +02002005 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02002006 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007
2008 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002009 while (!list_empty(&vm->moved)) {
2010 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002011 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002012 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002013
Christian König73fb16e2017-08-16 11:13:48 +02002014 /* Per VM BOs never need to bo cleared in the page tables */
2015 clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
2016
2017 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002018 if (r)
2019 return r;
2020
2021 spin_lock(&vm->status_lock);
2022 }
2023 spin_unlock(&vm->status_lock);
2024
monk.liucfe2c972015-05-26 15:01:54 +08002025 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08002026 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02002027
2028 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002029}
2030
2031/**
2032 * amdgpu_vm_bo_add - add a bo to a specific vm
2033 *
2034 * @adev: amdgpu_device pointer
2035 * @vm: requested vm
2036 * @bo: amdgpu buffer object
2037 *
Christian König8843dbb2016-01-26 12:17:11 +01002038 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002039 * Add @bo to the list of bos associated with the vm
2040 * Returns newly added bo_va or NULL for failure
2041 *
2042 * Object has to be reserved!
2043 */
2044struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2045 struct amdgpu_vm *vm,
2046 struct amdgpu_bo *bo)
2047{
2048 struct amdgpu_bo_va *bo_va;
2049
2050 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2051 if (bo_va == NULL) {
2052 return NULL;
2053 }
Christian Königec681542017-08-01 10:51:43 +02002054 bo_va->base.vm = vm;
2055 bo_va->base.bo = bo;
2056 INIT_LIST_HEAD(&bo_va->base.bo_list);
2057 INIT_LIST_HEAD(&bo_va->base.vm_status);
2058
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002060 INIT_LIST_HEAD(&bo_va->valids);
2061 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002062
Christian Königa5f6b5b2017-01-30 11:01:38 +01002063 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002064 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065
2066 return bo_va;
2067}
2068
Christian König73fb16e2017-08-16 11:13:48 +02002069
2070/**
2071 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2072 *
2073 * @adev: amdgpu_device pointer
2074 * @bo_va: bo_va to store the address
2075 * @mapping: the mapping to insert
2076 *
2077 * Insert a new mapping into all structures.
2078 */
2079static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2080 struct amdgpu_bo_va *bo_va,
2081 struct amdgpu_bo_va_mapping *mapping)
2082{
2083 struct amdgpu_vm *vm = bo_va->base.vm;
2084 struct amdgpu_bo *bo = bo_va->base.bo;
2085
2086 list_add(&mapping->list, &bo_va->invalids);
2087 amdgpu_vm_it_insert(mapping, &vm->va);
2088
2089 if (mapping->flags & AMDGPU_PTE_PRT)
2090 amdgpu_vm_prt_get(adev);
2091
2092 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2093 spin_lock(&vm->status_lock);
2094 list_move(&bo_va->base.vm_status, &vm->moved);
2095 spin_unlock(&vm->status_lock);
2096 }
2097 trace_amdgpu_vm_bo_map(bo_va, mapping);
2098}
2099
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002100/**
2101 * amdgpu_vm_bo_map - map bo inside a vm
2102 *
2103 * @adev: amdgpu_device pointer
2104 * @bo_va: bo_va to store the address
2105 * @saddr: where to map the BO
2106 * @offset: requested offset in the BO
2107 * @flags: attributes of pages (read/write/valid/etc.)
2108 *
2109 * Add a mapping of the BO at the specefied addr into the VM.
2110 * Returns 0 for success, error for failure.
2111 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002112 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002113 */
2114int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2115 struct amdgpu_bo_va *bo_va,
2116 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002117 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002118{
Christian Königa9f87f62017-03-30 14:03:59 +02002119 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002120 struct amdgpu_bo *bo = bo_va->base.bo;
2121 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002122 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002123
Christian König0be52de2015-05-18 14:37:27 +02002124 /* validate the parameters */
2125 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002126 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002127 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002128
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002129 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002130 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002131 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002132 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002133 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002134
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002135 saddr /= AMDGPU_GPU_PAGE_SIZE;
2136 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2137
Christian Königa9f87f62017-03-30 14:03:59 +02002138 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2139 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002140 /* bo and tmp overlap, invalid addr */
2141 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002142 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002143 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002144 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002145 }
2146
2147 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002148 if (!mapping)
2149 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002150
Christian Königa9f87f62017-03-30 14:03:59 +02002151 mapping->start = saddr;
2152 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002153 mapping->offset = offset;
2154 mapping->flags = flags;
2155
Christian König73fb16e2017-08-16 11:13:48 +02002156 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002157
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002158 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002159}
2160
2161/**
Christian König80f95c52017-03-13 10:13:39 +01002162 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2163 *
2164 * @adev: amdgpu_device pointer
2165 * @bo_va: bo_va to store the address
2166 * @saddr: where to map the BO
2167 * @offset: requested offset in the BO
2168 * @flags: attributes of pages (read/write/valid/etc.)
2169 *
2170 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2171 * mappings as we do so.
2172 * Returns 0 for success, error for failure.
2173 *
2174 * Object has to be reserved and unreserved outside!
2175 */
2176int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2177 struct amdgpu_bo_va *bo_va,
2178 uint64_t saddr, uint64_t offset,
2179 uint64_t size, uint64_t flags)
2180{
2181 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002182 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002183 uint64_t eaddr;
2184 int r;
2185
2186 /* validate the parameters */
2187 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2188 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2189 return -EINVAL;
2190
2191 /* make sure object fit at this offset */
2192 eaddr = saddr + size - 1;
2193 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002194 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002195 return -EINVAL;
2196
2197 /* Allocate all the needed memory */
2198 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2199 if (!mapping)
2200 return -ENOMEM;
2201
Christian Königec681542017-08-01 10:51:43 +02002202 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002203 if (r) {
2204 kfree(mapping);
2205 return r;
2206 }
2207
2208 saddr /= AMDGPU_GPU_PAGE_SIZE;
2209 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2210
Christian Königa9f87f62017-03-30 14:03:59 +02002211 mapping->start = saddr;
2212 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002213 mapping->offset = offset;
2214 mapping->flags = flags;
2215
Christian König73fb16e2017-08-16 11:13:48 +02002216 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002217
2218 return 0;
2219}
2220
2221/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002222 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2223 *
2224 * @adev: amdgpu_device pointer
2225 * @bo_va: bo_va to remove the address from
2226 * @saddr: where to the BO is mapped
2227 *
2228 * Remove a mapping of the BO at the specefied addr from the VM.
2229 * Returns 0 for success, error for failure.
2230 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002231 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232 */
2233int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2234 struct amdgpu_bo_va *bo_va,
2235 uint64_t saddr)
2236{
2237 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002238 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002239 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240
Christian König6c7fc502015-06-05 20:56:17 +02002241 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002242
Christian König7fc11952015-07-30 11:53:42 +02002243 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002244 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002245 break;
2246 }
2247
Christian König7fc11952015-07-30 11:53:42 +02002248 if (&mapping->list == &bo_va->valids) {
2249 valid = false;
2250
2251 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002252 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002253 break;
2254 }
2255
Christian König32b41ac2016-03-08 18:03:27 +01002256 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002257 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002258 }
Christian König32b41ac2016-03-08 18:03:27 +01002259
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002260 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002261 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002262 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002263
Christian Könige17841b2016-03-08 17:52:01 +01002264 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002265 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002266 else
Christian König284710f2017-01-30 11:09:31 +01002267 amdgpu_vm_free_mapping(adev, vm, mapping,
2268 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002269
2270 return 0;
2271}
2272
2273/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002274 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2275 *
2276 * @adev: amdgpu_device pointer
2277 * @vm: VM structure to use
2278 * @saddr: start of the range
2279 * @size: size of the range
2280 *
2281 * Remove all mappings in a range, split them as appropriate.
2282 * Returns 0 for success, error for failure.
2283 */
2284int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2285 struct amdgpu_vm *vm,
2286 uint64_t saddr, uint64_t size)
2287{
2288 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002289 LIST_HEAD(removed);
2290 uint64_t eaddr;
2291
2292 eaddr = saddr + size - 1;
2293 saddr /= AMDGPU_GPU_PAGE_SIZE;
2294 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2295
2296 /* Allocate all the needed memory */
2297 before = kzalloc(sizeof(*before), GFP_KERNEL);
2298 if (!before)
2299 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002300 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002301
2302 after = kzalloc(sizeof(*after), GFP_KERNEL);
2303 if (!after) {
2304 kfree(before);
2305 return -ENOMEM;
2306 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002307 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002308
2309 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002310 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2311 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002312 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002313 if (tmp->start < saddr) {
2314 before->start = tmp->start;
2315 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002316 before->offset = tmp->offset;
2317 before->flags = tmp->flags;
2318 list_add(&before->list, &tmp->list);
2319 }
2320
2321 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002322 if (tmp->last > eaddr) {
2323 after->start = eaddr + 1;
2324 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002325 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002326 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002327 after->flags = tmp->flags;
2328 list_add(&after->list, &tmp->list);
2329 }
2330
2331 list_del(&tmp->list);
2332 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002333
2334 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002335 }
2336
2337 /* And free them up */
2338 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002339 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002340 list_del(&tmp->list);
2341
Christian Königa9f87f62017-03-30 14:03:59 +02002342 if (tmp->start < saddr)
2343 tmp->start = saddr;
2344 if (tmp->last > eaddr)
2345 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002346
2347 list_add(&tmp->list, &vm->freed);
2348 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2349 }
2350
Junwei Zhang27f6d612017-03-16 16:09:24 +08002351 /* Insert partial mapping before the range */
2352 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002353 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002354 if (before->flags & AMDGPU_PTE_PRT)
2355 amdgpu_vm_prt_get(adev);
2356 } else {
2357 kfree(before);
2358 }
2359
2360 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002361 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002362 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002363 if (after->flags & AMDGPU_PTE_PRT)
2364 amdgpu_vm_prt_get(adev);
2365 } else {
2366 kfree(after);
2367 }
2368
2369 return 0;
2370}
2371
2372/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002373 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2374 *
2375 * @adev: amdgpu_device pointer
2376 * @bo_va: requested bo_va
2377 *
Christian König8843dbb2016-01-26 12:17:11 +01002378 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002379 *
2380 * Object have to be reserved!
2381 */
2382void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2383 struct amdgpu_bo_va *bo_va)
2384{
2385 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002386 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002387
Christian Königec681542017-08-01 10:51:43 +02002388 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002389
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002390 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002391 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 spin_unlock(&vm->status_lock);
2393
Christian König7fc11952015-07-30 11:53:42 +02002394 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002395 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002396 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02002397 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002398 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399 }
Christian König7fc11952015-07-30 11:53:42 +02002400 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2401 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002402 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002403 amdgpu_vm_free_mapping(adev, vm, mapping,
2404 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002405 }
Christian König32b41ac2016-03-08 18:03:27 +01002406
Chris Wilsonf54d1862016-10-25 13:00:45 +01002407 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002408 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002409}
2410
2411/**
2412 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2413 *
2414 * @adev: amdgpu_device pointer
2415 * @vm: requested vm
2416 * @bo: amdgpu buffer object
2417 *
Christian König8843dbb2016-01-26 12:17:11 +01002418 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002419 */
2420void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002421 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002422{
Christian Königec681542017-08-01 10:51:43 +02002423 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002424
Christian Königec681542017-08-01 10:51:43 +02002425 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002426 struct amdgpu_vm *vm = bo_base->vm;
2427
Christian König3d7d4d32017-08-23 16:13:33 +02002428 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002429 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2430 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002431 if (bo->tbo.type == ttm_bo_type_kernel)
2432 list_move(&bo_base->vm_status, &vm->evicted);
2433 else
2434 list_move_tail(&bo_base->vm_status,
2435 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002436 spin_unlock(&bo_base->vm->status_lock);
2437 continue;
2438 }
2439
Christian Königea097292017-08-09 14:15:46 +02002440 if (bo->tbo.type == ttm_bo_type_kernel) {
2441 spin_lock(&bo_base->vm->status_lock);
2442 if (list_empty(&bo_base->vm_status))
2443 list_add(&bo_base->vm_status, &vm->relocated);
2444 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002445 continue;
Christian Königea097292017-08-09 14:15:46 +02002446 }
Christian König3f3333f2017-08-03 14:02:13 +02002447
Christian Königec681542017-08-01 10:51:43 +02002448 spin_lock(&bo_base->vm->status_lock);
Christian Königcb7b6ec2017-08-15 17:08:12 +02002449 list_move(&bo_base->vm_status, &bo_base->vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002450 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002451 }
2452}
2453
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002454static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2455{
2456 /* Total bits covered by PD + PTs */
2457 unsigned bits = ilog2(vm_size) + 18;
2458
2459 /* Make sure the PD is 4K in size up to 8GB address space.
2460 Above that split equal between PD and PTs */
2461 if (vm_size <= 8)
2462 return (bits - 9);
2463 else
2464 return ((bits + 3) / 2);
2465}
2466
2467/**
Roger Hed07f14b2017-08-15 16:05:59 +08002468 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2469 *
2470 * @adev: amdgpu_device pointer
2471 * @fragment_size_default: the default fragment size if it's set auto
2472 */
2473void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev, uint32_t fragment_size_default)
2474{
2475 if (amdgpu_vm_fragment_size == -1)
2476 adev->vm_manager.fragment_size = fragment_size_default;
2477 else
2478 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2479}
2480
2481/**
2482 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002483 *
2484 * @adev: amdgpu_device pointer
2485 * @vm_size: the default vm size if it's set auto
2486 */
Roger Hed07f14b2017-08-15 16:05:59 +08002487void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size, uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002488{
2489 /* adjust vm size firstly */
2490 if (amdgpu_vm_size == -1)
2491 adev->vm_manager.vm_size = vm_size;
2492 else
2493 adev->vm_manager.vm_size = amdgpu_vm_size;
2494
2495 /* block size depends on vm size */
2496 if (amdgpu_vm_block_size == -1)
2497 adev->vm_manager.block_size =
2498 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2499 else
2500 adev->vm_manager.block_size = amdgpu_vm_block_size;
2501
Roger Hed07f14b2017-08-15 16:05:59 +08002502 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2503
2504 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2505 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2506 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002507}
2508
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002509/**
2510 * amdgpu_vm_init - initialize a vm instance
2511 *
2512 * @adev: amdgpu_device pointer
2513 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002514 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002515 *
Christian König8843dbb2016-01-26 12:17:11 +01002516 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002517 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002518int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2519 int vm_context)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520{
2521 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002522 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002523 unsigned ring_instance;
2524 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002525 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002526 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002527 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002528 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002529
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002530 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002531 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002532 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2533 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002534 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002535 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002536 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002537 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002538 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002539
Christian König2bd9ccf2016-02-01 12:53:58 +01002540 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002541
2542 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2543 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2544 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002545 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2546 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2547 rq, amdgpu_sched_jobs);
2548 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002549 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002550
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002551 vm->pte_support_ats = false;
2552
2553 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002554 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2555 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002556
2557 if (adev->asic_type == CHIP_RAVEN) {
2558 vm->pte_support_ats = true;
2559 init_pde_value = AMDGPU_PTE_SYSTEM | AMDGPU_PDE_PTE;
2560 }
2561 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002562 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2563 AMDGPU_VM_USE_CPU_FOR_GFX);
2564 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2565 vm->use_cpu_for_update ? "CPU" : "SDMA");
2566 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2567 "CPU update of VM recommended only for large BAR system\n");
Christian Königa24960f2016-10-12 13:20:52 +02002568 vm->last_dir_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002569
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002570 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2571 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2572 if (vm->use_cpu_for_update)
2573 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2574 else
2575 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2576 AMDGPU_GEM_CREATE_SHADOW);
2577
Christian Königf566ceb2016-10-27 20:04:38 +02002578 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002579 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002580 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002581 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002582 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002583 goto error_free_sched_entity;
2584
Christian König3f3333f2017-08-03 14:02:13 +02002585 vm->root.base.vm = vm;
2586 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2587 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002588
2589 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002590 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002591 if (r)
2592 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002593
Christian König3f3333f2017-08-03 14:02:13 +02002594 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
2595 if (r)
2596 goto error_free_root;
2597 amdgpu_bo_unreserve(vm->root.base.bo);
2598 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002599
2600 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002601
Christian König67003a12016-10-12 14:46:26 +02002602error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002603 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2604 amdgpu_bo_unref(&vm->root.base.bo);
2605 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002606
2607error_free_sched_entity:
2608 amd_sched_entity_fini(&ring->sched, &vm->entity);
2609
2610 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611}
2612
2613/**
Christian Königf566ceb2016-10-27 20:04:38 +02002614 * amdgpu_vm_free_levels - free PD/PT levels
2615 *
2616 * @level: PD/PT starting level to free
2617 *
2618 * Free the page directory or page table level and all sub levels.
2619 */
2620static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2621{
2622 unsigned i;
2623
Christian König3f3333f2017-08-03 14:02:13 +02002624 if (level->base.bo) {
2625 list_del(&level->base.bo_list);
2626 list_del(&level->base.vm_status);
2627 amdgpu_bo_unref(&level->base.bo->shadow);
2628 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002629 }
2630
2631 if (level->entries)
2632 for (i = 0; i <= level->last_entry_used; i++)
2633 amdgpu_vm_free_levels(&level->entries[i]);
2634
Michal Hocko20981052017-05-17 14:23:12 +02002635 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002636}
2637
2638/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002639 * amdgpu_vm_fini - tear down a vm instance
2640 *
2641 * @adev: amdgpu_device pointer
2642 * @vm: requested vm
2643 *
Christian König8843dbb2016-01-26 12:17:11 +01002644 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002645 * Unbind the VM and remove all bos from the vm bo list
2646 */
2647void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2648{
2649 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002650 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002651 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002652
Christian König2d55e452016-02-08 17:37:38 +01002653 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002654
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002655 if (!RB_EMPTY_ROOT(&vm->va)) {
2656 dev_err(adev->dev, "still active bo inside vm\n");
2657 }
Christian Königa9f87f62017-03-30 14:03:59 +02002658 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002659 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002660 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002661 kfree(mapping);
2662 }
2663 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002664 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002665 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002666 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002667 }
Christian König284710f2017-01-30 11:09:31 +01002668
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002669 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002670 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002671 }
2672
Christian Königf566ceb2016-10-27 20:04:38 +02002673 amdgpu_vm_free_levels(&vm->root);
Christian Königa24960f2016-10-12 13:20:52 +02002674 dma_fence_put(vm->last_dir_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002675 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2676 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002677}
Christian Königea89f8c2015-11-15 20:52:06 +01002678
2679/**
Christian Königa9a78b32016-01-21 10:19:11 +01002680 * amdgpu_vm_manager_init - init the VM manager
2681 *
2682 * @adev: amdgpu_device pointer
2683 *
2684 * Initialize the VM manager structures
2685 */
2686void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2687{
Christian König76456702017-04-06 17:52:39 +02002688 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002689
Christian König76456702017-04-06 17:52:39 +02002690 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2691 struct amdgpu_vm_id_manager *id_mgr =
2692 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002693
Christian König76456702017-04-06 17:52:39 +02002694 mutex_init(&id_mgr->lock);
2695 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002696 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002697
2698 /* skip over VMID 0, since it is the system VM */
2699 for (j = 1; j < id_mgr->num_ids; ++j) {
2700 amdgpu_vm_reset_id(adev, i, j);
2701 amdgpu_sync_create(&id_mgr->ids[i].active);
2702 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2703 }
Christian König971fe9a92016-03-01 15:09:25 +01002704 }
Christian König2d55e452016-02-08 17:37:38 +01002705
Chris Wilsonf54d1862016-10-25 13:00:45 +01002706 adev->vm_manager.fence_context =
2707 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002708 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2709 adev->vm_manager.seqno[i] = 0;
2710
Christian König2d55e452016-02-08 17:37:38 +01002711 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002712 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002713 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002714 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002715
2716 /* If not overridden by the user, by default, only in large BAR systems
2717 * Compute VM tables will be updated by CPU
2718 */
2719#ifdef CONFIG_X86_64
2720 if (amdgpu_vm_update_mode == -1) {
2721 if (amdgpu_vm_is_large_bar(adev))
2722 adev->vm_manager.vm_update_mode =
2723 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2724 else
2725 adev->vm_manager.vm_update_mode = 0;
2726 } else
2727 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2728#else
2729 adev->vm_manager.vm_update_mode = 0;
2730#endif
2731
Christian Königa9a78b32016-01-21 10:19:11 +01002732}
2733
2734/**
Christian Königea89f8c2015-11-15 20:52:06 +01002735 * amdgpu_vm_manager_fini - cleanup VM manager
2736 *
2737 * @adev: amdgpu_device pointer
2738 *
2739 * Cleanup the VM manager and free resources.
2740 */
2741void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2742{
Christian König76456702017-04-06 17:52:39 +02002743 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002744
Christian König76456702017-04-06 17:52:39 +02002745 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2746 struct amdgpu_vm_id_manager *id_mgr =
2747 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002748
Christian König76456702017-04-06 17:52:39 +02002749 mutex_destroy(&id_mgr->lock);
2750 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2751 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2752
2753 amdgpu_sync_free(&id->active);
2754 dma_fence_put(id->flushed_updates);
2755 dma_fence_put(id->last_flush);
2756 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002757 }
Christian Königea89f8c2015-11-15 20:52:06 +01002758}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002759
2760int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2761{
2762 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002763 struct amdgpu_device *adev = dev->dev_private;
2764 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2765 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002766
2767 switch (args->in.op) {
2768 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002769 /* current, we only have requirement to reserve vmid from gfxhub */
2770 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2771 AMDGPU_GFXHUB);
2772 if (r)
2773 return r;
2774 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002775 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002776 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002777 break;
2778 default:
2779 return -EINVAL;
2780 }
2781
2782 return 0;
2783}