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