xref: /netbsd-src/sys/arch/arm/fdt/psci_fdt.c (revision 7d8191aff7b2ceefaaf6e8a44d96c10d1862d2b9)
1 /* $NetBSD: psci_fdt.c,v 1.17 2018/09/09 21:16:05 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@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.17 2018/09/09 21:16:05 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 static bool
173 psci_fdt_cpu_okay(const int child)
174 {
175 	const char *s;
176 
177 	s = fdtbus_get_string(child, "device_type");
178 	if (!s || strcmp(s, "cpu") != 0)
179 		return false;
180 
181 	s = fdtbus_get_string(child, "status");
182 	if (s) {
183 		if (strcmp(s, "okay") == 0)
184 			return false;
185 		if (strcmp(s, "disabled") == 0)
186 			return of_hasprop(child, "enable-method");
187 		return false;
188 	} else {
189 		return true;
190 	}
191 }
192 
193 void
194 psci_fdt_bootstrap(void)
195 {
196 #ifdef MULTIPROCESSOR
197 	uint64_t mpidr, bp_mpidr;
198 	u_int cpuindex;
199 	int child;
200 	const char *devtype;
201 
202 	const int cpus = OF_finddevice("/cpus");
203 	if (cpus == -1) {
204 		aprint_error("PSCI: no /cpus node found\n");
205 		arm_cpu_max = 1;
206 		return;
207 	}
208 
209 	/* Count CPUs */
210 	arm_cpu_max = 0;
211 	for (child = OF_child(cpus); child; child = OF_peer(child))
212 		if (fdtbus_status_okay(child) && ((devtype =
213 		    fdtbus_get_string(child, "device_type")) != NULL) &&
214 		    (strcmp(devtype, "cpu") == 0))
215 			arm_cpu_max++;
216 
217 	if (psci_fdt_preinit() != 0)
218 		return;
219 
220 	/* MPIDR affinity levels of boot processor. */
221 	bp_mpidr = cpu_mpidr_aff_read();
222 
223 	/* Boot APs */
224 	cpuindex = 1;
225 	for (child = OF_child(cpus); child; child = OF_peer(child)) {
226 		if (!psci_fdt_cpu_okay(child))
227 			continue;
228 		if (fdtbus_get_reg64(child, 0, &mpidr, NULL) != 0)
229 			continue;
230 		if (mpidr == bp_mpidr)
231 			continue; 	/* BP already started */
232 
233 #ifdef __aarch64__
234 		/* argument for mpstart() */
235 		arm_cpu_hatch_arg = cpuindex;
236 		cpu_dcache_wb_range((vaddr_t)&arm_cpu_hatch_arg,
237 		    sizeof(arm_cpu_hatch_arg));
238 #endif
239 
240 		int ret = psci_cpu_on(mpidr, psci_fdt_mpstart_pa(), 0);
241 		if (ret != PSCI_SUCCESS)
242 			continue;
243 
244 		/* Wait for APs to start */
245 		for (u_int i = 0x4000000; i > 0; i--) {
246 			membar_consumer();
247 			if (arm_cpu_hatched & __BIT(cpuindex))
248 				break;
249 		}
250 
251 		cpuindex++;
252 	}
253 #endif
254 }
255 
256 void
257 psci_fdt_reset(void)
258 {
259 	if (psci_fdt_preinit() != 0) {
260 		aprint_error("PSCI: reset failed\n");
261 		return;
262 	}
263 
264 	psci_system_reset();
265 }
266