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