xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h (revision c62d3cd0ddd629606a3830aa22e9dcc6c2a0d3bf)
1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Christian König
23  */
24 #ifndef __AMDGPU_VM_H__
25 #define __AMDGPU_VM_H__
26 
27 #include <linux/idr.h>
28 #include <linux/kfifo.h>
29 #include <linux/rbtree.h>
30 #include <drm/gpu_scheduler.h>
31 #include <drm/drm_file.h>
32 
33 #include "amdgpu_sync.h"
34 #include "amdgpu_ring.h"
35 #include "amdgpu_ids.h"
36 
37 struct amdgpu_bo_va;
38 struct amdgpu_job;
39 struct amdgpu_bo_list_entry;
40 
41 /*
42  * GPUVM handling
43  */
44 
45 /* Maximum number of PTEs the hardware can write with one command */
46 #define AMDGPU_VM_MAX_UPDATE_SIZE	0x3FFFF
47 
48 /* number of entries in page table */
49 #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
50 
51 /* PTBs (Page Table Blocks) need to be aligned to 32K */
52 #define AMDGPU_VM_PTB_ALIGN_SIZE   32768
53 
54 #define AMDGPU_PTE_VALID	(1ULL << 0)
55 #define AMDGPU_PTE_SYSTEM	(1ULL << 1)
56 #define AMDGPU_PTE_SNOOPED	(1ULL << 2)
57 
58 /* VI only */
59 #define AMDGPU_PTE_EXECUTABLE	(1ULL << 4)
60 
61 #define AMDGPU_PTE_READABLE	(1ULL << 5)
62 #define AMDGPU_PTE_WRITEABLE	(1ULL << 6)
63 
64 #define AMDGPU_PTE_FRAG(x)	((x & 0x1fULL) << 7)
65 
66 /* TILED for VEGA10, reserved for older ASICs  */
67 #define AMDGPU_PTE_PRT		(1ULL << 51)
68 
69 /* PDE is handled as PTE for VEGA10 */
70 #define AMDGPU_PDE_PTE		(1ULL << 54)
71 
72 /* PTE is handled as PDE for VEGA10 (Translate Further) */
73 #define AMDGPU_PTE_TF		(1ULL << 56)
74 
75 /* PDE Block Fragment Size for VEGA10 */
76 #define AMDGPU_PDE_BFS(a)	((uint64_t)a << 59)
77 
78 
79 /* For GFX9 */
80 #define AMDGPU_PTE_MTYPE(a)    ((uint64_t)a << 57)
81 #define AMDGPU_PTE_MTYPE_MASK	AMDGPU_PTE_MTYPE(3ULL)
82 
83 #define AMDGPU_MTYPE_NC 0
84 #define AMDGPU_MTYPE_CC 2
85 
86 #define AMDGPU_PTE_DEFAULT_ATC  (AMDGPU_PTE_SYSTEM      \
87                                 | AMDGPU_PTE_SNOOPED    \
88                                 | AMDGPU_PTE_EXECUTABLE \
89                                 | AMDGPU_PTE_READABLE   \
90                                 | AMDGPU_PTE_WRITEABLE  \
91                                 | AMDGPU_PTE_MTYPE(AMDGPU_MTYPE_CC))
92 
93 /* How to programm VM fault handling */
94 #define AMDGPU_VM_FAULT_STOP_NEVER	0
95 #define AMDGPU_VM_FAULT_STOP_FIRST	1
96 #define AMDGPU_VM_FAULT_STOP_ALWAYS	2
97 
98 /* max number of VMHUB */
99 #define AMDGPU_MAX_VMHUBS			2
100 #define AMDGPU_GFXHUB				0
101 #define AMDGPU_MMHUB				1
102 
103 /* hardcode that limit for now */
104 #define AMDGPU_VA_RESERVED_SIZE			(1ULL << 20)
105 
106 /* VA hole for 48bit addresses on Vega10 */
107 #define AMDGPU_VA_HOLE_START			0x0000800000000000ULL
108 #define AMDGPU_VA_HOLE_END			0xffff800000000000ULL
109 
110 /*
111  * Hardware is programmed as if the hole doesn't exists with start and end
112  * address values.
113  *
114  * This mask is used to remove the upper 16bits of the VA and so come up with
115  * the linear addr value.
116  */
117 #define AMDGPU_VA_HOLE_MASK			0x0000ffffffffffffULL
118 
119 /* max vmids dedicated for process */
120 #define AMDGPU_VM_MAX_RESERVED_VMID	1
121 
122 #define AMDGPU_VM_CONTEXT_GFX 0
123 #define AMDGPU_VM_CONTEXT_COMPUTE 1
124 
125 /* See vm_update_mode */
126 #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
127 #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
128 
129 /* VMPT level enumerate, and the hiberachy is:
130  * PDB2->PDB1->PDB0->PTB
131  */
132 enum amdgpu_vm_level {
133 	AMDGPU_VM_PDB2,
134 	AMDGPU_VM_PDB1,
135 	AMDGPU_VM_PDB0,
136 	AMDGPU_VM_PTB
137 };
138 
139 /* base structure for tracking BO usage in a VM */
140 struct amdgpu_vm_bo_base {
141 	/* constant after initialization */
142 	struct amdgpu_vm		*vm;
143 	struct amdgpu_bo		*bo;
144 
145 	/* protected by bo being reserved */
146 	struct list_head		bo_list;
147 
148 	/* protected by spinlock */
149 	struct list_head		vm_status;
150 
151 	/* protected by the BO being reserved */
152 	bool				moved;
153 };
154 
155 struct amdgpu_vm_pt {
156 	struct amdgpu_vm_bo_base	base;
157 	bool				huge;
158 
159 	/* array of page tables, one for each directory entry */
160 	struct amdgpu_vm_pt		*entries;
161 };
162 
163 /* provided by hw blocks that can write ptes, e.g., sdma */
164 struct amdgpu_vm_pte_funcs {
165 	/* number of dw to reserve per operation */
166 	unsigned	copy_pte_num_dw;
167 
168 	/* copy pte entries from GART */
169 	void (*copy_pte)(struct amdgpu_ib *ib,
170 			 uint64_t pe, uint64_t src,
171 			 unsigned count);
172 
173 	/* write pte one entry at a time with addr mapping */
174 	void (*write_pte)(struct amdgpu_ib *ib, uint64_t pe,
175 			  uint64_t value, unsigned count,
176 			  uint32_t incr);
177 	/* for linear pte/pde updates without addr mapping */
178 	void (*set_pte_pde)(struct amdgpu_ib *ib,
179 			    uint64_t pe,
180 			    uint64_t addr, unsigned count,
181 			    uint32_t incr, uint64_t flags);
182 };
183 
184 #define AMDGPU_VM_FAULT(pasid, addr) (((u64)(pasid) << 48) | (addr))
185 #define AMDGPU_VM_FAULT_PASID(fault) ((u64)(fault) >> 48)
186 #define AMDGPU_VM_FAULT_ADDR(fault)  ((u64)(fault) & 0xfffffffff000ULL)
187 
188 
189 struct amdgpu_task_info {
190 	char	process_name[TASK_COMM_LEN];
191 	char	task_name[TASK_COMM_LEN];
192 	pid_t	pid;
193 	pid_t	tgid;
194 };
195 
196 struct amdgpu_vm {
197 	/* tree of virtual addresses mapped */
198 	struct rb_root_cached	va;
199 
200 	/* BOs who needs a validation */
201 	struct list_head	evicted;
202 
203 	/* PT BOs which relocated and their parent need an update */
204 	struct list_head	relocated;
205 
206 	/* BOs moved, but not yet updated in the PT */
207 	struct list_head	moved;
208 	spinlock_t		moved_lock;
209 
210 	/* All BOs of this VM not currently in the state machine */
211 	struct list_head	idle;
212 
213 	/* BO mappings freed, but not yet updated in the PT */
214 	struct list_head	freed;
215 
216 	/* contains the page directory */
217 	struct amdgpu_vm_pt     root;
218 	struct dma_fence	*last_update;
219 
220 	/* Scheduler entity for page table updates */
221 	struct drm_sched_entity	entity;
222 
223 	unsigned int		pasid;
224 	/* dedicated to vm */
225 	struct amdgpu_vmid	*reserved_vmid[AMDGPU_MAX_VMHUBS];
226 
227 	/* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
228 	bool                    use_cpu_for_update;
229 
230 	/* Flag to indicate ATS support from PTE for GFX9 */
231 	bool			pte_support_ats;
232 
233 	/* Up to 128 pending retry page faults */
234 	DECLARE_KFIFO(faults, u64, 128);
235 
236 	/* Limit non-retry fault storms */
237 	unsigned int		fault_credit;
238 
239 	/* Points to the KFD process VM info */
240 	struct amdkfd_process_info *process_info;
241 
242 	/* List node in amdkfd_process_info.vm_list_head */
243 	struct list_head	vm_list_node;
244 
245 	/* Valid while the PD is reserved or fenced */
246 	uint64_t		pd_phys_addr;
247 
248 	/* Some basic info about the task */
249 	struct amdgpu_task_info task_info;
250 };
251 
252 struct amdgpu_vm_manager {
253 	/* Handling of VMIDs */
254 	struct amdgpu_vmid_mgr			id_mgr[AMDGPU_MAX_VMHUBS];
255 
256 	/* Handling of VM fences */
257 	u64					fence_context;
258 	unsigned				seqno[AMDGPU_MAX_RINGS];
259 
260 	uint64_t				max_pfn;
261 	uint32_t				num_level;
262 	uint32_t				block_size;
263 	uint32_t				fragment_size;
264 	enum amdgpu_vm_level			root_level;
265 	/* vram base address for page table entry  */
266 	u64					vram_base_offset;
267 	/* vm pte handling */
268 	const struct amdgpu_vm_pte_funcs        *vm_pte_funcs;
269 	struct amdgpu_ring                      *vm_pte_rings[AMDGPU_MAX_RINGS];
270 	unsigned				vm_pte_num_rings;
271 	atomic_t				vm_pte_next_ring;
272 
273 	/* partial resident texture handling */
274 	spinlock_t				prt_lock;
275 	atomic_t				num_prt_users;
276 
277 	/* controls how VM page tables are updated for Graphics and Compute.
278 	 * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
279 	 * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
280 	 */
281 	int					vm_update_mode;
282 
283 	/* PASID to VM mapping, will be used in interrupt context to
284 	 * look up VM of a page fault
285 	 */
286 	struct idr				pasid_idr;
287 	spinlock_t				pasid_lock;
288 };
289 
290 #define amdgpu_vm_copy_pte(adev, ib, pe, src, count) ((adev)->vm_manager.vm_pte_funcs->copy_pte((ib), (pe), (src), (count)))
291 #define amdgpu_vm_write_pte(adev, ib, pe, value, count, incr) ((adev)->vm_manager.vm_pte_funcs->write_pte((ib), (pe), (value), (count), (incr)))
292 #define amdgpu_vm_set_pte_pde(adev, ib, pe, addr, count, incr, flags) ((adev)->vm_manager.vm_pte_funcs->set_pte_pde((ib), (pe), (addr), (count), (incr), (flags)))
293 
294 void amdgpu_vm_manager_init(struct amdgpu_device *adev);
295 void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
296 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
297 		   int vm_context, unsigned int pasid);
298 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
299 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
300 bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
301 				  unsigned int pasid);
302 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
303 			 struct list_head *validated,
304 			 struct amdgpu_bo_list_entry *entry);
305 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
306 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
307 			      int (*callback)(void *p, struct amdgpu_bo *bo),
308 			      void *param);
309 int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
310 			struct amdgpu_vm *vm,
311 			uint64_t saddr, uint64_t size);
312 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
313 int amdgpu_vm_update_directories(struct amdgpu_device *adev,
314 				 struct amdgpu_vm *vm);
315 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
316 			  struct amdgpu_vm *vm,
317 			  struct dma_fence **fence);
318 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
319 			   struct amdgpu_vm *vm);
320 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
321 			struct amdgpu_bo_va *bo_va,
322 			bool clear);
323 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
324 			     struct amdgpu_bo *bo, bool evicted);
325 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
326 				       struct amdgpu_bo *bo);
327 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
328 				      struct amdgpu_vm *vm,
329 				      struct amdgpu_bo *bo);
330 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
331 		     struct amdgpu_bo_va *bo_va,
332 		     uint64_t addr, uint64_t offset,
333 		     uint64_t size, uint64_t flags);
334 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
335 			     struct amdgpu_bo_va *bo_va,
336 			     uint64_t addr, uint64_t offset,
337 			     uint64_t size, uint64_t flags);
338 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
339 		       struct amdgpu_bo_va *bo_va,
340 		       uint64_t addr);
341 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
342 				struct amdgpu_vm *vm,
343 				uint64_t saddr, uint64_t size);
344 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
345 							 uint64_t addr);
346 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket);
347 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
348 		      struct amdgpu_bo_va *bo_va);
349 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
350 			   uint32_t fragment_size_default, unsigned max_level,
351 			   unsigned max_bits);
352 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
353 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
354 				  struct amdgpu_job *job);
355 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
356 
357 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid,
358 			 struct amdgpu_task_info *task_info);
359 
360 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
361 
362 #endif
363