1 /* $NetBSD: psci_fdt.c,v 1.21 2021/08/06 19:38:53 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.21 2021/08/06 19:38:53 jmcneill Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/atomic.h> 36 #include <sys/bus.h> 37 #include <sys/device.h> 38 #include <sys/kernel.h> 39 #include <sys/systm.h> 40 41 #include <dev/fdt/fdtvar.h> 42 43 #include <arm/arm/smccc.h> 44 #include <arm/arm/psci.h> 45 #include <arm/fdt/psci_fdtvar.h> 46 47 static int psci_fdt_match(device_t, cfdata_t, void *); 48 static void psci_fdt_attach(device_t, device_t, void *); 49 50 static int psci_fdt_init(const int); 51 52 static const struct device_compatible_entry compat_data[] = { 53 { .compat = "arm,psci" }, 54 { .compat = "arm,psci-0.2" }, 55 { .compat = "arm,psci-1.0" }, 56 DEVICE_COMPAT_EOL 57 }; 58 59 CFATTACH_DECL_NEW(psci_fdt, 0, psci_fdt_match, psci_fdt_attach, NULL, NULL); 60 61 static void 62 psci_fdt_power_reset(device_t dev) 63 { 64 delay(500000); 65 psci_system_reset(); 66 } 67 68 static void 69 psci_fdt_power_poweroff(device_t dev) 70 { 71 delay(500000); 72 psci_system_off(); 73 } 74 75 static const struct fdtbus_power_controller_func psci_power_funcs = { 76 .reset = psci_fdt_power_reset, 77 .poweroff = psci_fdt_power_poweroff, 78 }; 79 80 static int 81 psci_fdt_match(device_t parent, cfdata_t cf, void *aux) 82 { 83 struct fdt_attach_args * const faa = aux; 84 85 return of_compatible_match(faa->faa_phandle, compat_data); 86 } 87 88 static void 89 psci_fdt_attach(device_t parent, device_t self, void *aux) 90 { 91 struct fdt_attach_args * const faa = aux; 92 const int phandle = faa->faa_phandle; 93 94 psci_fdt_init(phandle); 95 96 const uint32_t ver = psci_version(); 97 const u_int ver_maj = __SHIFTOUT(ver, PSCI_VERSION_MAJOR); 98 const u_int ver_min = __SHIFTOUT(ver, PSCI_VERSION_MINOR); 99 100 aprint_naive("\n"); 101 aprint_normal(": PSCI %u.%u\n", ver_maj, ver_min); 102 103 fdtbus_register_power_controller(self, phandle, 104 &psci_power_funcs); 105 } 106 107 static int 108 psci_fdt_init(const int phandle) 109 { 110 const char *method, *psciver; 111 uint32_t val; 112 113 method = fdtbus_get_string(phandle, "method"); 114 psciver = fdtbus_get_string(phandle, "compatible"); 115 if (method == NULL || psciver == NULL) { 116 aprint_error("PSCI: missing required property on /psci\n"); 117 return EINVAL; 118 } 119 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 /* 130 * If the first compatible string is "arm,psci" then we 131 * are dealing with PSCI 0.1 132 */ 133 if (strcmp(psciver, "arm,psci") == 0) { 134 psci_clearfunc(); 135 if (of_getprop_uint32(phandle, "cpu_on", &val) == 0) 136 psci_setfunc(PSCI_FUNC_CPU_ON, val); 137 } 138 139 smccc_probe(); 140 141 return 0; 142 } 143 144 int 145 psci_fdt_preinit(void) 146 { 147 const int phandle = OF_finddevice("/psci"); 148 if (phandle == -1) { 149 aprint_error("PSCI: no /psci node found\n"); 150 return ENODEV; 151 } 152 153 return psci_fdt_init(phandle); 154 } 155 156 void 157 psci_fdt_reset(void) 158 { 159 if (psci_fdt_preinit() != 0) { 160 aprint_error("PSCI: reset failed\n"); 161 return; 162 } 163 164 psci_system_reset(); 165 } 166