• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GTT virtualization
3  *
4  * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Zhi Wang <zhi.a.wang@intel.com>
27  *    Zhenyu Wang <zhenyuw@linux.intel.com>
28  *    Xiao Zheng <xiao.zheng@intel.com>
29  *
30  * Contributors:
31  *    Min He <min.he@intel.com>
32  *    Bing Niu <bing.niu@intel.com>
33  *
34  */
35 
36 #include "i915_drv.h"
37 #include "gvt.h"
38 #include "i915_pvinfo.h"
39 #include "trace.h"
40 
41 #if defined(VERBOSE_DEBUG)
42 #define gvt_vdbg_mm(fmt, args...) gvt_dbg_mm(fmt, ##args)
43 #else
44 #define gvt_vdbg_mm(fmt, args...)
45 #endif
46 
47 static bool enable_out_of_sync = false;
48 static int preallocated_oos_pages = 8192;
49 
50 /*
51  * validate a gm address and related range size,
52  * translate it to host gm address
53  */
intel_gvt_ggtt_validate_range(struct intel_vgpu * vgpu,u64 addr,u32 size)54 bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size)
55 {
56 	if (size == 0)
57 		return vgpu_gmadr_is_valid(vgpu, addr);
58 
59 	if (vgpu_gmadr_is_aperture(vgpu, addr) &&
60 	    vgpu_gmadr_is_aperture(vgpu, addr + size - 1))
61 		return true;
62 	else if (vgpu_gmadr_is_hidden(vgpu, addr) &&
63 		 vgpu_gmadr_is_hidden(vgpu, addr + size - 1))
64 		return true;
65 
66 	gvt_dbg_mm("Invalid ggtt range at 0x%llx, size: 0x%x\n",
67 		     addr, size);
68 	return false;
69 }
70 
71 /* translate a guest gmadr to host gmadr */
intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu * vgpu,u64 g_addr,u64 * h_addr)72 int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr)
73 {
74 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
75 
76 	if (drm_WARN(&i915->drm, !vgpu_gmadr_is_valid(vgpu, g_addr),
77 		     "invalid guest gmadr %llx\n", g_addr))
78 		return -EACCES;
79 
80 	if (vgpu_gmadr_is_aperture(vgpu, g_addr))
81 		*h_addr = vgpu_aperture_gmadr_base(vgpu)
82 			  + (g_addr - vgpu_aperture_offset(vgpu));
83 	else
84 		*h_addr = vgpu_hidden_gmadr_base(vgpu)
85 			  + (g_addr - vgpu_hidden_offset(vgpu));
86 	return 0;
87 }
88 
89 /* translate a host gmadr to guest gmadr */
intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu * vgpu,u64 h_addr,u64 * g_addr)90 int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr)
91 {
92 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
93 
94 	if (drm_WARN(&i915->drm, !gvt_gmadr_is_valid(vgpu->gvt, h_addr),
95 		     "invalid host gmadr %llx\n", h_addr))
96 		return -EACCES;
97 
98 	if (gvt_gmadr_is_aperture(vgpu->gvt, h_addr))
99 		*g_addr = vgpu_aperture_gmadr_base(vgpu)
100 			+ (h_addr - gvt_aperture_gmadr_base(vgpu->gvt));
101 	else
102 		*g_addr = vgpu_hidden_gmadr_base(vgpu)
103 			+ (h_addr - gvt_hidden_gmadr_base(vgpu->gvt));
104 	return 0;
105 }
106 
intel_gvt_ggtt_index_g2h(struct intel_vgpu * vgpu,unsigned long g_index,unsigned long * h_index)107 int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index,
108 			     unsigned long *h_index)
109 {
110 	u64 h_addr;
111 	int ret;
112 
113 	ret = intel_gvt_ggtt_gmadr_g2h(vgpu, g_index << I915_GTT_PAGE_SHIFT,
114 				       &h_addr);
115 	if (ret)
116 		return ret;
117 
118 	*h_index = h_addr >> I915_GTT_PAGE_SHIFT;
119 	return 0;
120 }
121 
intel_gvt_ggtt_h2g_index(struct intel_vgpu * vgpu,unsigned long h_index,unsigned long * g_index)122 int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index,
123 			     unsigned long *g_index)
124 {
125 	u64 g_addr;
126 	int ret;
127 
128 	ret = intel_gvt_ggtt_gmadr_h2g(vgpu, h_index << I915_GTT_PAGE_SHIFT,
129 				       &g_addr);
130 	if (ret)
131 		return ret;
132 
133 	*g_index = g_addr >> I915_GTT_PAGE_SHIFT;
134 	return 0;
135 }
136 
137 #define gtt_type_is_entry(type) \
138 	(type > GTT_TYPE_INVALID && type < GTT_TYPE_PPGTT_ENTRY \
139 	 && type != GTT_TYPE_PPGTT_PTE_ENTRY \
140 	 && type != GTT_TYPE_PPGTT_ROOT_ENTRY)
141 
142 #define gtt_type_is_pt(type) \
143 	(type >= GTT_TYPE_PPGTT_PTE_PT && type < GTT_TYPE_MAX)
144 
145 #define gtt_type_is_pte_pt(type) \
146 	(type == GTT_TYPE_PPGTT_PTE_PT)
147 
148 #define gtt_type_is_root_pointer(type) \
149 	(gtt_type_is_entry(type) && type > GTT_TYPE_PPGTT_ROOT_ENTRY)
150 
151 #define gtt_init_entry(e, t, p, v) do { \
152 	(e)->type = t; \
153 	(e)->pdev = p; \
154 	memcpy(&(e)->val64, &v, sizeof(v)); \
155 } while (0)
156 
157 /*
158  * Mappings between GTT_TYPE* enumerations.
159  * Following information can be found according to the given type:
160  * - type of next level page table
161  * - type of entry inside this level page table
162  * - type of entry with PSE set
163  *
164  * If the given type doesn't have such a kind of information,
165  * e.g. give a l4 root entry type, then request to get its PSE type,
166  * give a PTE page table type, then request to get its next level page
167  * table type, as we know l4 root entry doesn't have a PSE bit,
168  * and a PTE page table doesn't have a next level page table type,
169  * GTT_TYPE_INVALID will be returned. This is useful when traversing a
170  * page table.
171  */
172 
173 struct gtt_type_table_entry {
174 	int entry_type;
175 	int pt_type;
176 	int next_pt_type;
177 	int pse_entry_type;
178 };
179 
180 #define GTT_TYPE_TABLE_ENTRY(type, e_type, cpt_type, npt_type, pse_type) \
181 	[type] = { \
182 		.entry_type = e_type, \
183 		.pt_type = cpt_type, \
184 		.next_pt_type = npt_type, \
185 		.pse_entry_type = pse_type, \
186 	}
187 
188 static struct gtt_type_table_entry gtt_type_table[] = {
189 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
190 			GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
191 			GTT_TYPE_INVALID,
192 			GTT_TYPE_PPGTT_PML4_PT,
193 			GTT_TYPE_INVALID),
194 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_PT,
195 			GTT_TYPE_PPGTT_PML4_ENTRY,
196 			GTT_TYPE_PPGTT_PML4_PT,
197 			GTT_TYPE_PPGTT_PDP_PT,
198 			GTT_TYPE_INVALID),
199 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_ENTRY,
200 			GTT_TYPE_PPGTT_PML4_ENTRY,
201 			GTT_TYPE_PPGTT_PML4_PT,
202 			GTT_TYPE_PPGTT_PDP_PT,
203 			GTT_TYPE_INVALID),
204 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_PT,
205 			GTT_TYPE_PPGTT_PDP_ENTRY,
206 			GTT_TYPE_PPGTT_PDP_PT,
207 			GTT_TYPE_PPGTT_PDE_PT,
208 			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
209 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
210 			GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
211 			GTT_TYPE_INVALID,
212 			GTT_TYPE_PPGTT_PDE_PT,
213 			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
214 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_ENTRY,
215 			GTT_TYPE_PPGTT_PDP_ENTRY,
216 			GTT_TYPE_PPGTT_PDP_PT,
217 			GTT_TYPE_PPGTT_PDE_PT,
218 			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
219 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_PT,
220 			GTT_TYPE_PPGTT_PDE_ENTRY,
221 			GTT_TYPE_PPGTT_PDE_PT,
222 			GTT_TYPE_PPGTT_PTE_PT,
223 			GTT_TYPE_PPGTT_PTE_2M_ENTRY),
224 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_ENTRY,
225 			GTT_TYPE_PPGTT_PDE_ENTRY,
226 			GTT_TYPE_PPGTT_PDE_PT,
227 			GTT_TYPE_PPGTT_PTE_PT,
228 			GTT_TYPE_PPGTT_PTE_2M_ENTRY),
229 	/* We take IPS bit as 'PSE' for PTE level. */
230 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_PT,
231 			GTT_TYPE_PPGTT_PTE_4K_ENTRY,
232 			GTT_TYPE_PPGTT_PTE_PT,
233 			GTT_TYPE_INVALID,
234 			GTT_TYPE_PPGTT_PTE_64K_ENTRY),
235 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_4K_ENTRY,
236 			GTT_TYPE_PPGTT_PTE_4K_ENTRY,
237 			GTT_TYPE_PPGTT_PTE_PT,
238 			GTT_TYPE_INVALID,
239 			GTT_TYPE_PPGTT_PTE_64K_ENTRY),
240 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_64K_ENTRY,
241 			GTT_TYPE_PPGTT_PTE_4K_ENTRY,
242 			GTT_TYPE_PPGTT_PTE_PT,
243 			GTT_TYPE_INVALID,
244 			GTT_TYPE_PPGTT_PTE_64K_ENTRY),
245 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_2M_ENTRY,
246 			GTT_TYPE_PPGTT_PDE_ENTRY,
247 			GTT_TYPE_PPGTT_PDE_PT,
248 			GTT_TYPE_INVALID,
249 			GTT_TYPE_PPGTT_PTE_2M_ENTRY),
250 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_1G_ENTRY,
251 			GTT_TYPE_PPGTT_PDP_ENTRY,
252 			GTT_TYPE_PPGTT_PDP_PT,
253 			GTT_TYPE_INVALID,
254 			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
255 	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_GGTT_PTE,
256 			GTT_TYPE_GGTT_PTE,
257 			GTT_TYPE_INVALID,
258 			GTT_TYPE_INVALID,
259 			GTT_TYPE_INVALID),
260 };
261 
get_next_pt_type(int type)262 static inline int get_next_pt_type(int type)
263 {
264 	return gtt_type_table[type].next_pt_type;
265 }
266 
get_pt_type(int type)267 static inline int get_pt_type(int type)
268 {
269 	return gtt_type_table[type].pt_type;
270 }
271 
get_entry_type(int type)272 static inline int get_entry_type(int type)
273 {
274 	return gtt_type_table[type].entry_type;
275 }
276 
get_pse_type(int type)277 static inline int get_pse_type(int type)
278 {
279 	return gtt_type_table[type].pse_entry_type;
280 }
281 
read_pte64(struct i915_ggtt * ggtt,unsigned long index)282 static u64 read_pte64(struct i915_ggtt *ggtt, unsigned long index)
283 {
284 	void __iomem *addr = (gen8_pte_t __iomem *)ggtt->gsm + index;
285 
286 	return readq(addr);
287 }
288 
ggtt_invalidate(struct intel_gt * gt)289 static void ggtt_invalidate(struct intel_gt *gt)
290 {
291 	mmio_hw_access_pre(gt);
292 	intel_uncore_write(gt->uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
293 	mmio_hw_access_post(gt);
294 }
295 
write_pte64(struct i915_ggtt * ggtt,unsigned long index,u64 pte)296 static void write_pte64(struct i915_ggtt *ggtt, unsigned long index, u64 pte)
297 {
298 	void __iomem *addr = (gen8_pte_t __iomem *)ggtt->gsm + index;
299 
300 	writeq(pte, addr);
301 }
302 
gtt_get_entry64(void * pt,struct intel_gvt_gtt_entry * e,unsigned long index,bool hypervisor_access,unsigned long gpa,struct intel_vgpu * vgpu)303 static inline int gtt_get_entry64(void *pt,
304 		struct intel_gvt_gtt_entry *e,
305 		unsigned long index, bool hypervisor_access, unsigned long gpa,
306 		struct intel_vgpu *vgpu)
307 {
308 	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
309 	int ret;
310 
311 	if (WARN_ON(info->gtt_entry_size != 8))
312 		return -EINVAL;
313 
314 	if (hypervisor_access) {
315 		ret = intel_gvt_hypervisor_read_gpa(vgpu, gpa +
316 				(index << info->gtt_entry_size_shift),
317 				&e->val64, 8);
318 		if (WARN_ON(ret))
319 			return ret;
320 	} else if (!pt) {
321 		e->val64 = read_pte64(vgpu->gvt->gt->ggtt, index);
322 	} else {
323 		e->val64 = *((u64 *)pt + index);
324 	}
325 	return 0;
326 }
327 
gtt_set_entry64(void * pt,struct intel_gvt_gtt_entry * e,unsigned long index,bool hypervisor_access,unsigned long gpa,struct intel_vgpu * vgpu)328 static inline int gtt_set_entry64(void *pt,
329 		struct intel_gvt_gtt_entry *e,
330 		unsigned long index, bool hypervisor_access, unsigned long gpa,
331 		struct intel_vgpu *vgpu)
332 {
333 	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
334 	int ret;
335 
336 	if (WARN_ON(info->gtt_entry_size != 8))
337 		return -EINVAL;
338 
339 	if (hypervisor_access) {
340 		ret = intel_gvt_hypervisor_write_gpa(vgpu, gpa +
341 				(index << info->gtt_entry_size_shift),
342 				&e->val64, 8);
343 		if (WARN_ON(ret))
344 			return ret;
345 	} else if (!pt) {
346 		write_pte64(vgpu->gvt->gt->ggtt, index, e->val64);
347 	} else {
348 		*((u64 *)pt + index) = e->val64;
349 	}
350 	return 0;
351 }
352 
353 #define GTT_HAW 46
354 
355 #define ADDR_1G_MASK	GENMASK_ULL(GTT_HAW - 1, 30)
356 #define ADDR_2M_MASK	GENMASK_ULL(GTT_HAW - 1, 21)
357 #define ADDR_64K_MASK	GENMASK_ULL(GTT_HAW - 1, 16)
358 #define ADDR_4K_MASK	GENMASK_ULL(GTT_HAW - 1, 12)
359 
360 #define GTT_SPTE_FLAG_MASK GENMASK_ULL(62, 52)
361 #define GTT_SPTE_FLAG_64K_SPLITED BIT(52) /* splited 64K gtt entry */
362 
363 #define GTT_64K_PTE_STRIDE 16
364 
gen8_gtt_get_pfn(struct intel_gvt_gtt_entry * e)365 static unsigned long gen8_gtt_get_pfn(struct intel_gvt_gtt_entry *e)
366 {
367 	unsigned long pfn;
368 
369 	if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY)
370 		pfn = (e->val64 & ADDR_1G_MASK) >> PAGE_SHIFT;
371 	else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY)
372 		pfn = (e->val64 & ADDR_2M_MASK) >> PAGE_SHIFT;
373 	else if (e->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY)
374 		pfn = (e->val64 & ADDR_64K_MASK) >> PAGE_SHIFT;
375 	else
376 		pfn = (e->val64 & ADDR_4K_MASK) >> PAGE_SHIFT;
377 	return pfn;
378 }
379 
gen8_gtt_set_pfn(struct intel_gvt_gtt_entry * e,unsigned long pfn)380 static void gen8_gtt_set_pfn(struct intel_gvt_gtt_entry *e, unsigned long pfn)
381 {
382 	if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY) {
383 		e->val64 &= ~ADDR_1G_MASK;
384 		pfn &= (ADDR_1G_MASK >> PAGE_SHIFT);
385 	} else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY) {
386 		e->val64 &= ~ADDR_2M_MASK;
387 		pfn &= (ADDR_2M_MASK >> PAGE_SHIFT);
388 	} else if (e->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY) {
389 		e->val64 &= ~ADDR_64K_MASK;
390 		pfn &= (ADDR_64K_MASK >> PAGE_SHIFT);
391 	} else {
392 		e->val64 &= ~ADDR_4K_MASK;
393 		pfn &= (ADDR_4K_MASK >> PAGE_SHIFT);
394 	}
395 
396 	e->val64 |= (pfn << PAGE_SHIFT);
397 }
398 
gen8_gtt_test_pse(struct intel_gvt_gtt_entry * e)399 static bool gen8_gtt_test_pse(struct intel_gvt_gtt_entry *e)
400 {
401 	return !!(e->val64 & _PAGE_PSE);
402 }
403 
gen8_gtt_clear_pse(struct intel_gvt_gtt_entry * e)404 static void gen8_gtt_clear_pse(struct intel_gvt_gtt_entry *e)
405 {
406 	if (gen8_gtt_test_pse(e)) {
407 		switch (e->type) {
408 		case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
409 			e->val64 &= ~_PAGE_PSE;
410 			e->type = GTT_TYPE_PPGTT_PDE_ENTRY;
411 			break;
412 		case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
413 			e->type = GTT_TYPE_PPGTT_PDP_ENTRY;
414 			e->val64 &= ~_PAGE_PSE;
415 			break;
416 		default:
417 			WARN_ON(1);
418 		}
419 	}
420 }
421 
gen8_gtt_test_ips(struct intel_gvt_gtt_entry * e)422 static bool gen8_gtt_test_ips(struct intel_gvt_gtt_entry *e)
423 {
424 	if (GEM_WARN_ON(e->type != GTT_TYPE_PPGTT_PDE_ENTRY))
425 		return false;
426 
427 	return !!(e->val64 & GEN8_PDE_IPS_64K);
428 }
429 
gen8_gtt_clear_ips(struct intel_gvt_gtt_entry * e)430 static void gen8_gtt_clear_ips(struct intel_gvt_gtt_entry *e)
431 {
432 	if (GEM_WARN_ON(e->type != GTT_TYPE_PPGTT_PDE_ENTRY))
433 		return;
434 
435 	e->val64 &= ~GEN8_PDE_IPS_64K;
436 }
437 
gen8_gtt_test_present(struct intel_gvt_gtt_entry * e)438 static bool gen8_gtt_test_present(struct intel_gvt_gtt_entry *e)
439 {
440 	/*
441 	 * i915 writes PDP root pointer registers without present bit,
442 	 * it also works, so we need to treat root pointer entry
443 	 * specifically.
444 	 */
445 	if (e->type == GTT_TYPE_PPGTT_ROOT_L3_ENTRY
446 			|| e->type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY)
447 		return (e->val64 != 0);
448 	else
449 		return (e->val64 & _PAGE_PRESENT);
450 }
451 
gtt_entry_clear_present(struct intel_gvt_gtt_entry * e)452 static void gtt_entry_clear_present(struct intel_gvt_gtt_entry *e)
453 {
454 	e->val64 &= ~_PAGE_PRESENT;
455 }
456 
gtt_entry_set_present(struct intel_gvt_gtt_entry * e)457 static void gtt_entry_set_present(struct intel_gvt_gtt_entry *e)
458 {
459 	e->val64 |= _PAGE_PRESENT;
460 }
461 
gen8_gtt_test_64k_splited(struct intel_gvt_gtt_entry * e)462 static bool gen8_gtt_test_64k_splited(struct intel_gvt_gtt_entry *e)
463 {
464 	return !!(e->val64 & GTT_SPTE_FLAG_64K_SPLITED);
465 }
466 
gen8_gtt_set_64k_splited(struct intel_gvt_gtt_entry * e)467 static void gen8_gtt_set_64k_splited(struct intel_gvt_gtt_entry *e)
468 {
469 	e->val64 |= GTT_SPTE_FLAG_64K_SPLITED;
470 }
471 
gen8_gtt_clear_64k_splited(struct intel_gvt_gtt_entry * e)472 static void gen8_gtt_clear_64k_splited(struct intel_gvt_gtt_entry *e)
473 {
474 	e->val64 &= ~GTT_SPTE_FLAG_64K_SPLITED;
475 }
476 
477 /*
478  * Per-platform GMA routines.
479  */
gma_to_ggtt_pte_index(unsigned long gma)480 static unsigned long gma_to_ggtt_pte_index(unsigned long gma)
481 {
482 	unsigned long x = (gma >> I915_GTT_PAGE_SHIFT);
483 
484 	trace_gma_index(__func__, gma, x);
485 	return x;
486 }
487 
488 #define DEFINE_PPGTT_GMA_TO_INDEX(prefix, ename, exp) \
489 static unsigned long prefix##_gma_to_##ename##_index(unsigned long gma) \
490 { \
491 	unsigned long x = (exp); \
492 	trace_gma_index(__func__, gma, x); \
493 	return x; \
494 }
495 
496 DEFINE_PPGTT_GMA_TO_INDEX(gen8, pte, (gma >> 12 & 0x1ff));
497 DEFINE_PPGTT_GMA_TO_INDEX(gen8, pde, (gma >> 21 & 0x1ff));
498 DEFINE_PPGTT_GMA_TO_INDEX(gen8, l3_pdp, (gma >> 30 & 0x3));
499 DEFINE_PPGTT_GMA_TO_INDEX(gen8, l4_pdp, (gma >> 30 & 0x1ff));
500 DEFINE_PPGTT_GMA_TO_INDEX(gen8, pml4, (gma >> 39 & 0x1ff));
501 
502 static struct intel_gvt_gtt_pte_ops gen8_gtt_pte_ops = {
503 	.get_entry = gtt_get_entry64,
504 	.set_entry = gtt_set_entry64,
505 	.clear_present = gtt_entry_clear_present,
506 	.set_present = gtt_entry_set_present,
507 	.test_present = gen8_gtt_test_present,
508 	.test_pse = gen8_gtt_test_pse,
509 	.clear_pse = gen8_gtt_clear_pse,
510 	.clear_ips = gen8_gtt_clear_ips,
511 	.test_ips = gen8_gtt_test_ips,
512 	.clear_64k_splited = gen8_gtt_clear_64k_splited,
513 	.set_64k_splited = gen8_gtt_set_64k_splited,
514 	.test_64k_splited = gen8_gtt_test_64k_splited,
515 	.get_pfn = gen8_gtt_get_pfn,
516 	.set_pfn = gen8_gtt_set_pfn,
517 };
518 
519 static struct intel_gvt_gtt_gma_ops gen8_gtt_gma_ops = {
520 	.gma_to_ggtt_pte_index = gma_to_ggtt_pte_index,
521 	.gma_to_pte_index = gen8_gma_to_pte_index,
522 	.gma_to_pde_index = gen8_gma_to_pde_index,
523 	.gma_to_l3_pdp_index = gen8_gma_to_l3_pdp_index,
524 	.gma_to_l4_pdp_index = gen8_gma_to_l4_pdp_index,
525 	.gma_to_pml4_index = gen8_gma_to_pml4_index,
526 };
527 
528 /* Update entry type per pse and ips bit. */
update_entry_type_for_real(struct intel_gvt_gtt_pte_ops * pte_ops,struct intel_gvt_gtt_entry * entry,bool ips)529 static void update_entry_type_for_real(struct intel_gvt_gtt_pte_ops *pte_ops,
530 	struct intel_gvt_gtt_entry *entry, bool ips)
531 {
532 	switch (entry->type) {
533 	case GTT_TYPE_PPGTT_PDE_ENTRY:
534 	case GTT_TYPE_PPGTT_PDP_ENTRY:
535 		if (pte_ops->test_pse(entry))
536 			entry->type = get_pse_type(entry->type);
537 		break;
538 	case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
539 		if (ips)
540 			entry->type = get_pse_type(entry->type);
541 		break;
542 	default:
543 		GEM_BUG_ON(!gtt_type_is_entry(entry->type));
544 	}
545 
546 	GEM_BUG_ON(entry->type == GTT_TYPE_INVALID);
547 }
548 
549 /*
550  * MM helpers.
551  */
_ppgtt_get_root_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index,bool guest)552 static void _ppgtt_get_root_entry(struct intel_vgpu_mm *mm,
553 		struct intel_gvt_gtt_entry *entry, unsigned long index,
554 		bool guest)
555 {
556 	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
557 
558 	GEM_BUG_ON(mm->type != INTEL_GVT_MM_PPGTT);
559 
560 	entry->type = mm->ppgtt_mm.root_entry_type;
561 	pte_ops->get_entry(guest ? mm->ppgtt_mm.guest_pdps :
562 			   mm->ppgtt_mm.shadow_pdps,
563 			   entry, index, false, 0, mm->vgpu);
564 	update_entry_type_for_real(pte_ops, entry, false);
565 }
566 
ppgtt_get_guest_root_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)567 static inline void ppgtt_get_guest_root_entry(struct intel_vgpu_mm *mm,
568 		struct intel_gvt_gtt_entry *entry, unsigned long index)
569 {
570 	_ppgtt_get_root_entry(mm, entry, index, true);
571 }
572 
ppgtt_get_shadow_root_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)573 static inline void ppgtt_get_shadow_root_entry(struct intel_vgpu_mm *mm,
574 		struct intel_gvt_gtt_entry *entry, unsigned long index)
575 {
576 	_ppgtt_get_root_entry(mm, entry, index, false);
577 }
578 
_ppgtt_set_root_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index,bool guest)579 static void _ppgtt_set_root_entry(struct intel_vgpu_mm *mm,
580 		struct intel_gvt_gtt_entry *entry, unsigned long index,
581 		bool guest)
582 {
583 	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
584 
585 	pte_ops->set_entry(guest ? mm->ppgtt_mm.guest_pdps :
586 			   mm->ppgtt_mm.shadow_pdps,
587 			   entry, index, false, 0, mm->vgpu);
588 }
589 
ppgtt_set_guest_root_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)590 static inline void ppgtt_set_guest_root_entry(struct intel_vgpu_mm *mm,
591 		struct intel_gvt_gtt_entry *entry, unsigned long index)
592 {
593 	_ppgtt_set_root_entry(mm, entry, index, true);
594 }
595 
ppgtt_set_shadow_root_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)596 static inline void ppgtt_set_shadow_root_entry(struct intel_vgpu_mm *mm,
597 		struct intel_gvt_gtt_entry *entry, unsigned long index)
598 {
599 	_ppgtt_set_root_entry(mm, entry, index, false);
600 }
601 
ggtt_get_guest_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)602 static void ggtt_get_guest_entry(struct intel_vgpu_mm *mm,
603 		struct intel_gvt_gtt_entry *entry, unsigned long index)
604 {
605 	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
606 
607 	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
608 
609 	entry->type = GTT_TYPE_GGTT_PTE;
610 	pte_ops->get_entry(mm->ggtt_mm.virtual_ggtt, entry, index,
611 			   false, 0, mm->vgpu);
612 }
613 
ggtt_set_guest_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)614 static void ggtt_set_guest_entry(struct intel_vgpu_mm *mm,
615 		struct intel_gvt_gtt_entry *entry, unsigned long index)
616 {
617 	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
618 
619 	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
620 
621 	pte_ops->set_entry(mm->ggtt_mm.virtual_ggtt, entry, index,
622 			   false, 0, mm->vgpu);
623 }
624 
ggtt_get_host_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)625 static void ggtt_get_host_entry(struct intel_vgpu_mm *mm,
626 		struct intel_gvt_gtt_entry *entry, unsigned long index)
627 {
628 	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
629 
630 	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
631 
632 	pte_ops->get_entry(NULL, entry, index, false, 0, mm->vgpu);
633 }
634 
ggtt_set_host_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * entry,unsigned long index)635 static void ggtt_set_host_entry(struct intel_vgpu_mm *mm,
636 		struct intel_gvt_gtt_entry *entry, unsigned long index)
637 {
638 	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
639 
640 	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
641 
642 	pte_ops->set_entry(NULL, entry, index, false, 0, mm->vgpu);
643 }
644 
645 /*
646  * PPGTT shadow page table helpers.
647  */
ppgtt_spt_get_entry(struct intel_vgpu_ppgtt_spt * spt,void * page_table,int type,struct intel_gvt_gtt_entry * e,unsigned long index,bool guest)648 static inline int ppgtt_spt_get_entry(
649 		struct intel_vgpu_ppgtt_spt *spt,
650 		void *page_table, int type,
651 		struct intel_gvt_gtt_entry *e, unsigned long index,
652 		bool guest)
653 {
654 	struct intel_gvt *gvt = spt->vgpu->gvt;
655 	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
656 	int ret;
657 
658 	e->type = get_entry_type(type);
659 
660 	if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n"))
661 		return -EINVAL;
662 
663 	ret = ops->get_entry(page_table, e, index, guest,
664 			spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
665 			spt->vgpu);
666 	if (ret)
667 		return ret;
668 
669 	update_entry_type_for_real(ops, e, guest ?
670 				   spt->guest_page.pde_ips : false);
671 
672 	gvt_vdbg_mm("read ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n",
673 		    type, e->type, index, e->val64);
674 	return 0;
675 }
676 
ppgtt_spt_set_entry(struct intel_vgpu_ppgtt_spt * spt,void * page_table,int type,struct intel_gvt_gtt_entry * e,unsigned long index,bool guest)677 static inline int ppgtt_spt_set_entry(
678 		struct intel_vgpu_ppgtt_spt *spt,
679 		void *page_table, int type,
680 		struct intel_gvt_gtt_entry *e, unsigned long index,
681 		bool guest)
682 {
683 	struct intel_gvt *gvt = spt->vgpu->gvt;
684 	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
685 
686 	if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n"))
687 		return -EINVAL;
688 
689 	gvt_vdbg_mm("set ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n",
690 		    type, e->type, index, e->val64);
691 
692 	return ops->set_entry(page_table, e, index, guest,
693 			spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
694 			spt->vgpu);
695 }
696 
697 #define ppgtt_get_guest_entry(spt, e, index) \
698 	ppgtt_spt_get_entry(spt, NULL, \
699 		spt->guest_page.type, e, index, true)
700 
701 #define ppgtt_set_guest_entry(spt, e, index) \
702 	ppgtt_spt_set_entry(spt, NULL, \
703 		spt->guest_page.type, e, index, true)
704 
705 #define ppgtt_get_shadow_entry(spt, e, index) \
706 	ppgtt_spt_get_entry(spt, spt->shadow_page.vaddr, \
707 		spt->shadow_page.type, e, index, false)
708 
709 #define ppgtt_set_shadow_entry(spt, e, index) \
710 	ppgtt_spt_set_entry(spt, spt->shadow_page.vaddr, \
711 		spt->shadow_page.type, e, index, false)
712 
alloc_spt(gfp_t gfp_mask)713 static void *alloc_spt(gfp_t gfp_mask)
714 {
715 	struct intel_vgpu_ppgtt_spt *spt;
716 
717 	spt = kzalloc(sizeof(*spt), gfp_mask);
718 	if (!spt)
719 		return NULL;
720 
721 	spt->shadow_page.page = alloc_page(gfp_mask);
722 	if (!spt->shadow_page.page) {
723 		kfree(spt);
724 		return NULL;
725 	}
726 	return spt;
727 }
728 
free_spt(struct intel_vgpu_ppgtt_spt * spt)729 static void free_spt(struct intel_vgpu_ppgtt_spt *spt)
730 {
731 	__free_page(spt->shadow_page.page);
732 	kfree(spt);
733 }
734 
735 static int detach_oos_page(struct intel_vgpu *vgpu,
736 		struct intel_vgpu_oos_page *oos_page);
737 
ppgtt_free_spt(struct intel_vgpu_ppgtt_spt * spt)738 static void ppgtt_free_spt(struct intel_vgpu_ppgtt_spt *spt)
739 {
740 	struct device *kdev = &spt->vgpu->gvt->gt->i915->drm.pdev->dev;
741 
742 	trace_spt_free(spt->vgpu->id, spt, spt->guest_page.type);
743 
744 	dma_unmap_page(kdev, spt->shadow_page.mfn << I915_GTT_PAGE_SHIFT, 4096,
745 		       PCI_DMA_BIDIRECTIONAL);
746 
747 	radix_tree_delete(&spt->vgpu->gtt.spt_tree, spt->shadow_page.mfn);
748 
749 	if (spt->guest_page.gfn) {
750 		if (spt->guest_page.oos_page)
751 			detach_oos_page(spt->vgpu, spt->guest_page.oos_page);
752 
753 		intel_vgpu_unregister_page_track(spt->vgpu, spt->guest_page.gfn);
754 	}
755 
756 	list_del_init(&spt->post_shadow_list);
757 	free_spt(spt);
758 }
759 
ppgtt_free_all_spt(struct intel_vgpu * vgpu)760 static void ppgtt_free_all_spt(struct intel_vgpu *vgpu)
761 {
762 	struct intel_vgpu_ppgtt_spt *spt, *spn;
763 	struct radix_tree_iter iter;
764 	LIST_HEAD(all_spt);
765 	void __rcu **slot;
766 
767 	rcu_read_lock();
768 	radix_tree_for_each_slot(slot, &vgpu->gtt.spt_tree, &iter, 0) {
769 		spt = radix_tree_deref_slot(slot);
770 		list_move(&spt->post_shadow_list, &all_spt);
771 	}
772 	rcu_read_unlock();
773 
774 	list_for_each_entry_safe(spt, spn, &all_spt, post_shadow_list)
775 		ppgtt_free_spt(spt);
776 }
777 
778 static int ppgtt_handle_guest_write_page_table_bytes(
779 		struct intel_vgpu_ppgtt_spt *spt,
780 		u64 pa, void *p_data, int bytes);
781 
ppgtt_write_protection_handler(struct intel_vgpu_page_track * page_track,u64 gpa,void * data,int bytes)782 static int ppgtt_write_protection_handler(
783 		struct intel_vgpu_page_track *page_track,
784 		u64 gpa, void *data, int bytes)
785 {
786 	struct intel_vgpu_ppgtt_spt *spt = page_track->priv_data;
787 
788 	int ret;
789 
790 	if (bytes != 4 && bytes != 8)
791 		return -EINVAL;
792 
793 	ret = ppgtt_handle_guest_write_page_table_bytes(spt, gpa, data, bytes);
794 	if (ret)
795 		return ret;
796 	return ret;
797 }
798 
799 /* Find a spt by guest gfn. */
intel_vgpu_find_spt_by_gfn(struct intel_vgpu * vgpu,unsigned long gfn)800 static struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_gfn(
801 		struct intel_vgpu *vgpu, unsigned long gfn)
802 {
803 	struct intel_vgpu_page_track *track;
804 
805 	track = intel_vgpu_find_page_track(vgpu, gfn);
806 	if (track && track->handler == ppgtt_write_protection_handler)
807 		return track->priv_data;
808 
809 	return NULL;
810 }
811 
812 /* Find the spt by shadow page mfn. */
intel_vgpu_find_spt_by_mfn(struct intel_vgpu * vgpu,unsigned long mfn)813 static inline struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_mfn(
814 		struct intel_vgpu *vgpu, unsigned long mfn)
815 {
816 	return radix_tree_lookup(&vgpu->gtt.spt_tree, mfn);
817 }
818 
819 static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt);
820 
821 /* Allocate shadow page table without guest page. */
ppgtt_alloc_spt(struct intel_vgpu * vgpu,enum intel_gvt_gtt_type type)822 static struct intel_vgpu_ppgtt_spt *ppgtt_alloc_spt(
823 		struct intel_vgpu *vgpu, enum intel_gvt_gtt_type type)
824 {
825 	struct device *kdev = &vgpu->gvt->gt->i915->drm.pdev->dev;
826 	struct intel_vgpu_ppgtt_spt *spt = NULL;
827 	dma_addr_t daddr;
828 	int ret;
829 
830 retry:
831 	spt = alloc_spt(GFP_KERNEL | __GFP_ZERO);
832 	if (!spt) {
833 		if (reclaim_one_ppgtt_mm(vgpu->gvt))
834 			goto retry;
835 
836 		gvt_vgpu_err("fail to allocate ppgtt shadow page\n");
837 		return ERR_PTR(-ENOMEM);
838 	}
839 
840 	spt->vgpu = vgpu;
841 	atomic_set(&spt->refcount, 1);
842 	INIT_LIST_HEAD(&spt->post_shadow_list);
843 
844 	/*
845 	 * Init shadow_page.
846 	 */
847 	spt->shadow_page.type = type;
848 	daddr = dma_map_page(kdev, spt->shadow_page.page,
849 			     0, 4096, PCI_DMA_BIDIRECTIONAL);
850 	if (dma_mapping_error(kdev, daddr)) {
851 		gvt_vgpu_err("fail to map dma addr\n");
852 		ret = -EINVAL;
853 		goto err_free_spt;
854 	}
855 	spt->shadow_page.vaddr = page_address(spt->shadow_page.page);
856 	spt->shadow_page.mfn = daddr >> I915_GTT_PAGE_SHIFT;
857 
858 	ret = radix_tree_insert(&vgpu->gtt.spt_tree, spt->shadow_page.mfn, spt);
859 	if (ret)
860 		goto err_unmap_dma;
861 
862 	return spt;
863 
864 err_unmap_dma:
865 	dma_unmap_page(kdev, daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
866 err_free_spt:
867 	free_spt(spt);
868 	return ERR_PTR(ret);
869 }
870 
871 /* Allocate shadow page table associated with specific gfn. */
ppgtt_alloc_spt_gfn(struct intel_vgpu * vgpu,enum intel_gvt_gtt_type type,unsigned long gfn,bool guest_pde_ips)872 static struct intel_vgpu_ppgtt_spt *ppgtt_alloc_spt_gfn(
873 		struct intel_vgpu *vgpu, enum intel_gvt_gtt_type type,
874 		unsigned long gfn, bool guest_pde_ips)
875 {
876 	struct intel_vgpu_ppgtt_spt *spt;
877 	int ret;
878 
879 	spt = ppgtt_alloc_spt(vgpu, type);
880 	if (IS_ERR(spt))
881 		return spt;
882 
883 	/*
884 	 * Init guest_page.
885 	 */
886 	ret = intel_vgpu_register_page_track(vgpu, gfn,
887 			ppgtt_write_protection_handler, spt);
888 	if (ret) {
889 		ppgtt_free_spt(spt);
890 		return ERR_PTR(ret);
891 	}
892 
893 	spt->guest_page.type = type;
894 	spt->guest_page.gfn = gfn;
895 	spt->guest_page.pde_ips = guest_pde_ips;
896 
897 	trace_spt_alloc(vgpu->id, spt, type, spt->shadow_page.mfn, gfn);
898 
899 	return spt;
900 }
901 
902 #define pt_entry_size_shift(spt) \
903 	((spt)->vgpu->gvt->device_info.gtt_entry_size_shift)
904 
905 #define pt_entries(spt) \
906 	(I915_GTT_PAGE_SIZE >> pt_entry_size_shift(spt))
907 
908 #define for_each_present_guest_entry(spt, e, i) \
909 	for (i = 0; i < pt_entries(spt); \
910 	     i += spt->guest_page.pde_ips ? GTT_64K_PTE_STRIDE : 1) \
911 		if (!ppgtt_get_guest_entry(spt, e, i) && \
912 		    spt->vgpu->gvt->gtt.pte_ops->test_present(e))
913 
914 #define for_each_present_shadow_entry(spt, e, i) \
915 	for (i = 0; i < pt_entries(spt); \
916 	     i += spt->shadow_page.pde_ips ? GTT_64K_PTE_STRIDE : 1) \
917 		if (!ppgtt_get_shadow_entry(spt, e, i) && \
918 		    spt->vgpu->gvt->gtt.pte_ops->test_present(e))
919 
920 #define for_each_shadow_entry(spt, e, i) \
921 	for (i = 0; i < pt_entries(spt); \
922 	     i += (spt->shadow_page.pde_ips ? GTT_64K_PTE_STRIDE : 1)) \
923 		if (!ppgtt_get_shadow_entry(spt, e, i))
924 
ppgtt_get_spt(struct intel_vgpu_ppgtt_spt * spt)925 static inline void ppgtt_get_spt(struct intel_vgpu_ppgtt_spt *spt)
926 {
927 	int v = atomic_read(&spt->refcount);
928 
929 	trace_spt_refcount(spt->vgpu->id, "inc", spt, v, (v + 1));
930 	atomic_inc(&spt->refcount);
931 }
932 
ppgtt_put_spt(struct intel_vgpu_ppgtt_spt * spt)933 static inline int ppgtt_put_spt(struct intel_vgpu_ppgtt_spt *spt)
934 {
935 	int v = atomic_read(&spt->refcount);
936 
937 	trace_spt_refcount(spt->vgpu->id, "dec", spt, v, (v - 1));
938 	return atomic_dec_return(&spt->refcount);
939 }
940 
941 static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt);
942 
ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu * vgpu,struct intel_gvt_gtt_entry * e)943 static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu,
944 		struct intel_gvt_gtt_entry *e)
945 {
946 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
947 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
948 	struct intel_vgpu_ppgtt_spt *s;
949 	enum intel_gvt_gtt_type cur_pt_type;
950 
951 	GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(e->type)));
952 
953 	if (e->type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY
954 		&& e->type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY) {
955 		cur_pt_type = get_next_pt_type(e->type);
956 
957 		if (!gtt_type_is_pt(cur_pt_type) ||
958 				!gtt_type_is_pt(cur_pt_type + 1)) {
959 			drm_WARN(&i915->drm, 1,
960 				 "Invalid page table type, cur_pt_type is: %d\n",
961 				 cur_pt_type);
962 			return -EINVAL;
963 		}
964 
965 		cur_pt_type += 1;
966 
967 		if (ops->get_pfn(e) ==
968 			vgpu->gtt.scratch_pt[cur_pt_type].page_mfn)
969 			return 0;
970 	}
971 	s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e));
972 	if (!s) {
973 		gvt_vgpu_err("fail to find shadow page: mfn: 0x%lx\n",
974 				ops->get_pfn(e));
975 		return -ENXIO;
976 	}
977 	return ppgtt_invalidate_spt(s);
978 }
979 
ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt * spt,struct intel_gvt_gtt_entry * entry)980 static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt,
981 		struct intel_gvt_gtt_entry *entry)
982 {
983 	struct intel_vgpu *vgpu = spt->vgpu;
984 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
985 	unsigned long pfn;
986 	int type;
987 
988 	pfn = ops->get_pfn(entry);
989 	type = spt->shadow_page.type;
990 
991 	/* Uninitialized spte or unshadowed spte. */
992 	if (!pfn || pfn == vgpu->gtt.scratch_pt[type].page_mfn)
993 		return;
994 
995 	intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT);
996 }
997 
ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt * spt)998 static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt)
999 {
1000 	struct intel_vgpu *vgpu = spt->vgpu;
1001 	struct intel_gvt_gtt_entry e;
1002 	unsigned long index;
1003 	int ret;
1004 
1005 	trace_spt_change(spt->vgpu->id, "die", spt,
1006 			spt->guest_page.gfn, spt->shadow_page.type);
1007 
1008 	if (ppgtt_put_spt(spt) > 0)
1009 		return 0;
1010 
1011 	for_each_present_shadow_entry(spt, &e, index) {
1012 		switch (e.type) {
1013 		case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
1014 			gvt_vdbg_mm("invalidate 4K entry\n");
1015 			ppgtt_invalidate_pte(spt, &e);
1016 			break;
1017 		case GTT_TYPE_PPGTT_PTE_64K_ENTRY:
1018 			/* We don't setup 64K shadow entry so far. */
1019 			WARN(1, "suspicious 64K gtt entry\n");
1020 			continue;
1021 		case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
1022 			gvt_vdbg_mm("invalidate 2M entry\n");
1023 			continue;
1024 		case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
1025 			WARN(1, "GVT doesn't support 1GB page\n");
1026 			continue;
1027 		case GTT_TYPE_PPGTT_PML4_ENTRY:
1028 		case GTT_TYPE_PPGTT_PDP_ENTRY:
1029 		case GTT_TYPE_PPGTT_PDE_ENTRY:
1030 			gvt_vdbg_mm("invalidate PMUL4/PDP/PDE entry\n");
1031 			ret = ppgtt_invalidate_spt_by_shadow_entry(
1032 					spt->vgpu, &e);
1033 			if (ret)
1034 				goto fail;
1035 			break;
1036 		default:
1037 			GEM_BUG_ON(1);
1038 		}
1039 	}
1040 
1041 	trace_spt_change(spt->vgpu->id, "release", spt,
1042 			 spt->guest_page.gfn, spt->shadow_page.type);
1043 	ppgtt_free_spt(spt);
1044 	return 0;
1045 fail:
1046 	gvt_vgpu_err("fail: shadow page %p shadow entry 0x%llx type %d\n",
1047 			spt, e.val64, e.type);
1048 	return ret;
1049 }
1050 
vgpu_ips_enabled(struct intel_vgpu * vgpu)1051 static bool vgpu_ips_enabled(struct intel_vgpu *vgpu)
1052 {
1053 	struct drm_i915_private *dev_priv = vgpu->gvt->gt->i915;
1054 
1055 	if (INTEL_GEN(dev_priv) == 9 || INTEL_GEN(dev_priv) == 10) {
1056 		u32 ips = vgpu_vreg_t(vgpu, GEN8_GAMW_ECO_DEV_RW_IA) &
1057 			GAMW_ECO_ENABLE_64K_IPS_FIELD;
1058 
1059 		return ips == GAMW_ECO_ENABLE_64K_IPS_FIELD;
1060 	} else if (INTEL_GEN(dev_priv) >= 11) {
1061 		/* 64K paging only controlled by IPS bit in PTE now. */
1062 		return true;
1063 	} else
1064 		return false;
1065 }
1066 
1067 static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt);
1068 
ppgtt_populate_spt_by_guest_entry(struct intel_vgpu * vgpu,struct intel_gvt_gtt_entry * we)1069 static struct intel_vgpu_ppgtt_spt *ppgtt_populate_spt_by_guest_entry(
1070 		struct intel_vgpu *vgpu, struct intel_gvt_gtt_entry *we)
1071 {
1072 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1073 	struct intel_vgpu_ppgtt_spt *spt = NULL;
1074 	bool ips = false;
1075 	int ret;
1076 
1077 	GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(we->type)));
1078 
1079 	if (we->type == GTT_TYPE_PPGTT_PDE_ENTRY)
1080 		ips = vgpu_ips_enabled(vgpu) && ops->test_ips(we);
1081 
1082 	spt = intel_vgpu_find_spt_by_gfn(vgpu, ops->get_pfn(we));
1083 	if (spt) {
1084 		ppgtt_get_spt(spt);
1085 
1086 		if (ips != spt->guest_page.pde_ips) {
1087 			spt->guest_page.pde_ips = ips;
1088 
1089 			gvt_dbg_mm("reshadow PDE since ips changed\n");
1090 			clear_page(spt->shadow_page.vaddr);
1091 			ret = ppgtt_populate_spt(spt);
1092 			if (ret) {
1093 				ppgtt_put_spt(spt);
1094 				goto err;
1095 			}
1096 		}
1097 	} else {
1098 		int type = get_next_pt_type(we->type);
1099 
1100 		if (!gtt_type_is_pt(type)) {
1101 			ret = -EINVAL;
1102 			goto err;
1103 		}
1104 
1105 		spt = ppgtt_alloc_spt_gfn(vgpu, type, ops->get_pfn(we), ips);
1106 		if (IS_ERR(spt)) {
1107 			ret = PTR_ERR(spt);
1108 			goto err;
1109 		}
1110 
1111 		ret = intel_vgpu_enable_page_track(vgpu, spt->guest_page.gfn);
1112 		if (ret)
1113 			goto err_free_spt;
1114 
1115 		ret = ppgtt_populate_spt(spt);
1116 		if (ret)
1117 			goto err_free_spt;
1118 
1119 		trace_spt_change(vgpu->id, "new", spt, spt->guest_page.gfn,
1120 				 spt->shadow_page.type);
1121 	}
1122 	return spt;
1123 
1124 err_free_spt:
1125 	ppgtt_free_spt(spt);
1126 	spt = NULL;
1127 err:
1128 	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
1129 		     spt, we->val64, we->type);
1130 	return ERR_PTR(ret);
1131 }
1132 
ppgtt_generate_shadow_entry(struct intel_gvt_gtt_entry * se,struct intel_vgpu_ppgtt_spt * s,struct intel_gvt_gtt_entry * ge)1133 static inline void ppgtt_generate_shadow_entry(struct intel_gvt_gtt_entry *se,
1134 		struct intel_vgpu_ppgtt_spt *s, struct intel_gvt_gtt_entry *ge)
1135 {
1136 	struct intel_gvt_gtt_pte_ops *ops = s->vgpu->gvt->gtt.pte_ops;
1137 
1138 	se->type = ge->type;
1139 	se->val64 = ge->val64;
1140 
1141 	/* Because we always split 64KB pages, so clear IPS in shadow PDE. */
1142 	if (se->type == GTT_TYPE_PPGTT_PDE_ENTRY)
1143 		ops->clear_ips(se);
1144 
1145 	ops->set_pfn(se, s->shadow_page.mfn);
1146 }
1147 
1148 /**
1149  * Check if can do 2M page
1150  * @vgpu: target vgpu
1151  * @entry: target pfn's gtt entry
1152  *
1153  * Return 1 if 2MB huge gtt shadowing is possilbe, 0 if miscondition,
1154  * negtive if found err.
1155  */
is_2MB_gtt_possible(struct intel_vgpu * vgpu,struct intel_gvt_gtt_entry * entry)1156 static int is_2MB_gtt_possible(struct intel_vgpu *vgpu,
1157 	struct intel_gvt_gtt_entry *entry)
1158 {
1159 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1160 	unsigned long pfn;
1161 
1162 	if (!HAS_PAGE_SIZES(vgpu->gvt->gt->i915, I915_GTT_PAGE_SIZE_2M))
1163 		return 0;
1164 
1165 	pfn = intel_gvt_hypervisor_gfn_to_mfn(vgpu, ops->get_pfn(entry));
1166 	if (pfn == INTEL_GVT_INVALID_ADDR)
1167 		return -EINVAL;
1168 
1169 	return PageTransHuge(pfn_to_page(pfn));
1170 }
1171 
split_2MB_gtt_entry(struct intel_vgpu * vgpu,struct intel_vgpu_ppgtt_spt * spt,unsigned long index,struct intel_gvt_gtt_entry * se)1172 static int split_2MB_gtt_entry(struct intel_vgpu *vgpu,
1173 	struct intel_vgpu_ppgtt_spt *spt, unsigned long index,
1174 	struct intel_gvt_gtt_entry *se)
1175 {
1176 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1177 	struct intel_vgpu_ppgtt_spt *sub_spt;
1178 	struct intel_gvt_gtt_entry sub_se;
1179 	unsigned long start_gfn;
1180 	dma_addr_t dma_addr;
1181 	unsigned long sub_index;
1182 	int ret;
1183 
1184 	gvt_dbg_mm("Split 2M gtt entry, index %lu\n", index);
1185 
1186 	start_gfn = ops->get_pfn(se);
1187 
1188 	sub_spt = ppgtt_alloc_spt(vgpu, GTT_TYPE_PPGTT_PTE_PT);
1189 	if (IS_ERR(sub_spt))
1190 		return PTR_ERR(sub_spt);
1191 
1192 	for_each_shadow_entry(sub_spt, &sub_se, sub_index) {
1193 		ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu,
1194 				start_gfn + sub_index, PAGE_SIZE, &dma_addr);
1195 		if (ret)
1196 			goto err;
1197 		sub_se.val64 = se->val64;
1198 
1199 		/* Copy the PAT field from PDE. */
1200 		sub_se.val64 &= ~_PAGE_PAT;
1201 		sub_se.val64 |= (se->val64 & _PAGE_PAT_LARGE) >> 5;
1202 
1203 		ops->set_pfn(&sub_se, dma_addr >> PAGE_SHIFT);
1204 		ppgtt_set_shadow_entry(sub_spt, &sub_se, sub_index);
1205 	}
1206 
1207 	/* Clear dirty field. */
1208 	se->val64 &= ~_PAGE_DIRTY;
1209 
1210 	ops->clear_pse(se);
1211 	ops->clear_ips(se);
1212 	ops->set_pfn(se, sub_spt->shadow_page.mfn);
1213 	ppgtt_set_shadow_entry(spt, se, index);
1214 	return 0;
1215 err:
1216 	/* Cancel the existing addess mappings of DMA addr. */
1217 	for_each_present_shadow_entry(sub_spt, &sub_se, sub_index) {
1218 		gvt_vdbg_mm("invalidate 4K entry\n");
1219 		ppgtt_invalidate_pte(sub_spt, &sub_se);
1220 	}
1221 	/* Release the new allocated spt. */
1222 	trace_spt_change(sub_spt->vgpu->id, "release", sub_spt,
1223 		sub_spt->guest_page.gfn, sub_spt->shadow_page.type);
1224 	ppgtt_free_spt(sub_spt);
1225 	return ret;
1226 }
1227 
split_64KB_gtt_entry(struct intel_vgpu * vgpu,struct intel_vgpu_ppgtt_spt * spt,unsigned long index,struct intel_gvt_gtt_entry * se)1228 static int split_64KB_gtt_entry(struct intel_vgpu *vgpu,
1229 	struct intel_vgpu_ppgtt_spt *spt, unsigned long index,
1230 	struct intel_gvt_gtt_entry *se)
1231 {
1232 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1233 	struct intel_gvt_gtt_entry entry = *se;
1234 	unsigned long start_gfn;
1235 	dma_addr_t dma_addr;
1236 	int i, ret;
1237 
1238 	gvt_vdbg_mm("Split 64K gtt entry, index %lu\n", index);
1239 
1240 	GEM_BUG_ON(index % GTT_64K_PTE_STRIDE);
1241 
1242 	start_gfn = ops->get_pfn(se);
1243 
1244 	entry.type = GTT_TYPE_PPGTT_PTE_4K_ENTRY;
1245 	ops->set_64k_splited(&entry);
1246 
1247 	for (i = 0; i < GTT_64K_PTE_STRIDE; i++) {
1248 		ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu,
1249 					start_gfn + i, PAGE_SIZE, &dma_addr);
1250 		if (ret)
1251 			return ret;
1252 
1253 		ops->set_pfn(&entry, dma_addr >> PAGE_SHIFT);
1254 		ppgtt_set_shadow_entry(spt, &entry, index + i);
1255 	}
1256 	return 0;
1257 }
1258 
ppgtt_populate_shadow_entry(struct intel_vgpu * vgpu,struct intel_vgpu_ppgtt_spt * spt,unsigned long index,struct intel_gvt_gtt_entry * ge)1259 static int ppgtt_populate_shadow_entry(struct intel_vgpu *vgpu,
1260 	struct intel_vgpu_ppgtt_spt *spt, unsigned long index,
1261 	struct intel_gvt_gtt_entry *ge)
1262 {
1263 	struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
1264 	struct intel_gvt_gtt_entry se = *ge;
1265 	unsigned long gfn, page_size = PAGE_SIZE;
1266 	dma_addr_t dma_addr;
1267 	int ret;
1268 
1269 	if (!pte_ops->test_present(ge))
1270 		return 0;
1271 
1272 	gfn = pte_ops->get_pfn(ge);
1273 
1274 	switch (ge->type) {
1275 	case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
1276 		gvt_vdbg_mm("shadow 4K gtt entry\n");
1277 		break;
1278 	case GTT_TYPE_PPGTT_PTE_64K_ENTRY:
1279 		gvt_vdbg_mm("shadow 64K gtt entry\n");
1280 		/*
1281 		 * The layout of 64K page is special, the page size is
1282 		 * controlled by uper PDE. To be simple, we always split
1283 		 * 64K page to smaller 4K pages in shadow PT.
1284 		 */
1285 		return split_64KB_gtt_entry(vgpu, spt, index, &se);
1286 	case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
1287 		gvt_vdbg_mm("shadow 2M gtt entry\n");
1288 		ret = is_2MB_gtt_possible(vgpu, ge);
1289 		if (ret == 0)
1290 			return split_2MB_gtt_entry(vgpu, spt, index, &se);
1291 		else if (ret < 0)
1292 			return ret;
1293 		page_size = I915_GTT_PAGE_SIZE_2M;
1294 		break;
1295 	case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
1296 		gvt_vgpu_err("GVT doesn't support 1GB entry\n");
1297 		return -EINVAL;
1298 	default:
1299 		GEM_BUG_ON(1);
1300 	}
1301 
1302 	/* direct shadow */
1303 	ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn, page_size,
1304 						      &dma_addr);
1305 	if (ret)
1306 		return -ENXIO;
1307 
1308 	pte_ops->set_pfn(&se, dma_addr >> PAGE_SHIFT);
1309 	ppgtt_set_shadow_entry(spt, &se, index);
1310 	return 0;
1311 }
1312 
ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt * spt)1313 static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt)
1314 {
1315 	struct intel_vgpu *vgpu = spt->vgpu;
1316 	struct intel_gvt *gvt = vgpu->gvt;
1317 	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
1318 	struct intel_vgpu_ppgtt_spt *s;
1319 	struct intel_gvt_gtt_entry se, ge;
1320 	unsigned long gfn, i;
1321 	int ret;
1322 
1323 	trace_spt_change(spt->vgpu->id, "born", spt,
1324 			 spt->guest_page.gfn, spt->shadow_page.type);
1325 
1326 	for_each_present_guest_entry(spt, &ge, i) {
1327 		if (gtt_type_is_pt(get_next_pt_type(ge.type))) {
1328 			s = ppgtt_populate_spt_by_guest_entry(vgpu, &ge);
1329 			if (IS_ERR(s)) {
1330 				ret = PTR_ERR(s);
1331 				goto fail;
1332 			}
1333 			ppgtt_get_shadow_entry(spt, &se, i);
1334 			ppgtt_generate_shadow_entry(&se, s, &ge);
1335 			ppgtt_set_shadow_entry(spt, &se, i);
1336 		} else {
1337 			gfn = ops->get_pfn(&ge);
1338 			if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
1339 				ops->set_pfn(&se, gvt->gtt.scratch_mfn);
1340 				ppgtt_set_shadow_entry(spt, &se, i);
1341 				continue;
1342 			}
1343 
1344 			ret = ppgtt_populate_shadow_entry(vgpu, spt, i, &ge);
1345 			if (ret)
1346 				goto fail;
1347 		}
1348 	}
1349 	return 0;
1350 fail:
1351 	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
1352 			spt, ge.val64, ge.type);
1353 	return ret;
1354 }
1355 
ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt * spt,struct intel_gvt_gtt_entry * se,unsigned long index)1356 static int ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt *spt,
1357 		struct intel_gvt_gtt_entry *se, unsigned long index)
1358 {
1359 	struct intel_vgpu *vgpu = spt->vgpu;
1360 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1361 	int ret;
1362 
1363 	trace_spt_guest_change(spt->vgpu->id, "remove", spt,
1364 			       spt->shadow_page.type, se->val64, index);
1365 
1366 	gvt_vdbg_mm("destroy old shadow entry, type %d, index %lu, value %llx\n",
1367 		    se->type, index, se->val64);
1368 
1369 	if (!ops->test_present(se))
1370 		return 0;
1371 
1372 	if (ops->get_pfn(se) ==
1373 	    vgpu->gtt.scratch_pt[spt->shadow_page.type].page_mfn)
1374 		return 0;
1375 
1376 	if (gtt_type_is_pt(get_next_pt_type(se->type))) {
1377 		struct intel_vgpu_ppgtt_spt *s =
1378 			intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(se));
1379 		if (!s) {
1380 			gvt_vgpu_err("fail to find guest page\n");
1381 			ret = -ENXIO;
1382 			goto fail;
1383 		}
1384 		ret = ppgtt_invalidate_spt(s);
1385 		if (ret)
1386 			goto fail;
1387 	} else {
1388 		/* We don't setup 64K shadow entry so far. */
1389 		WARN(se->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY,
1390 		     "suspicious 64K entry\n");
1391 		ppgtt_invalidate_pte(spt, se);
1392 	}
1393 
1394 	return 0;
1395 fail:
1396 	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
1397 			spt, se->val64, se->type);
1398 	return ret;
1399 }
1400 
ppgtt_handle_guest_entry_add(struct intel_vgpu_ppgtt_spt * spt,struct intel_gvt_gtt_entry * we,unsigned long index)1401 static int ppgtt_handle_guest_entry_add(struct intel_vgpu_ppgtt_spt *spt,
1402 		struct intel_gvt_gtt_entry *we, unsigned long index)
1403 {
1404 	struct intel_vgpu *vgpu = spt->vgpu;
1405 	struct intel_gvt_gtt_entry m;
1406 	struct intel_vgpu_ppgtt_spt *s;
1407 	int ret;
1408 
1409 	trace_spt_guest_change(spt->vgpu->id, "add", spt, spt->shadow_page.type,
1410 			       we->val64, index);
1411 
1412 	gvt_vdbg_mm("add shadow entry: type %d, index %lu, value %llx\n",
1413 		    we->type, index, we->val64);
1414 
1415 	if (gtt_type_is_pt(get_next_pt_type(we->type))) {
1416 		s = ppgtt_populate_spt_by_guest_entry(vgpu, we);
1417 		if (IS_ERR(s)) {
1418 			ret = PTR_ERR(s);
1419 			goto fail;
1420 		}
1421 		ppgtt_get_shadow_entry(spt, &m, index);
1422 		ppgtt_generate_shadow_entry(&m, s, we);
1423 		ppgtt_set_shadow_entry(spt, &m, index);
1424 	} else {
1425 		ret = ppgtt_populate_shadow_entry(vgpu, spt, index, we);
1426 		if (ret)
1427 			goto fail;
1428 	}
1429 	return 0;
1430 fail:
1431 	gvt_vgpu_err("fail: spt %p guest entry 0x%llx type %d\n",
1432 		spt, we->val64, we->type);
1433 	return ret;
1434 }
1435 
sync_oos_page(struct intel_vgpu * vgpu,struct intel_vgpu_oos_page * oos_page)1436 static int sync_oos_page(struct intel_vgpu *vgpu,
1437 		struct intel_vgpu_oos_page *oos_page)
1438 {
1439 	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
1440 	struct intel_gvt *gvt = vgpu->gvt;
1441 	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
1442 	struct intel_vgpu_ppgtt_spt *spt = oos_page->spt;
1443 	struct intel_gvt_gtt_entry old, new;
1444 	int index;
1445 	int ret;
1446 
1447 	trace_oos_change(vgpu->id, "sync", oos_page->id,
1448 			 spt, spt->guest_page.type);
1449 
1450 	old.type = new.type = get_entry_type(spt->guest_page.type);
1451 	old.val64 = new.val64 = 0;
1452 
1453 	for (index = 0; index < (I915_GTT_PAGE_SIZE >>
1454 				info->gtt_entry_size_shift); index++) {
1455 		ops->get_entry(oos_page->mem, &old, index, false, 0, vgpu);
1456 		ops->get_entry(NULL, &new, index, true,
1457 			       spt->guest_page.gfn << PAGE_SHIFT, vgpu);
1458 
1459 		if (old.val64 == new.val64
1460 			&& !test_and_clear_bit(index, spt->post_shadow_bitmap))
1461 			continue;
1462 
1463 		trace_oos_sync(vgpu->id, oos_page->id,
1464 				spt, spt->guest_page.type,
1465 				new.val64, index);
1466 
1467 		ret = ppgtt_populate_shadow_entry(vgpu, spt, index, &new);
1468 		if (ret)
1469 			return ret;
1470 
1471 		ops->set_entry(oos_page->mem, &new, index, false, 0, vgpu);
1472 	}
1473 
1474 	spt->guest_page.write_cnt = 0;
1475 	list_del_init(&spt->post_shadow_list);
1476 	return 0;
1477 }
1478 
detach_oos_page(struct intel_vgpu * vgpu,struct intel_vgpu_oos_page * oos_page)1479 static int detach_oos_page(struct intel_vgpu *vgpu,
1480 		struct intel_vgpu_oos_page *oos_page)
1481 {
1482 	struct intel_gvt *gvt = vgpu->gvt;
1483 	struct intel_vgpu_ppgtt_spt *spt = oos_page->spt;
1484 
1485 	trace_oos_change(vgpu->id, "detach", oos_page->id,
1486 			 spt, spt->guest_page.type);
1487 
1488 	spt->guest_page.write_cnt = 0;
1489 	spt->guest_page.oos_page = NULL;
1490 	oos_page->spt = NULL;
1491 
1492 	list_del_init(&oos_page->vm_list);
1493 	list_move_tail(&oos_page->list, &gvt->gtt.oos_page_free_list_head);
1494 
1495 	return 0;
1496 }
1497 
attach_oos_page(struct intel_vgpu_oos_page * oos_page,struct intel_vgpu_ppgtt_spt * spt)1498 static int attach_oos_page(struct intel_vgpu_oos_page *oos_page,
1499 		struct intel_vgpu_ppgtt_spt *spt)
1500 {
1501 	struct intel_gvt *gvt = spt->vgpu->gvt;
1502 	int ret;
1503 
1504 	ret = intel_gvt_hypervisor_read_gpa(spt->vgpu,
1505 			spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
1506 			oos_page->mem, I915_GTT_PAGE_SIZE);
1507 	if (ret)
1508 		return ret;
1509 
1510 	oos_page->spt = spt;
1511 	spt->guest_page.oos_page = oos_page;
1512 
1513 	list_move_tail(&oos_page->list, &gvt->gtt.oos_page_use_list_head);
1514 
1515 	trace_oos_change(spt->vgpu->id, "attach", oos_page->id,
1516 			 spt, spt->guest_page.type);
1517 	return 0;
1518 }
1519 
ppgtt_set_guest_page_sync(struct intel_vgpu_ppgtt_spt * spt)1520 static int ppgtt_set_guest_page_sync(struct intel_vgpu_ppgtt_spt *spt)
1521 {
1522 	struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
1523 	int ret;
1524 
1525 	ret = intel_vgpu_enable_page_track(spt->vgpu, spt->guest_page.gfn);
1526 	if (ret)
1527 		return ret;
1528 
1529 	trace_oos_change(spt->vgpu->id, "set page sync", oos_page->id,
1530 			 spt, spt->guest_page.type);
1531 
1532 	list_del_init(&oos_page->vm_list);
1533 	return sync_oos_page(spt->vgpu, oos_page);
1534 }
1535 
ppgtt_allocate_oos_page(struct intel_vgpu_ppgtt_spt * spt)1536 static int ppgtt_allocate_oos_page(struct intel_vgpu_ppgtt_spt *spt)
1537 {
1538 	struct intel_gvt *gvt = spt->vgpu->gvt;
1539 	struct intel_gvt_gtt *gtt = &gvt->gtt;
1540 	struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
1541 	int ret;
1542 
1543 	WARN(oos_page, "shadow PPGTT page has already has a oos page\n");
1544 
1545 	if (list_empty(&gtt->oos_page_free_list_head)) {
1546 		oos_page = container_of(gtt->oos_page_use_list_head.next,
1547 			struct intel_vgpu_oos_page, list);
1548 		ret = ppgtt_set_guest_page_sync(oos_page->spt);
1549 		if (ret)
1550 			return ret;
1551 		ret = detach_oos_page(spt->vgpu, oos_page);
1552 		if (ret)
1553 			return ret;
1554 	} else
1555 		oos_page = container_of(gtt->oos_page_free_list_head.next,
1556 			struct intel_vgpu_oos_page, list);
1557 	return attach_oos_page(oos_page, spt);
1558 }
1559 
ppgtt_set_guest_page_oos(struct intel_vgpu_ppgtt_spt * spt)1560 static int ppgtt_set_guest_page_oos(struct intel_vgpu_ppgtt_spt *spt)
1561 {
1562 	struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
1563 
1564 	if (WARN(!oos_page, "shadow PPGTT page should have a oos page\n"))
1565 		return -EINVAL;
1566 
1567 	trace_oos_change(spt->vgpu->id, "set page out of sync", oos_page->id,
1568 			 spt, spt->guest_page.type);
1569 
1570 	list_add_tail(&oos_page->vm_list, &spt->vgpu->gtt.oos_page_list_head);
1571 	return intel_vgpu_disable_page_track(spt->vgpu, spt->guest_page.gfn);
1572 }
1573 
1574 /**
1575  * intel_vgpu_sync_oos_pages - sync all the out-of-synced shadow for vGPU
1576  * @vgpu: a vGPU
1577  *
1578  * This function is called before submitting a guest workload to host,
1579  * to sync all the out-of-synced shadow for vGPU
1580  *
1581  * Returns:
1582  * Zero on success, negative error code if failed.
1583  */
intel_vgpu_sync_oos_pages(struct intel_vgpu * vgpu)1584 int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu)
1585 {
1586 	struct list_head *pos, *n;
1587 	struct intel_vgpu_oos_page *oos_page;
1588 	int ret;
1589 
1590 	if (!enable_out_of_sync)
1591 		return 0;
1592 
1593 	list_for_each_safe(pos, n, &vgpu->gtt.oos_page_list_head) {
1594 		oos_page = container_of(pos,
1595 				struct intel_vgpu_oos_page, vm_list);
1596 		ret = ppgtt_set_guest_page_sync(oos_page->spt);
1597 		if (ret)
1598 			return ret;
1599 	}
1600 	return 0;
1601 }
1602 
1603 /*
1604  * The heart of PPGTT shadow page table.
1605  */
ppgtt_handle_guest_write_page_table(struct intel_vgpu_ppgtt_spt * spt,struct intel_gvt_gtt_entry * we,unsigned long index)1606 static int ppgtt_handle_guest_write_page_table(
1607 		struct intel_vgpu_ppgtt_spt *spt,
1608 		struct intel_gvt_gtt_entry *we, unsigned long index)
1609 {
1610 	struct intel_vgpu *vgpu = spt->vgpu;
1611 	int type = spt->shadow_page.type;
1612 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1613 	struct intel_gvt_gtt_entry old_se;
1614 	int new_present;
1615 	int i, ret;
1616 
1617 	new_present = ops->test_present(we);
1618 
1619 	/*
1620 	 * Adding the new entry first and then removing the old one, that can
1621 	 * guarantee the ppgtt table is validated during the window between
1622 	 * adding and removal.
1623 	 */
1624 	ppgtt_get_shadow_entry(spt, &old_se, index);
1625 
1626 	if (new_present) {
1627 		ret = ppgtt_handle_guest_entry_add(spt, we, index);
1628 		if (ret)
1629 			goto fail;
1630 	}
1631 
1632 	ret = ppgtt_handle_guest_entry_removal(spt, &old_se, index);
1633 	if (ret)
1634 		goto fail;
1635 
1636 	if (!new_present) {
1637 		/* For 64KB splited entries, we need clear them all. */
1638 		if (ops->test_64k_splited(&old_se) &&
1639 		    !(index % GTT_64K_PTE_STRIDE)) {
1640 			gvt_vdbg_mm("remove splited 64K shadow entries\n");
1641 			for (i = 0; i < GTT_64K_PTE_STRIDE; i++) {
1642 				ops->clear_64k_splited(&old_se);
1643 				ops->set_pfn(&old_se,
1644 					vgpu->gtt.scratch_pt[type].page_mfn);
1645 				ppgtt_set_shadow_entry(spt, &old_se, index + i);
1646 			}
1647 		} else if (old_se.type == GTT_TYPE_PPGTT_PTE_2M_ENTRY ||
1648 			   old_se.type == GTT_TYPE_PPGTT_PTE_1G_ENTRY) {
1649 			ops->clear_pse(&old_se);
1650 			ops->set_pfn(&old_se,
1651 				     vgpu->gtt.scratch_pt[type].page_mfn);
1652 			ppgtt_set_shadow_entry(spt, &old_se, index);
1653 		} else {
1654 			ops->set_pfn(&old_se,
1655 				     vgpu->gtt.scratch_pt[type].page_mfn);
1656 			ppgtt_set_shadow_entry(spt, &old_se, index);
1657 		}
1658 	}
1659 
1660 	return 0;
1661 fail:
1662 	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d.\n",
1663 			spt, we->val64, we->type);
1664 	return ret;
1665 }
1666 
1667 
1668 
can_do_out_of_sync(struct intel_vgpu_ppgtt_spt * spt)1669 static inline bool can_do_out_of_sync(struct intel_vgpu_ppgtt_spt *spt)
1670 {
1671 	return enable_out_of_sync
1672 		&& gtt_type_is_pte_pt(spt->guest_page.type)
1673 		&& spt->guest_page.write_cnt >= 2;
1674 }
1675 
ppgtt_set_post_shadow(struct intel_vgpu_ppgtt_spt * spt,unsigned long index)1676 static void ppgtt_set_post_shadow(struct intel_vgpu_ppgtt_spt *spt,
1677 		unsigned long index)
1678 {
1679 	set_bit(index, spt->post_shadow_bitmap);
1680 	if (!list_empty(&spt->post_shadow_list))
1681 		return;
1682 
1683 	list_add_tail(&spt->post_shadow_list,
1684 			&spt->vgpu->gtt.post_shadow_list_head);
1685 }
1686 
1687 /**
1688  * intel_vgpu_flush_post_shadow - flush the post shadow transactions
1689  * @vgpu: a vGPU
1690  *
1691  * This function is called before submitting a guest workload to host,
1692  * to flush all the post shadows for a vGPU.
1693  *
1694  * Returns:
1695  * Zero on success, negative error code if failed.
1696  */
intel_vgpu_flush_post_shadow(struct intel_vgpu * vgpu)1697 int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu)
1698 {
1699 	struct list_head *pos, *n;
1700 	struct intel_vgpu_ppgtt_spt *spt;
1701 	struct intel_gvt_gtt_entry ge;
1702 	unsigned long index;
1703 	int ret;
1704 
1705 	list_for_each_safe(pos, n, &vgpu->gtt.post_shadow_list_head) {
1706 		spt = container_of(pos, struct intel_vgpu_ppgtt_spt,
1707 				post_shadow_list);
1708 
1709 		for_each_set_bit(index, spt->post_shadow_bitmap,
1710 				GTT_ENTRY_NUM_IN_ONE_PAGE) {
1711 			ppgtt_get_guest_entry(spt, &ge, index);
1712 
1713 			ret = ppgtt_handle_guest_write_page_table(spt,
1714 							&ge, index);
1715 			if (ret)
1716 				return ret;
1717 			clear_bit(index, spt->post_shadow_bitmap);
1718 		}
1719 		list_del_init(&spt->post_shadow_list);
1720 	}
1721 	return 0;
1722 }
1723 
ppgtt_handle_guest_write_page_table_bytes(struct intel_vgpu_ppgtt_spt * spt,u64 pa,void * p_data,int bytes)1724 static int ppgtt_handle_guest_write_page_table_bytes(
1725 		struct intel_vgpu_ppgtt_spt *spt,
1726 		u64 pa, void *p_data, int bytes)
1727 {
1728 	struct intel_vgpu *vgpu = spt->vgpu;
1729 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1730 	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
1731 	struct intel_gvt_gtt_entry we, se;
1732 	unsigned long index;
1733 	int ret;
1734 
1735 	index = (pa & (PAGE_SIZE - 1)) >> info->gtt_entry_size_shift;
1736 
1737 	ppgtt_get_guest_entry(spt, &we, index);
1738 
1739 	/*
1740 	 * For page table which has 64K gtt entry, only PTE#0, PTE#16,
1741 	 * PTE#32, ... PTE#496 are used. Unused PTEs update should be
1742 	 * ignored.
1743 	 */
1744 	if (we.type == GTT_TYPE_PPGTT_PTE_64K_ENTRY &&
1745 	    (index % GTT_64K_PTE_STRIDE)) {
1746 		gvt_vdbg_mm("Ignore write to unused PTE entry, index %lu\n",
1747 			    index);
1748 		return 0;
1749 	}
1750 
1751 	if (bytes == info->gtt_entry_size) {
1752 		ret = ppgtt_handle_guest_write_page_table(spt, &we, index);
1753 		if (ret)
1754 			return ret;
1755 	} else {
1756 		if (!test_bit(index, spt->post_shadow_bitmap)) {
1757 			int type = spt->shadow_page.type;
1758 
1759 			ppgtt_get_shadow_entry(spt, &se, index);
1760 			ret = ppgtt_handle_guest_entry_removal(spt, &se, index);
1761 			if (ret)
1762 				return ret;
1763 			ops->set_pfn(&se, vgpu->gtt.scratch_pt[type].page_mfn);
1764 			ppgtt_set_shadow_entry(spt, &se, index);
1765 		}
1766 		ppgtt_set_post_shadow(spt, index);
1767 	}
1768 
1769 	if (!enable_out_of_sync)
1770 		return 0;
1771 
1772 	spt->guest_page.write_cnt++;
1773 
1774 	if (spt->guest_page.oos_page)
1775 		ops->set_entry(spt->guest_page.oos_page->mem, &we, index,
1776 				false, 0, vgpu);
1777 
1778 	if (can_do_out_of_sync(spt)) {
1779 		if (!spt->guest_page.oos_page)
1780 			ppgtt_allocate_oos_page(spt);
1781 
1782 		ret = ppgtt_set_guest_page_oos(spt);
1783 		if (ret < 0)
1784 			return ret;
1785 	}
1786 	return 0;
1787 }
1788 
invalidate_ppgtt_mm(struct intel_vgpu_mm * mm)1789 static void invalidate_ppgtt_mm(struct intel_vgpu_mm *mm)
1790 {
1791 	struct intel_vgpu *vgpu = mm->vgpu;
1792 	struct intel_gvt *gvt = vgpu->gvt;
1793 	struct intel_gvt_gtt *gtt = &gvt->gtt;
1794 	struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops;
1795 	struct intel_gvt_gtt_entry se;
1796 	int index;
1797 
1798 	if (!mm->ppgtt_mm.shadowed)
1799 		return;
1800 
1801 	for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.shadow_pdps); index++) {
1802 		ppgtt_get_shadow_root_entry(mm, &se, index);
1803 
1804 		if (!ops->test_present(&se))
1805 			continue;
1806 
1807 		ppgtt_invalidate_spt_by_shadow_entry(vgpu, &se);
1808 		se.val64 = 0;
1809 		ppgtt_set_shadow_root_entry(mm, &se, index);
1810 
1811 		trace_spt_guest_change(vgpu->id, "destroy root pointer",
1812 				       NULL, se.type, se.val64, index);
1813 	}
1814 
1815 	mm->ppgtt_mm.shadowed = false;
1816 }
1817 
1818 
shadow_ppgtt_mm(struct intel_vgpu_mm * mm)1819 static int shadow_ppgtt_mm(struct intel_vgpu_mm *mm)
1820 {
1821 	struct intel_vgpu *vgpu = mm->vgpu;
1822 	struct intel_gvt *gvt = vgpu->gvt;
1823 	struct intel_gvt_gtt *gtt = &gvt->gtt;
1824 	struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops;
1825 	struct intel_vgpu_ppgtt_spt *spt;
1826 	struct intel_gvt_gtt_entry ge, se;
1827 	int index, ret;
1828 
1829 	if (mm->ppgtt_mm.shadowed)
1830 		return 0;
1831 
1832 	mm->ppgtt_mm.shadowed = true;
1833 
1834 	for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.guest_pdps); index++) {
1835 		ppgtt_get_guest_root_entry(mm, &ge, index);
1836 
1837 		if (!ops->test_present(&ge))
1838 			continue;
1839 
1840 		trace_spt_guest_change(vgpu->id, __func__, NULL,
1841 				       ge.type, ge.val64, index);
1842 
1843 		spt = ppgtt_populate_spt_by_guest_entry(vgpu, &ge);
1844 		if (IS_ERR(spt)) {
1845 			gvt_vgpu_err("fail to populate guest root pointer\n");
1846 			ret = PTR_ERR(spt);
1847 			goto fail;
1848 		}
1849 		ppgtt_generate_shadow_entry(&se, spt, &ge);
1850 		ppgtt_set_shadow_root_entry(mm, &se, index);
1851 
1852 		trace_spt_guest_change(vgpu->id, "populate root pointer",
1853 				       NULL, se.type, se.val64, index);
1854 	}
1855 
1856 	return 0;
1857 fail:
1858 	invalidate_ppgtt_mm(mm);
1859 	return ret;
1860 }
1861 
vgpu_alloc_mm(struct intel_vgpu * vgpu)1862 static struct intel_vgpu_mm *vgpu_alloc_mm(struct intel_vgpu *vgpu)
1863 {
1864 	struct intel_vgpu_mm *mm;
1865 
1866 	mm = kzalloc(sizeof(*mm), GFP_KERNEL);
1867 	if (!mm)
1868 		return NULL;
1869 
1870 	mm->vgpu = vgpu;
1871 	kref_init(&mm->ref);
1872 	atomic_set(&mm->pincount, 0);
1873 
1874 	return mm;
1875 }
1876 
vgpu_free_mm(struct intel_vgpu_mm * mm)1877 static void vgpu_free_mm(struct intel_vgpu_mm *mm)
1878 {
1879 	kfree(mm);
1880 }
1881 
1882 /**
1883  * intel_vgpu_create_ppgtt_mm - create a ppgtt mm object for a vGPU
1884  * @vgpu: a vGPU
1885  * @root_entry_type: ppgtt root entry type
1886  * @pdps: guest pdps.
1887  *
1888  * This function is used to create a ppgtt mm object for a vGPU.
1889  *
1890  * Returns:
1891  * Zero on success, negative error code in pointer if failed.
1892  */
intel_vgpu_create_ppgtt_mm(struct intel_vgpu * vgpu,enum intel_gvt_gtt_type root_entry_type,u64 pdps[])1893 struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
1894 		enum intel_gvt_gtt_type root_entry_type, u64 pdps[])
1895 {
1896 	struct intel_gvt *gvt = vgpu->gvt;
1897 	struct intel_vgpu_mm *mm;
1898 	int ret;
1899 
1900 	mm = vgpu_alloc_mm(vgpu);
1901 	if (!mm)
1902 		return ERR_PTR(-ENOMEM);
1903 
1904 	mm->type = INTEL_GVT_MM_PPGTT;
1905 
1906 	GEM_BUG_ON(root_entry_type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY &&
1907 		   root_entry_type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY);
1908 	mm->ppgtt_mm.root_entry_type = root_entry_type;
1909 
1910 	INIT_LIST_HEAD(&mm->ppgtt_mm.list);
1911 	INIT_LIST_HEAD(&mm->ppgtt_mm.lru_list);
1912 	INIT_LIST_HEAD(&mm->ppgtt_mm.link);
1913 
1914 	if (root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY)
1915 		mm->ppgtt_mm.guest_pdps[0] = pdps[0];
1916 	else
1917 		memcpy(mm->ppgtt_mm.guest_pdps, pdps,
1918 		       sizeof(mm->ppgtt_mm.guest_pdps));
1919 
1920 	ret = shadow_ppgtt_mm(mm);
1921 	if (ret) {
1922 		gvt_vgpu_err("failed to shadow ppgtt mm\n");
1923 		vgpu_free_mm(mm);
1924 		return ERR_PTR(ret);
1925 	}
1926 
1927 	list_add_tail(&mm->ppgtt_mm.list, &vgpu->gtt.ppgtt_mm_list_head);
1928 
1929 	mutex_lock(&gvt->gtt.ppgtt_mm_lock);
1930 	list_add_tail(&mm->ppgtt_mm.lru_list, &gvt->gtt.ppgtt_mm_lru_list_head);
1931 	mutex_unlock(&gvt->gtt.ppgtt_mm_lock);
1932 
1933 	return mm;
1934 }
1935 
intel_vgpu_create_ggtt_mm(struct intel_vgpu * vgpu)1936 static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu)
1937 {
1938 	struct intel_vgpu_mm *mm;
1939 	unsigned long nr_entries;
1940 
1941 	mm = vgpu_alloc_mm(vgpu);
1942 	if (!mm)
1943 		return ERR_PTR(-ENOMEM);
1944 
1945 	mm->type = INTEL_GVT_MM_GGTT;
1946 
1947 	nr_entries = gvt_ggtt_gm_sz(vgpu->gvt) >> I915_GTT_PAGE_SHIFT;
1948 	mm->ggtt_mm.virtual_ggtt =
1949 		vzalloc(array_size(nr_entries,
1950 				   vgpu->gvt->device_info.gtt_entry_size));
1951 	if (!mm->ggtt_mm.virtual_ggtt) {
1952 		vgpu_free_mm(mm);
1953 		return ERR_PTR(-ENOMEM);
1954 	}
1955 
1956 	return mm;
1957 }
1958 
1959 /**
1960  * _intel_vgpu_mm_release - destroy a mm object
1961  * @mm_ref: a kref object
1962  *
1963  * This function is used to destroy a mm object for vGPU
1964  *
1965  */
_intel_vgpu_mm_release(struct kref * mm_ref)1966 void _intel_vgpu_mm_release(struct kref *mm_ref)
1967 {
1968 	struct intel_vgpu_mm *mm = container_of(mm_ref, typeof(*mm), ref);
1969 
1970 	if (GEM_WARN_ON(atomic_read(&mm->pincount)))
1971 		gvt_err("vgpu mm pin count bug detected\n");
1972 
1973 	if (mm->type == INTEL_GVT_MM_PPGTT) {
1974 		list_del(&mm->ppgtt_mm.list);
1975 
1976 		mutex_lock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock);
1977 		list_del(&mm->ppgtt_mm.lru_list);
1978 		mutex_unlock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock);
1979 
1980 		invalidate_ppgtt_mm(mm);
1981 	} else {
1982 		vfree(mm->ggtt_mm.virtual_ggtt);
1983 	}
1984 
1985 	vgpu_free_mm(mm);
1986 }
1987 
1988 /**
1989  * intel_vgpu_unpin_mm - decrease the pin count of a vGPU mm object
1990  * @mm: a vGPU mm object
1991  *
1992  * This function is called when user doesn't want to use a vGPU mm object
1993  */
intel_vgpu_unpin_mm(struct intel_vgpu_mm * mm)1994 void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm)
1995 {
1996 	atomic_dec_if_positive(&mm->pincount);
1997 }
1998 
1999 /**
2000  * intel_vgpu_pin_mm - increase the pin count of a vGPU mm object
2001  * @mm: target vgpu mm
2002  *
2003  * This function is called when user wants to use a vGPU mm object. If this
2004  * mm object hasn't been shadowed yet, the shadow will be populated at this
2005  * time.
2006  *
2007  * Returns:
2008  * Zero on success, negative error code if failed.
2009  */
intel_vgpu_pin_mm(struct intel_vgpu_mm * mm)2010 int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm)
2011 {
2012 	int ret;
2013 
2014 	atomic_inc(&mm->pincount);
2015 
2016 	if (mm->type == INTEL_GVT_MM_PPGTT) {
2017 		ret = shadow_ppgtt_mm(mm);
2018 		if (ret)
2019 			return ret;
2020 
2021 		mutex_lock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock);
2022 		list_move_tail(&mm->ppgtt_mm.lru_list,
2023 			       &mm->vgpu->gvt->gtt.ppgtt_mm_lru_list_head);
2024 		mutex_unlock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock);
2025 	}
2026 
2027 	return 0;
2028 }
2029 
reclaim_one_ppgtt_mm(struct intel_gvt * gvt)2030 static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt)
2031 {
2032 	struct intel_vgpu_mm *mm;
2033 	struct list_head *pos, *n;
2034 
2035 	mutex_lock(&gvt->gtt.ppgtt_mm_lock);
2036 
2037 	list_for_each_safe(pos, n, &gvt->gtt.ppgtt_mm_lru_list_head) {
2038 		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.lru_list);
2039 
2040 		if (atomic_read(&mm->pincount))
2041 			continue;
2042 
2043 		list_del_init(&mm->ppgtt_mm.lru_list);
2044 		mutex_unlock(&gvt->gtt.ppgtt_mm_lock);
2045 		invalidate_ppgtt_mm(mm);
2046 		return 1;
2047 	}
2048 	mutex_unlock(&gvt->gtt.ppgtt_mm_lock);
2049 	return 0;
2050 }
2051 
2052 /*
2053  * GMA translation APIs.
2054  */
ppgtt_get_next_level_entry(struct intel_vgpu_mm * mm,struct intel_gvt_gtt_entry * e,unsigned long index,bool guest)2055 static inline int ppgtt_get_next_level_entry(struct intel_vgpu_mm *mm,
2056 		struct intel_gvt_gtt_entry *e, unsigned long index, bool guest)
2057 {
2058 	struct intel_vgpu *vgpu = mm->vgpu;
2059 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
2060 	struct intel_vgpu_ppgtt_spt *s;
2061 
2062 	s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e));
2063 	if (!s)
2064 		return -ENXIO;
2065 
2066 	if (!guest)
2067 		ppgtt_get_shadow_entry(s, e, index);
2068 	else
2069 		ppgtt_get_guest_entry(s, e, index);
2070 	return 0;
2071 }
2072 
2073 /**
2074  * intel_vgpu_gma_to_gpa - translate a gma to GPA
2075  * @mm: mm object. could be a PPGTT or GGTT mm object
2076  * @gma: graphics memory address in this mm object
2077  *
2078  * This function is used to translate a graphics memory address in specific
2079  * graphics memory space to guest physical address.
2080  *
2081  * Returns:
2082  * Guest physical address on success, INTEL_GVT_INVALID_ADDR if failed.
2083  */
intel_vgpu_gma_to_gpa(struct intel_vgpu_mm * mm,unsigned long gma)2084 unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm, unsigned long gma)
2085 {
2086 	struct intel_vgpu *vgpu = mm->vgpu;
2087 	struct intel_gvt *gvt = vgpu->gvt;
2088 	struct intel_gvt_gtt_pte_ops *pte_ops = gvt->gtt.pte_ops;
2089 	struct intel_gvt_gtt_gma_ops *gma_ops = gvt->gtt.gma_ops;
2090 	unsigned long gpa = INTEL_GVT_INVALID_ADDR;
2091 	unsigned long gma_index[4];
2092 	struct intel_gvt_gtt_entry e;
2093 	int i, levels = 0;
2094 	int ret;
2095 
2096 	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT &&
2097 		   mm->type != INTEL_GVT_MM_PPGTT);
2098 
2099 	if (mm->type == INTEL_GVT_MM_GGTT) {
2100 		if (!vgpu_gmadr_is_valid(vgpu, gma))
2101 			goto err;
2102 
2103 		ggtt_get_guest_entry(mm, &e,
2104 			gma_ops->gma_to_ggtt_pte_index(gma));
2105 
2106 		gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT)
2107 			+ (gma & ~I915_GTT_PAGE_MASK);
2108 
2109 		trace_gma_translate(vgpu->id, "ggtt", 0, 0, gma, gpa);
2110 	} else {
2111 		switch (mm->ppgtt_mm.root_entry_type) {
2112 		case GTT_TYPE_PPGTT_ROOT_L4_ENTRY:
2113 			ppgtt_get_shadow_root_entry(mm, &e, 0);
2114 
2115 			gma_index[0] = gma_ops->gma_to_pml4_index(gma);
2116 			gma_index[1] = gma_ops->gma_to_l4_pdp_index(gma);
2117 			gma_index[2] = gma_ops->gma_to_pde_index(gma);
2118 			gma_index[3] = gma_ops->gma_to_pte_index(gma);
2119 			levels = 4;
2120 			break;
2121 		case GTT_TYPE_PPGTT_ROOT_L3_ENTRY:
2122 			ppgtt_get_shadow_root_entry(mm, &e,
2123 					gma_ops->gma_to_l3_pdp_index(gma));
2124 
2125 			gma_index[0] = gma_ops->gma_to_pde_index(gma);
2126 			gma_index[1] = gma_ops->gma_to_pte_index(gma);
2127 			levels = 2;
2128 			break;
2129 		default:
2130 			GEM_BUG_ON(1);
2131 		}
2132 
2133 		/* walk the shadow page table and get gpa from guest entry */
2134 		for (i = 0; i < levels; i++) {
2135 			ret = ppgtt_get_next_level_entry(mm, &e, gma_index[i],
2136 				(i == levels - 1));
2137 			if (ret)
2138 				goto err;
2139 
2140 			if (!pte_ops->test_present(&e)) {
2141 				gvt_dbg_core("GMA 0x%lx is not present\n", gma);
2142 				goto err;
2143 			}
2144 		}
2145 
2146 		gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT) +
2147 					(gma & ~I915_GTT_PAGE_MASK);
2148 		trace_gma_translate(vgpu->id, "ppgtt", 0,
2149 				    mm->ppgtt_mm.root_entry_type, gma, gpa);
2150 	}
2151 
2152 	return gpa;
2153 err:
2154 	gvt_vgpu_err("invalid mm type: %d gma %lx\n", mm->type, gma);
2155 	return INTEL_GVT_INVALID_ADDR;
2156 }
2157 
emulate_ggtt_mmio_read(struct intel_vgpu * vgpu,unsigned int off,void * p_data,unsigned int bytes)2158 static int emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
2159 	unsigned int off, void *p_data, unsigned int bytes)
2160 {
2161 	struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm;
2162 	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
2163 	unsigned long index = off >> info->gtt_entry_size_shift;
2164 	unsigned long gma;
2165 	struct intel_gvt_gtt_entry e;
2166 
2167 	if (bytes != 4 && bytes != 8)
2168 		return -EINVAL;
2169 
2170 	gma = index << I915_GTT_PAGE_SHIFT;
2171 	if (!intel_gvt_ggtt_validate_range(vgpu,
2172 					   gma, 1 << I915_GTT_PAGE_SHIFT)) {
2173 		gvt_dbg_mm("read invalid ggtt at 0x%lx\n", gma);
2174 		memset(p_data, 0, bytes);
2175 		return 0;
2176 	}
2177 
2178 	ggtt_get_guest_entry(ggtt_mm, &e, index);
2179 	memcpy(p_data, (void *)&e.val64 + (off & (info->gtt_entry_size - 1)),
2180 			bytes);
2181 	return 0;
2182 }
2183 
2184 /**
2185  * intel_vgpu_emulate_gtt_mmio_read - emulate GTT MMIO register read
2186  * @vgpu: a vGPU
2187  * @off: register offset
2188  * @p_data: data will be returned to guest
2189  * @bytes: data length
2190  *
2191  * This function is used to emulate the GTT MMIO register read
2192  *
2193  * Returns:
2194  * Zero on success, error code if failed.
2195  */
intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu * vgpu,unsigned int off,void * p_data,unsigned int bytes)2196 int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu, unsigned int off,
2197 	void *p_data, unsigned int bytes)
2198 {
2199 	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
2200 	int ret;
2201 
2202 	if (bytes != 4 && bytes != 8)
2203 		return -EINVAL;
2204 
2205 	off -= info->gtt_start_offset;
2206 	ret = emulate_ggtt_mmio_read(vgpu, off, p_data, bytes);
2207 	return ret;
2208 }
2209 
ggtt_invalidate_pte(struct intel_vgpu * vgpu,struct intel_gvt_gtt_entry * entry)2210 static void ggtt_invalidate_pte(struct intel_vgpu *vgpu,
2211 		struct intel_gvt_gtt_entry *entry)
2212 {
2213 	struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
2214 	unsigned long pfn;
2215 
2216 	pfn = pte_ops->get_pfn(entry);
2217 	if (pfn != vgpu->gvt->gtt.scratch_mfn)
2218 		intel_gvt_hypervisor_dma_unmap_guest_page(vgpu,
2219 						pfn << PAGE_SHIFT);
2220 }
2221 
emulate_ggtt_mmio_write(struct intel_vgpu * vgpu,unsigned int off,void * p_data,unsigned int bytes)2222 static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off,
2223 	void *p_data, unsigned int bytes)
2224 {
2225 	struct intel_gvt *gvt = vgpu->gvt;
2226 	const struct intel_gvt_device_info *info = &gvt->device_info;
2227 	struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm;
2228 	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
2229 	unsigned long g_gtt_index = off >> info->gtt_entry_size_shift;
2230 	unsigned long gma, gfn;
2231 	struct intel_gvt_gtt_entry e = {.val64 = 0, .type = GTT_TYPE_GGTT_PTE};
2232 	struct intel_gvt_gtt_entry m = {.val64 = 0, .type = GTT_TYPE_GGTT_PTE};
2233 	dma_addr_t dma_addr;
2234 	int ret;
2235 	struct intel_gvt_partial_pte *partial_pte, *pos, *n;
2236 	bool partial_update = false;
2237 
2238 	if (bytes != 4 && bytes != 8)
2239 		return -EINVAL;
2240 
2241 	gma = g_gtt_index << I915_GTT_PAGE_SHIFT;
2242 
2243 	/* the VM may configure the whole GM space when ballooning is used */
2244 	if (!vgpu_gmadr_is_valid(vgpu, gma))
2245 		return 0;
2246 
2247 	e.type = GTT_TYPE_GGTT_PTE;
2248 	memcpy((void *)&e.val64 + (off & (info->gtt_entry_size - 1)), p_data,
2249 			bytes);
2250 
2251 	/* If ggtt entry size is 8 bytes, and it's split into two 4 bytes
2252 	 * write, save the first 4 bytes in a list and update virtual
2253 	 * PTE. Only update shadow PTE when the second 4 bytes comes.
2254 	 */
2255 	if (bytes < info->gtt_entry_size) {
2256 		bool found = false;
2257 
2258 		list_for_each_entry_safe(pos, n,
2259 				&ggtt_mm->ggtt_mm.partial_pte_list, list) {
2260 			if (g_gtt_index == pos->offset >>
2261 					info->gtt_entry_size_shift) {
2262 				if (off != pos->offset) {
2263 					/* the second partial part*/
2264 					int last_off = pos->offset &
2265 						(info->gtt_entry_size - 1);
2266 
2267 					memcpy((void *)&e.val64 + last_off,
2268 						(void *)&pos->data + last_off,
2269 						bytes);
2270 
2271 					list_del(&pos->list);
2272 					kfree(pos);
2273 					found = true;
2274 					break;
2275 				}
2276 
2277 				/* update of the first partial part */
2278 				pos->data = e.val64;
2279 				ggtt_set_guest_entry(ggtt_mm, &e, g_gtt_index);
2280 				return 0;
2281 			}
2282 		}
2283 
2284 		if (!found) {
2285 			/* the first partial part */
2286 			partial_pte = kzalloc(sizeof(*partial_pte), GFP_KERNEL);
2287 			if (!partial_pte)
2288 				return -ENOMEM;
2289 			partial_pte->offset = off;
2290 			partial_pte->data = e.val64;
2291 			list_add_tail(&partial_pte->list,
2292 				&ggtt_mm->ggtt_mm.partial_pte_list);
2293 			partial_update = true;
2294 		}
2295 	}
2296 
2297 	if (!partial_update && (ops->test_present(&e))) {
2298 		gfn = ops->get_pfn(&e);
2299 		m.val64 = e.val64;
2300 		m.type = e.type;
2301 
2302 		/* one PTE update may be issued in multiple writes and the
2303 		 * first write may not construct a valid gfn
2304 		 */
2305 		if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
2306 			ops->set_pfn(&m, gvt->gtt.scratch_mfn);
2307 			goto out;
2308 		}
2309 
2310 		ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn,
2311 							PAGE_SIZE, &dma_addr);
2312 		if (ret) {
2313 			gvt_vgpu_err("fail to populate guest ggtt entry\n");
2314 			/* guest driver may read/write the entry when partial
2315 			 * update the entry in this situation p2m will fail
2316 			 * settting the shadow entry to point to a scratch page
2317 			 */
2318 			ops->set_pfn(&m, gvt->gtt.scratch_mfn);
2319 		} else
2320 			ops->set_pfn(&m, dma_addr >> PAGE_SHIFT);
2321 	} else {
2322 		ops->set_pfn(&m, gvt->gtt.scratch_mfn);
2323 		ops->clear_present(&m);
2324 	}
2325 
2326 out:
2327 	ggtt_set_guest_entry(ggtt_mm, &e, g_gtt_index);
2328 
2329 	ggtt_get_host_entry(ggtt_mm, &e, g_gtt_index);
2330 	ggtt_invalidate_pte(vgpu, &e);
2331 
2332 	ggtt_set_host_entry(ggtt_mm, &m, g_gtt_index);
2333 	ggtt_invalidate(gvt->gt);
2334 	return 0;
2335 }
2336 
2337 /*
2338  * intel_vgpu_emulate_ggtt_mmio_write - emulate GTT MMIO register write
2339  * @vgpu: a vGPU
2340  * @off: register offset
2341  * @p_data: data from guest write
2342  * @bytes: data length
2343  *
2344  * This function is used to emulate the GTT MMIO register write
2345  *
2346  * Returns:
2347  * Zero on success, error code if failed.
2348  */
intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu * vgpu,unsigned int off,void * p_data,unsigned int bytes)2349 int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
2350 		unsigned int off, void *p_data, unsigned int bytes)
2351 {
2352 	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
2353 	int ret;
2354 	struct intel_vgpu_submission *s = &vgpu->submission;
2355 	struct intel_engine_cs *engine;
2356 	int i;
2357 
2358 	if (bytes != 4 && bytes != 8)
2359 		return -EINVAL;
2360 
2361 	off -= info->gtt_start_offset;
2362 	ret = emulate_ggtt_mmio_write(vgpu, off, p_data, bytes);
2363 
2364 	/* if ggtt of last submitted context is written,
2365 	 * that context is probably got unpinned.
2366 	 * Set last shadowed ctx to invalid.
2367 	 */
2368 	for_each_engine(engine, vgpu->gvt->gt, i) {
2369 		if (!s->last_ctx[i].valid)
2370 			continue;
2371 
2372 		if (s->last_ctx[i].lrca == (off >> info->gtt_entry_size_shift))
2373 			s->last_ctx[i].valid = false;
2374 	}
2375 	return ret;
2376 }
2377 
alloc_scratch_pages(struct intel_vgpu * vgpu,enum intel_gvt_gtt_type type)2378 static int alloc_scratch_pages(struct intel_vgpu *vgpu,
2379 		enum intel_gvt_gtt_type type)
2380 {
2381 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
2382 	struct intel_vgpu_gtt *gtt = &vgpu->gtt;
2383 	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
2384 	int page_entry_num = I915_GTT_PAGE_SIZE >>
2385 				vgpu->gvt->device_info.gtt_entry_size_shift;
2386 	void *scratch_pt;
2387 	int i;
2388 	struct device *dev = &vgpu->gvt->gt->i915->drm.pdev->dev;
2389 	dma_addr_t daddr;
2390 
2391 	if (drm_WARN_ON(&i915->drm,
2392 			type < GTT_TYPE_PPGTT_PTE_PT || type >= GTT_TYPE_MAX))
2393 		return -EINVAL;
2394 
2395 	scratch_pt = (void *)get_zeroed_page(GFP_KERNEL);
2396 	if (!scratch_pt) {
2397 		gvt_vgpu_err("fail to allocate scratch page\n");
2398 		return -ENOMEM;
2399 	}
2400 
2401 	daddr = dma_map_page(dev, virt_to_page(scratch_pt), 0,
2402 			4096, PCI_DMA_BIDIRECTIONAL);
2403 	if (dma_mapping_error(dev, daddr)) {
2404 		gvt_vgpu_err("fail to dmamap scratch_pt\n");
2405 		__free_page(virt_to_page(scratch_pt));
2406 		return -ENOMEM;
2407 	}
2408 	gtt->scratch_pt[type].page_mfn =
2409 		(unsigned long)(daddr >> I915_GTT_PAGE_SHIFT);
2410 	gtt->scratch_pt[type].page = virt_to_page(scratch_pt);
2411 	gvt_dbg_mm("vgpu%d create scratch_pt: type %d mfn=0x%lx\n",
2412 			vgpu->id, type, gtt->scratch_pt[type].page_mfn);
2413 
2414 	/* Build the tree by full filled the scratch pt with the entries which
2415 	 * point to the next level scratch pt or scratch page. The
2416 	 * scratch_pt[type] indicate the scratch pt/scratch page used by the
2417 	 * 'type' pt.
2418 	 * e.g. scratch_pt[GTT_TYPE_PPGTT_PDE_PT] is used by
2419 	 * GTT_TYPE_PPGTT_PDE_PT level pt, that means this scratch_pt it self
2420 	 * is GTT_TYPE_PPGTT_PTE_PT, and full filled by scratch page mfn.
2421 	 */
2422 	if (type > GTT_TYPE_PPGTT_PTE_PT) {
2423 		struct intel_gvt_gtt_entry se;
2424 
2425 		memset(&se, 0, sizeof(struct intel_gvt_gtt_entry));
2426 		se.type = get_entry_type(type - 1);
2427 		ops->set_pfn(&se, gtt->scratch_pt[type - 1].page_mfn);
2428 
2429 		/* The entry parameters like present/writeable/cache type
2430 		 * set to the same as i915's scratch page tree.
2431 		 */
2432 		se.val64 |= _PAGE_PRESENT | _PAGE_RW;
2433 		if (type == GTT_TYPE_PPGTT_PDE_PT)
2434 			se.val64 |= PPAT_CACHED;
2435 
2436 		for (i = 0; i < page_entry_num; i++)
2437 			ops->set_entry(scratch_pt, &se, i, false, 0, vgpu);
2438 	}
2439 
2440 	return 0;
2441 }
2442 
release_scratch_page_tree(struct intel_vgpu * vgpu)2443 static int release_scratch_page_tree(struct intel_vgpu *vgpu)
2444 {
2445 	int i;
2446 	struct device *dev = &vgpu->gvt->gt->i915->drm.pdev->dev;
2447 	dma_addr_t daddr;
2448 
2449 	for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) {
2450 		if (vgpu->gtt.scratch_pt[i].page != NULL) {
2451 			daddr = (dma_addr_t)(vgpu->gtt.scratch_pt[i].page_mfn <<
2452 					I915_GTT_PAGE_SHIFT);
2453 			dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
2454 			__free_page(vgpu->gtt.scratch_pt[i].page);
2455 			vgpu->gtt.scratch_pt[i].page = NULL;
2456 			vgpu->gtt.scratch_pt[i].page_mfn = 0;
2457 		}
2458 	}
2459 
2460 	return 0;
2461 }
2462 
create_scratch_page_tree(struct intel_vgpu * vgpu)2463 static int create_scratch_page_tree(struct intel_vgpu *vgpu)
2464 {
2465 	int i, ret;
2466 
2467 	for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) {
2468 		ret = alloc_scratch_pages(vgpu, i);
2469 		if (ret)
2470 			goto err;
2471 	}
2472 
2473 	return 0;
2474 
2475 err:
2476 	release_scratch_page_tree(vgpu);
2477 	return ret;
2478 }
2479 
2480 /**
2481  * intel_vgpu_init_gtt - initialize per-vGPU graphics memory virulization
2482  * @vgpu: a vGPU
2483  *
2484  * This function is used to initialize per-vGPU graphics memory virtualization
2485  * components.
2486  *
2487  * Returns:
2488  * Zero on success, error code if failed.
2489  */
intel_vgpu_init_gtt(struct intel_vgpu * vgpu)2490 int intel_vgpu_init_gtt(struct intel_vgpu *vgpu)
2491 {
2492 	struct intel_vgpu_gtt *gtt = &vgpu->gtt;
2493 
2494 	INIT_RADIX_TREE(&gtt->spt_tree, GFP_KERNEL);
2495 
2496 	INIT_LIST_HEAD(&gtt->ppgtt_mm_list_head);
2497 	INIT_LIST_HEAD(&gtt->oos_page_list_head);
2498 	INIT_LIST_HEAD(&gtt->post_shadow_list_head);
2499 
2500 	gtt->ggtt_mm = intel_vgpu_create_ggtt_mm(vgpu);
2501 	if (IS_ERR(gtt->ggtt_mm)) {
2502 		gvt_vgpu_err("fail to create mm for ggtt.\n");
2503 		return PTR_ERR(gtt->ggtt_mm);
2504 	}
2505 
2506 	intel_vgpu_reset_ggtt(vgpu, false);
2507 
2508 	INIT_LIST_HEAD(&gtt->ggtt_mm->ggtt_mm.partial_pte_list);
2509 
2510 	return create_scratch_page_tree(vgpu);
2511 }
2512 
intel_vgpu_destroy_all_ppgtt_mm(struct intel_vgpu * vgpu)2513 void intel_vgpu_destroy_all_ppgtt_mm(struct intel_vgpu *vgpu)
2514 {
2515 	struct list_head *pos, *n;
2516 	struct intel_vgpu_mm *mm;
2517 
2518 	list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) {
2519 		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
2520 		intel_vgpu_destroy_mm(mm);
2521 	}
2522 
2523 	if (GEM_WARN_ON(!list_empty(&vgpu->gtt.ppgtt_mm_list_head)))
2524 		gvt_err("vgpu ppgtt mm is not fully destroyed\n");
2525 
2526 	if (GEM_WARN_ON(!radix_tree_empty(&vgpu->gtt.spt_tree))) {
2527 		gvt_err("Why we still has spt not freed?\n");
2528 		ppgtt_free_all_spt(vgpu);
2529 	}
2530 }
2531 
intel_vgpu_destroy_ggtt_mm(struct intel_vgpu * vgpu)2532 static void intel_vgpu_destroy_ggtt_mm(struct intel_vgpu *vgpu)
2533 {
2534 	struct intel_gvt_partial_pte *pos, *next;
2535 
2536 	list_for_each_entry_safe(pos, next,
2537 				 &vgpu->gtt.ggtt_mm->ggtt_mm.partial_pte_list,
2538 				 list) {
2539 		gvt_dbg_mm("partial PTE update on hold 0x%lx : 0x%llx\n",
2540 			pos->offset, pos->data);
2541 		kfree(pos);
2542 	}
2543 	intel_vgpu_destroy_mm(vgpu->gtt.ggtt_mm);
2544 	vgpu->gtt.ggtt_mm = NULL;
2545 }
2546 
2547 /**
2548  * intel_vgpu_clean_gtt - clean up per-vGPU graphics memory virulization
2549  * @vgpu: a vGPU
2550  *
2551  * This function is used to clean up per-vGPU graphics memory virtualization
2552  * components.
2553  *
2554  * Returns:
2555  * Zero on success, error code if failed.
2556  */
intel_vgpu_clean_gtt(struct intel_vgpu * vgpu)2557 void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu)
2558 {
2559 	intel_vgpu_destroy_all_ppgtt_mm(vgpu);
2560 	intel_vgpu_destroy_ggtt_mm(vgpu);
2561 	release_scratch_page_tree(vgpu);
2562 }
2563 
clean_spt_oos(struct intel_gvt * gvt)2564 static void clean_spt_oos(struct intel_gvt *gvt)
2565 {
2566 	struct intel_gvt_gtt *gtt = &gvt->gtt;
2567 	struct list_head *pos, *n;
2568 	struct intel_vgpu_oos_page *oos_page;
2569 
2570 	WARN(!list_empty(&gtt->oos_page_use_list_head),
2571 		"someone is still using oos page\n");
2572 
2573 	list_for_each_safe(pos, n, &gtt->oos_page_free_list_head) {
2574 		oos_page = container_of(pos, struct intel_vgpu_oos_page, list);
2575 		list_del(&oos_page->list);
2576 		free_page((unsigned long)oos_page->mem);
2577 		kfree(oos_page);
2578 	}
2579 }
2580 
setup_spt_oos(struct intel_gvt * gvt)2581 static int setup_spt_oos(struct intel_gvt *gvt)
2582 {
2583 	struct intel_gvt_gtt *gtt = &gvt->gtt;
2584 	struct intel_vgpu_oos_page *oos_page;
2585 	int i;
2586 	int ret;
2587 
2588 	INIT_LIST_HEAD(&gtt->oos_page_free_list_head);
2589 	INIT_LIST_HEAD(&gtt->oos_page_use_list_head);
2590 
2591 	for (i = 0; i < preallocated_oos_pages; i++) {
2592 		oos_page = kzalloc(sizeof(*oos_page), GFP_KERNEL);
2593 		if (!oos_page) {
2594 			ret = -ENOMEM;
2595 			goto fail;
2596 		}
2597 		oos_page->mem = (void *)__get_free_pages(GFP_KERNEL, 0);
2598 		if (!oos_page->mem) {
2599 			ret = -ENOMEM;
2600 			kfree(oos_page);
2601 			goto fail;
2602 		}
2603 
2604 		INIT_LIST_HEAD(&oos_page->list);
2605 		INIT_LIST_HEAD(&oos_page->vm_list);
2606 		oos_page->id = i;
2607 		list_add_tail(&oos_page->list, &gtt->oos_page_free_list_head);
2608 	}
2609 
2610 	gvt_dbg_mm("%d oos pages preallocated\n", i);
2611 
2612 	return 0;
2613 fail:
2614 	clean_spt_oos(gvt);
2615 	return ret;
2616 }
2617 
2618 /**
2619  * intel_vgpu_find_ppgtt_mm - find a PPGTT mm object
2620  * @vgpu: a vGPU
2621  * @pdps: pdp root array
2622  *
2623  * This function is used to find a PPGTT mm object from mm object pool
2624  *
2625  * Returns:
2626  * pointer to mm object on success, NULL if failed.
2627  */
intel_vgpu_find_ppgtt_mm(struct intel_vgpu * vgpu,u64 pdps[])2628 struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
2629 		u64 pdps[])
2630 {
2631 	struct intel_vgpu_mm *mm;
2632 	struct list_head *pos;
2633 
2634 	list_for_each(pos, &vgpu->gtt.ppgtt_mm_list_head) {
2635 		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
2636 
2637 		switch (mm->ppgtt_mm.root_entry_type) {
2638 		case GTT_TYPE_PPGTT_ROOT_L4_ENTRY:
2639 			if (pdps[0] == mm->ppgtt_mm.guest_pdps[0])
2640 				return mm;
2641 			break;
2642 		case GTT_TYPE_PPGTT_ROOT_L3_ENTRY:
2643 			if (!memcmp(pdps, mm->ppgtt_mm.guest_pdps,
2644 				    sizeof(mm->ppgtt_mm.guest_pdps)))
2645 				return mm;
2646 			break;
2647 		default:
2648 			GEM_BUG_ON(1);
2649 		}
2650 	}
2651 	return NULL;
2652 }
2653 
2654 /**
2655  * intel_vgpu_get_ppgtt_mm - get or create a PPGTT mm object.
2656  * @vgpu: a vGPU
2657  * @root_entry_type: ppgtt root entry type
2658  * @pdps: guest pdps
2659  *
2660  * This function is used to find or create a PPGTT mm object from a guest.
2661  *
2662  * Returns:
2663  * Zero on success, negative error code if failed.
2664  */
intel_vgpu_get_ppgtt_mm(struct intel_vgpu * vgpu,enum intel_gvt_gtt_type root_entry_type,u64 pdps[])2665 struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
2666 		enum intel_gvt_gtt_type root_entry_type, u64 pdps[])
2667 {
2668 	struct intel_vgpu_mm *mm;
2669 
2670 	mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps);
2671 	if (mm) {
2672 		intel_vgpu_mm_get(mm);
2673 	} else {
2674 		mm = intel_vgpu_create_ppgtt_mm(vgpu, root_entry_type, pdps);
2675 		if (IS_ERR(mm))
2676 			gvt_vgpu_err("fail to create mm\n");
2677 	}
2678 	return mm;
2679 }
2680 
2681 /**
2682  * intel_vgpu_put_ppgtt_mm - find and put a PPGTT mm object.
2683  * @vgpu: a vGPU
2684  * @pdps: guest pdps
2685  *
2686  * This function is used to find a PPGTT mm object from a guest and destroy it.
2687  *
2688  * Returns:
2689  * Zero on success, negative error code if failed.
2690  */
intel_vgpu_put_ppgtt_mm(struct intel_vgpu * vgpu,u64 pdps[])2691 int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[])
2692 {
2693 	struct intel_vgpu_mm *mm;
2694 
2695 	mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps);
2696 	if (!mm) {
2697 		gvt_vgpu_err("fail to find ppgtt instance.\n");
2698 		return -EINVAL;
2699 	}
2700 	intel_vgpu_mm_put(mm);
2701 	return 0;
2702 }
2703 
2704 /**
2705  * intel_gvt_init_gtt - initialize mm components of a GVT device
2706  * @gvt: GVT device
2707  *
2708  * This function is called at the initialization stage, to initialize
2709  * the mm components of a GVT device.
2710  *
2711  * Returns:
2712  * zero on success, negative error code if failed.
2713  */
intel_gvt_init_gtt(struct intel_gvt * gvt)2714 int intel_gvt_init_gtt(struct intel_gvt *gvt)
2715 {
2716 	int ret;
2717 	void *page;
2718 	struct device *dev = &gvt->gt->i915->drm.pdev->dev;
2719 	dma_addr_t daddr;
2720 
2721 	gvt_dbg_core("init gtt\n");
2722 
2723 	gvt->gtt.pte_ops = &gen8_gtt_pte_ops;
2724 	gvt->gtt.gma_ops = &gen8_gtt_gma_ops;
2725 
2726 	page = (void *)get_zeroed_page(GFP_KERNEL);
2727 	if (!page) {
2728 		gvt_err("fail to allocate scratch ggtt page\n");
2729 		return -ENOMEM;
2730 	}
2731 
2732 	daddr = dma_map_page(dev, virt_to_page(page), 0,
2733 			4096, PCI_DMA_BIDIRECTIONAL);
2734 	if (dma_mapping_error(dev, daddr)) {
2735 		gvt_err("fail to dmamap scratch ggtt page\n");
2736 		__free_page(virt_to_page(page));
2737 		return -ENOMEM;
2738 	}
2739 
2740 	gvt->gtt.scratch_page = virt_to_page(page);
2741 	gvt->gtt.scratch_mfn = (unsigned long)(daddr >> I915_GTT_PAGE_SHIFT);
2742 
2743 	if (enable_out_of_sync) {
2744 		ret = setup_spt_oos(gvt);
2745 		if (ret) {
2746 			gvt_err("fail to initialize SPT oos\n");
2747 			dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
2748 			__free_page(gvt->gtt.scratch_page);
2749 			return ret;
2750 		}
2751 	}
2752 	INIT_LIST_HEAD(&gvt->gtt.ppgtt_mm_lru_list_head);
2753 	mutex_init(&gvt->gtt.ppgtt_mm_lock);
2754 	return 0;
2755 }
2756 
2757 /**
2758  * intel_gvt_clean_gtt - clean up mm components of a GVT device
2759  * @gvt: GVT device
2760  *
2761  * This function is called at the driver unloading stage, to clean up the
2762  * the mm components of a GVT device.
2763  *
2764  */
intel_gvt_clean_gtt(struct intel_gvt * gvt)2765 void intel_gvt_clean_gtt(struct intel_gvt *gvt)
2766 {
2767 	struct device *dev = &gvt->gt->i915->drm.pdev->dev;
2768 	dma_addr_t daddr = (dma_addr_t)(gvt->gtt.scratch_mfn <<
2769 					I915_GTT_PAGE_SHIFT);
2770 
2771 	dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
2772 
2773 	__free_page(gvt->gtt.scratch_page);
2774 
2775 	if (enable_out_of_sync)
2776 		clean_spt_oos(gvt);
2777 }
2778 
2779 /**
2780  * intel_vgpu_invalidate_ppgtt - invalidate PPGTT instances
2781  * @vgpu: a vGPU
2782  *
2783  * This function is called when invalidate all PPGTT instances of a vGPU.
2784  *
2785  */
intel_vgpu_invalidate_ppgtt(struct intel_vgpu * vgpu)2786 void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu)
2787 {
2788 	struct list_head *pos, *n;
2789 	struct intel_vgpu_mm *mm;
2790 
2791 	list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) {
2792 		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
2793 		if (mm->type == INTEL_GVT_MM_PPGTT) {
2794 			mutex_lock(&vgpu->gvt->gtt.ppgtt_mm_lock);
2795 			list_del_init(&mm->ppgtt_mm.lru_list);
2796 			mutex_unlock(&vgpu->gvt->gtt.ppgtt_mm_lock);
2797 			if (mm->ppgtt_mm.shadowed)
2798 				invalidate_ppgtt_mm(mm);
2799 		}
2800 	}
2801 }
2802 
2803 /**
2804  * intel_vgpu_reset_ggtt - reset the GGTT entry
2805  * @vgpu: a vGPU
2806  * @invalidate_old: invalidate old entries
2807  *
2808  * This function is called at the vGPU create stage
2809  * to reset all the GGTT entries.
2810  *
2811  */
intel_vgpu_reset_ggtt(struct intel_vgpu * vgpu,bool invalidate_old)2812 void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old)
2813 {
2814 	struct intel_gvt *gvt = vgpu->gvt;
2815 	struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
2816 	struct intel_gvt_gtt_entry entry = {.type = GTT_TYPE_GGTT_PTE};
2817 	struct intel_gvt_gtt_entry old_entry;
2818 	u32 index;
2819 	u32 num_entries;
2820 
2821 	pte_ops->set_pfn(&entry, gvt->gtt.scratch_mfn);
2822 	pte_ops->set_present(&entry);
2823 
2824 	index = vgpu_aperture_gmadr_base(vgpu) >> PAGE_SHIFT;
2825 	num_entries = vgpu_aperture_sz(vgpu) >> PAGE_SHIFT;
2826 	while (num_entries--) {
2827 		if (invalidate_old) {
2828 			ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index);
2829 			ggtt_invalidate_pte(vgpu, &old_entry);
2830 		}
2831 		ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++);
2832 	}
2833 
2834 	index = vgpu_hidden_gmadr_base(vgpu) >> PAGE_SHIFT;
2835 	num_entries = vgpu_hidden_sz(vgpu) >> PAGE_SHIFT;
2836 	while (num_entries--) {
2837 		if (invalidate_old) {
2838 			ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index);
2839 			ggtt_invalidate_pte(vgpu, &old_entry);
2840 		}
2841 		ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++);
2842 	}
2843 
2844 	ggtt_invalidate(gvt->gt);
2845 }
2846 
2847 /**
2848  * intel_vgpu_reset_gtt - reset the all GTT related status
2849  * @vgpu: a vGPU
2850  *
2851  * This function is called from vfio core to reset reset all
2852  * GTT related status, including GGTT, PPGTT, scratch page.
2853  *
2854  */
intel_vgpu_reset_gtt(struct intel_vgpu * vgpu)2855 void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu)
2856 {
2857 	/* Shadow pages are only created when there is no page
2858 	 * table tracking data, so remove page tracking data after
2859 	 * removing the shadow pages.
2860 	 */
2861 	intel_vgpu_destroy_all_ppgtt_mm(vgpu);
2862 	intel_vgpu_reset_ggtt(vgpu, true);
2863 }
2864