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