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