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