2 * Copyright 2016 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Christian König
24 #ifndef __AMDGPU_VM_H__
25 #define __AMDGPU_VM_H__
27 #include <linux/idr.h>
28 #include <linux/kfifo.h>
29 #include <linux/rbtree.h>
30 #include <drm/gpu_scheduler.h>
32 #include "amdgpu_sync.h"
33 #include "amdgpu_ring.h"
37 struct amdgpu_bo_list_entry;
43 /* maximum number of VMIDs */
44 #define AMDGPU_NUM_VM 16
46 /* Maximum number of PTEs the hardware can write with one command */
47 #define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF
49 /* number of entries in page table */
50 #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
52 /* PTBs (Page Table Blocks) need to be aligned to 32K */
53 #define AMDGPU_VM_PTB_ALIGN_SIZE 32768
55 #define AMDGPU_PTE_VALID (1ULL << 0)
56 #define AMDGPU_PTE_SYSTEM (1ULL << 1)
57 #define AMDGPU_PTE_SNOOPED (1ULL << 2)
60 #define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
62 #define AMDGPU_PTE_READABLE (1ULL << 5)
63 #define AMDGPU_PTE_WRITEABLE (1ULL << 6)
65 #define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
67 /* TILED for VEGA10, reserved for older ASICs */
68 #define AMDGPU_PTE_PRT (1ULL << 51)
70 /* PDE is handled as PTE for VEGA10 */
71 #define AMDGPU_PDE_PTE (1ULL << 54)
73 /* PTE is handled as PDE for VEGA10 (Translate Further) */
74 #define AMDGPU_PTE_TF (1ULL << 56)
76 /* PDE Block Fragment Size for VEGA10 */
77 #define AMDGPU_PDE_BFS(a) ((uint64_t)a << 59)
80 #define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
81 #define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
84 #define AMDGPU_MTYPE_CC 2
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))
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
98 /* max number of VMHUB */
99 #define AMDGPU_MAX_VMHUBS 2
100 #define AMDGPU_GFXHUB 0
101 #define AMDGPU_MMHUB 1
103 /* hardcode that limit for now */
104 #define AMDGPU_VA_RESERVED_SIZE (8ULL << 20)
106 /* VA hole for 48bit addresses on Vega10 */
107 #define AMDGPU_VA_HOLE_START 0x0000800000000000ULL
108 #define AMDGPU_VA_HOLE_END 0xffff800000000000ULL
111 * Hardware is programmed as if the hole doesn't exists with start and end
114 * This mask is used to remove the upper 16bits of the VA and so come up with
115 * the linear addr value.
117 #define AMDGPU_VA_HOLE_MASK 0x0000ffffffffffffULL
119 /* max vmids dedicated for process */
120 #define AMDGPU_VM_MAX_RESERVED_VMID 1
122 #define AMDGPU_VM_CONTEXT_GFX 0
123 #define AMDGPU_VM_CONTEXT_COMPUTE 1
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)
129 /* VMPT level enumerate, and the hiberachy is:
130 * PDB2->PDB1->PDB0->PTB
132 enum amdgpu_vm_level {
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;
145 /* protected by bo being reserved */
146 struct list_head bo_list;
148 /* protected by spinlock */
149 struct list_head vm_status;
151 /* protected by the BO being reserved */
155 struct amdgpu_vm_pt {
156 struct amdgpu_vm_bo_base base;
159 /* array of page tables, one for each directory entry */
160 struct amdgpu_vm_pt *entries;
163 #define AMDGPU_VM_FAULT(pasid, addr) (((u64)(pasid) << 48) | (addr))
164 #define AMDGPU_VM_FAULT_PASID(fault) ((u64)(fault) >> 48)
165 #define AMDGPU_VM_FAULT_ADDR(fault) ((u64)(fault) & 0xfffffffff000ULL)
168 /* tree of virtual addresses mapped */
169 struct rb_root_cached va;
171 /* protecting invalidated */
172 spinlock_t status_lock;
174 /* BOs who needs a validation */
175 struct list_head evicted;
177 /* PT BOs which relocated and their parent need an update */
178 struct list_head relocated;
180 /* BOs moved, but not yet updated in the PT */
181 struct list_head moved;
183 /* BO mappings freed, but not yet updated in the PT */
184 struct list_head freed;
186 /* contains the page directory */
187 struct amdgpu_vm_pt root;
188 struct dma_fence *last_update;
190 /* protecting freed */
191 spinlock_t freed_lock;
193 /* Scheduler entity for page table updates */
194 struct drm_sched_entity entity;
196 /* client id and PASID (TODO: replace client_id with PASID) */
199 /* dedicated to vm */
200 struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS];
202 /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
203 bool use_cpu_for_update;
205 /* Flag to indicate ATS support from PTE for GFX9 */
206 bool pte_support_ats;
208 /* Up to 128 pending retry page faults */
209 DECLARE_KFIFO(faults, u64, 128);
211 /* Limit non-retry fault storms */
212 unsigned int fault_credit;
215 struct amdgpu_vm_id {
216 struct list_head list;
217 struct amdgpu_sync active;
218 struct dma_fence *last_flush;
221 uint64_t pd_gpu_addr;
222 /* last flushed PD/PT update */
223 struct dma_fence *flushed_updates;
225 uint32_t current_gpu_reset_count;
235 struct amdgpu_vm_id_manager {
238 struct list_head ids_lru;
239 struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
240 atomic_t reserved_vmid_num;
243 struct amdgpu_vm_manager {
244 /* Handling of VMIDs */
245 struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
247 /* Handling of VM fences */
249 unsigned seqno[AMDGPU_MAX_RINGS];
254 uint32_t fragment_size;
255 enum amdgpu_vm_level root_level;
256 /* vram base address for page table entry */
257 u64 vram_base_offset;
258 /* vm pte handling */
259 const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
260 struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
261 unsigned vm_pte_num_rings;
262 atomic_t vm_pte_next_ring;
263 /* client id counter */
264 atomic64_t client_counter;
266 /* partial resident texture handling */
268 atomic_t num_prt_users;
270 /* controls how VM page tables are updated for Graphics and Compute.
271 * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
272 * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
276 /* PASID to VM mapping, will be used in interrupt context to
277 * look up VM of a page fault
279 struct idr pasid_idr;
280 spinlock_t pasid_lock;
283 int amdgpu_vm_alloc_pasid(unsigned int bits);
284 void amdgpu_vm_free_pasid(unsigned int pasid);
285 void amdgpu_vm_manager_init(struct amdgpu_device *adev);
286 void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
287 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
288 int vm_context, unsigned int pasid);
289 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
290 bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
292 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
293 struct list_head *validated,
294 struct amdgpu_bo_list_entry *entry);
295 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
296 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
297 int (*callback)(void *p, struct amdgpu_bo *bo),
299 int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
300 struct amdgpu_vm *vm,
301 uint64_t saddr, uint64_t size);
302 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
303 struct amdgpu_sync *sync, struct dma_fence *fence,
304 struct amdgpu_job *job);
305 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
306 void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
308 void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
309 int amdgpu_vm_update_directories(struct amdgpu_device *adev,
310 struct amdgpu_vm *vm);
311 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
312 struct amdgpu_vm *vm,
313 struct dma_fence **fence);
314 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
315 struct amdgpu_vm *vm);
316 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
317 struct amdgpu_bo_va *bo_va,
319 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
320 struct amdgpu_bo *bo, bool evicted);
321 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
322 struct amdgpu_bo *bo);
323 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
324 struct amdgpu_vm *vm,
325 struct amdgpu_bo *bo);
326 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
327 struct amdgpu_bo_va *bo_va,
328 uint64_t addr, uint64_t offset,
329 uint64_t size, uint64_t flags);
330 int amdgpu_vm_bo_replace_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_unmap(struct amdgpu_device *adev,
335 struct amdgpu_bo_va *bo_va,
337 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
338 struct amdgpu_vm *vm,
339 uint64_t saddr, uint64_t size);
340 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
342 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
343 struct amdgpu_bo_va *bo_va);
344 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
345 uint32_t fragment_size_default, unsigned max_level,
347 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
348 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
349 struct amdgpu_job *job);
350 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);