blob: 31f914742fdd9a70119629a4734509fed26c6891 [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 Mackerras1b400ba2012-11-21 23:28:08 +0000626 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100627BEGIN_FTR_SECTION
628 /*
629 * On POWER9, individual threads can come in here, but the
630 * TLB is shared between the 4 threads in a core, hence
631 * invalidating on one thread invalidates for all.
632 * Thus we make all 4 threads use the same bit here.
633 */
634 clrrdi r6,r6,2
635END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000636 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
637 srdi r6,r6,6 /* doubleword number */
638 sldi r6,r6,3 /* address offset */
639 add r6,r6,r9
640 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100641 li r8,1
642 sld r8,r8,r7
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000643 ld r7,0(r6)
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100644 and. r7,r7,r8
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000645 beq 22f
Paul Mackerrasca252052014-01-08 21:25:22 +1100646 /* Flush the TLB of any entries for this LPID */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100647 lwz r0,KVM_TLB_SETS(r9)
648 mtctr r0
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000649 li r7,0x800 /* IS field = 0b10 */
650 ptesync
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100651 li r0,0 /* RS for P9 version of tlbiel */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +110065228: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000653 addi r7,r7,0x1000
654 bdnz 28b
Nicholas Piggin9a4506e2018-05-17 17:06:29 +1000655 ptesync
Paul Mackerrasa29ebea2017-01-30 21:21:50 +110065623: ldarx r7,0,r6 /* clear the bit after TLB flushed */
657 andc r7,r7,r8
658 stdcx. r7,0,r6
659 bne 23b
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000660
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000661 /* Add timebase offset onto timebase */
66222: ld r8,VCORE_TB_OFFSET(r5)
663 cmpdi r8,0
664 beq 37f
Paul Mackerras57b8daa2018-04-20 22:51:11 +1000665 std r8, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000666 mftb r6 /* current host timebase */
667 add r8,r8,r6
668 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
669 mftb r7 /* check if lower 24 bits overflowed */
670 clrldi r6,r6,40
671 clrldi r7,r7,40
672 cmpld r7,r6
673 bge 37f
674 addis r8,r8,0x100 /* if so, increment upper 40 bits */
675 mtspr SPRN_TBU40,r8
676
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000677 /* Load guest PCR value to select appropriate compat mode */
67837: ld r7, VCORE_PCR(r5)
679 cmpdi r7, 0
680 beq 38f
681 mtspr SPRN_PCR, r7
68238:
Michael Neulingb005255e2014-01-08 21:25:21 +1100683
684BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000685 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100686 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000687 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100688 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000689 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100690END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
691
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530692 /* Mark the subcore state as inside guest */
693 bl kvmppc_subcore_enter_guest
694 nop
695 ld r5, HSTATE_KVM_VCORE(r13)
696 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000697 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000698 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000699
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100700 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110070110: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100702 beq kvmppc_primary_no_guest
703kvmppc_got_guest:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100704 /* Increment yield count if they have a VPA */
705 ld r3, VCPU_VPA(r4)
706 cmpdi r3, 0
707 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200708 li r6, LPPACA_YIELDCOUNT
709 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100710 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200711 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100712 li r6, 1
713 stb r6, VCPU_VPA_DIRTY(r4)
71425:
715
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100716 /* Save purr/spurr */
717 mfspr r5,SPRN_PURR
718 mfspr r6,SPRN_SPURR
719 std r5,HSTATE_PURR(r13)
720 std r6,HSTATE_SPURR(r13)
721 ld r7,VCPU_PURR(r4)
722 ld r8,VCPU_SPURR(r4)
723 mtspr SPRN_PURR,r7
724 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100725
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100726 /* Save host values of some registers */
727BEGIN_FTR_SECTION
728 mfspr r5, SPRN_TIDR
729 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100730 mfspr r7, SPRN_PID
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100731 std r5, STACK_SLOT_TID(r1)
732 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100733 std r7, STACK_SLOT_PID(r1)
Paul Mackerras769377f2017-02-15 14:30:17 +1100734 mfspr r5, SPRN_HFSCR
735 std r5, STACK_SLOT_HFSCR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100736END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000737BEGIN_FTR_SECTION
738 mfspr r5, SPRN_CIABR
739 mfspr r6, SPRN_DAWR
740 mfspr r7, SPRN_DAWRX
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100741 mfspr r8, SPRN_IAMR
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000742 std r5, STACK_SLOT_CIABR(r1)
743 std r6, STACK_SLOT_DAWR(r1)
744 std r7, STACK_SLOT_DAWRX(r1)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100745 std r8, STACK_SLOT_IAMR(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000746END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100747
Michael Ellermanc3c7470c2019-02-22 13:22:08 +1100748 mfspr r5, SPRN_AMR
749 std r5, STACK_SLOT_AMR(r1)
750 mfspr r6, SPRN_UAMOR
751 std r6, STACK_SLOT_UAMOR(r1)
752
Michael Neulingeee7ff92014-01-08 21:25:19 +1100753BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000754 /* Set partition DABR */
755 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100756 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000757 ld r6,VCPU_DABR(r4)
758 mtspr SPRN_DABRX,r5
759 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000760 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100761END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000762
Michael Neulinge4e38122014-03-25 10:47:02 +1100763#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100764/*
765 * Branch around the call if both CPU_FTR_TM and
766 * CPU_FTR_P9_TM_HV_ASSIST are off.
767 */
Michael Neulinge4e38122014-03-25 10:47:02 +1100768BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100769 b 91f
770END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000771 /*
Paul Mackerras7854f752018-10-08 16:30:53 +1100772 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000773 */
Simon Guo6f597c62018-05-23 15:01:48 +0800774 mr r3, r4
775 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +1100776 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +1000777 bl kvmppc_restore_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +1100778 nop
Simon Guo6f597c62018-05-23 15:01:48 +0800779 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +110078091:
Michael Neulinge4e38122014-03-25 10:47:02 +1100781#endif
782
Paul Mackerras41f4e632018-10-08 16:30:51 +1100783 /* Load guest PMU registers; r4 = vcpu pointer here */
784 mr r3, r4
785 bl kvmhv_load_guest_pmu
Paul Mackerrasde56a942011-06-29 00:21:34 +0000786
787 /* Load up FP, VMX and VSX registers */
Paul Mackerras41f4e632018-10-08 16:30:51 +1100788 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000789 bl kvmppc_load_fp
790
791 ld r14, VCPU_GPR(R14)(r4)
792 ld r15, VCPU_GPR(R15)(r4)
793 ld r16, VCPU_GPR(R16)(r4)
794 ld r17, VCPU_GPR(R17)(r4)
795 ld r18, VCPU_GPR(R18)(r4)
796 ld r19, VCPU_GPR(R19)(r4)
797 ld r20, VCPU_GPR(R20)(r4)
798 ld r21, VCPU_GPR(R21)(r4)
799 ld r22, VCPU_GPR(R22)(r4)
800 ld r23, VCPU_GPR(R23)(r4)
801 ld r24, VCPU_GPR(R24)(r4)
802 ld r25, VCPU_GPR(R25)(r4)
803 ld r26, VCPU_GPR(R26)(r4)
804 ld r27, VCPU_GPR(R27)(r4)
805 ld r28, VCPU_GPR(R28)(r4)
806 ld r29, VCPU_GPR(R29)(r4)
807 ld r30, VCPU_GPR(R30)(r4)
808 ld r31, VCPU_GPR(R31)(r4)
809
Paul Mackerrasde56a942011-06-29 00:21:34 +0000810 /* Switch DSCR to guest value */
811 ld r5, VCPU_DSCR(r4)
812 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000813
Michael Neulingb005255e2014-01-08 21:25:21 +1100814BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100815 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100816 b 8f
817END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100818 /* Load up POWER8-specific registers */
819 ld r5, VCPU_IAMR(r4)
820 lwz r6, VCPU_PSPB(r4)
821 ld r7, VCPU_FSCR(r4)
822 mtspr SPRN_IAMR, r5
823 mtspr SPRN_PSPB, r6
824 mtspr SPRN_FSCR, r7
825 ld r5, VCPU_DAWR(r4)
826 ld r6, VCPU_DAWRX(r4)
827 ld r7, VCPU_CIABR(r4)
828 ld r8, VCPU_TAR(r4)
Michael Neulingb53221e2018-03-27 15:37:22 +1100829 /*
830 * Handle broken DAWR case by not writing it. This means we
831 * can still store the DAWR register for migration.
832 */
833BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +1100834 mtspr SPRN_DAWR, r5
835 mtspr SPRN_DAWRX, r6
Michael Neulingb53221e2018-03-27 15:37:22 +1100836END_FTR_SECTION_IFSET(CPU_FTR_DAWR)
Michael Neulingb005255e2014-01-08 21:25:21 +1100837 mtspr SPRN_CIABR, r7
838 mtspr SPRN_TAR, r8
839 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100840 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000841 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100842 mtspr SPRN_EBBHR, r8
843 ld r5, VCPU_EBBRR(r4)
844 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100845 lwz r7, VCPU_GUEST_PID(r4)
846 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100847 mtspr SPRN_EBBRR, r5
848 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100849 mtspr SPRN_PID, r7
850 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100851BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100852 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100853 ld r5, VCPU_TCSCR(r4)
854 ld r6, VCPU_ACOP(r4)
855 ld r7, VCPU_CSIGR(r4)
856 ld r8, VCPU_TACR(r4)
857 mtspr SPRN_TCSCR, r5
858 mtspr SPRN_ACOP, r6
859 mtspr SPRN_CSIGR, r7
860 mtspr SPRN_TACR, r8
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100861 nop
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100862FTR_SECTION_ELSE
863 /* POWER9-only registers */
864 ld r5, VCPU_TID(r4)
865 ld r6, VCPU_PSSCR(r4)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100866 lbz r8, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100867 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100868 rldimi r6, r8, PSSCR_FAKE_SUSPEND_LG, 63 - PSSCR_FAKE_SUSPEND_LG
Paul Mackerras769377f2017-02-15 14:30:17 +1100869 ld r7, VCPU_HFSCR(r4)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100870 mtspr SPRN_TIDR, r5
871 mtspr SPRN_PSSCR, r6
Paul Mackerras769377f2017-02-15 14:30:17 +1100872 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100873ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11008748:
875
Paul Mackerrasde56a942011-06-29 00:21:34 +0000876 ld r5, VCPU_SPRG0(r4)
877 ld r6, VCPU_SPRG1(r4)
878 ld r7, VCPU_SPRG2(r4)
879 ld r8, VCPU_SPRG3(r4)
880 mtspr SPRN_SPRG0, r5
881 mtspr SPRN_SPRG1, r6
882 mtspr SPRN_SPRG2, r7
883 mtspr SPRN_SPRG3, r8
884
Paul Mackerrasde56a942011-06-29 00:21:34 +0000885 /* Load up DAR and DSISR */
886 ld r5, VCPU_DAR(r4)
887 lwz r6, VCPU_DSISR(r4)
888 mtspr SPRN_DAR, r5
889 mtspr SPRN_DSISR, r6
890
Paul Mackerrasde56a942011-06-29 00:21:34 +0000891 /* Restore AMR and UAMOR, set AMOR to all 1s */
892 ld r5,VCPU_AMR(r4)
893 ld r6,VCPU_UAMOR(r4)
894 li r7,-1
895 mtspr SPRN_AMR,r5
896 mtspr SPRN_UAMOR,r6
897 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000898
899 /* Restore state of CTRL run bit; assume 1 on entry */
900 lwz r5,VCPU_CTRL(r4)
901 andi. r5,r5,1
902 bne 4f
903 mfspr r6,SPRN_CTRLF
904 clrrdi r6,r6,1
905 mtspr SPRN_CTRLT,r6
9064:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100907 /* Secondary threads wait for primary to have done partition switch */
908 ld r5, HSTATE_KVM_VCORE(r13)
909 lbz r6, HSTATE_PTID(r13)
910 cmpwi r6, 0
911 beq 21f
912 lbz r0, VCORE_IN_GUEST(r5)
913 cmpwi r0, 0
914 bne 21f
915 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100091620: lwz r3, VCORE_ENTRY_EXIT(r5)
917 cmpwi r3, 0x100
918 bge no_switch_exit
919 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100920 cmpwi r0, 0
921 beq 20b
922 HMT_MEDIUM
92321:
924 /* Set LPCR. */
925 ld r8,VCORE_LPCR(r5)
926 mtspr SPRN_LPCR,r8
927 isync
928
Paul Mackerras57b8daa2018-04-20 22:51:11 +1000929 /*
930 * Set the decrementer to the guest decrementer.
931 */
932 ld r8,VCPU_DEC_EXPIRES(r4)
933 /* r8 is a host timebase value here, convert to guest TB */
934 ld r5,HSTATE_KVM_VCORE(r13)
935 ld r6,VCORE_TB_OFFSET_APPL(r5)
936 add r8,r8,r6
937 mftb r7
938 subf r3,r7,r8
939 mtspr SPRN_DEC,r3
940
Paul Mackerras6af27c82015-03-28 14:21:10 +1100941 /* Check if HDEC expires soon */
942 mfspr r3, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000943 EXTEND_HDEC(r3)
944 cmpdi r3, 512 /* 1 microsecond */
Paul Mackerras6af27c82015-03-28 14:21:10 +1100945 blt hdec_soon
946
Paul Mackerras6964e6a2018-01-11 14:51:02 +1100947 /* For hash guest, clear out and reload the SLB */
948 ld r6, VCPU_KVM(r4)
949 lbz r0, KVM_RADIX(r6)
950 cmpwi r0, 0
951 bne 9f
952 li r6, 0
953 slbmte r6, r6
954 slbia
955 ptesync
956
957 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
958 lwz r5,VCPU_SLB_MAX(r4)
959 cmpwi r5,0
960 beq 9f
961 mtctr r5
962 addi r6,r4,VCPU_SLB
9631: ld r8,VCPU_SLB_E(r6)
964 ld r9,VCPU_SLB_V(r6)
965 slbmte r9,r8
966 addi r6,r6,VCPU_SLB_SIZE
967 bdnz 1b
9689:
969
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000970#ifdef CONFIG_KVM_XICS
971 /* We are entering the guest on that thread, push VCPU to XIVE */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000972 ld r11, VCPU_XIVE_SAVED_STATE(r4)
973 li r9, TM_QW1_OS
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000974 lwz r8, VCPU_XIVE_CAM_WORD(r4)
975 li r7, TM_QW1_OS + TM_WORD2
976 mfmsr r0
977 andi. r0, r0, MSR_DR /* in real mode? */
978 beq 2f
979 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
980 cmpldi cr1, r10, 0
981 beq cr1, no_xive
982 eieio
983 stdx r11,r9,r10
984 stwx r8,r7,r10
985 b 3f
9862: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
987 cmpldi cr1, r10, 0
988 beq cr1, no_xive
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000989 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +1100990 stdcix r11,r9,r10
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +1000991 stwcix r8,r7,r10
9923: li r9, 1
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +1100993 stb r9, VCPU_XIVE_PUSHED(r4)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +1100994 eieio
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +1100995
996 /*
997 * We clear the irq_pending flag. There is a small chance of a
998 * race vs. the escalation interrupt happening on another
999 * processor setting it again, but the only consequence is to
1000 * cause a spurrious wakeup on the next H_CEDE which is not an
1001 * issue.
1002 */
1003 li r0,0
1004 stb r0, VCPU_IRQ_PENDING(r4)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11001005
1006 /*
1007 * In single escalation mode, if the escalation interrupt is
1008 * on, we mask it.
1009 */
1010 lbz r0, VCPU_XIVE_ESC_ON(r4)
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +10001011 cmpwi cr1, r0,0
1012 beq cr1, 1f
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11001013 li r9, XIVE_ESB_SET_PQ_01
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +10001014 beq 4f /* in real mode? */
1015 ld r10, VCPU_XIVE_ESC_VADDR(r4)
1016 ldx r0, r10, r9
1017 b 5f
10184: ld r10, VCPU_XIVE_ESC_RADDR(r4)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11001019 ldcix r0, r10, r9
Suraj Jitindar Singh7ae9bda2019-04-29 18:57:45 +100010205: sync
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11001021
1022 /* We have a possible subtle race here: The escalation interrupt might
1023 * have fired and be on its way to the host queue while we mask it,
1024 * and if we unmask it early enough (re-cede right away), there is
1025 * a theorical possibility that it fires again, thus landing in the
1026 * target queue more than once which is a big no-no.
1027 *
1028 * Fortunately, solving this is rather easy. If the above load setting
1029 * PQ to 01 returns a previous value where P is set, then we know the
1030 * escalation interrupt is somewhere on its way to the host. In that
1031 * case we simply don't clear the xive_esc_on flag below. It will be
1032 * eventually cleared by the handler for the escalation interrupt.
1033 *
1034 * Then, when doing a cede, we check that flag again before re-enabling
1035 * the escalation interrupt, and if set, we abort the cede.
1036 */
1037 andi. r0, r0, XIVE_ESB_VAL_P
1038 bne- 1f
1039
1040 /* Now P is 0, we can clear the flag */
1041 li r0, 0
1042 stb r0, VCPU_XIVE_ESC_ON(r4)
10431:
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001044no_xive:
1045#endif /* CONFIG_KVM_XICS */
1046
Paul Mackerras95a64322018-10-08 16:30:55 +11001047 li r0, 0
1048 stw r0, STACK_SLOT_SHORT_PATH(r1)
1049
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001050deliver_guest_interrupt: /* r4 = vcpu, r13 = paca */
Paul Mackerrasf7035ce2018-10-08 16:30:50 +11001051 /* Check if we can deliver an external or decrementer interrupt now */
1052 ld r0, VCPU_PENDING_EXC(r4)
1053BEGIN_FTR_SECTION
1054 /* On POWER9, also check for emulated doorbell interrupt */
1055 lbz r3, VCPU_DBELL_REQ(r4)
1056 or r0, r0, r3
1057END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1058 cmpdi r0, 0
1059 beq 71f
1060 mr r3, r4
1061 bl kvmppc_guest_entry_inject_int
1062 ld r4, HSTATE_KVM_VCPU(r13)
106371:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001064 ld r6, VCPU_SRR0(r4)
1065 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001066 mtspr SPRN_SRR0, r6
1067 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001068
Paul Mackerras95a64322018-10-08 16:30:55 +11001069fast_guest_entry_c:
1070 ld r10, VCPU_PC(r4)
1071 ld r11, VCPU_MSR(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001072 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001073 rldicl r11, r11, 63 - MSR_HV_LG, 1
1074 rotldi r11, r11, 1 + MSR_HV_LG
1075 ori r11, r11, MSR_ME
1076
Paul Mackerrasf7035ce2018-10-08 16:30:50 +11001077 ld r6, VCPU_CTR(r4)
1078 ld r7, VCPU_XER(r4)
1079 mtctr r6
1080 mtxer r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10001081
Liu Ping Fan27025a62013-11-19 14:12:48 +08001082/*
1083 * Required state:
1084 * R4 = vcpu
1085 * R10: value for HSRR0
1086 * R11: value for HSRR1
1087 * R13 = PACA
1088 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001089fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001090 li r0,0
1091 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001092 mtspr SPRN_HSRR0,r10
1093 mtspr SPRN_HSRR1,r11
1094
1095 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001096 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001097 stb r9, HSTATE_IN_GUEST(r13)
1098
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001099#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1100 /* Accumulate timing */
1101 addi r3, r4, VCPU_TB_GUEST
1102 bl kvmhv_accumulate_time
1103#endif
1104
Paul Mackerrasde56a942011-06-29 00:21:34 +00001105 /* Enter guest */
1106
Paul Mackerras0acb9112013-02-04 18:10:51 +00001107BEGIN_FTR_SECTION
1108 ld r5, VCPU_CFAR(r4)
1109 mtspr SPRN_CFAR, r5
1110END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001111BEGIN_FTR_SECTION
1112 ld r0, VCPU_PPR(r4)
1113END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001114
Paul Mackerrasde56a942011-06-29 00:21:34 +00001115 ld r5, VCPU_LR(r4)
Paul Mackerrasfd0944b2018-10-08 16:30:58 +11001116 ld r6, VCPU_CR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001117 mtlr r5
1118 mtcr r6
1119
Michael Neulingc75df6f2012-06-25 13:33:10 +00001120 ld r1, VCPU_GPR(R1)(r4)
1121 ld r2, VCPU_GPR(R2)(r4)
1122 ld r3, VCPU_GPR(R3)(r4)
1123 ld r5, VCPU_GPR(R5)(r4)
1124 ld r6, VCPU_GPR(R6)(r4)
1125 ld r7, VCPU_GPR(R7)(r4)
1126 ld r8, VCPU_GPR(R8)(r4)
1127 ld r9, VCPU_GPR(R9)(r4)
1128 ld r10, VCPU_GPR(R10)(r4)
1129 ld r11, VCPU_GPR(R11)(r4)
1130 ld r12, VCPU_GPR(R12)(r4)
1131 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001132
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001133BEGIN_FTR_SECTION
1134 mtspr SPRN_PPR, r0
1135END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Michael Neulinge001fa72017-09-15 15:26:14 +10001136
1137/* Move canary into DSISR to check for later */
1138BEGIN_FTR_SECTION
1139 li r0, 0x7fff
1140 mtspr SPRN_HDSISR, r0
1141END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1142
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001143 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001144 ld r4, VCPU_GPR(R4)(r4)
Nicholas Piggin222f20f2018-01-10 03:07:15 +11001145 HRFI_TO_GUEST
Paul Mackerrasde56a942011-06-29 00:21:34 +00001146 b .
1147
Paul Mackerras95a64322018-10-08 16:30:55 +11001148/*
1149 * Enter the guest on a P9 or later system where we have exactly
1150 * one vcpu per vcore and we don't need to go to real mode
1151 * (which implies that host and guest are both using radix MMU mode).
1152 * r3 = vcpu pointer
1153 * Most SPRs and all the VSRs have been loaded already.
1154 */
1155_GLOBAL(__kvmhv_vcpu_entry_p9)
1156EXPORT_SYMBOL_GPL(__kvmhv_vcpu_entry_p9)
1157 mflr r0
1158 std r0, PPC_LR_STKOFF(r1)
1159 stdu r1, -SFS(r1)
1160
1161 li r0, 1
1162 stw r0, STACK_SLOT_SHORT_PATH(r1)
1163
1164 std r3, HSTATE_KVM_VCPU(r13)
1165 mfcr r4
1166 stw r4, SFS+8(r1)
1167
1168 std r1, HSTATE_HOST_R1(r13)
1169
1170 reg = 14
1171 .rept 18
1172 std reg, STACK_SLOT_NVGPRS + ((reg - 14) * 8)(r1)
1173 reg = reg + 1
1174 .endr
1175
1176 reg = 14
1177 .rept 18
1178 ld reg, __VCPU_GPR(reg)(r3)
1179 reg = reg + 1
1180 .endr
1181
1182 mfmsr r10
1183 std r10, HSTATE_HOST_MSR(r13)
1184
1185 mr r4, r3
1186 b fast_guest_entry_c
1187guest_exit_short_path:
1188
1189 li r0, KVM_GUEST_MODE_NONE
1190 stb r0, HSTATE_IN_GUEST(r13)
1191
1192 reg = 14
1193 .rept 18
1194 std reg, __VCPU_GPR(reg)(r9)
1195 reg = reg + 1
1196 .endr
1197
1198 reg = 14
1199 .rept 18
1200 ld reg, STACK_SLOT_NVGPRS + ((reg - 14) * 8)(r1)
1201 reg = reg + 1
1202 .endr
1203
1204 lwz r4, SFS+8(r1)
1205 mtcr r4
1206
1207 mr r3, r12 /* trap number */
1208
1209 addi r1, r1, SFS
1210 ld r0, PPC_LR_STKOFF(r1)
1211 mtlr r0
1212
1213 /* If we are in real mode, do a rfid to get back to the caller */
1214 mfmsr r4
1215 andi. r5, r4, MSR_IR
1216 bnelr
1217 rldicl r5, r4, 64 - MSR_TS_S_LG, 62 /* extract TS field */
1218 mtspr SPRN_SRR0, r0
1219 ld r10, HSTATE_HOST_MSR(r13)
1220 rldimi r10, r5, MSR_TS_S_LG, 63 - MSR_TS_T_LG
1221 mtspr SPRN_SRR1, r10
1222 RFI_TO_KERNEL
1223 b .
1224
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001225secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001226 li r12, 0
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001227 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001228 cmpdi r4, 0
1229 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001230 stw r12, VCPU_TRAP(r4)
1231#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001232 addi r3, r4, VCPU_TB_RMEXIT
1233 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001234#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100123511: b kvmhv_switch_to_host
1236
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001237no_switch_exit:
1238 HMT_MEDIUM
1239 li r12, 0
1240 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001241hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001242 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000124312: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001244 mr r9, r4
1245#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001246 addi r3, r4, VCPU_TB_RMEXIT
1247 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001248#endif
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001249 b guest_bypass
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001250
Paul Mackerrasde56a942011-06-29 00:21:34 +00001251/******************************************************************************
1252 * *
1253 * Exit code *
1254 * *
1255 *****************************************************************************/
1256
1257/*
1258 * We come here from the first-level interrupt handlers.
1259 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301260 .globl kvmppc_interrupt_hv
1261kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001262 /*
1263 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001264 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001265 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001266 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001267 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001268 * guest R13 saved in SPRN_SCRATCH0
1269 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001270 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001271 lbz r9, HSTATE_IN_GUEST(r13)
1272 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1273 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301274#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1275 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001276 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301277 beq kvmppc_interrupt_pr
1278#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001279 /* We're now back in the host but in guest MMU context */
1280 li r9, KVM_GUEST_MODE_HOST_HV
1281 stb r9, HSTATE_IN_GUEST(r13)
1282
Paul Mackerrasde56a942011-06-29 00:21:34 +00001283 ld r9, HSTATE_KVM_VCPU(r13)
1284
1285 /* Save registers */
1286
Michael Neulingc75df6f2012-06-25 13:33:10 +00001287 std r0, VCPU_GPR(R0)(r9)
1288 std r1, VCPU_GPR(R1)(r9)
1289 std r2, VCPU_GPR(R2)(r9)
1290 std r3, VCPU_GPR(R3)(r9)
1291 std r4, VCPU_GPR(R4)(r9)
1292 std r5, VCPU_GPR(R5)(r9)
1293 std r6, VCPU_GPR(R6)(r9)
1294 std r7, VCPU_GPR(R7)(r9)
1295 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001296 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001297 std r0, VCPU_GPR(R9)(r9)
1298 std r10, VCPU_GPR(R10)(r9)
1299 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001300 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001301 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001302 /* CR is in the high half of r12 */
1303 srdi r4, r12, 32
Paul Mackerrasfd0944b2018-10-08 16:30:58 +11001304 std r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001305BEGIN_FTR_SECTION
1306 ld r3, HSTATE_CFAR(r13)
1307 std r3, VCPU_CFAR(r9)
1308END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001309BEGIN_FTR_SECTION
1310 ld r4, HSTATE_PPR(r13)
1311 std r4, VCPU_PPR(r9)
1312END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001313
1314 /* Restore R1/R2 so we can handle faults */
1315 ld r1, HSTATE_HOST_R1(r13)
1316 ld r2, PACATOC(r13)
1317
1318 mfspr r10, SPRN_SRR0
1319 mfspr r11, SPRN_SRR1
1320 std r10, VCPU_SRR0(r9)
1321 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001322 /* trap is in the low half of r12, clear CR from the high half */
1323 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001324 andi. r0, r12, 2 /* need to read HSRR0/1? */
1325 beq 1f
1326 mfspr r10, SPRN_HSRR0
1327 mfspr r11, SPRN_HSRR1
1328 clrrdi r12, r12, 2
13291: std r10, VCPU_PC(r9)
1330 std r11, VCPU_MSR(r9)
1331
1332 GET_SCRATCH0(r3)
1333 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001334 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001335 std r4, VCPU_LR(r9)
1336
Paul Mackerrasde56a942011-06-29 00:21:34 +00001337 stw r12,VCPU_TRAP(r9)
1338
Paul Mackerras8b24e692017-06-26 15:45:51 +10001339 /*
1340 * Now that we have saved away SRR0/1 and HSRR0/1,
1341 * interrupts are recoverable in principle, so set MSR_RI.
1342 * This becomes important for relocation-on interrupts from
1343 * the guest, which we can get in radix mode on POWER9.
1344 */
1345 li r0, MSR_RI
1346 mtmsrd r0, 1
1347
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001348#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1349 addi r3, r9, VCPU_TB_RMINTR
1350 mr r4, r9
1351 bl kvmhv_accumulate_time
1352 ld r5, VCPU_GPR(R5)(r9)
1353 ld r6, VCPU_GPR(R6)(r9)
1354 ld r7, VCPU_GPR(R7)(r9)
1355 ld r8, VCPU_GPR(R8)(r9)
1356#endif
1357
Paul Mackerras4a157d62014-12-03 13:30:39 +11001358 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001359 if this is an HEI (HV emulation interrupt, e40) */
1360 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001361 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001362 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1363 bne 11f
1364 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100136511: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001366
1367 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001368#ifdef CONFIG_RELOCATABLE
1369 ld r3, HSTATE_SCRATCH1(r13)
1370 mtctr r3
1371#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001372 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001373#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001374 mfxer r4
1375 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001376 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001377
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001378 /* Save more register state */
1379 mfdar r3
1380 mfdsisr r4
1381 std r3, VCPU_DAR(r9)
1382 stw r4, VCPU_DSISR(r9)
1383
1384 /* If this is a page table miss then see if it's theirs or ours */
1385 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1386 beq kvmppc_hdsi
1387 std r3, VCPU_FAULT_DAR(r9)
1388 stw r4, VCPU_FAULT_DSISR(r9)
1389 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1390 beq kvmppc_hisi
1391
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001392#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1393 /* For softpatch interrupt, go off and do TM instruction emulation */
1394 cmpwi r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
1395 beq kvmppc_tm_emul
1396#endif
1397
Paul Mackerrasde56a942011-06-29 00:21:34 +00001398 /* See if this is a leftover HDEC interrupt */
1399 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1400 bne 2f
1401 mfspr r3,SPRN_HDEC
Paul Mackerrasa4faf2e2017-08-25 19:52:12 +10001402 EXTEND_HDEC(r3)
1403 cmpdi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001404 mr r4,r9
1405 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000014062:
Paul Mackerras697d3892011-12-12 12:36:37 +00001407 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001408 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1409 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001410
Paul Mackerras66feed62015-03-28 14:21:12 +11001411 /* Hypervisor doorbell - exit only if host IPI flag set */
1412 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1413 bne 3f
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001414BEGIN_FTR_SECTION
1415 PPC_MSGSYNC
Nicholas Piggin2cde3712017-10-10 20:18:28 +10001416 lwsync
Paul Mackerras360cae32018-10-08 16:31:04 +11001417 /* always exit if we're running a nested guest */
1418 ld r0, VCPU_NESTED(r9)
1419 cmpdi r0, 0
1420 bne guest_exit_cont
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001421END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11001422 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301423 cmpwi r0, 0
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001424 beq maybe_reenter_guest
Paul Mackerras66feed62015-03-28 14:21:12 +11001425 b guest_exit_cont
14263:
Paul Mackerras769377f2017-02-15 14:30:17 +11001427 /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1428 cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1429 bne 14f
1430 mfspr r3, SPRN_HFSCR
1431 std r3, VCPU_HFSCR(r9)
1432 b guest_exit_cont
143314:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001434 /* External interrupt ? */
1435 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001436 beq kvmppc_guest_external
Paul Mackerras43ff3f62018-01-11 14:31:43 +11001437 /* See if it is a machine check */
1438 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1439 beq machine_check_realmode
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001440 /* Or a hypervisor maintenance interrupt */
1441 cmpwi r12, BOOK3S_INTERRUPT_HMI
1442 beq hmi_realmode
1443
1444guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
1445
Paul Mackerras43ff3f62018-01-11 14:31:43 +11001446#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1447 addi r3, r9, VCPU_TB_RMEXIT
1448 mr r4, r9
1449 bl kvmhv_accumulate_time
1450#endif
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001451#ifdef CONFIG_KVM_XICS
1452 /* We are exiting, pull the VP from the XIVE */
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001453 lbz r0, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001454 cmpwi cr0, r0, 0
1455 beq 1f
1456 li r7, TM_SPC_PULL_OS_CTX
1457 li r6, TM_QW1_OS
1458 mfmsr r0
Benjamin Herrenschmidt2662efd2018-01-12 13:37:14 +11001459 andi. r0, r0, MSR_DR /* in real mode? */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001460 beq 2f
1461 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1462 cmpldi cr0, r10, 0
1463 beq 1f
1464 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001465 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001466 lwzx r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001467 /* Second load to recover the context state (Words 0 and 1) */
1468 ldx r11, r6, r10
1469 b 3f
14702: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1471 cmpldi cr0, r10, 0
1472 beq 1f
1473 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001474 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001475 lwzcix r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001476 /* Second load to recover the context state (Words 0 and 1) */
1477 ldcix r11, r6, r10
14783: std r11, VCPU_XIVE_SAVED_STATE(r9)
1479 /* Fixup some of the state for the next load */
1480 li r10, 0
1481 li r0, 0xff
Benjamin Herrenschmidt35c24052018-01-12 13:37:15 +11001482 stb r10, VCPU_XIVE_PUSHED(r9)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001483 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1484 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001485 eieio
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100014861:
1487#endif /* CONFIG_KVM_XICS */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001488
Paul Mackerras95a64322018-10-08 16:30:55 +11001489 /* If we came in through the P9 short path, go back out to C now */
1490 lwz r0, STACK_SLOT_SHORT_PATH(r1)
1491 cmpwi r0, 0
1492 bne guest_exit_short_path
1493
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001494 /* For hash guest, read the guest SLB and save it away */
1495 ld r5, VCPU_KVM(r9)
1496 lbz r0, KVM_RADIX(r5)
1497 li r5, 0
1498 cmpwi r0, 0
1499 bne 3f /* for radix, save 0 entries */
1500 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1501 mtctr r0
1502 li r6,0
1503 addi r7,r9,VCPU_SLB
15041: slbmfee r8,r6
1505 andis. r0,r8,SLB_ESID_V@h
1506 beq 2f
1507 add r8,r8,r6 /* put index in */
1508 slbmfev r3,r6
1509 std r8,VCPU_SLB_E(r7)
1510 std r3,VCPU_SLB_V(r7)
1511 addi r7,r7,VCPU_SLB_SIZE
1512 addi r5,r5,1
15132: addi r6,r6,1
1514 bdnz 1b
1515 /* Finally clear out the SLB */
1516 li r0,0
1517 slbmte r0,r0
1518 slbia
1519 ptesync
15203: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001521
Paul Mackerrascda4a142018-03-22 09:48:54 +11001522 /* load host SLB entries */
1523BEGIN_MMU_FTR_SECTION
1524 b 0f
1525END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
1526 ld r8,PACA_SLBSHADOWPTR(r13)
1527
1528 .rept SLB_NUM_BOLTED
1529 li r3, SLBSHADOW_SAVEAREA
1530 LDX_BE r5, r8, r3
1531 addi r3, r3, 8
1532 LDX_BE r6, r8, r3
1533 andis. r7,r5,SLB_ESID_V@h
1534 beq 1f
1535 slbmte r6,r5
15361: addi r8,r8,16
1537 .endr
15380:
1539
Paul Mackerras6964e6a2018-01-11 14:51:02 +11001540guest_bypass:
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001541 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001542
1543 /* Save DEC */
1544 /* Do this before kvmhv_commence_exit so we know TB is guest TB */
1545 ld r3, HSTATE_KVM_VCORE(r13)
1546 mfspr r5,SPRN_DEC
1547 mftb r6
1548 /* On P9, if the guest has large decr enabled, don't sign extend */
1549BEGIN_FTR_SECTION
1550 ld r4, VCORE_LPCR(r3)
1551 andis. r4, r4, LPCR_LD@h
1552 bne 16f
1553END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1554 extsw r5,r5
155516: add r5,r5,r6
1556 /* r5 is a guest timebase value here, convert to host TB */
1557 ld r4,VCORE_TB_OFFSET_APPL(r3)
1558 subf r5,r4,r5
1559 std r5,VCPU_DEC_EXPIRES(r9)
1560
Paul Mackerras6af27c82015-03-28 14:21:10 +11001561 /* Increment exit count, poke other threads to exit */
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001562 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001563 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001564 nop
1565 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001566
Paul Mackerrasec257162015-06-24 21:18:03 +10001567 /* Stop others sending VCPU interrupts to this physical CPU */
1568 li r0, -1
1569 stw r0, VCPU_CPU(r9)
1570 stw r0, VCPU_THREAD_CPU(r9)
1571
Paul Mackerrasde56a942011-06-29 00:21:34 +00001572 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001573 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001574 stw r6,VCPU_CTRL(r9)
1575 andi. r0,r6,1
1576 bne 4f
1577 ori r6,r6,1
1578 mtspr SPRN_CTRLT,r6
15794:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001580 /*
1581 * Save the guest PURR/SPURR
1582 */
1583 mfspr r5,SPRN_PURR
1584 mfspr r6,SPRN_SPURR
1585 ld r7,VCPU_PURR(r9)
1586 ld r8,VCPU_SPURR(r9)
1587 std r5,VCPU_PURR(r9)
1588 std r6,VCPU_SPURR(r9)
1589 subf r5,r7,r5
1590 subf r6,r8,r6
1591
1592 /*
1593 * Restore host PURR/SPURR and add guest times
1594 * so that the time in the guest gets accounted.
1595 */
1596 ld r3,HSTATE_PURR(r13)
1597 ld r4,HSTATE_SPURR(r13)
1598 add r3,r3,r5
1599 add r4,r4,r6
1600 mtspr SPRN_PURR,r3
1601 mtspr SPRN_SPURR,r4
1602
Michael Neulingb005255e2014-01-08 21:25:21 +11001603BEGIN_FTR_SECTION
1604 b 8f
1605END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001606 /* Save POWER8-specific registers */
1607 mfspr r5, SPRN_IAMR
1608 mfspr r6, SPRN_PSPB
1609 mfspr r7, SPRN_FSCR
1610 std r5, VCPU_IAMR(r9)
1611 stw r6, VCPU_PSPB(r9)
1612 std r7, VCPU_FSCR(r9)
1613 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001614 mfspr r7, SPRN_TAR
1615 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001616 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001617 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001618 std r8, VCPU_EBBHR(r9)
1619 mfspr r5, SPRN_EBBRR
1620 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001621 mfspr r7, SPRN_PID
1622 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001623 std r5, VCPU_EBBRR(r9)
1624 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001625 stw r7, VCPU_GUEST_PID(r9)
1626 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001627BEGIN_FTR_SECTION
1628 mfspr r5, SPRN_TCSCR
1629 mfspr r6, SPRN_ACOP
1630 mfspr r7, SPRN_CSIGR
1631 mfspr r8, SPRN_TACR
1632 std r5, VCPU_TCSCR(r9)
1633 std r6, VCPU_ACOP(r9)
1634 std r7, VCPU_CSIGR(r9)
1635 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001636FTR_SECTION_ELSE
1637 mfspr r5, SPRN_TIDR
1638 mfspr r6, SPRN_PSSCR
1639 std r5, VCPU_TID(r9)
1640 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1641 rotldi r6, r6, 60
1642 std r6, VCPU_PSSCR(r9)
Paul Mackerras769377f2017-02-15 14:30:17 +11001643 /* Restore host HFSCR value */
1644 ld r7, STACK_SLOT_HFSCR(r1)
1645 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001646ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001647 /*
1648 * Restore various registers to 0, where non-zero values
1649 * set by the guest could disrupt the host.
1650 */
1651 li r0, 0
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001652 mtspr SPRN_PSPB, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001653 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001654BEGIN_FTR_SECTION
1655 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001656 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1657 li r0, 1
1658 sldi r0, r0, 31
1659 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001660END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11001661
Michael Ellermanc3c7470c2019-02-22 13:22:08 +11001662 /* Save and restore AMR, IAMR and UAMOR before turning on the MMU */
1663 ld r8, STACK_SLOT_IAMR(r1)
1664 mtspr SPRN_IAMR, r8
1665
16668: /* Power7 jumps back in here */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001667 mfspr r5,SPRN_AMR
1668 mfspr r6,SPRN_UAMOR
1669 std r5,VCPU_AMR(r9)
1670 std r6,VCPU_UAMOR(r9)
Michael Ellermanc3c7470c2019-02-22 13:22:08 +11001671 ld r5,STACK_SLOT_AMR(r1)
1672 ld r6,STACK_SLOT_UAMOR(r1)
1673 mtspr SPRN_AMR, r5
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001674 mtspr SPRN_UAMOR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001675
Paul Mackerrasde56a942011-06-29 00:21:34 +00001676 /* Switch DSCR back to host value */
1677 mfspr r8, SPRN_DSCR
1678 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001679 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001680 mtspr SPRN_DSCR, r7
1681
1682 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001683 std r14, VCPU_GPR(R14)(r9)
1684 std r15, VCPU_GPR(R15)(r9)
1685 std r16, VCPU_GPR(R16)(r9)
1686 std r17, VCPU_GPR(R17)(r9)
1687 std r18, VCPU_GPR(R18)(r9)
1688 std r19, VCPU_GPR(R19)(r9)
1689 std r20, VCPU_GPR(R20)(r9)
1690 std r21, VCPU_GPR(R21)(r9)
1691 std r22, VCPU_GPR(R22)(r9)
1692 std r23, VCPU_GPR(R23)(r9)
1693 std r24, VCPU_GPR(R24)(r9)
1694 std r25, VCPU_GPR(R25)(r9)
1695 std r26, VCPU_GPR(R26)(r9)
1696 std r27, VCPU_GPR(R27)(r9)
1697 std r28, VCPU_GPR(R28)(r9)
1698 std r29, VCPU_GPR(R29)(r9)
1699 std r30, VCPU_GPR(R30)(r9)
1700 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001701
1702 /* Save SPRGs */
1703 mfspr r3, SPRN_SPRG0
1704 mfspr r4, SPRN_SPRG1
1705 mfspr r5, SPRN_SPRG2
1706 mfspr r6, SPRN_SPRG3
1707 std r3, VCPU_SPRG0(r9)
1708 std r4, VCPU_SPRG1(r9)
1709 std r5, VCPU_SPRG2(r9)
1710 std r6, VCPU_SPRG3(r9)
1711
Paul Mackerras89436332012-03-02 01:38:23 +00001712 /* save FP state */
1713 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001714 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001715
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001716#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001717/*
1718 * Branch around the call if both CPU_FTR_TM and
1719 * CPU_FTR_P9_TM_HV_ASSIST are off.
1720 */
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001721BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001722 b 91f
1723END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001724 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11001725 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001726 */
Simon Guo6f597c62018-05-23 15:01:48 +08001727 mr r3, r9
1728 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11001729 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10001730 bl kvmppc_save_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11001731 nop
Simon Guo6f597c62018-05-23 15:01:48 +08001732 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100173391:
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001734#endif
1735
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001736 /* Increment yield count if they have a VPA */
1737 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1738 cmpdi r8, 0
1739 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001740 li r4, LPPACA_YIELDCOUNT
1741 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001742 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001743 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001744 li r3, 1
1745 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000174625:
1747 /* Save PMU registers if requested */
1748 /* r8 and cr0.eq are live here */
Paul Mackerras41f4e632018-10-08 16:30:51 +11001749 mr r3, r9
1750 li r4, 1
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001751 beq 21f /* if no VPA, save PMU stuff anyway */
Paul Mackerras41f4e632018-10-08 16:30:51 +11001752 lbz r4, LPPACA_PMCINUSE(r8)
175321: bl kvmhv_save_guest_pmu
1754 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001755
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001756 /* Restore host values of some registers */
1757BEGIN_FTR_SECTION
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001758 ld r5, STACK_SLOT_CIABR(r1)
1759 ld r6, STACK_SLOT_DAWR(r1)
1760 ld r7, STACK_SLOT_DAWRX(r1)
1761 mtspr SPRN_CIABR, r5
Michael Neulingb53221e2018-03-27 15:37:22 +11001762 /*
1763 * If the DAWR doesn't work, it's ok to write these here as
1764 * this value should always be zero
1765 */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001766 mtspr SPRN_DAWR, r6
1767 mtspr SPRN_DAWRX, r7
1768END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1769BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001770 ld r5, STACK_SLOT_TID(r1)
1771 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001772 ld r7, STACK_SLOT_PID(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001773 mtspr SPRN_TIDR, r5
1774 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001775 mtspr SPRN_PID, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001776END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001777
1778#ifdef CONFIG_PPC_RADIX_MMU
1779 /*
1780 * Are we running hash or radix ?
1781 */
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001782 ld r5, VCPU_KVM(r9)
1783 lbz r0, KVM_RADIX(r5)
1784 cmpwi cr2, r0, 0
Nicholas Piggin2bf10712018-07-05 18:47:00 +10001785 beq cr2, 2f
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001786
Paul Mackerrasdf158182018-05-17 14:47:59 +10001787 /*
1788 * Radix: do eieio; tlbsync; ptesync sequence in case we
1789 * interrupted the guest between a tlbie and a ptesync.
1790 */
1791 eieio
1792 tlbsync
1793 ptesync
1794
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001795 /* Radix: Handle the case where the guest used an illegal PID */
1796 LOAD_REG_ADDR(r4, mmu_base_pid)
1797 lwz r3, VCPU_GUEST_PID(r9)
1798 lwz r5, 0(r4)
1799 cmpw cr0,r3,r5
1800 blt 2f
1801
1802 /*
1803 * Illegal PID, the HW might have prefetched and cached in the TLB
1804 * some translations for the LPID 0 / guest PID combination which
1805 * Linux doesn't know about, so we need to flush that PID out of
1806 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1807 * the right context.
1808 */
1809 li r0,0
1810 mtspr SPRN_LPID,r0
1811 isync
1812
1813 /* Then do a congruence class local flush */
1814 ld r6,VCPU_KVM(r9)
1815 lwz r0,KVM_TLB_SETS(r6)
1816 mtctr r0
1817 li r7,0x400 /* IS field = 0b01 */
1818 ptesync
1819 sldi r0,r3,32 /* RS has PID */
18201: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
1821 addi r7,r7,0x1000
1822 bdnz 1b
1823 ptesync
1824
Nicholas Piggin2bf10712018-07-05 18:47:00 +100018252:
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001826#endif /* CONFIG_PPC_RADIX_MMU */
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001827
Paul Mackerrasde56a942011-06-29 00:21:34 +00001828 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001829 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001830 * We don't have to lock against tlbies but we do
1831 * have to coordinate the hardware threads.
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001832 * Here STACK_SLOT_TRAP(r1) contains the trap number.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001833 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001834kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001835 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001836 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001837 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1838 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001839 cmpwi r3,0
1840 beq 15f
1841 HMT_LOW
184213: lbz r3,VCORE_IN_GUEST(r5)
1843 cmpwi r3,0
1844 bne 13b
1845 HMT_MEDIUM
1846 b 16f
1847
1848 /* Primary thread waits for all the secondaries to exit guest */
184915: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001850 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001851 clrldi r3,r3,56
1852 cmpw r3,r0
1853 bne 15b
1854 isync
1855
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001856 /* Did we actually switch to the guest at all? */
1857 lbz r6, VCORE_IN_GUEST(r5)
1858 cmpwi r6, 0
1859 beq 19f
1860
Paul Mackerrasde56a942011-06-29 00:21:34 +00001861 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001862 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001863BEGIN_FTR_SECTION
1864 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001865 li r8,LPID_RSVD /* switch to reserved LPID */
1866 mtspr SPRN_LPID,r8
1867 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001868 mtspr SPRN_SDR1,r6 /* switch to host page table */
1869END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001870 mtspr SPRN_LPID,r7
1871 isync
1872
Michael Neulingb005255e2014-01-08 21:25:21 +11001873BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001874 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001875 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001876 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001877 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001878 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001879 /* clear DPDES so we don't get guest doorbells in the host */
1880 li r8, 0
1881 mtspr SPRN_DPDES, r8
1882END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1883
Paul Mackerrasde56a942011-06-29 00:21:34 +00001884 /* Subtract timebase offset from timebase */
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001885 ld r8, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001886 cmpdi r8,0
1887 beq 17f
Paul Mackerras57b8daa2018-04-20 22:51:11 +10001888 li r0, 0
1889 std r0, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001890 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001891 subf r8,r8,r6
1892 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1893 mftb r7 /* check if lower 24 bits overflowed */
1894 clrldi r6,r6,40
1895 clrldi r7,r7,40
1896 cmpld r7,r6
1897 bge 17f
1898 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1899 mtspr SPRN_TBU40,r8
1900
Paul Mackerrasdf709a22018-10-08 16:30:52 +1100190117:
1902 /*
1903 * If this is an HMI, we called kvmppc_realmode_hmi_handler
1904 * above, which may or may not have already called
1905 * kvmppc_subcore_exit_guest. Fortunately, all that
1906 * kvmppc_subcore_exit_guest does is clear a flag, so calling
1907 * it again here is benign even if kvmppc_realmode_hmi_handler
1908 * has already called it.
1909 */
1910 bl kvmppc_subcore_exit_guest
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301911 nop
191230: ld r5,HSTATE_KVM_VCORE(r13)
1913 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1914
Paul Mackerrasde56a942011-06-29 00:21:34 +00001915 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301916 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001917 cmpdi r0, 0
1918 beq 18f
1919 li r0, 0
1920 mtspr SPRN_PCR, r0
192118:
1922 /* Signal secondary CPUs to continue */
1923 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000192419: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001925 mtspr SPRN_HDEC,r8
1926
Paul Mackerrasc0101502017-10-19 14:11:23 +1100192716:
1928BEGIN_FTR_SECTION
1929 /* On POWER9 with HPT-on-radix we need to wait for all other threads */
1930 ld r3, HSTATE_SPLIT_MODE(r13)
1931 cmpdi r3, 0
1932 beq 47f
1933 lwz r8, KVM_SPLIT_DO_RESTORE(r3)
1934 cmpwi r8, 0
1935 beq 47f
Paul Mackerrasc0101502017-10-19 14:11:23 +11001936 bl kvmhv_p9_restore_lpcr
1937 nop
Paul Mackerrasc0101502017-10-19 14:11:23 +11001938 b 48f
193947:
1940END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1941 ld r8,KVM_HOST_LPCR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001942 mtspr SPRN_LPCR,r8
1943 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100194448:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001945#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1946 /* Finish timing, if we have a vcpu */
1947 ld r4, HSTATE_KVM_VCPU(r13)
1948 cmpdi r4, 0
1949 li r3, 0
1950 beq 2f
1951 bl kvmhv_accumulate_time
19522:
1953#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001954 /* Unset guest mode */
1955 li r0, KVM_GUEST_MODE_NONE
1956 stb r0, HSTATE_IN_GUEST(r13)
1957
Paul Mackerrasa8b48a42018-03-07 22:17:20 +11001958 lwz r12, STACK_SLOT_TRAP(r1) /* return trap # in r12 */
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001959 ld r0, SFS+PPC_LR_STKOFF(r1)
1960 addi r1, r1, SFS
Paul Mackerras218309b2013-09-06 13:23:44 +10001961 mtlr r0
1962 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001963
Paul Mackerrasdf709a22018-10-08 16:30:52 +11001964kvmppc_guest_external:
1965 /* External interrupt, first check for host_ipi. If this is
1966 * set, we know the host wants us out so let's do it now
1967 */
1968 bl kvmppc_read_intr
1969
1970 /*
1971 * Restore the active volatile registers after returning from
1972 * a C function.
1973 */
1974 ld r9, HSTATE_KVM_VCPU(r13)
1975 li r12, BOOK3S_INTERRUPT_EXTERNAL
1976
1977 /*
1978 * kvmppc_read_intr return codes:
1979 *
1980 * Exit to host (r3 > 0)
1981 * 1 An interrupt is pending that needs to be handled by the host
1982 * Exit guest and return to host by branching to guest_exit_cont
1983 *
1984 * 2 Passthrough that needs completion in the host
1985 * Exit guest and return to host by branching to guest_exit_cont
1986 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1987 * to indicate to the host to complete handling the interrupt
1988 *
1989 * Before returning to guest, we check if any CPU is heading out
1990 * to the host and if so, we head out also. If no CPUs are heading
1991 * check return values <= 0.
1992 *
1993 * Return to guest (r3 <= 0)
1994 * 0 No external interrupt is pending
1995 * -1 A guest wakeup IPI (which has now been cleared)
1996 * In either case, we return to guest to deliver any pending
1997 * guest interrupts.
1998 *
1999 * -2 A PCI passthrough external interrupt was handled
2000 * (interrupt was delivered directly to guest)
2001 * Return to guest to deliver any pending guest interrupts.
2002 */
2003
2004 cmpdi r3, 1
2005 ble 1f
2006
2007 /* Return code = 2 */
2008 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
2009 stw r12, VCPU_TRAP(r9)
2010 b guest_exit_cont
2011
20121: /* Return code <= 1 */
2013 cmpdi r3, 0
2014 bgt guest_exit_cont
2015
2016 /* Return code <= 0 */
2017maybe_reenter_guest:
2018 ld r5, HSTATE_KVM_VCORE(r13)
2019 lwz r0, VCORE_ENTRY_EXIT(r5)
2020 cmpwi r0, 0x100
2021 mr r4, r9
2022 blt deliver_guest_interrupt
2023 b guest_exit_cont
2024
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002025#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2026/*
2027 * Softpatch interrupt for transactional memory emulation cases
2028 * on POWER9 DD2.2. This is early in the guest exit path - we
2029 * haven't saved registers or done a treclaim yet.
2030 */
2031kvmppc_tm_emul:
2032 /* Save instruction image in HEIR */
2033 mfspr r3, SPRN_HEIR
2034 stw r3, VCPU_HEIR(r9)
2035
2036 /*
2037 * The cases we want to handle here are those where the guest
2038 * is in real suspend mode and is trying to transition to
2039 * transactional mode.
2040 */
2041 lbz r0, HSTATE_FAKE_SUSPEND(r13)
2042 cmpwi r0, 0 /* keep exiting guest if in fake suspend */
2043 bne guest_exit_cont
2044 rldicl r3, r11, 64 - MSR_TS_S_LG, 62
2045 cmpwi r3, 1 /* or if not in suspend state */
2046 bne guest_exit_cont
2047
2048 /* Call C code to do the emulation */
2049 mr r3, r9
2050 bl kvmhv_p9_tm_emulation_early
2051 nop
2052 ld r9, HSTATE_KVM_VCPU(r13)
2053 li r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
2054 cmpwi r3, 0
2055 beq guest_exit_cont /* continue exiting if not handled */
2056 ld r10, VCPU_PC(r9)
2057 ld r11, VCPU_MSR(r9)
2058 b fast_interrupt_c_return /* go back to guest if handled */
2059#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
2060
Paul Mackerras697d3892011-12-12 12:36:37 +00002061/*
2062 * Check whether an HDSI is an HPTE not found fault or something else.
2063 * If it is an HPTE not found fault that is due to the guest accessing
2064 * a page that they have mapped but which we have paged out, then
2065 * we continue on with the guest exit path. In all other cases,
2066 * reflect the HDSI to the guest as a DSI.
2067 */
2068kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002069 ld r3, VCPU_KVM(r9)
2070 lbz r0, KVM_RADIX(r3)
Paul Mackerras697d3892011-12-12 12:36:37 +00002071 mfspr r4, SPRN_HDAR
2072 mfspr r6, SPRN_HDSISR
Michael Neulinge001fa72017-09-15 15:26:14 +10002073BEGIN_FTR_SECTION
2074 /* Look for DSISR canary. If we find it, retry instruction */
2075 cmpdi r6, 0x7fff
2076 beq 6f
2077END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2078 cmpwi r0, 0
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002079 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00002080 /* HPTE not found fault or protection fault? */
2081 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00002082 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002083 andi. r0, r11, MSR_DR /* data relocation enabled? */
2084 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002085BEGIN_FTR_SECTION
2086 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2087 b 4f
2088END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00002089 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002090 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002091 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
2092 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000020934: std r4, VCPU_FAULT_DAR(r9)
2094 stw r6, VCPU_FAULT_DSISR(r9)
2095
2096 /* Search the hash table. */
2097 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002098 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002099 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00002100 ld r9, HSTATE_KVM_VCPU(r13)
2101 ld r10, VCPU_PC(r9)
2102 ld r11, VCPU_MSR(r9)
2103 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
2104 cmpdi r3, 0 /* retry the instruction */
2105 beq 6f
2106 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002107 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00002108 cmpdi r3, -2 /* MMIO emulation; need instr word */
2109 beq 2f
2110
Paul Mackerrascf29b212015-10-27 16:10:20 +11002111 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00002112 ld r4, VCPU_FAULT_DAR(r9)
2113 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110021141: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00002115 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110021167: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00002117 mtspr SPRN_SRR0, r10
2118 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002119 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002120 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002121fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000021226: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10002123 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00002124 mtctr r7
2125 mtxer r8
2126 mr r4, r9
2127 b fast_guest_return
2128
21293: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
2130 ld r5, KVM_VRMA_SLB_V(r5)
2131 b 4b
2132
2133 /* If this is for emulated MMIO, load the instruction word */
21342: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
2135
2136 /* Set guest mode to 'jump over instruction' so if lwz faults
2137 * we'll just continue at the next IP. */
2138 li r0, KVM_GUEST_MODE_SKIP
2139 stb r0, HSTATE_IN_GUEST(r13)
2140
2141 /* Do the access with MSR:DR enabled */
2142 mfmsr r3
2143 ori r4, r3, MSR_DR /* Enable paging for data */
2144 mtmsrd r4
2145 lwz r8, 0(r10)
2146 mtmsrd r3
2147
2148 /* Store the result */
2149 stw r8, VCPU_LAST_INST(r9)
2150
2151 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10002152 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00002153 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002154 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00002155
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002156.Lradix_hdsi:
2157 std r4, VCPU_FAULT_DAR(r9)
2158 stw r6, VCPU_FAULT_DSISR(r9)
2159.Lradix_hisi:
2160 mfspr r5, SPRN_ASDR
2161 std r5, VCPU_FAULT_GPA(r9)
2162 b guest_exit_cont
2163
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002164/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00002165 * Similarly for an HISI, reflect it to the guest as an ISI unless
2166 * it is an HPTE not found fault for a page that we have paged out.
2167 */
2168kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002169 ld r3, VCPU_KVM(r9)
2170 lbz r0, KVM_RADIX(r3)
2171 cmpwi r0, 0
2172 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002173 andis. r0, r11, SRR1_ISI_NOPT@h
2174 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002175 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
2176 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002177BEGIN_FTR_SECTION
2178 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2179 b 4f
2180END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00002181 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002182 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002183 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
2184 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000021854:
2186 /* Search the hash table. */
2187 mr r3, r9 /* vcpu pointer */
2188 mr r4, r10
2189 mr r6, r11
2190 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002191 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00002192 ld r9, HSTATE_KVM_VCPU(r13)
2193 ld r10, VCPU_PC(r9)
2194 ld r11, VCPU_MSR(r9)
2195 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2196 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002197 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002198 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002199 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00002200
Paul Mackerrascf29b212015-10-27 16:10:20 +11002201 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002202 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110022031: li r0, BOOK3S_INTERRUPT_INST_STORAGE
22047: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00002205 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002206 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002207 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002208 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002209
22103: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2211 ld r5, KVM_VRMA_SLB_V(r6)
2212 b 4b
2213
2214/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002215 * Try to handle an hcall in real mode.
2216 * Returns to the guest if we handle it, or continues on up to
2217 * the kernel if we can't (i.e. if we don't have a handler for
2218 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002219 *
2220 * r5 - r8 contain hcall args,
2221 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002222 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002223hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00002224 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002225 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08002226 /* sc 1 from userspace - reflect to guest syscall */
2227 bne sc_1_fast_return
Paul Mackerras360cae32018-10-08 16:31:04 +11002228 /* sc 1 from nested guest - give it to L1 to handle */
2229 ld r0, VCPU_NESTED(r9)
2230 cmpdi r0, 0
2231 bne guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002232 clrrdi r3,r3,2
2233 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002234 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002235 /* See if this hcall is enabled for in-kernel handling */
2236 ld r4, VCPU_KVM(r9)
2237 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2238 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2239 add r4, r4, r0
2240 ld r0, KVM_ENABLED_HCALLS(r4)
2241 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2242 srd r0, r0, r4
2243 andi. r0, r0, 1
2244 beq guest_exit_cont
2245 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002246 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10002247 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002248 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002249 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10002250 add r12,r3,r4
2251 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002252 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002253 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002254 bctrl
2255 cmpdi r3,H_TOO_HARD
2256 beq hcall_real_fallback
2257 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00002258 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002259 ld r10,VCPU_PC(r4)
2260 ld r11,VCPU_MSR(r4)
2261 b fast_guest_return
2262
Liu Ping Fan27025a62013-11-19 14:12:48 +08002263sc_1_fast_return:
2264 mtspr SPRN_SRR0,r10
2265 mtspr SPRN_SRR1,r11
2266 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11002267 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08002268 mr r4,r9
2269 b fast_guest_return
2270
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002271 /* We've attempted a real mode hcall, but it's punted it back
2272 * to userspace. We need to restore some clobbered volatiles
2273 * before resuming the pass-it-to-qemu path */
2274hcall_real_fallback:
2275 li r12,BOOK3S_INTERRUPT_SYSCALL
2276 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002277
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002278 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002279
2280 .globl hcall_real_table
2281hcall_real_table:
2282 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002283 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2284 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2285 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10002286 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2287 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002288 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002289#ifdef CONFIG_SPAPR_TCE_IOMMU
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002290 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002291 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002292#else
2293 .long 0 /* 0x1c */
2294 .long 0 /* 0x20 */
2295#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002296 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002297 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Suraj Jitindar Singheadfb1c2019-03-22 17:05:45 +11002298 .long DOTSYM(kvmppc_rm_h_page_init) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002299 .long 0 /* 0x30 */
2300 .long 0 /* 0x34 */
2301 .long 0 /* 0x38 */
2302 .long 0 /* 0x3c */
2303 .long 0 /* 0x40 */
2304 .long 0 /* 0x44 */
2305 .long 0 /* 0x48 */
2306 .long 0 /* 0x4c */
2307 .long 0 /* 0x50 */
2308 .long 0 /* 0x54 */
2309 .long 0 /* 0x58 */
2310 .long 0 /* 0x5c */
2311 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002312#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002313 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2314 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2315 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002316 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002317 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002318#else
2319 .long 0 /* 0x64 - H_EOI */
2320 .long 0 /* 0x68 - H_CPPR */
2321 .long 0 /* 0x6c - H_IPI */
2322 .long 0 /* 0x70 - H_IPOLL */
2323 .long 0 /* 0x74 - H_XIRR */
2324#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002325 .long 0 /* 0x78 */
2326 .long 0 /* 0x7c */
2327 .long 0 /* 0x80 */
2328 .long 0 /* 0x84 */
2329 .long 0 /* 0x88 */
2330 .long 0 /* 0x8c */
2331 .long 0 /* 0x90 */
2332 .long 0 /* 0x94 */
2333 .long 0 /* 0x98 */
2334 .long 0 /* 0x9c */
2335 .long 0 /* 0xa0 */
2336 .long 0 /* 0xa4 */
2337 .long 0 /* 0xa8 */
2338 .long 0 /* 0xac */
2339 .long 0 /* 0xb0 */
2340 .long 0 /* 0xb4 */
2341 .long 0 /* 0xb8 */
2342 .long 0 /* 0xbc */
2343 .long 0 /* 0xc0 */
2344 .long 0 /* 0xc4 */
2345 .long 0 /* 0xc8 */
2346 .long 0 /* 0xcc */
2347 .long 0 /* 0xd0 */
2348 .long 0 /* 0xd4 */
2349 .long 0 /* 0xd8 */
2350 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002351 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002352 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002353 .long 0 /* 0xe8 */
2354 .long 0 /* 0xec */
2355 .long 0 /* 0xf0 */
2356 .long 0 /* 0xf4 */
2357 .long 0 /* 0xf8 */
2358 .long 0 /* 0xfc */
2359 .long 0 /* 0x100 */
2360 .long 0 /* 0x104 */
2361 .long 0 /* 0x108 */
2362 .long 0 /* 0x10c */
2363 .long 0 /* 0x110 */
2364 .long 0 /* 0x114 */
2365 .long 0 /* 0x118 */
2366 .long 0 /* 0x11c */
2367 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002368 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002369 .long 0 /* 0x128 */
2370 .long 0 /* 0x12c */
2371 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002372 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002373#ifdef CONFIG_SPAPR_TCE_IOMMU
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002374 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002375 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Jordan Niethee40542a2019-02-21 14:28:48 +11002376#else
2377 .long 0 /* 0x138 */
2378 .long 0 /* 0x13c */
2379#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002380 .long 0 /* 0x140 */
2381 .long 0 /* 0x144 */
2382 .long 0 /* 0x148 */
2383 .long 0 /* 0x14c */
2384 .long 0 /* 0x150 */
2385 .long 0 /* 0x154 */
2386 .long 0 /* 0x158 */
2387 .long 0 /* 0x15c */
2388 .long 0 /* 0x160 */
2389 .long 0 /* 0x164 */
2390 .long 0 /* 0x168 */
2391 .long 0 /* 0x16c */
2392 .long 0 /* 0x170 */
2393 .long 0 /* 0x174 */
2394 .long 0 /* 0x178 */
2395 .long 0 /* 0x17c */
2396 .long 0 /* 0x180 */
2397 .long 0 /* 0x184 */
2398 .long 0 /* 0x188 */
2399 .long 0 /* 0x18c */
2400 .long 0 /* 0x190 */
2401 .long 0 /* 0x194 */
2402 .long 0 /* 0x198 */
2403 .long 0 /* 0x19c */
2404 .long 0 /* 0x1a0 */
2405 .long 0 /* 0x1a4 */
2406 .long 0 /* 0x1a8 */
2407 .long 0 /* 0x1ac */
2408 .long 0 /* 0x1b0 */
2409 .long 0 /* 0x1b4 */
2410 .long 0 /* 0x1b8 */
2411 .long 0 /* 0x1bc */
2412 .long 0 /* 0x1c0 */
2413 .long 0 /* 0x1c4 */
2414 .long 0 /* 0x1c8 */
2415 .long 0 /* 0x1cc */
2416 .long 0 /* 0x1d0 */
2417 .long 0 /* 0x1d4 */
2418 .long 0 /* 0x1d8 */
2419 .long 0 /* 0x1dc */
2420 .long 0 /* 0x1e0 */
2421 .long 0 /* 0x1e4 */
2422 .long 0 /* 0x1e8 */
2423 .long 0 /* 0x1ec */
2424 .long 0 /* 0x1f0 */
2425 .long 0 /* 0x1f4 */
2426 .long 0 /* 0x1f8 */
2427 .long 0 /* 0x1fc */
2428 .long 0 /* 0x200 */
2429 .long 0 /* 0x204 */
2430 .long 0 /* 0x208 */
2431 .long 0 /* 0x20c */
2432 .long 0 /* 0x210 */
2433 .long 0 /* 0x214 */
2434 .long 0 /* 0x218 */
2435 .long 0 /* 0x21c */
2436 .long 0 /* 0x220 */
2437 .long 0 /* 0x224 */
2438 .long 0 /* 0x228 */
2439 .long 0 /* 0x22c */
2440 .long 0 /* 0x230 */
2441 .long 0 /* 0x234 */
2442 .long 0 /* 0x238 */
2443 .long 0 /* 0x23c */
2444 .long 0 /* 0x240 */
2445 .long 0 /* 0x244 */
2446 .long 0 /* 0x248 */
2447 .long 0 /* 0x24c */
2448 .long 0 /* 0x250 */
2449 .long 0 /* 0x254 */
2450 .long 0 /* 0x258 */
2451 .long 0 /* 0x25c */
2452 .long 0 /* 0x260 */
2453 .long 0 /* 0x264 */
2454 .long 0 /* 0x268 */
2455 .long 0 /* 0x26c */
2456 .long 0 /* 0x270 */
2457 .long 0 /* 0x274 */
2458 .long 0 /* 0x278 */
2459 .long 0 /* 0x27c */
2460 .long 0 /* 0x280 */
2461 .long 0 /* 0x284 */
2462 .long 0 /* 0x288 */
2463 .long 0 /* 0x28c */
2464 .long 0 /* 0x290 */
2465 .long 0 /* 0x294 */
2466 .long 0 /* 0x298 */
2467 .long 0 /* 0x29c */
2468 .long 0 /* 0x2a0 */
2469 .long 0 /* 0x2a4 */
2470 .long 0 /* 0x2a8 */
2471 .long 0 /* 0x2ac */
2472 .long 0 /* 0x2b0 */
2473 .long 0 /* 0x2b4 */
2474 .long 0 /* 0x2b8 */
2475 .long 0 /* 0x2bc */
2476 .long 0 /* 0x2c0 */
2477 .long 0 /* 0x2c4 */
2478 .long 0 /* 0x2c8 */
2479 .long 0 /* 0x2cc */
2480 .long 0 /* 0x2d0 */
2481 .long 0 /* 0x2d4 */
2482 .long 0 /* 0x2d8 */
2483 .long 0 /* 0x2dc */
2484 .long 0 /* 0x2e0 */
2485 .long 0 /* 0x2e4 */
2486 .long 0 /* 0x2e8 */
2487 .long 0 /* 0x2ec */
2488 .long 0 /* 0x2f0 */
2489 .long 0 /* 0x2f4 */
2490 .long 0 /* 0x2f8 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002491#ifdef CONFIG_KVM_XICS
2492 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2493#else
2494 .long 0 /* 0x2fc - H_XIRR_X*/
2495#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002496 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002497 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002498hcall_real_table_end:
2499
Paul Mackerras8563bf52014-01-08 21:25:29 +11002500_GLOBAL(kvmppc_h_set_xdabr)
Paul Mackerras4bad7772018-10-08 16:31:06 +11002501EXPORT_SYMBOL_GPL(kvmppc_h_set_xdabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002502 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2503 beq 6f
2504 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2505 andc. r0, r5, r0
2506 beq 3f
25076: li r3, H_PARAMETER
2508 blr
2509
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002510_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras4bad7772018-10-08 16:31:06 +11002511EXPORT_SYMBOL_GPL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002512 li r5, DABRX_USER | DABRX_KERNEL
25133:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002514BEGIN_FTR_SECTION
2515 b 2f
2516END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002517 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002518 stw r5, VCPU_DABRX(r3)
2519 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002520 /* Work around P7 bug where DABR can get corrupted on mtspr */
25211: mtspr SPRN_DABR,r4
2522 mfspr r5, SPRN_DABR
2523 cmpd r4, r5
2524 bne 1b
2525 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002526 li r3,0
2527 blr
2528
Michael Neulinge8ebedb2018-03-27 15:37:21 +110025292:
2530BEGIN_FTR_SECTION
2531 /* POWER9 with disabled DAWR */
Aneesh Kumar K.Vca9a16c2018-03-30 17:27:24 +05302532 li r3, H_HARDWARE
Michael Neulinge8ebedb2018-03-27 15:37:21 +11002533 blr
2534END_FTR_SECTION_IFCLR(CPU_FTR_DAWR)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002535 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
Michael Neulinge8ebedb2018-03-27 15:37:21 +11002536 rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002537 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002538 clrrdi r4, r4, 3
2539 std r4, VCPU_DAWR(r3)
2540 std r5, VCPU_DAWRX(r3)
2541 mtspr SPRN_DAWR, r4
2542 mtspr SPRN_DAWRX, r5
2543 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002544 blr
2545
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002546_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002547 ori r11,r11,MSR_EE
2548 std r11,VCPU_MSR(r3)
2549 li r0,1
2550 stb r0,VCPU_CEDED(r3)
2551 sync /* order setting ceded vs. testing prodded */
2552 lbz r5,VCPU_PRODDED(r3)
2553 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002554 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002555 li r12,0 /* set trap to 0 to say hcall is handled */
2556 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002557 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002558 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002559
2560 /*
2561 * Set our bit in the bitmask of napping threads unless all the
2562 * other threads are already napping, in which case we send this
2563 * up to the host.
2564 */
2565 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002566 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002567 lwz r8,VCORE_ENTRY_EXIT(r5)
2568 clrldi r8,r8,56
2569 li r0,1
2570 sld r0,r0,r6
2571 addi r6,r5,VCORE_NAPPING_THREADS
257231: lwarx r4,0,r6
2573 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002574 cmpw r4,r8
2575 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002576 stwcx. r4,0,r6
2577 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002578 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002579 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002580 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002581 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002582 lwz r7,VCORE_ENTRY_EXIT(r5)
2583 cmpwi r7,0x100
2584 bge 33f /* another thread already exiting */
2585
2586/*
2587 * Although not specifically required by the architecture, POWER7
2588 * preserves the following registers in nap mode, even if an SMT mode
2589 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2590 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2591 */
2592 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002593 std r14, VCPU_GPR(R14)(r3)
2594 std r15, VCPU_GPR(R15)(r3)
2595 std r16, VCPU_GPR(R16)(r3)
2596 std r17, VCPU_GPR(R17)(r3)
2597 std r18, VCPU_GPR(R18)(r3)
2598 std r19, VCPU_GPR(R19)(r3)
2599 std r20, VCPU_GPR(R20)(r3)
2600 std r21, VCPU_GPR(R21)(r3)
2601 std r22, VCPU_GPR(R22)(r3)
2602 std r23, VCPU_GPR(R23)(r3)
2603 std r24, VCPU_GPR(R24)(r3)
2604 std r25, VCPU_GPR(R25)(r3)
2605 std r26, VCPU_GPR(R26)(r3)
2606 std r27, VCPU_GPR(R27)(r3)
2607 std r28, VCPU_GPR(R28)(r3)
2608 std r29, VCPU_GPR(R29)(r3)
2609 std r30, VCPU_GPR(R30)(r3)
2610 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002611
2612 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002613 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002614
Paul Mackerras93d17392016-06-22 15:52:55 +10002615#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002616/*
2617 * Branch around the call if both CPU_FTR_TM and
2618 * CPU_FTR_P9_TM_HV_ASSIST are off.
2619 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002620BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002621 b 91f
2622END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002623 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11002624 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002625 */
Simon Guo6f597c62018-05-23 15:01:48 +08002626 ld r3, HSTATE_KVM_VCPU(r13)
2627 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11002628 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10002629 bl kvmppc_save_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11002630 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100263191:
Paul Mackerras93d17392016-06-22 15:52:55 +10002632#endif
2633
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002634 /*
2635 * Set DEC to the smaller of DEC and HDEC, so that we wake
2636 * no later than the end of our timeslice (HDEC interrupts
2637 * don't wake us from nap).
2638 */
2639 mfspr r3, SPRN_DEC
2640 mfspr r4, SPRN_HDEC
2641 mftb r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10002642BEGIN_FTR_SECTION
2643 /* On P9 check whether the guest has large decrementer mode enabled */
2644 ld r6, HSTATE_KVM_VCORE(r13)
2645 ld r6, VCORE_LPCR(r6)
2646 andis. r6, r6, LPCR_LD@h
2647 bne 68f
2648END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras2f272462017-05-22 16:25:14 +10002649 extsw r3, r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000265068: EXTEND_HDEC(r4)
Paul Mackerras2f272462017-05-22 16:25:14 +10002651 cmpd r3, r4
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002652 ble 67f
2653 mtspr SPRN_DEC, r4
265467:
2655 /* save expiry time of guest decrementer */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002656 add r3, r3, r5
2657 ld r4, HSTATE_KVM_VCPU(r13)
2658 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10002659 ld r6, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002660 subf r3, r6, r3 /* convert to host TB value */
2661 std r3, VCPU_DEC_EXPIRES(r4)
2662
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002663#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2664 ld r4, HSTATE_KVM_VCPU(r13)
2665 addi r3, r4, VCPU_TB_CEDE
2666 bl kvmhv_accumulate_time
2667#endif
2668
Paul Mackerrasccc07772015-03-28 14:21:07 +11002669 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2670
Paul Mackerras19ccb762011-07-23 17:42:46 +10002671 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002672 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002673 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002674 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002675 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002676 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002677kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002678 mfspr r0, SPRN_CTRLF
2679 clrrdi r0, r0, 1
2680 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302681
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002682 li r0,1
2683 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002684 mfspr r5,SPRN_LPCR
2685 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002686BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002687 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002688 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002689END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002690
2691kvm_nap_sequence: /* desired LPCR value in r5 */
2692BEGIN_FTR_SECTION
2693 /*
2694 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2695 * enable state loss = 1 (allow SMT mode switch)
2696 * requested level = 0 (just stop dispatching)
2697 */
2698 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2699 mtspr SPRN_PSSCR, r3
2700 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2701 li r4, LPCR_PECE_HVEE@higher
2702 sldi r4, r4, 32
2703 or r5, r5, r4
2704END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002705 mtspr SPRN_LPCR,r5
2706 isync
2707 li r0, 0
2708 std r0, HSTATE_SCRATCH0(r13)
2709 ptesync
2710 ld r0, HSTATE_SCRATCH0(r13)
27111: cmpd r0, r0
2712 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002713BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002714 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002715FTR_SECTION_ELSE
2716 PPC_STOP
2717ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002718 b .
2719
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100272033: mr r4, r3
2721 li r3, 0
2722 li r12, 0
2723 b 34f
2724
Paul Mackerras19ccb762011-07-23 17:42:46 +10002725kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002726 /* get vcpu pointer */
2727 ld r4, HSTATE_KVM_VCPU(r13)
2728
Paul Mackerras19ccb762011-07-23 17:42:46 +10002729 /* Woken by external or decrementer interrupt */
2730 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002731
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002732#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2733 addi r3, r4, VCPU_TB_RMINTR
2734 bl kvmhv_accumulate_time
2735#endif
2736
Paul Mackerras93d17392016-06-22 15:52:55 +10002737#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002738/*
2739 * Branch around the call if both CPU_FTR_TM and
2740 * CPU_FTR_P9_TM_HV_ASSIST are off.
2741 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002742BEGIN_FTR_SECTION
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002743 b 91f
2744END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002745 /*
Paul Mackerras7854f752018-10-08 16:30:53 +11002746 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS (but not CR)
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002747 */
Simon Guo6f597c62018-05-23 15:01:48 +08002748 mr r3, r4
2749 ld r4, VCPU_MSR(r3)
Paul Mackerras7854f752018-10-08 16:30:53 +11002750 li r5, 0 /* don't preserve non-vol regs */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10002751 bl kvmppc_restore_tm_hv
Paul Mackerras7854f752018-10-08 16:30:53 +11002752 nop
Simon Guo6f597c62018-05-23 15:01:48 +08002753 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100275491:
Paul Mackerras93d17392016-06-22 15:52:55 +10002755#endif
2756
Paul Mackerras19ccb762011-07-23 17:42:46 +10002757 /* load up FP state */
2758 bl kvmppc_load_fp
2759
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002760 /* Restore guest decrementer */
2761 ld r3, VCPU_DEC_EXPIRES(r4)
2762 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10002763 ld r6, VCORE_TB_OFFSET_APPL(r5)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002764 add r3, r3, r6 /* convert host TB to guest TB value */
2765 mftb r7
2766 subf r3, r7, r3
2767 mtspr SPRN_DEC, r3
2768
Paul Mackerras19ccb762011-07-23 17:42:46 +10002769 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002770 ld r14, VCPU_GPR(R14)(r4)
2771 ld r15, VCPU_GPR(R15)(r4)
2772 ld r16, VCPU_GPR(R16)(r4)
2773 ld r17, VCPU_GPR(R17)(r4)
2774 ld r18, VCPU_GPR(R18)(r4)
2775 ld r19, VCPU_GPR(R19)(r4)
2776 ld r20, VCPU_GPR(R20)(r4)
2777 ld r21, VCPU_GPR(R21)(r4)
2778 ld r22, VCPU_GPR(R22)(r4)
2779 ld r23, VCPU_GPR(R23)(r4)
2780 ld r24, VCPU_GPR(R24)(r4)
2781 ld r25, VCPU_GPR(R25)(r4)
2782 ld r26, VCPU_GPR(R26)(r4)
2783 ld r27, VCPU_GPR(R27)(r4)
2784 ld r28, VCPU_GPR(R28)(r4)
2785 ld r29, VCPU_GPR(R29)(r4)
2786 ld r30, VCPU_GPR(R30)(r4)
2787 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002788
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002789 /* Check the wake reason in SRR1 to see why we got here */
2790 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002791
Suresh Warrier37f55d32016-08-19 15:35:46 +10002792 /*
2793 * Restore volatile registers since we could have called a
2794 * C routine in kvmppc_check_wake_reason
2795 * r4 = VCPU
2796 * r3 tells us whether we need to return to host or not
2797 * WARNING: it gets checked further down:
2798 * should not modify r3 until this check is done.
2799 */
2800 ld r4, HSTATE_KVM_VCPU(r13)
2801
Paul Mackerras19ccb762011-07-23 17:42:46 +10002802 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100280334: ld r5,HSTATE_KVM_VCORE(r13)
2804 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002805 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002806 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002807 addi r6,r5,VCORE_NAPPING_THREADS
280832: lwarx r7,0,r6
2809 andc r7,r7,r0
2810 stwcx. r7,0,r6
2811 bne 32b
2812 li r0,0
2813 stb r0,HSTATE_NAPPING(r13)
2814
Suresh Warrier37f55d32016-08-19 15:35:46 +10002815 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002816 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002817 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002818 cmpdi r3, 0
2819 bgt guest_exit_cont
Paul Mackerrasdf709a22018-10-08 16:30:52 +11002820 b maybe_reenter_guest
Paul Mackerras19ccb762011-07-23 17:42:46 +10002821
2822 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002823kvm_cede_prodded:
2824 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002825 stb r0,VCPU_PRODDED(r3)
2826 sync /* order testing prodded vs. clearing ceded */
2827 stb r0,VCPU_CEDED(r3)
2828 li r3,H_SUCCESS
2829 blr
2830
2831 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002832kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002833 ld r9, HSTATE_KVM_VCPU(r13)
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +11002834#ifdef CONFIG_KVM_XICS
2835 /* Abort if we still have a pending escalation */
2836 lbz r5, VCPU_XIVE_ESC_ON(r9)
2837 cmpwi r5, 0
2838 beq 1f
2839 li r0, 0
2840 stb r0, VCPU_CEDED(r9)
28411: /* Enable XIVE escalation */
2842 li r5, XIVE_ESB_SET_PQ_00
2843 mfmsr r0
2844 andi. r0, r0, MSR_DR /* in real mode? */
2845 beq 1f
2846 ld r10, VCPU_XIVE_ESC_VADDR(r9)
2847 cmpdi r10, 0
2848 beq 3f
2849 ldx r0, r10, r5
2850 b 2f
28511: ld r10, VCPU_XIVE_ESC_RADDR(r9)
2852 cmpdi r10, 0
2853 beq 3f
2854 ldcix r0, r10, r5
28552: sync
2856 li r0, 1
2857 stb r0, VCPU_XIVE_ESC_ON(r9)
2858#endif /* CONFIG_KVM_XICS */
28593: b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002860
Paul Mackerras884dfb72019-02-21 13:38:49 +11002861 /* Try to do machine check recovery in real mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002862machine_check_realmode:
2863 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002864 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002865 nop
Paul Mackerras884dfb72019-02-21 13:38:49 +11002866 /* all machine checks go to virtual mode for further handling */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002867 ld r9, HSTATE_KVM_VCPU(r13)
2868 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Paul Mackerras884dfb72019-02-21 13:38:49 +11002869 b guest_exit_cont
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002870
Paul Mackerrasde56a942011-06-29 00:21:34 +00002871/*
Paul Mackerrasdf709a22018-10-08 16:30:52 +11002872 * Call C code to handle a HMI in real mode.
2873 * Only the primary thread does the call, secondary threads are handled
2874 * by calling hmi_exception_realmode() after kvmppc_hv_entry returns.
2875 * r9 points to the vcpu on entry
2876 */
2877hmi_realmode:
2878 lbz r0, HSTATE_PTID(r13)
2879 cmpwi r0, 0
2880 bne guest_exit_cont
2881 bl kvmppc_realmode_hmi_handler
2882 ld r9, HSTATE_KVM_VCPU(r13)
2883 li r12, BOOK3S_INTERRUPT_HMI
2884 b guest_exit_cont
2885
2886/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002887 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002888 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002889 * 0 if nothing needs to be done
2890 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002891 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002892 * -2 if we handled a PCI passthrough interrupt (returned by
2893 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002894 *
2895 * Also sets r12 to the interrupt vector for any interrupt that needs
2896 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002897 * Modifies all volatile registers (since it may call a C function).
2898 * This routine calls kvmppc_read_intr, a C function, if an external
2899 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002900 */
2901kvmppc_check_wake_reason:
2902 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002903BEGIN_FTR_SECTION
2904 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2905FTR_SECTION_ELSE
2906 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2907ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2908 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002909 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002910 li r3, 0
2911 li r12, 0
2912 cmpwi r6, 6 /* was it the decrementer? */
2913 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002914BEGIN_FTR_SECTION
2915 cmpwi r6, 5 /* privileged doorbell? */
2916 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002917 cmpwi r6, 3 /* hypervisor doorbell? */
2918 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002919END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302920 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2921 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002922 li r3, 1 /* anything else, return 1 */
29230: blr
2924
Paul Mackerras5d00f662014-01-08 21:25:28 +11002925 /* hypervisor doorbell */
29263: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302927
2928 /*
2929 * Clear the doorbell as we will invoke the handler
2930 * explicitly in the guest exit path.
2931 */
2932 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2933 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002934 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002935 li r3, 1
Nicholas Piggin2cde3712017-10-10 20:18:28 +10002936BEGIN_FTR_SECTION
2937 PPC_MSGSYNC
2938 lwsync
2939END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11002940 lbz r0, HSTATE_HOST_IPI(r13)
2941 cmpwi r0, 0
2942 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302943 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002944 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002945 blr
2946
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302947 /* Woken up due to Hypervisor maintenance interrupt */
29484: li r12, BOOK3S_INTERRUPT_HMI
2949 li r3, 1
2950 blr
2951
Suresh Warrier37f55d32016-08-19 15:35:46 +10002952 /* external interrupt - create a stack frame so we can call C */
29537: mflr r0
2954 std r0, PPC_LR_STKOFF(r1)
2955 stdu r1, -PPC_MIN_STKFRM(r1)
2956 bl kvmppc_read_intr
2957 nop
2958 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002959 cmpdi r3, 1
2960 ble 1f
2961
2962 /*
2963 * Return code of 2 means PCI passthrough interrupt, but
2964 * we need to return back to host to complete handling the
2965 * interrupt. Trap reason is expected in r12 by guest
2966 * exit code.
2967 */
2968 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
29691:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002970 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2971 addi r1, r1, PPC_MIN_STKFRM
2972 mtlr r0
2973 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002974
2975/*
2976 * Save away FP, VMX and VSX registers.
2977 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002978 * N.B. r30 and r31 are volatile across this function,
2979 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002980 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002981kvmppc_save_fp:
2982 mflr r30
2983 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002984 mfmsr r5
2985 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002986#ifdef CONFIG_ALTIVEC
2987BEGIN_FTR_SECTION
2988 oris r8,r8,MSR_VEC@h
2989END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2990#endif
2991#ifdef CONFIG_VSX
2992BEGIN_FTR_SECTION
2993 oris r8,r8,MSR_VSX@h
2994END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2995#endif
2996 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002997 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002998 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002999#ifdef CONFIG_ALTIVEC
3000BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11003001 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003002 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003003END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3004#endif
3005 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11003006 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11003007 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00003008 blr
3009
3010/*
3011 * Load up FP, VMX and VSX registers
3012 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11003013 * N.B. r30 and r31 are volatile across this function,
3014 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00003015 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00003016kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11003017 mflr r30
3018 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00003019 mfmsr r9
3020 ori r8,r9,MSR_FP
3021#ifdef CONFIG_ALTIVEC
3022BEGIN_FTR_SECTION
3023 oris r8,r8,MSR_VEC@h
3024END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3025#endif
3026#ifdef CONFIG_VSX
3027BEGIN_FTR_SECTION
3028 oris r8,r8,MSR_VSX@h
3029END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3030#endif
3031 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11003032 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003033 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003034#ifdef CONFIG_ALTIVEC
3035BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11003036 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02003037 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00003038END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3039#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11003040 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003041 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11003042 mtlr r30
3043 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00003044 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10003045
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003046#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
3047/*
3048 * Save transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003049 * Called with r3 pointing to the vcpu struct and r4 containing
3050 * the guest MSR value.
Paul Mackerras7854f752018-10-08 16:30:53 +11003051 * r5 is non-zero iff non-volatile register state needs to be maintained.
3052 * If r5 == 0, this can modify all checkpointed registers, but
Simon Guo6f597c62018-05-23 15:01:48 +08003053 * restores r1 and r2 before exit.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003054 */
Paul Mackerras7854f752018-10-08 16:30:53 +11003055_GLOBAL_TOC(kvmppc_save_tm_hv)
3056EXPORT_SYMBOL_GPL(kvmppc_save_tm_hv)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003057 /* See if we need to handle fake suspend mode */
3058BEGIN_FTR_SECTION
Simon Guocaa3be92018-05-23 15:01:50 +08003059 b __kvmppc_save_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003060END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
3061
3062 lbz r0, HSTATE_FAKE_SUSPEND(r13) /* Were we fake suspended? */
3063 cmpwi r0, 0
Simon Guocaa3be92018-05-23 15:01:50 +08003064 beq __kvmppc_save_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003065
3066 /* The following code handles the fake_suspend = 1 case */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003067 mflr r0
3068 std r0, PPC_LR_STKOFF(r1)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003069 stdu r1, -PPC_MIN_STKFRM(r1)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003070
3071 /* Turn on TM. */
3072 mfmsr r8
3073 li r0, 1
3074 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
3075 mtmsrd r8
3076
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003077 rldicl. r8, r8, 64 - MSR_TS_S_LG, 62 /* Did we actually hrfid? */
3078 beq 4f
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003079BEGIN_FTR_SECTION
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003080 bl pnv_power9_force_smt4_catch
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003081END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003082 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003083
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003084 /* We have to treclaim here because that's the only way to do S->N */
3085 li r3, TM_CAUSE_KVM_RESCHED
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003086 TRECLAIM(R3)
3087
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003088 /*
3089 * We were in fake suspend, so we are not going to save the
3090 * register state as the guest checkpointed state (since
3091 * we already have it), therefore we can now use any volatile GPR.
Paul Mackerras7854f752018-10-08 16:30:53 +11003092 * In fact treclaim in fake suspend state doesn't modify
3093 * any registers.
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003094 */
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003095
Paul Mackerras7854f752018-10-08 16:30:53 +11003096BEGIN_FTR_SECTION
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003097 bl pnv_power9_force_smt4_release
Paul Mackerras7854f752018-10-08 16:30:53 +11003098END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003099 nop
3100
31014:
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003102 mfspr r3, SPRN_PSSCR
3103 /* PSSCR_FAKE_SUSPEND is a write-only bit, but clear it anyway */
3104 li r0, PSSCR_FAKE_SUSPEND
3105 andc r3, r3, r0
3106 mtspr SPRN_PSSCR, r3
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003107
Paul Mackerras681c6172018-03-21 21:32:03 +11003108 /* Don't save TEXASR, use value from last exit in real suspend state */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003109 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003110 mfspr r5, SPRN_TFHAR
3111 mfspr r6, SPRN_TFIAR
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003112 std r5, VCPU_TFHAR(r9)
3113 std r6, VCPU_TFIAR(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003114
Suraj Jitindar Singh87a11bb2018-03-21 21:32:02 +11003115 addi r1, r1, PPC_MIN_STKFRM
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003116 ld r0, PPC_LR_STKOFF(r1)
3117 mtlr r0
3118 blr
3119
3120/*
3121 * Restore transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003122 * Called with r3 pointing to the vcpu struct
3123 * and r4 containing the guest MSR value.
Paul Mackerras7854f752018-10-08 16:30:53 +11003124 * r5 is non-zero iff non-volatile register state needs to be maintained.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003125 * This potentially modifies all checkpointed registers.
Simon Guo6f597c62018-05-23 15:01:48 +08003126 * It restores r1 and r2 from the PACA.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003127 */
Paul Mackerras7854f752018-10-08 16:30:53 +11003128_GLOBAL_TOC(kvmppc_restore_tm_hv)
3129EXPORT_SYMBOL_GPL(kvmppc_restore_tm_hv)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003130 /*
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003131 * If we are doing TM emulation for the guest on a POWER9 DD2,
3132 * then we don't actually do a trechkpt -- we either set up
3133 * fake-suspend mode, or emulate a TM rollback.
3134 */
3135BEGIN_FTR_SECTION
Simon Guocaa3be92018-05-23 15:01:50 +08003136 b __kvmppc_restore_tm
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003137END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
3138 mflr r0
3139 std r0, PPC_LR_STKOFF(r1)
3140
3141 li r0, 0
3142 stb r0, HSTATE_FAKE_SUSPEND(r13)
3143
3144 /* Turn on TM so we can restore TM SPRs */
3145 mfmsr r5
3146 li r0, 1
3147 rldimi r5, r0, MSR_TM_LG, 63-MSR_TM_LG
3148 mtmsrd r5
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003149
3150 /*
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003151 * The user may change these outside of a transaction, so they must
3152 * always be context switched.
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003153 */
Simon Guo6f597c62018-05-23 15:01:48 +08003154 ld r5, VCPU_TFHAR(r3)
3155 ld r6, VCPU_TFIAR(r3)
3156 ld r7, VCPU_TEXASR(r3)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003157 mtspr SPRN_TFHAR, r5
3158 mtspr SPRN_TFIAR, r6
3159 mtspr SPRN_TEXASR, r7
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003160
Simon Guo6f597c62018-05-23 15:01:48 +08003161 rldicl. r5, r4, 64 - MSR_TS_S_LG, 62
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003162 beqlr /* TM not active in guest */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003163
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003164 /* Make sure the failure summary is set */
3165 oris r7, r7, (TEXASR_FS)@h
3166 mtspr SPRN_TEXASR, r7
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003167
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003168 cmpwi r5, 1 /* check for suspended state */
3169 bgt 10f
3170 stb r5, HSTATE_FAKE_SUSPEND(r13)
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003171 b 9f /* and return */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +1100317210: stdu r1, -PPC_MIN_STKFRM(r1)
3173 /* guest is in transactional state, so simulate rollback */
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003174 bl kvmhv_emulate_tm_rollback
3175 nop
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003176 addi r1, r1, PPC_MIN_STKFRM
Paul Mackerras7b0e8272018-05-30 20:07:52 +100031779: ld r0, PPC_LR_STKOFF(r1)
3178 mtlr r0
3179 blr
Paul Mackerras7b0e8272018-05-30 20:07:52 +10003180#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003181
Paul Mackerras44a3add2013-10-04 21:45:04 +10003182/*
3183 * We come here if we get any exception or interrupt while we are
3184 * executing host real mode code while in guest MMU context.
Paul Mackerras857b99e2017-09-01 16:17:27 +10003185 * r12 is (CR << 32) | vector
3186 * r13 points to our PACA
3187 * r12 is saved in HSTATE_SCRATCH0(r13)
3188 * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
3189 * r9 is saved in HSTATE_SCRATCH2(r13)
3190 * r13 is saved in HSPRG1
3191 * cfar is saved in HSTATE_CFAR(r13)
3192 * ppr is saved in HSTATE_PPR(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10003193 */
3194kvmppc_bad_host_intr:
Paul Mackerras857b99e2017-09-01 16:17:27 +10003195 /*
3196 * Switch to the emergency stack, but start half-way down in
3197 * case we were already on it.
3198 */
3199 mr r9, r1
3200 std r1, PACAR1(r13)
3201 ld r1, PACAEMERGSP(r13)
3202 subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
3203 std r9, 0(r1)
3204 std r0, GPR0(r1)
3205 std r9, GPR1(r1)
3206 std r2, GPR2(r1)
3207 SAVE_4GPRS(3, r1)
3208 SAVE_2GPRS(7, r1)
3209 srdi r0, r12, 32
3210 clrldi r12, r12, 32
3211 std r0, _CCR(r1)
3212 std r12, _TRAP(r1)
3213 andi. r0, r12, 2
3214 beq 1f
3215 mfspr r3, SPRN_HSRR0
3216 mfspr r4, SPRN_HSRR1
3217 mfspr r5, SPRN_HDAR
3218 mfspr r6, SPRN_HDSISR
3219 b 2f
32201: mfspr r3, SPRN_SRR0
3221 mfspr r4, SPRN_SRR1
3222 mfspr r5, SPRN_DAR
3223 mfspr r6, SPRN_DSISR
32242: std r3, _NIP(r1)
3225 std r4, _MSR(r1)
3226 std r5, _DAR(r1)
3227 std r6, _DSISR(r1)
3228 ld r9, HSTATE_SCRATCH2(r13)
3229 ld r12, HSTATE_SCRATCH0(r13)
3230 GET_SCRATCH0(r0)
3231 SAVE_4GPRS(9, r1)
3232 std r0, GPR13(r1)
3233 SAVE_NVGPRS(r1)
3234 ld r5, HSTATE_CFAR(r13)
3235 std r5, ORIG_GPR3(r1)
3236 mflr r3
3237#ifdef CONFIG_RELOCATABLE
3238 ld r4, HSTATE_SCRATCH1(r13)
3239#else
3240 mfctr r4
3241#endif
3242 mfxer r5
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +05303243 lbz r6, PACAIRQSOFTMASK(r13)
Paul Mackerras857b99e2017-09-01 16:17:27 +10003244 std r3, _LINK(r1)
3245 std r4, _CTR(r1)
3246 std r5, _XER(r1)
3247 std r6, SOFTE(r1)
3248 ld r2, PACATOC(r13)
3249 LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
3250 std r3, STACK_FRAME_OVERHEAD-16(r1)
3251
3252 /*
3253 * On POWER9 do a minimal restore of the MMU and call C code,
3254 * which will print a message and panic.
3255 * XXX On POWER7 and POWER8, we just spin here since we don't
3256 * know what the other threads are doing (and we don't want to
3257 * coordinate with them) - but at least we now have register state
3258 * in memory that we might be able to look at from another CPU.
3259 */
3260BEGIN_FTR_SECTION
Paul Mackerras44a3add2013-10-04 21:45:04 +10003261 b .
Paul Mackerras857b99e2017-09-01 16:17:27 +10003262END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
3263 ld r9, HSTATE_KVM_VCPU(r13)
3264 ld r10, VCPU_KVM(r9)
3265
3266 li r0, 0
3267 mtspr SPRN_AMR, r0
3268 mtspr SPRN_IAMR, r0
3269 mtspr SPRN_CIABR, r0
3270 mtspr SPRN_DAWRX, r0
3271
Paul Mackerras857b99e2017-09-01 16:17:27 +10003272BEGIN_MMU_FTR_SECTION
3273 b 4f
3274END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
3275
3276 slbmte r0, r0
3277 slbia
3278 ptesync
3279 ld r8, PACA_SLBSHADOWPTR(r13)
3280 .rept SLB_NUM_BOLTED
3281 li r3, SLBSHADOW_SAVEAREA
3282 LDX_BE r5, r8, r3
3283 addi r3, r3, 8
3284 LDX_BE r6, r8, r3
3285 andis. r7, r5, SLB_ESID_V@h
3286 beq 3f
3287 slbmte r6, r5
32883: addi r8, r8, 16
3289 .endr
3290
32914: lwz r7, KVM_HOST_LPID(r10)
3292 mtspr SPRN_LPID, r7
3293 mtspr SPRN_PID, r0
3294 ld r8, KVM_HOST_LPCR(r10)
3295 mtspr SPRN_LPCR, r8
3296 isync
3297 li r0, KVM_GUEST_MODE_NONE
3298 stb r0, HSTATE_IN_GUEST(r13)
3299
3300 /*
3301 * Turn on the MMU and jump to C code
3302 */
3303 bcl 20, 31, .+4
33045: mflr r3
3305 addi r3, r3, 9f - 5b
Nicholas Piggineadce3b2018-05-18 03:49:43 +10003306 li r4, -1
3307 rldimi r3, r4, 62, 0 /* ensure 0xc000000000000000 bits are set */
Paul Mackerras857b99e2017-09-01 16:17:27 +10003308 ld r4, PACAKMSR(r13)
3309 mtspr SPRN_SRR0, r3
3310 mtspr SPRN_SRR1, r4
Nicholas Piggin222f20f2018-01-10 03:07:15 +11003311 RFI_TO_KERNEL
Paul Mackerras857b99e2017-09-01 16:17:27 +100033129: addi r3, r1, STACK_FRAME_OVERHEAD
3313 bl kvmppc_bad_interrupt
3314 b 9b
Michael Neulinge4e38122014-03-25 10:47:02 +11003315
3316/*
3317 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3318 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3319 * r11 has the guest MSR value (in/out)
3320 * r9 has a vcpu pointer (in)
3321 * r0 is used as a scratch register
3322 */
3323kvmppc_msr_interrupt:
3324 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3325 cmpwi r0, 2 /* Check if we are in transactional state.. */
3326 ld r11, VCPU_INTR_MSR(r9)
3327 bne 1f
3328 /* ... if transactional, change to suspended */
3329 li r0, 1
33301: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3331 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003332
3333/*
Paul Mackerras41f4e632018-10-08 16:30:51 +11003334 * Load up guest PMU state. R3 points to the vcpu struct.
3335 */
3336_GLOBAL(kvmhv_load_guest_pmu)
3337EXPORT_SYMBOL_GPL(kvmhv_load_guest_pmu)
3338 mr r4, r3
3339 mflr r0
3340 li r3, 1
3341 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
3342 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
3343 isync
3344BEGIN_FTR_SECTION
3345 ld r3, VCPU_MMCR(r4)
3346 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
3347 cmpwi r5, MMCR0_PMAO
3348 beql kvmppc_fix_pmao
3349END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
3350 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
3351 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
3352 lwz r6, VCPU_PMC + 8(r4)
3353 lwz r7, VCPU_PMC + 12(r4)
3354 lwz r8, VCPU_PMC + 16(r4)
3355 lwz r9, VCPU_PMC + 20(r4)
3356 mtspr SPRN_PMC1, r3
3357 mtspr SPRN_PMC2, r5
3358 mtspr SPRN_PMC3, r6
3359 mtspr SPRN_PMC4, r7
3360 mtspr SPRN_PMC5, r8
3361 mtspr SPRN_PMC6, r9
3362 ld r3, VCPU_MMCR(r4)
3363 ld r5, VCPU_MMCR + 8(r4)
3364 ld r6, VCPU_MMCR + 16(r4)
3365 ld r7, VCPU_SIAR(r4)
3366 ld r8, VCPU_SDAR(r4)
3367 mtspr SPRN_MMCR1, r5
3368 mtspr SPRN_MMCRA, r6
3369 mtspr SPRN_SIAR, r7
3370 mtspr SPRN_SDAR, r8
3371BEGIN_FTR_SECTION
3372 ld r5, VCPU_MMCR + 24(r4)
3373 ld r6, VCPU_SIER(r4)
3374 mtspr SPRN_MMCR2, r5
3375 mtspr SPRN_SIER, r6
3376BEGIN_FTR_SECTION_NESTED(96)
3377 lwz r7, VCPU_PMC + 24(r4)
3378 lwz r8, VCPU_PMC + 28(r4)
3379 ld r9, VCPU_MMCR + 32(r4)
3380 mtspr SPRN_SPMC1, r7
3381 mtspr SPRN_SPMC2, r8
3382 mtspr SPRN_MMCRS, r9
3383END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
3384END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3385 mtspr SPRN_MMCR0, r3
3386 isync
3387 mtlr r0
3388 blr
3389
3390/*
3391 * Reload host PMU state saved in the PACA by kvmhv_save_host_pmu.
3392 */
3393_GLOBAL(kvmhv_load_host_pmu)
3394EXPORT_SYMBOL_GPL(kvmhv_load_host_pmu)
3395 mflr r0
3396 lbz r4, PACA_PMCINUSE(r13) /* is the host using the PMU? */
3397 cmpwi r4, 0
3398 beq 23f /* skip if not */
3399BEGIN_FTR_SECTION
3400 ld r3, HSTATE_MMCR0(r13)
3401 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
3402 cmpwi r4, MMCR0_PMAO
3403 beql kvmppc_fix_pmao
3404END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
3405 lwz r3, HSTATE_PMC1(r13)
3406 lwz r4, HSTATE_PMC2(r13)
3407 lwz r5, HSTATE_PMC3(r13)
3408 lwz r6, HSTATE_PMC4(r13)
3409 lwz r8, HSTATE_PMC5(r13)
3410 lwz r9, HSTATE_PMC6(r13)
3411 mtspr SPRN_PMC1, r3
3412 mtspr SPRN_PMC2, r4
3413 mtspr SPRN_PMC3, r5
3414 mtspr SPRN_PMC4, r6
3415 mtspr SPRN_PMC5, r8
3416 mtspr SPRN_PMC6, r9
3417 ld r3, HSTATE_MMCR0(r13)
3418 ld r4, HSTATE_MMCR1(r13)
3419 ld r5, HSTATE_MMCRA(r13)
3420 ld r6, HSTATE_SIAR(r13)
3421 ld r7, HSTATE_SDAR(r13)
3422 mtspr SPRN_MMCR1, r4
3423 mtspr SPRN_MMCRA, r5
3424 mtspr SPRN_SIAR, r6
3425 mtspr SPRN_SDAR, r7
3426BEGIN_FTR_SECTION
3427 ld r8, HSTATE_MMCR2(r13)
3428 ld r9, HSTATE_SIER(r13)
3429 mtspr SPRN_MMCR2, r8
3430 mtspr SPRN_SIER, r9
3431END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3432 mtspr SPRN_MMCR0, r3
3433 isync
3434 mtlr r0
343523: blr
3436
3437/*
3438 * Save guest PMU state into the vcpu struct.
3439 * r3 = vcpu, r4 = full save flag (PMU in use flag set in VPA)
3440 */
3441_GLOBAL(kvmhv_save_guest_pmu)
3442EXPORT_SYMBOL_GPL(kvmhv_save_guest_pmu)
3443 mr r9, r3
3444 mr r8, r4
3445BEGIN_FTR_SECTION
3446 /*
3447 * POWER8 seems to have a hardware bug where setting
3448 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
3449 * when some counters are already negative doesn't seem
3450 * to cause a performance monitor alert (and hence interrupt).
3451 * The effect of this is that when saving the PMU state,
3452 * if there is no PMU alert pending when we read MMCR0
3453 * before freezing the counters, but one becomes pending
3454 * before we read the counters, we lose it.
3455 * To work around this, we need a way to freeze the counters
3456 * before reading MMCR0. Normally, freezing the counters
3457 * is done by writing MMCR0 (to set MMCR0[FC]) which
3458 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
3459 * we can also freeze the counters using MMCR2, by writing
3460 * 1s to all the counter freeze condition bits (there are
3461 * 9 bits each for 6 counters).
3462 */
3463 li r3, -1 /* set all freeze bits */
3464 clrrdi r3, r3, 10
3465 mfspr r10, SPRN_MMCR2
3466 mtspr SPRN_MMCR2, r3
3467 isync
3468END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3469 li r3, 1
3470 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
3471 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
3472 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
3473 mfspr r6, SPRN_MMCRA
3474 /* Clear MMCRA in order to disable SDAR updates */
3475 li r7, 0
3476 mtspr SPRN_MMCRA, r7
3477 isync
3478 cmpwi r8, 0 /* did they ask for PMU stuff to be saved? */
3479 bne 21f
3480 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
3481 b 22f
348221: mfspr r5, SPRN_MMCR1
3483 mfspr r7, SPRN_SIAR
3484 mfspr r8, SPRN_SDAR
3485 std r4, VCPU_MMCR(r9)
3486 std r5, VCPU_MMCR + 8(r9)
3487 std r6, VCPU_MMCR + 16(r9)
3488BEGIN_FTR_SECTION
3489 std r10, VCPU_MMCR + 24(r9)
3490END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
3491 std r7, VCPU_SIAR(r9)
3492 std r8, VCPU_SDAR(r9)
3493 mfspr r3, SPRN_PMC1
3494 mfspr r4, SPRN_PMC2
3495 mfspr r5, SPRN_PMC3
3496 mfspr r6, SPRN_PMC4
3497 mfspr r7, SPRN_PMC5
3498 mfspr r8, SPRN_PMC6
3499 stw r3, VCPU_PMC(r9)
3500 stw r4, VCPU_PMC + 4(r9)
3501 stw r5, VCPU_PMC + 8(r9)
3502 stw r6, VCPU_PMC + 12(r9)
3503 stw r7, VCPU_PMC + 16(r9)
3504 stw r8, VCPU_PMC + 20(r9)
3505BEGIN_FTR_SECTION
3506 mfspr r5, SPRN_SIER
3507 std r5, VCPU_SIER(r9)
3508BEGIN_FTR_SECTION_NESTED(96)
3509 mfspr r6, SPRN_SPMC1
3510 mfspr r7, SPRN_SPMC2
3511 mfspr r8, SPRN_MMCRS
3512 stw r6, VCPU_PMC + 24(r9)
3513 stw r7, VCPU_PMC + 28(r9)
3514 std r8, VCPU_MMCR + 32(r9)
3515 lis r4, 0x8000
3516 mtspr SPRN_MMCRS, r4
3517END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
3518END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
351922: blr
3520
3521/*
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003522 * This works around a hardware bug on POWER8E processors, where
3523 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3524 * performance monitor interrupt. Instead, when we need to have
3525 * an interrupt pending, we have to arrange for a counter to overflow.
3526 */
3527kvmppc_fix_pmao:
3528 li r3, 0
3529 mtspr SPRN_MMCR2, r3
3530 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3531 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3532 mtspr SPRN_MMCR0, r3
3533 lis r3, 0x7fff
3534 ori r3, r3, 0xffff
3535 mtspr SPRN_PMC6, r3
3536 isync
3537 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003538
3539#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3540/*
3541 * Start timing an activity
3542 * r3 = pointer to time accumulation struct, r4 = vcpu
3543 */
3544kvmhv_start_timing:
3545 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003546 ld r6, VCORE_TB_OFFSET_APPL(r5)
3547 mftb r5
3548 subf r5, r6, r5 /* subtract current timebase offset */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003549 std r3, VCPU_CUR_ACTIVITY(r4)
3550 std r5, VCPU_ACTIVITY_START(r4)
3551 blr
3552
3553/*
3554 * Accumulate time to one activity and start another.
3555 * r3 = pointer to new time accumulation struct, r4 = vcpu
3556 */
3557kvmhv_accumulate_time:
3558 ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003559 ld r8, VCORE_TB_OFFSET_APPL(r5)
3560 ld r5, VCPU_CUR_ACTIVITY(r4)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003561 ld r6, VCPU_ACTIVITY_START(r4)
3562 std r3, VCPU_CUR_ACTIVITY(r4)
3563 mftb r7
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003564 subf r7, r8, r7 /* subtract current timebase offset */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003565 std r7, VCPU_ACTIVITY_START(r4)
3566 cmpdi r5, 0
3567 beqlr
3568 subf r3, r6, r7
3569 ld r8, TAS_SEQCOUNT(r5)
3570 cmpdi r8, 0
3571 addi r8, r8, 1
3572 std r8, TAS_SEQCOUNT(r5)
3573 lwsync
3574 ld r7, TAS_TOTAL(r5)
3575 add r7, r7, r3
3576 std r7, TAS_TOTAL(r5)
3577 ld r6, TAS_MIN(r5)
3578 ld r7, TAS_MAX(r5)
3579 beq 3f
3580 cmpd r3, r6
3581 bge 1f
35823: std r3, TAS_MIN(r5)
35831: cmpd r3, r7
3584 ble 2f
3585 std r3, TAS_MAX(r5)
35862: lwsync
3587 addi r8, r8, 1
3588 std r8, TAS_SEQCOUNT(r5)
3589 blr
3590#endif