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