xref: /freebsd/sys/dev/psci/psci.c (revision 3e19edca9eb1402afe330cb1d9d562dedca2d95a)
1 /*-
2  * Copyright (c) 2014 Robin Randhawa
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27 
28 /*
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.
33  *
34  * The PSCI specification used for this implementation is available at:
35  *
36  * <http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0022b/index.html>.
37  *
38  * TODO:
39  * - Add support for remaining PSCI calls [this implementation only
40  *   supports get_version, system_reset and cpu_on].
41  */
42 
43 #include <sys/cdefs.h>
44 #include "opt_acpi.h"
45 #include "opt_platform.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/bus.h>
50 #include <sys/eventhandler.h>
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/reboot.h>
54 
55 #include <machine/bus.h>
56 #include <machine/machdep.h>
57 
58 #ifdef DEV_ACPI
59 #include <contrib/dev/acpica/include/acpi.h>
60 #include <dev/acpica/acpivar.h>
61 #endif
62 
63 #ifdef FDT
64 #include <dev/fdt/fdt_common.h>
65 #include <dev/ofw/openfirm.h>
66 #include <dev/ofw/ofw_bus.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68 #endif
69 
70 #include <dev/psci/psci.h>
71 
72 struct psci_softc {
73 	device_t        dev;
74 	device_t	smccc_dev;
75 
76 	uint32_t	psci_version;
77 	uint32_t	psci_fnids[PSCI_FN_MAX];
78 };
79 
80 #ifdef FDT
81 static int psci_v0_1_init(device_t dev, int default_version);
82 #endif
83 static int psci_v0_2_init(device_t dev, int default_version);
84 
85 struct psci_softc *psci_softc = NULL;
86 bool psci_present;
87 
88 #ifdef __arm__
89 #define	USE_ACPI	0
90 #define	USE_FDT		1
91 #elif defined(__aarch64__)
92 #define	USE_ACPI	(arm64_bus_method == ARM64_BUS_ACPI)
93 #define	USE_FDT		(arm64_bus_method == ARM64_BUS_FDT)
94 #else
95 #error Unknown architecture
96 #endif
97 
98 #ifdef FDT
99 struct psci_init_def {
100 	int		default_version;
101 	psci_initfn_t	psci_init;
102 };
103 
104 static struct psci_init_def psci_v1_0_init_def = {
105 	.default_version = (1 << 16) | 0,
106 	.psci_init = psci_v0_2_init
107 };
108 
109 static struct psci_init_def psci_v0_2_init_def = {
110 	.default_version = (0 << 16) | 2,
111 	.psci_init = psci_v0_2_init
112 };
113 
114 static struct psci_init_def psci_v0_1_init_def = {
115 	.default_version = (0 << 16) | 1,
116 	.psci_init = psci_v0_1_init
117 };
118 
119 static struct ofw_compat_data compat_data[] = {
120 	{"arm,psci-1.0",        (uintptr_t)&psci_v1_0_init_def},
121 	{"arm,psci-0.2",        (uintptr_t)&psci_v0_2_init_def},
122 	{"arm,psci",            (uintptr_t)&psci_v0_1_init_def},
123 	{NULL,                  0}
124 };
125 #endif
126 
127 static int psci_attach(device_t, psci_initfn_t, int);
128 
129 static int psci_find_callfn(psci_callfn_t *);
130 static int psci_def_callfn(register_t, register_t, register_t, register_t,
131 	register_t, register_t, register_t, register_t,
132 	struct arm_smccc_res *res);
133 
134 psci_callfn_t psci_callfn = psci_def_callfn;
135 
136 static void
psci_init(void * dummy)137 psci_init(void *dummy)
138 {
139 	psci_callfn_t new_callfn;
140 
141 	if (psci_find_callfn(&new_callfn) != PSCI_RETVAL_SUCCESS) {
142 		printf("No PSCI/SMCCC call function found\n");
143 		return;
144 	}
145 
146 	psci_callfn = new_callfn;
147 	psci_present = true;
148 }
149 /* This needs to be before cpu_mp at SI_SUB_CPU, SI_ORDER_THIRD */
150 SYSINIT(psci_start, SI_SUB_CPU, SI_ORDER_FIRST, psci_init, NULL);
151 
152 static int
psci_def_callfn(register_t a __unused,register_t b __unused,register_t c __unused,register_t d __unused,register_t e __unused,register_t f __unused,register_t g __unused,register_t h __unused,struct arm_smccc_res * res __unused)153 psci_def_callfn(register_t a __unused, register_t b __unused,
154     register_t c __unused, register_t d __unused,
155     register_t e __unused, register_t f __unused,
156     register_t g __unused, register_t h __unused,
157     struct arm_smccc_res *res __unused)
158 {
159 
160 	panic("No PSCI/SMCCC call function set");
161 }
162 
163 #ifdef FDT
164 static int psci_fdt_probe(device_t dev);
165 static int psci_fdt_attach(device_t dev);
166 
167 static device_method_t psci_fdt_methods[] = {
168 	DEVMETHOD(device_probe,     psci_fdt_probe),
169 	DEVMETHOD(device_attach,    psci_fdt_attach),
170 
171 	DEVMETHOD_END
172 };
173 
174 static driver_t psci_fdt_driver = {
175 	"psci",
176 	psci_fdt_methods,
177 	sizeof(struct psci_softc),
178 };
179 
180 EARLY_DRIVER_MODULE(psci, simplebus, psci_fdt_driver, 0, 0,
181     BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
182 EARLY_DRIVER_MODULE(psci, ofwbus, psci_fdt_driver, 0, 0,
183     BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
184 
185 static psci_callfn_t
psci_fdt_get_callfn(phandle_t node)186 psci_fdt_get_callfn(phandle_t node)
187 {
188 	char method[16];
189 
190 	if ((OF_getprop(node, "method", method, sizeof(method))) > 0) {
191 		if (strcmp(method, "hvc") == 0)
192 			return (arm_smccc_hvc);
193 		else if (strcmp(method, "smc") == 0)
194 			return (arm_smccc_smc);
195 		else
196 			printf("psci: PSCI conduit \"%s\" invalid\n", method);
197 	} else
198 		printf("psci: PSCI conduit not supplied in the device tree\n");
199 
200 	return (NULL);
201 }
202 
203 static int
psci_fdt_probe(device_t dev)204 psci_fdt_probe(device_t dev)
205 {
206 	const struct ofw_compat_data *ocd;
207 
208 	if (!ofw_bus_status_okay(dev))
209 		return (ENXIO);
210 
211 	ocd = ofw_bus_search_compatible(dev, compat_data);
212 	if (ocd->ocd_str == NULL)
213 		return (ENXIO);
214 
215 	device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
216 
217 	return (BUS_PROBE_SPECIFIC);
218 }
219 
220 static int
psci_fdt_attach(device_t dev)221 psci_fdt_attach(device_t dev)
222 {
223 	const struct ofw_compat_data *ocd;
224 	struct psci_init_def *psci_init_def;
225 
226 	ocd = ofw_bus_search_compatible(dev, compat_data);
227 	psci_init_def = (struct psci_init_def *)ocd->ocd_data;
228 
229 	return (psci_attach(dev, psci_init_def->psci_init,
230 	    psci_init_def->default_version));
231 }
232 #endif
233 
234 #ifdef DEV_ACPI
235 static void psci_acpi_identify(driver_t *, device_t);
236 static int psci_acpi_probe(device_t);
237 static int psci_acpi_attach(device_t);
238 
239 static device_method_t psci_acpi_methods[] = {
240 	/* Device interface */
241 	DEVMETHOD(device_identify,	psci_acpi_identify),
242 	DEVMETHOD(device_probe,		psci_acpi_probe),
243 	DEVMETHOD(device_attach,	psci_acpi_attach),
244 
245 	DEVMETHOD_END
246 };
247 
248 static driver_t psci_acpi_driver = {
249 	"psci",
250 	psci_acpi_methods,
251 	sizeof(struct psci_softc),
252 };
253 
254 EARLY_DRIVER_MODULE(psci, acpi, psci_acpi_driver, 0, 0,
255     BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
256 
257 static int
psci_acpi_bootflags(void)258 psci_acpi_bootflags(void)
259 {
260 	ACPI_TABLE_FADT *fadt;
261 	vm_paddr_t physaddr;
262 	int flags;
263 
264 	physaddr = acpi_find_table(ACPI_SIG_FADT);
265 	if (physaddr == 0)
266 		return (0);
267 
268 	fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
269 	if (fadt == NULL) {
270 		printf("psci: Unable to map the FADT\n");
271 		return (0);
272 	}
273 
274 	flags = fadt->ArmBootFlags;
275 
276 	acpi_unmap_table(fadt);
277 	return (flags);
278 }
279 
280 static psci_callfn_t
psci_acpi_get_callfn(int flags)281 psci_acpi_get_callfn(int flags)
282 {
283 
284 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
285 		if ((flags & ACPI_FADT_PSCI_USE_HVC) != 0)
286 			return (arm_smccc_hvc);
287 		else
288 			return (arm_smccc_smc);
289 	} else {
290 		printf("psci: PSCI conduit not supplied in the device tree\n");
291 	}
292 
293 	return (NULL);
294 }
295 
296 static void
psci_acpi_identify(driver_t * driver,device_t parent)297 psci_acpi_identify(driver_t *driver, device_t parent)
298 {
299 	device_t dev;
300 	int flags;
301 
302 	flags = psci_acpi_bootflags();
303 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
304 		dev = BUS_ADD_CHILD(parent,
305 		    BUS_PASS_CPU + BUS_PASS_ORDER_FIRST, "psci", -1);
306 
307 		if (dev != NULL)
308 			acpi_set_private(dev, (void *)(uintptr_t)flags);
309 	}
310 }
311 
312 static int
psci_acpi_probe(device_t dev)313 psci_acpi_probe(device_t dev)
314 {
315 	uintptr_t flags;
316 
317 	flags = (uintptr_t)acpi_get_private(dev);
318 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) == 0)
319 		return (ENXIO);
320 
321 	device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
322 	return (BUS_PROBE_SPECIFIC);
323 }
324 
325 static int
psci_acpi_attach(device_t dev)326 psci_acpi_attach(device_t dev)
327 {
328 
329 	return (psci_attach(dev, psci_v0_2_init, PSCI_RETVAL_NOT_SUPPORTED));
330 }
331 #endif
332 
333 static int
psci_attach(device_t dev,psci_initfn_t psci_init,int default_version)334 psci_attach(device_t dev, psci_initfn_t psci_init, int default_version)
335 {
336 	struct psci_softc *sc = device_get_softc(dev);
337 
338 	if (psci_softc != NULL)
339 		return (ENXIO);
340 
341 	KASSERT(psci_init != NULL, ("PSCI init function cannot be NULL"));
342 	if (psci_init(dev, default_version))
343 		return (ENXIO);
344 
345 	psci_softc = sc;
346 
347 #ifdef __aarch64__
348 	smccc_init();
349 	sc->smccc_dev = device_add_child(dev, "smccc", DEVICE_UNIT_ANY);
350 	if (sc->smccc_dev == NULL)
351 		device_printf(dev, "Unable to add SMCCC device\n");
352 
353 	bus_generic_attach(dev);
354 #endif
355 
356 	return (0);
357 }
358 
359 static int
_psci_get_version(struct psci_softc * sc)360 _psci_get_version(struct psci_softc *sc)
361 {
362 	uint32_t fnid;
363 
364 	/* PSCI version wasn't supported in v0.1. */
365 	fnid = sc->psci_fnids[PSCI_FN_VERSION];
366 	if (fnid)
367 		return (psci_call(fnid, 0, 0, 0));
368 
369 	return (PSCI_RETVAL_NOT_SUPPORTED);
370 }
371 
372 int
psci_get_version(void)373 psci_get_version(void)
374 {
375 
376 	if (psci_softc == NULL)
377 		return (PSCI_RETVAL_NOT_SUPPORTED);
378 	return (_psci_get_version(psci_softc));
379 }
380 
381 #ifdef FDT
382 static int
psci_fdt_callfn(psci_callfn_t * callfn)383 psci_fdt_callfn(psci_callfn_t *callfn)
384 {
385 	phandle_t node;
386 
387 	/* XXX: This is suboptimal, we should walk the tree & check each
388 	 * node against compat_data, but we only have a few entries so
389 	 * it's ok for now.
390 	 */
391 	for (int i = 0; compat_data[i].ocd_str != NULL; i++) {
392 		node = ofw_bus_find_compatible(OF_peer(0),
393 		    compat_data[i].ocd_str);
394 		if (node != 0)
395 			break;
396 	}
397 	if (node == 0)
398 		return (PSCI_MISSING);
399 
400 	if (!ofw_bus_node_status_okay(node))
401 		return (PSCI_MISSING);
402 
403 	*callfn = psci_fdt_get_callfn(node);
404 	return (0);
405 }
406 #endif
407 
408 #ifdef DEV_ACPI
409 static int
psci_acpi_callfn(psci_callfn_t * callfn)410 psci_acpi_callfn(psci_callfn_t *callfn)
411 {
412 	int flags;
413 
414 	flags = psci_acpi_bootflags();
415 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) == 0)
416 		return (PSCI_MISSING);
417 
418 	*callfn = psci_acpi_get_callfn(flags);
419 	return (0);
420 }
421 #endif
422 
423 static int
psci_find_callfn(psci_callfn_t * callfn)424 psci_find_callfn(psci_callfn_t *callfn)
425 {
426 	int error;
427 
428 	*callfn = NULL;
429 #ifdef FDT
430 	if (USE_FDT) {
431 		error = psci_fdt_callfn(callfn);
432 		if (error != 0)
433 			return (error);
434 	}
435 #endif
436 #ifdef DEV_ACPI
437 	if (*callfn == NULL && USE_ACPI) {
438 		error = psci_acpi_callfn(callfn);
439 		if (error != 0)
440 			return (error);
441 	}
442 #endif
443 
444 	if (*callfn == NULL)
445 		return (PSCI_MISSING);
446 
447 	return (PSCI_RETVAL_SUCCESS);
448 }
449 
450 int32_t
psci_features(uint32_t psci_func_id)451 psci_features(uint32_t psci_func_id)
452 {
453 
454 	if (psci_softc == NULL)
455 		return (PSCI_RETVAL_NOT_SUPPORTED);
456 
457 	/* The feature flags were added to PSCI 1.0 */
458 	if (PSCI_VER_MAJOR(psci_softc->psci_version) < 1)
459 		return (PSCI_RETVAL_NOT_SUPPORTED);
460 
461 	return (psci_call(PSCI_FNID_FEATURES, psci_func_id, 0, 0));
462 }
463 
464 int
psci_cpu_on(unsigned long cpu,unsigned long entry,unsigned long context_id)465 psci_cpu_on(unsigned long cpu, unsigned long entry, unsigned long context_id)
466 {
467 	uint32_t fnid;
468 
469 	fnid = PSCI_FNID_CPU_ON;
470 	if (psci_softc != NULL)
471 		fnid = psci_softc->psci_fnids[PSCI_FN_CPU_ON];
472 
473 	/* PSCI v0.1 and v0.2 both support cpu_on. */
474 	return (psci_call(fnid, cpu, entry, context_id));
475 }
476 
477 static void
psci_shutdown(void * xsc,int howto)478 psci_shutdown(void *xsc, int howto)
479 {
480 	uint32_t fn = 0;
481 
482 	if (psci_softc == NULL)
483 		return;
484 
485 	if ((howto & RB_POWEROFF) != 0)
486 		fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_OFF];
487 	if (fn)
488 		psci_call(fn, 0, 0, 0);
489 
490 	/* System reset and off do not return. */
491 }
492 
493 static void
psci_reboot(void * xsc,int howto)494 psci_reboot(void *xsc, int howto)
495 {
496 	uint32_t fn = 0;
497 
498 	if (psci_softc == NULL)
499 		return;
500 
501 	if ((howto & RB_HALT) == 0)
502 		fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_RESET];
503 	if (fn)
504 		psci_call(fn, 0, 0, 0);
505 
506 	/* System reset and off do not return. */
507 }
508 
509 void
psci_reset(void)510 psci_reset(void)
511 {
512 
513 	psci_reboot(NULL, 0);
514 }
515 
516 #ifdef FDT
517 /* Only support PSCI 0.1 on FDT */
518 static int
psci_v0_1_init(device_t dev,int default_version __unused)519 psci_v0_1_init(device_t dev, int default_version __unused)
520 {
521 	struct psci_softc *sc = device_get_softc(dev);
522 	int psci_fn;
523 	uint32_t psci_fnid;
524 	phandle_t node;
525 	int len;
526 
527 	/* Zero out the function ID table - Is this needed ? */
528 	for (psci_fn = PSCI_FN_VERSION, psci_fnid = PSCI_FNID_VERSION;
529 	    psci_fn < PSCI_FN_MAX; psci_fn++, psci_fnid++)
530 		sc->psci_fnids[psci_fn] = 0;
531 
532 	/* PSCI v0.1 doesn't specify function IDs. Get them from DT */
533 	node = ofw_bus_get_node(dev);
534 
535 	if ((len = OF_getproplen(node, "cpu_suspend")) > 0) {
536 		OF_getencprop(node, "cpu_suspend", &psci_fnid, len);
537 		sc->psci_fnids[PSCI_FN_CPU_SUSPEND] = psci_fnid;
538 	}
539 
540 	if ((len = OF_getproplen(node, "cpu_on")) > 0) {
541 		OF_getencprop(node, "cpu_on", &psci_fnid, len);
542 		sc->psci_fnids[PSCI_FN_CPU_ON] = psci_fnid;
543 	}
544 
545 	if ((len = OF_getproplen(node, "cpu_off")) > 0) {
546 		OF_getencprop(node, "cpu_off", &psci_fnid, len);
547 		sc->psci_fnids[PSCI_FN_CPU_OFF] = psci_fnid;
548 	}
549 
550 	if ((len = OF_getproplen(node, "migrate")) > 0) {
551 		OF_getencprop(node, "migrate", &psci_fnid, len);
552 		sc->psci_fnids[PSCI_FN_MIGRATE] = psci_fnid;
553 	}
554 
555 	sc->psci_version = (0 << 16) | 1;
556 	if (bootverbose)
557 		device_printf(dev, "PSCI version 0.1 available\n");
558 
559 	return(0);
560 }
561 #endif
562 
563 static int
psci_v0_2_init(device_t dev,int default_version)564 psci_v0_2_init(device_t dev, int default_version)
565 {
566 	struct psci_softc *sc = device_get_softc(dev);
567 	int version;
568 
569 	/* PSCI v0.2 specifies explicit function IDs. */
570 	sc->psci_fnids[PSCI_FN_VERSION]		    = PSCI_FNID_VERSION;
571 	sc->psci_fnids[PSCI_FN_CPU_SUSPEND]	    = PSCI_FNID_CPU_SUSPEND;
572 	sc->psci_fnids[PSCI_FN_CPU_OFF]		    = PSCI_FNID_CPU_OFF;
573 	sc->psci_fnids[PSCI_FN_CPU_ON]		    = PSCI_FNID_CPU_ON;
574 	sc->psci_fnids[PSCI_FN_AFFINITY_INFO]	    = PSCI_FNID_AFFINITY_INFO;
575 	sc->psci_fnids[PSCI_FN_MIGRATE]		    = PSCI_FNID_MIGRATE;
576 	sc->psci_fnids[PSCI_FN_MIGRATE_INFO_TYPE]   = PSCI_FNID_MIGRATE_INFO_TYPE;
577 	sc->psci_fnids[PSCI_FN_MIGRATE_INFO_UP_CPU] = PSCI_FNID_MIGRATE_INFO_UP_CPU;
578 	sc->psci_fnids[PSCI_FN_SYSTEM_OFF]	    = PSCI_FNID_SYSTEM_OFF;
579 	sc->psci_fnids[PSCI_FN_SYSTEM_RESET]	    = PSCI_FNID_SYSTEM_RESET;
580 
581 	version = _psci_get_version(sc);
582 
583 	/*
584 	 * U-Boot PSCI implementation doesn't have psci_get_version()
585 	 * method implemented for many boards. In this case, use the version
586 	 * readed from FDT as fallback. No fallback method for ACPI.
587 	 */
588 	if (version == PSCI_RETVAL_NOT_SUPPORTED) {
589 		if (default_version == PSCI_RETVAL_NOT_SUPPORTED)
590 			return (1);
591 
592 		version = default_version;
593 		printf("PSCI get_version() function is not implemented, "
594 		    " assuming v%d.%d\n", PSCI_VER_MAJOR(version),
595 		    PSCI_VER_MINOR(version));
596 	}
597 
598 	sc->psci_version = version;
599 	if ((PSCI_VER_MAJOR(version) == 0 && PSCI_VER_MINOR(version) == 2) ||
600 	    PSCI_VER_MAJOR(version) == 1) {
601 		if (bootverbose)
602 			device_printf(dev, "PSCI version 0.2 compatible\n");
603 
604 		/*
605 		 * We only register this for v0.2 since v0.1 doesn't support
606 		 * system_reset.
607 		 */
608 		EVENTHANDLER_REGISTER(shutdown_final, psci_shutdown, sc,
609 		    SHUTDOWN_PRI_LAST);
610 
611 		/* Handle reboot after shutdown_panic. */
612 		EVENTHANDLER_REGISTER(shutdown_final, psci_reboot, sc,
613 		    SHUTDOWN_PRI_LAST + 150);
614 
615 		return (0);
616 	}
617 
618 	device_printf(dev, "PSCI version number mismatched with DT\n");
619 	return (1);
620 }
621