blob: 398abbcbf0299831da302766e71b04d7d2449794 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence-array.h>
Christian Königa9f87f62017-03-30 14:03:59 +020029#include <linux/interval_tree_generic.h>
Felix Kuehling02208442017-08-25 20:40:26 -040030#include <linux/idr.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031#include <drm/drmP.h>
32#include <drm/amdgpu_drm.h>
33#include "amdgpu.h"
34#include "amdgpu_trace.h"
35
36/*
Felix Kuehling02208442017-08-25 20:40:26 -040037 * PASID manager
38 *
39 * PASIDs are global address space identifiers that can be shared
40 * between the GPU, an IOMMU and the driver. VMs on different devices
41 * may use the same PASID if they share the same address
42 * space. Therefore PASIDs are allocated using a global IDA. VMs are
43 * looked up from the PASID per amdgpu_device.
44 */
45static DEFINE_IDA(amdgpu_vm_pasid_ida);
46
47/**
48 * amdgpu_vm_alloc_pasid - Allocate a PASID
49 * @bits: Maximum width of the PASID in bits, must be at least 1
50 *
51 * Allocates a PASID of the given width while keeping smaller PASIDs
52 * available if possible.
53 *
54 * Returns a positive integer on success. Returns %-EINVAL if bits==0.
55 * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
56 * memory allocation failure.
57 */
58int amdgpu_vm_alloc_pasid(unsigned int bits)
59{
60 int pasid = -EINVAL;
61
62 for (bits = min(bits, 31U); bits > 0; bits--) {
63 pasid = ida_simple_get(&amdgpu_vm_pasid_ida,
64 1U << (bits - 1), 1U << bits,
65 GFP_KERNEL);
66 if (pasid != -ENOSPC)
67 break;
68 }
69
70 return pasid;
71}
72
73/**
74 * amdgpu_vm_free_pasid - Free a PASID
75 * @pasid: PASID to free
76 */
77void amdgpu_vm_free_pasid(unsigned int pasid)
78{
79 ida_simple_remove(&amdgpu_vm_pasid_ida, pasid);
80}
81
82/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 * GPUVM
84 * GPUVM is similar to the legacy gart on older asics, however
85 * rather than there being a single global gart table
86 * for the entire GPU, there are multiple VM page tables active
87 * at any given time. The VM page tables can contain a mix
88 * vram pages and system memory pages and system memory pages
89 * can be mapped as snooped (cached system pages) or unsnooped
90 * (uncached system pages).
91 * Each VM has an ID associated with it and there is a page table
92 * associated with each VMID. When execting a command buffer,
93 * the kernel tells the the ring what VMID to use for that command
94 * buffer. VMIDs are allocated dynamically as commands are submitted.
95 * The userspace drivers maintain their own address space and the kernel
96 * sets up their pages tables accordingly when they submit their
97 * command buffers and a VMID is assigned.
98 * Cayman/Trinity support up to 8 active VMs at any given time;
99 * SI supports 16.
100 */
101
Christian Königa9f87f62017-03-30 14:03:59 +0200102#define START(node) ((node)->start)
103#define LAST(node) ((node)->last)
104
105INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
106 START, LAST, static, amdgpu_vm_it)
107
108#undef START
109#undef LAST
110
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400111/* Local structure. Encapsulate some VM table update parameters to reduce
112 * the number of function parameters
113 */
Christian König29efc4f2016-08-04 14:52:50 +0200114struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +0200115 /* amdgpu device we do this update for */
116 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +0200117 /* optional amdgpu_vm we do this update for */
118 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400119 /* address where to copy page table entries from */
120 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400121 /* indirect buffer to fill with commands */
122 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +0200123 /* Function which actually does the update */
124 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
125 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800126 uint64_t flags);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400127 /* The next two are used during VM update by CPU
128 * DMA addresses to use for mapping
129 * Kernel pointer of PD/PT BO that needs to be updated
130 */
131 dma_addr_t *pages_addr;
132 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400133};
134
Christian König284710f2017-01-30 11:09:31 +0100135/* Helper to disable partial resident texture feature from a fence callback */
136struct amdgpu_prt_cb {
137 struct amdgpu_device *adev;
138 struct dma_fence_cb cb;
139};
140
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141/**
Christian König50783142017-11-27 14:01:51 +0100142 * amdgpu_vm_level_shift - return the addr shift for each level
143 *
144 * @adev: amdgpu_device pointer
145 *
146 * Returns the number of bits the pfn needs to be right shifted for a level.
147 */
148static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
149 unsigned level)
150{
Chunming Zhou196f7482017-12-13 14:22:54 +0800151 unsigned shift = 0xff;
152
153 switch (level) {
154 case AMDGPU_VM_PDB2:
155 case AMDGPU_VM_PDB1:
156 case AMDGPU_VM_PDB0:
157 shift = 9 * (AMDGPU_VM_PDB0 - level) +
Christian König50783142017-11-27 14:01:51 +0100158 adev->vm_manager.block_size;
Chunming Zhou196f7482017-12-13 14:22:54 +0800159 break;
160 case AMDGPU_VM_PTB:
161 shift = 0;
162 break;
163 default:
164 dev_err(adev->dev, "the level%d isn't supported.\n", level);
165 }
166
167 return shift;
Christian König50783142017-11-27 14:01:51 +0100168}
169
170/**
Christian König72a7ec52016-10-19 11:03:57 +0200171 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172 *
173 * @adev: amdgpu_device pointer
174 *
Christian König72a7ec52016-10-19 11:03:57 +0200175 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 */
Christian König72a7ec52016-10-19 11:03:57 +0200177static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
178 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400179{
Chunming Zhou196f7482017-12-13 14:22:54 +0800180 unsigned shift = amdgpu_vm_level_shift(adev,
181 adev->vm_manager.root_level);
Christian König0410c5e2017-11-20 14:29:01 +0100182
Chunming Zhou196f7482017-12-13 14:22:54 +0800183 if (level == adev->vm_manager.root_level)
Christian König72a7ec52016-10-19 11:03:57 +0200184 /* For the root directory */
Christian König0410c5e2017-11-20 14:29:01 +0100185 return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
Chunming Zhou196f7482017-12-13 14:22:54 +0800186 else if (level != AMDGPU_VM_PTB)
Christian König0410c5e2017-11-20 14:29:01 +0100187 /* Everything in between */
188 return 512;
189 else
Christian König72a7ec52016-10-19 11:03:57 +0200190 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800191 return AMDGPU_VM_PTE_COUNT(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192}
193
194/**
Christian König72a7ec52016-10-19 11:03:57 +0200195 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196 *
197 * @adev: amdgpu_device pointer
198 *
Christian König72a7ec52016-10-19 11:03:57 +0200199 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200 */
Christian König72a7ec52016-10-19 11:03:57 +0200201static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202{
Christian König72a7ec52016-10-19 11:03:57 +0200203 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204}
205
206/**
Christian König56467eb2015-12-11 15:16:32 +0100207 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208 *
209 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100210 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100211 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 *
213 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100214 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215 */
Christian König56467eb2015-12-11 15:16:32 +0100216void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
217 struct list_head *validated,
218 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400219{
Christian König3f3333f2017-08-03 14:02:13 +0200220 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100221 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200222 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100223 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100224 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100225 list_add(&entry->tv.head, validated);
226}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400227
Christian König56467eb2015-12-11 15:16:32 +0100228/**
Christian Königf7da30d2016-09-28 12:03:04 +0200229 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100230 *
Christian König5a712a82016-06-21 16:28:15 +0200231 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100232 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200233 * @validate: callback to do the validation
234 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235 *
Christian Königf7da30d2016-09-28 12:03:04 +0200236 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400237 */
Christian Königf7da30d2016-09-28 12:03:04 +0200238int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
239 int (*validate)(void *p, struct amdgpu_bo *bo),
240 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241{
Christian König3f3333f2017-08-03 14:02:13 +0200242 struct ttm_bo_global *glob = adev->mman.bdev.glob;
243 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400244
Christian König3f3333f2017-08-03 14:02:13 +0200245 spin_lock(&vm->status_lock);
246 while (!list_empty(&vm->evicted)) {
247 struct amdgpu_vm_bo_base *bo_base;
248 struct amdgpu_bo *bo;
Christian König5a712a82016-06-21 16:28:15 +0200249
Christian König3f3333f2017-08-03 14:02:13 +0200250 bo_base = list_first_entry(&vm->evicted,
251 struct amdgpu_vm_bo_base,
252 vm_status);
253 spin_unlock(&vm->status_lock);
Christian Königeceb8a12016-01-11 15:35:21 +0100254
Christian König3f3333f2017-08-03 14:02:13 +0200255 bo = bo_base->bo;
256 BUG_ON(!bo);
257 if (bo->parent) {
258 r = validate(param, bo);
259 if (r)
260 return r;
Christian König34d7be52017-08-24 12:32:55 +0200261
Christian König3f3333f2017-08-03 14:02:13 +0200262 spin_lock(&glob->lru_lock);
263 ttm_bo_move_to_lru_tail(&bo->tbo);
264 if (bo->shadow)
265 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
266 spin_unlock(&glob->lru_lock);
267 }
268
Christian König73fb16e2017-08-16 11:13:48 +0200269 if (bo->tbo.type == ttm_bo_type_kernel &&
270 vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +0200271 r = amdgpu_bo_kmap(bo, NULL);
272 if (r)
273 return r;
274 }
275
276 spin_lock(&vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200277 if (bo->tbo.type != ttm_bo_type_kernel)
278 list_move(&bo_base->vm_status, &vm->moved);
279 else
280 list_move(&bo_base->vm_status, &vm->relocated);
Christian König3f3333f2017-08-03 14:02:13 +0200281 }
282 spin_unlock(&vm->status_lock);
Christian König34d7be52017-08-24 12:32:55 +0200283
284 return 0;
285}
286
287/**
288 * amdgpu_vm_ready - check VM is ready for updates
289 *
Christian König34d7be52017-08-24 12:32:55 +0200290 * @vm: VM to check
291 *
292 * Check if all VM PDs/PTs are ready for updates
293 */
Christian König3f3333f2017-08-03 14:02:13 +0200294bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200295{
Christian König3f3333f2017-08-03 14:02:13 +0200296 bool ready;
Christian König34d7be52017-08-24 12:32:55 +0200297
Christian König3f3333f2017-08-03 14:02:13 +0200298 spin_lock(&vm->status_lock);
299 ready = list_empty(&vm->evicted);
300 spin_unlock(&vm->status_lock);
301
302 return ready;
Christian Königeceb8a12016-01-11 15:35:21 +0100303}
304
305/**
Christian Königf566ceb2016-10-27 20:04:38 +0200306 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
307 *
308 * @adev: amdgpu_device pointer
309 * @vm: requested vm
310 * @saddr: start of the address range
311 * @eaddr: end of the address range
312 *
313 * Make sure the page directories and page tables are allocated
314 */
315static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
316 struct amdgpu_vm *vm,
317 struct amdgpu_vm_pt *parent,
318 uint64_t saddr, uint64_t eaddr,
319 unsigned level)
320{
Christian König50783142017-11-27 14:01:51 +0100321 unsigned shift = amdgpu_vm_level_shift(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200322 unsigned pt_idx, from, to;
323 int r;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400324 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400325 uint64_t init_value = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200326
327 if (!parent->entries) {
328 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
329
Michal Hocko20981052017-05-17 14:23:12 +0200330 parent->entries = kvmalloc_array(num_entries,
331 sizeof(struct amdgpu_vm_pt),
332 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200333 if (!parent->entries)
334 return -ENOMEM;
335 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
336 }
337
Felix Kuehling1866bac2017-03-28 20:36:12 -0400338 from = saddr >> shift;
339 to = eaddr >> shift;
340 if (from >= amdgpu_vm_num_entries(adev, level) ||
341 to >= amdgpu_vm_num_entries(adev, level))
342 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200343
Christian Königf566ceb2016-10-27 20:04:38 +0200344 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400345 saddr = saddr & ((1 << shift) - 1);
346 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200347
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400348 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
349 AMDGPU_GEM_CREATE_VRAM_CLEARED;
350 if (vm->use_cpu_for_update)
351 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
352 else
353 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
354 AMDGPU_GEM_CREATE_SHADOW);
355
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400356 if (vm->pte_support_ats) {
Yong Zhao6d16dac2017-08-31 15:55:00 -0400357 init_value = AMDGPU_PTE_DEFAULT_ATC;
Chunming Zhou196f7482017-12-13 14:22:54 +0800358 if (level != AMDGPU_VM_PTB)
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400359 init_value |= AMDGPU_PDE_PTE;
Yong Zhao6d16dac2017-08-31 15:55:00 -0400360
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400361 }
362
Christian Königf566ceb2016-10-27 20:04:38 +0200363 /* walk over the address space and allocate the page tables */
364 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200365 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200366 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
367 struct amdgpu_bo *pt;
368
Christian König3f3333f2017-08-03 14:02:13 +0200369 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200370 r = amdgpu_bo_create(adev,
371 amdgpu_vm_bo_size(adev, level),
372 AMDGPU_GPU_PAGE_SIZE, true,
373 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400374 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400375 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200376 if (r)
377 return r;
378
Christian König0a096fb2017-07-12 10:01:48 +0200379 if (vm->use_cpu_for_update) {
380 r = amdgpu_bo_kmap(pt, NULL);
381 if (r) {
382 amdgpu_bo_unref(&pt);
383 return r;
384 }
385 }
386
Christian Königf566ceb2016-10-27 20:04:38 +0200387 /* Keep a reference to the root directory to avoid
388 * freeing them up in the wrong order.
389 */
Christian König0f2fc432017-08-31 10:46:20 +0200390 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200391
Christian König3f3333f2017-08-03 14:02:13 +0200392 entry->base.vm = vm;
393 entry->base.bo = pt;
394 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200395 spin_lock(&vm->status_lock);
396 list_add(&entry->base.vm_status, &vm->relocated);
397 spin_unlock(&vm->status_lock);
Christian Königf566ceb2016-10-27 20:04:38 +0200398 }
399
Chunming Zhou196f7482017-12-13 14:22:54 +0800400 if (level < AMDGPU_VM_PTB) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400401 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
402 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
403 ((1 << shift) - 1);
404 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
405 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200406 if (r)
407 return r;
408 }
409 }
410
411 return 0;
412}
413
Christian König663e4572017-03-13 10:13:37 +0100414/**
415 * amdgpu_vm_alloc_pts - Allocate page tables.
416 *
417 * @adev: amdgpu_device pointer
418 * @vm: VM to allocate page tables for
419 * @saddr: Start address which needs to be allocated
420 * @size: Size from start address we need.
421 *
422 * Make sure the page tables are allocated.
423 */
424int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
425 struct amdgpu_vm *vm,
426 uint64_t saddr, uint64_t size)
427{
Felix Kuehling22770e52017-03-28 20:24:53 -0400428 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100429 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100430
431 /* validate the parameters */
432 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
433 return -EINVAL;
434
435 eaddr = saddr + size - 1;
436 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
437 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400438 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100439 last_pfn, adev->vm_manager.max_pfn);
440 return -EINVAL;
441 }
442
443 saddr /= AMDGPU_GPU_PAGE_SIZE;
444 eaddr /= AMDGPU_GPU_PAGE_SIZE;
445
Chunming Zhou196f7482017-12-13 14:22:54 +0800446 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
447 adev->vm_manager.root_level);
Christian König663e4572017-03-13 10:13:37 +0100448}
449
Christian König641e9402017-04-03 13:59:25 +0200450/**
451 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
452 *
453 * @adev: amdgpu_device pointer
454 * @id: VMID structure
455 *
456 * Check if GPU reset occured since last use of the VMID.
457 */
458static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
459 struct amdgpu_vm_id *id)
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800460{
461 return id->current_gpu_reset_count !=
Christian König641e9402017-04-03 13:59:25 +0200462 atomic_read(&adev->gpu_reset_counter);
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800463}
464
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800465static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
466{
467 return !!vm->reserved_vmid[vmhub];
468}
469
470/* idr_mgr->lock must be held */
471static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
472 struct amdgpu_ring *ring,
473 struct amdgpu_sync *sync,
474 struct dma_fence *fence,
475 struct amdgpu_job *job)
476{
477 struct amdgpu_device *adev = ring->adev;
478 unsigned vmhub = ring->funcs->vmhub;
479 uint64_t fence_context = adev->fence_context + ring->idx;
480 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
481 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
482 struct dma_fence *updates = sync->last_vm_update;
483 int r = 0;
484 struct dma_fence *flushed, *tmp;
Christian König6f1ceab2017-07-11 16:59:21 +0200485 bool needs_flush = vm->use_cpu_for_update;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800486
487 flushed = id->flushed_updates;
488 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
489 (atomic64_read(&id->owner) != vm->client_id) ||
490 (job->vm_pd_addr != id->pd_gpu_addr) ||
491 (updates && (!flushed || updates->context != flushed->context ||
492 dma_fence_is_later(updates, flushed))) ||
493 (!id->last_flush || (id->last_flush->context != fence_context &&
494 !dma_fence_is_signaled(id->last_flush)))) {
495 needs_flush = true;
496 /* to prevent one context starved by another context */
497 id->pd_gpu_addr = 0;
498 tmp = amdgpu_sync_peek_fence(&id->active, ring);
499 if (tmp) {
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -0500500 r = amdgpu_sync_fence(adev, sync, tmp, false);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800501 return r;
502 }
503 }
504
505 /* Good we can use this VMID. Remember this submission as
506 * user of the VMID.
507 */
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -0500508 r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800509 if (r)
510 goto out;
511
512 if (updates && (!flushed || updates->context != flushed->context ||
513 dma_fence_is_later(updates, flushed))) {
514 dma_fence_put(id->flushed_updates);
515 id->flushed_updates = dma_fence_get(updates);
516 }
517 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800518 atomic64_set(&id->owner, vm->client_id);
519 job->vm_needs_flush = needs_flush;
520 if (needs_flush) {
521 dma_fence_put(id->last_flush);
522 id->last_flush = NULL;
523 }
524 job->vm_id = id - id_mgr->ids;
525 trace_amdgpu_vm_grab_id(vm, ring, job);
526out:
527 return r;
528}
529
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530/**
531 * amdgpu_vm_grab_id - allocate the next free VMID
532 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200534 * @ring: ring we want to submit job to
535 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100536 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537 *
Christian König7f8a5292015-07-20 16:09:40 +0200538 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 */
Christian König7f8a5292015-07-20 16:09:40 +0200540int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100541 struct amdgpu_sync *sync, struct dma_fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800542 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 struct amdgpu_device *adev = ring->adev;
Christian König2e819842017-03-30 16:50:47 +0200545 unsigned vmhub = ring->funcs->vmhub;
Christian König76456702017-04-06 17:52:39 +0200546 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian König090b7672016-07-08 10:21:02 +0200547 uint64_t fence_context = adev->fence_context + ring->idx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100548 struct dma_fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200549 struct amdgpu_vm_id *id, *idle;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100550 struct dma_fence **fences;
Christian König1fbb2e92016-06-01 10:47:36 +0200551 unsigned i;
552 int r = 0;
553
Christian König76456702017-04-06 17:52:39 +0200554 mutex_lock(&id_mgr->lock);
Chunming Zhou7a63eb22017-04-21 11:13:56 +0800555 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
556 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
557 mutex_unlock(&id_mgr->lock);
558 return r;
559 }
560 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
561 if (!fences) {
562 mutex_unlock(&id_mgr->lock);
563 return -ENOMEM;
564 }
Christian König36fd7c52016-05-23 15:30:08 +0200565 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200566 i = 0;
Christian König76456702017-04-06 17:52:39 +0200567 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200568 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
569 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200570 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200571 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200572 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100573
Christian König1fbb2e92016-06-01 10:47:36 +0200574 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König76456702017-04-06 17:52:39 +0200575 if (&idle->list == &id_mgr->ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200576 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
577 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100578 struct dma_fence_array *array;
Christian König1fbb2e92016-06-01 10:47:36 +0200579 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200580
Christian König1fbb2e92016-06-01 10:47:36 +0200581 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100582 dma_fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200583
Chris Wilsonf54d1862016-10-25 13:00:45 +0100584 array = dma_fence_array_create(i, fences, fence_context,
Christian König1fbb2e92016-06-01 10:47:36 +0200585 seqno, true);
586 if (!array) {
587 for (j = 0; j < i; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100588 dma_fence_put(fences[j]);
Christian König1fbb2e92016-06-01 10:47:36 +0200589 kfree(fences);
590 r = -ENOMEM;
591 goto error;
592 }
Christian König8d76001e2016-05-23 16:00:32 +0200593
Christian König8d76001e2016-05-23 16:00:32 +0200594
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -0500595 r = amdgpu_sync_fence(ring->adev, sync, &array->base, false);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100596 dma_fence_put(&array->base);
Christian König1fbb2e92016-06-01 10:47:36 +0200597 if (r)
598 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200599
Christian König76456702017-04-06 17:52:39 +0200600 mutex_unlock(&id_mgr->lock);
Christian König1fbb2e92016-06-01 10:47:36 +0200601 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200602
Christian König1fbb2e92016-06-01 10:47:36 +0200603 }
604 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200605
Christian König6f1ceab2017-07-11 16:59:21 +0200606 job->vm_needs_flush = vm->use_cpu_for_update;
Christian König1fbb2e92016-06-01 10:47:36 +0200607 /* Check if we can use a VMID already assigned to this VM */
Christian König76456702017-04-06 17:52:39 +0200608 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100609 struct dma_fence *flushed;
Christian König6f1ceab2017-07-11 16:59:21 +0200610 bool needs_flush = vm->use_cpu_for_update;
Christian König8d76001e2016-05-23 16:00:32 +0200611
Christian König1fbb2e92016-06-01 10:47:36 +0200612 /* Check all the prerequisites to using this VMID */
Christian König641e9402017-04-03 13:59:25 +0200613 if (amdgpu_vm_had_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800614 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200615
616 if (atomic64_read(&id->owner) != vm->client_id)
617 continue;
618
Chunming Zhoufd53be32016-07-01 17:59:01 +0800619 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200620 continue;
621
Christian König87c910d2017-03-30 16:56:20 +0200622 if (!id->last_flush ||
623 (id->last_flush->context != fence_context &&
624 !dma_fence_is_signaled(id->last_flush)))
625 needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200626
627 flushed = id->flushed_updates;
Christian König87c910d2017-03-30 16:56:20 +0200628 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
629 needs_flush = true;
630
631 /* Concurrent flushes are only possible starting with Vega10 */
632 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
Christian König1fbb2e92016-06-01 10:47:36 +0200633 continue;
634
Christian König3dab83b2016-06-01 13:31:17 +0200635 /* Good we can use this VMID. Remember this submission as
636 * user of the VMID.
637 */
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -0500638 r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
Christian König1fbb2e92016-06-01 10:47:36 +0200639 if (r)
640 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200641
Christian König87c910d2017-03-30 16:56:20 +0200642 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
643 dma_fence_put(id->flushed_updates);
644 id->flushed_updates = dma_fence_get(updates);
645 }
Christian König8d76001e2016-05-23 16:00:32 +0200646
Christian König87c910d2017-03-30 16:56:20 +0200647 if (needs_flush)
648 goto needs_flush;
649 else
650 goto no_flush_needed;
Christian König8d76001e2016-05-23 16:00:32 +0200651
Christian König4f618e72017-04-06 15:18:21 +0200652 };
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800653
Christian König1fbb2e92016-06-01 10:47:36 +0200654 /* Still no ID to use? Then use the idle one found earlier */
655 id = idle;
656
657 /* Remember this submission as user of the VMID */
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -0500658 r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
Christian König832a9022016-02-15 12:33:02 +0100659 if (r)
660 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100661
Christian König87c910d2017-03-30 16:56:20 +0200662 id->pd_gpu_addr = job->vm_pd_addr;
663 dma_fence_put(id->flushed_updates);
664 id->flushed_updates = dma_fence_get(updates);
Christian König87c910d2017-03-30 16:56:20 +0200665 atomic64_set(&id->owner, vm->client_id);
666
667needs_flush:
668 job->vm_needs_flush = true;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100669 dma_fence_put(id->last_flush);
Christian König41d9eb22016-03-01 16:46:18 +0100670 id->last_flush = NULL;
671
Christian König87c910d2017-03-30 16:56:20 +0200672no_flush_needed:
Christian König76456702017-04-06 17:52:39 +0200673 list_move_tail(&id->list, &id_mgr->ids_lru);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400674
Christian König76456702017-04-06 17:52:39 +0200675 job->vm_id = id - id_mgr->ids;
Christian Königc5296d12017-04-07 15:31:13 +0200676 trace_amdgpu_vm_grab_id(vm, ring, job);
Christian König832a9022016-02-15 12:33:02 +0100677
678error:
Christian König76456702017-04-06 17:52:39 +0200679 mutex_unlock(&id_mgr->lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100680 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681}
682
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800683static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
684 struct amdgpu_vm *vm,
685 unsigned vmhub)
Alex Deucher93dcc372016-06-17 17:05:15 -0400686{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800687 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Alex Deucher93dcc372016-06-17 17:05:15 -0400688
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800689 mutex_lock(&id_mgr->lock);
690 if (vm->reserved_vmid[vmhub]) {
691 list_add(&vm->reserved_vmid[vmhub]->list,
692 &id_mgr->ids_lru);
693 vm->reserved_vmid[vmhub] = NULL;
Chunming Zhouc3505772017-04-21 15:51:04 +0800694 atomic_dec(&id_mgr->reserved_vmid_num);
Alex Deucher93dcc372016-06-17 17:05:15 -0400695 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800696 mutex_unlock(&id_mgr->lock);
Alex Deucher93dcc372016-06-17 17:05:15 -0400697}
698
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800699static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
700 struct amdgpu_vm *vm,
701 unsigned vmhub)
Alex Xiee60f8db2017-03-09 11:36:26 -0500702{
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800703 struct amdgpu_vm_id_manager *id_mgr;
704 struct amdgpu_vm_id *idle;
705 int r = 0;
Alex Xiee60f8db2017-03-09 11:36:26 -0500706
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800707 id_mgr = &adev->vm_manager.id_mgr[vmhub];
708 mutex_lock(&id_mgr->lock);
709 if (vm->reserved_vmid[vmhub])
710 goto unlock;
Chunming Zhouc3505772017-04-21 15:51:04 +0800711 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
712 AMDGPU_VM_MAX_RESERVED_VMID) {
713 DRM_ERROR("Over limitation of reserved vmid\n");
714 atomic_dec(&id_mgr->reserved_vmid_num);
715 r = -EINVAL;
716 goto unlock;
717 }
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800718 /* Select the first entry VMID */
719 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
720 list_del_init(&idle->list);
721 vm->reserved_vmid[vmhub] = idle;
722 mutex_unlock(&id_mgr->lock);
Alex Xiee60f8db2017-03-09 11:36:26 -0500723
Chunming Zhou1e9ef262017-04-20 16:18:48 +0800724 return 0;
725unlock:
726 mutex_unlock(&id_mgr->lock);
727 return r;
728}
729
Alex Xiee59c0202017-06-01 09:42:59 -0400730/**
731 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
732 *
733 * @adev: amdgpu_device pointer
734 */
735void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
736{
737 const struct amdgpu_ip_block *ip_block;
738 bool has_compute_vm_bug;
739 struct amdgpu_ring *ring;
740 int i;
741
742 has_compute_vm_bug = false;
743
Alex Deucher2990a1f2017-12-15 16:18:00 -0500744 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
Alex Xiee59c0202017-06-01 09:42:59 -0400745 if (ip_block) {
746 /* Compute has a VM bug for GFX version < 7.
747 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
748 if (ip_block->version->major <= 7)
749 has_compute_vm_bug = true;
750 else if (ip_block->version->major == 8)
751 if (adev->gfx.mec_fw_version < 673)
752 has_compute_vm_bug = true;
753 }
754
755 for (i = 0; i < adev->num_rings; i++) {
756 ring = adev->rings[i];
757 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
758 /* only compute rings */
759 ring->has_compute_vm_bug = has_compute_vm_bug;
760 else
761 ring->has_compute_vm_bug = false;
762 }
763}
764
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400765bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
766 struct amdgpu_job *job)
767{
768 struct amdgpu_device *adev = ring->adev;
769 unsigned vmhub = ring->funcs->vmhub;
770 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
771 struct amdgpu_vm_id *id;
772 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400773 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400774
775 if (job->vm_id == 0)
776 return false;
777 id = &id_mgr->ids[job->vm_id];
778 gds_switch_needed = ring->funcs->emit_gds_switch && (
779 id->gds_base != job->gds_base ||
780 id->gds_size != job->gds_size ||
781 id->gws_base != job->gws_base ||
782 id->gws_size != job->gws_size ||
783 id->oa_base != job->oa_base ||
784 id->oa_size != job->oa_size);
785
786 if (amdgpu_vm_had_gpu_reset(adev, id))
787 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400788
789 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400790}
791
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400792static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
793{
794 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500795}
796
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797/**
798 * amdgpu_vm_flush - hardware flush the vm
799 *
800 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100801 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100802 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400803 *
Christian König4ff37a82016-02-26 16:18:26 +0100804 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800806int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400807{
Christian König971fe9a92016-03-01 15:09:25 +0100808 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200809 unsigned vmhub = ring->funcs->vmhub;
810 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
811 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100812 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800813 id->gds_base != job->gds_base ||
814 id->gds_size != job->gds_size ||
815 id->gws_base != job->gws_base ||
816 id->gws_size != job->gws_size ||
817 id->oa_base != job->oa_base ||
818 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800819 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200820 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100821 int r;
Christian Königd564a062016-03-01 15:51:53 +0100822
Christian Königf7d015b2017-04-03 14:28:26 +0200823 if (amdgpu_vm_had_gpu_reset(adev, id)) {
824 gds_switch_needed = true;
825 vm_flush_needed = true;
826 }
Christian König971fe9a92016-03-01 15:09:25 +0100827
Monk Liu8fdf0742017-06-06 17:25:13 +0800828 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200829 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100830
Christian Königc0e51932017-04-03 14:16:07 +0200831 if (ring->funcs->init_cond_exec)
832 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100833
Monk Liu8fdf0742017-06-06 17:25:13 +0800834 if (need_pipe_sync)
835 amdgpu_ring_emit_pipeline_sync(ring);
836
Christian Königf7d015b2017-04-03 14:28:26 +0200837 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200838 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800839
Christian König9a94f5a2017-05-12 14:46:23 +0200840 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
841 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800842
Christian Königc0e51932017-04-03 14:16:07 +0200843 r = amdgpu_fence_emit(ring, &fence);
844 if (r)
845 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800846
Christian König76456702017-04-06 17:52:39 +0200847 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200848 dma_fence_put(id->last_flush);
849 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800850 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200851 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200852 }
Monk Liue9d672b2017-03-15 12:18:57 +0800853
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800854 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200855 id->gds_base = job->gds_base;
856 id->gds_size = job->gds_size;
857 id->gws_base = job->gws_base;
858 id->gws_size = job->gws_size;
859 id->oa_base = job->oa_base;
860 id->oa_size = job->oa_size;
861 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
862 job->gds_size, job->gws_base,
863 job->gws_size, job->oa_base,
864 job->oa_size);
865 }
866
867 if (ring->funcs->patch_cond_exec)
868 amdgpu_ring_patch_cond_exec(ring, patch_offset);
869
870 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
871 if (ring->funcs->emit_switch_buffer) {
872 amdgpu_ring_emit_switch_buffer(ring);
873 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 }
Christian König41d9eb22016-03-01 16:46:18 +0100875 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100876}
877
878/**
879 * amdgpu_vm_reset_id - reset VMID to zero
880 *
881 * @adev: amdgpu device structure
882 * @vm_id: vmid number to use
883 *
884 * Reset saved GDW, GWS and OA to force switch on next flush.
885 */
Christian König76456702017-04-06 17:52:39 +0200886void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
887 unsigned vmid)
Christian König971fe9a92016-03-01 15:09:25 +0100888{
Christian König76456702017-04-06 17:52:39 +0200889 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
890 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
Christian König971fe9a92016-03-01 15:09:25 +0100891
Christian Königb3c85a02017-05-10 20:06:58 +0200892 atomic64_set(&id->owner, 0);
Christian Königbcb1ba32016-03-08 15:40:11 +0100893 id->gds_base = 0;
894 id->gds_size = 0;
895 id->gws_base = 0;
896 id->gws_size = 0;
897 id->oa_base = 0;
898 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899}
900
901/**
Christian Königb3c85a02017-05-10 20:06:58 +0200902 * amdgpu_vm_reset_all_id - reset VMID to zero
903 *
904 * @adev: amdgpu device structure
905 *
906 * Reset VMID to force flush on next use
907 */
908void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
909{
910 unsigned i, j;
911
912 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
913 struct amdgpu_vm_id_manager *id_mgr =
914 &adev->vm_manager.id_mgr[i];
915
916 for (j = 1; j < id_mgr->num_ids; ++j)
917 amdgpu_vm_reset_id(adev, i, j);
918 }
919}
920
921/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
923 *
924 * @vm: requested vm
925 * @bo: requested buffer object
926 *
Christian König8843dbb2016-01-26 12:17:11 +0100927 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400928 * Search inside the @bos vm list for the requested vm
929 * Returns the found bo_va or NULL if none is found
930 *
931 * Object has to be reserved!
932 */
933struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
934 struct amdgpu_bo *bo)
935{
936 struct amdgpu_bo_va *bo_va;
937
Christian Königec681542017-08-01 10:51:43 +0200938 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
939 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940 return bo_va;
941 }
942 }
943 return NULL;
944}
945
946/**
Christian Königafef8b82016-08-12 13:29:18 +0200947 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948 *
Christian König29efc4f2016-08-04 14:52:50 +0200949 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 * @pe: addr of the page entry
951 * @addr: dst addr to write into pe
952 * @count: number of page entries to update
953 * @incr: increase next addr by incr bytes
954 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955 *
956 * Traces the parameters and calls the right asic functions
957 * to setup the page table using the DMA.
958 */
Christian Königafef8b82016-08-12 13:29:18 +0200959static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
960 uint64_t pe, uint64_t addr,
961 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800962 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963{
Christian Königec2f05f2016-09-25 16:11:52 +0200964 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965
Christian Königafef8b82016-08-12 13:29:18 +0200966 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200967 amdgpu_vm_write_pte(params->adev, params->ib, pe,
968 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969
970 } else {
Christian König27c5f362016-08-04 15:02:49 +0200971 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972 count, incr, flags);
973 }
974}
975
976/**
Christian Königafef8b82016-08-12 13:29:18 +0200977 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
978 *
979 * @params: see amdgpu_pte_update_params definition
980 * @pe: addr of the page entry
981 * @addr: dst addr to write into pe
982 * @count: number of page entries to update
983 * @incr: increase next addr by incr bytes
984 * @flags: hw access flags
985 *
986 * Traces the parameters and calls the DMA function to copy the PTEs.
987 */
988static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
989 uint64_t pe, uint64_t addr,
990 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800991 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200992{
Christian Königec2f05f2016-09-25 16:11:52 +0200993 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200994
Christian Königec2f05f2016-09-25 16:11:52 +0200995
996 trace_amdgpu_vm_copy_ptes(pe, src, count);
997
998 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200999}
1000
1001/**
Christian Königb07c9d22015-11-30 13:26:07 +01001002 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001003 *
Christian Königb07c9d22015-11-30 13:26:07 +01001004 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005 * @addr: the unmapped addr
1006 *
1007 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +01001008 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009 */
Christian Königde9ea7b2016-08-12 11:33:30 +02001010static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001011{
1012 uint64_t result;
1013
Christian Königde9ea7b2016-08-12 11:33:30 +02001014 /* page table offset */
1015 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016
Christian Königde9ea7b2016-08-12 11:33:30 +02001017 /* in case cpu page size != gpu page size*/
1018 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +01001019
1020 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001021
1022 return result;
1023}
1024
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001025/**
1026 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
1027 *
1028 * @params: see amdgpu_pte_update_params definition
1029 * @pe: kmap addr of the page entry
1030 * @addr: dst addr to write into pe
1031 * @count: number of page entries to update
1032 * @incr: increase next addr by incr bytes
1033 * @flags: hw access flags
1034 *
1035 * Write count number of PT/PD entries directly.
1036 */
1037static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
1038 uint64_t pe, uint64_t addr,
1039 unsigned count, uint32_t incr,
1040 uint64_t flags)
1041{
1042 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001043 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001044
Christian König03918b32017-07-11 17:15:37 +02001045 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
1046
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001047 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001048 value = params->pages_addr ?
1049 amdgpu_vm_map_gart(params->pages_addr, addr) :
1050 addr;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001051 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001052 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001053 addr += incr;
1054 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001055}
1056
Christian Königa33cab72017-07-11 17:13:00 +02001057static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1058 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001059{
1060 struct amdgpu_sync sync;
1061 int r;
1062
1063 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -04001064 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001065 r = amdgpu_sync_wait(&sync, true);
1066 amdgpu_sync_free(&sync);
1067
1068 return r;
1069}
1070
Christian Königf8991ba2016-09-16 15:36:49 +02001071/*
Christian König6989f242017-11-30 19:08:05 +01001072 * amdgpu_vm_update_pde - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +02001073 *
Christian König6989f242017-11-30 19:08:05 +01001074 * @param: parameters for the update
Christian Königf8991ba2016-09-16 15:36:49 +02001075 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +02001076 * @parent: parent directory
Christian König6989f242017-11-30 19:08:05 +01001077 * @entry: entry to update
Christian Königf8991ba2016-09-16 15:36:49 +02001078 *
Christian König6989f242017-11-30 19:08:05 +01001079 * Makes sure the requested entry in parent is up to date.
Christian Königf8991ba2016-09-16 15:36:49 +02001080 */
Christian König6989f242017-11-30 19:08:05 +01001081static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
1082 struct amdgpu_vm *vm,
1083 struct amdgpu_vm_pt *parent,
1084 struct amdgpu_vm_pt *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001085{
Christian König3de676d2017-11-29 13:27:26 +01001086 struct amdgpu_bo *bo = entry->base.bo, *shadow = NULL, *pbo;
Harish Kasiviswanathana19240052017-06-09 17:47:28 -04001087 uint64_t pd_addr, shadow_addr = 0;
Christian König3de676d2017-11-29 13:27:26 +01001088 uint64_t pde, pt, flags;
1089 unsigned level;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001090
Christian König6989f242017-11-30 19:08:05 +01001091 /* Don't update huge pages here */
1092 if (entry->huge)
1093 return;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001094
Alex Deucher69277982017-07-13 15:37:11 -04001095 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02001096 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001097 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001098 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Christian König6989f242017-11-30 19:08:05 +01001099 shadow = parent->base.bo->shadow;
Christian Königb852f3d2017-11-30 15:19:50 +01001100 if (shadow)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001101 shadow_addr = amdgpu_bo_gpu_offset(shadow);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04001102 }
1103
Christian König3de676d2017-11-29 13:27:26 +01001104 for (level = 0, pbo = parent->base.bo->parent; pbo; ++level)
1105 pbo = pbo->parent;
1106
Chunming Zhou196f7482017-12-13 14:22:54 +08001107 level += params->adev->vm_manager.root_level;
Christian Königb852f3d2017-11-30 15:19:50 +01001108 pt = amdgpu_bo_gpu_offset(bo);
Christian König3de676d2017-11-29 13:27:26 +01001109 flags = AMDGPU_PTE_VALID;
1110 amdgpu_gart_get_vm_pde(params->adev, level, &pt, &flags);
Christian Königb852f3d2017-11-30 15:19:50 +01001111 if (shadow) {
1112 pde = shadow_addr + (entry - parent->entries) * 8;
Christian König3de676d2017-11-29 13:27:26 +01001113 params->func(params, pde, pt, 1, 0, flags);
Christian Königb852f3d2017-11-30 15:19:50 +01001114 }
1115
1116 pde = pd_addr + (entry - parent->entries) * 8;
Christian König3de676d2017-11-29 13:27:26 +01001117 params->func(params, pde, pt, 1, 0, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118}
1119
Christian König194d2162016-10-12 15:13:52 +02001120/*
Christian König92456b92017-05-12 16:09:26 +02001121 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1122 *
1123 * @parent: parent PD
1124 *
1125 * Mark all PD level as invalid after an error.
1126 */
Christian König8f19cd72017-11-30 15:28:03 +01001127static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
1128 struct amdgpu_vm *vm,
1129 struct amdgpu_vm_pt *parent,
1130 unsigned level)
Christian König92456b92017-05-12 16:09:26 +02001131{
Christian König8f19cd72017-11-30 15:28:03 +01001132 unsigned pt_idx, num_entries;
Christian König92456b92017-05-12 16:09:26 +02001133
1134 /*
1135 * Recurse into the subdirectories. This recursion is harmless because
1136 * we only have a maximum of 5 layers.
1137 */
Christian König8f19cd72017-11-30 15:28:03 +01001138 num_entries = amdgpu_vm_num_entries(adev, level);
1139 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
Christian König92456b92017-05-12 16:09:26 +02001140 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1141
Christian König3f3333f2017-08-03 14:02:13 +02001142 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001143 continue;
1144
Christian Königea097292017-08-09 14:15:46 +02001145 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001146 if (list_empty(&entry->base.vm_status))
1147 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +02001148 spin_unlock(&vm->status_lock);
Christian König8f19cd72017-11-30 15:28:03 +01001149 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
Christian König92456b92017-05-12 16:09:26 +02001150 }
1151}
1152
1153/*
Christian König194d2162016-10-12 15:13:52 +02001154 * amdgpu_vm_update_directories - make sure that all directories are valid
1155 *
1156 * @adev: amdgpu_device pointer
1157 * @vm: requested vm
1158 *
1159 * Makes sure all directories are up to date.
1160 * Returns 0 for success, error for failure.
1161 */
1162int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1163 struct amdgpu_vm *vm)
1164{
Christian König6989f242017-11-30 19:08:05 +01001165 struct amdgpu_pte_update_params params;
1166 struct amdgpu_job *job;
1167 unsigned ndw = 0;
Dan Carpenter78aa02c2017-09-30 11:14:13 +03001168 int r = 0;
Christian König92456b92017-05-12 16:09:26 +02001169
Christian König6989f242017-11-30 19:08:05 +01001170 if (list_empty(&vm->relocated))
1171 return 0;
1172
1173restart:
1174 memset(&params, 0, sizeof(params));
1175 params.adev = adev;
1176
1177 if (vm->use_cpu_for_update) {
1178 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
1179 if (unlikely(r))
1180 return r;
1181
1182 params.func = amdgpu_vm_cpu_set_ptes;
1183 } else {
1184 ndw = 512 * 8;
1185 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1186 if (r)
1187 return r;
1188
1189 params.ib = &job->ibs[0];
1190 params.func = amdgpu_vm_do_set_ptes;
1191 }
1192
Christian Königea097292017-08-09 14:15:46 +02001193 spin_lock(&vm->status_lock);
1194 while (!list_empty(&vm->relocated)) {
Christian König6989f242017-11-30 19:08:05 +01001195 struct amdgpu_vm_bo_base *bo_base, *parent;
1196 struct amdgpu_vm_pt *pt, *entry;
Christian Königea097292017-08-09 14:15:46 +02001197 struct amdgpu_bo *bo;
1198
1199 bo_base = list_first_entry(&vm->relocated,
1200 struct amdgpu_vm_bo_base,
1201 vm_status);
Christian König6989f242017-11-30 19:08:05 +01001202 list_del_init(&bo_base->vm_status);
Christian Königea097292017-08-09 14:15:46 +02001203 spin_unlock(&vm->status_lock);
1204
1205 bo = bo_base->bo->parent;
Christian König6989f242017-11-30 19:08:05 +01001206 if (!bo) {
Christian Königea097292017-08-09 14:15:46 +02001207 spin_lock(&vm->status_lock);
Christian König6989f242017-11-30 19:08:05 +01001208 continue;
Christian Königea097292017-08-09 14:15:46 +02001209 }
Christian König6989f242017-11-30 19:08:05 +01001210
1211 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
1212 bo_list);
1213 pt = container_of(parent, struct amdgpu_vm_pt, base);
1214 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
1215
1216 amdgpu_vm_update_pde(&params, vm, pt, entry);
1217
1218 spin_lock(&vm->status_lock);
1219 if (!vm->use_cpu_for_update &&
1220 (ndw - params.ib->length_dw) < 32)
1221 break;
Christian Königea097292017-08-09 14:15:46 +02001222 }
1223 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +02001224
Christian König68c62302017-07-11 17:23:29 +02001225 if (vm->use_cpu_for_update) {
1226 /* Flush HDP */
1227 mb();
1228 amdgpu_gart_flush_gpu_tlb(adev, 0);
Christian König6989f242017-11-30 19:08:05 +01001229 } else if (params.ib->length_dw == 0) {
1230 amdgpu_job_free(job);
1231 } else {
1232 struct amdgpu_bo *root = vm->root.base.bo;
1233 struct amdgpu_ring *ring;
1234 struct dma_fence *fence;
1235
1236 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1237 sched);
1238
1239 amdgpu_ring_pad_ib(ring, params.ib);
1240 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
1241 AMDGPU_FENCE_OWNER_VM, false);
1242 if (root->shadow)
1243 amdgpu_sync_resv(adev, &job->sync,
1244 root->shadow->tbo.resv,
1245 AMDGPU_FENCE_OWNER_VM, false);
1246
1247 WARN_ON(params.ib->length_dw > ndw);
1248 r = amdgpu_job_submit(job, ring, &vm->entity,
1249 AMDGPU_FENCE_OWNER_VM, &fence);
1250 if (r)
1251 goto error;
1252
1253 amdgpu_bo_fence(root, fence, true);
1254 dma_fence_put(vm->last_update);
1255 vm->last_update = fence;
Christian König68c62302017-07-11 17:23:29 +02001256 }
1257
Christian König6989f242017-11-30 19:08:05 +01001258 if (!list_empty(&vm->relocated))
1259 goto restart;
1260
1261 return 0;
1262
1263error:
Chunming Zhou196f7482017-12-13 14:22:54 +08001264 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
1265 adev->vm_manager.root_level);
Christian König6989f242017-11-30 19:08:05 +01001266 amdgpu_job_free(job);
Christian König92456b92017-05-12 16:09:26 +02001267 return r;
Christian König194d2162016-10-12 15:13:52 +02001268}
1269
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001271 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001272 *
1273 * @p: see amdgpu_pte_update_params definition
1274 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001275 * @entry: resulting entry or NULL
1276 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001277 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001278 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001279 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001280void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1281 struct amdgpu_vm_pt **entry,
1282 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001283{
Chunming Zhou196f7482017-12-13 14:22:54 +08001284 unsigned level = p->adev->vm_manager.root_level;
Christian König4e2cb642016-10-25 15:52:28 +02001285
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001286 *parent = NULL;
1287 *entry = &p->vm->root;
1288 while ((*entry)->entries) {
Christian Könige3a1b322017-12-01 13:28:46 +01001289 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
Christian König50783142017-11-27 14:01:51 +01001290
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001291 *parent = *entry;
Christian Könige3a1b322017-12-01 13:28:46 +01001292 *entry = &(*entry)->entries[addr >> shift];
1293 addr &= (1ULL << shift) - 1;
Christian König4e2cb642016-10-25 15:52:28 +02001294 }
1295
Chunming Zhou196f7482017-12-13 14:22:54 +08001296 if (level != AMDGPU_VM_PTB)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001297 *entry = NULL;
1298}
Christian König4e2cb642016-10-25 15:52:28 +02001299
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001300/**
1301 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1302 *
1303 * @p: see amdgpu_pte_update_params definition
1304 * @entry: vm_pt entry to check
1305 * @parent: parent entry
1306 * @nptes: number of PTEs updated with this operation
1307 * @dst: destination address where the PTEs should point to
1308 * @flags: access flags fro the PTEs
1309 *
1310 * Check if we can update the PD with a huge page.
1311 */
Christian Königec5207c2017-08-03 19:24:06 +02001312static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1313 struct amdgpu_vm_pt *entry,
1314 struct amdgpu_vm_pt *parent,
1315 unsigned nptes, uint64_t dst,
1316 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001317{
1318 bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
1319 uint64_t pd_addr, pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001320
1321 /* In the case of a mixed PT the PDE must point to it*/
1322 if (p->adev->asic_type < CHIP_VEGA10 ||
1323 nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
Felix Kuehlingb2529032017-08-17 16:37:49 -04001324 p->src ||
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001325 !(flags & AMDGPU_PTE_VALID)) {
1326
Christian König3f3333f2017-08-03 14:02:13 +02001327 dst = amdgpu_bo_gpu_offset(entry->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001328 flags = AMDGPU_PTE_VALID;
1329 } else {
Christian König4ab40162017-08-03 20:30:50 +02001330 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001331 flags |= AMDGPU_PDE_PTE;
1332 }
1333
Christian König78eb2f02017-11-30 15:41:28 +01001334 if (!entry->huge && !(flags & AMDGPU_PDE_PTE))
Christian Königec5207c2017-08-03 19:24:06 +02001335 return;
Christian König78eb2f02017-11-30 15:41:28 +01001336 entry->huge = !!(flags & AMDGPU_PDE_PTE);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001337
Chunming Zhou196f7482017-12-13 14:22:54 +08001338 amdgpu_gart_get_vm_pde(p->adev, AMDGPU_VM_PDB0,
Christian König3de676d2017-11-29 13:27:26 +01001339 &dst, &flags);
1340
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001341 if (use_cpu_update) {
Felix Kuehlingb2529032017-08-17 16:37:49 -04001342 /* In case a huge page is replaced with a system
1343 * memory mapping, p->pages_addr != NULL and
1344 * amdgpu_vm_cpu_set_ptes would try to translate dst
1345 * through amdgpu_vm_map_gart. But dst is already a
1346 * GPU address (of the page table). Disable
1347 * amdgpu_vm_map_gart temporarily.
1348 */
1349 dma_addr_t *tmp;
1350
1351 tmp = p->pages_addr;
1352 p->pages_addr = NULL;
1353
Christian König3f3333f2017-08-03 14:02:13 +02001354 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001355 pde = pd_addr + (entry - parent->entries) * 8;
1356 amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
Felix Kuehlingb2529032017-08-17 16:37:49 -04001357
1358 p->pages_addr = tmp;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001359 } else {
Christian König3f3333f2017-08-03 14:02:13 +02001360 if (parent->base.bo->shadow) {
1361 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001362 pde = pd_addr + (entry - parent->entries) * 8;
1363 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1364 }
Christian König3f3333f2017-08-03 14:02:13 +02001365 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001366 pde = pd_addr + (entry - parent->entries) * 8;
1367 amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
1368 }
Christian König4e2cb642016-10-25 15:52:28 +02001369}
1370
1371/**
Christian König92696dd2016-08-05 13:56:35 +02001372 * amdgpu_vm_update_ptes - make sure that page tables are valid
1373 *
1374 * @params: see amdgpu_pte_update_params definition
1375 * @vm: requested vm
1376 * @start: start of GPU address range
1377 * @end: end of GPU address range
1378 * @dst: destination address to map to, the next dst inside the function
1379 * @flags: mapping flags
1380 *
1381 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001382 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001383 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001384static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001385 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001386 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001387{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001388 struct amdgpu_device *adev = params->adev;
1389 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001390
Christian König301654a2017-05-16 14:30:27 +02001391 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001392 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001393 unsigned nptes;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001394 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
Christian König92696dd2016-08-05 13:56:35 +02001395
1396 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001397 for (addr = start; addr < end; addr += nptes,
1398 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1399 struct amdgpu_vm_pt *entry, *parent;
1400
1401 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1402 if (!entry)
1403 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001404
Christian König92696dd2016-08-05 13:56:35 +02001405 if ((addr & ~mask) == (end & ~mask))
1406 nptes = end - addr;
1407 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001408 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001409
Christian Königec5207c2017-08-03 19:24:06 +02001410 amdgpu_vm_handle_huge_pages(params, entry, parent,
1411 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001412 /* We don't need to update PTEs for huge pages */
Christian König78eb2f02017-11-30 15:41:28 +01001413 if (entry->huge)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001414 continue;
1415
Christian König3f3333f2017-08-03 14:02:13 +02001416 pt = entry->base.bo;
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001417 if (use_cpu_update) {
Christian Königf5e1c742017-07-20 23:45:18 +02001418 pe_start = (unsigned long)amdgpu_bo_kptr(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001419 } else {
1420 if (pt->shadow) {
1421 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1422 pe_start += (addr & mask) * 8;
1423 params->func(params, pe_start, dst, nptes,
1424 AMDGPU_GPU_PAGE_SIZE, flags);
1425 }
Harish Kasiviswanathan370f0922017-06-09 17:47:27 -04001426 pe_start = amdgpu_bo_gpu_offset(pt);
Christian Königdd0792c2017-06-27 14:48:15 -04001427 }
Christian König92696dd2016-08-05 13:56:35 +02001428
Christian König301654a2017-05-16 14:30:27 +02001429 pe_start += (addr & mask) * 8;
Christian König301654a2017-05-16 14:30:27 +02001430 params->func(params, pe_start, dst, nptes,
1431 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001432 }
1433
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001434 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001435}
1436
1437/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001438 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1439 *
Christian König29efc4f2016-08-04 14:52:50 +02001440 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001441 * @vm: requested vm
1442 * @start: first PTE to handle
1443 * @end: last PTE to handle
1444 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001445 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001446 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001448static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001449 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001450 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001451{
1452 /**
1453 * The MC L1 TLB supports variable sized pages, based on a fragment
1454 * field in the PTE. When this field is set to a non-zero value, page
1455 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1456 * flags are considered valid for all PTEs within the fragment range
1457 * and corresponding mappings are assumed to be physically contiguous.
1458 *
1459 * The L1 TLB can store a single PTE for the whole fragment,
1460 * significantly increasing the space available for translation
1461 * caching. This leads to large improvements in throughput when the
1462 * TLB is under pressure.
1463 *
1464 * The L2 TLB distributes small and large fragments into two
1465 * asymmetric partitions. The large fragment cache is significantly
1466 * larger. Thus, we try to use large fragments wherever possible.
1467 * Userspace can support this by aligning virtual base address and
1468 * allocation size to the fragment size.
1469 */
Roger He6849d472017-08-30 13:01:19 +08001470 unsigned max_frag = params->adev->vm_manager.fragment_size;
1471 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001472
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001473 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001474 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001475 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476
Roger He6849d472017-08-30 13:01:19 +08001477 while (start != end) {
1478 uint64_t frag_flags, frag_end;
1479 unsigned frag;
1480
1481 /* This intentionally wraps around if no bit is set */
1482 frag = min((unsigned)ffs(start) - 1,
1483 (unsigned)fls64(end - start) - 1);
1484 if (frag >= max_frag) {
1485 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1486 frag_end = end & ~((1ULL << max_frag) - 1);
1487 } else {
1488 frag_flags = AMDGPU_PTE_FRAG(frag);
1489 frag_end = start + (1 << frag);
1490 }
1491
1492 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1493 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001494 if (r)
1495 return r;
Roger He6849d472017-08-30 13:01:19 +08001496
1497 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1498 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001499 }
1500
Roger He6849d472017-08-30 13:01:19 +08001501 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001502}
1503
1504/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1506 *
1507 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001508 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001509 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001510 * @vm: requested vm
1511 * @start: start of mapped range
1512 * @last: last mapped entry
1513 * @flags: flags for the entries
1514 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001515 * @fence: optional resulting fence
1516 *
Christian Königa14faa62016-01-25 14:27:31 +01001517 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001518 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001519 */
1520static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001521 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001522 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001524 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001525 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001526 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001527{
Christian König2d55e452016-02-08 17:37:38 +01001528 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001529 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001530 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001531 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001532 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001533 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001534 int r;
1535
Christian Königafef8b82016-08-12 13:29:18 +02001536 memset(&params, 0, sizeof(params));
1537 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001538 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001539
Christian Königa33cab72017-07-11 17:13:00 +02001540 /* sync to everything on unmapping */
1541 if (!(flags & AMDGPU_PTE_VALID))
1542 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1543
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001544 if (vm->use_cpu_for_update) {
1545 /* params.src is used as flag to indicate system Memory */
1546 if (pages_addr)
1547 params.src = ~0;
1548
1549 /* Wait for PT BOs to be free. PTs share the same resv. object
1550 * as the root PD BO
1551 */
Christian Königa33cab72017-07-11 17:13:00 +02001552 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001553 if (unlikely(r))
1554 return r;
1555
1556 params.func = amdgpu_vm_cpu_set_ptes;
1557 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001558 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1559 addr, flags);
1560 }
1561
Christian König2d55e452016-02-08 17:37:38 +01001562 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001563
Christian Königa14faa62016-01-25 14:27:31 +01001564 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001565
1566 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001567 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001568 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001569 *
1570 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571 */
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001572 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001573
1574 /* padding, etc. */
1575 ndw = 64;
1576
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001577 /* one PDE write for each huge page */
1578 ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
1579
Christian König570144c2017-08-30 15:38:45 +02001580 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001581 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001582 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583
Christian Königb0456f92016-08-11 14:06:54 +02001584 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585 ndw += nptes * 2;
1586
Christian Königafef8b82016-08-12 13:29:18 +02001587 params.func = amdgpu_vm_do_copy_ptes;
1588
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 } else {
1590 /* set page commands needed */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001591 ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592
Roger He6849d472017-08-30 13:01:19 +08001593 /* extra commands for begin/end fragments */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001594 ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
1595 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001596
1597 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598 }
1599
Christian Königd71518b2016-02-01 12:20:25 +01001600 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1601 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001603
Christian König29efc4f2016-08-04 14:52:50 +02001604 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001605
Christian König570144c2017-08-30 15:38:45 +02001606 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001607 uint64_t *pte;
1608 unsigned i;
1609
1610 /* Put the PTEs at the end of the IB. */
1611 i = ndw - nptes * 2;
1612 pte= (uint64_t *)&(job->ibs->ptr[i]);
1613 params.src = job->ibs->gpu_addr + i * 4;
1614
1615 for (i = 0; i < nptes; ++i) {
1616 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1617 AMDGPU_GPU_PAGE_SIZE);
1618 pte[i] |= flags;
1619 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001620 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001621 }
1622
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -05001623 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
Christian König3cabaa52016-06-06 10:17:58 +02001624 if (r)
1625 goto error_free;
1626
Christian König3f3333f2017-08-03 14:02:13 +02001627 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001628 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001629 if (r)
1630 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001631
Christian König3f3333f2017-08-03 14:02:13 +02001632 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001633 if (r)
1634 goto error_free;
1635
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001636 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1637 if (r)
1638 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001639
Christian König29efc4f2016-08-04 14:52:50 +02001640 amdgpu_ring_pad_ib(ring, params.ib);
1641 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001642 r = amdgpu_job_submit(job, ring, &vm->entity,
1643 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001644 if (r)
1645 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646
Christian König3f3333f2017-08-03 14:02:13 +02001647 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001648 dma_fence_put(*fence);
1649 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001651
1652error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001653 amdgpu_job_free(job);
Chunming Zhou196f7482017-12-13 14:22:54 +08001654 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
1655 adev->vm_manager.root_level);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001656 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001657}
1658
1659/**
Christian Königa14faa62016-01-25 14:27:31 +01001660 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1661 *
1662 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001663 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001664 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001665 * @vm: requested vm
1666 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001667 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001668 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001669 * @fence: optional resulting fence
1670 *
1671 * Split the mapping into smaller chunks so that each update fits
1672 * into a SDMA IB.
1673 * Returns 0 for success, -EINVAL for failure.
1674 */
1675static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001676 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001677 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001678 struct amdgpu_vm *vm,
1679 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001680 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001681 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001682 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001683{
Christian König9fc8fc72017-09-18 13:58:30 +02001684 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001685 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001686 int r;
1687
1688 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1689 * but in case of something, we filter the flags in first place
1690 */
1691 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1692 flags &= ~AMDGPU_PTE_READABLE;
1693 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1694 flags &= ~AMDGPU_PTE_WRITEABLE;
1695
Alex Xie15b31c52017-03-03 16:47:11 -05001696 flags &= ~AMDGPU_PTE_EXECUTABLE;
1697 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1698
Alex Xieb0fd18b2017-03-03 16:49:39 -05001699 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1700 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1701
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001702 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1703 (adev->asic_type >= CHIP_VEGA10)) {
1704 flags |= AMDGPU_PTE_PRT;
1705 flags &= ~AMDGPU_PTE_VALID;
1706 }
1707
Christian Königa14faa62016-01-25 14:27:31 +01001708 trace_amdgpu_vm_bo_update(mapping);
1709
Christian König63e0ba42016-08-16 17:38:37 +02001710 pfn = mapping->offset >> PAGE_SHIFT;
1711 if (nodes) {
1712 while (pfn >= nodes->size) {
1713 pfn -= nodes->size;
1714 ++nodes;
1715 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001716 }
Christian Königa14faa62016-01-25 14:27:31 +01001717
Christian König63e0ba42016-08-16 17:38:37 +02001718 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001719 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001720 uint64_t max_entries;
1721 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001722
Christian König63e0ba42016-08-16 17:38:37 +02001723 if (nodes) {
1724 addr = nodes->start << PAGE_SHIFT;
1725 max_entries = (nodes->size - pfn) *
1726 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1727 } else {
1728 addr = 0;
1729 max_entries = S64_MAX;
1730 }
Christian Königa14faa62016-01-25 14:27:31 +01001731
Christian König63e0ba42016-08-16 17:38:37 +02001732 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001733 uint64_t count;
1734
Christian König457e0fe2017-08-22 12:50:46 +02001735 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001736 for (count = 1; count < max_entries; ++count) {
1737 uint64_t idx = pfn + count;
1738
1739 if (pages_addr[idx] !=
1740 (pages_addr[idx - 1] + PAGE_SIZE))
1741 break;
1742 }
1743
1744 if (count < min_linear_pages) {
1745 addr = pfn << PAGE_SHIFT;
1746 dma_addr = pages_addr;
1747 } else {
1748 addr = pages_addr[pfn];
1749 max_entries = count;
1750 }
1751
Christian König63e0ba42016-08-16 17:38:37 +02001752 } else if (flags & AMDGPU_PTE_VALID) {
1753 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001754 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001755 }
Christian König63e0ba42016-08-16 17:38:37 +02001756
Christian Königa9f87f62017-03-30 14:03:59 +02001757 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001758 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001759 start, last, flags, addr,
1760 fence);
1761 if (r)
1762 return r;
1763
Christian König63e0ba42016-08-16 17:38:37 +02001764 pfn += last - start + 1;
1765 if (nodes && nodes->size == pfn) {
1766 pfn = 0;
1767 ++nodes;
1768 }
Christian Königa14faa62016-01-25 14:27:31 +01001769 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001770
Christian Königa9f87f62017-03-30 14:03:59 +02001771 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001772
1773 return 0;
1774}
1775
1776/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001777 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1778 *
1779 * @adev: amdgpu_device pointer
1780 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001781 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001782 *
1783 * Fill in the page table entries for @bo_va.
1784 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785 */
1786int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1787 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001788 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789{
Christian Königec681542017-08-01 10:51:43 +02001790 struct amdgpu_bo *bo = bo_va->base.bo;
1791 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001792 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001793 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001794 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001795 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001796 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001797 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001798 int r;
1799
Christian Königec681542017-08-01 10:51:43 +02001800 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001801 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001802 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001803 exclusive = NULL;
1804 } else {
Christian König8358dce2016-03-30 10:50:25 +02001805 struct ttm_dma_tt *ttm;
1806
Christian Königec681542017-08-01 10:51:43 +02001807 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001808 nodes = mem->mm_node;
1809 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001810 ttm = container_of(bo_va->base.bo->tbo.ttm,
1811 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001812 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001813 }
Christian Königec681542017-08-01 10:51:43 +02001814 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 }
1816
Christian König457e0fe2017-08-22 12:50:46 +02001817 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001818 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001819 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001820 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001821
Christian König4e55eb32017-09-11 16:54:59 +02001822 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1823 last_update = &vm->last_update;
1824 else
1825 last_update = &bo_va->last_pt_update;
1826
Christian König3d7d4d32017-08-23 16:13:33 +02001827 if (!clear && bo_va->base.moved) {
1828 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001829 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001830
Christian Königcb7b6ec2017-08-15 17:08:12 +02001831 } else if (bo_va->cleared != clear) {
1832 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001833 }
Christian König7fc11952015-07-30 11:53:42 +02001834
1835 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001836 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001837 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001838 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001839 if (r)
1840 return r;
1841 }
1842
Christian König68c62302017-07-11 17:23:29 +02001843 if (vm->use_cpu_for_update) {
1844 /* Flush HDP */
1845 mb();
1846 amdgpu_gart_flush_gpu_tlb(adev, 0);
1847 }
1848
Christian Königcb7b6ec2017-08-15 17:08:12 +02001849 spin_lock(&vm->status_lock);
1850 list_del_init(&bo_va->base.vm_status);
1851 spin_unlock(&vm->status_lock);
1852
1853 list_splice_init(&bo_va->invalids, &bo_va->valids);
1854 bo_va->cleared = clear;
1855
1856 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1857 list_for_each_entry(mapping, &bo_va->valids, list)
1858 trace_amdgpu_vm_bo_mapping(mapping);
1859 }
1860
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001861 return 0;
1862}
1863
1864/**
Christian König284710f2017-01-30 11:09:31 +01001865 * amdgpu_vm_update_prt_state - update the global PRT state
1866 */
1867static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1868{
1869 unsigned long flags;
1870 bool enable;
1871
1872 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001873 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König284710f2017-01-30 11:09:31 +01001874 adev->gart.gart_funcs->set_prt(adev, enable);
1875 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1876}
1877
1878/**
Christian König4388fc22017-03-13 10:13:36 +01001879 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001880 */
1881static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1882{
Christian König4388fc22017-03-13 10:13:36 +01001883 if (!adev->gart.gart_funcs->set_prt)
1884 return;
1885
Christian König451bc8e2017-02-14 16:02:52 +01001886 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1887 amdgpu_vm_update_prt_state(adev);
1888}
1889
1890/**
Christian König0b15f2f2017-02-14 15:47:03 +01001891 * amdgpu_vm_prt_put - drop a PRT user
1892 */
1893static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1894{
Christian König451bc8e2017-02-14 16:02:52 +01001895 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001896 amdgpu_vm_update_prt_state(adev);
1897}
1898
1899/**
Christian König451bc8e2017-02-14 16:02:52 +01001900 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001901 */
1902static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1903{
1904 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1905
Christian König0b15f2f2017-02-14 15:47:03 +01001906 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001907 kfree(cb);
1908}
1909
1910/**
Christian König451bc8e2017-02-14 16:02:52 +01001911 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1912 */
1913static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1914 struct dma_fence *fence)
1915{
Christian König4388fc22017-03-13 10:13:36 +01001916 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001917
Christian König4388fc22017-03-13 10:13:36 +01001918 if (!adev->gart.gart_funcs->set_prt)
1919 return;
1920
1921 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001922 if (!cb) {
1923 /* Last resort when we are OOM */
1924 if (fence)
1925 dma_fence_wait(fence, false);
1926
Dan Carpenter486a68f2017-04-03 21:41:39 +03001927 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001928 } else {
1929 cb->adev = adev;
1930 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1931 amdgpu_vm_prt_cb))
1932 amdgpu_vm_prt_cb(fence, &cb->cb);
1933 }
1934}
1935
1936/**
Christian König284710f2017-01-30 11:09:31 +01001937 * amdgpu_vm_free_mapping - free a mapping
1938 *
1939 * @adev: amdgpu_device pointer
1940 * @vm: requested vm
1941 * @mapping: mapping to be freed
1942 * @fence: fence of the unmap operation
1943 *
1944 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1945 */
1946static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1947 struct amdgpu_vm *vm,
1948 struct amdgpu_bo_va_mapping *mapping,
1949 struct dma_fence *fence)
1950{
Christian König451bc8e2017-02-14 16:02:52 +01001951 if (mapping->flags & AMDGPU_PTE_PRT)
1952 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001953 kfree(mapping);
1954}
1955
1956/**
Christian König451bc8e2017-02-14 16:02:52 +01001957 * amdgpu_vm_prt_fini - finish all prt mappings
1958 *
1959 * @adev: amdgpu_device pointer
1960 * @vm: requested vm
1961 *
1962 * Register a cleanup callback to disable PRT support after VM dies.
1963 */
1964static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1965{
Christian König3f3333f2017-08-03 14:02:13 +02001966 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001967 struct dma_fence *excl, **shared;
1968 unsigned i, shared_count;
1969 int r;
1970
1971 r = reservation_object_get_fences_rcu(resv, &excl,
1972 &shared_count, &shared);
1973 if (r) {
1974 /* Not enough memory to grab the fence list, as last resort
1975 * block for all the fences to complete.
1976 */
1977 reservation_object_wait_timeout_rcu(resv, true, false,
1978 MAX_SCHEDULE_TIMEOUT);
1979 return;
1980 }
1981
1982 /* Add a callback for each fence in the reservation object */
1983 amdgpu_vm_prt_get(adev);
1984 amdgpu_vm_add_prt_cb(adev, excl);
1985
1986 for (i = 0; i < shared_count; ++i) {
1987 amdgpu_vm_prt_get(adev);
1988 amdgpu_vm_add_prt_cb(adev, shared[i]);
1989 }
1990
1991 kfree(shared);
1992}
1993
1994/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001995 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1996 *
1997 * @adev: amdgpu_device pointer
1998 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001999 * @fence: optional resulting fence (unchanged if no work needed to be done
2000 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002001 *
2002 * Make sure all freed BOs are cleared in the PT.
2003 * Returns 0 for success.
2004 *
2005 * PTs have to be reserved and mutex must be locked!
2006 */
2007int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002008 struct amdgpu_vm *vm,
2009 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010{
2011 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002012 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002014 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015
2016 while (!list_empty(&vm->freed)) {
2017 mapping = list_first_entry(&vm->freed,
2018 struct amdgpu_bo_va_mapping, list);
2019 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01002020
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002021 if (vm->pte_support_ats)
Yong Zhao6d16dac2017-08-31 15:55:00 -04002022 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002023
Christian König570144c2017-08-30 15:38:45 +02002024 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02002025 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002026 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002027 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01002028 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002029 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030 return r;
Christian König284710f2017-01-30 11:09:31 +01002031 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002032 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01002033
2034 if (fence && f) {
2035 dma_fence_put(*fence);
2036 *fence = f;
2037 } else {
2038 dma_fence_put(f);
2039 }
2040
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002041 return 0;
2042
2043}
2044
2045/**
Christian König73fb16e2017-08-16 11:13:48 +02002046 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002047 *
2048 * @adev: amdgpu_device pointer
2049 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02002050 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002051 *
Christian König73fb16e2017-08-16 11:13:48 +02002052 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002053 * Returns 0 for success.
2054 *
Christian König73fb16e2017-08-16 11:13:48 +02002055 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002056 */
Christian König73fb16e2017-08-16 11:13:48 +02002057int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02002058 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059{
Christian König73fb16e2017-08-16 11:13:48 +02002060 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02002061 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002062
2063 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002064 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02002065 struct amdgpu_bo_va *bo_va;
2066
Christian König27c7b9a2017-08-01 11:27:36 +02002067 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02002068 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01002070
Christian König73fb16e2017-08-16 11:13:48 +02002071 /* Per VM BOs never need to bo cleared in the page tables */
2072 clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
2073
2074 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075 if (r)
2076 return r;
2077
2078 spin_lock(&vm->status_lock);
2079 }
2080 spin_unlock(&vm->status_lock);
2081
Christian König91e1a522015-07-06 22:06:40 +02002082 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002083}
2084
2085/**
2086 * amdgpu_vm_bo_add - add a bo to a specific vm
2087 *
2088 * @adev: amdgpu_device pointer
2089 * @vm: requested vm
2090 * @bo: amdgpu buffer object
2091 *
Christian König8843dbb2016-01-26 12:17:11 +01002092 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002093 * Add @bo to the list of bos associated with the vm
2094 * Returns newly added bo_va or NULL for failure
2095 *
2096 * Object has to be reserved!
2097 */
2098struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2099 struct amdgpu_vm *vm,
2100 struct amdgpu_bo *bo)
2101{
2102 struct amdgpu_bo_va *bo_va;
2103
2104 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2105 if (bo_va == NULL) {
2106 return NULL;
2107 }
Christian Königec681542017-08-01 10:51:43 +02002108 bo_va->base.vm = vm;
2109 bo_va->base.bo = bo;
2110 INIT_LIST_HEAD(&bo_va->base.bo_list);
2111 INIT_LIST_HEAD(&bo_va->base.vm_status);
2112
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002113 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002114 INIT_LIST_HEAD(&bo_va->valids);
2115 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002116
Christian Königa5f6b5b2017-01-30 11:01:38 +01002117 if (bo)
Christian Königec681542017-08-01 10:51:43 +02002118 list_add_tail(&bo_va->base.bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002119
2120 return bo_va;
2121}
2122
Christian König73fb16e2017-08-16 11:13:48 +02002123
2124/**
2125 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2126 *
2127 * @adev: amdgpu_device pointer
2128 * @bo_va: bo_va to store the address
2129 * @mapping: the mapping to insert
2130 *
2131 * Insert a new mapping into all structures.
2132 */
2133static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2134 struct amdgpu_bo_va *bo_va,
2135 struct amdgpu_bo_va_mapping *mapping)
2136{
2137 struct amdgpu_vm *vm = bo_va->base.vm;
2138 struct amdgpu_bo *bo = bo_va->base.bo;
2139
Christian Königaebc5e62017-09-06 16:55:16 +02002140 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002141 list_add(&mapping->list, &bo_va->invalids);
2142 amdgpu_vm_it_insert(mapping, &vm->va);
2143
2144 if (mapping->flags & AMDGPU_PTE_PRT)
2145 amdgpu_vm_prt_get(adev);
2146
2147 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2148 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02002149 if (list_empty(&bo_va->base.vm_status))
2150 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02002151 spin_unlock(&vm->status_lock);
2152 }
2153 trace_amdgpu_vm_bo_map(bo_va, mapping);
2154}
2155
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002156/**
2157 * amdgpu_vm_bo_map - map bo inside a vm
2158 *
2159 * @adev: amdgpu_device pointer
2160 * @bo_va: bo_va to store the address
2161 * @saddr: where to map the BO
2162 * @offset: requested offset in the BO
2163 * @flags: attributes of pages (read/write/valid/etc.)
2164 *
2165 * Add a mapping of the BO at the specefied addr into the VM.
2166 * Returns 0 for success, error for failure.
2167 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002168 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002169 */
2170int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2171 struct amdgpu_bo_va *bo_va,
2172 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002173 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002174{
Christian Königa9f87f62017-03-30 14:03:59 +02002175 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002176 struct amdgpu_bo *bo = bo_va->base.bo;
2177 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002178 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179
Christian König0be52de2015-05-18 14:37:27 +02002180 /* validate the parameters */
2181 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002182 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002183 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002184
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002185 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002186 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002187 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002188 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002189 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002190
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 saddr /= AMDGPU_GPU_PAGE_SIZE;
2192 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2193
Christian Königa9f87f62017-03-30 14:03:59 +02002194 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2195 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002196 /* bo and tmp overlap, invalid addr */
2197 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002198 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002199 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002200 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201 }
2202
2203 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002204 if (!mapping)
2205 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002206
Christian Königa9f87f62017-03-30 14:03:59 +02002207 mapping->start = saddr;
2208 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209 mapping->offset = offset;
2210 mapping->flags = flags;
2211
Christian König73fb16e2017-08-16 11:13:48 +02002212 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002213
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002215}
2216
2217/**
Christian König80f95c52017-03-13 10:13:39 +01002218 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2219 *
2220 * @adev: amdgpu_device pointer
2221 * @bo_va: bo_va to store the address
2222 * @saddr: where to map the BO
2223 * @offset: requested offset in the BO
2224 * @flags: attributes of pages (read/write/valid/etc.)
2225 *
2226 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2227 * mappings as we do so.
2228 * Returns 0 for success, error for failure.
2229 *
2230 * Object has to be reserved and unreserved outside!
2231 */
2232int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2233 struct amdgpu_bo_va *bo_va,
2234 uint64_t saddr, uint64_t offset,
2235 uint64_t size, uint64_t flags)
2236{
2237 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002238 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002239 uint64_t eaddr;
2240 int r;
2241
2242 /* validate the parameters */
2243 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2244 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2245 return -EINVAL;
2246
2247 /* make sure object fit at this offset */
2248 eaddr = saddr + size - 1;
2249 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002250 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002251 return -EINVAL;
2252
2253 /* Allocate all the needed memory */
2254 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2255 if (!mapping)
2256 return -ENOMEM;
2257
Christian Königec681542017-08-01 10:51:43 +02002258 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002259 if (r) {
2260 kfree(mapping);
2261 return r;
2262 }
2263
2264 saddr /= AMDGPU_GPU_PAGE_SIZE;
2265 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2266
Christian Königa9f87f62017-03-30 14:03:59 +02002267 mapping->start = saddr;
2268 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002269 mapping->offset = offset;
2270 mapping->flags = flags;
2271
Christian König73fb16e2017-08-16 11:13:48 +02002272 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002273
2274 return 0;
2275}
2276
2277/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002278 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2279 *
2280 * @adev: amdgpu_device pointer
2281 * @bo_va: bo_va to remove the address from
2282 * @saddr: where to the BO is mapped
2283 *
2284 * Remove a mapping of the BO at the specefied addr from the VM.
2285 * Returns 0 for success, error for failure.
2286 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002287 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002288 */
2289int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2290 struct amdgpu_bo_va *bo_va,
2291 uint64_t saddr)
2292{
2293 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002294 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002295 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002296
Christian König6c7fc502015-06-05 20:56:17 +02002297 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002298
Christian König7fc11952015-07-30 11:53:42 +02002299 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002300 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301 break;
2302 }
2303
Christian König7fc11952015-07-30 11:53:42 +02002304 if (&mapping->list == &bo_va->valids) {
2305 valid = false;
2306
2307 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002308 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002309 break;
2310 }
2311
Christian König32b41ac2016-03-08 18:03:27 +01002312 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002313 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314 }
Christian König32b41ac2016-03-08 18:03:27 +01002315
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002316 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002317 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002318 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002319 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002320
Christian Könige17841b2016-03-08 17:52:01 +01002321 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002322 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002323 else
Christian König284710f2017-01-30 11:09:31 +01002324 amdgpu_vm_free_mapping(adev, vm, mapping,
2325 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002326
2327 return 0;
2328}
2329
2330/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002331 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2332 *
2333 * @adev: amdgpu_device pointer
2334 * @vm: VM structure to use
2335 * @saddr: start of the range
2336 * @size: size of the range
2337 *
2338 * Remove all mappings in a range, split them as appropriate.
2339 * Returns 0 for success, error for failure.
2340 */
2341int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2342 struct amdgpu_vm *vm,
2343 uint64_t saddr, uint64_t size)
2344{
2345 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002346 LIST_HEAD(removed);
2347 uint64_t eaddr;
2348
2349 eaddr = saddr + size - 1;
2350 saddr /= AMDGPU_GPU_PAGE_SIZE;
2351 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2352
2353 /* Allocate all the needed memory */
2354 before = kzalloc(sizeof(*before), GFP_KERNEL);
2355 if (!before)
2356 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002357 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002358
2359 after = kzalloc(sizeof(*after), GFP_KERNEL);
2360 if (!after) {
2361 kfree(before);
2362 return -ENOMEM;
2363 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002364 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002365
2366 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002367 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2368 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002369 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002370 if (tmp->start < saddr) {
2371 before->start = tmp->start;
2372 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002373 before->offset = tmp->offset;
2374 before->flags = tmp->flags;
2375 list_add(&before->list, &tmp->list);
2376 }
2377
2378 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002379 if (tmp->last > eaddr) {
2380 after->start = eaddr + 1;
2381 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002382 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002383 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002384 after->flags = tmp->flags;
2385 list_add(&after->list, &tmp->list);
2386 }
2387
2388 list_del(&tmp->list);
2389 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002390
2391 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002392 }
2393
2394 /* And free them up */
2395 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002396 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002397 list_del(&tmp->list);
2398
Christian Königa9f87f62017-03-30 14:03:59 +02002399 if (tmp->start < saddr)
2400 tmp->start = saddr;
2401 if (tmp->last > eaddr)
2402 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002403
Christian Königaebc5e62017-09-06 16:55:16 +02002404 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002405 list_add(&tmp->list, &vm->freed);
2406 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2407 }
2408
Junwei Zhang27f6d612017-03-16 16:09:24 +08002409 /* Insert partial mapping before the range */
2410 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002411 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002412 if (before->flags & AMDGPU_PTE_PRT)
2413 amdgpu_vm_prt_get(adev);
2414 } else {
2415 kfree(before);
2416 }
2417
2418 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002419 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002420 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002421 if (after->flags & AMDGPU_PTE_PRT)
2422 amdgpu_vm_prt_get(adev);
2423 } else {
2424 kfree(after);
2425 }
2426
2427 return 0;
2428}
2429
2430/**
Christian Königaebc5e62017-09-06 16:55:16 +02002431 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2432 *
2433 * @vm: the requested VM
2434 *
2435 * Find a mapping by it's address.
2436 */
2437struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2438 uint64_t addr)
2439{
2440 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2441}
2442
2443/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2445 *
2446 * @adev: amdgpu_device pointer
2447 * @bo_va: requested bo_va
2448 *
Christian König8843dbb2016-01-26 12:17:11 +01002449 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002450 *
2451 * Object have to be reserved!
2452 */
2453void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2454 struct amdgpu_bo_va *bo_va)
2455{
2456 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002457 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002458
Christian Königec681542017-08-01 10:51:43 +02002459 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002460
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002461 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002462 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002463 spin_unlock(&vm->status_lock);
2464
Christian König7fc11952015-07-30 11:53:42 +02002465 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002466 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002467 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002468 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002469 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002470 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002471 }
Christian König7fc11952015-07-30 11:53:42 +02002472 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2473 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002474 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002475 amdgpu_vm_free_mapping(adev, vm, mapping,
2476 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002477 }
Christian König32b41ac2016-03-08 18:03:27 +01002478
Chris Wilsonf54d1862016-10-25 13:00:45 +01002479 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002480 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481}
2482
2483/**
2484 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2485 *
2486 * @adev: amdgpu_device pointer
2487 * @vm: requested vm
2488 * @bo: amdgpu buffer object
2489 *
Christian König8843dbb2016-01-26 12:17:11 +01002490 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002491 */
2492void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002493 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002494{
Christian Königec681542017-08-01 10:51:43 +02002495 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002496
Christian Königec681542017-08-01 10:51:43 +02002497 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002498 struct amdgpu_vm *vm = bo_base->vm;
2499
Christian König3d7d4d32017-08-23 16:13:33 +02002500 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002501 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2502 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002503 if (bo->tbo.type == ttm_bo_type_kernel)
2504 list_move(&bo_base->vm_status, &vm->evicted);
2505 else
2506 list_move_tail(&bo_base->vm_status,
2507 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002508 spin_unlock(&bo_base->vm->status_lock);
2509 continue;
2510 }
2511
Christian Königea097292017-08-09 14:15:46 +02002512 if (bo->tbo.type == ttm_bo_type_kernel) {
2513 spin_lock(&bo_base->vm->status_lock);
2514 if (list_empty(&bo_base->vm_status))
2515 list_add(&bo_base->vm_status, &vm->relocated);
2516 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002517 continue;
Christian Königea097292017-08-09 14:15:46 +02002518 }
Christian König3f3333f2017-08-03 14:02:13 +02002519
Christian Königec681542017-08-01 10:51:43 +02002520 spin_lock(&bo_base->vm->status_lock);
2521 if (list_empty(&bo_base->vm_status))
Christian König481c2e92017-09-01 14:46:19 +02002522 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002523 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002524 }
2525}
2526
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002527static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2528{
2529 /* Total bits covered by PD + PTs */
2530 unsigned bits = ilog2(vm_size) + 18;
2531
2532 /* Make sure the PD is 4K in size up to 8GB address space.
2533 Above that split equal between PD and PTs */
2534 if (vm_size <= 8)
2535 return (bits - 9);
2536 else
2537 return ((bits + 3) / 2);
2538}
2539
2540/**
Roger Hed07f14b2017-08-15 16:05:59 +08002541 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002542 *
2543 * @adev: amdgpu_device pointer
2544 * @vm_size: the default vm size if it's set auto
2545 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002546void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +01002547 uint32_t fragment_size_default, unsigned max_level,
2548 unsigned max_bits)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002549{
Christian König36539dc2017-11-23 11:16:05 +01002550 uint64_t tmp;
2551
2552 /* adjust vm size first */
Christian Königf3368122017-11-23 12:57:18 +01002553 if (amdgpu_vm_size != -1) {
2554 unsigned max_size = 1 << (max_bits - 30);
2555
Christian Königfdd5faa2017-11-04 16:51:44 +01002556 vm_size = amdgpu_vm_size;
Christian Königf3368122017-11-23 12:57:18 +01002557 if (vm_size > max_size) {
2558 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2559 amdgpu_vm_size, max_size);
2560 vm_size = max_size;
2561 }
2562 }
Christian Königfdd5faa2017-11-04 16:51:44 +01002563
2564 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Christian König36539dc2017-11-23 11:16:05 +01002565
2566 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
Christian König97489122017-11-27 16:22:05 +01002567 if (amdgpu_vm_block_size != -1)
2568 tmp >>= amdgpu_vm_block_size - 9;
Christian König36539dc2017-11-23 11:16:05 +01002569 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2570 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
Chunming Zhou196f7482017-12-13 14:22:54 +08002571 switch (adev->vm_manager.num_level) {
2572 case 3:
2573 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2574 break;
2575 case 2:
2576 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2577 break;
2578 case 1:
2579 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2580 break;
2581 default:
2582 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2583 }
Christian Königb38f41e2017-11-22 17:00:35 +01002584 /* block size depends on vm size and hw setup*/
Christian König97489122017-11-27 16:22:05 +01002585 if (amdgpu_vm_block_size != -1)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002586 adev->vm_manager.block_size =
Christian König97489122017-11-27 16:22:05 +01002587 min((unsigned)amdgpu_vm_block_size, max_bits
2588 - AMDGPU_GPU_PAGE_SHIFT
2589 - 9 * adev->vm_manager.num_level);
2590 else if (adev->vm_manager.num_level > 1)
2591 adev->vm_manager.block_size = 9;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002592 else
Christian König97489122017-11-27 16:22:05 +01002593 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002594
Christian Königb38f41e2017-11-22 17:00:35 +01002595 if (amdgpu_vm_fragment_size == -1)
2596 adev->vm_manager.fragment_size = fragment_size_default;
2597 else
2598 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
Roger Hed07f14b2017-08-15 16:05:59 +08002599
Christian König36539dc2017-11-23 11:16:05 +01002600 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2601 vm_size, adev->vm_manager.num_level + 1,
2602 adev->vm_manager.block_size,
Christian Königfdd5faa2017-11-04 16:51:44 +01002603 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002604}
2605
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002606/**
2607 * amdgpu_vm_init - initialize a vm instance
2608 *
2609 * @adev: amdgpu_device pointer
2610 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002611 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002612 *
Christian König8843dbb2016-01-26 12:17:11 +01002613 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002614 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002615int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002616 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002617{
2618 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002619 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002620 unsigned ring_instance;
2621 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002622 struct drm_sched_rq *rq;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002623 int r, i;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002624 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002625 uint64_t init_pde_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002626
Davidlohr Buesof808c132017-09-08 16:15:08 -07002627 vm->va = RB_ROOT_CACHED;
Chunming Zhou031e2982016-04-25 10:19:13 +08002628 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002629 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2630 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002631 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002632 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002633 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002634 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002635 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002636
Christian König2bd9ccf2016-02-01 12:53:58 +01002637 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002638
2639 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2640 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2641 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002642 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2643 r = drm_sched_entity_init(&ring->sched, &vm->entity,
Monk Liub3eebe32017-10-23 12:23:29 +08002644 rq, amdgpu_sched_jobs, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002645 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002646 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002647
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002648 vm->pte_support_ats = false;
2649
2650 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002651 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2652 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002653
2654 if (adev->asic_type == CHIP_RAVEN) {
2655 vm->pte_support_ats = true;
Yong Zhao6d16dac2017-08-31 15:55:00 -04002656 init_pde_value = AMDGPU_PTE_DEFAULT_ATC
2657 | AMDGPU_PDE_PTE;
2658
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002659 }
2660 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002661 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2662 AMDGPU_VM_USE_CPU_FOR_GFX);
2663 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2664 vm->use_cpu_for_update ? "CPU" : "SDMA");
2665 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2666 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002667 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002668
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002669 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2670 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2671 if (vm->use_cpu_for_update)
2672 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2673 else
2674 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2675 AMDGPU_GEM_CREATE_SHADOW);
2676
Chunming Zhou196f7482017-12-13 14:22:54 +08002677 r = amdgpu_bo_create(adev,
2678 amdgpu_vm_bo_size(adev, adev->vm_manager.root_level),
2679 align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04002680 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002681 flags,
Christian König3f3333f2017-08-03 14:02:13 +02002682 NULL, NULL, init_pde_value, &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002683 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002684 goto error_free_sched_entity;
2685
Christian König3f3333f2017-08-03 14:02:13 +02002686 vm->root.base.vm = vm;
2687 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2688 INIT_LIST_HEAD(&vm->root.base.vm_status);
Christian König0a096fb2017-07-12 10:01:48 +02002689
2690 if (vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +02002691 r = amdgpu_bo_reserve(vm->root.base.bo, false);
Christian König0a096fb2017-07-12 10:01:48 +02002692 if (r)
2693 goto error_free_root;
Christian König0a096fb2017-07-12 10:01:48 +02002694
Christian König3f3333f2017-08-03 14:02:13 +02002695 r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
Felix Kuehlingca290da2017-08-25 20:15:04 -04002696 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König2bd9ccf2016-02-01 12:53:58 +01002697 if (r)
2698 goto error_free_root;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002699 }
2700
Felix Kuehling02208442017-08-25 20:40:26 -04002701 if (pasid) {
2702 unsigned long flags;
2703
2704 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2705 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2706 GFP_ATOMIC);
2707 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2708 if (r < 0)
2709 goto error_free_root;
2710
2711 vm->pasid = pasid;
2712 }
2713
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002714 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002715 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002716
2717 return 0;
2718
2719error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002720 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2721 amdgpu_bo_unref(&vm->root.base.bo);
2722 vm->root.base.bo = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002723
2724error_free_sched_entity:
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002725 drm_sched_entity_fini(&ring->sched, &vm->entity);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002726
2727 return r;
2728}
2729
2730/**
Christian Königf566ceb2016-10-27 20:04:38 +02002731 * amdgpu_vm_free_levels - free PD/PT levels
2732 *
Christian König8f19cd72017-11-30 15:28:03 +01002733 * @adev: amdgpu device structure
2734 * @parent: PD/PT starting level to free
2735 * @level: level of parent structure
Christian Königf566ceb2016-10-27 20:04:38 +02002736 *
2737 * Free the page directory or page table level and all sub levels.
2738 */
Christian König8f19cd72017-11-30 15:28:03 +01002739static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2740 struct amdgpu_vm_pt *parent,
2741 unsigned level)
Christian Königf566ceb2016-10-27 20:04:38 +02002742{
Christian König8f19cd72017-11-30 15:28:03 +01002743 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +02002744
Christian König8f19cd72017-11-30 15:28:03 +01002745 if (parent->base.bo) {
2746 list_del(&parent->base.bo_list);
2747 list_del(&parent->base.vm_status);
2748 amdgpu_bo_unref(&parent->base.bo->shadow);
2749 amdgpu_bo_unref(&parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002750 }
2751
Christian König8f19cd72017-11-30 15:28:03 +01002752 if (parent->entries)
2753 for (i = 0; i < num_entries; i++)
2754 amdgpu_vm_free_levels(adev, &parent->entries[i],
2755 level + 1);
Christian Königf566ceb2016-10-27 20:04:38 +02002756
Christian König8f19cd72017-11-30 15:28:03 +01002757 kvfree(parent->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002758}
2759
2760/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002761 * amdgpu_vm_fini - tear down a vm instance
2762 *
2763 * @adev: amdgpu_device pointer
2764 * @vm: requested vm
2765 *
Christian König8843dbb2016-01-26 12:17:11 +01002766 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002767 * Unbind the VM and remove all bos from the vm bo list
2768 */
2769void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2770{
2771 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König4388fc22017-03-13 10:13:36 +01002772 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002773 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002774 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002775 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002776
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002777 /* Clear pending page faults from IH when the VM is destroyed */
2778 while (kfifo_get(&vm->faults, &fault))
2779 amdgpu_ih_clear_fault(adev, fault);
2780
Felix Kuehling02208442017-08-25 20:40:26 -04002781 if (vm->pasid) {
2782 unsigned long flags;
2783
2784 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2785 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2786 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2787 }
2788
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002789 drm_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002790
Davidlohr Buesof808c132017-09-08 16:15:08 -07002791 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002792 dev_err(adev->dev, "still active bo inside vm\n");
2793 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002794 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2795 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002796 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002797 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002798 kfree(mapping);
2799 }
2800 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002801 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002802 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002803 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002804 }
Christian König284710f2017-01-30 11:09:31 +01002805
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002806 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002807 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002808 }
2809
Christian König2642cf12017-10-13 17:24:31 +02002810 root = amdgpu_bo_ref(vm->root.base.bo);
2811 r = amdgpu_bo_reserve(root, true);
2812 if (r) {
2813 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2814 } else {
Chunming Zhou196f7482017-12-13 14:22:54 +08002815 amdgpu_vm_free_levels(adev, &vm->root,
2816 adev->vm_manager.root_level);
Christian König2642cf12017-10-13 17:24:31 +02002817 amdgpu_bo_unreserve(root);
2818 }
2819 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002820 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002821 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2822 amdgpu_vm_free_reserved_vmid(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002823}
Christian Königea89f8c2015-11-15 20:52:06 +01002824
2825/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002826 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2827 *
2828 * @adev: amdgpu_device pointer
2829 * @pasid: PASID do identify the VM
2830 *
2831 * This function is expected to be called in interrupt context. Returns
2832 * true if there was fault credit, false otherwise
2833 */
2834bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2835 unsigned int pasid)
2836{
2837 struct amdgpu_vm *vm;
2838
2839 spin_lock(&adev->vm_manager.pasid_lock);
2840 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2841 spin_unlock(&adev->vm_manager.pasid_lock);
2842 if (!vm)
2843 /* VM not found, can't track fault credit */
2844 return true;
2845
2846 /* No lock needed. only accessed by IRQ handler */
2847 if (!vm->fault_credit)
2848 /* Too many faults in this VM */
2849 return false;
2850
2851 vm->fault_credit--;
2852 return true;
2853}
2854
2855/**
Christian Königa9a78b32016-01-21 10:19:11 +01002856 * amdgpu_vm_manager_init - init the VM manager
2857 *
2858 * @adev: amdgpu_device pointer
2859 *
2860 * Initialize the VM manager structures
2861 */
2862void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2863{
Christian König76456702017-04-06 17:52:39 +02002864 unsigned i, j;
Christian Königa9a78b32016-01-21 10:19:11 +01002865
Christian König76456702017-04-06 17:52:39 +02002866 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2867 struct amdgpu_vm_id_manager *id_mgr =
2868 &adev->vm_manager.id_mgr[i];
Christian Königa9a78b32016-01-21 10:19:11 +01002869
Christian König76456702017-04-06 17:52:39 +02002870 mutex_init(&id_mgr->lock);
2871 INIT_LIST_HEAD(&id_mgr->ids_lru);
Chunming Zhouc3505772017-04-21 15:51:04 +08002872 atomic_set(&id_mgr->reserved_vmid_num, 0);
Christian König76456702017-04-06 17:52:39 +02002873
2874 /* skip over VMID 0, since it is the system VM */
2875 for (j = 1; j < id_mgr->num_ids; ++j) {
2876 amdgpu_vm_reset_id(adev, i, j);
2877 amdgpu_sync_create(&id_mgr->ids[i].active);
2878 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2879 }
Christian König971fe9a92016-03-01 15:09:25 +01002880 }
Christian König2d55e452016-02-08 17:37:38 +01002881
Chris Wilsonf54d1862016-10-25 13:00:45 +01002882 adev->vm_manager.fence_context =
2883 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002884 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2885 adev->vm_manager.seqno[i] = 0;
2886
Christian König2d55e452016-02-08 17:37:38 +01002887 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02002888 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian König284710f2017-01-30 11:09:31 +01002889 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002890 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002891
2892 /* If not overridden by the user, by default, only in large BAR systems
2893 * Compute VM tables will be updated by CPU
2894 */
2895#ifdef CONFIG_X86_64
2896 if (amdgpu_vm_update_mode == -1) {
2897 if (amdgpu_vm_is_large_bar(adev))
2898 adev->vm_manager.vm_update_mode =
2899 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2900 else
2901 adev->vm_manager.vm_update_mode = 0;
2902 } else
2903 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2904#else
2905 adev->vm_manager.vm_update_mode = 0;
2906#endif
2907
Felix Kuehling02208442017-08-25 20:40:26 -04002908 idr_init(&adev->vm_manager.pasid_idr);
2909 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002910}
2911
2912/**
Christian Königea89f8c2015-11-15 20:52:06 +01002913 * amdgpu_vm_manager_fini - cleanup VM manager
2914 *
2915 * @adev: amdgpu_device pointer
2916 *
2917 * Cleanup the VM manager and free resources.
2918 */
2919void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2920{
Christian König76456702017-04-06 17:52:39 +02002921 unsigned i, j;
Christian Königea89f8c2015-11-15 20:52:06 +01002922
Felix Kuehling02208442017-08-25 20:40:26 -04002923 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2924 idr_destroy(&adev->vm_manager.pasid_idr);
2925
Christian König76456702017-04-06 17:52:39 +02002926 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2927 struct amdgpu_vm_id_manager *id_mgr =
2928 &adev->vm_manager.id_mgr[i];
Christian Königbcb1ba32016-03-08 15:40:11 +01002929
Christian König76456702017-04-06 17:52:39 +02002930 mutex_destroy(&id_mgr->lock);
2931 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2932 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2933
2934 amdgpu_sync_free(&id->active);
2935 dma_fence_put(id->flushed_updates);
2936 dma_fence_put(id->last_flush);
2937 }
Christian Königbcb1ba32016-03-08 15:40:11 +01002938 }
Christian Königea89f8c2015-11-15 20:52:06 +01002939}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002940
2941int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2942{
2943 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002944 struct amdgpu_device *adev = dev->dev_private;
2945 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2946 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002947
2948 switch (args->in.op) {
2949 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002950 /* current, we only have requirement to reserve vmid from gfxhub */
2951 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2952 AMDGPU_GFXHUB);
2953 if (r)
2954 return r;
2955 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002956 case AMDGPU_VM_OP_UNRESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002957 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002958 break;
2959 default:
2960 return -EINVAL;
2961 }
2962
2963 return 0;
2964}