xref: /openbmc/linux/drivers/gpu/drm/i915/gvt/gtt.h (revision 08560328)
1 /*
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 
37 #define I915_GTT_PAGE_SHIFT         12
38 
39 struct intel_vgpu_mm;
40 
41 #define INTEL_GVT_INVALID_ADDR (~0UL)
42 
43 struct intel_gvt_gtt_entry {
44 	u64 val64;
45 	int type;
46 };
47 
48 struct intel_gvt_gtt_pte_ops {
49 	int (*get_entry)(void *pt,
50 			 struct intel_gvt_gtt_entry *e,
51 			 unsigned long index,
52 			 bool hypervisor_access,
53 			 unsigned long gpa,
54 			 struct intel_vgpu *vgpu);
55 	int (*set_entry)(void *pt,
56 			 struct intel_gvt_gtt_entry *e,
57 			 unsigned long index,
58 			 bool hypervisor_access,
59 			 unsigned long gpa,
60 			 struct intel_vgpu *vgpu);
61 	bool (*test_present)(struct intel_gvt_gtt_entry *e);
62 	void (*clear_present)(struct intel_gvt_gtt_entry *e);
63 	void (*set_present)(struct intel_gvt_gtt_entry *e);
64 	bool (*test_pse)(struct intel_gvt_gtt_entry *e);
65 	void (*clear_pse)(struct intel_gvt_gtt_entry *e);
66 	bool (*test_ips)(struct intel_gvt_gtt_entry *e);
67 	void (*clear_ips)(struct intel_gvt_gtt_entry *e);
68 	bool (*test_64k_splited)(struct intel_gvt_gtt_entry *e);
69 	void (*clear_64k_splited)(struct intel_gvt_gtt_entry *e);
70 	void (*set_64k_splited)(struct intel_gvt_gtt_entry *e);
71 	void (*set_pfn)(struct intel_gvt_gtt_entry *e, unsigned long pfn);
72 	unsigned long (*get_pfn)(struct intel_gvt_gtt_entry *e);
73 };
74 
75 struct intel_gvt_gtt_gma_ops {
76 	unsigned long (*gma_to_ggtt_pte_index)(unsigned long gma);
77 	unsigned long (*gma_to_pte_index)(unsigned long gma);
78 	unsigned long (*gma_to_pde_index)(unsigned long gma);
79 	unsigned long (*gma_to_l3_pdp_index)(unsigned long gma);
80 	unsigned long (*gma_to_l4_pdp_index)(unsigned long gma);
81 	unsigned long (*gma_to_pml4_index)(unsigned long gma);
82 };
83 
84 struct intel_gvt_gtt {
85 	struct intel_gvt_gtt_pte_ops *pte_ops;
86 	struct intel_gvt_gtt_gma_ops *gma_ops;
87 	int (*mm_alloc_page_table)(struct intel_vgpu_mm *mm);
88 	void (*mm_free_page_table)(struct intel_vgpu_mm *mm);
89 	struct list_head oos_page_use_list_head;
90 	struct list_head oos_page_free_list_head;
91 	struct list_head ppgtt_mm_lru_list_head;
92 
93 	struct page *scratch_page;
94 	unsigned long scratch_mfn;
95 };
96 
97 typedef 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_64K_ENTRY,
104 	GTT_TYPE_PPGTT_PTE_2M_ENTRY,
105 	GTT_TYPE_PPGTT_PTE_1G_ENTRY,
106 
107 	GTT_TYPE_PPGTT_PTE_ENTRY,
108 
109 	GTT_TYPE_PPGTT_PDE_ENTRY,
110 	GTT_TYPE_PPGTT_PDP_ENTRY,
111 	GTT_TYPE_PPGTT_PML4_ENTRY,
112 
113 	GTT_TYPE_PPGTT_ROOT_ENTRY,
114 
115 	GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
116 	GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
117 
118 	GTT_TYPE_PPGTT_ENTRY,
119 
120 	GTT_TYPE_PPGTT_PTE_PT,
121 	GTT_TYPE_PPGTT_PDE_PT,
122 	GTT_TYPE_PPGTT_PDP_PT,
123 	GTT_TYPE_PPGTT_PML4_PT,
124 
125 	GTT_TYPE_MAX,
126 } intel_gvt_gtt_type_t;
127 
128 enum intel_gvt_mm_type {
129 	INTEL_GVT_MM_GGTT,
130 	INTEL_GVT_MM_PPGTT,
131 };
132 
133 #define GVT_RING_CTX_NR_PDPS	GEN8_3LVL_PDPES
134 
135 struct intel_vgpu_mm {
136 	enum intel_gvt_mm_type type;
137 	struct intel_vgpu *vgpu;
138 
139 	struct kref ref;
140 	atomic_t pincount;
141 
142 	union {
143 		struct {
144 			intel_gvt_gtt_type_t root_entry_type;
145 			/*
146 			 * The 4 PDPs in ring context. For 48bit addressing,
147 			 * only PDP0 is valid and point to PML4. For 32it
148 			 * addressing, all 4 are used as true PDPs.
149 			 */
150 			u64 guest_pdps[GVT_RING_CTX_NR_PDPS];
151 			u64 shadow_pdps[GVT_RING_CTX_NR_PDPS];
152 			bool shadowed;
153 
154 			struct list_head list;
155 			struct list_head lru_list;
156 		} ppgtt_mm;
157 		struct {
158 			void *virtual_ggtt;
159 			unsigned long last_partial_off;
160 			u64 last_partial_data;
161 		} ggtt_mm;
162 	};
163 };
164 
165 struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
166 		intel_gvt_gtt_type_t root_entry_type, u64 pdps[]);
167 
168 static inline void intel_vgpu_mm_get(struct intel_vgpu_mm *mm)
169 {
170 	kref_get(&mm->ref);
171 }
172 
173 void _intel_vgpu_mm_release(struct kref *mm_ref);
174 
175 static inline void intel_vgpu_mm_put(struct intel_vgpu_mm *mm)
176 {
177 	kref_put(&mm->ref, _intel_vgpu_mm_release);
178 }
179 
180 static inline void intel_vgpu_destroy_mm(struct intel_vgpu_mm *mm)
181 {
182 	intel_vgpu_mm_put(mm);
183 }
184 
185 struct intel_vgpu_guest_page;
186 
187 struct intel_vgpu_scratch_pt {
188 	struct page *page;
189 	unsigned long page_mfn;
190 };
191 
192 struct intel_vgpu_gtt {
193 	struct intel_vgpu_mm *ggtt_mm;
194 	unsigned long active_ppgtt_mm_bitmap;
195 	struct list_head ppgtt_mm_list_head;
196 	struct radix_tree_root spt_tree;
197 	struct list_head oos_page_list_head;
198 	struct list_head post_shadow_list_head;
199 	struct intel_vgpu_scratch_pt scratch_pt[GTT_TYPE_MAX];
200 };
201 
202 extern int intel_vgpu_init_gtt(struct intel_vgpu *vgpu);
203 extern void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu);
204 void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old);
205 void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu);
206 
207 extern int intel_gvt_init_gtt(struct intel_gvt *gvt);
208 void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu);
209 extern void intel_gvt_clean_gtt(struct intel_gvt *gvt);
210 
211 extern struct intel_vgpu_mm *intel_gvt_find_ppgtt_mm(struct intel_vgpu *vgpu,
212 		int page_table_level, void *root_entry);
213 
214 struct intel_vgpu_oos_page {
215 	struct intel_vgpu_ppgtt_spt *spt;
216 	struct list_head list;
217 	struct list_head vm_list;
218 	int id;
219 	unsigned char mem[I915_GTT_PAGE_SIZE];
220 };
221 
222 #define GTT_ENTRY_NUM_IN_ONE_PAGE 512
223 
224 /* Represent a vgpu shadow page table. */
225 struct intel_vgpu_ppgtt_spt {
226 	atomic_t refcount;
227 	struct intel_vgpu *vgpu;
228 
229 	struct {
230 		intel_gvt_gtt_type_t type;
231 		bool pde_ips; /* for 64KB PTEs */
232 		void *vaddr;
233 		struct page *page;
234 		unsigned long mfn;
235 	} shadow_page;
236 
237 	struct {
238 		intel_gvt_gtt_type_t type;
239 		bool pde_ips; /* for 64KB PTEs */
240 		unsigned long gfn;
241 		unsigned long write_cnt;
242 		struct intel_vgpu_oos_page *oos_page;
243 	} guest_page;
244 
245 	DECLARE_BITMAP(post_shadow_bitmap, GTT_ENTRY_NUM_IN_ONE_PAGE);
246 	struct list_head post_shadow_list;
247 };
248 
249 int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu);
250 
251 int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu);
252 
253 int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm);
254 
255 void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm);
256 
257 unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm,
258 		unsigned long gma);
259 
260 struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
261 		u64 pdps[]);
262 
263 struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
264 		intel_gvt_gtt_type_t root_entry_type, u64 pdps[]);
265 
266 int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[]);
267 
268 int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
269 	unsigned int off, void *p_data, unsigned int bytes);
270 
271 int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
272 	unsigned int off, void *p_data, unsigned int bytes);
273 
274 #endif /* _GVT_GTT_H_ */
275