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