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