1 /* $NetBSD: psci_fdt.c,v 1.1 2017/06/28 23:48:22 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 <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v 1.1 2017/06/28 23:48:22 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/device.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 38 #include <dev/fdt/fdtvar.h> 39 40 #include <arm/locore.h> 41 #include <arm/armreg.h> 42 43 #include <arm/arm/psci.h> 44 #include <arm/fdt/psci_fdt.h> 45 46 static int psci_fdt_match(device_t, cfdata_t, void *); 47 static void psci_fdt_attach(device_t, device_t, void *); 48 49 static int psci_fdt_init(const int); 50 51 static const char * const compatible[] = { 52 "arm,psci", 53 "arm,psci-0.2", 54 "arm,psci-1.0", 55 NULL 56 }; 57 58 CFATTACH_DECL_NEW(psci_fdt, 0, psci_fdt_match, psci_fdt_attach, NULL, NULL); 59 60 static int 61 psci_fdt_match(device_t parent, cfdata_t cf, void *aux) 62 { 63 struct fdt_attach_args * const faa = aux; 64 65 return of_match_compatible(faa->faa_phandle, compatible); 66 } 67 68 static void 69 psci_fdt_attach(device_t parent, device_t self, void *aux) 70 { 71 struct fdt_attach_args * const faa = aux; 72 const int phandle = faa->faa_phandle; 73 74 psci_fdt_init(phandle); 75 76 const uint32_t ver = psci_version(); 77 const u_int ver_maj = __SHIFTOUT(ver, PSCI_VERSION_MAJOR); 78 const u_int ver_min = __SHIFTOUT(ver, PSCI_VERSION_MINOR); 79 80 aprint_naive("\n"); 81 aprint_normal(": PSCI %u.%u\n", ver_maj, ver_min); 82 } 83 84 static int 85 psci_fdt_init(const int phandle) 86 { 87 char method[4]; 88 uint32_t val; 89 90 if (!of_hasprop(phandle, "method")) { 91 aprint_error("PSCI: missing 'method' property\n"); 92 return EINVAL; 93 } 94 95 OF_getprop(phandle, "method", method, sizeof(method)); 96 if (strcmp(method, "smc") == 0) 97 psci_init(psci_call_smc); 98 else if (strcmp(method, "hvc") == 0) 99 psci_init(psci_call_hvc); 100 else { 101 aprint_error("PSCI: unsupported method '%s'\n", method); 102 return EINVAL; 103 } 104 105 const char * const compat_0_1[] = { "arm,psci", NULL }; 106 if (of_match_compatible(phandle, compat_0_1)) { 107 psci_clearfunc(); 108 if (of_getprop_uint32(phandle, "cpu_on", &val) == 0) 109 psci_setfunc(PSCI_FUNC_CPU_ON, val); 110 } 111 112 return 0; 113 } 114 115 void 116 psci_fdt_bootstrap(void) 117 { 118 extern void cortex_mpstart(void); 119 bus_addr_t mpidr; 120 uint32_t bp_mpidr; 121 int child; 122 123 const int cpus = OF_finddevice("/cpus"); 124 if (cpus == -1) { 125 aprint_error("PSCI: no /cpus node found\n"); 126 arm_cpu_max = 1; 127 return; 128 } 129 130 /* Count CPUs */ 131 arm_cpu_max = 0; 132 for (child = OF_child(cpus); child; child = OF_peer(child)) 133 if (fdtbus_status_okay(child)) 134 arm_cpu_max++; 135 136 const int phandle = OF_finddevice("/psci"); 137 if (phandle == -1) { 138 aprint_error("PSCI: no /psci node found\n"); 139 return; 140 } 141 142 if (psci_fdt_init(phandle) != 0) 143 return; 144 145 /* MPIDR affinity levels of boot processor. */ 146 bp_mpidr = armreg_mpidr_read() & (MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0); 147 148 /* Boot APs */ 149 uint32_t started = 0; 150 for (child = OF_child(cpus); child; child = OF_peer(child)) { 151 if (!fdtbus_status_okay(child)) 152 continue; 153 if (fdtbus_get_reg(child, 0, &mpidr, NULL) != 0) 154 continue; 155 if (mpidr == bp_mpidr) 156 continue; /* BP already started */ 157 158 /* XXX NetBSD requires all CPUs to be in the same cluster */ 159 const u_int bp_clid = __SHIFTOUT(bp_mpidr, CORTEXA9_MPIDR_CLID); 160 const u_int clid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CLID); 161 if (bp_clid != clid) 162 continue; 163 164 const u_int cpuid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CPUID); 165 int ret = psci_cpu_on(cpuid, (register_t)cortex_mpstart, 0); 166 if (ret == PSCI_SUCCESS) 167 started |= __BIT(cpuid); 168 } 169 170 /* Wait for APs to start */ 171 for (u_int i = 0x10000000; i > 0; i--) { 172 arm_dmb(); 173 if (arm_cpu_hatched == started) 174 break; 175 } 176 } 177