acpi.c revision 1.114.2.7 1 1.114.2.7 yamt /* $NetBSD: acpi.c,v 1.114.2.7 2010/08/11 22:53:15 yamt Exp $ */
2 1.50 mycroft
3 1.50 mycroft /*-
4 1.101 ad * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5 1.50 mycroft * All rights reserved.
6 1.50 mycroft *
7 1.50 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.50 mycroft * by Charles M. Hannum of By Noon Software, Inc.
9 1.50 mycroft *
10 1.50 mycroft * Redistribution and use in source and binary forms, with or without
11 1.50 mycroft * modification, are permitted provided that the following conditions
12 1.50 mycroft * are met:
13 1.50 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.50 mycroft * notice, this list of conditions and the following disclaimer.
15 1.50 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.50 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.50 mycroft * documentation and/or other materials provided with the distribution.
18 1.50 mycroft *
19 1.50 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.50 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.50 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.50 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.50 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.50 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.50 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.50 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.50 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.50 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.50 mycroft * POSSIBILITY OF SUCH DAMAGE.
30 1.50 mycroft */
31 1.1 thorpej
32 1.1 thorpej /*
33 1.114.2.7 yamt * Copyright (c) 2003 Wasabi Systems, Inc.
34 1.114.2.7 yamt * All rights reserved.
35 1.114.2.7 yamt *
36 1.114.2.7 yamt * Written by Frank van der Linden for Wasabi Systems, Inc.
37 1.114.2.7 yamt *
38 1.114.2.7 yamt * Redistribution and use in source and binary forms, with or without
39 1.114.2.7 yamt * modification, are permitted provided that the following conditions
40 1.114.2.7 yamt * are met:
41 1.114.2.7 yamt * 1. Redistributions of source code must retain the above copyright
42 1.114.2.7 yamt * notice, this list of conditions and the following disclaimer.
43 1.114.2.7 yamt * 2. Redistributions in binary form must reproduce the above copyright
44 1.114.2.7 yamt * notice, this list of conditions and the following disclaimer in the
45 1.114.2.7 yamt * documentation and/or other materials provided with the distribution.
46 1.114.2.7 yamt * 3. All advertising materials mentioning features or use of this software
47 1.114.2.7 yamt * must display the following acknowledgement:
48 1.114.2.7 yamt * This product includes software developed for the NetBSD Project by
49 1.114.2.7 yamt * Wasabi Systems, Inc.
50 1.114.2.7 yamt * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51 1.114.2.7 yamt * or promote products derived from this software without specific prior
52 1.114.2.7 yamt * written permission.
53 1.114.2.7 yamt *
54 1.114.2.7 yamt * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55 1.114.2.7 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 1.114.2.7 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 1.114.2.7 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
58 1.114.2.7 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 1.114.2.7 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 1.114.2.7 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 1.114.2.7 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 1.114.2.7 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 1.114.2.7 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 1.114.2.7 yamt * POSSIBILITY OF SUCH DAMAGE.
65 1.114.2.7 yamt */
66 1.114.2.7 yamt
67 1.114.2.7 yamt /*
68 1.34 thorpej * Copyright 2001, 2003 Wasabi Systems, Inc.
69 1.1 thorpej * All rights reserved.
70 1.1 thorpej *
71 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
72 1.1 thorpej *
73 1.1 thorpej * Redistribution and use in source and binary forms, with or without
74 1.1 thorpej * modification, are permitted provided that the following conditions
75 1.1 thorpej * are met:
76 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
77 1.1 thorpej * notice, this list of conditions and the following disclaimer.
78 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
79 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
80 1.1 thorpej * documentation and/or other materials provided with the distribution.
81 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
82 1.1 thorpej * must display the following acknowledgement:
83 1.1 thorpej * This product includes software developed for the NetBSD Project by
84 1.1 thorpej * Wasabi Systems, Inc.
85 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
86 1.1 thorpej * or promote products derived from this software without specific prior
87 1.1 thorpej * written permission.
88 1.1 thorpej *
89 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
90 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
91 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
92 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
93 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
94 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
95 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
96 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
97 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
98 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
99 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
100 1.1 thorpej */
101 1.1 thorpej
102 1.5 lukem #include <sys/cdefs.h>
103 1.114.2.7 yamt __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.114.2.7 2010/08/11 22:53:15 yamt Exp $");
104 1.27 christos
105 1.27 christos #include "opt_acpi.h"
106 1.73 sekiya #include "opt_pcifixup.h"
107 1.1 thorpej
108 1.1 thorpej #include <sys/param.h>
109 1.1 thorpej #include <sys/device.h>
110 1.114.2.6 yamt #include <sys/kernel.h>
111 1.1 thorpej #include <sys/malloc.h>
112 1.114.2.7 yamt #include <sys/module.h>
113 1.100 xtraeme #include <sys/mutex.h>
114 1.78 cube #include <sys/sysctl.h>
115 1.114.2.6 yamt #include <sys/systm.h>
116 1.114.2.7 yamt #include <sys/timetc.h>
117 1.1 thorpej
118 1.1 thorpej #include <dev/acpi/acpireg.h>
119 1.1 thorpej #include <dev/acpi/acpivar.h>
120 1.1 thorpej #include <dev/acpi/acpi_osd.h>
121 1.114.2.6 yamt #include <dev/acpi/acpi_pci.h>
122 1.114.2.7 yamt #include <dev/acpi/acpi_power.h>
123 1.90 drochner #include <dev/acpi/acpi_timer.h>
124 1.114.2.4 yamt #include <dev/acpi/acpi_wakedev.h>
125 1.114.2.6 yamt
126 1.114.2.7 yamt #define _COMPONENT ACPI_BUS_COMPONENT
127 1.114.2.7 yamt ACPI_MODULE_NAME ("acpi")
128 1.114.2.6 yamt
129 1.74 sekiya #if defined(ACPI_PCI_FIXUP)
130 1.93 christos #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED. Please adjust your kernel configuration file.
131 1.74 sekiya #endif
132 1.74 sekiya
133 1.93 christos #ifdef PCI_INTR_FIXUP_DISABLED
134 1.13 augustss #include <dev/pci/pcidevs.h>
135 1.13 augustss #endif
136 1.13 augustss
137 1.33 christos MALLOC_DECLARE(M_ACPI);
138 1.33 christos
139 1.10 tshiozak #include <machine/acpi_machdep.h>
140 1.39 kochi
141 1.40 kochi #ifdef ACPI_DEBUGGER
142 1.1 thorpej #define ACPI_DBGR_INIT 0x01
143 1.1 thorpej #define ACPI_DBGR_TABLES 0x02
144 1.1 thorpej #define ACPI_DBGR_ENABLE 0x04
145 1.1 thorpej #define ACPI_DBGR_PROBE 0x08
146 1.1 thorpej #define ACPI_DBGR_RUNNING 0x10
147 1.1 thorpej
148 1.82 kochi static int acpi_dbgr = 0x00;
149 1.1 thorpej #endif
150 1.1 thorpej
151 1.1 thorpej /*
152 1.1 thorpej * This is a flag we set when the ACPI subsystem is active. Machine
153 1.1 thorpej * dependent code may wish to skip other steps (such as attaching
154 1.1 thorpej * subsystems that ACPI supercedes) when ACPI is active.
155 1.1 thorpej */
156 1.1 thorpej int acpi_active;
157 1.93 christos int acpi_force_load;
158 1.114.2.2 yamt int acpi_suspended = 0;
159 1.114.2.7 yamt int acpi_verbose_loaded = 0;
160 1.1 thorpej
161 1.114.2.7 yamt struct acpi_softc *acpi_softc;
162 1.114.2.7 yamt static uint64_t acpi_root_pointer;
163 1.114.2.7 yamt extern kmutex_t acpi_interrupt_list_mtx;
164 1.114.2.7 yamt extern struct cfdriver acpi_cd;
165 1.114.2.7 yamt static ACPI_HANDLE acpi_scopes[4];
166 1.114.2.7 yamt ACPI_TABLE_HEADER *madt_header;
167 1.114.2.7 yamt
168 1.114.2.7 yamt /*
169 1.114.2.7 yamt * This structure provides a context for the ACPI
170 1.114.2.7 yamt * namespace walk performed in acpi_build_tree().
171 1.114.2.7 yamt */
172 1.114.2.7 yamt struct acpi_walkcontext {
173 1.114.2.7 yamt struct acpi_softc *aw_sc;
174 1.114.2.7 yamt struct acpi_devnode *aw_parent;
175 1.114.2.7 yamt };
176 1.32 tshiozak
177 1.79 cube /*
178 1.114.2.6 yamt * Ignored HIDs.
179 1.114.2.2 yamt */
180 1.114.2.2 yamt static const char * const acpi_ignored_ids[] = {
181 1.114.2.2 yamt #if defined(i386) || defined(x86_64)
182 1.114.2.2 yamt "PNP0000", /* AT interrupt controller is handled internally */
183 1.114.2.2 yamt "PNP0200", /* AT DMA controller is handled internally */
184 1.114.2.6 yamt "PNP0A??", /* PCI Busses are handled internally */
185 1.114.2.2 yamt "PNP0B00", /* AT RTC is handled internally */
186 1.114.2.6 yamt "PNP0C0B", /* No need for "ACPI fan" driver */
187 1.114.2.2 yamt "PNP0C0F", /* ACPI PCI link devices are handled internally */
188 1.114.2.7 yamt "IFX0102", /* No driver for Infineon TPM */
189 1.114.2.7 yamt "INT0800", /* No driver for Intel Firmware Hub device */
190 1.114.2.2 yamt #endif
191 1.114.2.2 yamt #if defined(x86_64)
192 1.114.2.2 yamt "PNP0C04", /* FPU is handled internally */
193 1.114.2.2 yamt #endif
194 1.114.2.2 yamt NULL
195 1.114.2.2 yamt };
196 1.114.2.2 yamt
197 1.114.2.7 yamt static int acpi_match(device_t, cfdata_t, void *);
198 1.114.2.7 yamt static int acpi_submatch(device_t, cfdata_t, const int *, void *);
199 1.114.2.7 yamt static void acpi_attach(device_t, device_t, void *);
200 1.114.2.7 yamt static int acpi_detach(device_t, int);
201 1.114.2.7 yamt static void acpi_childdet(device_t, device_t);
202 1.114.2.7 yamt static bool acpi_suspend(device_t, const pmf_qual_t *);
203 1.114.2.7 yamt static bool acpi_resume(device_t, const pmf_qual_t *);
204 1.78 cube
205 1.64 kochi static void acpi_build_tree(struct acpi_softc *);
206 1.114.2.6 yamt static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, uint32_t,
207 1.114.2.6 yamt void *, void **);
208 1.114.2.7 yamt static ACPI_STATUS acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
209 1.114.2.7 yamt void *, void **);
210 1.114.2.7 yamt
211 1.114.2.7 yamt #ifdef ACPI_ACTIVATE_DEV
212 1.114.2.7 yamt static void acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
213 1.114.2.7 yamt static ACPI_STATUS acpi_allocate_resources(ACPI_HANDLE);
214 1.114.2.7 yamt #endif
215 1.114.2.7 yamt
216 1.114.2.7 yamt static int acpi_rescan(device_t, const char *, const int *);
217 1.114.2.7 yamt static void acpi_rescan_nodes(struct acpi_softc *);
218 1.114.2.7 yamt static void acpi_rescan_capabilities(struct acpi_softc *);
219 1.114.2.7 yamt static int acpi_print(void *aux, const char *);
220 1.114.2.7 yamt
221 1.114.2.7 yamt static void acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
222 1.114.2.7 yamt
223 1.114.2.7 yamt static void acpi_register_fixed_button(struct acpi_softc *, int);
224 1.114.2.7 yamt static void acpi_deregister_fixed_button(struct acpi_softc *, int);
225 1.114.2.7 yamt static uint32_t acpi_fixed_button_handler(void *);
226 1.114.2.7 yamt static void acpi_fixed_button_pressed(void *);
227 1.1 thorpej
228 1.114.2.7 yamt static void acpi_sleep_init(struct acpi_softc *);
229 1.1 thorpej
230 1.114.2.7 yamt static int sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO);
231 1.114.2.7 yamt static int sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO);
232 1.114.2.7 yamt static int sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO);
233 1.114.2.7 yamt
234 1.114.2.7 yamt static bool acpi_is_scope(struct acpi_devnode *);
235 1.108 jmcneill static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
236 1.114.2.7 yamt static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
237 1.108 jmcneill
238 1.114.2.7 yamt void acpi_print_verbose_stub(struct acpi_softc *);
239 1.114.2.7 yamt void acpi_print_dev_stub(const char *);
240 1.114.2.7 yamt
241 1.114.2.7 yamt void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub;
242 1.114.2.7 yamt void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
243 1.114.2.7 yamt
244 1.114.2.7 yamt CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
245 1.114.2.7 yamt acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
246 1.114.2.2 yamt
247 1.1 thorpej /*
248 1.114.2.7 yamt * Probe for ACPI support.
249 1.1 thorpej *
250 1.114.2.7 yamt * This is called by the machine-dependent ACPI front-end.
251 1.114.2.7 yamt * Note: this is not an autoconfiguration interface function.
252 1.1 thorpej */
253 1.1 thorpej int
254 1.1 thorpej acpi_probe(void)
255 1.1 thorpej {
256 1.108 jmcneill ACPI_TABLE_HEADER *rsdt;
257 1.114.2.7 yamt const char *func;
258 1.114.2.7 yamt static int once;
259 1.114.2.7 yamt bool initialized;
260 1.1 thorpej ACPI_STATUS rv;
261 1.1 thorpej
262 1.114.2.7 yamt if (once != 0)
263 1.114.2.7 yamt panic("%s: already probed", __func__);
264 1.114.2.7 yamt
265 1.114.2.7 yamt once = 1;
266 1.114.2.7 yamt func = NULL;
267 1.114.2.7 yamt initialized = false;
268 1.1 thorpej
269 1.103 ad mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
270 1.32 tshiozak
271 1.1 thorpej /*
272 1.1 thorpej * Start up ACPICA.
273 1.1 thorpej */
274 1.40 kochi #ifdef ACPI_DEBUGGER
275 1.1 thorpej if (acpi_dbgr & ACPI_DBGR_INIT)
276 1.1 thorpej acpi_osd_debugger();
277 1.1 thorpej #endif
278 1.1 thorpej
279 1.114.2.7 yamt CTASSERT(TRUE == true);
280 1.114.2.7 yamt CTASSERT(FALSE == false);
281 1.114.2.7 yamt
282 1.114.2.7 yamt AcpiGbl_AllMethodsSerialized = false;
283 1.114.2.7 yamt AcpiGbl_EnableInterpreterSlack = true;
284 1.104 jmcneill
285 1.1 thorpej rv = AcpiInitializeSubsystem();
286 1.1 thorpej
287 1.114.2.7 yamt if (ACPI_SUCCESS(rv))
288 1.114.2.7 yamt initialized = true;
289 1.114.2.7 yamt else {
290 1.114.2.7 yamt func = "AcpiInitializeSubsystem()";
291 1.114.2.7 yamt goto fail;
292 1.104 jmcneill }
293 1.104 jmcneill
294 1.114.2.7 yamt /*
295 1.114.2.7 yamt * Allocate space for RSDT/XSDT and DSDT,
296 1.114.2.7 yamt * but allow resizing if more tables exist.
297 1.114.2.7 yamt */
298 1.114.2.7 yamt rv = AcpiInitializeTables(NULL, 2, true);
299 1.114.2.7 yamt
300 1.104 jmcneill if (ACPI_FAILURE(rv)) {
301 1.114.2.7 yamt func = "AcpiInitializeTables()";
302 1.114.2.7 yamt goto fail;
303 1.104 jmcneill }
304 1.104 jmcneill
305 1.40 kochi #ifdef ACPI_DEBUGGER
306 1.1 thorpej if (acpi_dbgr & ACPI_DBGR_TABLES)
307 1.1 thorpej acpi_osd_debugger();
308 1.1 thorpej #endif
309 1.1 thorpej
310 1.1 thorpej rv = AcpiLoadTables();
311 1.114.2.7 yamt
312 1.56 mycroft if (ACPI_FAILURE(rv)) {
313 1.114.2.7 yamt func = "AcpiLoadTables()";
314 1.114.2.7 yamt goto fail;
315 1.1 thorpej }
316 1.1 thorpej
317 1.108 jmcneill rsdt = acpi_map_rsdt();
318 1.114.2.7 yamt
319 1.108 jmcneill if (rsdt == NULL) {
320 1.114.2.7 yamt func = "acpi_map_rsdt()";
321 1.114.2.7 yamt rv = AE_ERROR;
322 1.114.2.7 yamt goto fail;
323 1.108 jmcneill }
324 1.107 jmcneill
325 1.114.2.7 yamt if (acpi_force_load == 0 && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
326 1.114.2.7 yamt aprint_normal("ACPI: BIOS is listed as broken:\n");
327 1.114.2.7 yamt aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
328 1.93 christos "AslId <%4.4s,%08x>\n",
329 1.108 jmcneill rsdt->OemId, rsdt->OemTableId,
330 1.108 jmcneill rsdt->OemRevision,
331 1.108 jmcneill rsdt->AslCompilerId,
332 1.108 jmcneill rsdt->AslCompilerRevision);
333 1.114.2.7 yamt aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
334 1.108 jmcneill acpi_unmap_rsdt(rsdt);
335 1.114.2.5 yamt AcpiTerminate();
336 1.93 christos return 0;
337 1.93 christos }
338 1.93 christos
339 1.108 jmcneill acpi_unmap_rsdt(rsdt);
340 1.108 jmcneill
341 1.104 jmcneill rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
342 1.114.2.7 yamt
343 1.104 jmcneill if (ACPI_FAILURE(rv)) {
344 1.114.2.7 yamt func = "AcpiEnableSubsystem()";
345 1.114.2.7 yamt goto fail;
346 1.104 jmcneill }
347 1.104 jmcneill
348 1.1 thorpej /*
349 1.1 thorpej * Looks like we have ACPI!
350 1.1 thorpej */
351 1.63 kochi return 1;
352 1.114.2.7 yamt
353 1.114.2.7 yamt fail:
354 1.114.2.7 yamt KASSERT(rv != AE_OK);
355 1.114.2.7 yamt KASSERT(func != NULL);
356 1.114.2.7 yamt
357 1.114.2.7 yamt aprint_error("%s: failed to probe ACPI: %s\n",
358 1.114.2.7 yamt func, AcpiFormatException(rv));
359 1.114.2.7 yamt
360 1.114.2.7 yamt if (initialized != false)
361 1.114.2.7 yamt (void)AcpiTerminate();
362 1.114.2.7 yamt
363 1.114.2.7 yamt return 0;
364 1.1 thorpej }
365 1.1 thorpej
366 1.114.2.7 yamt void
367 1.114.2.7 yamt acpi_disable(void)
368 1.98 cube {
369 1.98 cube
370 1.114.2.7 yamt if (AcpiGbl_FADT.SmiCommand != 0)
371 1.114.2.7 yamt AcpiDisable();
372 1.98 cube }
373 1.98 cube
374 1.98 cube int
375 1.98 cube acpi_check(device_t parent, const char *ifattr)
376 1.98 cube {
377 1.98 cube return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
378 1.98 cube }
379 1.98 cube
380 1.1 thorpej /*
381 1.114.2.7 yamt * Autoconfiguration.
382 1.1 thorpej */
383 1.64 kochi static int
384 1.114.2.3 yamt acpi_match(device_t parent, cfdata_t match, void *aux)
385 1.1 thorpej {
386 1.1 thorpej /*
387 1.114.2.7 yamt * XXX: Nada; MD code has called acpi_probe().
388 1.1 thorpej */
389 1.63 kochi return 1;
390 1.1 thorpej }
391 1.1 thorpej
392 1.114.2.7 yamt static int
393 1.114.2.7 yamt acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
394 1.111 dyoung {
395 1.114.2.7 yamt struct cfattach *ca;
396 1.114.2.2 yamt
397 1.114.2.7 yamt ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
398 1.114.2.6 yamt
399 1.114.2.7 yamt return (ca == &acpi_ca);
400 1.111 dyoung }
401 1.111 dyoung
402 1.64 kochi static void
403 1.111 dyoung acpi_attach(device_t parent, device_t self, void *aux)
404 1.1 thorpej {
405 1.111 dyoung struct acpi_softc *sc = device_private(self);
406 1.1 thorpej struct acpibus_attach_args *aa = aux;
407 1.108 jmcneill ACPI_TABLE_HEADER *rsdt;
408 1.114.2.7 yamt ACPI_STATUS rv;
409 1.1 thorpej
410 1.114.2.2 yamt aprint_naive("\n");
411 1.114.2.2 yamt aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
412 1.1 thorpej
413 1.1 thorpej if (acpi_softc != NULL)
414 1.114.2.7 yamt panic("%s: already attached", __func__);
415 1.37 kochi
416 1.108 jmcneill rsdt = acpi_map_rsdt();
417 1.114.2.7 yamt
418 1.114.2.7 yamt if (rsdt == NULL)
419 1.114.2.7 yamt aprint_error_dev(self, "X/RSDT: Not found\n");
420 1.114.2.7 yamt else {
421 1.114.2.7 yamt aprint_verbose_dev(self,
422 1.111 dyoung "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
423 1.108 jmcneill rsdt->OemId, rsdt->OemTableId,
424 1.108 jmcneill rsdt->OemRevision,
425 1.108 jmcneill rsdt->AslCompilerId, rsdt->AslCompilerRevision);
426 1.114.2.7 yamt }
427 1.114.2.6 yamt
428 1.108 jmcneill acpi_unmap_rsdt(rsdt);
429 1.1 thorpej
430 1.113 jmcneill sc->sc_dev = self;
431 1.114.2.7 yamt sc->sc_root = NULL;
432 1.114.2.7 yamt
433 1.114.2.7 yamt sc->sc_sleepstate = ACPI_STATE_S0;
434 1.36 fvdl sc->sc_quirks = acpi_find_quirks();
435 1.36 fvdl
436 1.114.2.7 yamt sysmon_power_settype("acpi");
437 1.114.2.7 yamt
438 1.1 thorpej sc->sc_iot = aa->aa_iot;
439 1.1 thorpej sc->sc_memt = aa->aa_memt;
440 1.1 thorpej sc->sc_pc = aa->aa_pc;
441 1.1 thorpej sc->sc_pciflags = aa->aa_pciflags;
442 1.19 jmcneill sc->sc_ic = aa->aa_ic;
443 1.1 thorpej
444 1.114.2.7 yamt SIMPLEQ_INIT(&sc->ad_head);
445 1.114.2.6 yamt
446 1.1 thorpej acpi_softc = sc;
447 1.1 thorpej
448 1.114.2.7 yamt if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
449 1.104 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
450 1.104 jmcneill
451 1.104 jmcneill /*
452 1.1 thorpej * Bring ACPI on-line.
453 1.1 thorpej */
454 1.40 kochi #ifdef ACPI_DEBUGGER
455 1.1 thorpej if (acpi_dbgr & ACPI_DBGR_ENABLE)
456 1.1 thorpej acpi_osd_debugger();
457 1.1 thorpej #endif
458 1.47 mycroft
459 1.104 jmcneill #define ACPI_ENABLE_PHASE1 \
460 1.104 jmcneill (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
461 1.104 jmcneill #define ACPI_ENABLE_PHASE2 \
462 1.104 jmcneill (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
463 1.104 jmcneill ACPI_NO_ADDRESS_SPACE_INIT)
464 1.104 jmcneill
465 1.104 jmcneill rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
466 1.114.2.7 yamt
467 1.114.2.7 yamt if (ACPI_FAILURE(rv))
468 1.114.2.7 yamt goto fail;
469 1.104 jmcneill
470 1.104 jmcneill acpi_md_callback();
471 1.104 jmcneill
472 1.104 jmcneill rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
473 1.60 kochi
474 1.114.2.7 yamt if (ACPI_FAILURE(rv))
475 1.114.2.7 yamt goto fail;
476 1.114.2.7 yamt
477 1.114.2.7 yamt /*
478 1.114.2.7 yamt * Early EC handler initialization if ECDT table is available.
479 1.114.2.7 yamt */
480 1.114.2.7 yamt config_found_ia(self, "acpiecdtbus", aa, NULL);
481 1.60 kochi
482 1.104 jmcneill rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
483 1.1 thorpej
484 1.114.2.7 yamt if (ACPI_FAILURE(rv))
485 1.114.2.7 yamt goto fail;
486 1.114.2.7 yamt
487 1.114.2.7 yamt /*
488 1.114.2.7 yamt * Install global notify handlers.
489 1.114.2.7 yamt */
490 1.114.2.7 yamt rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
491 1.114.2.7 yamt ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
492 1.114.2.7 yamt
493 1.114.2.7 yamt if (ACPI_FAILURE(rv))
494 1.114.2.7 yamt goto fail;
495 1.114.2.7 yamt
496 1.114.2.7 yamt rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
497 1.114.2.7 yamt ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
498 1.114.2.7 yamt
499 1.114.2.7 yamt if (ACPI_FAILURE(rv))
500 1.114.2.7 yamt goto fail;
501 1.114.2.7 yamt
502 1.114.2.7 yamt acpi_active = 1;
503 1.1 thorpej
504 1.9 kanaoka /* Show SCI interrupt. */
505 1.114.2.6 yamt aprint_verbose_dev(self, "SCI interrupting at int %u\n",
506 1.111 dyoung AcpiGbl_FADT.SciInterrupt);
507 1.104 jmcneill
508 1.1 thorpej /*
509 1.114.2.7 yamt * Install fixed-event handlers.
510 1.1 thorpej */
511 1.114.2.7 yamt acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
512 1.114.2.7 yamt acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
513 1.114.2.7 yamt
514 1.114.2.7 yamt acpitimer_init(sc);
515 1.1 thorpej
516 1.40 kochi #ifdef ACPI_DEBUGGER
517 1.1 thorpej if (acpi_dbgr & ACPI_DBGR_PROBE)
518 1.1 thorpej acpi_osd_debugger();
519 1.1 thorpej #endif
520 1.1 thorpej
521 1.114.2.7 yamt /*
522 1.114.2.7 yamt * Scan the namespace and build our device tree.
523 1.114.2.7 yamt */
524 1.114.2.7 yamt acpi_build_tree(sc);
525 1.114.2.7 yamt acpi_sleep_init(sc);
526 1.78 cube
527 1.40 kochi #ifdef ACPI_DEBUGGER
528 1.1 thorpej if (acpi_dbgr & ACPI_DBGR_RUNNING)
529 1.1 thorpej acpi_osd_debugger();
530 1.1 thorpej #endif
531 1.114.2.6 yamt
532 1.114.2.6 yamt #ifdef ACPI_DEBUG
533 1.114.2.6 yamt acpi_debug_init();
534 1.114.2.6 yamt #endif
535 1.114.2.7 yamt
536 1.114.2.7 yamt /*
537 1.114.2.7 yamt * Print debug information.
538 1.114.2.7 yamt */
539 1.114.2.7 yamt acpi_print_verbose(sc);
540 1.114.2.7 yamt
541 1.114.2.7 yamt return;
542 1.114.2.7 yamt
543 1.114.2.7 yamt fail:
544 1.114.2.7 yamt aprint_error("%s: failed to initialize ACPI: %s\n",
545 1.114.2.7 yamt __func__, AcpiFormatException(rv));
546 1.1 thorpej }
547 1.1 thorpej
548 1.114.2.7 yamt /*
549 1.114.2.7 yamt * XXX: This is incomplete.
550 1.114.2.7 yamt */
551 1.114.2.2 yamt static int
552 1.114.2.2 yamt acpi_detach(device_t self, int flags)
553 1.114.2.2 yamt {
554 1.114.2.7 yamt struct acpi_softc *sc = device_private(self);
555 1.114.2.7 yamt ACPI_STATUS rv;
556 1.114.2.2 yamt int rc;
557 1.114.2.2 yamt
558 1.114.2.7 yamt rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
559 1.114.2.7 yamt ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
560 1.114.2.7 yamt
561 1.114.2.7 yamt if (ACPI_FAILURE(rv))
562 1.114.2.7 yamt return EBUSY;
563 1.114.2.7 yamt
564 1.114.2.7 yamt rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
565 1.114.2.7 yamt ACPI_DEVICE_NOTIFY, acpi_notify_handler);
566 1.114.2.7 yamt
567 1.114.2.7 yamt if (ACPI_FAILURE(rv))
568 1.114.2.7 yamt return EBUSY;
569 1.114.2.2 yamt
570 1.114.2.2 yamt if ((rc = config_detach_children(self, flags)) != 0)
571 1.114.2.2 yamt return rc;
572 1.114.2.2 yamt
573 1.114.2.2 yamt if ((rc = acpitimer_detach()) != 0)
574 1.114.2.2 yamt return rc;
575 1.114.2.2 yamt
576 1.114.2.7 yamt acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
577 1.114.2.7 yamt acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
578 1.114.2.2 yamt
579 1.114.2.7 yamt pmf_device_deregister(self);
580 1.114.2.2 yamt
581 1.114.2.7 yamt acpi_softc = NULL;
582 1.114.2.2 yamt
583 1.114.2.7 yamt return 0;
584 1.114.2.7 yamt }
585 1.114.2.2 yamt
586 1.114.2.7 yamt /*
587 1.114.2.7 yamt * XXX: Need to reclaim any resources? Yes.
588 1.114.2.7 yamt */
589 1.114.2.7 yamt static void
590 1.114.2.7 yamt acpi_childdet(device_t self, device_t child)
591 1.114.2.7 yamt {
592 1.114.2.7 yamt struct acpi_softc *sc = device_private(self);
593 1.114.2.7 yamt struct acpi_devnode *ad;
594 1.114.2.2 yamt
595 1.114.2.7 yamt if (sc->sc_apmbus == child)
596 1.114.2.7 yamt sc->sc_apmbus = NULL;
597 1.114.2.2 yamt
598 1.114.2.7 yamt SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
599 1.114.2.2 yamt
600 1.114.2.7 yamt if (ad->ad_device == child)
601 1.114.2.7 yamt ad->ad_device = NULL;
602 1.114.2.7 yamt }
603 1.114.2.2 yamt }
604 1.114.2.2 yamt
605 1.114.2.2 yamt static bool
606 1.114.2.6 yamt acpi_suspend(device_t dv, const pmf_qual_t *qual)
607 1.114.2.2 yamt {
608 1.114.2.7 yamt
609 1.114.2.2 yamt acpi_suspended = 1;
610 1.114.2.7 yamt
611 1.114.2.2 yamt return true;
612 1.114.2.2 yamt }
613 1.114.2.2 yamt
614 1.114.2.2 yamt static bool
615 1.114.2.6 yamt acpi_resume(device_t dv, const pmf_qual_t *qual)
616 1.114.2.2 yamt {
617 1.114.2.2 yamt
618 1.114.2.7 yamt acpi_suspended = 0;
619 1.1 thorpej
620 1.114.2.7 yamt return true;
621 1.1 thorpej }
622 1.1 thorpej
623 1.1 thorpej /*
624 1.114.2.7 yamt * Namespace scan.
625 1.1 thorpej */
626 1.64 kochi static void
627 1.1 thorpej acpi_build_tree(struct acpi_softc *sc)
628 1.1 thorpej {
629 1.114.2.7 yamt struct acpi_walkcontext awc;
630 1.1 thorpej
631 1.1 thorpej /*
632 1.114.2.7 yamt * Get the root scope handles.
633 1.1 thorpej */
634 1.114.2.7 yamt KASSERT(__arraycount(acpi_scopes) == 4);
635 1.114.2.2 yamt
636 1.114.2.7 yamt (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
637 1.114.2.7 yamt (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
638 1.114.2.7 yamt (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
639 1.114.2.7 yamt (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
640 1.114.2.6 yamt
641 1.114.2.7 yamt /*
642 1.114.2.7 yamt * Make the root node.
643 1.114.2.7 yamt */
644 1.114.2.7 yamt awc.aw_sc = sc;
645 1.114.2.7 yamt awc.aw_parent = NULL;
646 1.114.2.6 yamt
647 1.114.2.7 yamt (void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
648 1.114.2.2 yamt
649 1.114.2.7 yamt KASSERT(sc->sc_root == NULL);
650 1.114.2.7 yamt KASSERT(awc.aw_parent != NULL);
651 1.1 thorpej
652 1.114.2.7 yamt sc->sc_root = awc.aw_parent;
653 1.15 augustss
654 1.114.2.7 yamt /*
655 1.114.2.7 yamt * Build the internal namespace.
656 1.114.2.7 yamt */
657 1.114.2.7 yamt (void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
658 1.114.2.7 yamt acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
659 1.15 augustss
660 1.114.2.7 yamt /*
661 1.114.2.7 yamt * Scan the internal namespace.
662 1.114.2.7 yamt */
663 1.114.2.7 yamt (void)acpi_rescan(sc->sc_dev, NULL, NULL);
664 1.15 augustss
665 1.114.2.7 yamt acpi_rescan_capabilities(sc);
666 1.54 kochi
667 1.114.2.7 yamt (void)acpi_pcidev_scan(sc->sc_root);
668 1.15 augustss }
669 1.15 augustss
670 1.64 kochi static ACPI_STATUS
671 1.114.2.6 yamt acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
672 1.114.2.6 yamt void *context, void **status)
673 1.1 thorpej {
674 1.114.2.7 yamt struct acpi_walkcontext *awc = context;
675 1.114.2.7 yamt struct acpi_softc *sc = awc->aw_sc;
676 1.1 thorpej struct acpi_devnode *ad;
677 1.54 kochi ACPI_DEVICE_INFO *devinfo;
678 1.114.2.6 yamt ACPI_OBJECT_TYPE type;
679 1.102 cube ACPI_NAME_UNION *anu;
680 1.114.2.6 yamt ACPI_STATUS rv;
681 1.114.2.6 yamt int clear, i;
682 1.1 thorpej
683 1.114.2.7 yamt rv = AcpiGetObjectInfo(handle, &devinfo);
684 1.15 augustss
685 1.114.2.6 yamt if (ACPI_FAILURE(rv))
686 1.114.2.6 yamt return AE_OK; /* Do not terminate the walk. */
687 1.114.2.6 yamt
688 1.114.2.7 yamt type = devinfo->Type;
689 1.114.2.6 yamt
690 1.114.2.6 yamt switch (type) {
691 1.114.2.6 yamt
692 1.114.2.6 yamt case ACPI_TYPE_DEVICE:
693 1.114.2.6 yamt
694 1.40 kochi #ifdef ACPI_ACTIVATE_DEV
695 1.114.2.7 yamt acpi_activate_device(handle, &devinfo);
696 1.15 augustss #endif
697 1.15 augustss
698 1.114.2.6 yamt case ACPI_TYPE_PROCESSOR:
699 1.114.2.6 yamt case ACPI_TYPE_THERMAL:
700 1.114.2.6 yamt case ACPI_TYPE_POWER:
701 1.114.2.6 yamt
702 1.114.2.6 yamt ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT | M_ZERO);
703 1.114.2.6 yamt
704 1.114.2.6 yamt if (ad == NULL)
705 1.114.2.6 yamt return AE_NO_MEMORY;
706 1.114.2.6 yamt
707 1.114.2.7 yamt ad->ad_device = NULL;
708 1.114.2.7 yamt ad->ad_notify = NULL;
709 1.114.2.7 yamt ad->ad_pciinfo = NULL;
710 1.114.2.7 yamt
711 1.114.2.6 yamt ad->ad_type = type;
712 1.114.2.7 yamt ad->ad_handle = handle;
713 1.114.2.7 yamt ad->ad_devinfo = devinfo;
714 1.114.2.7 yamt
715 1.114.2.7 yamt ad->ad_root = sc->sc_dev;
716 1.114.2.7 yamt ad->ad_parent = awc->aw_parent;
717 1.114.2.6 yamt
718 1.114.2.6 yamt anu = (ACPI_NAME_UNION *)&devinfo->Name;
719 1.114.2.6 yamt ad->ad_name[4] = '\0';
720 1.114.2.6 yamt
721 1.114.2.6 yamt for (i = 3, clear = 0; i >= 0; i--) {
722 1.114.2.6 yamt
723 1.114.2.6 yamt if (clear == 0 && anu->Ascii[i] == '_')
724 1.114.2.6 yamt ad->ad_name[i] = '\0';
725 1.114.2.6 yamt else {
726 1.114.2.6 yamt ad->ad_name[i] = anu->Ascii[i];
727 1.114.2.6 yamt clear = 1;
728 1.102 cube }
729 1.114.2.6 yamt }
730 1.114.2.6 yamt
731 1.114.2.6 yamt if (ad->ad_name[0] == '\0')
732 1.114.2.6 yamt ad->ad_name[0] = '_';
733 1.102 cube
734 1.114.2.7 yamt SIMPLEQ_INIT(&ad->ad_child_head);
735 1.114.2.7 yamt SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
736 1.1 thorpej
737 1.114.2.7 yamt acpi_set_node(ad);
738 1.114.2.6 yamt
739 1.114.2.7 yamt if (ad->ad_parent != NULL) {
740 1.114.2.6 yamt
741 1.114.2.7 yamt SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
742 1.114.2.7 yamt ad, ad_child_list);
743 1.114.2.7 yamt }
744 1.114.2.6 yamt
745 1.114.2.7 yamt awc->aw_parent = ad;
746 1.114.2.7 yamt }
747 1.114.2.6 yamt
748 1.114.2.7 yamt return AE_OK;
749 1.114.2.7 yamt }
750 1.114.2.6 yamt
751 1.114.2.7 yamt static ACPI_STATUS
752 1.114.2.7 yamt acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
753 1.114.2.7 yamt void *context, void **status)
754 1.114.2.7 yamt {
755 1.114.2.7 yamt struct acpi_walkcontext *awc = context;
756 1.114.2.7 yamt
757 1.114.2.7 yamt KASSERT(awc != NULL);
758 1.114.2.7 yamt KASSERT(awc->aw_parent != NULL);
759 1.114.2.7 yamt
760 1.114.2.7 yamt if (handle == awc->aw_parent->ad_handle)
761 1.114.2.7 yamt awc->aw_parent = awc->aw_parent->ad_parent;
762 1.114.2.6 yamt
763 1.63 kochi return AE_OK;
764 1.1 thorpej }
765 1.1 thorpej
766 1.114.2.7 yamt #ifdef ACPI_ACTIVATE_DEV
767 1.114.2.7 yamt static void
768 1.114.2.7 yamt acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
769 1.1 thorpej {
770 1.114.2.7 yamt static const int valid = ACPI_VALID_STA | ACPI_VALID_HID;
771 1.114.2.7 yamt ACPI_DEVICE_INFO *newdi;
772 1.56 mycroft ACPI_STATUS rv;
773 1.114.2.7 yamt uint32_t old;
774 1.1 thorpej
775 1.114.2.7 yamt /*
776 1.114.2.7 yamt * If the device is valid and present,
777 1.114.2.7 yamt * but not enabled, try to activate it.
778 1.114.2.7 yamt */
779 1.114.2.7 yamt if (((*di)->Valid & valid) != valid)
780 1.114.2.7 yamt return;
781 1.27 christos
782 1.114.2.7 yamt old = (*di)->CurrentStatus;
783 1.114.2.2 yamt
784 1.114.2.7 yamt if ((old & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED)) !=
785 1.114.2.7 yamt ACPI_STA_DEVICE_PRESENT)
786 1.114.2.7 yamt return;
787 1.27 christos
788 1.114.2.7 yamt rv = acpi_allocate_resources(handle);
789 1.62 kochi
790 1.114.2.7 yamt if (ACPI_FAILURE(rv))
791 1.114.2.7 yamt goto fail;
792 1.41 kochi
793 1.114.2.7 yamt rv = AcpiGetObjectInfo(handle, &newdi);
794 1.1 thorpej
795 1.114.2.7 yamt if (ACPI_FAILURE(rv))
796 1.114.2.7 yamt goto fail;
797 1.114.2.7 yamt
798 1.114.2.7 yamt ACPI_FREE(*di);
799 1.114.2.7 yamt *di = newdi;
800 1.1 thorpej
801 1.114.2.7 yamt aprint_verbose_dev(acpi_softc->sc_dev,
802 1.114.2.7 yamt "%s activated, STA 0x%08X -> STA 0x%08X\n",
803 1.114.2.7 yamt (*di)->HardwareId.String, old, (*di)->CurrentStatus);
804 1.1 thorpej
805 1.114.2.7 yamt return;
806 1.114.2.7 yamt
807 1.114.2.7 yamt fail:
808 1.114.2.7 yamt aprint_error_dev(acpi_softc->sc_dev, "failed to "
809 1.114.2.7 yamt "activate %s\n", (*di)->HardwareId.String);
810 1.114.2.7 yamt }
811 1.1 thorpej
812 1.1 thorpej /*
813 1.114.2.7 yamt * XXX: This very incomplete.
814 1.1 thorpej */
815 1.114.2.7 yamt ACPI_STATUS
816 1.114.2.7 yamt acpi_allocate_resources(ACPI_HANDLE handle)
817 1.1 thorpej {
818 1.114.2.7 yamt ACPI_BUFFER bufp, bufc, bufn;
819 1.114.2.7 yamt ACPI_RESOURCE *resp, *resc, *resn;
820 1.114.2.7 yamt ACPI_RESOURCE_IRQ *irq;
821 1.114.2.7 yamt ACPI_RESOURCE_EXTENDED_IRQ *xirq;
822 1.1 thorpej ACPI_STATUS rv;
823 1.114.2.7 yamt uint delta;
824 1.1 thorpej
825 1.114.2.7 yamt rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
826 1.114.2.7 yamt if (ACPI_FAILURE(rv))
827 1.114.2.7 yamt goto out;
828 1.114.2.7 yamt rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
829 1.114.2.7 yamt if (ACPI_FAILURE(rv)) {
830 1.114.2.7 yamt goto out1;
831 1.114.2.7 yamt }
832 1.34 thorpej
833 1.114.2.7 yamt bufn.Length = 1000;
834 1.114.2.7 yamt bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
835 1.114.2.7 yamt resp = bufp.Pointer;
836 1.114.2.7 yamt resc = bufc.Pointer;
837 1.114.2.7 yamt while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
838 1.114.2.7 yamt resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
839 1.114.2.7 yamt while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
840 1.114.2.7 yamt resp = ACPI_NEXT_RESOURCE(resp);
841 1.114.2.7 yamt if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
842 1.114.2.7 yamt break;
843 1.114.2.7 yamt /* Found identical Id */
844 1.114.2.7 yamt resn->Type = resc->Type;
845 1.114.2.7 yamt switch (resc->Type) {
846 1.114.2.7 yamt case ACPI_RESOURCE_TYPE_IRQ:
847 1.114.2.7 yamt memcpy(&resn->Data, &resp->Data,
848 1.114.2.7 yamt sizeof(ACPI_RESOURCE_IRQ));
849 1.114.2.7 yamt irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
850 1.114.2.7 yamt irq->Interrupts[0] =
851 1.114.2.7 yamt ((ACPI_RESOURCE_IRQ *)&resp->Data)->
852 1.114.2.7 yamt Interrupts[irq->InterruptCount-1];
853 1.114.2.7 yamt irq->InterruptCount = 1;
854 1.114.2.7 yamt resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
855 1.114.2.7 yamt break;
856 1.114.2.7 yamt case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
857 1.114.2.7 yamt memcpy(&resn->Data, &resp->Data,
858 1.114.2.7 yamt sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
859 1.114.2.7 yamt xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
860 1.114.2.7 yamt #if 0
861 1.114.2.7 yamt /*
862 1.114.2.7 yamt * XXX: Not duplicating the interrupt logic above
863 1.114.2.7 yamt * because its not clear what it accomplishes.
864 1.114.2.7 yamt */
865 1.114.2.7 yamt xirq->Interrupts[0] =
866 1.114.2.7 yamt ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
867 1.114.2.7 yamt Interrupts[irq->NumberOfInterrupts-1];
868 1.114.2.7 yamt xirq->NumberOfInterrupts = 1;
869 1.114.2.7 yamt #endif
870 1.114.2.7 yamt resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
871 1.114.2.7 yamt break;
872 1.114.2.7 yamt case ACPI_RESOURCE_TYPE_IO:
873 1.114.2.7 yamt memcpy(&resn->Data, &resp->Data,
874 1.114.2.7 yamt sizeof(ACPI_RESOURCE_IO));
875 1.114.2.7 yamt resn->Length = resp->Length;
876 1.114.2.7 yamt break;
877 1.114.2.7 yamt default:
878 1.114.2.7 yamt aprint_error_dev(acpi_softc->sc_dev,
879 1.114.2.7 yamt "%s: invalid type %u\n", __func__, resc->Type);
880 1.114.2.7 yamt rv = AE_BAD_DATA;
881 1.114.2.7 yamt goto out2;
882 1.114.2.7 yamt }
883 1.114.2.7 yamt resc = ACPI_NEXT_RESOURCE(resc);
884 1.114.2.7 yamt resn = ACPI_NEXT_RESOURCE(resn);
885 1.114.2.7 yamt resp = ACPI_NEXT_RESOURCE(resp);
886 1.114.2.7 yamt delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
887 1.114.2.7 yamt if (delta >=
888 1.114.2.7 yamt bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
889 1.114.2.7 yamt bufn.Length *= 2;
890 1.114.2.7 yamt bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
891 1.114.2.7 yamt M_ACPI, M_WAITOK);
892 1.114.2.7 yamt resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
893 1.114.2.7 yamt delta);
894 1.34 thorpej }
895 1.1 thorpej }
896 1.1 thorpej
897 1.114.2.7 yamt if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
898 1.114.2.7 yamt aprint_error_dev(acpi_softc->sc_dev,
899 1.114.2.7 yamt "%s: resc not exhausted\n", __func__);
900 1.114.2.7 yamt rv = AE_BAD_DATA;
901 1.114.2.7 yamt goto out3;
902 1.1 thorpej }
903 1.114.2.7 yamt
904 1.114.2.7 yamt resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
905 1.114.2.7 yamt rv = AcpiSetCurrentResources(handle, &bufn);
906 1.114.2.7 yamt
907 1.114.2.7 yamt if (ACPI_FAILURE(rv))
908 1.114.2.7 yamt aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
909 1.114.2.7 yamt "resources: %s\n", __func__, AcpiFormatException(rv));
910 1.114.2.7 yamt
911 1.114.2.7 yamt out3:
912 1.114.2.7 yamt free(bufn.Pointer, M_ACPI);
913 1.114.2.7 yamt out2:
914 1.114.2.7 yamt ACPI_FREE(bufc.Pointer);
915 1.114.2.7 yamt out1:
916 1.114.2.7 yamt ACPI_FREE(bufp.Pointer);
917 1.114.2.7 yamt out:
918 1.114.2.7 yamt return rv;
919 1.1 thorpej }
920 1.114.2.7 yamt #endif /* ACPI_ACTIVATE_DEV */
921 1.1 thorpej
922 1.1 thorpej /*
923 1.114.2.7 yamt * Device attachment.
924 1.1 thorpej */
925 1.114.2.7 yamt static int
926 1.114.2.7 yamt acpi_rescan(device_t self, const char *ifattr, const int *locators)
927 1.1 thorpej {
928 1.114.2.7 yamt struct acpi_softc *sc = device_private(self);
929 1.1 thorpej
930 1.114.2.7 yamt if (ifattr_match(ifattr, "acpinodebus"))
931 1.114.2.7 yamt acpi_rescan_nodes(sc);
932 1.1 thorpej
933 1.114.2.7 yamt if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
934 1.114.2.7 yamt sc->sc_apmbus = config_found_ia(sc->sc_dev,
935 1.114.2.7 yamt "acpiapmbus", NULL, NULL);
936 1.1 thorpej
937 1.114.2.7 yamt return 0;
938 1.1 thorpej }
939 1.1 thorpej
940 1.64 kochi static void
941 1.114.2.7 yamt acpi_rescan_nodes(struct acpi_softc *sc)
942 1.1 thorpej {
943 1.114.2.7 yamt struct acpi_attach_args aa;
944 1.114.2.7 yamt struct acpi_devnode *ad;
945 1.114.2.7 yamt ACPI_DEVICE_INFO *di;
946 1.1 thorpej
947 1.114.2.7 yamt SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
948 1.1 thorpej
949 1.114.2.7 yamt if (ad->ad_device != NULL)
950 1.114.2.7 yamt continue;
951 1.1 thorpej
952 1.114.2.7 yamt /*
953 1.114.2.7 yamt * There is a bug in ACPICA: it defines the type
954 1.114.2.7 yamt * of the scopes incorrectly for its own reasons.
955 1.114.2.7 yamt */
956 1.114.2.7 yamt if (acpi_is_scope(ad) != false)
957 1.114.2.7 yamt continue;
958 1.1 thorpej
959 1.114.2.7 yamt di = ad->ad_devinfo;
960 1.1 thorpej
961 1.114.2.7 yamt /*
962 1.114.2.7 yamt * We only attach devices which are present, enabled, and
963 1.114.2.7 yamt * functioning properly. However, if a device is enabled,
964 1.114.2.7 yamt * it is decoding resources and we should claim these,
965 1.114.2.7 yamt * if possible. This requires changes to bus_space(9).
966 1.114.2.7 yamt * Note: there is a possible race condition, because _STA
967 1.114.2.7 yamt * may have changed since di->CurrentStatus was set.
968 1.114.2.7 yamt */
969 1.114.2.7 yamt if (di->Type == ACPI_TYPE_DEVICE) {
970 1.1 thorpej
971 1.114.2.7 yamt if ((di->Valid & ACPI_VALID_STA) != 0 &&
972 1.114.2.7 yamt (di->CurrentStatus & ACPI_STA_OK) != ACPI_STA_OK)
973 1.114.2.7 yamt continue;
974 1.114.2.7 yamt }
975 1.4 thorpej
976 1.114.2.7 yamt /*
977 1.114.2.7 yamt * The same problem as above. As for example
978 1.114.2.7 yamt * thermal zones and power resources do not
979 1.114.2.7 yamt * have a valid HID, only evaluate devices.
980 1.114.2.7 yamt */
981 1.114.2.7 yamt if (di->Type == ACPI_TYPE_DEVICE &&
982 1.114.2.7 yamt (di->Valid & ACPI_VALID_HID) == 0)
983 1.114.2.7 yamt continue;
984 1.114.2.6 yamt
985 1.114.2.7 yamt /*
986 1.114.2.7 yamt * Handled internally.
987 1.114.2.7 yamt */
988 1.114.2.7 yamt if (di->Type == ACPI_TYPE_POWER)
989 1.114.2.7 yamt continue;
990 1.114.2.6 yamt
991 1.114.2.7 yamt /*
992 1.114.2.7 yamt * Skip ignored HIDs.
993 1.114.2.7 yamt */
994 1.114.2.7 yamt if (acpi_match_hid(di, acpi_ignored_ids))
995 1.114.2.7 yamt continue;
996 1.114.2.6 yamt
997 1.114.2.7 yamt aa.aa_node = ad;
998 1.114.2.7 yamt aa.aa_iot = sc->sc_iot;
999 1.114.2.7 yamt aa.aa_memt = sc->sc_memt;
1000 1.114.2.7 yamt aa.aa_pc = sc->sc_pc;
1001 1.114.2.7 yamt aa.aa_pciflags = sc->sc_pciflags;
1002 1.114.2.7 yamt aa.aa_ic = sc->sc_ic;
1003 1.114.2.6 yamt
1004 1.114.2.7 yamt ad->ad_device = config_found_ia(sc->sc_dev,
1005 1.114.2.7 yamt "acpinodebus", &aa, acpi_print);
1006 1.114.2.7 yamt }
1007 1.114.2.6 yamt }
1008 1.114.2.6 yamt
1009 1.114.2.7 yamt static void
1010 1.114.2.7 yamt acpi_rescan_capabilities(struct acpi_softc *sc)
1011 1.4 thorpej {
1012 1.114.2.7 yamt struct acpi_devnode *ad;
1013 1.114.2.7 yamt ACPI_HANDLE tmp;
1014 1.114.2.6 yamt ACPI_STATUS rv;
1015 1.4 thorpej
1016 1.114.2.7 yamt SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1017 1.4 thorpej
1018 1.114.2.7 yamt if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
1019 1.114.2.7 yamt continue;
1020 1.114.2.6 yamt
1021 1.114.2.7 yamt /*
1022 1.114.2.7 yamt * Scan power resource capabilities.
1023 1.114.2.7 yamt *
1024 1.114.2.7 yamt * If any power states are supported,
1025 1.114.2.7 yamt * at least _PR0 and _PR3 must be present.
1026 1.114.2.7 yamt */
1027 1.114.2.7 yamt rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
1028 1.4 thorpej
1029 1.114.2.7 yamt if (ACPI_SUCCESS(rv)) {
1030 1.114.2.7 yamt ad->ad_flags |= ACPI_DEVICE_POWER;
1031 1.114.2.7 yamt acpi_power_add(ad);
1032 1.114.2.7 yamt }
1033 1.46 mycroft
1034 1.114.2.7 yamt /*
1035 1.114.2.7 yamt * Scan wake-up capabilities.
1036 1.114.2.7 yamt */
1037 1.114.2.7 yamt rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
1038 1.114.2.6 yamt
1039 1.114.2.7 yamt if (ACPI_SUCCESS(rv)) {
1040 1.114.2.7 yamt ad->ad_flags |= ACPI_DEVICE_WAKEUP;
1041 1.114.2.7 yamt acpi_wakedev_add(ad);
1042 1.114.2.7 yamt }
1043 1.114.2.6 yamt }
1044 1.7 sommerfe }
1045 1.7 sommerfe
1046 1.114.2.7 yamt static int
1047 1.114.2.7 yamt acpi_print(void *aux, const char *pnp)
1048 1.7 sommerfe {
1049 1.114.2.7 yamt struct acpi_attach_args *aa = aux;
1050 1.7 sommerfe ACPI_STATUS rv;
1051 1.7 sommerfe
1052 1.114.2.7 yamt if (pnp) {
1053 1.114.2.7 yamt if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1054 1.114.2.7 yamt char *pnpstr =
1055 1.114.2.7 yamt aa->aa_node->ad_devinfo->HardwareId.String;
1056 1.114.2.7 yamt ACPI_BUFFER buf;
1057 1.114.2.7 yamt
1058 1.114.2.7 yamt aprint_normal("%s (%s) ", aa->aa_node->ad_name,
1059 1.114.2.7 yamt pnpstr);
1060 1.7 sommerfe
1061 1.114.2.7 yamt rv = acpi_eval_struct(aa->aa_node->ad_handle,
1062 1.114.2.7 yamt "_STR", &buf);
1063 1.114.2.7 yamt if (ACPI_SUCCESS(rv)) {
1064 1.114.2.7 yamt ACPI_OBJECT *obj = buf.Pointer;
1065 1.114.2.7 yamt switch (obj->Type) {
1066 1.114.2.7 yamt case ACPI_TYPE_STRING:
1067 1.114.2.7 yamt aprint_normal("[%s] ", obj->String.Pointer);
1068 1.114.2.7 yamt break;
1069 1.114.2.7 yamt case ACPI_TYPE_BUFFER:
1070 1.114.2.7 yamt aprint_normal("buffer %p ", obj->Buffer.Pointer);
1071 1.114.2.7 yamt break;
1072 1.114.2.7 yamt default:
1073 1.114.2.7 yamt aprint_normal("type %u ",obj->Type);
1074 1.114.2.7 yamt break;
1075 1.114.2.7 yamt }
1076 1.114.2.7 yamt ACPI_FREE(buf.Pointer);
1077 1.114.2.7 yamt }
1078 1.114.2.7 yamt else
1079 1.114.2.7 yamt acpi_print_dev(pnpstr);
1080 1.7 sommerfe
1081 1.114.2.7 yamt aprint_normal("at %s", pnp);
1082 1.114.2.7 yamt } else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
1083 1.114.2.7 yamt aprint_normal("%s (ACPI Object Type '%s' "
1084 1.114.2.7 yamt "[0x%02x]) ", aa->aa_node->ad_name,
1085 1.114.2.7 yamt AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
1086 1.114.2.7 yamt aa->aa_node->ad_devinfo->Type);
1087 1.114.2.7 yamt aprint_normal("at %s", pnp);
1088 1.114.2.7 yamt } else
1089 1.114.2.7 yamt return 0;
1090 1.114.2.7 yamt } else {
1091 1.114.2.7 yamt aprint_normal(" (%s", aa->aa_node->ad_name);
1092 1.114.2.7 yamt if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1093 1.114.2.7 yamt aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
1094 1.114.2.7 yamt if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
1095 1.114.2.7 yamt const char *uid;
1096 1.7 sommerfe
1097 1.114.2.7 yamt uid = aa->aa_node->ad_devinfo->UniqueId.String;
1098 1.114.2.7 yamt if (uid[0] == '\0')
1099 1.114.2.7 yamt uid = "<null>";
1100 1.114.2.7 yamt aprint_normal("-%s", uid);
1101 1.114.2.7 yamt }
1102 1.114.2.7 yamt }
1103 1.114.2.7 yamt aprint_normal(")");
1104 1.114.2.7 yamt }
1105 1.114.2.7 yamt
1106 1.114.2.7 yamt return UNCONF;
1107 1.2 thorpej }
1108 1.2 thorpej
1109 1.2 thorpej /*
1110 1.114.2.7 yamt * Notify.
1111 1.114.2.6 yamt */
1112 1.114.2.7 yamt static void
1113 1.114.2.7 yamt acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
1114 1.114.2.6 yamt {
1115 1.114.2.7 yamt struct acpi_softc *sc = acpi_softc;
1116 1.114.2.7 yamt struct acpi_devnode *ad;
1117 1.114.2.6 yamt
1118 1.114.2.7 yamt KASSERT(sc != NULL);
1119 1.114.2.7 yamt KASSERT(aux == NULL);
1120 1.114.2.7 yamt KASSERT(acpi_active != 0);
1121 1.114.2.6 yamt
1122 1.114.2.7 yamt if (acpi_suspended != 0)
1123 1.114.2.7 yamt return;
1124 1.114.2.6 yamt
1125 1.114.2.7 yamt /*
1126 1.114.2.7 yamt * System: 0x00 - 0x7F.
1127 1.114.2.7 yamt * Device: 0x80 - 0xFF.
1128 1.114.2.7 yamt */
1129 1.114.2.7 yamt switch (event) {
1130 1.114.2.6 yamt
1131 1.114.2.7 yamt case ACPI_NOTIFY_BUS_CHECK:
1132 1.114.2.7 yamt case ACPI_NOTIFY_DEVICE_CHECK:
1133 1.114.2.7 yamt case ACPI_NOTIFY_DEVICE_WAKE:
1134 1.114.2.7 yamt case ACPI_NOTIFY_EJECT_REQUEST:
1135 1.114.2.7 yamt case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
1136 1.114.2.7 yamt case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1137 1.114.2.7 yamt case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1138 1.114.2.7 yamt case ACPI_NOTIFY_POWER_FAULT:
1139 1.114.2.7 yamt case ACPI_NOTIFY_CAPABILITIES_CHECK:
1140 1.114.2.7 yamt case ACPI_NOTIFY_DEVICE_PLD_CHECK:
1141 1.114.2.7 yamt case ACPI_NOTIFY_RESERVED:
1142 1.114.2.7 yamt case ACPI_NOTIFY_LOCALITY_UPDATE:
1143 1.114.2.7 yamt break;
1144 1.114.2.7 yamt }
1145 1.114.2.6 yamt
1146 1.114.2.7 yamt ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
1147 1.114.2.7 yamt "%s (%p)\n", event, acpi_name(handle), handle));
1148 1.114.2.6 yamt
1149 1.114.2.7 yamt /*
1150 1.114.2.7 yamt * We deliver notifications only to drivers
1151 1.114.2.7 yamt * that have been succesfully attached and
1152 1.114.2.7 yamt * that have registered a handler with us.
1153 1.114.2.7 yamt * The opaque pointer is always the device_t.
1154 1.114.2.7 yamt */
1155 1.114.2.7 yamt SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1156 1.114.2.6 yamt
1157 1.114.2.7 yamt if (ad->ad_device == NULL)
1158 1.114.2.7 yamt continue;
1159 1.114.2.6 yamt
1160 1.114.2.7 yamt if (ad->ad_notify == NULL)
1161 1.114.2.7 yamt continue;
1162 1.114.2.6 yamt
1163 1.114.2.7 yamt if (ad->ad_handle != handle)
1164 1.114.2.7 yamt continue;
1165 1.65 kochi
1166 1.114.2.7 yamt (*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
1167 1.65 kochi
1168 1.114.2.7 yamt return;
1169 1.65 kochi }
1170 1.65 kochi
1171 1.114.2.7 yamt aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
1172 1.114.2.7 yamt "for %s (%p)\n", event, acpi_name(handle), handle);
1173 1.65 kochi }
1174 1.65 kochi
1175 1.114.2.7 yamt bool
1176 1.114.2.7 yamt acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
1177 1.65 kochi {
1178 1.114.2.7 yamt struct acpi_softc *sc = acpi_softc;
1179 1.65 kochi
1180 1.114.2.7 yamt KASSERT(sc != NULL);
1181 1.114.2.7 yamt KASSERT(acpi_active != 0);
1182 1.65 kochi
1183 1.114.2.7 yamt if (acpi_suspended != 0)
1184 1.114.2.7 yamt goto fail;
1185 1.65 kochi
1186 1.114.2.7 yamt if (ad == NULL || notify == NULL)
1187 1.114.2.7 yamt goto fail;
1188 1.2 thorpej
1189 1.114.2.7 yamt ad->ad_notify = notify;
1190 1.54 kochi
1191 1.114.2.7 yamt return true;
1192 1.54 kochi
1193 1.114.2.7 yamt fail:
1194 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "failed to register notify "
1195 1.114.2.7 yamt "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
1196 1.54 kochi
1197 1.114.2.7 yamt return false;
1198 1.114.2.7 yamt }
1199 1.54 kochi
1200 1.114.2.7 yamt void
1201 1.114.2.7 yamt acpi_deregister_notify(struct acpi_devnode *ad)
1202 1.114.2.7 yamt {
1203 1.54 kochi
1204 1.114.2.7 yamt ad->ad_notify = NULL;
1205 1.10 tshiozak }
1206 1.10 tshiozak
1207 1.69 kochi /*
1208 1.114.2.7 yamt * Fixed buttons.
1209 1.69 kochi */
1210 1.114.2.2 yamt static void
1211 1.114.2.7 yamt acpi_register_fixed_button(struct acpi_softc *sc, int event)
1212 1.69 kochi {
1213 1.114.2.7 yamt struct sysmon_pswitch *smpsw;
1214 1.69 kochi ACPI_STATUS rv;
1215 1.114.2.7 yamt int type;
1216 1.69 kochi
1217 1.114.2.7 yamt switch (event) {
1218 1.114.2.6 yamt
1219 1.114.2.7 yamt case ACPI_EVENT_POWER_BUTTON:
1220 1.114.2.6 yamt
1221 1.114.2.7 yamt if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
1222 1.114.2.7 yamt return;
1223 1.114.2.6 yamt
1224 1.114.2.7 yamt type = PSWITCH_TYPE_POWER;
1225 1.114.2.7 yamt smpsw = &sc->sc_smpsw_power;
1226 1.114.2.7 yamt break;
1227 1.69 kochi
1228 1.114.2.7 yamt case ACPI_EVENT_SLEEP_BUTTON:
1229 1.114.2.6 yamt
1230 1.114.2.7 yamt if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
1231 1.114.2.7 yamt return;
1232 1.114.2.6 yamt
1233 1.114.2.7 yamt type = PSWITCH_TYPE_SLEEP;
1234 1.114.2.7 yamt smpsw = &sc->sc_smpsw_sleep;
1235 1.114.2.6 yamt break;
1236 1.69 kochi
1237 1.114.2.7 yamt default:
1238 1.114.2.7 yamt rv = AE_TYPE;
1239 1.114.2.7 yamt goto fail;
1240 1.114.2.7 yamt }
1241 1.114.2.7 yamt
1242 1.114.2.7 yamt smpsw->smpsw_type = type;
1243 1.114.2.7 yamt smpsw->smpsw_name = device_xname(sc->sc_dev);
1244 1.114.2.6 yamt
1245 1.114.2.7 yamt if (sysmon_pswitch_register(smpsw) != 0) {
1246 1.114.2.7 yamt rv = AE_ERROR;
1247 1.114.2.7 yamt goto fail;
1248 1.114.2.7 yamt }
1249 1.114.2.7 yamt
1250 1.114.2.7 yamt rv = AcpiInstallFixedEventHandler(event,
1251 1.114.2.7 yamt acpi_fixed_button_handler, smpsw);
1252 1.114.2.7 yamt
1253 1.114.2.7 yamt if (ACPI_FAILURE(rv)) {
1254 1.114.2.7 yamt sysmon_pswitch_unregister(smpsw);
1255 1.114.2.7 yamt goto fail;
1256 1.114.2.7 yamt }
1257 1.114.2.6 yamt
1258 1.114.2.7 yamt aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
1259 1.114.2.7 yamt (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
1260 1.114.2.6 yamt
1261 1.114.2.7 yamt return;
1262 1.114.2.7 yamt
1263 1.114.2.7 yamt fail:
1264 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "failed to register "
1265 1.114.2.7 yamt "fixed event: %s\n", AcpiFormatException(rv));
1266 1.114.2.7 yamt }
1267 1.114.2.7 yamt
1268 1.114.2.7 yamt static void
1269 1.114.2.7 yamt acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
1270 1.114.2.7 yamt {
1271 1.114.2.7 yamt struct sysmon_pswitch *smpsw;
1272 1.114.2.7 yamt ACPI_STATUS rv;
1273 1.114.2.7 yamt
1274 1.114.2.7 yamt switch (event) {
1275 1.114.2.7 yamt
1276 1.114.2.7 yamt case ACPI_EVENT_POWER_BUTTON:
1277 1.114.2.7 yamt smpsw = &sc->sc_smpsw_power;
1278 1.114.2.7 yamt
1279 1.114.2.7 yamt if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
1280 1.114.2.7 yamt KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
1281 1.114.2.7 yamt return;
1282 1.114.2.7 yamt }
1283 1.114.2.7 yamt
1284 1.114.2.7 yamt break;
1285 1.114.2.7 yamt
1286 1.114.2.7 yamt case ACPI_EVENT_SLEEP_BUTTON:
1287 1.114.2.7 yamt smpsw = &sc->sc_smpsw_sleep;
1288 1.114.2.7 yamt
1289 1.114.2.7 yamt if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
1290 1.114.2.7 yamt KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
1291 1.114.2.7 yamt return;
1292 1.114.2.7 yamt }
1293 1.114.2.6 yamt
1294 1.114.2.6 yamt break;
1295 1.114.2.6 yamt
1296 1.114.2.6 yamt default:
1297 1.114.2.7 yamt rv = AE_TYPE;
1298 1.114.2.7 yamt goto fail;
1299 1.114.2.6 yamt }
1300 1.69 kochi
1301 1.114.2.7 yamt rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
1302 1.69 kochi
1303 1.114.2.7 yamt if (ACPI_SUCCESS(rv)) {
1304 1.114.2.7 yamt sysmon_pswitch_unregister(smpsw);
1305 1.114.2.7 yamt return;
1306 1.114.2.7 yamt }
1307 1.69 kochi
1308 1.114.2.7 yamt fail:
1309 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "failed to deregister "
1310 1.114.2.7 yamt "fixed event: %s\n", AcpiFormatException(rv));
1311 1.114.2.2 yamt }
1312 1.114.2.2 yamt
1313 1.114.2.7 yamt static uint32_t
1314 1.114.2.7 yamt acpi_fixed_button_handler(void *context)
1315 1.114.2.2 yamt {
1316 1.114.2.7 yamt static const int handler = OSL_NOTIFY_HANDLER;
1317 1.114.2.7 yamt struct sysmon_pswitch *smpsw = context;
1318 1.114.2.2 yamt
1319 1.114.2.7 yamt (void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
1320 1.10 tshiozak
1321 1.114.2.7 yamt return ACPI_INTERRUPT_HANDLED;
1322 1.114.2.7 yamt }
1323 1.10 tshiozak
1324 1.114.2.7 yamt static void
1325 1.114.2.7 yamt acpi_fixed_button_pressed(void *context)
1326 1.10 tshiozak {
1327 1.114.2.7 yamt struct sysmon_pswitch *smpsw = context;
1328 1.114.2.7 yamt
1329 1.114.2.7 yamt ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
1330 1.114.2.7 yamt (smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
1331 1.114.2.7 yamt "power" : "sleep"));
1332 1.10 tshiozak
1333 1.114.2.7 yamt sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1334 1.10 tshiozak }
1335 1.10 tshiozak
1336 1.10 tshiozak /*
1337 1.114.2.7 yamt * Sleep.
1338 1.10 tshiozak */
1339 1.114.2.7 yamt static void
1340 1.114.2.7 yamt acpi_sleep_init(struct acpi_softc *sc)
1341 1.114.2.7 yamt {
1342 1.114.2.7 yamt uint8_t a, b, i;
1343 1.114.2.7 yamt ACPI_STATUS rv;
1344 1.10 tshiozak
1345 1.114.2.7 yamt CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
1346 1.114.2.7 yamt CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
1347 1.114.2.7 yamt CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
1348 1.114.2.7 yamt
1349 1.114.2.7 yamt /*
1350 1.114.2.7 yamt * Evaluate supported sleep states.
1351 1.114.2.7 yamt */
1352 1.114.2.7 yamt for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
1353 1.114.2.7 yamt
1354 1.114.2.7 yamt rv = AcpiGetSleepTypeData(i, &a, &b);
1355 1.114.2.7 yamt
1356 1.114.2.7 yamt if (ACPI_SUCCESS(rv))
1357 1.114.2.7 yamt sc->sc_sleepstates |= __BIT(i);
1358 1.114.2.7 yamt }
1359 1.114.2.7 yamt }
1360 1.114.2.7 yamt
1361 1.114.2.7 yamt void
1362 1.10 tshiozak acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1363 1.10 tshiozak {
1364 1.114.2.7 yamt ACPI_STATUS rv;
1365 1.104 jmcneill int err;
1366 1.10 tshiozak
1367 1.114.2.7 yamt if (state == sc->sc_sleepstate)
1368 1.114.2.7 yamt return;
1369 1.92 christos
1370 1.114.2.7 yamt aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
1371 1.92 christos
1372 1.10 tshiozak switch (state) {
1373 1.114.2.7 yamt
1374 1.10 tshiozak case ACPI_STATE_S0:
1375 1.114.2.7 yamt sc->sc_sleepstate = ACPI_STATE_S0;
1376 1.114.2.7 yamt return;
1377 1.114.2.7 yamt
1378 1.10 tshiozak case ACPI_STATE_S1:
1379 1.10 tshiozak case ACPI_STATE_S2:
1380 1.10 tshiozak case ACPI_STATE_S3:
1381 1.10 tshiozak case ACPI_STATE_S4:
1382 1.114.2.7 yamt
1383 1.114.2.7 yamt if ((sc->sc_sleepstates & __BIT(state)) == 0) {
1384 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "sleep state "
1385 1.114.2.7 yamt "S%d is not available\n", state);
1386 1.114.2.7 yamt return;
1387 1.10 tshiozak }
1388 1.104 jmcneill
1389 1.114.2.7 yamt /*
1390 1.114.2.7 yamt * Evaluate the _TTS method. This should be done before
1391 1.114.2.7 yamt * pmf_system_suspend(9) and the evaluation of _PTS.
1392 1.114.2.7 yamt * We should also re-evaluate this once we return to
1393 1.114.2.7 yamt * S0 or if we abort the sleep state transition in the
1394 1.114.2.7 yamt * middle (see ACPI 3.0, section 7.3.6). In reality,
1395 1.114.2.7 yamt * however, the _TTS method is seldom seen in the field.
1396 1.114.2.7 yamt */
1397 1.114.2.7 yamt rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
1398 1.114.2.7 yamt
1399 1.114.2.7 yamt if (ACPI_SUCCESS(rv))
1400 1.114.2.7 yamt aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
1401 1.114.2.4 yamt
1402 1.114.2.7 yamt if (state != ACPI_STATE_S1 &&
1403 1.114.2.7 yamt pmf_system_suspend(PMF_Q_NONE) != true) {
1404 1.113 jmcneill aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1405 1.104 jmcneill break;
1406 1.104 jmcneill }
1407 1.104 jmcneill
1408 1.114.2.7 yamt /*
1409 1.114.2.7 yamt * This will evaluate the _PTS and _SST methods,
1410 1.114.2.7 yamt * but unlike the documentation claims, not _GTS,
1411 1.114.2.7 yamt * which is evaluated in AcpiEnterSleepState().
1412 1.114.2.7 yamt * This must be called with interrupts enabled.
1413 1.114.2.7 yamt */
1414 1.114.2.7 yamt rv = AcpiEnterSleepStatePrep(state);
1415 1.114.2.7 yamt
1416 1.114.2.7 yamt if (ACPI_FAILURE(rv)) {
1417 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "failed to prepare "
1418 1.114.2.7 yamt "S%d: %s\n", state, AcpiFormatException(rv));
1419 1.10 tshiozak break;
1420 1.10 tshiozak }
1421 1.104 jmcneill
1422 1.114.2.7 yamt /*
1423 1.114.2.7 yamt * After the _PTS method has been evaluated, we can
1424 1.114.2.7 yamt * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
1425 1.114.2.7 yamt */
1426 1.114.2.7 yamt acpi_wakedev_commit(sc, state);
1427 1.114.2.7 yamt
1428 1.114.2.7 yamt sc->sc_sleepstate = state;
1429 1.114.2.7 yamt
1430 1.92 christos if (state == ACPI_STATE_S1) {
1431 1.114.2.7 yamt
1432 1.114.2.7 yamt /* Just enter the state. */
1433 1.12 kanaoka acpi_md_OsDisableInterrupt();
1434 1.114.2.7 yamt rv = AcpiEnterSleepState(state);
1435 1.114.2.7 yamt
1436 1.114.2.7 yamt if (ACPI_FAILURE(rv))
1437 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "failed to "
1438 1.114.2.7 yamt "enter S1: %s\n", AcpiFormatException(rv));
1439 1.114.2.7 yamt
1440 1.114.2.7 yamt (void)AcpiLeaveSleepState(state);
1441 1.114.2.7 yamt
1442 1.10 tshiozak } else {
1443 1.114.2.7 yamt
1444 1.104 jmcneill err = acpi_md_sleep(state);
1445 1.114.2.7 yamt
1446 1.104 jmcneill if (state == ACPI_STATE_S4)
1447 1.10 tshiozak AcpiEnable();
1448 1.114.2.7 yamt
1449 1.114.2.6 yamt pmf_system_bus_resume(PMF_Q_NONE);
1450 1.114.2.7 yamt (void)AcpiLeaveSleepState(state);
1451 1.114.2.6 yamt pmf_system_resume(PMF_Q_NONE);
1452 1.10 tshiozak }
1453 1.104 jmcneill
1454 1.10 tshiozak break;
1455 1.114.2.7 yamt
1456 1.10 tshiozak case ACPI_STATE_S5:
1457 1.114.2.7 yamt
1458 1.114.2.7 yamt (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
1459 1.114.2.7 yamt
1460 1.114.2.7 yamt rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1461 1.114.2.7 yamt
1462 1.114.2.7 yamt if (ACPI_FAILURE(rv)) {
1463 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "failed to prepare "
1464 1.114.2.7 yamt "S%d: %s\n", state, AcpiFormatException(rv));
1465 1.42 kochi break;
1466 1.42 kochi }
1467 1.114.2.7 yamt
1468 1.104 jmcneill DELAY(1000000);
1469 1.10 tshiozak
1470 1.114.2.7 yamt sc->sc_sleepstate = state;
1471 1.114.2.7 yamt acpi_md_OsDisableInterrupt();
1472 1.13 augustss
1473 1.114.2.7 yamt (void)AcpiEnterSleepState(ACPI_STATE_S5);
1474 1.13 augustss
1475 1.114.2.7 yamt aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
1476 1.13 augustss
1477 1.114.2.7 yamt break;
1478 1.13 augustss }
1479 1.13 augustss
1480 1.114.2.7 yamt sc->sc_sleepstate = ACPI_STATE_S0;
1481 1.13 augustss
1482 1.114.2.7 yamt (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
1483 1.13 augustss }
1484 1.79 cube
1485 1.114.2.7 yamt /*
1486 1.114.2.7 yamt * Sysctl.
1487 1.114.2.7 yamt */
1488 1.79 cube SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1489 1.79 cube {
1490 1.114.2.7 yamt const struct sysctlnode *mnode, *rnode;
1491 1.114.2.7 yamt int err;
1492 1.79 cube
1493 1.114.2.7 yamt err = sysctl_createv(clog, 0, NULL, &rnode,
1494 1.114.2.7 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
1495 1.114.2.7 yamt NULL, NULL, 0, NULL, 0,
1496 1.114.2.7 yamt CTL_HW, CTL_EOL);
1497 1.114.2.7 yamt
1498 1.114.2.7 yamt if (err != 0)
1499 1.79 cube return;
1500 1.79 cube
1501 1.114.2.7 yamt err = sysctl_createv(clog, 0, &rnode, &rnode,
1502 1.114.2.7 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE,
1503 1.114.2.7 yamt "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
1504 1.79 cube NULL, 0, NULL, 0,
1505 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1506 1.114.2.7 yamt
1507 1.114.2.7 yamt if (err != 0)
1508 1.79 cube return;
1509 1.79 cube
1510 1.114.2.7 yamt (void)sysctl_createv(NULL, 0, &rnode, NULL,
1511 1.114.2.7 yamt CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1512 1.114.2.7 yamt "root", SYSCTL_DESCR("ACPI root pointer"),
1513 1.109 jmcneill NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1514 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1515 1.114.2.7 yamt
1516 1.114.2.7 yamt (void)sysctl_createv(NULL, 0, &rnode, NULL,
1517 1.114.2.7 yamt CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
1518 1.114.2.7 yamt "supported_states", SYSCTL_DESCR("Supported system states"),
1519 1.114.2.7 yamt sysctl_hw_acpi_sleepstates, 0, NULL, 0,
1520 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1521 1.114.2.7 yamt
1522 1.114.2.7 yamt err = sysctl_createv(NULL, 0, NULL, &mnode,
1523 1.114.2.7 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
1524 1.114.2.7 yamt NULL, NULL, 0, NULL, 0,
1525 1.114.2.7 yamt CTL_MACHDEP, CTL_EOL);
1526 1.114.2.7 yamt
1527 1.114.2.7 yamt if (err == 0) {
1528 1.114.2.7 yamt
1529 1.114.2.7 yamt (void)sysctl_createv(NULL, 0, &mnode, NULL,
1530 1.114.2.7 yamt CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
1531 1.114.2.7 yamt "sleep_state", SYSCTL_DESCR("System sleep state"),
1532 1.114.2.7 yamt sysctl_hw_acpi_sleepstate, 0, NULL, 0,
1533 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1534 1.114.2.7 yamt }
1535 1.114.2.7 yamt
1536 1.114.2.7 yamt err = sysctl_createv(clog, 0, &rnode, &rnode,
1537 1.114.2.7 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE,
1538 1.114.2.7 yamt "stat", SYSCTL_DESCR("ACPI statistics"),
1539 1.114.2.7 yamt NULL, 0, NULL, 0,
1540 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1541 1.114.2.7 yamt
1542 1.114.2.7 yamt if (err != 0)
1543 1.86 jmcneill return;
1544 1.114.2.7 yamt
1545 1.114.2.7 yamt (void)sysctl_createv(clog, 0, &rnode, NULL,
1546 1.114.2.7 yamt CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1547 1.114.2.7 yamt "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
1548 1.114.2.7 yamt NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
1549 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1550 1.114.2.7 yamt
1551 1.114.2.7 yamt (void)sysctl_createv(clog, 0, &rnode, NULL,
1552 1.114.2.7 yamt CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1553 1.114.2.7 yamt "sci", SYSCTL_DESCR("Number of SCI interrupts"),
1554 1.114.2.7 yamt NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
1555 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1556 1.114.2.7 yamt
1557 1.114.2.7 yamt (void)sysctl_createv(clog, 0, &rnode, NULL,
1558 1.114.2.7 yamt CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1559 1.114.2.7 yamt "fixed", SYSCTL_DESCR("Number of fixed events"),
1560 1.114.2.7 yamt sysctl_hw_acpi_fixedstats, 0, NULL, 0,
1561 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1562 1.114.2.7 yamt
1563 1.114.2.7 yamt (void)sysctl_createv(clog, 0, &rnode, NULL,
1564 1.114.2.7 yamt CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1565 1.114.2.7 yamt "method", SYSCTL_DESCR("Number of methods executed"),
1566 1.114.2.7 yamt NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
1567 1.114.2.7 yamt CTL_CREATE, CTL_EOL);
1568 1.114.2.7 yamt
1569 1.114.2.7 yamt CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
1570 1.114.2.7 yamt CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
1571 1.114.2.7 yamt }
1572 1.114.2.7 yamt
1573 1.114.2.7 yamt static int
1574 1.114.2.7 yamt sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
1575 1.114.2.7 yamt {
1576 1.114.2.7 yamt struct sysctlnode node;
1577 1.114.2.7 yamt uint64_t t;
1578 1.114.2.7 yamt int err, i;
1579 1.114.2.7 yamt
1580 1.114.2.7 yamt for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
1581 1.114.2.7 yamt t += AcpiFixedEventCount[i];
1582 1.114.2.7 yamt
1583 1.114.2.7 yamt node = *rnode;
1584 1.114.2.7 yamt node.sysctl_data = &t;
1585 1.114.2.7 yamt
1586 1.114.2.7 yamt err = sysctl_lookup(SYSCTLFN_CALL(&node));
1587 1.114.2.7 yamt
1588 1.114.2.7 yamt if (err || newp == NULL)
1589 1.114.2.7 yamt return err;
1590 1.114.2.7 yamt
1591 1.114.2.7 yamt return 0;
1592 1.86 jmcneill }
1593 1.86 jmcneill
1594 1.86 jmcneill static int
1595 1.86 jmcneill sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1596 1.86 jmcneill {
1597 1.114.2.7 yamt struct acpi_softc *sc = acpi_softc;
1598 1.86 jmcneill struct sysctlnode node;
1599 1.114.2.7 yamt int err, t;
1600 1.114.2.7 yamt
1601 1.114.2.7 yamt if (acpi_softc == NULL)
1602 1.114.2.7 yamt return ENOSYS;
1603 1.86 jmcneill
1604 1.86 jmcneill node = *rnode;
1605 1.114.2.7 yamt t = sc->sc_sleepstate;
1606 1.86 jmcneill node.sysctl_data = &t;
1607 1.114.2.7 yamt
1608 1.114.2.7 yamt err = sysctl_lookup(SYSCTLFN_CALL(&node));
1609 1.114.2.7 yamt
1610 1.114.2.7 yamt if (err || newp == NULL)
1611 1.114.2.7 yamt return err;
1612 1.114.2.7 yamt
1613 1.114.2.7 yamt if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
1614 1.114.2.7 yamt return EINVAL;
1615 1.114.2.7 yamt
1616 1.114.2.7 yamt acpi_enter_sleep_state(sc, t);
1617 1.114.2.7 yamt
1618 1.114.2.7 yamt return 0;
1619 1.114.2.7 yamt }
1620 1.114.2.7 yamt
1621 1.114.2.7 yamt static int
1622 1.114.2.7 yamt sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
1623 1.114.2.7 yamt {
1624 1.114.2.7 yamt struct acpi_softc *sc = acpi_softc;
1625 1.114.2.7 yamt struct sysctlnode node;
1626 1.114.2.7 yamt char t[3 * 6 + 1];
1627 1.114.2.7 yamt int err;
1628 1.86 jmcneill
1629 1.92 christos if (acpi_softc == NULL)
1630 1.92 christos return ENOSYS;
1631 1.86 jmcneill
1632 1.114.2.7 yamt (void)memset(t, '\0', sizeof(t));
1633 1.114.2.7 yamt
1634 1.114.2.7 yamt (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
1635 1.114.2.7 yamt ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
1636 1.114.2.7 yamt ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
1637 1.114.2.7 yamt ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
1638 1.114.2.7 yamt ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
1639 1.114.2.7 yamt ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
1640 1.114.2.7 yamt ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
1641 1.114.2.7 yamt
1642 1.114.2.7 yamt node = *rnode;
1643 1.114.2.7 yamt node.sysctl_data = &t;
1644 1.114.2.7 yamt
1645 1.114.2.7 yamt err = sysctl_lookup(SYSCTLFN_CALL(&node));
1646 1.114.2.7 yamt
1647 1.114.2.7 yamt if (err || newp == NULL)
1648 1.114.2.7 yamt return err;
1649 1.86 jmcneill
1650 1.86 jmcneill return 0;
1651 1.79 cube }
1652 1.108 jmcneill
1653 1.114.2.7 yamt /*
1654 1.114.2.7 yamt * Tables.
1655 1.114.2.7 yamt */
1656 1.114.2.7 yamt ACPI_PHYSICAL_ADDRESS
1657 1.114.2.7 yamt acpi_OsGetRootPointer(void)
1658 1.114.2.7 yamt {
1659 1.114.2.7 yamt ACPI_PHYSICAL_ADDRESS PhysicalAddress;
1660 1.114.2.7 yamt
1661 1.114.2.7 yamt /*
1662 1.114.2.7 yamt * We let MD code handle this since there are multiple ways to do it:
1663 1.114.2.7 yamt *
1664 1.114.2.7 yamt * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
1665 1.114.2.7 yamt *
1666 1.114.2.7 yamt * IA-64: Use the EFI.
1667 1.114.2.7 yamt */
1668 1.114.2.7 yamt PhysicalAddress = acpi_md_OsGetRootPointer();
1669 1.114.2.7 yamt
1670 1.114.2.7 yamt if (acpi_root_pointer == 0)
1671 1.114.2.7 yamt acpi_root_pointer = PhysicalAddress;
1672 1.114.2.7 yamt
1673 1.114.2.7 yamt return PhysicalAddress;
1674 1.114.2.7 yamt }
1675 1.114.2.7 yamt
1676 1.108 jmcneill static ACPI_TABLE_HEADER *
1677 1.108 jmcneill acpi_map_rsdt(void)
1678 1.108 jmcneill {
1679 1.108 jmcneill ACPI_PHYSICAL_ADDRESS paddr;
1680 1.108 jmcneill ACPI_TABLE_RSDP *rsdp;
1681 1.108 jmcneill
1682 1.108 jmcneill paddr = AcpiOsGetRootPointer();
1683 1.114.2.7 yamt
1684 1.114.2.7 yamt if (paddr == 0)
1685 1.108 jmcneill return NULL;
1686 1.114.2.7 yamt
1687 1.108 jmcneill rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1688 1.114.2.7 yamt
1689 1.114.2.7 yamt if (rsdp == NULL)
1690 1.108 jmcneill return NULL;
1691 1.114.2.7 yamt
1692 1.108 jmcneill if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1693 1.114.2.7 yamt paddr = rsdp->XsdtPhysicalAddress;
1694 1.108 jmcneill else
1695 1.114.2.7 yamt paddr = rsdp->RsdtPhysicalAddress;
1696 1.114.2.7 yamt
1697 1.108 jmcneill AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1698 1.108 jmcneill
1699 1.108 jmcneill return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1700 1.108 jmcneill }
1701 1.108 jmcneill
1702 1.108 jmcneill static void
1703 1.108 jmcneill acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1704 1.108 jmcneill {
1705 1.114.2.7 yamt
1706 1.108 jmcneill if (rsdt == NULL)
1707 1.108 jmcneill return;
1708 1.108 jmcneill
1709 1.108 jmcneill AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1710 1.108 jmcneill }
1711 1.114.2.7 yamt
1712 1.114.2.7 yamt /*
1713 1.114.2.7 yamt * XXX: Refactor to be a generic function that maps tables.
1714 1.114.2.7 yamt */
1715 1.114.2.7 yamt ACPI_STATUS
1716 1.114.2.7 yamt acpi_madt_map(void)
1717 1.114.2.7 yamt {
1718 1.114.2.7 yamt ACPI_STATUS rv;
1719 1.114.2.7 yamt
1720 1.114.2.7 yamt if (madt_header != NULL)
1721 1.114.2.7 yamt return AE_ALREADY_EXISTS;
1722 1.114.2.7 yamt
1723 1.114.2.7 yamt rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
1724 1.114.2.7 yamt
1725 1.114.2.7 yamt if (ACPI_FAILURE(rv))
1726 1.114.2.7 yamt return rv;
1727 1.114.2.7 yamt
1728 1.114.2.7 yamt return AE_OK;
1729 1.114.2.7 yamt }
1730 1.114.2.7 yamt
1731 1.114.2.7 yamt void
1732 1.114.2.7 yamt acpi_madt_unmap(void)
1733 1.114.2.7 yamt {
1734 1.114.2.7 yamt madt_header = NULL;
1735 1.114.2.7 yamt }
1736 1.114.2.7 yamt
1737 1.114.2.7 yamt /*
1738 1.114.2.7 yamt * XXX: Refactor to be a generic function that walks tables.
1739 1.114.2.7 yamt */
1740 1.114.2.7 yamt void
1741 1.114.2.7 yamt acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
1742 1.114.2.7 yamt {
1743 1.114.2.7 yamt ACPI_SUBTABLE_HEADER *hdrp;
1744 1.114.2.7 yamt char *madtend, *where;
1745 1.114.2.7 yamt
1746 1.114.2.7 yamt madtend = (char *)madt_header + madt_header->Length;
1747 1.114.2.7 yamt where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
1748 1.114.2.7 yamt
1749 1.114.2.7 yamt while (where < madtend) {
1750 1.114.2.7 yamt
1751 1.114.2.7 yamt hdrp = (ACPI_SUBTABLE_HEADER *)where;
1752 1.114.2.7 yamt
1753 1.114.2.7 yamt if (ACPI_FAILURE(func(hdrp, aux)))
1754 1.114.2.7 yamt break;
1755 1.114.2.7 yamt
1756 1.114.2.7 yamt where += hdrp->Length;
1757 1.114.2.7 yamt }
1758 1.114.2.7 yamt }
1759 1.114.2.7 yamt
1760 1.114.2.7 yamt /*
1761 1.114.2.7 yamt * Miscellaneous.
1762 1.114.2.7 yamt */
1763 1.114.2.7 yamt static bool
1764 1.114.2.7 yamt acpi_is_scope(struct acpi_devnode *ad)
1765 1.114.2.7 yamt {
1766 1.114.2.7 yamt int i;
1767 1.114.2.7 yamt
1768 1.114.2.7 yamt /*
1769 1.114.2.7 yamt * Return true if the node is a root scope.
1770 1.114.2.7 yamt */
1771 1.114.2.7 yamt if (ad->ad_parent == NULL)
1772 1.114.2.7 yamt return false;
1773 1.114.2.7 yamt
1774 1.114.2.7 yamt if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
1775 1.114.2.7 yamt return false;
1776 1.114.2.7 yamt
1777 1.114.2.7 yamt for (i = 0; i < __arraycount(acpi_scopes); i++) {
1778 1.114.2.7 yamt
1779 1.114.2.7 yamt if (acpi_scopes[i] == NULL)
1780 1.114.2.7 yamt continue;
1781 1.114.2.7 yamt
1782 1.114.2.7 yamt if (ad->ad_handle == acpi_scopes[i])
1783 1.114.2.7 yamt return true;
1784 1.114.2.7 yamt }
1785 1.114.2.7 yamt
1786 1.114.2.7 yamt return false;
1787 1.114.2.7 yamt }
1788 1.114.2.7 yamt
1789 1.114.2.7 yamt /*
1790 1.114.2.7 yamt * ACPIVERBOSE.
1791 1.114.2.7 yamt */
1792 1.114.2.7 yamt void
1793 1.114.2.7 yamt acpi_load_verbose(void)
1794 1.114.2.7 yamt {
1795 1.114.2.7 yamt
1796 1.114.2.7 yamt if (acpi_verbose_loaded == 0) {
1797 1.114.2.7 yamt mutex_enter(&module_lock);
1798 1.114.2.7 yamt module_autoload("acpiverbose", MODULE_CLASS_MISC);
1799 1.114.2.7 yamt mutex_exit(&module_lock);
1800 1.114.2.7 yamt }
1801 1.114.2.7 yamt }
1802 1.114.2.7 yamt
1803 1.114.2.7 yamt void
1804 1.114.2.7 yamt acpi_print_verbose_stub(struct acpi_softc *sc)
1805 1.114.2.7 yamt {
1806 1.114.2.7 yamt
1807 1.114.2.7 yamt acpi_load_verbose();
1808 1.114.2.7 yamt
1809 1.114.2.7 yamt if (acpi_verbose_loaded != 0)
1810 1.114.2.7 yamt acpi_print_verbose(sc);
1811 1.114.2.7 yamt }
1812 1.114.2.7 yamt
1813 1.114.2.7 yamt void
1814 1.114.2.7 yamt acpi_print_dev_stub(const char *pnpstr)
1815 1.114.2.7 yamt {
1816 1.114.2.7 yamt
1817 1.114.2.7 yamt acpi_load_verbose();
1818 1.114.2.7 yamt
1819 1.114.2.7 yamt if (acpi_verbose_loaded != 0)
1820 1.114.2.7 yamt acpi_print_dev(pnpstr);
1821 1.114.2.7 yamt }
1822