acpi.c revision 1.201 1 /* $NetBSD: acpi.c,v 1.201 2010/06/07 13:04:31 jruoho 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.201 2010/06/07 13:04:31 jruoho 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_POWER ||
1006 di->Type == ACPI_TYPE_PROCESSOR)
1007 continue;
1008
1009 /*
1010 * Skip ignored HIDs.
1011 */
1012 if (acpi_match_hid(di, acpi_ignored_ids))
1013 continue;
1014
1015 aa.aa_node = ad;
1016 aa.aa_iot = sc->sc_iot;
1017 aa.aa_memt = sc->sc_memt;
1018 aa.aa_pc = sc->sc_pc;
1019 aa.aa_pciflags = sc->sc_pciflags;
1020 aa.aa_ic = sc->sc_ic;
1021
1022 ad->ad_device = config_found_ia(sc->sc_dev,
1023 "acpinodebus", &aa, acpi_print);
1024 }
1025 }
1026
1027 static void
1028 acpi_rescan_capabilities(struct acpi_softc *sc)
1029 {
1030 struct acpi_devnode *ad;
1031 ACPI_HANDLE tmp;
1032 ACPI_STATUS rv;
1033
1034 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1035
1036 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
1037 continue;
1038
1039 /*
1040 * Scan power resource capabilities.
1041 *
1042 * If any power states are supported,
1043 * at least _PR0 and _PR3 must be present.
1044 */
1045 rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
1046
1047 if (ACPI_SUCCESS(rv)) {
1048 ad->ad_flags |= ACPI_DEVICE_POWER;
1049 acpi_power_add(ad);
1050 }
1051
1052 /*
1053 * Scan wake-up capabilities.
1054 */
1055 rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
1056
1057 if (ACPI_SUCCESS(rv)) {
1058 ad->ad_flags |= ACPI_DEVICE_WAKEUP;
1059 acpi_wakedev_add(ad);
1060 }
1061 }
1062 }
1063
1064 static int
1065 acpi_print(void *aux, const char *pnp)
1066 {
1067 struct acpi_attach_args *aa = aux;
1068 ACPI_STATUS rv;
1069
1070 if (pnp) {
1071 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1072 char *pnpstr =
1073 aa->aa_node->ad_devinfo->HardwareId.String;
1074 ACPI_BUFFER buf;
1075
1076 aprint_normal("%s (%s) ", aa->aa_node->ad_name,
1077 pnpstr);
1078
1079 rv = acpi_eval_struct(aa->aa_node->ad_handle,
1080 "_STR", &buf);
1081 if (ACPI_SUCCESS(rv)) {
1082 ACPI_OBJECT *obj = buf.Pointer;
1083 switch (obj->Type) {
1084 case ACPI_TYPE_STRING:
1085 aprint_normal("[%s] ", obj->String.Pointer);
1086 break;
1087 case ACPI_TYPE_BUFFER:
1088 aprint_normal("buffer %p ", obj->Buffer.Pointer);
1089 break;
1090 default:
1091 aprint_normal("type %u ",obj->Type);
1092 break;
1093 }
1094 ACPI_FREE(buf.Pointer);
1095 }
1096 else
1097 acpi_print_dev(pnpstr);
1098
1099 aprint_normal("at %s", pnp);
1100 } else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
1101 aprint_normal("%s (ACPI Object Type '%s' "
1102 "[0x%02x]) ", aa->aa_node->ad_name,
1103 AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
1104 aa->aa_node->ad_devinfo->Type);
1105 aprint_normal("at %s", pnp);
1106 } else
1107 return 0;
1108 } else {
1109 aprint_normal(" (%s", aa->aa_node->ad_name);
1110 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1111 aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
1112 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
1113 const char *uid;
1114
1115 uid = aa->aa_node->ad_devinfo->UniqueId.String;
1116 if (uid[0] == '\0')
1117 uid = "<null>";
1118 aprint_normal("-%s", uid);
1119 }
1120 }
1121 aprint_normal(")");
1122 }
1123
1124 return UNCONF;
1125 }
1126
1127 /*
1128 * Notify.
1129 */
1130 static void
1131 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
1132 {
1133 struct acpi_softc *sc = acpi_softc;
1134 struct acpi_devnode *ad;
1135
1136 KASSERT(sc != NULL);
1137 KASSERT(aux == NULL);
1138 KASSERT(acpi_active != 0);
1139
1140 if (acpi_suspended != 0)
1141 return;
1142
1143 /*
1144 * System: 0x00 - 0x7F.
1145 * Device: 0x80 - 0xFF.
1146 */
1147 switch (event) {
1148
1149 case ACPI_NOTIFY_BUS_CHECK:
1150 case ACPI_NOTIFY_DEVICE_CHECK:
1151 case ACPI_NOTIFY_DEVICE_WAKE:
1152 case ACPI_NOTIFY_EJECT_REQUEST:
1153 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
1154 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1155 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1156 case ACPI_NOTIFY_POWER_FAULT:
1157 case ACPI_NOTIFY_CAPABILITIES_CHECK:
1158 case ACPI_NOTIFY_DEVICE_PLD_CHECK:
1159 case ACPI_NOTIFY_RESERVED:
1160 case ACPI_NOTIFY_LOCALITY_UPDATE:
1161 break;
1162 }
1163
1164 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
1165 "%s (%p)\n", event, acpi_name(handle), handle));
1166
1167 /*
1168 * We deliver notifications only to drivers
1169 * that have been succesfully attached and
1170 * that have registered a handler with us.
1171 * The opaque pointer is always the device_t.
1172 */
1173 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1174
1175 if (ad->ad_device == NULL)
1176 continue;
1177
1178 if (ad->ad_notify == NULL)
1179 continue;
1180
1181 if (ad->ad_handle != handle)
1182 continue;
1183
1184 (*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
1185
1186 return;
1187 }
1188
1189 aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
1190 "for %s (%p)\n", event, acpi_name(handle), handle);
1191 }
1192
1193 bool
1194 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
1195 {
1196 struct acpi_softc *sc = acpi_softc;
1197
1198 KASSERT(sc != NULL);
1199 KASSERT(acpi_active != 0);
1200
1201 if (acpi_suspended != 0)
1202 goto fail;
1203
1204 if (ad == NULL || notify == NULL)
1205 goto fail;
1206
1207 ad->ad_notify = notify;
1208
1209 return true;
1210
1211 fail:
1212 aprint_error_dev(sc->sc_dev, "failed to register notify "
1213 "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
1214
1215 return false;
1216 }
1217
1218 void
1219 acpi_deregister_notify(struct acpi_devnode *ad)
1220 {
1221
1222 ad->ad_notify = NULL;
1223 }
1224
1225 /*
1226 * Fixed buttons.
1227 */
1228 static void
1229 acpi_register_fixed_button(struct acpi_softc *sc, int event)
1230 {
1231 struct sysmon_pswitch *smpsw;
1232 ACPI_STATUS rv;
1233 int type;
1234
1235 switch (event) {
1236
1237 case ACPI_EVENT_POWER_BUTTON:
1238
1239 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
1240 return;
1241
1242 type = PSWITCH_TYPE_POWER;
1243 smpsw = &sc->sc_smpsw_power;
1244 break;
1245
1246 case ACPI_EVENT_SLEEP_BUTTON:
1247
1248 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
1249 return;
1250
1251 type = PSWITCH_TYPE_SLEEP;
1252 smpsw = &sc->sc_smpsw_sleep;
1253 break;
1254
1255 default:
1256 rv = AE_TYPE;
1257 goto fail;
1258 }
1259
1260 smpsw->smpsw_type = type;
1261 smpsw->smpsw_name = device_xname(sc->sc_dev);
1262
1263 if (sysmon_pswitch_register(smpsw) != 0) {
1264 rv = AE_ERROR;
1265 goto fail;
1266 }
1267
1268 rv = AcpiInstallFixedEventHandler(event,
1269 acpi_fixed_button_handler, smpsw);
1270
1271 if (ACPI_FAILURE(rv)) {
1272 sysmon_pswitch_unregister(smpsw);
1273 goto fail;
1274 }
1275
1276 aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
1277 (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
1278
1279 return;
1280
1281 fail:
1282 aprint_error_dev(sc->sc_dev, "failed to register "
1283 "fixed event: %s\n", AcpiFormatException(rv));
1284 }
1285
1286 static void
1287 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
1288 {
1289 struct sysmon_pswitch *smpsw;
1290 ACPI_STATUS rv;
1291
1292 switch (event) {
1293
1294 case ACPI_EVENT_POWER_BUTTON:
1295 smpsw = &sc->sc_smpsw_power;
1296
1297 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
1298 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
1299 return;
1300 }
1301
1302 break;
1303
1304 case ACPI_EVENT_SLEEP_BUTTON:
1305 smpsw = &sc->sc_smpsw_sleep;
1306
1307 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
1308 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
1309 return;
1310 }
1311
1312 break;
1313
1314 default:
1315 rv = AE_TYPE;
1316 goto fail;
1317 }
1318
1319 rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
1320
1321 if (ACPI_SUCCESS(rv)) {
1322 sysmon_pswitch_unregister(smpsw);
1323 return;
1324 }
1325
1326 fail:
1327 aprint_error_dev(sc->sc_dev, "failed to deregister "
1328 "fixed event: %s\n", AcpiFormatException(rv));
1329 }
1330
1331 static uint32_t
1332 acpi_fixed_button_handler(void *context)
1333 {
1334 static const int handler = OSL_NOTIFY_HANDLER;
1335 struct sysmon_pswitch *smpsw = context;
1336
1337 (void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
1338
1339 return ACPI_INTERRUPT_HANDLED;
1340 }
1341
1342 static void
1343 acpi_fixed_button_pressed(void *context)
1344 {
1345 struct sysmon_pswitch *smpsw = context;
1346
1347 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
1348 (smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
1349 "power" : "sleep"));
1350
1351 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1352 }
1353
1354 /*
1355 * Sleep.
1356 */
1357 static void
1358 acpi_sleep_init(struct acpi_softc *sc)
1359 {
1360 uint8_t a, b, i;
1361 ACPI_STATUS rv;
1362
1363 CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
1364 CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
1365 CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
1366
1367 /*
1368 * Evaluate supported sleep states.
1369 */
1370 for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
1371
1372 rv = AcpiGetSleepTypeData(i, &a, &b);
1373
1374 if (ACPI_SUCCESS(rv))
1375 sc->sc_sleepstates |= __BIT(i);
1376 }
1377 }
1378
1379 void
1380 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1381 {
1382 ACPI_STATUS rv;
1383 int err;
1384
1385 if (state == sc->sc_sleepstate)
1386 return;
1387
1388 aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
1389
1390 switch (state) {
1391
1392 case ACPI_STATE_S0:
1393 sc->sc_sleepstate = ACPI_STATE_S0;
1394 return;
1395
1396 case ACPI_STATE_S1:
1397 case ACPI_STATE_S2:
1398 case ACPI_STATE_S3:
1399 case ACPI_STATE_S4:
1400
1401 if ((sc->sc_sleepstates & __BIT(state)) == 0) {
1402 aprint_error_dev(sc->sc_dev, "sleep state "
1403 "S%d is not available\n", state);
1404 return;
1405 }
1406
1407 /*
1408 * Evaluate the _TTS method. This should be done before
1409 * pmf_system_suspend(9) and the evaluation of _PTS.
1410 * We should also re-evaluate this once we return to
1411 * S0 or if we abort the sleep state transition in the
1412 * middle (see ACPI 3.0, section 7.3.6). In reality,
1413 * however, the _TTS method is seldom seen in the field.
1414 */
1415 rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
1416
1417 if (ACPI_SUCCESS(rv))
1418 aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
1419
1420 acpi_wakedev_commit(sc, state);
1421
1422 if (state != ACPI_STATE_S1 &&
1423 pmf_system_suspend(PMF_Q_NONE) != true) {
1424 aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1425 break;
1426 }
1427
1428 /*
1429 * This will evaluate the _PTS and _SST methods,
1430 * but unlike the documentation claims, not _GTS,
1431 * which is evaluated in AcpiEnterSleepState().
1432 *
1433 * This must be called with interrupts enabled.
1434 */
1435 rv = AcpiEnterSleepStatePrep(state);
1436
1437 if (ACPI_FAILURE(rv)) {
1438 aprint_error_dev(sc->sc_dev, "failed to prepare "
1439 "S%d: %s\n", state, AcpiFormatException(rv));
1440 break;
1441 }
1442
1443 sc->sc_sleepstate = state;
1444
1445 if (state == ACPI_STATE_S1) {
1446
1447 /* Just enter the state. */
1448 acpi_md_OsDisableInterrupt();
1449 rv = AcpiEnterSleepState(state);
1450
1451 if (ACPI_FAILURE(rv))
1452 aprint_error_dev(sc->sc_dev, "failed to "
1453 "enter S1: %s\n", AcpiFormatException(rv));
1454
1455 (void)AcpiLeaveSleepState(state);
1456
1457 } else {
1458
1459 err = acpi_md_sleep(state);
1460
1461 if (state == ACPI_STATE_S4)
1462 AcpiEnable();
1463
1464 pmf_system_bus_resume(PMF_Q_NONE);
1465 (void)AcpiLeaveSleepState(state);
1466 pmf_system_resume(PMF_Q_NONE);
1467 }
1468
1469 break;
1470
1471 case ACPI_STATE_S5:
1472
1473 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
1474
1475 rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1476
1477 if (ACPI_FAILURE(rv)) {
1478 aprint_error_dev(sc->sc_dev, "failed to prepare "
1479 "S%d: %s\n", state, AcpiFormatException(rv));
1480 break;
1481 }
1482
1483 DELAY(1000000);
1484
1485 sc->sc_sleepstate = state;
1486 acpi_md_OsDisableInterrupt();
1487
1488 (void)AcpiEnterSleepState(ACPI_STATE_S5);
1489
1490 aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
1491
1492 break;
1493 }
1494
1495 sc->sc_sleepstate = ACPI_STATE_S0;
1496
1497 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
1498 }
1499
1500 /*
1501 * Sysctl.
1502 */
1503 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1504 {
1505 const struct sysctlnode *mnode, *rnode;
1506 int err;
1507
1508 err = sysctl_createv(clog, 0, NULL, &rnode,
1509 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
1510 NULL, NULL, 0, NULL, 0,
1511 CTL_HW, CTL_EOL);
1512
1513 if (err != 0)
1514 return;
1515
1516 err = sysctl_createv(clog, 0, &rnode, &rnode,
1517 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1518 "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
1519 NULL, 0, NULL, 0,
1520 CTL_CREATE, CTL_EOL);
1521
1522 if (err != 0)
1523 return;
1524
1525 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1526 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1527 "root", SYSCTL_DESCR("ACPI root pointer"),
1528 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1529 CTL_CREATE, CTL_EOL);
1530
1531 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1532 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
1533 "supported_states", SYSCTL_DESCR("Supported system states"),
1534 sysctl_hw_acpi_sleepstates, 0, NULL, 0,
1535 CTL_CREATE, CTL_EOL);
1536
1537 err = sysctl_createv(NULL, 0, NULL, &mnode,
1538 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
1539 NULL, NULL, 0, NULL, 0,
1540 CTL_MACHDEP, CTL_EOL);
1541
1542 if (err == 0) {
1543
1544 (void)sysctl_createv(NULL, 0, &mnode, NULL,
1545 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
1546 "sleep_state", SYSCTL_DESCR("System sleep state"),
1547 sysctl_hw_acpi_sleepstate, 0, NULL, 0,
1548 CTL_CREATE, CTL_EOL);
1549 }
1550
1551 err = sysctl_createv(clog, 0, &rnode, &rnode,
1552 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1553 "stat", SYSCTL_DESCR("ACPI statistics"),
1554 NULL, 0, NULL, 0,
1555 CTL_CREATE, CTL_EOL);
1556
1557 if (err != 0)
1558 return;
1559
1560 (void)sysctl_createv(clog, 0, &rnode, NULL,
1561 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1562 "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
1563 NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
1564 CTL_CREATE, CTL_EOL);
1565
1566 (void)sysctl_createv(clog, 0, &rnode, NULL,
1567 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1568 "sci", SYSCTL_DESCR("Number of SCI interrupts"),
1569 NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
1570 CTL_CREATE, CTL_EOL);
1571
1572 (void)sysctl_createv(clog, 0, &rnode, NULL,
1573 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1574 "fixed", SYSCTL_DESCR("Number of fixed events"),
1575 sysctl_hw_acpi_fixedstats, 0, NULL, 0,
1576 CTL_CREATE, CTL_EOL);
1577
1578 (void)sysctl_createv(clog, 0, &rnode, NULL,
1579 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1580 "method", SYSCTL_DESCR("Number of methods executed"),
1581 NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
1582 CTL_CREATE, CTL_EOL);
1583
1584 CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
1585 CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
1586 }
1587
1588 static int
1589 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
1590 {
1591 struct sysctlnode node;
1592 uint64_t t;
1593 int err, i;
1594
1595 for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
1596 t += AcpiFixedEventCount[i];
1597
1598 node = *rnode;
1599 node.sysctl_data = &t;
1600
1601 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1602
1603 if (err || newp == NULL)
1604 return err;
1605
1606 return 0;
1607 }
1608
1609 static int
1610 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1611 {
1612 struct acpi_softc *sc = acpi_softc;
1613 struct sysctlnode node;
1614 int err, t;
1615
1616 if (acpi_softc == NULL)
1617 return ENOSYS;
1618
1619 node = *rnode;
1620 t = sc->sc_sleepstate;
1621 node.sysctl_data = &t;
1622
1623 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1624
1625 if (err || newp == NULL)
1626 return err;
1627
1628 if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
1629 return EINVAL;
1630
1631 acpi_enter_sleep_state(sc, t);
1632
1633 return 0;
1634 }
1635
1636 static int
1637 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
1638 {
1639 struct acpi_softc *sc = acpi_softc;
1640 struct sysctlnode node;
1641 char t[3 * 6 + 1];
1642 int err;
1643
1644 if (acpi_softc == NULL)
1645 return ENOSYS;
1646
1647 (void)memset(t, '\0', sizeof(t));
1648
1649 (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
1650 ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
1651 ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
1652 ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
1653 ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
1654 ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
1655 ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
1656
1657 node = *rnode;
1658 node.sysctl_data = &t;
1659
1660 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1661
1662 if (err || newp == NULL)
1663 return err;
1664
1665 return 0;
1666 }
1667
1668 /*
1669 * Miscellaneous.
1670 */
1671 static bool
1672 acpi_is_scope(struct acpi_devnode *ad)
1673 {
1674 int i;
1675
1676 /*
1677 * Return true if the node is a root scope.
1678 */
1679 if (ad->ad_parent == NULL)
1680 return false;
1681
1682 if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
1683 return false;
1684
1685 for (i = 0; i < __arraycount(acpi_scopes); i++) {
1686
1687 if (acpi_scopes[i] == NULL)
1688 continue;
1689
1690 if (ad->ad_handle == acpi_scopes[i])
1691 return true;
1692 }
1693
1694 return false;
1695 }
1696
1697 ACPI_PHYSICAL_ADDRESS
1698 acpi_OsGetRootPointer(void)
1699 {
1700 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
1701
1702 /*
1703 * We let MD code handle this since there are multiple ways to do it:
1704 *
1705 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
1706 *
1707 * IA-64: Use the EFI.
1708 */
1709 PhysicalAddress = acpi_md_OsGetRootPointer();
1710
1711 if (acpi_root_pointer == 0)
1712 acpi_root_pointer = PhysicalAddress;
1713
1714 return PhysicalAddress;
1715 }
1716
1717 static ACPI_TABLE_HEADER *
1718 acpi_map_rsdt(void)
1719 {
1720 ACPI_PHYSICAL_ADDRESS paddr;
1721 ACPI_TABLE_RSDP *rsdp;
1722
1723 paddr = AcpiOsGetRootPointer();
1724
1725 if (paddr == 0)
1726 return NULL;
1727
1728 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1729
1730 if (rsdp == NULL)
1731 return NULL;
1732
1733 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1734 paddr = rsdp->XsdtPhysicalAddress;
1735 else
1736 paddr = rsdp->RsdtPhysicalAddress;
1737
1738 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1739
1740 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1741 }
1742
1743 static void
1744 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1745 {
1746
1747 if (rsdt == NULL)
1748 return;
1749
1750 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1751 }
1752