blob: bca9eebb6947109545e223de5aed29dd0e39c34b [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önig34d7be52017-08-24 12:32:55 +0200273}
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) {
331 init_value = AMDGPU_PTE_SYSTEM;
332 if (level != adev->vm_manager.num_level - 1)
333 init_value |= AMDGPU_PDE_PTE;
334 }
335
Christian Königf566ceb2016-10-27 20:04:38 +0200336 /* walk over the address space and allocate the page tables */
337 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200338 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200339 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
340 struct amdgpu_bo *pt;
341
Christian König3f3333f2017-08-03 14:02:13 +0200342 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200343 r = amdgpu_bo_create(adev,
344 amdgpu_vm_bo_size(adev, level),
345 AMDGPU_GPU_PAGE_SIZE, true,
346 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400347 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400348 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200349 if (r)
350 return r;
351
Christian König0a096fb2017-07-12 10:01:48 +0200352 if (vm->use_cpu_for_update) {
353 r = amdgpu_bo_kmap(pt, NULL);
354 if (r) {
355 amdgpu_bo_unref(&pt);
356 return r;
357 }
358 }
359
Christian Königf566ceb2016-10-27 20:04:38 +0200360 /* Keep a reference to the root directory to avoid
361 * freeing them up in the wrong order.
362 */
Christian König0f2fc432017-08-31 10:46:20 +0200363 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200364
Christian König3f3333f2017-08-03 14:02:13 +0200365 entry->base.vm = vm;
366 entry->base.bo = pt;
367 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200368 spin_lock(&vm->status_lock);
369 list_add(&entry->base.vm_status, &vm->relocated);
370 spin_unlock(&vm->status_lock);
Christian König0f2fc432017-08-31 10:46:20 +0200371 entry->addr = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200372 }
373
374 if (level < adev->vm_manager.num_level) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400375 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
376 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
377 ((1 << shift) - 1);
378 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
379 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200380 if (r)
381 return r;
382 }
383 }
384
385 return 0;
386}
387
Christian König663e4572017-03-13 10:13:37 +0100388/**
389 * amdgpu_vm_alloc_pts - Allocate page tables.
390 *
391 * @adev: amdgpu_device pointer
392 * @vm: VM to allocate page tables for
393 * @saddr: Start address which needs to be allocated
394 * @size: Size from start address we need.
395 *
396 * Make sure the page tables are allocated.
397 */
398int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
399 struct amdgpu_vm *vm,
400 uint64_t saddr, uint64_t size)
401{
Felix Kuehling22770e52017-03-28 20:24:53 -0400402 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100403 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100404
405 /* validate the parameters */
406 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
407 return -EINVAL;
408
409 eaddr = saddr + size - 1;
410 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
411 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400412 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100413 last_pfn, adev->vm_manager.max_pfn);
414 return -EINVAL;
415 }
416
417 saddr /= AMDGPU_GPU_PAGE_SIZE;
418 eaddr /= AMDGPU_GPU_PAGE_SIZE;
419
Christian Königf566ceb2016-10-27 20:04:38 +0200420 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
Christian König663e4572017-03-13 10:13:37 +0100421}
422
Christian König641e9402017-04-03 13:59:25 +0200423/**
424 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
425 *
426 * @adev: amdgpu_device pointer
427 * @id: VMID structure
428 *
429 * Check if GPU reset occured since last use of the VMID.
430 */
431static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
432 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800433{
434 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200435 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800436}
437
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800438static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
439{
440 return !!vm->reserved_vmid[vmhub];
441}
442
443/* idr_mgr->lock must be held */
444static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
445 struct amdgpu_ring *ring,
446 struct amdgpu_sync *sync,
447 struct dma_fence *fence,
448 struct amdgpu_job *job)
449{
450 struct amdgpu_device *adev = ring->adev;
451 unsigned vmhub = ring->funcs->vmhub;
452 uint64_t fence_context = adev->fence_context + ring->idx;
453 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
454 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
455 struct dma_fence *updates = sync->last_vm_update;
456 int r = 0;
457 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200458 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800459
460 flushed = id->flushed_updates;
461 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
462 (atomic64_read(&id->owner) != vm->client_id) ||
463 (job->vm_pd_addr != id->pd_gpu_addr) ||
464 (updates && (!flushed || updates->context != flushed->context ||
465 dma_fence_is_later(updates, flushed))) ||
466 (!id->last_flush || (id->last_flush->context != fence_context &&
467 !dma_fence_is_signaled(id->last_flush)))) {
468 needs_flush = true;
469 /* to prevent one context starved by another context */
470 id->pd_gpu_addr = 0;
471 tmp = amdgpu_sync_peek_fence(&id->active, ring);
472 if (tmp) {
473 r = amdgpu_sync_fence(adev, sync, tmp);
474 return r;
475 }
476 }
477
478 /* Good we can use this VMID. Remember this submission as
479 * user of the VMID.
480 */
481 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
482 if (r)
483 goto out;
484
485 if (updates && (!flushed || updates->context != flushed->context ||
486 dma_fence_is_later(updates, flushed))) {
487 dma_fence_put(id->flushed_updates);
488 id->flushed_updates = dma_fence_get(updates);
489 }
490 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800491 atomic64_set(&id->owner, vm->client_id);
492 job->vm_needs_flush = needs_flush;
493 if (needs_flush) {
494 dma_fence_put(id->last_flush);
495 id->last_flush = NULL;
496 }
497 job->vm_id = id - id_mgr->ids;
498 trace_amdgpu_vm_grab_id(vm, ring, job);
499out:
500 return r;
501}
502
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503/**
504 * amdgpu_vm_grab_id - allocate the next free VMID
505 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200507 * @ring: ring we want to submit job to
508 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100509 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400510 *
Christian König7f8a5292015-07-20 16:09:40 +0200511 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400512 */
Christian König7f8a5292015-07-20 16:09:40 +0200513int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100514 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800515 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400516{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200518 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200519 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200520 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100521 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200522 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100523 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200524 unsigned i;
525 int r = 0;
526
Christian König76456702017-04-06 17:52:39 +0200527 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800528 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
529 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
530 mutex_unlock(&id_mgr->lock);
531 return r;
532 }
533 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
534 if (!fences) {
535 mutex_unlock(&id_mgr->lock);
536 return -ENOMEM;
537 }
Christian König36fd7c52016-05-23 15:30:08 +0200538 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200539 i = 0;
Christian König76456702017-04-06 17:52:39 +0200540 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200541 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
542 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200543 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200544 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200545 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100546
Christian König1fbb2e92016-06-01 10:47:36 +0200547 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200548 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200549 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
550 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100551 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200552 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200553
Christian König1fbb2e92016-06-01 10:47:36 +0200554 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100555 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200556
Chris Wilsonf54d1862016-10-25 13:00:45 +0100557 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200558 seqno, true);
559 if (!array) {
560 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100561 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200562 kfree(fences);
563 r = -ENOMEM;
564 goto error;
565 }
Christian König8d76001e2016-05-23 16:00:32 +0200566
Christian König8d76001e2016-05-23 16:00:32 +0200567
Christian König1fbb2e92016-06-01 10:47:36 +0200568 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100569 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200570 if (r)
571 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200572
Christian König76456702017-04-06 17:52:39 +0200573 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200574 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200575
Christian König1fbb2e92016-06-01 10:47:36 +0200576 }
577 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200578
Christian König6f1ceab2017-07-11 16:59:21 +0200579 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200580 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200581 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100582 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200583 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200584
Christian König1fbb2e92016-06-01 10:47:36 +0200585 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200586 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800587 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200588
589 if (atomic64_read(&id->owner) != vm->client_id)
590 continue;
591
Chunming Zhoufd53be32016-07-01 17:59:01 +0800592 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200593 continue;
594
Christian König87c910d2017-03-30 16:56:20 +0200595 if (!id->last_flush ||
596 (id->last_flush->context != fence_context &&
597 !dma_fence_is_signaled(id->last_flush)))
598 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200599
600 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200601 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
602 needs_flush = true;
603
604 /* Concurrent flushes are only possible starting with Vega10 */
605 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200606 continue;
607
Christian König3dab83b2016-06-01 13:31:17 +0200608 /* Good we can use this VMID. Remember this submission as
609 * user of the VMID.
610 */
Christian König1fbb2e92016-06-01 10:47:36 +0200611 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
612 if (r)
613 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200614
Christian König87c910d2017-03-30 16:56:20 +0200615 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
616 dma_fence_put(id->flushed_updates);
617 id->flushed_updates = dma_fence_get(updates);
618 }
Christian König8d76001e2016-05-23 16:00:32 +0200619
Christian König87c910d2017-03-30 16:56:20 +0200620 if (needs_flush)
621 goto needs_flush;
622 else
623 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200624
Christian König4f618e72017-04-06 15:18:21 +0200625 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800626
Christian König1fbb2e92016-06-01 10:47:36 +0200627 /* Still no ID to use? Then use the idle one found earlier */
628 id = idle;
629
630 /* Remember this submission as user of the VMID */
631 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100632 if (r)
633 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100634
Christian König87c910d2017-03-30 16:56:20 +0200635 id->pd_gpu_addr = job->vm_pd_addr;
636 dma_fence_put(id->flushed_updates);
637 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200638 atomic64_set(&id->owner, vm->client_id);
639
640needs_flush:
641 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100642 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100643 id->last_flush = NULL;
644
Christian König87c910d2017-03-30 16:56:20 +0200645no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200646 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647
Christian König76456702017-04-06 17:52:39 +0200648 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200649 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100650
651error:
Christian König76456702017-04-06 17:52:39 +0200652 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100653 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654}
655
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800656static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
657 struct amdgpu_vm *vm,
658 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400659{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800660 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400661
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800662 mutex_lock(&id_mgr->lock);
663 if (vm->reserved_vmid[vmhub]) {
664 list_add(&vm->reserved_vmid[vmhub]->list,
665 &id_mgr->ids_lru);
666 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800667 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400668 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800669 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400670}
671
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800672static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
673 struct amdgpu_vm *vm,
674 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500675{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800676 struct amdgpu_vm_id_manager *id_mgr;
677 struct amdgpu_vm_id *idle;
678 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500679
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800680 id_mgr = &adev->vm_manager.id_mgr[vmhub];
681 mutex_lock(&id_mgr->lock);
682 if (vm->reserved_vmid[vmhub])
683 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800684 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
685 AMDGPU_VM_MAX_RESERVED_VMID) {
686 DRM_ERROR("Over limitation of reserved vmid\n");
687 atomic_dec(&id_mgr->reserved_vmid_num);
688 r = -EINVAL;
689 goto unlock;
690 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800691 /* Select the first entry VMID */
692 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
693 list_del_init(&idle->list);
694 vm->reserved_vmid[vmhub] = idle;
695 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500696
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800697 return 0;
698unlock:
699 mutex_unlock(&id_mgr->lock);
700 return r;
701}
702
Alex Xiee59c0202017-06-01 09:42:59 -0400703/**
704 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
705 *
706 * @adev: amdgpu_device pointer
707 */
708void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
709{
710 const struct amdgpu_ip_block *ip_block;
711 bool has_compute_vm_bug;
712 struct amdgpu_ring *ring;
713 int i;
714
715 has_compute_vm_bug = false;
716
717 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
718 if (ip_block) {
719 /* Compute has a VM bug for GFX version < 7.
720 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
721 if (ip_block->version->major <= 7)
722 has_compute_vm_bug = true;
723 else if (ip_block->version->major == 8)
724 if (adev->gfx.mec_fw_version < 673)
725 has_compute_vm_bug = true;
726 }
727
728 for (i = 0; i < adev->num_rings; i++) {
729 ring = adev->rings[i];
730 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
731 /* only compute rings */
732 ring->has_compute_vm_bug = has_compute_vm_bug;
733 else
734 ring->has_compute_vm_bug = false;
735 }
736}
737
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400738bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
739 struct amdgpu_job *job)
740{
741 struct amdgpu_device *adev = ring->adev;
742 unsigned vmhub = ring->funcs->vmhub;
743 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
744 struct amdgpu_vm_id *id;
745 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400746 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400747
748 if (job->vm_id == 0)
749 return false;
750 id = &id_mgr->ids[job->vm_id];
751 gds_switch_needed = ring->funcs->emit_gds_switch && (
752 id->gds_base != job->gds_base ||
753 id->gds_size != job->gds_size ||
754 id->gws_base != job->gws_base ||
755 id->gws_size != job->gws_size ||
756 id->oa_base != job->oa_base ||
757 id->oa_size != job->oa_size);
758
759 if (amdgpu_vm_had_gpu_reset(adev, id))
760 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400761
762 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400763}
764
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400765static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
766{
767 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500768}
769
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770/**
771 * amdgpu_vm_flush - hardware flush the vm
772 *
773 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100774 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100775 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776 *
Christian König4ff37a82016-02-26 16:18:26 +0100777 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400778 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800779int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780{
Christian König971fe9a92016-03-01 15:09:25 +0100781 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200782 unsigned vmhub = ring->funcs->vmhub;
783 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
784 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100785 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800786 id->gds_base != job->gds_base ||
787 id->gds_size != job->gds_size ||
788 id->gws_base != job->gws_base ||
789 id->gws_size != job->gws_size ||
790 id->oa_base != job->oa_base ||
791 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800792 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200793 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100794 int r;
Christian Königd564a062016-03-01 15:51:53 +0100795
Christian Königf7d015b2017-04-03 14:28:26 +0200796 if (amdgpu_vm_had_gpu_reset(adev, id)) {
797 gds_switch_needed = true;
798 vm_flush_needed = true;
799 }
Christian König971fe9a92016-03-01 15:09:25 +0100800
Monk Liu8fdf0742017-06-06 17:25:13 +0800801 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200802 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100803
Christian Königc0e51932017-04-03 14:16:07 +0200804 if (ring->funcs->init_cond_exec)
805 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100806
Monk Liu8fdf0742017-06-06 17:25:13 +0800807 if (need_pipe_sync)
808 amdgpu_ring_emit_pipeline_sync(ring);
809
Christian Königf7d015b2017-04-03 14:28:26 +0200810 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200811 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800812
Christian König9a94f5a2017-05-12 14:46:23 +0200813 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
814 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800815
Christian Königc0e51932017-04-03 14:16:07 +0200816 r = amdgpu_fence_emit(ring, &fence);
817 if (r)
818 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800819
Christian König76456702017-04-06 17:52:39 +0200820 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200821 dma_fence_put(id->last_flush);
822 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800823 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200824 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200825 }
Monk Liue9d672b2017-03-15 12:18:57 +0800826
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800827 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200828 id->gds_base = job->gds_base;
829 id->gds_size = job->gds_size;
830 id->gws_base = job->gws_base;
831 id->gws_size = job->gws_size;
832 id->oa_base = job->oa_base;
833 id->oa_size = job->oa_size;
834 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
835 job->gds_size, job->gws_base,
836 job->gws_size, job->oa_base,
837 job->oa_size);
838 }
839
840 if (ring->funcs->patch_cond_exec)
841 amdgpu_ring_patch_cond_exec(ring, patch_offset);
842
843 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
844 if (ring->funcs->emit_switch_buffer) {
845 amdgpu_ring_emit_switch_buffer(ring);
846 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400847 }
Christian König41d9eb22016-03-01 16:46:18 +0100848 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100849}
850
851/**
852 * amdgpu_vm_reset_id - reset VMID to zero
853 *
854 * @adev: amdgpu device structure
855 * @vm_id: vmid number to use
856 *
857 * Reset saved GDW, GWS and OA to force switch on next flush.
858 */
Christian König76456702017-04-06 17:52:39 +0200859void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
860 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100861{
Christian König76456702017-04-06 17:52:39 +0200862 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
863 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100864
Christian Königb3c85a02017-05-10 20:06:58 +0200865 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100866 id->gds_base = 0;
867 id->gds_size = 0;
868 id->gws_base = 0;
869 id->gws_size = 0;
870 id->oa_base = 0;
871 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400872}
873
874/**
Christian Königb3c85a02017-05-10 20:06:58 +0200875 * amdgpu_vm_reset_all_id - reset VMID to zero
876 *
877 * @adev: amdgpu device structure
878 *
879 * Reset VMID to force flush on next use
880 */
881void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
882{
883 unsigned i, j;
884
885 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
886 struct amdgpu_vm_id_manager *id_mgr =
887 &adev->vm_manager.id_mgr[i];
888
889 for (j = 1; j < id_mgr->num_ids; ++j)
890 amdgpu_vm_reset_id(adev, i, j);
891 }
892}
893
894/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
896 *
897 * @vm: requested vm
898 * @bo: requested buffer object
899 *
Christian König8843dbb2016-01-26 12:17:11 +0100900 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400901 * Search inside the @bos vm list for the requested vm
902 * Returns the found bo_va or NULL if none is found
903 *
904 * Object has to be reserved!
905 */
906struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
907 struct amdgpu_bo *bo)
908{
909 struct amdgpu_bo_va *bo_va;
910
Christian Königec681542017-08-01 10:51:43 +0200911 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
912 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 return bo_va;
914 }
915 }
916 return NULL;
917}
918
919/**
Christian Königafef8b82016-08-12 13:29:18 +0200920 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921 *
Christian König29efc4f2016-08-04 14:52:50 +0200922 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400923 * @pe: addr of the page entry
924 * @addr: dst addr to write into pe
925 * @count: number of page entries to update
926 * @incr: increase next addr by incr bytes
927 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400928 *
929 * Traces the parameters and calls the right asic functions
930 * to setup the page table using the DMA.
931 */
Christian Königafef8b82016-08-12 13:29:18 +0200932static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
933 uint64_t pe, uint64_t addr,
934 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800935 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936{
Christian Königec2f05f2016-09-25 16:11:52 +0200937 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938
Christian Königafef8b82016-08-12 13:29:18 +0200939 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200940 amdgpu_vm_write_pte(params->adev, params->ib, pe,
941 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942
943 } else {
Christian König27c5f362016-08-04 15:02:49 +0200944 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400945 count, incr, flags);
946 }
947}
948
949/**
Christian Königafef8b82016-08-12 13:29:18 +0200950 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
951 *
952 * @params: see amdgpu_pte_update_params definition
953 * @pe: addr of the page entry
954 * @addr: dst addr to write into pe
955 * @count: number of page entries to update
956 * @incr: increase next addr by incr bytes
957 * @flags: hw access flags
958 *
959 * Traces the parameters and calls the DMA function to copy the PTEs.
960 */
961static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
962 uint64_t pe, uint64_t addr,
963 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800964 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200965{
Christian Königec2f05f2016-09-25 16:11:52 +0200966 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200967
Christian Königec2f05f2016-09-25 16:11:52 +0200968
969 trace_amdgpu_vm_copy_ptes(pe, src, count);
970
971 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200972}
973
974/**
Christian Königb07c9d22015-11-30 13:26:07 +0100975 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976 *
Christian Königb07c9d22015-11-30 13:26:07 +0100977 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978 * @addr: the unmapped addr
979 *
980 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100981 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200983static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984{
985 uint64_t result;
986
Christian Königde9ea7b2016-08-12 11:33:30 +0200987 /* page table offset */
988 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989
Christian Königde9ea7b2016-08-12 11:33:30 +0200990 /* in case cpu page size != gpu page size*/
991 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100992
993 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400994
995 return result;
996}
997
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400998/**
999 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
1000 *
1001 * @params: see amdgpu_pte_update_params definition
1002 * @pe: kmap addr of the page entry
1003 * @addr: dst addr to write into pe
1004 * @count: number of page entries to update
1005 * @incr: increase next addr by incr bytes
1006 * @flags: hw access flags
1007 *
1008 * Write count number of PT/PD entries directly.
1009 */
1010static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
1011 uint64_t pe, uint64_t addr,
1012 unsigned count, uint32_t incr,
1013 uint64_t flags)
1014{
1015 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001016 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001017
Christian König03918b32017-07-11 17:15:37 +02001018 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
1019
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001020 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001021 value = params->pages_addr ?
1022 amdgpu_vm_map_gart(params->pages_addr, addr) :
1023 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001024 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001025 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001026 addr += incr;
1027 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001028}
1029
Christian Königa33cab72017-07-11 17:13:00 +02001030static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1031 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001032{
1033 struct amdgpu_sync sync;
1034 int r;
1035
1036 amdgpu_sync_create(&sync);
Christian König3f3333f2017-08-03 14:02:13 +02001037 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001038 r = amdgpu_sync_wait(&sync, true);
1039 amdgpu_sync_free(&sync);
1040
1041 return r;
1042}
1043
Christian Königf8991ba2016-09-16 15:36:49 +02001044/*
Christian König194d2162016-10-12 15:13:52 +02001045 * amdgpu_vm_update_level - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +02001046 *
1047 * @adev: amdgpu_device pointer
1048 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +02001049 * @parent: parent directory
Christian Königf8991ba2016-09-16 15:36:49 +02001050 *
Christian König194d2162016-10-12 15:13:52 +02001051 * Makes sure all entries in @parent are up to date.
Christian Königf8991ba2016-09-16 15:36:49 +02001052 * Returns 0 for success, error for failure.
1053 */
Christian König194d2162016-10-12 15:13:52 +02001054static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1055 struct amdgpu_vm *vm,
Christian Königea097292017-08-09 14:15:46 +02001056 struct amdgpu_vm_pt *parent)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001057{
Christian Königf8991ba2016-09-16 15:36:49 +02001058 struct amdgpu_bo *shadow;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001059 struct amdgpu_ring *ring = NULL;
1060 uint64_t pd_addr, shadow_addr = 0;
Christian Königf8991ba2016-09-16 15:36:49 +02001061 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001062 unsigned count = 0, pt_idx, ndw = 0;
Christian Königd71518b2016-02-01 12:20:25 +01001063 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001064 struct amdgpu_pte_update_params params;
Dave Airlie220196b2016-10-28 11:33:52 +10001065 struct dma_fence *fence = NULL;
Christian Königea097292017-08-09 14:15:46 +02001066 uint32_t incr;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001067
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068 int r;
1069
Christian König194d2162016-10-12 15:13:52 +02001070 if (!parent->entries)
1071 return 0;
Christian Königd71518b2016-02-01 12:20:25 +01001072
Christian König27c5f362016-08-04 15:02:49 +02001073 memset(&params, 0, sizeof(params));
1074 params.adev = adev;
Christian König3f3333f2017-08-03 14:02:13 +02001075 shadow = parent->base.bo->shadow;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001076
Alex Deucher69277982017-07-13 15:37:11 -04001077 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02001078 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Christian Königa33cab72017-07-11 17:13:00 +02001079 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001080 if (unlikely(r))
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001081 return r;
Christian König0a096fb2017-07-12 10:01:48 +02001082
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001083 params.func = amdgpu_vm_cpu_set_ptes;
1084 } else {
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001085 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1086 sched);
1087
1088 /* padding, etc. */
1089 ndw = 64;
1090
1091 /* assume the worst case */
1092 ndw += parent->last_entry_used * 6;
1093
Christian König3f3333f2017-08-03 14:02:13 +02001094 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001095
1096 if (shadow) {
1097 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1098 ndw *= 2;
1099 } else {
1100 shadow_addr = 0;
1101 }
1102
1103 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1104 if (r)
1105 return r;
1106
1107 params.ib = &job->ibs[0];
1108 params.func = amdgpu_vm_do_set_ptes;
1109 }
1110
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001111
Christian König194d2162016-10-12 15:13:52 +02001112 /* walk over the address space and update the directory */
1113 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
Christian Königea097292017-08-09 14:15:46 +02001114 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1115 struct amdgpu_bo *bo = entry->base.bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 uint64_t pde, pt;
1117
1118 if (bo == NULL)
1119 continue;
1120
Christian Königea097292017-08-09 14:15:46 +02001121 spin_lock(&vm->status_lock);
1122 list_del_init(&entry->base.vm_status);
1123 spin_unlock(&vm->status_lock);
1124
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 pt = amdgpu_bo_gpu_offset(bo);
Christian König53e2e912017-05-15 15:19:10 +02001126 pt = amdgpu_gart_get_vm_pde(adev, pt);
Christian König4ab40162017-08-03 20:30:50 +02001127 /* Don't update huge pages here */
1128 if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
1129 parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
Christian Königf8991ba2016-09-16 15:36:49 +02001130 continue;
1131
Christian König4ab40162017-08-03 20:30:50 +02001132 parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133
1134 pde = pd_addr + pt_idx * 8;
Christian Königea097292017-08-09 14:15:46 +02001135 incr = amdgpu_bo_size(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +02001137 ((last_pt + incr * count) != pt) ||
1138 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001139
1140 if (count) {
Christian Königf8991ba2016-09-16 15:36:49 +02001141 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001142 params.func(&params,
1143 last_shadow,
1144 last_pt, count,
1145 incr,
1146 AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001147
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001148 params.func(&params, last_pde,
1149 last_pt, count, incr,
1150 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151 }
1152
1153 count = 1;
1154 last_pde = pde;
Christian Königf8991ba2016-09-16 15:36:49 +02001155 last_shadow = shadow_addr + pt_idx * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156 last_pt = pt;
1157 } else {
1158 ++count;
1159 }
1160 }
1161
Christian Königf8991ba2016-09-16 15:36:49 +02001162 if (count) {
Christian König3f3333f2017-08-03 14:02:13 +02001163 if (vm->root.base.bo->shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001164 params.func(&params, last_shadow, last_pt,
1165 count, incr, AMDGPU_PTE_VALID);
Christian Königf8991ba2016-09-16 15:36:49 +02001166
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001167 params.func(&params, last_pde, last_pt,
1168 count, incr, AMDGPU_PTE_VALID);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001169 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001170
Christian König0a096fb2017-07-12 10:01:48 +02001171 if (!vm->use_cpu_for_update) {
1172 if (params.ib->length_dw == 0) {
1173 amdgpu_job_free(job);
1174 } else {
1175 amdgpu_ring_pad_ib(ring, params.ib);
Christian König3f3333f2017-08-03 14:02:13 +02001176 amdgpu_sync_resv(adev, &job->sync,
1177 parent->base.bo->tbo.resv,
Christian König194d2162016-10-12 15:13:52 +02001178 AMDGPU_FENCE_OWNER_VM);
Christian König0a096fb2017-07-12 10:01:48 +02001179 if (shadow)
1180 amdgpu_sync_resv(adev, &job->sync,
1181 shadow->tbo.resv,
1182 AMDGPU_FENCE_OWNER_VM);
Christian Königf8991ba2016-09-16 15:36:49 +02001183
Christian König0a096fb2017-07-12 10:01:48 +02001184 WARN_ON(params.ib->length_dw > ndw);
1185 r = amdgpu_job_submit(job, ring, &vm->entity,
1186 AMDGPU_FENCE_OWNER_VM, &fence);
1187 if (r)
1188 goto error_free;
Christian Königf8991ba2016-09-16 15:36:49 +02001189
Christian König3f3333f2017-08-03 14:02:13 +02001190 amdgpu_bo_fence(parent->base.bo, fence, true);
Christian Königd5884512017-09-08 14:09:41 +02001191 dma_fence_put(vm->last_update);
1192 vm->last_update = fence;
Christian König0a096fb2017-07-12 10:01:48 +02001193 }
Christian König194d2162016-10-12 15:13:52 +02001194 }
Christian Königf8991ba2016-09-16 15:36:49 +02001195
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001197
1198error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001199 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001200 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201}
1202
Christian König194d2162016-10-12 15:13:52 +02001203/*
Christian König92456b92017-05-12 16:09:26 +02001204 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1205 *
1206 * @parent: parent PD
1207 *
1208 * Mark all PD level as invalid after an error.
1209 */
Christian Königea097292017-08-09 14:15:46 +02001210static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
1211 struct amdgpu_vm_pt *parent)
Christian König92456b92017-05-12 16:09:26 +02001212{
1213 unsigned pt_idx;
1214
1215 /*
1216 * Recurse into the subdirectories. This recursion is harmless because
1217 * we only have a maximum of 5 layers.
1218 */
1219 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1220 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1221
Christian König3f3333f2017-08-03 14:02:13 +02001222 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001223 continue;
1224
1225 entry->addr = ~0ULL;
Christian Königea097292017-08-09 14:15:46 +02001226 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001227 if (list_empty(&entry->base.vm_status))
1228 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +02001229 spin_unlock(&vm->status_lock);
1230 amdgpu_vm_invalidate_level(vm, entry);
Christian König92456b92017-05-12 16:09:26 +02001231 }
1232}
1233
1234/*
Christian König194d2162016-10-12 15:13:52 +02001235 * amdgpu_vm_update_directories - make sure that all directories are valid
1236 *
1237 * @adev: amdgpu_device pointer
1238 * @vm: requested vm
1239 *
1240 * Makes sure all directories are up to date.
1241 * Returns 0 for success, error for failure.
1242 */
1243int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1244 struct amdgpu_vm *vm)
1245{
Christian König92456b92017-05-12 16:09:26 +02001246 int r;
1247
Christian Königea097292017-08-09 14:15:46 +02001248 spin_lock(&vm->status_lock);
1249 while (!list_empty(&vm->relocated)) {
1250 struct amdgpu_vm_bo_base *bo_base;
1251 struct amdgpu_bo *bo;
1252
1253 bo_base = list_first_entry(&vm->relocated,
1254 struct amdgpu_vm_bo_base,
1255 vm_status);
1256 spin_unlock(&vm->status_lock);
1257
1258 bo = bo_base->bo->parent;
1259 if (bo) {
1260 struct amdgpu_vm_bo_base *parent;
1261 struct amdgpu_vm_pt *pt;
1262
1263 parent = list_first_entry(&bo->va,
1264 struct amdgpu_vm_bo_base,
1265 bo_list);
1266 pt = container_of(parent, struct amdgpu_vm_pt, base);
1267
1268 r = amdgpu_vm_update_level(adev, vm, pt);
1269 if (r) {
1270 amdgpu_vm_invalidate_level(vm, &vm->root);
1271 return r;
1272 }
1273 spin_lock(&vm->status_lock);
1274 } else {
1275 spin_lock(&vm->status_lock);
1276 list_del_init(&bo_base->vm_status);
1277 }
1278 }
1279 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +02001280
Christian König68c62302017-07-11 17:23:29 +02001281 if (vm->use_cpu_for_update) {
1282 /* Flush HDP */
1283 mb();
1284 amdgpu_gart_flush_gpu_tlb(adev, 0);
1285 }
1286
Christian König92456b92017-05-12 16:09:26 +02001287 return r;
Christian König194d2162016-10-12 15:13:52 +02001288}
1289
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001291 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001292 *
1293 * @p: see amdgpu_pte_update_params definition
1294 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001295 * @entry: resulting entry or NULL
1296 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001297 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001298 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001299 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001300void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1301 struct amdgpu_vm_pt **entry,
1302 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001303{
Christian König4e2cb642016-10-25 15:52:28 +02001304 unsigned idx, level = p->adev->vm_manager.num_level;
1305
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001306 *parent = NULL;
1307 *entry = &p->vm->root;
1308 while ((*entry)->entries) {
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001309 idx = addr >> (p->adev->vm_manager.block_size * level--);
Christian König3f3333f2017-08-03 14:02:13 +02001310 idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001311 *parent = *entry;
1312 *entry = &(*entry)->entries[idx];
Christian König4e2cb642016-10-25 15:52:28 +02001313 }
1314
1315 if (level)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001316 *entry = NULL;
1317}
Christian König4e2cb642016-10-25 15:52:28 +02001318
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001319/**
1320 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1321 *
1322 * @p: see amdgpu_pte_update_params definition
1323 * @entry: vm_pt entry to check
1324 * @parent: parent entry
1325 * @nptes: number of PTEs updated with this operation
1326 * @dst: destination address where the PTEs should point to
1327 * @flags: access flags fro the PTEs
1328 *
1329 * Check if we can update the PD with a huge page.
1330 */
Christian Königec5207c2017-08-03 19:24:06 +02001331static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1332 struct amdgpu_vm_pt *entry,
1333 struct amdgpu_vm_pt *parent,
1334 unsigned nptes, uint64_t dst,
1335 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001336{
1337 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1338 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001339
1340 /* In the case of a mixed PT the PDE must point to it*/
1341 if (p->adev->asic_type < CHIP_VEGA10 ||
1342 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehling38a87912017-08-17 16:37:49 -04001343 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001344 !(flags & AMDGPU_PTE_VALID)) {
1345
Christian König3f3333f2017-08-03 14:02:13 +02001346 dst = amdgpu_bo_gpu_offset(entry->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001347 dst = amdgpu_gart_get_vm_pde(p->adev, dst);
1348 flags = AMDGPU_PTE_VALID;
1349 } else {
Christian König4ab40162017-08-03 20:30:50 +02001350 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001351 flags |= AMDGPU_PDE_PTE;
1352 }
1353
Christian König4ab40162017-08-03 20:30:50 +02001354 if (entry->addr == (dst | flags))
Christian Königec5207c2017-08-03 19:24:06 +02001355 return;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001356
Christian König4ab40162017-08-03 20:30:50 +02001357 entry->addr = (dst | flags);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001358
1359 if (use_cpu_update) {
Felix Kuehling38a87912017-08-17 16:37:49 -04001360 /* In case a huge page is replaced with a system
1361 * memory mapping, p->pages_addr != NULL and
1362 * amdgpu_vm_cpu_set_ptes would try to translate dst
1363 * through amdgpu_vm_map_gart. But dst is already a
1364 * GPU address (of the page table). Disable
1365 * amdgpu_vm_map_gart temporarily.
1366 */
1367 dma_addr_t *tmp;
1368
1369 tmp = p->pages_addr;
1370 p->pages_addr = NULL;
1371
Christian König3f3333f2017-08-03 14:02:13 +02001372 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001373 pde = pd_addr + (entry - parent->entries) * 8;
1374 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehling38a87912017-08-17 16:37:49 -04001375
1376 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001377 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001378 if (parent->base.bo->shadow) {
1379 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001380 pde = pd_addr + (entry - parent->entries) * 8;
1381 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1382 }
Christian König3f3333f2017-08-03 14:02:13 +02001383 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001384 pde = pd_addr + (entry - parent->entries) * 8;
1385 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1386 }
Christian König4e2cb642016-10-25 15:52:28 +02001387}
1388
1389/**
Christian König92696dd2016-08-05 13:56:35 +02001390 * amdgpu_vm_update_ptes - make sure that page tables are valid
1391 *
1392 * @params: see amdgpu_pte_update_params definition
1393 * @vm: requested vm
1394 * @start: start of GPU address range
1395 * @end: end of GPU address range
1396 * @dst: destination address to map to, the next dst inside the function
1397 * @flags: mapping flags
1398 *
1399 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001400 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001401 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001402static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001403 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001404 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001405{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001406 struct amdgpu_device *adev = params->adev;
1407 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001408
Christian König301654a2017-05-16 14:30:27 +02001409 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001410 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001411 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001412 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001413
1414 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001415 for (addr = start; addr < end; addr += nptes,
1416 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1417 struct amdgpu_vm_pt *entry, *parent;
1418
1419 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1420 if (!entry)
1421 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001422
Christian König92696dd2016-08-05 13:56:35 +02001423 if ((addr & ~mask) == (end & ~mask))
1424 nptes = end - addr;
1425 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001426 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001427
Christian Königec5207c2017-08-03 19:24:06 +02001428 amdgpu_vm_handle_huge_pages(params, entry, parent,
1429 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001430 /* We don't need to update PTEs for huge pages */
1431 if (entry->addr & AMDGPU_PDE_PTE)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001432 continue;
1433
Christian König3f3333f2017-08-03 14:02:13 +02001434 pt = entry->base.bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001435 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001436 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001437 } else {
1438 if (pt->shadow) {
1439 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1440 pe_start += (addr & mask) * 8;
1441 params->func(params, pe_start, dst, nptes,
1442 AMDGPU_GPU_PAGE_SIZE, flags);
1443 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001444 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001445 }
Christian König92696dd2016-08-05 13:56:35 +02001446
Christian König301654a2017-05-16 14:30:27 +02001447 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001448 params->func(params, pe_start, dst, nptes,
1449 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001450 }
1451
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001452 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001453}
1454
1455/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001456 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1457 *
Christian König29efc4f2016-08-04 14:52:50 +02001458 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001459 * @vm: requested vm
1460 * @start: first PTE to handle
1461 * @end: last PTE to handle
1462 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001463 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001464 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001465 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001466static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001467 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001468 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001469{
1470 /**
1471 * The MC L1 TLB supports variable sized pages, based on a fragment
1472 * field in the PTE. When this field is set to a non-zero value, page
1473 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1474 * flags are considered valid for all PTEs within the fragment range
1475 * and corresponding mappings are assumed to be physically contiguous.
1476 *
1477 * The L1 TLB can store a single PTE for the whole fragment,
1478 * significantly increasing the space available for translation
1479 * caching. This leads to large improvements in throughput when the
1480 * TLB is under pressure.
1481 *
1482 * The L2 TLB distributes small and large fragments into two
1483 * asymmetric partitions. The large fragment cache is significantly
1484 * larger. Thus, we try to use large fragments wherever possible.
1485 * Userspace can support this by aligning virtual base address and
1486 * allocation size to the fragment size.
1487 */
Roger He6849d472017-08-30 13:01:19 +08001488 unsigned max_frag = params->adev->vm_manager.fragment_size;
1489 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001490
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001492 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001493 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494
Roger He6849d472017-08-30 13:01:19 +08001495 while (start != end) {
1496 uint64_t frag_flags, frag_end;
1497 unsigned frag;
1498
1499 /* This intentionally wraps around if no bit is set */
1500 frag = min((unsigned)ffs(start) - 1,
1501 (unsigned)fls64(end - start) - 1);
1502 if (frag >= max_frag) {
1503 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1504 frag_end = end & ~((1ULL << max_frag) - 1);
1505 } else {
1506 frag_flags = AMDGPU_PTE_FRAG(frag);
1507 frag_end = start + (1 << frag);
1508 }
1509
1510 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1511 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001512 if (r)
1513 return r;
Roger He6849d472017-08-30 13:01:19 +08001514
1515 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1516 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517 }
1518
Roger He6849d472017-08-30 13:01:19 +08001519 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001520}
1521
1522/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1524 *
1525 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001526 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001527 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001528 * @vm: requested vm
1529 * @start: start of mapped range
1530 * @last: last mapped entry
1531 * @flags: flags for the entries
1532 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001533 * @fence: optional resulting fence
1534 *
Christian Königa14faa62016-01-25 14:27:31 +01001535 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537 */
1538static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001539 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001540 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001542 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001543 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001544 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001545{
Christian König2d55e452016-02-08 17:37:38 +01001546 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001547 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001548 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001549 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001550 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001551 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 int r;
1553
Christian Königafef8b82016-08-12 13:29:18 +02001554 memset(&params, 0, sizeof(params));
1555 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001556 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001557
Christian Königa33cab72017-07-11 17:13:00 +02001558 /* sync to everything on unmapping */
1559 if (!(flags & AMDGPU_PTE_VALID))
1560 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1561
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001562 if (vm->use_cpu_for_update) {
1563 /* params.src is used as flag to indicate system Memory */
1564 if (pages_addr)
1565 params.src = ~0;
1566
1567 /* Wait for PT BOs to be free. PTs share the same resv. object
1568 * as the root PD BO
1569 */
Christian Königa33cab72017-07-11 17:13:00 +02001570 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001571 if (unlikely(r))
1572 return r;
1573
1574 params.func = amdgpu_vm_cpu_set_ptes;
1575 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001576 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1577 addr, flags);
1578 }
1579
Christian König2d55e452016-02-08 17:37:38 +01001580 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001581
Christian Königa14faa62016-01-25 14:27:31 +01001582 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583
1584 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001585 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001586 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001587 *
1588 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 */
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001590 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591
1592 /* padding, etc. */
1593 ndw = 64;
1594
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001595 /* one PDE write for each huge page */
1596 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1597
Christian König570144c2017-08-30 15:38:45 +02001598 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001599 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001600 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601
Christian Königb0456f92016-08-11 14:06:54 +02001602 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001603 ndw += nptes * 2;
1604
Christian Königafef8b82016-08-12 13:29:18 +02001605 params.func = amdgpu_vm_do_copy_ptes;
1606
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 } else {
1608 /* set page commands needed */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001609 ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001610
Roger He6849d472017-08-30 13:01:19 +08001611 /* extra commands for begin/end fragments */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001612 ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
1613 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001614
1615 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616 }
1617
Christian Königd71518b2016-02-01 12:20:25 +01001618 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1619 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001620 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001621
Christian König29efc4f2016-08-04 14:52:50 +02001622 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001623
Christian König570144c2017-08-30 15:38:45 +02001624 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001625 uint64_t *pte;
1626 unsigned i;
1627
1628 /* Put the PTEs at the end of the IB. */
1629 i = ndw - nptes * 2;
1630 pte= (uint64_t *)&(job->ibs->ptr[i]);
1631 params.src = job->ibs->gpu_addr + i * 4;
1632
1633 for (i = 0; i < nptes; ++i) {
1634 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1635 AMDGPU_GPU_PAGE_SIZE);
1636 pte[i] |= flags;
1637 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001638 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001639 }
1640
Christian König3cabaa52016-06-06 10:17:58 +02001641 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1642 if (r)
1643 goto error_free;
1644
Christian König3f3333f2017-08-03 14:02:13 +02001645 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001646 owner);
1647 if (r)
1648 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649
Christian König3f3333f2017-08-03 14:02:13 +02001650 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001651 if (r)
1652 goto error_free;
1653
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001654 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1655 if (r)
1656 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001657
Christian König29efc4f2016-08-04 14:52:50 +02001658 amdgpu_ring_pad_ib(ring, params.ib);
1659 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001660 r = amdgpu_job_submit(job, ring, &vm->entity,
1661 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001662 if (r)
1663 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001664
Christian König3f3333f2017-08-03 14:02:13 +02001665 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001666 dma_fence_put(*fence);
1667 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001668 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001669
1670error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001671 amdgpu_job_free(job);
Christian Königea097292017-08-09 14:15:46 +02001672 amdgpu_vm_invalidate_level(vm, &vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001673 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674}
1675
1676/**
Christian Königa14faa62016-01-25 14:27:31 +01001677 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1678 *
1679 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001680 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001681 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001682 * @vm: requested vm
1683 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001684 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001685 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001686 * @fence: optional resulting fence
1687 *
1688 * Split the mapping into smaller chunks so that each update fits
1689 * into a SDMA IB.
1690 * Returns 0 for success, -EINVAL for failure.
1691 */
1692static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001693 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001694 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001695 struct amdgpu_vm *vm,
1696 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001697 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001698 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001699 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001700{
Christian König570144c2017-08-30 15:38:45 +02001701 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001702 int r;
1703
1704 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1705 * but in case of something, we filter the flags in first place
1706 */
1707 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1708 flags &= ~AMDGPU_PTE_READABLE;
1709 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1710 flags &= ~AMDGPU_PTE_WRITEABLE;
1711
Alex Xie15b31c52017-03-03 16:47:11 -05001712 flags &= ~AMDGPU_PTE_EXECUTABLE;
1713 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1714
Alex Xieb0fd18b2017-03-03 16:49:39 -05001715 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1716 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1717
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001718 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1719 (adev->asic_type >= CHIP_VEGA10)) {
1720 flags |= AMDGPU_PTE_PRT;
1721 flags &= ~AMDGPU_PTE_VALID;
1722 }
1723
Christian Königa14faa62016-01-25 14:27:31 +01001724 trace_amdgpu_vm_bo_update(mapping);
1725
Christian König63e0ba42016-08-16 17:38:37 +02001726 pfn = mapping->offset >> PAGE_SHIFT;
1727 if (nodes) {
1728 while (pfn >= nodes->size) {
1729 pfn -= nodes->size;
1730 ++nodes;
1731 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001732 }
Christian Königa14faa62016-01-25 14:27:31 +01001733
Christian König63e0ba42016-08-16 17:38:37 +02001734 do {
1735 uint64_t max_entries;
1736 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001737
Christian König63e0ba42016-08-16 17:38:37 +02001738 if (nodes) {
1739 addr = nodes->start << PAGE_SHIFT;
1740 max_entries = (nodes->size - pfn) *
1741 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1742 } else {
1743 addr = 0;
1744 max_entries = S64_MAX;
1745 }
Christian Königa14faa62016-01-25 14:27:31 +01001746
Christian König63e0ba42016-08-16 17:38:37 +02001747 if (pages_addr) {
Christian Königfebb84a2017-08-22 12:50:46 +02001748 max_entries = min(max_entries, 16ull * 1024ull);
Christian König63e0ba42016-08-16 17:38:37 +02001749 addr = 0;
1750 } else if (flags & AMDGPU_PTE_VALID) {
1751 addr += adev->vm_manager.vram_base_offset;
1752 }
1753 addr += pfn << PAGE_SHIFT;
1754
Christian Königa9f87f62017-03-30 14:03:59 +02001755 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König570144c2017-08-30 15:38:45 +02001756 r = amdgpu_vm_bo_update_mapping(adev, exclusive, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001757 start, last, flags, addr,
1758 fence);
1759 if (r)
1760 return r;
1761
Christian König63e0ba42016-08-16 17:38:37 +02001762 pfn += last - start + 1;
1763 if (nodes && nodes->size == pfn) {
1764 pfn = 0;
1765 ++nodes;
1766 }
Christian Königa14faa62016-01-25 14:27:31 +01001767 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001768
Christian Königa9f87f62017-03-30 14:03:59 +02001769 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001770
1771 return 0;
1772}
1773
1774/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1776 *
1777 * @adev: amdgpu_device pointer
1778 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001779 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780 *
1781 * Fill in the page table entries for @bo_va.
1782 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 */
1784int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1785 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001786 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001787{
Christian Königec681542017-08-01 10:51:43 +02001788 struct amdgpu_bo *bo = bo_va->base.bo;
1789 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001790 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001791 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001792 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001793 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001794 struct dma_fence *exclusive, **last_update;
Christian Königfebb84a2017-08-22 12:50:46 +02001795 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001796 int r;
1797
Christian Königec681542017-08-01 10:51:43 +02001798 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001799 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001800 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001801 exclusive = NULL;
1802 } else {
Christian König8358dce2016-03-30 10:50:25 +02001803 struct ttm_dma_tt *ttm;
1804
Christian Königec681542017-08-01 10:51:43 +02001805 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001806 nodes = mem->mm_node;
1807 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001808 ttm = container_of(bo_va->base.bo->tbo.ttm,
1809 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001810 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001811 }
Christian Königec681542017-08-01 10:51:43 +02001812 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813 }
1814
Christian Königfebb84a2017-08-22 12:50:46 +02001815 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001816 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian Königfebb84a2017-08-22 12:50:46 +02001817 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001818 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819
Christian König4e55eb32017-09-11 16:54:59 +02001820 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1821 last_update = &vm->last_update;
1822 else
1823 last_update = &bo_va->last_pt_update;
1824
Christian König3d7d4d32017-08-23 16:13:33 +02001825 if (!clear && bo_va->base.moved) {
1826 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001827 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001828
Christian Königcb7b6ec2017-08-15 17:08:12 +02001829 } else if (bo_va->cleared != clear) {
1830 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001831 }
Christian König7fc11952015-07-30 11:53:42 +02001832
1833 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königfebb84a2017-08-22 12:50:46 +02001834 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001835 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001836 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001837 if (r)
1838 return r;
1839 }
1840
Christian König68c62302017-07-11 17:23:29 +02001841 if (vm->use_cpu_for_update) {
1842 /* Flush HDP */
1843 mb();
1844 amdgpu_gart_flush_gpu_tlb(adev, 0);
1845 }
1846
Christian Königcb7b6ec2017-08-15 17:08:12 +02001847 spin_lock(&vm->status_lock);
1848 list_del_init(&bo_va->base.vm_status);
1849 spin_unlock(&vm->status_lock);
1850
1851 list_splice_init(&bo_va->invalids, &bo_va->valids);
1852 bo_va->cleared = clear;
1853
1854 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1855 list_for_each_entry(mapping, &bo_va->valids, list)
1856 trace_amdgpu_vm_bo_mapping(mapping);
1857 }
1858
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001859 return 0;
1860}
1861
1862/**
Christian König284710f2017-01-30 11:09:31 +01001863 * amdgpu_vm_update_prt_state - update the global PRT state
1864 */
1865static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1866{
1867 unsigned long flags;
1868 bool enable;
1869
1870 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001871 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001872 adev->gart.gart_funcs->set_prt(adev, enable);
1873 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1874}
1875
1876/**
Christian König4388fc22017-03-13 10:13:36 +01001877 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001878 */
1879static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1880{
Christian König4388fc22017-03-13 10:13:36 +01001881 if (!adev->gart.gart_funcs->set_prt)
1882 return;
1883
Christian König451bc8e2017-02-14 16:02:52 +01001884 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1885 amdgpu_vm_update_prt_state(adev);
1886}
1887
1888/**
Christian König0b15f2f2017-02-14 15:47:03 +01001889 * amdgpu_vm_prt_put - drop a PRT user
1890 */
1891static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1892{
Christian König451bc8e2017-02-14 16:02:52 +01001893 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001894 amdgpu_vm_update_prt_state(adev);
1895}
1896
1897/**
Christian König451bc8e2017-02-14 16:02:52 +01001898 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001899 */
1900static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1901{
1902 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1903
Christian König0b15f2f2017-02-14 15:47:03 +01001904 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001905 kfree(cb);
1906}
1907
1908/**
Christian König451bc8e2017-02-14 16:02:52 +01001909 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1910 */
1911static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1912 struct dma_fence *fence)
1913{
Christian König4388fc22017-03-13 10:13:36 +01001914 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001915
Christian König4388fc22017-03-13 10:13:36 +01001916 if (!adev->gart.gart_funcs->set_prt)
1917 return;
1918
1919 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001920 if (!cb) {
1921 /* Last resort when we are OOM */
1922 if (fence)
1923 dma_fence_wait(fence, false);
1924
Dan Carpenter486a68f2017-04-03 21:41:39 +03001925 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001926 } else {
1927 cb->adev = adev;
1928 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1929 amdgpu_vm_prt_cb))
1930 amdgpu_vm_prt_cb(fence, &cb->cb);
1931 }
1932}
1933
1934/**
Christian König284710f2017-01-30 11:09:31 +01001935 * amdgpu_vm_free_mapping - free a mapping
1936 *
1937 * @adev: amdgpu_device pointer
1938 * @vm: requested vm
1939 * @mapping: mapping to be freed
1940 * @fence: fence of the unmap operation
1941 *
1942 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1943 */
1944static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1945 struct amdgpu_vm *vm,
1946 struct amdgpu_bo_va_mapping *mapping,
1947 struct dma_fence *fence)
1948{
Christian König451bc8e2017-02-14 16:02:52 +01001949 if (mapping->flags & AMDGPU_PTE_PRT)
1950 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001951 kfree(mapping);
1952}
1953
1954/**
Christian König451bc8e2017-02-14 16:02:52 +01001955 * amdgpu_vm_prt_fini - finish all prt mappings
1956 *
1957 * @adev: amdgpu_device pointer
1958 * @vm: requested vm
1959 *
1960 * Register a cleanup callback to disable PRT support after VM dies.
1961 */
1962static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1963{
Christian König3f3333f2017-08-03 14:02:13 +02001964 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001965 struct dma_fence *excl, **shared;
1966 unsigned i, shared_count;
1967 int r;
1968
1969 r = reservation_object_get_fences_rcu(resv, &excl,
1970 &shared_count, &shared);
1971 if (r) {
1972 /* Not enough memory to grab the fence list, as last resort
1973 * block for all the fences to complete.
1974 */
1975 reservation_object_wait_timeout_rcu(resv, true, false,
1976 MAX_SCHEDULE_TIMEOUT);
1977 return;
1978 }
1979
1980 /* Add a callback for each fence in the reservation object */
1981 amdgpu_vm_prt_get(adev);
1982 amdgpu_vm_add_prt_cb(adev, excl);
1983
1984 for (i = 0; i < shared_count; ++i) {
1985 amdgpu_vm_prt_get(adev);
1986 amdgpu_vm_add_prt_cb(adev, shared[i]);
1987 }
1988
1989 kfree(shared);
1990}
1991
1992/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001993 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1994 *
1995 * @adev: amdgpu_device pointer
1996 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001997 * @fence: optional resulting fence (unchanged if no work needed to be done
1998 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001999 *
2000 * Make sure all freed BOs are cleared in the PT.
2001 * Returns 0 for success.
2002 *
2003 * PTs have to be reserved and mutex must be locked!
2004 */
2005int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002006 struct amdgpu_vm *vm,
2007 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002008{
2009 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002010 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002011 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002012 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013
2014 while (!list_empty(&vm->freed)) {
2015 mapping = list_first_entry(&vm->freed,
2016 struct amdgpu_bo_va_mapping, list);
2017 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01002018
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002019 if (vm->pte_support_ats)
2020 init_pte_value = AMDGPU_PTE_SYSTEM;
2021
Christian König570144c2017-08-30 15:38:45 +02002022 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02002023 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002024 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002025 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01002026 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002027 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028 return r;
Christian König284710f2017-01-30 11:09:31 +01002029 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002031
2032 if (fence && f) {
2033 dma_fence_put(*fence);
2034 *fence = f;
2035 } else {
2036 dma_fence_put(f);
2037 }
2038
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002039 return 0;
2040
2041}
2042
2043/**
Christian König73fb16e2017-08-16 11:13:48 +02002044 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002045 *
2046 * @adev: amdgpu_device pointer
2047 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02002048 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049 *
Christian König73fb16e2017-08-16 11:13:48 +02002050 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002051 * Returns 0 for success.
2052 *
Christian König73fb16e2017-08-16 11:13:48 +02002053 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002054 */
Christian König73fb16e2017-08-16 11:13:48 +02002055int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02002056 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057{
Christian König73fb16e2017-08-16 11:13:48 +02002058 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02002059 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002060
2061 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002062 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02002063 struct amdgpu_bo_va *bo_va;
2064
Christian König27c7b9a2017-08-01 11:27:36 +02002065 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002066 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002067 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002068
Christian König73fb16e2017-08-16 11:13:48 +02002069 /* Per VM BOs never need to bo cleared in the page tables */
2070 clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
2071
2072 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 if (r)
2074 return r;
2075
2076 spin_lock(&vm->status_lock);
2077 }
2078 spin_unlock(&vm->status_lock);
2079
Christian König91e1a522015-07-06 22:06:40 +02002080 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081}
2082
2083/**
2084 * amdgpu_vm_bo_add - add a bo to a specific vm
2085 *
2086 * @adev: amdgpu_device pointer
2087 * @vm: requested vm
2088 * @bo: amdgpu buffer object
2089 *
Christian König8843dbb2016-01-26 12:17:11 +01002090 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091 * Add @bo to the list of bos associated with the vm
2092 * Returns newly added bo_va or NULL for failure
2093 *
2094 * Object has to be reserved!
2095 */
2096struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2097 struct amdgpu_vm *vm,
2098 struct amdgpu_bo *bo)
2099{
2100 struct amdgpu_bo_va *bo_va;
2101
2102 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2103 if (bo_va == NULL) {
2104 return NULL;
2105 }
Christian Königec681542017-08-01 10:51:43 +02002106 bo_va->base.vm = vm;
2107 bo_va->base.bo = bo;
2108 INIT_LIST_HEAD(&bo_va->base.bo_list);
2109 INIT_LIST_HEAD(&bo_va->base.vm_status);
2110
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002112 INIT_LIST_HEAD(&bo_va->valids);
2113 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002114
Christian Königa5f6b5b2017-01-30 11:01:38 +01002115 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002116 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002117
2118 return bo_va;
2119}
2120
Christian König73fb16e2017-08-16 11:13:48 +02002121
2122/**
2123 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2124 *
2125 * @adev: amdgpu_device pointer
2126 * @bo_va: bo_va to store the address
2127 * @mapping: the mapping to insert
2128 *
2129 * Insert a new mapping into all structures.
2130 */
2131static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2132 struct amdgpu_bo_va *bo_va,
2133 struct amdgpu_bo_va_mapping *mapping)
2134{
2135 struct amdgpu_vm *vm = bo_va->base.vm;
2136 struct amdgpu_bo *bo = bo_va->base.bo;
2137
Christian Königaebc5e62017-09-06 16:55:16 +02002138 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002139 list_add(&mapping->list, &bo_va->invalids);
2140 amdgpu_vm_it_insert(mapping, &vm->va);
2141
2142 if (mapping->flags & AMDGPU_PTE_PRT)
2143 amdgpu_vm_prt_get(adev);
2144
2145 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2146 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002147 if (list_empty(&bo_va->base.vm_status))
2148 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02002149 spin_unlock(&vm->status_lock);
2150 }
2151 trace_amdgpu_vm_bo_map(bo_va, mapping);
2152}
2153
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002154/**
2155 * amdgpu_vm_bo_map - map bo inside a vm
2156 *
2157 * @adev: amdgpu_device pointer
2158 * @bo_va: bo_va to store the address
2159 * @saddr: where to map the BO
2160 * @offset: requested offset in the BO
2161 * @flags: attributes of pages (read/write/valid/etc.)
2162 *
2163 * Add a mapping of the BO at the specefied addr into the VM.
2164 * Returns 0 for success, error for failure.
2165 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002166 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002167 */
2168int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2169 struct amdgpu_bo_va *bo_va,
2170 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002171 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002172{
Christian Königa9f87f62017-03-30 14:03:59 +02002173 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002174 struct amdgpu_bo *bo = bo_va->base.bo;
2175 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002176 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177
Christian König0be52de2015-05-18 14:37:27 +02002178 /* validate the parameters */
2179 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002180 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002181 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002182
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002184 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002185 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002186 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002189 saddr /= AMDGPU_GPU_PAGE_SIZE;
2190 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2191
Christian Königa9f87f62017-03-30 14:03:59 +02002192 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2193 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002194 /* bo and tmp overlap, invalid addr */
2195 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002196 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002197 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002198 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002199 }
2200
2201 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002202 if (!mapping)
2203 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002204
Christian Königa9f87f62017-03-30 14:03:59 +02002205 mapping->start = saddr;
2206 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 mapping->offset = offset;
2208 mapping->flags = flags;
2209
Christian König73fb16e2017-08-16 11:13:48 +02002210 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002211
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213}
2214
2215/**
Christian König80f95c52017-03-13 10:13:39 +01002216 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2217 *
2218 * @adev: amdgpu_device pointer
2219 * @bo_va: bo_va to store the address
2220 * @saddr: where to map the BO
2221 * @offset: requested offset in the BO
2222 * @flags: attributes of pages (read/write/valid/etc.)
2223 *
2224 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2225 * mappings as we do so.
2226 * Returns 0 for success, error for failure.
2227 *
2228 * Object has to be reserved and unreserved outside!
2229 */
2230int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2231 struct amdgpu_bo_va *bo_va,
2232 uint64_t saddr, uint64_t offset,
2233 uint64_t size, uint64_t flags)
2234{
2235 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002236 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002237 uint64_t eaddr;
2238 int r;
2239
2240 /* validate the parameters */
2241 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2242 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2243 return -EINVAL;
2244
2245 /* make sure object fit at this offset */
2246 eaddr = saddr + size - 1;
2247 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002248 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002249 return -EINVAL;
2250
2251 /* Allocate all the needed memory */
2252 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2253 if (!mapping)
2254 return -ENOMEM;
2255
Christian Königec681542017-08-01 10:51:43 +02002256 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002257 if (r) {
2258 kfree(mapping);
2259 return r;
2260 }
2261
2262 saddr /= AMDGPU_GPU_PAGE_SIZE;
2263 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2264
Christian Königa9f87f62017-03-30 14:03:59 +02002265 mapping->start = saddr;
2266 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002267 mapping->offset = offset;
2268 mapping->flags = flags;
2269
Christian König73fb16e2017-08-16 11:13:48 +02002270 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002271
2272 return 0;
2273}
2274
2275/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002276 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2277 *
2278 * @adev: amdgpu_device pointer
2279 * @bo_va: bo_va to remove the address from
2280 * @saddr: where to the BO is mapped
2281 *
2282 * Remove a mapping of the BO at the specefied addr from the VM.
2283 * Returns 0 for success, error for failure.
2284 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002285 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002286 */
2287int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2288 struct amdgpu_bo_va *bo_va,
2289 uint64_t saddr)
2290{
2291 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002292 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002293 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002294
Christian König6c7fc502015-06-05 20:56:17 +02002295 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002296
Christian König7fc11952015-07-30 11:53:42 +02002297 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002298 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002299 break;
2300 }
2301
Christian König7fc11952015-07-30 11:53:42 +02002302 if (&mapping->list == &bo_va->valids) {
2303 valid = false;
2304
2305 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002306 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002307 break;
2308 }
2309
Christian König32b41ac2016-03-08 18:03:27 +01002310 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002311 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312 }
Christian König32b41ac2016-03-08 18:03:27 +01002313
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002315 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002316 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002317 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002318
Christian Könige17841b2016-03-08 17:52:01 +01002319 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002320 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002321 else
Christian König284710f2017-01-30 11:09:31 +01002322 amdgpu_vm_free_mapping(adev, vm, mapping,
2323 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324
2325 return 0;
2326}
2327
2328/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002329 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2330 *
2331 * @adev: amdgpu_device pointer
2332 * @vm: VM structure to use
2333 * @saddr: start of the range
2334 * @size: size of the range
2335 *
2336 * Remove all mappings in a range, split them as appropriate.
2337 * Returns 0 for success, error for failure.
2338 */
2339int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2340 struct amdgpu_vm *vm,
2341 uint64_t saddr, uint64_t size)
2342{
2343 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002344 LIST_HEAD(removed);
2345 uint64_t eaddr;
2346
2347 eaddr = saddr + size - 1;
2348 saddr /= AMDGPU_GPU_PAGE_SIZE;
2349 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2350
2351 /* Allocate all the needed memory */
2352 before = kzalloc(sizeof(*before), GFP_KERNEL);
2353 if (!before)
2354 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002355 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002356
2357 after = kzalloc(sizeof(*after), GFP_KERNEL);
2358 if (!after) {
2359 kfree(before);
2360 return -ENOMEM;
2361 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002362 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002363
2364 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002365 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2366 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002367 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002368 if (tmp->start < saddr) {
2369 before->start = tmp->start;
2370 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002371 before->offset = tmp->offset;
2372 before->flags = tmp->flags;
2373 list_add(&before->list, &tmp->list);
2374 }
2375
2376 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002377 if (tmp->last > eaddr) {
2378 after->start = eaddr + 1;
2379 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002380 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002381 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002382 after->flags = tmp->flags;
2383 list_add(&after->list, &tmp->list);
2384 }
2385
2386 list_del(&tmp->list);
2387 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002388
2389 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002390 }
2391
2392 /* And free them up */
2393 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002394 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002395 list_del(&tmp->list);
2396
Christian Königa9f87f62017-03-30 14:03:59 +02002397 if (tmp->start < saddr)
2398 tmp->start = saddr;
2399 if (tmp->last > eaddr)
2400 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002401
Christian Königaebc5e62017-09-06 16:55:16 +02002402 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002403 list_add(&tmp->list, &vm->freed);
2404 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2405 }
2406
Junwei Zhang27f6d612017-03-16 16:09:24 +08002407 /* Insert partial mapping before the range */
2408 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002409 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002410 if (before->flags & AMDGPU_PTE_PRT)
2411 amdgpu_vm_prt_get(adev);
2412 } else {
2413 kfree(before);
2414 }
2415
2416 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002417 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002418 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002419 if (after->flags & AMDGPU_PTE_PRT)
2420 amdgpu_vm_prt_get(adev);
2421 } else {
2422 kfree(after);
2423 }
2424
2425 return 0;
2426}
2427
2428/**
Christian Königaebc5e62017-09-06 16:55:16 +02002429 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2430 *
2431 * @vm: the requested VM
2432 *
2433 * Find a mapping by it's address.
2434 */
2435struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2436 uint64_t addr)
2437{
2438 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2439}
2440
2441/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002442 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2443 *
2444 * @adev: amdgpu_device pointer
2445 * @bo_va: requested bo_va
2446 *
Christian König8843dbb2016-01-26 12:17:11 +01002447 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002448 *
2449 * Object have to be reserved!
2450 */
2451void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2452 struct amdgpu_bo_va *bo_va)
2453{
2454 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002455 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002456
Christian Königec681542017-08-01 10:51:43 +02002457 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002458
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002459 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002460 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002461 spin_unlock(&vm->status_lock);
2462
Christian König7fc11952015-07-30 11:53:42 +02002463 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002465 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002466 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002467 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002468 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002469 }
Christian König7fc11952015-07-30 11:53:42 +02002470 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2471 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002472 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002473 amdgpu_vm_free_mapping(adev, vm, mapping,
2474 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002475 }
Christian König32b41ac2016-03-08 18:03:27 +01002476
Chris Wilsonf54d1862016-10-25 13:00:45 +01002477 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002478 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479}
2480
2481/**
2482 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2483 *
2484 * @adev: amdgpu_device pointer
2485 * @vm: requested vm
2486 * @bo: amdgpu buffer object
2487 *
Christian König8843dbb2016-01-26 12:17:11 +01002488 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002489 */
2490void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002491 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002492{
Christian Königec681542017-08-01 10:51:43 +02002493 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002494
Christian Königec681542017-08-01 10:51:43 +02002495 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002496 struct amdgpu_vm *vm = bo_base->vm;
2497
Christian König3d7d4d32017-08-23 16:13:33 +02002498 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002499 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2500 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002501 if (bo->tbo.type == ttm_bo_type_kernel)
2502 list_move(&bo_base->vm_status, &vm->evicted);
2503 else
2504 list_move_tail(&bo_base->vm_status,
2505 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002506 spin_unlock(&bo_base->vm->status_lock);
2507 continue;
2508 }
2509
Christian Königea097292017-08-09 14:15:46 +02002510 if (bo->tbo.type == ttm_bo_type_kernel) {
2511 spin_lock(&bo_base->vm->status_lock);
2512 if (list_empty(&bo_base->vm_status))
2513 list_add(&bo_base->vm_status, &vm->relocated);
2514 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002515 continue;
Christian Königea097292017-08-09 14:15:46 +02002516 }
Christian König3f3333f2017-08-03 14:02:13 +02002517
Christian Königec681542017-08-01 10:51:43 +02002518 spin_lock(&bo_base->vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002519 if (list_empty(&bo_base->vm_status))
2520 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002521 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002522 }
2523}
2524
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002525static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2526{
2527 /* Total bits covered by PD + PTs */
2528 unsigned bits = ilog2(vm_size) + 18;
2529
2530 /* Make sure the PD is 4K in size up to 8GB address space.
2531 Above that split equal between PD and PTs */
2532 if (vm_size <= 8)
2533 return (bits - 9);
2534 else
2535 return ((bits + 3) / 2);
2536}
2537
2538/**
Roger Hed07f14b2017-08-15 16:05:59 +08002539 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2540 *
2541 * @adev: amdgpu_device pointer
2542 * @fragment_size_default: the default fragment size if it's set auto
2543 */
Christian Königc38e0692017-09-18 14:01:45 +02002544void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,
2545 uint32_t fragment_size_default)
Roger Hed07f14b2017-08-15 16:05:59 +08002546{
2547 if (amdgpu_vm_fragment_size == -1)
2548 adev->vm_manager.fragment_size = fragment_size_default;
2549 else
2550 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2551}
2552
2553/**
2554 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002555 *
2556 * @adev: amdgpu_device pointer
2557 * @vm_size: the default vm size if it's set auto
2558 */
Christian Königc38e0692017-09-18 14:01:45 +02002559void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size,
2560 uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002561{
2562 /* adjust vm size firstly */
2563 if (amdgpu_vm_size == -1)
2564 adev->vm_manager.vm_size = vm_size;
2565 else
2566 adev->vm_manager.vm_size = amdgpu_vm_size;
2567
2568 /* block size depends on vm size */
2569 if (amdgpu_vm_block_size == -1)
2570 adev->vm_manager.block_size =
2571 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2572 else
2573 adev->vm_manager.block_size = amdgpu_vm_block_size;
2574
Roger Hed07f14b2017-08-15 16:05:59 +08002575 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2576
2577 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2578 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2579 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002580}
2581
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002582/**
2583 * amdgpu_vm_init - initialize a vm instance
2584 *
2585 * @adev: amdgpu_device pointer
2586 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002587 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588 *
Christian König8843dbb2016-01-26 12:17:11 +01002589 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002590 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002591int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002592 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002593{
2594 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002595 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002596 unsigned ring_instance;
2597 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002598 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002599 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002600 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002601 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002602
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002603 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002604 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002605 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2606 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002607 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002608 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002609 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002610 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002612
Christian König2bd9ccf2016-02-01 12:53:58 +01002613 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002614
2615 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2616 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2617 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002618 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2619 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2620 rq, amdgpu_sched_jobs);
2621 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002622 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002623
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002624 vm->pte_support_ats = false;
2625
2626 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002627 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2628 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002629
2630 if (adev->asic_type == CHIP_RAVEN) {
2631 vm->pte_support_ats = true;
2632 init_pde_value = AMDGPU_PTE_SYSTEM | AMDGPU_PDE_PTE;
2633 }
2634 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002635 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2636 AMDGPU_VM_USE_CPU_FOR_GFX);
2637 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2638 vm->use_cpu_for_update ? "CPU" : "SDMA");
2639 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2640 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002641 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002642
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002643 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2644 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2645 if (vm->use_cpu_for_update)
2646 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2647 else
2648 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2649 AMDGPU_GEM_CREATE_SHADOW);
2650
Christian Königf566ceb2016-10-27 20:04:38 +02002651 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002652 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002653 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002654 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002655 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002656 goto error_free_sched_entity;
2657
Christian König3f3333f2017-08-03 14:02:13 +02002658 vm->root.base.vm = vm;
2659 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2660 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002661
2662 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002663 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002664 if (r)
2665 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002666
Christian König3f3333f2017-08-03 14:02:13 +02002667 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
Felix Kuehlingca290da2017-08-25 20:15:04 -04002668 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König3f3333f2017-08-03 14:02:13 +02002669 if (r)
2670 goto error_free_root;
Christian König3f3333f2017-08-03 14:02:13 +02002671 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002672
Felix Kuehling02208442017-08-25 20:40:26 -04002673 if (pasid) {
2674 unsigned long flags;
2675
2676 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2677 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2678 GFP_ATOMIC);
2679 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2680 if (r < 0)
2681 goto error_free_root;
2682
2683 vm->pasid = pasid;
2684 }
2685
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002686 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002687 vm->fault_credit = 16;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002688
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002689 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002690
Christian König67003a12016-10-12 14:46:26 +02002691error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002692 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2693 amdgpu_bo_unref(&vm->root.base.bo);
2694 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002695
2696error_free_sched_entity:
2697 amd_sched_entity_fini(&ring->sched, &vm->entity);
2698
2699 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002700}
2701
2702/**
Christian Königf566ceb2016-10-27 20:04:38 +02002703 * amdgpu_vm_free_levels - free PD/PT levels
2704 *
2705 * @level: PD/PT starting level to free
2706 *
2707 * Free the page directory or page table level and all sub levels.
2708 */
2709static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2710{
2711 unsigned i;
2712
Christian König3f3333f2017-08-03 14:02:13 +02002713 if (level->base.bo) {
2714 list_del(&level->base.bo_list);
2715 list_del(&level->base.vm_status);
2716 amdgpu_bo_unref(&level->base.bo->shadow);
2717 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002718 }
2719
2720 if (level->entries)
2721 for (i = 0; i <= level->last_entry_used; i++)
2722 amdgpu_vm_free_levels(&level->entries[i]);
2723
Michal Hocko20981052017-05-17 14:23:12 +02002724 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002725}
2726
2727/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002728 * amdgpu_vm_fini - tear down a vm instance
2729 *
2730 * @adev: amdgpu_device pointer
2731 * @vm: requested vm
2732 *
Christian König8843dbb2016-01-26 12:17:11 +01002733 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002734 * Unbind the VM and remove all bos from the vm bo list
2735 */
2736void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2737{
2738 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002739 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002740 u64 fault;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002741 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002742
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002743 /* Clear pending page faults from IH when the VM is destroyed */
2744 while (kfifo_get(&vm->faults, &fault))
2745 amdgpu_ih_clear_fault(adev, fault);
2746
Felix Kuehling02208442017-08-25 20:40:26 -04002747 if (vm->pasid) {
2748 unsigned long flags;
2749
2750 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2751 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2752 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2753 }
2754
Christian König2d55e452016-02-08 17:37:38 +01002755 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002756
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002757 if (!RB_EMPTY_ROOT(&vm->va)) {
2758 dev_err(adev->dev, "still active bo inside vm\n");
2759 }
Christian Königa9f87f62017-03-30 14:03:59 +02002760 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002761 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002762 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002763 kfree(mapping);
2764 }
2765 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002766 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002767 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002768 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002769 }
Christian König284710f2017-01-30 11:09:31 +01002770
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002771 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002772 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002773 }
2774
Christian Königf566ceb2016-10-27 20:04:38 +02002775 amdgpu_vm_free_levels(&vm->root);
Christian Königd5884512017-09-08 14:09:41 +02002776 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002777 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2778 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002779}
Christian Königea89f8c2015-11-15 20:52:06 +01002780
2781/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002782 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2783 *
2784 * @adev: amdgpu_device pointer
2785 * @pasid: PASID do identify the VM
2786 *
2787 * This function is expected to be called in interrupt context. Returns
2788 * true if there was fault credit, false otherwise
2789 */
2790bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2791 unsigned int pasid)
2792{
2793 struct amdgpu_vm *vm;
2794
2795 spin_lock(&adev->vm_manager.pasid_lock);
2796 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2797 spin_unlock(&adev->vm_manager.pasid_lock);
2798 if (!vm)
2799 /* VM not found, can't track fault credit */
2800 return true;
2801
2802 /* No lock needed. only accessed by IRQ handler */
2803 if (!vm->fault_credit)
2804 /* Too many faults in this VM */
2805 return false;
2806
2807 vm->fault_credit--;
2808 return true;
2809}
2810
2811/**
Christian Königa9a78b32016-01-21 10:19:11 +01002812 * amdgpu_vm_manager_init - init the VM manager
2813 *
2814 * @adev: amdgpu_device pointer
2815 *
2816 * Initialize the VM manager structures
2817 */
2818void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2819{
Christian König76456702017-04-06 17:52:39 +02002820 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002821
Christian König76456702017-04-06 17:52:39 +02002822 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2823 struct amdgpu_vm_id_manager *id_mgr =
2824 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002825
Christian König76456702017-04-06 17:52:39 +02002826 mutex_init(&id_mgr->lock);
2827 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002828 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002829
2830 /* skip over VMID 0, since it is the system VM */
2831 for (j = 1; j < id_mgr->num_ids; ++j) {
2832 amdgpu_vm_reset_id(adev, i, j);
2833 amdgpu_sync_create(&id_mgr->ids[i].active);
2834 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2835 }
Christian König971fe9a92016-03-01 15:09:25 +01002836 }
Christian König2d55e452016-02-08 17:37:38 +01002837
Chris Wilsonf54d1862016-10-25 13:00:45 +01002838 adev->vm_manager.fence_context =
2839 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002840 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2841 adev->vm_manager.seqno[i] = 0;
2842
Christian König2d55e452016-02-08 17:37:38 +01002843 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002844 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002845 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002846 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002847
2848 /* If not overridden by the user, by default, only in large BAR systems
2849 * Compute VM tables will be updated by CPU
2850 */
2851#ifdef CONFIG_X86_64
2852 if (amdgpu_vm_update_mode == -1) {
2853 if (amdgpu_vm_is_large_bar(adev))
2854 adev->vm_manager.vm_update_mode =
2855 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2856 else
2857 adev->vm_manager.vm_update_mode = 0;
2858 } else
2859 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2860#else
2861 adev->vm_manager.vm_update_mode = 0;
2862#endif
2863
Felix Kuehling02208442017-08-25 20:40:26 -04002864 idr_init(&adev->vm_manager.pasid_idr);
2865 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002866}
2867
2868/**
Christian Königea89f8c2015-11-15 20:52:06 +01002869 * amdgpu_vm_manager_fini - cleanup VM manager
2870 *
2871 * @adev: amdgpu_device pointer
2872 *
2873 * Cleanup the VM manager and free resources.
2874 */
2875void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2876{
Christian König76456702017-04-06 17:52:39 +02002877 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002878
Felix Kuehling02208442017-08-25 20:40:26 -04002879 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2880 idr_destroy(&adev->vm_manager.pasid_idr);
2881
Christian König76456702017-04-06 17:52:39 +02002882 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2883 struct amdgpu_vm_id_manager *id_mgr =
2884 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002885
Christian König76456702017-04-06 17:52:39 +02002886 mutex_destroy(&id_mgr->lock);
2887 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2888 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2889
2890 amdgpu_sync_free(&id->active);
2891 dma_fence_put(id->flushed_updates);
2892 dma_fence_put(id->last_flush);
2893 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002894 }
Christian Königea89f8c2015-11-15 20:52:06 +01002895}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002896
2897int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2898{
2899 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002900 struct amdgpu_device *adev = dev->dev_private;
2901 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2902 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002903
2904 switch (args->in.op) {
2905 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002906 /* current, we only have requirement to reserve vmid from gfxhub */
2907 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2908 AMDGPU_GFXHUB);
2909 if (r)
2910 return r;
2911 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002912 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002913 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002914 break;
2915 default:
2916 return -EINVAL;
2917 }
2918
2919 return 0;
2920}