2 * This file contains the power_save function for Power7 CPUs.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/threads.h>
11 #include <asm/processor.h>
13 #include <asm/cputable.h>
14 #include <asm/thread_info.h>
15 #include <asm/ppc_asm.h>
16 #include <asm/asm-offsets.h>
17 #include <asm/ppc-opcode.h>
18 #include <asm/hw_irq.h>
19 #include <asm/kvm_book3s_asm.h>
21 #include <asm/cpuidle.h>
25 /* Idle state entry routines */
27 #define IDLE_STATE_ENTER_SEQ(IDLE_INST) \
28 /* Magic NAP/SLEEP/WINKLE mode enter sequence */ \
40 * Pass requested state in r3:
41 * r3 - PNV_THREAD_NAP/SLEEP/WINKLE
43 * To check IRQ_HAPPENED in r4
47 _GLOBAL(power7_powersave_common)
48 /* Use r3 to pass state nap/sleep/winkle */
49 /* NAP is a state loss, we create a regs frame on the
50 * stack, fill it up with the state we care about and
51 * stick a pointer to it in PACAR1. We really only
52 * need to save PC, some CR bits and the NV GPRs,
53 * but for now an interrupt frame will do.
57 stdu r1,-INT_FRAME_SIZE(r1)
62 /* Make sure FPU, VSX etc... are flushed as we may lose
63 * state when going to nap mode
65 bl discard_lazy_cpu_state
66 #endif /* CONFIG_SMP */
68 /* Hard disable interrupts */
72 mtmsrd r9,1 /* hard-disable interrupts */
74 /* Check if something happened while soft-disabled */
75 lbz r0,PACAIRQHAPPENED(r13)
76 andi. r0,r0,~PACA_IRQ_HARD_DIS@l
80 addi r1,r1,INT_FRAME_SIZE
85 1: /* We mark irqs hard disabled as this is the state we'll
86 * be in when returning and we need to tell arch_local_irq_restore()
89 li r0,PACA_IRQ_HARD_DIS
90 stb r0,PACAIRQHAPPENED(r13)
92 /* We haven't lost state ... yet */
94 stb r0,PACA_NAPSTATELOST(r13)
96 /* Continue saving state */
105 * Go to real mode to do the nap, as required by the architecture.
106 * Also, we need to be in real mode before setting hwthread_state,
107 * because as soon as we do that, another thread can switch
108 * the MMU context to the guest.
110 LOAD_REG_IMMEDIATE(r5, MSR_IDLE)
113 LOAD_REG_ADDR(r7, power7_enter_nap_mode)
114 mtmsrd r6, 1 /* clear RI before setting SRR0/1 */
119 .globl power7_enter_nap_mode
120 power7_enter_nap_mode:
121 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
122 /* Tell KVM we're napping */
123 li r4,KVM_HWTHREAD_IN_NAP
124 stb r4,HSTATE_HWTHREAD_STATE(r13)
126 stb r3,PACA_THREAD_IDLE_STATE(r13)
127 cmpwi cr1,r3,PNV_THREAD_SLEEP
129 IDLE_STATE_ENTER_SEQ(PPC_NAP)
132 /* Sleep or winkle */
133 lbz r7,PACA_THREAD_MASK(r13)
134 ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
137 andc r15,r15,r7 /* Clear thread bit */
139 andi. r15,r15,PNV_CORE_IDLE_THREAD_BITS
142 * If cr0 = 0, then current thread is the last thread of the core entering
143 * sleep. Last thread needs to execute the hardware bug workaround code if
144 * required by the platform.
145 * Make the workaround call unconditionally here. The below branch call is
146 * patched out when the idle states are discovered if the platform does not
149 .global pnv_fastsleep_workaround_at_entry
150 pnv_fastsleep_workaround_at_entry:
151 beq fastsleep_workaround_at_entry
157 common_enter: /* common code for all the threads entering sleep */
158 IDLE_STATE_ENTER_SEQ(PPC_SLEEP)
160 fastsleep_workaround_at_entry:
161 ori r15,r15,PNV_CORE_IDLE_LOCK_BIT
166 /* Fast sleep workaround */
169 li r0,OPAL_CONFIG_CPU_IDLE_STATE
170 bl opal_call_realmode
180 /* Now check if user or arch enabled NAP mode */
181 LOAD_REG_ADDRBASE(r3,powersave_nap)
182 lwz r4,ADDROFF(powersave_nap)(r3)
191 b power7_powersave_common
194 _GLOBAL(power7_sleep)
195 li r3,PNV_THREAD_SLEEP
197 b power7_powersave_common
200 #define CHECK_HMI_INTERRUPT \
201 mfspr r0,SPRN_SRR1; \
202 BEGIN_FTR_SECTION_NESTED(66); \
203 rlwinm r0,r0,45-31,0xf; /* extract wake reason field (P8) */ \
204 FTR_SECTION_ELSE_NESTED(66); \
205 rlwinm r0,r0,45-31,0xe; /* P7 wake reason field is 3 bits */ \
206 ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66); \
207 cmpwi r0,0xa; /* Hypervisor maintenance ? */ \
209 /* Invoke opal call to handle hmi */ \
210 ld r2,PACATOC(r13); \
212 std r3,ORIG_GPR3(r1); /* Save original r3 */ \
213 li r0,OPAL_HANDLE_HMI; /* Pass opal token argument*/ \
214 bl opal_call_realmode; \
215 ld r3,ORIG_GPR3(r1); /* Restore original r3 */ \
219 _GLOBAL(power7_wakeup_tb_loss)
223 * Before entering any idle state, the NVGPRs are saved in the stack
224 * and they are restored before switching to the process context. Hence
225 * until they are restored, they are free to be used.
227 * Save SRR1 in a NVGPR as it might be clobbered in opal_call_realmode
228 * (called in CHECK_HMI_INTERRUPT). SRR1 is required to determine the
229 * wakeup reason if we branch to kvm_start_guest.
235 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
237 lbz r7,PACA_THREAD_MASK(r13)
238 ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
241 andi. r9,r15,PNV_CORE_IDLE_LOCK_BIT
243 * Lock bit is set in one of the 2 cases-
244 * a. In the sleep/winkle enter path, the last thread is executing
245 * fastsleep workaround code.
246 * b. In the wake up path, another thread is executing fastsleep
247 * workaround undo code or resyncing timebase or restoring context
248 * In either case loop until the lock bit is cleared.
250 bne core_idle_lock_held
253 or r15,r15,r7 /* Set thread bit */
257 /* Not first thread in core to wake up */
267 andi. r9,r15,PNV_CORE_IDLE_LOCK_BIT
268 bne core_idle_lock_loop
273 /* First thread in core to wakeup */
274 ori r15,r15,PNV_CORE_IDLE_LOCK_BIT
280 * First thread in the core waking up from fastsleep. It needs to
281 * call the fastsleep workaround code if the platform requires it.
282 * Call it unconditionally here. The below branch instruction will
283 * be patched out when the idle states are discovered if platform
284 * does not require workaround.
286 .global pnv_fastsleep_workaround_at_exit
287 pnv_fastsleep_workaround_at_exit:
288 b fastsleep_workaround_at_exit
291 /* Do timebase resync if we are waking up from sleep. Use cr3 value
292 * set in exceptions-64s.S */
294 /* Time base re-sync */
295 li r0,OPAL_RESYNC_TIMEBASE
296 bl opal_call_realmode;
297 /* TODO: Check r3 for failure */
300 andi. r15,r15,PNV_CORE_IDLE_THREAD_BITS
305 li r5,PNV_THREAD_RUNNING
306 stb r5,PACA_THREAD_IDLE_STATE(r13)
309 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
310 li r0,KVM_HWTHREAD_IN_KERNEL
311 stb r0,HSTATE_HWTHREAD_STATE(r13)
312 /* Order setting hwthread_state vs. testing hwthread_req */
314 lbz r0,HSTATE_HWTHREAD_REQ(r13)
326 addi r1,r1,INT_FRAME_SIZE
328 mfspr r3,SPRN_SRR1 /* Return SRR1 */
333 fastsleep_workaround_at_exit:
336 li r0,OPAL_CONFIG_CPU_IDLE_STATE
337 bl opal_call_realmode
341 * R3 here contains the value that will be returned to the caller
344 _GLOBAL(power7_wakeup_loss)
348 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
354 addi r1,r1,INT_FRAME_SIZE
361 * R3 here contains the value that will be returned to the caller
364 _GLOBAL(power7_wakeup_noloss)
365 lbz r0,PACA_NAPSTATELOST(r13)
367 bne power7_wakeup_loss
370 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
374 addi r1,r1,INT_FRAME_SIZE