Home | History | Annotate | Line # | Download | only in fdt
acpi_fdt.c revision 1.24
      1 /* $NetBSD: acpi_fdt.c,v 1.24 2022/11/25 22:17:20 mrg Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) 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.24 2022/11/25 22:17:20 mrg 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 
     45 #include <dev/fdt/fdtvar.h>
     46 
     47 #include <dev/acpi/acpivar.h>
     48 #include <dev/pci/pcivar.h>
     49 #include <dev/smbiosvar.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_smbios_init(device_t);
     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 	extern void platform_init(void); /* XXX */
     93 	struct fdt_attach_args * const faa = aux;
     94 	struct acpibus_attach_args aa;
     95 
     96 	aprint_naive("\n");
     97 	aprint_normal("\n");
     98 
     99 	acpi_fdt_smbios_init(self);
    100 	platform_init();
    101 
    102 	fdtbus_register_power_controller(self, faa->faa_phandle,
    103 	    &acpi_fdt_power_funcs);
    104 
    105 	if (!acpi_probe())
    106 		panic("ACPI subsystem failed to initialize");
    107 
    108 	platform_init();
    109 
    110 	memset(&aa, 0, sizeof(aa));
    111 #if NPCI > 0
    112 	aa.aa_pciflags =
    113 	    /*PCI_FLAGS_IO_OKAY |*/ PCI_FLAGS_MEM_OKAY |
    114 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY |
    115 	    PCI_FLAGS_MWI_OKAY;
    116 #ifdef __HAVE_PCI_MSI_MSIX
    117 	aa.aa_pciflags |= PCI_FLAGS_MSI_OKAY | PCI_FLAGS_MSIX_OKAY;
    118 #endif
    119 #endif
    120 
    121 	aa.aa_memt = faa->faa_bst;
    122 	aa.aa_dmat = NULL;
    123 	aa.aa_dmat64 = NULL;
    124 	config_found(self, &aa, NULL, CFARGS_NONE);
    125 }
    126 
    127 static void
    128 acpi_fdt_poweroff(device_t dev)
    129 {
    130 	delay(500000);
    131 #ifdef EFI_RUNTIME
    132 	if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0)
    133 		return;
    134 #endif
    135 	if (psci_available())
    136 		psci_system_off();
    137 }
    138 
    139 static int
    140 acpi_fdt_smbios_version(void)
    141 {
    142 	uint8_t *hdr;
    143 	int smbver;
    144 
    145 	if (smbios_table == 0) {
    146 		return 0;
    147 	}
    148 
    149 	hdr = AcpiOsMapMemory(smbios_table, 24);
    150 	if (hdr == NULL) {
    151 		return 0;
    152 	}
    153 	if (smbios3_check_header(hdr)) {
    154 		smbver = 3;
    155 	} else if (smbios2_check_header(hdr)) {
    156 		smbver = 2;
    157 	} else {
    158 		smbver = 0;
    159 	}
    160 	AcpiOsUnmapMemory(hdr, 24);
    161 	return smbver;
    162 }
    163 
    164 static void
    165 acpi_fdt_smbios_init(device_t dev)
    166 {
    167 	uint8_t *ptr;
    168 	int smbver;
    169 
    170 	const int chosen = OF_finddevice("/chosen");
    171 	if (chosen >= 0) {
    172 		of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table);
    173 	}
    174 	if (smbios_table == 0) {
    175 		return;
    176 	}
    177 
    178 	smbios_entry.hdrphys = smbios_table;
    179 
    180 	smbver = acpi_fdt_smbios_version();
    181 	if (smbver == 3) {
    182 		struct smb3hdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
    183 		if (sh == NULL) {
    184 			return;
    185 		}
    186 
    187 		ptr = AcpiOsMapMemory(sh->addr, sh->size);
    188 		if (ptr != NULL) {
    189 			smbios_entry.tabphys = sh->addr;
    190 			smbios_entry.addr = ptr;
    191 			smbios_entry.len = sh->size;
    192 			smbios_entry.rev = sh->eprev;
    193 			smbios_entry.mjr = sh->majrev;
    194 			smbios_entry.min = sh->minrev;
    195 			smbios_entry.doc = sh->docrev;
    196 			smbios_entry.count = UINT16_MAX;
    197 		}
    198 
    199 		aprint_normal_dev(dev, "SMBIOS rev. %d.%d.%d @ 0x%lx\n",
    200 		    sh->majrev, sh->minrev, sh->docrev, (u_long)sh->addr);
    201 		AcpiOsUnmapMemory(sh, sizeof(*sh));
    202 	} else if (smbver == 2) {
    203 		struct smbhdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
    204 		if (sh == NULL) {
    205 			return;
    206 		}
    207 
    208 		ptr = AcpiOsMapMemory(sh->addr, sh->size);
    209 		if (ptr != NULL) {
    210 			smbios_entry.tabphys = sh->addr;
    211 			smbios_entry.addr = ptr;
    212 			smbios_entry.len = sh->size;
    213 			smbios_entry.rev = 0;
    214 			smbios_entry.mjr = sh->majrev;
    215 			smbios_entry.min = sh->minrev;
    216 			smbios_entry.doc = 0;
    217 			smbios_entry.count = sh->count;
    218 		}
    219 
    220 		aprint_normal_dev(dev, "SMBIOS rev. %d.%d @ 0x%lx (%d entries)\n",
    221 		    sh->majrev, sh->minrev, (u_long)sh->addr, sh->count);
    222 		AcpiOsUnmapMemory(sh, sizeof(*sh));
    223 	}
    224 }
    225