sleep.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_MMU
  118. .arm
  119. ENTRY(cpu_resume_arm)
  120. THUMB( badr r9, 1f ) @ Kernel is entered in ARM.
  121. THUMB( bx r9 ) @ If this is a Thumb-2 kernel,
  122. THUMB( .thumb ) @ switch to Thumb now.
  123. THUMB(1: )
  124. #endif
  125. ENTRY(cpu_resume)
  126. ARM_BE8(setend be) @ ensure we are in BE mode
  127. #ifdef CONFIG_ARM_VIRT_EXT
  128. bl __hyp_stub_install_secondary
  129. #endif
  130. safe_svcmode_maskall r1
  131. mov r1, #0
  132. ALT_SMP(mrc p15, 0, r0, c0, c0, 5)
  133. ALT_UP_B(1f)
  134. adr r2, mpidr_hash_ptr
  135. ldr r3, [r2]
  136. add r2, r2, r3 @ r2 = struct mpidr_hash phys address
  137. /*
  138. * This ldmia relies on the memory layout of the mpidr_hash
  139. * struct mpidr_hash.
  140. */
  141. ldmia r2, { r3-r6 } @ r3 = mpidr mask (r4,r5,r6) = l[0,1,2] shifts
  142. compute_mpidr_hash r1, r4, r5, r6, r0, r3
  143. 1:
  144. adr r0, _sleep_save_sp
  145. ldr r2, [r0]
  146. add r0, r0, r2
  147. ldr r0, [r0, #SLEEP_SAVE_SP_PHYS]
  148. ldr r0, [r0, r1, lsl #2]
  149. @ load phys pgd, stack, resume fn
  150. ARM( ldmia r0!, {r1, sp, pc} )
  151. THUMB( ldmia r0!, {r1, r2, r3} )
  152. THUMB( mov sp, r2 )
  153. THUMB( bx r3 )
  154. ENDPROC(cpu_resume)
  155. #ifdef CONFIG_MMU
  156. ENDPROC(cpu_resume_arm)
  157. #endif
  158. .align 2
  159. _sleep_save_sp:
  160. .long sleep_save_sp - .
  161. mpidr_hash_ptr:
  162. .long mpidr_hash - . @ mpidr_hash struct offset
  163. .data
  164. .align 2
  165. .type sleep_save_sp, #object
  166. ENTRY(sleep_save_sp)
  167. .space SLEEP_SAVE_SP_SZ @ struct sleep_save_sp