blob: 4e465e817fe86bab73c1504d356c219c9dc0631e [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
Christian König35ba15f2017-02-13 14:22:58 +010053#define AMDGPU_PTE_VALID (1ULL << 0)
54#define AMDGPU_PTE_SYSTEM (1ULL << 1)
55#define AMDGPU_PTE_SNOOPED (1ULL << 2)
Christian König073440d2016-09-28 15:41:50 +020056
57/* VI only */
Christian König35ba15f2017-02-13 14:22:58 +010058#define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
Christian König073440d2016-09-28 15:41:50 +020059
Christian König35ba15f2017-02-13 14:22:58 +010060#define AMDGPU_PTE_READABLE (1ULL << 5)
61#define AMDGPU_PTE_WRITEABLE (1ULL << 6)
Christian König073440d2016-09-28 15:41:50 +020062
Alex Xie982a1342017-02-15 14:10:19 -050063#define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
Christian König073440d2016-09-28 15:41:50 +020064
Zhang, Jerryd0766e92017-04-19 09:53:29 +080065/* TILED for VEGA10, reserved for older ASICs */
66#define AMDGPU_PTE_PRT (1ULL << 51)
Christian König284710f2017-01-30 11:09:31 +010067
Alex Deuchercf2f0a32017-07-25 16:35:38 -040068/* PDE is handled as PTE for VEGA10 */
69#define AMDGPU_PDE_PTE (1ULL << 54)
70
Alex Deucherca020612017-03-03 15:23:14 -050071/* VEGA10 only */
72#define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
73#define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
74
Christian König073440d2016-09-28 15:41:50 +020075/* How to programm VM fault handling */
76#define AMDGPU_VM_FAULT_STOP_NEVER 0
77#define AMDGPU_VM_FAULT_STOP_FIRST 1
78#define AMDGPU_VM_FAULT_STOP_ALWAYS 2
79
Christian Königeb60ef22017-03-30 14:41:19 +020080/* max number of VMHUB */
81#define AMDGPU_MAX_VMHUBS 2
82#define AMDGPU_GFXHUB 0
83#define AMDGPU_MMHUB 1
84
85/* hardcode that limit for now */
86#define AMDGPU_VA_RESERVED_SIZE (8 << 20)
Chunming Zhouc3505772017-04-21 15:51:04 +080087/* max vmids dedicated for process */
88#define AMDGPU_VM_MAX_RESERVED_VMID 1
Christian Königeb60ef22017-03-30 14:41:19 +020089
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -040090#define AMDGPU_VM_CONTEXT_GFX 0
91#define AMDGPU_VM_CONTEXT_COMPUTE 1
92
93/* See vm_update_mode */
94#define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
95#define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
96
Christian Königec681542017-08-01 10:51:43 +020097/* base structure for tracking BO usage in a VM */
98struct amdgpu_vm_bo_base {
99 /* constant after initialization */
100 struct amdgpu_vm *vm;
101 struct amdgpu_bo *bo;
102
103 /* protected by bo being reserved */
104 struct list_head bo_list;
105
106 /* protected by spinlock */
107 struct list_head vm_status;
Christian König3d7d4d32017-08-23 16:13:33 +0200108
109 /* protected by the BO being reserved */
110 bool moved;
Christian Königec681542017-08-01 10:51:43 +0200111};
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400112
Christian König073440d2016-09-28 15:41:50 +0200113struct amdgpu_vm_pt {
Christian König3f3333f2017-08-03 14:02:13 +0200114 struct amdgpu_vm_bo_base base;
115 uint64_t addr;
Christian König67003a12016-10-12 14:46:26 +0200116
117 /* array of page tables, one for each directory entry */
Christian König3f3333f2017-08-03 14:02:13 +0200118 struct amdgpu_vm_pt *entries;
119 unsigned last_entry_used;
Christian König073440d2016-09-28 15:41:50 +0200120};
121
122struct amdgpu_vm {
123 /* tree of virtual addresses mapped */
124 struct rb_root va;
125
126 /* protecting invalidated */
127 spinlock_t status_lock;
128
Christian König3f3333f2017-08-03 14:02:13 +0200129 /* BOs who needs a validation */
130 struct list_head evicted;
131
Christian König073440d2016-09-28 15:41:50 +0200132 /* BOs moved, but not yet updated in the PT */
Christian König27c7b9a2017-08-01 11:27:36 +0200133 struct list_head moved;
Christian König073440d2016-09-28 15:41:50 +0200134
Christian König073440d2016-09-28 15:41:50 +0200135 /* BO mappings freed, but not yet updated in the PT */
136 struct list_head freed;
137
138 /* contains the page directory */
Christian König67003a12016-10-12 14:46:26 +0200139 struct amdgpu_vm_pt root;
Christian Königa24960f2016-10-12 13:20:52 +0200140 struct dma_fence *last_dir_update;
Christian König073440d2016-09-28 15:41:50 +0200141
Christian König073440d2016-09-28 15:41:50 +0200142 /* protecting freed */
143 spinlock_t freed_lock;
144
145 /* Scheduler entity for page table updates */
146 struct amd_sched_entity entity;
147
148 /* client id */
149 u64 client_id;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +0800150 /* dedicated to vm */
151 struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS];
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400152
153 /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
154 bool use_cpu_for_update;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400155
156 /* Flag to indicate ATS support from PTE for GFX9 */
157 bool pte_support_ats;
Christian König073440d2016-09-28 15:41:50 +0200158};
159
160struct amdgpu_vm_id {
161 struct list_head list;
Christian König073440d2016-09-28 15:41:50 +0200162 struct amdgpu_sync active;
Dave Airlie220196b2016-10-28 11:33:52 +1000163 struct dma_fence *last_flush;
Christian König073440d2016-09-28 15:41:50 +0200164 atomic64_t owner;
165
166 uint64_t pd_gpu_addr;
167 /* last flushed PD/PT update */
Dave Airlie220196b2016-10-28 11:33:52 +1000168 struct dma_fence *flushed_updates;
Christian König073440d2016-09-28 15:41:50 +0200169
170 uint32_t current_gpu_reset_count;
171
172 uint32_t gds_base;
173 uint32_t gds_size;
174 uint32_t gws_base;
175 uint32_t gws_size;
176 uint32_t oa_base;
177 uint32_t oa_size;
178};
179
Christian König76456702017-04-06 17:52:39 +0200180struct amdgpu_vm_id_manager {
181 struct mutex lock;
182 unsigned num_ids;
183 struct list_head ids_lru;
184 struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
Chunming Zhouc3505772017-04-21 15:51:04 +0800185 atomic_t reserved_vmid_num;
Christian König76456702017-04-06 17:52:39 +0200186};
187
Christian König073440d2016-09-28 15:41:50 +0200188struct amdgpu_vm_manager {
189 /* Handling of VMIDs */
Christian König76456702017-04-06 17:52:39 +0200190 struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
Christian König073440d2016-09-28 15:41:50 +0200191
192 /* Handling of VM fences */
193 u64 fence_context;
194 unsigned seqno[AMDGPU_MAX_RINGS];
195
Felix Kuehling22770e52017-03-28 20:24:53 -0400196 uint64_t max_pfn;
Christian König8437a092016-10-17 15:08:10 +0200197 uint32_t num_level;
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800198 uint64_t vm_size;
199 uint32_t block_size;
Roger Hee618d302017-08-11 20:00:41 +0800200 uint32_t fragment_size;
Christian König073440d2016-09-28 15:41:50 +0200201 /* vram base address for page table entry */
202 u64 vram_base_offset;
Christian König073440d2016-09-28 15:41:50 +0200203 /* vm pte handling */
204 const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
205 struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
206 unsigned vm_pte_num_rings;
207 atomic_t vm_pte_next_ring;
208 /* client id counter */
209 atomic64_t client_counter;
Christian König284710f2017-01-30 11:09:31 +0100210
211 /* partial resident texture handling */
212 spinlock_t prt_lock;
Christian König451bc8e2017-02-14 16:02:52 +0100213 atomic_t num_prt_users;
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400214
215 /* controls how VM page tables are updated for Graphics and Compute.
216 * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
217 * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
218 */
219 int vm_update_mode;
Christian König073440d2016-09-28 15:41:50 +0200220};
221
222void amdgpu_vm_manager_init(struct amdgpu_device *adev);
223void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400224int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
225 int vm_context);
Christian König073440d2016-09-28 15:41:50 +0200226void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
227void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
228 struct list_head *validated,
229 struct amdgpu_bo_list_entry *entry);
Christian König3f3333f2017-08-03 14:02:13 +0200230bool amdgpu_vm_ready(struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200231int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
232 int (*callback)(void *p, struct amdgpu_bo *bo),
233 void *param);
Christian König663e4572017-03-13 10:13:37 +0100234int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
235 struct amdgpu_vm *vm,
236 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200237int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Dave Airlie220196b2016-10-28 11:33:52 +1000238 struct amdgpu_sync *sync, struct dma_fence *fence,
Christian König073440d2016-09-28 15:41:50 +0200239 struct amdgpu_job *job);
Monk Liu8fdf0742017-06-06 17:25:13 +0800240int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
Christian König76456702017-04-06 17:52:39 +0200241void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
242 unsigned vmid);
Christian König32601d42017-05-10 20:06:58 +0200243void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
Christian König194d2162016-10-12 15:13:52 +0200244int amdgpu_vm_update_directories(struct amdgpu_device *adev,
245 struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200246int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100247 struct amdgpu_vm *vm,
248 struct dma_fence **fence);
Christian König27c7b9a2017-08-01 11:27:36 +0200249int amdgpu_vm_clear_moved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
250 struct amdgpu_sync *sync);
Christian König073440d2016-09-28 15:41:50 +0200251int amdgpu_vm_bo_update(struct amdgpu_device *adev,
252 struct amdgpu_bo_va *bo_va,
253 bool clear);
254void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +0200255 struct amdgpu_bo *bo, bool evicted);
Christian König073440d2016-09-28 15:41:50 +0200256struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
257 struct amdgpu_bo *bo);
258struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
259 struct amdgpu_vm *vm,
260 struct amdgpu_bo *bo);
261int amdgpu_vm_bo_map(struct amdgpu_device *adev,
262 struct amdgpu_bo_va *bo_va,
263 uint64_t addr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +0100264 uint64_t size, uint64_t flags);
Christian König80f95c52017-03-13 10:13:39 +0100265int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
266 struct amdgpu_bo_va *bo_va,
267 uint64_t addr, uint64_t offset,
268 uint64_t size, uint64_t flags);
Christian König073440d2016-09-28 15:41:50 +0200269int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
270 struct amdgpu_bo_va *bo_va,
271 uint64_t addr);
Christian Königdc54d3d2017-03-13 10:13:38 +0100272int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
273 struct amdgpu_vm *vm,
274 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200275void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
276 struct amdgpu_bo_va *bo_va);
Roger Hed07f14b2017-08-15 16:05:59 +0800277void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,
278 uint32_t fragment_size_default);
279void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size,
280 uint32_t fragment_size_default);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +0800281int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400282bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
283 struct amdgpu_job *job);
Alex Xiee59c0202017-06-01 09:42:59 -0400284void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
Christian König073440d2016-09-28 15:41:50 +0200285
286#endif