Merge tag 'microblaze-3.17-rc1' of git://git.monstr.eu/linux-2.6-microblaze
[linux-2.6-block.git] / arch / powerpc / kvm / book3s_hv_rmhandlers.S
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12  *
13  * Derived from book3s_rmhandlers.S and other files, which are:
14  *
15  * Copyright SUSE Linux Products GmbH 2009
16  *
17  * Authors: Alexander Graf <agraf@suse.de>
18  */
19
20 #include <asm/ppc_asm.h>
21 #include <asm/kvm_asm.h>
22 #include <asm/reg.h>
23 #include <asm/mmu.h>
24 #include <asm/page.h>
25 #include <asm/ptrace.h>
26 #include <asm/hvcall.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/exception-64s.h>
29 #include <asm/kvm_book3s_asm.h>
30 #include <asm/mmu-hash64.h>
31 #include <asm/tm.h>
32
33 #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
34
35 #ifdef __LITTLE_ENDIAN__
36 #error Need to fix lppaca and SLB shadow accesses in little endian mode
37 #endif
38
39 /* Values in HSTATE_NAPPING(r13) */
40 #define NAPPING_CEDE    1
41 #define NAPPING_NOVCPU  2
42
43 /*
44  * Call kvmppc_hv_entry in real mode.
45  * Must be called with interrupts hard-disabled.
46  *
47  * Input Registers:
48  *
49  * LR = return address to continue at after eventually re-enabling MMU
50  */
51 _GLOBAL_TOC(kvmppc_hv_entry_trampoline)
52         mflr    r0
53         std     r0, PPC_LR_STKOFF(r1)
54         stdu    r1, -112(r1)
55         mfmsr   r10
56         LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
57         li      r0,MSR_RI
58         andc    r0,r10,r0
59         li      r6,MSR_IR | MSR_DR
60         andc    r6,r10,r6
61         mtmsrd  r0,1            /* clear RI in MSR */
62         mtsrr0  r5
63         mtsrr1  r6
64         RFI
65
66 kvmppc_call_hv_entry:
67         ld      r4, HSTATE_KVM_VCPU(r13)
68         bl      kvmppc_hv_entry
69
70         /* Back from guest - restore host state and return to caller */
71
72 BEGIN_FTR_SECTION
73         /* Restore host DABR and DABRX */
74         ld      r5,HSTATE_DABR(r13)
75         li      r6,7
76         mtspr   SPRN_DABR,r5
77         mtspr   SPRN_DABRX,r6
78 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
79
80         /* Restore SPRG3 */
81         ld      r3,PACA_SPRG_VDSO(r13)
82         mtspr   SPRN_SPRG_VDSO_WRITE,r3
83
84         /* Reload the host's PMU registers */
85         ld      r3, PACALPPACAPTR(r13)  /* is the host using the PMU? */
86         lbz     r4, LPPACA_PMCINUSE(r3)
87         cmpwi   r4, 0
88         beq     23f                     /* skip if not */
89 BEGIN_FTR_SECTION
90         ld      r3, HSTATE_MMCR(r13)
91         andi.   r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
92         cmpwi   r4, MMCR0_PMAO
93         beql    kvmppc_fix_pmao
94 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
95         lwz     r3, HSTATE_PMC(r13)
96         lwz     r4, HSTATE_PMC + 4(r13)
97         lwz     r5, HSTATE_PMC + 8(r13)
98         lwz     r6, HSTATE_PMC + 12(r13)
99         lwz     r8, HSTATE_PMC + 16(r13)
100         lwz     r9, HSTATE_PMC + 20(r13)
101 BEGIN_FTR_SECTION
102         lwz     r10, HSTATE_PMC + 24(r13)
103         lwz     r11, HSTATE_PMC + 28(r13)
104 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
105         mtspr   SPRN_PMC1, r3
106         mtspr   SPRN_PMC2, r4
107         mtspr   SPRN_PMC3, r5
108         mtspr   SPRN_PMC4, r6
109         mtspr   SPRN_PMC5, r8
110         mtspr   SPRN_PMC6, r9
111 BEGIN_FTR_SECTION
112         mtspr   SPRN_PMC7, r10
113         mtspr   SPRN_PMC8, r11
114 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
115         ld      r3, HSTATE_MMCR(r13)
116         ld      r4, HSTATE_MMCR + 8(r13)
117         ld      r5, HSTATE_MMCR + 16(r13)
118         ld      r6, HSTATE_MMCR + 24(r13)
119         ld      r7, HSTATE_MMCR + 32(r13)
120         mtspr   SPRN_MMCR1, r4
121         mtspr   SPRN_MMCRA, r5
122         mtspr   SPRN_SIAR, r6
123         mtspr   SPRN_SDAR, r7
124 BEGIN_FTR_SECTION
125         ld      r8, HSTATE_MMCR + 40(r13)
126         ld      r9, HSTATE_MMCR + 48(r13)
127         mtspr   SPRN_MMCR2, r8
128         mtspr   SPRN_SIER, r9
129 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
130         mtspr   SPRN_MMCR0, r3
131         isync
132 23:
133
134         /*
135          * Reload DEC.  HDEC interrupts were disabled when
136          * we reloaded the host's LPCR value.
137          */
138         ld      r3, HSTATE_DECEXP(r13)
139         mftb    r4
140         subf    r4, r4, r3
141         mtspr   SPRN_DEC, r4
142
143         /*
144          * For external and machine check interrupts, we need
145          * to call the Linux handler to process the interrupt.
146          * We do that by jumping to absolute address 0x500 for
147          * external interrupts, or the machine_check_fwnmi label
148          * for machine checks (since firmware might have patched
149          * the vector area at 0x200).  The [h]rfid at the end of the
150          * handler will return to the book3s_hv_interrupts.S code.
151          * For other interrupts we do the rfid to get back
152          * to the book3s_hv_interrupts.S code here.
153          */
154         ld      r8, 112+PPC_LR_STKOFF(r1)
155         addi    r1, r1, 112
156         ld      r7, HSTATE_HOST_MSR(r13)
157
158         cmpwi   cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
159         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
160 BEGIN_FTR_SECTION
161         beq     11f
162         cmpwi   cr2, r12, BOOK3S_INTERRUPT_HMI
163         beq     cr2, 14f                        /* HMI check */
164 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
165
166         /* RFI into the highmem handler, or branch to interrupt handler */
167         mfmsr   r6
168         li      r0, MSR_RI
169         andc    r6, r6, r0
170         mtmsrd  r6, 1                   /* Clear RI in MSR */
171         mtsrr0  r8
172         mtsrr1  r7
173         beqa    0x500                   /* external interrupt (PPC970) */
174         beq     cr1, 13f                /* machine check */
175         RFI
176
177         /* On POWER7, we have external interrupts set to use HSRR0/1 */
178 11:     mtspr   SPRN_HSRR0, r8
179         mtspr   SPRN_HSRR1, r7
180         ba      0x500
181
182 13:     b       machine_check_fwnmi
183
184 14:     mtspr   SPRN_HSRR0, r8
185         mtspr   SPRN_HSRR1, r7
186         b       hmi_exception_after_realmode
187
188 kvmppc_primary_no_guest:
189         /* We handle this much like a ceded vcpu */
190         /* set our bit in napping_threads */
191         ld      r5, HSTATE_KVM_VCORE(r13)
192         lbz     r7, HSTATE_PTID(r13)
193         li      r0, 1
194         sld     r0, r0, r7
195         addi    r6, r5, VCORE_NAPPING_THREADS
196 1:      lwarx   r3, 0, r6
197         or      r3, r3, r0
198         stwcx.  r3, 0, r6
199         bne     1b
200         /* order napping_threads update vs testing entry_exit_count */
201         isync
202         li      r12, 0
203         lwz     r7, VCORE_ENTRY_EXIT(r5)
204         cmpwi   r7, 0x100
205         bge     kvm_novcpu_exit /* another thread already exiting */
206         li      r3, NAPPING_NOVCPU
207         stb     r3, HSTATE_NAPPING(r13)
208         li      r3, 1
209         stb     r3, HSTATE_HWTHREAD_REQ(r13)
210
211         b       kvm_do_nap
212
213 kvm_novcpu_wakeup:
214         ld      r1, HSTATE_HOST_R1(r13)
215         ld      r5, HSTATE_KVM_VCORE(r13)
216         li      r0, 0
217         stb     r0, HSTATE_NAPPING(r13)
218         stb     r0, HSTATE_HWTHREAD_REQ(r13)
219
220         /* check the wake reason */
221         bl      kvmppc_check_wake_reason
222         
223         /* see if any other thread is already exiting */
224         lwz     r0, VCORE_ENTRY_EXIT(r5)
225         cmpwi   r0, 0x100
226         bge     kvm_novcpu_exit
227
228         /* clear our bit in napping_threads */
229         lbz     r7, HSTATE_PTID(r13)
230         li      r0, 1
231         sld     r0, r0, r7
232         addi    r6, r5, VCORE_NAPPING_THREADS
233 4:      lwarx   r7, 0, r6
234         andc    r7, r7, r0
235         stwcx.  r7, 0, r6
236         bne     4b
237
238         /* See if the wake reason means we need to exit */
239         cmpdi   r3, 0
240         bge     kvm_novcpu_exit
241
242         /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
243         ld      r4, HSTATE_KVM_VCPU(r13)
244         cmpdi   r4, 0
245         bne     kvmppc_got_guest
246
247 kvm_novcpu_exit:
248         b       hdec_soon
249
250 /*
251  * We come in here when wakened from nap mode.
252  * Relocation is off and most register values are lost.
253  * r13 points to the PACA.
254  */
255         .globl  kvm_start_guest
256 kvm_start_guest:
257
258         /* Set runlatch bit the minute you wake up from nap */
259         mfspr   r1, SPRN_CTRLF
260         ori     r1, r1, 1
261         mtspr   SPRN_CTRLT, r1
262
263         ld      r2,PACATOC(r13)
264
265         li      r0,KVM_HWTHREAD_IN_KVM
266         stb     r0,HSTATE_HWTHREAD_STATE(r13)
267
268         /* NV GPR values from power7_idle() will no longer be valid */
269         li      r0,1
270         stb     r0,PACA_NAPSTATELOST(r13)
271
272         /* were we napping due to cede? */
273         lbz     r0,HSTATE_NAPPING(r13)
274         cmpwi   r0,NAPPING_CEDE
275         beq     kvm_end_cede
276         cmpwi   r0,NAPPING_NOVCPU
277         beq     kvm_novcpu_wakeup
278
279         ld      r1,PACAEMERGSP(r13)
280         subi    r1,r1,STACK_FRAME_OVERHEAD
281
282         /*
283          * We weren't napping due to cede, so this must be a secondary
284          * thread being woken up to run a guest, or being woken up due
285          * to a stray IPI.  (Or due to some machine check or hypervisor
286          * maintenance interrupt while the core is in KVM.)
287          */
288
289         /* Check the wake reason in SRR1 to see why we got here */
290         bl      kvmppc_check_wake_reason
291         cmpdi   r3, 0
292         bge     kvm_no_guest
293
294         /* get vcpu pointer, NULL if we have no vcpu to run */
295         ld      r4,HSTATE_KVM_VCPU(r13)
296         cmpdi   r4,0
297         /* if we have no vcpu to run, go back to sleep */
298         beq     kvm_no_guest
299
300         /* Set HSTATE_DSCR(r13) to something sensible */
301         ld      r6, PACA_DSCR(r13)
302         std     r6, HSTATE_DSCR(r13)
303
304         bl      kvmppc_hv_entry
305
306         /* Back from the guest, go back to nap */
307         /* Clear our vcpu pointer so we don't come back in early */
308         li      r0, 0
309         std     r0, HSTATE_KVM_VCPU(r13)
310         /*
311          * Make sure we clear HSTATE_KVM_VCPU(r13) before incrementing
312          * the nap_count, because once the increment to nap_count is
313          * visible we could be given another vcpu.
314          */
315         lwsync
316
317         /* increment the nap count and then go to nap mode */
318         ld      r4, HSTATE_KVM_VCORE(r13)
319         addi    r4, r4, VCORE_NAP_COUNT
320 51:     lwarx   r3, 0, r4
321         addi    r3, r3, 1
322         stwcx.  r3, 0, r4
323         bne     51b
324
325 kvm_no_guest:
326         li      r0, KVM_HWTHREAD_IN_NAP
327         stb     r0, HSTATE_HWTHREAD_STATE(r13)
328 kvm_do_nap:
329         /* Clear the runlatch bit before napping */
330         mfspr   r2, SPRN_CTRLF
331         clrrdi  r2, r2, 1
332         mtspr   SPRN_CTRLT, r2
333
334         li      r3, LPCR_PECE0
335         mfspr   r4, SPRN_LPCR
336         rlwimi  r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
337         mtspr   SPRN_LPCR, r4
338         isync
339         std     r0, HSTATE_SCRATCH0(r13)
340         ptesync
341         ld      r0, HSTATE_SCRATCH0(r13)
342 1:      cmpd    r0, r0
343         bne     1b
344         nap
345         b       .
346
347 /******************************************************************************
348  *                                                                            *
349  *                               Entry code                                   *
350  *                                                                            *
351  *****************************************************************************/
352
353 .global kvmppc_hv_entry
354 kvmppc_hv_entry:
355
356         /* Required state:
357          *
358          * R4 = vcpu pointer (or NULL)
359          * MSR = ~IR|DR
360          * R13 = PACA
361          * R1 = host R1
362          * all other volatile GPRS = free
363          */
364         mflr    r0
365         std     r0, PPC_LR_STKOFF(r1)
366         stdu    r1, -112(r1)
367
368         /* Save R1 in the PACA */
369         std     r1, HSTATE_HOST_R1(r13)
370
371         li      r6, KVM_GUEST_MODE_HOST_HV
372         stb     r6, HSTATE_IN_GUEST(r13)
373
374         /* Clear out SLB */
375         li      r6,0
376         slbmte  r6,r6
377         slbia
378         ptesync
379
380 BEGIN_FTR_SECTION
381         b       30f
382 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
383         /*
384          * POWER7 host -> guest partition switch code.
385          * We don't have to lock against concurrent tlbies,
386          * but we do have to coordinate across hardware threads.
387          */
388         /* Increment entry count iff exit count is zero. */
389         ld      r5,HSTATE_KVM_VCORE(r13)
390         addi    r9,r5,VCORE_ENTRY_EXIT
391 21:     lwarx   r3,0,r9
392         cmpwi   r3,0x100                /* any threads starting to exit? */
393         bge     secondary_too_late      /* if so we're too late to the party */
394         addi    r3,r3,1
395         stwcx.  r3,0,r9
396         bne     21b
397
398         /* Primary thread switches to guest partition. */
399         ld      r9,VCORE_KVM(r5)        /* pointer to struct kvm */
400         lbz     r6,HSTATE_PTID(r13)
401         cmpwi   r6,0
402         bne     20f
403         ld      r6,KVM_SDR1(r9)
404         lwz     r7,KVM_LPID(r9)
405         li      r0,LPID_RSVD            /* switch to reserved LPID */
406         mtspr   SPRN_LPID,r0
407         ptesync
408         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
409         mtspr   SPRN_LPID,r7
410         isync
411
412         /* See if we need to flush the TLB */
413         lhz     r6,PACAPACAINDEX(r13)   /* test_bit(cpu, need_tlb_flush) */
414         clrldi  r7,r6,64-6              /* extract bit number (6 bits) */
415         srdi    r6,r6,6                 /* doubleword number */
416         sldi    r6,r6,3                 /* address offset */
417         add     r6,r6,r9
418         addi    r6,r6,KVM_NEED_FLUSH    /* dword in kvm->arch.need_tlb_flush */
419         li      r0,1
420         sld     r0,r0,r7
421         ld      r7,0(r6)
422         and.    r7,r7,r0
423         beq     22f
424 23:     ldarx   r7,0,r6                 /* if set, clear the bit */
425         andc    r7,r7,r0
426         stdcx.  r7,0,r6
427         bne     23b
428         /* Flush the TLB of any entries for this LPID */
429         /* use arch 2.07S as a proxy for POWER8 */
430 BEGIN_FTR_SECTION
431         li      r6,512                  /* POWER8 has 512 sets */
432 FTR_SECTION_ELSE
433         li      r6,128                  /* POWER7 has 128 sets */
434 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
435         mtctr   r6
436         li      r7,0x800                /* IS field = 0b10 */
437         ptesync
438 28:     tlbiel  r7
439         addi    r7,r7,0x1000
440         bdnz    28b
441         ptesync
442
443         /* Add timebase offset onto timebase */
444 22:     ld      r8,VCORE_TB_OFFSET(r5)
445         cmpdi   r8,0
446         beq     37f
447         mftb    r6              /* current host timebase */
448         add     r8,r8,r6
449         mtspr   SPRN_TBU40,r8   /* update upper 40 bits */
450         mftb    r7              /* check if lower 24 bits overflowed */
451         clrldi  r6,r6,40
452         clrldi  r7,r7,40
453         cmpld   r7,r6
454         bge     37f
455         addis   r8,r8,0x100     /* if so, increment upper 40 bits */
456         mtspr   SPRN_TBU40,r8
457
458         /* Load guest PCR value to select appropriate compat mode */
459 37:     ld      r7, VCORE_PCR(r5)
460         cmpdi   r7, 0
461         beq     38f
462         mtspr   SPRN_PCR, r7
463 38:
464
465 BEGIN_FTR_SECTION
466         /* DPDES is shared between threads */
467         ld      r8, VCORE_DPDES(r5)
468         mtspr   SPRN_DPDES, r8
469 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
470
471         li      r0,1
472         stb     r0,VCORE_IN_GUEST(r5)   /* signal secondaries to continue */
473         b       10f
474
475         /* Secondary threads wait for primary to have done partition switch */
476 20:     lbz     r0,VCORE_IN_GUEST(r5)
477         cmpwi   r0,0
478         beq     20b
479
480         /* Set LPCR and RMOR. */
481 10:     ld      r8,VCORE_LPCR(r5)
482         mtspr   SPRN_LPCR,r8
483         ld      r8,KVM_RMOR(r9)
484         mtspr   SPRN_RMOR,r8
485         isync
486
487         /* Check if HDEC expires soon */
488         mfspr   r3,SPRN_HDEC
489         cmpwi   r3,512          /* 1 microsecond */
490         li      r12,BOOK3S_INTERRUPT_HV_DECREMENTER
491         blt     hdec_soon
492         b       31f
493
494         /*
495          * PPC970 host -> guest partition switch code.
496          * We have to lock against concurrent tlbies,
497          * using native_tlbie_lock to lock against host tlbies
498          * and kvm->arch.tlbie_lock to lock against guest tlbies.
499          * We also have to invalidate the TLB since its
500          * entries aren't tagged with the LPID.
501          */
502 30:     ld      r5,HSTATE_KVM_VCORE(r13)
503         ld      r9,VCORE_KVM(r5)        /* pointer to struct kvm */
504
505         /* first take native_tlbie_lock */
506         .section ".toc","aw"
507 toc_tlbie_lock:
508         .tc     native_tlbie_lock[TC],native_tlbie_lock
509         .previous
510         ld      r3,toc_tlbie_lock@toc(2)
511 #ifdef __BIG_ENDIAN__
512         lwz     r8,PACA_LOCK_TOKEN(r13)
513 #else
514         lwz     r8,PACAPACAINDEX(r13)
515 #endif
516 24:     lwarx   r0,0,r3
517         cmpwi   r0,0
518         bne     24b
519         stwcx.  r8,0,r3
520         bne     24b
521         isync
522
523         ld      r5,HSTATE_KVM_VCORE(r13)
524         ld      r7,VCORE_LPCR(r5)       /* use vcore->lpcr to store HID4 */
525         li      r0,0x18f
526         rotldi  r0,r0,HID4_LPID5_SH     /* all lpid bits in HID4 = 1 */
527         or      r0,r7,r0
528         ptesync
529         sync
530         mtspr   SPRN_HID4,r0            /* switch to reserved LPID */
531         isync
532         li      r0,0
533         stw     r0,0(r3)                /* drop native_tlbie_lock */
534
535         /* invalidate the whole TLB */
536         li      r0,256
537         mtctr   r0
538         li      r6,0
539 25:     tlbiel  r6
540         addi    r6,r6,0x1000
541         bdnz    25b
542         ptesync
543
544         /* Take the guest's tlbie_lock */
545         addi    r3,r9,KVM_TLBIE_LOCK
546 24:     lwarx   r0,0,r3
547         cmpwi   r0,0
548         bne     24b
549         stwcx.  r8,0,r3
550         bne     24b
551         isync
552         ld      r6,KVM_SDR1(r9)
553         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
554
555         /* Set up HID4 with the guest's LPID etc. */
556         sync
557         mtspr   SPRN_HID4,r7
558         isync
559
560         /* drop the guest's tlbie_lock */
561         li      r0,0
562         stw     r0,0(r3)
563
564         /* Check if HDEC expires soon */
565         mfspr   r3,SPRN_HDEC
566         cmpwi   r3,10
567         li      r12,BOOK3S_INTERRUPT_HV_DECREMENTER
568         blt     hdec_soon
569
570         /* Enable HDEC interrupts */
571         mfspr   r0,SPRN_HID0
572         li      r3,1
573         rldimi  r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
574         sync
575         mtspr   SPRN_HID0,r0
576         mfspr   r0,SPRN_HID0
577         mfspr   r0,SPRN_HID0
578         mfspr   r0,SPRN_HID0
579         mfspr   r0,SPRN_HID0
580         mfspr   r0,SPRN_HID0
581         mfspr   r0,SPRN_HID0
582 31:
583         /* Do we have a guest vcpu to run? */
584         cmpdi   r4, 0
585         beq     kvmppc_primary_no_guest
586 kvmppc_got_guest:
587
588         /* Load up guest SLB entries */
589         lwz     r5,VCPU_SLB_MAX(r4)
590         cmpwi   r5,0
591         beq     9f
592         mtctr   r5
593         addi    r6,r4,VCPU_SLB
594 1:      ld      r8,VCPU_SLB_E(r6)
595         ld      r9,VCPU_SLB_V(r6)
596         slbmte  r9,r8
597         addi    r6,r6,VCPU_SLB_SIZE
598         bdnz    1b
599 9:
600         /* Increment yield count if they have a VPA */
601         ld      r3, VCPU_VPA(r4)
602         cmpdi   r3, 0
603         beq     25f
604         lwz     r5, LPPACA_YIELDCOUNT(r3)
605         addi    r5, r5, 1
606         stw     r5, LPPACA_YIELDCOUNT(r3)
607         li      r6, 1
608         stb     r6, VCPU_VPA_DIRTY(r4)
609 25:
610
611 BEGIN_FTR_SECTION
612         /* Save purr/spurr */
613         mfspr   r5,SPRN_PURR
614         mfspr   r6,SPRN_SPURR
615         std     r5,HSTATE_PURR(r13)
616         std     r6,HSTATE_SPURR(r13)
617         ld      r7,VCPU_PURR(r4)
618         ld      r8,VCPU_SPURR(r4)
619         mtspr   SPRN_PURR,r7
620         mtspr   SPRN_SPURR,r8
621 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
622
623 BEGIN_FTR_SECTION
624         /* Set partition DABR */
625         /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
626         lwz     r5,VCPU_DABRX(r4)
627         ld      r6,VCPU_DABR(r4)
628         mtspr   SPRN_DABRX,r5
629         mtspr   SPRN_DABR,r6
630  BEGIN_FTR_SECTION_NESTED(89)
631         isync
632  END_FTR_SECTION_NESTED(CPU_FTR_ARCH_206, CPU_FTR_ARCH_206, 89)
633 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
634
635 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
636 BEGIN_FTR_SECTION
637         b       skip_tm
638 END_FTR_SECTION_IFCLR(CPU_FTR_TM)
639
640         /* Turn on TM/FP/VSX/VMX so we can restore them. */
641         mfmsr   r5
642         li      r6, MSR_TM >> 32
643         sldi    r6, r6, 32
644         or      r5, r5, r6
645         ori     r5, r5, MSR_FP
646         oris    r5, r5, (MSR_VEC | MSR_VSX)@h
647         mtmsrd  r5
648
649         /*
650          * The user may change these outside of a transaction, so they must
651          * always be context switched.
652          */
653         ld      r5, VCPU_TFHAR(r4)
654         ld      r6, VCPU_TFIAR(r4)
655         ld      r7, VCPU_TEXASR(r4)
656         mtspr   SPRN_TFHAR, r5
657         mtspr   SPRN_TFIAR, r6
658         mtspr   SPRN_TEXASR, r7
659
660         ld      r5, VCPU_MSR(r4)
661         rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
662         beq     skip_tm /* TM not active in guest */
663
664         /* Make sure the failure summary is set, otherwise we'll program check
665          * when we trechkpt.  It's possible that this might have been not set
666          * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
667          * host.
668          */
669         oris    r7, r7, (TEXASR_FS)@h
670         mtspr   SPRN_TEXASR, r7
671
672         /*
673          * We need to load up the checkpointed state for the guest.
674          * We need to do this early as it will blow away any GPRs, VSRs and
675          * some SPRs.
676          */
677
678         mr      r31, r4
679         addi    r3, r31, VCPU_FPRS_TM
680         bl      .load_fp_state
681         addi    r3, r31, VCPU_VRS_TM
682         bl      .load_vr_state
683         mr      r4, r31
684         lwz     r7, VCPU_VRSAVE_TM(r4)
685         mtspr   SPRN_VRSAVE, r7
686
687         ld      r5, VCPU_LR_TM(r4)
688         lwz     r6, VCPU_CR_TM(r4)
689         ld      r7, VCPU_CTR_TM(r4)
690         ld      r8, VCPU_AMR_TM(r4)
691         ld      r9, VCPU_TAR_TM(r4)
692         mtlr    r5
693         mtcr    r6
694         mtctr   r7
695         mtspr   SPRN_AMR, r8
696         mtspr   SPRN_TAR, r9
697
698         /*
699          * Load up PPR and DSCR values but don't put them in the actual SPRs
700          * till the last moment to avoid running with userspace PPR and DSCR for
701          * too long.
702          */
703         ld      r29, VCPU_DSCR_TM(r4)
704         ld      r30, VCPU_PPR_TM(r4)
705
706         std     r2, PACATMSCRATCH(r13) /* Save TOC */
707
708         /* Clear the MSR RI since r1, r13 are all going to be foobar. */
709         li      r5, 0
710         mtmsrd  r5, 1
711
712         /* Load GPRs r0-r28 */
713         reg = 0
714         .rept   29
715         ld      reg, VCPU_GPRS_TM(reg)(r31)
716         reg = reg + 1
717         .endr
718
719         mtspr   SPRN_DSCR, r29
720         mtspr   SPRN_PPR, r30
721
722         /* Load final GPRs */
723         ld      29, VCPU_GPRS_TM(29)(r31)
724         ld      30, VCPU_GPRS_TM(30)(r31)
725         ld      31, VCPU_GPRS_TM(31)(r31)
726
727         /* TM checkpointed state is now setup.  All GPRs are now volatile. */
728         TRECHKPT
729
730         /* Now let's get back the state we need. */
731         HMT_MEDIUM
732         GET_PACA(r13)
733         ld      r29, HSTATE_DSCR(r13)
734         mtspr   SPRN_DSCR, r29
735         ld      r4, HSTATE_KVM_VCPU(r13)
736         ld      r1, HSTATE_HOST_R1(r13)
737         ld      r2, PACATMSCRATCH(r13)
738
739         /* Set the MSR RI since we have our registers back. */
740         li      r5, MSR_RI
741         mtmsrd  r5, 1
742 skip_tm:
743 #endif
744
745         /* Load guest PMU registers */
746         /* R4 is live here (vcpu pointer) */
747         li      r3, 1
748         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
749         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
750         isync
751 BEGIN_FTR_SECTION
752         ld      r3, VCPU_MMCR(r4)
753         andi.   r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
754         cmpwi   r5, MMCR0_PMAO
755         beql    kvmppc_fix_pmao
756 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
757         lwz     r3, VCPU_PMC(r4)        /* always load up guest PMU registers */
758         lwz     r5, VCPU_PMC + 4(r4)    /* to prevent information leak */
759         lwz     r6, VCPU_PMC + 8(r4)
760         lwz     r7, VCPU_PMC + 12(r4)
761         lwz     r8, VCPU_PMC + 16(r4)
762         lwz     r9, VCPU_PMC + 20(r4)
763 BEGIN_FTR_SECTION
764         lwz     r10, VCPU_PMC + 24(r4)
765         lwz     r11, VCPU_PMC + 28(r4)
766 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
767         mtspr   SPRN_PMC1, r3
768         mtspr   SPRN_PMC2, r5
769         mtspr   SPRN_PMC3, r6
770         mtspr   SPRN_PMC4, r7
771         mtspr   SPRN_PMC5, r8
772         mtspr   SPRN_PMC6, r9
773 BEGIN_FTR_SECTION
774         mtspr   SPRN_PMC7, r10
775         mtspr   SPRN_PMC8, r11
776 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
777         ld      r3, VCPU_MMCR(r4)
778         ld      r5, VCPU_MMCR + 8(r4)
779         ld      r6, VCPU_MMCR + 16(r4)
780         ld      r7, VCPU_SIAR(r4)
781         ld      r8, VCPU_SDAR(r4)
782         mtspr   SPRN_MMCR1, r5
783         mtspr   SPRN_MMCRA, r6
784         mtspr   SPRN_SIAR, r7
785         mtspr   SPRN_SDAR, r8
786 BEGIN_FTR_SECTION
787         ld      r5, VCPU_MMCR + 24(r4)
788         ld      r6, VCPU_SIER(r4)
789         lwz     r7, VCPU_PMC + 24(r4)
790         lwz     r8, VCPU_PMC + 28(r4)
791         ld      r9, VCPU_MMCR + 32(r4)
792         mtspr   SPRN_MMCR2, r5
793         mtspr   SPRN_SIER, r6
794         mtspr   SPRN_SPMC1, r7
795         mtspr   SPRN_SPMC2, r8
796         mtspr   SPRN_MMCRS, r9
797 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
798         mtspr   SPRN_MMCR0, r3
799         isync
800
801         /* Load up FP, VMX and VSX registers */
802         bl      kvmppc_load_fp
803
804         ld      r14, VCPU_GPR(R14)(r4)
805         ld      r15, VCPU_GPR(R15)(r4)
806         ld      r16, VCPU_GPR(R16)(r4)
807         ld      r17, VCPU_GPR(R17)(r4)
808         ld      r18, VCPU_GPR(R18)(r4)
809         ld      r19, VCPU_GPR(R19)(r4)
810         ld      r20, VCPU_GPR(R20)(r4)
811         ld      r21, VCPU_GPR(R21)(r4)
812         ld      r22, VCPU_GPR(R22)(r4)
813         ld      r23, VCPU_GPR(R23)(r4)
814         ld      r24, VCPU_GPR(R24)(r4)
815         ld      r25, VCPU_GPR(R25)(r4)
816         ld      r26, VCPU_GPR(R26)(r4)
817         ld      r27, VCPU_GPR(R27)(r4)
818         ld      r28, VCPU_GPR(R28)(r4)
819         ld      r29, VCPU_GPR(R29)(r4)
820         ld      r30, VCPU_GPR(R30)(r4)
821         ld      r31, VCPU_GPR(R31)(r4)
822
823 BEGIN_FTR_SECTION
824         /* Switch DSCR to guest value */
825         ld      r5, VCPU_DSCR(r4)
826         mtspr   SPRN_DSCR, r5
827 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
828
829 BEGIN_FTR_SECTION
830         /* Skip next section on POWER7 or PPC970 */
831         b       8f
832 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
833         /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
834         mfmsr   r8
835         li      r0, 1
836         rldimi  r8, r0, MSR_TM_LG, 63-MSR_TM_LG
837         mtmsrd  r8
838
839         /* Load up POWER8-specific registers */
840         ld      r5, VCPU_IAMR(r4)
841         lwz     r6, VCPU_PSPB(r4)
842         ld      r7, VCPU_FSCR(r4)
843         mtspr   SPRN_IAMR, r5
844         mtspr   SPRN_PSPB, r6
845         mtspr   SPRN_FSCR, r7
846         ld      r5, VCPU_DAWR(r4)
847         ld      r6, VCPU_DAWRX(r4)
848         ld      r7, VCPU_CIABR(r4)
849         ld      r8, VCPU_TAR(r4)
850         mtspr   SPRN_DAWR, r5
851         mtspr   SPRN_DAWRX, r6
852         mtspr   SPRN_CIABR, r7
853         mtspr   SPRN_TAR, r8
854         ld      r5, VCPU_IC(r4)
855         ld      r6, VCPU_VTB(r4)
856         mtspr   SPRN_IC, r5
857         mtspr   SPRN_VTB, r6
858         ld      r8, VCPU_EBBHR(r4)
859         mtspr   SPRN_EBBHR, r8
860         ld      r5, VCPU_EBBRR(r4)
861         ld      r6, VCPU_BESCR(r4)
862         ld      r7, VCPU_CSIGR(r4)
863         ld      r8, VCPU_TACR(r4)
864         mtspr   SPRN_EBBRR, r5
865         mtspr   SPRN_BESCR, r6
866         mtspr   SPRN_CSIGR, r7
867         mtspr   SPRN_TACR, r8
868         ld      r5, VCPU_TCSCR(r4)
869         ld      r6, VCPU_ACOP(r4)
870         lwz     r7, VCPU_GUEST_PID(r4)
871         ld      r8, VCPU_WORT(r4)
872         mtspr   SPRN_TCSCR, r5
873         mtspr   SPRN_ACOP, r6
874         mtspr   SPRN_PID, r7
875         mtspr   SPRN_WORT, r8
876 8:
877
878         /*
879          * Set the decrementer to the guest decrementer.
880          */
881         ld      r8,VCPU_DEC_EXPIRES(r4)
882         /* r8 is a host timebase value here, convert to guest TB */
883         ld      r5,HSTATE_KVM_VCORE(r13)
884         ld      r6,VCORE_TB_OFFSET(r5)
885         add     r8,r8,r6
886         mftb    r7
887         subf    r3,r7,r8
888         mtspr   SPRN_DEC,r3
889         stw     r3,VCPU_DEC(r4)
890
891         ld      r5, VCPU_SPRG0(r4)
892         ld      r6, VCPU_SPRG1(r4)
893         ld      r7, VCPU_SPRG2(r4)
894         ld      r8, VCPU_SPRG3(r4)
895         mtspr   SPRN_SPRG0, r5
896         mtspr   SPRN_SPRG1, r6
897         mtspr   SPRN_SPRG2, r7
898         mtspr   SPRN_SPRG3, r8
899
900         /* Load up DAR and DSISR */
901         ld      r5, VCPU_DAR(r4)
902         lwz     r6, VCPU_DSISR(r4)
903         mtspr   SPRN_DAR, r5
904         mtspr   SPRN_DSISR, r6
905
906 BEGIN_FTR_SECTION
907         /* Restore AMR and UAMOR, set AMOR to all 1s */
908         ld      r5,VCPU_AMR(r4)
909         ld      r6,VCPU_UAMOR(r4)
910         li      r7,-1
911         mtspr   SPRN_AMR,r5
912         mtspr   SPRN_UAMOR,r6
913         mtspr   SPRN_AMOR,r7
914 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
915
916         /* Restore state of CTRL run bit; assume 1 on entry */
917         lwz     r5,VCPU_CTRL(r4)
918         andi.   r5,r5,1
919         bne     4f
920         mfspr   r6,SPRN_CTRLF
921         clrrdi  r6,r6,1
922         mtspr   SPRN_CTRLT,r6
923 4:
924         ld      r6, VCPU_CTR(r4)
925         lwz     r7, VCPU_XER(r4)
926
927         mtctr   r6
928         mtxer   r7
929
930 kvmppc_cede_reentry:            /* r4 = vcpu, r13 = paca */
931         ld      r10, VCPU_PC(r4)
932         ld      r11, VCPU_MSR(r4)
933         ld      r6, VCPU_SRR0(r4)
934         ld      r7, VCPU_SRR1(r4)
935         mtspr   SPRN_SRR0, r6
936         mtspr   SPRN_SRR1, r7
937
938 deliver_guest_interrupt:
939         /* r11 = vcpu->arch.msr & ~MSR_HV */
940         rldicl  r11, r11, 63 - MSR_HV_LG, 1
941         rotldi  r11, r11, 1 + MSR_HV_LG
942         ori     r11, r11, MSR_ME
943
944         /* Check if we can deliver an external or decrementer interrupt now */
945         ld      r0, VCPU_PENDING_EXC(r4)
946         rldicl  r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
947         cmpdi   cr1, r0, 0
948         andi.   r8, r11, MSR_EE
949 BEGIN_FTR_SECTION
950         mfspr   r8, SPRN_LPCR
951         /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
952         rldimi  r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
953         mtspr   SPRN_LPCR, r8
954         isync
955 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
956         beq     5f
957         li      r0, BOOK3S_INTERRUPT_EXTERNAL
958         bne     cr1, 12f
959         mfspr   r0, SPRN_DEC
960         cmpwi   r0, 0
961         li      r0, BOOK3S_INTERRUPT_DECREMENTER
962         bge     5f
963
964 12:     mtspr   SPRN_SRR0, r10
965         mr      r10,r0
966         mtspr   SPRN_SRR1, r11
967         mr      r9, r4
968         bl      kvmppc_msr_interrupt
969 5:
970
971 /*
972  * Required state:
973  * R4 = vcpu
974  * R10: value for HSRR0
975  * R11: value for HSRR1
976  * R13 = PACA
977  */
978 fast_guest_return:
979         li      r0,0
980         stb     r0,VCPU_CEDED(r4)       /* cancel cede */
981         mtspr   SPRN_HSRR0,r10
982         mtspr   SPRN_HSRR1,r11
983
984         /* Activate guest mode, so faults get handled by KVM */
985         li      r9, KVM_GUEST_MODE_GUEST_HV
986         stb     r9, HSTATE_IN_GUEST(r13)
987
988         /* Enter guest */
989
990 BEGIN_FTR_SECTION
991         ld      r5, VCPU_CFAR(r4)
992         mtspr   SPRN_CFAR, r5
993 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
994 BEGIN_FTR_SECTION
995         ld      r0, VCPU_PPR(r4)
996 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
997
998         ld      r5, VCPU_LR(r4)
999         lwz     r6, VCPU_CR(r4)
1000         mtlr    r5
1001         mtcr    r6
1002
1003         ld      r1, VCPU_GPR(R1)(r4)
1004         ld      r2, VCPU_GPR(R2)(r4)
1005         ld      r3, VCPU_GPR(R3)(r4)
1006         ld      r5, VCPU_GPR(R5)(r4)
1007         ld      r6, VCPU_GPR(R6)(r4)
1008         ld      r7, VCPU_GPR(R7)(r4)
1009         ld      r8, VCPU_GPR(R8)(r4)
1010         ld      r9, VCPU_GPR(R9)(r4)
1011         ld      r10, VCPU_GPR(R10)(r4)
1012         ld      r11, VCPU_GPR(R11)(r4)
1013         ld      r12, VCPU_GPR(R12)(r4)
1014         ld      r13, VCPU_GPR(R13)(r4)
1015
1016 BEGIN_FTR_SECTION
1017         mtspr   SPRN_PPR, r0
1018 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1019         ld      r0, VCPU_GPR(R0)(r4)
1020         ld      r4, VCPU_GPR(R4)(r4)
1021
1022         hrfid
1023         b       .
1024
1025 /******************************************************************************
1026  *                                                                            *
1027  *                               Exit code                                    *
1028  *                                                                            *
1029  *****************************************************************************/
1030
1031 /*
1032  * We come here from the first-level interrupt handlers.
1033  */
1034         .globl  kvmppc_interrupt_hv
1035 kvmppc_interrupt_hv:
1036         /*
1037          * Register contents:
1038          * R12          = interrupt vector
1039          * R13          = PACA
1040          * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1041          * guest R13 saved in SPRN_SCRATCH0
1042          */
1043         std     r9, HSTATE_SCRATCH2(r13)
1044
1045         lbz     r9, HSTATE_IN_GUEST(r13)
1046         cmpwi   r9, KVM_GUEST_MODE_HOST_HV
1047         beq     kvmppc_bad_host_intr
1048 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1049         cmpwi   r9, KVM_GUEST_MODE_GUEST
1050         ld      r9, HSTATE_SCRATCH2(r13)
1051         beq     kvmppc_interrupt_pr
1052 #endif
1053         /* We're now back in the host but in guest MMU context */
1054         li      r9, KVM_GUEST_MODE_HOST_HV
1055         stb     r9, HSTATE_IN_GUEST(r13)
1056
1057         ld      r9, HSTATE_KVM_VCPU(r13)
1058
1059         /* Save registers */
1060
1061         std     r0, VCPU_GPR(R0)(r9)
1062         std     r1, VCPU_GPR(R1)(r9)
1063         std     r2, VCPU_GPR(R2)(r9)
1064         std     r3, VCPU_GPR(R3)(r9)
1065         std     r4, VCPU_GPR(R4)(r9)
1066         std     r5, VCPU_GPR(R5)(r9)
1067         std     r6, VCPU_GPR(R6)(r9)
1068         std     r7, VCPU_GPR(R7)(r9)
1069         std     r8, VCPU_GPR(R8)(r9)
1070         ld      r0, HSTATE_SCRATCH2(r13)
1071         std     r0, VCPU_GPR(R9)(r9)
1072         std     r10, VCPU_GPR(R10)(r9)
1073         std     r11, VCPU_GPR(R11)(r9)
1074         ld      r3, HSTATE_SCRATCH0(r13)
1075         lwz     r4, HSTATE_SCRATCH1(r13)
1076         std     r3, VCPU_GPR(R12)(r9)
1077         stw     r4, VCPU_CR(r9)
1078 BEGIN_FTR_SECTION
1079         ld      r3, HSTATE_CFAR(r13)
1080         std     r3, VCPU_CFAR(r9)
1081 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1082 BEGIN_FTR_SECTION
1083         ld      r4, HSTATE_PPR(r13)
1084         std     r4, VCPU_PPR(r9)
1085 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1086
1087         /* Restore R1/R2 so we can handle faults */
1088         ld      r1, HSTATE_HOST_R1(r13)
1089         ld      r2, PACATOC(r13)
1090
1091         mfspr   r10, SPRN_SRR0
1092         mfspr   r11, SPRN_SRR1
1093         std     r10, VCPU_SRR0(r9)
1094         std     r11, VCPU_SRR1(r9)
1095         andi.   r0, r12, 2              /* need to read HSRR0/1? */
1096         beq     1f
1097         mfspr   r10, SPRN_HSRR0
1098         mfspr   r11, SPRN_HSRR1
1099         clrrdi  r12, r12, 2
1100 1:      std     r10, VCPU_PC(r9)
1101         std     r11, VCPU_MSR(r9)
1102
1103         GET_SCRATCH0(r3)
1104         mflr    r4
1105         std     r3, VCPU_GPR(R13)(r9)
1106         std     r4, VCPU_LR(r9)
1107
1108         stw     r12,VCPU_TRAP(r9)
1109
1110         /* Save HEIR (HV emulation assist reg) in last_inst
1111            if this is an HEI (HV emulation interrupt, e40) */
1112         li      r3,KVM_INST_FETCH_FAILED
1113 BEGIN_FTR_SECTION
1114         cmpwi   r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1115         bne     11f
1116         mfspr   r3,SPRN_HEIR
1117 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1118 11:     stw     r3,VCPU_LAST_INST(r9)
1119
1120         /* these are volatile across C function calls */
1121         mfctr   r3
1122         mfxer   r4
1123         std     r3, VCPU_CTR(r9)
1124         stw     r4, VCPU_XER(r9)
1125
1126 BEGIN_FTR_SECTION
1127         /* If this is a page table miss then see if it's theirs or ours */
1128         cmpwi   r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1129         beq     kvmppc_hdsi
1130         cmpwi   r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1131         beq     kvmppc_hisi
1132 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1133
1134         /* See if this is a leftover HDEC interrupt */
1135         cmpwi   r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1136         bne     2f
1137         mfspr   r3,SPRN_HDEC
1138         cmpwi   r3,0
1139         bge     ignore_hdec
1140 2:
1141         /* See if this is an hcall we can handle in real mode */
1142         cmpwi   r12,BOOK3S_INTERRUPT_SYSCALL
1143         beq     hcall_try_real_mode
1144
1145         /* Only handle external interrupts here on arch 206 and later */
1146 BEGIN_FTR_SECTION
1147         b       ext_interrupt_to_host
1148 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1149
1150         /* External interrupt ? */
1151         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
1152         bne+    ext_interrupt_to_host
1153
1154         /* External interrupt, first check for host_ipi. If this is
1155          * set, we know the host wants us out so let's do it now
1156          */
1157         bl      kvmppc_read_intr
1158         cmpdi   r3, 0
1159         bgt     ext_interrupt_to_host
1160
1161         /* Check if any CPU is heading out to the host, if so head out too */
1162         ld      r5, HSTATE_KVM_VCORE(r13)
1163         lwz     r0, VCORE_ENTRY_EXIT(r5)
1164         cmpwi   r0, 0x100
1165         bge     ext_interrupt_to_host
1166
1167         /* Return to guest after delivering any pending interrupt */
1168         mr      r4, r9
1169         b       deliver_guest_interrupt
1170
1171 ext_interrupt_to_host:
1172
1173 guest_exit_cont:                /* r9 = vcpu, r12 = trap, r13 = paca */
1174         /* Save more register state  */
1175         mfdar   r6
1176         mfdsisr r7
1177         std     r6, VCPU_DAR(r9)
1178         stw     r7, VCPU_DSISR(r9)
1179 BEGIN_FTR_SECTION
1180         /* don't overwrite fault_dar/fault_dsisr if HDSI */
1181         cmpwi   r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1182         beq     6f
1183 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1184         std     r6, VCPU_FAULT_DAR(r9)
1185         stw     r7, VCPU_FAULT_DSISR(r9)
1186
1187         /* See if it is a machine check */
1188         cmpwi   r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1189         beq     machine_check_realmode
1190 mc_cont:
1191
1192         /* Save guest CTRL register, set runlatch to 1 */
1193 6:      mfspr   r6,SPRN_CTRLF
1194         stw     r6,VCPU_CTRL(r9)
1195         andi.   r0,r6,1
1196         bne     4f
1197         ori     r6,r6,1
1198         mtspr   SPRN_CTRLT,r6
1199 4:
1200         /* Read the guest SLB and save it away */
1201         lwz     r0,VCPU_SLB_NR(r9)      /* number of entries in SLB */
1202         mtctr   r0
1203         li      r6,0
1204         addi    r7,r9,VCPU_SLB
1205         li      r5,0
1206 1:      slbmfee r8,r6
1207         andis.  r0,r8,SLB_ESID_V@h
1208         beq     2f
1209         add     r8,r8,r6                /* put index in */
1210         slbmfev r3,r6
1211         std     r8,VCPU_SLB_E(r7)
1212         std     r3,VCPU_SLB_V(r7)
1213         addi    r7,r7,VCPU_SLB_SIZE
1214         addi    r5,r5,1
1215 2:      addi    r6,r6,1
1216         bdnz    1b
1217         stw     r5,VCPU_SLB_MAX(r9)
1218
1219         /*
1220          * Save the guest PURR/SPURR
1221          */
1222 BEGIN_FTR_SECTION
1223         mfspr   r5,SPRN_PURR
1224         mfspr   r6,SPRN_SPURR
1225         ld      r7,VCPU_PURR(r9)
1226         ld      r8,VCPU_SPURR(r9)
1227         std     r5,VCPU_PURR(r9)
1228         std     r6,VCPU_SPURR(r9)
1229         subf    r5,r7,r5
1230         subf    r6,r8,r6
1231
1232         /*
1233          * Restore host PURR/SPURR and add guest times
1234          * so that the time in the guest gets accounted.
1235          */
1236         ld      r3,HSTATE_PURR(r13)
1237         ld      r4,HSTATE_SPURR(r13)
1238         add     r3,r3,r5
1239         add     r4,r4,r6
1240         mtspr   SPRN_PURR,r3
1241         mtspr   SPRN_SPURR,r4
1242 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
1243
1244         /* Save DEC */
1245         mfspr   r5,SPRN_DEC
1246         mftb    r6
1247         extsw   r5,r5
1248         add     r5,r5,r6
1249         /* r5 is a guest timebase value here, convert to host TB */
1250         ld      r3,HSTATE_KVM_VCORE(r13)
1251         ld      r4,VCORE_TB_OFFSET(r3)
1252         subf    r5,r4,r5
1253         std     r5,VCPU_DEC_EXPIRES(r9)
1254
1255 BEGIN_FTR_SECTION
1256         b       8f
1257 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1258         /* Save POWER8-specific registers */
1259         mfspr   r5, SPRN_IAMR
1260         mfspr   r6, SPRN_PSPB
1261         mfspr   r7, SPRN_FSCR
1262         std     r5, VCPU_IAMR(r9)
1263         stw     r6, VCPU_PSPB(r9)
1264         std     r7, VCPU_FSCR(r9)
1265         mfspr   r5, SPRN_IC
1266         mfspr   r6, SPRN_VTB
1267         mfspr   r7, SPRN_TAR
1268         std     r5, VCPU_IC(r9)
1269         std     r6, VCPU_VTB(r9)
1270         std     r7, VCPU_TAR(r9)
1271         mfspr   r8, SPRN_EBBHR
1272         std     r8, VCPU_EBBHR(r9)
1273         mfspr   r5, SPRN_EBBRR
1274         mfspr   r6, SPRN_BESCR
1275         mfspr   r7, SPRN_CSIGR
1276         mfspr   r8, SPRN_TACR
1277         std     r5, VCPU_EBBRR(r9)
1278         std     r6, VCPU_BESCR(r9)
1279         std     r7, VCPU_CSIGR(r9)
1280         std     r8, VCPU_TACR(r9)
1281         mfspr   r5, SPRN_TCSCR
1282         mfspr   r6, SPRN_ACOP
1283         mfspr   r7, SPRN_PID
1284         mfspr   r8, SPRN_WORT
1285         std     r5, VCPU_TCSCR(r9)
1286         std     r6, VCPU_ACOP(r9)
1287         stw     r7, VCPU_GUEST_PID(r9)
1288         std     r8, VCPU_WORT(r9)
1289 8:
1290
1291         /* Save and reset AMR and UAMOR before turning on the MMU */
1292 BEGIN_FTR_SECTION
1293         mfspr   r5,SPRN_AMR
1294         mfspr   r6,SPRN_UAMOR
1295         std     r5,VCPU_AMR(r9)
1296         std     r6,VCPU_UAMOR(r9)
1297         li      r6,0
1298         mtspr   SPRN_AMR,r6
1299 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1300
1301         /* Switch DSCR back to host value */
1302 BEGIN_FTR_SECTION
1303         mfspr   r8, SPRN_DSCR
1304         ld      r7, HSTATE_DSCR(r13)
1305         std     r8, VCPU_DSCR(r9)
1306         mtspr   SPRN_DSCR, r7
1307 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1308
1309         /* Save non-volatile GPRs */
1310         std     r14, VCPU_GPR(R14)(r9)
1311         std     r15, VCPU_GPR(R15)(r9)
1312         std     r16, VCPU_GPR(R16)(r9)
1313         std     r17, VCPU_GPR(R17)(r9)
1314         std     r18, VCPU_GPR(R18)(r9)
1315         std     r19, VCPU_GPR(R19)(r9)
1316         std     r20, VCPU_GPR(R20)(r9)
1317         std     r21, VCPU_GPR(R21)(r9)
1318         std     r22, VCPU_GPR(R22)(r9)
1319         std     r23, VCPU_GPR(R23)(r9)
1320         std     r24, VCPU_GPR(R24)(r9)
1321         std     r25, VCPU_GPR(R25)(r9)
1322         std     r26, VCPU_GPR(R26)(r9)
1323         std     r27, VCPU_GPR(R27)(r9)
1324         std     r28, VCPU_GPR(R28)(r9)
1325         std     r29, VCPU_GPR(R29)(r9)
1326         std     r30, VCPU_GPR(R30)(r9)
1327         std     r31, VCPU_GPR(R31)(r9)
1328
1329         /* Save SPRGs */
1330         mfspr   r3, SPRN_SPRG0
1331         mfspr   r4, SPRN_SPRG1
1332         mfspr   r5, SPRN_SPRG2
1333         mfspr   r6, SPRN_SPRG3
1334         std     r3, VCPU_SPRG0(r9)
1335         std     r4, VCPU_SPRG1(r9)
1336         std     r5, VCPU_SPRG2(r9)
1337         std     r6, VCPU_SPRG3(r9)
1338
1339         /* save FP state */
1340         mr      r3, r9
1341         bl      kvmppc_save_fp
1342
1343 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1344 BEGIN_FTR_SECTION
1345         b       2f
1346 END_FTR_SECTION_IFCLR(CPU_FTR_TM)
1347         /* Turn on TM. */
1348         mfmsr   r8
1349         li      r0, 1
1350         rldimi  r8, r0, MSR_TM_LG, 63-MSR_TM_LG
1351         mtmsrd  r8
1352
1353         ld      r5, VCPU_MSR(r9)
1354         rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
1355         beq     1f      /* TM not active in guest. */
1356
1357         li      r3, TM_CAUSE_KVM_RESCHED
1358
1359         /* Clear the MSR RI since r1, r13 are all going to be foobar. */
1360         li      r5, 0
1361         mtmsrd  r5, 1
1362
1363         /* All GPRs are volatile at this point. */
1364         TRECLAIM(R3)
1365
1366         /* Temporarily store r13 and r9 so we have some regs to play with */
1367         SET_SCRATCH0(r13)
1368         GET_PACA(r13)
1369         std     r9, PACATMSCRATCH(r13)
1370         ld      r9, HSTATE_KVM_VCPU(r13)
1371
1372         /* Get a few more GPRs free. */
1373         std     r29, VCPU_GPRS_TM(29)(r9)
1374         std     r30, VCPU_GPRS_TM(30)(r9)
1375         std     r31, VCPU_GPRS_TM(31)(r9)
1376
1377         /* Save away PPR and DSCR soon so don't run with user values. */
1378         mfspr   r31, SPRN_PPR
1379         HMT_MEDIUM
1380         mfspr   r30, SPRN_DSCR
1381         ld      r29, HSTATE_DSCR(r13)
1382         mtspr   SPRN_DSCR, r29
1383
1384         /* Save all but r9, r13 & r29-r31 */
1385         reg = 0
1386         .rept   29
1387         .if (reg != 9) && (reg != 13)
1388         std     reg, VCPU_GPRS_TM(reg)(r9)
1389         .endif
1390         reg = reg + 1
1391         .endr
1392         /* ... now save r13 */
1393         GET_SCRATCH0(r4)
1394         std     r4, VCPU_GPRS_TM(13)(r9)
1395         /* ... and save r9 */
1396         ld      r4, PACATMSCRATCH(r13)
1397         std     r4, VCPU_GPRS_TM(9)(r9)
1398
1399         /* Reload stack pointer and TOC. */
1400         ld      r1, HSTATE_HOST_R1(r13)
1401         ld      r2, PACATOC(r13)
1402
1403         /* Set MSR RI now we have r1 and r13 back. */
1404         li      r5, MSR_RI
1405         mtmsrd  r5, 1
1406
1407         /* Save away checkpinted SPRs. */
1408         std     r31, VCPU_PPR_TM(r9)
1409         std     r30, VCPU_DSCR_TM(r9)
1410         mflr    r5
1411         mfcr    r6
1412         mfctr   r7
1413         mfspr   r8, SPRN_AMR
1414         mfspr   r10, SPRN_TAR
1415         std     r5, VCPU_LR_TM(r9)
1416         stw     r6, VCPU_CR_TM(r9)
1417         std     r7, VCPU_CTR_TM(r9)
1418         std     r8, VCPU_AMR_TM(r9)
1419         std     r10, VCPU_TAR_TM(r9)
1420
1421         /* Restore r12 as trap number. */
1422         lwz     r12, VCPU_TRAP(r9)
1423
1424         /* Save FP/VSX. */
1425         addi    r3, r9, VCPU_FPRS_TM
1426         bl      .store_fp_state
1427         addi    r3, r9, VCPU_VRS_TM
1428         bl      .store_vr_state
1429         mfspr   r6, SPRN_VRSAVE
1430         stw     r6, VCPU_VRSAVE_TM(r9)
1431 1:
1432         /*
1433          * We need to save these SPRs after the treclaim so that the software
1434          * error code is recorded correctly in the TEXASR.  Also the user may
1435          * change these outside of a transaction, so they must always be
1436          * context switched.
1437          */
1438         mfspr   r5, SPRN_TFHAR
1439         mfspr   r6, SPRN_TFIAR
1440         mfspr   r7, SPRN_TEXASR
1441         std     r5, VCPU_TFHAR(r9)
1442         std     r6, VCPU_TFIAR(r9)
1443         std     r7, VCPU_TEXASR(r9)
1444 2:
1445 #endif
1446
1447         /* Increment yield count if they have a VPA */
1448         ld      r8, VCPU_VPA(r9)        /* do they have a VPA? */
1449         cmpdi   r8, 0
1450         beq     25f
1451         lwz     r3, LPPACA_YIELDCOUNT(r8)
1452         addi    r3, r3, 1
1453         stw     r3, LPPACA_YIELDCOUNT(r8)
1454         li      r3, 1
1455         stb     r3, VCPU_VPA_DIRTY(r9)
1456 25:
1457         /* Save PMU registers if requested */
1458         /* r8 and cr0.eq are live here */
1459 BEGIN_FTR_SECTION
1460         /*
1461          * POWER8 seems to have a hardware bug where setting
1462          * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1463          * when some counters are already negative doesn't seem
1464          * to cause a performance monitor alert (and hence interrupt).
1465          * The effect of this is that when saving the PMU state,
1466          * if there is no PMU alert pending when we read MMCR0
1467          * before freezing the counters, but one becomes pending
1468          * before we read the counters, we lose it.
1469          * To work around this, we need a way to freeze the counters
1470          * before reading MMCR0.  Normally, freezing the counters
1471          * is done by writing MMCR0 (to set MMCR0[FC]) which
1472          * unavoidably writes MMCR0[PMA0] as well.  On POWER8,
1473          * we can also freeze the counters using MMCR2, by writing
1474          * 1s to all the counter freeze condition bits (there are
1475          * 9 bits each for 6 counters).
1476          */
1477         li      r3, -1                  /* set all freeze bits */
1478         clrrdi  r3, r3, 10
1479         mfspr   r10, SPRN_MMCR2
1480         mtspr   SPRN_MMCR2, r3
1481         isync
1482 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1483         li      r3, 1
1484         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
1485         mfspr   r4, SPRN_MMCR0          /* save MMCR0 */
1486         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
1487         mfspr   r6, SPRN_MMCRA
1488 BEGIN_FTR_SECTION
1489         /* On P7, clear MMCRA in order to disable SDAR updates */
1490         li      r7, 0
1491         mtspr   SPRN_MMCRA, r7
1492 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1493         isync
1494         beq     21f                     /* if no VPA, save PMU stuff anyway */
1495         lbz     r7, LPPACA_PMCINUSE(r8)
1496         cmpwi   r7, 0                   /* did they ask for PMU stuff to be saved? */
1497         bne     21f
1498         std     r3, VCPU_MMCR(r9)       /* if not, set saved MMCR0 to FC */
1499         b       22f
1500 21:     mfspr   r5, SPRN_MMCR1
1501         mfspr   r7, SPRN_SIAR
1502         mfspr   r8, SPRN_SDAR
1503         std     r4, VCPU_MMCR(r9)
1504         std     r5, VCPU_MMCR + 8(r9)
1505         std     r6, VCPU_MMCR + 16(r9)
1506 BEGIN_FTR_SECTION
1507         std     r10, VCPU_MMCR + 24(r9)
1508 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1509         std     r7, VCPU_SIAR(r9)
1510         std     r8, VCPU_SDAR(r9)
1511         mfspr   r3, SPRN_PMC1
1512         mfspr   r4, SPRN_PMC2
1513         mfspr   r5, SPRN_PMC3
1514         mfspr   r6, SPRN_PMC4
1515         mfspr   r7, SPRN_PMC5
1516         mfspr   r8, SPRN_PMC6
1517 BEGIN_FTR_SECTION
1518         mfspr   r10, SPRN_PMC7
1519         mfspr   r11, SPRN_PMC8
1520 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1521         stw     r3, VCPU_PMC(r9)
1522         stw     r4, VCPU_PMC + 4(r9)
1523         stw     r5, VCPU_PMC + 8(r9)
1524         stw     r6, VCPU_PMC + 12(r9)
1525         stw     r7, VCPU_PMC + 16(r9)
1526         stw     r8, VCPU_PMC + 20(r9)
1527 BEGIN_FTR_SECTION
1528         stw     r10, VCPU_PMC + 24(r9)
1529         stw     r11, VCPU_PMC + 28(r9)
1530 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1531 BEGIN_FTR_SECTION
1532         mfspr   r5, SPRN_SIER
1533         mfspr   r6, SPRN_SPMC1
1534         mfspr   r7, SPRN_SPMC2
1535         mfspr   r8, SPRN_MMCRS
1536         std     r5, VCPU_SIER(r9)
1537         stw     r6, VCPU_PMC + 24(r9)
1538         stw     r7, VCPU_PMC + 28(r9)
1539         std     r8, VCPU_MMCR + 32(r9)
1540         lis     r4, 0x8000
1541         mtspr   SPRN_MMCRS, r4
1542 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1543 22:
1544         /* Clear out SLB */
1545         li      r5,0
1546         slbmte  r5,r5
1547         slbia
1548         ptesync
1549
1550 hdec_soon:                      /* r12 = trap, r13 = paca */
1551 BEGIN_FTR_SECTION
1552         b       32f
1553 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1554         /*
1555          * POWER7 guest -> host partition switch code.
1556          * We don't have to lock against tlbies but we do
1557          * have to coordinate the hardware threads.
1558          */
1559         /* Increment the threads-exiting-guest count in the 0xff00
1560            bits of vcore->entry_exit_count */
1561         ld      r5,HSTATE_KVM_VCORE(r13)
1562         addi    r6,r5,VCORE_ENTRY_EXIT
1563 41:     lwarx   r3,0,r6
1564         addi    r0,r3,0x100
1565         stwcx.  r0,0,r6
1566         bne     41b
1567         isync           /* order stwcx. vs. reading napping_threads */
1568
1569         /*
1570          * At this point we have an interrupt that we have to pass
1571          * up to the kernel or qemu; we can't handle it in real mode.
1572          * Thus we have to do a partition switch, so we have to
1573          * collect the other threads, if we are the first thread
1574          * to take an interrupt.  To do this, we set the HDEC to 0,
1575          * which causes an HDEC interrupt in all threads within 2ns
1576          * because the HDEC register is shared between all 4 threads.
1577          * However, we don't need to bother if this is an HDEC
1578          * interrupt, since the other threads will already be on their
1579          * way here in that case.
1580          */
1581         cmpwi   r3,0x100        /* Are we the first here? */
1582         bge     43f
1583         cmpwi   r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1584         beq     40f
1585         li      r0,0
1586         mtspr   SPRN_HDEC,r0
1587 40:
1588         /*
1589          * Send an IPI to any napping threads, since an HDEC interrupt
1590          * doesn't wake CPUs up from nap.
1591          */
1592         lwz     r3,VCORE_NAPPING_THREADS(r5)
1593         lbz     r4,HSTATE_PTID(r13)
1594         li      r0,1
1595         sld     r0,r0,r4
1596         andc.   r3,r3,r0                /* no sense IPI'ing ourselves */
1597         beq     43f
1598         /* Order entry/exit update vs. IPIs */
1599         sync
1600         mulli   r4,r4,PACA_SIZE         /* get paca for thread 0 */
1601         subf    r6,r4,r13
1602 42:     andi.   r0,r3,1
1603         beq     44f
1604         ld      r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
1605         li      r0,IPI_PRIORITY
1606         li      r7,XICS_MFRR
1607         stbcix  r0,r7,r8                /* trigger the IPI */
1608 44:     srdi.   r3,r3,1
1609         addi    r6,r6,PACA_SIZE
1610         bne     42b
1611
1612 secondary_too_late:
1613         /* Secondary threads wait for primary to do partition switch */
1614 43:     ld      r5,HSTATE_KVM_VCORE(r13)
1615         ld      r4,VCORE_KVM(r5)        /* pointer to struct kvm */
1616         lbz     r3,HSTATE_PTID(r13)
1617         cmpwi   r3,0
1618         beq     15f
1619         HMT_LOW
1620 13:     lbz     r3,VCORE_IN_GUEST(r5)
1621         cmpwi   r3,0
1622         bne     13b
1623         HMT_MEDIUM
1624         b       16f
1625
1626         /* Primary thread waits for all the secondaries to exit guest */
1627 15:     lwz     r3,VCORE_ENTRY_EXIT(r5)
1628         srwi    r0,r3,8
1629         clrldi  r3,r3,56
1630         cmpw    r3,r0
1631         bne     15b
1632         isync
1633
1634         /* Primary thread switches back to host partition */
1635         ld      r6,KVM_HOST_SDR1(r4)
1636         lwz     r7,KVM_HOST_LPID(r4)
1637         li      r8,LPID_RSVD            /* switch to reserved LPID */
1638         mtspr   SPRN_LPID,r8
1639         ptesync
1640         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
1641         mtspr   SPRN_LPID,r7
1642         isync
1643
1644 BEGIN_FTR_SECTION
1645         /* DPDES is shared between threads */
1646         mfspr   r7, SPRN_DPDES
1647         std     r7, VCORE_DPDES(r5)
1648         /* clear DPDES so we don't get guest doorbells in the host */
1649         li      r8, 0
1650         mtspr   SPRN_DPDES, r8
1651 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1652
1653         /* Subtract timebase offset from timebase */
1654         ld      r8,VCORE_TB_OFFSET(r5)
1655         cmpdi   r8,0
1656         beq     17f
1657         mftb    r6                      /* current guest timebase */
1658         subf    r8,r8,r6
1659         mtspr   SPRN_TBU40,r8           /* update upper 40 bits */
1660         mftb    r7                      /* check if lower 24 bits overflowed */
1661         clrldi  r6,r6,40
1662         clrldi  r7,r7,40
1663         cmpld   r7,r6
1664         bge     17f
1665         addis   r8,r8,0x100             /* if so, increment upper 40 bits */
1666         mtspr   SPRN_TBU40,r8
1667
1668         /* Reset PCR */
1669 17:     ld      r0, VCORE_PCR(r5)
1670         cmpdi   r0, 0
1671         beq     18f
1672         li      r0, 0
1673         mtspr   SPRN_PCR, r0
1674 18:
1675         /* Signal secondary CPUs to continue */
1676         stb     r0,VCORE_IN_GUEST(r5)
1677         lis     r8,0x7fff               /* MAX_INT@h */
1678         mtspr   SPRN_HDEC,r8
1679
1680 16:     ld      r8,KVM_HOST_LPCR(r4)
1681         mtspr   SPRN_LPCR,r8
1682         isync
1683         b       33f
1684
1685         /*
1686          * PPC970 guest -> host partition switch code.
1687          * We have to lock against concurrent tlbies, and
1688          * we have to flush the whole TLB.
1689          */
1690 32:     ld      r5,HSTATE_KVM_VCORE(r13)
1691         ld      r4,VCORE_KVM(r5)        /* pointer to struct kvm */
1692
1693         /* Take the guest's tlbie_lock */
1694 #ifdef __BIG_ENDIAN__
1695         lwz     r8,PACA_LOCK_TOKEN(r13)
1696 #else
1697         lwz     r8,PACAPACAINDEX(r13)
1698 #endif
1699         addi    r3,r4,KVM_TLBIE_LOCK
1700 24:     lwarx   r0,0,r3
1701         cmpwi   r0,0
1702         bne     24b
1703         stwcx.  r8,0,r3
1704         bne     24b
1705         isync
1706
1707         ld      r7,KVM_HOST_LPCR(r4)    /* use kvm->arch.host_lpcr for HID4 */
1708         li      r0,0x18f
1709         rotldi  r0,r0,HID4_LPID5_SH     /* all lpid bits in HID4 = 1 */
1710         or      r0,r7,r0
1711         ptesync
1712         sync
1713         mtspr   SPRN_HID4,r0            /* switch to reserved LPID */
1714         isync
1715         li      r0,0
1716         stw     r0,0(r3)                /* drop guest tlbie_lock */
1717
1718         /* invalidate the whole TLB */
1719         li      r0,256
1720         mtctr   r0
1721         li      r6,0
1722 25:     tlbiel  r6
1723         addi    r6,r6,0x1000
1724         bdnz    25b
1725         ptesync
1726
1727         /* take native_tlbie_lock */
1728         ld      r3,toc_tlbie_lock@toc(2)
1729 24:     lwarx   r0,0,r3
1730         cmpwi   r0,0
1731         bne     24b
1732         stwcx.  r8,0,r3
1733         bne     24b
1734         isync
1735
1736         ld      r6,KVM_HOST_SDR1(r4)
1737         mtspr   SPRN_SDR1,r6            /* switch to host page table */
1738
1739         /* Set up host HID4 value */
1740         sync
1741         mtspr   SPRN_HID4,r7
1742         isync
1743         li      r0,0
1744         stw     r0,0(r3)                /* drop native_tlbie_lock */
1745
1746         lis     r8,0x7fff               /* MAX_INT@h */
1747         mtspr   SPRN_HDEC,r8
1748
1749         /* Disable HDEC interrupts */
1750         mfspr   r0,SPRN_HID0
1751         li      r3,0
1752         rldimi  r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1753         sync
1754         mtspr   SPRN_HID0,r0
1755         mfspr   r0,SPRN_HID0
1756         mfspr   r0,SPRN_HID0
1757         mfspr   r0,SPRN_HID0
1758         mfspr   r0,SPRN_HID0
1759         mfspr   r0,SPRN_HID0
1760         mfspr   r0,SPRN_HID0
1761
1762         /* load host SLB entries */
1763 33:     ld      r8,PACA_SLBSHADOWPTR(r13)
1764
1765         .rept   SLB_NUM_BOLTED
1766         ld      r5,SLBSHADOW_SAVEAREA(r8)
1767         ld      r6,SLBSHADOW_SAVEAREA+8(r8)
1768         andis.  r7,r5,SLB_ESID_V@h
1769         beq     1f
1770         slbmte  r6,r5
1771 1:      addi    r8,r8,16
1772         .endr
1773
1774         /* Unset guest mode */
1775         li      r0, KVM_GUEST_MODE_NONE
1776         stb     r0, HSTATE_IN_GUEST(r13)
1777
1778         ld      r0, 112+PPC_LR_STKOFF(r1)
1779         addi    r1, r1, 112
1780         mtlr    r0
1781         blr
1782
1783 /*
1784  * Check whether an HDSI is an HPTE not found fault or something else.
1785  * If it is an HPTE not found fault that is due to the guest accessing
1786  * a page that they have mapped but which we have paged out, then
1787  * we continue on with the guest exit path.  In all other cases,
1788  * reflect the HDSI to the guest as a DSI.
1789  */
1790 kvmppc_hdsi:
1791         mfspr   r4, SPRN_HDAR
1792         mfspr   r6, SPRN_HDSISR
1793         /* HPTE not found fault or protection fault? */
1794         andis.  r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1795         beq     1f                      /* if not, send it to the guest */
1796         andi.   r0, r11, MSR_DR         /* data relocation enabled? */
1797         beq     3f
1798         clrrdi  r0, r4, 28
1799         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1800         bne     1f                      /* if no SLB entry found */
1801 4:      std     r4, VCPU_FAULT_DAR(r9)
1802         stw     r6, VCPU_FAULT_DSISR(r9)
1803
1804         /* Search the hash table. */
1805         mr      r3, r9                  /* vcpu pointer */
1806         li      r7, 1                   /* data fault */
1807         bl      kvmppc_hpte_hv_fault
1808         ld      r9, HSTATE_KVM_VCPU(r13)
1809         ld      r10, VCPU_PC(r9)
1810         ld      r11, VCPU_MSR(r9)
1811         li      r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1812         cmpdi   r3, 0                   /* retry the instruction */
1813         beq     6f
1814         cmpdi   r3, -1                  /* handle in kernel mode */
1815         beq     guest_exit_cont
1816         cmpdi   r3, -2                  /* MMIO emulation; need instr word */
1817         beq     2f
1818
1819         /* Synthesize a DSI for the guest */
1820         ld      r4, VCPU_FAULT_DAR(r9)
1821         mr      r6, r3
1822 1:      mtspr   SPRN_DAR, r4
1823         mtspr   SPRN_DSISR, r6
1824         mtspr   SPRN_SRR0, r10
1825         mtspr   SPRN_SRR1, r11
1826         li      r10, BOOK3S_INTERRUPT_DATA_STORAGE
1827         bl      kvmppc_msr_interrupt
1828 fast_interrupt_c_return:
1829 6:      ld      r7, VCPU_CTR(r9)
1830         lwz     r8, VCPU_XER(r9)
1831         mtctr   r7
1832         mtxer   r8
1833         mr      r4, r9
1834         b       fast_guest_return
1835
1836 3:      ld      r5, VCPU_KVM(r9)        /* not relocated, use VRMA */
1837         ld      r5, KVM_VRMA_SLB_V(r5)
1838         b       4b
1839
1840         /* If this is for emulated MMIO, load the instruction word */
1841 2:      li      r8, KVM_INST_FETCH_FAILED       /* In case lwz faults */
1842
1843         /* Set guest mode to 'jump over instruction' so if lwz faults
1844          * we'll just continue at the next IP. */
1845         li      r0, KVM_GUEST_MODE_SKIP
1846         stb     r0, HSTATE_IN_GUEST(r13)
1847
1848         /* Do the access with MSR:DR enabled */
1849         mfmsr   r3
1850         ori     r4, r3, MSR_DR          /* Enable paging for data */
1851         mtmsrd  r4
1852         lwz     r8, 0(r10)
1853         mtmsrd  r3
1854
1855         /* Store the result */
1856         stw     r8, VCPU_LAST_INST(r9)
1857
1858         /* Unset guest mode. */
1859         li      r0, KVM_GUEST_MODE_HOST_HV
1860         stb     r0, HSTATE_IN_GUEST(r13)
1861         b       guest_exit_cont
1862
1863 /*
1864  * Similarly for an HISI, reflect it to the guest as an ISI unless
1865  * it is an HPTE not found fault for a page that we have paged out.
1866  */
1867 kvmppc_hisi:
1868         andis.  r0, r11, SRR1_ISI_NOPT@h
1869         beq     1f
1870         andi.   r0, r11, MSR_IR         /* instruction relocation enabled? */
1871         beq     3f
1872         clrrdi  r0, r10, 28
1873         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1874         bne     1f                      /* if no SLB entry found */
1875 4:
1876         /* Search the hash table. */
1877         mr      r3, r9                  /* vcpu pointer */
1878         mr      r4, r10
1879         mr      r6, r11
1880         li      r7, 0                   /* instruction fault */
1881         bl      kvmppc_hpte_hv_fault
1882         ld      r9, HSTATE_KVM_VCPU(r13)
1883         ld      r10, VCPU_PC(r9)
1884         ld      r11, VCPU_MSR(r9)
1885         li      r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1886         cmpdi   r3, 0                   /* retry the instruction */
1887         beq     fast_interrupt_c_return
1888         cmpdi   r3, -1                  /* handle in kernel mode */
1889         beq     guest_exit_cont
1890
1891         /* Synthesize an ISI for the guest */
1892         mr      r11, r3
1893 1:      mtspr   SPRN_SRR0, r10
1894         mtspr   SPRN_SRR1, r11
1895         li      r10, BOOK3S_INTERRUPT_INST_STORAGE
1896         bl      kvmppc_msr_interrupt
1897         b       fast_interrupt_c_return
1898
1899 3:      ld      r6, VCPU_KVM(r9)        /* not relocated, use VRMA */
1900         ld      r5, KVM_VRMA_SLB_V(r6)
1901         b       4b
1902
1903 /*
1904  * Try to handle an hcall in real mode.
1905  * Returns to the guest if we handle it, or continues on up to
1906  * the kernel if we can't (i.e. if we don't have a handler for
1907  * it, or if the handler returns H_TOO_HARD).
1908  */
1909         .globl  hcall_try_real_mode
1910 hcall_try_real_mode:
1911         ld      r3,VCPU_GPR(R3)(r9)
1912         andi.   r0,r11,MSR_PR
1913         /* sc 1 from userspace - reflect to guest syscall */
1914         bne     sc_1_fast_return
1915         clrrdi  r3,r3,2
1916         cmpldi  r3,hcall_real_table_end - hcall_real_table
1917         bge     guest_exit_cont
1918         LOAD_REG_ADDR(r4, hcall_real_table)
1919         lwax    r3,r3,r4
1920         cmpwi   r3,0
1921         beq     guest_exit_cont
1922         add     r3,r3,r4
1923         mtctr   r3
1924         mr      r3,r9           /* get vcpu pointer */
1925         ld      r4,VCPU_GPR(R4)(r9)
1926         bctrl
1927         cmpdi   r3,H_TOO_HARD
1928         beq     hcall_real_fallback
1929         ld      r4,HSTATE_KVM_VCPU(r13)
1930         std     r3,VCPU_GPR(R3)(r4)
1931         ld      r10,VCPU_PC(r4)
1932         ld      r11,VCPU_MSR(r4)
1933         b       fast_guest_return
1934
1935 sc_1_fast_return:
1936         mtspr   SPRN_SRR0,r10
1937         mtspr   SPRN_SRR1,r11
1938         li      r10, BOOK3S_INTERRUPT_SYSCALL
1939         bl      kvmppc_msr_interrupt
1940         mr      r4,r9
1941         b       fast_guest_return
1942
1943         /* We've attempted a real mode hcall, but it's punted it back
1944          * to userspace.  We need to restore some clobbered volatiles
1945          * before resuming the pass-it-to-qemu path */
1946 hcall_real_fallback:
1947         li      r12,BOOK3S_INTERRUPT_SYSCALL
1948         ld      r9, HSTATE_KVM_VCPU(r13)
1949
1950         b       guest_exit_cont
1951
1952         .globl  hcall_real_table
1953 hcall_real_table:
1954         .long   0               /* 0 - unused */
1955         .long   DOTSYM(kvmppc_h_remove) - hcall_real_table
1956         .long   DOTSYM(kvmppc_h_enter) - hcall_real_table
1957         .long   DOTSYM(kvmppc_h_read) - hcall_real_table
1958         .long   0               /* 0x10 - H_CLEAR_MOD */
1959         .long   0               /* 0x14 - H_CLEAR_REF */
1960         .long   DOTSYM(kvmppc_h_protect) - hcall_real_table
1961         .long   DOTSYM(kvmppc_h_get_tce) - hcall_real_table
1962         .long   DOTSYM(kvmppc_h_put_tce) - hcall_real_table
1963         .long   0               /* 0x24 - H_SET_SPRG0 */
1964         .long   DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
1965         .long   0               /* 0x2c */
1966         .long   0               /* 0x30 */
1967         .long   0               /* 0x34 */
1968         .long   0               /* 0x38 */
1969         .long   0               /* 0x3c */
1970         .long   0               /* 0x40 */
1971         .long   0               /* 0x44 */
1972         .long   0               /* 0x48 */
1973         .long   0               /* 0x4c */
1974         .long   0               /* 0x50 */
1975         .long   0               /* 0x54 */
1976         .long   0               /* 0x58 */
1977         .long   0               /* 0x5c */
1978         .long   0               /* 0x60 */
1979 #ifdef CONFIG_KVM_XICS
1980         .long   DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
1981         .long   DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
1982         .long   DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
1983         .long   0               /* 0x70 - H_IPOLL */
1984         .long   DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
1985 #else
1986         .long   0               /* 0x64 - H_EOI */
1987         .long   0               /* 0x68 - H_CPPR */
1988         .long   0               /* 0x6c - H_IPI */
1989         .long   0               /* 0x70 - H_IPOLL */
1990         .long   0               /* 0x74 - H_XIRR */
1991 #endif
1992         .long   0               /* 0x78 */
1993         .long   0               /* 0x7c */
1994         .long   0               /* 0x80 */
1995         .long   0               /* 0x84 */
1996         .long   0               /* 0x88 */
1997         .long   0               /* 0x8c */
1998         .long   0               /* 0x90 */
1999         .long   0               /* 0x94 */
2000         .long   0               /* 0x98 */
2001         .long   0               /* 0x9c */
2002         .long   0               /* 0xa0 */
2003         .long   0               /* 0xa4 */
2004         .long   0               /* 0xa8 */
2005         .long   0               /* 0xac */
2006         .long   0               /* 0xb0 */
2007         .long   0               /* 0xb4 */
2008         .long   0               /* 0xb8 */
2009         .long   0               /* 0xbc */
2010         .long   0               /* 0xc0 */
2011         .long   0               /* 0xc4 */
2012         .long   0               /* 0xc8 */
2013         .long   0               /* 0xcc */
2014         .long   0               /* 0xd0 */
2015         .long   0               /* 0xd4 */
2016         .long   0               /* 0xd8 */
2017         .long   0               /* 0xdc */
2018         .long   DOTSYM(kvmppc_h_cede) - hcall_real_table
2019         .long   0               /* 0xe4 */
2020         .long   0               /* 0xe8 */
2021         .long   0               /* 0xec */
2022         .long   0               /* 0xf0 */
2023         .long   0               /* 0xf4 */
2024         .long   0               /* 0xf8 */
2025         .long   0               /* 0xfc */
2026         .long   0               /* 0x100 */
2027         .long   0               /* 0x104 */
2028         .long   0               /* 0x108 */
2029         .long   0               /* 0x10c */
2030         .long   0               /* 0x110 */
2031         .long   0               /* 0x114 */
2032         .long   0               /* 0x118 */
2033         .long   0               /* 0x11c */
2034         .long   0               /* 0x120 */
2035         .long   DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
2036         .long   0               /* 0x128 */
2037         .long   0               /* 0x12c */
2038         .long   0               /* 0x130 */
2039         .long   DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
2040 hcall_real_table_end:
2041
2042 ignore_hdec:
2043         mr      r4,r9
2044         b       fast_guest_return
2045
2046 _GLOBAL(kvmppc_h_set_xdabr)
2047         andi.   r0, r5, DABRX_USER | DABRX_KERNEL
2048         beq     6f
2049         li      r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2050         andc.   r0, r5, r0
2051         beq     3f
2052 6:      li      r3, H_PARAMETER
2053         blr
2054
2055 _GLOBAL(kvmppc_h_set_dabr)
2056         li      r5, DABRX_USER | DABRX_KERNEL
2057 3:
2058 BEGIN_FTR_SECTION
2059         b       2f
2060 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2061         std     r4,VCPU_DABR(r3)
2062         stw     r5, VCPU_DABRX(r3)
2063         mtspr   SPRN_DABRX, r5
2064         /* Work around P7 bug where DABR can get corrupted on mtspr */
2065 1:      mtspr   SPRN_DABR,r4
2066         mfspr   r5, SPRN_DABR
2067         cmpd    r4, r5
2068         bne     1b
2069         isync
2070         li      r3,0
2071         blr
2072
2073         /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
2074 2:      rlwimi  r5, r4, 5, DAWRX_DR | DAWRX_DW
2075         rlwimi  r5, r4, 1, DAWRX_WT
2076         clrrdi  r4, r4, 3
2077         std     r4, VCPU_DAWR(r3)
2078         std     r5, VCPU_DAWRX(r3)
2079         mtspr   SPRN_DAWR, r4
2080         mtspr   SPRN_DAWRX, r5
2081         li      r3, 0
2082         blr
2083
2084 _GLOBAL(kvmppc_h_cede)
2085         ori     r11,r11,MSR_EE
2086         std     r11,VCPU_MSR(r3)
2087         li      r0,1
2088         stb     r0,VCPU_CEDED(r3)
2089         sync                    /* order setting ceded vs. testing prodded */
2090         lbz     r5,VCPU_PRODDED(r3)
2091         cmpwi   r5,0
2092         bne     kvm_cede_prodded
2093         li      r0,0            /* set trap to 0 to say hcall is handled */
2094         stw     r0,VCPU_TRAP(r3)
2095         li      r0,H_SUCCESS
2096         std     r0,VCPU_GPR(R3)(r3)
2097 BEGIN_FTR_SECTION
2098         b       kvm_cede_exit   /* just send it up to host on 970 */
2099 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
2100
2101         /*
2102          * Set our bit in the bitmask of napping threads unless all the
2103          * other threads are already napping, in which case we send this
2104          * up to the host.
2105          */
2106         ld      r5,HSTATE_KVM_VCORE(r13)
2107         lbz     r6,HSTATE_PTID(r13)
2108         lwz     r8,VCORE_ENTRY_EXIT(r5)
2109         clrldi  r8,r8,56
2110         li      r0,1
2111         sld     r0,r0,r6
2112         addi    r6,r5,VCORE_NAPPING_THREADS
2113 31:     lwarx   r4,0,r6
2114         or      r4,r4,r0
2115         PPC_POPCNTW(R7,R4)
2116         cmpw    r7,r8
2117         bge     kvm_cede_exit
2118         stwcx.  r4,0,r6
2119         bne     31b
2120         /* order napping_threads update vs testing entry_exit_count */
2121         isync
2122         li      r0,NAPPING_CEDE
2123         stb     r0,HSTATE_NAPPING(r13)
2124         lwz     r7,VCORE_ENTRY_EXIT(r5)
2125         cmpwi   r7,0x100
2126         bge     33f             /* another thread already exiting */
2127
2128 /*
2129  * Although not specifically required by the architecture, POWER7
2130  * preserves the following registers in nap mode, even if an SMT mode
2131  * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2132  * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2133  */
2134         /* Save non-volatile GPRs */
2135         std     r14, VCPU_GPR(R14)(r3)
2136         std     r15, VCPU_GPR(R15)(r3)
2137         std     r16, VCPU_GPR(R16)(r3)
2138         std     r17, VCPU_GPR(R17)(r3)
2139         std     r18, VCPU_GPR(R18)(r3)
2140         std     r19, VCPU_GPR(R19)(r3)
2141         std     r20, VCPU_GPR(R20)(r3)
2142         std     r21, VCPU_GPR(R21)(r3)
2143         std     r22, VCPU_GPR(R22)(r3)
2144         std     r23, VCPU_GPR(R23)(r3)
2145         std     r24, VCPU_GPR(R24)(r3)
2146         std     r25, VCPU_GPR(R25)(r3)
2147         std     r26, VCPU_GPR(R26)(r3)
2148         std     r27, VCPU_GPR(R27)(r3)
2149         std     r28, VCPU_GPR(R28)(r3)
2150         std     r29, VCPU_GPR(R29)(r3)
2151         std     r30, VCPU_GPR(R30)(r3)
2152         std     r31, VCPU_GPR(R31)(r3)
2153
2154         /* save FP state */
2155         bl      kvmppc_save_fp
2156
2157         /*
2158          * Take a nap until a decrementer or external or doobell interrupt
2159          * occurs, with PECE1, PECE0 and PECEDP set in LPCR. Also clear the
2160          * runlatch bit before napping.
2161          */
2162         mfspr   r2, SPRN_CTRLF
2163         clrrdi  r2, r2, 1
2164         mtspr   SPRN_CTRLT, r2
2165
2166         li      r0,1
2167         stb     r0,HSTATE_HWTHREAD_REQ(r13)
2168         mfspr   r5,SPRN_LPCR
2169         ori     r5,r5,LPCR_PECE0 | LPCR_PECE1
2170 BEGIN_FTR_SECTION
2171         oris    r5,r5,LPCR_PECEDP@h
2172 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2173         mtspr   SPRN_LPCR,r5
2174         isync
2175         li      r0, 0
2176         std     r0, HSTATE_SCRATCH0(r13)
2177         ptesync
2178         ld      r0, HSTATE_SCRATCH0(r13)
2179 1:      cmpd    r0, r0
2180         bne     1b
2181         nap
2182         b       .
2183
2184 33:     mr      r4, r3
2185         li      r3, 0
2186         li      r12, 0
2187         b       34f
2188
2189 kvm_end_cede:
2190         /* get vcpu pointer */
2191         ld      r4, HSTATE_KVM_VCPU(r13)
2192
2193         /* Woken by external or decrementer interrupt */
2194         ld      r1, HSTATE_HOST_R1(r13)
2195
2196         /* load up FP state */
2197         bl      kvmppc_load_fp
2198
2199         /* Load NV GPRS */
2200         ld      r14, VCPU_GPR(R14)(r4)
2201         ld      r15, VCPU_GPR(R15)(r4)
2202         ld      r16, VCPU_GPR(R16)(r4)
2203         ld      r17, VCPU_GPR(R17)(r4)
2204         ld      r18, VCPU_GPR(R18)(r4)
2205         ld      r19, VCPU_GPR(R19)(r4)
2206         ld      r20, VCPU_GPR(R20)(r4)
2207         ld      r21, VCPU_GPR(R21)(r4)
2208         ld      r22, VCPU_GPR(R22)(r4)
2209         ld      r23, VCPU_GPR(R23)(r4)
2210         ld      r24, VCPU_GPR(R24)(r4)
2211         ld      r25, VCPU_GPR(R25)(r4)
2212         ld      r26, VCPU_GPR(R26)(r4)
2213         ld      r27, VCPU_GPR(R27)(r4)
2214         ld      r28, VCPU_GPR(R28)(r4)
2215         ld      r29, VCPU_GPR(R29)(r4)
2216         ld      r30, VCPU_GPR(R30)(r4)
2217         ld      r31, VCPU_GPR(R31)(r4)
2218  
2219         /* Check the wake reason in SRR1 to see why we got here */
2220         bl      kvmppc_check_wake_reason
2221
2222         /* clear our bit in vcore->napping_threads */
2223 34:     ld      r5,HSTATE_KVM_VCORE(r13)
2224         lbz     r7,HSTATE_PTID(r13)
2225         li      r0,1
2226         sld     r0,r0,r7
2227         addi    r6,r5,VCORE_NAPPING_THREADS
2228 32:     lwarx   r7,0,r6
2229         andc    r7,r7,r0
2230         stwcx.  r7,0,r6
2231         bne     32b
2232         li      r0,0
2233         stb     r0,HSTATE_NAPPING(r13)
2234
2235         /* See if the wake reason means we need to exit */
2236         stw     r12, VCPU_TRAP(r4)
2237         mr      r9, r4
2238         cmpdi   r3, 0
2239         bgt     guest_exit_cont
2240
2241         /* see if any other thread is already exiting */
2242         lwz     r0,VCORE_ENTRY_EXIT(r5)
2243         cmpwi   r0,0x100
2244         bge     guest_exit_cont
2245
2246         b       kvmppc_cede_reentry     /* if not go back to guest */
2247
2248         /* cede when already previously prodded case */
2249 kvm_cede_prodded:
2250         li      r0,0
2251         stb     r0,VCPU_PRODDED(r3)
2252         sync                    /* order testing prodded vs. clearing ceded */
2253         stb     r0,VCPU_CEDED(r3)
2254         li      r3,H_SUCCESS
2255         blr
2256
2257         /* we've ceded but we want to give control to the host */
2258 kvm_cede_exit:
2259         b       hcall_real_fallback
2260
2261         /* Try to handle a machine check in real mode */
2262 machine_check_realmode:
2263         mr      r3, r9          /* get vcpu pointer */
2264         bl      kvmppc_realmode_machine_check
2265         nop
2266         cmpdi   r3, 0           /* Did we handle MCE ? */
2267         ld      r9, HSTATE_KVM_VCPU(r13)
2268         li      r12, BOOK3S_INTERRUPT_MACHINE_CHECK
2269         /*
2270          * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2271          * machine check interrupt (set HSRR0 to 0x200). And for handled
2272          * errors (no-fatal), just go back to guest execution with current
2273          * HSRR0 instead of exiting guest. This new approach will inject
2274          * machine check to guest for fatal error causing guest to crash.
2275          *
2276          * The old code used to return to host for unhandled errors which
2277          * was causing guest to hang with soft lockups inside guest and
2278          * makes it difficult to recover guest instance.
2279          */
2280         ld      r10, VCPU_PC(r9)
2281         ld      r11, VCPU_MSR(r9)
2282         bne     2f      /* Continue guest execution. */
2283         /* If not, deliver a machine check.  SRR0/1 are already set */
2284         li      r10, BOOK3S_INTERRUPT_MACHINE_CHECK
2285         ld      r11, VCPU_MSR(r9)
2286         bl      kvmppc_msr_interrupt
2287 2:      b       fast_interrupt_c_return
2288
2289 /*
2290  * Check the reason we woke from nap, and take appropriate action.
2291  * Returns:
2292  *      0 if nothing needs to be done
2293  *      1 if something happened that needs to be handled by the host
2294  *      -1 if there was a guest wakeup (IPI)
2295  *
2296  * Also sets r12 to the interrupt vector for any interrupt that needs
2297  * to be handled now by the host (0x500 for external interrupt), or zero.
2298  */
2299 kvmppc_check_wake_reason:
2300         mfspr   r6, SPRN_SRR1
2301 BEGIN_FTR_SECTION
2302         rlwinm  r6, r6, 45-31, 0xf      /* extract wake reason field (P8) */
2303 FTR_SECTION_ELSE
2304         rlwinm  r6, r6, 45-31, 0xe      /* P7 wake reason field is 3 bits */
2305 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2306         cmpwi   r6, 8                   /* was it an external interrupt? */
2307         li      r12, BOOK3S_INTERRUPT_EXTERNAL
2308         beq     kvmppc_read_intr        /* if so, see what it was */
2309         li      r3, 0
2310         li      r12, 0
2311         cmpwi   r6, 6                   /* was it the decrementer? */
2312         beq     0f
2313 BEGIN_FTR_SECTION
2314         cmpwi   r6, 5                   /* privileged doorbell? */
2315         beq     0f
2316         cmpwi   r6, 3                   /* hypervisor doorbell? */
2317         beq     3f
2318 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2319         li      r3, 1                   /* anything else, return 1 */
2320 0:      blr
2321
2322         /* hypervisor doorbell */
2323 3:      li      r12, BOOK3S_INTERRUPT_H_DOORBELL
2324         li      r3, 1
2325         blr
2326
2327 /*
2328  * Determine what sort of external interrupt is pending (if any).
2329  * Returns:
2330  *      0 if no interrupt is pending
2331  *      1 if an interrupt is pending that needs to be handled by the host
2332  *      -1 if there was a guest wakeup IPI (which has now been cleared)
2333  */
2334 kvmppc_read_intr:
2335         /* see if a host IPI is pending */
2336         li      r3, 1
2337         lbz     r0, HSTATE_HOST_IPI(r13)
2338         cmpwi   r0, 0
2339         bne     1f
2340
2341         /* Now read the interrupt from the ICP */
2342         ld      r6, HSTATE_XICS_PHYS(r13)
2343         li      r7, XICS_XIRR
2344         cmpdi   r6, 0
2345         beq-    1f
2346         lwzcix  r0, r6, r7
2347         rlwinm. r3, r0, 0, 0xffffff
2348         sync
2349         beq     1f                      /* if nothing pending in the ICP */
2350
2351         /* We found something in the ICP...
2352          *
2353          * If it's not an IPI, stash it in the PACA and return to
2354          * the host, we don't (yet) handle directing real external
2355          * interrupts directly to the guest
2356          */
2357         cmpwi   r3, XICS_IPI            /* if there is, is it an IPI? */
2358         bne     42f
2359
2360         /* It's an IPI, clear the MFRR and EOI it */
2361         li      r3, 0xff
2362         li      r8, XICS_MFRR
2363         stbcix  r3, r6, r8              /* clear the IPI */
2364         stwcix  r0, r6, r7              /* EOI it */
2365         sync
2366
2367         /* We need to re-check host IPI now in case it got set in the
2368          * meantime. If it's clear, we bounce the interrupt to the
2369          * guest
2370          */
2371         lbz     r0, HSTATE_HOST_IPI(r13)
2372         cmpwi   r0, 0
2373         bne-    43f
2374
2375         /* OK, it's an IPI for us */
2376         li      r3, -1
2377 1:      blr
2378
2379 42:     /* It's not an IPI and it's for the host, stash it in the PACA
2380          * before exit, it will be picked up by the host ICP driver
2381          */
2382         stw     r0, HSTATE_SAVED_XIRR(r13)
2383         li      r3, 1
2384         b       1b
2385
2386 43:     /* We raced with the host, we need to resend that IPI, bummer */
2387         li      r0, IPI_PRIORITY
2388         stbcix  r0, r6, r8              /* set the IPI */
2389         sync
2390         li      r3, 1
2391         b       1b
2392
2393 /*
2394  * Save away FP, VMX and VSX registers.
2395  * r3 = vcpu pointer
2396  * N.B. r30 and r31 are volatile across this function,
2397  * thus it is not callable from C.
2398  */
2399 kvmppc_save_fp:
2400         mflr    r30
2401         mr      r31,r3
2402         mfmsr   r5
2403         ori     r8,r5,MSR_FP
2404 #ifdef CONFIG_ALTIVEC
2405 BEGIN_FTR_SECTION
2406         oris    r8,r8,MSR_VEC@h
2407 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2408 #endif
2409 #ifdef CONFIG_VSX
2410 BEGIN_FTR_SECTION
2411         oris    r8,r8,MSR_VSX@h
2412 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2413 #endif
2414         mtmsrd  r8
2415         isync
2416         addi    r3,r3,VCPU_FPRS
2417         bl      .store_fp_state
2418 #ifdef CONFIG_ALTIVEC
2419 BEGIN_FTR_SECTION
2420         addi    r3,r31,VCPU_VRS
2421         bl      .store_vr_state
2422 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2423 #endif
2424         mfspr   r6,SPRN_VRSAVE
2425         stw     r6,VCPU_VRSAVE(r31)
2426         mtlr    r30
2427         blr
2428
2429 /*
2430  * Load up FP, VMX and VSX registers
2431  * r4 = vcpu pointer
2432  * N.B. r30 and r31 are volatile across this function,
2433  * thus it is not callable from C.
2434  */
2435 kvmppc_load_fp:
2436         mflr    r30
2437         mr      r31,r4
2438         mfmsr   r9
2439         ori     r8,r9,MSR_FP
2440 #ifdef CONFIG_ALTIVEC
2441 BEGIN_FTR_SECTION
2442         oris    r8,r8,MSR_VEC@h
2443 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2444 #endif
2445 #ifdef CONFIG_VSX
2446 BEGIN_FTR_SECTION
2447         oris    r8,r8,MSR_VSX@h
2448 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2449 #endif
2450         mtmsrd  r8
2451         isync
2452         addi    r3,r4,VCPU_FPRS
2453         bl      .load_fp_state
2454 #ifdef CONFIG_ALTIVEC
2455 BEGIN_FTR_SECTION
2456         addi    r3,r31,VCPU_VRS
2457         bl      .load_vr_state
2458 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2459 #endif
2460         lwz     r7,VCPU_VRSAVE(r31)
2461         mtspr   SPRN_VRSAVE,r7
2462         mtlr    r30
2463         mr      r4,r31
2464         blr
2465
2466 /*
2467  * We come here if we get any exception or interrupt while we are
2468  * executing host real mode code while in guest MMU context.
2469  * For now just spin, but we should do something better.
2470  */
2471 kvmppc_bad_host_intr:
2472         b       .
2473
2474 /*
2475  * This mimics the MSR transition on IRQ delivery.  The new guest MSR is taken
2476  * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2477  *   r11 has the guest MSR value (in/out)
2478  *   r9 has a vcpu pointer (in)
2479  *   r0 is used as a scratch register
2480  */
2481 kvmppc_msr_interrupt:
2482         rldicl  r0, r11, 64 - MSR_TS_S_LG, 62
2483         cmpwi   r0, 2 /* Check if we are in transactional state..  */
2484         ld      r11, VCPU_INTR_MSR(r9)
2485         bne     1f
2486         /* ... if transactional, change to suspended */
2487         li      r0, 1
2488 1:      rldimi  r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2489         blr
2490
2491 /*
2492  * This works around a hardware bug on POWER8E processors, where
2493  * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2494  * performance monitor interrupt.  Instead, when we need to have
2495  * an interrupt pending, we have to arrange for a counter to overflow.
2496  */
2497 kvmppc_fix_pmao:
2498         li      r3, 0
2499         mtspr   SPRN_MMCR2, r3
2500         lis     r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2501         ori     r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2502         mtspr   SPRN_MMCR0, r3
2503         lis     r3, 0x7fff
2504         ori     r3, r3, 0xffff
2505         mtspr   SPRN_PMC6, r3
2506         isync
2507         blr