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