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