acpi.c revision 1.254.2.3 1 /* $NetBSD: acpi.c,v 1.254.2.3 2014/08/20 00:03:35 tls 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 (c) 2003 Wasabi Systems, Inc.
34 * All rights reserved.
35 *
36 * Written by Frank van der Linden 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 /*
68 * Copyright 2001, 2003 Wasabi Systems, Inc.
69 * All rights reserved.
70 *
71 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed for the NetBSD Project by
84 * Wasabi Systems, Inc.
85 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
86 * or promote products derived from this software without specific prior
87 * written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
91 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
92 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
93 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
94 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
95 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
96 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
97 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
98 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
99 * POSSIBILITY OF SUCH DAMAGE.
100 */
101
102 #include <sys/cdefs.h>
103 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.254.2.3 2014/08/20 00:03:35 tls Exp $");
104
105 #include "opt_acpi.h"
106 #include "opt_pcifixup.h"
107
108 #include <sys/param.h>
109 #include <sys/device.h>
110 #include <sys/kernel.h>
111 #include <sys/kmem.h>
112 #include <sys/malloc.h>
113 #include <sys/module.h>
114 #include <sys/mutex.h>
115 #include <sys/sysctl.h>
116 #include <sys/systm.h>
117 #include <sys/timetc.h>
118
119 #include <dev/acpi/acpireg.h>
120 #include <dev/acpi/acpivar.h>
121 #include <dev/acpi/acpi_osd.h>
122 #include <dev/acpi/acpi_pci.h>
123 #include <dev/acpi/acpi_power.h>
124 #include <dev/acpi/acpi_timer.h>
125 #include <dev/acpi/acpi_wakedev.h>
126
127 #include <machine/acpi_machdep.h>
128
129 #define _COMPONENT ACPI_BUS_COMPONENT
130 ACPI_MODULE_NAME ("acpi")
131
132 /*
133 * The acpi_active variable is set when the ACPI subsystem is active.
134 * Machine-dependent code may wish to skip other steps (such as attaching
135 * subsystems that ACPI supercedes) when ACPI is active.
136 */
137 int acpi_active = 0;
138 int acpi_suspended = 0;
139 int acpi_force_load = 0;
140 int acpi_verbose_loaded = 0;
141
142 struct acpi_softc *acpi_softc = NULL;
143 static uint64_t acpi_root_pointer;
144 extern kmutex_t acpi_interrupt_list_mtx;
145 extern struct cfdriver acpi_cd;
146 static ACPI_HANDLE acpi_scopes[4];
147 ACPI_TABLE_HEADER *madt_header;
148
149 /*
150 * This structure provides a context for the ACPI
151 * namespace walk performed in acpi_build_tree().
152 */
153 struct acpi_walkcontext {
154 struct acpi_softc *aw_sc;
155 struct acpi_devnode *aw_parent;
156 };
157
158 /*
159 * Ignored HIDs.
160 */
161 static const char * const acpi_ignored_ids[] = {
162 #if defined(i386) || defined(x86_64)
163 "ACPI0007", /* ACPI CPUs do not attach to acpi(4) */
164 "PNP0000", /* AT interrupt controller is handled internally */
165 "PNP0200", /* AT DMA controller is handled internally */
166 "PNP0A??", /* PCI Busses are handled internally */
167 "PNP0B00", /* AT RTC is handled internally */
168 "PNP0C0F", /* ACPI PCI link devices are handled internally */
169 #endif
170 #if defined(x86_64)
171 "PNP0C04", /* FPU is handled internally */
172 #endif
173 NULL
174 };
175
176 /*
177 * Devices that should be attached early.
178 */
179 static const char * const acpi_early_ids[] = {
180 "PNP0C09", /* acpiec(4) */
181 NULL
182 };
183
184 static int acpi_match(device_t, cfdata_t, void *);
185 static int acpi_submatch(device_t, cfdata_t, const int *, void *);
186 static void acpi_attach(device_t, device_t, void *);
187 static int acpi_detach(device_t, int);
188 static void acpi_childdet(device_t, device_t);
189 static bool acpi_suspend(device_t, const pmf_qual_t *);
190 static bool acpi_resume(device_t, const pmf_qual_t *);
191
192 static void acpi_build_tree(struct acpi_softc *);
193 static void acpi_config_tree(struct acpi_softc *);
194 static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, uint32_t,
195 void *, void **);
196 static ACPI_STATUS acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
197 void *, void **);
198 static void acpi_make_name(struct acpi_devnode *, uint32_t);
199
200 static int acpi_rescan(device_t, const char *, const int *);
201 static void acpi_rescan_early(struct acpi_softc *);
202 static void acpi_rescan_nodes(struct acpi_softc *);
203 static void acpi_rescan_capabilities(device_t);
204 static int acpi_print(void *aux, const char *);
205
206 static void acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
207
208 static void acpi_register_fixed_button(struct acpi_softc *, int);
209 static void acpi_deregister_fixed_button(struct acpi_softc *, int);
210 static uint32_t acpi_fixed_button_handler(void *);
211 static void acpi_fixed_button_pressed(void *);
212
213 static void acpi_sleep_init(struct acpi_softc *);
214
215 static int sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO);
216 static int sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO);
217 static int sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO);
218
219 static bool acpi_is_scope(struct acpi_devnode *);
220 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
221 static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
222
223 void acpi_print_verbose_stub(struct acpi_softc *);
224 void acpi_print_dev_stub(const char *);
225
226 static void acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
227 ACPI_STATUS acpi_allocate_resources(ACPI_HANDLE);
228
229 void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub;
230 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
231
232 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
233 acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
234
235 /*
236 * Probe for ACPI support.
237 *
238 * This is called by the machine-dependent ACPI front-end.
239 * Note: this is not an autoconfiguration interface function.
240 */
241 int
242 acpi_probe(void)
243 {
244 ACPI_TABLE_HEADER *rsdt;
245 ACPI_STATUS rv;
246 int quirks;
247
248 if (acpi_softc != NULL)
249 panic("%s: already probed", __func__);
250
251 mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
252
253 /*
254 * Start up ACPICA.
255 */
256 AcpiGbl_AllMethodsSerialized = false;
257 AcpiGbl_EnableInterpreterSlack = true;
258
259 rv = AcpiInitializeSubsystem();
260
261 if (ACPI_FAILURE(rv)) {
262 aprint_error("%s: failed to initialize subsystem\n", __func__);
263 return 0;
264 }
265
266 /*
267 * Allocate space for RSDT/XSDT and DSDT,
268 * but allow resizing if more tables exist.
269 */
270 rv = AcpiInitializeTables(NULL, 2, true);
271
272 if (ACPI_FAILURE(rv)) {
273 aprint_error("%s: failed to initialize tables\n", __func__);
274 goto fail;
275 }
276
277 rv = AcpiLoadTables();
278
279 if (ACPI_FAILURE(rv)) {
280 aprint_error("%s: failed to load tables\n", __func__);
281 goto fail;
282 }
283
284 rsdt = acpi_map_rsdt();
285
286 if (rsdt == NULL) {
287 aprint_error("%s: failed to map RSDT\n", __func__);
288 goto fail;
289 }
290
291 quirks = acpi_find_quirks();
292
293 if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_BROKEN) != 0) {
294
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", rsdt->OemId, rsdt->OemTableId,
298 rsdt->OemRevision, rsdt->AslCompilerId,
299 rsdt->AslCompilerRevision);
300 aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
301
302 acpi_unmap_rsdt(rsdt);
303 goto fail;
304 }
305
306 if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_OLDBIOS) != 0) {
307
308 aprint_normal("ACPI: BIOS is too old (%s). "
309 "Set acpi_force_load to use.\n",
310 pmf_get_platform("bios-date"));
311
312 acpi_unmap_rsdt(rsdt);
313 goto fail;
314 }
315
316 acpi_unmap_rsdt(rsdt);
317
318 rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
319
320 if (ACPI_FAILURE(rv)) {
321 aprint_error("%s: failed to enable subsystem\n", __func__);
322 goto fail;
323 }
324
325 return 1;
326
327 fail:
328 (void)AcpiTerminate();
329
330 return 0;
331 }
332
333 void
334 acpi_disable(void)
335 {
336
337 if (acpi_softc == NULL)
338 return;
339
340 KASSERT(acpi_active != 0);
341
342 if (AcpiGbl_FADT.SmiCommand != 0)
343 AcpiDisable();
344 }
345
346 int
347 acpi_check(device_t parent, const char *ifattr)
348 {
349 return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
350 }
351
352 int
353 acpi_reset(void)
354 {
355 struct acpi_softc *sc = acpi_softc;
356 ACPI_GENERIC_ADDRESS *ResetReg;
357 ACPI_PCI_ID PciId;
358 ACPI_STATUS status;
359
360 if (sc == NULL)
361 return ENXIO;
362
363 ResetReg = &AcpiGbl_FADT.ResetRegister;
364
365 /* Check if the reset register is supported */
366 if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||
367 !ResetReg->Address) {
368 return ENOENT;
369 }
370
371 switch (ResetReg->SpaceId) {
372 case ACPI_ADR_SPACE_PCI_CONFIG:
373 PciId.Segment = PciId.Bus = 0;
374 PciId.Device = ACPI_GAS_PCI_DEV(ResetReg->Address);
375 PciId.Function = ACPI_GAS_PCI_FUNC(ResetReg->Address);
376 status = AcpiOsWritePciConfiguration(&PciId,
377 ACPI_GAS_PCI_REGOFF(ResetReg->Address),
378 AcpiGbl_FADT.ResetValue, ResetReg->BitWidth);
379 break;
380 case ACPI_ADR_SPACE_SYSTEM_IO:
381 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
382 status = AcpiReset();
383 break;
384 default:
385 status = AE_TYPE;
386 break;
387 }
388
389 return ACPI_FAILURE(status) ? EIO : 0;
390 }
391
392 /*
393 * Autoconfiguration.
394 */
395 static int
396 acpi_match(device_t parent, cfdata_t match, void *aux)
397 {
398 /*
399 * XXX: Nada; MD code has called acpi_probe().
400 */
401 return 1;
402 }
403
404 static int
405 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
406 {
407 struct cfattach *ca;
408
409 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
410
411 return (ca == &acpi_ca);
412 }
413
414 static void
415 acpi_attach(device_t parent, device_t self, void *aux)
416 {
417 struct acpi_softc *sc = device_private(self);
418 struct acpibus_attach_args *aa = aux;
419 ACPI_TABLE_HEADER *rsdt;
420 ACPI_STATUS rv;
421
422 aprint_naive("\n");
423 aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
424
425 if (acpi_softc != NULL)
426 panic("%s: already attached", __func__);
427
428 rsdt = acpi_map_rsdt();
429
430 if (rsdt == NULL)
431 aprint_error_dev(self, "X/RSDT: Not found\n");
432 else {
433 aprint_verbose_dev(self,
434 "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
435 rsdt->OemId, rsdt->OemTableId,
436 rsdt->OemRevision,
437 rsdt->AslCompilerId, rsdt->AslCompilerRevision);
438 }
439
440 acpi_unmap_rsdt(rsdt);
441
442 /* Clamp the max transfer size - assume LPC devs may be beneath us. */
443 self->dv_maxphys = MIN(parent->dv_maxphys, 64 * 1024);
444
445 sc->sc_dev = self;
446 sc->sc_root = NULL;
447
448 sc->sc_sleepstate = ACPI_STATE_S0;
449 sc->sc_quirks = acpi_find_quirks();
450
451 sysmon_power_settype("acpi");
452
453 sc->sc_iot = aa->aa_iot;
454 sc->sc_memt = aa->aa_memt;
455 sc->sc_pc = aa->aa_pc;
456 sc->sc_pciflags = aa->aa_pciflags;
457 sc->sc_ic = aa->aa_ic;
458
459 SIMPLEQ_INIT(&sc->ad_head);
460
461 acpi_softc = sc;
462
463 if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
464 aprint_error_dev(self, "couldn't establish power handler\n");
465
466 /*
467 * Bring ACPICA on-line.
468 */
469
470 rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
471
472 if (ACPI_FAILURE(rv))
473 goto fail;
474
475 /*
476 * Early initialization of acpiec(4) via ECDT.
477 */
478 (void)config_found_ia(self, "acpiecdtbus", aa, NULL);
479
480 rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
481
482 if (ACPI_FAILURE(rv))
483 goto fail;
484
485 /*
486 * Scan the namespace and build our device tree.
487 */
488 acpi_build_tree(sc);
489
490 acpi_md_callback(sc);
491
492 /*
493 * Early initialization of the _PDC control method
494 * that may load additional SSDT tables dynamically.
495 */
496 (void)acpi_md_pdc();
497
498 /*
499 * Install global notify handlers.
500 */
501 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
502 ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
503
504 if (ACPI_FAILURE(rv))
505 goto fail;
506
507 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
508 ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
509
510 if (ACPI_FAILURE(rv))
511 goto fail;
512
513 acpi_active = 1;
514
515 /* Show SCI interrupt. */
516 aprint_verbose_dev(self, "SCI interrupting at int %u\n",
517 AcpiGbl_FADT.SciInterrupt);
518
519 /*
520 * Install fixed-event handlers.
521 */
522 acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
523 acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
524
525 acpitimer_init(sc);
526 acpi_config_tree(sc);
527 acpi_sleep_init(sc);
528
529 #ifdef ACPI_DEBUG
530 acpi_debug_init();
531 #endif
532
533 /*
534 * Print debug information.
535 */
536 acpi_print_verbose(sc);
537
538 return;
539
540 fail:
541 aprint_error("%s: failed to initialize ACPI: %s\n",
542 __func__, AcpiFormatException(rv));
543 }
544
545 /*
546 * XXX: This is incomplete.
547 */
548 static int
549 acpi_detach(device_t self, int flags)
550 {
551 struct acpi_softc *sc = device_private(self);
552 ACPI_STATUS rv;
553 int rc;
554
555 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
556 ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
557
558 if (ACPI_FAILURE(rv))
559 return EBUSY;
560
561 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
562 ACPI_DEVICE_NOTIFY, acpi_notify_handler);
563
564 if (ACPI_FAILURE(rv))
565 return EBUSY;
566
567 if ((rc = config_detach_children(self, flags)) != 0)
568 return rc;
569
570 if ((rc = acpitimer_detach()) != 0)
571 return rc;
572
573 acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
574 acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
575
576 pmf_device_deregister(self);
577
578 acpi_softc = NULL;
579
580 return 0;
581 }
582
583 static void
584 acpi_childdet(device_t self, device_t child)
585 {
586 struct acpi_softc *sc = device_private(self);
587 struct acpi_devnode *ad;
588
589 if (sc->sc_apmbus == child)
590 sc->sc_apmbus = NULL;
591
592 if (sc->sc_hpet == child)
593 sc->sc_hpet = NULL;
594
595 if (sc->sc_wdrt == child)
596 sc->sc_wdrt = NULL;
597
598 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
599
600 if (ad->ad_device == child)
601 ad->ad_device = NULL;
602 }
603 }
604
605 static bool
606 acpi_suspend(device_t dv, const pmf_qual_t *qual)
607 {
608
609 acpi_suspended = 1;
610
611 return true;
612 }
613
614 static bool
615 acpi_resume(device_t dv, const pmf_qual_t *qual)
616 {
617
618 acpi_suspended = 0;
619
620 return true;
621 }
622
623 /*
624 * Namespace scan.
625 */
626 static void
627 acpi_build_tree(struct acpi_softc *sc)
628 {
629 struct acpi_walkcontext awc;
630
631 /*
632 * Get the root scope handles.
633 */
634 KASSERT(__arraycount(acpi_scopes) == 4);
635
636 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
637 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
638 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
639 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
640
641 /*
642 * Make the root node.
643 */
644 awc.aw_sc = sc;
645 awc.aw_parent = NULL;
646
647 (void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
648
649 KASSERT(sc->sc_root == NULL);
650 KASSERT(awc.aw_parent != NULL);
651
652 sc->sc_root = awc.aw_parent;
653
654 /*
655 * Build the internal namespace.
656 */
657 (void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
658 acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
659
660 /*
661 * Scan the internal namespace.
662 */
663 (void)acpi_pcidev_scan(sc->sc_root);
664 }
665
666 static void
667 acpi_config_tree(struct acpi_softc *sc)
668 {
669
670 /*
671 * Configure all everything found "at acpi?".
672 */
673 (void)acpi_rescan(sc->sc_dev, NULL, NULL);
674
675 /*
676 * Update GPE information.
677 *
678 * Note that this must be called after
679 * all GPE handlers have been installed.
680 */
681 (void)AcpiUpdateAllGpes();
682
683 /*
684 * Defer rest of the configuration.
685 */
686 (void)config_defer(sc->sc_dev, acpi_rescan_capabilities);
687 }
688
689 static ACPI_STATUS
690 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
691 void *context, void **status)
692 {
693 struct acpi_walkcontext *awc = context;
694 struct acpi_softc *sc = awc->aw_sc;
695 struct acpi_devnode *ad;
696 ACPI_DEVICE_INFO *devinfo;
697 ACPI_OBJECT_TYPE type;
698 ACPI_STATUS rv;
699
700 rv = AcpiGetObjectInfo(handle, &devinfo);
701
702 if (ACPI_FAILURE(rv))
703 return AE_OK; /* Do not terminate the walk. */
704
705 type = devinfo->Type;
706
707 switch (type) {
708
709 case ACPI_TYPE_DEVICE:
710 acpi_activate_device(handle, &devinfo);
711 case ACPI_TYPE_PROCESSOR:
712 case ACPI_TYPE_THERMAL:
713 case ACPI_TYPE_POWER:
714
715 ad = kmem_zalloc(sizeof(*ad), KM_SLEEP);
716
717 if (ad == NULL)
718 return AE_NO_MEMORY;
719
720 ad->ad_device = NULL;
721 ad->ad_notify = NULL;
722 ad->ad_pciinfo = NULL;
723 ad->ad_wakedev = NULL;
724
725 ad->ad_type = type;
726 ad->ad_handle = handle;
727 ad->ad_devinfo = devinfo;
728
729 ad->ad_root = sc->sc_dev;
730 ad->ad_parent = awc->aw_parent;
731
732 acpi_match_node_init(ad);
733 acpi_make_name(ad, devinfo->Name);
734
735 /*
736 * Identify wake GPEs from the _PRW. Note that
737 * AcpiUpdateAllGpes() must be called afterwards.
738 */
739 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE)
740 acpi_wakedev_init(ad);
741
742 SIMPLEQ_INIT(&ad->ad_child_head);
743 SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
744
745 if (ad->ad_parent != NULL) {
746
747 SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
748 ad, ad_child_list);
749 }
750
751 awc->aw_parent = ad;
752 }
753
754 return AE_OK;
755 }
756
757 static ACPI_STATUS
758 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
759 void *context, void **status)
760 {
761 struct acpi_walkcontext *awc = context;
762
763 KASSERT(awc != NULL);
764 KASSERT(awc->aw_parent != NULL);
765
766 if (handle == awc->aw_parent->ad_handle)
767 awc->aw_parent = awc->aw_parent->ad_parent;
768
769 return AE_OK;
770 }
771
772 static void
773 acpi_make_name(struct acpi_devnode *ad, uint32_t name)
774 {
775 ACPI_NAME_UNION *anu;
776 int clear, i;
777
778 anu = (ACPI_NAME_UNION *)&name;
779 ad->ad_name[4] = '\0';
780
781 for (i = 3, clear = 0; i >= 0; i--) {
782
783 if (clear == 0 && anu->Ascii[i] == '_')
784 ad->ad_name[i] = '\0';
785 else {
786 ad->ad_name[i] = anu->Ascii[i];
787 clear = 1;
788 }
789 }
790
791 if (ad->ad_name[0] == '\0')
792 ad->ad_name[0] = '_';
793 }
794
795 /*
796 * Device attachment.
797 */
798 static int
799 acpi_rescan(device_t self, const char *ifattr, const int *locators)
800 {
801 struct acpi_softc *sc = device_private(self);
802 struct acpi_attach_args aa;
803
804 /*
805 * Try to attach hpet(4) first via a specific table.
806 */
807 aa.aa_memt = sc->sc_memt;
808
809 if (ifattr_match(ifattr, "acpihpetbus") && sc->sc_hpet == NULL)
810 sc->sc_hpet = config_found_ia(sc->sc_dev,
811 "acpihpetbus", &aa, NULL);
812
813 /*
814 * A two-pass scan for acpinodebus.
815 */
816 if (ifattr_match(ifattr, "acpinodebus")) {
817 acpi_rescan_early(sc);
818 acpi_rescan_nodes(sc);
819 }
820
821 /*
822 * Attach APM emulation and acpiwdrt(4).
823 */
824 if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
825 sc->sc_apmbus = config_found_ia(sc->sc_dev,
826 "acpiapmbus", NULL, NULL);
827
828 if (ifattr_match(ifattr, "acpiwdrtbus") && sc->sc_wdrt == NULL)
829 sc->sc_wdrt = config_found_ia(sc->sc_dev,
830 "acpiwdrtbus", NULL, NULL);
831
832 return 0;
833 }
834
835 static void
836 acpi_rescan_early(struct acpi_softc *sc)
837 {
838 struct acpi_attach_args aa;
839 struct acpi_devnode *ad;
840
841 /*
842 * First scan for devices such as acpiec(4) that
843 * should be always attached before anything else.
844 * We want these devices to attach regardless of
845 * the device status and other restrictions.
846 */
847 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
848
849 if (ad->ad_device != NULL)
850 continue;
851
852 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
853 continue;
854
855 if (acpi_match_hid(ad->ad_devinfo, acpi_early_ids) == 0)
856 continue;
857
858 aa.aa_node = ad;
859 aa.aa_iot = sc->sc_iot;
860 aa.aa_memt = sc->sc_memt;
861 aa.aa_pc = sc->sc_pc;
862 aa.aa_pciflags = sc->sc_pciflags;
863 aa.aa_ic = sc->sc_ic;
864
865 ad->ad_device = config_found_ia(sc->sc_dev,
866 "acpinodebus", &aa, acpi_print);
867 }
868 }
869
870 static void
871 acpi_rescan_nodes(struct acpi_softc *sc)
872 {
873 const char * const hpet_ids[] = { "PNP0103", NULL };
874 struct acpi_attach_args aa;
875 struct acpi_devnode *ad;
876 ACPI_DEVICE_INFO *di;
877
878 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
879
880 if (ad->ad_device != NULL)
881 continue;
882
883 /*
884 * There is a bug in ACPICA: it defines the type
885 * of the scopes incorrectly for its own reasons.
886 */
887 if (acpi_is_scope(ad) != false)
888 continue;
889
890 di = ad->ad_devinfo;
891
892 /*
893 * We only attach devices which are present, enabled, and
894 * functioning properly. However, if a device is enabled,
895 * it is decoding resources and we should claim these,
896 * if possible. This requires changes to bus_space(9).
897 * Note: there is a possible race condition, because _STA
898 * may have changed since di->CurrentStatus was set.
899 */
900 if (di->Type == ACPI_TYPE_DEVICE) {
901
902 if ((di->Valid & ACPI_VALID_STA) != 0 &&
903 (di->CurrentStatus & ACPI_STA_OK) != ACPI_STA_OK)
904 continue;
905 }
906
907 if (di->Type == ACPI_TYPE_POWER)
908 continue;
909
910 if (di->Type == ACPI_TYPE_PROCESSOR)
911 continue;
912
913 if (acpi_match_hid(di, acpi_early_ids) != 0)
914 continue;
915
916 if (acpi_match_hid(di, acpi_ignored_ids) != 0)
917 continue;
918
919 if (acpi_match_hid(di, hpet_ids) != 0 && sc->sc_hpet != NULL)
920 continue;
921
922 aa.aa_node = ad;
923 aa.aa_iot = sc->sc_iot;
924 aa.aa_memt = sc->sc_memt;
925 aa.aa_pc = sc->sc_pc;
926 aa.aa_pciflags = sc->sc_pciflags;
927 aa.aa_ic = sc->sc_ic;
928
929 ad->ad_device = config_found_ia(sc->sc_dev,
930 "acpinodebus", &aa, acpi_print);
931 }
932 }
933
934 static void
935 acpi_rescan_capabilities(device_t self)
936 {
937 struct acpi_softc *sc = device_private(self);
938 struct acpi_devnode *ad;
939 ACPI_HANDLE tmp;
940 ACPI_STATUS rv;
941
942 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
943
944 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
945 continue;
946
947 /*
948 * Scan power resource capabilities.
949 *
950 * If any power states are supported,
951 * at least _PR0 and _PR3 must be present.
952 */
953 rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
954
955 if (ACPI_SUCCESS(rv)) {
956 ad->ad_flags |= ACPI_DEVICE_POWER;
957 acpi_power_add(ad);
958 }
959
960 /*
961 * Scan wake-up capabilities.
962 */
963 if (ad->ad_wakedev != NULL) {
964 ad->ad_flags |= ACPI_DEVICE_WAKEUP;
965 acpi_wakedev_add(ad);
966 }
967
968 /*
969 * Scan docking stations.
970 */
971 rv = AcpiGetHandle(ad->ad_handle, "_DCK", &tmp);
972
973 if (ACPI_SUCCESS(rv))
974 ad->ad_flags |= ACPI_DEVICE_DOCK;
975
976 /*
977 * Scan devices that are ejectable.
978 */
979 rv = AcpiGetHandle(ad->ad_handle, "_EJ0", &tmp);
980
981 if (ACPI_SUCCESS(rv))
982 ad->ad_flags |= ACPI_DEVICE_EJECT;
983 }
984 }
985
986 static int
987 acpi_print(void *aux, const char *pnp)
988 {
989 struct acpi_attach_args *aa = aux;
990 struct acpi_devnode *ad;
991 const char *hid, *uid;
992 ACPI_DEVICE_INFO *di;
993
994 ad = aa->aa_node;
995 di = ad->ad_devinfo;
996
997 hid = di->HardwareId.String;
998 uid = di->UniqueId.String;
999
1000 if (pnp != NULL) {
1001
1002 if (di->Type != ACPI_TYPE_DEVICE) {
1003
1004 aprint_normal("%s (ACPI Object Type '%s') at %s",
1005 ad->ad_name, AcpiUtGetTypeName(ad->ad_type), pnp);
1006
1007 return UNCONF;
1008 }
1009
1010 if ((di->Valid & ACPI_VALID_HID) == 0 || hid == NULL)
1011 return 0;
1012
1013 aprint_normal("%s (%s) ", ad->ad_name, hid);
1014 acpi_print_dev(hid);
1015 aprint_normal("at %s", pnp);
1016
1017 return UNCONF;
1018 }
1019
1020 aprint_normal(" (%s", ad->ad_name);
1021
1022 if ((di->Valid & ACPI_VALID_HID) != 0 && hid != NULL) {
1023
1024 aprint_normal(", %s", hid);
1025
1026 if ((di->Valid & ACPI_VALID_UID) != 0 && uid != NULL) {
1027
1028 if (uid[0] == '\0')
1029 uid = "<null>";
1030
1031 aprint_normal("-%s", uid);
1032 }
1033 }
1034
1035 aprint_normal(")");
1036
1037 return UNCONF;
1038 }
1039
1040 /*
1041 * Notify.
1042 */
1043 static void
1044 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
1045 {
1046 struct acpi_softc *sc = acpi_softc;
1047 struct acpi_devnode *ad;
1048
1049 KASSERT(sc != NULL);
1050 KASSERT(aux == NULL);
1051 KASSERT(acpi_active != 0);
1052
1053 if (acpi_suspended != 0)
1054 return;
1055
1056 /*
1057 * System: 0x00 - 0x7F.
1058 * Device: 0x80 - 0xFF.
1059 */
1060 switch (event) {
1061
1062 case ACPI_NOTIFY_BUS_CHECK:
1063 case ACPI_NOTIFY_DEVICE_CHECK:
1064 case ACPI_NOTIFY_DEVICE_WAKE:
1065 case ACPI_NOTIFY_EJECT_REQUEST:
1066 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
1067 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1068 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1069 case ACPI_NOTIFY_POWER_FAULT:
1070 case ACPI_NOTIFY_CAPABILITIES_CHECK:
1071 case ACPI_NOTIFY_DEVICE_PLD_CHECK:
1072 case ACPI_NOTIFY_RESERVED:
1073 case ACPI_NOTIFY_LOCALITY_UPDATE:
1074 break;
1075 }
1076
1077 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
1078 "%s (%p)\n", event, acpi_name(handle), handle));
1079
1080 /*
1081 * We deliver notifications only to drivers
1082 * that have been successfully attached and
1083 * that have registered a handler with us.
1084 * The opaque pointer is always the device_t.
1085 */
1086 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1087
1088 if (ad->ad_device == NULL)
1089 continue;
1090
1091 if (ad->ad_notify == NULL)
1092 continue;
1093
1094 if (ad->ad_handle != handle)
1095 continue;
1096
1097 (*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
1098
1099 return;
1100 }
1101
1102 aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
1103 "for %s (%p)\n", event, acpi_name(handle), handle);
1104 }
1105
1106 bool
1107 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
1108 {
1109 struct acpi_softc *sc = acpi_softc;
1110
1111 KASSERT(sc != NULL);
1112 KASSERT(acpi_active != 0);
1113
1114 if (acpi_suspended != 0)
1115 goto fail;
1116
1117 if (ad == NULL || notify == NULL)
1118 goto fail;
1119
1120 ad->ad_notify = notify;
1121
1122 return true;
1123
1124 fail:
1125 aprint_error_dev(sc->sc_dev, "failed to register notify "
1126 "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
1127
1128 return false;
1129 }
1130
1131 void
1132 acpi_deregister_notify(struct acpi_devnode *ad)
1133 {
1134
1135 ad->ad_notify = NULL;
1136 }
1137
1138 /*
1139 * Fixed buttons.
1140 */
1141 static void
1142 acpi_register_fixed_button(struct acpi_softc *sc, int event)
1143 {
1144 struct sysmon_pswitch *smpsw;
1145 ACPI_STATUS rv;
1146 int type;
1147
1148 switch (event) {
1149
1150 case ACPI_EVENT_POWER_BUTTON:
1151
1152 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
1153 return;
1154
1155 type = PSWITCH_TYPE_POWER;
1156 smpsw = &sc->sc_smpsw_power;
1157 break;
1158
1159 case ACPI_EVENT_SLEEP_BUTTON:
1160
1161 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
1162 return;
1163
1164 type = PSWITCH_TYPE_SLEEP;
1165 smpsw = &sc->sc_smpsw_sleep;
1166 break;
1167
1168 default:
1169 rv = AE_TYPE;
1170 goto fail;
1171 }
1172
1173 smpsw->smpsw_type = type;
1174 smpsw->smpsw_name = device_xname(sc->sc_dev);
1175
1176 if (sysmon_pswitch_register(smpsw) != 0) {
1177 rv = AE_ERROR;
1178 goto fail;
1179 }
1180
1181 AcpiClearEvent(event);
1182
1183 rv = AcpiInstallFixedEventHandler(event,
1184 acpi_fixed_button_handler, smpsw);
1185
1186 if (ACPI_FAILURE(rv)) {
1187 sysmon_pswitch_unregister(smpsw);
1188 goto fail;
1189 }
1190
1191 aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
1192 (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
1193
1194 return;
1195
1196 fail:
1197 aprint_error_dev(sc->sc_dev, "failed to register "
1198 "fixed event %d: %s\n", event, AcpiFormatException(rv));
1199 }
1200
1201 static void
1202 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
1203 {
1204 struct sysmon_pswitch *smpsw;
1205 ACPI_STATUS rv;
1206
1207 switch (event) {
1208
1209 case ACPI_EVENT_POWER_BUTTON:
1210 smpsw = &sc->sc_smpsw_power;
1211
1212 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
1213 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
1214 return;
1215 }
1216
1217 break;
1218
1219 case ACPI_EVENT_SLEEP_BUTTON:
1220 smpsw = &sc->sc_smpsw_sleep;
1221
1222 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
1223 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
1224 return;
1225 }
1226
1227 break;
1228
1229 default:
1230 rv = AE_TYPE;
1231 goto fail;
1232 }
1233
1234 rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
1235
1236 if (ACPI_SUCCESS(rv)) {
1237 sysmon_pswitch_unregister(smpsw);
1238 return;
1239 }
1240
1241 fail:
1242 aprint_error_dev(sc->sc_dev, "failed to deregister "
1243 "fixed event: %s\n", AcpiFormatException(rv));
1244 }
1245
1246 static uint32_t
1247 acpi_fixed_button_handler(void *context)
1248 {
1249 static const int handler = OSL_NOTIFY_HANDLER;
1250 struct sysmon_pswitch *smpsw = context;
1251
1252 (void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
1253
1254 return ACPI_INTERRUPT_HANDLED;
1255 }
1256
1257 static void
1258 acpi_fixed_button_pressed(void *context)
1259 {
1260 struct sysmon_pswitch *smpsw = context;
1261
1262 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
1263 (smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
1264 "power" : "sleep"));
1265
1266 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1267 }
1268
1269 /*
1270 * Sleep.
1271 */
1272 static void
1273 acpi_sleep_init(struct acpi_softc *sc)
1274 {
1275 uint8_t a, b, i;
1276 ACPI_STATUS rv;
1277
1278 CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
1279 CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
1280 CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
1281
1282 /*
1283 * Evaluate supported sleep states.
1284 */
1285 for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
1286
1287 rv = AcpiGetSleepTypeData(i, &a, &b);
1288
1289 if (ACPI_SUCCESS(rv))
1290 sc->sc_sleepstates |= __BIT(i);
1291 }
1292 }
1293
1294 /*
1295 * Must be called with interrupts enabled.
1296 */
1297 void
1298 acpi_enter_sleep_state(int state)
1299 {
1300 struct acpi_softc *sc = acpi_softc;
1301 ACPI_STATUS rv;
1302
1303 if (acpi_softc == NULL)
1304 return;
1305
1306 if (state == sc->sc_sleepstate)
1307 return;
1308
1309 if (state < ACPI_STATE_S0 || state > ACPI_STATE_S5)
1310 return;
1311
1312 aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
1313
1314 switch (state) {
1315
1316 case ACPI_STATE_S0:
1317 sc->sc_sleepstate = ACPI_STATE_S0;
1318 return;
1319
1320 case ACPI_STATE_S1:
1321 case ACPI_STATE_S2:
1322 case ACPI_STATE_S3:
1323 case ACPI_STATE_S4:
1324
1325 if ((sc->sc_sleepstates & __BIT(state)) == 0) {
1326 aprint_error_dev(sc->sc_dev, "sleep state "
1327 "S%d is not available\n", state);
1328 return;
1329 }
1330
1331 /*
1332 * Evaluate the _TTS method. This should be done before
1333 * pmf_system_suspend(9) and the evaluation of _PTS.
1334 * We should also re-evaluate this once we return to
1335 * S0 or if we abort the sleep state transition in the
1336 * middle (see ACPI 3.0, section 7.3.6). In reality,
1337 * however, the _TTS method is seldom seen in the field.
1338 */
1339 rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
1340
1341 if (ACPI_SUCCESS(rv))
1342 aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
1343
1344 if (state != ACPI_STATE_S1 &&
1345 pmf_system_suspend(PMF_Q_NONE) != true) {
1346 aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1347 break;
1348 }
1349
1350 /*
1351 * This will evaluate the _PTS and _SST methods,
1352 * but unlike the documentation claims, not _GTS,
1353 * which is evaluated in AcpiEnterSleepState().
1354 * This must be called with interrupts enabled.
1355 */
1356 rv = AcpiEnterSleepStatePrep(state);
1357
1358 if (ACPI_FAILURE(rv)) {
1359 aprint_error_dev(sc->sc_dev, "failed to prepare "
1360 "S%d: %s\n", state, AcpiFormatException(rv));
1361 break;
1362 }
1363
1364 /*
1365 * After the _PTS method has been evaluated, we can
1366 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
1367 */
1368 acpi_wakedev_commit(sc, state);
1369
1370 sc->sc_sleepstate = state;
1371
1372 if (state == ACPI_STATE_S1) {
1373
1374 /*
1375 * Before the transition to S1, CPU caches
1376 * must be flushed (see ACPI 4.0, 7.3.4.2).
1377 *
1378 * Note that interrupts must be off before
1379 * calling AcpiEnterSleepState(). Conversely,
1380 * AcpiLeaveSleepState() should always be
1381 * called with interrupts enabled.
1382 */
1383 acpi_md_OsDisableInterrupt();
1384
1385 ACPI_FLUSH_CPU_CACHE();
1386 rv = AcpiEnterSleepState(state);
1387
1388 if (ACPI_FAILURE(rv))
1389 aprint_error_dev(sc->sc_dev, "failed to "
1390 "enter S1: %s\n", AcpiFormatException(rv));
1391
1392 /*
1393 * Clear fixed events and disable all GPEs before
1394 * interrupts are enabled.
1395 */
1396 AcpiClearEvent(ACPI_EVENT_PMTIMER);
1397 AcpiClearEvent(ACPI_EVENT_GLOBAL);
1398 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1399 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1400 AcpiClearEvent(ACPI_EVENT_RTC);
1401 AcpiHwDisableAllGpes();
1402
1403 acpi_md_OsEnableInterrupt();
1404 rv = AcpiLeaveSleepState(state);
1405
1406 } else {
1407
1408 (void)acpi_md_sleep(state);
1409
1410 if (state == ACPI_STATE_S4)
1411 AcpiEnable();
1412
1413 (void)pmf_system_bus_resume(PMF_Q_NONE);
1414 (void)AcpiLeaveSleepState(state);
1415 (void)AcpiSetFirmwareWakingVector(0);
1416 (void)pmf_system_resume(PMF_Q_NONE);
1417 }
1418
1419 /*
1420 * No wake GPEs should be enabled at runtime.
1421 */
1422 acpi_wakedev_commit(sc, ACPI_STATE_S0);
1423 break;
1424
1425 case ACPI_STATE_S5:
1426
1427 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
1428
1429 rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1430
1431 if (ACPI_FAILURE(rv)) {
1432 aprint_error_dev(sc->sc_dev, "failed to prepare "
1433 "S%d: %s\n", state, AcpiFormatException(rv));
1434 break;
1435 }
1436
1437 (void)AcpiDisableAllGpes();
1438
1439 DELAY(1000000);
1440
1441 sc->sc_sleepstate = state;
1442 acpi_md_OsDisableInterrupt();
1443
1444 (void)AcpiEnterSleepState(ACPI_STATE_S5);
1445
1446 aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
1447
1448 break;
1449 }
1450
1451 sc->sc_sleepstate = ACPI_STATE_S0;
1452
1453 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
1454 }
1455
1456 /*
1457 * Sysctl.
1458 */
1459 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1460 {
1461 const struct sysctlnode *rnode, *snode;
1462 int err;
1463
1464 err = sysctl_createv(clog, 0, NULL, &rnode,
1465 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1466 "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
1467 NULL, 0, NULL, 0,
1468 CTL_HW, CTL_CREATE, CTL_EOL);
1469
1470 if (err != 0)
1471 return;
1472
1473 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1474 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1475 "root", SYSCTL_DESCR("ACPI root pointer"),
1476 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1477 CTL_CREATE, CTL_EOL);
1478
1479 err = sysctl_createv(clog, 0, &rnode, &snode,
1480 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1481 "sleep", SYSCTL_DESCR("ACPI sleep"),
1482 NULL, 0, NULL, 0,
1483 CTL_CREATE, CTL_EOL);
1484
1485 if (err != 0)
1486 return;
1487
1488 (void)sysctl_createv(NULL, 0, &snode, NULL,
1489 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
1490 "state", SYSCTL_DESCR("System sleep state"),
1491 sysctl_hw_acpi_sleepstate, 0, NULL, 0,
1492 CTL_CREATE, CTL_EOL);
1493
1494 (void)sysctl_createv(NULL, 0, &snode, NULL,
1495 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
1496 "states", SYSCTL_DESCR("Supported sleep states"),
1497 sysctl_hw_acpi_sleepstates, 0, NULL, 0,
1498 CTL_CREATE, CTL_EOL);
1499
1500 err = sysctl_createv(clog, 0, &rnode, &rnode,
1501 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1502 "stat", SYSCTL_DESCR("ACPI statistics"),
1503 NULL, 0, NULL, 0,
1504 CTL_CREATE, CTL_EOL);
1505
1506 if (err != 0)
1507 return;
1508
1509 (void)sysctl_createv(clog, 0, &rnode, NULL,
1510 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1511 "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
1512 NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
1513 CTL_CREATE, CTL_EOL);
1514
1515 (void)sysctl_createv(clog, 0, &rnode, NULL,
1516 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1517 "sci", SYSCTL_DESCR("Number of SCI interrupts"),
1518 NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
1519 CTL_CREATE, CTL_EOL);
1520
1521 (void)sysctl_createv(clog, 0, &rnode, NULL,
1522 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1523 "fixed", SYSCTL_DESCR("Number of fixed events"),
1524 sysctl_hw_acpi_fixedstats, 0, NULL, 0,
1525 CTL_CREATE, CTL_EOL);
1526
1527 (void)sysctl_createv(clog, 0, &rnode, NULL,
1528 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1529 "method", SYSCTL_DESCR("Number of methods executed"),
1530 NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
1531 CTL_CREATE, CTL_EOL);
1532
1533 CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
1534 CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
1535 }
1536
1537 static int
1538 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
1539 {
1540 struct sysctlnode node;
1541 uint64_t t;
1542 int err, i;
1543
1544 for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
1545 t += AcpiFixedEventCount[i];
1546
1547 node = *rnode;
1548 node.sysctl_data = &t;
1549
1550 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1551
1552 if (err || newp == NULL)
1553 return err;
1554
1555 return 0;
1556 }
1557
1558 static int
1559 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1560 {
1561 struct acpi_softc *sc = acpi_softc;
1562 struct sysctlnode node;
1563 int err, t;
1564
1565 if (acpi_softc == NULL)
1566 return ENOSYS;
1567
1568 node = *rnode;
1569 t = sc->sc_sleepstate;
1570 node.sysctl_data = &t;
1571
1572 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1573
1574 if (err || newp == NULL)
1575 return err;
1576
1577 if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
1578 return EINVAL;
1579
1580 acpi_enter_sleep_state(t);
1581
1582 return 0;
1583 }
1584
1585 static int
1586 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
1587 {
1588 struct acpi_softc *sc = acpi_softc;
1589 struct sysctlnode node;
1590 char t[3 * 6 + 1];
1591 int err;
1592
1593 if (acpi_softc == NULL)
1594 return ENOSYS;
1595
1596 (void)memset(t, '\0', sizeof(t));
1597
1598 (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
1599 ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
1600 ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
1601 ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
1602 ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
1603 ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
1604 ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
1605
1606 node = *rnode;
1607 node.sysctl_data = &t;
1608
1609 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1610
1611 if (err || newp == NULL)
1612 return err;
1613
1614 return 0;
1615 }
1616
1617 /*
1618 * Tables.
1619 */
1620 ACPI_PHYSICAL_ADDRESS
1621 acpi_OsGetRootPointer(void)
1622 {
1623 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
1624
1625 /*
1626 * We let MD code handle this since there are multiple ways to do it:
1627 *
1628 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
1629 *
1630 * IA-64: Use the EFI.
1631 */
1632 PhysicalAddress = acpi_md_OsGetRootPointer();
1633
1634 if (acpi_root_pointer == 0)
1635 acpi_root_pointer = PhysicalAddress;
1636
1637 return PhysicalAddress;
1638 }
1639
1640 static ACPI_TABLE_HEADER *
1641 acpi_map_rsdt(void)
1642 {
1643 ACPI_PHYSICAL_ADDRESS paddr;
1644 ACPI_TABLE_RSDP *rsdp;
1645
1646 paddr = AcpiOsGetRootPointer();
1647
1648 if (paddr == 0)
1649 return NULL;
1650
1651 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1652
1653 if (rsdp == NULL)
1654 return NULL;
1655
1656 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1657 paddr = rsdp->XsdtPhysicalAddress;
1658 else
1659 paddr = rsdp->RsdtPhysicalAddress;
1660
1661 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1662
1663 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1664 }
1665
1666 /*
1667 * XXX: Refactor to be a generic function that unmaps tables.
1668 */
1669 static void
1670 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1671 {
1672
1673 if (rsdt == NULL)
1674 return;
1675
1676 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1677 }
1678
1679 /*
1680 * XXX: Refactor to be a generic function that maps tables.
1681 */
1682 ACPI_STATUS
1683 acpi_madt_map(void)
1684 {
1685 ACPI_STATUS rv;
1686
1687 if (madt_header != NULL)
1688 return AE_ALREADY_EXISTS;
1689
1690 rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
1691
1692 if (ACPI_FAILURE(rv))
1693 return rv;
1694
1695 return AE_OK;
1696 }
1697
1698 void
1699 acpi_madt_unmap(void)
1700 {
1701 madt_header = NULL;
1702 }
1703
1704 /*
1705 * XXX: Refactor to be a generic function that walks tables.
1706 */
1707 void
1708 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
1709 {
1710 ACPI_SUBTABLE_HEADER *hdrp;
1711 char *madtend, *where;
1712
1713 madtend = (char *)madt_header + madt_header->Length;
1714 where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
1715
1716 while (where < madtend) {
1717
1718 hdrp = (ACPI_SUBTABLE_HEADER *)where;
1719
1720 if (ACPI_FAILURE(func(hdrp, aux)))
1721 break;
1722
1723 where += hdrp->Length;
1724 }
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 /*
1757 * ACPIVERBOSE.
1758 */
1759 void
1760 acpi_load_verbose(void)
1761 {
1762
1763 if (acpi_verbose_loaded == 0)
1764 module_autoload("acpiverbose", MODULE_CLASS_MISC);
1765 }
1766
1767 void
1768 acpi_print_verbose_stub(struct acpi_softc *sc)
1769 {
1770
1771 acpi_load_verbose();
1772
1773 if (acpi_verbose_loaded != 0)
1774 acpi_print_verbose(sc);
1775 }
1776
1777 void
1778 acpi_print_dev_stub(const char *pnpstr)
1779 {
1780
1781 acpi_load_verbose();
1782
1783 if (acpi_verbose_loaded != 0)
1784 acpi_print_dev(pnpstr);
1785 }
1786
1787 MALLOC_DECLARE(M_ACPI); /* XXX: ACPI_ACTIVATE_DEV should use kmem(9). */
1788
1789 /*
1790 * ACPI_ACTIVATE_DEV.
1791 */
1792 static void
1793 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
1794 {
1795
1796 #ifndef ACPI_ACTIVATE_DEV
1797 return;
1798 }
1799 #else
1800 static const int valid = ACPI_VALID_STA | ACPI_VALID_HID;
1801 ACPI_DEVICE_INFO *newdi;
1802 ACPI_STATUS rv;
1803 uint32_t old;
1804
1805 /*
1806 * If the device is valid and present,
1807 * but not enabled, try to activate it.
1808 */
1809 if (((*di)->Valid & valid) != valid)
1810 return;
1811
1812 old = (*di)->CurrentStatus;
1813
1814 if ((old & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED)) !=
1815 ACPI_STA_DEVICE_PRESENT)
1816 return;
1817
1818 rv = acpi_allocate_resources(handle);
1819
1820 if (ACPI_FAILURE(rv))
1821 goto fail;
1822
1823 rv = AcpiGetObjectInfo(handle, &newdi);
1824
1825 if (ACPI_FAILURE(rv))
1826 goto fail;
1827
1828 ACPI_FREE(*di);
1829 *di = newdi;
1830
1831 aprint_verbose_dev(acpi_softc->sc_dev,
1832 "%s activated, STA 0x%08X -> STA 0x%08X\n",
1833 (*di)->HardwareId.String, old, (*di)->CurrentStatus);
1834
1835 return;
1836
1837 fail:
1838 aprint_error_dev(acpi_softc->sc_dev, "failed to "
1839 "activate %s\n", (*di)->HardwareId.String);
1840 }
1841
1842 /*
1843 * XXX: This very incomplete.
1844 */
1845 ACPI_STATUS
1846 acpi_allocate_resources(ACPI_HANDLE handle)
1847 {
1848 ACPI_BUFFER bufp, bufc, bufn;
1849 ACPI_RESOURCE *resp, *resc, *resn;
1850 ACPI_RESOURCE_IRQ *irq;
1851 #if 0
1852 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1853 #endif
1854 ACPI_STATUS rv;
1855 uint delta;
1856
1857 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1858 if (ACPI_FAILURE(rv))
1859 goto out;
1860 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1861 if (ACPI_FAILURE(rv)) {
1862 goto out1;
1863 }
1864
1865 bufn.Length = 1000;
1866 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1867 resp = bufp.Pointer;
1868 resc = bufc.Pointer;
1869 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
1870 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1871 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
1872 resp = ACPI_NEXT_RESOURCE(resp);
1873 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
1874 break;
1875 /* Found identical Id */
1876 resn->Type = resc->Type;
1877 switch (resc->Type) {
1878 case ACPI_RESOURCE_TYPE_IRQ:
1879 memcpy(&resn->Data, &resp->Data,
1880 sizeof(ACPI_RESOURCE_IRQ));
1881 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1882 irq->Interrupts[0] =
1883 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1884 Interrupts[irq->InterruptCount-1];
1885 irq->InterruptCount = 1;
1886 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
1887 break;
1888 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1889 memcpy(&resn->Data, &resp->Data,
1890 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
1891 #if 0
1892 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
1893 /*
1894 * XXX: Not duplicating the interrupt logic above
1895 * because its not clear what it accomplishes.
1896 */
1897 xirq->Interrupts[0] =
1898 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
1899 Interrupts[irq->NumberOfInterrupts-1];
1900 xirq->NumberOfInterrupts = 1;
1901 #endif
1902 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
1903 break;
1904 case ACPI_RESOURCE_TYPE_IO:
1905 memcpy(&resn->Data, &resp->Data,
1906 sizeof(ACPI_RESOURCE_IO));
1907 resn->Length = resp->Length;
1908 break;
1909 default:
1910 aprint_error_dev(acpi_softc->sc_dev,
1911 "%s: invalid type %u\n", __func__, resc->Type);
1912 rv = AE_BAD_DATA;
1913 goto out2;
1914 }
1915 resc = ACPI_NEXT_RESOURCE(resc);
1916 resn = ACPI_NEXT_RESOURCE(resn);
1917 resp = ACPI_NEXT_RESOURCE(resp);
1918 delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
1919 if (delta >=
1920 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
1921 bufn.Length *= 2;
1922 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1923 M_ACPI, M_WAITOK);
1924 resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
1925 delta);
1926 }
1927 }
1928
1929 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1930 aprint_error_dev(acpi_softc->sc_dev,
1931 "%s: resc not exhausted\n", __func__);
1932 rv = AE_BAD_DATA;
1933 goto out3;
1934 }
1935
1936 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
1937 rv = AcpiSetCurrentResources(handle, &bufn);
1938
1939 if (ACPI_FAILURE(rv))
1940 aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
1941 "resources: %s\n", __func__, AcpiFormatException(rv));
1942
1943 out3:
1944 free(bufn.Pointer, M_ACPI);
1945 out2:
1946 ACPI_FREE(bufc.Pointer);
1947 out1:
1948 ACPI_FREE(bufp.Pointer);
1949 out:
1950 return rv;
1951 }
1952
1953 #endif /* ACPI_ACTIVATE_DEV */
1954