acpi_fdt.c revision 1.23 1 /* $NetBSD: acpi_fdt.c,v 1.23 2021/08/07 16:18:43 thorpej 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.23 2021/08/07 16:18:43 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 #include <dev/smbiosvar.h>
51
52 #include <arm/arm/psci.h>
53
54 #include <arm/acpi/acpi_pci_machdep.h>
55
56 #ifdef EFI_RUNTIME
57 #include <arm/arm/efi_runtime.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 static void acpi_fdt_smbios_init(device_t);
66 static void acpi_fdt_sysctl_init(void);
67
68 extern struct arm32_bus_dma_tag acpi_coherent_dma_tag;
69
70 static uint64_t smbios_table = 0;
71
72 static const struct device_compatible_entry compat_data[] = {
73 { .compat = "netbsd,acpi" },
74 DEVICE_COMPAT_EOL
75 };
76
77 static const struct fdtbus_power_controller_func acpi_fdt_power_funcs = {
78 .poweroff = acpi_fdt_poweroff,
79 };
80
81 CFATTACH_DECL_NEW(acpi_fdt, 0, acpi_fdt_match, acpi_fdt_attach, NULL, NULL);
82
83 static int
84 acpi_fdt_match(device_t parent, cfdata_t cf, void *aux)
85 {
86 struct fdt_attach_args * const faa = aux;
87
88 return of_compatible_match(faa->faa_phandle, compat_data);
89 }
90
91 static void
92 acpi_fdt_attach(device_t parent, device_t self, void *aux)
93 {
94 extern void platform_init(void); /* XXX */
95 struct fdt_attach_args * const faa = aux;
96 struct acpibus_attach_args aa;
97
98 aprint_naive("\n");
99 aprint_normal("\n");
100
101 acpi_fdt_smbios_init(self);
102 platform_init();
103
104 fdtbus_register_power_controller(self, faa->faa_phandle,
105 &acpi_fdt_power_funcs);
106
107 if (!acpi_probe())
108 panic("ACPI subsystem failed to initialize");
109
110 platform_init();
111
112 memset(&aa, 0, sizeof(aa));
113 #if NPCI > 0
114 aa.aa_pciflags =
115 /*PCI_FLAGS_IO_OKAY |*/ PCI_FLAGS_MEM_OKAY |
116 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY |
117 PCI_FLAGS_MWI_OKAY;
118 #ifdef __HAVE_PCI_MSI_MSIX
119 aa.aa_pciflags |= PCI_FLAGS_MSI_OKAY | PCI_FLAGS_MSIX_OKAY;
120 #endif
121 #endif
122
123 aa.aa_memt = faa->faa_bst;
124 aa.aa_dmat = NULL;
125 aa.aa_dmat64 = NULL;
126 config_found(self, &aa, NULL, CFARGS_NONE);
127
128 acpi_fdt_sysctl_init();
129 }
130
131 static void
132 acpi_fdt_poweroff(device_t dev)
133 {
134 delay(500000);
135 #ifdef EFI_RUNTIME
136 if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0)
137 return;
138 #endif
139 if (psci_available())
140 psci_system_off();
141 }
142
143 static int
144 acpi_fdt_smbios_version(void)
145 {
146 uint8_t *hdr;
147 int smbver;
148
149 if (smbios_table == 0) {
150 return 0;
151 }
152
153 hdr = AcpiOsMapMemory(smbios_table, 24);
154 if (hdr == NULL) {
155 return 0;
156 }
157 if (smbios3_check_header(hdr)) {
158 smbver = 3;
159 } else if (smbios2_check_header(hdr)) {
160 smbver = 2;
161 } else {
162 smbver = 0;
163 }
164 AcpiOsUnmapMemory(hdr, 24);
165 return smbver;
166 }
167
168 static void
169 acpi_fdt_smbios_init(device_t dev)
170 {
171 uint8_t *ptr;
172 int smbver;
173
174 const int chosen = OF_finddevice("/chosen");
175 if (chosen >= 0) {
176 of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table);
177 }
178 if (smbios_table == 0) {
179 return;
180 }
181
182 smbios_entry.hdrphys = smbios_table;
183
184 smbver = acpi_fdt_smbios_version();
185 if (smbver == 3) {
186 struct smb3hdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
187 if (sh == NULL) {
188 return;
189 }
190
191 ptr = AcpiOsMapMemory(sh->addr, sh->size);
192 if (ptr != NULL) {
193 smbios_entry.tabphys = sh->addr;
194 smbios_entry.addr = ptr;
195 smbios_entry.len = sh->size;
196 smbios_entry.rev = sh->eprev;
197 smbios_entry.mjr = sh->majrev;
198 smbios_entry.min = sh->minrev;
199 smbios_entry.doc = sh->docrev;
200 smbios_entry.count = UINT16_MAX;
201 }
202
203 aprint_normal_dev(dev, "SMBIOS rev. %d.%d.%d @ 0x%lx\n",
204 sh->majrev, sh->minrev, sh->docrev, (u_long)sh->addr);
205 AcpiOsUnmapMemory(sh, sizeof(*sh));
206 } else if (smbver == 2) {
207 struct smbhdr *sh = AcpiOsMapMemory(smbios_table, sizeof(*sh));
208 if (sh == NULL) {
209 return;
210 }
211
212 ptr = AcpiOsMapMemory(sh->addr, sh->size);
213 if (ptr != NULL) {
214 smbios_entry.tabphys = sh->addr;
215 smbios_entry.addr = ptr;
216 smbios_entry.len = sh->size;
217 smbios_entry.rev = 0;
218 smbios_entry.mjr = sh->majrev;
219 smbios_entry.min = sh->minrev;
220 smbios_entry.doc = 0;
221 smbios_entry.count = sh->count;
222 }
223
224 aprint_normal_dev(dev, "SMBIOS rev. %d.%d @ 0x%lx (%d entries)\n",
225 sh->majrev, sh->minrev, (u_long)sh->addr, sh->count);
226 AcpiOsUnmapMemory(sh, sizeof(*sh));
227 }
228 }
229
230 static void
231 acpi_fdt_sysctl_init(void)
232 {
233 const struct sysctlnode *rnode;
234 int error;
235
236 if (smbios_table == 0) {
237 return;
238 }
239
240 error = sysctl_createv(NULL, 0, NULL, &rnode,
241 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
242 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
243 if (error) {
244 return;
245 }
246
247 (void)sysctl_createv(NULL, 0, &rnode, NULL,
248 CTLFLAG_PERMANENT | CTLFLAG_READONLY | CTLFLAG_HEX, CTLTYPE_QUAD,
249 "smbios", SYSCTL_DESCR("SMBIOS table pointer"),
250 NULL, 0, &smbios_table, sizeof(smbios_table),
251 CTL_CREATE, CTL_EOL);
252 }
253