acpi.c revision 1.48 1 /* $NetBSD: acpi.c,v 1.48 2003/10/31 20:54:18 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.48 2003/10/31 20:54:18 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_STATUS rv;
217
218 printf("\n");
219
220 if (acpi_softc != NULL)
221 panic("acpi_attach: ACPI has already been attached");
222
223 sysmon_power_settype("acpi");
224
225 printf("%s: using Intel ACPI CA subsystem version %08x\n",
226 sc->sc_dev.dv_xname, ACPI_CA_VERSION);
227
228 printf("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
229 sc->sc_dev.dv_xname,
230 AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
231 AcpiGbl_XSDT->OemRevision,
232 AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->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.Value, 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.Value);
482 } else {
483 printf("acpi: activated %s\n", di->HardwareId.Value);
484 }
485
486 #ifdef ACPI_DEBUG
487 (void)AcpiGetObjectInfo(handle, di);
488 printf("acpi_activate_device: %s, new status=%x\n",
489 di->HardwareId.Value, 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_OBJECT_TYPE type;
510 ACPI_BUFFER buf;
511 ACPI_DEVICE_INFO devinfo;
512 ACPI_STATUS rv;
513
514 if (AcpiGetType(handle, &type) == AE_OK) {
515 buf.Pointer = &devinfo;
516 buf.Length = sizeof(devinfo);
517 rv = AcpiGetObjectInfo(handle, &buf);
518 if (rv != AE_OK) {
519 #ifdef ACPI_DEBUG
520 printf("%s: AcpiGetObjectInfo failed\n",
521 sc->sc_dev.dv_xname);
522 #endif
523 goto out; /* XXX why return OK */
524 }
525
526 switch (type) {
527 case ACPI_TYPE_DEVICE:
528 #ifdef ACPI_ACTIVATE_DEV
529 if ((devinfo.Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
530 (ACPI_VALID_STA|ACPI_VALID_HID) &&
531 (devinfo.CurrentStatus &
532 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
533 ACPI_STA_DEV_PRESENT) {
534 acpi_activate_device(handle, &devinfo);
535
536 /* Update devinfo. */
537 buf.Pointer = &devinfo;
538 buf.Length = sizeof(devinfo);
539 rv = AcpiGetObjectInfo(handle, &buf);
540 if (rv != AE_OK)
541 goto out;
542 }
543
544 /* FALLTHROUGH */
545 #endif
546
547 case ACPI_TYPE_PROCESSOR:
548 case ACPI_TYPE_THERMAL:
549 case ACPI_TYPE_POWER:
550 ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
551 if (ad == NULL)
552 return (AE_NO_MEMORY);
553
554 ad->ad_devinfo = devinfo;
555 ad->ad_handle = handle;
556 ad->ad_level = level;
557 ad->ad_scope = as;
558 ad->ad_type = type;
559
560 TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
561
562 if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) == 0)
563 goto out;
564
565 #ifdef ACPI_EXTRA_DEBUG
566 printf("%s: HID %s found in scope %s level %d\n",
567 sc->sc_dev.dv_xname, ad->ad_devinfo.HardwareId.Value,
568 as->as_name, ad->ad_level);
569 if (ad->ad_devinfo.Valid & ACPI_VALID_UID)
570 printf(" UID %s\n",
571 ad->ad_devinfo.UniqueId);
572 if (ad->ad_devinfo.Valid & ACPI_VALID_ADR)
573 printf(" ADR 0x%016qx\n",
574 ad->ad_devinfo.Address);
575 if (ad->ad_devinfo.Valid & ACPI_VALID_STA)
576 printf(" STA 0x%08x\n",
577 ad->ad_devinfo.CurrentStatus);
578 #endif
579 }
580 }
581 out:
582 return (AE_OK);
583 }
584
585 /*
586 * acpi_print:
587 *
588 * Autoconfiguration print routine.
589 */
590 int
591 acpi_print(void *aux, const char *pnp)
592 {
593 struct acpi_attach_args *aa = aux;
594
595 if (pnp) {
596 if (aa->aa_node->ad_devinfo.Valid & ACPI_VALID_HID) {
597 char *pnpstr = aa->aa_node->ad_devinfo.HardwareId.Value;
598 char *str;
599
600 aprint_normal("%s ", pnpstr);
601 if (acpi_eval_string(aa->aa_node->ad_handle,
602 "_STR", &str) == AE_OK) {
603 aprint_normal("[%s] ", str);
604 AcpiOsFree(str);
605 }
606 #ifdef ACPIVERBOSE
607 else {
608 int i;
609
610 for (i = 0; i < sizeof(acpi_knowndevs) /
611 sizeof(acpi_knowndevs[0]); i++) {
612 if (strcmp(acpi_knowndevs[i].pnp,
613 pnpstr) == 0) {
614 printf("[%s] ",
615 acpi_knowndevs[i].str);
616 }
617 }
618 }
619
620 #endif
621 } else {
622 aprint_normal("ACPI Object Type '%s' (0x%02x) ",
623 AcpiUtGetTypeName(aa->aa_node->ad_devinfo.Type),
624 aa->aa_node->ad_devinfo.Type);
625 }
626 aprint_normal("at %s", pnp);
627 } else {
628 if (aa->aa_node->ad_devinfo.Valid & ACPI_VALID_HID) {
629 aprint_normal(" (%s", aa->aa_node->ad_devinfo.HardwareId.Value);
630 if (aa->aa_node->ad_devinfo.Valid & ACPI_VALID_UID) {
631 char *uid;
632
633 uid = aa->aa_node->ad_devinfo.UniqueId.Value;
634 if (uid[0] == '\0')
635 uid = "<null>";
636 aprint_normal("-%s", uid);
637 }
638 aprint_normal(")");
639 }
640 }
641
642 return (UNCONF);
643 }
644
645 /*****************************************************************************
646 * ACPI fixed-hardware feature handlers
647 *****************************************************************************/
648
649 UINT32 acpi_fixed_button_handler(void *);
650 void acpi_fixed_button_pressed(void *);
651
652 /*
653 * acpi_enable_fixed_events:
654 *
655 * Enable any fixed-hardware feature handlers.
656 */
657 void
658 acpi_enable_fixed_events(struct acpi_softc *sc)
659 {
660 static int beenhere;
661 ACPI_STATUS rv;
662
663 KASSERT(beenhere == 0);
664 beenhere = 1;
665
666 /*
667 * Check for fixed-hardware buttons.
668 */
669
670 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
671 printf("%s: fixed-feature power button present\n",
672 sc->sc_dev.dv_xname);
673 sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
674 sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
675 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
676 printf("%s: unable to register fixed power button "
677 "with sysmon\n", sc->sc_dev.dv_xname);
678 } else {
679 rv = AcpiInstallFixedEventHandler(
680 ACPI_EVENT_POWER_BUTTON,
681 acpi_fixed_button_handler, &sc->sc_smpsw_power);
682 if (rv != AE_OK) {
683 printf("%s: unable to install handler for "
684 "fixed power button: %d\n",
685 sc->sc_dev.dv_xname, rv);
686 }
687 }
688 }
689
690 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
691 printf("%s: fixed-feature sleep button present\n",
692 sc->sc_dev.dv_xname);
693 sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
694 sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
695 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
696 printf("%s: unable to register fixed sleep button "
697 "with sysmon\n", sc->sc_dev.dv_xname);
698 } else {
699 rv = AcpiInstallFixedEventHandler(
700 ACPI_EVENT_SLEEP_BUTTON,
701 acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
702 if (rv != AE_OK) {
703 printf("%s: unable to install handler for "
704 "fixed sleep button: %d\n",
705 sc->sc_dev.dv_xname, rv);
706 }
707 }
708 }
709 }
710
711 /*
712 * acpi_fixed_button_handler:
713 *
714 * Event handler for the fixed buttons.
715 */
716 UINT32
717 acpi_fixed_button_handler(void *context)
718 {
719 struct sysmon_pswitch *smpsw = context;
720 int rv;
721
722 #ifdef ACPI_BUT_DEBUG
723 printf("%s: fixed button handler\n", smpsw->smpsw_name);
724 #endif
725
726 rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
727 acpi_fixed_button_pressed, smpsw);
728 if (rv != AE_OK)
729 printf("%s: WARNING: unable to queue fixed button pressed "
730 "callback: %d\n", smpsw->smpsw_name, rv);
731
732 return (ACPI_INTERRUPT_HANDLED);
733 }
734
735 /*
736 * acpi_fixed_button_pressed:
737 *
738 * Deal with a fixed button being pressed.
739 */
740 void
741 acpi_fixed_button_pressed(void *context)
742 {
743 struct sysmon_pswitch *smpsw = context;
744
745 #ifdef ACPI_BUT_DEBUG
746 printf("%s: fixed button pressed, calling sysmon\n",
747 smpsw->smpsw_name);
748 #endif
749
750 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
751 }
752
753 /*****************************************************************************
754 * ACPI utility routines.
755 *****************************************************************************/
756
757 /*
758 * acpi_eval_integer:
759 *
760 * Evaluate an integer object.
761 */
762 ACPI_STATUS
763 acpi_eval_integer(ACPI_HANDLE handle, char *path, int *valp)
764 {
765 ACPI_STATUS rv;
766 ACPI_BUFFER buf;
767 ACPI_OBJECT param;
768
769 if (handle == NULL)
770 handle = ACPI_ROOT_OBJECT;
771
772 buf.Pointer = ¶m;
773 buf.Length = sizeof(param);
774
775 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
776 if (rv == AE_OK)
777 *valp = param.Integer.Value;
778
779 return (rv);
780 }
781
782 /*
783 * acpi_eval_string:
784 *
785 * Evaluate a (Unicode) string object.
786 */
787 ACPI_STATUS
788 acpi_eval_string(ACPI_HANDLE handle, char *path, char **stringp)
789 {
790 ACPI_STATUS rv;
791 ACPI_BUFFER buf;
792 ACPI_OBJECT *param;
793
794 if (handle == NULL)
795 handle = ACPI_ROOT_OBJECT;
796
797 buf.Pointer = NULL;
798 buf.Length = ACPI_ALLOCATE_BUFFER;
799
800 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
801 if (rv == AE_OK) {
802 param = buf.Pointer;
803 char *ptr = param->String.Pointer;
804 size_t len = param->String.Length;
805 if ((*stringp = AcpiOsAllocate(len)) == NULL)
806 rv = AE_NO_MEMORY;
807 else
808 (void)memcpy(*stringp, ptr, len);
809 AcpiOsFree(param);
810 }
811
812 return (rv);
813 }
814
815
816 /*
817 * acpi_eval_struct:
818 *
819 * Evaluate a more complex structure.
820 * Caller must free buf.Pointer by AcpiOsFree().
821 */
822 ACPI_STATUS
823 acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp)
824 {
825 ACPI_STATUS rv;
826
827 if (handle == NULL)
828 handle = ACPI_ROOT_OBJECT;
829
830 bufp->Pointer = NULL;
831 bufp->Length = ACPI_ALLOCATE_BUFFER;
832
833 rv = AcpiEvaluateObject(handle, path, NULL, bufp);
834
835 return (rv);
836 }
837
838 /*
839 * acpi_get:
840 *
841 * Fetch data info the specified (empty) ACPI buffer.
842 * Caller must free buf.Pointer by AcpiOsFree().
843 */
844 ACPI_STATUS
845 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
846 ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
847 {
848 buf->Pointer = NULL;
849 buf->Length = ACPI_ALLOCATE_BUFFER;
850
851 return ((*getit)(handle, buf));
852 }
853
854
855 /*****************************************************************************
856 * ACPI sleep support.
857 *****************************************************************************/
858
859 static int
860 is_available_state(struct acpi_softc *sc, int state)
861 {
862 UINT8 type_a, type_b;
863
864 return (ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
865 &type_a, &type_b)));
866 }
867
868 /*
869 * acpi_enter_sleep_state:
870 *
871 * enter to the specified sleep state.
872 */
873
874 ACPI_STATUS
875 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
876 {
877 int s;
878 ACPI_STATUS ret = AE_OK;
879
880 switch (state) {
881 case ACPI_STATE_S0:
882 break;
883 case ACPI_STATE_S1:
884 case ACPI_STATE_S2:
885 case ACPI_STATE_S3:
886 case ACPI_STATE_S4:
887 if (!is_available_state(sc, state)) {
888 printf("acpi: cannot enter the sleep state (%d).\n",
889 state);
890 break;
891 }
892 ret = AcpiEnterSleepStatePrep(state);
893 if (ACPI_FAILURE(ret)) {
894 printf("acpi: failed preparing to sleep (%s)\n",
895 AcpiFormatException(ret));
896 break;
897 }
898 if (state==ACPI_STATE_S1) {
899 /* just enter the state */
900 acpi_md_OsDisableInterrupt();
901 AcpiEnterSleepState((UINT8)state);
902 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
903 } else {
904 /* XXX: powerhooks(9) framework is too poor to
905 * support ACPI sleep state...
906 */
907 dopowerhooks(PWR_SOFTSUSPEND);
908 s = splhigh();
909 dopowerhooks(PWR_SUSPEND);
910 acpi_md_sleep(state);
911 dopowerhooks(PWR_RESUME);
912 splx(s);
913 dopowerhooks(PWR_SOFTRESUME);
914 if (state==ACPI_STATE_S4)
915 AcpiEnable();
916 }
917 AcpiLeaveSleepState((UINT8)state);
918 break;
919 case ACPI_STATE_S5:
920 ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
921 if (ACPI_FAILURE(ret)) {
922 printf("acpi: failed preparing to sleep (%s)\n",
923 AcpiFormatException(ret));
924 break;
925 }
926 acpi_md_OsDisableInterrupt();
927 AcpiEnterSleepState(ACPI_STATE_S5);
928 printf("WARNING: powerdown failed!\n");
929 break;
930 }
931
932 return (ret);
933 }
934
935 #ifdef ACPI_PCI_FIXUP
936 ACPI_STATUS acpi_pci_fixup_bus(ACPI_HANDLE, UINT32, void *, void **);
937 /*
938 * acpi_pci_fixup:
939 *
940 * Set up PCI devices that BIOS didn't handle right.
941 * Iterate through all devices and try to get the _PTR
942 * (PCI Routing Table). If it exists then make sure all
943 * interrupt links that it uses are working.
944 */
945 void
946 acpi_pci_fixup(struct acpi_softc *sc)
947 {
948 ACPI_HANDLE parent;
949
950 #ifdef ACPI_DEBUG
951 printf("acpi_pci_fixup starts:\n");
952 #endif
953 if (AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &parent) != AE_OK)
954 return;
955 sc->sc_pci_bus = 0;
956 AcpiWalkNamespace(ACPI_TYPE_DEVICE, parent, 100,
957 acpi_pci_fixup_bus, sc, NULL);
958 }
959
960 static ACPI_HANDLE
961 acpi_get_node(char *name)
962 {
963 ACPI_NAMESPACE_NODE *ObjDesc;
964 ACPI_STATUS Status;
965
966 Status = AcpiNsGetNodeByPath(name, NULL, 0, &ObjDesc);
967 if (ACPI_FAILURE (Status)) {
968 printf("acpi_get_node: could not find: %s\n",
969 AcpiFormatException (Status));
970 return NULL;
971 }
972 return ObjDesc;
973 }
974
975 static uint
976 acpi_get_intr(ACPI_HANDLE handle)
977 {
978 ACPI_BUFFER ret;
979 ACPI_STATUS rv;
980 ACPI_RESOURCE *res;
981 ACPI_RESOURCE_IRQ *irq;
982 uint intr;
983
984 intr = -1;
985 rv = acpi_get(handle, &ret, AcpiGetCurrentResources);
986 if (ACPI_FAILURE(rv))
987 return (intr);
988 for (res = ret.Pointer; res->Id != ACPI_RSTYPE_END_TAG;
989 res = ACPI_NEXT_RESOURCE(res)) {
990 if (res->Id == ACPI_RSTYPE_IRQ) {
991 irq = (ACPI_RESOURCE_IRQ *)&res->Data;
992 if (irq->NumberOfInterrupts == 1)
993 intr = irq->Interrupts[0];
994 break;
995 }
996 }
997 AcpiOsFree(ret.Pointer);
998 return (intr);
999 }
1000
1001 static void
1002 acpi_pci_set_line(int bus, int dev, int pin, int line)
1003 {
1004 ACPI_STATUS err;
1005 ACPI_PCI_ID pid;
1006 UINT32 intr, id, bhlc;
1007 int func, nfunc;
1008
1009 pid.Bus = bus;
1010 pid.Device = dev;
1011 pid.Function = 0;
1012
1013 err = AcpiOsReadPciConfiguration(&pid, PCI_BHLC_REG, &bhlc, 32);
1014 if (err)
1015 return;
1016 if (PCI_HDRTYPE_MULTIFN(bhlc))
1017 nfunc = 8;
1018 else
1019 nfunc = 1;
1020
1021 for (func = 0; func < nfunc; func++) {
1022 pid.Function = func;
1023
1024 err = AcpiOsReadPciConfiguration(&pid, PCI_ID_REG, &id, 32);
1025 if (err || PCI_VENDOR(id) == PCI_VENDOR_INVALID ||
1026 PCI_VENDOR(id) == 0)
1027 continue;
1028
1029 err = AcpiOsReadPciConfiguration(&pid, PCI_INTERRUPT_REG,
1030 &intr, 32);
1031 if (err) {
1032 printf("AcpiOsReadPciConfiguration failed %d\n", err);
1033 return;
1034 }
1035 if (pin == PCI_INTERRUPT_PIN(intr) &&
1036 line != PCI_INTERRUPT_LINE(intr)) {
1037 #ifdef ACPI_DEBUG
1038 printf("acpi fixup pci intr: %d:%d:%d %c: %d -> %d\n",
1039 bus, dev, func,
1040 pin + '@', PCI_INTERRUPT_LINE(intr),
1041 line);
1042 #endif
1043 intr &= ~(PCI_INTERRUPT_LINE_MASK <<
1044 PCI_INTERRUPT_LINE_SHIFT);
1045 intr |= line << PCI_INTERRUPT_LINE_SHIFT;
1046 err = AcpiOsWritePciConfiguration(&pid,
1047 PCI_INTERRUPT_REG, intr, 32);
1048 if (err) {
1049 printf("AcpiOsWritePciConfiguration failed"
1050 " %d\n", err);
1051 return;
1052 }
1053 }
1054 }
1055 }
1056
1057 ACPI_STATUS
1058 acpi_pci_fixup_bus(ACPI_HANDLE handle, UINT32 level, void *context,
1059 void **status)
1060 {
1061 struct acpi_softc *sc = context;
1062 ACPI_STATUS rv;
1063 ACPI_BUFFER buf;
1064 UINT8 *Buffer;
1065 ACPI_PCI_ROUTING_TABLE *PrtElement;
1066 ACPI_HANDLE link;
1067 uint line;
1068 ACPI_NAMESPACE_NODE *node;
1069 ACPI_INTEGER val;
1070
1071 rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable);
1072 if (ACPI_FAILURE(rv))
1073 return (AE_OK);
1074
1075 /*
1076 * If at level 1, this is a PCI root bus. Try the _BBN method
1077 * to get the right PCI bus numbering for the following
1078 * busses (this is a depth-first walk). It may fail,
1079 * for example if there's only one root bus, but that
1080 * case should be ok, so we'll ignore that.
1081 */
1082 if (level == 1) {
1083 node = AcpiNsMapHandleToNode(handle);
1084 rv = AcpiUtEvaluateNumericObject(METHOD_NAME__BBN, node, &val);
1085 if (!ACPI_FAILURE(rv)) {
1086 #ifdef ACPI_DEBUG
1087 printf("%s: fixup: _BBN success, bus # was %d now %d\n",
1088 sc->sc_dev.dv_xname, sc->sc_pci_bus,
1089 ACPI_LOWORD(val));
1090 #endif
1091 sc->sc_pci_bus = ACPI_LOWORD(val);
1092 }
1093 }
1094
1095
1096 #ifdef ACPI_DEBUG
1097 printf("%s: fixing up PCI bus %d at level %u\n", sc->sc_dev.dv_xname,
1098 sc->sc_pci_bus, level);
1099 #endif
1100
1101 for (Buffer = buf.Pointer; ; Buffer += PrtElement->Length) {
1102 PrtElement = (ACPI_PCI_ROUTING_TABLE *)Buffer;
1103 if (PrtElement->Length == 0)
1104 break;
1105 if (PrtElement->Source[0] == 0)
1106 continue;
1107
1108 link = acpi_get_node(PrtElement->Source);
1109 if (link == NULL)
1110 continue;
1111 line = acpi_get_intr(link);
1112 if (line == -1) {
1113 #ifdef ACPI_DEBUG
1114 printf("%s: fixing up intr link %s\n",
1115 sc->sc_dev.dv_xname, PrtElement->Source);
1116 #endif
1117 rv = acpi_allocate_resources(link);
1118 if (ACPI_FAILURE(rv)) {
1119 printf("%s: interrupt allocation failed %s\n",
1120 sc->sc_dev.dv_xname, PrtElement->Source);
1121 continue;
1122 }
1123 line = acpi_get_intr(link);
1124 if (line == -1) {
1125 printf("%s: get intr failed %s\n",
1126 sc->sc_dev.dv_xname, PrtElement->Source);
1127 continue;
1128 }
1129 }
1130
1131 acpi_pci_set_line(sc->sc_pci_bus, PrtElement->Address >> 16,
1132 PrtElement->Pin + 1, line);
1133 }
1134
1135 sc->sc_pci_bus++;
1136
1137 AcpiOsFree(buf.Pointer);
1138 return (AE_OK);
1139 }
1140 #endif /* ACPI_PCI_FIXUP */
1141
1142 #if defined(ACPI_PCI_FIXUP) || defined(ACPI_ACTIVATE_DEV)
1143 /* XXX This very incomplete */
1144 ACPI_STATUS
1145 acpi_allocate_resources(ACPI_HANDLE handle)
1146 {
1147 ACPI_BUFFER bufp, bufc, bufn;
1148 ACPI_RESOURCE *resp, *resc, *resn;
1149 ACPI_RESOURCE_IRQ *irq;
1150 ACPI_STATUS rv;
1151 uint delta;
1152
1153 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1154 if (ACPI_FAILURE(rv))
1155 goto out;
1156 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1157 if (ACPI_FAILURE(rv)) {
1158 goto out1;
1159 }
1160
1161 bufn.Length = 1000;
1162 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1163 resp = bufp.Pointer;
1164 resc = bufc.Pointer;
1165 while (resc->Id != ACPI_RSTYPE_END_TAG &&
1166 resp->Id != ACPI_RSTYPE_END_TAG) {
1167 while (resc->Id != resp->Id && resp->Id != ACPI_RSTYPE_END_TAG)
1168 resp = ACPI_NEXT_RESOURCE(resp);
1169 if (resp->Id == ACPI_RSTYPE_END_TAG)
1170 break;
1171 /* Found identical Id */
1172 resn->Id = resc->Id;
1173 switch (resc->Id) {
1174 case ACPI_RSTYPE_IRQ:
1175 memcpy(&resn->Data, &resp->Data,
1176 sizeof(ACPI_RESOURCE_IRQ));
1177 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1178 irq->Interrupts[0] =
1179 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1180 Interrupts[irq->NumberOfInterrupts-1];
1181 irq->NumberOfInterrupts = 1;
1182 resn->Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
1183 break;
1184 case ACPI_RSTYPE_IO:
1185 memcpy(&resn->Data, &resp->Data,
1186 sizeof(ACPI_RESOURCE_IO));
1187 resn->Length = resp->Length;
1188 break;
1189 default:
1190 printf("acpi_allocate_resources: res=%d\n", resc->Id);
1191 rv = AE_BAD_DATA;
1192 goto out2;
1193 }
1194 resc = ACPI_NEXT_RESOURCE(resc);
1195 resn = ACPI_NEXT_RESOURCE(resn);
1196 delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1197 if (delta >=
1198 bufn.Length-ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_DATA)) {
1199 bufn.Length *= 2;
1200 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1201 M_ACPI, M_WAITOK);
1202 resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1203 }
1204 }
1205 if (resc->Id != ACPI_RSTYPE_END_TAG) {
1206 printf("acpi_allocate_resources: resc not exhausted\n");
1207 rv = AE_BAD_DATA;
1208 goto out3;
1209 }
1210
1211 resn->Id = ACPI_RSTYPE_END_TAG;
1212 rv = AcpiSetCurrentResources(handle, &bufn);
1213 if (ACPI_FAILURE(rv)) {
1214 printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1215 AcpiFormatException(rv));
1216 }
1217
1218 out3:
1219 free(bufn.Pointer, M_ACPI);
1220 out2:
1221 AcpiOsFree(bufc.Pointer);
1222 out1:
1223 AcpiOsFree(bufp.Pointer);
1224 out:
1225 return rv;
1226 }
1227 #endif /* ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV */
1228