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