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