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 "amdgpu.h"
37 #include "amdgpu_trace.h"
38 #include "amdgpu_amdkfd.h"
39 #include "amdgpu_gmc.h"
40 #include "amdgpu_xgmi.h"
41 #include "amdgpu_dma_buf.h"
42 #include "amdgpu_res_cursor.h"
43 #include "kfd_svm.h"
44 
45 /**
46  * DOC: GPUVM
47  *
48  * GPUVM is the MMU functionality provided on the GPU.
49  * GPUVM is similar to the legacy GART on older asics, however
50  * rather than there being a single global GART table
51  * for the entire GPU, there can be multiple GPUVM page tables active
52  * at any given time.  The GPUVM page tables can contain a mix
53  * VRAM pages and system pages (both memory and MMIO) and system pages
54  * can be mapped as snooped (cached system pages) or unsnooped
55  * (uncached system pages).
56  *
57  * Each active GPUVM has an ID associated with it and there is a page table
58  * linked with each VMID.  When executing a command buffer,
59  * the kernel tells the engine what VMID to use for that command
60  * buffer.  VMIDs are allocated dynamically as commands are submitted.
61  * The userspace drivers maintain their own address space and the kernel
62  * sets up their pages tables accordingly when they submit their
63  * command buffers and a VMID is assigned.
64  * The hardware supports up to 16 active GPUVMs at any given time.
65  *
66  * Each GPUVM is represented by a 1-2 or 1-5 level page table, depending
67  * on the ASIC family.  GPUVM supports RWX attributes on each page as well
68  * as other features such as encryption and caching attributes.
69  *
70  * VMID 0 is special.  It is the GPUVM used for the kernel driver.  In
71  * addition to an aperture managed by a page table, VMID 0 also has
72  * several other apertures.  There is an aperture for direct access to VRAM
73  * and there is a legacy AGP aperture which just forwards accesses directly
74  * to the matching system physical addresses (or IOVAs when an IOMMU is
75  * present).  These apertures provide direct access to these memories without
76  * incurring the overhead of a page table.  VMID 0 is used by the kernel
77  * driver for tasks like memory management.
78  *
79  * GPU clients (i.e., engines on the GPU) use GPUVM VMIDs to access memory.
80  * For user applications, each application can have their own unique GPUVM
81  * address space.  The application manages the address space and the kernel
82  * driver manages the GPUVM page tables for each process.  If an GPU client
83  * accesses an invalid page, it will generate a GPU page fault, similar to
84  * accessing an invalid page on a CPU.
85  */
86 
87 #define START(node) ((node)->start)
88 #define LAST(node) ((node)->last)
89 
90 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
91 		     START, LAST, static, amdgpu_vm_it)
92 
93 #undef START
94 #undef LAST
95 
96 /**
97  * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
98  */
99 struct amdgpu_prt_cb {
100 
101 	/**
102 	 * @adev: amdgpu device
103 	 */
104 	struct amdgpu_device *adev;
105 
106 	/**
107 	 * @cb: callback
108 	 */
109 	struct dma_fence_cb cb;
110 };
111 
112 /**
113  * struct amdgpu_vm_tlb_seq_cb - Helper to increment the TLB flush sequence
114  */
115 struct amdgpu_vm_tlb_seq_cb {
116 	/**
117 	 * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
118 	 */
119 	struct amdgpu_vm *vm;
120 
121 	/**
122 	 * @cb: callback
123 	 */
124 	struct dma_fence_cb cb;
125 };
126 
127 /**
128  * amdgpu_vm_set_pasid - manage pasid and vm ptr mapping
129  *
130  * @adev: amdgpu_device pointer
131  * @vm: amdgpu_vm pointer
132  * @pasid: the pasid the VM is using on this GPU
133  *
134  * Set the pasid this VM is using on this GPU, can also be used to remove the
135  * pasid by passing in zero.
136  *
137  */
138 int amdgpu_vm_set_pasid(struct amdgpu_device *adev, struct amdgpu_vm *vm,
139 			u32 pasid)
140 {
141 	int r;
142 
143 	if (vm->pasid == pasid)
144 		return 0;
145 
146 	if (vm->pasid) {
147 		r = xa_err(xa_erase_irq(&adev->vm_manager.pasids, vm->pasid));
148 		if (r < 0)
149 			return r;
150 
151 		vm->pasid = 0;
152 	}
153 
154 	if (pasid) {
155 		r = xa_err(xa_store_irq(&adev->vm_manager.pasids, pasid, vm,
156 					GFP_KERNEL));
157 		if (r < 0)
158 			return r;
159 
160 		vm->pasid = pasid;
161 	}
162 
163 
164 	return 0;
165 }
166 
167 /**
168  * amdgpu_vm_bo_evicted - vm_bo is evicted
169  *
170  * @vm_bo: vm_bo which is evicted
171  *
172  * State for PDs/PTs and per VM BOs which are not at the location they should
173  * be.
174  */
175 static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo)
176 {
177 	struct amdgpu_vm *vm = vm_bo->vm;
178 	struct amdgpu_bo *bo = vm_bo->bo;
179 
180 	vm_bo->moved = true;
181 	spin_lock(&vm_bo->vm->status_lock);
182 	if (bo->tbo.type == ttm_bo_type_kernel)
183 		list_move(&vm_bo->vm_status, &vm->evicted);
184 	else
185 		list_move_tail(&vm_bo->vm_status, &vm->evicted);
186 	spin_unlock(&vm_bo->vm->status_lock);
187 }
188 /**
189  * amdgpu_vm_bo_moved - vm_bo is moved
190  *
191  * @vm_bo: vm_bo which is moved
192  *
193  * State for per VM BOs which are moved, but that change is not yet reflected
194  * in the page tables.
195  */
196 static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo)
197 {
198 	spin_lock(&vm_bo->vm->status_lock);
199 	list_move(&vm_bo->vm_status, &vm_bo->vm->moved);
200 	spin_unlock(&vm_bo->vm->status_lock);
201 }
202 
203 /**
204  * amdgpu_vm_bo_idle - vm_bo is idle
205  *
206  * @vm_bo: vm_bo which is now idle
207  *
208  * State for PDs/PTs and per VM BOs which have gone through the state machine
209  * and are now idle.
210  */
211 static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo)
212 {
213 	spin_lock(&vm_bo->vm->status_lock);
214 	list_move(&vm_bo->vm_status, &vm_bo->vm->idle);
215 	spin_unlock(&vm_bo->vm->status_lock);
216 	vm_bo->moved = false;
217 }
218 
219 /**
220  * amdgpu_vm_bo_invalidated - vm_bo is invalidated
221  *
222  * @vm_bo: vm_bo which is now invalidated
223  *
224  * State for normal BOs which are invalidated and that change not yet reflected
225  * in the PTs.
226  */
227 static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo)
228 {
229 	spin_lock(&vm_bo->vm->status_lock);
230 	list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated);
231 	spin_unlock(&vm_bo->vm->status_lock);
232 }
233 
234 /**
235  * amdgpu_vm_bo_relocated - vm_bo is reloacted
236  *
237  * @vm_bo: vm_bo which is relocated
238  *
239  * State for PDs/PTs which needs to update their parent PD.
240  * For the root PD, just move to idle state.
241  */
242 static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo)
243 {
244 	if (vm_bo->bo->parent) {
245 		spin_lock(&vm_bo->vm->status_lock);
246 		list_move(&vm_bo->vm_status, &vm_bo->vm->relocated);
247 		spin_unlock(&vm_bo->vm->status_lock);
248 	} else {
249 		amdgpu_vm_bo_idle(vm_bo);
250 	}
251 }
252 
253 /**
254  * amdgpu_vm_bo_done - vm_bo is done
255  *
256  * @vm_bo: vm_bo which is now done
257  *
258  * State for normal BOs which are invalidated and that change has been updated
259  * in the PTs.
260  */
261 static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo)
262 {
263 	spin_lock(&vm_bo->vm->status_lock);
264 	list_move(&vm_bo->vm_status, &vm_bo->vm->done);
265 	spin_unlock(&vm_bo->vm->status_lock);
266 }
267 
268 /**
269  * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
270  *
271  * @base: base structure for tracking BO usage in a VM
272  * @vm: vm to which bo is to be added
273  * @bo: amdgpu buffer object
274  *
275  * Initialize a bo_va_base structure and add it to the appropriate lists
276  *
277  */
278 void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
279 			    struct amdgpu_vm *vm, struct amdgpu_bo *bo)
280 {
281 	base->vm = vm;
282 	base->bo = bo;
283 	base->next = NULL;
284 	INIT_LIST_HEAD(&base->vm_status);
285 
286 	if (!bo)
287 		return;
288 	base->next = bo->vm_bo;
289 	bo->vm_bo = base;
290 
291 	if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv)
292 		return;
293 
294 	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
295 
296 	ttm_bo_set_bulk_move(&bo->tbo, &vm->lru_bulk_move);
297 	if (bo->tbo.type == ttm_bo_type_kernel && bo->parent)
298 		amdgpu_vm_bo_relocated(base);
299 	else
300 		amdgpu_vm_bo_idle(base);
301 
302 	if (bo->preferred_domains &
303 	    amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type))
304 		return;
305 
306 	/*
307 	 * we checked all the prerequisites, but it looks like this per vm bo
308 	 * is currently evicted. add the bo to the evicted list to make sure it
309 	 * is validated on next vm use to avoid fault.
310 	 * */
311 	amdgpu_vm_bo_evicted(base);
312 }
313 
314 /**
315  * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
316  *
317  * @vm: vm providing the BOs
318  * @validated: head of validation list
319  * @entry: entry to add
320  *
321  * Add the page directory to the list of BOs to
322  * validate for command submission.
323  */
324 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
325 			 struct list_head *validated,
326 			 struct amdgpu_bo_list_entry *entry)
327 {
328 	entry->priority = 0;
329 	entry->tv.bo = &vm->root.bo->tbo;
330 	/* Two for VM updates, one for TTM and one for the CS job */
331 	entry->tv.num_shared = 4;
332 	entry->user_pages = NULL;
333 	list_add(&entry->tv.head, validated);
334 }
335 
336 /**
337  * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
338  *
339  * @adev: amdgpu device pointer
340  * @vm: vm providing the BOs
341  *
342  * Move all BOs to the end of LRU and remember their positions to put them
343  * together.
344  */
345 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
346 				struct amdgpu_vm *vm)
347 {
348 	spin_lock(&adev->mman.bdev.lru_lock);
349 	ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
350 	spin_unlock(&adev->mman.bdev.lru_lock);
351 }
352 
353 /**
354  * amdgpu_vm_validate_pt_bos - validate the page table BOs
355  *
356  * @adev: amdgpu device pointer
357  * @vm: vm providing the BOs
358  * @validate: callback to do the validation
359  * @param: parameter for the validation callback
360  *
361  * Validate the page table BOs on command submission if neccessary.
362  *
363  * Returns:
364  * Validation result.
365  */
366 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
367 			      int (*validate)(void *p, struct amdgpu_bo *bo),
368 			      void *param)
369 {
370 	struct amdgpu_vm_bo_base *bo_base;
371 	struct amdgpu_bo *shadow;
372 	struct amdgpu_bo *bo;
373 	int r;
374 
375 	spin_lock(&vm->status_lock);
376 	while (!list_empty(&vm->evicted)) {
377 		bo_base = list_first_entry(&vm->evicted,
378 					   struct amdgpu_vm_bo_base,
379 					   vm_status);
380 		spin_unlock(&vm->status_lock);
381 
382 		bo = bo_base->bo;
383 		shadow = amdgpu_bo_shadowed(bo);
384 
385 		r = validate(param, bo);
386 		if (r)
387 			return r;
388 		if (shadow) {
389 			r = validate(param, shadow);
390 			if (r)
391 				return r;
392 		}
393 
394 		if (bo->tbo.type != ttm_bo_type_kernel) {
395 			amdgpu_vm_bo_moved(bo_base);
396 		} else {
397 			vm->update_funcs->map_table(to_amdgpu_bo_vm(bo));
398 			amdgpu_vm_bo_relocated(bo_base);
399 		}
400 		spin_lock(&vm->status_lock);
401 	}
402 	spin_unlock(&vm->status_lock);
403 
404 	amdgpu_vm_eviction_lock(vm);
405 	vm->evicting = false;
406 	amdgpu_vm_eviction_unlock(vm);
407 
408 	return 0;
409 }
410 
411 /**
412  * amdgpu_vm_ready - check VM is ready for updates
413  *
414  * @vm: VM to check
415  *
416  * Check if all VM PDs/PTs are ready for updates
417  *
418  * Returns:
419  * True if VM is not evicting.
420  */
421 bool amdgpu_vm_ready(struct amdgpu_vm *vm)
422 {
423 	bool empty;
424 	bool ret;
425 
426 	amdgpu_vm_eviction_lock(vm);
427 	ret = !vm->evicting;
428 	amdgpu_vm_eviction_unlock(vm);
429 
430 	spin_lock(&vm->status_lock);
431 	empty = list_empty(&vm->evicted);
432 	spin_unlock(&vm->status_lock);
433 
434 	return ret && empty;
435 }
436 
437 /**
438  * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
439  *
440  * @adev: amdgpu_device pointer
441  */
442 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
443 {
444 	const struct amdgpu_ip_block *ip_block;
445 	bool has_compute_vm_bug;
446 	struct amdgpu_ring *ring;
447 	int i;
448 
449 	has_compute_vm_bug = false;
450 
451 	ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
452 	if (ip_block) {
453 		/* Compute has a VM bug for GFX version < 7.
454 		   Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
455 		if (ip_block->version->major <= 7)
456 			has_compute_vm_bug = true;
457 		else if (ip_block->version->major == 8)
458 			if (adev->gfx.mec_fw_version < 673)
459 				has_compute_vm_bug = true;
460 	}
461 
462 	for (i = 0; i < adev->num_rings; i++) {
463 		ring = adev->rings[i];
464 		if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
465 			/* only compute rings */
466 			ring->has_compute_vm_bug = has_compute_vm_bug;
467 		else
468 			ring->has_compute_vm_bug = false;
469 	}
470 }
471 
472 /**
473  * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
474  *
475  * @ring: ring on which the job will be submitted
476  * @job: job to submit
477  *
478  * Returns:
479  * True if sync is needed.
480  */
481 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
482 				  struct amdgpu_job *job)
483 {
484 	struct amdgpu_device *adev = ring->adev;
485 	unsigned vmhub = ring->funcs->vmhub;
486 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
487 	struct amdgpu_vmid *id;
488 	bool gds_switch_needed;
489 	bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
490 
491 	if (job->vmid == 0)
492 		return false;
493 	id = &id_mgr->ids[job->vmid];
494 	gds_switch_needed = ring->funcs->emit_gds_switch && (
495 		id->gds_base != job->gds_base ||
496 		id->gds_size != job->gds_size ||
497 		id->gws_base != job->gws_base ||
498 		id->gws_size != job->gws_size ||
499 		id->oa_base != job->oa_base ||
500 		id->oa_size != job->oa_size);
501 
502 	if (amdgpu_vmid_had_gpu_reset(adev, id))
503 		return true;
504 
505 	return vm_flush_needed || gds_switch_needed;
506 }
507 
508 /**
509  * amdgpu_vm_flush - hardware flush the vm
510  *
511  * @ring: ring to use for flush
512  * @job:  related job
513  * @need_pipe_sync: is pipe sync needed
514  *
515  * Emit a VM flush when it is necessary.
516  *
517  * Returns:
518  * 0 on success, errno otherwise.
519  */
520 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
521 		    bool need_pipe_sync)
522 {
523 	struct amdgpu_device *adev = ring->adev;
524 	unsigned vmhub = ring->funcs->vmhub;
525 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
526 	struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
527 	bool gds_switch_needed = ring->funcs->emit_gds_switch && (
528 		id->gds_base != job->gds_base ||
529 		id->gds_size != job->gds_size ||
530 		id->gws_base != job->gws_base ||
531 		id->gws_size != job->gws_size ||
532 		id->oa_base != job->oa_base ||
533 		id->oa_size != job->oa_size);
534 	bool vm_flush_needed = job->vm_needs_flush;
535 	struct dma_fence *fence = NULL;
536 	bool pasid_mapping_needed = false;
537 	unsigned patch_offset = 0;
538 	bool update_spm_vmid_needed = (job->vm && (job->vm->reserved_vmid[vmhub] != NULL));
539 	int r;
540 
541 	if (update_spm_vmid_needed && adev->gfx.rlc.funcs->update_spm_vmid)
542 		adev->gfx.rlc.funcs->update_spm_vmid(adev, job->vmid);
543 
544 	if (amdgpu_vmid_had_gpu_reset(adev, id)) {
545 		gds_switch_needed = true;
546 		vm_flush_needed = true;
547 		pasid_mapping_needed = true;
548 	}
549 
550 	mutex_lock(&id_mgr->lock);
551 	if (id->pasid != job->pasid || !id->pasid_mapping ||
552 	    !dma_fence_is_signaled(id->pasid_mapping))
553 		pasid_mapping_needed = true;
554 	mutex_unlock(&id_mgr->lock);
555 
556 	gds_switch_needed &= !!ring->funcs->emit_gds_switch;
557 	vm_flush_needed &= !!ring->funcs->emit_vm_flush  &&
558 			job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
559 	pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
560 		ring->funcs->emit_wreg;
561 
562 	if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
563 		return 0;
564 
565 	amdgpu_ring_ib_begin(ring);
566 	if (ring->funcs->init_cond_exec)
567 		patch_offset = amdgpu_ring_init_cond_exec(ring);
568 
569 	if (need_pipe_sync)
570 		amdgpu_ring_emit_pipeline_sync(ring);
571 
572 	if (vm_flush_needed) {
573 		trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
574 		amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
575 	}
576 
577 	if (pasid_mapping_needed)
578 		amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
579 
580 	if (vm_flush_needed || pasid_mapping_needed) {
581 		r = amdgpu_fence_emit(ring, &fence, NULL, 0);
582 		if (r)
583 			return r;
584 	}
585 
586 	if (vm_flush_needed) {
587 		mutex_lock(&id_mgr->lock);
588 		dma_fence_put(id->last_flush);
589 		id->last_flush = dma_fence_get(fence);
590 		id->current_gpu_reset_count =
591 			atomic_read(&adev->gpu_reset_counter);
592 		mutex_unlock(&id_mgr->lock);
593 	}
594 
595 	if (pasid_mapping_needed) {
596 		mutex_lock(&id_mgr->lock);
597 		id->pasid = job->pasid;
598 		dma_fence_put(id->pasid_mapping);
599 		id->pasid_mapping = dma_fence_get(fence);
600 		mutex_unlock(&id_mgr->lock);
601 	}
602 	dma_fence_put(fence);
603 
604 	if (!ring->is_mes_queue && ring->funcs->emit_gds_switch &&
605 	    gds_switch_needed) {
606 		id->gds_base = job->gds_base;
607 		id->gds_size = job->gds_size;
608 		id->gws_base = job->gws_base;
609 		id->gws_size = job->gws_size;
610 		id->oa_base = job->oa_base;
611 		id->oa_size = job->oa_size;
612 		amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
613 					    job->gds_size, job->gws_base,
614 					    job->gws_size, job->oa_base,
615 					    job->oa_size);
616 	}
617 
618 	if (ring->funcs->patch_cond_exec)
619 		amdgpu_ring_patch_cond_exec(ring, patch_offset);
620 
621 	/* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
622 	if (ring->funcs->emit_switch_buffer) {
623 		amdgpu_ring_emit_switch_buffer(ring);
624 		amdgpu_ring_emit_switch_buffer(ring);
625 	}
626 	amdgpu_ring_ib_end(ring);
627 	return 0;
628 }
629 
630 /**
631  * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
632  *
633  * @vm: requested vm
634  * @bo: requested buffer object
635  *
636  * Find @bo inside the requested vm.
637  * Search inside the @bos vm list for the requested vm
638  * Returns the found bo_va or NULL if none is found
639  *
640  * Object has to be reserved!
641  *
642  * Returns:
643  * Found bo_va or NULL.
644  */
645 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
646 				       struct amdgpu_bo *bo)
647 {
648 	struct amdgpu_vm_bo_base *base;
649 
650 	for (base = bo->vm_bo; base; base = base->next) {
651 		if (base->vm != vm)
652 			continue;
653 
654 		return container_of(base, struct amdgpu_bo_va, base);
655 	}
656 	return NULL;
657 }
658 
659 /**
660  * amdgpu_vm_map_gart - Resolve gart mapping of addr
661  *
662  * @pages_addr: optional DMA address to use for lookup
663  * @addr: the unmapped addr
664  *
665  * Look up the physical address of the page that the pte resolves
666  * to.
667  *
668  * Returns:
669  * The pointer for the page table entry.
670  */
671 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
672 {
673 	uint64_t result;
674 
675 	/* page table offset */
676 	result = pages_addr[addr >> PAGE_SHIFT];
677 
678 	/* in case cpu page size != gpu page size*/
679 	result |= addr & (~PAGE_MASK);
680 
681 	result &= 0xFFFFFFFFFFFFF000ULL;
682 
683 	return result;
684 }
685 
686 /**
687  * amdgpu_vm_update_pdes - make sure that all directories are valid
688  *
689  * @adev: amdgpu_device pointer
690  * @vm: requested vm
691  * @immediate: submit immediately to the paging queue
692  *
693  * Makes sure all directories are up to date.
694  *
695  * Returns:
696  * 0 for success, error for failure.
697  */
698 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
699 			  struct amdgpu_vm *vm, bool immediate)
700 {
701 	struct amdgpu_vm_update_params params;
702 	struct amdgpu_vm_bo_base *entry;
703 	bool flush_tlb_needed = false;
704 	LIST_HEAD(relocated);
705 	int r, idx;
706 
707 	spin_lock(&vm->status_lock);
708 	list_splice_init(&vm->relocated, &relocated);
709 	spin_unlock(&vm->status_lock);
710 
711 	if (list_empty(&relocated))
712 		return 0;
713 
714 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
715 		return -ENODEV;
716 
717 	memset(&params, 0, sizeof(params));
718 	params.adev = adev;
719 	params.vm = vm;
720 	params.immediate = immediate;
721 
722 	r = vm->update_funcs->prepare(&params, NULL, AMDGPU_SYNC_EXPLICIT);
723 	if (r)
724 		goto error;
725 
726 	list_for_each_entry(entry, &relocated, vm_status) {
727 		/* vm_flush_needed after updating moved PDEs */
728 		flush_tlb_needed |= entry->moved;
729 
730 		r = amdgpu_vm_pde_update(&params, entry);
731 		if (r)
732 			goto error;
733 	}
734 
735 	r = vm->update_funcs->commit(&params, &vm->last_update);
736 	if (r)
737 		goto error;
738 
739 	if (flush_tlb_needed)
740 		atomic64_inc(&vm->tlb_seq);
741 
742 	while (!list_empty(&relocated)) {
743 		entry = list_first_entry(&relocated, struct amdgpu_vm_bo_base,
744 					 vm_status);
745 		amdgpu_vm_bo_idle(entry);
746 	}
747 
748 error:
749 	drm_dev_exit(idx);
750 	return r;
751 }
752 
753 /**
754  * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
755  * @fence: unused
756  * @cb: the callback structure
757  *
758  * Increments the tlb sequence to make sure that future CS execute a VM flush.
759  */
760 static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
761 				 struct dma_fence_cb *cb)
762 {
763 	struct amdgpu_vm_tlb_seq_cb *tlb_cb;
764 
765 	tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
766 	atomic64_inc(&tlb_cb->vm->tlb_seq);
767 	kfree(tlb_cb);
768 }
769 
770 /**
771  * amdgpu_vm_update_range - update a range in the vm page table
772  *
773  * @adev: amdgpu_device pointer to use for commands
774  * @vm: the VM to update the range
775  * @immediate: immediate submission in a page fault
776  * @unlocked: unlocked invalidation during MM callback
777  * @flush_tlb: trigger tlb invalidation after update completed
778  * @resv: fences we need to sync to
779  * @start: start of mapped range
780  * @last: last mapped entry
781  * @flags: flags for the entries
782  * @offset: offset into nodes and pages_addr
783  * @vram_base: base for vram mappings
784  * @res: ttm_resource to map
785  * @pages_addr: DMA addresses to use for mapping
786  * @fence: optional resulting fence
787  *
788  * Fill in the page table entries between @start and @last.
789  *
790  * Returns:
791  * 0 for success, negative erro code for failure.
792  */
793 int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
794 			   bool immediate, bool unlocked, bool flush_tlb,
795 			   struct dma_resv *resv, uint64_t start, uint64_t last,
796 			   uint64_t flags, uint64_t offset, uint64_t vram_base,
797 			   struct ttm_resource *res, dma_addr_t *pages_addr,
798 			   struct dma_fence **fence)
799 {
800 	struct amdgpu_vm_update_params params;
801 	struct amdgpu_vm_tlb_seq_cb *tlb_cb;
802 	struct amdgpu_res_cursor cursor;
803 	enum amdgpu_sync_mode sync_mode;
804 	int r, idx;
805 
806 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
807 		return -ENODEV;
808 
809 	tlb_cb = kmalloc(sizeof(*tlb_cb), GFP_KERNEL);
810 	if (!tlb_cb) {
811 		r = -ENOMEM;
812 		goto error_unlock;
813 	}
814 
815 	/* Vega20+XGMI where PTEs get inadvertently cached in L2 texture cache,
816 	 * heavy-weight flush TLB unconditionally.
817 	 */
818 	flush_tlb |= adev->gmc.xgmi.num_physical_nodes &&
819 		     adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0);
820 
821 	/*
822 	 * On GFX8 and older any 8 PTE block with a valid bit set enters the TLB
823 	 */
824 	flush_tlb |= adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 0, 0);
825 
826 	memset(&params, 0, sizeof(params));
827 	params.adev = adev;
828 	params.vm = vm;
829 	params.immediate = immediate;
830 	params.pages_addr = pages_addr;
831 	params.unlocked = unlocked;
832 
833 	/* Implicitly sync to command submissions in the same VM before
834 	 * unmapping. Sync to moving fences before mapping.
835 	 */
836 	if (!(flags & AMDGPU_PTE_VALID))
837 		sync_mode = AMDGPU_SYNC_EQ_OWNER;
838 	else
839 		sync_mode = AMDGPU_SYNC_EXPLICIT;
840 
841 	amdgpu_vm_eviction_lock(vm);
842 	if (vm->evicting) {
843 		r = -EBUSY;
844 		goto error_free;
845 	}
846 
847 	if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) {
848 		struct dma_fence *tmp = dma_fence_get_stub();
849 
850 		amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true);
851 		swap(vm->last_unlocked, tmp);
852 		dma_fence_put(tmp);
853 	}
854 
855 	r = vm->update_funcs->prepare(&params, resv, sync_mode);
856 	if (r)
857 		goto error_free;
858 
859 	amdgpu_res_first(pages_addr ? NULL : res, offset,
860 			 (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor);
861 	while (cursor.remaining) {
862 		uint64_t tmp, num_entries, addr;
863 
864 		num_entries = cursor.size >> AMDGPU_GPU_PAGE_SHIFT;
865 		if (pages_addr) {
866 			bool contiguous = true;
867 
868 			if (num_entries > AMDGPU_GPU_PAGES_IN_CPU_PAGE) {
869 				uint64_t pfn = cursor.start >> PAGE_SHIFT;
870 				uint64_t count;
871 
872 				contiguous = pages_addr[pfn + 1] ==
873 					pages_addr[pfn] + PAGE_SIZE;
874 
875 				tmp = num_entries /
876 					AMDGPU_GPU_PAGES_IN_CPU_PAGE;
877 				for (count = 2; count < tmp; ++count) {
878 					uint64_t idx = pfn + count;
879 
880 					if (contiguous != (pages_addr[idx] ==
881 					    pages_addr[idx - 1] + PAGE_SIZE))
882 						break;
883 				}
884 				num_entries = count *
885 					AMDGPU_GPU_PAGES_IN_CPU_PAGE;
886 			}
887 
888 			if (!contiguous) {
889 				addr = cursor.start;
890 				params.pages_addr = pages_addr;
891 			} else {
892 				addr = pages_addr[cursor.start >> PAGE_SHIFT];
893 				params.pages_addr = NULL;
894 			}
895 
896 		} else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT)) {
897 			addr = vram_base + cursor.start;
898 		} else {
899 			addr = 0;
900 		}
901 
902 		tmp = start + num_entries;
903 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
904 		if (r)
905 			goto error_free;
906 
907 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
908 		start = tmp;
909 	}
910 
911 	r = vm->update_funcs->commit(&params, fence);
912 
913 	if (flush_tlb || params.table_freed) {
914 		tlb_cb->vm = vm;
915 		if (fence && *fence &&
916 		    !dma_fence_add_callback(*fence, &tlb_cb->cb,
917 					   amdgpu_vm_tlb_seq_cb)) {
918 			dma_fence_put(vm->last_tlb_flush);
919 			vm->last_tlb_flush = dma_fence_get(*fence);
920 		} else {
921 			amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
922 		}
923 		tlb_cb = NULL;
924 	}
925 
926 error_free:
927 	kfree(tlb_cb);
928 
929 error_unlock:
930 	amdgpu_vm_eviction_unlock(vm);
931 	drm_dev_exit(idx);
932 	return r;
933 }
934 
935 void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem,
936 				uint64_t *gtt_mem, uint64_t *cpu_mem)
937 {
938 	struct amdgpu_bo_va *bo_va, *tmp;
939 
940 	spin_lock(&vm->status_lock);
941 	list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
942 		if (!bo_va->base.bo)
943 			continue;
944 		amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
945 				gtt_mem, cpu_mem);
946 	}
947 	list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
948 		if (!bo_va->base.bo)
949 			continue;
950 		amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
951 				gtt_mem, cpu_mem);
952 	}
953 	list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
954 		if (!bo_va->base.bo)
955 			continue;
956 		amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
957 				gtt_mem, cpu_mem);
958 	}
959 	list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
960 		if (!bo_va->base.bo)
961 			continue;
962 		amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
963 				gtt_mem, cpu_mem);
964 	}
965 	list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
966 		if (!bo_va->base.bo)
967 			continue;
968 		amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
969 				gtt_mem, cpu_mem);
970 	}
971 	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
972 		if (!bo_va->base.bo)
973 			continue;
974 		amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
975 				gtt_mem, cpu_mem);
976 	}
977 	spin_unlock(&vm->status_lock);
978 }
979 /**
980  * amdgpu_vm_bo_update - update all BO mappings in the vm page table
981  *
982  * @adev: amdgpu_device pointer
983  * @bo_va: requested BO and VM object
984  * @clear: if true clear the entries
985  *
986  * Fill in the page table entries for @bo_va.
987  *
988  * Returns:
989  * 0 for success, -EINVAL for failure.
990  */
991 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
992 			bool clear)
993 {
994 	struct amdgpu_bo *bo = bo_va->base.bo;
995 	struct amdgpu_vm *vm = bo_va->base.vm;
996 	struct amdgpu_bo_va_mapping *mapping;
997 	dma_addr_t *pages_addr = NULL;
998 	struct ttm_resource *mem;
999 	struct dma_fence **last_update;
1000 	bool flush_tlb = clear;
1001 	struct dma_resv *resv;
1002 	uint64_t vram_base;
1003 	uint64_t flags;
1004 	int r;
1005 
1006 	if (clear || !bo) {
1007 		mem = NULL;
1008 		resv = vm->root.bo->tbo.base.resv;
1009 	} else {
1010 		struct drm_gem_object *obj = &bo->tbo.base;
1011 
1012 		resv = bo->tbo.base.resv;
1013 		if (obj->import_attach && bo_va->is_xgmi) {
1014 			struct dma_buf *dma_buf = obj->import_attach->dmabuf;
1015 			struct drm_gem_object *gobj = dma_buf->priv;
1016 			struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
1017 
1018 			if (abo->tbo.resource->mem_type == TTM_PL_VRAM)
1019 				bo = gem_to_amdgpu_bo(gobj);
1020 		}
1021 		mem = bo->tbo.resource;
1022 		if (mem->mem_type == TTM_PL_TT ||
1023 		    mem->mem_type == AMDGPU_PL_PREEMPT)
1024 			pages_addr = bo->tbo.ttm->dma_address;
1025 	}
1026 
1027 	if (bo) {
1028 		struct amdgpu_device *bo_adev;
1029 
1030 		flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1031 
1032 		if (amdgpu_bo_encrypted(bo))
1033 			flags |= AMDGPU_PTE_TMZ;
1034 
1035 		bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
1036 		vram_base = bo_adev->vm_manager.vram_base_offset;
1037 	} else {
1038 		flags = 0x0;
1039 		vram_base = 0;
1040 	}
1041 
1042 	if (clear || (bo && bo->tbo.base.resv ==
1043 		      vm->root.bo->tbo.base.resv))
1044 		last_update = &vm->last_update;
1045 	else
1046 		last_update = &bo_va->last_pt_update;
1047 
1048 	if (!clear && bo_va->base.moved) {
1049 		flush_tlb = true;
1050 		list_splice_init(&bo_va->valids, &bo_va->invalids);
1051 
1052 	} else if (bo_va->cleared != clear) {
1053 		list_splice_init(&bo_va->valids, &bo_va->invalids);
1054 	}
1055 
1056 	list_for_each_entry(mapping, &bo_va->invalids, list) {
1057 		uint64_t update_flags = flags;
1058 
1059 		/* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1060 		 * but in case of something, we filter the flags in first place
1061 		 */
1062 		if (!(mapping->flags & AMDGPU_PTE_READABLE))
1063 			update_flags &= ~AMDGPU_PTE_READABLE;
1064 		if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1065 			update_flags &= ~AMDGPU_PTE_WRITEABLE;
1066 
1067 		/* Apply ASIC specific mapping flags */
1068 		amdgpu_gmc_get_vm_pte(adev, mapping, &update_flags);
1069 
1070 		trace_amdgpu_vm_bo_update(mapping);
1071 
1072 		r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb,
1073 					   resv, mapping->start, mapping->last,
1074 					   update_flags, mapping->offset,
1075 					   vram_base, mem, pages_addr,
1076 					   last_update);
1077 		if (r)
1078 			return r;
1079 	}
1080 
1081 	/* If the BO is not in its preferred location add it back to
1082 	 * the evicted list so that it gets validated again on the
1083 	 * next command submission.
1084 	 */
1085 	if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) {
1086 		uint32_t mem_type = bo->tbo.resource->mem_type;
1087 
1088 		if (!(bo->preferred_domains &
1089 		      amdgpu_mem_type_to_domain(mem_type)))
1090 			amdgpu_vm_bo_evicted(&bo_va->base);
1091 		else
1092 			amdgpu_vm_bo_idle(&bo_va->base);
1093 	} else {
1094 		amdgpu_vm_bo_done(&bo_va->base);
1095 	}
1096 
1097 	list_splice_init(&bo_va->invalids, &bo_va->valids);
1098 	bo_va->cleared = clear;
1099 	bo_va->base.moved = false;
1100 
1101 	if (trace_amdgpu_vm_bo_mapping_enabled()) {
1102 		list_for_each_entry(mapping, &bo_va->valids, list)
1103 			trace_amdgpu_vm_bo_mapping(mapping);
1104 	}
1105 
1106 	return 0;
1107 }
1108 
1109 /**
1110  * amdgpu_vm_update_prt_state - update the global PRT state
1111  *
1112  * @adev: amdgpu_device pointer
1113  */
1114 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1115 {
1116 	unsigned long flags;
1117 	bool enable;
1118 
1119 	spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1120 	enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1121 	adev->gmc.gmc_funcs->set_prt(adev, enable);
1122 	spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1123 }
1124 
1125 /**
1126  * amdgpu_vm_prt_get - add a PRT user
1127  *
1128  * @adev: amdgpu_device pointer
1129  */
1130 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1131 {
1132 	if (!adev->gmc.gmc_funcs->set_prt)
1133 		return;
1134 
1135 	if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1136 		amdgpu_vm_update_prt_state(adev);
1137 }
1138 
1139 /**
1140  * amdgpu_vm_prt_put - drop a PRT user
1141  *
1142  * @adev: amdgpu_device pointer
1143  */
1144 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1145 {
1146 	if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1147 		amdgpu_vm_update_prt_state(adev);
1148 }
1149 
1150 /**
1151  * amdgpu_vm_prt_cb - callback for updating the PRT status
1152  *
1153  * @fence: fence for the callback
1154  * @_cb: the callback function
1155  */
1156 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1157 {
1158 	struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1159 
1160 	amdgpu_vm_prt_put(cb->adev);
1161 	kfree(cb);
1162 }
1163 
1164 /**
1165  * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1166  *
1167  * @adev: amdgpu_device pointer
1168  * @fence: fence for the callback
1169  */
1170 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1171 				 struct dma_fence *fence)
1172 {
1173 	struct amdgpu_prt_cb *cb;
1174 
1175 	if (!adev->gmc.gmc_funcs->set_prt)
1176 		return;
1177 
1178 	cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
1179 	if (!cb) {
1180 		/* Last resort when we are OOM */
1181 		if (fence)
1182 			dma_fence_wait(fence, false);
1183 
1184 		amdgpu_vm_prt_put(adev);
1185 	} else {
1186 		cb->adev = adev;
1187 		if (!fence || dma_fence_add_callback(fence, &cb->cb,
1188 						     amdgpu_vm_prt_cb))
1189 			amdgpu_vm_prt_cb(fence, &cb->cb);
1190 	}
1191 }
1192 
1193 /**
1194  * amdgpu_vm_free_mapping - free a mapping
1195  *
1196  * @adev: amdgpu_device pointer
1197  * @vm: requested vm
1198  * @mapping: mapping to be freed
1199  * @fence: fence of the unmap operation
1200  *
1201  * Free a mapping and make sure we decrease the PRT usage count if applicable.
1202  */
1203 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1204 				   struct amdgpu_vm *vm,
1205 				   struct amdgpu_bo_va_mapping *mapping,
1206 				   struct dma_fence *fence)
1207 {
1208 	if (mapping->flags & AMDGPU_PTE_PRT)
1209 		amdgpu_vm_add_prt_cb(adev, fence);
1210 	kfree(mapping);
1211 }
1212 
1213 /**
1214  * amdgpu_vm_prt_fini - finish all prt mappings
1215  *
1216  * @adev: amdgpu_device pointer
1217  * @vm: requested vm
1218  *
1219  * Register a cleanup callback to disable PRT support after VM dies.
1220  */
1221 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1222 {
1223 	struct dma_resv *resv = vm->root.bo->tbo.base.resv;
1224 	struct dma_resv_iter cursor;
1225 	struct dma_fence *fence;
1226 
1227 	dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) {
1228 		/* Add a callback for each fence in the reservation object */
1229 		amdgpu_vm_prt_get(adev);
1230 		amdgpu_vm_add_prt_cb(adev, fence);
1231 	}
1232 }
1233 
1234 /**
1235  * amdgpu_vm_clear_freed - clear freed BOs in the PT
1236  *
1237  * @adev: amdgpu_device pointer
1238  * @vm: requested vm
1239  * @fence: optional resulting fence (unchanged if no work needed to be done
1240  * or if an error occurred)
1241  *
1242  * Make sure all freed BOs are cleared in the PT.
1243  * PTs have to be reserved and mutex must be locked!
1244  *
1245  * Returns:
1246  * 0 for success.
1247  *
1248  */
1249 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1250 			  struct amdgpu_vm *vm,
1251 			  struct dma_fence **fence)
1252 {
1253 	struct dma_resv *resv = vm->root.bo->tbo.base.resv;
1254 	struct amdgpu_bo_va_mapping *mapping;
1255 	uint64_t init_pte_value = 0;
1256 	struct dma_fence *f = NULL;
1257 	int r;
1258 
1259 	while (!list_empty(&vm->freed)) {
1260 		mapping = list_first_entry(&vm->freed,
1261 			struct amdgpu_bo_va_mapping, list);
1262 		list_del(&mapping->list);
1263 
1264 		if (vm->pte_support_ats &&
1265 		    mapping->start < AMDGPU_GMC_HOLE_START)
1266 			init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
1267 
1268 		r = amdgpu_vm_update_range(adev, vm, false, false, true, resv,
1269 					   mapping->start, mapping->last,
1270 					   init_pte_value, 0, 0, NULL, NULL,
1271 					   &f);
1272 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
1273 		if (r) {
1274 			dma_fence_put(f);
1275 			return r;
1276 		}
1277 	}
1278 
1279 	if (fence && f) {
1280 		dma_fence_put(*fence);
1281 		*fence = f;
1282 	} else {
1283 		dma_fence_put(f);
1284 	}
1285 
1286 	return 0;
1287 
1288 }
1289 
1290 /**
1291  * amdgpu_vm_handle_moved - handle moved BOs in the PT
1292  *
1293  * @adev: amdgpu_device pointer
1294  * @vm: requested vm
1295  *
1296  * Make sure all BOs which are moved are updated in the PTs.
1297  *
1298  * Returns:
1299  * 0 for success.
1300  *
1301  * PTs have to be reserved!
1302  */
1303 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
1304 			   struct amdgpu_vm *vm)
1305 {
1306 	struct amdgpu_bo_va *bo_va;
1307 	struct dma_resv *resv;
1308 	bool clear;
1309 	int r;
1310 
1311 	spin_lock(&vm->status_lock);
1312 	while (!list_empty(&vm->moved)) {
1313 		bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
1314 					 base.vm_status);
1315 		spin_unlock(&vm->status_lock);
1316 
1317 		/* Per VM BOs never need to bo cleared in the page tables */
1318 		r = amdgpu_vm_bo_update(adev, bo_va, false);
1319 		if (r)
1320 			return r;
1321 		spin_lock(&vm->status_lock);
1322 	}
1323 
1324 	while (!list_empty(&vm->invalidated)) {
1325 		bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
1326 					 base.vm_status);
1327 		resv = bo_va->base.bo->tbo.base.resv;
1328 		spin_unlock(&vm->status_lock);
1329 
1330 		/* Try to reserve the BO to avoid clearing its ptes */
1331 		if (!amdgpu_vm_debug && dma_resv_trylock(resv))
1332 			clear = false;
1333 		/* Somebody else is using the BO right now */
1334 		else
1335 			clear = true;
1336 
1337 		r = amdgpu_vm_bo_update(adev, bo_va, clear);
1338 		if (r)
1339 			return r;
1340 
1341 		if (!clear)
1342 			dma_resv_unlock(resv);
1343 		spin_lock(&vm->status_lock);
1344 	}
1345 	spin_unlock(&vm->status_lock);
1346 
1347 	return 0;
1348 }
1349 
1350 /**
1351  * amdgpu_vm_bo_add - add a bo to a specific vm
1352  *
1353  * @adev: amdgpu_device pointer
1354  * @vm: requested vm
1355  * @bo: amdgpu buffer object
1356  *
1357  * Add @bo into the requested vm.
1358  * Add @bo to the list of bos associated with the vm
1359  *
1360  * Returns:
1361  * Newly added bo_va or NULL for failure
1362  *
1363  * Object has to be reserved!
1364  */
1365 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1366 				      struct amdgpu_vm *vm,
1367 				      struct amdgpu_bo *bo)
1368 {
1369 	struct amdgpu_bo_va *bo_va;
1370 
1371 	bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1372 	if (bo_va == NULL) {
1373 		return NULL;
1374 	}
1375 	amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
1376 
1377 	bo_va->ref_count = 1;
1378 	INIT_LIST_HEAD(&bo_va->valids);
1379 	INIT_LIST_HEAD(&bo_va->invalids);
1380 
1381 	if (!bo)
1382 		return bo_va;
1383 
1384 	dma_resv_assert_held(bo->tbo.base.resv);
1385 	if (amdgpu_dmabuf_is_xgmi_accessible(adev, bo)) {
1386 		bo_va->is_xgmi = true;
1387 		/* Power up XGMI if it can be potentially used */
1388 		amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MAX_VEGA20);
1389 	}
1390 
1391 	return bo_va;
1392 }
1393 
1394 
1395 /**
1396  * amdgpu_vm_bo_insert_map - insert a new mapping
1397  *
1398  * @adev: amdgpu_device pointer
1399  * @bo_va: bo_va to store the address
1400  * @mapping: the mapping to insert
1401  *
1402  * Insert a new mapping into all structures.
1403  */
1404 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1405 				    struct amdgpu_bo_va *bo_va,
1406 				    struct amdgpu_bo_va_mapping *mapping)
1407 {
1408 	struct amdgpu_vm *vm = bo_va->base.vm;
1409 	struct amdgpu_bo *bo = bo_va->base.bo;
1410 
1411 	mapping->bo_va = bo_va;
1412 	list_add(&mapping->list, &bo_va->invalids);
1413 	amdgpu_vm_it_insert(mapping, &vm->va);
1414 
1415 	if (mapping->flags & AMDGPU_PTE_PRT)
1416 		amdgpu_vm_prt_get(adev);
1417 
1418 	if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
1419 	    !bo_va->base.moved) {
1420 		amdgpu_vm_bo_moved(&bo_va->base);
1421 	}
1422 	trace_amdgpu_vm_bo_map(bo_va, mapping);
1423 }
1424 
1425 /**
1426  * amdgpu_vm_bo_map - map bo inside a vm
1427  *
1428  * @adev: amdgpu_device pointer
1429  * @bo_va: bo_va to store the address
1430  * @saddr: where to map the BO
1431  * @offset: requested offset in the BO
1432  * @size: BO size in bytes
1433  * @flags: attributes of pages (read/write/valid/etc.)
1434  *
1435  * Add a mapping of the BO at the specefied addr into the VM.
1436  *
1437  * Returns:
1438  * 0 for success, error for failure.
1439  *
1440  * Object has to be reserved and unreserved outside!
1441  */
1442 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1443 		     struct amdgpu_bo_va *bo_va,
1444 		     uint64_t saddr, uint64_t offset,
1445 		     uint64_t size, uint64_t flags)
1446 {
1447 	struct amdgpu_bo_va_mapping *mapping, *tmp;
1448 	struct amdgpu_bo *bo = bo_va->base.bo;
1449 	struct amdgpu_vm *vm = bo_va->base.vm;
1450 	uint64_t eaddr;
1451 
1452 	/* validate the parameters */
1453 	if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
1454 	    size == 0 || size & ~PAGE_MASK)
1455 		return -EINVAL;
1456 
1457 	/* make sure object fit at this offset */
1458 	eaddr = saddr + size - 1;
1459 	if (saddr >= eaddr ||
1460 	    (bo && offset + size > amdgpu_bo_size(bo)) ||
1461 	    (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT))
1462 		return -EINVAL;
1463 
1464 	saddr /= AMDGPU_GPU_PAGE_SIZE;
1465 	eaddr /= AMDGPU_GPU_PAGE_SIZE;
1466 
1467 	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1468 	if (tmp) {
1469 		/* bo and tmp overlap, invalid addr */
1470 		dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1471 			"0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
1472 			tmp->start, tmp->last + 1);
1473 		return -EINVAL;
1474 	}
1475 
1476 	mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1477 	if (!mapping)
1478 		return -ENOMEM;
1479 
1480 	mapping->start = saddr;
1481 	mapping->last = eaddr;
1482 	mapping->offset = offset;
1483 	mapping->flags = flags;
1484 
1485 	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1486 
1487 	return 0;
1488 }
1489 
1490 /**
1491  * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1492  *
1493  * @adev: amdgpu_device pointer
1494  * @bo_va: bo_va to store the address
1495  * @saddr: where to map the BO
1496  * @offset: requested offset in the BO
1497  * @size: BO size in bytes
1498  * @flags: attributes of pages (read/write/valid/etc.)
1499  *
1500  * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1501  * mappings as we do so.
1502  *
1503  * Returns:
1504  * 0 for success, error for failure.
1505  *
1506  * Object has to be reserved and unreserved outside!
1507  */
1508 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1509 			     struct amdgpu_bo_va *bo_va,
1510 			     uint64_t saddr, uint64_t offset,
1511 			     uint64_t size, uint64_t flags)
1512 {
1513 	struct amdgpu_bo_va_mapping *mapping;
1514 	struct amdgpu_bo *bo = bo_va->base.bo;
1515 	uint64_t eaddr;
1516 	int r;
1517 
1518 	/* validate the parameters */
1519 	if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
1520 	    size == 0 || size & ~PAGE_MASK)
1521 		return -EINVAL;
1522 
1523 	/* make sure object fit at this offset */
1524 	eaddr = saddr + size - 1;
1525 	if (saddr >= eaddr ||
1526 	    (bo && offset + size > amdgpu_bo_size(bo)) ||
1527 	    (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT))
1528 		return -EINVAL;
1529 
1530 	/* Allocate all the needed memory */
1531 	mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1532 	if (!mapping)
1533 		return -ENOMEM;
1534 
1535 	r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
1536 	if (r) {
1537 		kfree(mapping);
1538 		return r;
1539 	}
1540 
1541 	saddr /= AMDGPU_GPU_PAGE_SIZE;
1542 	eaddr /= AMDGPU_GPU_PAGE_SIZE;
1543 
1544 	mapping->start = saddr;
1545 	mapping->last = eaddr;
1546 	mapping->offset = offset;
1547 	mapping->flags = flags;
1548 
1549 	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1550 
1551 	return 0;
1552 }
1553 
1554 /**
1555  * amdgpu_vm_bo_unmap - remove bo mapping from vm
1556  *
1557  * @adev: amdgpu_device pointer
1558  * @bo_va: bo_va to remove the address from
1559  * @saddr: where to the BO is mapped
1560  *
1561  * Remove a mapping of the BO at the specefied addr from the VM.
1562  *
1563  * Returns:
1564  * 0 for success, error for failure.
1565  *
1566  * Object has to be reserved and unreserved outside!
1567  */
1568 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1569 		       struct amdgpu_bo_va *bo_va,
1570 		       uint64_t saddr)
1571 {
1572 	struct amdgpu_bo_va_mapping *mapping;
1573 	struct amdgpu_vm *vm = bo_va->base.vm;
1574 	bool valid = true;
1575 
1576 	saddr /= AMDGPU_GPU_PAGE_SIZE;
1577 
1578 	list_for_each_entry(mapping, &bo_va->valids, list) {
1579 		if (mapping->start == saddr)
1580 			break;
1581 	}
1582 
1583 	if (&mapping->list == &bo_va->valids) {
1584 		valid = false;
1585 
1586 		list_for_each_entry(mapping, &bo_va->invalids, list) {
1587 			if (mapping->start == saddr)
1588 				break;
1589 		}
1590 
1591 		if (&mapping->list == &bo_va->invalids)
1592 			return -ENOENT;
1593 	}
1594 
1595 	list_del(&mapping->list);
1596 	amdgpu_vm_it_remove(mapping, &vm->va);
1597 	mapping->bo_va = NULL;
1598 	trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1599 
1600 	if (valid)
1601 		list_add(&mapping->list, &vm->freed);
1602 	else
1603 		amdgpu_vm_free_mapping(adev, vm, mapping,
1604 				       bo_va->last_pt_update);
1605 
1606 	return 0;
1607 }
1608 
1609 /**
1610  * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1611  *
1612  * @adev: amdgpu_device pointer
1613  * @vm: VM structure to use
1614  * @saddr: start of the range
1615  * @size: size of the range
1616  *
1617  * Remove all mappings in a range, split them as appropriate.
1618  *
1619  * Returns:
1620  * 0 for success, error for failure.
1621  */
1622 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1623 				struct amdgpu_vm *vm,
1624 				uint64_t saddr, uint64_t size)
1625 {
1626 	struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1627 	LIST_HEAD(removed);
1628 	uint64_t eaddr;
1629 
1630 	eaddr = saddr + size - 1;
1631 	saddr /= AMDGPU_GPU_PAGE_SIZE;
1632 	eaddr /= AMDGPU_GPU_PAGE_SIZE;
1633 
1634 	/* Allocate all the needed memory */
1635 	before = kzalloc(sizeof(*before), GFP_KERNEL);
1636 	if (!before)
1637 		return -ENOMEM;
1638 	INIT_LIST_HEAD(&before->list);
1639 
1640 	after = kzalloc(sizeof(*after), GFP_KERNEL);
1641 	if (!after) {
1642 		kfree(before);
1643 		return -ENOMEM;
1644 	}
1645 	INIT_LIST_HEAD(&after->list);
1646 
1647 	/* Now gather all removed mappings */
1648 	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1649 	while (tmp) {
1650 		/* Remember mapping split at the start */
1651 		if (tmp->start < saddr) {
1652 			before->start = tmp->start;
1653 			before->last = saddr - 1;
1654 			before->offset = tmp->offset;
1655 			before->flags = tmp->flags;
1656 			before->bo_va = tmp->bo_va;
1657 			list_add(&before->list, &tmp->bo_va->invalids);
1658 		}
1659 
1660 		/* Remember mapping split at the end */
1661 		if (tmp->last > eaddr) {
1662 			after->start = eaddr + 1;
1663 			after->last = tmp->last;
1664 			after->offset = tmp->offset;
1665 			after->offset += (after->start - tmp->start) << PAGE_SHIFT;
1666 			after->flags = tmp->flags;
1667 			after->bo_va = tmp->bo_va;
1668 			list_add(&after->list, &tmp->bo_va->invalids);
1669 		}
1670 
1671 		list_del(&tmp->list);
1672 		list_add(&tmp->list, &removed);
1673 
1674 		tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
1675 	}
1676 
1677 	/* And free them up */
1678 	list_for_each_entry_safe(tmp, next, &removed, list) {
1679 		amdgpu_vm_it_remove(tmp, &vm->va);
1680 		list_del(&tmp->list);
1681 
1682 		if (tmp->start < saddr)
1683 		    tmp->start = saddr;
1684 		if (tmp->last > eaddr)
1685 		    tmp->last = eaddr;
1686 
1687 		tmp->bo_va = NULL;
1688 		list_add(&tmp->list, &vm->freed);
1689 		trace_amdgpu_vm_bo_unmap(NULL, tmp);
1690 	}
1691 
1692 	/* Insert partial mapping before the range */
1693 	if (!list_empty(&before->list)) {
1694 		amdgpu_vm_it_insert(before, &vm->va);
1695 		if (before->flags & AMDGPU_PTE_PRT)
1696 			amdgpu_vm_prt_get(adev);
1697 	} else {
1698 		kfree(before);
1699 	}
1700 
1701 	/* Insert partial mapping after the range */
1702 	if (!list_empty(&after->list)) {
1703 		amdgpu_vm_it_insert(after, &vm->va);
1704 		if (after->flags & AMDGPU_PTE_PRT)
1705 			amdgpu_vm_prt_get(adev);
1706 	} else {
1707 		kfree(after);
1708 	}
1709 
1710 	return 0;
1711 }
1712 
1713 /**
1714  * amdgpu_vm_bo_lookup_mapping - find mapping by address
1715  *
1716  * @vm: the requested VM
1717  * @addr: the address
1718  *
1719  * Find a mapping by it's address.
1720  *
1721  * Returns:
1722  * The amdgpu_bo_va_mapping matching for addr or NULL
1723  *
1724  */
1725 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
1726 							 uint64_t addr)
1727 {
1728 	return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
1729 }
1730 
1731 /**
1732  * amdgpu_vm_bo_trace_cs - trace all reserved mappings
1733  *
1734  * @vm: the requested vm
1735  * @ticket: CS ticket
1736  *
1737  * Trace all mappings of BOs reserved during a command submission.
1738  */
1739 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket)
1740 {
1741 	struct amdgpu_bo_va_mapping *mapping;
1742 
1743 	if (!trace_amdgpu_vm_bo_cs_enabled())
1744 		return;
1745 
1746 	for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping;
1747 	     mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) {
1748 		if (mapping->bo_va && mapping->bo_va->base.bo) {
1749 			struct amdgpu_bo *bo;
1750 
1751 			bo = mapping->bo_va->base.bo;
1752 			if (dma_resv_locking_ctx(bo->tbo.base.resv) !=
1753 			    ticket)
1754 				continue;
1755 		}
1756 
1757 		trace_amdgpu_vm_bo_cs(mapping);
1758 	}
1759 }
1760 
1761 /**
1762  * amdgpu_vm_bo_del - remove a bo from a specific vm
1763  *
1764  * @adev: amdgpu_device pointer
1765  * @bo_va: requested bo_va
1766  *
1767  * Remove @bo_va->bo from the requested vm.
1768  *
1769  * Object have to be reserved!
1770  */
1771 void amdgpu_vm_bo_del(struct amdgpu_device *adev,
1772 		      struct amdgpu_bo_va *bo_va)
1773 {
1774 	struct amdgpu_bo_va_mapping *mapping, *next;
1775 	struct amdgpu_bo *bo = bo_va->base.bo;
1776 	struct amdgpu_vm *vm = bo_va->base.vm;
1777 	struct amdgpu_vm_bo_base **base;
1778 
1779 	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
1780 
1781 	if (bo) {
1782 		dma_resv_assert_held(bo->tbo.base.resv);
1783 		if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv)
1784 			ttm_bo_set_bulk_move(&bo->tbo, NULL);
1785 
1786 		for (base = &bo_va->base.bo->vm_bo; *base;
1787 		     base = &(*base)->next) {
1788 			if (*base != &bo_va->base)
1789 				continue;
1790 
1791 			*base = bo_va->base.next;
1792 			break;
1793 		}
1794 	}
1795 
1796 	spin_lock(&vm->status_lock);
1797 	list_del(&bo_va->base.vm_status);
1798 	spin_unlock(&vm->status_lock);
1799 
1800 	list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
1801 		list_del(&mapping->list);
1802 		amdgpu_vm_it_remove(mapping, &vm->va);
1803 		mapping->bo_va = NULL;
1804 		trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1805 		list_add(&mapping->list, &vm->freed);
1806 	}
1807 	list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1808 		list_del(&mapping->list);
1809 		amdgpu_vm_it_remove(mapping, &vm->va);
1810 		amdgpu_vm_free_mapping(adev, vm, mapping,
1811 				       bo_va->last_pt_update);
1812 	}
1813 
1814 	dma_fence_put(bo_va->last_pt_update);
1815 
1816 	if (bo && bo_va->is_xgmi)
1817 		amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN);
1818 
1819 	kfree(bo_va);
1820 }
1821 
1822 /**
1823  * amdgpu_vm_evictable - check if we can evict a VM
1824  *
1825  * @bo: A page table of the VM.
1826  *
1827  * Check if it is possible to evict a VM.
1828  */
1829 bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
1830 {
1831 	struct amdgpu_vm_bo_base *bo_base = bo->vm_bo;
1832 
1833 	/* Page tables of a destroyed VM can go away immediately */
1834 	if (!bo_base || !bo_base->vm)
1835 		return true;
1836 
1837 	/* Don't evict VM page tables while they are busy */
1838 	if (!dma_resv_test_signaled(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP))
1839 		return false;
1840 
1841 	/* Try to block ongoing updates */
1842 	if (!amdgpu_vm_eviction_trylock(bo_base->vm))
1843 		return false;
1844 
1845 	/* Don't evict VM page tables while they are updated */
1846 	if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) {
1847 		amdgpu_vm_eviction_unlock(bo_base->vm);
1848 		return false;
1849 	}
1850 
1851 	bo_base->vm->evicting = true;
1852 	amdgpu_vm_eviction_unlock(bo_base->vm);
1853 	return true;
1854 }
1855 
1856 /**
1857  * amdgpu_vm_bo_invalidate - mark the bo as invalid
1858  *
1859  * @adev: amdgpu_device pointer
1860  * @bo: amdgpu buffer object
1861  * @evicted: is the BO evicted
1862  *
1863  * Mark @bo as invalid.
1864  */
1865 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1866 			     struct amdgpu_bo *bo, bool evicted)
1867 {
1868 	struct amdgpu_vm_bo_base *bo_base;
1869 
1870 	/* shadow bo doesn't have bo base, its validation needs its parent */
1871 	if (bo->parent && (amdgpu_bo_shadowed(bo->parent) == bo))
1872 		bo = bo->parent;
1873 
1874 	for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
1875 		struct amdgpu_vm *vm = bo_base->vm;
1876 
1877 		if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) {
1878 			amdgpu_vm_bo_evicted(bo_base);
1879 			continue;
1880 		}
1881 
1882 		if (bo_base->moved)
1883 			continue;
1884 		bo_base->moved = true;
1885 
1886 		if (bo->tbo.type == ttm_bo_type_kernel)
1887 			amdgpu_vm_bo_relocated(bo_base);
1888 		else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv)
1889 			amdgpu_vm_bo_moved(bo_base);
1890 		else
1891 			amdgpu_vm_bo_invalidated(bo_base);
1892 	}
1893 }
1894 
1895 /**
1896  * amdgpu_vm_get_block_size - calculate VM page table size as power of two
1897  *
1898  * @vm_size: VM size
1899  *
1900  * Returns:
1901  * VM page table as power of two
1902  */
1903 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
1904 {
1905 	/* Total bits covered by PD + PTs */
1906 	unsigned bits = ilog2(vm_size) + 18;
1907 
1908 	/* Make sure the PD is 4K in size up to 8GB address space.
1909 	   Above that split equal between PD and PTs */
1910 	if (vm_size <= 8)
1911 		return (bits - 9);
1912 	else
1913 		return ((bits + 3) / 2);
1914 }
1915 
1916 /**
1917  * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
1918  *
1919  * @adev: amdgpu_device pointer
1920  * @min_vm_size: the minimum vm size in GB if it's set auto
1921  * @fragment_size_default: Default PTE fragment size
1922  * @max_level: max VMPT level
1923  * @max_bits: max address space size in bits
1924  *
1925  */
1926 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
1927 			   uint32_t fragment_size_default, unsigned max_level,
1928 			   unsigned max_bits)
1929 {
1930 	unsigned int max_size = 1 << (max_bits - 30);
1931 	unsigned int vm_size;
1932 	uint64_t tmp;
1933 
1934 	/* adjust vm size first */
1935 	if (amdgpu_vm_size != -1) {
1936 		vm_size = amdgpu_vm_size;
1937 		if (vm_size > max_size) {
1938 			dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
1939 				 amdgpu_vm_size, max_size);
1940 			vm_size = max_size;
1941 		}
1942 	} else {
1943 		struct sysinfo si;
1944 		unsigned int phys_ram_gb;
1945 
1946 		/* Optimal VM size depends on the amount of physical
1947 		 * RAM available. Underlying requirements and
1948 		 * assumptions:
1949 		 *
1950 		 *  - Need to map system memory and VRAM from all GPUs
1951 		 *     - VRAM from other GPUs not known here
1952 		 *     - Assume VRAM <= system memory
1953 		 *  - On GFX8 and older, VM space can be segmented for
1954 		 *    different MTYPEs
1955 		 *  - Need to allow room for fragmentation, guard pages etc.
1956 		 *
1957 		 * This adds up to a rough guess of system memory x3.
1958 		 * Round up to power of two to maximize the available
1959 		 * VM size with the given page table size.
1960 		 */
1961 		si_meminfo(&si);
1962 		phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit +
1963 			       (1 << 30) - 1) >> 30;
1964 		vm_size = roundup_pow_of_two(
1965 			min(max(phys_ram_gb * 3, min_vm_size), max_size));
1966 	}
1967 
1968 	adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
1969 
1970 	tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
1971 	if (amdgpu_vm_block_size != -1)
1972 		tmp >>= amdgpu_vm_block_size - 9;
1973 	tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
1974 	adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
1975 	switch (adev->vm_manager.num_level) {
1976 	case 3:
1977 		adev->vm_manager.root_level = AMDGPU_VM_PDB2;
1978 		break;
1979 	case 2:
1980 		adev->vm_manager.root_level = AMDGPU_VM_PDB1;
1981 		break;
1982 	case 1:
1983 		adev->vm_manager.root_level = AMDGPU_VM_PDB0;
1984 		break;
1985 	default:
1986 		dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
1987 	}
1988 	/* block size depends on vm size and hw setup*/
1989 	if (amdgpu_vm_block_size != -1)
1990 		adev->vm_manager.block_size =
1991 			min((unsigned)amdgpu_vm_block_size, max_bits
1992 			    - AMDGPU_GPU_PAGE_SHIFT
1993 			    - 9 * adev->vm_manager.num_level);
1994 	else if (adev->vm_manager.num_level > 1)
1995 		adev->vm_manager.block_size = 9;
1996 	else
1997 		adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
1998 
1999 	if (amdgpu_vm_fragment_size == -1)
2000 		adev->vm_manager.fragment_size = fragment_size_default;
2001 	else
2002 		adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2003 
2004 	DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2005 		 vm_size, adev->vm_manager.num_level + 1,
2006 		 adev->vm_manager.block_size,
2007 		 adev->vm_manager.fragment_size);
2008 }
2009 
2010 /**
2011  * amdgpu_vm_wait_idle - wait for the VM to become idle
2012  *
2013  * @vm: VM object to wait for
2014  * @timeout: timeout to wait for VM to become idle
2015  */
2016 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout)
2017 {
2018 	timeout = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv,
2019 					DMA_RESV_USAGE_BOOKKEEP,
2020 					true, timeout);
2021 	if (timeout <= 0)
2022 		return timeout;
2023 
2024 	return dma_fence_wait_timeout(vm->last_unlocked, true, timeout);
2025 }
2026 
2027 /**
2028  * amdgpu_vm_init - initialize a vm instance
2029  *
2030  * @adev: amdgpu_device pointer
2031  * @vm: requested vm
2032  *
2033  * Init @vm fields.
2034  *
2035  * Returns:
2036  * 0 for success, error for failure.
2037  */
2038 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2039 {
2040 	struct amdgpu_bo *root_bo;
2041 	struct amdgpu_bo_vm *root;
2042 	int r, i;
2043 
2044 	vm->va = RB_ROOT_CACHED;
2045 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2046 		vm->reserved_vmid[i] = NULL;
2047 	INIT_LIST_HEAD(&vm->evicted);
2048 	INIT_LIST_HEAD(&vm->relocated);
2049 	INIT_LIST_HEAD(&vm->moved);
2050 	INIT_LIST_HEAD(&vm->idle);
2051 	INIT_LIST_HEAD(&vm->invalidated);
2052 	spin_lock_init(&vm->status_lock);
2053 	INIT_LIST_HEAD(&vm->freed);
2054 	INIT_LIST_HEAD(&vm->done);
2055 	INIT_LIST_HEAD(&vm->pt_freed);
2056 	INIT_WORK(&vm->pt_free_work, amdgpu_vm_pt_free_work);
2057 
2058 	/* create scheduler entities for page table updates */
2059 	r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL,
2060 				  adev->vm_manager.vm_pte_scheds,
2061 				  adev->vm_manager.vm_pte_num_scheds, NULL);
2062 	if (r)
2063 		return r;
2064 
2065 	r = drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL,
2066 				  adev->vm_manager.vm_pte_scheds,
2067 				  adev->vm_manager.vm_pte_num_scheds, NULL);
2068 	if (r)
2069 		goto error_free_immediate;
2070 
2071 	vm->pte_support_ats = false;
2072 	vm->is_compute_context = false;
2073 
2074 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2075 				    AMDGPU_VM_USE_CPU_FOR_GFX);
2076 
2077 	DRM_DEBUG_DRIVER("VM update mode is %s\n",
2078 			 vm->use_cpu_for_update ? "CPU" : "SDMA");
2079 	WARN_ONCE((vm->use_cpu_for_update &&
2080 		   !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2081 		  "CPU update of VM recommended only for large BAR system\n");
2082 
2083 	if (vm->use_cpu_for_update)
2084 		vm->update_funcs = &amdgpu_vm_cpu_funcs;
2085 	else
2086 		vm->update_funcs = &amdgpu_vm_sdma_funcs;
2087 	vm->last_update = NULL;
2088 	vm->last_unlocked = dma_fence_get_stub();
2089 	vm->last_tlb_flush = dma_fence_get_stub();
2090 
2091 	mutex_init(&vm->eviction_lock);
2092 	vm->evicting = false;
2093 
2094 	r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level,
2095 				false, &root);
2096 	if (r)
2097 		goto error_free_delayed;
2098 	root_bo = &root->bo;
2099 	r = amdgpu_bo_reserve(root_bo, true);
2100 	if (r)
2101 		goto error_free_root;
2102 
2103 	r = dma_resv_reserve_fences(root_bo->tbo.base.resv, 1);
2104 	if (r)
2105 		goto error_unreserve;
2106 
2107 	amdgpu_vm_bo_base_init(&vm->root, vm, root_bo);
2108 
2109 	r = amdgpu_vm_pt_clear(adev, vm, root, false);
2110 	if (r)
2111 		goto error_unreserve;
2112 
2113 	amdgpu_bo_unreserve(vm->root.bo);
2114 
2115 	INIT_KFIFO(vm->faults);
2116 
2117 	return 0;
2118 
2119 error_unreserve:
2120 	amdgpu_bo_unreserve(vm->root.bo);
2121 
2122 error_free_root:
2123 	amdgpu_bo_unref(&root->shadow);
2124 	amdgpu_bo_unref(&root_bo);
2125 	vm->root.bo = NULL;
2126 
2127 error_free_delayed:
2128 	dma_fence_put(vm->last_tlb_flush);
2129 	dma_fence_put(vm->last_unlocked);
2130 	drm_sched_entity_destroy(&vm->delayed);
2131 
2132 error_free_immediate:
2133 	drm_sched_entity_destroy(&vm->immediate);
2134 
2135 	return r;
2136 }
2137 
2138 /**
2139  * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2140  *
2141  * @adev: amdgpu_device pointer
2142  * @vm: requested vm
2143  *
2144  * This only works on GFX VMs that don't have any BOs added and no
2145  * page tables allocated yet.
2146  *
2147  * Changes the following VM parameters:
2148  * - use_cpu_for_update
2149  * - pte_supports_ats
2150  *
2151  * Reinitializes the page directory to reflect the changed ATS
2152  * setting.
2153  *
2154  * Returns:
2155  * 0 for success, -errno for errors.
2156  */
2157 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2158 {
2159 	bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2160 	int r;
2161 
2162 	r = amdgpu_bo_reserve(vm->root.bo, true);
2163 	if (r)
2164 		return r;
2165 
2166 	/* Sanity checks */
2167 	if (!amdgpu_vm_pt_is_root_clean(adev, vm)) {
2168 		r = -EINVAL;
2169 		goto unreserve_bo;
2170 	}
2171 
2172 	/* Check if PD needs to be reinitialized and do it before
2173 	 * changing any other state, in case it fails.
2174 	 */
2175 	if (pte_support_ats != vm->pte_support_ats) {
2176 		vm->pte_support_ats = pte_support_ats;
2177 		r = amdgpu_vm_pt_clear(adev, vm, to_amdgpu_bo_vm(vm->root.bo),
2178 				       false);
2179 		if (r)
2180 			goto unreserve_bo;
2181 	}
2182 
2183 	/* Update VM state */
2184 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2185 				    AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2186 	DRM_DEBUG_DRIVER("VM update mode is %s\n",
2187 			 vm->use_cpu_for_update ? "CPU" : "SDMA");
2188 	WARN_ONCE((vm->use_cpu_for_update &&
2189 		   !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2190 		  "CPU update of VM recommended only for large BAR system\n");
2191 
2192 	if (vm->use_cpu_for_update) {
2193 		/* Sync with last SDMA update/clear before switching to CPU */
2194 		r = amdgpu_bo_sync_wait(vm->root.bo,
2195 					AMDGPU_FENCE_OWNER_UNDEFINED, true);
2196 		if (r)
2197 			goto unreserve_bo;
2198 
2199 		vm->update_funcs = &amdgpu_vm_cpu_funcs;
2200 	} else {
2201 		vm->update_funcs = &amdgpu_vm_sdma_funcs;
2202 	}
2203 	/*
2204 	 * Make sure root PD gets mapped. As vm_update_mode could be changed
2205 	 * when turning a GFX VM into a compute VM.
2206 	 */
2207 	r = vm->update_funcs->map_table(to_amdgpu_bo_vm(vm->root.bo));
2208 	if (r)
2209 		goto unreserve_bo;
2210 
2211 	dma_fence_put(vm->last_update);
2212 	vm->last_update = NULL;
2213 	vm->is_compute_context = true;
2214 
2215 	/* Free the shadow bo for compute VM */
2216 	amdgpu_bo_unref(&to_amdgpu_bo_vm(vm->root.bo)->shadow);
2217 
2218 	goto unreserve_bo;
2219 
2220 unreserve_bo:
2221 	amdgpu_bo_unreserve(vm->root.bo);
2222 	return r;
2223 }
2224 
2225 /**
2226  * amdgpu_vm_release_compute - release a compute vm
2227  * @adev: amdgpu_device pointer
2228  * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute
2229  *
2230  * This is a correspondant of amdgpu_vm_make_compute. It decouples compute
2231  * pasid from vm. Compute should stop use of vm after this call.
2232  */
2233 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2234 {
2235 	amdgpu_vm_set_pasid(adev, vm, 0);
2236 	vm->is_compute_context = false;
2237 }
2238 
2239 /**
2240  * amdgpu_vm_fini - tear down a vm instance
2241  *
2242  * @adev: amdgpu_device pointer
2243  * @vm: requested vm
2244  *
2245  * Tear down @vm.
2246  * Unbind the VM and remove all bos from the vm bo list
2247  */
2248 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2249 {
2250 	struct amdgpu_bo_va_mapping *mapping, *tmp;
2251 	bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2252 	struct amdgpu_bo *root;
2253 	unsigned long flags;
2254 	int i;
2255 
2256 	amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2257 
2258 	flush_work(&vm->pt_free_work);
2259 
2260 	root = amdgpu_bo_ref(vm->root.bo);
2261 	amdgpu_bo_reserve(root, true);
2262 	amdgpu_vm_set_pasid(adev, vm, 0);
2263 	dma_fence_wait(vm->last_unlocked, false);
2264 	dma_fence_put(vm->last_unlocked);
2265 	dma_fence_wait(vm->last_tlb_flush, false);
2266 	/* Make sure that all fence callbacks have completed */
2267 	spin_lock_irqsave(vm->last_tlb_flush->lock, flags);
2268 	spin_unlock_irqrestore(vm->last_tlb_flush->lock, flags);
2269 	dma_fence_put(vm->last_tlb_flush);
2270 
2271 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2272 		if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2273 			amdgpu_vm_prt_fini(adev, vm);
2274 			prt_fini_needed = false;
2275 		}
2276 
2277 		list_del(&mapping->list);
2278 		amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2279 	}
2280 
2281 	amdgpu_vm_pt_free_root(adev, vm);
2282 	amdgpu_bo_unreserve(root);
2283 	amdgpu_bo_unref(&root);
2284 	WARN_ON(vm->root.bo);
2285 
2286 	drm_sched_entity_destroy(&vm->immediate);
2287 	drm_sched_entity_destroy(&vm->delayed);
2288 
2289 	if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
2290 		dev_err(adev->dev, "still active bo inside vm\n");
2291 	}
2292 	rbtree_postorder_for_each_entry_safe(mapping, tmp,
2293 					     &vm->va.rb_root, rb) {
2294 		/* Don't remove the mapping here, we don't want to trigger a
2295 		 * rebalance and the tree is about to be destroyed anyway.
2296 		 */
2297 		list_del(&mapping->list);
2298 		kfree(mapping);
2299 	}
2300 
2301 	dma_fence_put(vm->last_update);
2302 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2303 		amdgpu_vmid_free_reserved(adev, vm, i);
2304 }
2305 
2306 /**
2307  * amdgpu_vm_manager_init - init the VM manager
2308  *
2309  * @adev: amdgpu_device pointer
2310  *
2311  * Initialize the VM manager structures
2312  */
2313 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2314 {
2315 	unsigned i;
2316 
2317 	/* Concurrent flushes are only possible starting with Vega10 and
2318 	 * are broken on Navi10 and Navi14.
2319 	 */
2320 	adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 ||
2321 					      adev->asic_type == CHIP_NAVI10 ||
2322 					      adev->asic_type == CHIP_NAVI14);
2323 	amdgpu_vmid_mgr_init(adev);
2324 
2325 	adev->vm_manager.fence_context =
2326 		dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2327 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2328 		adev->vm_manager.seqno[i] = 0;
2329 
2330 	spin_lock_init(&adev->vm_manager.prt_lock);
2331 	atomic_set(&adev->vm_manager.num_prt_users, 0);
2332 
2333 	/* If not overridden by the user, by default, only in large BAR systems
2334 	 * Compute VM tables will be updated by CPU
2335 	 */
2336 #ifdef CONFIG_X86_64
2337 	if (amdgpu_vm_update_mode == -1) {
2338 		/* For asic with VF MMIO access protection
2339 		 * avoid using CPU for VM table updates
2340 		 */
2341 		if (amdgpu_gmc_vram_full_visible(&adev->gmc) &&
2342 		    !amdgpu_sriov_vf_mmio_access_protection(adev))
2343 			adev->vm_manager.vm_update_mode =
2344 				AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2345 		else
2346 			adev->vm_manager.vm_update_mode = 0;
2347 	} else
2348 		adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2349 #else
2350 	adev->vm_manager.vm_update_mode = 0;
2351 #endif
2352 
2353 	xa_init_flags(&adev->vm_manager.pasids, XA_FLAGS_LOCK_IRQ);
2354 }
2355 
2356 /**
2357  * amdgpu_vm_manager_fini - cleanup VM manager
2358  *
2359  * @adev: amdgpu_device pointer
2360  *
2361  * Cleanup the VM manager and free resources.
2362  */
2363 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2364 {
2365 	WARN_ON(!xa_empty(&adev->vm_manager.pasids));
2366 	xa_destroy(&adev->vm_manager.pasids);
2367 
2368 	amdgpu_vmid_mgr_fini(adev);
2369 }
2370 
2371 /**
2372  * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
2373  *
2374  * @dev: drm device pointer
2375  * @data: drm_amdgpu_vm
2376  * @filp: drm file pointer
2377  *
2378  * Returns:
2379  * 0 for success, -errno for errors.
2380  */
2381 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2382 {
2383 	union drm_amdgpu_vm *args = data;
2384 	struct amdgpu_device *adev = drm_to_adev(dev);
2385 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
2386 	long timeout = msecs_to_jiffies(2000);
2387 	int r;
2388 
2389 	switch (args->in.op) {
2390 	case AMDGPU_VM_OP_RESERVE_VMID:
2391 		/* We only have requirement to reserve vmid from gfxhub */
2392 		r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm,
2393 					       AMDGPU_GFXHUB_0);
2394 		if (r)
2395 			return r;
2396 		break;
2397 	case AMDGPU_VM_OP_UNRESERVE_VMID:
2398 		if (amdgpu_sriov_runtime(adev))
2399 			timeout = 8 * timeout;
2400 
2401 		/* Wait vm idle to make sure the vmid set in SPM_VMID is
2402 		 * not referenced anymore.
2403 		 */
2404 		r = amdgpu_bo_reserve(fpriv->vm.root.bo, true);
2405 		if (r)
2406 			return r;
2407 
2408 		r = amdgpu_vm_wait_idle(&fpriv->vm, timeout);
2409 		if (r < 0)
2410 			return r;
2411 
2412 		amdgpu_bo_unreserve(fpriv->vm.root.bo);
2413 		amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0);
2414 		break;
2415 	default:
2416 		return -EINVAL;
2417 	}
2418 
2419 	return 0;
2420 }
2421 
2422 /**
2423  * amdgpu_vm_get_task_info - Extracts task info for a PASID.
2424  *
2425  * @adev: drm device pointer
2426  * @pasid: PASID identifier for VM
2427  * @task_info: task_info to fill.
2428  */
2429 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, u32 pasid,
2430 			 struct amdgpu_task_info *task_info)
2431 {
2432 	struct amdgpu_vm *vm;
2433 	unsigned long flags;
2434 
2435 	xa_lock_irqsave(&adev->vm_manager.pasids, flags);
2436 
2437 	vm = xa_load(&adev->vm_manager.pasids, pasid);
2438 	if (vm)
2439 		*task_info = vm->task_info;
2440 
2441 	xa_unlock_irqrestore(&adev->vm_manager.pasids, flags);
2442 }
2443 
2444 /**
2445  * amdgpu_vm_set_task_info - Sets VMs task info.
2446  *
2447  * @vm: vm for which to set the info
2448  */
2449 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
2450 {
2451 	if (vm->task_info.pid)
2452 		return;
2453 
2454 	vm->task_info.pid = current->pid;
2455 	get_task_comm(vm->task_info.task_name, current);
2456 
2457 	if (current->group_leader->mm != current->mm)
2458 		return;
2459 
2460 	vm->task_info.tgid = current->group_leader->pid;
2461 	get_task_comm(vm->task_info.process_name, current->group_leader);
2462 }
2463 
2464 /**
2465  * amdgpu_vm_handle_fault - graceful handling of VM faults.
2466  * @adev: amdgpu device pointer
2467  * @pasid: PASID of the VM
2468  * @addr: Address of the fault
2469  * @write_fault: true is write fault, false is read fault
2470  *
2471  * Try to gracefully handle a VM fault. Return true if the fault was handled and
2472  * shouldn't be reported any more.
2473  */
2474 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
2475 			    uint64_t addr, bool write_fault)
2476 {
2477 	bool is_compute_context = false;
2478 	struct amdgpu_bo *root;
2479 	unsigned long irqflags;
2480 	uint64_t value, flags;
2481 	struct amdgpu_vm *vm;
2482 	int r;
2483 
2484 	xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2485 	vm = xa_load(&adev->vm_manager.pasids, pasid);
2486 	if (vm) {
2487 		root = amdgpu_bo_ref(vm->root.bo);
2488 		is_compute_context = vm->is_compute_context;
2489 	} else {
2490 		root = NULL;
2491 	}
2492 	xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2493 
2494 	if (!root)
2495 		return false;
2496 
2497 	addr /= AMDGPU_GPU_PAGE_SIZE;
2498 
2499 	if (is_compute_context &&
2500 	    !svm_range_restore_pages(adev, pasid, addr, write_fault)) {
2501 		amdgpu_bo_unref(&root);
2502 		return true;
2503 	}
2504 
2505 	r = amdgpu_bo_reserve(root, true);
2506 	if (r)
2507 		goto error_unref;
2508 
2509 	/* Double check that the VM still exists */
2510 	xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2511 	vm = xa_load(&adev->vm_manager.pasids, pasid);
2512 	if (vm && vm->root.bo != root)
2513 		vm = NULL;
2514 	xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2515 	if (!vm)
2516 		goto error_unlock;
2517 
2518 	flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED |
2519 		AMDGPU_PTE_SYSTEM;
2520 
2521 	if (is_compute_context) {
2522 		/* Intentionally setting invalid PTE flag
2523 		 * combination to force a no-retry-fault
2524 		 */
2525 		flags = AMDGPU_PTE_SNOOPED | AMDGPU_PTE_PRT;
2526 		value = 0;
2527 	} else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) {
2528 		/* Redirect the access to the dummy page */
2529 		value = adev->dummy_page_addr;
2530 		flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE |
2531 			AMDGPU_PTE_WRITEABLE;
2532 
2533 	} else {
2534 		/* Let the hw retry silently on the PTE */
2535 		value = 0;
2536 	}
2537 
2538 	r = dma_resv_reserve_fences(root->tbo.base.resv, 1);
2539 	if (r) {
2540 		pr_debug("failed %d to reserve fence slot\n", r);
2541 		goto error_unlock;
2542 	}
2543 
2544 	r = amdgpu_vm_update_range(adev, vm, true, false, false, NULL, addr,
2545 				   addr, flags, value, 0, NULL, NULL, NULL);
2546 	if (r)
2547 		goto error_unlock;
2548 
2549 	r = amdgpu_vm_update_pdes(adev, vm, true);
2550 
2551 error_unlock:
2552 	amdgpu_bo_unreserve(root);
2553 	if (r < 0)
2554 		DRM_ERROR("Can't handle page fault (%d)\n", r);
2555 
2556 error_unref:
2557 	amdgpu_bo_unref(&root);
2558 
2559 	return false;
2560 }
2561 
2562 #if defined(CONFIG_DEBUG_FS)
2563 /**
2564  * amdgpu_debugfs_vm_bo_info  - print BO info for the VM
2565  *
2566  * @vm: Requested VM for printing BO info
2567  * @m: debugfs file
2568  *
2569  * Print BO information in debugfs file for the VM
2570  */
2571 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m)
2572 {
2573 	struct amdgpu_bo_va *bo_va, *tmp;
2574 	u64 total_idle = 0;
2575 	u64 total_evicted = 0;
2576 	u64 total_relocated = 0;
2577 	u64 total_moved = 0;
2578 	u64 total_invalidated = 0;
2579 	u64 total_done = 0;
2580 	unsigned int total_idle_objs = 0;
2581 	unsigned int total_evicted_objs = 0;
2582 	unsigned int total_relocated_objs = 0;
2583 	unsigned int total_moved_objs = 0;
2584 	unsigned int total_invalidated_objs = 0;
2585 	unsigned int total_done_objs = 0;
2586 	unsigned int id = 0;
2587 
2588 	spin_lock(&vm->status_lock);
2589 	seq_puts(m, "\tIdle BOs:\n");
2590 	list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
2591 		if (!bo_va->base.bo)
2592 			continue;
2593 		total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2594 	}
2595 	total_idle_objs = id;
2596 	id = 0;
2597 
2598 	seq_puts(m, "\tEvicted BOs:\n");
2599 	list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
2600 		if (!bo_va->base.bo)
2601 			continue;
2602 		total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2603 	}
2604 	total_evicted_objs = id;
2605 	id = 0;
2606 
2607 	seq_puts(m, "\tRelocated BOs:\n");
2608 	list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
2609 		if (!bo_va->base.bo)
2610 			continue;
2611 		total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2612 	}
2613 	total_relocated_objs = id;
2614 	id = 0;
2615 
2616 	seq_puts(m, "\tMoved BOs:\n");
2617 	list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
2618 		if (!bo_va->base.bo)
2619 			continue;
2620 		total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2621 	}
2622 	total_moved_objs = id;
2623 	id = 0;
2624 
2625 	seq_puts(m, "\tInvalidated BOs:\n");
2626 	list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
2627 		if (!bo_va->base.bo)
2628 			continue;
2629 		total_invalidated += amdgpu_bo_print_info(id++,	bo_va->base.bo, m);
2630 	}
2631 	total_invalidated_objs = id;
2632 	id = 0;
2633 
2634 	seq_puts(m, "\tDone BOs:\n");
2635 	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
2636 		if (!bo_va->base.bo)
2637 			continue;
2638 		total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2639 	}
2640 	spin_unlock(&vm->status_lock);
2641 	total_done_objs = id;
2642 
2643 	seq_printf(m, "\tTotal idle size:        %12lld\tobjs:\t%d\n", total_idle,
2644 		   total_idle_objs);
2645 	seq_printf(m, "\tTotal evicted size:     %12lld\tobjs:\t%d\n", total_evicted,
2646 		   total_evicted_objs);
2647 	seq_printf(m, "\tTotal relocated size:   %12lld\tobjs:\t%d\n", total_relocated,
2648 		   total_relocated_objs);
2649 	seq_printf(m, "\tTotal moved size:       %12lld\tobjs:\t%d\n", total_moved,
2650 		   total_moved_objs);
2651 	seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated,
2652 		   total_invalidated_objs);
2653 	seq_printf(m, "\tTotal done size:        %12lld\tobjs:\t%d\n", total_done,
2654 		   total_done_objs);
2655 }
2656 #endif
2657