xref: /netbsd-src/sys/arch/arm/fdt/acpi_fdt.c (revision 07ede9138182120d2cfa2ddb484613c658d0e777)
1 /* $NetBSD: acpi_fdt.c,v 1.25 2024/12/08 20:55:18 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.25 2024/12/08 20:55:18 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 
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 
132 	if (psci_available()) {
133 		psci_system_off();
134 	}
135 
136 #ifdef EFI_RUNTIME
137 	arm_efirt_reset(EFI_RESET_SHUTDOWN);
138 #endif
139 }
140 
141 static int
142 acpi_fdt_smbios_version(void)
143 {
144 	uint8_t *hdr;
145 	int smbver;
146 
147 	if (smbios_table == 0) {
148 		return 0;
149 	}
150 
151 	hdr = AcpiOsMapMemory(smbios_table, 24);
152 	if (hdr == NULL) {
153 		return 0;
154 	}
155 	if (smbios3_check_header(hdr)) {
156 		smbver = 3;
157 	} else if (smbios2_check_header(hdr)) {
158 		smbver = 2;
159 	} else {
160 		smbver = 0;
161 	}
162 	AcpiOsUnmapMemory(hdr, 24);
163 	return smbver;
164 }
165 
166 static void
167 acpi_fdt_smbios_init(device_t dev)
168 {
169 	uint8_t *ptr;
170 	int smbver;
171 
172 	const int chosen = OF_finddevice("/chosen");
173 	if (chosen >= 0) {
174 		of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table);
175 	}
176 	if (smbios_table == 0) {
177 		return;
178 	}
179 
180 	smbios_entry.hdrphys = smbios_table;
181 
182 	smbver = acpi_fdt_smbios_version();
183 	if (smbver == 3) {
184 		struct smb3hdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
185 		if (sh == NULL) {
186 			return;
187 		}
188 
189 		ptr = AcpiOsMapMemory(sh->addr, sh->size);
190 		if (ptr != NULL) {
191 			smbios_entry.tabphys = sh->addr;
192 			smbios_entry.addr = ptr;
193 			smbios_entry.len = sh->size;
194 			smbios_entry.rev = sh->eprev;
195 			smbios_entry.mjr = sh->majrev;
196 			smbios_entry.min = sh->minrev;
197 			smbios_entry.doc = sh->docrev;
198 			smbios_entry.count = UINT16_MAX;
199 		}
200 
201 		aprint_normal_dev(dev, "SMBIOS rev. %d.%d.%d @ 0x%lx\n",
202 		    sh->majrev, sh->minrev, sh->docrev, (u_long)sh->addr);
203 		AcpiOsUnmapMemory(sh, sizeof(*sh));
204 	} else if (smbver == 2) {
205 		struct smbhdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
206 		if (sh == NULL) {
207 			return;
208 		}
209 
210 		ptr = AcpiOsMapMemory(sh->addr, sh->size);
211 		if (ptr != NULL) {
212 			smbios_entry.tabphys = sh->addr;
213 			smbios_entry.addr = ptr;
214 			smbios_entry.len = sh->size;
215 			smbios_entry.rev = 0;
216 			smbios_entry.mjr = sh->majrev;
217 			smbios_entry.min = sh->minrev;
218 			smbios_entry.doc = 0;
219 			smbios_entry.count = sh->count;
220 		}
221 
222 		aprint_normal_dev(dev, "SMBIOS rev. %d.%d @ 0x%lx (%d entries)\n",
223 		    sh->majrev, sh->minrev, (u_long)sh->addr, sh->count);
224 		AcpiOsUnmapMemory(sh, sizeof(*sh));
225 	}
226 }
227