idle_power7.S 12 KB

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