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
16 #define pr_fmt(fmt) "psci: " fmt
18 #include <linux/init.h>
21 #include <asm/compiler.h>
22 #include <asm/errno.h>
23 #include <asm/opcodes-sec.h>
24 #include <asm/opcodes-virt.h>
27 struct psci_operations psci_ops;
29 static int (*invoke_psci_fn)(u32, u32, u32, u32);
39 static u32 psci_function_id[PSCI_FN_MAX];
41 #define PSCI_RET_SUCCESS 0
42 #define PSCI_RET_EOPNOTSUPP -1
43 #define PSCI_RET_EINVAL -2
44 #define PSCI_RET_EPERM -3
46 static int psci_to_linux_errno(int errno)
49 case PSCI_RET_SUCCESS:
51 case PSCI_RET_EOPNOTSUPP:
62 #define PSCI_POWER_STATE_ID_MASK 0xffff
63 #define PSCI_POWER_STATE_ID_SHIFT 0
64 #define PSCI_POWER_STATE_TYPE_MASK 0x1
65 #define PSCI_POWER_STATE_TYPE_SHIFT 16
66 #define PSCI_POWER_STATE_AFFL_MASK 0x3
67 #define PSCI_POWER_STATE_AFFL_SHIFT 24
69 static u32 psci_power_state_pack(struct psci_power_state state)
71 return ((state.id & PSCI_POWER_STATE_ID_MASK)
72 << PSCI_POWER_STATE_ID_SHIFT) |
73 ((state.type & PSCI_POWER_STATE_TYPE_MASK)
74 << PSCI_POWER_STATE_TYPE_SHIFT) |
75 ((state.affinity_level & PSCI_POWER_STATE_AFFL_MASK)
76 << PSCI_POWER_STATE_AFFL_SHIFT);
80 * The following two functions are invoked via the invoke_psci_fn pointer
81 * and will not be inlined, allowing us to piggyback on the AAPCS.
83 static noinline int __invoke_psci_fn_hvc(u32 function_id, u32 arg0, u32 arg1,
93 : "r" (arg0), "r" (arg1), "r" (arg2));
98 static noinline int __invoke_psci_fn_smc(u32 function_id, u32 arg0, u32 arg1,
108 : "r" (arg0), "r" (arg1), "r" (arg2));
113 static int psci_cpu_suspend(struct psci_power_state state,
114 unsigned long entry_point)
119 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
120 power_state = psci_power_state_pack(state);
121 err = invoke_psci_fn(fn, power_state, entry_point, 0);
122 return psci_to_linux_errno(err);
125 static int psci_cpu_off(struct psci_power_state state)
130 fn = psci_function_id[PSCI_FN_CPU_OFF];
131 power_state = psci_power_state_pack(state);
132 err = invoke_psci_fn(fn, power_state, 0, 0);
133 return psci_to_linux_errno(err);
136 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
141 fn = psci_function_id[PSCI_FN_CPU_ON];
142 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
143 return psci_to_linux_errno(err);
146 static int psci_migrate(unsigned long cpuid)
151 fn = psci_function_id[PSCI_FN_MIGRATE];
152 err = invoke_psci_fn(fn, cpuid, 0, 0);
153 return psci_to_linux_errno(err);
156 static const struct of_device_id psci_of_match[] __initconst = {
157 { .compatible = "arm,psci", },
161 static int __init psci_init(void)
163 struct device_node *np;
167 np = of_find_matching_node(NULL, psci_of_match);
171 pr_info("probing function IDs from device-tree\n");
173 if (of_property_read_string(np, "method", &method)) {
174 pr_warning("missing \"method\" property\n");
178 if (!strcmp("hvc", method)) {
179 invoke_psci_fn = __invoke_psci_fn_hvc;
180 } else if (!strcmp("smc", method)) {
181 invoke_psci_fn = __invoke_psci_fn_smc;
183 pr_warning("invalid \"method\" property: %s\n", method);
187 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
188 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
189 psci_ops.cpu_suspend = psci_cpu_suspend;
192 if (!of_property_read_u32(np, "cpu_off", &id)) {
193 psci_function_id[PSCI_FN_CPU_OFF] = id;
194 psci_ops.cpu_off = psci_cpu_off;
197 if (!of_property_read_u32(np, "cpu_on", &id)) {
198 psci_function_id[PSCI_FN_CPU_ON] = id;
199 psci_ops.cpu_on = psci_cpu_on;
202 if (!of_property_read_u32(np, "migrate", &id)) {
203 psci_function_id[PSCI_FN_MIGRATE] = id;
204 psci_ops.migrate = psci_migrate;
211 early_initcall(psci_init);