xref: /freebsd/sys/dev/psci/psci.c (revision 0c38b2d37c472a119a050203ebac022ab744ed10)
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 __FBSDID("$FreeBSD$");
45 
46 #include "opt_acpi.h"
47 #include "opt_platform.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/bus.h>
52 #include <sys/eventhandler.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/reboot.h>
56 
57 #include <machine/bus.h>
58 #include <machine/machdep.h>
59 
60 #ifdef DEV_ACPI
61 #include <contrib/dev/acpica/include/acpi.h>
62 #include <dev/acpica/acpivar.h>
63 #endif
64 
65 #ifdef FDT
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>
70 #endif
71 
72 #include <dev/psci/psci.h>
73 
74 struct psci_softc {
75 	device_t        dev;
76 
77 	uint32_t	psci_fnids[PSCI_FN_MAX];
78 };
79 
80 #ifdef FDT
81 static int psci_v0_1_init(device_t dev);
82 #endif
83 static int psci_v0_2_init(device_t dev);
84 
85 struct psci_softc *psci_softc = NULL;
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 static struct ofw_compat_data compat_data[] = {
99 	{"arm,psci-1.0",        (uintptr_t)psci_v0_2_init},
100 	{"arm,psci-0.2",        (uintptr_t)psci_v0_2_init},
101 	{"arm,psci",            (uintptr_t)psci_v0_1_init},
102 	{NULL,                  0}
103 };
104 #endif
105 
106 static int psci_attach(device_t, psci_initfn_t);
107 static void psci_shutdown(void *, int);
108 
109 static int psci_find_callfn(psci_callfn_t *);
110 static int psci_def_callfn(register_t, register_t, register_t, register_t);
111 
112 static psci_callfn_t psci_callfn = psci_def_callfn;
113 
114 static inline int
115 psci_call(register_t a, register_t b, register_t c, register_t d)
116 {
117 
118 	return (psci_callfn(a, b, c, d));
119 }
120 
121 static void
122 psci_init(void *dummy)
123 {
124 	psci_callfn_t new_callfn;
125 
126 	if (psci_find_callfn(&new_callfn) != PSCI_RETVAL_SUCCESS) {
127 		printf("No PSCI/SMCCC call function found");
128 		return;
129 	}
130 
131 	psci_callfn = new_callfn;
132 }
133 /* This needs to be before cpu_mp at SI_SUB_CPU, SI_ORDER_THIRD */
134 SYSINIT(psci_start, SI_SUB_CPU, SI_ORDER_FIRST, psci_init, NULL);
135 
136 static int
137 psci_def_callfn(register_t a __unused, register_t b __unused,
138     register_t c __unused, register_t d __unused)
139 {
140 
141 	panic("No PSCI/SMCCC call function set");
142 }
143 
144 #ifdef FDT
145 static int psci_fdt_probe(device_t dev);
146 static int psci_fdt_attach(device_t dev);
147 
148 static device_method_t psci_fdt_methods[] = {
149 	DEVMETHOD(device_probe,     psci_fdt_probe),
150 	DEVMETHOD(device_attach,    psci_fdt_attach),
151 
152 	DEVMETHOD_END
153 };
154 
155 static driver_t psci_fdt_driver = {
156 	"psci",
157 	psci_fdt_methods,
158 	sizeof(struct psci_softc),
159 };
160 
161 static devclass_t psci_fdt_devclass;
162 
163 EARLY_DRIVER_MODULE(psci, simplebus, psci_fdt_driver, psci_fdt_devclass, 0, 0,
164     BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
165 EARLY_DRIVER_MODULE(psci, ofwbus, psci_fdt_driver, psci_fdt_devclass, 0, 0,
166     BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
167 
168 static psci_callfn_t
169 psci_fdt_get_callfn(phandle_t node)
170 {
171 	char method[16];
172 
173 	if ((OF_getprop(node, "method", method, sizeof(method))) > 0) {
174 		if (strcmp(method, "hvc") == 0)
175 			return (psci_hvc_despatch);
176 		else if (strcmp(method, "smc") == 0)
177 			return (psci_smc_despatch);
178 		else
179 			printf("psci: PSCI conduit \"%s\" invalid\n", method);
180 	} else
181 		printf("psci: PSCI conduit not supplied in the device tree\n");
182 
183 	return (NULL);
184 }
185 
186 static int
187 psci_fdt_probe(device_t dev)
188 {
189 	const struct ofw_compat_data *ocd;
190 
191 	if (!ofw_bus_status_okay(dev))
192 		return (ENXIO);
193 
194 	ocd = ofw_bus_search_compatible(dev, compat_data);
195 	if (ocd->ocd_str == NULL)
196 		return (ENXIO);
197 
198 	device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
199 
200 	return (BUS_PROBE_SPECIFIC);
201 }
202 
203 static int
204 psci_fdt_attach(device_t dev)
205 {
206 	const struct ofw_compat_data *ocd;
207 	psci_initfn_t psci_init;
208 
209 	ocd = ofw_bus_search_compatible(dev, compat_data);
210 	psci_init = (psci_initfn_t)ocd->ocd_data;
211 
212 	return (psci_attach(dev, psci_init));
213 }
214 #endif
215 
216 #ifdef DEV_ACPI
217 static void psci_acpi_identify(driver_t *, device_t);
218 static int psci_acpi_probe(device_t);
219 static int psci_acpi_attach(device_t);
220 
221 static device_method_t psci_acpi_methods[] = {
222 	/* Device interface */
223 	DEVMETHOD(device_identify,	psci_acpi_identify),
224 	DEVMETHOD(device_probe,		psci_acpi_probe),
225 	DEVMETHOD(device_attach,	psci_acpi_attach),
226 
227 	DEVMETHOD_END
228 };
229 
230 static driver_t psci_acpi_driver = {
231 	"psci",
232 	psci_acpi_methods,
233 	sizeof(struct psci_softc),
234 };
235 
236 static devclass_t psci_acpi_devclass;
237 
238 EARLY_DRIVER_MODULE(psci, acpi, psci_acpi_driver, psci_acpi_devclass, 0, 0,
239     BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
240 
241 static int
242 psci_acpi_bootflags(void)
243 {
244 	ACPI_TABLE_FADT *fadt;
245 	vm_paddr_t physaddr;
246 	int flags;
247 
248 	physaddr = acpi_find_table(ACPI_SIG_FADT);
249 	if (physaddr == 0)
250 		return (0);
251 
252 	fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
253 	if (fadt == NULL) {
254 		printf("psci: Unable to map the FADT\n");
255 		return (0);
256 	}
257 
258 	flags = fadt->ArmBootFlags;
259 
260 	acpi_unmap_table(fadt);
261 	return (flags);
262 }
263 
264 static psci_callfn_t
265 psci_acpi_get_callfn(int flags)
266 {
267 
268 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
269 		if ((flags & ACPI_FADT_PSCI_USE_HVC) != 0)
270 			return (psci_hvc_despatch);
271 		else
272 			return (psci_smc_despatch);
273 	} else {
274 		printf("psci: PSCI conduit not supplied in the device tree\n");
275 	}
276 
277 	return (NULL);
278 }
279 
280 static void
281 psci_acpi_identify(driver_t *driver, device_t parent)
282 {
283 	device_t dev;
284 	int flags;
285 
286 	flags = psci_acpi_bootflags();
287 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
288 		dev = BUS_ADD_CHILD(parent,
289 		    BUS_PASS_CPU + BUS_PASS_ORDER_FIRST, "psci", -1);
290 
291 		if (dev != NULL)
292 			acpi_set_private(dev, (void *)(uintptr_t)flags);
293 	}
294 }
295 
296 static int
297 psci_acpi_probe(device_t dev)
298 {
299 	uintptr_t flags;
300 
301 	flags = (uintptr_t)acpi_get_private(dev);
302 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) == 0)
303 		return (ENXIO);
304 
305 	device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
306 	return (BUS_PROBE_SPECIFIC);
307 }
308 
309 static int
310 psci_acpi_attach(device_t dev)
311 {
312 
313 	return (psci_attach(dev, psci_v0_2_init));
314 }
315 #endif
316 
317 static int
318 psci_attach(device_t dev, psci_initfn_t psci_init)
319 {
320 	struct psci_softc *sc = device_get_softc(dev);
321 
322 	if (psci_softc != NULL)
323 		return (ENXIO);
324 
325 	KASSERT(psci_init != NULL, ("PSCI init function cannot be NULL"));
326 	if (psci_init(dev))
327 		return (ENXIO);
328 
329 	psci_softc = sc;
330 
331 	return (0);
332 }
333 
334 static int
335 _psci_get_version(struct psci_softc *sc)
336 {
337 	uint32_t fnid;
338 
339 	/* PSCI version wasn't supported in v0.1. */
340 	fnid = sc->psci_fnids[PSCI_FN_VERSION];
341 	if (fnid)
342 		return (psci_call(fnid, 0, 0, 0));
343 
344 	return (PSCI_RETVAL_NOT_SUPPORTED);
345 }
346 
347 int
348 psci_get_version(void)
349 {
350 
351 	if (psci_softc == NULL)
352 		return (PSCI_RETVAL_NOT_SUPPORTED);
353 	return (_psci_get_version(psci_softc));
354 }
355 
356 #ifdef FDT
357 static int
358 psci_fdt_callfn(psci_callfn_t *callfn)
359 {
360 	phandle_t node;
361 
362 	node = ofw_bus_find_compatible(OF_peer(0), "arm,psci-0.2");
363 	if (node == 0) {
364 		node = ofw_bus_find_compatible(OF_peer(0), "arm,psci-1.0");
365 		if (node == 0)
366 			return (PSCI_MISSING);
367 	}
368 
369 	*callfn = psci_fdt_get_callfn(node);
370 	return (0);
371 }
372 #endif
373 
374 #ifdef DEV_ACPI
375 static int
376 psci_acpi_callfn(psci_callfn_t *callfn)
377 {
378 	int flags;
379 
380 	flags = psci_acpi_bootflags();
381 	if ((flags & ACPI_FADT_PSCI_COMPLIANT) == 0)
382 		return (PSCI_MISSING);
383 
384 	*callfn = psci_acpi_get_callfn(flags);
385 	return (0);
386 }
387 #endif
388 
389 static int
390 psci_find_callfn(psci_callfn_t *callfn)
391 {
392 	int error;
393 
394 	*callfn = NULL;
395 #ifdef FDT
396 	if (USE_FDT) {
397 		error = psci_fdt_callfn(callfn);
398 		if (error != 0)
399 			return (error);
400 	}
401 #endif
402 #ifdef DEV_ACPI
403 	if (*callfn == NULL && USE_ACPI) {
404 		error = psci_acpi_callfn(callfn);
405 		if (error != 0)
406 			return (error);
407 	}
408 #endif
409 
410 	if (*callfn == NULL)
411 		return (PSCI_MISSING);
412 
413 	return (PSCI_RETVAL_SUCCESS);
414 }
415 
416 int
417 psci_cpu_on(unsigned long cpu, unsigned long entry, unsigned long context_id)
418 {
419 	uint32_t fnid;
420 
421 	fnid = PSCI_FNID_CPU_ON;
422 	if (psci_softc != NULL)
423 		fnid = psci_softc->psci_fnids[PSCI_FN_CPU_ON];
424 
425 	/* PSCI v0.1 and v0.2 both support cpu_on. */
426 	return (psci_call(fnid, cpu, entry, context_id));
427 }
428 
429 static void
430 psci_shutdown(void *xsc, int howto)
431 {
432 	uint32_t fn = 0;
433 
434 	if (psci_softc == NULL)
435 		return;
436 
437 	/* PSCI system_off and system_reset werent't supported in v0.1. */
438 	if ((howto & RB_POWEROFF) != 0)
439 		fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_OFF];
440 	else if ((howto & RB_HALT) == 0)
441 		fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_RESET];
442 
443 	if (fn)
444 		psci_call(fn, 0, 0, 0);
445 
446 	/* System reset and off do not return. */
447 }
448 
449 void
450 psci_reset(void)
451 {
452 
453 	psci_shutdown(NULL, 0);
454 }
455 
456 #ifdef FDT
457 /* Only support PSCI 0.1 on FDT */
458 static int
459 psci_v0_1_init(device_t dev)
460 {
461 	struct psci_softc *sc = device_get_softc(dev);
462 	int psci_fn;
463 	uint32_t psci_fnid;
464 	phandle_t node;
465 	int len;
466 
467 
468 	/* Zero out the function ID table - Is this needed ? */
469 	for (psci_fn = PSCI_FN_VERSION, psci_fnid = PSCI_FNID_VERSION;
470 	    psci_fn < PSCI_FN_MAX; psci_fn++, psci_fnid++)
471 		sc->psci_fnids[psci_fn] = 0;
472 
473 	/* PSCI v0.1 doesn't specify function IDs. Get them from DT */
474 	node = ofw_bus_get_node(dev);
475 
476 	if ((len = OF_getproplen(node, "cpu_suspend")) > 0) {
477 		OF_getencprop(node, "cpu_suspend", &psci_fnid, len);
478 		sc->psci_fnids[PSCI_FN_CPU_SUSPEND] = psci_fnid;
479 	}
480 
481 	if ((len = OF_getproplen(node, "cpu_on")) > 0) {
482 		OF_getencprop(node, "cpu_on", &psci_fnid, len);
483 		sc->psci_fnids[PSCI_FN_CPU_ON] = psci_fnid;
484 	}
485 
486 	if ((len = OF_getproplen(node, "cpu_off")) > 0) {
487 		OF_getencprop(node, "cpu_off", &psci_fnid, len);
488 		sc->psci_fnids[PSCI_FN_CPU_OFF] = psci_fnid;
489 	}
490 
491 	if ((len = OF_getproplen(node, "migrate")) > 0) {
492 		OF_getencprop(node, "migrate", &psci_fnid, len);
493 		sc->psci_fnids[PSCI_FN_MIGRATE] = psci_fnid;
494 	}
495 
496 	if (bootverbose)
497 		device_printf(dev, "PSCI version 0.1 available\n");
498 
499 	return(0);
500 }
501 #endif
502 
503 static int
504 psci_v0_2_init(device_t dev)
505 {
506 	struct psci_softc *sc = device_get_softc(dev);
507 	int version;
508 
509 	/* PSCI v0.2 specifies explicit function IDs. */
510 	sc->psci_fnids[PSCI_FN_VERSION]		    = PSCI_FNID_VERSION;
511 	sc->psci_fnids[PSCI_FN_CPU_SUSPEND]	    = PSCI_FNID_CPU_SUSPEND;
512 	sc->psci_fnids[PSCI_FN_CPU_OFF]		    = PSCI_FNID_CPU_OFF;
513 	sc->psci_fnids[PSCI_FN_CPU_ON]		    = PSCI_FNID_CPU_ON;
514 	sc->psci_fnids[PSCI_FN_AFFINITY_INFO]	    = PSCI_FNID_AFFINITY_INFO;
515 	sc->psci_fnids[PSCI_FN_MIGRATE]		    = PSCI_FNID_MIGRATE;
516 	sc->psci_fnids[PSCI_FN_MIGRATE_INFO_TYPE]   = PSCI_FNID_MIGRATE_INFO_TYPE;
517 	sc->psci_fnids[PSCI_FN_MIGRATE_INFO_UP_CPU] = PSCI_FNID_MIGRATE_INFO_UP_CPU;
518 	sc->psci_fnids[PSCI_FN_SYSTEM_OFF]	    = PSCI_FNID_SYSTEM_OFF;
519 	sc->psci_fnids[PSCI_FN_SYSTEM_RESET]	    = PSCI_FNID_SYSTEM_RESET;
520 
521 	version = _psci_get_version(sc);
522 
523 	if (version == PSCI_RETVAL_NOT_SUPPORTED)
524 		return (1);
525 
526 	if ((PSCI_VER_MAJOR(version) == 0 && PSCI_VER_MINOR(version) == 2) ||
527 	    PSCI_VER_MAJOR(version) == 1) {
528 		if (bootverbose)
529 			device_printf(dev, "PSCI version 0.2 compatible\n");
530 
531 		/*
532 		 * We only register this for v0.2 since v0.1 doesn't support
533 		 * system_reset.
534 		 */
535 		EVENTHANDLER_REGISTER(shutdown_final, psci_shutdown, sc,
536 		    SHUTDOWN_PRI_LAST);
537 
538 		return (0);
539 	}
540 
541 	device_printf(dev, "PSCI version number mismatched with DT\n");
542 	return (1);
543 }
544