blob: 23296547da95e8634c3bbaa401225c2d415498d5 [file] [log] [blame]
Zhi Wang2707e442016-03-28 23:23:16 +08001/*
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"
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +080037#include "gvt.h"
38#include "i915_pvinfo.h"
Zhi Wang2707e442016-03-28 23:23:16 +080039#include "trace.h"
40
Changbin Dubc37ab52018-01-30 19:19:44 +080041#if defined(VERBOSE_DEBUG)
42#define gvt_vdbg_mm(fmt, args...) gvt_dbg_mm(fmt, ##args)
43#else
44#define gvt_vdbg_mm(fmt, args...)
45#endif
46
Zhi Wang2707e442016-03-28 23:23:16 +080047static bool enable_out_of_sync = false;
48static int preallocated_oos_pages = 8192;
49
50/*
51 * validate a gm address and related range size,
52 * translate it to host gm address
53 */
54bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size)
55{
56 if ((!vgpu_gmadr_is_valid(vgpu, addr)) || (size
57 && !vgpu_gmadr_is_valid(vgpu, addr + size - 1))) {
Tina Zhang695fbc02017-03-10 04:26:53 -050058 gvt_vgpu_err("invalid range gmadr 0x%llx size 0x%x\n",
59 addr, size);
Zhi Wang2707e442016-03-28 23:23:16 +080060 return false;
61 }
62 return true;
63}
64
65/* translate a guest gmadr to host gmadr */
66int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr)
67{
68 if (WARN(!vgpu_gmadr_is_valid(vgpu, g_addr),
69 "invalid guest gmadr %llx\n", g_addr))
70 return -EACCES;
71
72 if (vgpu_gmadr_is_aperture(vgpu, g_addr))
73 *h_addr = vgpu_aperture_gmadr_base(vgpu)
74 + (g_addr - vgpu_aperture_offset(vgpu));
75 else
76 *h_addr = vgpu_hidden_gmadr_base(vgpu)
77 + (g_addr - vgpu_hidden_offset(vgpu));
78 return 0;
79}
80
81/* translate a host gmadr to guest gmadr */
82int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr)
83{
84 if (WARN(!gvt_gmadr_is_valid(vgpu->gvt, h_addr),
85 "invalid host gmadr %llx\n", h_addr))
86 return -EACCES;
87
88 if (gvt_gmadr_is_aperture(vgpu->gvt, h_addr))
89 *g_addr = vgpu_aperture_gmadr_base(vgpu)
90 + (h_addr - gvt_aperture_gmadr_base(vgpu->gvt));
91 else
92 *g_addr = vgpu_hidden_gmadr_base(vgpu)
93 + (h_addr - gvt_hidden_gmadr_base(vgpu->gvt));
94 return 0;
95}
96
97int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index,
98 unsigned long *h_index)
99{
100 u64 h_addr;
101 int ret;
102
Zhi Wang9556e112017-10-10 13:51:32 +0800103 ret = intel_gvt_ggtt_gmadr_g2h(vgpu, g_index << I915_GTT_PAGE_SHIFT,
Zhi Wang2707e442016-03-28 23:23:16 +0800104 &h_addr);
105 if (ret)
106 return ret;
107
Zhi Wang9556e112017-10-10 13:51:32 +0800108 *h_index = h_addr >> I915_GTT_PAGE_SHIFT;
Zhi Wang2707e442016-03-28 23:23:16 +0800109 return 0;
110}
111
112int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index,
113 unsigned long *g_index)
114{
115 u64 g_addr;
116 int ret;
117
Zhi Wang9556e112017-10-10 13:51:32 +0800118 ret = intel_gvt_ggtt_gmadr_h2g(vgpu, h_index << I915_GTT_PAGE_SHIFT,
Zhi Wang2707e442016-03-28 23:23:16 +0800119 &g_addr);
120 if (ret)
121 return ret;
122
Zhi Wang9556e112017-10-10 13:51:32 +0800123 *g_index = g_addr >> I915_GTT_PAGE_SHIFT;
Zhi Wang2707e442016-03-28 23:23:16 +0800124 return 0;
125}
126
127#define gtt_type_is_entry(type) \
128 (type > GTT_TYPE_INVALID && type < GTT_TYPE_PPGTT_ENTRY \
129 && type != GTT_TYPE_PPGTT_PTE_ENTRY \
130 && type != GTT_TYPE_PPGTT_ROOT_ENTRY)
131
132#define gtt_type_is_pt(type) \
133 (type >= GTT_TYPE_PPGTT_PTE_PT && type < GTT_TYPE_MAX)
134
135#define gtt_type_is_pte_pt(type) \
136 (type == GTT_TYPE_PPGTT_PTE_PT)
137
138#define gtt_type_is_root_pointer(type) \
139 (gtt_type_is_entry(type) && type > GTT_TYPE_PPGTT_ROOT_ENTRY)
140
141#define gtt_init_entry(e, t, p, v) do { \
142 (e)->type = t; \
143 (e)->pdev = p; \
144 memcpy(&(e)->val64, &v, sizeof(v)); \
145} while (0)
146
Zhi Wang2707e442016-03-28 23:23:16 +0800147/*
148 * Mappings between GTT_TYPE* enumerations.
149 * Following information can be found according to the given type:
150 * - type of next level page table
151 * - type of entry inside this level page table
152 * - type of entry with PSE set
153 *
154 * If the given type doesn't have such a kind of information,
155 * e.g. give a l4 root entry type, then request to get its PSE type,
156 * give a PTE page table type, then request to get its next level page
157 * table type, as we know l4 root entry doesn't have a PSE bit,
158 * and a PTE page table doesn't have a next level page table type,
159 * GTT_TYPE_INVALID will be returned. This is useful when traversing a
160 * page table.
161 */
162
163struct gtt_type_table_entry {
164 int entry_type;
Zhi Wang054f4eb2017-10-10 17:19:30 +0800165 int pt_type;
Zhi Wang2707e442016-03-28 23:23:16 +0800166 int next_pt_type;
167 int pse_entry_type;
168};
169
Zhi Wang054f4eb2017-10-10 17:19:30 +0800170#define GTT_TYPE_TABLE_ENTRY(type, e_type, cpt_type, npt_type, pse_type) \
Zhi Wang2707e442016-03-28 23:23:16 +0800171 [type] = { \
172 .entry_type = e_type, \
Zhi Wang054f4eb2017-10-10 17:19:30 +0800173 .pt_type = cpt_type, \
Zhi Wang2707e442016-03-28 23:23:16 +0800174 .next_pt_type = npt_type, \
175 .pse_entry_type = pse_type, \
176 }
177
178static struct gtt_type_table_entry gtt_type_table[] = {
179 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
180 GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800181 GTT_TYPE_INVALID,
Zhi Wang2707e442016-03-28 23:23:16 +0800182 GTT_TYPE_PPGTT_PML4_PT,
183 GTT_TYPE_INVALID),
184 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_PT,
185 GTT_TYPE_PPGTT_PML4_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800186 GTT_TYPE_PPGTT_PML4_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800187 GTT_TYPE_PPGTT_PDP_PT,
188 GTT_TYPE_INVALID),
189 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_ENTRY,
190 GTT_TYPE_PPGTT_PML4_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800191 GTT_TYPE_PPGTT_PML4_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800192 GTT_TYPE_PPGTT_PDP_PT,
193 GTT_TYPE_INVALID),
194 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_PT,
195 GTT_TYPE_PPGTT_PDP_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800196 GTT_TYPE_PPGTT_PDP_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800197 GTT_TYPE_PPGTT_PDE_PT,
198 GTT_TYPE_PPGTT_PTE_1G_ENTRY),
199 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
200 GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800201 GTT_TYPE_INVALID,
Zhi Wang2707e442016-03-28 23:23:16 +0800202 GTT_TYPE_PPGTT_PDE_PT,
203 GTT_TYPE_PPGTT_PTE_1G_ENTRY),
204 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_ENTRY,
205 GTT_TYPE_PPGTT_PDP_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800206 GTT_TYPE_PPGTT_PDP_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800207 GTT_TYPE_PPGTT_PDE_PT,
208 GTT_TYPE_PPGTT_PTE_1G_ENTRY),
209 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_PT,
210 GTT_TYPE_PPGTT_PDE_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800211 GTT_TYPE_PPGTT_PDE_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800212 GTT_TYPE_PPGTT_PTE_PT,
213 GTT_TYPE_PPGTT_PTE_2M_ENTRY),
214 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_ENTRY,
215 GTT_TYPE_PPGTT_PDE_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800216 GTT_TYPE_PPGTT_PDE_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800217 GTT_TYPE_PPGTT_PTE_PT,
218 GTT_TYPE_PPGTT_PTE_2M_ENTRY),
219 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_PT,
220 GTT_TYPE_PPGTT_PTE_4K_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800221 GTT_TYPE_PPGTT_PTE_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800222 GTT_TYPE_INVALID,
223 GTT_TYPE_INVALID),
224 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_4K_ENTRY,
225 GTT_TYPE_PPGTT_PTE_4K_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800226 GTT_TYPE_PPGTT_PTE_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800227 GTT_TYPE_INVALID,
228 GTT_TYPE_INVALID),
229 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_2M_ENTRY,
230 GTT_TYPE_PPGTT_PDE_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800231 GTT_TYPE_PPGTT_PDE_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800232 GTT_TYPE_INVALID,
233 GTT_TYPE_PPGTT_PTE_2M_ENTRY),
234 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_1G_ENTRY,
235 GTT_TYPE_PPGTT_PDP_ENTRY,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800236 GTT_TYPE_PPGTT_PDP_PT,
Zhi Wang2707e442016-03-28 23:23:16 +0800237 GTT_TYPE_INVALID,
238 GTT_TYPE_PPGTT_PTE_1G_ENTRY),
239 GTT_TYPE_TABLE_ENTRY(GTT_TYPE_GGTT_PTE,
240 GTT_TYPE_GGTT_PTE,
241 GTT_TYPE_INVALID,
Zhi Wang054f4eb2017-10-10 17:19:30 +0800242 GTT_TYPE_INVALID,
Zhi Wang2707e442016-03-28 23:23:16 +0800243 GTT_TYPE_INVALID),
244};
245
246static inline int get_next_pt_type(int type)
247{
248 return gtt_type_table[type].next_pt_type;
249}
250
Zhi Wang054f4eb2017-10-10 17:19:30 +0800251static inline int get_pt_type(int type)
252{
253 return gtt_type_table[type].pt_type;
254}
255
Zhi Wang2707e442016-03-28 23:23:16 +0800256static inline int get_entry_type(int type)
257{
258 return gtt_type_table[type].entry_type;
259}
260
261static inline int get_pse_type(int type)
262{
263 return gtt_type_table[type].pse_entry_type;
264}
265
266static u64 read_pte64(struct drm_i915_private *dev_priv, unsigned long index)
267{
Du, Changbin321927d2016-10-20 14:08:46 +0800268 void __iomem *addr = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm + index;
Zhi Wang2707e442016-03-28 23:23:16 +0800269
Changbin Du905a5032016-12-30 14:10:53 +0800270 return readq(addr);
Zhi Wang2707e442016-03-28 23:23:16 +0800271}
272
Changbin Dua143cef2018-01-30 19:19:45 +0800273static void ggtt_invalidate(struct drm_i915_private *dev_priv)
Chuanxiao Dongaf2c6392017-06-02 15:34:24 +0800274{
275 mmio_hw_access_pre(dev_priv);
276 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
277 mmio_hw_access_post(dev_priv);
278}
279
Zhi Wang2707e442016-03-28 23:23:16 +0800280static void write_pte64(struct drm_i915_private *dev_priv,
281 unsigned long index, u64 pte)
282{
Du, Changbin321927d2016-10-20 14:08:46 +0800283 void __iomem *addr = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm + index;
Zhi Wang2707e442016-03-28 23:23:16 +0800284
Zhi Wang2707e442016-03-28 23:23:16 +0800285 writeq(pte, addr);
Zhi Wang2707e442016-03-28 23:23:16 +0800286}
287
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800288static inline int gtt_get_entry64(void *pt,
Zhi Wang2707e442016-03-28 23:23:16 +0800289 struct intel_gvt_gtt_entry *e,
290 unsigned long index, bool hypervisor_access, unsigned long gpa,
291 struct intel_vgpu *vgpu)
292{
293 const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
294 int ret;
295
296 if (WARN_ON(info->gtt_entry_size != 8))
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800297 return -EINVAL;
Zhi Wang2707e442016-03-28 23:23:16 +0800298
299 if (hypervisor_access) {
300 ret = intel_gvt_hypervisor_read_gpa(vgpu, gpa +
301 (index << info->gtt_entry_size_shift),
302 &e->val64, 8);
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800303 if (WARN_ON(ret))
304 return ret;
Zhi Wang2707e442016-03-28 23:23:16 +0800305 } else if (!pt) {
306 e->val64 = read_pte64(vgpu->gvt->dev_priv, index);
307 } else {
308 e->val64 = *((u64 *)pt + index);
309 }
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800310 return 0;
Zhi Wang2707e442016-03-28 23:23:16 +0800311}
312
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800313static inline int gtt_set_entry64(void *pt,
Zhi Wang2707e442016-03-28 23:23:16 +0800314 struct intel_gvt_gtt_entry *e,
315 unsigned long index, bool hypervisor_access, unsigned long gpa,
316 struct intel_vgpu *vgpu)
317{
318 const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
319 int ret;
320
321 if (WARN_ON(info->gtt_entry_size != 8))
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800322 return -EINVAL;
Zhi Wang2707e442016-03-28 23:23:16 +0800323
324 if (hypervisor_access) {
325 ret = intel_gvt_hypervisor_write_gpa(vgpu, gpa +
326 (index << info->gtt_entry_size_shift),
327 &e->val64, 8);
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800328 if (WARN_ON(ret))
329 return ret;
Zhi Wang2707e442016-03-28 23:23:16 +0800330 } else if (!pt) {
331 write_pte64(vgpu->gvt->dev_priv, index, e->val64);
332 } else {
333 *((u64 *)pt + index) = e->val64;
334 }
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800335 return 0;
Zhi Wang2707e442016-03-28 23:23:16 +0800336}
337
338#define GTT_HAW 46
339
Changbin Du420fba72018-01-30 19:19:55 +0800340#define ADDR_1G_MASK GENMASK_ULL(GTT_HAW - 1, 30)
341#define ADDR_2M_MASK GENMASK_ULL(GTT_HAW - 1, 21)
342#define ADDR_4K_MASK GENMASK_ULL(GTT_HAW - 1, 12)
Zhi Wang2707e442016-03-28 23:23:16 +0800343
344static unsigned long gen8_gtt_get_pfn(struct intel_gvt_gtt_entry *e)
345{
346 unsigned long pfn;
347
348 if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY)
Changbin Dud861ca22018-01-30 19:19:47 +0800349 pfn = (e->val64 & ADDR_1G_MASK) >> PAGE_SHIFT;
Zhi Wang2707e442016-03-28 23:23:16 +0800350 else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY)
Changbin Dud861ca22018-01-30 19:19:47 +0800351 pfn = (e->val64 & ADDR_2M_MASK) >> PAGE_SHIFT;
Zhi Wang2707e442016-03-28 23:23:16 +0800352 else
Changbin Dud861ca22018-01-30 19:19:47 +0800353 pfn = (e->val64 & ADDR_4K_MASK) >> PAGE_SHIFT;
Zhi Wang2707e442016-03-28 23:23:16 +0800354 return pfn;
355}
356
357static void gen8_gtt_set_pfn(struct intel_gvt_gtt_entry *e, unsigned long pfn)
358{
359 if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY) {
360 e->val64 &= ~ADDR_1G_MASK;
Changbin Dud861ca22018-01-30 19:19:47 +0800361 pfn &= (ADDR_1G_MASK >> PAGE_SHIFT);
Zhi Wang2707e442016-03-28 23:23:16 +0800362 } else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY) {
363 e->val64 &= ~ADDR_2M_MASK;
Changbin Dud861ca22018-01-30 19:19:47 +0800364 pfn &= (ADDR_2M_MASK >> PAGE_SHIFT);
Zhi Wang2707e442016-03-28 23:23:16 +0800365 } else {
366 e->val64 &= ~ADDR_4K_MASK;
Changbin Dud861ca22018-01-30 19:19:47 +0800367 pfn &= (ADDR_4K_MASK >> PAGE_SHIFT);
Zhi Wang2707e442016-03-28 23:23:16 +0800368 }
369
Changbin Dud861ca22018-01-30 19:19:47 +0800370 e->val64 |= (pfn << PAGE_SHIFT);
Zhi Wang2707e442016-03-28 23:23:16 +0800371}
372
373static bool gen8_gtt_test_pse(struct intel_gvt_gtt_entry *e)
374{
375 /* Entry doesn't have PSE bit. */
376 if (get_pse_type(e->type) == GTT_TYPE_INVALID)
377 return false;
378
379 e->type = get_entry_type(e->type);
Changbin Dud861ca22018-01-30 19:19:47 +0800380 if (!(e->val64 & _PAGE_PSE))
Zhi Wang2707e442016-03-28 23:23:16 +0800381 return false;
382
383 e->type = get_pse_type(e->type);
384 return true;
385}
386
387static bool gen8_gtt_test_present(struct intel_gvt_gtt_entry *e)
388{
389 /*
390 * i915 writes PDP root pointer registers without present bit,
391 * it also works, so we need to treat root pointer entry
392 * specifically.
393 */
394 if (e->type == GTT_TYPE_PPGTT_ROOT_L3_ENTRY
395 || e->type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY)
396 return (e->val64 != 0);
397 else
Changbin Dud861ca22018-01-30 19:19:47 +0800398 return (e->val64 & _PAGE_PRESENT);
Zhi Wang2707e442016-03-28 23:23:16 +0800399}
400
401static void gtt_entry_clear_present(struct intel_gvt_gtt_entry *e)
402{
Changbin Dud861ca22018-01-30 19:19:47 +0800403 e->val64 &= ~_PAGE_PRESENT;
Zhi Wang2707e442016-03-28 23:23:16 +0800404}
405
Zhi Wang655c64e2017-10-10 17:24:26 +0800406static void gtt_entry_set_present(struct intel_gvt_gtt_entry *e)
407{
Changbin Dud861ca22018-01-30 19:19:47 +0800408 e->val64 |= _PAGE_PRESENT;
Zhi Wang2707e442016-03-28 23:23:16 +0800409}
410
411/*
412 * Per-platform GMA routines.
413 */
414static unsigned long gma_to_ggtt_pte_index(unsigned long gma)
415{
Zhi Wang9556e112017-10-10 13:51:32 +0800416 unsigned long x = (gma >> I915_GTT_PAGE_SHIFT);
Zhi Wang2707e442016-03-28 23:23:16 +0800417
418 trace_gma_index(__func__, gma, x);
419 return x;
420}
421
422#define DEFINE_PPGTT_GMA_TO_INDEX(prefix, ename, exp) \
423static unsigned long prefix##_gma_to_##ename##_index(unsigned long gma) \
424{ \
425 unsigned long x = (exp); \
426 trace_gma_index(__func__, gma, x); \
427 return x; \
428}
429
430DEFINE_PPGTT_GMA_TO_INDEX(gen8, pte, (gma >> 12 & 0x1ff));
431DEFINE_PPGTT_GMA_TO_INDEX(gen8, pde, (gma >> 21 & 0x1ff));
432DEFINE_PPGTT_GMA_TO_INDEX(gen8, l3_pdp, (gma >> 30 & 0x3));
433DEFINE_PPGTT_GMA_TO_INDEX(gen8, l4_pdp, (gma >> 30 & 0x1ff));
434DEFINE_PPGTT_GMA_TO_INDEX(gen8, pml4, (gma >> 39 & 0x1ff));
435
436static struct intel_gvt_gtt_pte_ops gen8_gtt_pte_ops = {
437 .get_entry = gtt_get_entry64,
438 .set_entry = gtt_set_entry64,
439 .clear_present = gtt_entry_clear_present,
Zhi Wang655c64e2017-10-10 17:24:26 +0800440 .set_present = gtt_entry_set_present,
Zhi Wang2707e442016-03-28 23:23:16 +0800441 .test_present = gen8_gtt_test_present,
442 .test_pse = gen8_gtt_test_pse,
443 .get_pfn = gen8_gtt_get_pfn,
444 .set_pfn = gen8_gtt_set_pfn,
445};
446
447static struct intel_gvt_gtt_gma_ops gen8_gtt_gma_ops = {
448 .gma_to_ggtt_pte_index = gma_to_ggtt_pte_index,
449 .gma_to_pte_index = gen8_gma_to_pte_index,
450 .gma_to_pde_index = gen8_gma_to_pde_index,
451 .gma_to_l3_pdp_index = gen8_gma_to_l3_pdp_index,
452 .gma_to_l4_pdp_index = gen8_gma_to_l4_pdp_index,
453 .gma_to_pml4_index = gen8_gma_to_pml4_index,
454};
455
Zhi Wang2707e442016-03-28 23:23:16 +0800456/*
457 * MM helpers.
458 */
Changbin Du3aff3512018-01-30 19:19:42 +0800459static void _ppgtt_get_root_entry(struct intel_vgpu_mm *mm,
460 struct intel_gvt_gtt_entry *entry, unsigned long index,
461 bool guest)
Zhi Wang2707e442016-03-28 23:23:16 +0800462{
Changbin Du3aff3512018-01-30 19:19:42 +0800463 struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
Zhi Wang2707e442016-03-28 23:23:16 +0800464
Changbin Du3aff3512018-01-30 19:19:42 +0800465 GEM_BUG_ON(mm->type != INTEL_GVT_MM_PPGTT);
Zhi Wang2707e442016-03-28 23:23:16 +0800466
Changbin Du3aff3512018-01-30 19:19:42 +0800467 entry->type = mm->ppgtt_mm.root_entry_type;
468 pte_ops->get_entry(guest ? mm->ppgtt_mm.guest_pdps :
469 mm->ppgtt_mm.shadow_pdps,
470 entry, index, false, 0, mm->vgpu);
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800471
Changbin Du3aff3512018-01-30 19:19:42 +0800472 pte_ops->test_pse(entry);
Zhi Wang2707e442016-03-28 23:23:16 +0800473}
474
Changbin Du3aff3512018-01-30 19:19:42 +0800475static inline void ppgtt_get_guest_root_entry(struct intel_vgpu_mm *mm,
476 struct intel_gvt_gtt_entry *entry, unsigned long index)
Zhi Wang2707e442016-03-28 23:23:16 +0800477{
Changbin Du3aff3512018-01-30 19:19:42 +0800478 _ppgtt_get_root_entry(mm, entry, index, true);
479}
Zhi Wang2707e442016-03-28 23:23:16 +0800480
Changbin Du3aff3512018-01-30 19:19:42 +0800481static inline void ppgtt_get_shadow_root_entry(struct intel_vgpu_mm *mm,
482 struct intel_gvt_gtt_entry *entry, unsigned long index)
483{
484 _ppgtt_get_root_entry(mm, entry, index, false);
485}
486
487static void _ppgtt_set_root_entry(struct intel_vgpu_mm *mm,
488 struct intel_gvt_gtt_entry *entry, unsigned long index,
489 bool guest)
490{
491 struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
492
493 pte_ops->set_entry(guest ? mm->ppgtt_mm.guest_pdps :
494 mm->ppgtt_mm.shadow_pdps,
495 entry, index, false, 0, mm->vgpu);
496}
497
498static inline void ppgtt_set_guest_root_entry(struct intel_vgpu_mm *mm,
499 struct intel_gvt_gtt_entry *entry, unsigned long index)
500{
501 _ppgtt_set_root_entry(mm, entry, index, true);
502}
503
504static inline void ppgtt_set_shadow_root_entry(struct intel_vgpu_mm *mm,
505 struct intel_gvt_gtt_entry *entry, unsigned long index)
506{
507 _ppgtt_set_root_entry(mm, entry, index, false);
508}
509
510static void ggtt_get_guest_entry(struct intel_vgpu_mm *mm,
511 struct intel_gvt_gtt_entry *entry, unsigned long index)
512{
513 struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
514
515 GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
516
517 entry->type = GTT_TYPE_GGTT_PTE;
518 pte_ops->get_entry(mm->ggtt_mm.virtual_ggtt, entry, index,
519 false, 0, mm->vgpu);
520}
521
522static void ggtt_set_guest_entry(struct intel_vgpu_mm *mm,
523 struct intel_gvt_gtt_entry *entry, unsigned long index)
524{
525 struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
526
527 GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
528
529 pte_ops->set_entry(mm->ggtt_mm.virtual_ggtt, entry, index,
530 false, 0, mm->vgpu);
531}
532
Changbin Du7598e872018-03-27 15:35:14 +0800533static void ggtt_get_host_entry(struct intel_vgpu_mm *mm,
534 struct intel_gvt_gtt_entry *entry, unsigned long index)
535{
536 struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
537
538 GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
539
540 pte_ops->get_entry(NULL, entry, index, false, 0, mm->vgpu);
541}
542
Changbin Du3aff3512018-01-30 19:19:42 +0800543static void ggtt_set_host_entry(struct intel_vgpu_mm *mm,
544 struct intel_gvt_gtt_entry *entry, unsigned long index)
545{
546 struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
547
548 GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
549
550 pte_ops->set_entry(NULL, entry, index, false, 0, mm->vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +0800551}
552
553/*
554 * PPGTT shadow page table helpers.
555 */
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800556static inline int ppgtt_spt_get_entry(
Zhi Wang2707e442016-03-28 23:23:16 +0800557 struct intel_vgpu_ppgtt_spt *spt,
558 void *page_table, int type,
559 struct intel_gvt_gtt_entry *e, unsigned long index,
560 bool guest)
561{
562 struct intel_gvt *gvt = spt->vgpu->gvt;
563 struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800564 int ret;
Zhi Wang2707e442016-03-28 23:23:16 +0800565
566 e->type = get_entry_type(type);
567
568 if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n"))
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800569 return -EINVAL;
Zhi Wang2707e442016-03-28 23:23:16 +0800570
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800571 ret = ops->get_entry(page_table, e, index, guest,
Changbin Due502a2a2018-01-30 19:19:53 +0800572 spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
Zhi Wang2707e442016-03-28 23:23:16 +0800573 spt->vgpu);
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800574 if (ret)
575 return ret;
576
Zhi Wang2707e442016-03-28 23:23:16 +0800577 ops->test_pse(e);
Changbin Dubc37ab52018-01-30 19:19:44 +0800578
579 gvt_vdbg_mm("read ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n",
580 type, e->type, index, e->val64);
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800581 return 0;
Zhi Wang2707e442016-03-28 23:23:16 +0800582}
583
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800584static inline int ppgtt_spt_set_entry(
Zhi Wang2707e442016-03-28 23:23:16 +0800585 struct intel_vgpu_ppgtt_spt *spt,
586 void *page_table, int type,
587 struct intel_gvt_gtt_entry *e, unsigned long index,
588 bool guest)
589{
590 struct intel_gvt *gvt = spt->vgpu->gvt;
591 struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
592
593 if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n"))
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800594 return -EINVAL;
Zhi Wang2707e442016-03-28 23:23:16 +0800595
Changbin Dubc37ab52018-01-30 19:19:44 +0800596 gvt_vdbg_mm("set ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n",
597 type, e->type, index, e->val64);
598
Zhi Wang2707e442016-03-28 23:23:16 +0800599 return ops->set_entry(page_table, e, index, guest,
Changbin Due502a2a2018-01-30 19:19:53 +0800600 spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
Zhi Wang2707e442016-03-28 23:23:16 +0800601 spt->vgpu);
602}
603
604#define ppgtt_get_guest_entry(spt, e, index) \
605 ppgtt_spt_get_entry(spt, NULL, \
Changbin Du44b46732018-01-30 19:19:49 +0800606 spt->guest_page.type, e, index, true)
Zhi Wang2707e442016-03-28 23:23:16 +0800607
608#define ppgtt_set_guest_entry(spt, e, index) \
609 ppgtt_spt_set_entry(spt, NULL, \
Changbin Du44b46732018-01-30 19:19:49 +0800610 spt->guest_page.type, e, index, true)
Zhi Wang2707e442016-03-28 23:23:16 +0800611
612#define ppgtt_get_shadow_entry(spt, e, index) \
613 ppgtt_spt_get_entry(spt, spt->shadow_page.vaddr, \
614 spt->shadow_page.type, e, index, false)
615
616#define ppgtt_set_shadow_entry(spt, e, index) \
617 ppgtt_spt_set_entry(spt, spt->shadow_page.vaddr, \
618 spt->shadow_page.type, e, index, false)
619
Changbin Du44b46732018-01-30 19:19:49 +0800620static void *alloc_spt(gfp_t gfp_mask)
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800621{
Changbin Du44b46732018-01-30 19:19:49 +0800622 struct intel_vgpu_ppgtt_spt *spt;
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800623
Changbin Du44b46732018-01-30 19:19:49 +0800624 spt = kzalloc(sizeof(*spt), gfp_mask);
625 if (!spt)
626 return NULL;
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800627
Changbin Du44b46732018-01-30 19:19:49 +0800628 spt->shadow_page.page = alloc_page(gfp_mask);
629 if (!spt->shadow_page.page) {
630 kfree(spt);
631 return NULL;
632 }
633 return spt;
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800634}
635
Changbin Du44b46732018-01-30 19:19:49 +0800636static void free_spt(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800637{
Changbin Du44b46732018-01-30 19:19:49 +0800638 __free_page(spt->shadow_page.page);
639 kfree(spt);
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800640}
641
Zhi Wang2707e442016-03-28 23:23:16 +0800642static int detach_oos_page(struct intel_vgpu *vgpu,
643 struct intel_vgpu_oos_page *oos_page);
644
Changbin Dud87f5ff2018-01-30 19:19:50 +0800645static void ppgtt_free_spt(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +0800646{
Changbin Du44b46732018-01-30 19:19:49 +0800647 struct device *kdev = &spt->vgpu->gvt->dev_priv->drm.pdev->dev;
Zhi Wang2707e442016-03-28 23:23:16 +0800648
Changbin Du44b46732018-01-30 19:19:49 +0800649 trace_spt_free(spt->vgpu->id, spt, spt->guest_page.type);
650
651 dma_unmap_page(kdev, spt->shadow_page.mfn << I915_GTT_PAGE_SHIFT, 4096,
652 PCI_DMA_BIDIRECTIONAL);
Changbin Dub6c126a2018-01-30 19:19:54 +0800653
654 radix_tree_delete(&spt->vgpu->gtt.spt_tree, spt->shadow_page.mfn);
Changbin Du44b46732018-01-30 19:19:49 +0800655
656 if (spt->guest_page.oos_page)
657 detach_oos_page(spt->vgpu, spt->guest_page.oos_page);
658
Changbin Due502a2a2018-01-30 19:19:53 +0800659 intel_vgpu_unregister_page_track(spt->vgpu, spt->guest_page.gfn);
Changbin Du44b46732018-01-30 19:19:49 +0800660
Zhi Wang2707e442016-03-28 23:23:16 +0800661 list_del_init(&spt->post_shadow_list);
Zhi Wang2707e442016-03-28 23:23:16 +0800662 free_spt(spt);
663}
664
Changbin Dud87f5ff2018-01-30 19:19:50 +0800665static void ppgtt_free_all_spt(struct intel_vgpu *vgpu)
Zhi Wang2707e442016-03-28 23:23:16 +0800666{
Changbin Du44b46732018-01-30 19:19:49 +0800667 struct intel_vgpu_ppgtt_spt *spt;
Changbin Dub6c126a2018-01-30 19:19:54 +0800668 struct radix_tree_iter iter;
669 void **slot;
Zhi Wang2707e442016-03-28 23:23:16 +0800670
Changbin Dub6c126a2018-01-30 19:19:54 +0800671 radix_tree_for_each_slot(slot, &vgpu->gtt.spt_tree, &iter, 0) {
672 spt = radix_tree_deref_slot(slot);
Changbin Dud87f5ff2018-01-30 19:19:50 +0800673 ppgtt_free_spt(spt);
Changbin Dub6c126a2018-01-30 19:19:54 +0800674 }
Zhi Wang2707e442016-03-28 23:23:16 +0800675}
676
Zhi Wang7d1e5cd2017-09-29 02:47:55 +0800677static int ppgtt_handle_guest_write_page_table_bytes(
Changbin Du44b46732018-01-30 19:19:49 +0800678 struct intel_vgpu_ppgtt_spt *spt,
Zhi Wang2707e442016-03-28 23:23:16 +0800679 u64 pa, void *p_data, int bytes);
680
Changbin Due502a2a2018-01-30 19:19:53 +0800681static int ppgtt_write_protection_handler(
682 struct intel_vgpu_page_track *page_track,
683 u64 gpa, void *data, int bytes)
Zhi Wang2707e442016-03-28 23:23:16 +0800684{
Changbin Due502a2a2018-01-30 19:19:53 +0800685 struct intel_vgpu_ppgtt_spt *spt = page_track->priv_data;
686
Zhi Wang2707e442016-03-28 23:23:16 +0800687 int ret;
688
689 if (bytes != 4 && bytes != 8)
690 return -EINVAL;
691
Changbin Due502a2a2018-01-30 19:19:53 +0800692 ret = ppgtt_handle_guest_write_page_table_bytes(spt, gpa, data, bytes);
Zhi Wang2707e442016-03-28 23:23:16 +0800693 if (ret)
694 return ret;
695 return ret;
696}
697
Changbin Du44b46732018-01-30 19:19:49 +0800698/* Find a spt by guest gfn. */
699static struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_gfn(
700 struct intel_vgpu *vgpu, unsigned long gfn)
701{
702 struct intel_vgpu_page_track *track;
703
Changbin Due502a2a2018-01-30 19:19:53 +0800704 track = intel_vgpu_find_page_track(vgpu, gfn);
705 if (track && track->handler == ppgtt_write_protection_handler)
706 return track->priv_data;
Changbin Du44b46732018-01-30 19:19:49 +0800707
708 return NULL;
709}
710
711/* Find the spt by shadow page mfn. */
Changbin Dub6c126a2018-01-30 19:19:54 +0800712static inline struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_mfn(
Changbin Du44b46732018-01-30 19:19:49 +0800713 struct intel_vgpu *vgpu, unsigned long mfn)
714{
Changbin Dub6c126a2018-01-30 19:19:54 +0800715 return radix_tree_lookup(&vgpu->gtt.spt_tree, mfn);
Changbin Du44b46732018-01-30 19:19:49 +0800716}
717
Changbin Duede9d0c2018-01-30 19:19:40 +0800718static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800719
Changbin Dud87f5ff2018-01-30 19:19:50 +0800720static struct intel_vgpu_ppgtt_spt *ppgtt_alloc_spt(
Zhi Wang2707e442016-03-28 23:23:16 +0800721 struct intel_vgpu *vgpu, int type, unsigned long gfn)
722{
Changbin Du44b46732018-01-30 19:19:49 +0800723 struct device *kdev = &vgpu->gvt->dev_priv->drm.pdev->dev;
Zhi Wang2707e442016-03-28 23:23:16 +0800724 struct intel_vgpu_ppgtt_spt *spt = NULL;
Changbin Du44b46732018-01-30 19:19:49 +0800725 dma_addr_t daddr;
Changbin Due502a2a2018-01-30 19:19:53 +0800726 int ret;
Zhi Wang2707e442016-03-28 23:23:16 +0800727
728retry:
729 spt = alloc_spt(GFP_KERNEL | __GFP_ZERO);
730 if (!spt) {
Changbin Duede9d0c2018-01-30 19:19:40 +0800731 if (reclaim_one_ppgtt_mm(vgpu->gvt))
Zhi Wang2707e442016-03-28 23:23:16 +0800732 goto retry;
733
Tina Zhang695fbc02017-03-10 04:26:53 -0500734 gvt_vgpu_err("fail to allocate ppgtt shadow page\n");
Zhi Wang2707e442016-03-28 23:23:16 +0800735 return ERR_PTR(-ENOMEM);
736 }
737
738 spt->vgpu = vgpu;
Zhi Wang2707e442016-03-28 23:23:16 +0800739 atomic_set(&spt->refcount, 1);
740 INIT_LIST_HEAD(&spt->post_shadow_list);
741
742 /*
Changbin Du44b46732018-01-30 19:19:49 +0800743 * Init shadow_page.
Zhi Wang2707e442016-03-28 23:23:16 +0800744 */
Changbin Du44b46732018-01-30 19:19:49 +0800745 spt->shadow_page.type = type;
746 daddr = dma_map_page(kdev, spt->shadow_page.page,
747 0, 4096, PCI_DMA_BIDIRECTIONAL);
748 if (dma_mapping_error(kdev, daddr)) {
749 gvt_vgpu_err("fail to map dma addr\n");
Changbin Dub6c126a2018-01-30 19:19:54 +0800750 ret = -EINVAL;
751 goto err_free_spt;
Zhi Wang2707e442016-03-28 23:23:16 +0800752 }
Changbin Du44b46732018-01-30 19:19:49 +0800753 spt->shadow_page.vaddr = page_address(spt->shadow_page.page);
754 spt->shadow_page.mfn = daddr >> I915_GTT_PAGE_SHIFT;
Zhi Wang2707e442016-03-28 23:23:16 +0800755
Changbin Du44b46732018-01-30 19:19:49 +0800756 /*
757 * Init guest_page.
758 */
759 spt->guest_page.type = type;
760 spt->guest_page.gfn = gfn;
761
Changbin Due502a2a2018-01-30 19:19:53 +0800762 ret = intel_vgpu_register_page_track(vgpu, spt->guest_page.gfn,
763 ppgtt_write_protection_handler, spt);
Changbin Dub6c126a2018-01-30 19:19:54 +0800764 if (ret)
765 goto err_unmap_dma;
Changbin Du44b46732018-01-30 19:19:49 +0800766
Changbin Dub6c126a2018-01-30 19:19:54 +0800767 ret = radix_tree_insert(&vgpu->gtt.spt_tree, spt->shadow_page.mfn, spt);
768 if (ret)
769 goto err_unreg_page_track;
Zhi Wang2707e442016-03-28 23:23:16 +0800770
771 trace_spt_alloc(vgpu->id, spt, type, spt->shadow_page.mfn, gfn);
772 return spt;
Changbin Dub6c126a2018-01-30 19:19:54 +0800773
774err_unreg_page_track:
775 intel_vgpu_unregister_page_track(vgpu, spt->guest_page.gfn);
776err_unmap_dma:
777 dma_unmap_page(kdev, daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
778err_free_spt:
779 free_spt(spt);
780 return ERR_PTR(ret);
Zhi Wang2707e442016-03-28 23:23:16 +0800781}
782
783#define pt_entry_size_shift(spt) \
784 ((spt)->vgpu->gvt->device_info.gtt_entry_size_shift)
785
786#define pt_entries(spt) \
Zhi Wang9556e112017-10-10 13:51:32 +0800787 (I915_GTT_PAGE_SIZE >> pt_entry_size_shift(spt))
Zhi Wang2707e442016-03-28 23:23:16 +0800788
789#define for_each_present_guest_entry(spt, e, i) \
790 for (i = 0; i < pt_entries(spt); i++) \
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800791 if (!ppgtt_get_guest_entry(spt, e, i) && \
792 spt->vgpu->gvt->gtt.pte_ops->test_present(e))
Zhi Wang2707e442016-03-28 23:23:16 +0800793
794#define for_each_present_shadow_entry(spt, e, i) \
795 for (i = 0; i < pt_entries(spt); i++) \
Changbin Du4b2dbbc2017-08-02 15:06:37 +0800796 if (!ppgtt_get_shadow_entry(spt, e, i) && \
797 spt->vgpu->gvt->gtt.pte_ops->test_present(e))
Zhi Wang2707e442016-03-28 23:23:16 +0800798
Changbin Dud87f5ff2018-01-30 19:19:50 +0800799static void ppgtt_get_spt(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +0800800{
801 int v = atomic_read(&spt->refcount);
802
803 trace_spt_refcount(spt->vgpu->id, "inc", spt, v, (v + 1));
804
805 atomic_inc(&spt->refcount);
806}
807
Changbin Dud87f5ff2018-01-30 19:19:50 +0800808static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt);
Zhi Wang2707e442016-03-28 23:23:16 +0800809
Changbin Dud87f5ff2018-01-30 19:19:50 +0800810static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu,
Zhi Wang2707e442016-03-28 23:23:16 +0800811 struct intel_gvt_gtt_entry *e)
812{
813 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
814 struct intel_vgpu_ppgtt_spt *s;
Ping Gao3b6411c2016-11-04 13:47:35 +0800815 intel_gvt_gtt_type_t cur_pt_type;
Zhi Wang2707e442016-03-28 23:23:16 +0800816
Changbin Du72f03d72018-01-30 19:19:48 +0800817 GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(e->type)));
Zhi Wang2707e442016-03-28 23:23:16 +0800818
Ping Gao3b6411c2016-11-04 13:47:35 +0800819 if (e->type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY
820 && e->type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY) {
821 cur_pt_type = get_next_pt_type(e->type) + 1;
822 if (ops->get_pfn(e) ==
823 vgpu->gtt.scratch_pt[cur_pt_type].page_mfn)
824 return 0;
825 }
Changbin Du44b46732018-01-30 19:19:49 +0800826 s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e));
Zhi Wang2707e442016-03-28 23:23:16 +0800827 if (!s) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500828 gvt_vgpu_err("fail to find shadow page: mfn: 0x%lx\n",
829 ops->get_pfn(e));
Zhi Wang2707e442016-03-28 23:23:16 +0800830 return -ENXIO;
831 }
Changbin Dud87f5ff2018-01-30 19:19:50 +0800832 return ppgtt_invalidate_spt(s);
Zhi Wang2707e442016-03-28 23:23:16 +0800833}
834
Changbin Ducf4ee732018-03-01 15:49:59 +0800835static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt,
836 struct intel_gvt_gtt_entry *entry)
837{
838 struct intel_vgpu *vgpu = spt->vgpu;
839 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
840 unsigned long pfn;
841 int type;
842
843 pfn = ops->get_pfn(entry);
844 type = spt->shadow_page.type;
845
846 if (pfn == vgpu->gtt.scratch_pt[type].page_mfn)
847 return;
848
849 intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT);
850}
851
Changbin Dud87f5ff2018-01-30 19:19:50 +0800852static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +0800853{
Tina Zhang695fbc02017-03-10 04:26:53 -0500854 struct intel_vgpu *vgpu = spt->vgpu;
Zhi Wang2707e442016-03-28 23:23:16 +0800855 struct intel_gvt_gtt_entry e;
856 unsigned long index;
857 int ret;
858 int v = atomic_read(&spt->refcount);
859
860 trace_spt_change(spt->vgpu->id, "die", spt,
Changbin Du44b46732018-01-30 19:19:49 +0800861 spt->guest_page.gfn, spt->shadow_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +0800862
863 trace_spt_refcount(spt->vgpu->id, "dec", spt, v, (v - 1));
864
865 if (atomic_dec_return(&spt->refcount) > 0)
866 return 0;
867
Zhi Wang2707e442016-03-28 23:23:16 +0800868 for_each_present_shadow_entry(spt, &e, index) {
Changbin Du72f03d72018-01-30 19:19:48 +0800869 switch (e.type) {
870 case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
871 gvt_vdbg_mm("invalidate 4K entry\n");
Changbin Ducf4ee732018-03-01 15:49:59 +0800872 ppgtt_invalidate_pte(spt, &e);
873 break;
Changbin Du72f03d72018-01-30 19:19:48 +0800874 case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
875 case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
876 WARN(1, "GVT doesn't support 2M/1GB page\n");
877 continue;
878 case GTT_TYPE_PPGTT_PML4_ENTRY:
879 case GTT_TYPE_PPGTT_PDP_ENTRY:
880 case GTT_TYPE_PPGTT_PDE_ENTRY:
881 gvt_vdbg_mm("invalidate PMUL4/PDP/PDE entry\n");
Changbin Dud87f5ff2018-01-30 19:19:50 +0800882 ret = ppgtt_invalidate_spt_by_shadow_entry(
Changbin Du72f03d72018-01-30 19:19:48 +0800883 spt->vgpu, &e);
884 if (ret)
885 goto fail;
886 break;
887 default:
888 GEM_BUG_ON(1);
Zhi Wang2707e442016-03-28 23:23:16 +0800889 }
Zhi Wang2707e442016-03-28 23:23:16 +0800890 }
Changbin Ducf4ee732018-03-01 15:49:59 +0800891
Zhi Wang2707e442016-03-28 23:23:16 +0800892 trace_spt_change(spt->vgpu->id, "release", spt,
Changbin Du44b46732018-01-30 19:19:49 +0800893 spt->guest_page.gfn, spt->shadow_page.type);
Changbin Dud87f5ff2018-01-30 19:19:50 +0800894 ppgtt_free_spt(spt);
Zhi Wang2707e442016-03-28 23:23:16 +0800895 return 0;
896fail:
Tina Zhang695fbc02017-03-10 04:26:53 -0500897 gvt_vgpu_err("fail: shadow page %p shadow entry 0x%llx type %d\n",
898 spt, e.val64, e.type);
Zhi Wang2707e442016-03-28 23:23:16 +0800899 return ret;
900}
901
Changbin Dud87f5ff2018-01-30 19:19:50 +0800902static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt);
Zhi Wang2707e442016-03-28 23:23:16 +0800903
Changbin Dud87f5ff2018-01-30 19:19:50 +0800904static struct intel_vgpu_ppgtt_spt *ppgtt_populate_spt_by_guest_entry(
Zhi Wang2707e442016-03-28 23:23:16 +0800905 struct intel_vgpu *vgpu, struct intel_gvt_gtt_entry *we)
906{
907 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
Changbin Du44b46732018-01-30 19:19:49 +0800908 struct intel_vgpu_ppgtt_spt *spt = NULL;
Zhi Wang2707e442016-03-28 23:23:16 +0800909 int ret;
910
Changbin Du72f03d72018-01-30 19:19:48 +0800911 GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(we->type)));
Zhi Wang2707e442016-03-28 23:23:16 +0800912
Changbin Du44b46732018-01-30 19:19:49 +0800913 spt = intel_vgpu_find_spt_by_gfn(vgpu, ops->get_pfn(we));
914 if (spt)
Changbin Dud87f5ff2018-01-30 19:19:50 +0800915 ppgtt_get_spt(spt);
Changbin Du44b46732018-01-30 19:19:49 +0800916 else {
Zhi Wang2707e442016-03-28 23:23:16 +0800917 int type = get_next_pt_type(we->type);
918
Changbin Dud87f5ff2018-01-30 19:19:50 +0800919 spt = ppgtt_alloc_spt(vgpu, type, ops->get_pfn(we));
Changbin Du44b46732018-01-30 19:19:49 +0800920 if (IS_ERR(spt)) {
921 ret = PTR_ERR(spt);
Zhi Wang2707e442016-03-28 23:23:16 +0800922 goto fail;
923 }
924
Changbin Due502a2a2018-01-30 19:19:53 +0800925 ret = intel_vgpu_enable_page_track(vgpu, spt->guest_page.gfn);
Zhi Wang2707e442016-03-28 23:23:16 +0800926 if (ret)
927 goto fail;
928
Changbin Dud87f5ff2018-01-30 19:19:50 +0800929 ret = ppgtt_populate_spt(spt);
Zhi Wang2707e442016-03-28 23:23:16 +0800930 if (ret)
931 goto fail;
932
Changbin Du44b46732018-01-30 19:19:49 +0800933 trace_spt_change(vgpu->id, "new", spt, spt->guest_page.gfn,
934 spt->shadow_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +0800935 }
Changbin Du44b46732018-01-30 19:19:49 +0800936 return spt;
Zhi Wang2707e442016-03-28 23:23:16 +0800937fail:
Tina Zhang695fbc02017-03-10 04:26:53 -0500938 gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
Changbin Du44b46732018-01-30 19:19:49 +0800939 spt, we->val64, we->type);
Zhi Wang2707e442016-03-28 23:23:16 +0800940 return ERR_PTR(ret);
941}
942
943static inline void ppgtt_generate_shadow_entry(struct intel_gvt_gtt_entry *se,
944 struct intel_vgpu_ppgtt_spt *s, struct intel_gvt_gtt_entry *ge)
945{
946 struct intel_gvt_gtt_pte_ops *ops = s->vgpu->gvt->gtt.pte_ops;
947
948 se->type = ge->type;
949 se->val64 = ge->val64;
950
951 ops->set_pfn(se, s->shadow_page.mfn);
952}
953
Changbin Du72f03d72018-01-30 19:19:48 +0800954static int ppgtt_populate_shadow_entry(struct intel_vgpu *vgpu,
955 struct intel_vgpu_ppgtt_spt *spt, unsigned long index,
956 struct intel_gvt_gtt_entry *ge)
957{
958 struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
959 struct intel_gvt_gtt_entry se = *ge;
Changbin Ducf4ee732018-03-01 15:49:59 +0800960 unsigned long gfn;
961 dma_addr_t dma_addr;
962 int ret;
Changbin Du72f03d72018-01-30 19:19:48 +0800963
964 if (!pte_ops->test_present(ge))
965 return 0;
966
967 gfn = pte_ops->get_pfn(ge);
968
969 switch (ge->type) {
970 case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
971 gvt_vdbg_mm("shadow 4K gtt entry\n");
972 break;
973 case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
974 case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
975 gvt_vgpu_err("GVT doesn't support 2M/1GB entry\n");
976 return -EINVAL;
977 default:
978 GEM_BUG_ON(1);
979 };
980
981 /* direct shadow */
Changbin Ducf4ee732018-03-01 15:49:59 +0800982 ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn, &dma_addr);
983 if (ret)
Changbin Du72f03d72018-01-30 19:19:48 +0800984 return -ENXIO;
985
Changbin Ducf4ee732018-03-01 15:49:59 +0800986 pte_ops->set_pfn(&se, dma_addr >> PAGE_SHIFT);
Changbin Du72f03d72018-01-30 19:19:48 +0800987 ppgtt_set_shadow_entry(spt, &se, index);
988 return 0;
989}
990
Changbin Dud87f5ff2018-01-30 19:19:50 +0800991static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +0800992{
993 struct intel_vgpu *vgpu = spt->vgpu;
Hang Yuancc753fb2017-12-22 18:06:31 +0800994 struct intel_gvt *gvt = vgpu->gvt;
995 struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
Zhi Wang2707e442016-03-28 23:23:16 +0800996 struct intel_vgpu_ppgtt_spt *s;
997 struct intel_gvt_gtt_entry se, ge;
Hang Yuancc753fb2017-12-22 18:06:31 +0800998 unsigned long gfn, i;
Zhi Wang2707e442016-03-28 23:23:16 +0800999 int ret;
1000
1001 trace_spt_change(spt->vgpu->id, "born", spt,
Changbin Due502a2a2018-01-30 19:19:53 +08001002 spt->guest_page.gfn, spt->shadow_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001003
Zhi Wang2707e442016-03-28 23:23:16 +08001004 for_each_present_guest_entry(spt, &ge, i) {
Changbin Du72f03d72018-01-30 19:19:48 +08001005 if (gtt_type_is_pt(get_next_pt_type(ge.type))) {
Changbin Dud87f5ff2018-01-30 19:19:50 +08001006 s = ppgtt_populate_spt_by_guest_entry(vgpu, &ge);
Changbin Du72f03d72018-01-30 19:19:48 +08001007 if (IS_ERR(s)) {
1008 ret = PTR_ERR(s);
1009 goto fail;
1010 }
1011 ppgtt_get_shadow_entry(spt, &se, i);
1012 ppgtt_generate_shadow_entry(&se, s, &ge);
1013 ppgtt_set_shadow_entry(spt, &se, i);
1014 } else {
1015 gfn = ops->get_pfn(&ge);
1016 if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
1017 ops->set_pfn(&se, gvt->gtt.scratch_mfn);
1018 ppgtt_set_shadow_entry(spt, &se, i);
1019 continue;
1020 }
Zhi Wang2707e442016-03-28 23:23:16 +08001021
Changbin Du72f03d72018-01-30 19:19:48 +08001022 ret = ppgtt_populate_shadow_entry(vgpu, spt, i, &ge);
1023 if (ret)
1024 goto fail;
Zhi Wang2707e442016-03-28 23:23:16 +08001025 }
Zhi Wang2707e442016-03-28 23:23:16 +08001026 }
1027 return 0;
1028fail:
Tina Zhang695fbc02017-03-10 04:26:53 -05001029 gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
1030 spt, ge.val64, ge.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001031 return ret;
1032}
1033
Changbin Du44b46732018-01-30 19:19:49 +08001034static int ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt *spt,
Tina Zhang6b3816d2017-08-14 15:24:14 +08001035 struct intel_gvt_gtt_entry *se, unsigned long index)
Zhi Wang2707e442016-03-28 23:23:16 +08001036{
Zhi Wang2707e442016-03-28 23:23:16 +08001037 struct intel_vgpu *vgpu = spt->vgpu;
1038 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
Zhi Wang2707e442016-03-28 23:23:16 +08001039 int ret;
1040
Changbin Du44b46732018-01-30 19:19:49 +08001041 trace_spt_guest_change(spt->vgpu->id, "remove", spt,
1042 spt->shadow_page.type, se->val64, index);
Bing Niu9baf0922016-11-07 10:44:36 +08001043
Changbin Dubc37ab52018-01-30 19:19:44 +08001044 gvt_vdbg_mm("destroy old shadow entry, type %d, index %lu, value %llx\n",
1045 se->type, index, se->val64);
1046
Tina Zhang6b3816d2017-08-14 15:24:14 +08001047 if (!ops->test_present(se))
Zhi Wang2707e442016-03-28 23:23:16 +08001048 return 0;
1049
Changbin Du44b46732018-01-30 19:19:49 +08001050 if (ops->get_pfn(se) ==
1051 vgpu->gtt.scratch_pt[spt->shadow_page.type].page_mfn)
Zhi Wang2707e442016-03-28 23:23:16 +08001052 return 0;
1053
Tina Zhang6b3816d2017-08-14 15:24:14 +08001054 if (gtt_type_is_pt(get_next_pt_type(se->type))) {
Bing Niu9baf0922016-11-07 10:44:36 +08001055 struct intel_vgpu_ppgtt_spt *s =
Changbin Du44b46732018-01-30 19:19:49 +08001056 intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(se));
Bing Niu9baf0922016-11-07 10:44:36 +08001057 if (!s) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001058 gvt_vgpu_err("fail to find guest page\n");
Zhi Wang2707e442016-03-28 23:23:16 +08001059 ret = -ENXIO;
1060 goto fail;
1061 }
Changbin Dud87f5ff2018-01-30 19:19:50 +08001062 ret = ppgtt_invalidate_spt(s);
Zhi Wang2707e442016-03-28 23:23:16 +08001063 if (ret)
1064 goto fail;
Changbin Ducf4ee732018-03-01 15:49:59 +08001065 } else
1066 ppgtt_invalidate_pte(spt, se);
1067
Zhi Wang2707e442016-03-28 23:23:16 +08001068 return 0;
1069fail:
Tina Zhang695fbc02017-03-10 04:26:53 -05001070 gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
Tina Zhang6b3816d2017-08-14 15:24:14 +08001071 spt, se->val64, se->type);
Zhi Wang2707e442016-03-28 23:23:16 +08001072 return ret;
1073}
1074
Changbin Du44b46732018-01-30 19:19:49 +08001075static int ppgtt_handle_guest_entry_add(struct intel_vgpu_ppgtt_spt *spt,
Zhi Wang2707e442016-03-28 23:23:16 +08001076 struct intel_gvt_gtt_entry *we, unsigned long index)
1077{
Zhi Wang2707e442016-03-28 23:23:16 +08001078 struct intel_vgpu *vgpu = spt->vgpu;
1079 struct intel_gvt_gtt_entry m;
1080 struct intel_vgpu_ppgtt_spt *s;
1081 int ret;
1082
Changbin Du44b46732018-01-30 19:19:49 +08001083 trace_spt_guest_change(spt->vgpu->id, "add", spt, spt->shadow_page.type,
1084 we->val64, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001085
Changbin Dubc37ab52018-01-30 19:19:44 +08001086 gvt_vdbg_mm("add shadow entry: type %d, index %lu, value %llx\n",
1087 we->type, index, we->val64);
1088
Zhi Wang2707e442016-03-28 23:23:16 +08001089 if (gtt_type_is_pt(get_next_pt_type(we->type))) {
Changbin Dud87f5ff2018-01-30 19:19:50 +08001090 s = ppgtt_populate_spt_by_guest_entry(vgpu, we);
Zhi Wang2707e442016-03-28 23:23:16 +08001091 if (IS_ERR(s)) {
1092 ret = PTR_ERR(s);
1093 goto fail;
1094 }
1095 ppgtt_get_shadow_entry(spt, &m, index);
1096 ppgtt_generate_shadow_entry(&m, s, we);
1097 ppgtt_set_shadow_entry(spt, &m, index);
1098 } else {
Changbin Du72f03d72018-01-30 19:19:48 +08001099 ret = ppgtt_populate_shadow_entry(vgpu, spt, index, we);
Zhi Wang2707e442016-03-28 23:23:16 +08001100 if (ret)
1101 goto fail;
Zhi Wang2707e442016-03-28 23:23:16 +08001102 }
1103 return 0;
1104fail:
Tina Zhang695fbc02017-03-10 04:26:53 -05001105 gvt_vgpu_err("fail: spt %p guest entry 0x%llx type %d\n",
1106 spt, we->val64, we->type);
Zhi Wang2707e442016-03-28 23:23:16 +08001107 return ret;
1108}
1109
1110static int sync_oos_page(struct intel_vgpu *vgpu,
1111 struct intel_vgpu_oos_page *oos_page)
1112{
1113 const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
1114 struct intel_gvt *gvt = vgpu->gvt;
1115 struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
Changbin Du44b46732018-01-30 19:19:49 +08001116 struct intel_vgpu_ppgtt_spt *spt = oos_page->spt;
Changbin Du72f03d72018-01-30 19:19:48 +08001117 struct intel_gvt_gtt_entry old, new;
Zhi Wang2707e442016-03-28 23:23:16 +08001118 int index;
1119 int ret;
1120
1121 trace_oos_change(vgpu->id, "sync", oos_page->id,
Changbin Du44b46732018-01-30 19:19:49 +08001122 spt, spt->guest_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001123
Changbin Du44b46732018-01-30 19:19:49 +08001124 old.type = new.type = get_entry_type(spt->guest_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001125 old.val64 = new.val64 = 0;
1126
Zhi Wang9556e112017-10-10 13:51:32 +08001127 for (index = 0; index < (I915_GTT_PAGE_SIZE >>
1128 info->gtt_entry_size_shift); index++) {
Zhi Wang2707e442016-03-28 23:23:16 +08001129 ops->get_entry(oos_page->mem, &old, index, false, 0, vgpu);
1130 ops->get_entry(NULL, &new, index, true,
Changbin Du44b46732018-01-30 19:19:49 +08001131 spt->guest_page.gfn << PAGE_SHIFT, vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +08001132
1133 if (old.val64 == new.val64
1134 && !test_and_clear_bit(index, spt->post_shadow_bitmap))
1135 continue;
1136
1137 trace_oos_sync(vgpu->id, oos_page->id,
Changbin Du44b46732018-01-30 19:19:49 +08001138 spt, spt->guest_page.type,
Zhi Wang2707e442016-03-28 23:23:16 +08001139 new.val64, index);
1140
Changbin Du72f03d72018-01-30 19:19:48 +08001141 ret = ppgtt_populate_shadow_entry(vgpu, spt, index, &new);
Zhi Wang2707e442016-03-28 23:23:16 +08001142 if (ret)
1143 return ret;
1144
1145 ops->set_entry(oos_page->mem, &new, index, false, 0, vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +08001146 }
1147
Changbin Du44b46732018-01-30 19:19:49 +08001148 spt->guest_page.write_cnt = 0;
Zhi Wang2707e442016-03-28 23:23:16 +08001149 list_del_init(&spt->post_shadow_list);
1150 return 0;
1151}
1152
1153static int detach_oos_page(struct intel_vgpu *vgpu,
1154 struct intel_vgpu_oos_page *oos_page)
1155{
1156 struct intel_gvt *gvt = vgpu->gvt;
Changbin Du44b46732018-01-30 19:19:49 +08001157 struct intel_vgpu_ppgtt_spt *spt = oos_page->spt;
Zhi Wang2707e442016-03-28 23:23:16 +08001158
1159 trace_oos_change(vgpu->id, "detach", oos_page->id,
Changbin Du44b46732018-01-30 19:19:49 +08001160 spt, spt->guest_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001161
Changbin Du44b46732018-01-30 19:19:49 +08001162 spt->guest_page.write_cnt = 0;
1163 spt->guest_page.oos_page = NULL;
1164 oos_page->spt = NULL;
Zhi Wang2707e442016-03-28 23:23:16 +08001165
1166 list_del_init(&oos_page->vm_list);
1167 list_move_tail(&oos_page->list, &gvt->gtt.oos_page_free_list_head);
1168
1169 return 0;
1170}
1171
Changbin Du44b46732018-01-30 19:19:49 +08001172static int attach_oos_page(struct intel_vgpu_oos_page *oos_page,
1173 struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +08001174{
Changbin Du44b46732018-01-30 19:19:49 +08001175 struct intel_gvt *gvt = spt->vgpu->gvt;
Zhi Wang2707e442016-03-28 23:23:16 +08001176 int ret;
1177
Changbin Du44b46732018-01-30 19:19:49 +08001178 ret = intel_gvt_hypervisor_read_gpa(spt->vgpu,
1179 spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
Zhi Wang9556e112017-10-10 13:51:32 +08001180 oos_page->mem, I915_GTT_PAGE_SIZE);
Zhi Wang2707e442016-03-28 23:23:16 +08001181 if (ret)
1182 return ret;
1183
Changbin Du44b46732018-01-30 19:19:49 +08001184 oos_page->spt = spt;
1185 spt->guest_page.oos_page = oos_page;
Zhi Wang2707e442016-03-28 23:23:16 +08001186
1187 list_move_tail(&oos_page->list, &gvt->gtt.oos_page_use_list_head);
1188
Changbin Du44b46732018-01-30 19:19:49 +08001189 trace_oos_change(spt->vgpu->id, "attach", oos_page->id,
1190 spt, spt->guest_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001191 return 0;
1192}
1193
Changbin Du44b46732018-01-30 19:19:49 +08001194static int ppgtt_set_guest_page_sync(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +08001195{
Changbin Du44b46732018-01-30 19:19:49 +08001196 struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
Zhi Wang2707e442016-03-28 23:23:16 +08001197 int ret;
1198
Changbin Due502a2a2018-01-30 19:19:53 +08001199 ret = intel_vgpu_enable_page_track(spt->vgpu, spt->guest_page.gfn);
Zhi Wang2707e442016-03-28 23:23:16 +08001200 if (ret)
1201 return ret;
1202
Changbin Du44b46732018-01-30 19:19:49 +08001203 trace_oos_change(spt->vgpu->id, "set page sync", oos_page->id,
1204 spt, spt->guest_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001205
Changbin Du44b46732018-01-30 19:19:49 +08001206 list_del_init(&oos_page->vm_list);
1207 return sync_oos_page(spt->vgpu, oos_page);
Zhi Wang2707e442016-03-28 23:23:16 +08001208}
1209
Changbin Du44b46732018-01-30 19:19:49 +08001210static int ppgtt_allocate_oos_page(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +08001211{
Changbin Du44b46732018-01-30 19:19:49 +08001212 struct intel_gvt *gvt = spt->vgpu->gvt;
Zhi Wang2707e442016-03-28 23:23:16 +08001213 struct intel_gvt_gtt *gtt = &gvt->gtt;
Changbin Du44b46732018-01-30 19:19:49 +08001214 struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
Zhi Wang2707e442016-03-28 23:23:16 +08001215 int ret;
1216
1217 WARN(oos_page, "shadow PPGTT page has already has a oos page\n");
1218
1219 if (list_empty(&gtt->oos_page_free_list_head)) {
1220 oos_page = container_of(gtt->oos_page_use_list_head.next,
1221 struct intel_vgpu_oos_page, list);
Changbin Du44b46732018-01-30 19:19:49 +08001222 ret = ppgtt_set_guest_page_sync(oos_page->spt);
Zhi Wang2707e442016-03-28 23:23:16 +08001223 if (ret)
1224 return ret;
Changbin Du44b46732018-01-30 19:19:49 +08001225 ret = detach_oos_page(spt->vgpu, oos_page);
Zhi Wang2707e442016-03-28 23:23:16 +08001226 if (ret)
1227 return ret;
1228 } else
1229 oos_page = container_of(gtt->oos_page_free_list_head.next,
1230 struct intel_vgpu_oos_page, list);
Changbin Du44b46732018-01-30 19:19:49 +08001231 return attach_oos_page(oos_page, spt);
Zhi Wang2707e442016-03-28 23:23:16 +08001232}
1233
Changbin Du44b46732018-01-30 19:19:49 +08001234static int ppgtt_set_guest_page_oos(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +08001235{
Changbin Du44b46732018-01-30 19:19:49 +08001236 struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
Zhi Wang2707e442016-03-28 23:23:16 +08001237
1238 if (WARN(!oos_page, "shadow PPGTT page should have a oos page\n"))
1239 return -EINVAL;
1240
Changbin Du44b46732018-01-30 19:19:49 +08001241 trace_oos_change(spt->vgpu->id, "set page out of sync", oos_page->id,
1242 spt, spt->guest_page.type);
Zhi Wang2707e442016-03-28 23:23:16 +08001243
Changbin Du44b46732018-01-30 19:19:49 +08001244 list_add_tail(&oos_page->vm_list, &spt->vgpu->gtt.oos_page_list_head);
Changbin Due502a2a2018-01-30 19:19:53 +08001245 return intel_vgpu_disable_page_track(spt->vgpu, spt->guest_page.gfn);
Zhi Wang2707e442016-03-28 23:23:16 +08001246}
1247
1248/**
1249 * intel_vgpu_sync_oos_pages - sync all the out-of-synced shadow for vGPU
1250 * @vgpu: a vGPU
1251 *
1252 * This function is called before submitting a guest workload to host,
1253 * to sync all the out-of-synced shadow for vGPU
1254 *
1255 * Returns:
1256 * Zero on success, negative error code if failed.
1257 */
1258int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu)
1259{
1260 struct list_head *pos, *n;
1261 struct intel_vgpu_oos_page *oos_page;
1262 int ret;
1263
1264 if (!enable_out_of_sync)
1265 return 0;
1266
1267 list_for_each_safe(pos, n, &vgpu->gtt.oos_page_list_head) {
1268 oos_page = container_of(pos,
1269 struct intel_vgpu_oos_page, vm_list);
Changbin Du44b46732018-01-30 19:19:49 +08001270 ret = ppgtt_set_guest_page_sync(oos_page->spt);
Zhi Wang2707e442016-03-28 23:23:16 +08001271 if (ret)
1272 return ret;
1273 }
1274 return 0;
1275}
1276
1277/*
1278 * The heart of PPGTT shadow page table.
1279 */
1280static int ppgtt_handle_guest_write_page_table(
Changbin Du44b46732018-01-30 19:19:49 +08001281 struct intel_vgpu_ppgtt_spt *spt,
Zhi Wang2707e442016-03-28 23:23:16 +08001282 struct intel_gvt_gtt_entry *we, unsigned long index)
1283{
Zhi Wang2707e442016-03-28 23:23:16 +08001284 struct intel_vgpu *vgpu = spt->vgpu;
Tina Zhang6b3816d2017-08-14 15:24:14 +08001285 int type = spt->shadow_page.type;
Zhi Wang2707e442016-03-28 23:23:16 +08001286 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
Changbin Du72f03d72018-01-30 19:19:48 +08001287 struct intel_gvt_gtt_entry old_se;
Bing Niu9baf0922016-11-07 10:44:36 +08001288 int new_present;
Changbin Du72f03d72018-01-30 19:19:48 +08001289 int ret;
Zhi Wang2707e442016-03-28 23:23:16 +08001290
Zhi Wang2707e442016-03-28 23:23:16 +08001291 new_present = ops->test_present(we);
1292
Tina Zhang6b3816d2017-08-14 15:24:14 +08001293 /*
1294 * Adding the new entry first and then removing the old one, that can
1295 * guarantee the ppgtt table is validated during the window between
1296 * adding and removal.
1297 */
Changbin Du72f03d72018-01-30 19:19:48 +08001298 ppgtt_get_shadow_entry(spt, &old_se, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001299
Zhi Wang2707e442016-03-28 23:23:16 +08001300 if (new_present) {
Changbin Du44b46732018-01-30 19:19:49 +08001301 ret = ppgtt_handle_guest_entry_add(spt, we, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001302 if (ret)
1303 goto fail;
1304 }
Tina Zhang6b3816d2017-08-14 15:24:14 +08001305
Changbin Du44b46732018-01-30 19:19:49 +08001306 ret = ppgtt_handle_guest_entry_removal(spt, &old_se, index);
Tina Zhang6b3816d2017-08-14 15:24:14 +08001307 if (ret)
1308 goto fail;
1309
1310 if (!new_present) {
Changbin Du72f03d72018-01-30 19:19:48 +08001311 ops->set_pfn(&old_se, vgpu->gtt.scratch_pt[type].page_mfn);
1312 ppgtt_set_shadow_entry(spt, &old_se, index);
Tina Zhang6b3816d2017-08-14 15:24:14 +08001313 }
1314
Zhi Wang2707e442016-03-28 23:23:16 +08001315 return 0;
1316fail:
Tina Zhang695fbc02017-03-10 04:26:53 -05001317 gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d.\n",
1318 spt, we->val64, we->type);
Zhi Wang2707e442016-03-28 23:23:16 +08001319 return ret;
1320}
1321
Changbin Du72f03d72018-01-30 19:19:48 +08001322
1323
Changbin Du44b46732018-01-30 19:19:49 +08001324static inline bool can_do_out_of_sync(struct intel_vgpu_ppgtt_spt *spt)
Zhi Wang2707e442016-03-28 23:23:16 +08001325{
1326 return enable_out_of_sync
Changbin Du44b46732018-01-30 19:19:49 +08001327 && gtt_type_is_pte_pt(spt->guest_page.type)
1328 && spt->guest_page.write_cnt >= 2;
Zhi Wang2707e442016-03-28 23:23:16 +08001329}
1330
1331static void ppgtt_set_post_shadow(struct intel_vgpu_ppgtt_spt *spt,
1332 unsigned long index)
1333{
1334 set_bit(index, spt->post_shadow_bitmap);
1335 if (!list_empty(&spt->post_shadow_list))
1336 return;
1337
1338 list_add_tail(&spt->post_shadow_list,
1339 &spt->vgpu->gtt.post_shadow_list_head);
1340}
1341
1342/**
1343 * intel_vgpu_flush_post_shadow - flush the post shadow transactions
1344 * @vgpu: a vGPU
1345 *
1346 * This function is called before submitting a guest workload to host,
1347 * to flush all the post shadows for a vGPU.
1348 *
1349 * Returns:
1350 * Zero on success, negative error code if failed.
1351 */
1352int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu)
1353{
1354 struct list_head *pos, *n;
1355 struct intel_vgpu_ppgtt_spt *spt;
Bing Niu9baf0922016-11-07 10:44:36 +08001356 struct intel_gvt_gtt_entry ge;
Zhi Wang2707e442016-03-28 23:23:16 +08001357 unsigned long index;
1358 int ret;
1359
1360 list_for_each_safe(pos, n, &vgpu->gtt.post_shadow_list_head) {
1361 spt = container_of(pos, struct intel_vgpu_ppgtt_spt,
1362 post_shadow_list);
1363
1364 for_each_set_bit(index, spt->post_shadow_bitmap,
1365 GTT_ENTRY_NUM_IN_ONE_PAGE) {
1366 ppgtt_get_guest_entry(spt, &ge, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001367
Changbin Du44b46732018-01-30 19:19:49 +08001368 ret = ppgtt_handle_guest_write_page_table(spt,
1369 &ge, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001370 if (ret)
1371 return ret;
1372 clear_bit(index, spt->post_shadow_bitmap);
1373 }
1374 list_del_init(&spt->post_shadow_list);
1375 }
1376 return 0;
1377}
1378
Zhi Wang7d1e5cd2017-09-29 02:47:55 +08001379static int ppgtt_handle_guest_write_page_table_bytes(
Changbin Du44b46732018-01-30 19:19:49 +08001380 struct intel_vgpu_ppgtt_spt *spt,
Zhi Wang2707e442016-03-28 23:23:16 +08001381 u64 pa, void *p_data, int bytes)
1382{
Zhi Wang2707e442016-03-28 23:23:16 +08001383 struct intel_vgpu *vgpu = spt->vgpu;
1384 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1385 const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
Tina Zhang6b3816d2017-08-14 15:24:14 +08001386 struct intel_gvt_gtt_entry we, se;
Zhi Wang2707e442016-03-28 23:23:16 +08001387 unsigned long index;
1388 int ret;
1389
1390 index = (pa & (PAGE_SIZE - 1)) >> info->gtt_entry_size_shift;
1391
1392 ppgtt_get_guest_entry(spt, &we, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001393
1394 ops->test_pse(&we);
1395
1396 if (bytes == info->gtt_entry_size) {
Changbin Du44b46732018-01-30 19:19:49 +08001397 ret = ppgtt_handle_guest_write_page_table(spt, &we, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001398 if (ret)
1399 return ret;
1400 } else {
Zhi Wang2707e442016-03-28 23:23:16 +08001401 if (!test_bit(index, spt->post_shadow_bitmap)) {
Zhi Wang121d760d2017-12-29 02:50:08 +08001402 int type = spt->shadow_page.type;
1403
Tina Zhang6b3816d2017-08-14 15:24:14 +08001404 ppgtt_get_shadow_entry(spt, &se, index);
Changbin Du44b46732018-01-30 19:19:49 +08001405 ret = ppgtt_handle_guest_entry_removal(spt, &se, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001406 if (ret)
1407 return ret;
Zhi Wang121d760d2017-12-29 02:50:08 +08001408 ops->set_pfn(&se, vgpu->gtt.scratch_pt[type].page_mfn);
1409 ppgtt_set_shadow_entry(spt, &se, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001410 }
Zhi Wang2707e442016-03-28 23:23:16 +08001411 ppgtt_set_post_shadow(spt, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001412 }
1413
1414 if (!enable_out_of_sync)
1415 return 0;
1416
Changbin Du44b46732018-01-30 19:19:49 +08001417 spt->guest_page.write_cnt++;
Zhi Wang2707e442016-03-28 23:23:16 +08001418
Changbin Du44b46732018-01-30 19:19:49 +08001419 if (spt->guest_page.oos_page)
1420 ops->set_entry(spt->guest_page.oos_page->mem, &we, index,
Zhi Wang2707e442016-03-28 23:23:16 +08001421 false, 0, vgpu);
1422
Changbin Du44b46732018-01-30 19:19:49 +08001423 if (can_do_out_of_sync(spt)) {
1424 if (!spt->guest_page.oos_page)
1425 ppgtt_allocate_oos_page(spt);
Zhi Wang2707e442016-03-28 23:23:16 +08001426
Changbin Du44b46732018-01-30 19:19:49 +08001427 ret = ppgtt_set_guest_page_oos(spt);
Zhi Wang2707e442016-03-28 23:23:16 +08001428 if (ret < 0)
1429 return ret;
1430 }
1431 return 0;
1432}
1433
Changbin Duede9d0c2018-01-30 19:19:40 +08001434static void invalidate_ppgtt_mm(struct intel_vgpu_mm *mm)
Zhi Wang2707e442016-03-28 23:23:16 +08001435{
1436 struct intel_vgpu *vgpu = mm->vgpu;
1437 struct intel_gvt *gvt = vgpu->gvt;
1438 struct intel_gvt_gtt *gtt = &gvt->gtt;
1439 struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops;
1440 struct intel_gvt_gtt_entry se;
Changbin Duede9d0c2018-01-30 19:19:40 +08001441 int index;
Zhi Wang2707e442016-03-28 23:23:16 +08001442
Changbin Duede9d0c2018-01-30 19:19:40 +08001443 if (!mm->ppgtt_mm.shadowed)
Zhi Wang2707e442016-03-28 23:23:16 +08001444 return;
1445
Changbin Duede9d0c2018-01-30 19:19:40 +08001446 for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.shadow_pdps); index++) {
1447 ppgtt_get_shadow_root_entry(mm, &se, index);
1448
Zhi Wang2707e442016-03-28 23:23:16 +08001449 if (!ops->test_present(&se))
1450 continue;
Changbin Duede9d0c2018-01-30 19:19:40 +08001451
Changbin Dud87f5ff2018-01-30 19:19:50 +08001452 ppgtt_invalidate_spt_by_shadow_entry(vgpu, &se);
Zhi Wang2707e442016-03-28 23:23:16 +08001453 se.val64 = 0;
Changbin Duede9d0c2018-01-30 19:19:40 +08001454 ppgtt_set_shadow_root_entry(mm, &se, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001455
Changbin Du44b46732018-01-30 19:19:49 +08001456 trace_spt_guest_change(vgpu->id, "destroy root pointer",
1457 NULL, se.type, se.val64, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001458 }
Changbin Duede9d0c2018-01-30 19:19:40 +08001459
1460 mm->ppgtt_mm.shadowed = false;
Zhi Wang2707e442016-03-28 23:23:16 +08001461}
1462
Zhi Wang2707e442016-03-28 23:23:16 +08001463
Changbin Duede9d0c2018-01-30 19:19:40 +08001464static int shadow_ppgtt_mm(struct intel_vgpu_mm *mm)
Zhi Wang2707e442016-03-28 23:23:16 +08001465{
1466 struct intel_vgpu *vgpu = mm->vgpu;
1467 struct intel_gvt *gvt = vgpu->gvt;
1468 struct intel_gvt_gtt *gtt = &gvt->gtt;
1469 struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops;
1470 struct intel_vgpu_ppgtt_spt *spt;
1471 struct intel_gvt_gtt_entry ge, se;
Changbin Duede9d0c2018-01-30 19:19:40 +08001472 int index, ret;
Zhi Wang2707e442016-03-28 23:23:16 +08001473
Changbin Duede9d0c2018-01-30 19:19:40 +08001474 if (mm->ppgtt_mm.shadowed)
Zhi Wang2707e442016-03-28 23:23:16 +08001475 return 0;
1476
Changbin Duede9d0c2018-01-30 19:19:40 +08001477 mm->ppgtt_mm.shadowed = true;
Zhi Wang2707e442016-03-28 23:23:16 +08001478
Changbin Duede9d0c2018-01-30 19:19:40 +08001479 for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.guest_pdps); index++) {
1480 ppgtt_get_guest_root_entry(mm, &ge, index);
1481
Zhi Wang2707e442016-03-28 23:23:16 +08001482 if (!ops->test_present(&ge))
1483 continue;
1484
Changbin Du44b46732018-01-30 19:19:49 +08001485 trace_spt_guest_change(vgpu->id, __func__, NULL,
1486 ge.type, ge.val64, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001487
Changbin Dud87f5ff2018-01-30 19:19:50 +08001488 spt = ppgtt_populate_spt_by_guest_entry(vgpu, &ge);
Zhi Wang2707e442016-03-28 23:23:16 +08001489 if (IS_ERR(spt)) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001490 gvt_vgpu_err("fail to populate guest root pointer\n");
Zhi Wang2707e442016-03-28 23:23:16 +08001491 ret = PTR_ERR(spt);
1492 goto fail;
1493 }
1494 ppgtt_generate_shadow_entry(&se, spt, &ge);
Changbin Duede9d0c2018-01-30 19:19:40 +08001495 ppgtt_set_shadow_root_entry(mm, &se, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001496
Changbin Du44b46732018-01-30 19:19:49 +08001497 trace_spt_guest_change(vgpu->id, "populate root pointer",
1498 NULL, se.type, se.val64, index);
Zhi Wang2707e442016-03-28 23:23:16 +08001499 }
Changbin Duede9d0c2018-01-30 19:19:40 +08001500
Zhi Wang2707e442016-03-28 23:23:16 +08001501 return 0;
1502fail:
Changbin Duede9d0c2018-01-30 19:19:40 +08001503 invalidate_ppgtt_mm(mm);
Zhi Wang2707e442016-03-28 23:23:16 +08001504 return ret;
1505}
1506
Changbin Duede9d0c2018-01-30 19:19:40 +08001507static struct intel_vgpu_mm *vgpu_alloc_mm(struct intel_vgpu *vgpu)
1508{
1509 struct intel_vgpu_mm *mm;
1510
1511 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
1512 if (!mm)
1513 return NULL;
1514
1515 mm->vgpu = vgpu;
1516 kref_init(&mm->ref);
1517 atomic_set(&mm->pincount, 0);
1518
1519 return mm;
1520}
1521
1522static void vgpu_free_mm(struct intel_vgpu_mm *mm)
1523{
1524 kfree(mm);
1525}
1526
Zhi Wang2707e442016-03-28 23:23:16 +08001527/**
Changbin Duede9d0c2018-01-30 19:19:40 +08001528 * intel_vgpu_create_ppgtt_mm - create a ppgtt mm object for a vGPU
Zhi Wang2707e442016-03-28 23:23:16 +08001529 * @vgpu: a vGPU
Changbin Duede9d0c2018-01-30 19:19:40 +08001530 * @root_entry_type: ppgtt root entry type
1531 * @pdps: guest pdps.
Zhi Wang2707e442016-03-28 23:23:16 +08001532 *
Changbin Duede9d0c2018-01-30 19:19:40 +08001533 * This function is used to create a ppgtt mm object for a vGPU.
Zhi Wang2707e442016-03-28 23:23:16 +08001534 *
1535 * Returns:
1536 * Zero on success, negative error code in pointer if failed.
1537 */
Changbin Duede9d0c2018-01-30 19:19:40 +08001538struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
1539 intel_gvt_gtt_type_t root_entry_type, u64 pdps[])
Zhi Wang2707e442016-03-28 23:23:16 +08001540{
1541 struct intel_gvt *gvt = vgpu->gvt;
Zhi Wang2707e442016-03-28 23:23:16 +08001542 struct intel_vgpu_mm *mm;
1543 int ret;
1544
Changbin Duede9d0c2018-01-30 19:19:40 +08001545 mm = vgpu_alloc_mm(vgpu);
1546 if (!mm)
1547 return ERR_PTR(-ENOMEM);
Zhi Wang2707e442016-03-28 23:23:16 +08001548
Changbin Duede9d0c2018-01-30 19:19:40 +08001549 mm->type = INTEL_GVT_MM_PPGTT;
Zhi Wang2707e442016-03-28 23:23:16 +08001550
Changbin Duede9d0c2018-01-30 19:19:40 +08001551 GEM_BUG_ON(root_entry_type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY &&
1552 root_entry_type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY);
1553 mm->ppgtt_mm.root_entry_type = root_entry_type;
Zhi Wang2707e442016-03-28 23:23:16 +08001554
Changbin Duede9d0c2018-01-30 19:19:40 +08001555 INIT_LIST_HEAD(&mm->ppgtt_mm.list);
1556 INIT_LIST_HEAD(&mm->ppgtt_mm.lru_list);
Zhi Wang2707e442016-03-28 23:23:16 +08001557
Changbin Duede9d0c2018-01-30 19:19:40 +08001558 if (root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY)
1559 mm->ppgtt_mm.guest_pdps[0] = pdps[0];
1560 else
1561 memcpy(mm->ppgtt_mm.guest_pdps, pdps,
1562 sizeof(mm->ppgtt_mm.guest_pdps));
Zhi Wang2707e442016-03-28 23:23:16 +08001563
Changbin Duede9d0c2018-01-30 19:19:40 +08001564 ret = shadow_ppgtt_mm(mm);
Zhi Wang2707e442016-03-28 23:23:16 +08001565 if (ret) {
Changbin Duede9d0c2018-01-30 19:19:40 +08001566 gvt_vgpu_err("failed to shadow ppgtt mm\n");
1567 vgpu_free_mm(mm);
1568 return ERR_PTR(ret);
Zhi Wang2707e442016-03-28 23:23:16 +08001569 }
1570
Changbin Duede9d0c2018-01-30 19:19:40 +08001571 list_add_tail(&mm->ppgtt_mm.list, &vgpu->gtt.ppgtt_mm_list_head);
1572 list_add_tail(&mm->ppgtt_mm.lru_list, &gvt->gtt.ppgtt_mm_lru_list_head);
Zhi Wang2707e442016-03-28 23:23:16 +08001573 return mm;
Changbin Duede9d0c2018-01-30 19:19:40 +08001574}
1575
1576static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu)
1577{
1578 struct intel_vgpu_mm *mm;
1579 unsigned long nr_entries;
1580
1581 mm = vgpu_alloc_mm(vgpu);
1582 if (!mm)
1583 return ERR_PTR(-ENOMEM);
1584
1585 mm->type = INTEL_GVT_MM_GGTT;
1586
1587 nr_entries = gvt_ggtt_gm_sz(vgpu->gvt) >> I915_GTT_PAGE_SHIFT;
Kees Cookfad953c2018-06-12 14:27:37 -07001588 mm->ggtt_mm.virtual_ggtt =
1589 vzalloc(array_size(nr_entries,
1590 vgpu->gvt->device_info.gtt_entry_size));
Changbin Duede9d0c2018-01-30 19:19:40 +08001591 if (!mm->ggtt_mm.virtual_ggtt) {
1592 vgpu_free_mm(mm);
1593 return ERR_PTR(-ENOMEM);
1594 }
1595
1596 return mm;
1597}
1598
1599/**
Changbin Du1bc25852018-01-30 19:19:41 +08001600 * _intel_vgpu_mm_release - destroy a mm object
Changbin Duede9d0c2018-01-30 19:19:40 +08001601 * @mm_ref: a kref object
1602 *
1603 * This function is used to destroy a mm object for vGPU
1604 *
1605 */
Changbin Du1bc25852018-01-30 19:19:41 +08001606void _intel_vgpu_mm_release(struct kref *mm_ref)
Changbin Duede9d0c2018-01-30 19:19:40 +08001607{
1608 struct intel_vgpu_mm *mm = container_of(mm_ref, typeof(*mm), ref);
1609
1610 if (GEM_WARN_ON(atomic_read(&mm->pincount)))
1611 gvt_err("vgpu mm pin count bug detected\n");
1612
1613 if (mm->type == INTEL_GVT_MM_PPGTT) {
1614 list_del(&mm->ppgtt_mm.list);
1615 list_del(&mm->ppgtt_mm.lru_list);
1616 invalidate_ppgtt_mm(mm);
1617 } else {
1618 vfree(mm->ggtt_mm.virtual_ggtt);
1619 }
1620
1621 vgpu_free_mm(mm);
Zhi Wang2707e442016-03-28 23:23:16 +08001622}
1623
1624/**
1625 * intel_vgpu_unpin_mm - decrease the pin count of a vGPU mm object
1626 * @mm: a vGPU mm object
1627 *
1628 * This function is called when user doesn't want to use a vGPU mm object
1629 */
1630void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm)
1631{
Zhi Wang2707e442016-03-28 23:23:16 +08001632 atomic_dec(&mm->pincount);
1633}
1634
1635/**
1636 * intel_vgpu_pin_mm - increase the pin count of a vGPU mm object
1637 * @vgpu: a vGPU
1638 *
1639 * This function is called when user wants to use a vGPU mm object. If this
1640 * mm object hasn't been shadowed yet, the shadow will be populated at this
1641 * time.
1642 *
1643 * Returns:
1644 * Zero on success, negative error code if failed.
1645 */
1646int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm)
1647{
1648 int ret;
1649
Changbin Duede9d0c2018-01-30 19:19:40 +08001650 atomic_inc(&mm->pincount);
Zhi Wang2707e442016-03-28 23:23:16 +08001651
Changbin Duede9d0c2018-01-30 19:19:40 +08001652 if (mm->type == INTEL_GVT_MM_PPGTT) {
1653 ret = shadow_ppgtt_mm(mm);
Zhi Wang2707e442016-03-28 23:23:16 +08001654 if (ret)
1655 return ret;
Changbin Duede9d0c2018-01-30 19:19:40 +08001656
1657 list_move_tail(&mm->ppgtt_mm.lru_list,
1658 &mm->vgpu->gvt->gtt.ppgtt_mm_lru_list_head);
1659
Zhi Wang2707e442016-03-28 23:23:16 +08001660 }
1661
Zhi Wang2707e442016-03-28 23:23:16 +08001662 return 0;
1663}
1664
Changbin Duede9d0c2018-01-30 19:19:40 +08001665static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt)
Zhi Wang2707e442016-03-28 23:23:16 +08001666{
1667 struct intel_vgpu_mm *mm;
1668 struct list_head *pos, *n;
1669
Changbin Duede9d0c2018-01-30 19:19:40 +08001670 list_for_each_safe(pos, n, &gvt->gtt.ppgtt_mm_lru_list_head) {
1671 mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.lru_list);
Zhi Wang2707e442016-03-28 23:23:16 +08001672
Zhi Wang2707e442016-03-28 23:23:16 +08001673 if (atomic_read(&mm->pincount))
1674 continue;
1675
Changbin Duede9d0c2018-01-30 19:19:40 +08001676 list_del_init(&mm->ppgtt_mm.lru_list);
1677 invalidate_ppgtt_mm(mm);
Zhi Wang2707e442016-03-28 23:23:16 +08001678 return 1;
1679 }
1680 return 0;
1681}
1682
1683/*
1684 * GMA translation APIs.
1685 */
1686static inline int ppgtt_get_next_level_entry(struct intel_vgpu_mm *mm,
1687 struct intel_gvt_gtt_entry *e, unsigned long index, bool guest)
1688{
1689 struct intel_vgpu *vgpu = mm->vgpu;
1690 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1691 struct intel_vgpu_ppgtt_spt *s;
1692
Changbin Du44b46732018-01-30 19:19:49 +08001693 s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e));
Zhi Wang2707e442016-03-28 23:23:16 +08001694 if (!s)
1695 return -ENXIO;
1696
1697 if (!guest)
1698 ppgtt_get_shadow_entry(s, e, index);
1699 else
1700 ppgtt_get_guest_entry(s, e, index);
1701 return 0;
1702}
1703
1704/**
1705 * intel_vgpu_gma_to_gpa - translate a gma to GPA
1706 * @mm: mm object. could be a PPGTT or GGTT mm object
1707 * @gma: graphics memory address in this mm object
1708 *
1709 * This function is used to translate a graphics memory address in specific
1710 * graphics memory space to guest physical address.
1711 *
1712 * Returns:
1713 * Guest physical address on success, INTEL_GVT_INVALID_ADDR if failed.
1714 */
1715unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm, unsigned long gma)
1716{
1717 struct intel_vgpu *vgpu = mm->vgpu;
1718 struct intel_gvt *gvt = vgpu->gvt;
1719 struct intel_gvt_gtt_pte_ops *pte_ops = gvt->gtt.pte_ops;
1720 struct intel_gvt_gtt_gma_ops *gma_ops = gvt->gtt.gma_ops;
1721 unsigned long gpa = INTEL_GVT_INVALID_ADDR;
1722 unsigned long gma_index[4];
1723 struct intel_gvt_gtt_entry e;
Changbin Duede9d0c2018-01-30 19:19:40 +08001724 int i, levels = 0;
Zhi Wang2707e442016-03-28 23:23:16 +08001725 int ret;
1726
Changbin Duede9d0c2018-01-30 19:19:40 +08001727 GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT &&
1728 mm->type != INTEL_GVT_MM_PPGTT);
Zhi Wang2707e442016-03-28 23:23:16 +08001729
1730 if (mm->type == INTEL_GVT_MM_GGTT) {
1731 if (!vgpu_gmadr_is_valid(vgpu, gma))
1732 goto err;
1733
Changbin Duede9d0c2018-01-30 19:19:40 +08001734 ggtt_get_guest_entry(mm, &e,
1735 gma_ops->gma_to_ggtt_pte_index(gma));
1736
Zhi Wang9556e112017-10-10 13:51:32 +08001737 gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT)
1738 + (gma & ~I915_GTT_PAGE_MASK);
Zhi Wang2707e442016-03-28 23:23:16 +08001739
1740 trace_gma_translate(vgpu->id, "ggtt", 0, 0, gma, gpa);
Changbin Duede9d0c2018-01-30 19:19:40 +08001741 } else {
1742 switch (mm->ppgtt_mm.root_entry_type) {
1743 case GTT_TYPE_PPGTT_ROOT_L4_ENTRY:
1744 ppgtt_get_shadow_root_entry(mm, &e, 0);
Zhi Wang2707e442016-03-28 23:23:16 +08001745
Changbin Duede9d0c2018-01-30 19:19:40 +08001746 gma_index[0] = gma_ops->gma_to_pml4_index(gma);
1747 gma_index[1] = gma_ops->gma_to_l4_pdp_index(gma);
1748 gma_index[2] = gma_ops->gma_to_pde_index(gma);
1749 gma_index[3] = gma_ops->gma_to_pte_index(gma);
1750 levels = 4;
1751 break;
1752 case GTT_TYPE_PPGTT_ROOT_L3_ENTRY:
1753 ppgtt_get_shadow_root_entry(mm, &e,
1754 gma_ops->gma_to_l3_pdp_index(gma));
Zhi Wang2707e442016-03-28 23:23:16 +08001755
Changbin Duede9d0c2018-01-30 19:19:40 +08001756 gma_index[0] = gma_ops->gma_to_pde_index(gma);
1757 gma_index[1] = gma_ops->gma_to_pte_index(gma);
1758 levels = 2;
1759 break;
1760 default:
1761 GEM_BUG_ON(1);
Changbin Du4b2dbbc2017-08-02 15:06:37 +08001762 }
Changbin Duede9d0c2018-01-30 19:19:40 +08001763
1764 /* walk the shadow page table and get gpa from guest entry */
1765 for (i = 0; i < levels; i++) {
1766 ret = ppgtt_get_next_level_entry(mm, &e, gma_index[i],
1767 (i == levels - 1));
1768 if (ret)
1769 goto err;
1770
1771 if (!pte_ops->test_present(&e)) {
1772 gvt_dbg_core("GMA 0x%lx is not present\n", gma);
1773 goto err;
1774 }
1775 }
1776
1777 gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT) +
1778 (gma & ~I915_GTT_PAGE_MASK);
1779 trace_gma_translate(vgpu->id, "ppgtt", 0,
1780 mm->ppgtt_mm.root_entry_type, gma, gpa);
Zhi Wang2707e442016-03-28 23:23:16 +08001781 }
1782
Zhi Wang2707e442016-03-28 23:23:16 +08001783 return gpa;
1784err:
Tina Zhang695fbc02017-03-10 04:26:53 -05001785 gvt_vgpu_err("invalid mm type: %d gma %lx\n", mm->type, gma);
Zhi Wang2707e442016-03-28 23:23:16 +08001786 return INTEL_GVT_INVALID_ADDR;
1787}
1788
Changbin Dua143cef2018-01-30 19:19:45 +08001789static int emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
Zhi Wang2707e442016-03-28 23:23:16 +08001790 unsigned int off, void *p_data, unsigned int bytes)
1791{
1792 struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm;
1793 const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
1794 unsigned long index = off >> info->gtt_entry_size_shift;
1795 struct intel_gvt_gtt_entry e;
1796
1797 if (bytes != 4 && bytes != 8)
1798 return -EINVAL;
1799
1800 ggtt_get_guest_entry(ggtt_mm, &e, index);
1801 memcpy(p_data, (void *)&e.val64 + (off & (info->gtt_entry_size - 1)),
1802 bytes);
1803 return 0;
1804}
1805
1806/**
1807 * intel_vgpu_emulate_gtt_mmio_read - emulate GTT MMIO register read
1808 * @vgpu: a vGPU
1809 * @off: register offset
1810 * @p_data: data will be returned to guest
1811 * @bytes: data length
1812 *
1813 * This function is used to emulate the GTT MMIO register read
1814 *
1815 * Returns:
1816 * Zero on success, error code if failed.
1817 */
Changbin Dua143cef2018-01-30 19:19:45 +08001818int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu, unsigned int off,
Zhi Wang2707e442016-03-28 23:23:16 +08001819 void *p_data, unsigned int bytes)
1820{
1821 const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
1822 int ret;
1823
1824 if (bytes != 4 && bytes != 8)
1825 return -EINVAL;
1826
1827 off -= info->gtt_start_offset;
Changbin Dua143cef2018-01-30 19:19:45 +08001828 ret = emulate_ggtt_mmio_read(vgpu, off, p_data, bytes);
Zhi Wang2707e442016-03-28 23:23:16 +08001829 return ret;
1830}
1831
Changbin Du7598e872018-03-27 15:35:14 +08001832static void ggtt_invalidate_pte(struct intel_vgpu *vgpu,
1833 struct intel_gvt_gtt_entry *entry)
1834{
1835 struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
1836 unsigned long pfn;
1837
1838 pfn = pte_ops->get_pfn(entry);
1839 if (pfn != vgpu->gvt->gtt.scratch_mfn)
1840 intel_gvt_hypervisor_dma_unmap_guest_page(vgpu,
1841 pfn << PAGE_SHIFT);
1842}
1843
Changbin Dua143cef2018-01-30 19:19:45 +08001844static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off,
Zhi Wang2707e442016-03-28 23:23:16 +08001845 void *p_data, unsigned int bytes)
1846{
1847 struct intel_gvt *gvt = vgpu->gvt;
1848 const struct intel_gvt_device_info *info = &gvt->device_info;
1849 struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm;
1850 struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
1851 unsigned long g_gtt_index = off >> info->gtt_entry_size_shift;
Changbin Ducf4ee732018-03-01 15:49:59 +08001852 unsigned long gma, gfn;
Zhi Wang2707e442016-03-28 23:23:16 +08001853 struct intel_gvt_gtt_entry e, m;
Changbin Ducf4ee732018-03-01 15:49:59 +08001854 dma_addr_t dma_addr;
1855 int ret;
Zhi Wang2707e442016-03-28 23:23:16 +08001856
1857 if (bytes != 4 && bytes != 8)
1858 return -EINVAL;
1859
Zhi Wang9556e112017-10-10 13:51:32 +08001860 gma = g_gtt_index << I915_GTT_PAGE_SHIFT;
Zhi Wang2707e442016-03-28 23:23:16 +08001861
1862 /* the VM may configure the whole GM space when ballooning is used */
Zhao, Xinda7c281352017-02-21 15:54:56 +08001863 if (!vgpu_gmadr_is_valid(vgpu, gma))
Zhi Wang2707e442016-03-28 23:23:16 +08001864 return 0;
Zhi Wang2707e442016-03-28 23:23:16 +08001865
1866 ggtt_get_guest_entry(ggtt_mm, &e, g_gtt_index);
1867
1868 memcpy((void *)&e.val64 + (off & (info->gtt_entry_size - 1)), p_data,
1869 bytes);
1870
1871 if (ops->test_present(&e)) {
Hang Yuancc753fb2017-12-22 18:06:31 +08001872 gfn = ops->get_pfn(&e);
Changbin Du7598e872018-03-27 15:35:14 +08001873 m = e;
Hang Yuancc753fb2017-12-22 18:06:31 +08001874
1875 /* one PTE update may be issued in multiple writes and the
1876 * first write may not construct a valid gfn
1877 */
1878 if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
1879 ops->set_pfn(&m, gvt->gtt.scratch_mfn);
1880 goto out;
1881 }
1882
Changbin Ducf4ee732018-03-01 15:49:59 +08001883 ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn,
1884 &dma_addr);
1885 if (ret) {
Changbin Du72f03d72018-01-30 19:19:48 +08001886 gvt_vgpu_err("fail to populate guest ggtt entry\n");
Xiaoguang Chen359b6932017-03-21 10:54:21 +08001887 /* guest driver may read/write the entry when partial
1888 * update the entry in this situation p2m will fail
1889 * settting the shadow entry to point to a scratch page
1890 */
Zhi Wang22115ce2017-10-10 14:34:11 +08001891 ops->set_pfn(&m, gvt->gtt.scratch_mfn);
Changbin Du72f03d72018-01-30 19:19:48 +08001892 } else
Changbin Ducf4ee732018-03-01 15:49:59 +08001893 ops->set_pfn(&m, dma_addr >> PAGE_SHIFT);
Changbin Du7598e872018-03-27 15:35:14 +08001894 } else {
1895 ggtt_get_host_entry(ggtt_mm, &m, g_gtt_index);
1896 ggtt_invalidate_pte(vgpu, &m);
Zhi Wang22115ce2017-10-10 14:34:11 +08001897 ops->set_pfn(&m, gvt->gtt.scratch_mfn);
Changbin Du7598e872018-03-27 15:35:14 +08001898 ops->clear_present(&m);
1899 }
Zhi Wang2707e442016-03-28 23:23:16 +08001900
Hang Yuancc753fb2017-12-22 18:06:31 +08001901out:
Changbin Du3aff3512018-01-30 19:19:42 +08001902 ggtt_set_host_entry(ggtt_mm, &m, g_gtt_index);
Changbin Dua143cef2018-01-30 19:19:45 +08001903 ggtt_invalidate(gvt->dev_priv);
Zhi Wang2707e442016-03-28 23:23:16 +08001904 ggtt_set_guest_entry(ggtt_mm, &e, g_gtt_index);
1905 return 0;
1906}
1907
1908/*
Changbin Dua143cef2018-01-30 19:19:45 +08001909 * intel_vgpu_emulate_ggtt_mmio_write - emulate GTT MMIO register write
Zhi Wang2707e442016-03-28 23:23:16 +08001910 * @vgpu: a vGPU
1911 * @off: register offset
1912 * @p_data: data from guest write
1913 * @bytes: data length
1914 *
1915 * This function is used to emulate the GTT MMIO register write
1916 *
1917 * Returns:
1918 * Zero on success, error code if failed.
1919 */
Changbin Dua143cef2018-01-30 19:19:45 +08001920int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
1921 unsigned int off, void *p_data, unsigned int bytes)
Zhi Wang2707e442016-03-28 23:23:16 +08001922{
1923 const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
1924 int ret;
1925
1926 if (bytes != 4 && bytes != 8)
1927 return -EINVAL;
1928
1929 off -= info->gtt_start_offset;
Changbin Dua143cef2018-01-30 19:19:45 +08001930 ret = emulate_ggtt_mmio_write(vgpu, off, p_data, bytes);
Zhi Wang2707e442016-03-28 23:23:16 +08001931 return ret;
1932}
1933
Ping Gao3b6411c2016-11-04 13:47:35 +08001934static int alloc_scratch_pages(struct intel_vgpu *vgpu,
1935 intel_gvt_gtt_type_t type)
Zhi Wang2707e442016-03-28 23:23:16 +08001936{
1937 struct intel_vgpu_gtt *gtt = &vgpu->gtt;
Ping Gao3b6411c2016-11-04 13:47:35 +08001938 struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
Zhenyu Wang5c352582017-11-02 17:44:52 +08001939 int page_entry_num = I915_GTT_PAGE_SIZE >>
Ping Gao3b6411c2016-11-04 13:47:35 +08001940 vgpu->gvt->device_info.gtt_entry_size_shift;
Jike Song96317392017-01-09 15:38:38 +08001941 void *scratch_pt;
Ping Gao3b6411c2016-11-04 13:47:35 +08001942 int i;
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08001943 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
1944 dma_addr_t daddr;
Zhi Wang2707e442016-03-28 23:23:16 +08001945
Ping Gao3b6411c2016-11-04 13:47:35 +08001946 if (WARN_ON(type < GTT_TYPE_PPGTT_PTE_PT || type >= GTT_TYPE_MAX))
1947 return -EINVAL;
1948
Jike Song96317392017-01-09 15:38:38 +08001949 scratch_pt = (void *)get_zeroed_page(GFP_KERNEL);
Ping Gao3b6411c2016-11-04 13:47:35 +08001950 if (!scratch_pt) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001951 gvt_vgpu_err("fail to allocate scratch page\n");
Zhi Wang2707e442016-03-28 23:23:16 +08001952 return -ENOMEM;
1953 }
1954
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08001955 daddr = dma_map_page(dev, virt_to_page(scratch_pt), 0,
1956 4096, PCI_DMA_BIDIRECTIONAL);
1957 if (dma_mapping_error(dev, daddr)) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001958 gvt_vgpu_err("fail to dmamap scratch_pt\n");
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08001959 __free_page(virt_to_page(scratch_pt));
1960 return -ENOMEM;
Ping Gao3b6411c2016-11-04 13:47:35 +08001961 }
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08001962 gtt->scratch_pt[type].page_mfn =
Zhenyu Wang5c352582017-11-02 17:44:52 +08001963 (unsigned long)(daddr >> I915_GTT_PAGE_SHIFT);
Jike Song96317392017-01-09 15:38:38 +08001964 gtt->scratch_pt[type].page = virt_to_page(scratch_pt);
Ping Gao3b6411c2016-11-04 13:47:35 +08001965 gvt_dbg_mm("vgpu%d create scratch_pt: type %d mfn=0x%lx\n",
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08001966 vgpu->id, type, gtt->scratch_pt[type].page_mfn);
Ping Gao3b6411c2016-11-04 13:47:35 +08001967
1968 /* Build the tree by full filled the scratch pt with the entries which
1969 * point to the next level scratch pt or scratch page. The
1970 * scratch_pt[type] indicate the scratch pt/scratch page used by the
1971 * 'type' pt.
1972 * e.g. scratch_pt[GTT_TYPE_PPGTT_PDE_PT] is used by
Jike Song96317392017-01-09 15:38:38 +08001973 * GTT_TYPE_PPGTT_PDE_PT level pt, that means this scratch_pt it self
Ping Gao3b6411c2016-11-04 13:47:35 +08001974 * is GTT_TYPE_PPGTT_PTE_PT, and full filled by scratch page mfn.
1975 */
1976 if (type > GTT_TYPE_PPGTT_PTE_PT && type < GTT_TYPE_MAX) {
1977 struct intel_gvt_gtt_entry se;
1978
1979 memset(&se, 0, sizeof(struct intel_gvt_gtt_entry));
1980 se.type = get_entry_type(type - 1);
1981 ops->set_pfn(&se, gtt->scratch_pt[type - 1].page_mfn);
1982
1983 /* The entry parameters like present/writeable/cache type
1984 * set to the same as i915's scratch page tree.
1985 */
1986 se.val64 |= _PAGE_PRESENT | _PAGE_RW;
1987 if (type == GTT_TYPE_PPGTT_PDE_PT)
Zhi Wangc095b972017-09-14 20:39:41 +08001988 se.val64 |= PPAT_CACHED;
Ping Gao3b6411c2016-11-04 13:47:35 +08001989
1990 for (i = 0; i < page_entry_num; i++)
Jike Song96317392017-01-09 15:38:38 +08001991 ops->set_entry(scratch_pt, &se, i, false, 0, vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +08001992 }
1993
Zhi Wang2707e442016-03-28 23:23:16 +08001994 return 0;
1995}
1996
Ping Gao3b6411c2016-11-04 13:47:35 +08001997static int release_scratch_page_tree(struct intel_vgpu *vgpu)
Zhi Wang2707e442016-03-28 23:23:16 +08001998{
Ping Gao3b6411c2016-11-04 13:47:35 +08001999 int i;
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08002000 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
2001 dma_addr_t daddr;
Ping Gao3b6411c2016-11-04 13:47:35 +08002002
2003 for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) {
2004 if (vgpu->gtt.scratch_pt[i].page != NULL) {
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08002005 daddr = (dma_addr_t)(vgpu->gtt.scratch_pt[i].page_mfn <<
Zhenyu Wang5c352582017-11-02 17:44:52 +08002006 I915_GTT_PAGE_SHIFT);
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08002007 dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
Ping Gao3b6411c2016-11-04 13:47:35 +08002008 __free_page(vgpu->gtt.scratch_pt[i].page);
2009 vgpu->gtt.scratch_pt[i].page = NULL;
2010 vgpu->gtt.scratch_pt[i].page_mfn = 0;
2011 }
Zhi Wang2707e442016-03-28 23:23:16 +08002012 }
Ping Gao3b6411c2016-11-04 13:47:35 +08002013
2014 return 0;
2015}
2016
2017static int create_scratch_page_tree(struct intel_vgpu *vgpu)
2018{
2019 int i, ret;
2020
2021 for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) {
2022 ret = alloc_scratch_pages(vgpu, i);
2023 if (ret)
2024 goto err;
2025 }
2026
2027 return 0;
2028
2029err:
2030 release_scratch_page_tree(vgpu);
2031 return ret;
Zhi Wang2707e442016-03-28 23:23:16 +08002032}
2033
2034/**
2035 * intel_vgpu_init_gtt - initialize per-vGPU graphics memory virulization
2036 * @vgpu: a vGPU
2037 *
2038 * This function is used to initialize per-vGPU graphics memory virtualization
2039 * components.
2040 *
2041 * Returns:
2042 * Zero on success, error code if failed.
2043 */
2044int intel_vgpu_init_gtt(struct intel_vgpu *vgpu)
2045{
2046 struct intel_vgpu_gtt *gtt = &vgpu->gtt;
Zhi Wang2707e442016-03-28 23:23:16 +08002047
Changbin Dub6c126a2018-01-30 19:19:54 +08002048 INIT_RADIX_TREE(&gtt->spt_tree, GFP_KERNEL);
Zhi Wang2707e442016-03-28 23:23:16 +08002049
Changbin Duede9d0c2018-01-30 19:19:40 +08002050 INIT_LIST_HEAD(&gtt->ppgtt_mm_list_head);
Zhi Wang2707e442016-03-28 23:23:16 +08002051 INIT_LIST_HEAD(&gtt->oos_page_list_head);
2052 INIT_LIST_HEAD(&gtt->post_shadow_list_head);
2053
Changbin Duede9d0c2018-01-30 19:19:40 +08002054 gtt->ggtt_mm = intel_vgpu_create_ggtt_mm(vgpu);
2055 if (IS_ERR(gtt->ggtt_mm)) {
Tina Zhang695fbc02017-03-10 04:26:53 -05002056 gvt_vgpu_err("fail to create mm for ggtt.\n");
Changbin Duede9d0c2018-01-30 19:19:40 +08002057 return PTR_ERR(gtt->ggtt_mm);
Zhi Wang2707e442016-03-28 23:23:16 +08002058 }
2059
Changbin Duf4c43db2018-03-27 15:35:15 +08002060 intel_vgpu_reset_ggtt(vgpu, false);
Zhi Wang2707e442016-03-28 23:23:16 +08002061
Ping Gao3b6411c2016-11-04 13:47:35 +08002062 return create_scratch_page_tree(vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +08002063}
2064
Changbin Duede9d0c2018-01-30 19:19:40 +08002065static void intel_vgpu_destroy_all_ppgtt_mm(struct intel_vgpu *vgpu)
Ping Gaoda9cc8d2017-02-21 15:52:56 +08002066{
2067 struct list_head *pos, *n;
2068 struct intel_vgpu_mm *mm;
2069
Changbin Duede9d0c2018-01-30 19:19:40 +08002070 list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) {
2071 mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
Changbin Du1bc25852018-01-30 19:19:41 +08002072 intel_vgpu_destroy_mm(mm);
Ping Gaoda9cc8d2017-02-21 15:52:56 +08002073 }
Changbin Duede9d0c2018-01-30 19:19:40 +08002074
2075 if (GEM_WARN_ON(!list_empty(&vgpu->gtt.ppgtt_mm_list_head)))
Colin Ian King84f69ba2018-03-12 12:43:58 +01002076 gvt_err("vgpu ppgtt mm is not fully destroyed\n");
Changbin Duede9d0c2018-01-30 19:19:40 +08002077
Changbin Dub6c126a2018-01-30 19:19:54 +08002078 if (GEM_WARN_ON(!radix_tree_empty(&vgpu->gtt.spt_tree))) {
Changbin Duede9d0c2018-01-30 19:19:40 +08002079 gvt_err("Why we still has spt not freed?\n");
Changbin Dud87f5ff2018-01-30 19:19:50 +08002080 ppgtt_free_all_spt(vgpu);
Changbin Duede9d0c2018-01-30 19:19:40 +08002081 }
2082}
2083
2084static void intel_vgpu_destroy_ggtt_mm(struct intel_vgpu *vgpu)
2085{
Changbin Du1bc25852018-01-30 19:19:41 +08002086 intel_vgpu_destroy_mm(vgpu->gtt.ggtt_mm);
Changbin Duede9d0c2018-01-30 19:19:40 +08002087 vgpu->gtt.ggtt_mm = NULL;
Ping Gaoda9cc8d2017-02-21 15:52:56 +08002088}
2089
Zhi Wang2707e442016-03-28 23:23:16 +08002090/**
2091 * intel_vgpu_clean_gtt - clean up per-vGPU graphics memory virulization
2092 * @vgpu: a vGPU
2093 *
2094 * This function is used to clean up per-vGPU graphics memory virtualization
2095 * components.
2096 *
2097 * Returns:
2098 * Zero on success, error code if failed.
2099 */
2100void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu)
2101{
Changbin Duede9d0c2018-01-30 19:19:40 +08002102 intel_vgpu_destroy_all_ppgtt_mm(vgpu);
2103 intel_vgpu_destroy_ggtt_mm(vgpu);
Ping Gao3b6411c2016-11-04 13:47:35 +08002104 release_scratch_page_tree(vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +08002105}
2106
2107static void clean_spt_oos(struct intel_gvt *gvt)
2108{
2109 struct intel_gvt_gtt *gtt = &gvt->gtt;
2110 struct list_head *pos, *n;
2111 struct intel_vgpu_oos_page *oos_page;
2112
2113 WARN(!list_empty(&gtt->oos_page_use_list_head),
2114 "someone is still using oos page\n");
2115
2116 list_for_each_safe(pos, n, &gtt->oos_page_free_list_head) {
2117 oos_page = container_of(pos, struct intel_vgpu_oos_page, list);
2118 list_del(&oos_page->list);
2119 kfree(oos_page);
2120 }
2121}
2122
2123static int setup_spt_oos(struct intel_gvt *gvt)
2124{
2125 struct intel_gvt_gtt *gtt = &gvt->gtt;
2126 struct intel_vgpu_oos_page *oos_page;
2127 int i;
2128 int ret;
2129
2130 INIT_LIST_HEAD(&gtt->oos_page_free_list_head);
2131 INIT_LIST_HEAD(&gtt->oos_page_use_list_head);
2132
2133 for (i = 0; i < preallocated_oos_pages; i++) {
2134 oos_page = kzalloc(sizeof(*oos_page), GFP_KERNEL);
2135 if (!oos_page) {
Zhi Wang2707e442016-03-28 23:23:16 +08002136 ret = -ENOMEM;
2137 goto fail;
2138 }
2139
2140 INIT_LIST_HEAD(&oos_page->list);
2141 INIT_LIST_HEAD(&oos_page->vm_list);
2142 oos_page->id = i;
2143 list_add_tail(&oos_page->list, &gtt->oos_page_free_list_head);
2144 }
2145
2146 gvt_dbg_mm("%d oos pages preallocated\n", i);
2147
2148 return 0;
2149fail:
2150 clean_spt_oos(gvt);
2151 return ret;
2152}
2153
2154/**
2155 * intel_vgpu_find_ppgtt_mm - find a PPGTT mm object
2156 * @vgpu: a vGPU
2157 * @page_table_level: PPGTT page table level
2158 * @root_entry: PPGTT page table root pointers
2159 *
2160 * This function is used to find a PPGTT mm object from mm object pool
2161 *
2162 * Returns:
2163 * pointer to mm object on success, NULL if failed.
2164 */
2165struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
Changbin Duede9d0c2018-01-30 19:19:40 +08002166 u64 pdps[])
Zhi Wang2707e442016-03-28 23:23:16 +08002167{
Zhi Wang2707e442016-03-28 23:23:16 +08002168 struct intel_vgpu_mm *mm;
Changbin Duede9d0c2018-01-30 19:19:40 +08002169 struct list_head *pos;
Zhi Wang2707e442016-03-28 23:23:16 +08002170
Changbin Duede9d0c2018-01-30 19:19:40 +08002171 list_for_each(pos, &vgpu->gtt.ppgtt_mm_list_head) {
2172 mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
Zhi Wang2707e442016-03-28 23:23:16 +08002173
Changbin Duede9d0c2018-01-30 19:19:40 +08002174 switch (mm->ppgtt_mm.root_entry_type) {
2175 case GTT_TYPE_PPGTT_ROOT_L4_ENTRY:
2176 if (pdps[0] == mm->ppgtt_mm.guest_pdps[0])
Zhi Wang2707e442016-03-28 23:23:16 +08002177 return mm;
Changbin Duede9d0c2018-01-30 19:19:40 +08002178 break;
2179 case GTT_TYPE_PPGTT_ROOT_L3_ENTRY:
2180 if (!memcmp(pdps, mm->ppgtt_mm.guest_pdps,
2181 sizeof(mm->ppgtt_mm.guest_pdps)))
Zhi Wang2707e442016-03-28 23:23:16 +08002182 return mm;
Changbin Duede9d0c2018-01-30 19:19:40 +08002183 break;
2184 default:
2185 GEM_BUG_ON(1);
Zhi Wang2707e442016-03-28 23:23:16 +08002186 }
2187 }
2188 return NULL;
2189}
2190
2191/**
Changbin Due6e9c462018-01-30 19:19:46 +08002192 * intel_vgpu_get_ppgtt_mm - get or create a PPGTT mm object.
Zhi Wang2707e442016-03-28 23:23:16 +08002193 * @vgpu: a vGPU
Changbin Duede9d0c2018-01-30 19:19:40 +08002194 * @root_entry_type: ppgtt root entry type
2195 * @pdps: guest pdps
Zhi Wang2707e442016-03-28 23:23:16 +08002196 *
Changbin Due6e9c462018-01-30 19:19:46 +08002197 * This function is used to find or create a PPGTT mm object from a guest.
Zhi Wang2707e442016-03-28 23:23:16 +08002198 *
2199 * Returns:
2200 * Zero on success, negative error code if failed.
2201 */
Changbin Due6e9c462018-01-30 19:19:46 +08002202struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
Changbin Duede9d0c2018-01-30 19:19:40 +08002203 intel_gvt_gtt_type_t root_entry_type, u64 pdps[])
Zhi Wang2707e442016-03-28 23:23:16 +08002204{
Zhi Wang2707e442016-03-28 23:23:16 +08002205 struct intel_vgpu_mm *mm;
2206
Changbin Duede9d0c2018-01-30 19:19:40 +08002207 mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps);
Zhi Wang2707e442016-03-28 23:23:16 +08002208 if (mm) {
Changbin Du1bc25852018-01-30 19:19:41 +08002209 intel_vgpu_mm_get(mm);
Zhi Wang2707e442016-03-28 23:23:16 +08002210 } else {
Changbin Duede9d0c2018-01-30 19:19:40 +08002211 mm = intel_vgpu_create_ppgtt_mm(vgpu, root_entry_type, pdps);
Changbin Due6e9c462018-01-30 19:19:46 +08002212 if (IS_ERR(mm))
Tina Zhang695fbc02017-03-10 04:26:53 -05002213 gvt_vgpu_err("fail to create mm\n");
Zhi Wang2707e442016-03-28 23:23:16 +08002214 }
Changbin Due6e9c462018-01-30 19:19:46 +08002215 return mm;
Zhi Wang2707e442016-03-28 23:23:16 +08002216}
2217
2218/**
Changbin Due6e9c462018-01-30 19:19:46 +08002219 * intel_vgpu_put_ppgtt_mm - find and put a PPGTT mm object.
Zhi Wang2707e442016-03-28 23:23:16 +08002220 * @vgpu: a vGPU
Changbin Duede9d0c2018-01-30 19:19:40 +08002221 * @pdps: guest pdps
Zhi Wang2707e442016-03-28 23:23:16 +08002222 *
Changbin Due6e9c462018-01-30 19:19:46 +08002223 * This function is used to find a PPGTT mm object from a guest and destroy it.
Zhi Wang2707e442016-03-28 23:23:16 +08002224 *
2225 * Returns:
2226 * Zero on success, negative error code if failed.
2227 */
Changbin Due6e9c462018-01-30 19:19:46 +08002228int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[])
Zhi Wang2707e442016-03-28 23:23:16 +08002229{
Zhi Wang2707e442016-03-28 23:23:16 +08002230 struct intel_vgpu_mm *mm;
2231
Changbin Duede9d0c2018-01-30 19:19:40 +08002232 mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps);
Zhi Wang2707e442016-03-28 23:23:16 +08002233 if (!mm) {
Tina Zhang695fbc02017-03-10 04:26:53 -05002234 gvt_vgpu_err("fail to find ppgtt instance.\n");
Zhi Wang2707e442016-03-28 23:23:16 +08002235 return -EINVAL;
2236 }
Changbin Du1bc25852018-01-30 19:19:41 +08002237 intel_vgpu_mm_put(mm);
Zhi Wang2707e442016-03-28 23:23:16 +08002238 return 0;
2239}
2240
2241/**
2242 * intel_gvt_init_gtt - initialize mm components of a GVT device
2243 * @gvt: GVT device
2244 *
2245 * This function is called at the initialization stage, to initialize
2246 * the mm components of a GVT device.
2247 *
2248 * Returns:
2249 * zero on success, negative error code if failed.
2250 */
2251int intel_gvt_init_gtt(struct intel_gvt *gvt)
2252{
2253 int ret;
Jike Song96317392017-01-09 15:38:38 +08002254 void *page;
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08002255 struct device *dev = &gvt->dev_priv->drm.pdev->dev;
2256 dma_addr_t daddr;
Zhi Wang2707e442016-03-28 23:23:16 +08002257
2258 gvt_dbg_core("init gtt\n");
2259
Xu Hane3476c02017-03-29 10:13:59 +08002260 if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)
2261 || IS_KABYLAKE(gvt->dev_priv)) {
Zhi Wang2707e442016-03-28 23:23:16 +08002262 gvt->gtt.pte_ops = &gen8_gtt_pte_ops;
2263 gvt->gtt.gma_ops = &gen8_gtt_gma_ops;
Zhi Wang2707e442016-03-28 23:23:16 +08002264 } else {
2265 return -ENODEV;
2266 }
2267
Jike Song96317392017-01-09 15:38:38 +08002268 page = (void *)get_zeroed_page(GFP_KERNEL);
2269 if (!page) {
Ping Gaod650ac02016-12-08 10:14:48 +08002270 gvt_err("fail to allocate scratch ggtt page\n");
2271 return -ENOMEM;
2272 }
2273
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08002274 daddr = dma_map_page(dev, virt_to_page(page), 0,
2275 4096, PCI_DMA_BIDIRECTIONAL);
2276 if (dma_mapping_error(dev, daddr)) {
2277 gvt_err("fail to dmamap scratch ggtt page\n");
2278 __free_page(virt_to_page(page));
2279 return -ENOMEM;
Ping Gaod650ac02016-12-08 10:14:48 +08002280 }
Zhi Wang22115ce2017-10-10 14:34:11 +08002281
2282 gvt->gtt.scratch_page = virt_to_page(page);
2283 gvt->gtt.scratch_mfn = (unsigned long)(daddr >> I915_GTT_PAGE_SHIFT);
Ping Gaod650ac02016-12-08 10:14:48 +08002284
Zhi Wang2707e442016-03-28 23:23:16 +08002285 if (enable_out_of_sync) {
2286 ret = setup_spt_oos(gvt);
2287 if (ret) {
2288 gvt_err("fail to initialize SPT oos\n");
Zhou, Wenjia0de98702017-07-04 15:47:00 +08002289 dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
Zhi Wang22115ce2017-10-10 14:34:11 +08002290 __free_page(gvt->gtt.scratch_page);
Zhi Wang2707e442016-03-28 23:23:16 +08002291 return ret;
2292 }
2293 }
Changbin Duede9d0c2018-01-30 19:19:40 +08002294 INIT_LIST_HEAD(&gvt->gtt.ppgtt_mm_lru_list_head);
Zhi Wang2707e442016-03-28 23:23:16 +08002295 return 0;
2296}
2297
2298/**
2299 * intel_gvt_clean_gtt - clean up mm components of a GVT device
2300 * @gvt: GVT device
2301 *
2302 * This function is called at the driver unloading stage, to clean up the
2303 * the mm components of a GVT device.
2304 *
2305 */
2306void intel_gvt_clean_gtt(struct intel_gvt *gvt)
2307{
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08002308 struct device *dev = &gvt->dev_priv->drm.pdev->dev;
Zhi Wang22115ce2017-10-10 14:34:11 +08002309 dma_addr_t daddr = (dma_addr_t)(gvt->gtt.scratch_mfn <<
Zhi Wang9556e112017-10-10 13:51:32 +08002310 I915_GTT_PAGE_SHIFT);
Chuanxiao Dong5de6bd42017-02-09 11:37:11 +08002311
2312 dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
2313
Zhi Wang22115ce2017-10-10 14:34:11 +08002314 __free_page(gvt->gtt.scratch_page);
Ping Gaod650ac02016-12-08 10:14:48 +08002315
Zhi Wang2707e442016-03-28 23:23:16 +08002316 if (enable_out_of_sync)
2317 clean_spt_oos(gvt);
2318}
Ping Gaod650ac02016-12-08 10:14:48 +08002319
2320/**
Zhi Wang730c8ea2018-02-07 18:12:14 +08002321 * intel_vgpu_invalidate_ppgtt - invalidate PPGTT instances
2322 * @vgpu: a vGPU
2323 *
2324 * This function is called when invalidate all PPGTT instances of a vGPU.
2325 *
2326 */
2327void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu)
2328{
2329 struct list_head *pos, *n;
2330 struct intel_vgpu_mm *mm;
2331
2332 list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) {
2333 mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
2334 if (mm->type == INTEL_GVT_MM_PPGTT) {
2335 list_del_init(&mm->ppgtt_mm.lru_list);
2336 if (mm->ppgtt_mm.shadowed)
2337 invalidate_ppgtt_mm(mm);
2338 }
2339 }
2340}
2341
2342/**
Ping Gaod650ac02016-12-08 10:14:48 +08002343 * intel_vgpu_reset_ggtt - reset the GGTT entry
2344 * @vgpu: a vGPU
Changbin Duf4c43db2018-03-27 15:35:15 +08002345 * @invalidate_old: invalidate old entries
Ping Gaod650ac02016-12-08 10:14:48 +08002346 *
2347 * This function is called at the vGPU create stage
2348 * to reset all the GGTT entries.
2349 *
2350 */
Changbin Duf4c43db2018-03-27 15:35:15 +08002351void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old)
Ping Gaod650ac02016-12-08 10:14:48 +08002352{
2353 struct intel_gvt *gvt = vgpu->gvt;
Zhenyu Wang5ad59bf2017-04-12 16:24:57 +08002354 struct drm_i915_private *dev_priv = gvt->dev_priv;
Changbin Dub0c766b2018-01-30 19:19:43 +08002355 struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
2356 struct intel_gvt_gtt_entry entry = {.type = GTT_TYPE_GGTT_PTE};
Changbin Duf4c43db2018-03-27 15:35:15 +08002357 struct intel_gvt_gtt_entry old_entry;
Ping Gaod650ac02016-12-08 10:14:48 +08002358 u32 index;
Ping Gaod650ac02016-12-08 10:14:48 +08002359 u32 num_entries;
Ping Gaod650ac02016-12-08 10:14:48 +08002360
Changbin Dub0c766b2018-01-30 19:19:43 +08002361 pte_ops->set_pfn(&entry, gvt->gtt.scratch_mfn);
2362 pte_ops->set_present(&entry);
Ping Gaod650ac02016-12-08 10:14:48 +08002363
2364 index = vgpu_aperture_gmadr_base(vgpu) >> PAGE_SHIFT;
2365 num_entries = vgpu_aperture_sz(vgpu) >> PAGE_SHIFT;
Changbin Duf4c43db2018-03-27 15:35:15 +08002366 while (num_entries--) {
2367 if (invalidate_old) {
2368 ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index);
2369 ggtt_invalidate_pte(vgpu, &old_entry);
2370 }
Changbin Dub0c766b2018-01-30 19:19:43 +08002371 ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++);
Changbin Duf4c43db2018-03-27 15:35:15 +08002372 }
Ping Gaod650ac02016-12-08 10:14:48 +08002373
2374 index = vgpu_hidden_gmadr_base(vgpu) >> PAGE_SHIFT;
2375 num_entries = vgpu_hidden_sz(vgpu) >> PAGE_SHIFT;
Changbin Duf4c43db2018-03-27 15:35:15 +08002376 while (num_entries--) {
2377 if (invalidate_old) {
2378 ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index);
2379 ggtt_invalidate_pte(vgpu, &old_entry);
2380 }
Changbin Dub0c766b2018-01-30 19:19:43 +08002381 ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++);
Changbin Duf4c43db2018-03-27 15:35:15 +08002382 }
Zhenyu Wang5ad59bf2017-04-12 16:24:57 +08002383
Changbin Dua143cef2018-01-30 19:19:45 +08002384 ggtt_invalidate(dev_priv);
Ping Gaod650ac02016-12-08 10:14:48 +08002385}
Changbin Dub6115812017-01-13 11:15:57 +08002386
2387/**
2388 * intel_vgpu_reset_gtt - reset the all GTT related status
2389 * @vgpu: a vGPU
Changbin Dub6115812017-01-13 11:15:57 +08002390 *
2391 * This function is called from vfio core to reset reset all
2392 * GTT related status, including GGTT, PPGTT, scratch page.
2393 *
2394 */
Chuanxiao Dong4d3e67b2017-08-04 13:08:59 +08002395void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu)
Changbin Dub6115812017-01-13 11:15:57 +08002396{
Ping Gaoda9cc8d2017-02-21 15:52:56 +08002397 /* Shadow pages are only created when there is no page
2398 * table tracking data, so remove page tracking data after
2399 * removing the shadow pages.
2400 */
Changbin Duede9d0c2018-01-30 19:19:40 +08002401 intel_vgpu_destroy_all_ppgtt_mm(vgpu);
Changbin Duf4c43db2018-03-27 15:35:15 +08002402 intel_vgpu_reset_ggtt(vgpu, true);
Changbin Dub6115812017-01-13 11:15:57 +08002403}