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