blob: 447ed6e7e5862e5bf17dbb02bdeaf47b17a2e232 [file] [log] [blame]
Christian König073440d2016-09-28 15:41:50 +02001/*
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/rbtree.h>
Felix Kuehling02208442017-08-25 20:40:26 -040028#include <linux/idr.h>
Christian König073440d2016-09-28 15:41:50 +020029
30#include "gpu_scheduler.h"
31#include "amdgpu_sync.h"
32#include "amdgpu_ring.h"
33
34struct amdgpu_bo_va;
35struct amdgpu_job;
36struct amdgpu_bo_list_entry;
37
38/*
39 * GPUVM handling
40 */
41
42/* maximum number of VMIDs */
43#define AMDGPU_NUM_VM 16
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 */
Zhang, Jerry36b32a62017-03-29 16:08:32 +080049#define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
Christian König073440d2016-09-28 15:41:50 +020050
51/* PTBs (Page Table Blocks) need to be aligned to 32K */
52#define AMDGPU_VM_PTB_ALIGN_SIZE 32768
53
Christian König35ba15f2017-02-13 14:22:58 +010054#define AMDGPU_PTE_VALID (1ULL << 0)
55#define AMDGPU_PTE_SYSTEM (1ULL << 1)
56#define AMDGPU_PTE_SNOOPED (1ULL << 2)
Christian König073440d2016-09-28 15:41:50 +020057
58/* VI only */
Christian König35ba15f2017-02-13 14:22:58 +010059#define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
Christian König073440d2016-09-28 15:41:50 +020060
Christian König35ba15f2017-02-13 14:22:58 +010061#define AMDGPU_PTE_READABLE (1ULL << 5)
62#define AMDGPU_PTE_WRITEABLE (1ULL << 6)
Christian König073440d2016-09-28 15:41:50 +020063
Alex Xie982a1342017-02-15 14:10:19 -050064#define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
Christian König073440d2016-09-28 15:41:50 +020065
Zhang, Jerryd0766e92017-04-19 09:53:29 +080066/* TILED for VEGA10, reserved for older ASICs */
67#define AMDGPU_PTE_PRT (1ULL << 51)
Christian König284710f2017-01-30 11:09:31 +010068
Alex Deuchercf2f0a32017-07-25 16:35:38 -040069/* PDE is handled as PTE for VEGA10 */
70#define AMDGPU_PDE_PTE (1ULL << 54)
71
Alex Deucherca020612017-03-03 15:23:14 -050072/* VEGA10 only */
73#define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
74#define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
75
Christian König073440d2016-09-28 15:41:50 +020076/* How to programm VM fault handling */
77#define AMDGPU_VM_FAULT_STOP_NEVER 0
78#define AMDGPU_VM_FAULT_STOP_FIRST 1
79#define AMDGPU_VM_FAULT_STOP_ALWAYS 2
80
Christian Königeb60ef22017-03-30 14:41:19 +020081/* max number of VMHUB */
82#define AMDGPU_MAX_VMHUBS 2
83#define AMDGPU_GFXHUB 0
84#define AMDGPU_MMHUB 1
85
86/* hardcode that limit for now */
87#define AMDGPU_VA_RESERVED_SIZE (8 << 20)
Chunming Zhouc3505772017-04-21 15:51:04 +080088/* max vmids dedicated for process */
89#define AMDGPU_VM_MAX_RESERVED_VMID 1
Christian Königeb60ef22017-03-30 14:41:19 +020090
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -040091#define AMDGPU_VM_CONTEXT_GFX 0
92#define AMDGPU_VM_CONTEXT_COMPUTE 1
93
94/* See vm_update_mode */
95#define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
96#define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
97
Christian Königec681542017-08-01 10:51:43 +020098/* base structure for tracking BO usage in a VM */
99struct amdgpu_vm_bo_base {
100 /* constant after initialization */
101 struct amdgpu_vm *vm;
102 struct amdgpu_bo *bo;
103
104 /* protected by bo being reserved */
105 struct list_head bo_list;
106
107 /* protected by spinlock */
108 struct list_head vm_status;
Christian König3d7d4d32017-08-23 16:13:33 +0200109
110 /* protected by the BO being reserved */
111 bool moved;
Christian Königec681542017-08-01 10:51:43 +0200112};
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400113
Christian König073440d2016-09-28 15:41:50 +0200114struct amdgpu_vm_pt {
Christian König3f3333f2017-08-03 14:02:13 +0200115 struct amdgpu_vm_bo_base base;
116 uint64_t addr;
Christian König67003a12016-10-12 14:46:26 +0200117
118 /* array of page tables, one for each directory entry */
Christian König3f3333f2017-08-03 14:02:13 +0200119 struct amdgpu_vm_pt *entries;
120 unsigned last_entry_used;
Christian König073440d2016-09-28 15:41:50 +0200121};
122
Felix Kuehlinga2f14822017-08-26 02:43:06 -0400123#define AMDGPU_VM_FAULT(pasid, addr) (((u64)(pasid) << 48) | (addr))
124#define AMDGPU_VM_FAULT_PASID(fault) ((u64)(fault) >> 48)
125#define AMDGPU_VM_FAULT_ADDR(fault) ((u64)(fault) & 0xfffffffff000ULL)
126
Christian König073440d2016-09-28 15:41:50 +0200127struct amdgpu_vm {
128 /* tree of virtual addresses mapped */
129 struct rb_root va;
130
131 /* protecting invalidated */
132 spinlock_t status_lock;
133
Christian König3f3333f2017-08-03 14:02:13 +0200134 /* BOs who needs a validation */
135 struct list_head evicted;
136
Christian Königea097292017-08-09 14:15:46 +0200137 /* PT BOs which relocated and their parent need an update */
138 struct list_head relocated;
139
Christian König073440d2016-09-28 15:41:50 +0200140 /* BOs moved, but not yet updated in the PT */
Christian König27c7b9a2017-08-01 11:27:36 +0200141 struct list_head moved;
Christian König073440d2016-09-28 15:41:50 +0200142
Christian König073440d2016-09-28 15:41:50 +0200143 /* BO mappings freed, but not yet updated in the PT */
144 struct list_head freed;
145
146 /* contains the page directory */
Christian König67003a12016-10-12 14:46:26 +0200147 struct amdgpu_vm_pt root;
Christian Königd5884512017-09-08 14:09:41 +0200148 struct dma_fence *last_update;
Christian König073440d2016-09-28 15:41:50 +0200149
Christian König073440d2016-09-28 15:41:50 +0200150 /* protecting freed */
151 spinlock_t freed_lock;
152
153 /* Scheduler entity for page table updates */
154 struct amd_sched_entity entity;
155
Felix Kuehling02208442017-08-25 20:40:26 -0400156 /* client id and PASID (TODO: replace client_id with PASID) */
Christian König073440d2016-09-28 15:41:50 +0200157 u64 client_id;
Felix Kuehling02208442017-08-25 20:40:26 -0400158 unsigned int pasid;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +0800159 /* dedicated to vm */
160 struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS];
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400161
162 /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
163 bool use_cpu_for_update;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400164
165 /* Flag to indicate ATS support from PTE for GFX9 */
166 bool pte_support_ats;
Felix Kuehlinga2f14822017-08-26 02:43:06 -0400167
168 /* Up to 128 pending page faults */
169 DECLARE_KFIFO(faults, u64, 128);
Christian König073440d2016-09-28 15:41:50 +0200170};
171
172struct amdgpu_vm_id {
173 struct list_head list;
Christian König073440d2016-09-28 15:41:50 +0200174 struct amdgpu_sync active;
Dave Airlie220196b2016-10-28 11:33:52 +1000175 struct dma_fence *last_flush;
Christian König073440d2016-09-28 15:41:50 +0200176 atomic64_t owner;
177
178 uint64_t pd_gpu_addr;
179 /* last flushed PD/PT update */
Dave Airlie220196b2016-10-28 11:33:52 +1000180 struct dma_fence *flushed_updates;
Christian König073440d2016-09-28 15:41:50 +0200181
182 uint32_t current_gpu_reset_count;
183
184 uint32_t gds_base;
185 uint32_t gds_size;
186 uint32_t gws_base;
187 uint32_t gws_size;
188 uint32_t oa_base;
189 uint32_t oa_size;
190};
191
Christian König76456702017-04-06 17:52:39 +0200192struct amdgpu_vm_id_manager {
193 struct mutex lock;
194 unsigned num_ids;
195 struct list_head ids_lru;
196 struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
Chunming Zhouc3505772017-04-21 15:51:04 +0800197 atomic_t reserved_vmid_num;
Christian König76456702017-04-06 17:52:39 +0200198};
199
Christian König073440d2016-09-28 15:41:50 +0200200struct amdgpu_vm_manager {
201 /* Handling of VMIDs */
Christian König76456702017-04-06 17:52:39 +0200202 struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
Christian König073440d2016-09-28 15:41:50 +0200203
204 /* Handling of VM fences */
205 u64 fence_context;
206 unsigned seqno[AMDGPU_MAX_RINGS];
207
Felix Kuehling22770e52017-03-28 20:24:53 -0400208 uint64_t max_pfn;
Christian König8437a092016-10-17 15:08:10 +0200209 uint32_t num_level;
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800210 uint64_t vm_size;
211 uint32_t block_size;
Roger Hee618d302017-08-11 20:00:41 +0800212 uint32_t fragment_size;
Christian König073440d2016-09-28 15:41:50 +0200213 /* vram base address for page table entry */
214 u64 vram_base_offset;
Christian König073440d2016-09-28 15:41:50 +0200215 /* vm pte handling */
216 const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
217 struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
218 unsigned vm_pte_num_rings;
219 atomic_t vm_pte_next_ring;
220 /* client id counter */
221 atomic64_t client_counter;
Christian König284710f2017-01-30 11:09:31 +0100222
223 /* partial resident texture handling */
224 spinlock_t prt_lock;
Christian König451bc8e2017-02-14 16:02:52 +0100225 atomic_t num_prt_users;
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400226
227 /* controls how VM page tables are updated for Graphics and Compute.
228 * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
229 * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
230 */
231 int vm_update_mode;
Felix Kuehling02208442017-08-25 20:40:26 -0400232
233 /* PASID to VM mapping, will be used in interrupt context to
234 * look up VM of a page fault
235 */
236 struct idr pasid_idr;
237 spinlock_t pasid_lock;
Christian König073440d2016-09-28 15:41:50 +0200238};
239
Felix Kuehling02208442017-08-25 20:40:26 -0400240int amdgpu_vm_alloc_pasid(unsigned int bits);
241void amdgpu_vm_free_pasid(unsigned int pasid);
Christian König073440d2016-09-28 15:41:50 +0200242void amdgpu_vm_manager_init(struct amdgpu_device *adev);
243void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400244int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -0400245 int vm_context, unsigned int pasid);
Christian König073440d2016-09-28 15:41:50 +0200246void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
247void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
248 struct list_head *validated,
249 struct amdgpu_bo_list_entry *entry);
Christian König3f3333f2017-08-03 14:02:13 +0200250bool amdgpu_vm_ready(struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200251int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
252 int (*callback)(void *p, struct amdgpu_bo *bo),
253 void *param);
Christian König663e4572017-03-13 10:13:37 +0100254int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
255 struct amdgpu_vm *vm,
256 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200257int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Dave Airlie220196b2016-10-28 11:33:52 +1000258 struct amdgpu_sync *sync, struct dma_fence *fence,
Christian König073440d2016-09-28 15:41:50 +0200259 struct amdgpu_job *job);
Monk Liu8fdf0742017-06-06 17:25:13 +0800260int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
Christian König76456702017-04-06 17:52:39 +0200261void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
262 unsigned vmid);
Christian König32601d42017-05-10 20:06:58 +0200263void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
Christian König194d2162016-10-12 15:13:52 +0200264int amdgpu_vm_update_directories(struct amdgpu_device *adev,
265 struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200266int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100267 struct amdgpu_vm *vm,
268 struct dma_fence **fence);
Christian König73fb16e2017-08-16 11:13:48 +0200269int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +0200270 struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200271int amdgpu_vm_bo_update(struct amdgpu_device *adev,
272 struct amdgpu_bo_va *bo_va,
273 bool clear);
274void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +0200275 struct amdgpu_bo *bo, bool evicted);
Christian König073440d2016-09-28 15:41:50 +0200276struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
277 struct amdgpu_bo *bo);
278struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
279 struct amdgpu_vm *vm,
280 struct amdgpu_bo *bo);
281int amdgpu_vm_bo_map(struct amdgpu_device *adev,
282 struct amdgpu_bo_va *bo_va,
283 uint64_t addr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +0100284 uint64_t size, uint64_t flags);
Christian König80f95c52017-03-13 10:13:39 +0100285int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
286 struct amdgpu_bo_va *bo_va,
287 uint64_t addr, uint64_t offset,
288 uint64_t size, uint64_t flags);
Christian König073440d2016-09-28 15:41:50 +0200289int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
290 struct amdgpu_bo_va *bo_va,
291 uint64_t addr);
Christian Königdc54d3d2017-03-13 10:13:38 +0100292int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
293 struct amdgpu_vm *vm,
294 uint64_t saddr, uint64_t size);
Christian Königaebc5e62017-09-06 16:55:16 +0200295struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
296 uint64_t addr);
Christian König073440d2016-09-28 15:41:50 +0200297void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
298 struct amdgpu_bo_va *bo_va);
Roger Hed07f14b2017-08-15 16:05:59 +0800299void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,
300 uint32_t fragment_size_default);
301void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size,
302 uint32_t fragment_size_default);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +0800303int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400304bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
305 struct amdgpu_job *job);
Alex Xiee59c0202017-06-01 09:42:59 -0400306void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
Christian König073440d2016-09-28 15:41:50 +0200307
308#endif