blob: 2196bca7331c262fb6aade12513e1d5cfb7ca990 [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önigd5884512017-09-08 14:09:41 +02001144 dma_fence_put(vm->last_update);
1145 vm->last_update = fence;
Christian König0a096fb2017-07-12 10:01:48 +02001146 }
Christian König194d2162016-10-12 15:13:52 +02001147 }
Christian Königf8991ba2016-09-16 15:36:49 +02001148
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001150
1151error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001152 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001153 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154}
1155
Christian König194d2162016-10-12 15:13:52 +02001156/*
Christian König92456b92017-05-12 16:09:26 +02001157 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1158 *
1159 * @parent: parent PD
1160 *
1161 * Mark all PD level as invalid after an error.
1162 */
Christian Königea097292017-08-09 14:15:46 +02001163static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
1164 struct amdgpu_vm_pt *parent)
Christian König92456b92017-05-12 16:09:26 +02001165{
1166 unsigned pt_idx;
1167
1168 /*
1169 * Recurse into the subdirectories. This recursion is harmless because
1170 * we only have a maximum of 5 layers.
1171 */
1172 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1173 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1174
Christian König3f3333f2017-08-03 14:02:13 +02001175 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001176 continue;
1177
1178 entry->addr = ~0ULL;
Christian Königea097292017-08-09 14:15:46 +02001179 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001180 if (list_empty(&entry->base.vm_status))
1181 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +02001182 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 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001538 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001540 *
1541 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 */
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001543 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001544
1545 /* padding, etc. */
1546 ndw = 64;
1547
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001548 /* one PDE write for each huge page */
1549 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1550
Christian König570144c2017-08-30 15:38:45 +02001551 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001552 /* copy commands needed */
1553 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001554
Christian Königb0456f92016-08-11 14:06:54 +02001555 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 ndw += nptes * 2;
1557
Christian Königafef8b82016-08-12 13:29:18 +02001558 params.func = amdgpu_vm_do_copy_ptes;
1559
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 } else {
1561 /* set page commands needed */
1562 ndw += ncmds * 10;
1563
Roger He6849d472017-08-30 13:01:19 +08001564 /* extra commands for begin/end fragments */
1565 ndw += 2 * 10 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001566
1567 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001568 }
1569
Christian Königd71518b2016-02-01 12:20:25 +01001570 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1571 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001572 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001573
Christian König29efc4f2016-08-04 14:52:50 +02001574 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001575
Christian König570144c2017-08-30 15:38:45 +02001576 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001577 uint64_t *pte;
1578 unsigned i;
1579
1580 /* Put the PTEs at the end of the IB. */
1581 i = ndw - nptes * 2;
1582 pte= (uint64_t *)&(job->ibs->ptr[i]);
1583 params.src = job->ibs->gpu_addr + i * 4;
1584
1585 for (i = 0; i < nptes; ++i) {
1586 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1587 AMDGPU_GPU_PAGE_SIZE);
1588 pte[i] |= flags;
1589 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001590 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001591 }
1592
Christian König3cabaa52016-06-06 10:17:58 +02001593 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1594 if (r)
1595 goto error_free;
1596
Christian König3f3333f2017-08-03 14:02:13 +02001597 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001598 owner);
1599 if (r)
1600 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601
Christian König3f3333f2017-08-03 14:02:13 +02001602 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001603 if (r)
1604 goto error_free;
1605
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001606 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1607 if (r)
1608 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001609
Christian König29efc4f2016-08-04 14:52:50 +02001610 amdgpu_ring_pad_ib(ring, params.ib);
1611 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001612 r = amdgpu_job_submit(job, ring, &vm->entity,
1613 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001614 if (r)
1615 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616
Christian König3f3333f2017-08-03 14:02:13 +02001617 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001618 dma_fence_put(*fence);
1619 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001620 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001621
1622error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001623 amdgpu_job_free(job);
Christian Königea097292017-08-09 14:15:46 +02001624 amdgpu_vm_invalidate_level(vm, &vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001625 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001626}
1627
1628/**
Christian Königa14faa62016-01-25 14:27:31 +01001629 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1630 *
1631 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001632 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001633 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001634 * @vm: requested vm
1635 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001636 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001637 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001638 * @fence: optional resulting fence
1639 *
1640 * Split the mapping into smaller chunks so that each update fits
1641 * into a SDMA IB.
1642 * Returns 0 for success, -EINVAL for failure.
1643 */
1644static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001645 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001646 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001647 struct amdgpu_vm *vm,
1648 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001649 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001650 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001651 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001652{
Christian König570144c2017-08-30 15:38:45 +02001653 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001654 int r;
1655
1656 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1657 * but in case of something, we filter the flags in first place
1658 */
1659 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1660 flags &= ~AMDGPU_PTE_READABLE;
1661 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1662 flags &= ~AMDGPU_PTE_WRITEABLE;
1663
Alex Xie15b31c52017-03-03 16:47:11 -05001664 flags &= ~AMDGPU_PTE_EXECUTABLE;
1665 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1666
Alex Xieb0fd18b2017-03-03 16:49:39 -05001667 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1668 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1669
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001670 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1671 (adev->asic_type >= CHIP_VEGA10)) {
1672 flags |= AMDGPU_PTE_PRT;
1673 flags &= ~AMDGPU_PTE_VALID;
1674 }
1675
Christian Königa14faa62016-01-25 14:27:31 +01001676 trace_amdgpu_vm_bo_update(mapping);
1677
Christian König63e0ba42016-08-16 17:38:37 +02001678 pfn = mapping->offset >> PAGE_SHIFT;
1679 if (nodes) {
1680 while (pfn >= nodes->size) {
1681 pfn -= nodes->size;
1682 ++nodes;
1683 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001684 }
Christian Königa14faa62016-01-25 14:27:31 +01001685
Christian König63e0ba42016-08-16 17:38:37 +02001686 do {
1687 uint64_t max_entries;
1688 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001689
Christian König63e0ba42016-08-16 17:38:37 +02001690 if (nodes) {
1691 addr = nodes->start << PAGE_SHIFT;
1692 max_entries = (nodes->size - pfn) *
1693 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1694 } else {
1695 addr = 0;
1696 max_entries = S64_MAX;
1697 }
Christian Königa14faa62016-01-25 14:27:31 +01001698
Christian König63e0ba42016-08-16 17:38:37 +02001699 if (pages_addr) {
Christian Königfebb84a2017-08-22 12:50:46 +02001700 max_entries = min(max_entries, 16ull * 1024ull);
Christian König63e0ba42016-08-16 17:38:37 +02001701 addr = 0;
1702 } else if (flags & AMDGPU_PTE_VALID) {
1703 addr += adev->vm_manager.vram_base_offset;
1704 }
1705 addr += pfn << PAGE_SHIFT;
1706
Christian Königa9f87f62017-03-30 14:03:59 +02001707 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König570144c2017-08-30 15:38:45 +02001708 r = amdgpu_vm_bo_update_mapping(adev, exclusive, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001709 start, last, flags, addr,
1710 fence);
1711 if (r)
1712 return r;
1713
Christian König63e0ba42016-08-16 17:38:37 +02001714 pfn += last - start + 1;
1715 if (nodes && nodes->size == pfn) {
1716 pfn = 0;
1717 ++nodes;
1718 }
Christian Königa14faa62016-01-25 14:27:31 +01001719 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001720
Christian Königa9f87f62017-03-30 14:03:59 +02001721 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001722
1723 return 0;
1724}
1725
1726/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001727 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1728 *
1729 * @adev: amdgpu_device pointer
1730 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001731 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001732 *
1733 * Fill in the page table entries for @bo_va.
1734 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001735 */
1736int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1737 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001738 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739{
Christian Königec681542017-08-01 10:51:43 +02001740 struct amdgpu_bo *bo = bo_va->base.bo;
1741 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001742 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001743 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001744 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001745 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001746 struct dma_fence *exclusive, **last_update;
Christian Königfebb84a2017-08-22 12:50:46 +02001747 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001748 int r;
1749
Christian Königec681542017-08-01 10:51:43 +02001750 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001751 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001752 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001753 exclusive = NULL;
1754 } else {
Christian König8358dce2016-03-30 10:50:25 +02001755 struct ttm_dma_tt *ttm;
1756
Christian Königec681542017-08-01 10:51:43 +02001757 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001758 nodes = mem->mm_node;
1759 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001760 ttm = container_of(bo_va->base.bo->tbo.ttm,
1761 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001762 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001763 }
Christian Königec681542017-08-01 10:51:43 +02001764 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001765 }
1766
Christian Königfebb84a2017-08-22 12:50:46 +02001767 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001768 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian Königfebb84a2017-08-22 12:50:46 +02001769 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001770 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001771
Christian König4e55eb32017-09-11 16:54:59 +02001772 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1773 last_update = &vm->last_update;
1774 else
1775 last_update = &bo_va->last_pt_update;
1776
Christian König3d7d4d32017-08-23 16:13:33 +02001777 if (!clear && bo_va->base.moved) {
1778 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001779 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001780
Christian Königcb7b6ec2017-08-15 17:08:12 +02001781 } else if (bo_va->cleared != clear) {
1782 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001783 }
Christian König7fc11952015-07-30 11:53:42 +02001784
1785 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königfebb84a2017-08-22 12:50:46 +02001786 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001787 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001788 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 if (r)
1790 return r;
1791 }
1792
Christian König68c62302017-07-11 17:23:29 +02001793 if (vm->use_cpu_for_update) {
1794 /* Flush HDP */
1795 mb();
1796 amdgpu_gart_flush_gpu_tlb(adev, 0);
1797 }
1798
Christian Königcb7b6ec2017-08-15 17:08:12 +02001799 spin_lock(&vm->status_lock);
1800 list_del_init(&bo_va->base.vm_status);
1801 spin_unlock(&vm->status_lock);
1802
1803 list_splice_init(&bo_va->invalids, &bo_va->valids);
1804 bo_va->cleared = clear;
1805
1806 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1807 list_for_each_entry(mapping, &bo_va->valids, list)
1808 trace_amdgpu_vm_bo_mapping(mapping);
1809 }
1810
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001811 return 0;
1812}
1813
1814/**
Christian König284710f2017-01-30 11:09:31 +01001815 * amdgpu_vm_update_prt_state - update the global PRT state
1816 */
1817static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1818{
1819 unsigned long flags;
1820 bool enable;
1821
1822 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001823 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001824 adev->gart.gart_funcs->set_prt(adev, enable);
1825 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1826}
1827
1828/**
Christian König4388fc22017-03-13 10:13:36 +01001829 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001830 */
1831static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1832{
Christian König4388fc22017-03-13 10:13:36 +01001833 if (!adev->gart.gart_funcs->set_prt)
1834 return;
1835
Christian König451bc8e2017-02-14 16:02:52 +01001836 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1837 amdgpu_vm_update_prt_state(adev);
1838}
1839
1840/**
Christian König0b15f2f2017-02-14 15:47:03 +01001841 * amdgpu_vm_prt_put - drop a PRT user
1842 */
1843static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1844{
Christian König451bc8e2017-02-14 16:02:52 +01001845 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001846 amdgpu_vm_update_prt_state(adev);
1847}
1848
1849/**
Christian König451bc8e2017-02-14 16:02:52 +01001850 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001851 */
1852static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1853{
1854 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1855
Christian König0b15f2f2017-02-14 15:47:03 +01001856 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001857 kfree(cb);
1858}
1859
1860/**
Christian König451bc8e2017-02-14 16:02:52 +01001861 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1862 */
1863static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1864 struct dma_fence *fence)
1865{
Christian König4388fc22017-03-13 10:13:36 +01001866 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001867
Christian König4388fc22017-03-13 10:13:36 +01001868 if (!adev->gart.gart_funcs->set_prt)
1869 return;
1870
1871 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001872 if (!cb) {
1873 /* Last resort when we are OOM */
1874 if (fence)
1875 dma_fence_wait(fence, false);
1876
Dan Carpenter486a68f2017-04-03 21:41:39 +03001877 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001878 } else {
1879 cb->adev = adev;
1880 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1881 amdgpu_vm_prt_cb))
1882 amdgpu_vm_prt_cb(fence, &cb->cb);
1883 }
1884}
1885
1886/**
Christian König284710f2017-01-30 11:09:31 +01001887 * amdgpu_vm_free_mapping - free a mapping
1888 *
1889 * @adev: amdgpu_device pointer
1890 * @vm: requested vm
1891 * @mapping: mapping to be freed
1892 * @fence: fence of the unmap operation
1893 *
1894 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1895 */
1896static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1897 struct amdgpu_vm *vm,
1898 struct amdgpu_bo_va_mapping *mapping,
1899 struct dma_fence *fence)
1900{
Christian König451bc8e2017-02-14 16:02:52 +01001901 if (mapping->flags & AMDGPU_PTE_PRT)
1902 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001903 kfree(mapping);
1904}
1905
1906/**
Christian König451bc8e2017-02-14 16:02:52 +01001907 * amdgpu_vm_prt_fini - finish all prt mappings
1908 *
1909 * @adev: amdgpu_device pointer
1910 * @vm: requested vm
1911 *
1912 * Register a cleanup callback to disable PRT support after VM dies.
1913 */
1914static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1915{
Christian König3f3333f2017-08-03 14:02:13 +02001916 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001917 struct dma_fence *excl, **shared;
1918 unsigned i, shared_count;
1919 int r;
1920
1921 r = reservation_object_get_fences_rcu(resv, &excl,
1922 &shared_count, &shared);
1923 if (r) {
1924 /* Not enough memory to grab the fence list, as last resort
1925 * block for all the fences to complete.
1926 */
1927 reservation_object_wait_timeout_rcu(resv, true, false,
1928 MAX_SCHEDULE_TIMEOUT);
1929 return;
1930 }
1931
1932 /* Add a callback for each fence in the reservation object */
1933 amdgpu_vm_prt_get(adev);
1934 amdgpu_vm_add_prt_cb(adev, excl);
1935
1936 for (i = 0; i < shared_count; ++i) {
1937 amdgpu_vm_prt_get(adev);
1938 amdgpu_vm_add_prt_cb(adev, shared[i]);
1939 }
1940
1941 kfree(shared);
1942}
1943
1944/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1946 *
1947 * @adev: amdgpu_device pointer
1948 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001949 * @fence: optional resulting fence (unchanged if no work needed to be done
1950 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001951 *
1952 * Make sure all freed BOs are cleared in the PT.
1953 * Returns 0 for success.
1954 *
1955 * PTs have to be reserved and mutex must be locked!
1956 */
1957int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001958 struct amdgpu_vm *vm,
1959 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001960{
1961 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001962 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001963 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001964 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001965
1966 while (!list_empty(&vm->freed)) {
1967 mapping = list_first_entry(&vm->freed,
1968 struct amdgpu_bo_va_mapping, list);
1969 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001970
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001971 if (vm->pte_support_ats)
1972 init_pte_value = AMDGPU_PTE_SYSTEM;
1973
Christian König570144c2017-08-30 15:38:45 +02001974 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001975 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001976 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001977 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001978 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001979 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001980 return r;
Christian König284710f2017-01-30 11:09:31 +01001981 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001982 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001983
1984 if (fence && f) {
1985 dma_fence_put(*fence);
1986 *fence = f;
1987 } else {
1988 dma_fence_put(f);
1989 }
1990
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001991 return 0;
1992
1993}
1994
1995/**
Christian König73fb16e2017-08-16 11:13:48 +02001996 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001997 *
1998 * @adev: amdgpu_device pointer
1999 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02002000 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002001 *
Christian König73fb16e2017-08-16 11:13:48 +02002002 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002003 * Returns 0 for success.
2004 *
Christian König73fb16e2017-08-16 11:13:48 +02002005 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006 */
Christian König73fb16e2017-08-16 11:13:48 +02002007int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02002008 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009{
Christian König73fb16e2017-08-16 11:13:48 +02002010 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02002011 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002012
2013 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002014 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02002015 struct amdgpu_bo_va *bo_va;
2016
Christian König27c7b9a2017-08-01 11:27:36 +02002017 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002018 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002020
Christian König73fb16e2017-08-16 11:13:48 +02002021 /* Per VM BOs never need to bo cleared in the page tables */
2022 clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
2023
2024 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002025 if (r)
2026 return r;
2027
2028 spin_lock(&vm->status_lock);
2029 }
2030 spin_unlock(&vm->status_lock);
2031
Christian König91e1a522015-07-06 22:06:40 +02002032 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002033}
2034
2035/**
2036 * amdgpu_vm_bo_add - add a bo to a specific vm
2037 *
2038 * @adev: amdgpu_device pointer
2039 * @vm: requested vm
2040 * @bo: amdgpu buffer object
2041 *
Christian König8843dbb2016-01-26 12:17:11 +01002042 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002043 * Add @bo to the list of bos associated with the vm
2044 * Returns newly added bo_va or NULL for failure
2045 *
2046 * Object has to be reserved!
2047 */
2048struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2049 struct amdgpu_vm *vm,
2050 struct amdgpu_bo *bo)
2051{
2052 struct amdgpu_bo_va *bo_va;
2053
2054 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2055 if (bo_va == NULL) {
2056 return NULL;
2057 }
Christian Königec681542017-08-01 10:51:43 +02002058 bo_va->base.vm = vm;
2059 bo_va->base.bo = bo;
2060 INIT_LIST_HEAD(&bo_va->base.bo_list);
2061 INIT_LIST_HEAD(&bo_va->base.vm_status);
2062
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002064 INIT_LIST_HEAD(&bo_va->valids);
2065 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002066
Christian Königa5f6b5b2017-01-30 11:01:38 +01002067 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002068 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069
2070 return bo_va;
2071}
2072
Christian König73fb16e2017-08-16 11:13:48 +02002073
2074/**
2075 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2076 *
2077 * @adev: amdgpu_device pointer
2078 * @bo_va: bo_va to store the address
2079 * @mapping: the mapping to insert
2080 *
2081 * Insert a new mapping into all structures.
2082 */
2083static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2084 struct amdgpu_bo_va *bo_va,
2085 struct amdgpu_bo_va_mapping *mapping)
2086{
2087 struct amdgpu_vm *vm = bo_va->base.vm;
2088 struct amdgpu_bo *bo = bo_va->base.bo;
2089
Christian Königaebc5e62017-09-06 16:55:16 +02002090 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002091 list_add(&mapping->list, &bo_va->invalids);
2092 amdgpu_vm_it_insert(mapping, &vm->va);
2093
2094 if (mapping->flags & AMDGPU_PTE_PRT)
2095 amdgpu_vm_prt_get(adev);
2096
2097 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2098 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002099 if (list_empty(&bo_va->base.vm_status))
2100 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02002101 spin_unlock(&vm->status_lock);
2102 }
2103 trace_amdgpu_vm_bo_map(bo_va, mapping);
2104}
2105
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002106/**
2107 * amdgpu_vm_bo_map - map bo inside a vm
2108 *
2109 * @adev: amdgpu_device pointer
2110 * @bo_va: bo_va to store the address
2111 * @saddr: where to map the BO
2112 * @offset: requested offset in the BO
2113 * @flags: attributes of pages (read/write/valid/etc.)
2114 *
2115 * Add a mapping of the BO at the specefied addr into the VM.
2116 * Returns 0 for success, error for failure.
2117 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002118 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002119 */
2120int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2121 struct amdgpu_bo_va *bo_va,
2122 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002123 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002124{
Christian Königa9f87f62017-03-30 14:03:59 +02002125 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002126 struct amdgpu_bo *bo = bo_va->base.bo;
2127 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002128 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002129
Christian König0be52de2015-05-18 14:37:27 +02002130 /* validate the parameters */
2131 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002132 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002133 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002134
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002135 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002136 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002137 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002138 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002139 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002140
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002141 saddr /= AMDGPU_GPU_PAGE_SIZE;
2142 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2143
Christian Königa9f87f62017-03-30 14:03:59 +02002144 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2145 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002146 /* bo and tmp overlap, invalid addr */
2147 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002148 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002149 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002150 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002151 }
2152
2153 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002154 if (!mapping)
2155 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002156
Christian Königa9f87f62017-03-30 14:03:59 +02002157 mapping->start = saddr;
2158 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002159 mapping->offset = offset;
2160 mapping->flags = flags;
2161
Christian König73fb16e2017-08-16 11:13:48 +02002162 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002163
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002164 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002165}
2166
2167/**
Christian König80f95c52017-03-13 10:13:39 +01002168 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2169 *
2170 * @adev: amdgpu_device pointer
2171 * @bo_va: bo_va to store the address
2172 * @saddr: where to map the BO
2173 * @offset: requested offset in the BO
2174 * @flags: attributes of pages (read/write/valid/etc.)
2175 *
2176 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2177 * mappings as we do so.
2178 * Returns 0 for success, error for failure.
2179 *
2180 * Object has to be reserved and unreserved outside!
2181 */
2182int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2183 struct amdgpu_bo_va *bo_va,
2184 uint64_t saddr, uint64_t offset,
2185 uint64_t size, uint64_t flags)
2186{
2187 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002188 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002189 uint64_t eaddr;
2190 int r;
2191
2192 /* validate the parameters */
2193 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2194 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2195 return -EINVAL;
2196
2197 /* make sure object fit at this offset */
2198 eaddr = saddr + size - 1;
2199 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002200 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002201 return -EINVAL;
2202
2203 /* Allocate all the needed memory */
2204 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2205 if (!mapping)
2206 return -ENOMEM;
2207
Christian Königec681542017-08-01 10:51:43 +02002208 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002209 if (r) {
2210 kfree(mapping);
2211 return r;
2212 }
2213
2214 saddr /= AMDGPU_GPU_PAGE_SIZE;
2215 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2216
Christian Königa9f87f62017-03-30 14:03:59 +02002217 mapping->start = saddr;
2218 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002219 mapping->offset = offset;
2220 mapping->flags = flags;
2221
Christian König73fb16e2017-08-16 11:13:48 +02002222 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002223
2224 return 0;
2225}
2226
2227/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002228 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2229 *
2230 * @adev: amdgpu_device pointer
2231 * @bo_va: bo_va to remove the address from
2232 * @saddr: where to the BO is mapped
2233 *
2234 * Remove a mapping of the BO at the specefied addr from the VM.
2235 * Returns 0 for success, error for failure.
2236 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002237 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002238 */
2239int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2240 struct amdgpu_bo_va *bo_va,
2241 uint64_t saddr)
2242{
2243 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002244 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002245 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002246
Christian König6c7fc502015-06-05 20:56:17 +02002247 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002248
Christian König7fc11952015-07-30 11:53:42 +02002249 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002250 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251 break;
2252 }
2253
Christian König7fc11952015-07-30 11:53:42 +02002254 if (&mapping->list == &bo_va->valids) {
2255 valid = false;
2256
2257 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002258 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002259 break;
2260 }
2261
Christian König32b41ac2016-03-08 18:03:27 +01002262 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002263 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264 }
Christian König32b41ac2016-03-08 18:03:27 +01002265
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002266 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002267 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002268 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002269 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002270
Christian Könige17841b2016-03-08 17:52:01 +01002271 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002272 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002273 else
Christian König284710f2017-01-30 11:09:31 +01002274 amdgpu_vm_free_mapping(adev, vm, mapping,
2275 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002276
2277 return 0;
2278}
2279
2280/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002281 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2282 *
2283 * @adev: amdgpu_device pointer
2284 * @vm: VM structure to use
2285 * @saddr: start of the range
2286 * @size: size of the range
2287 *
2288 * Remove all mappings in a range, split them as appropriate.
2289 * Returns 0 for success, error for failure.
2290 */
2291int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2292 struct amdgpu_vm *vm,
2293 uint64_t saddr, uint64_t size)
2294{
2295 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002296 LIST_HEAD(removed);
2297 uint64_t eaddr;
2298
2299 eaddr = saddr + size - 1;
2300 saddr /= AMDGPU_GPU_PAGE_SIZE;
2301 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2302
2303 /* Allocate all the needed memory */
2304 before = kzalloc(sizeof(*before), GFP_KERNEL);
2305 if (!before)
2306 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002307 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002308
2309 after = kzalloc(sizeof(*after), GFP_KERNEL);
2310 if (!after) {
2311 kfree(before);
2312 return -ENOMEM;
2313 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002314 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002315
2316 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002317 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2318 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002319 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002320 if (tmp->start < saddr) {
2321 before->start = tmp->start;
2322 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002323 before->offset = tmp->offset;
2324 before->flags = tmp->flags;
2325 list_add(&before->list, &tmp->list);
2326 }
2327
2328 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002329 if (tmp->last > eaddr) {
2330 after->start = eaddr + 1;
2331 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002332 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002333 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002334 after->flags = tmp->flags;
2335 list_add(&after->list, &tmp->list);
2336 }
2337
2338 list_del(&tmp->list);
2339 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002340
2341 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002342 }
2343
2344 /* And free them up */
2345 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002346 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002347 list_del(&tmp->list);
2348
Christian Königa9f87f62017-03-30 14:03:59 +02002349 if (tmp->start < saddr)
2350 tmp->start = saddr;
2351 if (tmp->last > eaddr)
2352 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002353
Christian Königaebc5e62017-09-06 16:55:16 +02002354 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002355 list_add(&tmp->list, &vm->freed);
2356 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2357 }
2358
Junwei Zhang27f6d612017-03-16 16:09:24 +08002359 /* Insert partial mapping before the range */
2360 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002361 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002362 if (before->flags & AMDGPU_PTE_PRT)
2363 amdgpu_vm_prt_get(adev);
2364 } else {
2365 kfree(before);
2366 }
2367
2368 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002369 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002370 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002371 if (after->flags & AMDGPU_PTE_PRT)
2372 amdgpu_vm_prt_get(adev);
2373 } else {
2374 kfree(after);
2375 }
2376
2377 return 0;
2378}
2379
2380/**
Christian Königaebc5e62017-09-06 16:55:16 +02002381 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2382 *
2383 * @vm: the requested VM
2384 *
2385 * Find a mapping by it's address.
2386 */
2387struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2388 uint64_t addr)
2389{
2390 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2391}
2392
2393/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002394 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2395 *
2396 * @adev: amdgpu_device pointer
2397 * @bo_va: requested bo_va
2398 *
Christian König8843dbb2016-01-26 12:17:11 +01002399 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002400 *
2401 * Object have to be reserved!
2402 */
2403void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2404 struct amdgpu_bo_va *bo_va)
2405{
2406 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002407 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002408
Christian Königec681542017-08-01 10:51:43 +02002409 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002410
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002411 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002412 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002413 spin_unlock(&vm->status_lock);
2414
Christian König7fc11952015-07-30 11:53:42 +02002415 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002416 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002417 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002418 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002419 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002420 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002421 }
Christian König7fc11952015-07-30 11:53:42 +02002422 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2423 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002424 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002425 amdgpu_vm_free_mapping(adev, vm, mapping,
2426 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002427 }
Christian König32b41ac2016-03-08 18:03:27 +01002428
Chris Wilsonf54d1862016-10-25 13:00:45 +01002429 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002430 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002431}
2432
2433/**
2434 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2435 *
2436 * @adev: amdgpu_device pointer
2437 * @vm: requested vm
2438 * @bo: amdgpu buffer object
2439 *
Christian König8843dbb2016-01-26 12:17:11 +01002440 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002441 */
2442void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002443 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444{
Christian Königec681542017-08-01 10:51:43 +02002445 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002446
Christian Königec681542017-08-01 10:51:43 +02002447 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002448 struct amdgpu_vm *vm = bo_base->vm;
2449
Christian König3d7d4d32017-08-23 16:13:33 +02002450 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002451 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2452 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002453 if (bo->tbo.type == ttm_bo_type_kernel)
2454 list_move(&bo_base->vm_status, &vm->evicted);
2455 else
2456 list_move_tail(&bo_base->vm_status,
2457 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002458 spin_unlock(&bo_base->vm->status_lock);
2459 continue;
2460 }
2461
Christian Königea097292017-08-09 14:15:46 +02002462 if (bo->tbo.type == ttm_bo_type_kernel) {
2463 spin_lock(&bo_base->vm->status_lock);
2464 if (list_empty(&bo_base->vm_status))
2465 list_add(&bo_base->vm_status, &vm->relocated);
2466 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002467 continue;
Christian Königea097292017-08-09 14:15:46 +02002468 }
Christian König3f3333f2017-08-03 14:02:13 +02002469
Christian Königec681542017-08-01 10:51:43 +02002470 spin_lock(&bo_base->vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002471 if (list_empty(&bo_base->vm_status))
2472 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002473 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002474 }
2475}
2476
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002477static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2478{
2479 /* Total bits covered by PD + PTs */
2480 unsigned bits = ilog2(vm_size) + 18;
2481
2482 /* Make sure the PD is 4K in size up to 8GB address space.
2483 Above that split equal between PD and PTs */
2484 if (vm_size <= 8)
2485 return (bits - 9);
2486 else
2487 return ((bits + 3) / 2);
2488}
2489
2490/**
Roger Hed07f14b2017-08-15 16:05:59 +08002491 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2492 *
2493 * @adev: amdgpu_device pointer
2494 * @fragment_size_default: the default fragment size if it's set auto
2495 */
2496void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev, uint32_t fragment_size_default)
2497{
2498 if (amdgpu_vm_fragment_size == -1)
2499 adev->vm_manager.fragment_size = fragment_size_default;
2500 else
2501 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2502}
2503
2504/**
2505 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002506 *
2507 * @adev: amdgpu_device pointer
2508 * @vm_size: the default vm size if it's set auto
2509 */
Roger Hed07f14b2017-08-15 16:05:59 +08002510void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size, uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002511{
2512 /* adjust vm size firstly */
2513 if (amdgpu_vm_size == -1)
2514 adev->vm_manager.vm_size = vm_size;
2515 else
2516 adev->vm_manager.vm_size = amdgpu_vm_size;
2517
2518 /* block size depends on vm size */
2519 if (amdgpu_vm_block_size == -1)
2520 adev->vm_manager.block_size =
2521 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2522 else
2523 adev->vm_manager.block_size = amdgpu_vm_block_size;
2524
Roger Hed07f14b2017-08-15 16:05:59 +08002525 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2526
2527 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2528 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2529 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002530}
2531
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002532/**
2533 * amdgpu_vm_init - initialize a vm instance
2534 *
2535 * @adev: amdgpu_device pointer
2536 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002537 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002538 *
Christian König8843dbb2016-01-26 12:17:11 +01002539 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002541int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2542 int vm_context)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002543{
2544 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002545 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002546 unsigned ring_instance;
2547 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002548 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002549 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002550 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002551 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002552
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002553 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002554 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002555 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2556 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002557 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002558 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002559 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002560 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002561 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002562
Christian König2bd9ccf2016-02-01 12:53:58 +01002563 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002564
2565 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2566 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2567 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002568 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2569 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2570 rq, amdgpu_sched_jobs);
2571 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002572 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002573
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002574 vm->pte_support_ats = false;
2575
2576 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002577 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2578 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002579
2580 if (adev->asic_type == CHIP_RAVEN) {
2581 vm->pte_support_ats = true;
2582 init_pde_value = AMDGPU_PTE_SYSTEM | AMDGPU_PDE_PTE;
2583 }
2584 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002585 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2586 AMDGPU_VM_USE_CPU_FOR_GFX);
2587 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2588 vm->use_cpu_for_update ? "CPU" : "SDMA");
2589 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2590 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002591 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002592
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002593 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2594 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2595 if (vm->use_cpu_for_update)
2596 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2597 else
2598 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2599 AMDGPU_GEM_CREATE_SHADOW);
2600
Christian Königf566ceb2016-10-27 20:04:38 +02002601 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002602 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002603 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002604 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002605 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002606 goto error_free_sched_entity;
2607
Christian König3f3333f2017-08-03 14:02:13 +02002608 vm->root.base.vm = vm;
2609 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2610 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002611
2612 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002613 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002614 if (r)
2615 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002616
Christian König3f3333f2017-08-03 14:02:13 +02002617 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
Felix Kuehlingca290da2017-08-25 20:15:04 -04002618 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König3f3333f2017-08-03 14:02:13 +02002619 if (r)
2620 goto error_free_root;
Christian König3f3333f2017-08-03 14:02:13 +02002621 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002622
2623 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002624
Christian König67003a12016-10-12 14:46:26 +02002625error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002626 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2627 amdgpu_bo_unref(&vm->root.base.bo);
2628 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002629
2630error_free_sched_entity:
2631 amd_sched_entity_fini(&ring->sched, &vm->entity);
2632
2633 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002634}
2635
2636/**
Christian Königf566ceb2016-10-27 20:04:38 +02002637 * amdgpu_vm_free_levels - free PD/PT levels
2638 *
2639 * @level: PD/PT starting level to free
2640 *
2641 * Free the page directory or page table level and all sub levels.
2642 */
2643static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2644{
2645 unsigned i;
2646
Christian König3f3333f2017-08-03 14:02:13 +02002647 if (level->base.bo) {
2648 list_del(&level->base.bo_list);
2649 list_del(&level->base.vm_status);
2650 amdgpu_bo_unref(&level->base.bo->shadow);
2651 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002652 }
2653
2654 if (level->entries)
2655 for (i = 0; i <= level->last_entry_used; i++)
2656 amdgpu_vm_free_levels(&level->entries[i]);
2657
Michal Hocko20981052017-05-17 14:23:12 +02002658 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002659}
2660
2661/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002662 * amdgpu_vm_fini - tear down a vm instance
2663 *
2664 * @adev: amdgpu_device pointer
2665 * @vm: requested vm
2666 *
Christian König8843dbb2016-01-26 12:17:11 +01002667 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002668 * Unbind the VM and remove all bos from the vm bo list
2669 */
2670void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2671{
2672 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002673 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002674 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002675
Christian König2d55e452016-02-08 17:37:38 +01002676 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002677
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002678 if (!RB_EMPTY_ROOT(&vm->va)) {
2679 dev_err(adev->dev, "still active bo inside vm\n");
2680 }
Christian Königa9f87f62017-03-30 14:03:59 +02002681 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002682 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002683 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002684 kfree(mapping);
2685 }
2686 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002687 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002688 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002689 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002690 }
Christian König284710f2017-01-30 11:09:31 +01002691
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002692 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002693 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002694 }
2695
Christian Königf566ceb2016-10-27 20:04:38 +02002696 amdgpu_vm_free_levels(&vm->root);
Christian Königd5884512017-09-08 14:09:41 +02002697 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002698 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2699 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002700}
Christian Königea89f8c2015-11-15 20:52:06 +01002701
2702/**
Christian Königa9a78b32016-01-21 10:19:11 +01002703 * amdgpu_vm_manager_init - init the VM manager
2704 *
2705 * @adev: amdgpu_device pointer
2706 *
2707 * Initialize the VM manager structures
2708 */
2709void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2710{
Christian König76456702017-04-06 17:52:39 +02002711 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002712
Christian König76456702017-04-06 17:52:39 +02002713 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2714 struct amdgpu_vm_id_manager *id_mgr =
2715 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002716
Christian König76456702017-04-06 17:52:39 +02002717 mutex_init(&id_mgr->lock);
2718 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002719 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002720
2721 /* skip over VMID 0, since it is the system VM */
2722 for (j = 1; j < id_mgr->num_ids; ++j) {
2723 amdgpu_vm_reset_id(adev, i, j);
2724 amdgpu_sync_create(&id_mgr->ids[i].active);
2725 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2726 }
Christian König971fe9a92016-03-01 15:09:25 +01002727 }
Christian König2d55e452016-02-08 17:37:38 +01002728
Chris Wilsonf54d1862016-10-25 13:00:45 +01002729 adev->vm_manager.fence_context =
2730 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002731 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2732 adev->vm_manager.seqno[i] = 0;
2733
Christian König2d55e452016-02-08 17:37:38 +01002734 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002735 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002736 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002737 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002738
2739 /* If not overridden by the user, by default, only in large BAR systems
2740 * Compute VM tables will be updated by CPU
2741 */
2742#ifdef CONFIG_X86_64
2743 if (amdgpu_vm_update_mode == -1) {
2744 if (amdgpu_vm_is_large_bar(adev))
2745 adev->vm_manager.vm_update_mode =
2746 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2747 else
2748 adev->vm_manager.vm_update_mode = 0;
2749 } else
2750 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2751#else
2752 adev->vm_manager.vm_update_mode = 0;
2753#endif
2754
Christian Königa9a78b32016-01-21 10:19:11 +01002755}
2756
2757/**
Christian Königea89f8c2015-11-15 20:52:06 +01002758 * amdgpu_vm_manager_fini - cleanup VM manager
2759 *
2760 * @adev: amdgpu_device pointer
2761 *
2762 * Cleanup the VM manager and free resources.
2763 */
2764void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2765{
Christian König76456702017-04-06 17:52:39 +02002766 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002767
Christian König76456702017-04-06 17:52:39 +02002768 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2769 struct amdgpu_vm_id_manager *id_mgr =
2770 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002771
Christian König76456702017-04-06 17:52:39 +02002772 mutex_destroy(&id_mgr->lock);
2773 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2774 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2775
2776 amdgpu_sync_free(&id->active);
2777 dma_fence_put(id->flushed_updates);
2778 dma_fence_put(id->last_flush);
2779 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002780 }
Christian Königea89f8c2015-11-15 20:52:06 +01002781}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002782
2783int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2784{
2785 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002786 struct amdgpu_device *adev = dev->dev_private;
2787 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2788 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002789
2790 switch (args->in.op) {
2791 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002792 /* current, we only have requirement to reserve vmid from gfxhub */
2793 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2794 AMDGPU_GFXHUB);
2795 if (r)
2796 return r;
2797 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002798 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002799 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002800 break;
2801 default:
2802 return -EINVAL;
2803 }
2804
2805 return 0;
2806}