sleep.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/linkage.h>
  3. #include <linux/threads.h>
  4. #include <asm/asm-offsets.h>
  5. #include <asm/assembler.h>
  6. #include <asm/glue-cache.h>
  7. #include <asm/glue-proc.h>
  8. .text
  9. /*
  10. * Implementation of MPIDR hash algorithm through shifting
  11. * and OR'ing.
  12. *
  13. * @dst: register containing hash result
  14. * @rs0: register containing affinity level 0 bit shift
  15. * @rs1: register containing affinity level 1 bit shift
  16. * @rs2: register containing affinity level 2 bit shift
  17. * @mpidr: register containing MPIDR value
  18. * @mask: register containing MPIDR mask
  19. *
  20. * Pseudo C-code:
  21. *
  22. *u32 dst;
  23. *
  24. *compute_mpidr_hash(u32 rs0, u32 rs1, u32 rs2, u32 mpidr, u32 mask) {
  25. * u32 aff0, aff1, aff2;
  26. * u32 mpidr_masked = mpidr & mask;
  27. * aff0 = mpidr_masked & 0xff;
  28. * aff1 = mpidr_masked & 0xff00;
  29. * aff2 = mpidr_masked & 0xff0000;
  30. * dst = (aff0 >> rs0 | aff1 >> rs1 | aff2 >> rs2);
  31. *}
  32. * Input registers: rs0, rs1, rs2, mpidr, mask
  33. * Output register: dst
  34. * Note: input and output registers must be disjoint register sets
  35. (eg: a macro instance with mpidr = r1 and dst = r1 is invalid)
  36. */
  37. .macro compute_mpidr_hash dst, rs0, rs1, rs2, mpidr, mask
  38. and \mpidr, \mpidr, \mask @ mask out MPIDR bits
  39. and \dst, \mpidr, #0xff @ mask=aff0
  40. ARM( mov \dst, \dst, lsr \rs0 ) @ dst=aff0>>rs0
  41. THUMB( lsr \dst, \dst, \rs0 )
  42. and \mask, \mpidr, #0xff00 @ mask = aff1
  43. ARM( orr \dst, \dst, \mask, lsr \rs1 ) @ dst|=(aff1>>rs1)
  44. THUMB( lsr \mask, \mask, \rs1 )
  45. THUMB( orr \dst, \dst, \mask )
  46. and \mask, \mpidr, #0xff0000 @ mask = aff2
  47. ARM( orr \dst, \dst, \mask, lsr \rs2 ) @ dst|=(aff2>>rs2)
  48. THUMB( lsr \mask, \mask, \rs2 )
  49. THUMB( orr \dst, \dst, \mask )
  50. .endm
  51. /*
  52. * Save CPU state for a suspend. This saves the CPU general purpose
  53. * registers, and allocates space on the kernel stack to save the CPU
  54. * specific registers and some other data for resume.
  55. * r0 = suspend function arg0
  56. * r1 = suspend function
  57. * r2 = MPIDR value the resuming CPU will use
  58. */
  59. ENTRY(__cpu_suspend)
  60. stmfd sp!, {r4 - r11, lr}
  61. #ifdef MULTI_CPU
  62. ldr r10, =processor
  63. ldr r4, [r10, #CPU_SLEEP_SIZE] @ size of CPU sleep state
  64. #else
  65. ldr r4, =cpu_suspend_size
  66. #endif
  67. mov r5, sp @ current virtual SP
  68. add r4, r4, #12 @ Space for pgd, virt sp, phys resume fn
  69. sub sp, sp, r4 @ allocate CPU state on stack
  70. ldr r3, =sleep_save_sp
  71. stmfd sp!, {r0, r1} @ save suspend func arg and pointer
  72. ldr r3, [r3, #SLEEP_SAVE_SP_VIRT]
  73. ALT_SMP(ldr r0, =mpidr_hash)
  74. ALT_UP_B(1f)
  75. /* This ldmia relies on the memory layout of the mpidr_hash struct */
  76. ldmia r0, {r1, r6-r8} @ r1 = mpidr mask (r6,r7,r8) = l[0,1,2] shifts
  77. compute_mpidr_hash r0, r6, r7, r8, r2, r1
  78. add r3, r3, r0, lsl #2
  79. 1: mov r2, r5 @ virtual SP
  80. mov r1, r4 @ size of save block
  81. add r0, sp, #8 @ pointer to save block
  82. bl __cpu_suspend_save
  83. badr lr, cpu_suspend_abort
  84. ldmfd sp!, {r0, pc} @ call suspend fn
  85. ENDPROC(__cpu_suspend)
  86. .ltorg
  87. cpu_suspend_abort:
  88. ldmia sp!, {r1 - r3} @ pop phys pgd, virt SP, phys resume fn
  89. teq r0, #0
  90. moveq r0, #1 @ force non-zero value
  91. mov sp, r2
  92. ldmfd sp!, {r4 - r11, pc}
  93. ENDPROC(cpu_suspend_abort)
  94. /*
  95. * r0 = control register value
  96. */
  97. .align 5
  98. .pushsection .idmap.text,"ax"
  99. ENTRY(cpu_resume_mmu)
  100. ldr r3, =cpu_resume_after_mmu
  101. instr_sync
  102. mcr p15, 0, r0, c1, c0, 0 @ turn on MMU, I-cache, etc
  103. mrc p15, 0, r0, c0, c0, 0 @ read id reg
  104. instr_sync
  105. mov r0, r0
  106. mov r0, r0
  107. ret r3 @ jump to virtual address
  108. ENDPROC(cpu_resume_mmu)
  109. .popsection
  110. cpu_resume_after_mmu:
  111. bl cpu_init @ restore the und/abt/irq banked regs
  112. mov r0, #0 @ return zero on success
  113. ldmfd sp!, {r4 - r11, pc}
  114. ENDPROC(cpu_resume_after_mmu)
  115. .text
  116. .align
  117. #ifdef CONFIG_MCPM
  118. .arm
  119. THUMB( .thumb )
  120. ENTRY(cpu_resume_no_hyp)
  121. ARM_BE8(setend be) @ ensure we are in BE mode
  122. b no_hyp
  123. #endif
  124. #ifdef CONFIG_MMU
  125. .arm
  126. ENTRY(cpu_resume_arm)
  127. THUMB( badr r9, 1f ) @ Kernel is entered in ARM.
  128. THUMB( bx r9 ) @ If this is a Thumb-2 kernel,
  129. THUMB( .thumb ) @ switch to Thumb now.
  130. THUMB(1: )
  131. #endif
  132. ENTRY(cpu_resume)
  133. ARM_BE8(setend be) @ ensure we are in BE mode
  134. #ifdef CONFIG_ARM_VIRT_EXT
  135. bl __hyp_stub_install_secondary
  136. #endif
  137. safe_svcmode_maskall r1
  138. no_hyp:
  139. mov r1, #0
  140. ALT_SMP(mrc p15, 0, r0, c0, c0, 5)
  141. ALT_UP_B(1f)
  142. adr r2, mpidr_hash_ptr
  143. ldr r3, [r2]
  144. add r2, r2, r3 @ r2 = struct mpidr_hash phys address
  145. /*
  146. * This ldmia relies on the memory layout of the mpidr_hash
  147. * struct mpidr_hash.
  148. */
  149. ldmia r2, { r3-r6 } @ r3 = mpidr mask (r4,r5,r6) = l[0,1,2] shifts
  150. compute_mpidr_hash r1, r4, r5, r6, r0, r3
  151. 1:
  152. adr r0, _sleep_save_sp
  153. ldr r2, [r0]
  154. add r0, r0, r2
  155. ldr r0, [r0, #SLEEP_SAVE_SP_PHYS]
  156. ldr r0, [r0, r1, lsl #2]
  157. @ load phys pgd, stack, resume fn
  158. ARM( ldmia r0!, {r1, sp, pc} )
  159. THUMB( ldmia r0!, {r1, r2, r3} )
  160. THUMB( mov sp, r2 )
  161. THUMB( bx r3 )
  162. ENDPROC(cpu_resume)
  163. #ifdef CONFIG_MMU
  164. ENDPROC(cpu_resume_arm)
  165. #endif
  166. #ifdef CONFIG_MCPM
  167. ENDPROC(cpu_resume_no_hyp)
  168. #endif
  169. .align 2
  170. _sleep_save_sp:
  171. .long sleep_save_sp - .
  172. mpidr_hash_ptr:
  173. .long mpidr_hash - . @ mpidr_hash struct offset
  174. .data
  175. .align 2
  176. .type sleep_save_sp, #object
  177. ENTRY(sleep_save_sp)
  178. .space SLEEP_SAVE_SP_SZ @ struct sleep_save_sp