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