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