blob: 5d7d7900ccabbb5e6dc56e08b9261ec24843dccf [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"
Andrey Grodzovskyc8c5e562018-06-12 14:28:20 -040036#include "amdgpu_gmc.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040037
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040038/**
39 * DOC: GPUVM
40 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -040041 * GPUVM is similar to the legacy gart on older asics, however
42 * rather than there being a single global gart table
43 * for the entire GPU, there are multiple VM page tables active
44 * at any given time. The VM page tables can contain a mix
45 * vram pages and system memory pages and system memory pages
46 * can be mapped as snooped (cached system pages) or unsnooped
47 * (uncached system pages).
48 * Each VM has an ID associated with it and there is a page table
49 * associated with each VMID. When execting a command buffer,
50 * the kernel tells the the ring what VMID to use for that command
51 * buffer. VMIDs are allocated dynamically as commands are submitted.
52 * The userspace drivers maintain their own address space and the kernel
53 * sets up their pages tables accordingly when they submit their
54 * command buffers and a VMID is assigned.
55 * Cayman/Trinity support up to 8 active VMs at any given time;
56 * SI supports 16.
57 */
58
Christian Königa9f87f62017-03-30 14:03:59 +020059#define START(node) ((node)->start)
60#define LAST(node) ((node)->last)
61
62INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
63 START, LAST, static, amdgpu_vm_it)
64
65#undef START
66#undef LAST
67
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040068/**
69 * struct amdgpu_pte_update_params - Local structure
70 *
71 * Encapsulate some VM table update parameters to reduce
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040072 * the number of function parameters
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040073 *
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040074 */
Christian König29efc4f2016-08-04 14:52:50 +020075struct amdgpu_pte_update_params {
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040076
77 /**
78 * @adev: amdgpu device we do this update for
79 */
Christian König27c5f362016-08-04 15:02:49 +020080 struct amdgpu_device *adev;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040081
82 /**
83 * @vm: optional amdgpu_vm we do this update for
84 */
Christian König49ac8a22016-10-13 15:09:08 +020085 struct amdgpu_vm *vm;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040086
87 /**
88 * @src: address where to copy page table entries from
89 */
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040090 uint64_t src;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040091
92 /**
93 * @ib: indirect buffer to fill with commands
94 */
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040095 struct amdgpu_ib *ib;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -040096
97 /**
98 * @func: Function which actually does the update
99 */
Christian König373ac642018-01-16 16:54:25 +0100100 void (*func)(struct amdgpu_pte_update_params *params,
101 struct amdgpu_bo *bo, uint64_t pe,
Christian Königafef8b82016-08-12 13:29:18 +0200102 uint64_t addr, unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800103 uint64_t flags);
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400104 /**
105 * @pages_addr:
106 *
107 * DMA addresses to use for mapping, used during VM update by CPU
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400108 */
109 dma_addr_t *pages_addr;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400110
111 /**
112 * @kptr:
113 *
114 * Kernel pointer of PD/PT BO that needs to be updated,
115 * used during VM update by CPU
116 */
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400117 void *kptr;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400118};
119
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400120/**
121 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
122 */
Christian König284710f2017-01-30 11:09:31 +0100123struct amdgpu_prt_cb {
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400124
125 /**
126 * @adev: amdgpu device
127 */
Christian König284710f2017-01-30 11:09:31 +0100128 struct amdgpu_device *adev;
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400129
130 /**
131 * @cb: callback
132 */
Christian König284710f2017-01-30 11:09:31 +0100133 struct dma_fence_cb cb;
134};
135
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400136/**
137 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
138 *
139 * @base: base structure for tracking BO usage in a VM
140 * @vm: vm to which bo is to be added
141 * @bo: amdgpu buffer object
142 *
143 * Initialize a bo_va_base structure and add it to the appropriate lists
144 *
145 */
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800146static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
147 struct amdgpu_vm *vm,
148 struct amdgpu_bo *bo)
149{
150 base->vm = vm;
151 base->bo = bo;
152 INIT_LIST_HEAD(&base->bo_list);
153 INIT_LIST_HEAD(&base->vm_status);
154
155 if (!bo)
156 return;
157 list_add_tail(&base->bo_list, &bo->va);
158
Andrey Grodzovskye8511572018-07-05 14:49:34 -0400159 if (bo->tbo.type == ttm_bo_type_kernel)
160 list_move(&base->vm_status, &vm->relocated);
161
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800162 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
163 return;
164
165 if (bo->preferred_domains &
166 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
167 return;
168
169 /*
170 * we checked all the prerequisites, but it looks like this per vm bo
171 * is currently evicted. add the bo to the evicted list to make sure it
172 * is validated on next vm use to avoid fault.
173 * */
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800174 list_move_tail(&base->vm_status, &vm->evicted);
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800175}
176
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400177/**
Christian König50783142017-11-27 14:01:51 +0100178 * amdgpu_vm_level_shift - return the addr shift for each level
179 *
180 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400181 * @level: VMPT level
Christian König50783142017-11-27 14:01:51 +0100182 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400183 * Returns:
184 * The number of bits the pfn needs to be right shifted for a level.
Christian König50783142017-11-27 14:01:51 +0100185 */
186static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
187 unsigned level)
188{
Chunming Zhou196f7482017-12-13 14:22:54 +0800189 unsigned shift = 0xff;
190
191 switch (level) {
192 case AMDGPU_VM_PDB2:
193 case AMDGPU_VM_PDB1:
194 case AMDGPU_VM_PDB0:
195 shift = 9 * (AMDGPU_VM_PDB0 - level) +
Christian König50783142017-11-27 14:01:51 +0100196 adev->vm_manager.block_size;
Chunming Zhou196f7482017-12-13 14:22:54 +0800197 break;
198 case AMDGPU_VM_PTB:
199 shift = 0;
200 break;
201 default:
202 dev_err(adev->dev, "the level%d isn't supported.\n", level);
203 }
204
205 return shift;
Christian König50783142017-11-27 14:01:51 +0100206}
207
208/**
Christian König72a7ec52016-10-19 11:03:57 +0200209 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210 *
211 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400212 * @level: VMPT level
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400213 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400214 * Returns:
215 * The number of entries in a page directory or page table.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216 */
Christian König72a7ec52016-10-19 11:03:57 +0200217static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
218 unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400219{
Chunming Zhou196f7482017-12-13 14:22:54 +0800220 unsigned shift = amdgpu_vm_level_shift(adev,
221 adev->vm_manager.root_level);
Christian König0410c5e2017-11-20 14:29:01 +0100222
Chunming Zhou196f7482017-12-13 14:22:54 +0800223 if (level == adev->vm_manager.root_level)
Christian König72a7ec52016-10-19 11:03:57 +0200224 /* For the root directory */
Christian König0410c5e2017-11-20 14:29:01 +0100225 return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
Chunming Zhou196f7482017-12-13 14:22:54 +0800226 else if (level != AMDGPU_VM_PTB)
Christian König0410c5e2017-11-20 14:29:01 +0100227 /* Everything in between */
228 return 512;
229 else
Christian König72a7ec52016-10-19 11:03:57 +0200230 /* For the page tables on the leaves */
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800231 return AMDGPU_VM_PTE_COUNT(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232}
233
234/**
Christian König72a7ec52016-10-19 11:03:57 +0200235 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236 *
237 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400238 * @level: VMPT level
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400240 * Returns:
241 * The size of the BO for a page directory or page table in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242 */
Christian König72a7ec52016-10-19 11:03:57 +0200243static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400244{
Christian König72a7ec52016-10-19 11:03:57 +0200245 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246}
247
248/**
Christian König56467eb2015-12-11 15:16:32 +0100249 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250 *
251 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100252 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100253 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254 *
255 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100256 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257 */
Christian König56467eb2015-12-11 15:16:32 +0100258void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
259 struct list_head *validated,
260 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400261{
Christian König3f3333f2017-08-03 14:02:13 +0200262 entry->robj = vm->root.base.bo;
Christian König56467eb2015-12-11 15:16:32 +0100263 entry->priority = 0;
Christian König67003a12016-10-12 14:46:26 +0200264 entry->tv.bo = &entry->robj->tbo;
Christian König56467eb2015-12-11 15:16:32 +0100265 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100266 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100267 list_add(&entry->tv.head, validated);
268}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269
Christian König56467eb2015-12-11 15:16:32 +0100270/**
Christian Königf7da30d2016-09-28 12:03:04 +0200271 * amdgpu_vm_validate_pt_bos - validate the page table BOs
Christian König56467eb2015-12-11 15:16:32 +0100272 *
Christian König5a712a82016-06-21 16:28:15 +0200273 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100274 * @vm: vm providing the BOs
Christian Königf7da30d2016-09-28 12:03:04 +0200275 * @validate: callback to do the validation
276 * @param: parameter for the validation callback
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400277 *
Christian Königf7da30d2016-09-28 12:03:04 +0200278 * Validate the page table BOs on command submission if neccessary.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400279 *
280 * Returns:
281 * Validation result.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282 */
Christian Königf7da30d2016-09-28 12:03:04 +0200283int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
284 int (*validate)(void *p, struct amdgpu_bo *bo),
285 void *param)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286{
Christian König3f3333f2017-08-03 14:02:13 +0200287 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Christian König91ccdd22018-04-19 11:02:54 +0200288 struct amdgpu_vm_bo_base *bo_base, *tmp;
289 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290
Christian König91ccdd22018-04-19 11:02:54 +0200291 list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
292 struct amdgpu_bo *bo = bo_base->bo;
Christian König5a712a82016-06-21 16:28:15 +0200293
Christian König3f3333f2017-08-03 14:02:13 +0200294 if (bo->parent) {
295 r = validate(param, bo);
296 if (r)
Christian König91ccdd22018-04-19 11:02:54 +0200297 break;
Christian König34d7be52017-08-24 12:32:55 +0200298
Christian König3f3333f2017-08-03 14:02:13 +0200299 spin_lock(&glob->lru_lock);
300 ttm_bo_move_to_lru_tail(&bo->tbo);
301 if (bo->shadow)
302 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
303 spin_unlock(&glob->lru_lock);
304 }
305
Christian Königaf4c0f62018-04-19 10:56:02 +0200306 if (bo->tbo.type != ttm_bo_type_kernel) {
307 spin_lock(&vm->moved_lock);
Christian König73fb16e2017-08-16 11:13:48 +0200308 list_move(&bo_base->vm_status, &vm->moved);
Christian Königaf4c0f62018-04-19 10:56:02 +0200309 spin_unlock(&vm->moved_lock);
310 } else {
Christian König73fb16e2017-08-16 11:13:48 +0200311 list_move(&bo_base->vm_status, &vm->relocated);
Christian Königaf4c0f62018-04-19 10:56:02 +0200312 }
Christian König3f3333f2017-08-03 14:02:13 +0200313 }
Christian König34d7be52017-08-24 12:32:55 +0200314
Christian König806f0432018-04-19 15:01:12 +0200315 spin_lock(&glob->lru_lock);
316 list_for_each_entry(bo_base, &vm->idle, vm_status) {
317 struct amdgpu_bo *bo = bo_base->bo;
318
319 if (!bo->parent)
320 continue;
321
322 ttm_bo_move_to_lru_tail(&bo->tbo);
323 if (bo->shadow)
324 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
325 }
326 spin_unlock(&glob->lru_lock);
327
Christian König91ccdd22018-04-19 11:02:54 +0200328 return r;
Christian König34d7be52017-08-24 12:32:55 +0200329}
330
331/**
332 * amdgpu_vm_ready - check VM is ready for updates
333 *
Christian König34d7be52017-08-24 12:32:55 +0200334 * @vm: VM to check
335 *
336 * Check if all VM PDs/PTs are ready for updates
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400337 *
338 * Returns:
339 * True if eviction list is empty.
Christian König34d7be52017-08-24 12:32:55 +0200340 */
Christian König3f3333f2017-08-03 14:02:13 +0200341bool amdgpu_vm_ready(struct amdgpu_vm *vm)
Christian König34d7be52017-08-24 12:32:55 +0200342{
Christian Königaf4c0f62018-04-19 10:56:02 +0200343 return list_empty(&vm->evicted);
Christian Königeceb8a12016-01-11 15:35:21 +0100344}
345
346/**
Christian König13307f72018-01-24 17:19:04 +0100347 * amdgpu_vm_clear_bo - initially clear the PDs/PTs
348 *
349 * @adev: amdgpu_device pointer
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400350 * @vm: VM to clear BO from
Christian König13307f72018-01-24 17:19:04 +0100351 * @bo: BO to clear
352 * @level: level this BO is at
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -0400353 * @pte_support_ats: indicate ATS support from PTE
Christian König13307f72018-01-24 17:19:04 +0100354 *
355 * Root PD needs to be reserved when calling this.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400356 *
357 * Returns:
358 * 0 on success, errno otherwise.
Christian König13307f72018-01-24 17:19:04 +0100359 */
360static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König45843122018-01-25 18:36:15 +0100361 struct amdgpu_vm *vm, struct amdgpu_bo *bo,
362 unsigned level, bool pte_support_ats)
Christian König13307f72018-01-24 17:19:04 +0100363{
364 struct ttm_operation_ctx ctx = { true, false };
365 struct dma_fence *fence = NULL;
Christian König45843122018-01-25 18:36:15 +0100366 unsigned entries, ats_entries;
Christian König13307f72018-01-24 17:19:04 +0100367 struct amdgpu_ring *ring;
368 struct amdgpu_job *job;
Christian König45843122018-01-25 18:36:15 +0100369 uint64_t addr;
Christian König13307f72018-01-24 17:19:04 +0100370 int r;
371
Christian König45843122018-01-25 18:36:15 +0100372 addr = amdgpu_bo_gpu_offset(bo);
373 entries = amdgpu_bo_size(bo) / 8;
374
375 if (pte_support_ats) {
376 if (level == adev->vm_manager.root_level) {
377 ats_entries = amdgpu_vm_level_shift(adev, level);
378 ats_entries += AMDGPU_GPU_PAGE_SHIFT;
379 ats_entries = AMDGPU_VA_HOLE_START >> ats_entries;
380 ats_entries = min(ats_entries, entries);
381 entries -= ats_entries;
382 } else {
383 ats_entries = entries;
384 entries = 0;
385 }
Christian König13307f72018-01-24 17:19:04 +0100386 } else {
Christian König45843122018-01-25 18:36:15 +0100387 ats_entries = 0;
Christian König13307f72018-01-24 17:19:04 +0100388 }
389
Nayan Deshmukh068c3302018-07-20 17:51:06 +0530390 ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
Christian König13307f72018-01-24 17:19:04 +0100391
392 r = reservation_object_reserve_shared(bo->tbo.resv);
393 if (r)
394 return r;
395
396 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
397 if (r)
398 goto error;
399
Christian König13307f72018-01-24 17:19:04 +0100400 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
401 if (r)
402 goto error;
403
Christian König45843122018-01-25 18:36:15 +0100404 if (ats_entries) {
405 uint64_t ats_value;
406
407 ats_value = AMDGPU_PTE_DEFAULT_ATC;
408 if (level != AMDGPU_VM_PTB)
409 ats_value |= AMDGPU_PDE_PTE;
410
411 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
412 ats_entries, 0, ats_value);
413 addr += ats_entries * 8;
414 }
415
416 if (entries)
417 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
418 entries, 0, 0);
419
Christian König13307f72018-01-24 17:19:04 +0100420 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
421
422 WARN_ON(job->ibs[0].length_dw > 64);
Christian König29e83572018-02-04 19:36:52 +0100423 r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
424 AMDGPU_FENCE_OWNER_UNDEFINED, false);
425 if (r)
426 goto error_free;
427
Christian König0e28b102018-07-13 13:54:56 +0200428 r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_UNDEFINED,
429 &fence);
Christian König13307f72018-01-24 17:19:04 +0100430 if (r)
431 goto error_free;
432
433 amdgpu_bo_fence(bo, fence, true);
434 dma_fence_put(fence);
Christian Könige61736d2018-02-02 21:05:40 +0100435
436 if (bo->shadow)
437 return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
438 level, pte_support_ats);
439
Christian König13307f72018-01-24 17:19:04 +0100440 return 0;
441
442error_free:
443 amdgpu_job_free(job);
444
445error:
446 return r;
447}
448
449/**
Christian Königf566ceb2016-10-27 20:04:38 +0200450 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
451 *
452 * @adev: amdgpu_device pointer
453 * @vm: requested vm
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400454 * @parent: parent PT
Christian Königf566ceb2016-10-27 20:04:38 +0200455 * @saddr: start of the address range
456 * @eaddr: end of the address range
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400457 * @level: VMPT level
458 * @ats: indicate ATS support from PTE
Christian Königf566ceb2016-10-27 20:04:38 +0200459 *
460 * Make sure the page directories and page tables are allocated
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400461 *
462 * Returns:
463 * 0 on success, errno otherwise.
Christian Königf566ceb2016-10-27 20:04:38 +0200464 */
465static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
466 struct amdgpu_vm *vm,
467 struct amdgpu_vm_pt *parent,
468 uint64_t saddr, uint64_t eaddr,
Christian König45843122018-01-25 18:36:15 +0100469 unsigned level, bool ats)
Christian Königf566ceb2016-10-27 20:04:38 +0200470{
Christian König50783142017-11-27 14:01:51 +0100471 unsigned shift = amdgpu_vm_level_shift(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +0200472 unsigned pt_idx, from, to;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400473 u64 flags;
Christian König13307f72018-01-24 17:19:04 +0100474 int r;
Christian Königf566ceb2016-10-27 20:04:38 +0200475
476 if (!parent->entries) {
477 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
478
Michal Hocko20981052017-05-17 14:23:12 +0200479 parent->entries = kvmalloc_array(num_entries,
480 sizeof(struct amdgpu_vm_pt),
481 GFP_KERNEL | __GFP_ZERO);
Christian Königf566ceb2016-10-27 20:04:38 +0200482 if (!parent->entries)
483 return -ENOMEM;
484 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
485 }
486
Felix Kuehling1866bac2017-03-28 20:36:12 -0400487 from = saddr >> shift;
488 to = eaddr >> shift;
489 if (from >= amdgpu_vm_num_entries(adev, level) ||
490 to >= amdgpu_vm_num_entries(adev, level))
491 return -EINVAL;
Christian Königf566ceb2016-10-27 20:04:38 +0200492
Christian Königf566ceb2016-10-27 20:04:38 +0200493 ++level;
Felix Kuehling1866bac2017-03-28 20:36:12 -0400494 saddr = saddr & ((1 << shift) - 1);
495 eaddr = eaddr & ((1 << shift) - 1);
Christian Königf566ceb2016-10-27 20:04:38 +0200496
Christian König13307f72018-01-24 17:19:04 +0100497 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400498 if (vm->use_cpu_for_update)
499 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
500 else
501 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
502 AMDGPU_GEM_CREATE_SHADOW);
503
Christian Königf566ceb2016-10-27 20:04:38 +0200504 /* walk over the address space and allocate the page tables */
505 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
Christian König3f3333f2017-08-03 14:02:13 +0200506 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian Königf566ceb2016-10-27 20:04:38 +0200507 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
508 struct amdgpu_bo *pt;
509
Christian König3f3333f2017-08-03 14:02:13 +0200510 if (!entry->base.bo) {
Chunming Zhou3216c6b2018-04-16 18:27:50 +0800511 struct amdgpu_bo_param bp;
512
513 memset(&bp, 0, sizeof(bp));
514 bp.size = amdgpu_vm_bo_size(adev, level);
515 bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
516 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
517 bp.flags = flags;
518 bp.type = ttm_bo_type_kernel;
519 bp.resv = resv;
520 r = amdgpu_bo_create(adev, &bp, &pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200521 if (r)
522 return r;
523
Christian König45843122018-01-25 18:36:15 +0100524 r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats);
Christian König13307f72018-01-24 17:19:04 +0100525 if (r) {
Christian Könige5197a42018-02-02 21:00:44 +0100526 amdgpu_bo_unref(&pt->shadow);
Christian König13307f72018-01-24 17:19:04 +0100527 amdgpu_bo_unref(&pt);
528 return r;
529 }
530
Christian König0a096fb2017-07-12 10:01:48 +0200531 if (vm->use_cpu_for_update) {
532 r = amdgpu_bo_kmap(pt, NULL);
533 if (r) {
Christian Könige5197a42018-02-02 21:00:44 +0100534 amdgpu_bo_unref(&pt->shadow);
Christian König0a096fb2017-07-12 10:01:48 +0200535 amdgpu_bo_unref(&pt);
536 return r;
537 }
538 }
539
Christian Königf566ceb2016-10-27 20:04:38 +0200540 /* Keep a reference to the root directory to avoid
541 * freeing them up in the wrong order.
542 */
Christian König0f2fc432017-08-31 10:46:20 +0200543 pt->parent = amdgpu_bo_ref(parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +0200544
Chunming Zhou3f4299b2018-04-24 12:14:39 +0800545 amdgpu_vm_bo_base_init(&entry->base, vm, pt);
Christian Königf566ceb2016-10-27 20:04:38 +0200546 }
547
Chunming Zhou196f7482017-12-13 14:22:54 +0800548 if (level < AMDGPU_VM_PTB) {
Felix Kuehling1866bac2017-03-28 20:36:12 -0400549 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
550 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
551 ((1 << shift) - 1);
552 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
Christian König45843122018-01-25 18:36:15 +0100553 sub_eaddr, level, ats);
Christian Königf566ceb2016-10-27 20:04:38 +0200554 if (r)
555 return r;
556 }
557 }
558
559 return 0;
560}
561
Christian König663e4572017-03-13 10:13:37 +0100562/**
563 * amdgpu_vm_alloc_pts - Allocate page tables.
564 *
565 * @adev: amdgpu_device pointer
566 * @vm: VM to allocate page tables for
567 * @saddr: Start address which needs to be allocated
568 * @size: Size from start address we need.
569 *
570 * Make sure the page tables are allocated.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400571 *
572 * Returns:
573 * 0 on success, errno otherwise.
Christian König663e4572017-03-13 10:13:37 +0100574 */
575int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
576 struct amdgpu_vm *vm,
577 uint64_t saddr, uint64_t size)
578{
Christian König663e4572017-03-13 10:13:37 +0100579 uint64_t eaddr;
Christian König45843122018-01-25 18:36:15 +0100580 bool ats = false;
Christian König663e4572017-03-13 10:13:37 +0100581
582 /* validate the parameters */
583 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
584 return -EINVAL;
585
586 eaddr = saddr + size - 1;
Christian König45843122018-01-25 18:36:15 +0100587
588 if (vm->pte_support_ats)
589 ats = saddr < AMDGPU_VA_HOLE_START;
Christian König663e4572017-03-13 10:13:37 +0100590
591 saddr /= AMDGPU_GPU_PAGE_SIZE;
592 eaddr /= AMDGPU_GPU_PAGE_SIZE;
593
Christian König45843122018-01-25 18:36:15 +0100594 if (eaddr >= adev->vm_manager.max_pfn) {
595 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
596 eaddr, adev->vm_manager.max_pfn);
597 return -EINVAL;
598 }
599
Chunming Zhou196f7482017-12-13 14:22:54 +0800600 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
Christian König45843122018-01-25 18:36:15 +0100601 adev->vm_manager.root_level, ats);
Christian König663e4572017-03-13 10:13:37 +0100602}
603
Christian König641e9402017-04-03 13:59:25 +0200604/**
Alex Xiee59c0202017-06-01 09:42:59 -0400605 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
606 *
607 * @adev: amdgpu_device pointer
608 */
609void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
610{
611 const struct amdgpu_ip_block *ip_block;
612 bool has_compute_vm_bug;
613 struct amdgpu_ring *ring;
614 int i;
615
616 has_compute_vm_bug = false;
617
Alex Deucher2990a1f2017-12-15 16:18:00 -0500618 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
Alex Xiee59c0202017-06-01 09:42:59 -0400619 if (ip_block) {
620 /* Compute has a VM bug for GFX version < 7.
621 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
622 if (ip_block->version->major <= 7)
623 has_compute_vm_bug = true;
624 else if (ip_block->version->major == 8)
625 if (adev->gfx.mec_fw_version < 673)
626 has_compute_vm_bug = true;
627 }
628
629 for (i = 0; i < adev->num_rings; i++) {
630 ring = adev->rings[i];
631 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
632 /* only compute rings */
633 ring->has_compute_vm_bug = has_compute_vm_bug;
634 else
635 ring->has_compute_vm_bug = false;
636 }
637}
638
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400639/**
640 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
641 *
642 * @ring: ring on which the job will be submitted
643 * @job: job to submit
644 *
645 * Returns:
646 * True if sync is needed.
647 */
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400648bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
649 struct amdgpu_job *job)
650{
651 struct amdgpu_device *adev = ring->adev;
652 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100653 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
654 struct amdgpu_vmid *id;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400655 bool gds_switch_needed;
Alex Xiee59c0202017-06-01 09:42:59 -0400656 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400657
Christian Königc4f46f22017-12-18 17:08:25 +0100658 if (job->vmid == 0)
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400659 return false;
Christian Königc4f46f22017-12-18 17:08:25 +0100660 id = &id_mgr->ids[job->vmid];
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400661 gds_switch_needed = ring->funcs->emit_gds_switch && (
662 id->gds_base != job->gds_base ||
663 id->gds_size != job->gds_size ||
664 id->gws_base != job->gws_base ||
665 id->gws_size != job->gws_size ||
666 id->oa_base != job->oa_base ||
667 id->oa_size != job->oa_size);
668
Christian König620f7742017-12-18 16:53:03 +0100669 if (amdgpu_vmid_had_gpu_reset(adev, id))
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400670 return true;
Alex Xiebb37b672017-05-30 23:50:10 -0400671
672 return vm_flush_needed || gds_switch_needed;
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400673}
674
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400675/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 * amdgpu_vm_flush - hardware flush the vm
677 *
678 * @ring: ring to use for flush
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -0400679 * @job: related job
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400680 * @need_pipe_sync: is pipe sync needed
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 *
Christian König4ff37a82016-02-26 16:18:26 +0100682 * Emit a VM flush when it is necessary.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400683 *
684 * Returns:
685 * 0 on success, errno otherwise.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 */
Monk Liu8fdf0742017-06-06 17:25:13 +0800687int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688{
Christian König971fe9a92016-03-01 15:09:25 +0100689 struct amdgpu_device *adev = ring->adev;
Christian König76456702017-04-06 17:52:39 +0200690 unsigned vmhub = ring->funcs->vmhub;
Christian König620f7742017-12-18 16:53:03 +0100691 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
Christian Königc4f46f22017-12-18 17:08:25 +0100692 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
Christian Königd564a062016-03-01 15:51:53 +0100693 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800694 id->gds_base != job->gds_base ||
695 id->gds_size != job->gds_size ||
696 id->gws_base != job->gws_base ||
697 id->gws_size != job->gws_size ||
698 id->oa_base != job->oa_base ||
699 id->oa_size != job->oa_size);
Flora Cuide37e682017-05-18 13:56:22 +0800700 bool vm_flush_needed = job->vm_needs_flush;
Christian Königb3cd2852018-02-05 17:38:01 +0100701 bool pasid_mapping_needed = id->pasid != job->pasid ||
702 !id->pasid_mapping ||
703 !dma_fence_is_signaled(id->pasid_mapping);
704 struct dma_fence *fence = NULL;
Christian Königc0e51932017-04-03 14:16:07 +0200705 unsigned patch_offset = 0;
Christian König41d9eb22016-03-01 16:46:18 +0100706 int r;
Christian Königd564a062016-03-01 15:51:53 +0100707
Christian König620f7742017-12-18 16:53:03 +0100708 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
Christian Königf7d015b2017-04-03 14:28:26 +0200709 gds_switch_needed = true;
710 vm_flush_needed = true;
Christian Königb3cd2852018-02-05 17:38:01 +0100711 pasid_mapping_needed = true;
Christian Königf7d015b2017-04-03 14:28:26 +0200712 }
Christian König971fe9a92016-03-01 15:09:25 +0100713
Christian Königb3cd2852018-02-05 17:38:01 +0100714 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
715 vm_flush_needed &= !!ring->funcs->emit_vm_flush;
716 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
717 ring->funcs->emit_wreg;
718
Monk Liu8fdf0742017-06-06 17:25:13 +0800719 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
Christian Königf7d015b2017-04-03 14:28:26 +0200720 return 0;
Christian König41d9eb22016-03-01 16:46:18 +0100721
Christian Königc0e51932017-04-03 14:16:07 +0200722 if (ring->funcs->init_cond_exec)
723 patch_offset = amdgpu_ring_init_cond_exec(ring);
Christian König41d9eb22016-03-01 16:46:18 +0100724
Monk Liu8fdf0742017-06-06 17:25:13 +0800725 if (need_pipe_sync)
726 amdgpu_ring_emit_pipeline_sync(ring);
727
Christian Königb3cd2852018-02-05 17:38:01 +0100728 if (vm_flush_needed) {
Christian Königc4f46f22017-12-18 17:08:25 +0100729 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian Königc633c002018-02-04 10:32:35 +0100730 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
Christian Königb3cd2852018-02-05 17:38:01 +0100731 }
Monk Liue9d672b2017-03-15 12:18:57 +0800732
Christian Königb3cd2852018-02-05 17:38:01 +0100733 if (pasid_mapping_needed)
734 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
735
736 if (vm_flush_needed || pasid_mapping_needed) {
Marek Olšákd240cd92018-04-03 13:05:03 -0400737 r = amdgpu_fence_emit(ring, &fence, 0);
Christian Königc0e51932017-04-03 14:16:07 +0200738 if (r)
739 return r;
Christian Königb3cd2852018-02-05 17:38:01 +0100740 }
Monk Liue9d672b2017-03-15 12:18:57 +0800741
Christian Königb3cd2852018-02-05 17:38:01 +0100742 if (vm_flush_needed) {
Christian König76456702017-04-06 17:52:39 +0200743 mutex_lock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200744 dma_fence_put(id->last_flush);
Christian Königb3cd2852018-02-05 17:38:01 +0100745 id->last_flush = dma_fence_get(fence);
746 id->current_gpu_reset_count =
747 atomic_read(&adev->gpu_reset_counter);
Christian König76456702017-04-06 17:52:39 +0200748 mutex_unlock(&id_mgr->lock);
Christian Königc0e51932017-04-03 14:16:07 +0200749 }
Monk Liue9d672b2017-03-15 12:18:57 +0800750
Christian Königb3cd2852018-02-05 17:38:01 +0100751 if (pasid_mapping_needed) {
752 id->pasid = job->pasid;
753 dma_fence_put(id->pasid_mapping);
754 id->pasid_mapping = dma_fence_get(fence);
755 }
756 dma_fence_put(fence);
757
Chunming Zhou7c4378f2017-05-11 18:22:17 +0800758 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
Christian Königc0e51932017-04-03 14:16:07 +0200759 id->gds_base = job->gds_base;
760 id->gds_size = job->gds_size;
761 id->gws_base = job->gws_base;
762 id->gws_size = job->gws_size;
763 id->oa_base = job->oa_base;
764 id->oa_size = job->oa_size;
Christian Königc4f46f22017-12-18 17:08:25 +0100765 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
Christian Königc0e51932017-04-03 14:16:07 +0200766 job->gds_size, job->gws_base,
767 job->gws_size, job->oa_base,
768 job->oa_size);
769 }
770
771 if (ring->funcs->patch_cond_exec)
772 amdgpu_ring_patch_cond_exec(ring, patch_offset);
773
774 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
775 if (ring->funcs->emit_switch_buffer) {
776 amdgpu_ring_emit_switch_buffer(ring);
777 amdgpu_ring_emit_switch_buffer(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400778 }
Christian König41d9eb22016-03-01 16:46:18 +0100779 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100780}
781
782/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400783 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
784 *
785 * @vm: requested vm
786 * @bo: requested buffer object
787 *
Christian König8843dbb2016-01-26 12:17:11 +0100788 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 * Search inside the @bos vm list for the requested vm
790 * Returns the found bo_va or NULL if none is found
791 *
792 * Object has to be reserved!
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400793 *
794 * Returns:
795 * Found bo_va or NULL.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796 */
797struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
798 struct amdgpu_bo *bo)
799{
800 struct amdgpu_bo_va *bo_va;
801
Christian Königec681542017-08-01 10:51:43 +0200802 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
803 if (bo_va->base.vm == vm) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 return bo_va;
805 }
806 }
807 return NULL;
808}
809
810/**
Christian Königafef8b82016-08-12 13:29:18 +0200811 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400812 *
Christian König29efc4f2016-08-04 14:52:50 +0200813 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100814 * @bo: PD/PT to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400815 * @pe: addr of the page entry
816 * @addr: dst addr to write into pe
817 * @count: number of page entries to update
818 * @incr: increase next addr by incr bytes
819 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820 *
821 * Traces the parameters and calls the right asic functions
822 * to setup the page table using the DMA.
823 */
Christian Königafef8b82016-08-12 13:29:18 +0200824static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100825 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200826 uint64_t pe, uint64_t addr,
827 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800828 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829{
Christian König373ac642018-01-16 16:54:25 +0100830 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200831 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400832
Christian Königafef8b82016-08-12 13:29:18 +0200833 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200834 amdgpu_vm_write_pte(params->adev, params->ib, pe,
835 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400836
837 } else {
Christian König27c5f362016-08-04 15:02:49 +0200838 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400839 count, incr, flags);
840 }
841}
842
843/**
Christian Königafef8b82016-08-12 13:29:18 +0200844 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
845 *
846 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100847 * @bo: PD/PT to update
Christian Königafef8b82016-08-12 13:29:18 +0200848 * @pe: addr of the page entry
849 * @addr: dst addr to write into pe
850 * @count: number of page entries to update
851 * @incr: increase next addr by incr bytes
852 * @flags: hw access flags
853 *
854 * Traces the parameters and calls the DMA function to copy the PTEs.
855 */
856static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100857 struct amdgpu_bo *bo,
Christian Königafef8b82016-08-12 13:29:18 +0200858 uint64_t pe, uint64_t addr,
859 unsigned count, uint32_t incr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800860 uint64_t flags)
Christian Königafef8b82016-08-12 13:29:18 +0200861{
Christian Königec2f05f2016-09-25 16:11:52 +0200862 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200863
Christian König373ac642018-01-16 16:54:25 +0100864 pe += amdgpu_bo_gpu_offset(bo);
Christian Königec2f05f2016-09-25 16:11:52 +0200865 trace_amdgpu_vm_copy_ptes(pe, src, count);
866
867 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200868}
869
870/**
Christian Königb07c9d22015-11-30 13:26:07 +0100871 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400872 *
Christian Königb07c9d22015-11-30 13:26:07 +0100873 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 * @addr: the unmapped addr
875 *
876 * Look up the physical address of the page that the pte resolves
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400877 * to.
878 *
879 * Returns:
880 * The pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400881 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200882static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400883{
884 uint64_t result;
885
Christian Königde9ea7b2016-08-12 11:33:30 +0200886 /* page table offset */
887 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400888
Christian Königde9ea7b2016-08-12 11:33:30 +0200889 /* in case cpu page size != gpu page size*/
890 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100891
892 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400893
894 return result;
895}
896
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400897/**
898 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
899 *
900 * @params: see amdgpu_pte_update_params definition
Christian König373ac642018-01-16 16:54:25 +0100901 * @bo: PD/PT to update
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400902 * @pe: kmap addr of the page entry
903 * @addr: dst addr to write into pe
904 * @count: number of page entries to update
905 * @incr: increase next addr by incr bytes
906 * @flags: hw access flags
907 *
908 * Write count number of PT/PD entries directly.
909 */
910static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
Christian König373ac642018-01-16 16:54:25 +0100911 struct amdgpu_bo *bo,
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400912 uint64_t pe, uint64_t addr,
913 unsigned count, uint32_t incr,
914 uint64_t flags)
915{
916 unsigned int i;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400917 uint64_t value;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400918
Christian König373ac642018-01-16 16:54:25 +0100919 pe += (unsigned long)amdgpu_bo_kptr(bo);
920
Christian König03918b32017-07-11 17:15:37 +0200921 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
922
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400923 for (i = 0; i < count; i++) {
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -0400924 value = params->pages_addr ?
925 amdgpu_vm_map_gart(params->pages_addr, addr) :
926 addr;
Christian König132f34e2018-01-12 15:26:08 +0100927 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
928 i, value, flags);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400929 addr += incr;
930 }
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400931}
932
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400933
934/**
935 * amdgpu_vm_wait_pd - Wait for PT BOs to be free.
936 *
937 * @adev: amdgpu_device pointer
938 * @vm: related vm
939 * @owner: fence owner
940 *
941 * Returns:
942 * 0 on success, errno otherwise.
943 */
Christian Königa33cab72017-07-11 17:13:00 +0200944static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
945 void *owner)
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400946{
947 struct amdgpu_sync sync;
948 int r;
949
950 amdgpu_sync_create(&sync);
Andres Rodriguez177ae092017-09-15 20:44:06 -0400951 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400952 r = amdgpu_sync_wait(&sync, true);
953 amdgpu_sync_free(&sync);
954
955 return r;
956}
957
Christian Königf8991ba2016-09-16 15:36:49 +0200958/*
Christian König6989f242017-11-30 19:08:05 +0100959 * amdgpu_vm_update_pde - update a single level in the hierarchy
Christian Königf8991ba2016-09-16 15:36:49 +0200960 *
Christian König6989f242017-11-30 19:08:05 +0100961 * @param: parameters for the update
Christian Königf8991ba2016-09-16 15:36:49 +0200962 * @vm: requested vm
Christian König194d2162016-10-12 15:13:52 +0200963 * @parent: parent directory
Christian König6989f242017-11-30 19:08:05 +0100964 * @entry: entry to update
Christian Königf8991ba2016-09-16 15:36:49 +0200965 *
Christian König6989f242017-11-30 19:08:05 +0100966 * Makes sure the requested entry in parent is up to date.
Christian Königf8991ba2016-09-16 15:36:49 +0200967 */
Christian König6989f242017-11-30 19:08:05 +0100968static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
969 struct amdgpu_vm *vm,
970 struct amdgpu_vm_pt *parent,
971 struct amdgpu_vm_pt *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972{
Christian König373ac642018-01-16 16:54:25 +0100973 struct amdgpu_bo *bo = parent->base.bo, *pbo;
Christian König3de676d2017-11-29 13:27:26 +0100974 uint64_t pde, pt, flags;
975 unsigned level;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800976
Christian König6989f242017-11-30 19:08:05 +0100977 /* Don't update huge pages here */
978 if (entry->huge)
979 return;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -0400980
Christian König373ac642018-01-16 16:54:25 +0100981 for (level = 0, pbo = bo->parent; pbo; ++level)
Christian König3de676d2017-11-29 13:27:26 +0100982 pbo = pbo->parent;
983
Chunming Zhou196f7482017-12-13 14:22:54 +0800984 level += params->adev->vm_manager.root_level;
Christian König373ac642018-01-16 16:54:25 +0100985 pt = amdgpu_bo_gpu_offset(entry->base.bo);
Christian König3de676d2017-11-29 13:27:26 +0100986 flags = AMDGPU_PTE_VALID;
Christian König132f34e2018-01-12 15:26:08 +0100987 amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
Christian König373ac642018-01-16 16:54:25 +0100988 pde = (entry - parent->entries) * 8;
989 if (bo->shadow)
990 params->func(params, bo->shadow, pde, pt, 1, 0, flags);
991 params->func(params, bo, pde, pt, 1, 0, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400992}
993
Christian König194d2162016-10-12 15:13:52 +0200994/*
Christian König92456b92017-05-12 16:09:26 +0200995 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
996 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -0400997 * @adev: amdgpu_device pointer
998 * @vm: related vm
Christian König92456b92017-05-12 16:09:26 +0200999 * @parent: parent PD
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001000 * @level: VMPT level
Christian König92456b92017-05-12 16:09:26 +02001001 *
1002 * Mark all PD level as invalid after an error.
1003 */
Christian König8f19cd72017-11-30 15:28:03 +01001004static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
1005 struct amdgpu_vm *vm,
1006 struct amdgpu_vm_pt *parent,
1007 unsigned level)
Christian König92456b92017-05-12 16:09:26 +02001008{
Christian König8f19cd72017-11-30 15:28:03 +01001009 unsigned pt_idx, num_entries;
Christian König92456b92017-05-12 16:09:26 +02001010
1011 /*
1012 * Recurse into the subdirectories. This recursion is harmless because
1013 * we only have a maximum of 5 layers.
1014 */
Christian König8f19cd72017-11-30 15:28:03 +01001015 num_entries = amdgpu_vm_num_entries(adev, level);
1016 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
Christian König92456b92017-05-12 16:09:26 +02001017 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1018
Christian König3f3333f2017-08-03 14:02:13 +02001019 if (!entry->base.bo)
Christian König92456b92017-05-12 16:09:26 +02001020 continue;
1021
Christian König862b8c52018-04-19 14:22:56 +02001022 if (!entry->base.moved)
1023 list_move(&entry->base.vm_status, &vm->relocated);
Christian König8f19cd72017-11-30 15:28:03 +01001024 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
Christian König92456b92017-05-12 16:09:26 +02001025 }
1026}
1027
1028/*
Christian König194d2162016-10-12 15:13:52 +02001029 * amdgpu_vm_update_directories - make sure that all directories are valid
1030 *
1031 * @adev: amdgpu_device pointer
1032 * @vm: requested vm
1033 *
1034 * Makes sure all directories are up to date.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001035 *
1036 * Returns:
1037 * 0 for success, error for failure.
Christian König194d2162016-10-12 15:13:52 +02001038 */
1039int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1040 struct amdgpu_vm *vm)
1041{
Christian König6989f242017-11-30 19:08:05 +01001042 struct amdgpu_pte_update_params params;
1043 struct amdgpu_job *job;
1044 unsigned ndw = 0;
Dan Carpenter78aa02c2017-09-30 11:14:13 +03001045 int r = 0;
Christian König92456b92017-05-12 16:09:26 +02001046
Christian König6989f242017-11-30 19:08:05 +01001047 if (list_empty(&vm->relocated))
1048 return 0;
1049
1050restart:
1051 memset(&params, 0, sizeof(params));
1052 params.adev = adev;
1053
1054 if (vm->use_cpu_for_update) {
Christian Königa7f91062018-04-19 13:58:42 +02001055 struct amdgpu_vm_bo_base *bo_base;
1056
1057 list_for_each_entry(bo_base, &vm->relocated, vm_status) {
1058 r = amdgpu_bo_kmap(bo_base->bo, NULL);
1059 if (unlikely(r))
1060 return r;
1061 }
1062
Christian König6989f242017-11-30 19:08:05 +01001063 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
1064 if (unlikely(r))
1065 return r;
1066
1067 params.func = amdgpu_vm_cpu_set_ptes;
1068 } else {
1069 ndw = 512 * 8;
1070 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1071 if (r)
1072 return r;
1073
1074 params.ib = &job->ibs[0];
1075 params.func = amdgpu_vm_do_set_ptes;
1076 }
1077
Christian Königea097292017-08-09 14:15:46 +02001078 while (!list_empty(&vm->relocated)) {
Christian König6989f242017-11-30 19:08:05 +01001079 struct amdgpu_vm_bo_base *bo_base, *parent;
1080 struct amdgpu_vm_pt *pt, *entry;
Christian Königea097292017-08-09 14:15:46 +02001081 struct amdgpu_bo *bo;
1082
1083 bo_base = list_first_entry(&vm->relocated,
1084 struct amdgpu_vm_bo_base,
1085 vm_status);
Christian König862b8c52018-04-19 14:22:56 +02001086 bo_base->moved = false;
Christian Königa315f232018-06-19 10:45:03 +02001087 list_del_init(&bo_base->vm_status);
Christian Königea097292017-08-09 14:15:46 +02001088
1089 bo = bo_base->bo->parent;
Christian Königaf4c0f62018-04-19 10:56:02 +02001090 if (!bo)
Christian König6989f242017-11-30 19:08:05 +01001091 continue;
Christian König6989f242017-11-30 19:08:05 +01001092
1093 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
1094 bo_list);
1095 pt = container_of(parent, struct amdgpu_vm_pt, base);
1096 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
1097
1098 amdgpu_vm_update_pde(&params, vm, pt, entry);
1099
Christian König6989f242017-11-30 19:08:05 +01001100 if (!vm->use_cpu_for_update &&
1101 (ndw - params.ib->length_dw) < 32)
1102 break;
Christian Königea097292017-08-09 14:15:46 +02001103 }
Christian König92456b92017-05-12 16:09:26 +02001104
Christian König68c62302017-07-11 17:23:29 +02001105 if (vm->use_cpu_for_update) {
1106 /* Flush HDP */
1107 mb();
Christian König69882562018-01-19 14:17:40 +01001108 amdgpu_asic_flush_hdp(adev, NULL);
Christian König6989f242017-11-30 19:08:05 +01001109 } else if (params.ib->length_dw == 0) {
1110 amdgpu_job_free(job);
1111 } else {
1112 struct amdgpu_bo *root = vm->root.base.bo;
1113 struct amdgpu_ring *ring;
1114 struct dma_fence *fence;
1115
Nayan Deshmukh068c3302018-07-20 17:51:06 +05301116 ring = container_of(vm->entity.rq->sched, struct amdgpu_ring,
Christian König6989f242017-11-30 19:08:05 +01001117 sched);
1118
1119 amdgpu_ring_pad_ib(ring, params.ib);
1120 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
1121 AMDGPU_FENCE_OWNER_VM, false);
Christian König6989f242017-11-30 19:08:05 +01001122 WARN_ON(params.ib->length_dw > ndw);
Christian König0e28b102018-07-13 13:54:56 +02001123 r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_VM,
1124 &fence);
Christian König6989f242017-11-30 19:08:05 +01001125 if (r)
1126 goto error;
1127
1128 amdgpu_bo_fence(root, fence, true);
1129 dma_fence_put(vm->last_update);
1130 vm->last_update = fence;
Christian König68c62302017-07-11 17:23:29 +02001131 }
1132
Christian König6989f242017-11-30 19:08:05 +01001133 if (!list_empty(&vm->relocated))
1134 goto restart;
1135
1136 return 0;
1137
1138error:
Chunming Zhou196f7482017-12-13 14:22:54 +08001139 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
1140 adev->vm_manager.root_level);
Christian König6989f242017-11-30 19:08:05 +01001141 amdgpu_job_free(job);
Christian König92456b92017-05-12 16:09:26 +02001142 return r;
Christian König194d2162016-10-12 15:13:52 +02001143}
1144
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001145/**
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001146 * amdgpu_vm_find_entry - find the entry for an address
Christian König4e2cb642016-10-25 15:52:28 +02001147 *
1148 * @p: see amdgpu_pte_update_params definition
1149 * @addr: virtual address in question
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001150 * @entry: resulting entry or NULL
1151 * @parent: parent entry
Christian König4e2cb642016-10-25 15:52:28 +02001152 *
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001153 * Find the vm_pt entry and it's parent for the given address.
Christian König4e2cb642016-10-25 15:52:28 +02001154 */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001155void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1156 struct amdgpu_vm_pt **entry,
1157 struct amdgpu_vm_pt **parent)
Christian König4e2cb642016-10-25 15:52:28 +02001158{
Chunming Zhou196f7482017-12-13 14:22:54 +08001159 unsigned level = p->adev->vm_manager.root_level;
Christian König4e2cb642016-10-25 15:52:28 +02001160
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001161 *parent = NULL;
1162 *entry = &p->vm->root;
1163 while ((*entry)->entries) {
Christian Könige3a1b322017-12-01 13:28:46 +01001164 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
Christian König50783142017-11-27 14:01:51 +01001165
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001166 *parent = *entry;
Christian Könige3a1b322017-12-01 13:28:46 +01001167 *entry = &(*entry)->entries[addr >> shift];
1168 addr &= (1ULL << shift) - 1;
Christian König4e2cb642016-10-25 15:52:28 +02001169 }
1170
Chunming Zhou196f7482017-12-13 14:22:54 +08001171 if (level != AMDGPU_VM_PTB)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001172 *entry = NULL;
1173}
Christian König4e2cb642016-10-25 15:52:28 +02001174
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001175/**
1176 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1177 *
1178 * @p: see amdgpu_pte_update_params definition
1179 * @entry: vm_pt entry to check
1180 * @parent: parent entry
1181 * @nptes: number of PTEs updated with this operation
1182 * @dst: destination address where the PTEs should point to
1183 * @flags: access flags fro the PTEs
1184 *
1185 * Check if we can update the PD with a huge page.
1186 */
Christian Königec5207c2017-08-03 19:24:06 +02001187static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1188 struct amdgpu_vm_pt *entry,
1189 struct amdgpu_vm_pt *parent,
1190 unsigned nptes, uint64_t dst,
1191 uint64_t flags)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001192{
Christian König373ac642018-01-16 16:54:25 +01001193 uint64_t pde;
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001194
1195 /* In the case of a mixed PT the PDE must point to it*/
Christian König3cc1d3e2017-12-21 15:47:28 +01001196 if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
1197 nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
Christian König4ab40162017-08-03 20:30:50 +02001198 /* Set the huge page flag to stop scanning at this PDE */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001199 flags |= AMDGPU_PDE_PTE;
1200 }
1201
Christian König3cc1d3e2017-12-21 15:47:28 +01001202 if (!(flags & AMDGPU_PDE_PTE)) {
1203 if (entry->huge) {
1204 /* Add the entry to the relocated list to update it. */
1205 entry->huge = false;
Christian König3cc1d3e2017-12-21 15:47:28 +01001206 list_move(&entry->base.vm_status, &p->vm->relocated);
Christian König3cc1d3e2017-12-21 15:47:28 +01001207 }
Christian Königec5207c2017-08-03 19:24:06 +02001208 return;
Christian König3cc1d3e2017-12-21 15:47:28 +01001209 }
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001210
Christian König3cc1d3e2017-12-21 15:47:28 +01001211 entry->huge = true;
Christian König132f34e2018-01-12 15:26:08 +01001212 amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
Christian König3de676d2017-11-29 13:27:26 +01001213
Christian König373ac642018-01-16 16:54:25 +01001214 pde = (entry - parent->entries) * 8;
1215 if (parent->base.bo->shadow)
1216 p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
1217 p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
Christian König4e2cb642016-10-25 15:52:28 +02001218}
1219
1220/**
Christian König92696dd2016-08-05 13:56:35 +02001221 * amdgpu_vm_update_ptes - make sure that page tables are valid
1222 *
1223 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001224 * @start: start of GPU address range
1225 * @end: end of GPU address range
1226 * @dst: destination address to map to, the next dst inside the function
1227 * @flags: mapping flags
1228 *
1229 * Update the page tables in the range @start - @end.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001230 *
1231 * Returns:
1232 * 0 for success, -EINVAL for failure.
Christian König92696dd2016-08-05 13:56:35 +02001233 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001234static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001235 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001236 uint64_t dst, uint64_t flags)
Christian König92696dd2016-08-05 13:56:35 +02001237{
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001238 struct amdgpu_device *adev = params->adev;
1239 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
Christian König92696dd2016-08-05 13:56:35 +02001240
Christian König301654a2017-05-16 14:30:27 +02001241 uint64_t addr, pe_start;
Christian König92696dd2016-08-05 13:56:35 +02001242 struct amdgpu_bo *pt;
Christian König301654a2017-05-16 14:30:27 +02001243 unsigned nptes;
Christian König92696dd2016-08-05 13:56:35 +02001244
1245 /* walk over the address space and update the page tables */
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001246 for (addr = start; addr < end; addr += nptes,
1247 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1248 struct amdgpu_vm_pt *entry, *parent;
1249
1250 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1251 if (!entry)
1252 return -ENOENT;
Christian König4e2cb642016-10-25 15:52:28 +02001253
Christian König92696dd2016-08-05 13:56:35 +02001254 if ((addr & ~mask) == (end & ~mask))
1255 nptes = end - addr;
1256 else
Zhang, Jerry36b32a62017-03-29 16:08:32 +08001257 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
Christian König92696dd2016-08-05 13:56:35 +02001258
Christian Königec5207c2017-08-03 19:24:06 +02001259 amdgpu_vm_handle_huge_pages(params, entry, parent,
1260 nptes, dst, flags);
Christian König4ab40162017-08-03 20:30:50 +02001261 /* We don't need to update PTEs for huge pages */
Christian König78eb2f02017-11-30 15:41:28 +01001262 if (entry->huge)
Alex Deuchercf2f0a32017-07-25 16:35:38 -04001263 continue;
1264
Christian König3f3333f2017-08-03 14:02:13 +02001265 pt = entry->base.bo;
Christian König373ac642018-01-16 16:54:25 +01001266 pe_start = (addr & mask) * 8;
1267 if (pt->shadow)
1268 params->func(params, pt->shadow, pe_start, dst, nptes,
1269 AMDGPU_GPU_PAGE_SIZE, flags);
1270 params->func(params, pt, pe_start, dst, nptes,
Christian König301654a2017-05-16 14:30:27 +02001271 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +02001272 }
1273
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001274 return 0;
Christian König92696dd2016-08-05 13:56:35 +02001275}
1276
1277/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1279 *
Christian König29efc4f2016-08-04 14:52:50 +02001280 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +02001281 * @vm: requested vm
1282 * @start: first PTE to handle
1283 * @end: last PTE to handle
1284 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001285 * @flags: hw mapping flags
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001286 *
1287 * Returns:
1288 * 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289 */
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001290static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +02001291 uint64_t start, uint64_t end,
Chunming Zhou6b777602016-09-21 16:19:19 +08001292 uint64_t dst, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001293{
1294 /**
1295 * The MC L1 TLB supports variable sized pages, based on a fragment
1296 * field in the PTE. When this field is set to a non-zero value, page
1297 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1298 * flags are considered valid for all PTEs within the fragment range
1299 * and corresponding mappings are assumed to be physically contiguous.
1300 *
1301 * The L1 TLB can store a single PTE for the whole fragment,
1302 * significantly increasing the space available for translation
1303 * caching. This leads to large improvements in throughput when the
1304 * TLB is under pressure.
1305 *
1306 * The L2 TLB distributes small and large fragments into two
1307 * asymmetric partitions. The large fragment cache is significantly
1308 * larger. Thus, we try to use large fragments wherever possible.
1309 * Userspace can support this by aligning virtual base address and
1310 * allocation size to the fragment size.
1311 */
Roger He6849d472017-08-30 13:01:19 +08001312 unsigned max_frag = params->adev->vm_manager.fragment_size;
1313 int r;
Christian König31f6c1f2016-01-26 12:37:49 +01001314
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 /* system pages are non continuously */
Roger He6849d472017-08-30 13:01:19 +08001316 if (params->src || !(flags & AMDGPU_PTE_VALID))
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001317 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318
Roger He6849d472017-08-30 13:01:19 +08001319 while (start != end) {
1320 uint64_t frag_flags, frag_end;
1321 unsigned frag;
1322
1323 /* This intentionally wraps around if no bit is set */
1324 frag = min((unsigned)ffs(start) - 1,
1325 (unsigned)fls64(end - start) - 1);
1326 if (frag >= max_frag) {
1327 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1328 frag_end = end & ~((1ULL << max_frag) - 1);
1329 } else {
1330 frag_flags = AMDGPU_PTE_FRAG(frag);
1331 frag_end = start + (1 << frag);
1332 }
1333
1334 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1335 flags | frag_flags);
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001336 if (r)
1337 return r;
Roger He6849d472017-08-30 13:01:19 +08001338
1339 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1340 start = frag_end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 }
1342
Roger He6849d472017-08-30 13:01:19 +08001343 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001344}
1345
1346/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001347 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1348 *
1349 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001350 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +01001351 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001352 * @vm: requested vm
1353 * @start: start of mapped range
1354 * @last: last mapped entry
1355 * @flags: flags for the entries
1356 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001357 * @fence: optional resulting fence
1358 *
Christian Königa14faa62016-01-25 14:27:31 +01001359 * Fill in the page table entries between @start and @last.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001360 *
1361 * Returns:
1362 * 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 */
1364static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001365 struct dma_fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001366 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001367 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +01001368 uint64_t start, uint64_t last,
Chunming Zhou6b777602016-09-21 16:19:19 +08001369 uint64_t flags, uint64_t addr,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001370 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371{
Christian König2d55e452016-02-08 17:37:38 +01001372 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +01001373 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001374 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +01001375 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +02001376 struct amdgpu_pte_update_params params;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001377 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 int r;
1379
Christian Königafef8b82016-08-12 13:29:18 +02001380 memset(&params, 0, sizeof(params));
1381 params.adev = adev;
Christian König49ac8a22016-10-13 15:09:08 +02001382 params.vm = vm;
Christian Königafef8b82016-08-12 13:29:18 +02001383
Christian Königa33cab72017-07-11 17:13:00 +02001384 /* sync to everything on unmapping */
1385 if (!(flags & AMDGPU_PTE_VALID))
1386 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1387
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001388 if (vm->use_cpu_for_update) {
1389 /* params.src is used as flag to indicate system Memory */
1390 if (pages_addr)
1391 params.src = ~0;
1392
1393 /* Wait for PT BOs to be free. PTs share the same resv. object
1394 * as the root PD BO
1395 */
Christian Königa33cab72017-07-11 17:13:00 +02001396 r = amdgpu_vm_wait_pd(adev, vm, owner);
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001397 if (unlikely(r))
1398 return r;
1399
1400 params.func = amdgpu_vm_cpu_set_ptes;
1401 params.pages_addr = pages_addr;
Harish Kasiviswanathanb4d42512017-05-11 19:47:22 -04001402 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1403 addr, flags);
1404 }
1405
Nayan Deshmukh068c3302018-07-20 17:51:06 +05301406 ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +02001407
Christian Königa14faa62016-01-25 14:27:31 +01001408 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001409
1410 /*
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001411 * reserve space for two commands every (1 << BLOCK_SIZE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412 * entries or 2k dwords (whatever is smaller)
Bas Nieuwenhuizen86209522017-09-07 13:23:21 +02001413 *
1414 * The second command is for the shadow pagetables.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 */
Emily Deng104bd2c2017-12-29 13:13:08 +08001416 if (vm->root.base.bo->shadow)
1417 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1418 else
1419 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420
1421 /* padding, etc. */
1422 ndw = 64;
1423
Christian König570144c2017-08-30 15:38:45 +02001424 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001425 /* copy commands needed */
Yong Zhaoe6d92192017-09-19 12:58:15 -04001426 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001427
Christian Königb0456f92016-08-11 14:06:54 +02001428 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429 ndw += nptes * 2;
1430
Christian Königafef8b82016-08-12 13:29:18 +02001431 params.func = amdgpu_vm_do_copy_ptes;
1432
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001433 } else {
1434 /* set page commands needed */
Christian König44e1bae2018-01-24 19:58:45 +01001435 ndw += ncmds * 10;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001436
Roger He6849d472017-08-30 13:01:19 +08001437 /* extra commands for begin/end fragments */
Emily Deng11528642018-06-08 16:36:22 +08001438 if (vm->root.base.bo->shadow)
1439 ndw += 2 * 10 * adev->vm_manager.fragment_size * 2;
1440 else
1441 ndw += 2 * 10 * adev->vm_manager.fragment_size;
Christian Königafef8b82016-08-12 13:29:18 +02001442
1443 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 }
1445
Christian Königd71518b2016-02-01 12:20:25 +01001446 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1447 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001448 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001449
Christian König29efc4f2016-08-04 14:52:50 +02001450 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001451
Christian König570144c2017-08-30 15:38:45 +02001452 if (pages_addr) {
Christian Königb0456f92016-08-11 14:06:54 +02001453 uint64_t *pte;
1454 unsigned i;
1455
1456 /* Put the PTEs at the end of the IB. */
1457 i = ndw - nptes * 2;
1458 pte= (uint64_t *)&(job->ibs->ptr[i]);
1459 params.src = job->ibs->gpu_addr + i * 4;
1460
1461 for (i = 0; i < nptes; ++i) {
1462 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1463 AMDGPU_GPU_PAGE_SIZE);
1464 pte[i] |= flags;
1465 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001466 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001467 }
1468
Andrey Grodzovskycebb52b2017-11-13 14:47:52 -05001469 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
Christian König3cabaa52016-06-06 10:17:58 +02001470 if (r)
1471 goto error_free;
1472
Christian König3f3333f2017-08-03 14:02:13 +02001473 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001474 owner, false);
Christian Königa1e08d32016-01-26 11:40:46 +01001475 if (r)
1476 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001477
Christian König3f3333f2017-08-03 14:02:13 +02001478 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
Christian Königa1e08d32016-01-26 11:40:46 +01001479 if (r)
1480 goto error_free;
1481
Harish Kasiviswanathancc28c4e2017-05-11 22:39:31 -04001482 r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1483 if (r)
1484 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001485
Christian König29efc4f2016-08-04 14:52:50 +02001486 amdgpu_ring_pad_ib(ring, params.ib);
1487 WARN_ON(params.ib->length_dw > ndw);
Christian König0e28b102018-07-13 13:54:56 +02001488 r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001489 if (r)
1490 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491
Christian König3f3333f2017-08-03 14:02:13 +02001492 amdgpu_bo_fence(vm->root.base.bo, f, true);
Christian König284710f2017-01-30 11:09:31 +01001493 dma_fence_put(*fence);
1494 *fence = f;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001495 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001496
1497error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001498 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001499 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001500}
1501
1502/**
Christian Königa14faa62016-01-25 14:27:31 +01001503 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1504 *
1505 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001506 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001507 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001508 * @vm: requested vm
1509 * @mapping: mapped range and flags to use for the update
Christian König8358dce2016-03-30 10:50:25 +02001510 * @flags: HW flags for the mapping
Christian König63e0ba42016-08-16 17:38:37 +02001511 * @nodes: array of drm_mm_nodes with the MC addresses
Christian Königa14faa62016-01-25 14:27:31 +01001512 * @fence: optional resulting fence
1513 *
1514 * Split the mapping into smaller chunks so that each update fits
1515 * into a SDMA IB.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001516 *
1517 * Returns:
1518 * 0 for success, -EINVAL for failure.
Christian Königa14faa62016-01-25 14:27:31 +01001519 */
1520static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001521 struct dma_fence *exclusive,
Christian König8358dce2016-03-30 10:50:25 +02001522 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001523 struct amdgpu_vm *vm,
1524 struct amdgpu_bo_va_mapping *mapping,
Chunming Zhou6b777602016-09-21 16:19:19 +08001525 uint64_t flags,
Christian König63e0ba42016-08-16 17:38:37 +02001526 struct drm_mm_node *nodes,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001527 struct dma_fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001528{
Christian König9fc8fc72017-09-18 13:58:30 +02001529 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
Christian König570144c2017-08-30 15:38:45 +02001530 uint64_t pfn, start = mapping->start;
Christian Königa14faa62016-01-25 14:27:31 +01001531 int r;
1532
1533 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1534 * but in case of something, we filter the flags in first place
1535 */
1536 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1537 flags &= ~AMDGPU_PTE_READABLE;
1538 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1539 flags &= ~AMDGPU_PTE_WRITEABLE;
1540
Alex Xie15b31c52017-03-03 16:47:11 -05001541 flags &= ~AMDGPU_PTE_EXECUTABLE;
1542 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1543
Alex Xieb0fd18b2017-03-03 16:49:39 -05001544 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1545 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1546
Zhang, Jerryd0766e92017-04-19 09:53:29 +08001547 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1548 (adev->asic_type >= CHIP_VEGA10)) {
1549 flags |= AMDGPU_PTE_PRT;
1550 flags &= ~AMDGPU_PTE_VALID;
1551 }
1552
Christian Königa14faa62016-01-25 14:27:31 +01001553 trace_amdgpu_vm_bo_update(mapping);
1554
Christian König63e0ba42016-08-16 17:38:37 +02001555 pfn = mapping->offset >> PAGE_SHIFT;
1556 if (nodes) {
1557 while (pfn >= nodes->size) {
1558 pfn -= nodes->size;
1559 ++nodes;
1560 }
Christian Königfa3ab3c2016-03-18 21:00:35 +01001561 }
Christian Königa14faa62016-01-25 14:27:31 +01001562
Christian König63e0ba42016-08-16 17:38:37 +02001563 do {
Christian König9fc8fc72017-09-18 13:58:30 +02001564 dma_addr_t *dma_addr = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001565 uint64_t max_entries;
1566 uint64_t addr, last;
Christian Königa14faa62016-01-25 14:27:31 +01001567
Christian König63e0ba42016-08-16 17:38:37 +02001568 if (nodes) {
1569 addr = nodes->start << PAGE_SHIFT;
1570 max_entries = (nodes->size - pfn) *
Michel Dänzer463d2fe2018-06-22 18:54:03 +02001571 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
Christian König63e0ba42016-08-16 17:38:37 +02001572 } else {
1573 addr = 0;
1574 max_entries = S64_MAX;
1575 }
Christian Königa14faa62016-01-25 14:27:31 +01001576
Christian König63e0ba42016-08-16 17:38:37 +02001577 if (pages_addr) {
Christian König9fc8fc72017-09-18 13:58:30 +02001578 uint64_t count;
1579
Christian König457e0fe2017-08-22 12:50:46 +02001580 max_entries = min(max_entries, 16ull * 1024ull);
Michel Dänzer38e624a2018-06-21 11:27:46 +02001581 for (count = 1;
Michel Dänzer463d2fe2018-06-22 18:54:03 +02001582 count < max_entries / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
Michel Dänzer38e624a2018-06-21 11:27:46 +02001583 ++count) {
Christian König9fc8fc72017-09-18 13:58:30 +02001584 uint64_t idx = pfn + count;
1585
1586 if (pages_addr[idx] !=
1587 (pages_addr[idx - 1] + PAGE_SIZE))
1588 break;
1589 }
1590
1591 if (count < min_linear_pages) {
1592 addr = pfn << PAGE_SHIFT;
1593 dma_addr = pages_addr;
1594 } else {
1595 addr = pages_addr[pfn];
Michel Dänzer463d2fe2018-06-22 18:54:03 +02001596 max_entries = count * AMDGPU_GPU_PAGES_IN_CPU_PAGE;
Christian König9fc8fc72017-09-18 13:58:30 +02001597 }
1598
Christian König63e0ba42016-08-16 17:38:37 +02001599 } else if (flags & AMDGPU_PTE_VALID) {
1600 addr += adev->vm_manager.vram_base_offset;
Christian König9fc8fc72017-09-18 13:58:30 +02001601 addr += pfn << PAGE_SHIFT;
Christian König63e0ba42016-08-16 17:38:37 +02001602 }
Christian König63e0ba42016-08-16 17:38:37 +02001603
Christian Königa9f87f62017-03-30 14:03:59 +02001604 last = min((uint64_t)mapping->last, start + max_entries - 1);
Christian König9fc8fc72017-09-18 13:58:30 +02001605 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001606 start, last, flags, addr,
1607 fence);
1608 if (r)
1609 return r;
1610
Michel Dänzer463d2fe2018-06-22 18:54:03 +02001611 pfn += (last - start + 1) / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
Christian König63e0ba42016-08-16 17:38:37 +02001612 if (nodes && nodes->size == pfn) {
1613 pfn = 0;
1614 ++nodes;
1615 }
Christian Königa14faa62016-01-25 14:27:31 +01001616 start = last + 1;
Christian König63e0ba42016-08-16 17:38:37 +02001617
Christian Königa9f87f62017-03-30 14:03:59 +02001618 } while (unlikely(start != mapping->last + 1));
Christian Königa14faa62016-01-25 14:27:31 +01001619
1620 return 0;
1621}
1622
1623/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001624 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1625 *
1626 * @adev: amdgpu_device pointer
1627 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001628 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001629 *
1630 * Fill in the page table entries for @bo_va.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001631 *
1632 * Returns:
1633 * 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 */
1635int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1636 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001637 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638{
Christian Königec681542017-08-01 10:51:43 +02001639 struct amdgpu_bo *bo = bo_va->base.bo;
1640 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001642 dma_addr_t *pages_addr = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001643 struct ttm_mem_reg *mem;
Christian König63e0ba42016-08-16 17:38:37 +02001644 struct drm_mm_node *nodes;
Christian König4e55eb32017-09-11 16:54:59 +02001645 struct dma_fence *exclusive, **last_update;
Christian König457e0fe2017-08-22 12:50:46 +02001646 uint64_t flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 int r;
1648
Huang Rui7eb80422018-07-04 18:08:54 +08001649 if (clear || !bo) {
Christian König99e124f2016-08-16 14:43:17 +02001650 mem = NULL;
Christian König63e0ba42016-08-16 17:38:37 +02001651 nodes = NULL;
Christian König99e124f2016-08-16 14:43:17 +02001652 exclusive = NULL;
1653 } else {
Christian König8358dce2016-03-30 10:50:25 +02001654 struct ttm_dma_tt *ttm;
1655
Huang Rui7eb80422018-07-04 18:08:54 +08001656 mem = &bo->tbo.mem;
Christian König63e0ba42016-08-16 17:38:37 +02001657 nodes = mem->mm_node;
1658 if (mem->mem_type == TTM_PL_TT) {
Huang Rui7eb80422018-07-04 18:08:54 +08001659 ttm = container_of(bo->tbo.ttm, struct ttm_dma_tt, ttm);
Christian König8358dce2016-03-30 10:50:25 +02001660 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001661 }
Christian Königec681542017-08-01 10:51:43 +02001662 exclusive = reservation_object_get_excl(bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001663 }
1664
Christian König457e0fe2017-08-22 12:50:46 +02001665 if (bo)
Christian Königec681542017-08-01 10:51:43 +02001666 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
Christian König457e0fe2017-08-22 12:50:46 +02001667 else
Christian Königa5f6b5b2017-01-30 11:01:38 +01001668 flags = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669
Christian König4e55eb32017-09-11 16:54:59 +02001670 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1671 last_update = &vm->last_update;
1672 else
1673 last_update = &bo_va->last_pt_update;
1674
Christian König3d7d4d32017-08-23 16:13:33 +02001675 if (!clear && bo_va->base.moved) {
1676 bo_va->base.moved = false;
Christian König7fc11952015-07-30 11:53:42 +02001677 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001678
Christian Königcb7b6ec2017-08-15 17:08:12 +02001679 } else if (bo_va->cleared != clear) {
1680 list_splice_init(&bo_va->valids, &bo_va->invalids);
Christian König3d7d4d32017-08-23 16:13:33 +02001681 }
Christian König7fc11952015-07-30 11:53:42 +02001682
1683 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König457e0fe2017-08-22 12:50:46 +02001684 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
Christian König63e0ba42016-08-16 17:38:37 +02001685 mapping, flags, nodes,
Christian König4e55eb32017-09-11 16:54:59 +02001686 last_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001687 if (r)
1688 return r;
1689 }
1690
Christian König68c62302017-07-11 17:23:29 +02001691 if (vm->use_cpu_for_update) {
1692 /* Flush HDP */
1693 mb();
Christian König69882562018-01-19 14:17:40 +01001694 amdgpu_asic_flush_hdp(adev, NULL);
Christian König68c62302017-07-11 17:23:29 +02001695 }
1696
Christian Königaf4c0f62018-04-19 10:56:02 +02001697 spin_lock(&vm->moved_lock);
Junwei Zhangbb475832018-04-19 13:17:26 +08001698 list_del_init(&bo_va->base.vm_status);
Christian Königaf4c0f62018-04-19 10:56:02 +02001699 spin_unlock(&vm->moved_lock);
Christian König36188362018-03-19 11:49:14 +01001700
Junwei Zhangbb475832018-04-19 13:17:26 +08001701 /* If the BO is not in its preferred location add it back to
1702 * the evicted list so that it gets validated again on the
1703 * next command submission.
1704 */
Christian König806f0432018-04-19 15:01:12 +02001705 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1706 uint32_t mem_type = bo->tbo.mem.mem_type;
1707
1708 if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type)))
1709 list_add_tail(&bo_va->base.vm_status, &vm->evicted);
1710 else
1711 list_add(&bo_va->base.vm_status, &vm->idle);
1712 }
Christian Königcb7b6ec2017-08-15 17:08:12 +02001713
1714 list_splice_init(&bo_va->invalids, &bo_va->valids);
1715 bo_va->cleared = clear;
1716
1717 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1718 list_for_each_entry(mapping, &bo_va->valids, list)
1719 trace_amdgpu_vm_bo_mapping(mapping);
1720 }
1721
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 return 0;
1723}
1724
1725/**
Christian König284710f2017-01-30 11:09:31 +01001726 * amdgpu_vm_update_prt_state - update the global PRT state
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001727 *
1728 * @adev: amdgpu_device pointer
Christian König284710f2017-01-30 11:09:31 +01001729 */
1730static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1731{
1732 unsigned long flags;
1733 bool enable;
1734
1735 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
Christian König451bc8e2017-02-14 16:02:52 +01001736 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
Christian König132f34e2018-01-12 15:26:08 +01001737 adev->gmc.gmc_funcs->set_prt(adev, enable);
Christian König284710f2017-01-30 11:09:31 +01001738 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1739}
1740
1741/**
Christian König4388fc22017-03-13 10:13:36 +01001742 * amdgpu_vm_prt_get - add a PRT user
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001743 *
1744 * @adev: amdgpu_device pointer
Christian König451bc8e2017-02-14 16:02:52 +01001745 */
1746static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1747{
Christian König132f34e2018-01-12 15:26:08 +01001748 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001749 return;
1750
Christian König451bc8e2017-02-14 16:02:52 +01001751 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1752 amdgpu_vm_update_prt_state(adev);
1753}
1754
1755/**
Christian König0b15f2f2017-02-14 15:47:03 +01001756 * amdgpu_vm_prt_put - drop a PRT user
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001757 *
1758 * @adev: amdgpu_device pointer
Christian König0b15f2f2017-02-14 15:47:03 +01001759 */
1760static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1761{
Christian König451bc8e2017-02-14 16:02:52 +01001762 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
Christian König0b15f2f2017-02-14 15:47:03 +01001763 amdgpu_vm_update_prt_state(adev);
1764}
1765
1766/**
Christian König451bc8e2017-02-14 16:02:52 +01001767 * amdgpu_vm_prt_cb - callback for updating the PRT status
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001768 *
1769 * @fence: fence for the callback
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -04001770 * @_cb: the callback function
Christian König284710f2017-01-30 11:09:31 +01001771 */
1772static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1773{
1774 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1775
Christian König0b15f2f2017-02-14 15:47:03 +01001776 amdgpu_vm_prt_put(cb->adev);
Christian König284710f2017-01-30 11:09:31 +01001777 kfree(cb);
1778}
1779
1780/**
Christian König451bc8e2017-02-14 16:02:52 +01001781 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001782 *
1783 * @adev: amdgpu_device pointer
1784 * @fence: fence for the callback
Christian König451bc8e2017-02-14 16:02:52 +01001785 */
1786static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1787 struct dma_fence *fence)
1788{
Christian König4388fc22017-03-13 10:13:36 +01001789 struct amdgpu_prt_cb *cb;
Christian König451bc8e2017-02-14 16:02:52 +01001790
Christian König132f34e2018-01-12 15:26:08 +01001791 if (!adev->gmc.gmc_funcs->set_prt)
Christian König4388fc22017-03-13 10:13:36 +01001792 return;
1793
1794 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
Christian König451bc8e2017-02-14 16:02:52 +01001795 if (!cb) {
1796 /* Last resort when we are OOM */
1797 if (fence)
1798 dma_fence_wait(fence, false);
1799
Dan Carpenter486a68f2017-04-03 21:41:39 +03001800 amdgpu_vm_prt_put(adev);
Christian König451bc8e2017-02-14 16:02:52 +01001801 } else {
1802 cb->adev = adev;
1803 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1804 amdgpu_vm_prt_cb))
1805 amdgpu_vm_prt_cb(fence, &cb->cb);
1806 }
1807}
1808
1809/**
Christian König284710f2017-01-30 11:09:31 +01001810 * amdgpu_vm_free_mapping - free a mapping
1811 *
1812 * @adev: amdgpu_device pointer
1813 * @vm: requested vm
1814 * @mapping: mapping to be freed
1815 * @fence: fence of the unmap operation
1816 *
1817 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1818 */
1819static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1820 struct amdgpu_vm *vm,
1821 struct amdgpu_bo_va_mapping *mapping,
1822 struct dma_fence *fence)
1823{
Christian König451bc8e2017-02-14 16:02:52 +01001824 if (mapping->flags & AMDGPU_PTE_PRT)
1825 amdgpu_vm_add_prt_cb(adev, fence);
Christian König284710f2017-01-30 11:09:31 +01001826 kfree(mapping);
1827}
1828
1829/**
Christian König451bc8e2017-02-14 16:02:52 +01001830 * amdgpu_vm_prt_fini - finish all prt mappings
1831 *
1832 * @adev: amdgpu_device pointer
1833 * @vm: requested vm
1834 *
1835 * Register a cleanup callback to disable PRT support after VM dies.
1836 */
1837static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1838{
Christian König3f3333f2017-08-03 14:02:13 +02001839 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
Christian König451bc8e2017-02-14 16:02:52 +01001840 struct dma_fence *excl, **shared;
1841 unsigned i, shared_count;
1842 int r;
1843
1844 r = reservation_object_get_fences_rcu(resv, &excl,
1845 &shared_count, &shared);
1846 if (r) {
1847 /* Not enough memory to grab the fence list, as last resort
1848 * block for all the fences to complete.
1849 */
1850 reservation_object_wait_timeout_rcu(resv, true, false,
1851 MAX_SCHEDULE_TIMEOUT);
1852 return;
1853 }
1854
1855 /* Add a callback for each fence in the reservation object */
1856 amdgpu_vm_prt_get(adev);
1857 amdgpu_vm_add_prt_cb(adev, excl);
1858
1859 for (i = 0; i < shared_count; ++i) {
1860 amdgpu_vm_prt_get(adev);
1861 amdgpu_vm_add_prt_cb(adev, shared[i]);
1862 }
1863
1864 kfree(shared);
1865}
1866
1867/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1869 *
1870 * @adev: amdgpu_device pointer
1871 * @vm: requested vm
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001872 * @fence: optional resulting fence (unchanged if no work needed to be done
1873 * or if an error occurred)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001874 *
1875 * Make sure all freed BOs are cleared in the PT.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001876 * PTs have to be reserved and mutex must be locked!
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001877 *
1878 * Returns:
1879 * 0 for success.
1880 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001881 */
1882int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001883 struct amdgpu_vm *vm,
1884 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001885{
1886 struct amdgpu_bo_va_mapping *mapping;
Christian König45843122018-01-25 18:36:15 +01001887 uint64_t init_pte_value = 0;
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001888 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 int r;
1890
1891 while (!list_empty(&vm->freed)) {
1892 mapping = list_first_entry(&vm->freed,
1893 struct amdgpu_bo_va_mapping, list);
1894 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001895
Christian König45843122018-01-25 18:36:15 +01001896 if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
Yong Zhao6d16dac2017-08-31 15:55:00 -04001897 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001898
Christian König570144c2017-08-30 15:38:45 +02001899 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
Christian Königfc6aa332017-04-19 14:41:19 +02001900 mapping->start, mapping->last,
Yong Zhao51ac7ee2017-07-27 12:48:22 -04001901 init_pte_value, 0, &f);
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001902 amdgpu_vm_free_mapping(adev, vm, mapping, f);
Christian König284710f2017-01-30 11:09:31 +01001903 if (r) {
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001904 dma_fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905 return r;
Christian König284710f2017-01-30 11:09:31 +01001906 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001907 }
Nicolai Hähnlef3467812017-03-23 19:36:31 +01001908
1909 if (fence && f) {
1910 dma_fence_put(*fence);
1911 *fence = f;
1912 } else {
1913 dma_fence_put(f);
1914 }
1915
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001916 return 0;
1917
1918}
1919
1920/**
Christian König73fb16e2017-08-16 11:13:48 +02001921 * amdgpu_vm_handle_moved - handle moved BOs in the PT
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001922 *
1923 * @adev: amdgpu_device pointer
1924 * @vm: requested vm
1925 *
Christian König73fb16e2017-08-16 11:13:48 +02001926 * Make sure all BOs which are moved are updated in the PTs.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001927 *
1928 * Returns:
1929 * 0 for success.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001930 *
Christian König73fb16e2017-08-16 11:13:48 +02001931 * PTs have to be reserved!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001932 */
Christian König73fb16e2017-08-16 11:13:48 +02001933int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +02001934 struct amdgpu_vm *vm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935{
Christian König789f3312018-04-19 11:08:24 +02001936 struct amdgpu_bo_va *bo_va, *tmp;
1937 struct list_head moved;
Christian König73fb16e2017-08-16 11:13:48 +02001938 bool clear;
Christian König789f3312018-04-19 11:08:24 +02001939 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940
Christian König789f3312018-04-19 11:08:24 +02001941 INIT_LIST_HEAD(&moved);
Christian Königaf4c0f62018-04-19 10:56:02 +02001942 spin_lock(&vm->moved_lock);
Christian König789f3312018-04-19 11:08:24 +02001943 list_splice_init(&vm->moved, &moved);
1944 spin_unlock(&vm->moved_lock);
Christian König4e55eb32017-09-11 16:54:59 +02001945
Christian König789f3312018-04-19 11:08:24 +02001946 list_for_each_entry_safe(bo_va, tmp, &moved, base.vm_status) {
1947 struct reservation_object *resv = bo_va->base.bo->tbo.resv;
Christian Königec363e02017-09-01 20:34:27 +02001948
Christian König73fb16e2017-08-16 11:13:48 +02001949 /* Per VM BOs never need to bo cleared in the page tables */
Christian Königec363e02017-09-01 20:34:27 +02001950 if (resv == vm->root.base.bo->tbo.resv)
1951 clear = false;
1952 /* Try to reserve the BO to avoid clearing its ptes */
Christian König9b8cad22018-01-03 13:36:22 +01001953 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
Christian Königec363e02017-09-01 20:34:27 +02001954 clear = false;
1955 /* Somebody else is using the BO right now */
1956 else
1957 clear = true;
Christian König73fb16e2017-08-16 11:13:48 +02001958
1959 r = amdgpu_vm_bo_update(adev, bo_va, clear);
Christian König789f3312018-04-19 11:08:24 +02001960 if (r) {
1961 spin_lock(&vm->moved_lock);
1962 list_splice(&moved, &vm->moved);
1963 spin_unlock(&vm->moved_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001964 return r;
Christian König789f3312018-04-19 11:08:24 +02001965 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001966
Christian Königec363e02017-09-01 20:34:27 +02001967 if (!clear && resv != vm->root.base.bo->tbo.resv)
1968 reservation_object_unlock(resv);
1969
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001970 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001971
Christian König789f3312018-04-19 11:08:24 +02001972 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001973}
1974
1975/**
1976 * amdgpu_vm_bo_add - add a bo to a specific vm
1977 *
1978 * @adev: amdgpu_device pointer
1979 * @vm: requested vm
1980 * @bo: amdgpu buffer object
1981 *
Christian König8843dbb2016-01-26 12:17:11 +01001982 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001983 * Add @bo to the list of bos associated with the vm
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04001984 *
1985 * Returns:
1986 * Newly added bo_va or NULL for failure
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001987 *
1988 * Object has to be reserved!
1989 */
1990struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1991 struct amdgpu_vm *vm,
1992 struct amdgpu_bo *bo)
1993{
1994 struct amdgpu_bo_va *bo_va;
1995
1996 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1997 if (bo_va == NULL) {
1998 return NULL;
1999 }
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002000 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
Christian Königec681542017-08-01 10:51:43 +02002001
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002002 bo_va->ref_count = 1;
Christian König7fc11952015-07-30 11:53:42 +02002003 INIT_LIST_HEAD(&bo_va->valids);
2004 INIT_LIST_HEAD(&bo_va->invalids);
Christian König32b41ac2016-03-08 18:03:27 +01002005
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006 return bo_va;
2007}
2008
Christian König73fb16e2017-08-16 11:13:48 +02002009
2010/**
2011 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2012 *
2013 * @adev: amdgpu_device pointer
2014 * @bo_va: bo_va to store the address
2015 * @mapping: the mapping to insert
2016 *
2017 * Insert a new mapping into all structures.
2018 */
2019static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2020 struct amdgpu_bo_va *bo_va,
2021 struct amdgpu_bo_va_mapping *mapping)
2022{
2023 struct amdgpu_vm *vm = bo_va->base.vm;
2024 struct amdgpu_bo *bo = bo_va->base.bo;
2025
Christian Königaebc5e62017-09-06 16:55:16 +02002026 mapping->bo_va = bo_va;
Christian König73fb16e2017-08-16 11:13:48 +02002027 list_add(&mapping->list, &bo_va->invalids);
2028 amdgpu_vm_it_insert(mapping, &vm->va);
2029
2030 if (mapping->flags & AMDGPU_PTE_PRT)
2031 amdgpu_vm_prt_get(adev);
2032
Christian König862b8c52018-04-19 14:22:56 +02002033 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
2034 !bo_va->base.moved) {
Christian Königaf4c0f62018-04-19 10:56:02 +02002035 spin_lock(&vm->moved_lock);
Christian König862b8c52018-04-19 14:22:56 +02002036 list_move(&bo_va->base.vm_status, &vm->moved);
Christian Königaf4c0f62018-04-19 10:56:02 +02002037 spin_unlock(&vm->moved_lock);
Christian König73fb16e2017-08-16 11:13:48 +02002038 }
2039 trace_amdgpu_vm_bo_map(bo_va, mapping);
2040}
2041
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042/**
2043 * amdgpu_vm_bo_map - map bo inside a vm
2044 *
2045 * @adev: amdgpu_device pointer
2046 * @bo_va: bo_va to store the address
2047 * @saddr: where to map the BO
2048 * @offset: requested offset in the BO
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -04002049 * @size: BO size in bytes
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050 * @flags: attributes of pages (read/write/valid/etc.)
2051 *
2052 * Add a mapping of the BO at the specefied addr into the VM.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002053 *
2054 * Returns:
2055 * 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002056 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002057 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002058 */
2059int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2060 struct amdgpu_bo_va *bo_va,
2061 uint64_t saddr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +01002062 uint64_t size, uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063{
Christian Königa9f87f62017-03-30 14:03:59 +02002064 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian Königec681542017-08-01 10:51:43 +02002065 struct amdgpu_bo *bo = bo_va->base.bo;
2066 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002067 uint64_t eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068
Christian König0be52de2015-05-18 14:37:27 +02002069 /* validate the parameters */
2070 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08002071 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02002072 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02002073
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002074 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05002075 eaddr = saddr + size - 1;
Christian Königa5f6b5b2017-01-30 11:01:38 +01002076 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002077 (bo && offset + size > amdgpu_bo_size(bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002078 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080 saddr /= AMDGPU_GPU_PAGE_SIZE;
2081 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2082
Christian Königa9f87f62017-03-30 14:03:59 +02002083 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2084 if (tmp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085 /* bo and tmp overlap, invalid addr */
2086 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Christian Königec681542017-08-01 10:51:43 +02002087 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
Christian Königa9f87f62017-03-30 14:03:59 +02002088 tmp->start, tmp->last + 1);
Christian König663e4572017-03-13 10:13:37 +01002089 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090 }
2091
2092 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
Christian König663e4572017-03-13 10:13:37 +01002093 if (!mapping)
2094 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002095
Christian Königa9f87f62017-03-30 14:03:59 +02002096 mapping->start = saddr;
2097 mapping->last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002098 mapping->offset = offset;
2099 mapping->flags = flags;
2100
Christian König73fb16e2017-08-16 11:13:48 +02002101 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König4388fc22017-03-13 10:13:36 +01002102
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002103 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002104}
2105
2106/**
Christian König80f95c52017-03-13 10:13:39 +01002107 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2108 *
2109 * @adev: amdgpu_device pointer
2110 * @bo_va: bo_va to store the address
2111 * @saddr: where to map the BO
2112 * @offset: requested offset in the BO
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -04002113 * @size: BO size in bytes
Christian König80f95c52017-03-13 10:13:39 +01002114 * @flags: attributes of pages (read/write/valid/etc.)
2115 *
2116 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2117 * mappings as we do so.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002118 *
2119 * Returns:
2120 * 0 for success, error for failure.
Christian König80f95c52017-03-13 10:13:39 +01002121 *
2122 * Object has to be reserved and unreserved outside!
2123 */
2124int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2125 struct amdgpu_bo_va *bo_va,
2126 uint64_t saddr, uint64_t offset,
2127 uint64_t size, uint64_t flags)
2128{
2129 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002130 struct amdgpu_bo *bo = bo_va->base.bo;
Christian König80f95c52017-03-13 10:13:39 +01002131 uint64_t eaddr;
2132 int r;
2133
2134 /* validate the parameters */
2135 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2136 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2137 return -EINVAL;
2138
2139 /* make sure object fit at this offset */
2140 eaddr = saddr + size - 1;
2141 if (saddr >= eaddr ||
Christian Königec681542017-08-01 10:51:43 +02002142 (bo && offset + size > amdgpu_bo_size(bo)))
Christian König80f95c52017-03-13 10:13:39 +01002143 return -EINVAL;
2144
2145 /* Allocate all the needed memory */
2146 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2147 if (!mapping)
2148 return -ENOMEM;
2149
Christian Königec681542017-08-01 10:51:43 +02002150 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
Christian König80f95c52017-03-13 10:13:39 +01002151 if (r) {
2152 kfree(mapping);
2153 return r;
2154 }
2155
2156 saddr /= AMDGPU_GPU_PAGE_SIZE;
2157 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2158
Christian Königa9f87f62017-03-30 14:03:59 +02002159 mapping->start = saddr;
2160 mapping->last = eaddr;
Christian König80f95c52017-03-13 10:13:39 +01002161 mapping->offset = offset;
2162 mapping->flags = flags;
2163
Christian König73fb16e2017-08-16 11:13:48 +02002164 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
Christian König80f95c52017-03-13 10:13:39 +01002165
2166 return 0;
2167}
2168
2169/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002170 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2171 *
2172 * @adev: amdgpu_device pointer
2173 * @bo_va: bo_va to remove the address from
2174 * @saddr: where to the BO is mapped
2175 *
2176 * Remove a mapping of the BO at the specefied addr from the VM.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002177 *
2178 * Returns:
2179 * 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08002181 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002182 */
2183int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2184 struct amdgpu_bo_va *bo_va,
2185 uint64_t saddr)
2186{
2187 struct amdgpu_bo_va_mapping *mapping;
Christian Königec681542017-08-01 10:51:43 +02002188 struct amdgpu_vm *vm = bo_va->base.vm;
Christian König7fc11952015-07-30 11:53:42 +02002189 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002190
Christian König6c7fc502015-06-05 20:56:17 +02002191 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01002192
Christian König7fc11952015-07-30 11:53:42 +02002193 list_for_each_entry(mapping, &bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002194 if (mapping->start == saddr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002195 break;
2196 }
2197
Christian König7fc11952015-07-30 11:53:42 +02002198 if (&mapping->list == &bo_va->valids) {
2199 valid = false;
2200
2201 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002202 if (mapping->start == saddr)
Christian König7fc11952015-07-30 11:53:42 +02002203 break;
2204 }
2205
Christian König32b41ac2016-03-08 18:03:27 +01002206 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02002207 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208 }
Christian König32b41ac2016-03-08 18:03:27 +01002209
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002210 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002211 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002212 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002213 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214
Christian Könige17841b2016-03-08 17:52:01 +01002215 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002216 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01002217 else
Christian König284710f2017-01-30 11:09:31 +01002218 amdgpu_vm_free_mapping(adev, vm, mapping,
2219 bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002220
2221 return 0;
2222}
2223
2224/**
Christian Königdc54d3d2017-03-13 10:13:38 +01002225 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2226 *
2227 * @adev: amdgpu_device pointer
2228 * @vm: VM structure to use
2229 * @saddr: start of the range
2230 * @size: size of the range
2231 *
2232 * Remove all mappings in a range, split them as appropriate.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002233 *
2234 * Returns:
2235 * 0 for success, error for failure.
Christian Königdc54d3d2017-03-13 10:13:38 +01002236 */
2237int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2238 struct amdgpu_vm *vm,
2239 uint64_t saddr, uint64_t size)
2240{
2241 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
Christian Königdc54d3d2017-03-13 10:13:38 +01002242 LIST_HEAD(removed);
2243 uint64_t eaddr;
2244
2245 eaddr = saddr + size - 1;
2246 saddr /= AMDGPU_GPU_PAGE_SIZE;
2247 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2248
2249 /* Allocate all the needed memory */
2250 before = kzalloc(sizeof(*before), GFP_KERNEL);
2251 if (!before)
2252 return -ENOMEM;
Junwei Zhang27f6d612017-03-16 16:09:24 +08002253 INIT_LIST_HEAD(&before->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002254
2255 after = kzalloc(sizeof(*after), GFP_KERNEL);
2256 if (!after) {
2257 kfree(before);
2258 return -ENOMEM;
2259 }
Junwei Zhang27f6d612017-03-16 16:09:24 +08002260 INIT_LIST_HEAD(&after->list);
Christian Königdc54d3d2017-03-13 10:13:38 +01002261
2262 /* Now gather all removed mappings */
Christian Königa9f87f62017-03-30 14:03:59 +02002263 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2264 while (tmp) {
Christian Königdc54d3d2017-03-13 10:13:38 +01002265 /* Remember mapping split at the start */
Christian Königa9f87f62017-03-30 14:03:59 +02002266 if (tmp->start < saddr) {
2267 before->start = tmp->start;
2268 before->last = saddr - 1;
Christian Königdc54d3d2017-03-13 10:13:38 +01002269 before->offset = tmp->offset;
2270 before->flags = tmp->flags;
Junwei Zhang387f49e2018-06-05 17:31:51 +08002271 before->bo_va = tmp->bo_va;
2272 list_add(&before->list, &tmp->bo_va->invalids);
Christian Königdc54d3d2017-03-13 10:13:38 +01002273 }
2274
2275 /* Remember mapping split at the end */
Christian Königa9f87f62017-03-30 14:03:59 +02002276 if (tmp->last > eaddr) {
2277 after->start = eaddr + 1;
2278 after->last = tmp->last;
Christian Königdc54d3d2017-03-13 10:13:38 +01002279 after->offset = tmp->offset;
Christian Königa9f87f62017-03-30 14:03:59 +02002280 after->offset += after->start - tmp->start;
Christian Königdc54d3d2017-03-13 10:13:38 +01002281 after->flags = tmp->flags;
Junwei Zhang387f49e2018-06-05 17:31:51 +08002282 after->bo_va = tmp->bo_va;
2283 list_add(&after->list, &tmp->bo_va->invalids);
Christian Königdc54d3d2017-03-13 10:13:38 +01002284 }
2285
2286 list_del(&tmp->list);
2287 list_add(&tmp->list, &removed);
Christian Königa9f87f62017-03-30 14:03:59 +02002288
2289 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
Christian Königdc54d3d2017-03-13 10:13:38 +01002290 }
2291
2292 /* And free them up */
2293 list_for_each_entry_safe(tmp, next, &removed, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02002294 amdgpu_vm_it_remove(tmp, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002295 list_del(&tmp->list);
2296
Christian Königa9f87f62017-03-30 14:03:59 +02002297 if (tmp->start < saddr)
2298 tmp->start = saddr;
2299 if (tmp->last > eaddr)
2300 tmp->last = eaddr;
Christian Königdc54d3d2017-03-13 10:13:38 +01002301
Christian Königaebc5e62017-09-06 16:55:16 +02002302 tmp->bo_va = NULL;
Christian Königdc54d3d2017-03-13 10:13:38 +01002303 list_add(&tmp->list, &vm->freed);
2304 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2305 }
2306
Junwei Zhang27f6d612017-03-16 16:09:24 +08002307 /* Insert partial mapping before the range */
2308 if (!list_empty(&before->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002309 amdgpu_vm_it_insert(before, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002310 if (before->flags & AMDGPU_PTE_PRT)
2311 amdgpu_vm_prt_get(adev);
2312 } else {
2313 kfree(before);
2314 }
2315
2316 /* Insert partial mapping after the range */
Junwei Zhang27f6d612017-03-16 16:09:24 +08002317 if (!list_empty(&after->list)) {
Christian Königa9f87f62017-03-30 14:03:59 +02002318 amdgpu_vm_it_insert(after, &vm->va);
Christian Königdc54d3d2017-03-13 10:13:38 +01002319 if (after->flags & AMDGPU_PTE_PRT)
2320 amdgpu_vm_prt_get(adev);
2321 } else {
2322 kfree(after);
2323 }
2324
2325 return 0;
2326}
2327
2328/**
Christian Königaebc5e62017-09-06 16:55:16 +02002329 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2330 *
2331 * @vm: the requested VM
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -04002332 * @addr: the address
Christian Königaebc5e62017-09-06 16:55:16 +02002333 *
2334 * Find a mapping by it's address.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002335 *
2336 * Returns:
2337 * The amdgpu_bo_va_mapping matching for addr or NULL
2338 *
Christian Königaebc5e62017-09-06 16:55:16 +02002339 */
2340struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2341 uint64_t addr)
2342{
2343 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2344}
2345
2346/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002347 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2348 *
2349 * @adev: amdgpu_device pointer
2350 * @bo_va: requested bo_va
2351 *
Christian König8843dbb2016-01-26 12:17:11 +01002352 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002353 *
2354 * Object have to be reserved!
2355 */
2356void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2357 struct amdgpu_bo_va *bo_va)
2358{
2359 struct amdgpu_bo_va_mapping *mapping, *next;
Christian Königec681542017-08-01 10:51:43 +02002360 struct amdgpu_vm *vm = bo_va->base.vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002361
Christian Königec681542017-08-01 10:51:43 +02002362 list_del(&bo_va->base.bo_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002363
Christian Königaf4c0f62018-04-19 10:56:02 +02002364 spin_lock(&vm->moved_lock);
Christian Königec681542017-08-01 10:51:43 +02002365 list_del(&bo_va->base.vm_status);
Christian Königaf4c0f62018-04-19 10:56:02 +02002366 spin_unlock(&vm->moved_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002367
Christian König7fc11952015-07-30 11:53:42 +02002368 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002369 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002370 amdgpu_vm_it_remove(mapping, &vm->va);
Christian Königaebc5e62017-09-06 16:55:16 +02002371 mapping->bo_va = NULL;
Christian König93e3e432015-06-09 16:58:33 +02002372 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02002373 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002374 }
Christian König7fc11952015-07-30 11:53:42 +02002375 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2376 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002377 amdgpu_vm_it_remove(mapping, &vm->va);
Christian König284710f2017-01-30 11:09:31 +01002378 amdgpu_vm_free_mapping(adev, vm, mapping,
2379 bo_va->last_pt_update);
Christian König7fc11952015-07-30 11:53:42 +02002380 }
Christian König32b41ac2016-03-08 18:03:27 +01002381
Chris Wilsonf54d1862016-10-25 13:00:45 +01002382 dma_fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002383 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384}
2385
2386/**
2387 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2388 *
2389 * @adev: amdgpu_device pointer
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002390 * @bo: amdgpu buffer object
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -04002391 * @evicted: is the BO evicted
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 *
Christian König8843dbb2016-01-26 12:17:11 +01002393 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002394 */
2395void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +02002396 struct amdgpu_bo *bo, bool evicted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002397{
Christian Königec681542017-08-01 10:51:43 +02002398 struct amdgpu_vm_bo_base *bo_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399
Chunming Zhou4bebcce2018-04-24 13:54:10 +08002400 /* shadow bo doesn't have bo base, its validation needs its parent */
2401 if (bo->parent && bo->parent->shadow == bo)
2402 bo = bo->parent;
2403
Christian Königec681542017-08-01 10:51:43 +02002404 list_for_each_entry(bo_base, &bo->va, bo_list) {
Christian König3f3333f2017-08-03 14:02:13 +02002405 struct amdgpu_vm *vm = bo_base->vm;
Christian König862b8c52018-04-19 14:22:56 +02002406 bool was_moved = bo_base->moved;
Christian König3f3333f2017-08-03 14:02:13 +02002407
Christian König3d7d4d32017-08-23 16:13:33 +02002408 bo_base->moved = true;
Christian König3f3333f2017-08-03 14:02:13 +02002409 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
Christian König73fb16e2017-08-16 11:13:48 +02002410 if (bo->tbo.type == ttm_bo_type_kernel)
2411 list_move(&bo_base->vm_status, &vm->evicted);
2412 else
2413 list_move_tail(&bo_base->vm_status,
2414 &vm->evicted);
Christian König3f3333f2017-08-03 14:02:13 +02002415 continue;
2416 }
2417
Christian König862b8c52018-04-19 14:22:56 +02002418 if (was_moved)
Christian König3f3333f2017-08-03 14:02:13 +02002419 continue;
2420
Christian König862b8c52018-04-19 14:22:56 +02002421 if (bo->tbo.type == ttm_bo_type_kernel) {
2422 list_move(&bo_base->vm_status, &vm->relocated);
2423 } else {
2424 spin_lock(&bo_base->vm->moved_lock);
2425 list_move(&bo_base->vm_status, &vm->moved);
2426 spin_unlock(&bo_base->vm->moved_lock);
2427 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002428 }
2429}
2430
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002431/**
2432 * amdgpu_vm_get_block_size - calculate VM page table size as power of two
2433 *
2434 * @vm_size: VM size
2435 *
2436 * Returns:
2437 * VM page table as power of two
2438 */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002439static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2440{
2441 /* Total bits covered by PD + PTs */
2442 unsigned bits = ilog2(vm_size) + 18;
2443
2444 /* Make sure the PD is 4K in size up to 8GB address space.
2445 Above that split equal between PD and PTs */
2446 if (vm_size <= 8)
2447 return (bits - 9);
2448 else
2449 return ((bits + 3) / 2);
2450}
2451
2452/**
Roger Hed07f14b2017-08-15 16:05:59 +08002453 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002454 *
2455 * @adev: amdgpu_device pointer
2456 * @vm_size: the default vm size if it's set auto
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -04002457 * @fragment_size_default: Default PTE fragment size
2458 * @max_level: max VMPT level
2459 * @max_bits: max address space size in bits
2460 *
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002461 */
Christian Königfdd5faa2017-11-04 16:51:44 +01002462void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +01002463 uint32_t fragment_size_default, unsigned max_level,
2464 unsigned max_bits)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002465{
Christian König36539dc2017-11-23 11:16:05 +01002466 uint64_t tmp;
2467
2468 /* adjust vm size first */
Christian Königf3368122017-11-23 12:57:18 +01002469 if (amdgpu_vm_size != -1) {
2470 unsigned max_size = 1 << (max_bits - 30);
2471
Christian Königfdd5faa2017-11-04 16:51:44 +01002472 vm_size = amdgpu_vm_size;
Christian Königf3368122017-11-23 12:57:18 +01002473 if (vm_size > max_size) {
2474 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2475 amdgpu_vm_size, max_size);
2476 vm_size = max_size;
2477 }
2478 }
Christian Königfdd5faa2017-11-04 16:51:44 +01002479
2480 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
Christian König36539dc2017-11-23 11:16:05 +01002481
2482 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
Christian König97489122017-11-27 16:22:05 +01002483 if (amdgpu_vm_block_size != -1)
2484 tmp >>= amdgpu_vm_block_size - 9;
Christian König36539dc2017-11-23 11:16:05 +01002485 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2486 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
Chunming Zhou196f7482017-12-13 14:22:54 +08002487 switch (adev->vm_manager.num_level) {
2488 case 3:
2489 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2490 break;
2491 case 2:
2492 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2493 break;
2494 case 1:
2495 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2496 break;
2497 default:
2498 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2499 }
Christian Königb38f41e2017-11-22 17:00:35 +01002500 /* block size depends on vm size and hw setup*/
Christian König97489122017-11-27 16:22:05 +01002501 if (amdgpu_vm_block_size != -1)
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002502 adev->vm_manager.block_size =
Christian König97489122017-11-27 16:22:05 +01002503 min((unsigned)amdgpu_vm_block_size, max_bits
2504 - AMDGPU_GPU_PAGE_SHIFT
2505 - 9 * adev->vm_manager.num_level);
2506 else if (adev->vm_manager.num_level > 1)
2507 adev->vm_manager.block_size = 9;
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002508 else
Christian König97489122017-11-27 16:22:05 +01002509 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002510
Christian Königb38f41e2017-11-22 17:00:35 +01002511 if (amdgpu_vm_fragment_size == -1)
2512 adev->vm_manager.fragment_size = fragment_size_default;
2513 else
2514 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
Roger Hed07f14b2017-08-15 16:05:59 +08002515
Christian König36539dc2017-11-23 11:16:05 +01002516 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2517 vm_size, adev->vm_manager.num_level + 1,
2518 adev->vm_manager.block_size,
Christian Königfdd5faa2017-11-04 16:51:44 +01002519 adev->vm_manager.fragment_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08002520}
2521
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002522/**
2523 * amdgpu_vm_init - initialize a vm instance
2524 *
2525 * @adev: amdgpu_device pointer
2526 * @vm: requested vm
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002527 * @vm_context: Indicates if it GFX or Compute context
Andrey Grodzovsky00553cf2018-06-13 16:01:38 -04002528 * @pasid: Process address space identifier
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002529 *
Christian König8843dbb2016-01-26 12:17:11 +01002530 * Init @vm fields.
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002531 *
2532 * Returns:
2533 * 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002534 */
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002535int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -04002536 int vm_context, unsigned int pasid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002537{
Chunming Zhou3216c6b2018-04-16 18:27:50 +08002538 struct amdgpu_bo_param bp;
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002539 struct amdgpu_bo *root;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
Zhang, Jerry36b32a62017-03-29 16:08:32 +08002541 AMDGPU_VM_PTE_COUNT(adev) * 8);
Christian König2d55e452016-02-08 17:37:38 +01002542 unsigned ring_instance;
2543 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002544 struct drm_sched_rq *rq;
Christian Königd3aab672018-01-24 14:57:02 +01002545 unsigned long size;
Christian König13307f72018-01-24 17:19:04 +01002546 uint64_t flags;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002547 int r, i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002548
Davidlohr Buesof808c132017-09-08 16:15:08 -07002549 vm->va = RB_ROOT_CACHED;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +08002550 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2551 vm->reserved_vmid[i] = NULL;
Christian König3f3333f2017-08-03 14:02:13 +02002552 INIT_LIST_HEAD(&vm->evicted);
Christian Königea097292017-08-09 14:15:46 +02002553 INIT_LIST_HEAD(&vm->relocated);
Christian Königaf4c0f62018-04-19 10:56:02 +02002554 spin_lock_init(&vm->moved_lock);
Christian König27c7b9a2017-08-01 11:27:36 +02002555 INIT_LIST_HEAD(&vm->moved);
Christian König806f0432018-04-19 15:01:12 +02002556 INIT_LIST_HEAD(&vm->idle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002557 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01002558
Christian König2bd9ccf2016-02-01 12:53:58 +01002559 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01002560
2561 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2562 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2563 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002564 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
Nayan Deshmukhaa16b6c2018-07-13 15:21:14 +05302565 r = drm_sched_entity_init(&vm->entity, &rq, 1, NULL);
Christian König2bd9ccf2016-02-01 12:53:58 +01002566 if (r)
Christian Königf566ceb2016-10-27 20:04:38 +02002567 return r;
Christian König2bd9ccf2016-02-01 12:53:58 +01002568
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002569 vm->pte_support_ats = false;
2570
2571 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002572 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2573 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002574
Christian König13307f72018-01-24 17:19:04 +01002575 if (adev->asic_type == CHIP_RAVEN)
Yong Zhao51ac7ee2017-07-27 12:48:22 -04002576 vm->pte_support_ats = true;
Christian König13307f72018-01-24 17:19:04 +01002577 } else {
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002578 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2579 AMDGPU_VM_USE_CPU_FOR_GFX);
Christian König13307f72018-01-24 17:19:04 +01002580 }
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002581 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2582 vm->use_cpu_for_update ? "CPU" : "SDMA");
Andrey Grodzovskyc8c5e562018-06-12 14:28:20 -04002583 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_gmc_vram_full_visible(&adev->gmc)),
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002584 "CPU update of VM recommended only for large BAR system\n");
Christian Königd5884512017-09-08 14:09:41 +02002585 vm->last_update = NULL;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02002586
Christian König13307f72018-01-24 17:19:04 +01002587 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002588 if (vm->use_cpu_for_update)
2589 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2590 else
Felix Kuehling810955b2018-03-23 15:30:35 -04002591 flags |= AMDGPU_GEM_CREATE_SHADOW;
Harish Kasiviswanathan3c824172017-05-11 15:50:08 -04002592
Christian Königd3aab672018-01-24 14:57:02 +01002593 size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
Chunming Zhou3216c6b2018-04-16 18:27:50 +08002594 memset(&bp, 0, sizeof(bp));
2595 bp.size = size;
2596 bp.byte_align = align;
2597 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
2598 bp.flags = flags;
2599 bp.type = ttm_bo_type_kernel;
2600 bp.resv = NULL;
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002601 r = amdgpu_bo_create(adev, &bp, &root);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002602 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01002603 goto error_free_sched_entity;
2604
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002605 r = amdgpu_bo_reserve(root, true);
Christian Königd3aab672018-01-24 14:57:02 +01002606 if (r)
2607 goto error_free_root;
2608
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002609 r = amdgpu_vm_clear_bo(adev, vm, root,
Christian König45843122018-01-25 18:36:15 +01002610 adev->vm_manager.root_level,
2611 vm->pte_support_ats);
Christian König13307f72018-01-24 17:19:04 +01002612 if (r)
2613 goto error_unreserve;
2614
Chunming Zhou3f4299b2018-04-24 12:14:39 +08002615 amdgpu_vm_bo_base_init(&vm->root.base, vm, root);
Christian Königd3aab672018-01-24 14:57:02 +01002616 amdgpu_bo_unreserve(vm->root.base.bo);
Christian König0a096fb2017-07-12 10:01:48 +02002617
Felix Kuehling02208442017-08-25 20:40:26 -04002618 if (pasid) {
2619 unsigned long flags;
2620
2621 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2622 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2623 GFP_ATOMIC);
2624 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2625 if (r < 0)
2626 goto error_free_root;
2627
2628 vm->pasid = pasid;
2629 }
2630
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002631 INIT_KFIFO(vm->faults);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002632 vm->fault_credit = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002633
2634 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01002635
Christian König13307f72018-01-24 17:19:04 +01002636error_unreserve:
2637 amdgpu_bo_unreserve(vm->root.base.bo);
2638
Christian König67003a12016-10-12 14:46:26 +02002639error_free_root:
Christian König3f3333f2017-08-03 14:02:13 +02002640 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2641 amdgpu_bo_unref(&vm->root.base.bo);
2642 vm->root.base.bo = NULL;
Christian König2bd9ccf2016-02-01 12:53:58 +01002643
2644error_free_sched_entity:
Nayan Deshmukhcdc50172018-07-20 17:51:05 +05302645 drm_sched_entity_destroy(&vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002646
2647 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002648}
2649
2650/**
Felix Kuehlingb236fa12018-03-15 17:27:42 -04002651 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2652 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002653 * @adev: amdgpu_device pointer
2654 * @vm: requested vm
2655 *
Felix Kuehlingb236fa12018-03-15 17:27:42 -04002656 * This only works on GFX VMs that don't have any BOs added and no
2657 * page tables allocated yet.
2658 *
2659 * Changes the following VM parameters:
2660 * - use_cpu_for_update
2661 * - pte_supports_ats
2662 * - pasid (old PASID is released, because compute manages its own PASIDs)
2663 *
2664 * Reinitializes the page directory to reflect the changed ATS
2665 * setting. May leave behind an unused shadow BO for the page
2666 * directory when switching from SDMA updates to CPU updates.
2667 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002668 * Returns:
2669 * 0 for success, -errno for errors.
Felix Kuehlingb236fa12018-03-15 17:27:42 -04002670 */
2671int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2672{
2673 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2674 int r;
2675
2676 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2677 if (r)
2678 return r;
2679
2680 /* Sanity checks */
2681 if (!RB_EMPTY_ROOT(&vm->va.rb_root) || vm->root.entries) {
2682 r = -EINVAL;
2683 goto error;
2684 }
2685
2686 /* Check if PD needs to be reinitialized and do it before
2687 * changing any other state, in case it fails.
2688 */
2689 if (pte_support_ats != vm->pte_support_ats) {
2690 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
2691 adev->vm_manager.root_level,
2692 pte_support_ats);
2693 if (r)
2694 goto error;
2695 }
2696
2697 /* Update VM state */
2698 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2699 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2700 vm->pte_support_ats = pte_support_ats;
2701 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2702 vm->use_cpu_for_update ? "CPU" : "SDMA");
Andrey Grodzovskyc8c5e562018-06-12 14:28:20 -04002703 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_gmc_vram_full_visible(&adev->gmc)),
Felix Kuehlingb236fa12018-03-15 17:27:42 -04002704 "CPU update of VM recommended only for large BAR system\n");
2705
2706 if (vm->pasid) {
2707 unsigned long flags;
2708
2709 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2710 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2711 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2712
2713 vm->pasid = 0;
2714 }
2715
2716error:
2717 amdgpu_bo_unreserve(vm->root.base.bo);
2718 return r;
2719}
2720
2721/**
Christian Königf566ceb2016-10-27 20:04:38 +02002722 * amdgpu_vm_free_levels - free PD/PT levels
2723 *
Christian König8f19cd72017-11-30 15:28:03 +01002724 * @adev: amdgpu device structure
2725 * @parent: PD/PT starting level to free
2726 * @level: level of parent structure
Christian Königf566ceb2016-10-27 20:04:38 +02002727 *
2728 * Free the page directory or page table level and all sub levels.
2729 */
Christian König8f19cd72017-11-30 15:28:03 +01002730static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2731 struct amdgpu_vm_pt *parent,
2732 unsigned level)
Christian Königf566ceb2016-10-27 20:04:38 +02002733{
Christian König8f19cd72017-11-30 15:28:03 +01002734 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
Christian Königf566ceb2016-10-27 20:04:38 +02002735
Christian König8f19cd72017-11-30 15:28:03 +01002736 if (parent->base.bo) {
2737 list_del(&parent->base.bo_list);
2738 list_del(&parent->base.vm_status);
2739 amdgpu_bo_unref(&parent->base.bo->shadow);
2740 amdgpu_bo_unref(&parent->base.bo);
Christian Königf566ceb2016-10-27 20:04:38 +02002741 }
2742
Christian König8f19cd72017-11-30 15:28:03 +01002743 if (parent->entries)
2744 for (i = 0; i < num_entries; i++)
2745 amdgpu_vm_free_levels(adev, &parent->entries[i],
2746 level + 1);
Christian Königf566ceb2016-10-27 20:04:38 +02002747
Christian König8f19cd72017-11-30 15:28:03 +01002748 kvfree(parent->entries);
Christian Königf566ceb2016-10-27 20:04:38 +02002749}
2750
2751/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002752 * amdgpu_vm_fini - tear down a vm instance
2753 *
2754 * @adev: amdgpu_device pointer
2755 * @vm: requested vm
2756 *
Christian König8843dbb2016-01-26 12:17:11 +01002757 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002758 * Unbind the VM and remove all bos from the vm bo list
2759 */
2760void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2761{
2762 struct amdgpu_bo_va_mapping *mapping, *tmp;
Christian König132f34e2018-01-12 15:26:08 +01002763 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
Christian König2642cf12017-10-13 17:24:31 +02002764 struct amdgpu_bo *root;
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002765 u64 fault;
Christian König2642cf12017-10-13 17:24:31 +02002766 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002767
Felix Kuehlingede0dd82018-03-15 17:27:43 -04002768 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2769
Felix Kuehlinga2f14822017-08-26 02:43:06 -04002770 /* Clear pending page faults from IH when the VM is destroyed */
2771 while (kfifo_get(&vm->faults, &fault))
2772 amdgpu_ih_clear_fault(adev, fault);
2773
Felix Kuehling02208442017-08-25 20:40:26 -04002774 if (vm->pasid) {
2775 unsigned long flags;
2776
2777 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2778 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2779 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2780 }
2781
Nayan Deshmukhcdc50172018-07-20 17:51:05 +05302782 drm_sched_entity_destroy(&vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01002783
Davidlohr Buesof808c132017-09-08 16:15:08 -07002784 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002785 dev_err(adev->dev, "still active bo inside vm\n");
2786 }
Davidlohr Buesof808c132017-09-08 16:15:08 -07002787 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2788 &vm->va.rb_root, rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002789 list_del(&mapping->list);
Christian Königa9f87f62017-03-30 14:03:59 +02002790 amdgpu_vm_it_remove(mapping, &vm->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002791 kfree(mapping);
2792 }
2793 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
Christian König4388fc22017-03-13 10:13:36 +01002794 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
Christian König451bc8e2017-02-14 16:02:52 +01002795 amdgpu_vm_prt_fini(adev, vm);
Christian König4388fc22017-03-13 10:13:36 +01002796 prt_fini_needed = false;
Christian König451bc8e2017-02-14 16:02:52 +01002797 }
Christian König284710f2017-01-30 11:09:31 +01002798
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002799 list_del(&mapping->list);
Christian König451bc8e2017-02-14 16:02:52 +01002800 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002801 }
2802
Christian König2642cf12017-10-13 17:24:31 +02002803 root = amdgpu_bo_ref(vm->root.base.bo);
2804 r = amdgpu_bo_reserve(root, true);
2805 if (r) {
2806 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2807 } else {
Chunming Zhou196f7482017-12-13 14:22:54 +08002808 amdgpu_vm_free_levels(adev, &vm->root,
2809 adev->vm_manager.root_level);
Christian König2642cf12017-10-13 17:24:31 +02002810 amdgpu_bo_unreserve(root);
2811 }
2812 amdgpu_bo_unref(&root);
Christian Königd5884512017-09-08 14:09:41 +02002813 dma_fence_put(vm->last_update);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002814 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
Christian König620f7742017-12-18 16:53:03 +01002815 amdgpu_vmid_free_reserved(adev, vm, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002816}
Christian Königea89f8c2015-11-15 20:52:06 +01002817
2818/**
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002819 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2820 *
2821 * @adev: amdgpu_device pointer
2822 * @pasid: PASID do identify the VM
2823 *
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002824 * This function is expected to be called in interrupt context.
2825 *
2826 * Returns:
2827 * True if there was fault credit, false otherwise
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002828 */
2829bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2830 unsigned int pasid)
2831{
2832 struct amdgpu_vm *vm;
2833
2834 spin_lock(&adev->vm_manager.pasid_lock);
2835 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
Christian Königd9589392018-01-09 19:18:59 +01002836 if (!vm) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002837 /* VM not found, can't track fault credit */
Christian Königd9589392018-01-09 19:18:59 +01002838 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002839 return true;
Christian Königd9589392018-01-09 19:18:59 +01002840 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002841
2842 /* No lock needed. only accessed by IRQ handler */
Christian Königd9589392018-01-09 19:18:59 +01002843 if (!vm->fault_credit) {
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002844 /* Too many faults in this VM */
Christian Königd9589392018-01-09 19:18:59 +01002845 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002846 return false;
Christian Königd9589392018-01-09 19:18:59 +01002847 }
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002848
2849 vm->fault_credit--;
Christian Königd9589392018-01-09 19:18:59 +01002850 spin_unlock(&adev->vm_manager.pasid_lock);
Felix Kuehlingc98171c2017-09-21 16:26:41 -04002851 return true;
2852}
2853
2854/**
Christian Königa9a78b32016-01-21 10:19:11 +01002855 * amdgpu_vm_manager_init - init the VM manager
2856 *
2857 * @adev: amdgpu_device pointer
2858 *
2859 * Initialize the VM manager structures
2860 */
2861void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2862{
Christian König620f7742017-12-18 16:53:03 +01002863 unsigned i;
Christian Königa9a78b32016-01-21 10:19:11 +01002864
Christian König620f7742017-12-18 16:53:03 +01002865 amdgpu_vmid_mgr_init(adev);
Christian König2d55e452016-02-08 17:37:38 +01002866
Chris Wilsonf54d1862016-10-25 13:00:45 +01002867 adev->vm_manager.fence_context =
2868 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Christian König1fbb2e92016-06-01 10:47:36 +02002869 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2870 adev->vm_manager.seqno[i] = 0;
2871
Christian König2d55e452016-02-08 17:37:38 +01002872 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian König284710f2017-01-30 11:09:31 +01002873 spin_lock_init(&adev->vm_manager.prt_lock);
Christian König451bc8e2017-02-14 16:02:52 +01002874 atomic_set(&adev->vm_manager.num_prt_users, 0);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002875
2876 /* If not overridden by the user, by default, only in large BAR systems
2877 * Compute VM tables will be updated by CPU
2878 */
2879#ifdef CONFIG_X86_64
2880 if (amdgpu_vm_update_mode == -1) {
Andrey Grodzovskyc8c5e562018-06-12 14:28:20 -04002881 if (amdgpu_gmc_vram_full_visible(&adev->gmc))
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -04002882 adev->vm_manager.vm_update_mode =
2883 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2884 else
2885 adev->vm_manager.vm_update_mode = 0;
2886 } else
2887 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2888#else
2889 adev->vm_manager.vm_update_mode = 0;
2890#endif
2891
Felix Kuehling02208442017-08-25 20:40:26 -04002892 idr_init(&adev->vm_manager.pasid_idr);
2893 spin_lock_init(&adev->vm_manager.pasid_lock);
Christian Königa9a78b32016-01-21 10:19:11 +01002894}
2895
2896/**
Christian Königea89f8c2015-11-15 20:52:06 +01002897 * amdgpu_vm_manager_fini - cleanup VM manager
2898 *
2899 * @adev: amdgpu_device pointer
2900 *
2901 * Cleanup the VM manager and free resources.
2902 */
2903void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2904{
Felix Kuehling02208442017-08-25 20:40:26 -04002905 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2906 idr_destroy(&adev->vm_manager.pasid_idr);
2907
Christian König620f7742017-12-18 16:53:03 +01002908 amdgpu_vmid_mgr_fini(adev);
Christian Königea89f8c2015-11-15 20:52:06 +01002909}
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002910
Andrey Grodzovsky7fc48e52018-06-11 11:11:24 -04002911/**
2912 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
2913 *
2914 * @dev: drm device pointer
2915 * @data: drm_amdgpu_vm
2916 * @filp: drm file pointer
2917 *
2918 * Returns:
2919 * 0 for success, -errno for errors.
2920 */
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002921int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2922{
2923 union drm_amdgpu_vm *args = data;
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002924 struct amdgpu_device *adev = dev->dev_private;
2925 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2926 int r;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002927
2928 switch (args->in.op) {
2929 case AMDGPU_VM_OP_RESERVE_VMID:
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002930 /* current, we only have requirement to reserve vmid from gfxhub */
Christian König620f7742017-12-18 16:53:03 +01002931 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhou1e9ef262017-04-20 16:18:48 +08002932 if (r)
2933 return r;
2934 break;
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002935 case AMDGPU_VM_OP_UNRESERVE_VMID:
Christian König620f7742017-12-18 16:53:03 +01002936 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +08002937 break;
2938 default:
2939 return -EINVAL;
2940 }
2941
2942 return 0;
2943}
Andrey Grodzovsky2aa37bf2018-06-28 22:51:32 -04002944
2945/**
2946 * amdgpu_vm_get_task_info - Extracts task info for a PASID.
2947 *
2948 * @dev: drm device pointer
2949 * @pasid: PASID identifier for VM
2950 * @task_info: task_info to fill.
2951 */
2952void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid,
2953 struct amdgpu_task_info *task_info)
2954{
2955 struct amdgpu_vm *vm;
2956
2957 spin_lock(&adev->vm_manager.pasid_lock);
2958
2959 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2960 if (vm)
2961 *task_info = vm->task_info;
2962
2963 spin_unlock(&adev->vm_manager.pasid_lock);
2964}
2965
2966/**
2967 * amdgpu_vm_set_task_info - Sets VMs task info.
2968 *
2969 * @vm: vm for which to set the info
2970 */
2971void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
2972{
2973 if (!vm->task_info.pid) {
2974 vm->task_info.pid = current->pid;
2975 get_task_comm(vm->task_info.task_name, current);
2976
2977 if (current->group_leader->mm == current->mm) {
2978 vm->task_info.tgid = current->group_leader->pid;
2979 get_task_comm(vm->task_info.process_name, current->group_leader);
2980 }
2981 }
2982}