blob: 95b962dadf0b2c380de57ce3dcdedfda91ff9caa [file] [log] [blame]
Marc Zyngierecf07a72014-07-12 14:24:04 +01001/*
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 Kiszka3317b982015-04-21 07:18:30 +020020#include <asm/macro.h>
Marc Zyngierecf07a72014-07-12 14:24:04 +010021#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
39ENTRY(psci_fiq_enter)
40 movs pc, lr
41ENDPROC(psci_fiq_enter)
42.weak psci_fiq_enter
43
44ENTRY(default_psci_vector)
45 movs pc, lr
46ENDPROC(default_psci_vector)
47.weak default_psci_vector
48
Hongbo Zhang116339d2016-07-21 18:09:36 +080049ENTRY(psci_version)
Marc Zyngierecf07a72014-07-12 14:24:04 +010050ENTRY(psci_cpu_suspend)
51ENTRY(psci_cpu_off)
52ENTRY(psci_cpu_on)
Hongbo Zhang116339d2016-07-21 18:09:36 +080053ENTRY(psci_affinity_info)
Marc Zyngierecf07a72014-07-12 14:24:04 +010054ENTRY(psci_migrate)
Hongbo Zhang116339d2016-07-21 18:09:36 +080055ENTRY(psci_migrate_info_type)
56ENTRY(psci_migrate_info_up_cpu)
57ENTRY(psci_system_off)
58ENTRY(psci_system_reset)
59ENTRY(psci_features)
60ENTRY(psci_cpu_freeze)
61ENTRY(psci_cpu_default_suspend)
62ENTRY(psci_node_hw_state)
63ENTRY(psci_system_suspend)
64ENTRY(psci_set_suspend_mode)
65ENTRY(psi_stat_residency)
66ENTRY(psci_stat_count)
Marc Zyngierecf07a72014-07-12 14:24:04 +010067 mov r0, #ARM_PSCI_RET_NI @ Return -1 (Not Implemented)
68 mov pc, lr
Hongbo Zhang116339d2016-07-21 18:09:36 +080069ENDPROC(psci_stat_count)
70ENDPROC(psi_stat_residency)
71ENDPROC(psci_set_suspend_mode)
72ENDPROC(psci_system_suspend)
73ENDPROC(psci_node_hw_state)
74ENDPROC(psci_cpu_default_suspend)
75ENDPROC(psci_cpu_freeze)
76ENDPROC(psci_features)
77ENDPROC(psci_system_reset)
78ENDPROC(psci_system_off)
79ENDPROC(psci_migrate_info_up_cpu)
80ENDPROC(psci_migrate_info_type)
Marc Zyngierecf07a72014-07-12 14:24:04 +010081ENDPROC(psci_migrate)
Hongbo Zhang116339d2016-07-21 18:09:36 +080082ENDPROC(psci_affinity_info)
Marc Zyngierecf07a72014-07-12 14:24:04 +010083ENDPROC(psci_cpu_on)
84ENDPROC(psci_cpu_off)
85ENDPROC(psci_cpu_suspend)
Hongbo Zhang116339d2016-07-21 18:09:36 +080086ENDPROC(psci_version)
87.weak psci_version
Marc Zyngierecf07a72014-07-12 14:24:04 +010088.weak psci_cpu_suspend
89.weak psci_cpu_off
90.weak psci_cpu_on
Hongbo Zhang116339d2016-07-21 18:09:36 +080091.weak psci_affinity_info
Marc Zyngierecf07a72014-07-12 14:24:04 +010092.weak psci_migrate
Hongbo Zhang116339d2016-07-21 18:09:36 +080093.weak psci_migrate_info_type
94.weak psci_migrate_info_up_cpu
95.weak psci_system_off
96.weak psci_system_reset
97.weak psci_features
98.weak psci_cpu_freeze
99.weak psci_cpu_default_suspend
100.weak psci_node_hw_state
101.weak psci_system_suspend
102.weak psci_set_suspend_mode
103.weak psi_stat_residency
104.weak psci_stat_count
Marc Zyngierecf07a72014-07-12 14:24:04 +0100105
106_psci_table:
107 .word ARM_PSCI_FN_CPU_SUSPEND
108 .word psci_cpu_suspend
109 .word ARM_PSCI_FN_CPU_OFF
110 .word psci_cpu_off
111 .word ARM_PSCI_FN_CPU_ON
112 .word psci_cpu_on
113 .word ARM_PSCI_FN_MIGRATE
114 .word psci_migrate
Hongbo Zhang116339d2016-07-21 18:09:36 +0800115 .word ARM_PSCI_0_2_FN_PSCI_VERSION
116 .word psci_version
117 .word ARM_PSCI_0_2_FN_CPU_SUSPEND
118 .word psci_cpu_suspend
119 .word ARM_PSCI_0_2_FN_CPU_OFF
120 .word psci_cpu_off
121 .word ARM_PSCI_0_2_FN_CPU_ON
122 .word psci_cpu_on
123 .word ARM_PSCI_0_2_FN_AFFINITY_INFO
124 .word psci_affinity_info
125 .word ARM_PSCI_0_2_FN_MIGRATE
126 .word psci_migrate
127 .word ARM_PSCI_0_2_FN_MIGRATE_INFO_TYPE
128 .word psci_migrate_info_type
129 .word ARM_PSCI_0_2_FN_MIGRATE_INFO_UP_CPU
130 .word psci_migrate_info_up_cpu
131 .word ARM_PSCI_0_2_FN_SYSTEM_OFF
132 .word psci_system_off
133 .word ARM_PSCI_0_2_FN_SYSTEM_RESET
134 .word psci_system_reset
135 .word ARM_PSCI_1_0_FN_PSCI_FEATURES
136 .word psci_features
137 .word ARM_PSCI_1_0_FN_CPU_FREEZE
138 .word psci_cpu_freeze
139 .word ARM_PSCI_1_0_FN_CPU_DEFAULT_SUSPEND
140 .word psci_cpu_default_suspend
141 .word ARM_PSCI_1_0_FN_NODE_HW_STATE
142 .word psci_node_hw_state
143 .word ARM_PSCI_1_0_FN_SYSTEM_SUSPEND
144 .word psci_system_suspend
145 .word ARM_PSCI_1_0_FN_SET_SUSPEND_MODE
146 .word psci_set_suspend_mode
147 .word ARM_PSCI_1_0_FN_STAT_RESIDENCY
148 .word psi_stat_residency
149 .word ARM_PSCI_1_0_FN_STAT_COUNT
150 .word psci_stat_count
Marc Zyngierecf07a72014-07-12 14:24:04 +0100151 .word 0
152 .word 0
153
154_smc_psci:
155 push {r4-r7,lr}
156
157 @ Switch to secure
158 mrc p15, 0, r7, c1, c1, 0
159 bic r4, r7, #1
160 mcr p15, 0, r4, c1, c1, 0
161 isb
162
163 adr r4, _psci_table
1641: ldr r5, [r4] @ Load PSCI function ID
165 ldr r6, [r4, #4] @ Load target PC
166 cmp r5, #0 @ If reach the end, bail out
167 moveq r0, #ARM_PSCI_RET_INVAL @ Return -2 (Invalid)
168 beq 2f
169 cmp r0, r5 @ If not matching, try next entry
170 addne r4, r4, #8
171 bne 1b
172
173 blx r6 @ Execute PSCI function
174
175 @ Switch back to non-secure
1762: mcr p15, 0, r7, c1, c1, 0
177
178 pop {r4-r7, lr}
179 movs pc, lr @ Return to the kernel
180
Jan Kiszka680f3962015-04-21 07:18:26 +0200181@ Requires dense and single-cluster CPU ID space
182ENTRY(psci_get_cpu_id)
183 mrc p15, 0, r0, c0, c0, 5 /* read MPIDR */
184 and r0, r0, #0xff /* return CPU ID in cluster */
185 bx lr
186ENDPROC(psci_get_cpu_id)
187.weak psci_get_cpu_id
188
Jan Kiszkab0206e72015-04-21 07:18:27 +0200189/* Imported from Linux kernel */
Hongbo Zhangd38def12016-08-19 17:20:30 +0800190ENTRY(psci_v7_flush_dcache_all)
Chen-Yu Tsai778dc5f2016-06-07 10:54:25 +0800191 stmfd sp!, {r4-r5, r7, r9-r11, lr}
Jan Kiszkab0206e72015-04-21 07:18:27 +0200192 dmb @ ensure ordering with previous memory accesses
193 mrc p15, 1, r0, c0, c0, 1 @ read clidr
194 ands r3, r0, #0x7000000 @ extract loc from clidr
195 mov r3, r3, lsr #23 @ left align loc bit field
196 beq finished @ if loc is 0, then no need to clean
197 mov r10, #0 @ start clean at cache level 0
198flush_levels:
199 add r2, r10, r10, lsr #1 @ work out 3x current cache level
200 mov r1, r0, lsr r2 @ extract cache type bits from clidr
201 and r1, r1, #7 @ mask of the bits for current cache only
202 cmp r1, #2 @ see what cache we have at this level
203 blt skip @ skip if no cache, or just i-cache
204 mrs r9, cpsr @ make cssr&csidr read atomic
205 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
206 isb @ isb to sych the new cssr&csidr
207 mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
208 msr cpsr_c, r9
209 and r2, r1, #7 @ extract the length of the cache lines
210 add r2, r2, #4 @ add 4 (line length offset)
211 ldr r4, =0x3ff
212 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
213 clz r5, r4 @ find bit position of way size increment
214 ldr r7, =0x7fff
215 ands r7, r7, r1, lsr #13 @ extract max number of the index size
216loop1:
217 mov r9, r7 @ create working copy of max index
218loop2:
219 orr r11, r10, r4, lsl r5 @ factor way and cache number into r11
220 orr r11, r11, r9, lsl r2 @ factor index number into r11
221 mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
222 subs r9, r9, #1 @ decrement the index
223 bge loop2
224 subs r4, r4, #1 @ decrement the way
225 bge loop1
226skip:
227 add r10, r10, #2 @ increment cache number
228 cmp r3, r10
229 bgt flush_levels
230finished:
231 mov r10, #0 @ swith back to cache level 0
232 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
233 dsb st
234 isb
Chen-Yu Tsai778dc5f2016-06-07 10:54:25 +0800235 ldmfd sp!, {r4-r5, r7, r9-r11, lr}
Jan Kiszkab0206e72015-04-21 07:18:27 +0200236 bx lr
Hongbo Zhangd38def12016-08-19 17:20:30 +0800237ENDPROC(psci_v7_flush_dcache_all)
Jan Kiszkab0206e72015-04-21 07:18:27 +0200238
239ENTRY(psci_disable_smp)
240 mrc p15, 0, r0, c1, c0, 1 @ ACTLR
241 bic r0, r0, #(1 << 6) @ Clear SMP bit
242 mcr p15, 0, r0, c1, c0, 1 @ ACTLR
243 isb
244 dsb
245 bx lr
246ENDPROC(psci_disable_smp)
247.weak psci_disable_smp
248
Jan Kiszka4ce4de12015-04-21 07:18:28 +0200249ENTRY(psci_enable_smp)
250 mrc p15, 0, r0, c1, c0, 1 @ ACTLR
251 orr r0, r0, #(1 << 6) @ Set SMP bit
252 mcr p15, 0, r0, c1, c0, 1 @ ACTLR
253 isb
254 bx lr
255ENDPROC(psci_enable_smp)
256.weak psci_enable_smp
257
Jan Kiszkab0206e72015-04-21 07:18:27 +0200258ENTRY(psci_cpu_off_common)
259 push {lr}
260
Peng Fanfea74522016-11-22 19:41:09 +0800261 bl psci_v7_flush_dcache_all
262
263 clrex @ Why???
264
Jan Kiszkab0206e72015-04-21 07:18:27 +0200265 mrc p15, 0, r0, c1, c0, 0 @ SCTLR
266 bic r0, r0, #(1 << 2) @ Clear C bit
267 mcr p15, 0, r0, c1, c0, 0 @ SCTLR
268 isb
269 dsb
270
Hongbo Zhangd38def12016-08-19 17:20:30 +0800271 bl psci_v7_flush_dcache_all
Jan Kiszkab0206e72015-04-21 07:18:27 +0200272
273 clrex @ Why???
274
275 bl psci_disable_smp
276
277 pop {lr}
278 bx lr
279ENDPROC(psci_cpu_off_common)
280
Chen-Yu Tsai8c0ef7f2016-06-19 12:38:37 +0800281@ The stacks are allocated in reverse order, i.e.
282@ the stack for CPU0 has the highest memory address.
283@
284@ -------------------- __secure_stack_end
285@ | CPU0 target PC |
286@ |------------------|
287@ | |
288@ | CPU0 stack |
289@ | |
290@ |------------------| __secure_stack_end - 1KB
291@ | . |
292@ | . |
293@ | . |
294@ | . |
295@ -------------------- __secure_stack_start
296@
297@ This expects CPU ID in r0 and returns stack top in r0
Chen-Yu Tsaib7073962016-06-19 12:38:45 +0800298LENTRY(psci_get_cpu_stack_top)
Chen-Yu Tsai8c0ef7f2016-06-19 12:38:37 +0800299 @ stack top = __secure_stack_end - (cpuid << ARM_PSCI_STACK_SHIFT)
300 ldr r3, =__secure_stack_end
301 sub r0, r3, r0, LSL #ARM_PSCI_STACK_SHIFT
302 sub r0, r0, #4 @ Save space for target PC
Jan Kiszka4c681a32015-04-21 07:18:29 +0200303 bx lr
304ENDPROC(psci_get_cpu_stack_top)
305
Chen-Yu Tsaib5281322016-06-19 12:38:31 +0800306@ {r0, r1, r2, ip} from _do_nonsec_entry(kernel_entry, 0, machid, r2) in
307@ arch/arm/lib/bootm.c:boot_jump_linux() must remain unchanged across
308@ this function.
309ENTRY(psci_stack_setup)
310 mov r6, lr
311 mov r7, r0
312 bl psci_get_cpu_id @ CPU ID => r0
313 bl psci_get_cpu_stack_top @ stack top => r0
314 mov sp, r0
315 mov r0, r7
316 bx r6
317ENDPROC(psci_stack_setup)
318
319ENTRY(psci_arch_init)
320 mov pc, lr
321ENDPROC(psci_arch_init)
322.weak psci_arch_init
323
Jan Kiszka4ce4de12015-04-21 07:18:28 +0200324ENTRY(psci_cpu_entry)
325 bl psci_enable_smp
326
327 bl _nonsec_init
328
Jan Kiszka3317b982015-04-21 07:18:30 +0200329 bl psci_get_cpu_id @ CPU ID => r0
Chen-Yu Tsai6e6622d2016-06-19 12:38:44 +0800330 bl psci_get_target_pc @ target PC => r0
Jan Kiszka4ce4de12015-04-21 07:18:28 +0200331 b _do_nonsec_entry
332ENDPROC(psci_cpu_entry)
333
Marc Zyngierecf07a72014-07-12 14:24:04 +0100334 .popsection