acpi_fdt.c revision 1.12 1 /* $NetBSD: acpi_fdt.c,v 1.12 2019/06/22 19:47:27 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.12 2019/06/22 19:47:27 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 = &acpi_coherent_dma_tag;
140 #ifdef _PCI_HAVE_DMA64
141 aa.aa_dmat64 = &acpi_coherent_dma_tag;
142 #endif
143 config_found_ia(self, "acpibus", &aa, 0);
144
145 acpi_fdt_sysctl_init();
146 }
147
148 static void
149 acpi_fdt_poweroff(device_t dev)
150 {
151 delay(500000);
152 #ifdef EFI_RUNTIME
153 if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0)
154 return;
155 #endif
156 if (psci_available())
157 psci_system_off();
158 }
159
160 static void
161 acpi_fdt_sysctl_init(void)
162 {
163 const struct sysctlnode *rnode;
164 int error;
165
166 const int chosen = OF_finddevice("/chosen");
167 if (chosen >= 0)
168 of_getprop_uint64(chosen, "netbsd,smbios-table", &smbios_table);
169
170 error = sysctl_createv(NULL, 0, NULL, &rnode,
171 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
172 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
173 if (error)
174 return;
175
176 if (smbios_table != 0) {
177 (void)sysctl_createv(NULL, 0, &rnode, NULL,
178 CTLFLAG_PERMANENT | CTLFLAG_READONLY | CTLFLAG_HEX, CTLTYPE_QUAD,
179 "smbios", SYSCTL_DESCR("SMBIOS table pointer"),
180 NULL, 0, &smbios_table, sizeof(smbios_table),
181 CTL_CREATE, CTL_EOL);
182 }
183 }
184
185 #ifdef EFI_RUNTIME
186 static void
187 acpi_fdt_efi_init(device_t dev)
188 {
189 uint64_t efi_system_table;
190 struct efi_tm tm;
191 int error;
192
193 const int chosen = OF_finddevice("/chosen");
194 if (chosen < 0)
195 return;
196
197 if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
198 return;
199
200 error = arm_efirt_init(efi_system_table);
201 if (error)
202 return;
203
204 aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
205
206 if (arm_efirt_gettime(&tm) == 0) {
207 aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
208 efi_todr.cookie = NULL;
209 efi_todr.todr_gettime_ymdhms = acpi_fdt_efi_rtc_gettime;
210 efi_todr.todr_settime_ymdhms = acpi_fdt_efi_rtc_settime;
211 todr_attach(&efi_todr);
212 }
213 }
214
215 static int
216 acpi_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
217 {
218 struct efi_tm tm;
219 int error;
220
221 error = arm_efirt_gettime(&tm);
222 if (error)
223 return error;
224
225 dt->dt_year = tm.tm_year;
226 dt->dt_mon = tm.tm_mon;
227 dt->dt_day = tm.tm_mday;
228 dt->dt_wday = 0;
229 dt->dt_hour = tm.tm_hour;
230 dt->dt_min = tm.tm_min;
231 dt->dt_sec = tm.tm_sec;
232
233 return 0;
234 }
235
236 static int
237 acpi_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
238 {
239 struct efi_tm tm;
240
241 memset(&tm, 0, sizeof(tm));
242 tm.tm_year = dt->dt_year;
243 tm.tm_mon = dt->dt_mon;
244 tm.tm_mday = dt->dt_day;
245 tm.tm_hour = dt->dt_hour;
246 tm.tm_min = dt->dt_min;
247 tm.tm_sec = dt->dt_sec;
248
249 return arm_efirt_settime(&tm);
250 }
251 #endif
252