acpi.c revision 1.192 1 /* $NetBSD: acpi.c,v 1.192 2010/04/26 04:31:09 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.192 2010/04/26 04:31:09 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
729 #define ACPI_DEV_VALID (ACPI_VALID_STA | ACPI_VALID_HID)
730 #define ACPI_DEV_STATUS (ACPI_STA_DEV_PRESENT | ACPI_STA_DEV_ENABLED)
731
732 static void
733 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
734 {
735 ACPI_DEVICE_INFO *newdi;
736 ACPI_STATUS rv;
737 uint32_t old;
738
739 /*
740 * If the device is valid and present,
741 * but not enabled, try to activate it.
742 */
743 if (((*di)->Valid & ACPI_DEV_VALID) != ACPI_DEV_VALID)
744 return;
745
746 old = (*di)->CurrentStatus;
747
748 if ((old & ACPI_DEV_STATUS) != ACPI_STA_DEV_PRESENT)
749 return;
750
751 rv = acpi_allocate_resources(handle);
752
753 if (ACPI_FAILURE(rv))
754 goto fail;
755
756 rv = AcpiGetObjectInfo(handle, &newdi);
757
758 if (ACPI_FAILURE(rv))
759 goto fail;
760
761 ACPI_FREE(*di);
762 *di = newdi;
763
764 aprint_verbose_dev(acpi_softc->sc_dev,
765 "%s activated, STA 0x%08X -> STA 0x%08X\n",
766 (*di)->HardwareId.String, old, (*di)->CurrentStatus);
767
768 return;
769
770 fail:
771 aprint_error_dev(acpi_softc->sc_dev, "failed to "
772 "activate %s\n", (*di)->HardwareId.String);
773 }
774
775 /*
776 * XXX: This very incomplete.
777 */
778 ACPI_STATUS
779 acpi_allocate_resources(ACPI_HANDLE handle)
780 {
781 ACPI_BUFFER bufp, bufc, bufn;
782 ACPI_RESOURCE *resp, *resc, *resn;
783 ACPI_RESOURCE_IRQ *irq;
784 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
785 ACPI_STATUS rv;
786 uint delta;
787
788 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
789 if (ACPI_FAILURE(rv))
790 goto out;
791 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
792 if (ACPI_FAILURE(rv)) {
793 goto out1;
794 }
795
796 bufn.Length = 1000;
797 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
798 resp = bufp.Pointer;
799 resc = bufc.Pointer;
800 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
801 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
802 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
803 resp = ACPI_NEXT_RESOURCE(resp);
804 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
805 break;
806 /* Found identical Id */
807 resn->Type = resc->Type;
808 switch (resc->Type) {
809 case ACPI_RESOURCE_TYPE_IRQ:
810 memcpy(&resn->Data, &resp->Data,
811 sizeof(ACPI_RESOURCE_IRQ));
812 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
813 irq->Interrupts[0] =
814 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
815 Interrupts[irq->InterruptCount-1];
816 irq->InterruptCount = 1;
817 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
818 break;
819 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
820 memcpy(&resn->Data, &resp->Data,
821 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
822 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
823 #if 0
824 /*
825 * XXX: Not duplicating the interrupt logic above
826 * because its not clear what it accomplishes.
827 */
828 xirq->Interrupts[0] =
829 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
830 Interrupts[irq->NumberOfInterrupts-1];
831 xirq->NumberOfInterrupts = 1;
832 #endif
833 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
834 break;
835 case ACPI_RESOURCE_TYPE_IO:
836 memcpy(&resn->Data, &resp->Data,
837 sizeof(ACPI_RESOURCE_IO));
838 resn->Length = resp->Length;
839 break;
840 default:
841 aprint_error_dev(acpi_softc->sc_dev,
842 "%s: invalid type %u\n", __func__, resc->Type);
843 rv = AE_BAD_DATA;
844 goto out2;
845 }
846 resc = ACPI_NEXT_RESOURCE(resc);
847 resn = ACPI_NEXT_RESOURCE(resn);
848 resp = ACPI_NEXT_RESOURCE(resp);
849 delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
850 if (delta >=
851 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
852 bufn.Length *= 2;
853 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
854 M_ACPI, M_WAITOK);
855 resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
856 delta);
857 }
858 }
859
860 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
861 aprint_error_dev(acpi_softc->sc_dev,
862 "%s: resc not exhausted\n", __func__);
863 rv = AE_BAD_DATA;
864 goto out3;
865 }
866
867 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
868 rv = AcpiSetCurrentResources(handle, &bufn);
869
870 if (ACPI_FAILURE(rv))
871 aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
872 "resources: %s\n", __func__, AcpiFormatException(rv));
873
874 out3:
875 free(bufn.Pointer, M_ACPI);
876 out2:
877 ACPI_FREE(bufc.Pointer);
878 out1:
879 ACPI_FREE(bufp.Pointer);
880 out:
881 return rv;
882 }
883
884 #undef ACPI_DEV_VALID
885 #undef ACPI_DEV_STATUS
886
887 #endif /* ACPI_ACTIVATE_DEV */
888
889 /*
890 * Device attachment.
891 */
892 static int
893 acpi_rescan(device_t self, const char *ifattr, const int *locators)
894 {
895 struct acpi_softc *sc = device_private(self);
896
897 acpi_rescan1(sc, ifattr, locators);
898
899 return 0;
900 }
901
902 static void
903 acpi_rescan1(struct acpi_softc *sc, const char *ifattr, const int *locators)
904 {
905
906 if (ifattr_match(ifattr, "acpinodebus"))
907 acpi_rescan_nodes(sc);
908
909 if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
910 sc->sc_apmbus = config_found_ia(sc->sc_dev,
911 "acpiapmbus", NULL, NULL);
912 }
913
914 static void
915 acpi_rescan_nodes(struct acpi_softc *sc)
916 {
917 struct acpi_attach_args aa;
918 struct acpi_devnode *ad;
919
920 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
921
922 if (ad->ad_device != NULL)
923 continue;
924
925 /*
926 * There is a bug in ACPICA: it defines the type
927 * of the scopes incorrectly for its own reasons.
928 */
929 if (acpi_is_scope(ad) != false)
930 continue;
931
932 /*
933 * We only attach devices which are present, enabled, and
934 * functioning properly. However, if a device is enabled,
935 * it is decoding resources and we should claim these,
936 * if possible. This requires changes to bus_space(9).
937 */
938 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
939
940 if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
941 ACPI_VALID_STA &&
942 (ad->ad_devinfo->CurrentStatus &
943 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
944 ACPI_STA_DEV_OK)) !=
945 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
946 ACPI_STA_DEV_OK))
947 continue;
948 }
949
950 /*
951 * The same problem as above. As for example
952 * thermal zones and power resources do not
953 * have a valid HID, only evaluate devices.
954 */
955 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE &&
956 (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
957 continue;
958
959 /*
960 * Handled internally.
961 */
962 if (ad->ad_devinfo->Type == ACPI_TYPE_PROCESSOR)
963 continue;
964
965 /*
966 * Ditto, but bind power resources.
967 */
968 if (ad->ad_devinfo->Type == ACPI_TYPE_POWER) {
969 acpi_power_res_add(ad);
970 continue;
971 }
972
973 /*
974 * Skip ignored HIDs.
975 */
976 if (acpi_match_hid(ad->ad_devinfo, acpi_ignored_ids))
977 continue;
978
979 aa.aa_node = ad;
980 aa.aa_iot = sc->sc_iot;
981 aa.aa_memt = sc->sc_memt;
982 aa.aa_pc = sc->sc_pc;
983 aa.aa_pciflags = sc->sc_pciflags;
984 aa.aa_ic = sc->sc_ic;
985
986 ad->ad_device = config_found_ia(sc->sc_dev,
987 "acpinodebus", &aa, acpi_print);
988 }
989 }
990
991 #define ACPI_STA_DEV_VALID \
992 (ACPI_STA_DEV_PRESENT | ACPI_STA_DEV_ENABLED | ACPI_STA_DEV_OK)
993
994 static void
995 acpi_rescan_capabilities(struct acpi_softc *sc)
996 {
997 struct acpi_devnode *ad;
998 ACPI_DEVICE_INFO *di;
999 ACPI_HANDLE tmp;
1000 ACPI_STATUS rv;
1001
1002 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1003
1004 di = ad->ad_devinfo;
1005
1006 if (di->Type != ACPI_TYPE_DEVICE)
1007 continue;
1008
1009 if ((di->Valid & ACPI_VALID_STA) != 0 &&
1010 (di->CurrentStatus & ACPI_STA_DEV_VALID) !=
1011 ACPI_STA_DEV_VALID)
1012 continue;
1013
1014 /*
1015 * Scan power resource capabilities.
1016 *
1017 * If any power states are supported,
1018 * at least _PR0 and _PR3 must be present.
1019 */
1020 rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
1021
1022 if (ACPI_SUCCESS(rv)) {
1023 ad->ad_flags |= ACPI_DEVICE_POWER;
1024 acpi_power_add(ad);
1025 }
1026
1027 /*
1028 * Scan wake-up capabilities.
1029 */
1030 rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
1031
1032 if (ACPI_SUCCESS(rv)) {
1033 ad->ad_flags |= ACPI_DEVICE_WAKEUP;
1034 acpi_wakedev_add(ad);
1035 }
1036 }
1037 }
1038
1039 #undef ACPI_STA_DEV_VALID
1040
1041 static int
1042 acpi_print(void *aux, const char *pnp)
1043 {
1044 struct acpi_attach_args *aa = aux;
1045 ACPI_STATUS rv;
1046
1047 if (pnp) {
1048 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1049 char *pnpstr =
1050 aa->aa_node->ad_devinfo->HardwareId.String;
1051 ACPI_BUFFER buf;
1052
1053 aprint_normal("%s (%s) ", aa->aa_node->ad_name,
1054 pnpstr);
1055
1056 rv = acpi_eval_struct(aa->aa_node->ad_handle,
1057 "_STR", &buf);
1058 if (ACPI_SUCCESS(rv)) {
1059 ACPI_OBJECT *obj = buf.Pointer;
1060 switch (obj->Type) {
1061 case ACPI_TYPE_STRING:
1062 aprint_normal("[%s] ", obj->String.Pointer);
1063 break;
1064 case ACPI_TYPE_BUFFER:
1065 aprint_normal("buffer %p ", obj->Buffer.Pointer);
1066 break;
1067 default:
1068 aprint_normal("type %u ",obj->Type);
1069 break;
1070 }
1071 ACPI_FREE(buf.Pointer);
1072 }
1073 #ifdef ACPIVERBOSE
1074 else {
1075 int i;
1076
1077 for (i = 0; i < __arraycount(acpi_knowndevs);
1078 i++) {
1079 if (strcmp(acpi_knowndevs[i].pnp,
1080 pnpstr) == 0) {
1081 aprint_normal("[%s] ",
1082 acpi_knowndevs[i].str);
1083 }
1084 }
1085 }
1086
1087 #endif
1088 aprint_normal("at %s", pnp);
1089 } else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
1090 aprint_normal("%s (ACPI Object Type '%s' "
1091 "[0x%02x]) ", aa->aa_node->ad_name,
1092 AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
1093 aa->aa_node->ad_devinfo->Type);
1094 aprint_normal("at %s", pnp);
1095 } else
1096 return 0;
1097 } else {
1098 aprint_normal(" (%s", aa->aa_node->ad_name);
1099 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1100 aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
1101 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
1102 const char *uid;
1103
1104 uid = aa->aa_node->ad_devinfo->UniqueId.String;
1105 if (uid[0] == '\0')
1106 uid = "<null>";
1107 aprint_normal("-%s", uid);
1108 }
1109 }
1110 aprint_normal(")");
1111 }
1112
1113 return UNCONF;
1114 }
1115
1116 #ifdef ACPIVERBOSE
1117 static void
1118 acpi_print_devnodes(struct acpi_softc *sc)
1119 {
1120 struct acpi_devnode *ad;
1121 ACPI_DEVICE_INFO *di;
1122
1123 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1124
1125 di = ad->ad_devinfo;
1126 aprint_normal_dev(sc->sc_dev, "%-5s ", ad->ad_name);
1127
1128 aprint_normal("HID %-10s ",
1129 ((di->Valid & ACPI_VALID_HID) != 0) ?
1130 di->HardwareId.String: "-");
1131
1132 aprint_normal("UID %-4s ",
1133 ((di->Valid & ACPI_VALID_UID) != 0) ?
1134 di->UniqueId.String : "-");
1135
1136 if ((di->Valid & ACPI_VALID_STA) != 0)
1137 aprint_normal("STA 0x%08X ", di->CurrentStatus);
1138 else
1139 aprint_normal("STA %10s ", "-");
1140
1141 if ((di->Valid & ACPI_VALID_ADR) != 0)
1142 aprint_normal("ADR 0x%016" PRIX64"", di->Address);
1143 else
1144 aprint_normal("ADR -");
1145
1146 aprint_normal("\n");
1147 }
1148 }
1149
1150 static void
1151 acpi_print_tree(struct acpi_devnode *ad, uint32_t level)
1152 {
1153 struct acpi_devnode *child;
1154 uint32_t i;
1155
1156 for (i = 0; i < level; i++)
1157 aprint_normal(" ");
1158
1159 aprint_normal("%-5s [%02u] [%c%c] ", ad->ad_name, ad->ad_type,
1160 ((ad->ad_flags & ACPI_DEVICE_POWER) != 0) ? 'P' : ' ',
1161 ((ad->ad_flags & ACPI_DEVICE_WAKEUP) != 0) ? 'W' : ' ');
1162
1163 if (ad->ad_pciinfo != NULL) {
1164
1165 aprint_normal("(PCI) @ 0x%02X:0x%02X:0x%02X:0x%02X ",
1166 ad->ad_pciinfo->ap_segment, ad->ad_pciinfo->ap_bus,
1167 ad->ad_pciinfo->ap_device, ad->ad_pciinfo->ap_function);
1168
1169 if ((ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0)
1170 aprint_normal("[R] ");
1171
1172 if (ad->ad_pciinfo->ap_bridge != false)
1173 aprint_normal("[B] -> 0x%02X",
1174 ad->ad_pciinfo->ap_downbus);
1175 }
1176
1177 aprint_normal("\n\n");
1178
1179 SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list)
1180 acpi_print_tree(child, level + 1);
1181 }
1182 #endif
1183
1184 /*
1185 * Notify.
1186 */
1187 static void
1188 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
1189 {
1190 struct acpi_softc *sc = acpi_softc;
1191 struct acpi_devnode *ad;
1192
1193 KASSERT(sc != NULL);
1194 KASSERT(aux == NULL);
1195 KASSERT(acpi_active != 0);
1196
1197 if (acpi_suspended != 0)
1198 return;
1199
1200 /*
1201 * System: 0x00 - 0x7F.
1202 * Device: 0x80 - 0xFF.
1203 */
1204 switch (event) {
1205
1206 case ACPI_NOTIFY_BUS_CHECK:
1207 case ACPI_NOTIFY_DEVICE_CHECK:
1208 case ACPI_NOTIFY_DEVICE_WAKE:
1209 case ACPI_NOTIFY_EJECT_REQUEST:
1210 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
1211 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1212 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1213 case ACPI_NOTIFY_POWER_FAULT:
1214 case ACPI_NOTIFY_CAPABILITIES_CHECK:
1215 case ACPI_NOTIFY_DEVICE_PLD_CHECK:
1216 case ACPI_NOTIFY_RESERVED:
1217 case ACPI_NOTIFY_LOCALITY_UPDATE:
1218 break;
1219 }
1220
1221 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
1222 "%s (%p)\n", event, acpi_name(handle), handle));
1223
1224 /*
1225 * We deliver notifications only to drivers
1226 * that have been succesfully attached and
1227 * that have registered a handler with us.
1228 * The opaque pointer is always the device_t.
1229 */
1230 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1231
1232 if (ad->ad_device == NULL)
1233 continue;
1234
1235 if (ad->ad_notify == NULL)
1236 continue;
1237
1238 if (ad->ad_handle != handle)
1239 continue;
1240
1241 (*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
1242
1243 return;
1244 }
1245
1246 aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
1247 "for %s (%p)\n", event, acpi_name(handle), handle);
1248 }
1249
1250 bool
1251 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
1252 {
1253 struct acpi_softc *sc = acpi_softc;
1254
1255 KASSERT(sc != NULL);
1256 KASSERT(acpi_active != 0);
1257
1258 if (acpi_suspended != 0)
1259 goto fail;
1260
1261 if (ad == NULL || notify == NULL)
1262 goto fail;
1263
1264 ad->ad_notify = notify;
1265
1266 return true;
1267
1268 fail:
1269 aprint_error_dev(sc->sc_dev, "failed to register notify "
1270 "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
1271
1272 return false;
1273 }
1274
1275 void
1276 acpi_deregister_notify(struct acpi_devnode *ad)
1277 {
1278
1279 ad->ad_notify = NULL;
1280 }
1281
1282 /*
1283 * Fixed buttons.
1284 */
1285 static void
1286 acpi_register_fixed_button(struct acpi_softc *sc, int event)
1287 {
1288 struct sysmon_pswitch *smpsw;
1289 ACPI_STATUS rv;
1290 int type;
1291
1292 switch (event) {
1293
1294 case ACPI_EVENT_POWER_BUTTON:
1295
1296 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
1297 return;
1298
1299 type = PSWITCH_TYPE_POWER;
1300 smpsw = &sc->sc_smpsw_power;
1301 break;
1302
1303 case ACPI_EVENT_SLEEP_BUTTON:
1304
1305 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
1306 return;
1307
1308 type = PSWITCH_TYPE_SLEEP;
1309 smpsw = &sc->sc_smpsw_sleep;
1310 break;
1311
1312 default:
1313 rv = AE_TYPE;
1314 goto fail;
1315 }
1316
1317 smpsw->smpsw_type = type;
1318 smpsw->smpsw_name = device_xname(sc->sc_dev);
1319
1320 if (sysmon_pswitch_register(smpsw) != 0) {
1321 rv = AE_ERROR;
1322 goto fail;
1323 }
1324
1325 rv = AcpiInstallFixedEventHandler(event,
1326 acpi_fixed_button_handler, smpsw);
1327
1328 if (ACPI_FAILURE(rv))
1329 goto fail;
1330
1331 aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
1332 (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
1333
1334 return;
1335
1336 fail:
1337 aprint_error_dev(sc->sc_dev, "failed to register "
1338 "fixed event: %s\n", AcpiFormatException(rv));
1339 }
1340
1341 static void
1342 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
1343 {
1344 struct sysmon_pswitch *smpsw;
1345 ACPI_STATUS rv;
1346
1347 switch (event) {
1348
1349 case ACPI_EVENT_POWER_BUTTON:
1350 smpsw = &sc->sc_smpsw_power;
1351
1352 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
1353 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
1354 return;
1355 }
1356
1357 break;
1358
1359 case ACPI_EVENT_SLEEP_BUTTON:
1360 smpsw = &sc->sc_smpsw_sleep;
1361
1362 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
1363 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
1364 return;
1365 }
1366
1367 break;
1368
1369 default:
1370 rv = AE_TYPE;
1371 goto fail;
1372 }
1373
1374 rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
1375
1376 if (ACPI_SUCCESS(rv)) {
1377 sysmon_pswitch_unregister(smpsw);
1378 return;
1379 }
1380
1381 fail:
1382 aprint_error_dev(sc->sc_dev, "failed to deregister "
1383 "fixed event: %s\n", AcpiFormatException(rv));
1384 }
1385
1386 static uint32_t
1387 acpi_fixed_button_handler(void *context)
1388 {
1389 static const int handler = OSL_NOTIFY_HANDLER;
1390 struct sysmon_pswitch *smpsw = context;
1391
1392 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "fixed event\n"));
1393
1394 (void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
1395
1396 return ACPI_INTERRUPT_HANDLED;
1397 }
1398
1399 static void
1400 acpi_fixed_button_pressed(void *context)
1401 {
1402 struct sysmon_pswitch *smpsw = context;
1403
1404 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1405 "%s fixed button pressed\n", smpsw->smpsw_name));
1406
1407 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1408 }
1409
1410 /*
1411 * Sleep.
1412 */
1413 static void
1414 acpi_sleep_init(struct acpi_softc *sc)
1415 {
1416 uint8_t a, b, i;
1417 ACPI_STATUS rv;
1418
1419 CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
1420 CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
1421 CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
1422
1423 /*
1424 * Evaluate supported sleep states.
1425 */
1426 for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
1427
1428 rv = AcpiGetSleepTypeData(i, &a, &b);
1429
1430 if (ACPI_SUCCESS(rv))
1431 sc->sc_sleepstates |= __BIT(i);
1432 }
1433 }
1434
1435 ACPI_STATUS
1436 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1437 {
1438 ACPI_STATUS rv = AE_OK;
1439 int err;
1440
1441 if (state == sc->sc_sleepstate)
1442 return AE_OK;
1443
1444 aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
1445
1446 switch (state) {
1447
1448 case ACPI_STATE_S0:
1449 break;
1450
1451 case ACPI_STATE_S1:
1452 case ACPI_STATE_S2:
1453 case ACPI_STATE_S3:
1454 case ACPI_STATE_S4:
1455
1456 if ((sc->sc_sleepstates & __BIT(state)) == 0) {
1457 aprint_error_dev(sc->sc_dev, "sleep state "
1458 "S%d is not available\n", state);
1459 return AE_OK;
1460 }
1461
1462 /*
1463 * Evaluate the _TTS method. This should be done before
1464 * pmf_system_suspend(9) and the evaluation of _PTS.
1465 * We should also re-evaluate this once we return to
1466 * S0 or if we abort the sleep state transition in the
1467 * middle (see ACPI 3.0, section 7.3.6). In reality,
1468 * however, the _TTS method is seldom seen in the field.
1469 */
1470 rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
1471
1472 if (ACPI_SUCCESS(rv))
1473 aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
1474
1475 rv = AE_OK;
1476
1477 acpi_wakedev_commit(sc, state);
1478
1479 if (state != ACPI_STATE_S1 &&
1480 pmf_system_suspend(PMF_Q_NONE) != true) {
1481 aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1482 break;
1483 }
1484
1485 /*
1486 * This will evaluate the _PTS and _SST methods,
1487 * but unlike the documentation claims, not _GTS,
1488 * which is evaluated in AcpiEnterSleepState().
1489 *
1490 * This must be called with interrupts enabled.
1491 */
1492 rv = AcpiEnterSleepStatePrep(state);
1493
1494 if (ACPI_FAILURE(rv)) {
1495 aprint_error_dev(sc->sc_dev, "failed to prepare "
1496 "S%d: %s\n", state, AcpiFormatException(rv));
1497 break;
1498 }
1499
1500 sc->sc_sleepstate = state;
1501
1502 if (state == ACPI_STATE_S1) {
1503
1504 /* Just enter the state. */
1505 acpi_md_OsDisableInterrupt();
1506 rv = AcpiEnterSleepState(state);
1507
1508 if (ACPI_FAILURE(rv))
1509 aprint_error_dev(sc->sc_dev, "failed to "
1510 "enter S1: %s\n", AcpiFormatException(rv));
1511
1512 (void)AcpiLeaveSleepState(state);
1513
1514 } else {
1515
1516 err = acpi_md_sleep(state);
1517
1518 if (state == ACPI_STATE_S4)
1519 AcpiEnable();
1520
1521 pmf_system_bus_resume(PMF_Q_NONE);
1522 (void)AcpiLeaveSleepState(state);
1523 pmf_system_resume(PMF_Q_NONE);
1524 }
1525
1526 break;
1527
1528 case ACPI_STATE_S5:
1529
1530 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
1531
1532 rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1533
1534 if (ACPI_FAILURE(rv)) {
1535 aprint_error_dev(sc->sc_dev, "failed to prepare "
1536 "S%d: %s\n", state, AcpiFormatException(rv));
1537 break;
1538 }
1539
1540 DELAY(1000000);
1541
1542 sc->sc_sleepstate = state;
1543 acpi_md_OsDisableInterrupt();
1544
1545 (void)AcpiEnterSleepState(ACPI_STATE_S5);
1546
1547 aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
1548
1549 break;
1550 }
1551
1552 sc->sc_sleepstate = ACPI_STATE_S0;
1553
1554 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
1555
1556 return rv;
1557 }
1558
1559 /*
1560 * Sysctl.
1561 */
1562 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1563 {
1564 const struct sysctlnode *mnode, *rnode;
1565 int err;
1566
1567 err = sysctl_createv(clog, 0, NULL, &rnode,
1568 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
1569 NULL, NULL, 0, NULL, 0,
1570 CTL_HW, CTL_EOL);
1571
1572 if (err != 0)
1573 return;
1574
1575 err = sysctl_createv(clog, 0, &rnode, &rnode,
1576 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1577 "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
1578 NULL, 0, NULL, 0,
1579 CTL_CREATE, CTL_EOL);
1580
1581 if (err != 0)
1582 return;
1583
1584 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1585 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1586 "root", SYSCTL_DESCR("ACPI root pointer"),
1587 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1588 CTL_CREATE, CTL_EOL);
1589
1590 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1591 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
1592 "supported_states", SYSCTL_DESCR("Supported system states"),
1593 sysctl_hw_acpi_sleepstates, 0, NULL, 0,
1594 CTL_CREATE, CTL_EOL);
1595
1596 err = sysctl_createv(NULL, 0, NULL, &mnode,
1597 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
1598 NULL, NULL, 0, NULL, 0,
1599 CTL_MACHDEP, CTL_EOL);
1600
1601 if (err == 0) {
1602
1603 (void)sysctl_createv(NULL, 0, &mnode, NULL,
1604 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
1605 "sleep_state", SYSCTL_DESCR("System sleep state"),
1606 sysctl_hw_acpi_sleepstate, 0, NULL, 0,
1607 CTL_CREATE, CTL_EOL);
1608 }
1609
1610 err = sysctl_createv(clog, 0, &rnode, &rnode,
1611 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1612 "stat", SYSCTL_DESCR("ACPI statistics"),
1613 NULL, 0, NULL, 0,
1614 CTL_CREATE, CTL_EOL);
1615
1616 if (err != 0)
1617 return;
1618
1619 (void)sysctl_createv(clog, 0, &rnode, NULL,
1620 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1621 "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
1622 NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
1623 CTL_CREATE, CTL_EOL);
1624
1625 (void)sysctl_createv(clog, 0, &rnode, NULL,
1626 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1627 "sci", SYSCTL_DESCR("Number of SCI interrupts"),
1628 NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
1629 CTL_CREATE, CTL_EOL);
1630
1631 (void)sysctl_createv(clog, 0, &rnode, NULL,
1632 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1633 "fixed", SYSCTL_DESCR("Number of fixed events"),
1634 sysctl_hw_acpi_fixedstats, 0, NULL, 0,
1635 CTL_CREATE, CTL_EOL);
1636
1637 (void)sysctl_createv(clog, 0, &rnode, NULL,
1638 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1639 "method", SYSCTL_DESCR("Number of methods executed"),
1640 NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
1641 CTL_CREATE, CTL_EOL);
1642
1643 CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
1644 CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
1645 }
1646
1647 static int
1648 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
1649 {
1650 struct sysctlnode node;
1651 uint64_t t;
1652 int err, i;
1653
1654 for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
1655 t += AcpiFixedEventCount[i];
1656
1657 node = *rnode;
1658 node.sysctl_data = &t;
1659
1660 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1661
1662 if (err || newp == NULL)
1663 return err;
1664
1665 return 0;
1666 }
1667
1668 static int
1669 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1670 {
1671 struct acpi_softc *sc = acpi_softc;
1672 struct sysctlnode node;
1673 int err, t;
1674
1675 if (acpi_softc == NULL)
1676 return ENOSYS;
1677
1678 node = *rnode;
1679 t = sc->sc_sleepstate;
1680 node.sysctl_data = &t;
1681
1682 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1683
1684 if (err || newp == NULL)
1685 return err;
1686
1687 if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
1688 return EINVAL;
1689
1690 acpi_enter_sleep_state(sc, t);
1691
1692 return 0;
1693 }
1694
1695 static int
1696 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
1697 {
1698 struct acpi_softc *sc = acpi_softc;
1699 struct sysctlnode node;
1700 char t[3 * 6 + 1];
1701 int err;
1702
1703 if (acpi_softc == NULL)
1704 return ENOSYS;
1705
1706 (void)memset(t, '\0', sizeof(t));
1707
1708 (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
1709 ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
1710 ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
1711 ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
1712 ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
1713 ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
1714 ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
1715
1716 node = *rnode;
1717 node.sysctl_data = &t;
1718
1719 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1720
1721 if (err || newp == NULL)
1722 return err;
1723
1724 return 0;
1725 }
1726
1727 /*
1728 * Miscellaneous.
1729 */
1730 static bool
1731 acpi_is_scope(struct acpi_devnode *ad)
1732 {
1733 int i;
1734
1735 /*
1736 * Return true if the node is a root scope.
1737 */
1738 if (ad->ad_parent == NULL)
1739 return false;
1740
1741 if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
1742 return false;
1743
1744 for (i = 0; i < __arraycount(acpi_scopes); i++) {
1745
1746 if (acpi_scopes[i] == NULL)
1747 continue;
1748
1749 if (ad->ad_handle == acpi_scopes[i])
1750 return true;
1751 }
1752
1753 return false;
1754 }
1755
1756 ACPI_PHYSICAL_ADDRESS
1757 acpi_OsGetRootPointer(void)
1758 {
1759 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
1760
1761 /*
1762 * We let MD code handle this since there are multiple ways to do it:
1763 *
1764 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
1765 *
1766 * IA-64: Use the EFI.
1767 */
1768 PhysicalAddress = acpi_md_OsGetRootPointer();
1769
1770 if (acpi_root_pointer == 0)
1771 acpi_root_pointer = PhysicalAddress;
1772
1773 return PhysicalAddress;
1774 }
1775
1776 static ACPI_TABLE_HEADER *
1777 acpi_map_rsdt(void)
1778 {
1779 ACPI_PHYSICAL_ADDRESS paddr;
1780 ACPI_TABLE_RSDP *rsdp;
1781
1782 paddr = AcpiOsGetRootPointer();
1783
1784 if (paddr == 0)
1785 return NULL;
1786
1787 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1788
1789 if (rsdp == NULL)
1790 return NULL;
1791
1792 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1793 paddr = rsdp->XsdtPhysicalAddress;
1794 else
1795 paddr = rsdp->RsdtPhysicalAddress;
1796
1797 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1798
1799 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1800 }
1801
1802 static void
1803 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1804 {
1805
1806 if (rsdt == NULL)
1807 return;
1808
1809 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1810 }
1811