blob: ab0429d12992fc3d549502d3471f958b5bad17ff [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 */
48#define AMDGPU_VM_PTE_COUNT (1 << amdgpu_vm_block_size)
49
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
Christian König073440d2016-09-28 15:41:50 +020070/* How to programm VM fault handling */
71#define AMDGPU_VM_FAULT_STOP_NEVER 0
72#define AMDGPU_VM_FAULT_STOP_FIRST 1
73#define AMDGPU_VM_FAULT_STOP_ALWAYS 2
74
75struct amdgpu_vm_pt {
76 struct amdgpu_bo *bo;
77 uint64_t addr;
78};
79
80struct amdgpu_vm {
81 /* tree of virtual addresses mapped */
82 struct rb_root va;
83
84 /* protecting invalidated */
85 spinlock_t status_lock;
86
87 /* BOs moved, but not yet updated in the PT */
88 struct list_head invalidated;
89
90 /* BOs cleared in the PT because of a move */
91 struct list_head cleared;
92
93 /* BO mappings freed, but not yet updated in the PT */
94 struct list_head freed;
95
96 /* contains the page directory */
97 struct amdgpu_bo *page_directory;
98 unsigned max_pde_used;
Dave Airlie220196b2016-10-28 11:33:52 +100099 struct dma_fence *page_directory_fence;
Christian König073440d2016-09-28 15:41:50 +0200100 uint64_t last_eviction_counter;
101
102 /* array of page tables, one for each page directory entry */
103 struct amdgpu_vm_pt *page_tables;
104
105 /* for id and flush management per ring */
106 struct amdgpu_vm_id *ids[AMDGPU_MAX_RINGS];
107
108 /* protecting freed */
109 spinlock_t freed_lock;
110
111 /* Scheduler entity for page table updates */
112 struct amd_sched_entity entity;
113
114 /* client id */
115 u64 client_id;
Monk Liubd7de272017-01-09 15:23:17 +0800116 /* each VM will map on CSA */
117 struct amdgpu_bo_va *csa_bo_va;
Christian König073440d2016-09-28 15:41:50 +0200118};
119
120struct amdgpu_vm_id {
121 struct list_head list;
Dave Airlie220196b2016-10-28 11:33:52 +1000122 struct dma_fence *first;
Christian König073440d2016-09-28 15:41:50 +0200123 struct amdgpu_sync active;
Dave Airlie220196b2016-10-28 11:33:52 +1000124 struct dma_fence *last_flush;
Christian König073440d2016-09-28 15:41:50 +0200125 atomic64_t owner;
126
127 uint64_t pd_gpu_addr;
128 /* last flushed PD/PT update */
Dave Airlie220196b2016-10-28 11:33:52 +1000129 struct dma_fence *flushed_updates;
Christian König073440d2016-09-28 15:41:50 +0200130
131 uint32_t current_gpu_reset_count;
132
133 uint32_t gds_base;
134 uint32_t gds_size;
135 uint32_t gws_base;
136 uint32_t gws_size;
137 uint32_t oa_base;
138 uint32_t oa_size;
139};
140
141struct amdgpu_vm_manager {
142 /* Handling of VMIDs */
143 struct mutex lock;
144 unsigned num_ids;
145 struct list_head ids_lru;
146 struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
147
148 /* Handling of VM fences */
149 u64 fence_context;
150 unsigned seqno[AMDGPU_MAX_RINGS];
151
152 uint32_t max_pfn;
153 /* vram base address for page table entry */
154 u64 vram_base_offset;
155 /* is vm enabled? */
156 bool enabled;
157 /* vm pte handling */
158 const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
159 struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
160 unsigned vm_pte_num_rings;
161 atomic_t vm_pte_next_ring;
162 /* client id counter */
163 atomic64_t client_counter;
Christian König284710f2017-01-30 11:09:31 +0100164
165 /* partial resident texture handling */
166 spinlock_t prt_lock;
Christian König451bc8e2017-02-14 16:02:52 +0100167 atomic_t num_prt_users;
Christian König073440d2016-09-28 15:41:50 +0200168};
169
170void amdgpu_vm_manager_init(struct amdgpu_device *adev);
171void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
172int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm);
173void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
174void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
175 struct list_head *validated,
176 struct amdgpu_bo_list_entry *entry);
177int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
178 int (*callback)(void *p, struct amdgpu_bo *bo),
179 void *param);
180void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
181 struct amdgpu_vm *vm);
Christian König663e4572017-03-13 10:13:37 +0100182int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
183 struct amdgpu_vm *vm,
184 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200185int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Dave Airlie220196b2016-10-28 11:33:52 +1000186 struct amdgpu_sync *sync, struct dma_fence *fence,
Christian König073440d2016-09-28 15:41:50 +0200187 struct amdgpu_job *job);
188int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job);
189void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id);
190int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
191 struct amdgpu_vm *vm);
192int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
193 struct amdgpu_vm *vm);
194int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, struct amdgpu_vm *vm,
195 struct amdgpu_sync *sync);
196int amdgpu_vm_bo_update(struct amdgpu_device *adev,
197 struct amdgpu_bo_va *bo_va,
198 bool clear);
199void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
200 struct amdgpu_bo *bo);
201struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
202 struct amdgpu_bo *bo);
203struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
204 struct amdgpu_vm *vm,
205 struct amdgpu_bo *bo);
206int amdgpu_vm_bo_map(struct amdgpu_device *adev,
207 struct amdgpu_bo_va *bo_va,
208 uint64_t addr, uint64_t offset,
Christian König268c3002017-01-18 14:49:43 +0100209 uint64_t size, uint64_t flags);
Christian König80f95c52017-03-13 10:13:39 +0100210int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
211 struct amdgpu_bo_va *bo_va,
212 uint64_t addr, uint64_t offset,
213 uint64_t size, uint64_t flags);
Christian König073440d2016-09-28 15:41:50 +0200214int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
215 struct amdgpu_bo_va *bo_va,
216 uint64_t addr);
Christian Königdc54d3d2017-03-13 10:13:38 +0100217int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
218 struct amdgpu_vm *vm,
219 uint64_t saddr, uint64_t size);
Christian König073440d2016-09-28 15:41:50 +0200220void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
221 struct amdgpu_bo_va *bo_va);
222
223#endif