xref: /openbmc/linux/arch/powerpc/kvm/booke.c (revision 6c645b01e536757a9e1a9f72c13767f9b3f8559f)
1d94d71cbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d9fbd03dSHollis Blanchard /*
3d9fbd03dSHollis Blanchard  *
4d9fbd03dSHollis Blanchard  * Copyright IBM Corp. 2007
54cd35f67SScott Wood  * Copyright 2010-2011 Freescale Semiconductor, Inc.
6d9fbd03dSHollis Blanchard  *
7d9fbd03dSHollis Blanchard  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
8d9fbd03dSHollis Blanchard  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
9d30f6e48SScott Wood  *          Scott Wood <scottwood@freescale.com>
10d30f6e48SScott Wood  *          Varun Sethi <varun.sethi@freescale.com>
11d9fbd03dSHollis Blanchard  */
12d9fbd03dSHollis Blanchard 
13d9fbd03dSHollis Blanchard #include <linux/errno.h>
14d9fbd03dSHollis Blanchard #include <linux/err.h>
15d9fbd03dSHollis Blanchard #include <linux/kvm_host.h>
165a0e3ad6STejun Heo #include <linux/gfp.h>
17d9fbd03dSHollis Blanchard #include <linux/module.h>
18d9fbd03dSHollis Blanchard #include <linux/vmalloc.h>
19d9fbd03dSHollis Blanchard #include <linux/fs.h>
207924bd41SHollis Blanchard 
21d9fbd03dSHollis Blanchard #include <asm/cputable.h>
227c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
233a96570fSNicholas Piggin #include <asm/interrupt.h>
24d9fbd03dSHollis Blanchard #include <asm/kvm_ppc.h>
25d9fbd03dSHollis Blanchard #include <asm/cacheflush.h>
26d30f6e48SScott Wood #include <asm/dbell.h>
27d30f6e48SScott Wood #include <asm/hw_irq.h>
28d30f6e48SScott Wood #include <asm/irq.h>
29b50df19cSMihai Caraman #include <asm/time.h>
30d9fbd03dSHollis Blanchard 
31d30f6e48SScott Wood #include "timing.h"
3275f74f0dSHollis Blanchard #include "booke.h"
33dba291f2SAneesh Kumar K.V 
34dba291f2SAneesh Kumar K.V #define CREATE_TRACE_POINTS
35dba291f2SAneesh Kumar K.V #include "trace_booke.h"
36d9fbd03dSHollis Blanchard 
37d9fbd03dSHollis Blanchard unsigned long kvmppc_booke_handlers;
38d9fbd03dSHollis Blanchard 
39fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
40fcfe1baeSJing Zhang 	KVM_GENERIC_VM_STATS(),
41fcfe1baeSJing Zhang 	STATS_DESC_ICOUNTER(VM, num_2M_pages),
42fcfe1baeSJing Zhang 	STATS_DESC_ICOUNTER(VM, num_1G_pages)
43fcfe1baeSJing Zhang };
44fcfe1baeSJing Zhang 
45fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = {
46fcfe1baeSJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
47fcfe1baeSJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
48fcfe1baeSJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
49fcfe1baeSJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
50fcfe1baeSJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
51fcfe1baeSJing Zhang 		       sizeof(kvm_vm_stats_desc),
52fcfe1baeSJing Zhang };
53fcfe1baeSJing Zhang 
54ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
55ce55c049SJing Zhang 	KVM_GENERIC_VCPU_STATS(),
56ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, sum_exits),
57ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, mmio_exits),
58ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, signal_exits),
59ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, light_exits),
60ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, itlb_real_miss_exits),
61ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, itlb_virt_miss_exits),
62ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dtlb_real_miss_exits),
63ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dtlb_virt_miss_exits),
64ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, syscall_exits),
65ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, isi_exits),
66ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dsi_exits),
67ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, emulated_inst_exits),
68ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dec_exits),
69ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, ext_intr_exits),
70ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, halt_successful_wait),
71ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dbell_exits),
72ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, gdbell_exits),
73ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, ld),
74ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, st),
75ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pthru_all),
76ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pthru_host),
77ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pthru_bad_aff)
78ce55c049SJing Zhang };
79ce55c049SJing Zhang 
80ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = {
81ce55c049SJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
82ce55c049SJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
83ce55c049SJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
84ce55c049SJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
85ce55c049SJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
86ce55c049SJing Zhang 		       sizeof(kvm_vcpu_stats_desc),
87ce55c049SJing Zhang };
88ce55c049SJing Zhang 
89d9fbd03dSHollis Blanchard /* TODO: use vcpu_printf() */
90d9fbd03dSHollis Blanchard void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
91d9fbd03dSHollis Blanchard {
92d9fbd03dSHollis Blanchard 	int i;
93d9fbd03dSHollis Blanchard 
94173c520aSSimon Guo 	printk("pc:   %08lx msr:  %08llx\n", vcpu->arch.regs.nip,
95173c520aSSimon Guo 			vcpu->arch.shared->msr);
96173c520aSSimon Guo 	printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.regs.link,
97173c520aSSimon Guo 			vcpu->arch.regs.ctr);
98de7906c3SAlexander Graf 	printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
99de7906c3SAlexander Graf 					    vcpu->arch.shared->srr1);
100d9fbd03dSHollis Blanchard 
101d9fbd03dSHollis Blanchard 	printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
102d9fbd03dSHollis Blanchard 
103d9fbd03dSHollis Blanchard 	for (i = 0; i < 32; i += 4) {
1045cf8ca22SHollis Blanchard 		printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
1058e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i),
1068e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+1),
1078e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+2),
1088e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+3));
109d9fbd03dSHollis Blanchard 	}
110d9fbd03dSHollis Blanchard }
111d9fbd03dSHollis Blanchard 
1124cd35f67SScott Wood #ifdef CONFIG_SPE
1134cd35f67SScott Wood void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
1144cd35f67SScott Wood {
1154cd35f67SScott Wood 	preempt_disable();
1164cd35f67SScott Wood 	enable_kernel_spe();
1174cd35f67SScott Wood 	kvmppc_save_guest_spe(vcpu);
118dc4fbba1SAnton Blanchard 	disable_kernel_spe();
1194cd35f67SScott Wood 	vcpu->arch.shadow_msr &= ~MSR_SPE;
1204cd35f67SScott Wood 	preempt_enable();
1214cd35f67SScott Wood }
1224cd35f67SScott Wood 
1234cd35f67SScott Wood static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
1244cd35f67SScott Wood {
1254cd35f67SScott Wood 	preempt_disable();
1264cd35f67SScott Wood 	enable_kernel_spe();
1274cd35f67SScott Wood 	kvmppc_load_guest_spe(vcpu);
128dc4fbba1SAnton Blanchard 	disable_kernel_spe();
1294cd35f67SScott Wood 	vcpu->arch.shadow_msr |= MSR_SPE;
1304cd35f67SScott Wood 	preempt_enable();
1314cd35f67SScott Wood }
1324cd35f67SScott Wood 
1334cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1344cd35f67SScott Wood {
1354cd35f67SScott Wood 	if (vcpu->arch.shared->msr & MSR_SPE) {
1364cd35f67SScott Wood 		if (!(vcpu->arch.shadow_msr & MSR_SPE))
1374cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
1384cd35f67SScott Wood 	} else if (vcpu->arch.shadow_msr & MSR_SPE) {
1394cd35f67SScott Wood 		kvmppc_vcpu_disable_spe(vcpu);
1404cd35f67SScott Wood 	}
1414cd35f67SScott Wood }
1424cd35f67SScott Wood #else
1434cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1444cd35f67SScott Wood {
1454cd35f67SScott Wood }
1464cd35f67SScott Wood #endif
1474cd35f67SScott Wood 
1483efc7da6SMihai Caraman /*
1493efc7da6SMihai Caraman  * Load up guest vcpu FP state if it's needed.
1503efc7da6SMihai Caraman  * It also set the MSR_FP in thread so that host know
1513efc7da6SMihai Caraman  * we're holding FPU, and then host can help to save
1523efc7da6SMihai Caraman  * guest vcpu FP state if other threads require to use FPU.
1533efc7da6SMihai Caraman  * This simulates an FP unavailable fault.
1543efc7da6SMihai Caraman  *
1553efc7da6SMihai Caraman  * It requires to be called with preemption disabled.
1563efc7da6SMihai Caraman  */
1573efc7da6SMihai Caraman static inline void kvmppc_load_guest_fp(struct kvm_vcpu *vcpu)
1583efc7da6SMihai Caraman {
1593efc7da6SMihai Caraman #ifdef CONFIG_PPC_FPU
1603efc7da6SMihai Caraman 	if (!(current->thread.regs->msr & MSR_FP)) {
1613efc7da6SMihai Caraman 		enable_kernel_fp();
1623efc7da6SMihai Caraman 		load_fp_state(&vcpu->arch.fp);
163dc4fbba1SAnton Blanchard 		disable_kernel_fp();
1643efc7da6SMihai Caraman 		current->thread.fp_save_area = &vcpu->arch.fp;
1653efc7da6SMihai Caraman 		current->thread.regs->msr |= MSR_FP;
1663efc7da6SMihai Caraman 	}
1673efc7da6SMihai Caraman #endif
1683efc7da6SMihai Caraman }
1693efc7da6SMihai Caraman 
1703efc7da6SMihai Caraman /*
1713efc7da6SMihai Caraman  * Save guest vcpu FP state into thread.
1723efc7da6SMihai Caraman  * It requires to be called with preemption disabled.
1733efc7da6SMihai Caraman  */
1743efc7da6SMihai Caraman static inline void kvmppc_save_guest_fp(struct kvm_vcpu *vcpu)
1753efc7da6SMihai Caraman {
1763efc7da6SMihai Caraman #ifdef CONFIG_PPC_FPU
1773efc7da6SMihai Caraman 	if (current->thread.regs->msr & MSR_FP)
1783efc7da6SMihai Caraman 		giveup_fpu(current);
1793efc7da6SMihai Caraman 	current->thread.fp_save_area = NULL;
1803efc7da6SMihai Caraman #endif
1813efc7da6SMihai Caraman }
1823efc7da6SMihai Caraman 
1837a08c274SAlexander Graf static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
1847a08c274SAlexander Graf {
1857a08c274SAlexander Graf #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
1867a08c274SAlexander Graf 	/* We always treat the FP bit as enabled from the host
1877a08c274SAlexander Graf 	   perspective, so only need to adjust the shadow MSR */
1887a08c274SAlexander Graf 	vcpu->arch.shadow_msr &= ~MSR_FP;
1897a08c274SAlexander Graf 	vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
1907a08c274SAlexander Graf #endif
1917a08c274SAlexander Graf }
1927a08c274SAlexander Graf 
19395d80a29SMihai Caraman /*
19495d80a29SMihai Caraman  * Simulate AltiVec unavailable fault to load guest state
19595d80a29SMihai Caraman  * from thread to AltiVec unit.
19695d80a29SMihai Caraman  * It requires to be called with preemption disabled.
19795d80a29SMihai Caraman  */
19895d80a29SMihai Caraman static inline void kvmppc_load_guest_altivec(struct kvm_vcpu *vcpu)
19995d80a29SMihai Caraman {
20095d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
20195d80a29SMihai Caraman 	if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
20295d80a29SMihai Caraman 		if (!(current->thread.regs->msr & MSR_VEC)) {
20395d80a29SMihai Caraman 			enable_kernel_altivec();
20495d80a29SMihai Caraman 			load_vr_state(&vcpu->arch.vr);
205dc4fbba1SAnton Blanchard 			disable_kernel_altivec();
20695d80a29SMihai Caraman 			current->thread.vr_save_area = &vcpu->arch.vr;
20795d80a29SMihai Caraman 			current->thread.regs->msr |= MSR_VEC;
20895d80a29SMihai Caraman 		}
20995d80a29SMihai Caraman 	}
21095d80a29SMihai Caraman #endif
21195d80a29SMihai Caraman }
21295d80a29SMihai Caraman 
21395d80a29SMihai Caraman /*
21495d80a29SMihai Caraman  * Save guest vcpu AltiVec state into thread.
21595d80a29SMihai Caraman  * It requires to be called with preemption disabled.
21695d80a29SMihai Caraman  */
21795d80a29SMihai Caraman static inline void kvmppc_save_guest_altivec(struct kvm_vcpu *vcpu)
21895d80a29SMihai Caraman {
21995d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
22095d80a29SMihai Caraman 	if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
22195d80a29SMihai Caraman 		if (current->thread.regs->msr & MSR_VEC)
22295d80a29SMihai Caraman 			giveup_altivec(current);
22395d80a29SMihai Caraman 		current->thread.vr_save_area = NULL;
22495d80a29SMihai Caraman 	}
22595d80a29SMihai Caraman #endif
22695d80a29SMihai Caraman }
22795d80a29SMihai Caraman 
228ce11e48bSBharat Bhushan static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
229ce11e48bSBharat Bhushan {
230ce11e48bSBharat Bhushan 	/* Synchronize guest's desire to get debug interrupts into shadow MSR */
231ce11e48bSBharat Bhushan #ifndef CONFIG_KVM_BOOKE_HV
232ce11e48bSBharat Bhushan 	vcpu->arch.shadow_msr &= ~MSR_DE;
233ce11e48bSBharat Bhushan 	vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
234ce11e48bSBharat Bhushan #endif
235ce11e48bSBharat Bhushan 
236ce11e48bSBharat Bhushan 	/* Force enable debug interrupts when user space wants to debug */
237ce11e48bSBharat Bhushan 	if (vcpu->guest_debug) {
238ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
239ce11e48bSBharat Bhushan 		/*
240ce11e48bSBharat Bhushan 		 * Since there is no shadow MSR, sync MSR_DE into the guest
241ce11e48bSBharat Bhushan 		 * visible MSR.
242ce11e48bSBharat Bhushan 		 */
243ce11e48bSBharat Bhushan 		vcpu->arch.shared->msr |= MSR_DE;
244ce11e48bSBharat Bhushan #else
245ce11e48bSBharat Bhushan 		vcpu->arch.shadow_msr |= MSR_DE;
246ce11e48bSBharat Bhushan 		vcpu->arch.shared->msr &= ~MSR_DE;
247ce11e48bSBharat Bhushan #endif
248ce11e48bSBharat Bhushan 	}
249ce11e48bSBharat Bhushan }
250ce11e48bSBharat Bhushan 
251dd9ebf1fSLiu Yu /*
252dd9ebf1fSLiu Yu  * Helper function for "full" MSR writes.  No need to call this if only
253dd9ebf1fSLiu Yu  * EE/CE/ME/DE/RI are changing.
254dd9ebf1fSLiu Yu  */
2554cd35f67SScott Wood void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
2564cd35f67SScott Wood {
257dd9ebf1fSLiu Yu 	u32 old_msr = vcpu->arch.shared->msr;
2584cd35f67SScott Wood 
259d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
260d30f6e48SScott Wood 	new_msr |= MSR_GS;
261d30f6e48SScott Wood #endif
262d30f6e48SScott Wood 
2634cd35f67SScott Wood 	vcpu->arch.shared->msr = new_msr;
2644cd35f67SScott Wood 
265dd9ebf1fSLiu Yu 	kvmppc_mmu_msr_notify(vcpu, old_msr);
2664cd35f67SScott Wood 	kvmppc_vcpu_sync_spe(vcpu);
2677a08c274SAlexander Graf 	kvmppc_vcpu_sync_fpu(vcpu);
268ce11e48bSBharat Bhushan 	kvmppc_vcpu_sync_debug(vcpu);
2694cd35f67SScott Wood }
2704cd35f67SScott Wood 
271d4cf3892SHollis Blanchard static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
272d4cf3892SHollis Blanchard                                        unsigned int priority)
2739dd921cfSHollis Blanchard {
2746346046cSAlexander Graf 	trace_kvm_booke_queue_irqprio(vcpu, priority);
2759dd921cfSHollis Blanchard 	set_bit(priority, &vcpu->arch.pending_exceptions);
2769dd921cfSHollis Blanchard }
2779dd921cfSHollis Blanchard 
2788de12015SAlexander Graf void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
279daf5e271SLiu Yu 				 ulong dear_flags, ulong esr_flags)
2809dd921cfSHollis Blanchard {
281daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
282daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
283daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
284daf5e271SLiu Yu }
285daf5e271SLiu Yu 
2868de12015SAlexander Graf void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
287daf5e271SLiu Yu 				    ulong dear_flags, ulong esr_flags)
288daf5e271SLiu Yu {
289daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
290daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
291daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
292daf5e271SLiu Yu }
293daf5e271SLiu Yu 
2948de12015SAlexander Graf void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu)
2958de12015SAlexander Graf {
2968de12015SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
2978de12015SAlexander Graf }
2988de12015SAlexander Graf 
2998de12015SAlexander Graf void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong esr_flags)
300daf5e271SLiu Yu {
301daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
302daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
303daf5e271SLiu Yu }
304daf5e271SLiu Yu 
305011da899SAlexander Graf static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
306011da899SAlexander Graf 					ulong esr_flags)
307011da899SAlexander Graf {
308011da899SAlexander Graf 	vcpu->arch.queued_dear = dear_flags;
309011da899SAlexander Graf 	vcpu->arch.queued_esr = esr_flags;
310011da899SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
311011da899SAlexander Graf }
312011da899SAlexander Graf 
313daf5e271SLiu Yu void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
314daf5e271SLiu Yu {
315daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
316d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
3179dd921cfSHollis Blanchard }
3189dd921cfSHollis Blanchard 
319307d9279SPaul Mackerras void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu)
320307d9279SPaul Mackerras {
321307d9279SPaul Mackerras 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
322307d9279SPaul Mackerras }
323307d9279SPaul Mackerras 
324b2d7ecbeSLaurentiu Tudor #ifdef CONFIG_ALTIVEC
325b2d7ecbeSLaurentiu Tudor void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu)
326b2d7ecbeSLaurentiu Tudor {
327b2d7ecbeSLaurentiu Tudor 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
328b2d7ecbeSLaurentiu Tudor }
329b2d7ecbeSLaurentiu Tudor #endif
330b2d7ecbeSLaurentiu Tudor 
3319dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
3329dd921cfSHollis Blanchard {
333d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
3349dd921cfSHollis Blanchard }
3359dd921cfSHollis Blanchard 
3369dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
3379dd921cfSHollis Blanchard {
338d4cf3892SHollis Blanchard 	return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
3399dd921cfSHollis Blanchard }
3409dd921cfSHollis Blanchard 
3417706664dSAlexander Graf void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
3427706664dSAlexander Graf {
3437706664dSAlexander Graf 	clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
3447706664dSAlexander Graf }
3457706664dSAlexander Graf 
3469dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
3479dd921cfSHollis Blanchard                                 struct kvm_interrupt *irq)
3489dd921cfSHollis Blanchard {
349c5335f17SAlexander Graf 	unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
350c5335f17SAlexander Graf 
351c5335f17SAlexander Graf 	if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
352c5335f17SAlexander Graf 		prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
353c5335f17SAlexander Graf 
354c5335f17SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, prio);
3559dd921cfSHollis Blanchard }
3569dd921cfSHollis Blanchard 
3574fe27d2aSPaul Mackerras void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
3584496f974SAlexander Graf {
3594496f974SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
360c5335f17SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
3614496f974SAlexander Graf }
3624496f974SAlexander Graf 
363f61c94bbSBharat Bhushan static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
364f61c94bbSBharat Bhushan {
365f61c94bbSBharat Bhushan 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
366f61c94bbSBharat Bhushan }
367f61c94bbSBharat Bhushan 
368f61c94bbSBharat Bhushan static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
369f61c94bbSBharat Bhushan {
370f61c94bbSBharat Bhushan 	clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
371f61c94bbSBharat Bhushan }
372f61c94bbSBharat Bhushan 
3732f699a59SBharat Bhushan void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu)
3742f699a59SBharat Bhushan {
3752f699a59SBharat Bhushan 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DEBUG);
3762f699a59SBharat Bhushan }
3772f699a59SBharat Bhushan 
3782f699a59SBharat Bhushan void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu)
3792f699a59SBharat Bhushan {
3802f699a59SBharat Bhushan 	clear_bit(BOOKE_IRQPRIO_DEBUG, &vcpu->arch.pending_exceptions);
3812f699a59SBharat Bhushan }
3822f699a59SBharat Bhushan 
383d30f6e48SScott Wood static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
384d30f6e48SScott Wood {
38531579eeaSBharat Bhushan 	kvmppc_set_srr0(vcpu, srr0);
38631579eeaSBharat Bhushan 	kvmppc_set_srr1(vcpu, srr1);
387d30f6e48SScott Wood }
388d30f6e48SScott Wood 
389d30f6e48SScott Wood static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
390d30f6e48SScott Wood {
391d30f6e48SScott Wood 	vcpu->arch.csrr0 = srr0;
392d30f6e48SScott Wood 	vcpu->arch.csrr1 = srr1;
393d30f6e48SScott Wood }
394d30f6e48SScott Wood 
395d30f6e48SScott Wood static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
396d30f6e48SScott Wood {
397d30f6e48SScott Wood 	if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
398d30f6e48SScott Wood 		vcpu->arch.dsrr0 = srr0;
399d30f6e48SScott Wood 		vcpu->arch.dsrr1 = srr1;
400d30f6e48SScott Wood 	} else {
401d30f6e48SScott Wood 		set_guest_csrr(vcpu, srr0, srr1);
402d30f6e48SScott Wood 	}
403d30f6e48SScott Wood }
404d30f6e48SScott Wood 
405d30f6e48SScott Wood static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
406d30f6e48SScott Wood {
407d30f6e48SScott Wood 	vcpu->arch.mcsrr0 = srr0;
408d30f6e48SScott Wood 	vcpu->arch.mcsrr1 = srr1;
409d30f6e48SScott Wood }
410d30f6e48SScott Wood 
411d4cf3892SHollis Blanchard /* Deliver the interrupt of the corresponding priority, if possible. */
412d4cf3892SHollis Blanchard static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
413d4cf3892SHollis Blanchard                                         unsigned int priority)
414d9fbd03dSHollis Blanchard {
415d4cf3892SHollis Blanchard 	int allowed = 0;
41679300f8cSAlexander Graf 	ulong msr_mask = 0;
4171c810636SAlexander Graf 	bool update_esr = false, update_dear = false, update_epr = false;
4185c6cedf4SAlexander Graf 	ulong crit_raw = vcpu->arch.shared->critical;
4195c6cedf4SAlexander Graf 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
4205c6cedf4SAlexander Graf 	bool crit;
421c5335f17SAlexander Graf 	bool keep_irq = false;
422d30f6e48SScott Wood 	enum int_class int_class;
42395e90b43SMihai Caraman 	ulong new_msr = vcpu->arch.shared->msr;
4245c6cedf4SAlexander Graf 
4255c6cedf4SAlexander Graf 	/* Truncate crit indicators in 32 bit mode */
4265c6cedf4SAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
4275c6cedf4SAlexander Graf 		crit_raw &= 0xffffffff;
4285c6cedf4SAlexander Graf 		crit_r1 &= 0xffffffff;
4295c6cedf4SAlexander Graf 	}
4305c6cedf4SAlexander Graf 
4315c6cedf4SAlexander Graf 	/* Critical section when crit == r1 */
4325c6cedf4SAlexander Graf 	crit = (crit_raw == crit_r1);
4335c6cedf4SAlexander Graf 	/* ... and we're in supervisor mode */
4345c6cedf4SAlexander Graf 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
435d9fbd03dSHollis Blanchard 
436c5335f17SAlexander Graf 	if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
437c5335f17SAlexander Graf 		priority = BOOKE_IRQPRIO_EXTERNAL;
438c5335f17SAlexander Graf 		keep_irq = true;
439c5335f17SAlexander Graf 	}
440c5335f17SAlexander Graf 
4415df554adSScott Wood 	if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
4421c810636SAlexander Graf 		update_epr = true;
4431c810636SAlexander Graf 
444d4cf3892SHollis Blanchard 	switch (priority) {
445d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DTLB_MISS:
446daf5e271SLiu Yu 	case BOOKE_IRQPRIO_DATA_STORAGE:
447011da899SAlexander Graf 	case BOOKE_IRQPRIO_ALIGNMENT:
448daf5e271SLiu Yu 		update_dear = true;
4498fc6ba0aSJoe Perches 		fallthrough;
450daf5e271SLiu Yu 	case BOOKE_IRQPRIO_INST_STORAGE:
451daf5e271SLiu Yu 	case BOOKE_IRQPRIO_PROGRAM:
452daf5e271SLiu Yu 		update_esr = true;
4538fc6ba0aSJoe Perches 		fallthrough;
454d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ITLB_MISS:
455d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_SYSCALL:
456d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FP_UNAVAIL:
45795d80a29SMihai Caraman #ifdef CONFIG_SPE_POSSIBLE
458bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_UNAVAIL:
459bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_DATA:
460bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_ROUND:
46195d80a29SMihai Caraman #endif
46295d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
46395d80a29SMihai Caraman 	case BOOKE_IRQPRIO_ALTIVEC_UNAVAIL:
46495d80a29SMihai Caraman 	case BOOKE_IRQPRIO_ALTIVEC_ASSIST:
46595d80a29SMihai Caraman #endif
466d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_AP_UNAVAIL:
467d4cf3892SHollis Blanchard 		allowed = 1;
46879300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
469d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
470d9fbd03dSHollis Blanchard 		break;
471f61c94bbSBharat Bhushan 	case BOOKE_IRQPRIO_WATCHDOG:
472d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_CRITICAL:
4734ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL_CRIT:
474666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_CE;
475d30f6e48SScott Wood 		allowed = allowed && !crit;
47679300f8cSAlexander Graf 		msr_mask = MSR_ME;
477d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
478d9fbd03dSHollis Blanchard 		break;
479d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_MACHINE_CHECK:
480666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_ME;
481d30f6e48SScott Wood 		allowed = allowed && !crit;
482d30f6e48SScott Wood 		int_class = INT_CLASS_MC;
483d9fbd03dSHollis Blanchard 		break;
484d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DECREMENTER:
485d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FIT:
486dfd4d47eSScott Wood 		keep_irq = true;
4878fc6ba0aSJoe Perches 		fallthrough;
488dfd4d47eSScott Wood 	case BOOKE_IRQPRIO_EXTERNAL:
4894ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL:
490666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_EE;
4915c6cedf4SAlexander Graf 		allowed = allowed && !crit;
49279300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
493d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
494d9fbd03dSHollis Blanchard 		break;
495d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DEBUG:
496666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_DE;
497d30f6e48SScott Wood 		allowed = allowed && !crit;
49879300f8cSAlexander Graf 		msr_mask = MSR_ME;
4999fee7563SBharat Bhushan 		if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
5009fee7563SBharat Bhushan 			int_class = INT_CLASS_DBG;
5019fee7563SBharat Bhushan 		else
502d30f6e48SScott Wood 			int_class = INT_CLASS_CRIT;
5039fee7563SBharat Bhushan 
504d9fbd03dSHollis Blanchard 		break;
505d9fbd03dSHollis Blanchard 	}
506d9fbd03dSHollis Blanchard 
507d4cf3892SHollis Blanchard 	if (allowed) {
508d30f6e48SScott Wood 		switch (int_class) {
509d30f6e48SScott Wood 		case INT_CLASS_NONCRIT:
510173c520aSSimon Guo 			set_guest_srr(vcpu, vcpu->arch.regs.nip,
511d30f6e48SScott Wood 				      vcpu->arch.shared->msr);
512d30f6e48SScott Wood 			break;
513d30f6e48SScott Wood 		case INT_CLASS_CRIT:
514173c520aSSimon Guo 			set_guest_csrr(vcpu, vcpu->arch.regs.nip,
515d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
516d30f6e48SScott Wood 			break;
517d30f6e48SScott Wood 		case INT_CLASS_DBG:
518173c520aSSimon Guo 			set_guest_dsrr(vcpu, vcpu->arch.regs.nip,
519d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
520d30f6e48SScott Wood 			break;
521d30f6e48SScott Wood 		case INT_CLASS_MC:
522173c520aSSimon Guo 			set_guest_mcsrr(vcpu, vcpu->arch.regs.nip,
523d30f6e48SScott Wood 					vcpu->arch.shared->msr);
524d30f6e48SScott Wood 			break;
525d30f6e48SScott Wood 		}
526d30f6e48SScott Wood 
527173c520aSSimon Guo 		vcpu->arch.regs.nip = vcpu->arch.ivpr |
528173c520aSSimon Guo 					vcpu->arch.ivor[priority];
529a300bf8cSKaixu Xia 		if (update_esr)
530dc168549SBharat Bhushan 			kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
531a300bf8cSKaixu Xia 		if (update_dear)
532a5414d4bSBharat Bhushan 			kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
533a300bf8cSKaixu Xia 		if (update_epr) {
5345df554adSScott Wood 			if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
5351c810636SAlexander Graf 				kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
536eb1e4f43SScott Wood 			else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
537eb1e4f43SScott Wood 				BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
538eb1e4f43SScott Wood 				kvmppc_mpic_set_epr(vcpu);
539eb1e4f43SScott Wood 			}
5405df554adSScott Wood 		}
54195e90b43SMihai Caraman 
54295e90b43SMihai Caraman 		new_msr &= msr_mask;
54395e90b43SMihai Caraman #if defined(CONFIG_64BIT)
54495e90b43SMihai Caraman 		if (vcpu->arch.epcr & SPRN_EPCR_ICM)
54595e90b43SMihai Caraman 			new_msr |= MSR_CM;
54695e90b43SMihai Caraman #endif
54795e90b43SMihai Caraman 		kvmppc_set_msr(vcpu, new_msr);
548d4cf3892SHollis Blanchard 
549c5335f17SAlexander Graf 		if (!keep_irq)
550d4cf3892SHollis Blanchard 			clear_bit(priority, &vcpu->arch.pending_exceptions);
551d4cf3892SHollis Blanchard 	}
552d4cf3892SHollis Blanchard 
553d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
554d30f6e48SScott Wood 	/*
555d30f6e48SScott Wood 	 * If an interrupt is pending but masked, raise a guest doorbell
556d30f6e48SScott Wood 	 * so that we are notified when the guest enables the relevant
557d30f6e48SScott Wood 	 * MSR bit.
558d30f6e48SScott Wood 	 */
559d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
560d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
561d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
562d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
563d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
564d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
565d30f6e48SScott Wood #endif
566d30f6e48SScott Wood 
567d4cf3892SHollis Blanchard 	return allowed;
568d9fbd03dSHollis Blanchard }
569d9fbd03dSHollis Blanchard 
570f61c94bbSBharat Bhushan /*
571f61c94bbSBharat Bhushan  * Return the number of jiffies until the next timeout.  If the timeout is
572f61c94bbSBharat Bhushan  * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
573f61c94bbSBharat Bhushan  * because the larger value can break the timer APIs.
574f61c94bbSBharat Bhushan  */
575f61c94bbSBharat Bhushan static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
576f61c94bbSBharat Bhushan {
577f61c94bbSBharat Bhushan 	u64 tb, wdt_tb, wdt_ticks = 0;
578f61c94bbSBharat Bhushan 	u64 nr_jiffies = 0;
579f61c94bbSBharat Bhushan 	u32 period = TCR_GET_WP(vcpu->arch.tcr);
580f61c94bbSBharat Bhushan 
581f61c94bbSBharat Bhushan 	wdt_tb = 1ULL << (63 - period);
582f61c94bbSBharat Bhushan 	tb = get_tb();
583f61c94bbSBharat Bhushan 	/*
584f61c94bbSBharat Bhushan 	 * The watchdog timeout will hapeen when TB bit corresponding
585f61c94bbSBharat Bhushan 	 * to watchdog will toggle from 0 to 1.
586f61c94bbSBharat Bhushan 	 */
587f61c94bbSBharat Bhushan 	if (tb & wdt_tb)
588f61c94bbSBharat Bhushan 		wdt_ticks = wdt_tb;
589f61c94bbSBharat Bhushan 
590f61c94bbSBharat Bhushan 	wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
591f61c94bbSBharat Bhushan 
592f61c94bbSBharat Bhushan 	/* Convert timebase ticks to jiffies */
593f61c94bbSBharat Bhushan 	nr_jiffies = wdt_ticks;
594f61c94bbSBharat Bhushan 
595f61c94bbSBharat Bhushan 	if (do_div(nr_jiffies, tb_ticks_per_jiffy))
596f61c94bbSBharat Bhushan 		nr_jiffies++;
597f61c94bbSBharat Bhushan 
598f61c94bbSBharat Bhushan 	return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
599f61c94bbSBharat Bhushan }
600f61c94bbSBharat Bhushan 
601f61c94bbSBharat Bhushan static void arm_next_watchdog(struct kvm_vcpu *vcpu)
602f61c94bbSBharat Bhushan {
603f61c94bbSBharat Bhushan 	unsigned long nr_jiffies;
604f61c94bbSBharat Bhushan 	unsigned long flags;
605f61c94bbSBharat Bhushan 
606f61c94bbSBharat Bhushan 	/*
607f61c94bbSBharat Bhushan 	 * If TSR_ENW and TSR_WIS are not set then no need to exit to
608f61c94bbSBharat Bhushan 	 * userspace, so clear the KVM_REQ_WATCHDOG request.
609f61c94bbSBharat Bhushan 	 */
610f61c94bbSBharat Bhushan 	if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
61172875d8aSRadim Krčmář 		kvm_clear_request(KVM_REQ_WATCHDOG, vcpu);
612f61c94bbSBharat Bhushan 
613f61c94bbSBharat Bhushan 	spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
614f61c94bbSBharat Bhushan 	nr_jiffies = watchdog_next_timeout(vcpu);
615f61c94bbSBharat Bhushan 	/*
616f61c94bbSBharat Bhushan 	 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
617f61c94bbSBharat Bhushan 	 * then do not run the watchdog timer as this can break timer APIs.
618f61c94bbSBharat Bhushan 	 */
619f61c94bbSBharat Bhushan 	if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
620f61c94bbSBharat Bhushan 		mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
621f61c94bbSBharat Bhushan 	else
622f61c94bbSBharat Bhushan 		del_timer(&vcpu->arch.wdt_timer);
623f61c94bbSBharat Bhushan 	spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
624f61c94bbSBharat Bhushan }
625f61c94bbSBharat Bhushan 
62686cb30ecSKees Cook void kvmppc_watchdog_func(struct timer_list *t)
627f61c94bbSBharat Bhushan {
62886cb30ecSKees Cook 	struct kvm_vcpu *vcpu = from_timer(vcpu, t, arch.wdt_timer);
629f61c94bbSBharat Bhushan 	u32 tsr, new_tsr;
630f61c94bbSBharat Bhushan 	int final;
631f61c94bbSBharat Bhushan 
632f61c94bbSBharat Bhushan 	do {
633f61c94bbSBharat Bhushan 		new_tsr = tsr = vcpu->arch.tsr;
634f61c94bbSBharat Bhushan 		final = 0;
635f61c94bbSBharat Bhushan 
636f61c94bbSBharat Bhushan 		/* Time out event */
637f61c94bbSBharat Bhushan 		if (tsr & TSR_ENW) {
638f61c94bbSBharat Bhushan 			if (tsr & TSR_WIS)
639f61c94bbSBharat Bhushan 				final = 1;
640f61c94bbSBharat Bhushan 			else
641f61c94bbSBharat Bhushan 				new_tsr = tsr | TSR_WIS;
642f61c94bbSBharat Bhushan 		} else {
643f61c94bbSBharat Bhushan 			new_tsr = tsr | TSR_ENW;
644f61c94bbSBharat Bhushan 		}
645f61c94bbSBharat Bhushan 	} while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
646f61c94bbSBharat Bhushan 
647f61c94bbSBharat Bhushan 	if (new_tsr & TSR_WIS) {
648f61c94bbSBharat Bhushan 		smp_wmb();
649f61c94bbSBharat Bhushan 		kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
650f61c94bbSBharat Bhushan 		kvm_vcpu_kick(vcpu);
651f61c94bbSBharat Bhushan 	}
652f61c94bbSBharat Bhushan 
653f61c94bbSBharat Bhushan 	/*
654f61c94bbSBharat Bhushan 	 * If this is final watchdog expiry and some action is required
655f61c94bbSBharat Bhushan 	 * then exit to userspace.
656f61c94bbSBharat Bhushan 	 */
657f61c94bbSBharat Bhushan 	if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
658f61c94bbSBharat Bhushan 	    vcpu->arch.watchdog_enabled) {
659f61c94bbSBharat Bhushan 		smp_wmb();
660f61c94bbSBharat Bhushan 		kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
661f61c94bbSBharat Bhushan 		kvm_vcpu_kick(vcpu);
662f61c94bbSBharat Bhushan 	}
663f61c94bbSBharat Bhushan 
664f61c94bbSBharat Bhushan 	/*
665f61c94bbSBharat Bhushan 	 * Stop running the watchdog timer after final expiration to
666f61c94bbSBharat Bhushan 	 * prevent the host from being flooded with timers if the
667f61c94bbSBharat Bhushan 	 * guest sets a short period.
668f61c94bbSBharat Bhushan 	 * Timers will resume when TSR/TCR is updated next time.
669f61c94bbSBharat Bhushan 	 */
670f61c94bbSBharat Bhushan 	if (!final)
671f61c94bbSBharat Bhushan 		arm_next_watchdog(vcpu);
672f61c94bbSBharat Bhushan }
673f61c94bbSBharat Bhushan 
674dfd4d47eSScott Wood static void update_timer_ints(struct kvm_vcpu *vcpu)
675dfd4d47eSScott Wood {
676dfd4d47eSScott Wood 	if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
677dfd4d47eSScott Wood 		kvmppc_core_queue_dec(vcpu);
678dfd4d47eSScott Wood 	else
679dfd4d47eSScott Wood 		kvmppc_core_dequeue_dec(vcpu);
680f61c94bbSBharat Bhushan 
681f61c94bbSBharat Bhushan 	if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
682f61c94bbSBharat Bhushan 		kvmppc_core_queue_watchdog(vcpu);
683f61c94bbSBharat Bhushan 	else
684f61c94bbSBharat Bhushan 		kvmppc_core_dequeue_watchdog(vcpu);
685dfd4d47eSScott Wood }
686dfd4d47eSScott Wood 
687c59a6a3eSScott Wood static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
688d9fbd03dSHollis Blanchard {
689d9fbd03dSHollis Blanchard 	unsigned long *pending = &vcpu->arch.pending_exceptions;
690d9fbd03dSHollis Blanchard 	unsigned int priority;
691d9fbd03dSHollis Blanchard 
6929ab80843SHollis Blanchard 	priority = __ffs(*pending);
6938b3a00fcSAlexander Graf 	while (priority < BOOKE_IRQPRIO_MAX) {
694d4cf3892SHollis Blanchard 		if (kvmppc_booke_irqprio_deliver(vcpu, priority))
695d9fbd03dSHollis Blanchard 			break;
696d9fbd03dSHollis Blanchard 
697d9fbd03dSHollis Blanchard 		priority = find_next_bit(pending,
698d9fbd03dSHollis Blanchard 		                         BITS_PER_BYTE * sizeof(*pending),
699d9fbd03dSHollis Blanchard 		                         priority + 1);
700d9fbd03dSHollis Blanchard 	}
70190bba358SAlexander Graf 
70290bba358SAlexander Graf 	/* Tell the guest about our interrupt status */
70329ac26efSScott Wood 	vcpu->arch.shared->int_pending = !!*pending;
704d9fbd03dSHollis Blanchard }
705d9fbd03dSHollis Blanchard 
706c59a6a3eSScott Wood /* Check pending exceptions and deliver one, if possible. */
707a8e4ef84SAlexander Graf int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
708c59a6a3eSScott Wood {
709a8e4ef84SAlexander Graf 	int r = 0;
710c59a6a3eSScott Wood 	WARN_ON_ONCE(!irqs_disabled());
711c59a6a3eSScott Wood 
712c59a6a3eSScott Wood 	kvmppc_core_check_exceptions(vcpu);
713c59a6a3eSScott Wood 
7142fa6e1e1SRadim Krčmář 	if (kvm_request_pending(vcpu)) {
715b8c649a9SAlexander Graf 		/* Exception delivery raised request; start over */
716b8c649a9SAlexander Graf 		return 1;
717b8c649a9SAlexander Graf 	}
718b8c649a9SAlexander Graf 
719c59a6a3eSScott Wood 	if (vcpu->arch.shared->msr & MSR_WE) {
720c59a6a3eSScott Wood 		local_irq_enable();
72191b99ea7SSean Christopherson 		kvm_vcpu_halt(vcpu);
7226c85f52bSScott Wood 		hard_irq_disable();
723c59a6a3eSScott Wood 
724c59a6a3eSScott Wood 		kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
725a8e4ef84SAlexander Graf 		r = 1;
72663e9f235SYang Li 	}
727a8e4ef84SAlexander Graf 
728a8e4ef84SAlexander Graf 	return r;
729a8e4ef84SAlexander Graf }
730a8e4ef84SAlexander Graf 
7317c973a2eSAlexander Graf int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
7324ffc6356SAlexander Graf {
7337c973a2eSAlexander Graf 	int r = 1; /* Indicate we want to get back into the guest */
7347c973a2eSAlexander Graf 
7354ffc6356SAlexander Graf 	if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
7364ffc6356SAlexander Graf 		update_timer_ints(vcpu);
737862d31f7SAlexander Graf #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
738862d31f7SAlexander Graf 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
739862d31f7SAlexander Graf 		kvmppc_core_flush_tlb(vcpu);
740862d31f7SAlexander Graf #endif
7417c973a2eSAlexander Graf 
742f61c94bbSBharat Bhushan 	if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
743f61c94bbSBharat Bhushan 		vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
744f61c94bbSBharat Bhushan 		r = 0;
745f61c94bbSBharat Bhushan 	}
746f61c94bbSBharat Bhushan 
7471c810636SAlexander Graf 	if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
7481c810636SAlexander Graf 		vcpu->run->epr.epr = 0;
7491c810636SAlexander Graf 		vcpu->arch.epr_needed = true;
7501c810636SAlexander Graf 		vcpu->run->exit_reason = KVM_EXIT_EPR;
7511c810636SAlexander Graf 		r = 0;
7521c810636SAlexander Graf 	}
7531c810636SAlexander Graf 
7547c973a2eSAlexander Graf 	return r;
7554ffc6356SAlexander Graf }
7564ffc6356SAlexander Graf 
7578c99d345STianjia Zhang int kvmppc_vcpu_run(struct kvm_vcpu *vcpu)
758df6909e5SPaul Mackerras {
7597ee78855SAlexander Graf 	int ret, s;
760f5f97210SScott Wood 	struct debug_reg debug;
761df6909e5SPaul Mackerras 
762af8f38b3SAlexander Graf 	if (!vcpu->arch.sane) {
7637ec21d9dSTianjia Zhang 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
764af8f38b3SAlexander Graf 		return -EINVAL;
765af8f38b3SAlexander Graf 	}
766af8f38b3SAlexander Graf 
7677ee78855SAlexander Graf 	s = kvmppc_prepare_to_enter(vcpu);
7687ee78855SAlexander Graf 	if (s <= 0) {
7697ee78855SAlexander Graf 		ret = s;
7701d1ef222SScott Wood 		goto out;
7711d1ef222SScott Wood 	}
7726c85f52bSScott Wood 	/* interrupts now hard-disabled */
7731d1ef222SScott Wood 
7748fae845fSScott Wood #ifdef CONFIG_PPC_FPU
7758fae845fSScott Wood 	/* Save userspace FPU state in stack */
7768fae845fSScott Wood 	enable_kernel_fp();
7778fae845fSScott Wood 
7788fae845fSScott Wood 	/*
7798fae845fSScott Wood 	 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
7803efc7da6SMihai Caraman 	 * as always using the FPU.
7818fae845fSScott Wood 	 */
7828fae845fSScott Wood 	kvmppc_load_guest_fp(vcpu);
7838fae845fSScott Wood #endif
7848fae845fSScott Wood 
78595d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
78695d80a29SMihai Caraman 	/* Save userspace AltiVec state in stack */
78795d80a29SMihai Caraman 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
78895d80a29SMihai Caraman 		enable_kernel_altivec();
78995d80a29SMihai Caraman 	/*
79095d80a29SMihai Caraman 	 * Since we can't trap on MSR_VEC in GS-mode, we consider the guest
79195d80a29SMihai Caraman 	 * as always using the AltiVec.
79295d80a29SMihai Caraman 	 */
79395d80a29SMihai Caraman 	kvmppc_load_guest_altivec(vcpu);
79495d80a29SMihai Caraman #endif
79595d80a29SMihai Caraman 
796ce11e48bSBharat Bhushan 	/* Switch to guest debug context */
797348ba710SBharat Bhushan 	debug = vcpu->arch.dbg_reg;
798f5f97210SScott Wood 	switch_booke_debug_regs(&debug);
799f5f97210SScott Wood 	debug = current->thread.debug;
800348ba710SBharat Bhushan 	current->thread.debug = vcpu->arch.dbg_reg;
801ce11e48bSBharat Bhushan 
802e1bd0a7eSLeonardo Bras 	vcpu->arch.pgdir = vcpu->kvm->mm->pgd;
8035f1c248fSScott Wood 	kvmppc_fix_ee_before_entry();
804f8941fbeSScott Wood 
8057ec21d9dSTianjia Zhang 	ret = __kvmppc_vcpu_run(vcpu);
8068fae845fSScott Wood 
8076edaa530SPaolo Bonzini 	/* No need for guest_exit. It's done in handle_exit.
80824afa37bSAlexander Graf 	   We also get here with interrupts enabled. */
80924afa37bSAlexander Graf 
810ce11e48bSBharat Bhushan 	/* Switch back to user space debug context */
811f5f97210SScott Wood 	switch_booke_debug_regs(&debug);
812f5f97210SScott Wood 	current->thread.debug = debug;
813ce11e48bSBharat Bhushan 
8148fae845fSScott Wood #ifdef CONFIG_PPC_FPU
8158fae845fSScott Wood 	kvmppc_save_guest_fp(vcpu);
8168fae845fSScott Wood #endif
8178fae845fSScott Wood 
81895d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
81995d80a29SMihai Caraman 	kvmppc_save_guest_altivec(vcpu);
82095d80a29SMihai Caraman #endif
82195d80a29SMihai Caraman 
8221d1ef222SScott Wood out:
823d69c6436SAlexander Graf 	vcpu->mode = OUTSIDE_GUEST_MODE;
824df6909e5SPaul Mackerras 	return ret;
825df6909e5SPaul Mackerras }
826df6909e5SPaul Mackerras 
8278c99d345STianjia Zhang static int emulation_exit(struct kvm_vcpu *vcpu)
828d9fbd03dSHollis Blanchard {
829d9fbd03dSHollis Blanchard 	enum emulation_result er;
830d9fbd03dSHollis Blanchard 
8318c99d345STianjia Zhang 	er = kvmppc_emulate_instruction(vcpu);
832d9fbd03dSHollis Blanchard 	switch (er) {
833d9fbd03dSHollis Blanchard 	case EMULATE_DONE:
83473e75b41SHollis Blanchard 		/* don't overwrite subtypes, just account kvm_stats */
8357b701591SHollis Blanchard 		kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
836d9fbd03dSHollis Blanchard 		/* Future optimization: only reload non-volatiles if
837d9fbd03dSHollis Blanchard 		 * they were actually modified by emulation. */
838d30f6e48SScott Wood 		return RESUME_GUEST_NV;
839d30f6e48SScott Wood 
84051f04726SMihai Caraman 	case EMULATE_AGAIN:
84151f04726SMihai Caraman 		return RESUME_GUEST;
84251f04726SMihai Caraman 
843d9fbd03dSHollis Blanchard 	case EMULATE_FAIL:
8445cf8ca22SHollis Blanchard 		printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
845173c520aSSimon Guo 		       __func__, vcpu->arch.regs.nip, vcpu->arch.last_inst);
846d9fbd03dSHollis Blanchard 		/* For debugging, encode the failing instruction and
847d9fbd03dSHollis Blanchard 		 * report it to userspace. */
8488c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
8498c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
850d1ff5499SAlexander Graf 		kvmppc_core_queue_program(vcpu, ESR_PIL);
851d30f6e48SScott Wood 		return RESUME_HOST;
852d30f6e48SScott Wood 
8539b4f5308SBharat Bhushan 	case EMULATE_EXIT_USER:
8549b4f5308SBharat Bhushan 		return RESUME_HOST;
8559b4f5308SBharat Bhushan 
856d9fbd03dSHollis Blanchard 	default:
857d9fbd03dSHollis Blanchard 		BUG();
858d9fbd03dSHollis Blanchard 	}
859d30f6e48SScott Wood }
860d30f6e48SScott Wood 
8618c99d345STianjia Zhang static int kvmppc_handle_debug(struct kvm_vcpu *vcpu)
862ce11e48bSBharat Bhushan {
8638c99d345STianjia Zhang 	struct kvm_run *run = vcpu->run;
864348ba710SBharat Bhushan 	struct debug_reg *dbg_reg = &(vcpu->arch.dbg_reg);
865ce11e48bSBharat Bhushan 	u32 dbsr = vcpu->arch.dbsr;
866ce11e48bSBharat Bhushan 
8672f699a59SBharat Bhushan 	if (vcpu->guest_debug == 0) {
8682f699a59SBharat Bhushan 		/*
8692f699a59SBharat Bhushan 		 * Debug resources belong to Guest.
8702f699a59SBharat Bhushan 		 * Imprecise debug event is not injected
8712f699a59SBharat Bhushan 		 */
8722f699a59SBharat Bhushan 		if (dbsr & DBSR_IDE) {
8732f699a59SBharat Bhushan 			dbsr &= ~DBSR_IDE;
8742f699a59SBharat Bhushan 			if (!dbsr)
8752f699a59SBharat Bhushan 				return RESUME_GUEST;
8762f699a59SBharat Bhushan 		}
8772f699a59SBharat Bhushan 
8782f699a59SBharat Bhushan 		if (dbsr && (vcpu->arch.shared->msr & MSR_DE) &&
8792f699a59SBharat Bhushan 			    (vcpu->arch.dbg_reg.dbcr0 & DBCR0_IDM))
8802f699a59SBharat Bhushan 			kvmppc_core_queue_debug(vcpu);
8812f699a59SBharat Bhushan 
8822f699a59SBharat Bhushan 		/* Inject a program interrupt if trap debug is not allowed */
8832f699a59SBharat Bhushan 		if ((dbsr & DBSR_TIE) && !(vcpu->arch.shared->msr & MSR_DE))
8842f699a59SBharat Bhushan 			kvmppc_core_queue_program(vcpu, ESR_PTR);
8852f699a59SBharat Bhushan 
8862f699a59SBharat Bhushan 		return RESUME_GUEST;
8872f699a59SBharat Bhushan 	}
8882f699a59SBharat Bhushan 
8892f699a59SBharat Bhushan 	/*
8902f699a59SBharat Bhushan 	 * Debug resource owned by userspace.
8912f699a59SBharat Bhushan 	 * Clear guest dbsr (vcpu->arch.dbsr)
8922f699a59SBharat Bhushan 	 */
8932190991eSBharat Bhushan 	vcpu->arch.dbsr = 0;
894ce11e48bSBharat Bhushan 	run->debug.arch.status = 0;
895173c520aSSimon Guo 	run->debug.arch.address = vcpu->arch.regs.nip;
896ce11e48bSBharat Bhushan 
897ce11e48bSBharat Bhushan 	if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
898ce11e48bSBharat Bhushan 		run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
899ce11e48bSBharat Bhushan 	} else {
900ce11e48bSBharat Bhushan 		if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
901ce11e48bSBharat Bhushan 			run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
902ce11e48bSBharat Bhushan 		else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
903ce11e48bSBharat Bhushan 			run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
904ce11e48bSBharat Bhushan 		if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
905ce11e48bSBharat Bhushan 			run->debug.arch.address = dbg_reg->dac1;
906ce11e48bSBharat Bhushan 		else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
907ce11e48bSBharat Bhushan 			run->debug.arch.address = dbg_reg->dac2;
908ce11e48bSBharat Bhushan 	}
909ce11e48bSBharat Bhushan 
910ce11e48bSBharat Bhushan 	return RESUME_HOST;
911ce11e48bSBharat Bhushan }
912ce11e48bSBharat Bhushan 
9134e642ccbSAlexander Graf static void kvmppc_fill_pt_regs(struct pt_regs *regs)
9144e642ccbSAlexander Graf {
9154e642ccbSAlexander Graf 	ulong r1, ip, msr, lr;
9164e642ccbSAlexander Graf 
9174e642ccbSAlexander Graf 	asm("mr %0, 1" : "=r"(r1));
9184e642ccbSAlexander Graf 	asm("mflr %0" : "=r"(lr));
9194e642ccbSAlexander Graf 	asm("mfmsr %0" : "=r"(msr));
9204e642ccbSAlexander Graf 	asm("bl 1f; 1: mflr %0" : "=r"(ip));
9214e642ccbSAlexander Graf 
9224e642ccbSAlexander Graf 	memset(regs, 0, sizeof(*regs));
9234e642ccbSAlexander Graf 	regs->gpr[1] = r1;
9244e642ccbSAlexander Graf 	regs->nip = ip;
9254e642ccbSAlexander Graf 	regs->msr = msr;
9264e642ccbSAlexander Graf 	regs->link = lr;
9274e642ccbSAlexander Graf }
9284e642ccbSAlexander Graf 
9296328e593SBharat Bhushan /*
9306328e593SBharat Bhushan  * For interrupts needed to be handled by host interrupt handlers,
9316328e593SBharat Bhushan  * corresponding host handler are called from here in similar way
9326328e593SBharat Bhushan  * (but not exact) as they are called from low level handler
9336328e593SBharat Bhushan  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
9346328e593SBharat Bhushan  */
9354e642ccbSAlexander Graf static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
9364e642ccbSAlexander Graf 				     unsigned int exit_nr)
9374e642ccbSAlexander Graf {
9384e642ccbSAlexander Graf 	struct pt_regs regs;
9394e642ccbSAlexander Graf 
9404e642ccbSAlexander Graf 	switch (exit_nr) {
9414e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_EXTERNAL:
9424e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9434e642ccbSAlexander Graf 		do_IRQ(&regs);
9444e642ccbSAlexander Graf 		break;
9454e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DECREMENTER:
9464e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9474e642ccbSAlexander Graf 		timer_interrupt(&regs);
9484e642ccbSAlexander Graf 		break;
9495f17ce8bSTiejun Chen #if defined(CONFIG_PPC_DOORBELL)
9504e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DOORBELL:
9514e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9524e642ccbSAlexander Graf 		doorbell_exception(&regs);
9534e642ccbSAlexander Graf 		break;
9544e642ccbSAlexander Graf #endif
9554e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_MACHINE_CHECK:
9564e642ccbSAlexander Graf 		/* FIXME */
9574e642ccbSAlexander Graf 		break;
9587cc1e8eeSAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
9597cc1e8eeSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9607cc1e8eeSAlexander Graf 		performance_monitor_exception(&regs);
9617cc1e8eeSAlexander Graf 		break;
9626328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
9636328e593SBharat Bhushan 		kvmppc_fill_pt_regs(&regs);
9646328e593SBharat Bhushan #ifdef CONFIG_BOOKE_WDT
9656328e593SBharat Bhushan 		WatchdogException(&regs);
9666328e593SBharat Bhushan #else
9676328e593SBharat Bhushan 		unknown_exception(&regs);
9686328e593SBharat Bhushan #endif
9696328e593SBharat Bhushan 		break;
9706328e593SBharat Bhushan 	case BOOKE_INTERRUPT_CRITICAL:
971845ac985STudor Laurentiu 		kvmppc_fill_pt_regs(&regs);
9726328e593SBharat Bhushan 		unknown_exception(&regs);
9736328e593SBharat Bhushan 		break;
974ce11e48bSBharat Bhushan 	case BOOKE_INTERRUPT_DEBUG:
975ce11e48bSBharat Bhushan 		/* Save DBSR before preemption is enabled */
976ce11e48bSBharat Bhushan 		vcpu->arch.dbsr = mfspr(SPRN_DBSR);
977ce11e48bSBharat Bhushan 		kvmppc_clear_dbsr();
978ce11e48bSBharat Bhushan 		break;
9794e642ccbSAlexander Graf 	}
9804e642ccbSAlexander Graf }
9814e642ccbSAlexander Graf 
9828c99d345STianjia Zhang static int kvmppc_resume_inst_load(struct kvm_vcpu *vcpu,
983f5250471SMihai Caraman 				  enum emulation_result emulated, u32 last_inst)
984f5250471SMihai Caraman {
985f5250471SMihai Caraman 	switch (emulated) {
986f5250471SMihai Caraman 	case EMULATE_AGAIN:
987f5250471SMihai Caraman 		return RESUME_GUEST;
988f5250471SMihai Caraman 
989f5250471SMihai Caraman 	case EMULATE_FAIL:
990f5250471SMihai Caraman 		pr_debug("%s: load instruction from guest address %lx failed\n",
991173c520aSSimon Guo 		       __func__, vcpu->arch.regs.nip);
992f5250471SMihai Caraman 		/* For debugging, encode the failing instruction and
993f5250471SMihai Caraman 		 * report it to userspace. */
9948c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
9958c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason |= last_inst;
996f5250471SMihai Caraman 		kvmppc_core_queue_program(vcpu, ESR_PIL);
997f5250471SMihai Caraman 		return RESUME_HOST;
998f5250471SMihai Caraman 
999f5250471SMihai Caraman 	default:
1000f5250471SMihai Caraman 		BUG();
1001f5250471SMihai Caraman 	}
1002f5250471SMihai Caraman }
1003f5250471SMihai Caraman 
1004d30f6e48SScott Wood /**
1005d30f6e48SScott Wood  * kvmppc_handle_exit
1006d30f6e48SScott Wood  *
1007d30f6e48SScott Wood  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1008d30f6e48SScott Wood  */
10097ec21d9dSTianjia Zhang int kvmppc_handle_exit(struct kvm_vcpu *vcpu, unsigned int exit_nr)
1010d30f6e48SScott Wood {
10117ec21d9dSTianjia Zhang 	struct kvm_run *run = vcpu->run;
1012d30f6e48SScott Wood 	int r = RESUME_HOST;
10137ee78855SAlexander Graf 	int s;
1014f1e89028SScott Wood 	int idx;
1015f5250471SMihai Caraman 	u32 last_inst = KVM_INST_FETCH_FAILED;
1016f5250471SMihai Caraman 	enum emulation_result emulated = EMULATE_DONE;
1017d30f6e48SScott Wood 
1018*6c645b01SNicholas Piggin 	/* Fix irq state (pairs with kvmppc_fix_ee_before_entry()) */
1019*6c645b01SNicholas Piggin 	kvmppc_fix_ee_after_exit();
1020*6c645b01SNicholas Piggin 
1021d30f6e48SScott Wood 	/* update before a new last_exit_type is rewritten */
1022d30f6e48SScott Wood 	kvmppc_update_timing_stats(vcpu);
1023d30f6e48SScott Wood 
10244e642ccbSAlexander Graf 	/* restart interrupts if they were meant for the host */
10254e642ccbSAlexander Graf 	kvmppc_restart_interrupt(vcpu, exit_nr);
1026d30f6e48SScott Wood 
1027f5250471SMihai Caraman 	/*
1028446957baSAdam Buchbinder 	 * get last instruction before being preempted
1029f5250471SMihai Caraman 	 * TODO: for e6500 check also BOOKE_INTERRUPT_LRAT_ERROR & ESR_DATA
1030f5250471SMihai Caraman 	 */
1031f5250471SMihai Caraman 	switch (exit_nr) {
1032f5250471SMihai Caraman 	case BOOKE_INTERRUPT_DATA_STORAGE:
1033f5250471SMihai Caraman 	case BOOKE_INTERRUPT_DTLB_MISS:
1034f5250471SMihai Caraman 	case BOOKE_INTERRUPT_HV_PRIV:
10358d0eff63SAlexander Graf 		emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
1036f5250471SMihai Caraman 		break;
1037033aaa14SMadhavan Srinivasan 	case BOOKE_INTERRUPT_PROGRAM:
1038033aaa14SMadhavan Srinivasan 		/* SW breakpoints arrive as illegal instructions on HV */
1039033aaa14SMadhavan Srinivasan 		if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
10408d0eff63SAlexander Graf 			emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
1041033aaa14SMadhavan Srinivasan 		break;
1042f5250471SMihai Caraman 	default:
1043f5250471SMihai Caraman 		break;
1044f5250471SMihai Caraman 	}
1045f5250471SMihai Caraman 
104697c95059SAlexander Graf 	trace_kvm_exit(exit_nr, vcpu);
1047235cee16SLaurent Vivier 
1048235cee16SLaurent Vivier 	context_tracking_guest_exit();
1049235cee16SLaurent Vivier 	if (!vtime_accounting_enabled_this_cpu()) {
1050235cee16SLaurent Vivier 		local_irq_enable();
1051235cee16SLaurent Vivier 		/*
1052235cee16SLaurent Vivier 		 * Service IRQs here before vtime_account_guest_exit() so any
1053235cee16SLaurent Vivier 		 * ticks that occurred while running the guest are accounted to
1054235cee16SLaurent Vivier 		 * the guest. If vtime accounting is enabled, accounting uses
1055235cee16SLaurent Vivier 		 * TB rather than ticks, so it can be done without enabling
1056235cee16SLaurent Vivier 		 * interrupts here, which has the problem that it accounts
1057235cee16SLaurent Vivier 		 * interrupt processing overhead to the host.
1058235cee16SLaurent Vivier 		 */
1059235cee16SLaurent Vivier 		local_irq_disable();
1060235cee16SLaurent Vivier 	}
1061235cee16SLaurent Vivier 	vtime_account_guest_exit();
1062e233d54dSPaolo Bonzini 
1063e233d54dSPaolo Bonzini 	local_irq_enable();
106497c95059SAlexander Graf 
1065d30f6e48SScott Wood 	run->exit_reason = KVM_EXIT_UNKNOWN;
1066d30f6e48SScott Wood 	run->ready_for_interrupt_injection = 1;
1067d30f6e48SScott Wood 
1068f5250471SMihai Caraman 	if (emulated != EMULATE_DONE) {
10698c99d345STianjia Zhang 		r = kvmppc_resume_inst_load(vcpu, emulated, last_inst);
1070f5250471SMihai Caraman 		goto out;
1071f5250471SMihai Caraman 	}
1072f5250471SMihai Caraman 
1073d30f6e48SScott Wood 	switch (exit_nr) {
1074d30f6e48SScott Wood 	case BOOKE_INTERRUPT_MACHINE_CHECK:
1075c35c9d84SAlexander Graf 		printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
1076c35c9d84SAlexander Graf 		kvmppc_dump_vcpu(vcpu);
1077c35c9d84SAlexander Graf 		/* For debugging, send invalid exit reason to user space */
1078c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason = ~1ULL << 32;
1079c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
1080c35c9d84SAlexander Graf 		r = RESUME_HOST;
1081d30f6e48SScott Wood 		break;
1082d30f6e48SScott Wood 
1083d30f6e48SScott Wood 	case BOOKE_INTERRUPT_EXTERNAL:
1084d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
1085d30f6e48SScott Wood 		r = RESUME_GUEST;
1086d30f6e48SScott Wood 		break;
1087d30f6e48SScott Wood 
1088d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DECREMENTER:
1089d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DEC_EXITS);
1090d30f6e48SScott Wood 		r = RESUME_GUEST;
1091d30f6e48SScott Wood 		break;
1092d30f6e48SScott Wood 
10936328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
10946328e593SBharat Bhushan 		r = RESUME_GUEST;
10956328e593SBharat Bhushan 		break;
10966328e593SBharat Bhushan 
1097d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DOORBELL:
1098d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DBELL_EXITS);
1099d30f6e48SScott Wood 		r = RESUME_GUEST;
1100d30f6e48SScott Wood 		break;
1101d30f6e48SScott Wood 
1102d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
1103d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
1104d30f6e48SScott Wood 
1105d30f6e48SScott Wood 		/*
1106d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
1107d30f6e48SScott Wood 		 * which could not be delivered as MSR_CE or MSR_ME was not
1108d30f6e48SScott Wood 		 * set.  Once we break from here we will retry delivery.
1109d30f6e48SScott Wood 		 */
1110d30f6e48SScott Wood 		r = RESUME_GUEST;
1111d30f6e48SScott Wood 		break;
1112d30f6e48SScott Wood 
1113d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL:
1114d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
1115d30f6e48SScott Wood 
1116d30f6e48SScott Wood 		/*
1117d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
1118d30f6e48SScott Wood 		 * which could not be delivered as MSR_EE was not set.  Once
1119d30f6e48SScott Wood 		 * we break from here we will retry delivery.
1120d30f6e48SScott Wood 		 */
1121d30f6e48SScott Wood 		r = RESUME_GUEST;
1122d30f6e48SScott Wood 		break;
1123d30f6e48SScott Wood 
112495f2e921SAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
112595f2e921SAlexander Graf 		r = RESUME_GUEST;
112695f2e921SAlexander Graf 		break;
112795f2e921SAlexander Graf 
1128d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_PRIV:
11298c99d345STianjia Zhang 		r = emulation_exit(vcpu);
1130d30f6e48SScott Wood 		break;
1131d30f6e48SScott Wood 
1132d30f6e48SScott Wood 	case BOOKE_INTERRUPT_PROGRAM:
1133033aaa14SMadhavan Srinivasan 		if ((vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) &&
1134033aaa14SMadhavan Srinivasan 			(last_inst == KVMPPC_INST_SW_BREAKPOINT)) {
1135033aaa14SMadhavan Srinivasan 			/*
1136033aaa14SMadhavan Srinivasan 			 * We are here because of an SW breakpoint instr,
1137033aaa14SMadhavan Srinivasan 			 * so lets return to host to handle.
1138033aaa14SMadhavan Srinivasan 			 */
11398c99d345STianjia Zhang 			r = kvmppc_handle_debug(vcpu);
1140033aaa14SMadhavan Srinivasan 			run->exit_reason = KVM_EXIT_DEBUG;
1141033aaa14SMadhavan Srinivasan 			kvmppc_account_exit(vcpu, DEBUG_EXITS);
1142033aaa14SMadhavan Srinivasan 			break;
1143033aaa14SMadhavan Srinivasan 		}
1144033aaa14SMadhavan Srinivasan 
1145d30f6e48SScott Wood 		if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
11460268597cSAlexander Graf 			/*
11470268597cSAlexander Graf 			 * Program traps generated by user-level software must
11480268597cSAlexander Graf 			 * be handled by the guest kernel.
11490268597cSAlexander Graf 			 *
11500268597cSAlexander Graf 			 * In GS mode, hypervisor privileged instructions trap
11510268597cSAlexander Graf 			 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
11520268597cSAlexander Graf 			 * actual program interrupts, handled by the guest.
11530268597cSAlexander Graf 			 */
1154d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
1155d30f6e48SScott Wood 			r = RESUME_GUEST;
1156d30f6e48SScott Wood 			kvmppc_account_exit(vcpu, USR_PR_INST);
1157d30f6e48SScott Wood 			break;
1158d30f6e48SScott Wood 		}
1159d30f6e48SScott Wood 
11608c99d345STianjia Zhang 		r = emulation_exit(vcpu);
1161d9fbd03dSHollis Blanchard 		break;
1162d9fbd03dSHollis Blanchard 
1163d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FP_UNAVAIL:
1164d4cf3892SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
11657b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, FP_UNAVAIL);
1166d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1167d9fbd03dSHollis Blanchard 		break;
1168d9fbd03dSHollis Blanchard 
11694cd35f67SScott Wood #ifdef CONFIG_SPE
11704cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL: {
11714cd35f67SScott Wood 		if (vcpu->arch.shared->msr & MSR_SPE)
11724cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
11734cd35f67SScott Wood 		else
11744cd35f67SScott Wood 			kvmppc_booke_queue_irqprio(vcpu,
11754cd35f67SScott Wood 						   BOOKE_IRQPRIO_SPE_UNAVAIL);
1176bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1177bb3a8a17SHollis Blanchard 		break;
11784cd35f67SScott Wood 	}
1179bb3a8a17SHollis Blanchard 
1180bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_DATA:
1181bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
1182bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1183bb3a8a17SHollis Blanchard 		break;
1184bb3a8a17SHollis Blanchard 
1185bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
1186bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
1187bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1188bb3a8a17SHollis Blanchard 		break;
118995d80a29SMihai Caraman #elif defined(CONFIG_SPE_POSSIBLE)
11904cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL:
11914cd35f67SScott Wood 		/*
11924cd35f67SScott Wood 		 * Guest wants SPE, but host kernel doesn't support it.  Send
11934cd35f67SScott Wood 		 * an "unimplemented operation" program check to the guest.
11944cd35f67SScott Wood 		 */
11954cd35f67SScott Wood 		kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
11964cd35f67SScott Wood 		r = RESUME_GUEST;
11974cd35f67SScott Wood 		break;
11984cd35f67SScott Wood 
11994cd35f67SScott Wood 	/*
12004cd35f67SScott Wood 	 * These really should never happen without CONFIG_SPE,
12014cd35f67SScott Wood 	 * as we should never enable the real MSR[SPE] in the guest.
12024cd35f67SScott Wood 	 */
12034cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_DATA:
12044cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
12054cd35f67SScott Wood 		printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
1206173c520aSSimon Guo 		       __func__, exit_nr, vcpu->arch.regs.nip);
12074cd35f67SScott Wood 		run->hw.hardware_exit_reason = exit_nr;
12084cd35f67SScott Wood 		r = RESUME_HOST;
12094cd35f67SScott Wood 		break;
121095d80a29SMihai Caraman #endif /* CONFIG_SPE_POSSIBLE */
121195d80a29SMihai Caraman 
121295d80a29SMihai Caraman /*
121395d80a29SMihai Caraman  * On cores with Vector category, KVM is loaded only if CONFIG_ALTIVEC,
121495d80a29SMihai Caraman  * see kvmppc_core_check_processor_compat().
121595d80a29SMihai Caraman  */
121695d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
121795d80a29SMihai Caraman 	case BOOKE_INTERRUPT_ALTIVEC_UNAVAIL:
121895d80a29SMihai Caraman 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
121995d80a29SMihai Caraman 		r = RESUME_GUEST;
122095d80a29SMihai Caraman 		break;
122195d80a29SMihai Caraman 
122295d80a29SMihai Caraman 	case BOOKE_INTERRUPT_ALTIVEC_ASSIST:
122395d80a29SMihai Caraman 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_ASSIST);
122495d80a29SMihai Caraman 		r = RESUME_GUEST;
122595d80a29SMihai Caraman 		break;
12264cd35f67SScott Wood #endif
1227bb3a8a17SHollis Blanchard 
1228d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DATA_STORAGE:
1229daf5e271SLiu Yu 		kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
1230daf5e271SLiu Yu 		                               vcpu->arch.fault_esr);
12317b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DSI_EXITS);
1232d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1233d9fbd03dSHollis Blanchard 		break;
1234d9fbd03dSHollis Blanchard 
1235d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_INST_STORAGE:
1236daf5e271SLiu Yu 		kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
12377b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ISI_EXITS);
1238d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1239d9fbd03dSHollis Blanchard 		break;
1240d9fbd03dSHollis Blanchard 
1241011da899SAlexander Graf 	case BOOKE_INTERRUPT_ALIGNMENT:
1242011da899SAlexander Graf 		kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1243011da899SAlexander Graf 		                            vcpu->arch.fault_esr);
1244011da899SAlexander Graf 		r = RESUME_GUEST;
1245011da899SAlexander Graf 		break;
1246011da899SAlexander Graf 
1247d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
1248d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_SYSCALL:
1249d30f6e48SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR)) {
1250d30f6e48SScott Wood 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1251d30f6e48SScott Wood 		} else {
1252d30f6e48SScott Wood 			/*
1253d30f6e48SScott Wood 			 * hcall from guest userspace -- send privileged
1254d30f6e48SScott Wood 			 * instruction program check.
1255d30f6e48SScott Wood 			 */
1256d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, ESR_PPR);
1257d30f6e48SScott Wood 		}
1258d30f6e48SScott Wood 
1259d30f6e48SScott Wood 		r = RESUME_GUEST;
1260d30f6e48SScott Wood 		break;
1261d30f6e48SScott Wood #else
1262d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_SYSCALL:
12632a342ed5SAlexander Graf 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
12642a342ed5SAlexander Graf 		    (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
12652a342ed5SAlexander Graf 			/* KVM PV hypercalls */
12662a342ed5SAlexander Graf 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
12672a342ed5SAlexander Graf 			r = RESUME_GUEST;
12682a342ed5SAlexander Graf 		} else {
12692a342ed5SAlexander Graf 			/* Guest syscalls */
1270d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
12712a342ed5SAlexander Graf 		}
12727b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, SYSCALL_EXITS);
1273d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1274d9fbd03dSHollis Blanchard 		break;
1275d30f6e48SScott Wood #endif
1276d9fbd03dSHollis Blanchard 
1277d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DTLB_MISS: {
1278d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.fault_dear;
12797924bd41SHollis Blanchard 		int gtlb_index;
1280475e7cddSHollis Blanchard 		gpa_t gpaddr;
1281d9fbd03dSHollis Blanchard 		gfn_t gfn;
1282d9fbd03dSHollis Blanchard 
1283bf7ca4bdSAlexander Graf #ifdef CONFIG_KVM_E500V2
1284a4cd8b23SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
1285a4cd8b23SScott Wood 		    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1286a4cd8b23SScott Wood 			kvmppc_map_magic(vcpu);
1287a4cd8b23SScott Wood 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1288a4cd8b23SScott Wood 			r = RESUME_GUEST;
1289a4cd8b23SScott Wood 
1290a4cd8b23SScott Wood 			break;
1291a4cd8b23SScott Wood 		}
1292a4cd8b23SScott Wood #endif
1293a4cd8b23SScott Wood 
1294d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
1295fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
12967924bd41SHollis Blanchard 		if (gtlb_index < 0) {
1297d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
1298daf5e271SLiu Yu 			kvmppc_core_queue_dtlb_miss(vcpu,
1299daf5e271SLiu Yu 			                            vcpu->arch.fault_dear,
1300daf5e271SLiu Yu 			                            vcpu->arch.fault_esr);
1301b52a638cSHollis Blanchard 			kvmppc_mmu_dtlb_miss(vcpu);
13027b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
1303d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
1304d9fbd03dSHollis Blanchard 			break;
1305d9fbd03dSHollis Blanchard 		}
1306d9fbd03dSHollis Blanchard 
1307f1e89028SScott Wood 		idx = srcu_read_lock(&vcpu->kvm->srcu);
1308f1e89028SScott Wood 
1309be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1310475e7cddSHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
1311d9fbd03dSHollis Blanchard 
1312d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1313d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
1314d9fbd03dSHollis Blanchard 			 * didn't, and it is RAM. This could be because:
1315d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
1316d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
1317d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
1318d9fbd03dSHollis Blanchard 			 * invoking the guest. */
131958a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
13207b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1321d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
1322d9fbd03dSHollis Blanchard 		} else {
1323d9fbd03dSHollis Blanchard 			/* Guest has mapped and accessed a page which is not
1324d9fbd03dSHollis Blanchard 			 * actually RAM. */
1325475e7cddSHollis Blanchard 			vcpu->arch.paddr_accessed = gpaddr;
13266020c0f6SAlexander Graf 			vcpu->arch.vaddr_accessed = eaddr;
13278c99d345STianjia Zhang 			r = kvmppc_emulate_mmio(vcpu);
13287b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, MMIO_EXITS);
1329d9fbd03dSHollis Blanchard 		}
1330d9fbd03dSHollis Blanchard 
1331f1e89028SScott Wood 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
1332d9fbd03dSHollis Blanchard 		break;
1333d9fbd03dSHollis Blanchard 	}
1334d9fbd03dSHollis Blanchard 
1335d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_ITLB_MISS: {
1336173c520aSSimon Guo 		unsigned long eaddr = vcpu->arch.regs.nip;
133789168618SHollis Blanchard 		gpa_t gpaddr;
1338d9fbd03dSHollis Blanchard 		gfn_t gfn;
13397924bd41SHollis Blanchard 		int gtlb_index;
1340d9fbd03dSHollis Blanchard 
1341d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1342d9fbd03dSHollis Blanchard 
1343d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
1344fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
13457924bd41SHollis Blanchard 		if (gtlb_index < 0) {
1346d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
1347d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
1348b52a638cSHollis Blanchard 			kvmppc_mmu_itlb_miss(vcpu);
13497b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
1350d9fbd03dSHollis Blanchard 			break;
1351d9fbd03dSHollis Blanchard 		}
1352d9fbd03dSHollis Blanchard 
13537b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
1354d9fbd03dSHollis Blanchard 
1355f1e89028SScott Wood 		idx = srcu_read_lock(&vcpu->kvm->srcu);
1356f1e89028SScott Wood 
1357be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
135889168618SHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
1359d9fbd03dSHollis Blanchard 
1360d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1361d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
1362d9fbd03dSHollis Blanchard 			 * didn't. This could be because:
1363d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
1364d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
1365d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
1366d9fbd03dSHollis Blanchard 			 * invoking the guest. */
136758a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
1368d9fbd03dSHollis Blanchard 		} else {
1369d9fbd03dSHollis Blanchard 			/* Guest mapped and leaped at non-RAM! */
1370d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
1371d9fbd03dSHollis Blanchard 		}
1372d9fbd03dSHollis Blanchard 
1373f1e89028SScott Wood 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
1374d9fbd03dSHollis Blanchard 		break;
1375d9fbd03dSHollis Blanchard 	}
1376d9fbd03dSHollis Blanchard 
1377d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG: {
13788c99d345STianjia Zhang 		r = kvmppc_handle_debug(vcpu);
1379ce11e48bSBharat Bhushan 		if (r == RESUME_HOST)
1380d9fbd03dSHollis Blanchard 			run->exit_reason = KVM_EXIT_DEBUG;
13817b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DEBUG_EXITS);
1382d9fbd03dSHollis Blanchard 		break;
1383d9fbd03dSHollis Blanchard 	}
1384d9fbd03dSHollis Blanchard 
1385d9fbd03dSHollis Blanchard 	default:
1386d9fbd03dSHollis Blanchard 		printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1387d9fbd03dSHollis Blanchard 		BUG();
1388d9fbd03dSHollis Blanchard 	}
1389d9fbd03dSHollis Blanchard 
1390f5250471SMihai Caraman out:
1391a8e4ef84SAlexander Graf 	/*
1392a8e4ef84SAlexander Graf 	 * To avoid clobbering exit_reason, only check for signals if we
1393a8e4ef84SAlexander Graf 	 * aren't already exiting to userspace for some other reason.
1394a8e4ef84SAlexander Graf 	 */
139503660ba2SAlexander Graf 	if (!(r & RESUME_HOST)) {
13967ee78855SAlexander Graf 		s = kvmppc_prepare_to_enter(vcpu);
13976c85f52bSScott Wood 		if (s <= 0)
13987ee78855SAlexander Graf 			r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
13996c85f52bSScott Wood 		else {
14006c85f52bSScott Wood 			/* interrupts now hard-disabled */
14015f1c248fSScott Wood 			kvmppc_fix_ee_before_entry();
14023efc7da6SMihai Caraman 			kvmppc_load_guest_fp(vcpu);
140395d80a29SMihai Caraman 			kvmppc_load_guest_altivec(vcpu);
140424afa37bSAlexander Graf 		}
140524afa37bSAlexander Graf 	}
1406706fb730SAlexander Graf 
1407d9fbd03dSHollis Blanchard 	return r;
1408d9fbd03dSHollis Blanchard }
1409d9fbd03dSHollis Blanchard 
1410d26f22c9SBharat Bhushan static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1411d26f22c9SBharat Bhushan {
1412d26f22c9SBharat Bhushan 	u32 old_tsr = vcpu->arch.tsr;
1413d26f22c9SBharat Bhushan 
1414d26f22c9SBharat Bhushan 	vcpu->arch.tsr = new_tsr;
1415d26f22c9SBharat Bhushan 
1416d26f22c9SBharat Bhushan 	if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1417d26f22c9SBharat Bhushan 		arm_next_watchdog(vcpu);
1418d26f22c9SBharat Bhushan 
1419d26f22c9SBharat Bhushan 	update_timer_ints(vcpu);
1420d26f22c9SBharat Bhushan }
1421d26f22c9SBharat Bhushan 
1422f61c94bbSBharat Bhushan int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1423f61c94bbSBharat Bhushan {
1424f61c94bbSBharat Bhushan 	/* setup watchdog timer once */
1425f61c94bbSBharat Bhushan 	spin_lock_init(&vcpu->arch.wdt_lock);
142686cb30ecSKees Cook 	timer_setup(&vcpu->arch.wdt_timer, kvmppc_watchdog_func, 0);
1427f61c94bbSBharat Bhushan 
14282f699a59SBharat Bhushan 	/*
14292f699a59SBharat Bhushan 	 * Clear DBSR.MRR to avoid guest debug interrupt as
14302f699a59SBharat Bhushan 	 * this is of host interest
14312f699a59SBharat Bhushan 	 */
14322f699a59SBharat Bhushan 	mtspr(SPRN_DBSR, DBSR_MRR);
1433f61c94bbSBharat Bhushan 	return 0;
1434f61c94bbSBharat Bhushan }
1435f61c94bbSBharat Bhushan 
1436f61c94bbSBharat Bhushan void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1437f61c94bbSBharat Bhushan {
1438f61c94bbSBharat Bhushan 	del_timer_sync(&vcpu->arch.wdt_timer);
1439f61c94bbSBharat Bhushan }
1440f61c94bbSBharat Bhushan 
1441d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1442d9fbd03dSHollis Blanchard {
1443d9fbd03dSHollis Blanchard 	int i;
1444d9fbd03dSHollis Blanchard 
14451fc9b76bSChristoffer Dall 	vcpu_load(vcpu);
14461fc9b76bSChristoffer Dall 
1447173c520aSSimon Guo 	regs->pc = vcpu->arch.regs.nip;
1448992b5b29SAlexander Graf 	regs->cr = kvmppc_get_cr(vcpu);
1449173c520aSSimon Guo 	regs->ctr = vcpu->arch.regs.ctr;
1450173c520aSSimon Guo 	regs->lr = vcpu->arch.regs.link;
1451992b5b29SAlexander Graf 	regs->xer = kvmppc_get_xer(vcpu);
1452666e7252SAlexander Graf 	regs->msr = vcpu->arch.shared->msr;
145331579eeaSBharat Bhushan 	regs->srr0 = kvmppc_get_srr0(vcpu);
145431579eeaSBharat Bhushan 	regs->srr1 = kvmppc_get_srr1(vcpu);
1455d9fbd03dSHollis Blanchard 	regs->pid = vcpu->arch.pid;
1456c1b8a01bSBharat Bhushan 	regs->sprg0 = kvmppc_get_sprg0(vcpu);
1457c1b8a01bSBharat Bhushan 	regs->sprg1 = kvmppc_get_sprg1(vcpu);
1458c1b8a01bSBharat Bhushan 	regs->sprg2 = kvmppc_get_sprg2(vcpu);
1459c1b8a01bSBharat Bhushan 	regs->sprg3 = kvmppc_get_sprg3(vcpu);
1460c1b8a01bSBharat Bhushan 	regs->sprg4 = kvmppc_get_sprg4(vcpu);
1461c1b8a01bSBharat Bhushan 	regs->sprg5 = kvmppc_get_sprg5(vcpu);
1462c1b8a01bSBharat Bhushan 	regs->sprg6 = kvmppc_get_sprg6(vcpu);
1463c1b8a01bSBharat Bhushan 	regs->sprg7 = kvmppc_get_sprg7(vcpu);
1464d9fbd03dSHollis Blanchard 
1465d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
14668e5b26b5SAlexander Graf 		regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1467d9fbd03dSHollis Blanchard 
14681fc9b76bSChristoffer Dall 	vcpu_put(vcpu);
1469d9fbd03dSHollis Blanchard 	return 0;
1470d9fbd03dSHollis Blanchard }
1471d9fbd03dSHollis Blanchard 
1472d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1473d9fbd03dSHollis Blanchard {
1474d9fbd03dSHollis Blanchard 	int i;
1475d9fbd03dSHollis Blanchard 
1476875656feSChristoffer Dall 	vcpu_load(vcpu);
1477875656feSChristoffer Dall 
1478173c520aSSimon Guo 	vcpu->arch.regs.nip = regs->pc;
1479992b5b29SAlexander Graf 	kvmppc_set_cr(vcpu, regs->cr);
1480173c520aSSimon Guo 	vcpu->arch.regs.ctr = regs->ctr;
1481173c520aSSimon Guo 	vcpu->arch.regs.link = regs->lr;
1482992b5b29SAlexander Graf 	kvmppc_set_xer(vcpu, regs->xer);
1483b8fd68acSHollis Blanchard 	kvmppc_set_msr(vcpu, regs->msr);
148431579eeaSBharat Bhushan 	kvmppc_set_srr0(vcpu, regs->srr0);
148531579eeaSBharat Bhushan 	kvmppc_set_srr1(vcpu, regs->srr1);
14865ce941eeSScott Wood 	kvmppc_set_pid(vcpu, regs->pid);
1487c1b8a01bSBharat Bhushan 	kvmppc_set_sprg0(vcpu, regs->sprg0);
1488c1b8a01bSBharat Bhushan 	kvmppc_set_sprg1(vcpu, regs->sprg1);
1489c1b8a01bSBharat Bhushan 	kvmppc_set_sprg2(vcpu, regs->sprg2);
1490c1b8a01bSBharat Bhushan 	kvmppc_set_sprg3(vcpu, regs->sprg3);
1491c1b8a01bSBharat Bhushan 	kvmppc_set_sprg4(vcpu, regs->sprg4);
1492c1b8a01bSBharat Bhushan 	kvmppc_set_sprg5(vcpu, regs->sprg5);
1493c1b8a01bSBharat Bhushan 	kvmppc_set_sprg6(vcpu, regs->sprg6);
1494c1b8a01bSBharat Bhushan 	kvmppc_set_sprg7(vcpu, regs->sprg7);
1495d9fbd03dSHollis Blanchard 
14968e5b26b5SAlexander Graf 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
14978e5b26b5SAlexander Graf 		kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1498d9fbd03dSHollis Blanchard 
1499875656feSChristoffer Dall 	vcpu_put(vcpu);
1500d9fbd03dSHollis Blanchard 	return 0;
1501d9fbd03dSHollis Blanchard }
1502d9fbd03dSHollis Blanchard 
15035ce941eeSScott Wood static void get_sregs_base(struct kvm_vcpu *vcpu,
15045ce941eeSScott Wood                            struct kvm_sregs *sregs)
15055ce941eeSScott Wood {
15065ce941eeSScott Wood 	u64 tb = get_tb();
15075ce941eeSScott Wood 
15085ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_BASE;
15095ce941eeSScott Wood 
15105ce941eeSScott Wood 	sregs->u.e.csrr0 = vcpu->arch.csrr0;
15115ce941eeSScott Wood 	sregs->u.e.csrr1 = vcpu->arch.csrr1;
15125ce941eeSScott Wood 	sregs->u.e.mcsr = vcpu->arch.mcsr;
1513dc168549SBharat Bhushan 	sregs->u.e.esr = kvmppc_get_esr(vcpu);
1514a5414d4bSBharat Bhushan 	sregs->u.e.dear = kvmppc_get_dar(vcpu);
15155ce941eeSScott Wood 	sregs->u.e.tsr = vcpu->arch.tsr;
15165ce941eeSScott Wood 	sregs->u.e.tcr = vcpu->arch.tcr;
15175ce941eeSScott Wood 	sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
15185ce941eeSScott Wood 	sregs->u.e.tb = tb;
15195ce941eeSScott Wood 	sregs->u.e.vrsave = vcpu->arch.vrsave;
15205ce941eeSScott Wood }
15215ce941eeSScott Wood 
15225ce941eeSScott Wood static int set_sregs_base(struct kvm_vcpu *vcpu,
15235ce941eeSScott Wood                           struct kvm_sregs *sregs)
15245ce941eeSScott Wood {
15255ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
15265ce941eeSScott Wood 		return 0;
15275ce941eeSScott Wood 
15285ce941eeSScott Wood 	vcpu->arch.csrr0 = sregs->u.e.csrr0;
15295ce941eeSScott Wood 	vcpu->arch.csrr1 = sregs->u.e.csrr1;
15305ce941eeSScott Wood 	vcpu->arch.mcsr = sregs->u.e.mcsr;
1531dc168549SBharat Bhushan 	kvmppc_set_esr(vcpu, sregs->u.e.esr);
1532a5414d4bSBharat Bhushan 	kvmppc_set_dar(vcpu, sregs->u.e.dear);
15335ce941eeSScott Wood 	vcpu->arch.vrsave = sregs->u.e.vrsave;
1534dfd4d47eSScott Wood 	kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
15355ce941eeSScott Wood 
1536dfd4d47eSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
15375ce941eeSScott Wood 		vcpu->arch.dec = sregs->u.e.dec;
15385ce941eeSScott Wood 		kvmppc_emulate_dec(vcpu);
1539dfd4d47eSScott Wood 	}
15405ce941eeSScott Wood 
1541d26f22c9SBharat Bhushan 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1542d26f22c9SBharat Bhushan 		kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
15435ce941eeSScott Wood 
15445ce941eeSScott Wood 	return 0;
15455ce941eeSScott Wood }
15465ce941eeSScott Wood 
15475ce941eeSScott Wood static void get_sregs_arch206(struct kvm_vcpu *vcpu,
15485ce941eeSScott Wood                               struct kvm_sregs *sregs)
15495ce941eeSScott Wood {
15505ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_ARCH206;
15515ce941eeSScott Wood 
1552841741f2SScott Wood 	sregs->u.e.pir = vcpu->vcpu_id;
15535ce941eeSScott Wood 	sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
15545ce941eeSScott Wood 	sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
15555ce941eeSScott Wood 	sregs->u.e.decar = vcpu->arch.decar;
15565ce941eeSScott Wood 	sregs->u.e.ivpr = vcpu->arch.ivpr;
15575ce941eeSScott Wood }
15585ce941eeSScott Wood 
15595ce941eeSScott Wood static int set_sregs_arch206(struct kvm_vcpu *vcpu,
15605ce941eeSScott Wood                              struct kvm_sregs *sregs)
15615ce941eeSScott Wood {
15625ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
15635ce941eeSScott Wood 		return 0;
15645ce941eeSScott Wood 
1565841741f2SScott Wood 	if (sregs->u.e.pir != vcpu->vcpu_id)
15665ce941eeSScott Wood 		return -EINVAL;
15675ce941eeSScott Wood 
15685ce941eeSScott Wood 	vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
15695ce941eeSScott Wood 	vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
15705ce941eeSScott Wood 	vcpu->arch.decar = sregs->u.e.decar;
15715ce941eeSScott Wood 	vcpu->arch.ivpr = sregs->u.e.ivpr;
15725ce941eeSScott Wood 
15735ce941eeSScott Wood 	return 0;
15745ce941eeSScott Wood }
15755ce941eeSScott Wood 
15763a167beaSAneesh Kumar K.V int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
15775ce941eeSScott Wood {
15785ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_IVOR;
15795ce941eeSScott Wood 
15805ce941eeSScott Wood 	sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
15815ce941eeSScott Wood 	sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
15825ce941eeSScott Wood 	sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
15835ce941eeSScott Wood 	sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
15845ce941eeSScott Wood 	sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
15855ce941eeSScott Wood 	sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
15865ce941eeSScott Wood 	sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
15875ce941eeSScott Wood 	sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
15885ce941eeSScott Wood 	sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
15895ce941eeSScott Wood 	sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
15905ce941eeSScott Wood 	sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
15915ce941eeSScott Wood 	sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
15925ce941eeSScott Wood 	sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
15935ce941eeSScott Wood 	sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
15945ce941eeSScott Wood 	sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
15955ce941eeSScott Wood 	sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
15963a167beaSAneesh Kumar K.V 	return 0;
15975ce941eeSScott Wood }
15985ce941eeSScott Wood 
15995ce941eeSScott Wood int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
16005ce941eeSScott Wood {
16015ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
16025ce941eeSScott Wood 		return 0;
16035ce941eeSScott Wood 
16045ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
16055ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
16065ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
16075ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
16085ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
16095ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
16105ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
16115ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
16125ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
16135ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
16145ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
16155ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
16165ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
16175ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
16185ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
16195ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
16205ce941eeSScott Wood 
16215ce941eeSScott Wood 	return 0;
16225ce941eeSScott Wood }
16235ce941eeSScott Wood 
1624d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1625d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1626d9fbd03dSHollis Blanchard {
1627bcdec41cSChristoffer Dall 	int ret;
1628bcdec41cSChristoffer Dall 
1629bcdec41cSChristoffer Dall 	vcpu_load(vcpu);
1630bcdec41cSChristoffer Dall 
16315ce941eeSScott Wood 	sregs->pvr = vcpu->arch.pvr;
16325ce941eeSScott Wood 
16335ce941eeSScott Wood 	get_sregs_base(vcpu, sregs);
16345ce941eeSScott Wood 	get_sregs_arch206(vcpu, sregs);
1635bcdec41cSChristoffer Dall 	ret = vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
1636bcdec41cSChristoffer Dall 
1637bcdec41cSChristoffer Dall 	vcpu_put(vcpu);
1638bcdec41cSChristoffer Dall 	return ret;
1639d9fbd03dSHollis Blanchard }
1640d9fbd03dSHollis Blanchard 
1641d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1642d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1643d9fbd03dSHollis Blanchard {
1644b4ef9d4eSChristoffer Dall 	int ret = -EINVAL;
16455ce941eeSScott Wood 
1646b4ef9d4eSChristoffer Dall 	vcpu_load(vcpu);
16475ce941eeSScott Wood 	if (vcpu->arch.pvr != sregs->pvr)
1648b4ef9d4eSChristoffer Dall 		goto out;
16495ce941eeSScott Wood 
16505ce941eeSScott Wood 	ret = set_sregs_base(vcpu, sregs);
16515ce941eeSScott Wood 	if (ret < 0)
1652b4ef9d4eSChristoffer Dall 		goto out;
16535ce941eeSScott Wood 
16545ce941eeSScott Wood 	ret = set_sregs_arch206(vcpu, sregs);
16555ce941eeSScott Wood 	if (ret < 0)
1656b4ef9d4eSChristoffer Dall 		goto out;
16575ce941eeSScott Wood 
1658b4ef9d4eSChristoffer Dall 	ret = vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
1659b4ef9d4eSChristoffer Dall 
1660b4ef9d4eSChristoffer Dall out:
1661b4ef9d4eSChristoffer Dall 	vcpu_put(vcpu);
1662b4ef9d4eSChristoffer Dall 	return ret;
1663d9fbd03dSHollis Blanchard }
1664d9fbd03dSHollis Blanchard 
16658a41ea53SMihai Caraman int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
16668a41ea53SMihai Caraman 			union kvmppc_one_reg *val)
166731f3438eSPaul Mackerras {
166835b299e2SMihai Caraman 	int r = 0;
166935b299e2SMihai Caraman 
16708a41ea53SMihai Caraman 	switch (id) {
16716df8d3fcSBharat Bhushan 	case KVM_REG_PPC_IAC1:
16728a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac1);
16736df8d3fcSBharat Bhushan 		break;
1674547465efSBharat Bhushan 	case KVM_REG_PPC_IAC2:
16758a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac2);
1676547465efSBharat Bhushan 		break;
1677547465efSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1678547465efSBharat Bhushan 	case KVM_REG_PPC_IAC3:
16798a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac3);
1680547465efSBharat Bhushan 		break;
1681547465efSBharat Bhushan 	case KVM_REG_PPC_IAC4:
16828a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac4);
1683547465efSBharat Bhushan 		break;
1684547465efSBharat Bhushan #endif
16856df8d3fcSBharat Bhushan 	case KVM_REG_PPC_DAC1:
16868a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.dac1);
1687547465efSBharat Bhushan 		break;
168835b299e2SMihai Caraman 	case KVM_REG_PPC_DAC2:
16898a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.dac2);
16902c509672SBharat Bhushan 		break;
1691324b3e63SAlexander Graf 	case KVM_REG_PPC_EPR: {
169234f754b9SBharat Bhushan 		u32 epr = kvmppc_get_epr(vcpu);
16938a41ea53SMihai Caraman 		*val = get_reg_val(id, epr);
1694324b3e63SAlexander Graf 		break;
1695324b3e63SAlexander Graf 	}
1696352df1deSMihai Caraman #if defined(CONFIG_64BIT)
1697352df1deSMihai Caraman 	case KVM_REG_PPC_EPCR:
16988a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.epcr);
1699352df1deSMihai Caraman 		break;
1700352df1deSMihai Caraman #endif
170178accda4SBharat Bhushan 	case KVM_REG_PPC_TCR:
17028a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.tcr);
170378accda4SBharat Bhushan 		break;
170478accda4SBharat Bhushan 	case KVM_REG_PPC_TSR:
17058a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.tsr);
170678accda4SBharat Bhushan 		break;
170735b299e2SMihai Caraman 	case KVM_REG_PPC_DEBUG_INST:
1708033aaa14SMadhavan Srinivasan 		*val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
17098c32a2eaSBharat Bhushan 		break;
17108b75cbbeSPaul Mackerras 	case KVM_REG_PPC_VRSAVE:
17118a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.vrsave);
17128c32a2eaSBharat Bhushan 		break;
17136df8d3fcSBharat Bhushan 	default:
17148a41ea53SMihai Caraman 		r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
17156df8d3fcSBharat Bhushan 		break;
17166df8d3fcSBharat Bhushan 	}
171735b299e2SMihai Caraman 
17186df8d3fcSBharat Bhushan 	return r;
171931f3438eSPaul Mackerras }
172031f3438eSPaul Mackerras 
17218a41ea53SMihai Caraman int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
17228a41ea53SMihai Caraman 			union kvmppc_one_reg *val)
172331f3438eSPaul Mackerras {
172435b299e2SMihai Caraman 	int r = 0;
172535b299e2SMihai Caraman 
17268a41ea53SMihai Caraman 	switch (id) {
17276df8d3fcSBharat Bhushan 	case KVM_REG_PPC_IAC1:
17288a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac1 = set_reg_val(id, *val);
17296df8d3fcSBharat Bhushan 		break;
1730547465efSBharat Bhushan 	case KVM_REG_PPC_IAC2:
17318a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac2 = set_reg_val(id, *val);
1732547465efSBharat Bhushan 		break;
1733547465efSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1734547465efSBharat Bhushan 	case KVM_REG_PPC_IAC3:
17358a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac3 = set_reg_val(id, *val);
1736547465efSBharat Bhushan 		break;
1737547465efSBharat Bhushan 	case KVM_REG_PPC_IAC4:
17388a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac4 = set_reg_val(id, *val);
1739547465efSBharat Bhushan 		break;
1740547465efSBharat Bhushan #endif
17416df8d3fcSBharat Bhushan 	case KVM_REG_PPC_DAC1:
17428a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.dac1 = set_reg_val(id, *val);
1743547465efSBharat Bhushan 		break;
174435b299e2SMihai Caraman 	case KVM_REG_PPC_DAC2:
17458a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.dac2 = set_reg_val(id, *val);
17462c509672SBharat Bhushan 		break;
1747324b3e63SAlexander Graf 	case KVM_REG_PPC_EPR: {
17488a41ea53SMihai Caraman 		u32 new_epr = set_reg_val(id, *val);
1749324b3e63SAlexander Graf 		kvmppc_set_epr(vcpu, new_epr);
1750324b3e63SAlexander Graf 		break;
1751324b3e63SAlexander Graf 	}
1752352df1deSMihai Caraman #if defined(CONFIG_64BIT)
1753352df1deSMihai Caraman 	case KVM_REG_PPC_EPCR: {
17548a41ea53SMihai Caraman 		u32 new_epcr = set_reg_val(id, *val);
1755352df1deSMihai Caraman 		kvmppc_set_epcr(vcpu, new_epcr);
1756352df1deSMihai Caraman 		break;
1757352df1deSMihai Caraman 	}
1758352df1deSMihai Caraman #endif
175978accda4SBharat Bhushan 	case KVM_REG_PPC_OR_TSR: {
17608a41ea53SMihai Caraman 		u32 tsr_bits = set_reg_val(id, *val);
176178accda4SBharat Bhushan 		kvmppc_set_tsr_bits(vcpu, tsr_bits);
176278accda4SBharat Bhushan 		break;
176378accda4SBharat Bhushan 	}
176478accda4SBharat Bhushan 	case KVM_REG_PPC_CLEAR_TSR: {
17658a41ea53SMihai Caraman 		u32 tsr_bits = set_reg_val(id, *val);
176678accda4SBharat Bhushan 		kvmppc_clr_tsr_bits(vcpu, tsr_bits);
176778accda4SBharat Bhushan 		break;
176878accda4SBharat Bhushan 	}
176978accda4SBharat Bhushan 	case KVM_REG_PPC_TSR: {
17708a41ea53SMihai Caraman 		u32 tsr = set_reg_val(id, *val);
177178accda4SBharat Bhushan 		kvmppc_set_tsr(vcpu, tsr);
177278accda4SBharat Bhushan 		break;
177378accda4SBharat Bhushan 	}
177478accda4SBharat Bhushan 	case KVM_REG_PPC_TCR: {
17758a41ea53SMihai Caraman 		u32 tcr = set_reg_val(id, *val);
177678accda4SBharat Bhushan 		kvmppc_set_tcr(vcpu, tcr);
177778accda4SBharat Bhushan 		break;
177878accda4SBharat Bhushan 	}
17798b75cbbeSPaul Mackerras 	case KVM_REG_PPC_VRSAVE:
17808a41ea53SMihai Caraman 		vcpu->arch.vrsave = set_reg_val(id, *val);
17818b75cbbeSPaul Mackerras 		break;
17826df8d3fcSBharat Bhushan 	default:
17838a41ea53SMihai Caraman 		r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
17846df8d3fcSBharat Bhushan 		break;
17856df8d3fcSBharat Bhushan 	}
178635b299e2SMihai Caraman 
17876df8d3fcSBharat Bhushan 	return r;
178831f3438eSPaul Mackerras }
178931f3438eSPaul Mackerras 
1790d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1791d9fbd03dSHollis Blanchard {
17924e1b2ab7SGreg Kurz 	return -EOPNOTSUPP;
1793d9fbd03dSHollis Blanchard }
1794d9fbd03dSHollis Blanchard 
1795d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1796d9fbd03dSHollis Blanchard {
17974e1b2ab7SGreg Kurz 	return -EOPNOTSUPP;
1798d9fbd03dSHollis Blanchard }
1799d9fbd03dSHollis Blanchard 
1800d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1801d9fbd03dSHollis Blanchard                                   struct kvm_translation *tr)
1802d9fbd03dSHollis Blanchard {
180398001d8dSAvi Kivity 	int r;
180498001d8dSAvi Kivity 
18051da5b61dSChristoffer Dall 	vcpu_load(vcpu);
180698001d8dSAvi Kivity 	r = kvmppc_core_vcpu_translate(vcpu, tr);
18071da5b61dSChristoffer Dall 	vcpu_put(vcpu);
180898001d8dSAvi Kivity 	return r;
1809d9fbd03dSHollis Blanchard }
1810d9fbd03dSHollis Blanchard 
18110dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
18120dff0846SSean Christopherson {
18130dff0846SSean Christopherson 
18140dff0846SSean Christopherson }
18150dff0846SSean Christopherson 
18164e755758SAlexander Graf int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
18174e755758SAlexander Graf {
18184e1b2ab7SGreg Kurz 	return -EOPNOTSUPP;
18194e755758SAlexander Graf }
18204e755758SAlexander Graf 
1821e96c81eeSSean Christopherson void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
1822a66b48c3SPaul Mackerras {
1823a66b48c3SPaul Mackerras }
1824a66b48c3SPaul Mackerras 
1825f9e0554dSPaul Mackerras int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1826537a17b3SSean Christopherson 				      const struct kvm_memory_slot *old,
1827537a17b3SSean Christopherson 				      struct kvm_memory_slot *new,
182882307e67SSean Christopherson 				      enum kvm_mr_change change)
1829f9e0554dSPaul Mackerras {
1830f9e0554dSPaul Mackerras 	return 0;
1831f9e0554dSPaul Mackerras }
1832f9e0554dSPaul Mackerras 
1833f9e0554dSPaul Mackerras void kvmppc_core_commit_memory_region(struct kvm *kvm,
1834537a17b3SSean Christopherson 				struct kvm_memory_slot *old,
1835f032b734SBharata B Rao 				const struct kvm_memory_slot *new,
1836f032b734SBharata B Rao 				enum kvm_mr_change change)
1837dfe49dbdSPaul Mackerras {
1838dfe49dbdSPaul Mackerras }
1839dfe49dbdSPaul Mackerras 
1840dfe49dbdSPaul Mackerras void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
1841f9e0554dSPaul Mackerras {
1842f9e0554dSPaul Mackerras }
1843f9e0554dSPaul Mackerras 
184438f98824SMihai Caraman void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
184538f98824SMihai Caraman {
184638f98824SMihai Caraman #if defined(CONFIG_64BIT)
184738f98824SMihai Caraman 	vcpu->arch.epcr = new_epcr;
184838f98824SMihai Caraman #ifdef CONFIG_KVM_BOOKE_HV
184938f98824SMihai Caraman 	vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
185038f98824SMihai Caraman 	if (vcpu->arch.epcr  & SPRN_EPCR_ICM)
185138f98824SMihai Caraman 		vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
185238f98824SMihai Caraman #endif
185338f98824SMihai Caraman #endif
185438f98824SMihai Caraman }
185538f98824SMihai Caraman 
1856dfd4d47eSScott Wood void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1857dfd4d47eSScott Wood {
1858dfd4d47eSScott Wood 	vcpu->arch.tcr = new_tcr;
1859f61c94bbSBharat Bhushan 	arm_next_watchdog(vcpu);
1860dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1861dfd4d47eSScott Wood }
1862dfd4d47eSScott Wood 
1863dfd4d47eSScott Wood void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1864dfd4d47eSScott Wood {
1865dfd4d47eSScott Wood 	set_bits(tsr_bits, &vcpu->arch.tsr);
1866dfd4d47eSScott Wood 	smp_wmb();
1867dfd4d47eSScott Wood 	kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1868dfd4d47eSScott Wood 	kvm_vcpu_kick(vcpu);
1869dfd4d47eSScott Wood }
1870dfd4d47eSScott Wood 
1871dfd4d47eSScott Wood void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1872dfd4d47eSScott Wood {
1873dfd4d47eSScott Wood 	clear_bits(tsr_bits, &vcpu->arch.tsr);
1874f61c94bbSBharat Bhushan 
1875f61c94bbSBharat Bhushan 	/*
1876f61c94bbSBharat Bhushan 	 * We may have stopped the watchdog due to
1877f61c94bbSBharat Bhushan 	 * being stuck on final expiration.
1878f61c94bbSBharat Bhushan 	 */
1879f61c94bbSBharat Bhushan 	if (tsr_bits & (TSR_ENW | TSR_WIS))
1880f61c94bbSBharat Bhushan 		arm_next_watchdog(vcpu);
1881f61c94bbSBharat Bhushan 
1882dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1883dfd4d47eSScott Wood }
1884dfd4d47eSScott Wood 
1885d02d4d15SMihai Caraman void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
1886dfd4d47eSScott Wood {
188721bd000aSBharat Bhushan 	if (vcpu->arch.tcr & TCR_ARE) {
188821bd000aSBharat Bhushan 		vcpu->arch.dec = vcpu->arch.decar;
188921bd000aSBharat Bhushan 		kvmppc_emulate_dec(vcpu);
189021bd000aSBharat Bhushan 	}
189121bd000aSBharat Bhushan 
1892dfd4d47eSScott Wood 	kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1893dfd4d47eSScott Wood }
1894dfd4d47eSScott Wood 
1895ce11e48bSBharat Bhushan static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1896ce11e48bSBharat Bhushan 				       uint64_t addr, int index)
1897ce11e48bSBharat Bhushan {
1898ce11e48bSBharat Bhushan 	switch (index) {
1899ce11e48bSBharat Bhushan 	case 0:
1900ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC1;
1901ce11e48bSBharat Bhushan 		dbg_reg->iac1 = addr;
1902ce11e48bSBharat Bhushan 		break;
1903ce11e48bSBharat Bhushan 	case 1:
1904ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC2;
1905ce11e48bSBharat Bhushan 		dbg_reg->iac2 = addr;
1906ce11e48bSBharat Bhushan 		break;
1907ce11e48bSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1908ce11e48bSBharat Bhushan 	case 2:
1909ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC3;
1910ce11e48bSBharat Bhushan 		dbg_reg->iac3 = addr;
1911ce11e48bSBharat Bhushan 		break;
1912ce11e48bSBharat Bhushan 	case 3:
1913ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC4;
1914ce11e48bSBharat Bhushan 		dbg_reg->iac4 = addr;
1915ce11e48bSBharat Bhushan 		break;
1916ce11e48bSBharat Bhushan #endif
1917ce11e48bSBharat Bhushan 	default:
1918ce11e48bSBharat Bhushan 		return -EINVAL;
1919ce11e48bSBharat Bhushan 	}
1920ce11e48bSBharat Bhushan 
1921ce11e48bSBharat Bhushan 	dbg_reg->dbcr0 |= DBCR0_IDM;
1922ce11e48bSBharat Bhushan 	return 0;
1923ce11e48bSBharat Bhushan }
1924ce11e48bSBharat Bhushan 
1925ce11e48bSBharat Bhushan static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1926ce11e48bSBharat Bhushan 				       int type, int index)
1927ce11e48bSBharat Bhushan {
1928ce11e48bSBharat Bhushan 	switch (index) {
1929ce11e48bSBharat Bhushan 	case 0:
1930ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_READ)
1931ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC1R;
1932ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_WRITE)
1933ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC1W;
1934ce11e48bSBharat Bhushan 		dbg_reg->dac1 = addr;
1935ce11e48bSBharat Bhushan 		break;
1936ce11e48bSBharat Bhushan 	case 1:
1937ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_READ)
1938ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC2R;
1939ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_WRITE)
1940ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC2W;
1941ce11e48bSBharat Bhushan 		dbg_reg->dac2 = addr;
1942ce11e48bSBharat Bhushan 		break;
1943ce11e48bSBharat Bhushan 	default:
1944ce11e48bSBharat Bhushan 		return -EINVAL;
1945ce11e48bSBharat Bhushan 	}
1946ce11e48bSBharat Bhushan 
1947ce11e48bSBharat Bhushan 	dbg_reg->dbcr0 |= DBCR0_IDM;
1948ce11e48bSBharat Bhushan 	return 0;
1949ce11e48bSBharat Bhushan }
1950ce11e48bSBharat Bhushan void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1951ce11e48bSBharat Bhushan {
1952ce11e48bSBharat Bhushan 	/* XXX: Add similar MSR protection for BookE-PR */
1953ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
1954ce11e48bSBharat Bhushan 	BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1955ce11e48bSBharat Bhushan 	if (set) {
1956ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_UCLE)
1957ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1958ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_DE)
1959ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_DEP;
1960ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_PMM)
1961ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_PMMP;
1962ce11e48bSBharat Bhushan 	} else {
1963ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_UCLE)
1964ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1965ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_DE)
1966ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1967ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_PMM)
1968ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1969ce11e48bSBharat Bhushan 	}
1970ce11e48bSBharat Bhushan #endif
1971ce11e48bSBharat Bhushan }
1972ce11e48bSBharat Bhushan 
19737d15c06fSAlexander Graf int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
19747d15c06fSAlexander Graf 		 enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
19757d15c06fSAlexander Graf {
19767d15c06fSAlexander Graf 	int gtlb_index;
19777d15c06fSAlexander Graf 	gpa_t gpaddr;
19787d15c06fSAlexander Graf 
19797d15c06fSAlexander Graf #ifdef CONFIG_KVM_E500V2
19807d15c06fSAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_PR) &&
19817d15c06fSAlexander Graf 	    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
19827d15c06fSAlexander Graf 		pte->eaddr = eaddr;
19837d15c06fSAlexander Graf 		pte->raddr = (vcpu->arch.magic_page_pa & PAGE_MASK) |
19847d15c06fSAlexander Graf 			     (eaddr & ~PAGE_MASK);
19857d15c06fSAlexander Graf 		pte->vpage = eaddr >> PAGE_SHIFT;
19867d15c06fSAlexander Graf 		pte->may_read = true;
19877d15c06fSAlexander Graf 		pte->may_write = true;
19887d15c06fSAlexander Graf 		pte->may_execute = true;
19897d15c06fSAlexander Graf 
19907d15c06fSAlexander Graf 		return 0;
19917d15c06fSAlexander Graf 	}
19927d15c06fSAlexander Graf #endif
19937d15c06fSAlexander Graf 
19947d15c06fSAlexander Graf 	/* Check the guest TLB. */
19957d15c06fSAlexander Graf 	switch (xlid) {
19967d15c06fSAlexander Graf 	case XLATE_INST:
19977d15c06fSAlexander Graf 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
19987d15c06fSAlexander Graf 		break;
19997d15c06fSAlexander Graf 	case XLATE_DATA:
20007d15c06fSAlexander Graf 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
20017d15c06fSAlexander Graf 		break;
20027d15c06fSAlexander Graf 	default:
20037d15c06fSAlexander Graf 		BUG();
20047d15c06fSAlexander Graf 	}
20057d15c06fSAlexander Graf 
20067d15c06fSAlexander Graf 	/* Do we have a TLB entry at all? */
20077d15c06fSAlexander Graf 	if (gtlb_index < 0)
20087d15c06fSAlexander Graf 		return -ENOENT;
20097d15c06fSAlexander Graf 
20107d15c06fSAlexander Graf 	gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
20117d15c06fSAlexander Graf 
20127d15c06fSAlexander Graf 	pte->eaddr = eaddr;
20137d15c06fSAlexander Graf 	pte->raddr = (gpaddr & PAGE_MASK) | (eaddr & ~PAGE_MASK);
20147d15c06fSAlexander Graf 	pte->vpage = eaddr >> PAGE_SHIFT;
20157d15c06fSAlexander Graf 
20167d15c06fSAlexander Graf 	/* XXX read permissions from the guest TLB */
20177d15c06fSAlexander Graf 	pte->may_read = true;
20187d15c06fSAlexander Graf 	pte->may_write = true;
20197d15c06fSAlexander Graf 	pte->may_execute = true;
20207d15c06fSAlexander Graf 
20217d15c06fSAlexander Graf 	return 0;
20227d15c06fSAlexander Graf }
20237d15c06fSAlexander Graf 
2024ce11e48bSBharat Bhushan int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2025ce11e48bSBharat Bhushan 					 struct kvm_guest_debug *dbg)
2026ce11e48bSBharat Bhushan {
2027ce11e48bSBharat Bhushan 	struct debug_reg *dbg_reg;
2028ce11e48bSBharat Bhushan 	int n, b = 0, w = 0;
202966b56562SChristoffer Dall 	int ret = 0;
203066b56562SChristoffer Dall 
203166b56562SChristoffer Dall 	vcpu_load(vcpu);
2032ce11e48bSBharat Bhushan 
2033ce11e48bSBharat Bhushan 	if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
2034348ba710SBharat Bhushan 		vcpu->arch.dbg_reg.dbcr0 = 0;
2035ce11e48bSBharat Bhushan 		vcpu->guest_debug = 0;
2036ce11e48bSBharat Bhushan 		kvm_guest_protect_msr(vcpu, MSR_DE, false);
203766b56562SChristoffer Dall 		goto out;
2038ce11e48bSBharat Bhushan 	}
2039ce11e48bSBharat Bhushan 
2040ce11e48bSBharat Bhushan 	kvm_guest_protect_msr(vcpu, MSR_DE, true);
2041ce11e48bSBharat Bhushan 	vcpu->guest_debug = dbg->control;
2042348ba710SBharat Bhushan 	vcpu->arch.dbg_reg.dbcr0 = 0;
2043ce11e48bSBharat Bhushan 
2044ce11e48bSBharat Bhushan 	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
2045348ba710SBharat Bhushan 		vcpu->arch.dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
2046ce11e48bSBharat Bhushan 
2047ce11e48bSBharat Bhushan 	/* Code below handles only HW breakpoints */
2048348ba710SBharat Bhushan 	dbg_reg = &(vcpu->arch.dbg_reg);
2049ce11e48bSBharat Bhushan 
2050ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
2051ce11e48bSBharat Bhushan 	/*
2052ce11e48bSBharat Bhushan 	 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
2053ce11e48bSBharat Bhushan 	 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
2054ce11e48bSBharat Bhushan 	 */
2055ce11e48bSBharat Bhushan 	dbg_reg->dbcr1 = 0;
2056ce11e48bSBharat Bhushan 	dbg_reg->dbcr2 = 0;
2057ce11e48bSBharat Bhushan #else
2058ce11e48bSBharat Bhushan 	/*
2059ce11e48bSBharat Bhushan 	 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
2060ce11e48bSBharat Bhushan 	 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
2061ce11e48bSBharat Bhushan 	 * is set.
2062ce11e48bSBharat Bhushan 	 */
2063ce11e48bSBharat Bhushan 	dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
2064ce11e48bSBharat Bhushan 			  DBCR1_IAC4US;
2065ce11e48bSBharat Bhushan 	dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
2066ce11e48bSBharat Bhushan #endif
2067ce11e48bSBharat Bhushan 
2068ce11e48bSBharat Bhushan 	if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
206966b56562SChristoffer Dall 		goto out;
2070ce11e48bSBharat Bhushan 
207166b56562SChristoffer Dall 	ret = -EINVAL;
2072ce11e48bSBharat Bhushan 	for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
2073ce11e48bSBharat Bhushan 		uint64_t addr = dbg->arch.bp[n].addr;
2074ce11e48bSBharat Bhushan 		uint32_t type = dbg->arch.bp[n].type;
2075ce11e48bSBharat Bhushan 
2076ce11e48bSBharat Bhushan 		if (type == KVMPPC_DEBUG_NONE)
2077ce11e48bSBharat Bhushan 			continue;
2078ce11e48bSBharat Bhushan 
2079ac0e89bbSDan Carpenter 		if (type & ~(KVMPPC_DEBUG_WATCH_READ |
2080ce11e48bSBharat Bhushan 			     KVMPPC_DEBUG_WATCH_WRITE |
2081ce11e48bSBharat Bhushan 			     KVMPPC_DEBUG_BREAKPOINT))
208266b56562SChristoffer Dall 			goto out;
2083ce11e48bSBharat Bhushan 
2084ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_BREAKPOINT) {
2085ce11e48bSBharat Bhushan 			/* Setting H/W breakpoint */
2086ce11e48bSBharat Bhushan 			if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
208766b56562SChristoffer Dall 				goto out;
2088ce11e48bSBharat Bhushan 		} else {
2089ce11e48bSBharat Bhushan 			/* Setting H/W watchpoint */
2090ce11e48bSBharat Bhushan 			if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
2091ce11e48bSBharat Bhushan 							type, w++))
209266b56562SChristoffer Dall 				goto out;
2093ce11e48bSBharat Bhushan 		}
2094ce11e48bSBharat Bhushan 	}
2095ce11e48bSBharat Bhushan 
209666b56562SChristoffer Dall 	ret = 0;
209766b56562SChristoffer Dall out:
209866b56562SChristoffer Dall 	vcpu_put(vcpu);
209966b56562SChristoffer Dall 	return ret;
2100ce11e48bSBharat Bhushan }
2101ce11e48bSBharat Bhushan 
210294fa9d99SScott Wood void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
210394fa9d99SScott Wood {
2104a47d72f3SPaul Mackerras 	vcpu->cpu = smp_processor_id();
2105d30f6e48SScott Wood 	current->thread.kvm_vcpu = vcpu;
210694fa9d99SScott Wood }
210794fa9d99SScott Wood 
210894fa9d99SScott Wood void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
210994fa9d99SScott Wood {
2110d30f6e48SScott Wood 	current->thread.kvm_vcpu = NULL;
2111a47d72f3SPaul Mackerras 	vcpu->cpu = -1;
2112ce11e48bSBharat Bhushan 
2113ce11e48bSBharat Bhushan 	/* Clear pending debug event in DBSR */
2114ce11e48bSBharat Bhushan 	kvmppc_clear_dbsr();
211594fa9d99SScott Wood }
211694fa9d99SScott Wood 
21173a167beaSAneesh Kumar K.V int kvmppc_core_init_vm(struct kvm *kvm)
21183a167beaSAneesh Kumar K.V {
2119cbbc58d4SAneesh Kumar K.V 	return kvm->arch.kvm_ops->init_vm(kvm);
21203a167beaSAneesh Kumar K.V }
21213a167beaSAneesh Kumar K.V 
2122ff030fdfSSean Christopherson int kvmppc_core_vcpu_create(struct kvm_vcpu *vcpu)
21233a167beaSAneesh Kumar K.V {
2124b3d42c98SSean Christopherson 	int i;
2125b3d42c98SSean Christopherson 	int r;
2126b3d42c98SSean Christopherson 
2127b3d42c98SSean Christopherson 	r = vcpu->kvm->arch.kvm_ops->vcpu_create(vcpu);
2128b3d42c98SSean Christopherson 	if (r)
2129b3d42c98SSean Christopherson 		return r;
2130b3d42c98SSean Christopherson 
2131b3d42c98SSean Christopherson 	/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
2132b3d42c98SSean Christopherson 	vcpu->arch.regs.nip = 0;
2133b3d42c98SSean Christopherson 	vcpu->arch.shared->pir = vcpu->vcpu_id;
2134b3d42c98SSean Christopherson 	kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
2135b3d42c98SSean Christopherson 	kvmppc_set_msr(vcpu, 0);
2136b3d42c98SSean Christopherson 
2137b3d42c98SSean Christopherson #ifndef CONFIG_KVM_BOOKE_HV
2138b3d42c98SSean Christopherson 	vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
2139b3d42c98SSean Christopherson 	vcpu->arch.shadow_pid = 1;
2140b3d42c98SSean Christopherson 	vcpu->arch.shared->msr = 0;
2141b3d42c98SSean Christopherson #endif
2142b3d42c98SSean Christopherson 
2143b3d42c98SSean Christopherson 	/* Eye-catching numbers so we know if the guest takes an interrupt
2144b3d42c98SSean Christopherson 	 * before it's programmed its own IVPR/IVORs. */
2145b3d42c98SSean Christopherson 	vcpu->arch.ivpr = 0x55550000;
2146b3d42c98SSean Christopherson 	for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
2147b3d42c98SSean Christopherson 		vcpu->arch.ivor[i] = 0x7700 | i * 4;
2148b3d42c98SSean Christopherson 
2149b3d42c98SSean Christopherson 	kvmppc_init_timing_stats(vcpu);
2150b3d42c98SSean Christopherson 
2151b3d42c98SSean Christopherson 	r = kvmppc_core_vcpu_setup(vcpu);
2152b3d42c98SSean Christopherson 	if (r)
2153b3d42c98SSean Christopherson 		vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
2154b3d42c98SSean Christopherson 	kvmppc_sanity_check(vcpu);
2155b3d42c98SSean Christopherson 	return r;
21563a167beaSAneesh Kumar K.V }
21573a167beaSAneesh Kumar K.V 
21583a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
21593a167beaSAneesh Kumar K.V {
2160cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
21613a167beaSAneesh Kumar K.V }
21623a167beaSAneesh Kumar K.V 
21633a167beaSAneesh Kumar K.V void kvmppc_core_destroy_vm(struct kvm *kvm)
21643a167beaSAneesh Kumar K.V {
2165cbbc58d4SAneesh Kumar K.V 	kvm->arch.kvm_ops->destroy_vm(kvm);
21663a167beaSAneesh Kumar K.V }
21673a167beaSAneesh Kumar K.V 
21683a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
21693a167beaSAneesh Kumar K.V {
2170cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
21713a167beaSAneesh Kumar K.V }
21723a167beaSAneesh Kumar K.V 
21733a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
21743a167beaSAneesh Kumar K.V {
2175cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
2176d9fbd03dSHollis Blanchard }
2177d9fbd03dSHollis Blanchard 
2178d9fbd03dSHollis Blanchard int __init kvmppc_booke_init(void)
2179d9fbd03dSHollis Blanchard {
2180d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
2181d9fbd03dSHollis Blanchard 	unsigned long ivor[16];
21821d542d9cSBharat Bhushan 	unsigned long *handler = kvmppc_booke_handler_addr;
2183d9fbd03dSHollis Blanchard 	unsigned long max_ivor = 0;
21841d542d9cSBharat Bhushan 	unsigned long handler_len;
2185d9fbd03dSHollis Blanchard 	int i;
2186d9fbd03dSHollis Blanchard 
2187d9fbd03dSHollis Blanchard 	/* We install our own exception handlers by hijacking IVPR. IVPR must
2188d9fbd03dSHollis Blanchard 	 * be 16-bit aligned, so we need a 64KB allocation. */
2189d9fbd03dSHollis Blanchard 	kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
2190d9fbd03dSHollis Blanchard 	                                         VCPU_SIZE_ORDER);
2191d9fbd03dSHollis Blanchard 	if (!kvmppc_booke_handlers)
2192d9fbd03dSHollis Blanchard 		return -ENOMEM;
2193d9fbd03dSHollis Blanchard 
2194d9fbd03dSHollis Blanchard 	/* XXX make sure our handlers are smaller than Linux's */
2195d9fbd03dSHollis Blanchard 
2196d9fbd03dSHollis Blanchard 	/* Copy our interrupt handlers to match host IVORs. That way we don't
2197d9fbd03dSHollis Blanchard 	 * have to swap the IVORs on every guest/host transition. */
2198d9fbd03dSHollis Blanchard 	ivor[0] = mfspr(SPRN_IVOR0);
2199d9fbd03dSHollis Blanchard 	ivor[1] = mfspr(SPRN_IVOR1);
2200d9fbd03dSHollis Blanchard 	ivor[2] = mfspr(SPRN_IVOR2);
2201d9fbd03dSHollis Blanchard 	ivor[3] = mfspr(SPRN_IVOR3);
2202d9fbd03dSHollis Blanchard 	ivor[4] = mfspr(SPRN_IVOR4);
2203d9fbd03dSHollis Blanchard 	ivor[5] = mfspr(SPRN_IVOR5);
2204d9fbd03dSHollis Blanchard 	ivor[6] = mfspr(SPRN_IVOR6);
2205d9fbd03dSHollis Blanchard 	ivor[7] = mfspr(SPRN_IVOR7);
2206d9fbd03dSHollis Blanchard 	ivor[8] = mfspr(SPRN_IVOR8);
2207d9fbd03dSHollis Blanchard 	ivor[9] = mfspr(SPRN_IVOR9);
2208d9fbd03dSHollis Blanchard 	ivor[10] = mfspr(SPRN_IVOR10);
2209d9fbd03dSHollis Blanchard 	ivor[11] = mfspr(SPRN_IVOR11);
2210d9fbd03dSHollis Blanchard 	ivor[12] = mfspr(SPRN_IVOR12);
2211d9fbd03dSHollis Blanchard 	ivor[13] = mfspr(SPRN_IVOR13);
2212d9fbd03dSHollis Blanchard 	ivor[14] = mfspr(SPRN_IVOR14);
2213d9fbd03dSHollis Blanchard 	ivor[15] = mfspr(SPRN_IVOR15);
2214d9fbd03dSHollis Blanchard 
2215d9fbd03dSHollis Blanchard 	for (i = 0; i < 16; i++) {
2216d9fbd03dSHollis Blanchard 		if (ivor[i] > max_ivor)
22171d542d9cSBharat Bhushan 			max_ivor = i;
2218d9fbd03dSHollis Blanchard 
22191d542d9cSBharat Bhushan 		handler_len = handler[i + 1] - handler[i];
2220d9fbd03dSHollis Blanchard 		memcpy((void *)kvmppc_booke_handlers + ivor[i],
22211d542d9cSBharat Bhushan 		       (void *)handler[i], handler_len);
2222d9fbd03dSHollis Blanchard 	}
22231d542d9cSBharat Bhushan 
22241d542d9cSBharat Bhushan 	handler_len = handler[max_ivor + 1] - handler[max_ivor];
22251d542d9cSBharat Bhushan 	flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
22261d542d9cSBharat Bhushan 			   ivor[max_ivor] + handler_len);
2227d30f6e48SScott Wood #endif /* !BOOKE_HV */
2228db93f574SHollis Blanchard 	return 0;
2229d9fbd03dSHollis Blanchard }
2230d9fbd03dSHollis Blanchard 
2231db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void)
2232d9fbd03dSHollis Blanchard {
2233d9fbd03dSHollis Blanchard 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
2234d9fbd03dSHollis Blanchard 	kvm_exit();
2235d9fbd03dSHollis Blanchard }
2236