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