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