]> git.karo-electronics.de Git - linux-beck.git/blob - arch/powerpc/kvm/book3s_hv_rmhandlers.S
powerpc: Fix little endian lppaca, slb_shadow and dtl_entry
[linux-beck.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/mmu-hash64.h>
31
32 #ifdef __LITTLE_ENDIAN__
33 #error Need to fix lppaca and SLB shadow accesses in little endian mode
34 #endif
35
36 /*****************************************************************************
37  *                                                                           *
38  *        Real Mode handlers that need to be in the linear mapping           *
39  *                                                                           *
40  ****************************************************************************/
41
42         .globl  kvmppc_skip_interrupt
43 kvmppc_skip_interrupt:
44         mfspr   r13,SPRN_SRR0
45         addi    r13,r13,4
46         mtspr   SPRN_SRR0,r13
47         GET_SCRATCH0(r13)
48         rfid
49         b       .
50
51         .globl  kvmppc_skip_Hinterrupt
52 kvmppc_skip_Hinterrupt:
53         mfspr   r13,SPRN_HSRR0
54         addi    r13,r13,4
55         mtspr   SPRN_HSRR0,r13
56         GET_SCRATCH0(r13)
57         hrfid
58         b       .
59
60 /*
61  * Call kvmppc_hv_entry in real mode.
62  * Must be called with interrupts hard-disabled.
63  *
64  * Input Registers:
65  *
66  * LR = return address to continue at after eventually re-enabling MMU
67  */
68 _GLOBAL(kvmppc_hv_entry_trampoline)
69         mfmsr   r10
70         LOAD_REG_ADDR(r5, kvmppc_hv_entry)
71         li      r0,MSR_RI
72         andc    r0,r10,r0
73         li      r6,MSR_IR | MSR_DR
74         andc    r6,r10,r6
75         mtmsrd  r0,1            /* clear RI in MSR */
76         mtsrr0  r5
77         mtsrr1  r6
78         RFI
79
80 /******************************************************************************
81  *                                                                            *
82  *                               Entry code                                   *
83  *                                                                            *
84  *****************************************************************************/
85
86 /*
87  * We come in here when wakened from nap mode on a secondary hw thread.
88  * Relocation is off and most register values are lost.
89  * r13 points to the PACA.
90  */
91         .globl  kvm_start_guest
92 kvm_start_guest:
93         ld      r1,PACAEMERGSP(r13)
94         subi    r1,r1,STACK_FRAME_OVERHEAD
95         ld      r2,PACATOC(r13)
96
97         li      r0,KVM_HWTHREAD_IN_KVM
98         stb     r0,HSTATE_HWTHREAD_STATE(r13)
99
100         /* NV GPR values from power7_idle() will no longer be valid */
101         li      r0,1
102         stb     r0,PACA_NAPSTATELOST(r13)
103
104         /* were we napping due to cede? */
105         lbz     r0,HSTATE_NAPPING(r13)
106         cmpwi   r0,0
107         bne     kvm_end_cede
108
109         /*
110          * We weren't napping due to cede, so this must be a secondary
111          * thread being woken up to run a guest, or being woken up due
112          * to a stray IPI.  (Or due to some machine check or hypervisor
113          * maintenance interrupt while the core is in KVM.)
114          */
115
116         /* Check the wake reason in SRR1 to see why we got here */
117         mfspr   r3,SPRN_SRR1
118         rlwinm  r3,r3,44-31,0x7         /* extract wake reason field */
119         cmpwi   r3,4                    /* was it an external interrupt? */
120         bne     27f                     /* if not */
121         ld      r5,HSTATE_XICS_PHYS(r13)
122         li      r7,XICS_XIRR            /* if it was an external interrupt, */
123         lwzcix  r8,r5,r7                /* get and ack the interrupt */
124         sync
125         clrldi. r9,r8,40                /* get interrupt source ID. */
126         beq     28f                     /* none there? */
127         cmpwi   r9,XICS_IPI             /* was it an IPI? */
128         bne     29f
129         li      r0,0xff
130         li      r6,XICS_MFRR
131         stbcix  r0,r5,r6                /* clear IPI */
132         stwcix  r8,r5,r7                /* EOI the interrupt */
133         sync                            /* order loading of vcpu after that */
134
135         /* get vcpu pointer, NULL if we have no vcpu to run */
136         ld      r4,HSTATE_KVM_VCPU(r13)
137         cmpdi   r4,0
138         /* if we have no vcpu to run, go back to sleep */
139         beq     kvm_no_guest
140         b       kvmppc_hv_entry
141
142 27:     /* XXX should handle hypervisor maintenance interrupts etc. here */
143         b       kvm_no_guest
144 28:     /* SRR1 said external but ICP said nope?? */
145         b       kvm_no_guest
146 29:     /* External non-IPI interrupt to offline secondary thread? help?? */
147         stw     r8,HSTATE_SAVED_XIRR(r13)
148         b       kvm_no_guest
149
150 .global kvmppc_hv_entry
151 kvmppc_hv_entry:
152
153         /* Required state:
154          *
155          * R4 = vcpu pointer
156          * MSR = ~IR|DR
157          * R13 = PACA
158          * R1 = host R1
159          * all other volatile GPRS = free
160          */
161         mflr    r0
162         std     r0, HSTATE_VMHANDLER(r13)
163
164         /* Set partition DABR */
165         /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
166         li      r5,3
167         ld      r6,VCPU_DABR(r4)
168         mtspr   SPRN_DABRX,r5
169         mtspr   SPRN_DABR,r6
170 BEGIN_FTR_SECTION
171         isync
172 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
173
174         /* Load guest PMU registers */
175         /* R4 is live here (vcpu pointer) */
176         li      r3, 1
177         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
178         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
179         isync
180         lwz     r3, VCPU_PMC(r4)        /* always load up guest PMU registers */
181         lwz     r5, VCPU_PMC + 4(r4)    /* to prevent information leak */
182         lwz     r6, VCPU_PMC + 8(r4)
183         lwz     r7, VCPU_PMC + 12(r4)
184         lwz     r8, VCPU_PMC + 16(r4)
185         lwz     r9, VCPU_PMC + 20(r4)
186 BEGIN_FTR_SECTION
187         lwz     r10, VCPU_PMC + 24(r4)
188         lwz     r11, VCPU_PMC + 28(r4)
189 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
190         mtspr   SPRN_PMC1, r3
191         mtspr   SPRN_PMC2, r5
192         mtspr   SPRN_PMC3, r6
193         mtspr   SPRN_PMC4, r7
194         mtspr   SPRN_PMC5, r8
195         mtspr   SPRN_PMC6, r9
196 BEGIN_FTR_SECTION
197         mtspr   SPRN_PMC7, r10
198         mtspr   SPRN_PMC8, r11
199 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
200         ld      r3, VCPU_MMCR(r4)
201         ld      r5, VCPU_MMCR + 8(r4)
202         ld      r6, VCPU_MMCR + 16(r4)
203         mtspr   SPRN_MMCR1, r5
204         mtspr   SPRN_MMCRA, r6
205         mtspr   SPRN_MMCR0, r3
206         isync
207
208         /* Load up FP, VMX and VSX registers */
209         bl      kvmppc_load_fp
210
211         ld      r14, VCPU_GPR(R14)(r4)
212         ld      r15, VCPU_GPR(R15)(r4)
213         ld      r16, VCPU_GPR(R16)(r4)
214         ld      r17, VCPU_GPR(R17)(r4)
215         ld      r18, VCPU_GPR(R18)(r4)
216         ld      r19, VCPU_GPR(R19)(r4)
217         ld      r20, VCPU_GPR(R20)(r4)
218         ld      r21, VCPU_GPR(R21)(r4)
219         ld      r22, VCPU_GPR(R22)(r4)
220         ld      r23, VCPU_GPR(R23)(r4)
221         ld      r24, VCPU_GPR(R24)(r4)
222         ld      r25, VCPU_GPR(R25)(r4)
223         ld      r26, VCPU_GPR(R26)(r4)
224         ld      r27, VCPU_GPR(R27)(r4)
225         ld      r28, VCPU_GPR(R28)(r4)
226         ld      r29, VCPU_GPR(R29)(r4)
227         ld      r30, VCPU_GPR(R30)(r4)
228         ld      r31, VCPU_GPR(R31)(r4)
229
230 BEGIN_FTR_SECTION
231         /* Switch DSCR to guest value */
232         ld      r5, VCPU_DSCR(r4)
233         mtspr   SPRN_DSCR, r5
234 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
235
236         /*
237          * Set the decrementer to the guest decrementer.
238          */
239         ld      r8,VCPU_DEC_EXPIRES(r4)
240         mftb    r7
241         subf    r3,r7,r8
242         mtspr   SPRN_DEC,r3
243         stw     r3,VCPU_DEC(r4)
244
245         ld      r5, VCPU_SPRG0(r4)
246         ld      r6, VCPU_SPRG1(r4)
247         ld      r7, VCPU_SPRG2(r4)
248         ld      r8, VCPU_SPRG3(r4)
249         mtspr   SPRN_SPRG0, r5
250         mtspr   SPRN_SPRG1, r6
251         mtspr   SPRN_SPRG2, r7
252         mtspr   SPRN_SPRG3, r8
253
254         /* Save R1 in the PACA */
255         std     r1, HSTATE_HOST_R1(r13)
256
257         /* Increment yield count if they have a VPA */
258         ld      r3, VCPU_VPA(r4)
259         cmpdi   r3, 0
260         beq     25f
261         lwz     r5, LPPACA_YIELDCOUNT(r3)
262         addi    r5, r5, 1
263         stw     r5, LPPACA_YIELDCOUNT(r3)
264         li      r6, 1
265         stb     r6, VCPU_VPA_DIRTY(r4)
266 25:
267         /* Load up DAR and DSISR */
268         ld      r5, VCPU_DAR(r4)
269         lwz     r6, VCPU_DSISR(r4)
270         mtspr   SPRN_DAR, r5
271         mtspr   SPRN_DSISR, r6
272
273 BEGIN_FTR_SECTION
274         /* Restore AMR and UAMOR, set AMOR to all 1s */
275         ld      r5,VCPU_AMR(r4)
276         ld      r6,VCPU_UAMOR(r4)
277         li      r7,-1
278         mtspr   SPRN_AMR,r5
279         mtspr   SPRN_UAMOR,r6
280         mtspr   SPRN_AMOR,r7
281 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
282
283         /* Clear out SLB */
284         li      r6,0
285         slbmte  r6,r6
286         slbia
287         ptesync
288
289 BEGIN_FTR_SECTION
290         b       30f
291 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
292         /*
293          * POWER7 host -> guest partition switch code.
294          * We don't have to lock against concurrent tlbies,
295          * but we do have to coordinate across hardware threads.
296          */
297         /* Increment entry count iff exit count is zero. */
298         ld      r5,HSTATE_KVM_VCORE(r13)
299         addi    r9,r5,VCORE_ENTRY_EXIT
300 21:     lwarx   r3,0,r9
301         cmpwi   r3,0x100                /* any threads starting to exit? */
302         bge     secondary_too_late      /* if so we're too late to the party */
303         addi    r3,r3,1
304         stwcx.  r3,0,r9
305         bne     21b
306
307         /* Primary thread switches to guest partition. */
308         ld      r9,VCPU_KVM(r4)         /* pointer to struct kvm */
309         lwz     r6,VCPU_PTID(r4)
310         cmpwi   r6,0
311         bne     20f
312         ld      r6,KVM_SDR1(r9)
313         lwz     r7,KVM_LPID(r9)
314         li      r0,LPID_RSVD            /* switch to reserved LPID */
315         mtspr   SPRN_LPID,r0
316         ptesync
317         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
318         mtspr   SPRN_LPID,r7
319         isync
320
321         /* See if we need to flush the TLB */
322         lhz     r6,PACAPACAINDEX(r13)   /* test_bit(cpu, need_tlb_flush) */
323         clrldi  r7,r6,64-6              /* extract bit number (6 bits) */
324         srdi    r6,r6,6                 /* doubleword number */
325         sldi    r6,r6,3                 /* address offset */
326         add     r6,r6,r9
327         addi    r6,r6,KVM_NEED_FLUSH    /* dword in kvm->arch.need_tlb_flush */
328         li      r0,1
329         sld     r0,r0,r7
330         ld      r7,0(r6)
331         and.    r7,r7,r0
332         beq     22f
333 23:     ldarx   r7,0,r6                 /* if set, clear the bit */
334         andc    r7,r7,r0
335         stdcx.  r7,0,r6
336         bne     23b
337         li      r6,128                  /* and flush the TLB */
338         mtctr   r6
339         li      r7,0x800                /* IS field = 0b10 */
340         ptesync
341 28:     tlbiel  r7
342         addi    r7,r7,0x1000
343         bdnz    28b
344         ptesync
345
346 22:     li      r0,1
347         stb     r0,VCORE_IN_GUEST(r5)   /* signal secondaries to continue */
348         b       10f
349
350         /* Secondary threads wait for primary to have done partition switch */
351 20:     lbz     r0,VCORE_IN_GUEST(r5)
352         cmpwi   r0,0
353         beq     20b
354
355         /* Set LPCR and RMOR. */
356 10:     ld      r8,KVM_LPCR(r9)
357         mtspr   SPRN_LPCR,r8
358         ld      r8,KVM_RMOR(r9)
359         mtspr   SPRN_RMOR,r8
360         isync
361
362         /* Check if HDEC expires soon */
363         mfspr   r3,SPRN_HDEC
364         cmpwi   r3,10
365         li      r12,BOOK3S_INTERRUPT_HV_DECREMENTER
366         mr      r9,r4
367         blt     hdec_soon
368
369         /* Save purr/spurr */
370         mfspr   r5,SPRN_PURR
371         mfspr   r6,SPRN_SPURR
372         std     r5,HSTATE_PURR(r13)
373         std     r6,HSTATE_SPURR(r13)
374         ld      r7,VCPU_PURR(r4)
375         ld      r8,VCPU_SPURR(r4)
376         mtspr   SPRN_PURR,r7
377         mtspr   SPRN_SPURR,r8
378         b       31f
379
380         /*
381          * PPC970 host -> guest partition switch code.
382          * We have to lock against concurrent tlbies,
383          * using native_tlbie_lock to lock against host tlbies
384          * and kvm->arch.tlbie_lock to lock against guest tlbies.
385          * We also have to invalidate the TLB since its
386          * entries aren't tagged with the LPID.
387          */
388 30:     ld      r9,VCPU_KVM(r4)         /* pointer to struct kvm */
389
390         /* first take native_tlbie_lock */
391         .section ".toc","aw"
392 toc_tlbie_lock:
393         .tc     native_tlbie_lock[TC],native_tlbie_lock
394         .previous
395         ld      r3,toc_tlbie_lock@toc(2)
396         lwz     r8,PACA_LOCK_TOKEN(r13)
397 24:     lwarx   r0,0,r3
398         cmpwi   r0,0
399         bne     24b
400         stwcx.  r8,0,r3
401         bne     24b
402         isync
403
404         ld      r7,KVM_LPCR(r9)         /* use kvm->arch.lpcr to store HID4 */
405         li      r0,0x18f
406         rotldi  r0,r0,HID4_LPID5_SH     /* all lpid bits in HID4 = 1 */
407         or      r0,r7,r0
408         ptesync
409         sync
410         mtspr   SPRN_HID4,r0            /* switch to reserved LPID */
411         isync
412         li      r0,0
413         stw     r0,0(r3)                /* drop native_tlbie_lock */
414
415         /* invalidate the whole TLB */
416         li      r0,256
417         mtctr   r0
418         li      r6,0
419 25:     tlbiel  r6
420         addi    r6,r6,0x1000
421         bdnz    25b
422         ptesync
423
424         /* Take the guest's tlbie_lock */
425         addi    r3,r9,KVM_TLBIE_LOCK
426 24:     lwarx   r0,0,r3
427         cmpwi   r0,0
428         bne     24b
429         stwcx.  r8,0,r3
430         bne     24b
431         isync
432         ld      r6,KVM_SDR1(r9)
433         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
434
435         /* Set up HID4 with the guest's LPID etc. */
436         sync
437         mtspr   SPRN_HID4,r7
438         isync
439
440         /* drop the guest's tlbie_lock */
441         li      r0,0
442         stw     r0,0(r3)
443
444         /* Check if HDEC expires soon */
445         mfspr   r3,SPRN_HDEC
446         cmpwi   r3,10
447         li      r12,BOOK3S_INTERRUPT_HV_DECREMENTER
448         mr      r9,r4
449         blt     hdec_soon
450
451         /* Enable HDEC interrupts */
452         mfspr   r0,SPRN_HID0
453         li      r3,1
454         rldimi  r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
455         sync
456         mtspr   SPRN_HID0,r0
457         mfspr   r0,SPRN_HID0
458         mfspr   r0,SPRN_HID0
459         mfspr   r0,SPRN_HID0
460         mfspr   r0,SPRN_HID0
461         mfspr   r0,SPRN_HID0
462         mfspr   r0,SPRN_HID0
463
464         /* Load up guest SLB entries */
465 31:     lwz     r5,VCPU_SLB_MAX(r4)
466         cmpwi   r5,0
467         beq     9f
468         mtctr   r5
469         addi    r6,r4,VCPU_SLB
470 1:      ld      r8,VCPU_SLB_E(r6)
471         ld      r9,VCPU_SLB_V(r6)
472         slbmte  r9,r8
473         addi    r6,r6,VCPU_SLB_SIZE
474         bdnz    1b
475 9:
476
477         /* Restore state of CTRL run bit; assume 1 on entry */
478         lwz     r5,VCPU_CTRL(r4)
479         andi.   r5,r5,1
480         bne     4f
481         mfspr   r6,SPRN_CTRLF
482         clrrdi  r6,r6,1
483         mtspr   SPRN_CTRLT,r6
484 4:
485         ld      r6, VCPU_CTR(r4)
486         lwz     r7, VCPU_XER(r4)
487
488         mtctr   r6
489         mtxer   r7
490
491         ld      r10, VCPU_PC(r4)
492         ld      r11, VCPU_MSR(r4)
493 kvmppc_cede_reentry:            /* r4 = vcpu, r13 = paca */
494         ld      r6, VCPU_SRR0(r4)
495         ld      r7, VCPU_SRR1(r4)
496
497         /* r11 = vcpu->arch.msr & ~MSR_HV */
498         rldicl  r11, r11, 63 - MSR_HV_LG, 1
499         rotldi  r11, r11, 1 + MSR_HV_LG
500         ori     r11, r11, MSR_ME
501
502         /* Check if we can deliver an external or decrementer interrupt now */
503         ld      r0,VCPU_PENDING_EXC(r4)
504         lis     r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
505         and     r0,r0,r8
506         cmpdi   cr1,r0,0
507         andi.   r0,r11,MSR_EE
508         beq     cr1,11f
509 BEGIN_FTR_SECTION
510         mfspr   r8,SPRN_LPCR
511         ori     r8,r8,LPCR_MER
512         mtspr   SPRN_LPCR,r8
513         isync
514 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
515         beq     5f
516         li      r0,BOOK3S_INTERRUPT_EXTERNAL
517 12:     mr      r6,r10
518         mr      r10,r0
519         mr      r7,r11
520         li      r11,(MSR_ME << 1) | 1   /* synthesize MSR_SF | MSR_ME */
521         rotldi  r11,r11,63
522         b       5f
523 11:     beq     5f
524         mfspr   r0,SPRN_DEC
525         cmpwi   r0,0
526         li      r0,BOOK3S_INTERRUPT_DECREMENTER
527         blt     12b
528
529         /* Move SRR0 and SRR1 into the respective regs */
530 5:      mtspr   SPRN_SRR0, r6
531         mtspr   SPRN_SRR1, r7
532
533 fast_guest_return:
534         li      r0,0
535         stb     r0,VCPU_CEDED(r4)       /* cancel cede */
536         mtspr   SPRN_HSRR0,r10
537         mtspr   SPRN_HSRR1,r11
538
539         /* Activate guest mode, so faults get handled by KVM */
540         li      r9, KVM_GUEST_MODE_GUEST
541         stb     r9, HSTATE_IN_GUEST(r13)
542
543         /* Enter guest */
544
545 BEGIN_FTR_SECTION
546         ld      r5, VCPU_CFAR(r4)
547         mtspr   SPRN_CFAR, r5
548 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
549
550         ld      r5, VCPU_LR(r4)
551         lwz     r6, VCPU_CR(r4)
552         mtlr    r5
553         mtcr    r6
554
555         ld      r0, VCPU_GPR(R0)(r4)
556         ld      r1, VCPU_GPR(R1)(r4)
557         ld      r2, VCPU_GPR(R2)(r4)
558         ld      r3, VCPU_GPR(R3)(r4)
559         ld      r5, VCPU_GPR(R5)(r4)
560         ld      r6, VCPU_GPR(R6)(r4)
561         ld      r7, VCPU_GPR(R7)(r4)
562         ld      r8, VCPU_GPR(R8)(r4)
563         ld      r9, VCPU_GPR(R9)(r4)
564         ld      r10, VCPU_GPR(R10)(r4)
565         ld      r11, VCPU_GPR(R11)(r4)
566         ld      r12, VCPU_GPR(R12)(r4)
567         ld      r13, VCPU_GPR(R13)(r4)
568
569         ld      r4, VCPU_GPR(R4)(r4)
570
571         hrfid
572         b       .
573
574 /******************************************************************************
575  *                                                                            *
576  *                               Exit code                                    *
577  *                                                                            *
578  *****************************************************************************/
579
580 /*
581  * We come here from the first-level interrupt handlers.
582  */
583         .globl  kvmppc_interrupt
584 kvmppc_interrupt:
585         /*
586          * Register contents:
587          * R12          = interrupt vector
588          * R13          = PACA
589          * guest CR, R12 saved in shadow VCPU SCRATCH1/0
590          * guest R13 saved in SPRN_SCRATCH0
591          */
592         /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
593         std     r9, HSTATE_HOST_R2(r13)
594         ld      r9, HSTATE_KVM_VCPU(r13)
595
596         /* Save registers */
597
598         std     r0, VCPU_GPR(R0)(r9)
599         std     r1, VCPU_GPR(R1)(r9)
600         std     r2, VCPU_GPR(R2)(r9)
601         std     r3, VCPU_GPR(R3)(r9)
602         std     r4, VCPU_GPR(R4)(r9)
603         std     r5, VCPU_GPR(R5)(r9)
604         std     r6, VCPU_GPR(R6)(r9)
605         std     r7, VCPU_GPR(R7)(r9)
606         std     r8, VCPU_GPR(R8)(r9)
607         ld      r0, HSTATE_HOST_R2(r13)
608         std     r0, VCPU_GPR(R9)(r9)
609         std     r10, VCPU_GPR(R10)(r9)
610         std     r11, VCPU_GPR(R11)(r9)
611         ld      r3, HSTATE_SCRATCH0(r13)
612         lwz     r4, HSTATE_SCRATCH1(r13)
613         std     r3, VCPU_GPR(R12)(r9)
614         stw     r4, VCPU_CR(r9)
615 BEGIN_FTR_SECTION
616         ld      r3, HSTATE_CFAR(r13)
617         std     r3, VCPU_CFAR(r9)
618 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
619
620         /* Restore R1/R2 so we can handle faults */
621         ld      r1, HSTATE_HOST_R1(r13)
622         ld      r2, PACATOC(r13)
623
624         mfspr   r10, SPRN_SRR0
625         mfspr   r11, SPRN_SRR1
626         std     r10, VCPU_SRR0(r9)
627         std     r11, VCPU_SRR1(r9)
628         andi.   r0, r12, 2              /* need to read HSRR0/1? */
629         beq     1f
630         mfspr   r10, SPRN_HSRR0
631         mfspr   r11, SPRN_HSRR1
632         clrrdi  r12, r12, 2
633 1:      std     r10, VCPU_PC(r9)
634         std     r11, VCPU_MSR(r9)
635
636         GET_SCRATCH0(r3)
637         mflr    r4
638         std     r3, VCPU_GPR(R13)(r9)
639         std     r4, VCPU_LR(r9)
640
641         /* Unset guest mode */
642         li      r0, KVM_GUEST_MODE_NONE
643         stb     r0, HSTATE_IN_GUEST(r13)
644
645         stw     r12,VCPU_TRAP(r9)
646
647         /* Save HEIR (HV emulation assist reg) in last_inst
648            if this is an HEI (HV emulation interrupt, e40) */
649         li      r3,KVM_INST_FETCH_FAILED
650 BEGIN_FTR_SECTION
651         cmpwi   r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
652         bne     11f
653         mfspr   r3,SPRN_HEIR
654 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
655 11:     stw     r3,VCPU_LAST_INST(r9)
656
657         /* these are volatile across C function calls */
658         mfctr   r3
659         mfxer   r4
660         std     r3, VCPU_CTR(r9)
661         stw     r4, VCPU_XER(r9)
662
663 BEGIN_FTR_SECTION
664         /* If this is a page table miss then see if it's theirs or ours */
665         cmpwi   r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
666         beq     kvmppc_hdsi
667         cmpwi   r12, BOOK3S_INTERRUPT_H_INST_STORAGE
668         beq     kvmppc_hisi
669 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
670
671         /* See if this is a leftover HDEC interrupt */
672         cmpwi   r12,BOOK3S_INTERRUPT_HV_DECREMENTER
673         bne     2f
674         mfspr   r3,SPRN_HDEC
675         cmpwi   r3,0
676         bge     ignore_hdec
677 2:
678         /* See if this is an hcall we can handle in real mode */
679         cmpwi   r12,BOOK3S_INTERRUPT_SYSCALL
680         beq     hcall_try_real_mode
681
682         /* Only handle external interrupts here on arch 206 and later */
683 BEGIN_FTR_SECTION
684         b       ext_interrupt_to_host
685 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
686
687         /* External interrupt ? */
688         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
689         bne+    ext_interrupt_to_host
690
691         /* External interrupt, first check for host_ipi. If this is
692          * set, we know the host wants us out so let's do it now
693          */
694 do_ext_interrupt:
695         lbz     r0, HSTATE_HOST_IPI(r13)
696         cmpwi   r0, 0
697         bne     ext_interrupt_to_host
698
699         /* Now read the interrupt from the ICP */
700         ld      r5, HSTATE_XICS_PHYS(r13)
701         li      r7, XICS_XIRR
702         cmpdi   r5, 0
703         beq-    ext_interrupt_to_host
704         lwzcix  r3, r5, r7
705         rlwinm. r0, r3, 0, 0xffffff
706         sync
707         beq     3f              /* if nothing pending in the ICP */
708
709         /* We found something in the ICP...
710          *
711          * If it's not an IPI, stash it in the PACA and return to
712          * the host, we don't (yet) handle directing real external
713          * interrupts directly to the guest
714          */
715         cmpwi   r0, XICS_IPI
716         bne     ext_stash_for_host
717
718         /* It's an IPI, clear the MFRR and EOI it */
719         li      r0, 0xff
720         li      r6, XICS_MFRR
721         stbcix  r0, r5, r6              /* clear the IPI */
722         stwcix  r3, r5, r7              /* EOI it */
723         sync
724
725         /* We need to re-check host IPI now in case it got set in the
726          * meantime. If it's clear, we bounce the interrupt to the
727          * guest
728          */
729         lbz     r0, HSTATE_HOST_IPI(r13)
730         cmpwi   r0, 0
731         bne-    1f
732
733         /* Allright, looks like an IPI for the guest, we need to set MER */
734 3:
735         /* Check if any CPU is heading out to the host, if so head out too */
736         ld      r5, HSTATE_KVM_VCORE(r13)
737         lwz     r0, VCORE_ENTRY_EXIT(r5)
738         cmpwi   r0, 0x100
739         bge     ext_interrupt_to_host
740
741         /* See if there is a pending interrupt for the guest */
742         mfspr   r8, SPRN_LPCR
743         ld      r0, VCPU_PENDING_EXC(r9)
744         /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
745         rldicl. r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
746         rldimi  r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
747         beq     2f
748
749         /* And if the guest EE is set, we can deliver immediately, else
750          * we return to the guest with MER set
751          */
752         andi.   r0, r11, MSR_EE
753         beq     2f
754         mtspr   SPRN_SRR0, r10
755         mtspr   SPRN_SRR1, r11
756         li      r10, BOOK3S_INTERRUPT_EXTERNAL
757         li      r11, (MSR_ME << 1) | 1  /* synthesize MSR_SF | MSR_ME */
758         rotldi  r11, r11, 63
759 2:      mr      r4, r9
760         mtspr   SPRN_LPCR, r8
761         b       fast_guest_return
762
763         /* We raced with the host, we need to resend that IPI, bummer */
764 1:      li      r0, IPI_PRIORITY
765         stbcix  r0, r5, r6              /* set the IPI */
766         sync
767         b       ext_interrupt_to_host
768
769 ext_stash_for_host:
770         /* It's not an IPI and it's for the host, stash it in the PACA
771          * before exit, it will be picked up by the host ICP driver
772          */
773         stw     r3, HSTATE_SAVED_XIRR(r13)
774 ext_interrupt_to_host:
775
776 guest_exit_cont:                /* r9 = vcpu, r12 = trap, r13 = paca */
777         /* Save DEC */
778         mfspr   r5,SPRN_DEC
779         mftb    r6
780         extsw   r5,r5
781         add     r5,r5,r6
782         std     r5,VCPU_DEC_EXPIRES(r9)
783
784         /* Save more register state  */
785         mfdar   r6
786         mfdsisr r7
787         std     r6, VCPU_DAR(r9)
788         stw     r7, VCPU_DSISR(r9)
789 BEGIN_FTR_SECTION
790         /* don't overwrite fault_dar/fault_dsisr if HDSI */
791         cmpwi   r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
792         beq     6f
793 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
794         std     r6, VCPU_FAULT_DAR(r9)
795         stw     r7, VCPU_FAULT_DSISR(r9)
796
797         /* See if it is a machine check */
798         cmpwi   r12, BOOK3S_INTERRUPT_MACHINE_CHECK
799         beq     machine_check_realmode
800 mc_cont:
801
802         /* Save guest CTRL register, set runlatch to 1 */
803 6:      mfspr   r6,SPRN_CTRLF
804         stw     r6,VCPU_CTRL(r9)
805         andi.   r0,r6,1
806         bne     4f
807         ori     r6,r6,1
808         mtspr   SPRN_CTRLT,r6
809 4:
810         /* Read the guest SLB and save it away */
811         lwz     r0,VCPU_SLB_NR(r9)      /* number of entries in SLB */
812         mtctr   r0
813         li      r6,0
814         addi    r7,r9,VCPU_SLB
815         li      r5,0
816 1:      slbmfee r8,r6
817         andis.  r0,r8,SLB_ESID_V@h
818         beq     2f
819         add     r8,r8,r6                /* put index in */
820         slbmfev r3,r6
821         std     r8,VCPU_SLB_E(r7)
822         std     r3,VCPU_SLB_V(r7)
823         addi    r7,r7,VCPU_SLB_SIZE
824         addi    r5,r5,1
825 2:      addi    r6,r6,1
826         bdnz    1b
827         stw     r5,VCPU_SLB_MAX(r9)
828
829         /*
830          * Save the guest PURR/SPURR
831          */
832 BEGIN_FTR_SECTION
833         mfspr   r5,SPRN_PURR
834         mfspr   r6,SPRN_SPURR
835         ld      r7,VCPU_PURR(r9)
836         ld      r8,VCPU_SPURR(r9)
837         std     r5,VCPU_PURR(r9)
838         std     r6,VCPU_SPURR(r9)
839         subf    r5,r7,r5
840         subf    r6,r8,r6
841
842         /*
843          * Restore host PURR/SPURR and add guest times
844          * so that the time in the guest gets accounted.
845          */
846         ld      r3,HSTATE_PURR(r13)
847         ld      r4,HSTATE_SPURR(r13)
848         add     r3,r3,r5
849         add     r4,r4,r6
850         mtspr   SPRN_PURR,r3
851         mtspr   SPRN_SPURR,r4
852 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
853
854         /* Clear out SLB */
855         li      r5,0
856         slbmte  r5,r5
857         slbia
858         ptesync
859
860 hdec_soon:                      /* r9 = vcpu, r12 = trap, r13 = paca */
861 BEGIN_FTR_SECTION
862         b       32f
863 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
864         /*
865          * POWER7 guest -> host partition switch code.
866          * We don't have to lock against tlbies but we do
867          * have to coordinate the hardware threads.
868          */
869         /* Increment the threads-exiting-guest count in the 0xff00
870            bits of vcore->entry_exit_count */
871         lwsync
872         ld      r5,HSTATE_KVM_VCORE(r13)
873         addi    r6,r5,VCORE_ENTRY_EXIT
874 41:     lwarx   r3,0,r6
875         addi    r0,r3,0x100
876         stwcx.  r0,0,r6
877         bne     41b
878         lwsync
879
880         /*
881          * At this point we have an interrupt that we have to pass
882          * up to the kernel or qemu; we can't handle it in real mode.
883          * Thus we have to do a partition switch, so we have to
884          * collect the other threads, if we are the first thread
885          * to take an interrupt.  To do this, we set the HDEC to 0,
886          * which causes an HDEC interrupt in all threads within 2ns
887          * because the HDEC register is shared between all 4 threads.
888          * However, we don't need to bother if this is an HDEC
889          * interrupt, since the other threads will already be on their
890          * way here in that case.
891          */
892         cmpwi   r3,0x100        /* Are we the first here? */
893         bge     43f
894         cmpwi   r3,1            /* Are any other threads in the guest? */
895         ble     43f
896         cmpwi   r12,BOOK3S_INTERRUPT_HV_DECREMENTER
897         beq     40f
898         li      r0,0
899         mtspr   SPRN_HDEC,r0
900 40:
901         /*
902          * Send an IPI to any napping threads, since an HDEC interrupt
903          * doesn't wake CPUs up from nap.
904          */
905         lwz     r3,VCORE_NAPPING_THREADS(r5)
906         lwz     r4,VCPU_PTID(r9)
907         li      r0,1
908         sld     r0,r0,r4
909         andc.   r3,r3,r0                /* no sense IPI'ing ourselves */
910         beq     43f
911         mulli   r4,r4,PACA_SIZE         /* get paca for thread 0 */
912         subf    r6,r4,r13
913 42:     andi.   r0,r3,1
914         beq     44f
915         ld      r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
916         li      r0,IPI_PRIORITY
917         li      r7,XICS_MFRR
918         stbcix  r0,r7,r8                /* trigger the IPI */
919 44:     srdi.   r3,r3,1
920         addi    r6,r6,PACA_SIZE
921         bne     42b
922
923         /* Secondary threads wait for primary to do partition switch */
924 43:     ld      r4,VCPU_KVM(r9)         /* pointer to struct kvm */
925         ld      r5,HSTATE_KVM_VCORE(r13)
926         lwz     r3,VCPU_PTID(r9)
927         cmpwi   r3,0
928         beq     15f
929         HMT_LOW
930 13:     lbz     r3,VCORE_IN_GUEST(r5)
931         cmpwi   r3,0
932         bne     13b
933         HMT_MEDIUM
934         b       16f
935
936         /* Primary thread waits for all the secondaries to exit guest */
937 15:     lwz     r3,VCORE_ENTRY_EXIT(r5)
938         srwi    r0,r3,8
939         clrldi  r3,r3,56
940         cmpw    r3,r0
941         bne     15b
942         isync
943
944         /* Primary thread switches back to host partition */
945         ld      r6,KVM_HOST_SDR1(r4)
946         lwz     r7,KVM_HOST_LPID(r4)
947         li      r8,LPID_RSVD            /* switch to reserved LPID */
948         mtspr   SPRN_LPID,r8
949         ptesync
950         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
951         mtspr   SPRN_LPID,r7
952         isync
953         li      r0,0
954         stb     r0,VCORE_IN_GUEST(r5)
955         lis     r8,0x7fff               /* MAX_INT@h */
956         mtspr   SPRN_HDEC,r8
957
958 16:     ld      r8,KVM_HOST_LPCR(r4)
959         mtspr   SPRN_LPCR,r8
960         isync
961         b       33f
962
963         /*
964          * PPC970 guest -> host partition switch code.
965          * We have to lock against concurrent tlbies, and
966          * we have to flush the whole TLB.
967          */
968 32:     ld      r4,VCPU_KVM(r9)         /* pointer to struct kvm */
969
970         /* Take the guest's tlbie_lock */
971         lwz     r8,PACA_LOCK_TOKEN(r13)
972         addi    r3,r4,KVM_TLBIE_LOCK
973 24:     lwarx   r0,0,r3
974         cmpwi   r0,0
975         bne     24b
976         stwcx.  r8,0,r3
977         bne     24b
978         isync
979
980         ld      r7,KVM_HOST_LPCR(r4)    /* use kvm->arch.host_lpcr for HID4 */
981         li      r0,0x18f
982         rotldi  r0,r0,HID4_LPID5_SH     /* all lpid bits in HID4 = 1 */
983         or      r0,r7,r0
984         ptesync
985         sync
986         mtspr   SPRN_HID4,r0            /* switch to reserved LPID */
987         isync
988         li      r0,0
989         stw     r0,0(r3)                /* drop guest tlbie_lock */
990
991         /* invalidate the whole TLB */
992         li      r0,256
993         mtctr   r0
994         li      r6,0
995 25:     tlbiel  r6
996         addi    r6,r6,0x1000
997         bdnz    25b
998         ptesync
999
1000         /* take native_tlbie_lock */
1001         ld      r3,toc_tlbie_lock@toc(2)
1002 24:     lwarx   r0,0,r3
1003         cmpwi   r0,0
1004         bne     24b
1005         stwcx.  r8,0,r3
1006         bne     24b
1007         isync
1008
1009         ld      r6,KVM_HOST_SDR1(r4)
1010         mtspr   SPRN_SDR1,r6            /* switch to host page table */
1011
1012         /* Set up host HID4 value */
1013         sync
1014         mtspr   SPRN_HID4,r7
1015         isync
1016         li      r0,0
1017         stw     r0,0(r3)                /* drop native_tlbie_lock */
1018
1019         lis     r8,0x7fff               /* MAX_INT@h */
1020         mtspr   SPRN_HDEC,r8
1021
1022         /* Disable HDEC interrupts */
1023         mfspr   r0,SPRN_HID0
1024         li      r3,0
1025         rldimi  r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1026         sync
1027         mtspr   SPRN_HID0,r0
1028         mfspr   r0,SPRN_HID0
1029         mfspr   r0,SPRN_HID0
1030         mfspr   r0,SPRN_HID0
1031         mfspr   r0,SPRN_HID0
1032         mfspr   r0,SPRN_HID0
1033         mfspr   r0,SPRN_HID0
1034
1035         /* load host SLB entries */
1036 33:     ld      r8,PACA_SLBSHADOWPTR(r13)
1037
1038         .rept   SLB_NUM_BOLTED
1039         ld      r5,SLBSHADOW_SAVEAREA(r8)
1040         ld      r6,SLBSHADOW_SAVEAREA+8(r8)
1041         andis.  r7,r5,SLB_ESID_V@h
1042         beq     1f
1043         slbmte  r6,r5
1044 1:      addi    r8,r8,16
1045         .endr
1046
1047         /* Save and reset AMR and UAMOR before turning on the MMU */
1048 BEGIN_FTR_SECTION
1049         mfspr   r5,SPRN_AMR
1050         mfspr   r6,SPRN_UAMOR
1051         std     r5,VCPU_AMR(r9)
1052         std     r6,VCPU_UAMOR(r9)
1053         li      r6,0
1054         mtspr   SPRN_AMR,r6
1055 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1056
1057         /* Switch DSCR back to host value */
1058 BEGIN_FTR_SECTION
1059         mfspr   r8, SPRN_DSCR
1060         ld      r7, HSTATE_DSCR(r13)
1061         std     r8, VCPU_DSCR(r7)
1062         mtspr   SPRN_DSCR, r7
1063 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1064
1065         /* Save non-volatile GPRs */
1066         std     r14, VCPU_GPR(R14)(r9)
1067         std     r15, VCPU_GPR(R15)(r9)
1068         std     r16, VCPU_GPR(R16)(r9)
1069         std     r17, VCPU_GPR(R17)(r9)
1070         std     r18, VCPU_GPR(R18)(r9)
1071         std     r19, VCPU_GPR(R19)(r9)
1072         std     r20, VCPU_GPR(R20)(r9)
1073         std     r21, VCPU_GPR(R21)(r9)
1074         std     r22, VCPU_GPR(R22)(r9)
1075         std     r23, VCPU_GPR(R23)(r9)
1076         std     r24, VCPU_GPR(R24)(r9)
1077         std     r25, VCPU_GPR(R25)(r9)
1078         std     r26, VCPU_GPR(R26)(r9)
1079         std     r27, VCPU_GPR(R27)(r9)
1080         std     r28, VCPU_GPR(R28)(r9)
1081         std     r29, VCPU_GPR(R29)(r9)
1082         std     r30, VCPU_GPR(R30)(r9)
1083         std     r31, VCPU_GPR(R31)(r9)
1084
1085         /* Save SPRGs */
1086         mfspr   r3, SPRN_SPRG0
1087         mfspr   r4, SPRN_SPRG1
1088         mfspr   r5, SPRN_SPRG2
1089         mfspr   r6, SPRN_SPRG3
1090         std     r3, VCPU_SPRG0(r9)
1091         std     r4, VCPU_SPRG1(r9)
1092         std     r5, VCPU_SPRG2(r9)
1093         std     r6, VCPU_SPRG3(r9)
1094
1095         /* save FP state */
1096         mr      r3, r9
1097         bl      .kvmppc_save_fp
1098
1099         /* Increment yield count if they have a VPA */
1100         ld      r8, VCPU_VPA(r9)        /* do they have a VPA? */
1101         cmpdi   r8, 0
1102         beq     25f
1103         lwz     r3, LPPACA_YIELDCOUNT(r8)
1104         addi    r3, r3, 1
1105         stw     r3, LPPACA_YIELDCOUNT(r8)
1106         li      r3, 1
1107         stb     r3, VCPU_VPA_DIRTY(r9)
1108 25:
1109         /* Save PMU registers if requested */
1110         /* r8 and cr0.eq are live here */
1111         li      r3, 1
1112         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
1113         mfspr   r4, SPRN_MMCR0          /* save MMCR0 */
1114         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
1115         mfspr   r6, SPRN_MMCRA
1116 BEGIN_FTR_SECTION
1117         /* On P7, clear MMCRA in order to disable SDAR updates */
1118         li      r7, 0
1119         mtspr   SPRN_MMCRA, r7
1120 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1121         isync
1122         beq     21f                     /* if no VPA, save PMU stuff anyway */
1123         lbz     r7, LPPACA_PMCINUSE(r8)
1124         cmpwi   r7, 0                   /* did they ask for PMU stuff to be saved? */
1125         bne     21f
1126         std     r3, VCPU_MMCR(r9)       /* if not, set saved MMCR0 to FC */
1127         b       22f
1128 21:     mfspr   r5, SPRN_MMCR1
1129         std     r4, VCPU_MMCR(r9)
1130         std     r5, VCPU_MMCR + 8(r9)
1131         std     r6, VCPU_MMCR + 16(r9)
1132         mfspr   r3, SPRN_PMC1
1133         mfspr   r4, SPRN_PMC2
1134         mfspr   r5, SPRN_PMC3
1135         mfspr   r6, SPRN_PMC4
1136         mfspr   r7, SPRN_PMC5
1137         mfspr   r8, SPRN_PMC6
1138 BEGIN_FTR_SECTION
1139         mfspr   r10, SPRN_PMC7
1140         mfspr   r11, SPRN_PMC8
1141 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1142         stw     r3, VCPU_PMC(r9)
1143         stw     r4, VCPU_PMC + 4(r9)
1144         stw     r5, VCPU_PMC + 8(r9)
1145         stw     r6, VCPU_PMC + 12(r9)
1146         stw     r7, VCPU_PMC + 16(r9)
1147         stw     r8, VCPU_PMC + 20(r9)
1148 BEGIN_FTR_SECTION
1149         stw     r10, VCPU_PMC + 24(r9)
1150         stw     r11, VCPU_PMC + 28(r9)
1151 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1152 22:
1153
1154         /* Secondary threads go off to take a nap on POWER7 */
1155 BEGIN_FTR_SECTION
1156         lwz     r0,VCPU_PTID(r9)
1157         cmpwi   r0,0
1158         bne     secondary_nap
1159 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1160
1161         /* Restore host DABR and DABRX */
1162         ld      r5,HSTATE_DABR(r13)
1163         li      r6,7
1164         mtspr   SPRN_DABR,r5
1165         mtspr   SPRN_DABRX,r6
1166
1167         /* Restore SPRG3 */
1168         ld      r3,PACA_SPRG3(r13)
1169         mtspr   SPRN_SPRG3,r3
1170
1171         /*
1172          * Reload DEC.  HDEC interrupts were disabled when
1173          * we reloaded the host's LPCR value.
1174          */
1175         ld      r3, HSTATE_DECEXP(r13)
1176         mftb    r4
1177         subf    r4, r4, r3
1178         mtspr   SPRN_DEC, r4
1179
1180         /* Reload the host's PMU registers */
1181         ld      r3, PACALPPACAPTR(r13)  /* is the host using the PMU? */
1182         lbz     r4, LPPACA_PMCINUSE(r3)
1183         cmpwi   r4, 0
1184         beq     23f                     /* skip if not */
1185         lwz     r3, HSTATE_PMC(r13)
1186         lwz     r4, HSTATE_PMC + 4(r13)
1187         lwz     r5, HSTATE_PMC + 8(r13)
1188         lwz     r6, HSTATE_PMC + 12(r13)
1189         lwz     r8, HSTATE_PMC + 16(r13)
1190         lwz     r9, HSTATE_PMC + 20(r13)
1191 BEGIN_FTR_SECTION
1192         lwz     r10, HSTATE_PMC + 24(r13)
1193         lwz     r11, HSTATE_PMC + 28(r13)
1194 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1195         mtspr   SPRN_PMC1, r3
1196         mtspr   SPRN_PMC2, r4
1197         mtspr   SPRN_PMC3, r5
1198         mtspr   SPRN_PMC4, r6
1199         mtspr   SPRN_PMC5, r8
1200         mtspr   SPRN_PMC6, r9
1201 BEGIN_FTR_SECTION
1202         mtspr   SPRN_PMC7, r10
1203         mtspr   SPRN_PMC8, r11
1204 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1205         ld      r3, HSTATE_MMCR(r13)
1206         ld      r4, HSTATE_MMCR + 8(r13)
1207         ld      r5, HSTATE_MMCR + 16(r13)
1208         mtspr   SPRN_MMCR1, r4
1209         mtspr   SPRN_MMCRA, r5
1210         mtspr   SPRN_MMCR0, r3
1211         isync
1212 23:
1213         /*
1214          * For external and machine check interrupts, we need
1215          * to call the Linux handler to process the interrupt.
1216          * We do that by jumping to absolute address 0x500 for
1217          * external interrupts, or the machine_check_fwnmi label
1218          * for machine checks (since firmware might have patched
1219          * the vector area at 0x200).  The [h]rfid at the end of the
1220          * handler will return to the book3s_hv_interrupts.S code.
1221          * For other interrupts we do the rfid to get back
1222          * to the book3s_hv_interrupts.S code here.
1223          */
1224         ld      r8, HSTATE_VMHANDLER(r13)
1225         ld      r7, HSTATE_HOST_MSR(r13)
1226
1227         cmpwi   cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1228         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
1229 BEGIN_FTR_SECTION
1230         beq     11f
1231 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1232
1233         /* RFI into the highmem handler, or branch to interrupt handler */
1234         mfmsr   r6
1235         li      r0, MSR_RI
1236         andc    r6, r6, r0
1237         mtmsrd  r6, 1                   /* Clear RI in MSR */
1238         mtsrr0  r8
1239         mtsrr1  r7
1240         beqa    0x500                   /* external interrupt (PPC970) */
1241         beq     cr1, 13f                /* machine check */
1242         RFI
1243
1244         /* On POWER7, we have external interrupts set to use HSRR0/1 */
1245 11:     mtspr   SPRN_HSRR0, r8
1246         mtspr   SPRN_HSRR1, r7
1247         ba      0x500
1248
1249 13:     b       machine_check_fwnmi
1250
1251 /*
1252  * Check whether an HDSI is an HPTE not found fault or something else.
1253  * If it is an HPTE not found fault that is due to the guest accessing
1254  * a page that they have mapped but which we have paged out, then
1255  * we continue on with the guest exit path.  In all other cases,
1256  * reflect the HDSI to the guest as a DSI.
1257  */
1258 kvmppc_hdsi:
1259         mfspr   r4, SPRN_HDAR
1260         mfspr   r6, SPRN_HDSISR
1261         /* HPTE not found fault or protection fault? */
1262         andis.  r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1263         beq     1f                      /* if not, send it to the guest */
1264         andi.   r0, r11, MSR_DR         /* data relocation enabled? */
1265         beq     3f
1266         clrrdi  r0, r4, 28
1267         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1268         bne     1f                      /* if no SLB entry found */
1269 4:      std     r4, VCPU_FAULT_DAR(r9)
1270         stw     r6, VCPU_FAULT_DSISR(r9)
1271
1272         /* Search the hash table. */
1273         mr      r3, r9                  /* vcpu pointer */
1274         li      r7, 1                   /* data fault */
1275         bl      .kvmppc_hpte_hv_fault
1276         ld      r9, HSTATE_KVM_VCPU(r13)
1277         ld      r10, VCPU_PC(r9)
1278         ld      r11, VCPU_MSR(r9)
1279         li      r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1280         cmpdi   r3, 0                   /* retry the instruction */
1281         beq     6f
1282         cmpdi   r3, -1                  /* handle in kernel mode */
1283         beq     guest_exit_cont
1284         cmpdi   r3, -2                  /* MMIO emulation; need instr word */
1285         beq     2f
1286
1287         /* Synthesize a DSI for the guest */
1288         ld      r4, VCPU_FAULT_DAR(r9)
1289         mr      r6, r3
1290 1:      mtspr   SPRN_DAR, r4
1291         mtspr   SPRN_DSISR, r6
1292         mtspr   SPRN_SRR0, r10
1293         mtspr   SPRN_SRR1, r11
1294         li      r10, BOOK3S_INTERRUPT_DATA_STORAGE
1295         li      r11, (MSR_ME << 1) | 1  /* synthesize MSR_SF | MSR_ME */
1296         rotldi  r11, r11, 63
1297 fast_interrupt_c_return:
1298 6:      ld      r7, VCPU_CTR(r9)
1299         lwz     r8, VCPU_XER(r9)
1300         mtctr   r7
1301         mtxer   r8
1302         mr      r4, r9
1303         b       fast_guest_return
1304
1305 3:      ld      r5, VCPU_KVM(r9)        /* not relocated, use VRMA */
1306         ld      r5, KVM_VRMA_SLB_V(r5)
1307         b       4b
1308
1309         /* If this is for emulated MMIO, load the instruction word */
1310 2:      li      r8, KVM_INST_FETCH_FAILED       /* In case lwz faults */
1311
1312         /* Set guest mode to 'jump over instruction' so if lwz faults
1313          * we'll just continue at the next IP. */
1314         li      r0, KVM_GUEST_MODE_SKIP
1315         stb     r0, HSTATE_IN_GUEST(r13)
1316
1317         /* Do the access with MSR:DR enabled */
1318         mfmsr   r3
1319         ori     r4, r3, MSR_DR          /* Enable paging for data */
1320         mtmsrd  r4
1321         lwz     r8, 0(r10)
1322         mtmsrd  r3
1323
1324         /* Store the result */
1325         stw     r8, VCPU_LAST_INST(r9)
1326
1327         /* Unset guest mode. */
1328         li      r0, KVM_GUEST_MODE_NONE
1329         stb     r0, HSTATE_IN_GUEST(r13)
1330         b       guest_exit_cont
1331
1332 /*
1333  * Similarly for an HISI, reflect it to the guest as an ISI unless
1334  * it is an HPTE not found fault for a page that we have paged out.
1335  */
1336 kvmppc_hisi:
1337         andis.  r0, r11, SRR1_ISI_NOPT@h
1338         beq     1f
1339         andi.   r0, r11, MSR_IR         /* instruction relocation enabled? */
1340         beq     3f
1341         clrrdi  r0, r10, 28
1342         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1343         bne     1f                      /* if no SLB entry found */
1344 4:
1345         /* Search the hash table. */
1346         mr      r3, r9                  /* vcpu pointer */
1347         mr      r4, r10
1348         mr      r6, r11
1349         li      r7, 0                   /* instruction fault */
1350         bl      .kvmppc_hpte_hv_fault
1351         ld      r9, HSTATE_KVM_VCPU(r13)
1352         ld      r10, VCPU_PC(r9)
1353         ld      r11, VCPU_MSR(r9)
1354         li      r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1355         cmpdi   r3, 0                   /* retry the instruction */
1356         beq     fast_interrupt_c_return
1357         cmpdi   r3, -1                  /* handle in kernel mode */
1358         beq     guest_exit_cont
1359
1360         /* Synthesize an ISI for the guest */
1361         mr      r11, r3
1362 1:      mtspr   SPRN_SRR0, r10
1363         mtspr   SPRN_SRR1, r11
1364         li      r10, BOOK3S_INTERRUPT_INST_STORAGE
1365         li      r11, (MSR_ME << 1) | 1  /* synthesize MSR_SF | MSR_ME */
1366         rotldi  r11, r11, 63
1367         b       fast_interrupt_c_return
1368
1369 3:      ld      r6, VCPU_KVM(r9)        /* not relocated, use VRMA */
1370         ld      r5, KVM_VRMA_SLB_V(r6)
1371         b       4b
1372
1373 /*
1374  * Try to handle an hcall in real mode.
1375  * Returns to the guest if we handle it, or continues on up to
1376  * the kernel if we can't (i.e. if we don't have a handler for
1377  * it, or if the handler returns H_TOO_HARD).
1378  */
1379         .globl  hcall_try_real_mode
1380 hcall_try_real_mode:
1381         ld      r3,VCPU_GPR(R3)(r9)
1382         andi.   r0,r11,MSR_PR
1383         bne     guest_exit_cont
1384         clrrdi  r3,r3,2
1385         cmpldi  r3,hcall_real_table_end - hcall_real_table
1386         bge     guest_exit_cont
1387         LOAD_REG_ADDR(r4, hcall_real_table)
1388         lwzx    r3,r3,r4
1389         cmpwi   r3,0
1390         beq     guest_exit_cont
1391         add     r3,r3,r4
1392         mtctr   r3
1393         mr      r3,r9           /* get vcpu pointer */
1394         ld      r4,VCPU_GPR(R4)(r9)
1395         bctrl
1396         cmpdi   r3,H_TOO_HARD
1397         beq     hcall_real_fallback
1398         ld      r4,HSTATE_KVM_VCPU(r13)
1399         std     r3,VCPU_GPR(R3)(r4)
1400         ld      r10,VCPU_PC(r4)
1401         ld      r11,VCPU_MSR(r4)
1402         b       fast_guest_return
1403
1404         /* We've attempted a real mode hcall, but it's punted it back
1405          * to userspace.  We need to restore some clobbered volatiles
1406          * before resuming the pass-it-to-qemu path */
1407 hcall_real_fallback:
1408         li      r12,BOOK3S_INTERRUPT_SYSCALL
1409         ld      r9, HSTATE_KVM_VCPU(r13)
1410
1411         b       guest_exit_cont
1412
1413         .globl  hcall_real_table
1414 hcall_real_table:
1415         .long   0               /* 0 - unused */
1416         .long   .kvmppc_h_remove - hcall_real_table
1417         .long   .kvmppc_h_enter - hcall_real_table
1418         .long   .kvmppc_h_read - hcall_real_table
1419         .long   0               /* 0x10 - H_CLEAR_MOD */
1420         .long   0               /* 0x14 - H_CLEAR_REF */
1421         .long   .kvmppc_h_protect - hcall_real_table
1422         .long   0               /* 0x1c - H_GET_TCE */
1423         .long   .kvmppc_h_put_tce - hcall_real_table
1424         .long   0               /* 0x24 - H_SET_SPRG0 */
1425         .long   .kvmppc_h_set_dabr - hcall_real_table
1426         .long   0               /* 0x2c */
1427         .long   0               /* 0x30 */
1428         .long   0               /* 0x34 */
1429         .long   0               /* 0x38 */
1430         .long   0               /* 0x3c */
1431         .long   0               /* 0x40 */
1432         .long   0               /* 0x44 */
1433         .long   0               /* 0x48 */
1434         .long   0               /* 0x4c */
1435         .long   0               /* 0x50 */
1436         .long   0               /* 0x54 */
1437         .long   0               /* 0x58 */
1438         .long   0               /* 0x5c */
1439         .long   0               /* 0x60 */
1440 #ifdef CONFIG_KVM_XICS
1441         .long   .kvmppc_rm_h_eoi - hcall_real_table
1442         .long   .kvmppc_rm_h_cppr - hcall_real_table
1443         .long   .kvmppc_rm_h_ipi - hcall_real_table
1444         .long   0               /* 0x70 - H_IPOLL */
1445         .long   .kvmppc_rm_h_xirr - hcall_real_table
1446 #else
1447         .long   0               /* 0x64 - H_EOI */
1448         .long   0               /* 0x68 - H_CPPR */
1449         .long   0               /* 0x6c - H_IPI */
1450         .long   0               /* 0x70 - H_IPOLL */
1451         .long   0               /* 0x74 - H_XIRR */
1452 #endif
1453         .long   0               /* 0x78 */
1454         .long   0               /* 0x7c */
1455         .long   0               /* 0x80 */
1456         .long   0               /* 0x84 */
1457         .long   0               /* 0x88 */
1458         .long   0               /* 0x8c */
1459         .long   0               /* 0x90 */
1460         .long   0               /* 0x94 */
1461         .long   0               /* 0x98 */
1462         .long   0               /* 0x9c */
1463         .long   0               /* 0xa0 */
1464         .long   0               /* 0xa4 */
1465         .long   0               /* 0xa8 */
1466         .long   0               /* 0xac */
1467         .long   0               /* 0xb0 */
1468         .long   0               /* 0xb4 */
1469         .long   0               /* 0xb8 */
1470         .long   0               /* 0xbc */
1471         .long   0               /* 0xc0 */
1472         .long   0               /* 0xc4 */
1473         .long   0               /* 0xc8 */
1474         .long   0               /* 0xcc */
1475         .long   0               /* 0xd0 */
1476         .long   0               /* 0xd4 */
1477         .long   0               /* 0xd8 */
1478         .long   0               /* 0xdc */
1479         .long   .kvmppc_h_cede - hcall_real_table
1480         .long   0               /* 0xe4 */
1481         .long   0               /* 0xe8 */
1482         .long   0               /* 0xec */
1483         .long   0               /* 0xf0 */
1484         .long   0               /* 0xf4 */
1485         .long   0               /* 0xf8 */
1486         .long   0               /* 0xfc */
1487         .long   0               /* 0x100 */
1488         .long   0               /* 0x104 */
1489         .long   0               /* 0x108 */
1490         .long   0               /* 0x10c */
1491         .long   0               /* 0x110 */
1492         .long   0               /* 0x114 */
1493         .long   0               /* 0x118 */
1494         .long   0               /* 0x11c */
1495         .long   0               /* 0x120 */
1496         .long   .kvmppc_h_bulk_remove - hcall_real_table
1497 hcall_real_table_end:
1498
1499 ignore_hdec:
1500         mr      r4,r9
1501         b       fast_guest_return
1502
1503 _GLOBAL(kvmppc_h_set_dabr)
1504         std     r4,VCPU_DABR(r3)
1505         /* Work around P7 bug where DABR can get corrupted on mtspr */
1506 1:      mtspr   SPRN_DABR,r4
1507         mfspr   r5, SPRN_DABR
1508         cmpd    r4, r5
1509         bne     1b
1510         isync
1511         li      r3,0
1512         blr
1513
1514 _GLOBAL(kvmppc_h_cede)
1515         ori     r11,r11,MSR_EE
1516         std     r11,VCPU_MSR(r3)
1517         li      r0,1
1518         stb     r0,VCPU_CEDED(r3)
1519         sync                    /* order setting ceded vs. testing prodded */
1520         lbz     r5,VCPU_PRODDED(r3)
1521         cmpwi   r5,0
1522         bne     kvm_cede_prodded
1523         li      r0,0            /* set trap to 0 to say hcall is handled */
1524         stw     r0,VCPU_TRAP(r3)
1525         li      r0,H_SUCCESS
1526         std     r0,VCPU_GPR(R3)(r3)
1527 BEGIN_FTR_SECTION
1528         b       kvm_cede_exit   /* just send it up to host on 970 */
1529 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1530
1531         /*
1532          * Set our bit in the bitmask of napping threads unless all the
1533          * other threads are already napping, in which case we send this
1534          * up to the host.
1535          */
1536         ld      r5,HSTATE_KVM_VCORE(r13)
1537         lwz     r6,VCPU_PTID(r3)
1538         lwz     r8,VCORE_ENTRY_EXIT(r5)
1539         clrldi  r8,r8,56
1540         li      r0,1
1541         sld     r0,r0,r6
1542         addi    r6,r5,VCORE_NAPPING_THREADS
1543 31:     lwarx   r4,0,r6
1544         or      r4,r4,r0
1545         PPC_POPCNTW(R7,R4)
1546         cmpw    r7,r8
1547         bge     kvm_cede_exit
1548         stwcx.  r4,0,r6
1549         bne     31b
1550         li      r0,1
1551         stb     r0,HSTATE_NAPPING(r13)
1552         /* order napping_threads update vs testing entry_exit_count */
1553         lwsync
1554         mr      r4,r3
1555         lwz     r7,VCORE_ENTRY_EXIT(r5)
1556         cmpwi   r7,0x100
1557         bge     33f             /* another thread already exiting */
1558
1559 /*
1560  * Although not specifically required by the architecture, POWER7
1561  * preserves the following registers in nap mode, even if an SMT mode
1562  * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1563  * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1564  */
1565         /* Save non-volatile GPRs */
1566         std     r14, VCPU_GPR(R14)(r3)
1567         std     r15, VCPU_GPR(R15)(r3)
1568         std     r16, VCPU_GPR(R16)(r3)
1569         std     r17, VCPU_GPR(R17)(r3)
1570         std     r18, VCPU_GPR(R18)(r3)
1571         std     r19, VCPU_GPR(R19)(r3)
1572         std     r20, VCPU_GPR(R20)(r3)
1573         std     r21, VCPU_GPR(R21)(r3)
1574         std     r22, VCPU_GPR(R22)(r3)
1575         std     r23, VCPU_GPR(R23)(r3)
1576         std     r24, VCPU_GPR(R24)(r3)
1577         std     r25, VCPU_GPR(R25)(r3)
1578         std     r26, VCPU_GPR(R26)(r3)
1579         std     r27, VCPU_GPR(R27)(r3)
1580         std     r28, VCPU_GPR(R28)(r3)
1581         std     r29, VCPU_GPR(R29)(r3)
1582         std     r30, VCPU_GPR(R30)(r3)
1583         std     r31, VCPU_GPR(R31)(r3)
1584
1585         /* save FP state */
1586         bl      .kvmppc_save_fp
1587
1588         /*
1589          * Take a nap until a decrementer or external interrupt occurs,
1590          * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1591          */
1592         li      r0,1
1593         stb     r0,HSTATE_HWTHREAD_REQ(r13)
1594         mfspr   r5,SPRN_LPCR
1595         ori     r5,r5,LPCR_PECE0 | LPCR_PECE1
1596         mtspr   SPRN_LPCR,r5
1597         isync
1598         li      r0, 0
1599         std     r0, HSTATE_SCRATCH0(r13)
1600         ptesync
1601         ld      r0, HSTATE_SCRATCH0(r13)
1602 1:      cmpd    r0, r0
1603         bne     1b
1604         nap
1605         b       .
1606
1607 kvm_end_cede:
1608         /* get vcpu pointer */
1609         ld      r4, HSTATE_KVM_VCPU(r13)
1610
1611         /* Woken by external or decrementer interrupt */
1612         ld      r1, HSTATE_HOST_R1(r13)
1613
1614         /* load up FP state */
1615         bl      kvmppc_load_fp
1616
1617         /* Load NV GPRS */
1618         ld      r14, VCPU_GPR(R14)(r4)
1619         ld      r15, VCPU_GPR(R15)(r4)
1620         ld      r16, VCPU_GPR(R16)(r4)
1621         ld      r17, VCPU_GPR(R17)(r4)
1622         ld      r18, VCPU_GPR(R18)(r4)
1623         ld      r19, VCPU_GPR(R19)(r4)
1624         ld      r20, VCPU_GPR(R20)(r4)
1625         ld      r21, VCPU_GPR(R21)(r4)
1626         ld      r22, VCPU_GPR(R22)(r4)
1627         ld      r23, VCPU_GPR(R23)(r4)
1628         ld      r24, VCPU_GPR(R24)(r4)
1629         ld      r25, VCPU_GPR(R25)(r4)
1630         ld      r26, VCPU_GPR(R26)(r4)
1631         ld      r27, VCPU_GPR(R27)(r4)
1632         ld      r28, VCPU_GPR(R28)(r4)
1633         ld      r29, VCPU_GPR(R29)(r4)
1634         ld      r30, VCPU_GPR(R30)(r4)
1635         ld      r31, VCPU_GPR(R31)(r4)
1636
1637         /* clear our bit in vcore->napping_threads */
1638 33:     ld      r5,HSTATE_KVM_VCORE(r13)
1639         lwz     r3,VCPU_PTID(r4)
1640         li      r0,1
1641         sld     r0,r0,r3
1642         addi    r6,r5,VCORE_NAPPING_THREADS
1643 32:     lwarx   r7,0,r6
1644         andc    r7,r7,r0
1645         stwcx.  r7,0,r6
1646         bne     32b
1647         li      r0,0
1648         stb     r0,HSTATE_NAPPING(r13)
1649
1650         /* Check the wake reason in SRR1 to see why we got here */
1651         mfspr   r3, SPRN_SRR1
1652         rlwinm  r3, r3, 44-31, 0x7      /* extract wake reason field */
1653         cmpwi   r3, 4                   /* was it an external interrupt? */
1654         li      r12, BOOK3S_INTERRUPT_EXTERNAL
1655         mr      r9, r4
1656         ld      r10, VCPU_PC(r9)
1657         ld      r11, VCPU_MSR(r9)
1658         beq     do_ext_interrupt        /* if so */
1659
1660         /* see if any other thread is already exiting */
1661         lwz     r0,VCORE_ENTRY_EXIT(r5)
1662         cmpwi   r0,0x100
1663         blt     kvmppc_cede_reentry     /* if not go back to guest */
1664
1665         /* some threads are exiting, so go to the guest exit path */
1666         b       hcall_real_fallback
1667
1668         /* cede when already previously prodded case */
1669 kvm_cede_prodded:
1670         li      r0,0
1671         stb     r0,VCPU_PRODDED(r3)
1672         sync                    /* order testing prodded vs. clearing ceded */
1673         stb     r0,VCPU_CEDED(r3)
1674         li      r3,H_SUCCESS
1675         blr
1676
1677         /* we've ceded but we want to give control to the host */
1678 kvm_cede_exit:
1679         b       hcall_real_fallback
1680
1681         /* Try to handle a machine check in real mode */
1682 machine_check_realmode:
1683         mr      r3, r9          /* get vcpu pointer */
1684         bl      .kvmppc_realmode_machine_check
1685         nop
1686         cmpdi   r3, 0           /* continue exiting from guest? */
1687         ld      r9, HSTATE_KVM_VCPU(r13)
1688         li      r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1689         beq     mc_cont
1690         /* If not, deliver a machine check.  SRR0/1 are already set */
1691         li      r10, BOOK3S_INTERRUPT_MACHINE_CHECK
1692         li      r11, (MSR_ME << 1) | 1  /* synthesize MSR_SF | MSR_ME */
1693         rotldi  r11, r11, 63
1694         b       fast_interrupt_c_return
1695
1696 secondary_too_late:
1697         ld      r5,HSTATE_KVM_VCORE(r13)
1698         HMT_LOW
1699 13:     lbz     r3,VCORE_IN_GUEST(r5)
1700         cmpwi   r3,0
1701         bne     13b
1702         HMT_MEDIUM
1703         ld      r11,PACA_SLBSHADOWPTR(r13)
1704
1705         .rept   SLB_NUM_BOLTED
1706         ld      r5,SLBSHADOW_SAVEAREA(r11)
1707         ld      r6,SLBSHADOW_SAVEAREA+8(r11)
1708         andis.  r7,r5,SLB_ESID_V@h
1709         beq     1f
1710         slbmte  r6,r5
1711 1:      addi    r11,r11,16
1712         .endr
1713
1714 secondary_nap:
1715         /* Clear our vcpu pointer so we don't come back in early */
1716         li      r0, 0
1717         std     r0, HSTATE_KVM_VCPU(r13)
1718         lwsync
1719         /* Clear any pending IPI - assume we're a secondary thread */
1720         ld      r5, HSTATE_XICS_PHYS(r13)
1721         li      r7, XICS_XIRR
1722         lwzcix  r3, r5, r7              /* ack any pending interrupt */
1723         rlwinm. r0, r3, 0, 0xffffff     /* any pending? */
1724         beq     37f
1725         sync
1726         li      r0, 0xff
1727         li      r6, XICS_MFRR
1728         stbcix  r0, r5, r6              /* clear the IPI */
1729         stwcix  r3, r5, r7              /* EOI it */
1730 37:     sync
1731
1732         /* increment the nap count and then go to nap mode */
1733         ld      r4, HSTATE_KVM_VCORE(r13)
1734         addi    r4, r4, VCORE_NAP_COUNT
1735         lwsync                          /* make previous updates visible */
1736 51:     lwarx   r3, 0, r4
1737         addi    r3, r3, 1
1738         stwcx.  r3, 0, r4
1739         bne     51b
1740
1741 kvm_no_guest:
1742         li      r0, KVM_HWTHREAD_IN_NAP
1743         stb     r0, HSTATE_HWTHREAD_STATE(r13)
1744
1745         li      r3, LPCR_PECE0
1746         mfspr   r4, SPRN_LPCR
1747         rlwimi  r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
1748         mtspr   SPRN_LPCR, r4
1749         isync
1750         std     r0, HSTATE_SCRATCH0(r13)
1751         ptesync
1752         ld      r0, HSTATE_SCRATCH0(r13)
1753 1:      cmpd    r0, r0
1754         bne     1b
1755         nap
1756         b       .
1757
1758 /*
1759  * Save away FP, VMX and VSX registers.
1760  * r3 = vcpu pointer
1761  */
1762 _GLOBAL(kvmppc_save_fp)
1763         mfmsr   r5
1764         ori     r8,r5,MSR_FP
1765 #ifdef CONFIG_ALTIVEC
1766 BEGIN_FTR_SECTION
1767         oris    r8,r8,MSR_VEC@h
1768 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1769 #endif
1770 #ifdef CONFIG_VSX
1771 BEGIN_FTR_SECTION
1772         oris    r8,r8,MSR_VSX@h
1773 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1774 #endif
1775         mtmsrd  r8
1776         isync
1777 #ifdef CONFIG_VSX
1778 BEGIN_FTR_SECTION
1779         reg = 0
1780         .rept   32
1781         li      r6,reg*16+VCPU_VSRS
1782         STXVD2X(reg,R6,R3)
1783         reg = reg + 1
1784         .endr
1785 FTR_SECTION_ELSE
1786 #endif
1787         reg = 0
1788         .rept   32
1789         stfd    reg,reg*8+VCPU_FPRS(r3)
1790         reg = reg + 1
1791         .endr
1792 #ifdef CONFIG_VSX
1793 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1794 #endif
1795         mffs    fr0
1796         stfd    fr0,VCPU_FPSCR(r3)
1797
1798 #ifdef CONFIG_ALTIVEC
1799 BEGIN_FTR_SECTION
1800         reg = 0
1801         .rept   32
1802         li      r6,reg*16+VCPU_VRS
1803         stvx    reg,r6,r3
1804         reg = reg + 1
1805         .endr
1806         mfvscr  vr0
1807         li      r6,VCPU_VSCR
1808         stvx    vr0,r6,r3
1809 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1810 #endif
1811         mfspr   r6,SPRN_VRSAVE
1812         stw     r6,VCPU_VRSAVE(r3)
1813         mtmsrd  r5
1814         isync
1815         blr
1816
1817 /*
1818  * Load up FP, VMX and VSX registers
1819  * r4 = vcpu pointer
1820  */
1821         .globl  kvmppc_load_fp
1822 kvmppc_load_fp:
1823         mfmsr   r9
1824         ori     r8,r9,MSR_FP
1825 #ifdef CONFIG_ALTIVEC
1826 BEGIN_FTR_SECTION
1827         oris    r8,r8,MSR_VEC@h
1828 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1829 #endif
1830 #ifdef CONFIG_VSX
1831 BEGIN_FTR_SECTION
1832         oris    r8,r8,MSR_VSX@h
1833 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1834 #endif
1835         mtmsrd  r8
1836         isync
1837         lfd     fr0,VCPU_FPSCR(r4)
1838         MTFSF_L(fr0)
1839 #ifdef CONFIG_VSX
1840 BEGIN_FTR_SECTION
1841         reg = 0
1842         .rept   32
1843         li      r7,reg*16+VCPU_VSRS
1844         LXVD2X(reg,R7,R4)
1845         reg = reg + 1
1846         .endr
1847 FTR_SECTION_ELSE
1848 #endif
1849         reg = 0
1850         .rept   32
1851         lfd     reg,reg*8+VCPU_FPRS(r4)
1852         reg = reg + 1
1853         .endr
1854 #ifdef CONFIG_VSX
1855 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1856 #endif
1857
1858 #ifdef CONFIG_ALTIVEC
1859 BEGIN_FTR_SECTION
1860         li      r7,VCPU_VSCR
1861         lvx     vr0,r7,r4
1862         mtvscr  vr0
1863         reg = 0
1864         .rept   32
1865         li      r7,reg*16+VCPU_VRS
1866         lvx     reg,r7,r4
1867         reg = reg + 1
1868         .endr
1869 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1870 #endif
1871         lwz     r7,VCPU_VRSAVE(r4)
1872         mtspr   SPRN_VRSAVE,r7
1873         blr