blob: c559d76ff695f7f5940c0f3a79da0879611036ba [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önig72a7ec52016-10-19 11:03:57 +0200142 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143 *
144 * @adev: amdgpu_device pointer
145 *
Christian König72a7ec52016-10-19 11:03:57 +0200146 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147 */
Christian König72a7ec52016-10-19 11:03:57 +0200148static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
149 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150{
Christian König72a7ec52016-10-19 11:03:57 +0200151 if (level == 0)
152 /* For the root directory */
153 return adev->vm_manager.max_pfn >>
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800154 (adev->vm_manager.block_size *
155 adev->vm_manager.num_level);
Christian König72a7ec52016-10-19 11:03:57 +0200156 else if (level == adev->vm_manager.num_level)
157 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800158 return AMDGPU_VM_PTE_COUNT(adev);
Christian König72a7ec52016-10-19 11:03:57 +0200159 else
160 /* Everything in between */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800161 return 1 << adev->vm_manager.block_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162}
163
164/**
Christian König72a7ec52016-10-19 11:03:57 +0200165 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166 *
167 * @adev: amdgpu_device pointer
168 *
Christian König72a7ec52016-10-19 11:03:57 +0200169 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170 */
Christian König72a7ec52016-10-19 11:03:57 +0200171static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172{
Christian König72a7ec52016-10-19 11:03:57 +0200173 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174}
175
176/**
Christian König56467eb2015-12-11 15:16:32 +0100177 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178 *
179 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100180 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100181 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182 *
183 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100184 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 */
Christian König56467eb2015-12-11 15:16:32 +0100186void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
187 struct list_head *validated,
188 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400189{
Christian König3f3333f2017-08-03 14:02:13 +0200190 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100191 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200192 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100193 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100194 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100195 list_add(&entry->tv.head, validated);
196}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197
Christian König56467eb2015-12-11 15:16:32 +0100198/**
Christian Königf7da30d2016-09-28 12:03:04 +0200199 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100200 *
Christian König5a712a82016-06-21 16:28:15 +0200201 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100202 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200203 * @validate: callback to do the validation
204 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400205 *
Christian Königf7da30d2016-09-28 12:03:04 +0200206 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207 */
Christian Königf7da30d2016-09-28 12:03:04 +0200208int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
209 int (*validate)(void *p, struct amdgpu_bo *bo),
210 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211{
Christian König3f3333f2017-08-03 14:02:13 +0200212 struct ttm_bo_global *glob = adev->mman.bdev.glob;
213 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214
Christian König3f3333f2017-08-03 14:02:13 +0200215 spin_lock(&vm->status_lock);
216 while (!list_empty(&vm->evicted)) {
217 struct amdgpu_vm_bo_base *bo_base;
218 struct amdgpu_bo *bo;
Christian König5a712a82016-06-21 16:28:15 +0200219
Christian König3f3333f2017-08-03 14:02:13 +0200220 bo_base = list_first_entry(&vm->evicted,
221 struct amdgpu_vm_bo_base,
222 vm_status);
223 spin_unlock(&vm->status_lock);
Christian Königeceb8a12016-01-11 15:35:21 +0100224
Christian König3f3333f2017-08-03 14:02:13 +0200225 bo = bo_base->bo;
226 BUG_ON(!bo);
227 if (bo->parent) {
228 r = validate(param, bo);
229 if (r)
230 return r;
Christian König34d7be52017-08-24 12:32:55 +0200231
Christian König3f3333f2017-08-03 14:02:13 +0200232 spin_lock(&glob->lru_lock);
233 ttm_bo_move_to_lru_tail(&bo->tbo);
234 if (bo->shadow)
235 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
236 spin_unlock(&glob->lru_lock);
237 }
238
Christian König73fb16e2017-08-16 11:13:48 +0200239 if (bo->tbo.type == ttm_bo_type_kernel &&
240 vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +0200241 r = amdgpu_bo_kmap(bo, NULL);
242 if (r)
243 return r;
244 }
245
246 spin_lock(&vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200247 if (bo->tbo.type != ttm_bo_type_kernel)
248 list_move(&bo_base->vm_status, &vm->moved);
249 else
250 list_move(&bo_base->vm_status, &vm->relocated);
Christian König3f3333f2017-08-03 14:02:13 +0200251 }
252 spin_unlock(&vm->status_lock);
Christian König34d7be52017-08-24 12:32:55 +0200253
254 return 0;
255}
256
257/**
258 * amdgpu_vm_ready - check VM is ready for updates
259 *
Christian König34d7be52017-08-24 12:32:55 +0200260 * @vm: VM to check
261 *
262 * Check if all VM PDs/PTs are ready for updates
263 */
Christian König3f3333f2017-08-03 14:02:13 +0200264bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200265{
Christian König3f3333f2017-08-03 14:02:13 +0200266 bool ready;
Christian König34d7be52017-08-24 12:32:55 +0200267
Christian König3f3333f2017-08-03 14:02:13 +0200268 spin_lock(&vm->status_lock);
269 ready = list_empty(&vm->evicted);
270 spin_unlock(&vm->status_lock);
271
272 return ready;
Christian Königeceb8a12016-01-11 15:35:21 +0100273}
274
275/**
Christian Königf566ceb2016-10-27 20:04:38 +0200276 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
277 *
278 * @adev: amdgpu_device pointer
279 * @vm: requested vm
280 * @saddr: start of the address range
281 * @eaddr: end of the address range
282 *
283 * Make sure the page directories and page tables are allocated
284 */
285static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
286 struct amdgpu_vm *vm,
287 struct amdgpu_vm_pt *parent,
288 uint64_t saddr, uint64_t eaddr,
289 unsigned level)
290{
291 unsigned shift = (adev->vm_manager.num_level - level) *
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800292 adev->vm_manager.block_size;
Christian Königf566ceb2016-10-27 20:04:38 +0200293 unsigned pt_idx, from, to;
294 int r;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400295 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400296 uint64_t init_value = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200297
298 if (!parent->entries) {
299 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
300
Michal Hocko20981052017-05-17 14:23:12 +0200301 parent->entries = kvmalloc_array(num_entries,
302 sizeof(struct amdgpu_vm_pt),
303 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200304 if (!parent->entries)
305 return -ENOMEM;
306 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
307 }
308
Felix Kuehling1866bac2017-03-28 20:36:12 -0400309 from = saddr >> shift;
310 to = eaddr >> shift;
311 if (from >= amdgpu_vm_num_entries(adev, level) ||
312 to >= amdgpu_vm_num_entries(adev, level))
313 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200314
315 if (to > parent->last_entry_used)
316 parent->last_entry_used = to;
317
318 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400319 saddr = saddr & ((1 << shift) - 1);
320 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200321
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400322 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
323 AMDGPU_GEM_CREATE_VRAM_CLEARED;
324 if (vm->use_cpu_for_update)
325 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
326 else
327 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
328 AMDGPU_GEM_CREATE_SHADOW);
329
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400330 if (vm->pte_support_ats) {
Yong Zhao6d16dac2017-08-31 15:55:00 -0400331 init_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400332 if (level != adev->vm_manager.num_level - 1)
333 init_value |= AMDGPU_PDE_PTE;
Yong Zhao6d16dac2017-08-31 15:55:00 -0400334
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400335 }
336
Christian Königf566ceb2016-10-27 20:04:38 +0200337 /* walk over the address space and allocate the page tables */
338 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200339 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200340 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
341 struct amdgpu_bo *pt;
342
Christian König3f3333f2017-08-03 14:02:13 +0200343 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200344 r = amdgpu_bo_create(adev,
345 amdgpu_vm_bo_size(adev, level),
346 AMDGPU_GPU_PAGE_SIZE, true,
347 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400348 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400349 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200350 if (r)
351 return r;
352
Christian König0a096fb2017-07-12 10:01:48 +0200353 if (vm->use_cpu_for_update) {
354 r = amdgpu_bo_kmap(pt, NULL);
355 if (r) {
356 amdgpu_bo_unref(&pt);
357 return r;
358 }
359 }
360
Christian Königf566ceb2016-10-27 20:04:38 +0200361 /* Keep a reference to the root directory to avoid
362 * freeing them up in the wrong order.
363 */
Christian König0f2fc432017-08-31 10:46:20 +0200364 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200365
Christian König3f3333f2017-08-03 14:02:13 +0200366 entry->base.vm = vm;
367 entry->base.bo = pt;
368 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200369 spin_lock(&vm->status_lock);
370 list_add(&entry->base.vm_status, &vm->relocated);
371 spin_unlock(&vm->status_lock);
Christian Königf566ceb2016-10-27 20:04:38 +0200372 entry->addr = 0;
373 }
374
375 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400376 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
377 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
378 ((1 << shift) - 1);
379 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
380 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200381 if (r)
382 return r;
383 }
384 }
385
386 return 0;
387}
388
Christian König663e4572017-03-13 10:13:37 +0100389/**
390 * amdgpu_vm_alloc_pts - Allocate page tables.
391 *
392 * @adev: amdgpu_device pointer
393 * @vm: VM to allocate page tables for
394 * @saddr: Start address which needs to be allocated
395 * @size: Size from start address we need.
396 *
397 * Make sure the page tables are allocated.
398 */
399int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
400 struct amdgpu_vm *vm,
401 uint64_t saddr, uint64_t size)
402{
Felix Kuehling22770e52017-03-28 20:24:53 -0400403 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100404 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100405
406 /* validate the parameters */
407 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
408 return -EINVAL;
409
410 eaddr = saddr + size - 1;
411 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
412 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400413 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100414 last_pfn, adev->vm_manager.max_pfn);
415 return -EINVAL;
416 }
417
418 saddr /= AMDGPU_GPU_PAGE_SIZE;
419 eaddr /= AMDGPU_GPU_PAGE_SIZE;
420
Christian Königf566ceb2016-10-27 20:04:38 +0200421 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100422}
423
Christian König641e9402017-04-03 13:59:25 +0200424/**
425 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
426 *
427 * @adev: amdgpu_device pointer
428 * @id: VMID structure
429 *
430 * Check if GPU reset occured since last use of the VMID.
431 */
432static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
433 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800434{
435 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200436 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800437}
438
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800439static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
440{
441 return !!vm->reserved_vmid[vmhub];
442}
443
444/* idr_mgr->lock must be held */
445static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
446 struct amdgpu_ring *ring,
447 struct amdgpu_sync *sync,
448 struct dma_fence *fence,
449 struct amdgpu_job *job)
450{
451 struct amdgpu_device *adev = ring->adev;
452 unsigned vmhub = ring->funcs->vmhub;
453 uint64_t fence_context = adev->fence_context + ring->idx;
454 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
455 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
456 struct dma_fence *updates = sync->last_vm_update;
457 int r = 0;
458 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200459 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800460
461 flushed = id->flushed_updates;
462 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
463 (atomic64_read(&id->owner) != vm->client_id) ||
464 (job->vm_pd_addr != id->pd_gpu_addr) ||
465 (updates && (!flushed || updates->context != flushed->context ||
466 dma_fence_is_later(updates, flushed))) ||
467 (!id->last_flush || (id->last_flush->context != fence_context &&
468 !dma_fence_is_signaled(id->last_flush)))) {
469 needs_flush = true;
470 /* to prevent one context starved by another context */
471 id->pd_gpu_addr = 0;
472 tmp = amdgpu_sync_peek_fence(&id->active, ring);
473 if (tmp) {
474 r = amdgpu_sync_fence(adev, sync, tmp);
475 return r;
476 }
477 }
478
479 /* Good we can use this VMID. Remember this submission as
480 * user of the VMID.
481 */
482 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
483 if (r)
484 goto out;
485
486 if (updates && (!flushed || updates->context != flushed->context ||
487 dma_fence_is_later(updates, flushed))) {
488 dma_fence_put(id->flushed_updates);
489 id->flushed_updates = dma_fence_get(updates);
490 }
491 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800492 atomic64_set(&id->owner, vm->client_id);
493 job->vm_needs_flush = needs_flush;
494 if (needs_flush) {
495 dma_fence_put(id->last_flush);
496 id->last_flush = NULL;
497 }
498 job->vm_id = id - id_mgr->ids;
499 trace_amdgpu_vm_grab_id(vm, ring, job);
500out:
501 return r;
502}
503
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504/**
505 * amdgpu_vm_grab_id - allocate the next free VMID
506 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200508 * @ring: ring we want to submit job to
509 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100510 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 *
Christian König7f8a5292015-07-20 16:09:40 +0200512 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 */
Christian König7f8a5292015-07-20 16:09:40 +0200514int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100515 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800516 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200519 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200520 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200521 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100522 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200523 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100524 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200525 unsigned i;
526 int r = 0;
527
Christian König76456702017-04-06 17:52:39 +0200528 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800529 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
530 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
531 mutex_unlock(&id_mgr->lock);
532 return r;
533 }
534 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
535 if (!fences) {
536 mutex_unlock(&id_mgr->lock);
537 return -ENOMEM;
538 }
Christian König36fd7c52016-05-23 15:30:08 +0200539 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200540 i = 0;
Christian König76456702017-04-06 17:52:39 +0200541 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200542 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
543 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200544 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200545 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200546 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100547
Christian König1fbb2e92016-06-01 10:47:36 +0200548 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200549 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200550 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
551 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100552 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200553 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200554
Christian König1fbb2e92016-06-01 10:47:36 +0200555 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100556 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200557
Chris Wilsonf54d1862016-10-25 13:00:45 +0100558 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200559 seqno, true);
560 if (!array) {
561 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100562 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200563 kfree(fences);
564 r = -ENOMEM;
565 goto error;
566 }
Christian König8d76001e2016-05-23 16:00:32 +0200567
Christian König8d76001e2016-05-23 16:00:32 +0200568
Christian König1fbb2e92016-06-01 10:47:36 +0200569 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100570 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200571 if (r)
572 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200573
Christian König76456702017-04-06 17:52:39 +0200574 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200575 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200576
Christian König1fbb2e92016-06-01 10:47:36 +0200577 }
578 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200579
Christian König6f1ceab2017-07-11 16:59:21 +0200580 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200581 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200582 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100583 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200584 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200585
Christian König1fbb2e92016-06-01 10:47:36 +0200586 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200587 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800588 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200589
590 if (atomic64_read(&id->owner) != vm->client_id)
591 continue;
592
Chunming Zhoufd53be32016-07-01 17:59:01 +0800593 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200594 continue;
595
Christian König87c910d2017-03-30 16:56:20 +0200596 if (!id->last_flush ||
597 (id->last_flush->context != fence_context &&
598 !dma_fence_is_signaled(id->last_flush)))
599 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200600
601 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200602 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
603 needs_flush = true;
604
605 /* Concurrent flushes are only possible starting with Vega10 */
606 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200607 continue;
608
Christian König3dab83b2016-06-01 13:31:17 +0200609 /* Good we can use this VMID. Remember this submission as
610 * user of the VMID.
611 */
Christian König1fbb2e92016-06-01 10:47:36 +0200612 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
613 if (r)
614 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200615
Christian König87c910d2017-03-30 16:56:20 +0200616 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
617 dma_fence_put(id->flushed_updates);
618 id->flushed_updates = dma_fence_get(updates);
619 }
Christian König8d76001e2016-05-23 16:00:32 +0200620
Christian König87c910d2017-03-30 16:56:20 +0200621 if (needs_flush)
622 goto needs_flush;
623 else
624 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200625
Christian König4f618e72017-04-06 15:18:21 +0200626 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800627
Christian König1fbb2e92016-06-01 10:47:36 +0200628 /* Still no ID to use? Then use the idle one found earlier */
629 id = idle;
630
631 /* Remember this submission as user of the VMID */
632 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100633 if (r)
634 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100635
Christian König87c910d2017-03-30 16:56:20 +0200636 id->pd_gpu_addr = job->vm_pd_addr;
637 dma_fence_put(id->flushed_updates);
638 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200639 atomic64_set(&id->owner, vm->client_id);
640
641needs_flush:
642 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100643 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100644 id->last_flush = NULL;
645
Christian König87c910d2017-03-30 16:56:20 +0200646no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200647 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648
Christian König76456702017-04-06 17:52:39 +0200649 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200650 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100651
652error:
Christian König76456702017-04-06 17:52:39 +0200653 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100654 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655}
656
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800657static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
658 struct amdgpu_vm *vm,
659 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400660{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800661 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400662
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800663 mutex_lock(&id_mgr->lock);
664 if (vm->reserved_vmid[vmhub]) {
665 list_add(&vm->reserved_vmid[vmhub]->list,
666 &id_mgr->ids_lru);
667 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800668 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400669 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800670 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400671}
672
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800673static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
674 struct amdgpu_vm *vm,
675 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500676{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800677 struct amdgpu_vm_id_manager *id_mgr;
678 struct amdgpu_vm_id *idle;
679 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500680
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800681 id_mgr = &adev->vm_manager.id_mgr[vmhub];
682 mutex_lock(&id_mgr->lock);
683 if (vm->reserved_vmid[vmhub])
684 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800685 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
686 AMDGPU_VM_MAX_RESERVED_VMID) {
687 DRM_ERROR("Over limitation of reserved vmid\n");
688 atomic_dec(&id_mgr->reserved_vmid_num);
689 r = -EINVAL;
690 goto unlock;
691 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800692 /* Select the first entry VMID */
693 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
694 list_del_init(&idle->list);
695 vm->reserved_vmid[vmhub] = idle;
696 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500697
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800698 return 0;
699unlock:
700 mutex_unlock(&id_mgr->lock);
701 return r;
702}
703
Alex Xiee59c0202017-06-01 09:42:59 -0400704/**
705 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
706 *
707 * @adev: amdgpu_device pointer
708 */
709void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
710{
711 const struct amdgpu_ip_block *ip_block;
712 bool has_compute_vm_bug;
713 struct amdgpu_ring *ring;
714 int i;
715
716 has_compute_vm_bug = false;
717
718 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
719 if (ip_block) {
720 /* Compute has a VM bug for GFX version < 7.
721 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
722 if (ip_block->version->major <= 7)
723 has_compute_vm_bug = true;
724 else if (ip_block->version->major == 8)
725 if (adev->gfx.mec_fw_version < 673)
726 has_compute_vm_bug = true;
727 }
728
729 for (i = 0; i < adev->num_rings; i++) {
730 ring = adev->rings[i];
731 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
732 /* only compute rings */
733 ring->has_compute_vm_bug = has_compute_vm_bug;
734 else
735 ring->has_compute_vm_bug = false;
736 }
737}
738
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400739bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
740 struct amdgpu_job *job)
741{
742 struct amdgpu_device *adev = ring->adev;
743 unsigned vmhub = ring->funcs->vmhub;
744 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
745 struct amdgpu_vm_id *id;
746 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400747 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400748
749 if (job->vm_id == 0)
750 return false;
751 id = &id_mgr->ids[job->vm_id];
752 gds_switch_needed = ring->funcs->emit_gds_switch && (
753 id->gds_base != job->gds_base ||
754 id->gds_size != job->gds_size ||
755 id->gws_base != job->gws_base ||
756 id->gws_size != job->gws_size ||
757 id->oa_base != job->oa_base ||
758 id->oa_size != job->oa_size);
759
760 if (amdgpu_vm_had_gpu_reset(adev, id))
761 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400762
763 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400764}
765
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400766static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
767{
768 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500769}
770
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400771/**
772 * amdgpu_vm_flush - hardware flush the vm
773 *
774 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100775 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100776 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400777 *
Christian König4ff37a82016-02-26 16:18:26 +0100778 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800780int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400781{
Christian König971fe9a92016-03-01 15:09:25 +0100782 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200783 unsigned vmhub = ring->funcs->vmhub;
784 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
785 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100786 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800787 id->gds_base != job->gds_base ||
788 id->gds_size != job->gds_size ||
789 id->gws_base != job->gws_base ||
790 id->gws_size != job->gws_size ||
791 id->oa_base != job->oa_base ||
792 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800793 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200794 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100795 int r;
Christian Königd564a062016-03-01 15:51:53 +0100796
Christian Königf7d015b2017-04-03 14:28:26 +0200797 if (amdgpu_vm_had_gpu_reset(adev, id)) {
798 gds_switch_needed = true;
799 vm_flush_needed = true;
800 }
Christian König971fe9a92016-03-01 15:09:25 +0100801
Monk Liu8fdf0742017-06-06 17:25:13 +0800802 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200803 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100804
Christian Königc0e51932017-04-03 14:16:07 +0200805 if (ring->funcs->init_cond_exec)
806 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100807
Monk Liu8fdf0742017-06-06 17:25:13 +0800808 if (need_pipe_sync)
809 amdgpu_ring_emit_pipeline_sync(ring);
810
Christian Königf7d015b2017-04-03 14:28:26 +0200811 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200812 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800813
Christian König9a94f5a2017-05-12 14:46:23 +0200814 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
815 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800816
Christian Königc0e51932017-04-03 14:16:07 +0200817 r = amdgpu_fence_emit(ring, &fence);
818 if (r)
819 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800820
Christian König76456702017-04-06 17:52:39 +0200821 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200822 dma_fence_put(id->last_flush);
823 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800824 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200825 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200826 }
Monk Liue9d672b2017-03-15 12:18:57 +0800827
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800828 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200829 id->gds_base = job->gds_base;
830 id->gds_size = job->gds_size;
831 id->gws_base = job->gws_base;
832 id->gws_size = job->gws_size;
833 id->oa_base = job->oa_base;
834 id->oa_size = job->oa_size;
835 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
836 job->gds_size, job->gws_base,
837 job->gws_size, job->oa_base,
838 job->oa_size);
839 }
840
841 if (ring->funcs->patch_cond_exec)
842 amdgpu_ring_patch_cond_exec(ring, patch_offset);
843
844 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
845 if (ring->funcs->emit_switch_buffer) {
846 amdgpu_ring_emit_switch_buffer(ring);
847 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 }
Christian König41d9eb22016-03-01 16:46:18 +0100849 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100850}
851
852/**
853 * amdgpu_vm_reset_id - reset VMID to zero
854 *
855 * @adev: amdgpu device structure
856 * @vm_id: vmid number to use
857 *
858 * Reset saved GDW, GWS and OA to force switch on next flush.
859 */
Christian König76456702017-04-06 17:52:39 +0200860void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
861 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100862{
Christian König76456702017-04-06 17:52:39 +0200863 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
864 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100865
Christian Königb3c85a02017-05-10 20:06:58 +0200866 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100867 id->gds_base = 0;
868 id->gds_size = 0;
869 id->gws_base = 0;
870 id->gws_size = 0;
871 id->oa_base = 0;
872 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873}
874
875/**
Christian Königb3c85a02017-05-10 20:06:58 +0200876 * amdgpu_vm_reset_all_id - reset VMID to zero
877 *
878 * @adev: amdgpu device structure
879 *
880 * Reset VMID to force flush on next use
881 */
882void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
883{
884 unsigned i, j;
885
886 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
887 struct amdgpu_vm_id_manager *id_mgr =
888 &adev->vm_manager.id_mgr[i];
889
890 for (j = 1; j < id_mgr->num_ids; ++j)
891 amdgpu_vm_reset_id(adev, i, j);
892 }
893}
894
895/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400896 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
897 *
898 * @vm: requested vm
899 * @bo: requested buffer object
900 *
Christian König8843dbb2016-01-26 12:17:11 +0100901 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400902 * Search inside the @bos vm list for the requested vm
903 * Returns the found bo_va or NULL if none is found
904 *
905 * Object has to be reserved!
906 */
907struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
908 struct amdgpu_bo *bo)
909{
910 struct amdgpu_bo_va *bo_va;
911
Christian Königec681542017-08-01 10:51:43 +0200912 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
913 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400914 return bo_va;
915 }
916 }
917 return NULL;
918}
919
920/**
Christian Königafef8b82016-08-12 13:29:18 +0200921 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922 *
Christian König29efc4f2016-08-04 14:52:50 +0200923 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400924 * @pe: addr of the page entry
925 * @addr: dst addr to write into pe
926 * @count: number of page entries to update
927 * @incr: increase next addr by incr bytes
928 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 *
930 * Traces the parameters and calls the right asic functions
931 * to setup the page table using the DMA.
932 */
Christian Königafef8b82016-08-12 13:29:18 +0200933static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
934 uint64_t pe, uint64_t addr,
935 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800936 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937{
Christian Königec2f05f2016-09-25 16:11:52 +0200938 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939
Christian Königafef8b82016-08-12 13:29:18 +0200940 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200941 amdgpu_vm_write_pte(params->adev, params->ib, pe,
942 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943
944 } else {
Christian König27c5f362016-08-04 15:02:49 +0200945 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 count, incr, flags);
947 }
948}
949
950/**
Christian Königafef8b82016-08-12 13:29:18 +0200951 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
952 *
953 * @params: see amdgpu_pte_update_params definition
954 * @pe: addr of the page entry
955 * @addr: dst addr to write into pe
956 * @count: number of page entries to update
957 * @incr: increase next addr by incr bytes
958 * @flags: hw access flags
959 *
960 * Traces the parameters and calls the DMA function to copy the PTEs.
961 */
962static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
963 uint64_t pe, uint64_t addr,
964 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800965 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200966{
Christian Königec2f05f2016-09-25 16:11:52 +0200967 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200968
Christian Königec2f05f2016-09-25 16:11:52 +0200969
970 trace_amdgpu_vm_copy_ptes(pe, src, count);
971
972 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200973}
974
975/**
Christian Königb07c9d22015-11-30 13:26:07 +0100976 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400977 *
Christian Königb07c9d22015-11-30 13:26:07 +0100978 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 * @addr: the unmapped addr
980 *
981 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100982 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400983 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200984static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985{
986 uint64_t result;
987
Christian Königde9ea7b2016-08-12 11:33:30 +0200988 /* page table offset */
989 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990
Christian Königde9ea7b2016-08-12 11:33:30 +0200991 /* in case cpu page size != gpu page size*/
992 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100993
994 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995
996 return result;
997}
998
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400999/**
1000 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
1001 *
1002 * @params: see amdgpu_pte_update_params definition
1003 * @pe: kmap addr of the page entry
1004 * @addr: dst addr to write into pe
1005 * @count: number of page entries to update
1006 * @incr: increase next addr by incr bytes
1007 * @flags: hw access flags
1008 *
1009 * Write count number of PT/PD entries directly.
1010 */
1011static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
1012 uint64_t pe, uint64_t addr,
1013 unsigned count, uint32_t incr,
1014 uint64_t flags)
1015{
1016 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001017 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001018
Christian König03918b32017-07-11 17:15:37 +02001019 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
1020
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001021 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001022 value = params->pages_addr ?
1023 amdgpu_vm_map_gart(params->pages_addr, addr) :
1024 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001025 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001026 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001027 addr += incr;
1028 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001029}
1030
Christian Königa33cab72017-07-11 17:13:00 +02001031static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1032 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001033{
1034 struct amdgpu_sync sync;
1035 int r;
1036
1037 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -04001038 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001039 r = amdgpu_sync_wait(&sync, true);
1040 amdgpu_sync_free(&sync);
1041
1042 return r;
1043}
1044
Christian Königf8991ba2016-09-16 15:36:49 +02001045/*
Christian König194d2162016-10-12 15:13:52 +02001046 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +02001047 *
1048 * @adev: amdgpu_device pointer
1049 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +02001050 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +02001051 *
Christian König194d2162016-10-12 15:13:52 +02001052 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +02001053 * Returns 0 for success, error for failure.
1054 */
Christian König194d2162016-10-12 15:13:52 +02001055static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1056 struct amdgpu_vm *vm,
Christian Königea097292017-08-09 14:15:46 +02001057 struct amdgpu_vm_pt *parent)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001058{
Christian Königf8991ba2016-09-16 15:36:49 +02001059 struct amdgpu_bo *shadow;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001060 struct amdgpu_ring *ring = NULL;
1061 uint64_t pd_addr, shadow_addr = 0;
Christian Königf8991ba2016-09-16 15:36:49 +02001062 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001063 unsigned count = 0, pt_idx, ndw = 0;
Christian Königd71518b2016-02-01 12:20:25 +01001064 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001065 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +10001066 struct dma_fence *fence = NULL;
Christian Königea097292017-08-09 14:15:46 +02001067 uint32_t incr;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001068
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001069 int r;
1070
Christian König194d2162016-10-12 15:13:52 +02001071 if (!parent->entries)
1072 return 0;
Christian Königd71518b2016-02-01 12:20:25 +01001073
Christian König27c5f362016-08-04 15:02:49 +02001074 memset(&params, 0, sizeof(params));
1075 params.adev = adev;
Christian König3f3333f2017-08-03 14:02:13 +02001076 shadow = parent->base.bo->shadow;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001077
Alex Deucher69277982017-07-13 15:37:11 -04001078 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02001079 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Christian Königa33cab72017-07-11 17:13:00 +02001080 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001081 if (unlikely(r))
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001082 return r;
Christian König0a096fb2017-07-12 10:01:48 +02001083
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001084 params.func = amdgpu_vm_cpu_set_ptes;
1085 } else {
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001086 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1087 sched);
1088
1089 /* padding, etc. */
1090 ndw = 64;
1091
1092 /* assume the worst case */
1093 ndw += parent->last_entry_used * 6;
1094
Christian König3f3333f2017-08-03 14:02:13 +02001095 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001096
1097 if (shadow) {
1098 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1099 ndw *= 2;
1100 } else {
1101 shadow_addr = 0;
1102 }
1103
1104 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1105 if (r)
1106 return r;
1107
1108 params.ib = &job->ibs[0];
1109 params.func = amdgpu_vm_do_set_ptes;
1110 }
1111
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001112
Christian König194d2162016-10-12 15:13:52 +02001113 /* walk over the address space and update the directory */
1114 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
Christian Königea097292017-08-09 14:15:46 +02001115 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1116 struct amdgpu_bo *bo = entry->base.bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117 uint64_t pde, pt;
1118
1119 if (bo == NULL)
1120 continue;
1121
Christian Königea097292017-08-09 14:15:46 +02001122 spin_lock(&vm->status_lock);
1123 list_del_init(&entry->base.vm_status);
1124 spin_unlock(&vm->status_lock);
1125
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001126 pt = amdgpu_bo_gpu_offset(bo);
Christian König53e2e912017-05-15 15:19:10 +02001127 pt = amdgpu_gart_get_vm_pde(adev, pt);
Christian König4ab40162017-08-03 20:30:50 +02001128 /* Don't update huge pages here */
1129 if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
1130 parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
Christian Königf8991ba2016-09-16 15:36:49 +02001131 continue;
1132
Christian König4ab40162017-08-03 20:30:50 +02001133 parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134
1135 pde = pd_addr + pt_idx * 8;
Christian Königea097292017-08-09 14:15:46 +02001136 incr = amdgpu_bo_size(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001137 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001138 ((last_pt + incr * count) != pt) ||
1139 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140
1141 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +02001142 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001143 params.func(&params,
1144 last_shadow,
1145 last_pt, count,
1146 incr,
1147 AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001148
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001149 params.func(&params, last_pde,
1150 last_pt, count, incr,
1151 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152 }
1153
1154 count = 1;
1155 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001156 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001157 last_pt = pt;
1158 } else {
1159 ++count;
1160 }
1161 }
1162
Christian Königf8991ba2016-09-16 15:36:49 +02001163 if (count) {
Christian König3f3333f2017-08-03 14:02:13 +02001164 if (vm->root.base.bo->shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001165 params.func(&params, last_shadow, last_pt,
1166 count, incr, AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001167
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001168 params.func(&params, last_pde, last_pt,
1169 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001170 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171
Christian König0a096fb2017-07-12 10:01:48 +02001172 if (!vm->use_cpu_for_update) {
1173 if (params.ib->length_dw == 0) {
1174 amdgpu_job_free(job);
1175 } else {
1176 amdgpu_ring_pad_ib(ring, params.ib);
Christian König3f3333f2017-08-03 14:02:13 +02001177 amdgpu_sync_resv(adev, &job->sync,
1178 parent->base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001179 AMDGPU_FENCE_OWNER_VM, false);
Christian König0a096fb2017-07-12 10:01:48 +02001180 if (shadow)
1181 amdgpu_sync_resv(adev, &job->sync,
1182 shadow->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001183 AMDGPU_FENCE_OWNER_VM, false);
Christian Königf8991ba2016-09-16 15:36:49 +02001184
Christian König0a096fb2017-07-12 10:01:48 +02001185 WARN_ON(params.ib->length_dw > ndw);
1186 r = amdgpu_job_submit(job, ring, &vm->entity,
1187 AMDGPU_FENCE_OWNER_VM, &fence);
1188 if (r)
1189 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001190
Christian König3f3333f2017-08-03 14:02:13 +02001191 amdgpu_bo_fence(parent->base.bo, fence, true);
Christian Königd5884512017-09-08 14:09:41 +02001192 dma_fence_put(vm->last_update);
1193 vm->last_update = fence;
Christian König0a096fb2017-07-12 10:01:48 +02001194 }
Christian König194d2162016-10-12 15:13:52 +02001195 }
Christian Königf8991ba2016-09-16 15:36:49 +02001196
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001198
1199error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001200 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001201 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001202}
1203
Christian König194d2162016-10-12 15:13:52 +02001204/*
Christian König92456b92017-05-12 16:09:26 +02001205 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1206 *
1207 * @parent: parent PD
1208 *
1209 * Mark all PD level as invalid after an error.
1210 */
Christian Königea097292017-08-09 14:15:46 +02001211static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
1212 struct amdgpu_vm_pt *parent)
Christian König92456b92017-05-12 16:09:26 +02001213{
1214 unsigned pt_idx;
1215
1216 /*
1217 * Recurse into the subdirectories. This recursion is harmless because
1218 * we only have a maximum of 5 layers.
1219 */
1220 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1221 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1222
Christian König3f3333f2017-08-03 14:02:13 +02001223 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001224 continue;
1225
1226 entry->addr = ~0ULL;
Christian Königea097292017-08-09 14:15:46 +02001227 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001228 if (list_empty(&entry->base.vm_status))
1229 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +02001230 spin_unlock(&vm->status_lock);
1231 amdgpu_vm_invalidate_level(vm, entry);
Christian König92456b92017-05-12 16:09:26 +02001232 }
1233}
1234
1235/*
Christian König194d2162016-10-12 15:13:52 +02001236 * amdgpu_vm_update_directories - make sure that all directories are valid
1237 *
1238 * @adev: amdgpu_device pointer
1239 * @vm: requested vm
1240 *
1241 * Makes sure all directories are up to date.
1242 * Returns 0 for success, error for failure.
1243 */
1244int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1245 struct amdgpu_vm *vm)
1246{
Christian König92456b92017-05-12 16:09:26 +02001247 int r;
1248
Christian Königea097292017-08-09 14:15:46 +02001249 spin_lock(&vm->status_lock);
1250 while (!list_empty(&vm->relocated)) {
1251 struct amdgpu_vm_bo_base *bo_base;
1252 struct amdgpu_bo *bo;
1253
1254 bo_base = list_first_entry(&vm->relocated,
1255 struct amdgpu_vm_bo_base,
1256 vm_status);
1257 spin_unlock(&vm->status_lock);
1258
1259 bo = bo_base->bo->parent;
1260 if (bo) {
1261 struct amdgpu_vm_bo_base *parent;
1262 struct amdgpu_vm_pt *pt;
1263
1264 parent = list_first_entry(&bo->va,
1265 struct amdgpu_vm_bo_base,
1266 bo_list);
1267 pt = container_of(parent, struct amdgpu_vm_pt, base);
1268
1269 r = amdgpu_vm_update_level(adev, vm, pt);
1270 if (r) {
1271 amdgpu_vm_invalidate_level(vm, &vm->root);
1272 return r;
1273 }
1274 spin_lock(&vm->status_lock);
1275 } else {
1276 spin_lock(&vm->status_lock);
1277 list_del_init(&bo_base->vm_status);
1278 }
1279 }
1280 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +02001281
Christian König68c62302017-07-11 17:23:29 +02001282 if (vm->use_cpu_for_update) {
1283 /* Flush HDP */
1284 mb();
1285 amdgpu_gart_flush_gpu_tlb(adev, 0);
1286 }
1287
Christian König92456b92017-05-12 16:09:26 +02001288 return r;
Christian König194d2162016-10-12 15:13:52 +02001289}
1290
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001291/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001292 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001293 *
1294 * @p: see amdgpu_pte_update_params definition
1295 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001296 * @entry: resulting entry or NULL
1297 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001298 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001299 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001300 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001301void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1302 struct amdgpu_vm_pt **entry,
1303 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001304{
Christian König4e2cb642016-10-25 15:52:28 +02001305 unsigned idx, level = p->adev->vm_manager.num_level;
1306
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001307 *parent = NULL;
1308 *entry = &p->vm->root;
1309 while ((*entry)->entries) {
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001310 idx = addr >> (p->adev->vm_manager.block_size * level--);
Christian König3f3333f2017-08-03 14:02:13 +02001311 idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001312 *parent = *entry;
1313 *entry = &(*entry)->entries[idx];
Christian König4e2cb642016-10-25 15:52:28 +02001314 }
1315
1316 if (level)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001317 *entry = NULL;
1318}
Christian König4e2cb642016-10-25 15:52:28 +02001319
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001320/**
1321 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1322 *
1323 * @p: see amdgpu_pte_update_params definition
1324 * @entry: vm_pt entry to check
1325 * @parent: parent entry
1326 * @nptes: number of PTEs updated with this operation
1327 * @dst: destination address where the PTEs should point to
1328 * @flags: access flags fro the PTEs
1329 *
1330 * Check if we can update the PD with a huge page.
1331 */
Christian Königec5207c2017-08-03 19:24:06 +02001332static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1333 struct amdgpu_vm_pt *entry,
1334 struct amdgpu_vm_pt *parent,
1335 unsigned nptes, uint64_t dst,
1336 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001337{
1338 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1339 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001340
1341 /* In the case of a mixed PT the PDE must point to it*/
1342 if (p->adev->asic_type < CHIP_VEGA10 ||
1343 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehlingb2529032017-08-17 16:37:49 -04001344 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001345 !(flags & AMDGPU_PTE_VALID)) {
1346
Christian König3f3333f2017-08-03 14:02:13 +02001347 dst = amdgpu_bo_gpu_offset(entry->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001348 dst = amdgpu_gart_get_vm_pde(p->adev, dst);
1349 flags = AMDGPU_PTE_VALID;
1350 } else {
Christian König4ab40162017-08-03 20:30:50 +02001351 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001352 flags |= AMDGPU_PDE_PTE;
1353 }
1354
Christian König4ab40162017-08-03 20:30:50 +02001355 if (entry->addr == (dst | flags))
Christian Königec5207c2017-08-03 19:24:06 +02001356 return;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001357
Christian König4ab40162017-08-03 20:30:50 +02001358 entry->addr = (dst | flags);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001359
1360 if (use_cpu_update) {
Felix Kuehlingb2529032017-08-17 16:37:49 -04001361 /* In case a huge page is replaced with a system
1362 * memory mapping, p->pages_addr != NULL and
1363 * amdgpu_vm_cpu_set_ptes would try to translate dst
1364 * through amdgpu_vm_map_gart. But dst is already a
1365 * GPU address (of the page table). Disable
1366 * amdgpu_vm_map_gart temporarily.
1367 */
1368 dma_addr_t *tmp;
1369
1370 tmp = p->pages_addr;
1371 p->pages_addr = NULL;
1372
Christian König3f3333f2017-08-03 14:02:13 +02001373 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001374 pde = pd_addr + (entry - parent->entries) * 8;
1375 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehlingb2529032017-08-17 16:37:49 -04001376
1377 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001378 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001379 if (parent->base.bo->shadow) {
1380 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001381 pde = pd_addr + (entry - parent->entries) * 8;
1382 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1383 }
Christian König3f3333f2017-08-03 14:02:13 +02001384 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001385 pde = pd_addr + (entry - parent->entries) * 8;
1386 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1387 }
Christian König4e2cb642016-10-25 15:52:28 +02001388}
1389
1390/**
Christian König92696dd2016-08-05 13:56:35 +02001391 * amdgpu_vm_update_ptes - make sure that page tables are valid
1392 *
1393 * @params: see amdgpu_pte_update_params definition
1394 * @vm: requested vm
1395 * @start: start of GPU address range
1396 * @end: end of GPU address range
1397 * @dst: destination address to map to, the next dst inside the function
1398 * @flags: mapping flags
1399 *
1400 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001401 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001402 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001403static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001404 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001405 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001406{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001407 struct amdgpu_device *adev = params->adev;
1408 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001409
Christian König301654a2017-05-16 14:30:27 +02001410 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001411 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001412 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001413 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001414
1415 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001416 for (addr = start; addr < end; addr += nptes,
1417 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1418 struct amdgpu_vm_pt *entry, *parent;
1419
1420 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1421 if (!entry)
1422 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001423
Christian König92696dd2016-08-05 13:56:35 +02001424 if ((addr & ~mask) == (end & ~mask))
1425 nptes = end - addr;
1426 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001427 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001428
Christian Königec5207c2017-08-03 19:24:06 +02001429 amdgpu_vm_handle_huge_pages(params, entry, parent,
1430 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001431 /* We don't need to update PTEs for huge pages */
1432 if (entry->addr & AMDGPU_PDE_PTE)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001433 continue;
1434
Christian König3f3333f2017-08-03 14:02:13 +02001435 pt = entry->base.bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001436 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001437 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001438 } else {
1439 if (pt->shadow) {
1440 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1441 pe_start += (addr & mask) * 8;
1442 params->func(params, pe_start, dst, nptes,
1443 AMDGPU_GPU_PAGE_SIZE, flags);
1444 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001445 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001446 }
Christian König92696dd2016-08-05 13:56:35 +02001447
Christian König301654a2017-05-16 14:30:27 +02001448 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001449 params->func(params, pe_start, dst, nptes,
1450 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001451 }
1452
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001453 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001454}
1455
1456/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001457 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1458 *
Christian König29efc4f2016-08-04 14:52:50 +02001459 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001460 * @vm: requested vm
1461 * @start: first PTE to handle
1462 * @end: last PTE to handle
1463 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001464 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001465 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001466 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001467static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001468 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001469 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470{
1471 /**
1472 * The MC L1 TLB supports variable sized pages, based on a fragment
1473 * field in the PTE. When this field is set to a non-zero value, page
1474 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1475 * flags are considered valid for all PTEs within the fragment range
1476 * and corresponding mappings are assumed to be physically contiguous.
1477 *
1478 * The L1 TLB can store a single PTE for the whole fragment,
1479 * significantly increasing the space available for translation
1480 * caching. This leads to large improvements in throughput when the
1481 * TLB is under pressure.
1482 *
1483 * The L2 TLB distributes small and large fragments into two
1484 * asymmetric partitions. The large fragment cache is significantly
1485 * larger. Thus, we try to use large fragments wherever possible.
1486 * Userspace can support this by aligning virtual base address and
1487 * allocation size to the fragment size.
1488 */
Roger He6849d472017-08-30 13:01:19 +08001489 unsigned max_frag = params->adev->vm_manager.fragment_size;
1490 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001491
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001493 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001494 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001495
Roger He6849d472017-08-30 13:01:19 +08001496 while (start != end) {
1497 uint64_t frag_flags, frag_end;
1498 unsigned frag;
1499
1500 /* This intentionally wraps around if no bit is set */
1501 frag = min((unsigned)ffs(start) - 1,
1502 (unsigned)fls64(end - start) - 1);
1503 if (frag >= max_frag) {
1504 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1505 frag_end = end & ~((1ULL << max_frag) - 1);
1506 } else {
1507 frag_flags = AMDGPU_PTE_FRAG(frag);
1508 frag_end = start + (1 << frag);
1509 }
1510
1511 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1512 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001513 if (r)
1514 return r;
Roger He6849d472017-08-30 13:01:19 +08001515
1516 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1517 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001518 }
1519
Roger He6849d472017-08-30 13:01:19 +08001520 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001521}
1522
1523/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001524 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1525 *
1526 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001527 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001528 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001529 * @vm: requested vm
1530 * @start: start of mapped range
1531 * @last: last mapped entry
1532 * @flags: flags for the entries
1533 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001534 * @fence: optional resulting fence
1535 *
Christian Königa14faa62016-01-25 14:27:31 +01001536 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001538 */
1539static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001540 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001541 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001543 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001544 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001545 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546{
Christian König2d55e452016-02-08 17:37:38 +01001547 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001548 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001549 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001550 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001551 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001552 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553 int r;
1554
Christian Königafef8b82016-08-12 13:29:18 +02001555 memset(&params, 0, sizeof(params));
1556 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001557 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001558
Christian Königa33cab72017-07-11 17:13:00 +02001559 /* sync to everything on unmapping */
1560 if (!(flags & AMDGPU_PTE_VALID))
1561 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1562
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001563 if (vm->use_cpu_for_update) {
1564 /* params.src is used as flag to indicate system Memory */
1565 if (pages_addr)
1566 params.src = ~0;
1567
1568 /* Wait for PT BOs to be free. PTs share the same resv. object
1569 * as the root PD BO
1570 */
Christian Königa33cab72017-07-11 17:13:00 +02001571 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001572 if (unlikely(r))
1573 return r;
1574
1575 params.func = amdgpu_vm_cpu_set_ptes;
1576 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001577 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1578 addr, flags);
1579 }
1580
Christian König2d55e452016-02-08 17:37:38 +01001581 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001582
Christian Königa14faa62016-01-25 14:27:31 +01001583 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001584
1585 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001586 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001587 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001588 *
1589 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001590 */
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001591 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592
1593 /* padding, etc. */
1594 ndw = 64;
1595
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001596 /* one PDE write for each huge page */
1597 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1598
Christian König570144c2017-08-30 15:38:45 +02001599 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001600 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001601 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602
Christian Königb0456f92016-08-11 14:06:54 +02001603 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001604 ndw += nptes * 2;
1605
Christian Königafef8b82016-08-12 13:29:18 +02001606 params.func = amdgpu_vm_do_copy_ptes;
1607
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608 } else {
1609 /* set page commands needed */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001610 ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611
Roger He6849d472017-08-30 13:01:19 +08001612 /* extra commands for begin/end fragments */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001613 ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
1614 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001615
1616 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 }
1618
Christian Königd71518b2016-02-01 12:20:25 +01001619 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1620 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001622
Christian König29efc4f2016-08-04 14:52:50 +02001623 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001624
Christian König570144c2017-08-30 15:38:45 +02001625 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001626 uint64_t *pte;
1627 unsigned i;
1628
1629 /* Put the PTEs at the end of the IB. */
1630 i = ndw - nptes * 2;
1631 pte= (uint64_t *)&(job->ibs->ptr[i]);
1632 params.src = job->ibs->gpu_addr + i * 4;
1633
1634 for (i = 0; i < nptes; ++i) {
1635 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1636 AMDGPU_GPU_PAGE_SIZE);
1637 pte[i] |= flags;
1638 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001639 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001640 }
1641
Christian König3cabaa52016-06-06 10:17:58 +02001642 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1643 if (r)
1644 goto error_free;
1645
Christian König3f3333f2017-08-03 14:02:13 +02001646 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001647 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001648 if (r)
1649 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650
Christian König3f3333f2017-08-03 14:02:13 +02001651 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001652 if (r)
1653 goto error_free;
1654
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001655 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1656 if (r)
1657 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001658
Christian König29efc4f2016-08-04 14:52:50 +02001659 amdgpu_ring_pad_ib(ring, params.ib);
1660 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001661 r = amdgpu_job_submit(job, ring, &vm->entity,
1662 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001663 if (r)
1664 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001665
Christian König3f3333f2017-08-03 14:02:13 +02001666 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001667 dma_fence_put(*fence);
1668 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001670
1671error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001672 amdgpu_job_free(job);
Christian Königea097292017-08-09 14:15:46 +02001673 amdgpu_vm_invalidate_level(vm, &vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001674 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675}
1676
1677/**
Christian Königa14faa62016-01-25 14:27:31 +01001678 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1679 *
1680 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001681 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001682 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001683 * @vm: requested vm
1684 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001685 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001686 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001687 * @fence: optional resulting fence
1688 *
1689 * Split the mapping into smaller chunks so that each update fits
1690 * into a SDMA IB.
1691 * Returns 0 for success, -EINVAL for failure.
1692 */
1693static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001694 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001695 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001696 struct amdgpu_vm *vm,
1697 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001698 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001699 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001700 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001701{
Christian König9fc8fc72017-09-18 13:58:30 +02001702 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001703 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001704 int r;
1705
1706 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1707 * but in case of something, we filter the flags in first place
1708 */
1709 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1710 flags &= ~AMDGPU_PTE_READABLE;
1711 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1712 flags &= ~AMDGPU_PTE_WRITEABLE;
1713
Alex Xie15b31c52017-03-03 16:47:11 -05001714 flags &= ~AMDGPU_PTE_EXECUTABLE;
1715 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1716
Alex Xieb0fd18b2017-03-03 16:49:39 -05001717 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1718 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1719
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001720 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1721 (adev->asic_type >= CHIP_VEGA10)) {
1722 flags |= AMDGPU_PTE_PRT;
1723 flags &= ~AMDGPU_PTE_VALID;
1724 }
1725
Christian Königa14faa62016-01-25 14:27:31 +01001726 trace_amdgpu_vm_bo_update(mapping);
1727
Christian König63e0ba42016-08-16 17:38:37 +02001728 pfn = mapping->offset >> PAGE_SHIFT;
1729 if (nodes) {
1730 while (pfn >= nodes->size) {
1731 pfn -= nodes->size;
1732 ++nodes;
1733 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001734 }
Christian Königa14faa62016-01-25 14:27:31 +01001735
Christian König63e0ba42016-08-16 17:38:37 +02001736 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001737 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001738 uint64_t max_entries;
1739 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001740
Christian König63e0ba42016-08-16 17:38:37 +02001741 if (nodes) {
1742 addr = nodes->start << PAGE_SHIFT;
1743 max_entries = (nodes->size - pfn) *
1744 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1745 } else {
1746 addr = 0;
1747 max_entries = S64_MAX;
1748 }
Christian Königa14faa62016-01-25 14:27:31 +01001749
Christian König63e0ba42016-08-16 17:38:37 +02001750 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001751 uint64_t count;
1752
Christian König457e0fe2017-08-22 12:50:46 +02001753 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001754 for (count = 1; count < max_entries; ++count) {
1755 uint64_t idx = pfn + count;
1756
1757 if (pages_addr[idx] !=
1758 (pages_addr[idx - 1] + PAGE_SIZE))
1759 break;
1760 }
1761
1762 if (count < min_linear_pages) {
1763 addr = pfn << PAGE_SHIFT;
1764 dma_addr = pages_addr;
1765 } else {
1766 addr = pages_addr[pfn];
1767 max_entries = count;
1768 }
1769
Christian König63e0ba42016-08-16 17:38:37 +02001770 } else if (flags & AMDGPU_PTE_VALID) {
1771 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001772 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001773 }
Christian König63e0ba42016-08-16 17:38:37 +02001774
Christian Königa9f87f62017-03-30 14:03:59 +02001775 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001776 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001777 start, last, flags, addr,
1778 fence);
1779 if (r)
1780 return r;
1781
Christian König63e0ba42016-08-16 17:38:37 +02001782 pfn += last - start + 1;
1783 if (nodes && nodes->size == pfn) {
1784 pfn = 0;
1785 ++nodes;
1786 }
Christian Königa14faa62016-01-25 14:27:31 +01001787 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001788
Christian Königa9f87f62017-03-30 14:03:59 +02001789 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001790
1791 return 0;
1792}
1793
1794/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1796 *
1797 * @adev: amdgpu_device pointer
1798 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001799 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001800 *
1801 * Fill in the page table entries for @bo_va.
1802 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001803 */
1804int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1805 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001806 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001807{
Christian Königec681542017-08-01 10:51:43 +02001808 struct amdgpu_bo *bo = bo_va->base.bo;
1809 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001811 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001812 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001813 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001814 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001815 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001816 int r;
1817
Christian Königec681542017-08-01 10:51:43 +02001818 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001819 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001820 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001821 exclusive = NULL;
1822 } else {
Christian König8358dce2016-03-30 10:50:25 +02001823 struct ttm_dma_tt *ttm;
1824
Christian Königec681542017-08-01 10:51:43 +02001825 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001826 nodes = mem->mm_node;
1827 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001828 ttm = container_of(bo_va->base.bo->tbo.ttm,
1829 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001830 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001831 }
Christian Königec681542017-08-01 10:51:43 +02001832 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001833 }
1834
Christian König457e0fe2017-08-22 12:50:46 +02001835 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001836 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001837 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001838 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001839
Christian König4e55eb32017-09-11 16:54:59 +02001840 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1841 last_update = &vm->last_update;
1842 else
1843 last_update = &bo_va->last_pt_update;
1844
Christian König3d7d4d32017-08-23 16:13:33 +02001845 if (!clear && bo_va->base.moved) {
1846 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001847 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001848
Christian Königcb7b6ec2017-08-15 17:08:12 +02001849 } else if (bo_va->cleared != clear) {
1850 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001851 }
Christian König7fc11952015-07-30 11:53:42 +02001852
1853 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001854 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001855 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001856 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857 if (r)
1858 return r;
1859 }
1860
Christian König68c62302017-07-11 17:23:29 +02001861 if (vm->use_cpu_for_update) {
1862 /* Flush HDP */
1863 mb();
1864 amdgpu_gart_flush_gpu_tlb(adev, 0);
1865 }
1866
Christian Königcb7b6ec2017-08-15 17:08:12 +02001867 spin_lock(&vm->status_lock);
1868 list_del_init(&bo_va->base.vm_status);
1869 spin_unlock(&vm->status_lock);
1870
1871 list_splice_init(&bo_va->invalids, &bo_va->valids);
1872 bo_va->cleared = clear;
1873
1874 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1875 list_for_each_entry(mapping, &bo_va->valids, list)
1876 trace_amdgpu_vm_bo_mapping(mapping);
1877 }
1878
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001879 return 0;
1880}
1881
1882/**
Christian König284710f2017-01-30 11:09:31 +01001883 * amdgpu_vm_update_prt_state - update the global PRT state
1884 */
1885static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1886{
1887 unsigned long flags;
1888 bool enable;
1889
1890 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001891 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001892 adev->gart.gart_funcs->set_prt(adev, enable);
1893 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1894}
1895
1896/**
Christian König4388fc22017-03-13 10:13:36 +01001897 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001898 */
1899static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1900{
Christian König4388fc22017-03-13 10:13:36 +01001901 if (!adev->gart.gart_funcs->set_prt)
1902 return;
1903
Christian König451bc8e2017-02-14 16:02:52 +01001904 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1905 amdgpu_vm_update_prt_state(adev);
1906}
1907
1908/**
Christian König0b15f2f2017-02-14 15:47:03 +01001909 * amdgpu_vm_prt_put - drop a PRT user
1910 */
1911static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1912{
Christian König451bc8e2017-02-14 16:02:52 +01001913 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001914 amdgpu_vm_update_prt_state(adev);
1915}
1916
1917/**
Christian König451bc8e2017-02-14 16:02:52 +01001918 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001919 */
1920static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1921{
1922 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1923
Christian König0b15f2f2017-02-14 15:47:03 +01001924 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001925 kfree(cb);
1926}
1927
1928/**
Christian König451bc8e2017-02-14 16:02:52 +01001929 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1930 */
1931static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1932 struct dma_fence *fence)
1933{
Christian König4388fc22017-03-13 10:13:36 +01001934 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001935
Christian König4388fc22017-03-13 10:13:36 +01001936 if (!adev->gart.gart_funcs->set_prt)
1937 return;
1938
1939 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001940 if (!cb) {
1941 /* Last resort when we are OOM */
1942 if (fence)
1943 dma_fence_wait(fence, false);
1944
Dan Carpenter486a68f2017-04-03 21:41:39 +03001945 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001946 } else {
1947 cb->adev = adev;
1948 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1949 amdgpu_vm_prt_cb))
1950 amdgpu_vm_prt_cb(fence, &cb->cb);
1951 }
1952}
1953
1954/**
Christian König284710f2017-01-30 11:09:31 +01001955 * amdgpu_vm_free_mapping - free a mapping
1956 *
1957 * @adev: amdgpu_device pointer
1958 * @vm: requested vm
1959 * @mapping: mapping to be freed
1960 * @fence: fence of the unmap operation
1961 *
1962 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1963 */
1964static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1965 struct amdgpu_vm *vm,
1966 struct amdgpu_bo_va_mapping *mapping,
1967 struct dma_fence *fence)
1968{
Christian König451bc8e2017-02-14 16:02:52 +01001969 if (mapping->flags & AMDGPU_PTE_PRT)
1970 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001971 kfree(mapping);
1972}
1973
1974/**
Christian König451bc8e2017-02-14 16:02:52 +01001975 * amdgpu_vm_prt_fini - finish all prt mappings
1976 *
1977 * @adev: amdgpu_device pointer
1978 * @vm: requested vm
1979 *
1980 * Register a cleanup callback to disable PRT support after VM dies.
1981 */
1982static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1983{
Christian König3f3333f2017-08-03 14:02:13 +02001984 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001985 struct dma_fence *excl, **shared;
1986 unsigned i, shared_count;
1987 int r;
1988
1989 r = reservation_object_get_fences_rcu(resv, &excl,
1990 &shared_count, &shared);
1991 if (r) {
1992 /* Not enough memory to grab the fence list, as last resort
1993 * block for all the fences to complete.
1994 */
1995 reservation_object_wait_timeout_rcu(resv, true, false,
1996 MAX_SCHEDULE_TIMEOUT);
1997 return;
1998 }
1999
2000 /* Add a callback for each fence in the reservation object */
2001 amdgpu_vm_prt_get(adev);
2002 amdgpu_vm_add_prt_cb(adev, excl);
2003
2004 for (i = 0; i < shared_count; ++i) {
2005 amdgpu_vm_prt_get(adev);
2006 amdgpu_vm_add_prt_cb(adev, shared[i]);
2007 }
2008
2009 kfree(shared);
2010}
2011
2012/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 * amdgpu_vm_clear_freed - clear freed BOs in the PT
2014 *
2015 * @adev: amdgpu_device pointer
2016 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002017 * @fence: optional resulting fence (unchanged if no work needed to be done
2018 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019 *
2020 * Make sure all freed BOs are cleared in the PT.
2021 * Returns 0 for success.
2022 *
2023 * PTs have to be reserved and mutex must be locked!
2024 */
2025int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002026 struct amdgpu_vm *vm,
2027 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028{
2029 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002030 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002032 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002033
2034 while (!list_empty(&vm->freed)) {
2035 mapping = list_first_entry(&vm->freed,
2036 struct amdgpu_bo_va_mapping, list);
2037 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01002038
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002039 if (vm->pte_support_ats)
Yong Zhao6d16dac2017-08-31 15:55:00 -04002040 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002041
Christian König570144c2017-08-30 15:38:45 +02002042 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02002043 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002044 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002045 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01002046 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002047 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048 return r;
Christian König284710f2017-01-30 11:09:31 +01002049 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002051
2052 if (fence && f) {
2053 dma_fence_put(*fence);
2054 *fence = f;
2055 } else {
2056 dma_fence_put(f);
2057 }
2058
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059 return 0;
2060
2061}
2062
2063/**
Christian König73fb16e2017-08-16 11:13:48 +02002064 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065 *
2066 * @adev: amdgpu_device pointer
2067 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02002068 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069 *
Christian König73fb16e2017-08-16 11:13:48 +02002070 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071 * Returns 0 for success.
2072 *
Christian König73fb16e2017-08-16 11:13:48 +02002073 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002074 */
Christian König73fb16e2017-08-16 11:13:48 +02002075int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02002076 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077{
Christian König73fb16e2017-08-16 11:13:48 +02002078 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02002079 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080
2081 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002082 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02002083 struct amdgpu_bo_va *bo_va;
2084
Christian König27c7b9a2017-08-01 11:27:36 +02002085 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002086 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002088
Christian König73fb16e2017-08-16 11:13:48 +02002089 /* Per VM BOs never need to bo cleared in the page tables */
2090 clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
2091
2092 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002093 if (r)
2094 return r;
2095
2096 spin_lock(&vm->status_lock);
2097 }
2098 spin_unlock(&vm->status_lock);
2099
Christian König91e1a522015-07-06 22:06:40 +02002100 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002101}
2102
2103/**
2104 * amdgpu_vm_bo_add - add a bo to a specific vm
2105 *
2106 * @adev: amdgpu_device pointer
2107 * @vm: requested vm
2108 * @bo: amdgpu buffer object
2109 *
Christian König8843dbb2016-01-26 12:17:11 +01002110 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 * Add @bo to the list of bos associated with the vm
2112 * Returns newly added bo_va or NULL for failure
2113 *
2114 * Object has to be reserved!
2115 */
2116struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2117 struct amdgpu_vm *vm,
2118 struct amdgpu_bo *bo)
2119{
2120 struct amdgpu_bo_va *bo_va;
2121
2122 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2123 if (bo_va == NULL) {
2124 return NULL;
2125 }
Christian Königec681542017-08-01 10:51:43 +02002126 bo_va->base.vm = vm;
2127 bo_va->base.bo = bo;
2128 INIT_LIST_HEAD(&bo_va->base.bo_list);
2129 INIT_LIST_HEAD(&bo_va->base.vm_status);
2130
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002131 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002132 INIT_LIST_HEAD(&bo_va->valids);
2133 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002134
Christian Königa5f6b5b2017-01-30 11:01:38 +01002135 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002136 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002137
2138 return bo_va;
2139}
2140
Christian König73fb16e2017-08-16 11:13:48 +02002141
2142/**
2143 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2144 *
2145 * @adev: amdgpu_device pointer
2146 * @bo_va: bo_va to store the address
2147 * @mapping: the mapping to insert
2148 *
2149 * Insert a new mapping into all structures.
2150 */
2151static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2152 struct amdgpu_bo_va *bo_va,
2153 struct amdgpu_bo_va_mapping *mapping)
2154{
2155 struct amdgpu_vm *vm = bo_va->base.vm;
2156 struct amdgpu_bo *bo = bo_va->base.bo;
2157
Christian Königaebc5e62017-09-06 16:55:16 +02002158 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002159 list_add(&mapping->list, &bo_va->invalids);
2160 amdgpu_vm_it_insert(mapping, &vm->va);
2161
2162 if (mapping->flags & AMDGPU_PTE_PRT)
2163 amdgpu_vm_prt_get(adev);
2164
2165 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2166 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002167 if (list_empty(&bo_va->base.vm_status))
2168 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02002169 spin_unlock(&vm->status_lock);
2170 }
2171 trace_amdgpu_vm_bo_map(bo_va, mapping);
2172}
2173
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002174/**
2175 * amdgpu_vm_bo_map - map bo inside a vm
2176 *
2177 * @adev: amdgpu_device pointer
2178 * @bo_va: bo_va to store the address
2179 * @saddr: where to map the BO
2180 * @offset: requested offset in the BO
2181 * @flags: attributes of pages (read/write/valid/etc.)
2182 *
2183 * Add a mapping of the BO at the specefied addr into the VM.
2184 * Returns 0 for success, error for failure.
2185 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002186 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 */
2188int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2189 struct amdgpu_bo_va *bo_va,
2190 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002191 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002192{
Christian Königa9f87f62017-03-30 14:03:59 +02002193 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002194 struct amdgpu_bo *bo = bo_va->base.bo;
2195 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002196 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002197
Christian König0be52de2015-05-18 14:37:27 +02002198 /* validate the parameters */
2199 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002200 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002201 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002202
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002204 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002205 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002206 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209 saddr /= AMDGPU_GPU_PAGE_SIZE;
2210 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2211
Christian Königa9f87f62017-03-30 14:03:59 +02002212 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2213 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 /* bo and tmp overlap, invalid addr */
2215 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002216 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002217 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002218 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 }
2220
2221 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002222 if (!mapping)
2223 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002224
Christian Königa9f87f62017-03-30 14:03:59 +02002225 mapping->start = saddr;
2226 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227 mapping->offset = offset;
2228 mapping->flags = flags;
2229
Christian König73fb16e2017-08-16 11:13:48 +02002230 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002231
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002233}
2234
2235/**
Christian König80f95c52017-03-13 10:13:39 +01002236 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2237 *
2238 * @adev: amdgpu_device pointer
2239 * @bo_va: bo_va to store the address
2240 * @saddr: where to map the BO
2241 * @offset: requested offset in the BO
2242 * @flags: attributes of pages (read/write/valid/etc.)
2243 *
2244 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2245 * mappings as we do so.
2246 * Returns 0 for success, error for failure.
2247 *
2248 * Object has to be reserved and unreserved outside!
2249 */
2250int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2251 struct amdgpu_bo_va *bo_va,
2252 uint64_t saddr, uint64_t offset,
2253 uint64_t size, uint64_t flags)
2254{
2255 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002256 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002257 uint64_t eaddr;
2258 int r;
2259
2260 /* validate the parameters */
2261 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2262 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2263 return -EINVAL;
2264
2265 /* make sure object fit at this offset */
2266 eaddr = saddr + size - 1;
2267 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002268 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002269 return -EINVAL;
2270
2271 /* Allocate all the needed memory */
2272 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2273 if (!mapping)
2274 return -ENOMEM;
2275
Christian Königec681542017-08-01 10:51:43 +02002276 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002277 if (r) {
2278 kfree(mapping);
2279 return r;
2280 }
2281
2282 saddr /= AMDGPU_GPU_PAGE_SIZE;
2283 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2284
Christian Königa9f87f62017-03-30 14:03:59 +02002285 mapping->start = saddr;
2286 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002287 mapping->offset = offset;
2288 mapping->flags = flags;
2289
Christian König73fb16e2017-08-16 11:13:48 +02002290 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002291
2292 return 0;
2293}
2294
2295/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002296 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2297 *
2298 * @adev: amdgpu_device pointer
2299 * @bo_va: bo_va to remove the address from
2300 * @saddr: where to the BO is mapped
2301 *
2302 * Remove a mapping of the BO at the specefied addr from the VM.
2303 * Returns 0 for success, error for failure.
2304 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002305 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002306 */
2307int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2308 struct amdgpu_bo_va *bo_va,
2309 uint64_t saddr)
2310{
2311 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002312 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002313 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314
Christian König6c7fc502015-06-05 20:56:17 +02002315 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002316
Christian König7fc11952015-07-30 11:53:42 +02002317 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002318 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002319 break;
2320 }
2321
Christian König7fc11952015-07-30 11:53:42 +02002322 if (&mapping->list == &bo_va->valids) {
2323 valid = false;
2324
2325 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002326 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002327 break;
2328 }
2329
Christian König32b41ac2016-03-08 18:03:27 +01002330 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002331 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002332 }
Christian König32b41ac2016-03-08 18:03:27 +01002333
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002334 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002335 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002336 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002337 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002338
Christian Könige17841b2016-03-08 17:52:01 +01002339 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002340 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002341 else
Christian König284710f2017-01-30 11:09:31 +01002342 amdgpu_vm_free_mapping(adev, vm, mapping,
2343 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002344
2345 return 0;
2346}
2347
2348/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002349 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2350 *
2351 * @adev: amdgpu_device pointer
2352 * @vm: VM structure to use
2353 * @saddr: start of the range
2354 * @size: size of the range
2355 *
2356 * Remove all mappings in a range, split them as appropriate.
2357 * Returns 0 for success, error for failure.
2358 */
2359int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2360 struct amdgpu_vm *vm,
2361 uint64_t saddr, uint64_t size)
2362{
2363 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002364 LIST_HEAD(removed);
2365 uint64_t eaddr;
2366
2367 eaddr = saddr + size - 1;
2368 saddr /= AMDGPU_GPU_PAGE_SIZE;
2369 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2370
2371 /* Allocate all the needed memory */
2372 before = kzalloc(sizeof(*before), GFP_KERNEL);
2373 if (!before)
2374 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002375 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002376
2377 after = kzalloc(sizeof(*after), GFP_KERNEL);
2378 if (!after) {
2379 kfree(before);
2380 return -ENOMEM;
2381 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002382 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002383
2384 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002385 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2386 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002387 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002388 if (tmp->start < saddr) {
2389 before->start = tmp->start;
2390 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002391 before->offset = tmp->offset;
2392 before->flags = tmp->flags;
2393 list_add(&before->list, &tmp->list);
2394 }
2395
2396 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002397 if (tmp->last > eaddr) {
2398 after->start = eaddr + 1;
2399 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002400 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002401 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002402 after->flags = tmp->flags;
2403 list_add(&after->list, &tmp->list);
2404 }
2405
2406 list_del(&tmp->list);
2407 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002408
2409 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002410 }
2411
2412 /* And free them up */
2413 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002414 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002415 list_del(&tmp->list);
2416
Christian Königa9f87f62017-03-30 14:03:59 +02002417 if (tmp->start < saddr)
2418 tmp->start = saddr;
2419 if (tmp->last > eaddr)
2420 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002421
Christian Königaebc5e62017-09-06 16:55:16 +02002422 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002423 list_add(&tmp->list, &vm->freed);
2424 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2425 }
2426
Junwei Zhang27f6d612017-03-16 16:09:24 +08002427 /* Insert partial mapping before the range */
2428 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002429 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002430 if (before->flags & AMDGPU_PTE_PRT)
2431 amdgpu_vm_prt_get(adev);
2432 } else {
2433 kfree(before);
2434 }
2435
2436 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002437 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002438 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002439 if (after->flags & AMDGPU_PTE_PRT)
2440 amdgpu_vm_prt_get(adev);
2441 } else {
2442 kfree(after);
2443 }
2444
2445 return 0;
2446}
2447
2448/**
Christian Königaebc5e62017-09-06 16:55:16 +02002449 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2450 *
2451 * @vm: the requested VM
2452 *
2453 * Find a mapping by it's address.
2454 */
2455struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2456 uint64_t addr)
2457{
2458 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2459}
2460
2461/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002462 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2463 *
2464 * @adev: amdgpu_device pointer
2465 * @bo_va: requested bo_va
2466 *
Christian König8843dbb2016-01-26 12:17:11 +01002467 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002468 *
2469 * Object have to be reserved!
2470 */
2471void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2472 struct amdgpu_bo_va *bo_va)
2473{
2474 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002475 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476
Christian Königec681542017-08-01 10:51:43 +02002477 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002478
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002480 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481 spin_unlock(&vm->status_lock);
2482
Christian König7fc11952015-07-30 11:53:42 +02002483 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002484 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002485 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002486 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002487 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002488 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002489 }
Christian König7fc11952015-07-30 11:53:42 +02002490 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2491 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002492 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002493 amdgpu_vm_free_mapping(adev, vm, mapping,
2494 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002495 }
Christian König32b41ac2016-03-08 18:03:27 +01002496
Chris Wilsonf54d1862016-10-25 13:00:45 +01002497 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002498 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499}
2500
2501/**
2502 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2503 *
2504 * @adev: amdgpu_device pointer
2505 * @vm: requested vm
2506 * @bo: amdgpu buffer object
2507 *
Christian König8843dbb2016-01-26 12:17:11 +01002508 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002509 */
2510void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002511 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002512{
Christian Königec681542017-08-01 10:51:43 +02002513 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002514
Christian Königec681542017-08-01 10:51:43 +02002515 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002516 struct amdgpu_vm *vm = bo_base->vm;
2517
Christian König3d7d4d32017-08-23 16:13:33 +02002518 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002519 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2520 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002521 if (bo->tbo.type == ttm_bo_type_kernel)
2522 list_move(&bo_base->vm_status, &vm->evicted);
2523 else
2524 list_move_tail(&bo_base->vm_status,
2525 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002526 spin_unlock(&bo_base->vm->status_lock);
2527 continue;
2528 }
2529
Christian Königea097292017-08-09 14:15:46 +02002530 if (bo->tbo.type == ttm_bo_type_kernel) {
2531 spin_lock(&bo_base->vm->status_lock);
2532 if (list_empty(&bo_base->vm_status))
2533 list_add(&bo_base->vm_status, &vm->relocated);
2534 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002535 continue;
Christian Königea097292017-08-09 14:15:46 +02002536 }
Christian König3f3333f2017-08-03 14:02:13 +02002537
Christian Königec681542017-08-01 10:51:43 +02002538 spin_lock(&bo_base->vm->status_lock);
2539 if (list_empty(&bo_base->vm_status))
Christian König481c2e92017-09-01 14:46:19 +02002540 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002541 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002542 }
2543}
2544
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002545static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2546{
2547 /* Total bits covered by PD + PTs */
2548 unsigned bits = ilog2(vm_size) + 18;
2549
2550 /* Make sure the PD is 4K in size up to 8GB address space.
2551 Above that split equal between PD and PTs */
2552 if (vm_size <= 8)
2553 return (bits - 9);
2554 else
2555 return ((bits + 3) / 2);
2556}
2557
2558/**
Roger Hed07f14b2017-08-15 16:05:59 +08002559 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2560 *
2561 * @adev: amdgpu_device pointer
2562 * @fragment_size_default: the default fragment size if it's set auto
2563 */
Christian Königc38e0692017-09-18 14:01:45 +02002564void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,
2565 uint32_t fragment_size_default)
Roger Hed07f14b2017-08-15 16:05:59 +08002566{
2567 if (amdgpu_vm_fragment_size == -1)
2568 adev->vm_manager.fragment_size = fragment_size_default;
2569 else
2570 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2571}
2572
2573/**
2574 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002575 *
2576 * @adev: amdgpu_device pointer
2577 * @vm_size: the default vm size if it's set auto
2578 */
Christian Königc38e0692017-09-18 14:01:45 +02002579void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size,
2580 uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002581{
2582 /* adjust vm size firstly */
2583 if (amdgpu_vm_size == -1)
2584 adev->vm_manager.vm_size = vm_size;
2585 else
2586 adev->vm_manager.vm_size = amdgpu_vm_size;
2587
2588 /* block size depends on vm size */
2589 if (amdgpu_vm_block_size == -1)
2590 adev->vm_manager.block_size =
2591 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2592 else
2593 adev->vm_manager.block_size = amdgpu_vm_block_size;
2594
Roger Hed07f14b2017-08-15 16:05:59 +08002595 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2596
2597 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2598 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2599 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002600}
2601
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002602/**
2603 * amdgpu_vm_init - initialize a vm instance
2604 *
2605 * @adev: amdgpu_device pointer
2606 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002607 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608 *
Christian König8843dbb2016-01-26 12:17:11 +01002609 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002610 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002611int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002612 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002613{
2614 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002615 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002616 unsigned ring_instance;
2617 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002618 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002619 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002620 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002621 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002622
Davidlohr Buesof808c132017-09-08 16:15:08 -07002623 vm->va = RB_ROOT_CACHED;
Chunming Zhou031e2982016-04-25 10:19:13 +08002624 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002625 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2626 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002627 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002628 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002629 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002630 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002631 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002632
Christian König2bd9ccf2016-02-01 12:53:58 +01002633 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002634
2635 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2636 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2637 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002638 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2639 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2640 rq, amdgpu_sched_jobs);
2641 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002642 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002643
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002644 vm->pte_support_ats = false;
2645
2646 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002647 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2648 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002649
2650 if (adev->asic_type == CHIP_RAVEN) {
2651 vm->pte_support_ats = true;
Yong Zhao6d16dac2017-08-31 15:55:00 -04002652 init_pde_value = AMDGPU_PTE_DEFAULT_ATC
2653 | AMDGPU_PDE_PTE;
2654
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002655 }
2656 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002657 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2658 AMDGPU_VM_USE_CPU_FOR_GFX);
2659 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2660 vm->use_cpu_for_update ? "CPU" : "SDMA");
2661 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2662 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002663 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002664
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002665 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2666 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2667 if (vm->use_cpu_for_update)
2668 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2669 else
2670 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2671 AMDGPU_GEM_CREATE_SHADOW);
2672
Christian Königf566ceb2016-10-27 20:04:38 +02002673 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002674 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002675 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002676 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002677 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002678 goto error_free_sched_entity;
2679
Christian König3f3333f2017-08-03 14:02:13 +02002680 vm->root.base.vm = vm;
2681 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2682 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002683
2684 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002685 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002686 if (r)
2687 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002688
Christian König3f3333f2017-08-03 14:02:13 +02002689 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
Felix Kuehlingca290da2017-08-25 20:15:04 -04002690 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König2bd9ccf2016-02-01 12:53:58 +01002691 if (r)
2692 goto error_free_root;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002693 }
2694
Felix Kuehling02208442017-08-25 20:40:26 -04002695 if (pasid) {
2696 unsigned long flags;
2697
2698 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2699 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2700 GFP_ATOMIC);
2701 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2702 if (r < 0)
2703 goto error_free_root;
2704
2705 vm->pasid = pasid;
2706 }
2707
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002708 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002709 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002710
2711 return 0;
2712
2713error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002714 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2715 amdgpu_bo_unref(&vm->root.base.bo);
2716 vm->root.base.bo = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002717
2718error_free_sched_entity:
2719 amd_sched_entity_fini(&ring->sched, &vm->entity);
2720
2721 return r;
2722}
2723
2724/**
Christian Königf566ceb2016-10-27 20:04:38 +02002725 * amdgpu_vm_free_levels - free PD/PT levels
2726 *
2727 * @level: PD/PT starting level to free
2728 *
2729 * Free the page directory or page table level and all sub levels.
2730 */
2731static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2732{
2733 unsigned i;
2734
Christian König3f3333f2017-08-03 14:02:13 +02002735 if (level->base.bo) {
2736 list_del(&level->base.bo_list);
2737 list_del(&level->base.vm_status);
2738 amdgpu_bo_unref(&level->base.bo->shadow);
2739 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002740 }
2741
2742 if (level->entries)
2743 for (i = 0; i <= level->last_entry_used; i++)
2744 amdgpu_vm_free_levels(&level->entries[i]);
2745
Michal Hocko20981052017-05-17 14:23:12 +02002746 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002747}
2748
2749/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002750 * amdgpu_vm_fini - tear down a vm instance
2751 *
2752 * @adev: amdgpu_device pointer
2753 * @vm: requested vm
2754 *
Christian König8843dbb2016-01-26 12:17:11 +01002755 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002756 * Unbind the VM and remove all bos from the vm bo list
2757 */
2758void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2759{
2760 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002761 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002762 u64 fault;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002763 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002764
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002765 /* Clear pending page faults from IH when the VM is destroyed */
2766 while (kfifo_get(&vm->faults, &fault))
2767 amdgpu_ih_clear_fault(adev, fault);
2768
Felix Kuehling02208442017-08-25 20:40:26 -04002769 if (vm->pasid) {
2770 unsigned long flags;
2771
2772 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2773 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2774 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2775 }
2776
Christian König2d55e452016-02-08 17:37:38 +01002777 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002778
Davidlohr Buesof808c132017-09-08 16:15:08 -07002779 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002780 dev_err(adev->dev, "still active bo inside vm\n");
2781 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002782 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2783 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002784 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002785 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002786 kfree(mapping);
2787 }
2788 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002789 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002790 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002791 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002792 }
Christian König284710f2017-01-30 11:09:31 +01002793
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002794 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002795 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002796 }
2797
Christian Königf566ceb2016-10-27 20:04:38 +02002798 amdgpu_vm_free_levels(&vm->root);
Christian Königd5884512017-09-08 14:09:41 +02002799 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002800 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2801 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002802}
Christian Königea89f8c2015-11-15 20:52:06 +01002803
2804/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002805 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2806 *
2807 * @adev: amdgpu_device pointer
2808 * @pasid: PASID do identify the VM
2809 *
2810 * This function is expected to be called in interrupt context. Returns
2811 * true if there was fault credit, false otherwise
2812 */
2813bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2814 unsigned int pasid)
2815{
2816 struct amdgpu_vm *vm;
2817
2818 spin_lock(&adev->vm_manager.pasid_lock);
2819 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2820 spin_unlock(&adev->vm_manager.pasid_lock);
2821 if (!vm)
2822 /* VM not found, can't track fault credit */
2823 return true;
2824
2825 /* No lock needed. only accessed by IRQ handler */
2826 if (!vm->fault_credit)
2827 /* Too many faults in this VM */
2828 return false;
2829
2830 vm->fault_credit--;
2831 return true;
2832}
2833
2834/**
Christian Königa9a78b32016-01-21 10:19:11 +01002835 * amdgpu_vm_manager_init - init the VM manager
2836 *
2837 * @adev: amdgpu_device pointer
2838 *
2839 * Initialize the VM manager structures
2840 */
2841void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2842{
Christian König76456702017-04-06 17:52:39 +02002843 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002844
Christian König76456702017-04-06 17:52:39 +02002845 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2846 struct amdgpu_vm_id_manager *id_mgr =
2847 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002848
Christian König76456702017-04-06 17:52:39 +02002849 mutex_init(&id_mgr->lock);
2850 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002851 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002852
2853 /* skip over VMID 0, since it is the system VM */
2854 for (j = 1; j < id_mgr->num_ids; ++j) {
2855 amdgpu_vm_reset_id(adev, i, j);
2856 amdgpu_sync_create(&id_mgr->ids[i].active);
2857 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2858 }
Christian König971fe9a92016-03-01 15:09:25 +01002859 }
Christian König2d55e452016-02-08 17:37:38 +01002860
Chris Wilsonf54d1862016-10-25 13:00:45 +01002861 adev->vm_manager.fence_context =
2862 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002863 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2864 adev->vm_manager.seqno[i] = 0;
2865
Christian König2d55e452016-02-08 17:37:38 +01002866 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002867 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002868 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002869 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002870
2871 /* If not overridden by the user, by default, only in large BAR systems
2872 * Compute VM tables will be updated by CPU
2873 */
2874#ifdef CONFIG_X86_64
2875 if (amdgpu_vm_update_mode == -1) {
2876 if (amdgpu_vm_is_large_bar(adev))
2877 adev->vm_manager.vm_update_mode =
2878 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2879 else
2880 adev->vm_manager.vm_update_mode = 0;
2881 } else
2882 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2883#else
2884 adev->vm_manager.vm_update_mode = 0;
2885#endif
2886
Felix Kuehling02208442017-08-25 20:40:26 -04002887 idr_init(&adev->vm_manager.pasid_idr);
2888 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002889}
2890
2891/**
Christian Königea89f8c2015-11-15 20:52:06 +01002892 * amdgpu_vm_manager_fini - cleanup VM manager
2893 *
2894 * @adev: amdgpu_device pointer
2895 *
2896 * Cleanup the VM manager and free resources.
2897 */
2898void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2899{
Christian König76456702017-04-06 17:52:39 +02002900 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002901
Felix Kuehling02208442017-08-25 20:40:26 -04002902 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2903 idr_destroy(&adev->vm_manager.pasid_idr);
2904
Christian König76456702017-04-06 17:52:39 +02002905 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2906 struct amdgpu_vm_id_manager *id_mgr =
2907 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002908
Christian König76456702017-04-06 17:52:39 +02002909 mutex_destroy(&id_mgr->lock);
2910 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2911 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2912
2913 amdgpu_sync_free(&id->active);
2914 dma_fence_put(id->flushed_updates);
2915 dma_fence_put(id->last_flush);
2916 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002917 }
Christian Königea89f8c2015-11-15 20:52:06 +01002918}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002919
2920int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2921{
2922 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002923 struct amdgpu_device *adev = dev->dev_private;
2924 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2925 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002926
2927 switch (args->in.op) {
2928 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002929 /* current, we only have requirement to reserve vmid from gfxhub */
2930 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2931 AMDGPU_GFXHUB);
2932 if (r)
2933 return r;
2934 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002935 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002936 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002937 break;
2938 default:
2939 return -EINVAL;
2940 }
2941
2942 return 0;
2943}