idle_book3s.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright 2018, IBM Corporation.
  4. *
  5. * This file contains general idle entry/exit functions to save
  6. * and restore stack and NVGPRs which allows C code to call idle
  7. * states that lose GPRs, and it will return transparently with
  8. * SRR1 wakeup reason return value.
  9. *
  10. * The platform / CPU caller must ensure SPRs and any other non-GPR
  11. * state is saved and restored correctly, handle KVM, interrupts, etc.
  12. */
  13. #include <asm/ppc_asm.h>
  14. #include <asm/asm-offsets.h>
  15. #include <asm/ppc-opcode.h>
  16. #include <asm/cpuidle.h>
  17. #include <asm/thread_info.h> /* TLF_NAPPING */
  18. #ifdef CONFIG_PPC_P7_NAP
  19. /*
  20. * Desired PSSCR in r3
  21. *
  22. * No state will be lost regardless of wakeup mechanism (interrupt or NIA).
  23. *
  24. * An EC=0 type wakeup will return with a value of 0. SRESET wakeup (which can
  25. * happen with xscom SRESET and possibly MCE) may clobber volatiles except LR,
  26. * and must blr, to return to caller with r3 set according to caller's expected
  27. * return code (for Book3S/64 that is SRR1).
  28. */
  29. _GLOBAL(isa300_idle_stop_noloss)
  30. mtspr SPRN_PSSCR,r3
  31. PPC_STOP
  32. li r3,0
  33. blr
  34. /*
  35. * Desired PSSCR in r3
  36. *
  37. * GPRs may be lost, so they are saved here. Wakeup is by interrupt only.
  38. * The SRESET wakeup returns to this function's caller by calling
  39. * idle_return_gpr_loss with r3 set to desired return value.
  40. *
  41. * A wakeup without GPR loss may alteratively be handled as in
  42. * isa300_idle_stop_noloss and blr directly, as an optimisation.
  43. *
  44. * The caller is responsible for saving/restoring SPRs, MSR, timebase,
  45. * etc.
  46. */
  47. _GLOBAL(isa300_idle_stop_mayloss)
  48. mtspr SPRN_PSSCR,r3
  49. std r1,PACAR1(r13)
  50. mflr r4
  51. mfcr r5
  52. /*
  53. * Use the stack red zone rather than a new frame for saving regs since
  54. * in the case of no GPR loss the wakeup code branches directly back to
  55. * the caller without deallocating the stack frame first.
  56. */
  57. std r2,-8*1(r1)
  58. std r14,-8*2(r1)
  59. std r15,-8*3(r1)
  60. std r16,-8*4(r1)
  61. std r17,-8*5(r1)
  62. std r18,-8*6(r1)
  63. std r19,-8*7(r1)
  64. std r20,-8*8(r1)
  65. std r21,-8*9(r1)
  66. std r22,-8*10(r1)
  67. std r23,-8*11(r1)
  68. std r24,-8*12(r1)
  69. std r25,-8*13(r1)
  70. std r26,-8*14(r1)
  71. std r27,-8*15(r1)
  72. std r28,-8*16(r1)
  73. std r29,-8*17(r1)
  74. std r30,-8*18(r1)
  75. std r31,-8*19(r1)
  76. std r4,-8*20(r1)
  77. std r5,-8*21(r1)
  78. /* 168 bytes */
  79. PPC_STOP
  80. b . /* catch bugs */
  81. /*
  82. * Desired return value in r3
  83. *
  84. * The idle wakeup SRESET interrupt can call this after calling
  85. * to return to the idle sleep function caller with r3 as the return code.
  86. *
  87. * This must not be used if idle was entered via a _noloss function (use
  88. * a simple blr instead).
  89. */
  90. _GLOBAL(idle_return_gpr_loss)
  91. ld r1,PACAR1(r13)
  92. ld r4,-8*20(r1)
  93. ld r5,-8*21(r1)
  94. mtlr r4
  95. mtcr r5
  96. /*
  97. * KVM nap requires r2 to be saved, rather than just restoring it
  98. * from PACATOC. This could be avoided for that less common case
  99. * if KVM saved its r2.
  100. */
  101. ld r2,-8*1(r1)
  102. ld r14,-8*2(r1)
  103. ld r15,-8*3(r1)
  104. ld r16,-8*4(r1)
  105. ld r17,-8*5(r1)
  106. ld r18,-8*6(r1)
  107. ld r19,-8*7(r1)
  108. ld r20,-8*8(r1)
  109. ld r21,-8*9(r1)
  110. ld r22,-8*10(r1)
  111. ld r23,-8*11(r1)
  112. ld r24,-8*12(r1)
  113. ld r25,-8*13(r1)
  114. ld r26,-8*14(r1)
  115. ld r27,-8*15(r1)
  116. ld r28,-8*16(r1)
  117. ld r29,-8*17(r1)
  118. ld r30,-8*18(r1)
  119. ld r31,-8*19(r1)
  120. blr
  121. /*
  122. * This is the sequence required to execute idle instructions, as
  123. * specified in ISA v2.07 (and earlier). MSR[IR] and MSR[DR] must be 0.
  124. * We have to store a GPR somewhere, ptesync, then reload it, and create
  125. * a false dependency on the result of the load. It doesn't matter which
  126. * GPR we store, or where we store it. We have already stored r2 to the
  127. * stack at -8(r1) in isa206_idle_insn_mayloss, so use that.
  128. */
  129. #define IDLE_STATE_ENTER_SEQ_NORET(IDLE_INST) \
  130. /* Magic NAP/SLEEP/WINKLE mode enter sequence */ \
  131. std r2,-8(r1); \
  132. ptesync; \
  133. ld r2,-8(r1); \
  134. 236: cmpd cr0,r2,r2; \
  135. bne 236b; \
  136. IDLE_INST; \
  137. b . /* catch bugs */
  138. /*
  139. * Desired instruction type in r3
  140. *
  141. * GPRs may be lost, so they are saved here. Wakeup is by interrupt only.
  142. * The SRESET wakeup returns to this function's caller by calling
  143. * idle_return_gpr_loss with r3 set to desired return value.
  144. *
  145. * A wakeup without GPR loss may alteratively be handled as in
  146. * isa300_idle_stop_noloss and blr directly, as an optimisation.
  147. *
  148. * The caller is responsible for saving/restoring SPRs, MSR, timebase,
  149. * etc.
  150. *
  151. * This must be called in real-mode (MSR_IDLE).
  152. */
  153. _GLOBAL(isa206_idle_insn_mayloss)
  154. std r1,PACAR1(r13)
  155. mflr r4
  156. mfcr r5
  157. /*
  158. * Use the stack red zone rather than a new frame for saving regs since
  159. * in the case of no GPR loss the wakeup code branches directly back to
  160. * the caller without deallocating the stack frame first.
  161. */
  162. std r2,-8*1(r1)
  163. std r14,-8*2(r1)
  164. std r15,-8*3(r1)
  165. std r16,-8*4(r1)
  166. std r17,-8*5(r1)
  167. std r18,-8*6(r1)
  168. std r19,-8*7(r1)
  169. std r20,-8*8(r1)
  170. std r21,-8*9(r1)
  171. std r22,-8*10(r1)
  172. std r23,-8*11(r1)
  173. std r24,-8*12(r1)
  174. std r25,-8*13(r1)
  175. std r26,-8*14(r1)
  176. std r27,-8*15(r1)
  177. std r28,-8*16(r1)
  178. std r29,-8*17(r1)
  179. std r30,-8*18(r1)
  180. std r31,-8*19(r1)
  181. std r4,-8*20(r1)
  182. std r5,-8*21(r1)
  183. cmpwi r3,PNV_THREAD_NAP
  184. bne 1f
  185. IDLE_STATE_ENTER_SEQ_NORET(PPC_NAP)
  186. 1: cmpwi r3,PNV_THREAD_SLEEP
  187. bne 2f
  188. IDLE_STATE_ENTER_SEQ_NORET(PPC_SLEEP)
  189. 2: IDLE_STATE_ENTER_SEQ_NORET(PPC_WINKLE)
  190. #endif
  191. #ifdef CONFIG_PPC_970_NAP
  192. _GLOBAL(power4_idle_nap)
  193. LOAD_REG_IMMEDIATE(r7, MSR_KERNEL|MSR_EE|MSR_POW)
  194. ld r9,PACA_THREAD_INFO(r13)
  195. ld r8,TI_LOCAL_FLAGS(r9)
  196. ori r8,r8,_TLF_NAPPING
  197. std r8,TI_LOCAL_FLAGS(r9)
  198. /*
  199. * NAPPING bit is set, from this point onward power4_fixup_nap
  200. * will cause exceptions to return to power4_idle_nap_return.
  201. */
  202. 1: sync
  203. isync
  204. mtmsrd r7
  205. isync
  206. b 1b
  207. #endif