psci_fdt.c revision 1.3.2.2 1 1.3.2.2 jdolecek /* $NetBSD: psci_fdt.c,v 1.3.2.2 2017/12/03 11:35:52 jdolecek Exp $ */
2 1.3.2.2 jdolecek
3 1.3.2.2 jdolecek /*-
4 1.3.2.2 jdolecek * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.3.2.2 jdolecek * All rights reserved.
6 1.3.2.2 jdolecek *
7 1.3.2.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.3.2.2 jdolecek * modification, are permitted provided that the following conditions
9 1.3.2.2 jdolecek * are met:
10 1.3.2.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.3.2.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.3.2.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.2.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.3.2.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.3.2.2 jdolecek *
16 1.3.2.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.3.2.2 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.3.2.2 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.3.2.2 jdolecek * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.3.2.2 jdolecek * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.3.2.2 jdolecek * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.3.2.2 jdolecek * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.3.2.2 jdolecek * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.3.2.2 jdolecek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.3.2.2 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.3.2.2 jdolecek * SUCH DAMAGE.
27 1.3.2.2 jdolecek */
28 1.3.2.2 jdolecek
29 1.3.2.2 jdolecek #include "opt_multiprocessor.h"
30 1.3.2.2 jdolecek
31 1.3.2.2 jdolecek #include <sys/cdefs.h>
32 1.3.2.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v 1.3.2.2 2017/12/03 11:35:52 jdolecek Exp $");
33 1.3.2.2 jdolecek
34 1.3.2.2 jdolecek #include <sys/param.h>
35 1.3.2.2 jdolecek #include <sys/bus.h>
36 1.3.2.2 jdolecek #include <sys/device.h>
37 1.3.2.2 jdolecek #include <sys/systm.h>
38 1.3.2.2 jdolecek #include <sys/kernel.h>
39 1.3.2.2 jdolecek
40 1.3.2.2 jdolecek #include <dev/fdt/fdtvar.h>
41 1.3.2.2 jdolecek
42 1.3.2.2 jdolecek #include <arm/locore.h>
43 1.3.2.2 jdolecek #include <arm/armreg.h>
44 1.3.2.2 jdolecek
45 1.3.2.2 jdolecek #include <arm/arm/psci.h>
46 1.3.2.2 jdolecek #include <arm/fdt/psci_fdt.h>
47 1.3.2.2 jdolecek
48 1.3.2.2 jdolecek static int psci_fdt_match(device_t, cfdata_t, void *);
49 1.3.2.2 jdolecek static void psci_fdt_attach(device_t, device_t, void *);
50 1.3.2.2 jdolecek
51 1.3.2.2 jdolecek static int psci_fdt_init(const int);
52 1.3.2.2 jdolecek
53 1.3.2.2 jdolecek static const char * const compatible[] = {
54 1.3.2.2 jdolecek "arm,psci",
55 1.3.2.2 jdolecek "arm,psci-0.2",
56 1.3.2.2 jdolecek "arm,psci-1.0",
57 1.3.2.2 jdolecek NULL
58 1.3.2.2 jdolecek };
59 1.3.2.2 jdolecek
60 1.3.2.2 jdolecek CFATTACH_DECL_NEW(psci_fdt, 0, psci_fdt_match, psci_fdt_attach, NULL, NULL);
61 1.3.2.2 jdolecek
62 1.3.2.2 jdolecek static void
63 1.3.2.2 jdolecek psci_fdt_reset(device_t dev)
64 1.3.2.2 jdolecek {
65 1.3.2.2 jdolecek delay(500000);
66 1.3.2.2 jdolecek psci_system_reset();
67 1.3.2.2 jdolecek }
68 1.3.2.2 jdolecek
69 1.3.2.2 jdolecek static void
70 1.3.2.2 jdolecek psci_fdt_poweroff(device_t dev)
71 1.3.2.2 jdolecek {
72 1.3.2.2 jdolecek delay(500000);
73 1.3.2.2 jdolecek psci_system_off();
74 1.3.2.2 jdolecek }
75 1.3.2.2 jdolecek
76 1.3.2.2 jdolecek static const struct fdtbus_power_controller_func psci_power_funcs = {
77 1.3.2.2 jdolecek .reset = psci_fdt_reset,
78 1.3.2.2 jdolecek .poweroff = psci_fdt_poweroff,
79 1.3.2.2 jdolecek };
80 1.3.2.2 jdolecek
81 1.3.2.2 jdolecek static int
82 1.3.2.2 jdolecek psci_fdt_match(device_t parent, cfdata_t cf, void *aux)
83 1.3.2.2 jdolecek {
84 1.3.2.2 jdolecek struct fdt_attach_args * const faa = aux;
85 1.3.2.2 jdolecek
86 1.3.2.2 jdolecek return of_match_compatible(faa->faa_phandle, compatible);
87 1.3.2.2 jdolecek }
88 1.3.2.2 jdolecek
89 1.3.2.2 jdolecek static void
90 1.3.2.2 jdolecek psci_fdt_attach(device_t parent, device_t self, void *aux)
91 1.3.2.2 jdolecek {
92 1.3.2.2 jdolecek struct fdt_attach_args * const faa = aux;
93 1.3.2.2 jdolecek const int phandle = faa->faa_phandle;
94 1.3.2.2 jdolecek
95 1.3.2.2 jdolecek psci_fdt_init(phandle);
96 1.3.2.2 jdolecek
97 1.3.2.2 jdolecek const uint32_t ver = psci_version();
98 1.3.2.2 jdolecek const u_int ver_maj = __SHIFTOUT(ver, PSCI_VERSION_MAJOR);
99 1.3.2.2 jdolecek const u_int ver_min = __SHIFTOUT(ver, PSCI_VERSION_MINOR);
100 1.3.2.2 jdolecek
101 1.3.2.2 jdolecek aprint_naive("\n");
102 1.3.2.2 jdolecek aprint_normal(": PSCI %u.%u\n", ver_maj, ver_min);
103 1.3.2.2 jdolecek
104 1.3.2.2 jdolecek fdtbus_register_power_controller(self, phandle,
105 1.3.2.2 jdolecek &psci_power_funcs);
106 1.3.2.2 jdolecek }
107 1.3.2.2 jdolecek
108 1.3.2.2 jdolecek static int
109 1.3.2.2 jdolecek psci_fdt_init(const int phandle)
110 1.3.2.2 jdolecek {
111 1.3.2.2 jdolecek char method[4];
112 1.3.2.2 jdolecek uint32_t val;
113 1.3.2.2 jdolecek
114 1.3.2.2 jdolecek if (!of_hasprop(phandle, "method")) {
115 1.3.2.2 jdolecek aprint_error("PSCI: missing 'method' property\n");
116 1.3.2.2 jdolecek return EINVAL;
117 1.3.2.2 jdolecek }
118 1.3.2.2 jdolecek
119 1.3.2.2 jdolecek OF_getprop(phandle, "method", method, sizeof(method));
120 1.3.2.2 jdolecek if (strcmp(method, "smc") == 0)
121 1.3.2.2 jdolecek psci_init(psci_call_smc);
122 1.3.2.2 jdolecek else if (strcmp(method, "hvc") == 0)
123 1.3.2.2 jdolecek psci_init(psci_call_hvc);
124 1.3.2.2 jdolecek else {
125 1.3.2.2 jdolecek aprint_error("PSCI: unsupported method '%s'\n", method);
126 1.3.2.2 jdolecek return EINVAL;
127 1.3.2.2 jdolecek }
128 1.3.2.2 jdolecek
129 1.3.2.2 jdolecek const char * const compat_0_1[] = { "arm,psci", NULL };
130 1.3.2.2 jdolecek if (of_match_compatible(phandle, compat_0_1)) {
131 1.3.2.2 jdolecek psci_clearfunc();
132 1.3.2.2 jdolecek if (of_getprop_uint32(phandle, "cpu_on", &val) == 0)
133 1.3.2.2 jdolecek psci_setfunc(PSCI_FUNC_CPU_ON, val);
134 1.3.2.2 jdolecek }
135 1.3.2.2 jdolecek
136 1.3.2.2 jdolecek return 0;
137 1.3.2.2 jdolecek }
138 1.3.2.2 jdolecek
139 1.3.2.2 jdolecek void
140 1.3.2.2 jdolecek psci_fdt_bootstrap(void)
141 1.3.2.2 jdolecek {
142 1.3.2.2 jdolecek #ifdef MULTIPROCESSOR
143 1.3.2.2 jdolecek extern void cortex_mpstart(void);
144 1.3.2.2 jdolecek bus_addr_t mpidr;
145 1.3.2.2 jdolecek uint32_t bp_mpidr;
146 1.3.2.2 jdolecek int child;
147 1.3.2.2 jdolecek
148 1.3.2.2 jdolecek const int cpus = OF_finddevice("/cpus");
149 1.3.2.2 jdolecek if (cpus == -1) {
150 1.3.2.2 jdolecek aprint_error("PSCI: no /cpus node found\n");
151 1.3.2.2 jdolecek arm_cpu_max = 1;
152 1.3.2.2 jdolecek return;
153 1.3.2.2 jdolecek }
154 1.3.2.2 jdolecek
155 1.3.2.2 jdolecek /* Count CPUs */
156 1.3.2.2 jdolecek arm_cpu_max = 0;
157 1.3.2.2 jdolecek for (child = OF_child(cpus); child; child = OF_peer(child))
158 1.3.2.2 jdolecek if (fdtbus_status_okay(child))
159 1.3.2.2 jdolecek arm_cpu_max++;
160 1.3.2.2 jdolecek
161 1.3.2.2 jdolecek const int phandle = OF_finddevice("/psci");
162 1.3.2.2 jdolecek if (phandle == -1) {
163 1.3.2.2 jdolecek aprint_error("PSCI: no /psci node found\n");
164 1.3.2.2 jdolecek return;
165 1.3.2.2 jdolecek }
166 1.3.2.2 jdolecek
167 1.3.2.2 jdolecek if (psci_fdt_init(phandle) != 0)
168 1.3.2.2 jdolecek return;
169 1.3.2.2 jdolecek
170 1.3.2.2 jdolecek /* MPIDR affinity levels of boot processor. */
171 1.3.2.2 jdolecek bp_mpidr = armreg_mpidr_read() & (MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0);
172 1.3.2.2 jdolecek
173 1.3.2.2 jdolecek /* Boot APs */
174 1.3.2.2 jdolecek uint32_t started = 0;
175 1.3.2.2 jdolecek for (child = OF_child(cpus); child; child = OF_peer(child)) {
176 1.3.2.2 jdolecek if (!fdtbus_status_okay(child))
177 1.3.2.2 jdolecek continue;
178 1.3.2.2 jdolecek if (fdtbus_get_reg(child, 0, &mpidr, NULL) != 0)
179 1.3.2.2 jdolecek continue;
180 1.3.2.2 jdolecek if (mpidr == bp_mpidr)
181 1.3.2.2 jdolecek continue; /* BP already started */
182 1.3.2.2 jdolecek
183 1.3.2.2 jdolecek /* XXX NetBSD requires all CPUs to be in the same cluster */
184 1.3.2.2 jdolecek const u_int bp_clid = __SHIFTOUT(bp_mpidr, CORTEXA9_MPIDR_CLID);
185 1.3.2.2 jdolecek const u_int clid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CLID);
186 1.3.2.2 jdolecek if (bp_clid != clid)
187 1.3.2.2 jdolecek continue;
188 1.3.2.2 jdolecek
189 1.3.2.2 jdolecek const u_int cpuid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CPUID);
190 1.3.2.2 jdolecek int ret = psci_cpu_on(cpuid, (register_t)cortex_mpstart, 0);
191 1.3.2.2 jdolecek if (ret == PSCI_SUCCESS)
192 1.3.2.2 jdolecek started |= __BIT(cpuid);
193 1.3.2.2 jdolecek }
194 1.3.2.2 jdolecek
195 1.3.2.2 jdolecek /* Wait for APs to start */
196 1.3.2.2 jdolecek for (u_int i = 0x10000000; i > 0; i--) {
197 1.3.2.2 jdolecek arm_dmb();
198 1.3.2.2 jdolecek if (arm_cpu_hatched == started)
199 1.3.2.2 jdolecek break;
200 1.3.2.2 jdolecek }
201 1.3.2.2 jdolecek #endif
202 1.3.2.2 jdolecek }
203