blob: f1e541e9b514c61c053636de333eecc255c78750 [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>
Felix Kuehling02208442017-08-25 20:40:26 -040030#include <linux/idr.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031#include <drm/drmP.h>
32#include <drm/amdgpu_drm.h>
33#include "amdgpu.h"
34#include "amdgpu_trace.h"
35
36/*
Felix Kuehling02208442017-08-25 20:40:26 -040037 * PASID manager
38 *
39 * PASIDs are global address space identifiers that can be shared
40 * between the GPU, an IOMMU and the driver. VMs on different devices
41 * may use the same PASID if they share the same address
42 * space. Therefore PASIDs are allocated using a global IDA. VMs are
43 * looked up from the PASID per amdgpu_device.
44 */
45static DEFINE_IDA(amdgpu_vm_pasid_ida);
46
47/**
48 * amdgpu_vm_alloc_pasid - Allocate a PASID
49 * @bits: Maximum width of the PASID in bits, must be at least 1
50 *
51 * Allocates a PASID of the given width while keeping smaller PASIDs
52 * available if possible.
53 *
54 * Returns a positive integer on success. Returns %-EINVAL if bits==0.
55 * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
56 * memory allocation failure.
57 */
58int amdgpu_vm_alloc_pasid(unsigned int bits)
59{
60 int pasid = -EINVAL;
61
62 for (bits = min(bits, 31U); bits > 0; bits--) {
63 pasid = ida_simple_get(&amdgpu_vm_pasid_ida,
64 1U << (bits - 1), 1U << bits,
65 GFP_KERNEL);
66 if (pasid != -ENOSPC)
67 break;
68 }
69
70 return pasid;
71}
72
73/**
74 * amdgpu_vm_free_pasid - Free a PASID
75 * @pasid: PASID to free
76 */
77void amdgpu_vm_free_pasid(unsigned int pasid)
78{
79 ida_simple_remove(&amdgpu_vm_pasid_ida, pasid);
80}
81
82/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 * GPUVM
84 * GPUVM is similar to the legacy gart on older asics, however
85 * rather than there being a single global gart table
86 * for the entire GPU, there are multiple VM page tables active
87 * at any given time. The VM page tables can contain a mix
88 * vram pages and system memory pages and system memory pages
89 * can be mapped as snooped (cached system pages) or unsnooped
90 * (uncached system pages).
91 * Each VM has an ID associated with it and there is a page table
92 * associated with each VMID. When execting a command buffer,
93 * the kernel tells the the ring what VMID to use for that command
94 * buffer. VMIDs are allocated dynamically as commands are submitted.
95 * The userspace drivers maintain their own address space and the kernel
96 * sets up their pages tables accordingly when they submit their
97 * command buffers and a VMID is assigned.
98 * Cayman/Trinity support up to 8 active VMs at any given time;
99 * SI supports 16.
100 */
101
Christian Königa9f87f62017-03-30 14:03:59 +0200102#define START(node) ((node)->start)
103#define LAST(node) ((node)->last)
104
105INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
106 START, LAST, static, amdgpu_vm_it)
107
108#undef START
109#undef LAST
110
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400111/* Local structure. Encapsulate some VM table update parameters to reduce
112 * the number of function parameters
113 */
Christian König29efc4f2016-08-04 14:52:50 +0200114struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +0200115 /* amdgpu device we do this update for */
116 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +0200117 /* optional amdgpu_vm we do this update for */
118 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400119 /* address where to copy page table entries from */
120 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400121 /* indirect buffer to fill with commands */
122 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +0200123 /* Function which actually does the update */
124 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
125 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800126 uint64_t flags);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400127 /* The next two are used during VM update by CPU
128 * DMA addresses to use for mapping
129 * Kernel pointer of PD/PT BO that needs to be updated
130 */
131 dma_addr_t *pages_addr;
132 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400133};
134
Christian König284710f2017-01-30 11:09:31 +0100135/* Helper to disable partial resident texture feature from a fence callback */
136struct amdgpu_prt_cb {
137 struct amdgpu_device *adev;
138 struct dma_fence_cb cb;
139};
140
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141/**
Christian König50783142017-11-27 14:01:51 +0100142 * amdgpu_vm_level_shift - return the addr shift for each level
143 *
144 * @adev: amdgpu_device pointer
145 *
146 * Returns the number of bits the pfn needs to be right shifted for a level.
147 */
148static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
149 unsigned level)
150{
151 if (level != adev->vm_manager.num_level)
152 return 9 * (adev->vm_manager.num_level - level - 1) +
153 adev->vm_manager.block_size;
154 else
155 /* For the page tables on the leaves */
156 return 0;
157}
158
159/**
Christian König72a7ec52016-10-19 11:03:57 +0200160 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161 *
162 * @adev: amdgpu_device pointer
163 *
Christian König72a7ec52016-10-19 11:03:57 +0200164 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 */
Christian König72a7ec52016-10-19 11:03:57 +0200166static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
167 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168{
Christian König72a7ec52016-10-19 11:03:57 +0200169 if (level == 0)
170 /* For the root directory */
171 return adev->vm_manager.max_pfn >>
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800172 (adev->vm_manager.block_size *
173 adev->vm_manager.num_level);
Christian König72a7ec52016-10-19 11:03:57 +0200174 else if (level == adev->vm_manager.num_level)
175 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800176 return AMDGPU_VM_PTE_COUNT(adev);
Christian König72a7ec52016-10-19 11:03:57 +0200177 else
178 /* Everything in between */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800179 return 1 << adev->vm_manager.block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180}
181
182/**
Christian König72a7ec52016-10-19 11:03:57 +0200183 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184 *
185 * @adev: amdgpu_device pointer
186 *
Christian König72a7ec52016-10-19 11:03:57 +0200187 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188 */
Christian König72a7ec52016-10-19 11:03:57 +0200189static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190{
Christian König72a7ec52016-10-19 11:03:57 +0200191 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192}
193
194/**
Christian König56467eb2015-12-11 15:16:32 +0100195 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196 *
197 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100198 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100199 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200 *
201 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100202 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400203 */
Christian König56467eb2015-12-11 15:16:32 +0100204void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
205 struct list_head *validated,
206 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207{
Christian König3f3333f2017-08-03 14:02:13 +0200208 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100209 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200210 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100211 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100212 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100213 list_add(&entry->tv.head, validated);
214}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215
Christian König56467eb2015-12-11 15:16:32 +0100216/**
Christian Königf7da30d2016-09-28 12:03:04 +0200217 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100218 *
Christian König5a712a82016-06-21 16:28:15 +0200219 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100220 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200221 * @validate: callback to do the validation
222 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400223 *
Christian Königf7da30d2016-09-28 12:03:04 +0200224 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225 */
Christian Königf7da30d2016-09-28 12:03:04 +0200226int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
227 int (*validate)(void *p, struct amdgpu_bo *bo),
228 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400229{
Christian König3f3333f2017-08-03 14:02:13 +0200230 struct ttm_bo_global *glob = adev->mman.bdev.glob;
231 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232
Christian König3f3333f2017-08-03 14:02:13 +0200233 spin_lock(&vm->status_lock);
234 while (!list_empty(&vm->evicted)) {
235 struct amdgpu_vm_bo_base *bo_base;
236 struct amdgpu_bo *bo;
Christian König5a712a82016-06-21 16:28:15 +0200237
Christian König3f3333f2017-08-03 14:02:13 +0200238 bo_base = list_first_entry(&vm->evicted,
239 struct amdgpu_vm_bo_base,
240 vm_status);
241 spin_unlock(&vm->status_lock);
Christian Königeceb8a12016-01-11 15:35:21 +0100242
Christian König3f3333f2017-08-03 14:02:13 +0200243 bo = bo_base->bo;
244 BUG_ON(!bo);
245 if (bo->parent) {
246 r = validate(param, bo);
247 if (r)
248 return r;
Christian König34d7be52017-08-24 12:32:55 +0200249
Christian König3f3333f2017-08-03 14:02:13 +0200250 spin_lock(&glob->lru_lock);
251 ttm_bo_move_to_lru_tail(&bo->tbo);
252 if (bo->shadow)
253 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
254 spin_unlock(&glob->lru_lock);
255 }
256
Christian König73fb16e2017-08-16 11:13:48 +0200257 if (bo->tbo.type == ttm_bo_type_kernel &&
258 vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +0200259 r = amdgpu_bo_kmap(bo, NULL);
260 if (r)
261 return r;
262 }
263
264 spin_lock(&vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200265 if (bo->tbo.type != ttm_bo_type_kernel)
266 list_move(&bo_base->vm_status, &vm->moved);
267 else
268 list_move(&bo_base->vm_status, &vm->relocated);
Christian König3f3333f2017-08-03 14:02:13 +0200269 }
270 spin_unlock(&vm->status_lock);
Christian König34d7be52017-08-24 12:32:55 +0200271
272 return 0;
273}
274
275/**
276 * amdgpu_vm_ready - check VM is ready for updates
277 *
Christian König34d7be52017-08-24 12:32:55 +0200278 * @vm: VM to check
279 *
280 * Check if all VM PDs/PTs are ready for updates
281 */
Christian König3f3333f2017-08-03 14:02:13 +0200282bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200283{
Christian König3f3333f2017-08-03 14:02:13 +0200284 bool ready;
Christian König34d7be52017-08-24 12:32:55 +0200285
Christian König3f3333f2017-08-03 14:02:13 +0200286 spin_lock(&vm->status_lock);
287 ready = list_empty(&vm->evicted);
288 spin_unlock(&vm->status_lock);
289
290 return ready;
Christian Königeceb8a12016-01-11 15:35:21 +0100291}
292
293/**
Christian Königf566ceb2016-10-27 20:04:38 +0200294 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
295 *
296 * @adev: amdgpu_device pointer
297 * @vm: requested vm
298 * @saddr: start of the address range
299 * @eaddr: end of the address range
300 *
301 * Make sure the page directories and page tables are allocated
302 */
303static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
304 struct amdgpu_vm *vm,
305 struct amdgpu_vm_pt *parent,
306 uint64_t saddr, uint64_t eaddr,
307 unsigned level)
308{
Christian König50783142017-11-27 14:01:51 +0100309 unsigned shift = amdgpu_vm_level_shift(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200310 unsigned pt_idx, from, to;
311 int r;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400312 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400313 uint64_t init_value = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200314
315 if (!parent->entries) {
316 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
317
Michal Hocko20981052017-05-17 14:23:12 +0200318 parent->entries = kvmalloc_array(num_entries,
319 sizeof(struct amdgpu_vm_pt),
320 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200321 if (!parent->entries)
322 return -ENOMEM;
323 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
324 }
325
Felix Kuehling1866bac2017-03-28 20:36:12 -0400326 from = saddr >> shift;
327 to = eaddr >> shift;
328 if (from >= amdgpu_vm_num_entries(adev, level) ||
329 to >= amdgpu_vm_num_entries(adev, level))
330 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200331
332 if (to > parent->last_entry_used)
333 parent->last_entry_used = to;
334
335 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400336 saddr = saddr & ((1 << shift) - 1);
337 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200338
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400339 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
340 AMDGPU_GEM_CREATE_VRAM_CLEARED;
341 if (vm->use_cpu_for_update)
342 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
343 else
344 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
345 AMDGPU_GEM_CREATE_SHADOW);
346
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400347 if (vm->pte_support_ats) {
Yong Zhao6d16dac2017-08-31 15:55:00 -0400348 init_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400349 if (level != adev->vm_manager.num_level - 1)
350 init_value |= AMDGPU_PDE_PTE;
Yong Zhao6d16dac2017-08-31 15:55:00 -0400351
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400352 }
353
Christian Königf566ceb2016-10-27 20:04:38 +0200354 /* walk over the address space and allocate the page tables */
355 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200356 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200357 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
358 struct amdgpu_bo *pt;
359
Christian König3f3333f2017-08-03 14:02:13 +0200360 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200361 r = amdgpu_bo_create(adev,
362 amdgpu_vm_bo_size(adev, level),
363 AMDGPU_GPU_PAGE_SIZE, true,
364 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400365 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400366 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200367 if (r)
368 return r;
369
Christian König0a096fb2017-07-12 10:01:48 +0200370 if (vm->use_cpu_for_update) {
371 r = amdgpu_bo_kmap(pt, NULL);
372 if (r) {
373 amdgpu_bo_unref(&pt);
374 return r;
375 }
376 }
377
Christian Königf566ceb2016-10-27 20:04:38 +0200378 /* Keep a reference to the root directory to avoid
379 * freeing them up in the wrong order.
380 */
Christian König0f2fc432017-08-31 10:46:20 +0200381 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200382
Christian König3f3333f2017-08-03 14:02:13 +0200383 entry->base.vm = vm;
384 entry->base.bo = pt;
385 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200386 spin_lock(&vm->status_lock);
387 list_add(&entry->base.vm_status, &vm->relocated);
388 spin_unlock(&vm->status_lock);
Christian Königf566ceb2016-10-27 20:04:38 +0200389 entry->addr = 0;
390 }
391
392 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400393 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
394 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
395 ((1 << shift) - 1);
396 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
397 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200398 if (r)
399 return r;
400 }
401 }
402
403 return 0;
404}
405
Christian König663e4572017-03-13 10:13:37 +0100406/**
407 * amdgpu_vm_alloc_pts - Allocate page tables.
408 *
409 * @adev: amdgpu_device pointer
410 * @vm: VM to allocate page tables for
411 * @saddr: Start address which needs to be allocated
412 * @size: Size from start address we need.
413 *
414 * Make sure the page tables are allocated.
415 */
416int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
417 struct amdgpu_vm *vm,
418 uint64_t saddr, uint64_t size)
419{
Felix Kuehling22770e52017-03-28 20:24:53 -0400420 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100421 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100422
423 /* validate the parameters */
424 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
425 return -EINVAL;
426
427 eaddr = saddr + size - 1;
428 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
429 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400430 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100431 last_pfn, adev->vm_manager.max_pfn);
432 return -EINVAL;
433 }
434
435 saddr /= AMDGPU_GPU_PAGE_SIZE;
436 eaddr /= AMDGPU_GPU_PAGE_SIZE;
437
Christian Königf566ceb2016-10-27 20:04:38 +0200438 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100439}
440
Christian König641e9402017-04-03 13:59:25 +0200441/**
442 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
443 *
444 * @adev: amdgpu_device pointer
445 * @id: VMID structure
446 *
447 * Check if GPU reset occured since last use of the VMID.
448 */
449static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
450 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800451{
452 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200453 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800454}
455
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800456static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
457{
458 return !!vm->reserved_vmid[vmhub];
459}
460
461/* idr_mgr->lock must be held */
462static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
463 struct amdgpu_ring *ring,
464 struct amdgpu_sync *sync,
465 struct dma_fence *fence,
466 struct amdgpu_job *job)
467{
468 struct amdgpu_device *adev = ring->adev;
469 unsigned vmhub = ring->funcs->vmhub;
470 uint64_t fence_context = adev->fence_context + ring->idx;
471 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
472 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
473 struct dma_fence *updates = sync->last_vm_update;
474 int r = 0;
475 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200476 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800477
478 flushed = id->flushed_updates;
479 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
480 (atomic64_read(&id->owner) != vm->client_id) ||
481 (job->vm_pd_addr != id->pd_gpu_addr) ||
482 (updates && (!flushed || updates->context != flushed->context ||
483 dma_fence_is_later(updates, flushed))) ||
484 (!id->last_flush || (id->last_flush->context != fence_context &&
485 !dma_fence_is_signaled(id->last_flush)))) {
486 needs_flush = true;
487 /* to prevent one context starved by another context */
488 id->pd_gpu_addr = 0;
489 tmp = amdgpu_sync_peek_fence(&id->active, ring);
490 if (tmp) {
491 r = amdgpu_sync_fence(adev, sync, tmp);
492 return r;
493 }
494 }
495
496 /* Good we can use this VMID. Remember this submission as
497 * user of the VMID.
498 */
499 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
500 if (r)
501 goto out;
502
503 if (updates && (!flushed || updates->context != flushed->context ||
504 dma_fence_is_later(updates, flushed))) {
505 dma_fence_put(id->flushed_updates);
506 id->flushed_updates = dma_fence_get(updates);
507 }
508 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800509 atomic64_set(&id->owner, vm->client_id);
510 job->vm_needs_flush = needs_flush;
511 if (needs_flush) {
512 dma_fence_put(id->last_flush);
513 id->last_flush = NULL;
514 }
515 job->vm_id = id - id_mgr->ids;
516 trace_amdgpu_vm_grab_id(vm, ring, job);
517out:
518 return r;
519}
520
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521/**
522 * amdgpu_vm_grab_id - allocate the next free VMID
523 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200525 * @ring: ring we want to submit job to
526 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100527 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528 *
Christian König7f8a5292015-07-20 16:09:40 +0200529 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 */
Christian König7f8a5292015-07-20 16:09:40 +0200531int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100532 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800533 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200536 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200537 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200538 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100539 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200540 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100541 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200542 unsigned i;
543 int r = 0;
544
Christian König76456702017-04-06 17:52:39 +0200545 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800546 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
547 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
548 mutex_unlock(&id_mgr->lock);
549 return r;
550 }
551 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
552 if (!fences) {
553 mutex_unlock(&id_mgr->lock);
554 return -ENOMEM;
555 }
Christian König36fd7c52016-05-23 15:30:08 +0200556 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200557 i = 0;
Christian König76456702017-04-06 17:52:39 +0200558 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200559 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
560 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200561 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200562 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200563 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100564
Christian König1fbb2e92016-06-01 10:47:36 +0200565 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200566 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200567 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
568 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100569 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200570 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200571
Christian König1fbb2e92016-06-01 10:47:36 +0200572 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100573 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200574
Chris Wilsonf54d1862016-10-25 13:00:45 +0100575 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200576 seqno, true);
577 if (!array) {
578 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100579 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200580 kfree(fences);
581 r = -ENOMEM;
582 goto error;
583 }
Christian König8d76001e2016-05-23 16:00:32 +0200584
Christian König8d76001e2016-05-23 16:00:32 +0200585
Christian König1fbb2e92016-06-01 10:47:36 +0200586 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100587 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200588 if (r)
589 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200590
Christian König76456702017-04-06 17:52:39 +0200591 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200592 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200593
Christian König1fbb2e92016-06-01 10:47:36 +0200594 }
595 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200596
Christian König6f1ceab2017-07-11 16:59:21 +0200597 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200598 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200599 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100600 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200601 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200602
Christian König1fbb2e92016-06-01 10:47:36 +0200603 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200604 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800605 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200606
607 if (atomic64_read(&id->owner) != vm->client_id)
608 continue;
609
Chunming Zhoufd53be32016-07-01 17:59:01 +0800610 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200611 continue;
612
Christian König87c910d2017-03-30 16:56:20 +0200613 if (!id->last_flush ||
614 (id->last_flush->context != fence_context &&
615 !dma_fence_is_signaled(id->last_flush)))
616 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200617
618 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200619 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
620 needs_flush = true;
621
622 /* Concurrent flushes are only possible starting with Vega10 */
623 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200624 continue;
625
Christian König3dab83b2016-06-01 13:31:17 +0200626 /* Good we can use this VMID. Remember this submission as
627 * user of the VMID.
628 */
Christian König1fbb2e92016-06-01 10:47:36 +0200629 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
630 if (r)
631 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200632
Christian König87c910d2017-03-30 16:56:20 +0200633 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
634 dma_fence_put(id->flushed_updates);
635 id->flushed_updates = dma_fence_get(updates);
636 }
Christian König8d76001e2016-05-23 16:00:32 +0200637
Christian König87c910d2017-03-30 16:56:20 +0200638 if (needs_flush)
639 goto needs_flush;
640 else
641 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200642
Christian König4f618e72017-04-06 15:18:21 +0200643 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800644
Christian König1fbb2e92016-06-01 10:47:36 +0200645 /* Still no ID to use? Then use the idle one found earlier */
646 id = idle;
647
648 /* Remember this submission as user of the VMID */
649 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100650 if (r)
651 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100652
Christian König87c910d2017-03-30 16:56:20 +0200653 id->pd_gpu_addr = job->vm_pd_addr;
654 dma_fence_put(id->flushed_updates);
655 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200656 atomic64_set(&id->owner, vm->client_id);
657
658needs_flush:
659 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100660 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100661 id->last_flush = NULL;
662
Christian König87c910d2017-03-30 16:56:20 +0200663no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200664 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665
Christian König76456702017-04-06 17:52:39 +0200666 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200667 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100668
669error:
Christian König76456702017-04-06 17:52:39 +0200670 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100671 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672}
673
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800674static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
675 struct amdgpu_vm *vm,
676 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400677{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800678 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400679
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800680 mutex_lock(&id_mgr->lock);
681 if (vm->reserved_vmid[vmhub]) {
682 list_add(&vm->reserved_vmid[vmhub]->list,
683 &id_mgr->ids_lru);
684 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800685 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400686 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800687 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400688}
689
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800690static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
691 struct amdgpu_vm *vm,
692 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500693{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800694 struct amdgpu_vm_id_manager *id_mgr;
695 struct amdgpu_vm_id *idle;
696 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500697
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800698 id_mgr = &adev->vm_manager.id_mgr[vmhub];
699 mutex_lock(&id_mgr->lock);
700 if (vm->reserved_vmid[vmhub])
701 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800702 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
703 AMDGPU_VM_MAX_RESERVED_VMID) {
704 DRM_ERROR("Over limitation of reserved vmid\n");
705 atomic_dec(&id_mgr->reserved_vmid_num);
706 r = -EINVAL;
707 goto unlock;
708 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800709 /* Select the first entry VMID */
710 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
711 list_del_init(&idle->list);
712 vm->reserved_vmid[vmhub] = idle;
713 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500714
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800715 return 0;
716unlock:
717 mutex_unlock(&id_mgr->lock);
718 return r;
719}
720
Alex Xiee59c0202017-06-01 09:42:59 -0400721/**
722 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
723 *
724 * @adev: amdgpu_device pointer
725 */
726void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
727{
728 const struct amdgpu_ip_block *ip_block;
729 bool has_compute_vm_bug;
730 struct amdgpu_ring *ring;
731 int i;
732
733 has_compute_vm_bug = false;
734
735 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
736 if (ip_block) {
737 /* Compute has a VM bug for GFX version < 7.
738 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
739 if (ip_block->version->major <= 7)
740 has_compute_vm_bug = true;
741 else if (ip_block->version->major == 8)
742 if (adev->gfx.mec_fw_version < 673)
743 has_compute_vm_bug = true;
744 }
745
746 for (i = 0; i < adev->num_rings; i++) {
747 ring = adev->rings[i];
748 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
749 /* only compute rings */
750 ring->has_compute_vm_bug = has_compute_vm_bug;
751 else
752 ring->has_compute_vm_bug = false;
753 }
754}
755
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400756bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
757 struct amdgpu_job *job)
758{
759 struct amdgpu_device *adev = ring->adev;
760 unsigned vmhub = ring->funcs->vmhub;
761 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
762 struct amdgpu_vm_id *id;
763 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400764 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400765
766 if (job->vm_id == 0)
767 return false;
768 id = &id_mgr->ids[job->vm_id];
769 gds_switch_needed = ring->funcs->emit_gds_switch && (
770 id->gds_base != job->gds_base ||
771 id->gds_size != job->gds_size ||
772 id->gws_base != job->gws_base ||
773 id->gws_size != job->gws_size ||
774 id->oa_base != job->oa_base ||
775 id->oa_size != job->oa_size);
776
777 if (amdgpu_vm_had_gpu_reset(adev, id))
778 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400779
780 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400781}
782
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400783static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
784{
785 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500786}
787
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400788/**
789 * amdgpu_vm_flush - hardware flush the vm
790 *
791 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100792 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100793 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794 *
Christian König4ff37a82016-02-26 16:18:26 +0100795 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800797int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798{
Christian König971fe9a92016-03-01 15:09:25 +0100799 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200800 unsigned vmhub = ring->funcs->vmhub;
801 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
802 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100803 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800804 id->gds_base != job->gds_base ||
805 id->gds_size != job->gds_size ||
806 id->gws_base != job->gws_base ||
807 id->gws_size != job->gws_size ||
808 id->oa_base != job->oa_base ||
809 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800810 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200811 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100812 int r;
Christian Königd564a062016-03-01 15:51:53 +0100813
Christian Königf7d015b2017-04-03 14:28:26 +0200814 if (amdgpu_vm_had_gpu_reset(adev, id)) {
815 gds_switch_needed = true;
816 vm_flush_needed = true;
817 }
Christian König971fe9a92016-03-01 15:09:25 +0100818
Monk Liu8fdf0742017-06-06 17:25:13 +0800819 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200820 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100821
Christian Königc0e51932017-04-03 14:16:07 +0200822 if (ring->funcs->init_cond_exec)
823 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100824
Monk Liu8fdf0742017-06-06 17:25:13 +0800825 if (need_pipe_sync)
826 amdgpu_ring_emit_pipeline_sync(ring);
827
Christian Königf7d015b2017-04-03 14:28:26 +0200828 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200829 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800830
Christian König9a94f5a2017-05-12 14:46:23 +0200831 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
832 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800833
Christian Königc0e51932017-04-03 14:16:07 +0200834 r = amdgpu_fence_emit(ring, &fence);
835 if (r)
836 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800837
Christian König76456702017-04-06 17:52:39 +0200838 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200839 dma_fence_put(id->last_flush);
840 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800841 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200842 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200843 }
Monk Liue9d672b2017-03-15 12:18:57 +0800844
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800845 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200846 id->gds_base = job->gds_base;
847 id->gds_size = job->gds_size;
848 id->gws_base = job->gws_base;
849 id->gws_size = job->gws_size;
850 id->oa_base = job->oa_base;
851 id->oa_size = job->oa_size;
852 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
853 job->gds_size, job->gws_base,
854 job->gws_size, job->oa_base,
855 job->oa_size);
856 }
857
858 if (ring->funcs->patch_cond_exec)
859 amdgpu_ring_patch_cond_exec(ring, patch_offset);
860
861 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
862 if (ring->funcs->emit_switch_buffer) {
863 amdgpu_ring_emit_switch_buffer(ring);
864 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 }
Christian König41d9eb22016-03-01 16:46:18 +0100866 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100867}
868
869/**
870 * amdgpu_vm_reset_id - reset VMID to zero
871 *
872 * @adev: amdgpu device structure
873 * @vm_id: vmid number to use
874 *
875 * Reset saved GDW, GWS and OA to force switch on next flush.
876 */
Christian König76456702017-04-06 17:52:39 +0200877void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
878 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100879{
Christian König76456702017-04-06 17:52:39 +0200880 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
881 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100882
Christian Königb3c85a02017-05-10 20:06:58 +0200883 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100884 id->gds_base = 0;
885 id->gds_size = 0;
886 id->gws_base = 0;
887 id->gws_size = 0;
888 id->oa_base = 0;
889 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400890}
891
892/**
Christian Königb3c85a02017-05-10 20:06:58 +0200893 * amdgpu_vm_reset_all_id - reset VMID to zero
894 *
895 * @adev: amdgpu device structure
896 *
897 * Reset VMID to force flush on next use
898 */
899void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
900{
901 unsigned i, j;
902
903 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
904 struct amdgpu_vm_id_manager *id_mgr =
905 &adev->vm_manager.id_mgr[i];
906
907 for (j = 1; j < id_mgr->num_ids; ++j)
908 amdgpu_vm_reset_id(adev, i, j);
909 }
910}
911
912/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
914 *
915 * @vm: requested vm
916 * @bo: requested buffer object
917 *
Christian König8843dbb2016-01-26 12:17:11 +0100918 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400919 * Search inside the @bos vm list for the requested vm
920 * Returns the found bo_va or NULL if none is found
921 *
922 * Object has to be reserved!
923 */
924struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
925 struct amdgpu_bo *bo)
926{
927 struct amdgpu_bo_va *bo_va;
928
Christian Königec681542017-08-01 10:51:43 +0200929 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
930 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931 return bo_va;
932 }
933 }
934 return NULL;
935}
936
937/**
Christian Königafef8b82016-08-12 13:29:18 +0200938 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939 *
Christian König29efc4f2016-08-04 14:52:50 +0200940 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400941 * @pe: addr of the page entry
942 * @addr: dst addr to write into pe
943 * @count: number of page entries to update
944 * @incr: increase next addr by incr bytes
945 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 *
947 * Traces the parameters and calls the right asic functions
948 * to setup the page table using the DMA.
949 */
Christian Königafef8b82016-08-12 13:29:18 +0200950static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
951 uint64_t pe, uint64_t addr,
952 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800953 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954{
Christian Königec2f05f2016-09-25 16:11:52 +0200955 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956
Christian Königafef8b82016-08-12 13:29:18 +0200957 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200958 amdgpu_vm_write_pte(params->adev, params->ib, pe,
959 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960
961 } else {
Christian König27c5f362016-08-04 15:02:49 +0200962 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963 count, incr, flags);
964 }
965}
966
967/**
Christian Königafef8b82016-08-12 13:29:18 +0200968 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
969 *
970 * @params: see amdgpu_pte_update_params definition
971 * @pe: addr of the page entry
972 * @addr: dst addr to write into pe
973 * @count: number of page entries to update
974 * @incr: increase next addr by incr bytes
975 * @flags: hw access flags
976 *
977 * Traces the parameters and calls the DMA function to copy the PTEs.
978 */
979static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
980 uint64_t pe, uint64_t addr,
981 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800982 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200983{
Christian Königec2f05f2016-09-25 16:11:52 +0200984 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200985
Christian Königec2f05f2016-09-25 16:11:52 +0200986
987 trace_amdgpu_vm_copy_ptes(pe, src, count);
988
989 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200990}
991
992/**
Christian Königb07c9d22015-11-30 13:26:07 +0100993 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400994 *
Christian Königb07c9d22015-11-30 13:26:07 +0100995 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 * @addr: the unmapped addr
997 *
998 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100999 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000 */
Christian Königde9ea7b2016-08-12 11:33:30 +02001001static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001002{
1003 uint64_t result;
1004
Christian Königde9ea7b2016-08-12 11:33:30 +02001005 /* page table offset */
1006 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007
Christian Königde9ea7b2016-08-12 11:33:30 +02001008 /* in case cpu page size != gpu page size*/
1009 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +01001010
1011 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001012
1013 return result;
1014}
1015
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001016/**
1017 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
1018 *
1019 * @params: see amdgpu_pte_update_params definition
1020 * @pe: kmap addr of the page entry
1021 * @addr: dst addr to write into pe
1022 * @count: number of page entries to update
1023 * @incr: increase next addr by incr bytes
1024 * @flags: hw access flags
1025 *
1026 * Write count number of PT/PD entries directly.
1027 */
1028static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
1029 uint64_t pe, uint64_t addr,
1030 unsigned count, uint32_t incr,
1031 uint64_t flags)
1032{
1033 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001034 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001035
Christian König03918b32017-07-11 17:15:37 +02001036 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
1037
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001038 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001039 value = params->pages_addr ?
1040 amdgpu_vm_map_gart(params->pages_addr, addr) :
1041 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001042 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001043 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001044 addr += incr;
1045 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001046}
1047
Christian Königa33cab72017-07-11 17:13:00 +02001048static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1049 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001050{
1051 struct amdgpu_sync sync;
1052 int r;
1053
1054 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -04001055 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001056 r = amdgpu_sync_wait(&sync, true);
1057 amdgpu_sync_free(&sync);
1058
1059 return r;
1060}
1061
Christian Königf8991ba2016-09-16 15:36:49 +02001062/*
Christian König194d2162016-10-12 15:13:52 +02001063 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +02001064 *
1065 * @adev: amdgpu_device pointer
1066 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +02001067 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +02001068 *
Christian König194d2162016-10-12 15:13:52 +02001069 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +02001070 * Returns 0 for success, error for failure.
1071 */
Christian König194d2162016-10-12 15:13:52 +02001072static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1073 struct amdgpu_vm *vm,
Christian Königea097292017-08-09 14:15:46 +02001074 struct amdgpu_vm_pt *parent)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001075{
Christian Königf8991ba2016-09-16 15:36:49 +02001076 struct amdgpu_bo *shadow;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001077 struct amdgpu_ring *ring = NULL;
1078 uint64_t pd_addr, shadow_addr = 0;
Christian Königf8991ba2016-09-16 15:36:49 +02001079 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001080 unsigned count = 0, pt_idx, ndw = 0;
Christian Königd71518b2016-02-01 12:20:25 +01001081 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001082 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +10001083 struct dma_fence *fence = NULL;
Christian Königea097292017-08-09 14:15:46 +02001084 uint32_t incr;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001085
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001086 int r;
1087
Christian König194d2162016-10-12 15:13:52 +02001088 if (!parent->entries)
1089 return 0;
Christian Königd71518b2016-02-01 12:20:25 +01001090
Christian König27c5f362016-08-04 15:02:49 +02001091 memset(&params, 0, sizeof(params));
1092 params.adev = adev;
Christian König3f3333f2017-08-03 14:02:13 +02001093 shadow = parent->base.bo->shadow;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001094
Alex Deucher69277982017-07-13 15:37:11 -04001095 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02001096 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Christian Königa33cab72017-07-11 17:13:00 +02001097 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001098 if (unlikely(r))
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001099 return r;
Christian König0a096fb2017-07-12 10:01:48 +02001100
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001101 params.func = amdgpu_vm_cpu_set_ptes;
1102 } else {
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001103 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1104 sched);
1105
1106 /* padding, etc. */
1107 ndw = 64;
1108
1109 /* assume the worst case */
1110 ndw += parent->last_entry_used * 6;
1111
Christian König3f3333f2017-08-03 14:02:13 +02001112 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001113
1114 if (shadow) {
1115 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1116 ndw *= 2;
1117 } else {
1118 shadow_addr = 0;
1119 }
1120
1121 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1122 if (r)
1123 return r;
1124
1125 params.ib = &job->ibs[0];
1126 params.func = amdgpu_vm_do_set_ptes;
1127 }
1128
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129
Christian König194d2162016-10-12 15:13:52 +02001130 /* walk over the address space and update the directory */
1131 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
Christian Königea097292017-08-09 14:15:46 +02001132 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1133 struct amdgpu_bo *bo = entry->base.bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134 uint64_t pde, pt;
1135
1136 if (bo == NULL)
1137 continue;
1138
Christian Königea097292017-08-09 14:15:46 +02001139 spin_lock(&vm->status_lock);
1140 list_del_init(&entry->base.vm_status);
1141 spin_unlock(&vm->status_lock);
1142
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001143 pt = amdgpu_bo_gpu_offset(bo);
Christian König53e2e912017-05-15 15:19:10 +02001144 pt = amdgpu_gart_get_vm_pde(adev, pt);
Christian König4ab40162017-08-03 20:30:50 +02001145 /* Don't update huge pages here */
1146 if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
1147 parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
Christian Königf8991ba2016-09-16 15:36:49 +02001148 continue;
1149
Christian König4ab40162017-08-03 20:30:50 +02001150 parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151
1152 pde = pd_addr + pt_idx * 8;
Christian Königea097292017-08-09 14:15:46 +02001153 incr = amdgpu_bo_size(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001155 ((last_pt + incr * count) != pt) ||
1156 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001157
1158 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +02001159 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001160 params.func(&params,
1161 last_shadow,
1162 last_pt, count,
1163 incr,
1164 AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001165
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001166 params.func(&params, last_pde,
1167 last_pt, count, incr,
1168 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001169 }
1170
1171 count = 1;
1172 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001173 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 last_pt = pt;
1175 } else {
1176 ++count;
1177 }
1178 }
1179
Christian Königf8991ba2016-09-16 15:36:49 +02001180 if (count) {
Christian König3f3333f2017-08-03 14:02:13 +02001181 if (vm->root.base.bo->shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001182 params.func(&params, last_shadow, last_pt,
1183 count, incr, AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001184
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001185 params.func(&params, last_pde, last_pt,
1186 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001187 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188
Christian König0a096fb2017-07-12 10:01:48 +02001189 if (!vm->use_cpu_for_update) {
1190 if (params.ib->length_dw == 0) {
1191 amdgpu_job_free(job);
1192 } else {
1193 amdgpu_ring_pad_ib(ring, params.ib);
Christian König3f3333f2017-08-03 14:02:13 +02001194 amdgpu_sync_resv(adev, &job->sync,
1195 parent->base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001196 AMDGPU_FENCE_OWNER_VM, false);
Christian König0a096fb2017-07-12 10:01:48 +02001197 if (shadow)
1198 amdgpu_sync_resv(adev, &job->sync,
1199 shadow->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001200 AMDGPU_FENCE_OWNER_VM, false);
Christian Königf8991ba2016-09-16 15:36:49 +02001201
Christian König0a096fb2017-07-12 10:01:48 +02001202 WARN_ON(params.ib->length_dw > ndw);
1203 r = amdgpu_job_submit(job, ring, &vm->entity,
1204 AMDGPU_FENCE_OWNER_VM, &fence);
1205 if (r)
1206 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001207
Christian König3f3333f2017-08-03 14:02:13 +02001208 amdgpu_bo_fence(parent->base.bo, fence, true);
Christian Königd5884512017-09-08 14:09:41 +02001209 dma_fence_put(vm->last_update);
1210 vm->last_update = fence;
Christian König0a096fb2017-07-12 10:01:48 +02001211 }
Christian König194d2162016-10-12 15:13:52 +02001212 }
Christian Königf8991ba2016-09-16 15:36:49 +02001213
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001215
1216error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001217 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001218 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219}
1220
Christian König194d2162016-10-12 15:13:52 +02001221/*
Christian König92456b92017-05-12 16:09:26 +02001222 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1223 *
1224 * @parent: parent PD
1225 *
1226 * Mark all PD level as invalid after an error.
1227 */
Christian Königea097292017-08-09 14:15:46 +02001228static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
1229 struct amdgpu_vm_pt *parent)
Christian König92456b92017-05-12 16:09:26 +02001230{
1231 unsigned pt_idx;
1232
1233 /*
1234 * Recurse into the subdirectories. This recursion is harmless because
1235 * we only have a maximum of 5 layers.
1236 */
1237 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1238 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1239
Christian König3f3333f2017-08-03 14:02:13 +02001240 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001241 continue;
1242
1243 entry->addr = ~0ULL;
Christian Königea097292017-08-09 14:15:46 +02001244 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001245 if (list_empty(&entry->base.vm_status))
1246 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +02001247 spin_unlock(&vm->status_lock);
1248 amdgpu_vm_invalidate_level(vm, entry);
Christian König92456b92017-05-12 16:09:26 +02001249 }
1250}
1251
1252/*
Christian König194d2162016-10-12 15:13:52 +02001253 * amdgpu_vm_update_directories - make sure that all directories are valid
1254 *
1255 * @adev: amdgpu_device pointer
1256 * @vm: requested vm
1257 *
1258 * Makes sure all directories are up to date.
1259 * Returns 0 for success, error for failure.
1260 */
1261int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1262 struct amdgpu_vm *vm)
1263{
Dan Carpenter78aa02c2017-09-30 11:14:13 +03001264 int r = 0;
Christian König92456b92017-05-12 16:09:26 +02001265
Christian Königea097292017-08-09 14:15:46 +02001266 spin_lock(&vm->status_lock);
1267 while (!list_empty(&vm->relocated)) {
1268 struct amdgpu_vm_bo_base *bo_base;
1269 struct amdgpu_bo *bo;
1270
1271 bo_base = list_first_entry(&vm->relocated,
1272 struct amdgpu_vm_bo_base,
1273 vm_status);
1274 spin_unlock(&vm->status_lock);
1275
1276 bo = bo_base->bo->parent;
1277 if (bo) {
1278 struct amdgpu_vm_bo_base *parent;
1279 struct amdgpu_vm_pt *pt;
1280
1281 parent = list_first_entry(&bo->va,
1282 struct amdgpu_vm_bo_base,
1283 bo_list);
1284 pt = container_of(parent, struct amdgpu_vm_pt, base);
1285
1286 r = amdgpu_vm_update_level(adev, vm, pt);
1287 if (r) {
1288 amdgpu_vm_invalidate_level(vm, &vm->root);
1289 return r;
1290 }
1291 spin_lock(&vm->status_lock);
1292 } else {
1293 spin_lock(&vm->status_lock);
1294 list_del_init(&bo_base->vm_status);
1295 }
1296 }
1297 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +02001298
Christian König68c62302017-07-11 17:23:29 +02001299 if (vm->use_cpu_for_update) {
1300 /* Flush HDP */
1301 mb();
1302 amdgpu_gart_flush_gpu_tlb(adev, 0);
1303 }
1304
Christian König92456b92017-05-12 16:09:26 +02001305 return r;
Christian König194d2162016-10-12 15:13:52 +02001306}
1307
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001309 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001310 *
1311 * @p: see amdgpu_pte_update_params definition
1312 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001313 * @entry: resulting entry or NULL
1314 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001315 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001316 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001317 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001318void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1319 struct amdgpu_vm_pt **entry,
1320 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001321{
Christian König50783142017-11-27 14:01:51 +01001322 unsigned level = 0;
Christian König4e2cb642016-10-25 15:52:28 +02001323
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001324 *parent = NULL;
1325 *entry = &p->vm->root;
1326 while ((*entry)->entries) {
Christian König50783142017-11-27 14:01:51 +01001327 unsigned idx = addr >> amdgpu_vm_level_shift(p->adev, level++);
1328
Christian König3f3333f2017-08-03 14:02:13 +02001329 idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001330 *parent = *entry;
1331 *entry = &(*entry)->entries[idx];
Christian König4e2cb642016-10-25 15:52:28 +02001332 }
1333
Christian König50783142017-11-27 14:01:51 +01001334 if (level != p->adev->vm_manager.num_level)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001335 *entry = NULL;
1336}
Christian König4e2cb642016-10-25 15:52:28 +02001337
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001338/**
1339 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1340 *
1341 * @p: see amdgpu_pte_update_params definition
1342 * @entry: vm_pt entry to check
1343 * @parent: parent entry
1344 * @nptes: number of PTEs updated with this operation
1345 * @dst: destination address where the PTEs should point to
1346 * @flags: access flags fro the PTEs
1347 *
1348 * Check if we can update the PD with a huge page.
1349 */
Christian Königec5207c2017-08-03 19:24:06 +02001350static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1351 struct amdgpu_vm_pt *entry,
1352 struct amdgpu_vm_pt *parent,
1353 unsigned nptes, uint64_t dst,
1354 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001355{
1356 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1357 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001358
1359 /* In the case of a mixed PT the PDE must point to it*/
1360 if (p->adev->asic_type < CHIP_VEGA10 ||
1361 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehlingb2529032017-08-17 16:37:49 -04001362 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001363 !(flags & AMDGPU_PTE_VALID)) {
1364
Christian König3f3333f2017-08-03 14:02:13 +02001365 dst = amdgpu_bo_gpu_offset(entry->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001366 dst = amdgpu_gart_get_vm_pde(p->adev, dst);
1367 flags = AMDGPU_PTE_VALID;
1368 } else {
Christian König4ab40162017-08-03 20:30:50 +02001369 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001370 flags |= AMDGPU_PDE_PTE;
1371 }
1372
Christian König4ab40162017-08-03 20:30:50 +02001373 if (entry->addr == (dst | flags))
Christian Königec5207c2017-08-03 19:24:06 +02001374 return;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001375
Christian König4ab40162017-08-03 20:30:50 +02001376 entry->addr = (dst | flags);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001377
1378 if (use_cpu_update) {
Felix Kuehlingb2529032017-08-17 16:37:49 -04001379 /* In case a huge page is replaced with a system
1380 * memory mapping, p->pages_addr != NULL and
1381 * amdgpu_vm_cpu_set_ptes would try to translate dst
1382 * through amdgpu_vm_map_gart. But dst is already a
1383 * GPU address (of the page table). Disable
1384 * amdgpu_vm_map_gart temporarily.
1385 */
1386 dma_addr_t *tmp;
1387
1388 tmp = p->pages_addr;
1389 p->pages_addr = NULL;
1390
Christian König3f3333f2017-08-03 14:02:13 +02001391 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001392 pde = pd_addr + (entry - parent->entries) * 8;
1393 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehlingb2529032017-08-17 16:37:49 -04001394
1395 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001396 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001397 if (parent->base.bo->shadow) {
1398 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001399 pde = pd_addr + (entry - parent->entries) * 8;
1400 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1401 }
Christian König3f3333f2017-08-03 14:02:13 +02001402 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001403 pde = pd_addr + (entry - parent->entries) * 8;
1404 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1405 }
Christian König4e2cb642016-10-25 15:52:28 +02001406}
1407
1408/**
Christian König92696dd2016-08-05 13:56:35 +02001409 * amdgpu_vm_update_ptes - make sure that page tables are valid
1410 *
1411 * @params: see amdgpu_pte_update_params definition
1412 * @vm: requested vm
1413 * @start: start of GPU address range
1414 * @end: end of GPU address range
1415 * @dst: destination address to map to, the next dst inside the function
1416 * @flags: mapping flags
1417 *
1418 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001419 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001420 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001421static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001422 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001423 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001424{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001425 struct amdgpu_device *adev = params->adev;
1426 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001427
Christian König301654a2017-05-16 14:30:27 +02001428 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001429 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001430 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001431 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001432
1433 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001434 for (addr = start; addr < end; addr += nptes,
1435 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1436 struct amdgpu_vm_pt *entry, *parent;
1437
1438 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1439 if (!entry)
1440 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001441
Christian König92696dd2016-08-05 13:56:35 +02001442 if ((addr & ~mask) == (end & ~mask))
1443 nptes = end - addr;
1444 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001445 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001446
Christian Königec5207c2017-08-03 19:24:06 +02001447 amdgpu_vm_handle_huge_pages(params, entry, parent,
1448 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001449 /* We don't need to update PTEs for huge pages */
1450 if (entry->addr & AMDGPU_PDE_PTE)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001451 continue;
1452
Christian König3f3333f2017-08-03 14:02:13 +02001453 pt = entry->base.bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001454 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001455 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001456 } else {
1457 if (pt->shadow) {
1458 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1459 pe_start += (addr & mask) * 8;
1460 params->func(params, pe_start, dst, nptes,
1461 AMDGPU_GPU_PAGE_SIZE, flags);
1462 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001463 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001464 }
Christian König92696dd2016-08-05 13:56:35 +02001465
Christian König301654a2017-05-16 14:30:27 +02001466 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001467 params->func(params, pe_start, dst, nptes,
1468 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001469 }
1470
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001471 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001472}
1473
1474/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001475 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1476 *
Christian König29efc4f2016-08-04 14:52:50 +02001477 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001478 * @vm: requested vm
1479 * @start: first PTE to handle
1480 * @end: last PTE to handle
1481 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001483 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001485static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001486 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001487 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488{
1489 /**
1490 * The MC L1 TLB supports variable sized pages, based on a fragment
1491 * field in the PTE. When this field is set to a non-zero value, page
1492 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1493 * flags are considered valid for all PTEs within the fragment range
1494 * and corresponding mappings are assumed to be physically contiguous.
1495 *
1496 * The L1 TLB can store a single PTE for the whole fragment,
1497 * significantly increasing the space available for translation
1498 * caching. This leads to large improvements in throughput when the
1499 * TLB is under pressure.
1500 *
1501 * The L2 TLB distributes small and large fragments into two
1502 * asymmetric partitions. The large fragment cache is significantly
1503 * larger. Thus, we try to use large fragments wherever possible.
1504 * Userspace can support this by aligning virtual base address and
1505 * allocation size to the fragment size.
1506 */
Roger He6849d472017-08-30 13:01:19 +08001507 unsigned max_frag = params->adev->vm_manager.fragment_size;
1508 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001509
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001510 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001511 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001512 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513
Roger He6849d472017-08-30 13:01:19 +08001514 while (start != end) {
1515 uint64_t frag_flags, frag_end;
1516 unsigned frag;
1517
1518 /* This intentionally wraps around if no bit is set */
1519 frag = min((unsigned)ffs(start) - 1,
1520 (unsigned)fls64(end - start) - 1);
1521 if (frag >= max_frag) {
1522 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1523 frag_end = end & ~((1ULL << max_frag) - 1);
1524 } else {
1525 frag_flags = AMDGPU_PTE_FRAG(frag);
1526 frag_end = start + (1 << frag);
1527 }
1528
1529 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1530 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001531 if (r)
1532 return r;
Roger He6849d472017-08-30 13:01:19 +08001533
1534 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1535 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536 }
1537
Roger He6849d472017-08-30 13:01:19 +08001538 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539}
1540
1541/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1543 *
1544 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001545 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001546 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001547 * @vm: requested vm
1548 * @start: start of mapped range
1549 * @last: last mapped entry
1550 * @flags: flags for the entries
1551 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 * @fence: optional resulting fence
1553 *
Christian Königa14faa62016-01-25 14:27:31 +01001554 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 */
1557static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001558 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001559 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001561 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001562 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001563 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001564{
Christian König2d55e452016-02-08 17:37:38 +01001565 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001566 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001567 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001568 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001569 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001570 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571 int r;
1572
Christian Königafef8b82016-08-12 13:29:18 +02001573 memset(&params, 0, sizeof(params));
1574 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001575 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001576
Christian Königa33cab72017-07-11 17:13:00 +02001577 /* sync to everything on unmapping */
1578 if (!(flags & AMDGPU_PTE_VALID))
1579 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1580
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001581 if (vm->use_cpu_for_update) {
1582 /* params.src is used as flag to indicate system Memory */
1583 if (pages_addr)
1584 params.src = ~0;
1585
1586 /* Wait for PT BOs to be free. PTs share the same resv. object
1587 * as the root PD BO
1588 */
Christian Königa33cab72017-07-11 17:13:00 +02001589 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001590 if (unlikely(r))
1591 return r;
1592
1593 params.func = amdgpu_vm_cpu_set_ptes;
1594 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001595 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1596 addr, flags);
1597 }
1598
Christian König2d55e452016-02-08 17:37:38 +01001599 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001600
Christian Königa14faa62016-01-25 14:27:31 +01001601 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602
1603 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001604 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001605 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001606 *
1607 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608 */
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001609 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001610
1611 /* padding, etc. */
1612 ndw = 64;
1613
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001614 /* one PDE write for each huge page */
1615 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1616
Christian König570144c2017-08-30 15:38:45 +02001617 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001618 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001619 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001620
Christian Königb0456f92016-08-11 14:06:54 +02001621 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001622 ndw += nptes * 2;
1623
Christian Königafef8b82016-08-12 13:29:18 +02001624 params.func = amdgpu_vm_do_copy_ptes;
1625
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001626 } else {
1627 /* set page commands needed */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001628 ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001629
Roger He6849d472017-08-30 13:01:19 +08001630 /* extra commands for begin/end fragments */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001631 ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
1632 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001633
1634 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001635 }
1636
Christian Königd71518b2016-02-01 12:20:25 +01001637 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1638 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001639 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001640
Christian König29efc4f2016-08-04 14:52:50 +02001641 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001642
Christian König570144c2017-08-30 15:38:45 +02001643 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001644 uint64_t *pte;
1645 unsigned i;
1646
1647 /* Put the PTEs at the end of the IB. */
1648 i = ndw - nptes * 2;
1649 pte= (uint64_t *)&(job->ibs->ptr[i]);
1650 params.src = job->ibs->gpu_addr + i * 4;
1651
1652 for (i = 0; i < nptes; ++i) {
1653 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1654 AMDGPU_GPU_PAGE_SIZE);
1655 pte[i] |= flags;
1656 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001657 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001658 }
1659
Christian König3cabaa52016-06-06 10:17:58 +02001660 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1661 if (r)
1662 goto error_free;
1663
Christian König3f3333f2017-08-03 14:02:13 +02001664 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001665 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001666 if (r)
1667 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001668
Christian König3f3333f2017-08-03 14:02:13 +02001669 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001670 if (r)
1671 goto error_free;
1672
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001673 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1674 if (r)
1675 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001676
Christian König29efc4f2016-08-04 14:52:50 +02001677 amdgpu_ring_pad_ib(ring, params.ib);
1678 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001679 r = amdgpu_job_submit(job, ring, &vm->entity,
1680 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001681 if (r)
1682 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683
Christian König3f3333f2017-08-03 14:02:13 +02001684 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001685 dma_fence_put(*fence);
1686 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001687 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001688
1689error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001690 amdgpu_job_free(job);
Christian Königea097292017-08-09 14:15:46 +02001691 amdgpu_vm_invalidate_level(vm, &vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001692 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001693}
1694
1695/**
Christian Königa14faa62016-01-25 14:27:31 +01001696 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1697 *
1698 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001699 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001700 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001701 * @vm: requested vm
1702 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001703 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001704 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001705 * @fence: optional resulting fence
1706 *
1707 * Split the mapping into smaller chunks so that each update fits
1708 * into a SDMA IB.
1709 * Returns 0 for success, -EINVAL for failure.
1710 */
1711static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001712 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001713 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001714 struct amdgpu_vm *vm,
1715 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001716 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001717 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001718 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001719{
Christian König9fc8fc72017-09-18 13:58:30 +02001720 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001721 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001722 int r;
1723
1724 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1725 * but in case of something, we filter the flags in first place
1726 */
1727 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1728 flags &= ~AMDGPU_PTE_READABLE;
1729 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1730 flags &= ~AMDGPU_PTE_WRITEABLE;
1731
Alex Xie15b31c52017-03-03 16:47:11 -05001732 flags &= ~AMDGPU_PTE_EXECUTABLE;
1733 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1734
Alex Xieb0fd18b2017-03-03 16:49:39 -05001735 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1736 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1737
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001738 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1739 (adev->asic_type >= CHIP_VEGA10)) {
1740 flags |= AMDGPU_PTE_PRT;
1741 flags &= ~AMDGPU_PTE_VALID;
1742 }
1743
Christian Königa14faa62016-01-25 14:27:31 +01001744 trace_amdgpu_vm_bo_update(mapping);
1745
Christian König63e0ba42016-08-16 17:38:37 +02001746 pfn = mapping->offset >> PAGE_SHIFT;
1747 if (nodes) {
1748 while (pfn >= nodes->size) {
1749 pfn -= nodes->size;
1750 ++nodes;
1751 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001752 }
Christian Königa14faa62016-01-25 14:27:31 +01001753
Christian König63e0ba42016-08-16 17:38:37 +02001754 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001755 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001756 uint64_t max_entries;
1757 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001758
Christian König63e0ba42016-08-16 17:38:37 +02001759 if (nodes) {
1760 addr = nodes->start << PAGE_SHIFT;
1761 max_entries = (nodes->size - pfn) *
1762 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1763 } else {
1764 addr = 0;
1765 max_entries = S64_MAX;
1766 }
Christian Königa14faa62016-01-25 14:27:31 +01001767
Christian König63e0ba42016-08-16 17:38:37 +02001768 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001769 uint64_t count;
1770
Christian König457e0fe2017-08-22 12:50:46 +02001771 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001772 for (count = 1; count < max_entries; ++count) {
1773 uint64_t idx = pfn + count;
1774
1775 if (pages_addr[idx] !=
1776 (pages_addr[idx - 1] + PAGE_SIZE))
1777 break;
1778 }
1779
1780 if (count < min_linear_pages) {
1781 addr = pfn << PAGE_SHIFT;
1782 dma_addr = pages_addr;
1783 } else {
1784 addr = pages_addr[pfn];
1785 max_entries = count;
1786 }
1787
Christian König63e0ba42016-08-16 17:38:37 +02001788 } else if (flags & AMDGPU_PTE_VALID) {
1789 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001790 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001791 }
Christian König63e0ba42016-08-16 17:38:37 +02001792
Christian Königa9f87f62017-03-30 14:03:59 +02001793 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001794 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001795 start, last, flags, addr,
1796 fence);
1797 if (r)
1798 return r;
1799
Christian König63e0ba42016-08-16 17:38:37 +02001800 pfn += last - start + 1;
1801 if (nodes && nodes->size == pfn) {
1802 pfn = 0;
1803 ++nodes;
1804 }
Christian Königa14faa62016-01-25 14:27:31 +01001805 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001806
Christian Königa9f87f62017-03-30 14:03:59 +02001807 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001808
1809 return 0;
1810}
1811
1812/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1814 *
1815 * @adev: amdgpu_device pointer
1816 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001817 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818 *
1819 * Fill in the page table entries for @bo_va.
1820 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001821 */
1822int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1823 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001824 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001825{
Christian Königec681542017-08-01 10:51:43 +02001826 struct amdgpu_bo *bo = bo_va->base.bo;
1827 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001828 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001829 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001830 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001831 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001832 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001833 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001834 int r;
1835
Christian Königec681542017-08-01 10:51:43 +02001836 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001837 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001838 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001839 exclusive = NULL;
1840 } else {
Christian König8358dce2016-03-30 10:50:25 +02001841 struct ttm_dma_tt *ttm;
1842
Christian Königec681542017-08-01 10:51:43 +02001843 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001844 nodes = mem->mm_node;
1845 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001846 ttm = container_of(bo_va->base.bo->tbo.ttm,
1847 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001848 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001849 }
Christian Königec681542017-08-01 10:51:43 +02001850 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001851 }
1852
Christian König457e0fe2017-08-22 12:50:46 +02001853 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001854 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001855 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001856 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857
Christian König4e55eb32017-09-11 16:54:59 +02001858 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1859 last_update = &vm->last_update;
1860 else
1861 last_update = &bo_va->last_pt_update;
1862
Christian König3d7d4d32017-08-23 16:13:33 +02001863 if (!clear && bo_va->base.moved) {
1864 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001865 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001866
Christian Königcb7b6ec2017-08-15 17:08:12 +02001867 } else if (bo_va->cleared != clear) {
1868 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001869 }
Christian König7fc11952015-07-30 11:53:42 +02001870
1871 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001872 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001873 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001874 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 if (r)
1876 return r;
1877 }
1878
Christian König68c62302017-07-11 17:23:29 +02001879 if (vm->use_cpu_for_update) {
1880 /* Flush HDP */
1881 mb();
1882 amdgpu_gart_flush_gpu_tlb(adev, 0);
1883 }
1884
Christian Königcb7b6ec2017-08-15 17:08:12 +02001885 spin_lock(&vm->status_lock);
1886 list_del_init(&bo_va->base.vm_status);
1887 spin_unlock(&vm->status_lock);
1888
1889 list_splice_init(&bo_va->invalids, &bo_va->valids);
1890 bo_va->cleared = clear;
1891
1892 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1893 list_for_each_entry(mapping, &bo_va->valids, list)
1894 trace_amdgpu_vm_bo_mapping(mapping);
1895 }
1896
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001897 return 0;
1898}
1899
1900/**
Christian König284710f2017-01-30 11:09:31 +01001901 * amdgpu_vm_update_prt_state - update the global PRT state
1902 */
1903static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1904{
1905 unsigned long flags;
1906 bool enable;
1907
1908 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001909 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001910 adev->gart.gart_funcs->set_prt(adev, enable);
1911 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1912}
1913
1914/**
Christian König4388fc22017-03-13 10:13:36 +01001915 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001916 */
1917static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1918{
Christian König4388fc22017-03-13 10:13:36 +01001919 if (!adev->gart.gart_funcs->set_prt)
1920 return;
1921
Christian König451bc8e2017-02-14 16:02:52 +01001922 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1923 amdgpu_vm_update_prt_state(adev);
1924}
1925
1926/**
Christian König0b15f2f2017-02-14 15:47:03 +01001927 * amdgpu_vm_prt_put - drop a PRT user
1928 */
1929static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1930{
Christian König451bc8e2017-02-14 16:02:52 +01001931 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001932 amdgpu_vm_update_prt_state(adev);
1933}
1934
1935/**
Christian König451bc8e2017-02-14 16:02:52 +01001936 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001937 */
1938static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1939{
1940 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1941
Christian König0b15f2f2017-02-14 15:47:03 +01001942 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001943 kfree(cb);
1944}
1945
1946/**
Christian König451bc8e2017-02-14 16:02:52 +01001947 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1948 */
1949static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1950 struct dma_fence *fence)
1951{
Christian König4388fc22017-03-13 10:13:36 +01001952 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001953
Christian König4388fc22017-03-13 10:13:36 +01001954 if (!adev->gart.gart_funcs->set_prt)
1955 return;
1956
1957 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001958 if (!cb) {
1959 /* Last resort when we are OOM */
1960 if (fence)
1961 dma_fence_wait(fence, false);
1962
Dan Carpenter486a68f2017-04-03 21:41:39 +03001963 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001964 } else {
1965 cb->adev = adev;
1966 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1967 amdgpu_vm_prt_cb))
1968 amdgpu_vm_prt_cb(fence, &cb->cb);
1969 }
1970}
1971
1972/**
Christian König284710f2017-01-30 11:09:31 +01001973 * amdgpu_vm_free_mapping - free a mapping
1974 *
1975 * @adev: amdgpu_device pointer
1976 * @vm: requested vm
1977 * @mapping: mapping to be freed
1978 * @fence: fence of the unmap operation
1979 *
1980 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1981 */
1982static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1983 struct amdgpu_vm *vm,
1984 struct amdgpu_bo_va_mapping *mapping,
1985 struct dma_fence *fence)
1986{
Christian König451bc8e2017-02-14 16:02:52 +01001987 if (mapping->flags & AMDGPU_PTE_PRT)
1988 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001989 kfree(mapping);
1990}
1991
1992/**
Christian König451bc8e2017-02-14 16:02:52 +01001993 * amdgpu_vm_prt_fini - finish all prt mappings
1994 *
1995 * @adev: amdgpu_device pointer
1996 * @vm: requested vm
1997 *
1998 * Register a cleanup callback to disable PRT support after VM dies.
1999 */
2000static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2001{
Christian König3f3333f2017-08-03 14:02:13 +02002002 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01002003 struct dma_fence *excl, **shared;
2004 unsigned i, shared_count;
2005 int r;
2006
2007 r = reservation_object_get_fences_rcu(resv, &excl,
2008 &shared_count, &shared);
2009 if (r) {
2010 /* Not enough memory to grab the fence list, as last resort
2011 * block for all the fences to complete.
2012 */
2013 reservation_object_wait_timeout_rcu(resv, true, false,
2014 MAX_SCHEDULE_TIMEOUT);
2015 return;
2016 }
2017
2018 /* Add a callback for each fence in the reservation object */
2019 amdgpu_vm_prt_get(adev);
2020 amdgpu_vm_add_prt_cb(adev, excl);
2021
2022 for (i = 0; i < shared_count; ++i) {
2023 amdgpu_vm_prt_get(adev);
2024 amdgpu_vm_add_prt_cb(adev, shared[i]);
2025 }
2026
2027 kfree(shared);
2028}
2029
2030/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 * amdgpu_vm_clear_freed - clear freed BOs in the PT
2032 *
2033 * @adev: amdgpu_device pointer
2034 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002035 * @fence: optional resulting fence (unchanged if no work needed to be done
2036 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037 *
2038 * Make sure all freed BOs are cleared in the PT.
2039 * Returns 0 for success.
2040 *
2041 * PTs have to be reserved and mutex must be locked!
2042 */
2043int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002044 struct amdgpu_vm *vm,
2045 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046{
2047 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002048 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002050 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002051
2052 while (!list_empty(&vm->freed)) {
2053 mapping = list_first_entry(&vm->freed,
2054 struct amdgpu_bo_va_mapping, list);
2055 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01002056
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002057 if (vm->pte_support_ats)
Yong Zhao6d16dac2017-08-31 15:55:00 -04002058 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002059
Christian König570144c2017-08-30 15:38:45 +02002060 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02002061 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002062 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002063 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01002064 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002065 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002066 return r;
Christian König284710f2017-01-30 11:09:31 +01002067 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002069
2070 if (fence && f) {
2071 dma_fence_put(*fence);
2072 *fence = f;
2073 } else {
2074 dma_fence_put(f);
2075 }
2076
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077 return 0;
2078
2079}
2080
2081/**
Christian König73fb16e2017-08-16 11:13:48 +02002082 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002083 *
2084 * @adev: amdgpu_device pointer
2085 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02002086 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087 *
Christian König73fb16e2017-08-16 11:13:48 +02002088 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002089 * Returns 0 for success.
2090 *
Christian König73fb16e2017-08-16 11:13:48 +02002091 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002092 */
Christian König73fb16e2017-08-16 11:13:48 +02002093int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02002094 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002095{
Christian König73fb16e2017-08-16 11:13:48 +02002096 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02002097 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002098
2099 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002100 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02002101 struct amdgpu_bo_va *bo_va;
2102
Christian König27c7b9a2017-08-01 11:27:36 +02002103 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002104 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002105 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002106
Christian König73fb16e2017-08-16 11:13:48 +02002107 /* Per VM BOs never need to bo cleared in the page tables */
2108 clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
2109
2110 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 if (r)
2112 return r;
2113
2114 spin_lock(&vm->status_lock);
2115 }
2116 spin_unlock(&vm->status_lock);
2117
Christian König91e1a522015-07-06 22:06:40 +02002118 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002119}
2120
2121/**
2122 * amdgpu_vm_bo_add - add a bo to a specific vm
2123 *
2124 * @adev: amdgpu_device pointer
2125 * @vm: requested vm
2126 * @bo: amdgpu buffer object
2127 *
Christian König8843dbb2016-01-26 12:17:11 +01002128 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002129 * Add @bo to the list of bos associated with the vm
2130 * Returns newly added bo_va or NULL for failure
2131 *
2132 * Object has to be reserved!
2133 */
2134struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2135 struct amdgpu_vm *vm,
2136 struct amdgpu_bo *bo)
2137{
2138 struct amdgpu_bo_va *bo_va;
2139
2140 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2141 if (bo_va == NULL) {
2142 return NULL;
2143 }
Christian Königec681542017-08-01 10:51:43 +02002144 bo_va->base.vm = vm;
2145 bo_va->base.bo = bo;
2146 INIT_LIST_HEAD(&bo_va->base.bo_list);
2147 INIT_LIST_HEAD(&bo_va->base.vm_status);
2148
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002149 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002150 INIT_LIST_HEAD(&bo_va->valids);
2151 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002152
Christian Königa5f6b5b2017-01-30 11:01:38 +01002153 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002154 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002155
2156 return bo_va;
2157}
2158
Christian König73fb16e2017-08-16 11:13:48 +02002159
2160/**
2161 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2162 *
2163 * @adev: amdgpu_device pointer
2164 * @bo_va: bo_va to store the address
2165 * @mapping: the mapping to insert
2166 *
2167 * Insert a new mapping into all structures.
2168 */
2169static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2170 struct amdgpu_bo_va *bo_va,
2171 struct amdgpu_bo_va_mapping *mapping)
2172{
2173 struct amdgpu_vm *vm = bo_va->base.vm;
2174 struct amdgpu_bo *bo = bo_va->base.bo;
2175
Christian Königaebc5e62017-09-06 16:55:16 +02002176 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002177 list_add(&mapping->list, &bo_va->invalids);
2178 amdgpu_vm_it_insert(mapping, &vm->va);
2179
2180 if (mapping->flags & AMDGPU_PTE_PRT)
2181 amdgpu_vm_prt_get(adev);
2182
2183 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2184 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002185 if (list_empty(&bo_va->base.vm_status))
2186 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02002187 spin_unlock(&vm->status_lock);
2188 }
2189 trace_amdgpu_vm_bo_map(bo_va, mapping);
2190}
2191
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002192/**
2193 * amdgpu_vm_bo_map - map bo inside a vm
2194 *
2195 * @adev: amdgpu_device pointer
2196 * @bo_va: bo_va to store the address
2197 * @saddr: where to map the BO
2198 * @offset: requested offset in the BO
2199 * @flags: attributes of pages (read/write/valid/etc.)
2200 *
2201 * Add a mapping of the BO at the specefied addr into the VM.
2202 * Returns 0 for success, error for failure.
2203 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002204 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002205 */
2206int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2207 struct amdgpu_bo_va *bo_va,
2208 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002209 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002210{
Christian Königa9f87f62017-03-30 14:03:59 +02002211 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002212 struct amdgpu_bo *bo = bo_va->base.bo;
2213 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002215
Christian König0be52de2015-05-18 14:37:27 +02002216 /* validate the parameters */
2217 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002218 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002219 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002220
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002221 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002222 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002223 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002224 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002225 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002226
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227 saddr /= AMDGPU_GPU_PAGE_SIZE;
2228 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2229
Christian Königa9f87f62017-03-30 14:03:59 +02002230 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2231 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232 /* bo and tmp overlap, invalid addr */
2233 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002234 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002235 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002236 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002237 }
2238
2239 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002240 if (!mapping)
2241 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002242
Christian Königa9f87f62017-03-30 14:03:59 +02002243 mapping->start = saddr;
2244 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002245 mapping->offset = offset;
2246 mapping->flags = flags;
2247
Christian König73fb16e2017-08-16 11:13:48 +02002248 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002249
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002250 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251}
2252
2253/**
Christian König80f95c52017-03-13 10:13:39 +01002254 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2255 *
2256 * @adev: amdgpu_device pointer
2257 * @bo_va: bo_va to store the address
2258 * @saddr: where to map the BO
2259 * @offset: requested offset in the BO
2260 * @flags: attributes of pages (read/write/valid/etc.)
2261 *
2262 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2263 * mappings as we do so.
2264 * Returns 0 for success, error for failure.
2265 *
2266 * Object has to be reserved and unreserved outside!
2267 */
2268int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2269 struct amdgpu_bo_va *bo_va,
2270 uint64_t saddr, uint64_t offset,
2271 uint64_t size, uint64_t flags)
2272{
2273 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002274 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002275 uint64_t eaddr;
2276 int r;
2277
2278 /* validate the parameters */
2279 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2280 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2281 return -EINVAL;
2282
2283 /* make sure object fit at this offset */
2284 eaddr = saddr + size - 1;
2285 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002286 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002287 return -EINVAL;
2288
2289 /* Allocate all the needed memory */
2290 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2291 if (!mapping)
2292 return -ENOMEM;
2293
Christian Königec681542017-08-01 10:51:43 +02002294 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002295 if (r) {
2296 kfree(mapping);
2297 return r;
2298 }
2299
2300 saddr /= AMDGPU_GPU_PAGE_SIZE;
2301 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2302
Christian Königa9f87f62017-03-30 14:03:59 +02002303 mapping->start = saddr;
2304 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002305 mapping->offset = offset;
2306 mapping->flags = flags;
2307
Christian König73fb16e2017-08-16 11:13:48 +02002308 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002309
2310 return 0;
2311}
2312
2313/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2315 *
2316 * @adev: amdgpu_device pointer
2317 * @bo_va: bo_va to remove the address from
2318 * @saddr: where to the BO is mapped
2319 *
2320 * Remove a mapping of the BO at the specefied addr from the VM.
2321 * Returns 0 for success, error for failure.
2322 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002323 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324 */
2325int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2326 struct amdgpu_bo_va *bo_va,
2327 uint64_t saddr)
2328{
2329 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002330 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002331 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002332
Christian König6c7fc502015-06-05 20:56:17 +02002333 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002334
Christian König7fc11952015-07-30 11:53:42 +02002335 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002336 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002337 break;
2338 }
2339
Christian König7fc11952015-07-30 11:53:42 +02002340 if (&mapping->list == &bo_va->valids) {
2341 valid = false;
2342
2343 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002344 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002345 break;
2346 }
2347
Christian König32b41ac2016-03-08 18:03:27 +01002348 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002349 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002350 }
Christian König32b41ac2016-03-08 18:03:27 +01002351
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002352 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002353 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002354 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002355 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002356
Christian Könige17841b2016-03-08 17:52:01 +01002357 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002358 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002359 else
Christian König284710f2017-01-30 11:09:31 +01002360 amdgpu_vm_free_mapping(adev, vm, mapping,
2361 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002362
2363 return 0;
2364}
2365
2366/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002367 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2368 *
2369 * @adev: amdgpu_device pointer
2370 * @vm: VM structure to use
2371 * @saddr: start of the range
2372 * @size: size of the range
2373 *
2374 * Remove all mappings in a range, split them as appropriate.
2375 * Returns 0 for success, error for failure.
2376 */
2377int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2378 struct amdgpu_vm *vm,
2379 uint64_t saddr, uint64_t size)
2380{
2381 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002382 LIST_HEAD(removed);
2383 uint64_t eaddr;
2384
2385 eaddr = saddr + size - 1;
2386 saddr /= AMDGPU_GPU_PAGE_SIZE;
2387 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2388
2389 /* Allocate all the needed memory */
2390 before = kzalloc(sizeof(*before), GFP_KERNEL);
2391 if (!before)
2392 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002393 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002394
2395 after = kzalloc(sizeof(*after), GFP_KERNEL);
2396 if (!after) {
2397 kfree(before);
2398 return -ENOMEM;
2399 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002400 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002401
2402 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002403 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2404 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002405 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002406 if (tmp->start < saddr) {
2407 before->start = tmp->start;
2408 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002409 before->offset = tmp->offset;
2410 before->flags = tmp->flags;
2411 list_add(&before->list, &tmp->list);
2412 }
2413
2414 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002415 if (tmp->last > eaddr) {
2416 after->start = eaddr + 1;
2417 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002418 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002419 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002420 after->flags = tmp->flags;
2421 list_add(&after->list, &tmp->list);
2422 }
2423
2424 list_del(&tmp->list);
2425 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002426
2427 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002428 }
2429
2430 /* And free them up */
2431 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002432 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002433 list_del(&tmp->list);
2434
Christian Königa9f87f62017-03-30 14:03:59 +02002435 if (tmp->start < saddr)
2436 tmp->start = saddr;
2437 if (tmp->last > eaddr)
2438 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002439
Christian Königaebc5e62017-09-06 16:55:16 +02002440 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002441 list_add(&tmp->list, &vm->freed);
2442 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2443 }
2444
Junwei Zhang27f6d612017-03-16 16:09:24 +08002445 /* Insert partial mapping before the range */
2446 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002447 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002448 if (before->flags & AMDGPU_PTE_PRT)
2449 amdgpu_vm_prt_get(adev);
2450 } else {
2451 kfree(before);
2452 }
2453
2454 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002455 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002456 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002457 if (after->flags & AMDGPU_PTE_PRT)
2458 amdgpu_vm_prt_get(adev);
2459 } else {
2460 kfree(after);
2461 }
2462
2463 return 0;
2464}
2465
2466/**
Christian Königaebc5e62017-09-06 16:55:16 +02002467 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2468 *
2469 * @vm: the requested VM
2470 *
2471 * Find a mapping by it's address.
2472 */
2473struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2474 uint64_t addr)
2475{
2476 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2477}
2478
2479/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002480 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2481 *
2482 * @adev: amdgpu_device pointer
2483 * @bo_va: requested bo_va
2484 *
Christian König8843dbb2016-01-26 12:17:11 +01002485 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002486 *
2487 * Object have to be reserved!
2488 */
2489void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2490 struct amdgpu_bo_va *bo_va)
2491{
2492 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002493 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002494
Christian Königec681542017-08-01 10:51:43 +02002495 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002496
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002497 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002498 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499 spin_unlock(&vm->status_lock);
2500
Christian König7fc11952015-07-30 11:53:42 +02002501 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002502 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002503 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002504 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002505 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002506 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002507 }
Christian König7fc11952015-07-30 11:53:42 +02002508 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2509 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002510 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002511 amdgpu_vm_free_mapping(adev, vm, mapping,
2512 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002513 }
Christian König32b41ac2016-03-08 18:03:27 +01002514
Chris Wilsonf54d1862016-10-25 13:00:45 +01002515 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002516 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002517}
2518
2519/**
2520 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2521 *
2522 * @adev: amdgpu_device pointer
2523 * @vm: requested vm
2524 * @bo: amdgpu buffer object
2525 *
Christian König8843dbb2016-01-26 12:17:11 +01002526 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002527 */
2528void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002529 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002530{
Christian Königec681542017-08-01 10:51:43 +02002531 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002532
Christian Königec681542017-08-01 10:51:43 +02002533 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002534 struct amdgpu_vm *vm = bo_base->vm;
2535
Christian König3d7d4d32017-08-23 16:13:33 +02002536 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002537 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2538 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002539 if (bo->tbo.type == ttm_bo_type_kernel)
2540 list_move(&bo_base->vm_status, &vm->evicted);
2541 else
2542 list_move_tail(&bo_base->vm_status,
2543 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002544 spin_unlock(&bo_base->vm->status_lock);
2545 continue;
2546 }
2547
Christian Königea097292017-08-09 14:15:46 +02002548 if (bo->tbo.type == ttm_bo_type_kernel) {
2549 spin_lock(&bo_base->vm->status_lock);
2550 if (list_empty(&bo_base->vm_status))
2551 list_add(&bo_base->vm_status, &vm->relocated);
2552 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002553 continue;
Christian Königea097292017-08-09 14:15:46 +02002554 }
Christian König3f3333f2017-08-03 14:02:13 +02002555
Christian Königec681542017-08-01 10:51:43 +02002556 spin_lock(&bo_base->vm->status_lock);
2557 if (list_empty(&bo_base->vm_status))
Christian König481c2e92017-09-01 14:46:19 +02002558 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002559 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002560 }
2561}
2562
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002563static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2564{
2565 /* Total bits covered by PD + PTs */
2566 unsigned bits = ilog2(vm_size) + 18;
2567
2568 /* Make sure the PD is 4K in size up to 8GB address space.
2569 Above that split equal between PD and PTs */
2570 if (vm_size <= 8)
2571 return (bits - 9);
2572 else
2573 return ((bits + 3) / 2);
2574}
2575
2576/**
Roger Hed07f14b2017-08-15 16:05:59 +08002577 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2578 *
2579 * @adev: amdgpu_device pointer
2580 * @fragment_size_default: the default fragment size if it's set auto
2581 */
Christian Königc38e0692017-09-18 14:01:45 +02002582void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,
2583 uint32_t fragment_size_default)
Roger Hed07f14b2017-08-15 16:05:59 +08002584{
2585 if (amdgpu_vm_fragment_size == -1)
2586 adev->vm_manager.fragment_size = fragment_size_default;
2587 else
2588 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2589}
2590
2591/**
2592 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002593 *
2594 * @adev: amdgpu_device pointer
2595 * @vm_size: the default vm size if it's set auto
2596 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002597void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königc38e0692017-09-18 14:01:45 +02002598 uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002599{
2600 /* adjust vm size firstly */
Christian Königfdd5faa2017-11-04 16:51:44 +01002601 if (amdgpu_vm_size != -1)
2602 vm_size = amdgpu_vm_size;
2603
2604 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002605
2606 /* block size depends on vm size */
2607 if (amdgpu_vm_block_size == -1)
2608 adev->vm_manager.block_size =
Christian Königfdd5faa2017-11-04 16:51:44 +01002609 amdgpu_vm_get_block_size(vm_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002610 else
2611 adev->vm_manager.block_size = amdgpu_vm_block_size;
2612
Roger Hed07f14b2017-08-15 16:05:59 +08002613 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2614
Christian Königfdd5faa2017-11-04 16:51:44 +01002615 DRM_INFO("vm size is %u GB, block size is %u-bit, fragment size is %u-bit\n",
2616 vm_size, adev->vm_manager.block_size,
2617 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002618}
2619
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002620/**
2621 * amdgpu_vm_init - initialize a vm instance
2622 *
2623 * @adev: amdgpu_device pointer
2624 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002625 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002626 *
Christian König8843dbb2016-01-26 12:17:11 +01002627 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002628 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002629int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002630 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002631{
2632 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002633 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002634 unsigned ring_instance;
2635 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002636 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002637 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002638 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002639 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002640
Davidlohr Buesof808c132017-09-08 16:15:08 -07002641 vm->va = RB_ROOT_CACHED;
Chunming Zhou031e2982016-04-25 10:19:13 +08002642 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002643 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2644 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002645 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002646 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002647 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002648 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002649 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002650
Christian König2bd9ccf2016-02-01 12:53:58 +01002651 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002652
2653 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2654 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2655 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002656 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2657 r = amd_sched_entity_init(&ring->sched, &vm->entity,
Monk Liub3eebe32017-10-23 12:23:29 +08002658 rq, amdgpu_sched_jobs, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002659 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002660 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002661
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002662 vm->pte_support_ats = false;
2663
2664 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002665 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2666 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002667
2668 if (adev->asic_type == CHIP_RAVEN) {
2669 vm->pte_support_ats = true;
Yong Zhao6d16dac2017-08-31 15:55:00 -04002670 init_pde_value = AMDGPU_PTE_DEFAULT_ATC
2671 | AMDGPU_PDE_PTE;
2672
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002673 }
2674 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002675 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2676 AMDGPU_VM_USE_CPU_FOR_GFX);
2677 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2678 vm->use_cpu_for_update ? "CPU" : "SDMA");
2679 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2680 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002681 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002682
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002683 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2684 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2685 if (vm->use_cpu_for_update)
2686 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2687 else
2688 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2689 AMDGPU_GEM_CREATE_SHADOW);
2690
Christian Königf566ceb2016-10-27 20:04:38 +02002691 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002692 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002693 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002694 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002695 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002696 goto error_free_sched_entity;
2697
Christian König3f3333f2017-08-03 14:02:13 +02002698 vm->root.base.vm = vm;
2699 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2700 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002701
2702 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002703 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002704 if (r)
2705 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002706
Christian König3f3333f2017-08-03 14:02:13 +02002707 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
Felix Kuehlingca290da2017-08-25 20:15:04 -04002708 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König2bd9ccf2016-02-01 12:53:58 +01002709 if (r)
2710 goto error_free_root;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002711 }
2712
Felix Kuehling02208442017-08-25 20:40:26 -04002713 if (pasid) {
2714 unsigned long flags;
2715
2716 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2717 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2718 GFP_ATOMIC);
2719 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2720 if (r < 0)
2721 goto error_free_root;
2722
2723 vm->pasid = pasid;
2724 }
2725
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002726 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002727 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002728
2729 return 0;
2730
2731error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002732 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2733 amdgpu_bo_unref(&vm->root.base.bo);
2734 vm->root.base.bo = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002735
2736error_free_sched_entity:
2737 amd_sched_entity_fini(&ring->sched, &vm->entity);
2738
2739 return r;
2740}
2741
2742/**
Christian Königf566ceb2016-10-27 20:04:38 +02002743 * amdgpu_vm_free_levels - free PD/PT levels
2744 *
2745 * @level: PD/PT starting level to free
2746 *
2747 * Free the page directory or page table level and all sub levels.
2748 */
2749static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2750{
2751 unsigned i;
2752
Christian König3f3333f2017-08-03 14:02:13 +02002753 if (level->base.bo) {
2754 list_del(&level->base.bo_list);
2755 list_del(&level->base.vm_status);
2756 amdgpu_bo_unref(&level->base.bo->shadow);
2757 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002758 }
2759
2760 if (level->entries)
2761 for (i = 0; i <= level->last_entry_used; i++)
2762 amdgpu_vm_free_levels(&level->entries[i]);
2763
Michal Hocko20981052017-05-17 14:23:12 +02002764 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002765}
2766
2767/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002768 * amdgpu_vm_fini - tear down a vm instance
2769 *
2770 * @adev: amdgpu_device pointer
2771 * @vm: requested vm
2772 *
Christian König8843dbb2016-01-26 12:17:11 +01002773 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002774 * Unbind the VM and remove all bos from the vm bo list
2775 */
2776void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2777{
2778 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002779 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002780 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002781 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002782 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002783
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002784 /* Clear pending page faults from IH when the VM is destroyed */
2785 while (kfifo_get(&vm->faults, &fault))
2786 amdgpu_ih_clear_fault(adev, fault);
2787
Felix Kuehling02208442017-08-25 20:40:26 -04002788 if (vm->pasid) {
2789 unsigned long flags;
2790
2791 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2792 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2793 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2794 }
2795
Christian König2d55e452016-02-08 17:37:38 +01002796 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002797
Davidlohr Buesof808c132017-09-08 16:15:08 -07002798 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002799 dev_err(adev->dev, "still active bo inside vm\n");
2800 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002801 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2802 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002803 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002804 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002805 kfree(mapping);
2806 }
2807 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002808 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002809 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002810 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002811 }
Christian König284710f2017-01-30 11:09:31 +01002812
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002813 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002814 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002815 }
2816
Christian König2642cf12017-10-13 17:24:31 +02002817 root = amdgpu_bo_ref(vm->root.base.bo);
2818 r = amdgpu_bo_reserve(root, true);
2819 if (r) {
2820 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2821 } else {
2822 amdgpu_vm_free_levels(&vm->root);
2823 amdgpu_bo_unreserve(root);
2824 }
2825 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002826 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002827 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2828 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002829}
Christian Königea89f8c2015-11-15 20:52:06 +01002830
2831/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002832 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2833 *
2834 * @adev: amdgpu_device pointer
2835 * @pasid: PASID do identify the VM
2836 *
2837 * This function is expected to be called in interrupt context. Returns
2838 * true if there was fault credit, false otherwise
2839 */
2840bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2841 unsigned int pasid)
2842{
2843 struct amdgpu_vm *vm;
2844
2845 spin_lock(&adev->vm_manager.pasid_lock);
2846 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2847 spin_unlock(&adev->vm_manager.pasid_lock);
2848 if (!vm)
2849 /* VM not found, can't track fault credit */
2850 return true;
2851
2852 /* No lock needed. only accessed by IRQ handler */
2853 if (!vm->fault_credit)
2854 /* Too many faults in this VM */
2855 return false;
2856
2857 vm->fault_credit--;
2858 return true;
2859}
2860
2861/**
Christian Königa9a78b32016-01-21 10:19:11 +01002862 * amdgpu_vm_manager_init - init the VM manager
2863 *
2864 * @adev: amdgpu_device pointer
2865 *
2866 * Initialize the VM manager structures
2867 */
2868void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2869{
Christian König76456702017-04-06 17:52:39 +02002870 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002871
Christian König76456702017-04-06 17:52:39 +02002872 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2873 struct amdgpu_vm_id_manager *id_mgr =
2874 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002875
Christian König76456702017-04-06 17:52:39 +02002876 mutex_init(&id_mgr->lock);
2877 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002878 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002879
2880 /* skip over VMID 0, since it is the system VM */
2881 for (j = 1; j < id_mgr->num_ids; ++j) {
2882 amdgpu_vm_reset_id(adev, i, j);
2883 amdgpu_sync_create(&id_mgr->ids[i].active);
2884 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2885 }
Christian König971fe9a92016-03-01 15:09:25 +01002886 }
Christian König2d55e452016-02-08 17:37:38 +01002887
Chris Wilsonf54d1862016-10-25 13:00:45 +01002888 adev->vm_manager.fence_context =
2889 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002890 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2891 adev->vm_manager.seqno[i] = 0;
2892
Christian König2d55e452016-02-08 17:37:38 +01002893 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002894 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002895 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002896 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002897
2898 /* If not overridden by the user, by default, only in large BAR systems
2899 * Compute VM tables will be updated by CPU
2900 */
2901#ifdef CONFIG_X86_64
2902 if (amdgpu_vm_update_mode == -1) {
2903 if (amdgpu_vm_is_large_bar(adev))
2904 adev->vm_manager.vm_update_mode =
2905 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2906 else
2907 adev->vm_manager.vm_update_mode = 0;
2908 } else
2909 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2910#else
2911 adev->vm_manager.vm_update_mode = 0;
2912#endif
2913
Felix Kuehling02208442017-08-25 20:40:26 -04002914 idr_init(&adev->vm_manager.pasid_idr);
2915 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002916}
2917
2918/**
Christian Königea89f8c2015-11-15 20:52:06 +01002919 * amdgpu_vm_manager_fini - cleanup VM manager
2920 *
2921 * @adev: amdgpu_device pointer
2922 *
2923 * Cleanup the VM manager and free resources.
2924 */
2925void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2926{
Christian König76456702017-04-06 17:52:39 +02002927 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002928
Felix Kuehling02208442017-08-25 20:40:26 -04002929 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2930 idr_destroy(&adev->vm_manager.pasid_idr);
2931
Christian König76456702017-04-06 17:52:39 +02002932 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2933 struct amdgpu_vm_id_manager *id_mgr =
2934 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002935
Christian König76456702017-04-06 17:52:39 +02002936 mutex_destroy(&id_mgr->lock);
2937 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2938 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2939
2940 amdgpu_sync_free(&id->active);
2941 dma_fence_put(id->flushed_updates);
2942 dma_fence_put(id->last_flush);
2943 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002944 }
Christian Königea89f8c2015-11-15 20:52:06 +01002945}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002946
2947int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2948{
2949 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002950 struct amdgpu_device *adev = dev->dev_private;
2951 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2952 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002953
2954 switch (args->in.op) {
2955 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002956 /* current, we only have requirement to reserve vmid from gfxhub */
2957 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2958 AMDGPU_GFXHUB);
2959 if (r)
2960 return r;
2961 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002962 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002963 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002964 break;
2965 default:
2966 return -EINVAL;
2967 }
2968
2969 return 0;
2970}