psci_fdt.c revision 1.20 1 /* $NetBSD: psci_fdt.c,v 1.20 2021/01/27 03:10:19 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "opt_multiprocessor.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v 1.20 2021/01/27 03:10:19 thorpej Exp $");
33
34 #include <sys/param.h>
35 #include <sys/atomic.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40
41 #include <dev/fdt/fdtvar.h>
42
43 #include <arm/arm/psci.h>
44 #include <arm/fdt/psci_fdtvar.h>
45
46 static int psci_fdt_match(device_t, cfdata_t, void *);
47 static void psci_fdt_attach(device_t, device_t, void *);
48
49 static int psci_fdt_init(const int);
50
51 static const struct device_compatible_entry compat_data[] = {
52 { .compat = "arm,psci" },
53 { .compat = "arm,psci-0.2" },
54 { .compat = "arm,psci-1.0" },
55 DEVICE_COMPAT_EOL
56 };
57
58 CFATTACH_DECL_NEW(psci_fdt, 0, psci_fdt_match, psci_fdt_attach, NULL, NULL);
59
60 static void
61 psci_fdt_power_reset(device_t dev)
62 {
63 delay(500000);
64 psci_system_reset();
65 }
66
67 static void
68 psci_fdt_power_poweroff(device_t dev)
69 {
70 delay(500000);
71 psci_system_off();
72 }
73
74 static const struct fdtbus_power_controller_func psci_power_funcs = {
75 .reset = psci_fdt_power_reset,
76 .poweroff = psci_fdt_power_poweroff,
77 };
78
79 static int
80 psci_fdt_match(device_t parent, cfdata_t cf, void *aux)
81 {
82 struct fdt_attach_args * const faa = aux;
83
84 return of_compatible_match(faa->faa_phandle, compat_data);
85 }
86
87 static void
88 psci_fdt_attach(device_t parent, device_t self, void *aux)
89 {
90 struct fdt_attach_args * const faa = aux;
91 const int phandle = faa->faa_phandle;
92
93 psci_fdt_init(phandle);
94
95 const uint32_t ver = psci_version();
96 const u_int ver_maj = __SHIFTOUT(ver, PSCI_VERSION_MAJOR);
97 const u_int ver_min = __SHIFTOUT(ver, PSCI_VERSION_MINOR);
98
99 aprint_naive("\n");
100 aprint_normal(": PSCI %u.%u\n", ver_maj, ver_min);
101
102 fdtbus_register_power_controller(self, phandle,
103 &psci_power_funcs);
104 }
105
106 static int
107 psci_fdt_init(const int phandle)
108 {
109 const char *method, *psciver;
110 uint32_t val;
111
112 method = fdtbus_get_string(phandle, "method");
113 psciver = fdtbus_get_string(phandle, "compatible");
114 if (method == NULL || psciver == NULL) {
115 aprint_error("PSCI: missing required property on /psci\n");
116 return EINVAL;
117 }
118
119 if (strcmp(method, "smc") == 0)
120 psci_init(psci_call_smc);
121 else if (strcmp(method, "hvc") == 0)
122 psci_init(psci_call_hvc);
123 else {
124 aprint_error("PSCI: unsupported method '%s'\n", method);
125 return EINVAL;
126 }
127
128 /*
129 * If the first compatible string is "arm,psci" then we
130 * are dealing with PSCI 0.1
131 */
132 if (strcmp(psciver, "arm,psci") == 0) {
133 psci_clearfunc();
134 if (of_getprop_uint32(phandle, "cpu_on", &val) == 0)
135 psci_setfunc(PSCI_FUNC_CPU_ON, val);
136 }
137
138 return 0;
139 }
140
141 int
142 psci_fdt_preinit(void)
143 {
144 const int phandle = OF_finddevice("/psci");
145 if (phandle == -1) {
146 aprint_error("PSCI: no /psci node found\n");
147 return ENODEV;
148 }
149
150 return psci_fdt_init(phandle);
151 }
152
153 void
154 psci_fdt_reset(void)
155 {
156 if (psci_fdt_preinit() != 0) {
157 aprint_error("PSCI: reset failed\n");
158 return;
159 }
160
161 psci_system_reset();
162 }
163