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