xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_vm.c (revision 1f4dfa9277d5d6a7456f6a1a7468bca792407389)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2008 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  * Copyright 2008 Red Hat Inc.
4b843c749SSergey Zigachev  * Copyright 2009 Jerome Glisse.
5b843c749SSergey Zigachev  *
6b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
7b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
8b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
9b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
11b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
12b843c749SSergey Zigachev  *
13b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
14b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
15b843c749SSergey Zigachev  *
16b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
23b843c749SSergey Zigachev  *
24b843c749SSergey Zigachev  * Authors: Dave Airlie
25b843c749SSergey Zigachev  *          Alex Deucher
26b843c749SSergey Zigachev  *          Jerome Glisse
27b843c749SSergey Zigachev  */
28b843c749SSergey Zigachev #include <linux/dma-fence-array.h>
29b843c749SSergey Zigachev #include <linux/interval_tree_generic.h>
30b843c749SSergey Zigachev #include <linux/idr.h>
31b843c749SSergey Zigachev #include <drm/drmP.h>
32b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
33b843c749SSergey Zigachev #include "amdgpu.h"
34b843c749SSergey Zigachev #include "amdgpu_trace.h"
35b843c749SSergey Zigachev #include "amdgpu_amdkfd.h"
36b843c749SSergey Zigachev #include "amdgpu_gmc.h"
37b843c749SSergey Zigachev 
3878973132SSergey Zigachev #include <linux/rbtree.h>
3978973132SSergey Zigachev #include "amdgpu_vm.h"
4078973132SSergey Zigachev 
41b843c749SSergey Zigachev /**
42b843c749SSergey Zigachev  * DOC: GPUVM
43b843c749SSergey Zigachev  *
44b843c749SSergey Zigachev  * GPUVM is similar to the legacy gart on older asics, however
45b843c749SSergey Zigachev  * rather than there being a single global gart table
46b843c749SSergey Zigachev  * for the entire GPU, there are multiple VM page tables active
47b843c749SSergey Zigachev  * at any given time.  The VM page tables can contain a mix
48b843c749SSergey Zigachev  * vram pages and system memory pages and system memory pages
49b843c749SSergey Zigachev  * can be mapped as snooped (cached system pages) or unsnooped
50b843c749SSergey Zigachev  * (uncached system pages).
51b843c749SSergey Zigachev  * Each VM has an ID associated with it and there is a page table
52b843c749SSergey Zigachev  * associated with each VMID.  When execting a command buffer,
53b843c749SSergey Zigachev  * the kernel tells the the ring what VMID to use for that command
54b843c749SSergey Zigachev  * buffer.  VMIDs are allocated dynamically as commands are submitted.
55b843c749SSergey Zigachev  * The userspace drivers maintain their own address space and the kernel
56b843c749SSergey Zigachev  * sets up their pages tables accordingly when they submit their
57b843c749SSergey Zigachev  * command buffers and a VMID is assigned.
58b843c749SSergey Zigachev  * Cayman/Trinity support up to 8 active VMs at any given time;
59b843c749SSergey Zigachev  * SI supports 16.
60b843c749SSergey Zigachev  */
61b843c749SSergey Zigachev 
62b843c749SSergey Zigachev #define START(node) ((node)->start)
63b843c749SSergey Zigachev #define LAST(node) ((node)->last)
64b843c749SSergey Zigachev 
6578973132SSergey Zigachev #ifdef __linux__
6678973132SSergey Zigachev 
67b843c749SSergey Zigachev INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
68b843c749SSergey Zigachev 		     START, LAST, static, amdgpu_vm_it)
6978973132SSergey Zigachev #else
7078973132SSergey Zigachev static struct amdgpu_bo_va_mapping *
7178973132SSergey Zigachev amdgpu_vm_it_iter_first(struct rb_root_cached *root, uint64_t start,
7278973132SSergey Zigachev     uint64_t last)
7378973132SSergey Zigachev {
7478973132SSergey Zigachev 	struct amdgpu_bo_va_mapping *node;
7578973132SSergey Zigachev 	struct rb_node *rb;
7678973132SSergey Zigachev 
7778973132SSergey Zigachev 	for (rb = rb_first_cached(root); rb; rb = rb_next(rb)) {
7878973132SSergey Zigachev 		node = rb_entry(rb, typeof(*node), rb);
7978973132SSergey Zigachev 		if (LAST(node) >= start && START(node) <= last)
8078973132SSergey Zigachev 			return node;
8178973132SSergey Zigachev 	}
8278973132SSergey Zigachev 	return NULL;
8378973132SSergey Zigachev }
8478973132SSergey Zigachev 
8578973132SSergey Zigachev static struct amdgpu_bo_va_mapping *
8678973132SSergey Zigachev amdgpu_vm_it_iter_next(struct amdgpu_bo_va_mapping *node, uint64_t start,
8778973132SSergey Zigachev     uint64_t last)
8878973132SSergey Zigachev {
8978973132SSergey Zigachev 	STUB();
9078973132SSergey Zigachev 	struct rb_node *rb = &node->rb;
9178973132SSergey Zigachev 
9278973132SSergey Zigachev 	for (rb = rb_next(rb); rb; rb = rb_next(rb)) {
9378973132SSergey Zigachev 		node = rb_entry(rb, typeof(*node), rb);
9478973132SSergey Zigachev 		if (LAST(node) >= start && START(node) <= last)
9578973132SSergey Zigachev 			return node;
9678973132SSergey Zigachev 	}
9778973132SSergey Zigachev 	return NULL;
9878973132SSergey Zigachev }
9978973132SSergey Zigachev 
10078973132SSergey Zigachev static void
10178973132SSergey Zigachev amdgpu_vm_it_remove(struct amdgpu_bo_va_mapping *node,
10278973132SSergey Zigachev     struct rb_root_cached *root)
10378973132SSergey Zigachev {
10478973132SSergey Zigachev 	rb_erase_cached(&node->rb, root);
10578973132SSergey Zigachev }
10678973132SSergey Zigachev 
10778973132SSergey Zigachev static void
10878973132SSergey Zigachev amdgpu_vm_it_insert(struct amdgpu_bo_va_mapping *node,
10978973132SSergey Zigachev     struct rb_root_cached *root)
11078973132SSergey Zigachev {
11178973132SSergey Zigachev 	struct rb_node **iter = &root->rb_root.rb_node;
11278973132SSergey Zigachev 	struct rb_node *parent = NULL;
11378973132SSergey Zigachev 	struct amdgpu_bo_va_mapping *iter_node;
11478973132SSergey Zigachev 
11578973132SSergey Zigachev 	while (*iter) {
11678973132SSergey Zigachev 		parent = *iter;
11778973132SSergey Zigachev 		iter_node = rb_entry(*iter, struct amdgpu_bo_va_mapping, rb);
11878973132SSergey Zigachev 
11978973132SSergey Zigachev 		if (node->start < iter_node->start)
12078973132SSergey Zigachev 			iter = &(*iter)->rb_left;
12178973132SSergey Zigachev 		else
12278973132SSergey Zigachev 			iter = &(*iter)->rb_right;
12378973132SSergey Zigachev 	}
12478973132SSergey Zigachev 
12578973132SSergey Zigachev 	rb_link_node(&node->rb, parent, iter);
12678973132SSergey Zigachev 	rb_insert_color_cached(&node->rb, root, false);
12778973132SSergey Zigachev }
12878973132SSergey Zigachev #endif
129b843c749SSergey Zigachev 
130b843c749SSergey Zigachev #undef START
131b843c749SSergey Zigachev #undef LAST
132b843c749SSergey Zigachev 
133b843c749SSergey Zigachev /**
134b843c749SSergey Zigachev  * struct amdgpu_pte_update_params - Local structure
135b843c749SSergey Zigachev  *
136b843c749SSergey Zigachev  * Encapsulate some VM table update parameters to reduce
137b843c749SSergey Zigachev  * the number of function parameters
138b843c749SSergey Zigachev  *
139b843c749SSergey Zigachev  */
140b843c749SSergey Zigachev struct amdgpu_pte_update_params {
141b843c749SSergey Zigachev 
142b843c749SSergey Zigachev 	/**
143b843c749SSergey Zigachev 	 * @adev: amdgpu device we do this update for
144b843c749SSergey Zigachev 	 */
145b843c749SSergey Zigachev 	struct amdgpu_device *adev;
146b843c749SSergey Zigachev 
147b843c749SSergey Zigachev 	/**
148b843c749SSergey Zigachev 	 * @vm: optional amdgpu_vm we do this update for
149b843c749SSergey Zigachev 	 */
150b843c749SSergey Zigachev 	struct amdgpu_vm *vm;
151b843c749SSergey Zigachev 
152b843c749SSergey Zigachev 	/**
153b843c749SSergey Zigachev 	 * @src: address where to copy page table entries from
154b843c749SSergey Zigachev 	 */
155b843c749SSergey Zigachev 	uint64_t src;
156b843c749SSergey Zigachev 
157b843c749SSergey Zigachev 	/**
158b843c749SSergey Zigachev 	 * @ib: indirect buffer to fill with commands
159b843c749SSergey Zigachev 	 */
160b843c749SSergey Zigachev 	struct amdgpu_ib *ib;
161b843c749SSergey Zigachev 
162b843c749SSergey Zigachev 	/**
163b843c749SSergey Zigachev 	 * @func: Function which actually does the update
164b843c749SSergey Zigachev 	 */
165b843c749SSergey Zigachev 	void (*func)(struct amdgpu_pte_update_params *params,
166b843c749SSergey Zigachev 		     struct amdgpu_bo *bo, uint64_t pe,
167b843c749SSergey Zigachev 		     uint64_t addr, unsigned count, uint32_t incr,
168b843c749SSergey Zigachev 		     uint64_t flags);
169b843c749SSergey Zigachev 	/**
170b843c749SSergey Zigachev 	 * @pages_addr:
171b843c749SSergey Zigachev 	 *
172b843c749SSergey Zigachev 	 * DMA addresses to use for mapping, used during VM update by CPU
173b843c749SSergey Zigachev 	 */
174b843c749SSergey Zigachev 	dma_addr_t *pages_addr;
175b843c749SSergey Zigachev 
176b843c749SSergey Zigachev 	/**
177b843c749SSergey Zigachev 	 * @kptr:
178b843c749SSergey Zigachev 	 *
179b843c749SSergey Zigachev 	 * Kernel pointer of PD/PT BO that needs to be updated,
180b843c749SSergey Zigachev 	 * used during VM update by CPU
181b843c749SSergey Zigachev 	 */
182b843c749SSergey Zigachev 	void *kptr;
183b843c749SSergey Zigachev };
184b843c749SSergey Zigachev 
185b843c749SSergey Zigachev /**
186b843c749SSergey Zigachev  * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
187b843c749SSergey Zigachev  */
188b843c749SSergey Zigachev struct amdgpu_prt_cb {
189b843c749SSergey Zigachev 
190b843c749SSergey Zigachev 	/**
191b843c749SSergey Zigachev 	 * @adev: amdgpu device
192b843c749SSergey Zigachev 	 */
193b843c749SSergey Zigachev 	struct amdgpu_device *adev;
194b843c749SSergey Zigachev 
195b843c749SSergey Zigachev 	/**
196b843c749SSergey Zigachev 	 * @cb: callback
197b843c749SSergey Zigachev 	 */
198b843c749SSergey Zigachev 	struct dma_fence_cb cb;
199b843c749SSergey Zigachev };
200b843c749SSergey Zigachev 
201b843c749SSergey Zigachev /**
202b843c749SSergey Zigachev  * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
203b843c749SSergey Zigachev  *
204b843c749SSergey Zigachev  * @base: base structure for tracking BO usage in a VM
205b843c749SSergey Zigachev  * @vm: vm to which bo is to be added
206b843c749SSergey Zigachev  * @bo: amdgpu buffer object
207b843c749SSergey Zigachev  *
208b843c749SSergey Zigachev  * Initialize a bo_va_base structure and add it to the appropriate lists
209b843c749SSergey Zigachev  *
210b843c749SSergey Zigachev  */
amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base * base,struct amdgpu_vm * vm,struct amdgpu_bo * bo)211b843c749SSergey Zigachev static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
212b843c749SSergey Zigachev 				   struct amdgpu_vm *vm,
213b843c749SSergey Zigachev 				   struct amdgpu_bo *bo)
214b843c749SSergey Zigachev {
215b843c749SSergey Zigachev 	base->vm = vm;
216b843c749SSergey Zigachev 	base->bo = bo;
217b843c749SSergey Zigachev 	INIT_LIST_HEAD(&base->bo_list);
218b843c749SSergey Zigachev 	INIT_LIST_HEAD(&base->vm_status);
219b843c749SSergey Zigachev 
220b843c749SSergey Zigachev 	if (!bo)
221b843c749SSergey Zigachev 		return;
222b843c749SSergey Zigachev 	list_add_tail(&base->bo_list, &bo->va);
223b843c749SSergey Zigachev 
224b843c749SSergey Zigachev 	if (bo->tbo.type == ttm_bo_type_kernel)
225b843c749SSergey Zigachev 		list_move(&base->vm_status, &vm->relocated);
226b843c749SSergey Zigachev 
227b843c749SSergey Zigachev 	if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
228b843c749SSergey Zigachev 		return;
229b843c749SSergey Zigachev 
230b843c749SSergey Zigachev 	if (bo->preferred_domains &
231b843c749SSergey Zigachev 	    amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
232b843c749SSergey Zigachev 		return;
233b843c749SSergey Zigachev 
234b843c749SSergey Zigachev 	/*
235b843c749SSergey Zigachev 	 * we checked all the prerequisites, but it looks like this per vm bo
236b843c749SSergey Zigachev 	 * is currently evicted. add the bo to the evicted list to make sure it
237b843c749SSergey Zigachev 	 * is validated on next vm use to avoid fault.
238b843c749SSergey Zigachev 	 * */
239b843c749SSergey Zigachev 	list_move_tail(&base->vm_status, &vm->evicted);
240b843c749SSergey Zigachev 	base->moved = true;
241b843c749SSergey Zigachev }
242b843c749SSergey Zigachev 
243b843c749SSergey Zigachev /**
244b843c749SSergey Zigachev  * amdgpu_vm_level_shift - return the addr shift for each level
245b843c749SSergey Zigachev  *
246b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
247b843c749SSergey Zigachev  * @level: VMPT level
248b843c749SSergey Zigachev  *
249b843c749SSergey Zigachev  * Returns:
250b843c749SSergey Zigachev  * The number of bits the pfn needs to be right shifted for a level.
251b843c749SSergey Zigachev  */
amdgpu_vm_level_shift(struct amdgpu_device * adev,unsigned level)252b843c749SSergey Zigachev static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
253b843c749SSergey Zigachev 				      unsigned level)
254b843c749SSergey Zigachev {
255b843c749SSergey Zigachev 	unsigned shift = 0xff;
256b843c749SSergey Zigachev 
257b843c749SSergey Zigachev 	switch (level) {
258b843c749SSergey Zigachev 	case AMDGPU_VM_PDB2:
259b843c749SSergey Zigachev 	case AMDGPU_VM_PDB1:
260b843c749SSergey Zigachev 	case AMDGPU_VM_PDB0:
261b843c749SSergey Zigachev 		shift = 9 * (AMDGPU_VM_PDB0 - level) +
262b843c749SSergey Zigachev 			adev->vm_manager.block_size;
263b843c749SSergey Zigachev 		break;
264b843c749SSergey Zigachev 	case AMDGPU_VM_PTB:
265b843c749SSergey Zigachev 		shift = 0;
266b843c749SSergey Zigachev 		break;
267b843c749SSergey Zigachev 	default:
268b843c749SSergey Zigachev 		dev_err(adev->dev, "the level%d isn't supported.\n", level);
269b843c749SSergey Zigachev 	}
270b843c749SSergey Zigachev 
271b843c749SSergey Zigachev 	return shift;
272b843c749SSergey Zigachev }
273b843c749SSergey Zigachev 
274b843c749SSergey Zigachev /**
275b843c749SSergey Zigachev  * amdgpu_vm_num_entries - return the number of entries in a PD/PT
276b843c749SSergey Zigachev  *
277b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
278b843c749SSergey Zigachev  * @level: VMPT level
279b843c749SSergey Zigachev  *
280b843c749SSergey Zigachev  * Returns:
281b843c749SSergey Zigachev  * The number of entries in a page directory or page table.
282b843c749SSergey Zigachev  */
amdgpu_vm_num_entries(struct amdgpu_device * adev,unsigned level)283b843c749SSergey Zigachev static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
284b843c749SSergey Zigachev 				      unsigned level)
285b843c749SSergey Zigachev {
286b843c749SSergey Zigachev 	unsigned shift = amdgpu_vm_level_shift(adev,
287b843c749SSergey Zigachev 					       adev->vm_manager.root_level);
288b843c749SSergey Zigachev 
289b843c749SSergey Zigachev 	if (level == adev->vm_manager.root_level)
290b843c749SSergey Zigachev 		/* For the root directory */
291b843c749SSergey Zigachev 		return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
292b843c749SSergey Zigachev 	else if (level != AMDGPU_VM_PTB)
293b843c749SSergey Zigachev 		/* Everything in between */
294b843c749SSergey Zigachev 		return 512;
295b843c749SSergey Zigachev 	else
296b843c749SSergey Zigachev 		/* For the page tables on the leaves */
297b843c749SSergey Zigachev 		return AMDGPU_VM_PTE_COUNT(adev);
298b843c749SSergey Zigachev }
299b843c749SSergey Zigachev 
300b843c749SSergey Zigachev /**
301b843c749SSergey Zigachev  * amdgpu_vm_bo_size - returns the size of the BOs in bytes
302b843c749SSergey Zigachev  *
303b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
304b843c749SSergey Zigachev  * @level: VMPT level
305b843c749SSergey Zigachev  *
306b843c749SSergey Zigachev  * Returns:
307b843c749SSergey Zigachev  * The size of the BO for a page directory or page table in bytes.
308b843c749SSergey Zigachev  */
amdgpu_vm_bo_size(struct amdgpu_device * adev,unsigned level)309b843c749SSergey Zigachev static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
310b843c749SSergey Zigachev {
311b843c749SSergey Zigachev 	return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
312b843c749SSergey Zigachev }
313b843c749SSergey Zigachev 
314b843c749SSergey Zigachev /**
315b843c749SSergey Zigachev  * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
316b843c749SSergey Zigachev  *
317b843c749SSergey Zigachev  * @vm: vm providing the BOs
318b843c749SSergey Zigachev  * @validated: head of validation list
319b843c749SSergey Zigachev  * @entry: entry to add
320b843c749SSergey Zigachev  *
321b843c749SSergey Zigachev  * Add the page directory to the list of BOs to
322b843c749SSergey Zigachev  * validate for command submission.
323b843c749SSergey Zigachev  */
amdgpu_vm_get_pd_bo(struct amdgpu_vm * vm,struct list_head * validated,struct amdgpu_bo_list_entry * entry)324b843c749SSergey Zigachev void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
325b843c749SSergey Zigachev 			 struct list_head *validated,
326b843c749SSergey Zigachev 			 struct amdgpu_bo_list_entry *entry)
327b843c749SSergey Zigachev {
328b843c749SSergey Zigachev 	entry->robj = vm->root.base.bo;
329b843c749SSergey Zigachev 	entry->priority = 0;
330b843c749SSergey Zigachev 	entry->tv.bo = &entry->robj->tbo;
331b843c749SSergey Zigachev 	entry->tv.shared = true;
332b843c749SSergey Zigachev 	entry->user_pages = NULL;
333b843c749SSergey Zigachev 	list_add(&entry->tv.head, validated);
334b843c749SSergey Zigachev }
335b843c749SSergey Zigachev 
336b843c749SSergey Zigachev /**
337b843c749SSergey Zigachev  * amdgpu_vm_validate_pt_bos - validate the page table BOs
338b843c749SSergey Zigachev  *
339b843c749SSergey Zigachev  * @adev: amdgpu device pointer
340b843c749SSergey Zigachev  * @vm: vm providing the BOs
341b843c749SSergey Zigachev  * @validate: callback to do the validation
342b843c749SSergey Zigachev  * @param: parameter for the validation callback
343b843c749SSergey Zigachev  *
344b843c749SSergey Zigachev  * Validate the page table BOs on command submission if neccessary.
345b843c749SSergey Zigachev  *
346b843c749SSergey Zigachev  * Returns:
347b843c749SSergey Zigachev  * Validation result.
348b843c749SSergey Zigachev  */
amdgpu_vm_validate_pt_bos(struct amdgpu_device * adev,struct amdgpu_vm * vm,int (* validate)(void * p,struct amdgpu_bo * bo),void * param)349b843c749SSergey Zigachev int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
350b843c749SSergey Zigachev 			      int (*validate)(void *p, struct amdgpu_bo *bo),
351b843c749SSergey Zigachev 			      void *param)
352b843c749SSergey Zigachev {
353b843c749SSergey Zigachev 	struct ttm_bo_global *glob = adev->mman.bdev.glob;
354b843c749SSergey Zigachev 	struct amdgpu_vm_bo_base *bo_base, *tmp;
355b843c749SSergey Zigachev 	int r = 0;
356b843c749SSergey Zigachev 
357b843c749SSergey Zigachev 	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
358b843c749SSergey Zigachev 		struct amdgpu_bo *bo = bo_base->bo;
359b843c749SSergey Zigachev 
360b843c749SSergey Zigachev 		if (bo->parent) {
361b843c749SSergey Zigachev 			r = validate(param, bo);
362b843c749SSergey Zigachev 			if (r)
363b843c749SSergey Zigachev 				break;
364b843c749SSergey Zigachev 
36578973132SSergey Zigachev 			lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
366b843c749SSergey Zigachev 			ttm_bo_move_to_lru_tail(&bo->tbo);
367b843c749SSergey Zigachev 			if (bo->shadow)
368b843c749SSergey Zigachev 				ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
36978973132SSergey Zigachev 			lockmgr(&glob->lru_lock, LK_RELEASE);
370b843c749SSergey Zigachev 		}
371b843c749SSergey Zigachev 
372b843c749SSergey Zigachev 		if (bo->tbo.type != ttm_bo_type_kernel) {
37378973132SSergey Zigachev 			lockmgr(&vm->moved_lock, LK_EXCLUSIVE);
374b843c749SSergey Zigachev 			list_move(&bo_base->vm_status, &vm->moved);
37578973132SSergey Zigachev 			lockmgr(&vm->moved_lock, LK_RELEASE);
376b843c749SSergey Zigachev 		} else {
377b843c749SSergey Zigachev 			list_move(&bo_base->vm_status, &vm->relocated);
378b843c749SSergey Zigachev 		}
379b843c749SSergey Zigachev 	}
380b843c749SSergey Zigachev 
38178973132SSergey Zigachev 	lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
382b843c749SSergey Zigachev 	list_for_each_entry(bo_base, &vm->idle, vm_status) {
383b843c749SSergey Zigachev 		struct amdgpu_bo *bo = bo_base->bo;
384b843c749SSergey Zigachev 
385b843c749SSergey Zigachev 		if (!bo->parent)
386b843c749SSergey Zigachev 			continue;
387b843c749SSergey Zigachev 
388b843c749SSergey Zigachev 		ttm_bo_move_to_lru_tail(&bo->tbo);
389b843c749SSergey Zigachev 		if (bo->shadow)
390b843c749SSergey Zigachev 			ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
391b843c749SSergey Zigachev 	}
39278973132SSergey Zigachev 	lockmgr(&glob->lru_lock, LK_RELEASE);
393b843c749SSergey Zigachev 
394b843c749SSergey Zigachev 	return r;
395b843c749SSergey Zigachev }
396b843c749SSergey Zigachev 
397b843c749SSergey Zigachev /**
398b843c749SSergey Zigachev  * amdgpu_vm_ready - check VM is ready for updates
399b843c749SSergey Zigachev  *
400b843c749SSergey Zigachev  * @vm: VM to check
401b843c749SSergey Zigachev  *
402b843c749SSergey Zigachev  * Check if all VM PDs/PTs are ready for updates
403b843c749SSergey Zigachev  *
404b843c749SSergey Zigachev  * Returns:
405b843c749SSergey Zigachev  * True if eviction list is empty.
406b843c749SSergey Zigachev  */
amdgpu_vm_ready(struct amdgpu_vm * vm)407b843c749SSergey Zigachev bool amdgpu_vm_ready(struct amdgpu_vm *vm)
408b843c749SSergey Zigachev {
409b843c749SSergey Zigachev 	return list_empty(&vm->evicted);
410b843c749SSergey Zigachev }
411b843c749SSergey Zigachev 
412b843c749SSergey Zigachev /**
413b843c749SSergey Zigachev  * amdgpu_vm_clear_bo - initially clear the PDs/PTs
414b843c749SSergey Zigachev  *
415b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
416b843c749SSergey Zigachev  * @vm: VM to clear BO from
417b843c749SSergey Zigachev  * @bo: BO to clear
418b843c749SSergey Zigachev  * @level: level this BO is at
419b843c749SSergey Zigachev  * @pte_support_ats: indicate ATS support from PTE
420b843c749SSergey Zigachev  *
421b843c749SSergey Zigachev  * Root PD needs to be reserved when calling this.
422b843c749SSergey Zigachev  *
423b843c749SSergey Zigachev  * Returns:
424b843c749SSergey Zigachev  * 0 on success, errno otherwise.
425b843c749SSergey Zigachev  */
amdgpu_vm_clear_bo(struct amdgpu_device * adev,struct amdgpu_vm * vm,struct amdgpu_bo * bo,unsigned level,bool pte_support_ats)426b843c749SSergey Zigachev static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
427b843c749SSergey Zigachev 			      struct amdgpu_vm *vm, struct amdgpu_bo *bo,
428b843c749SSergey Zigachev 			      unsigned level, bool pte_support_ats)
429b843c749SSergey Zigachev {
430b843c749SSergey Zigachev 	struct ttm_operation_ctx ctx = { true, false };
431b843c749SSergey Zigachev 	struct dma_fence *fence = NULL;
432b843c749SSergey Zigachev 	unsigned entries, ats_entries;
433b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
434b843c749SSergey Zigachev 	struct amdgpu_job *job;
435b843c749SSergey Zigachev 	uint64_t addr;
436b843c749SSergey Zigachev 	int r;
437b843c749SSergey Zigachev 
438b843c749SSergey Zigachev 	entries = amdgpu_bo_size(bo) / 8;
439b843c749SSergey Zigachev 
440b843c749SSergey Zigachev 	if (pte_support_ats) {
441b843c749SSergey Zigachev 		if (level == adev->vm_manager.root_level) {
442b843c749SSergey Zigachev 			ats_entries = amdgpu_vm_level_shift(adev, level);
443b843c749SSergey Zigachev 			ats_entries += AMDGPU_GPU_PAGE_SHIFT;
444b843c749SSergey Zigachev 			ats_entries = AMDGPU_VA_HOLE_START >> ats_entries;
445b843c749SSergey Zigachev 			ats_entries = min(ats_entries, entries);
446b843c749SSergey Zigachev 			entries -= ats_entries;
447b843c749SSergey Zigachev 		} else {
448b843c749SSergey Zigachev 			ats_entries = entries;
449b843c749SSergey Zigachev 			entries = 0;
450b843c749SSergey Zigachev 		}
451b843c749SSergey Zigachev 	} else {
452b843c749SSergey Zigachev 		ats_entries = 0;
453b843c749SSergey Zigachev 	}
454b843c749SSergey Zigachev 
455b843c749SSergey Zigachev 	ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
456b843c749SSergey Zigachev 
457b843c749SSergey Zigachev 	r = reservation_object_reserve_shared(bo->tbo.resv);
458b843c749SSergey Zigachev 	if (r)
459b843c749SSergey Zigachev 		return r;
460b843c749SSergey Zigachev 
461b843c749SSergey Zigachev 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
462b843c749SSergey Zigachev 	if (r)
463b843c749SSergey Zigachev 		goto error;
464b843c749SSergey Zigachev 
465b843c749SSergey Zigachev 	r = amdgpu_job_alloc_with_ib(adev, 64, &job);
466b843c749SSergey Zigachev 	if (r)
467b843c749SSergey Zigachev 		goto error;
468b843c749SSergey Zigachev 
469b843c749SSergey Zigachev 	addr = amdgpu_bo_gpu_offset(bo);
470b843c749SSergey Zigachev 	if (ats_entries) {
471b843c749SSergey Zigachev 		uint64_t ats_value;
472b843c749SSergey Zigachev 
473b843c749SSergey Zigachev 		ats_value = AMDGPU_PTE_DEFAULT_ATC;
474b843c749SSergey Zigachev 		if (level != AMDGPU_VM_PTB)
475b843c749SSergey Zigachev 			ats_value |= AMDGPU_PDE_PTE;
476b843c749SSergey Zigachev 
477b843c749SSergey Zigachev 		amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
478b843c749SSergey Zigachev 				      ats_entries, 0, ats_value);
479b843c749SSergey Zigachev 		addr += ats_entries * 8;
480b843c749SSergey Zigachev 	}
481b843c749SSergey Zigachev 
482b843c749SSergey Zigachev 	if (entries)
483b843c749SSergey Zigachev 		amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
484b843c749SSergey Zigachev 				      entries, 0, 0);
485b843c749SSergey Zigachev 
486b843c749SSergey Zigachev 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
487b843c749SSergey Zigachev 
488b843c749SSergey Zigachev 	WARN_ON(job->ibs[0].length_dw > 64);
489b843c749SSergey Zigachev 	r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
490b843c749SSergey Zigachev 			     AMDGPU_FENCE_OWNER_UNDEFINED, false);
491b843c749SSergey Zigachev 	if (r)
492b843c749SSergey Zigachev 		goto error_free;
493b843c749SSergey Zigachev 
494b843c749SSergey Zigachev 	r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_UNDEFINED,
495b843c749SSergey Zigachev 			      &fence);
496b843c749SSergey Zigachev 	if (r)
497b843c749SSergey Zigachev 		goto error_free;
498b843c749SSergey Zigachev 
499b843c749SSergey Zigachev 	amdgpu_bo_fence(bo, fence, true);
500b843c749SSergey Zigachev 	dma_fence_put(fence);
501b843c749SSergey Zigachev 
502b843c749SSergey Zigachev 	if (bo->shadow)
503b843c749SSergey Zigachev 		return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
504b843c749SSergey Zigachev 					  level, pte_support_ats);
505b843c749SSergey Zigachev 
506b843c749SSergey Zigachev 	return 0;
507b843c749SSergey Zigachev 
508b843c749SSergey Zigachev error_free:
509b843c749SSergey Zigachev 	amdgpu_job_free(job);
510b843c749SSergey Zigachev 
511b843c749SSergey Zigachev error:
512b843c749SSergey Zigachev 	return r;
513b843c749SSergey Zigachev }
514b843c749SSergey Zigachev 
515b843c749SSergey Zigachev /**
516b843c749SSergey Zigachev  * amdgpu_vm_alloc_levels - allocate the PD/PT levels
517b843c749SSergey Zigachev  *
518b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
519b843c749SSergey Zigachev  * @vm: requested vm
520b843c749SSergey Zigachev  * @parent: parent PT
521b843c749SSergey Zigachev  * @saddr: start of the address range
522b843c749SSergey Zigachev  * @eaddr: end of the address range
523b843c749SSergey Zigachev  * @level: VMPT level
524b843c749SSergey Zigachev  * @ats: indicate ATS support from PTE
525b843c749SSergey Zigachev  *
526b843c749SSergey Zigachev  * Make sure the page directories and page tables are allocated
527b843c749SSergey Zigachev  *
528b843c749SSergey Zigachev  * Returns:
529b843c749SSergey Zigachev  * 0 on success, errno otherwise.
530b843c749SSergey Zigachev  */
amdgpu_vm_alloc_levels(struct amdgpu_device * adev,struct amdgpu_vm * vm,struct amdgpu_vm_pt * parent,uint64_t saddr,uint64_t eaddr,unsigned level,bool ats)531b843c749SSergey Zigachev static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
532b843c749SSergey Zigachev 				  struct amdgpu_vm *vm,
533b843c749SSergey Zigachev 				  struct amdgpu_vm_pt *parent,
534b843c749SSergey Zigachev 				  uint64_t saddr, uint64_t eaddr,
535b843c749SSergey Zigachev 				  unsigned level, bool ats)
536b843c749SSergey Zigachev {
537b843c749SSergey Zigachev 	unsigned shift = amdgpu_vm_level_shift(adev, level);
538b843c749SSergey Zigachev 	unsigned pt_idx, from, to;
539b843c749SSergey Zigachev 	u64 flags;
540b843c749SSergey Zigachev 	int r;
541b843c749SSergey Zigachev 
542b843c749SSergey Zigachev 	if (!parent->entries) {
543b843c749SSergey Zigachev 		unsigned num_entries = amdgpu_vm_num_entries(adev, level);
544b843c749SSergey Zigachev 
545b843c749SSergey Zigachev 		parent->entries = kvmalloc_array(num_entries,
546b843c749SSergey Zigachev 						   sizeof(struct amdgpu_vm_pt),
547b843c749SSergey Zigachev 						   GFP_KERNEL | __GFP_ZERO);
548b843c749SSergey Zigachev 		if (!parent->entries)
549b843c749SSergey Zigachev 			return -ENOMEM;
550b843c749SSergey Zigachev 		memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
551b843c749SSergey Zigachev 	}
552b843c749SSergey Zigachev 
553b843c749SSergey Zigachev 	from = saddr >> shift;
554b843c749SSergey Zigachev 	to = eaddr >> shift;
555b843c749SSergey Zigachev 	if (from >= amdgpu_vm_num_entries(adev, level) ||
556b843c749SSergey Zigachev 	    to >= amdgpu_vm_num_entries(adev, level))
557b843c749SSergey Zigachev 		return -EINVAL;
558b843c749SSergey Zigachev 
559b843c749SSergey Zigachev 	++level;
560b843c749SSergey Zigachev 	saddr = saddr & ((1 << shift) - 1);
561b843c749SSergey Zigachev 	eaddr = eaddr & ((1 << shift) - 1);
562b843c749SSergey Zigachev 
563b843c749SSergey Zigachev 	flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
564b843c749SSergey Zigachev 	if (vm->root.base.bo->shadow)
565b843c749SSergey Zigachev 		flags |= AMDGPU_GEM_CREATE_SHADOW;
566b843c749SSergey Zigachev 	if (vm->use_cpu_for_update)
567b843c749SSergey Zigachev 		flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
568b843c749SSergey Zigachev 	else
569b843c749SSergey Zigachev 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
570b843c749SSergey Zigachev 
571b843c749SSergey Zigachev 	/* walk over the address space and allocate the page tables */
572b843c749SSergey Zigachev 	for (pt_idx = from; pt_idx <= to; ++pt_idx) {
573b843c749SSergey Zigachev 		struct reservation_object *resv = vm->root.base.bo->tbo.resv;
574b843c749SSergey Zigachev 		struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
575b843c749SSergey Zigachev 		struct amdgpu_bo *pt;
576b843c749SSergey Zigachev 
577b843c749SSergey Zigachev 		if (!entry->base.bo) {
578b843c749SSergey Zigachev 			struct amdgpu_bo_param bp;
579b843c749SSergey Zigachev 
580b843c749SSergey Zigachev 			memset(&bp, 0, sizeof(bp));
581b843c749SSergey Zigachev 			bp.size = amdgpu_vm_bo_size(adev, level);
582b843c749SSergey Zigachev 			bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
583b843c749SSergey Zigachev 			bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
584b843c749SSergey Zigachev 			bp.flags = flags;
585b843c749SSergey Zigachev 			bp.type = ttm_bo_type_kernel;
586b843c749SSergey Zigachev 			bp.resv = resv;
587b843c749SSergey Zigachev 			r = amdgpu_bo_create(adev, &bp, &pt);
588b843c749SSergey Zigachev 			if (r)
589b843c749SSergey Zigachev 				return r;
590b843c749SSergey Zigachev 
591b843c749SSergey Zigachev 			r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats);
592b843c749SSergey Zigachev 			if (r) {
593b843c749SSergey Zigachev 				amdgpu_bo_unref(&pt->shadow);
594b843c749SSergey Zigachev 				amdgpu_bo_unref(&pt);
595b843c749SSergey Zigachev 				return r;
596b843c749SSergey Zigachev 			}
597b843c749SSergey Zigachev 
598b843c749SSergey Zigachev 			if (vm->use_cpu_for_update) {
599b843c749SSergey Zigachev 				r = amdgpu_bo_kmap(pt, NULL);
600b843c749SSergey Zigachev 				if (r) {
601b843c749SSergey Zigachev 					amdgpu_bo_unref(&pt->shadow);
602b843c749SSergey Zigachev 					amdgpu_bo_unref(&pt);
603b843c749SSergey Zigachev 					return r;
604b843c749SSergey Zigachev 				}
605b843c749SSergey Zigachev 			}
606b843c749SSergey Zigachev 
607b843c749SSergey Zigachev 			/* Keep a reference to the root directory to avoid
608b843c749SSergey Zigachev 			* freeing them up in the wrong order.
609b843c749SSergey Zigachev 			*/
610b843c749SSergey Zigachev 			pt->parent = amdgpu_bo_ref(parent->base.bo);
611b843c749SSergey Zigachev 
612b843c749SSergey Zigachev 			amdgpu_vm_bo_base_init(&entry->base, vm, pt);
613b843c749SSergey Zigachev 		}
614b843c749SSergey Zigachev 
615b843c749SSergey Zigachev 		if (level < AMDGPU_VM_PTB) {
616b843c749SSergey Zigachev 			uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
617b843c749SSergey Zigachev 			uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
618b843c749SSergey Zigachev 				((1 << shift) - 1);
619b843c749SSergey Zigachev 			r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
620b843c749SSergey Zigachev 						   sub_eaddr, level, ats);
621b843c749SSergey Zigachev 			if (r)
622b843c749SSergey Zigachev 				return r;
623b843c749SSergey Zigachev 		}
624b843c749SSergey Zigachev 	}
625b843c749SSergey Zigachev 
626b843c749SSergey Zigachev 	return 0;
627b843c749SSergey Zigachev }
628b843c749SSergey Zigachev 
629b843c749SSergey Zigachev /**
630b843c749SSergey Zigachev  * amdgpu_vm_alloc_pts - Allocate page tables.
631b843c749SSergey Zigachev  *
632b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
633b843c749SSergey Zigachev  * @vm: VM to allocate page tables for
634b843c749SSergey Zigachev  * @saddr: Start address which needs to be allocated
635b843c749SSergey Zigachev  * @size: Size from start address we need.
636b843c749SSergey Zigachev  *
637b843c749SSergey Zigachev  * Make sure the page tables are allocated.
638b843c749SSergey Zigachev  *
639b843c749SSergey Zigachev  * Returns:
640b843c749SSergey Zigachev  * 0 on success, errno otherwise.
641b843c749SSergey Zigachev  */
amdgpu_vm_alloc_pts(struct amdgpu_device * adev,struct amdgpu_vm * vm,uint64_t saddr,uint64_t size)642b843c749SSergey Zigachev int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
643b843c749SSergey Zigachev 			struct amdgpu_vm *vm,
644b843c749SSergey Zigachev 			uint64_t saddr, uint64_t size)
645b843c749SSergey Zigachev {
646b843c749SSergey Zigachev 	uint64_t eaddr;
647b843c749SSergey Zigachev 	bool ats = false;
648b843c749SSergey Zigachev 
649b843c749SSergey Zigachev 	/* validate the parameters */
650b843c749SSergey Zigachev 	if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
651b843c749SSergey Zigachev 		return -EINVAL;
652b843c749SSergey Zigachev 
653b843c749SSergey Zigachev 	eaddr = saddr + size - 1;
654b843c749SSergey Zigachev 
655b843c749SSergey Zigachev 	if (vm->pte_support_ats)
656b843c749SSergey Zigachev 		ats = saddr < AMDGPU_VA_HOLE_START;
657b843c749SSergey Zigachev 
658b843c749SSergey Zigachev 	saddr /= AMDGPU_GPU_PAGE_SIZE;
659b843c749SSergey Zigachev 	eaddr /= AMDGPU_GPU_PAGE_SIZE;
660b843c749SSergey Zigachev 
661b843c749SSergey Zigachev 	if (eaddr >= adev->vm_manager.max_pfn) {
66278973132SSergey Zigachev 		dev_err(adev->dev, "va above limit (0x%08lX >= 0x%08lX)\n",
663b843c749SSergey Zigachev 			eaddr, adev->vm_manager.max_pfn);
664b843c749SSergey Zigachev 		return -EINVAL;
665b843c749SSergey Zigachev 	}
666b843c749SSergey Zigachev 
667b843c749SSergey Zigachev 	return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
668b843c749SSergey Zigachev 				      adev->vm_manager.root_level, ats);
669b843c749SSergey Zigachev }
670b843c749SSergey Zigachev 
671b843c749SSergey Zigachev /**
672b843c749SSergey Zigachev  * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
673b843c749SSergey Zigachev  *
674b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
675b843c749SSergey Zigachev  */
amdgpu_vm_check_compute_bug(struct amdgpu_device * adev)676b843c749SSergey Zigachev void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
677b843c749SSergey Zigachev {
678b843c749SSergey Zigachev 	const struct amdgpu_ip_block *ip_block;
679b843c749SSergey Zigachev 	bool has_compute_vm_bug;
680b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
681b843c749SSergey Zigachev 	int i;
682b843c749SSergey Zigachev 
683b843c749SSergey Zigachev 	has_compute_vm_bug = false;
684b843c749SSergey Zigachev 
685b843c749SSergey Zigachev 	ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
686b843c749SSergey Zigachev 	if (ip_block) {
687b843c749SSergey Zigachev 		/* Compute has a VM bug for GFX version < 7.
688b843c749SSergey Zigachev 		   Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
689b843c749SSergey Zigachev 		if (ip_block->version->major <= 7)
690b843c749SSergey Zigachev 			has_compute_vm_bug = true;
691b843c749SSergey Zigachev 		else if (ip_block->version->major == 8)
692b843c749SSergey Zigachev 			if (adev->gfx.mec_fw_version < 673)
693b843c749SSergey Zigachev 				has_compute_vm_bug = true;
694b843c749SSergey Zigachev 	}
695b843c749SSergey Zigachev 
696b843c749SSergey Zigachev 	for (i = 0; i < adev->num_rings; i++) {
697b843c749SSergey Zigachev 		ring = adev->rings[i];
698b843c749SSergey Zigachev 		if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
699b843c749SSergey Zigachev 			/* only compute rings */
700b843c749SSergey Zigachev 			ring->has_compute_vm_bug = has_compute_vm_bug;
701b843c749SSergey Zigachev 		else
702b843c749SSergey Zigachev 			ring->has_compute_vm_bug = false;
703b843c749SSergey Zigachev 	}
704b843c749SSergey Zigachev }
705b843c749SSergey Zigachev 
706b843c749SSergey Zigachev /**
707b843c749SSergey Zigachev  * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
708b843c749SSergey Zigachev  *
709b843c749SSergey Zigachev  * @ring: ring on which the job will be submitted
710b843c749SSergey Zigachev  * @job: job to submit
711b843c749SSergey Zigachev  *
712b843c749SSergey Zigachev  * Returns:
713b843c749SSergey Zigachev  * True if sync is needed.
714b843c749SSergey Zigachev  */
amdgpu_vm_need_pipeline_sync(struct amdgpu_ring * ring,struct amdgpu_job * job)715b843c749SSergey Zigachev bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
716b843c749SSergey Zigachev 				  struct amdgpu_job *job)
717b843c749SSergey Zigachev {
718b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
719b843c749SSergey Zigachev 	unsigned vmhub = ring->funcs->vmhub;
720b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
721b843c749SSergey Zigachev 	struct amdgpu_vmid *id;
722b843c749SSergey Zigachev 	bool gds_switch_needed;
723b843c749SSergey Zigachev 	bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
724b843c749SSergey Zigachev 
725b843c749SSergey Zigachev 	if (job->vmid == 0)
726b843c749SSergey Zigachev 		return false;
727b843c749SSergey Zigachev 	id = &id_mgr->ids[job->vmid];
728b843c749SSergey Zigachev 	gds_switch_needed = ring->funcs->emit_gds_switch && (
729b843c749SSergey Zigachev 		id->gds_base != job->gds_base ||
730b843c749SSergey Zigachev 		id->gds_size != job->gds_size ||
731b843c749SSergey Zigachev 		id->gws_base != job->gws_base ||
732b843c749SSergey Zigachev 		id->gws_size != job->gws_size ||
733b843c749SSergey Zigachev 		id->oa_base != job->oa_base ||
734b843c749SSergey Zigachev 		id->oa_size != job->oa_size);
735b843c749SSergey Zigachev 
736b843c749SSergey Zigachev 	if (amdgpu_vmid_had_gpu_reset(adev, id))
737b843c749SSergey Zigachev 		return true;
738b843c749SSergey Zigachev 
739b843c749SSergey Zigachev 	return vm_flush_needed || gds_switch_needed;
740b843c749SSergey Zigachev }
741b843c749SSergey Zigachev 
742b843c749SSergey Zigachev /**
743b843c749SSergey Zigachev  * amdgpu_vm_flush - hardware flush the vm
744b843c749SSergey Zigachev  *
745b843c749SSergey Zigachev  * @ring: ring to use for flush
746b843c749SSergey Zigachev  * @job:  related job
747b843c749SSergey Zigachev  * @need_pipe_sync: is pipe sync needed
748b843c749SSergey Zigachev  *
749b843c749SSergey Zigachev  * Emit a VM flush when it is necessary.
750b843c749SSergey Zigachev  *
751b843c749SSergey Zigachev  * Returns:
752b843c749SSergey Zigachev  * 0 on success, errno otherwise.
753b843c749SSergey Zigachev  */
amdgpu_vm_flush(struct amdgpu_ring * ring,struct amdgpu_job * job,bool need_pipe_sync)754b843c749SSergey Zigachev int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
755b843c749SSergey Zigachev {
756b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
757b843c749SSergey Zigachev 	unsigned vmhub = ring->funcs->vmhub;
758b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
759b843c749SSergey Zigachev 	struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
760b843c749SSergey Zigachev 	bool gds_switch_needed = ring->funcs->emit_gds_switch && (
761b843c749SSergey Zigachev 		id->gds_base != job->gds_base ||
762b843c749SSergey Zigachev 		id->gds_size != job->gds_size ||
763b843c749SSergey Zigachev 		id->gws_base != job->gws_base ||
764b843c749SSergey Zigachev 		id->gws_size != job->gws_size ||
765b843c749SSergey Zigachev 		id->oa_base != job->oa_base ||
766b843c749SSergey Zigachev 		id->oa_size != job->oa_size);
767b843c749SSergey Zigachev 	bool vm_flush_needed = job->vm_needs_flush;
768b843c749SSergey Zigachev 	struct dma_fence *fence = NULL;
769b843c749SSergey Zigachev 	bool pasid_mapping_needed = false;
770b843c749SSergey Zigachev 	unsigned patch_offset = 0;
771b843c749SSergey Zigachev 	int r;
772b843c749SSergey Zigachev 
773b843c749SSergey Zigachev 	if (amdgpu_vmid_had_gpu_reset(adev, id)) {
774b843c749SSergey Zigachev 		gds_switch_needed = true;
775b843c749SSergey Zigachev 		vm_flush_needed = true;
776b843c749SSergey Zigachev 		pasid_mapping_needed = true;
777b843c749SSergey Zigachev 	}
778b843c749SSergey Zigachev 
779b843c749SSergey Zigachev 	mutex_lock(&id_mgr->lock);
780b843c749SSergey Zigachev 	if (id->pasid != job->pasid || !id->pasid_mapping ||
781b843c749SSergey Zigachev 	    !dma_fence_is_signaled(id->pasid_mapping))
782b843c749SSergey Zigachev 		pasid_mapping_needed = true;
783b843c749SSergey Zigachev 	mutex_unlock(&id_mgr->lock);
784b843c749SSergey Zigachev 
785b843c749SSergey Zigachev 	gds_switch_needed &= !!ring->funcs->emit_gds_switch;
786b843c749SSergey Zigachev 	vm_flush_needed &= !!ring->funcs->emit_vm_flush  &&
787b843c749SSergey Zigachev 			job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
788b843c749SSergey Zigachev 	pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
789b843c749SSergey Zigachev 		ring->funcs->emit_wreg;
790b843c749SSergey Zigachev 
791b843c749SSergey Zigachev 	if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
792b843c749SSergey Zigachev 		return 0;
793b843c749SSergey Zigachev 
794b843c749SSergey Zigachev 	if (ring->funcs->init_cond_exec)
795b843c749SSergey Zigachev 		patch_offset = amdgpu_ring_init_cond_exec(ring);
796b843c749SSergey Zigachev 
797b843c749SSergey Zigachev 	if (need_pipe_sync)
798b843c749SSergey Zigachev 		amdgpu_ring_emit_pipeline_sync(ring);
799b843c749SSergey Zigachev 
800b843c749SSergey Zigachev 	if (vm_flush_needed) {
801b843c749SSergey Zigachev 		trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
802b843c749SSergey Zigachev 		amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
803b843c749SSergey Zigachev 	}
804b843c749SSergey Zigachev 
805b843c749SSergey Zigachev 	if (pasid_mapping_needed)
806b843c749SSergey Zigachev 		amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
807b843c749SSergey Zigachev 
808b843c749SSergey Zigachev 	if (vm_flush_needed || pasid_mapping_needed) {
809b843c749SSergey Zigachev 		r = amdgpu_fence_emit(ring, &fence, 0);
810b843c749SSergey Zigachev 		if (r)
811b843c749SSergey Zigachev 			return r;
812b843c749SSergey Zigachev 	}
813b843c749SSergey Zigachev 
814b843c749SSergey Zigachev 	if (vm_flush_needed) {
815b843c749SSergey Zigachev 		mutex_lock(&id_mgr->lock);
816b843c749SSergey Zigachev 		dma_fence_put(id->last_flush);
817b843c749SSergey Zigachev 		id->last_flush = dma_fence_get(fence);
818b843c749SSergey Zigachev 		id->current_gpu_reset_count =
819b843c749SSergey Zigachev 			atomic_read(&adev->gpu_reset_counter);
820b843c749SSergey Zigachev 		mutex_unlock(&id_mgr->lock);
821b843c749SSergey Zigachev 	}
822b843c749SSergey Zigachev 
823b843c749SSergey Zigachev 	if (pasid_mapping_needed) {
824b843c749SSergey Zigachev 		mutex_lock(&id_mgr->lock);
825b843c749SSergey Zigachev 		id->pasid = job->pasid;
826b843c749SSergey Zigachev 		dma_fence_put(id->pasid_mapping);
827b843c749SSergey Zigachev 		id->pasid_mapping = dma_fence_get(fence);
828b843c749SSergey Zigachev 		mutex_unlock(&id_mgr->lock);
829b843c749SSergey Zigachev 	}
830b843c749SSergey Zigachev 	dma_fence_put(fence);
831b843c749SSergey Zigachev 
832b843c749SSergey Zigachev 	if (ring->funcs->emit_gds_switch && gds_switch_needed) {
833b843c749SSergey Zigachev 		id->gds_base = job->gds_base;
834b843c749SSergey Zigachev 		id->gds_size = job->gds_size;
835b843c749SSergey Zigachev 		id->gws_base = job->gws_base;
836b843c749SSergey Zigachev 		id->gws_size = job->gws_size;
837b843c749SSergey Zigachev 		id->oa_base = job->oa_base;
838b843c749SSergey Zigachev 		id->oa_size = job->oa_size;
839b843c749SSergey Zigachev 		amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
840b843c749SSergey Zigachev 					    job->gds_size, job->gws_base,
841b843c749SSergey Zigachev 					    job->gws_size, job->oa_base,
842b843c749SSergey Zigachev 					    job->oa_size);
843b843c749SSergey Zigachev 	}
844b843c749SSergey Zigachev 
845b843c749SSergey Zigachev 	if (ring->funcs->patch_cond_exec)
846b843c749SSergey Zigachev 		amdgpu_ring_patch_cond_exec(ring, patch_offset);
847b843c749SSergey Zigachev 
848b843c749SSergey Zigachev 	/* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
849b843c749SSergey Zigachev 	if (ring->funcs->emit_switch_buffer) {
850b843c749SSergey Zigachev 		amdgpu_ring_emit_switch_buffer(ring);
851b843c749SSergey Zigachev 		amdgpu_ring_emit_switch_buffer(ring);
852b843c749SSergey Zigachev 	}
853b843c749SSergey Zigachev 	return 0;
854b843c749SSergey Zigachev }
855b843c749SSergey Zigachev 
856b843c749SSergey Zigachev /**
857b843c749SSergey Zigachev  * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
858b843c749SSergey Zigachev  *
859b843c749SSergey Zigachev  * @vm: requested vm
860b843c749SSergey Zigachev  * @bo: requested buffer object
861b843c749SSergey Zigachev  *
862b843c749SSergey Zigachev  * Find @bo inside the requested vm.
863b843c749SSergey Zigachev  * Search inside the @bos vm list for the requested vm
864b843c749SSergey Zigachev  * Returns the found bo_va or NULL if none is found
865b843c749SSergey Zigachev  *
866b843c749SSergey Zigachev  * Object has to be reserved!
867b843c749SSergey Zigachev  *
868b843c749SSergey Zigachev  * Returns:
869b843c749SSergey Zigachev  * Found bo_va or NULL.
870b843c749SSergey Zigachev  */
amdgpu_vm_bo_find(struct amdgpu_vm * vm,struct amdgpu_bo * bo)871b843c749SSergey Zigachev struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
872b843c749SSergey Zigachev 				       struct amdgpu_bo *bo)
873b843c749SSergey Zigachev {
874b843c749SSergey Zigachev 	struct amdgpu_bo_va *bo_va;
875b843c749SSergey Zigachev 
876b843c749SSergey Zigachev 	list_for_each_entry(bo_va, &bo->va, base.bo_list) {
877b843c749SSergey Zigachev 		if (bo_va->base.vm == vm) {
878b843c749SSergey Zigachev 			return bo_va;
879b843c749SSergey Zigachev 		}
880b843c749SSergey Zigachev 	}
881b843c749SSergey Zigachev 	return NULL;
882b843c749SSergey Zigachev }
883b843c749SSergey Zigachev 
884b843c749SSergey Zigachev /**
885b843c749SSergey Zigachev  * amdgpu_vm_do_set_ptes - helper to call the right asic function
886b843c749SSergey Zigachev  *
887b843c749SSergey Zigachev  * @params: see amdgpu_pte_update_params definition
888b843c749SSergey Zigachev  * @bo: PD/PT to update
889b843c749SSergey Zigachev  * @pe: addr of the page entry
890b843c749SSergey Zigachev  * @addr: dst addr to write into pe
891b843c749SSergey Zigachev  * @count: number of page entries to update
892b843c749SSergey Zigachev  * @incr: increase next addr by incr bytes
893b843c749SSergey Zigachev  * @flags: hw access flags
894b843c749SSergey Zigachev  *
895b843c749SSergey Zigachev  * Traces the parameters and calls the right asic functions
896b843c749SSergey Zigachev  * to setup the page table using the DMA.
897b843c749SSergey Zigachev  */
amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params * params,struct amdgpu_bo * bo,uint64_t pe,uint64_t addr,unsigned count,uint32_t incr,uint64_t flags)898b843c749SSergey Zigachev static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
899b843c749SSergey Zigachev 				  struct amdgpu_bo *bo,
900b843c749SSergey Zigachev 				  uint64_t pe, uint64_t addr,
901b843c749SSergey Zigachev 				  unsigned count, uint32_t incr,
902b843c749SSergey Zigachev 				  uint64_t flags)
903b843c749SSergey Zigachev {
904b843c749SSergey Zigachev 	pe += amdgpu_bo_gpu_offset(bo);
905b843c749SSergey Zigachev 	trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
906b843c749SSergey Zigachev 
907b843c749SSergey Zigachev 	if (count < 3) {
908b843c749SSergey Zigachev 		amdgpu_vm_write_pte(params->adev, params->ib, pe,
909b843c749SSergey Zigachev 				    addr | flags, count, incr);
910b843c749SSergey Zigachev 
911b843c749SSergey Zigachev 	} else {
912b843c749SSergey Zigachev 		amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
913b843c749SSergey Zigachev 				      count, incr, flags);
914b843c749SSergey Zigachev 	}
915b843c749SSergey Zigachev }
916b843c749SSergey Zigachev 
917b843c749SSergey Zigachev /**
918b843c749SSergey Zigachev  * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
919b843c749SSergey Zigachev  *
920b843c749SSergey Zigachev  * @params: see amdgpu_pte_update_params definition
921b843c749SSergey Zigachev  * @bo: PD/PT to update
922b843c749SSergey Zigachev  * @pe: addr of the page entry
923b843c749SSergey Zigachev  * @addr: dst addr to write into pe
924b843c749SSergey Zigachev  * @count: number of page entries to update
925b843c749SSergey Zigachev  * @incr: increase next addr by incr bytes
926b843c749SSergey Zigachev  * @flags: hw access flags
927b843c749SSergey Zigachev  *
928b843c749SSergey Zigachev  * Traces the parameters and calls the DMA function to copy the PTEs.
929b843c749SSergey Zigachev  */
amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params * params,struct amdgpu_bo * bo,uint64_t pe,uint64_t addr,unsigned count,uint32_t incr,uint64_t flags)930b843c749SSergey Zigachev static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
931b843c749SSergey Zigachev 				   struct amdgpu_bo *bo,
932b843c749SSergey Zigachev 				   uint64_t pe, uint64_t addr,
933b843c749SSergey Zigachev 				   unsigned count, uint32_t incr,
934b843c749SSergey Zigachev 				   uint64_t flags)
935b843c749SSergey Zigachev {
936b843c749SSergey Zigachev 	uint64_t src = (params->src + (addr >> 12) * 8);
937b843c749SSergey Zigachev 
938b843c749SSergey Zigachev 	pe += amdgpu_bo_gpu_offset(bo);
939b843c749SSergey Zigachev 	trace_amdgpu_vm_copy_ptes(pe, src, count);
940b843c749SSergey Zigachev 
941b843c749SSergey Zigachev 	amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
942b843c749SSergey Zigachev }
943b843c749SSergey Zigachev 
944b843c749SSergey Zigachev /**
945b843c749SSergey Zigachev  * amdgpu_vm_map_gart - Resolve gart mapping of addr
946b843c749SSergey Zigachev  *
947b843c749SSergey Zigachev  * @pages_addr: optional DMA address to use for lookup
948b843c749SSergey Zigachev  * @addr: the unmapped addr
949b843c749SSergey Zigachev  *
950b843c749SSergey Zigachev  * Look up the physical address of the page that the pte resolves
951b843c749SSergey Zigachev  * to.
952b843c749SSergey Zigachev  *
953b843c749SSergey Zigachev  * Returns:
954b843c749SSergey Zigachev  * The pointer for the page table entry.
955b843c749SSergey Zigachev  */
amdgpu_vm_map_gart(const dma_addr_t * pages_addr,uint64_t addr)956b843c749SSergey Zigachev static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
957b843c749SSergey Zigachev {
958b843c749SSergey Zigachev 	uint64_t result;
959b843c749SSergey Zigachev 
960b843c749SSergey Zigachev 	/* page table offset */
961b843c749SSergey Zigachev 	result = pages_addr[addr >> PAGE_SHIFT];
962b843c749SSergey Zigachev 
963b843c749SSergey Zigachev 	/* in case cpu page size != gpu page size*/
964b843c749SSergey Zigachev 	result |= addr & (~PAGE_MASK);
965b843c749SSergey Zigachev 
966b843c749SSergey Zigachev 	result &= 0xFFFFFFFFFFFFF000ULL;
967b843c749SSergey Zigachev 
968b843c749SSergey Zigachev 	return result;
969b843c749SSergey Zigachev }
970b843c749SSergey Zigachev 
971b843c749SSergey Zigachev /**
972b843c749SSergey Zigachev  * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
973b843c749SSergey Zigachev  *
974b843c749SSergey Zigachev  * @params: see amdgpu_pte_update_params definition
975b843c749SSergey Zigachev  * @bo: PD/PT to update
976b843c749SSergey Zigachev  * @pe: kmap addr of the page entry
977b843c749SSergey Zigachev  * @addr: dst addr to write into pe
978b843c749SSergey Zigachev  * @count: number of page entries to update
979b843c749SSergey Zigachev  * @incr: increase next addr by incr bytes
980b843c749SSergey Zigachev  * @flags: hw access flags
981b843c749SSergey Zigachev  *
982b843c749SSergey Zigachev  * Write count number of PT/PD entries directly.
983b843c749SSergey Zigachev  */
amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params * params,struct amdgpu_bo * bo,uint64_t pe,uint64_t addr,unsigned count,uint32_t incr,uint64_t flags)984b843c749SSergey Zigachev static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
985b843c749SSergey Zigachev 				   struct amdgpu_bo *bo,
986b843c749SSergey Zigachev 				   uint64_t pe, uint64_t addr,
987b843c749SSergey Zigachev 				   unsigned count, uint32_t incr,
988b843c749SSergey Zigachev 				   uint64_t flags)
989b843c749SSergey Zigachev {
990b843c749SSergey Zigachev 	unsigned int i;
991b843c749SSergey Zigachev 	uint64_t value;
992b843c749SSergey Zigachev 
993b843c749SSergey Zigachev 	pe += (unsigned long)amdgpu_bo_kptr(bo);
994b843c749SSergey Zigachev 
995b843c749SSergey Zigachev 	trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
996b843c749SSergey Zigachev 
997b843c749SSergey Zigachev 	for (i = 0; i < count; i++) {
998b843c749SSergey Zigachev 		value = params->pages_addr ?
999b843c749SSergey Zigachev 			amdgpu_vm_map_gart(params->pages_addr, addr) :
1000b843c749SSergey Zigachev 			addr;
1001b843c749SSergey Zigachev 		amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
1002b843c749SSergey Zigachev 				       i, value, flags);
1003b843c749SSergey Zigachev 		addr += incr;
1004b843c749SSergey Zigachev 	}
1005b843c749SSergey Zigachev }
1006b843c749SSergey Zigachev 
1007b843c749SSergey Zigachev 
1008b843c749SSergey Zigachev /**
1009b843c749SSergey Zigachev  * amdgpu_vm_wait_pd - Wait for PT BOs to be free.
1010b843c749SSergey Zigachev  *
1011b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1012b843c749SSergey Zigachev  * @vm: related vm
1013b843c749SSergey Zigachev  * @owner: fence owner
1014b843c749SSergey Zigachev  *
1015b843c749SSergey Zigachev  * Returns:
1016b843c749SSergey Zigachev  * 0 on success, errno otherwise.
1017b843c749SSergey Zigachev  */
amdgpu_vm_wait_pd(struct amdgpu_device * adev,struct amdgpu_vm * vm,void * owner)1018b843c749SSergey Zigachev static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1019b843c749SSergey Zigachev 			     void *owner)
1020b843c749SSergey Zigachev {
1021b843c749SSergey Zigachev 	struct amdgpu_sync sync;
1022b843c749SSergey Zigachev 	int r;
1023b843c749SSergey Zigachev 
1024b843c749SSergey Zigachev 	amdgpu_sync_create(&sync);
1025b843c749SSergey Zigachev 	amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
1026b843c749SSergey Zigachev 	r = amdgpu_sync_wait(&sync, true);
1027b843c749SSergey Zigachev 	amdgpu_sync_free(&sync);
1028b843c749SSergey Zigachev 
1029b843c749SSergey Zigachev 	return r;
1030b843c749SSergey Zigachev }
1031b843c749SSergey Zigachev 
1032b843c749SSergey Zigachev /*
1033b843c749SSergey Zigachev  * amdgpu_vm_update_pde - update a single level in the hierarchy
1034b843c749SSergey Zigachev  *
1035b843c749SSergey Zigachev  * @param: parameters for the update
1036b843c749SSergey Zigachev  * @vm: requested vm
1037b843c749SSergey Zigachev  * @parent: parent directory
1038b843c749SSergey Zigachev  * @entry: entry to update
1039b843c749SSergey Zigachev  *
1040b843c749SSergey Zigachev  * Makes sure the requested entry in parent is up to date.
1041b843c749SSergey Zigachev  */
amdgpu_vm_update_pde(struct amdgpu_pte_update_params * params,struct amdgpu_vm * vm,struct amdgpu_vm_pt * parent,struct amdgpu_vm_pt * entry)1042b843c749SSergey Zigachev static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
1043b843c749SSergey Zigachev 				 struct amdgpu_vm *vm,
1044b843c749SSergey Zigachev 				 struct amdgpu_vm_pt *parent,
1045b843c749SSergey Zigachev 				 struct amdgpu_vm_pt *entry)
1046b843c749SSergey Zigachev {
1047b843c749SSergey Zigachev 	struct amdgpu_bo *bo = parent->base.bo, *pbo;
1048b843c749SSergey Zigachev 	uint64_t pde, pt, flags;
1049b843c749SSergey Zigachev 	unsigned level;
1050b843c749SSergey Zigachev 
1051b843c749SSergey Zigachev 	/* Don't update huge pages here */
1052b843c749SSergey Zigachev 	if (entry->huge)
1053b843c749SSergey Zigachev 		return;
1054b843c749SSergey Zigachev 
1055b843c749SSergey Zigachev 	for (level = 0, pbo = bo->parent; pbo; ++level)
1056b843c749SSergey Zigachev 		pbo = pbo->parent;
1057b843c749SSergey Zigachev 
1058b843c749SSergey Zigachev 	level += params->adev->vm_manager.root_level;
1059b843c749SSergey Zigachev 	pt = amdgpu_bo_gpu_offset(entry->base.bo);
1060b843c749SSergey Zigachev 	flags = AMDGPU_PTE_VALID;
1061b843c749SSergey Zigachev 	amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
1062b843c749SSergey Zigachev 	pde = (entry - parent->entries) * 8;
1063b843c749SSergey Zigachev 	if (bo->shadow)
1064b843c749SSergey Zigachev 		params->func(params, bo->shadow, pde, pt, 1, 0, flags);
1065b843c749SSergey Zigachev 	params->func(params, bo, pde, pt, 1, 0, flags);
1066b843c749SSergey Zigachev }
1067b843c749SSergey Zigachev 
1068b843c749SSergey Zigachev /*
1069b843c749SSergey Zigachev  * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1070b843c749SSergey Zigachev  *
1071b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1072b843c749SSergey Zigachev  * @vm: related vm
1073b843c749SSergey Zigachev  * @parent: parent PD
1074b843c749SSergey Zigachev  * @level: VMPT level
1075b843c749SSergey Zigachev  *
1076b843c749SSergey Zigachev  * Mark all PD level as invalid after an error.
1077b843c749SSergey Zigachev  */
amdgpu_vm_invalidate_level(struct amdgpu_device * adev,struct amdgpu_vm * vm,struct amdgpu_vm_pt * parent,unsigned level)1078b843c749SSergey Zigachev static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
1079b843c749SSergey Zigachev 				       struct amdgpu_vm *vm,
1080b843c749SSergey Zigachev 				       struct amdgpu_vm_pt *parent,
1081b843c749SSergey Zigachev 				       unsigned level)
1082b843c749SSergey Zigachev {
1083b843c749SSergey Zigachev 	unsigned pt_idx, num_entries;
1084b843c749SSergey Zigachev 
1085b843c749SSergey Zigachev 	/*
1086b843c749SSergey Zigachev 	 * Recurse into the subdirectories. This recursion is harmless because
1087b843c749SSergey Zigachev 	 * we only have a maximum of 5 layers.
1088b843c749SSergey Zigachev 	 */
1089b843c749SSergey Zigachev 	num_entries = amdgpu_vm_num_entries(adev, level);
1090b843c749SSergey Zigachev 	for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
1091b843c749SSergey Zigachev 		struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1092b843c749SSergey Zigachev 
1093b843c749SSergey Zigachev 		if (!entry->base.bo)
1094b843c749SSergey Zigachev 			continue;
1095b843c749SSergey Zigachev 
1096b843c749SSergey Zigachev 		if (!entry->base.moved)
1097b843c749SSergey Zigachev 			list_move(&entry->base.vm_status, &vm->relocated);
1098b843c749SSergey Zigachev 		amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
1099b843c749SSergey Zigachev 	}
1100b843c749SSergey Zigachev }
1101b843c749SSergey Zigachev 
1102b843c749SSergey Zigachev /*
1103b843c749SSergey Zigachev  * amdgpu_vm_update_directories - make sure that all directories are valid
1104b843c749SSergey Zigachev  *
1105b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1106b843c749SSergey Zigachev  * @vm: requested vm
1107b843c749SSergey Zigachev  *
1108b843c749SSergey Zigachev  * Makes sure all directories are up to date.
1109b843c749SSergey Zigachev  *
1110b843c749SSergey Zigachev  * Returns:
1111b843c749SSergey Zigachev  * 0 for success, error for failure.
1112b843c749SSergey Zigachev  */
amdgpu_vm_update_directories(struct amdgpu_device * adev,struct amdgpu_vm * vm)1113b843c749SSergey Zigachev int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1114b843c749SSergey Zigachev 				 struct amdgpu_vm *vm)
1115b843c749SSergey Zigachev {
1116b843c749SSergey Zigachev 	struct amdgpu_pte_update_params params;
1117b843c749SSergey Zigachev 	struct amdgpu_job *job;
1118b843c749SSergey Zigachev 	unsigned ndw = 0;
1119b843c749SSergey Zigachev 	int r = 0;
1120b843c749SSergey Zigachev 
1121b843c749SSergey Zigachev 	if (list_empty(&vm->relocated))
1122b843c749SSergey Zigachev 		return 0;
1123b843c749SSergey Zigachev 
1124b843c749SSergey Zigachev restart:
1125b843c749SSergey Zigachev 	memset(&params, 0, sizeof(params));
1126b843c749SSergey Zigachev 	params.adev = adev;
1127b843c749SSergey Zigachev 
1128b843c749SSergey Zigachev 	if (vm->use_cpu_for_update) {
1129b843c749SSergey Zigachev 		struct amdgpu_vm_bo_base *bo_base;
1130b843c749SSergey Zigachev 
1131b843c749SSergey Zigachev 		list_for_each_entry(bo_base, &vm->relocated, vm_status) {
1132b843c749SSergey Zigachev 			r = amdgpu_bo_kmap(bo_base->bo, NULL);
1133b843c749SSergey Zigachev 			if (unlikely(r))
1134b843c749SSergey Zigachev 				return r;
1135b843c749SSergey Zigachev 		}
1136b843c749SSergey Zigachev 
1137b843c749SSergey Zigachev 		r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
1138b843c749SSergey Zigachev 		if (unlikely(r))
1139b843c749SSergey Zigachev 			return r;
1140b843c749SSergey Zigachev 
1141b843c749SSergey Zigachev 		params.func = amdgpu_vm_cpu_set_ptes;
1142b843c749SSergey Zigachev 	} else {
1143b843c749SSergey Zigachev 		ndw = 512 * 8;
1144b843c749SSergey Zigachev 		r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1145b843c749SSergey Zigachev 		if (r)
1146b843c749SSergey Zigachev 			return r;
1147b843c749SSergey Zigachev 
1148b843c749SSergey Zigachev 		params.ib = &job->ibs[0];
1149b843c749SSergey Zigachev 		params.func = amdgpu_vm_do_set_ptes;
1150b843c749SSergey Zigachev 	}
1151b843c749SSergey Zigachev 
1152b843c749SSergey Zigachev 	while (!list_empty(&vm->relocated)) {
1153b843c749SSergey Zigachev 		struct amdgpu_vm_bo_base *bo_base, *parent;
1154b843c749SSergey Zigachev 		struct amdgpu_vm_pt *pt, *entry;
1155b843c749SSergey Zigachev 		struct amdgpu_bo *bo;
1156b843c749SSergey Zigachev 
1157b843c749SSergey Zigachev 		bo_base = list_first_entry(&vm->relocated,
1158b843c749SSergey Zigachev 					   struct amdgpu_vm_bo_base,
1159b843c749SSergey Zigachev 					   vm_status);
1160b843c749SSergey Zigachev 		bo_base->moved = false;
1161b843c749SSergey Zigachev 		list_del_init(&bo_base->vm_status);
1162b843c749SSergey Zigachev 
1163b843c749SSergey Zigachev 		bo = bo_base->bo->parent;
1164b843c749SSergey Zigachev 		if (!bo)
1165b843c749SSergey Zigachev 			continue;
1166b843c749SSergey Zigachev 
1167b843c749SSergey Zigachev 		parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
1168b843c749SSergey Zigachev 					  bo_list);
1169b843c749SSergey Zigachev 		pt = container_of(parent, struct amdgpu_vm_pt, base);
1170b843c749SSergey Zigachev 		entry = container_of(bo_base, struct amdgpu_vm_pt, base);
1171b843c749SSergey Zigachev 
1172b843c749SSergey Zigachev 		amdgpu_vm_update_pde(&params, vm, pt, entry);
1173b843c749SSergey Zigachev 
1174b843c749SSergey Zigachev 		if (!vm->use_cpu_for_update &&
1175b843c749SSergey Zigachev 		    (ndw - params.ib->length_dw) < 32)
1176b843c749SSergey Zigachev 			break;
1177b843c749SSergey Zigachev 	}
1178b843c749SSergey Zigachev 
1179b843c749SSergey Zigachev 	if (vm->use_cpu_for_update) {
1180b843c749SSergey Zigachev 		/* Flush HDP */
1181b843c749SSergey Zigachev 		mb();
1182b843c749SSergey Zigachev 		amdgpu_asic_flush_hdp(adev, NULL);
1183b843c749SSergey Zigachev 	} else if (params.ib->length_dw == 0) {
1184b843c749SSergey Zigachev 		amdgpu_job_free(job);
1185b843c749SSergey Zigachev 	} else {
1186b843c749SSergey Zigachev 		struct amdgpu_bo *root = vm->root.base.bo;
1187b843c749SSergey Zigachev 		struct amdgpu_ring *ring;
1188b843c749SSergey Zigachev 		struct dma_fence *fence;
1189b843c749SSergey Zigachev 
1190b843c749SSergey Zigachev 		ring = container_of(vm->entity.rq->sched, struct amdgpu_ring,
1191b843c749SSergey Zigachev 				    sched);
1192b843c749SSergey Zigachev 
1193b843c749SSergey Zigachev 		amdgpu_ring_pad_ib(ring, params.ib);
1194b843c749SSergey Zigachev 		amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
1195b843c749SSergey Zigachev 				 AMDGPU_FENCE_OWNER_VM, false);
1196b843c749SSergey Zigachev 		WARN_ON(params.ib->length_dw > ndw);
1197b843c749SSergey Zigachev 		r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_VM,
1198b843c749SSergey Zigachev 				      &fence);
1199b843c749SSergey Zigachev 		if (r)
1200b843c749SSergey Zigachev 			goto error;
1201b843c749SSergey Zigachev 
1202b843c749SSergey Zigachev 		amdgpu_bo_fence(root, fence, true);
1203b843c749SSergey Zigachev 		dma_fence_put(vm->last_update);
1204b843c749SSergey Zigachev 		vm->last_update = fence;
1205b843c749SSergey Zigachev 	}
1206b843c749SSergey Zigachev 
1207b843c749SSergey Zigachev 	if (!list_empty(&vm->relocated))
1208b843c749SSergey Zigachev 		goto restart;
1209b843c749SSergey Zigachev 
1210b843c749SSergey Zigachev 	return 0;
1211b843c749SSergey Zigachev 
1212b843c749SSergey Zigachev error:
1213b843c749SSergey Zigachev 	amdgpu_vm_invalidate_level(adev, vm, &vm->root,
1214b843c749SSergey Zigachev 				   adev->vm_manager.root_level);
1215b843c749SSergey Zigachev 	amdgpu_job_free(job);
1216b843c749SSergey Zigachev 	return r;
1217b843c749SSergey Zigachev }
1218b843c749SSergey Zigachev 
1219b843c749SSergey Zigachev /**
1220b843c749SSergey Zigachev  * amdgpu_vm_find_entry - find the entry for an address
1221b843c749SSergey Zigachev  *
1222b843c749SSergey Zigachev  * @p: see amdgpu_pte_update_params definition
1223b843c749SSergey Zigachev  * @addr: virtual address in question
1224b843c749SSergey Zigachev  * @entry: resulting entry or NULL
1225b843c749SSergey Zigachev  * @parent: parent entry
1226b843c749SSergey Zigachev  *
1227b843c749SSergey Zigachev  * Find the vm_pt entry and it's parent for the given address.
1228b843c749SSergey Zigachev  */
1229b843c749SSergey Zigachev void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1230b843c749SSergey Zigachev 			 struct amdgpu_vm_pt **entry,
123178973132SSergey Zigachev 			 struct amdgpu_vm_pt **parent);
amdgpu_vm_get_entry(struct amdgpu_pte_update_params * p,uint64_t addr,struct amdgpu_vm_pt ** entry,struct amdgpu_vm_pt ** parent)123278973132SSergey Zigachev void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
123378973132SSergey Zigachev 			 struct amdgpu_vm_pt **entry,
1234b843c749SSergey Zigachev 			 struct amdgpu_vm_pt **parent)
1235b843c749SSergey Zigachev {
1236b843c749SSergey Zigachev 	unsigned level = p->adev->vm_manager.root_level;
1237b843c749SSergey Zigachev 
1238b843c749SSergey Zigachev 	*parent = NULL;
1239b843c749SSergey Zigachev 	*entry = &p->vm->root;
1240b843c749SSergey Zigachev 	while ((*entry)->entries) {
1241b843c749SSergey Zigachev 		unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
1242b843c749SSergey Zigachev 
1243b843c749SSergey Zigachev 		*parent = *entry;
1244b843c749SSergey Zigachev 		*entry = &(*entry)->entries[addr >> shift];
1245b843c749SSergey Zigachev 		addr &= (1ULL << shift) - 1;
1246b843c749SSergey Zigachev 	}
1247b843c749SSergey Zigachev 
1248b843c749SSergey Zigachev 	if (level != AMDGPU_VM_PTB)
1249b843c749SSergey Zigachev 		*entry = NULL;
1250b843c749SSergey Zigachev }
1251b843c749SSergey Zigachev 
1252b843c749SSergey Zigachev /**
1253b843c749SSergey Zigachev  * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1254b843c749SSergey Zigachev  *
1255b843c749SSergey Zigachev  * @p: see amdgpu_pte_update_params definition
1256b843c749SSergey Zigachev  * @entry: vm_pt entry to check
1257b843c749SSergey Zigachev  * @parent: parent entry
1258b843c749SSergey Zigachev  * @nptes: number of PTEs updated with this operation
1259b843c749SSergey Zigachev  * @dst: destination address where the PTEs should point to
1260b843c749SSergey Zigachev  * @flags: access flags fro the PTEs
1261b843c749SSergey Zigachev  *
1262b843c749SSergey Zigachev  * Check if we can update the PD with a huge page.
1263b843c749SSergey Zigachev  */
amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params * p,struct amdgpu_vm_pt * entry,struct amdgpu_vm_pt * parent,unsigned nptes,uint64_t dst,uint64_t flags)1264b843c749SSergey Zigachev static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1265b843c749SSergey Zigachev 					struct amdgpu_vm_pt *entry,
1266b843c749SSergey Zigachev 					struct amdgpu_vm_pt *parent,
1267b843c749SSergey Zigachev 					unsigned nptes, uint64_t dst,
1268b843c749SSergey Zigachev 					uint64_t flags)
1269b843c749SSergey Zigachev {
1270b843c749SSergey Zigachev 	uint64_t pde;
1271b843c749SSergey Zigachev 
1272b843c749SSergey Zigachev 	/* In the case of a mixed PT the PDE must point to it*/
1273b843c749SSergey Zigachev 	if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
1274b843c749SSergey Zigachev 	    nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
1275b843c749SSergey Zigachev 		/* Set the huge page flag to stop scanning at this PDE */
1276b843c749SSergey Zigachev 		flags |= AMDGPU_PDE_PTE;
1277b843c749SSergey Zigachev 	}
1278b843c749SSergey Zigachev 
1279b843c749SSergey Zigachev 	if (!(flags & AMDGPU_PDE_PTE)) {
1280b843c749SSergey Zigachev 		if (entry->huge) {
1281b843c749SSergey Zigachev 			/* Add the entry to the relocated list to update it. */
1282b843c749SSergey Zigachev 			entry->huge = false;
1283b843c749SSergey Zigachev 			list_move(&entry->base.vm_status, &p->vm->relocated);
1284b843c749SSergey Zigachev 		}
1285b843c749SSergey Zigachev 		return;
1286b843c749SSergey Zigachev 	}
1287b843c749SSergey Zigachev 
1288b843c749SSergey Zigachev 	entry->huge = true;
1289b843c749SSergey Zigachev 	amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
1290b843c749SSergey Zigachev 
1291b843c749SSergey Zigachev 	pde = (entry - parent->entries) * 8;
1292b843c749SSergey Zigachev 	if (parent->base.bo->shadow)
1293b843c749SSergey Zigachev 		p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
1294b843c749SSergey Zigachev 	p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
1295b843c749SSergey Zigachev }
1296b843c749SSergey Zigachev 
1297b843c749SSergey Zigachev /**
1298b843c749SSergey Zigachev  * amdgpu_vm_update_ptes - make sure that page tables are valid
1299b843c749SSergey Zigachev  *
1300b843c749SSergey Zigachev  * @params: see amdgpu_pte_update_params definition
1301b843c749SSergey Zigachev  * @start: start of GPU address range
1302b843c749SSergey Zigachev  * @end: end of GPU address range
1303b843c749SSergey Zigachev  * @dst: destination address to map to, the next dst inside the function
1304b843c749SSergey Zigachev  * @flags: mapping flags
1305b843c749SSergey Zigachev  *
1306b843c749SSergey Zigachev  * Update the page tables in the range @start - @end.
1307b843c749SSergey Zigachev  *
1308b843c749SSergey Zigachev  * Returns:
1309b843c749SSergey Zigachev  * 0 for success, -EINVAL for failure.
1310b843c749SSergey Zigachev  */
amdgpu_vm_update_ptes(struct amdgpu_pte_update_params * params,uint64_t start,uint64_t end,uint64_t dst,uint64_t flags)1311b843c749SSergey Zigachev static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
1312b843c749SSergey Zigachev 				  uint64_t start, uint64_t end,
1313b843c749SSergey Zigachev 				  uint64_t dst, uint64_t flags)
1314b843c749SSergey Zigachev {
1315b843c749SSergey Zigachev 	struct amdgpu_device *adev = params->adev;
1316b843c749SSergey Zigachev 	const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
1317b843c749SSergey Zigachev 
1318b843c749SSergey Zigachev 	uint64_t addr, pe_start;
1319b843c749SSergey Zigachev 	struct amdgpu_bo *pt;
1320b843c749SSergey Zigachev 	unsigned nptes;
1321b843c749SSergey Zigachev 
1322b843c749SSergey Zigachev 	/* walk over the address space and update the page tables */
1323b843c749SSergey Zigachev 	for (addr = start; addr < end; addr += nptes,
1324b843c749SSergey Zigachev 	     dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1325b843c749SSergey Zigachev 		struct amdgpu_vm_pt *entry, *parent;
1326b843c749SSergey Zigachev 
1327b843c749SSergey Zigachev 		amdgpu_vm_get_entry(params, addr, &entry, &parent);
1328b843c749SSergey Zigachev 		if (!entry)
1329b843c749SSergey Zigachev 			return -ENOENT;
1330b843c749SSergey Zigachev 
1331b843c749SSergey Zigachev 		if ((addr & ~mask) == (end & ~mask))
1332b843c749SSergey Zigachev 			nptes = end - addr;
1333b843c749SSergey Zigachev 		else
1334b843c749SSergey Zigachev 			nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
1335b843c749SSergey Zigachev 
1336b843c749SSergey Zigachev 		amdgpu_vm_handle_huge_pages(params, entry, parent,
1337b843c749SSergey Zigachev 					    nptes, dst, flags);
1338b843c749SSergey Zigachev 		/* We don't need to update PTEs for huge pages */
1339b843c749SSergey Zigachev 		if (entry->huge)
1340b843c749SSergey Zigachev 			continue;
1341b843c749SSergey Zigachev 
1342b843c749SSergey Zigachev 		pt = entry->base.bo;
1343b843c749SSergey Zigachev 		pe_start = (addr & mask) * 8;
1344b843c749SSergey Zigachev 		if (pt->shadow)
1345b843c749SSergey Zigachev 			params->func(params, pt->shadow, pe_start, dst, nptes,
1346b843c749SSergey Zigachev 				     AMDGPU_GPU_PAGE_SIZE, flags);
1347b843c749SSergey Zigachev 		params->func(params, pt, pe_start, dst, nptes,
1348b843c749SSergey Zigachev 			     AMDGPU_GPU_PAGE_SIZE, flags);
1349b843c749SSergey Zigachev 	}
1350b843c749SSergey Zigachev 
1351b843c749SSergey Zigachev 	return 0;
1352b843c749SSergey Zigachev }
1353b843c749SSergey Zigachev 
1354b843c749SSergey Zigachev /*
1355b843c749SSergey Zigachev  * amdgpu_vm_frag_ptes - add fragment information to PTEs
1356b843c749SSergey Zigachev  *
1357b843c749SSergey Zigachev  * @params: see amdgpu_pte_update_params definition
1358b843c749SSergey Zigachev  * @vm: requested vm
1359b843c749SSergey Zigachev  * @start: first PTE to handle
1360b843c749SSergey Zigachev  * @end: last PTE to handle
1361b843c749SSergey Zigachev  * @dst: addr those PTEs should point to
1362b843c749SSergey Zigachev  * @flags: hw mapping flags
1363b843c749SSergey Zigachev  *
1364b843c749SSergey Zigachev  * Returns:
1365b843c749SSergey Zigachev  * 0 for success, -EINVAL for failure.
1366b843c749SSergey Zigachev  */
amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params * params,uint64_t start,uint64_t end,uint64_t dst,uint64_t flags)1367b843c749SSergey Zigachev static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params	*params,
1368b843c749SSergey Zigachev 				uint64_t start, uint64_t end,
1369b843c749SSergey Zigachev 				uint64_t dst, uint64_t flags)
1370b843c749SSergey Zigachev {
1371b843c749SSergey Zigachev 	/**
1372b843c749SSergey Zigachev 	 * The MC L1 TLB supports variable sized pages, based on a fragment
1373b843c749SSergey Zigachev 	 * field in the PTE. When this field is set to a non-zero value, page
1374b843c749SSergey Zigachev 	 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1375b843c749SSergey Zigachev 	 * flags are considered valid for all PTEs within the fragment range
1376b843c749SSergey Zigachev 	 * and corresponding mappings are assumed to be physically contiguous.
1377b843c749SSergey Zigachev 	 *
1378b843c749SSergey Zigachev 	 * The L1 TLB can store a single PTE for the whole fragment,
1379b843c749SSergey Zigachev 	 * significantly increasing the space available for translation
1380b843c749SSergey Zigachev 	 * caching. This leads to large improvements in throughput when the
1381b843c749SSergey Zigachev 	 * TLB is under pressure.
1382b843c749SSergey Zigachev 	 *
1383b843c749SSergey Zigachev 	 * The L2 TLB distributes small and large fragments into two
1384b843c749SSergey Zigachev 	 * asymmetric partitions. The large fragment cache is significantly
1385b843c749SSergey Zigachev 	 * larger. Thus, we try to use large fragments wherever possible.
1386b843c749SSergey Zigachev 	 * Userspace can support this by aligning virtual base address and
1387b843c749SSergey Zigachev 	 * allocation size to the fragment size.
1388b843c749SSergey Zigachev 	 */
1389b843c749SSergey Zigachev 	unsigned max_frag = params->adev->vm_manager.fragment_size;
1390b843c749SSergey Zigachev 	int r;
1391b843c749SSergey Zigachev 
1392b843c749SSergey Zigachev 	/* system pages are non continuously */
1393b843c749SSergey Zigachev 	if (params->src || !(flags & AMDGPU_PTE_VALID))
1394b843c749SSergey Zigachev 		return amdgpu_vm_update_ptes(params, start, end, dst, flags);
1395b843c749SSergey Zigachev 
1396b843c749SSergey Zigachev 	while (start != end) {
1397b843c749SSergey Zigachev 		uint64_t frag_flags, frag_end;
1398b843c749SSergey Zigachev 		unsigned frag;
1399b843c749SSergey Zigachev 
1400b843c749SSergey Zigachev 		/* This intentionally wraps around if no bit is set */
1401b843c749SSergey Zigachev 		frag = min((unsigned)ffs(start) - 1,
1402b843c749SSergey Zigachev 			   (unsigned)fls64(end - start) - 1);
1403b843c749SSergey Zigachev 		if (frag >= max_frag) {
1404b843c749SSergey Zigachev 			frag_flags = AMDGPU_PTE_FRAG(max_frag);
1405b843c749SSergey Zigachev 			frag_end = end & ~((1ULL << max_frag) - 1);
1406b843c749SSergey Zigachev 		} else {
1407b843c749SSergey Zigachev 			frag_flags = AMDGPU_PTE_FRAG(frag);
1408b843c749SSergey Zigachev 			frag_end = start + (1 << frag);
1409b843c749SSergey Zigachev 		}
1410b843c749SSergey Zigachev 
1411b843c749SSergey Zigachev 		r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1412b843c749SSergey Zigachev 					  flags | frag_flags);
1413b843c749SSergey Zigachev 		if (r)
1414b843c749SSergey Zigachev 			return r;
1415b843c749SSergey Zigachev 
1416b843c749SSergey Zigachev 		dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1417b843c749SSergey Zigachev 		start = frag_end;
1418b843c749SSergey Zigachev 	}
1419b843c749SSergey Zigachev 
1420b843c749SSergey Zigachev 	return 0;
1421b843c749SSergey Zigachev }
1422b843c749SSergey Zigachev 
1423b843c749SSergey Zigachev /**
1424b843c749SSergey Zigachev  * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1425b843c749SSergey Zigachev  *
1426b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1427b843c749SSergey Zigachev  * @exclusive: fence we need to sync to
1428b843c749SSergey Zigachev  * @pages_addr: DMA addresses to use for mapping
1429b843c749SSergey Zigachev  * @vm: requested vm
1430b843c749SSergey Zigachev  * @start: start of mapped range
1431b843c749SSergey Zigachev  * @last: last mapped entry
1432b843c749SSergey Zigachev  * @flags: flags for the entries
1433b843c749SSergey Zigachev  * @addr: addr to set the area to
1434b843c749SSergey Zigachev  * @fence: optional resulting fence
1435b843c749SSergey Zigachev  *
1436b843c749SSergey Zigachev  * Fill in the page table entries between @start and @last.
1437b843c749SSergey Zigachev  *
1438b843c749SSergey Zigachev  * Returns:
1439b843c749SSergey Zigachev  * 0 for success, -EINVAL for failure.
1440b843c749SSergey Zigachev  */
amdgpu_vm_bo_update_mapping(struct amdgpu_device * adev,struct dma_fence * exclusive,dma_addr_t * pages_addr,struct amdgpu_vm * vm,uint64_t start,uint64_t last,uint64_t flags,uint64_t addr,struct dma_fence ** fence)1441b843c749SSergey Zigachev static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
1442b843c749SSergey Zigachev 				       struct dma_fence *exclusive,
1443b843c749SSergey Zigachev 				       dma_addr_t *pages_addr,
1444b843c749SSergey Zigachev 				       struct amdgpu_vm *vm,
1445b843c749SSergey Zigachev 				       uint64_t start, uint64_t last,
1446b843c749SSergey Zigachev 				       uint64_t flags, uint64_t addr,
1447b843c749SSergey Zigachev 				       struct dma_fence **fence)
1448b843c749SSergey Zigachev {
1449b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
1450b843c749SSergey Zigachev 	void *owner = AMDGPU_FENCE_OWNER_VM;
1451b843c749SSergey Zigachev 	unsigned nptes, ncmds, ndw;
1452b843c749SSergey Zigachev 	struct amdgpu_job *job;
1453b843c749SSergey Zigachev 	struct amdgpu_pte_update_params params;
1454b843c749SSergey Zigachev 	struct dma_fence *f = NULL;
1455b843c749SSergey Zigachev 	int r;
1456b843c749SSergey Zigachev 
1457b843c749SSergey Zigachev 	memset(&params, 0, sizeof(params));
1458b843c749SSergey Zigachev 	params.adev = adev;
1459b843c749SSergey Zigachev 	params.vm = vm;
1460b843c749SSergey Zigachev 
1461b843c749SSergey Zigachev 	/* sync to everything on unmapping */
1462b843c749SSergey Zigachev 	if (!(flags & AMDGPU_PTE_VALID))
1463b843c749SSergey Zigachev 		owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1464b843c749SSergey Zigachev 
1465b843c749SSergey Zigachev 	if (vm->use_cpu_for_update) {
1466b843c749SSergey Zigachev 		/* params.src is used as flag to indicate system Memory */
1467b843c749SSergey Zigachev 		if (pages_addr)
1468b843c749SSergey Zigachev 			params.src = ~0;
1469b843c749SSergey Zigachev 
1470b843c749SSergey Zigachev 		/* Wait for PT BOs to be free. PTs share the same resv. object
1471b843c749SSergey Zigachev 		 * as the root PD BO
1472b843c749SSergey Zigachev 		 */
1473b843c749SSergey Zigachev 		r = amdgpu_vm_wait_pd(adev, vm, owner);
1474b843c749SSergey Zigachev 		if (unlikely(r))
1475b843c749SSergey Zigachev 			return r;
1476b843c749SSergey Zigachev 
1477b843c749SSergey Zigachev 		params.func = amdgpu_vm_cpu_set_ptes;
1478b843c749SSergey Zigachev 		params.pages_addr = pages_addr;
1479b843c749SSergey Zigachev 		return amdgpu_vm_frag_ptes(&params, start, last + 1,
1480b843c749SSergey Zigachev 					   addr, flags);
1481b843c749SSergey Zigachev 	}
1482b843c749SSergey Zigachev 
1483b843c749SSergey Zigachev 	ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
1484b843c749SSergey Zigachev 
1485b843c749SSergey Zigachev 	nptes = last - start + 1;
1486b843c749SSergey Zigachev 
1487b843c749SSergey Zigachev 	/*
1488b843c749SSergey Zigachev 	 * reserve space for two commands every (1 << BLOCK_SIZE)
1489b843c749SSergey Zigachev 	 *  entries or 2k dwords (whatever is smaller)
1490b843c749SSergey Zigachev          *
1491b843c749SSergey Zigachev          * The second command is for the shadow pagetables.
1492b843c749SSergey Zigachev 	 */
1493b843c749SSergey Zigachev 	if (vm->root.base.bo->shadow)
1494b843c749SSergey Zigachev 		ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1495b843c749SSergey Zigachev 	else
1496b843c749SSergey Zigachev 		ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
1497b843c749SSergey Zigachev 
1498b843c749SSergey Zigachev 	/* padding, etc. */
1499b843c749SSergey Zigachev 	ndw = 64;
1500b843c749SSergey Zigachev 
1501b843c749SSergey Zigachev 	if (pages_addr) {
1502b843c749SSergey Zigachev 		/* copy commands needed */
1503b843c749SSergey Zigachev 		ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
1504b843c749SSergey Zigachev 
1505b843c749SSergey Zigachev 		/* and also PTEs */
1506b843c749SSergey Zigachev 		ndw += nptes * 2;
1507b843c749SSergey Zigachev 
1508b843c749SSergey Zigachev 		params.func = amdgpu_vm_do_copy_ptes;
1509b843c749SSergey Zigachev 
1510b843c749SSergey Zigachev 	} else {
1511b843c749SSergey Zigachev 		/* set page commands needed */
1512b843c749SSergey Zigachev 		ndw += ncmds * 10;
1513b843c749SSergey Zigachev 
1514b843c749SSergey Zigachev 		/* extra commands for begin/end fragments */
1515b843c749SSergey Zigachev 		if (vm->root.base.bo->shadow)
1516b843c749SSergey Zigachev 		        ndw += 2 * 10 * adev->vm_manager.fragment_size * 2;
1517b843c749SSergey Zigachev 		else
1518b843c749SSergey Zigachev 		        ndw += 2 * 10 * adev->vm_manager.fragment_size;
1519b843c749SSergey Zigachev 
1520b843c749SSergey Zigachev 		params.func = amdgpu_vm_do_set_ptes;
1521b843c749SSergey Zigachev 	}
1522b843c749SSergey Zigachev 
1523b843c749SSergey Zigachev 	r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1524b843c749SSergey Zigachev 	if (r)
1525b843c749SSergey Zigachev 		return r;
1526b843c749SSergey Zigachev 
1527b843c749SSergey Zigachev 	params.ib = &job->ibs[0];
1528b843c749SSergey Zigachev 
1529b843c749SSergey Zigachev 	if (pages_addr) {
1530b843c749SSergey Zigachev 		uint64_t *pte;
1531b843c749SSergey Zigachev 		unsigned i;
1532b843c749SSergey Zigachev 
1533b843c749SSergey Zigachev 		/* Put the PTEs at the end of the IB. */
1534b843c749SSergey Zigachev 		i = ndw - nptes * 2;
1535b843c749SSergey Zigachev 		pte= (uint64_t *)&(job->ibs->ptr[i]);
1536b843c749SSergey Zigachev 		params.src = job->ibs->gpu_addr + i * 4;
1537b843c749SSergey Zigachev 
1538b843c749SSergey Zigachev 		for (i = 0; i < nptes; ++i) {
1539b843c749SSergey Zigachev 			pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1540b843c749SSergey Zigachev 						    AMDGPU_GPU_PAGE_SIZE);
1541b843c749SSergey Zigachev 			pte[i] |= flags;
1542b843c749SSergey Zigachev 		}
1543b843c749SSergey Zigachev 		addr = 0;
1544b843c749SSergey Zigachev 	}
1545b843c749SSergey Zigachev 
1546b843c749SSergey Zigachev 	r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
1547b843c749SSergey Zigachev 	if (r)
1548b843c749SSergey Zigachev 		goto error_free;
1549b843c749SSergey Zigachev 
1550b843c749SSergey Zigachev 	r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
1551b843c749SSergey Zigachev 			     owner, false);
1552b843c749SSergey Zigachev 	if (r)
1553b843c749SSergey Zigachev 		goto error_free;
1554b843c749SSergey Zigachev 
1555b843c749SSergey Zigachev 	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
1556b843c749SSergey Zigachev 	if (r)
1557b843c749SSergey Zigachev 		goto error_free;
1558b843c749SSergey Zigachev 
1559b843c749SSergey Zigachev 	r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1560b843c749SSergey Zigachev 	if (r)
1561b843c749SSergey Zigachev 		goto error_free;
1562b843c749SSergey Zigachev 
1563b843c749SSergey Zigachev 	amdgpu_ring_pad_ib(ring, params.ib);
1564b843c749SSergey Zigachev 	WARN_ON(params.ib->length_dw > ndw);
1565b843c749SSergey Zigachev 	r = amdgpu_job_submit(job, &vm->entity, AMDGPU_FENCE_OWNER_VM, &f);
1566b843c749SSergey Zigachev 	if (r)
1567b843c749SSergey Zigachev 		goto error_free;
1568b843c749SSergey Zigachev 
1569b843c749SSergey Zigachev 	amdgpu_bo_fence(vm->root.base.bo, f, true);
1570b843c749SSergey Zigachev 	dma_fence_put(*fence);
1571b843c749SSergey Zigachev 	*fence = f;
1572b843c749SSergey Zigachev 	return 0;
1573b843c749SSergey Zigachev 
1574b843c749SSergey Zigachev error_free:
1575b843c749SSergey Zigachev 	amdgpu_job_free(job);
1576b843c749SSergey Zigachev 	return r;
1577b843c749SSergey Zigachev }
1578b843c749SSergey Zigachev 
1579b843c749SSergey Zigachev /**
1580b843c749SSergey Zigachev  * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1581b843c749SSergey Zigachev  *
1582b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1583b843c749SSergey Zigachev  * @exclusive: fence we need to sync to
1584b843c749SSergey Zigachev  * @pages_addr: DMA addresses to use for mapping
1585b843c749SSergey Zigachev  * @vm: requested vm
1586b843c749SSergey Zigachev  * @mapping: mapped range and flags to use for the update
1587b843c749SSergey Zigachev  * @flags: HW flags for the mapping
1588b843c749SSergey Zigachev  * @nodes: array of drm_mm_nodes with the MC addresses
1589b843c749SSergey Zigachev  * @fence: optional resulting fence
1590b843c749SSergey Zigachev  *
1591b843c749SSergey Zigachev  * Split the mapping into smaller chunks so that each update fits
1592b843c749SSergey Zigachev  * into a SDMA IB.
1593b843c749SSergey Zigachev  *
1594b843c749SSergey Zigachev  * Returns:
1595b843c749SSergey Zigachev  * 0 for success, -EINVAL for failure.
1596b843c749SSergey Zigachev  */
amdgpu_vm_bo_split_mapping(struct amdgpu_device * adev,struct dma_fence * exclusive,dma_addr_t * pages_addr,struct amdgpu_vm * vm,struct amdgpu_bo_va_mapping * mapping,uint64_t flags,struct drm_mm_node * nodes,struct dma_fence ** fence)1597b843c749SSergey Zigachev static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
1598b843c749SSergey Zigachev 				      struct dma_fence *exclusive,
1599b843c749SSergey Zigachev 				      dma_addr_t *pages_addr,
1600b843c749SSergey Zigachev 				      struct amdgpu_vm *vm,
1601b843c749SSergey Zigachev 				      struct amdgpu_bo_va_mapping *mapping,
1602b843c749SSergey Zigachev 				      uint64_t flags,
1603b843c749SSergey Zigachev 				      struct drm_mm_node *nodes,
1604b843c749SSergey Zigachev 				      struct dma_fence **fence)
1605b843c749SSergey Zigachev {
1606b843c749SSergey Zigachev 	unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
1607b843c749SSergey Zigachev 	uint64_t pfn, start = mapping->start;
1608b843c749SSergey Zigachev 	int r;
1609b843c749SSergey Zigachev 
1610b843c749SSergey Zigachev 	/* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1611b843c749SSergey Zigachev 	 * but in case of something, we filter the flags in first place
1612b843c749SSergey Zigachev 	 */
1613b843c749SSergey Zigachev 	if (!(mapping->flags & AMDGPU_PTE_READABLE))
1614b843c749SSergey Zigachev 		flags &= ~AMDGPU_PTE_READABLE;
1615b843c749SSergey Zigachev 	if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1616b843c749SSergey Zigachev 		flags &= ~AMDGPU_PTE_WRITEABLE;
1617b843c749SSergey Zigachev 
1618b843c749SSergey Zigachev 	flags &= ~AMDGPU_PTE_EXECUTABLE;
1619b843c749SSergey Zigachev 	flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1620b843c749SSergey Zigachev 
1621b843c749SSergey Zigachev 	flags &= ~AMDGPU_PTE_MTYPE_MASK;
1622b843c749SSergey Zigachev 	flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1623b843c749SSergey Zigachev 
1624b843c749SSergey Zigachev 	if ((mapping->flags & AMDGPU_PTE_PRT) &&
1625b843c749SSergey Zigachev 	    (adev->asic_type >= CHIP_VEGA10)) {
1626b843c749SSergey Zigachev 		flags |= AMDGPU_PTE_PRT;
1627b843c749SSergey Zigachev 		flags &= ~AMDGPU_PTE_VALID;
1628b843c749SSergey Zigachev 	}
1629b843c749SSergey Zigachev 
1630b843c749SSergey Zigachev 	trace_amdgpu_vm_bo_update(mapping);
1631b843c749SSergey Zigachev 
1632b843c749SSergey Zigachev 	pfn = mapping->offset >> PAGE_SHIFT;
1633b843c749SSergey Zigachev 	if (nodes) {
1634b843c749SSergey Zigachev 		while (pfn >= nodes->size) {
1635b843c749SSergey Zigachev 			pfn -= nodes->size;
1636b843c749SSergey Zigachev 			++nodes;
1637b843c749SSergey Zigachev 		}
1638b843c749SSergey Zigachev 	}
1639b843c749SSergey Zigachev 
1640b843c749SSergey Zigachev 	do {
1641b843c749SSergey Zigachev 		dma_addr_t *dma_addr = NULL;
1642b843c749SSergey Zigachev 		uint64_t max_entries;
1643b843c749SSergey Zigachev 		uint64_t addr, last;
1644b843c749SSergey Zigachev 
1645b843c749SSergey Zigachev 		if (nodes) {
1646b843c749SSergey Zigachev 			addr = nodes->start << PAGE_SHIFT;
1647b843c749SSergey Zigachev 			max_entries = (nodes->size - pfn) *
1648b843c749SSergey Zigachev 				AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1649b843c749SSergey Zigachev 		} else {
1650b843c749SSergey Zigachev 			addr = 0;
1651b843c749SSergey Zigachev 			max_entries = S64_MAX;
1652b843c749SSergey Zigachev 		}
1653b843c749SSergey Zigachev 
1654b843c749SSergey Zigachev 		if (pages_addr) {
1655b843c749SSergey Zigachev 			uint64_t count;
1656b843c749SSergey Zigachev 
1657b843c749SSergey Zigachev 			max_entries = min(max_entries, 16ull * 1024ull);
1658b843c749SSergey Zigachev 			for (count = 1;
1659b843c749SSergey Zigachev 			     count < max_entries / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1660b843c749SSergey Zigachev 			     ++count) {
1661b843c749SSergey Zigachev 				uint64_t idx = pfn + count;
1662b843c749SSergey Zigachev 
1663b843c749SSergey Zigachev 				if (pages_addr[idx] !=
1664b843c749SSergey Zigachev 				    (pages_addr[idx - 1] + PAGE_SIZE))
1665b843c749SSergey Zigachev 					break;
1666b843c749SSergey Zigachev 			}
1667b843c749SSergey Zigachev 
1668b843c749SSergey Zigachev 			if (count < min_linear_pages) {
1669b843c749SSergey Zigachev 				addr = pfn << PAGE_SHIFT;
1670b843c749SSergey Zigachev 				dma_addr = pages_addr;
1671b843c749SSergey Zigachev 			} else {
1672b843c749SSergey Zigachev 				addr = pages_addr[pfn];
1673b843c749SSergey Zigachev 				max_entries = count * AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1674b843c749SSergey Zigachev 			}
1675b843c749SSergey Zigachev 
1676b843c749SSergey Zigachev 		} else if (flags & AMDGPU_PTE_VALID) {
1677b843c749SSergey Zigachev 			addr += adev->vm_manager.vram_base_offset;
1678b843c749SSergey Zigachev 			addr += pfn << PAGE_SHIFT;
1679b843c749SSergey Zigachev 		}
1680b843c749SSergey Zigachev 
1681b843c749SSergey Zigachev 		last = min((uint64_t)mapping->last, start + max_entries - 1);
1682b843c749SSergey Zigachev 		r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
1683b843c749SSergey Zigachev 						start, last, flags, addr,
1684b843c749SSergey Zigachev 						fence);
1685b843c749SSergey Zigachev 		if (r)
1686b843c749SSergey Zigachev 			return r;
1687b843c749SSergey Zigachev 
1688b843c749SSergey Zigachev 		pfn += (last - start + 1) / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1689b843c749SSergey Zigachev 		if (nodes && nodes->size == pfn) {
1690b843c749SSergey Zigachev 			pfn = 0;
1691b843c749SSergey Zigachev 			++nodes;
1692b843c749SSergey Zigachev 		}
1693b843c749SSergey Zigachev 		start = last + 1;
1694b843c749SSergey Zigachev 
1695b843c749SSergey Zigachev 	} while (unlikely(start != mapping->last + 1));
1696b843c749SSergey Zigachev 
1697b843c749SSergey Zigachev 	return 0;
1698b843c749SSergey Zigachev }
1699b843c749SSergey Zigachev 
1700b843c749SSergey Zigachev /**
1701b843c749SSergey Zigachev  * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1702b843c749SSergey Zigachev  *
1703b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1704b843c749SSergey Zigachev  * @bo_va: requested BO and VM object
1705b843c749SSergey Zigachev  * @clear: if true clear the entries
1706b843c749SSergey Zigachev  *
1707b843c749SSergey Zigachev  * Fill in the page table entries for @bo_va.
1708b843c749SSergey Zigachev  *
1709b843c749SSergey Zigachev  * Returns:
1710b843c749SSergey Zigachev  * 0 for success, -EINVAL for failure.
1711b843c749SSergey Zigachev  */
amdgpu_vm_bo_update(struct amdgpu_device * adev,struct amdgpu_bo_va * bo_va,bool clear)1712b843c749SSergey Zigachev int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1713b843c749SSergey Zigachev 			struct amdgpu_bo_va *bo_va,
1714b843c749SSergey Zigachev 			bool clear)
1715b843c749SSergey Zigachev {
1716b843c749SSergey Zigachev 	struct amdgpu_bo *bo = bo_va->base.bo;
1717b843c749SSergey Zigachev 	struct amdgpu_vm *vm = bo_va->base.vm;
1718b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping;
1719b843c749SSergey Zigachev 	dma_addr_t *pages_addr = NULL;
1720b843c749SSergey Zigachev 	struct ttm_mem_reg *mem;
1721b843c749SSergey Zigachev 	struct drm_mm_node *nodes;
1722b843c749SSergey Zigachev 	struct dma_fence *exclusive, **last_update;
1723b843c749SSergey Zigachev 	uint64_t flags;
1724b843c749SSergey Zigachev 	int r;
1725b843c749SSergey Zigachev 
1726b843c749SSergey Zigachev 	if (clear || !bo) {
1727b843c749SSergey Zigachev 		mem = NULL;
1728b843c749SSergey Zigachev 		nodes = NULL;
1729b843c749SSergey Zigachev 		exclusive = NULL;
1730b843c749SSergey Zigachev 	} else {
1731b843c749SSergey Zigachev 		struct ttm_dma_tt *ttm;
1732b843c749SSergey Zigachev 
1733b843c749SSergey Zigachev 		mem = &bo->tbo.mem;
1734b843c749SSergey Zigachev 		nodes = mem->mm_node;
1735b843c749SSergey Zigachev 		if (mem->mem_type == TTM_PL_TT) {
1736b843c749SSergey Zigachev 			ttm = container_of(bo->tbo.ttm, struct ttm_dma_tt, ttm);
1737b843c749SSergey Zigachev 			pages_addr = ttm->dma_address;
1738b843c749SSergey Zigachev 		}
1739b843c749SSergey Zigachev 		exclusive = reservation_object_get_excl(bo->tbo.resv);
1740b843c749SSergey Zigachev 	}
1741b843c749SSergey Zigachev 
1742b843c749SSergey Zigachev 	if (bo)
1743b843c749SSergey Zigachev 		flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1744b843c749SSergey Zigachev 	else
1745b843c749SSergey Zigachev 		flags = 0x0;
1746b843c749SSergey Zigachev 
1747b843c749SSergey Zigachev 	if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1748b843c749SSergey Zigachev 		last_update = &vm->last_update;
1749b843c749SSergey Zigachev 	else
1750b843c749SSergey Zigachev 		last_update = &bo_va->last_pt_update;
1751b843c749SSergey Zigachev 
1752b843c749SSergey Zigachev 	if (!clear && bo_va->base.moved) {
1753b843c749SSergey Zigachev 		bo_va->base.moved = false;
1754b843c749SSergey Zigachev 		list_splice_init(&bo_va->valids, &bo_va->invalids);
1755b843c749SSergey Zigachev 
1756b843c749SSergey Zigachev 	} else if (bo_va->cleared != clear) {
1757b843c749SSergey Zigachev 		list_splice_init(&bo_va->valids, &bo_va->invalids);
1758b843c749SSergey Zigachev 	}
1759b843c749SSergey Zigachev 
1760b843c749SSergey Zigachev 	list_for_each_entry(mapping, &bo_va->invalids, list) {
1761b843c749SSergey Zigachev 		r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
1762b843c749SSergey Zigachev 					       mapping, flags, nodes,
1763b843c749SSergey Zigachev 					       last_update);
1764b843c749SSergey Zigachev 		if (r)
1765b843c749SSergey Zigachev 			return r;
1766b843c749SSergey Zigachev 	}
1767b843c749SSergey Zigachev 
1768b843c749SSergey Zigachev 	if (vm->use_cpu_for_update) {
1769b843c749SSergey Zigachev 		/* Flush HDP */
1770b843c749SSergey Zigachev 		mb();
1771b843c749SSergey Zigachev 		amdgpu_asic_flush_hdp(adev, NULL);
1772b843c749SSergey Zigachev 	}
1773b843c749SSergey Zigachev 
177478973132SSergey Zigachev 	lockmgr(&vm->moved_lock, LK_EXCLUSIVE);
1775b843c749SSergey Zigachev 	list_del_init(&bo_va->base.vm_status);
177678973132SSergey Zigachev 	lockmgr(&vm->moved_lock, LK_RELEASE);
1777b843c749SSergey Zigachev 
1778b843c749SSergey Zigachev 	/* If the BO is not in its preferred location add it back to
1779b843c749SSergey Zigachev 	 * the evicted list so that it gets validated again on the
1780b843c749SSergey Zigachev 	 * next command submission.
1781b843c749SSergey Zigachev 	 */
1782b843c749SSergey Zigachev 	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1783b843c749SSergey Zigachev 		uint32_t mem_type = bo->tbo.mem.mem_type;
1784b843c749SSergey Zigachev 
1785b843c749SSergey Zigachev 		if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type)))
1786b843c749SSergey Zigachev 			list_add_tail(&bo_va->base.vm_status, &vm->evicted);
1787b843c749SSergey Zigachev 		else
1788b843c749SSergey Zigachev 			list_add(&bo_va->base.vm_status, &vm->idle);
1789b843c749SSergey Zigachev 	}
1790b843c749SSergey Zigachev 
1791b843c749SSergey Zigachev 	list_splice_init(&bo_va->invalids, &bo_va->valids);
1792b843c749SSergey Zigachev 	bo_va->cleared = clear;
1793b843c749SSergey Zigachev 
179478973132SSergey Zigachev #if 0
1795b843c749SSergey Zigachev 	if (trace_amdgpu_vm_bo_mapping_enabled()) {
1796b843c749SSergey Zigachev 		list_for_each_entry(mapping, &bo_va->valids, list)
1797b843c749SSergey Zigachev 			trace_amdgpu_vm_bo_mapping(mapping);
1798b843c749SSergey Zigachev 	}
179978973132SSergey Zigachev #endif
1800b843c749SSergey Zigachev 
1801b843c749SSergey Zigachev 	return 0;
1802b843c749SSergey Zigachev }
1803b843c749SSergey Zigachev 
1804b843c749SSergey Zigachev /**
1805b843c749SSergey Zigachev  * amdgpu_vm_update_prt_state - update the global PRT state
1806b843c749SSergey Zigachev  *
1807b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1808b843c749SSergey Zigachev  */
amdgpu_vm_update_prt_state(struct amdgpu_device * adev)1809b843c749SSergey Zigachev static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1810b843c749SSergey Zigachev {
1811b843c749SSergey Zigachev 	unsigned long flags;
1812b843c749SSergey Zigachev 	bool enable;
1813b843c749SSergey Zigachev 
1814b843c749SSergey Zigachev 	spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1815b843c749SSergey Zigachev 	enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1816b843c749SSergey Zigachev 	adev->gmc.gmc_funcs->set_prt(adev, enable);
1817b843c749SSergey Zigachev 	spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1818b843c749SSergey Zigachev }
1819b843c749SSergey Zigachev 
1820b843c749SSergey Zigachev /**
1821b843c749SSergey Zigachev  * amdgpu_vm_prt_get - add a PRT user
1822b843c749SSergey Zigachev  *
1823b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1824b843c749SSergey Zigachev  */
amdgpu_vm_prt_get(struct amdgpu_device * adev)1825b843c749SSergey Zigachev static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1826b843c749SSergey Zigachev {
1827b843c749SSergey Zigachev 	if (!adev->gmc.gmc_funcs->set_prt)
1828b843c749SSergey Zigachev 		return;
1829b843c749SSergey Zigachev 
1830b843c749SSergey Zigachev 	if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1831b843c749SSergey Zigachev 		amdgpu_vm_update_prt_state(adev);
1832b843c749SSergey Zigachev }
1833b843c749SSergey Zigachev 
1834b843c749SSergey Zigachev /**
1835b843c749SSergey Zigachev  * amdgpu_vm_prt_put - drop a PRT user
1836b843c749SSergey Zigachev  *
1837b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1838b843c749SSergey Zigachev  */
amdgpu_vm_prt_put(struct amdgpu_device * adev)1839b843c749SSergey Zigachev static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1840b843c749SSergey Zigachev {
1841b843c749SSergey Zigachev 	if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1842b843c749SSergey Zigachev 		amdgpu_vm_update_prt_state(adev);
1843b843c749SSergey Zigachev }
1844b843c749SSergey Zigachev 
1845b843c749SSergey Zigachev /**
1846b843c749SSergey Zigachev  * amdgpu_vm_prt_cb - callback for updating the PRT status
1847b843c749SSergey Zigachev  *
1848b843c749SSergey Zigachev  * @fence: fence for the callback
1849b843c749SSergey Zigachev  * @_cb: the callback function
1850b843c749SSergey Zigachev  */
amdgpu_vm_prt_cb(struct dma_fence * fence,struct dma_fence_cb * _cb)1851b843c749SSergey Zigachev static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1852b843c749SSergey Zigachev {
1853b843c749SSergey Zigachev 	struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1854b843c749SSergey Zigachev 
1855b843c749SSergey Zigachev 	amdgpu_vm_prt_put(cb->adev);
1856b843c749SSergey Zigachev 	kfree(cb);
1857b843c749SSergey Zigachev }
1858b843c749SSergey Zigachev 
1859b843c749SSergey Zigachev /**
1860b843c749SSergey Zigachev  * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1861b843c749SSergey Zigachev  *
1862b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1863b843c749SSergey Zigachev  * @fence: fence for the callback
1864b843c749SSergey Zigachev  */
amdgpu_vm_add_prt_cb(struct amdgpu_device * adev,struct dma_fence * fence)1865b843c749SSergey Zigachev static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1866b843c749SSergey Zigachev 				 struct dma_fence *fence)
1867b843c749SSergey Zigachev {
1868b843c749SSergey Zigachev 	struct amdgpu_prt_cb *cb;
1869b843c749SSergey Zigachev 
1870b843c749SSergey Zigachev 	if (!adev->gmc.gmc_funcs->set_prt)
1871b843c749SSergey Zigachev 		return;
1872b843c749SSergey Zigachev 
187378973132SSergey Zigachev 	cb = kmalloc(sizeof(struct amdgpu_prt_cb), M_DRM, GFP_KERNEL);
1874b843c749SSergey Zigachev 	if (!cb) {
1875b843c749SSergey Zigachev 		/* Last resort when we are OOM */
1876b843c749SSergey Zigachev 		if (fence)
1877b843c749SSergey Zigachev 			dma_fence_wait(fence, false);
1878b843c749SSergey Zigachev 
1879b843c749SSergey Zigachev 		amdgpu_vm_prt_put(adev);
1880b843c749SSergey Zigachev 	} else {
1881b843c749SSergey Zigachev 		cb->adev = adev;
1882b843c749SSergey Zigachev 		if (!fence || dma_fence_add_callback(fence, &cb->cb,
1883b843c749SSergey Zigachev 						     amdgpu_vm_prt_cb))
1884b843c749SSergey Zigachev 			amdgpu_vm_prt_cb(fence, &cb->cb);
1885b843c749SSergey Zigachev 	}
1886b843c749SSergey Zigachev }
1887b843c749SSergey Zigachev 
1888b843c749SSergey Zigachev /**
1889b843c749SSergey Zigachev  * amdgpu_vm_free_mapping - free a mapping
1890b843c749SSergey Zigachev  *
1891b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1892b843c749SSergey Zigachev  * @vm: requested vm
1893b843c749SSergey Zigachev  * @mapping: mapping to be freed
1894b843c749SSergey Zigachev  * @fence: fence of the unmap operation
1895b843c749SSergey Zigachev  *
1896b843c749SSergey Zigachev  * Free a mapping and make sure we decrease the PRT usage count if applicable.
1897b843c749SSergey Zigachev  */
amdgpu_vm_free_mapping(struct amdgpu_device * adev,struct amdgpu_vm * vm,struct amdgpu_bo_va_mapping * mapping,struct dma_fence * fence)1898b843c749SSergey Zigachev static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1899b843c749SSergey Zigachev 				   struct amdgpu_vm *vm,
1900b843c749SSergey Zigachev 				   struct amdgpu_bo_va_mapping *mapping,
1901b843c749SSergey Zigachev 				   struct dma_fence *fence)
1902b843c749SSergey Zigachev {
1903b843c749SSergey Zigachev 	if (mapping->flags & AMDGPU_PTE_PRT)
1904b843c749SSergey Zigachev 		amdgpu_vm_add_prt_cb(adev, fence);
1905b843c749SSergey Zigachev 	kfree(mapping);
1906b843c749SSergey Zigachev }
1907b843c749SSergey Zigachev 
1908b843c749SSergey Zigachev /**
1909b843c749SSergey Zigachev  * amdgpu_vm_prt_fini - finish all prt mappings
1910b843c749SSergey Zigachev  *
1911b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1912b843c749SSergey Zigachev  * @vm: requested vm
1913b843c749SSergey Zigachev  *
1914b843c749SSergey Zigachev  * Register a cleanup callback to disable PRT support after VM dies.
1915b843c749SSergey Zigachev  */
amdgpu_vm_prt_fini(struct amdgpu_device * adev,struct amdgpu_vm * vm)1916b843c749SSergey Zigachev static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1917b843c749SSergey Zigachev {
1918b843c749SSergey Zigachev 	struct reservation_object *resv = vm->root.base.bo->tbo.resv;
1919b843c749SSergey Zigachev 	struct dma_fence *excl, **shared;
1920b843c749SSergey Zigachev 	unsigned i, shared_count;
1921b843c749SSergey Zigachev 	int r;
1922b843c749SSergey Zigachev 
1923b843c749SSergey Zigachev 	r = reservation_object_get_fences_rcu(resv, &excl,
1924b843c749SSergey Zigachev 					      &shared_count, &shared);
1925b843c749SSergey Zigachev 	if (r) {
1926b843c749SSergey Zigachev 		/* Not enough memory to grab the fence list, as last resort
1927b843c749SSergey Zigachev 		 * block for all the fences to complete.
1928b843c749SSergey Zigachev 		 */
1929b843c749SSergey Zigachev 		reservation_object_wait_timeout_rcu(resv, true, false,
1930b843c749SSergey Zigachev 						    MAX_SCHEDULE_TIMEOUT);
1931b843c749SSergey Zigachev 		return;
1932b843c749SSergey Zigachev 	}
1933b843c749SSergey Zigachev 
1934b843c749SSergey Zigachev 	/* Add a callback for each fence in the reservation object */
1935b843c749SSergey Zigachev 	amdgpu_vm_prt_get(adev);
1936b843c749SSergey Zigachev 	amdgpu_vm_add_prt_cb(adev, excl);
1937b843c749SSergey Zigachev 
1938b843c749SSergey Zigachev 	for (i = 0; i < shared_count; ++i) {
1939b843c749SSergey Zigachev 		amdgpu_vm_prt_get(adev);
1940b843c749SSergey Zigachev 		amdgpu_vm_add_prt_cb(adev, shared[i]);
1941b843c749SSergey Zigachev 	}
1942b843c749SSergey Zigachev 
1943b843c749SSergey Zigachev 	kfree(shared);
1944b843c749SSergey Zigachev }
1945b843c749SSergey Zigachev 
1946b843c749SSergey Zigachev /**
1947b843c749SSergey Zigachev  * amdgpu_vm_clear_freed - clear freed BOs in the PT
1948b843c749SSergey Zigachev  *
1949b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1950b843c749SSergey Zigachev  * @vm: requested vm
1951b843c749SSergey Zigachev  * @fence: optional resulting fence (unchanged if no work needed to be done
1952b843c749SSergey Zigachev  * or if an error occurred)
1953b843c749SSergey Zigachev  *
1954b843c749SSergey Zigachev  * Make sure all freed BOs are cleared in the PT.
1955b843c749SSergey Zigachev  * PTs have to be reserved and mutex must be locked!
1956b843c749SSergey Zigachev  *
1957b843c749SSergey Zigachev  * Returns:
1958b843c749SSergey Zigachev  * 0 for success.
1959b843c749SSergey Zigachev  *
1960b843c749SSergey Zigachev  */
amdgpu_vm_clear_freed(struct amdgpu_device * adev,struct amdgpu_vm * vm,struct dma_fence ** fence)1961b843c749SSergey Zigachev int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1962b843c749SSergey Zigachev 			  struct amdgpu_vm *vm,
1963b843c749SSergey Zigachev 			  struct dma_fence **fence)
1964b843c749SSergey Zigachev {
1965b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping;
1966b843c749SSergey Zigachev 	uint64_t init_pte_value = 0;
1967b843c749SSergey Zigachev 	struct dma_fence *f = NULL;
1968b843c749SSergey Zigachev 	int r;
1969b843c749SSergey Zigachev 
1970b843c749SSergey Zigachev 	while (!list_empty(&vm->freed)) {
1971b843c749SSergey Zigachev 		mapping = list_first_entry(&vm->freed,
1972b843c749SSergey Zigachev 			struct amdgpu_bo_va_mapping, list);
1973b843c749SSergey Zigachev 		list_del(&mapping->list);
1974b843c749SSergey Zigachev 
1975b843c749SSergey Zigachev 		if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
1976b843c749SSergey Zigachev 			init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
1977b843c749SSergey Zigachev 
1978b843c749SSergey Zigachev 		r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
1979b843c749SSergey Zigachev 						mapping->start, mapping->last,
1980b843c749SSergey Zigachev 						init_pte_value, 0, &f);
1981b843c749SSergey Zigachev 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
1982b843c749SSergey Zigachev 		if (r) {
1983b843c749SSergey Zigachev 			dma_fence_put(f);
1984b843c749SSergey Zigachev 			return r;
1985b843c749SSergey Zigachev 		}
1986b843c749SSergey Zigachev 	}
1987b843c749SSergey Zigachev 
1988b843c749SSergey Zigachev 	if (fence && f) {
1989b843c749SSergey Zigachev 		dma_fence_put(*fence);
1990b843c749SSergey Zigachev 		*fence = f;
1991b843c749SSergey Zigachev 	} else {
1992b843c749SSergey Zigachev 		dma_fence_put(f);
1993b843c749SSergey Zigachev 	}
1994b843c749SSergey Zigachev 
1995b843c749SSergey Zigachev 	return 0;
1996b843c749SSergey Zigachev 
1997b843c749SSergey Zigachev }
1998b843c749SSergey Zigachev 
1999b843c749SSergey Zigachev /**
2000b843c749SSergey Zigachev  * amdgpu_vm_handle_moved - handle moved BOs in the PT
2001b843c749SSergey Zigachev  *
2002b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2003b843c749SSergey Zigachev  * @vm: requested vm
2004b843c749SSergey Zigachev  *
2005b843c749SSergey Zigachev  * Make sure all BOs which are moved are updated in the PTs.
2006b843c749SSergey Zigachev  *
2007b843c749SSergey Zigachev  * Returns:
2008b843c749SSergey Zigachev  * 0 for success.
2009b843c749SSergey Zigachev  *
2010b843c749SSergey Zigachev  * PTs have to be reserved!
2011b843c749SSergey Zigachev  */
amdgpu_vm_handle_moved(struct amdgpu_device * adev,struct amdgpu_vm * vm)2012b843c749SSergey Zigachev int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
2013b843c749SSergey Zigachev 			   struct amdgpu_vm *vm)
2014b843c749SSergey Zigachev {
2015b843c749SSergey Zigachev 	struct amdgpu_bo_va *bo_va, *tmp;
2016b843c749SSergey Zigachev 	struct list_head moved;
2017b843c749SSergey Zigachev 	bool clear;
2018b843c749SSergey Zigachev 	int r;
2019b843c749SSergey Zigachev 
2020b843c749SSergey Zigachev 	INIT_LIST_HEAD(&moved);
202178973132SSergey Zigachev 	lockmgr(&vm->moved_lock, LK_EXCLUSIVE);
2022b843c749SSergey Zigachev 	list_splice_init(&vm->moved, &moved);
202378973132SSergey Zigachev 	lockmgr(&vm->moved_lock, LK_RELEASE);
2024b843c749SSergey Zigachev 
2025b843c749SSergey Zigachev 	list_for_each_entry_safe(bo_va, tmp, &moved, base.vm_status) {
2026b843c749SSergey Zigachev 		struct reservation_object *resv = bo_va->base.bo->tbo.resv;
2027b843c749SSergey Zigachev 
2028b843c749SSergey Zigachev 		/* Per VM BOs never need to bo cleared in the page tables */
2029b843c749SSergey Zigachev 		if (resv == vm->root.base.bo->tbo.resv)
2030b843c749SSergey Zigachev 			clear = false;
2031b843c749SSergey Zigachev 		/* Try to reserve the BO to avoid clearing its ptes */
2032b843c749SSergey Zigachev 		else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
2033b843c749SSergey Zigachev 			clear = false;
2034b843c749SSergey Zigachev 		/* Somebody else is using the BO right now */
2035b843c749SSergey Zigachev 		else
2036b843c749SSergey Zigachev 			clear = true;
2037b843c749SSergey Zigachev 
2038b843c749SSergey Zigachev 		r = amdgpu_vm_bo_update(adev, bo_va, clear);
2039b843c749SSergey Zigachev 		if (r) {
204078973132SSergey Zigachev 			lockmgr(&vm->moved_lock, LK_EXCLUSIVE);
2041b843c749SSergey Zigachev 			list_splice(&moved, &vm->moved);
204278973132SSergey Zigachev 			lockmgr(&vm->moved_lock, LK_RELEASE);
2043b843c749SSergey Zigachev 			return r;
2044b843c749SSergey Zigachev 		}
2045b843c749SSergey Zigachev 
2046b843c749SSergey Zigachev 		if (!clear && resv != vm->root.base.bo->tbo.resv)
2047b843c749SSergey Zigachev 			reservation_object_unlock(resv);
2048b843c749SSergey Zigachev 
2049b843c749SSergey Zigachev 	}
2050b843c749SSergey Zigachev 
2051b843c749SSergey Zigachev 	return 0;
2052b843c749SSergey Zigachev }
2053b843c749SSergey Zigachev 
2054b843c749SSergey Zigachev /**
2055b843c749SSergey Zigachev  * amdgpu_vm_bo_add - add a bo to a specific vm
2056b843c749SSergey Zigachev  *
2057b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2058b843c749SSergey Zigachev  * @vm: requested vm
2059b843c749SSergey Zigachev  * @bo: amdgpu buffer object
2060b843c749SSergey Zigachev  *
2061b843c749SSergey Zigachev  * Add @bo into the requested vm.
2062b843c749SSergey Zigachev  * Add @bo to the list of bos associated with the vm
2063b843c749SSergey Zigachev  *
2064b843c749SSergey Zigachev  * Returns:
2065b843c749SSergey Zigachev  * Newly added bo_va or NULL for failure
2066b843c749SSergey Zigachev  *
2067b843c749SSergey Zigachev  * Object has to be reserved!
2068b843c749SSergey Zigachev  */
amdgpu_vm_bo_add(struct amdgpu_device * adev,struct amdgpu_vm * vm,struct amdgpu_bo * bo)2069b843c749SSergey Zigachev struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2070b843c749SSergey Zigachev 				      struct amdgpu_vm *vm,
2071b843c749SSergey Zigachev 				      struct amdgpu_bo *bo)
2072b843c749SSergey Zigachev {
2073b843c749SSergey Zigachev 	struct amdgpu_bo_va *bo_va;
2074b843c749SSergey Zigachev 
2075b843c749SSergey Zigachev 	bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2076b843c749SSergey Zigachev 	if (bo_va == NULL) {
2077b843c749SSergey Zigachev 		return NULL;
2078b843c749SSergey Zigachev 	}
2079b843c749SSergey Zigachev 	amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
2080b843c749SSergey Zigachev 
2081b843c749SSergey Zigachev 	bo_va->ref_count = 1;
2082b843c749SSergey Zigachev 	INIT_LIST_HEAD(&bo_va->valids);
2083b843c749SSergey Zigachev 	INIT_LIST_HEAD(&bo_va->invalids);
2084b843c749SSergey Zigachev 
2085b843c749SSergey Zigachev 	return bo_va;
2086b843c749SSergey Zigachev }
2087b843c749SSergey Zigachev 
2088b843c749SSergey Zigachev 
2089b843c749SSergey Zigachev /**
2090b843c749SSergey Zigachev  * amdgpu_vm_bo_insert_mapping - insert a new mapping
2091b843c749SSergey Zigachev  *
2092b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2093b843c749SSergey Zigachev  * @bo_va: bo_va to store the address
2094b843c749SSergey Zigachev  * @mapping: the mapping to insert
2095b843c749SSergey Zigachev  *
2096b843c749SSergey Zigachev  * Insert a new mapping into all structures.
2097b843c749SSergey Zigachev  */
amdgpu_vm_bo_insert_map(struct amdgpu_device * adev,struct amdgpu_bo_va * bo_va,struct amdgpu_bo_va_mapping * mapping)2098b843c749SSergey Zigachev static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2099b843c749SSergey Zigachev 				    struct amdgpu_bo_va *bo_va,
2100b843c749SSergey Zigachev 				    struct amdgpu_bo_va_mapping *mapping)
2101b843c749SSergey Zigachev {
2102b843c749SSergey Zigachev 	struct amdgpu_vm *vm = bo_va->base.vm;
2103b843c749SSergey Zigachev 	struct amdgpu_bo *bo = bo_va->base.bo;
2104b843c749SSergey Zigachev 
2105b843c749SSergey Zigachev 	mapping->bo_va = bo_va;
2106b843c749SSergey Zigachev 	list_add(&mapping->list, &bo_va->invalids);
2107b843c749SSergey Zigachev 	amdgpu_vm_it_insert(mapping, &vm->va);
2108b843c749SSergey Zigachev 
2109b843c749SSergey Zigachev 	if (mapping->flags & AMDGPU_PTE_PRT)
2110b843c749SSergey Zigachev 		amdgpu_vm_prt_get(adev);
2111b843c749SSergey Zigachev 
2112b843c749SSergey Zigachev 	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
2113b843c749SSergey Zigachev 	    !bo_va->base.moved) {
211478973132SSergey Zigachev 		lockmgr(&vm->moved_lock, LK_EXCLUSIVE);
2115b843c749SSergey Zigachev 		list_move(&bo_va->base.vm_status, &vm->moved);
211678973132SSergey Zigachev 		lockmgr(&vm->moved_lock, LK_RELEASE);
2117b843c749SSergey Zigachev 	}
211878973132SSergey Zigachev #if 0
2119b843c749SSergey Zigachev 	trace_amdgpu_vm_bo_map(bo_va, mapping);
212078973132SSergey Zigachev #endif
2121b843c749SSergey Zigachev }
2122b843c749SSergey Zigachev 
2123b843c749SSergey Zigachev /**
2124b843c749SSergey Zigachev  * amdgpu_vm_bo_map - map bo inside a vm
2125b843c749SSergey Zigachev  *
2126b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2127b843c749SSergey Zigachev  * @bo_va: bo_va to store the address
2128b843c749SSergey Zigachev  * @saddr: where to map the BO
2129b843c749SSergey Zigachev  * @offset: requested offset in the BO
2130b843c749SSergey Zigachev  * @size: BO size in bytes
2131b843c749SSergey Zigachev  * @flags: attributes of pages (read/write/valid/etc.)
2132b843c749SSergey Zigachev  *
2133b843c749SSergey Zigachev  * Add a mapping of the BO at the specefied addr into the VM.
2134b843c749SSergey Zigachev  *
2135b843c749SSergey Zigachev  * Returns:
2136b843c749SSergey Zigachev  * 0 for success, error for failure.
2137b843c749SSergey Zigachev  *
2138b843c749SSergey Zigachev  * Object has to be reserved and unreserved outside!
2139b843c749SSergey Zigachev  */
amdgpu_vm_bo_map(struct amdgpu_device * adev,struct amdgpu_bo_va * bo_va,uint64_t saddr,uint64_t offset,uint64_t size,uint64_t flags)2140b843c749SSergey Zigachev int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2141b843c749SSergey Zigachev 		     struct amdgpu_bo_va *bo_va,
2142b843c749SSergey Zigachev 		     uint64_t saddr, uint64_t offset,
2143b843c749SSergey Zigachev 		     uint64_t size, uint64_t flags)
2144b843c749SSergey Zigachev {
2145b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping, *tmp;
2146b843c749SSergey Zigachev 	struct amdgpu_bo *bo = bo_va->base.bo;
2147b843c749SSergey Zigachev 	struct amdgpu_vm *vm = bo_va->base.vm;
2148b843c749SSergey Zigachev 	uint64_t eaddr;
2149b843c749SSergey Zigachev 
2150b843c749SSergey Zigachev 	/* validate the parameters */
2151b843c749SSergey Zigachev 	if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
2152b843c749SSergey Zigachev 	    size == 0 || size & ~PAGE_MASK)
2153b843c749SSergey Zigachev 		return -EINVAL;
2154b843c749SSergey Zigachev 
2155b843c749SSergey Zigachev 	/* make sure object fit at this offset */
2156b843c749SSergey Zigachev 	eaddr = saddr + size - 1;
2157b843c749SSergey Zigachev 	if (saddr >= eaddr ||
2158b843c749SSergey Zigachev 	    (bo && offset + size > amdgpu_bo_size(bo)))
2159b843c749SSergey Zigachev 		return -EINVAL;
2160b843c749SSergey Zigachev 
2161b843c749SSergey Zigachev 	saddr /= AMDGPU_GPU_PAGE_SIZE;
2162b843c749SSergey Zigachev 	eaddr /= AMDGPU_GPU_PAGE_SIZE;
2163b843c749SSergey Zigachev 
2164b843c749SSergey Zigachev 	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2165b843c749SSergey Zigachev 	if (tmp) {
2166b843c749SSergey Zigachev 		/* bo and tmp overlap, invalid addr */
216778973132SSergey Zigachev 		dev_err(adev->dev, "bo %p va 0x%010lx-0x%010lx conflict with "
216878973132SSergey Zigachev 			"0x%010lx-0x%010lx\n", bo, saddr, eaddr,
2169b843c749SSergey Zigachev 			tmp->start, tmp->last + 1);
2170b843c749SSergey Zigachev 		return -EINVAL;
2171b843c749SSergey Zigachev 	}
2172b843c749SSergey Zigachev 
217378973132SSergey Zigachev 	mapping = kmalloc(sizeof(*mapping), M_DRM, GFP_KERNEL);
2174b843c749SSergey Zigachev 	if (!mapping)
2175b843c749SSergey Zigachev 		return -ENOMEM;
2176b843c749SSergey Zigachev 
2177b843c749SSergey Zigachev 	mapping->start = saddr;
2178b843c749SSergey Zigachev 	mapping->last = eaddr;
2179b843c749SSergey Zigachev 	mapping->offset = offset;
2180b843c749SSergey Zigachev 	mapping->flags = flags;
2181b843c749SSergey Zigachev 
2182b843c749SSergey Zigachev 	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
2183b843c749SSergey Zigachev 
2184b843c749SSergey Zigachev 	return 0;
2185b843c749SSergey Zigachev }
2186b843c749SSergey Zigachev 
2187b843c749SSergey Zigachev /**
2188b843c749SSergey Zigachev  * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2189b843c749SSergey Zigachev  *
2190b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2191b843c749SSergey Zigachev  * @bo_va: bo_va to store the address
2192b843c749SSergey Zigachev  * @saddr: where to map the BO
2193b843c749SSergey Zigachev  * @offset: requested offset in the BO
2194b843c749SSergey Zigachev  * @size: BO size in bytes
2195b843c749SSergey Zigachev  * @flags: attributes of pages (read/write/valid/etc.)
2196b843c749SSergey Zigachev  *
2197b843c749SSergey Zigachev  * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2198b843c749SSergey Zigachev  * mappings as we do so.
2199b843c749SSergey Zigachev  *
2200b843c749SSergey Zigachev  * Returns:
2201b843c749SSergey Zigachev  * 0 for success, error for failure.
2202b843c749SSergey Zigachev  *
2203b843c749SSergey Zigachev  * Object has to be reserved and unreserved outside!
2204b843c749SSergey Zigachev  */
amdgpu_vm_bo_replace_map(struct amdgpu_device * adev,struct amdgpu_bo_va * bo_va,uint64_t saddr,uint64_t offset,uint64_t size,uint64_t flags)2205b843c749SSergey Zigachev int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2206b843c749SSergey Zigachev 			     struct amdgpu_bo_va *bo_va,
2207b843c749SSergey Zigachev 			     uint64_t saddr, uint64_t offset,
2208b843c749SSergey Zigachev 			     uint64_t size, uint64_t flags)
2209b843c749SSergey Zigachev {
2210b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping;
2211b843c749SSergey Zigachev 	struct amdgpu_bo *bo = bo_va->base.bo;
2212b843c749SSergey Zigachev 	uint64_t eaddr;
2213b843c749SSergey Zigachev 	int r;
2214b843c749SSergey Zigachev 
2215b843c749SSergey Zigachev 	/* validate the parameters */
2216b843c749SSergey Zigachev 	if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
2217b843c749SSergey Zigachev 	    size == 0 || size & ~PAGE_MASK)
2218b843c749SSergey Zigachev 		return -EINVAL;
2219b843c749SSergey Zigachev 
2220b843c749SSergey Zigachev 	/* make sure object fit at this offset */
2221b843c749SSergey Zigachev 	eaddr = saddr + size - 1;
2222b843c749SSergey Zigachev 	if (saddr >= eaddr ||
2223b843c749SSergey Zigachev 	    (bo && offset + size > amdgpu_bo_size(bo)))
2224b843c749SSergey Zigachev 		return -EINVAL;
2225b843c749SSergey Zigachev 
2226b843c749SSergey Zigachev 	/* Allocate all the needed memory */
222778973132SSergey Zigachev 	mapping = kmalloc(sizeof(*mapping), M_DRM, GFP_KERNEL);
2228b843c749SSergey Zigachev 	if (!mapping)
2229b843c749SSergey Zigachev 		return -ENOMEM;
2230b843c749SSergey Zigachev 
2231b843c749SSergey Zigachev 	r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
2232b843c749SSergey Zigachev 	if (r) {
2233b843c749SSergey Zigachev 		kfree(mapping);
2234b843c749SSergey Zigachev 		return r;
2235b843c749SSergey Zigachev 	}
2236b843c749SSergey Zigachev 
2237b843c749SSergey Zigachev 	saddr /= AMDGPU_GPU_PAGE_SIZE;
2238b843c749SSergey Zigachev 	eaddr /= AMDGPU_GPU_PAGE_SIZE;
2239b843c749SSergey Zigachev 
2240b843c749SSergey Zigachev 	mapping->start = saddr;
2241b843c749SSergey Zigachev 	mapping->last = eaddr;
2242b843c749SSergey Zigachev 	mapping->offset = offset;
2243b843c749SSergey Zigachev 	mapping->flags = flags;
2244b843c749SSergey Zigachev 
2245b843c749SSergey Zigachev 	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
2246b843c749SSergey Zigachev 
2247b843c749SSergey Zigachev 	return 0;
2248b843c749SSergey Zigachev }
2249b843c749SSergey Zigachev 
2250b843c749SSergey Zigachev /**
2251b843c749SSergey Zigachev  * amdgpu_vm_bo_unmap - remove bo mapping from vm
2252b843c749SSergey Zigachev  *
2253b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2254b843c749SSergey Zigachev  * @bo_va: bo_va to remove the address from
2255b843c749SSergey Zigachev  * @saddr: where to the BO is mapped
2256b843c749SSergey Zigachev  *
2257b843c749SSergey Zigachev  * Remove a mapping of the BO at the specefied addr from the VM.
2258b843c749SSergey Zigachev  *
2259b843c749SSergey Zigachev  * Returns:
2260b843c749SSergey Zigachev  * 0 for success, error for failure.
2261b843c749SSergey Zigachev  *
2262b843c749SSergey Zigachev  * Object has to be reserved and unreserved outside!
2263b843c749SSergey Zigachev  */
amdgpu_vm_bo_unmap(struct amdgpu_device * adev,struct amdgpu_bo_va * bo_va,uint64_t saddr)2264b843c749SSergey Zigachev int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2265b843c749SSergey Zigachev 		       struct amdgpu_bo_va *bo_va,
2266b843c749SSergey Zigachev 		       uint64_t saddr)
2267b843c749SSergey Zigachev {
2268b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping;
2269b843c749SSergey Zigachev 	struct amdgpu_vm *vm = bo_va->base.vm;
2270b843c749SSergey Zigachev 	bool valid = true;
2271b843c749SSergey Zigachev 
2272b843c749SSergey Zigachev 	saddr /= AMDGPU_GPU_PAGE_SIZE;
2273b843c749SSergey Zigachev 
2274b843c749SSergey Zigachev 	list_for_each_entry(mapping, &bo_va->valids, list) {
2275b843c749SSergey Zigachev 		if (mapping->start == saddr)
2276b843c749SSergey Zigachev 			break;
2277b843c749SSergey Zigachev 	}
2278b843c749SSergey Zigachev 
2279b843c749SSergey Zigachev 	if (&mapping->list == &bo_va->valids) {
2280b843c749SSergey Zigachev 		valid = false;
2281b843c749SSergey Zigachev 
2282b843c749SSergey Zigachev 		list_for_each_entry(mapping, &bo_va->invalids, list) {
2283b843c749SSergey Zigachev 			if (mapping->start == saddr)
2284b843c749SSergey Zigachev 				break;
2285b843c749SSergey Zigachev 		}
2286b843c749SSergey Zigachev 
2287b843c749SSergey Zigachev 		if (&mapping->list == &bo_va->invalids)
2288b843c749SSergey Zigachev 			return -ENOENT;
2289b843c749SSergey Zigachev 	}
2290b843c749SSergey Zigachev 
2291b843c749SSergey Zigachev 	list_del(&mapping->list);
2292b843c749SSergey Zigachev 	amdgpu_vm_it_remove(mapping, &vm->va);
2293b843c749SSergey Zigachev 	mapping->bo_va = NULL;
2294b843c749SSergey Zigachev 	trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2295b843c749SSergey Zigachev 
2296b843c749SSergey Zigachev 	if (valid)
2297b843c749SSergey Zigachev 		list_add(&mapping->list, &vm->freed);
2298b843c749SSergey Zigachev 	else
2299b843c749SSergey Zigachev 		amdgpu_vm_free_mapping(adev, vm, mapping,
2300b843c749SSergey Zigachev 				       bo_va->last_pt_update);
2301b843c749SSergey Zigachev 
2302b843c749SSergey Zigachev 	return 0;
2303b843c749SSergey Zigachev }
2304b843c749SSergey Zigachev 
2305b843c749SSergey Zigachev /**
2306b843c749SSergey Zigachev  * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2307b843c749SSergey Zigachev  *
2308b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2309b843c749SSergey Zigachev  * @vm: VM structure to use
2310b843c749SSergey Zigachev  * @saddr: start of the range
2311b843c749SSergey Zigachev  * @size: size of the range
2312b843c749SSergey Zigachev  *
2313b843c749SSergey Zigachev  * Remove all mappings in a range, split them as appropriate.
2314b843c749SSergey Zigachev  *
2315b843c749SSergey Zigachev  * Returns:
2316b843c749SSergey Zigachev  * 0 for success, error for failure.
2317b843c749SSergey Zigachev  */
amdgpu_vm_bo_clear_mappings(struct amdgpu_device * adev,struct amdgpu_vm * vm,uint64_t saddr,uint64_t size)2318b843c749SSergey Zigachev int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2319b843c749SSergey Zigachev 				struct amdgpu_vm *vm,
2320b843c749SSergey Zigachev 				uint64_t saddr, uint64_t size)
2321b843c749SSergey Zigachev {
2322b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
232378973132SSergey Zigachev 	DRM_LIST_HEAD(removed);
2324b843c749SSergey Zigachev 	uint64_t eaddr;
2325b843c749SSergey Zigachev 
2326b843c749SSergey Zigachev 	eaddr = saddr + size - 1;
2327b843c749SSergey Zigachev 	saddr /= AMDGPU_GPU_PAGE_SIZE;
2328b843c749SSergey Zigachev 	eaddr /= AMDGPU_GPU_PAGE_SIZE;
2329b843c749SSergey Zigachev 
2330b843c749SSergey Zigachev 	/* Allocate all the needed memory */
2331b843c749SSergey Zigachev 	before = kzalloc(sizeof(*before), GFP_KERNEL);
2332b843c749SSergey Zigachev 	if (!before)
2333b843c749SSergey Zigachev 		return -ENOMEM;
2334b843c749SSergey Zigachev 	INIT_LIST_HEAD(&before->list);
2335b843c749SSergey Zigachev 
2336b843c749SSergey Zigachev 	after = kzalloc(sizeof(*after), GFP_KERNEL);
2337b843c749SSergey Zigachev 	if (!after) {
2338b843c749SSergey Zigachev 		kfree(before);
2339b843c749SSergey Zigachev 		return -ENOMEM;
2340b843c749SSergey Zigachev 	}
2341b843c749SSergey Zigachev 	INIT_LIST_HEAD(&after->list);
2342b843c749SSergey Zigachev 
2343b843c749SSergey Zigachev 	/* Now gather all removed mappings */
2344b843c749SSergey Zigachev 	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2345b843c749SSergey Zigachev 	while (tmp) {
2346b843c749SSergey Zigachev 		/* Remember mapping split at the start */
2347b843c749SSergey Zigachev 		if (tmp->start < saddr) {
2348b843c749SSergey Zigachev 			before->start = tmp->start;
2349b843c749SSergey Zigachev 			before->last = saddr - 1;
2350b843c749SSergey Zigachev 			before->offset = tmp->offset;
2351b843c749SSergey Zigachev 			before->flags = tmp->flags;
2352b843c749SSergey Zigachev 			before->bo_va = tmp->bo_va;
2353b843c749SSergey Zigachev 			list_add(&before->list, &tmp->bo_va->invalids);
2354b843c749SSergey Zigachev 		}
2355b843c749SSergey Zigachev 
2356b843c749SSergey Zigachev 		/* Remember mapping split at the end */
2357b843c749SSergey Zigachev 		if (tmp->last > eaddr) {
2358b843c749SSergey Zigachev 			after->start = eaddr + 1;
2359b843c749SSergey Zigachev 			after->last = tmp->last;
2360b843c749SSergey Zigachev 			after->offset = tmp->offset;
2361b843c749SSergey Zigachev 			after->offset += (after->start - tmp->start) << PAGE_SHIFT;
2362b843c749SSergey Zigachev 			after->flags = tmp->flags;
2363b843c749SSergey Zigachev 			after->bo_va = tmp->bo_va;
2364b843c749SSergey Zigachev 			list_add(&after->list, &tmp->bo_va->invalids);
2365b843c749SSergey Zigachev 		}
2366b843c749SSergey Zigachev 
2367b843c749SSergey Zigachev 		list_del(&tmp->list);
2368b843c749SSergey Zigachev 		list_add(&tmp->list, &removed);
2369b843c749SSergey Zigachev 
2370b843c749SSergey Zigachev 		tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
2371b843c749SSergey Zigachev 	}
2372b843c749SSergey Zigachev 
2373b843c749SSergey Zigachev 	/* And free them up */
2374b843c749SSergey Zigachev 	list_for_each_entry_safe(tmp, next, &removed, list) {
2375b843c749SSergey Zigachev 		amdgpu_vm_it_remove(tmp, &vm->va);
2376b843c749SSergey Zigachev 		list_del(&tmp->list);
2377b843c749SSergey Zigachev 
2378b843c749SSergey Zigachev 		if (tmp->start < saddr)
2379b843c749SSergey Zigachev 		    tmp->start = saddr;
2380b843c749SSergey Zigachev 		if (tmp->last > eaddr)
2381b843c749SSergey Zigachev 		    tmp->last = eaddr;
2382b843c749SSergey Zigachev 
2383b843c749SSergey Zigachev 		tmp->bo_va = NULL;
2384b843c749SSergey Zigachev 		list_add(&tmp->list, &vm->freed);
2385b843c749SSergey Zigachev 		trace_amdgpu_vm_bo_unmap(NULL, tmp);
2386b843c749SSergey Zigachev 	}
2387b843c749SSergey Zigachev 
2388b843c749SSergey Zigachev 	/* Insert partial mapping before the range */
2389b843c749SSergey Zigachev 	if (!list_empty(&before->list)) {
2390b843c749SSergey Zigachev 		amdgpu_vm_it_insert(before, &vm->va);
2391b843c749SSergey Zigachev 		if (before->flags & AMDGPU_PTE_PRT)
2392b843c749SSergey Zigachev 			amdgpu_vm_prt_get(adev);
2393b843c749SSergey Zigachev 	} else {
2394b843c749SSergey Zigachev 		kfree(before);
2395b843c749SSergey Zigachev 	}
2396b843c749SSergey Zigachev 
2397b843c749SSergey Zigachev 	/* Insert partial mapping after the range */
2398b843c749SSergey Zigachev 	if (!list_empty(&after->list)) {
2399b843c749SSergey Zigachev 		amdgpu_vm_it_insert(after, &vm->va);
2400b843c749SSergey Zigachev 		if (after->flags & AMDGPU_PTE_PRT)
2401b843c749SSergey Zigachev 			amdgpu_vm_prt_get(adev);
2402b843c749SSergey Zigachev 	} else {
2403b843c749SSergey Zigachev 		kfree(after);
2404b843c749SSergey Zigachev 	}
2405b843c749SSergey Zigachev 
2406b843c749SSergey Zigachev 	return 0;
2407b843c749SSergey Zigachev }
2408b843c749SSergey Zigachev 
2409b843c749SSergey Zigachev /**
2410b843c749SSergey Zigachev  * amdgpu_vm_bo_lookup_mapping - find mapping by address
2411b843c749SSergey Zigachev  *
2412b843c749SSergey Zigachev  * @vm: the requested VM
2413b843c749SSergey Zigachev  * @addr: the address
2414b843c749SSergey Zigachev  *
2415b843c749SSergey Zigachev  * Find a mapping by it's address.
2416b843c749SSergey Zigachev  *
2417b843c749SSergey Zigachev  * Returns:
2418b843c749SSergey Zigachev  * The amdgpu_bo_va_mapping matching for addr or NULL
2419b843c749SSergey Zigachev  *
2420b843c749SSergey Zigachev  */
amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm * vm,uint64_t addr)2421b843c749SSergey Zigachev struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2422b843c749SSergey Zigachev 							 uint64_t addr)
2423b843c749SSergey Zigachev {
2424b843c749SSergey Zigachev 	return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2425b843c749SSergey Zigachev }
2426b843c749SSergey Zigachev 
2427b843c749SSergey Zigachev /**
2428b843c749SSergey Zigachev  * amdgpu_vm_bo_trace_cs - trace all reserved mappings
2429b843c749SSergey Zigachev  *
2430b843c749SSergey Zigachev  * @vm: the requested vm
2431b843c749SSergey Zigachev  * @ticket: CS ticket
2432b843c749SSergey Zigachev  *
2433b843c749SSergey Zigachev  * Trace all mappings of BOs reserved during a command submission.
2434b843c749SSergey Zigachev  */
amdgpu_vm_bo_trace_cs(struct amdgpu_vm * vm,struct ww_acquire_ctx * ticket)2435b843c749SSergey Zigachev void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket)
2436b843c749SSergey Zigachev {
243778973132SSergey Zigachev #if 0
2438b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping;
2439b843c749SSergey Zigachev 
2440b843c749SSergey Zigachev 	if (!trace_amdgpu_vm_bo_cs_enabled())
2441b843c749SSergey Zigachev 		return;
2442b843c749SSergey Zigachev 
2443b843c749SSergey Zigachev 	for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping;
2444b843c749SSergey Zigachev 	     mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) {
2445b843c749SSergey Zigachev 		if (mapping->bo_va && mapping->bo_va->base.bo) {
2446b843c749SSergey Zigachev 			struct amdgpu_bo *bo;
2447b843c749SSergey Zigachev 
2448b843c749SSergey Zigachev 			bo = mapping->bo_va->base.bo;
2449b843c749SSergey Zigachev 			if (READ_ONCE(bo->tbo.resv->lock.ctx) != ticket)
2450b843c749SSergey Zigachev 				continue;
2451b843c749SSergey Zigachev 		}
2452b843c749SSergey Zigachev 
2453b843c749SSergey Zigachev 		trace_amdgpu_vm_bo_cs(mapping);
2454b843c749SSergey Zigachev 	}
245578973132SSergey Zigachev #endif
2456b843c749SSergey Zigachev }
2457b843c749SSergey Zigachev 
2458b843c749SSergey Zigachev /**
2459b843c749SSergey Zigachev  * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2460b843c749SSergey Zigachev  *
2461b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2462b843c749SSergey Zigachev  * @bo_va: requested bo_va
2463b843c749SSergey Zigachev  *
2464b843c749SSergey Zigachev  * Remove @bo_va->bo from the requested vm.
2465b843c749SSergey Zigachev  *
2466b843c749SSergey Zigachev  * Object have to be reserved!
2467b843c749SSergey Zigachev  */
amdgpu_vm_bo_rmv(struct amdgpu_device * adev,struct amdgpu_bo_va * bo_va)2468b843c749SSergey Zigachev void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2469b843c749SSergey Zigachev 		      struct amdgpu_bo_va *bo_va)
2470b843c749SSergey Zigachev {
2471b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping, *next;
2472b843c749SSergey Zigachev 	struct amdgpu_vm *vm = bo_va->base.vm;
2473b843c749SSergey Zigachev 
2474b843c749SSergey Zigachev 	list_del(&bo_va->base.bo_list);
2475b843c749SSergey Zigachev 
247678973132SSergey Zigachev 	lockmgr(&vm->moved_lock, LK_EXCLUSIVE);
2477b843c749SSergey Zigachev 	list_del(&bo_va->base.vm_status);
247878973132SSergey Zigachev 	lockmgr(&vm->moved_lock, LK_RELEASE);
2479b843c749SSergey Zigachev 
2480b843c749SSergey Zigachev 	list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
2481b843c749SSergey Zigachev 		list_del(&mapping->list);
2482b843c749SSergey Zigachev 		amdgpu_vm_it_remove(mapping, &vm->va);
2483b843c749SSergey Zigachev 		mapping->bo_va = NULL;
2484b843c749SSergey Zigachev 		trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2485b843c749SSergey Zigachev 		list_add(&mapping->list, &vm->freed);
2486b843c749SSergey Zigachev 	}
2487b843c749SSergey Zigachev 	list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2488b843c749SSergey Zigachev 		list_del(&mapping->list);
2489b843c749SSergey Zigachev 		amdgpu_vm_it_remove(mapping, &vm->va);
2490b843c749SSergey Zigachev 		amdgpu_vm_free_mapping(adev, vm, mapping,
2491b843c749SSergey Zigachev 				       bo_va->last_pt_update);
2492b843c749SSergey Zigachev 	}
2493b843c749SSergey Zigachev 
2494b843c749SSergey Zigachev 	dma_fence_put(bo_va->last_pt_update);
2495b843c749SSergey Zigachev 	kfree(bo_va);
2496b843c749SSergey Zigachev }
2497b843c749SSergey Zigachev 
2498b843c749SSergey Zigachev /**
2499b843c749SSergey Zigachev  * amdgpu_vm_bo_invalidate - mark the bo as invalid
2500b843c749SSergey Zigachev  *
2501b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2502b843c749SSergey Zigachev  * @bo: amdgpu buffer object
2503b843c749SSergey Zigachev  * @evicted: is the BO evicted
2504b843c749SSergey Zigachev  *
2505b843c749SSergey Zigachev  * Mark @bo as invalid.
2506b843c749SSergey Zigachev  */
amdgpu_vm_bo_invalidate(struct amdgpu_device * adev,struct amdgpu_bo * bo,bool evicted)2507b843c749SSergey Zigachev void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
2508b843c749SSergey Zigachev 			     struct amdgpu_bo *bo, bool evicted)
2509b843c749SSergey Zigachev {
2510b843c749SSergey Zigachev 	struct amdgpu_vm_bo_base *bo_base;
2511b843c749SSergey Zigachev 
2512b843c749SSergey Zigachev 	/* shadow bo doesn't have bo base, its validation needs its parent */
2513b843c749SSergey Zigachev 	if (bo->parent && bo->parent->shadow == bo)
2514b843c749SSergey Zigachev 		bo = bo->parent;
2515b843c749SSergey Zigachev 
2516b843c749SSergey Zigachev 	list_for_each_entry(bo_base, &bo->va, bo_list) {
2517b843c749SSergey Zigachev 		struct amdgpu_vm *vm = bo_base->vm;
2518b843c749SSergey Zigachev 		bool was_moved = bo_base->moved;
2519b843c749SSergey Zigachev 
2520b843c749SSergey Zigachev 		bo_base->moved = true;
2521b843c749SSergey Zigachev 		if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2522b843c749SSergey Zigachev 			if (bo->tbo.type == ttm_bo_type_kernel)
2523b843c749SSergey Zigachev 				list_move(&bo_base->vm_status, &vm->evicted);
2524b843c749SSergey Zigachev 			else
2525b843c749SSergey Zigachev 				list_move_tail(&bo_base->vm_status,
2526b843c749SSergey Zigachev 					       &vm->evicted);
2527b843c749SSergey Zigachev 			continue;
2528b843c749SSergey Zigachev 		}
2529b843c749SSergey Zigachev 
2530b843c749SSergey Zigachev 		if (was_moved)
2531b843c749SSergey Zigachev 			continue;
2532b843c749SSergey Zigachev 
2533b843c749SSergey Zigachev 		if (bo->tbo.type == ttm_bo_type_kernel) {
2534b843c749SSergey Zigachev 			list_move(&bo_base->vm_status, &vm->relocated);
2535b843c749SSergey Zigachev 		} else {
253678973132SSergey Zigachev 			lockmgr(&bo_base->vm->moved_lock, LK_EXCLUSIVE);
2537b843c749SSergey Zigachev 			list_move(&bo_base->vm_status, &vm->moved);
253878973132SSergey Zigachev 			lockmgr(&bo_base->vm->moved_lock, LK_RELEASE);
2539b843c749SSergey Zigachev 		}
2540b843c749SSergey Zigachev 	}
2541b843c749SSergey Zigachev }
2542b843c749SSergey Zigachev 
2543b843c749SSergey Zigachev /**
2544b843c749SSergey Zigachev  * amdgpu_vm_get_block_size - calculate VM page table size as power of two
2545b843c749SSergey Zigachev  *
2546b843c749SSergey Zigachev  * @vm_size: VM size
2547b843c749SSergey Zigachev  *
2548b843c749SSergey Zigachev  * Returns:
2549b843c749SSergey Zigachev  * VM page table as power of two
2550b843c749SSergey Zigachev  */
amdgpu_vm_get_block_size(uint64_t vm_size)2551b843c749SSergey Zigachev static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2552b843c749SSergey Zigachev {
2553b843c749SSergey Zigachev 	/* Total bits covered by PD + PTs */
2554b843c749SSergey Zigachev 	unsigned bits = ilog2(vm_size) + 18;
2555b843c749SSergey Zigachev 
2556b843c749SSergey Zigachev 	/* Make sure the PD is 4K in size up to 8GB address space.
2557b843c749SSergey Zigachev 	   Above that split equal between PD and PTs */
2558b843c749SSergey Zigachev 	if (vm_size <= 8)
2559b843c749SSergey Zigachev 		return (bits - 9);
2560b843c749SSergey Zigachev 	else
2561b843c749SSergey Zigachev 		return ((bits + 3) / 2);
2562b843c749SSergey Zigachev }
2563b843c749SSergey Zigachev 
2564b843c749SSergey Zigachev /**
2565b843c749SSergey Zigachev  * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
2566b843c749SSergey Zigachev  *
2567b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2568b843c749SSergey Zigachev  * @min_vm_size: the minimum vm size in GB if it's set auto
2569b843c749SSergey Zigachev  * @fragment_size_default: Default PTE fragment size
2570b843c749SSergey Zigachev  * @max_level: max VMPT level
2571b843c749SSergey Zigachev  * @max_bits: max address space size in bits
2572b843c749SSergey Zigachev  *
2573b843c749SSergey Zigachev  */
amdgpu_vm_adjust_size(struct amdgpu_device * adev,uint32_t min_vm_size,uint32_t fragment_size_default,unsigned max_level,unsigned max_bits)2574b843c749SSergey Zigachev void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
2575b843c749SSergey Zigachev 			   uint32_t fragment_size_default, unsigned max_level,
2576b843c749SSergey Zigachev 			   unsigned max_bits)
2577b843c749SSergey Zigachev {
2578b843c749SSergey Zigachev 	unsigned int max_size = 1 << (max_bits - 30);
2579b843c749SSergey Zigachev 	unsigned int vm_size;
2580b843c749SSergey Zigachev 	uint64_t tmp;
2581b843c749SSergey Zigachev 
2582b843c749SSergey Zigachev 	/* adjust vm size first */
2583b843c749SSergey Zigachev 	if (amdgpu_vm_size != -1) {
2584b843c749SSergey Zigachev 		vm_size = amdgpu_vm_size;
2585b843c749SSergey Zigachev 		if (vm_size > max_size) {
2586b843c749SSergey Zigachev 			dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2587b843c749SSergey Zigachev 				 amdgpu_vm_size, max_size);
2588b843c749SSergey Zigachev 			vm_size = max_size;
2589b843c749SSergey Zigachev 		}
2590b843c749SSergey Zigachev 	} else {
2591b843c749SSergey Zigachev 		struct sysinfo si;
2592b843c749SSergey Zigachev 		unsigned int phys_ram_gb;
2593b843c749SSergey Zigachev 
2594b843c749SSergey Zigachev 		/* Optimal VM size depends on the amount of physical
2595b843c749SSergey Zigachev 		 * RAM available. Underlying requirements and
2596b843c749SSergey Zigachev 		 * assumptions:
2597b843c749SSergey Zigachev 		 *
2598b843c749SSergey Zigachev 		 *  - Need to map system memory and VRAM from all GPUs
2599b843c749SSergey Zigachev 		 *     - VRAM from other GPUs not known here
2600b843c749SSergey Zigachev 		 *     - Assume VRAM <= system memory
2601b843c749SSergey Zigachev 		 *  - On GFX8 and older, VM space can be segmented for
2602b843c749SSergey Zigachev 		 *    different MTYPEs
2603b843c749SSergey Zigachev 		 *  - Need to allow room for fragmentation, guard pages etc.
2604b843c749SSergey Zigachev 		 *
2605b843c749SSergey Zigachev 		 * This adds up to a rough guess of system memory x3.
2606b843c749SSergey Zigachev 		 * Round up to power of two to maximize the available
2607b843c749SSergey Zigachev 		 * VM size with the given page table size.
2608b843c749SSergey Zigachev 		 */
2609b843c749SSergey Zigachev 		si_meminfo(&si);
2610b843c749SSergey Zigachev 		phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit +
2611b843c749SSergey Zigachev 			       (1 << 30) - 1) >> 30;
2612b843c749SSergey Zigachev 		vm_size = roundup_pow_of_two(
2613b843c749SSergey Zigachev 			min(max(phys_ram_gb * 3, min_vm_size), max_size));
2614b843c749SSergey Zigachev 	}
2615b843c749SSergey Zigachev 
2616b843c749SSergey Zigachev 	adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
2617b843c749SSergey Zigachev 
2618b843c749SSergey Zigachev 	tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
2619b843c749SSergey Zigachev 	if (amdgpu_vm_block_size != -1)
2620b843c749SSergey Zigachev 		tmp >>= amdgpu_vm_block_size - 9;
2621b843c749SSergey Zigachev 	tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2622b843c749SSergey Zigachev 	adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
2623b843c749SSergey Zigachev 	switch (adev->vm_manager.num_level) {
2624b843c749SSergey Zigachev 	case 3:
2625b843c749SSergey Zigachev 		adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2626b843c749SSergey Zigachev 		break;
2627b843c749SSergey Zigachev 	case 2:
2628b843c749SSergey Zigachev 		adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2629b843c749SSergey Zigachev 		break;
2630b843c749SSergey Zigachev 	case 1:
2631b843c749SSergey Zigachev 		adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2632b843c749SSergey Zigachev 		break;
2633b843c749SSergey Zigachev 	default:
2634b843c749SSergey Zigachev 		dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2635b843c749SSergey Zigachev 	}
2636b843c749SSergey Zigachev 	/* block size depends on vm size and hw setup*/
2637b843c749SSergey Zigachev 	if (amdgpu_vm_block_size != -1)
2638b843c749SSergey Zigachev 		adev->vm_manager.block_size =
2639b843c749SSergey Zigachev 			min((unsigned)amdgpu_vm_block_size, max_bits
2640b843c749SSergey Zigachev 			    - AMDGPU_GPU_PAGE_SHIFT
2641b843c749SSergey Zigachev 			    - 9 * adev->vm_manager.num_level);
2642b843c749SSergey Zigachev 	else if (adev->vm_manager.num_level > 1)
2643b843c749SSergey Zigachev 		adev->vm_manager.block_size = 9;
2644b843c749SSergey Zigachev 	else
2645b843c749SSergey Zigachev 		adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
2646b843c749SSergey Zigachev 
2647b843c749SSergey Zigachev 	if (amdgpu_vm_fragment_size == -1)
2648b843c749SSergey Zigachev 		adev->vm_manager.fragment_size = fragment_size_default;
2649b843c749SSergey Zigachev 	else
2650b843c749SSergey Zigachev 		adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2651b843c749SSergey Zigachev 
2652b843c749SSergey Zigachev 	DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2653b843c749SSergey Zigachev 		 vm_size, adev->vm_manager.num_level + 1,
2654b843c749SSergey Zigachev 		 adev->vm_manager.block_size,
2655b843c749SSergey Zigachev 		 adev->vm_manager.fragment_size);
2656b843c749SSergey Zigachev }
2657b843c749SSergey Zigachev 
2658b843c749SSergey Zigachev /**
2659b843c749SSergey Zigachev  * amdgpu_vm_init - initialize a vm instance
2660b843c749SSergey Zigachev  *
2661b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2662b843c749SSergey Zigachev  * @vm: requested vm
2663b843c749SSergey Zigachev  * @vm_context: Indicates if it GFX or Compute context
2664b843c749SSergey Zigachev  * @pasid: Process address space identifier
2665b843c749SSergey Zigachev  *
2666b843c749SSergey Zigachev  * Init @vm fields.
2667b843c749SSergey Zigachev  *
2668b843c749SSergey Zigachev  * Returns:
2669b843c749SSergey Zigachev  * 0 for success, error for failure.
2670b843c749SSergey Zigachev  */
amdgpu_vm_init(struct amdgpu_device * adev,struct amdgpu_vm * vm,int vm_context,unsigned int pasid)2671b843c749SSergey Zigachev int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2672b843c749SSergey Zigachev 		   int vm_context, unsigned int pasid)
2673b843c749SSergey Zigachev {
2674b843c749SSergey Zigachev 	struct amdgpu_bo_param bp;
2675b843c749SSergey Zigachev 	struct amdgpu_bo *root;
2676b843c749SSergey Zigachev 	const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
2677b843c749SSergey Zigachev 		AMDGPU_VM_PTE_COUNT(adev) * 8);
2678b843c749SSergey Zigachev 	unsigned ring_instance;
2679b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
2680b843c749SSergey Zigachev 	struct drm_sched_rq *rq;
2681b843c749SSergey Zigachev 	unsigned long size;
2682b843c749SSergey Zigachev 	uint64_t flags;
2683b843c749SSergey Zigachev 	int r, i;
2684b843c749SSergey Zigachev 
268578973132SSergey Zigachev 	vm->va = LINUX_RB_ROOT_CACHED;
2686b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2687b843c749SSergey Zigachev 		vm->reserved_vmid[i] = NULL;
2688b843c749SSergey Zigachev 	INIT_LIST_HEAD(&vm->evicted);
2689b843c749SSergey Zigachev 	INIT_LIST_HEAD(&vm->relocated);
269078973132SSergey Zigachev 	lockinit(&vm->moved_lock, "agvmml", 0, LK_CANRECURSE);
2691b843c749SSergey Zigachev 	INIT_LIST_HEAD(&vm->moved);
2692b843c749SSergey Zigachev 	INIT_LIST_HEAD(&vm->idle);
2693b843c749SSergey Zigachev 	INIT_LIST_HEAD(&vm->freed);
2694b843c749SSergey Zigachev 
2695b843c749SSergey Zigachev 	/* create scheduler entity for page table updates */
2696b843c749SSergey Zigachev 
2697b843c749SSergey Zigachev 	ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2698b843c749SSergey Zigachev 	ring_instance %= adev->vm_manager.vm_pte_num_rings;
2699b843c749SSergey Zigachev 	ring = adev->vm_manager.vm_pte_rings[ring_instance];
2700b843c749SSergey Zigachev 	rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2701b843c749SSergey Zigachev 	r = drm_sched_entity_init(&vm->entity, &rq, 1, NULL);
2702b843c749SSergey Zigachev 	if (r)
2703b843c749SSergey Zigachev 		return r;
2704b843c749SSergey Zigachev 
2705b843c749SSergey Zigachev 	vm->pte_support_ats = false;
2706b843c749SSergey Zigachev 
2707b843c749SSergey Zigachev 	if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
2708b843c749SSergey Zigachev 		vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2709b843c749SSergey Zigachev 						AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2710b843c749SSergey Zigachev 
2711b843c749SSergey Zigachev 		if (adev->asic_type == CHIP_RAVEN)
2712b843c749SSergey Zigachev 			vm->pte_support_ats = true;
2713b843c749SSergey Zigachev 	} else {
2714b843c749SSergey Zigachev 		vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2715b843c749SSergey Zigachev 						AMDGPU_VM_USE_CPU_FOR_GFX);
2716b843c749SSergey Zigachev 	}
2717b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("VM update mode is %s\n",
2718b843c749SSergey Zigachev 			 vm->use_cpu_for_update ? "CPU" : "SDMA");
2719b843c749SSergey Zigachev 	WARN_ONCE((vm->use_cpu_for_update & !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2720b843c749SSergey Zigachev 		  "CPU update of VM recommended only for large BAR system\n");
2721b843c749SSergey Zigachev 	vm->last_update = NULL;
2722b843c749SSergey Zigachev 
2723b843c749SSergey Zigachev 	flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
2724b843c749SSergey Zigachev 	if (vm->use_cpu_for_update)
2725b843c749SSergey Zigachev 		flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2726b843c749SSergey Zigachev 	else if (vm_context != AMDGPU_VM_CONTEXT_COMPUTE)
2727b843c749SSergey Zigachev 		flags |= AMDGPU_GEM_CREATE_SHADOW;
2728b843c749SSergey Zigachev 
2729b843c749SSergey Zigachev 	size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
2730b843c749SSergey Zigachev 	memset(&bp, 0, sizeof(bp));
2731b843c749SSergey Zigachev 	bp.size = size;
2732b843c749SSergey Zigachev 	bp.byte_align = align;
2733b843c749SSergey Zigachev 	bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
2734b843c749SSergey Zigachev 	bp.flags = flags;
2735b843c749SSergey Zigachev 	bp.type = ttm_bo_type_kernel;
2736b843c749SSergey Zigachev 	bp.resv = NULL;
2737b843c749SSergey Zigachev 	r = amdgpu_bo_create(adev, &bp, &root);
2738b843c749SSergey Zigachev 	if (r)
2739b843c749SSergey Zigachev 		goto error_free_sched_entity;
2740b843c749SSergey Zigachev 
2741b843c749SSergey Zigachev 	r = amdgpu_bo_reserve(root, true);
2742b843c749SSergey Zigachev 	if (r)
2743b843c749SSergey Zigachev 		goto error_free_root;
2744b843c749SSergey Zigachev 
2745b843c749SSergey Zigachev 	r = amdgpu_vm_clear_bo(adev, vm, root,
2746b843c749SSergey Zigachev 			       adev->vm_manager.root_level,
2747b843c749SSergey Zigachev 			       vm->pte_support_ats);
2748b843c749SSergey Zigachev 	if (r)
2749b843c749SSergey Zigachev 		goto error_unreserve;
2750b843c749SSergey Zigachev 
2751b843c749SSergey Zigachev 	amdgpu_vm_bo_base_init(&vm->root.base, vm, root);
2752b843c749SSergey Zigachev 	amdgpu_bo_unreserve(vm->root.base.bo);
2753b843c749SSergey Zigachev 
2754b843c749SSergey Zigachev 	if (pasid) {
2755b843c749SSergey Zigachev 		unsigned long flags;
2756b843c749SSergey Zigachev 
2757b843c749SSergey Zigachev 		spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2758*1f4dfa92SMatthew Dillon 		r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2759b843c749SSergey Zigachev 			      GFP_ATOMIC);
2760b843c749SSergey Zigachev 		spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2761b843c749SSergey Zigachev 		if (r < 0)
2762b843c749SSergey Zigachev 			goto error_free_root;
2763b843c749SSergey Zigachev 
2764b843c749SSergey Zigachev 		vm->pasid = pasid;
2765b843c749SSergey Zigachev 	}
2766b843c749SSergey Zigachev 
276778973132SSergey Zigachev #if 0
2768b843c749SSergey Zigachev 	INIT_KFIFO(vm->faults);
276978973132SSergey Zigachev #endif
2770b843c749SSergey Zigachev 	vm->fault_credit = 16;
2771b843c749SSergey Zigachev 
2772b843c749SSergey Zigachev 	return 0;
2773b843c749SSergey Zigachev 
2774b843c749SSergey Zigachev error_unreserve:
2775b843c749SSergey Zigachev 	amdgpu_bo_unreserve(vm->root.base.bo);
2776b843c749SSergey Zigachev 
2777b843c749SSergey Zigachev error_free_root:
2778b843c749SSergey Zigachev 	amdgpu_bo_unref(&vm->root.base.bo->shadow);
2779b843c749SSergey Zigachev 	amdgpu_bo_unref(&vm->root.base.bo);
2780b843c749SSergey Zigachev 	vm->root.base.bo = NULL;
2781b843c749SSergey Zigachev 
2782b843c749SSergey Zigachev error_free_sched_entity:
2783b843c749SSergey Zigachev 	drm_sched_entity_destroy(&vm->entity);
2784b843c749SSergey Zigachev 
2785b843c749SSergey Zigachev 	return r;
2786b843c749SSergey Zigachev }
2787b843c749SSergey Zigachev 
2788b843c749SSergey Zigachev /**
2789b843c749SSergey Zigachev  * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2790b843c749SSergey Zigachev  *
2791b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2792b843c749SSergey Zigachev  * @vm: requested vm
2793b843c749SSergey Zigachev  *
2794b843c749SSergey Zigachev  * This only works on GFX VMs that don't have any BOs added and no
2795b843c749SSergey Zigachev  * page tables allocated yet.
2796b843c749SSergey Zigachev  *
2797b843c749SSergey Zigachev  * Changes the following VM parameters:
2798b843c749SSergey Zigachev  * - use_cpu_for_update
2799b843c749SSergey Zigachev  * - pte_supports_ats
2800b843c749SSergey Zigachev  * - pasid (old PASID is released, because compute manages its own PASIDs)
2801b843c749SSergey Zigachev  *
2802b843c749SSergey Zigachev  * Reinitializes the page directory to reflect the changed ATS
2803b843c749SSergey Zigachev  * setting.
2804b843c749SSergey Zigachev  *
2805b843c749SSergey Zigachev  * Returns:
2806b843c749SSergey Zigachev  * 0 for success, -errno for errors.
2807b843c749SSergey Zigachev  */
amdgpu_vm_make_compute(struct amdgpu_device * adev,struct amdgpu_vm * vm)2808b843c749SSergey Zigachev int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2809b843c749SSergey Zigachev {
2810b843c749SSergey Zigachev 	bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2811b843c749SSergey Zigachev 	int r;
2812b843c749SSergey Zigachev 
2813b843c749SSergey Zigachev 	r = amdgpu_bo_reserve(vm->root.base.bo, true);
2814b843c749SSergey Zigachev 	if (r)
2815b843c749SSergey Zigachev 		return r;
2816b843c749SSergey Zigachev 
2817b843c749SSergey Zigachev 	/* Sanity checks */
2818b843c749SSergey Zigachev 	if (!RB_EMPTY_ROOT(&vm->va.rb_root) || vm->root.entries) {
2819b843c749SSergey Zigachev 		r = -EINVAL;
2820b843c749SSergey Zigachev 		goto error;
2821b843c749SSergey Zigachev 	}
2822b843c749SSergey Zigachev 
2823b843c749SSergey Zigachev 	/* Check if PD needs to be reinitialized and do it before
2824b843c749SSergey Zigachev 	 * changing any other state, in case it fails.
2825b843c749SSergey Zigachev 	 */
2826b843c749SSergey Zigachev 	if (pte_support_ats != vm->pte_support_ats) {
2827b843c749SSergey Zigachev 		r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
2828b843c749SSergey Zigachev 			       adev->vm_manager.root_level,
2829b843c749SSergey Zigachev 			       pte_support_ats);
2830b843c749SSergey Zigachev 		if (r)
2831b843c749SSergey Zigachev 			goto error;
2832b843c749SSergey Zigachev 	}
2833b843c749SSergey Zigachev 
2834b843c749SSergey Zigachev 	/* Update VM state */
2835b843c749SSergey Zigachev 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2836b843c749SSergey Zigachev 				    AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2837b843c749SSergey Zigachev 	vm->pte_support_ats = pte_support_ats;
2838b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("VM update mode is %s\n",
2839b843c749SSergey Zigachev 			 vm->use_cpu_for_update ? "CPU" : "SDMA");
2840b843c749SSergey Zigachev 	WARN_ONCE((vm->use_cpu_for_update & !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2841b843c749SSergey Zigachev 		  "CPU update of VM recommended only for large BAR system\n");
2842b843c749SSergey Zigachev 
2843b843c749SSergey Zigachev 	if (vm->pasid) {
2844b843c749SSergey Zigachev 		unsigned long flags;
2845b843c749SSergey Zigachev 
2846b843c749SSergey Zigachev 		spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2847b843c749SSergey Zigachev 		idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2848b843c749SSergey Zigachev 		spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2849b843c749SSergey Zigachev 
2850b843c749SSergey Zigachev 		vm->pasid = 0;
2851b843c749SSergey Zigachev 	}
2852b843c749SSergey Zigachev 
2853b843c749SSergey Zigachev 	/* Free the shadow bo for compute VM */
2854b843c749SSergey Zigachev 	amdgpu_bo_unref(&vm->root.base.bo->shadow);
2855b843c749SSergey Zigachev 
2856b843c749SSergey Zigachev error:
2857b843c749SSergey Zigachev 	amdgpu_bo_unreserve(vm->root.base.bo);
2858b843c749SSergey Zigachev 	return r;
2859b843c749SSergey Zigachev }
2860b843c749SSergey Zigachev 
2861b843c749SSergey Zigachev /**
2862b843c749SSergey Zigachev  * amdgpu_vm_free_levels - free PD/PT levels
2863b843c749SSergey Zigachev  *
2864b843c749SSergey Zigachev  * @adev: amdgpu device structure
2865b843c749SSergey Zigachev  * @parent: PD/PT starting level to free
2866b843c749SSergey Zigachev  * @level: level of parent structure
2867b843c749SSergey Zigachev  *
2868b843c749SSergey Zigachev  * Free the page directory or page table level and all sub levels.
2869b843c749SSergey Zigachev  */
amdgpu_vm_free_levels(struct amdgpu_device * adev,struct amdgpu_vm_pt * parent,unsigned level)2870b843c749SSergey Zigachev static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2871b843c749SSergey Zigachev 				  struct amdgpu_vm_pt *parent,
2872b843c749SSergey Zigachev 				  unsigned level)
2873b843c749SSergey Zigachev {
2874b843c749SSergey Zigachev 	unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
2875b843c749SSergey Zigachev 
2876b843c749SSergey Zigachev 	if (parent->base.bo) {
2877b843c749SSergey Zigachev 		list_del(&parent->base.bo_list);
2878b843c749SSergey Zigachev 		list_del(&parent->base.vm_status);
2879b843c749SSergey Zigachev 		amdgpu_bo_unref(&parent->base.bo->shadow);
2880b843c749SSergey Zigachev 		amdgpu_bo_unref(&parent->base.bo);
2881b843c749SSergey Zigachev 	}
2882b843c749SSergey Zigachev 
2883b843c749SSergey Zigachev 	if (parent->entries)
2884b843c749SSergey Zigachev 		for (i = 0; i < num_entries; i++)
2885b843c749SSergey Zigachev 			amdgpu_vm_free_levels(adev, &parent->entries[i],
2886b843c749SSergey Zigachev 					      level + 1);
2887b843c749SSergey Zigachev 
2888b843c749SSergey Zigachev 	kvfree(parent->entries);
2889b843c749SSergey Zigachev }
2890b843c749SSergey Zigachev 
2891b843c749SSergey Zigachev /**
2892b843c749SSergey Zigachev  * amdgpu_vm_fini - tear down a vm instance
2893b843c749SSergey Zigachev  *
2894b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2895b843c749SSergey Zigachev  * @vm: requested vm
2896b843c749SSergey Zigachev  *
2897b843c749SSergey Zigachev  * Tear down @vm.
2898b843c749SSergey Zigachev  * Unbind the VM and remove all bos from the vm bo list
2899b843c749SSergey Zigachev  */
amdgpu_vm_fini(struct amdgpu_device * adev,struct amdgpu_vm * vm)2900b843c749SSergey Zigachev void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2901b843c749SSergey Zigachev {
2902b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping, *tmp;
2903b843c749SSergey Zigachev 	bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2904b843c749SSergey Zigachev 	struct amdgpu_bo *root;
290578973132SSergey Zigachev #if 0
2906b843c749SSergey Zigachev 	u64 fault;
290778973132SSergey Zigachev #endif
2908b843c749SSergey Zigachev 	int i, r;
2909b843c749SSergey Zigachev 
2910b843c749SSergey Zigachev 	amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2911b843c749SSergey Zigachev 
291278973132SSergey Zigachev #if 0
2913b843c749SSergey Zigachev 	/* Clear pending page faults from IH when the VM is destroyed */
2914b843c749SSergey Zigachev 	while (kfifo_get(&vm->faults, &fault))
2915b843c749SSergey Zigachev 		amdgpu_ih_clear_fault(adev, fault);
291678973132SSergey Zigachev #endif
2917b843c749SSergey Zigachev 
2918b843c749SSergey Zigachev 	if (vm->pasid) {
2919b843c749SSergey Zigachev 		unsigned long flags;
2920b843c749SSergey Zigachev 
2921b843c749SSergey Zigachev 		spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2922b843c749SSergey Zigachev 		idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2923b843c749SSergey Zigachev 		spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2924b843c749SSergey Zigachev 	}
2925b843c749SSergey Zigachev 
2926b843c749SSergey Zigachev 	drm_sched_entity_destroy(&vm->entity);
2927b843c749SSergey Zigachev 
2928b843c749SSergey Zigachev 	if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
2929b843c749SSergey Zigachev 		dev_err(adev->dev, "still active bo inside vm\n");
2930b843c749SSergey Zigachev 	}
293178973132SSergey Zigachev #ifndef __DragonFly__
293278973132SSergey Zigachev  	rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
293378973132SSergey Zigachev #else
293478973132SSergey Zigachev 	/*
293578973132SSergey Zigachev 	 * DFly interval tree mock-up does not use RB trees, the RB iterator
293678973132SSergey Zigachev 	 * can not be used.
293778973132SSergey Zigachev 	 * This code is removing all entries so it is fairly easy to replace.
293878973132SSergey Zigachev 	 */
293978973132SSergey Zigachev 	while (vm->va.rb_leftmost) {
294078973132SSergey Zigachev 		mapping = container_of((void *)vm->va.rb_leftmost, struct amdgpu_bo_va_mapping, rb);
294178973132SSergey Zigachev #endif
2942b843c749SSergey Zigachev 		list_del(&mapping->list);
2943b843c749SSergey Zigachev 		amdgpu_vm_it_remove(mapping, &vm->va);
2944b843c749SSergey Zigachev 		kfree(mapping);
2945b843c749SSergey Zigachev 	}
2946b843c749SSergey Zigachev 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2947b843c749SSergey Zigachev 		if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2948b843c749SSergey Zigachev 			amdgpu_vm_prt_fini(adev, vm);
2949b843c749SSergey Zigachev 			prt_fini_needed = false;
2950b843c749SSergey Zigachev 		}
2951b843c749SSergey Zigachev 
2952b843c749SSergey Zigachev 		list_del(&mapping->list);
2953b843c749SSergey Zigachev 		amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2954b843c749SSergey Zigachev 	}
2955b843c749SSergey Zigachev 
2956b843c749SSergey Zigachev 	root = amdgpu_bo_ref(vm->root.base.bo);
2957b843c749SSergey Zigachev 	r = amdgpu_bo_reserve(root, true);
2958b843c749SSergey Zigachev 	if (r) {
2959b843c749SSergey Zigachev 		dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2960b843c749SSergey Zigachev 	} else {
2961b843c749SSergey Zigachev 		amdgpu_vm_free_levels(adev, &vm->root,
2962b843c749SSergey Zigachev 				      adev->vm_manager.root_level);
2963b843c749SSergey Zigachev 		amdgpu_bo_unreserve(root);
2964b843c749SSergey Zigachev 	}
2965b843c749SSergey Zigachev 	amdgpu_bo_unref(&root);
2966b843c749SSergey Zigachev 	dma_fence_put(vm->last_update);
2967b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2968b843c749SSergey Zigachev 		amdgpu_vmid_free_reserved(adev, vm, i);
2969b843c749SSergey Zigachev }
2970b843c749SSergey Zigachev 
2971b843c749SSergey Zigachev /**
2972b843c749SSergey Zigachev  * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2973b843c749SSergey Zigachev  *
2974b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
2975b843c749SSergey Zigachev  * @pasid: PASID do identify the VM
2976b843c749SSergey Zigachev  *
2977b843c749SSergey Zigachev  * This function is expected to be called in interrupt context.
2978b843c749SSergey Zigachev  *
2979b843c749SSergey Zigachev  * Returns:
2980b843c749SSergey Zigachev  * True if there was fault credit, false otherwise
2981b843c749SSergey Zigachev  */
2982b843c749SSergey Zigachev bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2983b843c749SSergey Zigachev 				  unsigned int pasid)
2984b843c749SSergey Zigachev {
2985b843c749SSergey Zigachev 	struct amdgpu_vm *vm;
2986b843c749SSergey Zigachev 
298778973132SSergey Zigachev 	lockmgr(&adev->vm_manager.pasid_lock, LK_EXCLUSIVE);
2988b843c749SSergey Zigachev 	vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2989b843c749SSergey Zigachev 	if (!vm) {
2990b843c749SSergey Zigachev 		/* VM not found, can't track fault credit */
299178973132SSergey Zigachev 		lockmgr(&adev->vm_manager.pasid_lock, LK_RELEASE);
2992b843c749SSergey Zigachev 		return true;
2993b843c749SSergey Zigachev 	}
2994b843c749SSergey Zigachev 
2995b843c749SSergey Zigachev 	/* No lock needed. only accessed by IRQ handler */
2996b843c749SSergey Zigachev 	if (!vm->fault_credit) {
2997b843c749SSergey Zigachev 		/* Too many faults in this VM */
299878973132SSergey Zigachev 		lockmgr(&adev->vm_manager.pasid_lock, LK_RELEASE);
2999b843c749SSergey Zigachev 		return false;
3000b843c749SSergey Zigachev 	}
3001b843c749SSergey Zigachev 
3002b843c749SSergey Zigachev 	vm->fault_credit--;
300378973132SSergey Zigachev 	lockmgr(&adev->vm_manager.pasid_lock, LK_RELEASE);
3004b843c749SSergey Zigachev 	return true;
3005b843c749SSergey Zigachev }
3006b843c749SSergey Zigachev 
3007b843c749SSergey Zigachev /**
3008b843c749SSergey Zigachev  * amdgpu_vm_manager_init - init the VM manager
3009b843c749SSergey Zigachev  *
3010b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
3011b843c749SSergey Zigachev  *
3012b843c749SSergey Zigachev  * Initialize the VM manager structures
3013b843c749SSergey Zigachev  */
3014b843c749SSergey Zigachev void amdgpu_vm_manager_init(struct amdgpu_device *adev)
3015b843c749SSergey Zigachev {
3016b843c749SSergey Zigachev 	unsigned i;
3017b843c749SSergey Zigachev 
3018b843c749SSergey Zigachev 	amdgpu_vmid_mgr_init(adev);
3019b843c749SSergey Zigachev 
3020b843c749SSergey Zigachev 	adev->vm_manager.fence_context =
3021b843c749SSergey Zigachev 		dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3022b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
3023b843c749SSergey Zigachev 		adev->vm_manager.seqno[i] = 0;
3024b843c749SSergey Zigachev 
3025b843c749SSergey Zigachev 	atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
302678973132SSergey Zigachev 	lockinit(&adev->vm_manager.prt_lock, "agvmmprtl", 0, LK_CANRECURSE);
3027b843c749SSergey Zigachev 	atomic_set(&adev->vm_manager.num_prt_users, 0);
3028b843c749SSergey Zigachev 
3029b843c749SSergey Zigachev 	/* If not overridden by the user, by default, only in large BAR systems
3030b843c749SSergey Zigachev 	 * Compute VM tables will be updated by CPU
3031b843c749SSergey Zigachev 	 */
3032b843c749SSergey Zigachev #ifdef CONFIG_X86_64
3033b843c749SSergey Zigachev 	if (amdgpu_vm_update_mode == -1) {
3034b843c749SSergey Zigachev 		if (amdgpu_gmc_vram_full_visible(&adev->gmc))
3035b843c749SSergey Zigachev 			adev->vm_manager.vm_update_mode =
3036b843c749SSergey Zigachev 				AMDGPU_VM_USE_CPU_FOR_COMPUTE;
3037b843c749SSergey Zigachev 		else
3038b843c749SSergey Zigachev 			adev->vm_manager.vm_update_mode = 0;
3039b843c749SSergey Zigachev 	} else
3040b843c749SSergey Zigachev 		adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
3041b843c749SSergey Zigachev #else
3042b843c749SSergey Zigachev 	adev->vm_manager.vm_update_mode = 0;
3043b843c749SSergey Zigachev #endif
3044b843c749SSergey Zigachev 
3045b843c749SSergey Zigachev 	idr_init(&adev->vm_manager.pasid_idr);
304678973132SSergey Zigachev 	lockinit(&adev->vm_manager.pasid_lock, "agvmmpl", 0, LK_CANRECURSE);
3047b843c749SSergey Zigachev }
3048b843c749SSergey Zigachev 
3049b843c749SSergey Zigachev /**
3050b843c749SSergey Zigachev  * amdgpu_vm_manager_fini - cleanup VM manager
3051b843c749SSergey Zigachev  *
3052b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
3053b843c749SSergey Zigachev  *
3054b843c749SSergey Zigachev  * Cleanup the VM manager and free resources.
3055b843c749SSergey Zigachev  */
3056b843c749SSergey Zigachev void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
3057b843c749SSergey Zigachev {
305878973132SSergey Zigachev #if 0
3059b843c749SSergey Zigachev 	WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
306078973132SSergey Zigachev #endif
3061b843c749SSergey Zigachev 	idr_destroy(&adev->vm_manager.pasid_idr);
3062b843c749SSergey Zigachev 
3063b843c749SSergey Zigachev 	amdgpu_vmid_mgr_fini(adev);
3064b843c749SSergey Zigachev }
3065b843c749SSergey Zigachev 
3066b843c749SSergey Zigachev /**
3067b843c749SSergey Zigachev  * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
3068b843c749SSergey Zigachev  *
3069b843c749SSergey Zigachev  * @dev: drm device pointer
3070b843c749SSergey Zigachev  * @data: drm_amdgpu_vm
3071b843c749SSergey Zigachev  * @filp: drm file pointer
3072b843c749SSergey Zigachev  *
3073b843c749SSergey Zigachev  * Returns:
3074b843c749SSergey Zigachev  * 0 for success, -errno for errors.
3075b843c749SSergey Zigachev  */
3076b843c749SSergey Zigachev int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
3077b843c749SSergey Zigachev {
3078b843c749SSergey Zigachev 	union drm_amdgpu_vm *args = data;
3079b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
3080b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
3081b843c749SSergey Zigachev 	int r;
3082b843c749SSergey Zigachev 
3083b843c749SSergey Zigachev 	switch (args->in.op) {
3084b843c749SSergey Zigachev 	case AMDGPU_VM_OP_RESERVE_VMID:
3085b843c749SSergey Zigachev 		/* current, we only have requirement to reserve vmid from gfxhub */
3086b843c749SSergey Zigachev 		r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
3087b843c749SSergey Zigachev 		if (r)
3088b843c749SSergey Zigachev 			return r;
3089b843c749SSergey Zigachev 		break;
3090b843c749SSergey Zigachev 	case AMDGPU_VM_OP_UNRESERVE_VMID:
3091b843c749SSergey Zigachev 		amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
3092b843c749SSergey Zigachev 		break;
3093b843c749SSergey Zigachev 	default:
3094b843c749SSergey Zigachev 		return -EINVAL;
3095b843c749SSergey Zigachev 	}
3096b843c749SSergey Zigachev 
3097b843c749SSergey Zigachev 	return 0;
3098b843c749SSergey Zigachev }
3099b843c749SSergey Zigachev 
3100b843c749SSergey Zigachev /**
3101b843c749SSergey Zigachev  * amdgpu_vm_get_task_info - Extracts task info for a PASID.
3102b843c749SSergey Zigachev  *
3103b843c749SSergey Zigachev  * @dev: drm device pointer
3104b843c749SSergey Zigachev  * @pasid: PASID identifier for VM
3105b843c749SSergey Zigachev  * @task_info: task_info to fill.
3106b843c749SSergey Zigachev  */
3107b843c749SSergey Zigachev void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid,
3108b843c749SSergey Zigachev 			 struct amdgpu_task_info *task_info)
3109b843c749SSergey Zigachev {
3110b843c749SSergey Zigachev 	struct amdgpu_vm *vm;
3111b843c749SSergey Zigachev 	unsigned long flags;
3112b843c749SSergey Zigachev 
3113b843c749SSergey Zigachev 	spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3114b843c749SSergey Zigachev 
3115b843c749SSergey Zigachev 	vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
3116b843c749SSergey Zigachev 	if (vm)
3117b843c749SSergey Zigachev 		*task_info = vm->task_info;
3118b843c749SSergey Zigachev 
3119b843c749SSergey Zigachev 	spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3120b843c749SSergey Zigachev }
3121b843c749SSergey Zigachev 
3122b843c749SSergey Zigachev /**
3123b843c749SSergey Zigachev  * amdgpu_vm_set_task_info - Sets VMs task info.
3124b843c749SSergey Zigachev  *
3125b843c749SSergey Zigachev  * @vm: vm for which to set the info
3126b843c749SSergey Zigachev  */
3127b843c749SSergey Zigachev void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
3128b843c749SSergey Zigachev {
312978973132SSergey Zigachev 	kprintf("amdgpu_vm_set_task_info: not implemented\n");
313078973132SSergey Zigachev #if 0
3131b843c749SSergey Zigachev 	if (!vm->task_info.pid) {
3132b843c749SSergey Zigachev 		vm->task_info.pid = current->pid;
3133b843c749SSergey Zigachev 		get_task_comm(vm->task_info.task_name, current);
3134b843c749SSergey Zigachev 
3135b843c749SSergey Zigachev 		if (current->group_leader->mm == current->mm) {
3136b843c749SSergey Zigachev 			vm->task_info.tgid = current->group_leader->pid;
3137b843c749SSergey Zigachev 			get_task_comm(vm->task_info.process_name, current->group_leader);
3138b843c749SSergey Zigachev 		}
3139b843c749SSergey Zigachev 	}
314078973132SSergey Zigachev #endif
3141b843c749SSergey Zigachev }
3142