blob: 02b0dd3b135f2b802f92ba27f44dd56b976c4271 [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>
28
29#include "gpu_scheduler.h"
30#include "amdgpu_sync.h"
31#include "amdgpu_ring.h"
32
33struct amdgpu_bo_va;
34struct amdgpu_job;
35struct amdgpu_bo_list_entry;
36
37/*
38 * GPUVM handling
39 */
40
41/* maximum number of VMIDs */
42#define AMDGPU_NUM_VM 16
43
44/* Maximum number of PTEs the hardware can write with one command */
45#define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF
46
47/* number of entries in page table */
Zhang, Jerry36b32a62017-03-29 16:08:32 +080048#define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
Christian König073440d2016-09-28 15:41:50 +020049
50/* PTBs (Page Table Blocks) need to be aligned to 32K */
51#define AMDGPU_VM_PTB_ALIGN_SIZE 32768
52
53/* LOG2 number of continuous pages for the fragment field */
54#define AMDGPU_LOG2_PAGES_PER_FRAG 4
55
Christian König35ba15f2017-02-13 14:22:58 +010056#define AMDGPU_PTE_VALID (1ULL << 0)
57#define AMDGPU_PTE_SYSTEM (1ULL << 1)
58#define AMDGPU_PTE_SNOOPED (1ULL << 2)
Christian König073440d2016-09-28 15:41:50 +020059
60/* VI only */
Christian König35ba15f2017-02-13 14:22:58 +010061#define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
Christian König073440d2016-09-28 15:41:50 +020062
Christian König35ba15f2017-02-13 14:22:58 +010063#define AMDGPU_PTE_READABLE (1ULL << 5)
64#define AMDGPU_PTE_WRITEABLE (1ULL << 6)
Christian König073440d2016-09-28 15:41:50 +020065
Alex Xie982a1342017-02-15 14:10:19 -050066#define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
Christian König073440d2016-09-28 15:41:50 +020067
Christian König35ba15f2017-02-13 14:22:58 +010068#define AMDGPU_PTE_PRT (1ULL << 63)
Christian König284710f2017-01-30 11:09:31 +010069
Alex Deucherca020612017-03-03 15:23:14 -050070/* VEGA10 only */
71#define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
72#define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
73
Christian König073440d2016-09-28 15:41:50 +020074/* How to programm VM fault handling */
75#define AMDGPU_VM_FAULT_STOP_NEVER 0
76#define AMDGPU_VM_FAULT_STOP_FIRST 1
77#define AMDGPU_VM_FAULT_STOP_ALWAYS 2
78
Christian Königeb60ef22017-03-30 14:41:19 +020079/* max number of VMHUB */
80#define AMDGPU_MAX_VMHUBS 2
81#define AMDGPU_GFXHUB 0
82#define AMDGPU_MMHUB 1
83
84/* hardcode that limit for now */
85#define AMDGPU_VA_RESERVED_SIZE (8 << 20)
86
Christian König073440d2016-09-28 15:41:50 +020087struct amdgpu_vm_pt {
88 struct amdgpu_bo *bo;
89 uint64_t addr;
Christian König67003a12016-10-12 14:46:26 +020090
91 /* array of page tables, one for each directory entry */
92 struct amdgpu_vm_pt *entries;
93 unsigned last_entry_used;
Christian König073440d2016-09-28 15:41:50 +020094};
95
96struct amdgpu_vm {
97 /* tree of virtual addresses mapped */
98 struct rb_root va;
99
100 /* protecting invalidated */
101 spinlock_t status_lock;
102
103 /* BOs moved, but not yet updated in the PT */
104 struct list_head invalidated;
105
106 /* BOs cleared in the PT because of a move */
107 struct list_head cleared;
108
109 /* BO mappings freed, but not yet updated in the PT */
110 struct list_head freed;
111
112 /* contains the page directory */
Christian König67003a12016-10-12 14:46:26 +0200113 struct amdgpu_vm_pt root;
Christian Königa24960f2016-10-12 13:20:52 +0200114 struct dma_fence *last_dir_update;
Christian König073440d2016-09-28 15:41:50 +0200115 uint64_t last_eviction_counter;
116
Christian König073440d2016-09-28 15:41:50 +0200117 /* for id and flush management per ring */
118 struct amdgpu_vm_id *ids[AMDGPU_MAX_RINGS];
119
120 /* protecting freed */
121 spinlock_t freed_lock;
122
123 /* Scheduler entity for page table updates */
124 struct amd_sched_entity entity;
125
126 /* client id */
127 u64 client_id;
Monk Liubd7de272017-01-09 15:23:17 +0800128 /* each VM will map on CSA */
129 struct amdgpu_bo_va *csa_bo_va;
Christian König073440d2016-09-28 15:41:50 +0200130};
131
132struct amdgpu_vm_id {
133 struct list_head list;
Christian König073440d2016-09-28 15:41:50 +0200134 struct amdgpu_sync active;
Dave Airlie220196b2016-10-28 11:33:52 +1000135 struct dma_fence *last_flush;
Christian König073440d2016-09-28 15:41:50 +0200136 atomic64_t owner;
137
138 uint64_t pd_gpu_addr;
139 /* last flushed PD/PT update */
Dave Airlie220196b2016-10-28 11:33:52 +1000140 struct dma_fence *flushed_updates;
Christian König073440d2016-09-28 15:41:50 +0200141
142 uint32_t current_gpu_reset_count;
143
144 uint32_t gds_base;
145 uint32_t gds_size;
146 uint32_t gws_base;
147 uint32_t gws_size;
148 uint32_t oa_base;
149 uint32_t oa_size;
150};
151
152struct amdgpu_vm_manager {
153 /* Handling of VMIDs */
154 struct mutex lock;
155 unsigned num_ids;
156 struct list_head ids_lru;
157 struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
158
159 /* Handling of VM fences */
160 u64 fence_context;
161 unsigned seqno[AMDGPU_MAX_RINGS];
162
Felix Kuehling22770e52017-03-28 20:24:53 -0400163 uint64_t max_pfn;
Christian König8437a092016-10-17 15:08:10 +0200164 uint32_t num_level;
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800165 uint64_t vm_size;
166 uint32_t block_size;
Christian König073440d2016-09-28 15:41:50 +0200167 /* vram base address for page table entry */
168 u64 vram_base_offset;
169 /* is vm enabled? */
170 bool enabled;
171 /* vm pte handling */
172 const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
173 struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
174 unsigned vm_pte_num_rings;
175 atomic_t vm_pte_next_ring;
176 /* client id counter */
177 atomic64_t client_counter;
Christian König284710f2017-01-30 11:09:31 +0100178
179 /* partial resident texture handling */
180 spinlock_t prt_lock;
Christian König451bc8e2017-02-14 16:02:52 +0100181 atomic_t num_prt_users;
Christian König073440d2016-09-28 15:41:50 +0200182};
183
184void amdgpu_vm_manager_init(struct amdgpu_device *adev);
185void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
186int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm);
187void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
188void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
189 struct list_head *validated,
190 struct amdgpu_bo_list_entry *entry);
191int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
192 int (*callback)(void *p, struct amdgpu_bo *bo),
193 void *param);
194void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
195 struct amdgpu_vm *vm);
Christian König663e4572017-03-13 10:13:37 +0100196int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
197 struct amdgpu_vm *vm,
198 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200199int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Dave Airlie220196b2016-10-28 11:33:52 +1000200 struct amdgpu_sync *sync, struct dma_fence *fence,
Christian König073440d2016-09-28 15:41:50 +0200201 struct amdgpu_job *job);
202int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job);
203void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id);
Christian König194d2162016-10-12 15:13:52 +0200204int amdgpu_vm_update_directories(struct amdgpu_device *adev,
205 struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200206int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100207 struct amdgpu_vm *vm,
208 struct dma_fence **fence);
Christian König073440d2016-09-28 15:41:50 +0200209int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, struct amdgpu_vm *vm,
210 struct amdgpu_sync *sync);
211int amdgpu_vm_bo_update(struct amdgpu_device *adev,
212 struct amdgpu_bo_va *bo_va,
213 bool clear);
214void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
215 struct amdgpu_bo *bo);
216struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
217 struct amdgpu_bo *bo);
218struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
219 struct amdgpu_vm *vm,
220 struct amdgpu_bo *bo);
221int amdgpu_vm_bo_map(struct amdgpu_device *adev,
222 struct amdgpu_bo_va *bo_va,
223 uint64_t addr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +0100224 uint64_t size, uint64_t flags);
Christian König80f95c52017-03-13 10:13:39 +0100225int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
226 struct amdgpu_bo_va *bo_va,
227 uint64_t addr, uint64_t offset,
228 uint64_t size, uint64_t flags);
Christian König073440d2016-09-28 15:41:50 +0200229int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
230 struct amdgpu_bo_va *bo_va,
231 uint64_t addr);
Christian Königdc54d3d2017-03-13 10:13:38 +0100232int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
233 struct amdgpu_vm *vm,
234 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200235void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
236 struct amdgpu_bo_va *bo_va);
237
238#endif