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