1 /* $NetBSD: psci_fdt.c,v 1.3 2017/09/11 09:21:56 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.3 2017/09/11 09:21:56 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_reset(device_t dev) 64 { 65 delay(500000); 66 psci_system_reset(); 67 } 68 69 static void 70 psci_fdt_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_reset, 78 .poweroff = psci_fdt_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 void 140 psci_fdt_bootstrap(void) 141 { 142 #ifdef MULTIPROCESSOR 143 extern void cortex_mpstart(void); 144 bus_addr_t mpidr; 145 uint32_t bp_mpidr; 146 int child; 147 148 const int cpus = OF_finddevice("/cpus"); 149 if (cpus == -1) { 150 aprint_error("PSCI: no /cpus node found\n"); 151 arm_cpu_max = 1; 152 return; 153 } 154 155 /* Count CPUs */ 156 arm_cpu_max = 0; 157 for (child = OF_child(cpus); child; child = OF_peer(child)) 158 if (fdtbus_status_okay(child)) 159 arm_cpu_max++; 160 161 const int phandle = OF_finddevice("/psci"); 162 if (phandle == -1) { 163 aprint_error("PSCI: no /psci node found\n"); 164 return; 165 } 166 167 if (psci_fdt_init(phandle) != 0) 168 return; 169 170 /* MPIDR affinity levels of boot processor. */ 171 bp_mpidr = armreg_mpidr_read() & (MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0); 172 173 /* Boot APs */ 174 uint32_t started = 0; 175 for (child = OF_child(cpus); child; child = OF_peer(child)) { 176 if (!fdtbus_status_okay(child)) 177 continue; 178 if (fdtbus_get_reg(child, 0, &mpidr, NULL) != 0) 179 continue; 180 if (mpidr == bp_mpidr) 181 continue; /* BP already started */ 182 183 /* XXX NetBSD requires all CPUs to be in the same cluster */ 184 const u_int bp_clid = __SHIFTOUT(bp_mpidr, CORTEXA9_MPIDR_CLID); 185 const u_int clid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CLID); 186 if (bp_clid != clid) 187 continue; 188 189 const u_int cpuid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CPUID); 190 int ret = psci_cpu_on(cpuid, (register_t)cortex_mpstart, 0); 191 if (ret == PSCI_SUCCESS) 192 started |= __BIT(cpuid); 193 } 194 195 /* Wait for APs to start */ 196 for (u_int i = 0x10000000; i > 0; i--) { 197 arm_dmb(); 198 if (arm_cpu_hatched == started) 199 break; 200 } 201 #endif 202 } 203