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