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