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