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>
21 #include <asm/compiler.h>
22 #include <asm/errno.h>
25 struct psci_operations psci_ops
;
27 static int (*invoke_psci_fn
)(u64
, u64
, u64
, u64
);
37 static u32 psci_function_id
[PSCI_FN_MAX
];
39 #define PSCI_RET_SUCCESS 0
40 #define PSCI_RET_EOPNOTSUPP -1
41 #define PSCI_RET_EINVAL -2
42 #define PSCI_RET_EPERM -3
44 static int psci_to_linux_errno(int errno
)
47 case PSCI_RET_SUCCESS
:
49 case PSCI_RET_EOPNOTSUPP
:
60 #define PSCI_POWER_STATE_ID_MASK 0xffff
61 #define PSCI_POWER_STATE_ID_SHIFT 0
62 #define PSCI_POWER_STATE_TYPE_MASK 0x1
63 #define PSCI_POWER_STATE_TYPE_SHIFT 16
64 #define PSCI_POWER_STATE_AFFL_MASK 0x3
65 #define PSCI_POWER_STATE_AFFL_SHIFT 24
67 static u32
psci_power_state_pack(struct psci_power_state state
)
69 return ((state
.id
& PSCI_POWER_STATE_ID_MASK
)
70 << PSCI_POWER_STATE_ID_SHIFT
) |
71 ((state
.type
& PSCI_POWER_STATE_TYPE_MASK
)
72 << PSCI_POWER_STATE_TYPE_SHIFT
) |
73 ((state
.affinity_level
& PSCI_POWER_STATE_AFFL_MASK
)
74 << PSCI_POWER_STATE_AFFL_SHIFT
);
78 * The following two functions are invoked via the invoke_psci_fn pointer
79 * and will not be inlined, allowing us to piggyback on the AAPCS.
81 static noinline
int __invoke_psci_fn_hvc(u64 function_id
, u64 arg0
, u64 arg1
,
91 : "r" (arg0
), "r" (arg1
), "r" (arg2
));
96 static noinline
int __invoke_psci_fn_smc(u64 function_id
, u64 arg0
, u64 arg1
,
106 : "r" (arg0
), "r" (arg1
), "r" (arg2
));
111 static int psci_cpu_suspend(struct psci_power_state state
,
112 unsigned long entry_point
)
117 fn
= psci_function_id
[PSCI_FN_CPU_SUSPEND
];
118 power_state
= psci_power_state_pack(state
);
119 err
= invoke_psci_fn(fn
, power_state
, entry_point
, 0);
120 return psci_to_linux_errno(err
);
123 static int psci_cpu_off(struct psci_power_state state
)
128 fn
= psci_function_id
[PSCI_FN_CPU_OFF
];
129 power_state
= psci_power_state_pack(state
);
130 err
= invoke_psci_fn(fn
, power_state
, 0, 0);
131 return psci_to_linux_errno(err
);
134 static int psci_cpu_on(unsigned long cpuid
, unsigned long entry_point
)
139 fn
= psci_function_id
[PSCI_FN_CPU_ON
];
140 err
= invoke_psci_fn(fn
, cpuid
, entry_point
, 0);
141 return psci_to_linux_errno(err
);
144 static int psci_migrate(unsigned long cpuid
)
149 fn
= psci_function_id
[PSCI_FN_MIGRATE
];
150 err
= invoke_psci_fn(fn
, cpuid
, 0, 0);
151 return psci_to_linux_errno(err
);
154 static const struct of_device_id psci_of_match
[] __initconst
= {
155 { .compatible
= "arm,psci", },
159 int __init
psci_init(void)
161 struct device_node
*np
;
166 np
= of_find_matching_node(NULL
, psci_of_match
);
170 pr_info("probing function IDs from device-tree\n");
172 if (of_property_read_string(np
, "method", &method
)) {
173 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
);
188 if (!of_property_read_u32(np
, "cpu_suspend", &id
)) {
189 psci_function_id
[PSCI_FN_CPU_SUSPEND
] = id
;
190 psci_ops
.cpu_suspend
= psci_cpu_suspend
;
193 if (!of_property_read_u32(np
, "cpu_off", &id
)) {
194 psci_function_id
[PSCI_FN_CPU_OFF
] = id
;
195 psci_ops
.cpu_off
= psci_cpu_off
;
198 if (!of_property_read_u32(np
, "cpu_on", &id
)) {
199 psci_function_id
[PSCI_FN_CPU_ON
] = id
;
200 psci_ops
.cpu_on
= psci_cpu_on
;
203 if (!of_property_read_u32(np
, "migrate", &id
)) {
204 psci_function_id
[PSCI_FN_MIGRATE
] = id
;
205 psci_ops
.migrate
= psci_migrate
;