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