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