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