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