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) 2013 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/smp.h>
22 #include <asm/compiler.h>
23 #include <asm/errno.h>
25 #include <asm/smp_plat.h>
27 #define PSCI_POWER_STATE_TYPE_STANDBY 0
28 #define PSCI_POWER_STATE_TYPE_POWER_DOWN 1
30 struct psci_power_state {
36 struct psci_operations {
37 int (*cpu_suspend)(struct psci_power_state state,
38 unsigned long entry_point);
39 int (*cpu_off)(struct psci_power_state state);
40 int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
41 int (*migrate)(unsigned long cpuid);
44 static struct psci_operations psci_ops;
46 static int (*invoke_psci_fn)(u64, u64, u64, u64);
56 static u32 psci_function_id[PSCI_FN_MAX];
58 #define PSCI_RET_SUCCESS 0
59 #define PSCI_RET_EOPNOTSUPP -1
60 #define PSCI_RET_EINVAL -2
61 #define PSCI_RET_EPERM -3
63 static int psci_to_linux_errno(int errno)
66 case PSCI_RET_SUCCESS:
68 case PSCI_RET_EOPNOTSUPP:
79 #define PSCI_POWER_STATE_ID_MASK 0xffff
80 #define PSCI_POWER_STATE_ID_SHIFT 0
81 #define PSCI_POWER_STATE_TYPE_MASK 0x1
82 #define PSCI_POWER_STATE_TYPE_SHIFT 16
83 #define PSCI_POWER_STATE_AFFL_MASK 0x3
84 #define PSCI_POWER_STATE_AFFL_SHIFT 24
86 static u32 psci_power_state_pack(struct psci_power_state state)
88 return ((state.id & PSCI_POWER_STATE_ID_MASK)
89 << PSCI_POWER_STATE_ID_SHIFT) |
90 ((state.type & PSCI_POWER_STATE_TYPE_MASK)
91 << PSCI_POWER_STATE_TYPE_SHIFT) |
92 ((state.affinity_level & PSCI_POWER_STATE_AFFL_MASK)
93 << PSCI_POWER_STATE_AFFL_SHIFT);
97 * The following two functions are invoked via the invoke_psci_fn pointer
98 * and will not be inlined, allowing us to piggyback on the AAPCS.
100 static noinline int __invoke_psci_fn_hvc(u64 function_id, u64 arg0, u64 arg1,
110 : "r" (arg0), "r" (arg1), "r" (arg2));
115 static noinline int __invoke_psci_fn_smc(u64 function_id, u64 arg0, u64 arg1,
125 : "r" (arg0), "r" (arg1), "r" (arg2));
130 static int psci_cpu_suspend(struct psci_power_state state,
131 unsigned long entry_point)
136 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
137 power_state = psci_power_state_pack(state);
138 err = invoke_psci_fn(fn, power_state, entry_point, 0);
139 return psci_to_linux_errno(err);
142 static int psci_cpu_off(struct psci_power_state state)
147 fn = psci_function_id[PSCI_FN_CPU_OFF];
148 power_state = psci_power_state_pack(state);
149 err = invoke_psci_fn(fn, power_state, 0, 0);
150 return psci_to_linux_errno(err);
153 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
158 fn = psci_function_id[PSCI_FN_CPU_ON];
159 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
160 return psci_to_linux_errno(err);
163 static int psci_migrate(unsigned long cpuid)
168 fn = psci_function_id[PSCI_FN_MIGRATE];
169 err = invoke_psci_fn(fn, cpuid, 0, 0);
170 return psci_to_linux_errno(err);
173 static const struct of_device_id psci_of_match[] __initconst = {
174 { .compatible = "arm,psci", },
178 int __init psci_init(void)
180 struct device_node *np;
185 np = of_find_matching_node(NULL, psci_of_match);
189 pr_info("probing function IDs from device-tree\n");
191 if (of_property_read_string(np, "method", &method)) {
192 pr_warning("missing \"method\" property\n");
197 if (!strcmp("hvc", method)) {
198 invoke_psci_fn = __invoke_psci_fn_hvc;
199 } else if (!strcmp("smc", method)) {
200 invoke_psci_fn = __invoke_psci_fn_smc;
202 pr_warning("invalid \"method\" property: %s\n", method);
207 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
208 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
209 psci_ops.cpu_suspend = psci_cpu_suspend;
212 if (!of_property_read_u32(np, "cpu_off", &id)) {
213 psci_function_id[PSCI_FN_CPU_OFF] = id;
214 psci_ops.cpu_off = psci_cpu_off;
217 if (!of_property_read_u32(np, "cpu_on", &id)) {
218 psci_function_id[PSCI_FN_CPU_ON] = id;
219 psci_ops.cpu_on = psci_cpu_on;
222 if (!of_property_read_u32(np, "migrate", &id)) {
223 psci_function_id[PSCI_FN_MIGRATE] = id;
224 psci_ops.migrate = psci_migrate;
234 static int __init smp_psci_init_cpu(struct device_node *dn, int cpu)
239 static int __init smp_psci_prepare_cpu(int cpu)
243 if (!psci_ops.cpu_on) {
244 pr_err("no cpu_on method, not booting CPU%d\n", cpu);
248 err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_holding_pen));
250 pr_err("failed to boot CPU%d (%d)\n", cpu, err);
257 const struct smp_enable_ops smp_psci_ops __initconst = {
259 .init_cpu = smp_psci_init_cpu,
260 .prepare_cpu = smp_psci_prepare_cpu,