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