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