psci_fdt.c revision 1.4 1 /* $NetBSD: psci_fdt.c,v 1.4 2018/05/26 22:49:03 jmcneill 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.4 2018/05/26 22:49:03 jmcneill Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39
40 #include <dev/fdt/fdtvar.h>
41
42 #include <arm/locore.h>
43 #include <arm/armreg.h>
44
45 #include <arm/arm/psci.h>
46 #include <arm/fdt/psci_fdt.h>
47
48 static int psci_fdt_match(device_t, cfdata_t, void *);
49 static void psci_fdt_attach(device_t, device_t, void *);
50
51 static int psci_fdt_init(const int);
52
53 static const char * const compatible[] = {
54 "arm,psci",
55 "arm,psci-0.2",
56 "arm,psci-1.0",
57 NULL
58 };
59
60 CFATTACH_DECL_NEW(psci_fdt, 0, psci_fdt_match, psci_fdt_attach, NULL, NULL);
61
62 static void
63 psci_fdt_power_reset(device_t dev)
64 {
65 delay(500000);
66 psci_system_reset();
67 }
68
69 static void
70 psci_fdt_power_poweroff(device_t dev)
71 {
72 delay(500000);
73 psci_system_off();
74 }
75
76 static const struct fdtbus_power_controller_func psci_power_funcs = {
77 .reset = psci_fdt_power_reset,
78 .poweroff = psci_fdt_power_poweroff,
79 };
80
81 static int
82 psci_fdt_match(device_t parent, cfdata_t cf, void *aux)
83 {
84 struct fdt_attach_args * const faa = aux;
85
86 return of_match_compatible(faa->faa_phandle, compatible);
87 }
88
89 static void
90 psci_fdt_attach(device_t parent, device_t self, void *aux)
91 {
92 struct fdt_attach_args * const faa = aux;
93 const int phandle = faa->faa_phandle;
94
95 psci_fdt_init(phandle);
96
97 const uint32_t ver = psci_version();
98 const u_int ver_maj = __SHIFTOUT(ver, PSCI_VERSION_MAJOR);
99 const u_int ver_min = __SHIFTOUT(ver, PSCI_VERSION_MINOR);
100
101 aprint_naive("\n");
102 aprint_normal(": PSCI %u.%u\n", ver_maj, ver_min);
103
104 fdtbus_register_power_controller(self, phandle,
105 &psci_power_funcs);
106 }
107
108 static int
109 psci_fdt_init(const int phandle)
110 {
111 char method[4];
112 uint32_t val;
113
114 if (!of_hasprop(phandle, "method")) {
115 aprint_error("PSCI: missing 'method' property\n");
116 return EINVAL;
117 }
118
119 OF_getprop(phandle, "method", method, sizeof(method));
120 if (strcmp(method, "smc") == 0)
121 psci_init(psci_call_smc);
122 else if (strcmp(method, "hvc") == 0)
123 psci_init(psci_call_hvc);
124 else {
125 aprint_error("PSCI: unsupported method '%s'\n", method);
126 return EINVAL;
127 }
128
129 const char * const compat_0_1[] = { "arm,psci", NULL };
130 if (of_match_compatible(phandle, compat_0_1)) {
131 psci_clearfunc();
132 if (of_getprop_uint32(phandle, "cpu_on", &val) == 0)
133 psci_setfunc(PSCI_FUNC_CPU_ON, val);
134 }
135
136 return 0;
137 }
138
139 static int
140 psci_fdt_preinit(void)
141 {
142 const int phandle = OF_finddevice("/psci");
143 if (phandle == -1) {
144 aprint_error("PSCI: no /psci node found\n");
145 return ENODEV;
146 }
147
148 return psci_fdt_init(phandle);
149 }
150
151 void
152 psci_fdt_bootstrap(void)
153 {
154 #ifdef MULTIPROCESSOR
155 extern void cortex_mpstart(void);
156 bus_addr_t mpidr;
157 uint32_t bp_mpidr;
158 int child;
159
160 const int cpus = OF_finddevice("/cpus");
161 if (cpus == -1) {
162 aprint_error("PSCI: no /cpus node found\n");
163 arm_cpu_max = 1;
164 return;
165 }
166
167 /* Count CPUs */
168 arm_cpu_max = 0;
169 for (child = OF_child(cpus); child; child = OF_peer(child))
170 if (fdtbus_status_okay(child))
171 arm_cpu_max++;
172
173 if (psci_fdt_preinit() != 0)
174 return;
175
176 /* MPIDR affinity levels of boot processor. */
177 bp_mpidr = armreg_mpidr_read() & (MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0);
178
179 /* Boot APs */
180 uint32_t started = 0;
181 for (child = OF_child(cpus); child; child = OF_peer(child)) {
182 if (!fdtbus_status_okay(child))
183 continue;
184 if (fdtbus_get_reg(child, 0, &mpidr, NULL) != 0)
185 continue;
186 if (mpidr == bp_mpidr)
187 continue; /* BP already started */
188
189 /* XXX NetBSD requires all CPUs to be in the same cluster */
190 const u_int bp_clid = __SHIFTOUT(bp_mpidr, CORTEXA9_MPIDR_CLID);
191 const u_int clid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CLID);
192 if (bp_clid != clid)
193 continue;
194
195 const u_int cpuid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CPUID);
196 int ret = psci_cpu_on(cpuid, (register_t)cortex_mpstart, 0);
197 if (ret == PSCI_SUCCESS)
198 started |= __BIT(cpuid);
199 }
200
201 /* Wait for APs to start */
202 for (u_int i = 0x10000000; i > 0; i--) {
203 arm_dmb();
204 if (arm_cpu_hatched == started)
205 break;
206 }
207 #endif
208 }
209
210 void
211 psci_fdt_reset(void)
212 {
213 if (psci_fdt_preinit() != 0) {
214 aprint_error("PSCI: reset failed\n");
215 return;
216 }
217
218 psci_system_reset();
219 }
220