1 /* $NetBSD: acpi_fdt.c,v 1.19 2021/04/24 23:36:26 thorpej 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.19 2021/04/24 23:36:26 thorpej 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 #endif 58 59 static int acpi_fdt_match(device_t, cfdata_t, void *); 60 static void acpi_fdt_attach(device_t, device_t, void *); 61 62 static void acpi_fdt_poweroff(device_t); 63 64 static void acpi_fdt_sysctl_init(void); 65 66 extern struct arm32_bus_dma_tag acpi_coherent_dma_tag; 67 68 static uint64_t smbios_table = 0; 69 70 static const struct device_compatible_entry compat_data[] = { 71 { .compat = "netbsd,acpi" }, 72 DEVICE_COMPAT_EOL 73 }; 74 75 static const struct fdtbus_power_controller_func acpi_fdt_power_funcs = { 76 .poweroff = acpi_fdt_poweroff, 77 }; 78 79 CFATTACH_DECL_NEW(acpi_fdt, 0, acpi_fdt_match, acpi_fdt_attach, NULL, NULL); 80 81 static int 82 acpi_fdt_match(device_t parent, cfdata_t cf, void *aux) 83 { 84 struct fdt_attach_args * const faa = aux; 85 86 return of_compatible_match(faa->faa_phandle, compat_data); 87 } 88 89 static void 90 acpi_fdt_attach(device_t parent, device_t self, void *aux) 91 { 92 struct fdt_attach_args * const faa = aux; 93 struct acpibus_attach_args aa; 94 95 aprint_naive("\n"); 96 aprint_normal("\n"); 97 98 fdtbus_register_power_controller(self, faa->faa_phandle, 99 &acpi_fdt_power_funcs); 100 101 if (!acpi_probe()) 102 panic("ACPI subsystem failed to initialize"); 103 104 memset(&aa, 0, sizeof(aa)); 105 #if NPCI > 0 106 aa.aa_pciflags = 107 /*PCI_FLAGS_IO_OKAY |*/ PCI_FLAGS_MEM_OKAY | 108 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | 109 PCI_FLAGS_MWI_OKAY; 110 #ifdef __HAVE_PCI_MSI_MSIX 111 aa.aa_pciflags |= PCI_FLAGS_MSI_OKAY | PCI_FLAGS_MSIX_OKAY; 112 #endif 113 #endif 114 115 aa.aa_memt = faa->faa_bst; 116 aa.aa_dmat = NULL; 117 aa.aa_dmat64 = NULL; 118 config_found(self, &aa, NULL, CFARG_EOL); 119 120 acpi_fdt_sysctl_init(); 121 } 122 123 static void 124 acpi_fdt_poweroff(device_t dev) 125 { 126 delay(500000); 127 #ifdef EFI_RUNTIME 128 if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0) 129 return; 130 #endif 131 if (psci_available()) 132 psci_system_off(); 133 } 134 135 static void 136 acpi_fdt_sysctl_init(void) 137 { 138 const struct sysctlnode *rnode; 139 int error; 140 141 const int chosen = OF_finddevice("/chosen"); 142 if (chosen >= 0) 143 of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table); 144 145 error = sysctl_createv(NULL, 0, NULL, &rnode, 146 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL, 147 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL); 148 if (error) 149 return; 150 151 if (smbios_table != 0) { 152 (void)sysctl_createv(NULL, 0, &rnode, NULL, 153 CTLFLAG_PERMANENT | CTLFLAG_READONLY | CTLFLAG_HEX, CTLTYPE_QUAD, 154 "smbios", SYSCTL_DESCR("SMBIOS table pointer"), 155 NULL, 0, &smbios_table, sizeof(smbios_table), 156 CTL_CREATE, CTL_EOL); 157 } 158 } 159