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