book3s_64_mmu.c 15.8 KB
Newer Older
A
Alexander Graf 已提交
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
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright SUSE Linux Products GmbH 2009
 *
 * Authors: Alexander Graf <agraf@suse.de>
 */

#include <linux/types.h>
#include <linux/string.h>
#include <linux/kvm.h>
#include <linux/kvm_host.h>
#include <linux/highmem.h>

#include <asm/tlbflush.h>
#include <asm/kvm_ppc.h>
#include <asm/kvm_book3s.h>
29
#include <asm/book3s/64/mmu-hash.h>
A
Alexander Graf 已提交
30 31 32 33 34 35 36 37 38 39 40

/* #define DEBUG_MMU */

#ifdef DEBUG_MMU
#define dprintk(X...) printk(KERN_INFO X)
#else
#define dprintk(X...) do { } while(0)
#endif

static void kvmppc_mmu_book3s_64_reset_msr(struct kvm_vcpu *vcpu)
{
41
	kvmppc_set_msr(vcpu, vcpu->arch.intr_msr);
A
Alexander Graf 已提交
42 43 44
}

static struct kvmppc_slb *kvmppc_mmu_book3s_64_find_slbe(
45
				struct kvm_vcpu *vcpu,
A
Alexander Graf 已提交
46 47 48 49 50 51
				gva_t eaddr)
{
	int i;
	u64 esid = GET_ESID(eaddr);
	u64 esid_1t = GET_ESID_1T(eaddr);

52
	for (i = 0; i < vcpu->arch.slb_nr; i++) {
A
Alexander Graf 已提交
53 54
		u64 cmp_esid = esid;

55
		if (!vcpu->arch.slb[i].valid)
A
Alexander Graf 已提交
56 57
			continue;

58
		if (vcpu->arch.slb[i].tb)
A
Alexander Graf 已提交
59 60
			cmp_esid = esid_1t;

61 62
		if (vcpu->arch.slb[i].esid == cmp_esid)
			return &vcpu->arch.slb[i];
A
Alexander Graf 已提交
63 64 65 66
	}

	dprintk("KVM: No SLB entry found for 0x%lx [%llx | %llx]\n",
		eaddr, esid, esid_1t);
67 68
	for (i = 0; i < vcpu->arch.slb_nr; i++) {
	    if (vcpu->arch.slb[i].vsid)
A
Alexander Graf 已提交
69
		dprintk("  %d: %c%c%c %llx %llx\n", i,
70 71 72 73 74
			vcpu->arch.slb[i].valid ? 'v' : ' ',
			vcpu->arch.slb[i].large ? 'l' : ' ',
			vcpu->arch.slb[i].tb    ? 't' : ' ',
			vcpu->arch.slb[i].esid,
			vcpu->arch.slb[i].vsid);
A
Alexander Graf 已提交
75 76 77 78 79
	}

	return NULL;
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static int kvmppc_slb_sid_shift(struct kvmppc_slb *slbe)
{
	return slbe->tb ? SID_SHIFT_1T : SID_SHIFT;
}

static u64 kvmppc_slb_offset_mask(struct kvmppc_slb *slbe)
{
	return (1ul << kvmppc_slb_sid_shift(slbe)) - 1;
}

static u64 kvmppc_slb_calc_vpn(struct kvmppc_slb *slb, gva_t eaddr)
{
	eaddr &= kvmppc_slb_offset_mask(slb);

	return (eaddr >> VPN_SHIFT) |
		((slb->vsid) << (kvmppc_slb_sid_shift(slb) - VPN_SHIFT));
}

A
Alexander Graf 已提交
98 99 100 101 102
static u64 kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
					 bool data)
{
	struct kvmppc_slb *slb;

103
	slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
A
Alexander Graf 已提交
104 105 106
	if (!slb)
		return 0;

107
	return kvmppc_slb_calc_vpn(slb, eaddr);
A
Alexander Graf 已提交
108 109
}

110 111 112 113 114 115 116 117 118 119 120
static int mmu_pagesize(int mmu_pg)
{
	switch (mmu_pg) {
	case MMU_PAGE_64K:
		return 16;
	case MMU_PAGE_16M:
		return 24;
	}
	return 12;
}

A
Alexander Graf 已提交
121 122
static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb *slbe)
{
123
	return mmu_pagesize(slbe->base_page_size);
A
Alexander Graf 已提交
124 125 126 127 128
}

static u32 kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb *slbe, gva_t eaddr)
{
	int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
129 130

	return ((eaddr & kvmppc_slb_offset_mask(slbe)) >> p);
A
Alexander Graf 已提交
131 132
}

133
static hva_t kvmppc_mmu_book3s_64_get_pteg(struct kvm_vcpu *vcpu,
A
Alexander Graf 已提交
134 135 136
				struct kvmppc_slb *slbe, gva_t eaddr,
				bool second)
{
137
	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
A
Alexander Graf 已提交
138
	u64 hash, pteg, htabsize;
139
	u32 ssize;
A
Alexander Graf 已提交
140
	hva_t r;
141
	u64 vpn;
A
Alexander Graf 已提交
142 143 144

	htabsize = ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1);

145 146 147
	vpn = kvmppc_slb_calc_vpn(slbe, eaddr);
	ssize = slbe->tb ? MMU_SEGSIZE_1T : MMU_SEGSIZE_256M;
	hash = hpt_hash(vpn, kvmppc_mmu_book3s_64_get_pagesize(slbe), ssize);
A
Alexander Graf 已提交
148 149 150 151 152 153 154 155 156 157 158 159
	if (second)
		hash = ~hash;
	hash &= ((1ULL << 39ULL) - 1ULL);
	hash &= htabsize;
	hash <<= 7ULL;

	pteg = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL;
	pteg |= hash;

	dprintk("MMU: page=0x%x sdr1=0x%llx pteg=0x%llx vsid=0x%llx\n",
		page, vcpu_book3s->sdr1, pteg, slbe->vsid);

160 161
	/* When running a PAPR guest, SDR1 contains a HVA address instead
           of a GPA */
162
	if (vcpu->arch.papr_enabled)
163 164
		r = pteg;
	else
165
		r = gfn_to_hva(vcpu->kvm, pteg >> PAGE_SHIFT);
166

A
Alexander Graf 已提交
167 168 169 170 171 172 173 174 175 176 177
	if (kvm_is_error_hva(r))
		return r;
	return r | (pteg & ~PAGE_MASK);
}

static u64 kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb *slbe, gva_t eaddr)
{
	int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
	u64 avpn;

	avpn = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
178
	avpn |= slbe->vsid << (kvmppc_slb_sid_shift(slbe) - p);
A
Alexander Graf 已提交
179

180 181
	if (p < 16)
		avpn >>= ((80 - p) - 56) - 8;	/* 16 - p */
A
Alexander Graf 已提交
182
	else
183
		avpn <<= p - 16;
A
Alexander Graf 已提交
184 185 186 187

	return avpn;
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/*
 * Return page size encoded in the second word of a HPTE, or
 * -1 for an invalid encoding for the base page size indicated by
 * the SLB entry.  This doesn't handle mixed pagesize segments yet.
 */
static int decode_pagesize(struct kvmppc_slb *slbe, u64 r)
{
	switch (slbe->base_page_size) {
	case MMU_PAGE_64K:
		if ((r & 0xf000) == 0x1000)
			return MMU_PAGE_64K;
		break;
	case MMU_PAGE_16M:
		if ((r & 0xff000) == 0)
			return MMU_PAGE_16M;
		break;
	}
	return -1;
}

A
Alexander Graf 已提交
208
static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
209 210
				      struct kvmppc_pte *gpte, bool data,
				      bool iswrite)
A
Alexander Graf 已提交
211 212 213 214 215
{
	struct kvmppc_slb *slbe;
	hva_t ptegp;
	u64 pteg[16];
	u64 avpn = 0;
216 217 218
	u64 v, r;
	u64 v_val, v_mask;
	u64 eaddr_mask;
A
Alexander Graf 已提交
219
	int i;
220
	u8 pp, key = 0;
A
Alexander Graf 已提交
221
	bool found = false;
222
	bool second = false;
223
	int pgsize;
224 225 226 227 228
	ulong mp_ea = vcpu->arch.magic_page_ea;

	/* Magic page override */
	if (unlikely(mp_ea) &&
	    unlikely((eaddr & ~0xfffULL) == (mp_ea & ~0xfffULL)) &&
229
	    !(kvmppc_get_msr(vcpu) & MSR_PR)) {
230 231 232 233 234 235 236
		gpte->eaddr = eaddr;
		gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
		gpte->raddr = vcpu->arch.magic_page_pa | (gpte->raddr & 0xfff);
		gpte->raddr &= KVM_PAM;
		gpte->may_execute = true;
		gpte->may_read = true;
		gpte->may_write = true;
237
		gpte->page_size = MMU_PAGE_4K;
238
		gpte->wimg = HPTE_R_M;
239 240 241

		return 0;
	}
A
Alexander Graf 已提交
242

243
	slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
A
Alexander Graf 已提交
244 245 246
	if (!slbe)
		goto no_seg_found;

247
	avpn = kvmppc_mmu_book3s_64_get_avpn(slbe, eaddr);
248 249
	v_val = avpn & HPTE_V_AVPN;

250
	if (slbe->tb)
251 252 253 254 255 256 257
		v_val |= SLB_VSID_B_1T;
	if (slbe->large)
		v_val |= HPTE_V_LARGE;
	v_val |= HPTE_V_VALID;

	v_mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_LARGE | HPTE_V_VALID |
		HPTE_V_SECONDARY;
258

259 260
	pgsize = slbe->large ? MMU_PAGE_16M : MMU_PAGE_4K;

261 262
	mutex_lock(&vcpu->kvm->arch.hpt_mutex);

A
Alexander Graf 已提交
263
do_second:
264
	ptegp = kvmppc_mmu_book3s_64_get_pteg(vcpu, slbe, eaddr, second);
A
Alexander Graf 已提交
265 266 267 268
	if (kvm_is_error_hva(ptegp))
		goto no_page_found;

	if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
269 270
		printk_ratelimited(KERN_ERR
			"KVM: Can't copy data from 0x%lx!\n", ptegp);
A
Alexander Graf 已提交
271 272 273
		goto no_page_found;
	}

274
	if ((kvmppc_get_msr(vcpu) & MSR_PR) && slbe->Kp)
A
Alexander Graf 已提交
275
		key = 4;
276
	else if (!(kvmppc_get_msr(vcpu) & MSR_PR) && slbe->Ks)
A
Alexander Graf 已提交
277 278 279
		key = 4;

	for (i=0; i<16; i+=2) {
280 281 282
		u64 pte0 = be64_to_cpu(pteg[i]);
		u64 pte1 = be64_to_cpu(pteg[i + 1]);

283
		/* Check all relevant fields of 1st dword */
284
		if ((pte0 & v_mask) == v_val) {
285 286 287
			/* If large page bit is set, check pgsize encoding */
			if (slbe->large &&
			    (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) {
288
				pgsize = decode_pagesize(slbe, pte1);
289 290 291
				if (pgsize < 0)
					continue;
			}
A
Alexander Graf 已提交
292 293 294 295 296
			found = true;
			break;
		}
	}

297 298 299 300 301 302 303
	if (!found) {
		if (second)
			goto no_page_found;
		v_val |= HPTE_V_SECONDARY;
		second = true;
		goto do_second;
	}
A
Alexander Graf 已提交
304

305 306
	v = be64_to_cpu(pteg[i]);
	r = be64_to_cpu(pteg[i+1]);
307
	pp = (r & HPTE_R_PP) | key;
308 309
	if (r & HPTE_R_PP0)
		pp |= 8;
310 311 312

	gpte->eaddr = eaddr;
	gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
313 314

	eaddr_mask = (1ull << mmu_pagesize(pgsize)) - 1;
315
	gpte->raddr = (r & HPTE_R_RPN & ~eaddr_mask) | (eaddr & eaddr_mask);
316
	gpte->page_size = pgsize;
317
	gpte->may_execute = ((r & HPTE_R_N) ? false : true);
318 319 320
	if (unlikely(vcpu->arch.disable_kernel_nx) &&
	    !(kvmppc_get_msr(vcpu) & MSR_PR))
		gpte->may_execute = true;
321 322
	gpte->may_read = false;
	gpte->may_write = false;
323
	gpte->wimg = r & HPTE_R_WIMG;
324 325 326 327 328 329 330 331 332 333 334

	switch (pp) {
	case 0:
	case 1:
	case 2:
	case 6:
		gpte->may_write = true;
		/* fall through */
	case 3:
	case 5:
	case 7:
335
	case 10:
336 337 338
		gpte->may_read = true;
		break;
	}
A
Alexander Graf 已提交
339

340 341 342
	dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
		"-> 0x%lx\n",
		eaddr, avpn, gpte->vpage, gpte->raddr);
A
Alexander Graf 已提交
343

344 345
	/* Update PTE R and C bits, so the guest's swapper knows we used the
	 * page */
346 347 348 349 350 351 352 353
	if (gpte->may_read && !(r & HPTE_R_R)) {
		/*
		 * Set the accessed flag.
		 * We have to write this back with a single byte write
		 * because another vcpu may be accessing this on
		 * non-PAPR platforms such as mac99, and this is
		 * what real hardware does.
		 */
354
                char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
355
		r |= HPTE_R_R;
356
		put_user(r >> 8, addr + 6);
357
	}
358 359
	if (iswrite && gpte->may_write && !(r & HPTE_R_C)) {
		/* Set the dirty flag */
360
		/* Use a single byte write */
361
                char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
362
		r |= HPTE_R_C;
363
		put_user(r, addr + 7);
A
Alexander Graf 已提交
364 365
	}

366
	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
367

368
	if (!gpte->may_read || (iswrite && !gpte->may_write))
369 370 371
		return -EPERM;
	return 0;

A
Alexander Graf 已提交
372
no_page_found:
373
	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
A
Alexander Graf 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	return -ENOENT;

no_seg_found:
	dprintk("KVM MMU: Trigger segment fault\n");
	return -EINVAL;
}

static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu *vcpu, u64 rs, u64 rb)
{
	u64 esid, esid_1t;
	int slb_nr;
	struct kvmppc_slb *slbe;

	dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs, rb);

	esid = GET_ESID(rb);
	esid_1t = GET_ESID_1T(rb);
	slb_nr = rb & 0xfff;

393
	if (slb_nr > vcpu->arch.slb_nr)
A
Alexander Graf 已提交
394 395
		return;

396
	slbe = &vcpu->arch.slb[slb_nr];
A
Alexander Graf 已提交
397 398

	slbe->large = (rs & SLB_VSID_L) ? 1 : 0;
A
Alexander Graf 已提交
399 400
	slbe->tb    = (rs & SLB_VSID_B_1T) ? 1 : 0;
	slbe->esid  = slbe->tb ? esid_1t : esid;
401
	slbe->vsid  = (rs & ~SLB_VSID_B) >> (kvmppc_slb_sid_shift(slbe) - 16);
A
Alexander Graf 已提交
402 403 404 405 406 407
	slbe->valid = (rb & SLB_ESID_V) ? 1 : 0;
	slbe->Ks    = (rs & SLB_VSID_KS) ? 1 : 0;
	slbe->Kp    = (rs & SLB_VSID_KP) ? 1 : 0;
	slbe->nx    = (rs & SLB_VSID_N) ? 1 : 0;
	slbe->class = (rs & SLB_VSID_C) ? 1 : 0;

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	slbe->base_page_size = MMU_PAGE_4K;
	if (slbe->large) {
		if (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE) {
			switch (rs & SLB_VSID_LP) {
			case SLB_VSID_LP_00:
				slbe->base_page_size = MMU_PAGE_16M;
				break;
			case SLB_VSID_LP_01:
				slbe->base_page_size = MMU_PAGE_64K;
				break;
			}
		} else
			slbe->base_page_size = MMU_PAGE_16M;
	}

A
Alexander Graf 已提交
423 424 425 426 427 428 429 430 431 432 433
	slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
	slbe->origv = rs;

	/* Map the new segment */
	kvmppc_mmu_map_segment(vcpu, esid << SID_SHIFT);
}

static u64 kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu *vcpu, u64 slb_nr)
{
	struct kvmppc_slb *slbe;

434
	if (slb_nr > vcpu->arch.slb_nr)
A
Alexander Graf 已提交
435 436
		return 0;

437
	slbe = &vcpu->arch.slb[slb_nr];
A
Alexander Graf 已提交
438 439 440 441 442 443 444 445

	return slbe->orige;
}

static u64 kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu *vcpu, u64 slb_nr)
{
	struct kvmppc_slb *slbe;

446
	if (slb_nr > vcpu->arch.slb_nr)
A
Alexander Graf 已提交
447 448
		return 0;

449
	slbe = &vcpu->arch.slb[slb_nr];
A
Alexander Graf 已提交
450 451 452 453 454 455 456

	return slbe->origv;
}

static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu *vcpu, u64 ea)
{
	struct kvmppc_slb *slbe;
457
	u64 seg_size;
A
Alexander Graf 已提交
458 459 460

	dprintk("KVM MMU: slbie(0x%llx)\n", ea);

461
	slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
A
Alexander Graf 已提交
462 463 464 465 466 467 468

	if (!slbe)
		return;

	dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea, slbe->esid);

	slbe->valid = false;
469 470
	slbe->orige = 0;
	slbe->origv = 0;
A
Alexander Graf 已提交
471

472 473
	seg_size = 1ull << kvmppc_slb_sid_shift(slbe);
	kvmppc_mmu_flush_segment(vcpu, ea & ~(seg_size - 1), seg_size);
A
Alexander Graf 已提交
474 475 476 477 478 479 480 481
}

static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)
{
	int i;

	dprintk("KVM MMU: slbia()\n");

482
	for (i = 1; i < vcpu->arch.slb_nr; i++) {
483
		vcpu->arch.slb[i].valid = false;
484 485 486
		vcpu->arch.slb[i].orige = 0;
		vcpu->arch.slb[i].origv = 0;
	}
A
Alexander Graf 已提交
487

488
	if (kvmppc_get_msr(vcpu) & MSR_IR) {
A
Alexander Graf 已提交
489
		kvmppc_mmu_flush_segments(vcpu);
490
		kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
A
Alexander Graf 已提交
491 492 493 494 495 496 497 498
	}
}

static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
					ulong value)
{
	u64 rb = 0, rs = 0;

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
	/*
	 * According to Book3 2.01 mtsrin is implemented as:
	 *
	 * The SLB entry specified by (RB)32:35 is loaded from register
	 * RS, as follows.
	 *
	 * SLBE Bit	Source			SLB Field
	 *
	 * 0:31		0x0000_0000		ESID-0:31
	 * 32:35	(RB)32:35		ESID-32:35
	 * 36		0b1			V
	 * 37:61	0x00_0000|| 0b0		VSID-0:24
	 * 62:88	(RS)37:63		VSID-25:51
	 * 89:91	(RS)33:35		Ks Kp N
	 * 92		(RS)36			L ((RS)36 must be 0b0)
	 * 93		0b0			C
	 */

	dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum, value);

A
Alexander Graf 已提交
519 520 521 522 523 524 525 526 527 528
	/* ESID = srnum */
	rb |= (srnum & 0xf) << 28;
	/* Set the valid bit */
	rb |= 1 << 27;
	/* Index = ESID */
	rb |= srnum;

	/* VSID = VSID */
	rs |= (value & 0xfffffff) << 12;
	/* flags = flags */
529
	rs |= ((value >> 28) & 0x7) << 9;
A
Alexander Graf 已提交
530 531 532 533 534 535 536 537

	kvmppc_mmu_book3s_64_slbmte(vcpu, rs, rb);
}

static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va,
				       bool large)
{
	u64 mask = 0xFFFFFFFFFULL;
538 539
	long i;
	struct kvm_vcpu *v;
A
Alexander Graf 已提交
540 541 542

	dprintk("KVM MMU: tlbie(0x%lx)\n", va);

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	/*
	 * The tlbie instruction changed behaviour starting with
	 * POWER6.  POWER6 and later don't have the large page flag
	 * in the instruction but in the RB value, along with bits
	 * indicating page and segment sizes.
	 */
	if (vcpu->arch.hflags & BOOK3S_HFLAG_NEW_TLBIE) {
		/* POWER6 or later */
		if (va & 1) {		/* L bit */
			if ((va & 0xf000) == 0x1000)
				mask = 0xFFFFFFFF0ULL;	/* 64k page */
			else
				mask = 0xFFFFFF000ULL;	/* 16M page */
		}
	} else {
		/* older processors, e.g. PPC970 */
		if (large)
			mask = 0xFFFFFF000ULL;
	}
562 563 564
	/* flush this VA on all vcpus */
	kvm_for_each_vcpu(i, v, vcpu->kvm)
		kvmppc_mmu_pte_vflush(v, va >> 12, mask);
A
Alexander Graf 已提交
565 566
}

567 568 569 570 571
#ifdef CONFIG_PPC_64K_PAGES
static int segment_contains_magic_page(struct kvm_vcpu *vcpu, ulong esid)
{
	ulong mp_ea = vcpu->arch.magic_page_ea;

572
	return mp_ea && !(kvmppc_get_msr(vcpu) & MSR_PR) &&
573 574 575 576
		(mp_ea >> SID_SHIFT) == esid;
}
#endif

A
Alexander Graf 已提交
577
static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
A
Alexander Graf 已提交
578 579
					     u64 *vsid)
{
A
Alexander Graf 已提交
580 581 582
	ulong ea = esid << SID_SHIFT;
	struct kvmppc_slb *slb;
	u64 gvsid = esid;
583
	ulong mp_ea = vcpu->arch.magic_page_ea;
584
	int pagesize = MMU_PAGE_64K;
585
	u64 msr = kvmppc_get_msr(vcpu);
A
Alexander Graf 已提交
586

587
	if (msr & (MSR_DR|MSR_IR)) {
588
		slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
589
		if (slb) {
A
Alexander Graf 已提交
590
			gvsid = slb->vsid;
591
			pagesize = slb->base_page_size;
592 593 594 595 596 597
			if (slb->tb) {
				gvsid <<= SID_SHIFT_1T - SID_SHIFT;
				gvsid |= esid & ((1ul << (SID_SHIFT_1T - SID_SHIFT)) - 1);
				gvsid |= VSID_1T;
			}
		}
A
Alexander Graf 已提交
598 599
	}

600
	switch (msr & (MSR_DR|MSR_IR)) {
A
Alexander Graf 已提交
601
	case 0:
602
		gvsid = VSID_REAL | esid;
A
Alexander Graf 已提交
603 604
		break;
	case MSR_IR:
605
		gvsid |= VSID_REAL_IR;
A
Alexander Graf 已提交
606 607
		break;
	case MSR_DR:
608
		gvsid |= VSID_REAL_DR;
A
Alexander Graf 已提交
609 610
		break;
	case MSR_DR|MSR_IR:
A
Alexander Graf 已提交
611
		if (!slb)
612
			goto no_slb;
A
Alexander Graf 已提交
613 614 615 616 617 618 619

		break;
	default:
		BUG();
		break;
	}

620 621 622 623 624 625 626 627 628 629 630 631 632
#ifdef CONFIG_PPC_64K_PAGES
	/*
	 * Mark this as a 64k segment if the host is using
	 * 64k pages, the host MMU supports 64k pages and
	 * the guest segment page size is >= 64k,
	 * but not if this segment contains the magic page.
	 */
	if (pagesize >= MMU_PAGE_64K &&
	    mmu_psize_defs[MMU_PAGE_64K].shift &&
	    !segment_contains_magic_page(vcpu, esid))
		gvsid |= VSID_64K;
#endif

633
	if (kvmppc_get_msr(vcpu) & MSR_PR)
634
		gvsid |= VSID_PR;
635

636
	*vsid = gvsid;
A
Alexander Graf 已提交
637
	return 0;
638 639 640 641 642

no_slb:
	/* Catch magic page case */
	if (unlikely(mp_ea) &&
	    unlikely(esid == (mp_ea >> SID_SHIFT)) &&
643
	    !(kvmppc_get_msr(vcpu) & MSR_PR)) {
644 645 646 647 648
		*vsid = VSID_REAL | esid;
		return 0;
	}

	return -EINVAL;
A
Alexander Graf 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
}

static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu *vcpu)
{
	return (to_book3s(vcpu)->hid[5] & 0x80);
}

void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu)
{
	struct kvmppc_mmu *mmu = &vcpu->arch.mmu;

	mmu->mfsrin = NULL;
	mmu->mtsrin = kvmppc_mmu_book3s_64_mtsrin;
	mmu->slbmte = kvmppc_mmu_book3s_64_slbmte;
	mmu->slbmfee = kvmppc_mmu_book3s_64_slbmfee;
	mmu->slbmfev = kvmppc_mmu_book3s_64_slbmfev;
	mmu->slbie = kvmppc_mmu_book3s_64_slbie;
	mmu->slbia = kvmppc_mmu_book3s_64_slbia;
	mmu->xlate = kvmppc_mmu_book3s_64_xlate;
	mmu->reset_msr = kvmppc_mmu_book3s_64_reset_msr;
	mmu->tlbie = kvmppc_mmu_book3s_64_tlbie;
	mmu->esid_to_vsid = kvmppc_mmu_book3s_64_esid_to_vsid;
	mmu->ea_to_vp = kvmppc_mmu_book3s_64_ea_to_vp;
	mmu->is_dcbz32 = kvmppc_mmu_book3s_64_is_dcbz32;
673 674

	vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
A
Alexander Graf 已提交
675
}