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