idle_book3s.S 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * This file contains idle entry/exit functions for POWER7,
  3. * POWER8 and POWER9 CPUs.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <linux/threads.h>
  11. #include <asm/processor.h>
  12. #include <asm/page.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>
  20. #include <asm/opal.h>
  21. #include <asm/cpuidle.h>
  22. #include <asm/exception-64s.h>
  23. #include <asm/book3s/64/mmu-hash.h>
  24. #include <asm/mmu.h>
  25. #undef DEBUG
  26. /*
  27. * Use unused space in the interrupt stack to save and restore
  28. * registers for winkle support.
  29. */
  30. #define _MMCR0 GPR0
  31. #define _SDR1 GPR3
  32. #define _PTCR GPR3
  33. #define _RPR GPR4
  34. #define _SPURR GPR5
  35. #define _PURR GPR6
  36. #define _TSCR GPR7
  37. #define _DSCR GPR8
  38. #define _AMOR GPR9
  39. #define _WORT GPR10
  40. #define _WORC GPR11
  41. #define _LPCR GPR12
  42. #define PSSCR_EC_ESL_MASK_SHIFTED (PSSCR_EC | PSSCR_ESL) >> 16
  43. .text
  44. /*
  45. * Used by threads before entering deep idle states. Saves SPRs
  46. * in interrupt stack frame
  47. */
  48. save_sprs_to_stack:
  49. /*
  50. * Note all register i.e per-core, per-subcore or per-thread is saved
  51. * here since any thread in the core might wake up first
  52. */
  53. BEGIN_FTR_SECTION
  54. /*
  55. * Note - SDR1 is dropped in Power ISA v3. Hence not restoring
  56. * SDR1 here
  57. */
  58. mfspr r3,SPRN_PTCR
  59. std r3,_PTCR(r1)
  60. mfspr r3,SPRN_LPCR
  61. std r3,_LPCR(r1)
  62. FTR_SECTION_ELSE
  63. mfspr r3,SPRN_SDR1
  64. std r3,_SDR1(r1)
  65. ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_300)
  66. mfspr r3,SPRN_RPR
  67. std r3,_RPR(r1)
  68. mfspr r3,SPRN_SPURR
  69. std r3,_SPURR(r1)
  70. mfspr r3,SPRN_PURR
  71. std r3,_PURR(r1)
  72. mfspr r3,SPRN_TSCR
  73. std r3,_TSCR(r1)
  74. mfspr r3,SPRN_DSCR
  75. std r3,_DSCR(r1)
  76. mfspr r3,SPRN_AMOR
  77. std r3,_AMOR(r1)
  78. mfspr r3,SPRN_WORT
  79. std r3,_WORT(r1)
  80. mfspr r3,SPRN_WORC
  81. std r3,_WORC(r1)
  82. /*
  83. * On POWER9, there are idle states such as stop4, invoked via cpuidle,
  84. * that lose hypervisor resources. In such cases, we need to save
  85. * additional SPRs before entering those idle states so that they can
  86. * be restored to their older values on wakeup from the idle state.
  87. *
  88. * On POWER8, the only such deep idle state is winkle which is used
  89. * only in the context of CPU-Hotplug, where these additional SPRs are
  90. * reinitiazed to a sane value. Hence there is no need to save/restore
  91. * these SPRs.
  92. */
  93. BEGIN_FTR_SECTION
  94. blr
  95. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  96. power9_save_additional_sprs:
  97. mfspr r3, SPRN_PID
  98. mfspr r4, SPRN_LDBAR
  99. std r3, STOP_PID(r13)
  100. std r4, STOP_LDBAR(r13)
  101. mfspr r3, SPRN_FSCR
  102. mfspr r4, SPRN_HFSCR
  103. std r3, STOP_FSCR(r13)
  104. std r4, STOP_HFSCR(r13)
  105. mfspr r3, SPRN_MMCRA
  106. mfspr r4, SPRN_MMCR0
  107. std r3, STOP_MMCRA(r13)
  108. std r4, _MMCR0(r1)
  109. mfspr r3, SPRN_MMCR1
  110. mfspr r4, SPRN_MMCR2
  111. std r3, STOP_MMCR1(r13)
  112. std r4, STOP_MMCR2(r13)
  113. blr
  114. power9_restore_additional_sprs:
  115. ld r3,_LPCR(r1)
  116. ld r4, STOP_PID(r13)
  117. mtspr SPRN_LPCR,r3
  118. mtspr SPRN_PID, r4
  119. ld r3, STOP_LDBAR(r13)
  120. ld r4, STOP_FSCR(r13)
  121. mtspr SPRN_LDBAR, r3
  122. mtspr SPRN_FSCR, r4
  123. ld r3, STOP_HFSCR(r13)
  124. ld r4, STOP_MMCRA(r13)
  125. mtspr SPRN_HFSCR, r3
  126. mtspr SPRN_MMCRA, r4
  127. ld r3, _MMCR0(r1)
  128. ld r4, STOP_MMCR1(r13)
  129. mtspr SPRN_MMCR0, r3
  130. mtspr SPRN_MMCR1, r4
  131. ld r3, STOP_MMCR2(r13)
  132. mtspr SPRN_MMCR2, r3
  133. blr
  134. /*
  135. * Used by threads when the lock bit of core_idle_state is set.
  136. * Threads will spin in HMT_LOW until the lock bit is cleared.
  137. * r14 - pointer to core_idle_state
  138. * r15 - used to load contents of core_idle_state
  139. * r9 - used as a temporary variable
  140. */
  141. core_idle_lock_held:
  142. HMT_LOW
  143. 3: lwz r15,0(r14)
  144. andis. r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
  145. bne 3b
  146. HMT_MEDIUM
  147. lwarx r15,0,r14
  148. andis. r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
  149. bne- core_idle_lock_held
  150. blr
  151. /*
  152. * Pass requested state in r3:
  153. * r3 - PNV_THREAD_NAP/SLEEP/WINKLE in POWER8
  154. * - Requested PSSCR value in POWER9
  155. *
  156. * Address of idle handler to branch to in realmode in r4
  157. */
  158. pnv_powersave_common:
  159. /* Use r3 to pass state nap/sleep/winkle */
  160. /* NAP is a state loss, we create a regs frame on the
  161. * stack, fill it up with the state we care about and
  162. * stick a pointer to it in PACAR1. We really only
  163. * need to save PC, some CR bits and the NV GPRs,
  164. * but for now an interrupt frame will do.
  165. */
  166. mtctr r4
  167. mflr r0
  168. std r0,16(r1)
  169. stdu r1,-INT_FRAME_SIZE(r1)
  170. std r0,_LINK(r1)
  171. std r0,_NIP(r1)
  172. /* We haven't lost state ... yet */
  173. li r0,0
  174. stb r0,PACA_NAPSTATELOST(r13)
  175. /* Continue saving state */
  176. SAVE_GPR(2, r1)
  177. SAVE_NVGPRS(r1)
  178. mfcr r5
  179. std r5,_CCR(r1)
  180. std r1,PACAR1(r13)
  181. BEGIN_FTR_SECTION
  182. /*
  183. * POWER9 does not require real mode to stop, and presently does not
  184. * set hwthread_state for KVM (threads don't share MMU context), so
  185. * we can remain in virtual mode for this.
  186. */
  187. bctr
  188. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  189. /*
  190. * POWER8
  191. * Go to real mode to do the nap, as required by the architecture.
  192. * Also, we need to be in real mode before setting hwthread_state,
  193. * because as soon as we do that, another thread can switch
  194. * the MMU context to the guest.
  195. */
  196. LOAD_REG_IMMEDIATE(r7, MSR_IDLE)
  197. mtmsrd r7,0
  198. bctr
  199. /*
  200. * This is the sequence required to execute idle instructions, as
  201. * specified in ISA v2.07 (and earlier). MSR[IR] and MSR[DR] must be 0.
  202. */
  203. #define IDLE_STATE_ENTER_SEQ_NORET(IDLE_INST) \
  204. /* Magic NAP/SLEEP/WINKLE mode enter sequence */ \
  205. std r0,0(r1); \
  206. ptesync; \
  207. ld r0,0(r1); \
  208. 236: cmpd cr0,r0,r0; \
  209. bne 236b; \
  210. IDLE_INST;
  211. .globl pnv_enter_arch207_idle_mode
  212. pnv_enter_arch207_idle_mode:
  213. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  214. /* Tell KVM we're entering idle */
  215. li r4,KVM_HWTHREAD_IN_IDLE
  216. /******************************************************/
  217. /* N O T E W E L L ! ! ! N O T E W E L L */
  218. /* The following store to HSTATE_HWTHREAD_STATE(r13) */
  219. /* MUST occur in real mode, i.e. with the MMU off, */
  220. /* and the MMU must stay off until we clear this flag */
  221. /* and test HSTATE_HWTHREAD_REQ(r13) in */
  222. /* pnv_powersave_wakeup in this file. */
  223. /* The reason is that another thread can switch the */
  224. /* MMU to a guest context whenever this flag is set */
  225. /* to KVM_HWTHREAD_IN_IDLE, and if the MMU was on, */
  226. /* that would potentially cause this thread to start */
  227. /* executing instructions from guest memory in */
  228. /* hypervisor mode, leading to a host crash or data */
  229. /* corruption, or worse. */
  230. /******************************************************/
  231. stb r4,HSTATE_HWTHREAD_STATE(r13)
  232. #endif
  233. stb r3,PACA_THREAD_IDLE_STATE(r13)
  234. cmpwi cr3,r3,PNV_THREAD_SLEEP
  235. bge cr3,2f
  236. IDLE_STATE_ENTER_SEQ_NORET(PPC_NAP)
  237. /* No return */
  238. 2:
  239. /* Sleep or winkle */
  240. lbz r7,PACA_THREAD_MASK(r13)
  241. ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
  242. li r5,0
  243. beq cr3,3f
  244. lis r5,PNV_CORE_IDLE_WINKLE_COUNT@h
  245. 3:
  246. lwarx_loop1:
  247. lwarx r15,0,r14
  248. andis. r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
  249. bnel- core_idle_lock_held
  250. add r15,r15,r5 /* Add if winkle */
  251. andc r15,r15,r7 /* Clear thread bit */
  252. andi. r9,r15,PNV_CORE_IDLE_THREAD_BITS
  253. /*
  254. * If cr0 = 0, then current thread is the last thread of the core entering
  255. * sleep. Last thread needs to execute the hardware bug workaround code if
  256. * required by the platform.
  257. * Make the workaround call unconditionally here. The below branch call is
  258. * patched out when the idle states are discovered if the platform does not
  259. * require it.
  260. */
  261. .global pnv_fastsleep_workaround_at_entry
  262. pnv_fastsleep_workaround_at_entry:
  263. beq fastsleep_workaround_at_entry
  264. stwcx. r15,0,r14
  265. bne- lwarx_loop1
  266. isync
  267. common_enter: /* common code for all the threads entering sleep or winkle */
  268. bgt cr3,enter_winkle
  269. IDLE_STATE_ENTER_SEQ_NORET(PPC_SLEEP)
  270. fastsleep_workaround_at_entry:
  271. oris r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
  272. stwcx. r15,0,r14
  273. bne- lwarx_loop1
  274. isync
  275. /* Fast sleep workaround */
  276. li r3,1
  277. li r4,1
  278. bl opal_config_cpu_idle_state
  279. /* Unlock */
  280. xoris r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
  281. lwsync
  282. stw r15,0(r14)
  283. b common_enter
  284. enter_winkle:
  285. bl save_sprs_to_stack
  286. IDLE_STATE_ENTER_SEQ_NORET(PPC_WINKLE)
  287. /*
  288. * r3 - PSSCR value corresponding to the requested stop state.
  289. */
  290. power_enter_stop:
  291. /*
  292. * Check if we are executing the lite variant with ESL=EC=0
  293. */
  294. andis. r4,r3,PSSCR_EC_ESL_MASK_SHIFTED
  295. clrldi r3,r3,60 /* r3 = Bits[60:63] = Requested Level (RL) */
  296. bne .Lhandle_esl_ec_set
  297. PPC_STOP
  298. li r3,0 /* Since we didn't lose state, return 0 */
  299. std r3, PACA_REQ_PSSCR(r13)
  300. /*
  301. * pnv_wakeup_noloss() expects r12 to contain the SRR1 value so
  302. * it can determine if the wakeup reason is an HMI in
  303. * CHECK_HMI_INTERRUPT.
  304. *
  305. * However, when we wakeup with ESL=0, SRR1 will not contain the wakeup
  306. * reason, so there is no point setting r12 to SRR1.
  307. *
  308. * Further, we clear r12 here, so that we don't accidentally enter the
  309. * HMI in pnv_wakeup_noloss() if the value of r12[42:45] == WAKE_HMI.
  310. */
  311. li r12, 0
  312. b pnv_wakeup_noloss
  313. .Lhandle_esl_ec_set:
  314. BEGIN_FTR_SECTION
  315. /*
  316. * POWER9 DD2.0 or earlier can incorrectly set PMAO when waking up after
  317. * a state-loss idle. Saving and restoring MMCR0 over idle is a
  318. * workaround.
  319. */
  320. mfspr r4,SPRN_MMCR0
  321. std r4,_MMCR0(r1)
  322. END_FTR_SECTION_IFCLR(CPU_FTR_POWER9_DD2_1)
  323. /*
  324. * Check if the requested state is a deep idle state.
  325. */
  326. LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state)
  327. ld r4,ADDROFF(pnv_first_deep_stop_state)(r5)
  328. cmpd r3,r4
  329. bge .Lhandle_deep_stop
  330. PPC_STOP /* Does not return (system reset interrupt) */
  331. .Lhandle_deep_stop:
  332. /*
  333. * Entering deep idle state.
  334. * Clear thread bit in PACA_CORE_IDLE_STATE, save SPRs to
  335. * stack and enter stop
  336. */
  337. lbz r7,PACA_THREAD_MASK(r13)
  338. ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
  339. lwarx_loop_stop:
  340. lwarx r15,0,r14
  341. andis. r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
  342. bnel- core_idle_lock_held
  343. andc r15,r15,r7 /* Clear thread bit */
  344. stwcx. r15,0,r14
  345. bne- lwarx_loop_stop
  346. isync
  347. bl save_sprs_to_stack
  348. PPC_STOP /* Does not return (system reset interrupt) */
  349. /*
  350. * Entered with MSR[EE]=0 and no soft-masked interrupts pending.
  351. * r3 contains desired idle state (PNV_THREAD_NAP/SLEEP/WINKLE).
  352. */
  353. _GLOBAL(power7_idle_insn)
  354. /* Now check if user or arch enabled NAP mode */
  355. LOAD_REG_ADDR(r4, pnv_enter_arch207_idle_mode)
  356. b pnv_powersave_common
  357. #define CHECK_HMI_INTERRUPT \
  358. BEGIN_FTR_SECTION_NESTED(66); \
  359. rlwinm r0,r12,45-31,0xf; /* extract wake reason field (P8) */ \
  360. FTR_SECTION_ELSE_NESTED(66); \
  361. rlwinm r0,r12,45-31,0xe; /* P7 wake reason field is 3 bits */ \
  362. ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66); \
  363. cmpwi r0,0xa; /* Hypervisor maintenance ? */ \
  364. bne+ 20f; \
  365. /* Invoke opal call to handle hmi */ \
  366. ld r2,PACATOC(r13); \
  367. ld r1,PACAR1(r13); \
  368. std r3,ORIG_GPR3(r1); /* Save original r3 */ \
  369. li r3,0; /* NULL argument */ \
  370. bl hmi_exception_realmode; \
  371. nop; \
  372. ld r3,ORIG_GPR3(r1); /* Restore original r3 */ \
  373. 20: nop;
  374. /*
  375. * Entered with MSR[EE]=0 and no soft-masked interrupts pending.
  376. * r3 contains desired PSSCR register value.
  377. *
  378. * Offline (CPU unplug) case also must notify KVM that the CPU is
  379. * idle.
  380. */
  381. _GLOBAL(power9_offline_stop)
  382. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  383. /*
  384. * Tell KVM we're entering idle.
  385. * This does not have to be done in real mode because the P9 MMU
  386. * is independent per-thread. Some steppings share radix/hash mode
  387. * between threads, but in that case KVM has a barrier sync in real
  388. * mode before and after switching between radix and hash.
  389. */
  390. li r4,KVM_HWTHREAD_IN_IDLE
  391. stb r4,HSTATE_HWTHREAD_STATE(r13)
  392. #endif
  393. /* fall through */
  394. _GLOBAL(power9_idle_stop)
  395. std r3, PACA_REQ_PSSCR(r13)
  396. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  397. BEGIN_FTR_SECTION
  398. sync
  399. lwz r5, PACA_DONT_STOP(r13)
  400. cmpwi r5, 0
  401. bne 1f
  402. END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
  403. #endif
  404. mtspr SPRN_PSSCR,r3
  405. LOAD_REG_ADDR(r4,power_enter_stop)
  406. b pnv_powersave_common
  407. /* No return */
  408. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  409. 1:
  410. /*
  411. * We get here when TM / thread reconfiguration bug workaround
  412. * code wants to get the CPU into SMT4 mode, and therefore
  413. * we are being asked not to stop.
  414. */
  415. li r3, 0
  416. std r3, PACA_REQ_PSSCR(r13)
  417. blr /* return 0 for wakeup cause / SRR1 value */
  418. #endif
  419. /*
  420. * On waking up from stop 0,1,2 with ESL=1 on POWER9 DD1,
  421. * HSPRG0 will be set to the HSPRG0 value of one of the
  422. * threads in this core. Thus the value we have in r13
  423. * may not be this thread's paca pointer.
  424. *
  425. * Fortunately, the TIR remains invariant. Since this thread's
  426. * paca pointer is recorded in all its sibling's paca, we can
  427. * correctly recover this thread's paca pointer if we
  428. * know the index of this thread in the core.
  429. *
  430. * This index can be obtained from the TIR.
  431. *
  432. * i.e, thread's position in the core = TIR.
  433. * If this value is i, then this thread's paca is
  434. * paca->thread_sibling_pacas[i].
  435. */
  436. power9_dd1_recover_paca:
  437. mfspr r4, SPRN_TIR
  438. /*
  439. * Since each entry in thread_sibling_pacas is 8 bytes
  440. * we need to left-shift by 3 bits. Thus r4 = i * 8
  441. */
  442. sldi r4, r4, 3
  443. /* Get &paca->thread_sibling_pacas[0] in r5 */
  444. ld r5, PACA_SIBLING_PACA_PTRS(r13)
  445. /* Load paca->thread_sibling_pacas[i] into r13 */
  446. ldx r13, r4, r5
  447. SET_PACA(r13)
  448. /*
  449. * Indicate that we have lost NVGPR state
  450. * which needs to be restored from the stack.
  451. */
  452. li r3, 1
  453. stb r3,PACA_NAPSTATELOST(r13)
  454. blr
  455. /*
  456. * Called from machine check handler for powersave wakeups.
  457. * Low level machine check processing has already been done. Now just
  458. * go through the wake up path to get everything in order.
  459. *
  460. * r3 - The original SRR1 value.
  461. * Original SRR[01] have been clobbered.
  462. * MSR_RI is clear.
  463. */
  464. .global pnv_powersave_wakeup_mce
  465. pnv_powersave_wakeup_mce:
  466. /* Set cr3 for pnv_powersave_wakeup */
  467. rlwinm r11,r3,47-31,30,31
  468. cmpwi cr3,r11,2
  469. /*
  470. * Now put the original SRR1 with SRR1_WAKEMCE_RESVD as the wake
  471. * reason into r12, which allows reuse of the system reset wakeup
  472. * code without being mistaken for another type of wakeup.
  473. */
  474. oris r12,r3,SRR1_WAKEMCE_RESVD@h
  475. b pnv_powersave_wakeup
  476. /*
  477. * Called from reset vector for powersave wakeups.
  478. * cr3 - set to gt if waking up with partial/complete hypervisor state loss
  479. * r12 - SRR1
  480. */
  481. .global pnv_powersave_wakeup
  482. pnv_powersave_wakeup:
  483. ld r2, PACATOC(r13)
  484. BEGIN_FTR_SECTION
  485. BEGIN_FTR_SECTION_NESTED(70)
  486. bl power9_dd1_recover_paca
  487. END_FTR_SECTION_NESTED_IFSET(CPU_FTR_POWER9_DD1, 70)
  488. bl pnv_restore_hyp_resource_arch300
  489. FTR_SECTION_ELSE
  490. bl pnv_restore_hyp_resource_arch207
  491. ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_300)
  492. li r0,PNV_THREAD_RUNNING
  493. stb r0,PACA_THREAD_IDLE_STATE(r13) /* Clear thread state */
  494. mr r3,r12
  495. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  496. lbz r0,HSTATE_HWTHREAD_STATE(r13)
  497. cmpwi r0,KVM_HWTHREAD_IN_KERNEL
  498. beq 0f
  499. li r0,KVM_HWTHREAD_IN_KERNEL
  500. stb r0,HSTATE_HWTHREAD_STATE(r13)
  501. /* Order setting hwthread_state vs. testing hwthread_req */
  502. sync
  503. 0: lbz r0,HSTATE_HWTHREAD_REQ(r13)
  504. cmpwi r0,0
  505. beq 1f
  506. b kvm_start_guest
  507. 1:
  508. #endif
  509. /* Return SRR1 from power7_nap() */
  510. blt cr3,pnv_wakeup_noloss
  511. b pnv_wakeup_loss
  512. /*
  513. * Check whether we have woken up with hypervisor state loss.
  514. * If yes, restore hypervisor state and return back to link.
  515. *
  516. * cr3 - set to gt if waking up with partial/complete hypervisor state loss
  517. */
  518. pnv_restore_hyp_resource_arch300:
  519. /*
  520. * Workaround for POWER9, if we lost resources, the ERAT
  521. * might have been mixed up and needs flushing. We also need
  522. * to reload MMCR0 (see comment above). We also need to set
  523. * then clear bit 60 in MMCRA to ensure the PMU starts running.
  524. */
  525. blt cr3,1f
  526. BEGIN_FTR_SECTION
  527. PPC_INVALIDATE_ERAT
  528. ld r1,PACAR1(r13)
  529. ld r4,_MMCR0(r1)
  530. mtspr SPRN_MMCR0,r4
  531. END_FTR_SECTION_IFCLR(CPU_FTR_POWER9_DD2_1)
  532. mfspr r4,SPRN_MMCRA
  533. ori r4,r4,(1 << (63-60))
  534. mtspr SPRN_MMCRA,r4
  535. xori r4,r4,(1 << (63-60))
  536. mtspr SPRN_MMCRA,r4
  537. 1:
  538. /*
  539. * POWER ISA 3. Use PSSCR to determine if we
  540. * are waking up from deep idle state
  541. */
  542. LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state)
  543. ld r4,ADDROFF(pnv_first_deep_stop_state)(r5)
  544. BEGIN_FTR_SECTION_NESTED(71)
  545. /*
  546. * Assume that we are waking up from the state
  547. * same as the Requested Level (RL) in the PSSCR
  548. * which are Bits 60-63
  549. */
  550. ld r5,PACA_REQ_PSSCR(r13)
  551. rldicl r5,r5,0,60
  552. FTR_SECTION_ELSE_NESTED(71)
  553. /*
  554. * 0-3 bits correspond to Power-Saving Level Status
  555. * which indicates the idle state we are waking up from
  556. */
  557. mfspr r5, SPRN_PSSCR
  558. rldicl r5,r5,4,60
  559. ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_POWER9_DD1, 71)
  560. li r0, 0 /* clear requested_psscr to say we're awake */
  561. std r0, PACA_REQ_PSSCR(r13)
  562. cmpd cr4,r5,r4
  563. bge cr4,pnv_wakeup_tb_loss /* returns to caller */
  564. blr /* Waking up without hypervisor state loss. */
  565. /* Same calling convention as arch300 */
  566. pnv_restore_hyp_resource_arch207:
  567. /*
  568. * POWER ISA 2.07 or less.
  569. * Check if we slept with sleep or winkle.
  570. */
  571. lbz r4,PACA_THREAD_IDLE_STATE(r13)
  572. cmpwi cr2,r4,PNV_THREAD_NAP
  573. bgt cr2,pnv_wakeup_tb_loss /* Either sleep or Winkle */
  574. /*
  575. * We fall through here if PACA_THREAD_IDLE_STATE shows we are waking
  576. * up from nap. At this stage CR3 shouldn't contains 'gt' since that
  577. * indicates we are waking with hypervisor state loss from nap.
  578. */
  579. bgt cr3,.
  580. blr /* Waking up without hypervisor state loss */
  581. /*
  582. * Called if waking up from idle state which can cause either partial or
  583. * complete hyp state loss.
  584. * In POWER8, called if waking up from fastsleep or winkle
  585. * In POWER9, called if waking up from stop state >= pnv_first_deep_stop_state
  586. *
  587. * r13 - PACA
  588. * cr3 - gt if waking up with partial/complete hypervisor state loss
  589. *
  590. * If ISA300:
  591. * cr4 - gt or eq if waking up from complete hypervisor state loss.
  592. *
  593. * If ISA207:
  594. * r4 - PACA_THREAD_IDLE_STATE
  595. */
  596. pnv_wakeup_tb_loss:
  597. ld r1,PACAR1(r13)
  598. /*
  599. * Before entering any idle state, the NVGPRs are saved in the stack.
  600. * If there was a state loss, or PACA_NAPSTATELOST was set, then the
  601. * NVGPRs are restored. If we are here, it is likely that state is lost,
  602. * but not guaranteed -- neither ISA207 nor ISA300 tests to reach
  603. * here are the same as the test to restore NVGPRS:
  604. * PACA_THREAD_IDLE_STATE test for ISA207, PSSCR test for ISA300,
  605. * and SRR1 test for restoring NVGPRs.
  606. *
  607. * We are about to clobber NVGPRs now, so set NAPSTATELOST to
  608. * guarantee they will always be restored. This might be tightened
  609. * with careful reading of specs (particularly for ISA300) but this
  610. * is already a slow wakeup path and it's simpler to be safe.
  611. */
  612. li r0,1
  613. stb r0,PACA_NAPSTATELOST(r13)
  614. /*
  615. *
  616. * Save SRR1 and LR in NVGPRs as they might be clobbered in
  617. * opal_call() (called in CHECK_HMI_INTERRUPT). SRR1 is required
  618. * to determine the wakeup reason if we branch to kvm_start_guest. LR
  619. * is required to return back to reset vector after hypervisor state
  620. * restore is complete.
  621. */
  622. mr r19,r12
  623. mr r18,r4
  624. mflr r17
  625. BEGIN_FTR_SECTION
  626. CHECK_HMI_INTERRUPT
  627. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  628. ld r14,PACA_CORE_IDLE_STATE_PTR(r13)
  629. lbz r7,PACA_THREAD_MASK(r13)
  630. /*
  631. * Take the core lock to synchronize against other threads.
  632. *
  633. * Lock bit is set in one of the 2 cases-
  634. * a. In the sleep/winkle enter path, the last thread is executing
  635. * fastsleep workaround code.
  636. * b. In the wake up path, another thread is executing fastsleep
  637. * workaround undo code or resyncing timebase or restoring context
  638. * In either case loop until the lock bit is cleared.
  639. */
  640. 1:
  641. lwarx r15,0,r14
  642. andis. r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
  643. bnel- core_idle_lock_held
  644. oris r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
  645. stwcx. r15,0,r14
  646. bne- 1b
  647. isync
  648. andi. r9,r15,PNV_CORE_IDLE_THREAD_BITS
  649. cmpwi cr2,r9,0
  650. /*
  651. * At this stage
  652. * cr2 - eq if first thread to wakeup in core
  653. * cr3- gt if waking up with partial/complete hypervisor state loss
  654. * ISA300:
  655. * cr4 - gt or eq if waking up from complete hypervisor state loss.
  656. */
  657. BEGIN_FTR_SECTION
  658. /*
  659. * Were we in winkle?
  660. * If yes, check if all threads were in winkle, decrement our
  661. * winkle count, set all thread winkle bits if all were in winkle.
  662. * Check if our thread has a winkle bit set, and set cr4 accordingly
  663. * (to match ISA300, above). Pseudo-code for core idle state
  664. * transitions for ISA207 is as follows (everything happens atomically
  665. * due to store conditional and/or lock bit):
  666. *
  667. * nap_idle() { }
  668. * nap_wake() { }
  669. *
  670. * sleep_idle()
  671. * {
  672. * core_idle_state &= ~thread_in_core
  673. * }
  674. *
  675. * sleep_wake()
  676. * {
  677. * bool first_in_core, first_in_subcore;
  678. *
  679. * first_in_core = (core_idle_state & IDLE_THREAD_BITS) == 0;
  680. * first_in_subcore = (core_idle_state & SUBCORE_SIBLING_MASK) == 0;
  681. *
  682. * core_idle_state |= thread_in_core;
  683. * }
  684. *
  685. * winkle_idle()
  686. * {
  687. * core_idle_state &= ~thread_in_core;
  688. * core_idle_state += 1 << WINKLE_COUNT_SHIFT;
  689. * }
  690. *
  691. * winkle_wake()
  692. * {
  693. * bool first_in_core, first_in_subcore, winkle_state_lost;
  694. *
  695. * first_in_core = (core_idle_state & IDLE_THREAD_BITS) == 0;
  696. * first_in_subcore = (core_idle_state & SUBCORE_SIBLING_MASK) == 0;
  697. *
  698. * core_idle_state |= thread_in_core;
  699. *
  700. * if ((core_idle_state & WINKLE_MASK) == (8 << WINKLE_COUNT_SIHFT))
  701. * core_idle_state |= THREAD_WINKLE_BITS;
  702. * core_idle_state -= 1 << WINKLE_COUNT_SHIFT;
  703. *
  704. * winkle_state_lost = core_idle_state &
  705. * (thread_in_core << WINKLE_THREAD_SHIFT);
  706. * core_idle_state &= ~(thread_in_core << WINKLE_THREAD_SHIFT);
  707. * }
  708. *
  709. */
  710. cmpwi r18,PNV_THREAD_WINKLE
  711. bne 2f
  712. andis. r9,r15,PNV_CORE_IDLE_WINKLE_COUNT_ALL_BIT@h
  713. subis r15,r15,PNV_CORE_IDLE_WINKLE_COUNT@h
  714. beq 2f
  715. ori r15,r15,PNV_CORE_IDLE_THREAD_WINKLE_BITS /* all were winkle */
  716. 2:
  717. /* Shift thread bit to winkle mask, then test if this thread is set,
  718. * and remove it from the winkle bits */
  719. slwi r8,r7,8
  720. and r8,r8,r15
  721. andc r15,r15,r8
  722. cmpwi cr4,r8,1 /* cr4 will be gt if our bit is set, lt if not */
  723. lbz r4,PACA_SUBCORE_SIBLING_MASK(r13)
  724. and r4,r4,r15
  725. cmpwi r4,0 /* Check if first in subcore */
  726. or r15,r15,r7 /* Set thread bit */
  727. beq first_thread_in_subcore
  728. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  729. or r15,r15,r7 /* Set thread bit */
  730. beq cr2,first_thread_in_core
  731. /* Not first thread in core or subcore to wake up */
  732. b clear_lock
  733. first_thread_in_subcore:
  734. /*
  735. * If waking up from sleep, subcore state is not lost. Hence
  736. * skip subcore state restore
  737. */
  738. blt cr4,subcore_state_restored
  739. /* Restore per-subcore state */
  740. ld r4,_SDR1(r1)
  741. mtspr SPRN_SDR1,r4
  742. ld r4,_RPR(r1)
  743. mtspr SPRN_RPR,r4
  744. ld r4,_AMOR(r1)
  745. mtspr SPRN_AMOR,r4
  746. subcore_state_restored:
  747. /*
  748. * Check if the thread is also the first thread in the core. If not,
  749. * skip to clear_lock.
  750. */
  751. bne cr2,clear_lock
  752. first_thread_in_core:
  753. /*
  754. * First thread in the core waking up from any state which can cause
  755. * partial or complete hypervisor state loss. It needs to
  756. * call the fastsleep workaround code if the platform requires it.
  757. * Call it unconditionally here. The below branch instruction will
  758. * be patched out if the platform does not have fastsleep or does not
  759. * require the workaround. Patching will be performed during the
  760. * discovery of idle-states.
  761. */
  762. .global pnv_fastsleep_workaround_at_exit
  763. pnv_fastsleep_workaround_at_exit:
  764. b fastsleep_workaround_at_exit
  765. timebase_resync:
  766. /*
  767. * Use cr3 which indicates that we are waking up with atleast partial
  768. * hypervisor state loss to determine if TIMEBASE RESYNC is needed.
  769. */
  770. ble cr3,.Ltb_resynced
  771. /* Time base re-sync */
  772. bl opal_resync_timebase;
  773. /*
  774. * If waking up from sleep (POWER8), per core state
  775. * is not lost, skip to clear_lock.
  776. */
  777. .Ltb_resynced:
  778. blt cr4,clear_lock
  779. /*
  780. * First thread in the core to wake up and its waking up with
  781. * complete hypervisor state loss. Restore per core hypervisor
  782. * state.
  783. */
  784. BEGIN_FTR_SECTION
  785. ld r4,_PTCR(r1)
  786. mtspr SPRN_PTCR,r4
  787. ld r4,_RPR(r1)
  788. mtspr SPRN_RPR,r4
  789. ld r4,_AMOR(r1)
  790. mtspr SPRN_AMOR,r4
  791. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  792. ld r4,_TSCR(r1)
  793. mtspr SPRN_TSCR,r4
  794. ld r4,_WORC(r1)
  795. mtspr SPRN_WORC,r4
  796. clear_lock:
  797. xoris r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
  798. lwsync
  799. stw r15,0(r14)
  800. common_exit:
  801. /*
  802. * Common to all threads.
  803. *
  804. * If waking up from sleep, hypervisor state is not lost. Hence
  805. * skip hypervisor state restore.
  806. */
  807. blt cr4,hypervisor_state_restored
  808. /* Waking up from winkle */
  809. BEGIN_MMU_FTR_SECTION
  810. b no_segments
  811. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
  812. /* Restore SLB from PACA */
  813. ld r8,PACA_SLBSHADOWPTR(r13)
  814. .rept SLB_NUM_BOLTED
  815. li r3, SLBSHADOW_SAVEAREA
  816. LDX_BE r5, r8, r3
  817. addi r3, r3, 8
  818. LDX_BE r6, r8, r3
  819. andis. r7,r5,SLB_ESID_V@h
  820. beq 1f
  821. slbmte r6,r5
  822. 1: addi r8,r8,16
  823. .endr
  824. no_segments:
  825. /* Restore per thread state */
  826. ld r4,_SPURR(r1)
  827. mtspr SPRN_SPURR,r4
  828. ld r4,_PURR(r1)
  829. mtspr SPRN_PURR,r4
  830. ld r4,_DSCR(r1)
  831. mtspr SPRN_DSCR,r4
  832. ld r4,_WORT(r1)
  833. mtspr SPRN_WORT,r4
  834. /* Call cur_cpu_spec->cpu_restore() */
  835. LOAD_REG_ADDR(r4, cur_cpu_spec)
  836. ld r4,0(r4)
  837. ld r12,CPU_SPEC_RESTORE(r4)
  838. #ifdef PPC64_ELF_ABI_v1
  839. ld r12,0(r12)
  840. #endif
  841. mtctr r12
  842. bctrl
  843. /*
  844. * On POWER9, we can come here on wakeup from a cpuidle stop state.
  845. * Hence restore the additional SPRs to the saved value.
  846. *
  847. * On POWER8, we come here only on winkle. Since winkle is used
  848. * only in the case of CPU-Hotplug, we don't need to restore
  849. * the additional SPRs.
  850. */
  851. BEGIN_FTR_SECTION
  852. bl power9_restore_additional_sprs
  853. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  854. hypervisor_state_restored:
  855. mr r12,r19
  856. mtlr r17
  857. blr /* return to pnv_powersave_wakeup */
  858. fastsleep_workaround_at_exit:
  859. li r3,1
  860. li r4,0
  861. bl opal_config_cpu_idle_state
  862. b timebase_resync
  863. /*
  864. * R3 here contains the value that will be returned to the caller
  865. * of power7_nap.
  866. * R12 contains SRR1 for CHECK_HMI_INTERRUPT.
  867. */
  868. .global pnv_wakeup_loss
  869. pnv_wakeup_loss:
  870. ld r1,PACAR1(r13)
  871. BEGIN_FTR_SECTION
  872. CHECK_HMI_INTERRUPT
  873. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  874. REST_NVGPRS(r1)
  875. REST_GPR(2, r1)
  876. ld r4,PACAKMSR(r13)
  877. ld r5,_LINK(r1)
  878. ld r6,_CCR(r1)
  879. addi r1,r1,INT_FRAME_SIZE
  880. mtlr r5
  881. mtcr r6
  882. mtmsrd r4
  883. blr
  884. /*
  885. * R3 here contains the value that will be returned to the caller
  886. * of power7_nap.
  887. * R12 contains SRR1 for CHECK_HMI_INTERRUPT.
  888. */
  889. pnv_wakeup_noloss:
  890. lbz r0,PACA_NAPSTATELOST(r13)
  891. cmpwi r0,0
  892. bne pnv_wakeup_loss
  893. ld r1,PACAR1(r13)
  894. BEGIN_FTR_SECTION
  895. CHECK_HMI_INTERRUPT
  896. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  897. ld r4,PACAKMSR(r13)
  898. ld r5,_NIP(r1)
  899. ld r6,_CCR(r1)
  900. addi r1,r1,INT_FRAME_SIZE
  901. mtlr r5
  902. mtcr r6
  903. mtmsrd r4
  904. blr