acpi.c revision 1.149 1 /* $NetBSD: acpi.c,v 1.149 2010/02/24 22:37:56 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum of By Noon Software, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright 2001, 2003 Wasabi Systems, Inc.
34 * All rights reserved.
35 *
36 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed for the NetBSD Project by
49 * Wasabi Systems, Inc.
50 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51 * or promote products derived from this software without specific prior
52 * written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
65 */
66
67 /*
68 * Autoconfiguration support for the Intel ACPI Component Architecture
69 * ACPI reference implementation.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.149 2010/02/24 22:37:56 dyoung Exp $");
74
75 #include "opt_acpi.h"
76 #include "opt_pcifixup.h"
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/device.h>
81 #include <sys/malloc.h>
82 #include <sys/mutex.h>
83 #include <sys/kernel.h>
84 #include <sys/proc.h>
85 #include <sys/sysctl.h>
86
87 #include <dev/acpi/acpica.h>
88 #include <dev/acpi/acpireg.h>
89 #include <dev/acpi/acpivar.h>
90 #include <dev/acpi/acpi_osd.h>
91 #include <dev/acpi/acpi_timer.h>
92 #include <dev/acpi/acpi_wakedev.h>
93 #include <dev/acpi/acpi_pci.h>
94 #ifdef ACPIVERBOSE
95 #include <dev/acpi/acpidevs_data.h>
96 #endif
97
98 #define _COMPONENT ACPI_TOOLS
99 ACPI_MODULE_NAME ("acpi")
100
101 #if defined(ACPI_PCI_FIXUP)
102 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED. Please adjust your kernel configuration file.
103 #endif
104
105 #ifdef PCI_INTR_FIXUP_DISABLED
106 #include <dev/pci/pcidevs.h>
107 #endif
108
109 MALLOC_DECLARE(M_ACPI);
110
111 #include <machine/acpi_machdep.h>
112
113 #ifdef ACPI_DEBUGGER
114 #define ACPI_DBGR_INIT 0x01
115 #define ACPI_DBGR_TABLES 0x02
116 #define ACPI_DBGR_ENABLE 0x04
117 #define ACPI_DBGR_PROBE 0x08
118 #define ACPI_DBGR_RUNNING 0x10
119
120 static int acpi_dbgr = 0x00;
121 #endif
122
123 static ACPI_TABLE_DESC acpi_initial_tables[128];
124
125 static int acpi_match(device_t, cfdata_t, void *);
126 static void acpi_attach(device_t, device_t, void *);
127 static void acpi_childdet(device_t, device_t);
128 static int acpi_detach(device_t, int);
129
130 static int acpi_rescan(device_t, const char *, const int *);
131 static void acpi_rescan1(struct acpi_softc *, const char *, const int *);
132 static void acpi_rescan_nodes(struct acpi_softc *);
133
134 static int acpi_print(void *aux, const char *);
135
136 static int sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
137
138 extern struct cfdriver acpi_cd;
139
140 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
141 acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
142
143 /*
144 * This is a flag we set when the ACPI subsystem is active. Machine
145 * dependent code may wish to skip other steps (such as attaching
146 * subsystems that ACPI supercedes) when ACPI is active.
147 */
148 int acpi_active;
149 int acpi_force_load;
150 int acpi_suspended = 0;
151
152 /*
153 * Pointer to the ACPI subsystem's state. There can be only
154 * one ACPI instance.
155 */
156 struct acpi_softc *acpi_softc;
157
158 /*
159 * Locking stuff.
160 */
161 extern kmutex_t acpi_interrupt_list_mtx;
162
163 /*
164 * Ignored HIDs.
165 */
166 static const char * const acpi_ignored_ids[] = {
167 #if defined(i386) || defined(x86_64)
168 "PNP0000", /* AT interrupt controller is handled internally */
169 "PNP0200", /* AT DMA controller is handled internally */
170 "PNP0A??", /* PCI Busses are handled internally */
171 "PNP0B00", /* AT RTC is handled internally */
172 "PNP0C01", /* No "System Board" driver */
173 "PNP0C02", /* No "PnP motherboard register resources" driver */
174 "PNP0C0B", /* No need for "ACPI fan" driver */
175 "PNP0C0F", /* ACPI PCI link devices are handled internally */
176 "INT0800", /* Intel HW RNG is handled internally */
177 #endif
178 #if defined(x86_64)
179 "PNP0C04", /* FPU is handled internally */
180 #endif
181 NULL
182 };
183
184 /*
185 * sysctl-related information
186 */
187
188 static uint64_t acpi_root_pointer; /* found as hw.acpi.root */
189 static int acpi_sleepstate = ACPI_STATE_S0;
190 static char acpi_supported_states[3 * 6 + 1] = "";
191
192 /*
193 * Prototypes.
194 */
195 static void acpi_build_tree(struct acpi_softc *);
196 static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
197
198 static void acpi_enable_fixed_events(struct acpi_softc *);
199
200 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
201 static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
202 static int is_available_state(struct acpi_softc *, int);
203
204 static bool acpi_suspend(device_t, const pmf_qual_t *);
205 static bool acpi_resume(device_t, const pmf_qual_t *);
206
207 /*
208 * acpi_probe:
209 *
210 * Probe for ACPI support. This is called by the
211 * machine-dependent ACPI front-end. All of the
212 * actual work is done by ACPICA.
213 *
214 * NOTE: This is not an autoconfiguration interface function.
215 */
216 int
217 acpi_probe(void)
218 {
219 static int beenhere;
220 ACPI_TABLE_HEADER *rsdt;
221 ACPI_STATUS rv;
222
223 if (beenhere != 0)
224 panic("acpi_probe: ACPI has already been probed");
225 beenhere = 1;
226
227 mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
228
229 /*
230 * Start up ACPICA.
231 */
232 #ifdef ACPI_DEBUGGER
233 if (acpi_dbgr & ACPI_DBGR_INIT)
234 acpi_osd_debugger();
235 #endif
236
237 AcpiGbl_AllMethodsSerialized = FALSE;
238 AcpiGbl_EnableInterpreterSlack = TRUE;
239
240 rv = AcpiInitializeSubsystem();
241 if (ACPI_FAILURE(rv)) {
242 printf("ACPI: unable to initialize ACPICA: %s\n",
243 AcpiFormatException(rv));
244 return 0;
245 }
246
247 rv = AcpiInitializeTables(acpi_initial_tables, 128, 0);
248 if (ACPI_FAILURE(rv)) {
249 #ifdef ACPI_DEBUG
250 printf("ACPI: unable to initialize ACPI tables: %s\n",
251 AcpiFormatException(rv));
252 #endif
253 AcpiTerminate();
254 return 0;
255 }
256
257 rv = AcpiReallocateRootTable();
258 if (ACPI_FAILURE(rv)) {
259 printf("ACPI: unable to reallocate root table: %s\n",
260 AcpiFormatException(rv));
261 AcpiTerminate();
262 return 0;
263 }
264
265 #ifdef ACPI_DEBUGGER
266 if (acpi_dbgr & ACPI_DBGR_TABLES)
267 acpi_osd_debugger();
268 #endif
269
270 rv = AcpiLoadTables();
271 if (ACPI_FAILURE(rv)) {
272 printf("ACPI: unable to load tables: %s\n",
273 AcpiFormatException(rv));
274 AcpiTerminate();
275 return 0;
276 }
277
278 rsdt = acpi_map_rsdt();
279 if (rsdt == NULL) {
280 printf("ACPI: unable to map RSDT\n");
281 AcpiTerminate();
282 return 0;
283 }
284
285 if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
286 printf("ACPI: BIOS implementation in listed as broken:\n");
287 printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
288 "AslId <%4.4s,%08x>\n",
289 rsdt->OemId, rsdt->OemTableId,
290 rsdt->OemRevision,
291 rsdt->AslCompilerId,
292 rsdt->AslCompilerRevision);
293 printf("ACPI: not used. set acpi_force_load to use anyway.\n");
294 acpi_unmap_rsdt(rsdt);
295 AcpiTerminate();
296 return 0;
297 }
298
299 acpi_unmap_rsdt(rsdt);
300
301 #if notyet
302 /* Install the default address space handlers. */
303 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
304 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
305 if (ACPI_FAILURE(rv)) {
306 printf("ACPI: unable to initialize SystemMemory handler: %s\n",
307 AcpiFormatException(rv));
308 AcpiTerminate();
309 return 0;
310 }
311 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
312 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
313 if (ACPI_FAILURE(rv)) {
314 printf("ACPI: unable to initialize SystemIO handler: %s\n",
315 AcpiFormatException(rv));
316 AcpiTerminate();
317 return 0;
318 }
319 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
320 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
321 if (ACPI_FAILURE(rv)) {
322 printf("ACPI: unable to initialize PciConfig handler: %s\n",
323 AcpiFormatException(rv));
324 AcpiTerminate();
325 return 0;
326 }
327 #endif
328
329 rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
330 if (ACPI_FAILURE(rv)) {
331 printf("ACPI: unable to enable: %s\n", AcpiFormatException(rv));
332 AcpiTerminate();
333 return 0;
334 }
335
336 /*
337 * Looks like we have ACPI!
338 */
339
340 return 1;
341 }
342
343 static int
344 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
345 {
346 struct cfattach *ca;
347
348 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
349 return (ca == &acpi_ca);
350 }
351
352 int
353 acpi_check(device_t parent, const char *ifattr)
354 {
355 return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
356 }
357
358 ACPI_PHYSICAL_ADDRESS
359 acpi_OsGetRootPointer(void)
360 {
361 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
362
363 /*
364 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
365 *
366 * IA-64: Use the EFI.
367 *
368 * We let MD code handle this since there are multiple
369 * ways to do it.
370 */
371
372 PhysicalAddress = acpi_md_OsGetRootPointer();
373
374 if (acpi_root_pointer == 0)
375 acpi_root_pointer = PhysicalAddress;
376
377 return PhysicalAddress;
378 }
379
380 /*
381 * acpi_match:
382 *
383 * Autoconfiguration `match' routine.
384 */
385 static int
386 acpi_match(device_t parent, cfdata_t match, void *aux)
387 {
388 /*
389 * XXX Check other locators? Hard to know -- machine
390 * dependent code has already checked for the presence
391 * of ACPI by calling acpi_probe(), so I suppose we
392 * don't really have to do anything else.
393 */
394 return 1;
395 }
396
397 /* Remove references to child devices.
398 *
399 * XXX Need to reclaim any resources?
400 */
401 static void
402 acpi_childdet(device_t self, device_t child)
403 {
404 struct acpi_softc *sc = device_private(self);
405 struct acpi_scope *as;
406 struct acpi_devnode *ad;
407
408 if (sc->sc_apmbus == child)
409 sc->sc_apmbus = NULL;
410
411 TAILQ_FOREACH(as, &sc->sc_scopes, as_list) {
412 TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
413 if (ad->ad_device == child)
414 ad->ad_device = NULL;
415 }
416 }
417 }
418
419 /*
420 * acpi_attach:
421 *
422 * Autoconfiguration `attach' routine. Finish initializing
423 * ACPICA (some initialization was done in acpi_probe(),
424 * which was required to check for the presence of ACPI),
425 * and enable the ACPI subsystem.
426 */
427 static void
428 acpi_attach(device_t parent, device_t self, void *aux)
429 {
430 struct acpi_softc *sc = device_private(self);
431 struct acpibus_attach_args *aa = aux;
432 ACPI_STATUS rv;
433 ACPI_TABLE_HEADER *rsdt;
434
435 aprint_naive("\n");
436 aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
437
438 if (acpi_softc != NULL)
439 panic("acpi_attach: ACPI has already been attached");
440
441 sysmon_power_settype("acpi");
442
443 rsdt = acpi_map_rsdt();
444 if (rsdt) {
445 aprint_verbose_dev(
446 self,
447 "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
448 rsdt->OemId, rsdt->OemTableId,
449 rsdt->OemRevision,
450 rsdt->AslCompilerId, rsdt->AslCompilerRevision);
451 } else
452 aprint_error_dev(self, "X/RSDT: Not found\n");
453 acpi_unmap_rsdt(rsdt);
454
455 sc->sc_dev = self;
456 sc->sc_quirks = acpi_find_quirks();
457
458 sc->sc_iot = aa->aa_iot;
459 sc->sc_memt = aa->aa_memt;
460 sc->sc_pc = aa->aa_pc;
461 sc->sc_pciflags = aa->aa_pciflags;
462 sc->sc_ic = aa->aa_ic;
463
464 acpi_softc = sc;
465
466 /*
467 * Register null power management handler.
468 */
469 if (!pmf_device_register(self, acpi_suspend, acpi_resume))
470 aprint_error_dev(self, "couldn't establish power handler\n");
471
472 /*
473 * Bring ACPI on-line.
474 */
475 #ifdef ACPI_DEBUGGER
476 if (acpi_dbgr & ACPI_DBGR_ENABLE)
477 acpi_osd_debugger();
478 #endif
479
480 #define ACPI_ENABLE_PHASE1 \
481 (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
482 #define ACPI_ENABLE_PHASE2 \
483 (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
484 ACPI_NO_ADDRESS_SPACE_INIT)
485
486 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
487 if (ACPI_FAILURE(rv)) {
488 aprint_error_dev(self, "unable to enable ACPI: %s\n",
489 AcpiFormatException(rv));
490 return;
491 }
492
493 acpi_md_callback();
494
495 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
496 if (ACPI_FAILURE(rv)) {
497 aprint_error_dev(self, "unable to enable ACPI: %s\n",
498 AcpiFormatException(rv));
499 return;
500 }
501
502 /* Early EC handler initialization if ECDT table is available. */
503 config_found_ia(self, "acpiecdtbus", NULL, NULL);
504
505 rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
506 if (ACPI_FAILURE(rv)) {
507 aprint_error_dev(self,
508 "unable to initialize ACPI objects: %s\n",
509 AcpiFormatException(rv));
510 return;
511 }
512 acpi_active = 1;
513
514 /* Our current state is "awake". */
515 sc->sc_sleepstate = ACPI_STATE_S0;
516
517 /* Show SCI interrupt. */
518 aprint_verbose_dev(self, "SCI interrupting at int %d\n",
519 AcpiGbl_FADT.SciInterrupt);
520
521 /*
522 * Check for fixed-hardware features.
523 */
524 acpi_enable_fixed_events(sc);
525 acpitimer_init();
526
527 /*
528 * Scan the namespace and build our device tree.
529 */
530 #ifdef ACPI_DEBUGGER
531 if (acpi_dbgr & ACPI_DBGR_PROBE)
532 acpi_osd_debugger();
533 #endif
534 acpi_build_tree(sc);
535
536 snprintf(acpi_supported_states, sizeof(acpi_supported_states),
537 "%s%s%s%s%s%s",
538 is_available_state(sc, ACPI_STATE_S0) ? "S0 " : "",
539 is_available_state(sc, ACPI_STATE_S1) ? "S1 " : "",
540 is_available_state(sc, ACPI_STATE_S2) ? "S2 " : "",
541 is_available_state(sc, ACPI_STATE_S3) ? "S3 " : "",
542 is_available_state(sc, ACPI_STATE_S4) ? "S4 " : "",
543 is_available_state(sc, ACPI_STATE_S5) ? "S5 " : "");
544
545 #ifdef ACPI_DEBUGGER
546 if (acpi_dbgr & ACPI_DBGR_RUNNING)
547 acpi_osd_debugger();
548 #endif
549
550 #ifdef ACPI_DEBUG
551 acpi_debug_init();
552 #endif
553 }
554
555 static int
556 acpi_detach(device_t self, int flags)
557 {
558 int rc;
559
560 #ifdef ACPI_DEBUGGER
561 if (acpi_dbgr & ACPI_DBGR_RUNNING)
562 acpi_osd_debugger();
563 #endif
564
565 if ((rc = config_detach_children(self, flags)) != 0)
566 return rc;
567
568 #ifdef ACPI_DEBUGGER
569 if (acpi_dbgr & ACPI_DBGR_PROBE)
570 acpi_osd_debugger();
571 #endif
572
573 if ((rc = acpitimer_detach()) != 0)
574 return rc;
575
576 #if 0
577 /*
578 * Bring ACPI on-line.
579 */
580 #ifdef ACPI_DEBUGGER
581 if (acpi_dbgr & ACPI_DBGR_ENABLE)
582 acpi_osd_debugger();
583 #endif
584
585 #define ACPI_ENABLE_PHASE1 \
586 (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
587 #define ACPI_ENABLE_PHASE2 \
588 (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
589 ACPI_NO_ADDRESS_SPACE_INIT)
590
591 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
592 if (ACPI_FAILURE(rv)) {
593 aprint_error_dev(self, "unable to enable ACPI: %s\n",
594 AcpiFormatException(rv));
595 return;
596 }
597
598 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
599 if (ACPI_FAILURE(rv)) {
600 aprint_error_dev(self, "unable to enable ACPI: %s\n",
601 AcpiFormatException(rv));
602 return;
603 }
604
605 /* Early EC handler initialization if ECDT table is available. */
606 config_found_ia(self, "acpiecdtbus", NULL, NULL);
607
608 rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
609 if (ACPI_FAILURE(rv)) {
610 aprint_error_dev(self,
611 "unable to initialize ACPI objects: %s\n",
612 AcpiFormatException(rv));
613 return;
614 }
615 acpi_active = 1;
616
617 acpi_enable_fixed_events(sc);
618 #endif
619
620 pmf_device_deregister(self);
621
622 #if 0
623 sysmon_power_settype("acpi");
624 #endif
625 acpi_softc = NULL;
626
627 return 0;
628 }
629
630 static bool
631 acpi_suspend(device_t dv, const pmf_qual_t *qual)
632 {
633 acpi_suspended = 1;
634 return true;
635 }
636
637 static bool
638 acpi_resume(device_t dv, const pmf_qual_t *qual)
639 {
640 acpi_suspended = 0;
641 return true;
642 }
643
644 #if 0
645 /*
646 * acpi_disable:
647 *
648 * Disable ACPI.
649 */
650 static ACPI_STATUS
651 acpi_disable(struct acpi_softc *sc)
652 {
653 ACPI_STATUS rv = AE_OK;
654
655 if (acpi_active) {
656 rv = AcpiDisable();
657 if (ACPI_SUCCESS(rv))
658 acpi_active = 0;
659 }
660 return rv;
661 }
662 #endif
663
664 struct acpi_make_devnode_state {
665 struct acpi_softc *softc;
666 struct acpi_scope *scope;
667 };
668
669 /*
670 * acpi_build_tree:
671 *
672 * Scan relevant portions of the ACPI namespace and attach
673 * child devices.
674 */
675 static void
676 acpi_build_tree(struct acpi_softc *sc)
677 {
678 static const char *scopes[] = {
679 "\\_PR_", /* ACPI 1.0 processor namespace */
680 "\\_SB_", /* system bus namespace */
681 "\\_SI_", /* system indicator namespace */
682 "\\_TZ_", /* ACPI 1.0 thermal zone namespace */
683 NULL,
684 };
685 struct acpi_make_devnode_state state;
686 struct acpi_scope *as;
687 ACPI_HANDLE parent;
688 ACPI_STATUS rv;
689 int i;
690
691 TAILQ_INIT(&sc->sc_scopes);
692
693 state.softc = sc;
694
695 /*
696 * Scan the namespace and build our tree.
697 */
698 for (i = 0; scopes[i] != NULL; i++) {
699 as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
700 as->as_name = scopes[i];
701 TAILQ_INIT(&as->as_devnodes);
702
703 TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
704
705 state.scope = as;
706
707 rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
708 &parent);
709 if (ACPI_SUCCESS(rv)) {
710 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
711 acpi_make_devnode, &state, NULL);
712 }
713 }
714
715 acpi_rescan1(sc, NULL, NULL);
716
717 acpi_wakedev_scan(sc);
718
719 acpi_pcidev_scan(sc);
720 }
721
722 static int
723 acpi_rescan(device_t self, const char *ifattr, const int *locators)
724 {
725 struct acpi_softc *sc = device_private(self);
726
727 acpi_rescan1(sc, ifattr, locators);
728 return 0;
729 }
730
731 static void
732 acpi_rescan1(struct acpi_softc *sc, const char *ifattr, const int *locators)
733 {
734 if (ifattr_match(ifattr, "acpinodebus"))
735 acpi_rescan_nodes(sc);
736
737 if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL) {
738 sc->sc_apmbus = config_found_ia(sc->sc_dev, "acpiapmbus", NULL,
739 NULL);
740 }
741 }
742
743 static void
744 acpi_rescan_nodes(struct acpi_softc *sc)
745 {
746 struct acpi_scope *as;
747
748 TAILQ_FOREACH(as, &sc->sc_scopes, as_list) {
749 struct acpi_devnode *ad;
750
751 /* Now, for this namespace, try to attach the devices. */
752 TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
753 struct acpi_attach_args aa;
754
755 if (ad->ad_device != NULL)
756 continue;
757
758 aa.aa_node = ad;
759 aa.aa_iot = sc->sc_iot;
760 aa.aa_memt = sc->sc_memt;
761 aa.aa_pc = sc->sc_pc;
762 aa.aa_pciflags = sc->sc_pciflags;
763 aa.aa_ic = sc->sc_ic;
764
765 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
766 /*
767 * XXX We only attach devices which are:
768 *
769 * - present
770 * - enabled
771 * - functioning properly
772 *
773 * However, if enabled, it's decoding resources,
774 * so we should claim them, if possible.
775 * Requires changes to bus_space(9).
776 */
777 if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
778 ACPI_VALID_STA &&
779 (ad->ad_devinfo->CurrentStatus &
780 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
781 ACPI_STA_DEV_OK)) !=
782 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
783 ACPI_STA_DEV_OK))
784 continue;
785 }
786
787 /*
788 * XXX Same problem as above...
789 *
790 * Do this check only for devices, as e.g.
791 * a Thermal Zone doesn't have a HID.
792 */
793 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE &&
794 (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
795 continue;
796
797 /*
798 * Handled internally
799 */
800 if (ad->ad_devinfo->Type == ACPI_TYPE_PROCESSOR ||
801 ad->ad_devinfo->Type == ACPI_TYPE_POWER)
802 continue;
803
804 /*
805 * Skip ignored HIDs
806 */
807 if (acpi_match_hid(ad->ad_devinfo, acpi_ignored_ids))
808 continue;
809
810 ad->ad_device = config_found_ia(sc->sc_dev,
811 "acpinodebus", &aa, acpi_print);
812 }
813 }
814 }
815
816 #ifdef ACPI_ACTIVATE_DEV
817 static void
818 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
819 {
820 ACPI_STATUS rv;
821 ACPI_DEVICE_INFO *newdi;
822
823 #ifdef ACPI_DEBUG
824 aprint_normal("acpi_activate_device: %s, old status=%x\n",
825 (*di)->HardwareId.Value, (*di)->CurrentStatus);
826 #endif
827
828 rv = acpi_allocate_resources(handle);
829 if (ACPI_FAILURE(rv)) {
830 aprint_error("acpi: activate failed for %s\n",
831 (*di)->HardwareId.String);
832 } else {
833 aprint_verbose("acpi: activated %s\n",
834 (*di)->HardwareId.String);
835 }
836
837 (void)AcpiGetObjectInfo(handle, &newdi);
838 ACPI_FREE(*di);
839 *di = newdi;
840
841 #ifdef ACPI_DEBUG
842 aprint_normal("acpi_activate_device: %s, new status=%x\n",
843 (*di)->HardwareId.Value, (*di)->CurrentStatus);
844 #endif
845 }
846 #endif /* ACPI_ACTIVATE_DEV */
847
848 /*
849 * acpi_make_devnode:
850 *
851 * Make an ACPI devnode.
852 */
853 static ACPI_STATUS
854 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
855 void **status)
856 {
857 struct acpi_make_devnode_state *state = context;
858 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
859 struct acpi_softc *sc = state->softc;
860 #endif
861 struct acpi_scope *as = state->scope;
862 struct acpi_devnode *ad;
863 ACPI_OBJECT_TYPE type;
864 ACPI_DEVICE_INFO *devinfo;
865 ACPI_STATUS rv;
866 ACPI_NAME_UNION *anu;
867 int i, clear = 0;
868
869 rv = AcpiGetType(handle, &type);
870 if (ACPI_SUCCESS(rv)) {
871 rv = AcpiGetObjectInfo(handle, &devinfo);
872 if (ACPI_FAILURE(rv)) {
873 #ifdef ACPI_DEBUG
874 aprint_normal_dev(sc->sc_dev,
875 "AcpiGetObjectInfo failed: %s\n",
876 AcpiFormatException(rv));
877 #endif
878 goto out; /* XXX why return OK */
879 }
880
881 switch (type) {
882 case ACPI_TYPE_DEVICE:
883 #ifdef ACPI_ACTIVATE_DEV
884 if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
885 (ACPI_VALID_STA|ACPI_VALID_HID) &&
886 (devinfo->CurrentStatus &
887 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
888 ACPI_STA_DEV_PRESENT)
889 acpi_activate_device(handle, &devinfo);
890
891 /* FALLTHROUGH */
892 #endif
893
894 case ACPI_TYPE_PROCESSOR:
895 case ACPI_TYPE_THERMAL:
896 case ACPI_TYPE_POWER:
897 ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
898 if (ad == NULL)
899 return AE_NO_MEMORY;
900
901 ad->ad_devinfo = devinfo;
902 ad->ad_handle = handle;
903 ad->ad_level = level;
904 ad->ad_scope = as;
905 ad->ad_type = type;
906
907 anu = (ACPI_NAME_UNION *)&devinfo->Name;
908 ad->ad_name[4] = '\0';
909 for (i = 3, clear = 0; i >= 0; i--) {
910 if (!clear && anu->Ascii[i] == '_')
911 ad->ad_name[i] = '\0';
912 else {
913 ad->ad_name[i] = anu->Ascii[i];
914 clear = 1;
915 }
916 }
917 if (ad->ad_name[0] == '\0')
918 ad->ad_name[0] = '_';
919
920 TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
921
922 if (type == ACPI_TYPE_DEVICE &&
923 (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
924 goto out;
925
926 #ifdef ACPI_EXTRA_DEBUG
927 aprint_normal_dev(sc->sc_dev,
928 "HID %s found in scope %s level %d\n",
929 ad->ad_devinfo->HardwareId.String,
930 as->as_name, ad->ad_level);
931 if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
932 aprint_normal(" UID %s\n",
933 ad->ad_devinfo->UniqueId.String);
934 if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
935 aprint_normal(" ADR 0x%016" PRIx64 "\n",
936 ad->ad_devinfo->Address);
937 if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
938 aprint_normal(" STA 0x%08x\n",
939 ad->ad_devinfo->CurrentStatus);
940 #endif
941 }
942 }
943 out:
944 return AE_OK;
945 }
946
947 /*
948 * acpi_print:
949 *
950 * Autoconfiguration print routine for ACPI node bus.
951 */
952 static int
953 acpi_print(void *aux, const char *pnp)
954 {
955 struct acpi_attach_args *aa = aux;
956 ACPI_STATUS rv;
957
958 if (pnp) {
959 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
960 char *pnpstr =
961 aa->aa_node->ad_devinfo->HardwareId.String;
962 ACPI_BUFFER buf;
963
964 aprint_normal("%s (%s) ", aa->aa_node->ad_name,
965 pnpstr);
966
967 rv = acpi_eval_struct(aa->aa_node->ad_handle,
968 "_STR", &buf);
969 if (ACPI_SUCCESS(rv)) {
970 ACPI_OBJECT *obj = buf.Pointer;
971 switch (obj->Type) {
972 case ACPI_TYPE_STRING:
973 aprint_normal("[%s] ", obj->String.Pointer);
974 break;
975 case ACPI_TYPE_BUFFER:
976 aprint_normal("buffer %p ", obj->Buffer.Pointer);
977 break;
978 default:
979 aprint_normal("type %d ",obj->Type);
980 break;
981 }
982 ACPI_FREE(buf.Pointer);
983 }
984 #ifdef ACPIVERBOSE
985 else {
986 int i;
987
988 for (i = 0; i < __arraycount(acpi_knowndevs);
989 i++) {
990 if (strcmp(acpi_knowndevs[i].pnp,
991 pnpstr) == 0) {
992 aprint_normal("[%s] ",
993 acpi_knowndevs[i].str);
994 }
995 }
996 }
997
998 #endif
999 aprint_normal("at %s", pnp);
1000 } else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
1001 aprint_normal("%s (ACPI Object Type '%s' "
1002 "[0x%02x]) ", aa->aa_node->ad_name,
1003 AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
1004 aa->aa_node->ad_devinfo->Type);
1005 aprint_normal("at %s", pnp);
1006 } else
1007 return 0;
1008 } else {
1009 aprint_normal(" (%s", aa->aa_node->ad_name);
1010 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1011 aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
1012 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
1013 const char *uid;
1014
1015 uid = aa->aa_node->ad_devinfo->UniqueId.String;
1016 if (uid[0] == '\0')
1017 uid = "<null>";
1018 aprint_normal("-%s", uid);
1019 }
1020 }
1021 aprint_normal(")");
1022 }
1023
1024 return UNCONF;
1025 }
1026
1027 /*****************************************************************************
1028 * ACPI fixed-hardware feature handlers
1029 *****************************************************************************/
1030
1031 static UINT32 acpi_fixed_button_handler(void *);
1032 static void acpi_fixed_button_pressed(void *);
1033
1034 /*
1035 * acpi_enable_fixed_events:
1036 *
1037 * Enable any fixed-hardware feature handlers.
1038 */
1039 static void
1040 acpi_enable_fixed_events(struct acpi_softc *sc)
1041 {
1042 static int beenhere;
1043 ACPI_STATUS rv;
1044
1045 KASSERT(beenhere == 0);
1046 beenhere = 1;
1047
1048 /*
1049 * Check for fixed-hardware buttons.
1050 */
1051 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
1052 aprint_verbose_dev(sc->sc_dev,
1053 "fixed-feature power button present\n");
1054 sc->sc_smpsw_power.smpsw_name = device_xname(sc->sc_dev);
1055 sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
1056 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
1057 aprint_error_dev(sc->sc_dev,
1058 "unable to register fixed power "
1059 "button with sysmon\n");
1060 } else {
1061 rv = AcpiInstallFixedEventHandler(
1062 ACPI_EVENT_POWER_BUTTON,
1063 acpi_fixed_button_handler, &sc->sc_smpsw_power);
1064 if (ACPI_FAILURE(rv)) {
1065 aprint_error_dev(sc->sc_dev,
1066 "unable to install handler "
1067 "for fixed power button: %s\n",
1068 AcpiFormatException(rv));
1069 }
1070 }
1071 }
1072
1073 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1074 aprint_verbose_dev(sc->sc_dev,
1075 "fixed-feature sleep button present\n");
1076 sc->sc_smpsw_sleep.smpsw_name = device_xname(sc->sc_dev);
1077 sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
1078 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
1079 aprint_error_dev(sc->sc_dev,
1080 "unable to register fixed sleep "
1081 "button with sysmon\n");
1082 } else {
1083 rv = AcpiInstallFixedEventHandler(
1084 ACPI_EVENT_SLEEP_BUTTON,
1085 acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
1086 if (ACPI_FAILURE(rv)) {
1087 aprint_error_dev(sc->sc_dev,
1088 "unable to install handler "
1089 "for fixed sleep button: %s\n",
1090 AcpiFormatException(rv));
1091 }
1092 }
1093 }
1094 }
1095
1096 /*
1097 * acpi_fixed_button_handler:
1098 *
1099 * Event handler for the fixed buttons.
1100 */
1101 static UINT32
1102 acpi_fixed_button_handler(void *context)
1103 {
1104 struct sysmon_pswitch *smpsw = context;
1105 ACPI_STATUS rv;
1106
1107 #ifdef ACPI_BUT_DEBUG
1108 printf("%s: fixed button handler\n", smpsw->smpsw_name);
1109 #endif
1110
1111 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
1112 acpi_fixed_button_pressed, smpsw);
1113 if (ACPI_FAILURE(rv))
1114 printf("%s: WARNING: unable to queue fixed button pressed "
1115 "callback: %s\n", smpsw->smpsw_name,
1116 AcpiFormatException(rv));
1117
1118 return ACPI_INTERRUPT_HANDLED;
1119 }
1120
1121 /*
1122 * acpi_fixed_button_pressed:
1123 *
1124 * Deal with a fixed button being pressed.
1125 */
1126 static void
1127 acpi_fixed_button_pressed(void *context)
1128 {
1129 struct sysmon_pswitch *smpsw = context;
1130
1131 #ifdef ACPI_BUT_DEBUG
1132 printf("%s: fixed button pressed, calling sysmon\n",
1133 smpsw->smpsw_name);
1134 #endif
1135
1136 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1137 }
1138
1139 /*****************************************************************************
1140 * ACPI utility routines.
1141 *****************************************************************************/
1142
1143 /*
1144 * acpi_eval_integer:
1145 *
1146 * Evaluate an integer object.
1147 */
1148 ACPI_STATUS
1149 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
1150 {
1151 ACPI_STATUS rv;
1152 ACPI_BUFFER buf;
1153 ACPI_OBJECT param;
1154
1155 if (handle == NULL)
1156 handle = ACPI_ROOT_OBJECT;
1157
1158 buf.Pointer = ¶m;
1159 buf.Length = sizeof(param);
1160
1161 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf,
1162 ACPI_TYPE_INTEGER);
1163 if (ACPI_SUCCESS(rv))
1164 *valp = param.Integer.Value;
1165
1166 return rv;
1167 }
1168
1169 /*
1170 * acpi_eval_set_integer:
1171 *
1172 * Evaluate an integer object with a single integer input parameter.
1173 */
1174 ACPI_STATUS
1175 acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER arg)
1176 {
1177 ACPI_OBJECT param_arg;
1178 ACPI_OBJECT_LIST param_args;
1179
1180 if (handle == NULL)
1181 handle = ACPI_ROOT_OBJECT;
1182
1183 param_arg.Type = ACPI_TYPE_INTEGER;
1184 param_arg.Integer.Value = arg;
1185
1186 param_args.Count = 1;
1187 param_args.Pointer = ¶m_arg;
1188
1189 return AcpiEvaluateObject(handle, path, ¶m_args, NULL);
1190 }
1191
1192 /*
1193 * acpi_eval_string:
1194 *
1195 * Evaluate a (Unicode) string object.
1196 */
1197 ACPI_STATUS
1198 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
1199 {
1200 ACPI_OBJECT *obj;
1201 ACPI_BUFFER buf;
1202 ACPI_STATUS rv;
1203
1204 rv = acpi_eval_struct(handle, path, &buf);
1205
1206 if (ACPI_FAILURE(rv))
1207 return rv;
1208
1209 obj = buf.Pointer;
1210
1211 if (obj->Type != ACPI_TYPE_STRING) {
1212 rv = AE_TYPE;
1213 goto out;
1214 }
1215
1216 if (obj->String.Length == 0) {
1217 rv = AE_BAD_DATA;
1218 goto out;
1219 }
1220
1221 *stringp = ACPI_ALLOCATE(obj->String.Length + 1);
1222
1223 if (*stringp == NULL) {
1224 rv = AE_NO_MEMORY;
1225 goto out;
1226 }
1227
1228 (void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
1229
1230 (*stringp)[obj->String.Length] = '\0';
1231
1232 out:
1233 ACPI_FREE(buf.Pointer);
1234
1235 return rv;
1236 }
1237
1238 /*
1239 * acpi_eval_struct:
1240 *
1241 * Evaluate a more complex structure.
1242 * Caller must free buf.Pointer by ACPI_FREE().
1243 */
1244 ACPI_STATUS
1245 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
1246 {
1247 ACPI_STATUS rv;
1248
1249 if (handle == NULL)
1250 handle = ACPI_ROOT_OBJECT;
1251
1252 bufp->Pointer = NULL;
1253 bufp->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
1254
1255 rv = AcpiEvaluateObject(handle, path, NULL, bufp);
1256
1257 return rv;
1258 }
1259
1260 /*
1261 * acpi_eval_reference_handle:
1262 *
1263 * Evaluate a reference handle from an element in a package.
1264 */
1265 ACPI_STATUS
1266 acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
1267 {
1268
1269 if (elm == NULL || handle == NULL)
1270 return AE_BAD_PARAMETER;
1271
1272 switch (elm->Type) {
1273
1274 case ACPI_TYPE_ANY:
1275 case ACPI_TYPE_LOCAL_REFERENCE:
1276
1277 if (elm->Reference.Handle == NULL)
1278 return AE_NULL_ENTRY;
1279
1280 *handle = elm->Reference.Handle;
1281
1282 return AE_OK;
1283
1284 case ACPI_TYPE_STRING:
1285 return AcpiGetHandle(NULL, elm->String.Pointer, handle);
1286
1287 default:
1288 return AE_TYPE;
1289 }
1290 }
1291
1292 /*
1293 * acpi_foreach_package_object:
1294 *
1295 * Iterate over all objects in a package, and pass them all
1296 * to a function. If the called function returns non AE_OK, the
1297 * iteration is stopped and that value is returned.
1298 */
1299 ACPI_STATUS
1300 acpi_foreach_package_object(ACPI_OBJECT *pkg,
1301 ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
1302 void *arg)
1303 {
1304 ACPI_STATUS rv = AE_OK;
1305 int i;
1306
1307 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1308 return AE_BAD_PARAMETER;
1309
1310 for (i = 0; i < pkg->Package.Count; i++) {
1311 rv = (*func)(&pkg->Package.Elements[i], arg);
1312 if (ACPI_FAILURE(rv))
1313 break;
1314 }
1315
1316 return rv;
1317 }
1318
1319 /*
1320 * acpi_name:
1321 *
1322 * Return a complete pathname from a handle.
1323 *
1324 * Note that the function uses static data storage;
1325 * if the data is needed for future use, it should be
1326 * copied before any subsequent calls overwrite it.
1327 */
1328 const char *
1329 acpi_name(ACPI_HANDLE handle)
1330 {
1331 static char buffer[80];
1332 ACPI_BUFFER buf;
1333 ACPI_STATUS rv;
1334
1335 buf.Length = sizeof(buffer);
1336 buf.Pointer = buffer;
1337
1338 rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
1339 if (ACPI_FAILURE(rv))
1340 return "(unknown acpi path)";
1341 return buffer;
1342 }
1343
1344 /*
1345 * acpi_get:
1346 *
1347 * Fetch data info the specified (empty) ACPI buffer.
1348 * Caller must free buf.Pointer by ACPI_FREE().
1349 */
1350 ACPI_STATUS
1351 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
1352 ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
1353 {
1354 buf->Pointer = NULL;
1355 buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
1356
1357 return (*getit)(handle, buf);
1358 }
1359
1360
1361 /*
1362 * acpi_match_hid
1363 *
1364 * Match given ids against _HID and _CIDs.
1365 */
1366 int
1367 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
1368 {
1369 int i;
1370
1371 while (*ids) {
1372 if (ad->Valid & ACPI_VALID_HID) {
1373 if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
1374 return 1;
1375 }
1376
1377 if (ad->Valid & ACPI_VALID_CID) {
1378 for (i = 0; i < ad->CompatibleIdList.Count; i++) {
1379 if (pmatch(ad->CompatibleIdList.Ids[i].String, *ids, NULL) == 2)
1380 return 1;
1381 }
1382 }
1383 ids++;
1384 }
1385
1386 return 0;
1387 }
1388
1389 /*
1390 * acpi_wake_gpe_helper
1391 *
1392 * Set/unset GPE as both Runtime and Wake.
1393 */
1394 static void
1395 acpi_wake_gpe_helper(ACPI_HANDLE handle, bool enable)
1396 {
1397 ACPI_OBJECT *elm, *obj;
1398 ACPI_INTEGER val;
1399 ACPI_BUFFER buf;
1400 ACPI_STATUS rv;
1401
1402 rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
1403
1404 if (ACPI_FAILURE(rv))
1405 return;
1406
1407 obj = buf.Pointer;
1408
1409 if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count < 2)
1410 goto out;
1411
1412 /*
1413 * As noted in ACPI 3.0 (section 7.2.10), the _PRW object is
1414 * a package in which the first element is either an integer
1415 * or again a package. In the latter case the package inside
1416 * the package element has two elements, a reference handle
1417 * and the GPE number.
1418 */
1419 elm = &obj->Package.Elements[0];
1420
1421 switch (elm->Type) {
1422
1423 case ACPI_TYPE_INTEGER:
1424 val = elm->Integer.Value;
1425 break;
1426
1427 case ACPI_TYPE_PACKAGE:
1428
1429 if (elm->Package.Count < 2)
1430 goto out;
1431
1432 if (elm->Package.Elements[0].Type != ACPI_TYPE_LOCAL_REFERENCE)
1433 goto out;
1434
1435 if (elm->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
1436 goto out;
1437
1438 val = elm->Package.Elements[1].Integer.Value;
1439 break;
1440
1441 default:
1442 goto out;
1443 }
1444
1445 if (enable) {
1446 (void)AcpiSetGpeType(NULL, val, ACPI_GPE_TYPE_WAKE_RUN);
1447 (void)AcpiEnableGpe(NULL, val, ACPI_NOT_ISR);
1448 } else
1449 (void)AcpiDisableGpe(NULL, val, ACPI_NOT_ISR);
1450
1451 out:
1452 ACPI_FREE(buf.Pointer);
1453 }
1454
1455 /*
1456 * acpi_clear_wake_gpe
1457 *
1458 * Clear GPE as both Runtime and Wake.
1459 */
1460 void
1461 acpi_clear_wake_gpe(ACPI_HANDLE handle)
1462 {
1463 acpi_wake_gpe_helper(handle, false);
1464 }
1465
1466 /*
1467 * acpi_set_wake_gpe
1468 *
1469 * Set GPE as both Runtime and Wake.
1470 */
1471 void
1472 acpi_set_wake_gpe(ACPI_HANDLE handle)
1473 {
1474 acpi_wake_gpe_helper(handle, true);
1475 }
1476
1477
1478 /*****************************************************************************
1479 * ACPI sleep support.
1480 *****************************************************************************/
1481
1482 static int
1483 is_available_state(struct acpi_softc *sc, int state)
1484 {
1485 UINT8 type_a, type_b;
1486
1487 return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
1488 &type_a, &type_b));
1489 }
1490
1491 /*
1492 * acpi_enter_sleep_state:
1493 *
1494 * Enter to the specified sleep state.
1495 */
1496
1497 ACPI_STATUS
1498 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1499 {
1500 int err;
1501 ACPI_STATUS ret = AE_OK;
1502
1503 if (state == acpi_sleepstate)
1504 return AE_OK;
1505
1506 aprint_normal_dev(sc->sc_dev, "entering state %d\n", state);
1507
1508 switch (state) {
1509 case ACPI_STATE_S0:
1510 break;
1511 case ACPI_STATE_S1:
1512 case ACPI_STATE_S2:
1513 case ACPI_STATE_S3:
1514 case ACPI_STATE_S4:
1515 if (!is_available_state(sc, state)) {
1516 aprint_error_dev(sc->sc_dev,
1517 "ACPI S%d not available on this platform\n", state);
1518 break;
1519 }
1520
1521 acpi_wakedev_commit(sc);
1522
1523 if (state != ACPI_STATE_S1 && !pmf_system_suspend(PMF_Q_NONE)) {
1524 aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1525 break;
1526 }
1527
1528 ret = AcpiEnterSleepStatePrep(state);
1529 if (ACPI_FAILURE(ret)) {
1530 aprint_error_dev(sc->sc_dev,
1531 "failed preparing to sleep (%s)\n",
1532 AcpiFormatException(ret));
1533 break;
1534 }
1535
1536 acpi_sleepstate = state;
1537 if (state == ACPI_STATE_S1) {
1538 /* just enter the state */
1539 acpi_md_OsDisableInterrupt();
1540 ret = AcpiEnterSleepState((UINT8)state);
1541 if (ACPI_FAILURE(ret))
1542 aprint_error_dev(sc->sc_dev,
1543 "failed to enter sleep state S1: %s\n",
1544 AcpiFormatException(ret));
1545 AcpiLeaveSleepState((UINT8)state);
1546 } else {
1547 err = acpi_md_sleep(state);
1548 if (state == ACPI_STATE_S4)
1549 AcpiEnable();
1550 pmf_system_bus_resume(PMF_Q_NONE);
1551 AcpiLeaveSleepState((UINT8)state);
1552 pmf_system_resume(PMF_Q_NONE);
1553 }
1554
1555 break;
1556 case ACPI_STATE_S5:
1557 ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1558 if (ACPI_FAILURE(ret)) {
1559 aprint_error_dev(sc->sc_dev,
1560 "failed preparing to sleep (%s)\n",
1561 AcpiFormatException(ret));
1562 break;
1563 }
1564 DELAY(1000000);
1565 acpi_sleepstate = state;
1566 acpi_md_OsDisableInterrupt();
1567 AcpiEnterSleepState(ACPI_STATE_S5);
1568 aprint_error_dev(sc->sc_dev, "WARNING powerdown failed!\n");
1569 break;
1570 }
1571
1572 acpi_sleepstate = ACPI_STATE_S0;
1573 return ret;
1574 }
1575
1576 #if defined(ACPI_ACTIVATE_DEV)
1577 /* XXX This very incomplete */
1578 ACPI_STATUS
1579 acpi_allocate_resources(ACPI_HANDLE handle)
1580 {
1581 ACPI_BUFFER bufp, bufc, bufn;
1582 ACPI_RESOURCE *resp, *resc, *resn;
1583 ACPI_RESOURCE_IRQ *irq;
1584 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1585 ACPI_STATUS rv;
1586 uint delta;
1587
1588 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1589 if (ACPI_FAILURE(rv))
1590 goto out;
1591 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1592 if (ACPI_FAILURE(rv)) {
1593 goto out1;
1594 }
1595
1596 bufn.Length = 1000;
1597 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1598 resp = bufp.Pointer;
1599 resc = bufc.Pointer;
1600 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
1601 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1602 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
1603 resp = ACPI_NEXT_RESOURCE(resp);
1604 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
1605 break;
1606 /* Found identical Id */
1607 resn->Type = resc->Type;
1608 switch (resc->Type) {
1609 case ACPI_RESOURCE_TYPE_IRQ:
1610 memcpy(&resn->Data, &resp->Data,
1611 sizeof(ACPI_RESOURCE_IRQ));
1612 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1613 irq->Interrupts[0] =
1614 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1615 Interrupts[irq->InterruptCount-1];
1616 irq->InterruptCount = 1;
1617 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
1618 break;
1619 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1620 memcpy(&resn->Data, &resp->Data,
1621 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
1622 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
1623 #if 0
1624 /*
1625 * XXX not duplicating the interrupt logic above
1626 * because its not clear what it accomplishes.
1627 */
1628 xirq->Interrupts[0] =
1629 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
1630 Interrupts[irq->NumberOfInterrupts-1];
1631 xirq->NumberOfInterrupts = 1;
1632 #endif
1633 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
1634 break;
1635 case ACPI_RESOURCE_TYPE_IO:
1636 memcpy(&resn->Data, &resp->Data,
1637 sizeof(ACPI_RESOURCE_IO));
1638 resn->Length = resp->Length;
1639 break;
1640 default:
1641 printf("acpi_allocate_resources: res=%d\n", resc->Type);
1642 rv = AE_BAD_DATA;
1643 goto out2;
1644 }
1645 resc = ACPI_NEXT_RESOURCE(resc);
1646 resn = ACPI_NEXT_RESOURCE(resn);
1647 resp = ACPI_NEXT_RESOURCE(resp);
1648 delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1649 if (delta >=
1650 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
1651 bufn.Length *= 2;
1652 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1653 M_ACPI, M_WAITOK);
1654 resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1655 }
1656 }
1657 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1658 printf("acpi_allocate_resources: resc not exhausted\n");
1659 rv = AE_BAD_DATA;
1660 goto out3;
1661 }
1662
1663 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
1664 rv = AcpiSetCurrentResources(handle, &bufn);
1665 if (ACPI_FAILURE(rv)) {
1666 printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1667 AcpiFormatException(rv));
1668 }
1669
1670 out3:
1671 free(bufn.Pointer, M_ACPI);
1672 out2:
1673 ACPI_FREE(bufc.Pointer);
1674 out1:
1675 ACPI_FREE(bufp.Pointer);
1676 out:
1677 return rv;
1678 }
1679 #endif /* ACPI_ACTIVATE_DEV */
1680
1681 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1682 {
1683 const struct sysctlnode *node;
1684 const struct sysctlnode *ssnode;
1685
1686 if (sysctl_createv(clog, 0, NULL, NULL,
1687 CTLFLAG_PERMANENT,
1688 CTLTYPE_NODE, "hw", NULL,
1689 NULL, 0, NULL, 0,
1690 CTL_HW, CTL_EOL) != 0)
1691 return;
1692
1693 if (sysctl_createv(clog, 0, NULL, &node,
1694 CTLFLAG_PERMANENT,
1695 CTLTYPE_NODE, "acpi", NULL,
1696 NULL, 0, NULL, 0,
1697 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
1698 return;
1699
1700 sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
1701 CTLTYPE_QUAD, "root",
1702 SYSCTL_DESCR("ACPI root pointer"),
1703 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1704 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1705 sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
1706 CTLTYPE_STRING, "supported_states",
1707 SYSCTL_DESCR("Supported ACPI system states"),
1708 NULL, 0, acpi_supported_states, 0,
1709 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1710
1711 /* ACPI sleepstate sysctl */
1712 if (sysctl_createv(NULL, 0, NULL, &node,
1713 CTLFLAG_PERMANENT,
1714 CTLTYPE_NODE, "machdep", NULL,
1715 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
1716 return;
1717 if (sysctl_createv(NULL, 0, &node, &ssnode,
1718 CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
1719 NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
1720 CTL_EOL) != 0)
1721 return;
1722 }
1723
1724 static int
1725 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1726 {
1727 int error, t;
1728 struct sysctlnode node;
1729
1730 node = *rnode;
1731 t = acpi_sleepstate;
1732 node.sysctl_data = &t;
1733 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1734 if (error || newp == NULL)
1735 return error;
1736
1737 if (acpi_softc == NULL)
1738 return ENOSYS;
1739
1740 acpi_enter_sleep_state(acpi_softc, t);
1741
1742 return 0;
1743 }
1744
1745 static ACPI_TABLE_HEADER *
1746 acpi_map_rsdt(void)
1747 {
1748 ACPI_PHYSICAL_ADDRESS paddr;
1749 ACPI_TABLE_RSDP *rsdp;
1750
1751 paddr = AcpiOsGetRootPointer();
1752 if (paddr == 0) {
1753 printf("ACPI: couldn't get root pointer\n");
1754 return NULL;
1755 }
1756 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1757 if (rsdp == NULL) {
1758 printf("ACPI: couldn't map RSDP\n");
1759 return NULL;
1760 }
1761 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1762 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
1763 else
1764 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
1765 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1766
1767 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1768 }
1769
1770 static void
1771 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1772 {
1773 if (rsdt == NULL)
1774 return;
1775
1776 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1777 }
1778