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