acpi.c revision 1.56
1/*	$NetBSD: acpi.c,v 1.56 2003/11/03 18:07:10 mycroft Exp $	*/
2
3/*-
4 * Copyright (c) 2003 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.56 2003/11/03 18:07:10 mycroft Exp $");
81
82#include "opt_acpi.h"
83
84#include <sys/param.h>
85#include <sys/systm.h>
86#include <sys/device.h>
87#include <sys/malloc.h>
88#include <sys/kernel.h>
89#include <sys/proc.h>
90
91#include <dev/acpi/acpica.h>
92#include <dev/acpi/acpireg.h>
93#include <dev/acpi/acpivar.h>
94#include <dev/acpi/acpi_osd.h>
95#ifdef ACPIVERBOSE
96#include <dev/acpi/acpidevs_data.h>
97#endif
98
99#ifdef ACPI_PCI_FIXUP
100#include <dev/acpi/acpica/Subsystem/acnamesp.h> /* AcpiNsGetNodeByPath() */
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
115int	acpi_dbgr = 0x00;
116#endif
117
118int	acpi_match(struct device *, struct cfdata *, void *);
119void	acpi_attach(struct device *, struct device *, void *);
120
121int	acpi_print(void *aux, const char *);
122
123extern struct cfdriver acpi_cd;
124
125CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
126    acpi_match, acpi_attach, NULL, NULL);
127
128/*
129 * This is a flag we set when the ACPI subsystem is active.  Machine
130 * dependent code may wish to skip other steps (such as attaching
131 * subsystems that ACPI supercedes) when ACPI is active.
132 */
133int	acpi_active;
134
135/*
136 * Pointer to the ACPI subsystem's state.  There can be only
137 * one ACPI instance.
138 */
139struct acpi_softc *acpi_softc;
140
141/*
142 * Locking stuff.
143 */
144static struct simplelock acpi_slock;
145static int acpi_locked;
146
147/*
148 * Prototypes.
149 */
150void		acpi_shutdown(void *);
151ACPI_STATUS	acpi_disable(struct acpi_softc *sc);
152void		acpi_build_tree(struct acpi_softc *);
153ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
154
155void		acpi_enable_fixed_events(struct acpi_softc *);
156#ifdef ACPI_PCI_FIXUP
157void		acpi_pci_fixup(struct acpi_softc *);
158#endif
159#if defined(ACPI_PCI_FIXUP) || defined(ACPI_ACTIVATE_DEV)
160ACPI_STATUS	acpi_allocate_resources(ACPI_HANDLE handle);
161#endif
162
163/*
164 * acpi_probe:
165 *
166 *	Probe for ACPI support.  This is called by the
167 *	machine-dependent ACPI front-end.  All of the
168 *	actual work is done by ACPICA.
169 *
170 *	NOTE: This is not an autoconfiguration interface function.
171 */
172int
173acpi_probe(void)
174{
175	static int beenhere;
176	ACPI_STATUS rv;
177
178	if (beenhere != 0)
179		panic("acpi_probe: ACPI has already been probed");
180	beenhere = 1;
181
182	simple_lock_init(&acpi_slock);
183	acpi_locked = 0;
184
185	/*
186	 * Start up ACPICA.
187	 */
188#ifdef ACPI_DEBUGGER
189	if (acpi_dbgr & ACPI_DBGR_INIT)
190		acpi_osd_debugger();
191#endif
192
193	rv = AcpiInitializeSubsystem();
194	if (ACPI_FAILURE(rv)) {
195		printf("ACPI: unable to initialize ACPICA: %s\n",
196		    AcpiFormatException(rv));
197		return (0);
198	}
199
200#ifdef ACPI_DEBUGGER
201	if (acpi_dbgr & ACPI_DBGR_TABLES)
202		acpi_osd_debugger();
203#endif
204
205	rv = AcpiLoadTables();
206	if (ACPI_FAILURE(rv)) {
207		printf("ACPI: unable to load tables: %s\n",
208		    AcpiFormatException(rv));
209		return (0);
210	}
211
212	/*
213	 * Looks like we have ACPI!
214	 */
215
216	return (1);
217}
218
219/*
220 * acpi_match:
221 *
222 *	Autoconfiguration `match' routine.
223 */
224int
225acpi_match(struct device *parent, struct cfdata *match, void *aux)
226{
227	struct acpibus_attach_args *aa = aux;
228
229	if (strcmp(aa->aa_busname, acpi_cd.cd_name) != 0)
230		return (0);
231
232	/*
233	 * XXX Check other locators?  Hard to know -- machine
234	 * dependent code has already checked for the presence
235	 * of ACPI by calling acpi_probe(), so I suppose we
236	 * don't really have to do anything else.
237	 */
238	return (1);
239}
240
241/*
242 * acpi_attach:
243 *
244 *	Autoconfiguration `attach' routine.  Finish initializing
245 *	ACPICA (some initialization was done in acpi_probe(),
246 *	which was required to check for the presence of ACPI),
247 *	and enable the ACPI subsystem.
248 */
249void
250acpi_attach(struct device *parent, struct device *self, void *aux)
251{
252	struct acpi_softc *sc = (void *) self;
253	struct acpibus_attach_args *aa = aux;
254	ACPI_STATUS rv;
255
256	printf("\n");
257
258	if (acpi_softc != NULL)
259		panic("acpi_attach: ACPI has already been attached");
260
261	sysmon_power_settype("acpi");
262
263	printf("%s: using Intel ACPI CA subsystem version %08x\n",
264	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
265
266	printf("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
267	    sc->sc_dev.dv_xname,
268	    AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
269	    AcpiGbl_XSDT->OemRevision,
270	    AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
271
272	sc->sc_quirks = acpi_find_quirks();
273
274	sc->sc_iot = aa->aa_iot;
275	sc->sc_memt = aa->aa_memt;
276	sc->sc_pc = aa->aa_pc;
277	sc->sc_pciflags = aa->aa_pciflags;
278	sc->sc_ic = aa->aa_ic;
279
280	acpi_softc = sc;
281
282	/*
283	 * Bring ACPI on-line.
284	 */
285#ifdef ACPI_DEBUGGER
286	if (acpi_dbgr & ACPI_DBGR_ENABLE)
287		acpi_osd_debugger();
288#endif
289
290	rv = AcpiEnableSubsystem(0);
291	if (ACPI_FAILURE(rv)) {
292		printf("%s: unable to enable ACPI: %s\n",
293		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
294		return;
295	}
296	rv = AcpiInitializeObjects(0);
297	if (ACPI_FAILURE(rv)) {
298		printf("%s: unable to initialize ACPI objects: %s\n",
299		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
300		return;
301	}
302	acpi_active = 1;
303
304	/* Our current state is "awake". */
305	sc->sc_sleepstate = ACPI_STATE_S0;
306
307	/* Show SCI interrupt. */
308	if (AcpiGbl_FADT != NULL)
309		printf("%s: SCI interrupting at int %d\n",
310			sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
311	/*
312	 * Check for fixed-hardware features.
313	 */
314	acpi_enable_fixed_events(sc);
315
316	/*
317	 * Fix up PCI devices.
318	 */
319#ifdef ACPI_PCI_FIXUP
320	if ((sc->sc_quirks & (ACPI_QUIRK_BADPCI | ACPI_QUIRK_BADIRQ)) == 0)
321		acpi_pci_fixup(sc);
322#endif
323
324	/*
325	 * Scan the namespace and build our device tree.
326	 */
327#ifdef ACPI_DEBUGGER
328	if (acpi_dbgr & ACPI_DBGR_PROBE)
329		acpi_osd_debugger();
330#endif
331	acpi_md_callback((struct device *)sc);
332	acpi_build_tree(sc);
333
334	/*
335	 * Register a shutdown hook that disables certain ACPI
336	 * events that might happen and confuse us while we're
337	 * trying to shut down.
338	 */
339	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
340	if (sc->sc_sdhook == NULL)
341		printf("%s: WARNING: unable to register shutdown hook\n",
342		    sc->sc_dev.dv_xname);
343
344#ifdef ACPI_DEBUGGER
345	if (acpi_dbgr & ACPI_DBGR_RUNNING)
346		acpi_osd_debugger();
347#endif
348}
349
350/*
351 * acpi_shutdown:
352 *
353 *	Shutdown hook for ACPI -- disable some events that
354 *	might confuse us.
355 */
356void
357acpi_shutdown(void *arg)
358{
359	struct acpi_softc *sc = arg;
360	ACPI_STATUS rv;
361
362	rv = acpi_disable(sc);
363	if (ACPI_FAILURE(rv))
364		printf("%s: WARNING: unable to disable ACPI: %s\n",
365		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
366}
367
368/*
369 * acpi_disable:
370 *
371 *	Disable ACPI.
372 */
373ACPI_STATUS
374acpi_disable(struct acpi_softc *sc)
375{
376	ACPI_STATUS rv = AE_OK;
377
378	if (acpi_active) {
379		rv = AcpiDisable();
380		if (ACPI_SUCCESS(rv))
381			acpi_active = 0;
382	}
383	return (rv);
384}
385
386struct acpi_make_devnode_state {
387	struct acpi_softc *softc;
388	struct acpi_scope *scope;
389};
390
391/*
392 * acpi_build_tree:
393 *
394 *	Scan relevant portions of the ACPI namespace and attach
395 *	child devices.
396 */
397void
398acpi_build_tree(struct acpi_softc *sc)
399{
400	static const char *scopes[] = {
401		"\\_PR_",	/* ACPI 1.0 processor namespace */
402		"\\_SB_",	/* system bus namespace */
403		"\\_SI_",	/* system idicator namespace */
404		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
405		NULL,
406	};
407	struct acpi_attach_args aa;
408	struct acpi_make_devnode_state state;
409	struct acpi_scope *as;
410	struct acpi_devnode *ad;
411	ACPI_HANDLE parent;
412	ACPI_STATUS rv;
413	int i;
414
415	TAILQ_INIT(&sc->sc_scopes);
416
417	state.softc = sc;
418
419	/*
420	 * Scan the namespace and build our tree.
421	 */
422	for (i = 0; scopes[i] != NULL; i++) {
423		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
424		as->as_name = scopes[i];
425		TAILQ_INIT(&as->as_devnodes);
426
427		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
428
429		state.scope = as;
430
431		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, (char *) scopes[i],
432		    &parent);
433		if (ACPI_SUCCESS(rv)) {
434			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
435			    acpi_make_devnode, &state, NULL);
436		}
437
438		/* Now, for this namespace, try and attach the devices. */
439		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
440			aa.aa_node = ad;
441			aa.aa_iot = sc->sc_iot;
442			aa.aa_memt = sc->sc_memt;
443			aa.aa_pc = sc->sc_pc;
444			aa.aa_pciflags = sc->sc_pciflags;
445			aa.aa_ic = sc->sc_ic;
446
447			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
448				/*
449				 * XXX We only attach devices which are:
450				 *
451				 *	- present
452				 *	- enabled
453				 *	- functioning properly
454				 *
455				 * However, if enabled, it's decoding resources,
456				 * so we should claim them, if possible.
457				 * Requires changes to bus_space(9).
458				 */
459				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
460				    ACPI_VALID_STA &&
461				    (ad->ad_devinfo->CurrentStatus &
462				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
463				      ACPI_STA_DEV_OK)) !=
464				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
465				     ACPI_STA_DEV_OK))
466					continue;
467
468				/*
469				 * XXX Same problem as above...
470				 */
471				if ((ad->ad_devinfo->Valid & ACPI_VALID_HID)
472				    == 0)
473					continue;
474			}
475
476			ad->ad_device = config_found(&sc->sc_dev,
477			    &aa, acpi_print);
478		}
479	}
480}
481
482#ifdef ACPI_ACTIVATE_DEV
483static void
484acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
485{
486	ACPI_STATUS rv;
487	ACPI_BUFFER buf;
488
489	buf.Pointer = NULL;
490	buf.Length = ACPI_ALLOCATE_BUFFER;
491
492#ifdef ACPI_DEBUG
493	printf("acpi_activate_device: %s, old status=%x\n",
494	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
495#endif
496
497	rv = acpi_allocate_resources(handle);
498	if (ACPI_FAILURE(rv)) {
499		printf("acpi: activate failed for %s\n",
500		       (*di)->HardwareId.Value);
501	} else {
502		printf("acpi: activated %s\n", (*di)->HardwareId.Value);
503	}
504
505	(void)AcpiGetObjectInfo(handle, &buf);
506	AcpiOsFree(*di);
507	*di = buf.Pointer;
508
509#ifdef ACPI_DEBUG
510	printf("acpi_activate_device: %s, new status=%x\n",
511	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
512#endif
513}
514#endif /* ACPI_ACTIVATE_DEV */
515
516/*
517 * acpi_make_devnode:
518 *
519 *	Make an ACPI devnode.
520 */
521ACPI_STATUS
522acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
523    void **status)
524{
525	struct acpi_make_devnode_state *state = context;
526#if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
527	struct acpi_softc *sc = state->softc;
528#endif
529	struct acpi_scope *as = state->scope;
530	struct acpi_devnode *ad;
531	ACPI_OBJECT_TYPE type;
532	ACPI_BUFFER buf;
533	ACPI_DEVICE_INFO *devinfo;
534	ACPI_STATUS rv;
535
536	rv = AcpiGetType(handle, &type);
537	if (ACPI_SUCCESS(rv)) {
538		buf.Pointer = NULL;
539		buf.Length = ACPI_ALLOCATE_BUFFER;
540		rv = AcpiGetObjectInfo(handle, &buf);
541		if (ACPI_FAILURE(rv)) {
542#ifdef ACPI_DEBUG
543			printf("%s: AcpiGetObjectInfo failed: %s\n",
544			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
545#endif
546			goto out; /* XXX why return OK */
547		}
548
549		devinfo = buf.Pointer;
550
551		switch (type) {
552		case ACPI_TYPE_DEVICE:
553#ifdef ACPI_ACTIVATE_DEV
554			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
555			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
556			    (devinfo->CurrentStatus &
557			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
558			    ACPI_STA_DEV_PRESENT)
559				acpi_activate_device(handle, &devinfo);
560
561			/* FALLTHROUGH */
562#endif
563
564		case ACPI_TYPE_PROCESSOR:
565		case ACPI_TYPE_THERMAL:
566		case ACPI_TYPE_POWER:
567			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
568			if (ad == NULL)
569				return (AE_NO_MEMORY);
570
571			ad->ad_devinfo = devinfo;
572			ad->ad_handle = handle;
573			ad->ad_level = level;
574			ad->ad_scope = as;
575			ad->ad_type = type;
576
577			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
578
579			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
580				goto out;
581
582#ifdef ACPI_EXTRA_DEBUG
583			printf("%s: HID %s found in scope %s level %d\n",
584			    sc->sc_dev.dv_xname,
585			    ad->ad_devinfo->HardwareId.Value,
586			    as->as_name, ad->ad_level);
587			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
588				printf("       UID %s\n",
589				    ad->ad_devinfo->UniqueId.Value);
590			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
591				printf("       ADR 0x%016qx\n",
592				    ad->ad_devinfo->Address);
593			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
594				printf("       STA 0x%08x\n",
595				    ad->ad_devinfo->CurrentStatus);
596#endif
597		}
598	}
599 out:
600	return (AE_OK);
601}
602
603/*
604 * acpi_print:
605 *
606 *	Autoconfiguration print routine.
607 */
608int
609acpi_print(void *aux, const char *pnp)
610{
611	struct acpi_attach_args *aa = aux;
612	ACPI_STATUS rv;
613
614	if (pnp) {
615		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
616			char *pnpstr =
617			    aa->aa_node->ad_devinfo->HardwareId.Value;
618			char *str;
619
620			aprint_normal("%s ", pnpstr);
621			rv = acpi_eval_string(aa->aa_node->ad_handle,
622			    "_STR", &str);
623			if (ACPI_SUCCESS(rv)) {
624				aprint_normal("[%s] ", str);
625				AcpiOsFree(str);
626			}
627#ifdef ACPIVERBOSE
628			else {
629				int i;
630
631				for (i = 0; i < sizeof(acpi_knowndevs) /
632				    sizeof(acpi_knowndevs[0]); i++) {
633					if (strcmp(acpi_knowndevs[i].pnp,
634					    pnpstr) == 0) {
635						printf("[%s] ",
636						    acpi_knowndevs[i].str);
637					}
638				}
639			}
640
641#endif
642		} else {
643			aprint_normal("ACPI Object Type '%s' (0x%02x) ",
644			   AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
645			   aa->aa_node->ad_devinfo->Type);
646		}
647		aprint_normal("at %s", pnp);
648	} else {
649		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
650			aprint_normal(" (%s", aa->aa_node->ad_devinfo->HardwareId.Value);
651			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
652				char *uid;
653
654				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
655				if (uid[0] == '\0')
656					uid = "<null>";
657				aprint_normal("-%s", uid);
658			}
659			aprint_normal(")");
660		}
661	}
662
663	return (UNCONF);
664}
665
666/*****************************************************************************
667 * ACPI fixed-hardware feature handlers
668 *****************************************************************************/
669
670UINT32		acpi_fixed_button_handler(void *);
671void		acpi_fixed_button_pressed(void *);
672
673/*
674 * acpi_enable_fixed_events:
675 *
676 *	Enable any fixed-hardware feature handlers.
677 */
678void
679acpi_enable_fixed_events(struct acpi_softc *sc)
680{
681	static int beenhere;
682	ACPI_STATUS rv;
683
684	KASSERT(beenhere == 0);
685	beenhere = 1;
686
687	/*
688	 * Check for fixed-hardware buttons.
689	 */
690
691	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
692		printf("%s: fixed-feature power button present\n",
693		    sc->sc_dev.dv_xname);
694		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
695		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
696		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
697			printf("%s: unable to register fixed power button "
698			    "with sysmon\n", sc->sc_dev.dv_xname);
699		} else {
700			rv = AcpiInstallFixedEventHandler(
701			    ACPI_EVENT_POWER_BUTTON,
702			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
703			if (ACPI_FAILURE(rv)) {
704				printf("%s: unable to install handler for "
705				    "fixed power button: %s\n",
706				    sc->sc_dev.dv_xname,
707				    AcpiFormatException(rv));
708			}
709		}
710	}
711
712	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
713		printf("%s: fixed-feature sleep button present\n",
714		    sc->sc_dev.dv_xname);
715		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
716		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
717		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
718			printf("%s: unable to register fixed sleep button "
719			    "with sysmon\n", sc->sc_dev.dv_xname);
720		} else {
721			rv = AcpiInstallFixedEventHandler(
722			    ACPI_EVENT_SLEEP_BUTTON,
723			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
724			if (ACPI_FAILURE(rv)) {
725				printf("%s: unable to install handler for "
726				    "fixed sleep button: %s\n",
727				    sc->sc_dev.dv_xname,
728				    AcpiFormatException(rv));
729			}
730		}
731	}
732}
733
734/*
735 * acpi_fixed_button_handler:
736 *
737 *	Event handler for the fixed buttons.
738 */
739UINT32
740acpi_fixed_button_handler(void *context)
741{
742	struct sysmon_pswitch *smpsw = context;
743	int rv;
744
745#ifdef ACPI_BUT_DEBUG
746	printf("%s: fixed button handler\n", smpsw->smpsw_name);
747#endif
748
749	rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
750	    acpi_fixed_button_pressed, smpsw);
751	if (ACPI_FAILURE(rv))
752		printf("%s: WARNING: unable to queue fixed button pressed "
753		    "callback: %s\n", smpsw->smpsw_name,
754		    AcpiFormatException(rv));
755
756	return (ACPI_INTERRUPT_HANDLED);
757}
758
759/*
760 * acpi_fixed_button_pressed:
761 *
762 *	Deal with a fixed button being pressed.
763 */
764void
765acpi_fixed_button_pressed(void *context)
766{
767	struct sysmon_pswitch *smpsw = context;
768
769#ifdef ACPI_BUT_DEBUG
770	printf("%s: fixed button pressed, calling sysmon\n",
771	    smpsw->smpsw_name);
772#endif
773
774	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
775}
776
777/*****************************************************************************
778 * ACPI utility routines.
779 *****************************************************************************/
780
781/*
782 * acpi_eval_integer:
783 *
784 *	Evaluate an integer object.
785 */
786ACPI_STATUS
787acpi_eval_integer(ACPI_HANDLE handle, char *path, int *valp)
788{
789	ACPI_STATUS rv;
790	ACPI_BUFFER buf;
791	ACPI_OBJECT param;
792
793	if (handle == NULL)
794		handle = ACPI_ROOT_OBJECT;
795
796	buf.Pointer = &param;
797	buf.Length = sizeof(param);
798
799	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
800	if (ACPI_SUCCESS(rv))
801		*valp = param.Integer.Value;
802
803	return (rv);
804}
805
806/*
807 * acpi_eval_string:
808 *
809 *	Evaluate a (Unicode) string object.
810 */
811ACPI_STATUS
812acpi_eval_string(ACPI_HANDLE handle, char *path, char **stringp)
813{
814	ACPI_STATUS rv;
815	ACPI_BUFFER buf;
816
817	if (handle == NULL)
818		handle = ACPI_ROOT_OBJECT;
819
820	buf.Pointer = NULL;
821	buf.Length = ACPI_ALLOCATE_BUFFER;
822
823	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
824	if (ACPI_SUCCESS(rv)) {
825		ACPI_OBJECT *param = buf.Pointer;
826		char *ptr = param->String.Pointer;
827		size_t len = param->String.Length;
828		if ((*stringp = AcpiOsAllocate(len)) == NULL)
829			rv = AE_NO_MEMORY;
830		else
831			(void)memcpy(*stringp, ptr, len);
832		AcpiOsFree(param);
833	}
834
835	return (rv);
836}
837
838
839/*
840 * acpi_eval_struct:
841 *
842 *	Evaluate a more complex structure.
843 *	Caller must free buf.Pointer by AcpiOsFree().
844 */
845ACPI_STATUS
846acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp)
847{
848	ACPI_STATUS rv;
849
850	if (handle == NULL)
851		handle = ACPI_ROOT_OBJECT;
852
853	bufp->Pointer = NULL;
854	bufp->Length = ACPI_ALLOCATE_BUFFER;
855
856	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
857
858	return (rv);
859}
860
861/*
862 * acpi_get:
863 *
864 *	Fetch data info the specified (empty) ACPI buffer.
865 *	Caller must free buf.Pointer by AcpiOsFree().
866 */
867ACPI_STATUS
868acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
869    ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
870{
871	buf->Pointer = NULL;
872	buf->Length = ACPI_ALLOCATE_BUFFER;
873
874	return ((*getit)(handle, buf));
875}
876
877
878/*
879 * acpi_match_hid
880 *
881 *	Match given ids against _HID and _CIDs
882 */
883int
884acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
885{
886	int i;
887
888	while (*ids) {
889		if ((ad->Valid & ACPI_VALID_HID) &&
890		    strcmp(ad->HardwareId.Value, *ids) == 0)
891			return (1);
892
893		if (ad->Valid & ACPI_VALID_CID) {
894			for (i = 0; i < ad->CompatibilityId.Count; i++) {
895				if (strcmp(ad->CompatibilityId.Id[i].Value, *ids) == 0)
896					return (1);
897			}
898		}
899		ids++;
900	}
901
902	return (0);
903}
904
905
906/*****************************************************************************
907 * ACPI sleep support.
908 *****************************************************************************/
909
910static int
911is_available_state(struct acpi_softc *sc, int state)
912{
913	UINT8 type_a, type_b;
914
915	return (ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
916						  &type_a, &type_b)));
917}
918
919/*
920 * acpi_enter_sleep_state:
921 *
922 *	enter to the specified sleep state.
923 */
924
925ACPI_STATUS
926acpi_enter_sleep_state(struct acpi_softc *sc, int state)
927{
928	int s;
929	ACPI_STATUS ret = AE_OK;
930
931	switch (state) {
932	case ACPI_STATE_S0:
933		break;
934	case ACPI_STATE_S1:
935	case ACPI_STATE_S2:
936	case ACPI_STATE_S3:
937	case ACPI_STATE_S4:
938		if (!is_available_state(sc, state)) {
939			printf("acpi: cannot enter the sleep state (%d).\n",
940			       state);
941			break;
942		}
943		ret = AcpiEnterSleepStatePrep(state);
944		if (ACPI_FAILURE(ret)) {
945			printf("acpi: failed preparing to sleep (%s)\n",
946			       AcpiFormatException(ret));
947			break;
948		}
949		if (state==ACPI_STATE_S1) {
950			/* just enter the state */
951			acpi_md_OsDisableInterrupt();
952			AcpiEnterSleepState((UINT8)state);
953			AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
954		} else {
955			/* XXX: powerhooks(9) framework is too poor to
956			 * support ACPI sleep state...
957			 */
958			dopowerhooks(PWR_SOFTSUSPEND);
959			s = splhigh();
960			dopowerhooks(PWR_SUSPEND);
961			acpi_md_sleep(state);
962			dopowerhooks(PWR_RESUME);
963			splx(s);
964			dopowerhooks(PWR_SOFTRESUME);
965			if (state==ACPI_STATE_S4)
966				AcpiEnable();
967		}
968		AcpiLeaveSleepState((UINT8)state);
969		break;
970	case ACPI_STATE_S5:
971		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
972		if (ACPI_FAILURE(ret)) {
973			printf("acpi: failed preparing to sleep (%s)\n",
974			       AcpiFormatException(ret));
975			break;
976		}
977		acpi_md_OsDisableInterrupt();
978		AcpiEnterSleepState(ACPI_STATE_S5);
979		printf("WARNING: powerdown failed!\n");
980		break;
981	}
982
983	return (ret);
984}
985
986#ifdef ACPI_PCI_FIXUP
987ACPI_STATUS acpi_pci_fixup_bus(ACPI_HANDLE, UINT32, void *, void **);
988/*
989 * acpi_pci_fixup:
990 *
991 *	Set up PCI devices that BIOS didn't handle right.
992 *	Iterate through all devices and try to get the _PTR
993 *	(PCI Routing Table).  If it exists then make sure all
994 *	interrupt links that it uses are working.
995 */
996void
997acpi_pci_fixup(struct acpi_softc *sc)
998{
999	ACPI_HANDLE parent;
1000	ACPI_STATUS rv;
1001
1002#ifdef ACPI_DEBUG
1003	printf("acpi_pci_fixup starts:\n");
1004#endif
1005	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &parent);
1006	if (ACPI_FAILURE(rv))
1007		return;
1008	sc->sc_pci_bus = 0;
1009	AcpiWalkNamespace(ACPI_TYPE_DEVICE, parent, 100,
1010	    acpi_pci_fixup_bus, sc, NULL);
1011}
1012
1013static ACPI_HANDLE
1014acpi_get_node(char *name)
1015{
1016	ACPI_NAMESPACE_NODE *ObjDesc;
1017	ACPI_STATUS Status;
1018
1019	Status = AcpiNsGetNodeByPath(name, NULL, 0, &ObjDesc);
1020	if (ACPI_FAILURE (Status)) {
1021		printf("acpi_get_node: could not find: %s\n",
1022		       AcpiFormatException (Status));
1023		return NULL;
1024	}
1025	return ObjDesc;
1026}
1027
1028static uint
1029acpi_get_intr(ACPI_HANDLE handle)
1030{
1031	ACPI_BUFFER ret;
1032	ACPI_STATUS rv;
1033	ACPI_RESOURCE *res;
1034	ACPI_RESOURCE_IRQ *irq;
1035	uint intr;
1036
1037	intr = -1;
1038	rv = acpi_get(handle, &ret, AcpiGetCurrentResources);
1039	if (ACPI_FAILURE(rv))
1040		return (intr);
1041	for (res = ret.Pointer; res->Id != ACPI_RSTYPE_END_TAG;
1042	     res = ACPI_NEXT_RESOURCE(res)) {
1043		if (res->Id == ACPI_RSTYPE_IRQ) {
1044			irq = (ACPI_RESOURCE_IRQ *)&res->Data;
1045			if (irq->NumberOfInterrupts == 1)
1046				intr = irq->Interrupts[0];
1047			break;
1048		}
1049	}
1050	AcpiOsFree(ret.Pointer);
1051	return (intr);
1052}
1053
1054static void
1055acpi_pci_set_line(int bus, int dev, int pin, int line)
1056{
1057	ACPI_STATUS err;
1058	ACPI_PCI_ID pid;
1059	UINT32 intr, id, bhlc;
1060	int func, nfunc;
1061
1062	pid.Bus = bus;
1063	pid.Device = dev;
1064	pid.Function = 0;
1065
1066	err = AcpiOsReadPciConfiguration(&pid, PCI_BHLC_REG, &bhlc, 32);
1067	if (err)
1068		return;
1069	if (PCI_HDRTYPE_MULTIFN(bhlc))
1070		nfunc = 8;
1071	else
1072		nfunc = 1;
1073
1074	for (func = 0; func < nfunc; func++) {
1075		pid.Function = func;
1076
1077		err = AcpiOsReadPciConfiguration(&pid, PCI_ID_REG, &id, 32);
1078		if (err || PCI_VENDOR(id) == PCI_VENDOR_INVALID ||
1079		    PCI_VENDOR(id) == 0)
1080			continue;
1081
1082		err = AcpiOsReadPciConfiguration(&pid, PCI_INTERRUPT_REG,
1083			  &intr, 32);
1084		if (err) {
1085			printf("AcpiOsReadPciConfiguration failed %d\n", err);
1086			return;
1087		}
1088		if (pin == PCI_INTERRUPT_PIN(intr) &&
1089		    line != PCI_INTERRUPT_LINE(intr)) {
1090#ifdef ACPI_DEBUG
1091			printf("acpi fixup pci intr: %d:%d:%d %c: %d -> %d\n",
1092			       bus, dev, func,
1093			       pin + '@', PCI_INTERRUPT_LINE(intr),
1094			       line);
1095#endif
1096			intr &= ~(PCI_INTERRUPT_LINE_MASK <<
1097				  PCI_INTERRUPT_LINE_SHIFT);
1098			intr |= line << PCI_INTERRUPT_LINE_SHIFT;
1099			err = AcpiOsWritePciConfiguration(&pid,
1100				  PCI_INTERRUPT_REG, intr, 32);
1101			if (err) {
1102				printf("AcpiOsWritePciConfiguration failed"
1103				       " %d\n", err);
1104				return;
1105			}
1106		}
1107	}
1108}
1109
1110ACPI_STATUS
1111acpi_pci_fixup_bus(ACPI_HANDLE handle, UINT32 level, void *context,
1112		   void **status)
1113{
1114	struct acpi_softc *sc = context;
1115	ACPI_STATUS rv;
1116	ACPI_BUFFER buf;
1117	UINT8 *Buffer;
1118	ACPI_PCI_ROUTING_TABLE *PrtElement;
1119	ACPI_HANDLE link;
1120	uint line;
1121	ACPI_NAMESPACE_NODE *node;
1122	ACPI_INTEGER val;
1123
1124	rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable);
1125	if (ACPI_FAILURE(rv))
1126		return (AE_OK);
1127
1128	/*
1129	 * If at level 1, this is a PCI root bus. Try the _BBN method
1130	 * to get the right PCI bus numbering for the following
1131	 * busses (this is a depth-first walk). It may fail,
1132	 * for example if there's only one root bus, but that
1133	 * case should be ok, so we'll ignore that.
1134	 */
1135	if (level == 1) {
1136		node = AcpiNsMapHandleToNode(handle);
1137		rv = AcpiUtEvaluateNumericObject(METHOD_NAME__BBN, node, &val);
1138		if (!ACPI_FAILURE(rv)) {
1139#ifdef ACPI_DEBUG
1140			printf("%s: fixup: _BBN success, bus # was %d now %d\n",
1141			    sc->sc_dev.dv_xname, sc->sc_pci_bus,
1142			    ACPI_LOWORD(val));
1143#endif
1144			sc->sc_pci_bus = ACPI_LOWORD(val);
1145		}
1146	}
1147
1148
1149#ifdef ACPI_DEBUG
1150	printf("%s: fixing up PCI bus %d at level %u\n", sc->sc_dev.dv_xname,
1151	    sc->sc_pci_bus, level);
1152#endif
1153
1154        for (Buffer = buf.Pointer; ; Buffer += PrtElement->Length) {
1155		PrtElement = (ACPI_PCI_ROUTING_TABLE *)Buffer;
1156		if (PrtElement->Length == 0)
1157			break;
1158		if (PrtElement->Source[0] == 0)
1159			continue;
1160
1161		link = acpi_get_node(PrtElement->Source);
1162		if (link == NULL)
1163			continue;
1164		line = acpi_get_intr(link);
1165		if (line == -1) {
1166#ifdef ACPI_DEBUG
1167			printf("%s: fixing up intr link %s\n",
1168			    sc->sc_dev.dv_xname, PrtElement->Source);
1169#endif
1170			rv = acpi_allocate_resources(link);
1171			if (ACPI_FAILURE(rv)) {
1172				printf("%s: interrupt allocation failed %s\n",
1173				    sc->sc_dev.dv_xname, PrtElement->Source);
1174				continue;
1175			}
1176			line = acpi_get_intr(link);
1177			if (line == -1) {
1178				printf("%s: get intr failed %s\n",
1179				    sc->sc_dev.dv_xname, PrtElement->Source);
1180				continue;
1181			}
1182		}
1183
1184		acpi_pci_set_line(sc->sc_pci_bus, PrtElement->Address >> 16,
1185		    PrtElement->Pin + 1, line);
1186	}
1187
1188	sc->sc_pci_bus++;
1189
1190	AcpiOsFree(buf.Pointer);
1191	return (AE_OK);
1192}
1193#endif /* ACPI_PCI_FIXUP */
1194
1195#if defined(ACPI_PCI_FIXUP) || defined(ACPI_ACTIVATE_DEV)
1196/* XXX This very incomplete */
1197ACPI_STATUS
1198acpi_allocate_resources(ACPI_HANDLE handle)
1199{
1200	ACPI_BUFFER bufp, bufc, bufn;
1201	ACPI_RESOURCE *resp, *resc, *resn;
1202	ACPI_RESOURCE_IRQ *irq;
1203	ACPI_STATUS rv;
1204	uint delta;
1205
1206	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1207	if (ACPI_FAILURE(rv))
1208		goto out;
1209	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1210	if (ACPI_FAILURE(rv)) {
1211		goto out1;
1212	}
1213
1214	bufn.Length = 1000;
1215	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1216	resp = bufp.Pointer;
1217	resc = bufc.Pointer;
1218	while (resc->Id != ACPI_RSTYPE_END_TAG &&
1219	       resp->Id != ACPI_RSTYPE_END_TAG) {
1220		while (resc->Id != resp->Id && resp->Id != ACPI_RSTYPE_END_TAG)
1221			resp = ACPI_NEXT_RESOURCE(resp);
1222		if (resp->Id == ACPI_RSTYPE_END_TAG)
1223			break;
1224		/* Found identical Id */
1225		resn->Id = resc->Id;
1226		switch (resc->Id) {
1227		case ACPI_RSTYPE_IRQ:
1228			memcpy(&resn->Data, &resp->Data,
1229			       sizeof(ACPI_RESOURCE_IRQ));
1230			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1231			irq->Interrupts[0] =
1232			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1233			        Interrupts[irq->NumberOfInterrupts-1];
1234			irq->NumberOfInterrupts = 1;
1235			resn->Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
1236			break;
1237		case ACPI_RSTYPE_IO:
1238			memcpy(&resn->Data, &resp->Data,
1239			       sizeof(ACPI_RESOURCE_IO));
1240			resn->Length = resp->Length;
1241			break;
1242		default:
1243			printf("acpi_allocate_resources: res=%d\n", resc->Id);
1244			rv = AE_BAD_DATA;
1245			goto out2;
1246		}
1247		resc = ACPI_NEXT_RESOURCE(resc);
1248		resn = ACPI_NEXT_RESOURCE(resn);
1249		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1250		if (delta >=
1251		    bufn.Length-ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_DATA)) {
1252			bufn.Length *= 2;
1253			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1254					       M_ACPI, M_WAITOK);
1255			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1256		}
1257	}
1258	if (resc->Id != ACPI_RSTYPE_END_TAG) {
1259		printf("acpi_allocate_resources: resc not exhausted\n");
1260		rv = AE_BAD_DATA;
1261		goto out3;
1262	}
1263
1264	resn->Id = ACPI_RSTYPE_END_TAG;
1265	rv = AcpiSetCurrentResources(handle, &bufn);
1266	if (ACPI_FAILURE(rv)) {
1267		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1268		       AcpiFormatException(rv));
1269	}
1270
1271out3:
1272	free(bufn.Pointer, M_ACPI);
1273out2:
1274	AcpiOsFree(bufc.Pointer);
1275out1:
1276	AcpiOsFree(bufp.Pointer);
1277out:
1278	return rv;
1279}
1280#endif /* ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV */
1281