xref: /netbsd-src/sys/dev/acpi/acpi.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: acpi.c,v 1.292 2021/05/12 23:22:33 thorpej 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 (c) 2003 Wasabi Systems, Inc.
34  * All rights reserved.
35  *
36  * Written by Frank van der Linden 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  * Copyright 2001, 2003 Wasabi Systems, Inc.
69  * All rights reserved.
70  *
71  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
72  *
73  * Redistribution and use in source and binary forms, with or without
74  * modification, are permitted provided that the following conditions
75  * are met:
76  * 1. Redistributions of source code must retain the above copyright
77  *    notice, this list of conditions and the following disclaimer.
78  * 2. Redistributions in binary form must reproduce the above copyright
79  *    notice, this list of conditions and the following disclaimer in the
80  *    documentation and/or other materials provided with the distribution.
81  * 3. All advertising materials mentioning features or use of this software
82  *    must display the following acknowledgement:
83  *	This product includes software developed for the NetBSD Project by
84  *	Wasabi Systems, Inc.
85  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
86  *    or promote products derived from this software without specific prior
87  *    written permission.
88  *
89  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
91  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
92  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
93  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
94  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
95  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
96  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
97  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
98  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
99  * POSSIBILITY OF SUCH DAMAGE.
100  */
101 
102 #include <sys/cdefs.h>
103 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.292 2021/05/12 23:22:33 thorpej Exp $");
104 
105 #include "pci.h"
106 #include "opt_acpi.h"
107 #include "opt_pcifixup.h"
108 
109 #include <sys/param.h>
110 #include <sys/device.h>
111 #include <sys/kernel.h>
112 #include <sys/kmem.h>
113 #include <sys/malloc.h>
114 #include <sys/module.h>
115 #include <sys/mutex.h>
116 #include <sys/sysctl.h>
117 #include <sys/systm.h>
118 #include <sys/timetc.h>
119 
120 #include <dev/acpi/acpireg.h>
121 #include <dev/acpi/acpivar.h>
122 #include <dev/acpi/acpi_mcfg.h>
123 #include <dev/acpi/acpi_osd.h>
124 #include <dev/acpi/acpi_pci.h>
125 #include <dev/acpi/acpi_power.h>
126 #include <dev/acpi/acpi_timer.h>
127 #include <dev/acpi/acpi_wakedev.h>
128 
129 #include <machine/acpi_machdep.h>
130 
131 #include "ioconf.h"
132 
133 #define _COMPONENT	ACPI_BUS_COMPONENT
134 ACPI_MODULE_NAME	("acpi")
135 
136 /*
137  * The acpi_active variable is set when the ACPI subsystem is active.
138  * Machine-dependent code may wish to skip other steps (such as attaching
139  * subsystems that ACPI supercedes) when ACPI is active.
140  */
141 int		acpi_active = 0;
142 int		acpi_suspended = 0;
143 int		acpi_force_load = 0;
144 int		acpi_verbose_loaded = 0;
145 
146 struct acpi_softc	*acpi_softc = NULL;
147 static uint64_t		 acpi_root_pointer;
148 extern kmutex_t		 acpi_interrupt_list_mtx;
149 static ACPI_HANDLE	 acpi_scopes[4];
150 ACPI_TABLE_HEADER	*madt_header;
151 ACPI_TABLE_HEADER	*gtdt_header;
152 
153 /*
154  * This structure provides a context for the ACPI
155  * namespace walk performed in acpi_build_tree().
156  */
157 struct acpi_walkcontext {
158 	struct acpi_softc	*aw_sc;
159 	struct acpi_devnode	*aw_parent;
160 };
161 
162 /*
163  * Ignored HIDs.
164  */
165 static const char * const acpi_ignored_ids[] = {
166 #if defined(i386) || defined(x86_64)
167 	"ACPI0007",	/* ACPI CPUs do not attach to acpi(4) */
168 	"PNP0000",	/* AT interrupt controller is handled internally */
169 	"PNP0001",	/* EISA interrupt controller is handled internally */
170 	"PNP0200",	/* AT DMA controller is handled internally */
171 	"PNP0A??",	/* PCI Busses are handled internally */
172 	"PNP0B00",	/* AT RTC is handled internally */
173 	"PNP0C02",	/* PnP motherboard resources */
174 	"PNP0C0F",	/* ACPI PCI link devices are handled internally */
175 #endif
176 #if defined(x86_64)
177 	"PNP0C04",	/* FPU is handled internally */
178 #endif
179 #if defined(__aarch64__)
180 	"ACPI0004",	/* ACPI module devices are handled internally */
181 	"PNP0C0F",	/* ACPI PCI link devices are handled internally */
182 #endif
183 	NULL
184 };
185 
186 /*
187  * Devices that should be attached early.
188  */
189 static const char * const acpi_early_ids[] = {
190 	"PNP0C09",	/* acpiec(4) */
191 	NULL
192 };
193 
194 static int		acpi_match(device_t, cfdata_t, void *);
195 static int		acpi_submatch(device_t, cfdata_t, const int *, void *);
196 static void		acpi_attach(device_t, device_t, void *);
197 static int		acpi_detach(device_t, int);
198 static void		acpi_childdet(device_t, device_t);
199 static bool		acpi_suspend(device_t, const pmf_qual_t *);
200 static bool		acpi_resume(device_t, const pmf_qual_t *);
201 
202 static void		acpi_build_tree(struct acpi_softc *);
203 static void		acpi_config_tree(struct acpi_softc *);
204 static void		acpi_config_dma(struct acpi_softc *);
205 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, uint32_t,
206 					  void *, void **);
207 static ACPI_STATUS	acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
208 					       void *, void **);
209 static void		acpi_make_name(struct acpi_devnode *, uint32_t);
210 
211 static int		acpi_rescan(device_t, const char *, const int *);
212 static void		acpi_rescan_early(struct acpi_softc *);
213 static void		acpi_rescan_nodes(struct acpi_softc *);
214 static void		acpi_rescan_capabilities(device_t);
215 static int		acpi_print(void *aux, const char *);
216 
217 static void		acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
218 
219 static void		acpi_register_fixed_button(struct acpi_softc *, int);
220 static void		acpi_deregister_fixed_button(struct acpi_softc *, int);
221 static uint32_t		acpi_fixed_button_handler(void *);
222 static void		acpi_fixed_button_pressed(void *);
223 
224 static void		acpi_sleep_init(struct acpi_softc *);
225 
226 static int		sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO);
227 static int		sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO);
228 static int		sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO);
229 
230 static bool		  acpi_is_scope(struct acpi_devnode *);
231 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
232 static void		  acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
233 
234 void			acpi_print_verbose_stub(struct acpi_softc *);
235 void			acpi_print_dev_stub(const char *);
236 
237 static void		acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
238 ACPI_STATUS		acpi_allocate_resources(ACPI_HANDLE);
239 
240 void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub;
241 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
242 
243 bus_dma_tag_t		acpi_default_dma_tag(struct acpi_softc *, struct acpi_devnode *);
244 bus_dma_tag_t		acpi_default_dma64_tag(struct acpi_softc *, struct acpi_devnode *);
245 pci_chipset_tag_t	acpi_default_pci_chipset_tag(struct acpi_softc *, int, int);
246 
247 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
248     acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
249 
250 /*
251  * Probe for ACPI support.
252  *
253  * This is called by the machine-dependent ACPI front-end.
254  * Note: this is not an autoconfiguration interface function.
255  */
256 int
257 acpi_probe(void)
258 {
259 	ACPI_TABLE_HEADER *rsdt;
260 	ACPI_STATUS rv;
261 	int quirks;
262 
263 	if (acpi_softc != NULL)
264 		panic("%s: already probed", __func__);
265 
266 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
267 
268 	/*
269 	 * Start up ACPICA.
270 	 */
271 	AcpiGbl_EnableInterpreterSlack = true;
272 
273 	rv = AcpiInitializeSubsystem();
274 
275 	if (ACPI_FAILURE(rv)) {
276 		aprint_error("%s: failed to initialize subsystem\n", __func__);
277 		return 0;
278 	}
279 
280 	/*
281 	 * Allocate space for RSDT/XSDT and DSDT,
282 	 * but allow resizing if more tables exist.
283 	 */
284 	rv = AcpiInitializeTables(NULL, 2, true);
285 
286 	if (ACPI_FAILURE(rv)) {
287 		aprint_error("%s: failed to initialize tables\n", __func__);
288 		goto fail;
289 	}
290 
291 	rv = AcpiLoadTables();
292 
293 	if (ACPI_FAILURE(rv)) {
294 		aprint_error("%s: failed to load tables\n", __func__);
295 		goto fail;
296 	}
297 
298 	rsdt = acpi_map_rsdt();
299 
300 	if (rsdt == NULL) {
301 		aprint_error("%s: failed to map RSDT\n", __func__);
302 		goto fail;
303 	}
304 
305 	quirks = acpi_find_quirks();
306 
307 	if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_BROKEN) != 0) {
308 
309 		aprint_normal("ACPI: BIOS is listed as broken:\n");
310 		aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
311 		       "AslId <%4.4s,%08x>\n", rsdt->OemId, rsdt->OemTableId,
312 		        rsdt->OemRevision, rsdt->AslCompilerId,
313 		        rsdt->AslCompilerRevision);
314 		aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
315 
316 		acpi_unmap_rsdt(rsdt);
317 		goto fail;
318 	}
319 
320 	if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_OLDBIOS) != 0) {
321 
322 		aprint_normal("ACPI: BIOS is too old (%s). "
323 		    "Set acpi_force_load to use.\n",
324 		    pmf_get_platform("bios-date"));
325 
326 		acpi_unmap_rsdt(rsdt);
327 		goto fail;
328 	}
329 
330 	acpi_unmap_rsdt(rsdt);
331 
332 	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
333 
334 	if (ACPI_FAILURE(rv)) {
335 		aprint_error("%s: failed to enable subsystem\n", __func__);
336 		goto fail;
337 	}
338 
339 	return 1;
340 
341 fail:
342 	(void)AcpiTerminate();
343 
344 	return 0;
345 }
346 
347 void
348 acpi_disable(void)
349 {
350 
351 	if (acpi_softc == NULL)
352 		return;
353 
354 	KASSERT(acpi_active != 0);
355 
356 	if (AcpiGbl_FADT.SmiCommand != 0)
357 		AcpiDisable();
358 }
359 
360 int
361 acpi_check(device_t parent, const char *ifattr)
362 {
363 	return config_search(parent, NULL,
364 			     CFARG_SUBMATCH, acpi_submatch,
365 			     CFARG_IATTR, ifattr,
366 			     CFARG_EOL) != NULL;
367 }
368 
369 int
370 acpi_reset(void)
371 {
372 	struct acpi_softc *sc = acpi_softc;
373 	ACPI_GENERIC_ADDRESS *ResetReg;
374 	ACPI_PCI_ID PciId;
375 	ACPI_STATUS status;
376 
377 	if (sc == NULL)
378 		return ENXIO;
379 
380 	ResetReg = &AcpiGbl_FADT.ResetRegister;
381 
382 	/* Check if the reset register is supported */
383 	if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||
384 	    !ResetReg->Address) {
385 		return ENOENT;
386 	}
387 
388 	switch (ResetReg->SpaceId) {
389 	case ACPI_ADR_SPACE_PCI_CONFIG:
390 		PciId.Segment = PciId.Bus = 0;
391 		PciId.Device = ACPI_GAS_PCI_DEV(ResetReg->Address);
392 		PciId.Function = ACPI_GAS_PCI_FUNC(ResetReg->Address);
393 		status = AcpiOsWritePciConfiguration(&PciId,
394 		    ACPI_GAS_PCI_REGOFF(ResetReg->Address),
395 		    AcpiGbl_FADT.ResetValue, ResetReg->BitWidth);
396 		break;
397 	case ACPI_ADR_SPACE_SYSTEM_IO:
398 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
399 		status = AcpiReset();
400 		break;
401 	default:
402 		status = AE_TYPE;
403 		break;
404 	}
405 
406 	return ACPI_FAILURE(status) ? EIO : 0;
407 }
408 
409 /*
410  * Autoconfiguration.
411  */
412 static int
413 acpi_match(device_t parent, cfdata_t match, void *aux)
414 {
415 	/*
416 	 * XXX: Nada; MD code has called acpi_probe().
417 	 */
418 	return 1;
419 }
420 
421 static int
422 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
423 {
424 	struct cfattach *ca;
425 
426 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
427 
428 	return (ca == &acpi_ca);
429 }
430 
431 static void
432 acpi_attach(device_t parent, device_t self, void *aux)
433 {
434 	struct acpi_softc *sc = device_private(self);
435 	struct acpibus_attach_args *aa = aux;
436 	ACPI_TABLE_HEADER *rsdt, *hdr;
437 	ACPI_STATUS rv;
438 	int i;
439 
440 	aprint_naive("\n");
441 	aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
442 
443 	if (acpi_softc != NULL)
444 		panic("%s: already attached", __func__);
445 
446 	rsdt = acpi_map_rsdt();
447 
448 	if (rsdt == NULL)
449 		aprint_error_dev(self, "X/RSDT: Not found\n");
450 	else {
451 		aprint_verbose_dev(self,
452 		    "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
453 		    rsdt->OemId, rsdt->OemTableId,
454 		    rsdt->OemRevision,
455 		    rsdt->AslCompilerId, rsdt->AslCompilerRevision);
456 	}
457 
458 	acpi_unmap_rsdt(rsdt);
459 
460 	sc->sc_dev = self;
461 	sc->sc_root = NULL;
462 
463 	sc->sc_sleepstate = ACPI_STATE_S0;
464 	sc->sc_quirks = acpi_find_quirks();
465 
466 	sysmon_power_settype("acpi");
467 
468 	sc->sc_iot = aa->aa_iot;
469 	sc->sc_memt = aa->aa_memt;
470 	sc->sc_pciflags = aa->aa_pciflags;
471 	sc->sc_ic = aa->aa_ic;
472 	sc->sc_dmat = aa->aa_dmat;
473 	sc->sc_dmat64 = aa->aa_dmat64;
474 
475 	SIMPLEQ_INIT(&sc->ad_head);
476 
477 	acpi_softc = sc;
478 
479 	if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
480 		aprint_error_dev(self, "couldn't establish power handler\n");
481 
482 	/*
483 	 * Bring ACPICA on-line.
484 	 */
485 
486 	rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
487 
488 	if (ACPI_FAILURE(rv))
489 		goto fail;
490 
491 	/*
492 	 * Early initialization of acpiec(4) via ECDT.
493 	 */
494 	config_found(self, aa, NULL,
495 	    CFARG_IATTR, "acpiecdtbus",
496 	    CFARG_EOL);
497 
498 	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
499 
500 	if (ACPI_FAILURE(rv))
501 		goto fail;
502 
503 	/*
504 	 * Scan the namespace and build our device tree.
505 	 */
506 	acpi_build_tree(sc);
507 
508 #if NPCI > 0
509 	/*
510 	 * Probe MCFG table
511 	 */
512 	acpimcfg_probe(sc);
513 #endif
514 
515 	acpi_md_callback(sc);
516 
517 	/*
518 	 * Early initialization of the _PDC control method
519 	 * that may load additional SSDT tables dynamically.
520 	 */
521 	(void)acpi_md_pdc();
522 
523 	/*
524 	 * Install global notify handlers.
525 	 */
526 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
527 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
528 
529 	if (ACPI_FAILURE(rv))
530 		goto fail;
531 
532 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
533 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
534 
535 	if (ACPI_FAILURE(rv))
536 		goto fail;
537 
538 	acpi_active = 1;
539 
540 	if (!AcpiGbl_ReducedHardware) {
541 		/* Show SCI interrupt. */
542 		aprint_verbose_dev(self, "SCI interrupting at int %u\n",
543 		    AcpiGbl_FADT.SciInterrupt);
544 
545 		/*
546 		 * Install fixed-event handlers.
547 		 */
548 		acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
549 		acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
550 	}
551 
552 	/*
553 	 * Load drivers that operate on System Description Tables.
554 	 */
555 	for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; ++i) {
556 		rv = AcpiGetTableByIndex(i, &hdr);
557 		if (ACPI_FAILURE(rv)) {
558 			continue;
559 		}
560 		config_found(sc->sc_dev, hdr, NULL,
561 		    CFARG_IATTR, "acpisdtbus",
562 		    CFARG_EOL);
563 		AcpiPutTable(hdr);
564 	}
565 
566 	acpitimer_init(sc);
567 	acpi_config_tree(sc);
568 	acpi_sleep_init(sc);
569 
570 #ifdef ACPI_DEBUG
571 	acpi_debug_init();
572 #endif
573 
574 	/*
575 	 * Print debug information.
576 	 */
577 	acpi_print_verbose(sc);
578 
579 	return;
580 
581 fail:
582 	aprint_error("%s: failed to initialize ACPI: %s\n",
583 	    __func__, AcpiFormatException(rv));
584 }
585 
586 /*
587  * XXX: This is incomplete.
588  */
589 static int
590 acpi_detach(device_t self, int flags)
591 {
592 	struct acpi_softc *sc = device_private(self);
593 	ACPI_STATUS rv;
594 	int rc;
595 
596 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
597 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
598 
599 	if (ACPI_FAILURE(rv))
600 		return EBUSY;
601 
602 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
603 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler);
604 
605 	if (ACPI_FAILURE(rv))
606 		return EBUSY;
607 
608 	if ((rc = config_detach_children(self, flags)) != 0)
609 		return rc;
610 
611 	if ((rc = acpitimer_detach()) != 0)
612 		return rc;
613 
614 	if (!AcpiGbl_ReducedHardware) {
615 		acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
616 		acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
617 	}
618 
619 	pmf_device_deregister(self);
620 
621 	acpi_softc = NULL;
622 
623 	return 0;
624 }
625 
626 static void
627 acpi_childdet(device_t self, device_t child)
628 {
629 	struct acpi_softc *sc = device_private(self);
630 	struct acpi_devnode *ad;
631 
632 	if (sc->sc_apmbus == child)
633 		sc->sc_apmbus = NULL;
634 
635 	if (sc->sc_hpet == child)
636 		sc->sc_hpet = NULL;
637 
638 	if (sc->sc_wdrt == child)
639 		sc->sc_wdrt = NULL;
640 
641 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
642 
643 		if (ad->ad_device == child)
644 			ad->ad_device = NULL;
645 	}
646 }
647 
648 static bool
649 acpi_suspend(device_t dv, const pmf_qual_t *qual)
650 {
651 
652 	acpi_suspended = 1;
653 
654 	return true;
655 }
656 
657 static bool
658 acpi_resume(device_t dv, const pmf_qual_t *qual)
659 {
660 
661 	acpi_suspended = 0;
662 
663 	return true;
664 }
665 
666 /*
667  * Namespace scan.
668  */
669 static void
670 acpi_build_tree(struct acpi_softc *sc)
671 {
672 	struct acpi_walkcontext awc;
673 
674 	/*
675 	 * Get the root scope handles.
676 	 */
677 	KASSERT(__arraycount(acpi_scopes) == 4);
678 
679 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
680 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
681 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
682 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
683 
684 	/*
685 	 * Make the root node.
686 	 */
687 	awc.aw_sc = sc;
688 	awc.aw_parent = NULL;
689 
690 	(void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
691 
692 	KASSERT(sc->sc_root == NULL);
693 	KASSERT(awc.aw_parent != NULL);
694 
695 	sc->sc_root = awc.aw_parent;
696 
697 	/*
698 	 * Build the internal namespace.
699 	 */
700 	(void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
701 	    acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
702 
703 	/*
704 	 * Scan the internal namespace.
705 	 */
706 	(void)acpi_pcidev_scan(sc->sc_root);
707 }
708 
709 static void
710 acpi_config_tree(struct acpi_softc *sc)
711 {
712 	/*
713 	 * Assign bus_dma resources
714 	 */
715 	acpi_config_dma(sc);
716 
717 	/*
718 	 * Configure all everything found "at acpi?".
719 	 */
720 	(void)acpi_rescan(sc->sc_dev, NULL, NULL);
721 
722 	/*
723 	 * Update GPE information.
724 	 *
725 	 * Note that this must be called after
726 	 * all GPE handlers have been installed.
727 	 */
728 	(void)AcpiUpdateAllGpes();
729 
730 	/*
731 	 * Defer rest of the configuration.
732 	 */
733 	(void)config_defer(sc->sc_dev, acpi_rescan_capabilities);
734 }
735 
736 static void
737 acpi_config_dma(struct acpi_softc *sc)
738 {
739 	struct acpi_devnode *ad;
740 
741 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
742 
743 		if (ad->ad_device != NULL)
744 			continue;
745 
746 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
747 			continue;
748 
749 		ad->ad_dmat = acpi_get_dma_tag(sc, ad);
750 		ad->ad_dmat64 = acpi_get_dma64_tag(sc, ad);
751 	}
752 }
753 
754 static ACPI_STATUS
755 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
756     void *context, void **status)
757 {
758 	struct acpi_walkcontext *awc = context;
759 	struct acpi_softc *sc = awc->aw_sc;
760 	struct acpi_devnode *ad;
761 	ACPI_DEVICE_INFO *devinfo;
762 	ACPI_OBJECT_TYPE type;
763 	ACPI_STATUS rv;
764 
765 	rv = AcpiGetObjectInfo(handle, &devinfo);
766 
767 	if (ACPI_FAILURE(rv))
768 		return AE_OK;	/* Do not terminate the walk. */
769 
770 	type = devinfo->Type;
771 
772 	switch (type) {
773 
774 	case ACPI_TYPE_DEVICE:
775 		acpi_activate_device(handle, &devinfo);
776 		/* FALLTHROUGH */
777 
778 	case ACPI_TYPE_PROCESSOR:
779 	case ACPI_TYPE_THERMAL:
780 	case ACPI_TYPE_POWER:
781 
782 		ad = kmem_zalloc(sizeof(*ad), KM_SLEEP);
783 
784 		ad->ad_device = NULL;
785 		ad->ad_notify = NULL;
786 		ad->ad_pciinfo = NULL;
787 		ad->ad_wakedev = NULL;
788 
789 		ad->ad_type = type;
790 		ad->ad_handle = handle;
791 		ad->ad_devinfo = devinfo;
792 
793 		ad->ad_root = sc->sc_dev;
794 		ad->ad_parent = awc->aw_parent;
795 
796 		acpi_match_node_init(ad);
797 		acpi_make_name(ad, devinfo->Name);
798 
799 		/*
800 		 * Identify wake GPEs from the _PRW. Note that
801 		 * AcpiUpdateAllGpes() must be called afterwards.
802 		 */
803 		if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE)
804 			acpi_wakedev_init(ad);
805 
806 		SIMPLEQ_INIT(&ad->ad_child_head);
807 		SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
808 
809 		if (ad->ad_parent != NULL) {
810 
811 			SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
812 			    ad, ad_child_list);
813 		}
814 
815 		awc->aw_parent = ad;
816 		break;
817 
818 	default:
819 		ACPI_FREE(devinfo);
820 		break;
821 	}
822 
823 	return AE_OK;
824 }
825 
826 static ACPI_STATUS
827 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
828     void *context, void **status)
829 {
830 	struct acpi_walkcontext *awc = context;
831 
832 	KASSERT(awc != NULL);
833 	KASSERT(awc->aw_parent != NULL);
834 
835 	if (handle == awc->aw_parent->ad_handle)
836 		awc->aw_parent = awc->aw_parent->ad_parent;
837 
838 	return AE_OK;
839 }
840 
841 static void
842 acpi_make_name(struct acpi_devnode *ad, uint32_t name)
843 {
844 	ACPI_NAME_UNION *anu;
845 	int clear, i;
846 
847 	anu = (ACPI_NAME_UNION *)&name;
848 	ad->ad_name[4] = '\0';
849 
850 	for (i = 3, clear = 0; i >= 0; i--) {
851 
852 		if (clear == 0 && anu->Ascii[i] == '_')
853 			ad->ad_name[i] = '\0';
854 		else {
855 			ad->ad_name[i] = anu->Ascii[i];
856 			clear = 1;
857 		}
858 	}
859 
860 	if (ad->ad_name[0] == '\0')
861 		ad->ad_name[0] = '_';
862 }
863 
864 bus_dma_tag_t
865 acpi_default_dma_tag(struct acpi_softc *sc, struct acpi_devnode *ad)
866 {
867 	return sc->sc_dmat;
868 }
869 __weak_alias(acpi_get_dma_tag,acpi_default_dma_tag);
870 
871 bus_dma_tag_t
872 acpi_default_dma64_tag(struct acpi_softc *sc, struct acpi_devnode *ad)
873 {
874 	return sc->sc_dmat64;
875 }
876 __weak_alias(acpi_get_dma64_tag,acpi_default_dma64_tag);
877 
878 pci_chipset_tag_t
879 acpi_default_pci_chipset_tag(struct acpi_softc *sc, int seg, int bbn)
880 {
881 	return NULL;
882 }
883 __weak_alias(acpi_get_pci_chipset_tag,acpi_default_pci_chipset_tag);
884 
885 /*
886  * Device attachment.
887  */
888 static int
889 acpi_rescan(device_t self, const char *ifattr, const int *locators)
890 {
891 	struct acpi_softc *sc = device_private(self);
892 	struct acpi_attach_args aa;
893 
894 	/*
895 	 * Try to attach hpet(4) first via a specific table.
896 	 */
897 	aa.aa_memt = sc->sc_memt;
898 
899 	if (ifattr_match(ifattr, "acpihpetbus") && sc->sc_hpet == NULL) {
900 		sc->sc_hpet = config_found(sc->sc_dev, &aa, NULL,
901 					   CFARG_IATTR, "acpihpetbus",
902 					   CFARG_EOL);
903 	}
904 
905 	/*
906 	 * A two-pass scan for acpinodebus.
907 	 */
908 	if (ifattr_match(ifattr, "acpinodebus")) {
909 		acpi_rescan_early(sc);
910 		acpi_rescan_nodes(sc);
911 	}
912 
913 	/*
914 	 * Attach APM emulation and acpiwdrt(4).
915 	 */
916 	if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL) {
917 		sc->sc_apmbus = config_found(sc->sc_dev, NULL, NULL,
918 					     CFARG_IATTR, "acpiapmbus",
919 					     CFARG_EOL);
920 	}
921 
922 	if (ifattr_match(ifattr, "acpiwdrtbus") && sc->sc_wdrt == NULL) {
923 		sc->sc_wdrt = config_found(sc->sc_dev, NULL, NULL,
924 					   CFARG_IATTR, "acpiwdrtbus",
925 					   CFARG_EOL);
926 	}
927 
928 	return 0;
929 }
930 
931 static void
932 acpi_rescan_early(struct acpi_softc *sc)
933 {
934 	struct acpi_attach_args aa;
935 	struct acpi_devnode *ad;
936 
937 	/*
938 	 * First scan for devices such as acpiec(4) that
939 	 * should be always attached before anything else.
940 	 * We want these devices to attach regardless of
941 	 * the device status and other restrictions.
942 	 */
943 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
944 
945 		if (ad->ad_device != NULL)
946 			continue;
947 
948 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
949 			continue;
950 
951 		if (acpi_match_hid(ad->ad_devinfo, acpi_early_ids) == 0)
952 			continue;
953 
954 		KASSERT(ad->ad_handle != NULL);
955 
956 		aa.aa_node = ad;
957 		aa.aa_iot = sc->sc_iot;
958 		aa.aa_memt = sc->sc_memt;
959 		if (ad->ad_pciinfo != NULL) {
960 			aa.aa_pc = ad->ad_pciinfo->ap_pc;
961 			aa.aa_pciflags = sc->sc_pciflags;
962 		}
963 		aa.aa_ic = sc->sc_ic;
964 		aa.aa_dmat = ad->ad_dmat;
965 		aa.aa_dmat64 = ad->ad_dmat64;
966 
967 		ad->ad_device = config_found(sc->sc_dev, &aa, acpi_print,
968 		    CFARG_IATTR, "acpinodebus",
969 		    CFARG_DEVHANDLE, devhandle_from_acpi(ad->ad_handle),
970 		    CFARG_EOL);
971 	}
972 }
973 
974 static void
975 acpi_rescan_nodes(struct acpi_softc *sc)
976 {
977 	const char * const hpet_ids[] = { "PNP0103", NULL };
978 	struct acpi_attach_args aa;
979 	struct acpi_devnode *ad;
980 	ACPI_DEVICE_INFO *di;
981 
982 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
983 
984 		if (ad->ad_device != NULL)
985 			continue;
986 
987 		/*
988 		 * There is a bug in ACPICA: it defines the type
989 		 * of the scopes incorrectly for its own reasons.
990 		 */
991 		if (acpi_is_scope(ad) != false)
992 			continue;
993 
994 		di = ad->ad_devinfo;
995 
996 		/*
997 		 * We only attach devices which are present, enabled, and
998 		 * functioning properly. However, if a device is enabled,
999 		 * it is decoding resources and we should claim these,
1000 		 * if possible. This requires changes to bus_space(9).
1001 		 */
1002 		if (di->Type == ACPI_TYPE_DEVICE &&
1003 		    !acpi_device_present(ad->ad_handle)) {
1004 			continue;
1005 		}
1006 
1007 		if (di->Type == ACPI_TYPE_POWER)
1008 			continue;
1009 
1010 		if (di->Type == ACPI_TYPE_PROCESSOR)
1011 			continue;
1012 
1013 		if (acpi_match_hid(di, acpi_early_ids) != 0)
1014 			continue;
1015 
1016 		if (acpi_match_hid(di, acpi_ignored_ids) != 0)
1017 			continue;
1018 
1019 		if (acpi_match_hid(di, hpet_ids) != 0 && sc->sc_hpet != NULL)
1020 			continue;
1021 
1022 		KASSERT(ad->ad_handle != NULL);
1023 
1024 		aa.aa_node = ad;
1025 		aa.aa_iot = sc->sc_iot;
1026 		aa.aa_memt = sc->sc_memt;
1027 		if (ad->ad_pciinfo != NULL) {
1028 			aa.aa_pc = ad->ad_pciinfo->ap_pc;
1029 			aa.aa_pciflags = sc->sc_pciflags;
1030 		}
1031 		aa.aa_ic = sc->sc_ic;
1032 		aa.aa_dmat = ad->ad_dmat;
1033 		aa.aa_dmat64 = ad->ad_dmat64;
1034 
1035 		ad->ad_device = config_found(sc->sc_dev, &aa, acpi_print,
1036 		    CFARG_IATTR, "acpinodebus",
1037 		    CFARG_DEVHANDLE, devhandle_from_acpi(ad->ad_handle),
1038 		    CFARG_EOL);
1039 	}
1040 }
1041 
1042 static void
1043 acpi_rescan_capabilities(device_t self)
1044 {
1045 	struct acpi_softc *sc = device_private(self);
1046 	struct acpi_devnode *ad;
1047 	ACPI_HANDLE tmp;
1048 	ACPI_STATUS rv;
1049 
1050 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1051 
1052 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
1053 			continue;
1054 
1055 		/*
1056 		 * Scan power resource capabilities.
1057 		 *
1058 		 * If any power states are supported,
1059 		 * at least _PR0 and _PR3 must be present.
1060 		 */
1061 		rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
1062 
1063 		if (ACPI_SUCCESS(rv)) {
1064 			ad->ad_flags |= ACPI_DEVICE_POWER;
1065 			acpi_power_add(ad);
1066 		}
1067 
1068 		/*
1069 		 * Scan wake-up capabilities.
1070 		 */
1071 		if (ad->ad_wakedev != NULL) {
1072 			ad->ad_flags |= ACPI_DEVICE_WAKEUP;
1073 			acpi_wakedev_add(ad);
1074 		}
1075 
1076 		/*
1077 		 * Scan docking stations.
1078 		 */
1079 		rv = AcpiGetHandle(ad->ad_handle, "_DCK", &tmp);
1080 
1081 		if (ACPI_SUCCESS(rv))
1082 			ad->ad_flags |= ACPI_DEVICE_DOCK;
1083 
1084 		/*
1085 		 * Scan devices that are ejectable.
1086 		 */
1087 		rv = AcpiGetHandle(ad->ad_handle, "_EJ0", &tmp);
1088 
1089 		if (ACPI_SUCCESS(rv))
1090 			ad->ad_flags |= ACPI_DEVICE_EJECT;
1091 	}
1092 }
1093 
1094 static int
1095 acpi_print(void *aux, const char *pnp)
1096 {
1097 	struct acpi_attach_args *aa = aux;
1098 	struct acpi_devnode *ad;
1099 	const char *hid, *uid;
1100 	ACPI_DEVICE_INFO *di;
1101 
1102 	ad = aa->aa_node;
1103 	di = ad->ad_devinfo;
1104 
1105 	hid = di->HardwareId.String;
1106 	uid = di->UniqueId.String;
1107 
1108 	if (pnp != NULL) {
1109 
1110 		if (di->Type != ACPI_TYPE_DEVICE) {
1111 
1112 			aprint_normal("%s (ACPI Object Type '%s') at %s",
1113 			    ad->ad_name, AcpiUtGetTypeName(ad->ad_type), pnp);
1114 
1115 			return UNCONF;
1116 		}
1117 
1118 		if ((di->Valid & ACPI_VALID_HID) == 0 || hid == NULL)
1119 			return 0;
1120 
1121 		aprint_normal("%s (%s) ", ad->ad_name, hid);
1122 		acpi_print_dev(hid);
1123 		aprint_normal("at %s", pnp);
1124 
1125 		return UNCONF;
1126 	}
1127 
1128 	aprint_normal(" (%s", ad->ad_name);
1129 
1130 	if ((di->Valid & ACPI_VALID_HID) != 0 && hid != NULL) {
1131 
1132 		aprint_normal(", %s", hid);
1133 
1134 		if ((di->Valid & ACPI_VALID_UID) != 0 && uid != NULL) {
1135 
1136 			if (uid[0] == '\0')
1137 				uid = "<null>";
1138 
1139 			aprint_normal("-%s", uid);
1140 		}
1141 	}
1142 
1143 	aprint_normal(")");
1144 
1145 	return UNCONF;
1146 }
1147 
1148 /*
1149  * Notify.
1150  */
1151 static void
1152 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
1153 {
1154 	struct acpi_softc *sc = acpi_softc;
1155 	struct acpi_devnode *ad;
1156 
1157 	KASSERT(sc != NULL);
1158 	KASSERT(aux == NULL);
1159 	KASSERT(acpi_active != 0);
1160 
1161 	if (acpi_suspended != 0)
1162 		return;
1163 
1164 	/*
1165 	 *  System: 0x00 - 0x7F.
1166 	 *  Device: 0x80 - 0xFF.
1167 	 */
1168 	switch (event) {
1169 
1170 	case ACPI_NOTIFY_BUS_CHECK:
1171 	case ACPI_NOTIFY_DEVICE_CHECK:
1172 	case ACPI_NOTIFY_DEVICE_WAKE:
1173 	case ACPI_NOTIFY_EJECT_REQUEST:
1174 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
1175 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1176 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1177 	case ACPI_NOTIFY_POWER_FAULT:
1178 	case ACPI_NOTIFY_CAPABILITIES_CHECK:
1179 	case ACPI_NOTIFY_DEVICE_PLD_CHECK:
1180 	case ACPI_NOTIFY_RESERVED:
1181 	case ACPI_NOTIFY_LOCALITY_UPDATE:
1182 		break;
1183 	}
1184 
1185 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
1186 		"%s (%p)\n", event, acpi_name(handle), handle));
1187 
1188 	/*
1189 	 * We deliver notifications only to drivers
1190 	 * that have been successfully attached and
1191 	 * that have registered a handler with us.
1192 	 * The opaque pointer is always the device_t.
1193 	 */
1194 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1195 
1196 		if (ad->ad_device == NULL)
1197 			continue;
1198 
1199 		if (ad->ad_notify == NULL)
1200 			continue;
1201 
1202 		if (ad->ad_handle != handle)
1203 			continue;
1204 
1205 		(*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
1206 
1207 		return;
1208 	}
1209 
1210 	aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
1211 	    "for %s (%p)\n", event, acpi_name(handle), handle);
1212 }
1213 
1214 bool
1215 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
1216 {
1217 	struct acpi_softc *sc = acpi_softc;
1218 
1219 	KASSERT(sc != NULL);
1220 	KASSERT(acpi_active != 0);
1221 
1222 	if (acpi_suspended != 0)
1223 		goto fail;
1224 
1225 	if (ad == NULL || notify == NULL)
1226 		goto fail;
1227 
1228 	ad->ad_notify = notify;
1229 
1230 	return true;
1231 
1232 fail:
1233 	aprint_error_dev(sc->sc_dev, "failed to register notify "
1234 	    "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
1235 
1236 	return false;
1237 }
1238 
1239 void
1240 acpi_deregister_notify(struct acpi_devnode *ad)
1241 {
1242 
1243 	ad->ad_notify = NULL;
1244 }
1245 
1246 /*
1247  * Fixed buttons.
1248  */
1249 static void
1250 acpi_register_fixed_button(struct acpi_softc *sc, int event)
1251 {
1252 	struct sysmon_pswitch *smpsw;
1253 	ACPI_STATUS rv;
1254 	int type;
1255 
1256 	switch (event) {
1257 
1258 	case ACPI_EVENT_POWER_BUTTON:
1259 
1260 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
1261 			return;
1262 
1263 		type = PSWITCH_TYPE_POWER;
1264 		smpsw = &sc->sc_smpsw_power;
1265 		break;
1266 
1267 	case ACPI_EVENT_SLEEP_BUTTON:
1268 
1269 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
1270 			return;
1271 
1272 		type = PSWITCH_TYPE_SLEEP;
1273 		smpsw = &sc->sc_smpsw_sleep;
1274 		break;
1275 
1276 	default:
1277 		rv = AE_TYPE;
1278 		goto fail;
1279 	}
1280 
1281 	smpsw->smpsw_type = type;
1282 	smpsw->smpsw_name = device_xname(sc->sc_dev);
1283 
1284 	if (sysmon_pswitch_register(smpsw) != 0) {
1285 		rv = AE_ERROR;
1286 		goto fail;
1287 	}
1288 
1289 	AcpiClearEvent(event);
1290 
1291 	rv = AcpiInstallFixedEventHandler(event,
1292 	    acpi_fixed_button_handler, smpsw);
1293 
1294 	if (ACPI_FAILURE(rv)) {
1295 		sysmon_pswitch_unregister(smpsw);
1296 		goto fail;
1297 	}
1298 
1299 	aprint_normal_dev(sc->sc_dev, "fixed %s button present\n",
1300 	    (type != PSWITCH_TYPE_SLEEP) ? "power" : "sleep");
1301 
1302 	return;
1303 
1304 fail:
1305 	aprint_error_dev(sc->sc_dev, "failed to register "
1306 	    "fixed event %d: %s\n", event, AcpiFormatException(rv));
1307 }
1308 
1309 static void
1310 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
1311 {
1312 	struct sysmon_pswitch *smpsw;
1313 	ACPI_STATUS rv;
1314 
1315 	switch (event) {
1316 
1317 	case ACPI_EVENT_POWER_BUTTON:
1318 		smpsw = &sc->sc_smpsw_power;
1319 
1320 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
1321 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
1322 			return;
1323 		}
1324 
1325 		break;
1326 
1327 	case ACPI_EVENT_SLEEP_BUTTON:
1328 		smpsw = &sc->sc_smpsw_sleep;
1329 
1330 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
1331 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
1332 			return;
1333 		}
1334 
1335 		break;
1336 
1337 	default:
1338 		rv = AE_TYPE;
1339 		goto fail;
1340 	}
1341 
1342 	rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
1343 
1344 	if (ACPI_SUCCESS(rv)) {
1345 		sysmon_pswitch_unregister(smpsw);
1346 		return;
1347 	}
1348 
1349 fail:
1350 	aprint_error_dev(sc->sc_dev, "failed to deregister "
1351 	    "fixed event: %s\n", AcpiFormatException(rv));
1352 }
1353 
1354 static uint32_t
1355 acpi_fixed_button_handler(void *context)
1356 {
1357 	static const int handler = OSL_NOTIFY_HANDLER;
1358 	struct sysmon_pswitch *smpsw = context;
1359 
1360 	(void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
1361 
1362 	return ACPI_INTERRUPT_HANDLED;
1363 }
1364 
1365 static void
1366 acpi_fixed_button_pressed(void *context)
1367 {
1368 	struct sysmon_pswitch *smpsw = context;
1369 
1370 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
1371 		(smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
1372 		"power" : "sleep"));
1373 
1374 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1375 }
1376 
1377 /*
1378  * Sleep.
1379  */
1380 static void
1381 acpi_sleep_init(struct acpi_softc *sc)
1382 {
1383 	uint8_t a, b, i;
1384 	ACPI_STATUS rv;
1385 
1386 	CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
1387 	CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
1388 	CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
1389 
1390 	/*
1391 	 * Evaluate supported sleep states.
1392 	 */
1393 	for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
1394 
1395 		rv = AcpiGetSleepTypeData(i, &a, &b);
1396 
1397 		if (ACPI_SUCCESS(rv))
1398 			sc->sc_sleepstates |= __BIT(i);
1399 	}
1400 }
1401 
1402 /*
1403  * Must be called with interrupts enabled.
1404  */
1405 void
1406 acpi_enter_sleep_state(int state)
1407 {
1408 	struct acpi_softc *sc = acpi_softc;
1409 	ACPI_STATUS rv;
1410 
1411 	if (acpi_softc == NULL)
1412 		return;
1413 
1414 	if (state == sc->sc_sleepstate)
1415 		return;
1416 
1417 	if (state < ACPI_STATE_S0 || state > ACPI_STATE_S5)
1418 		return;
1419 
1420 	aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
1421 
1422 	switch (state) {
1423 
1424 	case ACPI_STATE_S0:
1425 		sc->sc_sleepstate = ACPI_STATE_S0;
1426 		return;
1427 
1428 	case ACPI_STATE_S1:
1429 	case ACPI_STATE_S2:
1430 	case ACPI_STATE_S3:
1431 	case ACPI_STATE_S4:
1432 
1433 		if ((sc->sc_sleepstates & __BIT(state)) == 0) {
1434 			aprint_error_dev(sc->sc_dev, "sleep state "
1435 			    "S%d is not available\n", state);
1436 			return;
1437 		}
1438 
1439 		/*
1440 		 * Evaluate the _TTS method. This should be done before
1441 		 * pmf_system_suspend(9) and the evaluation of _PTS.
1442 		 * We should also re-evaluate this once we return to
1443 		 * S0 or if we abort the sleep state transition in the
1444 		 * middle (see ACPI 3.0, section 7.3.6). In reality,
1445 		 * however, the _TTS method is seldom seen in the field.
1446 		 */
1447 		rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
1448 
1449 		if (ACPI_SUCCESS(rv))
1450 			aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
1451 
1452 		if (state != ACPI_STATE_S1 &&
1453 		    pmf_system_suspend(PMF_Q_NONE) != true) {
1454 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1455 			break;
1456 		}
1457 
1458 		/*
1459 		 * This will evaluate the  _PTS and _SST methods,
1460 		 * but unlike the documentation claims, not _GTS,
1461 		 * which is evaluated in AcpiEnterSleepState().
1462 		 * This must be called with interrupts enabled.
1463 		 */
1464 		rv = AcpiEnterSleepStatePrep(state);
1465 
1466 		if (ACPI_FAILURE(rv)) {
1467 			aprint_error_dev(sc->sc_dev, "failed to prepare "
1468 			    "S%d: %s\n", state, AcpiFormatException(rv));
1469 			break;
1470 		}
1471 
1472 		/*
1473 		 * After the _PTS method has been evaluated, we can
1474 		 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
1475 		 */
1476 		acpi_wakedev_commit(sc, state);
1477 
1478 		sc->sc_sleepstate = state;
1479 
1480 		if (state == ACPI_STATE_S1) {
1481 
1482 			/*
1483 			 * Before the transition to S1, CPU caches
1484 			 * must be flushed (see ACPI 4.0, 7.3.4.2).
1485 			 *
1486 			 * Note that interrupts must be off before
1487 			 * calling AcpiEnterSleepState(). Conversely,
1488 			 * AcpiLeaveSleepState() should always be
1489 			 * called with interrupts enabled.
1490 			 */
1491 			acpi_md_OsDisableInterrupt();
1492 
1493 			ACPI_FLUSH_CPU_CACHE();
1494 			rv = AcpiEnterSleepState(state);
1495 
1496 			if (ACPI_FAILURE(rv))
1497 				aprint_error_dev(sc->sc_dev, "failed to "
1498 				    "enter S1: %s\n", AcpiFormatException(rv));
1499 
1500 			/*
1501 			 * Clear fixed events and disable all GPEs before
1502 			 * interrupts are enabled.
1503 			 */
1504 			AcpiClearEvent(ACPI_EVENT_PMTIMER);
1505 			AcpiClearEvent(ACPI_EVENT_GLOBAL);
1506 			AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1507 			AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1508 			AcpiClearEvent(ACPI_EVENT_RTC);
1509 #if (!ACPI_REDUCED_HARDWARE)
1510 			AcpiHwDisableAllGpes();
1511 #endif
1512 
1513 			acpi_md_OsEnableInterrupt();
1514 			rv = AcpiLeaveSleepState(state);
1515 
1516 		} else {
1517 
1518 			(void)acpi_md_sleep(state);
1519 
1520 			if (state == ACPI_STATE_S4)
1521 				AcpiEnable();
1522 
1523 			(void)pmf_system_bus_resume(PMF_Q_NONE);
1524 			(void)AcpiLeaveSleepState(state);
1525 			(void)AcpiSetFirmwareWakingVector(0, 0);
1526 			(void)pmf_system_resume(PMF_Q_NONE);
1527 		}
1528 
1529 		/*
1530 		 * No wake GPEs should be enabled at runtime.
1531 		 */
1532 		acpi_wakedev_commit(sc, ACPI_STATE_S0);
1533 		break;
1534 
1535 	case ACPI_STATE_S5:
1536 
1537 		(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
1538 
1539 		rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1540 
1541 		if (ACPI_FAILURE(rv)) {
1542 			aprint_error_dev(sc->sc_dev, "failed to prepare "
1543 			    "S%d: %s\n", state, AcpiFormatException(rv));
1544 			break;
1545 		}
1546 
1547 		(void)AcpiDisableAllGpes();
1548 
1549 		DELAY(1000000);
1550 
1551 		sc->sc_sleepstate = state;
1552 		acpi_md_OsDisableInterrupt();
1553 
1554 		(void)AcpiEnterSleepState(ACPI_STATE_S5);
1555 
1556 		aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
1557 
1558 		break;
1559 	}
1560 
1561 	sc->sc_sleepstate = ACPI_STATE_S0;
1562 
1563 	(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
1564 }
1565 
1566 /*
1567  * Sysctl.
1568  */
1569 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1570 {
1571 	const struct sysctlnode *rnode, *snode;
1572 	int err;
1573 
1574 	err = sysctl_createv(clog, 0, NULL, &rnode,
1575 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
1576 	    "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
1577 	    NULL, 0, NULL, 0,
1578 	    CTL_HW, CTL_CREATE, CTL_EOL);
1579 
1580 	if (err != 0)
1581 		return;
1582 
1583 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
1584 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1585 	    "root", SYSCTL_DESCR("ACPI root pointer"),
1586 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1587 	    CTL_CREATE, CTL_EOL);
1588 
1589 	err = sysctl_createv(clog, 0, &rnode, &snode,
1590 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
1591 	    "sleep", SYSCTL_DESCR("ACPI sleep"),
1592 	    NULL, 0, NULL, 0,
1593 	    CTL_CREATE, CTL_EOL);
1594 
1595 	if (err != 0)
1596 		return;
1597 
1598 	(void)sysctl_createv(NULL, 0, &snode, NULL,
1599 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
1600 	    "state", SYSCTL_DESCR("System sleep state"),
1601 	    sysctl_hw_acpi_sleepstate, 0, NULL, 0,
1602 	    CTL_CREATE, CTL_EOL);
1603 
1604 	(void)sysctl_createv(NULL, 0, &snode, NULL,
1605 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
1606 	    "states", SYSCTL_DESCR("Supported sleep states"),
1607 	    sysctl_hw_acpi_sleepstates, 0, NULL, 0,
1608 	    CTL_CREATE, CTL_EOL);
1609 
1610 	err = sysctl_createv(clog, 0, &rnode, &rnode,
1611 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
1612 	    "stat", SYSCTL_DESCR("ACPI statistics"),
1613 	    NULL, 0, NULL, 0,
1614 	    CTL_CREATE, CTL_EOL);
1615 
1616 	if (err != 0)
1617 		return;
1618 
1619 	(void)sysctl_createv(clog, 0, &rnode, NULL,
1620 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1621 	    "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
1622 	    NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
1623 	    CTL_CREATE, CTL_EOL);
1624 
1625 	(void)sysctl_createv(clog, 0, &rnode, NULL,
1626 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1627 	    "sci", SYSCTL_DESCR("Number of SCI interrupts"),
1628 	    NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
1629 	    CTL_CREATE, CTL_EOL);
1630 
1631 	(void)sysctl_createv(clog, 0, &rnode, NULL,
1632 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1633 	    "fixed", SYSCTL_DESCR("Number of fixed events"),
1634 	    sysctl_hw_acpi_fixedstats, 0, NULL, 0,
1635 	    CTL_CREATE, CTL_EOL);
1636 
1637 	(void)sysctl_createv(clog, 0, &rnode, NULL,
1638 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1639 	    "method", SYSCTL_DESCR("Number of methods executed"),
1640 	    NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
1641 	    CTL_CREATE, CTL_EOL);
1642 
1643 	CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
1644 	CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
1645 }
1646 
1647 static int
1648 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
1649 {
1650 	struct sysctlnode node;
1651 	uint64_t t;
1652 	int err, i;
1653 
1654 	for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
1655 		t += AcpiFixedEventCount[i];
1656 
1657 	node = *rnode;
1658 	node.sysctl_data = &t;
1659 
1660 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
1661 
1662 	if (err || newp == NULL)
1663 		return err;
1664 
1665 	return 0;
1666 }
1667 
1668 static int
1669 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1670 {
1671 	struct acpi_softc *sc = acpi_softc;
1672 	struct sysctlnode node;
1673 	int err, t;
1674 
1675 	if (acpi_softc == NULL)
1676 		return ENOSYS;
1677 
1678 	node = *rnode;
1679 	t = sc->sc_sleepstate;
1680 	node.sysctl_data = &t;
1681 
1682 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
1683 
1684 	if (err || newp == NULL)
1685 		return err;
1686 
1687 	if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
1688 		return EINVAL;
1689 
1690 	acpi_enter_sleep_state(t);
1691 
1692 	return 0;
1693 }
1694 
1695 static int
1696 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
1697 {
1698 	struct acpi_softc *sc = acpi_softc;
1699 	struct sysctlnode node;
1700 	char t[3 * 6 + 1];
1701 	int err;
1702 
1703 	if (acpi_softc == NULL)
1704 		return ENOSYS;
1705 
1706 	(void)memset(t, '\0', sizeof(t));
1707 
1708 	(void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
1709 	    ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
1710 	    ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
1711 	    ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
1712 	    ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
1713 	    ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
1714 	    ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
1715 
1716 	node = *rnode;
1717 	node.sysctl_data = &t;
1718 
1719 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
1720 
1721 	if (err || newp == NULL)
1722 		return err;
1723 
1724 	return 0;
1725 }
1726 
1727 /*
1728  * Tables.
1729  */
1730 ACPI_PHYSICAL_ADDRESS
1731 acpi_OsGetRootPointer(void)
1732 {
1733 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
1734 
1735 	/*
1736 	 * We let MD code handle this since there are multiple ways to do it:
1737 	 *
1738 	 *	IA-32: Use AcpiFindRootPointer() to locate the RSDP.
1739 	 *
1740 	 *	IA-64: Use the EFI.
1741 	 */
1742 	PhysicalAddress = acpi_md_OsGetRootPointer();
1743 
1744 	if (acpi_root_pointer == 0)
1745 		acpi_root_pointer = PhysicalAddress;
1746 
1747 	return PhysicalAddress;
1748 }
1749 
1750 static ACPI_TABLE_HEADER *
1751 acpi_map_rsdt(void)
1752 {
1753 	ACPI_PHYSICAL_ADDRESS paddr;
1754 	ACPI_TABLE_RSDP *rsdp;
1755 
1756 	paddr = AcpiOsGetRootPointer();
1757 
1758 	if (paddr == 0)
1759 		return NULL;
1760 
1761 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1762 
1763 	if (rsdp == NULL)
1764 		return NULL;
1765 
1766 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1767 		paddr = rsdp->XsdtPhysicalAddress;
1768 	else
1769 		paddr = rsdp->RsdtPhysicalAddress;
1770 
1771 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1772 
1773 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1774 }
1775 
1776 /*
1777  * XXX: Refactor to be a generic function that unmaps tables.
1778  */
1779 static void
1780 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1781 {
1782 
1783 	if (rsdt == NULL)
1784 		return;
1785 
1786 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1787 }
1788 
1789 /*
1790  * XXX: Refactor to be a generic function that maps tables.
1791  */
1792 ACPI_STATUS
1793 acpi_madt_map(void)
1794 {
1795 	ACPI_STATUS  rv;
1796 
1797 	if (madt_header != NULL)
1798 		return AE_ALREADY_EXISTS;
1799 
1800 	rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
1801 
1802 	if (ACPI_FAILURE(rv))
1803 		return rv;
1804 
1805 	return AE_OK;
1806 }
1807 
1808 void
1809 acpi_madt_unmap(void)
1810 {
1811 	madt_header = NULL;
1812 }
1813 
1814 ACPI_STATUS
1815 acpi_gtdt_map(void)
1816 {
1817 	ACPI_STATUS  rv;
1818 
1819 	if (gtdt_header != NULL)
1820 		return AE_ALREADY_EXISTS;
1821 
1822 	rv = AcpiGetTable(ACPI_SIG_GTDT, 1, &gtdt_header);
1823 
1824 	if (ACPI_FAILURE(rv))
1825 		return rv;
1826 
1827 	return AE_OK;
1828 }
1829 
1830 void
1831 acpi_gtdt_unmap(void)
1832 {
1833 	gtdt_header = NULL;
1834 }
1835 
1836 /*
1837  * XXX: Refactor to be a generic function that walks tables.
1838  */
1839 void
1840 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
1841 {
1842 	ACPI_SUBTABLE_HEADER *hdrp;
1843 	char *madtend, *where;
1844 
1845 	madtend = (char *)madt_header + madt_header->Length;
1846 	where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
1847 
1848 	while (where < madtend) {
1849 
1850 		hdrp = (ACPI_SUBTABLE_HEADER *)where;
1851 
1852 		if (hdrp->Length == 0 || ACPI_FAILURE(func(hdrp, aux)))
1853 			break;
1854 
1855 		where += hdrp->Length;
1856 	}
1857 }
1858 
1859 void
1860 acpi_gtdt_walk(ACPI_STATUS (*func)(ACPI_GTDT_HEADER *, void *), void *aux)
1861 {
1862 	ACPI_GTDT_HEADER *hdrp;
1863 	char *gtdtend, *where;
1864 
1865 	gtdtend = (char *)gtdt_header + gtdt_header->Length;
1866 	where = (char *)gtdt_header + sizeof (ACPI_TABLE_GTDT);
1867 
1868 	while (where < gtdtend) {
1869 
1870 		hdrp = (ACPI_GTDT_HEADER *)where;
1871 
1872 		if (hdrp->Length == 0 || ACPI_FAILURE(func(hdrp, aux)))
1873 			break;
1874 
1875 		where += hdrp->Length;
1876 	}
1877 }
1878 
1879 /*
1880  * Miscellaneous.
1881  */
1882 static bool
1883 acpi_is_scope(struct acpi_devnode *ad)
1884 {
1885 	int i;
1886 
1887 	/*
1888 	 * Return true if the node is a root scope.
1889 	 */
1890 	if (ad->ad_parent == NULL)
1891 		return false;
1892 
1893 	if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
1894 		return false;
1895 
1896 	for (i = 0; i < __arraycount(acpi_scopes); i++) {
1897 
1898 		if (acpi_scopes[i] == NULL)
1899 			continue;
1900 
1901 		if (ad->ad_handle == acpi_scopes[i])
1902 			return true;
1903 	}
1904 
1905 	return false;
1906 }
1907 
1908 bool
1909 acpi_device_present(ACPI_HANDLE handle)
1910 {
1911 	ACPI_STATUS rv;
1912 	ACPI_INTEGER sta;
1913 
1914 	rv = acpi_eval_integer(handle, "_STA", &sta);
1915 
1916 	if (ACPI_FAILURE(rv)) {
1917 		/* No _STA method -> must be there */
1918 		return rv == AE_NOT_FOUND;
1919 	}
1920 
1921 	return (sta & ACPI_STA_OK) == ACPI_STA_OK;
1922 }
1923 
1924 /*
1925  * ACPIVERBOSE.
1926  */
1927 void
1928 acpi_load_verbose(void)
1929 {
1930 
1931 	if (acpi_verbose_loaded == 0)
1932 		module_autoload("acpiverbose", MODULE_CLASS_MISC);
1933 }
1934 
1935 void
1936 acpi_print_verbose_stub(struct acpi_softc *sc)
1937 {
1938 
1939 	acpi_load_verbose();
1940 
1941 	if (acpi_verbose_loaded != 0)
1942 		acpi_print_verbose(sc);
1943 }
1944 
1945 void
1946 acpi_print_dev_stub(const char *pnpstr)
1947 {
1948 
1949 	acpi_load_verbose();
1950 
1951 	if (acpi_verbose_loaded != 0)
1952 		acpi_print_dev(pnpstr);
1953 }
1954 
1955 MALLOC_DECLARE(M_ACPI); /* XXX: ACPI_ACTIVATE_DEV should use kmem(9). */
1956 
1957 /*
1958  * ACPI_ACTIVATE_DEV.
1959  */
1960 static void
1961 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
1962 {
1963 
1964 #ifndef ACPI_ACTIVATE_DEV
1965 	return;
1966 }
1967 #else
1968 	static const int valid = ACPI_VALID_HID;
1969 	ACPI_DEVICE_INFO *newdi;
1970 	ACPI_STATUS rv;
1971 
1972 
1973 	/*
1974 	 * If the device is valid and present,
1975 	 * but not enabled, try to activate it.
1976 	 */
1977 	if (((*di)->Valid & valid) != valid)
1978 		return;
1979 
1980 	if (!acpi_device_present(handle))
1981 		return;
1982 
1983 	rv = acpi_allocate_resources(handle);
1984 
1985 	if (ACPI_FAILURE(rv))
1986 		goto fail;
1987 
1988 	rv = AcpiGetObjectInfo(handle, &newdi);
1989 
1990 	if (ACPI_FAILURE(rv))
1991 		goto fail;
1992 
1993 	ACPI_FREE(*di);
1994 	*di = newdi;
1995 
1996 	aprint_verbose_dev(acpi_softc->sc_dev,
1997 	    "%s activated\n", (*di)->HardwareId.String);
1998 
1999 	return;
2000 
2001 fail:
2002 	aprint_error_dev(acpi_softc->sc_dev, "failed to "
2003 	    "activate %s\n", (*di)->HardwareId.String);
2004 }
2005 
2006 /*
2007  * XXX: This very incomplete.
2008  */
2009 ACPI_STATUS
2010 acpi_allocate_resources(ACPI_HANDLE handle)
2011 {
2012 	ACPI_BUFFER bufp, bufc, bufn;
2013 	ACPI_RESOURCE *resp, *resc, *resn;
2014 	ACPI_RESOURCE_IRQ *irq;
2015 #if 0
2016 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
2017 #endif
2018 	ACPI_STATUS rv;
2019 	uint delta;
2020 
2021 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
2022 	if (ACPI_FAILURE(rv))
2023 		goto out;
2024 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
2025 	if (ACPI_FAILURE(rv)) {
2026 		goto out1;
2027 	}
2028 
2029 	bufn.Length = 1000;
2030 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
2031 	resp = bufp.Pointer;
2032 	resc = bufc.Pointer;
2033 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
2034 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
2035 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
2036 			resp = ACPI_NEXT_RESOURCE(resp);
2037 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
2038 			break;
2039 		/* Found identical Id */
2040 		resn->Type = resc->Type;
2041 		switch (resc->Type) {
2042 		case ACPI_RESOURCE_TYPE_IRQ:
2043 			memcpy(&resn->Data, &resp->Data,
2044 			       sizeof(ACPI_RESOURCE_IRQ));
2045 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
2046 			irq->Interrupts[0] =
2047 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
2048 			        Interrupts[irq->InterruptCount-1];
2049 			irq->InterruptCount = 1;
2050 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
2051 			break;
2052 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2053 			memcpy(&resn->Data, &resp->Data,
2054 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
2055 #if 0
2056 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
2057 			/*
2058 			 * XXX:	Not duplicating the interrupt logic above
2059 			 *	because its not clear what it accomplishes.
2060 			 */
2061 			xirq->Interrupts[0] =
2062 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
2063 			    Interrupts[irq->NumberOfInterrupts-1];
2064 			xirq->NumberOfInterrupts = 1;
2065 #endif
2066 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
2067 			break;
2068 		case ACPI_RESOURCE_TYPE_IO:
2069 			memcpy(&resn->Data, &resp->Data,
2070 			       sizeof(ACPI_RESOURCE_IO));
2071 			resn->Length = resp->Length;
2072 			break;
2073 		default:
2074 			aprint_error_dev(acpi_softc->sc_dev,
2075 			    "%s: invalid type %u\n", __func__, resc->Type);
2076 			rv = AE_BAD_DATA;
2077 			goto out2;
2078 		}
2079 		resc = ACPI_NEXT_RESOURCE(resc);
2080 		resn = ACPI_NEXT_RESOURCE(resn);
2081 		resp = ACPI_NEXT_RESOURCE(resp);
2082 		delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
2083 		if (delta >=
2084 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
2085 			bufn.Length *= 2;
2086 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
2087 					       M_ACPI, M_WAITOK);
2088 			resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
2089 			    delta);
2090 		}
2091 	}
2092 
2093 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
2094 		aprint_error_dev(acpi_softc->sc_dev,
2095 		    "%s: resc not exhausted\n", __func__);
2096 		rv = AE_BAD_DATA;
2097 		goto out3;
2098 	}
2099 
2100 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
2101 	rv = AcpiSetCurrentResources(handle, &bufn);
2102 
2103 	if (ACPI_FAILURE(rv))
2104 		aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
2105 		    "resources: %s\n", __func__, AcpiFormatException(rv));
2106 
2107 out3:
2108 	free(bufn.Pointer, M_ACPI);
2109 out2:
2110 	ACPI_FREE(bufc.Pointer);
2111 out1:
2112 	ACPI_FREE(bufp.Pointer);
2113 out:
2114 	return rv;
2115 }
2116 
2117 #endif	/* ACPI_ACTIVATE_DEV */
2118