]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
drm/ttm: once more fix ttm_bo_bulk_move_lru_tail
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_vm.c
CommitLineData
d38ceaf9
AD
1/*
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 */
f54d1867 28#include <linux/dma-fence-array.h>
a9f87f64 29#include <linux/interval_tree_generic.h>
02208441 30#include <linux/idr.h>
d38ceaf9
AD
31#include <drm/drmP.h>
32#include <drm/amdgpu_drm.h>
33#include "amdgpu.h"
34#include "amdgpu_trace.h"
ede0dd86 35#include "amdgpu_amdkfd.h"
c8c5e569 36#include "amdgpu_gmc.h"
d38ceaf9 37
7fc48e59
AG
38/**
39 * DOC: GPUVM
40 *
d38ceaf9
AD
41 * 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
a9f87f64
CK
59#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
7fc48e59
AG
68/**
69 * struct amdgpu_pte_update_params - Local structure
70 *
71 * Encapsulate some VM table update parameters to reduce
f4833c4f 72 * the number of function parameters
7fc48e59 73 *
f4833c4f 74 */
29efc4f5 75struct amdgpu_pte_update_params {
7fc48e59
AG
76
77 /**
78 * @adev: amdgpu device we do this update for
79 */
27c5f36f 80 struct amdgpu_device *adev;
7fc48e59
AG
81
82 /**
83 * @vm: optional amdgpu_vm we do this update for
84 */
49ac8a24 85 struct amdgpu_vm *vm;
7fc48e59
AG
86
87 /**
88 * @src: address where to copy page table entries from
89 */
f4833c4f 90 uint64_t src;
7fc48e59
AG
91
92 /**
93 * @ib: indirect buffer to fill with commands
94 */
f4833c4f 95 struct amdgpu_ib *ib;
7fc48e59
AG
96
97 /**
98 * @func: Function which actually does the update
99 */
373ac645
CK
100 void (*func)(struct amdgpu_pte_update_params *params,
101 struct amdgpu_bo *bo, uint64_t pe,
afef8b8f 102 uint64_t addr, unsigned count, uint32_t incr,
6b777607 103 uint64_t flags);
7fc48e59
AG
104 /**
105 * @pages_addr:
106 *
107 * DMA addresses to use for mapping, used during VM update by CPU
b4d42511
HK
108 */
109 dma_addr_t *pages_addr;
7fc48e59
AG
110
111 /**
112 * @kptr:
113 *
114 * Kernel pointer of PD/PT BO that needs to be updated,
115 * used during VM update by CPU
116 */
b4d42511 117 void *kptr;
f4833c4f
HK
118};
119
7fc48e59
AG
120/**
121 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
122 */
284710fa 123struct amdgpu_prt_cb {
7fc48e59
AG
124
125 /**
126 * @adev: amdgpu device
127 */
284710fa 128 struct amdgpu_device *adev;
7fc48e59
AG
129
130 /**
131 * @cb: callback
132 */
284710fa
CK
133 struct dma_fence_cb cb;
134};
135
50783147
CK
136/**
137 * amdgpu_vm_level_shift - return the addr shift for each level
138 *
139 * @adev: amdgpu_device pointer
7fc48e59 140 * @level: VMPT level
50783147 141 *
7fc48e59
AG
142 * Returns:
143 * The number of bits the pfn needs to be right shifted for a level.
50783147
CK
144 */
145static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
146 unsigned level)
147{
196f7489
CZ
148 unsigned shift = 0xff;
149
150 switch (level) {
151 case AMDGPU_VM_PDB2:
152 case AMDGPU_VM_PDB1:
153 case AMDGPU_VM_PDB0:
154 shift = 9 * (AMDGPU_VM_PDB0 - level) +
50783147 155 adev->vm_manager.block_size;
196f7489
CZ
156 break;
157 case AMDGPU_VM_PTB:
158 shift = 0;
159 break;
160 default:
161 dev_err(adev->dev, "the level%d isn't supported.\n", level);
162 }
163
164 return shift;
50783147
CK
165}
166
d38ceaf9 167/**
72a7ec5c 168 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
d38ceaf9
AD
169 *
170 * @adev: amdgpu_device pointer
7fc48e59 171 * @level: VMPT level
d38ceaf9 172 *
7fc48e59
AG
173 * Returns:
174 * The number of entries in a page directory or page table.
d38ceaf9 175 */
72a7ec5c
CK
176static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
177 unsigned level)
d38ceaf9 178{
196f7489
CZ
179 unsigned shift = amdgpu_vm_level_shift(adev,
180 adev->vm_manager.root_level);
0410c5e5 181
196f7489 182 if (level == adev->vm_manager.root_level)
72a7ec5c 183 /* For the root directory */
0410c5e5 184 return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
196f7489 185 else if (level != AMDGPU_VM_PTB)
0410c5e5
CK
186 /* Everything in between */
187 return 512;
188 else
72a7ec5c 189 /* For the page tables on the leaves */
36b32a68 190 return AMDGPU_VM_PTE_COUNT(adev);
d38ceaf9
AD
191}
192
193/**
72a7ec5c 194 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
d38ceaf9
AD
195 *
196 * @adev: amdgpu_device pointer
7fc48e59 197 * @level: VMPT level
d38ceaf9 198 *
7fc48e59
AG
199 * Returns:
200 * The size of the BO for a page directory or page table in bytes.
d38ceaf9 201 */
72a7ec5c 202static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
d38ceaf9 203{
72a7ec5c 204 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
d38ceaf9
AD
205}
206
bcdc9fd6
CK
207/**
208 * amdgpu_vm_bo_evicted - vm_bo is evicted
209 *
210 * @vm_bo: vm_bo which is evicted
211 *
212 * State for PDs/PTs and per VM BOs which are not at the location they should
213 * be.
214 */
215static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo)
216{
217 struct amdgpu_vm *vm = vm_bo->vm;
218 struct amdgpu_bo *bo = vm_bo->bo;
219
220 vm_bo->moved = true;
221 if (bo->tbo.type == ttm_bo_type_kernel)
222 list_move(&vm_bo->vm_status, &vm->evicted);
223 else
224 list_move_tail(&vm_bo->vm_status, &vm->evicted);
225}
226
227/**
228 * amdgpu_vm_bo_relocated - vm_bo is reloacted
229 *
230 * @vm_bo: vm_bo which is relocated
231 *
232 * State for PDs/PTs which needs to update their parent PD.
233 */
234static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo)
235{
236 list_move(&vm_bo->vm_status, &vm_bo->vm->relocated);
237}
238
239/**
240 * amdgpu_vm_bo_moved - vm_bo is moved
241 *
242 * @vm_bo: vm_bo which is moved
243 *
244 * State for per VM BOs which are moved, but that change is not yet reflected
245 * in the page tables.
246 */
247static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo)
248{
249 list_move(&vm_bo->vm_status, &vm_bo->vm->moved);
250}
251
252/**
253 * amdgpu_vm_bo_idle - vm_bo is idle
254 *
255 * @vm_bo: vm_bo which is now idle
256 *
257 * State for PDs/PTs and per VM BOs which have gone through the state machine
258 * and are now idle.
259 */
260static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo)
261{
262 list_move(&vm_bo->vm_status, &vm_bo->vm->idle);
263 vm_bo->moved = false;
264}
265
266/**
267 * amdgpu_vm_bo_invalidated - vm_bo is invalidated
268 *
269 * @vm_bo: vm_bo which is now invalidated
270 *
271 * State for normal BOs which are invalidated and that change not yet reflected
272 * in the PTs.
273 */
274static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo)
275{
276 spin_lock(&vm_bo->vm->invalidated_lock);
277 list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated);
278 spin_unlock(&vm_bo->vm->invalidated_lock);
279}
280
281/**
282 * amdgpu_vm_bo_done - vm_bo is done
283 *
284 * @vm_bo: vm_bo which is now done
285 *
286 * State for normal BOs which are invalidated and that change has been updated
287 * in the PTs.
288 */
289static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo)
290{
291 spin_lock(&vm_bo->vm->invalidated_lock);
292 list_del_init(&vm_bo->vm_status);
293 spin_unlock(&vm_bo->vm->invalidated_lock);
294}
295
c460f8a6
CK
296/**
297 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
298 *
299 * @base: base structure for tracking BO usage in a VM
300 * @vm: vm to which bo is to be added
301 * @bo: amdgpu buffer object
302 *
303 * Initialize a bo_va_base structure and add it to the appropriate lists
304 *
305 */
306static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
307 struct amdgpu_vm *vm,
308 struct amdgpu_bo *bo)
309{
310 base->vm = vm;
311 base->bo = bo;
312 INIT_LIST_HEAD(&base->bo_list);
313 INIT_LIST_HEAD(&base->vm_status);
314
315 if (!bo)
316 return;
317 list_add_tail(&base->bo_list, &bo->va);
318
319 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
320 return;
321
322 vm->bulk_moveable = false;
323 if (bo->tbo.type == ttm_bo_type_kernel)
bcdc9fd6 324 amdgpu_vm_bo_relocated(base);
c460f8a6 325 else
bcdc9fd6 326 amdgpu_vm_bo_idle(base);
c460f8a6
CK
327
328 if (bo->preferred_domains &
329 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
330 return;
331
332 /*
333 * we checked all the prerequisites, but it looks like this per vm bo
334 * is currently evicted. add the bo to the evicted list to make sure it
335 * is validated on next vm use to avoid fault.
336 * */
bcdc9fd6 337 amdgpu_vm_bo_evicted(base);
c460f8a6
CK
338}
339
ba79fde4
CK
340/**
341 * amdgpu_vm_pt_parent - get the parent page directory
342 *
343 * @pt: child page table
344 *
345 * Helper to get the parent entry for the child page table. NULL if we are at
346 * the root page directory.
347 */
348static struct amdgpu_vm_pt *amdgpu_vm_pt_parent(struct amdgpu_vm_pt *pt)
349{
350 struct amdgpu_bo *parent = pt->base.bo->parent;
351
352 if (!parent)
353 return NULL;
354
355 return list_first_entry(&parent->va, struct amdgpu_vm_pt, base.bo_list);
356}
357
73633e32
CK
358/**
359 * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt
360 */
361struct amdgpu_vm_pt_cursor {
362 uint64_t pfn;
363 struct amdgpu_vm_pt *parent;
364 struct amdgpu_vm_pt *entry;
365 unsigned level;
366};
367
368/**
369 * amdgpu_vm_pt_start - start PD/PT walk
370 *
371 * @adev: amdgpu_device pointer
372 * @vm: amdgpu_vm structure
373 * @start: start address of the walk
374 * @cursor: state to initialize
375 *
376 * Initialize a amdgpu_vm_pt_cursor to start a walk.
377 */
378static void amdgpu_vm_pt_start(struct amdgpu_device *adev,
379 struct amdgpu_vm *vm, uint64_t start,
380 struct amdgpu_vm_pt_cursor *cursor)
381{
382 cursor->pfn = start;
383 cursor->parent = NULL;
384 cursor->entry = &vm->root;
385 cursor->level = adev->vm_manager.root_level;
386}
387
388/**
389 * amdgpu_vm_pt_descendant - go to child node
390 *
391 * @adev: amdgpu_device pointer
392 * @cursor: current state
393 *
394 * Walk to the child node of the current node.
395 * Returns:
396 * True if the walk was possible, false otherwise.
397 */
398static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev,
399 struct amdgpu_vm_pt_cursor *cursor)
400{
401 unsigned num_entries, shift, idx;
402
403 if (!cursor->entry->entries)
404 return false;
405
406 BUG_ON(!cursor->entry->base.bo);
407 num_entries = amdgpu_vm_num_entries(adev, cursor->level);
408 shift = amdgpu_vm_level_shift(adev, cursor->level);
409
410 ++cursor->level;
411 idx = (cursor->pfn >> shift) % num_entries;
412 cursor->parent = cursor->entry;
413 cursor->entry = &cursor->entry->entries[idx];
414 return true;
415}
416
417/**
418 * amdgpu_vm_pt_sibling - go to sibling node
419 *
420 * @adev: amdgpu_device pointer
421 * @cursor: current state
422 *
423 * Walk to the sibling node of the current node.
424 * Returns:
425 * True if the walk was possible, false otherwise.
426 */
427static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev,
428 struct amdgpu_vm_pt_cursor *cursor)
429{
430 unsigned shift, num_entries;
431
432 /* Root doesn't have a sibling */
433 if (!cursor->parent)
434 return false;
435
436 /* Go to our parents and see if we got a sibling */
437 shift = amdgpu_vm_level_shift(adev, cursor->level - 1);
438 num_entries = amdgpu_vm_num_entries(adev, cursor->level - 1);
439
440 if (cursor->entry == &cursor->parent->entries[num_entries - 1])
441 return false;
442
443 cursor->pfn += 1ULL << shift;
444 cursor->pfn &= ~((1ULL << shift) - 1);
445 ++cursor->entry;
446 return true;
447}
448
449/**
450 * amdgpu_vm_pt_ancestor - go to parent node
451 *
452 * @cursor: current state
453 *
454 * Walk to the parent node of the current node.
455 * Returns:
456 * True if the walk was possible, false otherwise.
457 */
458static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor)
459{
460 if (!cursor->parent)
461 return false;
462
463 --cursor->level;
464 cursor->entry = cursor->parent;
465 cursor->parent = amdgpu_vm_pt_parent(cursor->parent);
466 return true;
467}
468
469/**
470 * amdgpu_vm_pt_next - get next PD/PT in hieratchy
471 *
472 * @adev: amdgpu_device pointer
473 * @cursor: current state
474 *
475 * Walk the PD/PT tree to the next node.
476 */
477static void amdgpu_vm_pt_next(struct amdgpu_device *adev,
478 struct amdgpu_vm_pt_cursor *cursor)
479{
480 /* First try a newborn child */
481 if (amdgpu_vm_pt_descendant(adev, cursor))
482 return;
483
484 /* If that didn't worked try to find a sibling */
485 while (!amdgpu_vm_pt_sibling(adev, cursor)) {
486 /* No sibling, go to our parents and grandparents */
487 if (!amdgpu_vm_pt_ancestor(cursor)) {
488 cursor->pfn = ~0ll;
489 return;
490 }
491 }
492}
493
494/**
495 * amdgpu_vm_pt_first_leaf - get first leaf PD/PT
496 *
497 * @adev: amdgpu_device pointer
498 * @vm: amdgpu_vm structure
499 * @start: start addr of the walk
500 * @cursor: state to initialize
501 *
502 * Start a walk and go directly to the leaf node.
503 */
504static void amdgpu_vm_pt_first_leaf(struct amdgpu_device *adev,
505 struct amdgpu_vm *vm, uint64_t start,
506 struct amdgpu_vm_pt_cursor *cursor)
507{
508 amdgpu_vm_pt_start(adev, vm, start, cursor);
509 while (amdgpu_vm_pt_descendant(adev, cursor));
510}
511
512/**
513 * amdgpu_vm_pt_next_leaf - get next leaf PD/PT
514 *
515 * @adev: amdgpu_device pointer
516 * @cursor: current state
517 *
518 * Walk the PD/PT tree to the next leaf node.
519 */
520static void amdgpu_vm_pt_next_leaf(struct amdgpu_device *adev,
521 struct amdgpu_vm_pt_cursor *cursor)
522{
523 amdgpu_vm_pt_next(adev, cursor);
524 while (amdgpu_vm_pt_descendant(adev, cursor));
525}
526
527/**
528 * for_each_amdgpu_vm_pt_leaf - walk over all leaf PDs/PTs in the hierarchy
529 */
530#define for_each_amdgpu_vm_pt_leaf(adev, vm, start, end, cursor) \
531 for (amdgpu_vm_pt_first_leaf((adev), (vm), (start), &(cursor)); \
532 (cursor).pfn <= end; amdgpu_vm_pt_next_leaf((adev), &(cursor)))
533
534/**
535 * amdgpu_vm_pt_first_dfs - start a deep first search
536 *
537 * @adev: amdgpu_device structure
538 * @vm: amdgpu_vm structure
539 * @cursor: state to initialize
540 *
541 * Starts a deep first traversal of the PD/PT tree.
542 */
543static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev,
544 struct amdgpu_vm *vm,
545 struct amdgpu_vm_pt_cursor *cursor)
546{
547 amdgpu_vm_pt_start(adev, vm, 0, cursor);
548 while (amdgpu_vm_pt_descendant(adev, cursor));
549}
550
551/**
552 * amdgpu_vm_pt_next_dfs - get the next node for a deep first search
553 *
554 * @adev: amdgpu_device structure
555 * @cursor: current state
556 *
557 * Move the cursor to the next node in a deep first search.
558 */
559static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev,
560 struct amdgpu_vm_pt_cursor *cursor)
561{
562 if (!cursor->entry)
563 return;
564
565 if (!cursor->parent)
566 cursor->entry = NULL;
567 else if (amdgpu_vm_pt_sibling(adev, cursor))
568 while (amdgpu_vm_pt_descendant(adev, cursor));
569 else
570 amdgpu_vm_pt_ancestor(cursor);
571}
572
573/**
574 * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs
575 */
576#define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, cursor, entry) \
577 for (amdgpu_vm_pt_first_dfs((adev), (vm), &(cursor)), \
578 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\
579 (entry); (entry) = (cursor).entry, \
580 amdgpu_vm_pt_next_dfs((adev), &(cursor)))
581
d38ceaf9 582/**
56467ebf 583 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
d38ceaf9
AD
584 *
585 * @vm: vm providing the BOs
3c0eea6c 586 * @validated: head of validation list
56467ebf 587 * @entry: entry to add
d38ceaf9
AD
588 *
589 * Add the page directory to the list of BOs to
56467ebf 590 * validate for command submission.
d38ceaf9 591 */
56467ebf
CK
592void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
593 struct list_head *validated,
594 struct amdgpu_bo_list_entry *entry)
d38ceaf9 595{
3f3333f8 596 entry->robj = vm->root.base.bo;
56467ebf 597 entry->priority = 0;
67003a15 598 entry->tv.bo = &entry->robj->tbo;
56467ebf 599 entry->tv.shared = true;
2f568dbd 600 entry->user_pages = NULL;
56467ebf
CK
601 list_add(&entry->tv.head, validated);
602}
d38ceaf9 603
f921661b
HR
604/**
605 * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
606 *
607 * @adev: amdgpu device pointer
608 * @vm: vm providing the BOs
609 *
610 * Move all BOs to the end of LRU and remember their positions to put them
611 * together.
612 */
613void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
614 struct amdgpu_vm *vm)
615{
616 struct ttm_bo_global *glob = adev->mman.bdev.glob;
617 struct amdgpu_vm_bo_base *bo_base;
618
619 if (vm->bulk_moveable) {
620 spin_lock(&glob->lru_lock);
621 ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
622 spin_unlock(&glob->lru_lock);
623 return;
624 }
625
626 memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
627
628 spin_lock(&glob->lru_lock);
629 list_for_each_entry(bo_base, &vm->idle, vm_status) {
630 struct amdgpu_bo *bo = bo_base->bo;
631
632 if (!bo->parent)
633 continue;
634
635 ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
636 if (bo->shadow)
637 ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
638 &vm->lru_bulk_move);
639 }
640 spin_unlock(&glob->lru_lock);
641
642 vm->bulk_moveable = true;
643}
644
670fecc8 645/**
f7da30d9 646 * amdgpu_vm_validate_pt_bos - validate the page table BOs
670fecc8 647 *
5a712a87 648 * @adev: amdgpu device pointer
56467ebf 649 * @vm: vm providing the BOs
670fecc8
CK
650 * @validate: callback to do the validation
651 * @param: parameter for the validation callback
652 *
653 * Validate the page table BOs on command submission if neccessary.
7fc48e59
AG
654 *
655 * Returns:
656 * Validation result.
670fecc8 657 */
f7da30d9
CK
658int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
659 int (*validate)(void *p, struct amdgpu_bo *bo),
660 void *param)
670fecc8 661{
91ccdd24
CK
662 struct amdgpu_vm_bo_base *bo_base, *tmp;
663 int r = 0;
670fecc8 664
f921661b
HR
665 vm->bulk_moveable &= list_empty(&vm->evicted);
666
91ccdd24
CK
667 list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
668 struct amdgpu_bo *bo = bo_base->bo;
670fecc8 669
262b9c39
CK
670 r = validate(param, bo);
671 if (r)
672 break;
670fecc8 673
af4c0f65 674 if (bo->tbo.type != ttm_bo_type_kernel) {
bcdc9fd6 675 amdgpu_vm_bo_moved(bo_base);
af4c0f65 676 } else {
17cc5252
CK
677 if (vm->use_cpu_for_update)
678 r = amdgpu_bo_kmap(bo, NULL);
679 else
680 r = amdgpu_ttm_alloc_gart(&bo->tbo);
284dec43
CK
681 if (r)
682 break;
3d5fe658
CK
683 if (bo->shadow) {
684 r = amdgpu_ttm_alloc_gart(&bo->shadow->tbo);
685 if (r)
686 break;
687 }
bcdc9fd6 688 amdgpu_vm_bo_relocated(bo_base);
af4c0f65 689 }
670fecc8
CK
690 }
691
91ccdd24 692 return r;
670fecc8
CK
693}
694
56467ebf 695/**
34d7be5d 696 * amdgpu_vm_ready - check VM is ready for updates
56467ebf 697 *
34d7be5d 698 * @vm: VM to check
d38ceaf9 699 *
34d7be5d 700 * Check if all VM PDs/PTs are ready for updates
7fc48e59
AG
701 *
702 * Returns:
703 * True if eviction list is empty.
d38ceaf9 704 */
3f3333f8 705bool amdgpu_vm_ready(struct amdgpu_vm *vm)
d38ceaf9 706{
af4c0f65 707 return list_empty(&vm->evicted);
d711e139
CK
708}
709
13307f7e
CK
710/**
711 * amdgpu_vm_clear_bo - initially clear the PDs/PTs
712 *
713 * @adev: amdgpu_device pointer
7fc48e59 714 * @vm: VM to clear BO from
13307f7e
CK
715 * @bo: BO to clear
716 * @level: level this BO is at
00553cf8 717 * @pte_support_ats: indicate ATS support from PTE
13307f7e
CK
718 *
719 * Root PD needs to be reserved when calling this.
7fc48e59
AG
720 *
721 * Returns:
722 * 0 on success, errno otherwise.
13307f7e
CK
723 */
724static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
4584312d
CK
725 struct amdgpu_vm *vm, struct amdgpu_bo *bo,
726 unsigned level, bool pte_support_ats)
13307f7e
CK
727{
728 struct ttm_operation_ctx ctx = { true, false };
729 struct dma_fence *fence = NULL;
4584312d 730 unsigned entries, ats_entries;
13307f7e
CK
731 struct amdgpu_ring *ring;
732 struct amdgpu_job *job;
4584312d 733 uint64_t addr;
13307f7e
CK
734 int r;
735
4584312d
CK
736 entries = amdgpu_bo_size(bo) / 8;
737
738 if (pte_support_ats) {
739 if (level == adev->vm_manager.root_level) {
740 ats_entries = amdgpu_vm_level_shift(adev, level);
741 ats_entries += AMDGPU_GPU_PAGE_SHIFT;
ad9a5b78 742 ats_entries = AMDGPU_GMC_HOLE_START >> ats_entries;
4584312d
CK
743 ats_entries = min(ats_entries, entries);
744 entries -= ats_entries;
745 } else {
746 ats_entries = entries;
747 entries = 0;
748 }
13307f7e 749 } else {
4584312d 750 ats_entries = 0;
13307f7e
CK
751 }
752
068c3304 753 ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
13307f7e
CK
754
755 r = reservation_object_reserve_shared(bo->tbo.resv);
756 if (r)
757 return r;
758
759 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
760 if (r)
761 goto error;
762
284dec43
CK
763 r = amdgpu_ttm_alloc_gart(&bo->tbo);
764 if (r)
765 return r;
766
13307f7e
CK
767 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
768 if (r)
769 goto error;
770
1cadf2b3 771 addr = amdgpu_bo_gpu_offset(bo);
4584312d
CK
772 if (ats_entries) {
773 uint64_t ats_value;
774
775 ats_value = AMDGPU_PTE_DEFAULT_ATC;
776 if (level != AMDGPU_VM_PTB)
777 ats_value |= AMDGPU_PDE_PTE;
778
779 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
780 ats_entries, 0, ats_value);
781 addr += ats_entries * 8;
782 }
783
784 if (entries)
785 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
786 entries, 0, 0);
787
13307f7e
CK
788 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
789
790 WARN_ON(job->ibs[0].length_dw > 64);
29e8357b
CK
791 r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
792 AMDGPU_FENCE_OWNER_UNDEFINED, false);
793 if (r)
794 goto error_free;
795
0e28b10f
CK
796 r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_UNDEFINED,
797 &fence);
13307f7e
CK
798 if (r)
799 goto error_free;
800
801 amdgpu_bo_fence(bo, fence, true);
802 dma_fence_put(fence);
e61736da
CK
803
804 if (bo->shadow)
805 return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
806 level, pte_support_ats);
807
13307f7e
CK
808 return 0;
809
810error_free:
811 amdgpu_job_free(job);
812
813error:
814 return r;
815}
816
e21eb261
CK
817/**
818 * amdgpu_vm_bo_param - fill in parameters for PD/PT allocation
819 *
820 * @adev: amdgpu_device pointer
821 * @vm: requesting vm
822 * @bp: resulting BO allocation parameters
823 */
824static void amdgpu_vm_bo_param(struct amdgpu_device *adev, struct amdgpu_vm *vm,
825 int level, struct amdgpu_bo_param *bp)
826{
827 memset(bp, 0, sizeof(*bp));
828
829 bp->size = amdgpu_vm_bo_size(adev, level);
830 bp->byte_align = AMDGPU_GPU_PAGE_SIZE;
831 bp->domain = AMDGPU_GEM_DOMAIN_VRAM;
284dec43
CK
832 if (bp->size <= PAGE_SIZE && adev->asic_type >= CHIP_VEGA10 &&
833 adev->flags & AMD_IS_APU)
834 bp->domain |= AMDGPU_GEM_DOMAIN_GTT;
835 bp->domain = amdgpu_bo_get_preferred_pin_domain(adev, bp->domain);
836 bp->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
837 AMDGPU_GEM_CREATE_CPU_GTT_USWC;
e21eb261
CK
838 if (vm->use_cpu_for_update)
839 bp->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
03e9dee1
FK
840 else if (!vm->root.base.bo || vm->root.base.bo->shadow)
841 bp->flags |= AMDGPU_GEM_CREATE_SHADOW;
e21eb261
CK
842 bp->type = ttm_bo_type_kernel;
843 if (vm->root.base.bo)
844 bp->resv = vm->root.base.bo->tbo.resv;
845}
846
663e4577
CK
847/**
848 * amdgpu_vm_alloc_pts - Allocate page tables.
849 *
850 * @adev: amdgpu_device pointer
851 * @vm: VM to allocate page tables for
852 * @saddr: Start address which needs to be allocated
853 * @size: Size from start address we need.
854 *
d72a6887 855 * Make sure the page directories and page tables are allocated
7fc48e59
AG
856 *
857 * Returns:
858 * 0 on success, errno otherwise.
663e4577
CK
859 */
860int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
861 struct amdgpu_vm *vm,
862 uint64_t saddr, uint64_t size)
863{
d72a6887
CK
864 struct amdgpu_vm_pt_cursor cursor;
865 struct amdgpu_bo *pt;
4584312d 866 bool ats = false;
d72a6887
CK
867 uint64_t eaddr;
868 int r;
663e4577
CK
869
870 /* validate the parameters */
871 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
872 return -EINVAL;
873
874 eaddr = saddr + size - 1;
4584312d
CK
875
876 if (vm->pte_support_ats)
ad9a5b78 877 ats = saddr < AMDGPU_GMC_HOLE_START;
663e4577
CK
878
879 saddr /= AMDGPU_GPU_PAGE_SIZE;
880 eaddr /= AMDGPU_GPU_PAGE_SIZE;
881
4584312d
CK
882 if (eaddr >= adev->vm_manager.max_pfn) {
883 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
884 eaddr, adev->vm_manager.max_pfn);
885 return -EINVAL;
886 }
887
d72a6887
CK
888 for_each_amdgpu_vm_pt_leaf(adev, vm, saddr, eaddr, cursor) {
889 struct amdgpu_vm_pt *entry = cursor.entry;
890 struct amdgpu_bo_param bp;
891
892 if (cursor.level < AMDGPU_VM_PTB) {
893 unsigned num_entries;
894
895 num_entries = amdgpu_vm_num_entries(adev, cursor.level);
896 entry->entries = kvmalloc_array(num_entries,
897 sizeof(*entry->entries),
898 GFP_KERNEL |
899 __GFP_ZERO);
900 if (!entry->entries)
901 return -ENOMEM;
902 }
903
904
905 if (entry->base.bo)
906 continue;
907
908 amdgpu_vm_bo_param(adev, vm, cursor.level, &bp);
909
910 r = amdgpu_bo_create(adev, &bp, &pt);
911 if (r)
912 return r;
913
914 r = amdgpu_vm_clear_bo(adev, vm, pt, cursor.level, ats);
915 if (r)
916 goto error_free_pt;
917
918 if (vm->use_cpu_for_update) {
919 r = amdgpu_bo_kmap(pt, NULL);
920 if (r)
921 goto error_free_pt;
922 }
923
924 /* Keep a reference to the root directory to avoid
925 * freeing them up in the wrong order.
926 */
927 pt->parent = amdgpu_bo_ref(cursor.parent->base.bo);
928
929 amdgpu_vm_bo_base_init(&entry->base, vm, pt);
930 }
931
932 return 0;
933
934error_free_pt:
935 amdgpu_bo_unref(&pt->shadow);
936 amdgpu_bo_unref(&pt);
937 return r;
663e4577
CK
938}
939
229a37f8
CK
940/**
941 * amdgpu_vm_free_pts - free PD/PT levels
942 *
943 * @adev: amdgpu device structure
944 * @parent: PD/PT starting level to free
945 * @level: level of parent structure
946 *
947 * Free the page directory or page table level and all sub levels.
948 */
949static void amdgpu_vm_free_pts(struct amdgpu_device *adev,
950 struct amdgpu_vm *vm)
951{
952 struct amdgpu_vm_pt_cursor cursor;
953 struct amdgpu_vm_pt *entry;
954
955 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, cursor, entry) {
956
957 if (entry->base.bo) {
958 list_del(&entry->base.bo_list);
959 list_del(&entry->base.vm_status);
960 amdgpu_bo_unref(&entry->base.bo->shadow);
961 amdgpu_bo_unref(&entry->base.bo);
962 }
963 kvfree(entry->entries);
964 }
965
966 BUG_ON(vm->root.base.bo);
967}
968
e59c0205
AX
969/**
970 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
971 *
972 * @adev: amdgpu_device pointer
973 */
974void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
93dcc37d 975{
a1255107 976 const struct amdgpu_ip_block *ip_block;
e59c0205
AX
977 bool has_compute_vm_bug;
978 struct amdgpu_ring *ring;
979 int i;
93dcc37d 980
e59c0205 981 has_compute_vm_bug = false;
93dcc37d 982
2990a1fc 983 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
e59c0205
AX
984 if (ip_block) {
985 /* Compute has a VM bug for GFX version < 7.
986 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
987 if (ip_block->version->major <= 7)
988 has_compute_vm_bug = true;
989 else if (ip_block->version->major == 8)
990 if (adev->gfx.mec_fw_version < 673)
991 has_compute_vm_bug = true;
992 }
93dcc37d 993
e59c0205
AX
994 for (i = 0; i < adev->num_rings; i++) {
995 ring = adev->rings[i];
996 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
997 /* only compute rings */
998 ring->has_compute_vm_bug = has_compute_vm_bug;
93dcc37d 999 else
e59c0205 1000 ring->has_compute_vm_bug = false;
93dcc37d 1001 }
93dcc37d
AD
1002}
1003
7fc48e59
AG
1004/**
1005 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
1006 *
1007 * @ring: ring on which the job will be submitted
1008 * @job: job to submit
1009 *
1010 * Returns:
1011 * True if sync is needed.
1012 */
b9bf33d5
CZ
1013bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
1014 struct amdgpu_job *job)
e60f8db5 1015{
b9bf33d5
CZ
1016 struct amdgpu_device *adev = ring->adev;
1017 unsigned vmhub = ring->funcs->vmhub;
620f774f
CK
1018 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
1019 struct amdgpu_vmid *id;
b9bf33d5 1020 bool gds_switch_needed;
e59c0205 1021 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
b9bf33d5 1022
c4f46f22 1023 if (job->vmid == 0)
b9bf33d5 1024 return false;
c4f46f22 1025 id = &id_mgr->ids[job->vmid];
b9bf33d5
CZ
1026 gds_switch_needed = ring->funcs->emit_gds_switch && (
1027 id->gds_base != job->gds_base ||
1028 id->gds_size != job->gds_size ||
1029 id->gws_base != job->gws_base ||
1030 id->gws_size != job->gws_size ||
1031 id->oa_base != job->oa_base ||
1032 id->oa_size != job->oa_size);
e60f8db5 1033
620f774f 1034 if (amdgpu_vmid_had_gpu_reset(adev, id))
b9bf33d5 1035 return true;
e60f8db5 1036
bb37b67d 1037 return vm_flush_needed || gds_switch_needed;
b9bf33d5
CZ
1038}
1039
d38ceaf9
AD
1040/**
1041 * amdgpu_vm_flush - hardware flush the vm
1042 *
1043 * @ring: ring to use for flush
00553cf8 1044 * @job: related job
7fc48e59 1045 * @need_pipe_sync: is pipe sync needed
d38ceaf9 1046 *
4ff37a83 1047 * Emit a VM flush when it is necessary.
7fc48e59
AG
1048 *
1049 * Returns:
1050 * 0 on success, errno otherwise.
d38ceaf9 1051 */
8fdf074f 1052int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
d38ceaf9 1053{
971fe9a9 1054 struct amdgpu_device *adev = ring->adev;
7645670d 1055 unsigned vmhub = ring->funcs->vmhub;
620f774f 1056 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
c4f46f22 1057 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
d564a06e 1058 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
fd53be30
CZ
1059 id->gds_base != job->gds_base ||
1060 id->gds_size != job->gds_size ||
1061 id->gws_base != job->gws_base ||
1062 id->gws_size != job->gws_size ||
1063 id->oa_base != job->oa_base ||
1064 id->oa_size != job->oa_size);
de37e68a 1065 bool vm_flush_needed = job->vm_needs_flush;
b3cd285f
CK
1066 bool pasid_mapping_needed = id->pasid != job->pasid ||
1067 !id->pasid_mapping ||
1068 !dma_fence_is_signaled(id->pasid_mapping);
1069 struct dma_fence *fence = NULL;
c0e51931 1070 unsigned patch_offset = 0;
41d9eb2c 1071 int r;
d564a06e 1072
620f774f 1073 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
f7d015b9
CK
1074 gds_switch_needed = true;
1075 vm_flush_needed = true;
b3cd285f 1076 pasid_mapping_needed = true;
f7d015b9 1077 }
971fe9a9 1078
b3cd285f 1079 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
d8de8260
AG
1080 vm_flush_needed &= !!ring->funcs->emit_vm_flush &&
1081 job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
b3cd285f
CK
1082 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
1083 ring->funcs->emit_wreg;
1084
8fdf074f 1085 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
f7d015b9 1086 return 0;
41d9eb2c 1087
c0e51931
CK
1088 if (ring->funcs->init_cond_exec)
1089 patch_offset = amdgpu_ring_init_cond_exec(ring);
41d9eb2c 1090
8fdf074f
ML
1091 if (need_pipe_sync)
1092 amdgpu_ring_emit_pipeline_sync(ring);
1093
b3cd285f 1094 if (vm_flush_needed) {
c4f46f22 1095 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
c633c00b 1096 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
b3cd285f
CK
1097 }
1098
1099 if (pasid_mapping_needed)
1100 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
e9d672b2 1101
b3cd285f 1102 if (vm_flush_needed || pasid_mapping_needed) {
d240cd9e 1103 r = amdgpu_fence_emit(ring, &fence, 0);
c0e51931
CK
1104 if (r)
1105 return r;
b3cd285f 1106 }
e9d672b2 1107
b3cd285f 1108 if (vm_flush_needed) {
7645670d 1109 mutex_lock(&id_mgr->lock);
c0e51931 1110 dma_fence_put(id->last_flush);
b3cd285f
CK
1111 id->last_flush = dma_fence_get(fence);
1112 id->current_gpu_reset_count =
1113 atomic_read(&adev->gpu_reset_counter);
7645670d 1114 mutex_unlock(&id_mgr->lock);
c0e51931 1115 }
e9d672b2 1116
b3cd285f
CK
1117 if (pasid_mapping_needed) {
1118 id->pasid = job->pasid;
1119 dma_fence_put(id->pasid_mapping);
1120 id->pasid_mapping = dma_fence_get(fence);
1121 }
1122 dma_fence_put(fence);
1123
7c4378f4 1124 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
c0e51931
CK
1125 id->gds_base = job->gds_base;
1126 id->gds_size = job->gds_size;
1127 id->gws_base = job->gws_base;
1128 id->gws_size = job->gws_size;
1129 id->oa_base = job->oa_base;
1130 id->oa_size = job->oa_size;
c4f46f22 1131 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
c0e51931
CK
1132 job->gds_size, job->gws_base,
1133 job->gws_size, job->oa_base,
1134 job->oa_size);
1135 }
1136
1137 if (ring->funcs->patch_cond_exec)
1138 amdgpu_ring_patch_cond_exec(ring, patch_offset);
1139
1140 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
1141 if (ring->funcs->emit_switch_buffer) {
1142 amdgpu_ring_emit_switch_buffer(ring);
1143 amdgpu_ring_emit_switch_buffer(ring);
e9d672b2 1144 }
41d9eb2c 1145 return 0;
971fe9a9
CK
1146}
1147
d38ceaf9
AD
1148/**
1149 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
1150 *
1151 * @vm: requested vm
1152 * @bo: requested buffer object
1153 *
8843dbbb 1154 * Find @bo inside the requested vm.
d38ceaf9
AD
1155 * Search inside the @bos vm list for the requested vm
1156 * Returns the found bo_va or NULL if none is found
1157 *
1158 * Object has to be reserved!
7fc48e59
AG
1159 *
1160 * Returns:
1161 * Found bo_va or NULL.
d38ceaf9
AD
1162 */
1163struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
1164 struct amdgpu_bo *bo)
1165{
1166 struct amdgpu_bo_va *bo_va;
1167
ec681545
CK
1168 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
1169 if (bo_va->base.vm == vm) {
d38ceaf9
AD
1170 return bo_va;
1171 }
1172 }
1173 return NULL;
1174}
1175
1176/**
afef8b8f 1177 * amdgpu_vm_do_set_ptes - helper to call the right asic function
d38ceaf9 1178 *
29efc4f5 1179 * @params: see amdgpu_pte_update_params definition
373ac645 1180 * @bo: PD/PT to update
d38ceaf9
AD
1181 * @pe: addr of the page entry
1182 * @addr: dst addr to write into pe
1183 * @count: number of page entries to update
1184 * @incr: increase next addr by incr bytes
1185 * @flags: hw access flags
d38ceaf9
AD
1186 *
1187 * Traces the parameters and calls the right asic functions
1188 * to setup the page table using the DMA.
1189 */
afef8b8f 1190static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
373ac645 1191 struct amdgpu_bo *bo,
afef8b8f
CK
1192 uint64_t pe, uint64_t addr,
1193 unsigned count, uint32_t incr,
6b777607 1194 uint64_t flags)
d38ceaf9 1195{
373ac645 1196 pe += amdgpu_bo_gpu_offset(bo);
ec2f05f0 1197 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
d38ceaf9 1198
afef8b8f 1199 if (count < 3) {
de9ea7bd
CK
1200 amdgpu_vm_write_pte(params->adev, params->ib, pe,
1201 addr | flags, count, incr);
d38ceaf9
AD
1202
1203 } else {
27c5f36f 1204 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
d38ceaf9
AD
1205 count, incr, flags);
1206 }
1207}
1208
afef8b8f
CK
1209/**
1210 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
1211 *
1212 * @params: see amdgpu_pte_update_params definition
373ac645 1213 * @bo: PD/PT to update
afef8b8f
CK
1214 * @pe: addr of the page entry
1215 * @addr: dst addr to write into pe
1216 * @count: number of page entries to update
1217 * @incr: increase next addr by incr bytes
1218 * @flags: hw access flags
1219 *
1220 * Traces the parameters and calls the DMA function to copy the PTEs.
1221 */
1222static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
373ac645 1223 struct amdgpu_bo *bo,
afef8b8f
CK
1224 uint64_t pe, uint64_t addr,
1225 unsigned count, uint32_t incr,
6b777607 1226 uint64_t flags)
afef8b8f 1227{
ec2f05f0 1228 uint64_t src = (params->src + (addr >> 12) * 8);
afef8b8f 1229
373ac645 1230 pe += amdgpu_bo_gpu_offset(bo);
ec2f05f0
CK
1231 trace_amdgpu_vm_copy_ptes(pe, src, count);
1232
1233 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
afef8b8f
CK
1234}
1235
d38ceaf9 1236/**
b07c9d2a 1237 * amdgpu_vm_map_gart - Resolve gart mapping of addr
d38ceaf9 1238 *
b07c9d2a 1239 * @pages_addr: optional DMA address to use for lookup
d38ceaf9
AD
1240 * @addr: the unmapped addr
1241 *
1242 * Look up the physical address of the page that the pte resolves
7fc48e59
AG
1243 * to.
1244 *
1245 * Returns:
1246 * The pointer for the page table entry.
d38ceaf9 1247 */
de9ea7bd 1248static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
d38ceaf9
AD
1249{
1250 uint64_t result;
1251
de9ea7bd
CK
1252 /* page table offset */
1253 result = pages_addr[addr >> PAGE_SHIFT];
b07c9d2a 1254
de9ea7bd
CK
1255 /* in case cpu page size != gpu page size*/
1256 result |= addr & (~PAGE_MASK);
d38ceaf9 1257
b07c9d2a 1258 result &= 0xFFFFFFFFFFFFF000ULL;
d38ceaf9
AD
1259
1260 return result;
1261}
1262
3c824172
HK
1263/**
1264 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
1265 *
1266 * @params: see amdgpu_pte_update_params definition
373ac645 1267 * @bo: PD/PT to update
3c824172
HK
1268 * @pe: kmap addr of the page entry
1269 * @addr: dst addr to write into pe
1270 * @count: number of page entries to update
1271 * @incr: increase next addr by incr bytes
1272 * @flags: hw access flags
1273 *
1274 * Write count number of PT/PD entries directly.
1275 */
1276static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
373ac645 1277 struct amdgpu_bo *bo,
3c824172
HK
1278 uint64_t pe, uint64_t addr,
1279 unsigned count, uint32_t incr,
1280 uint64_t flags)
1281{
1282 unsigned int i;
b4d42511 1283 uint64_t value;
3c824172 1284
373ac645
CK
1285 pe += (unsigned long)amdgpu_bo_kptr(bo);
1286
03918b36
CK
1287 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
1288
3c824172 1289 for (i = 0; i < count; i++) {
b4d42511
HK
1290 value = params->pages_addr ?
1291 amdgpu_vm_map_gart(params->pages_addr, addr) :
1292 addr;
132f34e4
CK
1293 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
1294 i, value, flags);
3c824172
HK
1295 addr += incr;
1296 }
3c824172
HK
1297}
1298
7fc48e59
AG
1299
1300/**
1301 * amdgpu_vm_wait_pd - Wait for PT BOs to be free.
1302 *
1303 * @adev: amdgpu_device pointer
1304 * @vm: related vm
1305 * @owner: fence owner
1306 *
1307 * Returns:
1308 * 0 on success, errno otherwise.
1309 */
a33cab7a
CK
1310static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1311 void *owner)
3c824172
HK
1312{
1313 struct amdgpu_sync sync;
1314 int r;
1315
1316 amdgpu_sync_create(&sync);
177ae09b 1317 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
3c824172
HK
1318 r = amdgpu_sync_wait(&sync, true);
1319 amdgpu_sync_free(&sync);
1320
1321 return r;
1322}
1323
1c860a02
CK
1324/**
1325 * amdgpu_vm_update_func - helper to call update function
1326 *
1327 * Calls the update function for both the given BO as well as its shadow.
1328 */
1329static void amdgpu_vm_update_func(struct amdgpu_pte_update_params *params,
1330 struct amdgpu_bo *bo,
1331 uint64_t pe, uint64_t addr,
1332 unsigned count, uint32_t incr,
1333 uint64_t flags)
1334{
1335 if (bo->shadow)
1336 params->func(params, bo->shadow, pe, addr, count, incr, flags);
1337 params->func(params, bo, pe, addr, count, incr, flags);
1338}
1339
f8991bab 1340/*
6989f246 1341 * amdgpu_vm_update_pde - update a single level in the hierarchy
f8991bab 1342 *
6989f246 1343 * @param: parameters for the update
f8991bab 1344 * @vm: requested vm
194d2161 1345 * @parent: parent directory
6989f246 1346 * @entry: entry to update
f8991bab 1347 *
6989f246 1348 * Makes sure the requested entry in parent is up to date.
f8991bab 1349 */
6989f246
CK
1350static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
1351 struct amdgpu_vm *vm,
1352 struct amdgpu_vm_pt *parent,
1353 struct amdgpu_vm_pt *entry)
d38ceaf9 1354{
373ac645 1355 struct amdgpu_bo *bo = parent->base.bo, *pbo;
3de676d8
CK
1356 uint64_t pde, pt, flags;
1357 unsigned level;
d5fc5e82 1358
6989f246
CK
1359 /* Don't update huge pages here */
1360 if (entry->huge)
1361 return;
d38ceaf9 1362
373ac645 1363 for (level = 0, pbo = bo->parent; pbo; ++level)
3de676d8
CK
1364 pbo = pbo->parent;
1365
196f7489 1366 level += params->adev->vm_manager.root_level;
24a8d289 1367 amdgpu_gmc_get_pde_for_bo(entry->base.bo, level, &pt, &flags);
373ac645 1368 pde = (entry - parent->entries) * 8;
1c860a02 1369 amdgpu_vm_update_func(params, bo, pde, pt, 1, 0, flags);
d38ceaf9
AD
1370}
1371
92456b93 1372/*
d4085ea9 1373 * amdgpu_vm_invalidate_pds - mark all PDs as invalid
92456b93 1374 *
7fc48e59
AG
1375 * @adev: amdgpu_device pointer
1376 * @vm: related vm
92456b93
CK
1377 *
1378 * Mark all PD level as invalid after an error.
1379 */
d4085ea9
CK
1380static void amdgpu_vm_invalidate_pds(struct amdgpu_device *adev,
1381 struct amdgpu_vm *vm)
92456b93 1382{
d4085ea9
CK
1383 struct amdgpu_vm_pt_cursor cursor;
1384 struct amdgpu_vm_pt *entry;
92456b93 1385
d4085ea9
CK
1386 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, cursor, entry)
1387 if (entry->base.bo && !entry->base.moved)
bcdc9fd6 1388 amdgpu_vm_bo_relocated(&entry->base);
92456b93
CK
1389}
1390
194d2161
CK
1391/*
1392 * amdgpu_vm_update_directories - make sure that all directories are valid
1393 *
1394 * @adev: amdgpu_device pointer
1395 * @vm: requested vm
1396 *
1397 * Makes sure all directories are up to date.
7fc48e59
AG
1398 *
1399 * Returns:
1400 * 0 for success, error for failure.
194d2161
CK
1401 */
1402int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1403 struct amdgpu_vm *vm)
1404{
6989f246
CK
1405 struct amdgpu_pte_update_params params;
1406 struct amdgpu_job *job;
1407 unsigned ndw = 0;
78aa02c7 1408 int r = 0;
92456b93 1409
6989f246
CK
1410 if (list_empty(&vm->relocated))
1411 return 0;
1412
1413restart:
1414 memset(&params, 0, sizeof(params));
1415 params.adev = adev;
1416
1417 if (vm->use_cpu_for_update) {
1418 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
1419 if (unlikely(r))
1420 return r;
1421
1422 params.func = amdgpu_vm_cpu_set_ptes;
1423 } else {
1424 ndw = 512 * 8;
1425 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1426 if (r)
1427 return r;
1428
1429 params.ib = &job->ibs[0];
1430 params.func = amdgpu_vm_do_set_ptes;
1431 }
1432
ea09729c 1433 while (!list_empty(&vm->relocated)) {
6989f246 1434 struct amdgpu_vm_pt *pt, *entry;
ea09729c 1435
ba79fde4
CK
1436 entry = list_first_entry(&vm->relocated, struct amdgpu_vm_pt,
1437 base.vm_status);
1438 amdgpu_vm_bo_idle(&entry->base);
ea09729c 1439
ba79fde4
CK
1440 pt = amdgpu_vm_pt_parent(entry);
1441 if (!pt)
6989f246 1442 continue;
6989f246 1443
6989f246
CK
1444 amdgpu_vm_update_pde(&params, vm, pt, entry);
1445
6989f246
CK
1446 if (!vm->use_cpu_for_update &&
1447 (ndw - params.ib->length_dw) < 32)
1448 break;
ea09729c 1449 }
92456b93 1450
68c62306
CK
1451 if (vm->use_cpu_for_update) {
1452 /* Flush HDP */
1453 mb();
69882565 1454 amdgpu_asic_flush_hdp(adev, NULL);
6989f246
CK
1455 } else if (params.ib->length_dw == 0) {
1456 amdgpu_job_free(job);
1457 } else {
1458 struct amdgpu_bo *root = vm->root.base.bo;
1459 struct amdgpu_ring *ring;
1460 struct dma_fence *fence;
1461
068c3304 1462 ring = container_of(vm->entity.rq->sched, struct amdgpu_ring,
6989f246
CK
1463 sched);
1464
1465 amdgpu_ring_pad_ib(ring, params.ib);
1466 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
1467 AMDGPU_FENCE_OWNER_VM, false);
6989f246 1468 WARN_ON(params.ib->length_dw > ndw);
0e28b10f
CK
1469 r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_VM,
1470 &fence);
6989f246
CK
1471 if (r)
1472 goto error;
1473
1474 amdgpu_bo_fence(root, fence, true);
1475 dma_fence_put(vm->last_update);
1476 vm->last_update = fence;
68c62306
CK
1477 }
1478
6989f246
CK
1479 if (!list_empty(&vm->relocated))
1480 goto restart;
1481
1482 return 0;
1483
1484error:
d4085ea9 1485 amdgpu_vm_invalidate_pds(adev, vm);
6989f246 1486 amdgpu_job_free(job);
92456b93 1487 return r;
194d2161
CK
1488}
1489
cf2f0a37 1490/**
dfcd99f6 1491 * amdgpu_vm_update_huge - figure out parameters for PTE updates
cf2f0a37 1492 *
dfcd99f6 1493 * Make sure to set the right flags for the PTEs at the desired level.
cf2f0a37 1494 */
dfcd99f6
CK
1495static void amdgpu_vm_update_huge(struct amdgpu_pte_update_params *params,
1496 struct amdgpu_bo *bo, unsigned level,
1497 uint64_t pe, uint64_t addr,
1498 unsigned count, uint32_t incr,
1499 uint64_t flags)
cf2f0a37 1500
dfcd99f6
CK
1501{
1502 if (level != AMDGPU_VM_PTB) {
cf2f0a37 1503 flags |= AMDGPU_PDE_PTE;
dfcd99f6 1504 amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags);
cf2f0a37
AD
1505 }
1506
dfcd99f6
CK
1507 amdgpu_vm_update_func(params, bo, pe, addr, count, incr, flags);
1508}
1509
1510/**
1511 * amdgpu_vm_fragment - get fragment for PTEs
1512 *
1513 * @params: see amdgpu_pte_update_params definition
1514 * @start: first PTE to handle
1515 * @end: last PTE to handle
1516 * @flags: hw mapping flags
1517 * @frag: resulting fragment size
1518 * @frag_end: end of this fragment
1519 *
1520 * Returns the first possible fragment for the start and end address.
1521 */
1522static void amdgpu_vm_fragment(struct amdgpu_pte_update_params *params,
1523 uint64_t start, uint64_t end, uint64_t flags,
1524 unsigned int *frag, uint64_t *frag_end)
1525{
1526 /**
1527 * The MC L1 TLB supports variable sized pages, based on a fragment
1528 * field in the PTE. When this field is set to a non-zero value, page
1529 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1530 * flags are considered valid for all PTEs within the fragment range
1531 * and corresponding mappings are assumed to be physically contiguous.
1532 *
1533 * The L1 TLB can store a single PTE for the whole fragment,
1534 * significantly increasing the space available for translation
1535 * caching. This leads to large improvements in throughput when the
1536 * TLB is under pressure.
1537 *
1538 * The L2 TLB distributes small and large fragments into two
1539 * asymmetric partitions. The large fragment cache is significantly
1540 * larger. Thus, we try to use large fragments wherever possible.
1541 * Userspace can support this by aligning virtual base address and
1542 * allocation size to the fragment size.
1b1d5c43
CK
1543 *
1544 * Starting with Vega10 the fragment size only controls the L1. The L2
1545 * is now directly feed with small/huge/giant pages from the walker.
dfcd99f6 1546 */
1b1d5c43
CK
1547 unsigned max_frag;
1548
1549 if (params->adev->asic_type < CHIP_VEGA10)
1550 max_frag = params->adev->vm_manager.fragment_size;
1551 else
1552 max_frag = 31;
dfcd99f6
CK
1553
1554 /* system pages are non continuously */
0c70dd49 1555 if (params->src) {
dfcd99f6
CK
1556 *frag = 0;
1557 *frag_end = end;
ec5207c9 1558 return;
3cc1d3ea 1559 }
cf2f0a37 1560
dfcd99f6
CK
1561 /* This intentionally wraps around if no bit is set */
1562 *frag = min((unsigned)ffs(start) - 1, (unsigned)fls64(end - start) - 1);
1563 if (*frag >= max_frag) {
1564 *frag = max_frag;
1565 *frag_end = end & ~((1ULL << max_frag) - 1);
1566 } else {
1567 *frag_end = start + (1 << *frag);
1568 }
4e2cb640
CK
1569}
1570
d38ceaf9
AD
1571/**
1572 * amdgpu_vm_update_ptes - make sure that page tables are valid
1573 *
29efc4f5 1574 * @params: see amdgpu_pte_update_params definition
d38ceaf9
AD
1575 * @start: start of GPU address range
1576 * @end: end of GPU address range
677131a1 1577 * @dst: destination address to map to, the next dst inside the function
d38ceaf9
AD
1578 * @flags: mapping flags
1579 *
8843dbbb 1580 * Update the page tables in the range @start - @end.
7fc48e59
AG
1581 *
1582 * Returns:
1583 * 0 for success, -EINVAL for failure.
d38ceaf9 1584 */
cc28c4ed 1585static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
dfcd99f6
CK
1586 uint64_t start, uint64_t end,
1587 uint64_t dst, uint64_t flags)
d38ceaf9 1588{
36b32a68 1589 struct amdgpu_device *adev = params->adev;
dfa70550 1590 struct amdgpu_vm_pt_cursor cursor;
dfcd99f6
CK
1591 uint64_t frag_start = start, frag_end;
1592 unsigned int frag;
1593
1594 /* figure out the initial fragment */
1595 amdgpu_vm_fragment(params, frag_start, end, flags, &frag, &frag_end);
d38ceaf9 1596
dfcd99f6
CK
1597 /* walk over the address space and update the PTs */
1598 amdgpu_vm_pt_start(adev, params->vm, start, &cursor);
1599 while (cursor.pfn < end) {
dfa70550 1600 struct amdgpu_bo *pt = cursor.entry->base.bo;
dfcd99f6
CK
1601 unsigned shift, parent_shift, num_entries;
1602 uint64_t incr, entry_end, pe_start;
cf2f0a37 1603
dfcd99f6 1604 if (!pt)
cf2f0a37 1605 return -ENOENT;
4e2cb640 1606
dfcd99f6
CK
1607 /* The root level can't be a huge page */
1608 if (cursor.level == adev->vm_manager.root_level) {
1609 if (!amdgpu_vm_pt_descendant(adev, &cursor))
1610 return -ENOENT;
cf2f0a37 1611 continue;
dfa70550 1612 }
cf2f0a37 1613
dfcd99f6
CK
1614 /* First check if the entry is already handled */
1615 if (cursor.pfn < frag_start) {
1616 cursor.entry->huge = true;
1617 amdgpu_vm_pt_next(adev, &cursor);
1618 continue;
1619 }
92696dd5 1620
dfcd99f6
CK
1621 /* If it isn't already handled it can't be a huge page */
1622 if (cursor.entry->huge) {
1623 /* Add the entry to the relocated list to update it. */
1624 cursor.entry->huge = false;
1625 amdgpu_vm_bo_relocated(&cursor.entry->base);
1626 }
92696dd5 1627
dfcd99f6
CK
1628 shift = amdgpu_vm_level_shift(adev, cursor.level);
1629 parent_shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
1630 if (adev->asic_type < CHIP_VEGA10) {
1631 /* No huge page support before GMC v9 */
1632 if (cursor.level != AMDGPU_VM_PTB) {
1633 if (!amdgpu_vm_pt_descendant(adev, &cursor))
1634 return -ENOENT;
1635 continue;
1636 }
1637 } else if (frag < shift) {
1638 /* We can't use this level when the fragment size is
1639 * smaller than the address shift. Go to the next
1640 * child entry and try again.
1641 */
1642 if (!amdgpu_vm_pt_descendant(adev, &cursor))
1643 return -ENOENT;
1644 continue;
1645 } else if (frag >= parent_shift) {
1646 /* If the fragment size is even larger than the parent
1647 * shift we should go up one level and check it again.
1648 */
1649 if (!amdgpu_vm_pt_ancestor(&cursor))
1650 return -ENOENT;
1651 continue;
6849d47c
RH
1652 }
1653
dfcd99f6
CK
1654 /* Looks good so far, calculate parameters for the update */
1655 incr = AMDGPU_GPU_PAGE_SIZE << shift;
1656 num_entries = amdgpu_vm_num_entries(adev, cursor.level);
1657 pe_start = ((cursor.pfn >> shift) & (num_entries - 1)) * 8;
1658 entry_end = num_entries << shift;
1659 entry_end += cursor.pfn & ~(entry_end - 1);
1660 entry_end = min(entry_end, end);
1661
1662 do {
1663 uint64_t upd_end = min(entry_end, frag_end);
1664 unsigned nptes = (upd_end - frag_start) >> shift;
1665
1666 amdgpu_vm_update_huge(params, pt, cursor.level,
1667 pe_start, dst, nptes, incr,
1668 flags | AMDGPU_PTE_FRAG(frag));
1669
1670 pe_start += nptes * 8;
1671 dst += nptes * AMDGPU_GPU_PAGE_SIZE << shift;
1672
1673 frag_start = upd_end;
1674 if (frag_start >= frag_end) {
1675 /* figure out the next fragment */
1676 amdgpu_vm_fragment(params, frag_start, end,
1677 flags, &frag, &frag_end);
1678 if (frag < shift)
1679 break;
1680 }
1681 } while (frag_start < entry_end);
92696dd5 1682
dfcd99f6
CK
1683 if (frag >= shift)
1684 amdgpu_vm_pt_next(adev, &cursor);
92696dd5 1685 }
6849d47c
RH
1686
1687 return 0;
d38ceaf9
AD
1688}
1689
d38ceaf9
AD
1690/**
1691 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1692 *
1693 * @adev: amdgpu_device pointer
3cabaa54 1694 * @exclusive: fence we need to sync to
fa3ab3c7 1695 * @pages_addr: DMA addresses to use for mapping
d38ceaf9 1696 * @vm: requested vm
a14faa65
CK
1697 * @start: start of mapped range
1698 * @last: last mapped entry
1699 * @flags: flags for the entries
d38ceaf9 1700 * @addr: addr to set the area to
d38ceaf9
AD
1701 * @fence: optional resulting fence
1702 *
a14faa65 1703 * Fill in the page table entries between @start and @last.
7fc48e59
AG
1704 *
1705 * Returns:
1706 * 0 for success, -EINVAL for failure.
d38ceaf9
AD
1707 */
1708static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
f54d1867 1709 struct dma_fence *exclusive,
fa3ab3c7 1710 dma_addr_t *pages_addr,
d38ceaf9 1711 struct amdgpu_vm *vm,
a14faa65 1712 uint64_t start, uint64_t last,
6b777607 1713 uint64_t flags, uint64_t addr,
f54d1867 1714 struct dma_fence **fence)
d38ceaf9 1715{
2d55e45a 1716 struct amdgpu_ring *ring;
a1e08d3b 1717 void *owner = AMDGPU_FENCE_OWNER_VM;
d38ceaf9 1718 unsigned nptes, ncmds, ndw;
d71518b5 1719 struct amdgpu_job *job;
29efc4f5 1720 struct amdgpu_pte_update_params params;
f54d1867 1721 struct dma_fence *f = NULL;
d38ceaf9
AD
1722 int r;
1723
afef8b8f
CK
1724 memset(&params, 0, sizeof(params));
1725 params.adev = adev;
49ac8a24 1726 params.vm = vm;
afef8b8f 1727
a33cab7a
CK
1728 /* sync to everything on unmapping */
1729 if (!(flags & AMDGPU_PTE_VALID))
1730 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1731
b4d42511
HK
1732 if (vm->use_cpu_for_update) {
1733 /* params.src is used as flag to indicate system Memory */
1734 if (pages_addr)
1735 params.src = ~0;
1736
1737 /* Wait for PT BOs to be free. PTs share the same resv. object
1738 * as the root PD BO
1739 */
a33cab7a 1740 r = amdgpu_vm_wait_pd(adev, vm, owner);
b4d42511
HK
1741 if (unlikely(r))
1742 return r;
1743
1744 params.func = amdgpu_vm_cpu_set_ptes;
1745 params.pages_addr = pages_addr;
dfcd99f6
CK
1746 return amdgpu_vm_update_ptes(&params, start, last + 1,
1747 addr, flags);
b4d42511
HK
1748 }
1749
068c3304 1750 ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
27c5f36f 1751
a14faa65 1752 nptes = last - start + 1;
d38ceaf9
AD
1753
1754 /*
86209523 1755 * reserve space for two commands every (1 << BLOCK_SIZE)
d38ceaf9 1756 * entries or 2k dwords (whatever is smaller)
86209523
BN
1757 *
1758 * The second command is for the shadow pagetables.
d38ceaf9 1759 */
104bd2ca
ED
1760 if (vm->root.base.bo->shadow)
1761 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1762 else
1763 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
d38ceaf9
AD
1764
1765 /* padding, etc. */
1766 ndw = 64;
1767
570144c6 1768 if (pages_addr) {
b0456f93 1769 /* copy commands needed */
e6d92197 1770 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
d38ceaf9 1771
b0456f93 1772 /* and also PTEs */
d38ceaf9
AD
1773 ndw += nptes * 2;
1774
afef8b8f
CK
1775 params.func = amdgpu_vm_do_copy_ptes;
1776
d38ceaf9
AD
1777 } else {
1778 /* set page commands needed */
44e1baeb 1779 ndw += ncmds * 10;
d38ceaf9 1780
6849d47c 1781 /* extra commands for begin/end fragments */
11528640
ED
1782 if (vm->root.base.bo->shadow)
1783 ndw += 2 * 10 * adev->vm_manager.fragment_size * 2;
1784 else
1785 ndw += 2 * 10 * adev->vm_manager.fragment_size;
afef8b8f
CK
1786
1787 params.func = amdgpu_vm_do_set_ptes;
d38ceaf9
AD
1788 }
1789
d71518b5
CK
1790 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1791 if (r)
d38ceaf9 1792 return r;
d71518b5 1793
29efc4f5 1794 params.ib = &job->ibs[0];
d5fc5e82 1795
570144c6 1796 if (pages_addr) {
b0456f93
CK
1797 uint64_t *pte;
1798 unsigned i;
1799
1800 /* Put the PTEs at the end of the IB. */
1801 i = ndw - nptes * 2;
1802 pte= (uint64_t *)&(job->ibs->ptr[i]);
1803 params.src = job->ibs->gpu_addr + i * 4;
1804
1805 for (i = 0; i < nptes; ++i) {
1806 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1807 AMDGPU_GPU_PAGE_SIZE);
1808 pte[i] |= flags;
1809 }
d7a4ac66 1810 addr = 0;
b0456f93
CK
1811 }
1812
cebb52b7 1813 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
3cabaa54
CK
1814 if (r)
1815 goto error_free;
1816
3f3333f8 1817 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
177ae09b 1818 owner, false);
a1e08d3b
CK
1819 if (r)
1820 goto error_free;
d38ceaf9 1821
3f3333f8 1822 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
a1e08d3b
CK
1823 if (r)
1824 goto error_free;
1825
dfcd99f6 1826 r = amdgpu_vm_update_ptes(&params, start, last + 1, addr, flags);
cc28c4ed
HK
1827 if (r)
1828 goto error_free;
d38ceaf9 1829
29efc4f5
CK
1830 amdgpu_ring_pad_ib(ring, params.ib);
1831 WARN_ON(params.ib->length_dw > ndw);
0e28b10f 1832 r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_VM, &f);
4af9f07c
CZ
1833 if (r)
1834 goto error_free;
d38ceaf9 1835
3f3333f8 1836 amdgpu_bo_fence(vm->root.base.bo, f, true);
284710fa
CK
1837 dma_fence_put(*fence);
1838 *fence = f;
d38ceaf9 1839 return 0;
d5fc5e82
CZ
1840
1841error_free:
d71518b5 1842 amdgpu_job_free(job);
4af9f07c 1843 return r;
d38ceaf9
AD
1844}
1845
a14faa65
CK
1846/**
1847 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1848 *
1849 * @adev: amdgpu_device pointer
3cabaa54 1850 * @exclusive: fence we need to sync to
8358dcee 1851 * @pages_addr: DMA addresses to use for mapping
a14faa65
CK
1852 * @vm: requested vm
1853 * @mapping: mapped range and flags to use for the update
8358dcee 1854 * @flags: HW flags for the mapping
63e0ba40 1855 * @nodes: array of drm_mm_nodes with the MC addresses
a14faa65
CK
1856 * @fence: optional resulting fence
1857 *
1858 * Split the mapping into smaller chunks so that each update fits
1859 * into a SDMA IB.
7fc48e59
AG
1860 *
1861 * Returns:
1862 * 0 for success, -EINVAL for failure.
a14faa65
CK
1863 */
1864static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
f54d1867 1865 struct dma_fence *exclusive,
8358dcee 1866 dma_addr_t *pages_addr,
a14faa65
CK
1867 struct amdgpu_vm *vm,
1868 struct amdgpu_bo_va_mapping *mapping,
6b777607 1869 uint64_t flags,
63e0ba40 1870 struct drm_mm_node *nodes,
f54d1867 1871 struct dma_fence **fence)
a14faa65 1872{
9fc8fc70 1873 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
570144c6 1874 uint64_t pfn, start = mapping->start;
a14faa65
CK
1875 int r;
1876
1877 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1878 * but in case of something, we filter the flags in first place
1879 */
1880 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1881 flags &= ~AMDGPU_PTE_READABLE;
1882 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1883 flags &= ~AMDGPU_PTE_WRITEABLE;
1884
15b31c59
AX
1885 flags &= ~AMDGPU_PTE_EXECUTABLE;
1886 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1887
b0fd18b0
AX
1888 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1889 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1890
d0766e98
ZJ
1891 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1892 (adev->asic_type >= CHIP_VEGA10)) {
1893 flags |= AMDGPU_PTE_PRT;
1894 flags &= ~AMDGPU_PTE_VALID;
1895 }
1896
a14faa65
CK
1897 trace_amdgpu_vm_bo_update(mapping);
1898
63e0ba40
CK
1899 pfn = mapping->offset >> PAGE_SHIFT;
1900 if (nodes) {
1901 while (pfn >= nodes->size) {
1902 pfn -= nodes->size;
1903 ++nodes;
1904 }
fa3ab3c7 1905 }
a14faa65 1906
63e0ba40 1907 do {
9fc8fc70 1908 dma_addr_t *dma_addr = NULL;
63e0ba40
CK
1909 uint64_t max_entries;
1910 uint64_t addr, last;
a14faa65 1911
63e0ba40
CK
1912 if (nodes) {
1913 addr = nodes->start << PAGE_SHIFT;
1914 max_entries = (nodes->size - pfn) *
463d2fe8 1915 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
63e0ba40
CK
1916 } else {
1917 addr = 0;
1918 max_entries = S64_MAX;
1919 }
a14faa65 1920
63e0ba40 1921 if (pages_addr) {
9fc8fc70
CK
1922 uint64_t count;
1923
457e0fee 1924 max_entries = min(max_entries, 16ull * 1024ull);
38e624a1 1925 for (count = 1;
463d2fe8 1926 count < max_entries / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
38e624a1 1927 ++count) {
9fc8fc70
CK
1928 uint64_t idx = pfn + count;
1929
1930 if (pages_addr[idx] !=
1931 (pages_addr[idx - 1] + PAGE_SIZE))
1932 break;
1933 }
1934
1935 if (count < min_linear_pages) {
1936 addr = pfn << PAGE_SHIFT;
1937 dma_addr = pages_addr;
1938 } else {
1939 addr = pages_addr[pfn];
463d2fe8 1940 max_entries = count * AMDGPU_GPU_PAGES_IN_CPU_PAGE;
9fc8fc70
CK
1941 }
1942
63e0ba40
CK
1943 } else if (flags & AMDGPU_PTE_VALID) {
1944 addr += adev->vm_manager.vram_base_offset;
9fc8fc70 1945 addr += pfn << PAGE_SHIFT;
63e0ba40 1946 }
63e0ba40 1947
a9f87f64 1948 last = min((uint64_t)mapping->last, start + max_entries - 1);
9fc8fc70 1949 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
a14faa65
CK
1950 start, last, flags, addr,
1951 fence);
1952 if (r)
1953 return r;
1954
463d2fe8 1955 pfn += (last - start + 1) / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
63e0ba40
CK
1956 if (nodes && nodes->size == pfn) {
1957 pfn = 0;
1958 ++nodes;
1959 }
a14faa65 1960 start = last + 1;
63e0ba40 1961
a9f87f64 1962 } while (unlikely(start != mapping->last + 1));
a14faa65
CK
1963
1964 return 0;
1965}
1966
d38ceaf9
AD
1967/**
1968 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1969 *
1970 * @adev: amdgpu_device pointer
1971 * @bo_va: requested BO and VM object
99e124f4 1972 * @clear: if true clear the entries
d38ceaf9
AD
1973 *
1974 * Fill in the page table entries for @bo_va.
7fc48e59
AG
1975 *
1976 * Returns:
1977 * 0 for success, -EINVAL for failure.
d38ceaf9
AD
1978 */
1979int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1980 struct amdgpu_bo_va *bo_va,
99e124f4 1981 bool clear)
d38ceaf9 1982{
ec681545
CK
1983 struct amdgpu_bo *bo = bo_va->base.bo;
1984 struct amdgpu_vm *vm = bo_va->base.vm;
d38ceaf9 1985 struct amdgpu_bo_va_mapping *mapping;
8358dcee 1986 dma_addr_t *pages_addr = NULL;
99e124f4 1987 struct ttm_mem_reg *mem;
63e0ba40 1988 struct drm_mm_node *nodes;
4e55eb38 1989 struct dma_fence *exclusive, **last_update;
457e0fee 1990 uint64_t flags;
d38ceaf9
AD
1991 int r;
1992
7eb80427 1993 if (clear || !bo) {
99e124f4 1994 mem = NULL;
63e0ba40 1995 nodes = NULL;
99e124f4
CK
1996 exclusive = NULL;
1997 } else {
8358dcee
CK
1998 struct ttm_dma_tt *ttm;
1999
7eb80427 2000 mem = &bo->tbo.mem;
63e0ba40
CK
2001 nodes = mem->mm_node;
2002 if (mem->mem_type == TTM_PL_TT) {
7eb80427 2003 ttm = container_of(bo->tbo.ttm, struct ttm_dma_tt, ttm);
8358dcee 2004 pages_addr = ttm->dma_address;
9ab21462 2005 }
ec681545 2006 exclusive = reservation_object_get_excl(bo->tbo.resv);
d38ceaf9
AD
2007 }
2008
457e0fee 2009 if (bo)
ec681545 2010 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
457e0fee 2011 else
a5f6b5b1 2012 flags = 0x0;
d38ceaf9 2013
4e55eb38
CK
2014 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
2015 last_update = &vm->last_update;
2016 else
2017 last_update = &bo_va->last_pt_update;
2018
3d7d4d3a
CK
2019 if (!clear && bo_va->base.moved) {
2020 bo_va->base.moved = false;
7fc11959 2021 list_splice_init(&bo_va->valids, &bo_va->invalids);
3d7d4d3a 2022
cb7b6ec2
CK
2023 } else if (bo_va->cleared != clear) {
2024 list_splice_init(&bo_va->valids, &bo_va->invalids);
3d7d4d3a 2025 }
7fc11959
CK
2026
2027 list_for_each_entry(mapping, &bo_va->invalids, list) {
457e0fee 2028 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
63e0ba40 2029 mapping, flags, nodes,
4e55eb38 2030 last_update);
d38ceaf9
AD
2031 if (r)
2032 return r;
2033 }
2034
cb7b6ec2
CK
2035 if (vm->use_cpu_for_update) {
2036 /* Flush HDP */
2037 mb();
69882565 2038 amdgpu_asic_flush_hdp(adev, NULL);
d6c10f6b
CK
2039 }
2040
bb475839
JZ
2041 /* If the BO is not in its preferred location add it back to
2042 * the evicted list so that it gets validated again on the
2043 * next command submission.
2044 */
806f043f
CK
2045 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2046 uint32_t mem_type = bo->tbo.mem.mem_type;
2047
2048 if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type)))
bcdc9fd6 2049 amdgpu_vm_bo_evicted(&bo_va->base);
806f043f 2050 else
bcdc9fd6 2051 amdgpu_vm_bo_idle(&bo_va->base);
c12a2ee5 2052 } else {
bcdc9fd6 2053 amdgpu_vm_bo_done(&bo_va->base);
806f043f 2054 }
d38ceaf9 2055
cb7b6ec2
CK
2056 list_splice_init(&bo_va->invalids, &bo_va->valids);
2057 bo_va->cleared = clear;
2058
2059 if (trace_amdgpu_vm_bo_mapping_enabled()) {
2060 list_for_each_entry(mapping, &bo_va->valids, list)
2061 trace_amdgpu_vm_bo_mapping(mapping);
68c62306
CK
2062 }
2063
d38ceaf9
AD
2064 return 0;
2065}
2066
284710fa
CK
2067/**
2068 * amdgpu_vm_update_prt_state - update the global PRT state
7fc48e59
AG
2069 *
2070 * @adev: amdgpu_device pointer
284710fa
CK
2071 */
2072static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
2073{
2074 unsigned long flags;
2075 bool enable;
2076
2077 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
451bc8eb 2078 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
132f34e4 2079 adev->gmc.gmc_funcs->set_prt(adev, enable);
284710fa
CK
2080 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
2081}
2082
451bc8eb 2083/**
4388fc2a 2084 * amdgpu_vm_prt_get - add a PRT user
7fc48e59
AG
2085 *
2086 * @adev: amdgpu_device pointer
451bc8eb
CK
2087 */
2088static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
2089{
132f34e4 2090 if (!adev->gmc.gmc_funcs->set_prt)
4388fc2a
CK
2091 return;
2092
451bc8eb
CK
2093 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
2094 amdgpu_vm_update_prt_state(adev);
2095}
2096
0b15f2fc
CK
2097/**
2098 * amdgpu_vm_prt_put - drop a PRT user
7fc48e59
AG
2099 *
2100 * @adev: amdgpu_device pointer
0b15f2fc
CK
2101 */
2102static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
2103{
451bc8eb 2104 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
0b15f2fc
CK
2105 amdgpu_vm_update_prt_state(adev);
2106}
2107
284710fa 2108/**
451bc8eb 2109 * amdgpu_vm_prt_cb - callback for updating the PRT status
7fc48e59
AG
2110 *
2111 * @fence: fence for the callback
00553cf8 2112 * @_cb: the callback function
284710fa
CK
2113 */
2114static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
2115{
2116 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
2117
0b15f2fc 2118 amdgpu_vm_prt_put(cb->adev);
284710fa
CK
2119 kfree(cb);
2120}
2121
451bc8eb
CK
2122/**
2123 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
7fc48e59
AG
2124 *
2125 * @adev: amdgpu_device pointer
2126 * @fence: fence for the callback
451bc8eb
CK
2127 */
2128static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
2129 struct dma_fence *fence)
2130{
4388fc2a 2131 struct amdgpu_prt_cb *cb;
451bc8eb 2132
132f34e4 2133 if (!adev->gmc.gmc_funcs->set_prt)
4388fc2a
CK
2134 return;
2135
2136 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
451bc8eb
CK
2137 if (!cb) {
2138 /* Last resort when we are OOM */
2139 if (fence)
2140 dma_fence_wait(fence, false);
2141
486a68f5 2142 amdgpu_vm_prt_put(adev);
451bc8eb
CK
2143 } else {
2144 cb->adev = adev;
2145 if (!fence || dma_fence_add_callback(fence, &cb->cb,
2146 amdgpu_vm_prt_cb))
2147 amdgpu_vm_prt_cb(fence, &cb->cb);
2148 }
2149}
2150
284710fa
CK
2151/**
2152 * amdgpu_vm_free_mapping - free a mapping
2153 *
2154 * @adev: amdgpu_device pointer
2155 * @vm: requested vm
2156 * @mapping: mapping to be freed
2157 * @fence: fence of the unmap operation
2158 *
2159 * Free a mapping and make sure we decrease the PRT usage count if applicable.
2160 */
2161static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
2162 struct amdgpu_vm *vm,
2163 struct amdgpu_bo_va_mapping *mapping,
2164 struct dma_fence *fence)
2165{
451bc8eb
CK
2166 if (mapping->flags & AMDGPU_PTE_PRT)
2167 amdgpu_vm_add_prt_cb(adev, fence);
2168 kfree(mapping);
2169}
284710fa 2170
451bc8eb
CK
2171/**
2172 * amdgpu_vm_prt_fini - finish all prt mappings
2173 *
2174 * @adev: amdgpu_device pointer
2175 * @vm: requested vm
2176 *
2177 * Register a cleanup callback to disable PRT support after VM dies.
2178 */
2179static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2180{
3f3333f8 2181 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
451bc8eb
CK
2182 struct dma_fence *excl, **shared;
2183 unsigned i, shared_count;
2184 int r;
0b15f2fc 2185
451bc8eb
CK
2186 r = reservation_object_get_fences_rcu(resv, &excl,
2187 &shared_count, &shared);
2188 if (r) {
2189 /* Not enough memory to grab the fence list, as last resort
2190 * block for all the fences to complete.
2191 */
2192 reservation_object_wait_timeout_rcu(resv, true, false,
2193 MAX_SCHEDULE_TIMEOUT);
2194 return;
284710fa 2195 }
451bc8eb
CK
2196
2197 /* Add a callback for each fence in the reservation object */
2198 amdgpu_vm_prt_get(adev);
2199 amdgpu_vm_add_prt_cb(adev, excl);
2200
2201 for (i = 0; i < shared_count; ++i) {
2202 amdgpu_vm_prt_get(adev);
2203 amdgpu_vm_add_prt_cb(adev, shared[i]);
2204 }
2205
2206 kfree(shared);
284710fa
CK
2207}
2208
d38ceaf9
AD
2209/**
2210 * amdgpu_vm_clear_freed - clear freed BOs in the PT
2211 *
2212 * @adev: amdgpu_device pointer
2213 * @vm: requested vm
f3467818
NH
2214 * @fence: optional resulting fence (unchanged if no work needed to be done
2215 * or if an error occurred)
d38ceaf9
AD
2216 *
2217 * Make sure all freed BOs are cleared in the PT.
d38ceaf9 2218 * PTs have to be reserved and mutex must be locked!
7fc48e59
AG
2219 *
2220 * Returns:
2221 * 0 for success.
2222 *
d38ceaf9
AD
2223 */
2224int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
f3467818
NH
2225 struct amdgpu_vm *vm,
2226 struct dma_fence **fence)
d38ceaf9
AD
2227{
2228 struct amdgpu_bo_va_mapping *mapping;
4584312d 2229 uint64_t init_pte_value = 0;
f3467818 2230 struct dma_fence *f = NULL;
d38ceaf9
AD
2231 int r;
2232
2233 while (!list_empty(&vm->freed)) {
2234 mapping = list_first_entry(&vm->freed,
2235 struct amdgpu_bo_va_mapping, list);
2236 list_del(&mapping->list);
e17841b9 2237
ad9a5b78
CK
2238 if (vm->pte_support_ats &&
2239 mapping->start < AMDGPU_GMC_HOLE_START)
6d16dac8 2240 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
51ac7eec 2241
570144c6 2242 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
fc6aa33d 2243 mapping->start, mapping->last,
51ac7eec 2244 init_pte_value, 0, &f);
f3467818 2245 amdgpu_vm_free_mapping(adev, vm, mapping, f);
284710fa 2246 if (r) {
f3467818 2247 dma_fence_put(f);
d38ceaf9 2248 return r;
284710fa 2249 }
f3467818 2250 }
d38ceaf9 2251
f3467818
NH
2252 if (fence && f) {
2253 dma_fence_put(*fence);
2254 *fence = f;
2255 } else {
2256 dma_fence_put(f);
d38ceaf9 2257 }
f3467818 2258
d38ceaf9
AD
2259 return 0;
2260
2261}
2262
2263/**
73fb16e7 2264 * amdgpu_vm_handle_moved - handle moved BOs in the PT
d38ceaf9
AD
2265 *
2266 * @adev: amdgpu_device pointer
2267 * @vm: requested vm
2268 *
73fb16e7 2269 * Make sure all BOs which are moved are updated in the PTs.
7fc48e59
AG
2270 *
2271 * Returns:
2272 * 0 for success.
d38ceaf9 2273 *
73fb16e7 2274 * PTs have to be reserved!
d38ceaf9 2275 */
73fb16e7 2276int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
4e55eb38 2277 struct amdgpu_vm *vm)
d38ceaf9 2278{
789f3317 2279 struct amdgpu_bo_va *bo_va, *tmp;
c12a2ee5 2280 struct reservation_object *resv;
73fb16e7 2281 bool clear;
789f3317 2282 int r;
d38ceaf9 2283
c12a2ee5
CK
2284 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
2285 /* Per VM BOs never need to bo cleared in the page tables */
2286 r = amdgpu_vm_bo_update(adev, bo_va, false);
2287 if (r)
2288 return r;
2289 }
32b41ac2 2290
c12a2ee5
CK
2291 spin_lock(&vm->invalidated_lock);
2292 while (!list_empty(&vm->invalidated)) {
2293 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
2294 base.vm_status);
2295 resv = bo_va->base.bo->tbo.resv;
2296 spin_unlock(&vm->invalidated_lock);
ec363e0d 2297
ec363e0d 2298 /* Try to reserve the BO to avoid clearing its ptes */
c12a2ee5 2299 if (!amdgpu_vm_debug && reservation_object_trylock(resv))
ec363e0d
CK
2300 clear = false;
2301 /* Somebody else is using the BO right now */
2302 else
2303 clear = true;
73fb16e7
CK
2304
2305 r = amdgpu_vm_bo_update(adev, bo_va, clear);
c12a2ee5 2306 if (r)
d38ceaf9
AD
2307 return r;
2308
c12a2ee5 2309 if (!clear)
ec363e0d 2310 reservation_object_unlock(resv);
c12a2ee5 2311 spin_lock(&vm->invalidated_lock);
d38ceaf9 2312 }
c12a2ee5 2313 spin_unlock(&vm->invalidated_lock);
d38ceaf9 2314
789f3317 2315 return 0;
d38ceaf9
AD
2316}
2317
2318/**
2319 * amdgpu_vm_bo_add - add a bo to a specific vm
2320 *
2321 * @adev: amdgpu_device pointer
2322 * @vm: requested vm
2323 * @bo: amdgpu buffer object
2324 *
8843dbbb 2325 * Add @bo into the requested vm.
d38ceaf9 2326 * Add @bo to the list of bos associated with the vm
7fc48e59
AG
2327 *
2328 * Returns:
2329 * Newly added bo_va or NULL for failure
d38ceaf9
AD
2330 *
2331 * Object has to be reserved!
2332 */
2333struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2334 struct amdgpu_vm *vm,
2335 struct amdgpu_bo *bo)
2336{
2337 struct amdgpu_bo_va *bo_va;
2338
2339 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2340 if (bo_va == NULL) {
2341 return NULL;
2342 }
3f4299be 2343 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
ec681545 2344
d38ceaf9 2345 bo_va->ref_count = 1;
7fc11959
CK
2346 INIT_LIST_HEAD(&bo_va->valids);
2347 INIT_LIST_HEAD(&bo_va->invalids);
32b41ac2 2348
d38ceaf9
AD
2349 return bo_va;
2350}
2351
73fb16e7
CK
2352
2353/**
2354 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2355 *
2356 * @adev: amdgpu_device pointer
2357 * @bo_va: bo_va to store the address
2358 * @mapping: the mapping to insert
2359 *
2360 * Insert a new mapping into all structures.
2361 */
2362static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2363 struct amdgpu_bo_va *bo_va,
2364 struct amdgpu_bo_va_mapping *mapping)
2365{
2366 struct amdgpu_vm *vm = bo_va->base.vm;
2367 struct amdgpu_bo *bo = bo_va->base.bo;
2368
aebc5e6f 2369 mapping->bo_va = bo_va;
73fb16e7
CK
2370 list_add(&mapping->list, &bo_va->invalids);
2371 amdgpu_vm_it_insert(mapping, &vm->va);
2372
2373 if (mapping->flags & AMDGPU_PTE_PRT)
2374 amdgpu_vm_prt_get(adev);
2375
862b8c57
CK
2376 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
2377 !bo_va->base.moved) {
862b8c57 2378 list_move(&bo_va->base.vm_status, &vm->moved);
73fb16e7
CK
2379 }
2380 trace_amdgpu_vm_bo_map(bo_va, mapping);
2381}
2382
d38ceaf9
AD
2383/**
2384 * amdgpu_vm_bo_map - map bo inside a vm
2385 *
2386 * @adev: amdgpu_device pointer
2387 * @bo_va: bo_va to store the address
2388 * @saddr: where to map the BO
2389 * @offset: requested offset in the BO
00553cf8 2390 * @size: BO size in bytes
d38ceaf9
AD
2391 * @flags: attributes of pages (read/write/valid/etc.)
2392 *
2393 * Add a mapping of the BO at the specefied addr into the VM.
7fc48e59
AG
2394 *
2395 * Returns:
2396 * 0 for success, error for failure.
d38ceaf9 2397 *
49b02b18 2398 * Object has to be reserved and unreserved outside!
d38ceaf9
AD
2399 */
2400int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2401 struct amdgpu_bo_va *bo_va,
2402 uint64_t saddr, uint64_t offset,
268c3001 2403 uint64_t size, uint64_t flags)
d38ceaf9 2404{
a9f87f64 2405 struct amdgpu_bo_va_mapping *mapping, *tmp;
ec681545
CK
2406 struct amdgpu_bo *bo = bo_va->base.bo;
2407 struct amdgpu_vm *vm = bo_va->base.vm;
d38ceaf9 2408 uint64_t eaddr;
d38ceaf9 2409
0be52de9
CK
2410 /* validate the parameters */
2411 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
49b02b18 2412 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
0be52de9 2413 return -EINVAL;
0be52de9 2414
d38ceaf9 2415 /* make sure object fit at this offset */
005ae95e 2416 eaddr = saddr + size - 1;
a5f6b5b1 2417 if (saddr >= eaddr ||
ec681545 2418 (bo && offset + size > amdgpu_bo_size(bo)))
d38ceaf9 2419 return -EINVAL;
d38ceaf9 2420
d38ceaf9
AD
2421 saddr /= AMDGPU_GPU_PAGE_SIZE;
2422 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2423
a9f87f64
CK
2424 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2425 if (tmp) {
d38ceaf9
AD
2426 /* bo and tmp overlap, invalid addr */
2427 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
ec681545 2428 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
a9f87f64 2429 tmp->start, tmp->last + 1);
663e4577 2430 return -EINVAL;
d38ceaf9
AD
2431 }
2432
2433 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
663e4577
CK
2434 if (!mapping)
2435 return -ENOMEM;
d38ceaf9 2436
a9f87f64
CK
2437 mapping->start = saddr;
2438 mapping->last = eaddr;
d38ceaf9
AD
2439 mapping->offset = offset;
2440 mapping->flags = flags;
2441
73fb16e7 2442 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
80f95c57
CK
2443
2444 return 0;
2445}
2446
2447/**
2448 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2449 *
2450 * @adev: amdgpu_device pointer
2451 * @bo_va: bo_va to store the address
2452 * @saddr: where to map the BO
2453 * @offset: requested offset in the BO
00553cf8 2454 * @size: BO size in bytes
80f95c57
CK
2455 * @flags: attributes of pages (read/write/valid/etc.)
2456 *
2457 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2458 * mappings as we do so.
7fc48e59
AG
2459 *
2460 * Returns:
2461 * 0 for success, error for failure.
80f95c57
CK
2462 *
2463 * Object has to be reserved and unreserved outside!
2464 */
2465int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2466 struct amdgpu_bo_va *bo_va,
2467 uint64_t saddr, uint64_t offset,
2468 uint64_t size, uint64_t flags)
2469{
2470 struct amdgpu_bo_va_mapping *mapping;
ec681545 2471 struct amdgpu_bo *bo = bo_va->base.bo;
80f95c57
CK
2472 uint64_t eaddr;
2473 int r;
2474
2475 /* validate the parameters */
2476 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2477 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2478 return -EINVAL;
2479
2480 /* make sure object fit at this offset */
2481 eaddr = saddr + size - 1;
2482 if (saddr >= eaddr ||
ec681545 2483 (bo && offset + size > amdgpu_bo_size(bo)))
80f95c57
CK
2484 return -EINVAL;
2485
2486 /* Allocate all the needed memory */
2487 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2488 if (!mapping)
2489 return -ENOMEM;
2490
ec681545 2491 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
80f95c57
CK
2492 if (r) {
2493 kfree(mapping);
2494 return r;
2495 }
2496
2497 saddr /= AMDGPU_GPU_PAGE_SIZE;
2498 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2499
a9f87f64
CK
2500 mapping->start = saddr;
2501 mapping->last = eaddr;
80f95c57
CK
2502 mapping->offset = offset;
2503 mapping->flags = flags;
2504
73fb16e7 2505 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
4388fc2a 2506
d38ceaf9 2507 return 0;
d38ceaf9
AD
2508}
2509
2510/**
2511 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2512 *
2513 * @adev: amdgpu_device pointer
2514 * @bo_va: bo_va to remove the address from
2515 * @saddr: where to the BO is mapped
2516 *
2517 * Remove a mapping of the BO at the specefied addr from the VM.
7fc48e59
AG
2518 *
2519 * Returns:
2520 * 0 for success, error for failure.
d38ceaf9 2521 *
49b02b18 2522 * Object has to be reserved and unreserved outside!
d38ceaf9
AD
2523 */
2524int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2525 struct amdgpu_bo_va *bo_va,
2526 uint64_t saddr)
2527{
2528 struct amdgpu_bo_va_mapping *mapping;
ec681545 2529 struct amdgpu_vm *vm = bo_va->base.vm;
7fc11959 2530 bool valid = true;
d38ceaf9 2531
6c7fc503 2532 saddr /= AMDGPU_GPU_PAGE_SIZE;
32b41ac2 2533
7fc11959 2534 list_for_each_entry(mapping, &bo_va->valids, list) {
a9f87f64 2535 if (mapping->start == saddr)
d38ceaf9
AD
2536 break;
2537 }
2538
7fc11959
CK
2539 if (&mapping->list == &bo_va->valids) {
2540 valid = false;
2541
2542 list_for_each_entry(mapping, &bo_va->invalids, list) {
a9f87f64 2543 if (mapping->start == saddr)
7fc11959
CK
2544 break;
2545 }
2546
32b41ac2 2547 if (&mapping->list == &bo_va->invalids)
7fc11959 2548 return -ENOENT;
d38ceaf9 2549 }
32b41ac2 2550
d38ceaf9 2551 list_del(&mapping->list);
a9f87f64 2552 amdgpu_vm_it_remove(mapping, &vm->va);
aebc5e6f 2553 mapping->bo_va = NULL;
93e3e438 2554 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
d38ceaf9 2555
e17841b9 2556 if (valid)
d38ceaf9 2557 list_add(&mapping->list, &vm->freed);
e17841b9 2558 else
284710fa
CK
2559 amdgpu_vm_free_mapping(adev, vm, mapping,
2560 bo_va->last_pt_update);
d38ceaf9
AD
2561
2562 return 0;
2563}
2564
dc54d3d1
CK
2565/**
2566 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2567 *
2568 * @adev: amdgpu_device pointer
2569 * @vm: VM structure to use
2570 * @saddr: start of the range
2571 * @size: size of the range
2572 *
2573 * Remove all mappings in a range, split them as appropriate.
7fc48e59
AG
2574 *
2575 * Returns:
2576 * 0 for success, error for failure.
dc54d3d1
CK
2577 */
2578int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2579 struct amdgpu_vm *vm,
2580 uint64_t saddr, uint64_t size)
2581{
2582 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
dc54d3d1
CK
2583 LIST_HEAD(removed);
2584 uint64_t eaddr;
2585
2586 eaddr = saddr + size - 1;
2587 saddr /= AMDGPU_GPU_PAGE_SIZE;
2588 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2589
2590 /* Allocate all the needed memory */
2591 before = kzalloc(sizeof(*before), GFP_KERNEL);
2592 if (!before)
2593 return -ENOMEM;
27f6d610 2594 INIT_LIST_HEAD(&before->list);
dc54d3d1
CK
2595
2596 after = kzalloc(sizeof(*after), GFP_KERNEL);
2597 if (!after) {
2598 kfree(before);
2599 return -ENOMEM;
2600 }
27f6d610 2601 INIT_LIST_HEAD(&after->list);
dc54d3d1
CK
2602
2603 /* Now gather all removed mappings */
a9f87f64
CK
2604 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2605 while (tmp) {
dc54d3d1 2606 /* Remember mapping split at the start */
a9f87f64
CK
2607 if (tmp->start < saddr) {
2608 before->start = tmp->start;
2609 before->last = saddr - 1;
dc54d3d1
CK
2610 before->offset = tmp->offset;
2611 before->flags = tmp->flags;
387f49e5
JZ
2612 before->bo_va = tmp->bo_va;
2613 list_add(&before->list, &tmp->bo_va->invalids);
dc54d3d1
CK
2614 }
2615
2616 /* Remember mapping split at the end */
a9f87f64
CK
2617 if (tmp->last > eaddr) {
2618 after->start = eaddr + 1;
2619 after->last = tmp->last;
dc54d3d1 2620 after->offset = tmp->offset;
a9f87f64 2621 after->offset += after->start - tmp->start;
dc54d3d1 2622 after->flags = tmp->flags;
387f49e5
JZ
2623 after->bo_va = tmp->bo_va;
2624 list_add(&after->list, &tmp->bo_va->invalids);
dc54d3d1
CK
2625 }
2626
2627 list_del(&tmp->list);
2628 list_add(&tmp->list, &removed);
a9f87f64
CK
2629
2630 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
dc54d3d1
CK
2631 }
2632
2633 /* And free them up */
2634 list_for_each_entry_safe(tmp, next, &removed, list) {
a9f87f64 2635 amdgpu_vm_it_remove(tmp, &vm->va);
dc54d3d1
CK
2636 list_del(&tmp->list);
2637
a9f87f64
CK
2638 if (tmp->start < saddr)
2639 tmp->start = saddr;
2640 if (tmp->last > eaddr)
2641 tmp->last = eaddr;
dc54d3d1 2642
aebc5e6f 2643 tmp->bo_va = NULL;
dc54d3d1
CK
2644 list_add(&tmp->list, &vm->freed);
2645 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2646 }
2647
27f6d610
JZ
2648 /* Insert partial mapping before the range */
2649 if (!list_empty(&before->list)) {
a9f87f64 2650 amdgpu_vm_it_insert(before, &vm->va);
dc54d3d1
CK
2651 if (before->flags & AMDGPU_PTE_PRT)
2652 amdgpu_vm_prt_get(adev);
2653 } else {
2654 kfree(before);
2655 }
2656
2657 /* Insert partial mapping after the range */
27f6d610 2658 if (!list_empty(&after->list)) {
a9f87f64 2659 amdgpu_vm_it_insert(after, &vm->va);
dc54d3d1
CK
2660 if (after->flags & AMDGPU_PTE_PRT)
2661 amdgpu_vm_prt_get(adev);
2662 } else {
2663 kfree(after);
2664 }
2665
2666 return 0;
2667}
2668
aebc5e6f
CK
2669/**
2670 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2671 *
2672 * @vm: the requested VM
00553cf8 2673 * @addr: the address
aebc5e6f
CK
2674 *
2675 * Find a mapping by it's address.
7fc48e59
AG
2676 *
2677 * Returns:
2678 * The amdgpu_bo_va_mapping matching for addr or NULL
2679 *
aebc5e6f
CK
2680 */
2681struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2682 uint64_t addr)
2683{
2684 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2685}
2686
8ab19ea6
CK
2687/**
2688 * amdgpu_vm_bo_trace_cs - trace all reserved mappings
2689 *
2690 * @vm: the requested vm
2691 * @ticket: CS ticket
2692 *
2693 * Trace all mappings of BOs reserved during a command submission.
2694 */
2695void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket)
2696{
2697 struct amdgpu_bo_va_mapping *mapping;
2698
2699 if (!trace_amdgpu_vm_bo_cs_enabled())
2700 return;
2701
2702 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping;
2703 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) {
2704 if (mapping->bo_va && mapping->bo_va->base.bo) {
2705 struct amdgpu_bo *bo;
2706
2707 bo = mapping->bo_va->base.bo;
2708 if (READ_ONCE(bo->tbo.resv->lock.ctx) != ticket)
2709 continue;
2710 }
2711
2712 trace_amdgpu_vm_bo_cs(mapping);
2713 }
2714}
2715
d38ceaf9
AD
2716/**
2717 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2718 *
2719 * @adev: amdgpu_device pointer
2720 * @bo_va: requested bo_va
2721 *
8843dbbb 2722 * Remove @bo_va->bo from the requested vm.
d38ceaf9
AD
2723 *
2724 * Object have to be reserved!
2725 */
2726void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2727 struct amdgpu_bo_va *bo_va)
2728{
2729 struct amdgpu_bo_va_mapping *mapping, *next;
fbbf794c 2730 struct amdgpu_bo *bo = bo_va->base.bo;
ec681545 2731 struct amdgpu_vm *vm = bo_va->base.vm;
d38ceaf9 2732
fbbf794c
CK
2733 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv)
2734 vm->bulk_moveable = false;
2735
ec681545 2736 list_del(&bo_va->base.bo_list);
d38ceaf9 2737
c12a2ee5 2738 spin_lock(&vm->invalidated_lock);
ec681545 2739 list_del(&bo_va->base.vm_status);
c12a2ee5 2740 spin_unlock(&vm->invalidated_lock);
d38ceaf9 2741
7fc11959 2742 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
d38ceaf9 2743 list_del(&mapping->list);
a9f87f64 2744 amdgpu_vm_it_remove(mapping, &vm->va);
aebc5e6f 2745 mapping->bo_va = NULL;
93e3e438 2746 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
7fc11959
CK
2747 list_add(&mapping->list, &vm->freed);
2748 }
2749 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2750 list_del(&mapping->list);
a9f87f64 2751 amdgpu_vm_it_remove(mapping, &vm->va);
284710fa
CK
2752 amdgpu_vm_free_mapping(adev, vm, mapping,
2753 bo_va->last_pt_update);
d38ceaf9 2754 }
32b41ac2 2755
f54d1867 2756 dma_fence_put(bo_va->last_pt_update);
d38ceaf9 2757 kfree(bo_va);
d38ceaf9
AD
2758}
2759
2760/**
2761 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2762 *
2763 * @adev: amdgpu_device pointer
d38ceaf9 2764 * @bo: amdgpu buffer object
00553cf8 2765 * @evicted: is the BO evicted
d38ceaf9 2766 *
8843dbbb 2767 * Mark @bo as invalid.
d38ceaf9
AD
2768 */
2769void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
3f3333f8 2770 struct amdgpu_bo *bo, bool evicted)
d38ceaf9 2771{
ec681545
CK
2772 struct amdgpu_vm_bo_base *bo_base;
2773
4bebccee
CZ
2774 /* shadow bo doesn't have bo base, its validation needs its parent */
2775 if (bo->parent && bo->parent->shadow == bo)
2776 bo = bo->parent;
2777
ec681545 2778 list_for_each_entry(bo_base, &bo->va, bo_list) {
3f3333f8
CK
2779 struct amdgpu_vm *vm = bo_base->vm;
2780
3f3333f8 2781 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
bcdc9fd6 2782 amdgpu_vm_bo_evicted(bo_base);
3f3333f8
CK
2783 continue;
2784 }
2785
bcdc9fd6 2786 if (bo_base->moved)
3f3333f8 2787 continue;
bcdc9fd6 2788 bo_base->moved = true;
3f3333f8 2789
bcdc9fd6
CK
2790 if (bo->tbo.type == ttm_bo_type_kernel)
2791 amdgpu_vm_bo_relocated(bo_base);
2792 else if (bo->tbo.resv == vm->root.base.bo->tbo.resv)
2793 amdgpu_vm_bo_moved(bo_base);
2794 else
2795 amdgpu_vm_bo_invalidated(bo_base);
d38ceaf9
AD
2796 }
2797}
2798
7fc48e59
AG
2799/**
2800 * amdgpu_vm_get_block_size - calculate VM page table size as power of two
2801 *
2802 * @vm_size: VM size
2803 *
2804 * Returns:
2805 * VM page table as power of two
2806 */
bab4fee7
JZ
2807static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2808{
2809 /* Total bits covered by PD + PTs */
2810 unsigned bits = ilog2(vm_size) + 18;
2811
2812 /* Make sure the PD is 4K in size up to 8GB address space.
2813 Above that split equal between PD and PTs */
2814 if (vm_size <= 8)
2815 return (bits - 9);
2816 else
2817 return ((bits + 3) / 2);
2818}
2819
d07f14be
RH
2820/**
2821 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
bab4fee7
JZ
2822 *
2823 * @adev: amdgpu_device pointer
43370c4c 2824 * @min_vm_size: the minimum vm size in GB if it's set auto
00553cf8
AG
2825 * @fragment_size_default: Default PTE fragment size
2826 * @max_level: max VMPT level
2827 * @max_bits: max address space size in bits
2828 *
bab4fee7 2829 */
43370c4c 2830void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
f3368128
CK
2831 uint32_t fragment_size_default, unsigned max_level,
2832 unsigned max_bits)
bab4fee7 2833{
43370c4c
FK
2834 unsigned int max_size = 1 << (max_bits - 30);
2835 unsigned int vm_size;
36539dce
CK
2836 uint64_t tmp;
2837
2838 /* adjust vm size first */
f3368128 2839 if (amdgpu_vm_size != -1) {
fdd5faaa 2840 vm_size = amdgpu_vm_size;
f3368128
CK
2841 if (vm_size > max_size) {
2842 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2843 amdgpu_vm_size, max_size);
2844 vm_size = max_size;
2845 }
43370c4c
FK
2846 } else {
2847 struct sysinfo si;
2848 unsigned int phys_ram_gb;
2849
2850 /* Optimal VM size depends on the amount of physical
2851 * RAM available. Underlying requirements and
2852 * assumptions:
2853 *
2854 * - Need to map system memory and VRAM from all GPUs
2855 * - VRAM from other GPUs not known here
2856 * - Assume VRAM <= system memory
2857 * - On GFX8 and older, VM space can be segmented for
2858 * different MTYPEs
2859 * - Need to allow room for fragmentation, guard pages etc.
2860 *
2861 * This adds up to a rough guess of system memory x3.
2862 * Round up to power of two to maximize the available
2863 * VM size with the given page table size.
2864 */
2865 si_meminfo(&si);
2866 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit +
2867 (1 << 30) - 1) >> 30;
2868 vm_size = roundup_pow_of_two(
2869 min(max(phys_ram_gb * 3, min_vm_size), max_size));
f3368128 2870 }
fdd5faaa
CK
2871
2872 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
36539dce
CK
2873
2874 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
97489129
CK
2875 if (amdgpu_vm_block_size != -1)
2876 tmp >>= amdgpu_vm_block_size - 9;
36539dce
CK
2877 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2878 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
196f7489
CZ
2879 switch (adev->vm_manager.num_level) {
2880 case 3:
2881 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2882 break;
2883 case 2:
2884 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2885 break;
2886 case 1:
2887 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2888 break;
2889 default:
2890 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2891 }
b38f41eb 2892 /* block size depends on vm size and hw setup*/
97489129 2893 if (amdgpu_vm_block_size != -1)
bab4fee7 2894 adev->vm_manager.block_size =
97489129
CK
2895 min((unsigned)amdgpu_vm_block_size, max_bits
2896 - AMDGPU_GPU_PAGE_SHIFT
2897 - 9 * adev->vm_manager.num_level);
2898 else if (adev->vm_manager.num_level > 1)
2899 adev->vm_manager.block_size = 9;
bab4fee7 2900 else
97489129 2901 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
bab4fee7 2902
b38f41eb
CK
2903 if (amdgpu_vm_fragment_size == -1)
2904 adev->vm_manager.fragment_size = fragment_size_default;
2905 else
2906 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
d07f14be 2907
36539dce
CK
2908 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2909 vm_size, adev->vm_manager.num_level + 1,
2910 adev->vm_manager.block_size,
fdd5faaa 2911 adev->vm_manager.fragment_size);
bab4fee7
JZ
2912}
2913
240cd9a6
OZ
2914static struct amdgpu_retryfault_hashtable *init_fault_hash(void)
2915{
2916 struct amdgpu_retryfault_hashtable *fault_hash;
2917
2918 fault_hash = kmalloc(sizeof(*fault_hash), GFP_KERNEL);
2919 if (!fault_hash)
2920 return fault_hash;
2921
2922 INIT_CHASH_TABLE(fault_hash->hash,
2923 AMDGPU_PAGEFAULT_HASH_BITS, 8, 0);
2924 spin_lock_init(&fault_hash->lock);
2925 fault_hash->count = 0;
2926
2927 return fault_hash;
2928}
2929
d38ceaf9
AD
2930/**
2931 * amdgpu_vm_init - initialize a vm instance
2932 *
2933 * @adev: amdgpu_device pointer
2934 * @vm: requested vm
9a4b7d4c 2935 * @vm_context: Indicates if it GFX or Compute context
00553cf8 2936 * @pasid: Process address space identifier
d38ceaf9 2937 *
8843dbbb 2938 * Init @vm fields.
7fc48e59
AG
2939 *
2940 * Returns:
2941 * 0 for success, error for failure.
d38ceaf9 2942 */
9a4b7d4c 2943int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
02208441 2944 int vm_context, unsigned int pasid)
d38ceaf9 2945{
3216c6b7 2946 struct amdgpu_bo_param bp;
3f4299be 2947 struct amdgpu_bo *root;
36bbf3bf 2948 int r, i;
d38ceaf9 2949
f808c13f 2950 vm->va = RB_ROOT_CACHED;
36bbf3bf
CZ
2951 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2952 vm->reserved_vmid[i] = NULL;
3f3333f8 2953 INIT_LIST_HEAD(&vm->evicted);
ea09729c 2954 INIT_LIST_HEAD(&vm->relocated);
27c7b9ae 2955 INIT_LIST_HEAD(&vm->moved);
806f043f 2956 INIT_LIST_HEAD(&vm->idle);
c12a2ee5
CK
2957 INIT_LIST_HEAD(&vm->invalidated);
2958 spin_lock_init(&vm->invalidated_lock);
d38ceaf9 2959 INIT_LIST_HEAD(&vm->freed);
20250215 2960
2bd9ccfa 2961 /* create scheduler entity for page table updates */
3798e9a6
CK
2962 r = drm_sched_entity_init(&vm->entity, adev->vm_manager.vm_pte_rqs,
2963 adev->vm_manager.vm_pte_num_rqs, NULL);
2bd9ccfa 2964 if (r)
f566ceb1 2965 return r;
2bd9ccfa 2966
51ac7eec
YZ
2967 vm->pte_support_ats = false;
2968
2969 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
9a4b7d4c
HK
2970 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2971 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
51ac7eec 2972
13307f7e 2973 if (adev->asic_type == CHIP_RAVEN)
51ac7eec 2974 vm->pte_support_ats = true;
13307f7e 2975 } else {
9a4b7d4c
HK
2976 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2977 AMDGPU_VM_USE_CPU_FOR_GFX);
13307f7e 2978 }
9a4b7d4c
HK
2979 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2980 vm->use_cpu_for_update ? "CPU" : "SDMA");
c8c5e569 2981 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_gmc_vram_full_visible(&adev->gmc)),
9a4b7d4c 2982 "CPU update of VM recommended only for large BAR system\n");
d5884513 2983 vm->last_update = NULL;
05906dec 2984
e21eb261 2985 amdgpu_vm_bo_param(adev, vm, adev->vm_manager.root_level, &bp);
03e9dee1
FK
2986 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE)
2987 bp.flags &= ~AMDGPU_GEM_CREATE_SHADOW;
3f4299be 2988 r = amdgpu_bo_create(adev, &bp, &root);
d38ceaf9 2989 if (r)
2bd9ccfa
CK
2990 goto error_free_sched_entity;
2991
3f4299be 2992 r = amdgpu_bo_reserve(root, true);
d3aab672
CK
2993 if (r)
2994 goto error_free_root;
2995
3f4299be 2996 r = amdgpu_vm_clear_bo(adev, vm, root,
4584312d
CK
2997 adev->vm_manager.root_level,
2998 vm->pte_support_ats);
13307f7e
CK
2999 if (r)
3000 goto error_unreserve;
3001
3f4299be 3002 amdgpu_vm_bo_base_init(&vm->root.base, vm, root);
d3aab672 3003 amdgpu_bo_unreserve(vm->root.base.bo);
d38ceaf9 3004
02208441
FK
3005 if (pasid) {
3006 unsigned long flags;
3007
3008 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3009 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
3010 GFP_ATOMIC);
3011 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3012 if (r < 0)
3013 goto error_free_root;
3014
3015 vm->pasid = pasid;
0a096fb6
CK
3016 }
3017
240cd9a6
OZ
3018 vm->fault_hash = init_fault_hash();
3019 if (!vm->fault_hash) {
3020 r = -ENOMEM;
3021 goto error_free_root;
3022 }
3023
a2f14820 3024 INIT_KFIFO(vm->faults);
c98171cc 3025 vm->fault_credit = 16;
d38ceaf9
AD
3026
3027 return 0;
2bd9ccfa 3028
13307f7e
CK
3029error_unreserve:
3030 amdgpu_bo_unreserve(vm->root.base.bo);
3031
67003a15 3032error_free_root:
3f3333f8
CK
3033 amdgpu_bo_unref(&vm->root.base.bo->shadow);
3034 amdgpu_bo_unref(&vm->root.base.bo);
3035 vm->root.base.bo = NULL;
2bd9ccfa
CK
3036
3037error_free_sched_entity:
cdc50176 3038 drm_sched_entity_destroy(&vm->entity);
2bd9ccfa
CK
3039
3040 return r;
d38ceaf9
AD
3041}
3042
b236fa1d
FK
3043/**
3044 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
3045 *
7fc48e59
AG
3046 * @adev: amdgpu_device pointer
3047 * @vm: requested vm
3048 *
b236fa1d
FK
3049 * This only works on GFX VMs that don't have any BOs added and no
3050 * page tables allocated yet.
3051 *
3052 * Changes the following VM parameters:
3053 * - use_cpu_for_update
3054 * - pte_supports_ats
3055 * - pasid (old PASID is released, because compute manages its own PASIDs)
3056 *
3057 * Reinitializes the page directory to reflect the changed ATS
b5d21aac 3058 * setting.
b236fa1d 3059 *
7fc48e59
AG
3060 * Returns:
3061 * 0 for success, -errno for errors.
b236fa1d 3062 */
1685b01a 3063int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm, unsigned int pasid)
b236fa1d
FK
3064{
3065 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
3066 int r;
3067
3068 r = amdgpu_bo_reserve(vm->root.base.bo, true);
3069 if (r)
3070 return r;
3071
3072 /* Sanity checks */
3073 if (!RB_EMPTY_ROOT(&vm->va.rb_root) || vm->root.entries) {
3074 r = -EINVAL;
1685b01a
OZ
3075 goto unreserve_bo;
3076 }
3077
3078 if (pasid) {
3079 unsigned long flags;
3080
3081 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3082 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
3083 GFP_ATOMIC);
3084 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3085
3086 if (r == -ENOSPC)
3087 goto unreserve_bo;
3088 r = 0;
b236fa1d
FK
3089 }
3090
3091 /* Check if PD needs to be reinitialized and do it before
3092 * changing any other state, in case it fails.
3093 */
3094 if (pte_support_ats != vm->pte_support_ats) {
3095 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
3096 adev->vm_manager.root_level,
3097 pte_support_ats);
3098 if (r)
1685b01a 3099 goto free_idr;
b236fa1d
FK
3100 }
3101
3102 /* Update VM state */
3103 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
3104 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
3105 vm->pte_support_ats = pte_support_ats;
3106 DRM_DEBUG_DRIVER("VM update mode is %s\n",
3107 vm->use_cpu_for_update ? "CPU" : "SDMA");
c8c5e569 3108 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_gmc_vram_full_visible(&adev->gmc)),
b236fa1d
FK
3109 "CPU update of VM recommended only for large BAR system\n");
3110
3111 if (vm->pasid) {
3112 unsigned long flags;
3113
3114 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3115 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
3116 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3117
1685b01a
OZ
3118 /* Free the original amdgpu allocated pasid
3119 * Will be replaced with kfd allocated pasid
3120 */
3121 amdgpu_pasid_free(vm->pasid);
b236fa1d
FK
3122 vm->pasid = 0;
3123 }
3124
b5d21aac
SL
3125 /* Free the shadow bo for compute VM */
3126 amdgpu_bo_unref(&vm->root.base.bo->shadow);
3127
1685b01a
OZ
3128 if (pasid)
3129 vm->pasid = pasid;
3130
3131 goto unreserve_bo;
3132
3133free_idr:
3134 if (pasid) {
3135 unsigned long flags;
3136
3137 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3138 idr_remove(&adev->vm_manager.pasid_idr, pasid);
3139 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3140 }
3141unreserve_bo:
b236fa1d
FK
3142 amdgpu_bo_unreserve(vm->root.base.bo);
3143 return r;
3144}
3145
bf47afba
OZ
3146/**
3147 * amdgpu_vm_release_compute - release a compute vm
3148 * @adev: amdgpu_device pointer
3149 * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute
3150 *
3151 * This is a correspondant of amdgpu_vm_make_compute. It decouples compute
3152 * pasid from vm. Compute should stop use of vm after this call.
3153 */
3154void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
3155{
3156 if (vm->pasid) {
3157 unsigned long flags;
3158
3159 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3160 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
3161 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3162 }
3163 vm->pasid = 0;
3164}
3165
d38ceaf9
AD
3166/**
3167 * amdgpu_vm_fini - tear down a vm instance
3168 *
3169 * @adev: amdgpu_device pointer
3170 * @vm: requested vm
3171 *
8843dbbb 3172 * Tear down @vm.
d38ceaf9
AD
3173 * Unbind the VM and remove all bos from the vm bo list
3174 */
3175void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
3176{
3177 struct amdgpu_bo_va_mapping *mapping, *tmp;
132f34e4 3178 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2642cf11 3179 struct amdgpu_bo *root;
a2f14820 3180 u64 fault;
2642cf11 3181 int i, r;
d38ceaf9 3182
ede0dd86
FK
3183 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
3184
a2f14820
FK
3185 /* Clear pending page faults from IH when the VM is destroyed */
3186 while (kfifo_get(&vm->faults, &fault))
240cd9a6 3187 amdgpu_vm_clear_fault(vm->fault_hash, fault);
a2f14820 3188
02208441
FK
3189 if (vm->pasid) {
3190 unsigned long flags;
3191
3192 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3193 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
3194 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3195 }
3196
240cd9a6
OZ
3197 kfree(vm->fault_hash);
3198 vm->fault_hash = NULL;
3199
cdc50176 3200 drm_sched_entity_destroy(&vm->entity);
2bd9ccfa 3201
f808c13f 3202 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
d38ceaf9
AD
3203 dev_err(adev->dev, "still active bo inside vm\n");
3204 }
f808c13f
DB
3205 rbtree_postorder_for_each_entry_safe(mapping, tmp,
3206 &vm->va.rb_root, rb) {
d38ceaf9 3207 list_del(&mapping->list);
a9f87f64 3208 amdgpu_vm_it_remove(mapping, &vm->va);
d38ceaf9
AD
3209 kfree(mapping);
3210 }
3211 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
4388fc2a 3212 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
451bc8eb 3213 amdgpu_vm_prt_fini(adev, vm);
4388fc2a 3214 prt_fini_needed = false;
451bc8eb 3215 }
284710fa 3216
d38ceaf9 3217 list_del(&mapping->list);
451bc8eb 3218 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
d38ceaf9
AD
3219 }
3220
2642cf11
CK
3221 root = amdgpu_bo_ref(vm->root.base.bo);
3222 r = amdgpu_bo_reserve(root, true);
3223 if (r) {
3224 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
3225 } else {
229a37f8 3226 amdgpu_vm_free_pts(adev, vm);
2642cf11
CK
3227 amdgpu_bo_unreserve(root);
3228 }
3229 amdgpu_bo_unref(&root);
d5884513 3230 dma_fence_put(vm->last_update);
1e9ef26f 3231 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
620f774f 3232 amdgpu_vmid_free_reserved(adev, vm, i);
d38ceaf9 3233}
ea89f8c9 3234
c98171cc
FK
3235/**
3236 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
3237 *
3238 * @adev: amdgpu_device pointer
3239 * @pasid: PASID do identify the VM
3240 *
7fc48e59
AG
3241 * This function is expected to be called in interrupt context.
3242 *
3243 * Returns:
3244 * True if there was fault credit, false otherwise
c98171cc
FK
3245 */
3246bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
3247 unsigned int pasid)
3248{
3249 struct amdgpu_vm *vm;
3250
3251 spin_lock(&adev->vm_manager.pasid_lock);
3252 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
d958939a 3253 if (!vm) {
c98171cc 3254 /* VM not found, can't track fault credit */
d958939a 3255 spin_unlock(&adev->vm_manager.pasid_lock);
c98171cc 3256 return true;
d958939a 3257 }
c98171cc
FK
3258
3259 /* No lock needed. only accessed by IRQ handler */
d958939a 3260 if (!vm->fault_credit) {
c98171cc 3261 /* Too many faults in this VM */
d958939a 3262 spin_unlock(&adev->vm_manager.pasid_lock);
c98171cc 3263 return false;
d958939a 3264 }
c98171cc
FK
3265
3266 vm->fault_credit--;
d958939a 3267 spin_unlock(&adev->vm_manager.pasid_lock);
c98171cc
FK
3268 return true;
3269}
3270
a9a78b32
CK
3271/**
3272 * amdgpu_vm_manager_init - init the VM manager
3273 *
3274 * @adev: amdgpu_device pointer
3275 *
3276 * Initialize the VM manager structures
3277 */
3278void amdgpu_vm_manager_init(struct amdgpu_device *adev)
3279{
620f774f 3280 unsigned i;
a9a78b32 3281
620f774f 3282 amdgpu_vmid_mgr_init(adev);
2d55e45a 3283
f54d1867
CW
3284 adev->vm_manager.fence_context =
3285 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
1fbb2e92
CK
3286 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
3287 adev->vm_manager.seqno[i] = 0;
3288
284710fa 3289 spin_lock_init(&adev->vm_manager.prt_lock);
451bc8eb 3290 atomic_set(&adev->vm_manager.num_prt_users, 0);
9a4b7d4c
HK
3291
3292 /* If not overridden by the user, by default, only in large BAR systems
3293 * Compute VM tables will be updated by CPU
3294 */
3295#ifdef CONFIG_X86_64
3296 if (amdgpu_vm_update_mode == -1) {
c8c5e569 3297 if (amdgpu_gmc_vram_full_visible(&adev->gmc))
9a4b7d4c
HK
3298 adev->vm_manager.vm_update_mode =
3299 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
3300 else
3301 adev->vm_manager.vm_update_mode = 0;
3302 } else
3303 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
3304#else
3305 adev->vm_manager.vm_update_mode = 0;
3306#endif
3307
02208441
FK
3308 idr_init(&adev->vm_manager.pasid_idr);
3309 spin_lock_init(&adev->vm_manager.pasid_lock);
a9a78b32
CK
3310}
3311
ea89f8c9
CK
3312/**
3313 * amdgpu_vm_manager_fini - cleanup VM manager
3314 *
3315 * @adev: amdgpu_device pointer
3316 *
3317 * Cleanup the VM manager and free resources.
3318 */
3319void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
3320{
02208441
FK
3321 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
3322 idr_destroy(&adev->vm_manager.pasid_idr);
3323
620f774f 3324 amdgpu_vmid_mgr_fini(adev);
ea89f8c9 3325}
cfbcacf4 3326
7fc48e59
AG
3327/**
3328 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
3329 *
3330 * @dev: drm device pointer
3331 * @data: drm_amdgpu_vm
3332 * @filp: drm file pointer
3333 *
3334 * Returns:
3335 * 0 for success, -errno for errors.
3336 */
cfbcacf4
CZ
3337int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
3338{
3339 union drm_amdgpu_vm *args = data;
1e9ef26f
CZ
3340 struct amdgpu_device *adev = dev->dev_private;
3341 struct amdgpu_fpriv *fpriv = filp->driver_priv;
3342 int r;
cfbcacf4
CZ
3343
3344 switch (args->in.op) {
3345 case AMDGPU_VM_OP_RESERVE_VMID:
1e9ef26f 3346 /* current, we only have requirement to reserve vmid from gfxhub */
620f774f 3347 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
1e9ef26f
CZ
3348 if (r)
3349 return r;
3350 break;
cfbcacf4 3351 case AMDGPU_VM_OP_UNRESERVE_VMID:
620f774f 3352 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
cfbcacf4
CZ
3353 break;
3354 default:
3355 return -EINVAL;
3356 }
3357
3358 return 0;
3359}
2aa37bf5
AG
3360
3361/**
3362 * amdgpu_vm_get_task_info - Extracts task info for a PASID.
3363 *
989edc69 3364 * @adev: drm device pointer
2aa37bf5
AG
3365 * @pasid: PASID identifier for VM
3366 * @task_info: task_info to fill.
3367 */
3368void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid,
3369 struct amdgpu_task_info *task_info)
3370{
3371 struct amdgpu_vm *vm;
3372
3373 spin_lock(&adev->vm_manager.pasid_lock);
3374
3375 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
3376 if (vm)
3377 *task_info = vm->task_info;
3378
3379 spin_unlock(&adev->vm_manager.pasid_lock);
3380}
3381
3382/**
3383 * amdgpu_vm_set_task_info - Sets VMs task info.
3384 *
3385 * @vm: vm for which to set the info
3386 */
3387void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
3388{
3389 if (!vm->task_info.pid) {
3390 vm->task_info.pid = current->pid;
3391 get_task_comm(vm->task_info.task_name, current);
3392
3393 if (current->group_leader->mm == current->mm) {
3394 vm->task_info.tgid = current->group_leader->pid;
3395 get_task_comm(vm->task_info.process_name, current->group_leader);
3396 }
3397 }
3398}
240cd9a6
OZ
3399
3400/**
3401 * amdgpu_vm_add_fault - Add a page fault record to fault hash table
3402 *
3403 * @fault_hash: fault hash table
3404 * @key: 64-bit encoding of PASID and address
3405 *
3406 * This should be called when a retry page fault interrupt is
3407 * received. If this is a new page fault, it will be added to a hash
3408 * table. The return value indicates whether this is a new fault, or
3409 * a fault that was already known and is already being handled.
3410 *
3411 * If there are too many pending page faults, this will fail. Retry
3412 * interrupts should be ignored in this case until there is enough
3413 * free space.
3414 *
3415 * Returns 0 if the fault was added, 1 if the fault was already known,
3416 * -ENOSPC if there are too many pending faults.
3417 */
3418int amdgpu_vm_add_fault(struct amdgpu_retryfault_hashtable *fault_hash, u64 key)
3419{
3420 unsigned long flags;
3421 int r = -ENOSPC;
3422
3423 if (WARN_ON_ONCE(!fault_hash))
3424 /* Should be allocated in amdgpu_vm_init
3425 */
3426 return r;
3427
3428 spin_lock_irqsave(&fault_hash->lock, flags);
3429
3430 /* Only let the hash table fill up to 50% for best performance */
3431 if (fault_hash->count >= (1 << (AMDGPU_PAGEFAULT_HASH_BITS-1)))
3432 goto unlock_out;
3433
3434 r = chash_table_copy_in(&fault_hash->hash, key, NULL);
3435 if (!r)
3436 fault_hash->count++;
3437
3438 /* chash_table_copy_in should never fail unless we're losing count */
3439 WARN_ON_ONCE(r < 0);
3440
3441unlock_out:
3442 spin_unlock_irqrestore(&fault_hash->lock, flags);
3443 return r;
3444}
3445
3446/**
3447 * amdgpu_vm_clear_fault - Remove a page fault record
3448 *
3449 * @fault_hash: fault hash table
3450 * @key: 64-bit encoding of PASID and address
3451 *
3452 * This should be called when a page fault has been handled. Any
3453 * future interrupt with this key will be processed as a new
3454 * page fault.
3455 */
3456void amdgpu_vm_clear_fault(struct amdgpu_retryfault_hashtable *fault_hash, u64 key)
3457{
3458 unsigned long flags;
3459 int r;
3460
3461 if (!fault_hash)
3462 return;
3463
3464 spin_lock_irqsave(&fault_hash->lock, flags);
3465
3466 r = chash_table_remove(&fault_hash->hash, key, NULL);
3467 if (!WARN_ON_ONCE(r < 0)) {
3468 fault_hash->count--;
3469 WARN_ON_ONCE(fault_hash->count < 0);
3470 }
3471
3472 spin_unlock_irqrestore(&fault_hash->lock, flags);
3473}