xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c (revision 1c824497d8acd3187d585d6187cedc1897dcc871)
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  */
28 
29 #include <linux/dma-fence-array.h>
30 #include <linux/interval_tree_generic.h>
31 #include <linux/idr.h>
32 #include <linux/dma-buf.h>
33 
34 #include <drm/amdgpu_drm.h>
35 #include <drm/drm_drv.h>
36 #include <drm/ttm/ttm_tt.h>
37 #include <drm/drm_exec.h>
38 #include "amdgpu.h"
39 #include "amdgpu_vm.h"
40 #include "amdgpu_trace.h"
41 #include "amdgpu_amdkfd.h"
42 #include "amdgpu_gmc.h"
43 #include "amdgpu_xgmi.h"
44 #include "amdgpu_dma_buf.h"
45 #include "amdgpu_res_cursor.h"
46 #include "kfd_svm.h"
47 
48 /**
49  * DOC: GPUVM
50  *
51  * GPUVM is the MMU functionality provided on the GPU.
52  * GPUVM is similar to the legacy GART on older asics, however
53  * rather than there being a single global GART table
54  * for the entire GPU, there can be multiple GPUVM page tables active
55  * at any given time.  The GPUVM page tables can contain a mix
56  * VRAM pages and system pages (both memory and MMIO) and system pages
57  * can be mapped as snooped (cached system pages) or unsnooped
58  * (uncached system pages).
59  *
60  * Each active GPUVM has an ID associated with it and there is a page table
61  * linked with each VMID.  When executing a command buffer,
62  * the kernel tells the engine what VMID to use for that command
63  * buffer.  VMIDs are allocated dynamically as commands are submitted.
64  * The userspace drivers maintain their own address space and the kernel
65  * sets up their pages tables accordingly when they submit their
66  * command buffers and a VMID is assigned.
67  * The hardware supports up to 16 active GPUVMs at any given time.
68  *
69  * Each GPUVM is represented by a 1-2 or 1-5 level page table, depending
70  * on the ASIC family.  GPUVM supports RWX attributes on each page as well
71  * as other features such as encryption and caching attributes.
72  *
73  * VMID 0 is special.  It is the GPUVM used for the kernel driver.  In
74  * addition to an aperture managed by a page table, VMID 0 also has
75  * several other apertures.  There is an aperture for direct access to VRAM
76  * and there is a legacy AGP aperture which just forwards accesses directly
77  * to the matching system physical addresses (or IOVAs when an IOMMU is
78  * present).  These apertures provide direct access to these memories without
79  * incurring the overhead of a page table.  VMID 0 is used by the kernel
80  * driver for tasks like memory management.
81  *
82  * GPU clients (i.e., engines on the GPU) use GPUVM VMIDs to access memory.
83  * For user applications, each application can have their own unique GPUVM
84  * address space.  The application manages the address space and the kernel
85  * driver manages the GPUVM page tables for each process.  If an GPU client
86  * accesses an invalid page, it will generate a GPU page fault, similar to
87  * accessing an invalid page on a CPU.
88  */
89 
90 #define START(node) ((node)->start)
91 #define LAST(node) ((node)->last)
92 
93 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
94 		     START, LAST, static, amdgpu_vm_it)
95 
96 #undef START
97 #undef LAST
98 
99 /**
100  * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
101  */
102 struct amdgpu_prt_cb {
103 
104 	/**
105 	 * @adev: amdgpu device
106 	 */
107 	struct amdgpu_device *adev;
108 
109 	/**
110 	 * @cb: callback
111 	 */
112 	struct dma_fence_cb cb;
113 };
114 
115 /**
116  * struct amdgpu_vm_tlb_seq_struct - Helper to increment the TLB flush sequence
117  */
118 struct amdgpu_vm_tlb_seq_struct {
119 	/**
120 	 * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
121 	 */
122 	struct amdgpu_vm *vm;
123 
124 	/**
125 	 * @cb: callback
126 	 */
127 	struct dma_fence_cb cb;
128 };
129 
130 /**
131  * amdgpu_vm_assert_locked - check if VM is correctly locked
132  * @vm: the VM which schould be tested
133  *
134  * Asserts that the VM root PD is locked.
135  */
136 static void amdgpu_vm_assert_locked(struct amdgpu_vm *vm)
137 {
138 	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
139 }
140 
141 /**
142  * amdgpu_vm_is_bo_always_valid - check if the BO is VM always valid
143  *
144  * @vm: VM to test against.
145  * @bo: BO to be tested.
146  *
147  * Returns true if the BO shares the dma_resv object with the root PD and is
148  * always guaranteed to be valid inside the VM.
149  */
150 bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo)
151 {
152 	return bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv;
153 }
154 
155 /**
156  * amdgpu_vm_bo_evicted - vm_bo is evicted
157  *
158  * @vm_bo: vm_bo which is evicted
159  *
160  * State for PDs/PTs and per VM BOs which are not at the location they should
161  * be.
162  */
163 static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo)
164 {
165 	struct amdgpu_vm *vm = vm_bo->vm;
166 	struct amdgpu_bo *bo = vm_bo->bo;
167 
168 	vm_bo->moved = true;
169 	amdgpu_vm_assert_locked(vm);
170 	spin_lock(&vm_bo->vm->status_lock);
171 	if (bo->tbo.type == ttm_bo_type_kernel)
172 		list_move(&vm_bo->vm_status, &vm->evicted);
173 	else
174 		list_move_tail(&vm_bo->vm_status, &vm->evicted);
175 	spin_unlock(&vm_bo->vm->status_lock);
176 }
177 /**
178  * amdgpu_vm_bo_moved - vm_bo is moved
179  *
180  * @vm_bo: vm_bo which is moved
181  *
182  * State for per VM BOs which are moved, but that change is not yet reflected
183  * in the page tables.
184  */
185 static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo)
186 {
187 	amdgpu_vm_assert_locked(vm_bo->vm);
188 	spin_lock(&vm_bo->vm->status_lock);
189 	list_move(&vm_bo->vm_status, &vm_bo->vm->moved);
190 	spin_unlock(&vm_bo->vm->status_lock);
191 }
192 
193 /**
194  * amdgpu_vm_bo_idle - vm_bo is idle
195  *
196  * @vm_bo: vm_bo which is now idle
197  *
198  * State for PDs/PTs and per VM BOs which have gone through the state machine
199  * and are now idle.
200  */
201 static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo)
202 {
203 	amdgpu_vm_assert_locked(vm_bo->vm);
204 	spin_lock(&vm_bo->vm->status_lock);
205 	list_move(&vm_bo->vm_status, &vm_bo->vm->idle);
206 	spin_unlock(&vm_bo->vm->status_lock);
207 	vm_bo->moved = false;
208 }
209 
210 /**
211  * amdgpu_vm_bo_invalidated - vm_bo is invalidated
212  *
213  * @vm_bo: vm_bo which is now invalidated
214  *
215  * State for normal BOs which are invalidated and that change not yet reflected
216  * in the PTs.
217  */
218 static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo)
219 {
220 	spin_lock(&vm_bo->vm->status_lock);
221 	list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated);
222 	spin_unlock(&vm_bo->vm->status_lock);
223 }
224 
225 /**
226  * amdgpu_vm_bo_evicted_user - vm_bo is evicted
227  *
228  * @vm_bo: vm_bo which is evicted
229  *
230  * State for BOs used by user mode queues which are not at the location they
231  * should be.
232  */
233 static void amdgpu_vm_bo_evicted_user(struct amdgpu_vm_bo_base *vm_bo)
234 {
235 	vm_bo->moved = true;
236 	spin_lock(&vm_bo->vm->status_lock);
237 	list_move(&vm_bo->vm_status, &vm_bo->vm->evicted_user);
238 	spin_unlock(&vm_bo->vm->status_lock);
239 }
240 
241 /**
242  * amdgpu_vm_bo_relocated - vm_bo is reloacted
243  *
244  * @vm_bo: vm_bo which is relocated
245  *
246  * State for PDs/PTs which needs to update their parent PD.
247  * For the root PD, just move to idle state.
248  */
249 static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo)
250 {
251 	amdgpu_vm_assert_locked(vm_bo->vm);
252 	if (vm_bo->bo->parent) {
253 		spin_lock(&vm_bo->vm->status_lock);
254 		list_move(&vm_bo->vm_status, &vm_bo->vm->relocated);
255 		spin_unlock(&vm_bo->vm->status_lock);
256 	} else {
257 		amdgpu_vm_bo_idle(vm_bo);
258 	}
259 }
260 
261 /**
262  * amdgpu_vm_bo_done - vm_bo is done
263  *
264  * @vm_bo: vm_bo which is now done
265  *
266  * State for normal BOs which are invalidated and that change has been updated
267  * in the PTs.
268  */
269 static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo)
270 {
271 	amdgpu_vm_assert_locked(vm_bo->vm);
272 	spin_lock(&vm_bo->vm->status_lock);
273 	list_move(&vm_bo->vm_status, &vm_bo->vm->done);
274 	spin_unlock(&vm_bo->vm->status_lock);
275 }
276 
277 /**
278  * amdgpu_vm_bo_reset_state_machine - reset the vm_bo state machine
279  * @vm: the VM which state machine to reset
280  *
281  * Move all vm_bo object in the VM into a state where they will be updated
282  * again during validation.
283  */
284 static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)
285 {
286 	struct amdgpu_vm_bo_base *vm_bo, *tmp;
287 
288 	amdgpu_vm_assert_locked(vm);
289 
290 	spin_lock(&vm->status_lock);
291 	list_splice_init(&vm->done, &vm->invalidated);
292 	list_for_each_entry(vm_bo, &vm->invalidated, vm_status)
293 		vm_bo->moved = true;
294 
295 	list_for_each_entry_safe(vm_bo, tmp, &vm->idle, vm_status) {
296 		struct amdgpu_bo *bo = vm_bo->bo;
297 
298 		vm_bo->moved = true;
299 		if (!bo || bo->tbo.type != ttm_bo_type_kernel)
300 			list_move(&vm_bo->vm_status, &vm_bo->vm->moved);
301 		else if (bo->parent)
302 			list_move(&vm_bo->vm_status, &vm_bo->vm->relocated);
303 	}
304 	spin_unlock(&vm->status_lock);
305 }
306 
307 /**
308  * amdgpu_vm_update_shared - helper to update shared memory stat
309  * @base: base structure for tracking BO usage in a VM
310  *
311  * Takes the vm status_lock and updates the shared memory stat. If the basic
312  * stat changed (e.g. buffer was moved) amdgpu_vm_update_stats need to be called
313  * as well.
314  */
315 static void amdgpu_vm_update_shared(struct amdgpu_vm_bo_base *base)
316 {
317 	struct amdgpu_vm *vm = base->vm;
318 	struct amdgpu_bo *bo = base->bo;
319 	uint64_t size = amdgpu_bo_size(bo);
320 	uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
321 	bool shared;
322 
323 	dma_resv_assert_held(bo->tbo.base.resv);
324 	spin_lock(&vm->status_lock);
325 	shared = drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base);
326 	if (base->shared != shared) {
327 		base->shared = shared;
328 		if (shared) {
329 			vm->stats[bo_memtype].drm.shared += size;
330 			vm->stats[bo_memtype].drm.private -= size;
331 		} else {
332 			vm->stats[bo_memtype].drm.shared -= size;
333 			vm->stats[bo_memtype].drm.private += size;
334 		}
335 	}
336 	spin_unlock(&vm->status_lock);
337 }
338 
339 /**
340  * amdgpu_vm_bo_update_shared - callback when bo gets shared/unshared
341  * @bo: amdgpu buffer object
342  *
343  * Update the per VM stats for all the vm if needed from private to shared or
344  * vice versa.
345  */
346 void amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo)
347 {
348 	struct amdgpu_vm_bo_base *base;
349 
350 	for (base = bo->vm_bo; base; base = base->next)
351 		amdgpu_vm_update_shared(base);
352 }
353 
354 /**
355  * amdgpu_vm_update_stats_locked - helper to update normal memory stat
356  * @base: base structure for tracking BO usage in a VM
357  * @res:  the ttm_resource to use for the purpose of accounting, may or may not
358  *        be bo->tbo.resource
359  * @sign: if we should add (+1) or subtract (-1) from the stat
360  *
361  * Caller need to have the vm status_lock held. Useful for when multiple update
362  * need to happen at the same time.
363  */
364 static void amdgpu_vm_update_stats_locked(struct amdgpu_vm_bo_base *base,
365 			    struct ttm_resource *res, int sign)
366 {
367 	struct amdgpu_vm *vm = base->vm;
368 	struct amdgpu_bo *bo = base->bo;
369 	int64_t size = sign * amdgpu_bo_size(bo);
370 	uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
371 
372 	/* For drm-total- and drm-shared-, BO are accounted by their preferred
373 	 * placement, see also amdgpu_bo_mem_stats_placement.
374 	 */
375 	if (base->shared)
376 		vm->stats[bo_memtype].drm.shared += size;
377 	else
378 		vm->stats[bo_memtype].drm.private += size;
379 
380 	if (res && res->mem_type < __AMDGPU_PL_NUM) {
381 		uint32_t res_memtype = res->mem_type;
382 
383 		vm->stats[res_memtype].drm.resident += size;
384 		/* BO only count as purgeable if it is resident,
385 		 * since otherwise there's nothing to purge.
386 		 */
387 		if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
388 			vm->stats[res_memtype].drm.purgeable += size;
389 		if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(res_memtype)))
390 			vm->stats[bo_memtype].evicted += size;
391 	}
392 }
393 
394 /**
395  * amdgpu_vm_update_stats - helper to update normal memory stat
396  * @base: base structure for tracking BO usage in a VM
397  * @res:  the ttm_resource to use for the purpose of accounting, may or may not
398  *        be bo->tbo.resource
399  * @sign: if we should add (+1) or subtract (-1) from the stat
400  *
401  * Updates the basic memory stat when bo is added/deleted/moved.
402  */
403 void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
404 			    struct ttm_resource *res, int sign)
405 {
406 	struct amdgpu_vm *vm = base->vm;
407 
408 	spin_lock(&vm->status_lock);
409 	amdgpu_vm_update_stats_locked(base, res, sign);
410 	spin_unlock(&vm->status_lock);
411 }
412 
413 /**
414  * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
415  *
416  * @base: base structure for tracking BO usage in a VM
417  * @vm: vm to which bo is to be added
418  * @bo: amdgpu buffer object
419  *
420  * Initialize a bo_va_base structure and add it to the appropriate lists
421  *
422  */
423 void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
424 			    struct amdgpu_vm *vm, struct amdgpu_bo *bo)
425 {
426 	base->vm = vm;
427 	base->bo = bo;
428 	base->next = NULL;
429 	INIT_LIST_HEAD(&base->vm_status);
430 
431 	if (!bo)
432 		return;
433 	base->next = bo->vm_bo;
434 	bo->vm_bo = base;
435 
436 	spin_lock(&vm->status_lock);
437 	base->shared = drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base);
438 	amdgpu_vm_update_stats_locked(base, bo->tbo.resource, +1);
439 	spin_unlock(&vm->status_lock);
440 
441 	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
442 		return;
443 
444 	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
445 
446 	ttm_bo_set_bulk_move(&bo->tbo, &vm->lru_bulk_move);
447 	if (bo->tbo.type == ttm_bo_type_kernel && bo->parent)
448 		amdgpu_vm_bo_relocated(base);
449 	else
450 		amdgpu_vm_bo_idle(base);
451 
452 	if (bo->preferred_domains &
453 	    amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type))
454 		return;
455 
456 	/*
457 	 * we checked all the prerequisites, but it looks like this per vm bo
458 	 * is currently evicted. add the bo to the evicted list to make sure it
459 	 * is validated on next vm use to avoid fault.
460 	 * */
461 	amdgpu_vm_bo_evicted(base);
462 }
463 
464 /**
465  * amdgpu_vm_lock_pd - lock PD in drm_exec
466  *
467  * @vm: vm providing the BOs
468  * @exec: drm execution context
469  * @num_fences: number of extra fences to reserve
470  *
471  * Lock the VM root PD in the DRM execution context.
472  */
473 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec,
474 		      unsigned int num_fences)
475 {
476 	/* We need at least two fences for the VM PD/PT updates */
477 	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base,
478 				    2 + num_fences);
479 }
480 
481 /**
482  * amdgpu_vm_lock_done_list - lock all BOs on the done list
483  * @vm: vm providing the BOs
484  * @exec: drm execution context
485  * @num_fences: number of extra fences to reserve
486  *
487  * Lock the BOs on the done list in the DRM execution context.
488  */
489 int amdgpu_vm_lock_done_list(struct amdgpu_vm *vm, struct drm_exec *exec,
490 			     unsigned int num_fences)
491 {
492 	struct list_head *prev = &vm->done;
493 	struct amdgpu_bo_va *bo_va;
494 	struct amdgpu_bo *bo;
495 	int ret;
496 
497 	/* We can only trust prev->next while holding the lock */
498 	spin_lock(&vm->status_lock);
499 	while (!list_is_head(prev->next, &vm->done)) {
500 		bo_va = list_entry(prev->next, typeof(*bo_va), base.vm_status);
501 
502 		bo = bo_va->base.bo;
503 		if (bo) {
504 			amdgpu_bo_ref(bo);
505 			spin_unlock(&vm->status_lock);
506 
507 			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 1);
508 			amdgpu_bo_unref(&bo);
509 			if (unlikely(ret))
510 				return ret;
511 
512 			spin_lock(&vm->status_lock);
513 		}
514 		prev = prev->next;
515 	}
516 	spin_unlock(&vm->status_lock);
517 
518 	return 0;
519 }
520 
521 /**
522  * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
523  *
524  * @adev: amdgpu device pointer
525  * @vm: vm providing the BOs
526  *
527  * Move all BOs to the end of LRU and remember their positions to put them
528  * together.
529  */
530 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
531 				struct amdgpu_vm *vm)
532 {
533 	spin_lock(&adev->mman.bdev.lru_lock);
534 	ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
535 	spin_unlock(&adev->mman.bdev.lru_lock);
536 }
537 
538 /* Create scheduler entities for page table updates */
539 static int amdgpu_vm_init_entities(struct amdgpu_device *adev,
540 				   struct amdgpu_vm *vm)
541 {
542 	int r;
543 
544 	r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL,
545 				  adev->vm_manager.vm_pte_scheds,
546 				  adev->vm_manager.vm_pte_num_scheds, NULL);
547 	if (r)
548 		goto error;
549 
550 	return drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL,
551 				     adev->vm_manager.vm_pte_scheds,
552 				     adev->vm_manager.vm_pte_num_scheds, NULL);
553 
554 error:
555 	drm_sched_entity_destroy(&vm->immediate);
556 	return r;
557 }
558 
559 /* Destroy the entities for page table updates again */
560 static void amdgpu_vm_fini_entities(struct amdgpu_vm *vm)
561 {
562 	drm_sched_entity_destroy(&vm->immediate);
563 	drm_sched_entity_destroy(&vm->delayed);
564 }
565 
566 /**
567  * amdgpu_vm_generation - return the page table re-generation counter
568  * @adev: the amdgpu_device
569  * @vm: optional VM to check, might be NULL
570  *
571  * Returns a page table re-generation token to allow checking if submissions
572  * are still valid to use this VM. The VM parameter might be NULL in which case
573  * just the VRAM lost counter will be used.
574  */
575 uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm)
576 {
577 	uint64_t result = (u64)atomic_read(&adev->vram_lost_counter) << 32;
578 
579 	if (!vm)
580 		return result;
581 
582 	result += lower_32_bits(vm->generation);
583 	/* Add one if the page tables will be re-generated on next CS */
584 	if (drm_sched_entity_error(&vm->delayed))
585 		++result;
586 
587 	return result;
588 }
589 
590 /**
591  * amdgpu_vm_validate - validate evicted BOs tracked in the VM
592  *
593  * @adev: amdgpu device pointer
594  * @vm: vm providing the BOs
595  * @ticket: optional reservation ticket used to reserve the VM
596  * @validate: callback to do the validation
597  * @param: parameter for the validation callback
598  *
599  * Validate the page table BOs and per-VM BOs on command submission if
600  * necessary. If a ticket is given, also try to validate evicted user queue
601  * BOs. They must already be reserved with the given ticket.
602  *
603  * Returns:
604  * Validation result.
605  */
606 int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
607 		       struct ww_acquire_ctx *ticket,
608 		       int (*validate)(void *p, struct amdgpu_bo *bo),
609 		       void *param)
610 {
611 	uint64_t new_vm_generation = amdgpu_vm_generation(adev, vm);
612 	struct amdgpu_vm_bo_base *bo_base;
613 	struct amdgpu_bo *bo;
614 	int r;
615 
616 	if (vm->generation != new_vm_generation) {
617 		vm->generation = new_vm_generation;
618 		amdgpu_vm_bo_reset_state_machine(vm);
619 		amdgpu_vm_fini_entities(vm);
620 		r = amdgpu_vm_init_entities(adev, vm);
621 		if (r)
622 			return r;
623 	}
624 
625 	spin_lock(&vm->status_lock);
626 	while (!list_empty(&vm->evicted)) {
627 		bo_base = list_first_entry(&vm->evicted,
628 					   struct amdgpu_vm_bo_base,
629 					   vm_status);
630 		spin_unlock(&vm->status_lock);
631 
632 		bo = bo_base->bo;
633 
634 		r = validate(param, bo);
635 		if (r)
636 			return r;
637 
638 		if (bo->tbo.type != ttm_bo_type_kernel) {
639 			amdgpu_vm_bo_moved(bo_base);
640 		} else {
641 			vm->update_funcs->map_table(to_amdgpu_bo_vm(bo));
642 			amdgpu_vm_bo_relocated(bo_base);
643 		}
644 		spin_lock(&vm->status_lock);
645 	}
646 	while (ticket && !list_empty(&vm->evicted_user)) {
647 		bo_base = list_first_entry(&vm->evicted_user,
648 					   struct amdgpu_vm_bo_base,
649 					   vm_status);
650 		spin_unlock(&vm->status_lock);
651 
652 		bo = bo_base->bo;
653 		dma_resv_assert_held(bo->tbo.base.resv);
654 
655 		r = validate(param, bo);
656 		if (r)
657 			return r;
658 
659 		amdgpu_vm_bo_invalidated(bo_base);
660 
661 		spin_lock(&vm->status_lock);
662 	}
663 	spin_unlock(&vm->status_lock);
664 
665 	amdgpu_vm_eviction_lock(vm);
666 	vm->evicting = false;
667 	amdgpu_vm_eviction_unlock(vm);
668 
669 	return 0;
670 }
671 
672 /**
673  * amdgpu_vm_ready - check VM is ready for updates
674  *
675  * @vm: VM to check
676  *
677  * Check if all VM PDs/PTs are ready for updates
678  *
679  * Returns:
680  * True if VM is not evicting and all VM entities are not stopped
681  */
682 bool amdgpu_vm_ready(struct amdgpu_vm *vm)
683 {
684 	bool ret;
685 
686 	amdgpu_vm_assert_locked(vm);
687 
688 	amdgpu_vm_eviction_lock(vm);
689 	ret = !vm->evicting;
690 	amdgpu_vm_eviction_unlock(vm);
691 
692 	spin_lock(&vm->status_lock);
693 	ret &= list_empty(&vm->evicted);
694 	spin_unlock(&vm->status_lock);
695 
696 	spin_lock(&vm->immediate.lock);
697 	ret &= !vm->immediate.stopped;
698 	spin_unlock(&vm->immediate.lock);
699 
700 	spin_lock(&vm->delayed.lock);
701 	ret &= !vm->delayed.stopped;
702 	spin_unlock(&vm->delayed.lock);
703 
704 	return ret;
705 }
706 
707 /**
708  * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
709  *
710  * @adev: amdgpu_device pointer
711  */
712 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
713 {
714 	const struct amdgpu_ip_block *ip_block;
715 	bool has_compute_vm_bug;
716 	struct amdgpu_ring *ring;
717 	int i;
718 
719 	has_compute_vm_bug = false;
720 
721 	ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
722 	if (ip_block) {
723 		/* Compute has a VM bug for GFX version < 7.
724 		   Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
725 		if (ip_block->version->major <= 7)
726 			has_compute_vm_bug = true;
727 		else if (ip_block->version->major == 8)
728 			if (adev->gfx.mec_fw_version < 673)
729 				has_compute_vm_bug = true;
730 	}
731 
732 	for (i = 0; i < adev->num_rings; i++) {
733 		ring = adev->rings[i];
734 		if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
735 			/* only compute rings */
736 			ring->has_compute_vm_bug = has_compute_vm_bug;
737 		else
738 			ring->has_compute_vm_bug = false;
739 	}
740 }
741 
742 /**
743  * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
744  *
745  * @ring: ring on which the job will be submitted
746  * @job: job to submit
747  *
748  * Returns:
749  * True if sync is needed.
750  */
751 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
752 				  struct amdgpu_job *job)
753 {
754 	struct amdgpu_device *adev = ring->adev;
755 	unsigned vmhub = ring->vm_hub;
756 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
757 
758 	if (job->vmid == 0)
759 		return false;
760 
761 	if (job->vm_needs_flush || ring->has_compute_vm_bug)
762 		return true;
763 
764 	if (ring->funcs->emit_gds_switch && job->gds_switch_needed)
765 		return true;
766 
767 	if (amdgpu_vmid_had_gpu_reset(adev, &id_mgr->ids[job->vmid]))
768 		return true;
769 
770 	return false;
771 }
772 
773 /**
774  * amdgpu_vm_flush - hardware flush the vm
775  *
776  * @ring: ring to use for flush
777  * @job:  related job
778  * @need_pipe_sync: is pipe sync needed
779  *
780  * Emit a VM flush when it is necessary.
781  */
782 void amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
783 		     bool need_pipe_sync)
784 {
785 	struct amdgpu_device *adev = ring->adev;
786 	struct amdgpu_isolation *isolation = &adev->isolation[ring->xcp_id];
787 	unsigned vmhub = ring->vm_hub;
788 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
789 	struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
790 	bool spm_update_needed = job->spm_update_needed;
791 	bool gds_switch_needed = ring->funcs->emit_gds_switch &&
792 		job->gds_switch_needed;
793 	bool vm_flush_needed = job->vm_needs_flush;
794 	bool cleaner_shader_needed = false;
795 	bool pasid_mapping_needed = false;
796 	struct dma_fence *fence = NULL;
797 	unsigned int patch = 0;
798 
799 	if (amdgpu_vmid_had_gpu_reset(adev, id)) {
800 		gds_switch_needed = true;
801 		vm_flush_needed = true;
802 		pasid_mapping_needed = true;
803 		spm_update_needed = true;
804 	}
805 
806 	mutex_lock(&id_mgr->lock);
807 	if (id->pasid != job->pasid || !id->pasid_mapping ||
808 	    !dma_fence_is_signaled(id->pasid_mapping))
809 		pasid_mapping_needed = true;
810 	mutex_unlock(&id_mgr->lock);
811 
812 	gds_switch_needed &= !!ring->funcs->emit_gds_switch;
813 	vm_flush_needed &= !!ring->funcs->emit_vm_flush  &&
814 			job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
815 	pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
816 		ring->funcs->emit_wreg;
817 
818 	cleaner_shader_needed = job->run_cleaner_shader &&
819 		adev->gfx.enable_cleaner_shader &&
820 		ring->funcs->emit_cleaner_shader && job->base.s_fence &&
821 		&job->base.s_fence->scheduled == isolation->spearhead;
822 
823 	if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync &&
824 	    !cleaner_shader_needed)
825 		return;
826 
827 	amdgpu_ring_ib_begin(ring);
828 
829 	/* There is no matching insert_end for this on purpose for the vm flush.
830 	 * The IB portion of the submission has both.  Having multiple
831 	 * insert_start sequences is ok, but you can only have one insert_end
832 	 * per submission based on the way VCN FW works.  For JPEG
833 	 * you can as many insert_start and insert_end sequences as you like as
834 	 * long as the rest of the packets come between start and end sequences.
835 	 */
836 	if (ring->funcs->insert_start)
837 		ring->funcs->insert_start(ring);
838 
839 	if (ring->funcs->init_cond_exec)
840 		patch = amdgpu_ring_init_cond_exec(ring,
841 						   ring->cond_exe_gpu_addr);
842 
843 	if (need_pipe_sync)
844 		amdgpu_ring_emit_pipeline_sync(ring);
845 
846 	if (cleaner_shader_needed)
847 		ring->funcs->emit_cleaner_shader(ring);
848 
849 	if (vm_flush_needed) {
850 		trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
851 		amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
852 	}
853 
854 	if (pasid_mapping_needed)
855 		amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
856 
857 	if (spm_update_needed && adev->gfx.rlc.funcs->update_spm_vmid)
858 		adev->gfx.rlc.funcs->update_spm_vmid(adev, ring->xcc_id, ring, job->vmid);
859 
860 	if (ring->funcs->emit_gds_switch &&
861 	    gds_switch_needed) {
862 		amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
863 					    job->gds_size, job->gws_base,
864 					    job->gws_size, job->oa_base,
865 					    job->oa_size);
866 	}
867 
868 	if (vm_flush_needed || pasid_mapping_needed || cleaner_shader_needed) {
869 		amdgpu_fence_emit(ring, job->hw_vm_fence, 0);
870 		fence = &job->hw_vm_fence->base;
871 		/* get a ref for the job */
872 		dma_fence_get(fence);
873 	}
874 
875 	if (vm_flush_needed) {
876 		mutex_lock(&id_mgr->lock);
877 		dma_fence_put(id->last_flush);
878 		id->last_flush = dma_fence_get(fence);
879 		id->current_gpu_reset_count =
880 			atomic_read(&adev->gpu_reset_counter);
881 		mutex_unlock(&id_mgr->lock);
882 	}
883 
884 	if (pasid_mapping_needed) {
885 		mutex_lock(&id_mgr->lock);
886 		id->pasid = job->pasid;
887 		dma_fence_put(id->pasid_mapping);
888 		id->pasid_mapping = dma_fence_get(fence);
889 		mutex_unlock(&id_mgr->lock);
890 	}
891 
892 	/*
893 	 * Make sure that all other submissions wait for the cleaner shader to
894 	 * finish before we push them to the HW.
895 	 */
896 	if (cleaner_shader_needed) {
897 		trace_amdgpu_cleaner_shader(ring, fence);
898 		mutex_lock(&adev->enforce_isolation_mutex);
899 		dma_fence_put(isolation->spearhead);
900 		isolation->spearhead = dma_fence_get(fence);
901 		mutex_unlock(&adev->enforce_isolation_mutex);
902 	}
903 	dma_fence_put(fence);
904 
905 	amdgpu_ring_patch_cond_exec(ring, patch);
906 
907 	/* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
908 	if (ring->funcs->emit_switch_buffer) {
909 		amdgpu_ring_emit_switch_buffer(ring);
910 		amdgpu_ring_emit_switch_buffer(ring);
911 	}
912 
913 	amdgpu_ring_ib_end(ring);
914 }
915 
916 /**
917  * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
918  *
919  * @vm: requested vm
920  * @bo: requested buffer object
921  *
922  * Find @bo inside the requested vm.
923  * Search inside the @bos vm list for the requested vm
924  * Returns the found bo_va or NULL if none is found
925  *
926  * Object has to be reserved!
927  *
928  * Returns:
929  * Found bo_va or NULL.
930  */
931 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
932 				       struct amdgpu_bo *bo)
933 {
934 	struct amdgpu_vm_bo_base *base;
935 
936 	for (base = bo->vm_bo; base; base = base->next) {
937 		if (base->vm != vm)
938 			continue;
939 
940 		return container_of(base, struct amdgpu_bo_va, base);
941 	}
942 	return NULL;
943 }
944 
945 /**
946  * amdgpu_vm_map_gart - Resolve gart mapping of addr
947  *
948  * @pages_addr: optional DMA address to use for lookup
949  * @addr: the unmapped addr
950  *
951  * Look up the physical address of the page that the pte resolves
952  * to.
953  *
954  * Returns:
955  * The pointer for the page table entry.
956  */
957 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
958 {
959 	uint64_t result;
960 
961 	/* page table offset */
962 	result = pages_addr[addr >> PAGE_SHIFT];
963 
964 	/* in case cpu page size != gpu page size*/
965 	result |= addr & (~PAGE_MASK);
966 
967 	result &= 0xFFFFFFFFFFFFF000ULL;
968 
969 	return result;
970 }
971 
972 /**
973  * amdgpu_vm_update_pdes - make sure that all directories are valid
974  *
975  * @adev: amdgpu_device pointer
976  * @vm: requested vm
977  * @immediate: submit immediately to the paging queue
978  *
979  * Makes sure all directories are up to date.
980  *
981  * Returns:
982  * 0 for success, error for failure.
983  */
984 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
985 			  struct amdgpu_vm *vm, bool immediate)
986 {
987 	struct amdgpu_vm_update_params params;
988 	struct amdgpu_vm_bo_base *entry;
989 	bool flush_tlb_needed = false;
990 	LIST_HEAD(relocated);
991 	int r, idx;
992 
993 	amdgpu_vm_assert_locked(vm);
994 
995 	spin_lock(&vm->status_lock);
996 	list_splice_init(&vm->relocated, &relocated);
997 	spin_unlock(&vm->status_lock);
998 
999 	if (list_empty(&relocated))
1000 		return 0;
1001 
1002 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
1003 		return -ENODEV;
1004 
1005 	memset(&params, 0, sizeof(params));
1006 	params.adev = adev;
1007 	params.vm = vm;
1008 	params.immediate = immediate;
1009 
1010 	r = vm->update_funcs->prepare(&params, NULL,
1011 				      AMDGPU_KERNEL_JOB_ID_VM_UPDATE_PDES);
1012 	if (r)
1013 		goto error;
1014 
1015 	list_for_each_entry(entry, &relocated, vm_status) {
1016 		/* vm_flush_needed after updating moved PDEs */
1017 		flush_tlb_needed |= entry->moved;
1018 
1019 		r = amdgpu_vm_pde_update(&params, entry);
1020 		if (r)
1021 			goto error;
1022 	}
1023 
1024 	r = vm->update_funcs->commit(&params, &vm->last_update);
1025 	if (r)
1026 		goto error;
1027 
1028 	if (flush_tlb_needed)
1029 		atomic64_inc(&vm->tlb_seq);
1030 
1031 	while (!list_empty(&relocated)) {
1032 		entry = list_first_entry(&relocated, struct amdgpu_vm_bo_base,
1033 					 vm_status);
1034 		amdgpu_vm_bo_idle(entry);
1035 	}
1036 
1037 error:
1038 	drm_dev_exit(idx);
1039 	return r;
1040 }
1041 
1042 /**
1043  * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
1044  * @fence: unused
1045  * @cb: the callback structure
1046  *
1047  * Increments the tlb sequence to make sure that future CS execute a VM flush.
1048  */
1049 static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
1050 				 struct dma_fence_cb *cb)
1051 {
1052 	struct amdgpu_vm_tlb_seq_struct *tlb_cb;
1053 
1054 	tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
1055 	atomic64_inc(&tlb_cb->vm->tlb_seq);
1056 	kfree(tlb_cb);
1057 }
1058 
1059 /**
1060  * amdgpu_vm_tlb_flush - prepare TLB flush
1061  *
1062  * @params: parameters for update
1063  * @fence: input fence to sync TLB flush with
1064  * @tlb_cb: the callback structure
1065  *
1066  * Increments the tlb sequence to make sure that future CS execute a VM flush.
1067  */
1068 static void
1069 amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
1070 		    struct dma_fence **fence,
1071 		    struct amdgpu_vm_tlb_seq_struct *tlb_cb)
1072 {
1073 	struct amdgpu_vm *vm = params->vm;
1074 
1075 	tlb_cb->vm = vm;
1076 	if (!fence || !*fence) {
1077 		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
1078 		return;
1079 	}
1080 
1081 	if (!dma_fence_add_callback(*fence, &tlb_cb->cb,
1082 				    amdgpu_vm_tlb_seq_cb)) {
1083 		dma_fence_put(vm->last_tlb_flush);
1084 		vm->last_tlb_flush = dma_fence_get(*fence);
1085 	} else {
1086 		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
1087 	}
1088 
1089 	/* Prepare a TLB flush fence to be attached to PTs */
1090 	/* The check for need_tlb_fence should be dropped once we
1091 	 * sort out the issues with KIQ/MES TLB invalidation timeouts.
1092 	 */
1093 	if (!params->unlocked && vm->need_tlb_fence) {
1094 		amdgpu_vm_tlb_fence_create(params->adev, vm, fence);
1095 
1096 		/* Makes sure no PD/PT is freed before the flush */
1097 		dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence,
1098 				   DMA_RESV_USAGE_BOOKKEEP);
1099 	}
1100 }
1101 
1102 /**
1103  * amdgpu_vm_update_range - update a range in the vm page table
1104  *
1105  * @adev: amdgpu_device pointer to use for commands
1106  * @vm: the VM to update the range
1107  * @immediate: immediate submission in a page fault
1108  * @unlocked: unlocked invalidation during MM callback
1109  * @flush_tlb: trigger tlb invalidation after update completed
1110  * @allow_override: change MTYPE for local NUMA nodes
1111  * @sync: fences we need to sync to
1112  * @start: start of mapped range
1113  * @last: last mapped entry
1114  * @flags: flags for the entries
1115  * @offset: offset into nodes and pages_addr
1116  * @vram_base: base for vram mappings
1117  * @res: ttm_resource to map
1118  * @pages_addr: DMA addresses to use for mapping
1119  * @fence: optional resulting fence
1120  *
1121  * Fill in the page table entries between @start and @last.
1122  *
1123  * Returns:
1124  * 0 for success, negative erro code for failure.
1125  */
1126 int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1127 			   bool immediate, bool unlocked, bool flush_tlb,
1128 			   bool allow_override, struct amdgpu_sync *sync,
1129 			   uint64_t start, uint64_t last, uint64_t flags,
1130 			   uint64_t offset, uint64_t vram_base,
1131 			   struct ttm_resource *res, dma_addr_t *pages_addr,
1132 			   struct dma_fence **fence)
1133 {
1134 	struct amdgpu_vm_tlb_seq_struct *tlb_cb;
1135 	struct amdgpu_vm_update_params params;
1136 	struct amdgpu_res_cursor cursor;
1137 	int r, idx;
1138 
1139 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
1140 		return -ENODEV;
1141 
1142 	tlb_cb = kmalloc_obj(*tlb_cb);
1143 	if (!tlb_cb) {
1144 		drm_dev_exit(idx);
1145 		return -ENOMEM;
1146 	}
1147 
1148 	/* Vega20+XGMI where PTEs get inadvertently cached in L2 texture cache,
1149 	 * heavy-weight flush TLB unconditionally.
1150 	 */
1151 	flush_tlb |= adev->gmc.xgmi.num_physical_nodes &&
1152 		     amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 0);
1153 
1154 	/*
1155 	 * On GFX8 and older any 8 PTE block with a valid bit set enters the TLB
1156 	 */
1157 	flush_tlb |= amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(9, 0, 0);
1158 
1159 	memset(&params, 0, sizeof(params));
1160 	params.adev = adev;
1161 	params.vm = vm;
1162 	params.immediate = immediate;
1163 	params.pages_addr = pages_addr;
1164 	params.unlocked = unlocked;
1165 	params.needs_flush = flush_tlb;
1166 	params.allow_override = allow_override;
1167 	INIT_LIST_HEAD(&params.tlb_flush_waitlist);
1168 
1169 	amdgpu_vm_eviction_lock(vm);
1170 	if (vm->evicting) {
1171 		r = -EBUSY;
1172 		goto error_free;
1173 	}
1174 
1175 	if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) {
1176 		struct dma_fence *tmp = dma_fence_get_stub();
1177 
1178 		amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true);
1179 		swap(vm->last_unlocked, tmp);
1180 		dma_fence_put(tmp);
1181 	}
1182 
1183 	r = vm->update_funcs->prepare(&params, sync,
1184 				      AMDGPU_KERNEL_JOB_ID_VM_UPDATE_RANGE);
1185 	if (r)
1186 		goto error_free;
1187 
1188 	amdgpu_res_first(pages_addr ? NULL : res, offset,
1189 			 (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor);
1190 	while (cursor.remaining) {
1191 		uint64_t tmp, num_entries, addr;
1192 
1193 		num_entries = cursor.size >> AMDGPU_GPU_PAGE_SHIFT;
1194 		if (pages_addr) {
1195 			bool contiguous = true;
1196 
1197 			if (num_entries > AMDGPU_GPU_PAGES_IN_CPU_PAGE) {
1198 				uint64_t pfn = cursor.start >> PAGE_SHIFT;
1199 				uint64_t count;
1200 
1201 				contiguous = pages_addr[pfn + 1] ==
1202 					pages_addr[pfn] + PAGE_SIZE;
1203 
1204 				tmp = num_entries /
1205 					AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1206 				for (count = 2; count < tmp; ++count) {
1207 					uint64_t idx = pfn + count;
1208 
1209 					if (contiguous != (pages_addr[idx] ==
1210 					    pages_addr[idx - 1] + PAGE_SIZE))
1211 						break;
1212 				}
1213 				if (!contiguous)
1214 					count--;
1215 				num_entries = count *
1216 					AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1217 			}
1218 
1219 			if (!contiguous) {
1220 				addr = cursor.start;
1221 				params.pages_addr = pages_addr;
1222 			} else {
1223 				addr = pages_addr[cursor.start >> PAGE_SHIFT];
1224 				params.pages_addr = NULL;
1225 			}
1226 
1227 		} else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT_FLAG(adev))) {
1228 			addr = vram_base + cursor.start;
1229 		} else {
1230 			addr = 0;
1231 		}
1232 
1233 		tmp = start + num_entries;
1234 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
1235 		if (r)
1236 			goto error_free;
1237 
1238 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
1239 		start = tmp;
1240 	}
1241 
1242 	r = vm->update_funcs->commit(&params, fence);
1243 	if (r)
1244 		goto error_free;
1245 
1246 	if (params.needs_flush) {
1247 		amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
1248 		tlb_cb = NULL;
1249 	}
1250 
1251 	amdgpu_vm_pt_free_list(adev, &params);
1252 
1253 error_free:
1254 	kfree(tlb_cb);
1255 	amdgpu_vm_eviction_unlock(vm);
1256 	drm_dev_exit(idx);
1257 	return r;
1258 }
1259 
1260 void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
1261 			  struct amdgpu_mem_stats stats[__AMDGPU_PL_NUM])
1262 {
1263 	spin_lock(&vm->status_lock);
1264 	memcpy(stats, vm->stats, sizeof(*stats) * __AMDGPU_PL_NUM);
1265 	spin_unlock(&vm->status_lock);
1266 }
1267 
1268 /**
1269  * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1270  *
1271  * @adev: amdgpu_device pointer
1272  * @bo_va: requested BO and VM object
1273  * @clear: if true clear the entries
1274  *
1275  * Fill in the page table entries for @bo_va.
1276  *
1277  * Returns:
1278  * 0 for success, -EINVAL for failure.
1279  */
1280 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
1281 			bool clear)
1282 {
1283 	struct amdgpu_bo *bo = bo_va->base.bo;
1284 	struct amdgpu_vm *vm = bo_va->base.vm;
1285 	struct amdgpu_bo_va_mapping *mapping;
1286 	struct dma_fence **last_update;
1287 	dma_addr_t *pages_addr = NULL;
1288 	struct ttm_resource *mem;
1289 	struct amdgpu_sync sync;
1290 	bool flush_tlb = clear;
1291 	uint64_t vram_base;
1292 	uint64_t flags;
1293 	bool uncached;
1294 	int r;
1295 
1296 	amdgpu_sync_create(&sync);
1297 	if (clear) {
1298 		mem = NULL;
1299 
1300 		/* Implicitly sync to command submissions in the same VM before
1301 		 * unmapping.
1302 		 */
1303 		r = amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.base.resv,
1304 				     AMDGPU_SYNC_EQ_OWNER, vm);
1305 		if (r)
1306 			goto error_free;
1307 		if (bo) {
1308 			r = amdgpu_sync_kfd(&sync, bo->tbo.base.resv);
1309 			if (r)
1310 				goto error_free;
1311 		}
1312 	} else if (!bo) {
1313 		mem = NULL;
1314 
1315 		/* PRT map operations don't need to sync to anything. */
1316 
1317 	} else {
1318 		struct drm_gem_object *obj = &bo->tbo.base;
1319 
1320 		if (drm_gem_is_imported(obj) && bo_va->is_xgmi) {
1321 			struct dma_buf *dma_buf = obj->import_attach->dmabuf;
1322 			struct drm_gem_object *gobj = dma_buf->priv;
1323 			struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
1324 
1325 			if (abo->tbo.resource &&
1326 			    abo->tbo.resource->mem_type == TTM_PL_VRAM)
1327 				bo = gem_to_amdgpu_bo(gobj);
1328 		}
1329 		mem = bo->tbo.resource;
1330 		if (mem && (mem->mem_type == TTM_PL_TT ||
1331 			    mem->mem_type == AMDGPU_PL_PREEMPT))
1332 			pages_addr = bo->tbo.ttm->dma_address;
1333 
1334 		/* Implicitly sync to moving fences before mapping anything */
1335 		r = amdgpu_sync_resv(adev, &sync, bo->tbo.base.resv,
1336 				     AMDGPU_SYNC_EXPLICIT, vm);
1337 		if (r)
1338 			goto error_free;
1339 	}
1340 
1341 	if (bo) {
1342 		struct amdgpu_device *bo_adev;
1343 
1344 		flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1345 
1346 		if (amdgpu_bo_encrypted(bo))
1347 			flags |= AMDGPU_PTE_TMZ;
1348 
1349 		bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
1350 		vram_base = bo_adev->vm_manager.vram_base_offset;
1351 		uncached = (bo->flags & AMDGPU_GEM_CREATE_UNCACHED) != 0;
1352 	} else {
1353 		flags = 0x0;
1354 		vram_base = 0;
1355 		uncached = false;
1356 	}
1357 
1358 	if (clear || amdgpu_vm_is_bo_always_valid(vm, bo))
1359 		last_update = &vm->last_update;
1360 	else
1361 		last_update = &bo_va->last_pt_update;
1362 
1363 	if (!clear && bo_va->base.moved) {
1364 		flush_tlb = true;
1365 		list_splice_init(&bo_va->valids, &bo_va->invalids);
1366 
1367 	} else if (bo_va->cleared != clear) {
1368 		list_splice_init(&bo_va->valids, &bo_va->invalids);
1369 	}
1370 
1371 	list_for_each_entry(mapping, &bo_va->invalids, list) {
1372 		uint64_t update_flags = flags;
1373 
1374 		/* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1375 		 * but in case of something, we filter the flags in first place
1376 		 */
1377 		if (!(mapping->flags & AMDGPU_VM_PAGE_READABLE))
1378 			update_flags &= ~AMDGPU_PTE_READABLE;
1379 		if (!(mapping->flags & AMDGPU_VM_PAGE_WRITEABLE))
1380 			update_flags &= ~AMDGPU_PTE_WRITEABLE;
1381 
1382 		/* Apply ASIC specific mapping flags */
1383 		amdgpu_gmc_get_vm_pte(adev, vm, bo, mapping->flags,
1384 				      &update_flags);
1385 
1386 		trace_amdgpu_vm_bo_update(mapping);
1387 
1388 		r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb,
1389 					   !uncached, &sync, mapping->start,
1390 					   mapping->last, update_flags,
1391 					   mapping->offset, vram_base, mem,
1392 					   pages_addr, last_update);
1393 		if (r)
1394 			goto error_free;
1395 	}
1396 
1397 	/* If the BO is not in its preferred location add it back to
1398 	 * the evicted list so that it gets validated again on the
1399 	 * next command submission.
1400 	 */
1401 	if (amdgpu_vm_is_bo_always_valid(vm, bo)) {
1402 		if (bo->tbo.resource &&
1403 		    !(bo->preferred_domains &
1404 		      amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type)))
1405 			amdgpu_vm_bo_evicted(&bo_va->base);
1406 		else
1407 			amdgpu_vm_bo_idle(&bo_va->base);
1408 	} else {
1409 		amdgpu_vm_bo_done(&bo_va->base);
1410 	}
1411 
1412 	list_splice_init(&bo_va->invalids, &bo_va->valids);
1413 	bo_va->cleared = clear;
1414 	bo_va->base.moved = false;
1415 
1416 	if (trace_amdgpu_vm_bo_mapping_enabled()) {
1417 		list_for_each_entry(mapping, &bo_va->valids, list)
1418 			trace_amdgpu_vm_bo_mapping(mapping);
1419 	}
1420 
1421 error_free:
1422 	amdgpu_sync_free(&sync);
1423 	return r;
1424 }
1425 
1426 /**
1427  * amdgpu_vm_update_prt_state - update the global PRT state
1428  *
1429  * @adev: amdgpu_device pointer
1430  */
1431 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1432 {
1433 	unsigned long flags;
1434 	bool enable;
1435 
1436 	spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1437 	enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1438 	adev->gmc.gmc_funcs->set_prt(adev, enable);
1439 	spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1440 }
1441 
1442 /**
1443  * amdgpu_vm_prt_get - add a PRT user
1444  *
1445  * @adev: amdgpu_device pointer
1446  */
1447 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1448 {
1449 	if (!adev->gmc.gmc_funcs->set_prt)
1450 		return;
1451 
1452 	if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1453 		amdgpu_vm_update_prt_state(adev);
1454 }
1455 
1456 /**
1457  * amdgpu_vm_prt_put - drop a PRT user
1458  *
1459  * @adev: amdgpu_device pointer
1460  */
1461 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1462 {
1463 	if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1464 		amdgpu_vm_update_prt_state(adev);
1465 }
1466 
1467 /**
1468  * amdgpu_vm_prt_cb - callback for updating the PRT status
1469  *
1470  * @fence: fence for the callback
1471  * @_cb: the callback function
1472  */
1473 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1474 {
1475 	struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1476 
1477 	amdgpu_vm_prt_put(cb->adev);
1478 	kfree(cb);
1479 }
1480 
1481 /**
1482  * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1483  *
1484  * @adev: amdgpu_device pointer
1485  * @fence: fence for the callback
1486  */
1487 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1488 				 struct dma_fence *fence)
1489 {
1490 	struct amdgpu_prt_cb *cb;
1491 
1492 	if (!adev->gmc.gmc_funcs->set_prt)
1493 		return;
1494 
1495 	cb = kmalloc_obj(struct amdgpu_prt_cb);
1496 	if (!cb) {
1497 		/* Last resort when we are OOM */
1498 		if (fence)
1499 			dma_fence_wait(fence, false);
1500 
1501 		amdgpu_vm_prt_put(adev);
1502 	} else {
1503 		cb->adev = adev;
1504 		if (!fence || dma_fence_add_callback(fence, &cb->cb,
1505 						     amdgpu_vm_prt_cb))
1506 			amdgpu_vm_prt_cb(fence, &cb->cb);
1507 	}
1508 }
1509 
1510 /**
1511  * amdgpu_vm_free_mapping - free a mapping
1512  *
1513  * @adev: amdgpu_device pointer
1514  * @vm: requested vm
1515  * @mapping: mapping to be freed
1516  * @fence: fence of the unmap operation
1517  *
1518  * Free a mapping and make sure we decrease the PRT usage count if applicable.
1519  */
1520 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1521 				   struct amdgpu_vm *vm,
1522 				   struct amdgpu_bo_va_mapping *mapping,
1523 				   struct dma_fence *fence)
1524 {
1525 	if (mapping->flags & AMDGPU_VM_PAGE_PRT)
1526 		amdgpu_vm_add_prt_cb(adev, fence);
1527 	kfree(mapping);
1528 }
1529 
1530 /**
1531  * amdgpu_vm_prt_fini - finish all prt mappings
1532  *
1533  * @adev: amdgpu_device pointer
1534  * @vm: requested vm
1535  *
1536  * Register a cleanup callback to disable PRT support after VM dies.
1537  */
1538 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1539 {
1540 	struct dma_resv *resv = vm->root.bo->tbo.base.resv;
1541 	struct dma_resv_iter cursor;
1542 	struct dma_fence *fence;
1543 
1544 	dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) {
1545 		/* Add a callback for each fence in the reservation object */
1546 		amdgpu_vm_prt_get(adev);
1547 		amdgpu_vm_add_prt_cb(adev, fence);
1548 	}
1549 }
1550 
1551 /**
1552  * amdgpu_vm_clear_freed - clear freed BOs in the PT
1553  *
1554  * @adev: amdgpu_device pointer
1555  * @vm: requested vm
1556  * @fence: optional resulting fence (unchanged if no work needed to be done
1557  * or if an error occurred)
1558  *
1559  * Make sure all freed BOs are cleared in the PT.
1560  * PTs have to be reserved and mutex must be locked!
1561  *
1562  * Returns:
1563  * 0 for success.
1564  *
1565  */
1566 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1567 			  struct amdgpu_vm *vm,
1568 			  struct dma_fence **fence)
1569 {
1570 	struct amdgpu_bo_va_mapping *mapping;
1571 	struct dma_fence *f = NULL;
1572 	struct amdgpu_sync sync;
1573 	int r;
1574 
1575 
1576 	/*
1577 	 * Implicitly sync to command submissions in the same VM before
1578 	 * unmapping.
1579 	 */
1580 	amdgpu_sync_create(&sync);
1581 	r = amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.base.resv,
1582 			     AMDGPU_SYNC_EQ_OWNER, vm);
1583 	if (r)
1584 		goto error_free;
1585 
1586 	while (!list_empty(&vm->freed)) {
1587 		mapping = list_first_entry(&vm->freed,
1588 			struct amdgpu_bo_va_mapping, list);
1589 		list_del(&mapping->list);
1590 
1591 		r = amdgpu_vm_update_range(adev, vm, false, false, true, false,
1592 					   &sync, mapping->start, mapping->last,
1593 					   0, 0, 0, NULL, NULL, &f);
1594 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
1595 		if (r) {
1596 			dma_fence_put(f);
1597 			goto error_free;
1598 		}
1599 	}
1600 
1601 	if (fence && f) {
1602 		dma_fence_put(*fence);
1603 		*fence = f;
1604 	} else {
1605 		dma_fence_put(f);
1606 	}
1607 
1608 error_free:
1609 	amdgpu_sync_free(&sync);
1610 	return r;
1611 
1612 }
1613 
1614 /**
1615  * amdgpu_vm_handle_moved - handle moved BOs in the PT
1616  *
1617  * @adev: amdgpu_device pointer
1618  * @vm: requested vm
1619  * @ticket: optional reservation ticket used to reserve the VM
1620  *
1621  * Make sure all BOs which are moved are updated in the PTs.
1622  *
1623  * Returns:
1624  * 0 for success.
1625  *
1626  * PTs have to be reserved!
1627  */
1628 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
1629 			   struct amdgpu_vm *vm,
1630 			   struct ww_acquire_ctx *ticket)
1631 {
1632 	struct amdgpu_bo_va *bo_va;
1633 	struct dma_resv *resv;
1634 	struct amdgpu_bo *bo;
1635 	bool clear, unlock;
1636 	int r;
1637 
1638 	spin_lock(&vm->status_lock);
1639 	while (!list_empty(&vm->moved)) {
1640 		bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
1641 					 base.vm_status);
1642 		spin_unlock(&vm->status_lock);
1643 
1644 		/* Per VM BOs never need to bo cleared in the page tables */
1645 		r = amdgpu_vm_bo_update(adev, bo_va, false);
1646 		if (r)
1647 			return r;
1648 		spin_lock(&vm->status_lock);
1649 	}
1650 
1651 	while (!list_empty(&vm->invalidated)) {
1652 		bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
1653 					 base.vm_status);
1654 		bo = bo_va->base.bo;
1655 		resv = bo->tbo.base.resv;
1656 		spin_unlock(&vm->status_lock);
1657 
1658 		/* Try to reserve the BO to avoid clearing its ptes */
1659 		if (!adev->debug_vm && !amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) &&
1660 		    dma_resv_trylock(resv)) {
1661 			clear = false;
1662 			unlock = true;
1663 		/* The caller is already holding the reservation lock */
1664 		} else if (ticket && dma_resv_locking_ctx(resv) == ticket) {
1665 			clear = false;
1666 			unlock = false;
1667 		/* Somebody else is using the BO right now */
1668 		} else {
1669 			clear = true;
1670 			unlock = false;
1671 		}
1672 
1673 		r = amdgpu_vm_bo_update(adev, bo_va, clear);
1674 
1675 		if (unlock)
1676 			dma_resv_unlock(resv);
1677 		if (r)
1678 			return r;
1679 
1680 		/* Remember evicted DMABuf imports in compute VMs for later
1681 		 * validation
1682 		 */
1683 		if (vm->is_compute_context &&
1684 		    drm_gem_is_imported(&bo_va->base.bo->tbo.base) &&
1685 		    (!bo_va->base.bo->tbo.resource ||
1686 		     bo_va->base.bo->tbo.resource->mem_type == TTM_PL_SYSTEM))
1687 			amdgpu_vm_bo_evicted_user(&bo_va->base);
1688 
1689 		spin_lock(&vm->status_lock);
1690 	}
1691 	spin_unlock(&vm->status_lock);
1692 
1693 	return 0;
1694 }
1695 
1696 /**
1697  * amdgpu_vm_flush_compute_tlb - Flush TLB on compute VM
1698  *
1699  * @adev: amdgpu_device pointer
1700  * @vm: requested vm
1701  * @flush_type: flush type
1702  * @xcc_mask: mask of XCCs that belong to the compute partition in need of a TLB flush.
1703  *
1704  * Flush TLB if needed for a compute VM.
1705  *
1706  * Returns:
1707  * 0 for success.
1708  */
1709 int amdgpu_vm_flush_compute_tlb(struct amdgpu_device *adev,
1710 				struct amdgpu_vm *vm,
1711 				uint32_t flush_type,
1712 				uint32_t xcc_mask)
1713 {
1714 	uint64_t tlb_seq = amdgpu_vm_tlb_seq(vm);
1715 	bool all_hub = false;
1716 	int xcc = 0, r = 0;
1717 
1718 	WARN_ON_ONCE(!vm->is_compute_context);
1719 
1720 	/*
1721 	 * It can be that we race and lose here, but that is extremely unlikely
1722 	 * and the worst thing which could happen is that we flush the changes
1723 	 * into the TLB once more which is harmless.
1724 	 */
1725 	if (atomic64_xchg(&vm->kfd_last_flushed_seq, tlb_seq) == tlb_seq)
1726 		return 0;
1727 
1728 	if (adev->family == AMDGPU_FAMILY_AI ||
1729 	    adev->family == AMDGPU_FAMILY_RV)
1730 		all_hub = true;
1731 
1732 	for_each_inst(xcc, xcc_mask) {
1733 		r = amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, flush_type,
1734 						   all_hub, xcc);
1735 		if (r)
1736 			break;
1737 	}
1738 	return r;
1739 }
1740 
1741 /**
1742  * amdgpu_vm_bo_add - add a bo to a specific vm
1743  *
1744  * @adev: amdgpu_device pointer
1745  * @vm: requested vm
1746  * @bo: amdgpu buffer object
1747  *
1748  * Add @bo into the requested vm.
1749  * Add @bo to the list of bos associated with the vm
1750  *
1751  * Returns:
1752  * Newly added bo_va or NULL for failure
1753  *
1754  * Object has to be reserved!
1755  */
1756 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1757 				      struct amdgpu_vm *vm,
1758 				      struct amdgpu_bo *bo)
1759 {
1760 	struct amdgpu_bo_va *bo_va;
1761 
1762 	amdgpu_vm_assert_locked(vm);
1763 
1764 	bo_va = kzalloc_obj(struct amdgpu_bo_va);
1765 	if (bo_va == NULL) {
1766 		return NULL;
1767 	}
1768 	amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
1769 
1770 	bo_va->ref_count = 1;
1771 	bo_va->last_pt_update = dma_fence_get_stub();
1772 	INIT_LIST_HEAD(&bo_va->valids);
1773 	INIT_LIST_HEAD(&bo_va->invalids);
1774 
1775 	if (!bo)
1776 		return bo_va;
1777 
1778 	dma_resv_assert_held(bo->tbo.base.resv);
1779 	if (amdgpu_dmabuf_is_xgmi_accessible(adev, bo)) {
1780 		bo_va->is_xgmi = true;
1781 		/* Power up XGMI if it can be potentially used */
1782 		amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MAX_VEGA20);
1783 	}
1784 
1785 	return bo_va;
1786 }
1787 
1788 
1789 /**
1790  * amdgpu_vm_bo_insert_map - insert a new mapping
1791  *
1792  * @adev: amdgpu_device pointer
1793  * @bo_va: bo_va to store the address
1794  * @mapping: the mapping to insert
1795  *
1796  * Insert a new mapping into all structures.
1797  */
1798 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1799 				    struct amdgpu_bo_va *bo_va,
1800 				    struct amdgpu_bo_va_mapping *mapping)
1801 {
1802 	struct amdgpu_vm *vm = bo_va->base.vm;
1803 	struct amdgpu_bo *bo = bo_va->base.bo;
1804 
1805 	mapping->bo_va = bo_va;
1806 	list_add(&mapping->list, &bo_va->invalids);
1807 	amdgpu_vm_it_insert(mapping, &vm->va);
1808 
1809 	if (mapping->flags & AMDGPU_VM_PAGE_PRT)
1810 		amdgpu_vm_prt_get(adev);
1811 
1812 	if (amdgpu_vm_is_bo_always_valid(vm, bo) && !bo_va->base.moved)
1813 		amdgpu_vm_bo_moved(&bo_va->base);
1814 
1815 	trace_amdgpu_vm_bo_map(bo_va, mapping);
1816 }
1817 
1818 /* Validate operation parameters to prevent potential abuse */
1819 static int amdgpu_vm_verify_parameters(struct amdgpu_device *adev,
1820 					  struct amdgpu_bo *bo,
1821 					  uint64_t saddr,
1822 					  uint64_t offset,
1823 					  uint64_t size)
1824 {
1825 	uint64_t tmp, lpfn;
1826 
1827 	if (saddr & AMDGPU_GPU_PAGE_MASK
1828 	    || offset & AMDGPU_GPU_PAGE_MASK
1829 	    || size & AMDGPU_GPU_PAGE_MASK)
1830 		return -EINVAL;
1831 
1832 	if (check_add_overflow(saddr, size, &tmp)
1833 	    || check_add_overflow(offset, size, &tmp)
1834 	    || size == 0 /* which also leads to end < begin */)
1835 		return -EINVAL;
1836 
1837 	/* make sure object fit at this offset */
1838 	if (bo && offset + size > amdgpu_bo_size(bo))
1839 		return -EINVAL;
1840 
1841 	/* Ensure last pfn not exceed max_pfn */
1842 	lpfn = (saddr + size - 1) >> AMDGPU_GPU_PAGE_SHIFT;
1843 	if (lpfn >= adev->vm_manager.max_pfn)
1844 		return -EINVAL;
1845 
1846 	return 0;
1847 }
1848 
1849 /**
1850  * amdgpu_vm_bo_map - map bo inside a vm
1851  *
1852  * @adev: amdgpu_device pointer
1853  * @bo_va: bo_va to store the address
1854  * @saddr: where to map the BO
1855  * @offset: requested offset in the BO
1856  * @size: BO size in bytes
1857  * @flags: attributes of pages (read/write/valid/etc.)
1858  *
1859  * Add a mapping of the BO at the specefied addr into the VM.
1860  *
1861  * Returns:
1862  * 0 for success, error for failure.
1863  *
1864  * Object has to be reserved and unreserved outside!
1865  */
1866 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1867 		     struct amdgpu_bo_va *bo_va,
1868 		     uint64_t saddr, uint64_t offset,
1869 		     uint64_t size, uint32_t flags)
1870 {
1871 	struct amdgpu_bo_va_mapping *mapping, *tmp;
1872 	struct amdgpu_bo *bo = bo_va->base.bo;
1873 	struct amdgpu_vm *vm = bo_va->base.vm;
1874 	uint64_t eaddr;
1875 	int r;
1876 
1877 	r = amdgpu_vm_verify_parameters(adev, bo, saddr, offset, size);
1878 	if (r)
1879 		return r;
1880 
1881 	saddr /= AMDGPU_GPU_PAGE_SIZE;
1882 	eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE;
1883 
1884 	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1885 	if (tmp) {
1886 		/* bo and tmp overlap, invalid addr */
1887 		dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1888 			"0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
1889 			tmp->start, tmp->last + 1);
1890 		return -EINVAL;
1891 	}
1892 
1893 	mapping = kmalloc_obj(*mapping);
1894 	if (!mapping)
1895 		return -ENOMEM;
1896 
1897 	mapping->start = saddr;
1898 	mapping->last = eaddr;
1899 	mapping->offset = offset;
1900 	mapping->flags = flags;
1901 
1902 	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1903 
1904 	return 0;
1905 }
1906 
1907 /**
1908  * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1909  *
1910  * @adev: amdgpu_device pointer
1911  * @bo_va: bo_va to store the address
1912  * @saddr: where to map the BO
1913  * @offset: requested offset in the BO
1914  * @size: BO size in bytes
1915  * @flags: attributes of pages (read/write/valid/etc.)
1916  *
1917  * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1918  * mappings as we do so.
1919  *
1920  * Returns:
1921  * 0 for success, error for failure.
1922  *
1923  * Object has to be reserved and unreserved outside!
1924  */
1925 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1926 			     struct amdgpu_bo_va *bo_va,
1927 			     uint64_t saddr, uint64_t offset,
1928 			     uint64_t size, uint32_t flags)
1929 {
1930 	struct amdgpu_bo_va_mapping *mapping;
1931 	struct amdgpu_bo *bo = bo_va->base.bo;
1932 	uint64_t eaddr;
1933 	int r;
1934 
1935 	r = amdgpu_vm_verify_parameters(adev, bo, saddr, offset, size);
1936 	if (r)
1937 		return r;
1938 
1939 	/* Allocate all the needed memory */
1940 	mapping = kmalloc_obj(*mapping);
1941 	if (!mapping)
1942 		return -ENOMEM;
1943 
1944 	r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
1945 	if (r) {
1946 		kfree(mapping);
1947 		return r;
1948 	}
1949 
1950 	saddr /= AMDGPU_GPU_PAGE_SIZE;
1951 	eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE;
1952 
1953 	mapping->start = saddr;
1954 	mapping->last = eaddr;
1955 	mapping->offset = offset;
1956 	mapping->flags = flags;
1957 
1958 	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1959 
1960 	return 0;
1961 }
1962 
1963 /**
1964  * amdgpu_vm_bo_unmap - remove bo mapping from vm
1965  *
1966  * @adev: amdgpu_device pointer
1967  * @bo_va: bo_va to remove the address from
1968  * @saddr: where to the BO is mapped
1969  *
1970  * Remove a mapping of the BO at the specefied addr from the VM.
1971  *
1972  * Returns:
1973  * 0 for success, error for failure.
1974  *
1975  * Object has to be reserved and unreserved outside!
1976  */
1977 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1978 		       struct amdgpu_bo_va *bo_va,
1979 		       uint64_t saddr)
1980 {
1981 	struct amdgpu_bo_va_mapping *mapping;
1982 	struct amdgpu_vm *vm = bo_va->base.vm;
1983 	bool valid = true;
1984 
1985 	saddr /= AMDGPU_GPU_PAGE_SIZE;
1986 
1987 	list_for_each_entry(mapping, &bo_va->valids, list) {
1988 		if (mapping->start == saddr)
1989 			break;
1990 	}
1991 
1992 	if (&mapping->list == &bo_va->valids) {
1993 		valid = false;
1994 
1995 		list_for_each_entry(mapping, &bo_va->invalids, list) {
1996 			if (mapping->start == saddr)
1997 				break;
1998 		}
1999 
2000 		if (&mapping->list == &bo_va->invalids)
2001 			return -ENOENT;
2002 	}
2003 
2004 	/* It's unlikely to happen that the mapping userq hasn't been idled
2005 	 * during user requests GEM unmap IOCTL except for forcing the unmap
2006 	 * from user space.
2007 	 */
2008 	if (unlikely(bo_va->userq_va_mapped))
2009 		amdgpu_userq_gem_va_unmap_validate(adev, mapping, saddr);
2010 
2011 	list_del(&mapping->list);
2012 	amdgpu_vm_it_remove(mapping, &vm->va);
2013 	mapping->bo_va = NULL;
2014 	trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2015 
2016 	if (valid)
2017 		list_add(&mapping->list, &vm->freed);
2018 	else
2019 		amdgpu_vm_free_mapping(adev, vm, mapping,
2020 				       bo_va->last_pt_update);
2021 
2022 	return 0;
2023 }
2024 
2025 /**
2026  * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2027  *
2028  * @adev: amdgpu_device pointer
2029  * @vm: VM structure to use
2030  * @saddr: start of the range
2031  * @size: size of the range
2032  *
2033  * Remove all mappings in a range, split them as appropriate.
2034  *
2035  * Returns:
2036  * 0 for success, error for failure.
2037  */
2038 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2039 				struct amdgpu_vm *vm,
2040 				uint64_t saddr, uint64_t size)
2041 {
2042 	struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
2043 	LIST_HEAD(removed);
2044 	uint64_t eaddr;
2045 	int r;
2046 
2047 	r = amdgpu_vm_verify_parameters(adev, NULL, saddr, 0, size);
2048 	if (r)
2049 		return r;
2050 
2051 	saddr /= AMDGPU_GPU_PAGE_SIZE;
2052 	eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE;
2053 
2054 	/* Allocate all the needed memory */
2055 	before = kzalloc_obj(*before);
2056 	if (!before)
2057 		return -ENOMEM;
2058 	INIT_LIST_HEAD(&before->list);
2059 
2060 	after = kzalloc_obj(*after);
2061 	if (!after) {
2062 		kfree(before);
2063 		return -ENOMEM;
2064 	}
2065 	INIT_LIST_HEAD(&after->list);
2066 
2067 	/* Now gather all removed mappings */
2068 	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2069 	while (tmp) {
2070 		/* Remember mapping split at the start */
2071 		if (tmp->start < saddr) {
2072 			before->start = tmp->start;
2073 			before->last = saddr - 1;
2074 			before->offset = tmp->offset;
2075 			before->flags = tmp->flags;
2076 			before->bo_va = tmp->bo_va;
2077 			list_add(&before->list, &tmp->bo_va->invalids);
2078 		}
2079 
2080 		/* Remember mapping split at the end */
2081 		if (tmp->last > eaddr) {
2082 			after->start = eaddr + 1;
2083 			after->last = tmp->last;
2084 			after->offset = tmp->offset;
2085 			after->offset += (after->start - tmp->start) << PAGE_SHIFT;
2086 			after->flags = tmp->flags;
2087 			after->bo_va = tmp->bo_va;
2088 			list_add(&after->list, &tmp->bo_va->invalids);
2089 		}
2090 
2091 		list_del(&tmp->list);
2092 		list_add(&tmp->list, &removed);
2093 
2094 		tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
2095 	}
2096 
2097 	/* And free them up */
2098 	list_for_each_entry_safe(tmp, next, &removed, list) {
2099 		amdgpu_vm_it_remove(tmp, &vm->va);
2100 		list_del(&tmp->list);
2101 
2102 		if (tmp->start < saddr)
2103 		    tmp->start = saddr;
2104 		if (tmp->last > eaddr)
2105 		    tmp->last = eaddr;
2106 
2107 		tmp->bo_va = NULL;
2108 		list_add(&tmp->list, &vm->freed);
2109 		trace_amdgpu_vm_bo_unmap(NULL, tmp);
2110 	}
2111 
2112 	/* Insert partial mapping before the range */
2113 	if (!list_empty(&before->list)) {
2114 		struct amdgpu_bo *bo = before->bo_va->base.bo;
2115 
2116 		amdgpu_vm_it_insert(before, &vm->va);
2117 		if (before->flags & AMDGPU_VM_PAGE_PRT)
2118 			amdgpu_vm_prt_get(adev);
2119 
2120 		if (amdgpu_vm_is_bo_always_valid(vm, bo) &&
2121 		    !before->bo_va->base.moved)
2122 			amdgpu_vm_bo_moved(&before->bo_va->base);
2123 	} else {
2124 		kfree(before);
2125 	}
2126 
2127 	/* Insert partial mapping after the range */
2128 	if (!list_empty(&after->list)) {
2129 		struct amdgpu_bo *bo = after->bo_va->base.bo;
2130 
2131 		amdgpu_vm_it_insert(after, &vm->va);
2132 		if (after->flags & AMDGPU_VM_PAGE_PRT)
2133 			amdgpu_vm_prt_get(adev);
2134 
2135 		if (amdgpu_vm_is_bo_always_valid(vm, bo) &&
2136 		    !after->bo_va->base.moved)
2137 			amdgpu_vm_bo_moved(&after->bo_va->base);
2138 	} else {
2139 		kfree(after);
2140 	}
2141 
2142 	return 0;
2143 }
2144 
2145 /**
2146  * amdgpu_vm_bo_lookup_mapping - find mapping by address
2147  *
2148  * @vm: the requested VM
2149  * @addr: the address
2150  *
2151  * Find a mapping by it's address.
2152  *
2153  * Returns:
2154  * The amdgpu_bo_va_mapping matching for addr or NULL
2155  *
2156  */
2157 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2158 							 uint64_t addr)
2159 {
2160 	return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2161 }
2162 
2163 /**
2164  * amdgpu_vm_bo_trace_cs - trace all reserved mappings
2165  *
2166  * @vm: the requested vm
2167  * @ticket: CS ticket
2168  *
2169  * Trace all mappings of BOs reserved during a command submission.
2170  */
2171 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket)
2172 {
2173 	struct amdgpu_bo_va_mapping *mapping;
2174 
2175 	if (!trace_amdgpu_vm_bo_cs_enabled())
2176 		return;
2177 
2178 	for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping;
2179 	     mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) {
2180 		if (mapping->bo_va && mapping->bo_va->base.bo) {
2181 			struct amdgpu_bo *bo;
2182 
2183 			bo = mapping->bo_va->base.bo;
2184 			if (dma_resv_locking_ctx(bo->tbo.base.resv) !=
2185 			    ticket)
2186 				continue;
2187 		}
2188 
2189 		trace_amdgpu_vm_bo_cs(mapping);
2190 	}
2191 }
2192 
2193 /**
2194  * amdgpu_vm_bo_del - remove a bo from a specific vm
2195  *
2196  * @adev: amdgpu_device pointer
2197  * @bo_va: requested bo_va
2198  *
2199  * Remove @bo_va->bo from the requested vm.
2200  *
2201  * Object have to be reserved!
2202  */
2203 void amdgpu_vm_bo_del(struct amdgpu_device *adev,
2204 		      struct amdgpu_bo_va *bo_va)
2205 {
2206 	struct amdgpu_bo_va_mapping *mapping, *next;
2207 	struct amdgpu_bo *bo = bo_va->base.bo;
2208 	struct amdgpu_vm *vm = bo_va->base.vm;
2209 	struct amdgpu_vm_bo_base **base;
2210 
2211 	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
2212 
2213 	if (bo) {
2214 		dma_resv_assert_held(bo->tbo.base.resv);
2215 		if (amdgpu_vm_is_bo_always_valid(vm, bo))
2216 			ttm_bo_set_bulk_move(&bo->tbo, NULL);
2217 
2218 		for (base = &bo_va->base.bo->vm_bo; *base;
2219 		     base = &(*base)->next) {
2220 			if (*base != &bo_va->base)
2221 				continue;
2222 
2223 			amdgpu_vm_update_stats(*base, bo->tbo.resource, -1);
2224 			*base = bo_va->base.next;
2225 			break;
2226 		}
2227 	}
2228 
2229 	spin_lock(&vm->status_lock);
2230 	list_del(&bo_va->base.vm_status);
2231 	spin_unlock(&vm->status_lock);
2232 
2233 	list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
2234 		list_del(&mapping->list);
2235 		amdgpu_vm_it_remove(mapping, &vm->va);
2236 		mapping->bo_va = NULL;
2237 		trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2238 		list_add(&mapping->list, &vm->freed);
2239 	}
2240 	list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2241 		list_del(&mapping->list);
2242 		amdgpu_vm_it_remove(mapping, &vm->va);
2243 		amdgpu_vm_free_mapping(adev, vm, mapping,
2244 				       bo_va->last_pt_update);
2245 	}
2246 
2247 	dma_fence_put(bo_va->last_pt_update);
2248 
2249 	if (bo && bo_va->is_xgmi)
2250 		amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN);
2251 
2252 	kfree(bo_va);
2253 }
2254 
2255 /**
2256  * amdgpu_vm_evictable - check if we can evict a VM
2257  *
2258  * @bo: A page table of the VM.
2259  *
2260  * Check if it is possible to evict a VM.
2261  */
2262 bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
2263 {
2264 	struct amdgpu_vm_bo_base *bo_base = bo->vm_bo;
2265 
2266 	/* Page tables of a destroyed VM can go away immediately */
2267 	if (!bo_base || !bo_base->vm)
2268 		return true;
2269 
2270 	/* Don't evict VM page tables while they are busy */
2271 	if (!dma_resv_test_signaled(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP))
2272 		return false;
2273 
2274 	/* Try to block ongoing updates */
2275 	if (!amdgpu_vm_eviction_trylock(bo_base->vm))
2276 		return false;
2277 
2278 	/* Don't evict VM page tables while they are updated */
2279 	if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) {
2280 		amdgpu_vm_eviction_unlock(bo_base->vm);
2281 		return false;
2282 	}
2283 
2284 	bo_base->vm->evicting = true;
2285 	amdgpu_vm_eviction_unlock(bo_base->vm);
2286 	return true;
2287 }
2288 
2289 /**
2290  * amdgpu_vm_bo_invalidate - mark the bo as invalid
2291  *
2292  * @bo: amdgpu buffer object
2293  * @evicted: is the BO evicted
2294  *
2295  * Mark @bo as invalid.
2296  */
2297 void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted)
2298 {
2299 	struct amdgpu_vm_bo_base *bo_base;
2300 
2301 	for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
2302 		struct amdgpu_vm *vm = bo_base->vm;
2303 
2304 		if (evicted && amdgpu_vm_is_bo_always_valid(vm, bo)) {
2305 			amdgpu_vm_bo_evicted(bo_base);
2306 			continue;
2307 		}
2308 
2309 		if (bo_base->moved)
2310 			continue;
2311 		bo_base->moved = true;
2312 
2313 		if (bo->tbo.type == ttm_bo_type_kernel)
2314 			amdgpu_vm_bo_relocated(bo_base);
2315 		else if (amdgpu_vm_is_bo_always_valid(vm, bo))
2316 			amdgpu_vm_bo_moved(bo_base);
2317 		else
2318 			amdgpu_vm_bo_invalidated(bo_base);
2319 	}
2320 }
2321 
2322 /**
2323  * amdgpu_vm_bo_move - handle BO move
2324  *
2325  * @bo: amdgpu buffer object
2326  * @new_mem: the new placement of the BO move
2327  * @evicted: is the BO evicted
2328  *
2329  * Update the memory stats for the new placement and mark @bo as invalid.
2330  */
2331 void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource *new_mem,
2332 		       bool evicted)
2333 {
2334 	struct amdgpu_vm_bo_base *bo_base;
2335 
2336 	for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
2337 		struct amdgpu_vm *vm = bo_base->vm;
2338 
2339 		spin_lock(&vm->status_lock);
2340 		amdgpu_vm_update_stats_locked(bo_base, bo->tbo.resource, -1);
2341 		amdgpu_vm_update_stats_locked(bo_base, new_mem, +1);
2342 		spin_unlock(&vm->status_lock);
2343 	}
2344 
2345 	amdgpu_vm_bo_invalidate(bo, evicted);
2346 }
2347 
2348 /**
2349  * amdgpu_vm_get_block_size - calculate VM page table size as power of two
2350  *
2351  * @vm_size: VM size
2352  *
2353  * Returns:
2354  * VM page table as power of two
2355  */
2356 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2357 {
2358 	/* Total bits covered by PD + PTs */
2359 	unsigned bits = ilog2(vm_size) + 18;
2360 
2361 	/* Make sure the PD is 4K in size up to 8GB address space.
2362 	   Above that split equal between PD and PTs */
2363 	if (vm_size <= 8)
2364 		return (bits - 9);
2365 	else
2366 		return ((bits + 3) / 2);
2367 }
2368 
2369 /**
2370  * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
2371  *
2372  * @adev: amdgpu_device pointer
2373  * @min_vm_size: the minimum vm size in GB if it's set auto
2374  * @fragment_size_default: Default PTE fragment size
2375  * @max_level: max VMPT level
2376  * @max_bits: max address space size in bits
2377  *
2378  */
2379 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
2380 			   uint32_t fragment_size_default, unsigned max_level,
2381 			   unsigned max_bits)
2382 {
2383 	unsigned int max_size = 1 << (max_bits - 30);
2384 	unsigned int vm_size;
2385 	uint64_t tmp;
2386 
2387 	/* adjust vm size first */
2388 	if (amdgpu_vm_size != -1) {
2389 		vm_size = amdgpu_vm_size;
2390 		if (vm_size > max_size) {
2391 			dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2392 				 amdgpu_vm_size, max_size);
2393 			vm_size = max_size;
2394 		}
2395 	} else {
2396 		struct sysinfo si;
2397 		unsigned int phys_ram_gb;
2398 
2399 		/* Optimal VM size depends on the amount of physical
2400 		 * RAM available. Underlying requirements and
2401 		 * assumptions:
2402 		 *
2403 		 *  - Need to map system memory and VRAM from all GPUs
2404 		 *     - VRAM from other GPUs not known here
2405 		 *     - Assume VRAM <= system memory
2406 		 *  - On GFX8 and older, VM space can be segmented for
2407 		 *    different MTYPEs
2408 		 *  - Need to allow room for fragmentation, guard pages etc.
2409 		 *
2410 		 * This adds up to a rough guess of system memory x3.
2411 		 * Round up to power of two to maximize the available
2412 		 * VM size with the given page table size.
2413 		 */
2414 		si_meminfo(&si);
2415 		phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit +
2416 			       (1 << 30) - 1) >> 30;
2417 		vm_size = roundup_pow_of_two(
2418 			clamp(phys_ram_gb * 3, min_vm_size, max_size));
2419 	}
2420 
2421 	adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
2422 	adev->vm_manager.max_level = max_level;
2423 
2424 	tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
2425 	if (amdgpu_vm_block_size != -1)
2426 		tmp >>= amdgpu_vm_block_size - 9;
2427 	tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2428 	adev->vm_manager.num_level = min_t(unsigned int, max_level, tmp);
2429 	switch (adev->vm_manager.num_level) {
2430 	case 4:
2431 		adev->vm_manager.root_level = AMDGPU_VM_PDB3;
2432 		break;
2433 	case 3:
2434 		adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2435 		break;
2436 	case 2:
2437 		adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2438 		break;
2439 	case 1:
2440 		adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2441 		break;
2442 	default:
2443 		dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2444 	}
2445 	/* block size depends on vm size and hw setup*/
2446 	if (amdgpu_vm_block_size != -1)
2447 		adev->vm_manager.block_size =
2448 			min((unsigned)amdgpu_vm_block_size, max_bits
2449 			    - AMDGPU_GPU_PAGE_SHIFT
2450 			    - 9 * adev->vm_manager.num_level);
2451 	else if (adev->vm_manager.num_level > 1)
2452 		adev->vm_manager.block_size = 9;
2453 	else
2454 		adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
2455 
2456 	if (amdgpu_vm_fragment_size == -1)
2457 		adev->vm_manager.fragment_size = fragment_size_default;
2458 	else
2459 		adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2460 
2461 	dev_info(
2462 		adev->dev,
2463 		"vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2464 		vm_size, adev->vm_manager.num_level + 1,
2465 		adev->vm_manager.block_size, adev->vm_manager.fragment_size);
2466 }
2467 
2468 /**
2469  * amdgpu_vm_wait_idle - wait for the VM to become idle
2470  *
2471  * @vm: VM object to wait for
2472  * @timeout: timeout to wait for VM to become idle
2473  */
2474 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout)
2475 {
2476 	timeout = drm_sched_entity_flush(&vm->immediate, timeout);
2477 	if (timeout <= 0)
2478 		return timeout;
2479 
2480 	return drm_sched_entity_flush(&vm->delayed, timeout);
2481 }
2482 
2483 static void amdgpu_vm_destroy_task_info(struct kref *kref)
2484 {
2485 	struct amdgpu_task_info *ti = container_of(kref, struct amdgpu_task_info, refcount);
2486 
2487 	kfree(ti);
2488 }
2489 
2490 static inline struct amdgpu_vm *
2491 amdgpu_vm_get_vm_from_pasid(struct amdgpu_device *adev, u32 pasid)
2492 {
2493 	struct amdgpu_vm *vm;
2494 	unsigned long flags;
2495 
2496 	xa_lock_irqsave(&adev->vm_manager.pasids, flags);
2497 	vm = xa_load(&adev->vm_manager.pasids, pasid);
2498 	xa_unlock_irqrestore(&adev->vm_manager.pasids, flags);
2499 
2500 	return vm;
2501 }
2502 
2503 /**
2504  * amdgpu_vm_put_task_info - reference down the vm task_info ptr
2505  *
2506  * @task_info: task_info struct under discussion.
2507  *
2508  * frees the vm task_info ptr at the last put
2509  */
2510 void amdgpu_vm_put_task_info(struct amdgpu_task_info *task_info)
2511 {
2512 	if (task_info)
2513 		kref_put(&task_info->refcount, amdgpu_vm_destroy_task_info);
2514 }
2515 
2516 /**
2517  * amdgpu_vm_get_task_info_vm - Extracts task info for a vm.
2518  *
2519  * @vm: VM to get info from
2520  *
2521  * Returns the reference counted task_info structure, which must be
2522  * referenced down with amdgpu_vm_put_task_info.
2523  */
2524 struct amdgpu_task_info *
2525 amdgpu_vm_get_task_info_vm(struct amdgpu_vm *vm)
2526 {
2527 	struct amdgpu_task_info *ti = NULL;
2528 
2529 	if (vm) {
2530 		ti = vm->task_info;
2531 		kref_get(&vm->task_info->refcount);
2532 	}
2533 
2534 	return ti;
2535 }
2536 
2537 /**
2538  * amdgpu_vm_get_task_info_pasid - Extracts task info for a PASID.
2539  *
2540  * @adev: drm device pointer
2541  * @pasid: PASID identifier for VM
2542  *
2543  * Returns the reference counted task_info structure, which must be
2544  * referenced down with amdgpu_vm_put_task_info.
2545  */
2546 struct amdgpu_task_info *
2547 amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid)
2548 {
2549 	return amdgpu_vm_get_task_info_vm(
2550 			amdgpu_vm_get_vm_from_pasid(adev, pasid));
2551 }
2552 
2553 static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
2554 {
2555 	vm->task_info = kzalloc_obj(struct amdgpu_task_info);
2556 	if (!vm->task_info)
2557 		return -ENOMEM;
2558 
2559 	kref_init(&vm->task_info->refcount);
2560 	return 0;
2561 }
2562 
2563 /**
2564  * amdgpu_vm_set_task_info - Sets VMs task info.
2565  *
2566  * @vm: vm for which to set the info
2567  */
2568 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
2569 {
2570 	if (!vm->task_info)
2571 		return;
2572 
2573 	if (vm->task_info->task.pid == current->pid)
2574 		return;
2575 
2576 	vm->task_info->task.pid = current->pid;
2577 	get_task_comm(vm->task_info->task.comm, current);
2578 
2579 	vm->task_info->tgid = current->tgid;
2580 	get_task_comm(vm->task_info->process_name, current->group_leader);
2581 }
2582 
2583 /**
2584  * amdgpu_vm_init - initialize a vm instance
2585  *
2586  * @adev: amdgpu_device pointer
2587  * @vm: requested vm
2588  * @xcp_id: GPU partition selection id
2589  * @pasid: the pasid the VM is using on this GPU
2590  *
2591  * Init @vm fields.
2592  *
2593  * Returns:
2594  * 0 for success, error for failure.
2595  */
2596 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2597 		   int32_t xcp_id, uint32_t pasid)
2598 {
2599 	struct amdgpu_bo *root_bo;
2600 	struct amdgpu_bo_vm *root;
2601 	int r, i;
2602 
2603 	vm->va = RB_ROOT_CACHED;
2604 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2605 		vm->reserved_vmid[i] = NULL;
2606 	INIT_LIST_HEAD(&vm->evicted);
2607 	INIT_LIST_HEAD(&vm->evicted_user);
2608 	INIT_LIST_HEAD(&vm->relocated);
2609 	INIT_LIST_HEAD(&vm->moved);
2610 	INIT_LIST_HEAD(&vm->idle);
2611 	INIT_LIST_HEAD(&vm->invalidated);
2612 	spin_lock_init(&vm->status_lock);
2613 	INIT_LIST_HEAD(&vm->freed);
2614 	INIT_LIST_HEAD(&vm->done);
2615 	INIT_KFIFO(vm->faults);
2616 
2617 	r = amdgpu_vm_init_entities(adev, vm);
2618 	if (r)
2619 		return r;
2620 
2621 	ttm_lru_bulk_move_init(&vm->lru_bulk_move);
2622 
2623 	vm->is_compute_context = false;
2624 	vm->need_tlb_fence = amdgpu_userq_enabled(&adev->ddev);
2625 
2626 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2627 				    AMDGPU_VM_USE_CPU_FOR_GFX);
2628 
2629 	dev_dbg(adev->dev, "VM update mode is %s\n",
2630 		vm->use_cpu_for_update ? "CPU" : "SDMA");
2631 	WARN_ONCE((vm->use_cpu_for_update &&
2632 		   !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2633 		  "CPU update of VM recommended only for large BAR system\n");
2634 
2635 	if (vm->use_cpu_for_update)
2636 		vm->update_funcs = &amdgpu_vm_cpu_funcs;
2637 	else
2638 		vm->update_funcs = &amdgpu_vm_sdma_funcs;
2639 
2640 	vm->last_update = dma_fence_get_stub();
2641 	vm->last_unlocked = dma_fence_get_stub();
2642 	vm->last_tlb_flush = dma_fence_get_stub();
2643 	vm->generation = amdgpu_vm_generation(adev, NULL);
2644 
2645 	mutex_init(&vm->eviction_lock);
2646 	vm->evicting = false;
2647 	vm->tlb_fence_context = dma_fence_context_alloc(1);
2648 
2649 	r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level,
2650 				false, &root, xcp_id);
2651 	if (r)
2652 		goto error_free_delayed;
2653 
2654 	root_bo = amdgpu_bo_ref(&root->bo);
2655 	r = amdgpu_bo_reserve(root_bo, true);
2656 	if (r) {
2657 		amdgpu_bo_unref(&root_bo);
2658 		goto error_free_delayed;
2659 	}
2660 
2661 	amdgpu_vm_bo_base_init(&vm->root, vm, root_bo);
2662 	r = dma_resv_reserve_fences(root_bo->tbo.base.resv, 1);
2663 	if (r)
2664 		goto error_free_root;
2665 
2666 	r = amdgpu_vm_pt_clear(adev, vm, root, false);
2667 	if (r)
2668 		goto error_free_root;
2669 
2670 	r = amdgpu_vm_create_task_info(vm);
2671 	if (r)
2672 		dev_dbg(adev->dev, "Failed to create task info for VM\n");
2673 
2674 	/* Store new PASID in XArray (if non-zero) */
2675 	if (pasid != 0) {
2676 		r = xa_err(xa_store_irq(&adev->vm_manager.pasids, pasid, vm, GFP_KERNEL));
2677 		if (r < 0)
2678 			goto error_free_root;
2679 
2680 		vm->pasid = pasid;
2681 	}
2682 
2683 	amdgpu_bo_unreserve(vm->root.bo);
2684 	amdgpu_bo_unref(&root_bo);
2685 
2686 	return 0;
2687 
2688 error_free_root:
2689 	/* If PASID was partially set, erase it from XArray before failing */
2690 	if (vm->pasid != 0) {
2691 		xa_erase_irq(&adev->vm_manager.pasids, vm->pasid);
2692 		vm->pasid = 0;
2693 	}
2694 	amdgpu_vm_pt_free_root(adev, vm);
2695 	amdgpu_bo_unreserve(vm->root.bo);
2696 	amdgpu_bo_unref(&root_bo);
2697 
2698 error_free_delayed:
2699 	dma_fence_put(vm->last_tlb_flush);
2700 	dma_fence_put(vm->last_unlocked);
2701 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
2702 	amdgpu_vm_fini_entities(vm);
2703 
2704 	return r;
2705 }
2706 
2707 /**
2708  * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2709  *
2710  * @adev: amdgpu_device pointer
2711  * @vm: requested vm
2712  *
2713  * This only works on GFX VMs that don't have any BOs added and no
2714  * page tables allocated yet.
2715  *
2716  * Changes the following VM parameters:
2717  * - use_cpu_for_update
2718  * - pte_supports_ats
2719  *
2720  * Reinitializes the page directory to reflect the changed ATS
2721  * setting.
2722  *
2723  * Returns:
2724  * 0 for success, -errno for errors.
2725  */
2726 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2727 {
2728 	int r;
2729 
2730 	r = amdgpu_bo_reserve(vm->root.bo, true);
2731 	if (r)
2732 		return r;
2733 
2734 	/* Update VM state */
2735 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2736 				    AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2737 	dev_dbg(adev->dev, "VM update mode is %s\n",
2738 		vm->use_cpu_for_update ? "CPU" : "SDMA");
2739 	WARN_ONCE((vm->use_cpu_for_update &&
2740 		   !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2741 		  "CPU update of VM recommended only for large BAR system\n");
2742 
2743 	if (vm->use_cpu_for_update) {
2744 		/* Sync with last SDMA update/clear before switching to CPU */
2745 		r = amdgpu_bo_sync_wait(vm->root.bo,
2746 					AMDGPU_FENCE_OWNER_UNDEFINED, true);
2747 		if (r)
2748 			goto unreserve_bo;
2749 
2750 		vm->update_funcs = &amdgpu_vm_cpu_funcs;
2751 		r = amdgpu_vm_pt_map_tables(adev, vm);
2752 		if (r)
2753 			goto unreserve_bo;
2754 
2755 	} else {
2756 		vm->update_funcs = &amdgpu_vm_sdma_funcs;
2757 	}
2758 
2759 	dma_fence_put(vm->last_update);
2760 	vm->last_update = dma_fence_get_stub();
2761 	vm->is_compute_context = true;
2762 	vm->need_tlb_fence = true;
2763 
2764 unreserve_bo:
2765 	amdgpu_bo_unreserve(vm->root.bo);
2766 	return r;
2767 }
2768 
2769 static int amdgpu_vm_stats_is_zero(struct amdgpu_vm *vm)
2770 {
2771 	for (int i = 0; i < __AMDGPU_PL_NUM; ++i) {
2772 		if (!(drm_memory_stats_is_zero(&vm->stats[i].drm) &&
2773 		      vm->stats[i].evicted == 0))
2774 			return false;
2775 	}
2776 	return true;
2777 }
2778 
2779 /**
2780  * amdgpu_vm_fini - tear down a vm instance
2781  *
2782  * @adev: amdgpu_device pointer
2783  * @vm: requested vm
2784  *
2785  * Tear down @vm.
2786  * Unbind the VM and remove all bos from the vm bo list
2787  */
2788 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2789 {
2790 	struct amdgpu_bo_va_mapping *mapping, *tmp;
2791 	bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2792 	struct amdgpu_bo *root;
2793 	unsigned long flags;
2794 	int i;
2795 
2796 	amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2797 
2798 	root = amdgpu_bo_ref(vm->root.bo);
2799 	amdgpu_bo_reserve(root, true);
2800 	/* Remove PASID mapping before destroying VM */
2801 	if (vm->pasid != 0) {
2802 		xa_erase_irq(&adev->vm_manager.pasids, vm->pasid);
2803 		vm->pasid = 0;
2804 	}
2805 	dma_fence_wait(vm->last_unlocked, false);
2806 	dma_fence_put(vm->last_unlocked);
2807 	dma_fence_wait(vm->last_tlb_flush, false);
2808 	/* Make sure that all fence callbacks have completed */
2809 	dma_fence_lock_irqsave(vm->last_tlb_flush, flags);
2810 	dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags);
2811 	dma_fence_put(vm->last_tlb_flush);
2812 
2813 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2814 		if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
2815 			amdgpu_vm_prt_fini(adev, vm);
2816 			prt_fini_needed = false;
2817 		}
2818 
2819 		list_del(&mapping->list);
2820 		amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2821 	}
2822 
2823 	amdgpu_vm_pt_free_root(adev, vm);
2824 	amdgpu_bo_unreserve(root);
2825 	amdgpu_bo_unref(&root);
2826 	WARN_ON(vm->root.bo);
2827 
2828 	amdgpu_vm_fini_entities(vm);
2829 
2830 	if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
2831 		dev_err(adev->dev, "still active bo inside vm\n");
2832 	}
2833 	rbtree_postorder_for_each_entry_safe(mapping, tmp,
2834 					     &vm->va.rb_root, rb) {
2835 		/* Don't remove the mapping here, we don't want to trigger a
2836 		 * rebalance and the tree is about to be destroyed anyway.
2837 		 */
2838 		list_del(&mapping->list);
2839 		kfree(mapping);
2840 	}
2841 
2842 	dma_fence_put(vm->last_update);
2843 
2844 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) {
2845 		amdgpu_vmid_free_reserved(adev, vm, i);
2846 	}
2847 
2848 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
2849 
2850 	if (!amdgpu_vm_stats_is_zero(vm)) {
2851 		struct amdgpu_task_info *ti = vm->task_info;
2852 
2853 		dev_warn(adev->dev,
2854 			 "VM memory stats for proc %s(%d) task %s(%d) is non-zero when fini\n",
2855 			 ti->process_name, ti->task.pid, ti->task.comm, ti->tgid);
2856 	}
2857 
2858 	amdgpu_vm_put_task_info(vm->task_info);
2859 }
2860 
2861 /**
2862  * amdgpu_vm_manager_init - init the VM manager
2863  *
2864  * @adev: amdgpu_device pointer
2865  *
2866  * Initialize the VM manager structures
2867  */
2868 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2869 {
2870 	/* Concurrent flushes are only possible starting with Vega10 and
2871 	 * are broken on Navi10 and Navi14.
2872 	 */
2873 	adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 ||
2874 					      adev->asic_type == CHIP_NAVI10 ||
2875 					      adev->asic_type == CHIP_NAVI14);
2876 	amdgpu_vmid_mgr_init(adev);
2877 
2878 	spin_lock_init(&adev->vm_manager.prt_lock);
2879 	atomic_set(&adev->vm_manager.num_prt_users, 0);
2880 
2881 	/* If not overridden by the user, by default, only in large BAR systems
2882 	 * Compute VM tables will be updated by CPU
2883 	 */
2884 #ifdef CONFIG_X86_64
2885 	if (amdgpu_vm_update_mode == -1) {
2886 		/* For asic with VF MMIO access protection
2887 		 * avoid using CPU for VM table updates
2888 		 */
2889 		if (amdgpu_gmc_vram_full_visible(&adev->gmc) &&
2890 		    !amdgpu_sriov_vf_mmio_access_protection(adev))
2891 			adev->vm_manager.vm_update_mode =
2892 				AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2893 		else
2894 			adev->vm_manager.vm_update_mode = 0;
2895 	} else
2896 		adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2897 #else
2898 	adev->vm_manager.vm_update_mode = 0;
2899 #endif
2900 
2901 	xa_init_flags(&adev->vm_manager.pasids, XA_FLAGS_LOCK_IRQ);
2902 }
2903 
2904 /**
2905  * amdgpu_vm_manager_fini - cleanup VM manager
2906  *
2907  * @adev: amdgpu_device pointer
2908  *
2909  * Cleanup the VM manager and free resources.
2910  */
2911 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2912 {
2913 	WARN_ON(!xa_empty(&adev->vm_manager.pasids));
2914 	xa_destroy(&adev->vm_manager.pasids);
2915 
2916 	amdgpu_vmid_mgr_fini(adev);
2917 	amdgpu_pasid_mgr_cleanup();
2918 }
2919 
2920 /**
2921  * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
2922  *
2923  * @dev: drm device pointer
2924  * @data: drm_amdgpu_vm
2925  * @filp: drm file pointer
2926  *
2927  * Returns:
2928  * 0 for success, -errno for errors.
2929  */
2930 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2931 {
2932 	union drm_amdgpu_vm *args = data;
2933 	struct amdgpu_device *adev = drm_to_adev(dev);
2934 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
2935 	struct amdgpu_vm *vm = &fpriv->vm;
2936 
2937 	/* No valid flags defined yet */
2938 	if (args->in.flags)
2939 		return -EINVAL;
2940 
2941 	switch (args->in.op) {
2942 	case AMDGPU_VM_OP_RESERVE_VMID:
2943 		/* We only have requirement to reserve vmid from gfxhub */
2944 		return amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
2945 	case AMDGPU_VM_OP_UNRESERVE_VMID:
2946 		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
2947 		break;
2948 	default:
2949 		return -EINVAL;
2950 	}
2951 
2952 	return 0;
2953 }
2954 
2955 /**
2956  * amdgpu_vm_lock_by_pasid - return an amdgpu_vm and its root bo from a pasid, if possible.
2957  * @adev: amdgpu device pointer
2958  * @root: root BO of the VM
2959  * @pasid: PASID of the VM
2960  * The caller needs to unreserve and unref the root bo on success.
2961  */
2962 struct amdgpu_vm *amdgpu_vm_lock_by_pasid(struct amdgpu_device *adev,
2963 					  struct amdgpu_bo **root, u32 pasid)
2964 {
2965 	unsigned long irqflags;
2966 	struct amdgpu_vm *vm;
2967 	int r;
2968 
2969 	xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2970 	vm = xa_load(&adev->vm_manager.pasids, pasid);
2971 	*root = vm ? amdgpu_bo_ref(vm->root.bo) : NULL;
2972 	xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2973 
2974 	if (!*root)
2975 		return NULL;
2976 
2977 	r = amdgpu_bo_reserve(*root, true);
2978 	if (r)
2979 		goto error_unref;
2980 
2981 	/* Double check that the VM still exists */
2982 	xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2983 	vm = xa_load(&adev->vm_manager.pasids, pasid);
2984 	if (vm && vm->root.bo != *root)
2985 		vm = NULL;
2986 	xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2987 	if (!vm)
2988 		goto error_unlock;
2989 
2990 	return vm;
2991 error_unlock:
2992 	amdgpu_bo_unreserve(*root);
2993 
2994 error_unref:
2995 	amdgpu_bo_unref(root);
2996 	return NULL;
2997 }
2998 
2999 /**
3000  * amdgpu_vm_handle_fault - graceful handling of VM faults.
3001  * @adev: amdgpu device pointer
3002  * @pasid: PASID of the VM
3003  * @ts: Timestamp of the fault
3004  * @vmid: VMID, only used for GFX 9.4.3.
3005  * @node_id: Node_id received in IH cookie. Only applicable for
3006  *           GFX 9.4.3.
3007  * @addr: Address of the fault
3008  * @write_fault: true is write fault, false is read fault
3009  *
3010  * Try to gracefully handle a VM fault. Return true if the fault was handled and
3011  * shouldn't be reported any more.
3012  */
3013 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
3014 			    u32 vmid, u32 node_id, uint64_t addr,
3015 			    uint64_t ts, bool write_fault)
3016 {
3017 	bool is_compute_context = false;
3018 	struct amdgpu_bo *root;
3019 	uint64_t value, flags;
3020 	struct amdgpu_vm *vm;
3021 	int r;
3022 
3023 	vm = amdgpu_vm_lock_by_pasid(adev, &root, pasid);
3024 	if (!vm)
3025 		return false;
3026 
3027 	is_compute_context = vm->is_compute_context;
3028 
3029 	if (is_compute_context) {
3030 		/* Unreserve root since svm_range_restore_pages might try to reserve it. */
3031 		/* TODO: rework svm_range_restore_pages so that this isn't necessary. */
3032 		amdgpu_bo_unreserve(root);
3033 
3034 		if (!svm_range_restore_pages(adev, pasid, vmid,
3035 					     node_id, addr >> PAGE_SHIFT, ts, write_fault)) {
3036 			amdgpu_bo_unref(&root);
3037 			return true;
3038 		}
3039 		amdgpu_bo_unref(&root);
3040 
3041 		/* Re-acquire the VM lock, could be that the VM was freed in between. */
3042 		vm = amdgpu_vm_lock_by_pasid(adev, &root, pasid);
3043 		if (!vm)
3044 			return false;
3045 	}
3046 
3047 	addr /= AMDGPU_GPU_PAGE_SIZE;
3048 	flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED |
3049 		AMDGPU_PTE_SYSTEM;
3050 
3051 	if (is_compute_context) {
3052 		/* Intentionally setting invalid PTE flag
3053 		 * combination to force a no-retry-fault
3054 		 */
3055 		flags = AMDGPU_VM_NORETRY_FLAGS;
3056 		value = 0;
3057 	} else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) {
3058 		/* Redirect the access to the dummy page */
3059 		value = adev->dummy_page_addr;
3060 		flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE |
3061 			AMDGPU_PTE_WRITEABLE;
3062 
3063 	} else {
3064 		/* Let the hw retry silently on the PTE */
3065 		value = 0;
3066 	}
3067 
3068 	r = dma_resv_reserve_fences(root->tbo.base.resv, 1);
3069 	if (r) {
3070 		pr_debug("failed %d to reserve fence slot\n", r);
3071 		goto error_unlock;
3072 	}
3073 
3074 	r = amdgpu_vm_update_range(adev, vm, true, false, false, false,
3075 				   NULL, addr, addr, flags, value, 0, NULL, NULL, NULL);
3076 	if (r)
3077 		goto error_unlock;
3078 
3079 	r = amdgpu_vm_update_pdes(adev, vm, true);
3080 
3081 error_unlock:
3082 	amdgpu_bo_unreserve(root);
3083 	if (r < 0)
3084 		dev_err(adev->dev, "Can't handle page fault (%d)\n", r);
3085 
3086 	amdgpu_bo_unref(&root);
3087 
3088 	return false;
3089 }
3090 
3091 #if defined(CONFIG_DEBUG_FS)
3092 /**
3093  * amdgpu_debugfs_vm_bo_info  - print BO info for the VM
3094  *
3095  * @vm: Requested VM for printing BO info
3096  * @m: debugfs file
3097  *
3098  * Print BO information in debugfs file for the VM
3099  */
3100 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m)
3101 {
3102 	struct amdgpu_bo_va *bo_va, *tmp;
3103 	u64 total_idle = 0;
3104 	u64 total_evicted = 0;
3105 	u64 total_relocated = 0;
3106 	u64 total_moved = 0;
3107 	u64 total_invalidated = 0;
3108 	u64 total_done = 0;
3109 	unsigned int total_idle_objs = 0;
3110 	unsigned int total_evicted_objs = 0;
3111 	unsigned int total_relocated_objs = 0;
3112 	unsigned int total_moved_objs = 0;
3113 	unsigned int total_invalidated_objs = 0;
3114 	unsigned int total_done_objs = 0;
3115 	unsigned int id = 0;
3116 
3117 	amdgpu_vm_assert_locked(vm);
3118 
3119 	spin_lock(&vm->status_lock);
3120 	seq_puts(m, "\tIdle BOs:\n");
3121 	list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
3122 		if (!bo_va->base.bo)
3123 			continue;
3124 		total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
3125 	}
3126 	total_idle_objs = id;
3127 	id = 0;
3128 
3129 	seq_puts(m, "\tEvicted BOs:\n");
3130 	list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
3131 		if (!bo_va->base.bo)
3132 			continue;
3133 		total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
3134 	}
3135 	total_evicted_objs = id;
3136 	id = 0;
3137 
3138 	seq_puts(m, "\tRelocated BOs:\n");
3139 	list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
3140 		if (!bo_va->base.bo)
3141 			continue;
3142 		total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
3143 	}
3144 	total_relocated_objs = id;
3145 	id = 0;
3146 
3147 	seq_puts(m, "\tMoved BOs:\n");
3148 	list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
3149 		if (!bo_va->base.bo)
3150 			continue;
3151 		total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
3152 	}
3153 	total_moved_objs = id;
3154 	id = 0;
3155 
3156 	seq_puts(m, "\tInvalidated BOs:\n");
3157 	list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
3158 		if (!bo_va->base.bo)
3159 			continue;
3160 		total_invalidated += amdgpu_bo_print_info(id++,	bo_va->base.bo, m);
3161 	}
3162 	total_invalidated_objs = id;
3163 	id = 0;
3164 
3165 	seq_puts(m, "\tDone BOs:\n");
3166 	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
3167 		if (!bo_va->base.bo)
3168 			continue;
3169 		total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
3170 	}
3171 	spin_unlock(&vm->status_lock);
3172 	total_done_objs = id;
3173 
3174 	seq_printf(m, "\tTotal idle size:        %12lld\tobjs:\t%d\n", total_idle,
3175 		   total_idle_objs);
3176 	seq_printf(m, "\tTotal evicted size:     %12lld\tobjs:\t%d\n", total_evicted,
3177 		   total_evicted_objs);
3178 	seq_printf(m, "\tTotal relocated size:   %12lld\tobjs:\t%d\n", total_relocated,
3179 		   total_relocated_objs);
3180 	seq_printf(m, "\tTotal moved size:       %12lld\tobjs:\t%d\n", total_moved,
3181 		   total_moved_objs);
3182 	seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated,
3183 		   total_invalidated_objs);
3184 	seq_printf(m, "\tTotal done size:        %12lld\tobjs:\t%d\n", total_done,
3185 		   total_done_objs);
3186 }
3187 #endif
3188 
3189 /**
3190  * amdgpu_vm_update_fault_cache - update cached fault into.
3191  * @adev: amdgpu device pointer
3192  * @pasid: PASID of the VM
3193  * @addr: Address of the fault
3194  * @status: GPUVM fault status register
3195  * @vmhub: which vmhub got the fault
3196  *
3197  * Cache the fault info for later use by userspace in debugging.
3198  */
3199 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
3200 				  unsigned int pasid,
3201 				  uint64_t addr,
3202 				  uint32_t status,
3203 				  unsigned int vmhub)
3204 {
3205 	struct amdgpu_vm *vm;
3206 	unsigned long flags;
3207 
3208 	xa_lock_irqsave(&adev->vm_manager.pasids, flags);
3209 
3210 	vm = xa_load(&adev->vm_manager.pasids, pasid);
3211 	/* Don't update the fault cache if status is 0.  In the multiple
3212 	 * fault case, subsequent faults will return a 0 status which is
3213 	 * useless for userspace and replaces the useful fault status, so
3214 	 * only update if status is non-0.
3215 	 */
3216 	if (vm && status) {
3217 		vm->fault_info.addr = addr;
3218 		vm->fault_info.status = status;
3219 		/*
3220 		 * Update the fault information globally for later usage
3221 		 * when vm could be stale or freed.
3222 		 */
3223 		adev->vm_manager.fault_info.addr = addr;
3224 		adev->vm_manager.fault_info.vmhub = vmhub;
3225 		adev->vm_manager.fault_info.status = status;
3226 
3227 		if (AMDGPU_IS_GFXHUB(vmhub)) {
3228 			vm->fault_info.vmhub = AMDGPU_VMHUB_TYPE_GFX;
3229 			vm->fault_info.vmhub |=
3230 				(vmhub - AMDGPU_GFXHUB_START) << AMDGPU_VMHUB_IDX_SHIFT;
3231 		} else if (AMDGPU_IS_MMHUB0(vmhub)) {
3232 			vm->fault_info.vmhub = AMDGPU_VMHUB_TYPE_MM0;
3233 			vm->fault_info.vmhub |=
3234 				(vmhub - AMDGPU_MMHUB0_START) << AMDGPU_VMHUB_IDX_SHIFT;
3235 		} else if (AMDGPU_IS_MMHUB1(vmhub)) {
3236 			vm->fault_info.vmhub = AMDGPU_VMHUB_TYPE_MM1;
3237 			vm->fault_info.vmhub |=
3238 				(vmhub - AMDGPU_MMHUB1_START) << AMDGPU_VMHUB_IDX_SHIFT;
3239 		} else {
3240 			WARN_ONCE(1, "Invalid vmhub %u\n", vmhub);
3241 		}
3242 	}
3243 	xa_unlock_irqrestore(&adev->vm_manager.pasids, flags);
3244 }
3245 
3246 void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
3247 			       struct amdgpu_task_info *task_info)
3248 {
3249 	dev_err(adev->dev,
3250 		" Process %s pid %d thread %s pid %d\n",
3251 		task_info->process_name, task_info->tgid,
3252 		task_info->task.comm, task_info->task.pid);
3253 }
3254 
3255 void amdgpu_sdma_set_vm_pte_scheds(struct amdgpu_device *adev,
3256 				   const struct amdgpu_vm_pte_funcs *vm_pte_funcs)
3257 {
3258 	struct drm_gpu_scheduler *sched;
3259 	int i;
3260 
3261 	for (i = 0; i < adev->sdma.num_instances; i++) {
3262 		if (adev->sdma.has_page_queue)
3263 			sched = &adev->sdma.instance[i].page.sched;
3264 		else
3265 			sched = &adev->sdma.instance[i].ring.sched;
3266 		adev->vm_manager.vm_pte_scheds[i] = sched;
3267 	}
3268 	adev->vm_manager.vm_pte_num_scheds = adev->sdma.num_instances;
3269 	adev->vm_manager.vm_pte_funcs = vm_pte_funcs;
3270 }
3271