2 * Copyright (c) 2014 Robin Randhawa
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This implements support for ARM's Power State Co-ordination Interface
30 * [PSCI]. The implementation adheres to version 0.2 of the PSCI specification
31 * but also supports v0.1. PSCI standardizes operations such as system reset, CPU
32 * on/off/suspend. PSCI requires a compliant firmware implementation.
34 * The PSCI specification used for this implementation is available at:
36 * <http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0022b/index.html>.
39 * - Add support for remaining PSCI calls [this implementation only
40 * supports get_version, system_reset and cpu_on].
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 #include "opt_platform.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/eventhandler.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/reboot.h>
57 #include <machine/bus.h>
58 #include <machine/machdep.h>
61 #include <contrib/dev/acpica/include/acpi.h>
62 #include <dev/acpica/acpivar.h>
66 #include <dev/fdt/fdt_common.h>
67 #include <dev/ofw/openfirm.h>
68 #include <dev/ofw/ofw_bus.h>
69 #include <dev/ofw/ofw_bus_subr.h>
72 #include <dev/psci/psci.h>
77 uint32_t psci_version;
78 uint32_t psci_fnids[PSCI_FN_MAX];
82 static int psci_v0_1_init(device_t dev);
84 static int psci_v0_2_init(device_t dev);
86 struct psci_softc *psci_softc = NULL;
91 #elif defined(__aarch64__)
92 #define USE_ACPI (arm64_bus_method == ARM64_BUS_ACPI)
93 #define USE_FDT (arm64_bus_method == ARM64_BUS_FDT)
95 #error Unknown architecture
99 static struct ofw_compat_data compat_data[] = {
100 {"arm,psci-1.0", (uintptr_t)psci_v0_2_init},
101 {"arm,psci-0.2", (uintptr_t)psci_v0_2_init},
102 {"arm,psci", (uintptr_t)psci_v0_1_init},
107 static int psci_attach(device_t, psci_initfn_t);
108 static void psci_shutdown(void *, int);
110 static int psci_find_callfn(psci_callfn_t *);
111 static int psci_def_callfn(register_t, register_t, register_t, register_t);
113 psci_callfn_t psci_callfn = psci_def_callfn;
116 psci_init(void *dummy)
118 psci_callfn_t new_callfn;
120 if (psci_find_callfn(&new_callfn) != PSCI_RETVAL_SUCCESS) {
121 printf("No PSCI/SMCCC call function found\n");
125 psci_callfn = new_callfn;
127 /* This needs to be before cpu_mp at SI_SUB_CPU, SI_ORDER_THIRD */
128 SYSINIT(psci_start, SI_SUB_CPU, SI_ORDER_FIRST, psci_init, NULL);
131 psci_def_callfn(register_t a __unused, register_t b __unused,
132 register_t c __unused, register_t d __unused)
135 panic("No PSCI/SMCCC call function set");
139 static int psci_fdt_probe(device_t dev);
140 static int psci_fdt_attach(device_t dev);
142 static device_method_t psci_fdt_methods[] = {
143 DEVMETHOD(device_probe, psci_fdt_probe),
144 DEVMETHOD(device_attach, psci_fdt_attach),
149 static driver_t psci_fdt_driver = {
152 sizeof(struct psci_softc),
155 static devclass_t psci_fdt_devclass;
157 EARLY_DRIVER_MODULE(psci, simplebus, psci_fdt_driver, psci_fdt_devclass, 0, 0,
158 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
159 EARLY_DRIVER_MODULE(psci, ofwbus, psci_fdt_driver, psci_fdt_devclass, 0, 0,
160 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
163 psci_fdt_get_callfn(phandle_t node)
167 if ((OF_getprop(node, "method", method, sizeof(method))) > 0) {
168 if (strcmp(method, "hvc") == 0)
169 return (psci_hvc_despatch);
170 else if (strcmp(method, "smc") == 0)
171 return (psci_smc_despatch);
173 printf("psci: PSCI conduit \"%s\" invalid\n", method);
175 printf("psci: PSCI conduit not supplied in the device tree\n");
181 psci_fdt_probe(device_t dev)
183 const struct ofw_compat_data *ocd;
185 if (!ofw_bus_status_okay(dev))
188 ocd = ofw_bus_search_compatible(dev, compat_data);
189 if (ocd->ocd_str == NULL)
192 device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
194 return (BUS_PROBE_SPECIFIC);
198 psci_fdt_attach(device_t dev)
200 const struct ofw_compat_data *ocd;
201 psci_initfn_t psci_init;
203 ocd = ofw_bus_search_compatible(dev, compat_data);
204 psci_init = (psci_initfn_t)ocd->ocd_data;
206 return (psci_attach(dev, psci_init));
211 static void psci_acpi_identify(driver_t *, device_t);
212 static int psci_acpi_probe(device_t);
213 static int psci_acpi_attach(device_t);
215 static device_method_t psci_acpi_methods[] = {
216 /* Device interface */
217 DEVMETHOD(device_identify, psci_acpi_identify),
218 DEVMETHOD(device_probe, psci_acpi_probe),
219 DEVMETHOD(device_attach, psci_acpi_attach),
224 static driver_t psci_acpi_driver = {
227 sizeof(struct psci_softc),
230 static devclass_t psci_acpi_devclass;
232 EARLY_DRIVER_MODULE(psci, acpi, psci_acpi_driver, psci_acpi_devclass, 0, 0,
233 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
236 psci_acpi_bootflags(void)
238 ACPI_TABLE_FADT *fadt;
242 physaddr = acpi_find_table(ACPI_SIG_FADT);
246 fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
248 printf("psci: Unable to map the FADT\n");
252 flags = fadt->ArmBootFlags;
254 acpi_unmap_table(fadt);
259 psci_acpi_get_callfn(int flags)
262 if ((flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
263 if ((flags & ACPI_FADT_PSCI_USE_HVC) != 0)
264 return (psci_hvc_despatch);
266 return (psci_smc_despatch);
268 printf("psci: PSCI conduit not supplied in the device tree\n");
275 psci_acpi_identify(driver_t *driver, device_t parent)
280 flags = psci_acpi_bootflags();
281 if ((flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
282 dev = BUS_ADD_CHILD(parent,
283 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST, "psci", -1);
286 acpi_set_private(dev, (void *)(uintptr_t)flags);
291 psci_acpi_probe(device_t dev)
295 flags = (uintptr_t)acpi_get_private(dev);
296 if ((flags & ACPI_FADT_PSCI_COMPLIANT) == 0)
299 device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
300 return (BUS_PROBE_SPECIFIC);
304 psci_acpi_attach(device_t dev)
307 return (psci_attach(dev, psci_v0_2_init));
312 psci_attach(device_t dev, psci_initfn_t psci_init)
314 struct psci_softc *sc = device_get_softc(dev);
316 if (psci_softc != NULL)
319 KASSERT(psci_init != NULL, ("PSCI init function cannot be NULL"));
329 _psci_get_version(struct psci_softc *sc)
333 /* PSCI version wasn't supported in v0.1. */
334 fnid = sc->psci_fnids[PSCI_FN_VERSION];
336 return (psci_call(fnid, 0, 0, 0));
338 return (PSCI_RETVAL_NOT_SUPPORTED);
342 psci_get_version(void)
345 if (psci_softc == NULL)
346 return (PSCI_RETVAL_NOT_SUPPORTED);
347 return (_psci_get_version(psci_softc));
352 psci_fdt_callfn(psci_callfn_t *callfn)
356 node = ofw_bus_find_compatible(OF_peer(0), "arm,psci-0.2");
358 node = ofw_bus_find_compatible(OF_peer(0), "arm,psci-1.0");
360 return (PSCI_MISSING);
363 *callfn = psci_fdt_get_callfn(node);
370 psci_acpi_callfn(psci_callfn_t *callfn)
374 flags = psci_acpi_bootflags();
375 if ((flags & ACPI_FADT_PSCI_COMPLIANT) == 0)
376 return (PSCI_MISSING);
378 *callfn = psci_acpi_get_callfn(flags);
384 psci_find_callfn(psci_callfn_t *callfn)
391 error = psci_fdt_callfn(callfn);
397 if (*callfn == NULL && USE_ACPI) {
398 error = psci_acpi_callfn(callfn);
405 return (PSCI_MISSING);
407 return (PSCI_RETVAL_SUCCESS);
411 psci_features(uint32_t psci_func_id)
414 if (psci_softc == NULL)
415 return (PSCI_RETVAL_NOT_SUPPORTED);
417 /* The feature flags were added to PSCI 1.0 */
418 if (PSCI_VER_MAJOR(psci_softc->psci_version) < 1)
419 return (PSCI_RETVAL_NOT_SUPPORTED);
421 return (psci_call(PSCI_FNID_FEATURES, psci_func_id, 0, 0));
425 psci_cpu_on(unsigned long cpu, unsigned long entry, unsigned long context_id)
429 fnid = PSCI_FNID_CPU_ON;
430 if (psci_softc != NULL)
431 fnid = psci_softc->psci_fnids[PSCI_FN_CPU_ON];
433 /* PSCI v0.1 and v0.2 both support cpu_on. */
434 return (psci_call(fnid, cpu, entry, context_id));
438 psci_shutdown(void *xsc, int howto)
442 if (psci_softc == NULL)
445 /* PSCI system_off and system_reset werent't supported in v0.1. */
446 if ((howto & RB_POWEROFF) != 0)
447 fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_OFF];
448 else if ((howto & RB_HALT) == 0)
449 fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_RESET];
452 psci_call(fn, 0, 0, 0);
454 /* System reset and off do not return. */
461 psci_shutdown(NULL, 0);
465 /* Only support PSCI 0.1 on FDT */
467 psci_v0_1_init(device_t dev)
469 struct psci_softc *sc = device_get_softc(dev);
476 /* Zero out the function ID table - Is this needed ? */
477 for (psci_fn = PSCI_FN_VERSION, psci_fnid = PSCI_FNID_VERSION;
478 psci_fn < PSCI_FN_MAX; psci_fn++, psci_fnid++)
479 sc->psci_fnids[psci_fn] = 0;
481 /* PSCI v0.1 doesn't specify function IDs. Get them from DT */
482 node = ofw_bus_get_node(dev);
484 if ((len = OF_getproplen(node, "cpu_suspend")) > 0) {
485 OF_getencprop(node, "cpu_suspend", &psci_fnid, len);
486 sc->psci_fnids[PSCI_FN_CPU_SUSPEND] = psci_fnid;
489 if ((len = OF_getproplen(node, "cpu_on")) > 0) {
490 OF_getencprop(node, "cpu_on", &psci_fnid, len);
491 sc->psci_fnids[PSCI_FN_CPU_ON] = psci_fnid;
494 if ((len = OF_getproplen(node, "cpu_off")) > 0) {
495 OF_getencprop(node, "cpu_off", &psci_fnid, len);
496 sc->psci_fnids[PSCI_FN_CPU_OFF] = psci_fnid;
499 if ((len = OF_getproplen(node, "migrate")) > 0) {
500 OF_getencprop(node, "migrate", &psci_fnid, len);
501 sc->psci_fnids[PSCI_FN_MIGRATE] = psci_fnid;
504 sc->psci_version = (0 << 16) | 1;
506 device_printf(dev, "PSCI version 0.1 available\n");
513 psci_v0_2_init(device_t dev)
515 struct psci_softc *sc = device_get_softc(dev);
518 /* PSCI v0.2 specifies explicit function IDs. */
519 sc->psci_fnids[PSCI_FN_VERSION] = PSCI_FNID_VERSION;
520 sc->psci_fnids[PSCI_FN_CPU_SUSPEND] = PSCI_FNID_CPU_SUSPEND;
521 sc->psci_fnids[PSCI_FN_CPU_OFF] = PSCI_FNID_CPU_OFF;
522 sc->psci_fnids[PSCI_FN_CPU_ON] = PSCI_FNID_CPU_ON;
523 sc->psci_fnids[PSCI_FN_AFFINITY_INFO] = PSCI_FNID_AFFINITY_INFO;
524 sc->psci_fnids[PSCI_FN_MIGRATE] = PSCI_FNID_MIGRATE;
525 sc->psci_fnids[PSCI_FN_MIGRATE_INFO_TYPE] = PSCI_FNID_MIGRATE_INFO_TYPE;
526 sc->psci_fnids[PSCI_FN_MIGRATE_INFO_UP_CPU] = PSCI_FNID_MIGRATE_INFO_UP_CPU;
527 sc->psci_fnids[PSCI_FN_SYSTEM_OFF] = PSCI_FNID_SYSTEM_OFF;
528 sc->psci_fnids[PSCI_FN_SYSTEM_RESET] = PSCI_FNID_SYSTEM_RESET;
530 version = _psci_get_version(sc);
532 if (version == PSCI_RETVAL_NOT_SUPPORTED)
535 sc->psci_version = version;
536 if ((PSCI_VER_MAJOR(version) == 0 && PSCI_VER_MINOR(version) == 2) ||
537 PSCI_VER_MAJOR(version) == 1) {
539 device_printf(dev, "PSCI version 0.2 compatible\n");
542 * We only register this for v0.2 since v0.1 doesn't support
545 EVENTHANDLER_REGISTER(shutdown_final, psci_shutdown, sc,
551 device_printf(dev, "PSCI version number mismatched with DT\n");