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