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