blob: 9b795915cab1d731d9dbfad72191a88948b68118 [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 */
1600 ndw += ncmds * 7;
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 */
1609 ndw += ncmds * 10;
1610
Roger He6849d472017-08-30 13:01:19 +08001611 /* extra commands for begin/end fragments */
1612 ndw += 2 * 10 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001613
1614 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001615 }
1616
Christian Königd71518b2016-02-01 12:20:25 +01001617 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1618 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001620
Christian König29efc4f2016-08-04 14:52:50 +02001621 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001622
Christian König570144c2017-08-30 15:38:45 +02001623 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001624 uint64_t *pte;
1625 unsigned i;
1626
1627 /* Put the PTEs at the end of the IB. */
1628 i = ndw - nptes * 2;
1629 pte= (uint64_t *)&(job->ibs->ptr[i]);
1630 params.src = job->ibs->gpu_addr + i * 4;
1631
1632 for (i = 0; i < nptes; ++i) {
1633 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1634 AMDGPU_GPU_PAGE_SIZE);
1635 pte[i] |= flags;
1636 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001637 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001638 }
1639
Christian König3cabaa52016-06-06 10:17:58 +02001640 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1641 if (r)
1642 goto error_free;
1643
Christian König3f3333f2017-08-03 14:02:13 +02001644 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001645 owner);
1646 if (r)
1647 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001648
Christian König3f3333f2017-08-03 14:02:13 +02001649 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001650 if (r)
1651 goto error_free;
1652
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001653 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1654 if (r)
1655 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656
Christian König29efc4f2016-08-04 14:52:50 +02001657 amdgpu_ring_pad_ib(ring, params.ib);
1658 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001659 r = amdgpu_job_submit(job, ring, &vm->entity,
1660 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001661 if (r)
1662 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001663
Christian König3f3333f2017-08-03 14:02:13 +02001664 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001665 dma_fence_put(*fence);
1666 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001668
1669error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001670 amdgpu_job_free(job);
Christian Königea097292017-08-09 14:15:46 +02001671 amdgpu_vm_invalidate_level(vm, &vm->root);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001672 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001673}
1674
1675/**
Christian Königa14faa62016-01-25 14:27:31 +01001676 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1677 *
1678 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001679 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001680 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001681 * @vm: requested vm
1682 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001683 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001684 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001685 * @fence: optional resulting fence
1686 *
1687 * Split the mapping into smaller chunks so that each update fits
1688 * into a SDMA IB.
1689 * Returns 0 for success, -EINVAL for failure.
1690 */
1691static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001692 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001693 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001694 struct amdgpu_vm *vm,
1695 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001696 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001697 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001698 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001699{
Christian König570144c2017-08-30 15:38:45 +02001700 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001701 int r;
1702
1703 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1704 * but in case of something, we filter the flags in first place
1705 */
1706 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1707 flags &= ~AMDGPU_PTE_READABLE;
1708 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1709 flags &= ~AMDGPU_PTE_WRITEABLE;
1710
Alex Xie15b31c52017-03-03 16:47:11 -05001711 flags &= ~AMDGPU_PTE_EXECUTABLE;
1712 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1713
Alex Xieb0fd18b2017-03-03 16:49:39 -05001714 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1715 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1716
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001717 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1718 (adev->asic_type >= CHIP_VEGA10)) {
1719 flags |= AMDGPU_PTE_PRT;
1720 flags &= ~AMDGPU_PTE_VALID;
1721 }
1722
Christian Königa14faa62016-01-25 14:27:31 +01001723 trace_amdgpu_vm_bo_update(mapping);
1724
Christian König63e0ba42016-08-16 17:38:37 +02001725 pfn = mapping->offset >> PAGE_SHIFT;
1726 if (nodes) {
1727 while (pfn >= nodes->size) {
1728 pfn -= nodes->size;
1729 ++nodes;
1730 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001731 }
Christian Königa14faa62016-01-25 14:27:31 +01001732
Christian König63e0ba42016-08-16 17:38:37 +02001733 do {
1734 uint64_t max_entries;
1735 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001736
Christian König63e0ba42016-08-16 17:38:37 +02001737 if (nodes) {
1738 addr = nodes->start << PAGE_SHIFT;
1739 max_entries = (nodes->size - pfn) *
1740 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1741 } else {
1742 addr = 0;
1743 max_entries = S64_MAX;
1744 }
Christian Königa14faa62016-01-25 14:27:31 +01001745
Christian König63e0ba42016-08-16 17:38:37 +02001746 if (pages_addr) {
Christian Königfebb84a2017-08-22 12:50:46 +02001747 max_entries = min(max_entries, 16ull * 1024ull);
Christian König63e0ba42016-08-16 17:38:37 +02001748 addr = 0;
1749 } else if (flags & AMDGPU_PTE_VALID) {
1750 addr += adev->vm_manager.vram_base_offset;
1751 }
1752 addr += pfn << PAGE_SHIFT;
1753
Christian Königa9f87f62017-03-30 14:03:59 +02001754 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König570144c2017-08-30 15:38:45 +02001755 r = amdgpu_vm_bo_update_mapping(adev, exclusive, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001756 start, last, flags, addr,
1757 fence);
1758 if (r)
1759 return r;
1760
Christian König63e0ba42016-08-16 17:38:37 +02001761 pfn += last - start + 1;
1762 if (nodes && nodes->size == pfn) {
1763 pfn = 0;
1764 ++nodes;
1765 }
Christian Königa14faa62016-01-25 14:27:31 +01001766 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001767
Christian Königa9f87f62017-03-30 14:03:59 +02001768 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001769
1770 return 0;
1771}
1772
1773/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001774 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1775 *
1776 * @adev: amdgpu_device pointer
1777 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001778 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 *
1780 * Fill in the page table entries for @bo_va.
1781 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001782 */
1783int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1784 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001785 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001786{
Christian Königec681542017-08-01 10:51:43 +02001787 struct amdgpu_bo *bo = bo_va->base.bo;
1788 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001790 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001791 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001792 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001793 struct dma_fence *exclusive, **last_update;
Christian Königfebb84a2017-08-22 12:50:46 +02001794 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795 int r;
1796
Christian Königec681542017-08-01 10:51:43 +02001797 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001798 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001799 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001800 exclusive = NULL;
1801 } else {
Christian König8358dce2016-03-30 10:50:25 +02001802 struct ttm_dma_tt *ttm;
1803
Christian Königec681542017-08-01 10:51:43 +02001804 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001805 nodes = mem->mm_node;
1806 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001807 ttm = container_of(bo_va->base.bo->tbo.ttm,
1808 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001809 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001810 }
Christian Königec681542017-08-01 10:51:43 +02001811 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001812 }
1813
Christian Königfebb84a2017-08-22 12:50:46 +02001814 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001815 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian Königfebb84a2017-08-22 12:50:46 +02001816 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001817 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818
Christian König4e55eb32017-09-11 16:54:59 +02001819 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1820 last_update = &vm->last_update;
1821 else
1822 last_update = &bo_va->last_pt_update;
1823
Christian König3d7d4d32017-08-23 16:13:33 +02001824 if (!clear && bo_va->base.moved) {
1825 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001826 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001827
Christian Königcb7b6ec2017-08-15 17:08:12 +02001828 } else if (bo_va->cleared != clear) {
1829 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001830 }
Christian König7fc11952015-07-30 11:53:42 +02001831
1832 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königfebb84a2017-08-22 12:50:46 +02001833 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001834 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001835 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001836 if (r)
1837 return r;
1838 }
1839
Christian König68c62302017-07-11 17:23:29 +02001840 if (vm->use_cpu_for_update) {
1841 /* Flush HDP */
1842 mb();
1843 amdgpu_gart_flush_gpu_tlb(adev, 0);
1844 }
1845
Christian Königcb7b6ec2017-08-15 17:08:12 +02001846 spin_lock(&vm->status_lock);
1847 list_del_init(&bo_va->base.vm_status);
1848 spin_unlock(&vm->status_lock);
1849
1850 list_splice_init(&bo_va->invalids, &bo_va->valids);
1851 bo_va->cleared = clear;
1852
1853 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1854 list_for_each_entry(mapping, &bo_va->valids, list)
1855 trace_amdgpu_vm_bo_mapping(mapping);
1856 }
1857
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858 return 0;
1859}
1860
1861/**
Christian König284710f2017-01-30 11:09:31 +01001862 * amdgpu_vm_update_prt_state - update the global PRT state
1863 */
1864static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1865{
1866 unsigned long flags;
1867 bool enable;
1868
1869 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001870 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001871 adev->gart.gart_funcs->set_prt(adev, enable);
1872 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1873}
1874
1875/**
Christian König4388fc22017-03-13 10:13:36 +01001876 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001877 */
1878static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1879{
Christian König4388fc22017-03-13 10:13:36 +01001880 if (!adev->gart.gart_funcs->set_prt)
1881 return;
1882
Christian König451bc8e2017-02-14 16:02:52 +01001883 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1884 amdgpu_vm_update_prt_state(adev);
1885}
1886
1887/**
Christian König0b15f2f2017-02-14 15:47:03 +01001888 * amdgpu_vm_prt_put - drop a PRT user
1889 */
1890static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1891{
Christian König451bc8e2017-02-14 16:02:52 +01001892 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001893 amdgpu_vm_update_prt_state(adev);
1894}
1895
1896/**
Christian König451bc8e2017-02-14 16:02:52 +01001897 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001898 */
1899static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1900{
1901 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1902
Christian König0b15f2f2017-02-14 15:47:03 +01001903 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001904 kfree(cb);
1905}
1906
1907/**
Christian König451bc8e2017-02-14 16:02:52 +01001908 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1909 */
1910static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1911 struct dma_fence *fence)
1912{
Christian König4388fc22017-03-13 10:13:36 +01001913 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001914
Christian König4388fc22017-03-13 10:13:36 +01001915 if (!adev->gart.gart_funcs->set_prt)
1916 return;
1917
1918 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001919 if (!cb) {
1920 /* Last resort when we are OOM */
1921 if (fence)
1922 dma_fence_wait(fence, false);
1923
Dan Carpenter486a68f2017-04-03 21:41:39 +03001924 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001925 } else {
1926 cb->adev = adev;
1927 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1928 amdgpu_vm_prt_cb))
1929 amdgpu_vm_prt_cb(fence, &cb->cb);
1930 }
1931}
1932
1933/**
Christian König284710f2017-01-30 11:09:31 +01001934 * amdgpu_vm_free_mapping - free a mapping
1935 *
1936 * @adev: amdgpu_device pointer
1937 * @vm: requested vm
1938 * @mapping: mapping to be freed
1939 * @fence: fence of the unmap operation
1940 *
1941 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1942 */
1943static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1944 struct amdgpu_vm *vm,
1945 struct amdgpu_bo_va_mapping *mapping,
1946 struct dma_fence *fence)
1947{
Christian König451bc8e2017-02-14 16:02:52 +01001948 if (mapping->flags & AMDGPU_PTE_PRT)
1949 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001950 kfree(mapping);
1951}
1952
1953/**
Christian König451bc8e2017-02-14 16:02:52 +01001954 * amdgpu_vm_prt_fini - finish all prt mappings
1955 *
1956 * @adev: amdgpu_device pointer
1957 * @vm: requested vm
1958 *
1959 * Register a cleanup callback to disable PRT support after VM dies.
1960 */
1961static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1962{
Christian König3f3333f2017-08-03 14:02:13 +02001963 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001964 struct dma_fence *excl, **shared;
1965 unsigned i, shared_count;
1966 int r;
1967
1968 r = reservation_object_get_fences_rcu(resv, &excl,
1969 &shared_count, &shared);
1970 if (r) {
1971 /* Not enough memory to grab the fence list, as last resort
1972 * block for all the fences to complete.
1973 */
1974 reservation_object_wait_timeout_rcu(resv, true, false,
1975 MAX_SCHEDULE_TIMEOUT);
1976 return;
1977 }
1978
1979 /* Add a callback for each fence in the reservation object */
1980 amdgpu_vm_prt_get(adev);
1981 amdgpu_vm_add_prt_cb(adev, excl);
1982
1983 for (i = 0; i < shared_count; ++i) {
1984 amdgpu_vm_prt_get(adev);
1985 amdgpu_vm_add_prt_cb(adev, shared[i]);
1986 }
1987
1988 kfree(shared);
1989}
1990
1991/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001992 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1993 *
1994 * @adev: amdgpu_device pointer
1995 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001996 * @fence: optional resulting fence (unchanged if no work needed to be done
1997 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998 *
1999 * Make sure all freed BOs are cleared in the PT.
2000 * Returns 0 for success.
2001 *
2002 * PTs have to be reserved and mutex must be locked!
2003 */
2004int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002005 struct amdgpu_vm *vm,
2006 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007{
2008 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002009 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002011 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002012
2013 while (!list_empty(&vm->freed)) {
2014 mapping = list_first_entry(&vm->freed,
2015 struct amdgpu_bo_va_mapping, list);
2016 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01002017
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002018 if (vm->pte_support_ats)
2019 init_pte_value = AMDGPU_PTE_SYSTEM;
2020
Christian König570144c2017-08-30 15:38:45 +02002021 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02002022 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002023 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002024 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01002025 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002026 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 return r;
Christian König284710f2017-01-30 11:09:31 +01002028 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002029 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002030
2031 if (fence && f) {
2032 dma_fence_put(*fence);
2033 *fence = f;
2034 } else {
2035 dma_fence_put(f);
2036 }
2037
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038 return 0;
2039
2040}
2041
2042/**
Christian König73fb16e2017-08-16 11:13:48 +02002043 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002044 *
2045 * @adev: amdgpu_device pointer
2046 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02002047 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048 *
Christian König73fb16e2017-08-16 11:13:48 +02002049 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050 * Returns 0 for success.
2051 *
Christian König73fb16e2017-08-16 11:13:48 +02002052 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002053 */
Christian König73fb16e2017-08-16 11:13:48 +02002054int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02002055 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002056{
Christian König73fb16e2017-08-16 11:13:48 +02002057 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02002058 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059
2060 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002061 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02002062 struct amdgpu_bo_va *bo_va;
2063
Christian König27c7b9a2017-08-01 11:27:36 +02002064 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002065 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002066 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002067
Christian König73fb16e2017-08-16 11:13:48 +02002068 /* Per VM BOs never need to bo cleared in the page tables */
2069 clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
2070
2071 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002072 if (r)
2073 return r;
2074
2075 spin_lock(&vm->status_lock);
2076 }
2077 spin_unlock(&vm->status_lock);
2078
Christian König91e1a522015-07-06 22:06:40 +02002079 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080}
2081
2082/**
2083 * amdgpu_vm_bo_add - add a bo to a specific vm
2084 *
2085 * @adev: amdgpu_device pointer
2086 * @vm: requested vm
2087 * @bo: amdgpu buffer object
2088 *
Christian König8843dbb2016-01-26 12:17:11 +01002089 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090 * Add @bo to the list of bos associated with the vm
2091 * Returns newly added bo_va or NULL for failure
2092 *
2093 * Object has to be reserved!
2094 */
2095struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2096 struct amdgpu_vm *vm,
2097 struct amdgpu_bo *bo)
2098{
2099 struct amdgpu_bo_va *bo_va;
2100
2101 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2102 if (bo_va == NULL) {
2103 return NULL;
2104 }
Christian Königec681542017-08-01 10:51:43 +02002105 bo_va->base.vm = vm;
2106 bo_va->base.bo = bo;
2107 INIT_LIST_HEAD(&bo_va->base.bo_list);
2108 INIT_LIST_HEAD(&bo_va->base.vm_status);
2109
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002110 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002111 INIT_LIST_HEAD(&bo_va->valids);
2112 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002113
Christian Königa5f6b5b2017-01-30 11:01:38 +01002114 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002115 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002116
2117 return bo_va;
2118}
2119
Christian König73fb16e2017-08-16 11:13:48 +02002120
2121/**
2122 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2123 *
2124 * @adev: amdgpu_device pointer
2125 * @bo_va: bo_va to store the address
2126 * @mapping: the mapping to insert
2127 *
2128 * Insert a new mapping into all structures.
2129 */
2130static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2131 struct amdgpu_bo_va *bo_va,
2132 struct amdgpu_bo_va_mapping *mapping)
2133{
2134 struct amdgpu_vm *vm = bo_va->base.vm;
2135 struct amdgpu_bo *bo = bo_va->base.bo;
2136
Christian Königaebc5e62017-09-06 16:55:16 +02002137 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002138 list_add(&mapping->list, &bo_va->invalids);
2139 amdgpu_vm_it_insert(mapping, &vm->va);
2140
2141 if (mapping->flags & AMDGPU_PTE_PRT)
2142 amdgpu_vm_prt_get(adev);
2143
2144 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2145 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002146 if (list_empty(&bo_va->base.vm_status))
2147 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02002148 spin_unlock(&vm->status_lock);
2149 }
2150 trace_amdgpu_vm_bo_map(bo_va, mapping);
2151}
2152
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002153/**
2154 * amdgpu_vm_bo_map - map bo inside a vm
2155 *
2156 * @adev: amdgpu_device pointer
2157 * @bo_va: bo_va to store the address
2158 * @saddr: where to map the BO
2159 * @offset: requested offset in the BO
2160 * @flags: attributes of pages (read/write/valid/etc.)
2161 *
2162 * Add a mapping of the BO at the specefied addr into the VM.
2163 * Returns 0 for success, error for failure.
2164 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002165 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002166 */
2167int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2168 struct amdgpu_bo_va *bo_va,
2169 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002170 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171{
Christian Königa9f87f62017-03-30 14:03:59 +02002172 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002173 struct amdgpu_bo *bo = bo_va->base.bo;
2174 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002176
Christian König0be52de2015-05-18 14:37:27 +02002177 /* validate the parameters */
2178 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002179 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002180 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002181
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002182 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002183 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002184 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002185 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188 saddr /= AMDGPU_GPU_PAGE_SIZE;
2189 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2190
Christian Königa9f87f62017-03-30 14:03:59 +02002191 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2192 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002193 /* bo and tmp overlap, invalid addr */
2194 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002195 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002196 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002197 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198 }
2199
2200 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002201 if (!mapping)
2202 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203
Christian Königa9f87f62017-03-30 14:03:59 +02002204 mapping->start = saddr;
2205 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002206 mapping->offset = offset;
2207 mapping->flags = flags;
2208
Christian König73fb16e2017-08-16 11:13:48 +02002209 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002210
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002211 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212}
2213
2214/**
Christian König80f95c52017-03-13 10:13:39 +01002215 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2216 *
2217 * @adev: amdgpu_device pointer
2218 * @bo_va: bo_va to store the address
2219 * @saddr: where to map the BO
2220 * @offset: requested offset in the BO
2221 * @flags: attributes of pages (read/write/valid/etc.)
2222 *
2223 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2224 * mappings as we do so.
2225 * Returns 0 for success, error for failure.
2226 *
2227 * Object has to be reserved and unreserved outside!
2228 */
2229int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2230 struct amdgpu_bo_va *bo_va,
2231 uint64_t saddr, uint64_t offset,
2232 uint64_t size, uint64_t flags)
2233{
2234 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002235 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002236 uint64_t eaddr;
2237 int r;
2238
2239 /* validate the parameters */
2240 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2241 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2242 return -EINVAL;
2243
2244 /* make sure object fit at this offset */
2245 eaddr = saddr + size - 1;
2246 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002247 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002248 return -EINVAL;
2249
2250 /* Allocate all the needed memory */
2251 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2252 if (!mapping)
2253 return -ENOMEM;
2254
Christian Königec681542017-08-01 10:51:43 +02002255 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002256 if (r) {
2257 kfree(mapping);
2258 return r;
2259 }
2260
2261 saddr /= AMDGPU_GPU_PAGE_SIZE;
2262 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2263
Christian Königa9f87f62017-03-30 14:03:59 +02002264 mapping->start = saddr;
2265 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002266 mapping->offset = offset;
2267 mapping->flags = flags;
2268
Christian König73fb16e2017-08-16 11:13:48 +02002269 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002270
2271 return 0;
2272}
2273
2274/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002275 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2276 *
2277 * @adev: amdgpu_device pointer
2278 * @bo_va: bo_va to remove the address from
2279 * @saddr: where to the BO is mapped
2280 *
2281 * Remove a mapping of the BO at the specefied addr from the VM.
2282 * Returns 0 for success, error for failure.
2283 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002284 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002285 */
2286int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2287 struct amdgpu_bo_va *bo_va,
2288 uint64_t saddr)
2289{
2290 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002291 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002292 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002293
Christian König6c7fc502015-06-05 20:56:17 +02002294 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002295
Christian König7fc11952015-07-30 11:53:42 +02002296 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002297 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002298 break;
2299 }
2300
Christian König7fc11952015-07-30 11:53:42 +02002301 if (&mapping->list == &bo_va->valids) {
2302 valid = false;
2303
2304 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002305 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002306 break;
2307 }
2308
Christian König32b41ac2016-03-08 18:03:27 +01002309 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002310 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002311 }
Christian König32b41ac2016-03-08 18:03:27 +01002312
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002313 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002314 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002315 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002316 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002317
Christian Könige17841b2016-03-08 17:52:01 +01002318 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002319 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002320 else
Christian König284710f2017-01-30 11:09:31 +01002321 amdgpu_vm_free_mapping(adev, vm, mapping,
2322 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002323
2324 return 0;
2325}
2326
2327/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002328 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2329 *
2330 * @adev: amdgpu_device pointer
2331 * @vm: VM structure to use
2332 * @saddr: start of the range
2333 * @size: size of the range
2334 *
2335 * Remove all mappings in a range, split them as appropriate.
2336 * Returns 0 for success, error for failure.
2337 */
2338int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2339 struct amdgpu_vm *vm,
2340 uint64_t saddr, uint64_t size)
2341{
2342 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002343 LIST_HEAD(removed);
2344 uint64_t eaddr;
2345
2346 eaddr = saddr + size - 1;
2347 saddr /= AMDGPU_GPU_PAGE_SIZE;
2348 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2349
2350 /* Allocate all the needed memory */
2351 before = kzalloc(sizeof(*before), GFP_KERNEL);
2352 if (!before)
2353 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002354 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002355
2356 after = kzalloc(sizeof(*after), GFP_KERNEL);
2357 if (!after) {
2358 kfree(before);
2359 return -ENOMEM;
2360 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002361 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002362
2363 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002364 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2365 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002366 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002367 if (tmp->start < saddr) {
2368 before->start = tmp->start;
2369 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002370 before->offset = tmp->offset;
2371 before->flags = tmp->flags;
2372 list_add(&before->list, &tmp->list);
2373 }
2374
2375 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002376 if (tmp->last > eaddr) {
2377 after->start = eaddr + 1;
2378 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002379 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002380 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002381 after->flags = tmp->flags;
2382 list_add(&after->list, &tmp->list);
2383 }
2384
2385 list_del(&tmp->list);
2386 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002387
2388 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002389 }
2390
2391 /* And free them up */
2392 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002393 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002394 list_del(&tmp->list);
2395
Christian Königa9f87f62017-03-30 14:03:59 +02002396 if (tmp->start < saddr)
2397 tmp->start = saddr;
2398 if (tmp->last > eaddr)
2399 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002400
Christian Königaebc5e62017-09-06 16:55:16 +02002401 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002402 list_add(&tmp->list, &vm->freed);
2403 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2404 }
2405
Junwei Zhang27f6d612017-03-16 16:09:24 +08002406 /* Insert partial mapping before the range */
2407 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002408 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002409 if (before->flags & AMDGPU_PTE_PRT)
2410 amdgpu_vm_prt_get(adev);
2411 } else {
2412 kfree(before);
2413 }
2414
2415 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002416 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002417 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002418 if (after->flags & AMDGPU_PTE_PRT)
2419 amdgpu_vm_prt_get(adev);
2420 } else {
2421 kfree(after);
2422 }
2423
2424 return 0;
2425}
2426
2427/**
Christian Königaebc5e62017-09-06 16:55:16 +02002428 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2429 *
2430 * @vm: the requested VM
2431 *
2432 * Find a mapping by it's address.
2433 */
2434struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2435 uint64_t addr)
2436{
2437 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2438}
2439
2440/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002441 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2442 *
2443 * @adev: amdgpu_device pointer
2444 * @bo_va: requested bo_va
2445 *
Christian König8843dbb2016-01-26 12:17:11 +01002446 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002447 *
2448 * Object have to be reserved!
2449 */
2450void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2451 struct amdgpu_bo_va *bo_va)
2452{
2453 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002454 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002455
Christian Königec681542017-08-01 10:51:43 +02002456 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002457
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002458 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002459 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002460 spin_unlock(&vm->status_lock);
2461
Christian König7fc11952015-07-30 11:53:42 +02002462 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002463 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002464 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002465 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002466 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002467 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002468 }
Christian König7fc11952015-07-30 11:53:42 +02002469 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2470 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002471 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002472 amdgpu_vm_free_mapping(adev, vm, mapping,
2473 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002474 }
Christian König32b41ac2016-03-08 18:03:27 +01002475
Chris Wilsonf54d1862016-10-25 13:00:45 +01002476 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002477 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002478}
2479
2480/**
2481 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2482 *
2483 * @adev: amdgpu_device pointer
2484 * @vm: requested vm
2485 * @bo: amdgpu buffer object
2486 *
Christian König8843dbb2016-01-26 12:17:11 +01002487 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488 */
2489void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002490 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002491{
Christian Königec681542017-08-01 10:51:43 +02002492 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002493
Christian Königec681542017-08-01 10:51:43 +02002494 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002495 struct amdgpu_vm *vm = bo_base->vm;
2496
Christian König3d7d4d32017-08-23 16:13:33 +02002497 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002498 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2499 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002500 if (bo->tbo.type == ttm_bo_type_kernel)
2501 list_move(&bo_base->vm_status, &vm->evicted);
2502 else
2503 list_move_tail(&bo_base->vm_status,
2504 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002505 spin_unlock(&bo_base->vm->status_lock);
2506 continue;
2507 }
2508
Christian Königea097292017-08-09 14:15:46 +02002509 if (bo->tbo.type == ttm_bo_type_kernel) {
2510 spin_lock(&bo_base->vm->status_lock);
2511 if (list_empty(&bo_base->vm_status))
2512 list_add(&bo_base->vm_status, &vm->relocated);
2513 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002514 continue;
Christian Königea097292017-08-09 14:15:46 +02002515 }
Christian König3f3333f2017-08-03 14:02:13 +02002516
Christian Königec681542017-08-01 10:51:43 +02002517 spin_lock(&bo_base->vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002518 if (list_empty(&bo_base->vm_status))
2519 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002520 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002521 }
2522}
2523
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002524static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2525{
2526 /* Total bits covered by PD + PTs */
2527 unsigned bits = ilog2(vm_size) + 18;
2528
2529 /* Make sure the PD is 4K in size up to 8GB address space.
2530 Above that split equal between PD and PTs */
2531 if (vm_size <= 8)
2532 return (bits - 9);
2533 else
2534 return ((bits + 3) / 2);
2535}
2536
2537/**
Roger Hed07f14b2017-08-15 16:05:59 +08002538 * amdgpu_vm_set_fragment_size - adjust fragment size in PTE
2539 *
2540 * @adev: amdgpu_device pointer
2541 * @fragment_size_default: the default fragment size if it's set auto
2542 */
2543void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev, uint32_t fragment_size_default)
2544{
2545 if (amdgpu_vm_fragment_size == -1)
2546 adev->vm_manager.fragment_size = fragment_size_default;
2547 else
2548 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2549}
2550
2551/**
2552 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002553 *
2554 * @adev: amdgpu_device pointer
2555 * @vm_size: the default vm size if it's set auto
2556 */
Roger Hed07f14b2017-08-15 16:05:59 +08002557void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size, uint32_t fragment_size_default)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002558{
2559 /* adjust vm size firstly */
2560 if (amdgpu_vm_size == -1)
2561 adev->vm_manager.vm_size = vm_size;
2562 else
2563 adev->vm_manager.vm_size = amdgpu_vm_size;
2564
2565 /* block size depends on vm size */
2566 if (amdgpu_vm_block_size == -1)
2567 adev->vm_manager.block_size =
2568 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2569 else
2570 adev->vm_manager.block_size = amdgpu_vm_block_size;
2571
Roger Hed07f14b2017-08-15 16:05:59 +08002572 amdgpu_vm_set_fragment_size(adev, fragment_size_default);
2573
2574 DRM_INFO("vm size is %llu GB, block size is %u-bit, fragment size is %u-bit\n",
2575 adev->vm_manager.vm_size, adev->vm_manager.block_size,
2576 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002577}
2578
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002579/**
2580 * amdgpu_vm_init - initialize a vm instance
2581 *
2582 * @adev: amdgpu_device pointer
2583 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002584 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002585 *
Christian König8843dbb2016-01-26 12:17:11 +01002586 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002587 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002588int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002589 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002590{
2591 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002592 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002593 unsigned ring_instance;
2594 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01002595 struct amd_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002596 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002597 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002598 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002599
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002600 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08002601 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002602 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2603 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002604 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002605 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002606 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002607 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002609
Christian König2bd9ccf2016-02-01 12:53:58 +01002610 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002611
2612 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2613 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2614 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01002615 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2616 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2617 rq, amdgpu_sched_jobs);
2618 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002619 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002620
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002621 vm->pte_support_ats = false;
2622
2623 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002624 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2625 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002626
2627 if (adev->asic_type == CHIP_RAVEN) {
2628 vm->pte_support_ats = true;
2629 init_pde_value = AMDGPU_PTE_SYSTEM | AMDGPU_PDE_PTE;
2630 }
2631 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002632 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2633 AMDGPU_VM_USE_CPU_FOR_GFX);
2634 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2635 vm->use_cpu_for_update ? "CPU" : "SDMA");
2636 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2637 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002638 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002639
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002640 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2641 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2642 if (vm->use_cpu_for_update)
2643 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2644 else
2645 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2646 AMDGPU_GEM_CREATE_SHADOW);
2647
Christian Königf566ceb2016-10-27 20:04:38 +02002648 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002649 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002650 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002651 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002652 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002653 goto error_free_sched_entity;
2654
Christian König3f3333f2017-08-03 14:02:13 +02002655 vm->root.base.vm = vm;
2656 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2657 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002658
2659 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002660 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002661 if (r)
2662 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002663
Christian König3f3333f2017-08-03 14:02:13 +02002664 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
Felix Kuehlingca290da2017-08-25 20:15:04 -04002665 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König3f3333f2017-08-03 14:02:13 +02002666 if (r)
2667 goto error_free_root;
Christian König3f3333f2017-08-03 14:02:13 +02002668 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002669
Felix Kuehling02208442017-08-25 20:40:26 -04002670 if (pasid) {
2671 unsigned long flags;
2672
2673 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2674 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2675 GFP_ATOMIC);
2676 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2677 if (r < 0)
2678 goto error_free_root;
2679
2680 vm->pasid = pasid;
2681 }
2682
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002683 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002684
Christian König67003a12016-10-12 14:46:26 +02002685error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002686 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2687 amdgpu_bo_unref(&vm->root.base.bo);
2688 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002689
2690error_free_sched_entity:
2691 amd_sched_entity_fini(&ring->sched, &vm->entity);
2692
2693 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002694}
2695
2696/**
Christian Königf566ceb2016-10-27 20:04:38 +02002697 * amdgpu_vm_free_levels - free PD/PT levels
2698 *
2699 * @level: PD/PT starting level to free
2700 *
2701 * Free the page directory or page table level and all sub levels.
2702 */
2703static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2704{
2705 unsigned i;
2706
Christian König3f3333f2017-08-03 14:02:13 +02002707 if (level->base.bo) {
2708 list_del(&level->base.bo_list);
2709 list_del(&level->base.vm_status);
2710 amdgpu_bo_unref(&level->base.bo->shadow);
2711 amdgpu_bo_unref(&level->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002712 }
2713
2714 if (level->entries)
2715 for (i = 0; i <= level->last_entry_used; i++)
2716 amdgpu_vm_free_levels(&level->entries[i]);
2717
Michal Hocko20981052017-05-17 14:23:12 +02002718 kvfree(level->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002719}
2720
2721/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002722 * amdgpu_vm_fini - tear down a vm instance
2723 *
2724 * @adev: amdgpu_device pointer
2725 * @vm: requested vm
2726 *
Christian König8843dbb2016-01-26 12:17:11 +01002727 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002728 * Unbind the VM and remove all bos from the vm bo list
2729 */
2730void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2731{
2732 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002733 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002734 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002735
Felix Kuehling02208442017-08-25 20:40:26 -04002736 if (vm->pasid) {
2737 unsigned long flags;
2738
2739 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2740 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2741 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2742 }
2743
Christian König2d55e452016-02-08 17:37:38 +01002744 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002745
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002746 if (!RB_EMPTY_ROOT(&vm->va)) {
2747 dev_err(adev->dev, "still active bo inside vm\n");
2748 }
Christian Königa9f87f62017-03-30 14:03:59 +02002749 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002750 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002751 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002752 kfree(mapping);
2753 }
2754 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002755 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002756 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002757 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002758 }
Christian König284710f2017-01-30 11:09:31 +01002759
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002760 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002761 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002762 }
2763
Christian Königf566ceb2016-10-27 20:04:38 +02002764 amdgpu_vm_free_levels(&vm->root);
Christian Königd5884512017-09-08 14:09:41 +02002765 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002766 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2767 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002768}
Christian Königea89f8c2015-11-15 20:52:06 +01002769
2770/**
Christian Königa9a78b32016-01-21 10:19:11 +01002771 * amdgpu_vm_manager_init - init the VM manager
2772 *
2773 * @adev: amdgpu_device pointer
2774 *
2775 * Initialize the VM manager structures
2776 */
2777void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2778{
Christian König76456702017-04-06 17:52:39 +02002779 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002780
Christian König76456702017-04-06 17:52:39 +02002781 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2782 struct amdgpu_vm_id_manager *id_mgr =
2783 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002784
Christian König76456702017-04-06 17:52:39 +02002785 mutex_init(&id_mgr->lock);
2786 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002787 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002788
2789 /* skip over VMID 0, since it is the system VM */
2790 for (j = 1; j < id_mgr->num_ids; ++j) {
2791 amdgpu_vm_reset_id(adev, i, j);
2792 amdgpu_sync_create(&id_mgr->ids[i].active);
2793 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2794 }
Christian König971fe9a92016-03-01 15:09:25 +01002795 }
Christian König2d55e452016-02-08 17:37:38 +01002796
Chris Wilsonf54d1862016-10-25 13:00:45 +01002797 adev->vm_manager.fence_context =
2798 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002799 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2800 adev->vm_manager.seqno[i] = 0;
2801
Christian König2d55e452016-02-08 17:37:38 +01002802 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002803 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002804 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002805 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002806
2807 /* If not overridden by the user, by default, only in large BAR systems
2808 * Compute VM tables will be updated by CPU
2809 */
2810#ifdef CONFIG_X86_64
2811 if (amdgpu_vm_update_mode == -1) {
2812 if (amdgpu_vm_is_large_bar(adev))
2813 adev->vm_manager.vm_update_mode =
2814 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2815 else
2816 adev->vm_manager.vm_update_mode = 0;
2817 } else
2818 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2819#else
2820 adev->vm_manager.vm_update_mode = 0;
2821#endif
2822
Felix Kuehling02208442017-08-25 20:40:26 -04002823 idr_init(&adev->vm_manager.pasid_idr);
2824 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002825}
2826
2827/**
Christian Königea89f8c2015-11-15 20:52:06 +01002828 * amdgpu_vm_manager_fini - cleanup VM manager
2829 *
2830 * @adev: amdgpu_device pointer
2831 *
2832 * Cleanup the VM manager and free resources.
2833 */
2834void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2835{
Christian König76456702017-04-06 17:52:39 +02002836 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002837
Felix Kuehling02208442017-08-25 20:40:26 -04002838 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2839 idr_destroy(&adev->vm_manager.pasid_idr);
2840
Christian König76456702017-04-06 17:52:39 +02002841 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2842 struct amdgpu_vm_id_manager *id_mgr =
2843 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002844
Christian König76456702017-04-06 17:52:39 +02002845 mutex_destroy(&id_mgr->lock);
2846 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2847 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2848
2849 amdgpu_sync_free(&id->active);
2850 dma_fence_put(id->flushed_updates);
2851 dma_fence_put(id->last_flush);
2852 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002853 }
Christian Königea89f8c2015-11-15 20:52:06 +01002854}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002855
2856int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2857{
2858 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002859 struct amdgpu_device *adev = dev->dev_private;
2860 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2861 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002862
2863 switch (args->in.op) {
2864 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002865 /* current, we only have requirement to reserve vmid from gfxhub */
2866 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2867 AMDGPU_GFXHUB);
2868 if (r)
2869 return r;
2870 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002871 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002872 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002873 break;
2874 default:
2875 return -EINVAL;
2876 }
2877
2878 return 0;
2879}