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