blob: 80b2deb097ad9cc7838dae8f50cebe672ced18fd [file] [log] [blame]
Zhi Wang2707e442016-03-28 23:23:16 +08001/*
2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Zhi Wang <zhi.a.wang@intel.com>
25 * Zhenyu Wang <zhenyuw@linux.intel.com>
26 * Xiao Zheng <xiao.zheng@intel.com>
27 *
28 * Contributors:
29 * Min He <min.he@intel.com>
30 * Bing Niu <bing.niu@intel.com>
31 *
32 */
33
34#ifndef _GVT_GTT_H_
35#define _GVT_GTT_H_
36
Zhi Wang9556e112017-10-10 13:51:32 +080037#define I915_GTT_PAGE_SHIFT 12
38#define I915_GTT_PAGE_MASK (~(I915_GTT_PAGE_SIZE - 1))
Zhi Wang2707e442016-03-28 23:23:16 +080039
40struct intel_vgpu_mm;
41
42#define INTEL_GVT_GTT_HASH_BITS 8
43#define INTEL_GVT_INVALID_ADDR (~0UL)
44
45struct intel_gvt_gtt_entry {
46 u64 val64;
47 int type;
48};
49
50struct intel_gvt_gtt_pte_ops {
Changbin Du4b2dbbc2017-08-02 15:06:37 +080051 int (*get_entry)(void *pt,
52 struct intel_gvt_gtt_entry *e,
53 unsigned long index,
54 bool hypervisor_access,
55 unsigned long gpa,
56 struct intel_vgpu *vgpu);
57 int (*set_entry)(void *pt,
58 struct intel_gvt_gtt_entry *e,
59 unsigned long index,
60 bool hypervisor_access,
61 unsigned long gpa,
62 struct intel_vgpu *vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +080063 bool (*test_present)(struct intel_gvt_gtt_entry *e);
64 void (*clear_present)(struct intel_gvt_gtt_entry *e);
65 bool (*test_pse)(struct intel_gvt_gtt_entry *e);
66 void (*set_pfn)(struct intel_gvt_gtt_entry *e, unsigned long pfn);
67 unsigned long (*get_pfn)(struct intel_gvt_gtt_entry *e);
68};
69
70struct intel_gvt_gtt_gma_ops {
71 unsigned long (*gma_to_ggtt_pte_index)(unsigned long gma);
72 unsigned long (*gma_to_pte_index)(unsigned long gma);
73 unsigned long (*gma_to_pde_index)(unsigned long gma);
74 unsigned long (*gma_to_l3_pdp_index)(unsigned long gma);
75 unsigned long (*gma_to_l4_pdp_index)(unsigned long gma);
76 unsigned long (*gma_to_pml4_index)(unsigned long gma);
77};
78
79struct intel_gvt_gtt {
80 struct intel_gvt_gtt_pte_ops *pte_ops;
81 struct intel_gvt_gtt_gma_ops *gma_ops;
82 int (*mm_alloc_page_table)(struct intel_vgpu_mm *mm);
83 void (*mm_free_page_table)(struct intel_vgpu_mm *mm);
84 struct list_head oos_page_use_list_head;
85 struct list_head oos_page_free_list_head;
86 struct list_head mm_lru_list_head;
Ping Gaod650ac02016-12-08 10:14:48 +080087
88 struct page *scratch_ggtt_page;
89 unsigned long scratch_ggtt_mfn;
Zhi Wang2707e442016-03-28 23:23:16 +080090};
91
92enum {
93 INTEL_GVT_MM_GGTT = 0,
94 INTEL_GVT_MM_PPGTT,
95};
96
Ping Gao3b6411c2016-11-04 13:47:35 +080097typedef enum {
98 GTT_TYPE_INVALID = -1,
99
100 GTT_TYPE_GGTT_PTE,
101
102 GTT_TYPE_PPGTT_PTE_4K_ENTRY,
103 GTT_TYPE_PPGTT_PTE_2M_ENTRY,
104 GTT_TYPE_PPGTT_PTE_1G_ENTRY,
105
106 GTT_TYPE_PPGTT_PTE_ENTRY,
107
108 GTT_TYPE_PPGTT_PDE_ENTRY,
109 GTT_TYPE_PPGTT_PDP_ENTRY,
110 GTT_TYPE_PPGTT_PML4_ENTRY,
111
112 GTT_TYPE_PPGTT_ROOT_ENTRY,
113
114 GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
115 GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
116
117 GTT_TYPE_PPGTT_ENTRY,
118
119 GTT_TYPE_PPGTT_PTE_PT,
120 GTT_TYPE_PPGTT_PDE_PT,
121 GTT_TYPE_PPGTT_PDP_PT,
122 GTT_TYPE_PPGTT_PML4_PT,
123
124 GTT_TYPE_MAX,
125} intel_gvt_gtt_type_t;
126
Zhi Wang2707e442016-03-28 23:23:16 +0800127struct intel_vgpu_mm {
128 int type;
129 bool initialized;
130 bool shadowed;
131
132 int page_table_entry_type;
133 u32 page_table_entry_size;
134 u32 page_table_entry_cnt;
135 void *virtual_page_table;
136 void *shadow_page_table;
137
138 int page_table_level;
139 bool has_shadow_page_table;
140 u32 pde_base_index;
141
142 struct list_head list;
143 struct kref ref;
144 atomic_t pincount;
145 struct list_head lru_list;
146 struct intel_vgpu *vgpu;
147};
148
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800149extern int intel_vgpu_mm_get_entry(
Zhi Wang2707e442016-03-28 23:23:16 +0800150 struct intel_vgpu_mm *mm,
151 void *page_table, struct intel_gvt_gtt_entry *e,
152 unsigned long index);
153
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800154extern int intel_vgpu_mm_set_entry(
Zhi Wang2707e442016-03-28 23:23:16 +0800155 struct intel_vgpu_mm *mm,
156 void *page_table, struct intel_gvt_gtt_entry *e,
157 unsigned long index);
158
159#define ggtt_get_guest_entry(mm, e, index) \
160 intel_vgpu_mm_get_entry(mm, mm->virtual_page_table, e, index)
161
162#define ggtt_set_guest_entry(mm, e, index) \
163 intel_vgpu_mm_set_entry(mm, mm->virtual_page_table, e, index)
164
165#define ggtt_get_shadow_entry(mm, e, index) \
166 intel_vgpu_mm_get_entry(mm, mm->shadow_page_table, e, index)
167
168#define ggtt_set_shadow_entry(mm, e, index) \
169 intel_vgpu_mm_set_entry(mm, mm->shadow_page_table, e, index)
170
171#define ppgtt_get_guest_root_entry(mm, e, index) \
172 intel_vgpu_mm_get_entry(mm, mm->virtual_page_table, e, index)
173
174#define ppgtt_set_guest_root_entry(mm, e, index) \
175 intel_vgpu_mm_set_entry(mm, mm->virtual_page_table, e, index)
176
177#define ppgtt_get_shadow_root_entry(mm, e, index) \
178 intel_vgpu_mm_get_entry(mm, mm->shadow_page_table, e, index)
179
180#define ppgtt_set_shadow_root_entry(mm, e, index) \
181 intel_vgpu_mm_set_entry(mm, mm->shadow_page_table, e, index)
182
183extern struct intel_vgpu_mm *intel_vgpu_create_mm(struct intel_vgpu *vgpu,
184 int mm_type, void *virtual_page_table, int page_table_level,
185 u32 pde_base_index);
186extern void intel_vgpu_destroy_mm(struct kref *mm_ref);
187
188struct intel_vgpu_guest_page;
189
Ping Gao3b6411c2016-11-04 13:47:35 +0800190struct intel_vgpu_scratch_pt {
191 struct page *page;
192 unsigned long page_mfn;
193};
194
Zhi Wang2707e442016-03-28 23:23:16 +0800195struct intel_vgpu_gtt {
196 struct intel_vgpu_mm *ggtt_mm;
197 unsigned long active_ppgtt_mm_bitmap;
198 struct list_head mm_list_head;
199 DECLARE_HASHTABLE(shadow_page_hash_table, INTEL_GVT_GTT_HASH_BITS);
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800200 DECLARE_HASHTABLE(tracked_guest_page_hash_table, INTEL_GVT_GTT_HASH_BITS);
201 atomic_t n_tracked_guest_page;
Zhi Wang2707e442016-03-28 23:23:16 +0800202 struct list_head oos_page_list_head;
203 struct list_head post_shadow_list_head;
Ping Gao3b6411c2016-11-04 13:47:35 +0800204 struct intel_vgpu_scratch_pt scratch_pt[GTT_TYPE_MAX];
Zhi Wang2707e442016-03-28 23:23:16 +0800205};
206
207extern int intel_vgpu_init_gtt(struct intel_vgpu *vgpu);
208extern void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu);
Ping Gaod650ac02016-12-08 10:14:48 +0800209void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +0800210
211extern int intel_gvt_init_gtt(struct intel_gvt *gvt);
Chuanxiao Dong4d3e67b2017-08-04 13:08:59 +0800212void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +0800213extern void intel_gvt_clean_gtt(struct intel_gvt *gvt);
214
215extern struct intel_vgpu_mm *intel_gvt_find_ppgtt_mm(struct intel_vgpu *vgpu,
216 int page_table_level, void *root_entry);
217
218struct intel_vgpu_oos_page;
219
220struct intel_vgpu_shadow_page {
221 void *vaddr;
222 struct page *page;
223 int type;
224 struct hlist_node node;
225 unsigned long mfn;
226};
227
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800228struct intel_vgpu_page_track {
Zhi Wang2707e442016-03-28 23:23:16 +0800229 struct hlist_node node;
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800230 bool tracked;
Zhi Wang2707e442016-03-28 23:23:16 +0800231 unsigned long gfn;
232 int (*handler)(void *, u64, void *, int);
233 void *data;
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800234};
235
236struct intel_vgpu_guest_page {
237 struct intel_vgpu_page_track track;
Zhi Wang2707e442016-03-28 23:23:16 +0800238 unsigned long write_cnt;
239 struct intel_vgpu_oos_page *oos_page;
240};
241
242struct intel_vgpu_oos_page {
243 struct intel_vgpu_guest_page *guest_page;
244 struct list_head list;
245 struct list_head vm_list;
246 int id;
Zhi Wang9556e112017-10-10 13:51:32 +0800247 unsigned char mem[I915_GTT_PAGE_SIZE];
Zhi Wang2707e442016-03-28 23:23:16 +0800248};
249
250#define GTT_ENTRY_NUM_IN_ONE_PAGE 512
251
252struct intel_vgpu_ppgtt_spt {
253 struct intel_vgpu_shadow_page shadow_page;
254 struct intel_vgpu_guest_page guest_page;
255 int guest_page_type;
256 atomic_t refcount;
257 struct intel_vgpu *vgpu;
258 DECLARE_BITMAP(post_shadow_bitmap, GTT_ENTRY_NUM_IN_ONE_PAGE);
259 struct list_head post_shadow_list;
260};
261
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800262int intel_vgpu_init_page_track(struct intel_vgpu *vgpu,
263 struct intel_vgpu_page_track *t,
Zhi Wang2707e442016-03-28 23:23:16 +0800264 unsigned long gfn,
265 int (*handler)(void *gp, u64, void *, int),
266 void *data);
267
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800268void intel_vgpu_clean_page_track(struct intel_vgpu *vgpu,
269 struct intel_vgpu_page_track *t);
Zhi Wang2707e442016-03-28 23:23:16 +0800270
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800271struct intel_vgpu_page_track *intel_vgpu_find_tracked_page(
Zhi Wang2707e442016-03-28 23:23:16 +0800272 struct intel_vgpu *vgpu, unsigned long gfn);
273
274int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu);
275
276int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu);
277
278static inline void intel_gvt_mm_reference(struct intel_vgpu_mm *mm)
279{
280 kref_get(&mm->ref);
281}
282
283static inline void intel_gvt_mm_unreference(struct intel_vgpu_mm *mm)
284{
285 kref_put(&mm->ref, intel_vgpu_destroy_mm);
286}
287
288int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm);
289
290void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm);
291
292unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm,
293 unsigned long gma);
294
295struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
296 int page_table_level, void *root_entry);
297
298int intel_vgpu_g2v_create_ppgtt_mm(struct intel_vgpu *vgpu,
299 int page_table_level);
300
301int intel_vgpu_g2v_destroy_ppgtt_mm(struct intel_vgpu *vgpu,
302 int page_table_level);
303
304int intel_vgpu_emulate_gtt_mmio_read(struct intel_vgpu *vgpu,
305 unsigned int off, void *p_data, unsigned int bytes);
306
307int intel_vgpu_emulate_gtt_mmio_write(struct intel_vgpu *vgpu,
308 unsigned int off, void *p_data, unsigned int bytes);
309
310#endif /* _GVT_GTT_H_ */