1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 * GNU General Public License for more details.
10 *
11 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12 *
13 * Derived from book3s_rmhandlers.S and other files, which are:
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
23#include <asm/mmu.h>
24#include <asm/page.h>
25#include <asm/ptrace.h>
26#include <asm/hvcall.h>
27#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
29#include <asm/kvm_book3s_asm.h>
30#include <asm/book3s/64/mmu-hash.h>
31#include <asm/tm.h>
32#include <asm/opal.h>
33#include <asm/xive-regs.h>
34
35/* Sign-extend HDEC if not on POWER9 */
36#define EXTEND_HDEC(reg)			\
37BEGIN_FTR_SECTION;				\
38	extsw	reg, reg;			\
39END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
40
41#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
42
43/* Values in HSTATE_NAPPING(r13) */
44#define NAPPING_CEDE	1
45#define NAPPING_NOVCPU	2
46
47/* Stack frame offsets for kvmppc_hv_entry */
48#define SFS			160
49#define STACK_SLOT_TRAP		(SFS-4)
50#define STACK_SLOT_TID		(SFS-16)
51#define STACK_SLOT_PSSCR	(SFS-24)
52#define STACK_SLOT_PID		(SFS-32)
53#define STACK_SLOT_IAMR		(SFS-40)
54#define STACK_SLOT_CIABR	(SFS-48)
55#define STACK_SLOT_DAWR		(SFS-56)
56#define STACK_SLOT_DAWRX	(SFS-64)
57#define STACK_SLOT_HFSCR	(SFS-72)
58
59/*
60 * Call kvmppc_hv_entry in real mode.
61 * Must be called with interrupts hard-disabled.
62 *
63 * Input Registers:
64 *
65 * LR = return address to continue at after eventually re-enabling MMU
66 */
67_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
68	mflr	r0
69	std	r0, PPC_LR_STKOFF(r1)
70	stdu	r1, -112(r1)
71	mfmsr	r10
72	std	r10, HSTATE_HOST_MSR(r13)
73	LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
74	li	r0,MSR_RI
75	andc	r0,r10,r0
76	li	r6,MSR_IR | MSR_DR
77	andc	r6,r10,r6
78	mtmsrd	r0,1		/* clear RI in MSR */
79	mtsrr0	r5
80	mtsrr1	r6
81	RFI
82
83kvmppc_call_hv_entry:
84	ld	r4, HSTATE_KVM_VCPU(r13)
85	bl	kvmppc_hv_entry
86
87	/* Back from guest - restore host state and return to caller */
88
89BEGIN_FTR_SECTION
90	/* Restore host DABR and DABRX */
91	ld	r5,HSTATE_DABR(r13)
92	li	r6,7
93	mtspr	SPRN_DABR,r5
94	mtspr	SPRN_DABRX,r6
95END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
96
97	/* Restore SPRG3 */
98	ld	r3,PACA_SPRG_VDSO(r13)
99	mtspr	SPRN_SPRG_VDSO_WRITE,r3
100
101	/* Reload the host's PMU registers */
102	ld	r3, PACALPPACAPTR(r13)	/* is the host using the PMU? */
103	lbz	r4, LPPACA_PMCINUSE(r3)
104	cmpwi	r4, 0
105	beq	23f			/* skip if not */
106BEGIN_FTR_SECTION
107	ld	r3, HSTATE_MMCR0(r13)
108	andi.	r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
109	cmpwi	r4, MMCR0_PMAO
110	beql	kvmppc_fix_pmao
111END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
112	lwz	r3, HSTATE_PMC1(r13)
113	lwz	r4, HSTATE_PMC2(r13)
114	lwz	r5, HSTATE_PMC3(r13)
115	lwz	r6, HSTATE_PMC4(r13)
116	lwz	r8, HSTATE_PMC5(r13)
117	lwz	r9, HSTATE_PMC6(r13)
118	mtspr	SPRN_PMC1, r3
119	mtspr	SPRN_PMC2, r4
120	mtspr	SPRN_PMC3, r5
121	mtspr	SPRN_PMC4, r6
122	mtspr	SPRN_PMC5, r8
123	mtspr	SPRN_PMC6, r9
124	ld	r3, HSTATE_MMCR0(r13)
125	ld	r4, HSTATE_MMCR1(r13)
126	ld	r5, HSTATE_MMCRA(r13)
127	ld	r6, HSTATE_SIAR(r13)
128	ld	r7, HSTATE_SDAR(r13)
129	mtspr	SPRN_MMCR1, r4
130	mtspr	SPRN_MMCRA, r5
131	mtspr	SPRN_SIAR, r6
132	mtspr	SPRN_SDAR, r7
133BEGIN_FTR_SECTION
134	ld	r8, HSTATE_MMCR2(r13)
135	ld	r9, HSTATE_SIER(r13)
136	mtspr	SPRN_MMCR2, r8
137	mtspr	SPRN_SIER, r9
138END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
139	mtspr	SPRN_MMCR0, r3
140	isync
14123:
142
143	/*
144	 * Reload DEC.  HDEC interrupts were disabled when
145	 * we reloaded the host's LPCR value.
146	 */
147	ld	r3, HSTATE_DECEXP(r13)
148	mftb	r4
149	subf	r4, r4, r3
150	mtspr	SPRN_DEC, r4
151
152BEGIN_FTR_SECTION
153	/* hwthread_req may have got set by cede or no vcpu, so clear it */
154	li	r0, 0
155	stb	r0, HSTATE_HWTHREAD_REQ(r13)
156END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
157
158	/*
159	 * For external interrupts we need to call the Linux
160	 * handler to process the interrupt. We do that by jumping
161	 * to absolute address 0x500 for external interrupts.
162	 * The [h]rfid at the end of the handler will return to
163	 * the book3s_hv_interrupts.S code. For other interrupts
164	 * we do the rfid to get back to the book3s_hv_interrupts.S
165	 * code here.
166	 */
167	ld	r8, 112+PPC_LR_STKOFF(r1)
168	addi	r1, r1, 112
169	ld	r7, HSTATE_HOST_MSR(r13)
170
171	/* Return the trap number on this thread as the return value */
172	mr	r3, r12
173
174	/*
175	 * If we came back from the guest via a relocation-on interrupt,
176	 * we will be in virtual mode at this point, which makes it a
177	 * little easier to get back to the caller.
178	 */
179	mfmsr	r0
180	andi.	r0, r0, MSR_IR		/* in real mode? */
181	bne	.Lvirt_return
182
183	/* RFI into the highmem handler */
184	mfmsr	r6
185	li	r0, MSR_RI
186	andc	r6, r6, r0
187	mtmsrd	r6, 1			/* Clear RI in MSR */
188	mtsrr0	r8
189	mtsrr1	r7
190	RFI
191
192	/* Virtual-mode return */
193.Lvirt_return:
194	mtlr	r8
195	blr
196
197kvmppc_primary_no_guest:
198	/* We handle this much like a ceded vcpu */
199	/* put the HDEC into the DEC, since HDEC interrupts don't wake us */
200	/* HDEC may be larger than DEC for arch >= v3.00, but since the */
201	/* HDEC value came from DEC in the first place, it will fit */
202	mfspr	r3, SPRN_HDEC
203	mtspr	SPRN_DEC, r3
204	/*
205	 * Make sure the primary has finished the MMU switch.
206	 * We should never get here on a secondary thread, but
207	 * check it for robustness' sake.
208	 */
209	ld	r5, HSTATE_KVM_VCORE(r13)
21065:	lbz	r0, VCORE_IN_GUEST(r5)
211	cmpwi	r0, 0
212	beq	65b
213	/* Set LPCR. */
214	ld	r8,VCORE_LPCR(r5)
215	mtspr	SPRN_LPCR,r8
216	isync
217	/* set our bit in napping_threads */
218	ld	r5, HSTATE_KVM_VCORE(r13)
219	lbz	r7, HSTATE_PTID(r13)
220	li	r0, 1
221	sld	r0, r0, r7
222	addi	r6, r5, VCORE_NAPPING_THREADS
2231:	lwarx	r3, 0, r6
224	or	r3, r3, r0
225	stwcx.	r3, 0, r6
226	bne	1b
227	/* order napping_threads update vs testing entry_exit_map */
228	isync
229	li	r12, 0
230	lwz	r7, VCORE_ENTRY_EXIT(r5)
231	cmpwi	r7, 0x100
232	bge	kvm_novcpu_exit	/* another thread already exiting */
233	li	r3, NAPPING_NOVCPU
234	stb	r3, HSTATE_NAPPING(r13)
235
236	li	r3, 0		/* Don't wake on privileged (OS) doorbell */
237	b	kvm_do_nap
238
239/*
240 * kvm_novcpu_wakeup
241 *	Entered from kvm_start_guest if kvm_hstate.napping is set
242 *	to NAPPING_NOVCPU
243 *		r2 = kernel TOC
244 *		r13 = paca
245 */
246kvm_novcpu_wakeup:
247	ld	r1, HSTATE_HOST_R1(r13)
248	ld	r5, HSTATE_KVM_VCORE(r13)
249	li	r0, 0
250	stb	r0, HSTATE_NAPPING(r13)
251
252	/* check the wake reason */
253	bl	kvmppc_check_wake_reason
254
255	/*
256	 * Restore volatile registers since we could have called
257	 * a C routine in kvmppc_check_wake_reason.
258	 *	r5 = VCORE
259	 */
260	ld	r5, HSTATE_KVM_VCORE(r13)
261
262	/* see if any other thread is already exiting */
263	lwz	r0, VCORE_ENTRY_EXIT(r5)
264	cmpwi	r0, 0x100
265	bge	kvm_novcpu_exit
266
267	/* clear our bit in napping_threads */
268	lbz	r7, HSTATE_PTID(r13)
269	li	r0, 1
270	sld	r0, r0, r7
271	addi	r6, r5, VCORE_NAPPING_THREADS
2724:	lwarx	r7, 0, r6
273	andc	r7, r7, r0
274	stwcx.	r7, 0, r6
275	bne	4b
276
277	/* See if the wake reason means we need to exit */
278	cmpdi	r3, 0
279	bge	kvm_novcpu_exit
280
281	/* See if our timeslice has expired (HDEC is negative) */
282	mfspr	r0, SPRN_HDEC
283	EXTEND_HDEC(r0)
284	li	r12, BOOK3S_INTERRUPT_HV_DECREMENTER
285	cmpdi	r0, 0
286	blt	kvm_novcpu_exit
287
288	/* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
289	ld	r4, HSTATE_KVM_VCPU(r13)
290	cmpdi	r4, 0
291	beq	kvmppc_primary_no_guest
292
293#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
294	addi	r3, r4, VCPU_TB_RMENTRY
295	bl	kvmhv_start_timing
296#endif
297	b	kvmppc_got_guest
298
299kvm_novcpu_exit:
300#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
301	ld	r4, HSTATE_KVM_VCPU(r13)
302	cmpdi	r4, 0
303	beq	13f
304	addi	r3, r4, VCPU_TB_RMEXIT
305	bl	kvmhv_accumulate_time
306#endif
30713:	mr	r3, r12
308	stw	r12, STACK_SLOT_TRAP(r1)
309	bl	kvmhv_commence_exit
310	nop
311	lwz	r12, STACK_SLOT_TRAP(r1)
312	b	kvmhv_switch_to_host
313
314/*
315 * We come in here when wakened from nap mode.
316 * Relocation is off and most register values are lost.
317 * r13 points to the PACA.
318 * r3 contains the SRR1 wakeup value, SRR1 is trashed.
319 * This is not used by ISAv3.0B processors.
320 */
321	.globl	kvm_start_guest
322kvm_start_guest:
323	/* Set runlatch bit the minute you wake up from nap */
324	mfspr	r0, SPRN_CTRLF
325	ori 	r0, r0, 1
326	mtspr	SPRN_CTRLT, r0
327
328	/*
329	 * Could avoid this and pass it through in r3. For now,
330	 * code expects it to be in SRR1.
331	 */
332	mtspr	SPRN_SRR1,r3
333
334	ld	r2,PACATOC(r13)
335
336	li	r0,KVM_HWTHREAD_IN_KVM
337	stb	r0,HSTATE_HWTHREAD_STATE(r13)
338
339	/* NV GPR values from power7_idle() will no longer be valid */
340	li	r0,1
341	stb	r0,PACA_NAPSTATELOST(r13)
342
343	/* were we napping due to cede? */
344	lbz	r0,HSTATE_NAPPING(r13)
345	cmpwi	r0,NAPPING_CEDE
346	beq	kvm_end_cede
347	cmpwi	r0,NAPPING_NOVCPU
348	beq	kvm_novcpu_wakeup
349
350	ld	r1,PACAEMERGSP(r13)
351	subi	r1,r1,STACK_FRAME_OVERHEAD
352
353	/*
354	 * We weren't napping due to cede, so this must be a secondary
355	 * thread being woken up to run a guest, or being woken up due
356	 * to a stray IPI.  (Or due to some machine check or hypervisor
357	 * maintenance interrupt while the core is in KVM.)
358	 */
359
360	/* Check the wake reason in SRR1 to see why we got here */
361	bl	kvmppc_check_wake_reason
362	/*
363	 * kvmppc_check_wake_reason could invoke a C routine, but we
364	 * have no volatile registers to restore when we return.
365	 */
366
367	cmpdi	r3, 0
368	bge	kvm_no_guest
369
370	/* get vcore pointer, NULL if we have nothing to run */
371	ld	r5,HSTATE_KVM_VCORE(r13)
372	cmpdi	r5,0
373	/* if we have no vcore to run, go back to sleep */
374	beq	kvm_no_guest
375
376kvm_secondary_got_guest:
377
378	/* Set HSTATE_DSCR(r13) to something sensible */
379	ld	r6, PACA_DSCR_DEFAULT(r13)
380	std	r6, HSTATE_DSCR(r13)
381
382	/* On thread 0 of a subcore, set HDEC to max */
383	lbz	r4, HSTATE_PTID(r13)
384	cmpwi	r4, 0
385	bne	63f
386	LOAD_REG_ADDR(r6, decrementer_max)
387	ld	r6, 0(r6)
388	mtspr	SPRN_HDEC, r6
389	/* and set per-LPAR registers, if doing dynamic micro-threading */
390	ld	r6, HSTATE_SPLIT_MODE(r13)
391	cmpdi	r6, 0
392	beq	63f
393	ld	r0, KVM_SPLIT_RPR(r6)
394	mtspr	SPRN_RPR, r0
395	ld	r0, KVM_SPLIT_PMMAR(r6)
396	mtspr	SPRN_PMMAR, r0
397	ld	r0, KVM_SPLIT_LDBAR(r6)
398	mtspr	SPRN_LDBAR, r0
399	isync
40063:
401	/* Order load of vcpu after load of vcore */
402	lwsync
403	ld	r4, HSTATE_KVM_VCPU(r13)
404	bl	kvmppc_hv_entry
405
406	/* Back from the guest, go back to nap */
407	/* Clear our vcpu and vcore pointers so we don't come back in early */
408	li	r0, 0
409	std	r0, HSTATE_KVM_VCPU(r13)
410	/*
411	 * Once we clear HSTATE_KVM_VCORE(r13), the code in
412	 * kvmppc_run_core() is going to assume that all our vcpu
413	 * state is visible in memory.  This lwsync makes sure
414	 * that that is true.
415	 */
416	lwsync
417	std	r0, HSTATE_KVM_VCORE(r13)
418
419	/*
420	 * All secondaries exiting guest will fall through this path.
421	 * Before proceeding, just check for HMI interrupt and
422	 * invoke opal hmi handler. By now we are sure that the
423	 * primary thread on this core/subcore has already made partition
424	 * switch/TB resync and we are good to call opal hmi handler.
425	 */
426	cmpwi	r12, BOOK3S_INTERRUPT_HMI
427	bne	kvm_no_guest
428
429	li	r3,0			/* NULL argument */
430	bl	hmi_exception_realmode
431/*
432 * At this point we have finished executing in the guest.
433 * We need to wait for hwthread_req to become zero, since
434 * we may not turn on the MMU while hwthread_req is non-zero.
435 * While waiting we also need to check if we get given a vcpu to run.
436 */
437kvm_no_guest:
438BEGIN_FTR_SECTION
439	twi	31,0,0
440END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
441	lbz	r3, HSTATE_HWTHREAD_REQ(r13)
442	cmpwi	r3, 0
443	bne	53f
444	HMT_MEDIUM
445	li	r0, KVM_HWTHREAD_IN_KERNEL
446	stb	r0, HSTATE_HWTHREAD_STATE(r13)
447	/* need to recheck hwthread_req after a barrier, to avoid race */
448	sync
449	lbz	r3, HSTATE_HWTHREAD_REQ(r13)
450	cmpwi	r3, 0
451	bne	54f
452/*
453 * We jump to pnv_wakeup_loss, which will return to the caller
454 * of power7_nap in the powernv cpu offline loop.  The value we
455 * put in r3 becomes the return value for power7_nap. pnv_wakeup_loss
456 * requires SRR1 in r12.
457 */
458	li	r3, LPCR_PECE0
459	mfspr	r4, SPRN_LPCR
460	rlwimi	r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
461	mtspr	SPRN_LPCR, r4
462	li	r3, 0
463	mfspr	r12,SPRN_SRR1
464	b	pnv_wakeup_loss
465
46653:	HMT_LOW
467	ld	r5, HSTATE_KVM_VCORE(r13)
468	cmpdi	r5, 0
469	bne	60f
470	ld	r3, HSTATE_SPLIT_MODE(r13)
471	cmpdi	r3, 0
472	beq	kvm_no_guest
473	lbz	r0, KVM_SPLIT_DO_NAP(r3)
474	cmpwi	r0, 0
475	beq	kvm_no_guest
476	HMT_MEDIUM
477	b	kvm_unsplit_nap
47860:	HMT_MEDIUM
479	b	kvm_secondary_got_guest
480
48154:	li	r0, KVM_HWTHREAD_IN_KVM
482	stb	r0, HSTATE_HWTHREAD_STATE(r13)
483	b	kvm_no_guest
484
485/*
486 * Here the primary thread is trying to return the core to
487 * whole-core mode, so we need to nap.
488 */
489kvm_unsplit_nap:
490	/*
491	 * When secondaries are napping in kvm_unsplit_nap() with
492	 * hwthread_req = 1, HMI goes ignored even though subcores are
493	 * already exited the guest. Hence HMI keeps waking up secondaries
494	 * from nap in a loop and secondaries always go back to nap since
495	 * no vcore is assigned to them. This makes impossible for primary
496	 * thread to get hold of secondary threads resulting into a soft
497	 * lockup in KVM path.
498	 *
499	 * Let us check if HMI is pending and handle it before we go to nap.
500	 */
501	cmpwi	r12, BOOK3S_INTERRUPT_HMI
502	bne	55f
503	li	r3, 0			/* NULL argument */
504	bl	hmi_exception_realmode
50555:
506	/*
507	 * Ensure that secondary doesn't nap when it has
508	 * its vcore pointer set.
509	 */
510	sync		/* matches smp_mb() before setting split_info.do_nap */
511	ld	r0, HSTATE_KVM_VCORE(r13)
512	cmpdi	r0, 0
513	bne	kvm_no_guest
514	/* clear any pending message */
515BEGIN_FTR_SECTION
516	lis	r6, (PPC_DBELL_SERVER << (63-36))@h
517	PPC_MSGCLR(6)
518END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
519	/* Set kvm_split_mode.napped[tid] = 1 */
520	ld	r3, HSTATE_SPLIT_MODE(r13)
521	li	r0, 1
522	lhz	r4, PACAPACAINDEX(r13)
523	clrldi	r4, r4, 61	/* micro-threading => P8 => 8 threads/core */
524	addi	r4, r4, KVM_SPLIT_NAPPED
525	stbx	r0, r3, r4
526	/* Check the do_nap flag again after setting napped[] */
527	sync
528	lbz	r0, KVM_SPLIT_DO_NAP(r3)
529	cmpwi	r0, 0
530	beq	57f
531	li	r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
532	mfspr	r5, SPRN_LPCR
533	rlwimi	r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
534	b	kvm_nap_sequence
535
53657:	li	r0, 0
537	stbx	r0, r3, r4
538	b	kvm_no_guest
539
540/******************************************************************************
541 *                                                                            *
542 *                               Entry code                                   *
543 *                                                                            *
544 *****************************************************************************/
545
546.global kvmppc_hv_entry
547kvmppc_hv_entry:
548
549	/* Required state:
550	 *
551	 * R4 = vcpu pointer (or NULL)
552	 * MSR = ~IR|DR
553	 * R13 = PACA
554	 * R1 = host R1
555	 * R2 = TOC
556	 * all other volatile GPRS = free
557	 * Does not preserve non-volatile GPRs or CR fields
558	 */
559	mflr	r0
560	std	r0, PPC_LR_STKOFF(r1)
561	stdu	r1, -SFS(r1)
562
563	/* Save R1 in the PACA */
564	std	r1, HSTATE_HOST_R1(r13)
565
566	li	r6, KVM_GUEST_MODE_HOST_HV
567	stb	r6, HSTATE_IN_GUEST(r13)
568
569#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
570	/* Store initial timestamp */
571	cmpdi	r4, 0
572	beq	1f
573	addi	r3, r4, VCPU_TB_RMENTRY
574	bl	kvmhv_start_timing
5751:
576#endif
577
578	/* Use cr7 as an indication of radix mode */
579	ld	r5, HSTATE_KVM_VCORE(r13)
580	ld	r9, VCORE_KVM(r5)	/* pointer to struct kvm */
581	lbz	r0, KVM_RADIX(r9)
582	cmpwi	cr7, r0, 0
583
584	/* Clear out SLB if hash */
585	bne	cr7, 2f
586	li	r6,0
587	slbmte	r6,r6
588	slbia
589	ptesync
5902:
591	/*
592	 * POWER7/POWER8 host -> guest partition switch code.
593	 * We don't have to lock against concurrent tlbies,
594	 * but we do have to coordinate across hardware threads.
595	 */
596	/* Set bit in entry map iff exit map is zero. */
597	li	r7, 1
598	lbz	r6, HSTATE_PTID(r13)
599	sld	r7, r7, r6
600	addi	r8, r5, VCORE_ENTRY_EXIT
60121:	lwarx	r3, 0, r8
602	cmpwi	r3, 0x100		/* any threads starting to exit? */
603	bge	secondary_too_late	/* if so we're too late to the party */
604	or	r3, r3, r7
605	stwcx.	r3, 0, r8
606	bne	21b
607
608	/* Primary thread switches to guest partition. */
609	cmpwi	r6,0
610	bne	10f
611	lwz	r7,KVM_LPID(r9)
612BEGIN_FTR_SECTION
613	ld	r6,KVM_SDR1(r9)
614	li	r0,LPID_RSVD		/* switch to reserved LPID */
615	mtspr	SPRN_LPID,r0
616	ptesync
617	mtspr	SPRN_SDR1,r6		/* switch to partition page table */
618END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
619	mtspr	SPRN_LPID,r7
620	isync
621
622	/* See if we need to flush the TLB */
623	lhz	r6,PACAPACAINDEX(r13)	/* test_bit(cpu, need_tlb_flush) */
624BEGIN_FTR_SECTION
625	/*
626	 * On POWER9, individual threads can come in here, but the
627	 * TLB is shared between the 4 threads in a core, hence
628	 * invalidating on one thread invalidates for all.
629	 * Thus we make all 4 threads use the same bit here.
630	 */
631	clrrdi	r6,r6,2
632END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
633	clrldi	r7,r6,64-6		/* extract bit number (6 bits) */
634	srdi	r6,r6,6			/* doubleword number */
635	sldi	r6,r6,3			/* address offset */
636	add	r6,r6,r9
637	addi	r6,r6,KVM_NEED_FLUSH	/* dword in kvm->arch.need_tlb_flush */
638	li	r8,1
639	sld	r8,r8,r7
640	ld	r7,0(r6)
641	and.	r7,r7,r8
642	beq	22f
643	/* Flush the TLB of any entries for this LPID */
644	lwz	r0,KVM_TLB_SETS(r9)
645	mtctr	r0
646	li	r7,0x800		/* IS field = 0b10 */
647	ptesync
648	li	r0,0			/* RS for P9 version of tlbiel */
649	bne	cr7, 29f
65028:	tlbiel	r7			/* On P9, rs=0, RIC=0, PRS=0, R=0 */
651	addi	r7,r7,0x1000
652	bdnz	28b
653	b	30f
65429:	PPC_TLBIEL(7,0,2,1,1)		/* for radix, RIC=2, PRS=1, R=1 */
655	addi	r7,r7,0x1000
656	bdnz	29b
65730:	ptesync
65823:	ldarx	r7,0,r6			/* clear the bit after TLB flushed */
659	andc	r7,r7,r8
660	stdcx.	r7,0,r6
661	bne	23b
662
663	/* Add timebase offset onto timebase */
66422:	ld	r8,VCORE_TB_OFFSET(r5)
665	cmpdi	r8,0
666	beq	37f
667	mftb	r6		/* current host timebase */
668	add	r8,r8,r6
669	mtspr	SPRN_TBU40,r8	/* update upper 40 bits */
670	mftb	r7		/* check if lower 24 bits overflowed */
671	clrldi	r6,r6,40
672	clrldi	r7,r7,40
673	cmpld	r7,r6
674	bge	37f
675	addis	r8,r8,0x100	/* if so, increment upper 40 bits */
676	mtspr	SPRN_TBU40,r8
677
678	/* Load guest PCR value to select appropriate compat mode */
67937:	ld	r7, VCORE_PCR(r5)
680	cmpdi	r7, 0
681	beq	38f
682	mtspr	SPRN_PCR, r7
68338:
684
685BEGIN_FTR_SECTION
686	/* DPDES and VTB are shared between threads */
687	ld	r8, VCORE_DPDES(r5)
688	ld	r7, VCORE_VTB(r5)
689	mtspr	SPRN_DPDES, r8
690	mtspr	SPRN_VTB, r7
691END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
692
693	/* Mark the subcore state as inside guest */
694	bl	kvmppc_subcore_enter_guest
695	nop
696	ld	r5, HSTATE_KVM_VCORE(r13)
697	ld	r4, HSTATE_KVM_VCPU(r13)
698	li	r0,1
699	stb	r0,VCORE_IN_GUEST(r5)	/* signal secondaries to continue */
700
701	/* Do we have a guest vcpu to run? */
70210:	cmpdi	r4, 0
703	beq	kvmppc_primary_no_guest
704kvmppc_got_guest:
705
706	/* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
707	lwz	r5,VCPU_SLB_MAX(r4)
708	cmpwi	r5,0
709	beq	9f
710	mtctr	r5
711	addi	r6,r4,VCPU_SLB
7121:	ld	r8,VCPU_SLB_E(r6)
713	ld	r9,VCPU_SLB_V(r6)
714	slbmte	r9,r8
715	addi	r6,r6,VCPU_SLB_SIZE
716	bdnz	1b
7179:
718	/* Increment yield count if they have a VPA */
719	ld	r3, VCPU_VPA(r4)
720	cmpdi	r3, 0
721	beq	25f
722	li	r6, LPPACA_YIELDCOUNT
723	LWZX_BE	r5, r3, r6
724	addi	r5, r5, 1
725	STWX_BE	r5, r3, r6
726	li	r6, 1
727	stb	r6, VCPU_VPA_DIRTY(r4)
72825:
729
730	/* Save purr/spurr */
731	mfspr	r5,SPRN_PURR
732	mfspr	r6,SPRN_SPURR
733	std	r5,HSTATE_PURR(r13)
734	std	r6,HSTATE_SPURR(r13)
735	ld	r7,VCPU_PURR(r4)
736	ld	r8,VCPU_SPURR(r4)
737	mtspr	SPRN_PURR,r7
738	mtspr	SPRN_SPURR,r8
739
740	/* Save host values of some registers */
741BEGIN_FTR_SECTION
742	mfspr	r5, SPRN_TIDR
743	mfspr	r6, SPRN_PSSCR
744	mfspr	r7, SPRN_PID
745	mfspr	r8, SPRN_IAMR
746	std	r5, STACK_SLOT_TID(r1)
747	std	r6, STACK_SLOT_PSSCR(r1)
748	std	r7, STACK_SLOT_PID(r1)
749	std	r8, STACK_SLOT_IAMR(r1)
750	mfspr	r5, SPRN_HFSCR
751	std	r5, STACK_SLOT_HFSCR(r1)
752END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
753BEGIN_FTR_SECTION
754	mfspr	r5, SPRN_CIABR
755	mfspr	r6, SPRN_DAWR
756	mfspr	r7, SPRN_DAWRX
757	std	r5, STACK_SLOT_CIABR(r1)
758	std	r6, STACK_SLOT_DAWR(r1)
759	std	r7, STACK_SLOT_DAWRX(r1)
760END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
761
762BEGIN_FTR_SECTION
763	/* Set partition DABR */
764	/* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
765	lwz	r5,VCPU_DABRX(r4)
766	ld	r6,VCPU_DABR(r4)
767	mtspr	SPRN_DABRX,r5
768	mtspr	SPRN_DABR,r6
769	isync
770END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
771
772#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
773BEGIN_FTR_SECTION
774	bl	kvmppc_restore_tm
775END_FTR_SECTION_IFSET(CPU_FTR_TM)
776#endif
777
778	/* Load guest PMU registers */
779	/* R4 is live here (vcpu pointer) */
780	li	r3, 1
781	sldi	r3, r3, 31		/* MMCR0_FC (freeze counters) bit */
782	mtspr	SPRN_MMCR0, r3		/* freeze all counters, disable ints */
783	isync
784BEGIN_FTR_SECTION
785	ld	r3, VCPU_MMCR(r4)
786	andi.	r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
787	cmpwi	r5, MMCR0_PMAO
788	beql	kvmppc_fix_pmao
789END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
790	lwz	r3, VCPU_PMC(r4)	/* always load up guest PMU registers */
791	lwz	r5, VCPU_PMC + 4(r4)	/* to prevent information leak */
792	lwz	r6, VCPU_PMC + 8(r4)
793	lwz	r7, VCPU_PMC + 12(r4)
794	lwz	r8, VCPU_PMC + 16(r4)
795	lwz	r9, VCPU_PMC + 20(r4)
796	mtspr	SPRN_PMC1, r3
797	mtspr	SPRN_PMC2, r5
798	mtspr	SPRN_PMC3, r6
799	mtspr	SPRN_PMC4, r7
800	mtspr	SPRN_PMC5, r8
801	mtspr	SPRN_PMC6, r9
802	ld	r3, VCPU_MMCR(r4)
803	ld	r5, VCPU_MMCR + 8(r4)
804	ld	r6, VCPU_MMCR + 16(r4)
805	ld	r7, VCPU_SIAR(r4)
806	ld	r8, VCPU_SDAR(r4)
807	mtspr	SPRN_MMCR1, r5
808	mtspr	SPRN_MMCRA, r6
809	mtspr	SPRN_SIAR, r7
810	mtspr	SPRN_SDAR, r8
811BEGIN_FTR_SECTION
812	ld	r5, VCPU_MMCR + 24(r4)
813	ld	r6, VCPU_SIER(r4)
814	mtspr	SPRN_MMCR2, r5
815	mtspr	SPRN_SIER, r6
816BEGIN_FTR_SECTION_NESTED(96)
817	lwz	r7, VCPU_PMC + 24(r4)
818	lwz	r8, VCPU_PMC + 28(r4)
819	ld	r9, VCPU_MMCR + 32(r4)
820	mtspr	SPRN_SPMC1, r7
821	mtspr	SPRN_SPMC2, r8
822	mtspr	SPRN_MMCRS, r9
823END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
824END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
825	mtspr	SPRN_MMCR0, r3
826	isync
827
828	/* Load up FP, VMX and VSX registers */
829	bl	kvmppc_load_fp
830
831	ld	r14, VCPU_GPR(R14)(r4)
832	ld	r15, VCPU_GPR(R15)(r4)
833	ld	r16, VCPU_GPR(R16)(r4)
834	ld	r17, VCPU_GPR(R17)(r4)
835	ld	r18, VCPU_GPR(R18)(r4)
836	ld	r19, VCPU_GPR(R19)(r4)
837	ld	r20, VCPU_GPR(R20)(r4)
838	ld	r21, VCPU_GPR(R21)(r4)
839	ld	r22, VCPU_GPR(R22)(r4)
840	ld	r23, VCPU_GPR(R23)(r4)
841	ld	r24, VCPU_GPR(R24)(r4)
842	ld	r25, VCPU_GPR(R25)(r4)
843	ld	r26, VCPU_GPR(R26)(r4)
844	ld	r27, VCPU_GPR(R27)(r4)
845	ld	r28, VCPU_GPR(R28)(r4)
846	ld	r29, VCPU_GPR(R29)(r4)
847	ld	r30, VCPU_GPR(R30)(r4)
848	ld	r31, VCPU_GPR(R31)(r4)
849
850	/* Switch DSCR to guest value */
851	ld	r5, VCPU_DSCR(r4)
852	mtspr	SPRN_DSCR, r5
853
854BEGIN_FTR_SECTION
855	/* Skip next section on POWER7 */
856	b	8f
857END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
858	/* Load up POWER8-specific registers */
859	ld	r5, VCPU_IAMR(r4)
860	lwz	r6, VCPU_PSPB(r4)
861	ld	r7, VCPU_FSCR(r4)
862	mtspr	SPRN_IAMR, r5
863	mtspr	SPRN_PSPB, r6
864	mtspr	SPRN_FSCR, r7
865	ld	r5, VCPU_DAWR(r4)
866	ld	r6, VCPU_DAWRX(r4)
867	ld	r7, VCPU_CIABR(r4)
868	ld	r8, VCPU_TAR(r4)
869	mtspr	SPRN_DAWR, r5
870	mtspr	SPRN_DAWRX, r6
871	mtspr	SPRN_CIABR, r7
872	mtspr	SPRN_TAR, r8
873	ld	r5, VCPU_IC(r4)
874	ld	r8, VCPU_EBBHR(r4)
875	mtspr	SPRN_IC, r5
876	mtspr	SPRN_EBBHR, r8
877	ld	r5, VCPU_EBBRR(r4)
878	ld	r6, VCPU_BESCR(r4)
879	lwz	r7, VCPU_GUEST_PID(r4)
880	ld	r8, VCPU_WORT(r4)
881	mtspr	SPRN_EBBRR, r5
882	mtspr	SPRN_BESCR, r6
883	mtspr	SPRN_PID, r7
884	mtspr	SPRN_WORT, r8
885BEGIN_FTR_SECTION
886	PPC_INVALIDATE_ERAT
887END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
888BEGIN_FTR_SECTION
889	/* POWER8-only registers */
890	ld	r5, VCPU_TCSCR(r4)
891	ld	r6, VCPU_ACOP(r4)
892	ld	r7, VCPU_CSIGR(r4)
893	ld	r8, VCPU_TACR(r4)
894	mtspr	SPRN_TCSCR, r5
895	mtspr	SPRN_ACOP, r6
896	mtspr	SPRN_CSIGR, r7
897	mtspr	SPRN_TACR, r8
898FTR_SECTION_ELSE
899	/* POWER9-only registers */
900	ld	r5, VCPU_TID(r4)
901	ld	r6, VCPU_PSSCR(r4)
902	oris	r6, r6, PSSCR_EC@h	/* This makes stop trap to HV */
903	ld	r7, VCPU_HFSCR(r4)
904	mtspr	SPRN_TIDR, r5
905	mtspr	SPRN_PSSCR, r6
906	mtspr	SPRN_HFSCR, r7
907ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
9088:
909
910	/*
911	 * Set the decrementer to the guest decrementer.
912	 */
913	ld	r8,VCPU_DEC_EXPIRES(r4)
914	/* r8 is a host timebase value here, convert to guest TB */
915	ld	r5,HSTATE_KVM_VCORE(r13)
916	ld	r6,VCORE_TB_OFFSET(r5)
917	add	r8,r8,r6
918	mftb	r7
919	subf	r3,r7,r8
920	mtspr	SPRN_DEC,r3
921	std	r3,VCPU_DEC(r4)
922
923	ld	r5, VCPU_SPRG0(r4)
924	ld	r6, VCPU_SPRG1(r4)
925	ld	r7, VCPU_SPRG2(r4)
926	ld	r8, VCPU_SPRG3(r4)
927	mtspr	SPRN_SPRG0, r5
928	mtspr	SPRN_SPRG1, r6
929	mtspr	SPRN_SPRG2, r7
930	mtspr	SPRN_SPRG3, r8
931
932	/* Load up DAR and DSISR */
933	ld	r5, VCPU_DAR(r4)
934	lwz	r6, VCPU_DSISR(r4)
935	mtspr	SPRN_DAR, r5
936	mtspr	SPRN_DSISR, r6
937
938	/* Restore AMR and UAMOR, set AMOR to all 1s */
939	ld	r5,VCPU_AMR(r4)
940	ld	r6,VCPU_UAMOR(r4)
941	li	r7,-1
942	mtspr	SPRN_AMR,r5
943	mtspr	SPRN_UAMOR,r6
944	mtspr	SPRN_AMOR,r7
945
946	/* Restore state of CTRL run bit; assume 1 on entry */
947	lwz	r5,VCPU_CTRL(r4)
948	andi.	r5,r5,1
949	bne	4f
950	mfspr	r6,SPRN_CTRLF
951	clrrdi	r6,r6,1
952	mtspr	SPRN_CTRLT,r6
9534:
954	/* Secondary threads wait for primary to have done partition switch */
955	ld	r5, HSTATE_KVM_VCORE(r13)
956	lbz	r6, HSTATE_PTID(r13)
957	cmpwi	r6, 0
958	beq	21f
959	lbz	r0, VCORE_IN_GUEST(r5)
960	cmpwi	r0, 0
961	bne	21f
962	HMT_LOW
96320:	lwz	r3, VCORE_ENTRY_EXIT(r5)
964	cmpwi	r3, 0x100
965	bge	no_switch_exit
966	lbz	r0, VCORE_IN_GUEST(r5)
967	cmpwi	r0, 0
968	beq	20b
969	HMT_MEDIUM
97021:
971	/* Set LPCR. */
972	ld	r8,VCORE_LPCR(r5)
973	mtspr	SPRN_LPCR,r8
974	isync
975
976	/* Check if HDEC expires soon */
977	mfspr	r3, SPRN_HDEC
978	EXTEND_HDEC(r3)
979	cmpdi	r3, 512		/* 1 microsecond */
980	blt	hdec_soon
981
982#ifdef CONFIG_KVM_XICS
983	/* We are entering the guest on that thread, push VCPU to XIVE */
984	ld	r10, HSTATE_XIVE_TIMA_PHYS(r13)
985	cmpldi	cr0, r10, 0
986	beq	no_xive
987	ld	r11, VCPU_XIVE_SAVED_STATE(r4)
988	li	r9, TM_QW1_OS
989	stdcix	r11,r9,r10
990	eieio
991	lwz	r11, VCPU_XIVE_CAM_WORD(r4)
992	li	r9, TM_QW1_OS + TM_WORD2
993	stwcix	r11,r9,r10
994	li	r9, 1
995	stw	r9, VCPU_XIVE_PUSHED(r4)
996no_xive:
997#endif /* CONFIG_KVM_XICS */
998
999deliver_guest_interrupt:
1000	ld	r6, VCPU_CTR(r4)
1001	ld	r7, VCPU_XER(r4)
1002
1003	mtctr	r6
1004	mtxer	r7
1005
1006kvmppc_cede_reentry:		/* r4 = vcpu, r13 = paca */
1007	ld	r10, VCPU_PC(r4)
1008	ld	r11, VCPU_MSR(r4)
1009	ld	r6, VCPU_SRR0(r4)
1010	ld	r7, VCPU_SRR1(r4)
1011	mtspr	SPRN_SRR0, r6
1012	mtspr	SPRN_SRR1, r7
1013
1014	/* r11 = vcpu->arch.msr & ~MSR_HV */
1015	rldicl	r11, r11, 63 - MSR_HV_LG, 1
1016	rotldi	r11, r11, 1 + MSR_HV_LG
1017	ori	r11, r11, MSR_ME
1018
1019	/* Check if we can deliver an external or decrementer interrupt now */
1020	ld	r0, VCPU_PENDING_EXC(r4)
1021	rldicl	r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1022	cmpdi	cr1, r0, 0
1023	andi.	r8, r11, MSR_EE
1024	mfspr	r8, SPRN_LPCR
1025	/* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1026	rldimi	r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1027	mtspr	SPRN_LPCR, r8
1028	isync
1029	beq	5f
1030	li	r0, BOOK3S_INTERRUPT_EXTERNAL
1031	bne	cr1, 12f
1032	mfspr	r0, SPRN_DEC
1033BEGIN_FTR_SECTION
1034	/* On POWER9 check whether the guest has large decrementer enabled */
1035	andis.	r8, r8, LPCR_LD@h
1036	bne	15f
1037END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1038	extsw	r0, r0
103915:	cmpdi	r0, 0
1040	li	r0, BOOK3S_INTERRUPT_DECREMENTER
1041	bge	5f
1042
104312:	mtspr	SPRN_SRR0, r10
1044	mr	r10,r0
1045	mtspr	SPRN_SRR1, r11
1046	mr	r9, r4
1047	bl	kvmppc_msr_interrupt
10485:
1049BEGIN_FTR_SECTION
1050	b	fast_guest_return
1051END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1052	/* On POWER9, check for pending doorbell requests */
1053	lbz	r0, VCPU_DBELL_REQ(r4)
1054	cmpwi	r0, 0
1055	beq	fast_guest_return
1056	ld	r5, HSTATE_KVM_VCORE(r13)
1057	/* Set DPDES register so the CPU will take a doorbell interrupt */
1058	li	r0, 1
1059	mtspr	SPRN_DPDES, r0
1060	std	r0, VCORE_DPDES(r5)
1061	/* Make sure other cpus see vcore->dpdes set before dbell req clear */
1062	lwsync
1063	/* Clear the pending doorbell request */
1064	li	r0, 0
1065	stb	r0, VCPU_DBELL_REQ(r4)
1066
1067/*
1068 * Required state:
1069 * R4 = vcpu
1070 * R10: value for HSRR0
1071 * R11: value for HSRR1
1072 * R13 = PACA
1073 */
1074fast_guest_return:
1075	li	r0,0
1076	stb	r0,VCPU_CEDED(r4)	/* cancel cede */
1077	mtspr	SPRN_HSRR0,r10
1078	mtspr	SPRN_HSRR1,r11
1079
1080	/* Activate guest mode, so faults get handled by KVM */
1081	li	r9, KVM_GUEST_MODE_GUEST_HV
1082	stb	r9, HSTATE_IN_GUEST(r13)
1083
1084#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1085	/* Accumulate timing */
1086	addi	r3, r4, VCPU_TB_GUEST
1087	bl	kvmhv_accumulate_time
1088#endif
1089
1090	/* Enter guest */
1091
1092BEGIN_FTR_SECTION
1093	ld	r5, VCPU_CFAR(r4)
1094	mtspr	SPRN_CFAR, r5
1095END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1096BEGIN_FTR_SECTION
1097	ld	r0, VCPU_PPR(r4)
1098END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1099
1100	ld	r5, VCPU_LR(r4)
1101	lwz	r6, VCPU_CR(r4)
1102	mtlr	r5
1103	mtcr	r6
1104
1105	ld	r1, VCPU_GPR(R1)(r4)
1106	ld	r2, VCPU_GPR(R2)(r4)
1107	ld	r3, VCPU_GPR(R3)(r4)
1108	ld	r5, VCPU_GPR(R5)(r4)
1109	ld	r6, VCPU_GPR(R6)(r4)
1110	ld	r7, VCPU_GPR(R7)(r4)
1111	ld	r8, VCPU_GPR(R8)(r4)
1112	ld	r9, VCPU_GPR(R9)(r4)
1113	ld	r10, VCPU_GPR(R10)(r4)
1114	ld	r11, VCPU_GPR(R11)(r4)
1115	ld	r12, VCPU_GPR(R12)(r4)
1116	ld	r13, VCPU_GPR(R13)(r4)
1117
1118BEGIN_FTR_SECTION
1119	mtspr	SPRN_PPR, r0
1120END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1121	ld	r0, VCPU_GPR(R0)(r4)
1122	ld	r4, VCPU_GPR(R4)(r4)
1123
1124	hrfid
1125	b	.
1126
1127secondary_too_late:
1128	li	r12, 0
1129	cmpdi	r4, 0
1130	beq	11f
1131	stw	r12, VCPU_TRAP(r4)
1132#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1133	addi	r3, r4, VCPU_TB_RMEXIT
1134	bl	kvmhv_accumulate_time
1135#endif
113611:	b	kvmhv_switch_to_host
1137
1138no_switch_exit:
1139	HMT_MEDIUM
1140	li	r12, 0
1141	b	12f
1142hdec_soon:
1143	li	r12, BOOK3S_INTERRUPT_HV_DECREMENTER
114412:	stw	r12, VCPU_TRAP(r4)
1145	mr	r9, r4
1146#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1147	addi	r3, r4, VCPU_TB_RMEXIT
1148	bl	kvmhv_accumulate_time
1149#endif
1150	b	guest_exit_cont
1151
1152/******************************************************************************
1153 *                                                                            *
1154 *                               Exit code                                    *
1155 *                                                                            *
1156 *****************************************************************************/
1157
1158/*
1159 * We come here from the first-level interrupt handlers.
1160 */
1161	.globl	kvmppc_interrupt_hv
1162kvmppc_interrupt_hv:
1163	/*
1164	 * Register contents:
1165	 * R12		= (guest CR << 32) | interrupt vector
1166	 * R13		= PACA
1167	 * guest R12 saved in shadow VCPU SCRATCH0
1168	 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
1169	 * guest R13 saved in SPRN_SCRATCH0
1170	 */
1171	std	r9, HSTATE_SCRATCH2(r13)
1172	lbz	r9, HSTATE_IN_GUEST(r13)
1173	cmpwi	r9, KVM_GUEST_MODE_HOST_HV
1174	beq	kvmppc_bad_host_intr
1175#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1176	cmpwi	r9, KVM_GUEST_MODE_GUEST
1177	ld	r9, HSTATE_SCRATCH2(r13)
1178	beq	kvmppc_interrupt_pr
1179#endif
1180	/* We're now back in the host but in guest MMU context */
1181	li	r9, KVM_GUEST_MODE_HOST_HV
1182	stb	r9, HSTATE_IN_GUEST(r13)
1183
1184	ld	r9, HSTATE_KVM_VCPU(r13)
1185
1186	/* Save registers */
1187
1188	std	r0, VCPU_GPR(R0)(r9)
1189	std	r1, VCPU_GPR(R1)(r9)
1190	std	r2, VCPU_GPR(R2)(r9)
1191	std	r3, VCPU_GPR(R3)(r9)
1192	std	r4, VCPU_GPR(R4)(r9)
1193	std	r5, VCPU_GPR(R5)(r9)
1194	std	r6, VCPU_GPR(R6)(r9)
1195	std	r7, VCPU_GPR(R7)(r9)
1196	std	r8, VCPU_GPR(R8)(r9)
1197	ld	r0, HSTATE_SCRATCH2(r13)
1198	std	r0, VCPU_GPR(R9)(r9)
1199	std	r10, VCPU_GPR(R10)(r9)
1200	std	r11, VCPU_GPR(R11)(r9)
1201	ld	r3, HSTATE_SCRATCH0(r13)
1202	std	r3, VCPU_GPR(R12)(r9)
1203	/* CR is in the high half of r12 */
1204	srdi	r4, r12, 32
1205	stw	r4, VCPU_CR(r9)
1206BEGIN_FTR_SECTION
1207	ld	r3, HSTATE_CFAR(r13)
1208	std	r3, VCPU_CFAR(r9)
1209END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1210BEGIN_FTR_SECTION
1211	ld	r4, HSTATE_PPR(r13)
1212	std	r4, VCPU_PPR(r9)
1213END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1214
1215	/* Restore R1/R2 so we can handle faults */
1216	ld	r1, HSTATE_HOST_R1(r13)
1217	ld	r2, PACATOC(r13)
1218
1219	mfspr	r10, SPRN_SRR0
1220	mfspr	r11, SPRN_SRR1
1221	std	r10, VCPU_SRR0(r9)
1222	std	r11, VCPU_SRR1(r9)
1223	/* trap is in the low half of r12, clear CR from the high half */
1224	clrldi	r12, r12, 32
1225	andi.	r0, r12, 2		/* need to read HSRR0/1? */
1226	beq	1f
1227	mfspr	r10, SPRN_HSRR0
1228	mfspr	r11, SPRN_HSRR1
1229	clrrdi	r12, r12, 2
12301:	std	r10, VCPU_PC(r9)
1231	std	r11, VCPU_MSR(r9)
1232
1233	GET_SCRATCH0(r3)
1234	mflr	r4
1235	std	r3, VCPU_GPR(R13)(r9)
1236	std	r4, VCPU_LR(r9)
1237
1238	stw	r12,VCPU_TRAP(r9)
1239
1240	/*
1241	 * Now that we have saved away SRR0/1 and HSRR0/1,
1242	 * interrupts are recoverable in principle, so set MSR_RI.
1243	 * This becomes important for relocation-on interrupts from
1244	 * the guest, which we can get in radix mode on POWER9.
1245	 */
1246	li	r0, MSR_RI
1247	mtmsrd	r0, 1
1248
1249#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1250	addi	r3, r9, VCPU_TB_RMINTR
1251	mr	r4, r9
1252	bl	kvmhv_accumulate_time
1253	ld	r5, VCPU_GPR(R5)(r9)
1254	ld	r6, VCPU_GPR(R6)(r9)
1255	ld	r7, VCPU_GPR(R7)(r9)
1256	ld	r8, VCPU_GPR(R8)(r9)
1257#endif
1258
1259	/* Save HEIR (HV emulation assist reg) in emul_inst
1260	   if this is an HEI (HV emulation interrupt, e40) */
1261	li	r3,KVM_INST_FETCH_FAILED
1262	stw	r3,VCPU_LAST_INST(r9)
1263	cmpwi	r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1264	bne	11f
1265	mfspr	r3,SPRN_HEIR
126611:	stw	r3,VCPU_HEIR(r9)
1267
1268	/* these are volatile across C function calls */
1269#ifdef CONFIG_RELOCATABLE
1270	ld	r3, HSTATE_SCRATCH1(r13)
1271	mtctr	r3
1272#else
1273	mfctr	r3
1274#endif
1275	mfxer	r4
1276	std	r3, VCPU_CTR(r9)
1277	std	r4, VCPU_XER(r9)
1278
1279	/* If this is a page table miss then see if it's theirs or ours */
1280	cmpwi	r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1281	beq	kvmppc_hdsi
1282	cmpwi	r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1283	beq	kvmppc_hisi
1284
1285	/* See if this is a leftover HDEC interrupt */
1286	cmpwi	r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1287	bne	2f
1288	mfspr	r3,SPRN_HDEC
1289	EXTEND_HDEC(r3)
1290	cmpdi	r3,0
1291	mr	r4,r9
1292	bge	fast_guest_return
12932:
1294	/* See if this is an hcall we can handle in real mode */
1295	cmpwi	r12,BOOK3S_INTERRUPT_SYSCALL
1296	beq	hcall_try_real_mode
1297
1298	/* Hypervisor doorbell - exit only if host IPI flag set */
1299	cmpwi	r12, BOOK3S_INTERRUPT_H_DOORBELL
1300	bne	3f
1301BEGIN_FTR_SECTION
1302	PPC_MSGSYNC
1303END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1304	lbz	r0, HSTATE_HOST_IPI(r13)
1305	cmpwi	r0, 0
1306	beq	4f
1307	b	guest_exit_cont
13083:
1309	/* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1310	cmpwi	r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1311	bne	14f
1312	mfspr	r3, SPRN_HFSCR
1313	std	r3, VCPU_HFSCR(r9)
1314	b	guest_exit_cont
131514:
1316	/* External interrupt ? */
1317	cmpwi	r12, BOOK3S_INTERRUPT_EXTERNAL
1318	bne+	guest_exit_cont
1319
1320	/* External interrupt, first check for host_ipi. If this is
1321	 * set, we know the host wants us out so let's do it now
1322	 */
1323	bl	kvmppc_read_intr
1324
1325	/*
1326	 * Restore the active volatile registers after returning from
1327	 * a C function.
1328	 */
1329	ld	r9, HSTATE_KVM_VCPU(r13)
1330	li	r12, BOOK3S_INTERRUPT_EXTERNAL
1331
1332	/*
1333	 * kvmppc_read_intr return codes:
1334	 *
1335	 * Exit to host (r3 > 0)
1336	 *   1 An interrupt is pending that needs to be handled by the host
1337	 *     Exit guest and return to host by branching to guest_exit_cont
1338	 *
1339	 *   2 Passthrough that needs completion in the host
1340	 *     Exit guest and return to host by branching to guest_exit_cont
1341	 *     However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1342	 *     to indicate to the host to complete handling the interrupt
1343	 *
1344	 * Before returning to guest, we check if any CPU is heading out
1345	 * to the host and if so, we head out also. If no CPUs are heading
1346	 * check return values <= 0.
1347	 *
1348	 * Return to guest (r3 <= 0)
1349	 *  0 No external interrupt is pending
1350	 * -1 A guest wakeup IPI (which has now been cleared)
1351	 *    In either case, we return to guest to deliver any pending
1352	 *    guest interrupts.
1353	 *
1354	 * -2 A PCI passthrough external interrupt was handled
1355	 *    (interrupt was delivered directly to guest)
1356	 *    Return to guest to deliver any pending guest interrupts.
1357	 */
1358
1359	cmpdi	r3, 1
1360	ble	1f
1361
1362	/* Return code = 2 */
1363	li	r12, BOOK3S_INTERRUPT_HV_RM_HARD
1364	stw	r12, VCPU_TRAP(r9)
1365	b	guest_exit_cont
1366
13671:	/* Return code <= 1 */
1368	cmpdi	r3, 0
1369	bgt	guest_exit_cont
1370
1371	/* Return code <= 0 */
13724:	ld	r5, HSTATE_KVM_VCORE(r13)
1373	lwz	r0, VCORE_ENTRY_EXIT(r5)
1374	cmpwi	r0, 0x100
1375	mr	r4, r9
1376	blt	deliver_guest_interrupt
1377
1378guest_exit_cont:		/* r9 = vcpu, r12 = trap, r13 = paca */
1379#ifdef CONFIG_KVM_XICS
1380	/* We are exiting, pull the VP from the XIVE */
1381	lwz	r0, VCPU_XIVE_PUSHED(r9)
1382	cmpwi	cr0, r0, 0
1383	beq	1f
1384	li	r7, TM_SPC_PULL_OS_CTX
1385	li	r6, TM_QW1_OS
1386	mfmsr	r0
1387	andi.	r0, r0, MSR_IR		/* in real mode? */
1388	beq	2f
1389	ld	r10, HSTATE_XIVE_TIMA_VIRT(r13)
1390	cmpldi	cr0, r10, 0
1391	beq	1f
1392	/* First load to pull the context, we ignore the value */
1393	lwzx	r11, r7, r10
1394	eieio
1395	/* Second load to recover the context state (Words 0 and 1) */
1396	ldx	r11, r6, r10
1397	b	3f
13982:	ld	r10, HSTATE_XIVE_TIMA_PHYS(r13)
1399	cmpldi	cr0, r10, 0
1400	beq	1f
1401	/* First load to pull the context, we ignore the value */
1402	lwzcix	r11, r7, r10
1403	eieio
1404	/* Second load to recover the context state (Words 0 and 1) */
1405	ldcix	r11, r6, r10
14063:	std	r11, VCPU_XIVE_SAVED_STATE(r9)
1407	/* Fixup some of the state for the next load */
1408	li	r10, 0
1409	li	r0, 0xff
1410	stw	r10, VCPU_XIVE_PUSHED(r9)
1411	stb	r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1412	stb	r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
14131:
1414#endif /* CONFIG_KVM_XICS */
1415	/* Save more register state  */
1416	mfdar	r6
1417	mfdsisr	r7
1418	std	r6, VCPU_DAR(r9)
1419	stw	r7, VCPU_DSISR(r9)
1420	/* don't overwrite fault_dar/fault_dsisr if HDSI */
1421	cmpwi	r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1422	beq	mc_cont
1423	std	r6, VCPU_FAULT_DAR(r9)
1424	stw	r7, VCPU_FAULT_DSISR(r9)
1425
1426	/* See if it is a machine check */
1427	cmpwi	r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1428	beq	machine_check_realmode
1429mc_cont:
1430#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1431	addi	r3, r9, VCPU_TB_RMEXIT
1432	mr	r4, r9
1433	bl	kvmhv_accumulate_time
1434#endif
1435
1436	mr 	r3, r12
1437	/* Increment exit count, poke other threads to exit */
1438	bl	kvmhv_commence_exit
1439	nop
1440	ld	r9, HSTATE_KVM_VCPU(r13)
1441	lwz	r12, VCPU_TRAP(r9)
1442
1443	/* Stop others sending VCPU interrupts to this physical CPU */
1444	li	r0, -1
1445	stw	r0, VCPU_CPU(r9)
1446	stw	r0, VCPU_THREAD_CPU(r9)
1447
1448	/* Save guest CTRL register, set runlatch to 1 */
1449	mfspr	r6,SPRN_CTRLF
1450	stw	r6,VCPU_CTRL(r9)
1451	andi.	r0,r6,1
1452	bne	4f
1453	ori	r6,r6,1
1454	mtspr	SPRN_CTRLT,r6
14554:
1456	/* Check if we are running hash or radix and store it in cr2 */
1457	ld	r5, VCPU_KVM(r9)
1458	lbz	r0, KVM_RADIX(r5)
1459	cmpwi	cr2,r0,0
1460
1461	/* Read the guest SLB and save it away */
1462	li	r5, 0
1463	bne	cr2, 3f			/* for radix, save 0 entries */
1464	lwz	r0,VCPU_SLB_NR(r9)	/* number of entries in SLB */
1465	mtctr	r0
1466	li	r6,0
1467	addi	r7,r9,VCPU_SLB
14681:	slbmfee	r8,r6
1469	andis.	r0,r8,SLB_ESID_V@h
1470	beq	2f
1471	add	r8,r8,r6		/* put index in */
1472	slbmfev	r3,r6
1473	std	r8,VCPU_SLB_E(r7)
1474	std	r3,VCPU_SLB_V(r7)
1475	addi	r7,r7,VCPU_SLB_SIZE
1476	addi	r5,r5,1
14772:	addi	r6,r6,1
1478	bdnz	1b
14793:	stw	r5,VCPU_SLB_MAX(r9)
1480
1481	/*
1482	 * Save the guest PURR/SPURR
1483	 */
1484	mfspr	r5,SPRN_PURR
1485	mfspr	r6,SPRN_SPURR
1486	ld	r7,VCPU_PURR(r9)
1487	ld	r8,VCPU_SPURR(r9)
1488	std	r5,VCPU_PURR(r9)
1489	std	r6,VCPU_SPURR(r9)
1490	subf	r5,r7,r5
1491	subf	r6,r8,r6
1492
1493	/*
1494	 * Restore host PURR/SPURR and add guest times
1495	 * so that the time in the guest gets accounted.
1496	 */
1497	ld	r3,HSTATE_PURR(r13)
1498	ld	r4,HSTATE_SPURR(r13)
1499	add	r3,r3,r5
1500	add	r4,r4,r6
1501	mtspr	SPRN_PURR,r3
1502	mtspr	SPRN_SPURR,r4
1503
1504	/* Save DEC */
1505	ld	r3, HSTATE_KVM_VCORE(r13)
1506	mfspr	r5,SPRN_DEC
1507	mftb	r6
1508	/* On P9, if the guest has large decr enabled, don't sign extend */
1509BEGIN_FTR_SECTION
1510	ld	r4, VCORE_LPCR(r3)
1511	andis.	r4, r4, LPCR_LD@h
1512	bne	16f
1513END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1514	extsw	r5,r5
151516:	add	r5,r5,r6
1516	/* r5 is a guest timebase value here, convert to host TB */
1517	ld	r4,VCORE_TB_OFFSET(r3)
1518	subf	r5,r4,r5
1519	std	r5,VCPU_DEC_EXPIRES(r9)
1520
1521BEGIN_FTR_SECTION
1522	b	8f
1523END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1524	/* Save POWER8-specific registers */
1525	mfspr	r5, SPRN_IAMR
1526	mfspr	r6, SPRN_PSPB
1527	mfspr	r7, SPRN_FSCR
1528	std	r5, VCPU_IAMR(r9)
1529	stw	r6, VCPU_PSPB(r9)
1530	std	r7, VCPU_FSCR(r9)
1531	mfspr	r5, SPRN_IC
1532	mfspr	r7, SPRN_TAR
1533	std	r5, VCPU_IC(r9)
1534	std	r7, VCPU_TAR(r9)
1535	mfspr	r8, SPRN_EBBHR
1536	std	r8, VCPU_EBBHR(r9)
1537	mfspr	r5, SPRN_EBBRR
1538	mfspr	r6, SPRN_BESCR
1539	mfspr	r7, SPRN_PID
1540	mfspr	r8, SPRN_WORT
1541	std	r5, VCPU_EBBRR(r9)
1542	std	r6, VCPU_BESCR(r9)
1543	stw	r7, VCPU_GUEST_PID(r9)
1544	std	r8, VCPU_WORT(r9)
1545BEGIN_FTR_SECTION
1546	mfspr	r5, SPRN_TCSCR
1547	mfspr	r6, SPRN_ACOP
1548	mfspr	r7, SPRN_CSIGR
1549	mfspr	r8, SPRN_TACR
1550	std	r5, VCPU_TCSCR(r9)
1551	std	r6, VCPU_ACOP(r9)
1552	std	r7, VCPU_CSIGR(r9)
1553	std	r8, VCPU_TACR(r9)
1554FTR_SECTION_ELSE
1555	mfspr	r5, SPRN_TIDR
1556	mfspr	r6, SPRN_PSSCR
1557	std	r5, VCPU_TID(r9)
1558	rldicl	r6, r6, 4, 50		/* r6 &= PSSCR_GUEST_VIS */
1559	rotldi	r6, r6, 60
1560	std	r6, VCPU_PSSCR(r9)
1561	/* Restore host HFSCR value */
1562	ld	r7, STACK_SLOT_HFSCR(r1)
1563	mtspr	SPRN_HFSCR, r7
1564ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
1565	/*
1566	 * Restore various registers to 0, where non-zero values
1567	 * set by the guest could disrupt the host.
1568	 */
1569	li	r0, 0
1570	mtspr	SPRN_PSPB, r0
1571	mtspr	SPRN_WORT, r0
1572BEGIN_FTR_SECTION
1573	mtspr	SPRN_IAMR, r0
1574	mtspr	SPRN_TCSCR, r0
1575	/* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1576	li	r0, 1
1577	sldi	r0, r0, 31
1578	mtspr	SPRN_MMCRS, r0
1579END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
15808:
1581
1582	/* Save and reset AMR and UAMOR before turning on the MMU */
1583	mfspr	r5,SPRN_AMR
1584	mfspr	r6,SPRN_UAMOR
1585	std	r5,VCPU_AMR(r9)
1586	std	r6,VCPU_UAMOR(r9)
1587	li	r6,0
1588	mtspr	SPRN_AMR,r6
1589	mtspr	SPRN_UAMOR, r6
1590
1591	/* Switch DSCR back to host value */
1592	mfspr	r8, SPRN_DSCR
1593	ld	r7, HSTATE_DSCR(r13)
1594	std	r8, VCPU_DSCR(r9)
1595	mtspr	SPRN_DSCR, r7
1596
1597	/* Save non-volatile GPRs */
1598	std	r14, VCPU_GPR(R14)(r9)
1599	std	r15, VCPU_GPR(R15)(r9)
1600	std	r16, VCPU_GPR(R16)(r9)
1601	std	r17, VCPU_GPR(R17)(r9)
1602	std	r18, VCPU_GPR(R18)(r9)
1603	std	r19, VCPU_GPR(R19)(r9)
1604	std	r20, VCPU_GPR(R20)(r9)
1605	std	r21, VCPU_GPR(R21)(r9)
1606	std	r22, VCPU_GPR(R22)(r9)
1607	std	r23, VCPU_GPR(R23)(r9)
1608	std	r24, VCPU_GPR(R24)(r9)
1609	std	r25, VCPU_GPR(R25)(r9)
1610	std	r26, VCPU_GPR(R26)(r9)
1611	std	r27, VCPU_GPR(R27)(r9)
1612	std	r28, VCPU_GPR(R28)(r9)
1613	std	r29, VCPU_GPR(R29)(r9)
1614	std	r30, VCPU_GPR(R30)(r9)
1615	std	r31, VCPU_GPR(R31)(r9)
1616
1617	/* Save SPRGs */
1618	mfspr	r3, SPRN_SPRG0
1619	mfspr	r4, SPRN_SPRG1
1620	mfspr	r5, SPRN_SPRG2
1621	mfspr	r6, SPRN_SPRG3
1622	std	r3, VCPU_SPRG0(r9)
1623	std	r4, VCPU_SPRG1(r9)
1624	std	r5, VCPU_SPRG2(r9)
1625	std	r6, VCPU_SPRG3(r9)
1626
1627	/* save FP state */
1628	mr	r3, r9
1629	bl	kvmppc_save_fp
1630
1631#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1632BEGIN_FTR_SECTION
1633	bl	kvmppc_save_tm
1634END_FTR_SECTION_IFSET(CPU_FTR_TM)
1635#endif
1636
1637	/* Increment yield count if they have a VPA */
1638	ld	r8, VCPU_VPA(r9)	/* do they have a VPA? */
1639	cmpdi	r8, 0
1640	beq	25f
1641	li	r4, LPPACA_YIELDCOUNT
1642	LWZX_BE	r3, r8, r4
1643	addi	r3, r3, 1
1644	STWX_BE	r3, r8, r4
1645	li	r3, 1
1646	stb	r3, VCPU_VPA_DIRTY(r9)
164725:
1648	/* Save PMU registers if requested */
1649	/* r8 and cr0.eq are live here */
1650BEGIN_FTR_SECTION
1651	/*
1652	 * POWER8 seems to have a hardware bug where setting
1653	 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1654	 * when some counters are already negative doesn't seem
1655	 * to cause a performance monitor alert (and hence interrupt).
1656	 * The effect of this is that when saving the PMU state,
1657	 * if there is no PMU alert pending when we read MMCR0
1658	 * before freezing the counters, but one becomes pending
1659	 * before we read the counters, we lose it.
1660	 * To work around this, we need a way to freeze the counters
1661	 * before reading MMCR0.  Normally, freezing the counters
1662	 * is done by writing MMCR0 (to set MMCR0[FC]) which
1663	 * unavoidably writes MMCR0[PMA0] as well.  On POWER8,
1664	 * we can also freeze the counters using MMCR2, by writing
1665	 * 1s to all the counter freeze condition bits (there are
1666	 * 9 bits each for 6 counters).
1667	 */
1668	li	r3, -1			/* set all freeze bits */
1669	clrrdi	r3, r3, 10
1670	mfspr	r10, SPRN_MMCR2
1671	mtspr	SPRN_MMCR2, r3
1672	isync
1673END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1674	li	r3, 1
1675	sldi	r3, r3, 31		/* MMCR0_FC (freeze counters) bit */
1676	mfspr	r4, SPRN_MMCR0		/* save MMCR0 */
1677	mtspr	SPRN_MMCR0, r3		/* freeze all counters, disable ints */
1678	mfspr	r6, SPRN_MMCRA
1679	/* Clear MMCRA in order to disable SDAR updates */
1680	li	r7, 0
1681	mtspr	SPRN_MMCRA, r7
1682	isync
1683	beq	21f			/* if no VPA, save PMU stuff anyway */
1684	lbz	r7, LPPACA_PMCINUSE(r8)
1685	cmpwi	r7, 0			/* did they ask for PMU stuff to be saved? */
1686	bne	21f
1687	std	r3, VCPU_MMCR(r9)	/* if not, set saved MMCR0 to FC */
1688	b	22f
168921:	mfspr	r5, SPRN_MMCR1
1690	mfspr	r7, SPRN_SIAR
1691	mfspr	r8, SPRN_SDAR
1692	std	r4, VCPU_MMCR(r9)
1693	std	r5, VCPU_MMCR + 8(r9)
1694	std	r6, VCPU_MMCR + 16(r9)
1695BEGIN_FTR_SECTION
1696	std	r10, VCPU_MMCR + 24(r9)
1697END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1698	std	r7, VCPU_SIAR(r9)
1699	std	r8, VCPU_SDAR(r9)
1700	mfspr	r3, SPRN_PMC1
1701	mfspr	r4, SPRN_PMC2
1702	mfspr	r5, SPRN_PMC3
1703	mfspr	r6, SPRN_PMC4
1704	mfspr	r7, SPRN_PMC5
1705	mfspr	r8, SPRN_PMC6
1706	stw	r3, VCPU_PMC(r9)
1707	stw	r4, VCPU_PMC + 4(r9)
1708	stw	r5, VCPU_PMC + 8(r9)
1709	stw	r6, VCPU_PMC + 12(r9)
1710	stw	r7, VCPU_PMC + 16(r9)
1711	stw	r8, VCPU_PMC + 20(r9)
1712BEGIN_FTR_SECTION
1713	mfspr	r5, SPRN_SIER
1714	std	r5, VCPU_SIER(r9)
1715BEGIN_FTR_SECTION_NESTED(96)
1716	mfspr	r6, SPRN_SPMC1
1717	mfspr	r7, SPRN_SPMC2
1718	mfspr	r8, SPRN_MMCRS
1719	stw	r6, VCPU_PMC + 24(r9)
1720	stw	r7, VCPU_PMC + 28(r9)
1721	std	r8, VCPU_MMCR + 32(r9)
1722	lis	r4, 0x8000
1723	mtspr	SPRN_MMCRS, r4
1724END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
1725END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
172622:
1727
1728	/* Restore host values of some registers */
1729BEGIN_FTR_SECTION
1730	ld	r5, STACK_SLOT_CIABR(r1)
1731	ld	r6, STACK_SLOT_DAWR(r1)
1732	ld	r7, STACK_SLOT_DAWRX(r1)
1733	mtspr	SPRN_CIABR, r5
1734	mtspr	SPRN_DAWR, r6
1735	mtspr	SPRN_DAWRX, r7
1736END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1737BEGIN_FTR_SECTION
1738	ld	r5, STACK_SLOT_TID(r1)
1739	ld	r6, STACK_SLOT_PSSCR(r1)
1740	ld	r7, STACK_SLOT_PID(r1)
1741	ld	r8, STACK_SLOT_IAMR(r1)
1742	mtspr	SPRN_TIDR, r5
1743	mtspr	SPRN_PSSCR, r6
1744	mtspr	SPRN_PID, r7
1745	mtspr	SPRN_IAMR, r8
1746END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1747
1748#ifdef CONFIG_PPC_RADIX_MMU
1749	/*
1750	 * Are we running hash or radix ?
1751	 */
1752	beq	cr2,3f
1753
1754	/* Radix: Handle the case where the guest used an illegal PID */
1755	LOAD_REG_ADDR(r4, mmu_base_pid)
1756	lwz	r3, VCPU_GUEST_PID(r9)
1757	lwz	r5, 0(r4)
1758	cmpw	cr0,r3,r5
1759	blt	2f
1760
1761	/*
1762	 * Illegal PID, the HW might have prefetched and cached in the TLB
1763	 * some translations for the  LPID 0 / guest PID combination which
1764	 * Linux doesn't know about, so we need to flush that PID out of
1765	 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1766	 * the right context.
1767	*/
1768	li	r0,0
1769	mtspr	SPRN_LPID,r0
1770	isync
1771
1772	/* Then do a congruence class local flush */
1773	ld	r6,VCPU_KVM(r9)
1774	lwz	r0,KVM_TLB_SETS(r6)
1775	mtctr	r0
1776	li	r7,0x400		/* IS field = 0b01 */
1777	ptesync
1778	sldi	r0,r3,32		/* RS has PID */
17791:	PPC_TLBIEL(7,0,2,1,1)		/* RIC=2, PRS=1, R=1 */
1780	addi	r7,r7,0x1000
1781	bdnz	1b
1782	ptesync
1783
17842:	/* Flush the ERAT on radix P9 DD1 guest exit */
1785BEGIN_FTR_SECTION
1786	PPC_INVALIDATE_ERAT
1787END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
1788	b	4f
1789#endif /* CONFIG_PPC_RADIX_MMU */
1790
1791	/* Hash: clear out SLB */
17923:	li	r5,0
1793	slbmte	r5,r5
1794	slbia
1795	ptesync
17964:
1797	/*
1798	 * POWER7/POWER8 guest -> host partition switch code.
1799	 * We don't have to lock against tlbies but we do
1800	 * have to coordinate the hardware threads.
1801	 */
1802kvmhv_switch_to_host:
1803	/* Secondary threads wait for primary to do partition switch */
1804	ld	r5,HSTATE_KVM_VCORE(r13)
1805	ld	r4,VCORE_KVM(r5)	/* pointer to struct kvm */
1806	lbz	r3,HSTATE_PTID(r13)
1807	cmpwi	r3,0
1808	beq	15f
1809	HMT_LOW
181013:	lbz	r3,VCORE_IN_GUEST(r5)
1811	cmpwi	r3,0
1812	bne	13b
1813	HMT_MEDIUM
1814	b	16f
1815
1816	/* Primary thread waits for all the secondaries to exit guest */
181715:	lwz	r3,VCORE_ENTRY_EXIT(r5)
1818	rlwinm	r0,r3,32-8,0xff
1819	clrldi	r3,r3,56
1820	cmpw	r3,r0
1821	bne	15b
1822	isync
1823
1824	/* Did we actually switch to the guest at all? */
1825	lbz	r6, VCORE_IN_GUEST(r5)
1826	cmpwi	r6, 0
1827	beq	19f
1828
1829	/* Primary thread switches back to host partition */
1830	lwz	r7,KVM_HOST_LPID(r4)
1831BEGIN_FTR_SECTION
1832	ld	r6,KVM_HOST_SDR1(r4)
1833	li	r8,LPID_RSVD		/* switch to reserved LPID */
1834	mtspr	SPRN_LPID,r8
1835	ptesync
1836	mtspr	SPRN_SDR1,r6		/* switch to host page table */
1837END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1838	mtspr	SPRN_LPID,r7
1839	isync
1840
1841BEGIN_FTR_SECTION
1842	/* DPDES and VTB are shared between threads */
1843	mfspr	r7, SPRN_DPDES
1844	mfspr	r8, SPRN_VTB
1845	std	r7, VCORE_DPDES(r5)
1846	std	r8, VCORE_VTB(r5)
1847	/* clear DPDES so we don't get guest doorbells in the host */
1848	li	r8, 0
1849	mtspr	SPRN_DPDES, r8
1850END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1851
1852	/* If HMI, call kvmppc_realmode_hmi_handler() */
1853	cmpwi	r12, BOOK3S_INTERRUPT_HMI
1854	bne	27f
1855	bl	kvmppc_realmode_hmi_handler
1856	nop
1857	li	r12, BOOK3S_INTERRUPT_HMI
1858	/*
1859	 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1860	 * the TB. Hence it is not required to subtract guest timebase
1861	 * offset from timebase. So, skip it.
1862	 *
1863	 * Also, do not call kvmppc_subcore_exit_guest() because it has
1864	 * been invoked as part of kvmppc_realmode_hmi_handler().
1865	 */
1866	b	30f
1867
186827:
1869	/* Subtract timebase offset from timebase */
1870	ld	r8,VCORE_TB_OFFSET(r5)
1871	cmpdi	r8,0
1872	beq	17f
1873	mftb	r6			/* current guest timebase */
1874	subf	r8,r8,r6
1875	mtspr	SPRN_TBU40,r8		/* update upper 40 bits */
1876	mftb	r7			/* check if lower 24 bits overflowed */
1877	clrldi	r6,r6,40
1878	clrldi	r7,r7,40
1879	cmpld	r7,r6
1880	bge	17f
1881	addis	r8,r8,0x100		/* if so, increment upper 40 bits */
1882	mtspr	SPRN_TBU40,r8
1883
188417:	bl	kvmppc_subcore_exit_guest
1885	nop
188630:	ld	r5,HSTATE_KVM_VCORE(r13)
1887	ld	r4,VCORE_KVM(r5)	/* pointer to struct kvm */
1888
1889	/* Reset PCR */
1890	ld	r0, VCORE_PCR(r5)
1891	cmpdi	r0, 0
1892	beq	18f
1893	li	r0, 0
1894	mtspr	SPRN_PCR, r0
189518:
1896	/* Signal secondary CPUs to continue */
1897	stb	r0,VCORE_IN_GUEST(r5)
189819:	lis	r8,0x7fff		/* MAX_INT@h */
1899	mtspr	SPRN_HDEC,r8
1900
190116:	ld	r8,KVM_HOST_LPCR(r4)
1902	mtspr	SPRN_LPCR,r8
1903	isync
1904
1905	/* load host SLB entries */
1906BEGIN_MMU_FTR_SECTION
1907	b	0f
1908END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
1909	ld	r8,PACA_SLBSHADOWPTR(r13)
1910
1911	.rept	SLB_NUM_BOLTED
1912	li	r3, SLBSHADOW_SAVEAREA
1913	LDX_BE	r5, r8, r3
1914	addi	r3, r3, 8
1915	LDX_BE	r6, r8, r3
1916	andis.	r7,r5,SLB_ESID_V@h
1917	beq	1f
1918	slbmte	r6,r5
19191:	addi	r8,r8,16
1920	.endr
19210:
1922#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1923	/* Finish timing, if we have a vcpu */
1924	ld	r4, HSTATE_KVM_VCPU(r13)
1925	cmpdi	r4, 0
1926	li	r3, 0
1927	beq	2f
1928	bl	kvmhv_accumulate_time
19292:
1930#endif
1931	/* Unset guest mode */
1932	li	r0, KVM_GUEST_MODE_NONE
1933	stb	r0, HSTATE_IN_GUEST(r13)
1934
1935	ld	r0, SFS+PPC_LR_STKOFF(r1)
1936	addi	r1, r1, SFS
1937	mtlr	r0
1938	blr
1939
1940/*
1941 * Check whether an HDSI is an HPTE not found fault or something else.
1942 * If it is an HPTE not found fault that is due to the guest accessing
1943 * a page that they have mapped but which we have paged out, then
1944 * we continue on with the guest exit path.  In all other cases,
1945 * reflect the HDSI to the guest as a DSI.
1946 */
1947kvmppc_hdsi:
1948	ld	r3, VCPU_KVM(r9)
1949	lbz	r0, KVM_RADIX(r3)
1950	cmpwi	r0, 0
1951	mfspr	r4, SPRN_HDAR
1952	mfspr	r6, SPRN_HDSISR
1953	bne	.Lradix_hdsi		/* on radix, just save DAR/DSISR/ASDR */
1954	/* HPTE not found fault or protection fault? */
1955	andis.	r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1956	beq	1f			/* if not, send it to the guest */
1957	andi.	r0, r11, MSR_DR		/* data relocation enabled? */
1958	beq	3f
1959BEGIN_FTR_SECTION
1960	mfspr	r5, SPRN_ASDR		/* on POWER9, use ASDR to get VSID */
1961	b	4f
1962END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1963	clrrdi	r0, r4, 28
1964	PPC_SLBFEE_DOT(R5, R0)		/* if so, look up SLB */
1965	li	r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1966	bne	7f			/* if no SLB entry found */
19674:	std	r4, VCPU_FAULT_DAR(r9)
1968	stw	r6, VCPU_FAULT_DSISR(r9)
1969
1970	/* Search the hash table. */
1971	mr	r3, r9			/* vcpu pointer */
1972	li	r7, 1			/* data fault */
1973	bl	kvmppc_hpte_hv_fault
1974	ld	r9, HSTATE_KVM_VCPU(r13)
1975	ld	r10, VCPU_PC(r9)
1976	ld	r11, VCPU_MSR(r9)
1977	li	r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1978	cmpdi	r3, 0			/* retry the instruction */
1979	beq	6f
1980	cmpdi	r3, -1			/* handle in kernel mode */
1981	beq	guest_exit_cont
1982	cmpdi	r3, -2			/* MMIO emulation; need instr word */
1983	beq	2f
1984
1985	/* Synthesize a DSI (or DSegI) for the guest */
1986	ld	r4, VCPU_FAULT_DAR(r9)
1987	mr	r6, r3
19881:	li	r0, BOOK3S_INTERRUPT_DATA_STORAGE
1989	mtspr	SPRN_DSISR, r6
19907:	mtspr	SPRN_DAR, r4
1991	mtspr	SPRN_SRR0, r10
1992	mtspr	SPRN_SRR1, r11
1993	mr	r10, r0
1994	bl	kvmppc_msr_interrupt
1995fast_interrupt_c_return:
19966:	ld	r7, VCPU_CTR(r9)
1997	ld	r8, VCPU_XER(r9)
1998	mtctr	r7
1999	mtxer	r8
2000	mr	r4, r9
2001	b	fast_guest_return
2002
20033:	ld	r5, VCPU_KVM(r9)	/* not relocated, use VRMA */
2004	ld	r5, KVM_VRMA_SLB_V(r5)
2005	b	4b
2006
2007	/* If this is for emulated MMIO, load the instruction word */
20082:	li	r8, KVM_INST_FETCH_FAILED	/* In case lwz faults */
2009
2010	/* Set guest mode to 'jump over instruction' so if lwz faults
2011	 * we'll just continue at the next IP. */
2012	li	r0, KVM_GUEST_MODE_SKIP
2013	stb	r0, HSTATE_IN_GUEST(r13)
2014
2015	/* Do the access with MSR:DR enabled */
2016	mfmsr	r3
2017	ori	r4, r3, MSR_DR		/* Enable paging for data */
2018	mtmsrd	r4
2019	lwz	r8, 0(r10)
2020	mtmsrd	r3
2021
2022	/* Store the result */
2023	stw	r8, VCPU_LAST_INST(r9)
2024
2025	/* Unset guest mode. */
2026	li	r0, KVM_GUEST_MODE_HOST_HV
2027	stb	r0, HSTATE_IN_GUEST(r13)
2028	b	guest_exit_cont
2029
2030.Lradix_hdsi:
2031	std	r4, VCPU_FAULT_DAR(r9)
2032	stw	r6, VCPU_FAULT_DSISR(r9)
2033.Lradix_hisi:
2034	mfspr	r5, SPRN_ASDR
2035	std	r5, VCPU_FAULT_GPA(r9)
2036	b	guest_exit_cont
2037
2038/*
2039 * Similarly for an HISI, reflect it to the guest as an ISI unless
2040 * it is an HPTE not found fault for a page that we have paged out.
2041 */
2042kvmppc_hisi:
2043	ld	r3, VCPU_KVM(r9)
2044	lbz	r0, KVM_RADIX(r3)
2045	cmpwi	r0, 0
2046	bne	.Lradix_hisi		/* for radix, just save ASDR */
2047	andis.	r0, r11, SRR1_ISI_NOPT@h
2048	beq	1f
2049	andi.	r0, r11, MSR_IR		/* instruction relocation enabled? */
2050	beq	3f
2051BEGIN_FTR_SECTION
2052	mfspr	r5, SPRN_ASDR		/* on POWER9, use ASDR to get VSID */
2053	b	4f
2054END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2055	clrrdi	r0, r10, 28
2056	PPC_SLBFEE_DOT(R5, R0)		/* if so, look up SLB */
2057	li	r0, BOOK3S_INTERRUPT_INST_SEGMENT
2058	bne	7f			/* if no SLB entry found */
20594:
2060	/* Search the hash table. */
2061	mr	r3, r9			/* vcpu pointer */
2062	mr	r4, r10
2063	mr	r6, r11
2064	li	r7, 0			/* instruction fault */
2065	bl	kvmppc_hpte_hv_fault
2066	ld	r9, HSTATE_KVM_VCPU(r13)
2067	ld	r10, VCPU_PC(r9)
2068	ld	r11, VCPU_MSR(r9)
2069	li	r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2070	cmpdi	r3, 0			/* retry the instruction */
2071	beq	fast_interrupt_c_return
2072	cmpdi	r3, -1			/* handle in kernel mode */
2073	beq	guest_exit_cont
2074
2075	/* Synthesize an ISI (or ISegI) for the guest */
2076	mr	r11, r3
20771:	li	r0, BOOK3S_INTERRUPT_INST_STORAGE
20787:	mtspr	SPRN_SRR0, r10
2079	mtspr	SPRN_SRR1, r11
2080	mr	r10, r0
2081	bl	kvmppc_msr_interrupt
2082	b	fast_interrupt_c_return
2083
20843:	ld	r6, VCPU_KVM(r9)	/* not relocated, use VRMA */
2085	ld	r5, KVM_VRMA_SLB_V(r6)
2086	b	4b
2087
2088/*
2089 * Try to handle an hcall in real mode.
2090 * Returns to the guest if we handle it, or continues on up to
2091 * the kernel if we can't (i.e. if we don't have a handler for
2092 * it, or if the handler returns H_TOO_HARD).
2093 *
2094 * r5 - r8 contain hcall args,
2095 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
2096 */
2097hcall_try_real_mode:
2098	ld	r3,VCPU_GPR(R3)(r9)
2099	andi.	r0,r11,MSR_PR
2100	/* sc 1 from userspace - reflect to guest syscall */
2101	bne	sc_1_fast_return
2102	clrrdi	r3,r3,2
2103	cmpldi	r3,hcall_real_table_end - hcall_real_table
2104	bge	guest_exit_cont
2105	/* See if this hcall is enabled for in-kernel handling */
2106	ld	r4, VCPU_KVM(r9)
2107	srdi	r0, r3, 8	/* r0 = (r3 / 4) >> 6 */
2108	sldi	r0, r0, 3	/* index into kvm->arch.enabled_hcalls[] */
2109	add	r4, r4, r0
2110	ld	r0, KVM_ENABLED_HCALLS(r4)
2111	rlwinm	r4, r3, 32-2, 0x3f	/* r4 = (r3 / 4) & 0x3f */
2112	srd	r0, r0, r4
2113	andi.	r0, r0, 1
2114	beq	guest_exit_cont
2115	/* Get pointer to handler, if any, and call it */
2116	LOAD_REG_ADDR(r4, hcall_real_table)
2117	lwax	r3,r3,r4
2118	cmpwi	r3,0
2119	beq	guest_exit_cont
2120	add	r12,r3,r4
2121	mtctr	r12
2122	mr	r3,r9		/* get vcpu pointer */
2123	ld	r4,VCPU_GPR(R4)(r9)
2124	bctrl
2125	cmpdi	r3,H_TOO_HARD
2126	beq	hcall_real_fallback
2127	ld	r4,HSTATE_KVM_VCPU(r13)
2128	std	r3,VCPU_GPR(R3)(r4)
2129	ld	r10,VCPU_PC(r4)
2130	ld	r11,VCPU_MSR(r4)
2131	b	fast_guest_return
2132
2133sc_1_fast_return:
2134	mtspr	SPRN_SRR0,r10
2135	mtspr	SPRN_SRR1,r11
2136	li	r10, BOOK3S_INTERRUPT_SYSCALL
2137	bl	kvmppc_msr_interrupt
2138	mr	r4,r9
2139	b	fast_guest_return
2140
2141	/* We've attempted a real mode hcall, but it's punted it back
2142	 * to userspace.  We need to restore some clobbered volatiles
2143	 * before resuming the pass-it-to-qemu path */
2144hcall_real_fallback:
2145	li	r12,BOOK3S_INTERRUPT_SYSCALL
2146	ld	r9, HSTATE_KVM_VCPU(r13)
2147
2148	b	guest_exit_cont
2149
2150	.globl	hcall_real_table
2151hcall_real_table:
2152	.long	0		/* 0 - unused */
2153	.long	DOTSYM(kvmppc_h_remove) - hcall_real_table
2154	.long	DOTSYM(kvmppc_h_enter) - hcall_real_table
2155	.long	DOTSYM(kvmppc_h_read) - hcall_real_table
2156	.long	DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2157	.long	DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
2158	.long	DOTSYM(kvmppc_h_protect) - hcall_real_table
2159	.long	DOTSYM(kvmppc_h_get_tce) - hcall_real_table
2160	.long	DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
2161	.long	0		/* 0x24 - H_SET_SPRG0 */
2162	.long	DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
2163	.long	0		/* 0x2c */
2164	.long	0		/* 0x30 */
2165	.long	0		/* 0x34 */
2166	.long	0		/* 0x38 */
2167	.long	0		/* 0x3c */
2168	.long	0		/* 0x40 */
2169	.long	0		/* 0x44 */
2170	.long	0		/* 0x48 */
2171	.long	0		/* 0x4c */
2172	.long	0		/* 0x50 */
2173	.long	0		/* 0x54 */
2174	.long	0		/* 0x58 */
2175	.long	0		/* 0x5c */
2176	.long	0		/* 0x60 */
2177#ifdef CONFIG_KVM_XICS
2178	.long	DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2179	.long	DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2180	.long	DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
2181	.long	DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
2182	.long	DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
2183#else
2184	.long	0		/* 0x64 - H_EOI */
2185	.long	0		/* 0x68 - H_CPPR */
2186	.long	0		/* 0x6c - H_IPI */
2187	.long	0		/* 0x70 - H_IPOLL */
2188	.long	0		/* 0x74 - H_XIRR */
2189#endif
2190	.long	0		/* 0x78 */
2191	.long	0		/* 0x7c */
2192	.long	0		/* 0x80 */
2193	.long	0		/* 0x84 */
2194	.long	0		/* 0x88 */
2195	.long	0		/* 0x8c */
2196	.long	0		/* 0x90 */
2197	.long	0		/* 0x94 */
2198	.long	0		/* 0x98 */
2199	.long	0		/* 0x9c */
2200	.long	0		/* 0xa0 */
2201	.long	0		/* 0xa4 */
2202	.long	0		/* 0xa8 */
2203	.long	0		/* 0xac */
2204	.long	0		/* 0xb0 */
2205	.long	0		/* 0xb4 */
2206	.long	0		/* 0xb8 */
2207	.long	0		/* 0xbc */
2208	.long	0		/* 0xc0 */
2209	.long	0		/* 0xc4 */
2210	.long	0		/* 0xc8 */
2211	.long	0		/* 0xcc */
2212	.long	0		/* 0xd0 */
2213	.long	0		/* 0xd4 */
2214	.long	0		/* 0xd8 */
2215	.long	0		/* 0xdc */
2216	.long	DOTSYM(kvmppc_h_cede) - hcall_real_table
2217	.long	DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
2218	.long	0		/* 0xe8 */
2219	.long	0		/* 0xec */
2220	.long	0		/* 0xf0 */
2221	.long	0		/* 0xf4 */
2222	.long	0		/* 0xf8 */
2223	.long	0		/* 0xfc */
2224	.long	0		/* 0x100 */
2225	.long	0		/* 0x104 */
2226	.long	0		/* 0x108 */
2227	.long	0		/* 0x10c */
2228	.long	0		/* 0x110 */
2229	.long	0		/* 0x114 */
2230	.long	0		/* 0x118 */
2231	.long	0		/* 0x11c */
2232	.long	0		/* 0x120 */
2233	.long	DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
2234	.long	0		/* 0x128 */
2235	.long	0		/* 0x12c */
2236	.long	0		/* 0x130 */
2237	.long	DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
2238	.long	DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
2239	.long	DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
2240	.long	0		/* 0x140 */
2241	.long	0		/* 0x144 */
2242	.long	0		/* 0x148 */
2243	.long	0		/* 0x14c */
2244	.long	0		/* 0x150 */
2245	.long	0		/* 0x154 */
2246	.long	0		/* 0x158 */
2247	.long	0		/* 0x15c */
2248	.long	0		/* 0x160 */
2249	.long	0		/* 0x164 */
2250	.long	0		/* 0x168 */
2251	.long	0		/* 0x16c */
2252	.long	0		/* 0x170 */
2253	.long	0		/* 0x174 */
2254	.long	0		/* 0x178 */
2255	.long	0		/* 0x17c */
2256	.long	0		/* 0x180 */
2257	.long	0		/* 0x184 */
2258	.long	0		/* 0x188 */
2259	.long	0		/* 0x18c */
2260	.long	0		/* 0x190 */
2261	.long	0		/* 0x194 */
2262	.long	0		/* 0x198 */
2263	.long	0		/* 0x19c */
2264	.long	0		/* 0x1a0 */
2265	.long	0		/* 0x1a4 */
2266	.long	0		/* 0x1a8 */
2267	.long	0		/* 0x1ac */
2268	.long	0		/* 0x1b0 */
2269	.long	0		/* 0x1b4 */
2270	.long	0		/* 0x1b8 */
2271	.long	0		/* 0x1bc */
2272	.long	0		/* 0x1c0 */
2273	.long	0		/* 0x1c4 */
2274	.long	0		/* 0x1c8 */
2275	.long	0		/* 0x1cc */
2276	.long	0		/* 0x1d0 */
2277	.long	0		/* 0x1d4 */
2278	.long	0		/* 0x1d8 */
2279	.long	0		/* 0x1dc */
2280	.long	0		/* 0x1e0 */
2281	.long	0		/* 0x1e4 */
2282	.long	0		/* 0x1e8 */
2283	.long	0		/* 0x1ec */
2284	.long	0		/* 0x1f0 */
2285	.long	0		/* 0x1f4 */
2286	.long	0		/* 0x1f8 */
2287	.long	0		/* 0x1fc */
2288	.long	0		/* 0x200 */
2289	.long	0		/* 0x204 */
2290	.long	0		/* 0x208 */
2291	.long	0		/* 0x20c */
2292	.long	0		/* 0x210 */
2293	.long	0		/* 0x214 */
2294	.long	0		/* 0x218 */
2295	.long	0		/* 0x21c */
2296	.long	0		/* 0x220 */
2297	.long	0		/* 0x224 */
2298	.long	0		/* 0x228 */
2299	.long	0		/* 0x22c */
2300	.long	0		/* 0x230 */
2301	.long	0		/* 0x234 */
2302	.long	0		/* 0x238 */
2303	.long	0		/* 0x23c */
2304	.long	0		/* 0x240 */
2305	.long	0		/* 0x244 */
2306	.long	0		/* 0x248 */
2307	.long	0		/* 0x24c */
2308	.long	0		/* 0x250 */
2309	.long	0		/* 0x254 */
2310	.long	0		/* 0x258 */
2311	.long	0		/* 0x25c */
2312	.long	0		/* 0x260 */
2313	.long	0		/* 0x264 */
2314	.long	0		/* 0x268 */
2315	.long	0		/* 0x26c */
2316	.long	0		/* 0x270 */
2317	.long	0		/* 0x274 */
2318	.long	0		/* 0x278 */
2319	.long	0		/* 0x27c */
2320	.long	0		/* 0x280 */
2321	.long	0		/* 0x284 */
2322	.long	0		/* 0x288 */
2323	.long	0		/* 0x28c */
2324	.long	0		/* 0x290 */
2325	.long	0		/* 0x294 */
2326	.long	0		/* 0x298 */
2327	.long	0		/* 0x29c */
2328	.long	0		/* 0x2a0 */
2329	.long	0		/* 0x2a4 */
2330	.long	0		/* 0x2a8 */
2331	.long	0		/* 0x2ac */
2332	.long	0		/* 0x2b0 */
2333	.long	0		/* 0x2b4 */
2334	.long	0		/* 0x2b8 */
2335	.long	0		/* 0x2bc */
2336	.long	0		/* 0x2c0 */
2337	.long	0		/* 0x2c4 */
2338	.long	0		/* 0x2c8 */
2339	.long	0		/* 0x2cc */
2340	.long	0		/* 0x2d0 */
2341	.long	0		/* 0x2d4 */
2342	.long	0		/* 0x2d8 */
2343	.long	0		/* 0x2dc */
2344	.long	0		/* 0x2e0 */
2345	.long	0		/* 0x2e4 */
2346	.long	0		/* 0x2e8 */
2347	.long	0		/* 0x2ec */
2348	.long	0		/* 0x2f0 */
2349	.long	0		/* 0x2f4 */
2350	.long	0		/* 0x2f8 */
2351#ifdef CONFIG_KVM_XICS
2352	.long	DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2353#else
2354	.long	0		/* 0x2fc - H_XIRR_X*/
2355#endif
2356	.long	DOTSYM(kvmppc_h_random) - hcall_real_table
2357	.globl	hcall_real_table_end
2358hcall_real_table_end:
2359
2360_GLOBAL(kvmppc_h_set_xdabr)
2361	andi.	r0, r5, DABRX_USER | DABRX_KERNEL
2362	beq	6f
2363	li	r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2364	andc.	r0, r5, r0
2365	beq	3f
23666:	li	r3, H_PARAMETER
2367	blr
2368
2369_GLOBAL(kvmppc_h_set_dabr)
2370	li	r5, DABRX_USER | DABRX_KERNEL
23713:
2372BEGIN_FTR_SECTION
2373	b	2f
2374END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2375	std	r4,VCPU_DABR(r3)
2376	stw	r5, VCPU_DABRX(r3)
2377	mtspr	SPRN_DABRX, r5
2378	/* Work around P7 bug where DABR can get corrupted on mtspr */
23791:	mtspr	SPRN_DABR,r4
2380	mfspr	r5, SPRN_DABR
2381	cmpd	r4, r5
2382	bne	1b
2383	isync
2384	li	r3,0
2385	blr
2386
2387	/* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
23882:	rlwimi	r5, r4, 5, DAWRX_DR | DAWRX_DW
2389	rlwimi	r5, r4, 2, DAWRX_WT
2390	clrrdi	r4, r4, 3
2391	std	r4, VCPU_DAWR(r3)
2392	std	r5, VCPU_DAWRX(r3)
2393	mtspr	SPRN_DAWR, r4
2394	mtspr	SPRN_DAWRX, r5
2395	li	r3, 0
2396	blr
2397
2398_GLOBAL(kvmppc_h_cede)		/* r3 = vcpu pointer, r11 = msr, r13 = paca */
2399	ori	r11,r11,MSR_EE
2400	std	r11,VCPU_MSR(r3)
2401	li	r0,1
2402	stb	r0,VCPU_CEDED(r3)
2403	sync			/* order setting ceded vs. testing prodded */
2404	lbz	r5,VCPU_PRODDED(r3)
2405	cmpwi	r5,0
2406	bne	kvm_cede_prodded
2407	li	r12,0		/* set trap to 0 to say hcall is handled */
2408	stw	r12,VCPU_TRAP(r3)
2409	li	r0,H_SUCCESS
2410	std	r0,VCPU_GPR(R3)(r3)
2411
2412	/*
2413	 * Set our bit in the bitmask of napping threads unless all the
2414	 * other threads are already napping, in which case we send this
2415	 * up to the host.
2416	 */
2417	ld	r5,HSTATE_KVM_VCORE(r13)
2418	lbz	r6,HSTATE_PTID(r13)
2419	lwz	r8,VCORE_ENTRY_EXIT(r5)
2420	clrldi	r8,r8,56
2421	li	r0,1
2422	sld	r0,r0,r6
2423	addi	r6,r5,VCORE_NAPPING_THREADS
242431:	lwarx	r4,0,r6
2425	or	r4,r4,r0
2426	cmpw	r4,r8
2427	beq	kvm_cede_exit
2428	stwcx.	r4,0,r6
2429	bne	31b
2430	/* order napping_threads update vs testing entry_exit_map */
2431	isync
2432	li	r0,NAPPING_CEDE
2433	stb	r0,HSTATE_NAPPING(r13)
2434	lwz	r7,VCORE_ENTRY_EXIT(r5)
2435	cmpwi	r7,0x100
2436	bge	33f		/* another thread already exiting */
2437
2438/*
2439 * Although not specifically required by the architecture, POWER7
2440 * preserves the following registers in nap mode, even if an SMT mode
2441 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2442 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2443 */
2444	/* Save non-volatile GPRs */
2445	std	r14, VCPU_GPR(R14)(r3)
2446	std	r15, VCPU_GPR(R15)(r3)
2447	std	r16, VCPU_GPR(R16)(r3)
2448	std	r17, VCPU_GPR(R17)(r3)
2449	std	r18, VCPU_GPR(R18)(r3)
2450	std	r19, VCPU_GPR(R19)(r3)
2451	std	r20, VCPU_GPR(R20)(r3)
2452	std	r21, VCPU_GPR(R21)(r3)
2453	std	r22, VCPU_GPR(R22)(r3)
2454	std	r23, VCPU_GPR(R23)(r3)
2455	std	r24, VCPU_GPR(R24)(r3)
2456	std	r25, VCPU_GPR(R25)(r3)
2457	std	r26, VCPU_GPR(R26)(r3)
2458	std	r27, VCPU_GPR(R27)(r3)
2459	std	r28, VCPU_GPR(R28)(r3)
2460	std	r29, VCPU_GPR(R29)(r3)
2461	std	r30, VCPU_GPR(R30)(r3)
2462	std	r31, VCPU_GPR(R31)(r3)
2463
2464	/* save FP state */
2465	bl	kvmppc_save_fp
2466
2467#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2468BEGIN_FTR_SECTION
2469	ld	r9, HSTATE_KVM_VCPU(r13)
2470	bl	kvmppc_save_tm
2471END_FTR_SECTION_IFSET(CPU_FTR_TM)
2472#endif
2473
2474	/*
2475	 * Set DEC to the smaller of DEC and HDEC, so that we wake
2476	 * no later than the end of our timeslice (HDEC interrupts
2477	 * don't wake us from nap).
2478	 */
2479	mfspr	r3, SPRN_DEC
2480	mfspr	r4, SPRN_HDEC
2481	mftb	r5
2482BEGIN_FTR_SECTION
2483	/* On P9 check whether the guest has large decrementer mode enabled */
2484	ld	r6, HSTATE_KVM_VCORE(r13)
2485	ld	r6, VCORE_LPCR(r6)
2486	andis.	r6, r6, LPCR_LD@h
2487	bne	68f
2488END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2489	extsw	r3, r3
249068:	EXTEND_HDEC(r4)
2491	cmpd	r3, r4
2492	ble	67f
2493	mtspr	SPRN_DEC, r4
249467:
2495	/* save expiry time of guest decrementer */
2496	add	r3, r3, r5
2497	ld	r4, HSTATE_KVM_VCPU(r13)
2498	ld	r5, HSTATE_KVM_VCORE(r13)
2499	ld	r6, VCORE_TB_OFFSET(r5)
2500	subf	r3, r6, r3	/* convert to host TB value */
2501	std	r3, VCPU_DEC_EXPIRES(r4)
2502
2503#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2504	ld	r4, HSTATE_KVM_VCPU(r13)
2505	addi	r3, r4, VCPU_TB_CEDE
2506	bl	kvmhv_accumulate_time
2507#endif
2508
2509	lis	r3, LPCR_PECEDP@h	/* Do wake on privileged doorbell */
2510
2511	/*
2512	 * Take a nap until a decrementer or external or doobell interrupt
2513	 * occurs, with PECE1 and PECE0 set in LPCR.
2514	 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
2515	 * Also clear the runlatch bit before napping.
2516	 */
2517kvm_do_nap:
2518	mfspr	r0, SPRN_CTRLF
2519	clrrdi	r0, r0, 1
2520	mtspr	SPRN_CTRLT, r0
2521
2522BEGIN_FTR_SECTION
2523	li	r0,1
2524	stb	r0,HSTATE_HWTHREAD_REQ(r13)
2525END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
2526	mfspr	r5,SPRN_LPCR
2527	ori	r5,r5,LPCR_PECE0 | LPCR_PECE1
2528BEGIN_FTR_SECTION
2529	ori	r5, r5, LPCR_PECEDH
2530	rlwimi	r5, r3, 0, LPCR_PECEDP
2531END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2532
2533kvm_nap_sequence:		/* desired LPCR value in r5 */
2534BEGIN_FTR_SECTION
2535	/*
2536	 * PSSCR bits:	exit criterion = 1 (wakeup based on LPCR at sreset)
2537	 *		enable state loss = 1 (allow SMT mode switch)
2538	 *		requested level = 0 (just stop dispatching)
2539	 */
2540	lis	r3, (PSSCR_EC | PSSCR_ESL)@h
2541	mtspr	SPRN_PSSCR, r3
2542	/* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2543	li	r4, LPCR_PECE_HVEE@higher
2544	sldi	r4, r4, 32
2545	or	r5, r5, r4
2546END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2547	mtspr	SPRN_LPCR,r5
2548	isync
2549	li	r0, 0
2550	std	r0, HSTATE_SCRATCH0(r13)
2551	ptesync
2552	ld	r0, HSTATE_SCRATCH0(r13)
25531:	cmpd	r0, r0
2554	bne	1b
2555BEGIN_FTR_SECTION
2556	nap
2557FTR_SECTION_ELSE
2558	PPC_STOP
2559ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
2560	b	.
2561
256233:	mr	r4, r3
2563	li	r3, 0
2564	li	r12, 0
2565	b	34f
2566
2567kvm_end_cede:
2568	/* get vcpu pointer */
2569	ld	r4, HSTATE_KVM_VCPU(r13)
2570
2571	/* Woken by external or decrementer interrupt */
2572	ld	r1, HSTATE_HOST_R1(r13)
2573
2574#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2575	addi	r3, r4, VCPU_TB_RMINTR
2576	bl	kvmhv_accumulate_time
2577#endif
2578
2579#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2580BEGIN_FTR_SECTION
2581	bl	kvmppc_restore_tm
2582END_FTR_SECTION_IFSET(CPU_FTR_TM)
2583#endif
2584
2585	/* load up FP state */
2586	bl	kvmppc_load_fp
2587
2588	/* Restore guest decrementer */
2589	ld	r3, VCPU_DEC_EXPIRES(r4)
2590	ld	r5, HSTATE_KVM_VCORE(r13)
2591	ld	r6, VCORE_TB_OFFSET(r5)
2592	add	r3, r3, r6	/* convert host TB to guest TB value */
2593	mftb	r7
2594	subf	r3, r7, r3
2595	mtspr	SPRN_DEC, r3
2596
2597	/* Load NV GPRS */
2598	ld	r14, VCPU_GPR(R14)(r4)
2599	ld	r15, VCPU_GPR(R15)(r4)
2600	ld	r16, VCPU_GPR(R16)(r4)
2601	ld	r17, VCPU_GPR(R17)(r4)
2602	ld	r18, VCPU_GPR(R18)(r4)
2603	ld	r19, VCPU_GPR(R19)(r4)
2604	ld	r20, VCPU_GPR(R20)(r4)
2605	ld	r21, VCPU_GPR(R21)(r4)
2606	ld	r22, VCPU_GPR(R22)(r4)
2607	ld	r23, VCPU_GPR(R23)(r4)
2608	ld	r24, VCPU_GPR(R24)(r4)
2609	ld	r25, VCPU_GPR(R25)(r4)
2610	ld	r26, VCPU_GPR(R26)(r4)
2611	ld	r27, VCPU_GPR(R27)(r4)
2612	ld	r28, VCPU_GPR(R28)(r4)
2613	ld	r29, VCPU_GPR(R29)(r4)
2614	ld	r30, VCPU_GPR(R30)(r4)
2615	ld	r31, VCPU_GPR(R31)(r4)
2616
2617	/* Check the wake reason in SRR1 to see why we got here */
2618	bl	kvmppc_check_wake_reason
2619
2620	/*
2621	 * Restore volatile registers since we could have called a
2622	 * C routine in kvmppc_check_wake_reason
2623	 *	r4 = VCPU
2624	 * r3 tells us whether we need to return to host or not
2625	 * WARNING: it gets checked further down:
2626	 * should not modify r3 until this check is done.
2627	 */
2628	ld	r4, HSTATE_KVM_VCPU(r13)
2629
2630	/* clear our bit in vcore->napping_threads */
263134:	ld	r5,HSTATE_KVM_VCORE(r13)
2632	lbz	r7,HSTATE_PTID(r13)
2633	li	r0,1
2634	sld	r0,r0,r7
2635	addi	r6,r5,VCORE_NAPPING_THREADS
263632:	lwarx	r7,0,r6
2637	andc	r7,r7,r0
2638	stwcx.	r7,0,r6
2639	bne	32b
2640	li	r0,0
2641	stb	r0,HSTATE_NAPPING(r13)
2642
2643	/* See if the wake reason saved in r3 means we need to exit */
2644	stw	r12, VCPU_TRAP(r4)
2645	mr	r9, r4
2646	cmpdi	r3, 0
2647	bgt	guest_exit_cont
2648
2649	/* see if any other thread is already exiting */
2650	lwz	r0,VCORE_ENTRY_EXIT(r5)
2651	cmpwi	r0,0x100
2652	bge	guest_exit_cont
2653
2654	b	kvmppc_cede_reentry	/* if not go back to guest */
2655
2656	/* cede when already previously prodded case */
2657kvm_cede_prodded:
2658	li	r0,0
2659	stb	r0,VCPU_PRODDED(r3)
2660	sync			/* order testing prodded vs. clearing ceded */
2661	stb	r0,VCPU_CEDED(r3)
2662	li	r3,H_SUCCESS
2663	blr
2664
2665	/* we've ceded but we want to give control to the host */
2666kvm_cede_exit:
2667	ld	r9, HSTATE_KVM_VCPU(r13)
2668	b	guest_exit_cont
2669
2670	/* Try to handle a machine check in real mode */
2671machine_check_realmode:
2672	mr	r3, r9		/* get vcpu pointer */
2673	bl	kvmppc_realmode_machine_check
2674	nop
2675	ld	r9, HSTATE_KVM_VCPU(r13)
2676	li	r12, BOOK3S_INTERRUPT_MACHINE_CHECK
2677	/*
2678	 * For the guest that is FWNMI capable, deliver all the MCE errors
2679	 * (handled/unhandled) by exiting the guest with KVM_EXIT_NMI exit
2680	 * reason. This new approach injects machine check errors in guest
2681	 * address space to guest with additional information in the form
2682	 * of RTAS event, thus enabling guest kernel to suitably handle
2683	 * such errors.
2684	 *
2685	 * For the guest that is not FWNMI capable (old QEMU) fallback
2686	 * to old behaviour for backward compatibility:
2687	 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest either
2688	 * through machine check interrupt (set HSRR0 to 0x200).
2689	 * For handled errors (no-fatal), just go back to guest execution
2690	 * with current HSRR0.
2691	 * if we receive machine check with MSR(RI=0) then deliver it to
2692	 * guest as machine check causing guest to crash.
2693	 */
2694	ld	r11, VCPU_MSR(r9)
2695	rldicl.	r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2696	bne	mc_cont			/* if so, exit to host */
2697	/* Check if guest is capable of handling NMI exit */
2698	ld	r10, VCPU_KVM(r9)
2699	lbz	r10, KVM_FWNMI(r10)
2700	cmpdi	r10, 1			/* FWNMI capable? */
2701	beq	mc_cont			/* if so, exit with KVM_EXIT_NMI. */
2702
2703	/* if not, fall through for backward compatibility. */
2704	andi.	r10, r11, MSR_RI	/* check for unrecoverable exception */
2705	beq	1f			/* Deliver a machine check to guest */
2706	ld	r10, VCPU_PC(r9)
2707	cmpdi	r3, 0		/* Did we handle MCE ? */
2708	bne	2f	/* Continue guest execution. */
2709	/* If not, deliver a machine check.  SRR0/1 are already set */
27101:	li	r10, BOOK3S_INTERRUPT_MACHINE_CHECK
2711	bl	kvmppc_msr_interrupt
27122:	b	fast_interrupt_c_return
2713
2714/*
2715 * Check the reason we woke from nap, and take appropriate action.
2716 * Returns (in r3):
2717 *	0 if nothing needs to be done
2718 *	1 if something happened that needs to be handled by the host
2719 *	-1 if there was a guest wakeup (IPI or msgsnd)
2720 *	-2 if we handled a PCI passthrough interrupt (returned by
2721 *		kvmppc_read_intr only)
2722 *
2723 * Also sets r12 to the interrupt vector for any interrupt that needs
2724 * to be handled now by the host (0x500 for external interrupt), or zero.
2725 * Modifies all volatile registers (since it may call a C function).
2726 * This routine calls kvmppc_read_intr, a C function, if an external
2727 * interrupt is pending.
2728 */
2729kvmppc_check_wake_reason:
2730	mfspr	r6, SPRN_SRR1
2731BEGIN_FTR_SECTION
2732	rlwinm	r6, r6, 45-31, 0xf	/* extract wake reason field (P8) */
2733FTR_SECTION_ELSE
2734	rlwinm	r6, r6, 45-31, 0xe	/* P7 wake reason field is 3 bits */
2735ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2736	cmpwi	r6, 8			/* was it an external interrupt? */
2737	beq	7f			/* if so, see what it was */
2738	li	r3, 0
2739	li	r12, 0
2740	cmpwi	r6, 6			/* was it the decrementer? */
2741	beq	0f
2742BEGIN_FTR_SECTION
2743	cmpwi	r6, 5			/* privileged doorbell? */
2744	beq	0f
2745	cmpwi	r6, 3			/* hypervisor doorbell? */
2746	beq	3f
2747END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2748	cmpwi	r6, 0xa			/* Hypervisor maintenance ? */
2749	beq	4f
2750	li	r3, 1			/* anything else, return 1 */
27510:	blr
2752
2753	/* hypervisor doorbell */
27543:	li	r12, BOOK3S_INTERRUPT_H_DOORBELL
2755
2756	/*
2757	 * Clear the doorbell as we will invoke the handler
2758	 * explicitly in the guest exit path.
2759	 */
2760	lis	r6, (PPC_DBELL_SERVER << (63-36))@h
2761	PPC_MSGCLR(6)
2762	/* see if it's a host IPI */
2763	li	r3, 1
2764	lbz	r0, HSTATE_HOST_IPI(r13)
2765	cmpwi	r0, 0
2766	bnelr
2767	/* if not, return -1 */
2768	li	r3, -1
2769	blr
2770
2771	/* Woken up due to Hypervisor maintenance interrupt */
27724:	li	r12, BOOK3S_INTERRUPT_HMI
2773	li	r3, 1
2774	blr
2775
2776	/* external interrupt - create a stack frame so we can call C */
27777:	mflr	r0
2778	std	r0, PPC_LR_STKOFF(r1)
2779	stdu	r1, -PPC_MIN_STKFRM(r1)
2780	bl	kvmppc_read_intr
2781	nop
2782	li	r12, BOOK3S_INTERRUPT_EXTERNAL
2783	cmpdi	r3, 1
2784	ble	1f
2785
2786	/*
2787	 * Return code of 2 means PCI passthrough interrupt, but
2788	 * we need to return back to host to complete handling the
2789	 * interrupt. Trap reason is expected in r12 by guest
2790	 * exit code.
2791	 */
2792	li	r12, BOOK3S_INTERRUPT_HV_RM_HARD
27931:
2794	ld	r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2795	addi	r1, r1, PPC_MIN_STKFRM
2796	mtlr	r0
2797	blr
2798
2799/*
2800 * Save away FP, VMX and VSX registers.
2801 * r3 = vcpu pointer
2802 * N.B. r30 and r31 are volatile across this function,
2803 * thus it is not callable from C.
2804 */
2805kvmppc_save_fp:
2806	mflr	r30
2807	mr	r31,r3
2808	mfmsr	r5
2809	ori	r8,r5,MSR_FP
2810#ifdef CONFIG_ALTIVEC
2811BEGIN_FTR_SECTION
2812	oris	r8,r8,MSR_VEC@h
2813END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2814#endif
2815#ifdef CONFIG_VSX
2816BEGIN_FTR_SECTION
2817	oris	r8,r8,MSR_VSX@h
2818END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2819#endif
2820	mtmsrd	r8
2821	addi	r3,r3,VCPU_FPRS
2822	bl	store_fp_state
2823#ifdef CONFIG_ALTIVEC
2824BEGIN_FTR_SECTION
2825	addi	r3,r31,VCPU_VRS
2826	bl	store_vr_state
2827END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2828#endif
2829	mfspr	r6,SPRN_VRSAVE
2830	stw	r6,VCPU_VRSAVE(r31)
2831	mtlr	r30
2832	blr
2833
2834/*
2835 * Load up FP, VMX and VSX registers
2836 * r4 = vcpu pointer
2837 * N.B. r30 and r31 are volatile across this function,
2838 * thus it is not callable from C.
2839 */
2840kvmppc_load_fp:
2841	mflr	r30
2842	mr	r31,r4
2843	mfmsr	r9
2844	ori	r8,r9,MSR_FP
2845#ifdef CONFIG_ALTIVEC
2846BEGIN_FTR_SECTION
2847	oris	r8,r8,MSR_VEC@h
2848END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2849#endif
2850#ifdef CONFIG_VSX
2851BEGIN_FTR_SECTION
2852	oris	r8,r8,MSR_VSX@h
2853END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2854#endif
2855	mtmsrd	r8
2856	addi	r3,r4,VCPU_FPRS
2857	bl	load_fp_state
2858#ifdef CONFIG_ALTIVEC
2859BEGIN_FTR_SECTION
2860	addi	r3,r31,VCPU_VRS
2861	bl	load_vr_state
2862END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2863#endif
2864	lwz	r7,VCPU_VRSAVE(r31)
2865	mtspr	SPRN_VRSAVE,r7
2866	mtlr	r30
2867	mr	r4,r31
2868	blr
2869
2870#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2871/*
2872 * Save transactional state and TM-related registers.
2873 * Called with r9 pointing to the vcpu struct.
2874 * This can modify all checkpointed registers, but
2875 * restores r1, r2 and r9 (vcpu pointer) before exit.
2876 */
2877kvmppc_save_tm:
2878	mflr	r0
2879	std	r0, PPC_LR_STKOFF(r1)
2880
2881	/* Turn on TM. */
2882	mfmsr	r8
2883	li	r0, 1
2884	rldimi	r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2885	mtmsrd	r8
2886
2887	ld	r5, VCPU_MSR(r9)
2888	rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2889	beq	1f	/* TM not active in guest. */
2890
2891	std	r1, HSTATE_HOST_R1(r13)
2892	li	r3, TM_CAUSE_KVM_RESCHED
2893
2894	/* Clear the MSR RI since r1, r13 are all going to be foobar. */
2895	li	r5, 0
2896	mtmsrd	r5, 1
2897
2898	/* All GPRs are volatile at this point. */
2899	TRECLAIM(R3)
2900
2901	/* Temporarily store r13 and r9 so we have some regs to play with */
2902	SET_SCRATCH0(r13)
2903	GET_PACA(r13)
2904	std	r9, PACATMSCRATCH(r13)
2905	ld	r9, HSTATE_KVM_VCPU(r13)
2906
2907	/* Get a few more GPRs free. */
2908	std	r29, VCPU_GPRS_TM(29)(r9)
2909	std	r30, VCPU_GPRS_TM(30)(r9)
2910	std	r31, VCPU_GPRS_TM(31)(r9)
2911
2912	/* Save away PPR and DSCR soon so don't run with user values. */
2913	mfspr	r31, SPRN_PPR
2914	HMT_MEDIUM
2915	mfspr	r30, SPRN_DSCR
2916	ld	r29, HSTATE_DSCR(r13)
2917	mtspr	SPRN_DSCR, r29
2918
2919	/* Save all but r9, r13 & r29-r31 */
2920	reg = 0
2921	.rept	29
2922	.if (reg != 9) && (reg != 13)
2923	std	reg, VCPU_GPRS_TM(reg)(r9)
2924	.endif
2925	reg = reg + 1
2926	.endr
2927	/* ... now save r13 */
2928	GET_SCRATCH0(r4)
2929	std	r4, VCPU_GPRS_TM(13)(r9)
2930	/* ... and save r9 */
2931	ld	r4, PACATMSCRATCH(r13)
2932	std	r4, VCPU_GPRS_TM(9)(r9)
2933
2934	/* Reload stack pointer and TOC. */
2935	ld	r1, HSTATE_HOST_R1(r13)
2936	ld	r2, PACATOC(r13)
2937
2938	/* Set MSR RI now we have r1 and r13 back. */
2939	li	r5, MSR_RI
2940	mtmsrd	r5, 1
2941
2942	/* Save away checkpinted SPRs. */
2943	std	r31, VCPU_PPR_TM(r9)
2944	std	r30, VCPU_DSCR_TM(r9)
2945	mflr	r5
2946	mfcr	r6
2947	mfctr	r7
2948	mfspr	r8, SPRN_AMR
2949	mfspr	r10, SPRN_TAR
2950	mfxer	r11
2951	std	r5, VCPU_LR_TM(r9)
2952	stw	r6, VCPU_CR_TM(r9)
2953	std	r7, VCPU_CTR_TM(r9)
2954	std	r8, VCPU_AMR_TM(r9)
2955	std	r10, VCPU_TAR_TM(r9)
2956	std	r11, VCPU_XER_TM(r9)
2957
2958	/* Restore r12 as trap number. */
2959	lwz	r12, VCPU_TRAP(r9)
2960
2961	/* Save FP/VSX. */
2962	addi	r3, r9, VCPU_FPRS_TM
2963	bl	store_fp_state
2964	addi	r3, r9, VCPU_VRS_TM
2965	bl	store_vr_state
2966	mfspr	r6, SPRN_VRSAVE
2967	stw	r6, VCPU_VRSAVE_TM(r9)
29681:
2969	/*
2970	 * We need to save these SPRs after the treclaim so that the software
2971	 * error code is recorded correctly in the TEXASR.  Also the user may
2972	 * change these outside of a transaction, so they must always be
2973	 * context switched.
2974	 */
2975	mfspr	r5, SPRN_TFHAR
2976	mfspr	r6, SPRN_TFIAR
2977	mfspr	r7, SPRN_TEXASR
2978	std	r5, VCPU_TFHAR(r9)
2979	std	r6, VCPU_TFIAR(r9)
2980	std	r7, VCPU_TEXASR(r9)
2981
2982	ld	r0, PPC_LR_STKOFF(r1)
2983	mtlr	r0
2984	blr
2985
2986/*
2987 * Restore transactional state and TM-related registers.
2988 * Called with r4 pointing to the vcpu struct.
2989 * This potentially modifies all checkpointed registers.
2990 * It restores r1, r2, r4 from the PACA.
2991 */
2992kvmppc_restore_tm:
2993	mflr	r0
2994	std	r0, PPC_LR_STKOFF(r1)
2995
2996	/* Turn on TM/FP/VSX/VMX so we can restore them. */
2997	mfmsr	r5
2998	li	r6, MSR_TM >> 32
2999	sldi	r6, r6, 32
3000	or	r5, r5, r6
3001	ori	r5, r5, MSR_FP
3002	oris	r5, r5, (MSR_VEC | MSR_VSX)@h
3003	mtmsrd	r5
3004
3005	/*
3006	 * The user may change these outside of a transaction, so they must
3007	 * always be context switched.
3008	 */
3009	ld	r5, VCPU_TFHAR(r4)
3010	ld	r6, VCPU_TFIAR(r4)
3011	ld	r7, VCPU_TEXASR(r4)
3012	mtspr	SPRN_TFHAR, r5
3013	mtspr	SPRN_TFIAR, r6
3014	mtspr	SPRN_TEXASR, r7
3015
3016	ld	r5, VCPU_MSR(r4)
3017	rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
3018	beqlr		/* TM not active in guest */
3019	std	r1, HSTATE_HOST_R1(r13)
3020
3021	/* Make sure the failure summary is set, otherwise we'll program check
3022	 * when we trechkpt.  It's possible that this might have been not set
3023	 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
3024	 * host.
3025	 */
3026	oris	r7, r7, (TEXASR_FS)@h
3027	mtspr	SPRN_TEXASR, r7
3028
3029	/*
3030	 * We need to load up the checkpointed state for the guest.
3031	 * We need to do this early as it will blow away any GPRs, VSRs and
3032	 * some SPRs.
3033	 */
3034
3035	mr	r31, r4
3036	addi	r3, r31, VCPU_FPRS_TM
3037	bl	load_fp_state
3038	addi	r3, r31, VCPU_VRS_TM
3039	bl	load_vr_state
3040	mr	r4, r31
3041	lwz	r7, VCPU_VRSAVE_TM(r4)
3042	mtspr	SPRN_VRSAVE, r7
3043
3044	ld	r5, VCPU_LR_TM(r4)
3045	lwz	r6, VCPU_CR_TM(r4)
3046	ld	r7, VCPU_CTR_TM(r4)
3047	ld	r8, VCPU_AMR_TM(r4)
3048	ld	r9, VCPU_TAR_TM(r4)
3049	ld	r10, VCPU_XER_TM(r4)
3050	mtlr	r5
3051	mtcr	r6
3052	mtctr	r7
3053	mtspr	SPRN_AMR, r8
3054	mtspr	SPRN_TAR, r9
3055	mtxer	r10
3056
3057	/*
3058	 * Load up PPR and DSCR values but don't put them in the actual SPRs
3059	 * till the last moment to avoid running with userspace PPR and DSCR for
3060	 * too long.
3061	 */
3062	ld	r29, VCPU_DSCR_TM(r4)
3063	ld	r30, VCPU_PPR_TM(r4)
3064
3065	std	r2, PACATMSCRATCH(r13) /* Save TOC */
3066
3067	/* Clear the MSR RI since r1, r13 are all going to be foobar. */
3068	li	r5, 0
3069	mtmsrd	r5, 1
3070
3071	/* Load GPRs r0-r28 */
3072	reg = 0
3073	.rept	29
3074	ld	reg, VCPU_GPRS_TM(reg)(r31)
3075	reg = reg + 1
3076	.endr
3077
3078	mtspr	SPRN_DSCR, r29
3079	mtspr	SPRN_PPR, r30
3080
3081	/* Load final GPRs */
3082	ld	29, VCPU_GPRS_TM(29)(r31)
3083	ld	30, VCPU_GPRS_TM(30)(r31)
3084	ld	31, VCPU_GPRS_TM(31)(r31)
3085
3086	/* TM checkpointed state is now setup.  All GPRs are now volatile. */
3087	TRECHKPT
3088
3089	/* Now let's get back the state we need. */
3090	HMT_MEDIUM
3091	GET_PACA(r13)
3092	ld	r29, HSTATE_DSCR(r13)
3093	mtspr	SPRN_DSCR, r29
3094	ld	r4, HSTATE_KVM_VCPU(r13)
3095	ld	r1, HSTATE_HOST_R1(r13)
3096	ld	r2, PACATMSCRATCH(r13)
3097
3098	/* Set the MSR RI since we have our registers back. */
3099	li	r5, MSR_RI
3100	mtmsrd	r5, 1
3101
3102	ld	r0, PPC_LR_STKOFF(r1)
3103	mtlr	r0
3104	blr
3105#endif
3106
3107/*
3108 * We come here if we get any exception or interrupt while we are
3109 * executing host real mode code while in guest MMU context.
3110 * For now just spin, but we should do something better.
3111 */
3112kvmppc_bad_host_intr:
3113	b	.
3114
3115/*
3116 * This mimics the MSR transition on IRQ delivery.  The new guest MSR is taken
3117 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3118 *   r11 has the guest MSR value (in/out)
3119 *   r9 has a vcpu pointer (in)
3120 *   r0 is used as a scratch register
3121 */
3122kvmppc_msr_interrupt:
3123	rldicl	r0, r11, 64 - MSR_TS_S_LG, 62
3124	cmpwi	r0, 2 /* Check if we are in transactional state..  */
3125	ld	r11, VCPU_INTR_MSR(r9)
3126	bne	1f
3127	/* ... if transactional, change to suspended */
3128	li	r0, 1
31291:	rldimi	r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3130	blr
3131
3132/*
3133 * This works around a hardware bug on POWER8E processors, where
3134 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3135 * performance monitor interrupt.  Instead, when we need to have
3136 * an interrupt pending, we have to arrange for a counter to overflow.
3137 */
3138kvmppc_fix_pmao:
3139	li	r3, 0
3140	mtspr	SPRN_MMCR2, r3
3141	lis	r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3142	ori	r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3143	mtspr	SPRN_MMCR0, r3
3144	lis	r3, 0x7fff
3145	ori	r3, r3, 0xffff
3146	mtspr	SPRN_PMC6, r3
3147	isync
3148	blr
3149
3150#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3151/*
3152 * Start timing an activity
3153 * r3 = pointer to time accumulation struct, r4 = vcpu
3154 */
3155kvmhv_start_timing:
3156	ld	r5, HSTATE_KVM_VCORE(r13)
3157	lbz	r6, VCORE_IN_GUEST(r5)
3158	cmpwi	r6, 0
3159	beq	5f				/* if in guest, need to */
3160	ld	r6, VCORE_TB_OFFSET(r5)		/* subtract timebase offset */
31615:	mftb	r5
3162	subf	r5, r6, r5
3163	std	r3, VCPU_CUR_ACTIVITY(r4)
3164	std	r5, VCPU_ACTIVITY_START(r4)
3165	blr
3166
3167/*
3168 * Accumulate time to one activity and start another.
3169 * r3 = pointer to new time accumulation struct, r4 = vcpu
3170 */
3171kvmhv_accumulate_time:
3172	ld	r5, HSTATE_KVM_VCORE(r13)
3173	lbz	r8, VCORE_IN_GUEST(r5)
3174	cmpwi	r8, 0
3175	beq	4f				/* if in guest, need to */
3176	ld	r8, VCORE_TB_OFFSET(r5)		/* subtract timebase offset */
31774:	ld	r5, VCPU_CUR_ACTIVITY(r4)
3178	ld	r6, VCPU_ACTIVITY_START(r4)
3179	std	r3, VCPU_CUR_ACTIVITY(r4)
3180	mftb	r7
3181	subf	r7, r8, r7
3182	std	r7, VCPU_ACTIVITY_START(r4)
3183	cmpdi	r5, 0
3184	beqlr
3185	subf	r3, r6, r7
3186	ld	r8, TAS_SEQCOUNT(r5)
3187	cmpdi	r8, 0
3188	addi	r8, r8, 1
3189	std	r8, TAS_SEQCOUNT(r5)
3190	lwsync
3191	ld	r7, TAS_TOTAL(r5)
3192	add	r7, r7, r3
3193	std	r7, TAS_TOTAL(r5)
3194	ld	r6, TAS_MIN(r5)
3195	ld	r7, TAS_MAX(r5)
3196	beq	3f
3197	cmpd	r3, r6
3198	bge	1f
31993:	std	r3, TAS_MIN(r5)
32001:	cmpd	r3, r7
3201	ble	2f
3202	std	r3, TAS_MAX(r5)
32032:	lwsync
3204	addi	r8, r8, 1
3205	std	r8, TAS_SEQCOUNT(r5)
3206	blr
3207#endif
3208