xref: /netbsd-src/sys/dev/acpi/acpi.c (revision f983e71d70cfccf7b3de601eb4d998b2d886ede4)
1 /*	$NetBSD: acpi.c,v 1.114 2008/04/20 16:26:36 jmcneill Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum of By Noon Software, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright 2001, 2003 Wasabi Systems, Inc.
41  * All rights reserved.
42  *
43  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed for the NetBSD Project by
56  *	Wasabi Systems, Inc.
57  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
58  *    or promote products derived from this software without specific prior
59  *    written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
65  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
66  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71  * POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 /*
75  * Autoconfiguration support for the Intel ACPI Component Architecture
76  * ACPI reference implementation.
77  */
78 
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.114 2008/04/20 16:26:36 jmcneill Exp $");
81 
82 #include "opt_acpi.h"
83 #include "opt_pcifixup.h"
84 
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/device.h>
88 #include <sys/malloc.h>
89 #include <sys/mutex.h>
90 #include <sys/kernel.h>
91 #include <sys/proc.h>
92 #include <sys/sysctl.h>
93 
94 #include <dev/acpi/acpica.h>
95 #include <dev/acpi/acpireg.h>
96 #include <dev/acpi/acpivar.h>
97 #include <dev/acpi/acpi_osd.h>
98 #include <dev/acpi/acpi_timer.h>
99 #ifdef ACPIVERBOSE
100 #include <dev/acpi/acpidevs_data.h>
101 #endif
102 
103 #if defined(ACPI_PCI_FIXUP)
104 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED.  Please adjust your kernel configuration file.
105 #endif
106 
107 #ifdef PCI_INTR_FIXUP_DISABLED
108 #include <dev/pci/pcidevs.h>
109 #endif
110 
111 MALLOC_DECLARE(M_ACPI);
112 
113 #include <machine/acpi_machdep.h>
114 
115 #ifdef ACPI_DEBUGGER
116 #define	ACPI_DBGR_INIT		0x01
117 #define	ACPI_DBGR_TABLES	0x02
118 #define	ACPI_DBGR_ENABLE	0x04
119 #define	ACPI_DBGR_PROBE		0x08
120 #define	ACPI_DBGR_RUNNING	0x10
121 
122 static int acpi_dbgr = 0x00;
123 #endif
124 
125 static ACPI_TABLE_DESC	acpi_initial_tables[128];
126 
127 static int	acpi_match(device_t, struct cfdata *, void *);
128 static void	acpi_attach(device_t, device_t, void *);
129 static void	acpi_childdet(device_t, device_t);
130 
131 static int	acpi_print(void *aux, const char *);
132 
133 static int	sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
134 
135 extern struct cfdriver acpi_cd;
136 
137 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
138     acpi_match, acpi_attach, NULL, NULL, NULL, acpi_childdet);
139 
140 /*
141  * This is a flag we set when the ACPI subsystem is active.  Machine
142  * dependent code may wish to skip other steps (such as attaching
143  * subsystems that ACPI supercedes) when ACPI is active.
144  */
145 int	acpi_active;
146 int	acpi_force_load;
147 
148 /*
149  * Pointer to the ACPI subsystem's state.  There can be only
150  * one ACPI instance.
151  */
152 struct acpi_softc *acpi_softc;
153 
154 /*
155  * Locking stuff.
156  */
157 static kmutex_t acpi_slock;
158 static int acpi_locked;
159 extern kmutex_t acpi_interrupt_list_mtx;
160 
161 /*
162  * sysctl-related information
163  */
164 
165 static uint64_t acpi_root_pointer;	/* found as hw.acpi.root */
166 static int acpi_sleepstate = ACPI_STATE_S0;
167 static char acpi_supported_states[3 * 6 + 1] = "";;
168 
169 /*
170  * Prototypes.
171  */
172 static void		acpi_build_tree(struct acpi_softc *);
173 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
174 
175 static void		acpi_enable_fixed_events(struct acpi_softc *);
176 
177 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
178 static void		acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
179 static int		is_available_state(struct acpi_softc *, int);
180 
181 /*
182  * acpi_probe:
183  *
184  *	Probe for ACPI support.  This is called by the
185  *	machine-dependent ACPI front-end.  All of the
186  *	actual work is done by ACPICA.
187  *
188  *	NOTE: This is not an autoconfiguration interface function.
189  */
190 int
191 acpi_probe(void)
192 {
193 	static int beenhere;
194 	ACPI_TABLE_HEADER *rsdt;
195 	ACPI_STATUS rv;
196 
197 	if (beenhere != 0)
198 		panic("acpi_probe: ACPI has already been probed");
199 	beenhere = 1;
200 
201 	mutex_init(&acpi_slock, MUTEX_DEFAULT, IPL_NONE);
202 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
203 	acpi_locked = 0;
204 
205 	/*
206 	 * Start up ACPICA.
207 	 */
208 #ifdef ACPI_DEBUGGER
209 	if (acpi_dbgr & ACPI_DBGR_INIT)
210 		acpi_osd_debugger();
211 #endif
212 
213 	AcpiGbl_AllMethodsSerialized = FALSE;
214 	AcpiGbl_EnableInterpreterSlack = TRUE;
215 
216 	rv = AcpiInitializeSubsystem();
217 	if (ACPI_FAILURE(rv)) {
218 		printf("ACPI: unable to initialize ACPICA: %s\n",
219 		    AcpiFormatException(rv));
220 		return 0;
221 	}
222 
223 	rv = AcpiInitializeTables(acpi_initial_tables, 128, 0);
224 	if (ACPI_FAILURE(rv)) {
225 		printf("ACPI: unable to initialize ACPI tables: %s\n",
226 		    AcpiFormatException(rv));
227 		return 0;
228 	}
229 
230 	rv = AcpiReallocateRootTable();
231 	if (ACPI_FAILURE(rv)) {
232 		printf("ACPI: unable to reallocate root table: %s\n",
233 		    AcpiFormatException(rv));
234 		return 0;
235 	}
236 
237 #ifdef ACPI_DEBUGGER
238 	if (acpi_dbgr & ACPI_DBGR_TABLES)
239 		acpi_osd_debugger();
240 #endif
241 
242 	rv = AcpiLoadTables();
243 	if (ACPI_FAILURE(rv)) {
244 		printf("ACPI: unable to load tables: %s\n",
245 		    AcpiFormatException(rv));
246 		return 0;
247 	}
248 
249 	rsdt = acpi_map_rsdt();
250 	if (rsdt == NULL) {
251 		printf("ACPI: unable to map RSDT\n");
252 		return 0;
253 	}
254 
255 	if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
256 		printf("ACPI: BIOS implementation in listed as broken:\n");
257 		printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
258 		       "AslId <%4.4s,%08x>\n",
259 			rsdt->OemId, rsdt->OemTableId,
260 		        rsdt->OemRevision,
261 			rsdt->AslCompilerId,
262 		        rsdt->AslCompilerRevision);
263 		printf("ACPI: not used. set acpi_force_load to use anyway.\n");
264 		acpi_unmap_rsdt(rsdt);
265 		return 0;
266 	}
267 
268 	acpi_unmap_rsdt(rsdt);
269 
270 #if notyet
271 	/* Install the default address space handlers. */
272 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
273 	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
274 	if (ACPI_FAILURE(rv)) {
275 		printf("ACPI: unable to initialise SystemMemory handler: %s\n",
276 		    AcpiFormatException(rv));
277 		return 0;
278 	}
279 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
280 	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
281 	if (ACPI_FAILURE(rv)) {
282 		printf("ACPI: unable to initialise SystemIO handler: %s\n",
283 		     AcpiFormatException(rv));
284 		return 0;
285 	}
286 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
287 	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
288 	if (ACPI_FAILURE(rv)) {
289 		printf("ACPI: unabled to initialise PciConfig handler: %s\n",
290 		    AcpiFormatException(rv));
291 		return 0;
292 	}
293 #endif
294 
295 	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
296 	if (ACPI_FAILURE(rv)) {
297 		printf("ACPI: unable to enable: %s\n", AcpiFormatException(rv));
298 		return 0;
299 	}
300 
301 	/*
302 	 * Looks like we have ACPI!
303 	 */
304 
305 	return 1;
306 }
307 
308 static int
309 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
310 {
311 	struct cfattach *ca;
312 
313 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
314 	return (ca == &acpi_ca);
315 }
316 
317 int
318 acpi_check(device_t parent, const char *ifattr)
319 {
320 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
321 }
322 
323 ACPI_PHYSICAL_ADDRESS
324 acpi_OsGetRootPointer(void)
325 {
326 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
327 
328 	/*
329 	 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
330 	 *
331 	 * IA-64: Use the EFI.
332 	 *
333 	 * We let MD code handle this since there are multiple
334 	 * ways to do it.
335 	 */
336 
337 	PhysicalAddress = acpi_md_OsGetRootPointer();
338 
339 	if (acpi_root_pointer == 0)
340 		acpi_root_pointer = PhysicalAddress;
341 
342 	return PhysicalAddress;
343 }
344 
345 /*
346  * acpi_match:
347  *
348  *	Autoconfiguration `match' routine.
349  */
350 static int
351 acpi_match(device_t parent, struct cfdata *match, void *aux)
352 {
353 	/*
354 	 * XXX Check other locators?  Hard to know -- machine
355 	 * dependent code has already checked for the presence
356 	 * of ACPI by calling acpi_probe(), so I suppose we
357 	 * don't really have to do anything else.
358 	 */
359 	return 1;
360 }
361 
362 /* Remove references to child devices.
363  *
364  * XXX Need to reclaim any resources?
365  */
366 static void
367 acpi_childdet(device_t self, device_t child)
368 {
369 	struct acpi_softc *sc = device_private(self);
370 	struct acpi_scope *as;
371 	struct acpi_devnode *ad;
372 
373 	TAILQ_FOREACH(as, &sc->sc_scopes, as_list) {
374 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
375 			if (ad->ad_device == child)
376 				ad->ad_device = NULL;
377 		}
378 	}
379 }
380 
381 /*
382  * acpi_attach:
383  *
384  *	Autoconfiguration `attach' routine.  Finish initializing
385  *	ACPICA (some initialization was done in acpi_probe(),
386  *	which was required to check for the presence of ACPI),
387  *	and enable the ACPI subsystem.
388  */
389 static void
390 acpi_attach(device_t parent, device_t self, void *aux)
391 {
392 	struct acpi_softc *sc = device_private(self);
393 	struct acpibus_attach_args *aa = aux;
394 	ACPI_STATUS rv;
395 	ACPI_TABLE_HEADER *rsdt;
396 
397 	aprint_naive(": Advanced Configuration and Power Interface\n");
398 	aprint_normal(": Advanced Configuration and Power Interface\n");
399 
400 	if (acpi_softc != NULL)
401 		panic("acpi_attach: ACPI has already been attached");
402 
403 	sysmon_power_settype("acpi");
404 
405 	aprint_verbose_dev(self,
406 	    "using Intel ACPI CA subsystem version %08x\n", ACPI_CA_VERSION);
407 
408 	rsdt = acpi_map_rsdt();
409 	if (rsdt) {
410 		aprint_verbose_dev(
411 		    self,
412 		    "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
413 		    rsdt->OemId, rsdt->OemTableId,
414 		    rsdt->OemRevision,
415 		    rsdt->AslCompilerId, rsdt->AslCompilerRevision);
416 	} else
417 		aprint_error_dev(self, "X/RSDT: Not found\n");
418 	acpi_unmap_rsdt(rsdt);
419 
420 	sc->sc_dev = self;
421 	sc->sc_quirks = acpi_find_quirks();
422 
423 	sc->sc_iot = aa->aa_iot;
424 	sc->sc_memt = aa->aa_memt;
425 	sc->sc_pc = aa->aa_pc;
426 	sc->sc_pciflags = aa->aa_pciflags;
427 	sc->sc_ic = aa->aa_ic;
428 
429 	acpi_softc = sc;
430 
431 	/*
432 	 * Register null power management handler
433 	 */
434 	if (!pmf_device_register(self, NULL, NULL))
435 		aprint_error_dev(self, "couldn't establish power handler\n");
436 
437 	/*
438 	 * Bring ACPI on-line.
439 	 */
440 #ifdef ACPI_DEBUGGER
441 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
442 		acpi_osd_debugger();
443 #endif
444 
445 #define ACPI_ENABLE_PHASE1 \
446     (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
447 #define ACPI_ENABLE_PHASE2 \
448     (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
449      ACPI_NO_ADDRESS_SPACE_INIT)
450 
451 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
452 	if (ACPI_FAILURE(rv)) {
453 		aprint_error_dev(self, "unable to enable ACPI: %s\n",
454 		    AcpiFormatException(rv));
455 		return;
456 	}
457 
458 	acpi_md_callback();
459 
460 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
461 	if (ACPI_FAILURE(rv)) {
462 		aprint_error_dev(self, "unable to enable ACPI: %s\n",
463 		    AcpiFormatException(rv));
464 		return;
465 	}
466 
467 	/* early EC handler initialization if ECDT table is available */
468 	config_found_ia(self, "acpiecdtbus", NULL, NULL);
469 
470 	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
471 	if (ACPI_FAILURE(rv)) {
472 		aprint_error_dev(self,
473 		    "unable to initialize ACPI objects: %s\n",
474 		    AcpiFormatException(rv));
475 		return;
476 	}
477 	acpi_active = 1;
478 
479 	/* Our current state is "awake". */
480 	sc->sc_sleepstate = ACPI_STATE_S0;
481 
482 	/* Show SCI interrupt. */
483 	aprint_verbose_dev(self, "SCI interrupting at int %d\n",
484 	    AcpiGbl_FADT.SciInterrupt);
485 
486 	/*
487 	 * Check for fixed-hardware features.
488 	 */
489 	acpi_enable_fixed_events(sc);
490 	acpitimer_init();
491 
492 	/*
493 	 * Scan the namespace and build our device tree.
494 	 */
495 #ifdef ACPI_DEBUGGER
496 	if (acpi_dbgr & ACPI_DBGR_PROBE)
497 		acpi_osd_debugger();
498 #endif
499 	acpi_build_tree(sc);
500 
501 	sprintf(acpi_supported_states, "%s%s%s%s%s%s",
502 	    is_available_state(sc, ACPI_STATE_S0) ? "S0 " : "",
503 	    is_available_state(sc, ACPI_STATE_S1) ? "S1 " : "",
504 	    is_available_state(sc, ACPI_STATE_S2) ? "S2 " : "",
505 	    is_available_state(sc, ACPI_STATE_S3) ? "S3 " : "",
506 	    is_available_state(sc, ACPI_STATE_S4) ? "S4 " : "",
507 	    is_available_state(sc, ACPI_STATE_S5) ? "S5 " : "");
508 
509 #ifdef ACPI_DEBUGGER
510 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
511 		acpi_osd_debugger();
512 #endif
513 }
514 
515 #if 0
516 /*
517  * acpi_disable:
518  *
519  *	Disable ACPI.
520  */
521 static ACPI_STATUS
522 acpi_disable(struct acpi_softc *sc)
523 {
524 	ACPI_STATUS rv = AE_OK;
525 
526 	if (acpi_active) {
527 		rv = AcpiDisable();
528 		if (ACPI_SUCCESS(rv))
529 			acpi_active = 0;
530 	}
531 	return rv;
532 }
533 #endif
534 
535 struct acpi_make_devnode_state {
536 	struct acpi_softc *softc;
537 	struct acpi_scope *scope;
538 };
539 
540 /*
541  * acpi_build_tree:
542  *
543  *	Scan relevant portions of the ACPI namespace and attach
544  *	child devices.
545  */
546 static void
547 acpi_build_tree(struct acpi_softc *sc)
548 {
549 	static const char *scopes[] = {
550 		"\\_PR_",	/* ACPI 1.0 processor namespace */
551 		"\\_SB_",	/* system bus namespace */
552 		"\\_SI_",	/* system indicator namespace */
553 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
554 		NULL,
555 	};
556 	struct acpi_attach_args aa;
557 	struct acpi_make_devnode_state state;
558 	struct acpi_scope *as;
559 	struct acpi_devnode *ad;
560 	ACPI_HANDLE parent;
561 	ACPI_STATUS rv;
562 	int i;
563 
564 	TAILQ_INIT(&sc->sc_scopes);
565 
566 	state.softc = sc;
567 
568 	/*
569 	 * Scan the namespace and build our tree.
570 	 */
571 	for (i = 0; scopes[i] != NULL; i++) {
572 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
573 		as->as_name = scopes[i];
574 		TAILQ_INIT(&as->as_devnodes);
575 
576 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
577 
578 		state.scope = as;
579 
580 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
581 		    &parent);
582 		if (ACPI_SUCCESS(rv)) {
583 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
584 			    acpi_make_devnode, &state, NULL);
585 		}
586 
587 		/* Now, for this namespace, try and attach the devices. */
588 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
589 			aa.aa_node = ad;
590 			aa.aa_iot = sc->sc_iot;
591 			aa.aa_memt = sc->sc_memt;
592 			aa.aa_pc = sc->sc_pc;
593 			aa.aa_pciflags = sc->sc_pciflags;
594 			aa.aa_ic = sc->sc_ic;
595 
596 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
597 				/*
598 				 * XXX We only attach devices which are:
599 				 *
600 				 *	- present
601 				 *	- enabled
602 				 *	- functioning properly
603 				 *
604 				 * However, if enabled, it's decoding resources,
605 				 * so we should claim them, if possible.
606 				 * Requires changes to bus_space(9).
607 				 */
608 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
609 				    ACPI_VALID_STA &&
610 				    (ad->ad_devinfo->CurrentStatus &
611 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
612 				      ACPI_STA_DEV_OK)) !=
613 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
614 				     ACPI_STA_DEV_OK))
615 					continue;
616 			}
617 
618 			/*
619 			 * XXX Same problem as above...
620 			 *
621 			 * Do this check only for devices, as e.g.
622 			 * a Thermal Zone doesn't have a HID.
623 			 */
624 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE &&
625 			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
626 				continue;
627 
628 			ad->ad_device = config_found_ia(sc->sc_dev,
629 			    "acpinodebus", &aa, acpi_print);
630 		}
631 	}
632 	config_found_ia(sc->sc_dev, "acpiapmbus", NULL, NULL);
633 }
634 
635 #ifdef ACPI_ACTIVATE_DEV
636 static void
637 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
638 {
639 	ACPI_STATUS rv;
640 	ACPI_BUFFER buf;
641 
642 	buf.Pointer = NULL;
643 	buf.Length = ACPI_ALLOCATE_BUFFER;
644 
645 #ifdef ACPI_DEBUG
646 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
647 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
648 #endif
649 
650 	rv = acpi_allocate_resources(handle);
651 	if (ACPI_FAILURE(rv)) {
652 		aprint_error("acpi: activate failed for %s\n",
653 		       (*di)->HardwareId.Value);
654 	} else {
655 		aprint_verbose("acpi: activated %s\n",
656 		    (*di)->HardwareId.Value);
657 	}
658 
659 	(void)AcpiGetObjectInfo(handle, &buf);
660 	AcpiOsFree(*di);
661 	*di = buf.Pointer;
662 
663 #ifdef ACPI_DEBUG
664 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
665 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
666 #endif
667 }
668 #endif /* ACPI_ACTIVATE_DEV */
669 
670 /*
671  * acpi_make_devnode:
672  *
673  *	Make an ACPI devnode.
674  */
675 static ACPI_STATUS
676 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
677     void **status)
678 {
679 	struct acpi_make_devnode_state *state = context;
680 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
681 	struct acpi_softc *sc = state->softc;
682 #endif
683 	struct acpi_scope *as = state->scope;
684 	struct acpi_devnode *ad;
685 	ACPI_OBJECT_TYPE type;
686 	ACPI_BUFFER buf;
687 	ACPI_DEVICE_INFO *devinfo;
688 	ACPI_STATUS rv;
689 	ACPI_NAME_UNION *anu;
690 	int i, clear = 0;
691 
692 	rv = AcpiGetType(handle, &type);
693 	if (ACPI_SUCCESS(rv)) {
694 		buf.Pointer = NULL;
695 		buf.Length = ACPI_ALLOCATE_BUFFER;
696 		rv = AcpiGetObjectInfo(handle, &buf);
697 		if (ACPI_FAILURE(rv)) {
698 #ifdef ACPI_DEBUG
699 			aprint_normal_dev(sc->sc_dev,
700 			    "AcpiGetObjectInfo failed: %s\n",
701 			    AcpiFormatException(rv));
702 #endif
703 			goto out; /* XXX why return OK */
704 		}
705 
706 		devinfo = buf.Pointer;
707 
708 		switch (type) {
709 		case ACPI_TYPE_DEVICE:
710 #ifdef ACPI_ACTIVATE_DEV
711 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
712 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
713 			    (devinfo->CurrentStatus &
714 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
715 			    ACPI_STA_DEV_PRESENT)
716 				acpi_activate_device(handle, &devinfo);
717 
718 			/* FALLTHROUGH */
719 #endif
720 
721 		case ACPI_TYPE_PROCESSOR:
722 		case ACPI_TYPE_THERMAL:
723 		case ACPI_TYPE_POWER:
724 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
725 			if (ad == NULL)
726 				return AE_NO_MEMORY;
727 
728 			ad->ad_devinfo = devinfo;
729 			ad->ad_handle = handle;
730 			ad->ad_level = level;
731 			ad->ad_scope = as;
732 			ad->ad_type = type;
733 
734 			anu = (ACPI_NAME_UNION *)&devinfo->Name;
735 			ad->ad_name[4] = '\0';
736 			for (i = 3, clear = 0; i >= 0; i--) {
737 				if (!clear && anu->Ascii[i] == '_')
738 					ad->ad_name[i] = '\0';
739 				else {
740 					ad->ad_name[i] = anu->Ascii[i];
741 					clear = 1;
742 				}
743 			}
744 			if (ad->ad_name[0] == '\0')
745 				ad->ad_name[0] = '_';
746 
747 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
748 
749 			if (type == ACPI_TYPE_DEVICE &&
750 			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
751 				goto out;
752 
753 #ifdef ACPI_EXTRA_DEBUG
754 			aprint_normal_dev(sc->sc_dev,
755 			    "HID %s found in scope %s level %d\n",
756 			    ad->ad_devinfo->HardwareId.Value,
757 			    as->as_name, ad->ad_level);
758 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
759 				aprint_normal("       UID %s\n",
760 				    ad->ad_devinfo->UniqueId.Value);
761 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
762 				aprint_normal("       ADR 0x%016qx\n",
763 				    ad->ad_devinfo->Address);
764 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
765 				aprint_normal("       STA 0x%08x\n",
766 				    ad->ad_devinfo->CurrentStatus);
767 #endif
768 		}
769 	}
770  out:
771 	return AE_OK;
772 }
773 
774 /*
775  * acpi_print:
776  *
777  *	Autoconfiguration print routine for ACPI node bus.
778  */
779 static int
780 acpi_print(void *aux, const char *pnp)
781 {
782 	struct acpi_attach_args *aa = aux;
783 	ACPI_STATUS rv;
784 
785 	if (pnp) {
786 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
787 			char *pnpstr =
788 			    aa->aa_node->ad_devinfo->HardwareId.Value;
789 			char *str;
790 
791 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
792 			    pnpstr);
793 			rv = acpi_eval_string(aa->aa_node->ad_handle,
794 			    "_STR", &str);
795 			if (ACPI_SUCCESS(rv)) {
796 				aprint_normal("[%s] ", str);
797 				AcpiOsFree(str);
798 			}
799 #ifdef ACPIVERBOSE
800 			else {
801 				int i;
802 
803 				for (i = 0; i < sizeof(acpi_knowndevs) /
804 				    sizeof(acpi_knowndevs[0]); i++) {
805 					if (strcmp(acpi_knowndevs[i].pnp,
806 					    pnpstr) == 0) {
807 						aprint_normal("[%s] ",
808 						    acpi_knowndevs[i].str);
809 					}
810 				}
811 			}
812 
813 #endif
814 			aprint_normal("at %s", pnp);
815 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
816 			aprint_normal("%s (ACPI Object Type '%s' "
817 			    "[0x%02x]) ", aa->aa_node->ad_name,
818 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
819 			     aa->aa_node->ad_devinfo->Type);
820 			aprint_normal("at %s", pnp);
821 		} else
822 			return 0;
823 	} else {
824 		aprint_normal(" (%s", aa->aa_node->ad_name);
825 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
826 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
827 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
828 				const char *uid;
829 
830 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
831 				if (uid[0] == '\0')
832 					uid = "<null>";
833 				aprint_normal("-%s", uid);
834 			}
835 		}
836 		aprint_normal(")");
837 	}
838 
839 	return UNCONF;
840 }
841 
842 /*****************************************************************************
843  * ACPI fixed-hardware feature handlers
844  *****************************************************************************/
845 
846 static UINT32	acpi_fixed_button_handler(void *);
847 static void	acpi_fixed_button_pressed(void *);
848 
849 /*
850  * acpi_enable_fixed_events:
851  *
852  *	Enable any fixed-hardware feature handlers.
853  */
854 static void
855 acpi_enable_fixed_events(struct acpi_softc *sc)
856 {
857 	static int beenhere;
858 	ACPI_STATUS rv;
859 
860 	KASSERT(beenhere == 0);
861 	beenhere = 1;
862 
863 	/*
864 	 * Check for fixed-hardware buttons.
865 	 */
866 
867 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
868 		aprint_verbose_dev(sc->sc_dev,
869 		    "fixed-feature power button present\n");
870 		sc->sc_smpsw_power.smpsw_name = device_xname(sc->sc_dev);
871 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
872 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
873 			aprint_error_dev(sc->sc_dev,
874 			    "unable to register fixed power "
875 			    "button with sysmon\n");
876 		} else {
877 			rv = AcpiInstallFixedEventHandler(
878 			    ACPI_EVENT_POWER_BUTTON,
879 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
880 			if (ACPI_FAILURE(rv)) {
881 				aprint_error_dev(sc->sc_dev,
882 				    "unable to install handler "
883 				    "for fixed power button: %s\n",
884 				    AcpiFormatException(rv));
885 			}
886 		}
887 	}
888 
889 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
890 		aprint_verbose_dev(sc->sc_dev,
891 		    "fixed-feature sleep button present\n");
892 		sc->sc_smpsw_sleep.smpsw_name = device_xname(sc->sc_dev);
893 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
894 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
895 			aprint_error_dev(sc->sc_dev,
896 			    "unable to register fixed sleep "
897 			    "button with sysmon\n");
898 		} else {
899 			rv = AcpiInstallFixedEventHandler(
900 			    ACPI_EVENT_SLEEP_BUTTON,
901 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
902 			if (ACPI_FAILURE(rv)) {
903 				aprint_error_dev(sc->sc_dev,
904 				    "unable to install handler "
905 				    "for fixed sleep button: %s\n",
906 				    AcpiFormatException(rv));
907 			}
908 		}
909 	}
910 }
911 
912 /*
913  * acpi_fixed_button_handler:
914  *
915  *	Event handler for the fixed buttons.
916  */
917 static UINT32
918 acpi_fixed_button_handler(void *context)
919 {
920 	struct sysmon_pswitch *smpsw = context;
921 	int rv;
922 
923 #ifdef ACPI_BUT_DEBUG
924 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
925 #endif
926 
927 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
928 	    acpi_fixed_button_pressed, smpsw);
929 	if (ACPI_FAILURE(rv))
930 		printf("%s: WARNING: unable to queue fixed button pressed "
931 		    "callback: %s\n", smpsw->smpsw_name,
932 		    AcpiFormatException(rv));
933 
934 	return ACPI_INTERRUPT_HANDLED;
935 }
936 
937 /*
938  * acpi_fixed_button_pressed:
939  *
940  *	Deal with a fixed button being pressed.
941  */
942 static void
943 acpi_fixed_button_pressed(void *context)
944 {
945 	struct sysmon_pswitch *smpsw = context;
946 
947 #ifdef ACPI_BUT_DEBUG
948 	printf("%s: fixed button pressed, calling sysmon\n",
949 	    smpsw->smpsw_name);
950 #endif
951 
952 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
953 }
954 
955 /*****************************************************************************
956  * ACPI utility routines.
957  *****************************************************************************/
958 
959 /*
960  * acpi_eval_integer:
961  *
962  *	Evaluate an integer object.
963  */
964 ACPI_STATUS
965 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
966 {
967 	ACPI_STATUS rv;
968 	ACPI_BUFFER buf;
969 	ACPI_OBJECT param;
970 
971 	if (handle == NULL)
972 		handle = ACPI_ROOT_OBJECT;
973 
974 	buf.Pointer = &param;
975 	buf.Length = sizeof(param);
976 
977 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
978 	if (ACPI_SUCCESS(rv))
979 		*valp = param.Integer.Value;
980 
981 	return rv;
982 }
983 
984 /*
985  * acpi_eval_string:
986  *
987  *	Evaluate a (Unicode) string object.
988  */
989 ACPI_STATUS
990 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
991 {
992 	ACPI_STATUS rv;
993 	ACPI_BUFFER buf;
994 
995 	if (handle == NULL)
996 		handle = ACPI_ROOT_OBJECT;
997 
998 	buf.Pointer = NULL;
999 	buf.Length = ACPI_ALLOCATE_BUFFER;
1000 
1001 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
1002 	if (ACPI_SUCCESS(rv)) {
1003 		ACPI_OBJECT *param = buf.Pointer;
1004 		const char *ptr = param->String.Pointer;
1005 		size_t len = param->String.Length;
1006 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
1007 			rv = AE_NO_MEMORY;
1008 		else
1009 			(void)memcpy(*stringp, ptr, len);
1010 		AcpiOsFree(param);
1011 	}
1012 
1013 	return rv;
1014 }
1015 
1016 
1017 /*
1018  * acpi_eval_struct:
1019  *
1020  *	Evaluate a more complex structure.
1021  *	Caller must free buf.Pointer by AcpiOsFree().
1022  */
1023 ACPI_STATUS
1024 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
1025 {
1026 	ACPI_STATUS rv;
1027 
1028 	if (handle == NULL)
1029 		handle = ACPI_ROOT_OBJECT;
1030 
1031 	bufp->Pointer = NULL;
1032 	bufp->Length = ACPI_ALLOCATE_BUFFER;
1033 
1034 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
1035 
1036 	return rv;
1037 }
1038 
1039 /*
1040  * acpi_foreach_package_object:
1041  *
1042  *	Iterate over all objects in a in a packages and pass then all
1043  *	to a function. If the called function returns non AE_OK, the
1044  *	iteration is stopped and that value is returned.
1045  */
1046 
1047 ACPI_STATUS
1048 acpi_foreach_package_object(ACPI_OBJECT *pkg,
1049     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
1050     void *arg)
1051 {
1052 	ACPI_STATUS rv = AE_OK;
1053 	int i;
1054 
1055 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1056 		return AE_BAD_PARAMETER;
1057 
1058 	for (i = 0; i < pkg->Package.Count; i++) {
1059 		rv = (*func)(&pkg->Package.Elements[i], arg);
1060 		if (ACPI_FAILURE(rv))
1061 			break;
1062 	}
1063 
1064 	return rv;
1065 }
1066 
1067 const char *
1068 acpi_name(ACPI_HANDLE handle)
1069 {
1070 	static char buffer[80];
1071 	ACPI_BUFFER buf;
1072 	ACPI_STATUS rv;
1073 
1074 	buf.Length = sizeof(buffer);
1075 	buf.Pointer = buffer;
1076 
1077 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
1078 	if (ACPI_FAILURE(rv))
1079 		return "(unknown acpi path)";
1080 	return buffer;
1081 }
1082 
1083 /*
1084  * acpi_get:
1085  *
1086  *	Fetch data info the specified (empty) ACPI buffer.
1087  *	Caller must free buf.Pointer by AcpiOsFree().
1088  */
1089 ACPI_STATUS
1090 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
1091     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
1092 {
1093 	buf->Pointer = NULL;
1094 	buf->Length = ACPI_ALLOCATE_BUFFER;
1095 
1096 	return (*getit)(handle, buf);
1097 }
1098 
1099 
1100 /*
1101  * acpi_match_hid
1102  *
1103  *	Match given ids against _HID and _CIDs
1104  */
1105 int
1106 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
1107 {
1108 	int i;
1109 
1110 	while (*ids) {
1111 		if (ad->Valid & ACPI_VALID_HID) {
1112 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
1113 				return 1;
1114 		}
1115 
1116 		if (ad->Valid & ACPI_VALID_CID) {
1117 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
1118 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
1119 					return 1;
1120 			}
1121 		}
1122 		ids++;
1123 	}
1124 
1125 	return 0;
1126 }
1127 
1128 /*
1129  * acpi_set_wake_gpe
1130  *
1131  *	Set GPE as both Runtime and Wake
1132  */
1133 void
1134 acpi_set_wake_gpe(ACPI_HANDLE handle)
1135 {
1136 	ACPI_BUFFER buf;
1137 	ACPI_STATUS rv;
1138 	ACPI_OBJECT *p, *elt;
1139 
1140 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
1141 	if (ACPI_FAILURE(rv))
1142 		return;			/* just ignore */
1143 
1144 	p = buf.Pointer;
1145 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
1146 		goto out;		/* just ignore */
1147 
1148 	elt = p->Package.Elements;
1149 
1150 	/* TBD: package support */
1151 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
1152 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
1153 
1154  out:
1155 	AcpiOsFree(buf.Pointer);
1156 }
1157 
1158 
1159 /*****************************************************************************
1160  * ACPI sleep support.
1161  *****************************************************************************/
1162 
1163 static int
1164 is_available_state(struct acpi_softc *sc, int state)
1165 {
1166 	UINT8 type_a, type_b;
1167 
1168 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
1169 				&type_a, &type_b));
1170 }
1171 
1172 /*
1173  * acpi_enter_sleep_state:
1174  *
1175  *	enter to the specified sleep state.
1176  */
1177 
1178 ACPI_STATUS
1179 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1180 {
1181 	int err;
1182 	ACPI_STATUS ret = AE_OK;
1183 
1184 	if (state == acpi_sleepstate)
1185 		return AE_OK;
1186 
1187 	aprint_normal_dev(sc->sc_dev, "entering state %d\n", state);
1188 
1189 	switch (state) {
1190 	case ACPI_STATE_S0:
1191 		break;
1192 	case ACPI_STATE_S1:
1193 	case ACPI_STATE_S2:
1194 	case ACPI_STATE_S3:
1195 	case ACPI_STATE_S4:
1196 		if (!is_available_state(sc, state)) {
1197 			aprint_error_dev(sc->sc_dev,
1198 			    "ACPI S%d not available on this platform\n", state);
1199 			break;
1200 		}
1201 
1202 		if (state != ACPI_STATE_S1 && !pmf_system_suspend(PMF_F_NONE)) {
1203 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1204 			break;
1205 		}
1206 
1207 		ret = AcpiEnterSleepStatePrep(state);
1208 		if (ACPI_FAILURE(ret)) {
1209 			aprint_error_dev(sc->sc_dev,
1210 			    "failed preparing to sleep (%s)\n",
1211 			    AcpiFormatException(ret));
1212 			break;
1213 		}
1214 
1215 		acpi_sleepstate = state;
1216 		if (state == ACPI_STATE_S1) {
1217 			/* just enter the state */
1218 			acpi_md_OsDisableInterrupt();
1219 			ret = AcpiEnterSleepState((UINT8)state);
1220 			if (ACPI_FAILURE(ret))
1221 				aprint_error_dev(sc->sc_dev,
1222 				    "failed to enter sleep state S1: %s\n",
1223 				    AcpiFormatException(ret));
1224 			AcpiLeaveSleepState((UINT8)state);
1225 		} else {
1226 			err = acpi_md_sleep(state);
1227 			if (state == ACPI_STATE_S4)
1228 				AcpiEnable();
1229 			pmf_system_bus_resume(PMF_F_NONE);
1230 			AcpiLeaveSleepState((UINT8)state);
1231 			pmf_system_resume(PMF_F_NONE);
1232 		}
1233 
1234 		break;
1235 	case ACPI_STATE_S5:
1236 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1237 		if (ACPI_FAILURE(ret)) {
1238 			aprint_error_dev(sc->sc_dev,
1239 			    "failed preparing to sleep (%s)\n",
1240 			    AcpiFormatException(ret));
1241 			break;
1242 		}
1243 		DELAY(1000000);
1244 		acpi_sleepstate = state;
1245 		acpi_md_OsDisableInterrupt();
1246 		AcpiEnterSleepState(ACPI_STATE_S5);
1247 		aprint_error_dev(sc->sc_dev, "WARNING powerdown failed!\n");
1248 		break;
1249 	}
1250 
1251 	acpi_sleepstate = ACPI_STATE_S0;
1252 	return ret;
1253 }
1254 
1255 #if defined(ACPI_ACTIVATE_DEV)
1256 /* XXX This very incomplete */
1257 ACPI_STATUS
1258 acpi_allocate_resources(ACPI_HANDLE handle)
1259 {
1260 	ACPI_BUFFER bufp, bufc, bufn;
1261 	ACPI_RESOURCE *resp, *resc, *resn;
1262 	ACPI_RESOURCE_IRQ *irq;
1263 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1264 	ACPI_STATUS rv;
1265 	uint delta;
1266 
1267 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1268 	if (ACPI_FAILURE(rv))
1269 		goto out;
1270 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1271 	if (ACPI_FAILURE(rv)) {
1272 		goto out1;
1273 	}
1274 
1275 	bufn.Length = 1000;
1276 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1277 	resp = bufp.Pointer;
1278 	resc = bufc.Pointer;
1279 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
1280 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1281 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
1282 			resp = ACPI_NEXT_RESOURCE(resp);
1283 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
1284 			break;
1285 		/* Found identical Id */
1286 		resn->Type = resc->Type;
1287 		switch (resc->Type) {
1288 		case ACPI_RESOURCE_TYPE_IRQ:
1289 			memcpy(&resn->Data, &resp->Data,
1290 			       sizeof(ACPI_RESOURCE_IRQ));
1291 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1292 			irq->Interrupts[0] =
1293 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1294 			        Interrupts[irq->InterruptCount-1];
1295 			irq->InterruptCount = 1;
1296 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
1297 			break;
1298 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1299 			memcpy(&resn->Data, &resp->Data,
1300 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
1301 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
1302 #if 0
1303 			/*
1304 			 * XXX not duplicating the interrupt logic above
1305 			 * because its not clear what it accomplishes.
1306 			 */
1307 			xirq->Interrupts[0] =
1308 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
1309 			    Interrupts[irq->NumberOfInterrupts-1];
1310 			xirq->NumberOfInterrupts = 1;
1311 #endif
1312 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
1313 			break;
1314 		case ACPI_RESOURCE_TYPE_IO:
1315 			memcpy(&resn->Data, &resp->Data,
1316 			       sizeof(ACPI_RESOURCE_IO));
1317 			resn->Length = resp->Length;
1318 			break;
1319 		default:
1320 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
1321 			rv = AE_BAD_DATA;
1322 			goto out2;
1323 		}
1324 		resc = ACPI_NEXT_RESOURCE(resc);
1325 		resn = ACPI_NEXT_RESOURCE(resn);
1326 		resp = ACPI_NEXT_RESOURCE(resp);
1327 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1328 		if (delta >=
1329 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
1330 			bufn.Length *= 2;
1331 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1332 					       M_ACPI, M_WAITOK);
1333 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1334 		}
1335 	}
1336 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1337 		printf("acpi_allocate_resources: resc not exhausted\n");
1338 		rv = AE_BAD_DATA;
1339 		goto out3;
1340 	}
1341 
1342 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
1343 	rv = AcpiSetCurrentResources(handle, &bufn);
1344 	if (ACPI_FAILURE(rv)) {
1345 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1346 		       AcpiFormatException(rv));
1347 	}
1348 
1349 out3:
1350 	free(bufn.Pointer, M_ACPI);
1351 out2:
1352 	AcpiOsFree(bufc.Pointer);
1353 out1:
1354 	AcpiOsFree(bufp.Pointer);
1355 out:
1356 	return rv;
1357 }
1358 #endif /* ACPI_ACTIVATE_DEV */
1359 
1360 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1361 {
1362 	const struct sysctlnode *node;
1363 	const struct sysctlnode *ssnode;
1364 
1365 	if (sysctl_createv(clog, 0, NULL, NULL,
1366 	    CTLFLAG_PERMANENT,
1367 	    CTLTYPE_NODE, "hw", NULL,
1368 	    NULL, 0, NULL, 0,
1369 	    CTL_HW, CTL_EOL) != 0)
1370 		return;
1371 
1372 	if (sysctl_createv(clog, 0, NULL, &node,
1373 	    CTLFLAG_PERMANENT,
1374 	    CTLTYPE_NODE, "acpi", NULL,
1375 	    NULL, 0, NULL, 0,
1376 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
1377 		return;
1378 
1379 	sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
1380 	    CTLTYPE_QUAD, "root",
1381 	    SYSCTL_DESCR("ACPI root pointer"),
1382 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1383 	    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1384 	sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
1385 	    CTLTYPE_STRING, "supported_states",
1386 	    SYSCTL_DESCR("Supported ACPI system states"),
1387 	    NULL, 0, acpi_supported_states, 0,
1388 	    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1389 
1390 	/* ACPI sleepstate sysctl */
1391 	if (sysctl_createv(NULL, 0, NULL, &node,
1392 	    CTLFLAG_PERMANENT,
1393 	    CTLTYPE_NODE, "machdep", NULL,
1394 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
1395 		return;
1396 	if (sysctl_createv(NULL, 0, &node, &ssnode,
1397 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
1398 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
1399 	    CTL_EOL) != 0)
1400 		return;
1401 }
1402 
1403 static int
1404 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1405 {
1406 	int error, t;
1407 	struct sysctlnode node;
1408 
1409 	node = *rnode;
1410 	t = acpi_sleepstate;
1411 	node.sysctl_data = &t;
1412 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1413 	if (error || newp == NULL)
1414 		return error;
1415 
1416 	if (acpi_softc == NULL)
1417 		return ENOSYS;
1418 
1419 	acpi_enter_sleep_state(acpi_softc, t);
1420 
1421 	return 0;
1422 }
1423 
1424 static ACPI_TABLE_HEADER *
1425 acpi_map_rsdt(void)
1426 {
1427 	ACPI_PHYSICAL_ADDRESS paddr;
1428 	ACPI_TABLE_RSDP *rsdp;
1429 
1430 	paddr = AcpiOsGetRootPointer();
1431 	if (paddr == 0) {
1432 		printf("ACPI: couldn't get root pointer\n");
1433 		return NULL;
1434 	}
1435 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1436 	if (rsdp == NULL) {
1437 		printf("ACPI: couldn't map RSDP\n");
1438 		return NULL;
1439 	}
1440 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1441 		paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
1442 	else
1443 		paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
1444 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1445 
1446 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1447 }
1448 
1449 static void
1450 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1451 {
1452 	if (rsdt == NULL)
1453 		return;
1454 
1455 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1456 }
1457