blob: 0496e66aaa565fbe69f8fb238f503b0eccb6a27f [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002/*
Paul Mackerrasde56a942011-06-29 00:21:34 +00003 *
4 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
5 *
6 * Derived from book3s_rmhandlers.S and other files, which are:
7 *
8 * Copyright SUSE Linux Products GmbH 2009
9 *
10 * Authors: Alexander Graf <agraf@suse.de>
11 */
12
13#include <asm/ppc_asm.h>
Michael Ellermanaf2e8c62019-11-13 21:05:44 +110014#include <asm/code-patching-asm.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000015#include <asm/kvm_asm.h>
16#include <asm/reg.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100017#include <asm/mmu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000018#include <asm/page.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100019#include <asm/ptrace.h>
20#include <asm/hvcall.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000021#include <asm/asm-offsets.h>
22#include <asm/exception-64s.h>
Paul Mackerrasf0888f72012-02-03 00:54:17 +000023#include <asm/kvm_book3s_asm.h>
Aneesh Kumar K.Vf64e8082016-03-01 12:59:20 +053024#include <asm/book3s/64/mmu-hash.h>
Paul Mackerras41f4e632018-10-08 16:30:51 +110025#include <asm/export.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110026#include <asm/tm.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053027#include <asm/opal.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100028#include <asm/xive-regs.h>
Paul Mackerras857b99e2017-09-01 16:17:27 +100029#include <asm/thread_info.h>
Christophe Leroyec0c4642018-07-05 16:24:57 +000030#include <asm/asm-compat.h>
Christophe Leroy2c86cd12018-07-05 16:25:01 +000031#include <asm/feature-fixups.h>
Nicholas Piggin10d91612019-04-13 00:30:52 +100032#include <asm/cpuidle.h>
Sukadev Bhattiprolu6c85b7bc2019-08-22 00:48:38 -030033#include <asm/ultravisor-api.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110034
Paul Mackerras2f272462017-05-22 16:25:14 +100035/* Sign-extend HDEC if not on POWER9 */
36#define EXTEND_HDEC(reg) \
37BEGIN_FTR_SECTION; \
38 extsw reg, reg; \
39END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
40
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110041/* Values in HSTATE_NAPPING(r13) */
42#define NAPPING_CEDE 1
43#define NAPPING_NOVCPU 2
Nicholas Piggin10d91612019-04-13 00:30:52 +100044#define NAPPING_UNSPLIT 3
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110045
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100046/* Stack frame offsets for kvmppc_hv_entry */
Paul Mackerras95a64322018-10-08 16:30:55 +110047#define SFS 208
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100048#define STACK_SLOT_TRAP (SFS-4)
Paul Mackerras95a64322018-10-08 16:30:55 +110049#define STACK_SLOT_SHORT_PATH (SFS-8)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100050#define STACK_SLOT_TID (SFS-16)
51#define STACK_SLOT_PSSCR (SFS-24)
52#define STACK_SLOT_PID (SFS-32)
53#define STACK_SLOT_IAMR (SFS-40)
54#define STACK_SLOT_CIABR (SFS-48)
55#define STACK_SLOT_DAWR (SFS-56)
56#define STACK_SLOT_DAWRX (SFS-64)
Paul Mackerras769377f2017-02-15 14:30:17 +110057#define STACK_SLOT_HFSCR (SFS-72)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +110058#define STACK_SLOT_AMR (SFS-80)
59#define STACK_SLOT_UAMOR (SFS-88)
Paul Mackerras95a64322018-10-08 16:30:55 +110060/* the following is used by the P9 short path */
61#define STACK_SLOT_NVGPRS (SFS-152) /* 18 gprs */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100062
Paul Mackerrasde56a942011-06-29 00:21:34 +000063/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100064 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000065 * Must be called with interrupts hard-disabled.
66 *
67 * Input Registers:
68 *
69 * LR = return address to continue at after eventually re-enabling MMU
70 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100071_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100072 mflr r0
73 std r0, PPC_LR_STKOFF(r1)
74 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000075 mfmsr r10
Paul Mackerras8b24e692017-06-26 15:45:51 +100076 std r10, HSTATE_HOST_MSR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100077 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000078 li r0,MSR_RI
79 andc r0,r10,r0
80 li r6,MSR_IR | MSR_DR
81 andc r6,r10,r6
82 mtmsrd r0,1 /* clear RI in MSR */
83 mtsrr0 r5
84 mtsrr1 r6
Nicholas Piggin222f20f2018-01-10 03:07:15 +110085 RFI_TO_KERNEL
Paul Mackerrasde56a942011-06-29 00:21:34 +000086
Paul Mackerras218309b2013-09-06 13:23:44 +100087kvmppc_call_hv_entry:
Paul Mackerrasc0101502017-10-19 14:11:23 +110088BEGIN_FTR_SECTION
89 /* On P9, do LPCR setting, if necessary */
90 ld r3, HSTATE_SPLIT_MODE(r13)
91 cmpdi r3, 0
92 beq 46f
93 lwz r4, KVM_SPLIT_DO_SET(r3)
94 cmpwi r4, 0
95 beq 46f
96 bl kvmhv_p9_set_lpcr
97 nop
9846:
99END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
100
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100101 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000102 bl kvmppc_hv_entry
103
104 /* Back from guest - restore host state and return to caller */
105
Michael Neulingeee7ff92014-01-08 21:25:19 +1100106BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +1000107 /* Restore host DABR and DABRX */
108 ld r5,HSTATE_DABR(r13)
109 li r6,7
110 mtspr SPRN_DABR,r5
111 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +1100112END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000113
114 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -0500115 ld r3,PACA_SPRG_VDSO(r13)
116 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +1000117
Paul Mackerras218309b2013-09-06 13:23:44 +1000118 /* Reload the host's PMU registers */
Paul Mackerras41f4e632018-10-08 16:30:51 +1100119 bl kvmhv_load_host_pmu
Paul Mackerras218309b2013-09-06 13:23:44 +1000120
121 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100122 * Reload DEC. HDEC interrupts were disabled when
123 * we reloaded the host's LPCR value.
124 */
125 ld r3, HSTATE_DECEXP(r13)
126 mftb r4
127 subf r4, r4, r3
128 mtspr SPRN_DEC, r4
129
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000130 /* hwthread_req may have got set by cede or no vcpu, so clear it */
131 li r0, 0
132 stb r0, HSTATE_HWTHREAD_REQ(r13)
133
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100134 /*
Aravinda Prasade20bbd32017-05-11 16:33:37 +0530135 * For external interrupts we need to call the Linux
136 * handler to process the interrupt. We do that by jumping
137 * to absolute address 0x500 for external interrupts.
138 * The [h]rfid at the end of the handler will return to
139 * the book3s_hv_interrupts.S code. For other interrupts
140 * we do the rfid to get back to the book3s_hv_interrupts.S
141 * code here.
Paul Mackerras218309b2013-09-06 13:23:44 +1000142 */
143 ld r8, 112+PPC_LR_STKOFF(r1)
144 addi r1, r1, 112
145 ld r7, HSTATE_HOST_MSR(r13)
146
Paul Mackerras8b24e692017-06-26 15:45:51 +1000147 /* Return the trap number on this thread as the return value */
148 mr r3, r12
149
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100150 /*
151 * If we came back from the guest via a relocation-on interrupt,
152 * we will be in virtual mode at this point, which makes it a
153 * little easier to get back to the caller.
154 */
155 mfmsr r0
156 andi. r0, r0, MSR_IR /* in real mode? */
157 bne .Lvirt_return
158
Paul Mackerras8b24e692017-06-26 15:45:51 +1000159 /* RFI into the highmem handler */
Paul Mackerras218309b2013-09-06 13:23:44 +1000160 mfmsr r6
161 li r0, MSR_RI
162 andc r6, r6, r0
163 mtmsrd r6, 1 /* Clear RI in MSR */
164 mtsrr0 r8
165 mtsrr1 r7
Nicholas Piggin222f20f2018-01-10 03:07:15 +1100166 RFI_TO_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000167
Paul Mackerras8b24e692017-06-26 15:45:51 +1000168 /* Virtual-mode return */
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100169.Lvirt_return:
Paul Mackerras8b24e692017-06-26 15:45:51 +1000170 mtlr r8
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100171 blr
172
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100173kvmppc_primary_no_guest:
174 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100175 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
Paul Mackerras2f272462017-05-22 16:25:14 +1000176 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
177 /* HDEC value came from DEC in the first place, it will fit */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100178 mfspr r3, SPRN_HDEC
179 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100180 /*
181 * Make sure the primary has finished the MMU switch.
182 * We should never get here on a secondary thread, but
183 * check it for robustness' sake.
184 */
185 ld r5, HSTATE_KVM_VCORE(r13)
18665: lbz r0, VCORE_IN_GUEST(r5)
187 cmpwi r0, 0
188 beq 65b
189 /* Set LPCR. */
190 ld r8,VCORE_LPCR(r5)
191 mtspr SPRN_LPCR,r8
192 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100193 /* set our bit in napping_threads */
194 ld r5, HSTATE_KVM_VCORE(r13)
195 lbz r7, HSTATE_PTID(r13)
196 li r0, 1
197 sld r0, r0, r7
198 addi r6, r5, VCORE_NAPPING_THREADS
1991: lwarx r3, 0, r6
200 or r3, r3, r0
201 stwcx. r3, 0, r6
202 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100203 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100204 isync
205 li r12, 0
206 lwz r7, VCORE_ENTRY_EXIT(r5)
207 cmpwi r7, 0x100
208 bge kvm_novcpu_exit /* another thread already exiting */
209 li r3, NAPPING_NOVCPU
210 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100211
Paul Mackerrasccc07772015-03-28 14:21:07 +1100212 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100213 b kvm_do_nap
214
Suresh Warrier37f55d32016-08-19 15:35:46 +1000215/*
216 * kvm_novcpu_wakeup
217 * Entered from kvm_start_guest if kvm_hstate.napping is set
218 * to NAPPING_NOVCPU
219 * r2 = kernel TOC
220 * r13 = paca
221 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100222kvm_novcpu_wakeup:
223 ld r1, HSTATE_HOST_R1(r13)
224 ld r5, HSTATE_KVM_VCORE(r13)
225 li r0, 0
226 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100227
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100228 /* check the wake reason */
229 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100230
Suresh Warrier37f55d32016-08-19 15:35:46 +1000231 /*
232 * Restore volatile registers since we could have called
233 * a C routine in kvmppc_check_wake_reason.
234 * r5 = VCORE
235 */
236 ld r5, HSTATE_KVM_VCORE(r13)
237
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100238 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100239 lwz r0, VCORE_ENTRY_EXIT(r5)
240 cmpwi r0, 0x100
241 bge kvm_novcpu_exit
242
243 /* clear our bit in napping_threads */
244 lbz r7, HSTATE_PTID(r13)
245 li r0, 1
246 sld r0, r0, r7
247 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002484: lwarx r7, 0, r6
249 andc r7, r7, r0
250 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100251 bne 4b
252
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100253 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100254 cmpdi r3, 0
255 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100256
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100257 /* See if our timeslice has expired (HDEC is negative) */
258 mfspr r0, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000259 EXTEND_HDEC(r0)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100260 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerras2f272462017-05-22 16:25:14 +1000261 cmpdi r0, 0
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100262 blt kvm_novcpu_exit
263
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100264 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
265 ld r4, HSTATE_KVM_VCPU(r13)
266 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100267 beq kvmppc_primary_no_guest
268
269#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
270 addi r3, r4, VCPU_TB_RMENTRY
271 bl kvmhv_start_timing
272#endif
273 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100274
275kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100276#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
277 ld r4, HSTATE_KVM_VCPU(r13)
278 cmpdi r4, 0
279 beq 13f
280 addi r3, r4, VCPU_TB_RMEXIT
281 bl kvmhv_accumulate_time
282#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110028313: mr r3, r12
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000284 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerraseddb60f2015-03-28 14:21:11 +1100285 bl kvmhv_commence_exit
286 nop
Paul Mackerras6af27c82015-03-28 14:21:10 +1100287 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100288
Paul Mackerras371fefd2011-06-29 00:23:08 +0000289/*
Nicholas Piggin10d91612019-04-13 00:30:52 +1000290 * We come in here when wakened from Linux offline idle code.
291 * Relocation is off
Nicholas Piggin9d292502017-06-13 23:05:51 +1000292 * r3 contains the SRR1 wakeup value, SRR1 is trashed.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000293 */
Nicholas Piggin10d91612019-04-13 00:30:52 +1000294_GLOBAL(idle_kvm_start_guest)
295 ld r4,PACAEMERGSP(r13)
296 mfcr r5
297 mflr r0
298 std r1,0(r4)
299 std r5,8(r4)
300 std r0,16(r4)
301 subi r1,r4,STACK_FRAME_OVERHEAD
302 SAVE_NVGPRS(r1)
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530303
Nicholas Piggin9d292502017-06-13 23:05:51 +1000304 /*
305 * Could avoid this and pass it through in r3. For now,
306 * code expects it to be in SRR1.
307 */
308 mtspr SPRN_SRR1,r3
309
Naveen N. Raoa4bc64d2018-04-19 12:34:05 +0530310 li r0,0
311 stb r0,PACA_FTRACE_ENABLED(r13)
312
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000313 li r0,KVM_HWTHREAD_IN_KVM
314 stb r0,HSTATE_HWTHREAD_STATE(r13)
315
Nicholas Piggin10d91612019-04-13 00:30:52 +1000316 /* kvm cede / napping does not come through here */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000317 lbz r0,HSTATE_NAPPING(r13)
Nicholas Piggin10d91612019-04-13 00:30:52 +1000318 twnei r0,0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100319
Nicholas Piggin10d91612019-04-13 00:30:52 +1000320 b 1f
321
322kvm_unsplit_wakeup:
323 li r0, 0
324 stb r0, HSTATE_NAPPING(r13)
325
3261:
Paul Mackerras4619ac82013-04-17 20:31:41 +0000327
328 /*
329 * We weren't napping due to cede, so this must be a secondary
330 * thread being woken up to run a guest, or being woken up due
331 * to a stray IPI. (Or due to some machine check or hypervisor
332 * maintenance interrupt while the core is in KVM.)
333 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000334
335 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100336 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000337 /*
338 * kvmppc_check_wake_reason could invoke a C routine, but we
339 * have no volatile registers to restore when we return.
340 */
341
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100342 cmpdi r3, 0
343 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000344
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000345 /* get vcore pointer, NULL if we have nothing to run */
346 ld r5,HSTATE_KVM_VCORE(r13)
347 cmpdi r5,0
348 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000349 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000350
Paul Mackerras56548fc2014-12-03 14:48:40 +1100351kvm_secondary_got_guest:
352
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100353 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530354 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100355 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000356
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000357 /* On thread 0 of a subcore, set HDEC to max */
358 lbz r4, HSTATE_PTID(r13)
359 cmpwi r4, 0
360 bne 63f
Paul Mackerras2f272462017-05-22 16:25:14 +1000361 LOAD_REG_ADDR(r6, decrementer_max)
362 ld r6, 0(r6)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000363 mtspr SPRN_HDEC, r6
364 /* and set per-LPAR registers, if doing dynamic micro-threading */
365 ld r6, HSTATE_SPLIT_MODE(r13)
366 cmpdi r6, 0
367 beq 63f
Paul Mackerrasc0101502017-10-19 14:11:23 +1100368BEGIN_FTR_SECTION
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000369 ld r0, KVM_SPLIT_RPR(r6)
370 mtspr SPRN_RPR, r0
371 ld r0, KVM_SPLIT_PMMAR(r6)
372 mtspr SPRN_PMMAR, r0
373 ld r0, KVM_SPLIT_LDBAR(r6)
374 mtspr SPRN_LDBAR, r0
375 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100376FTR_SECTION_ELSE
377 /* On P9 we use the split_info for coordinating LPCR changes */
378 lwz r4, KVM_SPLIT_DO_SET(r6)
379 cmpwi r4, 0
Alexander Grafd20fe502018-02-08 18:38:53 +0100380 beq 1f
Paul Mackerrasc0101502017-10-19 14:11:23 +1100381 mr r3, r6
382 bl kvmhv_p9_set_lpcr
383 nop
Alexander Grafd20fe502018-02-08 18:38:53 +01003841:
Paul Mackerrasc0101502017-10-19 14:11:23 +1100385ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasb4deba52015-07-02 20:38:16 +100038663:
387 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100388 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000389 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100390 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000391
392 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000393 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000394 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000395 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100396 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000397 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100398 * kvmppc_run_core() is going to assume that all our vcpu
399 * state is visible in memory. This lwsync makes sure
400 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100401 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000402 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000403 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000404
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530405 /*
406 * All secondaries exiting guest will fall through this path.
407 * Before proceeding, just check for HMI interrupt and
408 * invoke opal hmi handler. By now we are sure that the
409 * primary thread on this core/subcore has already made partition
410 * switch/TB resync and we are good to call opal hmi handler.
411 */
412 cmpwi r12, BOOK3S_INTERRUPT_HMI
413 bne kvm_no_guest
414
415 li r3,0 /* NULL argument */
416 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100417/*
418 * At this point we have finished executing in the guest.
419 * We need to wait for hwthread_req to become zero, since
420 * we may not turn on the MMU while hwthread_req is non-zero.
421 * While waiting we also need to check if we get given a vcpu to run.
422 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000423kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100424 lbz r3, HSTATE_HWTHREAD_REQ(r13)
425 cmpwi r3, 0
426 bne 53f
427 HMT_MEDIUM
428 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000429 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100430 /* need to recheck hwthread_req after a barrier, to avoid race */
431 sync
432 lbz r3, HSTATE_HWTHREAD_REQ(r13)
433 cmpwi r3, 0
434 bne 54f
Nicholas Piggin10d91612019-04-13 00:30:52 +1000435
436 /*
437 * Jump to idle_return_gpr_loss, which returns to the
438 * idle_kvm_start_guest caller.
439 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000440 li r3, LPCR_PECE0
441 mfspr r4, SPRN_LPCR
442 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
443 mtspr SPRN_LPCR, r4
Nicholas Piggin10d91612019-04-13 00:30:52 +1000444 /* set up r3 for return */
445 mfspr r3,SPRN_SRR1
446 REST_NVGPRS(r1)
447 addi r1, r1, STACK_FRAME_OVERHEAD
448 ld r0, 16(r1)
449 ld r5, 8(r1)
450 ld r1, 0(r1)
451 mtlr r0
452 mtcr r5
453 blr
Paul Mackerras56548fc2014-12-03 14:48:40 +1100454
45553: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000456 ld r5, HSTATE_KVM_VCORE(r13)
457 cmpdi r5, 0
458 bne 60f
459 ld r3, HSTATE_SPLIT_MODE(r13)
460 cmpdi r3, 0
461 beq kvm_no_guest
Paul Mackerrasc0101502017-10-19 14:11:23 +1100462 lwz r0, KVM_SPLIT_DO_SET(r3)
463 cmpwi r0, 0
464 bne kvmhv_do_set
465 lwz r0, KVM_SPLIT_DO_RESTORE(r3)
466 cmpwi r0, 0
467 bne kvmhv_do_restore
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000468 lbz r0, KVM_SPLIT_DO_NAP(r3)
469 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100470 beq kvm_no_guest
471 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000472 b kvm_unsplit_nap
47360: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100474 b kvm_secondary_got_guest
475
47654: li r0, KVM_HWTHREAD_IN_KVM
477 stb r0, HSTATE_HWTHREAD_STATE(r13)
478 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000479
Paul Mackerrasc0101502017-10-19 14:11:23 +1100480kvmhv_do_set:
481 /* Set LPCR, LPIDR etc. on P9 */
482 HMT_MEDIUM
483 bl kvmhv_p9_set_lpcr
484 nop
485 b kvm_no_guest
486
487kvmhv_do_restore:
488 HMT_MEDIUM
489 bl kvmhv_p9_restore_lpcr
490 nop
491 b kvm_no_guest
492
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000493/*
494 * Here the primary thread is trying to return the core to
495 * whole-core mode, so we need to nap.
496 */
497kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530498 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530499 * When secondaries are napping in kvm_unsplit_nap() with
500 * hwthread_req = 1, HMI goes ignored even though subcores are
501 * already exited the guest. Hence HMI keeps waking up secondaries
502 * from nap in a loop and secondaries always go back to nap since
503 * no vcore is assigned to them. This makes impossible for primary
504 * thread to get hold of secondary threads resulting into a soft
505 * lockup in KVM path.
506 *
507 * Let us check if HMI is pending and handle it before we go to nap.
508 */
509 cmpwi r12, BOOK3S_INTERRUPT_HMI
510 bne 55f
511 li r3, 0 /* NULL argument */
512 bl hmi_exception_realmode
51355:
514 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530515 * Ensure that secondary doesn't nap when it has
516 * its vcore pointer set.
517 */
518 sync /* matches smp_mb() before setting split_info.do_nap */
519 ld r0, HSTATE_KVM_VCORE(r13)
520 cmpdi r0, 0
521 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000522 /* clear any pending message */
523BEGIN_FTR_SECTION
524 lis r6, (PPC_DBELL_SERVER << (63-36))@h
525 PPC_MSGCLR(6)
526END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
527 /* Set kvm_split_mode.napped[tid] = 1 */
528 ld r3, HSTATE_SPLIT_MODE(r13)
529 li r0, 1
Paul Mackerrasc0101502017-10-19 14:11:23 +1100530 lbz r4, HSTATE_TID(r13)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000531 addi r4, r4, KVM_SPLIT_NAPPED
532 stbx r0, r3, r4
533 /* Check the do_nap flag again after setting napped[] */
534 sync
535 lbz r0, KVM_SPLIT_DO_NAP(r3)
536 cmpwi r0, 0
537 beq 57f
Nicholas Piggin10d91612019-04-13 00:30:52 +1000538 li r3, NAPPING_UNSPLIT
539 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000540 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
Paul Mackerrasbf53c882016-11-18 14:34:07 +1100541 mfspr r5, SPRN_LPCR
542 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
543 b kvm_nap_sequence
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000544
54557: li r0, 0
546 stbx r0, r3, r4
547 b kvm_no_guest
548
Paul Mackerras218309b2013-09-06 13:23:44 +1000549/******************************************************************************
550 * *
551 * Entry code *
552 * *
553 *****************************************************************************/
554
Paul Mackerrasde56a942011-06-29 00:21:34 +0000555.global kvmppc_hv_entry
556kvmppc_hv_entry:
557
558 /* Required state:
559 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100560 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000561 * MSR = ~IR|DR
562 * R13 = PACA
563 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000564 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000565 * all other volatile GPRS = free
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100566 * Does not preserve non-volatile GPRs or CR fields
Paul Mackerrasde56a942011-06-29 00:21:34 +0000567 */
568 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000569 std r0, PPC_LR_STKOFF(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000570 stdu r1, -SFS(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000571
Paul Mackerrasde56a942011-06-29 00:21:34 +0000572 /* Save R1 in the PACA */
573 std r1, HSTATE_HOST_R1(r13)
574
Paul Mackerras44a3add2013-10-04 21:45:04 +1000575 li r6, KVM_GUEST_MODE_HOST_HV
576 stb r6, HSTATE_IN_GUEST(r13)
577
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100578#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
579 /* Store initial timestamp */
580 cmpdi r4, 0
581 beq 1f
582 addi r3, r4, VCPU_TB_RMENTRY
583 bl kvmhv_start_timing
5841:
585#endif
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100586
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100587 ld r5, HSTATE_KVM_VCORE(r13)
588 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100589
Paul Mackerras9e368f22011-06-29 00:40:08 +0000590 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100591 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000592 * We don't have to lock against concurrent tlbies,
593 * but we do have to coordinate across hardware threads.
594 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100595 /* Set bit in entry map iff exit map is zero. */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100596 li r7, 1
597 lbz r6, HSTATE_PTID(r13)
598 sld r7, r7, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100599 addi r8, r5, VCORE_ENTRY_EXIT
60021: lwarx r3, 0, r8
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100601 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000602 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100603 or r3, r3, r7
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100604 stwcx. r3, 0, r8
Paul Mackerras371fefd2011-06-29 00:23:08 +0000605 bne 21b
606
607 /* Primary thread switches to guest partition. */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000608 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100609 bne 10f
Nicholas Piggin9a4506e2018-05-17 17:06:29 +1000610
Paul Mackerrasde56a942011-06-29 00:21:34 +0000611 lwz r7,KVM_LPID(r9)
Paul Mackerras7a840842016-11-16 22:25:20 +1100612BEGIN_FTR_SECTION
613 ld r6,KVM_SDR1(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000614 li r0,LPID_RSVD /* switch to reserved LPID */
615 mtspr SPRN_LPID,r0
616 ptesync
617 mtspr SPRN_SDR1,r6 /* switch to partition page table */
Paul Mackerras7a840842016-11-16 22:25:20 +1100618END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000619 mtspr SPRN_LPID,r7
620 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000621
Paul Mackerras70ea13f2019-04-29 19:02:58 +1000622 /* See if we need to flush the TLB. */
Paul Mackerras2940ba02019-04-29 19:00:40 +1000623 mr r3, r9 /* kvm pointer */
Paul Mackerras70ea13f2019-04-29 19:02:58 +1000624 lhz r4, PACAPACAINDEX(r13) /* physical cpu number */
625 li r5, 0 /* nested vcpu pointer */
626 bl kvmppc_check_need_tlb_flush
Paul Mackerras2940ba02019-04-29 19:00:40 +1000627 nop
628 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000629
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000630 /* Add timebase offset onto timebase */
63122: ld r8,VCORE_TB_OFFSET(r5)
632 cmpdi r8,0
633 beq 37f
Paul Mackerras57b8daa2018-04-20 22:51:11 +1000634 std r8, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000635 mftb r6 /* current host timebase */
636 add r8,r8,r6
637 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
638 mftb r7 /* check if lower 24 bits overflowed */
639 clrldi r6,r6,40
640 clrldi r7,r7,40
641 cmpld r7,r6
642 bge 37f
643 addis r8,r8,0x100 /* if so, increment upper 40 bits */
644 mtspr SPRN_TBU40,r8
645
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000646 /* Load guest PCR value to select appropriate compat mode */
64737: ld r7, VCORE_PCR(r5)
Jordan Niethe13c7bb32019-09-17 10:46:05 +1000648 LOAD_REG_IMMEDIATE(r6, PCR_MASK)
649 cmpld r7, r6
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000650 beq 38f
Jordan Niethe13c7bb32019-09-17 10:46:05 +1000651 or r7, r7, r6
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000652 mtspr SPRN_PCR, r7
65338:
Michael Neulingb005255e2014-01-08 21:25:21 +1100654
655BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000656 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100657 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000658 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100659 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000660 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100661END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
662
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530663 /* Mark the subcore state as inside guest */
664 bl kvmppc_subcore_enter_guest
665 nop
666 ld r5, HSTATE_KVM_VCORE(r13)
667 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000668 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000669 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000670
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100671 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110067210: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100673 beq kvmppc_primary_no_guest
674kvmppc_got_guest:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100675 /* Increment yield count if they have a VPA */
676 ld r3, VCPU_VPA(r4)
677 cmpdi r3, 0
678 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200679 li r6, LPPACA_YIELDCOUNT
680 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100681 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200682 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100683 li r6, 1
684 stb r6, VCPU_VPA_DIRTY(r4)
68525:
686
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100687 /* Save purr/spurr */
688 mfspr r5,SPRN_PURR
689 mfspr r6,SPRN_SPURR
690 std r5,HSTATE_PURR(r13)
691 std r6,HSTATE_SPURR(r13)
692 ld r7,VCPU_PURR(r4)
693 ld r8,VCPU_SPURR(r4)
694 mtspr SPRN_PURR,r7
695 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100696
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100697 /* Save host values of some registers */
698BEGIN_FTR_SECTION
699 mfspr r5, SPRN_TIDR
700 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100701 mfspr r7, SPRN_PID
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100702 std r5, STACK_SLOT_TID(r1)
703 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100704 std r7, STACK_SLOT_PID(r1)
Paul Mackerras769377f2017-02-15 14:30:17 +1100705 mfspr r5, SPRN_HFSCR
706 std r5, STACK_SLOT_HFSCR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100707END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000708BEGIN_FTR_SECTION
709 mfspr r5, SPRN_CIABR
710 mfspr r6, SPRN_DAWR
711 mfspr r7, SPRN_DAWRX
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100712 mfspr r8, SPRN_IAMR
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000713 std r5, STACK_SLOT_CIABR(r1)
714 std r6, STACK_SLOT_DAWR(r1)
715 std r7, STACK_SLOT_DAWRX(r1)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100716 std r8, STACK_SLOT_IAMR(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000717END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100718
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100719 mfspr r5, SPRN_AMR
720 std r5, STACK_SLOT_AMR(r1)
721 mfspr r6, SPRN_UAMOR
722 std r6, STACK_SLOT_UAMOR(r1)
723
Michael Neulingeee7ff92014-01-08 21:25:19 +1100724BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000725 /* Set partition DABR */
726 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100727 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000728 ld r6,VCPU_DABR(r4)
729 mtspr SPRN_DABRX,r5
730 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000731 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100732END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000733
Michael Neulinge4e38122014-03-25 10:47:02 +1100734#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100735/*
736 * Branch around the call if both CPU_FTR_TM and
737 * CPU_FTR_P9_TM_HV_ASSIST are off.
738 */
Michael Neulinge4e38122014-03-25 10:47:02 +1100739BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100740 b 91f
741END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000742 /*
Paul Mackerras7854f752018-10-08 16:30:53 +1100743 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000744 */
Simon Guo6f597c62018-05-23 15:01:48 +0800745 mr r3, r4
746 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +1100747 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +1000748 bl kvmppc_restore_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +1100749 nop
Simon Guo6f597c62018-05-23 15:01:48 +0800750 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +110075191:
Michael Neulinge4e38122014-03-25 10:47:02 +1100752#endif
753
Paul Mackerras41f4e632018-10-08 16:30:51 +1100754 /* Load guest PMU registers; r4 = vcpu pointer here */
755 mr r3, r4
756 bl kvmhv_load_guest_pmu
Paul Mackerrasde56a942011-06-29 00:21:34 +0000757
758 /* Load up FP, VMX and VSX registers */
Paul Mackerras41f4e632018-10-08 16:30:51 +1100759 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000760 bl kvmppc_load_fp
761
762 ld r14, VCPU_GPR(R14)(r4)
763 ld r15, VCPU_GPR(R15)(r4)
764 ld r16, VCPU_GPR(R16)(r4)
765 ld r17, VCPU_GPR(R17)(r4)
766 ld r18, VCPU_GPR(R18)(r4)
767 ld r19, VCPU_GPR(R19)(r4)
768 ld r20, VCPU_GPR(R20)(r4)
769 ld r21, VCPU_GPR(R21)(r4)
770 ld r22, VCPU_GPR(R22)(r4)
771 ld r23, VCPU_GPR(R23)(r4)
772 ld r24, VCPU_GPR(R24)(r4)
773 ld r25, VCPU_GPR(R25)(r4)
774 ld r26, VCPU_GPR(R26)(r4)
775 ld r27, VCPU_GPR(R27)(r4)
776 ld r28, VCPU_GPR(R28)(r4)
777 ld r29, VCPU_GPR(R29)(r4)
778 ld r30, VCPU_GPR(R30)(r4)
779 ld r31, VCPU_GPR(R31)(r4)
780
Paul Mackerrasde56a942011-06-29 00:21:34 +0000781 /* Switch DSCR to guest value */
782 ld r5, VCPU_DSCR(r4)
783 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000784
Michael Neulingb005255e2014-01-08 21:25:21 +1100785BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100786 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100787 b 8f
788END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100789 /* Load up POWER8-specific registers */
790 ld r5, VCPU_IAMR(r4)
791 lwz r6, VCPU_PSPB(r4)
792 ld r7, VCPU_FSCR(r4)
793 mtspr SPRN_IAMR, r5
794 mtspr SPRN_PSPB, r6
795 mtspr SPRN_FSCR, r7
Michael Neulingb53221e2018-03-27 15:37:22 +1100796 /*
797 * Handle broken DAWR case by not writing it. This means we
798 * can still store the DAWR register for migration.
799 */
Michael Neulingc1fe1902019-04-01 17:03:12 +1100800 LOAD_REG_ADDR(r5, dawr_force_enable)
801 lbz r5, 0(r5)
802 cmpdi r5, 0
803 beq 1f
804 ld r5, VCPU_DAWR(r4)
805 ld r6, VCPU_DAWRX(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100806 mtspr SPRN_DAWR, r5
807 mtspr SPRN_DAWRX, r6
Michael Neulingc1fe1902019-04-01 17:03:12 +11008081:
809 ld r7, VCPU_CIABR(r4)
810 ld r8, VCPU_TAR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100811 mtspr SPRN_CIABR, r7
812 mtspr SPRN_TAR, r8
813 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100814 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000815 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100816 mtspr SPRN_EBBHR, r8
817 ld r5, VCPU_EBBRR(r4)
818 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100819 lwz r7, VCPU_GUEST_PID(r4)
820 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100821 mtspr SPRN_EBBRR, r5
822 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100823 mtspr SPRN_PID, r7
824 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100825BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100826 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100827 ld r5, VCPU_TCSCR(r4)
828 ld r6, VCPU_ACOP(r4)
829 ld r7, VCPU_CSIGR(r4)
830 ld r8, VCPU_TACR(r4)
831 mtspr SPRN_TCSCR, r5
832 mtspr SPRN_ACOP, r6
833 mtspr SPRN_CSIGR, r7
834 mtspr SPRN_TACR, r8
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100835 nop
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100836FTR_SECTION_ELSE
837 /* POWER9-only registers */
838 ld r5, VCPU_TID(r4)
839 ld r6, VCPU_PSSCR(r4)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100840 lbz r8, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100841 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100842 rldimi r6, r8, PSSCR_FAKE_SUSPEND_LG, 63 - PSSCR_FAKE_SUSPEND_LG
Paul Mackerras769377f2017-02-15 14:30:17 +1100843 ld r7, VCPU_HFSCR(r4)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100844 mtspr SPRN_TIDR, r5
845 mtspr SPRN_PSSCR, r6
Paul Mackerras769377f2017-02-15 14:30:17 +1100846 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100847ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11008488:
849
Paul Mackerrasde56a942011-06-29 00:21:34 +0000850 ld r5, VCPU_SPRG0(r4)
851 ld r6, VCPU_SPRG1(r4)
852 ld r7, VCPU_SPRG2(r4)
853 ld r8, VCPU_SPRG3(r4)
854 mtspr SPRN_SPRG0, r5
855 mtspr SPRN_SPRG1, r6
856 mtspr SPRN_SPRG2, r7
857 mtspr SPRN_SPRG3, r8
858
Paul Mackerrasde56a942011-06-29 00:21:34 +0000859 /* Load up DAR and DSISR */
860 ld r5, VCPU_DAR(r4)
861 lwz r6, VCPU_DSISR(r4)
862 mtspr SPRN_DAR, r5
863 mtspr SPRN_DSISR, r6
864
Paul Mackerrasde56a942011-06-29 00:21:34 +0000865 /* Restore AMR and UAMOR, set AMOR to all 1s */
866 ld r5,VCPU_AMR(r4)
867 ld r6,VCPU_UAMOR(r4)
868 li r7,-1
869 mtspr SPRN_AMR,r5
870 mtspr SPRN_UAMOR,r6
871 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000872
873 /* Restore state of CTRL run bit; assume 1 on entry */
874 lwz r5,VCPU_CTRL(r4)
875 andi. r5,r5,1
876 bne 4f
877 mfspr r6,SPRN_CTRLF
878 clrrdi r6,r6,1
879 mtspr SPRN_CTRLT,r6
8804:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100881 /* Secondary threads wait for primary to have done partition switch */
882 ld r5, HSTATE_KVM_VCORE(r13)
883 lbz r6, HSTATE_PTID(r13)
884 cmpwi r6, 0
885 beq 21f
886 lbz r0, VCORE_IN_GUEST(r5)
887 cmpwi r0, 0
888 bne 21f
889 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100089020: lwz r3, VCORE_ENTRY_EXIT(r5)
891 cmpwi r3, 0x100
892 bge no_switch_exit
893 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100894 cmpwi r0, 0
895 beq 20b
896 HMT_MEDIUM
89721:
898 /* Set LPCR. */
899 ld r8,VCORE_LPCR(r5)
900 mtspr SPRN_LPCR,r8
901 isync
902
Paul Mackerras57b8daa2018-04-20 22:51:11 +1000903 /*
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_APPL(r5)
910 add r8,r8,r6
911 mftb r7
912 subf r3,r7,r8
913 mtspr SPRN_DEC,r3
914
Paul Mackerras6af27c82015-03-28 14:21:10 +1100915 /* Check if HDEC expires soon */
916 mfspr r3, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000917 EXTEND_HDEC(r3)
918 cmpdi r3, 512 /* 1 microsecond */
Paul Mackerras6af27c82015-03-28 14:21:10 +1100919 blt hdec_soon
920
Paul Mackerras6964e6a2018-01-11 14:51:02 +1100921 /* For hash guest, clear out and reload the SLB */
922 ld r6, VCPU_KVM(r4)
923 lbz r0, KVM_RADIX(r6)
924 cmpwi r0, 0
925 bne 9f
926 li r6, 0
927 slbmte r6, r6
928 slbia
929 ptesync
930
931 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
932 lwz r5,VCPU_SLB_MAX(r4)
933 cmpwi r5,0
934 beq 9f
935 mtctr r5
936 addi r6,r4,VCPU_SLB
9371: ld r8,VCPU_SLB_E(r6)
938 ld r9,VCPU_SLB_V(r6)
939 slbmte r9,r8
940 addi r6,r6,VCPU_SLB_SIZE
941 bdnz 1b
9429:
943
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000944#ifdef CONFIG_KVM_XICS
945 /* We are entering the guest on that thread, push VCPU to XIVE */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000946 ld r11, VCPU_XIVE_SAVED_STATE(r4)
947 li r9, TM_QW1_OS
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000948 lwz r8, VCPU_XIVE_CAM_WORD(r4)
Paul Mackerras8d4ba9c2019-08-13 20:01:00 +1000949 cmpwi r8, 0
950 beq no_xive
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000951 li r7, TM_QW1_OS + TM_WORD2
952 mfmsr r0
953 andi. r0, r0, MSR_DR /* in real mode? */
954 beq 2f
955 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
956 cmpldi cr1, r10, 0
957 beq cr1, no_xive
958 eieio
959 stdx r11,r9,r10
960 stwx r8,r7,r10
961 b 3f
9622: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
963 cmpldi cr1, r10, 0
964 beq cr1, no_xive
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000965 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +1100966 stdcix r11,r9,r10
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000967 stwcix r8,r7,r10
9683: li r9, 1
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +1100969 stb r9, VCPU_XIVE_PUSHED(r4)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +1100970 eieio
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +1100971
972 /*
973 * We clear the irq_pending flag. There is a small chance of a
974 * race vs. the escalation interrupt happening on another
975 * processor setting it again, but the only consequence is to
976 * cause a spurrious wakeup on the next H_CEDE which is not an
977 * issue.
978 */
979 li r0,0
980 stb r0, VCPU_IRQ_PENDING(r4)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100981
982 /*
983 * In single escalation mode, if the escalation interrupt is
984 * on, we mask it.
985 */
986 lbz r0, VCPU_XIVE_ESC_ON(r4)
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000987 cmpwi cr1, r0,0
988 beq cr1, 1f
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100989 li r9, XIVE_ESB_SET_PQ_01
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000990 beq 4f /* in real mode? */
991 ld r10, VCPU_XIVE_ESC_VADDR(r4)
992 ldx r0, r10, r9
993 b 5f
9944: ld r10, VCPU_XIVE_ESC_RADDR(r4)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100995 ldcix r0, r10, r9
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +10009965: sync
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100997
998 /* We have a possible subtle race here: The escalation interrupt might
999 * have fired and be on its way to the host queue while we mask it,
1000 * and if we unmask it early enough (re-cede right away), there is
1001 * a theorical possibility that it fires again, thus landing in the
1002 * target queue more than once which is a big no-no.
1003 *
1004 * Fortunately, solving this is rather easy. If the above load setting
1005 * PQ to 01 returns a previous value where P is set, then we know the
1006 * escalation interrupt is somewhere on its way to the host. In that
1007 * case we simply don't clear the xive_esc_on flag below. It will be
1008 * eventually cleared by the handler for the escalation interrupt.
1009 *
1010 * Then, when doing a cede, we check that flag again before re-enabling
1011 * the escalation interrupt, and if set, we abort the cede.
1012 */
1013 andi. r0, r0, XIVE_ESB_VAL_P
1014 bne- 1f
1015
1016 /* Now P is 0, we can clear the flag */
1017 li r0, 0
1018 stb r0, VCPU_XIVE_ESC_ON(r4)
10191:
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001020no_xive:
1021#endif /* CONFIG_KVM_XICS */
1022
Paul Mackerras95a64322018-10-08 16:30:55 +11001023 li r0, 0
1024 stw r0, STACK_SLOT_SHORT_PATH(r1)
1025
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001026deliver_guest_interrupt: /* r4 = vcpu, r13 = paca */
Paul Mackerrasf7035ce2018-10-08 16:30:50 +11001027 /* Check if we can deliver an external or decrementer interrupt now */
1028 ld r0, VCPU_PENDING_EXC(r4)
1029BEGIN_FTR_SECTION
1030 /* On POWER9, also check for emulated doorbell interrupt */
1031 lbz r3, VCPU_DBELL_REQ(r4)
1032 or r0, r0, r3
1033END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1034 cmpdi r0, 0
1035 beq 71f
1036 mr r3, r4
1037 bl kvmppc_guest_entry_inject_int
1038 ld r4, HSTATE_KVM_VCPU(r13)
103971:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001040 ld r6, VCPU_SRR0(r4)
1041 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001042 mtspr SPRN_SRR0, r6
1043 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001044
Paul Mackerras95a64322018-10-08 16:30:55 +11001045fast_guest_entry_c:
1046 ld r10, VCPU_PC(r4)
1047 ld r11, VCPU_MSR(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001048 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001049 rldicl r11, r11, 63 - MSR_HV_LG, 1
1050 rotldi r11, r11, 1 + MSR_HV_LG
1051 ori r11, r11, MSR_ME
1052
Paul Mackerrasf7035ce2018-10-08 16:30:50 +11001053 ld r6, VCPU_CTR(r4)
1054 ld r7, VCPU_XER(r4)
1055 mtctr r6
1056 mtxer r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10001057
Liu Ping Fan27025a62013-11-19 14:12:48 +08001058/*
1059 * Required state:
1060 * R4 = vcpu
1061 * R10: value for HSRR0
1062 * R11: value for HSRR1
1063 * R13 = PACA
1064 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001065fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001066 li r0,0
1067 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001068 mtspr SPRN_HSRR0,r10
1069 mtspr SPRN_HSRR1,r11
1070
1071 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001072 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001073 stb r9, HSTATE_IN_GUEST(r13)
1074
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001075#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1076 /* Accumulate timing */
1077 addi r3, r4, VCPU_TB_GUEST
1078 bl kvmhv_accumulate_time
1079#endif
1080
Paul Mackerrasde56a942011-06-29 00:21:34 +00001081 /* Enter guest */
1082
Paul Mackerras0acb9112013-02-04 18:10:51 +00001083BEGIN_FTR_SECTION
1084 ld r5, VCPU_CFAR(r4)
1085 mtspr SPRN_CFAR, r5
1086END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001087BEGIN_FTR_SECTION
1088 ld r0, VCPU_PPR(r4)
1089END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001090
Paul Mackerrasde56a942011-06-29 00:21:34 +00001091 ld r5, VCPU_LR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001092 mtlr r5
Paul Mackerrasde56a942011-06-29 00:21:34 +00001093
Michael Neulingc75df6f2012-06-25 13:33:10 +00001094 ld r1, VCPU_GPR(R1)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001095 ld r5, VCPU_GPR(R5)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001096 ld r8, VCPU_GPR(R8)(r4)
1097 ld r9, VCPU_GPR(R9)(r4)
1098 ld r10, VCPU_GPR(R10)(r4)
1099 ld r11, VCPU_GPR(R11)(r4)
1100 ld r12, VCPU_GPR(R12)(r4)
1101 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001102
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001103BEGIN_FTR_SECTION
1104 mtspr SPRN_PPR, r0
1105END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Michael Neulinge001fa72017-09-15 15:26:14 +10001106
1107/* Move canary into DSISR to check for later */
1108BEGIN_FTR_SECTION
1109 li r0, 0x7fff
1110 mtspr SPRN_HDSISR, r0
1111END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1112
Sukadev Bhattiprolu6c85b7bc2019-08-22 00:48:38 -03001113 ld r6, VCPU_KVM(r4)
1114 lbz r7, KVM_SECURE_GUEST(r6)
1115 cmpdi r7, 0
1116 ld r6, VCPU_GPR(R6)(r4)
1117 ld r7, VCPU_GPR(R7)(r4)
1118 bne ret_to_ultra
1119
1120 lwz r0, VCPU_CR(r4)
1121 mtcr r0
1122
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001123 ld r0, VCPU_GPR(R0)(r4)
Sukadev Bhattiprolu6c85b7bc2019-08-22 00:48:38 -03001124 ld r2, VCPU_GPR(R2)(r4)
1125 ld r3, VCPU_GPR(R3)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001126 ld r4, VCPU_GPR(R4)(r4)
Nicholas Piggin222f20f2018-01-10 03:07:15 +11001127 HRFI_TO_GUEST
Paul Mackerrasde56a942011-06-29 00:21:34 +00001128 b .
Sukadev Bhattiprolu6c85b7bc2019-08-22 00:48:38 -03001129/*
1130 * Use UV_RETURN ultracall to return control back to the Ultravisor after
1131 * processing an hypercall or interrupt that was forwarded (a.k.a. reflected)
1132 * to the Hypervisor.
1133 *
1134 * All registers have already been loaded, except:
1135 * R0 = hcall result
1136 * R2 = SRR1, so UV can detect a synthesized interrupt (if any)
1137 * R3 = UV_RETURN
1138 */
1139ret_to_ultra:
1140 lwz r0, VCPU_CR(r4)
1141 mtcr r0
1142
1143 ld r0, VCPU_GPR(R3)(r4)
1144 mfspr r2, SPRN_SRR1
1145 li r3, 0
1146 ori r3, r3, UV_RETURN
1147 ld r4, VCPU_GPR(R4)(r4)
1148 sc 2
Paul Mackerrasde56a942011-06-29 00:21:34 +00001149
Paul Mackerras95a64322018-10-08 16:30:55 +11001150/*
1151 * Enter the guest on a P9 or later system where we have exactly
1152 * one vcpu per vcore and we don't need to go to real mode
1153 * (which implies that host and guest are both using radix MMU mode).
1154 * r3 = vcpu pointer
1155 * Most SPRs and all the VSRs have been loaded already.
1156 */
1157_GLOBAL(__kvmhv_vcpu_entry_p9)
1158EXPORT_SYMBOL_GPL(__kvmhv_vcpu_entry_p9)
1159 mflr r0
1160 std r0, PPC_LR_STKOFF(r1)
1161 stdu r1, -SFS(r1)
1162
1163 li r0, 1
1164 stw r0, STACK_SLOT_SHORT_PATH(r1)
1165
1166 std r3, HSTATE_KVM_VCPU(r13)
1167 mfcr r4
1168 stw r4, SFS+8(r1)
1169
1170 std r1, HSTATE_HOST_R1(r13)
1171
1172 reg = 14
1173 .rept 18
1174 std reg, STACK_SLOT_NVGPRS + ((reg - 14) * 8)(r1)
1175 reg = reg + 1
1176 .endr
1177
1178 reg = 14
1179 .rept 18
1180 ld reg, __VCPU_GPR(reg)(r3)
1181 reg = reg + 1
1182 .endr
1183
1184 mfmsr r10
1185 std r10, HSTATE_HOST_MSR(r13)
1186
1187 mr r4, r3
1188 b fast_guest_entry_c
1189guest_exit_short_path:
1190
1191 li r0, KVM_GUEST_MODE_NONE
1192 stb r0, HSTATE_IN_GUEST(r13)
1193
1194 reg = 14
1195 .rept 18
1196 std reg, __VCPU_GPR(reg)(r9)
1197 reg = reg + 1
1198 .endr
1199
1200 reg = 14
1201 .rept 18
1202 ld reg, STACK_SLOT_NVGPRS + ((reg - 14) * 8)(r1)
1203 reg = reg + 1
1204 .endr
1205
1206 lwz r4, SFS+8(r1)
1207 mtcr r4
1208
1209 mr r3, r12 /* trap number */
1210
1211 addi r1, r1, SFS
1212 ld r0, PPC_LR_STKOFF(r1)
1213 mtlr r0
1214
1215 /* If we are in real mode, do a rfid to get back to the caller */
1216 mfmsr r4
1217 andi. r5, r4, MSR_IR
1218 bnelr
1219 rldicl r5, r4, 64 - MSR_TS_S_LG, 62 /* extract TS field */
1220 mtspr SPRN_SRR0, r0
1221 ld r10, HSTATE_HOST_MSR(r13)
1222 rldimi r10, r5, MSR_TS_S_LG, 63 - MSR_TS_T_LG
1223 mtspr SPRN_SRR1, r10
1224 RFI_TO_KERNEL
1225 b .
1226
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001227secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001228 li r12, 0
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001229 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001230 cmpdi r4, 0
1231 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001232 stw r12, VCPU_TRAP(r4)
1233#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001234 addi r3, r4, VCPU_TB_RMEXIT
1235 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001236#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100123711: b kvmhv_switch_to_host
1238
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001239no_switch_exit:
1240 HMT_MEDIUM
1241 li r12, 0
1242 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001243hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001244 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000124512: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001246 mr r9, r4
1247#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001248 addi r3, r4, VCPU_TB_RMEXIT
1249 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001250#endif
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001251 b guest_bypass
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001252
Paul Mackerrasde56a942011-06-29 00:21:34 +00001253/******************************************************************************
1254 * *
1255 * Exit code *
1256 * *
1257 *****************************************************************************/
1258
1259/*
1260 * We come here from the first-level interrupt handlers.
1261 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301262 .globl kvmppc_interrupt_hv
1263kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001264 /*
1265 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001266 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001267 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001268 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001269 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001270 * guest R13 saved in SPRN_SCRATCH0
1271 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001272 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001273 lbz r9, HSTATE_IN_GUEST(r13)
1274 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1275 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301276#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1277 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001278 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301279 beq kvmppc_interrupt_pr
1280#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001281 /* We're now back in the host but in guest MMU context */
1282 li r9, KVM_GUEST_MODE_HOST_HV
1283 stb r9, HSTATE_IN_GUEST(r13)
1284
Paul Mackerrasde56a942011-06-29 00:21:34 +00001285 ld r9, HSTATE_KVM_VCPU(r13)
1286
1287 /* Save registers */
1288
Michael Neulingc75df6f2012-06-25 13:33:10 +00001289 std r0, VCPU_GPR(R0)(r9)
1290 std r1, VCPU_GPR(R1)(r9)
1291 std r2, VCPU_GPR(R2)(r9)
1292 std r3, VCPU_GPR(R3)(r9)
1293 std r4, VCPU_GPR(R4)(r9)
1294 std r5, VCPU_GPR(R5)(r9)
1295 std r6, VCPU_GPR(R6)(r9)
1296 std r7, VCPU_GPR(R7)(r9)
1297 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001298 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001299 std r0, VCPU_GPR(R9)(r9)
1300 std r10, VCPU_GPR(R10)(r9)
1301 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001302 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001303 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001304 /* CR is in the high half of r12 */
1305 srdi r4, r12, 32
Paul Mackerrasfd0944b2018-10-08 16:30:58 +11001306 std r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001307BEGIN_FTR_SECTION
1308 ld r3, HSTATE_CFAR(r13)
1309 std r3, VCPU_CFAR(r9)
1310END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001311BEGIN_FTR_SECTION
1312 ld r4, HSTATE_PPR(r13)
1313 std r4, VCPU_PPR(r9)
1314END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001315
1316 /* Restore R1/R2 so we can handle faults */
1317 ld r1, HSTATE_HOST_R1(r13)
1318 ld r2, PACATOC(r13)
1319
1320 mfspr r10, SPRN_SRR0
1321 mfspr r11, SPRN_SRR1
1322 std r10, VCPU_SRR0(r9)
1323 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001324 /* trap is in the low half of r12, clear CR from the high half */
1325 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001326 andi. r0, r12, 2 /* need to read HSRR0/1? */
1327 beq 1f
1328 mfspr r10, SPRN_HSRR0
1329 mfspr r11, SPRN_HSRR1
1330 clrrdi r12, r12, 2
13311: std r10, VCPU_PC(r9)
1332 std r11, VCPU_MSR(r9)
1333
1334 GET_SCRATCH0(r3)
1335 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001336 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001337 std r4, VCPU_LR(r9)
1338
Paul Mackerrasde56a942011-06-29 00:21:34 +00001339 stw r12,VCPU_TRAP(r9)
1340
Paul Mackerras8b24e692017-06-26 15:45:51 +10001341 /*
1342 * Now that we have saved away SRR0/1 and HSRR0/1,
1343 * interrupts are recoverable in principle, so set MSR_RI.
1344 * This becomes important for relocation-on interrupts from
1345 * the guest, which we can get in radix mode on POWER9.
1346 */
1347 li r0, MSR_RI
1348 mtmsrd r0, 1
1349
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001350#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1351 addi r3, r9, VCPU_TB_RMINTR
1352 mr r4, r9
1353 bl kvmhv_accumulate_time
1354 ld r5, VCPU_GPR(R5)(r9)
1355 ld r6, VCPU_GPR(R6)(r9)
1356 ld r7, VCPU_GPR(R7)(r9)
1357 ld r8, VCPU_GPR(R8)(r9)
1358#endif
1359
Paul Mackerras4a157d62014-12-03 13:30:39 +11001360 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001361 if this is an HEI (HV emulation interrupt, e40) */
1362 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001363 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001364 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1365 bne 11f
1366 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100136711: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001368
1369 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001370#ifdef CONFIG_RELOCATABLE
1371 ld r3, HSTATE_SCRATCH1(r13)
1372 mtctr r3
1373#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001374 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001375#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001376 mfxer r4
1377 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001378 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001379
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001380 /* Save more register state */
1381 mfdar r3
1382 mfdsisr r4
1383 std r3, VCPU_DAR(r9)
1384 stw r4, VCPU_DSISR(r9)
1385
1386 /* If this is a page table miss then see if it's theirs or ours */
1387 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1388 beq kvmppc_hdsi
1389 std r3, VCPU_FAULT_DAR(r9)
1390 stw r4, VCPU_FAULT_DSISR(r9)
1391 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1392 beq kvmppc_hisi
1393
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001394#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1395 /* For softpatch interrupt, go off and do TM instruction emulation */
1396 cmpwi r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
1397 beq kvmppc_tm_emul
1398#endif
1399
Paul Mackerrasde56a942011-06-29 00:21:34 +00001400 /* See if this is a leftover HDEC interrupt */
1401 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1402 bne 2f
1403 mfspr r3,SPRN_HDEC
Paul Mackerrasa4faf2e2017-08-25 19:52:12 +10001404 EXTEND_HDEC(r3)
1405 cmpdi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001406 mr r4,r9
1407 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000014082:
Paul Mackerras697d3892011-12-12 12:36:37 +00001409 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001410 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1411 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001412
Paul Mackerras66feed62015-03-28 14:21:12 +11001413 /* Hypervisor doorbell - exit only if host IPI flag set */
1414 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1415 bne 3f
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001416BEGIN_FTR_SECTION
1417 PPC_MSGSYNC
Nicholas Piggin2cde3712017-10-10 20:18:28 +10001418 lwsync
Paul Mackerras360cae32018-10-08 16:31:04 +11001419 /* always exit if we're running a nested guest */
1420 ld r0, VCPU_NESTED(r9)
1421 cmpdi r0, 0
1422 bne guest_exit_cont
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001423END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11001424 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301425 cmpwi r0, 0
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001426 beq maybe_reenter_guest
Paul Mackerras66feed62015-03-28 14:21:12 +11001427 b guest_exit_cont
14283:
Paul Mackerras769377f2017-02-15 14:30:17 +11001429 /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1430 cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1431 bne 14f
1432 mfspr r3, SPRN_HFSCR
1433 std r3, VCPU_HFSCR(r9)
1434 b guest_exit_cont
143514:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001436 /* External interrupt ? */
1437 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001438 beq kvmppc_guest_external
Paul Mackerras43ff3f62018-01-11 14:31:43 +11001439 /* See if it is a machine check */
1440 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1441 beq machine_check_realmode
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001442 /* Or a hypervisor maintenance interrupt */
1443 cmpwi r12, BOOK3S_INTERRUPT_HMI
1444 beq hmi_realmode
1445
1446guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
1447
Paul Mackerras43ff3f62018-01-11 14:31:43 +11001448#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1449 addi r3, r9, VCPU_TB_RMEXIT
1450 mr r4, r9
1451 bl kvmhv_accumulate_time
1452#endif
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001453#ifdef CONFIG_KVM_XICS
1454 /* We are exiting, pull the VP from the XIVE */
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001455 lbz r0, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001456 cmpwi cr0, r0, 0
1457 beq 1f
1458 li r7, TM_SPC_PULL_OS_CTX
1459 li r6, TM_QW1_OS
1460 mfmsr r0
Benjamin Herrenschmidt2662efd2018-01-12 13:37:14 +11001461 andi. r0, r0, MSR_DR /* in real mode? */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001462 beq 2f
1463 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1464 cmpldi cr0, r10, 0
1465 beq 1f
1466 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001467 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001468 lwzx r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001469 /* Second load to recover the context state (Words 0 and 1) */
1470 ldx r11, r6, r10
1471 b 3f
14722: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1473 cmpldi cr0, r10, 0
1474 beq 1f
1475 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001476 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001477 lwzcix r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001478 /* Second load to recover the context state (Words 0 and 1) */
1479 ldcix r11, r6, r10
14803: std r11, VCPU_XIVE_SAVED_STATE(r9)
1481 /* Fixup some of the state for the next load */
1482 li r10, 0
1483 li r0, 0xff
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001484 stb r10, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001485 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1486 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001487 eieio
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100014881:
1489#endif /* CONFIG_KVM_XICS */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001490
Michael Ellermanaf2e8c62019-11-13 21:05:44 +11001491 /*
1492 * Possibly flush the link stack here, before we do a blr in
1493 * guest_exit_short_path.
1494 */
14951: nop
1496 patch_site 1b patch__call_kvm_flush_link_stack
1497
Paul Mackerras95a64322018-10-08 16:30:55 +11001498 /* If we came in through the P9 short path, go back out to C now */
1499 lwz r0, STACK_SLOT_SHORT_PATH(r1)
1500 cmpwi r0, 0
1501 bne guest_exit_short_path
1502
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001503 /* For hash guest, read the guest SLB and save it away */
1504 ld r5, VCPU_KVM(r9)
1505 lbz r0, KVM_RADIX(r5)
1506 li r5, 0
1507 cmpwi r0, 0
1508 bne 3f /* for radix, save 0 entries */
1509 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1510 mtctr r0
1511 li r6,0
1512 addi r7,r9,VCPU_SLB
15131: slbmfee r8,r6
1514 andis. r0,r8,SLB_ESID_V@h
1515 beq 2f
1516 add r8,r8,r6 /* put index in */
1517 slbmfev r3,r6
1518 std r8,VCPU_SLB_E(r7)
1519 std r3,VCPU_SLB_V(r7)
1520 addi r7,r7,VCPU_SLB_SIZE
1521 addi r5,r5,1
15222: addi r6,r6,1
1523 bdnz 1b
1524 /* Finally clear out the SLB */
1525 li r0,0
1526 slbmte r0,r0
1527 slbia
1528 ptesync
15293: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001530
Paul Mackerrascda4a142018-03-22 09:48:54 +11001531 /* load host SLB entries */
1532BEGIN_MMU_FTR_SECTION
1533 b 0f
1534END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
1535 ld r8,PACA_SLBSHADOWPTR(r13)
1536
1537 .rept SLB_NUM_BOLTED
1538 li r3, SLBSHADOW_SAVEAREA
1539 LDX_BE r5, r8, r3
1540 addi r3, r3, 8
1541 LDX_BE r6, r8, r3
1542 andis. r7,r5,SLB_ESID_V@h
1543 beq 1f
1544 slbmte r6,r5
15451: addi r8,r8,16
1546 .endr
15470:
1548
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001549guest_bypass:
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001550 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001551
1552 /* Save DEC */
1553 /* Do this before kvmhv_commence_exit so we know TB is guest TB */
1554 ld r3, HSTATE_KVM_VCORE(r13)
1555 mfspr r5,SPRN_DEC
1556 mftb r6
1557 /* On P9, if the guest has large decr enabled, don't sign extend */
1558BEGIN_FTR_SECTION
1559 ld r4, VCORE_LPCR(r3)
1560 andis. r4, r4, LPCR_LD@h
1561 bne 16f
1562END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1563 extsw r5,r5
156416: add r5,r5,r6
1565 /* r5 is a guest timebase value here, convert to host TB */
1566 ld r4,VCORE_TB_OFFSET_APPL(r3)
1567 subf r5,r4,r5
1568 std r5,VCPU_DEC_EXPIRES(r9)
1569
Paul Mackerras6af27c82015-03-28 14:21:10 +11001570 /* Increment exit count, poke other threads to exit */
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001571 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001572 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001573 nop
1574 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001575
Paul Mackerrasec257162015-06-24 21:18:03 +10001576 /* Stop others sending VCPU interrupts to this physical CPU */
1577 li r0, -1
1578 stw r0, VCPU_CPU(r9)
1579 stw r0, VCPU_THREAD_CPU(r9)
1580
Paul Mackerrasde56a942011-06-29 00:21:34 +00001581 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001582 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001583 stw r6,VCPU_CTRL(r9)
1584 andi. r0,r6,1
1585 bne 4f
1586 ori r6,r6,1
1587 mtspr SPRN_CTRLT,r6
15884:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001589 /*
1590 * Save the guest PURR/SPURR
1591 */
1592 mfspr r5,SPRN_PURR
1593 mfspr r6,SPRN_SPURR
1594 ld r7,VCPU_PURR(r9)
1595 ld r8,VCPU_SPURR(r9)
1596 std r5,VCPU_PURR(r9)
1597 std r6,VCPU_SPURR(r9)
1598 subf r5,r7,r5
1599 subf r6,r8,r6
1600
1601 /*
1602 * Restore host PURR/SPURR and add guest times
1603 * so that the time in the guest gets accounted.
1604 */
1605 ld r3,HSTATE_PURR(r13)
1606 ld r4,HSTATE_SPURR(r13)
1607 add r3,r3,r5
1608 add r4,r4,r6
1609 mtspr SPRN_PURR,r3
1610 mtspr SPRN_SPURR,r4
1611
Michael Neulingb005255e2014-01-08 21:25:21 +11001612BEGIN_FTR_SECTION
1613 b 8f
1614END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001615 /* Save POWER8-specific registers */
1616 mfspr r5, SPRN_IAMR
1617 mfspr r6, SPRN_PSPB
1618 mfspr r7, SPRN_FSCR
1619 std r5, VCPU_IAMR(r9)
1620 stw r6, VCPU_PSPB(r9)
1621 std r7, VCPU_FSCR(r9)
1622 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001623 mfspr r7, SPRN_TAR
1624 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001625 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001626 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001627 std r8, VCPU_EBBHR(r9)
1628 mfspr r5, SPRN_EBBRR
1629 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001630 mfspr r7, SPRN_PID
1631 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001632 std r5, VCPU_EBBRR(r9)
1633 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001634 stw r7, VCPU_GUEST_PID(r9)
1635 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001636BEGIN_FTR_SECTION
1637 mfspr r5, SPRN_TCSCR
1638 mfspr r6, SPRN_ACOP
1639 mfspr r7, SPRN_CSIGR
1640 mfspr r8, SPRN_TACR
1641 std r5, VCPU_TCSCR(r9)
1642 std r6, VCPU_ACOP(r9)
1643 std r7, VCPU_CSIGR(r9)
1644 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001645FTR_SECTION_ELSE
1646 mfspr r5, SPRN_TIDR
1647 mfspr r6, SPRN_PSSCR
1648 std r5, VCPU_TID(r9)
1649 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1650 rotldi r6, r6, 60
1651 std r6, VCPU_PSSCR(r9)
Paul Mackerras769377f2017-02-15 14:30:17 +11001652 /* Restore host HFSCR value */
1653 ld r7, STACK_SLOT_HFSCR(r1)
1654 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001655ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001656 /*
1657 * Restore various registers to 0, where non-zero values
1658 * set by the guest could disrupt the host.
1659 */
1660 li r0, 0
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001661 mtspr SPRN_PSPB, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001662 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001663BEGIN_FTR_SECTION
1664 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001665 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1666 li r0, 1
1667 sldi r0, r0, 31
1668 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001669END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11001670
Michael Ellermanc3c7470c2019-02-22 13:22:08 +11001671 /* Save and restore AMR, IAMR and UAMOR before turning on the MMU */
1672 ld r8, STACK_SLOT_IAMR(r1)
1673 mtspr SPRN_IAMR, r8
1674
16758: /* Power7 jumps back in here */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001676 mfspr r5,SPRN_AMR
1677 mfspr r6,SPRN_UAMOR
1678 std r5,VCPU_AMR(r9)
1679 std r6,VCPU_UAMOR(r9)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +11001680 ld r5,STACK_SLOT_AMR(r1)
1681 ld r6,STACK_SLOT_UAMOR(r1)
1682 mtspr SPRN_AMR, r5
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001683 mtspr SPRN_UAMOR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001684
Paul Mackerrasde56a942011-06-29 00:21:34 +00001685 /* Switch DSCR back to host value */
1686 mfspr r8, SPRN_DSCR
1687 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001688 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001689 mtspr SPRN_DSCR, r7
1690
1691 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001692 std r14, VCPU_GPR(R14)(r9)
1693 std r15, VCPU_GPR(R15)(r9)
1694 std r16, VCPU_GPR(R16)(r9)
1695 std r17, VCPU_GPR(R17)(r9)
1696 std r18, VCPU_GPR(R18)(r9)
1697 std r19, VCPU_GPR(R19)(r9)
1698 std r20, VCPU_GPR(R20)(r9)
1699 std r21, VCPU_GPR(R21)(r9)
1700 std r22, VCPU_GPR(R22)(r9)
1701 std r23, VCPU_GPR(R23)(r9)
1702 std r24, VCPU_GPR(R24)(r9)
1703 std r25, VCPU_GPR(R25)(r9)
1704 std r26, VCPU_GPR(R26)(r9)
1705 std r27, VCPU_GPR(R27)(r9)
1706 std r28, VCPU_GPR(R28)(r9)
1707 std r29, VCPU_GPR(R29)(r9)
1708 std r30, VCPU_GPR(R30)(r9)
1709 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001710
1711 /* Save SPRGs */
1712 mfspr r3, SPRN_SPRG0
1713 mfspr r4, SPRN_SPRG1
1714 mfspr r5, SPRN_SPRG2
1715 mfspr r6, SPRN_SPRG3
1716 std r3, VCPU_SPRG0(r9)
1717 std r4, VCPU_SPRG1(r9)
1718 std r5, VCPU_SPRG2(r9)
1719 std r6, VCPU_SPRG3(r9)
1720
Paul Mackerras89436332012-03-02 01:38:23 +00001721 /* save FP state */
1722 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001723 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001724
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001725#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001726/*
1727 * Branch around the call if both CPU_FTR_TM and
1728 * CPU_FTR_P9_TM_HV_ASSIST are off.
1729 */
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001730BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001731 b 91f
1732END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001733 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11001734 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001735 */
Simon Guo6f597c62018-05-23 15:01:48 +08001736 mr r3, r9
1737 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11001738 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10001739 bl kvmppc_save_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11001740 nop
Simon Guo6f597c62018-05-23 15:01:48 +08001741 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100174291:
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001743#endif
1744
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001745 /* Increment yield count if they have a VPA */
1746 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1747 cmpdi r8, 0
1748 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001749 li r4, LPPACA_YIELDCOUNT
1750 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001751 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001752 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001753 li r3, 1
1754 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000175525:
1756 /* Save PMU registers if requested */
1757 /* r8 and cr0.eq are live here */
Paul Mackerras41f4e632018-10-08 16:30:51 +11001758 mr r3, r9
1759 li r4, 1
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001760 beq 21f /* if no VPA, save PMU stuff anyway */
Paul Mackerras41f4e632018-10-08 16:30:51 +11001761 lbz r4, LPPACA_PMCINUSE(r8)
176221: bl kvmhv_save_guest_pmu
1763 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001764
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001765 /* Restore host values of some registers */
1766BEGIN_FTR_SECTION
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001767 ld r5, STACK_SLOT_CIABR(r1)
1768 ld r6, STACK_SLOT_DAWR(r1)
1769 ld r7, STACK_SLOT_DAWRX(r1)
1770 mtspr SPRN_CIABR, r5
Michael Neulingb53221e2018-03-27 15:37:22 +11001771 /*
1772 * If the DAWR doesn't work, it's ok to write these here as
1773 * this value should always be zero
1774 */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001775 mtspr SPRN_DAWR, r6
1776 mtspr SPRN_DAWRX, r7
1777END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1778BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001779 ld r5, STACK_SLOT_TID(r1)
1780 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001781 ld r7, STACK_SLOT_PID(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001782 mtspr SPRN_TIDR, r5
1783 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001784 mtspr SPRN_PID, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001785END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001786
1787#ifdef CONFIG_PPC_RADIX_MMU
1788 /*
1789 * Are we running hash or radix ?
1790 */
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001791 ld r5, VCPU_KVM(r9)
1792 lbz r0, KVM_RADIX(r5)
1793 cmpwi cr2, r0, 0
Nicholas Piggin2bf10712018-07-05 18:47:00 +10001794 beq cr2, 2f
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001795
Paul Mackerrasdf158182018-05-17 14:47:59 +10001796 /*
1797 * Radix: do eieio; tlbsync; ptesync sequence in case we
1798 * interrupted the guest between a tlbie and a ptesync.
1799 */
1800 eieio
1801 tlbsync
1802 ptesync
1803
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001804 /* Radix: Handle the case where the guest used an illegal PID */
1805 LOAD_REG_ADDR(r4, mmu_base_pid)
1806 lwz r3, VCPU_GUEST_PID(r9)
1807 lwz r5, 0(r4)
1808 cmpw cr0,r3,r5
1809 blt 2f
1810
1811 /*
1812 * Illegal PID, the HW might have prefetched and cached in the TLB
1813 * some translations for the LPID 0 / guest PID combination which
1814 * Linux doesn't know about, so we need to flush that PID out of
1815 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1816 * the right context.
1817 */
1818 li r0,0
1819 mtspr SPRN_LPID,r0
1820 isync
1821
1822 /* Then do a congruence class local flush */
1823 ld r6,VCPU_KVM(r9)
1824 lwz r0,KVM_TLB_SETS(r6)
1825 mtctr r0
1826 li r7,0x400 /* IS field = 0b01 */
1827 ptesync
1828 sldi r0,r3,32 /* RS has PID */
18291: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
1830 addi r7,r7,0x1000
1831 bdnz 1b
1832 ptesync
1833
Nicholas Piggin2bf10712018-07-05 18:47:00 +100018342:
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001835#endif /* CONFIG_PPC_RADIX_MMU */
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001836
Paul Mackerrasde56a942011-06-29 00:21:34 +00001837 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001838 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001839 * We don't have to lock against tlbies but we do
1840 * have to coordinate the hardware threads.
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001841 * Here STACK_SLOT_TRAP(r1) contains the trap number.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001842 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001843kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001844 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001845 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001846 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1847 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001848 cmpwi r3,0
1849 beq 15f
1850 HMT_LOW
185113: lbz r3,VCORE_IN_GUEST(r5)
1852 cmpwi r3,0
1853 bne 13b
1854 HMT_MEDIUM
1855 b 16f
1856
1857 /* Primary thread waits for all the secondaries to exit guest */
185815: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001859 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001860 clrldi r3,r3,56
1861 cmpw r3,r0
1862 bne 15b
1863 isync
1864
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001865 /* Did we actually switch to the guest at all? */
1866 lbz r6, VCORE_IN_GUEST(r5)
1867 cmpwi r6, 0
1868 beq 19f
1869
Paul Mackerrasde56a942011-06-29 00:21:34 +00001870 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001871 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001872BEGIN_FTR_SECTION
1873 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001874 li r8,LPID_RSVD /* switch to reserved LPID */
1875 mtspr SPRN_LPID,r8
1876 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001877 mtspr SPRN_SDR1,r6 /* switch to host page table */
1878END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001879 mtspr SPRN_LPID,r7
1880 isync
1881
Michael Neulingb005255e2014-01-08 21:25:21 +11001882BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001883 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001884 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001885 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001886 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001887 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001888 /* clear DPDES so we don't get guest doorbells in the host */
1889 li r8, 0
1890 mtspr SPRN_DPDES, r8
1891END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1892
Paul Mackerrasde56a942011-06-29 00:21:34 +00001893 /* Subtract timebase offset from timebase */
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001894 ld r8, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001895 cmpdi r8,0
1896 beq 17f
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001897 li r0, 0
1898 std r0, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001899 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001900 subf r8,r8,r6
1901 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1902 mftb r7 /* check if lower 24 bits overflowed */
1903 clrldi r6,r6,40
1904 clrldi r7,r7,40
1905 cmpld r7,r6
1906 bge 17f
1907 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1908 mtspr SPRN_TBU40,r8
1909
Paul Mackerrasdf709a22018-10-08 16:30:52 +1100191017:
1911 /*
1912 * If this is an HMI, we called kvmppc_realmode_hmi_handler
1913 * above, which may or may not have already called
1914 * kvmppc_subcore_exit_guest. Fortunately, all that
1915 * kvmppc_subcore_exit_guest does is clear a flag, so calling
1916 * it again here is benign even if kvmppc_realmode_hmi_handler
1917 * has already called it.
1918 */
1919 bl kvmppc_subcore_exit_guest
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301920 nop
192130: ld r5,HSTATE_KVM_VCORE(r13)
1922 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1923
Paul Mackerrasde56a942011-06-29 00:21:34 +00001924 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301925 ld r0, VCORE_PCR(r5)
Jordan Niethe13c7bb32019-09-17 10:46:05 +10001926 LOAD_REG_IMMEDIATE(r6, PCR_MASK)
1927 cmpld r0, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001928 beq 18f
Jordan Niethe13c7bb32019-09-17 10:46:05 +10001929 mtspr SPRN_PCR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000193018:
1931 /* Signal secondary CPUs to continue */
Jordan Niethe7fe4e112019-10-04 12:53:17 +10001932 li r0, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00001933 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000193419: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001935 mtspr SPRN_HDEC,r8
1936
Paul Mackerrasc0101502017-10-19 14:11:23 +1100193716:
1938BEGIN_FTR_SECTION
1939 /* On POWER9 with HPT-on-radix we need to wait for all other threads */
1940 ld r3, HSTATE_SPLIT_MODE(r13)
1941 cmpdi r3, 0
1942 beq 47f
1943 lwz r8, KVM_SPLIT_DO_RESTORE(r3)
1944 cmpwi r8, 0
1945 beq 47f
Paul Mackerrasc0101502017-10-19 14:11:23 +11001946 bl kvmhv_p9_restore_lpcr
1947 nop
Paul Mackerrasc0101502017-10-19 14:11:23 +11001948 b 48f
194947:
1950END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1951 ld r8,KVM_HOST_LPCR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001952 mtspr SPRN_LPCR,r8
1953 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100195448:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001955#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1956 /* Finish timing, if we have a vcpu */
1957 ld r4, HSTATE_KVM_VCPU(r13)
1958 cmpdi r4, 0
1959 li r3, 0
1960 beq 2f
1961 bl kvmhv_accumulate_time
19622:
1963#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001964 /* Unset guest mode */
1965 li r0, KVM_GUEST_MODE_NONE
1966 stb r0, HSTATE_IN_GUEST(r13)
1967
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001968 lwz r12, STACK_SLOT_TRAP(r1) /* return trap # in r12 */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001969 ld r0, SFS+PPC_LR_STKOFF(r1)
1970 addi r1, r1, SFS
Paul Mackerras218309b2013-09-06 13:23:44 +10001971 mtlr r0
1972 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001973
Michael Ellermanaf2e8c62019-11-13 21:05:44 +11001974.balign 32
1975.global kvm_flush_link_stack
1976kvm_flush_link_stack:
1977 /* Save LR into r0 */
1978 mflr r0
1979
1980 /* Flush the link stack. On Power8 it's up to 32 entries in size. */
1981 .rept 32
1982 bl .+4
1983 .endr
1984
1985 /* And on Power9 it's up to 64. */
1986BEGIN_FTR_SECTION
1987 .rept 32
1988 bl .+4
1989 .endr
1990END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1991
1992 /* Restore LR */
1993 mtlr r0
1994 blr
1995
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001996kvmppc_guest_external:
1997 /* External interrupt, first check for host_ipi. If this is
1998 * set, we know the host wants us out so let's do it now
1999 */
2000 bl kvmppc_read_intr
2001
2002 /*
2003 * Restore the active volatile registers after returning from
2004 * a C function.
2005 */
2006 ld r9, HSTATE_KVM_VCPU(r13)
2007 li r12, BOOK3S_INTERRUPT_EXTERNAL
2008
2009 /*
2010 * kvmppc_read_intr return codes:
2011 *
2012 * Exit to host (r3 > 0)
2013 * 1 An interrupt is pending that needs to be handled by the host
2014 * Exit guest and return to host by branching to guest_exit_cont
2015 *
2016 * 2 Passthrough that needs completion in the host
2017 * Exit guest and return to host by branching to guest_exit_cont
2018 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
2019 * to indicate to the host to complete handling the interrupt
2020 *
2021 * Before returning to guest, we check if any CPU is heading out
2022 * to the host and if so, we head out also. If no CPUs are heading
2023 * check return values <= 0.
2024 *
2025 * Return to guest (r3 <= 0)
2026 * 0 No external interrupt is pending
2027 * -1 A guest wakeup IPI (which has now been cleared)
2028 * In either case, we return to guest to deliver any pending
2029 * guest interrupts.
2030 *
2031 * -2 A PCI passthrough external interrupt was handled
2032 * (interrupt was delivered directly to guest)
2033 * Return to guest to deliver any pending guest interrupts.
2034 */
2035
2036 cmpdi r3, 1
2037 ble 1f
2038
2039 /* Return code = 2 */
2040 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
2041 stw r12, VCPU_TRAP(r9)
2042 b guest_exit_cont
2043
20441: /* Return code <= 1 */
2045 cmpdi r3, 0
2046 bgt guest_exit_cont
2047
2048 /* Return code <= 0 */
2049maybe_reenter_guest:
2050 ld r5, HSTATE_KVM_VCORE(r13)
2051 lwz r0, VCORE_ENTRY_EXIT(r5)
2052 cmpwi r0, 0x100
2053 mr r4, r9
2054 blt deliver_guest_interrupt
2055 b guest_exit_cont
2056
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002057#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2058/*
2059 * Softpatch interrupt for transactional memory emulation cases
2060 * on POWER9 DD2.2. This is early in the guest exit path - we
2061 * haven't saved registers or done a treclaim yet.
2062 */
2063kvmppc_tm_emul:
2064 /* Save instruction image in HEIR */
2065 mfspr r3, SPRN_HEIR
2066 stw r3, VCPU_HEIR(r9)
2067
2068 /*
2069 * The cases we want to handle here are those where the guest
2070 * is in real suspend mode and is trying to transition to
2071 * transactional mode.
2072 */
2073 lbz r0, HSTATE_FAKE_SUSPEND(r13)
2074 cmpwi r0, 0 /* keep exiting guest if in fake suspend */
2075 bne guest_exit_cont
2076 rldicl r3, r11, 64 - MSR_TS_S_LG, 62
2077 cmpwi r3, 1 /* or if not in suspend state */
2078 bne guest_exit_cont
2079
2080 /* Call C code to do the emulation */
2081 mr r3, r9
2082 bl kvmhv_p9_tm_emulation_early
2083 nop
2084 ld r9, HSTATE_KVM_VCPU(r13)
2085 li r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
2086 cmpwi r3, 0
2087 beq guest_exit_cont /* continue exiting if not handled */
2088 ld r10, VCPU_PC(r9)
2089 ld r11, VCPU_MSR(r9)
2090 b fast_interrupt_c_return /* go back to guest if handled */
2091#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
2092
Paul Mackerras697d3892011-12-12 12:36:37 +00002093/*
2094 * Check whether an HDSI is an HPTE not found fault or something else.
2095 * If it is an HPTE not found fault that is due to the guest accessing
2096 * a page that they have mapped but which we have paged out, then
2097 * we continue on with the guest exit path. In all other cases,
2098 * reflect the HDSI to the guest as a DSI.
2099 */
2100kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002101 ld r3, VCPU_KVM(r9)
2102 lbz r0, KVM_RADIX(r3)
Paul Mackerras697d3892011-12-12 12:36:37 +00002103 mfspr r4, SPRN_HDAR
2104 mfspr r6, SPRN_HDSISR
Michael Neulinge001fa72017-09-15 15:26:14 +10002105BEGIN_FTR_SECTION
2106 /* Look for DSISR canary. If we find it, retry instruction */
2107 cmpdi r6, 0x7fff
2108 beq 6f
2109END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2110 cmpwi r0, 0
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002111 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00002112 /* HPTE not found fault or protection fault? */
2113 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00002114 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002115 andi. r0, r11, MSR_DR /* data relocation enabled? */
2116 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002117BEGIN_FTR_SECTION
2118 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2119 b 4f
2120END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00002121 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002122 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002123 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
2124 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000021254: std r4, VCPU_FAULT_DAR(r9)
2126 stw r6, VCPU_FAULT_DSISR(r9)
2127
2128 /* Search the hash table. */
2129 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002130 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002131 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00002132 ld r9, HSTATE_KVM_VCPU(r13)
2133 ld r10, VCPU_PC(r9)
2134 ld r11, VCPU_MSR(r9)
2135 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
2136 cmpdi r3, 0 /* retry the instruction */
2137 beq 6f
2138 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002139 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00002140 cmpdi r3, -2 /* MMIO emulation; need instr word */
2141 beq 2f
2142
Paul Mackerrascf29b212015-10-27 16:10:20 +11002143 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00002144 ld r4, VCPU_FAULT_DAR(r9)
2145 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110021461: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00002147 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110021487: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00002149 mtspr SPRN_SRR0, r10
2150 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002151 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002152 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002153fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000021546: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10002155 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00002156 mtctr r7
2157 mtxer r8
2158 mr r4, r9
2159 b fast_guest_return
2160
21613: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
2162 ld r5, KVM_VRMA_SLB_V(r5)
2163 b 4b
2164
2165 /* If this is for emulated MMIO, load the instruction word */
21662: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
2167
2168 /* Set guest mode to 'jump over instruction' so if lwz faults
2169 * we'll just continue at the next IP. */
2170 li r0, KVM_GUEST_MODE_SKIP
2171 stb r0, HSTATE_IN_GUEST(r13)
2172
2173 /* Do the access with MSR:DR enabled */
2174 mfmsr r3
2175 ori r4, r3, MSR_DR /* Enable paging for data */
2176 mtmsrd r4
2177 lwz r8, 0(r10)
2178 mtmsrd r3
2179
2180 /* Store the result */
2181 stw r8, VCPU_LAST_INST(r9)
2182
2183 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10002184 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00002185 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002186 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00002187
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002188.Lradix_hdsi:
2189 std r4, VCPU_FAULT_DAR(r9)
2190 stw r6, VCPU_FAULT_DSISR(r9)
2191.Lradix_hisi:
2192 mfspr r5, SPRN_ASDR
2193 std r5, VCPU_FAULT_GPA(r9)
2194 b guest_exit_cont
2195
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002196/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00002197 * Similarly for an HISI, reflect it to the guest as an ISI unless
2198 * it is an HPTE not found fault for a page that we have paged out.
2199 */
2200kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002201 ld r3, VCPU_KVM(r9)
2202 lbz r0, KVM_RADIX(r3)
2203 cmpwi r0, 0
2204 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002205 andis. r0, r11, SRR1_ISI_NOPT@h
2206 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002207 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
2208 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002209BEGIN_FTR_SECTION
2210 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2211 b 4f
2212END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00002213 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002214 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002215 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
2216 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000022174:
2218 /* Search the hash table. */
2219 mr r3, r9 /* vcpu pointer */
2220 mr r4, r10
2221 mr r6, r11
2222 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002223 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00002224 ld r9, HSTATE_KVM_VCPU(r13)
2225 ld r10, VCPU_PC(r9)
2226 ld r11, VCPU_MSR(r9)
2227 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2228 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002229 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002230 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002231 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00002232
Paul Mackerrascf29b212015-10-27 16:10:20 +11002233 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002234 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110022351: li r0, BOOK3S_INTERRUPT_INST_STORAGE
22367: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00002237 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002238 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002239 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002240 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002241
22423: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2243 ld r5, KVM_VRMA_SLB_V(r6)
2244 b 4b
2245
2246/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002247 * Try to handle an hcall in real mode.
2248 * Returns to the guest if we handle it, or continues on up to
2249 * the kernel if we can't (i.e. if we don't have a handler for
2250 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002251 *
2252 * r5 - r8 contain hcall args,
2253 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002254 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002255hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00002256 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002257 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08002258 /* sc 1 from userspace - reflect to guest syscall */
2259 bne sc_1_fast_return
Paul Mackerras360cae32018-10-08 16:31:04 +11002260 /* sc 1 from nested guest - give it to L1 to handle */
2261 ld r0, VCPU_NESTED(r9)
2262 cmpdi r0, 0
2263 bne guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002264 clrrdi r3,r3,2
2265 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002266 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002267 /* See if this hcall is enabled for in-kernel handling */
2268 ld r4, VCPU_KVM(r9)
2269 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2270 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2271 add r4, r4, r0
2272 ld r0, KVM_ENABLED_HCALLS(r4)
2273 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2274 srd r0, r0, r4
2275 andi. r0, r0, 1
2276 beq guest_exit_cont
2277 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002278 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10002279 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002280 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002281 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10002282 add r12,r3,r4
2283 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002284 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002285 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002286 bctrl
2287 cmpdi r3,H_TOO_HARD
2288 beq hcall_real_fallback
2289 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00002290 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002291 ld r10,VCPU_PC(r4)
2292 ld r11,VCPU_MSR(r4)
2293 b fast_guest_return
2294
Liu Ping Fan27025a62013-11-19 14:12:48 +08002295sc_1_fast_return:
2296 mtspr SPRN_SRR0,r10
2297 mtspr SPRN_SRR1,r11
2298 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11002299 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08002300 mr r4,r9
2301 b fast_guest_return
2302
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002303 /* We've attempted a real mode hcall, but it's punted it back
2304 * to userspace. We need to restore some clobbered volatiles
2305 * before resuming the pass-it-to-qemu path */
2306hcall_real_fallback:
2307 li r12,BOOK3S_INTERRUPT_SYSCALL
2308 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002309
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002310 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002311
2312 .globl hcall_real_table
2313hcall_real_table:
2314 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002315 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2316 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2317 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10002318 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2319 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002320 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002321#ifdef CONFIG_SPAPR_TCE_IOMMU
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002322 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002323 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002324#else
2325 .long 0 /* 0x1c */
2326 .long 0 /* 0x20 */
2327#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002328 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002329 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Suraj Jitindar Singheadfb1c2019-03-22 17:05:45 +11002330 .long DOTSYM(kvmppc_rm_h_page_init) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002331 .long 0 /* 0x30 */
2332 .long 0 /* 0x34 */
2333 .long 0 /* 0x38 */
2334 .long 0 /* 0x3c */
2335 .long 0 /* 0x40 */
2336 .long 0 /* 0x44 */
2337 .long 0 /* 0x48 */
2338 .long 0 /* 0x4c */
2339 .long 0 /* 0x50 */
2340 .long 0 /* 0x54 */
2341 .long 0 /* 0x58 */
2342 .long 0 /* 0x5c */
2343 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002344#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002345 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2346 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2347 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002348 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002349 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002350#else
2351 .long 0 /* 0x64 - H_EOI */
2352 .long 0 /* 0x68 - H_CPPR */
2353 .long 0 /* 0x6c - H_IPI */
2354 .long 0 /* 0x70 - H_IPOLL */
2355 .long 0 /* 0x74 - H_XIRR */
2356#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002357 .long 0 /* 0x78 */
2358 .long 0 /* 0x7c */
2359 .long 0 /* 0x80 */
2360 .long 0 /* 0x84 */
2361 .long 0 /* 0x88 */
2362 .long 0 /* 0x8c */
2363 .long 0 /* 0x90 */
2364 .long 0 /* 0x94 */
2365 .long 0 /* 0x98 */
2366 .long 0 /* 0x9c */
2367 .long 0 /* 0xa0 */
2368 .long 0 /* 0xa4 */
2369 .long 0 /* 0xa8 */
2370 .long 0 /* 0xac */
2371 .long 0 /* 0xb0 */
2372 .long 0 /* 0xb4 */
2373 .long 0 /* 0xb8 */
2374 .long 0 /* 0xbc */
2375 .long 0 /* 0xc0 */
2376 .long 0 /* 0xc4 */
2377 .long 0 /* 0xc8 */
2378 .long 0 /* 0xcc */
2379 .long 0 /* 0xd0 */
2380 .long 0 /* 0xd4 */
2381 .long 0 /* 0xd8 */
2382 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002383 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002384 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002385 .long 0 /* 0xe8 */
2386 .long 0 /* 0xec */
2387 .long 0 /* 0xf0 */
2388 .long 0 /* 0xf4 */
2389 .long 0 /* 0xf8 */
2390 .long 0 /* 0xfc */
2391 .long 0 /* 0x100 */
2392 .long 0 /* 0x104 */
2393 .long 0 /* 0x108 */
2394 .long 0 /* 0x10c */
2395 .long 0 /* 0x110 */
2396 .long 0 /* 0x114 */
2397 .long 0 /* 0x118 */
2398 .long 0 /* 0x11c */
2399 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002400 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002401 .long 0 /* 0x128 */
2402 .long 0 /* 0x12c */
2403 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002404 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002405#ifdef CONFIG_SPAPR_TCE_IOMMU
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002406 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002407 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002408#else
2409 .long 0 /* 0x138 */
2410 .long 0 /* 0x13c */
2411#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002412 .long 0 /* 0x140 */
2413 .long 0 /* 0x144 */
2414 .long 0 /* 0x148 */
2415 .long 0 /* 0x14c */
2416 .long 0 /* 0x150 */
2417 .long 0 /* 0x154 */
2418 .long 0 /* 0x158 */
2419 .long 0 /* 0x15c */
2420 .long 0 /* 0x160 */
2421 .long 0 /* 0x164 */
2422 .long 0 /* 0x168 */
2423 .long 0 /* 0x16c */
2424 .long 0 /* 0x170 */
2425 .long 0 /* 0x174 */
2426 .long 0 /* 0x178 */
2427 .long 0 /* 0x17c */
2428 .long 0 /* 0x180 */
2429 .long 0 /* 0x184 */
2430 .long 0 /* 0x188 */
2431 .long 0 /* 0x18c */
2432 .long 0 /* 0x190 */
2433 .long 0 /* 0x194 */
2434 .long 0 /* 0x198 */
2435 .long 0 /* 0x19c */
2436 .long 0 /* 0x1a0 */
2437 .long 0 /* 0x1a4 */
2438 .long 0 /* 0x1a8 */
2439 .long 0 /* 0x1ac */
2440 .long 0 /* 0x1b0 */
2441 .long 0 /* 0x1b4 */
2442 .long 0 /* 0x1b8 */
2443 .long 0 /* 0x1bc */
2444 .long 0 /* 0x1c0 */
2445 .long 0 /* 0x1c4 */
2446 .long 0 /* 0x1c8 */
2447 .long 0 /* 0x1cc */
2448 .long 0 /* 0x1d0 */
2449 .long 0 /* 0x1d4 */
2450 .long 0 /* 0x1d8 */
2451 .long 0 /* 0x1dc */
2452 .long 0 /* 0x1e0 */
2453 .long 0 /* 0x1e4 */
2454 .long 0 /* 0x1e8 */
2455 .long 0 /* 0x1ec */
2456 .long 0 /* 0x1f0 */
2457 .long 0 /* 0x1f4 */
2458 .long 0 /* 0x1f8 */
2459 .long 0 /* 0x1fc */
2460 .long 0 /* 0x200 */
2461 .long 0 /* 0x204 */
2462 .long 0 /* 0x208 */
2463 .long 0 /* 0x20c */
2464 .long 0 /* 0x210 */
2465 .long 0 /* 0x214 */
2466 .long 0 /* 0x218 */
2467 .long 0 /* 0x21c */
2468 .long 0 /* 0x220 */
2469 .long 0 /* 0x224 */
2470 .long 0 /* 0x228 */
2471 .long 0 /* 0x22c */
2472 .long 0 /* 0x230 */
2473 .long 0 /* 0x234 */
2474 .long 0 /* 0x238 */
2475 .long 0 /* 0x23c */
2476 .long 0 /* 0x240 */
2477 .long 0 /* 0x244 */
2478 .long 0 /* 0x248 */
2479 .long 0 /* 0x24c */
2480 .long 0 /* 0x250 */
2481 .long 0 /* 0x254 */
2482 .long 0 /* 0x258 */
2483 .long 0 /* 0x25c */
2484 .long 0 /* 0x260 */
2485 .long 0 /* 0x264 */
2486 .long 0 /* 0x268 */
2487 .long 0 /* 0x26c */
2488 .long 0 /* 0x270 */
2489 .long 0 /* 0x274 */
2490 .long 0 /* 0x278 */
2491 .long 0 /* 0x27c */
2492 .long 0 /* 0x280 */
2493 .long 0 /* 0x284 */
2494 .long 0 /* 0x288 */
2495 .long 0 /* 0x28c */
2496 .long 0 /* 0x290 */
2497 .long 0 /* 0x294 */
2498 .long 0 /* 0x298 */
2499 .long 0 /* 0x29c */
2500 .long 0 /* 0x2a0 */
2501 .long 0 /* 0x2a4 */
2502 .long 0 /* 0x2a8 */
2503 .long 0 /* 0x2ac */
2504 .long 0 /* 0x2b0 */
2505 .long 0 /* 0x2b4 */
2506 .long 0 /* 0x2b8 */
2507 .long 0 /* 0x2bc */
2508 .long 0 /* 0x2c0 */
2509 .long 0 /* 0x2c4 */
2510 .long 0 /* 0x2c8 */
2511 .long 0 /* 0x2cc */
2512 .long 0 /* 0x2d0 */
2513 .long 0 /* 0x2d4 */
2514 .long 0 /* 0x2d8 */
2515 .long 0 /* 0x2dc */
2516 .long 0 /* 0x2e0 */
2517 .long 0 /* 0x2e4 */
2518 .long 0 /* 0x2e8 */
2519 .long 0 /* 0x2ec */
2520 .long 0 /* 0x2f0 */
2521 .long 0 /* 0x2f4 */
2522 .long 0 /* 0x2f8 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002523#ifdef CONFIG_KVM_XICS
2524 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2525#else
2526 .long 0 /* 0x2fc - H_XIRR_X*/
2527#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002528 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002529 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002530hcall_real_table_end:
2531
Paul Mackerras8563bf52014-01-08 21:25:29 +11002532_GLOBAL(kvmppc_h_set_xdabr)
Paul Mackerras4bad7772018-10-08 16:31:06 +11002533EXPORT_SYMBOL_GPL(kvmppc_h_set_xdabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002534 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2535 beq 6f
2536 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2537 andc. r0, r5, r0
2538 beq 3f
25396: li r3, H_PARAMETER
2540 blr
2541
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002542_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras4bad7772018-10-08 16:31:06 +11002543EXPORT_SYMBOL_GPL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002544 li r5, DABRX_USER | DABRX_KERNEL
25453:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002546BEGIN_FTR_SECTION
2547 b 2f
2548END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002549 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002550 stw r5, VCPU_DABRX(r3)
2551 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002552 /* Work around P7 bug where DABR can get corrupted on mtspr */
25531: mtspr SPRN_DABR,r4
2554 mfspr r5, SPRN_DABR
2555 cmpd r4, r5
2556 bne 1b
2557 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002558 li r3,0
2559 blr
2560
Michael Neulinge8ebedb2018-03-27 15:37:21 +110025612:
Michael Neulingc1fe1902019-04-01 17:03:12 +11002562 LOAD_REG_ADDR(r11, dawr_force_enable)
2563 lbz r11, 0(r11)
2564 cmpdi r11, 0
Michael Neulingfabb2ef2019-06-17 17:16:18 +10002565 bne 3f
Aneesh Kumar K.Vca9a16c2018-03-30 17:27:24 +05302566 li r3, H_HARDWARE
Michael Neulingfabb2ef2019-06-17 17:16:18 +10002567 blr
25683:
Paul Mackerras8563bf52014-01-08 21:25:29 +11002569 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
Michael Neulinge8ebedb2018-03-27 15:37:21 +11002570 rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002571 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002572 clrrdi r4, r4, 3
2573 std r4, VCPU_DAWR(r3)
2574 std r5, VCPU_DAWRX(r3)
Suraj Jitindar Singh84b02822019-06-17 17:16:19 +10002575 /*
2576 * If came in through the real mode hcall handler then it is necessary
2577 * to write the registers since the return path won't. Otherwise it is
2578 * sufficient to store then in the vcpu struct as they will be loaded
2579 * next time the vcpu is run.
2580 */
2581 mfmsr r6
2582 andi. r6, r6, MSR_DR /* in real mode? */
2583 bne 4f
Paul Mackerras8563bf52014-01-08 21:25:29 +11002584 mtspr SPRN_DAWR, r4
2585 mtspr SPRN_DAWRX, r5
Suraj Jitindar Singh84b02822019-06-17 17:16:19 +100025864: li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002587 blr
2588
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002589_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002590 ori r11,r11,MSR_EE
2591 std r11,VCPU_MSR(r3)
2592 li r0,1
2593 stb r0,VCPU_CEDED(r3)
2594 sync /* order setting ceded vs. testing prodded */
2595 lbz r5,VCPU_PRODDED(r3)
2596 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002597 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002598 li r12,0 /* set trap to 0 to say hcall is handled */
2599 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002600 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002601 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002602
2603 /*
2604 * Set our bit in the bitmask of napping threads unless all the
2605 * other threads are already napping, in which case we send this
2606 * up to the host.
2607 */
2608 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002609 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002610 lwz r8,VCORE_ENTRY_EXIT(r5)
2611 clrldi r8,r8,56
2612 li r0,1
2613 sld r0,r0,r6
2614 addi r6,r5,VCORE_NAPPING_THREADS
261531: lwarx r4,0,r6
2616 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002617 cmpw r4,r8
2618 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002619 stwcx. r4,0,r6
2620 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002621 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002622 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002623 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002624 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002625 lwz r7,VCORE_ENTRY_EXIT(r5)
2626 cmpwi r7,0x100
2627 bge 33f /* another thread already exiting */
2628
2629/*
2630 * Although not specifically required by the architecture, POWER7
2631 * preserves the following registers in nap mode, even if an SMT mode
2632 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2633 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2634 */
2635 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002636 std r14, VCPU_GPR(R14)(r3)
2637 std r15, VCPU_GPR(R15)(r3)
2638 std r16, VCPU_GPR(R16)(r3)
2639 std r17, VCPU_GPR(R17)(r3)
2640 std r18, VCPU_GPR(R18)(r3)
2641 std r19, VCPU_GPR(R19)(r3)
2642 std r20, VCPU_GPR(R20)(r3)
2643 std r21, VCPU_GPR(R21)(r3)
2644 std r22, VCPU_GPR(R22)(r3)
2645 std r23, VCPU_GPR(R23)(r3)
2646 std r24, VCPU_GPR(R24)(r3)
2647 std r25, VCPU_GPR(R25)(r3)
2648 std r26, VCPU_GPR(R26)(r3)
2649 std r27, VCPU_GPR(R27)(r3)
2650 std r28, VCPU_GPR(R28)(r3)
2651 std r29, VCPU_GPR(R29)(r3)
2652 std r30, VCPU_GPR(R30)(r3)
2653 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002654
2655 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002656 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002657
Paul Mackerras93d17392016-06-22 15:52:55 +10002658#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002659/*
2660 * Branch around the call if both CPU_FTR_TM and
2661 * CPU_FTR_P9_TM_HV_ASSIST are off.
2662 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002663BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002664 b 91f
2665END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002666 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11002667 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002668 */
Simon Guo6f597c62018-05-23 15:01:48 +08002669 ld r3, HSTATE_KVM_VCPU(r13)
2670 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11002671 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10002672 bl kvmppc_save_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11002673 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100267491:
Paul Mackerras93d17392016-06-22 15:52:55 +10002675#endif
2676
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002677 /*
2678 * Set DEC to the smaller of DEC and HDEC, so that we wake
2679 * no later than the end of our timeslice (HDEC interrupts
2680 * don't wake us from nap).
2681 */
2682 mfspr r3, SPRN_DEC
2683 mfspr r4, SPRN_HDEC
2684 mftb r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10002685BEGIN_FTR_SECTION
2686 /* On P9 check whether the guest has large decrementer mode enabled */
2687 ld r6, HSTATE_KVM_VCORE(r13)
2688 ld r6, VCORE_LPCR(r6)
2689 andis. r6, r6, LPCR_LD@h
2690 bne 68f
2691END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras2f272462017-05-22 16:25:14 +10002692 extsw r3, r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000269368: EXTEND_HDEC(r4)
Paul Mackerras2f272462017-05-22 16:25:14 +10002694 cmpd r3, r4
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002695 ble 67f
2696 mtspr SPRN_DEC, r4
269767:
2698 /* save expiry time of guest decrementer */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002699 add r3, r3, r5
2700 ld r4, HSTATE_KVM_VCPU(r13)
2701 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10002702 ld r6, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002703 subf r3, r6, r3 /* convert to host TB value */
2704 std r3, VCPU_DEC_EXPIRES(r4)
2705
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002706#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2707 ld r4, HSTATE_KVM_VCPU(r13)
2708 addi r3, r4, VCPU_TB_CEDE
2709 bl kvmhv_accumulate_time
2710#endif
2711
Paul Mackerrasccc07772015-03-28 14:21:07 +11002712 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2713
Nicholas Piggin10d91612019-04-13 00:30:52 +10002714 /* Go back to host stack */
2715 ld r1, HSTATE_HOST_R1(r13)
2716
Paul Mackerras19ccb762011-07-23 17:42:46 +10002717 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002718 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002719 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002720 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002721 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002722 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002723kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002724 mfspr r0, SPRN_CTRLF
2725 clrrdi r0, r0, 1
2726 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302727
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002728 li r0,1
2729 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002730 mfspr r5,SPRN_LPCR
2731 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002732BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002733 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002734 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002735END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002736
2737kvm_nap_sequence: /* desired LPCR value in r5 */
2738BEGIN_FTR_SECTION
2739 /*
2740 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2741 * enable state loss = 1 (allow SMT mode switch)
2742 * requested level = 0 (just stop dispatching)
2743 */
2744 lis r3, (PSSCR_EC | PSSCR_ESL)@h
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002745 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2746 li r4, LPCR_PECE_HVEE@higher
2747 sldi r4, r4, 32
2748 or r5, r5, r4
Nicholas Piggin10d91612019-04-13 00:30:52 +10002749FTR_SECTION_ELSE
2750 li r3, PNV_THREAD_NAP
2751ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002752 mtspr SPRN_LPCR,r5
2753 isync
Nicholas Piggin10d91612019-04-13 00:30:52 +10002754
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002755BEGIN_FTR_SECTION
Nicholas Piggin10d91612019-04-13 00:30:52 +10002756 bl isa300_idle_stop_mayloss
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002757FTR_SECTION_ELSE
Nicholas Piggin10d91612019-04-13 00:30:52 +10002758 bl isa206_idle_insn_mayloss
2759ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_300)
2760
2761 mfspr r0, SPRN_CTRLF
2762 ori r0, r0, 1
2763 mtspr SPRN_CTRLT, r0
2764
2765 mtspr SPRN_SRR1, r3
2766
2767 li r0, 0
2768 stb r0, PACA_FTRACE_ENABLED(r13)
2769
2770 li r0, KVM_HWTHREAD_IN_KVM
2771 stb r0, HSTATE_HWTHREAD_STATE(r13)
2772
2773 lbz r0, HSTATE_NAPPING(r13)
2774 cmpwi r0, NAPPING_CEDE
2775 beq kvm_end_cede
2776 cmpwi r0, NAPPING_NOVCPU
2777 beq kvm_novcpu_wakeup
2778 cmpwi r0, NAPPING_UNSPLIT
2779 beq kvm_unsplit_wakeup
2780 twi 31,0,0 /* Nap state must not be zero */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002781
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100278233: mr r4, r3
2783 li r3, 0
2784 li r12, 0
2785 b 34f
2786
Paul Mackerras19ccb762011-07-23 17:42:46 +10002787kvm_end_cede:
Nicholas Piggin10d91612019-04-13 00:30:52 +10002788 /* Woken by external or decrementer interrupt */
2789
Paul Mackerras4619ac82013-04-17 20:31:41 +00002790 /* get vcpu pointer */
2791 ld r4, HSTATE_KVM_VCPU(r13)
2792
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002793#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2794 addi r3, r4, VCPU_TB_RMINTR
2795 bl kvmhv_accumulate_time
2796#endif
2797
Paul Mackerras93d17392016-06-22 15:52:55 +10002798#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002799/*
2800 * Branch around the call if both CPU_FTR_TM and
2801 * CPU_FTR_P9_TM_HV_ASSIST are off.
2802 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002803BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002804 b 91f
2805END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002806 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11002807 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002808 */
Simon Guo6f597c62018-05-23 15:01:48 +08002809 mr r3, r4
2810 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11002811 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10002812 bl kvmppc_restore_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11002813 nop
Simon Guo6f597c62018-05-23 15:01:48 +08002814 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100281591:
Paul Mackerras93d17392016-06-22 15:52:55 +10002816#endif
2817
Paul Mackerras19ccb762011-07-23 17:42:46 +10002818 /* load up FP state */
2819 bl kvmppc_load_fp
2820
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002821 /* Restore guest decrementer */
2822 ld r3, VCPU_DEC_EXPIRES(r4)
2823 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10002824 ld r6, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002825 add r3, r3, r6 /* convert host TB to guest TB value */
2826 mftb r7
2827 subf r3, r7, r3
2828 mtspr SPRN_DEC, r3
2829
Paul Mackerras19ccb762011-07-23 17:42:46 +10002830 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002831 ld r14, VCPU_GPR(R14)(r4)
2832 ld r15, VCPU_GPR(R15)(r4)
2833 ld r16, VCPU_GPR(R16)(r4)
2834 ld r17, VCPU_GPR(R17)(r4)
2835 ld r18, VCPU_GPR(R18)(r4)
2836 ld r19, VCPU_GPR(R19)(r4)
2837 ld r20, VCPU_GPR(R20)(r4)
2838 ld r21, VCPU_GPR(R21)(r4)
2839 ld r22, VCPU_GPR(R22)(r4)
2840 ld r23, VCPU_GPR(R23)(r4)
2841 ld r24, VCPU_GPR(R24)(r4)
2842 ld r25, VCPU_GPR(R25)(r4)
2843 ld r26, VCPU_GPR(R26)(r4)
2844 ld r27, VCPU_GPR(R27)(r4)
2845 ld r28, VCPU_GPR(R28)(r4)
2846 ld r29, VCPU_GPR(R29)(r4)
2847 ld r30, VCPU_GPR(R30)(r4)
2848 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002849
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002850 /* Check the wake reason in SRR1 to see why we got here */
2851 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002852
Suresh Warrier37f55d32016-08-19 15:35:46 +10002853 /*
2854 * Restore volatile registers since we could have called a
2855 * C routine in kvmppc_check_wake_reason
2856 * r4 = VCPU
2857 * r3 tells us whether we need to return to host or not
2858 * WARNING: it gets checked further down:
2859 * should not modify r3 until this check is done.
2860 */
2861 ld r4, HSTATE_KVM_VCPU(r13)
2862
Paul Mackerras19ccb762011-07-23 17:42:46 +10002863 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100286434: ld r5,HSTATE_KVM_VCORE(r13)
2865 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002866 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002867 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002868 addi r6,r5,VCORE_NAPPING_THREADS
286932: lwarx r7,0,r6
2870 andc r7,r7,r0
2871 stwcx. r7,0,r6
2872 bne 32b
2873 li r0,0
2874 stb r0,HSTATE_NAPPING(r13)
2875
Suresh Warrier37f55d32016-08-19 15:35:46 +10002876 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002877 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002878 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002879 cmpdi r3, 0
2880 bgt guest_exit_cont
Paul Mackerrasdf709a22018-10-08 16:30:52 +11002881 b maybe_reenter_guest
Paul Mackerras19ccb762011-07-23 17:42:46 +10002882
2883 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002884kvm_cede_prodded:
2885 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002886 stb r0,VCPU_PRODDED(r3)
2887 sync /* order testing prodded vs. clearing ceded */
2888 stb r0,VCPU_CEDED(r3)
2889 li r3,H_SUCCESS
2890 blr
2891
2892 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002893kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002894 ld r9, HSTATE_KVM_VCPU(r13)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11002895#ifdef CONFIG_KVM_XICS
Paul Mackerras959c5d52019-08-13 20:03:49 +10002896 /* are we using XIVE with single escalation? */
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11002897 ld r10, VCPU_XIVE_ESC_VADDR(r9)
2898 cmpdi r10, 0
2899 beq 3f
Paul Mackerras959c5d52019-08-13 20:03:49 +10002900 li r6, XIVE_ESB_SET_PQ_00
2901 /*
2902 * If we still have a pending escalation, abort the cede,
2903 * and we must set PQ to 10 rather than 00 so that we don't
2904 * potentially end up with two entries for the escalation
2905 * interrupt in the XIVE interrupt queue. In that case
2906 * we also don't want to set xive_esc_on to 1 here in
2907 * case we race with xive_esc_irq().
2908 */
2909 lbz r5, VCPU_XIVE_ESC_ON(r9)
2910 cmpwi r5, 0
2911 beq 4f
2912 li r0, 0
2913 stb r0, VCPU_CEDED(r9)
2914 li r6, XIVE_ESB_SET_PQ_10
2915 b 5f
29164: li r0, 1
2917 stb r0, VCPU_XIVE_ESC_ON(r9)
2918 /* make sure store to xive_esc_on is seen before xive_esc_irq runs */
2919 sync
29205: /* Enable XIVE escalation */
2921 mfmsr r0
2922 andi. r0, r0, MSR_DR /* in real mode? */
2923 beq 1f
2924 ldx r0, r10, r6
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11002925 b 2f
29261: ld r10, VCPU_XIVE_ESC_RADDR(r9)
Paul Mackerras959c5d52019-08-13 20:03:49 +10002927 ldcix r0, r10, r6
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +110029282: sync
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11002929#endif /* CONFIG_KVM_XICS */
29303: b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002931
Paul Mackerras884dfb72019-02-21 13:38:49 +11002932 /* Try to do machine check recovery in real mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002933machine_check_realmode:
2934 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002935 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002936 nop
Paul Mackerras884dfb72019-02-21 13:38:49 +11002937 /* all machine checks go to virtual mode for further handling */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002938 ld r9, HSTATE_KVM_VCPU(r13)
2939 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Paul Mackerras884dfb72019-02-21 13:38:49 +11002940 b guest_exit_cont
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002941
Paul Mackerrasde56a942011-06-29 00:21:34 +00002942/*
Paul Mackerrasdf709a22018-10-08 16:30:52 +11002943 * Call C code to handle a HMI in real mode.
2944 * Only the primary thread does the call, secondary threads are handled
2945 * by calling hmi_exception_realmode() after kvmppc_hv_entry returns.
2946 * r9 points to the vcpu on entry
2947 */
2948hmi_realmode:
2949 lbz r0, HSTATE_PTID(r13)
2950 cmpwi r0, 0
2951 bne guest_exit_cont
2952 bl kvmppc_realmode_hmi_handler
2953 ld r9, HSTATE_KVM_VCPU(r13)
2954 li r12, BOOK3S_INTERRUPT_HMI
2955 b guest_exit_cont
2956
2957/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002958 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002959 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002960 * 0 if nothing needs to be done
2961 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002962 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002963 * -2 if we handled a PCI passthrough interrupt (returned by
2964 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002965 *
2966 * Also sets r12 to the interrupt vector for any interrupt that needs
2967 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002968 * Modifies all volatile registers (since it may call a C function).
2969 * This routine calls kvmppc_read_intr, a C function, if an external
2970 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002971 */
2972kvmppc_check_wake_reason:
2973 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002974BEGIN_FTR_SECTION
2975 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2976FTR_SECTION_ELSE
2977 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2978ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2979 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002980 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002981 li r3, 0
2982 li r12, 0
2983 cmpwi r6, 6 /* was it the decrementer? */
2984 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002985BEGIN_FTR_SECTION
2986 cmpwi r6, 5 /* privileged doorbell? */
2987 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002988 cmpwi r6, 3 /* hypervisor doorbell? */
2989 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002990END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302991 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2992 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002993 li r3, 1 /* anything else, return 1 */
29940: blr
2995
Paul Mackerras5d00f662014-01-08 21:25:28 +11002996 /* hypervisor doorbell */
29973: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302998
2999 /*
3000 * Clear the doorbell as we will invoke the handler
3001 * explicitly in the guest exit path.
3002 */
3003 lis r6, (PPC_DBELL_SERVER << (63-36))@h
3004 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11003005 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11003006 li r3, 1
Nicholas Piggin2cde3712017-10-10 20:18:28 +10003007BEGIN_FTR_SECTION
3008 PPC_MSGSYNC
3009 lwsync
3010END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11003011 lbz r0, HSTATE_HOST_IPI(r13)
3012 cmpwi r0, 0
3013 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05303014 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11003015 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11003016 blr
3017
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303018 /* Woken up due to Hypervisor maintenance interrupt */
30194: li r12, BOOK3S_INTERRUPT_HMI
3020 li r3, 1
3021 blr
3022
Suresh Warrier37f55d32016-08-19 15:35:46 +10003023 /* external interrupt - create a stack frame so we can call C */
30247: mflr r0
3025 std r0, PPC_LR_STKOFF(r1)
3026 stdu r1, -PPC_MIN_STKFRM(r1)
3027 bl kvmppc_read_intr
3028 nop
3029 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10003030 cmpdi r3, 1
3031 ble 1f
3032
3033 /*
3034 * Return code of 2 means PCI passthrough interrupt, but
3035 * we need to return back to host to complete handling the
3036 * interrupt. Trap reason is expected in r12 by guest
3037 * exit code.
3038 */
3039 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
30401:
Suresh Warrier37f55d32016-08-19 15:35:46 +10003041 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
3042 addi r1, r1, PPC_MIN_STKFRM
3043 mtlr r0
3044 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00003045
3046/*
3047 * Save away FP, VMX and VSX registers.
3048 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11003049 * N.B. r30 and r31 are volatile across this function,
3050 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00003051 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11003052kvmppc_save_fp:
3053 mflr r30
3054 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00003055 mfmsr r5
3056 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00003057#ifdef CONFIG_ALTIVEC
3058BEGIN_FTR_SECTION
3059 oris r8,r8,MSR_VEC@h
3060END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3061#endif
3062#ifdef CONFIG_VSX
3063BEGIN_FTR_SECTION
3064 oris r8,r8,MSR_VSX@h
3065END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3066#endif
3067 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11003068 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003069 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003070#ifdef CONFIG_ALTIVEC
3071BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11003072 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003073 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003074END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3075#endif
3076 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11003077 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11003078 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00003079 blr
3080
3081/*
3082 * Load up FP, VMX and VSX registers
3083 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11003084 * N.B. r30 and r31 are volatile across this function,
3085 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00003086 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00003087kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11003088 mflr r30
3089 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00003090 mfmsr r9
3091 ori r8,r9,MSR_FP
3092#ifdef CONFIG_ALTIVEC
3093BEGIN_FTR_SECTION
3094 oris r8,r8,MSR_VEC@h
3095END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3096#endif
3097#ifdef CONFIG_VSX
3098BEGIN_FTR_SECTION
3099 oris r8,r8,MSR_VSX@h
3100END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3101#endif
3102 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11003103 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003104 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003105#ifdef CONFIG_ALTIVEC
3106BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11003107 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003108 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003109END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3110#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11003111 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003112 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11003113 mtlr r30
3114 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00003115 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10003116
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003117#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
3118/*
3119 * Save transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003120 * Called with r3 pointing to the vcpu struct and r4 containing
3121 * the guest MSR value.
Paul Mackerras7854f752018-10-08 16:30:53 +11003122 * r5 is non-zero iff non-volatile register state needs to be maintained.
3123 * If r5 == 0, this can modify all checkpointed registers, but
Simon Guo6f597c62018-05-23 15:01:48 +08003124 * restores r1 and r2 before exit.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003125 */
Paul Mackerras7854f752018-10-08 16:30:53 +11003126_GLOBAL_TOC(kvmppc_save_tm_hv)
3127EXPORT_SYMBOL_GPL(kvmppc_save_tm_hv)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003128 /* See if we need to handle fake suspend mode */
3129BEGIN_FTR_SECTION
Simon Guocaa3be92018-05-23 15:01:50 +08003130 b __kvmppc_save_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003131END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
3132
3133 lbz r0, HSTATE_FAKE_SUSPEND(r13) /* Were we fake suspended? */
3134 cmpwi r0, 0
Simon Guocaa3be92018-05-23 15:01:50 +08003135 beq __kvmppc_save_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003136
3137 /* The following code handles the fake_suspend = 1 case */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003138 mflr r0
3139 std r0, PPC_LR_STKOFF(r1)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003140 stdu r1, -PPC_MIN_STKFRM(r1)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003141
3142 /* Turn on TM. */
3143 mfmsr r8
3144 li r0, 1
3145 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
3146 mtmsrd r8
3147
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003148 rldicl. r8, r8, 64 - MSR_TS_S_LG, 62 /* Did we actually hrfid? */
3149 beq 4f
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003150BEGIN_FTR_SECTION
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003151 bl pnv_power9_force_smt4_catch
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003152END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003153 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003154
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003155 /* We have to treclaim here because that's the only way to do S->N */
3156 li r3, TM_CAUSE_KVM_RESCHED
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003157 TRECLAIM(R3)
3158
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003159 /*
3160 * We were in fake suspend, so we are not going to save the
3161 * register state as the guest checkpointed state (since
3162 * we already have it), therefore we can now use any volatile GPR.
Paul Mackerras7854f752018-10-08 16:30:53 +11003163 * In fact treclaim in fake suspend state doesn't modify
3164 * any registers.
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003165 */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003166
Paul Mackerras7854f752018-10-08 16:30:53 +11003167BEGIN_FTR_SECTION
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003168 bl pnv_power9_force_smt4_release
Paul Mackerras7854f752018-10-08 16:30:53 +11003169END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003170 nop
3171
31724:
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003173 mfspr r3, SPRN_PSSCR
3174 /* PSSCR_FAKE_SUSPEND is a write-only bit, but clear it anyway */
3175 li r0, PSSCR_FAKE_SUSPEND
3176 andc r3, r3, r0
3177 mtspr SPRN_PSSCR, r3
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003178
Paul Mackerras681c6172018-03-21 21:32:03 +11003179 /* Don't save TEXASR, use value from last exit in real suspend state */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003180 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003181 mfspr r5, SPRN_TFHAR
3182 mfspr r6, SPRN_TFIAR
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003183 std r5, VCPU_TFHAR(r9)
3184 std r6, VCPU_TFIAR(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003185
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003186 addi r1, r1, PPC_MIN_STKFRM
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003187 ld r0, PPC_LR_STKOFF(r1)
3188 mtlr r0
3189 blr
3190
3191/*
3192 * Restore transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003193 * Called with r3 pointing to the vcpu struct
3194 * and r4 containing the guest MSR value.
Paul Mackerras7854f752018-10-08 16:30:53 +11003195 * r5 is non-zero iff non-volatile register state needs to be maintained.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003196 * This potentially modifies all checkpointed registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003197 * It restores r1 and r2 from the PACA.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003198 */
Paul Mackerras7854f752018-10-08 16:30:53 +11003199_GLOBAL_TOC(kvmppc_restore_tm_hv)
3200EXPORT_SYMBOL_GPL(kvmppc_restore_tm_hv)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003201 /*
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003202 * If we are doing TM emulation for the guest on a POWER9 DD2,
3203 * then we don't actually do a trechkpt -- we either set up
3204 * fake-suspend mode, or emulate a TM rollback.
3205 */
3206BEGIN_FTR_SECTION
Simon Guocaa3be92018-05-23 15:01:50 +08003207 b __kvmppc_restore_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003208END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
3209 mflr r0
3210 std r0, PPC_LR_STKOFF(r1)
3211
3212 li r0, 0
3213 stb r0, HSTATE_FAKE_SUSPEND(r13)
3214
3215 /* Turn on TM so we can restore TM SPRs */
3216 mfmsr r5
3217 li r0, 1
3218 rldimi r5, r0, MSR_TM_LG, 63-MSR_TM_LG
3219 mtmsrd r5
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003220
3221 /*
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003222 * The user may change these outside of a transaction, so they must
3223 * always be context switched.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003224 */
Simon Guo6f597c62018-05-23 15:01:48 +08003225 ld r5, VCPU_TFHAR(r3)
3226 ld r6, VCPU_TFIAR(r3)
3227 ld r7, VCPU_TEXASR(r3)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003228 mtspr SPRN_TFHAR, r5
3229 mtspr SPRN_TFIAR, r6
3230 mtspr SPRN_TEXASR, r7
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003231
Simon Guo6f597c62018-05-23 15:01:48 +08003232 rldicl. r5, r4, 64 - MSR_TS_S_LG, 62
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003233 beqlr /* TM not active in guest */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003234
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003235 /* Make sure the failure summary is set */
3236 oris r7, r7, (TEXASR_FS)@h
3237 mtspr SPRN_TEXASR, r7
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003238
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003239 cmpwi r5, 1 /* check for suspended state */
3240 bgt 10f
3241 stb r5, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003242 b 9f /* and return */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100324310: stdu r1, -PPC_MIN_STKFRM(r1)
3244 /* guest is in transactional state, so simulate rollback */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003245 bl kvmhv_emulate_tm_rollback
3246 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003247 addi r1, r1, PPC_MIN_STKFRM
Paul Mackerras7b0e8272018-05-30 20:07:52 +100032489: ld r0, PPC_LR_STKOFF(r1)
3249 mtlr r0
3250 blr
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003251#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003252
Paul Mackerras44a3add2013-10-04 21:45:04 +10003253/*
3254 * We come here if we get any exception or interrupt while we are
3255 * executing host real mode code while in guest MMU context.
Paul Mackerras857b99e2017-09-01 16:17:27 +10003256 * r12 is (CR << 32) | vector
3257 * r13 points to our PACA
3258 * r12 is saved in HSTATE_SCRATCH0(r13)
3259 * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
3260 * r9 is saved in HSTATE_SCRATCH2(r13)
3261 * r13 is saved in HSPRG1
3262 * cfar is saved in HSTATE_CFAR(r13)
3263 * ppr is saved in HSTATE_PPR(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10003264 */
3265kvmppc_bad_host_intr:
Paul Mackerras857b99e2017-09-01 16:17:27 +10003266 /*
3267 * Switch to the emergency stack, but start half-way down in
3268 * case we were already on it.
3269 */
3270 mr r9, r1
3271 std r1, PACAR1(r13)
3272 ld r1, PACAEMERGSP(r13)
3273 subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
3274 std r9, 0(r1)
3275 std r0, GPR0(r1)
3276 std r9, GPR1(r1)
3277 std r2, GPR2(r1)
3278 SAVE_4GPRS(3, r1)
3279 SAVE_2GPRS(7, r1)
3280 srdi r0, r12, 32
3281 clrldi r12, r12, 32
3282 std r0, _CCR(r1)
3283 std r12, _TRAP(r1)
3284 andi. r0, r12, 2
3285 beq 1f
3286 mfspr r3, SPRN_HSRR0
3287 mfspr r4, SPRN_HSRR1
3288 mfspr r5, SPRN_HDAR
3289 mfspr r6, SPRN_HDSISR
3290 b 2f
32911: mfspr r3, SPRN_SRR0
3292 mfspr r4, SPRN_SRR1
3293 mfspr r5, SPRN_DAR
3294 mfspr r6, SPRN_DSISR
32952: std r3, _NIP(r1)
3296 std r4, _MSR(r1)
3297 std r5, _DAR(r1)
3298 std r6, _DSISR(r1)
3299 ld r9, HSTATE_SCRATCH2(r13)
3300 ld r12, HSTATE_SCRATCH0(r13)
3301 GET_SCRATCH0(r0)
3302 SAVE_4GPRS(9, r1)
3303 std r0, GPR13(r1)
3304 SAVE_NVGPRS(r1)
3305 ld r5, HSTATE_CFAR(r13)
3306 std r5, ORIG_GPR3(r1)
3307 mflr r3
3308#ifdef CONFIG_RELOCATABLE
3309 ld r4, HSTATE_SCRATCH1(r13)
3310#else
3311 mfctr r4
3312#endif
3313 mfxer r5
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +05303314 lbz r6, PACAIRQSOFTMASK(r13)
Paul Mackerras857b99e2017-09-01 16:17:27 +10003315 std r3, _LINK(r1)
3316 std r4, _CTR(r1)
3317 std r5, _XER(r1)
3318 std r6, SOFTE(r1)
3319 ld r2, PACATOC(r13)
3320 LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
3321 std r3, STACK_FRAME_OVERHEAD-16(r1)
3322
3323 /*
3324 * On POWER9 do a minimal restore of the MMU and call C code,
3325 * which will print a message and panic.
3326 * XXX On POWER7 and POWER8, we just spin here since we don't
3327 * know what the other threads are doing (and we don't want to
3328 * coordinate with them) - but at least we now have register state
3329 * in memory that we might be able to look at from another CPU.
3330 */
3331BEGIN_FTR_SECTION
Paul Mackerras44a3add2013-10-04 21:45:04 +10003332 b .
Paul Mackerras857b99e2017-09-01 16:17:27 +10003333END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
3334 ld r9, HSTATE_KVM_VCPU(r13)
3335 ld r10, VCPU_KVM(r9)
3336
3337 li r0, 0
3338 mtspr SPRN_AMR, r0
3339 mtspr SPRN_IAMR, r0
3340 mtspr SPRN_CIABR, r0
3341 mtspr SPRN_DAWRX, r0
3342
Paul Mackerras857b99e2017-09-01 16:17:27 +10003343BEGIN_MMU_FTR_SECTION
3344 b 4f
3345END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
3346
3347 slbmte r0, r0
3348 slbia
3349 ptesync
3350 ld r8, PACA_SLBSHADOWPTR(r13)
3351 .rept SLB_NUM_BOLTED
3352 li r3, SLBSHADOW_SAVEAREA
3353 LDX_BE r5, r8, r3
3354 addi r3, r3, 8
3355 LDX_BE r6, r8, r3
3356 andis. r7, r5, SLB_ESID_V@h
3357 beq 3f
3358 slbmte r6, r5
33593: addi r8, r8, 16
3360 .endr
3361
33624: lwz r7, KVM_HOST_LPID(r10)
3363 mtspr SPRN_LPID, r7
3364 mtspr SPRN_PID, r0
3365 ld r8, KVM_HOST_LPCR(r10)
3366 mtspr SPRN_LPCR, r8
3367 isync
3368 li r0, KVM_GUEST_MODE_NONE
3369 stb r0, HSTATE_IN_GUEST(r13)
3370
3371 /*
3372 * Turn on the MMU and jump to C code
3373 */
3374 bcl 20, 31, .+4
33755: mflr r3
3376 addi r3, r3, 9f - 5b
Nicholas Piggineadce3b2018-05-18 03:49:43 +10003377 li r4, -1
3378 rldimi r3, r4, 62, 0 /* ensure 0xc000000000000000 bits are set */
Paul Mackerras857b99e2017-09-01 16:17:27 +10003379 ld r4, PACAKMSR(r13)
3380 mtspr SPRN_SRR0, r3
3381 mtspr SPRN_SRR1, r4
Nicholas Piggin222f20f2018-01-10 03:07:15 +11003382 RFI_TO_KERNEL
Paul Mackerras857b99e2017-09-01 16:17:27 +100033839: addi r3, r1, STACK_FRAME_OVERHEAD
3384 bl kvmppc_bad_interrupt
3385 b 9b
Michael Neulinge4e38122014-03-25 10:47:02 +11003386
3387/*
3388 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3389 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3390 * r11 has the guest MSR value (in/out)
3391 * r9 has a vcpu pointer (in)
3392 * r0 is used as a scratch register
3393 */
3394kvmppc_msr_interrupt:
3395 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3396 cmpwi r0, 2 /* Check if we are in transactional state.. */
3397 ld r11, VCPU_INTR_MSR(r9)
3398 bne 1f
3399 /* ... if transactional, change to suspended */
3400 li r0, 1
34011: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3402 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003403
3404/*
Paul Mackerras41f4e632018-10-08 16:30:51 +11003405 * Load up guest PMU state. R3 points to the vcpu struct.
3406 */
3407_GLOBAL(kvmhv_load_guest_pmu)
3408EXPORT_SYMBOL_GPL(kvmhv_load_guest_pmu)
3409 mr r4, r3
3410 mflr r0
3411 li r3, 1
3412 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
3413 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
3414 isync
3415BEGIN_FTR_SECTION
3416 ld r3, VCPU_MMCR(r4)
3417 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
3418 cmpwi r5, MMCR0_PMAO
3419 beql kvmppc_fix_pmao
3420END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
3421 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
3422 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
3423 lwz r6, VCPU_PMC + 8(r4)
3424 lwz r7, VCPU_PMC + 12(r4)
3425 lwz r8, VCPU_PMC + 16(r4)
3426 lwz r9, VCPU_PMC + 20(r4)
3427 mtspr SPRN_PMC1, r3
3428 mtspr SPRN_PMC2, r5
3429 mtspr SPRN_PMC3, r6
3430 mtspr SPRN_PMC4, r7
3431 mtspr SPRN_PMC5, r8
3432 mtspr SPRN_PMC6, r9
3433 ld r3, VCPU_MMCR(r4)
3434 ld r5, VCPU_MMCR + 8(r4)
3435 ld r6, VCPU_MMCR + 16(r4)
3436 ld r7, VCPU_SIAR(r4)
3437 ld r8, VCPU_SDAR(r4)
3438 mtspr SPRN_MMCR1, r5
3439 mtspr SPRN_MMCRA, r6
3440 mtspr SPRN_SIAR, r7
3441 mtspr SPRN_SDAR, r8
3442BEGIN_FTR_SECTION
3443 ld r5, VCPU_MMCR + 24(r4)
3444 ld r6, VCPU_SIER(r4)
3445 mtspr SPRN_MMCR2, r5
3446 mtspr SPRN_SIER, r6
3447BEGIN_FTR_SECTION_NESTED(96)
3448 lwz r7, VCPU_PMC + 24(r4)
3449 lwz r8, VCPU_PMC + 28(r4)
3450 ld r9, VCPU_MMCR + 32(r4)
3451 mtspr SPRN_SPMC1, r7
3452 mtspr SPRN_SPMC2, r8
3453 mtspr SPRN_MMCRS, r9
3454END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
3455END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3456 mtspr SPRN_MMCR0, r3
3457 isync
3458 mtlr r0
3459 blr
3460
3461/*
3462 * Reload host PMU state saved in the PACA by kvmhv_save_host_pmu.
3463 */
3464_GLOBAL(kvmhv_load_host_pmu)
3465EXPORT_SYMBOL_GPL(kvmhv_load_host_pmu)
3466 mflr r0
3467 lbz r4, PACA_PMCINUSE(r13) /* is the host using the PMU? */
3468 cmpwi r4, 0
3469 beq 23f /* skip if not */
3470BEGIN_FTR_SECTION
3471 ld r3, HSTATE_MMCR0(r13)
3472 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
3473 cmpwi r4, MMCR0_PMAO
3474 beql kvmppc_fix_pmao
3475END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
3476 lwz r3, HSTATE_PMC1(r13)
3477 lwz r4, HSTATE_PMC2(r13)
3478 lwz r5, HSTATE_PMC3(r13)
3479 lwz r6, HSTATE_PMC4(r13)
3480 lwz r8, HSTATE_PMC5(r13)
3481 lwz r9, HSTATE_PMC6(r13)
3482 mtspr SPRN_PMC1, r3
3483 mtspr SPRN_PMC2, r4
3484 mtspr SPRN_PMC3, r5
3485 mtspr SPRN_PMC4, r6
3486 mtspr SPRN_PMC5, r8
3487 mtspr SPRN_PMC6, r9
3488 ld r3, HSTATE_MMCR0(r13)
3489 ld r4, HSTATE_MMCR1(r13)
3490 ld r5, HSTATE_MMCRA(r13)
3491 ld r6, HSTATE_SIAR(r13)
3492 ld r7, HSTATE_SDAR(r13)
3493 mtspr SPRN_MMCR1, r4
3494 mtspr SPRN_MMCRA, r5
3495 mtspr SPRN_SIAR, r6
3496 mtspr SPRN_SDAR, r7
3497BEGIN_FTR_SECTION
3498 ld r8, HSTATE_MMCR2(r13)
3499 ld r9, HSTATE_SIER(r13)
3500 mtspr SPRN_MMCR2, r8
3501 mtspr SPRN_SIER, r9
3502END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3503 mtspr SPRN_MMCR0, r3
3504 isync
3505 mtlr r0
350623: blr
3507
3508/*
3509 * Save guest PMU state into the vcpu struct.
3510 * r3 = vcpu, r4 = full save flag (PMU in use flag set in VPA)
3511 */
3512_GLOBAL(kvmhv_save_guest_pmu)
3513EXPORT_SYMBOL_GPL(kvmhv_save_guest_pmu)
3514 mr r9, r3
3515 mr r8, r4
3516BEGIN_FTR_SECTION
3517 /*
3518 * POWER8 seems to have a hardware bug where setting
3519 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
3520 * when some counters are already negative doesn't seem
3521 * to cause a performance monitor alert (and hence interrupt).
3522 * The effect of this is that when saving the PMU state,
3523 * if there is no PMU alert pending when we read MMCR0
3524 * before freezing the counters, but one becomes pending
3525 * before we read the counters, we lose it.
3526 * To work around this, we need a way to freeze the counters
3527 * before reading MMCR0. Normally, freezing the counters
3528 * is done by writing MMCR0 (to set MMCR0[FC]) which
3529 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
3530 * we can also freeze the counters using MMCR2, by writing
3531 * 1s to all the counter freeze condition bits (there are
3532 * 9 bits each for 6 counters).
3533 */
3534 li r3, -1 /* set all freeze bits */
3535 clrrdi r3, r3, 10
3536 mfspr r10, SPRN_MMCR2
3537 mtspr SPRN_MMCR2, r3
3538 isync
3539END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3540 li r3, 1
3541 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
3542 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
3543 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
3544 mfspr r6, SPRN_MMCRA
3545 /* Clear MMCRA in order to disable SDAR updates */
3546 li r7, 0
3547 mtspr SPRN_MMCRA, r7
3548 isync
3549 cmpwi r8, 0 /* did they ask for PMU stuff to be saved? */
3550 bne 21f
3551 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
3552 b 22f
355321: mfspr r5, SPRN_MMCR1
3554 mfspr r7, SPRN_SIAR
3555 mfspr r8, SPRN_SDAR
3556 std r4, VCPU_MMCR(r9)
3557 std r5, VCPU_MMCR + 8(r9)
3558 std r6, VCPU_MMCR + 16(r9)
3559BEGIN_FTR_SECTION
3560 std r10, VCPU_MMCR + 24(r9)
3561END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3562 std r7, VCPU_SIAR(r9)
3563 std r8, VCPU_SDAR(r9)
3564 mfspr r3, SPRN_PMC1
3565 mfspr r4, SPRN_PMC2
3566 mfspr r5, SPRN_PMC3
3567 mfspr r6, SPRN_PMC4
3568 mfspr r7, SPRN_PMC5
3569 mfspr r8, SPRN_PMC6
3570 stw r3, VCPU_PMC(r9)
3571 stw r4, VCPU_PMC + 4(r9)
3572 stw r5, VCPU_PMC + 8(r9)
3573 stw r6, VCPU_PMC + 12(r9)
3574 stw r7, VCPU_PMC + 16(r9)
3575 stw r8, VCPU_PMC + 20(r9)
3576BEGIN_FTR_SECTION
3577 mfspr r5, SPRN_SIER
3578 std r5, VCPU_SIER(r9)
3579BEGIN_FTR_SECTION_NESTED(96)
3580 mfspr r6, SPRN_SPMC1
3581 mfspr r7, SPRN_SPMC2
3582 mfspr r8, SPRN_MMCRS
3583 stw r6, VCPU_PMC + 24(r9)
3584 stw r7, VCPU_PMC + 28(r9)
3585 std r8, VCPU_MMCR + 32(r9)
3586 lis r4, 0x8000
3587 mtspr SPRN_MMCRS, r4
3588END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
3589END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
359022: blr
3591
3592/*
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003593 * This works around a hardware bug on POWER8E processors, where
3594 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3595 * performance monitor interrupt. Instead, when we need to have
3596 * an interrupt pending, we have to arrange for a counter to overflow.
3597 */
3598kvmppc_fix_pmao:
3599 li r3, 0
3600 mtspr SPRN_MMCR2, r3
3601 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3602 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3603 mtspr SPRN_MMCR0, r3
3604 lis r3, 0x7fff
3605 ori r3, r3, 0xffff
3606 mtspr SPRN_PMC6, r3
3607 isync
3608 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003609
3610#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3611/*
3612 * Start timing an activity
3613 * r3 = pointer to time accumulation struct, r4 = vcpu
3614 */
3615kvmhv_start_timing:
3616 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003617 ld r6, VCORE_TB_OFFSET_APPL(r5)
3618 mftb r5
3619 subf r5, r6, r5 /* subtract current timebase offset */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003620 std r3, VCPU_CUR_ACTIVITY(r4)
3621 std r5, VCPU_ACTIVITY_START(r4)
3622 blr
3623
3624/*
3625 * Accumulate time to one activity and start another.
3626 * r3 = pointer to new time accumulation struct, r4 = vcpu
3627 */
3628kvmhv_accumulate_time:
3629 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003630 ld r8, VCORE_TB_OFFSET_APPL(r5)
3631 ld r5, VCPU_CUR_ACTIVITY(r4)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003632 ld r6, VCPU_ACTIVITY_START(r4)
3633 std r3, VCPU_CUR_ACTIVITY(r4)
3634 mftb r7
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003635 subf r7, r8, r7 /* subtract current timebase offset */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003636 std r7, VCPU_ACTIVITY_START(r4)
3637 cmpdi r5, 0
3638 beqlr
3639 subf r3, r6, r7
3640 ld r8, TAS_SEQCOUNT(r5)
3641 cmpdi r8, 0
3642 addi r8, r8, 1
3643 std r8, TAS_SEQCOUNT(r5)
3644 lwsync
3645 ld r7, TAS_TOTAL(r5)
3646 add r7, r7, r3
3647 std r7, TAS_TOTAL(r5)
3648 ld r6, TAS_MIN(r5)
3649 ld r7, TAS_MAX(r5)
3650 beq 3f
3651 cmpd r3, r6
3652 bge 1f
36533: std r3, TAS_MIN(r5)
36541: cmpd r3, r7
3655 ble 2f
3656 std r3, TAS_MAX(r5)
36572: lwsync
3658 addi r8, r8, 1
3659 std r8, TAS_SEQCOUNT(r5)
3660 blr
3661#endif