idle_power7.S 12 KB

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