blob: 9d2fea43b0c5780bd85511a7e81ffc5b920f93a2 [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>
Paul Mackerras41f4e632018-10-08 16:30:51 +110031#include <asm/export.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110032#include <asm/tm.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053033#include <asm/opal.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100034#include <asm/xive-regs.h>
Paul Mackerras857b99e2017-09-01 16:17:27 +100035#include <asm/thread_info.h>
Christophe Leroyec0c4642018-07-05 16:24:57 +000036#include <asm/asm-compat.h>
Christophe Leroy2c86cd12018-07-05 16:25:01 +000037#include <asm/feature-fixups.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110038
Paul Mackerras2f272462017-05-22 16:25:14 +100039/* Sign-extend HDEC if not on POWER9 */
40#define EXTEND_HDEC(reg) \
41BEGIN_FTR_SECTION; \
42 extsw reg, reg; \
43END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
44
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110045/* Values in HSTATE_NAPPING(r13) */
46#define NAPPING_CEDE 1
47#define NAPPING_NOVCPU 2
48
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100049/* Stack frame offsets for kvmppc_hv_entry */
Paul Mackerras95a64322018-10-08 16:30:55 +110050#define SFS 208
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100051#define STACK_SLOT_TRAP (SFS-4)
Paul Mackerras95a64322018-10-08 16:30:55 +110052#define STACK_SLOT_SHORT_PATH (SFS-8)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100053#define STACK_SLOT_TID (SFS-16)
54#define STACK_SLOT_PSSCR (SFS-24)
55#define STACK_SLOT_PID (SFS-32)
56#define STACK_SLOT_IAMR (SFS-40)
57#define STACK_SLOT_CIABR (SFS-48)
58#define STACK_SLOT_DAWR (SFS-56)
59#define STACK_SLOT_DAWRX (SFS-64)
Paul Mackerras769377f2017-02-15 14:30:17 +110060#define STACK_SLOT_HFSCR (SFS-72)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +110061#define STACK_SLOT_AMR (SFS-80)
62#define STACK_SLOT_UAMOR (SFS-88)
Paul Mackerras95a64322018-10-08 16:30:55 +110063/* the following is used by the P9 short path */
64#define STACK_SLOT_NVGPRS (SFS-152) /* 18 gprs */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100065
Paul Mackerrasde56a942011-06-29 00:21:34 +000066/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100067 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000068 * Must be called with interrupts hard-disabled.
69 *
70 * Input Registers:
71 *
72 * LR = return address to continue at after eventually re-enabling MMU
73 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100074_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100075 mflr r0
76 std r0, PPC_LR_STKOFF(r1)
77 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000078 mfmsr r10
Paul Mackerras8b24e692017-06-26 15:45:51 +100079 std r10, HSTATE_HOST_MSR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100080 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000081 li r0,MSR_RI
82 andc r0,r10,r0
83 li r6,MSR_IR | MSR_DR
84 andc r6,r10,r6
85 mtmsrd r0,1 /* clear RI in MSR */
86 mtsrr0 r5
87 mtsrr1 r6
Nicholas Piggin222f20f2018-01-10 03:07:15 +110088 RFI_TO_KERNEL
Paul Mackerrasde56a942011-06-29 00:21:34 +000089
Paul Mackerras218309b2013-09-06 13:23:44 +100090kvmppc_call_hv_entry:
Paul Mackerrasc0101502017-10-19 14:11:23 +110091BEGIN_FTR_SECTION
92 /* On P9, do LPCR setting, if necessary */
93 ld r3, HSTATE_SPLIT_MODE(r13)
94 cmpdi r3, 0
95 beq 46f
96 lwz r4, KVM_SPLIT_DO_SET(r3)
97 cmpwi r4, 0
98 beq 46f
99 bl kvmhv_p9_set_lpcr
100 nop
10146:
102END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
103
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100104 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000105 bl kvmppc_hv_entry
106
107 /* Back from guest - restore host state and return to caller */
108
Michael Neulingeee7ff92014-01-08 21:25:19 +1100109BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +1000110 /* Restore host DABR and DABRX */
111 ld r5,HSTATE_DABR(r13)
112 li r6,7
113 mtspr SPRN_DABR,r5
114 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +1100115END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000116
117 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -0500118 ld r3,PACA_SPRG_VDSO(r13)
119 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +1000120
Paul Mackerras218309b2013-09-06 13:23:44 +1000121 /* Reload the host's PMU registers */
Paul Mackerras41f4e632018-10-08 16:30:51 +1100122 bl kvmhv_load_host_pmu
Paul Mackerras218309b2013-09-06 13:23:44 +1000123
124 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100125 * Reload DEC. HDEC interrupts were disabled when
126 * we reloaded the host's LPCR value.
127 */
128 ld r3, HSTATE_DECEXP(r13)
129 mftb r4
130 subf r4, r4, r3
131 mtspr SPRN_DEC, r4
132
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000133 /* hwthread_req may have got set by cede or no vcpu, so clear it */
134 li r0, 0
135 stb r0, HSTATE_HWTHREAD_REQ(r13)
136
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100137 /*
Aravinda Prasade20bbd32017-05-11 16:33:37 +0530138 * For external interrupts we need to call the Linux
139 * handler to process the interrupt. We do that by jumping
140 * to absolute address 0x500 for external interrupts.
141 * The [h]rfid at the end of the handler will return to
142 * the book3s_hv_interrupts.S code. For other interrupts
143 * we do the rfid to get back to the book3s_hv_interrupts.S
144 * code here.
Paul Mackerras218309b2013-09-06 13:23:44 +1000145 */
146 ld r8, 112+PPC_LR_STKOFF(r1)
147 addi r1, r1, 112
148 ld r7, HSTATE_HOST_MSR(r13)
149
Paul Mackerras8b24e692017-06-26 15:45:51 +1000150 /* Return the trap number on this thread as the return value */
151 mr r3, r12
152
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100153 /*
154 * If we came back from the guest via a relocation-on interrupt,
155 * we will be in virtual mode at this point, which makes it a
156 * little easier to get back to the caller.
157 */
158 mfmsr r0
159 andi. r0, r0, MSR_IR /* in real mode? */
160 bne .Lvirt_return
161
Paul Mackerras8b24e692017-06-26 15:45:51 +1000162 /* RFI into the highmem handler */
Paul Mackerras218309b2013-09-06 13:23:44 +1000163 mfmsr r6
164 li r0, MSR_RI
165 andc r6, r6, r0
166 mtmsrd r6, 1 /* Clear RI in MSR */
167 mtsrr0 r8
168 mtsrr1 r7
Nicholas Piggin222f20f2018-01-10 03:07:15 +1100169 RFI_TO_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000170
Paul Mackerras8b24e692017-06-26 15:45:51 +1000171 /* Virtual-mode return */
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100172.Lvirt_return:
Paul Mackerras8b24e692017-06-26 15:45:51 +1000173 mtlr r8
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100174 blr
175
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100176kvmppc_primary_no_guest:
177 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100178 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
Paul Mackerras2f272462017-05-22 16:25:14 +1000179 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
180 /* HDEC value came from DEC in the first place, it will fit */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100181 mfspr r3, SPRN_HDEC
182 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100183 /*
184 * Make sure the primary has finished the MMU switch.
185 * We should never get here on a secondary thread, but
186 * check it for robustness' sake.
187 */
188 ld r5, HSTATE_KVM_VCORE(r13)
18965: lbz r0, VCORE_IN_GUEST(r5)
190 cmpwi r0, 0
191 beq 65b
192 /* Set LPCR. */
193 ld r8,VCORE_LPCR(r5)
194 mtspr SPRN_LPCR,r8
195 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100196 /* set our bit in napping_threads */
197 ld r5, HSTATE_KVM_VCORE(r13)
198 lbz r7, HSTATE_PTID(r13)
199 li r0, 1
200 sld r0, r0, r7
201 addi r6, r5, VCORE_NAPPING_THREADS
2021: lwarx r3, 0, r6
203 or r3, r3, r0
204 stwcx. r3, 0, r6
205 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100206 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100207 isync
208 li r12, 0
209 lwz r7, VCORE_ENTRY_EXIT(r5)
210 cmpwi r7, 0x100
211 bge kvm_novcpu_exit /* another thread already exiting */
212 li r3, NAPPING_NOVCPU
213 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100214
Paul Mackerrasccc07772015-03-28 14:21:07 +1100215 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100216 b kvm_do_nap
217
Suresh Warrier37f55d32016-08-19 15:35:46 +1000218/*
219 * kvm_novcpu_wakeup
220 * Entered from kvm_start_guest if kvm_hstate.napping is set
221 * to NAPPING_NOVCPU
222 * r2 = kernel TOC
223 * r13 = paca
224 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100225kvm_novcpu_wakeup:
226 ld r1, HSTATE_HOST_R1(r13)
227 ld r5, HSTATE_KVM_VCORE(r13)
228 li r0, 0
229 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100230
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100231 /* check the wake reason */
232 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100233
Suresh Warrier37f55d32016-08-19 15:35:46 +1000234 /*
235 * Restore volatile registers since we could have called
236 * a C routine in kvmppc_check_wake_reason.
237 * r5 = VCORE
238 */
239 ld r5, HSTATE_KVM_VCORE(r13)
240
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100241 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100242 lwz r0, VCORE_ENTRY_EXIT(r5)
243 cmpwi r0, 0x100
244 bge kvm_novcpu_exit
245
246 /* clear our bit in napping_threads */
247 lbz r7, HSTATE_PTID(r13)
248 li r0, 1
249 sld r0, r0, r7
250 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002514: lwarx r7, 0, r6
252 andc r7, r7, r0
253 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100254 bne 4b
255
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100256 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100257 cmpdi r3, 0
258 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100259
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100260 /* See if our timeslice has expired (HDEC is negative) */
261 mfspr r0, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000262 EXTEND_HDEC(r0)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100263 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerras2f272462017-05-22 16:25:14 +1000264 cmpdi r0, 0
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100265 blt kvm_novcpu_exit
266
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100267 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
268 ld r4, HSTATE_KVM_VCPU(r13)
269 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100270 beq kvmppc_primary_no_guest
271
272#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
273 addi r3, r4, VCPU_TB_RMENTRY
274 bl kvmhv_start_timing
275#endif
276 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100277
278kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100279#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
280 ld r4, HSTATE_KVM_VCPU(r13)
281 cmpdi r4, 0
282 beq 13f
283 addi r3, r4, VCPU_TB_RMEXIT
284 bl kvmhv_accumulate_time
285#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110028613: mr r3, r12
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000287 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerraseddb60f2015-03-28 14:21:11 +1100288 bl kvmhv_commence_exit
289 nop
Paul Mackerras6af27c82015-03-28 14:21:10 +1100290 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100291
Paul Mackerras371fefd2011-06-29 00:23:08 +0000292/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100293 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000294 * Relocation is off and most register values are lost.
295 * r13 points to the PACA.
Nicholas Piggin9d292502017-06-13 23:05:51 +1000296 * r3 contains the SRR1 wakeup value, SRR1 is trashed.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000297 */
298 .globl kvm_start_guest
299kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530300 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100301 mfspr r0, SPRN_CTRLF
302 ori r0, r0, 1
303 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530304
Nicholas Piggin9d292502017-06-13 23:05:51 +1000305 /*
306 * Could avoid this and pass it through in r3. For now,
307 * code expects it to be in SRR1.
308 */
309 mtspr SPRN_SRR1,r3
310
Paul Mackerras19ccb762011-07-23 17:42:46 +1000311 ld r2,PACATOC(r13)
312
Naveen N. Raoa4bc64d2018-04-19 12:34:05 +0530313 li r0,0
314 stb r0,PACA_FTRACE_ENABLED(r13)
315
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000316 li r0,KVM_HWTHREAD_IN_KVM
317 stb r0,HSTATE_HWTHREAD_STATE(r13)
318
319 /* NV GPR values from power7_idle() will no longer be valid */
320 li r0,1
321 stb r0,PACA_NAPSTATELOST(r13)
322
Paul Mackerras4619ac82013-04-17 20:31:41 +0000323 /* were we napping due to cede? */
324 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100325 cmpwi r0,NAPPING_CEDE
326 beq kvm_end_cede
327 cmpwi r0,NAPPING_NOVCPU
328 beq kvm_novcpu_wakeup
329
330 ld r1,PACAEMERGSP(r13)
331 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000332
333 /*
334 * We weren't napping due to cede, so this must be a secondary
335 * thread being woken up to run a guest, or being woken up due
336 * to a stray IPI. (Or due to some machine check or hypervisor
337 * maintenance interrupt while the core is in KVM.)
338 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000339
340 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100341 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000342 /*
343 * kvmppc_check_wake_reason could invoke a C routine, but we
344 * have no volatile registers to restore when we return.
345 */
346
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100347 cmpdi r3, 0
348 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000349
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000350 /* get vcore pointer, NULL if we have nothing to run */
351 ld r5,HSTATE_KVM_VCORE(r13)
352 cmpdi r5,0
353 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000354 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000355
Paul Mackerras56548fc2014-12-03 14:48:40 +1100356kvm_secondary_got_guest:
357
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100358 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530359 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100360 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000361
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000362 /* On thread 0 of a subcore, set HDEC to max */
363 lbz r4, HSTATE_PTID(r13)
364 cmpwi r4, 0
365 bne 63f
Paul Mackerras2f272462017-05-22 16:25:14 +1000366 LOAD_REG_ADDR(r6, decrementer_max)
367 ld r6, 0(r6)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000368 mtspr SPRN_HDEC, r6
369 /* and set per-LPAR registers, if doing dynamic micro-threading */
370 ld r6, HSTATE_SPLIT_MODE(r13)
371 cmpdi r6, 0
372 beq 63f
Paul Mackerrasc0101502017-10-19 14:11:23 +1100373BEGIN_FTR_SECTION
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000374 ld r0, KVM_SPLIT_RPR(r6)
375 mtspr SPRN_RPR, r0
376 ld r0, KVM_SPLIT_PMMAR(r6)
377 mtspr SPRN_PMMAR, r0
378 ld r0, KVM_SPLIT_LDBAR(r6)
379 mtspr SPRN_LDBAR, r0
380 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100381FTR_SECTION_ELSE
382 /* On P9 we use the split_info for coordinating LPCR changes */
383 lwz r4, KVM_SPLIT_DO_SET(r6)
384 cmpwi r4, 0
Alexander Grafd20fe502018-02-08 18:38:53 +0100385 beq 1f
Paul Mackerrasc0101502017-10-19 14:11:23 +1100386 mr r3, r6
387 bl kvmhv_p9_set_lpcr
388 nop
Alexander Grafd20fe502018-02-08 18:38:53 +01003891:
Paul Mackerrasc0101502017-10-19 14:11:23 +1100390ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasb4deba52015-07-02 20:38:16 +100039163:
392 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100393 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000394 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100395 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000396
397 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000398 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000399 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000400 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100401 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000402 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100403 * kvmppc_run_core() is going to assume that all our vcpu
404 * state is visible in memory. This lwsync makes sure
405 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100406 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000407 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000408 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000409
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530410 /*
411 * All secondaries exiting guest will fall through this path.
412 * Before proceeding, just check for HMI interrupt and
413 * invoke opal hmi handler. By now we are sure that the
414 * primary thread on this core/subcore has already made partition
415 * switch/TB resync and we are good to call opal hmi handler.
416 */
417 cmpwi r12, BOOK3S_INTERRUPT_HMI
418 bne kvm_no_guest
419
420 li r3,0 /* NULL argument */
421 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100422/*
423 * At this point we have finished executing in the guest.
424 * We need to wait for hwthread_req to become zero, since
425 * we may not turn on the MMU while hwthread_req is non-zero.
426 * While waiting we also need to check if we get given a vcpu to run.
427 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000428kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100429 lbz r3, HSTATE_HWTHREAD_REQ(r13)
430 cmpwi r3, 0
431 bne 53f
432 HMT_MEDIUM
433 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000434 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100435 /* need to recheck hwthread_req after a barrier, to avoid race */
436 sync
437 lbz r3, HSTATE_HWTHREAD_REQ(r13)
438 cmpwi r3, 0
439 bne 54f
440/*
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530441 * We jump to pnv_wakeup_loss, which will return to the caller
Paul Mackerras56548fc2014-12-03 14:48:40 +1100442 * of power7_nap in the powernv cpu offline loop. The value we
Nicholas Piggin9d292502017-06-13 23:05:51 +1000443 * put in r3 becomes the return value for power7_nap. pnv_wakeup_loss
444 * requires SRR1 in r12.
Paul Mackerras56548fc2014-12-03 14:48:40 +1100445 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000446 li r3, LPCR_PECE0
447 mfspr r4, SPRN_LPCR
448 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
449 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100450 li r3, 0
Nicholas Piggin9d292502017-06-13 23:05:51 +1000451 mfspr r12,SPRN_SRR1
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530452 b pnv_wakeup_loss
Paul Mackerras56548fc2014-12-03 14:48:40 +1100453
45453: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000455 ld r5, HSTATE_KVM_VCORE(r13)
456 cmpdi r5, 0
457 bne 60f
458 ld r3, HSTATE_SPLIT_MODE(r13)
459 cmpdi r3, 0
460 beq kvm_no_guest
Paul Mackerrasc0101502017-10-19 14:11:23 +1100461 lwz r0, KVM_SPLIT_DO_SET(r3)
462 cmpwi r0, 0
463 bne kvmhv_do_set
464 lwz r0, KVM_SPLIT_DO_RESTORE(r3)
465 cmpwi r0, 0
466 bne kvmhv_do_restore
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000467 lbz r0, KVM_SPLIT_DO_NAP(r3)
468 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100469 beq kvm_no_guest
470 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000471 b kvm_unsplit_nap
47260: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100473 b kvm_secondary_got_guest
474
47554: li r0, KVM_HWTHREAD_IN_KVM
476 stb r0, HSTATE_HWTHREAD_STATE(r13)
477 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000478
Paul Mackerrasc0101502017-10-19 14:11:23 +1100479kvmhv_do_set:
480 /* Set LPCR, LPIDR etc. on P9 */
481 HMT_MEDIUM
482 bl kvmhv_p9_set_lpcr
483 nop
484 b kvm_no_guest
485
486kvmhv_do_restore:
487 HMT_MEDIUM
488 bl kvmhv_p9_restore_lpcr
489 nop
490 b kvm_no_guest
491
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000492/*
493 * Here the primary thread is trying to return the core to
494 * whole-core mode, so we need to nap.
495 */
496kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530497 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530498 * When secondaries are napping in kvm_unsplit_nap() with
499 * hwthread_req = 1, HMI goes ignored even though subcores are
500 * already exited the guest. Hence HMI keeps waking up secondaries
501 * from nap in a loop and secondaries always go back to nap since
502 * no vcore is assigned to them. This makes impossible for primary
503 * thread to get hold of secondary threads resulting into a soft
504 * lockup in KVM path.
505 *
506 * Let us check if HMI is pending and handle it before we go to nap.
507 */
508 cmpwi r12, BOOK3S_INTERRUPT_HMI
509 bne 55f
510 li r3, 0 /* NULL argument */
511 bl hmi_exception_realmode
51255:
513 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530514 * Ensure that secondary doesn't nap when it has
515 * its vcore pointer set.
516 */
517 sync /* matches smp_mb() before setting split_info.do_nap */
518 ld r0, HSTATE_KVM_VCORE(r13)
519 cmpdi r0, 0
520 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000521 /* clear any pending message */
522BEGIN_FTR_SECTION
523 lis r6, (PPC_DBELL_SERVER << (63-36))@h
524 PPC_MSGCLR(6)
525END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
526 /* Set kvm_split_mode.napped[tid] = 1 */
527 ld r3, HSTATE_SPLIT_MODE(r13)
528 li r0, 1
Paul Mackerrasc0101502017-10-19 14:11:23 +1100529 lbz r4, HSTATE_TID(r13)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000530 addi r4, r4, KVM_SPLIT_NAPPED
531 stbx r0, r3, r4
532 /* Check the do_nap flag again after setting napped[] */
533 sync
534 lbz r0, KVM_SPLIT_DO_NAP(r3)
535 cmpwi r0, 0
536 beq 57f
537 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
Paul Mackerrasbf53c882016-11-18 14:34:07 +1100538 mfspr r5, SPRN_LPCR
539 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
540 b kvm_nap_sequence
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000541
54257: li r0, 0
543 stbx r0, r3, r4
544 b kvm_no_guest
545
Paul Mackerras218309b2013-09-06 13:23:44 +1000546/******************************************************************************
547 * *
548 * Entry code *
549 * *
550 *****************************************************************************/
551
Paul Mackerrasde56a942011-06-29 00:21:34 +0000552.global kvmppc_hv_entry
553kvmppc_hv_entry:
554
555 /* Required state:
556 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100557 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000558 * MSR = ~IR|DR
559 * R13 = PACA
560 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000561 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000562 * all other volatile GPRS = free
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100563 * Does not preserve non-volatile GPRs or CR fields
Paul Mackerrasde56a942011-06-29 00:21:34 +0000564 */
565 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000566 std r0, PPC_LR_STKOFF(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000567 stdu r1, -SFS(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000568
Paul Mackerrasde56a942011-06-29 00:21:34 +0000569 /* Save R1 in the PACA */
570 std r1, HSTATE_HOST_R1(r13)
571
Paul Mackerras44a3add2013-10-04 21:45:04 +1000572 li r6, KVM_GUEST_MODE_HOST_HV
573 stb r6, HSTATE_IN_GUEST(r13)
574
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100575#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
576 /* Store initial timestamp */
577 cmpdi r4, 0
578 beq 1f
579 addi r3, r4, VCPU_TB_RMENTRY
580 bl kvmhv_start_timing
5811:
582#endif
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100583
584 /* Use cr7 as an indication of radix mode */
585 ld r5, HSTATE_KVM_VCORE(r13)
586 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
587 lbz r0, KVM_RADIX(r9)
588 cmpwi cr7, r0, 0
589
Paul Mackerras9e368f22011-06-29 00:40:08 +0000590 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100591 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000592 * We don't have to lock against concurrent tlbies,
593 * but we do have to coordinate across hardware threads.
594 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100595 /* Set bit in entry map iff exit map is zero. */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100596 li r7, 1
597 lbz r6, HSTATE_PTID(r13)
598 sld r7, r7, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100599 addi r8, r5, VCORE_ENTRY_EXIT
60021: lwarx r3, 0, r8
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100601 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000602 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100603 or r3, r3, r7
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100604 stwcx. r3, 0, r8
Paul Mackerras371fefd2011-06-29 00:23:08 +0000605 bne 21b
606
607 /* Primary thread switches to guest partition. */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000608 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100609 bne 10f
Nicholas Piggin9a4506e2018-05-17 17:06:29 +1000610
611 /* Radix has already switched LPID and flushed core TLB */
612 bne cr7, 22f
613
Paul Mackerrasde56a942011-06-29 00:21:34 +0000614 lwz r7,KVM_LPID(r9)
Paul Mackerras7a840842016-11-16 22:25:20 +1100615BEGIN_FTR_SECTION
616 ld r6,KVM_SDR1(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000617 li r0,LPID_RSVD /* switch to reserved LPID */
618 mtspr SPRN_LPID,r0
619 ptesync
620 mtspr SPRN_SDR1,r6 /* switch to partition page table */
Paul Mackerras7a840842016-11-16 22:25:20 +1100621END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000622 mtspr SPRN_LPID,r7
623 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000624
Nicholas Piggin9a4506e2018-05-17 17:06:29 +1000625 /* See if we need to flush the TLB. Hash has to be done in RM */
Paul Mackerras2940ba02019-04-29 19:00:40 +1000626 mr r3, r9 /* kvm pointer */
627 bl kvmppc_hpt_check_need_tlb_flush
628 nop
629 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000630
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000631 /* Add timebase offset onto timebase */
63222: ld r8,VCORE_TB_OFFSET(r5)
633 cmpdi r8,0
634 beq 37f
Paul Mackerras57b8daa2018-04-20 22:51:11 +1000635 std r8, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000636 mftb r6 /* current host timebase */
637 add r8,r8,r6
638 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
639 mftb r7 /* check if lower 24 bits overflowed */
640 clrldi r6,r6,40
641 clrldi r7,r7,40
642 cmpld r7,r6
643 bge 37f
644 addis r8,r8,0x100 /* if so, increment upper 40 bits */
645 mtspr SPRN_TBU40,r8
646
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000647 /* Load guest PCR value to select appropriate compat mode */
64837: ld r7, VCORE_PCR(r5)
649 cmpdi r7, 0
650 beq 38f
651 mtspr SPRN_PCR, r7
65238:
Michael Neulingb005255e2014-01-08 21:25:21 +1100653
654BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000655 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100656 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000657 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100658 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000659 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100660END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
661
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530662 /* Mark the subcore state as inside guest */
663 bl kvmppc_subcore_enter_guest
664 nop
665 ld r5, HSTATE_KVM_VCORE(r13)
666 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000667 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000668 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000669
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100670 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110067110: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100672 beq kvmppc_primary_no_guest
673kvmppc_got_guest:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100674 /* Increment yield count if they have a VPA */
675 ld r3, VCPU_VPA(r4)
676 cmpdi r3, 0
677 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200678 li r6, LPPACA_YIELDCOUNT
679 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100680 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200681 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100682 li r6, 1
683 stb r6, VCPU_VPA_DIRTY(r4)
68425:
685
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100686 /* Save purr/spurr */
687 mfspr r5,SPRN_PURR
688 mfspr r6,SPRN_SPURR
689 std r5,HSTATE_PURR(r13)
690 std r6,HSTATE_SPURR(r13)
691 ld r7,VCPU_PURR(r4)
692 ld r8,VCPU_SPURR(r4)
693 mtspr SPRN_PURR,r7
694 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100695
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100696 /* Save host values of some registers */
697BEGIN_FTR_SECTION
698 mfspr r5, SPRN_TIDR
699 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100700 mfspr r7, SPRN_PID
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100701 std r5, STACK_SLOT_TID(r1)
702 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100703 std r7, STACK_SLOT_PID(r1)
Paul Mackerras769377f2017-02-15 14:30:17 +1100704 mfspr r5, SPRN_HFSCR
705 std r5, STACK_SLOT_HFSCR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100706END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000707BEGIN_FTR_SECTION
708 mfspr r5, SPRN_CIABR
709 mfspr r6, SPRN_DAWR
710 mfspr r7, SPRN_DAWRX
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100711 mfspr r8, SPRN_IAMR
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000712 std r5, STACK_SLOT_CIABR(r1)
713 std r6, STACK_SLOT_DAWR(r1)
714 std r7, STACK_SLOT_DAWRX(r1)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100715 std r8, STACK_SLOT_IAMR(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000716END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100717
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100718 mfspr r5, SPRN_AMR
719 std r5, STACK_SLOT_AMR(r1)
720 mfspr r6, SPRN_UAMOR
721 std r6, STACK_SLOT_UAMOR(r1)
722
Michael Neulingeee7ff92014-01-08 21:25:19 +1100723BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000724 /* Set partition DABR */
725 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100726 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000727 ld r6,VCPU_DABR(r4)
728 mtspr SPRN_DABRX,r5
729 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000730 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100731END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000732
Michael Neulinge4e38122014-03-25 10:47:02 +1100733#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100734/*
735 * Branch around the call if both CPU_FTR_TM and
736 * CPU_FTR_P9_TM_HV_ASSIST are off.
737 */
Michael Neulinge4e38122014-03-25 10:47:02 +1100738BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100739 b 91f
740END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000741 /*
Paul Mackerras7854f752018-10-08 16:30:53 +1100742 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000743 */
Simon Guo6f597c62018-05-23 15:01:48 +0800744 mr r3, r4
745 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +1100746 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +1000747 bl kvmppc_restore_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +1100748 nop
Simon Guo6f597c62018-05-23 15:01:48 +0800749 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +110075091:
Michael Neulinge4e38122014-03-25 10:47:02 +1100751#endif
752
Paul Mackerras41f4e632018-10-08 16:30:51 +1100753 /* Load guest PMU registers; r4 = vcpu pointer here */
754 mr r3, r4
755 bl kvmhv_load_guest_pmu
Paul Mackerrasde56a942011-06-29 00:21:34 +0000756
757 /* Load up FP, VMX and VSX registers */
Paul Mackerras41f4e632018-10-08 16:30:51 +1100758 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000759 bl kvmppc_load_fp
760
761 ld r14, VCPU_GPR(R14)(r4)
762 ld r15, VCPU_GPR(R15)(r4)
763 ld r16, VCPU_GPR(R16)(r4)
764 ld r17, VCPU_GPR(R17)(r4)
765 ld r18, VCPU_GPR(R18)(r4)
766 ld r19, VCPU_GPR(R19)(r4)
767 ld r20, VCPU_GPR(R20)(r4)
768 ld r21, VCPU_GPR(R21)(r4)
769 ld r22, VCPU_GPR(R22)(r4)
770 ld r23, VCPU_GPR(R23)(r4)
771 ld r24, VCPU_GPR(R24)(r4)
772 ld r25, VCPU_GPR(R25)(r4)
773 ld r26, VCPU_GPR(R26)(r4)
774 ld r27, VCPU_GPR(R27)(r4)
775 ld r28, VCPU_GPR(R28)(r4)
776 ld r29, VCPU_GPR(R29)(r4)
777 ld r30, VCPU_GPR(R30)(r4)
778 ld r31, VCPU_GPR(R31)(r4)
779
Paul Mackerrasde56a942011-06-29 00:21:34 +0000780 /* Switch DSCR to guest value */
781 ld r5, VCPU_DSCR(r4)
782 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000783
Michael Neulingb005255e2014-01-08 21:25:21 +1100784BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100785 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100786 b 8f
787END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100788 /* Load up POWER8-specific registers */
789 ld r5, VCPU_IAMR(r4)
790 lwz r6, VCPU_PSPB(r4)
791 ld r7, VCPU_FSCR(r4)
792 mtspr SPRN_IAMR, r5
793 mtspr SPRN_PSPB, r6
794 mtspr SPRN_FSCR, r7
795 ld r5, VCPU_DAWR(r4)
796 ld r6, VCPU_DAWRX(r4)
797 ld r7, VCPU_CIABR(r4)
798 ld r8, VCPU_TAR(r4)
Michael Neulingb53221e2018-03-27 15:37:22 +1100799 /*
800 * Handle broken DAWR case by not writing it. This means we
801 * can still store the DAWR register for migration.
802 */
803BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +1100804 mtspr SPRN_DAWR, r5
805 mtspr SPRN_DAWRX, r6
Michael Neulingb53221e2018-03-27 15:37:22 +1100806END_FTR_SECTION_IFSET(CPU_FTR_DAWR)
Michael Neulingb005255e2014-01-08 21:25:21 +1100807 mtspr SPRN_CIABR, r7
808 mtspr SPRN_TAR, r8
809 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100810 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000811 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100812 mtspr SPRN_EBBHR, r8
813 ld r5, VCPU_EBBRR(r4)
814 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100815 lwz r7, VCPU_GUEST_PID(r4)
816 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100817 mtspr SPRN_EBBRR, r5
818 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100819 mtspr SPRN_PID, r7
820 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100821BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100822 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100823 ld r5, VCPU_TCSCR(r4)
824 ld r6, VCPU_ACOP(r4)
825 ld r7, VCPU_CSIGR(r4)
826 ld r8, VCPU_TACR(r4)
827 mtspr SPRN_TCSCR, r5
828 mtspr SPRN_ACOP, r6
829 mtspr SPRN_CSIGR, r7
830 mtspr SPRN_TACR, r8
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100831 nop
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100832FTR_SECTION_ELSE
833 /* POWER9-only registers */
834 ld r5, VCPU_TID(r4)
835 ld r6, VCPU_PSSCR(r4)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100836 lbz r8, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100837 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100838 rldimi r6, r8, PSSCR_FAKE_SUSPEND_LG, 63 - PSSCR_FAKE_SUSPEND_LG
Paul Mackerras769377f2017-02-15 14:30:17 +1100839 ld r7, VCPU_HFSCR(r4)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100840 mtspr SPRN_TIDR, r5
841 mtspr SPRN_PSSCR, r6
Paul Mackerras769377f2017-02-15 14:30:17 +1100842 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100843ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11008448:
845
Paul Mackerrasde56a942011-06-29 00:21:34 +0000846 ld r5, VCPU_SPRG0(r4)
847 ld r6, VCPU_SPRG1(r4)
848 ld r7, VCPU_SPRG2(r4)
849 ld r8, VCPU_SPRG3(r4)
850 mtspr SPRN_SPRG0, r5
851 mtspr SPRN_SPRG1, r6
852 mtspr SPRN_SPRG2, r7
853 mtspr SPRN_SPRG3, r8
854
Paul Mackerrasde56a942011-06-29 00:21:34 +0000855 /* Load up DAR and DSISR */
856 ld r5, VCPU_DAR(r4)
857 lwz r6, VCPU_DSISR(r4)
858 mtspr SPRN_DAR, r5
859 mtspr SPRN_DSISR, r6
860
Paul Mackerrasde56a942011-06-29 00:21:34 +0000861 /* Restore AMR and UAMOR, set AMOR to all 1s */
862 ld r5,VCPU_AMR(r4)
863 ld r6,VCPU_UAMOR(r4)
864 li r7,-1
865 mtspr SPRN_AMR,r5
866 mtspr SPRN_UAMOR,r6
867 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000868
869 /* Restore state of CTRL run bit; assume 1 on entry */
870 lwz r5,VCPU_CTRL(r4)
871 andi. r5,r5,1
872 bne 4f
873 mfspr r6,SPRN_CTRLF
874 clrrdi r6,r6,1
875 mtspr SPRN_CTRLT,r6
8764:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100877 /* Secondary threads wait for primary to have done partition switch */
878 ld r5, HSTATE_KVM_VCORE(r13)
879 lbz r6, HSTATE_PTID(r13)
880 cmpwi r6, 0
881 beq 21f
882 lbz r0, VCORE_IN_GUEST(r5)
883 cmpwi r0, 0
884 bne 21f
885 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100088620: lwz r3, VCORE_ENTRY_EXIT(r5)
887 cmpwi r3, 0x100
888 bge no_switch_exit
889 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100890 cmpwi r0, 0
891 beq 20b
892 HMT_MEDIUM
89321:
894 /* Set LPCR. */
895 ld r8,VCORE_LPCR(r5)
896 mtspr SPRN_LPCR,r8
897 isync
898
Paul Mackerras57b8daa2018-04-20 22:51:11 +1000899 /*
900 * Set the decrementer to the guest decrementer.
901 */
902 ld r8,VCPU_DEC_EXPIRES(r4)
903 /* r8 is a host timebase value here, convert to guest TB */
904 ld r5,HSTATE_KVM_VCORE(r13)
905 ld r6,VCORE_TB_OFFSET_APPL(r5)
906 add r8,r8,r6
907 mftb r7
908 subf r3,r7,r8
909 mtspr SPRN_DEC,r3
910
Paul Mackerras6af27c82015-03-28 14:21:10 +1100911 /* Check if HDEC expires soon */
912 mfspr r3, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000913 EXTEND_HDEC(r3)
914 cmpdi r3, 512 /* 1 microsecond */
Paul Mackerras6af27c82015-03-28 14:21:10 +1100915 blt hdec_soon
916
Paul Mackerras6964e6a2018-01-11 14:51:02 +1100917 /* For hash guest, clear out and reload the SLB */
918 ld r6, VCPU_KVM(r4)
919 lbz r0, KVM_RADIX(r6)
920 cmpwi r0, 0
921 bne 9f
922 li r6, 0
923 slbmte r6, r6
924 slbia
925 ptesync
926
927 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
928 lwz r5,VCPU_SLB_MAX(r4)
929 cmpwi r5,0
930 beq 9f
931 mtctr r5
932 addi r6,r4,VCPU_SLB
9331: ld r8,VCPU_SLB_E(r6)
934 ld r9,VCPU_SLB_V(r6)
935 slbmte r9,r8
936 addi r6,r6,VCPU_SLB_SIZE
937 bdnz 1b
9389:
939
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000940#ifdef CONFIG_KVM_XICS
941 /* We are entering the guest on that thread, push VCPU to XIVE */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000942 ld r11, VCPU_XIVE_SAVED_STATE(r4)
943 li r9, TM_QW1_OS
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000944 lwz r8, VCPU_XIVE_CAM_WORD(r4)
945 li r7, TM_QW1_OS + TM_WORD2
946 mfmsr r0
947 andi. r0, r0, MSR_DR /* in real mode? */
948 beq 2f
949 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
950 cmpldi cr1, r10, 0
951 beq cr1, no_xive
952 eieio
953 stdx r11,r9,r10
954 stwx r8,r7,r10
955 b 3f
9562: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
957 cmpldi cr1, r10, 0
958 beq cr1, no_xive
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000959 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +1100960 stdcix r11,r9,r10
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000961 stwcix r8,r7,r10
9623: li r9, 1
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +1100963 stb r9, VCPU_XIVE_PUSHED(r4)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +1100964 eieio
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +1100965
966 /*
967 * We clear the irq_pending flag. There is a small chance of a
968 * race vs. the escalation interrupt happening on another
969 * processor setting it again, but the only consequence is to
970 * cause a spurrious wakeup on the next H_CEDE which is not an
971 * issue.
972 */
973 li r0,0
974 stb r0, VCPU_IRQ_PENDING(r4)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100975
976 /*
977 * In single escalation mode, if the escalation interrupt is
978 * on, we mask it.
979 */
980 lbz r0, VCPU_XIVE_ESC_ON(r4)
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000981 cmpwi cr1, r0,0
982 beq cr1, 1f
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100983 li r9, XIVE_ESB_SET_PQ_01
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000984 beq 4f /* in real mode? */
985 ld r10, VCPU_XIVE_ESC_VADDR(r4)
986 ldx r0, r10, r9
987 b 5f
9884: ld r10, VCPU_XIVE_ESC_RADDR(r4)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100989 ldcix r0, r10, r9
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +10009905: sync
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100991
992 /* We have a possible subtle race here: The escalation interrupt might
993 * have fired and be on its way to the host queue while we mask it,
994 * and if we unmask it early enough (re-cede right away), there is
995 * a theorical possibility that it fires again, thus landing in the
996 * target queue more than once which is a big no-no.
997 *
998 * Fortunately, solving this is rather easy. If the above load setting
999 * PQ to 01 returns a previous value where P is set, then we know the
1000 * escalation interrupt is somewhere on its way to the host. In that
1001 * case we simply don't clear the xive_esc_on flag below. It will be
1002 * eventually cleared by the handler for the escalation interrupt.
1003 *
1004 * Then, when doing a cede, we check that flag again before re-enabling
1005 * the escalation interrupt, and if set, we abort the cede.
1006 */
1007 andi. r0, r0, XIVE_ESB_VAL_P
1008 bne- 1f
1009
1010 /* Now P is 0, we can clear the flag */
1011 li r0, 0
1012 stb r0, VCPU_XIVE_ESC_ON(r4)
10131:
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001014no_xive:
1015#endif /* CONFIG_KVM_XICS */
1016
Paul Mackerras95a64322018-10-08 16:30:55 +11001017 li r0, 0
1018 stw r0, STACK_SLOT_SHORT_PATH(r1)
1019
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001020deliver_guest_interrupt: /* r4 = vcpu, r13 = paca */
Paul Mackerrasf7035ce2018-10-08 16:30:50 +11001021 /* Check if we can deliver an external or decrementer interrupt now */
1022 ld r0, VCPU_PENDING_EXC(r4)
1023BEGIN_FTR_SECTION
1024 /* On POWER9, also check for emulated doorbell interrupt */
1025 lbz r3, VCPU_DBELL_REQ(r4)
1026 or r0, r0, r3
1027END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1028 cmpdi r0, 0
1029 beq 71f
1030 mr r3, r4
1031 bl kvmppc_guest_entry_inject_int
1032 ld r4, HSTATE_KVM_VCPU(r13)
103371:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001034 ld r6, VCPU_SRR0(r4)
1035 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001036 mtspr SPRN_SRR0, r6
1037 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001038
Paul Mackerras95a64322018-10-08 16:30:55 +11001039fast_guest_entry_c:
1040 ld r10, VCPU_PC(r4)
1041 ld r11, VCPU_MSR(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001042 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001043 rldicl r11, r11, 63 - MSR_HV_LG, 1
1044 rotldi r11, r11, 1 + MSR_HV_LG
1045 ori r11, r11, MSR_ME
1046
Paul Mackerrasf7035ce2018-10-08 16:30:50 +11001047 ld r6, VCPU_CTR(r4)
1048 ld r7, VCPU_XER(r4)
1049 mtctr r6
1050 mtxer r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10001051
Liu Ping Fan27025a62013-11-19 14:12:48 +08001052/*
1053 * Required state:
1054 * R4 = vcpu
1055 * R10: value for HSRR0
1056 * R11: value for HSRR1
1057 * R13 = PACA
1058 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001059fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001060 li r0,0
1061 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001062 mtspr SPRN_HSRR0,r10
1063 mtspr SPRN_HSRR1,r11
1064
1065 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001066 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001067 stb r9, HSTATE_IN_GUEST(r13)
1068
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001069#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1070 /* Accumulate timing */
1071 addi r3, r4, VCPU_TB_GUEST
1072 bl kvmhv_accumulate_time
1073#endif
1074
Paul Mackerrasde56a942011-06-29 00:21:34 +00001075 /* Enter guest */
1076
Paul Mackerras0acb9112013-02-04 18:10:51 +00001077BEGIN_FTR_SECTION
1078 ld r5, VCPU_CFAR(r4)
1079 mtspr SPRN_CFAR, r5
1080END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001081BEGIN_FTR_SECTION
1082 ld r0, VCPU_PPR(r4)
1083END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001084
Paul Mackerrasde56a942011-06-29 00:21:34 +00001085 ld r5, VCPU_LR(r4)
Paul Mackerrasfd0944b2018-10-08 16:30:58 +11001086 ld r6, VCPU_CR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001087 mtlr r5
1088 mtcr r6
1089
Michael Neulingc75df6f2012-06-25 13:33:10 +00001090 ld r1, VCPU_GPR(R1)(r4)
1091 ld r2, VCPU_GPR(R2)(r4)
1092 ld r3, VCPU_GPR(R3)(r4)
1093 ld r5, VCPU_GPR(R5)(r4)
1094 ld r6, VCPU_GPR(R6)(r4)
1095 ld r7, VCPU_GPR(R7)(r4)
1096 ld r8, VCPU_GPR(R8)(r4)
1097 ld r9, VCPU_GPR(R9)(r4)
1098 ld r10, VCPU_GPR(R10)(r4)
1099 ld r11, VCPU_GPR(R11)(r4)
1100 ld r12, VCPU_GPR(R12)(r4)
1101 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001102
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001103BEGIN_FTR_SECTION
1104 mtspr SPRN_PPR, r0
1105END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Michael Neulinge001fa72017-09-15 15:26:14 +10001106
1107/* Move canary into DSISR to check for later */
1108BEGIN_FTR_SECTION
1109 li r0, 0x7fff
1110 mtspr SPRN_HDSISR, r0
1111END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1112
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001113 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001114 ld r4, VCPU_GPR(R4)(r4)
Nicholas Piggin222f20f2018-01-10 03:07:15 +11001115 HRFI_TO_GUEST
Paul Mackerrasde56a942011-06-29 00:21:34 +00001116 b .
1117
Paul Mackerras95a64322018-10-08 16:30:55 +11001118/*
1119 * Enter the guest on a P9 or later system where we have exactly
1120 * one vcpu per vcore and we don't need to go to real mode
1121 * (which implies that host and guest are both using radix MMU mode).
1122 * r3 = vcpu pointer
1123 * Most SPRs and all the VSRs have been loaded already.
1124 */
1125_GLOBAL(__kvmhv_vcpu_entry_p9)
1126EXPORT_SYMBOL_GPL(__kvmhv_vcpu_entry_p9)
1127 mflr r0
1128 std r0, PPC_LR_STKOFF(r1)
1129 stdu r1, -SFS(r1)
1130
1131 li r0, 1
1132 stw r0, STACK_SLOT_SHORT_PATH(r1)
1133
1134 std r3, HSTATE_KVM_VCPU(r13)
1135 mfcr r4
1136 stw r4, SFS+8(r1)
1137
1138 std r1, HSTATE_HOST_R1(r13)
1139
1140 reg = 14
1141 .rept 18
1142 std reg, STACK_SLOT_NVGPRS + ((reg - 14) * 8)(r1)
1143 reg = reg + 1
1144 .endr
1145
1146 reg = 14
1147 .rept 18
1148 ld reg, __VCPU_GPR(reg)(r3)
1149 reg = reg + 1
1150 .endr
1151
1152 mfmsr r10
1153 std r10, HSTATE_HOST_MSR(r13)
1154
1155 mr r4, r3
1156 b fast_guest_entry_c
1157guest_exit_short_path:
1158
1159 li r0, KVM_GUEST_MODE_NONE
1160 stb r0, HSTATE_IN_GUEST(r13)
1161
1162 reg = 14
1163 .rept 18
1164 std reg, __VCPU_GPR(reg)(r9)
1165 reg = reg + 1
1166 .endr
1167
1168 reg = 14
1169 .rept 18
1170 ld reg, STACK_SLOT_NVGPRS + ((reg - 14) * 8)(r1)
1171 reg = reg + 1
1172 .endr
1173
1174 lwz r4, SFS+8(r1)
1175 mtcr r4
1176
1177 mr r3, r12 /* trap number */
1178
1179 addi r1, r1, SFS
1180 ld r0, PPC_LR_STKOFF(r1)
1181 mtlr r0
1182
1183 /* If we are in real mode, do a rfid to get back to the caller */
1184 mfmsr r4
1185 andi. r5, r4, MSR_IR
1186 bnelr
1187 rldicl r5, r4, 64 - MSR_TS_S_LG, 62 /* extract TS field */
1188 mtspr SPRN_SRR0, r0
1189 ld r10, HSTATE_HOST_MSR(r13)
1190 rldimi r10, r5, MSR_TS_S_LG, 63 - MSR_TS_T_LG
1191 mtspr SPRN_SRR1, r10
1192 RFI_TO_KERNEL
1193 b .
1194
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001195secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001196 li r12, 0
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001197 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001198 cmpdi r4, 0
1199 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001200 stw r12, VCPU_TRAP(r4)
1201#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001202 addi r3, r4, VCPU_TB_RMEXIT
1203 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001204#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100120511: b kvmhv_switch_to_host
1206
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001207no_switch_exit:
1208 HMT_MEDIUM
1209 li r12, 0
1210 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001211hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001212 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000121312: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001214 mr r9, r4
1215#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001216 addi r3, r4, VCPU_TB_RMEXIT
1217 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001218#endif
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001219 b guest_bypass
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001220
Paul Mackerrasde56a942011-06-29 00:21:34 +00001221/******************************************************************************
1222 * *
1223 * Exit code *
1224 * *
1225 *****************************************************************************/
1226
1227/*
1228 * We come here from the first-level interrupt handlers.
1229 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301230 .globl kvmppc_interrupt_hv
1231kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001232 /*
1233 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001234 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001235 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001236 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001237 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001238 * guest R13 saved in SPRN_SCRATCH0
1239 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001240 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001241 lbz r9, HSTATE_IN_GUEST(r13)
1242 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1243 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301244#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1245 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001246 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301247 beq kvmppc_interrupt_pr
1248#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001249 /* We're now back in the host but in guest MMU context */
1250 li r9, KVM_GUEST_MODE_HOST_HV
1251 stb r9, HSTATE_IN_GUEST(r13)
1252
Paul Mackerrasde56a942011-06-29 00:21:34 +00001253 ld r9, HSTATE_KVM_VCPU(r13)
1254
1255 /* Save registers */
1256
Michael Neulingc75df6f2012-06-25 13:33:10 +00001257 std r0, VCPU_GPR(R0)(r9)
1258 std r1, VCPU_GPR(R1)(r9)
1259 std r2, VCPU_GPR(R2)(r9)
1260 std r3, VCPU_GPR(R3)(r9)
1261 std r4, VCPU_GPR(R4)(r9)
1262 std r5, VCPU_GPR(R5)(r9)
1263 std r6, VCPU_GPR(R6)(r9)
1264 std r7, VCPU_GPR(R7)(r9)
1265 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001266 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001267 std r0, VCPU_GPR(R9)(r9)
1268 std r10, VCPU_GPR(R10)(r9)
1269 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001270 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001271 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001272 /* CR is in the high half of r12 */
1273 srdi r4, r12, 32
Paul Mackerrasfd0944b2018-10-08 16:30:58 +11001274 std r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001275BEGIN_FTR_SECTION
1276 ld r3, HSTATE_CFAR(r13)
1277 std r3, VCPU_CFAR(r9)
1278END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001279BEGIN_FTR_SECTION
1280 ld r4, HSTATE_PPR(r13)
1281 std r4, VCPU_PPR(r9)
1282END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001283
1284 /* Restore R1/R2 so we can handle faults */
1285 ld r1, HSTATE_HOST_R1(r13)
1286 ld r2, PACATOC(r13)
1287
1288 mfspr r10, SPRN_SRR0
1289 mfspr r11, SPRN_SRR1
1290 std r10, VCPU_SRR0(r9)
1291 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001292 /* trap is in the low half of r12, clear CR from the high half */
1293 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001294 andi. r0, r12, 2 /* need to read HSRR0/1? */
1295 beq 1f
1296 mfspr r10, SPRN_HSRR0
1297 mfspr r11, SPRN_HSRR1
1298 clrrdi r12, r12, 2
12991: std r10, VCPU_PC(r9)
1300 std r11, VCPU_MSR(r9)
1301
1302 GET_SCRATCH0(r3)
1303 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001304 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001305 std r4, VCPU_LR(r9)
1306
Paul Mackerrasde56a942011-06-29 00:21:34 +00001307 stw r12,VCPU_TRAP(r9)
1308
Paul Mackerras8b24e692017-06-26 15:45:51 +10001309 /*
1310 * Now that we have saved away SRR0/1 and HSRR0/1,
1311 * interrupts are recoverable in principle, so set MSR_RI.
1312 * This becomes important for relocation-on interrupts from
1313 * the guest, which we can get in radix mode on POWER9.
1314 */
1315 li r0, MSR_RI
1316 mtmsrd r0, 1
1317
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001318#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1319 addi r3, r9, VCPU_TB_RMINTR
1320 mr r4, r9
1321 bl kvmhv_accumulate_time
1322 ld r5, VCPU_GPR(R5)(r9)
1323 ld r6, VCPU_GPR(R6)(r9)
1324 ld r7, VCPU_GPR(R7)(r9)
1325 ld r8, VCPU_GPR(R8)(r9)
1326#endif
1327
Paul Mackerras4a157d62014-12-03 13:30:39 +11001328 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001329 if this is an HEI (HV emulation interrupt, e40) */
1330 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001331 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001332 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1333 bne 11f
1334 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100133511: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001336
1337 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001338#ifdef CONFIG_RELOCATABLE
1339 ld r3, HSTATE_SCRATCH1(r13)
1340 mtctr r3
1341#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001342 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001343#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001344 mfxer r4
1345 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001346 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001347
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001348 /* Save more register state */
1349 mfdar r3
1350 mfdsisr r4
1351 std r3, VCPU_DAR(r9)
1352 stw r4, VCPU_DSISR(r9)
1353
1354 /* If this is a page table miss then see if it's theirs or ours */
1355 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1356 beq kvmppc_hdsi
1357 std r3, VCPU_FAULT_DAR(r9)
1358 stw r4, VCPU_FAULT_DSISR(r9)
1359 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1360 beq kvmppc_hisi
1361
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001362#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1363 /* For softpatch interrupt, go off and do TM instruction emulation */
1364 cmpwi r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
1365 beq kvmppc_tm_emul
1366#endif
1367
Paul Mackerrasde56a942011-06-29 00:21:34 +00001368 /* See if this is a leftover HDEC interrupt */
1369 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1370 bne 2f
1371 mfspr r3,SPRN_HDEC
Paul Mackerrasa4faf2e2017-08-25 19:52:12 +10001372 EXTEND_HDEC(r3)
1373 cmpdi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001374 mr r4,r9
1375 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000013762:
Paul Mackerras697d3892011-12-12 12:36:37 +00001377 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001378 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1379 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001380
Paul Mackerras66feed62015-03-28 14:21:12 +11001381 /* Hypervisor doorbell - exit only if host IPI flag set */
1382 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1383 bne 3f
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001384BEGIN_FTR_SECTION
1385 PPC_MSGSYNC
Nicholas Piggin2cde3712017-10-10 20:18:28 +10001386 lwsync
Paul Mackerras360cae32018-10-08 16:31:04 +11001387 /* always exit if we're running a nested guest */
1388 ld r0, VCPU_NESTED(r9)
1389 cmpdi r0, 0
1390 bne guest_exit_cont
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001391END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11001392 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301393 cmpwi r0, 0
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001394 beq maybe_reenter_guest
Paul Mackerras66feed62015-03-28 14:21:12 +11001395 b guest_exit_cont
13963:
Paul Mackerras769377f2017-02-15 14:30:17 +11001397 /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1398 cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1399 bne 14f
1400 mfspr r3, SPRN_HFSCR
1401 std r3, VCPU_HFSCR(r9)
1402 b guest_exit_cont
140314:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001404 /* External interrupt ? */
1405 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001406 beq kvmppc_guest_external
Paul Mackerras43ff3f62018-01-11 14:31:43 +11001407 /* See if it is a machine check */
1408 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1409 beq machine_check_realmode
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001410 /* Or a hypervisor maintenance interrupt */
1411 cmpwi r12, BOOK3S_INTERRUPT_HMI
1412 beq hmi_realmode
1413
1414guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
1415
Paul Mackerras43ff3f62018-01-11 14:31:43 +11001416#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1417 addi r3, r9, VCPU_TB_RMEXIT
1418 mr r4, r9
1419 bl kvmhv_accumulate_time
1420#endif
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001421#ifdef CONFIG_KVM_XICS
1422 /* We are exiting, pull the VP from the XIVE */
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001423 lbz r0, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001424 cmpwi cr0, r0, 0
1425 beq 1f
1426 li r7, TM_SPC_PULL_OS_CTX
1427 li r6, TM_QW1_OS
1428 mfmsr r0
Benjamin Herrenschmidt2662efd2018-01-12 13:37:14 +11001429 andi. r0, r0, MSR_DR /* in real mode? */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001430 beq 2f
1431 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1432 cmpldi cr0, r10, 0
1433 beq 1f
1434 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001435 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001436 lwzx r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001437 /* Second load to recover the context state (Words 0 and 1) */
1438 ldx r11, r6, r10
1439 b 3f
14402: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1441 cmpldi cr0, r10, 0
1442 beq 1f
1443 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001444 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001445 lwzcix r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001446 /* Second load to recover the context state (Words 0 and 1) */
1447 ldcix r11, r6, r10
14483: std r11, VCPU_XIVE_SAVED_STATE(r9)
1449 /* Fixup some of the state for the next load */
1450 li r10, 0
1451 li r0, 0xff
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001452 stb r10, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001453 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1454 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001455 eieio
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100014561:
1457#endif /* CONFIG_KVM_XICS */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001458
Paul Mackerras95a64322018-10-08 16:30:55 +11001459 /* If we came in through the P9 short path, go back out to C now */
1460 lwz r0, STACK_SLOT_SHORT_PATH(r1)
1461 cmpwi r0, 0
1462 bne guest_exit_short_path
1463
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001464 /* For hash guest, read the guest SLB and save it away */
1465 ld r5, VCPU_KVM(r9)
1466 lbz r0, KVM_RADIX(r5)
1467 li r5, 0
1468 cmpwi r0, 0
1469 bne 3f /* for radix, save 0 entries */
1470 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1471 mtctr r0
1472 li r6,0
1473 addi r7,r9,VCPU_SLB
14741: slbmfee r8,r6
1475 andis. r0,r8,SLB_ESID_V@h
1476 beq 2f
1477 add r8,r8,r6 /* put index in */
1478 slbmfev r3,r6
1479 std r8,VCPU_SLB_E(r7)
1480 std r3,VCPU_SLB_V(r7)
1481 addi r7,r7,VCPU_SLB_SIZE
1482 addi r5,r5,1
14832: addi r6,r6,1
1484 bdnz 1b
1485 /* Finally clear out the SLB */
1486 li r0,0
1487 slbmte r0,r0
1488 slbia
1489 ptesync
14903: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001491
Paul Mackerrascda4a142018-03-22 09:48:54 +11001492 /* load host SLB entries */
1493BEGIN_MMU_FTR_SECTION
1494 b 0f
1495END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
1496 ld r8,PACA_SLBSHADOWPTR(r13)
1497
1498 .rept SLB_NUM_BOLTED
1499 li r3, SLBSHADOW_SAVEAREA
1500 LDX_BE r5, r8, r3
1501 addi r3, r3, 8
1502 LDX_BE r6, r8, r3
1503 andis. r7,r5,SLB_ESID_V@h
1504 beq 1f
1505 slbmte r6,r5
15061: addi r8,r8,16
1507 .endr
15080:
1509
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001510guest_bypass:
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001511 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001512
1513 /* Save DEC */
1514 /* Do this before kvmhv_commence_exit so we know TB is guest TB */
1515 ld r3, HSTATE_KVM_VCORE(r13)
1516 mfspr r5,SPRN_DEC
1517 mftb r6
1518 /* On P9, if the guest has large decr enabled, don't sign extend */
1519BEGIN_FTR_SECTION
1520 ld r4, VCORE_LPCR(r3)
1521 andis. r4, r4, LPCR_LD@h
1522 bne 16f
1523END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1524 extsw r5,r5
152516: add r5,r5,r6
1526 /* r5 is a guest timebase value here, convert to host TB */
1527 ld r4,VCORE_TB_OFFSET_APPL(r3)
1528 subf r5,r4,r5
1529 std r5,VCPU_DEC_EXPIRES(r9)
1530
Paul Mackerras6af27c82015-03-28 14:21:10 +11001531 /* Increment exit count, poke other threads to exit */
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001532 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001533 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001534 nop
1535 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001536
Paul Mackerrasec257162015-06-24 21:18:03 +10001537 /* Stop others sending VCPU interrupts to this physical CPU */
1538 li r0, -1
1539 stw r0, VCPU_CPU(r9)
1540 stw r0, VCPU_THREAD_CPU(r9)
1541
Paul Mackerrasde56a942011-06-29 00:21:34 +00001542 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001543 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001544 stw r6,VCPU_CTRL(r9)
1545 andi. r0,r6,1
1546 bne 4f
1547 ori r6,r6,1
1548 mtspr SPRN_CTRLT,r6
15494:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001550 /*
1551 * Save the guest PURR/SPURR
1552 */
1553 mfspr r5,SPRN_PURR
1554 mfspr r6,SPRN_SPURR
1555 ld r7,VCPU_PURR(r9)
1556 ld r8,VCPU_SPURR(r9)
1557 std r5,VCPU_PURR(r9)
1558 std r6,VCPU_SPURR(r9)
1559 subf r5,r7,r5
1560 subf r6,r8,r6
1561
1562 /*
1563 * Restore host PURR/SPURR and add guest times
1564 * so that the time in the guest gets accounted.
1565 */
1566 ld r3,HSTATE_PURR(r13)
1567 ld r4,HSTATE_SPURR(r13)
1568 add r3,r3,r5
1569 add r4,r4,r6
1570 mtspr SPRN_PURR,r3
1571 mtspr SPRN_SPURR,r4
1572
Michael Neulingb005255e2014-01-08 21:25:21 +11001573BEGIN_FTR_SECTION
1574 b 8f
1575END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001576 /* Save POWER8-specific registers */
1577 mfspr r5, SPRN_IAMR
1578 mfspr r6, SPRN_PSPB
1579 mfspr r7, SPRN_FSCR
1580 std r5, VCPU_IAMR(r9)
1581 stw r6, VCPU_PSPB(r9)
1582 std r7, VCPU_FSCR(r9)
1583 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001584 mfspr r7, SPRN_TAR
1585 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001586 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001587 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001588 std r8, VCPU_EBBHR(r9)
1589 mfspr r5, SPRN_EBBRR
1590 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001591 mfspr r7, SPRN_PID
1592 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001593 std r5, VCPU_EBBRR(r9)
1594 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001595 stw r7, VCPU_GUEST_PID(r9)
1596 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001597BEGIN_FTR_SECTION
1598 mfspr r5, SPRN_TCSCR
1599 mfspr r6, SPRN_ACOP
1600 mfspr r7, SPRN_CSIGR
1601 mfspr r8, SPRN_TACR
1602 std r5, VCPU_TCSCR(r9)
1603 std r6, VCPU_ACOP(r9)
1604 std r7, VCPU_CSIGR(r9)
1605 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001606FTR_SECTION_ELSE
1607 mfspr r5, SPRN_TIDR
1608 mfspr r6, SPRN_PSSCR
1609 std r5, VCPU_TID(r9)
1610 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1611 rotldi r6, r6, 60
1612 std r6, VCPU_PSSCR(r9)
Paul Mackerras769377f2017-02-15 14:30:17 +11001613 /* Restore host HFSCR value */
1614 ld r7, STACK_SLOT_HFSCR(r1)
1615 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001616ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001617 /*
1618 * Restore various registers to 0, where non-zero values
1619 * set by the guest could disrupt the host.
1620 */
1621 li r0, 0
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001622 mtspr SPRN_PSPB, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001623 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001624BEGIN_FTR_SECTION
1625 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001626 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1627 li r0, 1
1628 sldi r0, r0, 31
1629 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001630END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11001631
Michael Ellermanc3c7470c2019-02-22 13:22:08 +11001632 /* Save and restore AMR, IAMR and UAMOR before turning on the MMU */
1633 ld r8, STACK_SLOT_IAMR(r1)
1634 mtspr SPRN_IAMR, r8
1635
16368: /* Power7 jumps back in here */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001637 mfspr r5,SPRN_AMR
1638 mfspr r6,SPRN_UAMOR
1639 std r5,VCPU_AMR(r9)
1640 std r6,VCPU_UAMOR(r9)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +11001641 ld r5,STACK_SLOT_AMR(r1)
1642 ld r6,STACK_SLOT_UAMOR(r1)
1643 mtspr SPRN_AMR, r5
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001644 mtspr SPRN_UAMOR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001645
Paul Mackerrasde56a942011-06-29 00:21:34 +00001646 /* Switch DSCR back to host value */
1647 mfspr r8, SPRN_DSCR
1648 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001649 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001650 mtspr SPRN_DSCR, r7
1651
1652 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001653 std r14, VCPU_GPR(R14)(r9)
1654 std r15, VCPU_GPR(R15)(r9)
1655 std r16, VCPU_GPR(R16)(r9)
1656 std r17, VCPU_GPR(R17)(r9)
1657 std r18, VCPU_GPR(R18)(r9)
1658 std r19, VCPU_GPR(R19)(r9)
1659 std r20, VCPU_GPR(R20)(r9)
1660 std r21, VCPU_GPR(R21)(r9)
1661 std r22, VCPU_GPR(R22)(r9)
1662 std r23, VCPU_GPR(R23)(r9)
1663 std r24, VCPU_GPR(R24)(r9)
1664 std r25, VCPU_GPR(R25)(r9)
1665 std r26, VCPU_GPR(R26)(r9)
1666 std r27, VCPU_GPR(R27)(r9)
1667 std r28, VCPU_GPR(R28)(r9)
1668 std r29, VCPU_GPR(R29)(r9)
1669 std r30, VCPU_GPR(R30)(r9)
1670 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001671
1672 /* Save SPRGs */
1673 mfspr r3, SPRN_SPRG0
1674 mfspr r4, SPRN_SPRG1
1675 mfspr r5, SPRN_SPRG2
1676 mfspr r6, SPRN_SPRG3
1677 std r3, VCPU_SPRG0(r9)
1678 std r4, VCPU_SPRG1(r9)
1679 std r5, VCPU_SPRG2(r9)
1680 std r6, VCPU_SPRG3(r9)
1681
Paul Mackerras89436332012-03-02 01:38:23 +00001682 /* save FP state */
1683 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001684 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001685
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001686#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001687/*
1688 * Branch around the call if both CPU_FTR_TM and
1689 * CPU_FTR_P9_TM_HV_ASSIST are off.
1690 */
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001691BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001692 b 91f
1693END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001694 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11001695 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001696 */
Simon Guo6f597c62018-05-23 15:01:48 +08001697 mr r3, r9
1698 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11001699 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10001700 bl kvmppc_save_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11001701 nop
Simon Guo6f597c62018-05-23 15:01:48 +08001702 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100170391:
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001704#endif
1705
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001706 /* Increment yield count if they have a VPA */
1707 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1708 cmpdi r8, 0
1709 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001710 li r4, LPPACA_YIELDCOUNT
1711 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001712 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001713 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001714 li r3, 1
1715 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000171625:
1717 /* Save PMU registers if requested */
1718 /* r8 and cr0.eq are live here */
Paul Mackerras41f4e632018-10-08 16:30:51 +11001719 mr r3, r9
1720 li r4, 1
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001721 beq 21f /* if no VPA, save PMU stuff anyway */
Paul Mackerras41f4e632018-10-08 16:30:51 +11001722 lbz r4, LPPACA_PMCINUSE(r8)
172321: bl kvmhv_save_guest_pmu
1724 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001725
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001726 /* Restore host values of some registers */
1727BEGIN_FTR_SECTION
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001728 ld r5, STACK_SLOT_CIABR(r1)
1729 ld r6, STACK_SLOT_DAWR(r1)
1730 ld r7, STACK_SLOT_DAWRX(r1)
1731 mtspr SPRN_CIABR, r5
Michael Neulingb53221e2018-03-27 15:37:22 +11001732 /*
1733 * If the DAWR doesn't work, it's ok to write these here as
1734 * this value should always be zero
1735 */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001736 mtspr SPRN_DAWR, r6
1737 mtspr SPRN_DAWRX, r7
1738END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1739BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001740 ld r5, STACK_SLOT_TID(r1)
1741 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001742 ld r7, STACK_SLOT_PID(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001743 mtspr SPRN_TIDR, r5
1744 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001745 mtspr SPRN_PID, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001746END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001747
1748#ifdef CONFIG_PPC_RADIX_MMU
1749 /*
1750 * Are we running hash or radix ?
1751 */
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001752 ld r5, VCPU_KVM(r9)
1753 lbz r0, KVM_RADIX(r5)
1754 cmpwi cr2, r0, 0
Nicholas Piggin2bf10712018-07-05 18:47:00 +10001755 beq cr2, 2f
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001756
Paul Mackerrasdf158182018-05-17 14:47:59 +10001757 /*
1758 * Radix: do eieio; tlbsync; ptesync sequence in case we
1759 * interrupted the guest between a tlbie and a ptesync.
1760 */
1761 eieio
1762 tlbsync
1763 ptesync
1764
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001765 /* Radix: Handle the case where the guest used an illegal PID */
1766 LOAD_REG_ADDR(r4, mmu_base_pid)
1767 lwz r3, VCPU_GUEST_PID(r9)
1768 lwz r5, 0(r4)
1769 cmpw cr0,r3,r5
1770 blt 2f
1771
1772 /*
1773 * Illegal PID, the HW might have prefetched and cached in the TLB
1774 * some translations for the LPID 0 / guest PID combination which
1775 * Linux doesn't know about, so we need to flush that PID out of
1776 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1777 * the right context.
1778 */
1779 li r0,0
1780 mtspr SPRN_LPID,r0
1781 isync
1782
1783 /* Then do a congruence class local flush */
1784 ld r6,VCPU_KVM(r9)
1785 lwz r0,KVM_TLB_SETS(r6)
1786 mtctr r0
1787 li r7,0x400 /* IS field = 0b01 */
1788 ptesync
1789 sldi r0,r3,32 /* RS has PID */
17901: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
1791 addi r7,r7,0x1000
1792 bdnz 1b
1793 ptesync
1794
Nicholas Piggin2bf10712018-07-05 18:47:00 +100017952:
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001796#endif /* CONFIG_PPC_RADIX_MMU */
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001797
Paul Mackerrasde56a942011-06-29 00:21:34 +00001798 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001799 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001800 * We don't have to lock against tlbies but we do
1801 * have to coordinate the hardware threads.
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001802 * Here STACK_SLOT_TRAP(r1) contains the trap number.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001803 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001804kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001805 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001806 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001807 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1808 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001809 cmpwi r3,0
1810 beq 15f
1811 HMT_LOW
181213: lbz r3,VCORE_IN_GUEST(r5)
1813 cmpwi r3,0
1814 bne 13b
1815 HMT_MEDIUM
1816 b 16f
1817
1818 /* Primary thread waits for all the secondaries to exit guest */
181915: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001820 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001821 clrldi r3,r3,56
1822 cmpw r3,r0
1823 bne 15b
1824 isync
1825
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001826 /* Did we actually switch to the guest at all? */
1827 lbz r6, VCORE_IN_GUEST(r5)
1828 cmpwi r6, 0
1829 beq 19f
1830
Paul Mackerrasde56a942011-06-29 00:21:34 +00001831 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001832 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001833BEGIN_FTR_SECTION
1834 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001835 li r8,LPID_RSVD /* switch to reserved LPID */
1836 mtspr SPRN_LPID,r8
1837 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001838 mtspr SPRN_SDR1,r6 /* switch to host page table */
1839END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001840 mtspr SPRN_LPID,r7
1841 isync
1842
Michael Neulingb005255e2014-01-08 21:25:21 +11001843BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001844 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001845 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001846 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001847 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001848 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001849 /* clear DPDES so we don't get guest doorbells in the host */
1850 li r8, 0
1851 mtspr SPRN_DPDES, r8
1852END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1853
Paul Mackerrasde56a942011-06-29 00:21:34 +00001854 /* Subtract timebase offset from timebase */
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001855 ld r8, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001856 cmpdi r8,0
1857 beq 17f
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001858 li r0, 0
1859 std r0, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001860 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001861 subf r8,r8,r6
1862 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1863 mftb r7 /* check if lower 24 bits overflowed */
1864 clrldi r6,r6,40
1865 clrldi r7,r7,40
1866 cmpld r7,r6
1867 bge 17f
1868 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1869 mtspr SPRN_TBU40,r8
1870
Paul Mackerrasdf709a22018-10-08 16:30:52 +1100187117:
1872 /*
1873 * If this is an HMI, we called kvmppc_realmode_hmi_handler
1874 * above, which may or may not have already called
1875 * kvmppc_subcore_exit_guest. Fortunately, all that
1876 * kvmppc_subcore_exit_guest does is clear a flag, so calling
1877 * it again here is benign even if kvmppc_realmode_hmi_handler
1878 * has already called it.
1879 */
1880 bl kvmppc_subcore_exit_guest
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301881 nop
188230: ld r5,HSTATE_KVM_VCORE(r13)
1883 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1884
Paul Mackerrasde56a942011-06-29 00:21:34 +00001885 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301886 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001887 cmpdi r0, 0
1888 beq 18f
1889 li r0, 0
1890 mtspr SPRN_PCR, r0
189118:
1892 /* Signal secondary CPUs to continue */
1893 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000189419: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001895 mtspr SPRN_HDEC,r8
1896
Paul Mackerrasc0101502017-10-19 14:11:23 +1100189716:
1898BEGIN_FTR_SECTION
1899 /* On POWER9 with HPT-on-radix we need to wait for all other threads */
1900 ld r3, HSTATE_SPLIT_MODE(r13)
1901 cmpdi r3, 0
1902 beq 47f
1903 lwz r8, KVM_SPLIT_DO_RESTORE(r3)
1904 cmpwi r8, 0
1905 beq 47f
Paul Mackerrasc0101502017-10-19 14:11:23 +11001906 bl kvmhv_p9_restore_lpcr
1907 nop
Paul Mackerrasc0101502017-10-19 14:11:23 +11001908 b 48f
190947:
1910END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1911 ld r8,KVM_HOST_LPCR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001912 mtspr SPRN_LPCR,r8
1913 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100191448:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001915#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1916 /* Finish timing, if we have a vcpu */
1917 ld r4, HSTATE_KVM_VCPU(r13)
1918 cmpdi r4, 0
1919 li r3, 0
1920 beq 2f
1921 bl kvmhv_accumulate_time
19222:
1923#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001924 /* Unset guest mode */
1925 li r0, KVM_GUEST_MODE_NONE
1926 stb r0, HSTATE_IN_GUEST(r13)
1927
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001928 lwz r12, STACK_SLOT_TRAP(r1) /* return trap # in r12 */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001929 ld r0, SFS+PPC_LR_STKOFF(r1)
1930 addi r1, r1, SFS
Paul Mackerras218309b2013-09-06 13:23:44 +10001931 mtlr r0
1932 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001933
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001934kvmppc_guest_external:
1935 /* External interrupt, first check for host_ipi. If this is
1936 * set, we know the host wants us out so let's do it now
1937 */
1938 bl kvmppc_read_intr
1939
1940 /*
1941 * Restore the active volatile registers after returning from
1942 * a C function.
1943 */
1944 ld r9, HSTATE_KVM_VCPU(r13)
1945 li r12, BOOK3S_INTERRUPT_EXTERNAL
1946
1947 /*
1948 * kvmppc_read_intr return codes:
1949 *
1950 * Exit to host (r3 > 0)
1951 * 1 An interrupt is pending that needs to be handled by the host
1952 * Exit guest and return to host by branching to guest_exit_cont
1953 *
1954 * 2 Passthrough that needs completion in the host
1955 * Exit guest and return to host by branching to guest_exit_cont
1956 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1957 * to indicate to the host to complete handling the interrupt
1958 *
1959 * Before returning to guest, we check if any CPU is heading out
1960 * to the host and if so, we head out also. If no CPUs are heading
1961 * check return values <= 0.
1962 *
1963 * Return to guest (r3 <= 0)
1964 * 0 No external interrupt is pending
1965 * -1 A guest wakeup IPI (which has now been cleared)
1966 * In either case, we return to guest to deliver any pending
1967 * guest interrupts.
1968 *
1969 * -2 A PCI passthrough external interrupt was handled
1970 * (interrupt was delivered directly to guest)
1971 * Return to guest to deliver any pending guest interrupts.
1972 */
1973
1974 cmpdi r3, 1
1975 ble 1f
1976
1977 /* Return code = 2 */
1978 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1979 stw r12, VCPU_TRAP(r9)
1980 b guest_exit_cont
1981
19821: /* Return code <= 1 */
1983 cmpdi r3, 0
1984 bgt guest_exit_cont
1985
1986 /* Return code <= 0 */
1987maybe_reenter_guest:
1988 ld r5, HSTATE_KVM_VCORE(r13)
1989 lwz r0, VCORE_ENTRY_EXIT(r5)
1990 cmpwi r0, 0x100
1991 mr r4, r9
1992 blt deliver_guest_interrupt
1993 b guest_exit_cont
1994
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001995#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1996/*
1997 * Softpatch interrupt for transactional memory emulation cases
1998 * on POWER9 DD2.2. This is early in the guest exit path - we
1999 * haven't saved registers or done a treclaim yet.
2000 */
2001kvmppc_tm_emul:
2002 /* Save instruction image in HEIR */
2003 mfspr r3, SPRN_HEIR
2004 stw r3, VCPU_HEIR(r9)
2005
2006 /*
2007 * The cases we want to handle here are those where the guest
2008 * is in real suspend mode and is trying to transition to
2009 * transactional mode.
2010 */
2011 lbz r0, HSTATE_FAKE_SUSPEND(r13)
2012 cmpwi r0, 0 /* keep exiting guest if in fake suspend */
2013 bne guest_exit_cont
2014 rldicl r3, r11, 64 - MSR_TS_S_LG, 62
2015 cmpwi r3, 1 /* or if not in suspend state */
2016 bne guest_exit_cont
2017
2018 /* Call C code to do the emulation */
2019 mr r3, r9
2020 bl kvmhv_p9_tm_emulation_early
2021 nop
2022 ld r9, HSTATE_KVM_VCPU(r13)
2023 li r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
2024 cmpwi r3, 0
2025 beq guest_exit_cont /* continue exiting if not handled */
2026 ld r10, VCPU_PC(r9)
2027 ld r11, VCPU_MSR(r9)
2028 b fast_interrupt_c_return /* go back to guest if handled */
2029#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
2030
Paul Mackerras697d3892011-12-12 12:36:37 +00002031/*
2032 * Check whether an HDSI is an HPTE not found fault or something else.
2033 * If it is an HPTE not found fault that is due to the guest accessing
2034 * a page that they have mapped but which we have paged out, then
2035 * we continue on with the guest exit path. In all other cases,
2036 * reflect the HDSI to the guest as a DSI.
2037 */
2038kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002039 ld r3, VCPU_KVM(r9)
2040 lbz r0, KVM_RADIX(r3)
Paul Mackerras697d3892011-12-12 12:36:37 +00002041 mfspr r4, SPRN_HDAR
2042 mfspr r6, SPRN_HDSISR
Michael Neulinge001fa72017-09-15 15:26:14 +10002043BEGIN_FTR_SECTION
2044 /* Look for DSISR canary. If we find it, retry instruction */
2045 cmpdi r6, 0x7fff
2046 beq 6f
2047END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2048 cmpwi r0, 0
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002049 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00002050 /* HPTE not found fault or protection fault? */
2051 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00002052 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002053 andi. r0, r11, MSR_DR /* data relocation enabled? */
2054 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002055BEGIN_FTR_SECTION
2056 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2057 b 4f
2058END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00002059 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002060 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002061 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
2062 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000020634: std r4, VCPU_FAULT_DAR(r9)
2064 stw r6, VCPU_FAULT_DSISR(r9)
2065
2066 /* Search the hash table. */
2067 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002068 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002069 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00002070 ld r9, HSTATE_KVM_VCPU(r13)
2071 ld r10, VCPU_PC(r9)
2072 ld r11, VCPU_MSR(r9)
2073 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
2074 cmpdi r3, 0 /* retry the instruction */
2075 beq 6f
2076 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002077 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00002078 cmpdi r3, -2 /* MMIO emulation; need instr word */
2079 beq 2f
2080
Paul Mackerrascf29b212015-10-27 16:10:20 +11002081 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00002082 ld r4, VCPU_FAULT_DAR(r9)
2083 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110020841: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00002085 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110020867: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00002087 mtspr SPRN_SRR0, r10
2088 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002089 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002090 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002091fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000020926: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10002093 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00002094 mtctr r7
2095 mtxer r8
2096 mr r4, r9
2097 b fast_guest_return
2098
20993: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
2100 ld r5, KVM_VRMA_SLB_V(r5)
2101 b 4b
2102
2103 /* If this is for emulated MMIO, load the instruction word */
21042: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
2105
2106 /* Set guest mode to 'jump over instruction' so if lwz faults
2107 * we'll just continue at the next IP. */
2108 li r0, KVM_GUEST_MODE_SKIP
2109 stb r0, HSTATE_IN_GUEST(r13)
2110
2111 /* Do the access with MSR:DR enabled */
2112 mfmsr r3
2113 ori r4, r3, MSR_DR /* Enable paging for data */
2114 mtmsrd r4
2115 lwz r8, 0(r10)
2116 mtmsrd r3
2117
2118 /* Store the result */
2119 stw r8, VCPU_LAST_INST(r9)
2120
2121 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10002122 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00002123 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002124 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00002125
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002126.Lradix_hdsi:
2127 std r4, VCPU_FAULT_DAR(r9)
2128 stw r6, VCPU_FAULT_DSISR(r9)
2129.Lradix_hisi:
2130 mfspr r5, SPRN_ASDR
2131 std r5, VCPU_FAULT_GPA(r9)
2132 b guest_exit_cont
2133
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002134/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00002135 * Similarly for an HISI, reflect it to the guest as an ISI unless
2136 * it is an HPTE not found fault for a page that we have paged out.
2137 */
2138kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002139 ld r3, VCPU_KVM(r9)
2140 lbz r0, KVM_RADIX(r3)
2141 cmpwi r0, 0
2142 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002143 andis. r0, r11, SRR1_ISI_NOPT@h
2144 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002145 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
2146 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002147BEGIN_FTR_SECTION
2148 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2149 b 4f
2150END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00002151 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002152 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002153 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
2154 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000021554:
2156 /* Search the hash table. */
2157 mr r3, r9 /* vcpu pointer */
2158 mr r4, r10
2159 mr r6, r11
2160 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002161 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00002162 ld r9, HSTATE_KVM_VCPU(r13)
2163 ld r10, VCPU_PC(r9)
2164 ld r11, VCPU_MSR(r9)
2165 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2166 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002167 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002168 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002169 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00002170
Paul Mackerrascf29b212015-10-27 16:10:20 +11002171 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002172 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110021731: li r0, BOOK3S_INTERRUPT_INST_STORAGE
21747: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00002175 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002176 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002177 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002178 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002179
21803: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2181 ld r5, KVM_VRMA_SLB_V(r6)
2182 b 4b
2183
2184/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002185 * Try to handle an hcall in real mode.
2186 * Returns to the guest if we handle it, or continues on up to
2187 * the kernel if we can't (i.e. if we don't have a handler for
2188 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002189 *
2190 * r5 - r8 contain hcall args,
2191 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002192 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002193hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00002194 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002195 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08002196 /* sc 1 from userspace - reflect to guest syscall */
2197 bne sc_1_fast_return
Paul Mackerras360cae32018-10-08 16:31:04 +11002198 /* sc 1 from nested guest - give it to L1 to handle */
2199 ld r0, VCPU_NESTED(r9)
2200 cmpdi r0, 0
2201 bne guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002202 clrrdi r3,r3,2
2203 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002204 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002205 /* See if this hcall is enabled for in-kernel handling */
2206 ld r4, VCPU_KVM(r9)
2207 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2208 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2209 add r4, r4, r0
2210 ld r0, KVM_ENABLED_HCALLS(r4)
2211 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2212 srd r0, r0, r4
2213 andi. r0, r0, 1
2214 beq guest_exit_cont
2215 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002216 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10002217 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002218 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002219 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10002220 add r12,r3,r4
2221 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002222 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002223 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002224 bctrl
2225 cmpdi r3,H_TOO_HARD
2226 beq hcall_real_fallback
2227 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00002228 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002229 ld r10,VCPU_PC(r4)
2230 ld r11,VCPU_MSR(r4)
2231 b fast_guest_return
2232
Liu Ping Fan27025a62013-11-19 14:12:48 +08002233sc_1_fast_return:
2234 mtspr SPRN_SRR0,r10
2235 mtspr SPRN_SRR1,r11
2236 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11002237 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08002238 mr r4,r9
2239 b fast_guest_return
2240
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002241 /* We've attempted a real mode hcall, but it's punted it back
2242 * to userspace. We need to restore some clobbered volatiles
2243 * before resuming the pass-it-to-qemu path */
2244hcall_real_fallback:
2245 li r12,BOOK3S_INTERRUPT_SYSCALL
2246 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002247
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002248 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002249
2250 .globl hcall_real_table
2251hcall_real_table:
2252 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002253 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2254 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2255 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10002256 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2257 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002258 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002259#ifdef CONFIG_SPAPR_TCE_IOMMU
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002260 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002261 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002262#else
2263 .long 0 /* 0x1c */
2264 .long 0 /* 0x20 */
2265#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002266 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002267 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Suraj Jitindar Singheadfb1c2019-03-22 17:05:45 +11002268 .long DOTSYM(kvmppc_rm_h_page_init) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002269 .long 0 /* 0x30 */
2270 .long 0 /* 0x34 */
2271 .long 0 /* 0x38 */
2272 .long 0 /* 0x3c */
2273 .long 0 /* 0x40 */
2274 .long 0 /* 0x44 */
2275 .long 0 /* 0x48 */
2276 .long 0 /* 0x4c */
2277 .long 0 /* 0x50 */
2278 .long 0 /* 0x54 */
2279 .long 0 /* 0x58 */
2280 .long 0 /* 0x5c */
2281 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002282#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002283 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2284 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2285 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002286 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002287 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002288#else
2289 .long 0 /* 0x64 - H_EOI */
2290 .long 0 /* 0x68 - H_CPPR */
2291 .long 0 /* 0x6c - H_IPI */
2292 .long 0 /* 0x70 - H_IPOLL */
2293 .long 0 /* 0x74 - H_XIRR */
2294#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002295 .long 0 /* 0x78 */
2296 .long 0 /* 0x7c */
2297 .long 0 /* 0x80 */
2298 .long 0 /* 0x84 */
2299 .long 0 /* 0x88 */
2300 .long 0 /* 0x8c */
2301 .long 0 /* 0x90 */
2302 .long 0 /* 0x94 */
2303 .long 0 /* 0x98 */
2304 .long 0 /* 0x9c */
2305 .long 0 /* 0xa0 */
2306 .long 0 /* 0xa4 */
2307 .long 0 /* 0xa8 */
2308 .long 0 /* 0xac */
2309 .long 0 /* 0xb0 */
2310 .long 0 /* 0xb4 */
2311 .long 0 /* 0xb8 */
2312 .long 0 /* 0xbc */
2313 .long 0 /* 0xc0 */
2314 .long 0 /* 0xc4 */
2315 .long 0 /* 0xc8 */
2316 .long 0 /* 0xcc */
2317 .long 0 /* 0xd0 */
2318 .long 0 /* 0xd4 */
2319 .long 0 /* 0xd8 */
2320 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002321 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002322 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002323 .long 0 /* 0xe8 */
2324 .long 0 /* 0xec */
2325 .long 0 /* 0xf0 */
2326 .long 0 /* 0xf4 */
2327 .long 0 /* 0xf8 */
2328 .long 0 /* 0xfc */
2329 .long 0 /* 0x100 */
2330 .long 0 /* 0x104 */
2331 .long 0 /* 0x108 */
2332 .long 0 /* 0x10c */
2333 .long 0 /* 0x110 */
2334 .long 0 /* 0x114 */
2335 .long 0 /* 0x118 */
2336 .long 0 /* 0x11c */
2337 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002338 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002339 .long 0 /* 0x128 */
2340 .long 0 /* 0x12c */
2341 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002342 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002343#ifdef CONFIG_SPAPR_TCE_IOMMU
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002344 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002345 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002346#else
2347 .long 0 /* 0x138 */
2348 .long 0 /* 0x13c */
2349#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002350 .long 0 /* 0x140 */
2351 .long 0 /* 0x144 */
2352 .long 0 /* 0x148 */
2353 .long 0 /* 0x14c */
2354 .long 0 /* 0x150 */
2355 .long 0 /* 0x154 */
2356 .long 0 /* 0x158 */
2357 .long 0 /* 0x15c */
2358 .long 0 /* 0x160 */
2359 .long 0 /* 0x164 */
2360 .long 0 /* 0x168 */
2361 .long 0 /* 0x16c */
2362 .long 0 /* 0x170 */
2363 .long 0 /* 0x174 */
2364 .long 0 /* 0x178 */
2365 .long 0 /* 0x17c */
2366 .long 0 /* 0x180 */
2367 .long 0 /* 0x184 */
2368 .long 0 /* 0x188 */
2369 .long 0 /* 0x18c */
2370 .long 0 /* 0x190 */
2371 .long 0 /* 0x194 */
2372 .long 0 /* 0x198 */
2373 .long 0 /* 0x19c */
2374 .long 0 /* 0x1a0 */
2375 .long 0 /* 0x1a4 */
2376 .long 0 /* 0x1a8 */
2377 .long 0 /* 0x1ac */
2378 .long 0 /* 0x1b0 */
2379 .long 0 /* 0x1b4 */
2380 .long 0 /* 0x1b8 */
2381 .long 0 /* 0x1bc */
2382 .long 0 /* 0x1c0 */
2383 .long 0 /* 0x1c4 */
2384 .long 0 /* 0x1c8 */
2385 .long 0 /* 0x1cc */
2386 .long 0 /* 0x1d0 */
2387 .long 0 /* 0x1d4 */
2388 .long 0 /* 0x1d8 */
2389 .long 0 /* 0x1dc */
2390 .long 0 /* 0x1e0 */
2391 .long 0 /* 0x1e4 */
2392 .long 0 /* 0x1e8 */
2393 .long 0 /* 0x1ec */
2394 .long 0 /* 0x1f0 */
2395 .long 0 /* 0x1f4 */
2396 .long 0 /* 0x1f8 */
2397 .long 0 /* 0x1fc */
2398 .long 0 /* 0x200 */
2399 .long 0 /* 0x204 */
2400 .long 0 /* 0x208 */
2401 .long 0 /* 0x20c */
2402 .long 0 /* 0x210 */
2403 .long 0 /* 0x214 */
2404 .long 0 /* 0x218 */
2405 .long 0 /* 0x21c */
2406 .long 0 /* 0x220 */
2407 .long 0 /* 0x224 */
2408 .long 0 /* 0x228 */
2409 .long 0 /* 0x22c */
2410 .long 0 /* 0x230 */
2411 .long 0 /* 0x234 */
2412 .long 0 /* 0x238 */
2413 .long 0 /* 0x23c */
2414 .long 0 /* 0x240 */
2415 .long 0 /* 0x244 */
2416 .long 0 /* 0x248 */
2417 .long 0 /* 0x24c */
2418 .long 0 /* 0x250 */
2419 .long 0 /* 0x254 */
2420 .long 0 /* 0x258 */
2421 .long 0 /* 0x25c */
2422 .long 0 /* 0x260 */
2423 .long 0 /* 0x264 */
2424 .long 0 /* 0x268 */
2425 .long 0 /* 0x26c */
2426 .long 0 /* 0x270 */
2427 .long 0 /* 0x274 */
2428 .long 0 /* 0x278 */
2429 .long 0 /* 0x27c */
2430 .long 0 /* 0x280 */
2431 .long 0 /* 0x284 */
2432 .long 0 /* 0x288 */
2433 .long 0 /* 0x28c */
2434 .long 0 /* 0x290 */
2435 .long 0 /* 0x294 */
2436 .long 0 /* 0x298 */
2437 .long 0 /* 0x29c */
2438 .long 0 /* 0x2a0 */
2439 .long 0 /* 0x2a4 */
2440 .long 0 /* 0x2a8 */
2441 .long 0 /* 0x2ac */
2442 .long 0 /* 0x2b0 */
2443 .long 0 /* 0x2b4 */
2444 .long 0 /* 0x2b8 */
2445 .long 0 /* 0x2bc */
2446 .long 0 /* 0x2c0 */
2447 .long 0 /* 0x2c4 */
2448 .long 0 /* 0x2c8 */
2449 .long 0 /* 0x2cc */
2450 .long 0 /* 0x2d0 */
2451 .long 0 /* 0x2d4 */
2452 .long 0 /* 0x2d8 */
2453 .long 0 /* 0x2dc */
2454 .long 0 /* 0x2e0 */
2455 .long 0 /* 0x2e4 */
2456 .long 0 /* 0x2e8 */
2457 .long 0 /* 0x2ec */
2458 .long 0 /* 0x2f0 */
2459 .long 0 /* 0x2f4 */
2460 .long 0 /* 0x2f8 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002461#ifdef CONFIG_KVM_XICS
2462 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2463#else
2464 .long 0 /* 0x2fc - H_XIRR_X*/
2465#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002466 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002467 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002468hcall_real_table_end:
2469
Paul Mackerras8563bf52014-01-08 21:25:29 +11002470_GLOBAL(kvmppc_h_set_xdabr)
Paul Mackerras4bad7772018-10-08 16:31:06 +11002471EXPORT_SYMBOL_GPL(kvmppc_h_set_xdabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002472 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2473 beq 6f
2474 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2475 andc. r0, r5, r0
2476 beq 3f
24776: li r3, H_PARAMETER
2478 blr
2479
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002480_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras4bad7772018-10-08 16:31:06 +11002481EXPORT_SYMBOL_GPL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002482 li r5, DABRX_USER | DABRX_KERNEL
24833:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002484BEGIN_FTR_SECTION
2485 b 2f
2486END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002487 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002488 stw r5, VCPU_DABRX(r3)
2489 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002490 /* Work around P7 bug where DABR can get corrupted on mtspr */
24911: mtspr SPRN_DABR,r4
2492 mfspr r5, SPRN_DABR
2493 cmpd r4, r5
2494 bne 1b
2495 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002496 li r3,0
2497 blr
2498
Michael Neulinge8ebedb2018-03-27 15:37:21 +110024992:
2500BEGIN_FTR_SECTION
2501 /* POWER9 with disabled DAWR */
Aneesh Kumar K.Vca9a16c2018-03-30 17:27:24 +05302502 li r3, H_HARDWARE
Michael Neulinge8ebedb2018-03-27 15:37:21 +11002503 blr
2504END_FTR_SECTION_IFCLR(CPU_FTR_DAWR)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002505 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
Michael Neulinge8ebedb2018-03-27 15:37:21 +11002506 rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002507 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002508 clrrdi r4, r4, 3
2509 std r4, VCPU_DAWR(r3)
2510 std r5, VCPU_DAWRX(r3)
2511 mtspr SPRN_DAWR, r4
2512 mtspr SPRN_DAWRX, r5
2513 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002514 blr
2515
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002516_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002517 ori r11,r11,MSR_EE
2518 std r11,VCPU_MSR(r3)
2519 li r0,1
2520 stb r0,VCPU_CEDED(r3)
2521 sync /* order setting ceded vs. testing prodded */
2522 lbz r5,VCPU_PRODDED(r3)
2523 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002524 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002525 li r12,0 /* set trap to 0 to say hcall is handled */
2526 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002527 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002528 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002529
2530 /*
2531 * Set our bit in the bitmask of napping threads unless all the
2532 * other threads are already napping, in which case we send this
2533 * up to the host.
2534 */
2535 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002536 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002537 lwz r8,VCORE_ENTRY_EXIT(r5)
2538 clrldi r8,r8,56
2539 li r0,1
2540 sld r0,r0,r6
2541 addi r6,r5,VCORE_NAPPING_THREADS
254231: lwarx r4,0,r6
2543 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002544 cmpw r4,r8
2545 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002546 stwcx. r4,0,r6
2547 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002548 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002549 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002550 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002551 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002552 lwz r7,VCORE_ENTRY_EXIT(r5)
2553 cmpwi r7,0x100
2554 bge 33f /* another thread already exiting */
2555
2556/*
2557 * Although not specifically required by the architecture, POWER7
2558 * preserves the following registers in nap mode, even if an SMT mode
2559 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2560 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2561 */
2562 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002563 std r14, VCPU_GPR(R14)(r3)
2564 std r15, VCPU_GPR(R15)(r3)
2565 std r16, VCPU_GPR(R16)(r3)
2566 std r17, VCPU_GPR(R17)(r3)
2567 std r18, VCPU_GPR(R18)(r3)
2568 std r19, VCPU_GPR(R19)(r3)
2569 std r20, VCPU_GPR(R20)(r3)
2570 std r21, VCPU_GPR(R21)(r3)
2571 std r22, VCPU_GPR(R22)(r3)
2572 std r23, VCPU_GPR(R23)(r3)
2573 std r24, VCPU_GPR(R24)(r3)
2574 std r25, VCPU_GPR(R25)(r3)
2575 std r26, VCPU_GPR(R26)(r3)
2576 std r27, VCPU_GPR(R27)(r3)
2577 std r28, VCPU_GPR(R28)(r3)
2578 std r29, VCPU_GPR(R29)(r3)
2579 std r30, VCPU_GPR(R30)(r3)
2580 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002581
2582 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002583 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002584
Paul Mackerras93d17392016-06-22 15:52:55 +10002585#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002586/*
2587 * Branch around the call if both CPU_FTR_TM and
2588 * CPU_FTR_P9_TM_HV_ASSIST are off.
2589 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002590BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002591 b 91f
2592END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002593 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11002594 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002595 */
Simon Guo6f597c62018-05-23 15:01:48 +08002596 ld r3, HSTATE_KVM_VCPU(r13)
2597 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11002598 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10002599 bl kvmppc_save_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11002600 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100260191:
Paul Mackerras93d17392016-06-22 15:52:55 +10002602#endif
2603
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002604 /*
2605 * Set DEC to the smaller of DEC and HDEC, so that we wake
2606 * no later than the end of our timeslice (HDEC interrupts
2607 * don't wake us from nap).
2608 */
2609 mfspr r3, SPRN_DEC
2610 mfspr r4, SPRN_HDEC
2611 mftb r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10002612BEGIN_FTR_SECTION
2613 /* On P9 check whether the guest has large decrementer mode enabled */
2614 ld r6, HSTATE_KVM_VCORE(r13)
2615 ld r6, VCORE_LPCR(r6)
2616 andis. r6, r6, LPCR_LD@h
2617 bne 68f
2618END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras2f272462017-05-22 16:25:14 +10002619 extsw r3, r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000262068: EXTEND_HDEC(r4)
Paul Mackerras2f272462017-05-22 16:25:14 +10002621 cmpd r3, r4
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002622 ble 67f
2623 mtspr SPRN_DEC, r4
262467:
2625 /* save expiry time of guest decrementer */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002626 add r3, r3, r5
2627 ld r4, HSTATE_KVM_VCPU(r13)
2628 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10002629 ld r6, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002630 subf r3, r6, r3 /* convert to host TB value */
2631 std r3, VCPU_DEC_EXPIRES(r4)
2632
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002633#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2634 ld r4, HSTATE_KVM_VCPU(r13)
2635 addi r3, r4, VCPU_TB_CEDE
2636 bl kvmhv_accumulate_time
2637#endif
2638
Paul Mackerrasccc07772015-03-28 14:21:07 +11002639 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2640
Paul Mackerras19ccb762011-07-23 17:42:46 +10002641 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002642 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002643 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002644 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002645 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002646 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002647kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002648 mfspr r0, SPRN_CTRLF
2649 clrrdi r0, r0, 1
2650 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302651
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002652 li r0,1
2653 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002654 mfspr r5,SPRN_LPCR
2655 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002656BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002657 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002658 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002659END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002660
2661kvm_nap_sequence: /* desired LPCR value in r5 */
2662BEGIN_FTR_SECTION
2663 /*
2664 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2665 * enable state loss = 1 (allow SMT mode switch)
2666 * requested level = 0 (just stop dispatching)
2667 */
2668 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2669 mtspr SPRN_PSSCR, r3
2670 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2671 li r4, LPCR_PECE_HVEE@higher
2672 sldi r4, r4, 32
2673 or r5, r5, r4
2674END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002675 mtspr SPRN_LPCR,r5
2676 isync
2677 li r0, 0
2678 std r0, HSTATE_SCRATCH0(r13)
2679 ptesync
2680 ld r0, HSTATE_SCRATCH0(r13)
26811: cmpd r0, r0
2682 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002683BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002684 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002685FTR_SECTION_ELSE
2686 PPC_STOP
2687ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002688 b .
2689
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100269033: mr r4, r3
2691 li r3, 0
2692 li r12, 0
2693 b 34f
2694
Paul Mackerras19ccb762011-07-23 17:42:46 +10002695kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002696 /* get vcpu pointer */
2697 ld r4, HSTATE_KVM_VCPU(r13)
2698
Paul Mackerras19ccb762011-07-23 17:42:46 +10002699 /* Woken by external or decrementer interrupt */
2700 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002701
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002702#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2703 addi r3, r4, VCPU_TB_RMINTR
2704 bl kvmhv_accumulate_time
2705#endif
2706
Paul Mackerras93d17392016-06-22 15:52:55 +10002707#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002708/*
2709 * Branch around the call if both CPU_FTR_TM and
2710 * CPU_FTR_P9_TM_HV_ASSIST are off.
2711 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002712BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002713 b 91f
2714END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002715 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11002716 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002717 */
Simon Guo6f597c62018-05-23 15:01:48 +08002718 mr r3, r4
2719 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11002720 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10002721 bl kvmppc_restore_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11002722 nop
Simon Guo6f597c62018-05-23 15:01:48 +08002723 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100272491:
Paul Mackerras93d17392016-06-22 15:52:55 +10002725#endif
2726
Paul Mackerras19ccb762011-07-23 17:42:46 +10002727 /* load up FP state */
2728 bl kvmppc_load_fp
2729
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002730 /* Restore guest decrementer */
2731 ld r3, VCPU_DEC_EXPIRES(r4)
2732 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10002733 ld r6, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002734 add r3, r3, r6 /* convert host TB to guest TB value */
2735 mftb r7
2736 subf r3, r7, r3
2737 mtspr SPRN_DEC, r3
2738
Paul Mackerras19ccb762011-07-23 17:42:46 +10002739 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002740 ld r14, VCPU_GPR(R14)(r4)
2741 ld r15, VCPU_GPR(R15)(r4)
2742 ld r16, VCPU_GPR(R16)(r4)
2743 ld r17, VCPU_GPR(R17)(r4)
2744 ld r18, VCPU_GPR(R18)(r4)
2745 ld r19, VCPU_GPR(R19)(r4)
2746 ld r20, VCPU_GPR(R20)(r4)
2747 ld r21, VCPU_GPR(R21)(r4)
2748 ld r22, VCPU_GPR(R22)(r4)
2749 ld r23, VCPU_GPR(R23)(r4)
2750 ld r24, VCPU_GPR(R24)(r4)
2751 ld r25, VCPU_GPR(R25)(r4)
2752 ld r26, VCPU_GPR(R26)(r4)
2753 ld r27, VCPU_GPR(R27)(r4)
2754 ld r28, VCPU_GPR(R28)(r4)
2755 ld r29, VCPU_GPR(R29)(r4)
2756 ld r30, VCPU_GPR(R30)(r4)
2757 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002758
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002759 /* Check the wake reason in SRR1 to see why we got here */
2760 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002761
Suresh Warrier37f55d32016-08-19 15:35:46 +10002762 /*
2763 * Restore volatile registers since we could have called a
2764 * C routine in kvmppc_check_wake_reason
2765 * r4 = VCPU
2766 * r3 tells us whether we need to return to host or not
2767 * WARNING: it gets checked further down:
2768 * should not modify r3 until this check is done.
2769 */
2770 ld r4, HSTATE_KVM_VCPU(r13)
2771
Paul Mackerras19ccb762011-07-23 17:42:46 +10002772 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100277334: ld r5,HSTATE_KVM_VCORE(r13)
2774 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002775 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002776 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002777 addi r6,r5,VCORE_NAPPING_THREADS
277832: lwarx r7,0,r6
2779 andc r7,r7,r0
2780 stwcx. r7,0,r6
2781 bne 32b
2782 li r0,0
2783 stb r0,HSTATE_NAPPING(r13)
2784
Suresh Warrier37f55d32016-08-19 15:35:46 +10002785 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002786 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002787 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002788 cmpdi r3, 0
2789 bgt guest_exit_cont
Paul Mackerrasdf709a22018-10-08 16:30:52 +11002790 b maybe_reenter_guest
Paul Mackerras19ccb762011-07-23 17:42:46 +10002791
2792 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002793kvm_cede_prodded:
2794 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002795 stb r0,VCPU_PRODDED(r3)
2796 sync /* order testing prodded vs. clearing ceded */
2797 stb r0,VCPU_CEDED(r3)
2798 li r3,H_SUCCESS
2799 blr
2800
2801 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002802kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002803 ld r9, HSTATE_KVM_VCPU(r13)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11002804#ifdef CONFIG_KVM_XICS
2805 /* Abort if we still have a pending escalation */
2806 lbz r5, VCPU_XIVE_ESC_ON(r9)
2807 cmpwi r5, 0
2808 beq 1f
2809 li r0, 0
2810 stb r0, VCPU_CEDED(r9)
28111: /* Enable XIVE escalation */
2812 li r5, XIVE_ESB_SET_PQ_00
2813 mfmsr r0
2814 andi. r0, r0, MSR_DR /* in real mode? */
2815 beq 1f
2816 ld r10, VCPU_XIVE_ESC_VADDR(r9)
2817 cmpdi r10, 0
2818 beq 3f
2819 ldx r0, r10, r5
2820 b 2f
28211: ld r10, VCPU_XIVE_ESC_RADDR(r9)
2822 cmpdi r10, 0
2823 beq 3f
2824 ldcix r0, r10, r5
28252: sync
2826 li r0, 1
2827 stb r0, VCPU_XIVE_ESC_ON(r9)
2828#endif /* CONFIG_KVM_XICS */
28293: b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002830
Paul Mackerras884dfb72019-02-21 13:38:49 +11002831 /* Try to do machine check recovery in real mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002832machine_check_realmode:
2833 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002834 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002835 nop
Paul Mackerras884dfb72019-02-21 13:38:49 +11002836 /* all machine checks go to virtual mode for further handling */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002837 ld r9, HSTATE_KVM_VCPU(r13)
2838 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Paul Mackerras884dfb72019-02-21 13:38:49 +11002839 b guest_exit_cont
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002840
Paul Mackerrasde56a942011-06-29 00:21:34 +00002841/*
Paul Mackerrasdf709a22018-10-08 16:30:52 +11002842 * Call C code to handle a HMI in real mode.
2843 * Only the primary thread does the call, secondary threads are handled
2844 * by calling hmi_exception_realmode() after kvmppc_hv_entry returns.
2845 * r9 points to the vcpu on entry
2846 */
2847hmi_realmode:
2848 lbz r0, HSTATE_PTID(r13)
2849 cmpwi r0, 0
2850 bne guest_exit_cont
2851 bl kvmppc_realmode_hmi_handler
2852 ld r9, HSTATE_KVM_VCPU(r13)
2853 li r12, BOOK3S_INTERRUPT_HMI
2854 b guest_exit_cont
2855
2856/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002857 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002858 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002859 * 0 if nothing needs to be done
2860 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002861 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002862 * -2 if we handled a PCI passthrough interrupt (returned by
2863 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002864 *
2865 * Also sets r12 to the interrupt vector for any interrupt that needs
2866 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002867 * Modifies all volatile registers (since it may call a C function).
2868 * This routine calls kvmppc_read_intr, a C function, if an external
2869 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002870 */
2871kvmppc_check_wake_reason:
2872 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002873BEGIN_FTR_SECTION
2874 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2875FTR_SECTION_ELSE
2876 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2877ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2878 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002879 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002880 li r3, 0
2881 li r12, 0
2882 cmpwi r6, 6 /* was it the decrementer? */
2883 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002884BEGIN_FTR_SECTION
2885 cmpwi r6, 5 /* privileged doorbell? */
2886 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002887 cmpwi r6, 3 /* hypervisor doorbell? */
2888 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002889END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302890 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2891 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002892 li r3, 1 /* anything else, return 1 */
28930: blr
2894
Paul Mackerras5d00f662014-01-08 21:25:28 +11002895 /* hypervisor doorbell */
28963: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302897
2898 /*
2899 * Clear the doorbell as we will invoke the handler
2900 * explicitly in the guest exit path.
2901 */
2902 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2903 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002904 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002905 li r3, 1
Nicholas Piggin2cde3712017-10-10 20:18:28 +10002906BEGIN_FTR_SECTION
2907 PPC_MSGSYNC
2908 lwsync
2909END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11002910 lbz r0, HSTATE_HOST_IPI(r13)
2911 cmpwi r0, 0
2912 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302913 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002914 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002915 blr
2916
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302917 /* Woken up due to Hypervisor maintenance interrupt */
29184: li r12, BOOK3S_INTERRUPT_HMI
2919 li r3, 1
2920 blr
2921
Suresh Warrier37f55d32016-08-19 15:35:46 +10002922 /* external interrupt - create a stack frame so we can call C */
29237: mflr r0
2924 std r0, PPC_LR_STKOFF(r1)
2925 stdu r1, -PPC_MIN_STKFRM(r1)
2926 bl kvmppc_read_intr
2927 nop
2928 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002929 cmpdi r3, 1
2930 ble 1f
2931
2932 /*
2933 * Return code of 2 means PCI passthrough interrupt, but
2934 * we need to return back to host to complete handling the
2935 * interrupt. Trap reason is expected in r12 by guest
2936 * exit code.
2937 */
2938 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
29391:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002940 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2941 addi r1, r1, PPC_MIN_STKFRM
2942 mtlr r0
2943 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002944
2945/*
2946 * Save away FP, VMX and VSX registers.
2947 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002948 * N.B. r30 and r31 are volatile across this function,
2949 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002950 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002951kvmppc_save_fp:
2952 mflr r30
2953 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002954 mfmsr r5
2955 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002956#ifdef CONFIG_ALTIVEC
2957BEGIN_FTR_SECTION
2958 oris r8,r8,MSR_VEC@h
2959END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2960#endif
2961#ifdef CONFIG_VSX
2962BEGIN_FTR_SECTION
2963 oris r8,r8,MSR_VSX@h
2964END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2965#endif
2966 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002967 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002968 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002969#ifdef CONFIG_ALTIVEC
2970BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002971 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002972 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002973END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2974#endif
2975 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002976 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002977 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002978 blr
2979
2980/*
2981 * Load up FP, VMX and VSX registers
2982 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002983 * N.B. r30 and r31 are volatile across this function,
2984 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002985 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002986kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002987 mflr r30
2988 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002989 mfmsr r9
2990 ori r8,r9,MSR_FP
2991#ifdef CONFIG_ALTIVEC
2992BEGIN_FTR_SECTION
2993 oris r8,r8,MSR_VEC@h
2994END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2995#endif
2996#ifdef CONFIG_VSX
2997BEGIN_FTR_SECTION
2998 oris r8,r8,MSR_VSX@h
2999END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3000#endif
3001 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11003002 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003003 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003004#ifdef CONFIG_ALTIVEC
3005BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11003006 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003007 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003008END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3009#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11003010 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003011 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11003012 mtlr r30
3013 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00003014 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10003015
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003016#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
3017/*
3018 * Save transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003019 * Called with r3 pointing to the vcpu struct and r4 containing
3020 * the guest MSR value.
Paul Mackerras7854f752018-10-08 16:30:53 +11003021 * r5 is non-zero iff non-volatile register state needs to be maintained.
3022 * If r5 == 0, this can modify all checkpointed registers, but
Simon Guo6f597c62018-05-23 15:01:48 +08003023 * restores r1 and r2 before exit.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003024 */
Paul Mackerras7854f752018-10-08 16:30:53 +11003025_GLOBAL_TOC(kvmppc_save_tm_hv)
3026EXPORT_SYMBOL_GPL(kvmppc_save_tm_hv)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003027 /* See if we need to handle fake suspend mode */
3028BEGIN_FTR_SECTION
Simon Guocaa3be92018-05-23 15:01:50 +08003029 b __kvmppc_save_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003030END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
3031
3032 lbz r0, HSTATE_FAKE_SUSPEND(r13) /* Were we fake suspended? */
3033 cmpwi r0, 0
Simon Guocaa3be92018-05-23 15:01:50 +08003034 beq __kvmppc_save_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003035
3036 /* The following code handles the fake_suspend = 1 case */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003037 mflr r0
3038 std r0, PPC_LR_STKOFF(r1)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003039 stdu r1, -PPC_MIN_STKFRM(r1)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003040
3041 /* Turn on TM. */
3042 mfmsr r8
3043 li r0, 1
3044 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
3045 mtmsrd r8
3046
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003047 rldicl. r8, r8, 64 - MSR_TS_S_LG, 62 /* Did we actually hrfid? */
3048 beq 4f
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003049BEGIN_FTR_SECTION
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003050 bl pnv_power9_force_smt4_catch
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003051END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003052 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003053
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003054 /* We have to treclaim here because that's the only way to do S->N */
3055 li r3, TM_CAUSE_KVM_RESCHED
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003056 TRECLAIM(R3)
3057
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003058 /*
3059 * We were in fake suspend, so we are not going to save the
3060 * register state as the guest checkpointed state (since
3061 * we already have it), therefore we can now use any volatile GPR.
Paul Mackerras7854f752018-10-08 16:30:53 +11003062 * In fact treclaim in fake suspend state doesn't modify
3063 * any registers.
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003064 */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003065
Paul Mackerras7854f752018-10-08 16:30:53 +11003066BEGIN_FTR_SECTION
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003067 bl pnv_power9_force_smt4_release
Paul Mackerras7854f752018-10-08 16:30:53 +11003068END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003069 nop
3070
30714:
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003072 mfspr r3, SPRN_PSSCR
3073 /* PSSCR_FAKE_SUSPEND is a write-only bit, but clear it anyway */
3074 li r0, PSSCR_FAKE_SUSPEND
3075 andc r3, r3, r0
3076 mtspr SPRN_PSSCR, r3
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003077
Paul Mackerras681c6172018-03-21 21:32:03 +11003078 /* Don't save TEXASR, use value from last exit in real suspend state */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003079 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003080 mfspr r5, SPRN_TFHAR
3081 mfspr r6, SPRN_TFIAR
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003082 std r5, VCPU_TFHAR(r9)
3083 std r6, VCPU_TFIAR(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003084
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003085 addi r1, r1, PPC_MIN_STKFRM
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003086 ld r0, PPC_LR_STKOFF(r1)
3087 mtlr r0
3088 blr
3089
3090/*
3091 * Restore transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003092 * Called with r3 pointing to the vcpu struct
3093 * and r4 containing the guest MSR value.
Paul Mackerras7854f752018-10-08 16:30:53 +11003094 * r5 is non-zero iff non-volatile register state needs to be maintained.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003095 * This potentially modifies all checkpointed registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003096 * It restores r1 and r2 from the PACA.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003097 */
Paul Mackerras7854f752018-10-08 16:30:53 +11003098_GLOBAL_TOC(kvmppc_restore_tm_hv)
3099EXPORT_SYMBOL_GPL(kvmppc_restore_tm_hv)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003100 /*
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003101 * If we are doing TM emulation for the guest on a POWER9 DD2,
3102 * then we don't actually do a trechkpt -- we either set up
3103 * fake-suspend mode, or emulate a TM rollback.
3104 */
3105BEGIN_FTR_SECTION
Simon Guocaa3be92018-05-23 15:01:50 +08003106 b __kvmppc_restore_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003107END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
3108 mflr r0
3109 std r0, PPC_LR_STKOFF(r1)
3110
3111 li r0, 0
3112 stb r0, HSTATE_FAKE_SUSPEND(r13)
3113
3114 /* Turn on TM so we can restore TM SPRs */
3115 mfmsr r5
3116 li r0, 1
3117 rldimi r5, r0, MSR_TM_LG, 63-MSR_TM_LG
3118 mtmsrd r5
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003119
3120 /*
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003121 * The user may change these outside of a transaction, so they must
3122 * always be context switched.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003123 */
Simon Guo6f597c62018-05-23 15:01:48 +08003124 ld r5, VCPU_TFHAR(r3)
3125 ld r6, VCPU_TFIAR(r3)
3126 ld r7, VCPU_TEXASR(r3)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003127 mtspr SPRN_TFHAR, r5
3128 mtspr SPRN_TFIAR, r6
3129 mtspr SPRN_TEXASR, r7
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003130
Simon Guo6f597c62018-05-23 15:01:48 +08003131 rldicl. r5, r4, 64 - MSR_TS_S_LG, 62
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003132 beqlr /* TM not active in guest */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003133
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003134 /* Make sure the failure summary is set */
3135 oris r7, r7, (TEXASR_FS)@h
3136 mtspr SPRN_TEXASR, r7
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003137
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003138 cmpwi r5, 1 /* check for suspended state */
3139 bgt 10f
3140 stb r5, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003141 b 9f /* and return */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100314210: stdu r1, -PPC_MIN_STKFRM(r1)
3143 /* guest is in transactional state, so simulate rollback */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003144 bl kvmhv_emulate_tm_rollback
3145 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003146 addi r1, r1, PPC_MIN_STKFRM
Paul Mackerras7b0e8272018-05-30 20:07:52 +100031479: ld r0, PPC_LR_STKOFF(r1)
3148 mtlr r0
3149 blr
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003150#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003151
Paul Mackerras44a3add2013-10-04 21:45:04 +10003152/*
3153 * We come here if we get any exception or interrupt while we are
3154 * executing host real mode code while in guest MMU context.
Paul Mackerras857b99e2017-09-01 16:17:27 +10003155 * r12 is (CR << 32) | vector
3156 * r13 points to our PACA
3157 * r12 is saved in HSTATE_SCRATCH0(r13)
3158 * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
3159 * r9 is saved in HSTATE_SCRATCH2(r13)
3160 * r13 is saved in HSPRG1
3161 * cfar is saved in HSTATE_CFAR(r13)
3162 * ppr is saved in HSTATE_PPR(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10003163 */
3164kvmppc_bad_host_intr:
Paul Mackerras857b99e2017-09-01 16:17:27 +10003165 /*
3166 * Switch to the emergency stack, but start half-way down in
3167 * case we were already on it.
3168 */
3169 mr r9, r1
3170 std r1, PACAR1(r13)
3171 ld r1, PACAEMERGSP(r13)
3172 subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
3173 std r9, 0(r1)
3174 std r0, GPR0(r1)
3175 std r9, GPR1(r1)
3176 std r2, GPR2(r1)
3177 SAVE_4GPRS(3, r1)
3178 SAVE_2GPRS(7, r1)
3179 srdi r0, r12, 32
3180 clrldi r12, r12, 32
3181 std r0, _CCR(r1)
3182 std r12, _TRAP(r1)
3183 andi. r0, r12, 2
3184 beq 1f
3185 mfspr r3, SPRN_HSRR0
3186 mfspr r4, SPRN_HSRR1
3187 mfspr r5, SPRN_HDAR
3188 mfspr r6, SPRN_HDSISR
3189 b 2f
31901: mfspr r3, SPRN_SRR0
3191 mfspr r4, SPRN_SRR1
3192 mfspr r5, SPRN_DAR
3193 mfspr r6, SPRN_DSISR
31942: std r3, _NIP(r1)
3195 std r4, _MSR(r1)
3196 std r5, _DAR(r1)
3197 std r6, _DSISR(r1)
3198 ld r9, HSTATE_SCRATCH2(r13)
3199 ld r12, HSTATE_SCRATCH0(r13)
3200 GET_SCRATCH0(r0)
3201 SAVE_4GPRS(9, r1)
3202 std r0, GPR13(r1)
3203 SAVE_NVGPRS(r1)
3204 ld r5, HSTATE_CFAR(r13)
3205 std r5, ORIG_GPR3(r1)
3206 mflr r3
3207#ifdef CONFIG_RELOCATABLE
3208 ld r4, HSTATE_SCRATCH1(r13)
3209#else
3210 mfctr r4
3211#endif
3212 mfxer r5
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +05303213 lbz r6, PACAIRQSOFTMASK(r13)
Paul Mackerras857b99e2017-09-01 16:17:27 +10003214 std r3, _LINK(r1)
3215 std r4, _CTR(r1)
3216 std r5, _XER(r1)
3217 std r6, SOFTE(r1)
3218 ld r2, PACATOC(r13)
3219 LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
3220 std r3, STACK_FRAME_OVERHEAD-16(r1)
3221
3222 /*
3223 * On POWER9 do a minimal restore of the MMU and call C code,
3224 * which will print a message and panic.
3225 * XXX On POWER7 and POWER8, we just spin here since we don't
3226 * know what the other threads are doing (and we don't want to
3227 * coordinate with them) - but at least we now have register state
3228 * in memory that we might be able to look at from another CPU.
3229 */
3230BEGIN_FTR_SECTION
Paul Mackerras44a3add2013-10-04 21:45:04 +10003231 b .
Paul Mackerras857b99e2017-09-01 16:17:27 +10003232END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
3233 ld r9, HSTATE_KVM_VCPU(r13)
3234 ld r10, VCPU_KVM(r9)
3235
3236 li r0, 0
3237 mtspr SPRN_AMR, r0
3238 mtspr SPRN_IAMR, r0
3239 mtspr SPRN_CIABR, r0
3240 mtspr SPRN_DAWRX, r0
3241
Paul Mackerras857b99e2017-09-01 16:17:27 +10003242BEGIN_MMU_FTR_SECTION
3243 b 4f
3244END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
3245
3246 slbmte r0, r0
3247 slbia
3248 ptesync
3249 ld r8, PACA_SLBSHADOWPTR(r13)
3250 .rept SLB_NUM_BOLTED
3251 li r3, SLBSHADOW_SAVEAREA
3252 LDX_BE r5, r8, r3
3253 addi r3, r3, 8
3254 LDX_BE r6, r8, r3
3255 andis. r7, r5, SLB_ESID_V@h
3256 beq 3f
3257 slbmte r6, r5
32583: addi r8, r8, 16
3259 .endr
3260
32614: lwz r7, KVM_HOST_LPID(r10)
3262 mtspr SPRN_LPID, r7
3263 mtspr SPRN_PID, r0
3264 ld r8, KVM_HOST_LPCR(r10)
3265 mtspr SPRN_LPCR, r8
3266 isync
3267 li r0, KVM_GUEST_MODE_NONE
3268 stb r0, HSTATE_IN_GUEST(r13)
3269
3270 /*
3271 * Turn on the MMU and jump to C code
3272 */
3273 bcl 20, 31, .+4
32745: mflr r3
3275 addi r3, r3, 9f - 5b
Nicholas Piggineadce3b2018-05-18 03:49:43 +10003276 li r4, -1
3277 rldimi r3, r4, 62, 0 /* ensure 0xc000000000000000 bits are set */
Paul Mackerras857b99e2017-09-01 16:17:27 +10003278 ld r4, PACAKMSR(r13)
3279 mtspr SPRN_SRR0, r3
3280 mtspr SPRN_SRR1, r4
Nicholas Piggin222f20f2018-01-10 03:07:15 +11003281 RFI_TO_KERNEL
Paul Mackerras857b99e2017-09-01 16:17:27 +100032829: addi r3, r1, STACK_FRAME_OVERHEAD
3283 bl kvmppc_bad_interrupt
3284 b 9b
Michael Neulinge4e38122014-03-25 10:47:02 +11003285
3286/*
3287 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3288 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3289 * r11 has the guest MSR value (in/out)
3290 * r9 has a vcpu pointer (in)
3291 * r0 is used as a scratch register
3292 */
3293kvmppc_msr_interrupt:
3294 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3295 cmpwi r0, 2 /* Check if we are in transactional state.. */
3296 ld r11, VCPU_INTR_MSR(r9)
3297 bne 1f
3298 /* ... if transactional, change to suspended */
3299 li r0, 1
33001: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3301 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003302
3303/*
Paul Mackerras41f4e632018-10-08 16:30:51 +11003304 * Load up guest PMU state. R3 points to the vcpu struct.
3305 */
3306_GLOBAL(kvmhv_load_guest_pmu)
3307EXPORT_SYMBOL_GPL(kvmhv_load_guest_pmu)
3308 mr r4, r3
3309 mflr r0
3310 li r3, 1
3311 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
3312 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
3313 isync
3314BEGIN_FTR_SECTION
3315 ld r3, VCPU_MMCR(r4)
3316 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
3317 cmpwi r5, MMCR0_PMAO
3318 beql kvmppc_fix_pmao
3319END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
3320 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
3321 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
3322 lwz r6, VCPU_PMC + 8(r4)
3323 lwz r7, VCPU_PMC + 12(r4)
3324 lwz r8, VCPU_PMC + 16(r4)
3325 lwz r9, VCPU_PMC + 20(r4)
3326 mtspr SPRN_PMC1, r3
3327 mtspr SPRN_PMC2, r5
3328 mtspr SPRN_PMC3, r6
3329 mtspr SPRN_PMC4, r7
3330 mtspr SPRN_PMC5, r8
3331 mtspr SPRN_PMC6, r9
3332 ld r3, VCPU_MMCR(r4)
3333 ld r5, VCPU_MMCR + 8(r4)
3334 ld r6, VCPU_MMCR + 16(r4)
3335 ld r7, VCPU_SIAR(r4)
3336 ld r8, VCPU_SDAR(r4)
3337 mtspr SPRN_MMCR1, r5
3338 mtspr SPRN_MMCRA, r6
3339 mtspr SPRN_SIAR, r7
3340 mtspr SPRN_SDAR, r8
3341BEGIN_FTR_SECTION
3342 ld r5, VCPU_MMCR + 24(r4)
3343 ld r6, VCPU_SIER(r4)
3344 mtspr SPRN_MMCR2, r5
3345 mtspr SPRN_SIER, r6
3346BEGIN_FTR_SECTION_NESTED(96)
3347 lwz r7, VCPU_PMC + 24(r4)
3348 lwz r8, VCPU_PMC + 28(r4)
3349 ld r9, VCPU_MMCR + 32(r4)
3350 mtspr SPRN_SPMC1, r7
3351 mtspr SPRN_SPMC2, r8
3352 mtspr SPRN_MMCRS, r9
3353END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
3354END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3355 mtspr SPRN_MMCR0, r3
3356 isync
3357 mtlr r0
3358 blr
3359
3360/*
3361 * Reload host PMU state saved in the PACA by kvmhv_save_host_pmu.
3362 */
3363_GLOBAL(kvmhv_load_host_pmu)
3364EXPORT_SYMBOL_GPL(kvmhv_load_host_pmu)
3365 mflr r0
3366 lbz r4, PACA_PMCINUSE(r13) /* is the host using the PMU? */
3367 cmpwi r4, 0
3368 beq 23f /* skip if not */
3369BEGIN_FTR_SECTION
3370 ld r3, HSTATE_MMCR0(r13)
3371 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
3372 cmpwi r4, MMCR0_PMAO
3373 beql kvmppc_fix_pmao
3374END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
3375 lwz r3, HSTATE_PMC1(r13)
3376 lwz r4, HSTATE_PMC2(r13)
3377 lwz r5, HSTATE_PMC3(r13)
3378 lwz r6, HSTATE_PMC4(r13)
3379 lwz r8, HSTATE_PMC5(r13)
3380 lwz r9, HSTATE_PMC6(r13)
3381 mtspr SPRN_PMC1, r3
3382 mtspr SPRN_PMC2, r4
3383 mtspr SPRN_PMC3, r5
3384 mtspr SPRN_PMC4, r6
3385 mtspr SPRN_PMC5, r8
3386 mtspr SPRN_PMC6, r9
3387 ld r3, HSTATE_MMCR0(r13)
3388 ld r4, HSTATE_MMCR1(r13)
3389 ld r5, HSTATE_MMCRA(r13)
3390 ld r6, HSTATE_SIAR(r13)
3391 ld r7, HSTATE_SDAR(r13)
3392 mtspr SPRN_MMCR1, r4
3393 mtspr SPRN_MMCRA, r5
3394 mtspr SPRN_SIAR, r6
3395 mtspr SPRN_SDAR, r7
3396BEGIN_FTR_SECTION
3397 ld r8, HSTATE_MMCR2(r13)
3398 ld r9, HSTATE_SIER(r13)
3399 mtspr SPRN_MMCR2, r8
3400 mtspr SPRN_SIER, r9
3401END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3402 mtspr SPRN_MMCR0, r3
3403 isync
3404 mtlr r0
340523: blr
3406
3407/*
3408 * Save guest PMU state into the vcpu struct.
3409 * r3 = vcpu, r4 = full save flag (PMU in use flag set in VPA)
3410 */
3411_GLOBAL(kvmhv_save_guest_pmu)
3412EXPORT_SYMBOL_GPL(kvmhv_save_guest_pmu)
3413 mr r9, r3
3414 mr r8, r4
3415BEGIN_FTR_SECTION
3416 /*
3417 * POWER8 seems to have a hardware bug where setting
3418 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
3419 * when some counters are already negative doesn't seem
3420 * to cause a performance monitor alert (and hence interrupt).
3421 * The effect of this is that when saving the PMU state,
3422 * if there is no PMU alert pending when we read MMCR0
3423 * before freezing the counters, but one becomes pending
3424 * before we read the counters, we lose it.
3425 * To work around this, we need a way to freeze the counters
3426 * before reading MMCR0. Normally, freezing the counters
3427 * is done by writing MMCR0 (to set MMCR0[FC]) which
3428 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
3429 * we can also freeze the counters using MMCR2, by writing
3430 * 1s to all the counter freeze condition bits (there are
3431 * 9 bits each for 6 counters).
3432 */
3433 li r3, -1 /* set all freeze bits */
3434 clrrdi r3, r3, 10
3435 mfspr r10, SPRN_MMCR2
3436 mtspr SPRN_MMCR2, r3
3437 isync
3438END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3439 li r3, 1
3440 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
3441 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
3442 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
3443 mfspr r6, SPRN_MMCRA
3444 /* Clear MMCRA in order to disable SDAR updates */
3445 li r7, 0
3446 mtspr SPRN_MMCRA, r7
3447 isync
3448 cmpwi r8, 0 /* did they ask for PMU stuff to be saved? */
3449 bne 21f
3450 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
3451 b 22f
345221: mfspr r5, SPRN_MMCR1
3453 mfspr r7, SPRN_SIAR
3454 mfspr r8, SPRN_SDAR
3455 std r4, VCPU_MMCR(r9)
3456 std r5, VCPU_MMCR + 8(r9)
3457 std r6, VCPU_MMCR + 16(r9)
3458BEGIN_FTR_SECTION
3459 std r10, VCPU_MMCR + 24(r9)
3460END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3461 std r7, VCPU_SIAR(r9)
3462 std r8, VCPU_SDAR(r9)
3463 mfspr r3, SPRN_PMC1
3464 mfspr r4, SPRN_PMC2
3465 mfspr r5, SPRN_PMC3
3466 mfspr r6, SPRN_PMC4
3467 mfspr r7, SPRN_PMC5
3468 mfspr r8, SPRN_PMC6
3469 stw r3, VCPU_PMC(r9)
3470 stw r4, VCPU_PMC + 4(r9)
3471 stw r5, VCPU_PMC + 8(r9)
3472 stw r6, VCPU_PMC + 12(r9)
3473 stw r7, VCPU_PMC + 16(r9)
3474 stw r8, VCPU_PMC + 20(r9)
3475BEGIN_FTR_SECTION
3476 mfspr r5, SPRN_SIER
3477 std r5, VCPU_SIER(r9)
3478BEGIN_FTR_SECTION_NESTED(96)
3479 mfspr r6, SPRN_SPMC1
3480 mfspr r7, SPRN_SPMC2
3481 mfspr r8, SPRN_MMCRS
3482 stw r6, VCPU_PMC + 24(r9)
3483 stw r7, VCPU_PMC + 28(r9)
3484 std r8, VCPU_MMCR + 32(r9)
3485 lis r4, 0x8000
3486 mtspr SPRN_MMCRS, r4
3487END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
3488END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
348922: blr
3490
3491/*
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003492 * This works around a hardware bug on POWER8E processors, where
3493 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3494 * performance monitor interrupt. Instead, when we need to have
3495 * an interrupt pending, we have to arrange for a counter to overflow.
3496 */
3497kvmppc_fix_pmao:
3498 li r3, 0
3499 mtspr SPRN_MMCR2, r3
3500 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3501 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3502 mtspr SPRN_MMCR0, r3
3503 lis r3, 0x7fff
3504 ori r3, r3, 0xffff
3505 mtspr SPRN_PMC6, r3
3506 isync
3507 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003508
3509#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3510/*
3511 * Start timing an activity
3512 * r3 = pointer to time accumulation struct, r4 = vcpu
3513 */
3514kvmhv_start_timing:
3515 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003516 ld r6, VCORE_TB_OFFSET_APPL(r5)
3517 mftb r5
3518 subf r5, r6, r5 /* subtract current timebase offset */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003519 std r3, VCPU_CUR_ACTIVITY(r4)
3520 std r5, VCPU_ACTIVITY_START(r4)
3521 blr
3522
3523/*
3524 * Accumulate time to one activity and start another.
3525 * r3 = pointer to new time accumulation struct, r4 = vcpu
3526 */
3527kvmhv_accumulate_time:
3528 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003529 ld r8, VCORE_TB_OFFSET_APPL(r5)
3530 ld r5, VCPU_CUR_ACTIVITY(r4)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003531 ld r6, VCPU_ACTIVITY_START(r4)
3532 std r3, VCPU_CUR_ACTIVITY(r4)
3533 mftb r7
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003534 subf r7, r8, r7 /* subtract current timebase offset */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003535 std r7, VCPU_ACTIVITY_START(r4)
3536 cmpdi r5, 0
3537 beqlr
3538 subf r3, r6, r7
3539 ld r8, TAS_SEQCOUNT(r5)
3540 cmpdi r8, 0
3541 addi r8, r8, 1
3542 std r8, TAS_SEQCOUNT(r5)
3543 lwsync
3544 ld r7, TAS_TOTAL(r5)
3545 add r7, r7, r3
3546 std r7, TAS_TOTAL(r5)
3547 ld r6, TAS_MIN(r5)
3548 ld r7, TAS_MAX(r5)
3549 beq 3f
3550 cmpd r3, r6
3551 bge 1f
35523: std r3, TAS_MIN(r5)
35531: cmpd r3, r7
3554 ble 2f
3555 std r3, TAS_MAX(r5)
35562: lwsync
3557 addi r8, r8, 1
3558 std r8, TAS_SEQCOUNT(r5)
3559 blr
3560#endif