2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * Copyright (C) 2012 ARM Limited
13 * Author: Will Deacon <will.deacon@arm.com>
16 #define pr_fmt(fmt) "psci: " fmt
18 #include <linux/init.h>
20 #include <linux/string.h>
22 #include <asm/compiler.h>
23 #include <asm/errno.h>
24 #include <asm/opcodes-sec.h>
25 #include <asm/opcodes-virt.h>
28 struct psci_operations psci_ops;
30 /* Type of psci support. Currently can only be enabled or disabled */
31 #define PSCI_SUP_DISABLED 0
32 #define PSCI_SUP_ENABLED 1
34 static unsigned int psci;
35 static int (*invoke_psci_fn)(u32, u32, u32, u32);
45 static u32 psci_function_id[PSCI_FN_MAX];
47 #define PSCI_RET_SUCCESS 0
48 #define PSCI_RET_EOPNOTSUPP -1
49 #define PSCI_RET_EINVAL -2
50 #define PSCI_RET_EPERM -3
51 #define PSCI_RET_EALREADYON -4
53 static int psci_to_linux_errno(int errno)
56 case PSCI_RET_SUCCESS:
58 case PSCI_RET_EOPNOTSUPP:
64 case PSCI_RET_EALREADYON:
71 #define PSCI_POWER_STATE_ID_MASK 0xffff
72 #define PSCI_POWER_STATE_ID_SHIFT 0
73 #define PSCI_POWER_STATE_TYPE_MASK 0x1
74 #define PSCI_POWER_STATE_TYPE_SHIFT 16
75 #define PSCI_POWER_STATE_AFFL_MASK 0x3
76 #define PSCI_POWER_STATE_AFFL_SHIFT 24
78 static u32 psci_power_state_pack(struct psci_power_state state)
80 return ((state.id & PSCI_POWER_STATE_ID_MASK)
81 << PSCI_POWER_STATE_ID_SHIFT) |
82 ((state.type & PSCI_POWER_STATE_TYPE_MASK)
83 << PSCI_POWER_STATE_TYPE_SHIFT) |
84 ((state.affinity_level & PSCI_POWER_STATE_AFFL_MASK)
85 << PSCI_POWER_STATE_AFFL_SHIFT);
89 * The following two functions are invoked via the invoke_psci_fn pointer
90 * and will not be inlined, allowing us to piggyback on the AAPCS.
92 static noinline int __invoke_psci_fn_hvc(u32 function_id, u32 arg0, u32 arg1,
102 : "r" (arg0), "r" (arg1), "r" (arg2));
107 static noinline int __invoke_psci_fn_smc(u32 function_id, u32 arg0, u32 arg1,
117 : "r" (arg0), "r" (arg1), "r" (arg2));
122 static int psci_cpu_suspend(struct psci_power_state state,
123 unsigned long entry_point)
128 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
129 power_state = psci_power_state_pack(state);
130 err = invoke_psci_fn(fn, power_state, entry_point, 0);
131 return psci_to_linux_errno(err);
134 static int psci_cpu_off(struct psci_power_state state)
139 fn = psci_function_id[PSCI_FN_CPU_OFF];
140 power_state = psci_power_state_pack(state);
141 err = invoke_psci_fn(fn, power_state, 0, 0);
142 return psci_to_linux_errno(err);
145 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
150 fn = psci_function_id[PSCI_FN_CPU_ON];
151 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
152 return psci_to_linux_errno(err);
155 static int psci_migrate(unsigned long cpuid)
160 fn = psci_function_id[PSCI_FN_MIGRATE];
161 err = invoke_psci_fn(fn, cpuid, 0, 0);
162 return psci_to_linux_errno(err);
165 static const struct of_device_id psci_of_match[] __initconst = {
166 { .compatible = "arm,psci", },
170 void __init psci_init(void)
172 struct device_node *np;
176 if (psci == PSCI_SUP_DISABLED)
179 np = of_find_matching_node(NULL, psci_of_match);
183 pr_info("probing function IDs from device-tree\n");
185 if (of_property_read_string(np, "method", &method)) {
186 pr_warning("missing \"method\" property\n");
190 if (!strcmp("hvc", method)) {
191 invoke_psci_fn = __invoke_psci_fn_hvc;
192 } else if (!strcmp("smc", method)) {
193 invoke_psci_fn = __invoke_psci_fn_smc;
195 pr_warning("invalid \"method\" property: %s\n", method);
199 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
200 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
201 psci_ops.cpu_suspend = psci_cpu_suspend;
204 if (!of_property_read_u32(np, "cpu_off", &id)) {
205 psci_function_id[PSCI_FN_CPU_OFF] = id;
206 psci_ops.cpu_off = psci_cpu_off;
209 if (!of_property_read_u32(np, "cpu_on", &id)) {
210 psci_function_id[PSCI_FN_CPU_ON] = id;
211 psci_ops.cpu_on = psci_cpu_on;
214 if (!of_property_read_u32(np, "migrate", &id)) {
215 psci_function_id[PSCI_FN_MIGRATE] = id;
216 psci_ops.migrate = psci_migrate;
224 int __init psci_probe(void)
226 struct device_node *np;
229 if (psci == PSCI_SUP_ENABLED) {
230 np = of_find_matching_node(NULL, psci_of_match);
239 static int __init early_psci(char *val)
243 if (strcmp(val, "enable") == 0)
244 psci = PSCI_SUP_ENABLED;
245 else if (strcmp(val, "disable") == 0)
246 psci = PSCI_SUP_DISABLED;
252 early_param("psci", early_psci);