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