Home | History | Annotate | Line # | Download | only in fdt
acpi_fdt.c revision 1.23
      1  1.23   thorpej /* $NetBSD: acpi_fdt.c,v 1.23 2021/08/07 16:18:43 thorpej Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.9  jmcneill #include "pci.h"
     30   1.7  jmcneill #include "opt_efi.h"
     31   1.7  jmcneill 
     32   1.1  jmcneill #include <sys/cdefs.h>
     33  1.23   thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_fdt.c,v 1.23 2021/08/07 16:18:43 thorpej Exp $");
     34   1.1  jmcneill 
     35   1.1  jmcneill #include <sys/param.h>
     36   1.1  jmcneill #include <sys/bus.h>
     37   1.1  jmcneill #include <sys/device.h>
     38   1.1  jmcneill #include <sys/intr.h>
     39   1.1  jmcneill #include <sys/systm.h>
     40   1.1  jmcneill #include <sys/kernel.h>
     41   1.1  jmcneill #include <sys/lwp.h>
     42   1.1  jmcneill #include <sys/kmem.h>
     43   1.1  jmcneill #include <sys/queue.h>
     44   1.6  jmcneill #include <sys/sysctl.h>
     45   1.1  jmcneill 
     46   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     47   1.1  jmcneill 
     48   1.1  jmcneill #include <dev/acpi/acpivar.h>
     49   1.1  jmcneill #include <dev/pci/pcivar.h>
     50  1.20  jmcneill #include <dev/smbiosvar.h>
     51   1.1  jmcneill 
     52   1.2  jmcneill #include <arm/arm/psci.h>
     53   1.2  jmcneill 
     54   1.3  jmcneill #include <arm/acpi/acpi_pci_machdep.h>
     55   1.3  jmcneill 
     56   1.7  jmcneill #ifdef EFI_RUNTIME
     57   1.7  jmcneill #include <arm/arm/efi_runtime.h>
     58   1.7  jmcneill #endif
     59   1.7  jmcneill 
     60   1.1  jmcneill static int	acpi_fdt_match(device_t, cfdata_t, void *);
     61   1.1  jmcneill static void	acpi_fdt_attach(device_t, device_t, void *);
     62   1.1  jmcneill 
     63   1.2  jmcneill static void	acpi_fdt_poweroff(device_t);
     64   1.2  jmcneill 
     65  1.20  jmcneill static void	acpi_fdt_smbios_init(device_t);
     66   1.6  jmcneill static void	acpi_fdt_sysctl_init(void);
     67   1.6  jmcneill 
     68  1.12  jmcneill extern struct arm32_bus_dma_tag acpi_coherent_dma_tag;
     69  1.10  jmcneill 
     70   1.6  jmcneill static uint64_t smbios_table = 0;
     71   1.6  jmcneill 
     72  1.18   thorpej static const struct device_compatible_entry compat_data[] = {
     73  1.18   thorpej 	{ .compat = "netbsd,acpi" },
     74  1.18   thorpej 	DEVICE_COMPAT_EOL
     75   1.1  jmcneill };
     76   1.1  jmcneill 
     77   1.2  jmcneill static const struct fdtbus_power_controller_func acpi_fdt_power_funcs = {
     78   1.2  jmcneill 	.poweroff = acpi_fdt_poweroff,
     79   1.2  jmcneill };
     80   1.2  jmcneill 
     81   1.1  jmcneill CFATTACH_DECL_NEW(acpi_fdt, 0, acpi_fdt_match, acpi_fdt_attach, NULL, NULL);
     82   1.1  jmcneill 
     83   1.1  jmcneill static int
     84   1.1  jmcneill acpi_fdt_match(device_t parent, cfdata_t cf, void *aux)
     85   1.1  jmcneill {
     86   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     87   1.1  jmcneill 
     88  1.18   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     89   1.1  jmcneill }
     90   1.1  jmcneill 
     91   1.1  jmcneill static void
     92   1.1  jmcneill acpi_fdt_attach(device_t parent, device_t self, void *aux)
     93   1.1  jmcneill {
     94  1.20  jmcneill 	extern void platform_init(void); /* XXX */
     95   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     96   1.1  jmcneill 	struct acpibus_attach_args aa;
     97   1.1  jmcneill 
     98   1.1  jmcneill 	aprint_naive("\n");
     99   1.7  jmcneill 	aprint_normal("\n");
    100   1.7  jmcneill 
    101  1.20  jmcneill 	acpi_fdt_smbios_init(self);
    102  1.20  jmcneill 	platform_init();
    103  1.20  jmcneill 
    104   1.2  jmcneill 	fdtbus_register_power_controller(self, faa->faa_phandle,
    105   1.2  jmcneill 	    &acpi_fdt_power_funcs);
    106   1.2  jmcneill 
    107   1.1  jmcneill 	if (!acpi_probe())
    108  1.15  jmcneill 		panic("ACPI subsystem failed to initialize");
    109   1.1  jmcneill 
    110  1.20  jmcneill 	platform_init();
    111  1.20  jmcneill 
    112   1.9  jmcneill 	memset(&aa, 0, sizeof(aa));
    113   1.9  jmcneill #if NPCI > 0
    114   1.1  jmcneill 	aa.aa_pciflags =
    115   1.3  jmcneill 	    /*PCI_FLAGS_IO_OKAY |*/ PCI_FLAGS_MEM_OKAY |
    116  1.17  jmcneill 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY |
    117   1.1  jmcneill 	    PCI_FLAGS_MWI_OKAY;
    118   1.4  jmcneill #ifdef __HAVE_PCI_MSI_MSIX
    119   1.8  jmcneill 	aa.aa_pciflags |= PCI_FLAGS_MSI_OKAY | PCI_FLAGS_MSIX_OKAY;
    120   1.4  jmcneill #endif
    121   1.9  jmcneill #endif
    122   1.9  jmcneill 
    123   1.9  jmcneill 	aa.aa_memt = faa->faa_bst;
    124  1.13  jmcneill 	aa.aa_dmat = NULL;
    125  1.13  jmcneill 	aa.aa_dmat64 = NULL;
    126  1.23   thorpej 	config_found(self, &aa, NULL, CFARGS_NONE);
    127   1.6  jmcneill 
    128   1.6  jmcneill 	acpi_fdt_sysctl_init();
    129   1.1  jmcneill }
    130   1.2  jmcneill 
    131   1.2  jmcneill static void
    132   1.2  jmcneill acpi_fdt_poweroff(device_t dev)
    133   1.2  jmcneill {
    134   1.2  jmcneill 	delay(500000);
    135   1.7  jmcneill #ifdef EFI_RUNTIME
    136   1.7  jmcneill 	if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0)
    137   1.7  jmcneill 		return;
    138   1.7  jmcneill #endif
    139   1.2  jmcneill 	if (psci_available())
    140   1.2  jmcneill 		psci_system_off();
    141   1.2  jmcneill }
    142   1.6  jmcneill 
    143  1.21  jmcneill static int
    144  1.21  jmcneill acpi_fdt_smbios_version(void)
    145  1.21  jmcneill {
    146  1.21  jmcneill 	uint8_t *hdr;
    147  1.21  jmcneill 	int smbver;
    148  1.21  jmcneill 
    149  1.21  jmcneill 	if (smbios_table == 0) {
    150  1.21  jmcneill 		return 0;
    151  1.21  jmcneill 	}
    152  1.21  jmcneill 
    153  1.21  jmcneill 	hdr = AcpiOsMapMemory(smbios_table, 24);
    154  1.21  jmcneill 	if (hdr == NULL) {
    155  1.21  jmcneill 		return 0;
    156  1.21  jmcneill 	}
    157  1.21  jmcneill 	if (smbios3_check_header(hdr)) {
    158  1.21  jmcneill 		smbver = 3;
    159  1.21  jmcneill 	} else if (smbios2_check_header(hdr)) {
    160  1.21  jmcneill 		smbver = 2;
    161  1.21  jmcneill 	} else {
    162  1.21  jmcneill 		smbver = 0;
    163  1.21  jmcneill 	}
    164  1.21  jmcneill 	AcpiOsUnmapMemory(hdr, 24);
    165  1.21  jmcneill 	return smbver;
    166  1.21  jmcneill }
    167  1.21  jmcneill 
    168   1.6  jmcneill static void
    169  1.20  jmcneill acpi_fdt_smbios_init(device_t dev)
    170  1.20  jmcneill {
    171  1.20  jmcneill 	uint8_t *ptr;
    172  1.21  jmcneill 	int smbver;
    173  1.20  jmcneill 
    174  1.20  jmcneill 	const int chosen = OF_finddevice("/chosen");
    175  1.20  jmcneill 	if (chosen >= 0) {
    176  1.20  jmcneill 		of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table);
    177  1.20  jmcneill 	}
    178  1.20  jmcneill 	if (smbios_table == 0) {
    179  1.20  jmcneill 		return;
    180  1.20  jmcneill 	}
    181  1.20  jmcneill 
    182  1.22  jmcneill 	smbios_entry.hdrphys = smbios_table;
    183  1.22  jmcneill 
    184  1.21  jmcneill 	smbver = acpi_fdt_smbios_version();
    185  1.21  jmcneill 	if (smbver == 3) {
    186  1.21  jmcneill 		struct smb3hdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
    187  1.21  jmcneill 		if (sh == NULL) {
    188  1.21  jmcneill 			return;
    189  1.21  jmcneill 		}
    190  1.21  jmcneill 
    191  1.21  jmcneill 		ptr = AcpiOsMapMemory(sh->addr, sh->size);
    192  1.21  jmcneill 		if (ptr != NULL) {
    193  1.22  jmcneill 			smbios_entry.tabphys = sh->addr;
    194  1.21  jmcneill 			smbios_entry.addr = ptr;
    195  1.21  jmcneill 			smbios_entry.len = sh->size;
    196  1.21  jmcneill 			smbios_entry.rev = sh->eprev;
    197  1.21  jmcneill 			smbios_entry.mjr = sh->majrev;
    198  1.21  jmcneill 			smbios_entry.min = sh->minrev;
    199  1.21  jmcneill 			smbios_entry.doc = sh->docrev;
    200  1.21  jmcneill 			smbios_entry.count = UINT16_MAX;
    201  1.21  jmcneill 		}
    202  1.21  jmcneill 
    203  1.21  jmcneill 		aprint_normal_dev(dev, "SMBIOS rev. %d.%d.%d @ 0x%lx\n",
    204  1.21  jmcneill 		    sh->majrev, sh->minrev, sh->docrev, (u_long)sh->addr);
    205  1.20  jmcneill 		AcpiOsUnmapMemory(sh, sizeof(*sh));
    206  1.21  jmcneill 	} else if (smbver == 2) {
    207  1.21  jmcneill 		struct smbhdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
    208  1.21  jmcneill 		if (sh == NULL) {
    209  1.21  jmcneill 			return;
    210  1.21  jmcneill 		}
    211  1.21  jmcneill 
    212  1.21  jmcneill 		ptr = AcpiOsMapMemory(sh->addr, sh->size);
    213  1.21  jmcneill 		if (ptr != NULL) {
    214  1.22  jmcneill 			smbios_entry.tabphys = sh->addr;
    215  1.21  jmcneill 			smbios_entry.addr = ptr;
    216  1.21  jmcneill 			smbios_entry.len = sh->size;
    217  1.21  jmcneill 			smbios_entry.rev = 0;
    218  1.21  jmcneill 			smbios_entry.mjr = sh->majrev;
    219  1.21  jmcneill 			smbios_entry.min = sh->minrev;
    220  1.21  jmcneill 			smbios_entry.doc = 0;
    221  1.21  jmcneill 			smbios_entry.count = sh->count;
    222  1.21  jmcneill 		}
    223  1.20  jmcneill 
    224  1.21  jmcneill 		aprint_normal_dev(dev, "SMBIOS rev. %d.%d @ 0x%lx (%d entries)\n",
    225  1.21  jmcneill 		    sh->majrev, sh->minrev, (u_long)sh->addr, sh->count);
    226  1.21  jmcneill 		AcpiOsUnmapMemory(sh, sizeof(*sh));
    227  1.20  jmcneill 	}
    228  1.20  jmcneill }
    229  1.20  jmcneill 
    230  1.20  jmcneill static void
    231   1.6  jmcneill acpi_fdt_sysctl_init(void)
    232   1.6  jmcneill {
    233   1.6  jmcneill 	const struct sysctlnode *rnode;
    234   1.6  jmcneill 	int error;
    235   1.6  jmcneill 
    236  1.20  jmcneill 	if (smbios_table == 0) {
    237  1.20  jmcneill 		return;
    238  1.20  jmcneill 	}
    239   1.6  jmcneill 
    240   1.6  jmcneill 	error = sysctl_createv(NULL, 0, NULL, &rnode,
    241   1.6  jmcneill 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    242   1.6  jmcneill 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    243  1.20  jmcneill 	if (error) {
    244   1.6  jmcneill 		return;
    245  1.20  jmcneill 	}
    246   1.6  jmcneill 
    247  1.20  jmcneill 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
    248  1.20  jmcneill 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY | CTLFLAG_HEX, CTLTYPE_QUAD,
    249  1.20  jmcneill 	    "smbios", SYSCTL_DESCR("SMBIOS table pointer"),
    250  1.20  jmcneill 	    NULL, 0, &smbios_table, sizeof(smbios_table),
    251  1.20  jmcneill 	    CTL_CREATE, CTL_EOL);
    252   1.6  jmcneill }
    253