acpi.c revision 1.200 1 /* $NetBSD: acpi.c,v 1.200 2010/06/07 01:45:27 pgoyette 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright 2001, 2003 Wasabi Systems, Inc.
34 * All rights reserved.
35 *
36 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed for the NetBSD Project by
49 * Wasabi Systems, Inc.
50 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51 * or promote products derived from this software without specific prior
52 * written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.200 2010/06/07 01:45:27 pgoyette Exp $");
69
70 #include "opt_acpi.h"
71 #include "opt_pcifixup.h"
72
73 #include <sys/param.h>
74 #include <sys/device.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/module.h>
78 #include <sys/mutex.h>
79 #include <sys/sysctl.h>
80 #include <sys/systm.h>
81
82 #include <dev/acpi/acpireg.h>
83 #include <dev/acpi/acpivar.h>
84 #include <dev/acpi/acpi_osd.h>
85 #include <dev/acpi/acpi_pci.h>
86 #include <dev/acpi/acpi_power.h>
87 #include <dev/acpi/acpi_timer.h>
88 #include <dev/acpi/acpi_wakedev.h>
89
90 #define _COMPONENT ACPI_BUS_COMPONENT
91 ACPI_MODULE_NAME ("acpi")
92
93 #if defined(ACPI_PCI_FIXUP)
94 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED. Please adjust your kernel configuration file.
95 #endif
96
97 #ifdef PCI_INTR_FIXUP_DISABLED
98 #include <dev/pci/pcidevs.h>
99 #endif
100
101 MALLOC_DECLARE(M_ACPI);
102
103 #include <machine/acpi_machdep.h>
104
105 #ifdef ACPI_DEBUGGER
106 #define ACPI_DBGR_INIT 0x01
107 #define ACPI_DBGR_TABLES 0x02
108 #define ACPI_DBGR_ENABLE 0x04
109 #define ACPI_DBGR_PROBE 0x08
110 #define ACPI_DBGR_RUNNING 0x10
111
112 static int acpi_dbgr = 0x00;
113 #endif
114
115 /*
116 * This is a flag we set when the ACPI subsystem is active. Machine
117 * dependent code may wish to skip other steps (such as attaching
118 * subsystems that ACPI supercedes) when ACPI is active.
119 */
120 int acpi_active;
121 int acpi_force_load;
122 int acpi_suspended = 0;
123
124 struct acpi_softc *acpi_softc;
125 static uint64_t acpi_root_pointer;
126 extern kmutex_t acpi_interrupt_list_mtx;
127 static ACPI_HANDLE acpi_scopes[4];
128
129 /*
130 * This structure provides a context for the ACPI
131 * namespace walk performed in acpi_build_tree().
132 */
133 struct acpi_walkcontext {
134 struct acpi_softc *aw_sc;
135 struct acpi_devnode *aw_parent;
136 };
137
138 /*
139 * Ignored HIDs.
140 */
141 static const char * const acpi_ignored_ids[] = {
142 #if defined(i386) || defined(x86_64)
143 "PNP0000", /* AT interrupt controller is handled internally */
144 "PNP0200", /* AT DMA controller is handled internally */
145 "PNP0A??", /* PCI Busses are handled internally */
146 "PNP0B00", /* AT RTC is handled internally */
147 "PNP0C01", /* No "System Board" driver */
148 "PNP0C02", /* No "PnP motherboard register resources" driver */
149 "PNP0C0B", /* No need for "ACPI fan" driver */
150 "PNP0C0F", /* ACPI PCI link devices are handled internally */
151 "IFX0102", /* No driver for Infineon TPM */
152 "INT0800", /* No driver for Intel Firmware Hub device */
153 #endif
154 #if defined(x86_64)
155 "PNP0C04", /* FPU is handled internally */
156 #endif
157 NULL
158 };
159
160 static int acpi_match(device_t, cfdata_t, void *);
161 static int acpi_submatch(device_t, cfdata_t, const int *, void *);
162 static void acpi_attach(device_t, device_t, void *);
163 static int acpi_detach(device_t, int);
164 static void acpi_childdet(device_t, device_t);
165 static bool acpi_suspend(device_t, const pmf_qual_t *);
166 static bool acpi_resume(device_t, const pmf_qual_t *);
167
168 static void acpi_build_tree(struct acpi_softc *);
169 static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, uint32_t,
170 void *, void **);
171 static ACPI_STATUS acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
172 void *, void **);
173
174 #ifdef ACPI_ACTIVATE_DEV
175 static void acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
176 static ACPI_STATUS acpi_allocate_resources(ACPI_HANDLE);
177 #endif
178
179 static int acpi_rescan(device_t, const char *, const int *);
180 static void acpi_rescan1(struct acpi_softc *,
181 const char *, const int *);
182 static void acpi_rescan_nodes(struct acpi_softc *);
183 static void acpi_rescan_capabilities(struct acpi_softc *);
184 static int acpi_print(void *aux, const char *);
185
186 static void acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
187
188 static void acpi_register_fixed_button(struct acpi_softc *, int);
189 static void acpi_deregister_fixed_button(struct acpi_softc *, int);
190 static uint32_t acpi_fixed_button_handler(void *);
191 static void acpi_fixed_button_pressed(void *);
192
193 static void acpi_sleep_init(struct acpi_softc *);
194
195 static int sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS);
196 static int sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
197 static int sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS);
198
199 static bool acpi_is_scope(struct acpi_devnode *);
200 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
201 static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
202
203 extern struct cfdriver acpi_cd;
204
205 /* Handle routine vectors and loading for acpiverbose module */
206 void acpi_null(void);
207
208 void acpi_print_devnodes_stub(struct acpi_softc *);
209 void acpi_print_tree_stub(struct acpi_devnode *, uint32_t);
210 void acpi_print_dev_stub(const char *);
211 void acpi_wmidump_stub(void *);
212
213 void (*acpi_print_devnodes)(struct acpi_softc *) = acpi_print_devnodes_stub;
214 void (*acpi_print_tree)(struct acpi_devnode *, uint32_t) = acpi_print_tree_stub;
215 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
216 void (*acpi_wmidump)(void *) = acpi_wmidump_stub;
217
218 int acpi_verbose_loaded = 0;
219
220 /* acpiverbose support */
221 void
222 acpi_null(void)
223 {
224 /* Nothing to do */
225 }
226
227 void
228 acpi_load_verbose(void)
229 {
230 if (acpi_verbose_loaded)
231 return;
232
233 mutex_enter(&module_lock);
234 if (module_autoload("acpiverbose", MODULE_CLASS_MISC) == 0)
235 acpi_verbose_loaded++;
236 mutex_exit(&module_lock);
237 }
238
239 void acpi_print_devnodes_stub(struct acpi_softc *sc)
240 {
241 acpi_load_verbose();
242 if (acpi_verbose_loaded)
243 acpi_print_devnodes(sc);
244 }
245
246 void acpi_print_tree_stub(struct acpi_devnode *ad, uint32_t level)
247 {
248 acpi_load_verbose();
249 if (acpi_verbose_loaded)
250 acpi_print_tree(ad, level);
251 }
252
253 void acpi_print_dev_stub(const char *pnpstr)
254 {
255 acpi_load_verbose();
256 if (acpi_verbose_loaded)
257 acpi_print_dev(pnpstr);
258 }
259
260 void acpi_wmidump_stub(void *arg)
261 {
262 acpi_load_verbose();
263 if (acpi_verbose_loaded)
264 acpi_wmidump(arg);
265 }
266
267 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
268 acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
269
270 /*
271 * Probe for ACPI support.
272 *
273 * This is called by the machine-dependent ACPI front-end.
274 * Note: this is not an autoconfiguration interface function.
275 */
276 int
277 acpi_probe(void)
278 {
279 ACPI_TABLE_HEADER *rsdt;
280 const char *func;
281 static int once;
282 bool initialized;
283 ACPI_STATUS rv;
284
285 if (once != 0)
286 panic("%s: already probed", __func__);
287
288 once = 1;
289 func = NULL;
290 initialized = false;
291
292 mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
293
294 /*
295 * Start up ACPICA.
296 */
297 #ifdef ACPI_DEBUGGER
298 if (acpi_dbgr & ACPI_DBGR_INIT)
299 acpi_osd_debugger();
300 #endif
301
302 CTASSERT(TRUE == true);
303 CTASSERT(FALSE == false);
304
305 AcpiGbl_AllMethodsSerialized = false;
306 AcpiGbl_EnableInterpreterSlack = true;
307
308 rv = AcpiInitializeSubsystem();
309
310 if (ACPI_SUCCESS(rv))
311 initialized = true;
312 else {
313 func = "AcpiInitializeSubsystem()";
314 goto fail;
315 }
316
317 /*
318 * Allocate space for RSDT/XSDT and DSDT,
319 * but allow resizing if more tables exist.
320 */
321 rv = AcpiInitializeTables(NULL, 2, true);
322
323 if (ACPI_FAILURE(rv)) {
324 func = "AcpiInitializeTables()";
325 goto fail;
326 }
327
328 #ifdef ACPI_DEBUGGER
329 if (acpi_dbgr & ACPI_DBGR_TABLES)
330 acpi_osd_debugger();
331 #endif
332
333 rv = AcpiLoadTables();
334
335 if (ACPI_FAILURE(rv)) {
336 func = "AcpiLoadTables()";
337 goto fail;
338 }
339
340 rsdt = acpi_map_rsdt();
341
342 if (rsdt == NULL) {
343 func = "acpi_map_rsdt()";
344 rv = AE_ERROR;
345 goto fail;
346 }
347
348 if (acpi_force_load == 0 && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
349 aprint_normal("ACPI: BIOS is listed as broken:\n");
350 aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
351 "AslId <%4.4s,%08x>\n",
352 rsdt->OemId, rsdt->OemTableId,
353 rsdt->OemRevision,
354 rsdt->AslCompilerId,
355 rsdt->AslCompilerRevision);
356 aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
357 acpi_unmap_rsdt(rsdt);
358 AcpiTerminate();
359 return 0;
360 }
361
362 acpi_unmap_rsdt(rsdt);
363
364 rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
365
366 if (ACPI_FAILURE(rv)) {
367 func = "AcpiEnableSubsystem()";
368 goto fail;
369 }
370
371 /*
372 * Looks like we have ACPI!
373 */
374 return 1;
375
376 fail:
377 KASSERT(rv != AE_OK);
378 KASSERT(func != NULL);
379
380 aprint_error("%s: failed to probe ACPI: %s\n",
381 func, AcpiFormatException(rv));
382
383 if (initialized != false)
384 (void)AcpiTerminate();
385
386 return 0;
387 }
388
389 int
390 acpi_check(device_t parent, const char *ifattr)
391 {
392 return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
393 }
394
395 /*
396 * Autoconfiguration.
397 */
398 static int
399 acpi_match(device_t parent, cfdata_t match, void *aux)
400 {
401 /*
402 * XXX: Nada; MD code has called acpi_probe().
403 */
404 return 1;
405 }
406
407 static int
408 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
409 {
410 struct cfattach *ca;
411
412 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
413
414 return (ca == &acpi_ca);
415 }
416
417 static void
418 acpi_attach(device_t parent, device_t self, void *aux)
419 {
420 struct acpi_softc *sc = device_private(self);
421 struct acpibus_attach_args *aa = aux;
422 ACPI_TABLE_HEADER *rsdt;
423 ACPI_STATUS rv;
424
425 aprint_naive("\n");
426 aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
427
428 if (acpi_softc != NULL)
429 panic("%s: already attached", __func__);
430
431 rsdt = acpi_map_rsdt();
432
433 if (rsdt == NULL)
434 aprint_error_dev(self, "X/RSDT: Not found\n");
435 else {
436 aprint_verbose_dev(self,
437 "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
438 rsdt->OemId, rsdt->OemTableId,
439 rsdt->OemRevision,
440 rsdt->AslCompilerId, rsdt->AslCompilerRevision);
441 }
442
443 acpi_unmap_rsdt(rsdt);
444
445 sc->sc_dev = self;
446 sc->sc_root = NULL;
447
448 sc->sc_sleepstate = ACPI_STATE_S0;
449 sc->sc_quirks = acpi_find_quirks();
450
451 sysmon_power_settype("acpi");
452
453 sc->sc_iot = aa->aa_iot;
454 sc->sc_memt = aa->aa_memt;
455 sc->sc_pc = aa->aa_pc;
456 sc->sc_pciflags = aa->aa_pciflags;
457 sc->sc_ic = aa->aa_ic;
458
459 SIMPLEQ_INIT(&sc->ad_head);
460
461 acpi_softc = sc;
462
463 if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
464 aprint_error_dev(self, "couldn't establish power handler\n");
465
466 /*
467 * Bring ACPI on-line.
468 */
469 #ifdef ACPI_DEBUGGER
470 if (acpi_dbgr & ACPI_DBGR_ENABLE)
471 acpi_osd_debugger();
472 #endif
473
474 #define ACPI_ENABLE_PHASE1 \
475 (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
476 #define ACPI_ENABLE_PHASE2 \
477 (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
478 ACPI_NO_ADDRESS_SPACE_INIT)
479
480 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
481
482 if (ACPI_FAILURE(rv))
483 goto fail;
484
485 acpi_md_callback();
486
487 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
488
489 if (ACPI_FAILURE(rv))
490 goto fail;
491
492 /*
493 * Early EC handler initialization if ECDT table is available.
494 */
495 config_found_ia(self, "acpiecdtbus", aa, NULL);
496
497 rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
498
499 if (ACPI_FAILURE(rv))
500 goto fail;
501
502 /*
503 * Install global notify handlers.
504 */
505 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
506 ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
507
508 if (ACPI_FAILURE(rv))
509 goto fail;
510
511 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
512 ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
513
514 if (ACPI_FAILURE(rv))
515 goto fail;
516
517 acpi_active = 1;
518
519 /* Show SCI interrupt. */
520 aprint_verbose_dev(self, "SCI interrupting at int %u\n",
521 AcpiGbl_FADT.SciInterrupt);
522
523 /*
524 * Install fixed-event handlers.
525 */
526 acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
527 acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
528
529 acpitimer_init();
530
531 #ifdef ACPI_DEBUGGER
532 if (acpi_dbgr & ACPI_DBGR_PROBE)
533 acpi_osd_debugger();
534 #endif
535
536 /*
537 * Scan the namespace and build our device tree.
538 */
539 acpi_build_tree(sc);
540 acpi_sleep_init(sc);
541
542 #ifdef ACPI_DEBUGGER
543 if (acpi_dbgr & ACPI_DBGR_RUNNING)
544 acpi_osd_debugger();
545 #endif
546
547 #ifdef ACPI_DEBUG
548 acpi_debug_init();
549 #endif
550
551 return;
552
553 fail:
554 KASSERT(rv != AE_OK);
555
556 aprint_error("%s: failed to initialize ACPI: %s\n",
557 __func__, AcpiFormatException(rv));
558 }
559
560 /*
561 * XXX: This is incomplete.
562 */
563 static int
564 acpi_detach(device_t self, int flags)
565 {
566 struct acpi_softc *sc = device_private(self);
567 ACPI_STATUS rv;
568 int rc;
569
570 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
571 ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
572
573 if (ACPI_FAILURE(rv))
574 return EBUSY;
575
576 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
577 ACPI_DEVICE_NOTIFY, acpi_notify_handler);
578
579 if (ACPI_FAILURE(rv))
580 return EBUSY;
581
582 if ((rc = config_detach_children(self, flags)) != 0)
583 return rc;
584
585 if ((rc = acpitimer_detach()) != 0)
586 return rc;
587
588 acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
589 acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
590
591 pmf_device_deregister(self);
592
593 acpi_softc = NULL;
594
595 return 0;
596 }
597
598 /*
599 * XXX: Need to reclaim any resources? Yes.
600 */
601 static void
602 acpi_childdet(device_t self, device_t child)
603 {
604 struct acpi_softc *sc = device_private(self);
605 struct acpi_devnode *ad;
606
607 if (sc->sc_apmbus == child)
608 sc->sc_apmbus = NULL;
609
610 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
611
612 if (ad->ad_device == child)
613 ad->ad_device = NULL;
614 }
615 }
616
617 static bool
618 acpi_suspend(device_t dv, const pmf_qual_t *qual)
619 {
620
621 acpi_suspended = 1;
622
623 return true;
624 }
625
626 static bool
627 acpi_resume(device_t dv, const pmf_qual_t *qual)
628 {
629
630 acpi_suspended = 0;
631
632 return true;
633 }
634
635 /*
636 * Namespace scan.
637 */
638 static void
639 acpi_build_tree(struct acpi_softc *sc)
640 {
641 struct acpi_walkcontext awc;
642
643 /*
644 * Get the root scope handles.
645 */
646 KASSERT(__arraycount(acpi_scopes) == 4);
647
648 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
649 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
650 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
651 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
652
653 /*
654 * Make the root node.
655 */
656 awc.aw_sc = sc;
657 awc.aw_parent = NULL;
658
659 (void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
660
661 KASSERT(sc->sc_root == NULL);
662 KASSERT(awc.aw_parent != NULL);
663
664 sc->sc_root = awc.aw_parent;
665
666 /*
667 * Build the internal namespace.
668 */
669 (void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
670 acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
671
672 /*
673 * Scan the internal namespace.
674 */
675 acpi_rescan1(sc, NULL, NULL);
676 acpi_rescan_capabilities(sc);
677
678 (void)acpi_pcidev_scan(sc->sc_root);
679
680 acpi_print_devnodes(sc);
681 acpi_print_tree(sc->sc_root, 0);
682 }
683
684 static ACPI_STATUS
685 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
686 void *context, void **status)
687 {
688 struct acpi_walkcontext *awc = context;
689 struct acpi_softc *sc = awc->aw_sc;
690 struct acpi_devnode *ad;
691 ACPI_DEVICE_INFO *devinfo;
692 ACPI_OBJECT_TYPE type;
693 ACPI_NAME_UNION *anu;
694 ACPI_STATUS rv;
695 int clear, i;
696
697 rv = AcpiGetObjectInfo(handle, &devinfo);
698
699 if (ACPI_FAILURE(rv))
700 return AE_OK; /* Do not terminate the walk. */
701
702 type = devinfo->Type;
703
704 switch (type) {
705
706 case ACPI_TYPE_DEVICE:
707
708 #ifdef ACPI_ACTIVATE_DEV
709 acpi_activate_device(handle, &devinfo);
710 #endif
711
712 case ACPI_TYPE_PROCESSOR:
713 case ACPI_TYPE_THERMAL:
714 case ACPI_TYPE_POWER:
715
716 ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT | M_ZERO);
717
718 if (ad == NULL)
719 return AE_NO_MEMORY;
720
721 ad->ad_device = NULL;
722 ad->ad_notify = NULL;
723 ad->ad_pciinfo = NULL;
724
725 ad->ad_type = type;
726 ad->ad_handle = handle;
727 ad->ad_devinfo = devinfo;
728
729 ad->ad_root = sc->sc_dev;
730 ad->ad_parent = awc->aw_parent;
731
732 anu = (ACPI_NAME_UNION *)&devinfo->Name;
733 ad->ad_name[4] = '\0';
734
735 for (i = 3, clear = 0; i >= 0; i--) {
736
737 if (clear == 0 && anu->Ascii[i] == '_')
738 ad->ad_name[i] = '\0';
739 else {
740 ad->ad_name[i] = anu->Ascii[i];
741 clear = 1;
742 }
743 }
744
745 if (ad->ad_name[0] == '\0')
746 ad->ad_name[0] = '_';
747
748 SIMPLEQ_INIT(&ad->ad_child_head);
749 SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
750
751 if (ad->ad_parent != NULL) {
752
753 SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
754 ad, ad_child_list);
755 }
756
757 awc->aw_parent = ad;
758 }
759
760 return AE_OK;
761 }
762
763 static ACPI_STATUS
764 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
765 void *context, void **status)
766 {
767 struct acpi_walkcontext *awc = context;
768
769 KASSERT(awc != NULL);
770 KASSERT(awc->aw_parent != NULL);
771
772 if (handle == awc->aw_parent->ad_handle)
773 awc->aw_parent = awc->aw_parent->ad_parent;
774
775 return AE_OK;
776 }
777
778 #ifdef ACPI_ACTIVATE_DEV
779 static void
780 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
781 {
782 static const int valid = ACPI_VALID_STA | ACPI_VALID_HID;
783 ACPI_DEVICE_INFO *newdi;
784 ACPI_STATUS rv;
785 uint32_t old;
786
787 /*
788 * If the device is valid and present,
789 * but not enabled, try to activate it.
790 */
791 if (((*di)->Valid & valid) != valid)
792 return;
793
794 old = (*di)->CurrentStatus;
795
796 if ((old & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED)) !=
797 ACPI_STA_DEVICE_PRESENT)
798 return;
799
800 rv = acpi_allocate_resources(handle);
801
802 if (ACPI_FAILURE(rv))
803 goto fail;
804
805 rv = AcpiGetObjectInfo(handle, &newdi);
806
807 if (ACPI_FAILURE(rv))
808 goto fail;
809
810 ACPI_FREE(*di);
811 *di = newdi;
812
813 aprint_verbose_dev(acpi_softc->sc_dev,
814 "%s activated, STA 0x%08X -> STA 0x%08X\n",
815 (*di)->HardwareId.String, old, (*di)->CurrentStatus);
816
817 return;
818
819 fail:
820 aprint_error_dev(acpi_softc->sc_dev, "failed to "
821 "activate %s\n", (*di)->HardwareId.String);
822 }
823
824 /*
825 * XXX: This very incomplete.
826 */
827 ACPI_STATUS
828 acpi_allocate_resources(ACPI_HANDLE handle)
829 {
830 ACPI_BUFFER bufp, bufc, bufn;
831 ACPI_RESOURCE *resp, *resc, *resn;
832 ACPI_RESOURCE_IRQ *irq;
833 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
834 ACPI_STATUS rv;
835 uint delta;
836
837 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
838 if (ACPI_FAILURE(rv))
839 goto out;
840 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
841 if (ACPI_FAILURE(rv)) {
842 goto out1;
843 }
844
845 bufn.Length = 1000;
846 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
847 resp = bufp.Pointer;
848 resc = bufc.Pointer;
849 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
850 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
851 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
852 resp = ACPI_NEXT_RESOURCE(resp);
853 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
854 break;
855 /* Found identical Id */
856 resn->Type = resc->Type;
857 switch (resc->Type) {
858 case ACPI_RESOURCE_TYPE_IRQ:
859 memcpy(&resn->Data, &resp->Data,
860 sizeof(ACPI_RESOURCE_IRQ));
861 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
862 irq->Interrupts[0] =
863 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
864 Interrupts[irq->InterruptCount-1];
865 irq->InterruptCount = 1;
866 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
867 break;
868 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
869 memcpy(&resn->Data, &resp->Data,
870 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
871 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
872 #if 0
873 /*
874 * XXX: Not duplicating the interrupt logic above
875 * because its not clear what it accomplishes.
876 */
877 xirq->Interrupts[0] =
878 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
879 Interrupts[irq->NumberOfInterrupts-1];
880 xirq->NumberOfInterrupts = 1;
881 #endif
882 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
883 break;
884 case ACPI_RESOURCE_TYPE_IO:
885 memcpy(&resn->Data, &resp->Data,
886 sizeof(ACPI_RESOURCE_IO));
887 resn->Length = resp->Length;
888 break;
889 default:
890 aprint_error_dev(acpi_softc->sc_dev,
891 "%s: invalid type %u\n", __func__, resc->Type);
892 rv = AE_BAD_DATA;
893 goto out2;
894 }
895 resc = ACPI_NEXT_RESOURCE(resc);
896 resn = ACPI_NEXT_RESOURCE(resn);
897 resp = ACPI_NEXT_RESOURCE(resp);
898 delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
899 if (delta >=
900 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
901 bufn.Length *= 2;
902 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
903 M_ACPI, M_WAITOK);
904 resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
905 delta);
906 }
907 }
908
909 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
910 aprint_error_dev(acpi_softc->sc_dev,
911 "%s: resc not exhausted\n", __func__);
912 rv = AE_BAD_DATA;
913 goto out3;
914 }
915
916 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
917 rv = AcpiSetCurrentResources(handle, &bufn);
918
919 if (ACPI_FAILURE(rv))
920 aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
921 "resources: %s\n", __func__, AcpiFormatException(rv));
922
923 out3:
924 free(bufn.Pointer, M_ACPI);
925 out2:
926 ACPI_FREE(bufc.Pointer);
927 out1:
928 ACPI_FREE(bufp.Pointer);
929 out:
930 return rv;
931 }
932 #endif /* ACPI_ACTIVATE_DEV */
933
934 /*
935 * Device attachment.
936 */
937 static int
938 acpi_rescan(device_t self, const char *ifattr, const int *locators)
939 {
940 struct acpi_softc *sc = device_private(self);
941
942 acpi_rescan1(sc, ifattr, locators);
943
944 return 0;
945 }
946
947 static void
948 acpi_rescan1(struct acpi_softc *sc, const char *ifattr, const int *locators)
949 {
950
951 if (ifattr_match(ifattr, "acpinodebus"))
952 acpi_rescan_nodes(sc);
953
954 if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
955 sc->sc_apmbus = config_found_ia(sc->sc_dev,
956 "acpiapmbus", NULL, NULL);
957 }
958
959 static void
960 acpi_rescan_nodes(struct acpi_softc *sc)
961 {
962 struct acpi_attach_args aa;
963 struct acpi_devnode *ad;
964 ACPI_DEVICE_INFO *di;
965
966 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
967
968 if (ad->ad_device != NULL)
969 continue;
970
971 /*
972 * There is a bug in ACPICA: it defines the type
973 * of the scopes incorrectly for its own reasons.
974 */
975 if (acpi_is_scope(ad) != false)
976 continue;
977
978 di = ad->ad_devinfo;
979
980 /*
981 * We only attach devices which are present, enabled, and
982 * functioning properly. However, if a device is enabled,
983 * it is decoding resources and we should claim these,
984 * if possible. This requires changes to bus_space(9).
985 */
986 if (di->Type == ACPI_TYPE_DEVICE) {
987
988 if ((di->Valid & ACPI_VALID_STA) != 0 &&
989 (di->CurrentStatus & ACPI_STA_OK) != ACPI_STA_OK)
990 continue;
991 }
992
993 /*
994 * The same problem as above. As for example
995 * thermal zones and power resources do not
996 * have a valid HID, only evaluate devices.
997 */
998 if (di->Type == ACPI_TYPE_DEVICE &&
999 (di->Valid & ACPI_VALID_HID) == 0)
1000 continue;
1001
1002 /*
1003 * Handled internally.
1004 */
1005 if (di->Type == ACPI_TYPE_PROCESSOR)
1006 continue;
1007
1008 /*
1009 * Ditto, but bind power resources.
1010 */
1011 if (di->Type == ACPI_TYPE_POWER) {
1012 acpi_power_res_add(ad);
1013 continue;
1014 }
1015
1016 /*
1017 * Skip ignored HIDs.
1018 */
1019 if (acpi_match_hid(di, acpi_ignored_ids))
1020 continue;
1021
1022 aa.aa_node = ad;
1023 aa.aa_iot = sc->sc_iot;
1024 aa.aa_memt = sc->sc_memt;
1025 aa.aa_pc = sc->sc_pc;
1026 aa.aa_pciflags = sc->sc_pciflags;
1027 aa.aa_ic = sc->sc_ic;
1028
1029 ad->ad_device = config_found_ia(sc->sc_dev,
1030 "acpinodebus", &aa, acpi_print);
1031 }
1032 }
1033
1034 static void
1035 acpi_rescan_capabilities(struct acpi_softc *sc)
1036 {
1037 struct acpi_devnode *ad;
1038 ACPI_HANDLE tmp;
1039 ACPI_STATUS rv;
1040
1041 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1042
1043 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
1044 continue;
1045
1046 /*
1047 * Scan power resource capabilities.
1048 *
1049 * If any power states are supported,
1050 * at least _PR0 and _PR3 must be present.
1051 */
1052 rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
1053
1054 if (ACPI_SUCCESS(rv)) {
1055 ad->ad_flags |= ACPI_DEVICE_POWER;
1056 acpi_power_add(ad);
1057 }
1058
1059 /*
1060 * Scan wake-up capabilities.
1061 */
1062 rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
1063
1064 if (ACPI_SUCCESS(rv)) {
1065 ad->ad_flags |= ACPI_DEVICE_WAKEUP;
1066 acpi_wakedev_add(ad);
1067 }
1068 }
1069 }
1070
1071 static int
1072 acpi_print(void *aux, const char *pnp)
1073 {
1074 struct acpi_attach_args *aa = aux;
1075 ACPI_STATUS rv;
1076
1077 if (pnp) {
1078 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1079 char *pnpstr =
1080 aa->aa_node->ad_devinfo->HardwareId.String;
1081 ACPI_BUFFER buf;
1082
1083 aprint_normal("%s (%s) ", aa->aa_node->ad_name,
1084 pnpstr);
1085
1086 rv = acpi_eval_struct(aa->aa_node->ad_handle,
1087 "_STR", &buf);
1088 if (ACPI_SUCCESS(rv)) {
1089 ACPI_OBJECT *obj = buf.Pointer;
1090 switch (obj->Type) {
1091 case ACPI_TYPE_STRING:
1092 aprint_normal("[%s] ", obj->String.Pointer);
1093 break;
1094 case ACPI_TYPE_BUFFER:
1095 aprint_normal("buffer %p ", obj->Buffer.Pointer);
1096 break;
1097 default:
1098 aprint_normal("type %u ",obj->Type);
1099 break;
1100 }
1101 ACPI_FREE(buf.Pointer);
1102 }
1103 else
1104 acpi_print_dev(pnpstr);
1105
1106 aprint_normal("at %s", pnp);
1107 } else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
1108 aprint_normal("%s (ACPI Object Type '%s' "
1109 "[0x%02x]) ", aa->aa_node->ad_name,
1110 AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
1111 aa->aa_node->ad_devinfo->Type);
1112 aprint_normal("at %s", pnp);
1113 } else
1114 return 0;
1115 } else {
1116 aprint_normal(" (%s", aa->aa_node->ad_name);
1117 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1118 aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
1119 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
1120 const char *uid;
1121
1122 uid = aa->aa_node->ad_devinfo->UniqueId.String;
1123 if (uid[0] == '\0')
1124 uid = "<null>";
1125 aprint_normal("-%s", uid);
1126 }
1127 }
1128 aprint_normal(")");
1129 }
1130
1131 return UNCONF;
1132 }
1133
1134 /*
1135 * Notify.
1136 */
1137 static void
1138 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
1139 {
1140 struct acpi_softc *sc = acpi_softc;
1141 struct acpi_devnode *ad;
1142
1143 KASSERT(sc != NULL);
1144 KASSERT(aux == NULL);
1145 KASSERT(acpi_active != 0);
1146
1147 if (acpi_suspended != 0)
1148 return;
1149
1150 /*
1151 * System: 0x00 - 0x7F.
1152 * Device: 0x80 - 0xFF.
1153 */
1154 switch (event) {
1155
1156 case ACPI_NOTIFY_BUS_CHECK:
1157 case ACPI_NOTIFY_DEVICE_CHECK:
1158 case ACPI_NOTIFY_DEVICE_WAKE:
1159 case ACPI_NOTIFY_EJECT_REQUEST:
1160 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
1161 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1162 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1163 case ACPI_NOTIFY_POWER_FAULT:
1164 case ACPI_NOTIFY_CAPABILITIES_CHECK:
1165 case ACPI_NOTIFY_DEVICE_PLD_CHECK:
1166 case ACPI_NOTIFY_RESERVED:
1167 case ACPI_NOTIFY_LOCALITY_UPDATE:
1168 break;
1169 }
1170
1171 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
1172 "%s (%p)\n", event, acpi_name(handle), handle));
1173
1174 /*
1175 * We deliver notifications only to drivers
1176 * that have been succesfully attached and
1177 * that have registered a handler with us.
1178 * The opaque pointer is always the device_t.
1179 */
1180 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1181
1182 if (ad->ad_device == NULL)
1183 continue;
1184
1185 if (ad->ad_notify == NULL)
1186 continue;
1187
1188 if (ad->ad_handle != handle)
1189 continue;
1190
1191 (*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
1192
1193 return;
1194 }
1195
1196 aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
1197 "for %s (%p)\n", event, acpi_name(handle), handle);
1198 }
1199
1200 bool
1201 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
1202 {
1203 struct acpi_softc *sc = acpi_softc;
1204
1205 KASSERT(sc != NULL);
1206 KASSERT(acpi_active != 0);
1207
1208 if (acpi_suspended != 0)
1209 goto fail;
1210
1211 if (ad == NULL || notify == NULL)
1212 goto fail;
1213
1214 ad->ad_notify = notify;
1215
1216 return true;
1217
1218 fail:
1219 aprint_error_dev(sc->sc_dev, "failed to register notify "
1220 "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
1221
1222 return false;
1223 }
1224
1225 void
1226 acpi_deregister_notify(struct acpi_devnode *ad)
1227 {
1228
1229 ad->ad_notify = NULL;
1230 }
1231
1232 /*
1233 * Fixed buttons.
1234 */
1235 static void
1236 acpi_register_fixed_button(struct acpi_softc *sc, int event)
1237 {
1238 struct sysmon_pswitch *smpsw;
1239 ACPI_STATUS rv;
1240 int type;
1241
1242 switch (event) {
1243
1244 case ACPI_EVENT_POWER_BUTTON:
1245
1246 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
1247 return;
1248
1249 type = PSWITCH_TYPE_POWER;
1250 smpsw = &sc->sc_smpsw_power;
1251 break;
1252
1253 case ACPI_EVENT_SLEEP_BUTTON:
1254
1255 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
1256 return;
1257
1258 type = PSWITCH_TYPE_SLEEP;
1259 smpsw = &sc->sc_smpsw_sleep;
1260 break;
1261
1262 default:
1263 rv = AE_TYPE;
1264 goto fail;
1265 }
1266
1267 smpsw->smpsw_type = type;
1268 smpsw->smpsw_name = device_xname(sc->sc_dev);
1269
1270 if (sysmon_pswitch_register(smpsw) != 0) {
1271 rv = AE_ERROR;
1272 goto fail;
1273 }
1274
1275 rv = AcpiInstallFixedEventHandler(event,
1276 acpi_fixed_button_handler, smpsw);
1277
1278 if (ACPI_FAILURE(rv)) {
1279 sysmon_pswitch_unregister(smpsw);
1280 goto fail;
1281 }
1282
1283 aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
1284 (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
1285
1286 return;
1287
1288 fail:
1289 aprint_error_dev(sc->sc_dev, "failed to register "
1290 "fixed event: %s\n", AcpiFormatException(rv));
1291 }
1292
1293 static void
1294 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
1295 {
1296 struct sysmon_pswitch *smpsw;
1297 ACPI_STATUS rv;
1298
1299 switch (event) {
1300
1301 case ACPI_EVENT_POWER_BUTTON:
1302 smpsw = &sc->sc_smpsw_power;
1303
1304 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
1305 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
1306 return;
1307 }
1308
1309 break;
1310
1311 case ACPI_EVENT_SLEEP_BUTTON:
1312 smpsw = &sc->sc_smpsw_sleep;
1313
1314 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
1315 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
1316 return;
1317 }
1318
1319 break;
1320
1321 default:
1322 rv = AE_TYPE;
1323 goto fail;
1324 }
1325
1326 rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
1327
1328 if (ACPI_SUCCESS(rv)) {
1329 sysmon_pswitch_unregister(smpsw);
1330 return;
1331 }
1332
1333 fail:
1334 aprint_error_dev(sc->sc_dev, "failed to deregister "
1335 "fixed event: %s\n", AcpiFormatException(rv));
1336 }
1337
1338 static uint32_t
1339 acpi_fixed_button_handler(void *context)
1340 {
1341 static const int handler = OSL_NOTIFY_HANDLER;
1342 struct sysmon_pswitch *smpsw = context;
1343
1344 (void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
1345
1346 return ACPI_INTERRUPT_HANDLED;
1347 }
1348
1349 static void
1350 acpi_fixed_button_pressed(void *context)
1351 {
1352 struct sysmon_pswitch *smpsw = context;
1353
1354 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
1355 (smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
1356 "power" : "sleep"));
1357
1358 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1359 }
1360
1361 /*
1362 * Sleep.
1363 */
1364 static void
1365 acpi_sleep_init(struct acpi_softc *sc)
1366 {
1367 uint8_t a, b, i;
1368 ACPI_STATUS rv;
1369
1370 CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
1371 CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
1372 CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
1373
1374 /*
1375 * Evaluate supported sleep states.
1376 */
1377 for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
1378
1379 rv = AcpiGetSleepTypeData(i, &a, &b);
1380
1381 if (ACPI_SUCCESS(rv))
1382 sc->sc_sleepstates |= __BIT(i);
1383 }
1384 }
1385
1386 void
1387 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1388 {
1389 ACPI_STATUS rv;
1390 int err;
1391
1392 if (state == sc->sc_sleepstate)
1393 return;
1394
1395 aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
1396
1397 switch (state) {
1398
1399 case ACPI_STATE_S0:
1400 sc->sc_sleepstate = ACPI_STATE_S0;
1401 return;
1402
1403 case ACPI_STATE_S1:
1404 case ACPI_STATE_S2:
1405 case ACPI_STATE_S3:
1406 case ACPI_STATE_S4:
1407
1408 if ((sc->sc_sleepstates & __BIT(state)) == 0) {
1409 aprint_error_dev(sc->sc_dev, "sleep state "
1410 "S%d is not available\n", state);
1411 return;
1412 }
1413
1414 /*
1415 * Evaluate the _TTS method. This should be done before
1416 * pmf_system_suspend(9) and the evaluation of _PTS.
1417 * We should also re-evaluate this once we return to
1418 * S0 or if we abort the sleep state transition in the
1419 * middle (see ACPI 3.0, section 7.3.6). In reality,
1420 * however, the _TTS method is seldom seen in the field.
1421 */
1422 rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
1423
1424 if (ACPI_SUCCESS(rv))
1425 aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
1426
1427 acpi_wakedev_commit(sc, state);
1428
1429 if (state != ACPI_STATE_S1 &&
1430 pmf_system_suspend(PMF_Q_NONE) != true) {
1431 aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1432 break;
1433 }
1434
1435 /*
1436 * This will evaluate the _PTS and _SST methods,
1437 * but unlike the documentation claims, not _GTS,
1438 * which is evaluated in AcpiEnterSleepState().
1439 *
1440 * This must be called with interrupts enabled.
1441 */
1442 rv = AcpiEnterSleepStatePrep(state);
1443
1444 if (ACPI_FAILURE(rv)) {
1445 aprint_error_dev(sc->sc_dev, "failed to prepare "
1446 "S%d: %s\n", state, AcpiFormatException(rv));
1447 break;
1448 }
1449
1450 sc->sc_sleepstate = state;
1451
1452 if (state == ACPI_STATE_S1) {
1453
1454 /* Just enter the state. */
1455 acpi_md_OsDisableInterrupt();
1456 rv = AcpiEnterSleepState(state);
1457
1458 if (ACPI_FAILURE(rv))
1459 aprint_error_dev(sc->sc_dev, "failed to "
1460 "enter S1: %s\n", AcpiFormatException(rv));
1461
1462 (void)AcpiLeaveSleepState(state);
1463
1464 } else {
1465
1466 err = acpi_md_sleep(state);
1467
1468 if (state == ACPI_STATE_S4)
1469 AcpiEnable();
1470
1471 pmf_system_bus_resume(PMF_Q_NONE);
1472 (void)AcpiLeaveSleepState(state);
1473 pmf_system_resume(PMF_Q_NONE);
1474 }
1475
1476 break;
1477
1478 case ACPI_STATE_S5:
1479
1480 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
1481
1482 rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1483
1484 if (ACPI_FAILURE(rv)) {
1485 aprint_error_dev(sc->sc_dev, "failed to prepare "
1486 "S%d: %s\n", state, AcpiFormatException(rv));
1487 break;
1488 }
1489
1490 DELAY(1000000);
1491
1492 sc->sc_sleepstate = state;
1493 acpi_md_OsDisableInterrupt();
1494
1495 (void)AcpiEnterSleepState(ACPI_STATE_S5);
1496
1497 aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
1498
1499 break;
1500 }
1501
1502 sc->sc_sleepstate = ACPI_STATE_S0;
1503
1504 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
1505 }
1506
1507 /*
1508 * Sysctl.
1509 */
1510 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1511 {
1512 const struct sysctlnode *mnode, *rnode;
1513 int err;
1514
1515 err = sysctl_createv(clog, 0, NULL, &rnode,
1516 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
1517 NULL, NULL, 0, NULL, 0,
1518 CTL_HW, CTL_EOL);
1519
1520 if (err != 0)
1521 return;
1522
1523 err = sysctl_createv(clog, 0, &rnode, &rnode,
1524 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1525 "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
1526 NULL, 0, NULL, 0,
1527 CTL_CREATE, CTL_EOL);
1528
1529 if (err != 0)
1530 return;
1531
1532 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1533 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1534 "root", SYSCTL_DESCR("ACPI root pointer"),
1535 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1536 CTL_CREATE, CTL_EOL);
1537
1538 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1539 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
1540 "supported_states", SYSCTL_DESCR("Supported system states"),
1541 sysctl_hw_acpi_sleepstates, 0, NULL, 0,
1542 CTL_CREATE, CTL_EOL);
1543
1544 err = sysctl_createv(NULL, 0, NULL, &mnode,
1545 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
1546 NULL, NULL, 0, NULL, 0,
1547 CTL_MACHDEP, CTL_EOL);
1548
1549 if (err == 0) {
1550
1551 (void)sysctl_createv(NULL, 0, &mnode, NULL,
1552 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
1553 "sleep_state", SYSCTL_DESCR("System sleep state"),
1554 sysctl_hw_acpi_sleepstate, 0, NULL, 0,
1555 CTL_CREATE, CTL_EOL);
1556 }
1557
1558 err = sysctl_createv(clog, 0, &rnode, &rnode,
1559 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1560 "stat", SYSCTL_DESCR("ACPI statistics"),
1561 NULL, 0, NULL, 0,
1562 CTL_CREATE, CTL_EOL);
1563
1564 if (err != 0)
1565 return;
1566
1567 (void)sysctl_createv(clog, 0, &rnode, NULL,
1568 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1569 "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
1570 NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
1571 CTL_CREATE, CTL_EOL);
1572
1573 (void)sysctl_createv(clog, 0, &rnode, NULL,
1574 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1575 "sci", SYSCTL_DESCR("Number of SCI interrupts"),
1576 NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
1577 CTL_CREATE, CTL_EOL);
1578
1579 (void)sysctl_createv(clog, 0, &rnode, NULL,
1580 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1581 "fixed", SYSCTL_DESCR("Number of fixed events"),
1582 sysctl_hw_acpi_fixedstats, 0, NULL, 0,
1583 CTL_CREATE, CTL_EOL);
1584
1585 (void)sysctl_createv(clog, 0, &rnode, NULL,
1586 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1587 "method", SYSCTL_DESCR("Number of methods executed"),
1588 NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
1589 CTL_CREATE, CTL_EOL);
1590
1591 CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
1592 CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
1593 }
1594
1595 static int
1596 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
1597 {
1598 struct sysctlnode node;
1599 uint64_t t;
1600 int err, i;
1601
1602 for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
1603 t += AcpiFixedEventCount[i];
1604
1605 node = *rnode;
1606 node.sysctl_data = &t;
1607
1608 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1609
1610 if (err || newp == NULL)
1611 return err;
1612
1613 return 0;
1614 }
1615
1616 static int
1617 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1618 {
1619 struct acpi_softc *sc = acpi_softc;
1620 struct sysctlnode node;
1621 int err, t;
1622
1623 if (acpi_softc == NULL)
1624 return ENOSYS;
1625
1626 node = *rnode;
1627 t = sc->sc_sleepstate;
1628 node.sysctl_data = &t;
1629
1630 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1631
1632 if (err || newp == NULL)
1633 return err;
1634
1635 if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
1636 return EINVAL;
1637
1638 acpi_enter_sleep_state(sc, t);
1639
1640 return 0;
1641 }
1642
1643 static int
1644 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
1645 {
1646 struct acpi_softc *sc = acpi_softc;
1647 struct sysctlnode node;
1648 char t[3 * 6 + 1];
1649 int err;
1650
1651 if (acpi_softc == NULL)
1652 return ENOSYS;
1653
1654 (void)memset(t, '\0', sizeof(t));
1655
1656 (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
1657 ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
1658 ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
1659 ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
1660 ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
1661 ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
1662 ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
1663
1664 node = *rnode;
1665 node.sysctl_data = &t;
1666
1667 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1668
1669 if (err || newp == NULL)
1670 return err;
1671
1672 return 0;
1673 }
1674
1675 /*
1676 * Miscellaneous.
1677 */
1678 static bool
1679 acpi_is_scope(struct acpi_devnode *ad)
1680 {
1681 int i;
1682
1683 /*
1684 * Return true if the node is a root scope.
1685 */
1686 if (ad->ad_parent == NULL)
1687 return false;
1688
1689 if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
1690 return false;
1691
1692 for (i = 0; i < __arraycount(acpi_scopes); i++) {
1693
1694 if (acpi_scopes[i] == NULL)
1695 continue;
1696
1697 if (ad->ad_handle == acpi_scopes[i])
1698 return true;
1699 }
1700
1701 return false;
1702 }
1703
1704 ACPI_PHYSICAL_ADDRESS
1705 acpi_OsGetRootPointer(void)
1706 {
1707 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
1708
1709 /*
1710 * We let MD code handle this since there are multiple ways to do it:
1711 *
1712 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
1713 *
1714 * IA-64: Use the EFI.
1715 */
1716 PhysicalAddress = acpi_md_OsGetRootPointer();
1717
1718 if (acpi_root_pointer == 0)
1719 acpi_root_pointer = PhysicalAddress;
1720
1721 return PhysicalAddress;
1722 }
1723
1724 static ACPI_TABLE_HEADER *
1725 acpi_map_rsdt(void)
1726 {
1727 ACPI_PHYSICAL_ADDRESS paddr;
1728 ACPI_TABLE_RSDP *rsdp;
1729
1730 paddr = AcpiOsGetRootPointer();
1731
1732 if (paddr == 0)
1733 return NULL;
1734
1735 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1736
1737 if (rsdp == NULL)
1738 return NULL;
1739
1740 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1741 paddr = rsdp->XsdtPhysicalAddress;
1742 else
1743 paddr = rsdp->RsdtPhysicalAddress;
1744
1745 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1746
1747 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1748 }
1749
1750 static void
1751 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1752 {
1753
1754 if (rsdt == NULL)
1755 return;
1756
1757 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1758 }
1759