idle_power7.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * This file contains the power_save function for Power7 CPUs.
  3. *
  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.
  8. */
  9. #include <linux/threads.h>
  10. #include <asm/processor.h>
  11. #include <asm/page.h>
  12. #include <asm/cputable.h>
  13. #include <asm/thread_info.h>
  14. #include <asm/ppc_asm.h>
  15. #include <asm/asm-offsets.h>
  16. #include <asm/ppc-opcode.h>
  17. #include <asm/hw_irq.h>
  18. #include <asm/kvm_book3s_asm.h>
  19. #include <asm/opal.h>
  20. #include <asm/cpuidle.h>
  21. #include <asm/mmu-hash64.h>
  22. #undef DEBUG
  23. /*
  24. * Use unused space in the interrupt stack to save and restore
  25. * registers for winkle support.
  26. */
  27. #define _SDR1 GPR3
  28. #define _RPR GPR4
  29. #define _SPURR GPR5
  30. #define _PURR GPR6
  31. #define _TSCR GPR7
  32. #define _DSCR GPR8
  33. #define _AMOR GPR9
  34. #define _WORT GPR10
  35. #define _WORC GPR11
  36. /* Idle state entry routines */
  37. #define IDLE_STATE_ENTER_SEQ(IDLE_INST) \
  38. /* Magic NAP/SLEEP/WINKLE mode enter sequence */ \
  39. std r0,0(r1); \
  40. ptesync; \
  41. ld r0,0(r1); \
  42. 1: cmpd cr0,r0,r0; \
  43. bne 1b; \
  44. IDLE_INST; \
  45. b .
  46. .text
  47. /*
  48. * Used by threads when the lock bit of core_idle_state is set.
  49. * Threads will spin in HMT_LOW until the lock bit is cleared.
  50. * r14 - pointer to core_idle_state
  51. * r15 - used to load contents of core_idle_state
  52. */
  53. core_idle_lock_held:
  54. HMT_LOW
  55. 3: lwz r15,0(r14)
  56. andi. r15,r15,PNV_CORE_IDLE_LOCK_BIT
  57. bne 3b
  58. HMT_MEDIUM
  59. lwarx r15,0,r14
  60. blr
  61. /*
  62. * Pass requested state in r3:
  63. * r3 - PNV_THREAD_NAP/SLEEP/WINKLE
  64. *
  65. * To check IRQ_HAPPENED in r4
  66. * 0 - don't check
  67. * 1 - check
  68. */
  69. _GLOBAL(power7_powersave_common)
  70. /* Use r3 to pass state nap/sleep/winkle */
  71. /* NAP is a state loss, we create a regs frame on the
  72. * stack, fill it up with the state we care about and
  73. * stick a pointer to it in PACAR1. We really only
  74. * need to save PC, some CR bits and the NV GPRs,
  75. * but for now an interrupt frame will do.
  76. */
  77. mflr r0
  78. std r0,16(r1)
  79. stdu r1,-INT_FRAME_SIZE(r1)
  80. std r0,_LINK(r1)
  81. std r0,_NIP(r1)
  82. #ifndef CONFIG_SMP
  83. /* Make sure FPU, VSX etc... are flushed as we may lose
  84. * state when going to nap mode
  85. */
  86. bl discard_lazy_cpu_state
  87. #endif /* CONFIG_SMP */
  88. /* Hard disable interrupts */
  89. mfmsr r9
  90. rldicl r9,r9,48,1
  91. rotldi r9,r9,16
  92. mtmsrd r9,1 /* hard-disable interrupts */
  93. /* Check if something happened while soft-disabled */
  94. lbz r0,PACAIRQHAPPENED(r13)
  95. andi. r0,r0,~PACA_IRQ_HARD_DIS@l
  96. beq 1f
  97. cmpwi cr0,r4,0
  98. beq 1f
  99. addi r1,r1,INT_FRAME_SIZE
  100. ld r0,16(r1)
  101. li r3,0 /* Return 0 (no nap) */
  102. mtlr r0
  103. blr
  104. 1: /* We mark irqs hard disabled as this is the state we'll
  105. * be in when returning and we need to tell arch_local_irq_restore()
  106. * about it
  107. */
  108. li r0,PACA_IRQ_HARD_DIS
  109. stb r0,PACAIRQHAPPENED(r13)
  110. /* We haven't lost state ... yet */
  111. li r0,0
  112. stb r0,PACA_NAPSTATELOST(r13)
  113. /* Continue saving state */
  114. SAVE_GPR(2, r1)
  115. SAVE_NVGPRS(r1)
  116. mfcr r4
  117. std r4,_CCR(r1)
  118. std r9,_MSR(r1)
  119. std r1,PACAR1(r13)
  120. /*
  121. * Go to real mode to do the nap, as required by the architecture.
  122. * Also, we need to be in real mode before setting hwthread_state,
  123. * because as soon as we do that, another thread can switch
  124. * the MMU context to the guest.
  125. */
  126. LOAD_REG_IMMEDIATE(r5, MSR_IDLE)
  127. li r6, MSR_RI
  128. andc r6, r9, r6
  129. LOAD_REG_ADDR(r7, power7_enter_nap_mode)
  130. mtmsrd r6, 1 /* clear RI before setting SRR0/1 */
  131. mtspr SPRN_SRR0, r7
  132. mtspr SPRN_SRR1, r5
  133. rfid
  134. .globl power7_enter_nap_mode
  135. power7_enter_nap_mode:
  136. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  137. /* Tell KVM we're napping */
  138. li r4,KVM_HWTHREAD_IN_NAP
  139. stb r4,HSTATE_HWTHREAD_STATE(r13)
  140. #endif
  141. stb r3,PACA_THREAD_IDLE_STATE(r13)
  142. cmpwi cr3,r3,PNV_THREAD_SLEEP
  143. bge cr3,2f
  144. IDLE_STATE_ENTER_SEQ(PPC_NAP)
  145. /* No return */
  146. 2:
  147. /* Sleep or winkle */
  148. lbz r7,PACA_THREAD_MASK(r13)
  149. ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
  150. lwarx_loop1:
  151. lwarx r15,0,r14
  152. andi. r9,r15,PNV_CORE_IDLE_LOCK_BIT
  153. bnel core_idle_lock_held
  154. andc r15,r15,r7 /* Clear thread bit */
  155. andi. r15,r15,PNV_CORE_IDLE_THREAD_BITS
  156. /*
  157. * If cr0 = 0, then current thread is the last thread of the core entering
  158. * sleep. Last thread needs to execute the hardware bug workaround code if
  159. * required by the platform.
  160. * Make the workaround call unconditionally here. The below branch call is
  161. * patched out when the idle states are discovered if the platform does not
  162. * require it.
  163. */
  164. .global pnv_fastsleep_workaround_at_entry
  165. pnv_fastsleep_workaround_at_entry:
  166. beq fastsleep_workaround_at_entry
  167. stwcx. r15,0,r14
  168. bne- lwarx_loop1
  169. isync
  170. common_enter: /* common code for all the threads entering sleep or winkle */
  171. bgt cr3,enter_winkle
  172. IDLE_STATE_ENTER_SEQ(PPC_SLEEP)
  173. fastsleep_workaround_at_entry:
  174. ori r15,r15,PNV_CORE_IDLE_LOCK_BIT
  175. stwcx. r15,0,r14
  176. bne- lwarx_loop1
  177. isync
  178. /* Fast sleep workaround */
  179. li r3,1
  180. li r4,1
  181. li r0,OPAL_CONFIG_CPU_IDLE_STATE
  182. bl opal_call_realmode
  183. /* Clear Lock bit */
  184. li r0,0
  185. lwsync
  186. stw r0,0(r14)
  187. b common_enter
  188. enter_winkle:
  189. /*
  190. * Note all register i.e per-core, per-subcore or per-thread is saved
  191. * here since any thread in the core might wake up first
  192. */
  193. mfspr r3,SPRN_SDR1
  194. std r3,_SDR1(r1)
  195. mfspr r3,SPRN_RPR
  196. std r3,_RPR(r1)
  197. mfspr r3,SPRN_SPURR
  198. std r3,_SPURR(r1)
  199. mfspr r3,SPRN_PURR
  200. std r3,_PURR(r1)
  201. mfspr r3,SPRN_TSCR
  202. std r3,_TSCR(r1)
  203. mfspr r3,SPRN_DSCR
  204. std r3,_DSCR(r1)
  205. mfspr r3,SPRN_AMOR
  206. std r3,_AMOR(r1)
  207. mfspr r3,SPRN_WORT
  208. std r3,_WORT(r1)
  209. mfspr r3,SPRN_WORC
  210. std r3,_WORC(r1)
  211. IDLE_STATE_ENTER_SEQ(PPC_WINKLE)
  212. _GLOBAL(power7_idle)
  213. /* Now check if user or arch enabled NAP mode */
  214. LOAD_REG_ADDRBASE(r3,powersave_nap)
  215. lwz r4,ADDROFF(powersave_nap)(r3)
  216. cmpwi 0,r4,0
  217. beqlr
  218. li r3, 1
  219. /* fall through */
  220. _GLOBAL(power7_nap)
  221. mr r4,r3
  222. li r3,PNV_THREAD_NAP
  223. b power7_powersave_common
  224. /* No return */
  225. _GLOBAL(power7_sleep)
  226. li r3,PNV_THREAD_SLEEP
  227. li r4,1
  228. b power7_powersave_common
  229. /* No return */
  230. _GLOBAL(power7_winkle)
  231. li r3,3
  232. li r4,1
  233. b power7_powersave_common
  234. /* No return */
  235. #define CHECK_HMI_INTERRUPT \
  236. mfspr r0,SPRN_SRR1; \
  237. BEGIN_FTR_SECTION_NESTED(66); \
  238. rlwinm r0,r0,45-31,0xf; /* extract wake reason field (P8) */ \
  239. FTR_SECTION_ELSE_NESTED(66); \
  240. rlwinm r0,r0,45-31,0xe; /* P7 wake reason field is 3 bits */ \
  241. ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66); \
  242. cmpwi r0,0xa; /* Hypervisor maintenance ? */ \
  243. bne 20f; \
  244. /* Invoke opal call to handle hmi */ \
  245. ld r2,PACATOC(r13); \
  246. ld r1,PACAR1(r13); \
  247. std r3,ORIG_GPR3(r1); /* Save original r3 */ \
  248. li r0,OPAL_HANDLE_HMI; /* Pass opal token argument*/ \
  249. bl opal_call_realmode; \
  250. ld r3,ORIG_GPR3(r1); /* Restore original r3 */ \
  251. 20: nop;
  252. _GLOBAL(power7_wakeup_tb_loss)
  253. ld r2,PACATOC(r13);
  254. ld r1,PACAR1(r13)
  255. /*
  256. * Before entering any idle state, the NVGPRs are saved in the stack
  257. * and they are restored before switching to the process context. Hence
  258. * until they are restored, they are free to be used.
  259. *
  260. * Save SRR1 in a NVGPR as it might be clobbered in opal_call_realmode
  261. * (called in CHECK_HMI_INTERRUPT). SRR1 is required to determine the
  262. * wakeup reason if we branch to kvm_start_guest.
  263. */
  264. mfspr r16,SPRN_SRR1
  265. BEGIN_FTR_SECTION
  266. CHECK_HMI_INTERRUPT
  267. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  268. lbz r7,PACA_THREAD_MASK(r13)
  269. ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
  270. lwarx_loop2:
  271. lwarx r15,0,r14
  272. andi. r9,r15,PNV_CORE_IDLE_LOCK_BIT
  273. /*
  274. * Lock bit is set in one of the 2 cases-
  275. * a. In the sleep/winkle enter path, the last thread is executing
  276. * fastsleep workaround code.
  277. * b. In the wake up path, another thread is executing fastsleep
  278. * workaround undo code or resyncing timebase or restoring context
  279. * In either case loop until the lock bit is cleared.
  280. */
  281. bnel core_idle_lock_held
  282. cmpwi cr2,r15,0
  283. lbz r4,PACA_SUBCORE_SIBLING_MASK(r13)
  284. and r4,r4,r15
  285. cmpwi cr1,r4,0 /* Check if first in subcore */
  286. /*
  287. * At this stage
  288. * cr1 - 0b0100 if first thread to wakeup in subcore
  289. * cr2 - 0b0100 if first thread to wakeup in core
  290. * cr3- 0b0010 if waking up from sleep or winkle
  291. * cr4 - 0b0100 if waking up from winkle
  292. */
  293. or r15,r15,r7 /* Set thread bit */
  294. beq cr1,first_thread_in_subcore
  295. /* Not first thread in subcore to wake up */
  296. stwcx. r15,0,r14
  297. bne- lwarx_loop2
  298. isync
  299. b common_exit
  300. first_thread_in_subcore:
  301. /* First thread in subcore to wakeup */
  302. ori r15,r15,PNV_CORE_IDLE_LOCK_BIT
  303. stwcx. r15,0,r14
  304. bne- lwarx_loop2
  305. isync
  306. /*
  307. * If waking up from sleep, subcore state is not lost. Hence
  308. * skip subcore state restore
  309. */
  310. bne cr4,subcore_state_restored
  311. /* Restore per-subcore state */
  312. ld r4,_SDR1(r1)
  313. mtspr SPRN_SDR1,r4
  314. ld r4,_RPR(r1)
  315. mtspr SPRN_RPR,r4
  316. ld r4,_AMOR(r1)
  317. mtspr SPRN_AMOR,r4
  318. subcore_state_restored:
  319. /*
  320. * Check if the thread is also the first thread in the core. If not,
  321. * skip to clear_lock.
  322. */
  323. bne cr2,clear_lock
  324. first_thread_in_core:
  325. /*
  326. * First thread in the core waking up from fastsleep. It needs to
  327. * call the fastsleep workaround code if the platform requires it.
  328. * Call it unconditionally here. The below branch instruction will
  329. * be patched out when the idle states are discovered if platform
  330. * does not require workaround.
  331. */
  332. .global pnv_fastsleep_workaround_at_exit
  333. pnv_fastsleep_workaround_at_exit:
  334. b fastsleep_workaround_at_exit
  335. timebase_resync:
  336. /* Do timebase resync if we are waking up from sleep. Use cr3 value
  337. * set in exceptions-64s.S */
  338. ble cr3,clear_lock
  339. /* Time base re-sync */
  340. li r0,OPAL_RESYNC_TIMEBASE
  341. bl opal_call_realmode;
  342. /* TODO: Check r3 for failure */
  343. /*
  344. * If waking up from sleep, per core state is not lost, skip to
  345. * clear_lock.
  346. */
  347. bne cr4,clear_lock
  348. /* Restore per core state */
  349. ld r4,_TSCR(r1)
  350. mtspr SPRN_TSCR,r4
  351. ld r4,_WORC(r1)
  352. mtspr SPRN_WORC,r4
  353. clear_lock:
  354. andi. r15,r15,PNV_CORE_IDLE_THREAD_BITS
  355. lwsync
  356. stw r15,0(r14)
  357. common_exit:
  358. /*
  359. * Common to all threads.
  360. *
  361. * If waking up from sleep, hypervisor state is not lost. Hence
  362. * skip hypervisor state restore.
  363. */
  364. bne cr4,hypervisor_state_restored
  365. /* Waking up from winkle */
  366. /* Restore per thread state */
  367. bl __restore_cpu_power8
  368. /* Restore SLB from PACA */
  369. ld r8,PACA_SLBSHADOWPTR(r13)
  370. .rept SLB_NUM_BOLTED
  371. li r3, SLBSHADOW_SAVEAREA
  372. LDX_BE r5, r8, r3
  373. addi r3, r3, 8
  374. LDX_BE r6, r8, r3
  375. andis. r7,r5,SLB_ESID_V@h
  376. beq 1f
  377. slbmte r6,r5
  378. 1: addi r8,r8,16
  379. .endr
  380. ld r4,_SPURR(r1)
  381. mtspr SPRN_SPURR,r4
  382. ld r4,_PURR(r1)
  383. mtspr SPRN_PURR,r4
  384. ld r4,_DSCR(r1)
  385. mtspr SPRN_DSCR,r4
  386. ld r4,_WORT(r1)
  387. mtspr SPRN_WORT,r4
  388. hypervisor_state_restored:
  389. li r5,PNV_THREAD_RUNNING
  390. stb r5,PACA_THREAD_IDLE_STATE(r13)
  391. mtspr SPRN_SRR1,r16
  392. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  393. li r0,KVM_HWTHREAD_IN_KERNEL
  394. stb r0,HSTATE_HWTHREAD_STATE(r13)
  395. /* Order setting hwthread_state vs. testing hwthread_req */
  396. sync
  397. lbz r0,HSTATE_HWTHREAD_REQ(r13)
  398. cmpwi r0,0
  399. beq 6f
  400. b kvm_start_guest
  401. 6:
  402. #endif
  403. REST_NVGPRS(r1)
  404. REST_GPR(2, r1)
  405. ld r3,_CCR(r1)
  406. ld r4,_MSR(r1)
  407. ld r5,_NIP(r1)
  408. addi r1,r1,INT_FRAME_SIZE
  409. mtcr r3
  410. mfspr r3,SPRN_SRR1 /* Return SRR1 */
  411. mtspr SPRN_SRR1,r4
  412. mtspr SPRN_SRR0,r5
  413. rfid
  414. fastsleep_workaround_at_exit:
  415. li r3,1
  416. li r4,0
  417. li r0,OPAL_CONFIG_CPU_IDLE_STATE
  418. bl opal_call_realmode
  419. b timebase_resync
  420. /*
  421. * R3 here contains the value that will be returned to the caller
  422. * of power7_nap.
  423. */
  424. _GLOBAL(power7_wakeup_loss)
  425. ld r1,PACAR1(r13)
  426. BEGIN_FTR_SECTION
  427. CHECK_HMI_INTERRUPT
  428. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  429. REST_NVGPRS(r1)
  430. REST_GPR(2, r1)
  431. ld r6,_CCR(r1)
  432. ld r4,_MSR(r1)
  433. ld r5,_NIP(r1)
  434. addi r1,r1,INT_FRAME_SIZE
  435. mtcr r6
  436. mtspr SPRN_SRR1,r4
  437. mtspr SPRN_SRR0,r5
  438. rfid
  439. /*
  440. * R3 here contains the value that will be returned to the caller
  441. * of power7_nap.
  442. */
  443. _GLOBAL(power7_wakeup_noloss)
  444. lbz r0,PACA_NAPSTATELOST(r13)
  445. cmpwi r0,0
  446. bne power7_wakeup_loss
  447. BEGIN_FTR_SECTION
  448. CHECK_HMI_INTERRUPT
  449. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  450. ld r1,PACAR1(r13)
  451. ld r6,_CCR(r1)
  452. ld r4,_MSR(r1)
  453. ld r5,_NIP(r1)
  454. addi r1,r1,INT_FRAME_SIZE
  455. mtcr r6
  456. mtspr SPRN_SRR1,r4
  457. mtspr SPRN_SRR0,r5
  458. rfid