acpi.c revision 1.49 1 /* $NetBSD: acpi.c,v 1.49 2003/11/01 01:03:23 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.49 2003/11/01 01:03:23 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
793 if (handle == NULL)
794 handle = ACPI_ROOT_OBJECT;
795
796 buf.Pointer = NULL;
797 buf.Length = ACPI_ALLOCATE_BUFFER;
798
799 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
800 if (rv == AE_OK) {
801 ACPI_OBJECT *param = buf.Pointer;
802 char *ptr = param->String.Pointer;
803 size_t len = param->String.Length;
804 if ((*stringp = AcpiOsAllocate(len)) == NULL)
805 rv = AE_NO_MEMORY;
806 else
807 (void)memcpy(*stringp, ptr, len);
808 AcpiOsFree(param);
809 }
810
811 return (rv);
812 }
813
814
815 /*
816 * acpi_eval_struct:
817 *
818 * Evaluate a more complex structure.
819 * Caller must free buf.Pointer by AcpiOsFree().
820 */
821 ACPI_STATUS
822 acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp)
823 {
824 ACPI_STATUS rv;
825
826 if (handle == NULL)
827 handle = ACPI_ROOT_OBJECT;
828
829 bufp->Pointer = NULL;
830 bufp->Length = ACPI_ALLOCATE_BUFFER;
831
832 rv = AcpiEvaluateObject(handle, path, NULL, bufp);
833
834 return (rv);
835 }
836
837 /*
838 * acpi_get:
839 *
840 * Fetch data info the specified (empty) ACPI buffer.
841 * Caller must free buf.Pointer by AcpiOsFree().
842 */
843 ACPI_STATUS
844 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
845 ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
846 {
847 buf->Pointer = NULL;
848 buf->Length = ACPI_ALLOCATE_BUFFER;
849
850 return ((*getit)(handle, buf));
851 }
852
853
854 /*****************************************************************************
855 * ACPI sleep support.
856 *****************************************************************************/
857
858 static int
859 is_available_state(struct acpi_softc *sc, int state)
860 {
861 UINT8 type_a, type_b;
862
863 return (ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
864 &type_a, &type_b)));
865 }
866
867 /*
868 * acpi_enter_sleep_state:
869 *
870 * enter to the specified sleep state.
871 */
872
873 ACPI_STATUS
874 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
875 {
876 int s;
877 ACPI_STATUS ret = AE_OK;
878
879 switch (state) {
880 case ACPI_STATE_S0:
881 break;
882 case ACPI_STATE_S1:
883 case ACPI_STATE_S2:
884 case ACPI_STATE_S3:
885 case ACPI_STATE_S4:
886 if (!is_available_state(sc, state)) {
887 printf("acpi: cannot enter the sleep state (%d).\n",
888 state);
889 break;
890 }
891 ret = AcpiEnterSleepStatePrep(state);
892 if (ACPI_FAILURE(ret)) {
893 printf("acpi: failed preparing to sleep (%s)\n",
894 AcpiFormatException(ret));
895 break;
896 }
897 if (state==ACPI_STATE_S1) {
898 /* just enter the state */
899 acpi_md_OsDisableInterrupt();
900 AcpiEnterSleepState((UINT8)state);
901 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
902 } else {
903 /* XXX: powerhooks(9) framework is too poor to
904 * support ACPI sleep state...
905 */
906 dopowerhooks(PWR_SOFTSUSPEND);
907 s = splhigh();
908 dopowerhooks(PWR_SUSPEND);
909 acpi_md_sleep(state);
910 dopowerhooks(PWR_RESUME);
911 splx(s);
912 dopowerhooks(PWR_SOFTRESUME);
913 if (state==ACPI_STATE_S4)
914 AcpiEnable();
915 }
916 AcpiLeaveSleepState((UINT8)state);
917 break;
918 case ACPI_STATE_S5:
919 ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
920 if (ACPI_FAILURE(ret)) {
921 printf("acpi: failed preparing to sleep (%s)\n",
922 AcpiFormatException(ret));
923 break;
924 }
925 acpi_md_OsDisableInterrupt();
926 AcpiEnterSleepState(ACPI_STATE_S5);
927 printf("WARNING: powerdown failed!\n");
928 break;
929 }
930
931 return (ret);
932 }
933
934 #ifdef ACPI_PCI_FIXUP
935 ACPI_STATUS acpi_pci_fixup_bus(ACPI_HANDLE, UINT32, void *, void **);
936 /*
937 * acpi_pci_fixup:
938 *
939 * Set up PCI devices that BIOS didn't handle right.
940 * Iterate through all devices and try to get the _PTR
941 * (PCI Routing Table). If it exists then make sure all
942 * interrupt links that it uses are working.
943 */
944 void
945 acpi_pci_fixup(struct acpi_softc *sc)
946 {
947 ACPI_HANDLE parent;
948
949 #ifdef ACPI_DEBUG
950 printf("acpi_pci_fixup starts:\n");
951 #endif
952 if (AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &parent) != AE_OK)
953 return;
954 sc->sc_pci_bus = 0;
955 AcpiWalkNamespace(ACPI_TYPE_DEVICE, parent, 100,
956 acpi_pci_fixup_bus, sc, NULL);
957 }
958
959 static ACPI_HANDLE
960 acpi_get_node(char *name)
961 {
962 ACPI_NAMESPACE_NODE *ObjDesc;
963 ACPI_STATUS Status;
964
965 Status = AcpiNsGetNodeByPath(name, NULL, 0, &ObjDesc);
966 if (ACPI_FAILURE (Status)) {
967 printf("acpi_get_node: could not find: %s\n",
968 AcpiFormatException (Status));
969 return NULL;
970 }
971 return ObjDesc;
972 }
973
974 static uint
975 acpi_get_intr(ACPI_HANDLE handle)
976 {
977 ACPI_BUFFER ret;
978 ACPI_STATUS rv;
979 ACPI_RESOURCE *res;
980 ACPI_RESOURCE_IRQ *irq;
981 uint intr;
982
983 intr = -1;
984 rv = acpi_get(handle, &ret, AcpiGetCurrentResources);
985 if (ACPI_FAILURE(rv))
986 return (intr);
987 for (res = ret.Pointer; res->Id != ACPI_RSTYPE_END_TAG;
988 res = ACPI_NEXT_RESOURCE(res)) {
989 if (res->Id == ACPI_RSTYPE_IRQ) {
990 irq = (ACPI_RESOURCE_IRQ *)&res->Data;
991 if (irq->NumberOfInterrupts == 1)
992 intr = irq->Interrupts[0];
993 break;
994 }
995 }
996 AcpiOsFree(ret.Pointer);
997 return (intr);
998 }
999
1000 static void
1001 acpi_pci_set_line(int bus, int dev, int pin, int line)
1002 {
1003 ACPI_STATUS err;
1004 ACPI_PCI_ID pid;
1005 UINT32 intr, id, bhlc;
1006 int func, nfunc;
1007
1008 pid.Bus = bus;
1009 pid.Device = dev;
1010 pid.Function = 0;
1011
1012 err = AcpiOsReadPciConfiguration(&pid, PCI_BHLC_REG, &bhlc, 32);
1013 if (err)
1014 return;
1015 if (PCI_HDRTYPE_MULTIFN(bhlc))
1016 nfunc = 8;
1017 else
1018 nfunc = 1;
1019
1020 for (func = 0; func < nfunc; func++) {
1021 pid.Function = func;
1022
1023 err = AcpiOsReadPciConfiguration(&pid, PCI_ID_REG, &id, 32);
1024 if (err || PCI_VENDOR(id) == PCI_VENDOR_INVALID ||
1025 PCI_VENDOR(id) == 0)
1026 continue;
1027
1028 err = AcpiOsReadPciConfiguration(&pid, PCI_INTERRUPT_REG,
1029 &intr, 32);
1030 if (err) {
1031 printf("AcpiOsReadPciConfiguration failed %d\n", err);
1032 return;
1033 }
1034 if (pin == PCI_INTERRUPT_PIN(intr) &&
1035 line != PCI_INTERRUPT_LINE(intr)) {
1036 #ifdef ACPI_DEBUG
1037 printf("acpi fixup pci intr: %d:%d:%d %c: %d -> %d\n",
1038 bus, dev, func,
1039 pin + '@', PCI_INTERRUPT_LINE(intr),
1040 line);
1041 #endif
1042 intr &= ~(PCI_INTERRUPT_LINE_MASK <<
1043 PCI_INTERRUPT_LINE_SHIFT);
1044 intr |= line << PCI_INTERRUPT_LINE_SHIFT;
1045 err = AcpiOsWritePciConfiguration(&pid,
1046 PCI_INTERRUPT_REG, intr, 32);
1047 if (err) {
1048 printf("AcpiOsWritePciConfiguration failed"
1049 " %d\n", err);
1050 return;
1051 }
1052 }
1053 }
1054 }
1055
1056 ACPI_STATUS
1057 acpi_pci_fixup_bus(ACPI_HANDLE handle, UINT32 level, void *context,
1058 void **status)
1059 {
1060 struct acpi_softc *sc = context;
1061 ACPI_STATUS rv;
1062 ACPI_BUFFER buf;
1063 UINT8 *Buffer;
1064 ACPI_PCI_ROUTING_TABLE *PrtElement;
1065 ACPI_HANDLE link;
1066 uint line;
1067 ACPI_NAMESPACE_NODE *node;
1068 ACPI_INTEGER val;
1069
1070 rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable);
1071 if (ACPI_FAILURE(rv))
1072 return (AE_OK);
1073
1074 /*
1075 * If at level 1, this is a PCI root bus. Try the _BBN method
1076 * to get the right PCI bus numbering for the following
1077 * busses (this is a depth-first walk). It may fail,
1078 * for example if there's only one root bus, but that
1079 * case should be ok, so we'll ignore that.
1080 */
1081 if (level == 1) {
1082 node = AcpiNsMapHandleToNode(handle);
1083 rv = AcpiUtEvaluateNumericObject(METHOD_NAME__BBN, node, &val);
1084 if (!ACPI_FAILURE(rv)) {
1085 #ifdef ACPI_DEBUG
1086 printf("%s: fixup: _BBN success, bus # was %d now %d\n",
1087 sc->sc_dev.dv_xname, sc->sc_pci_bus,
1088 ACPI_LOWORD(val));
1089 #endif
1090 sc->sc_pci_bus = ACPI_LOWORD(val);
1091 }
1092 }
1093
1094
1095 #ifdef ACPI_DEBUG
1096 printf("%s: fixing up PCI bus %d at level %u\n", sc->sc_dev.dv_xname,
1097 sc->sc_pci_bus, level);
1098 #endif
1099
1100 for (Buffer = buf.Pointer; ; Buffer += PrtElement->Length) {
1101 PrtElement = (ACPI_PCI_ROUTING_TABLE *)Buffer;
1102 if (PrtElement->Length == 0)
1103 break;
1104 if (PrtElement->Source[0] == 0)
1105 continue;
1106
1107 link = acpi_get_node(PrtElement->Source);
1108 if (link == NULL)
1109 continue;
1110 line = acpi_get_intr(link);
1111 if (line == -1) {
1112 #ifdef ACPI_DEBUG
1113 printf("%s: fixing up intr link %s\n",
1114 sc->sc_dev.dv_xname, PrtElement->Source);
1115 #endif
1116 rv = acpi_allocate_resources(link);
1117 if (ACPI_FAILURE(rv)) {
1118 printf("%s: interrupt allocation failed %s\n",
1119 sc->sc_dev.dv_xname, PrtElement->Source);
1120 continue;
1121 }
1122 line = acpi_get_intr(link);
1123 if (line == -1) {
1124 printf("%s: get intr failed %s\n",
1125 sc->sc_dev.dv_xname, PrtElement->Source);
1126 continue;
1127 }
1128 }
1129
1130 acpi_pci_set_line(sc->sc_pci_bus, PrtElement->Address >> 16,
1131 PrtElement->Pin + 1, line);
1132 }
1133
1134 sc->sc_pci_bus++;
1135
1136 AcpiOsFree(buf.Pointer);
1137 return (AE_OK);
1138 }
1139 #endif /* ACPI_PCI_FIXUP */
1140
1141 #if defined(ACPI_PCI_FIXUP) || defined(ACPI_ACTIVATE_DEV)
1142 /* XXX This very incomplete */
1143 ACPI_STATUS
1144 acpi_allocate_resources(ACPI_HANDLE handle)
1145 {
1146 ACPI_BUFFER bufp, bufc, bufn;
1147 ACPI_RESOURCE *resp, *resc, *resn;
1148 ACPI_RESOURCE_IRQ *irq;
1149 ACPI_STATUS rv;
1150 uint delta;
1151
1152 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1153 if (ACPI_FAILURE(rv))
1154 goto out;
1155 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1156 if (ACPI_FAILURE(rv)) {
1157 goto out1;
1158 }
1159
1160 bufn.Length = 1000;
1161 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1162 resp = bufp.Pointer;
1163 resc = bufc.Pointer;
1164 while (resc->Id != ACPI_RSTYPE_END_TAG &&
1165 resp->Id != ACPI_RSTYPE_END_TAG) {
1166 while (resc->Id != resp->Id && resp->Id != ACPI_RSTYPE_END_TAG)
1167 resp = ACPI_NEXT_RESOURCE(resp);
1168 if (resp->Id == ACPI_RSTYPE_END_TAG)
1169 break;
1170 /* Found identical Id */
1171 resn->Id = resc->Id;
1172 switch (resc->Id) {
1173 case ACPI_RSTYPE_IRQ:
1174 memcpy(&resn->Data, &resp->Data,
1175 sizeof(ACPI_RESOURCE_IRQ));
1176 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1177 irq->Interrupts[0] =
1178 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1179 Interrupts[irq->NumberOfInterrupts-1];
1180 irq->NumberOfInterrupts = 1;
1181 resn->Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
1182 break;
1183 case ACPI_RSTYPE_IO:
1184 memcpy(&resn->Data, &resp->Data,
1185 sizeof(ACPI_RESOURCE_IO));
1186 resn->Length = resp->Length;
1187 break;
1188 default:
1189 printf("acpi_allocate_resources: res=%d\n", resc->Id);
1190 rv = AE_BAD_DATA;
1191 goto out2;
1192 }
1193 resc = ACPI_NEXT_RESOURCE(resc);
1194 resn = ACPI_NEXT_RESOURCE(resn);
1195 delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1196 if (delta >=
1197 bufn.Length-ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_DATA)) {
1198 bufn.Length *= 2;
1199 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1200 M_ACPI, M_WAITOK);
1201 resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1202 }
1203 }
1204 if (resc->Id != ACPI_RSTYPE_END_TAG) {
1205 printf("acpi_allocate_resources: resc not exhausted\n");
1206 rv = AE_BAD_DATA;
1207 goto out3;
1208 }
1209
1210 resn->Id = ACPI_RSTYPE_END_TAG;
1211 rv = AcpiSetCurrentResources(handle, &bufn);
1212 if (ACPI_FAILURE(rv)) {
1213 printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1214 AcpiFormatException(rv));
1215 }
1216
1217 out3:
1218 free(bufn.Pointer, M_ACPI);
1219 out2:
1220 AcpiOsFree(bufc.Pointer);
1221 out1:
1222 AcpiOsFree(bufp.Pointer);
1223 out:
1224 return rv;
1225 }
1226 #endif /* ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV */
1227