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