idle_book3s.S 22 KB

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