blob: 6aece1198712a4b129b8c798fab08e57f0a138ce [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +08002/*
3 * Copyright 2016 Freescale Semiconductor, Inc.
4 * Author: Hongbo Zhang <hongbo.zhang@nxp.com>
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +08005 * This file implements LS102X platform PSCI SYSTEM-SUSPEND function
6 */
7
8#include <config.h>
9#include <linux/linkage.h>
10#include <asm/psci.h>
Chee Hong Angf6b01152019-02-12 00:27:02 -080011#include <asm/secure.h>
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +080012
13/* Default PSCI function, return -1, Not Implemented */
14#define PSCI_DEFAULT(__fn) \
Tom Rinifc224002022-11-22 12:31:56 -050015 WEAK(__fn); \
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +080016 mov w0, #ARM_PSCI_RET_NI; \
17 ret; \
18 ENDPROC(__fn); \
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +080019
20/* PSCI function and ID table definition*/
21#define PSCI_TABLE(__id, __fn) \
Lars Povlsen25c07c72019-04-04 14:38:50 +020022 .quad __id; \
23 .quad __fn
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +080024
25.pushsection ._secure.text, "ax"
26
27/* 32 bits PSCI default functions */
28PSCI_DEFAULT(psci_version)
29PSCI_DEFAULT(psci_cpu_suspend)
30PSCI_DEFAULT(psci_cpu_off)
31PSCI_DEFAULT(psci_cpu_on)
32PSCI_DEFAULT(psci_affinity_info)
33PSCI_DEFAULT(psci_migrate)
34PSCI_DEFAULT(psci_migrate_info_type)
35PSCI_DEFAULT(psci_migrate_info_up_cpu)
36PSCI_DEFAULT(psci_system_off)
37PSCI_DEFAULT(psci_system_reset)
38PSCI_DEFAULT(psci_features)
39PSCI_DEFAULT(psci_cpu_freeze)
40PSCI_DEFAULT(psci_cpu_default_suspend)
41PSCI_DEFAULT(psci_node_hw_state)
42PSCI_DEFAULT(psci_system_suspend)
43PSCI_DEFAULT(psci_set_suspend_mode)
44PSCI_DEFAULT(psi_stat_residency)
45PSCI_DEFAULT(psci_stat_count)
46
47.align 3
48_psci_32_table:
49PSCI_TABLE(ARM_PSCI_FN_CPU_SUSPEND, psci_cpu_suspend)
50PSCI_TABLE(ARM_PSCI_FN_CPU_OFF, psci_cpu_off)
51PSCI_TABLE(ARM_PSCI_FN_CPU_ON, psci_cpu_on)
52PSCI_TABLE(ARM_PSCI_FN_MIGRATE, psci_migrate)
53PSCI_TABLE(ARM_PSCI_0_2_FN_PSCI_VERSION, psci_version)
54PSCI_TABLE(ARM_PSCI_0_2_FN_CPU_SUSPEND, psci_cpu_suspend)
55PSCI_TABLE(ARM_PSCI_0_2_FN_CPU_OFF, psci_cpu_off)
56PSCI_TABLE(ARM_PSCI_0_2_FN_CPU_ON, psci_cpu_on)
57PSCI_TABLE(ARM_PSCI_0_2_FN_AFFINITY_INFO, psci_affinity_info)
58PSCI_TABLE(ARM_PSCI_0_2_FN_MIGRATE, psci_migrate)
59PSCI_TABLE(ARM_PSCI_0_2_FN_MIGRATE_INFO_TYPE, psci_migrate_info_type)
60PSCI_TABLE(ARM_PSCI_0_2_FN_MIGRATE_INFO_UP_CPU, psci_migrate_info_up_cpu)
61PSCI_TABLE(ARM_PSCI_0_2_FN_SYSTEM_OFF, psci_system_off)
62PSCI_TABLE(ARM_PSCI_0_2_FN_SYSTEM_RESET, psci_system_reset)
63PSCI_TABLE(ARM_PSCI_1_0_FN_PSCI_FEATURES, psci_features)
64PSCI_TABLE(ARM_PSCI_1_0_FN_CPU_FREEZE, psci_cpu_freeze)
65PSCI_TABLE(ARM_PSCI_1_0_FN_CPU_DEFAULT_SUSPEND, psci_cpu_default_suspend)
66PSCI_TABLE(ARM_PSCI_1_0_FN_NODE_HW_STATE, psci_node_hw_state)
67PSCI_TABLE(ARM_PSCI_1_0_FN_SYSTEM_SUSPEND, psci_system_suspend)
68PSCI_TABLE(ARM_PSCI_1_0_FN_SET_SUSPEND_MODE, psci_set_suspend_mode)
69PSCI_TABLE(ARM_PSCI_1_0_FN_STAT_RESIDENCY, psi_stat_residency)
70PSCI_TABLE(ARM_PSCI_1_0_FN_STAT_COUNT, psci_stat_count)
71PSCI_TABLE(0, 0)
72
73/* 64 bits PSCI default functions */
74PSCI_DEFAULT(psci_cpu_suspend_64)
75PSCI_DEFAULT(psci_cpu_on_64)
76PSCI_DEFAULT(psci_affinity_info_64)
77PSCI_DEFAULT(psci_migrate_64)
78PSCI_DEFAULT(psci_migrate_info_up_cpu_64)
79PSCI_DEFAULT(psci_cpu_default_suspend_64)
80PSCI_DEFAULT(psci_node_hw_state_64)
81PSCI_DEFAULT(psci_system_suspend_64)
82PSCI_DEFAULT(psci_stat_residency_64)
83PSCI_DEFAULT(psci_stat_count_64)
84
85.align 3
86_psci_64_table:
87PSCI_TABLE(ARM_PSCI_0_2_FN64_CPU_SUSPEND, psci_cpu_suspend_64)
88PSCI_TABLE(ARM_PSCI_0_2_FN64_CPU_ON, psci_cpu_on_64)
89PSCI_TABLE(ARM_PSCI_0_2_FN64_AFFINITY_INFO, psci_affinity_info_64)
90PSCI_TABLE(ARM_PSCI_0_2_FN64_MIGRATE, psci_migrate_64)
91PSCI_TABLE(ARM_PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU, psci_migrate_info_up_cpu_64)
92PSCI_TABLE(ARM_PSCI_1_0_FN64_CPU_DEFAULT_SUSPEND, psci_cpu_default_suspend_64)
93PSCI_TABLE(ARM_PSCI_1_0_FN64_NODE_HW_STATE, psci_node_hw_state_64)
94PSCI_TABLE(ARM_PSCI_1_0_FN64_SYSTEM_SUSPEND, psci_system_suspend_64)
95PSCI_TABLE(ARM_PSCI_1_0_FN64_STAT_RESIDENCY, psci_stat_residency_64)
96PSCI_TABLE(ARM_PSCI_1_0_FN64_STAT_COUNT, psci_stat_count_64)
97PSCI_TABLE(0, 0)
98
99.macro psci_enter
100 /* PSCI call is Fast Call(atomic), so mask DAIF */
101 mrs x15, DAIF
102 stp x15, xzr, [sp, #-16]!
103 ldr x15, =0x3C0
104 msr DAIF, x15
105 /* SMC convention, x18 ~ x30 should be saved by callee */
106 stp x29, x30, [sp, #-16]!
107 stp x27, x28, [sp, #-16]!
108 stp x25, x26, [sp, #-16]!
109 stp x23, x24, [sp, #-16]!
110 stp x21, x22, [sp, #-16]!
111 stp x19, x20, [sp, #-16]!
112 mrs x15, elr_el3
113 stp x18, x15, [sp, #-16]!
114.endm
115
116.macro psci_return
117 /* restore registers */
118 ldp x18, x15, [sp], #16
119 msr elr_el3, x15
120 ldp x19, x20, [sp], #16
121 ldp x21, x22, [sp], #16
122 ldp x23, x24, [sp], #16
123 ldp x25, x26, [sp], #16
124 ldp x27, x28, [sp], #16
125 ldp x29, x30, [sp], #16
126 /* restore DAIF */
127 ldp x15, xzr, [sp], #16
128 msr DAIF, x15
129 eret
130.endm
131
132/* Caller must put PSCI function-ID table base in x9 */
133handle_psci:
134 psci_enter
Lars Povlsen25c07c72019-04-04 14:38:50 +02001351: ldr x10, [x9] /* Load PSCI function table */
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800136 cbz x10, 3f /* If reach the end, bail out */
137 cmp x10, x0
138 b.eq 2f /* PSCI function found */
Lars Povlsen25c07c72019-04-04 14:38:50 +0200139 add x9, x9, #16 /* If not match, try next entry */
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800140 b 1b
141
Lars Povlsen25c07c72019-04-04 14:38:50 +02001422: ldr x11, [x9, #8] /* Load PSCI function */
143 blr x11 /* Call PSCI function */
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800144 psci_return
145
1463: mov x0, #ARM_PSCI_RET_NI
147 psci_return
148
Chee Hong Angf6b01152019-02-12 00:27:02 -0800149/*
150 * Handle SiP service functions defined in SiP service function table.
151 * Use DECLARE_SECURE_SVC(_name, _id, _fn) to add platform specific SiP
152 * service function into the SiP service function table.
153 * SiP service function table is located in '._secure_svc_tbl_entries' section,
154 * which is next to '._secure.text' section.
155 */
156handle_svc:
157 adr x9, __secure_svc_tbl_start
158 adr x10, __secure_svc_tbl_end
159 subs x12, x10, x9 /* Get number of entries in table */
160 b.eq 2f /* Make sure SiP function table is not empty */
161 psci_enter
1621: ldr x10, [x9] /* Load SiP function table */
163 ldr x11, [x9, #8]
164 cmp w10, w0
165 b.eq 2b /* SiP service function found */
166 add x9, x9, #SECURE_SVC_TBL_OFFSET /* Move to next entry */
167 subs x12, x12, #SECURE_SVC_TBL_OFFSET
168 b.eq 3b /* If reach the end, bail out */
169 b 1b
1702: ldr x0, =0xFFFFFFFF
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800171 eret
172
173handle_smc32:
174 /* SMC function ID 0x84000000-0x8400001F: 32 bits PSCI */
175 ldr w9, =0x8400001F
176 cmp w0, w9
Chee Hong Angf6b01152019-02-12 00:27:02 -0800177 b.gt handle_svc
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800178 ldr w9, =0x84000000
179 cmp w0, w9
Chee Hong Angf6b01152019-02-12 00:27:02 -0800180 b.lt handle_svc
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800181
182 adr x9, _psci_32_table
183 b handle_psci
184
185handle_smc64:
186 /* check SMC32 or SMC64 calls */
187 ubfx x9, x0, #30, #1
188 cbz x9, handle_smc32
189
190 /* SMC function ID 0xC4000000-0xC400001F: 64 bits PSCI */
191 ldr x9, =0xC400001F
192 cmp x0, x9
Chee Hong Angf6b01152019-02-12 00:27:02 -0800193 b.gt handle_svc
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800194 ldr x9, =0xC4000000
195 cmp x0, x9
Chee Hong Angf6b01152019-02-12 00:27:02 -0800196 b.lt handle_svc
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800197
198 adr x9, _psci_64_table
199 b handle_psci
200
201/*
202 * Get CPU ID from MPIDR, suppose every cluster has same number of CPU cores,
203 * Platform with asymmetric clusters should implement their own interface.
204 * In case this function being called by other platform's C code, the ARM
205 * Architecture Procedure Call Standard is considered, e.g. register X0 is
206 * used for the return value, while in this PSCI environment, X0 usually holds
207 * the SMC function identifier, so X0 should be saved by caller function.
208 */
Tom Rinifc224002022-11-22 12:31:56 -0500209WEAK(psci_get_cpu_id)
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800210#ifdef CONFIG_ARMV8_PSCI_CPUS_PER_CLUSTER
211 mrs x9, MPIDR_EL1
212 ubfx x9, x9, #8, #8
213 ldr x10, =CONFIG_ARMV8_PSCI_CPUS_PER_CLUSTER
214 mul x9, x10, x9
215#else
216 mov x9, xzr
217#endif
218 mrs x10, MPIDR_EL1
219 ubfx x10, x10, #0, #8
220 add x0, x10, x9
221 ret
222ENDPROC(psci_get_cpu_id)
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800223
224/* CPU ID input in x0, stack top output in x0*/
225LENTRY(psci_get_cpu_stack_top)
226 adr x9, __secure_stack_end
227 lsl x0, x0, #ARM_PSCI_STACK_SHIFT
228 sub x0, x9, x0
229 ret
230ENDPROC(psci_get_cpu_stack_top)
231
232unhandled_exception:
233 b unhandled_exception /* simply dead loop */
234
235handle_sync:
236 mov x15, x30
237 mov x14, x0
238
239 bl psci_get_cpu_id
240 bl psci_get_cpu_stack_top
241 mov x9, #1
242 msr spsel, x9
243 mov sp, x0
244
245 mov x0, x14
246 mov x30, x15
247
248 mrs x9, esr_el3
249 ubfx x9, x9, #26, #6
250 cmp x9, #0x13
251 b.eq handle_smc32
252 cmp x9, #0x17
253 b.eq handle_smc64
254
255 b unhandled_exception
256
Chee Hong Angeb13ddd2018-08-20 10:57:36 -0700257#ifdef CONFIG_ARMV8_EA_EL3_FIRST
258/*
259 * Override this function if custom error handling is
260 * needed for asynchronous aborts
261 */
Tom Rinifc224002022-11-22 12:31:56 -0500262WEAK(plat_error_handler)
Chee Hong Angeb13ddd2018-08-20 10:57:36 -0700263 ret
264ENDPROC(plat_error_handler)
Chee Hong Angeb13ddd2018-08-20 10:57:36 -0700265
266handle_error:
267 bl psci_get_cpu_id
268 bl psci_get_cpu_stack_top
269 mov x9, #1
270 msr spsel, x9
271 mov sp, x0
272
273 bl plat_error_handler /* Platform specific error handling */
274deadloop:
275 b deadloop /* Never return */
276#endif
277
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800278 .align 11
279 .globl el3_exception_vectors
280el3_exception_vectors:
281 b unhandled_exception /* Sync, Current EL using SP0 */
282 .align 7
283 b unhandled_exception /* IRQ, Current EL using SP0 */
284 .align 7
285 b unhandled_exception /* FIQ, Current EL using SP0 */
286 .align 7
287 b unhandled_exception /* SError, Current EL using SP0 */
288 .align 7
289 b unhandled_exception /* Sync, Current EL using SPx */
290 .align 7
291 b unhandled_exception /* IRQ, Current EL using SPx */
292 .align 7
293 b unhandled_exception /* FIQ, Current EL using SPx */
294 .align 7
295 b unhandled_exception /* SError, Current EL using SPx */
296 .align 7
297 b handle_sync /* Sync, Lower EL using AArch64 */
298 .align 7
299 b unhandled_exception /* IRQ, Lower EL using AArch64 */
300 .align 7
301 b unhandled_exception /* FIQ, Lower EL using AArch64 */
302 .align 7
Chee Hong Angeb13ddd2018-08-20 10:57:36 -0700303#ifdef CONFIG_ARMV8_EA_EL3_FIRST
304 b handle_error /* SError, Lower EL using AArch64 */
305#else
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800306 b unhandled_exception /* SError, Lower EL using AArch64 */
Chee Hong Angeb13ddd2018-08-20 10:57:36 -0700307#endif
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800308 .align 7
309 b unhandled_exception /* Sync, Lower EL using AArch32 */
310 .align 7
311 b unhandled_exception /* IRQ, Lower EL using AArch32 */
312 .align 7
313 b unhandled_exception /* FIQ, Lower EL using AArch32 */
314 .align 7
315 b unhandled_exception /* SError, Lower EL using AArch32 */
316
317ENTRY(psci_setup_vectors)
318 adr x0, el3_exception_vectors
319 msr vbar_el3, x0
320 ret
321ENDPROC(psci_setup_vectors)
322
Tom Rinifc224002022-11-22 12:31:56 -0500323WEAK(psci_arch_init)
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800324 ret
325ENDPROC(psci_arch_init)
macro.wave.z@gmail.com14bf25d2016-12-08 11:58:24 +0800326
327.popsection