blob: 2dca47ad4f0947df872565acbe248437c8d474de [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/*
37 * GPUVM
38 * GPUVM is similar to the legacy gart on older asics, however
39 * rather than there being a single global gart table
40 * for the entire GPU, there are multiple VM page tables active
41 * at any given time. The VM page tables can contain a mix
42 * vram pages and system memory pages and system memory pages
43 * can be mapped as snooped (cached system pages) or unsnooped
44 * (uncached system pages).
45 * Each VM has an ID associated with it and there is a page table
46 * associated with each VMID. When execting a command buffer,
47 * the kernel tells the the ring what VMID to use for that command
48 * buffer. VMIDs are allocated dynamically as commands are submitted.
49 * The userspace drivers maintain their own address space and the kernel
50 * sets up their pages tables accordingly when they submit their
51 * command buffers and a VMID is assigned.
52 * Cayman/Trinity support up to 8 active VMs at any given time;
53 * SI supports 16.
54 */
55
Christian Königa9f87f62017-03-30 14:03:59 +020056#define START(node) ((node)->start)
57#define LAST(node) ((node)->last)
58
59INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
60 START, LAST, static, amdgpu_vm_it)
61
62#undef START
63#undef LAST
64
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040065/* Local structure. Encapsulate some VM table update parameters to reduce
66 * the number of function parameters
67 */
Christian König29efc4f2016-08-04 14:52:50 +020068struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020069 /* amdgpu device we do this update for */
70 struct amdgpu_device *adev;
Christian König49ac8a22016-10-13 15:09:08 +020071 /* optional amdgpu_vm we do this update for */
72 struct amdgpu_vm *vm;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040073 /* address where to copy page table entries from */
74 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040075 /* indirect buffer to fill with commands */
76 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020077 /* Function which actually does the update */
Christian König373ac642018-01-16 16:54:25 +010078 void (*func)(struct amdgpu_pte_update_params *params,
79 struct amdgpu_bo *bo, uint64_t pe,
Christian Königafef8b82016-08-12 13:29:18 +020080 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +080081 uint64_t flags);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -040082 /* The next two are used during VM update by CPU
83 * DMA addresses to use for mapping
84 * Kernel pointer of PD/PT BO that needs to be updated
85 */
86 dma_addr_t *pages_addr;
87 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040088};
89
Christian König284710f2017-01-30 11:09:31 +010090/* Helper to disable partial resident texture feature from a fence callback */
91struct amdgpu_prt_cb {
92 struct amdgpu_device *adev;
93 struct dma_fence_cb cb;
94};
95
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096/**
Christian König50783142017-11-27 14:01:51 +010097 * amdgpu_vm_level_shift - return the addr shift for each level
98 *
99 * @adev: amdgpu_device pointer
100 *
101 * Returns the number of bits the pfn needs to be right shifted for a level.
102 */
103static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
104 unsigned level)
105{
Chunming Zhou196f7482017-12-13 14:22:54 +0800106 unsigned shift = 0xff;
107
108 switch (level) {
109 case AMDGPU_VM_PDB2:
110 case AMDGPU_VM_PDB1:
111 case AMDGPU_VM_PDB0:
112 shift = 9 * (AMDGPU_VM_PDB0 - level) +
Christian König50783142017-11-27 14:01:51 +0100113 adev->vm_manager.block_size;
Chunming Zhou196f7482017-12-13 14:22:54 +0800114 break;
115 case AMDGPU_VM_PTB:
116 shift = 0;
117 break;
118 default:
119 dev_err(adev->dev, "the level%d isn't supported.\n", level);
120 }
121
122 return shift;
Christian König50783142017-11-27 14:01:51 +0100123}
124
125/**
Christian König72a7ec52016-10-19 11:03:57 +0200126 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 *
128 * @adev: amdgpu_device pointer
129 *
Christian König72a7ec52016-10-19 11:03:57 +0200130 * Calculate the number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 */
Christian König72a7ec52016-10-19 11:03:57 +0200132static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
133 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134{
Chunming Zhou196f7482017-12-13 14:22:54 +0800135 unsigned shift = amdgpu_vm_level_shift(adev,
136 adev->vm_manager.root_level);
Christian König0410c5e2017-11-20 14:29:01 +0100137
Chunming Zhou196f7482017-12-13 14:22:54 +0800138 if (level == adev->vm_manager.root_level)
Christian König72a7ec52016-10-19 11:03:57 +0200139 /* For the root directory */
Christian König0410c5e2017-11-20 14:29:01 +0100140 return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
Chunming Zhou196f7482017-12-13 14:22:54 +0800141 else if (level != AMDGPU_VM_PTB)
Christian König0410c5e2017-11-20 14:29:01 +0100142 /* Everything in between */
143 return 512;
144 else
Christian König72a7ec52016-10-19 11:03:57 +0200145 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800146 return AMDGPU_VM_PTE_COUNT(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147}
148
149/**
Christian König72a7ec52016-10-19 11:03:57 +0200150 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151 *
152 * @adev: amdgpu_device pointer
153 *
Christian König72a7ec52016-10-19 11:03:57 +0200154 * Calculate the size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155 */
Christian König72a7ec52016-10-19 11:03:57 +0200156static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157{
Christian König72a7ec52016-10-19 11:03:57 +0200158 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159}
160
161/**
Christian König56467eb2015-12-11 15:16:32 +0100162 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163 *
164 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100165 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100166 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167 *
168 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100169 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170 */
Christian König56467eb2015-12-11 15:16:32 +0100171void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
172 struct list_head *validated,
173 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174{
Christian König3f3333f2017-08-03 14:02:13 +0200175 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100176 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200177 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100178 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100179 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100180 list_add(&entry->tv.head, validated);
181}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182
Christian König56467eb2015-12-11 15:16:32 +0100183/**
Christian Königf7da30d2016-09-28 12:03:04 +0200184 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100185 *
Christian König5a712a82016-06-21 16:28:15 +0200186 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100187 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200188 * @validate: callback to do the validation
189 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190 *
Christian Königf7da30d2016-09-28 12:03:04 +0200191 * Validate the page table BOs on command submission if neccessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192 */
Christian Königf7da30d2016-09-28 12:03:04 +0200193int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
194 int (*validate)(void *p, struct amdgpu_bo *bo),
195 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196{
Christian König3f3333f2017-08-03 14:02:13 +0200197 struct ttm_bo_global *glob = adev->mman.bdev.glob;
198 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199
Christian König3f3333f2017-08-03 14:02:13 +0200200 spin_lock(&vm->status_lock);
201 while (!list_empty(&vm->evicted)) {
202 struct amdgpu_vm_bo_base *bo_base;
203 struct amdgpu_bo *bo;
Christian König5a712a82016-06-21 16:28:15 +0200204
Christian König3f3333f2017-08-03 14:02:13 +0200205 bo_base = list_first_entry(&vm->evicted,
206 struct amdgpu_vm_bo_base,
207 vm_status);
208 spin_unlock(&vm->status_lock);
Christian Königeceb8a12016-01-11 15:35:21 +0100209
Christian König3f3333f2017-08-03 14:02:13 +0200210 bo = bo_base->bo;
211 BUG_ON(!bo);
212 if (bo->parent) {
213 r = validate(param, bo);
214 if (r)
215 return r;
Christian König34d7be52017-08-24 12:32:55 +0200216
Christian König3f3333f2017-08-03 14:02:13 +0200217 spin_lock(&glob->lru_lock);
218 ttm_bo_move_to_lru_tail(&bo->tbo);
219 if (bo->shadow)
220 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
221 spin_unlock(&glob->lru_lock);
222 }
223
Christian König73fb16e2017-08-16 11:13:48 +0200224 if (bo->tbo.type == ttm_bo_type_kernel &&
225 vm->use_cpu_for_update) {
Christian König3f3333f2017-08-03 14:02:13 +0200226 r = amdgpu_bo_kmap(bo, NULL);
227 if (r)
228 return r;
229 }
230
231 spin_lock(&vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200232 if (bo->tbo.type != ttm_bo_type_kernel)
233 list_move(&bo_base->vm_status, &vm->moved);
234 else
235 list_move(&bo_base->vm_status, &vm->relocated);
Christian König3f3333f2017-08-03 14:02:13 +0200236 }
237 spin_unlock(&vm->status_lock);
Christian König34d7be52017-08-24 12:32:55 +0200238
239 return 0;
240}
241
242/**
243 * amdgpu_vm_ready - check VM is ready for updates
244 *
Christian König34d7be52017-08-24 12:32:55 +0200245 * @vm: VM to check
246 *
247 * Check if all VM PDs/PTs are ready for updates
248 */
Christian König3f3333f2017-08-03 14:02:13 +0200249bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200250{
Christian König3f3333f2017-08-03 14:02:13 +0200251 bool ready;
Christian König34d7be52017-08-24 12:32:55 +0200252
Christian König3f3333f2017-08-03 14:02:13 +0200253 spin_lock(&vm->status_lock);
254 ready = list_empty(&vm->evicted);
255 spin_unlock(&vm->status_lock);
256
257 return ready;
Christian Königeceb8a12016-01-11 15:35:21 +0100258}
259
260/**
Christian Königf566ceb2016-10-27 20:04:38 +0200261 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
262 *
263 * @adev: amdgpu_device pointer
264 * @vm: requested vm
265 * @saddr: start of the address range
266 * @eaddr: end of the address range
267 *
268 * Make sure the page directories and page tables are allocated
269 */
270static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
271 struct amdgpu_vm *vm,
272 struct amdgpu_vm_pt *parent,
273 uint64_t saddr, uint64_t eaddr,
274 unsigned level)
275{
Christian König50783142017-11-27 14:01:51 +0100276 unsigned shift = amdgpu_vm_level_shift(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200277 unsigned pt_idx, from, to;
278 int r;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400279 u64 flags;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400280 uint64_t init_value = 0;
Christian Königf566ceb2016-10-27 20:04:38 +0200281
282 if (!parent->entries) {
283 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
284
Michal Hocko20981052017-05-17 14:23:12 +0200285 parent->entries = kvmalloc_array(num_entries,
286 sizeof(struct amdgpu_vm_pt),
287 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200288 if (!parent->entries)
289 return -ENOMEM;
290 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
291 }
292
Felix Kuehling1866bac2017-03-28 20:36:12 -0400293 from = saddr >> shift;
294 to = eaddr >> shift;
295 if (from >= amdgpu_vm_num_entries(adev, level) ||
296 to >= amdgpu_vm_num_entries(adev, level))
297 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200298
Christian Königf566ceb2016-10-27 20:04:38 +0200299 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400300 saddr = saddr & ((1 << shift) - 1);
301 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200302
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400303 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
304 AMDGPU_GEM_CREATE_VRAM_CLEARED;
305 if (vm->use_cpu_for_update)
306 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
307 else
308 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
309 AMDGPU_GEM_CREATE_SHADOW);
310
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400311 if (vm->pte_support_ats) {
Yong Zhao6d16dac2017-08-31 15:55:00 -0400312 init_value = AMDGPU_PTE_DEFAULT_ATC;
Chunming Zhou196f7482017-12-13 14:22:54 +0800313 if (level != AMDGPU_VM_PTB)
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400314 init_value |= AMDGPU_PDE_PTE;
Yong Zhao6d16dac2017-08-31 15:55:00 -0400315
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400316 }
317
Christian Königf566ceb2016-10-27 20:04:38 +0200318 /* walk over the address space and allocate the page tables */
319 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200320 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200321 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
322 struct amdgpu_bo *pt;
323
Christian König3f3333f2017-08-03 14:02:13 +0200324 if (!entry->base.bo) {
Christian Königf566ceb2016-10-27 20:04:38 +0200325 r = amdgpu_bo_create(adev,
326 amdgpu_vm_bo_size(adev, level),
327 AMDGPU_GPU_PAGE_SIZE, true,
328 AMDGPU_GEM_DOMAIN_VRAM,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400329 flags,
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400330 NULL, resv, init_value, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200331 if (r)
332 return r;
333
Christian König0a096fb2017-07-12 10:01:48 +0200334 if (vm->use_cpu_for_update) {
335 r = amdgpu_bo_kmap(pt, NULL);
336 if (r) {
337 amdgpu_bo_unref(&pt);
338 return r;
339 }
340 }
341
Christian Königf566ceb2016-10-27 20:04:38 +0200342 /* Keep a reference to the root directory to avoid
343 * freeing them up in the wrong order.
344 */
Christian König0f2fc432017-08-31 10:46:20 +0200345 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200346
Christian König3f3333f2017-08-03 14:02:13 +0200347 entry->base.vm = vm;
348 entry->base.bo = pt;
349 list_add_tail(&entry->base.bo_list, &pt->va);
Christian Königea097292017-08-09 14:15:46 +0200350 spin_lock(&vm->status_lock);
351 list_add(&entry->base.vm_status, &vm->relocated);
352 spin_unlock(&vm->status_lock);
Christian Königf566ceb2016-10-27 20:04:38 +0200353 }
354
Chunming Zhou196f7482017-12-13 14:22:54 +0800355 if (level < AMDGPU_VM_PTB) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400356 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
357 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
358 ((1 << shift) - 1);
359 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
360 sub_eaddr, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200361 if (r)
362 return r;
363 }
364 }
365
366 return 0;
367}
368
Christian König663e4572017-03-13 10:13:37 +0100369/**
370 * amdgpu_vm_alloc_pts - Allocate page tables.
371 *
372 * @adev: amdgpu_device pointer
373 * @vm: VM to allocate page tables for
374 * @saddr: Start address which needs to be allocated
375 * @size: Size from start address we need.
376 *
377 * Make sure the page tables are allocated.
378 */
379int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
380 struct amdgpu_vm *vm,
381 uint64_t saddr, uint64_t size)
382{
Felix Kuehling22770e52017-03-28 20:24:53 -0400383 uint64_t last_pfn;
Christian König663e4572017-03-13 10:13:37 +0100384 uint64_t eaddr;
Christian König663e4572017-03-13 10:13:37 +0100385
386 /* validate the parameters */
387 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
388 return -EINVAL;
389
390 eaddr = saddr + size - 1;
391 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
392 if (last_pfn >= adev->vm_manager.max_pfn) {
Felix Kuehling22770e52017-03-28 20:24:53 -0400393 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
Christian König663e4572017-03-13 10:13:37 +0100394 last_pfn, adev->vm_manager.max_pfn);
395 return -EINVAL;
396 }
397
398 saddr /= AMDGPU_GPU_PAGE_SIZE;
399 eaddr /= AMDGPU_GPU_PAGE_SIZE;
400
Chunming Zhou196f7482017-12-13 14:22:54 +0800401 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
402 adev->vm_manager.root_level);
Christian König663e4572017-03-13 10:13:37 +0100403}
404
Christian König641e9402017-04-03 13:59:25 +0200405/**
Alex Xiee59c0202017-06-01 09:42:59 -0400406 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
407 *
408 * @adev: amdgpu_device pointer
409 */
410void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
411{
412 const struct amdgpu_ip_block *ip_block;
413 bool has_compute_vm_bug;
414 struct amdgpu_ring *ring;
415 int i;
416
417 has_compute_vm_bug = false;
418
Alex Deucher2990a1f2017-12-15 16:18:00 -0500419 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
Alex Xiee59c0202017-06-01 09:42:59 -0400420 if (ip_block) {
421 /* Compute has a VM bug for GFX version < 7.
422 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
423 if (ip_block->version->major <= 7)
424 has_compute_vm_bug = true;
425 else if (ip_block->version->major == 8)
426 if (adev->gfx.mec_fw_version < 673)
427 has_compute_vm_bug = true;
428 }
429
430 for (i = 0; i < adev->num_rings; i++) {
431 ring = adev->rings[i];
432 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
433 /* only compute rings */
434 ring->has_compute_vm_bug = has_compute_vm_bug;
435 else
436 ring->has_compute_vm_bug = false;
437 }
438}
439
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400440bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
441 struct amdgpu_job *job)
442{
443 struct amdgpu_device *adev = ring->adev;
444 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100445 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
446 struct amdgpu_vmid *id;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400447 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400448 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400449
Christian Königc4f46f22017-12-18 17:08:25 +0100450 if (job->vmid == 0)
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400451 return false;
Christian Königc4f46f22017-12-18 17:08:25 +0100452 id = &id_mgr->ids[job->vmid];
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400453 gds_switch_needed = ring->funcs->emit_gds_switch && (
454 id->gds_base != job->gds_base ||
455 id->gds_size != job->gds_size ||
456 id->gws_base != job->gws_base ||
457 id->gws_size != job->gws_size ||
458 id->oa_base != job->oa_base ||
459 id->oa_size != job->oa_size);
460
Christian König620f7742017-12-18 16:53:03 +0100461 if (amdgpu_vmid_had_gpu_reset(adev, id))
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400462 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400463
464 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400465}
466
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400467static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
468{
Christian König770d13b2018-01-12 14:52:22 +0100469 return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500470}
471
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472/**
473 * amdgpu_vm_flush - hardware flush the vm
474 *
475 * @ring: ring to use for flush
Christian Königc4f46f22017-12-18 17:08:25 +0100476 * @vmid: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100477 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400478 *
Christian König4ff37a82016-02-26 16:18:26 +0100479 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800481int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400482{
Christian König971fe9a92016-03-01 15:09:25 +0100483 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200484 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100485 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian Königc4f46f22017-12-18 17:08:25 +0100486 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
Christian Königd564a062016-03-01 15:51:53 +0100487 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800488 id->gds_base != job->gds_base ||
489 id->gds_size != job->gds_size ||
490 id->gws_base != job->gws_base ||
491 id->gws_size != job->gws_size ||
492 id->oa_base != job->oa_base ||
493 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800494 bool vm_flush_needed = job->vm_needs_flush;
Christian Königc0e51932017-04-03 14:16:07 +0200495 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100496 int r;
Christian Königd564a062016-03-01 15:51:53 +0100497
Christian König620f7742017-12-18 16:53:03 +0100498 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
Christian Königf7d015b2017-04-03 14:28:26 +0200499 gds_switch_needed = true;
500 vm_flush_needed = true;
501 }
Christian König971fe9a92016-03-01 15:09:25 +0100502
Monk Liu8fdf0742017-06-06 17:25:13 +0800503 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200504 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100505
Christian Königc0e51932017-04-03 14:16:07 +0200506 if (ring->funcs->init_cond_exec)
507 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100508
Monk Liu8fdf0742017-06-06 17:25:13 +0800509 if (need_pipe_sync)
510 amdgpu_ring_emit_pipeline_sync(ring);
511
Christian Königf7d015b2017-04-03 14:28:26 +0200512 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200513 struct dma_fence *fence;
Monk Liue9d672b2017-03-15 12:18:57 +0800514
Christian Königc4f46f22017-12-18 17:08:25 +0100515 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian König5a4633c2018-01-08 14:48:11 +0100516 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->pasid,
517 job->vm_pd_addr);
Monk Liue9d672b2017-03-15 12:18:57 +0800518
Christian Königc0e51932017-04-03 14:16:07 +0200519 r = amdgpu_fence_emit(ring, &fence);
520 if (r)
521 return r;
Monk Liue9d672b2017-03-15 12:18:57 +0800522
Christian König76456702017-04-06 17:52:39 +0200523 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200524 dma_fence_put(id->last_flush);
525 id->last_flush = fence;
Chunming Zhoubea396722017-05-10 13:02:39 +0800526 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200527 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200528 }
Monk Liue9d672b2017-03-15 12:18:57 +0800529
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800530 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200531 id->gds_base = job->gds_base;
532 id->gds_size = job->gds_size;
533 id->gws_base = job->gws_base;
534 id->gws_size = job->gws_size;
535 id->oa_base = job->oa_base;
536 id->oa_size = job->oa_size;
Christian Königc4f46f22017-12-18 17:08:25 +0100537 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
Christian Königc0e51932017-04-03 14:16:07 +0200538 job->gds_size, job->gws_base,
539 job->gws_size, job->oa_base,
540 job->oa_size);
541 }
542
543 if (ring->funcs->patch_cond_exec)
544 amdgpu_ring_patch_cond_exec(ring, patch_offset);
545
546 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
547 if (ring->funcs->emit_switch_buffer) {
548 amdgpu_ring_emit_switch_buffer(ring);
549 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550 }
Christian König41d9eb22016-03-01 16:46:18 +0100551 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100552}
553
554/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
556 *
557 * @vm: requested vm
558 * @bo: requested buffer object
559 *
Christian König8843dbb2016-01-26 12:17:11 +0100560 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 * Search inside the @bos vm list for the requested vm
562 * Returns the found bo_va or NULL if none is found
563 *
564 * Object has to be reserved!
565 */
566struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
567 struct amdgpu_bo *bo)
568{
569 struct amdgpu_bo_va *bo_va;
570
Christian Königec681542017-08-01 10:51:43 +0200571 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
572 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 return bo_va;
574 }
575 }
576 return NULL;
577}
578
579/**
Christian Königafef8b82016-08-12 13:29:18 +0200580 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 *
Christian König29efc4f2016-08-04 14:52:50 +0200582 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100583 * @bo: PD/PT to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 * @pe: addr of the page entry
585 * @addr: dst addr to write into pe
586 * @count: number of page entries to update
587 * @incr: increase next addr by incr bytes
588 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589 *
590 * Traces the parameters and calls the right asic functions
591 * to setup the page table using the DMA.
592 */
Christian Königafef8b82016-08-12 13:29:18 +0200593static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100594 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200595 uint64_t pe, uint64_t addr,
596 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800597 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598{
Christian König373ac642018-01-16 16:54:25 +0100599 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200600 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601
Christian Königafef8b82016-08-12 13:29:18 +0200602 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200603 amdgpu_vm_write_pte(params->adev, params->ib, pe,
604 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400605
606 } else {
Christian König27c5f362016-08-04 15:02:49 +0200607 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 count, incr, flags);
609 }
610}
611
612/**
Christian Königafef8b82016-08-12 13:29:18 +0200613 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
614 *
615 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100616 * @bo: PD/PT to update
Christian Königafef8b82016-08-12 13:29:18 +0200617 * @pe: addr of the page entry
618 * @addr: dst addr to write into pe
619 * @count: number of page entries to update
620 * @incr: increase next addr by incr bytes
621 * @flags: hw access flags
622 *
623 * Traces the parameters and calls the DMA function to copy the PTEs.
624 */
625static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100626 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200627 uint64_t pe, uint64_t addr,
628 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800629 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200630{
Christian Königec2f05f2016-09-25 16:11:52 +0200631 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200632
Christian König373ac642018-01-16 16:54:25 +0100633 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200634 trace_amdgpu_vm_copy_ptes(pe, src, count);
635
636 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200637}
638
639/**
Christian Königb07c9d22015-11-30 13:26:07 +0100640 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641 *
Christian Königb07c9d22015-11-30 13:26:07 +0100642 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643 * @addr: the unmapped addr
644 *
645 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100646 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200648static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649{
650 uint64_t result;
651
Christian Königde9ea7b2016-08-12 11:33:30 +0200652 /* page table offset */
653 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654
Christian Königde9ea7b2016-08-12 11:33:30 +0200655 /* in case cpu page size != gpu page size*/
656 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100657
658 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659
660 return result;
661}
662
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400663/**
664 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
665 *
666 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100667 * @bo: PD/PT to update
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400668 * @pe: kmap addr of the page entry
669 * @addr: dst addr to write into pe
670 * @count: number of page entries to update
671 * @incr: increase next addr by incr bytes
672 * @flags: hw access flags
673 *
674 * Write count number of PT/PD entries directly.
675 */
676static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100677 struct amdgpu_bo *bo,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400678 uint64_t pe, uint64_t addr,
679 unsigned count, uint32_t incr,
680 uint64_t flags)
681{
682 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400683 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400684
Christian König373ac642018-01-16 16:54:25 +0100685 pe += (unsigned long)amdgpu_bo_kptr(bo);
686
Christian König03918b32017-07-11 17:15:37 +0200687 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
688
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400689 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400690 value = params->pages_addr ?
691 amdgpu_vm_map_gart(params->pages_addr, addr) :
692 addr;
Christian König132f34e2018-01-12 15:26:08 +0100693 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
694 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400695 addr += incr;
696 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400697}
698
Christian Königa33cab72017-07-11 17:13:00 +0200699static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
700 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400701{
702 struct amdgpu_sync sync;
703 int r;
704
705 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -0400706 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400707 r = amdgpu_sync_wait(&sync, true);
708 amdgpu_sync_free(&sync);
709
710 return r;
711}
712
Christian Königf8991ba2016-09-16 15:36:49 +0200713/*
Christian König6989f242017-11-30 19:08:05 +0100714 * amdgpu_vm_update_pde - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200715 *
Christian König6989f242017-11-30 19:08:05 +0100716 * @param: parameters for the update
Christian Königf8991ba2016-09-16 15:36:49 +0200717 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200718 * @parent: parent directory
Christian König6989f242017-11-30 19:08:05 +0100719 * @entry: entry to update
Christian Königf8991ba2016-09-16 15:36:49 +0200720 *
Christian König6989f242017-11-30 19:08:05 +0100721 * Makes sure the requested entry in parent is up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200722 */
Christian König6989f242017-11-30 19:08:05 +0100723static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
724 struct amdgpu_vm *vm,
725 struct amdgpu_vm_pt *parent,
726 struct amdgpu_vm_pt *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727{
Christian König373ac642018-01-16 16:54:25 +0100728 struct amdgpu_bo *bo = parent->base.bo, *pbo;
Christian König3de676d2017-11-29 13:27:26 +0100729 uint64_t pde, pt, flags;
730 unsigned level;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800731
Christian König6989f242017-11-30 19:08:05 +0100732 /* Don't update huge pages here */
733 if (entry->huge)
734 return;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400735
Christian König373ac642018-01-16 16:54:25 +0100736 for (level = 0, pbo = bo->parent; pbo; ++level)
Christian König3de676d2017-11-29 13:27:26 +0100737 pbo = pbo->parent;
738
Chunming Zhou196f7482017-12-13 14:22:54 +0800739 level += params->adev->vm_manager.root_level;
Christian König373ac642018-01-16 16:54:25 +0100740 pt = amdgpu_bo_gpu_offset(entry->base.bo);
Christian König3de676d2017-11-29 13:27:26 +0100741 flags = AMDGPU_PTE_VALID;
Christian König132f34e2018-01-12 15:26:08 +0100742 amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
Christian König373ac642018-01-16 16:54:25 +0100743 pde = (entry - parent->entries) * 8;
744 if (bo->shadow)
745 params->func(params, bo->shadow, pde, pt, 1, 0, flags);
746 params->func(params, bo, pde, pt, 1, 0, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747}
748
Christian König194d2162016-10-12 15:13:52 +0200749/*
Christian König92456b92017-05-12 16:09:26 +0200750 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
751 *
752 * @parent: parent PD
753 *
754 * Mark all PD level as invalid after an error.
755 */
Christian König8f19cd72017-11-30 15:28:03 +0100756static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
757 struct amdgpu_vm *vm,
758 struct amdgpu_vm_pt *parent,
759 unsigned level)
Christian König92456b92017-05-12 16:09:26 +0200760{
Christian König8f19cd72017-11-30 15:28:03 +0100761 unsigned pt_idx, num_entries;
Christian König92456b92017-05-12 16:09:26 +0200762
763 /*
764 * Recurse into the subdirectories. This recursion is harmless because
765 * we only have a maximum of 5 layers.
766 */
Christian König8f19cd72017-11-30 15:28:03 +0100767 num_entries = amdgpu_vm_num_entries(adev, level);
768 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
Christian König92456b92017-05-12 16:09:26 +0200769 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
770
Christian König3f3333f2017-08-03 14:02:13 +0200771 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +0200772 continue;
773
Christian Königea097292017-08-09 14:15:46 +0200774 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +0200775 if (list_empty(&entry->base.vm_status))
776 list_add(&entry->base.vm_status, &vm->relocated);
Christian Königea097292017-08-09 14:15:46 +0200777 spin_unlock(&vm->status_lock);
Christian König8f19cd72017-11-30 15:28:03 +0100778 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
Christian König92456b92017-05-12 16:09:26 +0200779 }
780}
781
782/*
Christian König194d2162016-10-12 15:13:52 +0200783 * amdgpu_vm_update_directories - make sure that all directories are valid
784 *
785 * @adev: amdgpu_device pointer
786 * @vm: requested vm
787 *
788 * Makes sure all directories are up to date.
789 * Returns 0 for success, error for failure.
790 */
791int amdgpu_vm_update_directories(struct amdgpu_device *adev,
792 struct amdgpu_vm *vm)
793{
Christian König6989f242017-11-30 19:08:05 +0100794 struct amdgpu_pte_update_params params;
795 struct amdgpu_job *job;
796 unsigned ndw = 0;
Dan Carpenter78aa02c2017-09-30 11:14:13 +0300797 int r = 0;
Christian König92456b92017-05-12 16:09:26 +0200798
Christian König6989f242017-11-30 19:08:05 +0100799 if (list_empty(&vm->relocated))
800 return 0;
801
802restart:
803 memset(&params, 0, sizeof(params));
804 params.adev = adev;
805
806 if (vm->use_cpu_for_update) {
807 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
808 if (unlikely(r))
809 return r;
810
811 params.func = amdgpu_vm_cpu_set_ptes;
812 } else {
813 ndw = 512 * 8;
814 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
815 if (r)
816 return r;
817
818 params.ib = &job->ibs[0];
819 params.func = amdgpu_vm_do_set_ptes;
820 }
821
Christian Königea097292017-08-09 14:15:46 +0200822 spin_lock(&vm->status_lock);
823 while (!list_empty(&vm->relocated)) {
Christian König6989f242017-11-30 19:08:05 +0100824 struct amdgpu_vm_bo_base *bo_base, *parent;
825 struct amdgpu_vm_pt *pt, *entry;
Christian Königea097292017-08-09 14:15:46 +0200826 struct amdgpu_bo *bo;
827
828 bo_base = list_first_entry(&vm->relocated,
829 struct amdgpu_vm_bo_base,
830 vm_status);
Christian König6989f242017-11-30 19:08:05 +0100831 list_del_init(&bo_base->vm_status);
Christian Königea097292017-08-09 14:15:46 +0200832 spin_unlock(&vm->status_lock);
833
834 bo = bo_base->bo->parent;
Christian König6989f242017-11-30 19:08:05 +0100835 if (!bo) {
Christian Königea097292017-08-09 14:15:46 +0200836 spin_lock(&vm->status_lock);
Christian König6989f242017-11-30 19:08:05 +0100837 continue;
Christian Königea097292017-08-09 14:15:46 +0200838 }
Christian König6989f242017-11-30 19:08:05 +0100839
840 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
841 bo_list);
842 pt = container_of(parent, struct amdgpu_vm_pt, base);
843 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
844
845 amdgpu_vm_update_pde(&params, vm, pt, entry);
846
847 spin_lock(&vm->status_lock);
848 if (!vm->use_cpu_for_update &&
849 (ndw - params.ib->length_dw) < 32)
850 break;
Christian Königea097292017-08-09 14:15:46 +0200851 }
852 spin_unlock(&vm->status_lock);
Christian König92456b92017-05-12 16:09:26 +0200853
Christian König68c62302017-07-11 17:23:29 +0200854 if (vm->use_cpu_for_update) {
855 /* Flush HDP */
856 mb();
Alex Deucherb1d12862018-01-05 10:25:57 -0500857 amdgpu_asic_flush_hdp(adev);
Christian König6989f242017-11-30 19:08:05 +0100858 } else if (params.ib->length_dw == 0) {
859 amdgpu_job_free(job);
860 } else {
861 struct amdgpu_bo *root = vm->root.base.bo;
862 struct amdgpu_ring *ring;
863 struct dma_fence *fence;
864
865 ring = container_of(vm->entity.sched, struct amdgpu_ring,
866 sched);
867
868 amdgpu_ring_pad_ib(ring, params.ib);
869 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
870 AMDGPU_FENCE_OWNER_VM, false);
871 if (root->shadow)
872 amdgpu_sync_resv(adev, &job->sync,
873 root->shadow->tbo.resv,
874 AMDGPU_FENCE_OWNER_VM, false);
875
876 WARN_ON(params.ib->length_dw > ndw);
877 r = amdgpu_job_submit(job, ring, &vm->entity,
878 AMDGPU_FENCE_OWNER_VM, &fence);
879 if (r)
880 goto error;
881
882 amdgpu_bo_fence(root, fence, true);
883 dma_fence_put(vm->last_update);
884 vm->last_update = fence;
Christian König68c62302017-07-11 17:23:29 +0200885 }
886
Christian König6989f242017-11-30 19:08:05 +0100887 if (!list_empty(&vm->relocated))
888 goto restart;
889
890 return 0;
891
892error:
Chunming Zhou196f7482017-12-13 14:22:54 +0800893 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
894 adev->vm_manager.root_level);
Christian König6989f242017-11-30 19:08:05 +0100895 amdgpu_job_free(job);
Christian König92456b92017-05-12 16:09:26 +0200896 return r;
Christian König194d2162016-10-12 15:13:52 +0200897}
898
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400900 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +0200901 *
902 * @p: see amdgpu_pte_update_params definition
903 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400904 * @entry: resulting entry or NULL
905 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +0200906 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400907 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +0200908 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400909void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
910 struct amdgpu_vm_pt **entry,
911 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +0200912{
Chunming Zhou196f7482017-12-13 14:22:54 +0800913 unsigned level = p->adev->vm_manager.root_level;
Christian König4e2cb642016-10-25 15:52:28 +0200914
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400915 *parent = NULL;
916 *entry = &p->vm->root;
917 while ((*entry)->entries) {
Christian Könige3a1b322017-12-01 13:28:46 +0100918 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
Christian König50783142017-11-27 14:01:51 +0100919
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400920 *parent = *entry;
Christian Könige3a1b322017-12-01 13:28:46 +0100921 *entry = &(*entry)->entries[addr >> shift];
922 addr &= (1ULL << shift) - 1;
Christian König4e2cb642016-10-25 15:52:28 +0200923 }
924
Chunming Zhou196f7482017-12-13 14:22:54 +0800925 if (level != AMDGPU_VM_PTB)
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400926 *entry = NULL;
927}
Christian König4e2cb642016-10-25 15:52:28 +0200928
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400929/**
930 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
931 *
932 * @p: see amdgpu_pte_update_params definition
933 * @entry: vm_pt entry to check
934 * @parent: parent entry
935 * @nptes: number of PTEs updated with this operation
936 * @dst: destination address where the PTEs should point to
937 * @flags: access flags fro the PTEs
938 *
939 * Check if we can update the PD with a huge page.
940 */
Christian Königec5207c2017-08-03 19:24:06 +0200941static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
942 struct amdgpu_vm_pt *entry,
943 struct amdgpu_vm_pt *parent,
944 unsigned nptes, uint64_t dst,
945 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400946{
Christian König373ac642018-01-16 16:54:25 +0100947 uint64_t pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400948
949 /* In the case of a mixed PT the PDE must point to it*/
Christian König3cc1d3e2017-12-21 15:47:28 +0100950 if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
951 nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
Christian König4ab40162017-08-03 20:30:50 +0200952 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400953 flags |= AMDGPU_PDE_PTE;
954 }
955
Christian König3cc1d3e2017-12-21 15:47:28 +0100956 if (!(flags & AMDGPU_PDE_PTE)) {
957 if (entry->huge) {
958 /* Add the entry to the relocated list to update it. */
959 entry->huge = false;
960 spin_lock(&p->vm->status_lock);
961 list_move(&entry->base.vm_status, &p->vm->relocated);
962 spin_unlock(&p->vm->status_lock);
963 }
Christian Königec5207c2017-08-03 19:24:06 +0200964 return;
Christian König3cc1d3e2017-12-21 15:47:28 +0100965 }
Alex Deuchercf2f0a32017-07-25 16:35:38 -0400966
Christian König3cc1d3e2017-12-21 15:47:28 +0100967 entry->huge = true;
Christian König132f34e2018-01-12 15:26:08 +0100968 amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
Christian König3de676d2017-11-29 13:27:26 +0100969
Christian König373ac642018-01-16 16:54:25 +0100970 pde = (entry - parent->entries) * 8;
971 if (parent->base.bo->shadow)
972 p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
973 p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
Christian König4e2cb642016-10-25 15:52:28 +0200974}
975
976/**
Christian König92696dd2016-08-05 13:56:35 +0200977 * amdgpu_vm_update_ptes - make sure that page tables are valid
978 *
979 * @params: see amdgpu_pte_update_params definition
980 * @vm: requested vm
981 * @start: start of GPU address range
982 * @end: end of GPU address range
983 * @dst: destination address to map to, the next dst inside the function
984 * @flags: mapping flags
985 *
986 * Update the page tables in the range @start - @end.
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -0400987 * Returns 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +0200988 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -0400989static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200990 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +0800991 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +0200992{
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800993 struct amdgpu_device *adev = params->adev;
994 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +0200995
Christian König301654a2017-05-16 14:30:27 +0200996 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +0200997 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +0200998 unsigned nptes;
Christian König92696dd2016-08-05 13:56:35 +0200999
1000 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001001 for (addr = start; addr < end; addr += nptes,
1002 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1003 struct amdgpu_vm_pt *entry, *parent;
1004
1005 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1006 if (!entry)
1007 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001008
Christian König92696dd2016-08-05 13:56:35 +02001009 if ((addr & ~mask) == (end & ~mask))
1010 nptes = end - addr;
1011 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001012 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001013
Christian Königec5207c2017-08-03 19:24:06 +02001014 amdgpu_vm_handle_huge_pages(params, entry, parent,
1015 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001016 /* We don't need to update PTEs for huge pages */
Christian König78eb2f02017-11-30 15:41:28 +01001017 if (entry->huge)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001018 continue;
1019
Christian König3f3333f2017-08-03 14:02:13 +02001020 pt = entry->base.bo;
Christian König373ac642018-01-16 16:54:25 +01001021 pe_start = (addr & mask) * 8;
1022 if (pt->shadow)
1023 params->func(params, pt->shadow, pe_start, dst, nptes,
1024 AMDGPU_GPU_PAGE_SIZE, flags);
1025 params->func(params, pt, pe_start, dst, nptes,
Christian König301654a2017-05-16 14:30:27 +02001026 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001027 }
1028
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001029 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001030}
1031
1032/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1034 *
Christian König29efc4f2016-08-04 14:52:50 +02001035 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001036 * @vm: requested vm
1037 * @start: first PTE to handle
1038 * @end: last PTE to handle
1039 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001040 * @flags: hw mapping flags
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001041 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001042 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001043static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001044 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001045 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046{
1047 /**
1048 * The MC L1 TLB supports variable sized pages, based on a fragment
1049 * field in the PTE. When this field is set to a non-zero value, page
1050 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1051 * flags are considered valid for all PTEs within the fragment range
1052 * and corresponding mappings are assumed to be physically contiguous.
1053 *
1054 * The L1 TLB can store a single PTE for the whole fragment,
1055 * significantly increasing the space available for translation
1056 * caching. This leads to large improvements in throughput when the
1057 * TLB is under pressure.
1058 *
1059 * The L2 TLB distributes small and large fragments into two
1060 * asymmetric partitions. The large fragment cache is significantly
1061 * larger. Thus, we try to use large fragments wherever possible.
1062 * Userspace can support this by aligning virtual base address and
1063 * allocation size to the fragment size.
1064 */
Roger He6849d472017-08-30 13:01:19 +08001065 unsigned max_frag = params->adev->vm_manager.fragment_size;
1066 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001067
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001069 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001070 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001071
Roger He6849d472017-08-30 13:01:19 +08001072 while (start != end) {
1073 uint64_t frag_flags, frag_end;
1074 unsigned frag;
1075
1076 /* This intentionally wraps around if no bit is set */
1077 frag = min((unsigned)ffs(start) - 1,
1078 (unsigned)fls64(end - start) - 1);
1079 if (frag >= max_frag) {
1080 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1081 frag_end = end & ~((1ULL << max_frag) - 1);
1082 } else {
1083 frag_flags = AMDGPU_PTE_FRAG(frag);
1084 frag_end = start + (1 << frag);
1085 }
1086
1087 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1088 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001089 if (r)
1090 return r;
Roger He6849d472017-08-30 13:01:19 +08001091
1092 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1093 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001094 }
1095
Roger He6849d472017-08-30 13:01:19 +08001096 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097}
1098
1099/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1101 *
1102 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001103 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001104 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001105 * @vm: requested vm
1106 * @start: start of mapped range
1107 * @last: last mapped entry
1108 * @flags: flags for the entries
1109 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001110 * @fence: optional resulting fence
1111 *
Christian Königa14faa62016-01-25 14:27:31 +01001112 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001113 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001114 */
1115static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001116 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001117 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001119 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001120 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001121 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001122{
Christian König2d55e452016-02-08 17:37:38 +01001123 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001124 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001126 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001127 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001128 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129 int r;
1130
Christian Königafef8b82016-08-12 13:29:18 +02001131 memset(&params, 0, sizeof(params));
1132 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001133 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001134
Christian Königa33cab72017-07-11 17:13:00 +02001135 /* sync to everything on unmapping */
1136 if (!(flags & AMDGPU_PTE_VALID))
1137 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1138
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001139 if (vm->use_cpu_for_update) {
1140 /* params.src is used as flag to indicate system Memory */
1141 if (pages_addr)
1142 params.src = ~0;
1143
1144 /* Wait for PT BOs to be free. PTs share the same resv. object
1145 * as the root PD BO
1146 */
Christian Königa33cab72017-07-11 17:13:00 +02001147 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001148 if (unlikely(r))
1149 return r;
1150
1151 params.func = amdgpu_vm_cpu_set_ptes;
1152 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001153 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1154 addr, flags);
1155 }
1156
Christian König2d55e452016-02-08 17:37:38 +01001157 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001158
Christian Königa14faa62016-01-25 14:27:31 +01001159 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160
1161 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001162 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001163 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001164 *
1165 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001166 */
Emily Deng104bd2c2017-12-29 13:13:08 +08001167 if (vm->root.base.bo->shadow)
1168 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1169 else
1170 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171
1172 /* padding, etc. */
1173 ndw = 64;
1174
Christian König570144c2017-08-30 15:38:45 +02001175 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001176 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001177 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001178
Christian Königb0456f92016-08-11 14:06:54 +02001179 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001180 ndw += nptes * 2;
1181
Christian Königafef8b82016-08-12 13:29:18 +02001182 params.func = amdgpu_vm_do_copy_ptes;
1183
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 } else {
1185 /* set page commands needed */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001186 ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187
Roger He6849d472017-08-30 13:01:19 +08001188 /* extra commands for begin/end fragments */
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001189 ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
1190 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001191
1192 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001193 }
1194
Christian Königd71518b2016-02-01 12:20:25 +01001195 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1196 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001198
Christian König29efc4f2016-08-04 14:52:50 +02001199 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001200
Christian König570144c2017-08-30 15:38:45 +02001201 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001202 uint64_t *pte;
1203 unsigned i;
1204
1205 /* Put the PTEs at the end of the IB. */
1206 i = ndw - nptes * 2;
1207 pte= (uint64_t *)&(job->ibs->ptr[i]);
1208 params.src = job->ibs->gpu_addr + i * 4;
1209
1210 for (i = 0; i < nptes; ++i) {
1211 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1212 AMDGPU_GPU_PAGE_SIZE);
1213 pte[i] |= flags;
1214 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001215 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001216 }
1217
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -05001218 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
Christian König3cabaa52016-06-06 10:17:58 +02001219 if (r)
1220 goto error_free;
1221
Christian König3f3333f2017-08-03 14:02:13 +02001222 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001223 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001224 if (r)
1225 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226
Christian König3f3333f2017-08-03 14:02:13 +02001227 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001228 if (r)
1229 goto error_free;
1230
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001231 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1232 if (r)
1233 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001234
Christian König29efc4f2016-08-04 14:52:50 +02001235 amdgpu_ring_pad_ib(ring, params.ib);
1236 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001237 r = amdgpu_job_submit(job, ring, &vm->entity,
1238 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001239 if (r)
1240 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001241
Christian König3f3333f2017-08-03 14:02:13 +02001242 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001243 dma_fence_put(*fence);
1244 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001245 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001246
1247error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001248 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001249 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250}
1251
1252/**
Christian Königa14faa62016-01-25 14:27:31 +01001253 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1254 *
1255 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001256 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001257 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001258 * @vm: requested vm
1259 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001260 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001261 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001262 * @fence: optional resulting fence
1263 *
1264 * Split the mapping into smaller chunks so that each update fits
1265 * into a SDMA IB.
1266 * Returns 0 for success, -EINVAL for failure.
1267 */
1268static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001269 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001270 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001271 struct amdgpu_vm *vm,
1272 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001273 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001274 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001275 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001276{
Christian König9fc8fc72017-09-18 13:58:30 +02001277 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001278 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001279 int r;
1280
1281 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1282 * but in case of something, we filter the flags in first place
1283 */
1284 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1285 flags &= ~AMDGPU_PTE_READABLE;
1286 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1287 flags &= ~AMDGPU_PTE_WRITEABLE;
1288
Alex Xie15b31c52017-03-03 16:47:11 -05001289 flags &= ~AMDGPU_PTE_EXECUTABLE;
1290 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1291
Alex Xieb0fd18b2017-03-03 16:49:39 -05001292 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1293 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1294
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001295 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1296 (adev->asic_type >= CHIP_VEGA10)) {
1297 flags |= AMDGPU_PTE_PRT;
1298 flags &= ~AMDGPU_PTE_VALID;
1299 }
1300
Christian Königa14faa62016-01-25 14:27:31 +01001301 trace_amdgpu_vm_bo_update(mapping);
1302
Christian König63e0ba42016-08-16 17:38:37 +02001303 pfn = mapping->offset >> PAGE_SHIFT;
1304 if (nodes) {
1305 while (pfn >= nodes->size) {
1306 pfn -= nodes->size;
1307 ++nodes;
1308 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001309 }
Christian Königa14faa62016-01-25 14:27:31 +01001310
Christian König63e0ba42016-08-16 17:38:37 +02001311 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001312 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001313 uint64_t max_entries;
1314 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001315
Christian König63e0ba42016-08-16 17:38:37 +02001316 if (nodes) {
1317 addr = nodes->start << PAGE_SHIFT;
1318 max_entries = (nodes->size - pfn) *
1319 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1320 } else {
1321 addr = 0;
1322 max_entries = S64_MAX;
1323 }
Christian Königa14faa62016-01-25 14:27:31 +01001324
Christian König63e0ba42016-08-16 17:38:37 +02001325 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001326 uint64_t count;
1327
Christian König457e0fe2017-08-22 12:50:46 +02001328 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001329 for (count = 1; count < max_entries; ++count) {
1330 uint64_t idx = pfn + count;
1331
1332 if (pages_addr[idx] !=
1333 (pages_addr[idx - 1] + PAGE_SIZE))
1334 break;
1335 }
1336
1337 if (count < min_linear_pages) {
1338 addr = pfn << PAGE_SHIFT;
1339 dma_addr = pages_addr;
1340 } else {
1341 addr = pages_addr[pfn];
1342 max_entries = count;
1343 }
1344
Christian König63e0ba42016-08-16 17:38:37 +02001345 } else if (flags & AMDGPU_PTE_VALID) {
1346 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001347 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001348 }
Christian König63e0ba42016-08-16 17:38:37 +02001349
Christian Königa9f87f62017-03-30 14:03:59 +02001350 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001351 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001352 start, last, flags, addr,
1353 fence);
1354 if (r)
1355 return r;
1356
Christian König63e0ba42016-08-16 17:38:37 +02001357 pfn += last - start + 1;
1358 if (nodes && nodes->size == pfn) {
1359 pfn = 0;
1360 ++nodes;
1361 }
Christian Königa14faa62016-01-25 14:27:31 +01001362 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001363
Christian Königa9f87f62017-03-30 14:03:59 +02001364 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001365
1366 return 0;
1367}
1368
1369/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001370 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1371 *
1372 * @adev: amdgpu_device pointer
1373 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001374 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001375 *
1376 * Fill in the page table entries for @bo_va.
1377 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 */
1379int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1380 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001381 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001382{
Christian Königec681542017-08-01 10:51:43 +02001383 struct amdgpu_bo *bo = bo_va->base.bo;
1384 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001386 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001387 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001388 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001389 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001390 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001391 int r;
1392
Christian Königec681542017-08-01 10:51:43 +02001393 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001394 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001395 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001396 exclusive = NULL;
1397 } else {
Christian König8358dce2016-03-30 10:50:25 +02001398 struct ttm_dma_tt *ttm;
1399
Christian Königec681542017-08-01 10:51:43 +02001400 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001401 nodes = mem->mm_node;
1402 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001403 ttm = container_of(bo_va->base.bo->tbo.ttm,
1404 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001405 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001406 }
Christian Königec681542017-08-01 10:51:43 +02001407 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001408 }
1409
Christian König457e0fe2017-08-22 12:50:46 +02001410 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001411 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001412 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001413 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414
Christian König4e55eb32017-09-11 16:54:59 +02001415 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1416 last_update = &vm->last_update;
1417 else
1418 last_update = &bo_va->last_pt_update;
1419
Christian König3d7d4d32017-08-23 16:13:33 +02001420 if (!clear && bo_va->base.moved) {
1421 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001422 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001423
Christian Königcb7b6ec2017-08-15 17:08:12 +02001424 } else if (bo_va->cleared != clear) {
1425 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001426 }
Christian König7fc11952015-07-30 11:53:42 +02001427
1428 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001429 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001430 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001431 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 if (r)
1433 return r;
1434 }
1435
Christian König68c62302017-07-11 17:23:29 +02001436 if (vm->use_cpu_for_update) {
1437 /* Flush HDP */
1438 mb();
Alex Deucherb1d12862018-01-05 10:25:57 -05001439 amdgpu_asic_flush_hdp(adev);
Christian König68c62302017-07-11 17:23:29 +02001440 }
1441
Christian Königcb7b6ec2017-08-15 17:08:12 +02001442 spin_lock(&vm->status_lock);
1443 list_del_init(&bo_va->base.vm_status);
1444 spin_unlock(&vm->status_lock);
1445
1446 list_splice_init(&bo_va->invalids, &bo_va->valids);
1447 bo_va->cleared = clear;
1448
1449 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1450 list_for_each_entry(mapping, &bo_va->valids, list)
1451 trace_amdgpu_vm_bo_mapping(mapping);
1452 }
1453
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001454 return 0;
1455}
1456
1457/**
Christian König284710f2017-01-30 11:09:31 +01001458 * amdgpu_vm_update_prt_state - update the global PRT state
1459 */
1460static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1461{
1462 unsigned long flags;
1463 bool enable;
1464
1465 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001466 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König132f34e2018-01-12 15:26:08 +01001467 adev->gmc.gmc_funcs->set_prt(adev, enable);
Christian König284710f2017-01-30 11:09:31 +01001468 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1469}
1470
1471/**
Christian König4388fc22017-03-13 10:13:36 +01001472 * amdgpu_vm_prt_get - add a PRT user
Christian König451bc8e2017-02-14 16:02:52 +01001473 */
1474static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1475{
Christian König132f34e2018-01-12 15:26:08 +01001476 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001477 return;
1478
Christian König451bc8e2017-02-14 16:02:52 +01001479 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1480 amdgpu_vm_update_prt_state(adev);
1481}
1482
1483/**
Christian König0b15f2f2017-02-14 15:47:03 +01001484 * amdgpu_vm_prt_put - drop a PRT user
1485 */
1486static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1487{
Christian König451bc8e2017-02-14 16:02:52 +01001488 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001489 amdgpu_vm_update_prt_state(adev);
1490}
1491
1492/**
Christian König451bc8e2017-02-14 16:02:52 +01001493 * amdgpu_vm_prt_cb - callback for updating the PRT status
Christian König284710f2017-01-30 11:09:31 +01001494 */
1495static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1496{
1497 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1498
Christian König0b15f2f2017-02-14 15:47:03 +01001499 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001500 kfree(cb);
1501}
1502
1503/**
Christian König451bc8e2017-02-14 16:02:52 +01001504 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1505 */
1506static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1507 struct dma_fence *fence)
1508{
Christian König4388fc22017-03-13 10:13:36 +01001509 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001510
Christian König132f34e2018-01-12 15:26:08 +01001511 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001512 return;
1513
1514 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001515 if (!cb) {
1516 /* Last resort when we are OOM */
1517 if (fence)
1518 dma_fence_wait(fence, false);
1519
Dan Carpenter486a68f2017-04-03 21:41:39 +03001520 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001521 } else {
1522 cb->adev = adev;
1523 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1524 amdgpu_vm_prt_cb))
1525 amdgpu_vm_prt_cb(fence, &cb->cb);
1526 }
1527}
1528
1529/**
Christian König284710f2017-01-30 11:09:31 +01001530 * amdgpu_vm_free_mapping - free a mapping
1531 *
1532 * @adev: amdgpu_device pointer
1533 * @vm: requested vm
1534 * @mapping: mapping to be freed
1535 * @fence: fence of the unmap operation
1536 *
1537 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1538 */
1539static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1540 struct amdgpu_vm *vm,
1541 struct amdgpu_bo_va_mapping *mapping,
1542 struct dma_fence *fence)
1543{
Christian König451bc8e2017-02-14 16:02:52 +01001544 if (mapping->flags & AMDGPU_PTE_PRT)
1545 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001546 kfree(mapping);
1547}
1548
1549/**
Christian König451bc8e2017-02-14 16:02:52 +01001550 * amdgpu_vm_prt_fini - finish all prt mappings
1551 *
1552 * @adev: amdgpu_device pointer
1553 * @vm: requested vm
1554 *
1555 * Register a cleanup callback to disable PRT support after VM dies.
1556 */
1557static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1558{
Christian König3f3333f2017-08-03 14:02:13 +02001559 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001560 struct dma_fence *excl, **shared;
1561 unsigned i, shared_count;
1562 int r;
1563
1564 r = reservation_object_get_fences_rcu(resv, &excl,
1565 &shared_count, &shared);
1566 if (r) {
1567 /* Not enough memory to grab the fence list, as last resort
1568 * block for all the fences to complete.
1569 */
1570 reservation_object_wait_timeout_rcu(resv, true, false,
1571 MAX_SCHEDULE_TIMEOUT);
1572 return;
1573 }
1574
1575 /* Add a callback for each fence in the reservation object */
1576 amdgpu_vm_prt_get(adev);
1577 amdgpu_vm_add_prt_cb(adev, excl);
1578
1579 for (i = 0; i < shared_count; ++i) {
1580 amdgpu_vm_prt_get(adev);
1581 amdgpu_vm_add_prt_cb(adev, shared[i]);
1582 }
1583
1584 kfree(shared);
1585}
1586
1587/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001588 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1589 *
1590 * @adev: amdgpu_device pointer
1591 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001592 * @fence: optional resulting fence (unchanged if no work needed to be done
1593 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001594 *
1595 * Make sure all freed BOs are cleared in the PT.
1596 * Returns 0 for success.
1597 *
1598 * PTs have to be reserved and mutex must be locked!
1599 */
1600int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001601 struct amdgpu_vm *vm,
1602 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001603{
1604 struct amdgpu_bo_va_mapping *mapping;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001605 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606 int r;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001607 uint64_t init_pte_value = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608
1609 while (!list_empty(&vm->freed)) {
1610 mapping = list_first_entry(&vm->freed,
1611 struct amdgpu_bo_va_mapping, list);
1612 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001613
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001614 if (vm->pte_support_ats)
Yong Zhao6d16dac2017-08-31 15:55:00 -04001615 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001616
Christian König570144c2017-08-30 15:38:45 +02001617 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001618 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001619 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001620 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001621 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001622 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623 return r;
Christian König284710f2017-01-30 11:09:31 +01001624 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001625 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001626
1627 if (fence && f) {
1628 dma_fence_put(*fence);
1629 *fence = f;
1630 } else {
1631 dma_fence_put(f);
1632 }
1633
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 return 0;
1635
1636}
1637
1638/**
Christian König73fb16e2017-08-16 11:13:48 +02001639 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 *
1641 * @adev: amdgpu_device pointer
1642 * @vm: requested vm
Christian König73fb16e2017-08-16 11:13:48 +02001643 * @sync: sync object to add fences to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001644 *
Christian König73fb16e2017-08-16 11:13:48 +02001645 * Make sure all BOs which are moved are updated in the PTs.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646 * Returns 0 for success.
1647 *
Christian König73fb16e2017-08-16 11:13:48 +02001648 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 */
Christian König73fb16e2017-08-16 11:13:48 +02001650int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02001651 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001652{
Christian König73fb16e2017-08-16 11:13:48 +02001653 bool clear;
Christian König91e1a522015-07-06 22:06:40 +02001654 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655
1656 spin_lock(&vm->status_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02001657 while (!list_empty(&vm->moved)) {
Christian König4e55eb32017-09-11 16:54:59 +02001658 struct amdgpu_bo_va *bo_va;
Christian Königec363e02017-09-01 20:34:27 +02001659 struct reservation_object *resv;
Christian König4e55eb32017-09-11 16:54:59 +02001660
Christian König27c7b9a2017-08-01 11:27:36 +02001661 bo_va = list_first_entry(&vm->moved,
Christian Königec681542017-08-01 10:51:43 +02001662 struct amdgpu_bo_va, base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001663 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001664
Christian Königec363e02017-09-01 20:34:27 +02001665 resv = bo_va->base.bo->tbo.resv;
1666
Christian König73fb16e2017-08-16 11:13:48 +02001667 /* Per VM BOs never need to bo cleared in the page tables */
Christian Königec363e02017-09-01 20:34:27 +02001668 if (resv == vm->root.base.bo->tbo.resv)
1669 clear = false;
1670 /* Try to reserve the BO to avoid clearing its ptes */
Christian König9b8cad22018-01-03 13:36:22 +01001671 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
Christian Königec363e02017-09-01 20:34:27 +02001672 clear = false;
1673 /* Somebody else is using the BO right now */
1674 else
1675 clear = true;
Christian König73fb16e2017-08-16 11:13:48 +02001676
1677 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 if (r)
1679 return r;
1680
Christian Königec363e02017-09-01 20:34:27 +02001681 if (!clear && resv != vm->root.base.bo->tbo.resv)
1682 reservation_object_unlock(resv);
1683
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 spin_lock(&vm->status_lock);
1685 }
1686 spin_unlock(&vm->status_lock);
1687
Christian König91e1a522015-07-06 22:06:40 +02001688 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689}
1690
1691/**
1692 * amdgpu_vm_bo_add - add a bo to a specific vm
1693 *
1694 * @adev: amdgpu_device pointer
1695 * @vm: requested vm
1696 * @bo: amdgpu buffer object
1697 *
Christian König8843dbb2016-01-26 12:17:11 +01001698 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001699 * Add @bo to the list of bos associated with the vm
1700 * Returns newly added bo_va or NULL for failure
1701 *
1702 * Object has to be reserved!
1703 */
1704struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1705 struct amdgpu_vm *vm,
1706 struct amdgpu_bo *bo)
1707{
1708 struct amdgpu_bo_va *bo_va;
1709
1710 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1711 if (bo_va == NULL) {
1712 return NULL;
1713 }
Christian Königec681542017-08-01 10:51:43 +02001714 bo_va->base.vm = vm;
1715 bo_va->base.bo = bo;
1716 INIT_LIST_HEAD(&bo_va->base.bo_list);
1717 INIT_LIST_HEAD(&bo_va->base.vm_status);
1718
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001719 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02001720 INIT_LIST_HEAD(&bo_va->valids);
1721 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01001722
Christian König727ffdf2017-12-22 17:13:03 +01001723 if (!bo)
1724 return bo_va;
1725
1726 list_add_tail(&bo_va->base.bo_list, &bo->va);
1727
1728 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
1729 return bo_va;
1730
1731 if (bo->preferred_domains &
1732 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
1733 return bo_va;
1734
1735 /*
1736 * We checked all the prerequisites, but it looks like this per VM BO
1737 * is currently evicted. add the BO to the evicted list to make sure it
1738 * is validated on next VM use to avoid fault.
1739 * */
1740 spin_lock(&vm->status_lock);
1741 list_move_tail(&bo_va->base.vm_status, &vm->evicted);
1742 spin_unlock(&vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001743
1744 return bo_va;
1745}
1746
Christian König73fb16e2017-08-16 11:13:48 +02001747
1748/**
1749 * amdgpu_vm_bo_insert_mapping - insert a new mapping
1750 *
1751 * @adev: amdgpu_device pointer
1752 * @bo_va: bo_va to store the address
1753 * @mapping: the mapping to insert
1754 *
1755 * Insert a new mapping into all structures.
1756 */
1757static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1758 struct amdgpu_bo_va *bo_va,
1759 struct amdgpu_bo_va_mapping *mapping)
1760{
1761 struct amdgpu_vm *vm = bo_va->base.vm;
1762 struct amdgpu_bo *bo = bo_va->base.bo;
1763
Christian Königaebc5e62017-09-06 16:55:16 +02001764 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02001765 list_add(&mapping->list, &bo_va->invalids);
1766 amdgpu_vm_it_insert(mapping, &vm->va);
1767
1768 if (mapping->flags & AMDGPU_PTE_PRT)
1769 amdgpu_vm_prt_get(adev);
1770
1771 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1772 spin_lock(&vm->status_lock);
Christian König481c2e92017-09-01 14:46:19 +02001773 if (list_empty(&bo_va->base.vm_status))
1774 list_add(&bo_va->base.vm_status, &vm->moved);
Christian König73fb16e2017-08-16 11:13:48 +02001775 spin_unlock(&vm->status_lock);
1776 }
1777 trace_amdgpu_vm_bo_map(bo_va, mapping);
1778}
1779
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780/**
1781 * amdgpu_vm_bo_map - map bo inside a vm
1782 *
1783 * @adev: amdgpu_device pointer
1784 * @bo_va: bo_va to store the address
1785 * @saddr: where to map the BO
1786 * @offset: requested offset in the BO
1787 * @flags: attributes of pages (read/write/valid/etc.)
1788 *
1789 * Add a mapping of the BO at the specefied addr into the VM.
1790 * Returns 0 for success, error for failure.
1791 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001792 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793 */
1794int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1795 struct amdgpu_bo_va *bo_va,
1796 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01001797 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001798{
Christian Königa9f87f62017-03-30 14:03:59 +02001799 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02001800 struct amdgpu_bo *bo = bo_va->base.bo;
1801 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001802 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001803
Christian König0be52de2015-05-18 14:37:27 +02001804 /* validate the parameters */
1805 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001806 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001807 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001808
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001809 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001810 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01001811 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001812 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001814
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 saddr /= AMDGPU_GPU_PAGE_SIZE;
1816 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1817
Christian Königa9f87f62017-03-30 14:03:59 +02001818 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1819 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001820 /* bo and tmp overlap, invalid addr */
1821 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02001822 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02001823 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01001824 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001825 }
1826
1827 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01001828 if (!mapping)
1829 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001830
Christian Königa9f87f62017-03-30 14:03:59 +02001831 mapping->start = saddr;
1832 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001833 mapping->offset = offset;
1834 mapping->flags = flags;
1835
Christian König73fb16e2017-08-16 11:13:48 +02001836 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01001837
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001838 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001839}
1840
1841/**
Christian König80f95c52017-03-13 10:13:39 +01001842 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1843 *
1844 * @adev: amdgpu_device pointer
1845 * @bo_va: bo_va to store the address
1846 * @saddr: where to map the BO
1847 * @offset: requested offset in the BO
1848 * @flags: attributes of pages (read/write/valid/etc.)
1849 *
1850 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1851 * mappings as we do so.
1852 * Returns 0 for success, error for failure.
1853 *
1854 * Object has to be reserved and unreserved outside!
1855 */
1856int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1857 struct amdgpu_bo_va *bo_va,
1858 uint64_t saddr, uint64_t offset,
1859 uint64_t size, uint64_t flags)
1860{
1861 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02001862 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01001863 uint64_t eaddr;
1864 int r;
1865
1866 /* validate the parameters */
1867 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1868 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1869 return -EINVAL;
1870
1871 /* make sure object fit at this offset */
1872 eaddr = saddr + size - 1;
1873 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02001874 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01001875 return -EINVAL;
1876
1877 /* Allocate all the needed memory */
1878 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1879 if (!mapping)
1880 return -ENOMEM;
1881
Christian Königec681542017-08-01 10:51:43 +02001882 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01001883 if (r) {
1884 kfree(mapping);
1885 return r;
1886 }
1887
1888 saddr /= AMDGPU_GPU_PAGE_SIZE;
1889 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1890
Christian Königa9f87f62017-03-30 14:03:59 +02001891 mapping->start = saddr;
1892 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01001893 mapping->offset = offset;
1894 mapping->flags = flags;
1895
Christian König73fb16e2017-08-16 11:13:48 +02001896 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01001897
1898 return 0;
1899}
1900
1901/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001902 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1903 *
1904 * @adev: amdgpu_device pointer
1905 * @bo_va: bo_va to remove the address from
1906 * @saddr: where to the BO is mapped
1907 *
1908 * Remove a mapping of the BO at the specefied addr from the VM.
1909 * Returns 0 for success, error for failure.
1910 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001911 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001912 */
1913int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1914 struct amdgpu_bo_va *bo_va,
1915 uint64_t saddr)
1916{
1917 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02001918 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02001919 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920
Christian König6c7fc502015-06-05 20:56:17 +02001921 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001922
Christian König7fc11952015-07-30 11:53:42 +02001923 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001924 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001925 break;
1926 }
1927
Christian König7fc11952015-07-30 11:53:42 +02001928 if (&mapping->list == &bo_va->valids) {
1929 valid = false;
1930
1931 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001932 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02001933 break;
1934 }
1935
Christian König32b41ac2016-03-08 18:03:27 +01001936 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001937 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001938 }
Christian König32b41ac2016-03-08 18:03:27 +01001939
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02001941 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02001942 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02001943 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001944
Christian Könige17841b2016-03-08 17:52:01 +01001945 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001946 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001947 else
Christian König284710f2017-01-30 11:09:31 +01001948 amdgpu_vm_free_mapping(adev, vm, mapping,
1949 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001950
1951 return 0;
1952}
1953
1954/**
Christian Königdc54d3d2017-03-13 10:13:38 +01001955 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1956 *
1957 * @adev: amdgpu_device pointer
1958 * @vm: VM structure to use
1959 * @saddr: start of the range
1960 * @size: size of the range
1961 *
1962 * Remove all mappings in a range, split them as appropriate.
1963 * Returns 0 for success, error for failure.
1964 */
1965int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1966 struct amdgpu_vm *vm,
1967 uint64_t saddr, uint64_t size)
1968{
1969 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01001970 LIST_HEAD(removed);
1971 uint64_t eaddr;
1972
1973 eaddr = saddr + size - 1;
1974 saddr /= AMDGPU_GPU_PAGE_SIZE;
1975 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1976
1977 /* Allocate all the needed memory */
1978 before = kzalloc(sizeof(*before), GFP_KERNEL);
1979 if (!before)
1980 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08001981 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001982
1983 after = kzalloc(sizeof(*after), GFP_KERNEL);
1984 if (!after) {
1985 kfree(before);
1986 return -ENOMEM;
1987 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08001988 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01001989
1990 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02001991 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1992 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01001993 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02001994 if (tmp->start < saddr) {
1995 before->start = tmp->start;
1996 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01001997 before->offset = tmp->offset;
1998 before->flags = tmp->flags;
1999 list_add(&before->list, &tmp->list);
2000 }
2001
2002 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002003 if (tmp->last > eaddr) {
2004 after->start = eaddr + 1;
2005 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002006 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002007 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002008 after->flags = tmp->flags;
2009 list_add(&after->list, &tmp->list);
2010 }
2011
2012 list_del(&tmp->list);
2013 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002014
2015 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002016 }
2017
2018 /* And free them up */
2019 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002020 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002021 list_del(&tmp->list);
2022
Christian Königa9f87f62017-03-30 14:03:59 +02002023 if (tmp->start < saddr)
2024 tmp->start = saddr;
2025 if (tmp->last > eaddr)
2026 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002027
Christian Königaebc5e62017-09-06 16:55:16 +02002028 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002029 list_add(&tmp->list, &vm->freed);
2030 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2031 }
2032
Junwei Zhang27f6d612017-03-16 16:09:24 +08002033 /* Insert partial mapping before the range */
2034 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002035 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002036 if (before->flags & AMDGPU_PTE_PRT)
2037 amdgpu_vm_prt_get(adev);
2038 } else {
2039 kfree(before);
2040 }
2041
2042 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002043 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002044 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002045 if (after->flags & AMDGPU_PTE_PRT)
2046 amdgpu_vm_prt_get(adev);
2047 } else {
2048 kfree(after);
2049 }
2050
2051 return 0;
2052}
2053
2054/**
Christian Königaebc5e62017-09-06 16:55:16 +02002055 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2056 *
2057 * @vm: the requested VM
2058 *
2059 * Find a mapping by it's address.
2060 */
2061struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2062 uint64_t addr)
2063{
2064 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2065}
2066
2067/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2069 *
2070 * @adev: amdgpu_device pointer
2071 * @bo_va: requested bo_va
2072 *
Christian König8843dbb2016-01-26 12:17:11 +01002073 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002074 *
2075 * Object have to be reserved!
2076 */
2077void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2078 struct amdgpu_bo_va *bo_va)
2079{
2080 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002081 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002082
Christian Königec681542017-08-01 10:51:43 +02002083 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002084
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085 spin_lock(&vm->status_lock);
Christian Königec681542017-08-01 10:51:43 +02002086 list_del(&bo_va->base.vm_status);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087 spin_unlock(&vm->status_lock);
2088
Christian König7fc11952015-07-30 11:53:42 +02002089 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002091 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002092 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002093 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002094 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002095 }
Christian König7fc11952015-07-30 11:53:42 +02002096 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2097 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002098 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002099 amdgpu_vm_free_mapping(adev, vm, mapping,
2100 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002101 }
Christian König32b41ac2016-03-08 18:03:27 +01002102
Chris Wilsonf54d1862016-10-25 13:00:45 +01002103 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002104 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002105}
2106
2107/**
2108 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2109 *
2110 * @adev: amdgpu_device pointer
2111 * @vm: requested vm
2112 * @bo: amdgpu buffer object
2113 *
Christian König8843dbb2016-01-26 12:17:11 +01002114 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002115 */
2116void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002117 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002118{
Christian Königec681542017-08-01 10:51:43 +02002119 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002120
Christian Königec681542017-08-01 10:51:43 +02002121 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002122 struct amdgpu_vm *vm = bo_base->vm;
2123
Christian König3d7d4d32017-08-23 16:13:33 +02002124 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002125 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2126 spin_lock(&bo_base->vm->status_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002127 if (bo->tbo.type == ttm_bo_type_kernel)
2128 list_move(&bo_base->vm_status, &vm->evicted);
2129 else
2130 list_move_tail(&bo_base->vm_status,
2131 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002132 spin_unlock(&bo_base->vm->status_lock);
2133 continue;
2134 }
2135
Christian Königea097292017-08-09 14:15:46 +02002136 if (bo->tbo.type == ttm_bo_type_kernel) {
2137 spin_lock(&bo_base->vm->status_lock);
2138 if (list_empty(&bo_base->vm_status))
2139 list_add(&bo_base->vm_status, &vm->relocated);
2140 spin_unlock(&bo_base->vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002141 continue;
Christian Königea097292017-08-09 14:15:46 +02002142 }
Christian König3f3333f2017-08-03 14:02:13 +02002143
Christian Königec681542017-08-01 10:51:43 +02002144 spin_lock(&bo_base->vm->status_lock);
2145 if (list_empty(&bo_base->vm_status))
Christian König481c2e92017-09-01 14:46:19 +02002146 list_add(&bo_base->vm_status, &vm->moved);
Christian Königec681542017-08-01 10:51:43 +02002147 spin_unlock(&bo_base->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002148 }
2149}
2150
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002151static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2152{
2153 /* Total bits covered by PD + PTs */
2154 unsigned bits = ilog2(vm_size) + 18;
2155
2156 /* Make sure the PD is 4K in size up to 8GB address space.
2157 Above that split equal between PD and PTs */
2158 if (vm_size <= 8)
2159 return (bits - 9);
2160 else
2161 return ((bits + 3) / 2);
2162}
2163
2164/**
Roger Hed07f14b2017-08-15 16:05:59 +08002165 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002166 *
2167 * @adev: amdgpu_device pointer
2168 * @vm_size: the default vm size if it's set auto
2169 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002170void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +01002171 uint32_t fragment_size_default, unsigned max_level,
2172 unsigned max_bits)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002173{
Christian König36539dc2017-11-23 11:16:05 +01002174 uint64_t tmp;
2175
2176 /* adjust vm size first */
Christian Königf3368122017-11-23 12:57:18 +01002177 if (amdgpu_vm_size != -1) {
2178 unsigned max_size = 1 << (max_bits - 30);
2179
Christian Königfdd5faa2017-11-04 16:51:44 +01002180 vm_size = amdgpu_vm_size;
Christian Königf3368122017-11-23 12:57:18 +01002181 if (vm_size > max_size) {
2182 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2183 amdgpu_vm_size, max_size);
2184 vm_size = max_size;
2185 }
2186 }
Christian Königfdd5faa2017-11-04 16:51:44 +01002187
2188 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Christian König36539dc2017-11-23 11:16:05 +01002189
2190 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
Christian König97489122017-11-27 16:22:05 +01002191 if (amdgpu_vm_block_size != -1)
2192 tmp >>= amdgpu_vm_block_size - 9;
Christian König36539dc2017-11-23 11:16:05 +01002193 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2194 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
Chunming Zhou196f7482017-12-13 14:22:54 +08002195 switch (adev->vm_manager.num_level) {
2196 case 3:
2197 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2198 break;
2199 case 2:
2200 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2201 break;
2202 case 1:
2203 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2204 break;
2205 default:
2206 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2207 }
Christian Königb38f41e2017-11-22 17:00:35 +01002208 /* block size depends on vm size and hw setup*/
Christian König97489122017-11-27 16:22:05 +01002209 if (amdgpu_vm_block_size != -1)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002210 adev->vm_manager.block_size =
Christian König97489122017-11-27 16:22:05 +01002211 min((unsigned)amdgpu_vm_block_size, max_bits
2212 - AMDGPU_GPU_PAGE_SHIFT
2213 - 9 * adev->vm_manager.num_level);
2214 else if (adev->vm_manager.num_level > 1)
2215 adev->vm_manager.block_size = 9;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002216 else
Christian König97489122017-11-27 16:22:05 +01002217 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002218
Christian Königb38f41e2017-11-22 17:00:35 +01002219 if (amdgpu_vm_fragment_size == -1)
2220 adev->vm_manager.fragment_size = fragment_size_default;
2221 else
2222 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
Roger Hed07f14b2017-08-15 16:05:59 +08002223
Christian König36539dc2017-11-23 11:16:05 +01002224 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2225 vm_size, adev->vm_manager.num_level + 1,
2226 adev->vm_manager.block_size,
Christian Königfdd5faa2017-11-04 16:51:44 +01002227 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002228}
2229
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002230/**
2231 * amdgpu_vm_init - initialize a vm instance
2232 *
2233 * @adev: amdgpu_device pointer
2234 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002235 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002236 *
Christian König8843dbb2016-01-26 12:17:11 +01002237 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002238 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002239int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002240 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002241{
2242 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002243 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian Königd3aab672018-01-24 14:57:02 +01002244 uint64_t init_pde_value = 0, flags;
Christian König2d55e452016-02-08 17:37:38 +01002245 unsigned ring_instance;
2246 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002247 struct drm_sched_rq *rq;
Christian Königd3aab672018-01-24 14:57:02 +01002248 unsigned long size;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002249 int r, i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002250
Davidlohr Buesof808c132017-09-08 16:15:08 -07002251 vm->va = RB_ROOT_CACHED;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002252 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2253 vm->reserved_vmid[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002254 spin_lock_init(&vm->status_lock);
Christian König3f3333f2017-08-03 14:02:13 +02002255 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002256 INIT_LIST_HEAD(&vm->relocated);
Christian König27c7b9a2017-08-01 11:27:36 +02002257 INIT_LIST_HEAD(&vm->moved);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002258 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002259
Christian König2bd9ccf2016-02-01 12:53:58 +01002260 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002261
2262 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2263 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2264 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002265 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2266 r = drm_sched_entity_init(&ring->sched, &vm->entity,
Monk Liub3eebe32017-10-23 12:23:29 +08002267 rq, amdgpu_sched_jobs, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002268 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002269 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002270
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002271 vm->pte_support_ats = false;
2272
2273 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002274 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2275 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002276
2277 if (adev->asic_type == CHIP_RAVEN) {
2278 vm->pte_support_ats = true;
Yong Zhao6d16dac2017-08-31 15:55:00 -04002279 init_pde_value = AMDGPU_PTE_DEFAULT_ATC
2280 | AMDGPU_PDE_PTE;
2281
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002282 }
2283 } else
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002284 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2285 AMDGPU_VM_USE_CPU_FOR_GFX);
2286 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2287 vm->use_cpu_for_update ? "CPU" : "SDMA");
2288 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2289 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002290 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002291
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002292 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2293 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2294 if (vm->use_cpu_for_update)
2295 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2296 else
2297 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2298 AMDGPU_GEM_CREATE_SHADOW);
2299
Christian Königd3aab672018-01-24 14:57:02 +01002300 size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
2301 r = amdgpu_bo_create(adev, size, align, true, AMDGPU_GEM_DOMAIN_VRAM,
2302 flags, NULL, NULL, init_pde_value,
2303 &vm->root.base.bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002304 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002305 goto error_free_sched_entity;
2306
Christian Königd3aab672018-01-24 14:57:02 +01002307 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2308 if (r)
2309 goto error_free_root;
2310
Christian König3f3333f2017-08-03 14:02:13 +02002311 vm->root.base.vm = vm;
2312 list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
Christian Königd3aab672018-01-24 14:57:02 +01002313 list_add_tail(&vm->root.base.vm_status, &vm->evicted);
2314 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König0a096fb2017-07-12 10:01:48 +02002315
Felix Kuehling02208442017-08-25 20:40:26 -04002316 if (pasid) {
2317 unsigned long flags;
2318
2319 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2320 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2321 GFP_ATOMIC);
2322 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2323 if (r < 0)
2324 goto error_free_root;
2325
2326 vm->pasid = pasid;
2327 }
2328
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002329 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002330 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331
2332 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002333
Christian König67003a12016-10-12 14:46:26 +02002334error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002335 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2336 amdgpu_bo_unref(&vm->root.base.bo);
2337 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002338
2339error_free_sched_entity:
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002340 drm_sched_entity_fini(&ring->sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002341
2342 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002343}
2344
2345/**
Christian Königf566ceb2016-10-27 20:04:38 +02002346 * amdgpu_vm_free_levels - free PD/PT levels
2347 *
Christian König8f19cd72017-11-30 15:28:03 +01002348 * @adev: amdgpu device structure
2349 * @parent: PD/PT starting level to free
2350 * @level: level of parent structure
Christian Königf566ceb2016-10-27 20:04:38 +02002351 *
2352 * Free the page directory or page table level and all sub levels.
2353 */
Christian König8f19cd72017-11-30 15:28:03 +01002354static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2355 struct amdgpu_vm_pt *parent,
2356 unsigned level)
Christian Königf566ceb2016-10-27 20:04:38 +02002357{
Christian König8f19cd72017-11-30 15:28:03 +01002358 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +02002359
Christian König8f19cd72017-11-30 15:28:03 +01002360 if (parent->base.bo) {
2361 list_del(&parent->base.bo_list);
2362 list_del(&parent->base.vm_status);
2363 amdgpu_bo_unref(&parent->base.bo->shadow);
2364 amdgpu_bo_unref(&parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002365 }
2366
Christian König8f19cd72017-11-30 15:28:03 +01002367 if (parent->entries)
2368 for (i = 0; i < num_entries; i++)
2369 amdgpu_vm_free_levels(adev, &parent->entries[i],
2370 level + 1);
Christian Königf566ceb2016-10-27 20:04:38 +02002371
Christian König8f19cd72017-11-30 15:28:03 +01002372 kvfree(parent->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002373}
2374
2375/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002376 * amdgpu_vm_fini - tear down a vm instance
2377 *
2378 * @adev: amdgpu_device pointer
2379 * @vm: requested vm
2380 *
Christian König8843dbb2016-01-26 12:17:11 +01002381 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382 * Unbind the VM and remove all bos from the vm bo list
2383 */
2384void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2385{
2386 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König132f34e2018-01-12 15:26:08 +01002387 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002388 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002389 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002390 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002391
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002392 /* Clear pending page faults from IH when the VM is destroyed */
2393 while (kfifo_get(&vm->faults, &fault))
2394 amdgpu_ih_clear_fault(adev, fault);
2395
Felix Kuehling02208442017-08-25 20:40:26 -04002396 if (vm->pasid) {
2397 unsigned long flags;
2398
2399 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2400 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2401 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2402 }
2403
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002404 drm_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002405
Davidlohr Buesof808c132017-09-08 16:15:08 -07002406 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002407 dev_err(adev->dev, "still active bo inside vm\n");
2408 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002409 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2410 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002411 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002412 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002413 kfree(mapping);
2414 }
2415 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002416 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002417 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002418 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002419 }
Christian König284710f2017-01-30 11:09:31 +01002420
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002421 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002422 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002423 }
2424
Christian König2642cf12017-10-13 17:24:31 +02002425 root = amdgpu_bo_ref(vm->root.base.bo);
2426 r = amdgpu_bo_reserve(root, true);
2427 if (r) {
2428 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2429 } else {
Chunming Zhou196f7482017-12-13 14:22:54 +08002430 amdgpu_vm_free_levels(adev, &vm->root,
2431 adev->vm_manager.root_level);
Christian König2642cf12017-10-13 17:24:31 +02002432 amdgpu_bo_unreserve(root);
2433 }
2434 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002435 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002436 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
Christian König620f7742017-12-18 16:53:03 +01002437 amdgpu_vmid_free_reserved(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002438}
Christian Königea89f8c2015-11-15 20:52:06 +01002439
2440/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002441 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2442 *
2443 * @adev: amdgpu_device pointer
2444 * @pasid: PASID do identify the VM
2445 *
2446 * This function is expected to be called in interrupt context. Returns
2447 * true if there was fault credit, false otherwise
2448 */
2449bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2450 unsigned int pasid)
2451{
2452 struct amdgpu_vm *vm;
2453
2454 spin_lock(&adev->vm_manager.pasid_lock);
2455 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
Christian Königd9589392018-01-09 19:18:59 +01002456 if (!vm) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002457 /* VM not found, can't track fault credit */
Christian Königd9589392018-01-09 19:18:59 +01002458 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002459 return true;
Christian Königd9589392018-01-09 19:18:59 +01002460 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002461
2462 /* No lock needed. only accessed by IRQ handler */
Christian Königd9589392018-01-09 19:18:59 +01002463 if (!vm->fault_credit) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002464 /* Too many faults in this VM */
Christian Königd9589392018-01-09 19:18:59 +01002465 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002466 return false;
Christian Königd9589392018-01-09 19:18:59 +01002467 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002468
2469 vm->fault_credit--;
Christian Königd9589392018-01-09 19:18:59 +01002470 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002471 return true;
2472}
2473
2474/**
Christian Königa9a78b32016-01-21 10:19:11 +01002475 * amdgpu_vm_manager_init - init the VM manager
2476 *
2477 * @adev: amdgpu_device pointer
2478 *
2479 * Initialize the VM manager structures
2480 */
2481void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2482{
Christian König620f7742017-12-18 16:53:03 +01002483 unsigned i;
Christian Königa9a78b32016-01-21 10:19:11 +01002484
Christian König620f7742017-12-18 16:53:03 +01002485 amdgpu_vmid_mgr_init(adev);
Christian König2d55e452016-02-08 17:37:38 +01002486
Chris Wilsonf54d1862016-10-25 13:00:45 +01002487 adev->vm_manager.fence_context =
2488 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002489 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2490 adev->vm_manager.seqno[i] = 0;
2491
Christian König2d55e452016-02-08 17:37:38 +01002492 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian König284710f2017-01-30 11:09:31 +01002493 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002494 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002495
2496 /* If not overridden by the user, by default, only in large BAR systems
2497 * Compute VM tables will be updated by CPU
2498 */
2499#ifdef CONFIG_X86_64
2500 if (amdgpu_vm_update_mode == -1) {
2501 if (amdgpu_vm_is_large_bar(adev))
2502 adev->vm_manager.vm_update_mode =
2503 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2504 else
2505 adev->vm_manager.vm_update_mode = 0;
2506 } else
2507 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2508#else
2509 adev->vm_manager.vm_update_mode = 0;
2510#endif
2511
Felix Kuehling02208442017-08-25 20:40:26 -04002512 idr_init(&adev->vm_manager.pasid_idr);
2513 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002514}
2515
2516/**
Christian Königea89f8c2015-11-15 20:52:06 +01002517 * amdgpu_vm_manager_fini - cleanup VM manager
2518 *
2519 * @adev: amdgpu_device pointer
2520 *
2521 * Cleanup the VM manager and free resources.
2522 */
2523void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2524{
Felix Kuehling02208442017-08-25 20:40:26 -04002525 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2526 idr_destroy(&adev->vm_manager.pasid_idr);
2527
Christian König620f7742017-12-18 16:53:03 +01002528 amdgpu_vmid_mgr_fini(adev);
Christian Königea89f8c2015-11-15 20:52:06 +01002529}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002530
2531int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2532{
2533 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002534 struct amdgpu_device *adev = dev->dev_private;
2535 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2536 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002537
2538 switch (args->in.op) {
2539 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002540 /* current, we only have requirement to reserve vmid from gfxhub */
Christian König620f7742017-12-18 16:53:03 +01002541 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002542 if (r)
2543 return r;
2544 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002545 case AMDGPU_VM_OP_UNRESERVE_VMID:
Christian König620f7742017-12-18 16:53:03 +01002546 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002547 break;
2548 default:
2549 return -EINVAL;
2550 }
2551
2552 return 0;
2553}