KVM: PPC: Book3S HV: radix: Do not clear partition PTE when RC or write bits do not...
[linux-2.6-block.git] / arch / powerpc / kvm / book3s_hv_rmhandlers.S
CommitLineData
de56a948
PM
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12 *
13 * Derived from book3s_rmhandlers.S and other files, which are:
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
177339d7 23#include <asm/mmu.h>
de56a948 24#include <asm/page.h>
177339d7
PM
25#include <asm/ptrace.h>
26#include <asm/hvcall.h>
de56a948
PM
27#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
f0888f70 29#include <asm/kvm_book3s_asm.h>
f64e8084 30#include <asm/book3s/64/mmu-hash.h>
e4e38121 31#include <asm/tm.h>
fd7bacbc 32#include <asm/opal.h>
5af50993 33#include <asm/xive-regs.h>
857b99e1 34#include <asm/thread_info.h>
e4e38121 35
2f272463
PM
36/* Sign-extend HDEC if not on POWER9 */
37#define EXTEND_HDEC(reg) \
38BEGIN_FTR_SECTION; \
39 extsw reg, reg; \
40END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
41
e4e38121 42#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
de56a948 43
e0b7ec05
PM
44/* Values in HSTATE_NAPPING(r13) */
45#define NAPPING_CEDE 1
46#define NAPPING_NOVCPU 2
47
7ceaa6dc 48/* Stack frame offsets for kvmppc_hv_entry */
769377f7 49#define SFS 160
7ceaa6dc
PM
50#define STACK_SLOT_TRAP (SFS-4)
51#define STACK_SLOT_TID (SFS-16)
52#define STACK_SLOT_PSSCR (SFS-24)
53#define STACK_SLOT_PID (SFS-32)
54#define STACK_SLOT_IAMR (SFS-40)
55#define STACK_SLOT_CIABR (SFS-48)
56#define STACK_SLOT_DAWR (SFS-56)
57#define STACK_SLOT_DAWRX (SFS-64)
769377f7 58#define STACK_SLOT_HFSCR (SFS-72)
7ceaa6dc 59
de56a948 60/*
19ccb76a 61 * Call kvmppc_hv_entry in real mode.
de56a948
PM
62 * Must be called with interrupts hard-disabled.
63 *
64 * Input Registers:
65 *
66 * LR = return address to continue at after eventually re-enabling MMU
67 */
6ed179b6 68_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
218309b7
PM
69 mflr r0
70 std r0, PPC_LR_STKOFF(r1)
71 stdu r1, -112(r1)
de56a948 72 mfmsr r10
8b24e69f 73 std r10, HSTATE_HOST_MSR(r13)
218309b7 74 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
de56a948
PM
75 li r0,MSR_RI
76 andc r0,r10,r0
77 li r6,MSR_IR | MSR_DR
78 andc r6,r10,r6
79 mtmsrd r0,1 /* clear RI in MSR */
80 mtsrr0 r5
81 mtsrr1 r6
222f20f1 82 RFI_TO_KERNEL
de56a948 83
218309b7 84kvmppc_call_hv_entry:
c0101509
PM
85BEGIN_FTR_SECTION
86 /* On P9, do LPCR setting, if necessary */
87 ld r3, HSTATE_SPLIT_MODE(r13)
88 cmpdi r3, 0
89 beq 46f
90 lwz r4, KVM_SPLIT_DO_SET(r3)
91 cmpwi r4, 0
92 beq 46f
93 bl kvmhv_p9_set_lpcr
94 nop
9546:
96END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
97
e0b7ec05 98 ld r4, HSTATE_KVM_VCPU(r13)
218309b7
PM
99 bl kvmppc_hv_entry
100
101 /* Back from guest - restore host state and return to caller */
102
eee7ff9d 103BEGIN_FTR_SECTION
218309b7
PM
104 /* Restore host DABR and DABRX */
105 ld r5,HSTATE_DABR(r13)
106 li r6,7
107 mtspr SPRN_DABR,r5
108 mtspr SPRN_DABRX,r6
eee7ff9d 109END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
218309b7
PM
110
111 /* Restore SPRG3 */
9d378dfa
SW
112 ld r3,PACA_SPRG_VDSO(r13)
113 mtspr SPRN_SPRG_VDSO_WRITE,r3
218309b7 114
218309b7 115 /* Reload the host's PMU registers */
8e0b634b 116 lbz r4, PACA_PMCINUSE(r13) /* is the host using the PMU? */
218309b7
PM
117 cmpwi r4, 0
118 beq 23f /* skip if not */
9bc01a9b 119BEGIN_FTR_SECTION
9a4fc4ea 120 ld r3, HSTATE_MMCR0(r13)
9bc01a9b
PM
121 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
122 cmpwi r4, MMCR0_PMAO
123 beql kvmppc_fix_pmao
124END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
9a4fc4ea
ME
125 lwz r3, HSTATE_PMC1(r13)
126 lwz r4, HSTATE_PMC2(r13)
127 lwz r5, HSTATE_PMC3(r13)
128 lwz r6, HSTATE_PMC4(r13)
129 lwz r8, HSTATE_PMC5(r13)
130 lwz r9, HSTATE_PMC6(r13)
218309b7
PM
131 mtspr SPRN_PMC1, r3
132 mtspr SPRN_PMC2, r4
133 mtspr SPRN_PMC3, r5
134 mtspr SPRN_PMC4, r6
135 mtspr SPRN_PMC5, r8
136 mtspr SPRN_PMC6, r9
9a4fc4ea
ME
137 ld r3, HSTATE_MMCR0(r13)
138 ld r4, HSTATE_MMCR1(r13)
139 ld r5, HSTATE_MMCRA(r13)
140 ld r6, HSTATE_SIAR(r13)
141 ld r7, HSTATE_SDAR(r13)
218309b7
PM
142 mtspr SPRN_MMCR1, r4
143 mtspr SPRN_MMCRA, r5
72cde5a8
PM
144 mtspr SPRN_SIAR, r6
145 mtspr SPRN_SDAR, r7
146BEGIN_FTR_SECTION
9a4fc4ea
ME
147 ld r8, HSTATE_MMCR2(r13)
148 ld r9, HSTATE_SIER(r13)
72cde5a8
PM
149 mtspr SPRN_MMCR2, r8
150 mtspr SPRN_SIER, r9
151END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
218309b7
PM
152 mtspr SPRN_MMCR0, r3
153 isync
15423:
155
e0b7ec05
PM
156 /*
157 * Reload DEC. HDEC interrupts were disabled when
158 * we reloaded the host's LPCR value.
159 */
160 ld r3, HSTATE_DECEXP(r13)
161 mftb r4
162 subf r4, r4, r3
163 mtspr SPRN_DEC, r4
164
b4deba5c
PM
165 /* hwthread_req may have got set by cede or no vcpu, so clear it */
166 li r0, 0
167 stb r0, HSTATE_HWTHREAD_REQ(r13)
168
218309b7 169 /*
e20bbd3d
AP
170 * For external interrupts we need to call the Linux
171 * handler to process the interrupt. We do that by jumping
172 * to absolute address 0x500 for external interrupts.
173 * The [h]rfid at the end of the handler will return to
174 * the book3s_hv_interrupts.S code. For other interrupts
175 * we do the rfid to get back to the book3s_hv_interrupts.S
176 * code here.
218309b7
PM
177 */
178 ld r8, 112+PPC_LR_STKOFF(r1)
179 addi r1, r1, 112
180 ld r7, HSTATE_HOST_MSR(r13)
181
8b24e69f
PM
182 /* Return the trap number on this thread as the return value */
183 mr r3, r12
184
53af3ba2
PM
185 /*
186 * If we came back from the guest via a relocation-on interrupt,
187 * we will be in virtual mode at this point, which makes it a
188 * little easier to get back to the caller.
189 */
190 mfmsr r0
191 andi. r0, r0, MSR_IR /* in real mode? */
192 bne .Lvirt_return
193
8b24e69f 194 /* RFI into the highmem handler */
218309b7
PM
195 mfmsr r6
196 li r0, MSR_RI
197 andc r6, r6, r0
198 mtmsrd r6, 1 /* Clear RI in MSR */
199 mtsrr0 r8
200 mtsrr1 r7
222f20f1 201 RFI_TO_KERNEL
218309b7 202
8b24e69f 203 /* Virtual-mode return */
53af3ba2 204.Lvirt_return:
8b24e69f 205 mtlr r8
53af3ba2
PM
206 blr
207
e0b7ec05
PM
208kvmppc_primary_no_guest:
209 /* We handle this much like a ceded vcpu */
fd6d53b1 210 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
2f272463
PM
211 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
212 /* HDEC value came from DEC in the first place, it will fit */
fd6d53b1
PM
213 mfspr r3, SPRN_HDEC
214 mtspr SPRN_DEC, r3
6af27c84
PM
215 /*
216 * Make sure the primary has finished the MMU switch.
217 * We should never get here on a secondary thread, but
218 * check it for robustness' sake.
219 */
220 ld r5, HSTATE_KVM_VCORE(r13)
22165: lbz r0, VCORE_IN_GUEST(r5)
222 cmpwi r0, 0
223 beq 65b
224 /* Set LPCR. */
225 ld r8,VCORE_LPCR(r5)
226 mtspr SPRN_LPCR,r8
227 isync
e0b7ec05
PM
228 /* set our bit in napping_threads */
229 ld r5, HSTATE_KVM_VCORE(r13)
230 lbz r7, HSTATE_PTID(r13)
231 li r0, 1
232 sld r0, r0, r7
233 addi r6, r5, VCORE_NAPPING_THREADS
2341: lwarx r3, 0, r6
235 or r3, r3, r0
236 stwcx. r3, 0, r6
237 bne 1b
7d6c40da 238 /* order napping_threads update vs testing entry_exit_map */
e0b7ec05
PM
239 isync
240 li r12, 0
241 lwz r7, VCORE_ENTRY_EXIT(r5)
242 cmpwi r7, 0x100
243 bge kvm_novcpu_exit /* another thread already exiting */
244 li r3, NAPPING_NOVCPU
245 stb r3, HSTATE_NAPPING(r13)
e0b7ec05 246
ccc07772 247 li r3, 0 /* Don't wake on privileged (OS) doorbell */
e0b7ec05
PM
248 b kvm_do_nap
249
37f55d30
SW
250/*
251 * kvm_novcpu_wakeup
252 * Entered from kvm_start_guest if kvm_hstate.napping is set
253 * to NAPPING_NOVCPU
254 * r2 = kernel TOC
255 * r13 = paca
256 */
e0b7ec05
PM
257kvm_novcpu_wakeup:
258 ld r1, HSTATE_HOST_R1(r13)
259 ld r5, HSTATE_KVM_VCORE(r13)
260 li r0, 0
261 stb r0, HSTATE_NAPPING(r13)
e0b7ec05 262
e3bbbbfa
PM
263 /* check the wake reason */
264 bl kvmppc_check_wake_reason
6af27c84 265
37f55d30
SW
266 /*
267 * Restore volatile registers since we could have called
268 * a C routine in kvmppc_check_wake_reason.
269 * r5 = VCORE
270 */
271 ld r5, HSTATE_KVM_VCORE(r13)
272
e0b7ec05 273 /* see if any other thread is already exiting */
e0b7ec05
PM
274 lwz r0, VCORE_ENTRY_EXIT(r5)
275 cmpwi r0, 0x100
276 bge kvm_novcpu_exit
277
278 /* clear our bit in napping_threads */
279 lbz r7, HSTATE_PTID(r13)
280 li r0, 1
281 sld r0, r0, r7
282 addi r6, r5, VCORE_NAPPING_THREADS
e3bbbbfa
PM
2834: lwarx r7, 0, r6
284 andc r7, r7, r0
285 stwcx. r7, 0, r6
e0b7ec05
PM
286 bne 4b
287
e3bbbbfa 288 /* See if the wake reason means we need to exit */
e0b7ec05
PM
289 cmpdi r3, 0
290 bge kvm_novcpu_exit
e0b7ec05 291
fd6d53b1
PM
292 /* See if our timeslice has expired (HDEC is negative) */
293 mfspr r0, SPRN_HDEC
2f272463 294 EXTEND_HDEC(r0)
fd6d53b1 295 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
2f272463 296 cmpdi r0, 0
fd6d53b1
PM
297 blt kvm_novcpu_exit
298
e0b7ec05
PM
299 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
300 ld r4, HSTATE_KVM_VCPU(r13)
301 cmpdi r4, 0
b6c295df
PM
302 beq kvmppc_primary_no_guest
303
304#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
305 addi r3, r4, VCPU_TB_RMENTRY
306 bl kvmhv_start_timing
307#endif
308 b kvmppc_got_guest
e0b7ec05
PM
309
310kvm_novcpu_exit:
6af27c84
PM
311#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
312 ld r4, HSTATE_KVM_VCPU(r13)
313 cmpdi r4, 0
314 beq 13f
315 addi r3, r4, VCPU_TB_RMEXIT
316 bl kvmhv_accumulate_time
317#endif
eddb60fb 31813: mr r3, r12
7ceaa6dc 319 stw r12, STACK_SLOT_TRAP(r1)
eddb60fb
PM
320 bl kvmhv_commence_exit
321 nop
6af27c84 322 b kvmhv_switch_to_host
e0b7ec05 323
371fefd6 324/*
e0b7ec05 325 * We come in here when wakened from nap mode.
371fefd6
PM
326 * Relocation is off and most register values are lost.
327 * r13 points to the PACA.
9d292501 328 * r3 contains the SRR1 wakeup value, SRR1 is trashed.
371fefd6
PM
329 */
330 .globl kvm_start_guest
331kvm_start_guest:
fd17dc7b 332 /* Set runlatch bit the minute you wake up from nap */
1f09c3ed
PM
333 mfspr r0, SPRN_CTRLF
334 ori r0, r0, 1
335 mtspr SPRN_CTRLT, r0
fd17dc7b 336
9d292501
NP
337 /*
338 * Could avoid this and pass it through in r3. For now,
339 * code expects it to be in SRR1.
340 */
341 mtspr SPRN_SRR1,r3
342
19ccb76a
PM
343 ld r2,PACATOC(r13)
344
f0888f70
PM
345 li r0,KVM_HWTHREAD_IN_KVM
346 stb r0,HSTATE_HWTHREAD_STATE(r13)
371fefd6 347
f0888f70
PM
348 /* NV GPR values from power7_idle() will no longer be valid */
349 li r0,1
350 stb r0,PACA_NAPSTATELOST(r13)
371fefd6 351
4619ac88
PM
352 /* were we napping due to cede? */
353 lbz r0,HSTATE_NAPPING(r13)
e0b7ec05
PM
354 cmpwi r0,NAPPING_CEDE
355 beq kvm_end_cede
356 cmpwi r0,NAPPING_NOVCPU
357 beq kvm_novcpu_wakeup
358
359 ld r1,PACAEMERGSP(r13)
360 subi r1,r1,STACK_FRAME_OVERHEAD
4619ac88
PM
361
362 /*
363 * We weren't napping due to cede, so this must be a secondary
364 * thread being woken up to run a guest, or being woken up due
365 * to a stray IPI. (Or due to some machine check or hypervisor
366 * maintenance interrupt while the core is in KVM.)
367 */
f0888f70
PM
368
369 /* Check the wake reason in SRR1 to see why we got here */
e3bbbbfa 370 bl kvmppc_check_wake_reason
37f55d30
SW
371 /*
372 * kvmppc_check_wake_reason could invoke a C routine, but we
373 * have no volatile registers to restore when we return.
374 */
375
e3bbbbfa
PM
376 cmpdi r3, 0
377 bge kvm_no_guest
371fefd6 378
b4deba5c
PM
379 /* get vcore pointer, NULL if we have nothing to run */
380 ld r5,HSTATE_KVM_VCORE(r13)
381 cmpdi r5,0
382 /* if we have no vcore to run, go back to sleep */
7b444c67 383 beq kvm_no_guest
f0888f70 384
56548fc0
PM
385kvm_secondary_got_guest:
386
e0b7ec05 387 /* Set HSTATE_DSCR(r13) to something sensible */
1db36525 388 ld r6, PACA_DSCR_DEFAULT(r13)
e0b7ec05 389 std r6, HSTATE_DSCR(r13)
2fde6d20 390
b4deba5c
PM
391 /* On thread 0 of a subcore, set HDEC to max */
392 lbz r4, HSTATE_PTID(r13)
393 cmpwi r4, 0
394 bne 63f
2f272463
PM
395 LOAD_REG_ADDR(r6, decrementer_max)
396 ld r6, 0(r6)
b4deba5c
PM
397 mtspr SPRN_HDEC, r6
398 /* and set per-LPAR registers, if doing dynamic micro-threading */
399 ld r6, HSTATE_SPLIT_MODE(r13)
400 cmpdi r6, 0
401 beq 63f
c0101509 402BEGIN_FTR_SECTION
b4deba5c
PM
403 ld r0, KVM_SPLIT_RPR(r6)
404 mtspr SPRN_RPR, r0
405 ld r0, KVM_SPLIT_PMMAR(r6)
406 mtspr SPRN_PMMAR, r0
407 ld r0, KVM_SPLIT_LDBAR(r6)
408 mtspr SPRN_LDBAR, r0
409 isync
c0101509
PM
410FTR_SECTION_ELSE
411 /* On P9 we use the split_info for coordinating LPCR changes */
412 lwz r4, KVM_SPLIT_DO_SET(r6)
413 cmpwi r4, 0
d20fe50a 414 beq 1f
c0101509
PM
415 mr r3, r6
416 bl kvmhv_p9_set_lpcr
417 nop
d20fe50a 4181:
c0101509 419ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
b4deba5c
PM
42063:
421 /* Order load of vcpu after load of vcore */
5d5b99cd 422 lwsync
b4deba5c 423 ld r4, HSTATE_KVM_VCPU(r13)
e0b7ec05 424 bl kvmppc_hv_entry
218309b7
PM
425
426 /* Back from the guest, go back to nap */
b4deba5c 427 /* Clear our vcpu and vcore pointers so we don't come back in early */
218309b7 428 li r0, 0
b4deba5c 429 std r0, HSTATE_KVM_VCPU(r13)
f019b7ad 430 /*
b4deba5c 431 * Once we clear HSTATE_KVM_VCORE(r13), the code in
5d5b99cd
PM
432 * kvmppc_run_core() is going to assume that all our vcpu
433 * state is visible in memory. This lwsync makes sure
434 * that that is true.
f019b7ad 435 */
218309b7 436 lwsync
b4deba5c 437 std r0, HSTATE_KVM_VCORE(r13)
218309b7 438
fd7bacbc
MS
439 /*
440 * All secondaries exiting guest will fall through this path.
441 * Before proceeding, just check for HMI interrupt and
442 * invoke opal hmi handler. By now we are sure that the
443 * primary thread on this core/subcore has already made partition
444 * switch/TB resync and we are good to call opal hmi handler.
445 */
446 cmpwi r12, BOOK3S_INTERRUPT_HMI
447 bne kvm_no_guest
448
449 li r3,0 /* NULL argument */
450 bl hmi_exception_realmode
56548fc0
PM
451/*
452 * At this point we have finished executing in the guest.
453 * We need to wait for hwthread_req to become zero, since
454 * we may not turn on the MMU while hwthread_req is non-zero.
455 * While waiting we also need to check if we get given a vcpu to run.
456 */
218309b7 457kvm_no_guest:
56548fc0
PM
458 lbz r3, HSTATE_HWTHREAD_REQ(r13)
459 cmpwi r3, 0
460 bne 53f
461 HMT_MEDIUM
462 li r0, KVM_HWTHREAD_IN_KERNEL
218309b7 463 stb r0, HSTATE_HWTHREAD_STATE(r13)
56548fc0
PM
464 /* need to recheck hwthread_req after a barrier, to avoid race */
465 sync
466 lbz r3, HSTATE_HWTHREAD_REQ(r13)
467 cmpwi r3, 0
468 bne 54f
469/*
5fa6b6bd 470 * We jump to pnv_wakeup_loss, which will return to the caller
56548fc0 471 * of power7_nap in the powernv cpu offline loop. The value we
9d292501
NP
472 * put in r3 becomes the return value for power7_nap. pnv_wakeup_loss
473 * requires SRR1 in r12.
56548fc0 474 */
218309b7
PM
475 li r3, LPCR_PECE0
476 mfspr r4, SPRN_LPCR
477 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
478 mtspr SPRN_LPCR, r4
56548fc0 479 li r3, 0
9d292501 480 mfspr r12,SPRN_SRR1
5fa6b6bd 481 b pnv_wakeup_loss
56548fc0
PM
482
48353: HMT_LOW
b4deba5c
PM
484 ld r5, HSTATE_KVM_VCORE(r13)
485 cmpdi r5, 0
486 bne 60f
487 ld r3, HSTATE_SPLIT_MODE(r13)
488 cmpdi r3, 0
489 beq kvm_no_guest
c0101509
PM
490 lwz r0, KVM_SPLIT_DO_SET(r3)
491 cmpwi r0, 0
492 bne kvmhv_do_set
493 lwz r0, KVM_SPLIT_DO_RESTORE(r3)
494 cmpwi r0, 0
495 bne kvmhv_do_restore
b4deba5c
PM
496 lbz r0, KVM_SPLIT_DO_NAP(r3)
497 cmpwi r0, 0
56548fc0
PM
498 beq kvm_no_guest
499 HMT_MEDIUM
b4deba5c
PM
500 b kvm_unsplit_nap
50160: HMT_MEDIUM
56548fc0
PM
502 b kvm_secondary_got_guest
503
50454: li r0, KVM_HWTHREAD_IN_KVM
505 stb r0, HSTATE_HWTHREAD_STATE(r13)
506 b kvm_no_guest
218309b7 507
c0101509
PM
508kvmhv_do_set:
509 /* Set LPCR, LPIDR etc. on P9 */
510 HMT_MEDIUM
511 bl kvmhv_p9_set_lpcr
512 nop
513 b kvm_no_guest
514
515kvmhv_do_restore:
516 HMT_MEDIUM
517 bl kvmhv_p9_restore_lpcr
518 nop
519 b kvm_no_guest
520
b4deba5c
PM
521/*
522 * Here the primary thread is trying to return the core to
523 * whole-core mode, so we need to nap.
524 */
525kvm_unsplit_nap:
fd7bacbc
MS
526 /*
527 * When secondaries are napping in kvm_unsplit_nap() with
528 * hwthread_req = 1, HMI goes ignored even though subcores are
529 * already exited the guest. Hence HMI keeps waking up secondaries
530 * from nap in a loop and secondaries always go back to nap since
531 * no vcore is assigned to them. This makes impossible for primary
532 * thread to get hold of secondary threads resulting into a soft
533 * lockup in KVM path.
534 *
535 * Let us check if HMI is pending and handle it before we go to nap.
536 */
537 cmpwi r12, BOOK3S_INTERRUPT_HMI
538 bne 55f
539 li r3, 0 /* NULL argument */
540 bl hmi_exception_realmode
54155:
7f235328
GS
542 /*
543 * Ensure that secondary doesn't nap when it has
544 * its vcore pointer set.
545 */
546 sync /* matches smp_mb() before setting split_info.do_nap */
547 ld r0, HSTATE_KVM_VCORE(r13)
548 cmpdi r0, 0
549 bne kvm_no_guest
b4deba5c
PM
550 /* clear any pending message */
551BEGIN_FTR_SECTION
552 lis r6, (PPC_DBELL_SERVER << (63-36))@h
553 PPC_MSGCLR(6)
554END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
555 /* Set kvm_split_mode.napped[tid] = 1 */
556 ld r3, HSTATE_SPLIT_MODE(r13)
557 li r0, 1
c0101509 558 lbz r4, HSTATE_TID(r13)
b4deba5c
PM
559 addi r4, r4, KVM_SPLIT_NAPPED
560 stbx r0, r3, r4
561 /* Check the do_nap flag again after setting napped[] */
562 sync
563 lbz r0, KVM_SPLIT_DO_NAP(r3)
564 cmpwi r0, 0
565 beq 57f
566 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
bf53c88e
PM
567 mfspr r5, SPRN_LPCR
568 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
569 b kvm_nap_sequence
b4deba5c
PM
570
57157: li r0, 0
572 stbx r0, r3, r4
573 b kvm_no_guest
574
218309b7
PM
575/******************************************************************************
576 * *
577 * Entry code *
578 * *
579 *****************************************************************************/
580
de56a948
PM
581.global kvmppc_hv_entry
582kvmppc_hv_entry:
583
584 /* Required state:
585 *
e0b7ec05 586 * R4 = vcpu pointer (or NULL)
de56a948
PM
587 * MSR = ~IR|DR
588 * R13 = PACA
589 * R1 = host R1
06a29e42 590 * R2 = TOC
de56a948 591 * all other volatile GPRS = free
f4c51f84 592 * Does not preserve non-volatile GPRs or CR fields
de56a948
PM
593 */
594 mflr r0
218309b7 595 std r0, PPC_LR_STKOFF(r1)
7ceaa6dc 596 stdu r1, -SFS(r1)
de56a948 597
de56a948
PM
598 /* Save R1 in the PACA */
599 std r1, HSTATE_HOST_R1(r13)
600
44a3add8
PM
601 li r6, KVM_GUEST_MODE_HOST_HV
602 stb r6, HSTATE_IN_GUEST(r13)
603
b6c295df
PM
604#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
605 /* Store initial timestamp */
606 cmpdi r4, 0
607 beq 1f
608 addi r3, r4, VCPU_TB_RMENTRY
609 bl kvmhv_start_timing
6101:
611#endif
f4c51f84
PM
612
613 /* Use cr7 as an indication of radix mode */
614 ld r5, HSTATE_KVM_VCORE(r13)
615 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
616 lbz r0, KVM_RADIX(r9)
617 cmpwi cr7, r0, 0
618
9e368f29 619 /*
c17b98cf 620 * POWER7/POWER8 host -> guest partition switch code.
9e368f29
PM
621 * We don't have to lock against concurrent tlbies,
622 * but we do have to coordinate across hardware threads.
623 */
7d6c40da 624 /* Set bit in entry map iff exit map is zero. */
7d6c40da
PM
625 li r7, 1
626 lbz r6, HSTATE_PTID(r13)
627 sld r7, r7, r6
f4c51f84
PM
628 addi r8, r5, VCORE_ENTRY_EXIT
62921: lwarx r3, 0, r8
7d6c40da 630 cmpwi r3, 0x100 /* any threads starting to exit? */
371fefd6 631 bge secondary_too_late /* if so we're too late to the party */
7d6c40da 632 or r3, r3, r7
f4c51f84 633 stwcx. r3, 0, r8
371fefd6
PM
634 bne 21b
635
636 /* Primary thread switches to guest partition. */
371fefd6 637 cmpwi r6,0
6af27c84 638 bne 10f
9a4506e1
NP
639
640 /* Radix has already switched LPID and flushed core TLB */
641 bne cr7, 22f
642
de56a948 643 lwz r7,KVM_LPID(r9)
7a84084c
PM
644BEGIN_FTR_SECTION
645 ld r6,KVM_SDR1(r9)
de56a948
PM
646 li r0,LPID_RSVD /* switch to reserved LPID */
647 mtspr SPRN_LPID,r0
648 ptesync
649 mtspr SPRN_SDR1,r6 /* switch to partition page table */
7a84084c 650END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
de56a948
PM
651 mtspr SPRN_LPID,r7
652 isync
1b400ba0 653
9a4506e1 654 /* See if we need to flush the TLB. Hash has to be done in RM */
1b400ba0 655 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
a29ebeaf
PM
656BEGIN_FTR_SECTION
657 /*
658 * On POWER9, individual threads can come in here, but the
659 * TLB is shared between the 4 threads in a core, hence
660 * invalidating on one thread invalidates for all.
661 * Thus we make all 4 threads use the same bit here.
662 */
663 clrrdi r6,r6,2
664END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1b400ba0
PM
665 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
666 srdi r6,r6,6 /* doubleword number */
667 sldi r6,r6,3 /* address offset */
668 add r6,r6,r9
669 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
a29ebeaf
PM
670 li r8,1
671 sld r8,r8,r7
1b400ba0 672 ld r7,0(r6)
a29ebeaf 673 and. r7,r7,r8
1b400ba0 674 beq 22f
ca252055 675 /* Flush the TLB of any entries for this LPID */
a29ebeaf
PM
676 lwz r0,KVM_TLB_SETS(r9)
677 mtctr r0
1b400ba0
PM
678 li r7,0x800 /* IS field = 0b10 */
679 ptesync
a29ebeaf 680 li r0,0 /* RS for P9 version of tlbiel */
a29ebeaf 68128: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
1b400ba0
PM
682 addi r7,r7,0x1000
683 bdnz 28b
9a4506e1 684 ptesync
a29ebeaf
PM
68523: ldarx r7,0,r6 /* clear the bit after TLB flushed */
686 andc r7,r7,r8
687 stdcx. r7,0,r6
688 bne 23b
1b400ba0 689
93b0f4dc
PM
690 /* Add timebase offset onto timebase */
69122: ld r8,VCORE_TB_OFFSET(r5)
692 cmpdi r8,0
693 beq 37f
57b8daa7 694 std r8, VCORE_TB_OFFSET_APPL(r5)
93b0f4dc
PM
695 mftb r6 /* current host timebase */
696 add r8,r8,r6
697 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
698 mftb r7 /* check if lower 24 bits overflowed */
699 clrldi r6,r6,40
700 clrldi r7,r7,40
701 cmpld r7,r6
702 bge 37f
703 addis r8,r8,0x100 /* if so, increment upper 40 bits */
704 mtspr SPRN_TBU40,r8
705
388cc6e1
PM
706 /* Load guest PCR value to select appropriate compat mode */
70737: ld r7, VCORE_PCR(r5)
708 cmpdi r7, 0
709 beq 38f
710 mtspr SPRN_PCR, r7
71138:
b005255e
MN
712
713BEGIN_FTR_SECTION
88b02cf9 714 /* DPDES and VTB are shared between threads */
b005255e 715 ld r8, VCORE_DPDES(r5)
88b02cf9 716 ld r7, VCORE_VTB(r5)
b005255e 717 mtspr SPRN_DPDES, r8
88b02cf9 718 mtspr SPRN_VTB, r7
b005255e
MN
719END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
720
fd7bacbc
MS
721 /* Mark the subcore state as inside guest */
722 bl kvmppc_subcore_enter_guest
723 nop
724 ld r5, HSTATE_KVM_VCORE(r13)
725 ld r4, HSTATE_KVM_VCPU(r13)
388cc6e1 726 li r0,1
371fefd6 727 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
9e368f29 728
e0b7ec05 729 /* Do we have a guest vcpu to run? */
6af27c84 73010: cmpdi r4, 0
e0b7ec05
PM
731 beq kvmppc_primary_no_guest
732kvmppc_got_guest:
e0b7ec05
PM
733 /* Increment yield count if they have a VPA */
734 ld r3, VCPU_VPA(r4)
735 cmpdi r3, 0
736 beq 25f
0865a583
AG
737 li r6, LPPACA_YIELDCOUNT
738 LWZX_BE r5, r3, r6
e0b7ec05 739 addi r5, r5, 1
0865a583 740 STWX_BE r5, r3, r6
e0b7ec05
PM
741 li r6, 1
742 stb r6, VCPU_VPA_DIRTY(r4)
74325:
744
e0b7ec05
PM
745 /* Save purr/spurr */
746 mfspr r5,SPRN_PURR
747 mfspr r6,SPRN_SPURR
748 std r5,HSTATE_PURR(r13)
749 std r6,HSTATE_SPURR(r13)
750 ld r7,VCPU_PURR(r4)
751 ld r8,VCPU_SPURR(r4)
752 mtspr SPRN_PURR,r7
753 mtspr SPRN_SPURR,r8
e0b7ec05 754
e9cf1e08
PM
755 /* Save host values of some registers */
756BEGIN_FTR_SECTION
757 mfspr r5, SPRN_TIDR
758 mfspr r6, SPRN_PSSCR
f4c51f84 759 mfspr r7, SPRN_PID
4c3bb4cc 760 mfspr r8, SPRN_IAMR
e9cf1e08
PM
761 std r5, STACK_SLOT_TID(r1)
762 std r6, STACK_SLOT_PSSCR(r1)
f4c51f84 763 std r7, STACK_SLOT_PID(r1)
4c3bb4cc 764 std r8, STACK_SLOT_IAMR(r1)
769377f7
PM
765 mfspr r5, SPRN_HFSCR
766 std r5, STACK_SLOT_HFSCR(r1)
e9cf1e08 767END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
7ceaa6dc
PM
768BEGIN_FTR_SECTION
769 mfspr r5, SPRN_CIABR
770 mfspr r6, SPRN_DAWR
771 mfspr r7, SPRN_DAWRX
772 std r5, STACK_SLOT_CIABR(r1)
773 std r6, STACK_SLOT_DAWR(r1)
774 std r7, STACK_SLOT_DAWRX(r1)
775END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e9cf1e08 776
e0b7ec05
PM
777BEGIN_FTR_SECTION
778 /* Set partition DABR */
779 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
8563bf52 780 lwz r5,VCPU_DABRX(r4)
e0b7ec05
PM
781 ld r6,VCPU_DABR(r4)
782 mtspr SPRN_DABRX,r5
783 mtspr SPRN_DABR,r6
e0b7ec05 784 isync
e0b7ec05
PM
785END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
786
e4e38121 787#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
4bb3c7a0
PM
788/*
789 * Branch around the call if both CPU_FTR_TM and
790 * CPU_FTR_P9_TM_HV_ASSIST are off.
791 */
e4e38121 792BEGIN_FTR_SECTION
4bb3c7a0
PM
793 b 91f
794END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
67f8a8c1
PM
795 /*
796 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
797 */
f024ee09 798 bl kvmppc_restore_tm
4bb3c7a0 79991:
e4e38121
MN
800#endif
801
e0b7ec05
PM
802 /* Load guest PMU registers */
803 /* R4 is live here (vcpu pointer) */
804 li r3, 1
805 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
806 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
807 isync
9bc01a9b
PM
808BEGIN_FTR_SECTION
809 ld r3, VCPU_MMCR(r4)
810 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
811 cmpwi r5, MMCR0_PMAO
812 beql kvmppc_fix_pmao
813END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
e0b7ec05
PM
814 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
815 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
816 lwz r6, VCPU_PMC + 8(r4)
817 lwz r7, VCPU_PMC + 12(r4)
818 lwz r8, VCPU_PMC + 16(r4)
819 lwz r9, VCPU_PMC + 20(r4)
e0b7ec05
PM
820 mtspr SPRN_PMC1, r3
821 mtspr SPRN_PMC2, r5
822 mtspr SPRN_PMC3, r6
823 mtspr SPRN_PMC4, r7
824 mtspr SPRN_PMC5, r8
825 mtspr SPRN_PMC6, r9
e0b7ec05
PM
826 ld r3, VCPU_MMCR(r4)
827 ld r5, VCPU_MMCR + 8(r4)
828 ld r6, VCPU_MMCR + 16(r4)
829 ld r7, VCPU_SIAR(r4)
830 ld r8, VCPU_SDAR(r4)
831 mtspr SPRN_MMCR1, r5
832 mtspr SPRN_MMCRA, r6
833 mtspr SPRN_SIAR, r7
834 mtspr SPRN_SDAR, r8
b005255e
MN
835BEGIN_FTR_SECTION
836 ld r5, VCPU_MMCR + 24(r4)
837 ld r6, VCPU_SIER(r4)
83677f55
PM
838 mtspr SPRN_MMCR2, r5
839 mtspr SPRN_SIER, r6
840BEGIN_FTR_SECTION_NESTED(96)
b005255e
MN
841 lwz r7, VCPU_PMC + 24(r4)
842 lwz r8, VCPU_PMC + 28(r4)
843 ld r9, VCPU_MMCR + 32(r4)
b005255e
MN
844 mtspr SPRN_SPMC1, r7
845 mtspr SPRN_SPMC2, r8
846 mtspr SPRN_MMCRS, r9
83677f55 847END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
b005255e 848END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
849 mtspr SPRN_MMCR0, r3
850 isync
851
852 /* Load up FP, VMX and VSX registers */
853 bl kvmppc_load_fp
854
855 ld r14, VCPU_GPR(R14)(r4)
856 ld r15, VCPU_GPR(R15)(r4)
857 ld r16, VCPU_GPR(R16)(r4)
858 ld r17, VCPU_GPR(R17)(r4)
859 ld r18, VCPU_GPR(R18)(r4)
860 ld r19, VCPU_GPR(R19)(r4)
861 ld r20, VCPU_GPR(R20)(r4)
862 ld r21, VCPU_GPR(R21)(r4)
863 ld r22, VCPU_GPR(R22)(r4)
864 ld r23, VCPU_GPR(R23)(r4)
865 ld r24, VCPU_GPR(R24)(r4)
866 ld r25, VCPU_GPR(R25)(r4)
867 ld r26, VCPU_GPR(R26)(r4)
868 ld r27, VCPU_GPR(R27)(r4)
869 ld r28, VCPU_GPR(R28)(r4)
870 ld r29, VCPU_GPR(R29)(r4)
871 ld r30, VCPU_GPR(R30)(r4)
872 ld r31, VCPU_GPR(R31)(r4)
873
e0b7ec05
PM
874 /* Switch DSCR to guest value */
875 ld r5, VCPU_DSCR(r4)
876 mtspr SPRN_DSCR, r5
e0b7ec05 877
b005255e 878BEGIN_FTR_SECTION
c17b98cf 879 /* Skip next section on POWER7 */
b005255e
MN
880 b 8f
881END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
b005255e
MN
882 /* Load up POWER8-specific registers */
883 ld r5, VCPU_IAMR(r4)
884 lwz r6, VCPU_PSPB(r4)
885 ld r7, VCPU_FSCR(r4)
886 mtspr SPRN_IAMR, r5
887 mtspr SPRN_PSPB, r6
888 mtspr SPRN_FSCR, r7
889 ld r5, VCPU_DAWR(r4)
890 ld r6, VCPU_DAWRX(r4)
891 ld r7, VCPU_CIABR(r4)
892 ld r8, VCPU_TAR(r4)
b53221e7
MN
893 /*
894 * Handle broken DAWR case by not writing it. This means we
895 * can still store the DAWR register for migration.
896 */
897BEGIN_FTR_SECTION
b005255e
MN
898 mtspr SPRN_DAWR, r5
899 mtspr SPRN_DAWRX, r6
b53221e7 900END_FTR_SECTION_IFSET(CPU_FTR_DAWR)
b005255e
MN
901 mtspr SPRN_CIABR, r7
902 mtspr SPRN_TAR, r8
903 ld r5, VCPU_IC(r4)
7b490411 904 ld r8, VCPU_EBBHR(r4)
88b02cf9 905 mtspr SPRN_IC, r5
b005255e
MN
906 mtspr SPRN_EBBHR, r8
907 ld r5, VCPU_EBBRR(r4)
908 ld r6, VCPU_BESCR(r4)
83677f55
PM
909 lwz r7, VCPU_GUEST_PID(r4)
910 ld r8, VCPU_WORT(r4)
b005255e
MN
911 mtspr SPRN_EBBRR, r5
912 mtspr SPRN_BESCR, r6
83677f55
PM
913 mtspr SPRN_PID, r7
914 mtspr SPRN_WORT, r8
f11f6f79
PM
915BEGIN_FTR_SECTION
916 PPC_INVALIDATE_ERAT
917END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
83677f55 918BEGIN_FTR_SECTION
e9cf1e08 919 /* POWER8-only registers */
b005255e
MN
920 ld r5, VCPU_TCSCR(r4)
921 ld r6, VCPU_ACOP(r4)
83677f55
PM
922 ld r7, VCPU_CSIGR(r4)
923 ld r8, VCPU_TACR(r4)
b005255e
MN
924 mtspr SPRN_TCSCR, r5
925 mtspr SPRN_ACOP, r6
83677f55
PM
926 mtspr SPRN_CSIGR, r7
927 mtspr SPRN_TACR, r8
4bb3c7a0 928 nop
e9cf1e08
PM
929FTR_SECTION_ELSE
930 /* POWER9-only registers */
931 ld r5, VCPU_TID(r4)
932 ld r6, VCPU_PSSCR(r4)
4bb3c7a0 933 lbz r8, HSTATE_FAKE_SUSPEND(r13)
e9cf1e08 934 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
4bb3c7a0 935 rldimi r6, r8, PSSCR_FAKE_SUSPEND_LG, 63 - PSSCR_FAKE_SUSPEND_LG
769377f7 936 ld r7, VCPU_HFSCR(r4)
e9cf1e08
PM
937 mtspr SPRN_TIDR, r5
938 mtspr SPRN_PSSCR, r6
769377f7 939 mtspr SPRN_HFSCR, r7
e9cf1e08 940ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
b005255e
MN
9418:
942
e0b7ec05
PM
943 ld r5, VCPU_SPRG0(r4)
944 ld r6, VCPU_SPRG1(r4)
945 ld r7, VCPU_SPRG2(r4)
946 ld r8, VCPU_SPRG3(r4)
947 mtspr SPRN_SPRG0, r5
948 mtspr SPRN_SPRG1, r6
949 mtspr SPRN_SPRG2, r7
950 mtspr SPRN_SPRG3, r8
951
952 /* Load up DAR and DSISR */
953 ld r5, VCPU_DAR(r4)
954 lwz r6, VCPU_DSISR(r4)
955 mtspr SPRN_DAR, r5
956 mtspr SPRN_DSISR, r6
957
e0b7ec05
PM
958 /* Restore AMR and UAMOR, set AMOR to all 1s */
959 ld r5,VCPU_AMR(r4)
960 ld r6,VCPU_UAMOR(r4)
961 li r7,-1
962 mtspr SPRN_AMR,r5
963 mtspr SPRN_UAMOR,r6
964 mtspr SPRN_AMOR,r7
de56a948
PM
965
966 /* Restore state of CTRL run bit; assume 1 on entry */
967 lwz r5,VCPU_CTRL(r4)
968 andi. r5,r5,1
969 bne 4f
970 mfspr r6,SPRN_CTRLF
971 clrrdi r6,r6,1
972 mtspr SPRN_CTRLT,r6
9734:
6af27c84
PM
974 /* Secondary threads wait for primary to have done partition switch */
975 ld r5, HSTATE_KVM_VCORE(r13)
976 lbz r6, HSTATE_PTID(r13)
977 cmpwi r6, 0
978 beq 21f
979 lbz r0, VCORE_IN_GUEST(r5)
980 cmpwi r0, 0
981 bne 21f
982 HMT_LOW
b4deba5c
PM
98320: lwz r3, VCORE_ENTRY_EXIT(r5)
984 cmpwi r3, 0x100
985 bge no_switch_exit
986 lbz r0, VCORE_IN_GUEST(r5)
6af27c84
PM
987 cmpwi r0, 0
988 beq 20b
989 HMT_MEDIUM
99021:
991 /* Set LPCR. */
992 ld r8,VCORE_LPCR(r5)
993 mtspr SPRN_LPCR,r8
994 isync
995
57b8daa7
PM
996 /*
997 * Set the decrementer to the guest decrementer.
998 */
999 ld r8,VCPU_DEC_EXPIRES(r4)
1000 /* r8 is a host timebase value here, convert to guest TB */
1001 ld r5,HSTATE_KVM_VCORE(r13)
1002 ld r6,VCORE_TB_OFFSET_APPL(r5)
1003 add r8,r8,r6
1004 mftb r7
1005 subf r3,r7,r8
1006 mtspr SPRN_DEC,r3
1007
6af27c84
PM
1008 /* Check if HDEC expires soon */
1009 mfspr r3, SPRN_HDEC
2f272463
PM
1010 EXTEND_HDEC(r3)
1011 cmpdi r3, 512 /* 1 microsecond */
6af27c84
PM
1012 blt hdec_soon
1013
6964e6a4
PM
1014 /* For hash guest, clear out and reload the SLB */
1015 ld r6, VCPU_KVM(r4)
1016 lbz r0, KVM_RADIX(r6)
1017 cmpwi r0, 0
1018 bne 9f
1019 li r6, 0
1020 slbmte r6, r6
1021 slbia
1022 ptesync
1023
1024 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
1025 lwz r5,VCPU_SLB_MAX(r4)
1026 cmpwi r5,0
1027 beq 9f
1028 mtctr r5
1029 addi r6,r4,VCPU_SLB
10301: ld r8,VCPU_SLB_E(r6)
1031 ld r9,VCPU_SLB_V(r6)
1032 slbmte r9,r8
1033 addi r6,r6,VCPU_SLB_SIZE
1034 bdnz 1b
10359:
1036
5af50993
BH
1037#ifdef CONFIG_KVM_XICS
1038 /* We are entering the guest on that thread, push VCPU to XIVE */
1039 ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
0bfa33c7 1040 cmpldi cr0, r10, 0
5af50993
BH
1041 beq no_xive
1042 ld r11, VCPU_XIVE_SAVED_STATE(r4)
1043 li r9, TM_QW1_OS
5af50993 1044 eieio
ad98dd1a 1045 stdcix r11,r9,r10
5af50993
BH
1046 lwz r11, VCPU_XIVE_CAM_WORD(r4)
1047 li r9, TM_QW1_OS + TM_WORD2
1048 stwcix r11,r9,r10
1049 li r9, 1
35c2405e 1050 stb r9, VCPU_XIVE_PUSHED(r4)
ad98dd1a 1051 eieio
2267ea76
BH
1052
1053 /*
1054 * We clear the irq_pending flag. There is a small chance of a
1055 * race vs. the escalation interrupt happening on another
1056 * processor setting it again, but the only consequence is to
1057 * cause a spurrious wakeup on the next H_CEDE which is not an
1058 * issue.
1059 */
1060 li r0,0
1061 stb r0, VCPU_IRQ_PENDING(r4)
9b9b13a6
BH
1062
1063 /*
1064 * In single escalation mode, if the escalation interrupt is
1065 * on, we mask it.
1066 */
1067 lbz r0, VCPU_XIVE_ESC_ON(r4)
1068 cmpwi r0,0
1069 beq 1f
1070 ld r10, VCPU_XIVE_ESC_RADDR(r4)
1071 li r9, XIVE_ESB_SET_PQ_01
1072 ldcix r0, r10, r9
1073 sync
1074
1075 /* We have a possible subtle race here: The escalation interrupt might
1076 * have fired and be on its way to the host queue while we mask it,
1077 * and if we unmask it early enough (re-cede right away), there is
1078 * a theorical possibility that it fires again, thus landing in the
1079 * target queue more than once which is a big no-no.
1080 *
1081 * Fortunately, solving this is rather easy. If the above load setting
1082 * PQ to 01 returns a previous value where P is set, then we know the
1083 * escalation interrupt is somewhere on its way to the host. In that
1084 * case we simply don't clear the xive_esc_on flag below. It will be
1085 * eventually cleared by the handler for the escalation interrupt.
1086 *
1087 * Then, when doing a cede, we check that flag again before re-enabling
1088 * the escalation interrupt, and if set, we abort the cede.
1089 */
1090 andi. r0, r0, XIVE_ESB_VAL_P
1091 bne- 1f
1092
1093 /* Now P is 0, we can clear the flag */
1094 li r0, 0
1095 stb r0, VCPU_XIVE_ESC_ON(r4)
10961:
5af50993
BH
1097no_xive:
1098#endif /* CONFIG_KVM_XICS */
1099
37f55d30 1100deliver_guest_interrupt:
de56a948 1101 ld r6, VCPU_CTR(r4)
c63517c2 1102 ld r7, VCPU_XER(r4)
de56a948
PM
1103
1104 mtctr r6
1105 mtxer r7
1106
e3bbbbfa 1107kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
4619ac88
PM
1108 ld r10, VCPU_PC(r4)
1109 ld r11, VCPU_MSR(r4)
de56a948
PM
1110 ld r6, VCPU_SRR0(r4)
1111 ld r7, VCPU_SRR1(r4)
e3bbbbfa
PM
1112 mtspr SPRN_SRR0, r6
1113 mtspr SPRN_SRR1, r7
de56a948 1114
4619ac88 1115 /* r11 = vcpu->arch.msr & ~MSR_HV */
de56a948
PM
1116 rldicl r11, r11, 63 - MSR_HV_LG, 1
1117 rotldi r11, r11, 1 + MSR_HV_LG
1118 ori r11, r11, MSR_ME
1119
19ccb76a 1120 /* Check if we can deliver an external or decrementer interrupt now */
e3bbbbfa
PM
1121 ld r0, VCPU_PENDING_EXC(r4)
1122 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1123 cmpdi cr1, r0, 0
1124 andi. r8, r11, MSR_EE
e3bbbbfa
PM
1125 mfspr r8, SPRN_LPCR
1126 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1127 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1128 mtspr SPRN_LPCR, r8
19ccb76a 1129 isync
19ccb76a 1130 beq 5f
e3bbbbfa
PM
1131 li r0, BOOK3S_INTERRUPT_EXTERNAL
1132 bne cr1, 12f
1133 mfspr r0, SPRN_DEC
1bc3fe81
PM
1134BEGIN_FTR_SECTION
1135 /* On POWER9 check whether the guest has large decrementer enabled */
1136 andis. r8, r8, LPCR_LD@h
1137 bne 15f
1138END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1139 extsw r0, r0
114015: cmpdi r0, 0
e3bbbbfa
PM
1141 li r0, BOOK3S_INTERRUPT_DECREMENTER
1142 bge 5f
19ccb76a 1143
e3bbbbfa 114412: mtspr SPRN_SRR0, r10
19ccb76a 1145 mr r10,r0
e3bbbbfa 1146 mtspr SPRN_SRR1, r11
e4e38121
MN
1147 mr r9, r4
1148 bl kvmppc_msr_interrupt
e3bbbbfa 11495:
57900694
PM
1150BEGIN_FTR_SECTION
1151 b fast_guest_return
1152END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1153 /* On POWER9, check for pending doorbell requests */
1154 lbz r0, VCPU_DBELL_REQ(r4)
1155 cmpwi r0, 0
1156 beq fast_guest_return
1157 ld r5, HSTATE_KVM_VCORE(r13)
1158 /* Set DPDES register so the CPU will take a doorbell interrupt */
1159 li r0, 1
1160 mtspr SPRN_DPDES, r0
1161 std r0, VCORE_DPDES(r5)
1162 /* Make sure other cpus see vcore->dpdes set before dbell req clear */
1163 lwsync
1164 /* Clear the pending doorbell request */
1165 li r0, 0
1166 stb r0, VCPU_DBELL_REQ(r4)
19ccb76a 1167
27025a60
LPF
1168/*
1169 * Required state:
1170 * R4 = vcpu
1171 * R10: value for HSRR0
1172 * R11: value for HSRR1
1173 * R13 = PACA
1174 */
de56a948 1175fast_guest_return:
4619ac88
PM
1176 li r0,0
1177 stb r0,VCPU_CEDED(r4) /* cancel cede */
de56a948
PM
1178 mtspr SPRN_HSRR0,r10
1179 mtspr SPRN_HSRR1,r11
1180
1181 /* Activate guest mode, so faults get handled by KVM */
44a3add8 1182 li r9, KVM_GUEST_MODE_GUEST_HV
de56a948
PM
1183 stb r9, HSTATE_IN_GUEST(r13)
1184
b6c295df
PM
1185#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1186 /* Accumulate timing */
1187 addi r3, r4, VCPU_TB_GUEST
1188 bl kvmhv_accumulate_time
1189#endif
1190
de56a948
PM
1191 /* Enter guest */
1192
0acb9111
PM
1193BEGIN_FTR_SECTION
1194 ld r5, VCPU_CFAR(r4)
1195 mtspr SPRN_CFAR, r5
1196END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
1197BEGIN_FTR_SECTION
1198 ld r0, VCPU_PPR(r4)
1199END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
0acb9111 1200
de56a948
PM
1201 ld r5, VCPU_LR(r4)
1202 lwz r6, VCPU_CR(r4)
1203 mtlr r5
1204 mtcr r6
1205
c75df6f9
MN
1206 ld r1, VCPU_GPR(R1)(r4)
1207 ld r2, VCPU_GPR(R2)(r4)
1208 ld r3, VCPU_GPR(R3)(r4)
1209 ld r5, VCPU_GPR(R5)(r4)
1210 ld r6, VCPU_GPR(R6)(r4)
1211 ld r7, VCPU_GPR(R7)(r4)
1212 ld r8, VCPU_GPR(R8)(r4)
1213 ld r9, VCPU_GPR(R9)(r4)
1214 ld r10, VCPU_GPR(R10)(r4)
1215 ld r11, VCPU_GPR(R11)(r4)
1216 ld r12, VCPU_GPR(R12)(r4)
1217 ld r13, VCPU_GPR(R13)(r4)
1218
4b8473c9
PM
1219BEGIN_FTR_SECTION
1220 mtspr SPRN_PPR, r0
1221END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
e001fa78
MN
1222
1223/* Move canary into DSISR to check for later */
1224BEGIN_FTR_SECTION
1225 li r0, 0x7fff
1226 mtspr SPRN_HDSISR, r0
1227END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1228
4b8473c9 1229 ld r0, VCPU_GPR(R0)(r4)
c75df6f9 1230 ld r4, VCPU_GPR(R4)(r4)
222f20f1 1231 HRFI_TO_GUEST
de56a948
PM
1232 b .
1233
b6c295df 1234secondary_too_late:
6af27c84 1235 li r12, 0
a8b48a4d 1236 stw r12, STACK_SLOT_TRAP(r1)
b6c295df
PM
1237 cmpdi r4, 0
1238 beq 11f
6af27c84
PM
1239 stw r12, VCPU_TRAP(r4)
1240#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
b6c295df
PM
1241 addi r3, r4, VCPU_TB_RMEXIT
1242 bl kvmhv_accumulate_time
6af27c84 1243#endif
b6c295df
PM
124411: b kvmhv_switch_to_host
1245
b4deba5c
PM
1246no_switch_exit:
1247 HMT_MEDIUM
1248 li r12, 0
1249 b 12f
b6c295df 1250hdec_soon:
6af27c84 1251 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
b4deba5c 125212: stw r12, VCPU_TRAP(r4)
6af27c84
PM
1253 mr r9, r4
1254#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
b6c295df
PM
1255 addi r3, r4, VCPU_TB_RMEXIT
1256 bl kvmhv_accumulate_time
b6c295df 1257#endif
6964e6a4 1258 b guest_bypass
b6c295df 1259
de56a948
PM
1260/******************************************************************************
1261 * *
1262 * Exit code *
1263 * *
1264 *****************************************************************************/
1265
1266/*
1267 * We come here from the first-level interrupt handlers.
1268 */
dd96b2c2
AK
1269 .globl kvmppc_interrupt_hv
1270kvmppc_interrupt_hv:
de56a948
PM
1271 /*
1272 * Register contents:
d3918e7f 1273 * R12 = (guest CR << 32) | interrupt vector
de56a948 1274 * R13 = PACA
d3918e7f 1275 * guest R12 saved in shadow VCPU SCRATCH0
a97a65d5 1276 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
de56a948
PM
1277 * guest R13 saved in SPRN_SCRATCH0
1278 */
a97a65d5 1279 std r9, HSTATE_SCRATCH2(r13)
44a3add8
PM
1280 lbz r9, HSTATE_IN_GUEST(r13)
1281 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1282 beq kvmppc_bad_host_intr
dd96b2c2
AK
1283#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1284 cmpwi r9, KVM_GUEST_MODE_GUEST
a97a65d5 1285 ld r9, HSTATE_SCRATCH2(r13)
dd96b2c2
AK
1286 beq kvmppc_interrupt_pr
1287#endif
44a3add8
PM
1288 /* We're now back in the host but in guest MMU context */
1289 li r9, KVM_GUEST_MODE_HOST_HV
1290 stb r9, HSTATE_IN_GUEST(r13)
1291
de56a948
PM
1292 ld r9, HSTATE_KVM_VCPU(r13)
1293
1294 /* Save registers */
1295
c75df6f9
MN
1296 std r0, VCPU_GPR(R0)(r9)
1297 std r1, VCPU_GPR(R1)(r9)
1298 std r2, VCPU_GPR(R2)(r9)
1299 std r3, VCPU_GPR(R3)(r9)
1300 std r4, VCPU_GPR(R4)(r9)
1301 std r5, VCPU_GPR(R5)(r9)
1302 std r6, VCPU_GPR(R6)(r9)
1303 std r7, VCPU_GPR(R7)(r9)
1304 std r8, VCPU_GPR(R8)(r9)
a97a65d5 1305 ld r0, HSTATE_SCRATCH2(r13)
c75df6f9
MN
1306 std r0, VCPU_GPR(R9)(r9)
1307 std r10, VCPU_GPR(R10)(r9)
1308 std r11, VCPU_GPR(R11)(r9)
de56a948 1309 ld r3, HSTATE_SCRATCH0(r13)
c75df6f9 1310 std r3, VCPU_GPR(R12)(r9)
d3918e7f
NP
1311 /* CR is in the high half of r12 */
1312 srdi r4, r12, 32
de56a948 1313 stw r4, VCPU_CR(r9)
0acb9111
PM
1314BEGIN_FTR_SECTION
1315 ld r3, HSTATE_CFAR(r13)
1316 std r3, VCPU_CFAR(r9)
1317END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
1318BEGIN_FTR_SECTION
1319 ld r4, HSTATE_PPR(r13)
1320 std r4, VCPU_PPR(r9)
1321END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
de56a948
PM
1322
1323 /* Restore R1/R2 so we can handle faults */
1324 ld r1, HSTATE_HOST_R1(r13)
1325 ld r2, PACATOC(r13)
1326
1327 mfspr r10, SPRN_SRR0
1328 mfspr r11, SPRN_SRR1
1329 std r10, VCPU_SRR0(r9)
1330 std r11, VCPU_SRR1(r9)
d3918e7f
NP
1331 /* trap is in the low half of r12, clear CR from the high half */
1332 clrldi r12, r12, 32
de56a948
PM
1333 andi. r0, r12, 2 /* need to read HSRR0/1? */
1334 beq 1f
1335 mfspr r10, SPRN_HSRR0
1336 mfspr r11, SPRN_HSRR1
1337 clrrdi r12, r12, 2
13381: std r10, VCPU_PC(r9)
1339 std r11, VCPU_MSR(r9)
1340
1341 GET_SCRATCH0(r3)
1342 mflr r4
c75df6f9 1343 std r3, VCPU_GPR(R13)(r9)
de56a948
PM
1344 std r4, VCPU_LR(r9)
1345
de56a948
PM
1346 stw r12,VCPU_TRAP(r9)
1347
8b24e69f
PM
1348 /*
1349 * Now that we have saved away SRR0/1 and HSRR0/1,
1350 * interrupts are recoverable in principle, so set MSR_RI.
1351 * This becomes important for relocation-on interrupts from
1352 * the guest, which we can get in radix mode on POWER9.
1353 */
1354 li r0, MSR_RI
1355 mtmsrd r0, 1
1356
b6c295df
PM
1357#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1358 addi r3, r9, VCPU_TB_RMINTR
1359 mr r4, r9
1360 bl kvmhv_accumulate_time
1361 ld r5, VCPU_GPR(R5)(r9)
1362 ld r6, VCPU_GPR(R6)(r9)
1363 ld r7, VCPU_GPR(R7)(r9)
1364 ld r8, VCPU_GPR(R8)(r9)
1365#endif
1366
4a157d61 1367 /* Save HEIR (HV emulation assist reg) in emul_inst
697d3899
PM
1368 if this is an HEI (HV emulation interrupt, e40) */
1369 li r3,KVM_INST_FETCH_FAILED
2bf27601 1370 stw r3,VCPU_LAST_INST(r9)
697d3899
PM
1371 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1372 bne 11f
1373 mfspr r3,SPRN_HEIR
4a157d61 137411: stw r3,VCPU_HEIR(r9)
697d3899
PM
1375
1376 /* these are volatile across C function calls */
a97a65d5
NP
1377#ifdef CONFIG_RELOCATABLE
1378 ld r3, HSTATE_SCRATCH1(r13)
1379 mtctr r3
1380#else
697d3899 1381 mfctr r3
a97a65d5 1382#endif
697d3899
PM
1383 mfxer r4
1384 std r3, VCPU_CTR(r9)
c63517c2 1385 std r4, VCPU_XER(r9)
697d3899 1386
4bb3c7a0
PM
1387#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1388 /* For softpatch interrupt, go off and do TM instruction emulation */
1389 cmpwi r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
1390 beq kvmppc_tm_emul
1391#endif
1392
697d3899
PM
1393 /* If this is a page table miss then see if it's theirs or ours */
1394 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1395 beq kvmppc_hdsi
342d3db7
PM
1396 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1397 beq kvmppc_hisi
697d3899 1398
de56a948
PM
1399 /* See if this is a leftover HDEC interrupt */
1400 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1401 bne 2f
1402 mfspr r3,SPRN_HDEC
a4faf2e7
PM
1403 EXTEND_HDEC(r3)
1404 cmpdi r3,0
1f09c3ed
PM
1405 mr r4,r9
1406 bge fast_guest_return
de56a948 14072:
697d3899 1408 /* See if this is an hcall we can handle in real mode */
a8606e20
PM
1409 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1410 beq hcall_try_real_mode
de56a948 1411
66feed61
PM
1412 /* Hypervisor doorbell - exit only if host IPI flag set */
1413 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1414 bne 3f
bd0fdb19
NP
1415BEGIN_FTR_SECTION
1416 PPC_MSGSYNC
2cde3716 1417 lwsync
bd0fdb19 1418END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
66feed61 1419 lbz r0, HSTATE_HOST_IPI(r13)
06554d9f 1420 cmpwi r0, 0
66feed61
PM
1421 beq 4f
1422 b guest_exit_cont
14233:
769377f7
PM
1424 /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1425 cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1426 bne 14f
1427 mfspr r3, SPRN_HFSCR
1428 std r3, VCPU_HFSCR(r9)
1429 b guest_exit_cont
143014:
54695c30
BH
1431 /* External interrupt ? */
1432 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1f09c3ed 1433 bne+ guest_exit_cont
54695c30
BH
1434
1435 /* External interrupt, first check for host_ipi. If this is
1436 * set, we know the host wants us out so let's do it now
1437 */
c934243c 1438 bl kvmppc_read_intr
37f55d30
SW
1439
1440 /*
1441 * Restore the active volatile registers after returning from
1442 * a C function.
1443 */
1444 ld r9, HSTATE_KVM_VCPU(r13)
1445 li r12, BOOK3S_INTERRUPT_EXTERNAL
1446
1447 /*
1448 * kvmppc_read_intr return codes:
1449 *
1450 * Exit to host (r3 > 0)
1451 * 1 An interrupt is pending that needs to be handled by the host
1452 * Exit guest and return to host by branching to guest_exit_cont
1453 *
f7af5209
SW
1454 * 2 Passthrough that needs completion in the host
1455 * Exit guest and return to host by branching to guest_exit_cont
1456 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1457 * to indicate to the host to complete handling the interrupt
1458 *
37f55d30
SW
1459 * Before returning to guest, we check if any CPU is heading out
1460 * to the host and if so, we head out also. If no CPUs are heading
1461 * check return values <= 0.
1462 *
1463 * Return to guest (r3 <= 0)
1464 * 0 No external interrupt is pending
1465 * -1 A guest wakeup IPI (which has now been cleared)
1466 * In either case, we return to guest to deliver any pending
1467 * guest interrupts.
e3c13e56
SW
1468 *
1469 * -2 A PCI passthrough external interrupt was handled
1470 * (interrupt was delivered directly to guest)
1471 * Return to guest to deliver any pending guest interrupts.
37f55d30
SW
1472 */
1473
f7af5209
SW
1474 cmpdi r3, 1
1475 ble 1f
1476
1477 /* Return code = 2 */
1478 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1479 stw r12, VCPU_TRAP(r9)
1480 b guest_exit_cont
1481
14821: /* Return code <= 1 */
c934243c 1483 cmpdi r3, 0
1f09c3ed 1484 bgt guest_exit_cont
54695c30 1485
37f55d30 1486 /* Return code <= 0 */
66feed61 14874: ld r5, HSTATE_KVM_VCORE(r13)
4619ac88
PM
1488 lwz r0, VCORE_ENTRY_EXIT(r5)
1489 cmpwi r0, 0x100
e3bbbbfa 1490 mr r4, r9
1f09c3ed 1491 blt deliver_guest_interrupt
de56a948 1492
b4072df4 1493guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
43ff3f65
PM
1494 /* Save more register state */
1495 mfdar r6
1496 mfdsisr r7
1497 std r6, VCPU_DAR(r9)
1498 stw r7, VCPU_DSISR(r9)
1499 /* don't overwrite fault_dar/fault_dsisr if HDSI */
1500 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1501 beq mc_cont
1502 std r6, VCPU_FAULT_DAR(r9)
1503 stw r7, VCPU_FAULT_DSISR(r9)
1504
1505 /* See if it is a machine check */
1506 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1507 beq machine_check_realmode
1508mc_cont:
1509#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1510 addi r3, r9, VCPU_TB_RMEXIT
1511 mr r4, r9
1512 bl kvmhv_accumulate_time
1513#endif
5af50993
BH
1514#ifdef CONFIG_KVM_XICS
1515 /* We are exiting, pull the VP from the XIVE */
35c2405e 1516 lbz r0, VCPU_XIVE_PUSHED(r9)
5af50993
BH
1517 cmpwi cr0, r0, 0
1518 beq 1f
1519 li r7, TM_SPC_PULL_OS_CTX
1520 li r6, TM_QW1_OS
1521 mfmsr r0
2662efd0 1522 andi. r0, r0, MSR_DR /* in real mode? */
5af50993
BH
1523 beq 2f
1524 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1525 cmpldi cr0, r10, 0
1526 beq 1f
1527 /* First load to pull the context, we ignore the value */
5af50993 1528 eieio
ad98dd1a 1529 lwzx r11, r7, r10
5af50993
BH
1530 /* Second load to recover the context state (Words 0 and 1) */
1531 ldx r11, r6, r10
1532 b 3f
15332: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1534 cmpldi cr0, r10, 0
1535 beq 1f
1536 /* First load to pull the context, we ignore the value */
5af50993 1537 eieio
ad98dd1a 1538 lwzcix r11, r7, r10
5af50993
BH
1539 /* Second load to recover the context state (Words 0 and 1) */
1540 ldcix r11, r6, r10
15413: std r11, VCPU_XIVE_SAVED_STATE(r9)
1542 /* Fixup some of the state for the next load */
1543 li r10, 0
1544 li r0, 0xff
35c2405e 1545 stb r10, VCPU_XIVE_PUSHED(r9)
5af50993
BH
1546 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1547 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
ad98dd1a 1548 eieio
5af50993
BH
15491:
1550#endif /* CONFIG_KVM_XICS */
de56a948 1551
6964e6a4 1552 /* For hash guest, read the guest SLB and save it away */
f4c51f84
PM
1553 ld r5, VCPU_KVM(r9)
1554 lbz r0, KVM_RADIX(r5)
f4c51f84 1555 li r5, 0
6964e6a4
PM
1556 cmpwi r0, 0
1557 bne 3f /* for radix, save 0 entries */
de56a948
PM
1558 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1559 mtctr r0
1560 li r6,0
1561 addi r7,r9,VCPU_SLB
de56a948
PM
15621: slbmfee r8,r6
1563 andis. r0,r8,SLB_ESID_V@h
1564 beq 2f
1565 add r8,r8,r6 /* put index in */
1566 slbmfev r3,r6
1567 std r8,VCPU_SLB_E(r7)
1568 std r3,VCPU_SLB_V(r7)
1569 addi r7,r7,VCPU_SLB_SIZE
1570 addi r5,r5,1
15712: addi r6,r6,1
1572 bdnz 1b
6964e6a4
PM
1573 /* Finally clear out the SLB */
1574 li r0,0
1575 slbmte r0,r0
1576 slbia
1577 ptesync
f4c51f84 15783: stw r5,VCPU_SLB_MAX(r9)
b4072df4 1579
cda4a147
PM
1580 /* load host SLB entries */
1581BEGIN_MMU_FTR_SECTION
1582 b 0f
1583END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
1584 ld r8,PACA_SLBSHADOWPTR(r13)
1585
1586 .rept SLB_NUM_BOLTED
1587 li r3, SLBSHADOW_SAVEAREA
1588 LDX_BE r5, r8, r3
1589 addi r3, r3, 8
1590 LDX_BE r6, r8, r3
1591 andis. r7,r5,SLB_ESID_V@h
1592 beq 1f
1593 slbmte r6,r5
15941: addi r8,r8,16
1595 .endr
15960:
1597
6964e6a4 1598guest_bypass:
a8b48a4d 1599 stw r12, STACK_SLOT_TRAP(r1)
57b8daa7
PM
1600
1601 /* Save DEC */
1602 /* Do this before kvmhv_commence_exit so we know TB is guest TB */
1603 ld r3, HSTATE_KVM_VCORE(r13)
1604 mfspr r5,SPRN_DEC
1605 mftb r6
1606 /* On P9, if the guest has large decr enabled, don't sign extend */
1607BEGIN_FTR_SECTION
1608 ld r4, VCORE_LPCR(r3)
1609 andis. r4, r4, LPCR_LD@h
1610 bne 16f
1611END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1612 extsw r5,r5
161316: add r5,r5,r6
1614 /* r5 is a guest timebase value here, convert to host TB */
1615 ld r4,VCORE_TB_OFFSET_APPL(r3)
1616 subf r5,r4,r5
1617 std r5,VCPU_DEC_EXPIRES(r9)
1618
6af27c84 1619 /* Increment exit count, poke other threads to exit */
57b8daa7 1620 mr r3, r12
6af27c84 1621 bl kvmhv_commence_exit
eddb60fb
PM
1622 nop
1623 ld r9, HSTATE_KVM_VCPU(r13)
6af27c84 1624
ec257165
PM
1625 /* Stop others sending VCPU interrupts to this physical CPU */
1626 li r0, -1
1627 stw r0, VCPU_CPU(r9)
1628 stw r0, VCPU_THREAD_CPU(r9)
1629
de56a948 1630 /* Save guest CTRL register, set runlatch to 1 */
6af27c84 1631 mfspr r6,SPRN_CTRLF
de56a948
PM
1632 stw r6,VCPU_CTRL(r9)
1633 andi. r0,r6,1
1634 bne 4f
1635 ori r6,r6,1
1636 mtspr SPRN_CTRLT,r6
16374:
de56a948
PM
1638 /*
1639 * Save the guest PURR/SPURR
1640 */
1641 mfspr r5,SPRN_PURR
1642 mfspr r6,SPRN_SPURR
1643 ld r7,VCPU_PURR(r9)
1644 ld r8,VCPU_SPURR(r9)
1645 std r5,VCPU_PURR(r9)
1646 std r6,VCPU_SPURR(r9)
1647 subf r5,r7,r5
1648 subf r6,r8,r6
1649
1650 /*
1651 * Restore host PURR/SPURR and add guest times
1652 * so that the time in the guest gets accounted.
1653 */
1654 ld r3,HSTATE_PURR(r13)
1655 ld r4,HSTATE_SPURR(r13)
1656 add r3,r3,r5
1657 add r4,r4,r6
1658 mtspr SPRN_PURR,r3
1659 mtspr SPRN_SPURR,r4
1660
b005255e
MN
1661BEGIN_FTR_SECTION
1662 b 8f
1663END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
b005255e
MN
1664 /* Save POWER8-specific registers */
1665 mfspr r5, SPRN_IAMR
1666 mfspr r6, SPRN_PSPB
1667 mfspr r7, SPRN_FSCR
1668 std r5, VCPU_IAMR(r9)
1669 stw r6, VCPU_PSPB(r9)
1670 std r7, VCPU_FSCR(r9)
1671 mfspr r5, SPRN_IC
b005255e
MN
1672 mfspr r7, SPRN_TAR
1673 std r5, VCPU_IC(r9)
b005255e 1674 std r7, VCPU_TAR(r9)
7b490411 1675 mfspr r8, SPRN_EBBHR
b005255e
MN
1676 std r8, VCPU_EBBHR(r9)
1677 mfspr r5, SPRN_EBBRR
1678 mfspr r6, SPRN_BESCR
83677f55
PM
1679 mfspr r7, SPRN_PID
1680 mfspr r8, SPRN_WORT
b005255e
MN
1681 std r5, VCPU_EBBRR(r9)
1682 std r6, VCPU_BESCR(r9)
83677f55
PM
1683 stw r7, VCPU_GUEST_PID(r9)
1684 std r8, VCPU_WORT(r9)
1685BEGIN_FTR_SECTION
b005255e
MN
1686 mfspr r5, SPRN_TCSCR
1687 mfspr r6, SPRN_ACOP
83677f55
PM
1688 mfspr r7, SPRN_CSIGR
1689 mfspr r8, SPRN_TACR
b005255e
MN
1690 std r5, VCPU_TCSCR(r9)
1691 std r6, VCPU_ACOP(r9)
83677f55
PM
1692 std r7, VCPU_CSIGR(r9)
1693 std r8, VCPU_TACR(r9)
e9cf1e08
PM
1694FTR_SECTION_ELSE
1695 mfspr r5, SPRN_TIDR
1696 mfspr r6, SPRN_PSSCR
1697 std r5, VCPU_TID(r9)
1698 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1699 rotldi r6, r6, 60
1700 std r6, VCPU_PSSCR(r9)
769377f7
PM
1701 /* Restore host HFSCR value */
1702 ld r7, STACK_SLOT_HFSCR(r1)
1703 mtspr SPRN_HFSCR, r7
e9cf1e08 1704ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
ccec4456
PM
1705 /*
1706 * Restore various registers to 0, where non-zero values
1707 * set by the guest could disrupt the host.
1708 */
1709 li r0, 0
4c3bb4cc 1710 mtspr SPRN_PSPB, r0
ccec4456 1711 mtspr SPRN_WORT, r0
83677f55 1712BEGIN_FTR_SECTION
4c3bb4cc 1713 mtspr SPRN_IAMR, r0
83677f55 1714 mtspr SPRN_TCSCR, r0
ccec4456
PM
1715 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1716 li r0, 1
1717 sldi r0, r0, 31
1718 mtspr SPRN_MMCRS, r0
83677f55 1719END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
b005255e
MN
17208:
1721
e0b7ec05 1722 /* Save and reset AMR and UAMOR before turning on the MMU */
e0b7ec05
PM
1723 mfspr r5,SPRN_AMR
1724 mfspr r6,SPRN_UAMOR
1725 std r5,VCPU_AMR(r9)
1726 std r6,VCPU_UAMOR(r9)
1727 li r6,0
1728 mtspr SPRN_AMR,r6
4c3bb4cc 1729 mtspr SPRN_UAMOR, r6
e0b7ec05
PM
1730
1731 /* Switch DSCR back to host value */
e0b7ec05
PM
1732 mfspr r8, SPRN_DSCR
1733 ld r7, HSTATE_DSCR(r13)
1734 std r8, VCPU_DSCR(r9)
1735 mtspr SPRN_DSCR, r7
e0b7ec05
PM
1736
1737 /* Save non-volatile GPRs */
1738 std r14, VCPU_GPR(R14)(r9)
1739 std r15, VCPU_GPR(R15)(r9)
1740 std r16, VCPU_GPR(R16)(r9)
1741 std r17, VCPU_GPR(R17)(r9)
1742 std r18, VCPU_GPR(R18)(r9)
1743 std r19, VCPU_GPR(R19)(r9)
1744 std r20, VCPU_GPR(R20)(r9)
1745 std r21, VCPU_GPR(R21)(r9)
1746 std r22, VCPU_GPR(R22)(r9)
1747 std r23, VCPU_GPR(R23)(r9)
1748 std r24, VCPU_GPR(R24)(r9)
1749 std r25, VCPU_GPR(R25)(r9)
1750 std r26, VCPU_GPR(R26)(r9)
1751 std r27, VCPU_GPR(R27)(r9)
1752 std r28, VCPU_GPR(R28)(r9)
1753 std r29, VCPU_GPR(R29)(r9)
1754 std r30, VCPU_GPR(R30)(r9)
1755 std r31, VCPU_GPR(R31)(r9)
1756
1757 /* Save SPRGs */
1758 mfspr r3, SPRN_SPRG0
1759 mfspr r4, SPRN_SPRG1
1760 mfspr r5, SPRN_SPRG2
1761 mfspr r6, SPRN_SPRG3
1762 std r3, VCPU_SPRG0(r9)
1763 std r4, VCPU_SPRG1(r9)
1764 std r5, VCPU_SPRG2(r9)
1765 std r6, VCPU_SPRG3(r9)
1766
1767 /* save FP state */
1768 mr r3, r9
1769 bl kvmppc_save_fp
de56a948 1770
0a8eccef 1771#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
4bb3c7a0
PM
1772/*
1773 * Branch around the call if both CPU_FTR_TM and
1774 * CPU_FTR_P9_TM_HV_ASSIST are off.
1775 */
0a8eccef 1776BEGIN_FTR_SECTION
4bb3c7a0
PM
1777 b 91f
1778END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
67f8a8c1
PM
1779 /*
1780 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
1781 */
f024ee09 1782 bl kvmppc_save_tm
4bb3c7a0 178391:
0a8eccef
PM
1784#endif
1785
e0b7ec05
PM
1786 /* Increment yield count if they have a VPA */
1787 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1788 cmpdi r8, 0
1789 beq 25f
0865a583
AG
1790 li r4, LPPACA_YIELDCOUNT
1791 LWZX_BE r3, r8, r4
e0b7ec05 1792 addi r3, r3, 1
0865a583 1793 STWX_BE r3, r8, r4
e0b7ec05
PM
1794 li r3, 1
1795 stb r3, VCPU_VPA_DIRTY(r9)
179625:
1797 /* Save PMU registers if requested */
1798 /* r8 and cr0.eq are live here */
9bc01a9b
PM
1799BEGIN_FTR_SECTION
1800 /*
1801 * POWER8 seems to have a hardware bug where setting
1802 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1803 * when some counters are already negative doesn't seem
1804 * to cause a performance monitor alert (and hence interrupt).
1805 * The effect of this is that when saving the PMU state,
1806 * if there is no PMU alert pending when we read MMCR0
1807 * before freezing the counters, but one becomes pending
1808 * before we read the counters, we lose it.
1809 * To work around this, we need a way to freeze the counters
1810 * before reading MMCR0. Normally, freezing the counters
1811 * is done by writing MMCR0 (to set MMCR0[FC]) which
1812 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1813 * we can also freeze the counters using MMCR2, by writing
1814 * 1s to all the counter freeze condition bits (there are
1815 * 9 bits each for 6 counters).
1816 */
1817 li r3, -1 /* set all freeze bits */
1818 clrrdi r3, r3, 10
1819 mfspr r10, SPRN_MMCR2
1820 mtspr SPRN_MMCR2, r3
1821 isync
1822END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
1823 li r3, 1
1824 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1825 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1826 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1827 mfspr r6, SPRN_MMCRA
c17b98cf 1828 /* Clear MMCRA in order to disable SDAR updates */
e0b7ec05
PM
1829 li r7, 0
1830 mtspr SPRN_MMCRA, r7
e0b7ec05
PM
1831 isync
1832 beq 21f /* if no VPA, save PMU stuff anyway */
1833 lbz r7, LPPACA_PMCINUSE(r8)
1834 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1835 bne 21f
1836 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1837 b 22f
183821: mfspr r5, SPRN_MMCR1
1839 mfspr r7, SPRN_SIAR
1840 mfspr r8, SPRN_SDAR
1841 std r4, VCPU_MMCR(r9)
1842 std r5, VCPU_MMCR + 8(r9)
1843 std r6, VCPU_MMCR + 16(r9)
9bc01a9b
PM
1844BEGIN_FTR_SECTION
1845 std r10, VCPU_MMCR + 24(r9)
1846END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
1847 std r7, VCPU_SIAR(r9)
1848 std r8, VCPU_SDAR(r9)
1849 mfspr r3, SPRN_PMC1
1850 mfspr r4, SPRN_PMC2
1851 mfspr r5, SPRN_PMC3
1852 mfspr r6, SPRN_PMC4
1853 mfspr r7, SPRN_PMC5
1854 mfspr r8, SPRN_PMC6
e0b7ec05
PM
1855 stw r3, VCPU_PMC(r9)
1856 stw r4, VCPU_PMC + 4(r9)
1857 stw r5, VCPU_PMC + 8(r9)
1858 stw r6, VCPU_PMC + 12(r9)
1859 stw r7, VCPU_PMC + 16(r9)
1860 stw r8, VCPU_PMC + 20(r9)
b005255e 1861BEGIN_FTR_SECTION
b005255e 1862 mfspr r5, SPRN_SIER
83677f55
PM
1863 std r5, VCPU_SIER(r9)
1864BEGIN_FTR_SECTION_NESTED(96)
b005255e
MN
1865 mfspr r6, SPRN_SPMC1
1866 mfspr r7, SPRN_SPMC2
1867 mfspr r8, SPRN_MMCRS
b005255e
MN
1868 stw r6, VCPU_PMC + 24(r9)
1869 stw r7, VCPU_PMC + 28(r9)
1870 std r8, VCPU_MMCR + 32(r9)
1871 lis r4, 0x8000
1872 mtspr SPRN_MMCRS, r4
83677f55 1873END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
b005255e 1874END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05 187522:
de56a948 1876
e9cf1e08 1877 /* Restore host values of some registers */
7ceaa6dc
PM
1878BEGIN_FTR_SECTION
1879 ld r5, STACK_SLOT_CIABR(r1)
1880 ld r6, STACK_SLOT_DAWR(r1)
1881 ld r7, STACK_SLOT_DAWRX(r1)
1882 mtspr SPRN_CIABR, r5
b53221e7
MN
1883 /*
1884 * If the DAWR doesn't work, it's ok to write these here as
1885 * this value should always be zero
1886 */
7ceaa6dc
PM
1887 mtspr SPRN_DAWR, r6
1888 mtspr SPRN_DAWRX, r7
1889END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e9cf1e08
PM
1890BEGIN_FTR_SECTION
1891 ld r5, STACK_SLOT_TID(r1)
1892 ld r6, STACK_SLOT_PSSCR(r1)
f4c51f84 1893 ld r7, STACK_SLOT_PID(r1)
4c3bb4cc 1894 ld r8, STACK_SLOT_IAMR(r1)
e9cf1e08
PM
1895 mtspr SPRN_TIDR, r5
1896 mtspr SPRN_PSSCR, r6
f4c51f84 1897 mtspr SPRN_PID, r7
4c3bb4cc 1898 mtspr SPRN_IAMR, r8
e9cf1e08 1899END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
a25bd72b
BH
1900
1901#ifdef CONFIG_PPC_RADIX_MMU
1902 /*
1903 * Are we running hash or radix ?
1904 */
67f8a8c1
PM
1905 ld r5, VCPU_KVM(r9)
1906 lbz r0, KVM_RADIX(r5)
1907 cmpwi cr2, r0, 0
6964e6a4 1908 beq cr2, 4f
a25bd72b 1909
df158189
PM
1910 /*
1911 * Radix: do eieio; tlbsync; ptesync sequence in case we
1912 * interrupted the guest between a tlbie and a ptesync.
1913 */
1914 eieio
1915 tlbsync
1916 ptesync
1917
a25bd72b
BH
1918 /* Radix: Handle the case where the guest used an illegal PID */
1919 LOAD_REG_ADDR(r4, mmu_base_pid)
1920 lwz r3, VCPU_GUEST_PID(r9)
1921 lwz r5, 0(r4)
1922 cmpw cr0,r3,r5
1923 blt 2f
1924
1925 /*
1926 * Illegal PID, the HW might have prefetched and cached in the TLB
1927 * some translations for the LPID 0 / guest PID combination which
1928 * Linux doesn't know about, so we need to flush that PID out of
1929 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1930 * the right context.
1931 */
1932 li r0,0
1933 mtspr SPRN_LPID,r0
1934 isync
1935
1936 /* Then do a congruence class local flush */
1937 ld r6,VCPU_KVM(r9)
1938 lwz r0,KVM_TLB_SETS(r6)
1939 mtctr r0
1940 li r7,0x400 /* IS field = 0b01 */
1941 ptesync
1942 sldi r0,r3,32 /* RS has PID */
19431: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
1944 addi r7,r7,0x1000
1945 bdnz 1b
1946 ptesync
1947
19482: /* Flush the ERAT on radix P9 DD1 guest exit */
f11f6f79
PM
1949BEGIN_FTR_SECTION
1950 PPC_INVALIDATE_ERAT
1951END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
6964e6a4 19524:
a25bd72b 1953#endif /* CONFIG_PPC_RADIX_MMU */
e9cf1e08 1954
9e368f29 1955 /*
c17b98cf 1956 * POWER7/POWER8 guest -> host partition switch code.
9e368f29
PM
1957 * We don't have to lock against tlbies but we do
1958 * have to coordinate the hardware threads.
a8b48a4d 1959 * Here STACK_SLOT_TRAP(r1) contains the trap number.
9e368f29 1960 */
b6c295df 1961kvmhv_switch_to_host:
371fefd6 1962 /* Secondary threads wait for primary to do partition switch */
6af27c84 1963 ld r5,HSTATE_KVM_VCORE(r13)
e0b7ec05
PM
1964 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1965 lbz r3,HSTATE_PTID(r13)
371fefd6
PM
1966 cmpwi r3,0
1967 beq 15f
1968 HMT_LOW
196913: lbz r3,VCORE_IN_GUEST(r5)
1970 cmpwi r3,0
1971 bne 13b
1972 HMT_MEDIUM
1973 b 16f
1974
1975 /* Primary thread waits for all the secondaries to exit guest */
197615: lwz r3,VCORE_ENTRY_EXIT(r5)
b4deba5c 1977 rlwinm r0,r3,32-8,0xff
371fefd6
PM
1978 clrldi r3,r3,56
1979 cmpw r3,r0
1980 bne 15b
1981 isync
1982
b4deba5c
PM
1983 /* Did we actually switch to the guest at all? */
1984 lbz r6, VCORE_IN_GUEST(r5)
1985 cmpwi r6, 0
1986 beq 19f
1987
371fefd6 1988 /* Primary thread switches back to host partition */
de56a948 1989 lwz r7,KVM_HOST_LPID(r4)
7a84084c
PM
1990BEGIN_FTR_SECTION
1991 ld r6,KVM_HOST_SDR1(r4)
de56a948
PM
1992 li r8,LPID_RSVD /* switch to reserved LPID */
1993 mtspr SPRN_LPID,r8
1994 ptesync
7a84084c
PM
1995 mtspr SPRN_SDR1,r6 /* switch to host page table */
1996END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
de56a948
PM
1997 mtspr SPRN_LPID,r7
1998 isync
93b0f4dc 1999
b005255e 2000BEGIN_FTR_SECTION
88b02cf9 2001 /* DPDES and VTB are shared between threads */
b005255e 2002 mfspr r7, SPRN_DPDES
88b02cf9 2003 mfspr r8, SPRN_VTB
b005255e 2004 std r7, VCORE_DPDES(r5)
88b02cf9 2005 std r8, VCORE_VTB(r5)
b005255e
MN
2006 /* clear DPDES so we don't get guest doorbells in the host */
2007 li r8, 0
2008 mtspr SPRN_DPDES, r8
2009END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2010
fd7bacbc 2011 /* If HMI, call kvmppc_realmode_hmi_handler() */
a8b48a4d 2012 lwz r12, STACK_SLOT_TRAP(r1)
fd7bacbc
MS
2013 cmpwi r12, BOOK3S_INTERRUPT_HMI
2014 bne 27f
2015 bl kvmppc_realmode_hmi_handler
2016 nop
d075745d 2017 cmpdi r3, 0
fd7bacbc 2018 /*
d075745d
PM
2019 * At this point kvmppc_realmode_hmi_handler may have resync-ed
2020 * the TB, and if it has, we must not subtract the guest timebase
2021 * offset from the timebase. So, skip it.
fd7bacbc
MS
2022 *
2023 * Also, do not call kvmppc_subcore_exit_guest() because it has
2024 * been invoked as part of kvmppc_realmode_hmi_handler().
2025 */
d075745d 2026 beq 30f
fd7bacbc
MS
2027
202827:
93b0f4dc 2029 /* Subtract timebase offset from timebase */
57b8daa7 2030 ld r8, VCORE_TB_OFFSET_APPL(r5)
93b0f4dc
PM
2031 cmpdi r8,0
2032 beq 17f
57b8daa7
PM
2033 li r0, 0
2034 std r0, VCORE_TB_OFFSET_APPL(r5)
c5fb80d3 2035 mftb r6 /* current guest timebase */
93b0f4dc
PM
2036 subf r8,r8,r6
2037 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
2038 mftb r7 /* check if lower 24 bits overflowed */
2039 clrldi r6,r6,40
2040 clrldi r7,r7,40
2041 cmpld r7,r6
2042 bge 17f
2043 addis r8,r8,0x100 /* if so, increment upper 40 bits */
2044 mtspr SPRN_TBU40,r8
2045
fd7bacbc
MS
204617: bl kvmppc_subcore_exit_guest
2047 nop
204830: ld r5,HSTATE_KVM_VCORE(r13)
2049 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
2050
388cc6e1 2051 /* Reset PCR */
fd7bacbc 2052 ld r0, VCORE_PCR(r5)
388cc6e1
PM
2053 cmpdi r0, 0
2054 beq 18f
2055 li r0, 0
2056 mtspr SPRN_PCR, r0
205718:
93b0f4dc 2058 /* Signal secondary CPUs to continue */
371fefd6 2059 stb r0,VCORE_IN_GUEST(r5)
b4deba5c 206019: lis r8,0x7fff /* MAX_INT@h */
de56a948
PM
2061 mtspr SPRN_HDEC,r8
2062
c0101509
PM
206316:
2064BEGIN_FTR_SECTION
2065 /* On POWER9 with HPT-on-radix we need to wait for all other threads */
2066 ld r3, HSTATE_SPLIT_MODE(r13)
2067 cmpdi r3, 0
2068 beq 47f
2069 lwz r8, KVM_SPLIT_DO_RESTORE(r3)
2070 cmpwi r8, 0
2071 beq 47f
c0101509
PM
2072 bl kvmhv_p9_restore_lpcr
2073 nop
c0101509
PM
2074 b 48f
207547:
2076END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2077 ld r8,KVM_HOST_LPCR(r4)
de56a948
PM
2078 mtspr SPRN_LPCR,r8
2079 isync
c0101509 208048:
b6c295df
PM
2081#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2082 /* Finish timing, if we have a vcpu */
2083 ld r4, HSTATE_KVM_VCPU(r13)
2084 cmpdi r4, 0
2085 li r3, 0
2086 beq 2f
2087 bl kvmhv_accumulate_time
20882:
2089#endif
44a3add8
PM
2090 /* Unset guest mode */
2091 li r0, KVM_GUEST_MODE_NONE
2092 stb r0, HSTATE_IN_GUEST(r13)
2093
a8b48a4d 2094 lwz r12, STACK_SLOT_TRAP(r1) /* return trap # in r12 */
7ceaa6dc
PM
2095 ld r0, SFS+PPC_LR_STKOFF(r1)
2096 addi r1, r1, SFS
218309b7
PM
2097 mtlr r0
2098 blr
b4072df4 2099
4bb3c7a0
PM
2100#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2101/*
2102 * Softpatch interrupt for transactional memory emulation cases
2103 * on POWER9 DD2.2. This is early in the guest exit path - we
2104 * haven't saved registers or done a treclaim yet.
2105 */
2106kvmppc_tm_emul:
2107 /* Save instruction image in HEIR */
2108 mfspr r3, SPRN_HEIR
2109 stw r3, VCPU_HEIR(r9)
2110
2111 /*
2112 * The cases we want to handle here are those where the guest
2113 * is in real suspend mode and is trying to transition to
2114 * transactional mode.
2115 */
2116 lbz r0, HSTATE_FAKE_SUSPEND(r13)
2117 cmpwi r0, 0 /* keep exiting guest if in fake suspend */
2118 bne guest_exit_cont
2119 rldicl r3, r11, 64 - MSR_TS_S_LG, 62
2120 cmpwi r3, 1 /* or if not in suspend state */
2121 bne guest_exit_cont
2122
2123 /* Call C code to do the emulation */
2124 mr r3, r9
2125 bl kvmhv_p9_tm_emulation_early
2126 nop
2127 ld r9, HSTATE_KVM_VCPU(r13)
2128 li r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
2129 cmpwi r3, 0
2130 beq guest_exit_cont /* continue exiting if not handled */
2131 ld r10, VCPU_PC(r9)
2132 ld r11, VCPU_MSR(r9)
2133 b fast_interrupt_c_return /* go back to guest if handled */
2134#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
2135
697d3899
PM
2136/*
2137 * Check whether an HDSI is an HPTE not found fault or something else.
2138 * If it is an HPTE not found fault that is due to the guest accessing
2139 * a page that they have mapped but which we have paged out, then
2140 * we continue on with the guest exit path. In all other cases,
2141 * reflect the HDSI to the guest as a DSI.
2142 */
2143kvmppc_hdsi:
f4c51f84
PM
2144 ld r3, VCPU_KVM(r9)
2145 lbz r0, KVM_RADIX(r3)
697d3899
PM
2146 mfspr r4, SPRN_HDAR
2147 mfspr r6, SPRN_HDSISR
e001fa78
MN
2148BEGIN_FTR_SECTION
2149 /* Look for DSISR canary. If we find it, retry instruction */
2150 cmpdi r6, 0x7fff
2151 beq 6f
2152END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2153 cmpwi r0, 0
f4c51f84 2154 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
4cf302bc
PM
2155 /* HPTE not found fault or protection fault? */
2156 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
697d3899 2157 beq 1f /* if not, send it to the guest */
4e5acdc2
PM
2158 andi. r0, r11, MSR_DR /* data relocation enabled? */
2159 beq 3f
ef8c640c
PM
2160BEGIN_FTR_SECTION
2161 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2162 b 4f
2163END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
697d3899 2164 clrrdi r0, r4, 28
c75df6f9 2165 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
cf29b215
PM
2166 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
2167 bne 7f /* if no SLB entry found */
697d3899
PM
21684: std r4, VCPU_FAULT_DAR(r9)
2169 stw r6, VCPU_FAULT_DSISR(r9)
2170
2171 /* Search the hash table. */
2172 mr r3, r9 /* vcpu pointer */
342d3db7 2173 li r7, 1 /* data fault */
b1576fec 2174 bl kvmppc_hpte_hv_fault
697d3899
PM
2175 ld r9, HSTATE_KVM_VCPU(r13)
2176 ld r10, VCPU_PC(r9)
2177 ld r11, VCPU_MSR(r9)
2178 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
2179 cmpdi r3, 0 /* retry the instruction */
2180 beq 6f
2181 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 2182 beq guest_exit_cont
697d3899
PM
2183 cmpdi r3, -2 /* MMIO emulation; need instr word */
2184 beq 2f
2185
cf29b215 2186 /* Synthesize a DSI (or DSegI) for the guest */
697d3899
PM
2187 ld r4, VCPU_FAULT_DAR(r9)
2188 mr r6, r3
cf29b215 21891: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
697d3899 2190 mtspr SPRN_DSISR, r6
cf29b215 21917: mtspr SPRN_DAR, r4
697d3899
PM
2192 mtspr SPRN_SRR0, r10
2193 mtspr SPRN_SRR1, r11
cf29b215 2194 mr r10, r0
e4e38121 2195 bl kvmppc_msr_interrupt
b4072df4 2196fast_interrupt_c_return:
697d3899 21976: ld r7, VCPU_CTR(r9)
c63517c2 2198 ld r8, VCPU_XER(r9)
697d3899
PM
2199 mtctr r7
2200 mtxer r8
2201 mr r4, r9
2202 b fast_guest_return
2203
22043: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
2205 ld r5, KVM_VRMA_SLB_V(r5)
2206 b 4b
2207
2208 /* If this is for emulated MMIO, load the instruction word */
22092: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
2210
2211 /* Set guest mode to 'jump over instruction' so if lwz faults
2212 * we'll just continue at the next IP. */
2213 li r0, KVM_GUEST_MODE_SKIP
2214 stb r0, HSTATE_IN_GUEST(r13)
2215
2216 /* Do the access with MSR:DR enabled */
2217 mfmsr r3
2218 ori r4, r3, MSR_DR /* Enable paging for data */
2219 mtmsrd r4
2220 lwz r8, 0(r10)
2221 mtmsrd r3
2222
2223 /* Store the result */
2224 stw r8, VCPU_LAST_INST(r9)
2225
2226 /* Unset guest mode. */
44a3add8 2227 li r0, KVM_GUEST_MODE_HOST_HV
697d3899 2228 stb r0, HSTATE_IN_GUEST(r13)
b4072df4 2229 b guest_exit_cont
de56a948 2230
f4c51f84
PM
2231.Lradix_hdsi:
2232 std r4, VCPU_FAULT_DAR(r9)
2233 stw r6, VCPU_FAULT_DSISR(r9)
2234.Lradix_hisi:
2235 mfspr r5, SPRN_ASDR
2236 std r5, VCPU_FAULT_GPA(r9)
2237 b guest_exit_cont
2238
342d3db7
PM
2239/*
2240 * Similarly for an HISI, reflect it to the guest as an ISI unless
2241 * it is an HPTE not found fault for a page that we have paged out.
2242 */
2243kvmppc_hisi:
f4c51f84
PM
2244 ld r3, VCPU_KVM(r9)
2245 lbz r0, KVM_RADIX(r3)
2246 cmpwi r0, 0
2247 bne .Lradix_hisi /* for radix, just save ASDR */
342d3db7
PM
2248 andis. r0, r11, SRR1_ISI_NOPT@h
2249 beq 1f
4e5acdc2
PM
2250 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
2251 beq 3f
ef8c640c
PM
2252BEGIN_FTR_SECTION
2253 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2254 b 4f
2255END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
342d3db7 2256 clrrdi r0, r10, 28
c75df6f9 2257 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
cf29b215
PM
2258 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
2259 bne 7f /* if no SLB entry found */
342d3db7
PM
22604:
2261 /* Search the hash table. */
2262 mr r3, r9 /* vcpu pointer */
2263 mr r4, r10
2264 mr r6, r11
2265 li r7, 0 /* instruction fault */
b1576fec 2266 bl kvmppc_hpte_hv_fault
342d3db7
PM
2267 ld r9, HSTATE_KVM_VCPU(r13)
2268 ld r10, VCPU_PC(r9)
2269 ld r11, VCPU_MSR(r9)
2270 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2271 cmpdi r3, 0 /* retry the instruction */
b4072df4 2272 beq fast_interrupt_c_return
342d3db7 2273 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 2274 beq guest_exit_cont
342d3db7 2275
cf29b215 2276 /* Synthesize an ISI (or ISegI) for the guest */
342d3db7 2277 mr r11, r3
cf29b215
PM
22781: li r0, BOOK3S_INTERRUPT_INST_STORAGE
22797: mtspr SPRN_SRR0, r10
342d3db7 2280 mtspr SPRN_SRR1, r11
cf29b215 2281 mr r10, r0
e4e38121 2282 bl kvmppc_msr_interrupt
b4072df4 2283 b fast_interrupt_c_return
342d3db7
PM
2284
22853: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2286 ld r5, KVM_VRMA_SLB_V(r6)
2287 b 4b
2288
a8606e20
PM
2289/*
2290 * Try to handle an hcall in real mode.
2291 * Returns to the guest if we handle it, or continues on up to
2292 * the kernel if we can't (i.e. if we don't have a handler for
2293 * it, or if the handler returns H_TOO_HARD).
1f09c3ed
PM
2294 *
2295 * r5 - r8 contain hcall args,
2296 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
a8606e20 2297 */
a8606e20 2298hcall_try_real_mode:
c75df6f9 2299 ld r3,VCPU_GPR(R3)(r9)
a8606e20 2300 andi. r0,r11,MSR_PR
27025a60
LPF
2301 /* sc 1 from userspace - reflect to guest syscall */
2302 bne sc_1_fast_return
a8606e20
PM
2303 clrrdi r3,r3,2
2304 cmpldi r3,hcall_real_table_end - hcall_real_table
b4072df4 2305 bge guest_exit_cont
699a0ea0
PM
2306 /* See if this hcall is enabled for in-kernel handling */
2307 ld r4, VCPU_KVM(r9)
2308 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2309 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2310 add r4, r4, r0
2311 ld r0, KVM_ENABLED_HCALLS(r4)
2312 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2313 srd r0, r0, r4
2314 andi. r0, r0, 1
2315 beq guest_exit_cont
2316 /* Get pointer to handler, if any, and call it */
a8606e20 2317 LOAD_REG_ADDR(r4, hcall_real_table)
4baa1d87 2318 lwax r3,r3,r4
a8606e20 2319 cmpwi r3,0
b4072df4 2320 beq guest_exit_cont
05a308c7
AB
2321 add r12,r3,r4
2322 mtctr r12
a8606e20 2323 mr r3,r9 /* get vcpu pointer */
c75df6f9 2324 ld r4,VCPU_GPR(R4)(r9)
a8606e20
PM
2325 bctrl
2326 cmpdi r3,H_TOO_HARD
2327 beq hcall_real_fallback
2328 ld r4,HSTATE_KVM_VCPU(r13)
c75df6f9 2329 std r3,VCPU_GPR(R3)(r4)
a8606e20
PM
2330 ld r10,VCPU_PC(r4)
2331 ld r11,VCPU_MSR(r4)
2332 b fast_guest_return
2333
27025a60
LPF
2334sc_1_fast_return:
2335 mtspr SPRN_SRR0,r10
2336 mtspr SPRN_SRR1,r11
2337 li r10, BOOK3S_INTERRUPT_SYSCALL
e4e38121 2338 bl kvmppc_msr_interrupt
27025a60
LPF
2339 mr r4,r9
2340 b fast_guest_return
2341
a8606e20
PM
2342 /* We've attempted a real mode hcall, but it's punted it back
2343 * to userspace. We need to restore some clobbered volatiles
2344 * before resuming the pass-it-to-qemu path */
2345hcall_real_fallback:
2346 li r12,BOOK3S_INTERRUPT_SYSCALL
2347 ld r9, HSTATE_KVM_VCPU(r13)
a8606e20 2348
b4072df4 2349 b guest_exit_cont
a8606e20
PM
2350
2351 .globl hcall_real_table
2352hcall_real_table:
2353 .long 0 /* 0 - unused */
c1fb0194
AB
2354 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2355 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2356 .long DOTSYM(kvmppc_h_read) - hcall_real_table
cdeee518
PM
2357 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2358 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
c1fb0194
AB
2359 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
2360 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
31217db7 2361 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
a8606e20 2362 .long 0 /* 0x24 - H_SET_SPRG0 */
c1fb0194 2363 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
a8606e20
PM
2364 .long 0 /* 0x2c */
2365 .long 0 /* 0x30 */
2366 .long 0 /* 0x34 */
2367 .long 0 /* 0x38 */
2368 .long 0 /* 0x3c */
2369 .long 0 /* 0x40 */
2370 .long 0 /* 0x44 */
2371 .long 0 /* 0x48 */
2372 .long 0 /* 0x4c */
2373 .long 0 /* 0x50 */
2374 .long 0 /* 0x54 */
2375 .long 0 /* 0x58 */
2376 .long 0 /* 0x5c */
2377 .long 0 /* 0x60 */
e7d26f28 2378#ifdef CONFIG_KVM_XICS
c1fb0194
AB
2379 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2380 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2381 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
5af50993 2382 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
c1fb0194 2383 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
e7d26f28
BH
2384#else
2385 .long 0 /* 0x64 - H_EOI */
2386 .long 0 /* 0x68 - H_CPPR */
2387 .long 0 /* 0x6c - H_IPI */
2388 .long 0 /* 0x70 - H_IPOLL */
2389 .long 0 /* 0x74 - H_XIRR */
2390#endif
a8606e20
PM
2391 .long 0 /* 0x78 */
2392 .long 0 /* 0x7c */
2393 .long 0 /* 0x80 */
2394 .long 0 /* 0x84 */
2395 .long 0 /* 0x88 */
2396 .long 0 /* 0x8c */
2397 .long 0 /* 0x90 */
2398 .long 0 /* 0x94 */
2399 .long 0 /* 0x98 */
2400 .long 0 /* 0x9c */
2401 .long 0 /* 0xa0 */
2402 .long 0 /* 0xa4 */
2403 .long 0 /* 0xa8 */
2404 .long 0 /* 0xac */
2405 .long 0 /* 0xb0 */
2406 .long 0 /* 0xb4 */
2407 .long 0 /* 0xb8 */
2408 .long 0 /* 0xbc */
2409 .long 0 /* 0xc0 */
2410 .long 0 /* 0xc4 */
2411 .long 0 /* 0xc8 */
2412 .long 0 /* 0xcc */
2413 .long 0 /* 0xd0 */
2414 .long 0 /* 0xd4 */
2415 .long 0 /* 0xd8 */
2416 .long 0 /* 0xdc */
c1fb0194 2417 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
90fd09f8 2418 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
a8606e20
PM
2419 .long 0 /* 0xe8 */
2420 .long 0 /* 0xec */
2421 .long 0 /* 0xf0 */
2422 .long 0 /* 0xf4 */
2423 .long 0 /* 0xf8 */
2424 .long 0 /* 0xfc */
2425 .long 0 /* 0x100 */
2426 .long 0 /* 0x104 */
2427 .long 0 /* 0x108 */
2428 .long 0 /* 0x10c */
2429 .long 0 /* 0x110 */
2430 .long 0 /* 0x114 */
2431 .long 0 /* 0x118 */
2432 .long 0 /* 0x11c */
2433 .long 0 /* 0x120 */
c1fb0194 2434 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
8563bf52
PM
2435 .long 0 /* 0x128 */
2436 .long 0 /* 0x12c */
2437 .long 0 /* 0x130 */
c1fb0194 2438 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
31217db7 2439 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
d3695aa4 2440 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
e928e9cb
ME
2441 .long 0 /* 0x140 */
2442 .long 0 /* 0x144 */
2443 .long 0 /* 0x148 */
2444 .long 0 /* 0x14c */
2445 .long 0 /* 0x150 */
2446 .long 0 /* 0x154 */
2447 .long 0 /* 0x158 */
2448 .long 0 /* 0x15c */
2449 .long 0 /* 0x160 */
2450 .long 0 /* 0x164 */
2451 .long 0 /* 0x168 */
2452 .long 0 /* 0x16c */
2453 .long 0 /* 0x170 */
2454 .long 0 /* 0x174 */
2455 .long 0 /* 0x178 */
2456 .long 0 /* 0x17c */
2457 .long 0 /* 0x180 */
2458 .long 0 /* 0x184 */
2459 .long 0 /* 0x188 */
2460 .long 0 /* 0x18c */
2461 .long 0 /* 0x190 */
2462 .long 0 /* 0x194 */
2463 .long 0 /* 0x198 */
2464 .long 0 /* 0x19c */
2465 .long 0 /* 0x1a0 */
2466 .long 0 /* 0x1a4 */
2467 .long 0 /* 0x1a8 */
2468 .long 0 /* 0x1ac */
2469 .long 0 /* 0x1b0 */
2470 .long 0 /* 0x1b4 */
2471 .long 0 /* 0x1b8 */
2472 .long 0 /* 0x1bc */
2473 .long 0 /* 0x1c0 */
2474 .long 0 /* 0x1c4 */
2475 .long 0 /* 0x1c8 */
2476 .long 0 /* 0x1cc */
2477 .long 0 /* 0x1d0 */
2478 .long 0 /* 0x1d4 */
2479 .long 0 /* 0x1d8 */
2480 .long 0 /* 0x1dc */
2481 .long 0 /* 0x1e0 */
2482 .long 0 /* 0x1e4 */
2483 .long 0 /* 0x1e8 */
2484 .long 0 /* 0x1ec */
2485 .long 0 /* 0x1f0 */
2486 .long 0 /* 0x1f4 */
2487 .long 0 /* 0x1f8 */
2488 .long 0 /* 0x1fc */
2489 .long 0 /* 0x200 */
2490 .long 0 /* 0x204 */
2491 .long 0 /* 0x208 */
2492 .long 0 /* 0x20c */
2493 .long 0 /* 0x210 */
2494 .long 0 /* 0x214 */
2495 .long 0 /* 0x218 */
2496 .long 0 /* 0x21c */
2497 .long 0 /* 0x220 */
2498 .long 0 /* 0x224 */
2499 .long 0 /* 0x228 */
2500 .long 0 /* 0x22c */
2501 .long 0 /* 0x230 */
2502 .long 0 /* 0x234 */
2503 .long 0 /* 0x238 */
2504 .long 0 /* 0x23c */
2505 .long 0 /* 0x240 */
2506 .long 0 /* 0x244 */
2507 .long 0 /* 0x248 */
2508 .long 0 /* 0x24c */
2509 .long 0 /* 0x250 */
2510 .long 0 /* 0x254 */
2511 .long 0 /* 0x258 */
2512 .long 0 /* 0x25c */
2513 .long 0 /* 0x260 */
2514 .long 0 /* 0x264 */
2515 .long 0 /* 0x268 */
2516 .long 0 /* 0x26c */
2517 .long 0 /* 0x270 */
2518 .long 0 /* 0x274 */
2519 .long 0 /* 0x278 */
2520 .long 0 /* 0x27c */
2521 .long 0 /* 0x280 */
2522 .long 0 /* 0x284 */
2523 .long 0 /* 0x288 */
2524 .long 0 /* 0x28c */
2525 .long 0 /* 0x290 */
2526 .long 0 /* 0x294 */
2527 .long 0 /* 0x298 */
2528 .long 0 /* 0x29c */
2529 .long 0 /* 0x2a0 */
2530 .long 0 /* 0x2a4 */
2531 .long 0 /* 0x2a8 */
2532 .long 0 /* 0x2ac */
2533 .long 0 /* 0x2b0 */
2534 .long 0 /* 0x2b4 */
2535 .long 0 /* 0x2b8 */
2536 .long 0 /* 0x2bc */
2537 .long 0 /* 0x2c0 */
2538 .long 0 /* 0x2c4 */
2539 .long 0 /* 0x2c8 */
2540 .long 0 /* 0x2cc */
2541 .long 0 /* 0x2d0 */
2542 .long 0 /* 0x2d4 */
2543 .long 0 /* 0x2d8 */
2544 .long 0 /* 0x2dc */
2545 .long 0 /* 0x2e0 */
2546 .long 0 /* 0x2e4 */
2547 .long 0 /* 0x2e8 */
2548 .long 0 /* 0x2ec */
2549 .long 0 /* 0x2f0 */
2550 .long 0 /* 0x2f4 */
2551 .long 0 /* 0x2f8 */
5af50993
BH
2552#ifdef CONFIG_KVM_XICS
2553 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2554#else
2555 .long 0 /* 0x2fc - H_XIRR_X*/
2556#endif
e928e9cb 2557 .long DOTSYM(kvmppc_h_random) - hcall_real_table
ae2113a4 2558 .globl hcall_real_table_end
a8606e20
PM
2559hcall_real_table_end:
2560
8563bf52
PM
2561_GLOBAL(kvmppc_h_set_xdabr)
2562 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2563 beq 6f
2564 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2565 andc. r0, r5, r0
2566 beq 3f
25676: li r3, H_PARAMETER
2568 blr
2569
a8606e20 2570_GLOBAL(kvmppc_h_set_dabr)
8563bf52
PM
2571 li r5, DABRX_USER | DABRX_KERNEL
25723:
eee7ff9d
MN
2573BEGIN_FTR_SECTION
2574 b 2f
2575END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
a8606e20 2576 std r4,VCPU_DABR(r3)
8563bf52
PM
2577 stw r5, VCPU_DABRX(r3)
2578 mtspr SPRN_DABRX, r5
8943633c
PM
2579 /* Work around P7 bug where DABR can get corrupted on mtspr */
25801: mtspr SPRN_DABR,r4
2581 mfspr r5, SPRN_DABR
2582 cmpd r4, r5
2583 bne 1b
2584 isync
a8606e20
PM
2585 li r3,0
2586 blr
2587
e8ebedbf
MN
25882:
2589BEGIN_FTR_SECTION
2590 /* POWER9 with disabled DAWR */
ca9a16c3 2591 li r3, H_HARDWARE
e8ebedbf
MN
2592 blr
2593END_FTR_SECTION_IFCLR(CPU_FTR_DAWR)
8563bf52 2594 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
e8ebedbf 2595 rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
760a7364 2596 rlwimi r5, r4, 2, DAWRX_WT
8563bf52
PM
2597 clrrdi r4, r4, 3
2598 std r4, VCPU_DAWR(r3)
2599 std r5, VCPU_DAWRX(r3)
2600 mtspr SPRN_DAWR, r4
2601 mtspr SPRN_DAWRX, r5
2602 li r3, 0
a8606e20
PM
2603 blr
2604
1f09c3ed 2605_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
19ccb76a
PM
2606 ori r11,r11,MSR_EE
2607 std r11,VCPU_MSR(r3)
2608 li r0,1
2609 stb r0,VCPU_CEDED(r3)
2610 sync /* order setting ceded vs. testing prodded */
2611 lbz r5,VCPU_PRODDED(r3)
2612 cmpwi r5,0
04f995a5 2613 bne kvm_cede_prodded
6af27c84
PM
2614 li r12,0 /* set trap to 0 to say hcall is handled */
2615 stw r12,VCPU_TRAP(r3)
19ccb76a 2616 li r0,H_SUCCESS
c75df6f9 2617 std r0,VCPU_GPR(R3)(r3)
19ccb76a
PM
2618
2619 /*
2620 * Set our bit in the bitmask of napping threads unless all the
2621 * other threads are already napping, in which case we send this
2622 * up to the host.
2623 */
2624 ld r5,HSTATE_KVM_VCORE(r13)
e0b7ec05 2625 lbz r6,HSTATE_PTID(r13)
19ccb76a
PM
2626 lwz r8,VCORE_ENTRY_EXIT(r5)
2627 clrldi r8,r8,56
2628 li r0,1
2629 sld r0,r0,r6
2630 addi r6,r5,VCORE_NAPPING_THREADS
263131: lwarx r4,0,r6
2632 or r4,r4,r0
7d6c40da
PM
2633 cmpw r4,r8
2634 beq kvm_cede_exit
19ccb76a
PM
2635 stwcx. r4,0,r6
2636 bne 31b
7d6c40da 2637 /* order napping_threads update vs testing entry_exit_map */
f019b7ad 2638 isync
e0b7ec05 2639 li r0,NAPPING_CEDE
19ccb76a 2640 stb r0,HSTATE_NAPPING(r13)
19ccb76a
PM
2641 lwz r7,VCORE_ENTRY_EXIT(r5)
2642 cmpwi r7,0x100
2643 bge 33f /* another thread already exiting */
2644
2645/*
2646 * Although not specifically required by the architecture, POWER7
2647 * preserves the following registers in nap mode, even if an SMT mode
2648 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2649 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2650 */
2651 /* Save non-volatile GPRs */
c75df6f9
MN
2652 std r14, VCPU_GPR(R14)(r3)
2653 std r15, VCPU_GPR(R15)(r3)
2654 std r16, VCPU_GPR(R16)(r3)
2655 std r17, VCPU_GPR(R17)(r3)
2656 std r18, VCPU_GPR(R18)(r3)
2657 std r19, VCPU_GPR(R19)(r3)
2658 std r20, VCPU_GPR(R20)(r3)
2659 std r21, VCPU_GPR(R21)(r3)
2660 std r22, VCPU_GPR(R22)(r3)
2661 std r23, VCPU_GPR(R23)(r3)
2662 std r24, VCPU_GPR(R24)(r3)
2663 std r25, VCPU_GPR(R25)(r3)
2664 std r26, VCPU_GPR(R26)(r3)
2665 std r27, VCPU_GPR(R27)(r3)
2666 std r28, VCPU_GPR(R28)(r3)
2667 std r29, VCPU_GPR(R29)(r3)
2668 std r30, VCPU_GPR(R30)(r3)
2669 std r31, VCPU_GPR(R31)(r3)
19ccb76a
PM
2670
2671 /* save FP state */
595e4f7e 2672 bl kvmppc_save_fp
19ccb76a 2673
93d17397 2674#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
4bb3c7a0
PM
2675/*
2676 * Branch around the call if both CPU_FTR_TM and
2677 * CPU_FTR_P9_TM_HV_ASSIST are off.
2678 */
93d17397 2679BEGIN_FTR_SECTION
4bb3c7a0
PM
2680 b 91f
2681END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
67f8a8c1
PM
2682 /*
2683 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2684 */
93d17397
PM
2685 ld r9, HSTATE_KVM_VCPU(r13)
2686 bl kvmppc_save_tm
4bb3c7a0 268791:
93d17397
PM
2688#endif
2689
fd6d53b1
PM
2690 /*
2691 * Set DEC to the smaller of DEC and HDEC, so that we wake
2692 * no later than the end of our timeslice (HDEC interrupts
2693 * don't wake us from nap).
2694 */
2695 mfspr r3, SPRN_DEC
2696 mfspr r4, SPRN_HDEC
2697 mftb r5
1bc3fe81
PM
2698BEGIN_FTR_SECTION
2699 /* On P9 check whether the guest has large decrementer mode enabled */
2700 ld r6, HSTATE_KVM_VCORE(r13)
2701 ld r6, VCORE_LPCR(r6)
2702 andis. r6, r6, LPCR_LD@h
2703 bne 68f
2704END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2f272463 2705 extsw r3, r3
1bc3fe81 270668: EXTEND_HDEC(r4)
2f272463 2707 cmpd r3, r4
fd6d53b1
PM
2708 ble 67f
2709 mtspr SPRN_DEC, r4
271067:
2711 /* save expiry time of guest decrementer */
fd6d53b1
PM
2712 add r3, r3, r5
2713 ld r4, HSTATE_KVM_VCPU(r13)
2714 ld r5, HSTATE_KVM_VCORE(r13)
57b8daa7 2715 ld r6, VCORE_TB_OFFSET_APPL(r5)
fd6d53b1
PM
2716 subf r3, r6, r3 /* convert to host TB value */
2717 std r3, VCPU_DEC_EXPIRES(r4)
2718
b6c295df
PM
2719#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2720 ld r4, HSTATE_KVM_VCPU(r13)
2721 addi r3, r4, VCPU_TB_CEDE
2722 bl kvmhv_accumulate_time
2723#endif
2724
ccc07772
PM
2725 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2726
19ccb76a 2727 /*
aa31e843 2728 * Take a nap until a decrementer or external or doobell interrupt
ccc07772 2729 * occurs, with PECE1 and PECE0 set in LPCR.
66feed61 2730 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
ccc07772 2731 * Also clear the runlatch bit before napping.
19ccb76a 2732 */
56548fc0 2733kvm_do_nap:
1f09c3ed
PM
2734 mfspr r0, SPRN_CTRLF
2735 clrrdi r0, r0, 1
2736 mtspr SPRN_CTRLT, r0
582b910e 2737
f0888f70
PM
2738 li r0,1
2739 stb r0,HSTATE_HWTHREAD_REQ(r13)
19ccb76a
PM
2740 mfspr r5,SPRN_LPCR
2741 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
aa31e843 2742BEGIN_FTR_SECTION
66feed61 2743 ori r5, r5, LPCR_PECEDH
ccc07772 2744 rlwimi r5, r3, 0, LPCR_PECEDP
aa31e843 2745END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
bf53c88e
PM
2746
2747kvm_nap_sequence: /* desired LPCR value in r5 */
2748BEGIN_FTR_SECTION
2749 /*
2750 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2751 * enable state loss = 1 (allow SMT mode switch)
2752 * requested level = 0 (just stop dispatching)
2753 */
2754 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2755 mtspr SPRN_PSSCR, r3
2756 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2757 li r4, LPCR_PECE_HVEE@higher
2758 sldi r4, r4, 32
2759 or r5, r5, r4
2760END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
19ccb76a
PM
2761 mtspr SPRN_LPCR,r5
2762 isync
2763 li r0, 0
2764 std r0, HSTATE_SCRATCH0(r13)
2765 ptesync
2766 ld r0, HSTATE_SCRATCH0(r13)
27671: cmpd r0, r0
2768 bne 1b
bf53c88e 2769BEGIN_FTR_SECTION
19ccb76a 2770 nap
bf53c88e
PM
2771FTR_SECTION_ELSE
2772 PPC_STOP
2773ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
19ccb76a
PM
2774 b .
2775
e3bbbbfa
PM
277633: mr r4, r3
2777 li r3, 0
2778 li r12, 0
2779 b 34f
2780
19ccb76a 2781kvm_end_cede:
4619ac88
PM
2782 /* get vcpu pointer */
2783 ld r4, HSTATE_KVM_VCPU(r13)
2784
19ccb76a
PM
2785 /* Woken by external or decrementer interrupt */
2786 ld r1, HSTATE_HOST_R1(r13)
19ccb76a 2787
b6c295df
PM
2788#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2789 addi r3, r4, VCPU_TB_RMINTR
2790 bl kvmhv_accumulate_time
2791#endif
2792
93d17397 2793#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
4bb3c7a0
PM
2794/*
2795 * Branch around the call if both CPU_FTR_TM and
2796 * CPU_FTR_P9_TM_HV_ASSIST are off.
2797 */
93d17397 2798BEGIN_FTR_SECTION
4bb3c7a0
PM
2799 b 91f
2800END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
67f8a8c1
PM
2801 /*
2802 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2803 */
93d17397 2804 bl kvmppc_restore_tm
4bb3c7a0 280591:
93d17397
PM
2806#endif
2807
19ccb76a
PM
2808 /* load up FP state */
2809 bl kvmppc_load_fp
2810
fd6d53b1
PM
2811 /* Restore guest decrementer */
2812 ld r3, VCPU_DEC_EXPIRES(r4)
2813 ld r5, HSTATE_KVM_VCORE(r13)
57b8daa7 2814 ld r6, VCORE_TB_OFFSET_APPL(r5)
fd6d53b1
PM
2815 add r3, r3, r6 /* convert host TB to guest TB value */
2816 mftb r7
2817 subf r3, r7, r3
2818 mtspr SPRN_DEC, r3
2819
19ccb76a 2820 /* Load NV GPRS */
c75df6f9
MN
2821 ld r14, VCPU_GPR(R14)(r4)
2822 ld r15, VCPU_GPR(R15)(r4)
2823 ld r16, VCPU_GPR(R16)(r4)
2824 ld r17, VCPU_GPR(R17)(r4)
2825 ld r18, VCPU_GPR(R18)(r4)
2826 ld r19, VCPU_GPR(R19)(r4)
2827 ld r20, VCPU_GPR(R20)(r4)
2828 ld r21, VCPU_GPR(R21)(r4)
2829 ld r22, VCPU_GPR(R22)(r4)
2830 ld r23, VCPU_GPR(R23)(r4)
2831 ld r24, VCPU_GPR(R24)(r4)
2832 ld r25, VCPU_GPR(R25)(r4)
2833 ld r26, VCPU_GPR(R26)(r4)
2834 ld r27, VCPU_GPR(R27)(r4)
2835 ld r28, VCPU_GPR(R28)(r4)
2836 ld r29, VCPU_GPR(R29)(r4)
2837 ld r30, VCPU_GPR(R30)(r4)
2838 ld r31, VCPU_GPR(R31)(r4)
37f55d30 2839
e3bbbbfa
PM
2840 /* Check the wake reason in SRR1 to see why we got here */
2841 bl kvmppc_check_wake_reason
19ccb76a 2842
37f55d30
SW
2843 /*
2844 * Restore volatile registers since we could have called a
2845 * C routine in kvmppc_check_wake_reason
2846 * r4 = VCPU
2847 * r3 tells us whether we need to return to host or not
2848 * WARNING: it gets checked further down:
2849 * should not modify r3 until this check is done.
2850 */
2851 ld r4, HSTATE_KVM_VCPU(r13)
2852
19ccb76a 2853 /* clear our bit in vcore->napping_threads */
e3bbbbfa
PM
285434: ld r5,HSTATE_KVM_VCORE(r13)
2855 lbz r7,HSTATE_PTID(r13)
19ccb76a 2856 li r0,1
e3bbbbfa 2857 sld r0,r0,r7
19ccb76a
PM
2858 addi r6,r5,VCORE_NAPPING_THREADS
285932: lwarx r7,0,r6
2860 andc r7,r7,r0
2861 stwcx. r7,0,r6
2862 bne 32b
2863 li r0,0
2864 stb r0,HSTATE_NAPPING(r13)
2865
37f55d30 2866 /* See if the wake reason saved in r3 means we need to exit */
e3bbbbfa 2867 stw r12, VCPU_TRAP(r4)
4619ac88 2868 mr r9, r4
e3bbbbfa
PM
2869 cmpdi r3, 0
2870 bgt guest_exit_cont
4619ac88 2871
19ccb76a
PM
2872 /* see if any other thread is already exiting */
2873 lwz r0,VCORE_ENTRY_EXIT(r5)
2874 cmpwi r0,0x100
e3bbbbfa 2875 bge guest_exit_cont
19ccb76a 2876
e3bbbbfa 2877 b kvmppc_cede_reentry /* if not go back to guest */
19ccb76a
PM
2878
2879 /* cede when already previously prodded case */
04f995a5
PM
2880kvm_cede_prodded:
2881 li r0,0
19ccb76a
PM
2882 stb r0,VCPU_PRODDED(r3)
2883 sync /* order testing prodded vs. clearing ceded */
2884 stb r0,VCPU_CEDED(r3)
2885 li r3,H_SUCCESS
2886 blr
2887
2888 /* we've ceded but we want to give control to the host */
04f995a5 2889kvm_cede_exit:
6af27c84 2890 ld r9, HSTATE_KVM_VCPU(r13)
9b9b13a6
BH
2891#ifdef CONFIG_KVM_XICS
2892 /* Abort if we still have a pending escalation */
2893 lbz r5, VCPU_XIVE_ESC_ON(r9)
2894 cmpwi r5, 0
2895 beq 1f
2896 li r0, 0
2897 stb r0, VCPU_CEDED(r9)
28981: /* Enable XIVE escalation */
2899 li r5, XIVE_ESB_SET_PQ_00
2900 mfmsr r0
2901 andi. r0, r0, MSR_DR /* in real mode? */
2902 beq 1f
2903 ld r10, VCPU_XIVE_ESC_VADDR(r9)
2904 cmpdi r10, 0
2905 beq 3f
2906 ldx r0, r10, r5
2907 b 2f
29081: ld r10, VCPU_XIVE_ESC_RADDR(r9)
2909 cmpdi r10, 0
2910 beq 3f
2911 ldcix r0, r10, r5
29122: sync
2913 li r0, 1
2914 stb r0, VCPU_XIVE_ESC_ON(r9)
2915#endif /* CONFIG_KVM_XICS */
29163: b guest_exit_cont
19ccb76a 2917
b4072df4
PM
2918 /* Try to handle a machine check in real mode */
2919machine_check_realmode:
2920 mr r3, r9 /* get vcpu pointer */
b1576fec 2921 bl kvmppc_realmode_machine_check
b4072df4 2922 nop
b4072df4
PM
2923 ld r9, HSTATE_KVM_VCPU(r13)
2924 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
74845bc2 2925 /*
e20bbd3d
AP
2926 * For the guest that is FWNMI capable, deliver all the MCE errors
2927 * (handled/unhandled) by exiting the guest with KVM_EXIT_NMI exit
2928 * reason. This new approach injects machine check errors in guest
2929 * address space to guest with additional information in the form
2930 * of RTAS event, thus enabling guest kernel to suitably handle
2931 * such errors.
966d713e 2932 *
e20bbd3d
AP
2933 * For the guest that is not FWNMI capable (old QEMU) fallback
2934 * to old behaviour for backward compatibility:
2935 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest either
2936 * through machine check interrupt (set HSRR0 to 0x200).
2937 * For handled errors (no-fatal), just go back to guest execution
2938 * with current HSRR0.
966d713e
MS
2939 * if we receive machine check with MSR(RI=0) then deliver it to
2940 * guest as machine check causing guest to crash.
74845bc2 2941 */
74845bc2 2942 ld r11, VCPU_MSR(r9)
1c9e3d51
PM
2943 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2944 bne mc_cont /* if so, exit to host */
e20bbd3d
AP
2945 /* Check if guest is capable of handling NMI exit */
2946 ld r10, VCPU_KVM(r9)
2947 lbz r10, KVM_FWNMI(r10)
2948 cmpdi r10, 1 /* FWNMI capable? */
2949 beq mc_cont /* if so, exit with KVM_EXIT_NMI. */
2950
2951 /* if not, fall through for backward compatibility. */
966d713e
MS
2952 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2953 beq 1f /* Deliver a machine check to guest */
2954 ld r10, VCPU_PC(r9)
2955 cmpdi r3, 0 /* Did we handle MCE ? */
74845bc2 2956 bne 2f /* Continue guest execution. */
b4072df4 2957 /* If not, deliver a machine check. SRR0/1 are already set */
966d713e 29581: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
e4e38121 2959 bl kvmppc_msr_interrupt
74845bc2 29602: b fast_interrupt_c_return
b4072df4 2961
e3bbbbfa
PM
2962/*
2963 * Check the reason we woke from nap, and take appropriate action.
1f09c3ed 2964 * Returns (in r3):
e3bbbbfa
PM
2965 * 0 if nothing needs to be done
2966 * 1 if something happened that needs to be handled by the host
66feed61 2967 * -1 if there was a guest wakeup (IPI or msgsnd)
e3c13e56
SW
2968 * -2 if we handled a PCI passthrough interrupt (returned by
2969 * kvmppc_read_intr only)
e3bbbbfa
PM
2970 *
2971 * Also sets r12 to the interrupt vector for any interrupt that needs
2972 * to be handled now by the host (0x500 for external interrupt), or zero.
37f55d30
SW
2973 * Modifies all volatile registers (since it may call a C function).
2974 * This routine calls kvmppc_read_intr, a C function, if an external
2975 * interrupt is pending.
e3bbbbfa
PM
2976 */
2977kvmppc_check_wake_reason:
2978 mfspr r6, SPRN_SRR1
aa31e843
PM
2979BEGIN_FTR_SECTION
2980 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2981FTR_SECTION_ELSE
2982 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2983ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2984 cmpwi r6, 8 /* was it an external interrupt? */
37f55d30 2985 beq 7f /* if so, see what it was */
e3bbbbfa
PM
2986 li r3, 0
2987 li r12, 0
2988 cmpwi r6, 6 /* was it the decrementer? */
2989 beq 0f
aa31e843
PM
2990BEGIN_FTR_SECTION
2991 cmpwi r6, 5 /* privileged doorbell? */
2992 beq 0f
5d00f66b
PM
2993 cmpwi r6, 3 /* hypervisor doorbell? */
2994 beq 3f
aa31e843 2995END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
fd7bacbc
MS
2996 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2997 beq 4f
e3bbbbfa
PM
2998 li r3, 1 /* anything else, return 1 */
29990: blr
3000
5d00f66b
PM
3001 /* hypervisor doorbell */
30023: li r12, BOOK3S_INTERRUPT_H_DOORBELL
70aa3961
GS
3003
3004 /*
3005 * Clear the doorbell as we will invoke the handler
3006 * explicitly in the guest exit path.
3007 */
3008 lis r6, (PPC_DBELL_SERVER << (63-36))@h
3009 PPC_MSGCLR(6)
66feed61 3010 /* see if it's a host IPI */
5d00f66b 3011 li r3, 1
2cde3716
NP
3012BEGIN_FTR_SECTION
3013 PPC_MSGSYNC
3014 lwsync
3015END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
66feed61
PM
3016 lbz r0, HSTATE_HOST_IPI(r13)
3017 cmpwi r0, 0
3018 bnelr
70aa3961 3019 /* if not, return -1 */
66feed61 3020 li r3, -1
5d00f66b
PM
3021 blr
3022
fd7bacbc
MS
3023 /* Woken up due to Hypervisor maintenance interrupt */
30244: li r12, BOOK3S_INTERRUPT_HMI
3025 li r3, 1
3026 blr
3027
37f55d30
SW
3028 /* external interrupt - create a stack frame so we can call C */
30297: mflr r0
3030 std r0, PPC_LR_STKOFF(r1)
3031 stdu r1, -PPC_MIN_STKFRM(r1)
3032 bl kvmppc_read_intr
3033 nop
3034 li r12, BOOK3S_INTERRUPT_EXTERNAL
f7af5209
SW
3035 cmpdi r3, 1
3036 ble 1f
3037
3038 /*
3039 * Return code of 2 means PCI passthrough interrupt, but
3040 * we need to return back to host to complete handling the
3041 * interrupt. Trap reason is expected in r12 by guest
3042 * exit code.
3043 */
3044 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
30451:
37f55d30
SW
3046 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
3047 addi r1, r1, PPC_MIN_STKFRM
3048 mtlr r0
3049 blr
371fefd6 3050
de56a948
PM
3051/*
3052 * Save away FP, VMX and VSX registers.
3053 * r3 = vcpu pointer
595e4f7e
PM
3054 * N.B. r30 and r31 are volatile across this function,
3055 * thus it is not callable from C.
a8606e20 3056 */
595e4f7e
PM
3057kvmppc_save_fp:
3058 mflr r30
3059 mr r31,r3
8943633c
PM
3060 mfmsr r5
3061 ori r8,r5,MSR_FP
de56a948
PM
3062#ifdef CONFIG_ALTIVEC
3063BEGIN_FTR_SECTION
3064 oris r8,r8,MSR_VEC@h
3065END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3066#endif
3067#ifdef CONFIG_VSX
3068BEGIN_FTR_SECTION
3069 oris r8,r8,MSR_VSX@h
3070END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3071#endif
3072 mtmsrd r8
595e4f7e 3073 addi r3,r3,VCPU_FPRS
9bf163f8 3074 bl store_fp_state
de56a948
PM
3075#ifdef CONFIG_ALTIVEC
3076BEGIN_FTR_SECTION
595e4f7e 3077 addi r3,r31,VCPU_VRS
9bf163f8 3078 bl store_vr_state
de56a948
PM
3079END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3080#endif
3081 mfspr r6,SPRN_VRSAVE
e724f080 3082 stw r6,VCPU_VRSAVE(r31)
595e4f7e 3083 mtlr r30
de56a948
PM
3084 blr
3085
3086/*
3087 * Load up FP, VMX and VSX registers
3088 * r4 = vcpu pointer
595e4f7e
PM
3089 * N.B. r30 and r31 are volatile across this function,
3090 * thus it is not callable from C.
de56a948 3091 */
de56a948 3092kvmppc_load_fp:
595e4f7e
PM
3093 mflr r30
3094 mr r31,r4
de56a948
PM
3095 mfmsr r9
3096 ori r8,r9,MSR_FP
3097#ifdef CONFIG_ALTIVEC
3098BEGIN_FTR_SECTION
3099 oris r8,r8,MSR_VEC@h
3100END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3101#endif
3102#ifdef CONFIG_VSX
3103BEGIN_FTR_SECTION
3104 oris r8,r8,MSR_VSX@h
3105END_FTR_SECTION_IFSET(CPU_FTR_VSX)
3106#endif
3107 mtmsrd r8
595e4f7e 3108 addi r3,r4,VCPU_FPRS
9bf163f8 3109 bl load_fp_state
de56a948
PM
3110#ifdef CONFIG_ALTIVEC
3111BEGIN_FTR_SECTION
595e4f7e 3112 addi r3,r31,VCPU_VRS
9bf163f8 3113 bl load_vr_state
de56a948
PM
3114END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
3115#endif
e724f080 3116 lwz r7,VCPU_VRSAVE(r31)
de56a948 3117 mtspr SPRN_VRSAVE,r7
595e4f7e
PM
3118 mtlr r30
3119 mr r4,r31
de56a948 3120 blr
44a3add8 3121
f024ee09
PM
3122#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
3123/*
3124 * Save transactional state and TM-related registers.
3125 * Called with r9 pointing to the vcpu struct.
3126 * This can modify all checkpointed registers, but
3127 * restores r1, r2 and r9 (vcpu pointer) before exit.
3128 */
3129kvmppc_save_tm:
3130 mflr r0
3131 std r0, PPC_LR_STKOFF(r1)
87a11bb6 3132 stdu r1, -PPC_MIN_STKFRM(r1)
f024ee09
PM
3133
3134 /* Turn on TM. */
3135 mfmsr r8
3136 li r0, 1
3137 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
3138 mtmsrd r8
3139
3140 ld r5, VCPU_MSR(r9)
3141 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
3142 beq 1f /* TM not active in guest. */
3143
3144 std r1, HSTATE_HOST_R1(r13)
3145 li r3, TM_CAUSE_KVM_RESCHED
3146
4bb3c7a0 3147BEGIN_FTR_SECTION
87a11bb6
SJS
3148 lbz r0, HSTATE_FAKE_SUSPEND(r13) /* Were we fake suspended? */
3149 cmpwi r0, 0
4bb3c7a0 3150 beq 3f
87a11bb6
SJS
3151 rldicl. r8, r8, 64 - MSR_TS_S_LG, 62 /* Did we actually hrfid? */
3152 beq 4f
3153BEGIN_FTR_SECTION_NESTED(96)
3154 bl pnv_power9_force_smt4_catch
3155END_FTR_SECTION_NESTED(CPU_FTR_P9_TM_XER_SO_BUG, CPU_FTR_P9_TM_XER_SO_BUG, 96)
3156 nop
681c617b 3157 b 6f
87a11bb6 31583:
681c617b
PM
3159 /* Emulation of the treclaim instruction needs TEXASR before treclaim */
3160 mfspr r6, SPRN_TEXASR
3161 std r6, VCPU_ORIG_TEXASR(r9)
31626:
4bb3c7a0
PM
3163END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
3164
f024ee09
PM
3165 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
3166 li r5, 0
3167 mtmsrd r5, 1
3168
3169 /* All GPRs are volatile at this point. */
3170 TRECLAIM(R3)
3171
3172 /* Temporarily store r13 and r9 so we have some regs to play with */
3173 SET_SCRATCH0(r13)
3174 GET_PACA(r13)
3175 std r9, PACATMSCRATCH(r13)
4bb3c7a0
PM
3176
3177 /* If doing TM emulation on POWER9 DD2.2, check for fake suspend mode */
3178BEGIN_FTR_SECTION
4bb3c7a0
PM
3179 lbz r9, HSTATE_FAKE_SUSPEND(r13)
3180 cmpwi r9, 0
3181 beq 2f
3182 /*
3183 * We were in fake suspend, so we are not going to save the
3184 * register state as the guest checkpointed state (since
3185 * we already have it), therefore we can now use any volatile GPR.
3186 */
3187 /* Reload stack pointer and TOC. */
3188 ld r1, HSTATE_HOST_R1(r13)
3189 ld r2, PACATOC(r13)
87a11bb6 3190 /* Set MSR RI now we have r1 and r13 back. */
4bb3c7a0
PM
3191 li r5, MSR_RI
3192 mtmsrd r5, 1
3193 HMT_MEDIUM
3194 ld r6, HSTATE_DSCR(r13)
3195 mtspr SPRN_DSCR, r6
87a11bb6
SJS
3196BEGIN_FTR_SECTION_NESTED(96)
3197 bl pnv_power9_force_smt4_release
3198END_FTR_SECTION_NESTED(CPU_FTR_P9_TM_XER_SO_BUG, CPU_FTR_P9_TM_XER_SO_BUG, 96)
3199 nop
3200
32014:
4bb3c7a0
PM
3202 mfspr r3, SPRN_PSSCR
3203 /* PSSCR_FAKE_SUSPEND is a write-only bit, but clear it anyway */
3204 li r0, PSSCR_FAKE_SUSPEND
3205 andc r3, r3, r0
3206 mtspr SPRN_PSSCR, r3
3207 ld r9, HSTATE_KVM_VCPU(r13)
681c617b
PM
3208 /* Don't save TEXASR, use value from last exit in real suspend state */
3209 b 11f
4bb3c7a0
PM
32102:
3211END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
3212
f024ee09
PM
3213 ld r9, HSTATE_KVM_VCPU(r13)
3214
3215 /* Get a few more GPRs free. */
3216 std r29, VCPU_GPRS_TM(29)(r9)
3217 std r30, VCPU_GPRS_TM(30)(r9)
3218 std r31, VCPU_GPRS_TM(31)(r9)
3219
3220 /* Save away PPR and DSCR soon so don't run with user values. */
3221 mfspr r31, SPRN_PPR
3222 HMT_MEDIUM
3223 mfspr r30, SPRN_DSCR
3224 ld r29, HSTATE_DSCR(r13)
3225 mtspr SPRN_DSCR, r29
3226
3227 /* Save all but r9, r13 & r29-r31 */
3228 reg = 0
3229 .rept 29
3230 .if (reg != 9) && (reg != 13)
3231 std reg, VCPU_GPRS_TM(reg)(r9)
3232 .endif
3233 reg = reg + 1
3234 .endr
3235 /* ... now save r13 */
3236 GET_SCRATCH0(r4)
3237 std r4, VCPU_GPRS_TM(13)(r9)
3238 /* ... and save r9 */
3239 ld r4, PACATMSCRATCH(r13)
3240 std r4, VCPU_GPRS_TM(9)(r9)
3241
3242 /* Reload stack pointer and TOC. */
3243 ld r1, HSTATE_HOST_R1(r13)
3244 ld r2, PACATOC(r13)
3245
3246 /* Set MSR RI now we have r1 and r13 back. */
3247 li r5, MSR_RI
3248 mtmsrd r5, 1
3249
3250 /* Save away checkpinted SPRs. */
3251 std r31, VCPU_PPR_TM(r9)
3252 std r30, VCPU_DSCR_TM(r9)
3253 mflr r5
3254 mfcr r6
3255 mfctr r7
3256 mfspr r8, SPRN_AMR
3257 mfspr r10, SPRN_TAR
0d808df0 3258 mfxer r11
f024ee09
PM
3259 std r5, VCPU_LR_TM(r9)
3260 stw r6, VCPU_CR_TM(r9)
3261 std r7, VCPU_CTR_TM(r9)
3262 std r8, VCPU_AMR_TM(r9)
3263 std r10, VCPU_TAR_TM(r9)
0d808df0 3264 std r11, VCPU_XER_TM(r9)
f024ee09
PM
3265
3266 /* Restore r12 as trap number. */
3267 lwz r12, VCPU_TRAP(r9)
3268
3269 /* Save FP/VSX. */
3270 addi r3, r9, VCPU_FPRS_TM
3271 bl store_fp_state
3272 addi r3, r9, VCPU_VRS_TM
3273 bl store_vr_state
3274 mfspr r6, SPRN_VRSAVE
3275 stw r6, VCPU_VRSAVE_TM(r9)
32761:
3277 /*
3278 * We need to save these SPRs after the treclaim so that the software
3279 * error code is recorded correctly in the TEXASR. Also the user may
3280 * change these outside of a transaction, so they must always be
3281 * context switched.
3282 */
681c617b
PM
3283 mfspr r7, SPRN_TEXASR
3284 std r7, VCPU_TEXASR(r9)
328511:
f024ee09
PM
3286 mfspr r5, SPRN_TFHAR
3287 mfspr r6, SPRN_TFIAR
f024ee09
PM
3288 std r5, VCPU_TFHAR(r9)
3289 std r6, VCPU_TFIAR(r9)
f024ee09 3290
87a11bb6 3291 addi r1, r1, PPC_MIN_STKFRM
f024ee09
PM
3292 ld r0, PPC_LR_STKOFF(r1)
3293 mtlr r0
3294 blr
3295
3296/*
3297 * Restore transactional state and TM-related registers.
3298 * Called with r4 pointing to the vcpu struct.
3299 * This potentially modifies all checkpointed registers.
3300 * It restores r1, r2, r4 from the PACA.
3301 */
3302kvmppc_restore_tm:
3303 mflr r0
3304 std r0, PPC_LR_STKOFF(r1)
3305
3306 /* Turn on TM/FP/VSX/VMX so we can restore them. */
3307 mfmsr r5
3308 li r6, MSR_TM >> 32
3309 sldi r6, r6, 32
3310 or r5, r5, r6
3311 ori r5, r5, MSR_FP
3312 oris r5, r5, (MSR_VEC | MSR_VSX)@h
3313 mtmsrd r5
3314
3315 /*
3316 * The user may change these outside of a transaction, so they must
3317 * always be context switched.
3318 */
3319 ld r5, VCPU_TFHAR(r4)
3320 ld r6, VCPU_TFIAR(r4)
3321 ld r7, VCPU_TEXASR(r4)
3322 mtspr SPRN_TFHAR, r5
3323 mtspr SPRN_TFIAR, r6
3324 mtspr SPRN_TEXASR, r7
3325
87a11bb6
SJS
3326 li r0, 0
3327 stb r0, HSTATE_FAKE_SUSPEND(r13)
f024ee09
PM
3328 ld r5, VCPU_MSR(r4)
3329 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
3330 beqlr /* TM not active in guest */
3331 std r1, HSTATE_HOST_R1(r13)
3332
3333 /* Make sure the failure summary is set, otherwise we'll program check
3334 * when we trechkpt. It's possible that this might have been not set
3335 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
3336 * host.
3337 */
3338 oris r7, r7, (TEXASR_FS)@h
3339 mtspr SPRN_TEXASR, r7
3340
4bb3c7a0
PM
3341 /*
3342 * If we are doing TM emulation for the guest on a POWER9 DD2,
3343 * then we don't actually do a trechkpt -- we either set up
3344 * fake-suspend mode, or emulate a TM rollback.
3345 */
3346BEGIN_FTR_SECTION
3347 b .Ldo_tm_fake_load
3348END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
3349
f024ee09
PM
3350 /*
3351 * We need to load up the checkpointed state for the guest.
3352 * We need to do this early as it will blow away any GPRs, VSRs and
3353 * some SPRs.
3354 */
3355
3356 mr r31, r4
3357 addi r3, r31, VCPU_FPRS_TM
3358 bl load_fp_state
3359 addi r3, r31, VCPU_VRS_TM
3360 bl load_vr_state
3361 mr r4, r31
3362 lwz r7, VCPU_VRSAVE_TM(r4)
3363 mtspr SPRN_VRSAVE, r7
3364
3365 ld r5, VCPU_LR_TM(r4)
3366 lwz r6, VCPU_CR_TM(r4)
3367 ld r7, VCPU_CTR_TM(r4)
3368 ld r8, VCPU_AMR_TM(r4)
3369 ld r9, VCPU_TAR_TM(r4)
0d808df0 3370 ld r10, VCPU_XER_TM(r4)
f024ee09
PM
3371 mtlr r5
3372 mtcr r6
3373 mtctr r7
3374 mtspr SPRN_AMR, r8
3375 mtspr SPRN_TAR, r9
0d808df0 3376 mtxer r10
f024ee09
PM
3377
3378 /*
3379 * Load up PPR and DSCR values but don't put them in the actual SPRs
3380 * till the last moment to avoid running with userspace PPR and DSCR for
3381 * too long.
3382 */
3383 ld r29, VCPU_DSCR_TM(r4)
3384 ld r30, VCPU_PPR_TM(r4)
3385
3386 std r2, PACATMSCRATCH(r13) /* Save TOC */
3387
3388 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
3389 li r5, 0
3390 mtmsrd r5, 1
3391
3392 /* Load GPRs r0-r28 */
3393 reg = 0
3394 .rept 29
3395 ld reg, VCPU_GPRS_TM(reg)(r31)
3396 reg = reg + 1
3397 .endr
3398
3399 mtspr SPRN_DSCR, r29
3400 mtspr SPRN_PPR, r30
3401
3402 /* Load final GPRs */
3403 ld 29, VCPU_GPRS_TM(29)(r31)
3404 ld 30, VCPU_GPRS_TM(30)(r31)
3405 ld 31, VCPU_GPRS_TM(31)(r31)
3406
3407 /* TM checkpointed state is now setup. All GPRs are now volatile. */
3408 TRECHKPT
3409
3410 /* Now let's get back the state we need. */
3411 HMT_MEDIUM
3412 GET_PACA(r13)
3413 ld r29, HSTATE_DSCR(r13)
3414 mtspr SPRN_DSCR, r29
3415 ld r4, HSTATE_KVM_VCPU(r13)
3416 ld r1, HSTATE_HOST_R1(r13)
3417 ld r2, PACATMSCRATCH(r13)
3418
3419 /* Set the MSR RI since we have our registers back. */
3420 li r5, MSR_RI
3421 mtmsrd r5, 1
4bb3c7a0 34229:
f024ee09
PM
3423 ld r0, PPC_LR_STKOFF(r1)
3424 mtlr r0
3425 blr
4bb3c7a0
PM
3426
3427.Ldo_tm_fake_load:
3428 cmpwi r5, 1 /* check for suspended state */
3429 bgt 10f
3430 stb r5, HSTATE_FAKE_SUSPEND(r13)
3431 b 9b /* and return */
343210: stdu r1, -PPC_MIN_STKFRM(r1)
3433 /* guest is in transactional state, so simulate rollback */
3434 mr r3, r4
3435 bl kvmhv_emulate_tm_rollback
3436 nop
3437 ld r4, HSTATE_KVM_VCPU(r13) /* our vcpu pointer has been trashed */
3438 addi r1, r1, PPC_MIN_STKFRM
3439 b 9b
f024ee09
PM
3440#endif
3441
44a3add8
PM
3442/*
3443 * We come here if we get any exception or interrupt while we are
3444 * executing host real mode code while in guest MMU context.
857b99e1
PM
3445 * r12 is (CR << 32) | vector
3446 * r13 points to our PACA
3447 * r12 is saved in HSTATE_SCRATCH0(r13)
3448 * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
3449 * r9 is saved in HSTATE_SCRATCH2(r13)
3450 * r13 is saved in HSPRG1
3451 * cfar is saved in HSTATE_CFAR(r13)
3452 * ppr is saved in HSTATE_PPR(r13)
44a3add8
PM
3453 */
3454kvmppc_bad_host_intr:
857b99e1
PM
3455 /*
3456 * Switch to the emergency stack, but start half-way down in
3457 * case we were already on it.
3458 */
3459 mr r9, r1
3460 std r1, PACAR1(r13)
3461 ld r1, PACAEMERGSP(r13)
3462 subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
3463 std r9, 0(r1)
3464 std r0, GPR0(r1)
3465 std r9, GPR1(r1)
3466 std r2, GPR2(r1)
3467 SAVE_4GPRS(3, r1)
3468 SAVE_2GPRS(7, r1)
3469 srdi r0, r12, 32
3470 clrldi r12, r12, 32
3471 std r0, _CCR(r1)
3472 std r12, _TRAP(r1)
3473 andi. r0, r12, 2
3474 beq 1f
3475 mfspr r3, SPRN_HSRR0
3476 mfspr r4, SPRN_HSRR1
3477 mfspr r5, SPRN_HDAR
3478 mfspr r6, SPRN_HDSISR
3479 b 2f
34801: mfspr r3, SPRN_SRR0
3481 mfspr r4, SPRN_SRR1
3482 mfspr r5, SPRN_DAR
3483 mfspr r6, SPRN_DSISR
34842: std r3, _NIP(r1)
3485 std r4, _MSR(r1)
3486 std r5, _DAR(r1)
3487 std r6, _DSISR(r1)
3488 ld r9, HSTATE_SCRATCH2(r13)
3489 ld r12, HSTATE_SCRATCH0(r13)
3490 GET_SCRATCH0(r0)
3491 SAVE_4GPRS(9, r1)
3492 std r0, GPR13(r1)
3493 SAVE_NVGPRS(r1)
3494 ld r5, HSTATE_CFAR(r13)
3495 std r5, ORIG_GPR3(r1)
3496 mflr r3
3497#ifdef CONFIG_RELOCATABLE
3498 ld r4, HSTATE_SCRATCH1(r13)
3499#else
3500 mfctr r4
3501#endif
3502 mfxer r5
4e26bc4a 3503 lbz r6, PACAIRQSOFTMASK(r13)
857b99e1
PM
3504 std r3, _LINK(r1)
3505 std r4, _CTR(r1)
3506 std r5, _XER(r1)
3507 std r6, SOFTE(r1)
3508 ld r2, PACATOC(r13)
3509 LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
3510 std r3, STACK_FRAME_OVERHEAD-16(r1)
3511
3512 /*
3513 * On POWER9 do a minimal restore of the MMU and call C code,
3514 * which will print a message and panic.
3515 * XXX On POWER7 and POWER8, we just spin here since we don't
3516 * know what the other threads are doing (and we don't want to
3517 * coordinate with them) - but at least we now have register state
3518 * in memory that we might be able to look at from another CPU.
3519 */
3520BEGIN_FTR_SECTION
44a3add8 3521 b .
857b99e1
PM
3522END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
3523 ld r9, HSTATE_KVM_VCPU(r13)
3524 ld r10, VCPU_KVM(r9)
3525
3526 li r0, 0
3527 mtspr SPRN_AMR, r0
3528 mtspr SPRN_IAMR, r0
3529 mtspr SPRN_CIABR, r0
3530 mtspr SPRN_DAWRX, r0
3531
3532 /* Flush the ERAT on radix P9 DD1 guest exit */
3533BEGIN_FTR_SECTION
3534 PPC_INVALIDATE_ERAT
3535END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
3536
3537BEGIN_MMU_FTR_SECTION
3538 b 4f
3539END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
3540
3541 slbmte r0, r0
3542 slbia
3543 ptesync
3544 ld r8, PACA_SLBSHADOWPTR(r13)
3545 .rept SLB_NUM_BOLTED
3546 li r3, SLBSHADOW_SAVEAREA
3547 LDX_BE r5, r8, r3
3548 addi r3, r3, 8
3549 LDX_BE r6, r8, r3
3550 andis. r7, r5, SLB_ESID_V@h
3551 beq 3f
3552 slbmte r6, r5
35533: addi r8, r8, 16
3554 .endr
3555
35564: lwz r7, KVM_HOST_LPID(r10)
3557 mtspr SPRN_LPID, r7
3558 mtspr SPRN_PID, r0
3559 ld r8, KVM_HOST_LPCR(r10)
3560 mtspr SPRN_LPCR, r8
3561 isync
3562 li r0, KVM_GUEST_MODE_NONE
3563 stb r0, HSTATE_IN_GUEST(r13)
3564
3565 /*
3566 * Turn on the MMU and jump to C code
3567 */
3568 bcl 20, 31, .+4
35695: mflr r3
3570 addi r3, r3, 9f - 5b
3571 ld r4, PACAKMSR(r13)
3572 mtspr SPRN_SRR0, r3
3573 mtspr SPRN_SRR1, r4
222f20f1 3574 RFI_TO_KERNEL
857b99e1
PM
35759: addi r3, r1, STACK_FRAME_OVERHEAD
3576 bl kvmppc_bad_interrupt
3577 b 9b
e4e38121
MN
3578
3579/*
3580 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3581 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3582 * r11 has the guest MSR value (in/out)
3583 * r9 has a vcpu pointer (in)
3584 * r0 is used as a scratch register
3585 */
3586kvmppc_msr_interrupt:
3587 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3588 cmpwi r0, 2 /* Check if we are in transactional state.. */
3589 ld r11, VCPU_INTR_MSR(r9)
3590 bne 1f
3591 /* ... if transactional, change to suspended */
3592 li r0, 1
35931: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3594 blr
9bc01a9b
PM
3595
3596/*
3597 * This works around a hardware bug on POWER8E processors, where
3598 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3599 * performance monitor interrupt. Instead, when we need to have
3600 * an interrupt pending, we have to arrange for a counter to overflow.
3601 */
3602kvmppc_fix_pmao:
3603 li r3, 0
3604 mtspr SPRN_MMCR2, r3
3605 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3606 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3607 mtspr SPRN_MMCR0, r3
3608 lis r3, 0x7fff
3609 ori r3, r3, 0xffff
3610 mtspr SPRN_PMC6, r3
3611 isync
3612 blr
b6c295df
PM
3613
3614#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3615/*
3616 * Start timing an activity
3617 * r3 = pointer to time accumulation struct, r4 = vcpu
3618 */
3619kvmhv_start_timing:
3620 ld r5, HSTATE_KVM_VCORE(r13)
57b8daa7
PM
3621 ld r6, VCORE_TB_OFFSET_APPL(r5)
3622 mftb r5
3623 subf r5, r6, r5 /* subtract current timebase offset */
b6c295df
PM
3624 std r3, VCPU_CUR_ACTIVITY(r4)
3625 std r5, VCPU_ACTIVITY_START(r4)
3626 blr
3627
3628/*
3629 * Accumulate time to one activity and start another.
3630 * r3 = pointer to new time accumulation struct, r4 = vcpu
3631 */
3632kvmhv_accumulate_time:
3633 ld r5, HSTATE_KVM_VCORE(r13)
57b8daa7
PM
3634 ld r8, VCORE_TB_OFFSET_APPL(r5)
3635 ld r5, VCPU_CUR_ACTIVITY(r4)
b6c295df
PM
3636 ld r6, VCPU_ACTIVITY_START(r4)
3637 std r3, VCPU_CUR_ACTIVITY(r4)
3638 mftb r7
57b8daa7 3639 subf r7, r8, r7 /* subtract current timebase offset */
b6c295df
PM
3640 std r7, VCPU_ACTIVITY_START(r4)
3641 cmpdi r5, 0
3642 beqlr
3643 subf r3, r6, r7
3644 ld r8, TAS_SEQCOUNT(r5)
3645 cmpdi r8, 0
3646 addi r8, r8, 1
3647 std r8, TAS_SEQCOUNT(r5)
3648 lwsync
3649 ld r7, TAS_TOTAL(r5)
3650 add r7, r7, r3
3651 std r7, TAS_TOTAL(r5)
3652 ld r6, TAS_MIN(r5)
3653 ld r7, TAS_MAX(r5)
3654 beq 3f
3655 cmpd r3, r6
3656 bge 1f
36573: std r3, TAS_MIN(r5)
36581: cmpd r3, r7
3659 ble 2f
3660 std r3, TAS_MAX(r5)
36612: lwsync
3662 addi r8, r8, 1
3663 std r8, TAS_SEQCOUNT(r5)
3664 blr
3665#endif