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