sleep.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <linux/errno.h>
  2. #include <linux/linkage.h>
  3. #include <asm/asm-offsets.h>
  4. #include <asm/assembler.h>
  5. .text
  6. /*
  7. * Implementation of MPIDR_EL1 hash algorithm through shifting
  8. * and OR'ing.
  9. *
  10. * @dst: register containing hash result
  11. * @rs0: register containing affinity level 0 bit shift
  12. * @rs1: register containing affinity level 1 bit shift
  13. * @rs2: register containing affinity level 2 bit shift
  14. * @rs3: register containing affinity level 3 bit shift
  15. * @mpidr: register containing MPIDR_EL1 value
  16. * @mask: register containing MPIDR mask
  17. *
  18. * Pseudo C-code:
  19. *
  20. *u32 dst;
  21. *
  22. *compute_mpidr_hash(u32 rs0, u32 rs1, u32 rs2, u32 rs3, u64 mpidr, u64 mask) {
  23. * u32 aff0, aff1, aff2, aff3;
  24. * u64 mpidr_masked = mpidr & mask;
  25. * aff0 = mpidr_masked & 0xff;
  26. * aff1 = mpidr_masked & 0xff00;
  27. * aff2 = mpidr_masked & 0xff0000;
  28. * aff2 = mpidr_masked & 0xff00000000;
  29. * dst = (aff0 >> rs0 | aff1 >> rs1 | aff2 >> rs2 | aff3 >> rs3);
  30. *}
  31. * Input registers: rs0, rs1, rs2, rs3, mpidr, mask
  32. * Output register: dst
  33. * Note: input and output registers must be disjoint register sets
  34. (eg: a macro instance with mpidr = x1 and dst = x1 is invalid)
  35. */
  36. .macro compute_mpidr_hash dst, rs0, rs1, rs2, rs3, mpidr, mask
  37. and \mpidr, \mpidr, \mask // mask out MPIDR bits
  38. and \dst, \mpidr, #0xff // mask=aff0
  39. lsr \dst ,\dst, \rs0 // dst=aff0>>rs0
  40. and \mask, \mpidr, #0xff00 // mask = aff1
  41. lsr \mask ,\mask, \rs1
  42. orr \dst, \dst, \mask // dst|=(aff1>>rs1)
  43. and \mask, \mpidr, #0xff0000 // mask = aff2
  44. lsr \mask ,\mask, \rs2
  45. orr \dst, \dst, \mask // dst|=(aff2>>rs2)
  46. and \mask, \mpidr, #0xff00000000 // mask = aff3
  47. lsr \mask ,\mask, \rs3
  48. orr \dst, \dst, \mask // dst|=(aff3>>rs3)
  49. .endm
  50. /*
  51. * Save CPU state for a suspend and execute the suspend finisher.
  52. * On success it will return 0 through cpu_resume - ie through a CPU
  53. * soft/hard reboot from the reset vector.
  54. * On failure it returns the suspend finisher return value or force
  55. * -EOPNOTSUPP if the finisher erroneously returns 0 (the suspend finisher
  56. * is not allowed to return, if it does this must be considered failure).
  57. * It saves callee registers, and allocates space on the kernel stack
  58. * to save the CPU specific registers + some other data for resume.
  59. *
  60. * x0 = suspend finisher argument
  61. * x1 = suspend finisher function pointer
  62. */
  63. ENTRY(__cpu_suspend_enter)
  64. stp x29, lr, [sp, #-96]!
  65. stp x19, x20, [sp,#16]
  66. stp x21, x22, [sp,#32]
  67. stp x23, x24, [sp,#48]
  68. stp x25, x26, [sp,#64]
  69. stp x27, x28, [sp,#80]
  70. /*
  71. * Stash suspend finisher and its argument in x20 and x19
  72. */
  73. mov x19, x0
  74. mov x20, x1
  75. mov x2, sp
  76. sub sp, sp, #CPU_SUSPEND_SZ // allocate cpu_suspend_ctx
  77. mov x0, sp
  78. /*
  79. * x0 now points to struct cpu_suspend_ctx allocated on the stack
  80. */
  81. str x2, [x0, #CPU_CTX_SP]
  82. ldr x1, =sleep_save_sp
  83. ldr x1, [x1, #SLEEP_SAVE_SP_VIRT]
  84. mrs x7, mpidr_el1
  85. ldr x9, =mpidr_hash
  86. ldr x10, [x9, #MPIDR_HASH_MASK]
  87. /*
  88. * Following code relies on the struct mpidr_hash
  89. * members size.
  90. */
  91. ldp w3, w4, [x9, #MPIDR_HASH_SHIFTS]
  92. ldp w5, w6, [x9, #(MPIDR_HASH_SHIFTS + 8)]
  93. compute_mpidr_hash x8, x3, x4, x5, x6, x7, x10
  94. add x1, x1, x8, lsl #3
  95. bl __cpu_suspend_save
  96. /*
  97. * Grab suspend finisher in x20 and its argument in x19
  98. */
  99. mov x0, x19
  100. mov x1, x20
  101. /*
  102. * We are ready for power down, fire off the suspend finisher
  103. * in x1, with argument in x0
  104. */
  105. blr x1
  106. /*
  107. * Never gets here, unless suspend finisher fails.
  108. * Successful cpu_suspend should return from cpu_resume, returning
  109. * through this code path is considered an error
  110. * If the return value is set to 0 force x0 = -EOPNOTSUPP
  111. * to make sure a proper error condition is propagated
  112. */
  113. cmp x0, #0
  114. mov x3, #-EOPNOTSUPP
  115. csel x0, x3, x0, eq
  116. add sp, sp, #CPU_SUSPEND_SZ // rewind stack pointer
  117. ldp x19, x20, [sp, #16]
  118. ldp x21, x22, [sp, #32]
  119. ldp x23, x24, [sp, #48]
  120. ldp x25, x26, [sp, #64]
  121. ldp x27, x28, [sp, #80]
  122. ldp x29, lr, [sp], #96
  123. ret
  124. ENDPROC(__cpu_suspend_enter)
  125. .ltorg
  126. /*
  127. * x0 must contain the sctlr value retrieved from restored context
  128. */
  129. .pushsection ".idmap.text", "ax"
  130. ENTRY(cpu_resume_mmu)
  131. ldr x3, =cpu_resume_after_mmu
  132. msr sctlr_el1, x0 // restore sctlr_el1
  133. isb
  134. /*
  135. * Invalidate the local I-cache so that any instructions fetched
  136. * speculatively from the PoC are discarded, since they may have
  137. * been dynamically patched at the PoU.
  138. */
  139. ic iallu
  140. dsb nsh
  141. isb
  142. br x3 // global jump to virtual address
  143. ENDPROC(cpu_resume_mmu)
  144. .popsection
  145. cpu_resume_after_mmu:
  146. mov x0, #0 // return zero on success
  147. ldp x19, x20, [sp, #16]
  148. ldp x21, x22, [sp, #32]
  149. ldp x23, x24, [sp, #48]
  150. ldp x25, x26, [sp, #64]
  151. ldp x27, x28, [sp, #80]
  152. ldp x29, lr, [sp], #96
  153. ret
  154. ENDPROC(cpu_resume_after_mmu)
  155. ENTRY(cpu_resume)
  156. bl el2_setup // if in EL2 drop to EL1 cleanly
  157. mrs x1, mpidr_el1
  158. adrp x8, mpidr_hash
  159. add x8, x8, #:lo12:mpidr_hash // x8 = struct mpidr_hash phys address
  160. /* retrieve mpidr_hash members to compute the hash */
  161. ldr x2, [x8, #MPIDR_HASH_MASK]
  162. ldp w3, w4, [x8, #MPIDR_HASH_SHIFTS]
  163. ldp w5, w6, [x8, #(MPIDR_HASH_SHIFTS + 8)]
  164. compute_mpidr_hash x7, x3, x4, x5, x6, x1, x2
  165. /* x7 contains hash index, let's use it to grab context pointer */
  166. ldr_l x0, sleep_save_sp + SLEEP_SAVE_SP_PHYS
  167. ldr x0, [x0, x7, lsl #3]
  168. /* load sp from context */
  169. ldr x2, [x0, #CPU_CTX_SP]
  170. /* load physical address of identity map page table in x1 */
  171. adrp x1, idmap_pg_dir
  172. mov sp, x2
  173. /*
  174. * cpu_do_resume expects x0 to contain context physical address
  175. * pointer and x1 to contain physical address of 1:1 page tables
  176. */
  177. bl cpu_do_resume // PC relative jump, MMU off
  178. b cpu_resume_mmu // Resume MMU, never returns
  179. ENDPROC(cpu_resume)