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_version;
78 uint32_t psci_fnids[PSCI_FN_MAX];
79 };
80
81 #ifdef FDT
82 static int psci_v0_1_init(device_t dev, int default_version);
83 #endif
84 static int psci_v0_2_init(device_t dev, int default_version);
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 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 static void psci_shutdown(void *, int);
129
130 static int psci_find_callfn(psci_callfn_t *);
131 static int psci_def_callfn(register_t, register_t, register_t, register_t);
132
133 psci_callfn_t psci_callfn = psci_def_callfn;
134
135 static void
psci_init(void * dummy)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 }
147 /* This needs to be before cpu_mp at SI_SUB_CPU, SI_ORDER_THIRD */
148 SYSINIT(psci_start, SI_SUB_CPU, SI_ORDER_FIRST, psci_init, NULL);
149
150 static int
psci_def_callfn(register_t a __unused,register_t b __unused,register_t c __unused,register_t d __unused)151 psci_def_callfn(register_t a __unused, register_t b __unused,
152 register_t c __unused, register_t d __unused)
153 {
154
155 panic("No PSCI/SMCCC call function set");
156 }
157
158 #ifdef FDT
159 static int psci_fdt_probe(device_t dev);
160 static int psci_fdt_attach(device_t dev);
161
162 static device_method_t psci_fdt_methods[] = {
163 DEVMETHOD(device_probe, psci_fdt_probe),
164 DEVMETHOD(device_attach, psci_fdt_attach),
165
166 DEVMETHOD_END
167 };
168
169 static driver_t psci_fdt_driver = {
170 "psci",
171 psci_fdt_methods,
172 sizeof(struct psci_softc),
173 };
174
175 static devclass_t psci_fdt_devclass;
176
177 EARLY_DRIVER_MODULE(psci, simplebus, psci_fdt_driver, psci_fdt_devclass, 0, 0,
178 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
179 EARLY_DRIVER_MODULE(psci, ofwbus, psci_fdt_driver, psci_fdt_devclass, 0, 0,
180 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
181
182 static psci_callfn_t
psci_fdt_get_callfn(phandle_t node)183 psci_fdt_get_callfn(phandle_t node)
184 {
185 char method[16];
186
187 if ((OF_getprop(node, "method", method, sizeof(method))) > 0) {
188 if (strcmp(method, "hvc") == 0)
189 return (psci_hvc_despatch);
190 else if (strcmp(method, "smc") == 0)
191 return (psci_smc_despatch);
192 else
193 printf("psci: PSCI conduit \"%s\" invalid\n", method);
194 } else
195 printf("psci: PSCI conduit not supplied in the device tree\n");
196
197 return (NULL);
198 }
199
200 static int
psci_fdt_probe(device_t dev)201 psci_fdt_probe(device_t dev)
202 {
203 const struct ofw_compat_data *ocd;
204
205 if (!ofw_bus_status_okay(dev))
206 return (ENXIO);
207
208 ocd = ofw_bus_search_compatible(dev, compat_data);
209 if (ocd->ocd_str == NULL)
210 return (ENXIO);
211
212 device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
213
214 return (BUS_PROBE_SPECIFIC);
215 }
216
217 static int
psci_fdt_attach(device_t dev)218 psci_fdt_attach(device_t dev)
219 {
220 const struct ofw_compat_data *ocd;
221 struct psci_init_def *psci_init_def;
222
223 ocd = ofw_bus_search_compatible(dev, compat_data);
224 psci_init_def = (struct psci_init_def *)ocd->ocd_data;
225
226 return (psci_attach(dev, psci_init_def->psci_init,
227 psci_init_def->default_version));
228 }
229 #endif
230
231 #ifdef DEV_ACPI
232 static void psci_acpi_identify(driver_t *, device_t);
233 static int psci_acpi_probe(device_t);
234 static int psci_acpi_attach(device_t);
235
236 static device_method_t psci_acpi_methods[] = {
237 /* Device interface */
238 DEVMETHOD(device_identify, psci_acpi_identify),
239 DEVMETHOD(device_probe, psci_acpi_probe),
240 DEVMETHOD(device_attach, psci_acpi_attach),
241
242 DEVMETHOD_END
243 };
244
245 static driver_t psci_acpi_driver = {
246 "psci",
247 psci_acpi_methods,
248 sizeof(struct psci_softc),
249 };
250
251 static devclass_t psci_acpi_devclass;
252
253 EARLY_DRIVER_MODULE(psci, acpi, psci_acpi_driver, psci_acpi_devclass, 0, 0,
254 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
255
256 static int
psci_acpi_bootflags(void)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
psci_acpi_get_callfn(int flags)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 (psci_hvc_despatch);
286 else
287 return (psci_smc_despatch);
288 } else {
289 printf("psci: PSCI conduit not supplied in the device tree\n");
290 }
291
292 return (NULL);
293 }
294
295 static void
psci_acpi_identify(driver_t * driver,device_t parent)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
psci_acpi_probe(device_t dev)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
psci_acpi_attach(device_t dev)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
psci_attach(device_t dev,psci_initfn_t psci_init,int default_version)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 psci_softc = sc;
345
346 return (0);
347 }
348
349 static int
_psci_get_version(struct psci_softc * sc)350 _psci_get_version(struct psci_softc *sc)
351 {
352 uint32_t fnid;
353
354 /* PSCI version wasn't supported in v0.1. */
355 fnid = sc->psci_fnids[PSCI_FN_VERSION];
356 if (fnid)
357 return (psci_call(fnid, 0, 0, 0));
358
359 return (PSCI_RETVAL_NOT_SUPPORTED);
360 }
361
362 int
psci_get_version(void)363 psci_get_version(void)
364 {
365
366 if (psci_softc == NULL)
367 return (PSCI_RETVAL_NOT_SUPPORTED);
368 return (_psci_get_version(psci_softc));
369 }
370
371 #ifdef FDT
372 static int
psci_fdt_callfn(psci_callfn_t * callfn)373 psci_fdt_callfn(psci_callfn_t *callfn)
374 {
375 phandle_t node;
376
377 node = ofw_bus_find_compatible(OF_peer(0), "arm,psci-0.2");
378 if (node == 0) {
379 node = ofw_bus_find_compatible(OF_peer(0), "arm,psci-1.0");
380 if (node == 0)
381 return (PSCI_MISSING);
382 }
383
384 *callfn = psci_fdt_get_callfn(node);
385 return (0);
386 }
387 #endif
388
389 #ifdef DEV_ACPI
390 static int
psci_acpi_callfn(psci_callfn_t * callfn)391 psci_acpi_callfn(psci_callfn_t *callfn)
392 {
393 int flags;
394
395 flags = psci_acpi_bootflags();
396 if ((flags & ACPI_FADT_PSCI_COMPLIANT) == 0)
397 return (PSCI_MISSING);
398
399 *callfn = psci_acpi_get_callfn(flags);
400 return (0);
401 }
402 #endif
403
404 static int
psci_find_callfn(psci_callfn_t * callfn)405 psci_find_callfn(psci_callfn_t *callfn)
406 {
407 int error;
408
409 *callfn = NULL;
410 #ifdef FDT
411 if (USE_FDT) {
412 error = psci_fdt_callfn(callfn);
413 if (error != 0)
414 return (error);
415 }
416 #endif
417 #ifdef DEV_ACPI
418 if (*callfn == NULL && USE_ACPI) {
419 error = psci_acpi_callfn(callfn);
420 if (error != 0)
421 return (error);
422 }
423 #endif
424
425 if (*callfn == NULL)
426 return (PSCI_MISSING);
427
428 return (PSCI_RETVAL_SUCCESS);
429 }
430
431 int32_t
psci_features(uint32_t psci_func_id)432 psci_features(uint32_t psci_func_id)
433 {
434
435 if (psci_softc == NULL)
436 return (PSCI_RETVAL_NOT_SUPPORTED);
437
438 /* The feature flags were added to PSCI 1.0 */
439 if (PSCI_VER_MAJOR(psci_softc->psci_version) < 1)
440 return (PSCI_RETVAL_NOT_SUPPORTED);
441
442 return (psci_call(PSCI_FNID_FEATURES, psci_func_id, 0, 0));
443 }
444
445 int
psci_cpu_on(unsigned long cpu,unsigned long entry,unsigned long context_id)446 psci_cpu_on(unsigned long cpu, unsigned long entry, unsigned long context_id)
447 {
448 uint32_t fnid;
449
450 fnid = PSCI_FNID_CPU_ON;
451 if (psci_softc != NULL)
452 fnid = psci_softc->psci_fnids[PSCI_FN_CPU_ON];
453
454 /* PSCI v0.1 and v0.2 both support cpu_on. */
455 return (psci_call(fnid, cpu, entry, context_id));
456 }
457
458 static void
psci_shutdown(void * xsc,int howto)459 psci_shutdown(void *xsc, int howto)
460 {
461 uint32_t fn = 0;
462
463 if (psci_softc == NULL)
464 return;
465
466 /* PSCI system_off and system_reset werent't supported in v0.1. */
467 if ((howto & RB_POWEROFF) != 0)
468 fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_OFF];
469 else if ((howto & RB_HALT) == 0)
470 fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_RESET];
471
472 if (fn)
473 psci_call(fn, 0, 0, 0);
474
475 /* System reset and off do not return. */
476 }
477
478 void
psci_reset(void)479 psci_reset(void)
480 {
481
482 psci_shutdown(NULL, 0);
483 }
484
485 #ifdef FDT
486 /* Only support PSCI 0.1 on FDT */
487 static int
psci_v0_1_init(device_t dev,int default_version __unused)488 psci_v0_1_init(device_t dev, int default_version __unused)
489 {
490 struct psci_softc *sc = device_get_softc(dev);
491 int psci_fn;
492 uint32_t psci_fnid;
493 phandle_t node;
494 int len;
495
496
497 /* Zero out the function ID table - Is this needed ? */
498 for (psci_fn = PSCI_FN_VERSION, psci_fnid = PSCI_FNID_VERSION;
499 psci_fn < PSCI_FN_MAX; psci_fn++, psci_fnid++)
500 sc->psci_fnids[psci_fn] = 0;
501
502 /* PSCI v0.1 doesn't specify function IDs. Get them from DT */
503 node = ofw_bus_get_node(dev);
504
505 if ((len = OF_getproplen(node, "cpu_suspend")) > 0) {
506 OF_getencprop(node, "cpu_suspend", &psci_fnid, len);
507 sc->psci_fnids[PSCI_FN_CPU_SUSPEND] = psci_fnid;
508 }
509
510 if ((len = OF_getproplen(node, "cpu_on")) > 0) {
511 OF_getencprop(node, "cpu_on", &psci_fnid, len);
512 sc->psci_fnids[PSCI_FN_CPU_ON] = psci_fnid;
513 }
514
515 if ((len = OF_getproplen(node, "cpu_off")) > 0) {
516 OF_getencprop(node, "cpu_off", &psci_fnid, len);
517 sc->psci_fnids[PSCI_FN_CPU_OFF] = psci_fnid;
518 }
519
520 if ((len = OF_getproplen(node, "migrate")) > 0) {
521 OF_getencprop(node, "migrate", &psci_fnid, len);
522 sc->psci_fnids[PSCI_FN_MIGRATE] = psci_fnid;
523 }
524
525 sc->psci_version = (0 << 16) | 1;
526 if (bootverbose)
527 device_printf(dev, "PSCI version 0.1 available\n");
528
529 return(0);
530 }
531 #endif
532
533 static int
psci_v0_2_init(device_t dev,int default_version)534 psci_v0_2_init(device_t dev, int default_version)
535 {
536 struct psci_softc *sc = device_get_softc(dev);
537 int version;
538
539 /* PSCI v0.2 specifies explicit function IDs. */
540 sc->psci_fnids[PSCI_FN_VERSION] = PSCI_FNID_VERSION;
541 sc->psci_fnids[PSCI_FN_CPU_SUSPEND] = PSCI_FNID_CPU_SUSPEND;
542 sc->psci_fnids[PSCI_FN_CPU_OFF] = PSCI_FNID_CPU_OFF;
543 sc->psci_fnids[PSCI_FN_CPU_ON] = PSCI_FNID_CPU_ON;
544 sc->psci_fnids[PSCI_FN_AFFINITY_INFO] = PSCI_FNID_AFFINITY_INFO;
545 sc->psci_fnids[PSCI_FN_MIGRATE] = PSCI_FNID_MIGRATE;
546 sc->psci_fnids[PSCI_FN_MIGRATE_INFO_TYPE] = PSCI_FNID_MIGRATE_INFO_TYPE;
547 sc->psci_fnids[PSCI_FN_MIGRATE_INFO_UP_CPU] = PSCI_FNID_MIGRATE_INFO_UP_CPU;
548 sc->psci_fnids[PSCI_FN_SYSTEM_OFF] = PSCI_FNID_SYSTEM_OFF;
549 sc->psci_fnids[PSCI_FN_SYSTEM_RESET] = PSCI_FNID_SYSTEM_RESET;
550
551 version = _psci_get_version(sc);
552
553 /*
554 * U-Boot PSCI implementation doesn't have psci_get_version()
555 * method implemented for many boards. In this case, use the version
556 * readed from FDT as fallback. No fallback method for ACPI.
557 */
558 if (version == PSCI_RETVAL_NOT_SUPPORTED) {
559 if (default_version == PSCI_RETVAL_NOT_SUPPORTED)
560 return (1);
561
562 version = default_version;
563 printf("PSCI get_version() function is not implemented, "
564 " assuming v%d.%d\n", PSCI_VER_MAJOR(version),
565 PSCI_VER_MINOR(version));
566 }
567
568 sc->psci_version = version;
569 if ((PSCI_VER_MAJOR(version) == 0 && PSCI_VER_MINOR(version) == 2) ||
570 PSCI_VER_MAJOR(version) == 1) {
571 if (bootverbose)
572 device_printf(dev, "PSCI version 0.2 compatible\n");
573
574 /*
575 * We only register this for v0.2 since v0.1 doesn't support
576 * system_reset.
577 */
578 EVENTHANDLER_REGISTER(shutdown_final, psci_shutdown, sc,
579 SHUTDOWN_PRI_LAST);
580
581 return (0);
582 }
583
584 device_printf(dev, "PSCI version number mismatched with DT\n");
585 return (1);
586 }
587