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