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