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