psci.S 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (C) 2013,2014 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <config.h>
  18. #include <linux/linkage.h>
  19. #include <asm/macro.h>
  20. #include <asm/psci.h>
  21. .pushsection ._secure.text, "ax"
  22. .arch_extension sec
  23. .align 5
  24. .globl _psci_vectors
  25. _psci_vectors:
  26. b default_psci_vector @ reset
  27. b default_psci_vector @ undef
  28. b _smc_psci @ smc
  29. b default_psci_vector @ pabort
  30. b default_psci_vector @ dabort
  31. b default_psci_vector @ hyp
  32. b default_psci_vector @ irq
  33. b psci_fiq_enter @ fiq
  34. ENTRY(psci_fiq_enter)
  35. movs pc, lr
  36. ENDPROC(psci_fiq_enter)
  37. .weak psci_fiq_enter
  38. ENTRY(default_psci_vector)
  39. movs pc, lr
  40. ENDPROC(default_psci_vector)
  41. .weak default_psci_vector
  42. ENTRY(psci_version)
  43. ENTRY(psci_cpu_suspend)
  44. ENTRY(psci_cpu_off)
  45. ENTRY(psci_cpu_on)
  46. ENTRY(psci_affinity_info)
  47. ENTRY(psci_migrate)
  48. ENTRY(psci_migrate_info_type)
  49. ENTRY(psci_migrate_info_up_cpu)
  50. ENTRY(psci_system_off)
  51. ENTRY(psci_system_reset)
  52. ENTRY(psci_features)
  53. ENTRY(psci_cpu_freeze)
  54. ENTRY(psci_cpu_default_suspend)
  55. ENTRY(psci_node_hw_state)
  56. ENTRY(psci_system_suspend)
  57. ENTRY(psci_set_suspend_mode)
  58. ENTRY(psi_stat_residency)
  59. ENTRY(psci_stat_count)
  60. mov r0, #ARM_PSCI_RET_NI @ Return -1 (Not Implemented)
  61. mov pc, lr
  62. ENDPROC(psci_stat_count)
  63. ENDPROC(psi_stat_residency)
  64. ENDPROC(psci_set_suspend_mode)
  65. ENDPROC(psci_system_suspend)
  66. ENDPROC(psci_node_hw_state)
  67. ENDPROC(psci_cpu_default_suspend)
  68. ENDPROC(psci_cpu_freeze)
  69. ENDPROC(psci_features)
  70. ENDPROC(psci_system_reset)
  71. ENDPROC(psci_system_off)
  72. ENDPROC(psci_migrate_info_up_cpu)
  73. ENDPROC(psci_migrate_info_type)
  74. ENDPROC(psci_migrate)
  75. ENDPROC(psci_affinity_info)
  76. ENDPROC(psci_cpu_on)
  77. ENDPROC(psci_cpu_off)
  78. ENDPROC(psci_cpu_suspend)
  79. ENDPROC(psci_version)
  80. .weak psci_version
  81. .weak psci_cpu_suspend
  82. .weak psci_cpu_off
  83. .weak psci_cpu_on
  84. .weak psci_affinity_info
  85. .weak psci_migrate
  86. .weak psci_migrate_info_type
  87. .weak psci_migrate_info_up_cpu
  88. .weak psci_system_off
  89. .weak psci_system_reset
  90. .weak psci_features
  91. .weak psci_cpu_freeze
  92. .weak psci_cpu_default_suspend
  93. .weak psci_node_hw_state
  94. .weak psci_system_suspend
  95. .weak psci_set_suspend_mode
  96. .weak psi_stat_residency
  97. .weak psci_stat_count
  98. _psci_table:
  99. .word ARM_PSCI_FN_CPU_SUSPEND
  100. .word psci_cpu_suspend
  101. .word ARM_PSCI_FN_CPU_OFF
  102. .word psci_cpu_off
  103. .word ARM_PSCI_FN_CPU_ON
  104. .word psci_cpu_on
  105. .word ARM_PSCI_FN_MIGRATE
  106. .word psci_migrate
  107. .word ARM_PSCI_0_2_FN_PSCI_VERSION
  108. .word psci_version
  109. .word ARM_PSCI_0_2_FN_CPU_SUSPEND
  110. .word psci_cpu_suspend
  111. .word ARM_PSCI_0_2_FN_CPU_OFF
  112. .word psci_cpu_off
  113. .word ARM_PSCI_0_2_FN_CPU_ON
  114. .word psci_cpu_on
  115. .word ARM_PSCI_0_2_FN_AFFINITY_INFO
  116. .word psci_affinity_info
  117. .word ARM_PSCI_0_2_FN_MIGRATE
  118. .word psci_migrate
  119. .word ARM_PSCI_0_2_FN_MIGRATE_INFO_TYPE
  120. .word psci_migrate_info_type
  121. .word ARM_PSCI_0_2_FN_MIGRATE_INFO_UP_CPU
  122. .word psci_migrate_info_up_cpu
  123. .word ARM_PSCI_0_2_FN_SYSTEM_OFF
  124. .word psci_system_off
  125. .word ARM_PSCI_0_2_FN_SYSTEM_RESET
  126. .word psci_system_reset
  127. .word ARM_PSCI_1_0_FN_PSCI_FEATURES
  128. .word psci_features
  129. .word ARM_PSCI_1_0_FN_CPU_FREEZE
  130. .word psci_cpu_freeze
  131. .word ARM_PSCI_1_0_FN_CPU_DEFAULT_SUSPEND
  132. .word psci_cpu_default_suspend
  133. .word ARM_PSCI_1_0_FN_NODE_HW_STATE
  134. .word psci_node_hw_state
  135. .word ARM_PSCI_1_0_FN_SYSTEM_SUSPEND
  136. .word psci_system_suspend
  137. .word ARM_PSCI_1_0_FN_SET_SUSPEND_MODE
  138. .word psci_set_suspend_mode
  139. .word ARM_PSCI_1_0_FN_STAT_RESIDENCY
  140. .word psi_stat_residency
  141. .word ARM_PSCI_1_0_FN_STAT_COUNT
  142. .word psci_stat_count
  143. .word 0
  144. .word 0
  145. _smc_psci:
  146. push {r4-r7,lr}
  147. @ Switch to secure
  148. mrc p15, 0, r7, c1, c1, 0
  149. bic r4, r7, #1
  150. mcr p15, 0, r4, c1, c1, 0
  151. isb
  152. adr r4, _psci_table
  153. 1: ldr r5, [r4] @ Load PSCI function ID
  154. ldr r6, [r4, #4] @ Load target PC
  155. cmp r5, #0 @ If reach the end, bail out
  156. moveq r0, #ARM_PSCI_RET_INVAL @ Return -2 (Invalid)
  157. beq 2f
  158. cmp r0, r5 @ If not matching, try next entry
  159. addne r4, r4, #8
  160. bne 1b
  161. blx r6 @ Execute PSCI function
  162. @ Switch back to non-secure
  163. 2: mcr p15, 0, r7, c1, c1, 0
  164. pop {r4-r7, lr}
  165. movs pc, lr @ Return to the kernel
  166. @ Requires dense and single-cluster CPU ID space
  167. ENTRY(psci_get_cpu_id)
  168. mrc p15, 0, r0, c0, c0, 5 /* read MPIDR */
  169. and r0, r0, #0xff /* return CPU ID in cluster */
  170. bx lr
  171. ENDPROC(psci_get_cpu_id)
  172. .weak psci_get_cpu_id
  173. /* Imported from Linux kernel */
  174. ENTRY(psci_v7_flush_dcache_all)
  175. stmfd sp!, {r4-r5, r7, r9-r11, lr}
  176. dmb @ ensure ordering with previous memory accesses
  177. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  178. ands r3, r0, #0x7000000 @ extract loc from clidr
  179. mov r3, r3, lsr #23 @ left align loc bit field
  180. beq finished @ if loc is 0, then no need to clean
  181. mov r10, #0 @ start clean at cache level 0
  182. flush_levels:
  183. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  184. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  185. and r1, r1, #7 @ mask of the bits for current cache only
  186. cmp r1, #2 @ see what cache we have at this level
  187. blt skip @ skip if no cache, or just i-cache
  188. mrs r9, cpsr @ make cssr&csidr read atomic
  189. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  190. isb @ isb to sych the new cssr&csidr
  191. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  192. msr cpsr_c, r9
  193. and r2, r1, #7 @ extract the length of the cache lines
  194. add r2, r2, #4 @ add 4 (line length offset)
  195. ldr r4, =0x3ff
  196. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  197. clz r5, r4 @ find bit position of way size increment
  198. ldr r7, =0x7fff
  199. ands r7, r7, r1, lsr #13 @ extract max number of the index size
  200. loop1:
  201. mov r9, r7 @ create working copy of max index
  202. loop2:
  203. orr r11, r10, r4, lsl r5 @ factor way and cache number into r11
  204. orr r11, r11, r9, lsl r2 @ factor index number into r11
  205. mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
  206. subs r9, r9, #1 @ decrement the index
  207. bge loop2
  208. subs r4, r4, #1 @ decrement the way
  209. bge loop1
  210. skip:
  211. add r10, r10, #2 @ increment cache number
  212. cmp r3, r10
  213. bgt flush_levels
  214. finished:
  215. mov r10, #0 @ swith back to cache level 0
  216. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  217. dsb st
  218. isb
  219. ldmfd sp!, {r4-r5, r7, r9-r11, lr}
  220. bx lr
  221. ENDPROC(psci_v7_flush_dcache_all)
  222. ENTRY(psci_disable_smp)
  223. mrc p15, 0, r0, c1, c0, 1 @ ACTLR
  224. bic r0, r0, #(1 << 6) @ Clear SMP bit
  225. mcr p15, 0, r0, c1, c0, 1 @ ACTLR
  226. isb
  227. dsb
  228. bx lr
  229. ENDPROC(psci_disable_smp)
  230. .weak psci_disable_smp
  231. ENTRY(psci_enable_smp)
  232. mrc p15, 0, r0, c1, c0, 1 @ ACTLR
  233. orr r0, r0, #(1 << 6) @ Set SMP bit
  234. mcr p15, 0, r0, c1, c0, 1 @ ACTLR
  235. isb
  236. bx lr
  237. ENDPROC(psci_enable_smp)
  238. .weak psci_enable_smp
  239. ENTRY(psci_cpu_off_common)
  240. push {lr}
  241. bl psci_v7_flush_dcache_all
  242. clrex @ Why???
  243. mrc p15, 0, r0, c1, c0, 0 @ SCTLR
  244. bic r0, r0, #(1 << 2) @ Clear C bit
  245. mcr p15, 0, r0, c1, c0, 0 @ SCTLR
  246. isb
  247. dsb
  248. bl psci_v7_flush_dcache_all
  249. clrex @ Why???
  250. bl psci_disable_smp
  251. pop {lr}
  252. bx lr
  253. ENDPROC(psci_cpu_off_common)
  254. @ The stacks are allocated in reverse order, i.e.
  255. @ the stack for CPU0 has the highest memory address.
  256. @
  257. @ -------------------- __secure_stack_end
  258. @ | CPU0 target PC |
  259. @ |------------------|
  260. @ | |
  261. @ | CPU0 stack |
  262. @ | |
  263. @ |------------------| __secure_stack_end - 1KB
  264. @ | . |
  265. @ | . |
  266. @ | . |
  267. @ | . |
  268. @ -------------------- __secure_stack_start
  269. @
  270. @ This expects CPU ID in r0 and returns stack top in r0
  271. LENTRY(psci_get_cpu_stack_top)
  272. @ stack top = __secure_stack_end - (cpuid << ARM_PSCI_STACK_SHIFT)
  273. ldr r3, =__secure_stack_end
  274. sub r0, r3, r0, LSL #ARM_PSCI_STACK_SHIFT
  275. sub r0, r0, #4 @ Save space for target PC
  276. bx lr
  277. ENDPROC(psci_get_cpu_stack_top)
  278. @ {r0, r1, r2, ip} from _do_nonsec_entry(kernel_entry, 0, machid, r2) in
  279. @ arch/arm/lib/bootm.c:boot_jump_linux() must remain unchanged across
  280. @ this function.
  281. ENTRY(psci_stack_setup)
  282. mov r6, lr
  283. mov r7, r0
  284. bl psci_get_cpu_id @ CPU ID => r0
  285. bl psci_get_cpu_stack_top @ stack top => r0
  286. mov sp, r0
  287. mov r0, r7
  288. bx r6
  289. ENDPROC(psci_stack_setup)
  290. ENTRY(psci_arch_init)
  291. mov pc, lr
  292. ENDPROC(psci_arch_init)
  293. .weak psci_arch_init
  294. ENTRY(psci_cpu_entry)
  295. bl psci_enable_smp
  296. bl _nonsec_init
  297. bl psci_get_cpu_id @ CPU ID => r0
  298. bl psci_get_target_pc @ target PC => r0
  299. b _do_nonsec_entry
  300. ENDPROC(psci_cpu_entry)
  301. .popsection