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