acpi.c revision 1.107
1/*	$NetBSD: acpi.c,v 1.107 2007/12/15 02:19:55 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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*
40 * Copyright 2001, 2003 Wasabi Systems, Inc.
41 * All rights reserved.
42 *
43 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 *    notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 *    notice, this list of conditions and the following disclaimer in the
52 *    documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 *    must display the following acknowledgement:
55 *	This product includes software developed for the NetBSD Project by
56 *	Wasabi Systems, Inc.
57 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
58 *    or promote products derived from this software without specific prior
59 *    written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
65 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
66 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71 * POSSIBILITY OF SUCH DAMAGE.
72 */
73
74/*
75 * Autoconfiguration support for the Intel ACPI Component Architecture
76 * ACPI reference implementation.
77 */
78
79#include <sys/cdefs.h>
80__KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.107 2007/12/15 02:19:55 jmcneill Exp $");
81
82#include "opt_acpi.h"
83#include "opt_pcifixup.h"
84
85#include <sys/param.h>
86#include <sys/systm.h>
87#include <sys/device.h>
88#include <sys/malloc.h>
89#include <sys/mutex.h>
90#include <sys/kernel.h>
91#include <sys/proc.h>
92#include <sys/sysctl.h>
93
94#include <dev/acpi/acpica.h>
95#include <dev/acpi/acpireg.h>
96#include <dev/acpi/acpivar.h>
97#include <dev/acpi/acpi_osd.h>
98#include <dev/acpi/acpi_timer.h>
99#ifdef ACPIVERBOSE
100#include <dev/acpi/acpidevs_data.h>
101#endif
102
103#if defined(ACPI_PCI_FIXUP)
104#error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED.  Please adjust your kernel configuration file.
105#endif
106
107#ifdef PCI_INTR_FIXUP_DISABLED
108#include <dev/pci/pcidevs.h>
109#endif
110
111MALLOC_DECLARE(M_ACPI);
112
113#include <machine/acpi_machdep.h>
114
115#ifdef ACPI_DEBUGGER
116#define	ACPI_DBGR_INIT		0x01
117#define	ACPI_DBGR_TABLES	0x02
118#define	ACPI_DBGR_ENABLE	0x04
119#define	ACPI_DBGR_PROBE		0x08
120#define	ACPI_DBGR_RUNNING	0x10
121
122static int acpi_dbgr = 0x00;
123#endif
124
125static ACPI_TABLE_DESC	acpi_initial_tables[128];
126
127static int	acpi_match(struct device *, struct cfdata *, void *);
128static void	acpi_attach(struct device *, struct device *, void *);
129
130static int	acpi_print(void *aux, const char *);
131
132static int	sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
133
134extern struct cfdriver acpi_cd;
135
136CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
137    acpi_match, acpi_attach, NULL, NULL);
138
139/*
140 * This is a flag we set when the ACPI subsystem is active.  Machine
141 * dependent code may wish to skip other steps (such as attaching
142 * subsystems that ACPI supercedes) when ACPI is active.
143 */
144int	acpi_active;
145int	acpi_force_load;
146
147/*
148 * Pointer to the ACPI subsystem's state.  There can be only
149 * one ACPI instance.
150 */
151struct acpi_softc *acpi_softc;
152
153/*
154 * Locking stuff.
155 */
156static kmutex_t acpi_slock;
157static int acpi_locked;
158extern kmutex_t acpi_interrupt_list_mtx;
159
160/*
161 * sysctl-related information
162 */
163
164static int acpi_node = CTL_EOL;
165static uint64_t acpi_root_pointer;	/* found as hw.acpi.root */
166static int acpi_sleepstate = ACPI_STATE_S0;
167
168/*
169 * Prototypes.
170 */
171static void		acpi_shutdown(void *);
172static void		acpi_build_tree(struct acpi_softc *);
173static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
174
175static void		acpi_enable_fixed_events(struct acpi_softc *);
176
177/*
178 * acpi_probe:
179 *
180 *	Probe for ACPI support.  This is called by the
181 *	machine-dependent ACPI front-end.  All of the
182 *	actual work is done by ACPICA.
183 *
184 *	NOTE: This is not an autoconfiguration interface function.
185 */
186int
187acpi_probe(void)
188{
189	static int beenhere;
190	ACPI_TABLE_HEADER xsdt;
191	ACPI_STATUS rv;
192
193	if (beenhere != 0)
194		panic("acpi_probe: ACPI has already been probed");
195	beenhere = 1;
196
197	mutex_init(&acpi_slock, MUTEX_DEFAULT, IPL_NONE);
198	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
199	acpi_locked = 0;
200
201	/*
202	 * Start up ACPICA.
203	 */
204#ifdef ACPI_DEBUGGER
205	if (acpi_dbgr & ACPI_DBGR_INIT)
206		acpi_osd_debugger();
207#endif
208
209	AcpiGbl_AllMethodsSerialized = FALSE;
210	AcpiGbl_EnableInterpreterSlack = TRUE;
211
212	rv = AcpiInitializeSubsystem();
213	if (ACPI_FAILURE(rv)) {
214		printf("ACPI: unable to initialize ACPICA: %s\n",
215		    AcpiFormatException(rv));
216		return 0;
217	}
218
219	rv = AcpiInitializeTables(acpi_initial_tables, 128, 0);
220	if (ACPI_FAILURE(rv)) {
221		printf("ACPI: unable to initialize ACPI tables: %s\n",
222		    AcpiFormatException(rv));
223		return 0;
224	}
225
226	rv = AcpiReallocateRootTable();
227	if (ACPI_FAILURE(rv)) {
228		printf("ACPI: unable to reallocate root table: %s\n",
229		    AcpiFormatException(rv));
230		return 0;
231	}
232
233#ifdef ACPI_DEBUGGER
234	if (acpi_dbgr & ACPI_DBGR_TABLES)
235		acpi_osd_debugger();
236#endif
237
238	rv = AcpiLoadTables();
239	if (ACPI_FAILURE(rv)) {
240		printf("ACPI: unable to load tables: %s\n",
241		    AcpiFormatException(rv));
242		return 0;
243	}
244
245	if (ACPI_FAILURE(AcpiGetTableHeader(ACPI_SIG_XSDT, 0, &xsdt)))
246		memset(&xsdt, 0, sizeof(xsdt));
247
248	if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
249		printf("ACPI: BIOS implementation in listed as broken:\n");
250		printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
251		       "AslId <%4.4s,%08x>\n",
252			xsdt.OemId, xsdt.OemTableId,
253		        xsdt.OemRevision,
254			xsdt.AslCompilerId,
255		        xsdt.AslCompilerRevision);
256		printf("ACPI: not used. set acpi_force_load to use anyway.\n");
257		return 0;
258	}
259
260#if notyet
261	/* Install the default address space handlers. */
262	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
263	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
264	if (ACPI_FAILURE(rv)) {
265		printf("ACPI: unable to initialise SystemMemory handler: %s\n",
266		    AcpiFormatException(rv));
267		return 0;
268	}
269	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
270	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
271	if (ACPI_FAILURE(rv)) {
272		printf("ACPI: unable to initialise SystemIO handler: %s\n",
273		     AcpiFormatException(rv));
274		return 0;
275	}
276	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
277	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
278	if (ACPI_FAILURE(rv)) {
279		printf("ACPI: unabled to initialise PciConfig handler: %s\n",
280		    AcpiFormatException(rv));
281		return 0;
282	}
283#endif
284
285	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
286	if (ACPI_FAILURE(rv)) {
287		printf("ACPI: unable to enable: %s\n", AcpiFormatException(rv));
288		return 0;
289	}
290
291	/*
292	 * Looks like we have ACPI!
293	 */
294
295	return 1;
296}
297
298static int
299acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
300{
301	struct cfattach *ca;
302
303	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
304	return (ca == &acpi_ca);
305}
306
307int
308acpi_check(device_t parent, const char *ifattr)
309{
310	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
311}
312
313ACPI_PHYSICAL_ADDRESS
314acpi_OsGetRootPointer(void)
315{
316	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
317
318	/*
319	 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
320	 *
321	 * IA-64: Use the EFI.
322	 *
323	 * We let MD code handle this since there are multiple
324	 * ways to do it.
325	 */
326
327	PhysicalAddress = acpi_md_OsGetRootPointer();
328
329	if (acpi_root_pointer == 0)
330		acpi_root_pointer = PhysicalAddress;
331
332	return PhysicalAddress;
333}
334
335/*
336 * acpi_match:
337 *
338 *	Autoconfiguration `match' routine.
339 */
340static int
341acpi_match(struct device *parent, struct cfdata *match,
342    void *aux)
343{
344	/*
345	 * XXX Check other locators?  Hard to know -- machine
346	 * dependent code has already checked for the presence
347	 * of ACPI by calling acpi_probe(), so I suppose we
348	 * don't really have to do anything else.
349	 */
350	return 1;
351}
352
353/*
354 * acpi_attach:
355 *
356 *	Autoconfiguration `attach' routine.  Finish initializing
357 *	ACPICA (some initialization was done in acpi_probe(),
358 *	which was required to check for the presence of ACPI),
359 *	and enable the ACPI subsystem.
360 */
361static void
362acpi_attach(struct device *parent, struct device *self, void *aux)
363{
364	struct acpi_softc *sc = (void *) self;
365	struct acpibus_attach_args *aa = aux;
366	ACPI_STATUS rv;
367	ACPI_TABLE_HEADER xsdt;
368
369	aprint_naive(": Advanced Configuration and Power Interface\n");
370	aprint_normal(": Advanced Configuration and Power Interface\n");
371
372	if (acpi_softc != NULL)
373		panic("acpi_attach: ACPI has already been attached");
374
375	sysmon_power_settype("acpi");
376
377	aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
378	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
379
380	if (ACPI_FAILURE(AcpiGetTableHeader(ACPI_SIG_XSDT, 0, &xsdt)))
381		memset(&xsdt, 0, sizeof(xsdt));
382
383	aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
384	    sc->sc_dev.dv_xname,
385	    xsdt.OemId, xsdt.OemTableId,
386	    xsdt.OemRevision,
387	    xsdt.AslCompilerId, xsdt.AslCompilerRevision);
388
389	sc->sc_quirks = acpi_find_quirks();
390
391	sc->sc_iot = aa->aa_iot;
392	sc->sc_memt = aa->aa_memt;
393	sc->sc_pc = aa->aa_pc;
394	sc->sc_pciflags = aa->aa_pciflags;
395	sc->sc_ic = aa->aa_ic;
396
397	acpi_softc = sc;
398
399	/*
400	 * Register null power management handler
401	 */
402	if (!pmf_device_register(self, NULL, NULL))
403		aprint_error_dev(self, "couldn't establish power handler\n");
404
405	/*
406	 * Bring ACPI on-line.
407	 */
408#ifdef ACPI_DEBUGGER
409	if (acpi_dbgr & ACPI_DBGR_ENABLE)
410		acpi_osd_debugger();
411#endif
412
413#define ACPI_ENABLE_PHASE1 \
414    (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
415#define ACPI_ENABLE_PHASE2 \
416    (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
417     ACPI_NO_ADDRESS_SPACE_INIT)
418
419	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
420	if (ACPI_FAILURE(rv)) {
421		aprint_error("%s: unable to enable ACPI: %s\n",
422		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
423		return;
424	}
425
426	acpi_md_callback();
427
428	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
429	if (ACPI_FAILURE(rv)) {
430		aprint_error("%s: unable to enable ACPI: %s\n",
431		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
432		return;
433	}
434
435	/* early EC handler initialization if ECDT table is available */
436	config_found_ia(&sc->sc_dev, "acpiecdtbus", NULL, NULL);
437
438	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
439	if (ACPI_FAILURE(rv)) {
440		aprint_error("%s: unable to initialize ACPI objects: %s\n",
441		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
442		return;
443	}
444	acpi_active = 1;
445
446	/* Our current state is "awake". */
447	sc->sc_sleepstate = ACPI_STATE_S0;
448
449	/* Show SCI interrupt. */
450	aprint_verbose("%s: SCI interrupting at int %d\n",
451	    sc->sc_dev.dv_xname, AcpiGbl_FADT.SciInterrupt);
452
453	/*
454	 * Check for fixed-hardware features.
455	 */
456	acpi_enable_fixed_events(sc);
457	acpitimer_init();
458
459	/*
460	 * Scan the namespace and build our device tree.
461	 */
462#ifdef ACPI_DEBUGGER
463	if (acpi_dbgr & ACPI_DBGR_PROBE)
464		acpi_osd_debugger();
465#endif
466	acpi_build_tree(sc);
467
468	if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
469		(void)sysctl_createv(NULL, 0, NULL, NULL,
470		    CTLFLAG_IMMEDIATE,
471		    CTLTYPE_QUAD, "root", NULL, NULL,
472		    acpi_root_pointer, NULL, 0,
473		    CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
474	}
475
476
477	/*
478	 * Register a shutdown hook that disables certain ACPI
479	 * events that might happen and confuse us while we're
480	 * trying to shut down.
481	 */
482	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
483	if (sc->sc_sdhook == NULL)
484		aprint_error("%s: WARNING: unable to register shutdown hook\n",
485		    sc->sc_dev.dv_xname);
486
487#ifdef ACPI_DEBUGGER
488	if (acpi_dbgr & ACPI_DBGR_RUNNING)
489		acpi_osd_debugger();
490#endif
491}
492
493/*
494 * acpi_shutdown:
495 *
496 *	Shutdown hook for ACPI -- disable some events that
497 *	might confuse us.
498 */
499static void
500acpi_shutdown(void *arg)
501{
502	/* nothing */
503}
504
505#if 0
506/*
507 * acpi_disable:
508 *
509 *	Disable ACPI.
510 */
511static ACPI_STATUS
512acpi_disable(struct acpi_softc *sc)
513{
514	ACPI_STATUS rv = AE_OK;
515
516	if (acpi_active) {
517		rv = AcpiDisable();
518		if (ACPI_SUCCESS(rv))
519			acpi_active = 0;
520	}
521	return rv;
522}
523#endif
524
525struct acpi_make_devnode_state {
526	struct acpi_softc *softc;
527	struct acpi_scope *scope;
528};
529
530/*
531 * acpi_build_tree:
532 *
533 *	Scan relevant portions of the ACPI namespace and attach
534 *	child devices.
535 */
536static void
537acpi_build_tree(struct acpi_softc *sc)
538{
539	static const char *scopes[] = {
540		"\\_PR_",	/* ACPI 1.0 processor namespace */
541		"\\_SB_",	/* system bus namespace */
542		"\\_SI_",	/* system indicator namespace */
543		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
544		NULL,
545	};
546	struct acpi_attach_args aa;
547	struct acpi_make_devnode_state state;
548	struct acpi_scope *as;
549	struct acpi_devnode *ad;
550	ACPI_HANDLE parent;
551	ACPI_STATUS rv;
552	int i;
553
554	TAILQ_INIT(&sc->sc_scopes);
555
556	state.softc = sc;
557
558	/*
559	 * Scan the namespace and build our tree.
560	 */
561	for (i = 0; scopes[i] != NULL; i++) {
562		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
563		as->as_name = scopes[i];
564		TAILQ_INIT(&as->as_devnodes);
565
566		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
567
568		state.scope = as;
569
570		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
571		    &parent);
572		if (ACPI_SUCCESS(rv)) {
573			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
574			    acpi_make_devnode, &state, NULL);
575		}
576
577		/* Now, for this namespace, try and attach the devices. */
578		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
579			aa.aa_node = ad;
580			aa.aa_iot = sc->sc_iot;
581			aa.aa_memt = sc->sc_memt;
582			aa.aa_pc = sc->sc_pc;
583			aa.aa_pciflags = sc->sc_pciflags;
584			aa.aa_ic = sc->sc_ic;
585
586			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
587				/*
588				 * XXX We only attach devices which are:
589				 *
590				 *	- present
591				 *	- enabled
592				 *	- functioning properly
593				 *
594				 * However, if enabled, it's decoding resources,
595				 * so we should claim them, if possible.
596				 * Requires changes to bus_space(9).
597				 */
598				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
599				    ACPI_VALID_STA &&
600				    (ad->ad_devinfo->CurrentStatus &
601				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
602				      ACPI_STA_DEV_OK)) !=
603				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
604				     ACPI_STA_DEV_OK))
605					continue;
606			}
607
608			/*
609			 * XXX Same problem as above...
610			 *
611			 * Do this check only for devices, as e.g.
612			 * a Thermal Zone doesn't have a HID.
613			 */
614			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE &&
615			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
616				continue;
617
618			ad->ad_device = config_found_ia(&sc->sc_dev,
619			    "acpinodebus", &aa, acpi_print);
620		}
621	}
622	config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
623}
624
625#ifdef ACPI_ACTIVATE_DEV
626static void
627acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
628{
629	ACPI_STATUS rv;
630	ACPI_BUFFER buf;
631
632	buf.Pointer = NULL;
633	buf.Length = ACPI_ALLOCATE_BUFFER;
634
635#ifdef ACPI_DEBUG
636	aprint_normal("acpi_activate_device: %s, old status=%x\n",
637	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
638#endif
639
640	rv = acpi_allocate_resources(handle);
641	if (ACPI_FAILURE(rv)) {
642		aprint_error("acpi: activate failed for %s\n",
643		       (*di)->HardwareId.Value);
644	} else {
645		aprint_verbose("acpi: activated %s\n",
646		    (*di)->HardwareId.Value);
647	}
648
649	(void)AcpiGetObjectInfo(handle, &buf);
650	AcpiOsFree(*di);
651	*di = buf.Pointer;
652
653#ifdef ACPI_DEBUG
654	aprint_normal("acpi_activate_device: %s, new status=%x\n",
655	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
656#endif
657}
658#endif /* ACPI_ACTIVATE_DEV */
659
660/*
661 * acpi_make_devnode:
662 *
663 *	Make an ACPI devnode.
664 */
665static ACPI_STATUS
666acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
667    void **status)
668{
669	struct acpi_make_devnode_state *state = context;
670#if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
671	struct acpi_softc *sc = state->softc;
672#endif
673	struct acpi_scope *as = state->scope;
674	struct acpi_devnode *ad;
675	ACPI_OBJECT_TYPE type;
676	ACPI_BUFFER buf;
677	ACPI_DEVICE_INFO *devinfo;
678	ACPI_STATUS rv;
679	ACPI_NAME_UNION *anu;
680	int i, clear = 0;
681
682	rv = AcpiGetType(handle, &type);
683	if (ACPI_SUCCESS(rv)) {
684		buf.Pointer = NULL;
685		buf.Length = ACPI_ALLOCATE_BUFFER;
686		rv = AcpiGetObjectInfo(handle, &buf);
687		if (ACPI_FAILURE(rv)) {
688#ifdef ACPI_DEBUG
689			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
690			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
691#endif
692			goto out; /* XXX why return OK */
693		}
694
695		devinfo = buf.Pointer;
696
697		switch (type) {
698		case ACPI_TYPE_DEVICE:
699#ifdef ACPI_ACTIVATE_DEV
700			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
701			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
702			    (devinfo->CurrentStatus &
703			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
704			    ACPI_STA_DEV_PRESENT)
705				acpi_activate_device(handle, &devinfo);
706
707			/* FALLTHROUGH */
708#endif
709
710		case ACPI_TYPE_PROCESSOR:
711		case ACPI_TYPE_THERMAL:
712		case ACPI_TYPE_POWER:
713			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
714			if (ad == NULL)
715				return AE_NO_MEMORY;
716
717			ad->ad_devinfo = devinfo;
718			ad->ad_handle = handle;
719			ad->ad_level = level;
720			ad->ad_scope = as;
721			ad->ad_type = type;
722
723			anu = (ACPI_NAME_UNION *)&devinfo->Name;
724			ad->ad_name[4] = '\0';
725			for (i = 3, clear = 0; i >= 0; i--) {
726				if (!clear && anu->Ascii[i] == '_')
727					ad->ad_name[i] = '\0';
728				else {
729					ad->ad_name[i] = anu->Ascii[i];
730					clear = 1;
731				}
732			}
733			if (ad->ad_name[0] == '\0')
734				ad->ad_name[0] = '_';
735
736			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
737
738			if (type == ACPI_TYPE_DEVICE &&
739			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
740				goto out;
741
742#ifdef ACPI_EXTRA_DEBUG
743			aprint_normal("%s: HID %s found in scope %s level %d\n",
744			    sc->sc_dev.dv_xname,
745			    ad->ad_devinfo->HardwareId.Value,
746			    as->as_name, ad->ad_level);
747			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
748				aprint_normal("       UID %s\n",
749				    ad->ad_devinfo->UniqueId.Value);
750			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
751				aprint_normal("       ADR 0x%016qx\n",
752				    ad->ad_devinfo->Address);
753			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
754				aprint_normal("       STA 0x%08x\n",
755				    ad->ad_devinfo->CurrentStatus);
756#endif
757		}
758	}
759 out:
760	return AE_OK;
761}
762
763/*
764 * acpi_print:
765 *
766 *	Autoconfiguration print routine for ACPI node bus.
767 */
768static int
769acpi_print(void *aux, const char *pnp)
770{
771	struct acpi_attach_args *aa = aux;
772	ACPI_STATUS rv;
773
774	if (pnp) {
775		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
776			char *pnpstr =
777			    aa->aa_node->ad_devinfo->HardwareId.Value;
778			char *str;
779
780			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
781			    pnpstr);
782			rv = acpi_eval_string(aa->aa_node->ad_handle,
783			    "_STR", &str);
784			if (ACPI_SUCCESS(rv)) {
785				aprint_normal("[%s] ", str);
786				AcpiOsFree(str);
787			}
788#ifdef ACPIVERBOSE
789			else {
790				int i;
791
792				for (i = 0; i < sizeof(acpi_knowndevs) /
793				    sizeof(acpi_knowndevs[0]); i++) {
794					if (strcmp(acpi_knowndevs[i].pnp,
795					    pnpstr) == 0) {
796						aprint_normal("[%s] ",
797						    acpi_knowndevs[i].str);
798					}
799				}
800			}
801
802#endif
803			aprint_normal("at %s", pnp);
804		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
805			aprint_normal("%s (ACPI Object Type '%s' "
806			    "[0x%02x]) ", aa->aa_node->ad_name,
807			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
808			     aa->aa_node->ad_devinfo->Type);
809			aprint_normal("at %s", pnp);
810		} else
811			return 0;
812	} else {
813		aprint_normal(" (%s", aa->aa_node->ad_name);
814		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
815			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
816			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
817				const char *uid;
818
819				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
820				if (uid[0] == '\0')
821					uid = "<null>";
822				aprint_normal("-%s", uid);
823			}
824		}
825		aprint_normal(")");
826	}
827
828	return UNCONF;
829}
830
831/*****************************************************************************
832 * ACPI fixed-hardware feature handlers
833 *****************************************************************************/
834
835static UINT32	acpi_fixed_button_handler(void *);
836static void	acpi_fixed_button_pressed(void *);
837
838/*
839 * acpi_enable_fixed_events:
840 *
841 *	Enable any fixed-hardware feature handlers.
842 */
843static void
844acpi_enable_fixed_events(struct acpi_softc *sc)
845{
846	static int beenhere;
847	ACPI_STATUS rv;
848
849	KASSERT(beenhere == 0);
850	beenhere = 1;
851
852	/*
853	 * Check for fixed-hardware buttons.
854	 */
855
856	if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
857		aprint_verbose("%s: fixed-feature power button present\n",
858		    sc->sc_dev.dv_xname);
859		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
860		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
861		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
862			aprint_error("%s: unable to register fixed power "
863			    "button with sysmon\n", sc->sc_dev.dv_xname);
864		} else {
865			rv = AcpiInstallFixedEventHandler(
866			    ACPI_EVENT_POWER_BUTTON,
867			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
868			if (ACPI_FAILURE(rv)) {
869				aprint_error("%s: unable to install handler "
870				    "for fixed power button: %s\n",
871				    sc->sc_dev.dv_xname,
872				    AcpiFormatException(rv));
873			}
874		}
875	}
876
877	if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
878		aprint_verbose("%s: fixed-feature sleep button present\n",
879		    sc->sc_dev.dv_xname);
880		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
881		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
882		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
883			aprint_error("%s: unable to register fixed sleep "
884			    "button with sysmon\n", sc->sc_dev.dv_xname);
885		} else {
886			rv = AcpiInstallFixedEventHandler(
887			    ACPI_EVENT_SLEEP_BUTTON,
888			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
889			if (ACPI_FAILURE(rv)) {
890				aprint_error("%s: unable to install handler "
891				    "for fixed sleep button: %s\n",
892				    sc->sc_dev.dv_xname,
893				    AcpiFormatException(rv));
894			}
895		}
896	}
897}
898
899/*
900 * acpi_fixed_button_handler:
901 *
902 *	Event handler for the fixed buttons.
903 */
904static UINT32
905acpi_fixed_button_handler(void *context)
906{
907	struct sysmon_pswitch *smpsw = context;
908	int rv;
909
910#ifdef ACPI_BUT_DEBUG
911	printf("%s: fixed button handler\n", smpsw->smpsw_name);
912#endif
913
914	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
915	    acpi_fixed_button_pressed, smpsw);
916	if (ACPI_FAILURE(rv))
917		printf("%s: WARNING: unable to queue fixed button pressed "
918		    "callback: %s\n", smpsw->smpsw_name,
919		    AcpiFormatException(rv));
920
921	return ACPI_INTERRUPT_HANDLED;
922}
923
924/*
925 * acpi_fixed_button_pressed:
926 *
927 *	Deal with a fixed button being pressed.
928 */
929static void
930acpi_fixed_button_pressed(void *context)
931{
932	struct sysmon_pswitch *smpsw = context;
933
934#ifdef ACPI_BUT_DEBUG
935	printf("%s: fixed button pressed, calling sysmon\n",
936	    smpsw->smpsw_name);
937#endif
938
939	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
940}
941
942/*****************************************************************************
943 * ACPI utility routines.
944 *****************************************************************************/
945
946/*
947 * acpi_eval_integer:
948 *
949 *	Evaluate an integer object.
950 */
951ACPI_STATUS
952acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
953{
954	ACPI_STATUS rv;
955	ACPI_BUFFER buf;
956	ACPI_OBJECT param;
957
958	if (handle == NULL)
959		handle = ACPI_ROOT_OBJECT;
960
961	buf.Pointer = &param;
962	buf.Length = sizeof(param);
963
964	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
965	if (ACPI_SUCCESS(rv))
966		*valp = param.Integer.Value;
967
968	return rv;
969}
970
971/*
972 * acpi_eval_string:
973 *
974 *	Evaluate a (Unicode) string object.
975 */
976ACPI_STATUS
977acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
978{
979	ACPI_STATUS rv;
980	ACPI_BUFFER buf;
981
982	if (handle == NULL)
983		handle = ACPI_ROOT_OBJECT;
984
985	buf.Pointer = NULL;
986	buf.Length = ACPI_ALLOCATE_BUFFER;
987
988	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
989	if (ACPI_SUCCESS(rv)) {
990		ACPI_OBJECT *param = buf.Pointer;
991		const char *ptr = param->String.Pointer;
992		size_t len = param->String.Length;
993		if ((*stringp = AcpiOsAllocate(len)) == NULL)
994			rv = AE_NO_MEMORY;
995		else
996			(void)memcpy(*stringp, ptr, len);
997		AcpiOsFree(param);
998	}
999
1000	return rv;
1001}
1002
1003
1004/*
1005 * acpi_eval_struct:
1006 *
1007 *	Evaluate a more complex structure.
1008 *	Caller must free buf.Pointer by AcpiOsFree().
1009 */
1010ACPI_STATUS
1011acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
1012{
1013	ACPI_STATUS rv;
1014
1015	if (handle == NULL)
1016		handle = ACPI_ROOT_OBJECT;
1017
1018	bufp->Pointer = NULL;
1019	bufp->Length = ACPI_ALLOCATE_BUFFER;
1020
1021	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
1022
1023	return rv;
1024}
1025
1026/*
1027 * acpi_foreach_package_object:
1028 *
1029 *	Iterate over all objects in a in a packages and pass then all
1030 *	to a function. If the called function returns non AE_OK, the
1031 *	iteration is stopped and that value is returned.
1032 */
1033
1034ACPI_STATUS
1035acpi_foreach_package_object(ACPI_OBJECT *pkg,
1036    ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
1037    void *arg)
1038{
1039	ACPI_STATUS rv = AE_OK;
1040	int i;
1041
1042	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1043		return AE_BAD_PARAMETER;
1044
1045	for (i = 0; i < pkg->Package.Count; i++) {
1046		rv = (*func)(&pkg->Package.Elements[i], arg);
1047		if (ACPI_FAILURE(rv))
1048			break;
1049	}
1050
1051	return rv;
1052}
1053
1054const char *
1055acpi_name(ACPI_HANDLE handle)
1056{
1057	static char buffer[80];
1058	ACPI_BUFFER buf;
1059	ACPI_STATUS rv;
1060
1061	buf.Length = sizeof(buffer);
1062	buf.Pointer = buffer;
1063
1064	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
1065	if (ACPI_FAILURE(rv))
1066		return "(unknown acpi path)";
1067	return buffer;
1068}
1069
1070/*
1071 * acpi_get:
1072 *
1073 *	Fetch data info the specified (empty) ACPI buffer.
1074 *	Caller must free buf.Pointer by AcpiOsFree().
1075 */
1076ACPI_STATUS
1077acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
1078    ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
1079{
1080	buf->Pointer = NULL;
1081	buf->Length = ACPI_ALLOCATE_BUFFER;
1082
1083	return (*getit)(handle, buf);
1084}
1085
1086
1087/*
1088 * acpi_match_hid
1089 *
1090 *	Match given ids against _HID and _CIDs
1091 */
1092int
1093acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
1094{
1095	int i;
1096
1097	while (*ids) {
1098		if (ad->Valid & ACPI_VALID_HID) {
1099			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
1100				return 1;
1101		}
1102
1103		if (ad->Valid & ACPI_VALID_CID) {
1104			for (i = 0; i < ad->CompatibilityId.Count; i++) {
1105				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
1106					return 1;
1107			}
1108		}
1109		ids++;
1110	}
1111
1112	return 0;
1113}
1114
1115/*
1116 * acpi_set_wake_gpe
1117 *
1118 *	Set GPE as both Runtime and Wake
1119 */
1120void
1121acpi_set_wake_gpe(ACPI_HANDLE handle)
1122{
1123	ACPI_BUFFER buf;
1124	ACPI_STATUS rv;
1125	ACPI_OBJECT *p, *elt;
1126
1127	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
1128	if (ACPI_FAILURE(rv))
1129		return;			/* just ignore */
1130
1131	p = buf.Pointer;
1132	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
1133		goto out;		/* just ignore */
1134
1135	elt = p->Package.Elements;
1136
1137	/* TBD: package support */
1138	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
1139	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
1140
1141 out:
1142	AcpiOsFree(buf.Pointer);
1143}
1144
1145
1146/*****************************************************************************
1147 * ACPI sleep support.
1148 *****************************************************************************/
1149
1150static int
1151is_available_state(struct acpi_softc *sc, int state)
1152{
1153	UINT8 type_a, type_b;
1154
1155	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
1156				&type_a, &type_b));
1157}
1158
1159/*
1160 * acpi_enter_sleep_state:
1161 *
1162 *	enter to the specified sleep state.
1163 */
1164
1165ACPI_STATUS
1166acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1167{
1168	int err;
1169	ACPI_STATUS ret = AE_OK;
1170
1171	if (state == acpi_sleepstate)
1172		return AE_OK;
1173
1174	aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
1175
1176	switch (state) {
1177	case ACPI_STATE_S0:
1178		break;
1179	case ACPI_STATE_S1:
1180	case ACPI_STATE_S2:
1181	case ACPI_STATE_S3:
1182	case ACPI_STATE_S4:
1183		if (!is_available_state(sc, state)) {
1184			aprint_error("%s: cannot enter the sleep state (%d)\n",
1185			    sc->sc_dev.dv_xname, state);
1186			break;
1187		}
1188
1189		if (state != ACPI_STATE_S1 && !pmf_system_suspend()) {
1190			aprint_error_dev(&sc->sc_dev, "aborting suspend\n");
1191			break;
1192		}
1193
1194		ret = AcpiEnterSleepStatePrep(state);
1195		if (ACPI_FAILURE(ret)) {
1196			aprint_error("%s: failed preparing to sleep (%s)\n",
1197			    sc->sc_dev.dv_xname, AcpiFormatException(ret));
1198			break;
1199		}
1200
1201		acpi_sleepstate = state;
1202		if (state == ACPI_STATE_S1) {
1203			/* just enter the state */
1204			acpi_md_OsDisableInterrupt();
1205			AcpiEnterSleepState((UINT8)state);
1206			AcpiLeaveSleepState((UINT8)state);
1207		} else {
1208			err = acpi_md_sleep(state);
1209			if (state == ACPI_STATE_S4)
1210				AcpiEnable();
1211			pmf_system_bus_resume();
1212			AcpiLeaveSleepState((UINT8)state);
1213			pmf_system_resume();
1214		}
1215
1216		break;
1217	case ACPI_STATE_S5:
1218		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1219		if (ACPI_FAILURE(ret)) {
1220			aprint_error("%s: failed preparing to sleep (%s)\n",
1221			       sc->sc_dev.dv_xname, AcpiFormatException(ret));
1222			break;
1223		}
1224		DELAY(1000000);
1225		acpi_sleepstate = state;
1226		acpi_md_OsDisableInterrupt();
1227		AcpiEnterSleepState(ACPI_STATE_S5);
1228		aprint_error("%s: WARNING powerdown failed!\n",
1229		    sc->sc_dev.dv_xname);
1230		break;
1231	}
1232
1233	acpi_sleepstate = ACPI_STATE_S0;
1234	return ret;
1235}
1236
1237#if defined(ACPI_ACTIVATE_DEV)
1238/* XXX This very incomplete */
1239ACPI_STATUS
1240acpi_allocate_resources(ACPI_HANDLE handle)
1241{
1242	ACPI_BUFFER bufp, bufc, bufn;
1243	ACPI_RESOURCE *resp, *resc, *resn;
1244	ACPI_RESOURCE_IRQ *irq;
1245	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1246	ACPI_STATUS rv;
1247	uint delta;
1248
1249	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1250	if (ACPI_FAILURE(rv))
1251		goto out;
1252	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1253	if (ACPI_FAILURE(rv)) {
1254		goto out1;
1255	}
1256
1257	bufn.Length = 1000;
1258	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1259	resp = bufp.Pointer;
1260	resc = bufc.Pointer;
1261	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
1262	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1263		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
1264			resp = ACPI_NEXT_RESOURCE(resp);
1265		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
1266			break;
1267		/* Found identical Id */
1268		resn->Type = resc->Type;
1269		switch (resc->Type) {
1270		case ACPI_RESOURCE_TYPE_IRQ:
1271			memcpy(&resn->Data, &resp->Data,
1272			       sizeof(ACPI_RESOURCE_IRQ));
1273			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1274			irq->Interrupts[0] =
1275			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1276			        Interrupts[irq->InterruptCount-1];
1277			irq->InterruptCount = 1;
1278			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
1279			break;
1280		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1281			memcpy(&resn->Data, &resp->Data,
1282			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
1283			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
1284#if 0
1285			/*
1286			 * XXX not duplicating the interrupt logic above
1287			 * because its not clear what it accomplishes.
1288			 */
1289			xirq->Interrupts[0] =
1290			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
1291			    Interrupts[irq->NumberOfInterrupts-1];
1292			xirq->NumberOfInterrupts = 1;
1293#endif
1294			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
1295			break;
1296		case ACPI_RESOURCE_TYPE_IO:
1297			memcpy(&resn->Data, &resp->Data,
1298			       sizeof(ACPI_RESOURCE_IO));
1299			resn->Length = resp->Length;
1300			break;
1301		default:
1302			printf("acpi_allocate_resources: res=%d\n", resc->Type);
1303			rv = AE_BAD_DATA;
1304			goto out2;
1305		}
1306		resc = ACPI_NEXT_RESOURCE(resc);
1307		resn = ACPI_NEXT_RESOURCE(resn);
1308		resp = ACPI_NEXT_RESOURCE(resp);
1309		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1310		if (delta >=
1311		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
1312			bufn.Length *= 2;
1313			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1314					       M_ACPI, M_WAITOK);
1315			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1316		}
1317	}
1318	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1319		printf("acpi_allocate_resources: resc not exhausted\n");
1320		rv = AE_BAD_DATA;
1321		goto out3;
1322	}
1323
1324	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
1325	rv = AcpiSetCurrentResources(handle, &bufn);
1326	if (ACPI_FAILURE(rv)) {
1327		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1328		       AcpiFormatException(rv));
1329	}
1330
1331out3:
1332	free(bufn.Pointer, M_ACPI);
1333out2:
1334	AcpiOsFree(bufc.Pointer);
1335out1:
1336	AcpiOsFree(bufp.Pointer);
1337out:
1338	return rv;
1339}
1340#endif /* ACPI_ACTIVATE_DEV */
1341
1342SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1343{
1344	const struct sysctlnode *node;
1345	const struct sysctlnode *ssnode;
1346
1347	if (sysctl_createv(clog, 0, NULL, NULL,
1348	    CTLFLAG_PERMANENT,
1349	    CTLTYPE_NODE, "hw", NULL,
1350	    NULL, 0, NULL, 0,
1351	    CTL_HW, CTL_EOL) != 0)
1352		return;
1353
1354	if (sysctl_createv(clog, 0, NULL, &node,
1355	    CTLFLAG_PERMANENT,
1356	    CTLTYPE_NODE, "acpi", NULL,
1357	    NULL, 0, NULL, 0,
1358	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
1359		return;
1360
1361	acpi_node = node->sysctl_num;
1362
1363	/* ACPI sleepstate sysctl */
1364	if (sysctl_createv(NULL, 0, NULL, &node,
1365	    CTLFLAG_PERMANENT,
1366	    CTLTYPE_NODE, "machdep", NULL,
1367	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
1368		return;
1369	if (sysctl_createv(NULL, 0, &node, &ssnode,
1370	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
1371	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
1372	    CTL_EOL) != 0)
1373		return;
1374}
1375
1376static int
1377sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1378{
1379	int error, t;
1380	struct sysctlnode node;
1381
1382	node = *rnode;
1383	t = acpi_sleepstate;
1384	node.sysctl_data = &t;
1385	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1386	if (error || newp == NULL)
1387		return error;
1388
1389	if (acpi_softc == NULL)
1390		return ENOSYS;
1391
1392	acpi_enter_sleep_state(acpi_softc, t);
1393
1394	return 0;
1395}
1396