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