sleep.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifdef CONFIG_VMAP_STACK
  69. @ Run the suspend code from the overflow stack so we don't have to rely
  70. @ on vmalloc-to-phys conversions anywhere in the arch suspend code.
  71. @ The original SP value captured in R5 will be restored on the way out.
  72. ldr_this_cpu sp, overflow_stack_ptr, r6, r7
  73. #endif
  74. add r4, r4, #12 @ Space for pgd, virt sp, phys resume fn
  75. sub sp, sp, r4 @ allocate CPU state on stack
  76. ldr r3, =sleep_save_sp
  77. stmfd sp!, {r0, r1} @ save suspend func arg and pointer
  78. ldr r3, [r3, #SLEEP_SAVE_SP_VIRT]
  79. ALT_SMP(W(nop)) @ don't use adr_l inside ALT_SMP()
  80. ALT_UP_B(1f)
  81. adr_l r0, mpidr_hash
  82. /* This ldmia relies on the memory layout of the mpidr_hash struct */
  83. ldmia r0, {r1, r6-r8} @ r1 = mpidr mask (r6,r7,r8) = l[0,1,2] shifts
  84. compute_mpidr_hash r0, r6, r7, r8, r2, r1
  85. add r3, r3, r0, lsl #2
  86. 1: mov r2, r5 @ virtual SP
  87. mov r1, r4 @ size of save block
  88. add r0, sp, #8 @ pointer to save block
  89. bl __cpu_suspend_save
  90. badr lr, cpu_suspend_abort
  91. ldmfd sp!, {r0, pc} @ call suspend fn
  92. ENDPROC(__cpu_suspend)
  93. .ltorg
  94. cpu_suspend_abort:
  95. ldmia sp!, {r1 - r3} @ pop phys pgd, virt SP, phys resume fn
  96. teq r0, #0
  97. moveq r0, #1 @ force non-zero value
  98. mov sp, r2
  99. ldmfd sp!, {r4 - r11, pc}
  100. ENDPROC(cpu_suspend_abort)
  101. /*
  102. * r0 = control register value
  103. */
  104. .align 5
  105. .pushsection .idmap.text,"ax"
  106. ENTRY(cpu_resume_mmu)
  107. ldr r3, =cpu_resume_after_mmu
  108. instr_sync
  109. mcr p15, 0, r0, c1, c0, 0 @ turn on MMU, I-cache, etc
  110. mrc p15, 0, r0, c0, c0, 0 @ read id reg
  111. instr_sync
  112. mov r0, r0
  113. mov r0, r0
  114. ret r3 @ jump to virtual address
  115. ENDPROC(cpu_resume_mmu)
  116. .popsection
  117. cpu_resume_after_mmu:
  118. #if defined(CONFIG_VMAP_STACK) && !defined(CONFIG_ARM_LPAE)
  119. @ Before using the vmap'ed stack, we have to switch to swapper_pg_dir
  120. @ as the ID map does not cover the vmalloc region.
  121. mrc p15, 0, ip, c2, c0, 1 @ read TTBR1
  122. mcr p15, 0, ip, c2, c0, 0 @ set TTBR0
  123. instr_sync
  124. #endif
  125. bl cpu_init @ restore the und/abt/irq banked regs
  126. mov r0, #0 @ return zero on success
  127. ldmfd sp!, {r4 - r11, pc}
  128. ENDPROC(cpu_resume_after_mmu)
  129. .text
  130. .align
  131. #ifdef CONFIG_MCPM
  132. .arm
  133. THUMB( .thumb )
  134. ENTRY(cpu_resume_no_hyp)
  135. ARM_BE8(setend be) @ ensure we are in BE mode
  136. b no_hyp
  137. #endif
  138. #ifdef CONFIG_MMU
  139. .arm
  140. ENTRY(cpu_resume_arm)
  141. THUMB( badr r9, 1f ) @ Kernel is entered in ARM.
  142. THUMB( bx r9 ) @ If this is a Thumb-2 kernel,
  143. THUMB( .thumb ) @ switch to Thumb now.
  144. THUMB(1: )
  145. #endif
  146. ENTRY(cpu_resume)
  147. ARM_BE8(setend be) @ ensure we are in BE mode
  148. #ifdef CONFIG_ARM_VIRT_EXT
  149. bl __hyp_stub_install_secondary
  150. #endif
  151. safe_svcmode_maskall r1
  152. no_hyp:
  153. mov r1, #0
  154. ALT_SMP(mrc p15, 0, r0, c0, c0, 5)
  155. ALT_UP_B(1f)
  156. adr_l r2, mpidr_hash @ r2 = struct mpidr_hash phys address
  157. /*
  158. * This ldmia relies on the memory layout of the mpidr_hash
  159. * struct mpidr_hash.
  160. */
  161. ldmia r2, { r3-r6 } @ r3 = mpidr mask (r4,r5,r6) = l[0,1,2] shifts
  162. compute_mpidr_hash r1, r4, r5, r6, r0, r3
  163. 1:
  164. ldr_l r0, sleep_save_sp + SLEEP_SAVE_SP_PHYS
  165. ldr r0, [r0, r1, lsl #2]
  166. @ load phys pgd, stack, resume fn
  167. ARM( ldmia r0!, {r1, sp, pc} )
  168. THUMB( ldmia r0!, {r1, r2, r3} )
  169. THUMB( mov sp, r2 )
  170. THUMB( bx r3 )
  171. ENDPROC(cpu_resume)
  172. #ifdef CONFIG_MMU
  173. ENDPROC(cpu_resume_arm)
  174. #endif
  175. #ifdef CONFIG_MCPM
  176. ENDPROC(cpu_resume_no_hyp)
  177. #endif
  178. .data
  179. .align 2
  180. .type sleep_save_sp, #object
  181. ENTRY(sleep_save_sp)
  182. .space SLEEP_SAVE_SP_SZ @ struct sleep_save_sp