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