blob: 7f03f8c387085523e06198c1769eab2adb20b5a3 [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"
Felix Kuehlingede0dd82018-03-15 17:27:43 -040035#include "amdgpu_amdkfd.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040036
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040037/**
38 * DOC: GPUVM
39 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040 * GPUVM is similar to the legacy gart on older asics, however
41 * rather than there being a single global gart table
42 * for the entire GPU, there are multiple VM page tables active
43 * at any given time. The VM page tables can contain a mix
44 * vram pages and system memory pages and system memory pages
45 * can be mapped as snooped (cached system pages) or unsnooped
46 * (uncached system pages).
47 * Each VM has an ID associated with it and there is a page table
48 * associated with each VMID. When execting a command buffer,
49 * the kernel tells the the ring what VMID to use for that command
50 * buffer. VMIDs are allocated dynamically as commands are submitted.
51 * The userspace drivers maintain their own address space and the kernel
52 * sets up their pages tables accordingly when they submit their
53 * command buffers and a VMID is assigned.
54 * Cayman/Trinity support up to 8 active VMs at any given time;
55 * SI supports 16.
56 */
57
Christian Königa9f87f62017-03-30 14:03:59 +020058#define START(node) ((node)->start)
59#define LAST(node) ((node)->last)
60
61INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
62 START, LAST, static, amdgpu_vm_it)
63
64#undef START
65#undef LAST
66
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040067/**
68 * struct amdgpu_pte_update_params - Local structure
69 *
70 * Encapsulate some VM table update parameters to reduce
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040071 * the number of function parameters
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040072 *
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040073 */
Christian König29efc4f2016-08-04 14:52:50 +020074struct amdgpu_pte_update_params {
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040075
76 /**
77 * @adev: amdgpu device we do this update for
78 */
Christian König27c5f362016-08-04 15:02:49 +020079 struct amdgpu_device *adev;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040080
81 /**
82 * @vm: optional amdgpu_vm we do this update for
83 */
Christian König49ac8a22016-10-13 15:09:08 +020084 struct amdgpu_vm *vm;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040085
86 /**
87 * @src: address where to copy page table entries from
88 */
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040089 uint64_t src;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040090
91 /**
92 * @ib: indirect buffer to fill with commands
93 */
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040094 struct amdgpu_ib *ib;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040095
96 /**
97 * @func: Function which actually does the update
98 */
Christian König373ac642018-01-16 16:54:25 +010099 void (*func)(struct amdgpu_pte_update_params *params,
100 struct amdgpu_bo *bo, uint64_t pe,
Christian Königafef8b82016-08-12 13:29:18 +0200101 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800102 uint64_t flags);
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400103 /**
104 * @pages_addr:
105 *
106 * DMA addresses to use for mapping, used during VM update by CPU
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400107 */
108 dma_addr_t *pages_addr;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400109
110 /**
111 * @kptr:
112 *
113 * Kernel pointer of PD/PT BO that needs to be updated,
114 * used during VM update by CPU
115 */
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400116 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400117};
118
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400119/**
120 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
121 */
Christian König284710f2017-01-30 11:09:31 +0100122struct amdgpu_prt_cb {
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400123
124 /**
125 * @adev: amdgpu device
126 */
Christian König284710f2017-01-30 11:09:31 +0100127 struct amdgpu_device *adev;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400128
129 /**
130 * @cb: callback
131 */
Christian König284710f2017-01-30 11:09:31 +0100132 struct dma_fence_cb cb;
133};
134
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400135/**
136 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
137 *
138 * @base: base structure for tracking BO usage in a VM
139 * @vm: vm to which bo is to be added
140 * @bo: amdgpu buffer object
141 *
142 * Initialize a bo_va_base structure and add it to the appropriate lists
143 *
144 */
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800145static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
146 struct amdgpu_vm *vm,
147 struct amdgpu_bo *bo)
148{
149 base->vm = vm;
150 base->bo = bo;
151 INIT_LIST_HEAD(&base->bo_list);
152 INIT_LIST_HEAD(&base->vm_status);
153
154 if (!bo)
155 return;
156 list_add_tail(&base->bo_list, &bo->va);
157
158 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
159 return;
160
161 if (bo->preferred_domains &
162 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
163 return;
164
165 /*
166 * we checked all the prerequisites, but it looks like this per vm bo
167 * is currently evicted. add the bo to the evicted list to make sure it
168 * is validated on next vm use to avoid fault.
169 * */
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800170 list_move_tail(&base->vm_status, &vm->evicted);
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800171}
172
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173/**
Christian König50783142017-11-27 14:01:51 +0100174 * amdgpu_vm_level_shift - return the addr shift for each level
175 *
176 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400177 * @level: VMPT level
Christian König50783142017-11-27 14:01:51 +0100178 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400179 * Returns:
180 * The number of bits the pfn needs to be right shifted for a level.
Christian König50783142017-11-27 14:01:51 +0100181 */
182static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
183 unsigned level)
184{
Chunming Zhou196f7482017-12-13 14:22:54 +0800185 unsigned shift = 0xff;
186
187 switch (level) {
188 case AMDGPU_VM_PDB2:
189 case AMDGPU_VM_PDB1:
190 case AMDGPU_VM_PDB0:
191 shift = 9 * (AMDGPU_VM_PDB0 - level) +
Christian König50783142017-11-27 14:01:51 +0100192 adev->vm_manager.block_size;
Chunming Zhou196f7482017-12-13 14:22:54 +0800193 break;
194 case AMDGPU_VM_PTB:
195 shift = 0;
196 break;
197 default:
198 dev_err(adev->dev, "the level%d isn't supported.\n", level);
199 }
200
201 return shift;
Christian König50783142017-11-27 14:01:51 +0100202}
203
204/**
Christian König72a7ec52016-10-19 11:03:57 +0200205 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 *
207 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400208 * @level: VMPT level
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400209 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400210 * Returns:
211 * The number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 */
Christian König72a7ec52016-10-19 11:03:57 +0200213static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
214 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215{
Chunming Zhou196f7482017-12-13 14:22:54 +0800216 unsigned shift = amdgpu_vm_level_shift(adev,
217 adev->vm_manager.root_level);
Christian König0410c5e2017-11-20 14:29:01 +0100218
Chunming Zhou196f7482017-12-13 14:22:54 +0800219 if (level == adev->vm_manager.root_level)
Christian König72a7ec52016-10-19 11:03:57 +0200220 /* For the root directory */
Christian König0410c5e2017-11-20 14:29:01 +0100221 return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
Chunming Zhou196f7482017-12-13 14:22:54 +0800222 else if (level != AMDGPU_VM_PTB)
Christian König0410c5e2017-11-20 14:29:01 +0100223 /* Everything in between */
224 return 512;
225 else
Christian König72a7ec52016-10-19 11:03:57 +0200226 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800227 return AMDGPU_VM_PTE_COUNT(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228}
229
230/**
Christian König72a7ec52016-10-19 11:03:57 +0200231 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232 *
233 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400234 * @level: VMPT level
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400236 * Returns:
237 * The size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238 */
Christian König72a7ec52016-10-19 11:03:57 +0200239static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240{
Christian König72a7ec52016-10-19 11:03:57 +0200241 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242}
243
244/**
Christian König56467eb2015-12-11 15:16:32 +0100245 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246 *
247 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100248 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100249 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250 *
251 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100252 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400253 */
Christian König56467eb2015-12-11 15:16:32 +0100254void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
255 struct list_head *validated,
256 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257{
Christian König3f3333f2017-08-03 14:02:13 +0200258 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100259 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200260 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100261 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100262 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100263 list_add(&entry->tv.head, validated);
264}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265
Christian König56467eb2015-12-11 15:16:32 +0100266/**
Christian Königf7da30d2016-09-28 12:03:04 +0200267 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100268 *
Christian König5a712a82016-06-21 16:28:15 +0200269 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100270 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200271 * @validate: callback to do the validation
272 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400273 *
Christian Königf7da30d2016-09-28 12:03:04 +0200274 * Validate the page table BOs on command submission if neccessary.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400275 *
276 * Returns:
277 * Validation result.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278 */
Christian Königf7da30d2016-09-28 12:03:04 +0200279int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
280 int (*validate)(void *p, struct amdgpu_bo *bo),
281 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282{
Christian König3f3333f2017-08-03 14:02:13 +0200283 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Christian König91ccdd22018-04-19 11:02:54 +0200284 struct amdgpu_vm_bo_base *bo_base, *tmp;
285 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286
Christian König91ccdd22018-04-19 11:02:54 +0200287 list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
288 struct amdgpu_bo *bo = bo_base->bo;
Christian König5a712a82016-06-21 16:28:15 +0200289
Christian König3f3333f2017-08-03 14:02:13 +0200290 if (bo->parent) {
291 r = validate(param, bo);
292 if (r)
Christian König91ccdd22018-04-19 11:02:54 +0200293 break;
Christian König34d7be52017-08-24 12:32:55 +0200294
Christian König3f3333f2017-08-03 14:02:13 +0200295 spin_lock(&glob->lru_lock);
296 ttm_bo_move_to_lru_tail(&bo->tbo);
297 if (bo->shadow)
298 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
299 spin_unlock(&glob->lru_lock);
300 }
301
Christian Königaf4c0f62018-04-19 10:56:02 +0200302 if (bo->tbo.type != ttm_bo_type_kernel) {
303 spin_lock(&vm->moved_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200304 list_move(&bo_base->vm_status, &vm->moved);
Christian Königaf4c0f62018-04-19 10:56:02 +0200305 spin_unlock(&vm->moved_lock);
306 } else {
Christian König73fb16e2017-08-16 11:13:48 +0200307 list_move(&bo_base->vm_status, &vm->relocated);
Christian Königaf4c0f62018-04-19 10:56:02 +0200308 }
Christian König3f3333f2017-08-03 14:02:13 +0200309 }
Christian König34d7be52017-08-24 12:32:55 +0200310
Christian König806f0432018-04-19 15:01:12 +0200311 spin_lock(&glob->lru_lock);
312 list_for_each_entry(bo_base, &vm->idle, vm_status) {
313 struct amdgpu_bo *bo = bo_base->bo;
314
315 if (!bo->parent)
316 continue;
317
318 ttm_bo_move_to_lru_tail(&bo->tbo);
319 if (bo->shadow)
320 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
321 }
322 spin_unlock(&glob->lru_lock);
323
Christian König91ccdd22018-04-19 11:02:54 +0200324 return r;
Christian König34d7be52017-08-24 12:32:55 +0200325}
326
327/**
328 * amdgpu_vm_ready - check VM is ready for updates
329 *
Christian König34d7be52017-08-24 12:32:55 +0200330 * @vm: VM to check
331 *
332 * Check if all VM PDs/PTs are ready for updates
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400333 *
334 * Returns:
335 * True if eviction list is empty.
Christian König34d7be52017-08-24 12:32:55 +0200336 */
Christian König3f3333f2017-08-03 14:02:13 +0200337bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200338{
Christian Königaf4c0f62018-04-19 10:56:02 +0200339 return list_empty(&vm->evicted);
Christian Königeceb8a12016-01-11 15:35:21 +0100340}
341
342/**
Christian König13307f72018-01-24 17:19:04 +0100343 * amdgpu_vm_clear_bo - initially clear the PDs/PTs
344 *
345 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400346 * @vm: VM to clear BO from
Christian König13307f72018-01-24 17:19:04 +0100347 * @bo: BO to clear
348 * @level: level this BO is at
349 *
350 * Root PD needs to be reserved when calling this.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400351 *
352 * Returns:
353 * 0 on success, errno otherwise.
Christian König13307f72018-01-24 17:19:04 +0100354 */
355static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König45843122018-01-25 18:36:15 +0100356 struct amdgpu_vm *vm, struct amdgpu_bo *bo,
357 unsigned level, bool pte_support_ats)
Christian König13307f72018-01-24 17:19:04 +0100358{
359 struct ttm_operation_ctx ctx = { true, false };
360 struct dma_fence *fence = NULL;
Christian König45843122018-01-25 18:36:15 +0100361 unsigned entries, ats_entries;
Christian König13307f72018-01-24 17:19:04 +0100362 struct amdgpu_ring *ring;
363 struct amdgpu_job *job;
Christian König45843122018-01-25 18:36:15 +0100364 uint64_t addr;
Christian König13307f72018-01-24 17:19:04 +0100365 int r;
366
Christian König45843122018-01-25 18:36:15 +0100367 addr = amdgpu_bo_gpu_offset(bo);
368 entries = amdgpu_bo_size(bo) / 8;
369
370 if (pte_support_ats) {
371 if (level == adev->vm_manager.root_level) {
372 ats_entries = amdgpu_vm_level_shift(adev, level);
373 ats_entries += AMDGPU_GPU_PAGE_SHIFT;
374 ats_entries = AMDGPU_VA_HOLE_START >> ats_entries;
375 ats_entries = min(ats_entries, entries);
376 entries -= ats_entries;
377 } else {
378 ats_entries = entries;
379 entries = 0;
380 }
Christian König13307f72018-01-24 17:19:04 +0100381 } else {
Christian König45843122018-01-25 18:36:15 +0100382 ats_entries = 0;
Christian König13307f72018-01-24 17:19:04 +0100383 }
384
385 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
386
387 r = reservation_object_reserve_shared(bo->tbo.resv);
388 if (r)
389 return r;
390
391 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
392 if (r)
393 goto error;
394
Christian König13307f72018-01-24 17:19:04 +0100395 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
396 if (r)
397 goto error;
398
Christian König45843122018-01-25 18:36:15 +0100399 if (ats_entries) {
400 uint64_t ats_value;
401
402 ats_value = AMDGPU_PTE_DEFAULT_ATC;
403 if (level != AMDGPU_VM_PTB)
404 ats_value |= AMDGPU_PDE_PTE;
405
406 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
407 ats_entries, 0, ats_value);
408 addr += ats_entries * 8;
409 }
410
411 if (entries)
412 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
413 entries, 0, 0);
414
Christian König13307f72018-01-24 17:19:04 +0100415 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
416
417 WARN_ON(job->ibs[0].length_dw > 64);
Christian König29e83572018-02-04 19:36:52 +0100418 r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
419 AMDGPU_FENCE_OWNER_UNDEFINED, false);
420 if (r)
421 goto error_free;
422
Christian König13307f72018-01-24 17:19:04 +0100423 r = amdgpu_job_submit(job, ring, &vm->entity,
424 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
425 if (r)
426 goto error_free;
427
428 amdgpu_bo_fence(bo, fence, true);
429 dma_fence_put(fence);
Christian Könige61736d2018-02-02 21:05:40 +0100430
431 if (bo->shadow)
432 return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
433 level, pte_support_ats);
434
Christian König13307f72018-01-24 17:19:04 +0100435 return 0;
436
437error_free:
438 amdgpu_job_free(job);
439
440error:
441 return r;
442}
443
444/**
Christian Königf566ceb2016-10-27 20:04:38 +0200445 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
446 *
447 * @adev: amdgpu_device pointer
448 * @vm: requested vm
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400449 * @parent: parent PT
Christian Königf566ceb2016-10-27 20:04:38 +0200450 * @saddr: start of the address range
451 * @eaddr: end of the address range
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400452 * @level: VMPT level
453 * @ats: indicate ATS support from PTE
Christian Königf566ceb2016-10-27 20:04:38 +0200454 *
455 * Make sure the page directories and page tables are allocated
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400456 *
457 * Returns:
458 * 0 on success, errno otherwise.
Christian Königf566ceb2016-10-27 20:04:38 +0200459 */
460static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
461 struct amdgpu_vm *vm,
462 struct amdgpu_vm_pt *parent,
463 uint64_t saddr, uint64_t eaddr,
Christian König45843122018-01-25 18:36:15 +0100464 unsigned level, bool ats)
Christian Königf566ceb2016-10-27 20:04:38 +0200465{
Christian König50783142017-11-27 14:01:51 +0100466 unsigned shift = amdgpu_vm_level_shift(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200467 unsigned pt_idx, from, to;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400468 u64 flags;
Christian König13307f72018-01-24 17:19:04 +0100469 int r;
Christian Königf566ceb2016-10-27 20:04:38 +0200470
471 if (!parent->entries) {
472 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
473
Michal Hocko20981052017-05-17 14:23:12 +0200474 parent->entries = kvmalloc_array(num_entries,
475 sizeof(struct amdgpu_vm_pt),
476 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200477 if (!parent->entries)
478 return -ENOMEM;
479 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
480 }
481
Felix Kuehling1866bac2017-03-28 20:36:12 -0400482 from = saddr >> shift;
483 to = eaddr >> shift;
484 if (from >= amdgpu_vm_num_entries(adev, level) ||
485 to >= amdgpu_vm_num_entries(adev, level))
486 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200487
Christian Königf566ceb2016-10-27 20:04:38 +0200488 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400489 saddr = saddr & ((1 << shift) - 1);
490 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200491
Christian König13307f72018-01-24 17:19:04 +0100492 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400493 if (vm->use_cpu_for_update)
494 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
495 else
496 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
497 AMDGPU_GEM_CREATE_SHADOW);
498
Christian Königf566ceb2016-10-27 20:04:38 +0200499 /* walk over the address space and allocate the page tables */
500 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200501 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200502 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
503 struct amdgpu_bo *pt;
504
Christian König3f3333f2017-08-03 14:02:13 +0200505 if (!entry->base.bo) {
Chunming Zhou3216c6b2018-04-16 18:27:50 +0800506 struct amdgpu_bo_param bp;
507
508 memset(&bp, 0, sizeof(bp));
509 bp.size = amdgpu_vm_bo_size(adev, level);
510 bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
511 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
512 bp.flags = flags;
513 bp.type = ttm_bo_type_kernel;
514 bp.resv = resv;
515 r = amdgpu_bo_create(adev, &bp, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200516 if (r)
517 return r;
518
Christian König45843122018-01-25 18:36:15 +0100519 r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats);
Christian König13307f72018-01-24 17:19:04 +0100520 if (r) {
Christian Könige5197a42018-02-02 21:00:44 +0100521 amdgpu_bo_unref(&pt->shadow);
Christian König13307f72018-01-24 17:19:04 +0100522 amdgpu_bo_unref(&pt);
523 return r;
524 }
525
Christian König0a096fb2017-07-12 10:01:48 +0200526 if (vm->use_cpu_for_update) {
527 r = amdgpu_bo_kmap(pt, NULL);
528 if (r) {
Christian Könige5197a42018-02-02 21:00:44 +0100529 amdgpu_bo_unref(&pt->shadow);
Christian König0a096fb2017-07-12 10:01:48 +0200530 amdgpu_bo_unref(&pt);
531 return r;
532 }
533 }
534
Christian Königf566ceb2016-10-27 20:04:38 +0200535 /* Keep a reference to the root directory to avoid
536 * freeing them up in the wrong order.
537 */
Christian König0f2fc432017-08-31 10:46:20 +0200538 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200539
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800540 amdgpu_vm_bo_base_init(&entry->base, vm, pt);
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800541 list_move(&entry->base.vm_status, &vm->relocated);
Christian Königf566ceb2016-10-27 20:04:38 +0200542 }
543
Chunming Zhou196f7482017-12-13 14:22:54 +0800544 if (level < AMDGPU_VM_PTB) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400545 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
546 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
547 ((1 << shift) - 1);
548 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
Christian König45843122018-01-25 18:36:15 +0100549 sub_eaddr, level, ats);
Christian Königf566ceb2016-10-27 20:04:38 +0200550 if (r)
551 return r;
552 }
553 }
554
555 return 0;
556}
557
Christian König663e4572017-03-13 10:13:37 +0100558/**
559 * amdgpu_vm_alloc_pts - Allocate page tables.
560 *
561 * @adev: amdgpu_device pointer
562 * @vm: VM to allocate page tables for
563 * @saddr: Start address which needs to be allocated
564 * @size: Size from start address we need.
565 *
566 * Make sure the page tables are allocated.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400567 *
568 * Returns:
569 * 0 on success, errno otherwise.
Christian König663e4572017-03-13 10:13:37 +0100570 */
571int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
572 struct amdgpu_vm *vm,
573 uint64_t saddr, uint64_t size)
574{
Christian König663e4572017-03-13 10:13:37 +0100575 uint64_t eaddr;
Christian König45843122018-01-25 18:36:15 +0100576 bool ats = false;
Christian König663e4572017-03-13 10:13:37 +0100577
578 /* validate the parameters */
579 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
580 return -EINVAL;
581
582 eaddr = saddr + size - 1;
Christian König45843122018-01-25 18:36:15 +0100583
584 if (vm->pte_support_ats)
585 ats = saddr < AMDGPU_VA_HOLE_START;
Christian König663e4572017-03-13 10:13:37 +0100586
587 saddr /= AMDGPU_GPU_PAGE_SIZE;
588 eaddr /= AMDGPU_GPU_PAGE_SIZE;
589
Christian König45843122018-01-25 18:36:15 +0100590 if (eaddr >= adev->vm_manager.max_pfn) {
591 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
592 eaddr, adev->vm_manager.max_pfn);
593 return -EINVAL;
594 }
595
Chunming Zhou196f7482017-12-13 14:22:54 +0800596 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
Christian König45843122018-01-25 18:36:15 +0100597 adev->vm_manager.root_level, ats);
Christian König663e4572017-03-13 10:13:37 +0100598}
599
Christian König641e9402017-04-03 13:59:25 +0200600/**
Alex Xiee59c0202017-06-01 09:42:59 -0400601 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
602 *
603 * @adev: amdgpu_device pointer
604 */
605void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
606{
607 const struct amdgpu_ip_block *ip_block;
608 bool has_compute_vm_bug;
609 struct amdgpu_ring *ring;
610 int i;
611
612 has_compute_vm_bug = false;
613
Alex Deucher2990a1f2017-12-15 16:18:00 -0500614 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
Alex Xiee59c0202017-06-01 09:42:59 -0400615 if (ip_block) {
616 /* Compute has a VM bug for GFX version < 7.
617 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
618 if (ip_block->version->major <= 7)
619 has_compute_vm_bug = true;
620 else if (ip_block->version->major == 8)
621 if (adev->gfx.mec_fw_version < 673)
622 has_compute_vm_bug = true;
623 }
624
625 for (i = 0; i < adev->num_rings; i++) {
626 ring = adev->rings[i];
627 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
628 /* only compute rings */
629 ring->has_compute_vm_bug = has_compute_vm_bug;
630 else
631 ring->has_compute_vm_bug = false;
632 }
633}
634
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400635/**
636 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
637 *
638 * @ring: ring on which the job will be submitted
639 * @job: job to submit
640 *
641 * Returns:
642 * True if sync is needed.
643 */
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400644bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
645 struct amdgpu_job *job)
646{
647 struct amdgpu_device *adev = ring->adev;
648 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100649 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
650 struct amdgpu_vmid *id;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400651 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400652 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400653
Christian Königc4f46f22017-12-18 17:08:25 +0100654 if (job->vmid == 0)
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400655 return false;
Christian Königc4f46f22017-12-18 17:08:25 +0100656 id = &id_mgr->ids[job->vmid];
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400657 gds_switch_needed = ring->funcs->emit_gds_switch && (
658 id->gds_base != job->gds_base ||
659 id->gds_size != job->gds_size ||
660 id->gws_base != job->gws_base ||
661 id->gws_size != job->gws_size ||
662 id->oa_base != job->oa_base ||
663 id->oa_size != job->oa_size);
664
Christian König620f7742017-12-18 16:53:03 +0100665 if (amdgpu_vmid_had_gpu_reset(adev, id))
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400666 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400667
668 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400669}
670
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400671/**
672 * amdgpu_vm_is_large_bar - Check if BAR is large enough
673 *
674 * @adev: amdgpu_device pointer
675 *
676 * Returns:
677 * True if BAR is large enough.
678 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400679static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
680{
Christian König770d13b2018-01-12 14:52:22 +0100681 return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size);
Alex Xiee60f8db2017-03-09 11:36:26 -0500682}
683
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684/**
685 * amdgpu_vm_flush - hardware flush the vm
686 *
687 * @ring: ring to use for flush
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400688 * @need_pipe_sync: is pipe sync needed
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689 *
Christian König4ff37a82016-02-26 16:18:26 +0100690 * Emit a VM flush when it is necessary.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400691 *
692 * Returns:
693 * 0 on success, errno otherwise.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800695int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696{
Christian König971fe9a92016-03-01 15:09:25 +0100697 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200698 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100699 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian Königc4f46f22017-12-18 17:08:25 +0100700 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
Christian Königd564a062016-03-01 15:51:53 +0100701 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800702 id->gds_base != job->gds_base ||
703 id->gds_size != job->gds_size ||
704 id->gws_base != job->gws_base ||
705 id->gws_size != job->gws_size ||
706 id->oa_base != job->oa_base ||
707 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800708 bool vm_flush_needed = job->vm_needs_flush;
Christian Königb3cd2852018-02-05 17:38:01 +0100709 bool pasid_mapping_needed = id->pasid != job->pasid ||
710 !id->pasid_mapping ||
711 !dma_fence_is_signaled(id->pasid_mapping);
712 struct dma_fence *fence = NULL;
Christian Königc0e51932017-04-03 14:16:07 +0200713 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100714 int r;
Christian Königd564a062016-03-01 15:51:53 +0100715
Christian König620f7742017-12-18 16:53:03 +0100716 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
Christian Königf7d015b2017-04-03 14:28:26 +0200717 gds_switch_needed = true;
718 vm_flush_needed = true;
Christian Königb3cd2852018-02-05 17:38:01 +0100719 pasid_mapping_needed = true;
Christian Königf7d015b2017-04-03 14:28:26 +0200720 }
Christian König971fe9a92016-03-01 15:09:25 +0100721
Christian Königb3cd2852018-02-05 17:38:01 +0100722 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
723 vm_flush_needed &= !!ring->funcs->emit_vm_flush;
724 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
725 ring->funcs->emit_wreg;
726
Monk Liu8fdf0742017-06-06 17:25:13 +0800727 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200728 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100729
Christian Königc0e51932017-04-03 14:16:07 +0200730 if (ring->funcs->init_cond_exec)
731 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100732
Monk Liu8fdf0742017-06-06 17:25:13 +0800733 if (need_pipe_sync)
734 amdgpu_ring_emit_pipeline_sync(ring);
735
Christian Königb3cd2852018-02-05 17:38:01 +0100736 if (vm_flush_needed) {
Christian Königc4f46f22017-12-18 17:08:25 +0100737 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian Königc633c002018-02-04 10:32:35 +0100738 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian Königb3cd2852018-02-05 17:38:01 +0100739 }
Monk Liue9d672b2017-03-15 12:18:57 +0800740
Christian Königb3cd2852018-02-05 17:38:01 +0100741 if (pasid_mapping_needed)
742 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
743
744 if (vm_flush_needed || pasid_mapping_needed) {
Marek Olšákd240cd92018-04-03 13:05:03 -0400745 r = amdgpu_fence_emit(ring, &fence, 0);
Christian Königc0e51932017-04-03 14:16:07 +0200746 if (r)
747 return r;
Christian Königb3cd2852018-02-05 17:38:01 +0100748 }
Monk Liue9d672b2017-03-15 12:18:57 +0800749
Christian Königb3cd2852018-02-05 17:38:01 +0100750 if (vm_flush_needed) {
Christian König76456702017-04-06 17:52:39 +0200751 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200752 dma_fence_put(id->last_flush);
Christian Königb3cd2852018-02-05 17:38:01 +0100753 id->last_flush = dma_fence_get(fence);
754 id->current_gpu_reset_count =
755 atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200756 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200757 }
Monk Liue9d672b2017-03-15 12:18:57 +0800758
Christian Königb3cd2852018-02-05 17:38:01 +0100759 if (pasid_mapping_needed) {
760 id->pasid = job->pasid;
761 dma_fence_put(id->pasid_mapping);
762 id->pasid_mapping = dma_fence_get(fence);
763 }
764 dma_fence_put(fence);
765
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800766 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200767 id->gds_base = job->gds_base;
768 id->gds_size = job->gds_size;
769 id->gws_base = job->gws_base;
770 id->gws_size = job->gws_size;
771 id->oa_base = job->oa_base;
772 id->oa_size = job->oa_size;
Christian Königc4f46f22017-12-18 17:08:25 +0100773 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
Christian Königc0e51932017-04-03 14:16:07 +0200774 job->gds_size, job->gws_base,
775 job->gws_size, job->oa_base,
776 job->oa_size);
777 }
778
779 if (ring->funcs->patch_cond_exec)
780 amdgpu_ring_patch_cond_exec(ring, patch_offset);
781
782 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
783 if (ring->funcs->emit_switch_buffer) {
784 amdgpu_ring_emit_switch_buffer(ring);
785 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786 }
Christian König41d9eb22016-03-01 16:46:18 +0100787 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100788}
789
790/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400791 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
792 *
793 * @vm: requested vm
794 * @bo: requested buffer object
795 *
Christian König8843dbb2016-01-26 12:17:11 +0100796 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797 * Search inside the @bos vm list for the requested vm
798 * Returns the found bo_va or NULL if none is found
799 *
800 * Object has to be reserved!
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400801 *
802 * Returns:
803 * Found bo_va or NULL.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 */
805struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
806 struct amdgpu_bo *bo)
807{
808 struct amdgpu_bo_va *bo_va;
809
Christian Königec681542017-08-01 10:51:43 +0200810 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
811 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400812 return bo_va;
813 }
814 }
815 return NULL;
816}
817
818/**
Christian Königafef8b82016-08-12 13:29:18 +0200819 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820 *
Christian König29efc4f2016-08-04 14:52:50 +0200821 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100822 * @bo: PD/PT to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823 * @pe: addr of the page entry
824 * @addr: dst addr to write into pe
825 * @count: number of page entries to update
826 * @incr: increase next addr by incr bytes
827 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400828 *
829 * Traces the parameters and calls the right asic functions
830 * to setup the page table using the DMA.
831 */
Christian Königafef8b82016-08-12 13:29:18 +0200832static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100833 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200834 uint64_t pe, uint64_t addr,
835 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800836 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400837{
Christian König373ac642018-01-16 16:54:25 +0100838 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200839 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840
Christian Königafef8b82016-08-12 13:29:18 +0200841 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200842 amdgpu_vm_write_pte(params->adev, params->ib, pe,
843 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844
845 } else {
Christian König27c5f362016-08-04 15:02:49 +0200846 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400847 count, incr, flags);
848 }
849}
850
851/**
Christian Königafef8b82016-08-12 13:29:18 +0200852 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
853 *
854 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100855 * @bo: PD/PT to update
Christian Königafef8b82016-08-12 13:29:18 +0200856 * @pe: addr of the page entry
857 * @addr: dst addr to write into pe
858 * @count: number of page entries to update
859 * @incr: increase next addr by incr bytes
860 * @flags: hw access flags
861 *
862 * Traces the parameters and calls the DMA function to copy the PTEs.
863 */
864static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100865 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200866 uint64_t pe, uint64_t addr,
867 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800868 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200869{
Christian Königec2f05f2016-09-25 16:11:52 +0200870 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200871
Christian König373ac642018-01-16 16:54:25 +0100872 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200873 trace_amdgpu_vm_copy_ptes(pe, src, count);
874
875 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200876}
877
878/**
Christian Königb07c9d22015-11-30 13:26:07 +0100879 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880 *
Christian Königb07c9d22015-11-30 13:26:07 +0100881 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400882 * @addr: the unmapped addr
883 *
884 * Look up the physical address of the page that the pte resolves
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400885 * to.
886 *
887 * Returns:
888 * The pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200890static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400891{
892 uint64_t result;
893
Christian Königde9ea7b2016-08-12 11:33:30 +0200894 /* page table offset */
895 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400896
Christian Königde9ea7b2016-08-12 11:33:30 +0200897 /* in case cpu page size != gpu page size*/
898 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100899
900 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400901
902 return result;
903}
904
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400905/**
906 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
907 *
908 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100909 * @bo: PD/PT to update
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400910 * @pe: kmap addr of the page entry
911 * @addr: dst addr to write into pe
912 * @count: number of page entries to update
913 * @incr: increase next addr by incr bytes
914 * @flags: hw access flags
915 *
916 * Write count number of PT/PD entries directly.
917 */
918static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100919 struct amdgpu_bo *bo,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400920 uint64_t pe, uint64_t addr,
921 unsigned count, uint32_t incr,
922 uint64_t flags)
923{
924 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400925 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400926
Christian König373ac642018-01-16 16:54:25 +0100927 pe += (unsigned long)amdgpu_bo_kptr(bo);
928
Christian König03918b32017-07-11 17:15:37 +0200929 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
930
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400931 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400932 value = params->pages_addr ?
933 amdgpu_vm_map_gart(params->pages_addr, addr) :
934 addr;
Christian König132f34e2018-01-12 15:26:08 +0100935 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
936 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400937 addr += incr;
938 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400939}
940
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400941
942/**
943 * amdgpu_vm_wait_pd - Wait for PT BOs to be free.
944 *
945 * @adev: amdgpu_device pointer
946 * @vm: related vm
947 * @owner: fence owner
948 *
949 * Returns:
950 * 0 on success, errno otherwise.
951 */
Christian Königa33cab72017-07-11 17:13:00 +0200952static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
953 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400954{
955 struct amdgpu_sync sync;
956 int r;
957
958 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -0400959 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400960 r = amdgpu_sync_wait(&sync, true);
961 amdgpu_sync_free(&sync);
962
963 return r;
964}
965
Christian Königf8991ba2016-09-16 15:36:49 +0200966/*
Christian König6989f242017-11-30 19:08:05 +0100967 * amdgpu_vm_update_pde - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200968 *
Christian König6989f242017-11-30 19:08:05 +0100969 * @param: parameters for the update
Christian Königf8991ba2016-09-16 15:36:49 +0200970 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200971 * @parent: parent directory
Christian König6989f242017-11-30 19:08:05 +0100972 * @entry: entry to update
Christian Königf8991ba2016-09-16 15:36:49 +0200973 *
Christian König6989f242017-11-30 19:08:05 +0100974 * Makes sure the requested entry in parent is up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200975 */
Christian König6989f242017-11-30 19:08:05 +0100976static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
977 struct amdgpu_vm *vm,
978 struct amdgpu_vm_pt *parent,
979 struct amdgpu_vm_pt *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980{
Christian König373ac642018-01-16 16:54:25 +0100981 struct amdgpu_bo *bo = parent->base.bo, *pbo;
Christian König3de676d2017-11-29 13:27:26 +0100982 uint64_t pde, pt, flags;
983 unsigned level;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800984
Christian König6989f242017-11-30 19:08:05 +0100985 /* Don't update huge pages here */
986 if (entry->huge)
987 return;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400988
Christian König373ac642018-01-16 16:54:25 +0100989 for (level = 0, pbo = bo->parent; pbo; ++level)
Christian König3de676d2017-11-29 13:27:26 +0100990 pbo = pbo->parent;
991
Chunming Zhou196f7482017-12-13 14:22:54 +0800992 level += params->adev->vm_manager.root_level;
Christian König373ac642018-01-16 16:54:25 +0100993 pt = amdgpu_bo_gpu_offset(entry->base.bo);
Christian König3de676d2017-11-29 13:27:26 +0100994 flags = AMDGPU_PTE_VALID;
Christian König132f34e2018-01-12 15:26:08 +0100995 amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
Christian König373ac642018-01-16 16:54:25 +0100996 pde = (entry - parent->entries) * 8;
997 if (bo->shadow)
998 params->func(params, bo->shadow, pde, pt, 1, 0, flags);
999 params->func(params, bo, pde, pt, 1, 0, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000}
1001
Christian König194d2162016-10-12 15:13:52 +02001002/*
Christian König92456b92017-05-12 16:09:26 +02001003 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1004 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001005 * @adev: amdgpu_device pointer
1006 * @vm: related vm
Christian König92456b92017-05-12 16:09:26 +02001007 * @parent: parent PD
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001008 * @level: VMPT level
Christian König92456b92017-05-12 16:09:26 +02001009 *
1010 * Mark all PD level as invalid after an error.
1011 */
Christian König8f19cd72017-11-30 15:28:03 +01001012static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
1013 struct amdgpu_vm *vm,
1014 struct amdgpu_vm_pt *parent,
1015 unsigned level)
Christian König92456b92017-05-12 16:09:26 +02001016{
Christian König8f19cd72017-11-30 15:28:03 +01001017 unsigned pt_idx, num_entries;
Christian König92456b92017-05-12 16:09:26 +02001018
1019 /*
1020 * Recurse into the subdirectories. This recursion is harmless because
1021 * we only have a maximum of 5 layers.
1022 */
Christian König8f19cd72017-11-30 15:28:03 +01001023 num_entries = amdgpu_vm_num_entries(adev, level);
1024 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
Christian König92456b92017-05-12 16:09:26 +02001025 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1026
Christian König3f3333f2017-08-03 14:02:13 +02001027 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001028 continue;
1029
Christian König862b8c52018-04-19 14:22:56 +02001030 if (!entry->base.moved)
1031 list_move(&entry->base.vm_status, &vm->relocated);
Christian König8f19cd72017-11-30 15:28:03 +01001032 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
Christian König92456b92017-05-12 16:09:26 +02001033 }
1034}
1035
1036/*
Christian König194d2162016-10-12 15:13:52 +02001037 * amdgpu_vm_update_directories - make sure that all directories are valid
1038 *
1039 * @adev: amdgpu_device pointer
1040 * @vm: requested vm
1041 *
1042 * Makes sure all directories are up to date.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001043 *
1044 * Returns:
1045 * 0 for success, error for failure.
Christian König194d2162016-10-12 15:13:52 +02001046 */
1047int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1048 struct amdgpu_vm *vm)
1049{
Christian König6989f242017-11-30 19:08:05 +01001050 struct amdgpu_pte_update_params params;
1051 struct amdgpu_job *job;
1052 unsigned ndw = 0;
Dan Carpenter78aa02c2017-09-30 11:14:13 +03001053 int r = 0;
Christian König92456b92017-05-12 16:09:26 +02001054
Christian König6989f242017-11-30 19:08:05 +01001055 if (list_empty(&vm->relocated))
1056 return 0;
1057
1058restart:
1059 memset(&params, 0, sizeof(params));
1060 params.adev = adev;
1061
1062 if (vm->use_cpu_for_update) {
Christian Königa7f91062018-04-19 13:58:42 +02001063 struct amdgpu_vm_bo_base *bo_base;
1064
1065 list_for_each_entry(bo_base, &vm->relocated, vm_status) {
1066 r = amdgpu_bo_kmap(bo_base->bo, NULL);
1067 if (unlikely(r))
1068 return r;
1069 }
1070
Christian König6989f242017-11-30 19:08:05 +01001071 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
1072 if (unlikely(r))
1073 return r;
1074
1075 params.func = amdgpu_vm_cpu_set_ptes;
1076 } else {
1077 ndw = 512 * 8;
1078 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1079 if (r)
1080 return r;
1081
1082 params.ib = &job->ibs[0];
1083 params.func = amdgpu_vm_do_set_ptes;
1084 }
1085
Christian Königea097292017-08-09 14:15:46 +02001086 while (!list_empty(&vm->relocated)) {
Christian König6989f242017-11-30 19:08:05 +01001087 struct amdgpu_vm_bo_base *bo_base, *parent;
1088 struct amdgpu_vm_pt *pt, *entry;
Christian Königea097292017-08-09 14:15:46 +02001089 struct amdgpu_bo *bo;
1090
1091 bo_base = list_first_entry(&vm->relocated,
1092 struct amdgpu_vm_bo_base,
1093 vm_status);
Christian König862b8c52018-04-19 14:22:56 +02001094 bo_base->moved = false;
Christian König806f0432018-04-19 15:01:12 +02001095 list_move(&bo_base->vm_status, &vm->idle);
Christian Königea097292017-08-09 14:15:46 +02001096
1097 bo = bo_base->bo->parent;
Christian Königaf4c0f62018-04-19 10:56:02 +02001098 if (!bo)
Christian König6989f242017-11-30 19:08:05 +01001099 continue;
Christian König6989f242017-11-30 19:08:05 +01001100
1101 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
1102 bo_list);
1103 pt = container_of(parent, struct amdgpu_vm_pt, base);
1104 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
1105
1106 amdgpu_vm_update_pde(&params, vm, pt, entry);
1107
Christian König6989f242017-11-30 19:08:05 +01001108 if (!vm->use_cpu_for_update &&
1109 (ndw - params.ib->length_dw) < 32)
1110 break;
Christian Königea097292017-08-09 14:15:46 +02001111 }
Christian König92456b92017-05-12 16:09:26 +02001112
Christian König68c62302017-07-11 17:23:29 +02001113 if (vm->use_cpu_for_update) {
1114 /* Flush HDP */
1115 mb();
Christian König69882562018-01-19 14:17:40 +01001116 amdgpu_asic_flush_hdp(adev, NULL);
Christian König6989f242017-11-30 19:08:05 +01001117 } else if (params.ib->length_dw == 0) {
1118 amdgpu_job_free(job);
1119 } else {
1120 struct amdgpu_bo *root = vm->root.base.bo;
1121 struct amdgpu_ring *ring;
1122 struct dma_fence *fence;
1123
1124 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1125 sched);
1126
1127 amdgpu_ring_pad_ib(ring, params.ib);
1128 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
1129 AMDGPU_FENCE_OWNER_VM, false);
Christian König6989f242017-11-30 19:08:05 +01001130 WARN_ON(params.ib->length_dw > ndw);
1131 r = amdgpu_job_submit(job, ring, &vm->entity,
1132 AMDGPU_FENCE_OWNER_VM, &fence);
1133 if (r)
1134 goto error;
1135
1136 amdgpu_bo_fence(root, fence, true);
1137 dma_fence_put(vm->last_update);
1138 vm->last_update = fence;
Christian König68c62302017-07-11 17:23:29 +02001139 }
1140
Christian König6989f242017-11-30 19:08:05 +01001141 if (!list_empty(&vm->relocated))
1142 goto restart;
1143
1144 return 0;
1145
1146error:
Chunming Zhou196f7482017-12-13 14:22:54 +08001147 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
1148 adev->vm_manager.root_level);
Christian König6989f242017-11-30 19:08:05 +01001149 amdgpu_job_free(job);
Christian König92456b92017-05-12 16:09:26 +02001150 return r;
Christian König194d2162016-10-12 15:13:52 +02001151}
1152
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001154 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001155 *
1156 * @p: see amdgpu_pte_update_params definition
1157 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001158 * @entry: resulting entry or NULL
1159 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001160 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001161 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001162 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001163void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1164 struct amdgpu_vm_pt **entry,
1165 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001166{
Chunming Zhou196f7482017-12-13 14:22:54 +08001167 unsigned level = p->adev->vm_manager.root_level;
Christian König4e2cb642016-10-25 15:52:28 +02001168
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001169 *parent = NULL;
1170 *entry = &p->vm->root;
1171 while ((*entry)->entries) {
Christian Könige3a1b322017-12-01 13:28:46 +01001172 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
Christian König50783142017-11-27 14:01:51 +01001173
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001174 *parent = *entry;
Christian Könige3a1b322017-12-01 13:28:46 +01001175 *entry = &(*entry)->entries[addr >> shift];
1176 addr &= (1ULL << shift) - 1;
Christian König4e2cb642016-10-25 15:52:28 +02001177 }
1178
Chunming Zhou196f7482017-12-13 14:22:54 +08001179 if (level != AMDGPU_VM_PTB)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001180 *entry = NULL;
1181}
Christian König4e2cb642016-10-25 15:52:28 +02001182
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001183/**
1184 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1185 *
1186 * @p: see amdgpu_pte_update_params definition
1187 * @entry: vm_pt entry to check
1188 * @parent: parent entry
1189 * @nptes: number of PTEs updated with this operation
1190 * @dst: destination address where the PTEs should point to
1191 * @flags: access flags fro the PTEs
1192 *
1193 * Check if we can update the PD with a huge page.
1194 */
Christian Königec5207c2017-08-03 19:24:06 +02001195static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1196 struct amdgpu_vm_pt *entry,
1197 struct amdgpu_vm_pt *parent,
1198 unsigned nptes, uint64_t dst,
1199 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001200{
Christian König373ac642018-01-16 16:54:25 +01001201 uint64_t pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001202
1203 /* In the case of a mixed PT the PDE must point to it*/
Christian König3cc1d3e2017-12-21 15:47:28 +01001204 if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
1205 nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
Christian König4ab40162017-08-03 20:30:50 +02001206 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001207 flags |= AMDGPU_PDE_PTE;
1208 }
1209
Christian König3cc1d3e2017-12-21 15:47:28 +01001210 if (!(flags & AMDGPU_PDE_PTE)) {
1211 if (entry->huge) {
1212 /* Add the entry to the relocated list to update it. */
1213 entry->huge = false;
Christian König3cc1d3e2017-12-21 15:47:28 +01001214 list_move(&entry->base.vm_status, &p->vm->relocated);
Christian König3cc1d3e2017-12-21 15:47:28 +01001215 }
Christian Königec5207c2017-08-03 19:24:06 +02001216 return;
Christian König3cc1d3e2017-12-21 15:47:28 +01001217 }
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001218
Christian König3cc1d3e2017-12-21 15:47:28 +01001219 entry->huge = true;
Christian König132f34e2018-01-12 15:26:08 +01001220 amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
Christian König3de676d2017-11-29 13:27:26 +01001221
Christian König373ac642018-01-16 16:54:25 +01001222 pde = (entry - parent->entries) * 8;
1223 if (parent->base.bo->shadow)
1224 p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
1225 p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
Christian König4e2cb642016-10-25 15:52:28 +02001226}
1227
1228/**
Christian König92696dd2016-08-05 13:56:35 +02001229 * amdgpu_vm_update_ptes - make sure that page tables are valid
1230 *
1231 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001232 * @start: start of GPU address range
1233 * @end: end of GPU address range
1234 * @dst: destination address to map to, the next dst inside the function
1235 * @flags: mapping flags
1236 *
1237 * Update the page tables in the range @start - @end.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001238 *
1239 * Returns:
1240 * 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001241 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001242static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001243 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001244 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001245{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001246 struct amdgpu_device *adev = params->adev;
1247 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001248
Christian König301654a2017-05-16 14:30:27 +02001249 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001250 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001251 unsigned nptes;
Christian König92696dd2016-08-05 13:56:35 +02001252
1253 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001254 for (addr = start; addr < end; addr += nptes,
1255 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1256 struct amdgpu_vm_pt *entry, *parent;
1257
1258 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1259 if (!entry)
1260 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001261
Christian König92696dd2016-08-05 13:56:35 +02001262 if ((addr & ~mask) == (end & ~mask))
1263 nptes = end - addr;
1264 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001265 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001266
Christian Königec5207c2017-08-03 19:24:06 +02001267 amdgpu_vm_handle_huge_pages(params, entry, parent,
1268 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001269 /* We don't need to update PTEs for huge pages */
Christian König78eb2f02017-11-30 15:41:28 +01001270 if (entry->huge)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001271 continue;
1272
Christian König3f3333f2017-08-03 14:02:13 +02001273 pt = entry->base.bo;
Christian König373ac642018-01-16 16:54:25 +01001274 pe_start = (addr & mask) * 8;
1275 if (pt->shadow)
1276 params->func(params, pt->shadow, pe_start, dst, nptes,
1277 AMDGPU_GPU_PAGE_SIZE, flags);
1278 params->func(params, pt, pe_start, dst, nptes,
Christian König301654a2017-05-16 14:30:27 +02001279 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001280 }
1281
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001282 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001283}
1284
1285/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001286 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1287 *
Christian König29efc4f2016-08-04 14:52:50 +02001288 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001289 * @vm: requested vm
1290 * @start: first PTE to handle
1291 * @end: last PTE to handle
1292 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001293 * @flags: hw mapping flags
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001294 *
1295 * Returns:
1296 * 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001298static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001299 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001300 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301{
1302 /**
1303 * The MC L1 TLB supports variable sized pages, based on a fragment
1304 * field in the PTE. When this field is set to a non-zero value, page
1305 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1306 * flags are considered valid for all PTEs within the fragment range
1307 * and corresponding mappings are assumed to be physically contiguous.
1308 *
1309 * The L1 TLB can store a single PTE for the whole fragment,
1310 * significantly increasing the space available for translation
1311 * caching. This leads to large improvements in throughput when the
1312 * TLB is under pressure.
1313 *
1314 * The L2 TLB distributes small and large fragments into two
1315 * asymmetric partitions. The large fragment cache is significantly
1316 * larger. Thus, we try to use large fragments wherever possible.
1317 * Userspace can support this by aligning virtual base address and
1318 * allocation size to the fragment size.
1319 */
Roger He6849d472017-08-30 13:01:19 +08001320 unsigned max_frag = params->adev->vm_manager.fragment_size;
1321 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001322
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001324 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001325 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001326
Roger He6849d472017-08-30 13:01:19 +08001327 while (start != end) {
1328 uint64_t frag_flags, frag_end;
1329 unsigned frag;
1330
1331 /* This intentionally wraps around if no bit is set */
1332 frag = min((unsigned)ffs(start) - 1,
1333 (unsigned)fls64(end - start) - 1);
1334 if (frag >= max_frag) {
1335 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1336 frag_end = end & ~((1ULL << max_frag) - 1);
1337 } else {
1338 frag_flags = AMDGPU_PTE_FRAG(frag);
1339 frag_end = start + (1 << frag);
1340 }
1341
1342 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1343 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001344 if (r)
1345 return r;
Roger He6849d472017-08-30 13:01:19 +08001346
1347 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1348 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 }
1350
Roger He6849d472017-08-30 13:01:19 +08001351 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001352}
1353
1354/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001355 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1356 *
1357 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001358 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001359 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001360 * @vm: requested vm
1361 * @start: start of mapped range
1362 * @last: last mapped entry
1363 * @flags: flags for the entries
1364 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001365 * @fence: optional resulting fence
1366 *
Christian Königa14faa62016-01-25 14:27:31 +01001367 * Fill in the page table entries between @start and @last.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001368 *
1369 * Returns:
1370 * 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371 */
1372static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001373 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001374 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001375 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001376 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001377 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001378 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379{
Christian König2d55e452016-02-08 17:37:38 +01001380 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001381 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001382 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001383 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001384 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001385 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 int r;
1387
Christian Königafef8b82016-08-12 13:29:18 +02001388 memset(&params, 0, sizeof(params));
1389 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001390 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001391
Christian Königa33cab72017-07-11 17:13:00 +02001392 /* sync to everything on unmapping */
1393 if (!(flags & AMDGPU_PTE_VALID))
1394 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1395
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001396 if (vm->use_cpu_for_update) {
1397 /* params.src is used as flag to indicate system Memory */
1398 if (pages_addr)
1399 params.src = ~0;
1400
1401 /* Wait for PT BOs to be free. PTs share the same resv. object
1402 * as the root PD BO
1403 */
Christian Königa33cab72017-07-11 17:13:00 +02001404 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001405 if (unlikely(r))
1406 return r;
1407
1408 params.func = amdgpu_vm_cpu_set_ptes;
1409 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001410 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1411 addr, flags);
1412 }
1413
Christian König2d55e452016-02-08 17:37:38 +01001414 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001415
Christian Königa14faa62016-01-25 14:27:31 +01001416 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417
1418 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001419 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001421 *
1422 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001423 */
Emily Deng104bd2c2017-12-29 13:13:08 +08001424 if (vm->root.base.bo->shadow)
1425 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1426 else
1427 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428
1429 /* padding, etc. */
1430 ndw = 64;
1431
Christian König570144c2017-08-30 15:38:45 +02001432 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001433 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001434 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001435
Christian Königb0456f92016-08-11 14:06:54 +02001436 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437 ndw += nptes * 2;
1438
Christian Königafef8b82016-08-12 13:29:18 +02001439 params.func = amdgpu_vm_do_copy_ptes;
1440
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001441 } else {
1442 /* set page commands needed */
Christian König44e1bae2018-01-24 19:58:45 +01001443 ndw += ncmds * 10;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444
Roger He6849d472017-08-30 13:01:19 +08001445 /* extra commands for begin/end fragments */
Emily Deng11528642018-06-08 16:36:22 +08001446 if (vm->root.base.bo->shadow)
1447 ndw += 2 * 10 * adev->vm_manager.fragment_size * 2;
1448 else
1449 ndw += 2 * 10 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001450
1451 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001452 }
1453
Christian Königd71518b2016-02-01 12:20:25 +01001454 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1455 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001456 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001457
Christian König29efc4f2016-08-04 14:52:50 +02001458 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001459
Christian König570144c2017-08-30 15:38:45 +02001460 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001461 uint64_t *pte;
1462 unsigned i;
1463
1464 /* Put the PTEs at the end of the IB. */
1465 i = ndw - nptes * 2;
1466 pte= (uint64_t *)&(job->ibs->ptr[i]);
1467 params.src = job->ibs->gpu_addr + i * 4;
1468
1469 for (i = 0; i < nptes; ++i) {
1470 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1471 AMDGPU_GPU_PAGE_SIZE);
1472 pte[i] |= flags;
1473 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001474 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001475 }
1476
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -05001477 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
Christian König3cabaa52016-06-06 10:17:58 +02001478 if (r)
1479 goto error_free;
1480
Christian König3f3333f2017-08-03 14:02:13 +02001481 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001482 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001483 if (r)
1484 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001485
Christian König3f3333f2017-08-03 14:02:13 +02001486 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001487 if (r)
1488 goto error_free;
1489
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001490 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1491 if (r)
1492 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493
Christian König29efc4f2016-08-04 14:52:50 +02001494 amdgpu_ring_pad_ib(ring, params.ib);
1495 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001496 r = amdgpu_job_submit(job, ring, &vm->entity,
1497 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001498 if (r)
1499 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001500
Christian König3f3333f2017-08-03 14:02:13 +02001501 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001502 dma_fence_put(*fence);
1503 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001504 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001505
1506error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001507 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001508 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509}
1510
1511/**
Christian Königa14faa62016-01-25 14:27:31 +01001512 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1513 *
1514 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001515 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001516 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001517 * @vm: requested vm
1518 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001519 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001520 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001521 * @fence: optional resulting fence
1522 *
1523 * Split the mapping into smaller chunks so that each update fits
1524 * into a SDMA IB.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001525 *
1526 * Returns:
1527 * 0 for success, -EINVAL for failure.
Christian Königa14faa62016-01-25 14:27:31 +01001528 */
1529static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001530 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001531 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001532 struct amdgpu_vm *vm,
1533 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001534 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001535 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001536 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001537{
Christian König9fc8fc72017-09-18 13:58:30 +02001538 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001539 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001540 int r;
1541
1542 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1543 * but in case of something, we filter the flags in first place
1544 */
1545 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1546 flags &= ~AMDGPU_PTE_READABLE;
1547 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1548 flags &= ~AMDGPU_PTE_WRITEABLE;
1549
Alex Xie15b31c52017-03-03 16:47:11 -05001550 flags &= ~AMDGPU_PTE_EXECUTABLE;
1551 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1552
Alex Xieb0fd18b2017-03-03 16:49:39 -05001553 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1554 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1555
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001556 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1557 (adev->asic_type >= CHIP_VEGA10)) {
1558 flags |= AMDGPU_PTE_PRT;
1559 flags &= ~AMDGPU_PTE_VALID;
1560 }
1561
Christian Königa14faa62016-01-25 14:27:31 +01001562 trace_amdgpu_vm_bo_update(mapping);
1563
Christian König63e0ba42016-08-16 17:38:37 +02001564 pfn = mapping->offset >> PAGE_SHIFT;
1565 if (nodes) {
1566 while (pfn >= nodes->size) {
1567 pfn -= nodes->size;
1568 ++nodes;
1569 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001570 }
Christian Königa14faa62016-01-25 14:27:31 +01001571
Christian König63e0ba42016-08-16 17:38:37 +02001572 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001573 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001574 uint64_t max_entries;
1575 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001576
Christian König63e0ba42016-08-16 17:38:37 +02001577 if (nodes) {
1578 addr = nodes->start << PAGE_SHIFT;
1579 max_entries = (nodes->size - pfn) *
1580 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1581 } else {
1582 addr = 0;
1583 max_entries = S64_MAX;
1584 }
Christian Königa14faa62016-01-25 14:27:31 +01001585
Christian König63e0ba42016-08-16 17:38:37 +02001586 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001587 uint64_t count;
1588
Christian König457e0fe2017-08-22 12:50:46 +02001589 max_entries = min(max_entries, 16ull * 1024ull);
Christian König9fc8fc72017-09-18 13:58:30 +02001590 for (count = 1; count < max_entries; ++count) {
1591 uint64_t idx = pfn + count;
1592
1593 if (pages_addr[idx] !=
1594 (pages_addr[idx - 1] + PAGE_SIZE))
1595 break;
1596 }
1597
1598 if (count < min_linear_pages) {
1599 addr = pfn << PAGE_SHIFT;
1600 dma_addr = pages_addr;
1601 } else {
1602 addr = pages_addr[pfn];
1603 max_entries = count;
1604 }
1605
Christian König63e0ba42016-08-16 17:38:37 +02001606 } else if (flags & AMDGPU_PTE_VALID) {
1607 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001608 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001609 }
Christian König63e0ba42016-08-16 17:38:37 +02001610
Christian Königa9f87f62017-03-30 14:03:59 +02001611 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001612 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001613 start, last, flags, addr,
1614 fence);
1615 if (r)
1616 return r;
1617
Christian König63e0ba42016-08-16 17:38:37 +02001618 pfn += last - start + 1;
1619 if (nodes && nodes->size == pfn) {
1620 pfn = 0;
1621 ++nodes;
1622 }
Christian Königa14faa62016-01-25 14:27:31 +01001623 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001624
Christian Königa9f87f62017-03-30 14:03:59 +02001625 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001626
1627 return 0;
1628}
1629
1630/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001631 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1632 *
1633 * @adev: amdgpu_device pointer
1634 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001635 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001636 *
1637 * Fill in the page table entries for @bo_va.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001638 *
1639 * Returns:
1640 * 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641 */
1642int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1643 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001644 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645{
Christian Königec681542017-08-01 10:51:43 +02001646 struct amdgpu_bo *bo = bo_va->base.bo;
1647 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001648 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001649 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001650 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001651 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001652 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001653 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001654 int r;
1655
Christian Königec681542017-08-01 10:51:43 +02001656 if (clear || !bo_va->base.bo) {
Christian König99e124f2016-08-16 14:43:17 +02001657 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001658 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001659 exclusive = NULL;
1660 } else {
Christian König8358dce2016-03-30 10:50:25 +02001661 struct ttm_dma_tt *ttm;
1662
Christian Königec681542017-08-01 10:51:43 +02001663 mem = &bo_va->base.bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001664 nodes = mem->mm_node;
1665 if (mem->mem_type == TTM_PL_TT) {
Christian Königec681542017-08-01 10:51:43 +02001666 ttm = container_of(bo_va->base.bo->tbo.ttm,
1667 struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001668 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001669 }
Christian Königec681542017-08-01 10:51:43 +02001670 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671 }
1672
Christian König457e0fe2017-08-22 12:50:46 +02001673 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001674 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001675 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001676 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001677
Christian König4e55eb32017-09-11 16:54:59 +02001678 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1679 last_update = &vm->last_update;
1680 else
1681 last_update = &bo_va->last_pt_update;
1682
Christian König3d7d4d32017-08-23 16:13:33 +02001683 if (!clear && bo_va->base.moved) {
1684 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001685 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001686
Christian Königcb7b6ec2017-08-15 17:08:12 +02001687 } else if (bo_va->cleared != clear) {
1688 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001689 }
Christian König7fc11952015-07-30 11:53:42 +02001690
1691 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001692 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001693 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001694 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001695 if (r)
1696 return r;
1697 }
1698
Christian König68c62302017-07-11 17:23:29 +02001699 if (vm->use_cpu_for_update) {
1700 /* Flush HDP */
1701 mb();
Christian König69882562018-01-19 14:17:40 +01001702 amdgpu_asic_flush_hdp(adev, NULL);
Christian König68c62302017-07-11 17:23:29 +02001703 }
1704
Christian Königaf4c0f62018-04-19 10:56:02 +02001705 spin_lock(&vm->moved_lock);
Junwei Zhangbb475832018-04-19 13:17:26 +08001706 list_del_init(&bo_va->base.vm_status);
Christian Königaf4c0f62018-04-19 10:56:02 +02001707 spin_unlock(&vm->moved_lock);
Christian König36188362018-03-19 11:49:14 +01001708
Junwei Zhangbb475832018-04-19 13:17:26 +08001709 /* If the BO is not in its preferred location add it back to
1710 * the evicted list so that it gets validated again on the
1711 * next command submission.
1712 */
Christian König806f0432018-04-19 15:01:12 +02001713 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1714 uint32_t mem_type = bo->tbo.mem.mem_type;
1715
1716 if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type)))
1717 list_add_tail(&bo_va->base.vm_status, &vm->evicted);
1718 else
1719 list_add(&bo_va->base.vm_status, &vm->idle);
1720 }
Christian Königcb7b6ec2017-08-15 17:08:12 +02001721
1722 list_splice_init(&bo_va->invalids, &bo_va->valids);
1723 bo_va->cleared = clear;
1724
1725 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1726 list_for_each_entry(mapping, &bo_va->valids, list)
1727 trace_amdgpu_vm_bo_mapping(mapping);
1728 }
1729
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730 return 0;
1731}
1732
1733/**
Christian König284710f2017-01-30 11:09:31 +01001734 * amdgpu_vm_update_prt_state - update the global PRT state
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001735 *
1736 * @adev: amdgpu_device pointer
Christian König284710f2017-01-30 11:09:31 +01001737 */
1738static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1739{
1740 unsigned long flags;
1741 bool enable;
1742
1743 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001744 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König132f34e2018-01-12 15:26:08 +01001745 adev->gmc.gmc_funcs->set_prt(adev, enable);
Christian König284710f2017-01-30 11:09:31 +01001746 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1747}
1748
1749/**
Christian König4388fc22017-03-13 10:13:36 +01001750 * amdgpu_vm_prt_get - add a PRT user
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001751 *
1752 * @adev: amdgpu_device pointer
Christian König451bc8e2017-02-14 16:02:52 +01001753 */
1754static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1755{
Christian König132f34e2018-01-12 15:26:08 +01001756 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001757 return;
1758
Christian König451bc8e2017-02-14 16:02:52 +01001759 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1760 amdgpu_vm_update_prt_state(adev);
1761}
1762
1763/**
Christian König0b15f2f2017-02-14 15:47:03 +01001764 * amdgpu_vm_prt_put - drop a PRT user
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001765 *
1766 * @adev: amdgpu_device pointer
Christian König0b15f2f2017-02-14 15:47:03 +01001767 */
1768static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1769{
Christian König451bc8e2017-02-14 16:02:52 +01001770 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001771 amdgpu_vm_update_prt_state(adev);
1772}
1773
1774/**
Christian König451bc8e2017-02-14 16:02:52 +01001775 * amdgpu_vm_prt_cb - callback for updating the PRT status
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001776 *
1777 * @fence: fence for the callback
Christian König284710f2017-01-30 11:09:31 +01001778 */
1779static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1780{
1781 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1782
Christian König0b15f2f2017-02-14 15:47:03 +01001783 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001784 kfree(cb);
1785}
1786
1787/**
Christian König451bc8e2017-02-14 16:02:52 +01001788 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001789 *
1790 * @adev: amdgpu_device pointer
1791 * @fence: fence for the callback
Christian König451bc8e2017-02-14 16:02:52 +01001792 */
1793static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1794 struct dma_fence *fence)
1795{
Christian König4388fc22017-03-13 10:13:36 +01001796 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001797
Christian König132f34e2018-01-12 15:26:08 +01001798 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001799 return;
1800
1801 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001802 if (!cb) {
1803 /* Last resort when we are OOM */
1804 if (fence)
1805 dma_fence_wait(fence, false);
1806
Dan Carpenter486a68f2017-04-03 21:41:39 +03001807 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001808 } else {
1809 cb->adev = adev;
1810 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1811 amdgpu_vm_prt_cb))
1812 amdgpu_vm_prt_cb(fence, &cb->cb);
1813 }
1814}
1815
1816/**
Christian König284710f2017-01-30 11:09:31 +01001817 * amdgpu_vm_free_mapping - free a mapping
1818 *
1819 * @adev: amdgpu_device pointer
1820 * @vm: requested vm
1821 * @mapping: mapping to be freed
1822 * @fence: fence of the unmap operation
1823 *
1824 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1825 */
1826static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1827 struct amdgpu_vm *vm,
1828 struct amdgpu_bo_va_mapping *mapping,
1829 struct dma_fence *fence)
1830{
Christian König451bc8e2017-02-14 16:02:52 +01001831 if (mapping->flags & AMDGPU_PTE_PRT)
1832 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001833 kfree(mapping);
1834}
1835
1836/**
Christian König451bc8e2017-02-14 16:02:52 +01001837 * amdgpu_vm_prt_fini - finish all prt mappings
1838 *
1839 * @adev: amdgpu_device pointer
1840 * @vm: requested vm
1841 *
1842 * Register a cleanup callback to disable PRT support after VM dies.
1843 */
1844static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1845{
Christian König3f3333f2017-08-03 14:02:13 +02001846 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001847 struct dma_fence *excl, **shared;
1848 unsigned i, shared_count;
1849 int r;
1850
1851 r = reservation_object_get_fences_rcu(resv, &excl,
1852 &shared_count, &shared);
1853 if (r) {
1854 /* Not enough memory to grab the fence list, as last resort
1855 * block for all the fences to complete.
1856 */
1857 reservation_object_wait_timeout_rcu(resv, true, false,
1858 MAX_SCHEDULE_TIMEOUT);
1859 return;
1860 }
1861
1862 /* Add a callback for each fence in the reservation object */
1863 amdgpu_vm_prt_get(adev);
1864 amdgpu_vm_add_prt_cb(adev, excl);
1865
1866 for (i = 0; i < shared_count; ++i) {
1867 amdgpu_vm_prt_get(adev);
1868 amdgpu_vm_add_prt_cb(adev, shared[i]);
1869 }
1870
1871 kfree(shared);
1872}
1873
1874/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1876 *
1877 * @adev: amdgpu_device pointer
1878 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001879 * @fence: optional resulting fence (unchanged if no work needed to be done
1880 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001881 *
1882 * Make sure all freed BOs are cleared in the PT.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001883 * PTs have to be reserved and mutex must be locked!
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001884 *
1885 * Returns:
1886 * 0 for success.
1887 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001888 */
1889int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001890 struct amdgpu_vm *vm,
1891 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001892{
1893 struct amdgpu_bo_va_mapping *mapping;
Christian König45843122018-01-25 18:36:15 +01001894 uint64_t init_pte_value = 0;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001895 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896 int r;
1897
1898 while (!list_empty(&vm->freed)) {
1899 mapping = list_first_entry(&vm->freed,
1900 struct amdgpu_bo_va_mapping, list);
1901 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001902
Christian König45843122018-01-25 18:36:15 +01001903 if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
Yong Zhao6d16dac2017-08-31 15:55:00 -04001904 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001905
Christian König570144c2017-08-30 15:38:45 +02001906 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001907 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001908 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001909 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001910 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001911 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001912 return r;
Christian König284710f2017-01-30 11:09:31 +01001913 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001914 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001915
1916 if (fence && f) {
1917 dma_fence_put(*fence);
1918 *fence = f;
1919 } else {
1920 dma_fence_put(f);
1921 }
1922
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923 return 0;
1924
1925}
1926
1927/**
Christian König73fb16e2017-08-16 11:13:48 +02001928 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929 *
1930 * @adev: amdgpu_device pointer
1931 * @vm: requested vm
1932 *
Christian König73fb16e2017-08-16 11:13:48 +02001933 * Make sure all BOs which are moved are updated in the PTs.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001934 *
1935 * Returns:
1936 * 0 for success.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001937 *
Christian König73fb16e2017-08-16 11:13:48 +02001938 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939 */
Christian König73fb16e2017-08-16 11:13:48 +02001940int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02001941 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001942{
Christian König789f3312018-04-19 11:08:24 +02001943 struct amdgpu_bo_va *bo_va, *tmp;
1944 struct list_head moved;
Christian König73fb16e2017-08-16 11:13:48 +02001945 bool clear;
Christian König789f3312018-04-19 11:08:24 +02001946 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001947
Christian König789f3312018-04-19 11:08:24 +02001948 INIT_LIST_HEAD(&moved);
Christian Königaf4c0f62018-04-19 10:56:02 +02001949 spin_lock(&vm->moved_lock);
Christian König789f3312018-04-19 11:08:24 +02001950 list_splice_init(&vm->moved, &moved);
1951 spin_unlock(&vm->moved_lock);
Christian König4e55eb32017-09-11 16:54:59 +02001952
Christian König789f3312018-04-19 11:08:24 +02001953 list_for_each_entry_safe(bo_va, tmp, &moved, base.vm_status) {
1954 struct reservation_object *resv = bo_va->base.bo->tbo.resv;
Christian Königec363e02017-09-01 20:34:27 +02001955
Christian König73fb16e2017-08-16 11:13:48 +02001956 /* Per VM BOs never need to bo cleared in the page tables */
Christian Königec363e02017-09-01 20:34:27 +02001957 if (resv == vm->root.base.bo->tbo.resv)
1958 clear = false;
1959 /* Try to reserve the BO to avoid clearing its ptes */
Christian König9b8cad22018-01-03 13:36:22 +01001960 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
Christian Königec363e02017-09-01 20:34:27 +02001961 clear = false;
1962 /* Somebody else is using the BO right now */
1963 else
1964 clear = true;
Christian König73fb16e2017-08-16 11:13:48 +02001965
1966 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Christian König789f3312018-04-19 11:08:24 +02001967 if (r) {
1968 spin_lock(&vm->moved_lock);
1969 list_splice(&moved, &vm->moved);
1970 spin_unlock(&vm->moved_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001971 return r;
Christian König789f3312018-04-19 11:08:24 +02001972 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001973
Christian Königec363e02017-09-01 20:34:27 +02001974 if (!clear && resv != vm->root.base.bo->tbo.resv)
1975 reservation_object_unlock(resv);
1976
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001977 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001978
Christian König789f3312018-04-19 11:08:24 +02001979 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001980}
1981
1982/**
1983 * amdgpu_vm_bo_add - add a bo to a specific vm
1984 *
1985 * @adev: amdgpu_device pointer
1986 * @vm: requested vm
1987 * @bo: amdgpu buffer object
1988 *
Christian König8843dbb2016-01-26 12:17:11 +01001989 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001990 * Add @bo to the list of bos associated with the vm
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001991 *
1992 * Returns:
1993 * Newly added bo_va or NULL for failure
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001994 *
1995 * Object has to be reserved!
1996 */
1997struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1998 struct amdgpu_vm *vm,
1999 struct amdgpu_bo *bo)
2000{
2001 struct amdgpu_bo_va *bo_va;
2002
2003 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2004 if (bo_va == NULL) {
2005 return NULL;
2006 }
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002007 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
Christian Königec681542017-08-01 10:51:43 +02002008
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002010 INIT_LIST_HEAD(&bo_va->valids);
2011 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002012
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 return bo_va;
2014}
2015
Christian König73fb16e2017-08-16 11:13:48 +02002016
2017/**
2018 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2019 *
2020 * @adev: amdgpu_device pointer
2021 * @bo_va: bo_va to store the address
2022 * @mapping: the mapping to insert
2023 *
2024 * Insert a new mapping into all structures.
2025 */
2026static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2027 struct amdgpu_bo_va *bo_va,
2028 struct amdgpu_bo_va_mapping *mapping)
2029{
2030 struct amdgpu_vm *vm = bo_va->base.vm;
2031 struct amdgpu_bo *bo = bo_va->base.bo;
2032
Christian Königaebc5e62017-09-06 16:55:16 +02002033 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002034 list_add(&mapping->list, &bo_va->invalids);
2035 amdgpu_vm_it_insert(mapping, &vm->va);
2036
2037 if (mapping->flags & AMDGPU_PTE_PRT)
2038 amdgpu_vm_prt_get(adev);
2039
Christian König862b8c52018-04-19 14:22:56 +02002040 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
2041 !bo_va->base.moved) {
Christian Königaf4c0f62018-04-19 10:56:02 +02002042 spin_lock(&vm->moved_lock);
Christian König862b8c52018-04-19 14:22:56 +02002043 list_move(&bo_va->base.vm_status, &vm->moved);
Christian Königaf4c0f62018-04-19 10:56:02 +02002044 spin_unlock(&vm->moved_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002045 }
2046 trace_amdgpu_vm_bo_map(bo_va, mapping);
2047}
2048
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049/**
2050 * amdgpu_vm_bo_map - map bo inside a vm
2051 *
2052 * @adev: amdgpu_device pointer
2053 * @bo_va: bo_va to store the address
2054 * @saddr: where to map the BO
2055 * @offset: requested offset in the BO
2056 * @flags: attributes of pages (read/write/valid/etc.)
2057 *
2058 * Add a mapping of the BO at the specefied addr into the VM.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002059 *
2060 * Returns:
2061 * 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002062 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002063 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002064 */
2065int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2066 struct amdgpu_bo_va *bo_va,
2067 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002068 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069{
Christian Königa9f87f62017-03-30 14:03:59 +02002070 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002071 struct amdgpu_bo *bo = bo_va->base.bo;
2072 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002074
Christian König0be52de2015-05-18 14:37:27 +02002075 /* validate the parameters */
2076 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002077 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002078 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002081 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002082 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002083 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002084 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002086 saddr /= AMDGPU_GPU_PAGE_SIZE;
2087 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2088
Christian Königa9f87f62017-03-30 14:03:59 +02002089 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2090 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091 /* bo and tmp overlap, invalid addr */
2092 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002093 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002094 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002095 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002096 }
2097
2098 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002099 if (!mapping)
2100 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002101
Christian Königa9f87f62017-03-30 14:03:59 +02002102 mapping->start = saddr;
2103 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002104 mapping->offset = offset;
2105 mapping->flags = flags;
2106
Christian König73fb16e2017-08-16 11:13:48 +02002107 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002108
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002109 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002110}
2111
2112/**
Christian König80f95c52017-03-13 10:13:39 +01002113 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2114 *
2115 * @adev: amdgpu_device pointer
2116 * @bo_va: bo_va to store the address
2117 * @saddr: where to map the BO
2118 * @offset: requested offset in the BO
2119 * @flags: attributes of pages (read/write/valid/etc.)
2120 *
2121 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2122 * mappings as we do so.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002123 *
2124 * Returns:
2125 * 0 for success, error for failure.
Christian König80f95c52017-03-13 10:13:39 +01002126 *
2127 * Object has to be reserved and unreserved outside!
2128 */
2129int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2130 struct amdgpu_bo_va *bo_va,
2131 uint64_t saddr, uint64_t offset,
2132 uint64_t size, uint64_t flags)
2133{
2134 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002135 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002136 uint64_t eaddr;
2137 int r;
2138
2139 /* validate the parameters */
2140 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2141 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2142 return -EINVAL;
2143
2144 /* make sure object fit at this offset */
2145 eaddr = saddr + size - 1;
2146 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002147 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002148 return -EINVAL;
2149
2150 /* Allocate all the needed memory */
2151 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2152 if (!mapping)
2153 return -ENOMEM;
2154
Christian Königec681542017-08-01 10:51:43 +02002155 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002156 if (r) {
2157 kfree(mapping);
2158 return r;
2159 }
2160
2161 saddr /= AMDGPU_GPU_PAGE_SIZE;
2162 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2163
Christian Königa9f87f62017-03-30 14:03:59 +02002164 mapping->start = saddr;
2165 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002166 mapping->offset = offset;
2167 mapping->flags = flags;
2168
Christian König73fb16e2017-08-16 11:13:48 +02002169 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002170
2171 return 0;
2172}
2173
2174/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2176 *
2177 * @adev: amdgpu_device pointer
2178 * @bo_va: bo_va to remove the address from
2179 * @saddr: where to the BO is mapped
2180 *
2181 * Remove a mapping of the BO at the specefied addr from the VM.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002182 *
2183 * Returns:
2184 * 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002185 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002186 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 */
2188int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2189 struct amdgpu_bo_va *bo_va,
2190 uint64_t saddr)
2191{
2192 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002193 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002194 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002195
Christian König6c7fc502015-06-05 20:56:17 +02002196 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002197
Christian König7fc11952015-07-30 11:53:42 +02002198 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002199 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002200 break;
2201 }
2202
Christian König7fc11952015-07-30 11:53:42 +02002203 if (&mapping->list == &bo_va->valids) {
2204 valid = false;
2205
2206 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002207 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002208 break;
2209 }
2210
Christian König32b41ac2016-03-08 18:03:27 +01002211 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002212 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213 }
Christian König32b41ac2016-03-08 18:03:27 +01002214
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002215 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002216 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002217 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002218 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219
Christian Könige17841b2016-03-08 17:52:01 +01002220 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002221 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002222 else
Christian König284710f2017-01-30 11:09:31 +01002223 amdgpu_vm_free_mapping(adev, vm, mapping,
2224 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002225
2226 return 0;
2227}
2228
2229/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002230 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2231 *
2232 * @adev: amdgpu_device pointer
2233 * @vm: VM structure to use
2234 * @saddr: start of the range
2235 * @size: size of the range
2236 *
2237 * Remove all mappings in a range, split them as appropriate.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002238 *
2239 * Returns:
2240 * 0 for success, error for failure.
Christian Königdc54d3d2017-03-13 10:13:38 +01002241 */
2242int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2243 struct amdgpu_vm *vm,
2244 uint64_t saddr, uint64_t size)
2245{
2246 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002247 LIST_HEAD(removed);
2248 uint64_t eaddr;
2249
2250 eaddr = saddr + size - 1;
2251 saddr /= AMDGPU_GPU_PAGE_SIZE;
2252 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2253
2254 /* Allocate all the needed memory */
2255 before = kzalloc(sizeof(*before), GFP_KERNEL);
2256 if (!before)
2257 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002258 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002259
2260 after = kzalloc(sizeof(*after), GFP_KERNEL);
2261 if (!after) {
2262 kfree(before);
2263 return -ENOMEM;
2264 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002265 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002266
2267 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002268 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2269 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002270 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002271 if (tmp->start < saddr) {
2272 before->start = tmp->start;
2273 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002274 before->offset = tmp->offset;
2275 before->flags = tmp->flags;
Junwei Zhang387f49e2018-06-05 17:31:51 +08002276 before->bo_va = tmp->bo_va;
2277 list_add(&before->list, &tmp->bo_va->invalids);
Christian Königdc54d3d2017-03-13 10:13:38 +01002278 }
2279
2280 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002281 if (tmp->last > eaddr) {
2282 after->start = eaddr + 1;
2283 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002284 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002285 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002286 after->flags = tmp->flags;
Junwei Zhang387f49e2018-06-05 17:31:51 +08002287 after->bo_va = tmp->bo_va;
2288 list_add(&after->list, &tmp->bo_va->invalids);
Christian Königdc54d3d2017-03-13 10:13:38 +01002289 }
2290
2291 list_del(&tmp->list);
2292 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002293
2294 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002295 }
2296
2297 /* And free them up */
2298 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002299 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002300 list_del(&tmp->list);
2301
Christian Königa9f87f62017-03-30 14:03:59 +02002302 if (tmp->start < saddr)
2303 tmp->start = saddr;
2304 if (tmp->last > eaddr)
2305 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002306
Christian Königaebc5e62017-09-06 16:55:16 +02002307 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002308 list_add(&tmp->list, &vm->freed);
2309 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2310 }
2311
Junwei Zhang27f6d612017-03-16 16:09:24 +08002312 /* Insert partial mapping before the range */
2313 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002314 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002315 if (before->flags & AMDGPU_PTE_PRT)
2316 amdgpu_vm_prt_get(adev);
2317 } else {
2318 kfree(before);
2319 }
2320
2321 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002322 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002323 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002324 if (after->flags & AMDGPU_PTE_PRT)
2325 amdgpu_vm_prt_get(adev);
2326 } else {
2327 kfree(after);
2328 }
2329
2330 return 0;
2331}
2332
2333/**
Christian Königaebc5e62017-09-06 16:55:16 +02002334 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2335 *
2336 * @vm: the requested VM
2337 *
2338 * Find a mapping by it's address.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002339 *
2340 * Returns:
2341 * The amdgpu_bo_va_mapping matching for addr or NULL
2342 *
Christian Königaebc5e62017-09-06 16:55:16 +02002343 */
2344struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2345 uint64_t addr)
2346{
2347 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2348}
2349
2350/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002351 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2352 *
2353 * @adev: amdgpu_device pointer
2354 * @bo_va: requested bo_va
2355 *
Christian König8843dbb2016-01-26 12:17:11 +01002356 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002357 *
2358 * Object have to be reserved!
2359 */
2360void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2361 struct amdgpu_bo_va *bo_va)
2362{
2363 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002364 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002365
Christian Königec681542017-08-01 10:51:43 +02002366 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002367
Christian Königaf4c0f62018-04-19 10:56:02 +02002368 spin_lock(&vm->moved_lock);
Christian Königec681542017-08-01 10:51:43 +02002369 list_del(&bo_va->base.vm_status);
Christian Königaf4c0f62018-04-19 10:56:02 +02002370 spin_unlock(&vm->moved_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002371
Christian König7fc11952015-07-30 11:53:42 +02002372 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002373 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002374 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002375 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002376 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002377 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002378 }
Christian König7fc11952015-07-30 11:53:42 +02002379 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2380 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002381 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002382 amdgpu_vm_free_mapping(adev, vm, mapping,
2383 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002384 }
Christian König32b41ac2016-03-08 18:03:27 +01002385
Chris Wilsonf54d1862016-10-25 13:00:45 +01002386 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002387 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002388}
2389
2390/**
2391 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2392 *
2393 * @adev: amdgpu_device pointer
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002394 * @bo: amdgpu buffer object
2395 *
Christian König8843dbb2016-01-26 12:17:11 +01002396 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002397 */
2398void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002399 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002400{
Christian Königec681542017-08-01 10:51:43 +02002401 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402
Chunming Zhou4bebcce2018-04-24 13:54:10 +08002403 /* shadow bo doesn't have bo base, its validation needs its parent */
2404 if (bo->parent && bo->parent->shadow == bo)
2405 bo = bo->parent;
2406
Christian Königec681542017-08-01 10:51:43 +02002407 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002408 struct amdgpu_vm *vm = bo_base->vm;
Christian König862b8c52018-04-19 14:22:56 +02002409 bool was_moved = bo_base->moved;
Christian König3f3333f2017-08-03 14:02:13 +02002410
Christian König3d7d4d32017-08-23 16:13:33 +02002411 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002412 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
Christian König73fb16e2017-08-16 11:13:48 +02002413 if (bo->tbo.type == ttm_bo_type_kernel)
2414 list_move(&bo_base->vm_status, &vm->evicted);
2415 else
2416 list_move_tail(&bo_base->vm_status,
2417 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002418 continue;
2419 }
2420
Christian König862b8c52018-04-19 14:22:56 +02002421 if (was_moved)
Christian König3f3333f2017-08-03 14:02:13 +02002422 continue;
2423
Christian König862b8c52018-04-19 14:22:56 +02002424 if (bo->tbo.type == ttm_bo_type_kernel) {
2425 list_move(&bo_base->vm_status, &vm->relocated);
2426 } else {
2427 spin_lock(&bo_base->vm->moved_lock);
2428 list_move(&bo_base->vm_status, &vm->moved);
2429 spin_unlock(&bo_base->vm->moved_lock);
2430 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002431 }
2432}
2433
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002434/**
2435 * amdgpu_vm_get_block_size - calculate VM page table size as power of two
2436 *
2437 * @vm_size: VM size
2438 *
2439 * Returns:
2440 * VM page table as power of two
2441 */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002442static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2443{
2444 /* Total bits covered by PD + PTs */
2445 unsigned bits = ilog2(vm_size) + 18;
2446
2447 /* Make sure the PD is 4K in size up to 8GB address space.
2448 Above that split equal between PD and PTs */
2449 if (vm_size <= 8)
2450 return (bits - 9);
2451 else
2452 return ((bits + 3) / 2);
2453}
2454
2455/**
Roger Hed07f14b2017-08-15 16:05:59 +08002456 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002457 *
2458 * @adev: amdgpu_device pointer
2459 * @vm_size: the default vm size if it's set auto
2460 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002461void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +01002462 uint32_t fragment_size_default, unsigned max_level,
2463 unsigned max_bits)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002464{
Christian König36539dc2017-11-23 11:16:05 +01002465 uint64_t tmp;
2466
2467 /* adjust vm size first */
Christian Königf3368122017-11-23 12:57:18 +01002468 if (amdgpu_vm_size != -1) {
2469 unsigned max_size = 1 << (max_bits - 30);
2470
Christian Königfdd5faa2017-11-04 16:51:44 +01002471 vm_size = amdgpu_vm_size;
Christian Königf3368122017-11-23 12:57:18 +01002472 if (vm_size > max_size) {
2473 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2474 amdgpu_vm_size, max_size);
2475 vm_size = max_size;
2476 }
2477 }
Christian Königfdd5faa2017-11-04 16:51:44 +01002478
2479 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Christian König36539dc2017-11-23 11:16:05 +01002480
2481 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
Christian König97489122017-11-27 16:22:05 +01002482 if (amdgpu_vm_block_size != -1)
2483 tmp >>= amdgpu_vm_block_size - 9;
Christian König36539dc2017-11-23 11:16:05 +01002484 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2485 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
Chunming Zhou196f7482017-12-13 14:22:54 +08002486 switch (adev->vm_manager.num_level) {
2487 case 3:
2488 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2489 break;
2490 case 2:
2491 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2492 break;
2493 case 1:
2494 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2495 break;
2496 default:
2497 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2498 }
Christian Königb38f41e2017-11-22 17:00:35 +01002499 /* block size depends on vm size and hw setup*/
Christian König97489122017-11-27 16:22:05 +01002500 if (amdgpu_vm_block_size != -1)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002501 adev->vm_manager.block_size =
Christian König97489122017-11-27 16:22:05 +01002502 min((unsigned)amdgpu_vm_block_size, max_bits
2503 - AMDGPU_GPU_PAGE_SHIFT
2504 - 9 * adev->vm_manager.num_level);
2505 else if (adev->vm_manager.num_level > 1)
2506 adev->vm_manager.block_size = 9;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002507 else
Christian König97489122017-11-27 16:22:05 +01002508 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002509
Christian Königb38f41e2017-11-22 17:00:35 +01002510 if (amdgpu_vm_fragment_size == -1)
2511 adev->vm_manager.fragment_size = fragment_size_default;
2512 else
2513 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
Roger Hed07f14b2017-08-15 16:05:59 +08002514
Christian König36539dc2017-11-23 11:16:05 +01002515 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2516 vm_size, adev->vm_manager.num_level + 1,
2517 adev->vm_manager.block_size,
Christian Königfdd5faa2017-11-04 16:51:44 +01002518 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002519}
2520
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002521/**
2522 * amdgpu_vm_init - initialize a vm instance
2523 *
2524 * @adev: amdgpu_device pointer
2525 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002526 * @vm_context: Indicates if it GFX or Compute context
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002527 *
Christian König8843dbb2016-01-26 12:17:11 +01002528 * Init @vm fields.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002529 *
2530 * Returns:
2531 * 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002532 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002533int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002534 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002535{
Chunming Zhou3216c6b2018-04-16 18:27:50 +08002536 struct amdgpu_bo_param bp;
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002537 struct amdgpu_bo *root;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002538 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002539 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002540 unsigned ring_instance;
2541 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002542 struct drm_sched_rq *rq;
Christian Königd3aab672018-01-24 14:57:02 +01002543 unsigned long size;
Christian König13307f72018-01-24 17:19:04 +01002544 uint64_t flags;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002545 int r, i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002546
Davidlohr Buesof808c132017-09-08 16:15:08 -07002547 vm->va = RB_ROOT_CACHED;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002548 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2549 vm->reserved_vmid[i] = NULL;
Christian König3f3333f2017-08-03 14:02:13 +02002550 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002551 INIT_LIST_HEAD(&vm->relocated);
Christian Königaf4c0f62018-04-19 10:56:02 +02002552 spin_lock_init(&vm->moved_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002553 INIT_LIST_HEAD(&vm->moved);
Christian König806f0432018-04-19 15:01:12 +02002554 INIT_LIST_HEAD(&vm->idle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002555 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002556
Christian König2bd9ccf2016-02-01 12:53:58 +01002557 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002558
2559 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2560 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2561 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002562 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2563 r = drm_sched_entity_init(&ring->sched, &vm->entity,
Nayan Deshmukh8344c532018-03-29 22:36:32 +05302564 rq, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002565 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002566 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002567
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002568 vm->pte_support_ats = false;
2569
2570 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002571 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2572 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002573
Christian König13307f72018-01-24 17:19:04 +01002574 if (adev->asic_type == CHIP_RAVEN)
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002575 vm->pte_support_ats = true;
Christian König13307f72018-01-24 17:19:04 +01002576 } else {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002577 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2578 AMDGPU_VM_USE_CPU_FOR_GFX);
Christian König13307f72018-01-24 17:19:04 +01002579 }
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002580 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2581 vm->use_cpu_for_update ? "CPU" : "SDMA");
2582 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2583 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002584 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002585
Christian König13307f72018-01-24 17:19:04 +01002586 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002587 if (vm->use_cpu_for_update)
2588 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2589 else
Felix Kuehling810955b2018-03-23 15:30:35 -04002590 flags |= AMDGPU_GEM_CREATE_SHADOW;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002591
Christian Königd3aab672018-01-24 14:57:02 +01002592 size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
Chunming Zhou3216c6b2018-04-16 18:27:50 +08002593 memset(&bp, 0, sizeof(bp));
2594 bp.size = size;
2595 bp.byte_align = align;
2596 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
2597 bp.flags = flags;
2598 bp.type = ttm_bo_type_kernel;
2599 bp.resv = NULL;
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002600 r = amdgpu_bo_create(adev, &bp, &root);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002601 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002602 goto error_free_sched_entity;
2603
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002604 r = amdgpu_bo_reserve(root, true);
Christian Königd3aab672018-01-24 14:57:02 +01002605 if (r)
2606 goto error_free_root;
2607
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002608 r = amdgpu_vm_clear_bo(adev, vm, root,
Christian König45843122018-01-25 18:36:15 +01002609 adev->vm_manager.root_level,
2610 vm->pte_support_ats);
Christian König13307f72018-01-24 17:19:04 +01002611 if (r)
2612 goto error_unreserve;
2613
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002614 amdgpu_vm_bo_base_init(&vm->root.base, vm, root);
Christian Königd3aab672018-01-24 14:57:02 +01002615 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König0a096fb2017-07-12 10:01:48 +02002616
Felix Kuehling02208442017-08-25 20:40:26 -04002617 if (pasid) {
2618 unsigned long flags;
2619
2620 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2621 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2622 GFP_ATOMIC);
2623 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2624 if (r < 0)
2625 goto error_free_root;
2626
2627 vm->pasid = pasid;
2628 }
2629
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002630 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002631 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002632
2633 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002634
Christian König13307f72018-01-24 17:19:04 +01002635error_unreserve:
2636 amdgpu_bo_unreserve(vm->root.base.bo);
2637
Christian König67003a12016-10-12 14:46:26 +02002638error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002639 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2640 amdgpu_bo_unref(&vm->root.base.bo);
2641 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002642
2643error_free_sched_entity:
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002644 drm_sched_entity_fini(&ring->sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002645
2646 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002647}
2648
2649/**
Felix Kuehlingb236fa12018-03-15 17:27:42 -04002650 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2651 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002652 * @adev: amdgpu_device pointer
2653 * @vm: requested vm
2654 *
Felix Kuehlingb236fa12018-03-15 17:27:42 -04002655 * This only works on GFX VMs that don't have any BOs added and no
2656 * page tables allocated yet.
2657 *
2658 * Changes the following VM parameters:
2659 * - use_cpu_for_update
2660 * - pte_supports_ats
2661 * - pasid (old PASID is released, because compute manages its own PASIDs)
2662 *
2663 * Reinitializes the page directory to reflect the changed ATS
2664 * setting. May leave behind an unused shadow BO for the page
2665 * directory when switching from SDMA updates to CPU updates.
2666 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002667 * Returns:
2668 * 0 for success, -errno for errors.
Felix Kuehlingb236fa12018-03-15 17:27:42 -04002669 */
2670int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2671{
2672 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2673 int r;
2674
2675 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2676 if (r)
2677 return r;
2678
2679 /* Sanity checks */
2680 if (!RB_EMPTY_ROOT(&vm->va.rb_root) || vm->root.entries) {
2681 r = -EINVAL;
2682 goto error;
2683 }
2684
2685 /* Check if PD needs to be reinitialized and do it before
2686 * changing any other state, in case it fails.
2687 */
2688 if (pte_support_ats != vm->pte_support_ats) {
2689 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
2690 adev->vm_manager.root_level,
2691 pte_support_ats);
2692 if (r)
2693 goto error;
2694 }
2695
2696 /* Update VM state */
2697 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2698 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2699 vm->pte_support_ats = pte_support_ats;
2700 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2701 vm->use_cpu_for_update ? "CPU" : "SDMA");
2702 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2703 "CPU update of VM recommended only for large BAR system\n");
2704
2705 if (vm->pasid) {
2706 unsigned long flags;
2707
2708 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2709 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2710 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2711
2712 vm->pasid = 0;
2713 }
2714
2715error:
2716 amdgpu_bo_unreserve(vm->root.base.bo);
2717 return r;
2718}
2719
2720/**
Christian Königf566ceb2016-10-27 20:04:38 +02002721 * amdgpu_vm_free_levels - free PD/PT levels
2722 *
Christian König8f19cd72017-11-30 15:28:03 +01002723 * @adev: amdgpu device structure
2724 * @parent: PD/PT starting level to free
2725 * @level: level of parent structure
Christian Königf566ceb2016-10-27 20:04:38 +02002726 *
2727 * Free the page directory or page table level and all sub levels.
2728 */
Christian König8f19cd72017-11-30 15:28:03 +01002729static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2730 struct amdgpu_vm_pt *parent,
2731 unsigned level)
Christian Königf566ceb2016-10-27 20:04:38 +02002732{
Christian König8f19cd72017-11-30 15:28:03 +01002733 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +02002734
Christian König8f19cd72017-11-30 15:28:03 +01002735 if (parent->base.bo) {
2736 list_del(&parent->base.bo_list);
2737 list_del(&parent->base.vm_status);
2738 amdgpu_bo_unref(&parent->base.bo->shadow);
2739 amdgpu_bo_unref(&parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002740 }
2741
Christian König8f19cd72017-11-30 15:28:03 +01002742 if (parent->entries)
2743 for (i = 0; i < num_entries; i++)
2744 amdgpu_vm_free_levels(adev, &parent->entries[i],
2745 level + 1);
Christian Königf566ceb2016-10-27 20:04:38 +02002746
Christian König8f19cd72017-11-30 15:28:03 +01002747 kvfree(parent->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002748}
2749
2750/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002751 * amdgpu_vm_fini - tear down a vm instance
2752 *
2753 * @adev: amdgpu_device pointer
2754 * @vm: requested vm
2755 *
Christian König8843dbb2016-01-26 12:17:11 +01002756 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002757 * Unbind the VM and remove all bos from the vm bo list
2758 */
2759void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2760{
2761 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König132f34e2018-01-12 15:26:08 +01002762 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002763 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002764 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002765 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002766
Felix Kuehlingede0dd82018-03-15 17:27:43 -04002767 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2768
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002769 /* Clear pending page faults from IH when the VM is destroyed */
2770 while (kfifo_get(&vm->faults, &fault))
2771 amdgpu_ih_clear_fault(adev, fault);
2772
Felix Kuehling02208442017-08-25 20:40:26 -04002773 if (vm->pasid) {
2774 unsigned long flags;
2775
2776 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2777 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2778 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2779 }
2780
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002781 drm_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002782
Davidlohr Buesof808c132017-09-08 16:15:08 -07002783 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002784 dev_err(adev->dev, "still active bo inside vm\n");
2785 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002786 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2787 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002788 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002789 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002790 kfree(mapping);
2791 }
2792 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002793 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002794 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002795 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002796 }
Christian König284710f2017-01-30 11:09:31 +01002797
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002798 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002799 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002800 }
2801
Christian König2642cf12017-10-13 17:24:31 +02002802 root = amdgpu_bo_ref(vm->root.base.bo);
2803 r = amdgpu_bo_reserve(root, true);
2804 if (r) {
2805 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2806 } else {
Chunming Zhou196f7482017-12-13 14:22:54 +08002807 amdgpu_vm_free_levels(adev, &vm->root,
2808 adev->vm_manager.root_level);
Christian König2642cf12017-10-13 17:24:31 +02002809 amdgpu_bo_unreserve(root);
2810 }
2811 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002812 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002813 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
Christian König620f7742017-12-18 16:53:03 +01002814 amdgpu_vmid_free_reserved(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002815}
Christian Königea89f8c2015-11-15 20:52:06 +01002816
2817/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002818 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2819 *
2820 * @adev: amdgpu_device pointer
2821 * @pasid: PASID do identify the VM
2822 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002823 * This function is expected to be called in interrupt context.
2824 *
2825 * Returns:
2826 * True if there was fault credit, false otherwise
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002827 */
2828bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2829 unsigned int pasid)
2830{
2831 struct amdgpu_vm *vm;
2832
2833 spin_lock(&adev->vm_manager.pasid_lock);
2834 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
Christian Königd9589392018-01-09 19:18:59 +01002835 if (!vm) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002836 /* VM not found, can't track fault credit */
Christian Königd9589392018-01-09 19:18:59 +01002837 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002838 return true;
Christian Königd9589392018-01-09 19:18:59 +01002839 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002840
2841 /* No lock needed. only accessed by IRQ handler */
Christian Königd9589392018-01-09 19:18:59 +01002842 if (!vm->fault_credit) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002843 /* Too many faults in this VM */
Christian Königd9589392018-01-09 19:18:59 +01002844 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002845 return false;
Christian Königd9589392018-01-09 19:18:59 +01002846 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002847
2848 vm->fault_credit--;
Christian Königd9589392018-01-09 19:18:59 +01002849 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002850 return true;
2851}
2852
2853/**
Christian Königa9a78b32016-01-21 10:19:11 +01002854 * amdgpu_vm_manager_init - init the VM manager
2855 *
2856 * @adev: amdgpu_device pointer
2857 *
2858 * Initialize the VM manager structures
2859 */
2860void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2861{
Christian König620f7742017-12-18 16:53:03 +01002862 unsigned i;
Christian Königa9a78b32016-01-21 10:19:11 +01002863
Christian König620f7742017-12-18 16:53:03 +01002864 amdgpu_vmid_mgr_init(adev);
Christian König2d55e452016-02-08 17:37:38 +01002865
Chris Wilsonf54d1862016-10-25 13:00:45 +01002866 adev->vm_manager.fence_context =
2867 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002868 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2869 adev->vm_manager.seqno[i] = 0;
2870
Christian König2d55e452016-02-08 17:37:38 +01002871 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian König284710f2017-01-30 11:09:31 +01002872 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002873 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002874
2875 /* If not overridden by the user, by default, only in large BAR systems
2876 * Compute VM tables will be updated by CPU
2877 */
2878#ifdef CONFIG_X86_64
2879 if (amdgpu_vm_update_mode == -1) {
2880 if (amdgpu_vm_is_large_bar(adev))
2881 adev->vm_manager.vm_update_mode =
2882 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2883 else
2884 adev->vm_manager.vm_update_mode = 0;
2885 } else
2886 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2887#else
2888 adev->vm_manager.vm_update_mode = 0;
2889#endif
2890
Felix Kuehling02208442017-08-25 20:40:26 -04002891 idr_init(&adev->vm_manager.pasid_idr);
2892 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002893}
2894
2895/**
Christian Königea89f8c2015-11-15 20:52:06 +01002896 * amdgpu_vm_manager_fini - cleanup VM manager
2897 *
2898 * @adev: amdgpu_device pointer
2899 *
2900 * Cleanup the VM manager and free resources.
2901 */
2902void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2903{
Felix Kuehling02208442017-08-25 20:40:26 -04002904 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2905 idr_destroy(&adev->vm_manager.pasid_idr);
2906
Christian König620f7742017-12-18 16:53:03 +01002907 amdgpu_vmid_mgr_fini(adev);
Christian Königea89f8c2015-11-15 20:52:06 +01002908}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002909
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002910/**
2911 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
2912 *
2913 * @dev: drm device pointer
2914 * @data: drm_amdgpu_vm
2915 * @filp: drm file pointer
2916 *
2917 * Returns:
2918 * 0 for success, -errno for errors.
2919 */
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002920int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2921{
2922 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002923 struct amdgpu_device *adev = dev->dev_private;
2924 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2925 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002926
2927 switch (args->in.op) {
2928 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002929 /* current, we only have requirement to reserve vmid from gfxhub */
Christian König620f7742017-12-18 16:53:03 +01002930 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002931 if (r)
2932 return r;
2933 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002934 case AMDGPU_VM_OP_UNRESERVE_VMID:
Christian König620f7742017-12-18 16:53:03 +01002935 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002936 break;
2937 default:
2938 return -EINVAL;
2939 }
2940
2941 return 0;
2942}