gtt.c 65.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * GTT virtualization
 *
 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Zhi Wang <zhi.a.wang@intel.com>
 *    Zhenyu Wang <zhenyuw@linux.intel.com>
 *    Xiao Zheng <xiao.zheng@intel.com>
 *
 * Contributors:
 *    Min He <min.he@intel.com>
 *    Bing Niu <bing.niu@intel.com>
 *
 */

#include "i915_drv.h"
37 38
#include "gvt.h"
#include "i915_pvinfo.h"
39 40
#include "trace.h"

41 42 43 44 45 46
#if defined(VERBOSE_DEBUG)
#define gvt_vdbg_mm(fmt, args...) gvt_dbg_mm(fmt, ##args)
#else
#define gvt_vdbg_mm(fmt, args...)
#endif

47 48 49 50 51 52 53 54 55 56 57
static bool enable_out_of_sync = false;
static int preallocated_oos_pages = 8192;

/*
 * validate a gm address and related range size,
 * translate it to host gm address
 */
bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size)
{
	if ((!vgpu_gmadr_is_valid(vgpu, addr)) || (size
			&& !vgpu_gmadr_is_valid(vgpu, addr + size - 1))) {
58 59
		gvt_vgpu_err("invalid range gmadr 0x%llx size 0x%x\n",
				addr, size);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		return false;
	}
	return true;
}

/* translate a guest gmadr to host gmadr */
int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr)
{
	if (WARN(!vgpu_gmadr_is_valid(vgpu, g_addr),
		 "invalid guest gmadr %llx\n", g_addr))
		return -EACCES;

	if (vgpu_gmadr_is_aperture(vgpu, g_addr))
		*h_addr = vgpu_aperture_gmadr_base(vgpu)
			  + (g_addr - vgpu_aperture_offset(vgpu));
	else
		*h_addr = vgpu_hidden_gmadr_base(vgpu)
			  + (g_addr - vgpu_hidden_offset(vgpu));
	return 0;
}

/* translate a host gmadr to guest gmadr */
int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr)
{
	if (WARN(!gvt_gmadr_is_valid(vgpu->gvt, h_addr),
		 "invalid host gmadr %llx\n", h_addr))
		return -EACCES;

	if (gvt_gmadr_is_aperture(vgpu->gvt, h_addr))
		*g_addr = vgpu_aperture_gmadr_base(vgpu)
			+ (h_addr - gvt_aperture_gmadr_base(vgpu->gvt));
	else
		*g_addr = vgpu_hidden_gmadr_base(vgpu)
			+ (h_addr - gvt_hidden_gmadr_base(vgpu->gvt));
	return 0;
}

int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index,
			     unsigned long *h_index)
{
	u64 h_addr;
	int ret;

Z
Zhi Wang 已提交
103
	ret = intel_gvt_ggtt_gmadr_g2h(vgpu, g_index << I915_GTT_PAGE_SHIFT,
104 105 106 107
				       &h_addr);
	if (ret)
		return ret;

Z
Zhi Wang 已提交
108
	*h_index = h_addr >> I915_GTT_PAGE_SHIFT;
109 110 111 112 113 114 115 116 117
	return 0;
}

int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index,
			     unsigned long *g_index)
{
	u64 g_addr;
	int ret;

Z
Zhi Wang 已提交
118
	ret = intel_gvt_ggtt_gmadr_h2g(vgpu, h_index << I915_GTT_PAGE_SHIFT,
119 120 121 122
				       &g_addr);
	if (ret)
		return ret;

Z
Zhi Wang 已提交
123
	*g_index = g_addr >> I915_GTT_PAGE_SHIFT;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	return 0;
}

#define gtt_type_is_entry(type) \
	(type > GTT_TYPE_INVALID && type < GTT_TYPE_PPGTT_ENTRY \
	 && type != GTT_TYPE_PPGTT_PTE_ENTRY \
	 && type != GTT_TYPE_PPGTT_ROOT_ENTRY)

#define gtt_type_is_pt(type) \
	(type >= GTT_TYPE_PPGTT_PTE_PT && type < GTT_TYPE_MAX)

#define gtt_type_is_pte_pt(type) \
	(type == GTT_TYPE_PPGTT_PTE_PT)

#define gtt_type_is_root_pointer(type) \
	(gtt_type_is_entry(type) && type > GTT_TYPE_PPGTT_ROOT_ENTRY)

#define gtt_init_entry(e, t, p, v) do { \
	(e)->type = t; \
	(e)->pdev = p; \
	memcpy(&(e)->val64, &v, sizeof(v)); \
} while (0)

/*
 * Mappings between GTT_TYPE* enumerations.
 * Following information can be found according to the given type:
 * - type of next level page table
 * - type of entry inside this level page table
 * - type of entry with PSE set
 *
 * If the given type doesn't have such a kind of information,
 * e.g. give a l4 root entry type, then request to get its PSE type,
 * give a PTE page table type, then request to get its next level page
 * table type, as we know l4 root entry doesn't have a PSE bit,
 * and a PTE page table doesn't have a next level page table type,
 * GTT_TYPE_INVALID will be returned. This is useful when traversing a
 * page table.
 */

struct gtt_type_table_entry {
	int entry_type;
165
	int pt_type;
166 167 168 169
	int next_pt_type;
	int pse_entry_type;
};

170
#define GTT_TYPE_TABLE_ENTRY(type, e_type, cpt_type, npt_type, pse_type) \
171 172
	[type] = { \
		.entry_type = e_type, \
173
		.pt_type = cpt_type, \
174 175 176 177 178 179 180
		.next_pt_type = npt_type, \
		.pse_entry_type = pse_type, \
	}

static struct gtt_type_table_entry gtt_type_table[] = {
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
			GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
181
			GTT_TYPE_INVALID,
182 183 184 185
			GTT_TYPE_PPGTT_PML4_PT,
			GTT_TYPE_INVALID),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_PT,
			GTT_TYPE_PPGTT_PML4_ENTRY,
186
			GTT_TYPE_PPGTT_PML4_PT,
187 188 189 190
			GTT_TYPE_PPGTT_PDP_PT,
			GTT_TYPE_INVALID),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_ENTRY,
			GTT_TYPE_PPGTT_PML4_ENTRY,
191
			GTT_TYPE_PPGTT_PML4_PT,
192 193 194 195
			GTT_TYPE_PPGTT_PDP_PT,
			GTT_TYPE_INVALID),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_PT,
			GTT_TYPE_PPGTT_PDP_ENTRY,
196
			GTT_TYPE_PPGTT_PDP_PT,
197 198 199 200
			GTT_TYPE_PPGTT_PDE_PT,
			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
			GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
201
			GTT_TYPE_INVALID,
202 203 204 205
			GTT_TYPE_PPGTT_PDE_PT,
			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_ENTRY,
			GTT_TYPE_PPGTT_PDP_ENTRY,
206
			GTT_TYPE_PPGTT_PDP_PT,
207 208 209 210
			GTT_TYPE_PPGTT_PDE_PT,
			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_PT,
			GTT_TYPE_PPGTT_PDE_ENTRY,
211
			GTT_TYPE_PPGTT_PDE_PT,
212 213 214 215
			GTT_TYPE_PPGTT_PTE_PT,
			GTT_TYPE_PPGTT_PTE_2M_ENTRY),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_ENTRY,
			GTT_TYPE_PPGTT_PDE_ENTRY,
216
			GTT_TYPE_PPGTT_PDE_PT,
217 218
			GTT_TYPE_PPGTT_PTE_PT,
			GTT_TYPE_PPGTT_PTE_2M_ENTRY),
219
	/* We take IPS bit as 'PSE' for PTE level. */
220 221
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_PT,
			GTT_TYPE_PPGTT_PTE_4K_ENTRY,
222
			GTT_TYPE_PPGTT_PTE_PT,
223
			GTT_TYPE_INVALID,
224
			GTT_TYPE_PPGTT_PTE_64K_ENTRY),
225 226
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_4K_ENTRY,
			GTT_TYPE_PPGTT_PTE_4K_ENTRY,
227
			GTT_TYPE_PPGTT_PTE_PT,
228
			GTT_TYPE_INVALID,
229 230 231 232 233 234
			GTT_TYPE_PPGTT_PTE_64K_ENTRY),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_64K_ENTRY,
			GTT_TYPE_PPGTT_PTE_4K_ENTRY,
			GTT_TYPE_PPGTT_PTE_PT,
			GTT_TYPE_INVALID,
			GTT_TYPE_PPGTT_PTE_64K_ENTRY),
235 236
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_2M_ENTRY,
			GTT_TYPE_PPGTT_PDE_ENTRY,
237
			GTT_TYPE_PPGTT_PDE_PT,
238 239 240 241
			GTT_TYPE_INVALID,
			GTT_TYPE_PPGTT_PTE_2M_ENTRY),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_1G_ENTRY,
			GTT_TYPE_PPGTT_PDP_ENTRY,
242
			GTT_TYPE_PPGTT_PDP_PT,
243 244 245 246 247
			GTT_TYPE_INVALID,
			GTT_TYPE_PPGTT_PTE_1G_ENTRY),
	GTT_TYPE_TABLE_ENTRY(GTT_TYPE_GGTT_PTE,
			GTT_TYPE_GGTT_PTE,
			GTT_TYPE_INVALID,
248
			GTT_TYPE_INVALID,
249 250 251 252 253 254 255 256
			GTT_TYPE_INVALID),
};

static inline int get_next_pt_type(int type)
{
	return gtt_type_table[type].next_pt_type;
}

257 258 259 260 261
static inline int get_pt_type(int type)
{
	return gtt_type_table[type].pt_type;
}

262 263 264 265 266 267 268 269 270 271 272 273
static inline int get_entry_type(int type)
{
	return gtt_type_table[type].entry_type;
}

static inline int get_pse_type(int type)
{
	return gtt_type_table[type].pse_entry_type;
}

static u64 read_pte64(struct drm_i915_private *dev_priv, unsigned long index)
{
274
	void __iomem *addr = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm + index;
275 276

	return readq(addr);
277 278
}

279
static void ggtt_invalidate(struct drm_i915_private *dev_priv)
280 281 282 283 284 285
{
	mmio_hw_access_pre(dev_priv);
	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
	mmio_hw_access_post(dev_priv);
}

286 287 288
static void write_pte64(struct drm_i915_private *dev_priv,
		unsigned long index, u64 pte)
{
289
	void __iomem *addr = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm + index;
290 291 292 293

	writeq(pte, addr);
}

294
static inline int gtt_get_entry64(void *pt,
295 296 297 298 299 300 301 302
		struct intel_gvt_gtt_entry *e,
		unsigned long index, bool hypervisor_access, unsigned long gpa,
		struct intel_vgpu *vgpu)
{
	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
	int ret;

	if (WARN_ON(info->gtt_entry_size != 8))
303
		return -EINVAL;
304 305 306 307 308

	if (hypervisor_access) {
		ret = intel_gvt_hypervisor_read_gpa(vgpu, gpa +
				(index << info->gtt_entry_size_shift),
				&e->val64, 8);
309 310
		if (WARN_ON(ret))
			return ret;
311 312 313 314 315
	} else if (!pt) {
		e->val64 = read_pte64(vgpu->gvt->dev_priv, index);
	} else {
		e->val64 = *((u64 *)pt + index);
	}
316
	return 0;
317 318
}

319
static inline int gtt_set_entry64(void *pt,
320 321 322 323 324 325 326 327
		struct intel_gvt_gtt_entry *e,
		unsigned long index, bool hypervisor_access, unsigned long gpa,
		struct intel_vgpu *vgpu)
{
	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
	int ret;

	if (WARN_ON(info->gtt_entry_size != 8))
328
		return -EINVAL;
329 330 331 332 333

	if (hypervisor_access) {
		ret = intel_gvt_hypervisor_write_gpa(vgpu, gpa +
				(index << info->gtt_entry_size_shift),
				&e->val64, 8);
334 335
		if (WARN_ON(ret))
			return ret;
336 337 338 339 340
	} else if (!pt) {
		write_pte64(vgpu->gvt->dev_priv, index, e->val64);
	} else {
		*((u64 *)pt + index) = e->val64;
	}
341
	return 0;
342 343 344 345
}

#define GTT_HAW 46

346 347
#define ADDR_1G_MASK	GENMASK_ULL(GTT_HAW - 1, 30)
#define ADDR_2M_MASK	GENMASK_ULL(GTT_HAW - 1, 21)
348
#define ADDR_64K_MASK	GENMASK_ULL(GTT_HAW - 1, 16)
349
#define ADDR_4K_MASK	GENMASK_ULL(GTT_HAW - 1, 12)
350

351 352 353
#define GTT_SPTE_FLAG_MASK GENMASK_ULL(62, 52)
#define GTT_SPTE_FLAG_64K_SPLITED BIT(52) /* splited 64K gtt entry */

354 355 356 357 358
static unsigned long gen8_gtt_get_pfn(struct intel_gvt_gtt_entry *e)
{
	unsigned long pfn;

	if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY)
359
		pfn = (e->val64 & ADDR_1G_MASK) >> PAGE_SHIFT;
360
	else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY)
361
		pfn = (e->val64 & ADDR_2M_MASK) >> PAGE_SHIFT;
362 363
	else if (e->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY)
		pfn = (e->val64 & ADDR_64K_MASK) >> PAGE_SHIFT;
364
	else
365
		pfn = (e->val64 & ADDR_4K_MASK) >> PAGE_SHIFT;
366 367 368 369 370 371 372
	return pfn;
}

static void gen8_gtt_set_pfn(struct intel_gvt_gtt_entry *e, unsigned long pfn)
{
	if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY) {
		e->val64 &= ~ADDR_1G_MASK;
373
		pfn &= (ADDR_1G_MASK >> PAGE_SHIFT);
374 375
	} else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY) {
		e->val64 &= ~ADDR_2M_MASK;
376
		pfn &= (ADDR_2M_MASK >> PAGE_SHIFT);
377 378 379
	} else if (e->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY) {
		e->val64 &= ~ADDR_64K_MASK;
		pfn &= (ADDR_64K_MASK >> PAGE_SHIFT);
380 381
	} else {
		e->val64 &= ~ADDR_4K_MASK;
382
		pfn &= (ADDR_4K_MASK >> PAGE_SHIFT);
383 384
	}

385
	e->val64 |= (pfn << PAGE_SHIFT);
386 387 388 389
}

static bool gen8_gtt_test_pse(struct intel_gvt_gtt_entry *e)
{
390
	return !!(e->val64 & _PAGE_PSE);
391 392
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static void gen8_gtt_clear_pse(struct intel_gvt_gtt_entry *e)
{
	if (gen8_gtt_test_pse(e)) {
		switch (e->type) {
		case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
			e->val64 &= ~_PAGE_PSE;
			e->type = GTT_TYPE_PPGTT_PDE_ENTRY;
			break;
		case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
			e->type = GTT_TYPE_PPGTT_PDP_ENTRY;
			e->val64 &= ~_PAGE_PSE;
			break;
		default:
			WARN_ON(1);
		}
	}
}

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
static bool gen8_gtt_test_ips(struct intel_gvt_gtt_entry *e)
{
	if (GEM_WARN_ON(e->type != GTT_TYPE_PPGTT_PDE_ENTRY))
		return false;

	return !!(e->val64 & GEN8_PDE_IPS_64K);
}

static void gen8_gtt_clear_ips(struct intel_gvt_gtt_entry *e)
{
	if (GEM_WARN_ON(e->type != GTT_TYPE_PPGTT_PDE_ENTRY))
		return;

	e->val64 &= ~GEN8_PDE_IPS_64K;
}

427 428 429 430 431 432 433 434 435 436 437
static bool gen8_gtt_test_present(struct intel_gvt_gtt_entry *e)
{
	/*
	 * i915 writes PDP root pointer registers without present bit,
	 * it also works, so we need to treat root pointer entry
	 * specifically.
	 */
	if (e->type == GTT_TYPE_PPGTT_ROOT_L3_ENTRY
			|| e->type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY)
		return (e->val64 != 0);
	else
438
		return (e->val64 & _PAGE_PRESENT);
439 440 441 442
}

static void gtt_entry_clear_present(struct intel_gvt_gtt_entry *e)
{
443
	e->val64 &= ~_PAGE_PRESENT;
444 445
}

446 447
static void gtt_entry_set_present(struct intel_gvt_gtt_entry *e)
{
448
	e->val64 |= _PAGE_PRESENT;
449 450
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static bool gen8_gtt_test_64k_splited(struct intel_gvt_gtt_entry *e)
{
	return !!(e->val64 & GTT_SPTE_FLAG_64K_SPLITED);
}

static void gen8_gtt_set_64k_splited(struct intel_gvt_gtt_entry *e)
{
	e->val64 |= GTT_SPTE_FLAG_64K_SPLITED;
}

static void gen8_gtt_clear_64k_splited(struct intel_gvt_gtt_entry *e)
{
	e->val64 &= ~GTT_SPTE_FLAG_64K_SPLITED;
}

466 467 468 469 470
/*
 * Per-platform GMA routines.
 */
static unsigned long gma_to_ggtt_pte_index(unsigned long gma)
{
Z
Zhi Wang 已提交
471
	unsigned long x = (gma >> I915_GTT_PAGE_SHIFT);
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

	trace_gma_index(__func__, gma, x);
	return x;
}

#define DEFINE_PPGTT_GMA_TO_INDEX(prefix, ename, exp) \
static unsigned long prefix##_gma_to_##ename##_index(unsigned long gma) \
{ \
	unsigned long x = (exp); \
	trace_gma_index(__func__, gma, x); \
	return x; \
}

DEFINE_PPGTT_GMA_TO_INDEX(gen8, pte, (gma >> 12 & 0x1ff));
DEFINE_PPGTT_GMA_TO_INDEX(gen8, pde, (gma >> 21 & 0x1ff));
DEFINE_PPGTT_GMA_TO_INDEX(gen8, l3_pdp, (gma >> 30 & 0x3));
DEFINE_PPGTT_GMA_TO_INDEX(gen8, l4_pdp, (gma >> 30 & 0x1ff));
DEFINE_PPGTT_GMA_TO_INDEX(gen8, pml4, (gma >> 39 & 0x1ff));

static struct intel_gvt_gtt_pte_ops gen8_gtt_pte_ops = {
	.get_entry = gtt_get_entry64,
	.set_entry = gtt_set_entry64,
	.clear_present = gtt_entry_clear_present,
495
	.set_present = gtt_entry_set_present,
496 497
	.test_present = gen8_gtt_test_present,
	.test_pse = gen8_gtt_test_pse,
498
	.clear_pse = gen8_gtt_clear_pse,
499 500
	.clear_ips = gen8_gtt_clear_ips,
	.test_ips = gen8_gtt_test_ips,
501 502 503
	.clear_64k_splited = gen8_gtt_clear_64k_splited,
	.set_64k_splited = gen8_gtt_set_64k_splited,
	.test_64k_splited = gen8_gtt_test_64k_splited,
504 505 506 507 508 509 510 511 512 513 514 515 516
	.get_pfn = gen8_gtt_get_pfn,
	.set_pfn = gen8_gtt_set_pfn,
};

static struct intel_gvt_gtt_gma_ops gen8_gtt_gma_ops = {
	.gma_to_ggtt_pte_index = gma_to_ggtt_pte_index,
	.gma_to_pte_index = gen8_gma_to_pte_index,
	.gma_to_pde_index = gen8_gma_to_pde_index,
	.gma_to_l3_pdp_index = gen8_gma_to_l3_pdp_index,
	.gma_to_l4_pdp_index = gen8_gma_to_l4_pdp_index,
	.gma_to_pml4_index = gen8_gma_to_pml4_index,
};

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
/* Update entry type per pse and ips bit. */
static void update_entry_type_for_real(struct intel_gvt_gtt_pte_ops *pte_ops,
	struct intel_gvt_gtt_entry *entry, bool ips)
{
	switch (entry->type) {
	case GTT_TYPE_PPGTT_PDE_ENTRY:
	case GTT_TYPE_PPGTT_PDP_ENTRY:
		if (pte_ops->test_pse(entry))
			entry->type = get_pse_type(entry->type);
		break;
	case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
		if (ips)
			entry->type = get_pse_type(entry->type);
		break;
	default:
		GEM_BUG_ON(!gtt_type_is_entry(entry->type));
	}

	GEM_BUG_ON(entry->type == GTT_TYPE_INVALID);
}

538 539 540
/*
 * MM helpers.
 */
541 542 543
static void _ppgtt_get_root_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index,
		bool guest)
544
{
545
	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
546

547
	GEM_BUG_ON(mm->type != INTEL_GVT_MM_PPGTT);
548

549 550 551 552
	entry->type = mm->ppgtt_mm.root_entry_type;
	pte_ops->get_entry(guest ? mm->ppgtt_mm.guest_pdps :
			   mm->ppgtt_mm.shadow_pdps,
			   entry, index, false, 0, mm->vgpu);
553
	update_entry_type_for_real(pte_ops, entry, false);
554 555
}

556 557
static inline void ppgtt_get_guest_root_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
558
{
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	_ppgtt_get_root_entry(mm, entry, index, true);
}

static inline void ppgtt_get_shadow_root_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
{
	_ppgtt_get_root_entry(mm, entry, index, false);
}

static void _ppgtt_set_root_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index,
		bool guest)
{
	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;

	pte_ops->set_entry(guest ? mm->ppgtt_mm.guest_pdps :
			   mm->ppgtt_mm.shadow_pdps,
			   entry, index, false, 0, mm->vgpu);
}

static inline void ppgtt_set_guest_root_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
{
	_ppgtt_set_root_entry(mm, entry, index, true);
}

static inline void ppgtt_set_shadow_root_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
{
	_ppgtt_set_root_entry(mm, entry, index, false);
}

static void ggtt_get_guest_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
{
	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;

	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);

	entry->type = GTT_TYPE_GGTT_PTE;
	pte_ops->get_entry(mm->ggtt_mm.virtual_ggtt, entry, index,
			   false, 0, mm->vgpu);
}

static void ggtt_set_guest_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
{
	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;

	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);

	pte_ops->set_entry(mm->ggtt_mm.virtual_ggtt, entry, index,
			   false, 0, mm->vgpu);
}

614 615 616 617 618 619 620 621 622 623
static void ggtt_get_host_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
{
	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;

	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);

	pte_ops->get_entry(NULL, entry, index, false, 0, mm->vgpu);
}

624 625 626 627 628 629
static void ggtt_set_host_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *entry, unsigned long index)
{
	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;

	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT);
630

631
	pte_ops->set_entry(NULL, entry, index, false, 0, mm->vgpu);
632 633 634 635 636
}

/*
 * PPGTT shadow page table helpers.
 */
637
static inline int ppgtt_spt_get_entry(
638 639 640 641 642 643 644
		struct intel_vgpu_ppgtt_spt *spt,
		void *page_table, int type,
		struct intel_gvt_gtt_entry *e, unsigned long index,
		bool guest)
{
	struct intel_gvt *gvt = spt->vgpu->gvt;
	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
645
	int ret;
646 647 648 649

	e->type = get_entry_type(type);

	if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n"))
650
		return -EINVAL;
651

652
	ret = ops->get_entry(page_table, e, index, guest,
653
			spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
654
			spt->vgpu);
655 656 657
	if (ret)
		return ret;

658 659
	update_entry_type_for_real(ops, e, guest ?
				   spt->guest_page.pde_ips : false);
660 661 662

	gvt_vdbg_mm("read ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n",
		    type, e->type, index, e->val64);
663
	return 0;
664 665
}

666
static inline int ppgtt_spt_set_entry(
667 668 669 670 671 672 673 674 675
		struct intel_vgpu_ppgtt_spt *spt,
		void *page_table, int type,
		struct intel_gvt_gtt_entry *e, unsigned long index,
		bool guest)
{
	struct intel_gvt *gvt = spt->vgpu->gvt;
	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;

	if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n"))
676
		return -EINVAL;
677

678 679 680
	gvt_vdbg_mm("set ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n",
		    type, e->type, index, e->val64);

681
	return ops->set_entry(page_table, e, index, guest,
682
			spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
683 684 685 686 687
			spt->vgpu);
}

#define ppgtt_get_guest_entry(spt, e, index) \
	ppgtt_spt_get_entry(spt, NULL, \
688
		spt->guest_page.type, e, index, true)
689 690 691

#define ppgtt_set_guest_entry(spt, e, index) \
	ppgtt_spt_set_entry(spt, NULL, \
692
		spt->guest_page.type, e, index, true)
693 694 695 696 697 698 699 700 701

#define ppgtt_get_shadow_entry(spt, e, index) \
	ppgtt_spt_get_entry(spt, spt->shadow_page.vaddr, \
		spt->shadow_page.type, e, index, false)

#define ppgtt_set_shadow_entry(spt, e, index) \
	ppgtt_spt_set_entry(spt, spt->shadow_page.vaddr, \
		spt->shadow_page.type, e, index, false)

702
static void *alloc_spt(gfp_t gfp_mask)
703
{
704
	struct intel_vgpu_ppgtt_spt *spt;
705

706 707 708
	spt = kzalloc(sizeof(*spt), gfp_mask);
	if (!spt)
		return NULL;
709

710 711 712 713 714 715
	spt->shadow_page.page = alloc_page(gfp_mask);
	if (!spt->shadow_page.page) {
		kfree(spt);
		return NULL;
	}
	return spt;
716 717
}

718
static void free_spt(struct intel_vgpu_ppgtt_spt *spt)
719
{
720 721
	__free_page(spt->shadow_page.page);
	kfree(spt);
722 723
}

724 725 726
static int detach_oos_page(struct intel_vgpu *vgpu,
		struct intel_vgpu_oos_page *oos_page);

727
static void ppgtt_free_spt(struct intel_vgpu_ppgtt_spt *spt)
728
{
729
	struct device *kdev = &spt->vgpu->gvt->dev_priv->drm.pdev->dev;
730

731
	trace_spt_free(spt->vgpu->id, spt, spt->guest_page.type);
732

733 734
	dma_unmap_page(kdev, spt->shadow_page.mfn << I915_GTT_PAGE_SHIFT, 4096,
		       PCI_DMA_BIDIRECTIONAL);
735 736

	radix_tree_delete(&spt->vgpu->gtt.spt_tree, spt->shadow_page.mfn);
737

738 739 740
	if (spt->guest_page.gfn) {
		if (spt->guest_page.oos_page)
			detach_oos_page(spt->vgpu, spt->guest_page.oos_page);
741

742 743
		intel_vgpu_unregister_page_track(spt->vgpu, spt->guest_page.gfn);
	}
744 745 746 747 748

	list_del_init(&spt->post_shadow_list);
	free_spt(spt);
}

749
static void ppgtt_free_all_spt(struct intel_vgpu *vgpu)
750
{
751
	struct intel_vgpu_ppgtt_spt *spt;
752 753
	struct radix_tree_iter iter;
	void **slot;
754

755 756
	radix_tree_for_each_slot(slot, &vgpu->gtt.spt_tree, &iter, 0) {
		spt = radix_tree_deref_slot(slot);
757
		ppgtt_free_spt(spt);
758
	}
759 760
}

761
static int ppgtt_handle_guest_write_page_table_bytes(
762
		struct intel_vgpu_ppgtt_spt *spt,
763 764
		u64 pa, void *p_data, int bytes);

765 766 767
static int ppgtt_write_protection_handler(
		struct intel_vgpu_page_track *page_track,
		u64 gpa, void *data, int bytes)
768
{
769 770
	struct intel_vgpu_ppgtt_spt *spt = page_track->priv_data;

771 772 773 774 775
	int ret;

	if (bytes != 4 && bytes != 8)
		return -EINVAL;

776
	ret = ppgtt_handle_guest_write_page_table_bytes(spt, gpa, data, bytes);
777 778 779 780 781
	if (ret)
		return ret;
	return ret;
}

782 783 784 785 786 787
/* Find a spt by guest gfn. */
static struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_gfn(
		struct intel_vgpu *vgpu, unsigned long gfn)
{
	struct intel_vgpu_page_track *track;

788 789 790
	track = intel_vgpu_find_page_track(vgpu, gfn);
	if (track && track->handler == ppgtt_write_protection_handler)
		return track->priv_data;
791 792 793 794 795

	return NULL;
}

/* Find the spt by shadow page mfn. */
796
static inline struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_mfn(
797 798
		struct intel_vgpu *vgpu, unsigned long mfn)
{
799
	return radix_tree_lookup(&vgpu->gtt.spt_tree, mfn);
800 801
}

802
static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt);
803

804
/* Allocate shadow page table without guest page. */
805
static struct intel_vgpu_ppgtt_spt *ppgtt_alloc_spt(
806
		struct intel_vgpu *vgpu, intel_gvt_gtt_type_t type)
807
{
808
	struct device *kdev = &vgpu->gvt->dev_priv->drm.pdev->dev;
809
	struct intel_vgpu_ppgtt_spt *spt = NULL;
810
	dma_addr_t daddr;
811
	int ret;
812 813 814 815

retry:
	spt = alloc_spt(GFP_KERNEL | __GFP_ZERO);
	if (!spt) {
816
		if (reclaim_one_ppgtt_mm(vgpu->gvt))
817 818
			goto retry;

819
		gvt_vgpu_err("fail to allocate ppgtt shadow page\n");
820 821 822 823 824 825 826 827
		return ERR_PTR(-ENOMEM);
	}

	spt->vgpu = vgpu;
	atomic_set(&spt->refcount, 1);
	INIT_LIST_HEAD(&spt->post_shadow_list);

	/*
828
	 * Init shadow_page.
829
	 */
830 831 832 833 834
	spt->shadow_page.type = type;
	daddr = dma_map_page(kdev, spt->shadow_page.page,
			     0, 4096, PCI_DMA_BIDIRECTIONAL);
	if (dma_mapping_error(kdev, daddr)) {
		gvt_vgpu_err("fail to map dma addr\n");
835 836
		ret = -EINVAL;
		goto err_free_spt;
837
	}
838 839
	spt->shadow_page.vaddr = page_address(spt->shadow_page.page);
	spt->shadow_page.mfn = daddr >> I915_GTT_PAGE_SHIFT;
840

841 842
	ret = radix_tree_insert(&vgpu->gtt.spt_tree, spt->shadow_page.mfn, spt);
	if (ret)
843
		goto err_unmap_dma;
844

845
	return spt;
846 847 848 849 850 851

err_unmap_dma:
	dma_unmap_page(kdev, daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
err_free_spt:
	free_spt(spt);
	return ERR_PTR(ret);
852 853
}

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
/* Allocate shadow page table associated with specific gfn. */
static struct intel_vgpu_ppgtt_spt *ppgtt_alloc_spt_gfn(
		struct intel_vgpu *vgpu, intel_gvt_gtt_type_t type,
		unsigned long gfn, bool guest_pde_ips)
{
	struct intel_vgpu_ppgtt_spt *spt;
	int ret;

	spt = ppgtt_alloc_spt(vgpu, type);
	if (IS_ERR(spt))
		return spt;

	/*
	 * Init guest_page.
	 */
	ret = intel_vgpu_register_page_track(vgpu, gfn,
			ppgtt_write_protection_handler, spt);
	if (ret) {
		ppgtt_free_spt(spt);
		return ERR_PTR(ret);
	}

	spt->guest_page.type = type;
	spt->guest_page.gfn = gfn;
	spt->guest_page.pde_ips = guest_pde_ips;

	trace_spt_alloc(vgpu->id, spt, type, spt->shadow_page.mfn, gfn);

	return spt;
}

885 886 887 888
#define pt_entry_size_shift(spt) \
	((spt)->vgpu->gvt->device_info.gtt_entry_size_shift)

#define pt_entries(spt) \
Z
Zhi Wang 已提交
889
	(I915_GTT_PAGE_SIZE >> pt_entry_size_shift(spt))
890 891 892

#define for_each_present_guest_entry(spt, e, i) \
	for (i = 0; i < pt_entries(spt); i++) \
893 894
		if (!ppgtt_get_guest_entry(spt, e, i) && \
		    spt->vgpu->gvt->gtt.pte_ops->test_present(e))
895 896 897

#define for_each_present_shadow_entry(spt, e, i) \
	for (i = 0; i < pt_entries(spt); i++) \
898 899
		if (!ppgtt_get_shadow_entry(spt, e, i) && \
		    spt->vgpu->gvt->gtt.pte_ops->test_present(e))
900

901
static void ppgtt_get_spt(struct intel_vgpu_ppgtt_spt *spt)
902 903 904 905 906 907 908 909
{
	int v = atomic_read(&spt->refcount);

	trace_spt_refcount(spt->vgpu->id, "inc", spt, v, (v + 1));

	atomic_inc(&spt->refcount);
}

910
static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt);
911

912
static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu,
913 914 915 916
		struct intel_gvt_gtt_entry *e)
{
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
	struct intel_vgpu_ppgtt_spt *s;
917
	intel_gvt_gtt_type_t cur_pt_type;
918

919
	GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(e->type)));
920

921 922 923 924 925 926 927
	if (e->type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY
		&& e->type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY) {
		cur_pt_type = get_next_pt_type(e->type) + 1;
		if (ops->get_pfn(e) ==
			vgpu->gtt.scratch_pt[cur_pt_type].page_mfn)
			return 0;
	}
928
	s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e));
929
	if (!s) {
930 931
		gvt_vgpu_err("fail to find shadow page: mfn: 0x%lx\n",
				ops->get_pfn(e));
932 933
		return -ENXIO;
	}
934
	return ppgtt_invalidate_spt(s);
935 936
}

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt,
		struct intel_gvt_gtt_entry *entry)
{
	struct intel_vgpu *vgpu = spt->vgpu;
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
	unsigned long pfn;
	int type;

	pfn = ops->get_pfn(entry);
	type = spt->shadow_page.type;

	if (pfn == vgpu->gtt.scratch_pt[type].page_mfn)
		return;

	intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT);
}

954
static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt)
955
{
956
	struct intel_vgpu *vgpu = spt->vgpu;
957 958 959 960 961 962
	struct intel_gvt_gtt_entry e;
	unsigned long index;
	int ret;
	int v = atomic_read(&spt->refcount);

	trace_spt_change(spt->vgpu->id, "die", spt,
963
			spt->guest_page.gfn, spt->shadow_page.type);
964 965 966 967 968 969 970

	trace_spt_refcount(spt->vgpu->id, "dec", spt, v, (v - 1));

	if (atomic_dec_return(&spt->refcount) > 0)
		return 0;

	for_each_present_shadow_entry(spt, &e, index) {
971 972 973
		switch (e.type) {
		case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
			gvt_vdbg_mm("invalidate 4K entry\n");
974 975
			ppgtt_invalidate_pte(spt, &e);
			break;
976
		case GTT_TYPE_PPGTT_PTE_64K_ENTRY:
977 978
		case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
		case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
979
			WARN(1, "GVT doesn't support 64K/2M/1GB page\n");
980 981 982 983 984
			continue;
		case GTT_TYPE_PPGTT_PML4_ENTRY:
		case GTT_TYPE_PPGTT_PDP_ENTRY:
		case GTT_TYPE_PPGTT_PDE_ENTRY:
			gvt_vdbg_mm("invalidate PMUL4/PDP/PDE entry\n");
985
			ret = ppgtt_invalidate_spt_by_shadow_entry(
986 987 988 989 990 991
					spt->vgpu, &e);
			if (ret)
				goto fail;
			break;
		default:
			GEM_BUG_ON(1);
992 993
		}
	}
994

995
	trace_spt_change(spt->vgpu->id, "release", spt,
996
			 spt->guest_page.gfn, spt->shadow_page.type);
997
	ppgtt_free_spt(spt);
998 999
	return 0;
fail:
1000 1001
	gvt_vgpu_err("fail: shadow page %p shadow entry 0x%llx type %d\n",
			spt, e.val64, e.type);
1002 1003 1004
	return ret;
}

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
static bool vgpu_ips_enabled(struct intel_vgpu *vgpu)
{
	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;

	if (INTEL_GEN(dev_priv) == 9 || INTEL_GEN(dev_priv) == 10) {
		u32 ips = vgpu_vreg_t(vgpu, GEN8_GAMW_ECO_DEV_RW_IA) &
			GAMW_ECO_ENABLE_64K_IPS_FIELD;

		return ips == GAMW_ECO_ENABLE_64K_IPS_FIELD;
	} else if (INTEL_GEN(dev_priv) >= 11) {
		/* 64K paging only controlled by IPS bit in PTE now. */
		return true;
	} else
		return false;
}

1021
static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt);
1022

1023
static struct intel_vgpu_ppgtt_spt *ppgtt_populate_spt_by_guest_entry(
1024 1025 1026
		struct intel_vgpu *vgpu, struct intel_gvt_gtt_entry *we)
{
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1027
	struct intel_vgpu_ppgtt_spt *spt = NULL;
1028
	bool ips = false;
1029 1030
	int ret;

1031
	GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(we->type)));
1032

1033 1034
	spt = intel_vgpu_find_spt_by_gfn(vgpu, ops->get_pfn(we));
	if (spt)
1035
		ppgtt_get_spt(spt);
1036
	else {
1037 1038
		int type = get_next_pt_type(we->type);

1039 1040 1041
		if (we->type == GTT_TYPE_PPGTT_PDE_ENTRY)
			ips = vgpu_ips_enabled(vgpu) && ops->test_ips(we);

1042
		spt = ppgtt_alloc_spt_gfn(vgpu, type, ops->get_pfn(we), ips);
1043 1044
		if (IS_ERR(spt)) {
			ret = PTR_ERR(spt);
1045 1046 1047
			goto fail;
		}

1048
		ret = intel_vgpu_enable_page_track(vgpu, spt->guest_page.gfn);
1049 1050 1051
		if (ret)
			goto fail;

1052
		ret = ppgtt_populate_spt(spt);
1053 1054 1055
		if (ret)
			goto fail;

1056 1057
		trace_spt_change(vgpu->id, "new", spt, spt->guest_page.gfn,
				 spt->shadow_page.type);
1058
	}
1059
	return spt;
1060
fail:
1061
	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
1062
		     spt, we->val64, we->type);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	return ERR_PTR(ret);
}

static inline void ppgtt_generate_shadow_entry(struct intel_gvt_gtt_entry *se,
		struct intel_vgpu_ppgtt_spt *s, struct intel_gvt_gtt_entry *ge)
{
	struct intel_gvt_gtt_pte_ops *ops = s->vgpu->gvt->gtt.pte_ops;

	se->type = ge->type;
	se->val64 = ge->val64;

	ops->set_pfn(se, s->shadow_page.mfn);
}

1077 1078 1079 1080 1081 1082
static int ppgtt_populate_shadow_entry(struct intel_vgpu *vgpu,
	struct intel_vgpu_ppgtt_spt *spt, unsigned long index,
	struct intel_gvt_gtt_entry *ge)
{
	struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
	struct intel_gvt_gtt_entry se = *ge;
1083 1084 1085
	unsigned long gfn;
	dma_addr_t dma_addr;
	int ret;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

	if (!pte_ops->test_present(ge))
		return 0;

	gfn = pte_ops->get_pfn(ge);

	switch (ge->type) {
	case GTT_TYPE_PPGTT_PTE_4K_ENTRY:
		gvt_vdbg_mm("shadow 4K gtt entry\n");
		break;
1096
	case GTT_TYPE_PPGTT_PTE_64K_ENTRY:
1097 1098
	case GTT_TYPE_PPGTT_PTE_2M_ENTRY:
	case GTT_TYPE_PPGTT_PTE_1G_ENTRY:
1099
		gvt_vgpu_err("GVT doesn't support 64K/2M/1GB entry\n");
1100 1101 1102 1103 1104 1105
		return -EINVAL;
	default:
		GEM_BUG_ON(1);
	};

	/* direct shadow */
1106 1107
	ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn, &dma_addr);
	if (ret)
1108 1109
		return -ENXIO;

1110
	pte_ops->set_pfn(&se, dma_addr >> PAGE_SHIFT);
1111 1112 1113 1114
	ppgtt_set_shadow_entry(spt, &se, index);
	return 0;
}

1115
static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt)
1116 1117
{
	struct intel_vgpu *vgpu = spt->vgpu;
1118 1119
	struct intel_gvt *gvt = vgpu->gvt;
	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
1120 1121
	struct intel_vgpu_ppgtt_spt *s;
	struct intel_gvt_gtt_entry se, ge;
1122
	unsigned long gfn, i;
1123 1124 1125
	int ret;

	trace_spt_change(spt->vgpu->id, "born", spt,
1126
			 spt->guest_page.gfn, spt->shadow_page.type);
1127

1128 1129
	for_each_present_guest_entry(spt, &ge, i) {
		if (gtt_type_is_pt(get_next_pt_type(ge.type))) {
1130
			s = ppgtt_populate_spt_by_guest_entry(vgpu, &ge);
1131 1132 1133 1134 1135 1136 1137 1138
			if (IS_ERR(s)) {
				ret = PTR_ERR(s);
				goto fail;
			}
			ppgtt_get_shadow_entry(spt, &se, i);
			ppgtt_generate_shadow_entry(&se, s, &ge);
			ppgtt_set_shadow_entry(spt, &se, i);
		} else {
1139
			gfn = ops->get_pfn(&ge);
1140
			if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
1141
				ops->set_pfn(&se, gvt->gtt.scratch_mfn);
1142 1143 1144
				ppgtt_set_shadow_entry(spt, &se, i);
				continue;
			}
1145

1146 1147 1148
			ret = ppgtt_populate_shadow_entry(vgpu, spt, i, &ge);
			if (ret)
				goto fail;
1149 1150 1151 1152
		}
	}
	return 0;
fail:
1153 1154
	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
			spt, ge.val64, ge.type);
1155 1156 1157
	return ret;
}

1158
static int ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt *spt,
1159
		struct intel_gvt_gtt_entry *se, unsigned long index)
1160 1161 1162 1163 1164
{
	struct intel_vgpu *vgpu = spt->vgpu;
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
	int ret;

1165 1166
	trace_spt_guest_change(spt->vgpu->id, "remove", spt,
			       spt->shadow_page.type, se->val64, index);
1167

1168 1169 1170
	gvt_vdbg_mm("destroy old shadow entry, type %d, index %lu, value %llx\n",
		    se->type, index, se->val64);

1171
	if (!ops->test_present(se))
1172 1173
		return 0;

1174 1175
	if (ops->get_pfn(se) ==
	    vgpu->gtt.scratch_pt[spt->shadow_page.type].page_mfn)
1176 1177
		return 0;

1178
	if (gtt_type_is_pt(get_next_pt_type(se->type))) {
1179
		struct intel_vgpu_ppgtt_spt *s =
1180
			intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(se));
1181
		if (!s) {
1182
			gvt_vgpu_err("fail to find guest page\n");
1183 1184 1185
			ret = -ENXIO;
			goto fail;
		}
1186
		ret = ppgtt_invalidate_spt(s);
1187 1188
		if (ret)
			goto fail;
1189 1190 1191
	} else
		ppgtt_invalidate_pte(spt, se);

1192 1193
	return 0;
fail:
1194
	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n",
1195
			spt, se->val64, se->type);
1196 1197 1198
	return ret;
}

1199
static int ppgtt_handle_guest_entry_add(struct intel_vgpu_ppgtt_spt *spt,
1200 1201 1202 1203 1204 1205 1206
		struct intel_gvt_gtt_entry *we, unsigned long index)
{
	struct intel_vgpu *vgpu = spt->vgpu;
	struct intel_gvt_gtt_entry m;
	struct intel_vgpu_ppgtt_spt *s;
	int ret;

1207 1208
	trace_spt_guest_change(spt->vgpu->id, "add", spt, spt->shadow_page.type,
			       we->val64, index);
1209

1210 1211 1212
	gvt_vdbg_mm("add shadow entry: type %d, index %lu, value %llx\n",
		    we->type, index, we->val64);

1213
	if (gtt_type_is_pt(get_next_pt_type(we->type))) {
1214
		s = ppgtt_populate_spt_by_guest_entry(vgpu, we);
1215 1216 1217 1218 1219 1220 1221 1222
		if (IS_ERR(s)) {
			ret = PTR_ERR(s);
			goto fail;
		}
		ppgtt_get_shadow_entry(spt, &m, index);
		ppgtt_generate_shadow_entry(&m, s, we);
		ppgtt_set_shadow_entry(spt, &m, index);
	} else {
1223
		ret = ppgtt_populate_shadow_entry(vgpu, spt, index, we);
1224 1225 1226 1227 1228
		if (ret)
			goto fail;
	}
	return 0;
fail:
1229 1230
	gvt_vgpu_err("fail: spt %p guest entry 0x%llx type %d\n",
		spt, we->val64, we->type);
1231 1232 1233 1234 1235 1236 1237 1238 1239
	return ret;
}

static int sync_oos_page(struct intel_vgpu *vgpu,
		struct intel_vgpu_oos_page *oos_page)
{
	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
	struct intel_gvt *gvt = vgpu->gvt;
	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
1240
	struct intel_vgpu_ppgtt_spt *spt = oos_page->spt;
1241
	struct intel_gvt_gtt_entry old, new;
1242 1243 1244 1245
	int index;
	int ret;

	trace_oos_change(vgpu->id, "sync", oos_page->id,
1246
			 spt, spt->guest_page.type);
1247

1248
	old.type = new.type = get_entry_type(spt->guest_page.type);
1249 1250
	old.val64 = new.val64 = 0;

Z
Zhi Wang 已提交
1251 1252
	for (index = 0; index < (I915_GTT_PAGE_SIZE >>
				info->gtt_entry_size_shift); index++) {
1253 1254
		ops->get_entry(oos_page->mem, &old, index, false, 0, vgpu);
		ops->get_entry(NULL, &new, index, true,
1255
			       spt->guest_page.gfn << PAGE_SHIFT, vgpu);
1256 1257 1258 1259 1260 1261

		if (old.val64 == new.val64
			&& !test_and_clear_bit(index, spt->post_shadow_bitmap))
			continue;

		trace_oos_sync(vgpu->id, oos_page->id,
1262
				spt, spt->guest_page.type,
1263 1264
				new.val64, index);

1265
		ret = ppgtt_populate_shadow_entry(vgpu, spt, index, &new);
1266 1267 1268 1269 1270 1271
		if (ret)
			return ret;

		ops->set_entry(oos_page->mem, &new, index, false, 0, vgpu);
	}

1272
	spt->guest_page.write_cnt = 0;
1273 1274 1275 1276 1277 1278 1279 1280
	list_del_init(&spt->post_shadow_list);
	return 0;
}

static int detach_oos_page(struct intel_vgpu *vgpu,
		struct intel_vgpu_oos_page *oos_page)
{
	struct intel_gvt *gvt = vgpu->gvt;
1281
	struct intel_vgpu_ppgtt_spt *spt = oos_page->spt;
1282 1283

	trace_oos_change(vgpu->id, "detach", oos_page->id,
1284
			 spt, spt->guest_page.type);
1285

1286 1287 1288
	spt->guest_page.write_cnt = 0;
	spt->guest_page.oos_page = NULL;
	oos_page->spt = NULL;
1289 1290 1291 1292 1293 1294 1295

	list_del_init(&oos_page->vm_list);
	list_move_tail(&oos_page->list, &gvt->gtt.oos_page_free_list_head);

	return 0;
}

1296 1297
static int attach_oos_page(struct intel_vgpu_oos_page *oos_page,
		struct intel_vgpu_ppgtt_spt *spt)
1298
{
1299
	struct intel_gvt *gvt = spt->vgpu->gvt;
1300 1301
	int ret;

1302 1303
	ret = intel_gvt_hypervisor_read_gpa(spt->vgpu,
			spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
Z
Zhi Wang 已提交
1304
			oos_page->mem, I915_GTT_PAGE_SIZE);
1305 1306 1307
	if (ret)
		return ret;

1308 1309
	oos_page->spt = spt;
	spt->guest_page.oos_page = oos_page;
1310 1311 1312

	list_move_tail(&oos_page->list, &gvt->gtt.oos_page_use_list_head);

1313 1314
	trace_oos_change(spt->vgpu->id, "attach", oos_page->id,
			 spt, spt->guest_page.type);
1315 1316 1317
	return 0;
}

1318
static int ppgtt_set_guest_page_sync(struct intel_vgpu_ppgtt_spt *spt)
1319
{
1320
	struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
1321 1322
	int ret;

1323
	ret = intel_vgpu_enable_page_track(spt->vgpu, spt->guest_page.gfn);
1324 1325 1326
	if (ret)
		return ret;

1327 1328
	trace_oos_change(spt->vgpu->id, "set page sync", oos_page->id,
			 spt, spt->guest_page.type);
1329

1330 1331
	list_del_init(&oos_page->vm_list);
	return sync_oos_page(spt->vgpu, oos_page);
1332 1333
}

1334
static int ppgtt_allocate_oos_page(struct intel_vgpu_ppgtt_spt *spt)
1335
{
1336
	struct intel_gvt *gvt = spt->vgpu->gvt;
1337
	struct intel_gvt_gtt *gtt = &gvt->gtt;
1338
	struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
1339 1340 1341 1342 1343 1344 1345
	int ret;

	WARN(oos_page, "shadow PPGTT page has already has a oos page\n");

	if (list_empty(&gtt->oos_page_free_list_head)) {
		oos_page = container_of(gtt->oos_page_use_list_head.next,
			struct intel_vgpu_oos_page, list);
1346
		ret = ppgtt_set_guest_page_sync(oos_page->spt);
1347 1348
		if (ret)
			return ret;
1349
		ret = detach_oos_page(spt->vgpu, oos_page);
1350 1351 1352 1353 1354
		if (ret)
			return ret;
	} else
		oos_page = container_of(gtt->oos_page_free_list_head.next,
			struct intel_vgpu_oos_page, list);
1355
	return attach_oos_page(oos_page, spt);
1356 1357
}

1358
static int ppgtt_set_guest_page_oos(struct intel_vgpu_ppgtt_spt *spt)
1359
{
1360
	struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page;
1361 1362 1363 1364

	if (WARN(!oos_page, "shadow PPGTT page should have a oos page\n"))
		return -EINVAL;

1365 1366
	trace_oos_change(spt->vgpu->id, "set page out of sync", oos_page->id,
			 spt, spt->guest_page.type);
1367

1368
	list_add_tail(&oos_page->vm_list, &spt->vgpu->gtt.oos_page_list_head);
1369
	return intel_vgpu_disable_page_track(spt->vgpu, spt->guest_page.gfn);
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
}

/**
 * intel_vgpu_sync_oos_pages - sync all the out-of-synced shadow for vGPU
 * @vgpu: a vGPU
 *
 * This function is called before submitting a guest workload to host,
 * to sync all the out-of-synced shadow for vGPU
 *
 * Returns:
 * Zero on success, negative error code if failed.
 */
int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu)
{
	struct list_head *pos, *n;
	struct intel_vgpu_oos_page *oos_page;
	int ret;

	if (!enable_out_of_sync)
		return 0;

	list_for_each_safe(pos, n, &vgpu->gtt.oos_page_list_head) {
		oos_page = container_of(pos,
				struct intel_vgpu_oos_page, vm_list);
1394
		ret = ppgtt_set_guest_page_sync(oos_page->spt);
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
		if (ret)
			return ret;
	}
	return 0;
}

/*
 * The heart of PPGTT shadow page table.
 */
static int ppgtt_handle_guest_write_page_table(
1405
		struct intel_vgpu_ppgtt_spt *spt,
1406 1407 1408
		struct intel_gvt_gtt_entry *we, unsigned long index)
{
	struct intel_vgpu *vgpu = spt->vgpu;
1409
	int type = spt->shadow_page.type;
1410
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
1411
	struct intel_gvt_gtt_entry old_se;
1412
	int new_present;
1413
	int ret;
1414 1415 1416

	new_present = ops->test_present(we);

1417 1418 1419 1420 1421
	/*
	 * Adding the new entry first and then removing the old one, that can
	 * guarantee the ppgtt table is validated during the window between
	 * adding and removal.
	 */
1422
	ppgtt_get_shadow_entry(spt, &old_se, index);
1423 1424

	if (new_present) {
1425
		ret = ppgtt_handle_guest_entry_add(spt, we, index);
1426 1427 1428
		if (ret)
			goto fail;
	}
1429

1430
	ret = ppgtt_handle_guest_entry_removal(spt, &old_se, index);
1431 1432 1433 1434
	if (ret)
		goto fail;

	if (!new_present) {
1435 1436
		ops->set_pfn(&old_se, vgpu->gtt.scratch_pt[type].page_mfn);
		ppgtt_set_shadow_entry(spt, &old_se, index);
1437 1438
	}

1439 1440
	return 0;
fail:
1441 1442
	gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d.\n",
			spt, we->val64, we->type);
1443 1444 1445
	return ret;
}

1446 1447


1448
static inline bool can_do_out_of_sync(struct intel_vgpu_ppgtt_spt *spt)
1449 1450
{
	return enable_out_of_sync
1451 1452
		&& gtt_type_is_pte_pt(spt->guest_page.type)
		&& spt->guest_page.write_cnt >= 2;
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
}

static void ppgtt_set_post_shadow(struct intel_vgpu_ppgtt_spt *spt,
		unsigned long index)
{
	set_bit(index, spt->post_shadow_bitmap);
	if (!list_empty(&spt->post_shadow_list))
		return;

	list_add_tail(&spt->post_shadow_list,
			&spt->vgpu->gtt.post_shadow_list_head);
}

/**
 * intel_vgpu_flush_post_shadow - flush the post shadow transactions
 * @vgpu: a vGPU
 *
 * This function is called before submitting a guest workload to host,
 * to flush all the post shadows for a vGPU.
 *
 * Returns:
 * Zero on success, negative error code if failed.
 */
int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu)
{
	struct list_head *pos, *n;
	struct intel_vgpu_ppgtt_spt *spt;
1480
	struct intel_gvt_gtt_entry ge;
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
	unsigned long index;
	int ret;

	list_for_each_safe(pos, n, &vgpu->gtt.post_shadow_list_head) {
		spt = container_of(pos, struct intel_vgpu_ppgtt_spt,
				post_shadow_list);

		for_each_set_bit(index, spt->post_shadow_bitmap,
				GTT_ENTRY_NUM_IN_ONE_PAGE) {
			ppgtt_get_guest_entry(spt, &ge, index);

1492 1493
			ret = ppgtt_handle_guest_write_page_table(spt,
							&ge, index);
1494 1495 1496 1497 1498 1499 1500 1501 1502
			if (ret)
				return ret;
			clear_bit(index, spt->post_shadow_bitmap);
		}
		list_del_init(&spt->post_shadow_list);
	}
	return 0;
}

1503
static int ppgtt_handle_guest_write_page_table_bytes(
1504
		struct intel_vgpu_ppgtt_spt *spt,
1505 1506 1507 1508 1509
		u64 pa, void *p_data, int bytes)
{
	struct intel_vgpu *vgpu = spt->vgpu;
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
1510
	struct intel_gvt_gtt_entry we, se;
1511 1512 1513 1514 1515 1516 1517 1518
	unsigned long index;
	int ret;

	index = (pa & (PAGE_SIZE - 1)) >> info->gtt_entry_size_shift;

	ppgtt_get_guest_entry(spt, &we, index);

	if (bytes == info->gtt_entry_size) {
1519
		ret = ppgtt_handle_guest_write_page_table(spt, &we, index);
1520 1521 1522 1523
		if (ret)
			return ret;
	} else {
		if (!test_bit(index, spt->post_shadow_bitmap)) {
1524 1525
			int type = spt->shadow_page.type;

1526
			ppgtt_get_shadow_entry(spt, &se, index);
1527
			ret = ppgtt_handle_guest_entry_removal(spt, &se, index);
1528 1529
			if (ret)
				return ret;
1530 1531
			ops->set_pfn(&se, vgpu->gtt.scratch_pt[type].page_mfn);
			ppgtt_set_shadow_entry(spt, &se, index);
1532 1533 1534 1535 1536 1537 1538
		}
		ppgtt_set_post_shadow(spt, index);
	}

	if (!enable_out_of_sync)
		return 0;

1539
	spt->guest_page.write_cnt++;
1540

1541 1542
	if (spt->guest_page.oos_page)
		ops->set_entry(spt->guest_page.oos_page->mem, &we, index,
1543 1544
				false, 0, vgpu);

1545 1546 1547
	if (can_do_out_of_sync(spt)) {
		if (!spt->guest_page.oos_page)
			ppgtt_allocate_oos_page(spt);
1548

1549
		ret = ppgtt_set_guest_page_oos(spt);
1550 1551 1552 1553 1554 1555
		if (ret < 0)
			return ret;
	}
	return 0;
}

1556
static void invalidate_ppgtt_mm(struct intel_vgpu_mm *mm)
1557 1558 1559 1560 1561 1562
{
	struct intel_vgpu *vgpu = mm->vgpu;
	struct intel_gvt *gvt = vgpu->gvt;
	struct intel_gvt_gtt *gtt = &gvt->gtt;
	struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops;
	struct intel_gvt_gtt_entry se;
1563
	int index;
1564

1565
	if (!mm->ppgtt_mm.shadowed)
1566 1567
		return;

1568 1569 1570
	for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.shadow_pdps); index++) {
		ppgtt_get_shadow_root_entry(mm, &se, index);

1571 1572
		if (!ops->test_present(&se))
			continue;
1573

1574
		ppgtt_invalidate_spt_by_shadow_entry(vgpu, &se);
1575
		se.val64 = 0;
1576
		ppgtt_set_shadow_root_entry(mm, &se, index);
1577

1578 1579
		trace_spt_guest_change(vgpu->id, "destroy root pointer",
				       NULL, se.type, se.val64, index);
1580 1581
	}

1582
	mm->ppgtt_mm.shadowed = false;
1583 1584
}

1585 1586

static int shadow_ppgtt_mm(struct intel_vgpu_mm *mm)
1587 1588 1589 1590 1591 1592 1593
{
	struct intel_vgpu *vgpu = mm->vgpu;
	struct intel_gvt *gvt = vgpu->gvt;
	struct intel_gvt_gtt *gtt = &gvt->gtt;
	struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops;
	struct intel_vgpu_ppgtt_spt *spt;
	struct intel_gvt_gtt_entry ge, se;
1594
	int index, ret;
1595

1596
	if (mm->ppgtt_mm.shadowed)
1597 1598
		return 0;

1599 1600 1601 1602
	mm->ppgtt_mm.shadowed = true;

	for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.guest_pdps); index++) {
		ppgtt_get_guest_root_entry(mm, &ge, index);
1603 1604 1605 1606

		if (!ops->test_present(&ge))
			continue;

1607 1608
		trace_spt_guest_change(vgpu->id, __func__, NULL,
				       ge.type, ge.val64, index);
1609

1610
		spt = ppgtt_populate_spt_by_guest_entry(vgpu, &ge);
1611
		if (IS_ERR(spt)) {
1612
			gvt_vgpu_err("fail to populate guest root pointer\n");
1613 1614 1615 1616
			ret = PTR_ERR(spt);
			goto fail;
		}
		ppgtt_generate_shadow_entry(&se, spt, &ge);
1617
		ppgtt_set_shadow_root_entry(mm, &se, index);
1618

1619 1620
		trace_spt_guest_change(vgpu->id, "populate root pointer",
				       NULL, se.type, se.val64, index);
1621
	}
1622

1623 1624
	return 0;
fail:
1625
	invalidate_ppgtt_mm(mm);
1626 1627 1628
	return ret;
}

1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
static struct intel_vgpu_mm *vgpu_alloc_mm(struct intel_vgpu *vgpu)
{
	struct intel_vgpu_mm *mm;

	mm = kzalloc(sizeof(*mm), GFP_KERNEL);
	if (!mm)
		return NULL;

	mm->vgpu = vgpu;
	kref_init(&mm->ref);
	atomic_set(&mm->pincount, 0);

	return mm;
}

static void vgpu_free_mm(struct intel_vgpu_mm *mm)
{
	kfree(mm);
}

1649
/**
1650
 * intel_vgpu_create_ppgtt_mm - create a ppgtt mm object for a vGPU
1651
 * @vgpu: a vGPU
1652 1653
 * @root_entry_type: ppgtt root entry type
 * @pdps: guest pdps.
1654
 *
1655
 * This function is used to create a ppgtt mm object for a vGPU.
1656 1657 1658 1659
 *
 * Returns:
 * Zero on success, negative error code in pointer if failed.
 */
1660 1661
struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
		intel_gvt_gtt_type_t root_entry_type, u64 pdps[])
1662 1663 1664 1665 1666
{
	struct intel_gvt *gvt = vgpu->gvt;
	struct intel_vgpu_mm *mm;
	int ret;

1667 1668 1669
	mm = vgpu_alloc_mm(vgpu);
	if (!mm)
		return ERR_PTR(-ENOMEM);
1670

1671
	mm->type = INTEL_GVT_MM_PPGTT;
1672

1673 1674 1675
	GEM_BUG_ON(root_entry_type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY &&
		   root_entry_type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY);
	mm->ppgtt_mm.root_entry_type = root_entry_type;
1676

1677 1678
	INIT_LIST_HEAD(&mm->ppgtt_mm.list);
	INIT_LIST_HEAD(&mm->ppgtt_mm.lru_list);
1679

1680 1681 1682 1683 1684
	if (root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY)
		mm->ppgtt_mm.guest_pdps[0] = pdps[0];
	else
		memcpy(mm->ppgtt_mm.guest_pdps, pdps,
		       sizeof(mm->ppgtt_mm.guest_pdps));
1685

1686
	ret = shadow_ppgtt_mm(mm);
1687
	if (ret) {
1688 1689 1690
		gvt_vgpu_err("failed to shadow ppgtt mm\n");
		vgpu_free_mm(mm);
		return ERR_PTR(ret);
1691 1692
	}

1693 1694 1695 1696
	list_add_tail(&mm->ppgtt_mm.list, &vgpu->gtt.ppgtt_mm_list_head);
	list_add_tail(&mm->ppgtt_mm.lru_list, &gvt->gtt.ppgtt_mm_lru_list_head);
	return mm;
}
1697

1698 1699 1700 1701
static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu)
{
	struct intel_vgpu_mm *mm;
	unsigned long nr_entries;
1702

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
	mm = vgpu_alloc_mm(vgpu);
	if (!mm)
		return ERR_PTR(-ENOMEM);

	mm->type = INTEL_GVT_MM_GGTT;

	nr_entries = gvt_ggtt_gm_sz(vgpu->gvt) >> I915_GTT_PAGE_SHIFT;
	mm->ggtt_mm.virtual_ggtt = vzalloc(nr_entries *
					vgpu->gvt->device_info.gtt_entry_size);
	if (!mm->ggtt_mm.virtual_ggtt) {
		vgpu_free_mm(mm);
		return ERR_PTR(-ENOMEM);
1715
	}
1716

1717
	return mm;
1718 1719 1720
}

/**
1721
 * _intel_vgpu_mm_release - destroy a mm object
1722 1723 1724 1725 1726
 * @mm_ref: a kref object
 *
 * This function is used to destroy a mm object for vGPU
 *
 */
1727
void _intel_vgpu_mm_release(struct kref *mm_ref)
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
{
	struct intel_vgpu_mm *mm = container_of(mm_ref, typeof(*mm), ref);

	if (GEM_WARN_ON(atomic_read(&mm->pincount)))
		gvt_err("vgpu mm pin count bug detected\n");

	if (mm->type == INTEL_GVT_MM_PPGTT) {
		list_del(&mm->ppgtt_mm.list);
		list_del(&mm->ppgtt_mm.lru_list);
		invalidate_ppgtt_mm(mm);
	} else {
		vfree(mm->ggtt_mm.virtual_ggtt);
	}

	vgpu_free_mm(mm);
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
}

/**
 * intel_vgpu_unpin_mm - decrease the pin count of a vGPU mm object
 * @mm: a vGPU mm object
 *
 * This function is called when user doesn't want to use a vGPU mm object
 */
void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm)
{
	atomic_dec(&mm->pincount);
}

/**
 * intel_vgpu_pin_mm - increase the pin count of a vGPU mm object
 * @vgpu: a vGPU
 *
 * This function is called when user wants to use a vGPU mm object. If this
 * mm object hasn't been shadowed yet, the shadow will be populated at this
 * time.
 *
 * Returns:
 * Zero on success, negative error code if failed.
 */
int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm)
{
	int ret;

1771
	atomic_inc(&mm->pincount);
1772

1773 1774
	if (mm->type == INTEL_GVT_MM_PPGTT) {
		ret = shadow_ppgtt_mm(mm);
1775 1776
		if (ret)
			return ret;
1777 1778 1779 1780

		list_move_tail(&mm->ppgtt_mm.lru_list,
			       &mm->vgpu->gvt->gtt.ppgtt_mm_lru_list_head);

1781 1782 1783 1784 1785
	}

	return 0;
}

1786
static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt)
1787 1788 1789 1790
{
	struct intel_vgpu_mm *mm;
	struct list_head *pos, *n;

1791 1792
	list_for_each_safe(pos, n, &gvt->gtt.ppgtt_mm_lru_list_head) {
		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.lru_list);
1793 1794 1795 1796

		if (atomic_read(&mm->pincount))
			continue;

1797 1798
		list_del_init(&mm->ppgtt_mm.lru_list);
		invalidate_ppgtt_mm(mm);
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
		return 1;
	}
	return 0;
}

/*
 * GMA translation APIs.
 */
static inline int ppgtt_get_next_level_entry(struct intel_vgpu_mm *mm,
		struct intel_gvt_gtt_entry *e, unsigned long index, bool guest)
{
	struct intel_vgpu *vgpu = mm->vgpu;
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
	struct intel_vgpu_ppgtt_spt *s;

1814
	s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e));
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
	if (!s)
		return -ENXIO;

	if (!guest)
		ppgtt_get_shadow_entry(s, e, index);
	else
		ppgtt_get_guest_entry(s, e, index);
	return 0;
}

/**
 * intel_vgpu_gma_to_gpa - translate a gma to GPA
 * @mm: mm object. could be a PPGTT or GGTT mm object
 * @gma: graphics memory address in this mm object
 *
 * This function is used to translate a graphics memory address in specific
 * graphics memory space to guest physical address.
 *
 * Returns:
 * Guest physical address on success, INTEL_GVT_INVALID_ADDR if failed.
 */
unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm, unsigned long gma)
{
	struct intel_vgpu *vgpu = mm->vgpu;
	struct intel_gvt *gvt = vgpu->gvt;
	struct intel_gvt_gtt_pte_ops *pte_ops = gvt->gtt.pte_ops;
	struct intel_gvt_gtt_gma_ops *gma_ops = gvt->gtt.gma_ops;
	unsigned long gpa = INTEL_GVT_INVALID_ADDR;
	unsigned long gma_index[4];
	struct intel_gvt_gtt_entry e;
1845
	int i, levels = 0;
1846 1847
	int ret;

1848 1849
	GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT &&
		   mm->type != INTEL_GVT_MM_PPGTT);
1850 1851 1852 1853 1854

	if (mm->type == INTEL_GVT_MM_GGTT) {
		if (!vgpu_gmadr_is_valid(vgpu, gma))
			goto err;

1855 1856 1857
		ggtt_get_guest_entry(mm, &e,
			gma_ops->gma_to_ggtt_pte_index(gma));

Z
Zhi Wang 已提交
1858 1859
		gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT)
			+ (gma & ~I915_GTT_PAGE_MASK);
1860 1861

		trace_gma_translate(vgpu->id, "ggtt", 0, 0, gma, gpa);
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
	} else {
		switch (mm->ppgtt_mm.root_entry_type) {
		case GTT_TYPE_PPGTT_ROOT_L4_ENTRY:
			ppgtt_get_shadow_root_entry(mm, &e, 0);

			gma_index[0] = gma_ops->gma_to_pml4_index(gma);
			gma_index[1] = gma_ops->gma_to_l4_pdp_index(gma);
			gma_index[2] = gma_ops->gma_to_pde_index(gma);
			gma_index[3] = gma_ops->gma_to_pte_index(gma);
			levels = 4;
			break;
		case GTT_TYPE_PPGTT_ROOT_L3_ENTRY:
			ppgtt_get_shadow_root_entry(mm, &e,
					gma_ops->gma_to_l3_pdp_index(gma));

			gma_index[0] = gma_ops->gma_to_pde_index(gma);
			gma_index[1] = gma_ops->gma_to_pte_index(gma);
			levels = 2;
			break;
		default:
			GEM_BUG_ON(1);
		}
1884

1885 1886 1887 1888 1889 1890
		/* walk the shadow page table and get gpa from guest entry */
		for (i = 0; i < levels; i++) {
			ret = ppgtt_get_next_level_entry(mm, &e, gma_index[i],
				(i == levels - 1));
			if (ret)
				goto err;
1891

1892 1893 1894 1895
			if (!pte_ops->test_present(&e)) {
				gvt_dbg_core("GMA 0x%lx is not present\n", gma);
				goto err;
			}
1896
		}
1897

1898 1899 1900 1901 1902
		gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT) +
					(gma & ~I915_GTT_PAGE_MASK);
		trace_gma_translate(vgpu->id, "ppgtt", 0,
				    mm->ppgtt_mm.root_entry_type, gma, gpa);
	}
1903 1904 1905

	return gpa;
err:
1906
	gvt_vgpu_err("invalid mm type: %d gma %lx\n", mm->type, gma);
1907 1908 1909
	return INTEL_GVT_INVALID_ADDR;
}

1910
static int emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
	unsigned int off, void *p_data, unsigned int bytes)
{
	struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm;
	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
	unsigned long index = off >> info->gtt_entry_size_shift;
	struct intel_gvt_gtt_entry e;

	if (bytes != 4 && bytes != 8)
		return -EINVAL;

	ggtt_get_guest_entry(ggtt_mm, &e, index);
	memcpy(p_data, (void *)&e.val64 + (off & (info->gtt_entry_size - 1)),
			bytes);
	return 0;
}

/**
 * intel_vgpu_emulate_gtt_mmio_read - emulate GTT MMIO register read
 * @vgpu: a vGPU
 * @off: register offset
 * @p_data: data will be returned to guest
 * @bytes: data length
 *
 * This function is used to emulate the GTT MMIO register read
 *
 * Returns:
 * Zero on success, error code if failed.
 */
1939
int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu, unsigned int off,
1940 1941 1942 1943 1944 1945 1946 1947 1948
	void *p_data, unsigned int bytes)
{
	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
	int ret;

	if (bytes != 4 && bytes != 8)
		return -EINVAL;

	off -= info->gtt_start_offset;
1949
	ret = emulate_ggtt_mmio_read(vgpu, off, p_data, bytes);
1950 1951 1952
	return ret;
}

1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
static void ggtt_invalidate_pte(struct intel_vgpu *vgpu,
		struct intel_gvt_gtt_entry *entry)
{
	struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
	unsigned long pfn;

	pfn = pte_ops->get_pfn(entry);
	if (pfn != vgpu->gvt->gtt.scratch_mfn)
		intel_gvt_hypervisor_dma_unmap_guest_page(vgpu,
						pfn << PAGE_SHIFT);
}

1965
static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off,
1966 1967 1968 1969 1970 1971 1972
	void *p_data, unsigned int bytes)
{
	struct intel_gvt *gvt = vgpu->gvt;
	const struct intel_gvt_device_info *info = &gvt->device_info;
	struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm;
	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
	unsigned long g_gtt_index = off >> info->gtt_entry_size_shift;
1973
	unsigned long gma, gfn;
1974
	struct intel_gvt_gtt_entry e, m;
1975 1976
	dma_addr_t dma_addr;
	int ret;
1977 1978 1979 1980

	if (bytes != 4 && bytes != 8)
		return -EINVAL;

Z
Zhi Wang 已提交
1981
	gma = g_gtt_index << I915_GTT_PAGE_SHIFT;
1982 1983

	/* the VM may configure the whole GM space when ballooning is used */
1984
	if (!vgpu_gmadr_is_valid(vgpu, gma))
1985 1986 1987 1988 1989 1990 1991 1992
		return 0;

	ggtt_get_guest_entry(ggtt_mm, &e, g_gtt_index);

	memcpy((void *)&e.val64 + (off & (info->gtt_entry_size - 1)), p_data,
			bytes);

	if (ops->test_present(&e)) {
1993
		gfn = ops->get_pfn(&e);
1994
		m = e;
1995 1996 1997 1998 1999 2000 2001 2002 2003

		/* one PTE update may be issued in multiple writes and the
		 * first write may not construct a valid gfn
		 */
		if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
			ops->set_pfn(&m, gvt->gtt.scratch_mfn);
			goto out;
		}

2004 2005 2006
		ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn,
							      &dma_addr);
		if (ret) {
2007
			gvt_vgpu_err("fail to populate guest ggtt entry\n");
2008 2009 2010 2011
			/* guest driver may read/write the entry when partial
			 * update the entry in this situation p2m will fail
			 * settting the shadow entry to point to a scratch page
			 */
2012
			ops->set_pfn(&m, gvt->gtt.scratch_mfn);
2013
		} else
2014
			ops->set_pfn(&m, dma_addr >> PAGE_SHIFT);
2015 2016 2017
	} else {
		ggtt_get_host_entry(ggtt_mm, &m, g_gtt_index);
		ggtt_invalidate_pte(vgpu, &m);
2018
		ops->set_pfn(&m, gvt->gtt.scratch_mfn);
2019 2020
		ops->clear_present(&m);
	}
2021

2022
out:
2023
	ggtt_set_host_entry(ggtt_mm, &m, g_gtt_index);
2024
	ggtt_invalidate(gvt->dev_priv);
2025 2026 2027 2028 2029
	ggtt_set_guest_entry(ggtt_mm, &e, g_gtt_index);
	return 0;
}

/*
2030
 * intel_vgpu_emulate_ggtt_mmio_write - emulate GTT MMIO register write
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
 * @vgpu: a vGPU
 * @off: register offset
 * @p_data: data from guest write
 * @bytes: data length
 *
 * This function is used to emulate the GTT MMIO register write
 *
 * Returns:
 * Zero on success, error code if failed.
 */
2041 2042
int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
		unsigned int off, void *p_data, unsigned int bytes)
2043 2044 2045 2046 2047 2048 2049 2050
{
	const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
	int ret;

	if (bytes != 4 && bytes != 8)
		return -EINVAL;

	off -= info->gtt_start_offset;
2051
	ret = emulate_ggtt_mmio_write(vgpu, off, p_data, bytes);
2052 2053 2054
	return ret;
}

2055 2056
static int alloc_scratch_pages(struct intel_vgpu *vgpu,
		intel_gvt_gtt_type_t type)
2057 2058
{
	struct intel_vgpu_gtt *gtt = &vgpu->gtt;
2059
	struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
2060
	int page_entry_num = I915_GTT_PAGE_SIZE >>
2061
				vgpu->gvt->device_info.gtt_entry_size_shift;
J
Jike Song 已提交
2062
	void *scratch_pt;
2063
	int i;
2064 2065
	struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
	dma_addr_t daddr;
2066

2067 2068 2069
	if (WARN_ON(type < GTT_TYPE_PPGTT_PTE_PT || type >= GTT_TYPE_MAX))
		return -EINVAL;

J
Jike Song 已提交
2070
	scratch_pt = (void *)get_zeroed_page(GFP_KERNEL);
2071
	if (!scratch_pt) {
2072
		gvt_vgpu_err("fail to allocate scratch page\n");
2073 2074 2075
		return -ENOMEM;
	}

2076 2077 2078
	daddr = dma_map_page(dev, virt_to_page(scratch_pt), 0,
			4096, PCI_DMA_BIDIRECTIONAL);
	if (dma_mapping_error(dev, daddr)) {
2079
		gvt_vgpu_err("fail to dmamap scratch_pt\n");
2080 2081
		__free_page(virt_to_page(scratch_pt));
		return -ENOMEM;
2082
	}
2083
	gtt->scratch_pt[type].page_mfn =
2084
		(unsigned long)(daddr >> I915_GTT_PAGE_SHIFT);
J
Jike Song 已提交
2085
	gtt->scratch_pt[type].page = virt_to_page(scratch_pt);
2086
	gvt_dbg_mm("vgpu%d create scratch_pt: type %d mfn=0x%lx\n",
2087
			vgpu->id, type, gtt->scratch_pt[type].page_mfn);
2088 2089 2090 2091 2092 2093

	/* Build the tree by full filled the scratch pt with the entries which
	 * point to the next level scratch pt or scratch page. The
	 * scratch_pt[type] indicate the scratch pt/scratch page used by the
	 * 'type' pt.
	 * e.g. scratch_pt[GTT_TYPE_PPGTT_PDE_PT] is used by
J
Jike Song 已提交
2094
	 * GTT_TYPE_PPGTT_PDE_PT level pt, that means this scratch_pt it self
2095 2096
	 * is GTT_TYPE_PPGTT_PTE_PT, and full filled by scratch page mfn.
	 */
2097
	if (type > GTT_TYPE_PPGTT_PTE_PT) {
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
		struct intel_gvt_gtt_entry se;

		memset(&se, 0, sizeof(struct intel_gvt_gtt_entry));
		se.type = get_entry_type(type - 1);
		ops->set_pfn(&se, gtt->scratch_pt[type - 1].page_mfn);

		/* The entry parameters like present/writeable/cache type
		 * set to the same as i915's scratch page tree.
		 */
		se.val64 |= _PAGE_PRESENT | _PAGE_RW;
		if (type == GTT_TYPE_PPGTT_PDE_PT)
2109
			se.val64 |= PPAT_CACHED;
2110 2111

		for (i = 0; i < page_entry_num; i++)
J
Jike Song 已提交
2112
			ops->set_entry(scratch_pt, &se, i, false, 0, vgpu);
2113 2114 2115 2116
	}

	return 0;
}
2117

2118 2119 2120
static int release_scratch_page_tree(struct intel_vgpu *vgpu)
{
	int i;
2121 2122
	struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
	dma_addr_t daddr;
2123 2124 2125

	for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) {
		if (vgpu->gtt.scratch_pt[i].page != NULL) {
2126
			daddr = (dma_addr_t)(vgpu->gtt.scratch_pt[i].page_mfn <<
2127
					I915_GTT_PAGE_SHIFT);
2128
			dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
2129 2130 2131 2132
			__free_page(vgpu->gtt.scratch_pt[i].page);
			vgpu->gtt.scratch_pt[i].page = NULL;
			vgpu->gtt.scratch_pt[i].page_mfn = 0;
		}
2133 2134 2135 2136 2137
	}

	return 0;
}

2138
static int create_scratch_page_tree(struct intel_vgpu *vgpu)
2139
{
2140 2141 2142 2143 2144 2145
	int i, ret;

	for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) {
		ret = alloc_scratch_pages(vgpu, i);
		if (ret)
			goto err;
2146
	}
2147 2148 2149 2150 2151 2152

	return 0;

err:
	release_scratch_page_tree(vgpu);
	return ret;
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
}

/**
 * intel_vgpu_init_gtt - initialize per-vGPU graphics memory virulization
 * @vgpu: a vGPU
 *
 * This function is used to initialize per-vGPU graphics memory virtualization
 * components.
 *
 * Returns:
 * Zero on success, error code if failed.
 */
int intel_vgpu_init_gtt(struct intel_vgpu *vgpu)
{
	struct intel_vgpu_gtt *gtt = &vgpu->gtt;

2169
	INIT_RADIX_TREE(&gtt->spt_tree, GFP_KERNEL);
2170

2171
	INIT_LIST_HEAD(&gtt->ppgtt_mm_list_head);
2172 2173 2174
	INIT_LIST_HEAD(&gtt->oos_page_list_head);
	INIT_LIST_HEAD(&gtt->post_shadow_list_head);

2175 2176
	gtt->ggtt_mm = intel_vgpu_create_ggtt_mm(vgpu);
	if (IS_ERR(gtt->ggtt_mm)) {
2177
		gvt_vgpu_err("fail to create mm for ggtt.\n");
2178
		return PTR_ERR(gtt->ggtt_mm);
2179 2180
	}

2181
	intel_vgpu_reset_ggtt(vgpu, false);
2182

2183
	return create_scratch_page_tree(vgpu);
2184 2185
}

2186
static void intel_vgpu_destroy_all_ppgtt_mm(struct intel_vgpu *vgpu)
2187 2188 2189 2190
{
	struct list_head *pos, *n;
	struct intel_vgpu_mm *mm;

2191 2192
	list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) {
		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
2193
		intel_vgpu_destroy_mm(mm);
2194
	}
2195 2196

	if (GEM_WARN_ON(!list_empty(&vgpu->gtt.ppgtt_mm_list_head)))
2197
		gvt_err("vgpu ppgtt mm is not fully destroyed\n");
2198

2199
	if (GEM_WARN_ON(!radix_tree_empty(&vgpu->gtt.spt_tree))) {
2200
		gvt_err("Why we still has spt not freed?\n");
2201
		ppgtt_free_all_spt(vgpu);
2202 2203 2204 2205 2206
	}
}

static void intel_vgpu_destroy_ggtt_mm(struct intel_vgpu *vgpu)
{
2207
	intel_vgpu_destroy_mm(vgpu->gtt.ggtt_mm);
2208
	vgpu->gtt.ggtt_mm = NULL;
2209 2210
}

2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
/**
 * intel_vgpu_clean_gtt - clean up per-vGPU graphics memory virulization
 * @vgpu: a vGPU
 *
 * This function is used to clean up per-vGPU graphics memory virtualization
 * components.
 *
 * Returns:
 * Zero on success, error code if failed.
 */
void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu)
{
2223 2224
	intel_vgpu_destroy_all_ppgtt_mm(vgpu);
	intel_vgpu_destroy_ggtt_mm(vgpu);
2225
	release_scratch_page_tree(vgpu);
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
}

static void clean_spt_oos(struct intel_gvt *gvt)
{
	struct intel_gvt_gtt *gtt = &gvt->gtt;
	struct list_head *pos, *n;
	struct intel_vgpu_oos_page *oos_page;

	WARN(!list_empty(&gtt->oos_page_use_list_head),
		"someone is still using oos page\n");

	list_for_each_safe(pos, n, &gtt->oos_page_free_list_head) {
		oos_page = container_of(pos, struct intel_vgpu_oos_page, list);
		list_del(&oos_page->list);
		kfree(oos_page);
	}
}

static int setup_spt_oos(struct intel_gvt *gvt)
{
	struct intel_gvt_gtt *gtt = &gvt->gtt;
	struct intel_vgpu_oos_page *oos_page;
	int i;
	int ret;

	INIT_LIST_HEAD(&gtt->oos_page_free_list_head);
	INIT_LIST_HEAD(&gtt->oos_page_use_list_head);

	for (i = 0; i < preallocated_oos_pages; i++) {
		oos_page = kzalloc(sizeof(*oos_page), GFP_KERNEL);
		if (!oos_page) {
			ret = -ENOMEM;
			goto fail;
		}

		INIT_LIST_HEAD(&oos_page->list);
		INIT_LIST_HEAD(&oos_page->vm_list);
		oos_page->id = i;
		list_add_tail(&oos_page->list, &gtt->oos_page_free_list_head);
	}

	gvt_dbg_mm("%d oos pages preallocated\n", i);

	return 0;
fail:
	clean_spt_oos(gvt);
	return ret;
}

/**
 * intel_vgpu_find_ppgtt_mm - find a PPGTT mm object
 * @vgpu: a vGPU
 * @page_table_level: PPGTT page table level
 * @root_entry: PPGTT page table root pointers
 *
 * This function is used to find a PPGTT mm object from mm object pool
 *
 * Returns:
 * pointer to mm object on success, NULL if failed.
 */
struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
2287
		u64 pdps[])
2288 2289
{
	struct intel_vgpu_mm *mm;
2290
	struct list_head *pos;
2291

2292 2293
	list_for_each(pos, &vgpu->gtt.ppgtt_mm_list_head) {
		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
2294

2295 2296 2297
		switch (mm->ppgtt_mm.root_entry_type) {
		case GTT_TYPE_PPGTT_ROOT_L4_ENTRY:
			if (pdps[0] == mm->ppgtt_mm.guest_pdps[0])
2298
				return mm;
2299 2300 2301 2302
			break;
		case GTT_TYPE_PPGTT_ROOT_L3_ENTRY:
			if (!memcmp(pdps, mm->ppgtt_mm.guest_pdps,
				    sizeof(mm->ppgtt_mm.guest_pdps)))
2303
				return mm;
2304 2305 2306
			break;
		default:
			GEM_BUG_ON(1);
2307 2308 2309 2310 2311 2312
		}
	}
	return NULL;
}

/**
2313
 * intel_vgpu_get_ppgtt_mm - get or create a PPGTT mm object.
2314
 * @vgpu: a vGPU
2315 2316
 * @root_entry_type: ppgtt root entry type
 * @pdps: guest pdps
2317
 *
2318
 * This function is used to find or create a PPGTT mm object from a guest.
2319 2320 2321 2322
 *
 * Returns:
 * Zero on success, negative error code if failed.
 */
2323
struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
2324
		intel_gvt_gtt_type_t root_entry_type, u64 pdps[])
2325 2326 2327
{
	struct intel_vgpu_mm *mm;

2328
	mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps);
2329
	if (mm) {
2330
		intel_vgpu_mm_get(mm);
2331
	} else {
2332
		mm = intel_vgpu_create_ppgtt_mm(vgpu, root_entry_type, pdps);
2333
		if (IS_ERR(mm))
2334
			gvt_vgpu_err("fail to create mm\n");
2335
	}
2336
	return mm;
2337 2338 2339
}

/**
2340
 * intel_vgpu_put_ppgtt_mm - find and put a PPGTT mm object.
2341
 * @vgpu: a vGPU
2342
 * @pdps: guest pdps
2343
 *
2344
 * This function is used to find a PPGTT mm object from a guest and destroy it.
2345 2346 2347 2348
 *
 * Returns:
 * Zero on success, negative error code if failed.
 */
2349
int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[])
2350 2351 2352
{
	struct intel_vgpu_mm *mm;

2353
	mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps);
2354
	if (!mm) {
2355
		gvt_vgpu_err("fail to find ppgtt instance.\n");
2356 2357
		return -EINVAL;
	}
2358
	intel_vgpu_mm_put(mm);
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
	return 0;
}

/**
 * intel_gvt_init_gtt - initialize mm components of a GVT device
 * @gvt: GVT device
 *
 * This function is called at the initialization stage, to initialize
 * the mm components of a GVT device.
 *
 * Returns:
 * zero on success, negative error code if failed.
 */
int intel_gvt_init_gtt(struct intel_gvt *gvt)
{
	int ret;
J
Jike Song 已提交
2375
	void *page;
2376 2377
	struct device *dev = &gvt->dev_priv->drm.pdev->dev;
	dma_addr_t daddr;
2378 2379 2380

	gvt_dbg_core("init gtt\n");

2381 2382
	gvt->gtt.pte_ops = &gen8_gtt_pte_ops;
	gvt->gtt.gma_ops = &gen8_gtt_gma_ops;
2383

J
Jike Song 已提交
2384 2385
	page = (void *)get_zeroed_page(GFP_KERNEL);
	if (!page) {
2386 2387 2388 2389
		gvt_err("fail to allocate scratch ggtt page\n");
		return -ENOMEM;
	}

2390 2391 2392 2393 2394 2395
	daddr = dma_map_page(dev, virt_to_page(page), 0,
			4096, PCI_DMA_BIDIRECTIONAL);
	if (dma_mapping_error(dev, daddr)) {
		gvt_err("fail to dmamap scratch ggtt page\n");
		__free_page(virt_to_page(page));
		return -ENOMEM;
2396
	}
2397 2398 2399

	gvt->gtt.scratch_page = virt_to_page(page);
	gvt->gtt.scratch_mfn = (unsigned long)(daddr >> I915_GTT_PAGE_SHIFT);
2400

2401 2402 2403 2404
	if (enable_out_of_sync) {
		ret = setup_spt_oos(gvt);
		if (ret) {
			gvt_err("fail to initialize SPT oos\n");
2405
			dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
2406
			__free_page(gvt->gtt.scratch_page);
2407 2408 2409
			return ret;
		}
	}
2410
	INIT_LIST_HEAD(&gvt->gtt.ppgtt_mm_lru_list_head);
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
	return 0;
}

/**
 * intel_gvt_clean_gtt - clean up mm components of a GVT device
 * @gvt: GVT device
 *
 * This function is called at the driver unloading stage, to clean up the
 * the mm components of a GVT device.
 *
 */
void intel_gvt_clean_gtt(struct intel_gvt *gvt)
{
2424
	struct device *dev = &gvt->dev_priv->drm.pdev->dev;
2425
	dma_addr_t daddr = (dma_addr_t)(gvt->gtt.scratch_mfn <<
Z
Zhi Wang 已提交
2426
					I915_GTT_PAGE_SHIFT);
2427 2428 2429

	dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL);

2430
	__free_page(gvt->gtt.scratch_page);
2431

2432 2433 2434
	if (enable_out_of_sync)
		clean_spt_oos(gvt);
}
2435

2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
/**
 * intel_vgpu_invalidate_ppgtt - invalidate PPGTT instances
 * @vgpu: a vGPU
 *
 * This function is called when invalidate all PPGTT instances of a vGPU.
 *
 */
void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu)
{
	struct list_head *pos, *n;
	struct intel_vgpu_mm *mm;

	list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) {
		mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list);
		if (mm->type == INTEL_GVT_MM_PPGTT) {
			list_del_init(&mm->ppgtt_mm.lru_list);
			if (mm->ppgtt_mm.shadowed)
				invalidate_ppgtt_mm(mm);
		}
	}
}

2458 2459 2460
/**
 * intel_vgpu_reset_ggtt - reset the GGTT entry
 * @vgpu: a vGPU
2461
 * @invalidate_old: invalidate old entries
2462 2463 2464 2465 2466
 *
 * This function is called at the vGPU create stage
 * to reset all the GGTT entries.
 *
 */
2467
void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old)
2468 2469
{
	struct intel_gvt *gvt = vgpu->gvt;
2470
	struct drm_i915_private *dev_priv = gvt->dev_priv;
2471 2472
	struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops;
	struct intel_gvt_gtt_entry entry = {.type = GTT_TYPE_GGTT_PTE};
2473
	struct intel_gvt_gtt_entry old_entry;
2474 2475 2476
	u32 index;
	u32 num_entries;

2477 2478
	pte_ops->set_pfn(&entry, gvt->gtt.scratch_mfn);
	pte_ops->set_present(&entry);
2479 2480 2481

	index = vgpu_aperture_gmadr_base(vgpu) >> PAGE_SHIFT;
	num_entries = vgpu_aperture_sz(vgpu) >> PAGE_SHIFT;
2482 2483 2484 2485 2486
	while (num_entries--) {
		if (invalidate_old) {
			ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index);
			ggtt_invalidate_pte(vgpu, &old_entry);
		}
2487
		ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++);
2488
	}
2489 2490 2491

	index = vgpu_hidden_gmadr_base(vgpu) >> PAGE_SHIFT;
	num_entries = vgpu_hidden_sz(vgpu) >> PAGE_SHIFT;
2492 2493 2494 2495 2496
	while (num_entries--) {
		if (invalidate_old) {
			ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index);
			ggtt_invalidate_pte(vgpu, &old_entry);
		}
2497
		ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++);
2498
	}
2499

2500
	ggtt_invalidate(dev_priv);
2501
}
2502 2503 2504 2505 2506 2507 2508 2509 2510

/**
 * intel_vgpu_reset_gtt - reset the all GTT related status
 * @vgpu: a vGPU
 *
 * This function is called from vfio core to reset reset all
 * GTT related status, including GGTT, PPGTT, scratch page.
 *
 */
2511
void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu)
2512
{
2513 2514 2515 2516
	/* Shadow pages are only created when there is no page
	 * table tracking data, so remove page tracking data after
	 * removing the shadow pages.
	 */
2517
	intel_vgpu_destroy_all_ppgtt_mm(vgpu);
2518
	intel_vgpu_reset_ggtt(vgpu, true);
2519
}