blob: edd2ea52dc00999fcb6267bf69c2215ab28f6644 [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
Felix Kuehling02208442017-08-25 20:40:26 -040027#include <linux/idr.h>
Lucas Stach1b1f42d2017-12-06 17:49:39 +010028#include <linux/kfifo.h>
29#include <linux/rbtree.h>
30#include <drm/gpu_scheduler.h>
Christian König073440d2016-09-28 15:41:50 +020031
Christian König073440d2016-09-28 15:41:50 +020032#include "amdgpu_sync.h"
33#include "amdgpu_ring.h"
34
35struct amdgpu_bo_va;
36struct amdgpu_job;
37struct amdgpu_bo_list_entry;
38
39/*
40 * GPUVM handling
41 */
42
43/* maximum number of VMIDs */
44#define AMDGPU_NUM_VM 16
45
46/* Maximum number of PTEs the hardware can write with one command */
47#define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF
48
49/* number of entries in page table */
Zhang, Jerry36b32a62017-03-29 16:08:32 +080050#define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
Christian König073440d2016-09-28 15:41:50 +020051
52/* PTBs (Page Table Blocks) need to be aligned to 32K */
53#define AMDGPU_VM_PTB_ALIGN_SIZE 32768
54
Christian König35ba15f2017-02-13 14:22:58 +010055#define AMDGPU_PTE_VALID (1ULL << 0)
56#define AMDGPU_PTE_SYSTEM (1ULL << 1)
57#define AMDGPU_PTE_SNOOPED (1ULL << 2)
Christian König073440d2016-09-28 15:41:50 +020058
59/* VI only */
Christian König35ba15f2017-02-13 14:22:58 +010060#define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
Christian König073440d2016-09-28 15:41:50 +020061
Christian König35ba15f2017-02-13 14:22:58 +010062#define AMDGPU_PTE_READABLE (1ULL << 5)
63#define AMDGPU_PTE_WRITEABLE (1ULL << 6)
Christian König073440d2016-09-28 15:41:50 +020064
Alex Xie982a1342017-02-15 14:10:19 -050065#define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
Christian König073440d2016-09-28 15:41:50 +020066
Zhang, Jerryd0766e92017-04-19 09:53:29 +080067/* TILED for VEGA10, reserved for older ASICs */
68#define AMDGPU_PTE_PRT (1ULL << 51)
Christian König284710f2017-01-30 11:09:31 +010069
Alex Deuchercf2f0a32017-07-25 16:35:38 -040070/* PDE is handled as PTE for VEGA10 */
71#define AMDGPU_PDE_PTE (1ULL << 54)
72
Christian König6a42fd62017-12-05 15:23:26 +010073/* PTE is handled as PDE for VEGA10 (Translate Further) */
74#define AMDGPU_PTE_TF (1ULL << 56)
75
76/* PDE Block Fragment Size for VEGA10 */
77#define AMDGPU_PDE_BFS(a) ((uint64_t)a << 59)
78
Alex Deucherca020612017-03-03 15:23:14 -050079/* VEGA10 only */
80#define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
81#define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
82
Yong Zhao6d16dac2017-08-31 15:55:00 -040083/* For Raven */
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
Christian König073440d2016-09-28 15:41:50 +020093/* 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
Christian Königeb60ef22017-03-30 14:41:19 +020098/* 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 */
Christian Königff4cd382017-11-06 15:25:37 +0100104#define AMDGPU_VA_RESERVED_SIZE (8ULL << 20)
105
Christian Königbb7939b2017-11-06 15:37:01 +0100106/* 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
Chunming Zhouc3505772017-04-21 15:51:04 +0800119/* max vmids dedicated for process */
120#define AMDGPU_VM_MAX_RESERVED_VMID 1
Christian Königeb60ef22017-03-30 14:41:19 +0200121
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400122#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
Chunming Zhou196f7482017-12-13 14:22:54 +0800129/* VMPT level enumerate, and the hiberachy is:
130 * PDB2->PDB1->PDB0->PTB
131 */
132enum amdgpu_vm_level {
133 AMDGPU_VM_PDB2,
134 AMDGPU_VM_PDB1,
135 AMDGPU_VM_PDB0,
136 AMDGPU_VM_PTB
137};
138
Christian Königec681542017-08-01 10:51:43 +0200139/* base structure for tracking BO usage in a VM */
140struct 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;
Christian König3d7d4d32017-08-23 16:13:33 +0200150
151 /* protected by the BO being reserved */
152 bool moved;
Christian Königec681542017-08-01 10:51:43 +0200153};
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400154
Christian König073440d2016-09-28 15:41:50 +0200155struct amdgpu_vm_pt {
Christian König3f3333f2017-08-03 14:02:13 +0200156 struct amdgpu_vm_bo_base base;
Christian König78eb2f02017-11-30 15:41:28 +0100157 bool huge;
Christian König67003a12016-10-12 14:46:26 +0200158
159 /* array of page tables, one for each directory entry */
Christian König3f3333f2017-08-03 14:02:13 +0200160 struct amdgpu_vm_pt *entries;
Christian König073440d2016-09-28 15:41:50 +0200161};
162
Felix Kuehlinga2f14822017-08-26 02:43:06 -0400163#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)
166
Christian König073440d2016-09-28 15:41:50 +0200167struct amdgpu_vm {
168 /* tree of virtual addresses mapped */
Davidlohr Buesof808c132017-09-08 16:15:08 -0700169 struct rb_root_cached va;
Christian König073440d2016-09-28 15:41:50 +0200170
171 /* protecting invalidated */
172 spinlock_t status_lock;
173
Christian König3f3333f2017-08-03 14:02:13 +0200174 /* BOs who needs a validation */
175 struct list_head evicted;
176
Christian Königea097292017-08-09 14:15:46 +0200177 /* PT BOs which relocated and their parent need an update */
178 struct list_head relocated;
179
Christian König073440d2016-09-28 15:41:50 +0200180 /* BOs moved, but not yet updated in the PT */
Christian König27c7b9a2017-08-01 11:27:36 +0200181 struct list_head moved;
Christian König073440d2016-09-28 15:41:50 +0200182
Christian König073440d2016-09-28 15:41:50 +0200183 /* BO mappings freed, but not yet updated in the PT */
184 struct list_head freed;
185
186 /* contains the page directory */
Christian König67003a12016-10-12 14:46:26 +0200187 struct amdgpu_vm_pt root;
Christian Königd5884512017-09-08 14:09:41 +0200188 struct dma_fence *last_update;
Christian König073440d2016-09-28 15:41:50 +0200189
Christian König073440d2016-09-28 15:41:50 +0200190 /* protecting freed */
191 spinlock_t freed_lock;
192
193 /* Scheduler entity for page table updates */
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100194 struct drm_sched_entity entity;
Christian König073440d2016-09-28 15:41:50 +0200195
Felix Kuehling02208442017-08-25 20:40:26 -0400196 /* client id and PASID (TODO: replace client_id with PASID) */
Christian König073440d2016-09-28 15:41:50 +0200197 u64 client_id;
Felix Kuehling02208442017-08-25 20:40:26 -0400198 unsigned int pasid;
Chunming Zhou36bbf3b2017-04-20 16:17:34 +0800199 /* dedicated to vm */
200 struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS];
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400201
202 /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
203 bool use_cpu_for_update;
Yong Zhao51ac7ee2017-07-27 12:48:22 -0400204
205 /* Flag to indicate ATS support from PTE for GFX9 */
206 bool pte_support_ats;
Felix Kuehlinga2f14822017-08-26 02:43:06 -0400207
Felix Kuehlingc98171c2017-09-21 16:26:41 -0400208 /* Up to 128 pending retry page faults */
Felix Kuehlinga2f14822017-08-26 02:43:06 -0400209 DECLARE_KFIFO(faults, u64, 128);
Felix Kuehlingc98171c2017-09-21 16:26:41 -0400210
211 /* Limit non-retry fault storms */
212 unsigned int fault_credit;
Christian König073440d2016-09-28 15:41:50 +0200213};
214
215struct amdgpu_vm_id {
216 struct list_head list;
Christian König073440d2016-09-28 15:41:50 +0200217 struct amdgpu_sync active;
Dave Airlie220196b2016-10-28 11:33:52 +1000218 struct dma_fence *last_flush;
Christian König073440d2016-09-28 15:41:50 +0200219 atomic64_t owner;
220
221 uint64_t pd_gpu_addr;
222 /* last flushed PD/PT update */
Dave Airlie220196b2016-10-28 11:33:52 +1000223 struct dma_fence *flushed_updates;
Christian König073440d2016-09-28 15:41:50 +0200224
225 uint32_t current_gpu_reset_count;
226
227 uint32_t gds_base;
228 uint32_t gds_size;
229 uint32_t gws_base;
230 uint32_t gws_size;
231 uint32_t oa_base;
232 uint32_t oa_size;
233};
234
Christian König76456702017-04-06 17:52:39 +0200235struct amdgpu_vm_id_manager {
236 struct mutex lock;
237 unsigned num_ids;
238 struct list_head ids_lru;
239 struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
Chunming Zhouc3505772017-04-21 15:51:04 +0800240 atomic_t reserved_vmid_num;
Christian König76456702017-04-06 17:52:39 +0200241};
242
Christian König073440d2016-09-28 15:41:50 +0200243struct amdgpu_vm_manager {
244 /* Handling of VMIDs */
Christian König76456702017-04-06 17:52:39 +0200245 struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
Christian König073440d2016-09-28 15:41:50 +0200246
247 /* Handling of VM fences */
248 u64 fence_context;
249 unsigned seqno[AMDGPU_MAX_RINGS];
250
Felix Kuehling22770e52017-03-28 20:24:53 -0400251 uint64_t max_pfn;
Christian König8437a092016-10-17 15:08:10 +0200252 uint32_t num_level;
Zhang, Jerry36b32a62017-03-29 16:08:32 +0800253 uint32_t block_size;
Roger Hee618d302017-08-11 20:00:41 +0800254 uint32_t fragment_size;
Chunming Zhou196f7482017-12-13 14:22:54 +0800255 enum amdgpu_vm_level root_level;
Christian König073440d2016-09-28 15:41:50 +0200256 /* vram base address for page table entry */
257 u64 vram_base_offset;
Christian König073440d2016-09-28 15:41:50 +0200258 /* 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;
Christian König284710f2017-01-30 11:09:31 +0100265
266 /* partial resident texture handling */
267 spinlock_t prt_lock;
Christian König451bc8e2017-02-14 16:02:52 +0100268 atomic_t num_prt_users;
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400269
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
273 */
274 int vm_update_mode;
Felix Kuehling02208442017-08-25 20:40:26 -0400275
276 /* PASID to VM mapping, will be used in interrupt context to
277 * look up VM of a page fault
278 */
279 struct idr pasid_idr;
280 spinlock_t pasid_lock;
Christian König073440d2016-09-28 15:41:50 +0200281};
282
Felix Kuehling02208442017-08-25 20:40:26 -0400283int amdgpu_vm_alloc_pasid(unsigned int bits);
284void amdgpu_vm_free_pasid(unsigned int pasid);
Christian König073440d2016-09-28 15:41:50 +0200285void amdgpu_vm_manager_init(struct amdgpu_device *adev);
286void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
Harish Kasiviswanathan9a4b7d42017-06-09 11:26:57 -0400287int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Felix Kuehling02208442017-08-25 20:40:26 -0400288 int vm_context, unsigned int pasid);
Christian König073440d2016-09-28 15:41:50 +0200289void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
Felix Kuehlingc98171c2017-09-21 16:26:41 -0400290bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
291 unsigned int pasid);
Christian König073440d2016-09-28 15:41:50 +0200292void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
293 struct list_head *validated,
294 struct amdgpu_bo_list_entry *entry);
Christian König3f3333f2017-08-03 14:02:13 +0200295bool amdgpu_vm_ready(struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200296int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
297 int (*callback)(void *p, struct amdgpu_bo *bo),
298 void *param);
Christian König663e4572017-03-13 10:13:37 +0100299int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
300 struct amdgpu_vm *vm,
301 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200302int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Dave Airlie220196b2016-10-28 11:33:52 +1000303 struct amdgpu_sync *sync, struct dma_fence *fence,
Christian König073440d2016-09-28 15:41:50 +0200304 struct amdgpu_job *job);
Monk Liu8fdf0742017-06-06 17:25:13 +0800305int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
Christian König76456702017-04-06 17:52:39 +0200306void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
307 unsigned vmid);
Christian König32601d42017-05-10 20:06:58 +0200308void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
Christian König194d2162016-10-12 15:13:52 +0200309int amdgpu_vm_update_directories(struct amdgpu_device *adev,
310 struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200311int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100312 struct amdgpu_vm *vm,
313 struct dma_fence **fence);
Christian König73fb16e2017-08-16 11:13:48 +0200314int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
Christian König4e55eb32017-09-11 16:54:59 +0200315 struct amdgpu_vm *vm);
Christian König073440d2016-09-28 15:41:50 +0200316int amdgpu_vm_bo_update(struct amdgpu_device *adev,
317 struct amdgpu_bo_va *bo_va,
318 bool clear);
319void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
Christian König3f3333f2017-08-03 14:02:13 +0200320 struct amdgpu_bo *bo, bool evicted);
Christian König073440d2016-09-28 15:41:50 +0200321struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
322 struct amdgpu_bo *bo);
323struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
324 struct amdgpu_vm *vm,
325 struct amdgpu_bo *bo);
326int amdgpu_vm_bo_map(struct amdgpu_device *adev,
327 struct amdgpu_bo_va *bo_va,
328 uint64_t addr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +0100329 uint64_t size, uint64_t flags);
Christian König80f95c52017-03-13 10:13:39 +0100330int 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);
Christian König073440d2016-09-28 15:41:50 +0200334int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
335 struct amdgpu_bo_va *bo_va,
336 uint64_t addr);
Christian Königdc54d3d2017-03-13 10:13:38 +0100337int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
338 struct amdgpu_vm *vm,
339 uint64_t saddr, uint64_t size);
Christian Königaebc5e62017-09-06 16:55:16 +0200340struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
341 uint64_t addr);
Christian König073440d2016-09-28 15:41:50 +0200342void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
343 struct amdgpu_bo_va *bo_va);
Christian Königfdd5faa2017-11-04 16:51:44 +0100344void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
Christian Königf3368122017-11-23 12:57:18 +0100345 uint32_t fragment_size_default, unsigned max_level,
346 unsigned max_bits);
Chunming Zhoucfbcacf2017-04-24 11:09:04 +0800347int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
Chunming Zhoub9bf33d2017-05-11 14:52:48 -0400348bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
349 struct amdgpu_job *job);
Alex Xiee59c0202017-06-01 09:42:59 -0400350void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
Christian König073440d2016-09-28 15:41:50 +0200351
352#endif