acpi_fdt.c revision 1.13 1 /* $NetBSD: acpi_fdt.c,v 1.13 2019/12/29 23:47:56 jmcneill 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.13 2019/12/29 23:47:56 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 #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 #include <dev/clock_subr.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 #ifdef EFI_RUNTIME
66 static void acpi_fdt_efi_init(device_t);
67 static int acpi_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
68 static int acpi_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
69 #endif
70
71 static void acpi_fdt_sysctl_init(void);
72
73 extern struct arm32_bus_dma_tag acpi_coherent_dma_tag;
74
75 #if NPCI > 0
76 static struct acpi_pci_context acpi_fdt_pci_context;
77 #endif
78
79 static uint64_t smbios_table = 0;
80
81 #ifdef EFI_RUNTIME
82 static struct todr_chip_handle efi_todr;
83 #endif
84
85 static const char * const compatible[] = {
86 "netbsd,acpi",
87 NULL
88 };
89
90 static const struct fdtbus_power_controller_func acpi_fdt_power_funcs = {
91 .poweroff = acpi_fdt_poweroff,
92 };
93
94 CFATTACH_DECL_NEW(acpi_fdt, 0, acpi_fdt_match, acpi_fdt_attach, NULL, NULL);
95
96 static int
97 acpi_fdt_match(device_t parent, cfdata_t cf, void *aux)
98 {
99 struct fdt_attach_args * const faa = aux;
100
101 return of_compatible(faa->faa_phandle, compatible) >= 0;
102 }
103
104 static void
105 acpi_fdt_attach(device_t parent, device_t self, void *aux)
106 {
107 struct fdt_attach_args * const faa = aux;
108 struct acpibus_attach_args aa;
109
110 aprint_naive("\n");
111 aprint_normal("\n");
112
113 #ifdef EFI_RUNTIME
114 acpi_fdt_efi_init(self);
115 #endif
116
117 fdtbus_register_power_controller(self, faa->faa_phandle,
118 &acpi_fdt_power_funcs);
119
120 if (!acpi_probe())
121 aprint_error_dev(self, "failed to probe ACPI\n");
122
123 memset(&aa, 0, sizeof(aa));
124 #if NPCI > 0
125 acpi_fdt_pci_context.ap_pc = arm_acpi_pci_chipset;
126 acpi_fdt_pci_context.ap_pc.pc_conf_v = &acpi_fdt_pci_context;
127 acpi_fdt_pci_context.ap_seg = 0;
128 aa.aa_pciflags =
129 /*PCI_FLAGS_IO_OKAY |*/ PCI_FLAGS_MEM_OKAY |
130 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY |
131 PCI_FLAGS_MWI_OKAY;
132 #ifdef __HAVE_PCI_MSI_MSIX
133 aa.aa_pciflags |= PCI_FLAGS_MSI_OKAY | PCI_FLAGS_MSIX_OKAY;
134 #endif
135 aa.aa_pc = &acpi_fdt_pci_context.ap_pc;
136 #endif
137
138 aa.aa_memt = faa->faa_bst;
139 aa.aa_dmat = NULL;
140 aa.aa_dmat64 = NULL;
141 config_found_ia(self, "acpibus", &aa, 0);
142
143 acpi_fdt_sysctl_init();
144 }
145
146 static void
147 acpi_fdt_poweroff(device_t dev)
148 {
149 delay(500000);
150 #ifdef EFI_RUNTIME
151 if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0)
152 return;
153 #endif
154 if (psci_available())
155 psci_system_off();
156 }
157
158 static void
159 acpi_fdt_sysctl_init(void)
160 {
161 const struct sysctlnode *rnode;
162 int error;
163
164 const int chosen = OF_finddevice("/chosen");
165 if (chosen >= 0)
166 of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table);
167
168 error = sysctl_createv(NULL, 0, NULL, &rnode,
169 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
170 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
171 if (error)
172 return;
173
174 if (smbios_table != 0) {
175 (void)sysctl_createv(NULL, 0, &rnode, NULL,
176 CTLFLAG_PERMANENT | CTLFLAG_READONLY | CTLFLAG_HEX, CTLTYPE_QUAD,
177 "smbios", SYSCTL_DESCR("SMBIOS table pointer"),
178 NULL, 0, &smbios_table, sizeof(smbios_table),
179 CTL_CREATE, CTL_EOL);
180 }
181 }
182
183 #ifdef EFI_RUNTIME
184 static void
185 acpi_fdt_efi_init(device_t dev)
186 {
187 uint64_t efi_system_table;
188 struct efi_tm tm;
189 int error;
190
191 const int chosen = OF_finddevice("/chosen");
192 if (chosen < 0)
193 return;
194
195 if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
196 return;
197
198 error = arm_efirt_init(efi_system_table);
199 if (error)
200 return;
201
202 aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
203
204 if (arm_efirt_gettime(&tm) == 0) {
205 aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
206 efi_todr.cookie = NULL;
207 efi_todr.todr_gettime_ymdhms = acpi_fdt_efi_rtc_gettime;
208 efi_todr.todr_settime_ymdhms = acpi_fdt_efi_rtc_settime;
209 todr_attach(&efi_todr);
210 }
211 }
212
213 static int
214 acpi_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
215 {
216 struct efi_tm tm;
217 int error;
218
219 error = arm_efirt_gettime(&tm);
220 if (error)
221 return error;
222
223 dt->dt_year = tm.tm_year;
224 dt->dt_mon = tm.tm_mon;
225 dt->dt_day = tm.tm_mday;
226 dt->dt_wday = 0;
227 dt->dt_hour = tm.tm_hour;
228 dt->dt_min = tm.tm_min;
229 dt->dt_sec = tm.tm_sec;
230
231 return 0;
232 }
233
234 static int
235 acpi_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
236 {
237 struct efi_tm tm;
238
239 memset(&tm, 0, sizeof(tm));
240 tm.tm_year = dt->dt_year;
241 tm.tm_mon = dt->dt_mon;
242 tm.tm_mday = dt->dt_day;
243 tm.tm_hour = dt->dt_hour;
244 tm.tm_min = dt->dt_min;
245 tm.tm_sec = dt->dt_sec;
246
247 return arm_efirt_settime(&tm);
248 }
249 #endif
250