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