sleep.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #ifdef CONFIG_SMP
  85. mrs x7, mpidr_el1
  86. ldr x9, =mpidr_hash
  87. ldr x10, [x9, #MPIDR_HASH_MASK]
  88. /*
  89. * Following code relies on the struct mpidr_hash
  90. * members size.
  91. */
  92. ldp w3, w4, [x9, #MPIDR_HASH_SHIFTS]
  93. ldp w5, w6, [x9, #(MPIDR_HASH_SHIFTS + 8)]
  94. compute_mpidr_hash x8, x3, x4, x5, x6, x7, x10
  95. add x1, x1, x8, lsl #3
  96. #endif
  97. bl __cpu_suspend_save
  98. /*
  99. * Grab suspend finisher in x20 and its argument in x19
  100. */
  101. mov x0, x19
  102. mov x1, x20
  103. /*
  104. * We are ready for power down, fire off the suspend finisher
  105. * in x1, with argument in x0
  106. */
  107. blr x1
  108. /*
  109. * Never gets here, unless suspend finisher fails.
  110. * Successful cpu_suspend should return from cpu_resume, returning
  111. * through this code path is considered an error
  112. * If the return value is set to 0 force x0 = -EOPNOTSUPP
  113. * to make sure a proper error condition is propagated
  114. */
  115. cmp x0, #0
  116. mov x3, #-EOPNOTSUPP
  117. csel x0, x3, x0, eq
  118. add sp, sp, #CPU_SUSPEND_SZ // rewind stack pointer
  119. ldp x19, x20, [sp, #16]
  120. ldp x21, x22, [sp, #32]
  121. ldp x23, x24, [sp, #48]
  122. ldp x25, x26, [sp, #64]
  123. ldp x27, x28, [sp, #80]
  124. ldp x29, lr, [sp], #96
  125. ret
  126. ENDPROC(__cpu_suspend_enter)
  127. .ltorg
  128. /*
  129. * x0 must contain the sctlr value retrieved from restored context
  130. */
  131. .pushsection ".idmap.text", "ax"
  132. ENTRY(cpu_resume_mmu)
  133. ldr x3, =cpu_resume_after_mmu
  134. msr sctlr_el1, x0 // restore sctlr_el1
  135. isb
  136. br x3 // global jump to virtual address
  137. ENDPROC(cpu_resume_mmu)
  138. .popsection
  139. cpu_resume_after_mmu:
  140. mov x0, #0 // return zero on success
  141. ldp x19, x20, [sp, #16]
  142. ldp x21, x22, [sp, #32]
  143. ldp x23, x24, [sp, #48]
  144. ldp x25, x26, [sp, #64]
  145. ldp x27, x28, [sp, #80]
  146. ldp x29, lr, [sp], #96
  147. ret
  148. ENDPROC(cpu_resume_after_mmu)
  149. ENTRY(cpu_resume)
  150. bl el2_setup // if in EL2 drop to EL1 cleanly
  151. #ifdef CONFIG_SMP
  152. mrs x1, mpidr_el1
  153. adrp x8, mpidr_hash
  154. add x8, x8, #:lo12:mpidr_hash // x8 = struct mpidr_hash phys address
  155. /* retrieve mpidr_hash members to compute the hash */
  156. ldr x2, [x8, #MPIDR_HASH_MASK]
  157. ldp w3, w4, [x8, #MPIDR_HASH_SHIFTS]
  158. ldp w5, w6, [x8, #(MPIDR_HASH_SHIFTS + 8)]
  159. compute_mpidr_hash x7, x3, x4, x5, x6, x1, x2
  160. /* x7 contains hash index, let's use it to grab context pointer */
  161. #else
  162. mov x7, xzr
  163. #endif
  164. ldr_l x0, sleep_save_sp + SLEEP_SAVE_SP_PHYS
  165. ldr x0, [x0, x7, lsl #3]
  166. /* load sp from context */
  167. ldr x2, [x0, #CPU_CTX_SP]
  168. /* load physical address of identity map page table in x1 */
  169. adrp x1, idmap_pg_dir
  170. mov sp, x2
  171. /*
  172. * cpu_do_resume expects x0 to contain context physical address
  173. * pointer and x1 to contain physical address of 1:1 page tables
  174. */
  175. bl cpu_do_resume // PC relative jump, MMU off
  176. b cpu_resume_mmu // Resume MMU, never returns
  177. ENDPROC(cpu_resume)