1 /* $NetBSD: acpi_fdt.c,v 1.14 2020/01/17 17:06:33 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2015-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 "pci.h" 30 #include "opt_efi.h" 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: acpi_fdt.c,v 1.14 2020/01/17 17:06:33 jmcneill Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/device.h> 38 #include <sys/intr.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/lwp.h> 42 #include <sys/kmem.h> 43 #include <sys/queue.h> 44 #include <sys/sysctl.h> 45 46 #include <dev/fdt/fdtvar.h> 47 48 #include <dev/acpi/acpivar.h> 49 #include <dev/pci/pcivar.h> 50 51 #include <arm/arm/psci.h> 52 53 #include <arm/acpi/acpi_pci_machdep.h> 54 55 #ifdef EFI_RUNTIME 56 #include <arm/arm/efi_runtime.h> 57 #include <dev/clock_subr.h> 58 #endif 59 60 static int acpi_fdt_match(device_t, cfdata_t, void *); 61 static void acpi_fdt_attach(device_t, device_t, void *); 62 63 static void acpi_fdt_poweroff(device_t); 64 65 #ifdef EFI_RUNTIME 66 static void acpi_fdt_efi_init(device_t); 67 static int acpi_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *); 68 static int acpi_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *); 69 #endif 70 71 static void acpi_fdt_sysctl_init(void); 72 73 extern struct arm32_bus_dma_tag acpi_coherent_dma_tag; 74 75 static uint64_t smbios_table = 0; 76 77 #ifdef EFI_RUNTIME 78 static struct todr_chip_handle efi_todr; 79 #endif 80 81 static const char * const compatible[] = { 82 "netbsd,acpi", 83 NULL 84 }; 85 86 static const struct fdtbus_power_controller_func acpi_fdt_power_funcs = { 87 .poweroff = acpi_fdt_poweroff, 88 }; 89 90 CFATTACH_DECL_NEW(acpi_fdt, 0, acpi_fdt_match, acpi_fdt_attach, NULL, NULL); 91 92 static int 93 acpi_fdt_match(device_t parent, cfdata_t cf, void *aux) 94 { 95 struct fdt_attach_args * const faa = aux; 96 97 return of_compatible(faa->faa_phandle, compatible) >= 0; 98 } 99 100 static void 101 acpi_fdt_attach(device_t parent, device_t self, void *aux) 102 { 103 struct fdt_attach_args * const faa = aux; 104 struct acpibus_attach_args aa; 105 106 aprint_naive("\n"); 107 aprint_normal("\n"); 108 109 #ifdef EFI_RUNTIME 110 acpi_fdt_efi_init(self); 111 #endif 112 113 fdtbus_register_power_controller(self, faa->faa_phandle, 114 &acpi_fdt_power_funcs); 115 116 if (!acpi_probe()) 117 aprint_error_dev(self, "failed to probe ACPI\n"); 118 119 memset(&aa, 0, sizeof(aa)); 120 #if NPCI > 0 121 aa.aa_pciflags = 122 /*PCI_FLAGS_IO_OKAY |*/ PCI_FLAGS_MEM_OKAY | 123 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | 124 PCI_FLAGS_MWI_OKAY; 125 #ifdef __HAVE_PCI_MSI_MSIX 126 aa.aa_pciflags |= PCI_FLAGS_MSI_OKAY | PCI_FLAGS_MSIX_OKAY; 127 #endif 128 #endif 129 130 aa.aa_memt = faa->faa_bst; 131 aa.aa_dmat = NULL; 132 aa.aa_dmat64 = NULL; 133 config_found_ia(self, "acpibus", &aa, 0); 134 135 acpi_fdt_sysctl_init(); 136 } 137 138 static void 139 acpi_fdt_poweroff(device_t dev) 140 { 141 delay(500000); 142 #ifdef EFI_RUNTIME 143 if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0) 144 return; 145 #endif 146 if (psci_available()) 147 psci_system_off(); 148 } 149 150 static void 151 acpi_fdt_sysctl_init(void) 152 { 153 const struct sysctlnode *rnode; 154 int error; 155 156 const int chosen = OF_finddevice("/chosen"); 157 if (chosen >= 0) 158 of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table); 159 160 error = sysctl_createv(NULL, 0, NULL, &rnode, 161 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL, 162 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL); 163 if (error) 164 return; 165 166 if (smbios_table != 0) { 167 (void)sysctl_createv(NULL, 0, &rnode, NULL, 168 CTLFLAG_PERMANENT | CTLFLAG_READONLY | CTLFLAG_HEX, CTLTYPE_QUAD, 169 "smbios", SYSCTL_DESCR("SMBIOS table pointer"), 170 NULL, 0, &smbios_table, sizeof(smbios_table), 171 CTL_CREATE, CTL_EOL); 172 } 173 } 174 175 #ifdef EFI_RUNTIME 176 static void 177 acpi_fdt_efi_init(device_t dev) 178 { 179 uint64_t efi_system_table; 180 struct efi_tm tm; 181 int error; 182 183 const int chosen = OF_finddevice("/chosen"); 184 if (chosen < 0) 185 return; 186 187 if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0) 188 return; 189 190 error = arm_efirt_init(efi_system_table); 191 if (error) 192 return; 193 194 aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table); 195 196 if (arm_efirt_gettime(&tm) == 0) { 197 aprint_normal_dev(dev, "using EFI runtime services for RTC\n"); 198 efi_todr.cookie = NULL; 199 efi_todr.todr_gettime_ymdhms = acpi_fdt_efi_rtc_gettime; 200 efi_todr.todr_settime_ymdhms = acpi_fdt_efi_rtc_settime; 201 todr_attach(&efi_todr); 202 } 203 } 204 205 static int 206 acpi_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt) 207 { 208 struct efi_tm tm; 209 int error; 210 211 error = arm_efirt_gettime(&tm); 212 if (error) 213 return error; 214 215 dt->dt_year = tm.tm_year; 216 dt->dt_mon = tm.tm_mon; 217 dt->dt_day = tm.tm_mday; 218 dt->dt_wday = 0; 219 dt->dt_hour = tm.tm_hour; 220 dt->dt_min = tm.tm_min; 221 dt->dt_sec = tm.tm_sec; 222 223 return 0; 224 } 225 226 static int 227 acpi_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt) 228 { 229 struct efi_tm tm; 230 231 memset(&tm, 0, sizeof(tm)); 232 tm.tm_year = dt->dt_year; 233 tm.tm_mon = dt->dt_mon; 234 tm.tm_mday = dt->dt_day; 235 tm.tm_hour = dt->dt_hour; 236 tm.tm_min = dt->dt_min; 237 tm.tm_sec = dt->dt_sec; 238 239 return arm_efirt_settime(&tm); 240 } 241 #endif 242