amdgpu_vm.h 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2016 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Christian König
 */
#ifndef __AMDGPU_VM_H__
#define __AMDGPU_VM_H__

#include <linux/rbtree.h>
28
#include <linux/idr.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include "gpu_scheduler.h"
#include "amdgpu_sync.h"
#include "amdgpu_ring.h"

struct amdgpu_bo_va;
struct amdgpu_job;
struct amdgpu_bo_list_entry;

/*
 * GPUVM handling
 */

/* maximum number of VMIDs */
#define AMDGPU_NUM_VM	16

/* Maximum number of PTEs the hardware can write with one command */
#define AMDGPU_VM_MAX_UPDATE_SIZE	0x3FFFF

/* number of entries in page table */
49
#define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
50 51 52 53

/* PTBs (Page Table Blocks) need to be aligned to 32K */
#define AMDGPU_VM_PTB_ALIGN_SIZE   32768

C
Christian König 已提交
54 55 56
#define AMDGPU_PTE_VALID	(1ULL << 0)
#define AMDGPU_PTE_SYSTEM	(1ULL << 1)
#define AMDGPU_PTE_SNOOPED	(1ULL << 2)
57 58

/* VI only */
C
Christian König 已提交
59
#define AMDGPU_PTE_EXECUTABLE	(1ULL << 4)
60

C
Christian König 已提交
61 62
#define AMDGPU_PTE_READABLE	(1ULL << 5)
#define AMDGPU_PTE_WRITEABLE	(1ULL << 6)
63

64
#define AMDGPU_PTE_FRAG(x)	((x & 0x1fULL) << 7)
65

66 67
/* TILED for VEGA10, reserved for older ASICs  */
#define AMDGPU_PTE_PRT		(1ULL << 51)
68

69 70 71
/* PDE is handled as PTE for VEGA10 */
#define AMDGPU_PDE_PTE		(1ULL << 54)

72 73 74 75
/* VEGA10 only */
#define AMDGPU_PTE_MTYPE(a)    ((uint64_t)a << 57)
#define AMDGPU_PTE_MTYPE_MASK	AMDGPU_PTE_MTYPE(3ULL)

76 77 78 79 80 81 82 83 84 85
/* For Raven */
#define AMDGPU_MTYPE_CC 2

#define AMDGPU_PTE_DEFAULT_ATC  (AMDGPU_PTE_SYSTEM      \
                                | AMDGPU_PTE_SNOOPED    \
                                | AMDGPU_PTE_EXECUTABLE \
                                | AMDGPU_PTE_READABLE   \
                                | AMDGPU_PTE_WRITEABLE  \
                                | AMDGPU_PTE_MTYPE(AMDGPU_MTYPE_CC))

86 87 88 89 90
/* How to programm VM fault handling */
#define AMDGPU_VM_FAULT_STOP_NEVER	0
#define AMDGPU_VM_FAULT_STOP_FIRST	1
#define AMDGPU_VM_FAULT_STOP_ALWAYS	2

91 92 93 94 95 96
/* max number of VMHUB */
#define AMDGPU_MAX_VMHUBS			2
#define AMDGPU_GFXHUB				0
#define AMDGPU_MMHUB				1

/* hardcode that limit for now */
97 98
#define AMDGPU_VA_RESERVED_SIZE			(8ULL << 20)

99 100 101 102 103 104 105 106 107 108 109 110 111
/* VA hole for 48bit addresses on Vega10 */
#define AMDGPU_VA_HOLE_START			0x0000800000000000ULL
#define AMDGPU_VA_HOLE_END			0xffff800000000000ULL

/*
 * Hardware is programmed as if the hole doesn't exists with start and end
 * address values.
 *
 * This mask is used to remove the upper 16bits of the VA and so come up with
 * the linear addr value.
 */
#define AMDGPU_VA_HOLE_MASK			0x0000ffffffffffffULL

112 113
/* max vmids dedicated for process */
#define AMDGPU_VM_MAX_RESERVED_VMID	1
114

115 116 117 118 119 120 121
#define AMDGPU_VM_CONTEXT_GFX 0
#define AMDGPU_VM_CONTEXT_COMPUTE 1

/* See vm_update_mode */
#define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
#define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)

122 123 124 125 126 127 128 129 130 131 132
/* base structure for tracking BO usage in a VM */
struct amdgpu_vm_bo_base {
	/* constant after initialization */
	struct amdgpu_vm		*vm;
	struct amdgpu_bo		*bo;

	/* protected by bo being reserved */
	struct list_head		bo_list;

	/* protected by spinlock */
	struct list_head		vm_status;
133 134 135

	/* protected by the BO being reserved */
	bool				moved;
136
};
137

138
struct amdgpu_vm_pt {
139 140
	struct amdgpu_vm_bo_base	base;
	uint64_t			addr;
141 142

	/* array of page tables, one for each directory entry */
143 144
	struct amdgpu_vm_pt		*entries;
	unsigned			last_entry_used;
145 146
};

147 148 149 150
#define AMDGPU_VM_FAULT(pasid, addr) (((u64)(pasid) << 48) | (addr))
#define AMDGPU_VM_FAULT_PASID(fault) ((u64)(fault) >> 48)
#define AMDGPU_VM_FAULT_ADDR(fault)  ((u64)(fault) & 0xfffffffff000ULL)

151 152
struct amdgpu_vm {
	/* tree of virtual addresses mapped */
153
	struct rb_root_cached	va;
154 155 156 157

	/* protecting invalidated */
	spinlock_t		status_lock;

158 159 160
	/* BOs who needs a validation */
	struct list_head	evicted;

161 162 163
	/* PT BOs which relocated and their parent need an update */
	struct list_head	relocated;

164
	/* BOs moved, but not yet updated in the PT */
165
	struct list_head	moved;
166 167 168 169 170

	/* BO mappings freed, but not yet updated in the PT */
	struct list_head	freed;

	/* contains the page directory */
171
	struct amdgpu_vm_pt     root;
172
	struct dma_fence	*last_update;
173 174 175 176 177 178 179

	/* protecting freed */
	spinlock_t		freed_lock;

	/* Scheduler entity for page table updates */
	struct amd_sched_entity	entity;

180
	/* client id and PASID (TODO: replace client_id with PASID) */
181
	u64                     client_id;
182
	unsigned int		pasid;
183 184
	/* dedicated to vm */
	struct amdgpu_vm_id	*reserved_vmid[AMDGPU_MAX_VMHUBS];
185 186 187

	/* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
	bool                    use_cpu_for_update;
Y
Yong Zhao 已提交
188 189 190

	/* Flag to indicate ATS support from PTE for GFX9 */
	bool			pte_support_ats;
191

192
	/* Up to 128 pending retry page faults */
193
	DECLARE_KFIFO(faults, u64, 128);
194 195 196

	/* Limit non-retry fault storms */
	unsigned int		fault_credit;
197 198 199 200 201
};

struct amdgpu_vm_id {
	struct list_head	list;
	struct amdgpu_sync	active;
202
	struct dma_fence		*last_flush;
203 204 205 206
	atomic64_t		owner;

	uint64_t		pd_gpu_addr;
	/* last flushed PD/PT update */
207
	struct dma_fence		*flushed_updates;
208 209 210 211 212 213 214 215 216 217 218

	uint32_t                current_gpu_reset_count;

	uint32_t		gds_base;
	uint32_t		gds_size;
	uint32_t		gws_base;
	uint32_t		gws_size;
	uint32_t		oa_base;
	uint32_t		oa_size;
};

219 220 221 222 223
struct amdgpu_vm_id_manager {
	struct mutex		lock;
	unsigned		num_ids;
	struct list_head	ids_lru;
	struct amdgpu_vm_id	ids[AMDGPU_NUM_VM];
224
	atomic_t		reserved_vmid_num;
225 226
};

227 228
struct amdgpu_vm_manager {
	/* Handling of VMIDs */
229
	struct amdgpu_vm_id_manager		id_mgr[AMDGPU_MAX_VMHUBS];
230 231 232 233 234

	/* Handling of VM fences */
	u64					fence_context;
	unsigned				seqno[AMDGPU_MAX_RINGS];

F
Felix Kuehling 已提交
235
	uint64_t				max_pfn;
236
	uint32_t				num_level;
237
	uint32_t				block_size;
238
	uint32_t				fragment_size;
239 240 241 242 243 244 245 246 247
	/* vram base address for page table entry  */
	u64					vram_base_offset;
	/* vm pte handling */
	const struct amdgpu_vm_pte_funcs        *vm_pte_funcs;
	struct amdgpu_ring                      *vm_pte_rings[AMDGPU_MAX_RINGS];
	unsigned				vm_pte_num_rings;
	atomic_t				vm_pte_next_ring;
	/* client id counter */
	atomic64_t				client_counter;
248 249 250

	/* partial resident texture handling */
	spinlock_t				prt_lock;
251
	atomic_t				num_prt_users;
252 253 254 255 256 257

	/* controls how VM page tables are updated for Graphics and Compute.
	 * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
	 * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
	 */
	int					vm_update_mode;
258 259 260 261 262 263

	/* PASID to VM mapping, will be used in interrupt context to
	 * look up VM of a page fault
	 */
	struct idr				pasid_idr;
	spinlock_t				pasid_lock;
264 265
};

266 267
int amdgpu_vm_alloc_pasid(unsigned int bits);
void amdgpu_vm_free_pasid(unsigned int pasid);
268 269
void amdgpu_vm_manager_init(struct amdgpu_device *adev);
void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
270
int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
271
		   int vm_context, unsigned int pasid);
272
void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
273 274
bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
				  unsigned int pasid);
275 276 277
void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
			 struct list_head *validated,
			 struct amdgpu_bo_list_entry *entry);
278
bool amdgpu_vm_ready(struct amdgpu_vm *vm);
279 280 281
int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
			      int (*callback)(void *p, struct amdgpu_bo *bo),
			      void *param);
282 283 284
int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
			struct amdgpu_vm *vm,
			uint64_t saddr, uint64_t size);
285
int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
286
		      struct amdgpu_sync *sync, struct dma_fence *fence,
287
		      struct amdgpu_job *job);
M
Monk Liu 已提交
288
int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
289 290
void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
			unsigned vmid);
291
void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
292 293
int amdgpu_vm_update_directories(struct amdgpu_device *adev,
				 struct amdgpu_vm *vm);
294
int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
295 296
			  struct amdgpu_vm *vm,
			  struct dma_fence **fence);
297
int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
298
			   struct amdgpu_vm *vm);
299 300 301 302
int amdgpu_vm_bo_update(struct amdgpu_device *adev,
			struct amdgpu_bo_va *bo_va,
			bool clear);
void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
303
			     struct amdgpu_bo *bo, bool evicted);
304 305 306 307 308 309 310 311
struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
				       struct amdgpu_bo *bo);
struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
				      struct amdgpu_vm *vm,
				      struct amdgpu_bo *bo);
int amdgpu_vm_bo_map(struct amdgpu_device *adev,
		     struct amdgpu_bo_va *bo_va,
		     uint64_t addr, uint64_t offset,
312
		     uint64_t size, uint64_t flags);
313 314 315 316
int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
			     struct amdgpu_bo_va *bo_va,
			     uint64_t addr, uint64_t offset,
			     uint64_t size, uint64_t flags);
317 318 319
int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
		       struct amdgpu_bo_va *bo_va,
		       uint64_t addr);
320 321 322
int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
				struct amdgpu_vm *vm,
				uint64_t saddr, uint64_t size);
323 324
struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
							 uint64_t addr);
325 326
void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
		      struct amdgpu_bo_va *bo_va);
327
void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
328
			   uint32_t fragment_size_default, unsigned max_level);
C
Chunming Zhou 已提交
329
int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
330 331
bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
				  struct amdgpu_job *job);
332
void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
333 334

#endif