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