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