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