blob: 11396c0fee96fa5159ac33c200987b19d0cfac92 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12 *
13 * Derived from book3s_rmhandlers.S and other files, which are:
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100023#include <asm/mmu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000024#include <asm/page.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100025#include <asm/ptrace.h>
26#include <asm/hvcall.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
Paul Mackerrasf0888f72012-02-03 00:54:17 +000029#include <asm/kvm_book3s_asm.h>
Aneesh Kumar K.Vf64e8082016-03-01 12:59:20 +053030#include <asm/book3s/64/mmu-hash.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110031#include <asm/tm.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053032#include <asm/opal.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100033#include <asm/xive-regs.h>
Paul Mackerras857b99e2017-09-01 16:17:27 +100034#include <asm/thread_info.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110035
Paul Mackerras2f272462017-05-22 16:25:14 +100036/* Sign-extend HDEC if not on POWER9 */
37#define EXTEND_HDEC(reg) \
38BEGIN_FTR_SECTION; \
39 extsw reg, reg; \
40END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
41
Michael Neulinge4e38122014-03-25 10:47:02 +110042#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
Paul Mackerrasde56a942011-06-29 00:21:34 +000043
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110044/* Values in HSTATE_NAPPING(r13) */
45#define NAPPING_CEDE 1
46#define NAPPING_NOVCPU 2
47
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100048/* Stack frame offsets for kvmppc_hv_entry */
Paul Mackerras769377f2017-02-15 14:30:17 +110049#define SFS 160
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100050#define STACK_SLOT_TRAP (SFS-4)
51#define STACK_SLOT_TID (SFS-16)
52#define STACK_SLOT_PSSCR (SFS-24)
53#define STACK_SLOT_PID (SFS-32)
54#define STACK_SLOT_IAMR (SFS-40)
55#define STACK_SLOT_CIABR (SFS-48)
56#define STACK_SLOT_DAWR (SFS-56)
57#define STACK_SLOT_DAWRX (SFS-64)
Paul Mackerras769377f2017-02-15 14:30:17 +110058#define STACK_SLOT_HFSCR (SFS-72)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100059
Paul Mackerrasde56a942011-06-29 00:21:34 +000060/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100061 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000062 * Must be called with interrupts hard-disabled.
63 *
64 * Input Registers:
65 *
66 * LR = return address to continue at after eventually re-enabling MMU
67 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100068_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100069 mflr r0
70 std r0, PPC_LR_STKOFF(r1)
71 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000072 mfmsr r10
Paul Mackerras8b24e692017-06-26 15:45:51 +100073 std r10, HSTATE_HOST_MSR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100074 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000075 li r0,MSR_RI
76 andc r0,r10,r0
77 li r6,MSR_IR | MSR_DR
78 andc r6,r10,r6
79 mtmsrd r0,1 /* clear RI in MSR */
80 mtsrr0 r5
81 mtsrr1 r6
Nicholas Piggin222f20f2018-01-10 03:07:15 +110082 RFI_TO_KERNEL
Paul Mackerrasde56a942011-06-29 00:21:34 +000083
Paul Mackerras218309b2013-09-06 13:23:44 +100084kvmppc_call_hv_entry:
Paul Mackerrasc0101502017-10-19 14:11:23 +110085BEGIN_FTR_SECTION
86 /* On P9, do LPCR setting, if necessary */
87 ld r3, HSTATE_SPLIT_MODE(r13)
88 cmpdi r3, 0
89 beq 46f
90 lwz r4, KVM_SPLIT_DO_SET(r3)
91 cmpwi r4, 0
92 beq 46f
93 bl kvmhv_p9_set_lpcr
94 nop
9546:
96END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
97
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110098 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100099 bl kvmppc_hv_entry
100
101 /* Back from guest - restore host state and return to caller */
102
Michael Neulingeee7ff92014-01-08 21:25:19 +1100103BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +1000104 /* Restore host DABR and DABRX */
105 ld r5,HSTATE_DABR(r13)
106 li r6,7
107 mtspr SPRN_DABR,r5
108 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +1100109END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000110
111 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -0500112 ld r3,PACA_SPRG_VDSO(r13)
113 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +1000114
Paul Mackerras218309b2013-09-06 13:23:44 +1000115 /* Reload the host's PMU registers */
116 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
117 lbz r4, LPPACA_PMCINUSE(r3)
118 cmpwi r4, 0
119 beq 23f /* skip if not */
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000120BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000121 ld r3, HSTATE_MMCR0(r13)
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000122 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
123 cmpwi r4, MMCR0_PMAO
124 beql kvmppc_fix_pmao
125END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000126 lwz r3, HSTATE_PMC1(r13)
127 lwz r4, HSTATE_PMC2(r13)
128 lwz r5, HSTATE_PMC3(r13)
129 lwz r6, HSTATE_PMC4(r13)
130 lwz r8, HSTATE_PMC5(r13)
131 lwz r9, HSTATE_PMC6(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000132 mtspr SPRN_PMC1, r3
133 mtspr SPRN_PMC2, r4
134 mtspr SPRN_PMC3, r5
135 mtspr SPRN_PMC4, r6
136 mtspr SPRN_PMC5, r8
137 mtspr SPRN_PMC6, r9
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000138 ld r3, HSTATE_MMCR0(r13)
139 ld r4, HSTATE_MMCR1(r13)
140 ld r5, HSTATE_MMCRA(r13)
141 ld r6, HSTATE_SIAR(r13)
142 ld r7, HSTATE_SDAR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000143 mtspr SPRN_MMCR1, r4
144 mtspr SPRN_MMCRA, r5
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100145 mtspr SPRN_SIAR, r6
146 mtspr SPRN_SDAR, r7
147BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000148 ld r8, HSTATE_MMCR2(r13)
149 ld r9, HSTATE_SIER(r13)
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100150 mtspr SPRN_MMCR2, r8
151 mtspr SPRN_SIER, r9
152END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000153 mtspr SPRN_MMCR0, r3
154 isync
15523:
156
157 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100158 * Reload DEC. HDEC interrupts were disabled when
159 * we reloaded the host's LPCR value.
160 */
161 ld r3, HSTATE_DECEXP(r13)
162 mftb r4
163 subf r4, r4, r3
164 mtspr SPRN_DEC, r4
165
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000166 /* hwthread_req may have got set by cede or no vcpu, so clear it */
167 li r0, 0
168 stb r0, HSTATE_HWTHREAD_REQ(r13)
169
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100170 /*
Aravinda Prasade20bbd32017-05-11 16:33:37 +0530171 * For external interrupts we need to call the Linux
172 * handler to process the interrupt. We do that by jumping
173 * to absolute address 0x500 for external interrupts.
174 * The [h]rfid at the end of the handler will return to
175 * the book3s_hv_interrupts.S code. For other interrupts
176 * we do the rfid to get back to the book3s_hv_interrupts.S
177 * code here.
Paul Mackerras218309b2013-09-06 13:23:44 +1000178 */
179 ld r8, 112+PPC_LR_STKOFF(r1)
180 addi r1, r1, 112
181 ld r7, HSTATE_HOST_MSR(r13)
182
Paul Mackerras8b24e692017-06-26 15:45:51 +1000183 /* Return the trap number on this thread as the return value */
184 mr r3, r12
185
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100186 /*
187 * If we came back from the guest via a relocation-on interrupt,
188 * we will be in virtual mode at this point, which makes it a
189 * little easier to get back to the caller.
190 */
191 mfmsr r0
192 andi. r0, r0, MSR_IR /* in real mode? */
193 bne .Lvirt_return
194
Paul Mackerras8b24e692017-06-26 15:45:51 +1000195 /* RFI into the highmem handler */
Paul Mackerras218309b2013-09-06 13:23:44 +1000196 mfmsr r6
197 li r0, MSR_RI
198 andc r6, r6, r0
199 mtmsrd r6, 1 /* Clear RI in MSR */
200 mtsrr0 r8
201 mtsrr1 r7
Nicholas Piggin222f20f2018-01-10 03:07:15 +1100202 RFI_TO_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000203
Paul Mackerras8b24e692017-06-26 15:45:51 +1000204 /* Virtual-mode return */
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100205.Lvirt_return:
Paul Mackerras8b24e692017-06-26 15:45:51 +1000206 mtlr r8
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100207 blr
208
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100209kvmppc_primary_no_guest:
210 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100211 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
Paul Mackerras2f272462017-05-22 16:25:14 +1000212 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
213 /* HDEC value came from DEC in the first place, it will fit */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100214 mfspr r3, SPRN_HDEC
215 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100216 /*
217 * Make sure the primary has finished the MMU switch.
218 * We should never get here on a secondary thread, but
219 * check it for robustness' sake.
220 */
221 ld r5, HSTATE_KVM_VCORE(r13)
22265: lbz r0, VCORE_IN_GUEST(r5)
223 cmpwi r0, 0
224 beq 65b
225 /* Set LPCR. */
226 ld r8,VCORE_LPCR(r5)
227 mtspr SPRN_LPCR,r8
228 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100229 /* set our bit in napping_threads */
230 ld r5, HSTATE_KVM_VCORE(r13)
231 lbz r7, HSTATE_PTID(r13)
232 li r0, 1
233 sld r0, r0, r7
234 addi r6, r5, VCORE_NAPPING_THREADS
2351: lwarx r3, 0, r6
236 or r3, r3, r0
237 stwcx. r3, 0, r6
238 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100239 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100240 isync
241 li r12, 0
242 lwz r7, VCORE_ENTRY_EXIT(r5)
243 cmpwi r7, 0x100
244 bge kvm_novcpu_exit /* another thread already exiting */
245 li r3, NAPPING_NOVCPU
246 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100247
Paul Mackerrasccc07772015-03-28 14:21:07 +1100248 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100249 b kvm_do_nap
250
Suresh Warrier37f55d32016-08-19 15:35:46 +1000251/*
252 * kvm_novcpu_wakeup
253 * Entered from kvm_start_guest if kvm_hstate.napping is set
254 * to NAPPING_NOVCPU
255 * r2 = kernel TOC
256 * r13 = paca
257 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100258kvm_novcpu_wakeup:
259 ld r1, HSTATE_HOST_R1(r13)
260 ld r5, HSTATE_KVM_VCORE(r13)
261 li r0, 0
262 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100263
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100264 /* check the wake reason */
265 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100266
Suresh Warrier37f55d32016-08-19 15:35:46 +1000267 /*
268 * Restore volatile registers since we could have called
269 * a C routine in kvmppc_check_wake_reason.
270 * r5 = VCORE
271 */
272 ld r5, HSTATE_KVM_VCORE(r13)
273
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100274 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100275 lwz r0, VCORE_ENTRY_EXIT(r5)
276 cmpwi r0, 0x100
277 bge kvm_novcpu_exit
278
279 /* clear our bit in napping_threads */
280 lbz r7, HSTATE_PTID(r13)
281 li r0, 1
282 sld r0, r0, r7
283 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002844: lwarx r7, 0, r6
285 andc r7, r7, r0
286 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100287 bne 4b
288
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100289 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100290 cmpdi r3, 0
291 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100292
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100293 /* See if our timeslice has expired (HDEC is negative) */
294 mfspr r0, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000295 EXTEND_HDEC(r0)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100296 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerras2f272462017-05-22 16:25:14 +1000297 cmpdi r0, 0
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100298 blt kvm_novcpu_exit
299
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100300 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
301 ld r4, HSTATE_KVM_VCPU(r13)
302 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100303 beq kvmppc_primary_no_guest
304
305#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
306 addi r3, r4, VCPU_TB_RMENTRY
307 bl kvmhv_start_timing
308#endif
309 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100310
311kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100312#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
313 ld r4, HSTATE_KVM_VCPU(r13)
314 cmpdi r4, 0
315 beq 13f
316 addi r3, r4, VCPU_TB_RMEXIT
317 bl kvmhv_accumulate_time
318#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110031913: mr r3, r12
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000320 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerraseddb60f2015-03-28 14:21:11 +1100321 bl kvmhv_commence_exit
322 nop
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000323 lwz r12, STACK_SLOT_TRAP(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100324 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100325
Paul Mackerras371fefd2011-06-29 00:23:08 +0000326/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100327 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000328 * Relocation is off and most register values are lost.
329 * r13 points to the PACA.
Nicholas Piggin9d292502017-06-13 23:05:51 +1000330 * r3 contains the SRR1 wakeup value, SRR1 is trashed.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000331 */
332 .globl kvm_start_guest
333kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530334 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100335 mfspr r0, SPRN_CTRLF
336 ori r0, r0, 1
337 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530338
Nicholas Piggin9d292502017-06-13 23:05:51 +1000339 /*
340 * Could avoid this and pass it through in r3. For now,
341 * code expects it to be in SRR1.
342 */
343 mtspr SPRN_SRR1,r3
344
Paul Mackerras19ccb762011-07-23 17:42:46 +1000345 ld r2,PACATOC(r13)
346
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000347 li r0,KVM_HWTHREAD_IN_KVM
348 stb r0,HSTATE_HWTHREAD_STATE(r13)
349
350 /* NV GPR values from power7_idle() will no longer be valid */
351 li r0,1
352 stb r0,PACA_NAPSTATELOST(r13)
353
Paul Mackerras4619ac82013-04-17 20:31:41 +0000354 /* were we napping due to cede? */
355 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100356 cmpwi r0,NAPPING_CEDE
357 beq kvm_end_cede
358 cmpwi r0,NAPPING_NOVCPU
359 beq kvm_novcpu_wakeup
360
361 ld r1,PACAEMERGSP(r13)
362 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000363
364 /*
365 * We weren't napping due to cede, so this must be a secondary
366 * thread being woken up to run a guest, or being woken up due
367 * to a stray IPI. (Or due to some machine check or hypervisor
368 * maintenance interrupt while the core is in KVM.)
369 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000370
371 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100372 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000373 /*
374 * kvmppc_check_wake_reason could invoke a C routine, but we
375 * have no volatile registers to restore when we return.
376 */
377
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100378 cmpdi r3, 0
379 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000380
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000381 /* get vcore pointer, NULL if we have nothing to run */
382 ld r5,HSTATE_KVM_VCORE(r13)
383 cmpdi r5,0
384 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000385 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000386
Paul Mackerras56548fc2014-12-03 14:48:40 +1100387kvm_secondary_got_guest:
388
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100389 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530390 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100391 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000392
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000393 /* On thread 0 of a subcore, set HDEC to max */
394 lbz r4, HSTATE_PTID(r13)
395 cmpwi r4, 0
396 bne 63f
Paul Mackerras2f272462017-05-22 16:25:14 +1000397 LOAD_REG_ADDR(r6, decrementer_max)
398 ld r6, 0(r6)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000399 mtspr SPRN_HDEC, r6
400 /* and set per-LPAR registers, if doing dynamic micro-threading */
401 ld r6, HSTATE_SPLIT_MODE(r13)
402 cmpdi r6, 0
403 beq 63f
Paul Mackerrasc0101502017-10-19 14:11:23 +1100404BEGIN_FTR_SECTION
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000405 ld r0, KVM_SPLIT_RPR(r6)
406 mtspr SPRN_RPR, r0
407 ld r0, KVM_SPLIT_PMMAR(r6)
408 mtspr SPRN_PMMAR, r0
409 ld r0, KVM_SPLIT_LDBAR(r6)
410 mtspr SPRN_LDBAR, r0
411 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100412FTR_SECTION_ELSE
413 /* On P9 we use the split_info for coordinating LPCR changes */
414 lwz r4, KVM_SPLIT_DO_SET(r6)
415 cmpwi r4, 0
Alexander Grafd20fe502018-02-08 18:38:53 +0100416 beq 1f
Paul Mackerrasc0101502017-10-19 14:11:23 +1100417 mr r3, r6
418 bl kvmhv_p9_set_lpcr
419 nop
Alexander Grafd20fe502018-02-08 18:38:53 +01004201:
Paul Mackerrasc0101502017-10-19 14:11:23 +1100421ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasb4deba52015-07-02 20:38:16 +100042263:
423 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100424 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000425 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100426 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000427
428 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000429 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000430 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000431 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100432 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000433 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100434 * kvmppc_run_core() is going to assume that all our vcpu
435 * state is visible in memory. This lwsync makes sure
436 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100437 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000438 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000439 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000440
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530441 /*
442 * All secondaries exiting guest will fall through this path.
443 * Before proceeding, just check for HMI interrupt and
444 * invoke opal hmi handler. By now we are sure that the
445 * primary thread on this core/subcore has already made partition
446 * switch/TB resync and we are good to call opal hmi handler.
447 */
448 cmpwi r12, BOOK3S_INTERRUPT_HMI
449 bne kvm_no_guest
450
451 li r3,0 /* NULL argument */
452 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100453/*
454 * At this point we have finished executing in the guest.
455 * We need to wait for hwthread_req to become zero, since
456 * we may not turn on the MMU while hwthread_req is non-zero.
457 * While waiting we also need to check if we get given a vcpu to run.
458 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000459kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100460 lbz r3, HSTATE_HWTHREAD_REQ(r13)
461 cmpwi r3, 0
462 bne 53f
463 HMT_MEDIUM
464 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000465 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100466 /* need to recheck hwthread_req after a barrier, to avoid race */
467 sync
468 lbz r3, HSTATE_HWTHREAD_REQ(r13)
469 cmpwi r3, 0
470 bne 54f
471/*
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530472 * We jump to pnv_wakeup_loss, which will return to the caller
Paul Mackerras56548fc2014-12-03 14:48:40 +1100473 * of power7_nap in the powernv cpu offline loop. The value we
Nicholas Piggin9d292502017-06-13 23:05:51 +1000474 * put in r3 becomes the return value for power7_nap. pnv_wakeup_loss
475 * requires SRR1 in r12.
Paul Mackerras56548fc2014-12-03 14:48:40 +1100476 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000477 li r3, LPCR_PECE0
478 mfspr r4, SPRN_LPCR
479 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
480 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100481 li r3, 0
Nicholas Piggin9d292502017-06-13 23:05:51 +1000482 mfspr r12,SPRN_SRR1
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530483 b pnv_wakeup_loss
Paul Mackerras56548fc2014-12-03 14:48:40 +1100484
48553: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000486 ld r5, HSTATE_KVM_VCORE(r13)
487 cmpdi r5, 0
488 bne 60f
489 ld r3, HSTATE_SPLIT_MODE(r13)
490 cmpdi r3, 0
491 beq kvm_no_guest
Paul Mackerrasc0101502017-10-19 14:11:23 +1100492 lwz r0, KVM_SPLIT_DO_SET(r3)
493 cmpwi r0, 0
494 bne kvmhv_do_set
495 lwz r0, KVM_SPLIT_DO_RESTORE(r3)
496 cmpwi r0, 0
497 bne kvmhv_do_restore
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000498 lbz r0, KVM_SPLIT_DO_NAP(r3)
499 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100500 beq kvm_no_guest
501 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000502 b kvm_unsplit_nap
50360: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100504 b kvm_secondary_got_guest
505
50654: li r0, KVM_HWTHREAD_IN_KVM
507 stb r0, HSTATE_HWTHREAD_STATE(r13)
508 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000509
Paul Mackerrasc0101502017-10-19 14:11:23 +1100510kvmhv_do_set:
511 /* Set LPCR, LPIDR etc. on P9 */
512 HMT_MEDIUM
513 bl kvmhv_p9_set_lpcr
514 nop
515 b kvm_no_guest
516
517kvmhv_do_restore:
518 HMT_MEDIUM
519 bl kvmhv_p9_restore_lpcr
520 nop
521 b kvm_no_guest
522
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000523/*
524 * Here the primary thread is trying to return the core to
525 * whole-core mode, so we need to nap.
526 */
527kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530528 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530529 * When secondaries are napping in kvm_unsplit_nap() with
530 * hwthread_req = 1, HMI goes ignored even though subcores are
531 * already exited the guest. Hence HMI keeps waking up secondaries
532 * from nap in a loop and secondaries always go back to nap since
533 * no vcore is assigned to them. This makes impossible for primary
534 * thread to get hold of secondary threads resulting into a soft
535 * lockup in KVM path.
536 *
537 * Let us check if HMI is pending and handle it before we go to nap.
538 */
539 cmpwi r12, BOOK3S_INTERRUPT_HMI
540 bne 55f
541 li r3, 0 /* NULL argument */
542 bl hmi_exception_realmode
54355:
544 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530545 * Ensure that secondary doesn't nap when it has
546 * its vcore pointer set.
547 */
548 sync /* matches smp_mb() before setting split_info.do_nap */
549 ld r0, HSTATE_KVM_VCORE(r13)
550 cmpdi r0, 0
551 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000552 /* clear any pending message */
553BEGIN_FTR_SECTION
554 lis r6, (PPC_DBELL_SERVER << (63-36))@h
555 PPC_MSGCLR(6)
556END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
557 /* Set kvm_split_mode.napped[tid] = 1 */
558 ld r3, HSTATE_SPLIT_MODE(r13)
559 li r0, 1
Paul Mackerrasc0101502017-10-19 14:11:23 +1100560 lbz r4, HSTATE_TID(r13)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000561 addi r4, r4, KVM_SPLIT_NAPPED
562 stbx r0, r3, r4
563 /* Check the do_nap flag again after setting napped[] */
564 sync
565 lbz r0, KVM_SPLIT_DO_NAP(r3)
566 cmpwi r0, 0
567 beq 57f
568 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
Paul Mackerrasbf53c882016-11-18 14:34:07 +1100569 mfspr r5, SPRN_LPCR
570 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
571 b kvm_nap_sequence
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000572
57357: li r0, 0
574 stbx r0, r3, r4
575 b kvm_no_guest
576
Paul Mackerras218309b2013-09-06 13:23:44 +1000577/******************************************************************************
578 * *
579 * Entry code *
580 * *
581 *****************************************************************************/
582
Paul Mackerrasde56a942011-06-29 00:21:34 +0000583.global kvmppc_hv_entry
584kvmppc_hv_entry:
585
586 /* Required state:
587 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100588 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000589 * MSR = ~IR|DR
590 * R13 = PACA
591 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000592 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000593 * all other volatile GPRS = free
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100594 * Does not preserve non-volatile GPRs or CR fields
Paul Mackerrasde56a942011-06-29 00:21:34 +0000595 */
596 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000597 std r0, PPC_LR_STKOFF(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000598 stdu r1, -SFS(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000599
Paul Mackerrasde56a942011-06-29 00:21:34 +0000600 /* Save R1 in the PACA */
601 std r1, HSTATE_HOST_R1(r13)
602
Paul Mackerras44a3add2013-10-04 21:45:04 +1000603 li r6, KVM_GUEST_MODE_HOST_HV
604 stb r6, HSTATE_IN_GUEST(r13)
605
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100606#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
607 /* Store initial timestamp */
608 cmpdi r4, 0
609 beq 1f
610 addi r3, r4, VCPU_TB_RMENTRY
611 bl kvmhv_start_timing
6121:
613#endif
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100614
615 /* Use cr7 as an indication of radix mode */
616 ld r5, HSTATE_KVM_VCORE(r13)
617 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
618 lbz r0, KVM_RADIX(r9)
619 cmpwi cr7, r0, 0
620
Paul Mackerras9e368f22011-06-29 00:40:08 +0000621 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100622 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000623 * We don't have to lock against concurrent tlbies,
624 * but we do have to coordinate across hardware threads.
625 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100626 /* Set bit in entry map iff exit map is zero. */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100627 li r7, 1
628 lbz r6, HSTATE_PTID(r13)
629 sld r7, r7, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100630 addi r8, r5, VCORE_ENTRY_EXIT
63121: lwarx r3, 0, r8
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100632 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000633 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100634 or r3, r3, r7
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100635 stwcx. r3, 0, r8
Paul Mackerras371fefd2011-06-29 00:23:08 +0000636 bne 21b
637
638 /* Primary thread switches to guest partition. */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000639 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100640 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000641 lwz r7,KVM_LPID(r9)
Paul Mackerras7a840842016-11-16 22:25:20 +1100642BEGIN_FTR_SECTION
643 ld r6,KVM_SDR1(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000644 li r0,LPID_RSVD /* switch to reserved LPID */
645 mtspr SPRN_LPID,r0
646 ptesync
647 mtspr SPRN_SDR1,r6 /* switch to partition page table */
Paul Mackerras7a840842016-11-16 22:25:20 +1100648END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000649 mtspr SPRN_LPID,r7
650 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000651
652 /* See if we need to flush the TLB */
653 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100654BEGIN_FTR_SECTION
655 /*
656 * On POWER9, individual threads can come in here, but the
657 * TLB is shared between the 4 threads in a core, hence
658 * invalidating on one thread invalidates for all.
659 * Thus we make all 4 threads use the same bit here.
660 */
661 clrrdi r6,r6,2
662END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000663 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
664 srdi r6,r6,6 /* doubleword number */
665 sldi r6,r6,3 /* address offset */
666 add r6,r6,r9
667 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100668 li r8,1
669 sld r8,r8,r7
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000670 ld r7,0(r6)
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100671 and. r7,r7,r8
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000672 beq 22f
Paul Mackerrasca252052014-01-08 21:25:22 +1100673 /* Flush the TLB of any entries for this LPID */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100674 lwz r0,KVM_TLB_SETS(r9)
675 mtctr r0
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000676 li r7,0x800 /* IS field = 0b10 */
677 ptesync
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100678 li r0,0 /* RS for P9 version of tlbiel */
679 bne cr7, 29f
68028: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000681 addi r7,r7,0x1000
682 bdnz 28b
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100683 b 30f
68429: PPC_TLBIEL(7,0,2,1,1) /* for radix, RIC=2, PRS=1, R=1 */
685 addi r7,r7,0x1000
686 bdnz 29b
68730: ptesync
68823: ldarx r7,0,r6 /* clear the bit after TLB flushed */
689 andc r7,r7,r8
690 stdcx. r7,0,r6
691 bne 23b
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000692
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000693 /* Add timebase offset onto timebase */
69422: ld r8,VCORE_TB_OFFSET(r5)
695 cmpdi r8,0
696 beq 37f
697 mftb r6 /* current host timebase */
698 add r8,r8,r6
699 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
700 mftb r7 /* check if lower 24 bits overflowed */
701 clrldi r6,r6,40
702 clrldi r7,r7,40
703 cmpld r7,r6
704 bge 37f
705 addis r8,r8,0x100 /* if so, increment upper 40 bits */
706 mtspr SPRN_TBU40,r8
707
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000708 /* Load guest PCR value to select appropriate compat mode */
70937: ld r7, VCORE_PCR(r5)
710 cmpdi r7, 0
711 beq 38f
712 mtspr SPRN_PCR, r7
71338:
Michael Neulingb005255e2014-01-08 21:25:21 +1100714
715BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000716 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100717 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000718 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100719 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000720 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100721END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
722
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530723 /* Mark the subcore state as inside guest */
724 bl kvmppc_subcore_enter_guest
725 nop
726 ld r5, HSTATE_KVM_VCORE(r13)
727 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000728 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000729 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000730
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100731 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110073210: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100733 beq kvmppc_primary_no_guest
734kvmppc_got_guest:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100735 /* Increment yield count if they have a VPA */
736 ld r3, VCPU_VPA(r4)
737 cmpdi r3, 0
738 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200739 li r6, LPPACA_YIELDCOUNT
740 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100741 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200742 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100743 li r6, 1
744 stb r6, VCPU_VPA_DIRTY(r4)
74525:
746
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100747 /* Save purr/spurr */
748 mfspr r5,SPRN_PURR
749 mfspr r6,SPRN_SPURR
750 std r5,HSTATE_PURR(r13)
751 std r6,HSTATE_SPURR(r13)
752 ld r7,VCPU_PURR(r4)
753 ld r8,VCPU_SPURR(r4)
754 mtspr SPRN_PURR,r7
755 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100756
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100757 /* Save host values of some registers */
758BEGIN_FTR_SECTION
759 mfspr r5, SPRN_TIDR
760 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100761 mfspr r7, SPRN_PID
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000762 mfspr r8, SPRN_IAMR
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100763 std r5, STACK_SLOT_TID(r1)
764 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100765 std r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000766 std r8, STACK_SLOT_IAMR(r1)
Paul Mackerras769377f2017-02-15 14:30:17 +1100767 mfspr r5, SPRN_HFSCR
768 std r5, STACK_SLOT_HFSCR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100769END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000770BEGIN_FTR_SECTION
771 mfspr r5, SPRN_CIABR
772 mfspr r6, SPRN_DAWR
773 mfspr r7, SPRN_DAWRX
774 std r5, STACK_SLOT_CIABR(r1)
775 std r6, STACK_SLOT_DAWR(r1)
776 std r7, STACK_SLOT_DAWRX(r1)
777END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100778
Michael Neulingeee7ff92014-01-08 21:25:19 +1100779BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000780 /* Set partition DABR */
781 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100782 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000783 ld r6,VCPU_DABR(r4)
784 mtspr SPRN_DABRX,r5
785 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000786 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100787END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000788
Michael Neulinge4e38122014-03-25 10:47:02 +1100789#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100790/*
791 * Branch around the call if both CPU_FTR_TM and
792 * CPU_FTR_P9_TM_HV_ASSIST are off.
793 */
Michael Neulinge4e38122014-03-25 10:47:02 +1100794BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100795 b 91f
796END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000797 /*
798 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
799 */
Paul Mackerrasf024ee02016-06-22 14:21:59 +1000800 bl kvmppc_restore_tm
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +110080191:
Michael Neulinge4e38122014-03-25 10:47:02 +1100802#endif
803
Paul Mackerrasde56a942011-06-29 00:21:34 +0000804 /* Load guest PMU registers */
805 /* R4 is live here (vcpu pointer) */
806 li r3, 1
807 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
808 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
809 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000810BEGIN_FTR_SECTION
811 ld r3, VCPU_MMCR(r4)
812 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
813 cmpwi r5, MMCR0_PMAO
814 beql kvmppc_fix_pmao
815END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000816 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
817 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
818 lwz r6, VCPU_PMC + 8(r4)
819 lwz r7, VCPU_PMC + 12(r4)
820 lwz r8, VCPU_PMC + 16(r4)
821 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000822 mtspr SPRN_PMC1, r3
823 mtspr SPRN_PMC2, r5
824 mtspr SPRN_PMC3, r6
825 mtspr SPRN_PMC4, r7
826 mtspr SPRN_PMC5, r8
827 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000828 ld r3, VCPU_MMCR(r4)
829 ld r5, VCPU_MMCR + 8(r4)
830 ld r6, VCPU_MMCR + 16(r4)
831 ld r7, VCPU_SIAR(r4)
832 ld r8, VCPU_SDAR(r4)
833 mtspr SPRN_MMCR1, r5
834 mtspr SPRN_MMCRA, r6
835 mtspr SPRN_SIAR, r7
836 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100837BEGIN_FTR_SECTION
838 ld r5, VCPU_MMCR + 24(r4)
839 ld r6, VCPU_SIER(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100840 mtspr SPRN_MMCR2, r5
841 mtspr SPRN_SIER, r6
842BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100843 lwz r7, VCPU_PMC + 24(r4)
844 lwz r8, VCPU_PMC + 28(r4)
845 ld r9, VCPU_MMCR + 32(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100846 mtspr SPRN_SPMC1, r7
847 mtspr SPRN_SPMC2, r8
848 mtspr SPRN_MMCRS, r9
Paul Mackerras83677f52016-11-16 22:33:27 +1100849END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100850END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000851 mtspr SPRN_MMCR0, r3
852 isync
853
854 /* Load up FP, VMX and VSX registers */
855 bl kvmppc_load_fp
856
857 ld r14, VCPU_GPR(R14)(r4)
858 ld r15, VCPU_GPR(R15)(r4)
859 ld r16, VCPU_GPR(R16)(r4)
860 ld r17, VCPU_GPR(R17)(r4)
861 ld r18, VCPU_GPR(R18)(r4)
862 ld r19, VCPU_GPR(R19)(r4)
863 ld r20, VCPU_GPR(R20)(r4)
864 ld r21, VCPU_GPR(R21)(r4)
865 ld r22, VCPU_GPR(R22)(r4)
866 ld r23, VCPU_GPR(R23)(r4)
867 ld r24, VCPU_GPR(R24)(r4)
868 ld r25, VCPU_GPR(R25)(r4)
869 ld r26, VCPU_GPR(R26)(r4)
870 ld r27, VCPU_GPR(R27)(r4)
871 ld r28, VCPU_GPR(R28)(r4)
872 ld r29, VCPU_GPR(R29)(r4)
873 ld r30, VCPU_GPR(R30)(r4)
874 ld r31, VCPU_GPR(R31)(r4)
875
Paul Mackerrasde56a942011-06-29 00:21:34 +0000876 /* Switch DSCR to guest value */
877 ld r5, VCPU_DSCR(r4)
878 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000879
Michael Neulingb005255e2014-01-08 21:25:21 +1100880BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100881 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100882 b 8f
883END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100884 /* Load up POWER8-specific registers */
885 ld r5, VCPU_IAMR(r4)
886 lwz r6, VCPU_PSPB(r4)
887 ld r7, VCPU_FSCR(r4)
888 mtspr SPRN_IAMR, r5
889 mtspr SPRN_PSPB, r6
890 mtspr SPRN_FSCR, r7
891 ld r5, VCPU_DAWR(r4)
892 ld r6, VCPU_DAWRX(r4)
893 ld r7, VCPU_CIABR(r4)
894 ld r8, VCPU_TAR(r4)
895 mtspr SPRN_DAWR, r5
896 mtspr SPRN_DAWRX, r6
897 mtspr SPRN_CIABR, r7
898 mtspr SPRN_TAR, r8
899 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100900 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000901 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100902 mtspr SPRN_EBBHR, r8
903 ld r5, VCPU_EBBRR(r4)
904 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100905 lwz r7, VCPU_GUEST_PID(r4)
906 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100907 mtspr SPRN_EBBRR, r5
908 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100909 mtspr SPRN_PID, r7
910 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100911BEGIN_FTR_SECTION
Paul Mackerrasf11f6f72017-01-30 21:21:52 +1100912 PPC_INVALIDATE_ERAT
913END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
914BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100915 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100916 ld r5, VCPU_TCSCR(r4)
917 ld r6, VCPU_ACOP(r4)
918 ld r7, VCPU_CSIGR(r4)
919 ld r8, VCPU_TACR(r4)
920 mtspr SPRN_TCSCR, r5
921 mtspr SPRN_ACOP, r6
922 mtspr SPRN_CSIGR, r7
923 mtspr SPRN_TACR, r8
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100924 nop
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100925FTR_SECTION_ELSE
926 /* POWER9-only registers */
927 ld r5, VCPU_TID(r4)
928 ld r6, VCPU_PSSCR(r4)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100929 lbz r8, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100930 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100931 rldimi r6, r8, PSSCR_FAKE_SUSPEND_LG, 63 - PSSCR_FAKE_SUSPEND_LG
Paul Mackerras769377f2017-02-15 14:30:17 +1100932 ld r7, VCPU_HFSCR(r4)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100933 mtspr SPRN_TIDR, r5
934 mtspr SPRN_PSSCR, r6
Paul Mackerras769377f2017-02-15 14:30:17 +1100935 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100936ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11009378:
938
Paul Mackerrasde56a942011-06-29 00:21:34 +0000939 /*
940 * Set the decrementer to the guest decrementer.
941 */
942 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100943 /* r8 is a host timebase value here, convert to guest TB */
944 ld r5,HSTATE_KVM_VCORE(r13)
945 ld r6,VCORE_TB_OFFSET(r5)
946 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000947 mftb r7
948 subf r3,r7,r8
949 mtspr SPRN_DEC,r3
Paul Mackerrasde56a942011-06-29 00:21:34 +0000950
951 ld r5, VCPU_SPRG0(r4)
952 ld r6, VCPU_SPRG1(r4)
953 ld r7, VCPU_SPRG2(r4)
954 ld r8, VCPU_SPRG3(r4)
955 mtspr SPRN_SPRG0, r5
956 mtspr SPRN_SPRG1, r6
957 mtspr SPRN_SPRG2, r7
958 mtspr SPRN_SPRG3, r8
959
Paul Mackerrasde56a942011-06-29 00:21:34 +0000960 /* Load up DAR and DSISR */
961 ld r5, VCPU_DAR(r4)
962 lwz r6, VCPU_DSISR(r4)
963 mtspr SPRN_DAR, r5
964 mtspr SPRN_DSISR, r6
965
Paul Mackerrasde56a942011-06-29 00:21:34 +0000966 /* Restore AMR and UAMOR, set AMOR to all 1s */
967 ld r5,VCPU_AMR(r4)
968 ld r6,VCPU_UAMOR(r4)
969 li r7,-1
970 mtspr SPRN_AMR,r5
971 mtspr SPRN_UAMOR,r6
972 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000973
974 /* Restore state of CTRL run bit; assume 1 on entry */
975 lwz r5,VCPU_CTRL(r4)
976 andi. r5,r5,1
977 bne 4f
978 mfspr r6,SPRN_CTRLF
979 clrrdi r6,r6,1
980 mtspr SPRN_CTRLT,r6
9814:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100982 /* Secondary threads wait for primary to have done partition switch */
983 ld r5, HSTATE_KVM_VCORE(r13)
984 lbz r6, HSTATE_PTID(r13)
985 cmpwi r6, 0
986 beq 21f
987 lbz r0, VCORE_IN_GUEST(r5)
988 cmpwi r0, 0
989 bne 21f
990 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100099120: lwz r3, VCORE_ENTRY_EXIT(r5)
992 cmpwi r3, 0x100
993 bge no_switch_exit
994 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100995 cmpwi r0, 0
996 beq 20b
997 HMT_MEDIUM
99821:
999 /* Set LPCR. */
1000 ld r8,VCORE_LPCR(r5)
1001 mtspr SPRN_LPCR,r8
1002 isync
1003
1004 /* Check if HDEC expires soon */
1005 mfspr r3, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +10001006 EXTEND_HDEC(r3)
1007 cmpdi r3, 512 /* 1 microsecond */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001008 blt hdec_soon
1009
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001010 /* For hash guest, clear out and reload the SLB */
1011 ld r6, VCPU_KVM(r4)
1012 lbz r0, KVM_RADIX(r6)
1013 cmpwi r0, 0
1014 bne 9f
1015 li r6, 0
1016 slbmte r6, r6
1017 slbia
1018 ptesync
1019
1020 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
1021 lwz r5,VCPU_SLB_MAX(r4)
1022 cmpwi r5,0
1023 beq 9f
1024 mtctr r5
1025 addi r6,r4,VCPU_SLB
10261: ld r8,VCPU_SLB_E(r6)
1027 ld r9,VCPU_SLB_V(r6)
1028 slbmte r9,r8
1029 addi r6,r6,VCPU_SLB_SIZE
1030 bdnz 1b
10319:
1032
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001033#ifdef CONFIG_KVM_XICS
1034 /* We are entering the guest on that thread, push VCPU to XIVE */
1035 ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
Andreas Schwab0bfa33c2017-08-15 14:37:01 +10001036 cmpldi cr0, r10, 0
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001037 beq no_xive
1038 ld r11, VCPU_XIVE_SAVED_STATE(r4)
1039 li r9, TM_QW1_OS
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001040 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001041 stdcix r11,r9,r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001042 lwz r11, VCPU_XIVE_CAM_WORD(r4)
1043 li r9, TM_QW1_OS + TM_WORD2
1044 stwcix r11,r9,r10
1045 li r9, 1
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001046 stb r9, VCPU_XIVE_PUSHED(r4)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001047 eieio
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +11001048
1049 /*
1050 * We clear the irq_pending flag. There is a small chance of a
1051 * race vs. the escalation interrupt happening on another
1052 * processor setting it again, but the only consequence is to
1053 * cause a spurrious wakeup on the next H_CEDE which is not an
1054 * issue.
1055 */
1056 li r0,0
1057 stb r0, VCPU_IRQ_PENDING(r4)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11001058
1059 /*
1060 * In single escalation mode, if the escalation interrupt is
1061 * on, we mask it.
1062 */
1063 lbz r0, VCPU_XIVE_ESC_ON(r4)
1064 cmpwi r0,0
1065 beq 1f
1066 ld r10, VCPU_XIVE_ESC_RADDR(r4)
1067 li r9, XIVE_ESB_SET_PQ_01
1068 ldcix r0, r10, r9
1069 sync
1070
1071 /* We have a possible subtle race here: The escalation interrupt might
1072 * have fired and be on its way to the host queue while we mask it,
1073 * and if we unmask it early enough (re-cede right away), there is
1074 * a theorical possibility that it fires again, thus landing in the
1075 * target queue more than once which is a big no-no.
1076 *
1077 * Fortunately, solving this is rather easy. If the above load setting
1078 * PQ to 01 returns a previous value where P is set, then we know the
1079 * escalation interrupt is somewhere on its way to the host. In that
1080 * case we simply don't clear the xive_esc_on flag below. It will be
1081 * eventually cleared by the handler for the escalation interrupt.
1082 *
1083 * Then, when doing a cede, we check that flag again before re-enabling
1084 * the escalation interrupt, and if set, we abort the cede.
1085 */
1086 andi. r0, r0, XIVE_ESB_VAL_P
1087 bne- 1f
1088
1089 /* Now P is 0, we can clear the flag */
1090 li r0, 0
1091 stb r0, VCPU_XIVE_ESC_ON(r4)
10921:
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001093no_xive:
1094#endif /* CONFIG_KVM_XICS */
1095
Suresh Warrier37f55d32016-08-19 15:35:46 +10001096deliver_guest_interrupt:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001097 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +10001098 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001099
1100 mtctr r6
1101 mtxer r7
1102
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001103kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +00001104 ld r10, VCPU_PC(r4)
1105 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001106 ld r6, VCPU_SRR0(r4)
1107 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001108 mtspr SPRN_SRR0, r6
1109 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001110
Paul Mackerras4619ac82013-04-17 20:31:41 +00001111 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001112 rldicl r11, r11, 63 - MSR_HV_LG, 1
1113 rotldi r11, r11, 1 + MSR_HV_LG
1114 ori r11, r11, MSR_ME
1115
Paul Mackerras19ccb762011-07-23 17:42:46 +10001116 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001117 ld r0, VCPU_PENDING_EXC(r4)
1118 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1119 cmpdi cr1, r0, 0
1120 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001121 mfspr r8, SPRN_LPCR
1122 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1123 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1124 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +10001125 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +10001126 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001127 li r0, BOOK3S_INTERRUPT_EXTERNAL
1128 bne cr1, 12f
1129 mfspr r0, SPRN_DEC
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001130BEGIN_FTR_SECTION
1131 /* On POWER9 check whether the guest has large decrementer enabled */
1132 andis. r8, r8, LPCR_LD@h
1133 bne 15f
1134END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1135 extsw r0, r0
113615: cmpdi r0, 0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001137 li r0, BOOK3S_INTERRUPT_DECREMENTER
1138 bge 5f
1139
114012: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +10001141 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001142 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +11001143 mr r9, r4
1144 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +110011455:
Paul Mackerras57900692017-05-16 16:41:20 +10001146BEGIN_FTR_SECTION
1147 b fast_guest_return
1148END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1149 /* On POWER9, check for pending doorbell requests */
1150 lbz r0, VCPU_DBELL_REQ(r4)
1151 cmpwi r0, 0
1152 beq fast_guest_return
1153 ld r5, HSTATE_KVM_VCORE(r13)
1154 /* Set DPDES register so the CPU will take a doorbell interrupt */
1155 li r0, 1
1156 mtspr SPRN_DPDES, r0
1157 std r0, VCORE_DPDES(r5)
1158 /* Make sure other cpus see vcore->dpdes set before dbell req clear */
1159 lwsync
1160 /* Clear the pending doorbell request */
1161 li r0, 0
1162 stb r0, VCPU_DBELL_REQ(r4)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001163
Liu Ping Fan27025a62013-11-19 14:12:48 +08001164/*
1165 * Required state:
1166 * R4 = vcpu
1167 * R10: value for HSRR0
1168 * R11: value for HSRR1
1169 * R13 = PACA
1170 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001171fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001172 li r0,0
1173 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001174 mtspr SPRN_HSRR0,r10
1175 mtspr SPRN_HSRR1,r11
1176
1177 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001178 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001179 stb r9, HSTATE_IN_GUEST(r13)
1180
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001181#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1182 /* Accumulate timing */
1183 addi r3, r4, VCPU_TB_GUEST
1184 bl kvmhv_accumulate_time
1185#endif
1186
Paul Mackerrasde56a942011-06-29 00:21:34 +00001187 /* Enter guest */
1188
Paul Mackerras0acb9112013-02-04 18:10:51 +00001189BEGIN_FTR_SECTION
1190 ld r5, VCPU_CFAR(r4)
1191 mtspr SPRN_CFAR, r5
1192END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001193BEGIN_FTR_SECTION
1194 ld r0, VCPU_PPR(r4)
1195END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001196
Paul Mackerrasde56a942011-06-29 00:21:34 +00001197 ld r5, VCPU_LR(r4)
1198 lwz r6, VCPU_CR(r4)
1199 mtlr r5
1200 mtcr r6
1201
Michael Neulingc75df6f2012-06-25 13:33:10 +00001202 ld r1, VCPU_GPR(R1)(r4)
1203 ld r2, VCPU_GPR(R2)(r4)
1204 ld r3, VCPU_GPR(R3)(r4)
1205 ld r5, VCPU_GPR(R5)(r4)
1206 ld r6, VCPU_GPR(R6)(r4)
1207 ld r7, VCPU_GPR(R7)(r4)
1208 ld r8, VCPU_GPR(R8)(r4)
1209 ld r9, VCPU_GPR(R9)(r4)
1210 ld r10, VCPU_GPR(R10)(r4)
1211 ld r11, VCPU_GPR(R11)(r4)
1212 ld r12, VCPU_GPR(R12)(r4)
1213 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001214
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001215BEGIN_FTR_SECTION
1216 mtspr SPRN_PPR, r0
1217END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Michael Neulinge001fa72017-09-15 15:26:14 +10001218
1219/* Move canary into DSISR to check for later */
1220BEGIN_FTR_SECTION
1221 li r0, 0x7fff
1222 mtspr SPRN_HDSISR, r0
1223END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1224
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001225 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001226 ld r4, VCPU_GPR(R4)(r4)
Nicholas Piggin222f20f2018-01-10 03:07:15 +11001227 HRFI_TO_GUEST
Paul Mackerrasde56a942011-06-29 00:21:34 +00001228 b .
1229
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001230secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001231 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001232 cmpdi r4, 0
1233 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001234 stw r12, VCPU_TRAP(r4)
1235#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001236 addi r3, r4, VCPU_TB_RMEXIT
1237 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001238#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100123911: b kvmhv_switch_to_host
1240
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001241no_switch_exit:
1242 HMT_MEDIUM
1243 li r12, 0
1244 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001245hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001246 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000124712: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001248 mr r9, r4
1249#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001250 addi r3, r4, VCPU_TB_RMEXIT
1251 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001252#endif
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001253 b guest_bypass
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001254
Paul Mackerrasde56a942011-06-29 00:21:34 +00001255/******************************************************************************
1256 * *
1257 * Exit code *
1258 * *
1259 *****************************************************************************/
1260
1261/*
1262 * We come here from the first-level interrupt handlers.
1263 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301264 .globl kvmppc_interrupt_hv
1265kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001266 /*
1267 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001268 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001269 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001270 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001271 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001272 * guest R13 saved in SPRN_SCRATCH0
1273 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001274 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001275 lbz r9, HSTATE_IN_GUEST(r13)
1276 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1277 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301278#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1279 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001280 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301281 beq kvmppc_interrupt_pr
1282#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001283 /* We're now back in the host but in guest MMU context */
1284 li r9, KVM_GUEST_MODE_HOST_HV
1285 stb r9, HSTATE_IN_GUEST(r13)
1286
Paul Mackerrasde56a942011-06-29 00:21:34 +00001287 ld r9, HSTATE_KVM_VCPU(r13)
1288
1289 /* Save registers */
1290
Michael Neulingc75df6f2012-06-25 13:33:10 +00001291 std r0, VCPU_GPR(R0)(r9)
1292 std r1, VCPU_GPR(R1)(r9)
1293 std r2, VCPU_GPR(R2)(r9)
1294 std r3, VCPU_GPR(R3)(r9)
1295 std r4, VCPU_GPR(R4)(r9)
1296 std r5, VCPU_GPR(R5)(r9)
1297 std r6, VCPU_GPR(R6)(r9)
1298 std r7, VCPU_GPR(R7)(r9)
1299 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001300 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001301 std r0, VCPU_GPR(R9)(r9)
1302 std r10, VCPU_GPR(R10)(r9)
1303 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001304 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001305 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001306 /* CR is in the high half of r12 */
1307 srdi r4, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001308 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001309BEGIN_FTR_SECTION
1310 ld r3, HSTATE_CFAR(r13)
1311 std r3, VCPU_CFAR(r9)
1312END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001313BEGIN_FTR_SECTION
1314 ld r4, HSTATE_PPR(r13)
1315 std r4, VCPU_PPR(r9)
1316END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001317
1318 /* Restore R1/R2 so we can handle faults */
1319 ld r1, HSTATE_HOST_R1(r13)
1320 ld r2, PACATOC(r13)
1321
1322 mfspr r10, SPRN_SRR0
1323 mfspr r11, SPRN_SRR1
1324 std r10, VCPU_SRR0(r9)
1325 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001326 /* trap is in the low half of r12, clear CR from the high half */
1327 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001328 andi. r0, r12, 2 /* need to read HSRR0/1? */
1329 beq 1f
1330 mfspr r10, SPRN_HSRR0
1331 mfspr r11, SPRN_HSRR1
1332 clrrdi r12, r12, 2
13331: std r10, VCPU_PC(r9)
1334 std r11, VCPU_MSR(r9)
1335
1336 GET_SCRATCH0(r3)
1337 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001338 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001339 std r4, VCPU_LR(r9)
1340
Paul Mackerrasde56a942011-06-29 00:21:34 +00001341 stw r12,VCPU_TRAP(r9)
1342
Paul Mackerras8b24e692017-06-26 15:45:51 +10001343 /*
1344 * Now that we have saved away SRR0/1 and HSRR0/1,
1345 * interrupts are recoverable in principle, so set MSR_RI.
1346 * This becomes important for relocation-on interrupts from
1347 * the guest, which we can get in radix mode on POWER9.
1348 */
1349 li r0, MSR_RI
1350 mtmsrd r0, 1
1351
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001352#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1353 addi r3, r9, VCPU_TB_RMINTR
1354 mr r4, r9
1355 bl kvmhv_accumulate_time
1356 ld r5, VCPU_GPR(R5)(r9)
1357 ld r6, VCPU_GPR(R6)(r9)
1358 ld r7, VCPU_GPR(R7)(r9)
1359 ld r8, VCPU_GPR(R8)(r9)
1360#endif
1361
Paul Mackerras4a157d62014-12-03 13:30:39 +11001362 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001363 if this is an HEI (HV emulation interrupt, e40) */
1364 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001365 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001366 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1367 bne 11f
1368 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100136911: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001370
1371 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001372#ifdef CONFIG_RELOCATABLE
1373 ld r3, HSTATE_SCRATCH1(r13)
1374 mtctr r3
1375#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001376 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001377#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001378 mfxer r4
1379 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001380 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001381
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001382#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1383 /* For softpatch interrupt, go off and do TM instruction emulation */
1384 cmpwi r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
1385 beq kvmppc_tm_emul
1386#endif
1387
Paul Mackerras697d3892011-12-12 12:36:37 +00001388 /* If this is a page table miss then see if it's theirs or ours */
1389 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1390 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001391 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1392 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001393
Paul Mackerrasde56a942011-06-29 00:21:34 +00001394 /* See if this is a leftover HDEC interrupt */
1395 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1396 bne 2f
1397 mfspr r3,SPRN_HDEC
Paul Mackerrasa4faf2e2017-08-25 19:52:12 +10001398 EXTEND_HDEC(r3)
1399 cmpdi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001400 mr r4,r9
1401 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000014022:
Paul Mackerras697d3892011-12-12 12:36:37 +00001403 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001404 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1405 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001406
Paul Mackerras66feed62015-03-28 14:21:12 +11001407 /* Hypervisor doorbell - exit only if host IPI flag set */
1408 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1409 bne 3f
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001410BEGIN_FTR_SECTION
1411 PPC_MSGSYNC
Nicholas Piggin2cde3712017-10-10 20:18:28 +10001412 lwsync
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001413END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11001414 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301415 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001416 beq 4f
1417 b guest_exit_cont
14183:
Paul Mackerras769377f2017-02-15 14:30:17 +11001419 /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1420 cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1421 bne 14f
1422 mfspr r3, SPRN_HFSCR
1423 std r3, VCPU_HFSCR(r9)
1424 b guest_exit_cont
142514:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001426 /* External interrupt ? */
1427 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001428 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001429
1430 /* External interrupt, first check for host_ipi. If this is
1431 * set, we know the host wants us out so let's do it now
1432 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001433 bl kvmppc_read_intr
Suresh Warrier37f55d32016-08-19 15:35:46 +10001434
1435 /*
1436 * Restore the active volatile registers after returning from
1437 * a C function.
1438 */
1439 ld r9, HSTATE_KVM_VCPU(r13)
1440 li r12, BOOK3S_INTERRUPT_EXTERNAL
1441
1442 /*
1443 * kvmppc_read_intr return codes:
1444 *
1445 * Exit to host (r3 > 0)
1446 * 1 An interrupt is pending that needs to be handled by the host
1447 * Exit guest and return to host by branching to guest_exit_cont
1448 *
Suresh Warrierf7af5202016-08-19 15:35:52 +10001449 * 2 Passthrough that needs completion in the host
1450 * Exit guest and return to host by branching to guest_exit_cont
1451 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1452 * to indicate to the host to complete handling the interrupt
1453 *
Suresh Warrier37f55d32016-08-19 15:35:46 +10001454 * Before returning to guest, we check if any CPU is heading out
1455 * to the host and if so, we head out also. If no CPUs are heading
1456 * check return values <= 0.
1457 *
1458 * Return to guest (r3 <= 0)
1459 * 0 No external interrupt is pending
1460 * -1 A guest wakeup IPI (which has now been cleared)
1461 * In either case, we return to guest to deliver any pending
1462 * guest interrupts.
Suresh Warriere3c13e52016-08-19 15:35:51 +10001463 *
1464 * -2 A PCI passthrough external interrupt was handled
1465 * (interrupt was delivered directly to guest)
1466 * Return to guest to deliver any pending guest interrupts.
Suresh Warrier37f55d32016-08-19 15:35:46 +10001467 */
1468
Suresh Warrierf7af5202016-08-19 15:35:52 +10001469 cmpdi r3, 1
1470 ble 1f
1471
1472 /* Return code = 2 */
1473 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1474 stw r12, VCPU_TRAP(r9)
1475 b guest_exit_cont
1476
14771: /* Return code <= 1 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001478 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001479 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001480
Suresh Warrier37f55d32016-08-19 15:35:46 +10001481 /* Return code <= 0 */
Paul Mackerras66feed62015-03-28 14:21:12 +110014824: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001483 lwz r0, VCORE_ENTRY_EXIT(r5)
1484 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001485 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001486 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001487
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001488guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerras43ff3f62018-01-11 14:31:43 +11001489 /* Save more register state */
1490 mfdar r6
1491 mfdsisr r7
1492 std r6, VCPU_DAR(r9)
1493 stw r7, VCPU_DSISR(r9)
1494 /* don't overwrite fault_dar/fault_dsisr if HDSI */
1495 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1496 beq mc_cont
1497 std r6, VCPU_FAULT_DAR(r9)
1498 stw r7, VCPU_FAULT_DSISR(r9)
1499
1500 /* See if it is a machine check */
1501 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1502 beq machine_check_realmode
1503mc_cont:
1504#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1505 addi r3, r9, VCPU_TB_RMEXIT
1506 mr r4, r9
1507 bl kvmhv_accumulate_time
1508#endif
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001509#ifdef CONFIG_KVM_XICS
1510 /* We are exiting, pull the VP from the XIVE */
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001511 lbz r0, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001512 cmpwi cr0, r0, 0
1513 beq 1f
1514 li r7, TM_SPC_PULL_OS_CTX
1515 li r6, TM_QW1_OS
1516 mfmsr r0
Benjamin Herrenschmidt2662efd2018-01-12 13:37:14 +11001517 andi. r0, r0, MSR_DR /* in real mode? */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001518 beq 2f
1519 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1520 cmpldi cr0, r10, 0
1521 beq 1f
1522 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001523 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001524 lwzx r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001525 /* Second load to recover the context state (Words 0 and 1) */
1526 ldx r11, r6, r10
1527 b 3f
15282: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1529 cmpldi cr0, r10, 0
1530 beq 1f
1531 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001532 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001533 lwzcix r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001534 /* Second load to recover the context state (Words 0 and 1) */
1535 ldcix r11, r6, r10
15363: std r11, VCPU_XIVE_SAVED_STATE(r9)
1537 /* Fixup some of the state for the next load */
1538 li r10, 0
1539 li r0, 0xff
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001540 stb r10, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001541 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1542 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001543 eieio
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100015441:
1545#endif /* CONFIG_KVM_XICS */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001546
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001547 /* For hash guest, read the guest SLB and save it away */
1548 ld r5, VCPU_KVM(r9)
1549 lbz r0, KVM_RADIX(r5)
1550 li r5, 0
1551 cmpwi r0, 0
1552 bne 3f /* for radix, save 0 entries */
1553 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1554 mtctr r0
1555 li r6,0
1556 addi r7,r9,VCPU_SLB
15571: slbmfee r8,r6
1558 andis. r0,r8,SLB_ESID_V@h
1559 beq 2f
1560 add r8,r8,r6 /* put index in */
1561 slbmfev r3,r6
1562 std r8,VCPU_SLB_E(r7)
1563 std r3,VCPU_SLB_V(r7)
1564 addi r7,r7,VCPU_SLB_SIZE
1565 addi r5,r5,1
15662: addi r6,r6,1
1567 bdnz 1b
1568 /* Finally clear out the SLB */
1569 li r0,0
1570 slbmte r0,r0
1571 slbia
1572 ptesync
15733: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001574
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001575guest_bypass:
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301576 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001577 /* Increment exit count, poke other threads to exit */
1578 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001579 nop
1580 ld r9, HSTATE_KVM_VCPU(r13)
1581 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001582
Paul Mackerrasec257162015-06-24 21:18:03 +10001583 /* Stop others sending VCPU interrupts to this physical CPU */
1584 li r0, -1
1585 stw r0, VCPU_CPU(r9)
1586 stw r0, VCPU_THREAD_CPU(r9)
1587
Paul Mackerrasde56a942011-06-29 00:21:34 +00001588 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001589 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001590 stw r6,VCPU_CTRL(r9)
1591 andi. r0,r6,1
1592 bne 4f
1593 ori r6,r6,1
1594 mtspr SPRN_CTRLT,r6
15954:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001596 /*
1597 * Save the guest PURR/SPURR
1598 */
1599 mfspr r5,SPRN_PURR
1600 mfspr r6,SPRN_SPURR
1601 ld r7,VCPU_PURR(r9)
1602 ld r8,VCPU_SPURR(r9)
1603 std r5,VCPU_PURR(r9)
1604 std r6,VCPU_SPURR(r9)
1605 subf r5,r7,r5
1606 subf r6,r8,r6
1607
1608 /*
1609 * Restore host PURR/SPURR and add guest times
1610 * so that the time in the guest gets accounted.
1611 */
1612 ld r3,HSTATE_PURR(r13)
1613 ld r4,HSTATE_SPURR(r13)
1614 add r3,r3,r5
1615 add r4,r4,r6
1616 mtspr SPRN_PURR,r3
1617 mtspr SPRN_SPURR,r4
1618
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001619 /* Save DEC */
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001620 ld r3, HSTATE_KVM_VCORE(r13)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001621 mfspr r5,SPRN_DEC
1622 mftb r6
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001623 /* On P9, if the guest has large decr enabled, don't sign extend */
1624BEGIN_FTR_SECTION
1625 ld r4, VCORE_LPCR(r3)
1626 andis. r4, r4, LPCR_LD@h
1627 bne 16f
1628END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001629 extsw r5,r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000163016: add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001631 /* r5 is a guest timebase value here, convert to host TB */
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001632 ld r4,VCORE_TB_OFFSET(r3)
1633 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001634 std r5,VCPU_DEC_EXPIRES(r9)
1635
Michael Neulingb005255e2014-01-08 21:25:21 +11001636BEGIN_FTR_SECTION
1637 b 8f
1638END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001639 /* Save POWER8-specific registers */
1640 mfspr r5, SPRN_IAMR
1641 mfspr r6, SPRN_PSPB
1642 mfspr r7, SPRN_FSCR
1643 std r5, VCPU_IAMR(r9)
1644 stw r6, VCPU_PSPB(r9)
1645 std r7, VCPU_FSCR(r9)
1646 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001647 mfspr r7, SPRN_TAR
1648 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001649 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001650 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001651 std r8, VCPU_EBBHR(r9)
1652 mfspr r5, SPRN_EBBRR
1653 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001654 mfspr r7, SPRN_PID
1655 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001656 std r5, VCPU_EBBRR(r9)
1657 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001658 stw r7, VCPU_GUEST_PID(r9)
1659 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001660BEGIN_FTR_SECTION
1661 mfspr r5, SPRN_TCSCR
1662 mfspr r6, SPRN_ACOP
1663 mfspr r7, SPRN_CSIGR
1664 mfspr r8, SPRN_TACR
1665 std r5, VCPU_TCSCR(r9)
1666 std r6, VCPU_ACOP(r9)
1667 std r7, VCPU_CSIGR(r9)
1668 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001669FTR_SECTION_ELSE
1670 mfspr r5, SPRN_TIDR
1671 mfspr r6, SPRN_PSSCR
1672 std r5, VCPU_TID(r9)
1673 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1674 rotldi r6, r6, 60
1675 std r6, VCPU_PSSCR(r9)
Paul Mackerras769377f2017-02-15 14:30:17 +11001676 /* Restore host HFSCR value */
1677 ld r7, STACK_SLOT_HFSCR(r1)
1678 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001679ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001680 /*
1681 * Restore various registers to 0, where non-zero values
1682 * set by the guest could disrupt the host.
1683 */
1684 li r0, 0
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001685 mtspr SPRN_PSPB, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001686 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001687BEGIN_FTR_SECTION
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001688 mtspr SPRN_IAMR, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001689 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001690 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1691 li r0, 1
1692 sldi r0, r0, 31
1693 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001694END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +110016958:
1696
Paul Mackerrasde56a942011-06-29 00:21:34 +00001697 /* Save and reset AMR and UAMOR before turning on the MMU */
1698 mfspr r5,SPRN_AMR
1699 mfspr r6,SPRN_UAMOR
1700 std r5,VCPU_AMR(r9)
1701 std r6,VCPU_UAMOR(r9)
1702 li r6,0
1703 mtspr SPRN_AMR,r6
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001704 mtspr SPRN_UAMOR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001705
Paul Mackerrasde56a942011-06-29 00:21:34 +00001706 /* Switch DSCR back to host value */
1707 mfspr r8, SPRN_DSCR
1708 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001709 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001710 mtspr SPRN_DSCR, r7
1711
1712 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001713 std r14, VCPU_GPR(R14)(r9)
1714 std r15, VCPU_GPR(R15)(r9)
1715 std r16, VCPU_GPR(R16)(r9)
1716 std r17, VCPU_GPR(R17)(r9)
1717 std r18, VCPU_GPR(R18)(r9)
1718 std r19, VCPU_GPR(R19)(r9)
1719 std r20, VCPU_GPR(R20)(r9)
1720 std r21, VCPU_GPR(R21)(r9)
1721 std r22, VCPU_GPR(R22)(r9)
1722 std r23, VCPU_GPR(R23)(r9)
1723 std r24, VCPU_GPR(R24)(r9)
1724 std r25, VCPU_GPR(R25)(r9)
1725 std r26, VCPU_GPR(R26)(r9)
1726 std r27, VCPU_GPR(R27)(r9)
1727 std r28, VCPU_GPR(R28)(r9)
1728 std r29, VCPU_GPR(R29)(r9)
1729 std r30, VCPU_GPR(R30)(r9)
1730 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001731
1732 /* Save SPRGs */
1733 mfspr r3, SPRN_SPRG0
1734 mfspr r4, SPRN_SPRG1
1735 mfspr r5, SPRN_SPRG2
1736 mfspr r6, SPRN_SPRG3
1737 std r3, VCPU_SPRG0(r9)
1738 std r4, VCPU_SPRG1(r9)
1739 std r5, VCPU_SPRG2(r9)
1740 std r6, VCPU_SPRG3(r9)
1741
Paul Mackerras89436332012-03-02 01:38:23 +00001742 /* save FP state */
1743 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001744 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001745
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001746#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001747/*
1748 * Branch around the call if both CPU_FTR_TM and
1749 * CPU_FTR_P9_TM_HV_ASSIST are off.
1750 */
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001751BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001752 b 91f
1753END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001754 /*
1755 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
1756 */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10001757 bl kvmppc_save_tm
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100175891:
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001759#endif
1760
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001761 /* Increment yield count if they have a VPA */
1762 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1763 cmpdi r8, 0
1764 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001765 li r4, LPPACA_YIELDCOUNT
1766 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001767 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001768 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001769 li r3, 1
1770 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000177125:
1772 /* Save PMU registers if requested */
1773 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001774BEGIN_FTR_SECTION
1775 /*
1776 * POWER8 seems to have a hardware bug where setting
1777 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1778 * when some counters are already negative doesn't seem
1779 * to cause a performance monitor alert (and hence interrupt).
1780 * The effect of this is that when saving the PMU state,
1781 * if there is no PMU alert pending when we read MMCR0
1782 * before freezing the counters, but one becomes pending
1783 * before we read the counters, we lose it.
1784 * To work around this, we need a way to freeze the counters
1785 * before reading MMCR0. Normally, freezing the counters
1786 * is done by writing MMCR0 (to set MMCR0[FC]) which
1787 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1788 * we can also freeze the counters using MMCR2, by writing
1789 * 1s to all the counter freeze condition bits (there are
1790 * 9 bits each for 6 counters).
1791 */
1792 li r3, -1 /* set all freeze bits */
1793 clrrdi r3, r3, 10
1794 mfspr r10, SPRN_MMCR2
1795 mtspr SPRN_MMCR2, r3
1796 isync
1797END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001798 li r3, 1
1799 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1800 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1801 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001802 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001803 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001804 li r7, 0
1805 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001806 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001807 beq 21f /* if no VPA, save PMU stuff anyway */
1808 lbz r7, LPPACA_PMCINUSE(r8)
1809 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1810 bne 21f
1811 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1812 b 22f
181321: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001814 mfspr r7, SPRN_SIAR
1815 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001816 std r4, VCPU_MMCR(r9)
1817 std r5, VCPU_MMCR + 8(r9)
1818 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001819BEGIN_FTR_SECTION
1820 std r10, VCPU_MMCR + 24(r9)
1821END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001822 std r7, VCPU_SIAR(r9)
1823 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001824 mfspr r3, SPRN_PMC1
1825 mfspr r4, SPRN_PMC2
1826 mfspr r5, SPRN_PMC3
1827 mfspr r6, SPRN_PMC4
1828 mfspr r7, SPRN_PMC5
1829 mfspr r8, SPRN_PMC6
1830 stw r3, VCPU_PMC(r9)
1831 stw r4, VCPU_PMC + 4(r9)
1832 stw r5, VCPU_PMC + 8(r9)
1833 stw r6, VCPU_PMC + 12(r9)
1834 stw r7, VCPU_PMC + 16(r9)
1835 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001836BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001837 mfspr r5, SPRN_SIER
Paul Mackerras83677f52016-11-16 22:33:27 +11001838 std r5, VCPU_SIER(r9)
1839BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001840 mfspr r6, SPRN_SPMC1
1841 mfspr r7, SPRN_SPMC2
1842 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001843 stw r6, VCPU_PMC + 24(r9)
1844 stw r7, VCPU_PMC + 28(r9)
1845 std r8, VCPU_MMCR + 32(r9)
1846 lis r4, 0x8000
1847 mtspr SPRN_MMCRS, r4
Paul Mackerras83677f52016-11-16 22:33:27 +11001848END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001849END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000185022:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001851
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001852 /* Restore host values of some registers */
1853BEGIN_FTR_SECTION
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001854 ld r5, STACK_SLOT_CIABR(r1)
1855 ld r6, STACK_SLOT_DAWR(r1)
1856 ld r7, STACK_SLOT_DAWRX(r1)
1857 mtspr SPRN_CIABR, r5
1858 mtspr SPRN_DAWR, r6
1859 mtspr SPRN_DAWRX, r7
1860END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1861BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001862 ld r5, STACK_SLOT_TID(r1)
1863 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001864 ld r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001865 ld r8, STACK_SLOT_IAMR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001866 mtspr SPRN_TIDR, r5
1867 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001868 mtspr SPRN_PID, r7
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001869 mtspr SPRN_IAMR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001870END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001871
1872#ifdef CONFIG_PPC_RADIX_MMU
1873 /*
1874 * Are we running hash or radix ?
1875 */
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001876 ld r5, VCPU_KVM(r9)
1877 lbz r0, KVM_RADIX(r5)
1878 cmpwi cr2, r0, 0
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001879 beq cr2, 4f
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001880
1881 /* Radix: Handle the case where the guest used an illegal PID */
1882 LOAD_REG_ADDR(r4, mmu_base_pid)
1883 lwz r3, VCPU_GUEST_PID(r9)
1884 lwz r5, 0(r4)
1885 cmpw cr0,r3,r5
1886 blt 2f
1887
1888 /*
1889 * Illegal PID, the HW might have prefetched and cached in the TLB
1890 * some translations for the LPID 0 / guest PID combination which
1891 * Linux doesn't know about, so we need to flush that PID out of
1892 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1893 * the right context.
1894 */
1895 li r0,0
1896 mtspr SPRN_LPID,r0
1897 isync
1898
1899 /* Then do a congruence class local flush */
1900 ld r6,VCPU_KVM(r9)
1901 lwz r0,KVM_TLB_SETS(r6)
1902 mtctr r0
1903 li r7,0x400 /* IS field = 0b01 */
1904 ptesync
1905 sldi r0,r3,32 /* RS has PID */
19061: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
1907 addi r7,r7,0x1000
1908 bdnz 1b
1909 ptesync
1910
19112: /* Flush the ERAT on radix P9 DD1 guest exit */
Paul Mackerrasf11f6f72017-01-30 21:21:52 +11001912BEGIN_FTR_SECTION
1913 PPC_INVALIDATE_ERAT
1914END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
Paul Mackerras6964e6a2018-01-11 14:51:02 +110019154:
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001916#endif /* CONFIG_PPC_RADIX_MMU */
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001917
Paul Mackerrasde56a942011-06-29 00:21:34 +00001918 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001919 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001920 * We don't have to lock against tlbies but we do
1921 * have to coordinate the hardware threads.
1922 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001923kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001924 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001925 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001926 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1927 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001928 cmpwi r3,0
1929 beq 15f
1930 HMT_LOW
193113: lbz r3,VCORE_IN_GUEST(r5)
1932 cmpwi r3,0
1933 bne 13b
1934 HMT_MEDIUM
1935 b 16f
1936
1937 /* Primary thread waits for all the secondaries to exit guest */
193815: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001939 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001940 clrldi r3,r3,56
1941 cmpw r3,r0
1942 bne 15b
1943 isync
1944
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001945 /* Did we actually switch to the guest at all? */
1946 lbz r6, VCORE_IN_GUEST(r5)
1947 cmpwi r6, 0
1948 beq 19f
1949
Paul Mackerrasde56a942011-06-29 00:21:34 +00001950 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001951 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001952BEGIN_FTR_SECTION
1953 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001954 li r8,LPID_RSVD /* switch to reserved LPID */
1955 mtspr SPRN_LPID,r8
1956 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001957 mtspr SPRN_SDR1,r6 /* switch to host page table */
1958END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001959 mtspr SPRN_LPID,r7
1960 isync
1961
Michael Neulingb005255e2014-01-08 21:25:21 +11001962BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001963 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001964 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001965 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001966 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001967 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001968 /* clear DPDES so we don't get guest doorbells in the host */
1969 li r8, 0
1970 mtspr SPRN_DPDES, r8
1971END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1972
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301973 /* If HMI, call kvmppc_realmode_hmi_handler() */
1974 cmpwi r12, BOOK3S_INTERRUPT_HMI
1975 bne 27f
1976 bl kvmppc_realmode_hmi_handler
1977 nop
Paul Mackerrasd0757452018-01-17 20:51:13 +11001978 cmpdi r3, 0
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301979 li r12, BOOK3S_INTERRUPT_HMI
1980 /*
Paul Mackerrasd0757452018-01-17 20:51:13 +11001981 * At this point kvmppc_realmode_hmi_handler may have resync-ed
1982 * the TB, and if it has, we must not subtract the guest timebase
1983 * offset from the timebase. So, skip it.
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301984 *
1985 * Also, do not call kvmppc_subcore_exit_guest() because it has
1986 * been invoked as part of kvmppc_realmode_hmi_handler().
1987 */
Paul Mackerrasd0757452018-01-17 20:51:13 +11001988 beq 30f
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301989
199027:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001991 /* Subtract timebase offset from timebase */
1992 ld r8,VCORE_TB_OFFSET(r5)
1993 cmpdi r8,0
1994 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001995 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001996 subf r8,r8,r6
1997 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1998 mftb r7 /* check if lower 24 bits overflowed */
1999 clrldi r6,r6,40
2000 clrldi r7,r7,40
2001 cmpld r7,r6
2002 bge 17f
2003 addis r8,r8,0x100 /* if so, increment upper 40 bits */
2004 mtspr SPRN_TBU40,r8
2005
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530200617: bl kvmppc_subcore_exit_guest
2007 nop
200830: ld r5,HSTATE_KVM_VCORE(r13)
2009 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
2010
Paul Mackerrasde56a942011-06-29 00:21:34 +00002011 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302012 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002013 cmpdi r0, 0
2014 beq 18f
2015 li r0, 0
2016 mtspr SPRN_PCR, r0
201718:
2018 /* Signal secondary CPUs to continue */
2019 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000202019: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002021 mtspr SPRN_HDEC,r8
2022
Paul Mackerrasc0101502017-10-19 14:11:23 +1100202316:
2024BEGIN_FTR_SECTION
2025 /* On POWER9 with HPT-on-radix we need to wait for all other threads */
2026 ld r3, HSTATE_SPLIT_MODE(r13)
2027 cmpdi r3, 0
2028 beq 47f
2029 lwz r8, KVM_SPLIT_DO_RESTORE(r3)
2030 cmpwi r8, 0
2031 beq 47f
2032 stw r12, STACK_SLOT_TRAP(r1)
2033 bl kvmhv_p9_restore_lpcr
2034 nop
2035 lwz r12, STACK_SLOT_TRAP(r1)
2036 b 48f
203747:
2038END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2039 ld r8,KVM_HOST_LPCR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002040 mtspr SPRN_LPCR,r8
2041 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100204248:
Paul Mackerrasde56a942011-06-29 00:21:34 +00002043 /* load host SLB entries */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002044BEGIN_MMU_FTR_SECTION
2045 b 0f
2046END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002047 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002048
2049 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02002050 li r3, SLBSHADOW_SAVEAREA
2051 LDX_BE r5, r8, r3
2052 addi r3, r3, 8
2053 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00002054 andis. r7,r5,SLB_ESID_V@h
2055 beq 1f
2056 slbmte r6,r5
20571: addi r8,r8,16
2058 .endr
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110020590:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002060#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2061 /* Finish timing, if we have a vcpu */
2062 ld r4, HSTATE_KVM_VCPU(r13)
2063 cmpdi r4, 0
2064 li r3, 0
2065 beq 2f
2066 bl kvmhv_accumulate_time
20672:
2068#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00002069 /* Unset guest mode */
2070 li r0, KVM_GUEST_MODE_NONE
2071 stb r0, HSTATE_IN_GUEST(r13)
2072
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10002073 ld r0, SFS+PPC_LR_STKOFF(r1)
2074 addi r1, r1, SFS
Paul Mackerras218309b2013-09-06 13:23:44 +10002075 mtlr r0
2076 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002077
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002078#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2079/*
2080 * Softpatch interrupt for transactional memory emulation cases
2081 * on POWER9 DD2.2. This is early in the guest exit path - we
2082 * haven't saved registers or done a treclaim yet.
2083 */
2084kvmppc_tm_emul:
2085 /* Save instruction image in HEIR */
2086 mfspr r3, SPRN_HEIR
2087 stw r3, VCPU_HEIR(r9)
2088
2089 /*
2090 * The cases we want to handle here are those where the guest
2091 * is in real suspend mode and is trying to transition to
2092 * transactional mode.
2093 */
2094 lbz r0, HSTATE_FAKE_SUSPEND(r13)
2095 cmpwi r0, 0 /* keep exiting guest if in fake suspend */
2096 bne guest_exit_cont
2097 rldicl r3, r11, 64 - MSR_TS_S_LG, 62
2098 cmpwi r3, 1 /* or if not in suspend state */
2099 bne guest_exit_cont
2100
2101 /* Call C code to do the emulation */
2102 mr r3, r9
2103 bl kvmhv_p9_tm_emulation_early
2104 nop
2105 ld r9, HSTATE_KVM_VCPU(r13)
2106 li r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
2107 cmpwi r3, 0
2108 beq guest_exit_cont /* continue exiting if not handled */
2109 ld r10, VCPU_PC(r9)
2110 ld r11, VCPU_MSR(r9)
2111 b fast_interrupt_c_return /* go back to guest if handled */
2112#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
2113
Paul Mackerras697d3892011-12-12 12:36:37 +00002114/*
2115 * Check whether an HDSI is an HPTE not found fault or something else.
2116 * If it is an HPTE not found fault that is due to the guest accessing
2117 * a page that they have mapped but which we have paged out, then
2118 * we continue on with the guest exit path. In all other cases,
2119 * reflect the HDSI to the guest as a DSI.
2120 */
2121kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002122 ld r3, VCPU_KVM(r9)
2123 lbz r0, KVM_RADIX(r3)
Paul Mackerras697d3892011-12-12 12:36:37 +00002124 mfspr r4, SPRN_HDAR
2125 mfspr r6, SPRN_HDSISR
Michael Neulinge001fa72017-09-15 15:26:14 +10002126BEGIN_FTR_SECTION
2127 /* Look for DSISR canary. If we find it, retry instruction */
2128 cmpdi r6, 0x7fff
2129 beq 6f
2130END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2131 cmpwi r0, 0
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002132 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00002133 /* HPTE not found fault or protection fault? */
2134 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00002135 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002136 andi. r0, r11, MSR_DR /* data relocation enabled? */
2137 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002138BEGIN_FTR_SECTION
2139 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2140 b 4f
2141END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00002142 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002143 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002144 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
2145 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000021464: std r4, VCPU_FAULT_DAR(r9)
2147 stw r6, VCPU_FAULT_DSISR(r9)
2148
2149 /* Search the hash table. */
2150 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002151 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002152 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00002153 ld r9, HSTATE_KVM_VCPU(r13)
2154 ld r10, VCPU_PC(r9)
2155 ld r11, VCPU_MSR(r9)
2156 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
2157 cmpdi r3, 0 /* retry the instruction */
2158 beq 6f
2159 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002160 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00002161 cmpdi r3, -2 /* MMIO emulation; need instr word */
2162 beq 2f
2163
Paul Mackerrascf29b212015-10-27 16:10:20 +11002164 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00002165 ld r4, VCPU_FAULT_DAR(r9)
2166 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110021671: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00002168 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110021697: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00002170 mtspr SPRN_SRR0, r10
2171 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002172 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002173 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002174fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000021756: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10002176 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00002177 mtctr r7
2178 mtxer r8
2179 mr r4, r9
2180 b fast_guest_return
2181
21823: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
2183 ld r5, KVM_VRMA_SLB_V(r5)
2184 b 4b
2185
2186 /* If this is for emulated MMIO, load the instruction word */
21872: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
2188
2189 /* Set guest mode to 'jump over instruction' so if lwz faults
2190 * we'll just continue at the next IP. */
2191 li r0, KVM_GUEST_MODE_SKIP
2192 stb r0, HSTATE_IN_GUEST(r13)
2193
2194 /* Do the access with MSR:DR enabled */
2195 mfmsr r3
2196 ori r4, r3, MSR_DR /* Enable paging for data */
2197 mtmsrd r4
2198 lwz r8, 0(r10)
2199 mtmsrd r3
2200
2201 /* Store the result */
2202 stw r8, VCPU_LAST_INST(r9)
2203
2204 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10002205 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00002206 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002207 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00002208
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002209.Lradix_hdsi:
2210 std r4, VCPU_FAULT_DAR(r9)
2211 stw r6, VCPU_FAULT_DSISR(r9)
2212.Lradix_hisi:
2213 mfspr r5, SPRN_ASDR
2214 std r5, VCPU_FAULT_GPA(r9)
2215 b guest_exit_cont
2216
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002217/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00002218 * Similarly for an HISI, reflect it to the guest as an ISI unless
2219 * it is an HPTE not found fault for a page that we have paged out.
2220 */
2221kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002222 ld r3, VCPU_KVM(r9)
2223 lbz r0, KVM_RADIX(r3)
2224 cmpwi r0, 0
2225 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002226 andis. r0, r11, SRR1_ISI_NOPT@h
2227 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002228 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
2229 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002230BEGIN_FTR_SECTION
2231 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2232 b 4f
2233END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00002234 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002235 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002236 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
2237 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000022384:
2239 /* Search the hash table. */
2240 mr r3, r9 /* vcpu pointer */
2241 mr r4, r10
2242 mr r6, r11
2243 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002244 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00002245 ld r9, HSTATE_KVM_VCPU(r13)
2246 ld r10, VCPU_PC(r9)
2247 ld r11, VCPU_MSR(r9)
2248 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2249 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002250 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002251 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002252 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00002253
Paul Mackerrascf29b212015-10-27 16:10:20 +11002254 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002255 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110022561: li r0, BOOK3S_INTERRUPT_INST_STORAGE
22577: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00002258 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002259 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002260 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002261 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002262
22633: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2264 ld r5, KVM_VRMA_SLB_V(r6)
2265 b 4b
2266
2267/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002268 * Try to handle an hcall in real mode.
2269 * Returns to the guest if we handle it, or continues on up to
2270 * the kernel if we can't (i.e. if we don't have a handler for
2271 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002272 *
2273 * r5 - r8 contain hcall args,
2274 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002275 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002276hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00002277 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002278 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08002279 /* sc 1 from userspace - reflect to guest syscall */
2280 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002281 clrrdi r3,r3,2
2282 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002283 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002284 /* See if this hcall is enabled for in-kernel handling */
2285 ld r4, VCPU_KVM(r9)
2286 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2287 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2288 add r4, r4, r0
2289 ld r0, KVM_ENABLED_HCALLS(r4)
2290 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2291 srd r0, r0, r4
2292 andi. r0, r0, 1
2293 beq guest_exit_cont
2294 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002295 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10002296 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002297 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002298 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10002299 add r12,r3,r4
2300 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002301 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002302 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002303 bctrl
2304 cmpdi r3,H_TOO_HARD
2305 beq hcall_real_fallback
2306 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00002307 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002308 ld r10,VCPU_PC(r4)
2309 ld r11,VCPU_MSR(r4)
2310 b fast_guest_return
2311
Liu Ping Fan27025a62013-11-19 14:12:48 +08002312sc_1_fast_return:
2313 mtspr SPRN_SRR0,r10
2314 mtspr SPRN_SRR1,r11
2315 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11002316 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08002317 mr r4,r9
2318 b fast_guest_return
2319
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002320 /* We've attempted a real mode hcall, but it's punted it back
2321 * to userspace. We need to restore some clobbered volatiles
2322 * before resuming the pass-it-to-qemu path */
2323hcall_real_fallback:
2324 li r12,BOOK3S_INTERRUPT_SYSCALL
2325 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002326
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002327 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002328
2329 .globl hcall_real_table
2330hcall_real_table:
2331 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002332 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2333 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2334 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10002335 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2336 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002337 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
2338 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002339 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002340 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002341 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002342 .long 0 /* 0x2c */
2343 .long 0 /* 0x30 */
2344 .long 0 /* 0x34 */
2345 .long 0 /* 0x38 */
2346 .long 0 /* 0x3c */
2347 .long 0 /* 0x40 */
2348 .long 0 /* 0x44 */
2349 .long 0 /* 0x48 */
2350 .long 0 /* 0x4c */
2351 .long 0 /* 0x50 */
2352 .long 0 /* 0x54 */
2353 .long 0 /* 0x58 */
2354 .long 0 /* 0x5c */
2355 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002356#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002357 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2358 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2359 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002360 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002361 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002362#else
2363 .long 0 /* 0x64 - H_EOI */
2364 .long 0 /* 0x68 - H_CPPR */
2365 .long 0 /* 0x6c - H_IPI */
2366 .long 0 /* 0x70 - H_IPOLL */
2367 .long 0 /* 0x74 - H_XIRR */
2368#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002369 .long 0 /* 0x78 */
2370 .long 0 /* 0x7c */
2371 .long 0 /* 0x80 */
2372 .long 0 /* 0x84 */
2373 .long 0 /* 0x88 */
2374 .long 0 /* 0x8c */
2375 .long 0 /* 0x90 */
2376 .long 0 /* 0x94 */
2377 .long 0 /* 0x98 */
2378 .long 0 /* 0x9c */
2379 .long 0 /* 0xa0 */
2380 .long 0 /* 0xa4 */
2381 .long 0 /* 0xa8 */
2382 .long 0 /* 0xac */
2383 .long 0 /* 0xb0 */
2384 .long 0 /* 0xb4 */
2385 .long 0 /* 0xb8 */
2386 .long 0 /* 0xbc */
2387 .long 0 /* 0xc0 */
2388 .long 0 /* 0xc4 */
2389 .long 0 /* 0xc8 */
2390 .long 0 /* 0xcc */
2391 .long 0 /* 0xd0 */
2392 .long 0 /* 0xd4 */
2393 .long 0 /* 0xd8 */
2394 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002395 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002396 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002397 .long 0 /* 0xe8 */
2398 .long 0 /* 0xec */
2399 .long 0 /* 0xf0 */
2400 .long 0 /* 0xf4 */
2401 .long 0 /* 0xf8 */
2402 .long 0 /* 0xfc */
2403 .long 0 /* 0x100 */
2404 .long 0 /* 0x104 */
2405 .long 0 /* 0x108 */
2406 .long 0 /* 0x10c */
2407 .long 0 /* 0x110 */
2408 .long 0 /* 0x114 */
2409 .long 0 /* 0x118 */
2410 .long 0 /* 0x11c */
2411 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002412 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002413 .long 0 /* 0x128 */
2414 .long 0 /* 0x12c */
2415 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002416 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002417 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002418 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11002419 .long 0 /* 0x140 */
2420 .long 0 /* 0x144 */
2421 .long 0 /* 0x148 */
2422 .long 0 /* 0x14c */
2423 .long 0 /* 0x150 */
2424 .long 0 /* 0x154 */
2425 .long 0 /* 0x158 */
2426 .long 0 /* 0x15c */
2427 .long 0 /* 0x160 */
2428 .long 0 /* 0x164 */
2429 .long 0 /* 0x168 */
2430 .long 0 /* 0x16c */
2431 .long 0 /* 0x170 */
2432 .long 0 /* 0x174 */
2433 .long 0 /* 0x178 */
2434 .long 0 /* 0x17c */
2435 .long 0 /* 0x180 */
2436 .long 0 /* 0x184 */
2437 .long 0 /* 0x188 */
2438 .long 0 /* 0x18c */
2439 .long 0 /* 0x190 */
2440 .long 0 /* 0x194 */
2441 .long 0 /* 0x198 */
2442 .long 0 /* 0x19c */
2443 .long 0 /* 0x1a0 */
2444 .long 0 /* 0x1a4 */
2445 .long 0 /* 0x1a8 */
2446 .long 0 /* 0x1ac */
2447 .long 0 /* 0x1b0 */
2448 .long 0 /* 0x1b4 */
2449 .long 0 /* 0x1b8 */
2450 .long 0 /* 0x1bc */
2451 .long 0 /* 0x1c0 */
2452 .long 0 /* 0x1c4 */
2453 .long 0 /* 0x1c8 */
2454 .long 0 /* 0x1cc */
2455 .long 0 /* 0x1d0 */
2456 .long 0 /* 0x1d4 */
2457 .long 0 /* 0x1d8 */
2458 .long 0 /* 0x1dc */
2459 .long 0 /* 0x1e0 */
2460 .long 0 /* 0x1e4 */
2461 .long 0 /* 0x1e8 */
2462 .long 0 /* 0x1ec */
2463 .long 0 /* 0x1f0 */
2464 .long 0 /* 0x1f4 */
2465 .long 0 /* 0x1f8 */
2466 .long 0 /* 0x1fc */
2467 .long 0 /* 0x200 */
2468 .long 0 /* 0x204 */
2469 .long 0 /* 0x208 */
2470 .long 0 /* 0x20c */
2471 .long 0 /* 0x210 */
2472 .long 0 /* 0x214 */
2473 .long 0 /* 0x218 */
2474 .long 0 /* 0x21c */
2475 .long 0 /* 0x220 */
2476 .long 0 /* 0x224 */
2477 .long 0 /* 0x228 */
2478 .long 0 /* 0x22c */
2479 .long 0 /* 0x230 */
2480 .long 0 /* 0x234 */
2481 .long 0 /* 0x238 */
2482 .long 0 /* 0x23c */
2483 .long 0 /* 0x240 */
2484 .long 0 /* 0x244 */
2485 .long 0 /* 0x248 */
2486 .long 0 /* 0x24c */
2487 .long 0 /* 0x250 */
2488 .long 0 /* 0x254 */
2489 .long 0 /* 0x258 */
2490 .long 0 /* 0x25c */
2491 .long 0 /* 0x260 */
2492 .long 0 /* 0x264 */
2493 .long 0 /* 0x268 */
2494 .long 0 /* 0x26c */
2495 .long 0 /* 0x270 */
2496 .long 0 /* 0x274 */
2497 .long 0 /* 0x278 */
2498 .long 0 /* 0x27c */
2499 .long 0 /* 0x280 */
2500 .long 0 /* 0x284 */
2501 .long 0 /* 0x288 */
2502 .long 0 /* 0x28c */
2503 .long 0 /* 0x290 */
2504 .long 0 /* 0x294 */
2505 .long 0 /* 0x298 */
2506 .long 0 /* 0x29c */
2507 .long 0 /* 0x2a0 */
2508 .long 0 /* 0x2a4 */
2509 .long 0 /* 0x2a8 */
2510 .long 0 /* 0x2ac */
2511 .long 0 /* 0x2b0 */
2512 .long 0 /* 0x2b4 */
2513 .long 0 /* 0x2b8 */
2514 .long 0 /* 0x2bc */
2515 .long 0 /* 0x2c0 */
2516 .long 0 /* 0x2c4 */
2517 .long 0 /* 0x2c8 */
2518 .long 0 /* 0x2cc */
2519 .long 0 /* 0x2d0 */
2520 .long 0 /* 0x2d4 */
2521 .long 0 /* 0x2d8 */
2522 .long 0 /* 0x2dc */
2523 .long 0 /* 0x2e0 */
2524 .long 0 /* 0x2e4 */
2525 .long 0 /* 0x2e8 */
2526 .long 0 /* 0x2ec */
2527 .long 0 /* 0x2f0 */
2528 .long 0 /* 0x2f4 */
2529 .long 0 /* 0x2f8 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002530#ifdef CONFIG_KVM_XICS
2531 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2532#else
2533 .long 0 /* 0x2fc - H_XIRR_X*/
2534#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002535 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002536 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002537hcall_real_table_end:
2538
Paul Mackerras8563bf52014-01-08 21:25:29 +11002539_GLOBAL(kvmppc_h_set_xdabr)
2540 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2541 beq 6f
2542 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2543 andc. r0, r5, r0
2544 beq 3f
25456: li r3, H_PARAMETER
2546 blr
2547
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002548_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002549 li r5, DABRX_USER | DABRX_KERNEL
25503:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002551BEGIN_FTR_SECTION
2552 b 2f
2553END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002554 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002555 stw r5, VCPU_DABRX(r3)
2556 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002557 /* Work around P7 bug where DABR can get corrupted on mtspr */
25581: mtspr SPRN_DABR,r4
2559 mfspr r5, SPRN_DABR
2560 cmpd r4, r5
2561 bne 1b
2562 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002563 li r3,0
2564 blr
2565
Paul Mackerras8563bf52014-01-08 21:25:29 +11002566 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
25672: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002568 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002569 clrrdi r4, r4, 3
2570 std r4, VCPU_DAWR(r3)
2571 std r5, VCPU_DAWRX(r3)
2572 mtspr SPRN_DAWR, r4
2573 mtspr SPRN_DAWRX, r5
2574 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002575 blr
2576
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002577_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002578 ori r11,r11,MSR_EE
2579 std r11,VCPU_MSR(r3)
2580 li r0,1
2581 stb r0,VCPU_CEDED(r3)
2582 sync /* order setting ceded vs. testing prodded */
2583 lbz r5,VCPU_PRODDED(r3)
2584 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002585 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002586 li r12,0 /* set trap to 0 to say hcall is handled */
2587 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002588 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002589 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002590
2591 /*
2592 * Set our bit in the bitmask of napping threads unless all the
2593 * other threads are already napping, in which case we send this
2594 * up to the host.
2595 */
2596 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002597 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002598 lwz r8,VCORE_ENTRY_EXIT(r5)
2599 clrldi r8,r8,56
2600 li r0,1
2601 sld r0,r0,r6
2602 addi r6,r5,VCORE_NAPPING_THREADS
260331: lwarx r4,0,r6
2604 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002605 cmpw r4,r8
2606 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002607 stwcx. r4,0,r6
2608 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002609 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002610 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002611 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002612 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002613 lwz r7,VCORE_ENTRY_EXIT(r5)
2614 cmpwi r7,0x100
2615 bge 33f /* another thread already exiting */
2616
2617/*
2618 * Although not specifically required by the architecture, POWER7
2619 * preserves the following registers in nap mode, even if an SMT mode
2620 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2621 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2622 */
2623 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002624 std r14, VCPU_GPR(R14)(r3)
2625 std r15, VCPU_GPR(R15)(r3)
2626 std r16, VCPU_GPR(R16)(r3)
2627 std r17, VCPU_GPR(R17)(r3)
2628 std r18, VCPU_GPR(R18)(r3)
2629 std r19, VCPU_GPR(R19)(r3)
2630 std r20, VCPU_GPR(R20)(r3)
2631 std r21, VCPU_GPR(R21)(r3)
2632 std r22, VCPU_GPR(R22)(r3)
2633 std r23, VCPU_GPR(R23)(r3)
2634 std r24, VCPU_GPR(R24)(r3)
2635 std r25, VCPU_GPR(R25)(r3)
2636 std r26, VCPU_GPR(R26)(r3)
2637 std r27, VCPU_GPR(R27)(r3)
2638 std r28, VCPU_GPR(R28)(r3)
2639 std r29, VCPU_GPR(R29)(r3)
2640 std r30, VCPU_GPR(R30)(r3)
2641 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002642
2643 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002644 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002645
Paul Mackerras93d17392016-06-22 15:52:55 +10002646#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002647/*
2648 * Branch around the call if both CPU_FTR_TM and
2649 * CPU_FTR_P9_TM_HV_ASSIST are off.
2650 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002651BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002652 b 91f
2653END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002654 /*
2655 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2656 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002657 ld r9, HSTATE_KVM_VCPU(r13)
2658 bl kvmppc_save_tm
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100265991:
Paul Mackerras93d17392016-06-22 15:52:55 +10002660#endif
2661
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002662 /*
2663 * Set DEC to the smaller of DEC and HDEC, so that we wake
2664 * no later than the end of our timeslice (HDEC interrupts
2665 * don't wake us from nap).
2666 */
2667 mfspr r3, SPRN_DEC
2668 mfspr r4, SPRN_HDEC
2669 mftb r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10002670BEGIN_FTR_SECTION
2671 /* On P9 check whether the guest has large decrementer mode enabled */
2672 ld r6, HSTATE_KVM_VCORE(r13)
2673 ld r6, VCORE_LPCR(r6)
2674 andis. r6, r6, LPCR_LD@h
2675 bne 68f
2676END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras2f272462017-05-22 16:25:14 +10002677 extsw r3, r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000267868: EXTEND_HDEC(r4)
Paul Mackerras2f272462017-05-22 16:25:14 +10002679 cmpd r3, r4
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002680 ble 67f
2681 mtspr SPRN_DEC, r4
268267:
2683 /* save expiry time of guest decrementer */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002684 add r3, r3, r5
2685 ld r4, HSTATE_KVM_VCPU(r13)
2686 ld r5, HSTATE_KVM_VCORE(r13)
2687 ld r6, VCORE_TB_OFFSET(r5)
2688 subf r3, r6, r3 /* convert to host TB value */
2689 std r3, VCPU_DEC_EXPIRES(r4)
2690
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002691#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2692 ld r4, HSTATE_KVM_VCPU(r13)
2693 addi r3, r4, VCPU_TB_CEDE
2694 bl kvmhv_accumulate_time
2695#endif
2696
Paul Mackerrasccc07772015-03-28 14:21:07 +11002697 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2698
Paul Mackerras19ccb762011-07-23 17:42:46 +10002699 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002700 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002701 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002702 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002703 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002704 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002705kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002706 mfspr r0, SPRN_CTRLF
2707 clrrdi r0, r0, 1
2708 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302709
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002710 li r0,1
2711 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002712 mfspr r5,SPRN_LPCR
2713 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002714BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002715 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002716 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002717END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002718
2719kvm_nap_sequence: /* desired LPCR value in r5 */
2720BEGIN_FTR_SECTION
2721 /*
2722 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2723 * enable state loss = 1 (allow SMT mode switch)
2724 * requested level = 0 (just stop dispatching)
2725 */
2726 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2727 mtspr SPRN_PSSCR, r3
2728 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2729 li r4, LPCR_PECE_HVEE@higher
2730 sldi r4, r4, 32
2731 or r5, r5, r4
2732END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002733 mtspr SPRN_LPCR,r5
2734 isync
2735 li r0, 0
2736 std r0, HSTATE_SCRATCH0(r13)
2737 ptesync
2738 ld r0, HSTATE_SCRATCH0(r13)
27391: cmpd r0, r0
2740 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002741BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002742 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002743FTR_SECTION_ELSE
2744 PPC_STOP
2745ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002746 b .
2747
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100274833: mr r4, r3
2749 li r3, 0
2750 li r12, 0
2751 b 34f
2752
Paul Mackerras19ccb762011-07-23 17:42:46 +10002753kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002754 /* get vcpu pointer */
2755 ld r4, HSTATE_KVM_VCPU(r13)
2756
Paul Mackerras19ccb762011-07-23 17:42:46 +10002757 /* Woken by external or decrementer interrupt */
2758 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002759
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002760#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2761 addi r3, r4, VCPU_TB_RMINTR
2762 bl kvmhv_accumulate_time
2763#endif
2764
Paul Mackerras93d17392016-06-22 15:52:55 +10002765#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002766/*
2767 * Branch around the call if both CPU_FTR_TM and
2768 * CPU_FTR_P9_TM_HV_ASSIST are off.
2769 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002770BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002771 b 91f
2772END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002773 /*
2774 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2775 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002776 bl kvmppc_restore_tm
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100277791:
Paul Mackerras93d17392016-06-22 15:52:55 +10002778#endif
2779
Paul Mackerras19ccb762011-07-23 17:42:46 +10002780 /* load up FP state */
2781 bl kvmppc_load_fp
2782
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002783 /* Restore guest decrementer */
2784 ld r3, VCPU_DEC_EXPIRES(r4)
2785 ld r5, HSTATE_KVM_VCORE(r13)
2786 ld r6, VCORE_TB_OFFSET(r5)
2787 add r3, r3, r6 /* convert host TB to guest TB value */
2788 mftb r7
2789 subf r3, r7, r3
2790 mtspr SPRN_DEC, r3
2791
Paul Mackerras19ccb762011-07-23 17:42:46 +10002792 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002793 ld r14, VCPU_GPR(R14)(r4)
2794 ld r15, VCPU_GPR(R15)(r4)
2795 ld r16, VCPU_GPR(R16)(r4)
2796 ld r17, VCPU_GPR(R17)(r4)
2797 ld r18, VCPU_GPR(R18)(r4)
2798 ld r19, VCPU_GPR(R19)(r4)
2799 ld r20, VCPU_GPR(R20)(r4)
2800 ld r21, VCPU_GPR(R21)(r4)
2801 ld r22, VCPU_GPR(R22)(r4)
2802 ld r23, VCPU_GPR(R23)(r4)
2803 ld r24, VCPU_GPR(R24)(r4)
2804 ld r25, VCPU_GPR(R25)(r4)
2805 ld r26, VCPU_GPR(R26)(r4)
2806 ld r27, VCPU_GPR(R27)(r4)
2807 ld r28, VCPU_GPR(R28)(r4)
2808 ld r29, VCPU_GPR(R29)(r4)
2809 ld r30, VCPU_GPR(R30)(r4)
2810 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002811
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002812 /* Check the wake reason in SRR1 to see why we got here */
2813 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002814
Suresh Warrier37f55d32016-08-19 15:35:46 +10002815 /*
2816 * Restore volatile registers since we could have called a
2817 * C routine in kvmppc_check_wake_reason
2818 * r4 = VCPU
2819 * r3 tells us whether we need to return to host or not
2820 * WARNING: it gets checked further down:
2821 * should not modify r3 until this check is done.
2822 */
2823 ld r4, HSTATE_KVM_VCPU(r13)
2824
Paul Mackerras19ccb762011-07-23 17:42:46 +10002825 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100282634: ld r5,HSTATE_KVM_VCORE(r13)
2827 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002828 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002829 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002830 addi r6,r5,VCORE_NAPPING_THREADS
283132: lwarx r7,0,r6
2832 andc r7,r7,r0
2833 stwcx. r7,0,r6
2834 bne 32b
2835 li r0,0
2836 stb r0,HSTATE_NAPPING(r13)
2837
Suresh Warrier37f55d32016-08-19 15:35:46 +10002838 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002839 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002840 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002841 cmpdi r3, 0
2842 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002843
Paul Mackerras19ccb762011-07-23 17:42:46 +10002844 /* see if any other thread is already exiting */
2845 lwz r0,VCORE_ENTRY_EXIT(r5)
2846 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002847 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002848
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002849 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002850
2851 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002852kvm_cede_prodded:
2853 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002854 stb r0,VCPU_PRODDED(r3)
2855 sync /* order testing prodded vs. clearing ceded */
2856 stb r0,VCPU_CEDED(r3)
2857 li r3,H_SUCCESS
2858 blr
2859
2860 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002861kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002862 ld r9, HSTATE_KVM_VCPU(r13)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11002863#ifdef CONFIG_KVM_XICS
2864 /* Abort if we still have a pending escalation */
2865 lbz r5, VCPU_XIVE_ESC_ON(r9)
2866 cmpwi r5, 0
2867 beq 1f
2868 li r0, 0
2869 stb r0, VCPU_CEDED(r9)
28701: /* Enable XIVE escalation */
2871 li r5, XIVE_ESB_SET_PQ_00
2872 mfmsr r0
2873 andi. r0, r0, MSR_DR /* in real mode? */
2874 beq 1f
2875 ld r10, VCPU_XIVE_ESC_VADDR(r9)
2876 cmpdi r10, 0
2877 beq 3f
2878 ldx r0, r10, r5
2879 b 2f
28801: ld r10, VCPU_XIVE_ESC_RADDR(r9)
2881 cmpdi r10, 0
2882 beq 3f
2883 ldcix r0, r10, r5
28842: sync
2885 li r0, 1
2886 stb r0, VCPU_XIVE_ESC_ON(r9)
2887#endif /* CONFIG_KVM_XICS */
28883: b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002889
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002890 /* Try to handle a machine check in real mode */
2891machine_check_realmode:
2892 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002893 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002894 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002895 ld r9, HSTATE_KVM_VCPU(r13)
2896 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302897 /*
Aravinda Prasade20bbd32017-05-11 16:33:37 +05302898 * For the guest that is FWNMI capable, deliver all the MCE errors
2899 * (handled/unhandled) by exiting the guest with KVM_EXIT_NMI exit
2900 * reason. This new approach injects machine check errors in guest
2901 * address space to guest with additional information in the form
2902 * of RTAS event, thus enabling guest kernel to suitably handle
2903 * such errors.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302904 *
Aravinda Prasade20bbd32017-05-11 16:33:37 +05302905 * For the guest that is not FWNMI capable (old QEMU) fallback
2906 * to old behaviour for backward compatibility:
2907 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest either
2908 * through machine check interrupt (set HSRR0 to 0x200).
2909 * For handled errors (no-fatal), just go back to guest execution
2910 * with current HSRR0.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302911 * if we receive machine check with MSR(RI=0) then deliver it to
2912 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302913 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302914 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002915 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2916 bne mc_cont /* if so, exit to host */
Aravinda Prasade20bbd32017-05-11 16:33:37 +05302917 /* Check if guest is capable of handling NMI exit */
2918 ld r10, VCPU_KVM(r9)
2919 lbz r10, KVM_FWNMI(r10)
2920 cmpdi r10, 1 /* FWNMI capable? */
2921 beq mc_cont /* if so, exit with KVM_EXIT_NMI. */
2922
2923 /* if not, fall through for backward compatibility. */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302924 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2925 beq 1f /* Deliver a machine check to guest */
2926 ld r10, VCPU_PC(r9)
2927 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302928 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002929 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053029301: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002931 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053029322: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002933
Paul Mackerrasde56a942011-06-29 00:21:34 +00002934/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002935 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002936 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002937 * 0 if nothing needs to be done
2938 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002939 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002940 * -2 if we handled a PCI passthrough interrupt (returned by
2941 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002942 *
2943 * Also sets r12 to the interrupt vector for any interrupt that needs
2944 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002945 * Modifies all volatile registers (since it may call a C function).
2946 * This routine calls kvmppc_read_intr, a C function, if an external
2947 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002948 */
2949kvmppc_check_wake_reason:
2950 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002951BEGIN_FTR_SECTION
2952 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2953FTR_SECTION_ELSE
2954 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2955ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2956 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002957 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002958 li r3, 0
2959 li r12, 0
2960 cmpwi r6, 6 /* was it the decrementer? */
2961 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002962BEGIN_FTR_SECTION
2963 cmpwi r6, 5 /* privileged doorbell? */
2964 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002965 cmpwi r6, 3 /* hypervisor doorbell? */
2966 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002967END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302968 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2969 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002970 li r3, 1 /* anything else, return 1 */
29710: blr
2972
Paul Mackerras5d00f662014-01-08 21:25:28 +11002973 /* hypervisor doorbell */
29743: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302975
2976 /*
2977 * Clear the doorbell as we will invoke the handler
2978 * explicitly in the guest exit path.
2979 */
2980 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2981 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002982 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002983 li r3, 1
Nicholas Piggin2cde3712017-10-10 20:18:28 +10002984BEGIN_FTR_SECTION
2985 PPC_MSGSYNC
2986 lwsync
2987END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11002988 lbz r0, HSTATE_HOST_IPI(r13)
2989 cmpwi r0, 0
2990 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302991 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002992 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002993 blr
2994
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302995 /* Woken up due to Hypervisor maintenance interrupt */
29964: li r12, BOOK3S_INTERRUPT_HMI
2997 li r3, 1
2998 blr
2999
Suresh Warrier37f55d32016-08-19 15:35:46 +10003000 /* external interrupt - create a stack frame so we can call C */
30017: mflr r0
3002 std r0, PPC_LR_STKOFF(r1)
3003 stdu r1, -PPC_MIN_STKFRM(r1)
3004 bl kvmppc_read_intr
3005 nop
3006 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10003007 cmpdi r3, 1
3008 ble 1f
3009
3010 /*
3011 * Return code of 2 means PCI passthrough interrupt, but
3012 * we need to return back to host to complete handling the
3013 * interrupt. Trap reason is expected in r12 by guest
3014 * exit code.
3015 */
3016 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
30171:
Suresh Warrier37f55d32016-08-19 15:35:46 +10003018 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
3019 addi r1, r1, PPC_MIN_STKFRM
3020 mtlr r0
3021 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00003022
3023/*
3024 * Save away FP, VMX and VSX registers.
3025 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11003026 * N.B. r30 and r31 are volatile across this function,
3027 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00003028 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11003029kvmppc_save_fp:
3030 mflr r30
3031 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00003032 mfmsr r5
3033 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00003034#ifdef CONFIG_ALTIVEC
3035BEGIN_FTR_SECTION
3036 oris r8,r8,MSR_VEC@h
3037END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3038#endif
3039#ifdef CONFIG_VSX
3040BEGIN_FTR_SECTION
3041 oris r8,r8,MSR_VSX@h
3042END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3043#endif
3044 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11003045 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003046 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003047#ifdef CONFIG_ALTIVEC
3048BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11003049 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003050 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003051END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3052#endif
3053 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11003054 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11003055 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00003056 blr
3057
3058/*
3059 * Load up FP, VMX and VSX registers
3060 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11003061 * N.B. r30 and r31 are volatile across this function,
3062 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00003063 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00003064kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11003065 mflr r30
3066 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00003067 mfmsr r9
3068 ori r8,r9,MSR_FP
3069#ifdef CONFIG_ALTIVEC
3070BEGIN_FTR_SECTION
3071 oris r8,r8,MSR_VEC@h
3072END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3073#endif
3074#ifdef CONFIG_VSX
3075BEGIN_FTR_SECTION
3076 oris r8,r8,MSR_VSX@h
3077END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3078#endif
3079 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11003080 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003081 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003082#ifdef CONFIG_ALTIVEC
3083BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11003084 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003085 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003086END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3087#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11003088 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003089 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11003090 mtlr r30
3091 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00003092 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10003093
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003094#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
3095/*
3096 * Save transactional state and TM-related registers.
3097 * Called with r9 pointing to the vcpu struct.
3098 * This can modify all checkpointed registers, but
3099 * restores r1, r2 and r9 (vcpu pointer) before exit.
3100 */
3101kvmppc_save_tm:
3102 mflr r0
3103 std r0, PPC_LR_STKOFF(r1)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003104 stdu r1, -PPC_MIN_STKFRM(r1)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003105
3106 /* Turn on TM. */
3107 mfmsr r8
3108 li r0, 1
3109 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
3110 mtmsrd r8
3111
3112 ld r5, VCPU_MSR(r9)
3113 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
3114 beq 1f /* TM not active in guest. */
3115
3116 std r1, HSTATE_HOST_R1(r13)
3117 li r3, TM_CAUSE_KVM_RESCHED
3118
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003119BEGIN_FTR_SECTION
3120 /* Emulation of the treclaim instruction needs TEXASR before treclaim */
3121 mfspr r6, SPRN_TEXASR
3122 std r6, VCPU_ORIG_TEXASR(r9)
3123
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003124 lbz r0, HSTATE_FAKE_SUSPEND(r13) /* Were we fake suspended? */
3125 cmpwi r0, 0
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003126 beq 3f
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003127 rldicl. r8, r8, 64 - MSR_TS_S_LG, 62 /* Did we actually hrfid? */
3128 beq 4f
3129BEGIN_FTR_SECTION_NESTED(96)
3130 bl pnv_power9_force_smt4_catch
3131END_FTR_SECTION_NESTED(CPU_FTR_P9_TM_XER_SO_BUG, CPU_FTR_P9_TM_XER_SO_BUG, 96)
3132 nop
31333:
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003134END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
3135
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003136 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
3137 li r5, 0
3138 mtmsrd r5, 1
3139
3140 /* All GPRs are volatile at this point. */
3141 TRECLAIM(R3)
3142
3143 /* Temporarily store r13 and r9 so we have some regs to play with */
3144 SET_SCRATCH0(r13)
3145 GET_PACA(r13)
3146 std r9, PACATMSCRATCH(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003147
3148 /* If doing TM emulation on POWER9 DD2.2, check for fake suspend mode */
3149BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003150 lbz r9, HSTATE_FAKE_SUSPEND(r13)
3151 cmpwi r9, 0
3152 beq 2f
3153 /*
3154 * We were in fake suspend, so we are not going to save the
3155 * register state as the guest checkpointed state (since
3156 * we already have it), therefore we can now use any volatile GPR.
3157 */
3158 /* Reload stack pointer and TOC. */
3159 ld r1, HSTATE_HOST_R1(r13)
3160 ld r2, PACATOC(r13)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003161 /* Set MSR RI now we have r1 and r13 back. */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003162 li r5, MSR_RI
3163 mtmsrd r5, 1
3164 HMT_MEDIUM
3165 ld r6, HSTATE_DSCR(r13)
3166 mtspr SPRN_DSCR, r6
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003167BEGIN_FTR_SECTION_NESTED(96)
3168 bl pnv_power9_force_smt4_release
3169END_FTR_SECTION_NESTED(CPU_FTR_P9_TM_XER_SO_BUG, CPU_FTR_P9_TM_XER_SO_BUG, 96)
3170 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
3178 ld r9, HSTATE_KVM_VCPU(r13)
3179 b 1f
31802:
3181END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
3182
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003183 ld r9, HSTATE_KVM_VCPU(r13)
3184
3185 /* Get a few more GPRs free. */
3186 std r29, VCPU_GPRS_TM(29)(r9)
3187 std r30, VCPU_GPRS_TM(30)(r9)
3188 std r31, VCPU_GPRS_TM(31)(r9)
3189
3190 /* Save away PPR and DSCR soon so don't run with user values. */
3191 mfspr r31, SPRN_PPR
3192 HMT_MEDIUM
3193 mfspr r30, SPRN_DSCR
3194 ld r29, HSTATE_DSCR(r13)
3195 mtspr SPRN_DSCR, r29
3196
3197 /* Save all but r9, r13 & r29-r31 */
3198 reg = 0
3199 .rept 29
3200 .if (reg != 9) && (reg != 13)
3201 std reg, VCPU_GPRS_TM(reg)(r9)
3202 .endif
3203 reg = reg + 1
3204 .endr
3205 /* ... now save r13 */
3206 GET_SCRATCH0(r4)
3207 std r4, VCPU_GPRS_TM(13)(r9)
3208 /* ... and save r9 */
3209 ld r4, PACATMSCRATCH(r13)
3210 std r4, VCPU_GPRS_TM(9)(r9)
3211
3212 /* Reload stack pointer and TOC. */
3213 ld r1, HSTATE_HOST_R1(r13)
3214 ld r2, PACATOC(r13)
3215
3216 /* Set MSR RI now we have r1 and r13 back. */
3217 li r5, MSR_RI
3218 mtmsrd r5, 1
3219
3220 /* Save away checkpinted SPRs. */
3221 std r31, VCPU_PPR_TM(r9)
3222 std r30, VCPU_DSCR_TM(r9)
3223 mflr r5
3224 mfcr r6
3225 mfctr r7
3226 mfspr r8, SPRN_AMR
3227 mfspr r10, SPRN_TAR
Paul Mackerras0d808df2016-11-07 15:09:58 +11003228 mfxer r11
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003229 std r5, VCPU_LR_TM(r9)
3230 stw r6, VCPU_CR_TM(r9)
3231 std r7, VCPU_CTR_TM(r9)
3232 std r8, VCPU_AMR_TM(r9)
3233 std r10, VCPU_TAR_TM(r9)
Paul Mackerras0d808df2016-11-07 15:09:58 +11003234 std r11, VCPU_XER_TM(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003235
3236 /* Restore r12 as trap number. */
3237 lwz r12, VCPU_TRAP(r9)
3238
3239 /* Save FP/VSX. */
3240 addi r3, r9, VCPU_FPRS_TM
3241 bl store_fp_state
3242 addi r3, r9, VCPU_VRS_TM
3243 bl store_vr_state
3244 mfspr r6, SPRN_VRSAVE
3245 stw r6, VCPU_VRSAVE_TM(r9)
32461:
3247 /*
3248 * We need to save these SPRs after the treclaim so that the software
3249 * error code is recorded correctly in the TEXASR. Also the user may
3250 * change these outside of a transaction, so they must always be
3251 * context switched.
3252 */
3253 mfspr r5, SPRN_TFHAR
3254 mfspr r6, SPRN_TFIAR
3255 mfspr r7, SPRN_TEXASR
3256 std r5, VCPU_TFHAR(r9)
3257 std r6, VCPU_TFIAR(r9)
3258 std r7, VCPU_TEXASR(r9)
3259
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003260 addi r1, r1, PPC_MIN_STKFRM
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003261 ld r0, PPC_LR_STKOFF(r1)
3262 mtlr r0
3263 blr
3264
3265/*
3266 * Restore transactional state and TM-related registers.
3267 * Called with r4 pointing to the vcpu struct.
3268 * This potentially modifies all checkpointed registers.
3269 * It restores r1, r2, r4 from the PACA.
3270 */
3271kvmppc_restore_tm:
3272 mflr r0
3273 std r0, PPC_LR_STKOFF(r1)
3274
3275 /* Turn on TM/FP/VSX/VMX so we can restore them. */
3276 mfmsr r5
3277 li r6, MSR_TM >> 32
3278 sldi r6, r6, 32
3279 or r5, r5, r6
3280 ori r5, r5, MSR_FP
3281 oris r5, r5, (MSR_VEC | MSR_VSX)@h
3282 mtmsrd r5
3283
3284 /*
3285 * The user may change these outside of a transaction, so they must
3286 * always be context switched.
3287 */
3288 ld r5, VCPU_TFHAR(r4)
3289 ld r6, VCPU_TFIAR(r4)
3290 ld r7, VCPU_TEXASR(r4)
3291 mtspr SPRN_TFHAR, r5
3292 mtspr SPRN_TFIAR, r6
3293 mtspr SPRN_TEXASR, r7
3294
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003295 li r0, 0
3296 stb r0, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003297 ld r5, VCPU_MSR(r4)
3298 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
3299 beqlr /* TM not active in guest */
3300 std r1, HSTATE_HOST_R1(r13)
3301
3302 /* Make sure the failure summary is set, otherwise we'll program check
3303 * when we trechkpt. It's possible that this might have been not set
3304 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
3305 * host.
3306 */
3307 oris r7, r7, (TEXASR_FS)@h
3308 mtspr SPRN_TEXASR, r7
3309
3310 /*
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003311 * If we are doing TM emulation for the guest on a POWER9 DD2,
3312 * then we don't actually do a trechkpt -- we either set up
3313 * fake-suspend mode, or emulate a TM rollback.
3314 */
3315BEGIN_FTR_SECTION
3316 b .Ldo_tm_fake_load
3317END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
3318
3319 /*
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003320 * We need to load up the checkpointed state for the guest.
3321 * We need to do this early as it will blow away any GPRs, VSRs and
3322 * some SPRs.
3323 */
3324
3325 mr r31, r4
3326 addi r3, r31, VCPU_FPRS_TM
3327 bl load_fp_state
3328 addi r3, r31, VCPU_VRS_TM
3329 bl load_vr_state
3330 mr r4, r31
3331 lwz r7, VCPU_VRSAVE_TM(r4)
3332 mtspr SPRN_VRSAVE, r7
3333
3334 ld r5, VCPU_LR_TM(r4)
3335 lwz r6, VCPU_CR_TM(r4)
3336 ld r7, VCPU_CTR_TM(r4)
3337 ld r8, VCPU_AMR_TM(r4)
3338 ld r9, VCPU_TAR_TM(r4)
Paul Mackerras0d808df2016-11-07 15:09:58 +11003339 ld r10, VCPU_XER_TM(r4)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003340 mtlr r5
3341 mtcr r6
3342 mtctr r7
3343 mtspr SPRN_AMR, r8
3344 mtspr SPRN_TAR, r9
Paul Mackerras0d808df2016-11-07 15:09:58 +11003345 mtxer r10
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003346
3347 /*
3348 * Load up PPR and DSCR values but don't put them in the actual SPRs
3349 * till the last moment to avoid running with userspace PPR and DSCR for
3350 * too long.
3351 */
3352 ld r29, VCPU_DSCR_TM(r4)
3353 ld r30, VCPU_PPR_TM(r4)
3354
3355 std r2, PACATMSCRATCH(r13) /* Save TOC */
3356
3357 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
3358 li r5, 0
3359 mtmsrd r5, 1
3360
3361 /* Load GPRs r0-r28 */
3362 reg = 0
3363 .rept 29
3364 ld reg, VCPU_GPRS_TM(reg)(r31)
3365 reg = reg + 1
3366 .endr
3367
3368 mtspr SPRN_DSCR, r29
3369 mtspr SPRN_PPR, r30
3370
3371 /* Load final GPRs */
3372 ld 29, VCPU_GPRS_TM(29)(r31)
3373 ld 30, VCPU_GPRS_TM(30)(r31)
3374 ld 31, VCPU_GPRS_TM(31)(r31)
3375
3376 /* TM checkpointed state is now setup. All GPRs are now volatile. */
3377 TRECHKPT
3378
3379 /* Now let's get back the state we need. */
3380 HMT_MEDIUM
3381 GET_PACA(r13)
3382 ld r29, HSTATE_DSCR(r13)
3383 mtspr SPRN_DSCR, r29
3384 ld r4, HSTATE_KVM_VCPU(r13)
3385 ld r1, HSTATE_HOST_R1(r13)
3386 ld r2, PACATMSCRATCH(r13)
3387
3388 /* Set the MSR RI since we have our registers back. */
3389 li r5, MSR_RI
3390 mtmsrd r5, 1
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +110033919:
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003392 ld r0, PPC_LR_STKOFF(r1)
3393 mtlr r0
3394 blr
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003395
3396.Ldo_tm_fake_load:
3397 cmpwi r5, 1 /* check for suspended state */
3398 bgt 10f
3399 stb r5, HSTATE_FAKE_SUSPEND(r13)
3400 b 9b /* and return */
340110: stdu r1, -PPC_MIN_STKFRM(r1)
3402 /* guest is in transactional state, so simulate rollback */
3403 mr r3, r4
3404 bl kvmhv_emulate_tm_rollback
3405 nop
3406 ld r4, HSTATE_KVM_VCPU(r13) /* our vcpu pointer has been trashed */
3407 addi r1, r1, PPC_MIN_STKFRM
3408 b 9b
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003409#endif
3410
Paul Mackerras44a3add2013-10-04 21:45:04 +10003411/*
3412 * We come here if we get any exception or interrupt while we are
3413 * executing host real mode code while in guest MMU context.
Paul Mackerras857b99e2017-09-01 16:17:27 +10003414 * r12 is (CR << 32) | vector
3415 * r13 points to our PACA
3416 * r12 is saved in HSTATE_SCRATCH0(r13)
3417 * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
3418 * r9 is saved in HSTATE_SCRATCH2(r13)
3419 * r13 is saved in HSPRG1
3420 * cfar is saved in HSTATE_CFAR(r13)
3421 * ppr is saved in HSTATE_PPR(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10003422 */
3423kvmppc_bad_host_intr:
Paul Mackerras857b99e2017-09-01 16:17:27 +10003424 /*
3425 * Switch to the emergency stack, but start half-way down in
3426 * case we were already on it.
3427 */
3428 mr r9, r1
3429 std r1, PACAR1(r13)
3430 ld r1, PACAEMERGSP(r13)
3431 subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
3432 std r9, 0(r1)
3433 std r0, GPR0(r1)
3434 std r9, GPR1(r1)
3435 std r2, GPR2(r1)
3436 SAVE_4GPRS(3, r1)
3437 SAVE_2GPRS(7, r1)
3438 srdi r0, r12, 32
3439 clrldi r12, r12, 32
3440 std r0, _CCR(r1)
3441 std r12, _TRAP(r1)
3442 andi. r0, r12, 2
3443 beq 1f
3444 mfspr r3, SPRN_HSRR0
3445 mfspr r4, SPRN_HSRR1
3446 mfspr r5, SPRN_HDAR
3447 mfspr r6, SPRN_HDSISR
3448 b 2f
34491: mfspr r3, SPRN_SRR0
3450 mfspr r4, SPRN_SRR1
3451 mfspr r5, SPRN_DAR
3452 mfspr r6, SPRN_DSISR
34532: std r3, _NIP(r1)
3454 std r4, _MSR(r1)
3455 std r5, _DAR(r1)
3456 std r6, _DSISR(r1)
3457 ld r9, HSTATE_SCRATCH2(r13)
3458 ld r12, HSTATE_SCRATCH0(r13)
3459 GET_SCRATCH0(r0)
3460 SAVE_4GPRS(9, r1)
3461 std r0, GPR13(r1)
3462 SAVE_NVGPRS(r1)
3463 ld r5, HSTATE_CFAR(r13)
3464 std r5, ORIG_GPR3(r1)
3465 mflr r3
3466#ifdef CONFIG_RELOCATABLE
3467 ld r4, HSTATE_SCRATCH1(r13)
3468#else
3469 mfctr r4
3470#endif
3471 mfxer r5
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +05303472 lbz r6, PACAIRQSOFTMASK(r13)
Paul Mackerras857b99e2017-09-01 16:17:27 +10003473 std r3, _LINK(r1)
3474 std r4, _CTR(r1)
3475 std r5, _XER(r1)
3476 std r6, SOFTE(r1)
3477 ld r2, PACATOC(r13)
3478 LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
3479 std r3, STACK_FRAME_OVERHEAD-16(r1)
3480
3481 /*
3482 * On POWER9 do a minimal restore of the MMU and call C code,
3483 * which will print a message and panic.
3484 * XXX On POWER7 and POWER8, we just spin here since we don't
3485 * know what the other threads are doing (and we don't want to
3486 * coordinate with them) - but at least we now have register state
3487 * in memory that we might be able to look at from another CPU.
3488 */
3489BEGIN_FTR_SECTION
Paul Mackerras44a3add2013-10-04 21:45:04 +10003490 b .
Paul Mackerras857b99e2017-09-01 16:17:27 +10003491END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
3492 ld r9, HSTATE_KVM_VCPU(r13)
3493 ld r10, VCPU_KVM(r9)
3494
3495 li r0, 0
3496 mtspr SPRN_AMR, r0
3497 mtspr SPRN_IAMR, r0
3498 mtspr SPRN_CIABR, r0
3499 mtspr SPRN_DAWRX, r0
3500
3501 /* Flush the ERAT on radix P9 DD1 guest exit */
3502BEGIN_FTR_SECTION
3503 PPC_INVALIDATE_ERAT
3504END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
3505
3506BEGIN_MMU_FTR_SECTION
3507 b 4f
3508END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
3509
3510 slbmte r0, r0
3511 slbia
3512 ptesync
3513 ld r8, PACA_SLBSHADOWPTR(r13)
3514 .rept SLB_NUM_BOLTED
3515 li r3, SLBSHADOW_SAVEAREA
3516 LDX_BE r5, r8, r3
3517 addi r3, r3, 8
3518 LDX_BE r6, r8, r3
3519 andis. r7, r5, SLB_ESID_V@h
3520 beq 3f
3521 slbmte r6, r5
35223: addi r8, r8, 16
3523 .endr
3524
35254: lwz r7, KVM_HOST_LPID(r10)
3526 mtspr SPRN_LPID, r7
3527 mtspr SPRN_PID, r0
3528 ld r8, KVM_HOST_LPCR(r10)
3529 mtspr SPRN_LPCR, r8
3530 isync
3531 li r0, KVM_GUEST_MODE_NONE
3532 stb r0, HSTATE_IN_GUEST(r13)
3533
3534 /*
3535 * Turn on the MMU and jump to C code
3536 */
3537 bcl 20, 31, .+4
35385: mflr r3
3539 addi r3, r3, 9f - 5b
3540 ld r4, PACAKMSR(r13)
3541 mtspr SPRN_SRR0, r3
3542 mtspr SPRN_SRR1, r4
Nicholas Piggin222f20f2018-01-10 03:07:15 +11003543 RFI_TO_KERNEL
Paul Mackerras857b99e2017-09-01 16:17:27 +100035449: addi r3, r1, STACK_FRAME_OVERHEAD
3545 bl kvmppc_bad_interrupt
3546 b 9b
Michael Neulinge4e38122014-03-25 10:47:02 +11003547
3548/*
3549 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3550 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3551 * r11 has the guest MSR value (in/out)
3552 * r9 has a vcpu pointer (in)
3553 * r0 is used as a scratch register
3554 */
3555kvmppc_msr_interrupt:
3556 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3557 cmpwi r0, 2 /* Check if we are in transactional state.. */
3558 ld r11, VCPU_INTR_MSR(r9)
3559 bne 1f
3560 /* ... if transactional, change to suspended */
3561 li r0, 1
35621: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3563 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003564
3565/*
3566 * This works around a hardware bug on POWER8E processors, where
3567 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3568 * performance monitor interrupt. Instead, when we need to have
3569 * an interrupt pending, we have to arrange for a counter to overflow.
3570 */
3571kvmppc_fix_pmao:
3572 li r3, 0
3573 mtspr SPRN_MMCR2, r3
3574 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3575 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3576 mtspr SPRN_MMCR0, r3
3577 lis r3, 0x7fff
3578 ori r3, r3, 0xffff
3579 mtspr SPRN_PMC6, r3
3580 isync
3581 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003582
3583#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3584/*
3585 * Start timing an activity
3586 * r3 = pointer to time accumulation struct, r4 = vcpu
3587 */
3588kvmhv_start_timing:
3589 ld r5, HSTATE_KVM_VCORE(r13)
3590 lbz r6, VCORE_IN_GUEST(r5)
3591 cmpwi r6, 0
3592 beq 5f /* if in guest, need to */
3593 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
35945: mftb r5
3595 subf r5, r6, r5
3596 std r3, VCPU_CUR_ACTIVITY(r4)
3597 std r5, VCPU_ACTIVITY_START(r4)
3598 blr
3599
3600/*
3601 * Accumulate time to one activity and start another.
3602 * r3 = pointer to new time accumulation struct, r4 = vcpu
3603 */
3604kvmhv_accumulate_time:
3605 ld r5, HSTATE_KVM_VCORE(r13)
3606 lbz r8, VCORE_IN_GUEST(r5)
3607 cmpwi r8, 0
3608 beq 4f /* if in guest, need to */
3609 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
36104: ld r5, VCPU_CUR_ACTIVITY(r4)
3611 ld r6, VCPU_ACTIVITY_START(r4)
3612 std r3, VCPU_CUR_ACTIVITY(r4)
3613 mftb r7
3614 subf r7, r8, r7
3615 std r7, VCPU_ACTIVITY_START(r4)
3616 cmpdi r5, 0
3617 beqlr
3618 subf r3, r6, r7
3619 ld r8, TAS_SEQCOUNT(r5)
3620 cmpdi r8, 0
3621 addi r8, r8, 1
3622 std r8, TAS_SEQCOUNT(r5)
3623 lwsync
3624 ld r7, TAS_TOTAL(r5)
3625 add r7, r7, r3
3626 std r7, TAS_TOTAL(r5)
3627 ld r6, TAS_MIN(r5)
3628 ld r7, TAS_MAX(r5)
3629 beq 3f
3630 cmpd r3, r6
3631 bge 1f
36323: std r3, TAS_MIN(r5)
36331: cmpd r3, r7
3634 ble 2f
3635 std r3, TAS_MAX(r5)
36362: lwsync
3637 addi r8, r8, 1
3638 std r8, TAS_SEQCOUNT(r5)
3639 blr
3640#endif