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>
22 #include <asm/mmu-hash64.h>
27 * Use unused space in the interrupt stack to save and restore
28 * registers for winkle support.
40 /* Idle state entry routines */
42 #define IDLE_STATE_ENTER_SEQ(IDLE_INST) \
43 /* Magic NAP/SLEEP/WINKLE mode enter sequence */ \
55 * Used by threads when the lock bit of core_idle_state is set.
56 * Threads will spin in HMT_LOW until the lock bit is cleared.
57 * r14 - pointer to core_idle_state
58 * r15 - used to load contents of core_idle_state
64 andi. r15,r15,PNV_CORE_IDLE_LOCK_BIT
71 * Pass requested state in r3:
72 * r3 - PNV_THREAD_NAP/SLEEP/WINKLE
74 * To check IRQ_HAPPENED in r4
78 _GLOBAL(power7_powersave_common)
79 /* Use r3 to pass state nap/sleep/winkle */
80 /* NAP is a state loss, we create a regs frame on the
81 * stack, fill it up with the state we care about and
82 * stick a pointer to it in PACAR1. We really only
83 * need to save PC, some CR bits and the NV GPRs,
84 * but for now an interrupt frame will do.
88 stdu r1,-INT_FRAME_SIZE(r1)
93 /* Make sure FPU, VSX etc... are flushed as we may lose
94 * state when going to nap mode
96 bl discard_lazy_cpu_state
97 #endif /* CONFIG_SMP */
99 /* Hard disable interrupts */
103 mtmsrd r9,1 /* hard-disable interrupts */
105 /* Check if something happened while soft-disabled */
106 lbz r0,PACAIRQHAPPENED(r13)
107 andi. r0,r0,~PACA_IRQ_HARD_DIS@l
111 addi r1,r1,INT_FRAME_SIZE
113 li r3,0 /* Return 0 (no nap) */
117 1: /* We mark irqs hard disabled as this is the state we'll
118 * be in when returning and we need to tell arch_local_irq_restore()
121 li r0,PACA_IRQ_HARD_DIS
122 stb r0,PACAIRQHAPPENED(r13)
124 /* We haven't lost state ... yet */
126 stb r0,PACA_NAPSTATELOST(r13)
128 /* Continue saving state */
137 * Go to real mode to do the nap, as required by the architecture.
138 * Also, we need to be in real mode before setting hwthread_state,
139 * because as soon as we do that, another thread can switch
140 * the MMU context to the guest.
142 LOAD_REG_IMMEDIATE(r5, MSR_IDLE)
145 LOAD_REG_ADDR(r7, power7_enter_nap_mode)
146 mtmsrd r6, 1 /* clear RI before setting SRR0/1 */
151 .globl power7_enter_nap_mode
152 power7_enter_nap_mode:
153 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
154 /* Tell KVM we're napping */
155 li r4,KVM_HWTHREAD_IN_NAP
156 stb r4,HSTATE_HWTHREAD_STATE(r13)
158 stb r3,PACA_THREAD_IDLE_STATE(r13)
159 cmpwi cr3,r3,PNV_THREAD_SLEEP
161 IDLE_STATE_ENTER_SEQ(PPC_NAP)
164 /* Sleep or winkle */
165 lbz r7,PACA_THREAD_MASK(r13)
166 ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
170 andi. r9,r15,PNV_CORE_IDLE_LOCK_BIT
171 bnel core_idle_lock_held
173 andc r15,r15,r7 /* Clear thread bit */
175 andi. r15,r15,PNV_CORE_IDLE_THREAD_BITS
178 * If cr0 = 0, then current thread is the last thread of the core entering
179 * sleep. Last thread needs to execute the hardware bug workaround code if
180 * required by the platform.
181 * Make the workaround call unconditionally here. The below branch call is
182 * patched out when the idle states are discovered if the platform does not
185 .global pnv_fastsleep_workaround_at_entry
186 pnv_fastsleep_workaround_at_entry:
187 beq fastsleep_workaround_at_entry
193 common_enter: /* common code for all the threads entering sleep or winkle */
195 IDLE_STATE_ENTER_SEQ(PPC_SLEEP)
197 fastsleep_workaround_at_entry:
198 ori r15,r15,PNV_CORE_IDLE_LOCK_BIT
203 /* Fast sleep workaround */
206 li r0,OPAL_CONFIG_CPU_IDLE_STATE
207 bl opal_call_realmode
217 * Note all register i.e per-core, per-subcore or per-thread is saved
218 * here since any thread in the core might wake up first
238 IDLE_STATE_ENTER_SEQ(PPC_WINKLE)
241 /* Now check if user or arch enabled NAP mode */
242 LOAD_REG_ADDRBASE(r3,powersave_nap)
243 lwz r4,ADDROFF(powersave_nap)(r3)
252 b power7_powersave_common
255 _GLOBAL(power7_sleep)
256 li r3,PNV_THREAD_SLEEP
258 b power7_powersave_common
261 _GLOBAL(power7_winkle)
264 b power7_powersave_common
267 #define CHECK_HMI_INTERRUPT \
268 mfspr r0,SPRN_SRR1; \
269 BEGIN_FTR_SECTION_NESTED(66); \
270 rlwinm r0,r0,45-31,0xf; /* extract wake reason field (P8) */ \
271 FTR_SECTION_ELSE_NESTED(66); \
272 rlwinm r0,r0,45-31,0xe; /* P7 wake reason field is 3 bits */ \
273 ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66); \
274 cmpwi r0,0xa; /* Hypervisor maintenance ? */ \
276 /* Invoke opal call to handle hmi */ \
277 ld r2,PACATOC(r13); \
279 std r3,ORIG_GPR3(r1); /* Save original r3 */ \
280 li r0,OPAL_HANDLE_HMI; /* Pass opal token argument*/ \
281 bl opal_call_realmode; \
282 ld r3,ORIG_GPR3(r1); /* Restore original r3 */ \
286 _GLOBAL(power7_wakeup_tb_loss)
290 * Before entering any idle state, the NVGPRs are saved in the stack
291 * and they are restored before switching to the process context. Hence
292 * until they are restored, they are free to be used.
294 * Save SRR1 in a NVGPR as it might be clobbered in opal_call_realmode
295 * (called in CHECK_HMI_INTERRUPT). SRR1 is required to determine the
296 * wakeup reason if we branch to kvm_start_guest.
302 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
304 lbz r7,PACA_THREAD_MASK(r13)
305 ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
308 andi. r9,r15,PNV_CORE_IDLE_LOCK_BIT
310 * Lock bit is set in one of the 2 cases-
311 * a. In the sleep/winkle enter path, the last thread is executing
312 * fastsleep workaround code.
313 * b. In the wake up path, another thread is executing fastsleep
314 * workaround undo code or resyncing timebase or restoring context
315 * In either case loop until the lock bit is cleared.
317 bnel core_idle_lock_held
320 lbz r4,PACA_SUBCORE_SIBLING_MASK(r13)
322 cmpwi cr1,r4,0 /* Check if first in subcore */
326 * cr1 - 0b0100 if first thread to wakeup in subcore
327 * cr2 - 0b0100 if first thread to wakeup in core
328 * cr3- 0b0010 if waking up from sleep or winkle
329 * cr4 - 0b0100 if waking up from winkle
332 or r15,r15,r7 /* Set thread bit */
334 beq cr1,first_thread_in_subcore
336 /* Not first thread in subcore to wake up */
342 first_thread_in_subcore:
343 /* First thread in subcore to wakeup */
344 ori r15,r15,PNV_CORE_IDLE_LOCK_BIT
350 * If waking up from sleep, subcore state is not lost. Hence
351 * skip subcore state restore
353 bne cr4,subcore_state_restored
355 /* Restore per-subcore state */
363 subcore_state_restored:
365 * Check if the thread is also the first thread in the core. If not,
366 * skip to clear_lock.
370 first_thread_in_core:
373 * First thread in the core waking up from fastsleep. It needs to
374 * call the fastsleep workaround code if the platform requires it.
375 * Call it unconditionally here. The below branch instruction will
376 * be patched out when the idle states are discovered if platform
377 * does not require workaround.
379 .global pnv_fastsleep_workaround_at_exit
380 pnv_fastsleep_workaround_at_exit:
381 b fastsleep_workaround_at_exit
384 /* Do timebase resync if we are waking up from sleep. Use cr3 value
385 * set in exceptions-64s.S */
387 /* Time base re-sync */
388 li r0,OPAL_RESYNC_TIMEBASE
389 bl opal_call_realmode;
390 /* TODO: Check r3 for failure */
393 * If waking up from sleep, per core state is not lost, skip to
398 /* Restore per core state */
405 andi. r15,r15,PNV_CORE_IDLE_THREAD_BITS
411 * Common to all threads.
413 * If waking up from sleep, hypervisor state is not lost. Hence
414 * skip hypervisor state restore.
416 bne cr4,hypervisor_state_restored
418 /* Waking up from winkle */
420 /* Restore per thread state */
421 bl __restore_cpu_power8
423 /* Restore SLB from PACA */
424 ld r8,PACA_SLBSHADOWPTR(r13)
427 li r3, SLBSHADOW_SAVEAREA
431 andis. r7,r5,SLB_ESID_V@h
446 hypervisor_state_restored:
448 li r5,PNV_THREAD_RUNNING
449 stb r5,PACA_THREAD_IDLE_STATE(r13)
452 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
453 li r0,KVM_HWTHREAD_IN_KERNEL
454 stb r0,HSTATE_HWTHREAD_STATE(r13)
455 /* Order setting hwthread_state vs. testing hwthread_req */
457 lbz r0,HSTATE_HWTHREAD_REQ(r13)
469 addi r1,r1,INT_FRAME_SIZE
471 mfspr r3,SPRN_SRR1 /* Return SRR1 */
476 fastsleep_workaround_at_exit:
479 li r0,OPAL_CONFIG_CPU_IDLE_STATE
480 bl opal_call_realmode
484 * R3 here contains the value that will be returned to the caller
487 _GLOBAL(power7_wakeup_loss)
491 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
497 addi r1,r1,INT_FRAME_SIZE
504 * R3 here contains the value that will be returned to the caller
507 _GLOBAL(power7_wakeup_noloss)
508 lbz r0,PACA_NAPSTATELOST(r13)
510 bne power7_wakeup_loss
513 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
518 addi r1,r1,INT_FRAME_SIZE