acpi.c revision 1.147 1 /* $NetBSD: acpi.c,v 1.147 2010/01/18 18:49:27 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 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.147 2010/01/18 18:49:27 jruoho 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, pmf_qual_t);
205 static bool acpi_resume(device_t, 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
551 static int
552 acpi_detach(device_t self, int flags)
553 {
554 int rc;
555
556 #ifdef ACPI_DEBUGGER
557 if (acpi_dbgr & ACPI_DBGR_RUNNING)
558 acpi_osd_debugger();
559 #endif
560
561 if ((rc = config_detach_children(self, flags)) != 0)
562 return rc;
563
564 #ifdef ACPI_DEBUGGER
565 if (acpi_dbgr & ACPI_DBGR_PROBE)
566 acpi_osd_debugger();
567 #endif
568
569 if ((rc = acpitimer_detach()) != 0)
570 return rc;
571
572 #if 0
573 /*
574 * Bring ACPI on-line.
575 */
576 #ifdef ACPI_DEBUGGER
577 if (acpi_dbgr & ACPI_DBGR_ENABLE)
578 acpi_osd_debugger();
579 #endif
580
581 #define ACPI_ENABLE_PHASE1 \
582 (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
583 #define ACPI_ENABLE_PHASE2 \
584 (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
585 ACPI_NO_ADDRESS_SPACE_INIT)
586
587 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
588 if (ACPI_FAILURE(rv)) {
589 aprint_error_dev(self, "unable to enable ACPI: %s\n",
590 AcpiFormatException(rv));
591 return;
592 }
593
594 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
595 if (ACPI_FAILURE(rv)) {
596 aprint_error_dev(self, "unable to enable ACPI: %s\n",
597 AcpiFormatException(rv));
598 return;
599 }
600
601 /* Early EC handler initialization if ECDT table is available. */
602 config_found_ia(self, "acpiecdtbus", NULL, NULL);
603
604 rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
605 if (ACPI_FAILURE(rv)) {
606 aprint_error_dev(self,
607 "unable to initialize ACPI objects: %s\n",
608 AcpiFormatException(rv));
609 return;
610 }
611 acpi_active = 1;
612
613 acpi_enable_fixed_events(sc);
614 #endif
615
616 pmf_device_deregister(self);
617
618 #if 0
619 sysmon_power_settype("acpi");
620 #endif
621 acpi_softc = NULL;
622
623 return 0;
624 }
625
626 static bool
627 acpi_suspend(device_t dv, pmf_qual_t qual)
628 {
629 acpi_suspended = 1;
630 return true;
631 }
632
633 static bool
634 acpi_resume(device_t dv, pmf_qual_t qual)
635 {
636 acpi_suspended = 0;
637 return true;
638 }
639
640 #if 0
641 /*
642 * acpi_disable:
643 *
644 * Disable ACPI.
645 */
646 static ACPI_STATUS
647 acpi_disable(struct acpi_softc *sc)
648 {
649 ACPI_STATUS rv = AE_OK;
650
651 if (acpi_active) {
652 rv = AcpiDisable();
653 if (ACPI_SUCCESS(rv))
654 acpi_active = 0;
655 }
656 return rv;
657 }
658 #endif
659
660 struct acpi_make_devnode_state {
661 struct acpi_softc *softc;
662 struct acpi_scope *scope;
663 };
664
665 /*
666 * acpi_build_tree:
667 *
668 * Scan relevant portions of the ACPI namespace and attach
669 * child devices.
670 */
671 static void
672 acpi_build_tree(struct acpi_softc *sc)
673 {
674 static const char *scopes[] = {
675 "\\_PR_", /* ACPI 1.0 processor namespace */
676 "\\_SB_", /* system bus namespace */
677 "\\_SI_", /* system indicator namespace */
678 "\\_TZ_", /* ACPI 1.0 thermal zone namespace */
679 NULL,
680 };
681 struct acpi_make_devnode_state state;
682 struct acpi_scope *as;
683 ACPI_HANDLE parent;
684 ACPI_STATUS rv;
685 int i;
686
687 TAILQ_INIT(&sc->sc_scopes);
688
689 state.softc = sc;
690
691 /*
692 * Scan the namespace and build our tree.
693 */
694 for (i = 0; scopes[i] != NULL; i++) {
695 as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
696 as->as_name = scopes[i];
697 TAILQ_INIT(&as->as_devnodes);
698
699 TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
700
701 state.scope = as;
702
703 rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
704 &parent);
705 if (ACPI_SUCCESS(rv)) {
706 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
707 acpi_make_devnode, &state, NULL);
708 }
709 }
710
711 acpi_rescan1(sc, NULL, NULL);
712
713 acpi_wakedev_scan(sc);
714
715 acpi_pcidev_scan(sc);
716 }
717
718 static int
719 acpi_rescan(device_t self, const char *ifattr, const int *locators)
720 {
721 struct acpi_softc *sc = device_private(self);
722
723 acpi_rescan1(sc, ifattr, locators);
724 return 0;
725 }
726
727 static void
728 acpi_rescan1(struct acpi_softc *sc, const char *ifattr, const int *locators)
729 {
730 if (ifattr_match(ifattr, "acpinodebus"))
731 acpi_rescan_nodes(sc);
732
733 if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL) {
734 sc->sc_apmbus = config_found_ia(sc->sc_dev, "acpiapmbus", NULL,
735 NULL);
736 }
737 }
738
739 static void
740 acpi_rescan_nodes(struct acpi_softc *sc)
741 {
742 struct acpi_scope *as;
743
744 TAILQ_FOREACH(as, &sc->sc_scopes, as_list) {
745 struct acpi_devnode *ad;
746
747 /* Now, for this namespace, try to attach the devices. */
748 TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
749 struct acpi_attach_args aa;
750
751 if (ad->ad_device != NULL)
752 continue;
753
754 aa.aa_node = ad;
755 aa.aa_iot = sc->sc_iot;
756 aa.aa_memt = sc->sc_memt;
757 aa.aa_pc = sc->sc_pc;
758 aa.aa_pciflags = sc->sc_pciflags;
759 aa.aa_ic = sc->sc_ic;
760
761 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
762 /*
763 * XXX We only attach devices which are:
764 *
765 * - present
766 * - enabled
767 * - functioning properly
768 *
769 * However, if enabled, it's decoding resources,
770 * so we should claim them, if possible.
771 * Requires changes to bus_space(9).
772 */
773 if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
774 ACPI_VALID_STA &&
775 (ad->ad_devinfo->CurrentStatus &
776 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
777 ACPI_STA_DEV_OK)) !=
778 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
779 ACPI_STA_DEV_OK))
780 continue;
781 }
782
783 /*
784 * XXX Same problem as above...
785 *
786 * Do this check only for devices, as e.g.
787 * a Thermal Zone doesn't have a HID.
788 */
789 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE &&
790 (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
791 continue;
792
793 /*
794 * Handled internally
795 */
796 if (ad->ad_devinfo->Type == ACPI_TYPE_PROCESSOR ||
797 ad->ad_devinfo->Type == ACPI_TYPE_POWER)
798 continue;
799
800 /*
801 * Skip ignored HIDs
802 */
803 if (acpi_match_hid(ad->ad_devinfo, acpi_ignored_ids))
804 continue;
805
806 ad->ad_device = config_found_ia(sc->sc_dev,
807 "acpinodebus", &aa, acpi_print);
808 }
809 }
810 }
811
812 #ifdef ACPI_ACTIVATE_DEV
813 static void
814 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
815 {
816 ACPI_STATUS rv;
817 ACPI_DEVICE_INFO *newdi;
818
819 #ifdef ACPI_DEBUG
820 aprint_normal("acpi_activate_device: %s, old status=%x\n",
821 (*di)->HardwareId.Value, (*di)->CurrentStatus);
822 #endif
823
824 rv = acpi_allocate_resources(handle);
825 if (ACPI_FAILURE(rv)) {
826 aprint_error("acpi: activate failed for %s\n",
827 (*di)->HardwareId.String);
828 } else {
829 aprint_verbose("acpi: activated %s\n",
830 (*di)->HardwareId.String);
831 }
832
833 (void)AcpiGetObjectInfo(handle, &newdi);
834 ACPI_FREE(*di);
835 *di = newdi;
836
837 #ifdef ACPI_DEBUG
838 aprint_normal("acpi_activate_device: %s, new status=%x\n",
839 (*di)->HardwareId.Value, (*di)->CurrentStatus);
840 #endif
841 }
842 #endif /* ACPI_ACTIVATE_DEV */
843
844 /*
845 * acpi_make_devnode:
846 *
847 * Make an ACPI devnode.
848 */
849 static ACPI_STATUS
850 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
851 void **status)
852 {
853 struct acpi_make_devnode_state *state = context;
854 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
855 struct acpi_softc *sc = state->softc;
856 #endif
857 struct acpi_scope *as = state->scope;
858 struct acpi_devnode *ad;
859 ACPI_OBJECT_TYPE type;
860 ACPI_DEVICE_INFO *devinfo;
861 ACPI_STATUS rv;
862 ACPI_NAME_UNION *anu;
863 int i, clear = 0;
864
865 rv = AcpiGetType(handle, &type);
866 if (ACPI_SUCCESS(rv)) {
867 rv = AcpiGetObjectInfo(handle, &devinfo);
868 if (ACPI_FAILURE(rv)) {
869 #ifdef ACPI_DEBUG
870 aprint_normal_dev(sc->sc_dev,
871 "AcpiGetObjectInfo failed: %s\n",
872 AcpiFormatException(rv));
873 #endif
874 goto out; /* XXX why return OK */
875 }
876
877 switch (type) {
878 case ACPI_TYPE_DEVICE:
879 #ifdef ACPI_ACTIVATE_DEV
880 if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
881 (ACPI_VALID_STA|ACPI_VALID_HID) &&
882 (devinfo->CurrentStatus &
883 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
884 ACPI_STA_DEV_PRESENT)
885 acpi_activate_device(handle, &devinfo);
886
887 /* FALLTHROUGH */
888 #endif
889
890 case ACPI_TYPE_PROCESSOR:
891 case ACPI_TYPE_THERMAL:
892 case ACPI_TYPE_POWER:
893 ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
894 if (ad == NULL)
895 return AE_NO_MEMORY;
896
897 ad->ad_devinfo = devinfo;
898 ad->ad_handle = handle;
899 ad->ad_level = level;
900 ad->ad_scope = as;
901 ad->ad_type = type;
902
903 anu = (ACPI_NAME_UNION *)&devinfo->Name;
904 ad->ad_name[4] = '\0';
905 for (i = 3, clear = 0; i >= 0; i--) {
906 if (!clear && anu->Ascii[i] == '_')
907 ad->ad_name[i] = '\0';
908 else {
909 ad->ad_name[i] = anu->Ascii[i];
910 clear = 1;
911 }
912 }
913 if (ad->ad_name[0] == '\0')
914 ad->ad_name[0] = '_';
915
916 TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
917
918 if (type == ACPI_TYPE_DEVICE &&
919 (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
920 goto out;
921
922 #ifdef ACPI_EXTRA_DEBUG
923 aprint_normal_dev(sc->sc_dev,
924 "HID %s found in scope %s level %d\n",
925 ad->ad_devinfo->HardwareId.String,
926 as->as_name, ad->ad_level);
927 if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
928 aprint_normal(" UID %s\n",
929 ad->ad_devinfo->UniqueId.String);
930 if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
931 aprint_normal(" ADR 0x%016" PRIx64 "\n",
932 ad->ad_devinfo->Address);
933 if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
934 aprint_normal(" STA 0x%08x\n",
935 ad->ad_devinfo->CurrentStatus);
936 #endif
937 }
938 }
939 out:
940 return AE_OK;
941 }
942
943 /*
944 * acpi_print:
945 *
946 * Autoconfiguration print routine for ACPI node bus.
947 */
948 static int
949 acpi_print(void *aux, const char *pnp)
950 {
951 struct acpi_attach_args *aa = aux;
952 ACPI_STATUS rv;
953
954 if (pnp) {
955 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
956 char *pnpstr =
957 aa->aa_node->ad_devinfo->HardwareId.String;
958 ACPI_BUFFER buf;
959
960 aprint_normal("%s (%s) ", aa->aa_node->ad_name,
961 pnpstr);
962
963 rv = acpi_eval_struct(aa->aa_node->ad_handle,
964 "_STR", &buf);
965 if (ACPI_SUCCESS(rv)) {
966 ACPI_OBJECT *obj = buf.Pointer;
967 switch (obj->Type) {
968 case ACPI_TYPE_STRING:
969 aprint_normal("[%s] ", obj->String.Pointer);
970 break;
971 case ACPI_TYPE_BUFFER:
972 aprint_normal("buffer %p ", obj->Buffer.Pointer);
973 break;
974 default:
975 aprint_normal("type %d ",obj->Type);
976 break;
977 }
978 ACPI_FREE(buf.Pointer);
979 }
980 #ifdef ACPIVERBOSE
981 else {
982 int i;
983
984 for (i = 0; i < __arraycount(acpi_knowndevs);
985 i++) {
986 if (strcmp(acpi_knowndevs[i].pnp,
987 pnpstr) == 0) {
988 aprint_normal("[%s] ",
989 acpi_knowndevs[i].str);
990 }
991 }
992 }
993
994 #endif
995 aprint_normal("at %s", pnp);
996 } else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
997 aprint_normal("%s (ACPI Object Type '%s' "
998 "[0x%02x]) ", aa->aa_node->ad_name,
999 AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
1000 aa->aa_node->ad_devinfo->Type);
1001 aprint_normal("at %s", pnp);
1002 } else
1003 return 0;
1004 } else {
1005 aprint_normal(" (%s", aa->aa_node->ad_name);
1006 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
1007 aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
1008 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
1009 const char *uid;
1010
1011 uid = aa->aa_node->ad_devinfo->UniqueId.String;
1012 if (uid[0] == '\0')
1013 uid = "<null>";
1014 aprint_normal("-%s", uid);
1015 }
1016 }
1017 aprint_normal(")");
1018 }
1019
1020 return UNCONF;
1021 }
1022
1023 /*****************************************************************************
1024 * ACPI fixed-hardware feature handlers
1025 *****************************************************************************/
1026
1027 static UINT32 acpi_fixed_button_handler(void *);
1028 static void acpi_fixed_button_pressed(void *);
1029
1030 /*
1031 * acpi_enable_fixed_events:
1032 *
1033 * Enable any fixed-hardware feature handlers.
1034 */
1035 static void
1036 acpi_enable_fixed_events(struct acpi_softc *sc)
1037 {
1038 static int beenhere;
1039 ACPI_STATUS rv;
1040
1041 KASSERT(beenhere == 0);
1042 beenhere = 1;
1043
1044 /*
1045 * Check for fixed-hardware buttons.
1046 */
1047 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
1048 aprint_verbose_dev(sc->sc_dev,
1049 "fixed-feature power button present\n");
1050 sc->sc_smpsw_power.smpsw_name = device_xname(sc->sc_dev);
1051 sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
1052 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
1053 aprint_error_dev(sc->sc_dev,
1054 "unable to register fixed power "
1055 "button with sysmon\n");
1056 } else {
1057 rv = AcpiInstallFixedEventHandler(
1058 ACPI_EVENT_POWER_BUTTON,
1059 acpi_fixed_button_handler, &sc->sc_smpsw_power);
1060 if (ACPI_FAILURE(rv)) {
1061 aprint_error_dev(sc->sc_dev,
1062 "unable to install handler "
1063 "for fixed power button: %s\n",
1064 AcpiFormatException(rv));
1065 }
1066 }
1067 }
1068
1069 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1070 aprint_verbose_dev(sc->sc_dev,
1071 "fixed-feature sleep button present\n");
1072 sc->sc_smpsw_sleep.smpsw_name = device_xname(sc->sc_dev);
1073 sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
1074 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
1075 aprint_error_dev(sc->sc_dev,
1076 "unable to register fixed sleep "
1077 "button with sysmon\n");
1078 } else {
1079 rv = AcpiInstallFixedEventHandler(
1080 ACPI_EVENT_SLEEP_BUTTON,
1081 acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
1082 if (ACPI_FAILURE(rv)) {
1083 aprint_error_dev(sc->sc_dev,
1084 "unable to install handler "
1085 "for fixed sleep button: %s\n",
1086 AcpiFormatException(rv));
1087 }
1088 }
1089 }
1090 }
1091
1092 /*
1093 * acpi_fixed_button_handler:
1094 *
1095 * Event handler for the fixed buttons.
1096 */
1097 static UINT32
1098 acpi_fixed_button_handler(void *context)
1099 {
1100 struct sysmon_pswitch *smpsw = context;
1101 ACPI_STATUS rv;
1102
1103 #ifdef ACPI_BUT_DEBUG
1104 printf("%s: fixed button handler\n", smpsw->smpsw_name);
1105 #endif
1106
1107 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
1108 acpi_fixed_button_pressed, smpsw);
1109 if (ACPI_FAILURE(rv))
1110 printf("%s: WARNING: unable to queue fixed button pressed "
1111 "callback: %s\n", smpsw->smpsw_name,
1112 AcpiFormatException(rv));
1113
1114 return ACPI_INTERRUPT_HANDLED;
1115 }
1116
1117 /*
1118 * acpi_fixed_button_pressed:
1119 *
1120 * Deal with a fixed button being pressed.
1121 */
1122 static void
1123 acpi_fixed_button_pressed(void *context)
1124 {
1125 struct sysmon_pswitch *smpsw = context;
1126
1127 #ifdef ACPI_BUT_DEBUG
1128 printf("%s: fixed button pressed, calling sysmon\n",
1129 smpsw->smpsw_name);
1130 #endif
1131
1132 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1133 }
1134
1135 /*****************************************************************************
1136 * ACPI utility routines.
1137 *****************************************************************************/
1138
1139 /*
1140 * acpi_eval_integer:
1141 *
1142 * Evaluate an integer object.
1143 */
1144 ACPI_STATUS
1145 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
1146 {
1147 ACPI_STATUS rv;
1148 ACPI_BUFFER buf;
1149 ACPI_OBJECT param;
1150
1151 if (handle == NULL)
1152 handle = ACPI_ROOT_OBJECT;
1153
1154 buf.Pointer = ¶m;
1155 buf.Length = sizeof(param);
1156
1157 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf,
1158 ACPI_TYPE_INTEGER);
1159 if (ACPI_SUCCESS(rv))
1160 *valp = param.Integer.Value;
1161
1162 return rv;
1163 }
1164
1165 /*
1166 * acpi_eval_set_integer:
1167 *
1168 * Evaluate an integer object with a single integer input parameter.
1169 */
1170 ACPI_STATUS
1171 acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER arg)
1172 {
1173 ACPI_OBJECT param_arg;
1174 ACPI_OBJECT_LIST param_args;
1175
1176 if (handle == NULL)
1177 handle = ACPI_ROOT_OBJECT;
1178
1179 param_arg.Type = ACPI_TYPE_INTEGER;
1180 param_arg.Integer.Value = arg;
1181
1182 param_args.Count = 1;
1183 param_args.Pointer = ¶m_arg;
1184
1185 return AcpiEvaluateObject(handle, path, ¶m_args, NULL);
1186 }
1187
1188 /*
1189 * acpi_eval_string:
1190 *
1191 * Evaluate a (Unicode) string object.
1192 */
1193 ACPI_STATUS
1194 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
1195 {
1196 ACPI_OBJECT *obj;
1197 ACPI_BUFFER buf;
1198 ACPI_STATUS rv;
1199
1200 rv = acpi_eval_struct(handle, path, &buf);
1201
1202 if (ACPI_FAILURE(rv))
1203 return rv;
1204
1205 obj = buf.Pointer;
1206
1207 if (obj->Type != ACPI_TYPE_STRING) {
1208 rv = AE_TYPE;
1209 goto out;
1210 }
1211
1212 if (obj->String.Length == 0) {
1213 rv = AE_BAD_DATA;
1214 goto out;
1215 }
1216
1217 *stringp = ACPI_ALLOCATE(obj->String.Length + 1);
1218
1219 if (*stringp == NULL) {
1220 rv = AE_NO_MEMORY;
1221 goto out;
1222 }
1223
1224 (void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
1225
1226 (*stringp)[obj->String.Length] = '\0';
1227
1228 out:
1229 ACPI_FREE(buf.Pointer);
1230
1231 return rv;
1232 }
1233
1234 /*
1235 * acpi_eval_struct:
1236 *
1237 * Evaluate a more complex structure.
1238 * Caller must free buf.Pointer by ACPI_FREE().
1239 */
1240 ACPI_STATUS
1241 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
1242 {
1243 ACPI_STATUS rv;
1244
1245 if (handle == NULL)
1246 handle = ACPI_ROOT_OBJECT;
1247
1248 bufp->Pointer = NULL;
1249 bufp->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
1250
1251 rv = AcpiEvaluateObject(handle, path, NULL, bufp);
1252
1253 return rv;
1254 }
1255
1256 /*
1257 * acpi_eval_reference_handle:
1258 *
1259 * Evaluate a reference handle from an element in a package.
1260 */
1261 ACPI_STATUS
1262 acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
1263 {
1264
1265 if (elm == NULL || handle == NULL)
1266 return AE_BAD_PARAMETER;
1267
1268 switch (elm->Type) {
1269
1270 case ACPI_TYPE_ANY:
1271 case ACPI_TYPE_LOCAL_REFERENCE:
1272
1273 if (elm->Reference.Handle == NULL)
1274 return AE_NULL_ENTRY;
1275
1276 *handle = elm->Reference.Handle;
1277
1278 return AE_OK;
1279
1280 case ACPI_TYPE_STRING:
1281 return AcpiGetHandle(NULL, elm->String.Pointer, handle);
1282
1283 default:
1284 return AE_TYPE;
1285 }
1286 }
1287
1288 /*
1289 * acpi_foreach_package_object:
1290 *
1291 * Iterate over all objects in a package, and pass them all
1292 * to a function. If the called function returns non AE_OK, the
1293 * iteration is stopped and that value is returned.
1294 */
1295 ACPI_STATUS
1296 acpi_foreach_package_object(ACPI_OBJECT *pkg,
1297 ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
1298 void *arg)
1299 {
1300 ACPI_STATUS rv = AE_OK;
1301 int i;
1302
1303 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1304 return AE_BAD_PARAMETER;
1305
1306 for (i = 0; i < pkg->Package.Count; i++) {
1307 rv = (*func)(&pkg->Package.Elements[i], arg);
1308 if (ACPI_FAILURE(rv))
1309 break;
1310 }
1311
1312 return rv;
1313 }
1314
1315 /*
1316 * acpi_name:
1317 *
1318 * Return a complete pathname from a handle.
1319 *
1320 * Note that the function uses static data storage;
1321 * if the data is needed for future use, it should be
1322 * copied before any subsequent calls overwrite it.
1323 */
1324 const char *
1325 acpi_name(ACPI_HANDLE handle)
1326 {
1327 static char buffer[80];
1328 ACPI_BUFFER buf;
1329 ACPI_STATUS rv;
1330
1331 buf.Length = sizeof(buffer);
1332 buf.Pointer = buffer;
1333
1334 rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
1335 if (ACPI_FAILURE(rv))
1336 return "(unknown acpi path)";
1337 return buffer;
1338 }
1339
1340 /*
1341 * acpi_get:
1342 *
1343 * Fetch data info the specified (empty) ACPI buffer.
1344 * Caller must free buf.Pointer by ACPI_FREE().
1345 */
1346 ACPI_STATUS
1347 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
1348 ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
1349 {
1350 buf->Pointer = NULL;
1351 buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
1352
1353 return (*getit)(handle, buf);
1354 }
1355
1356
1357 /*
1358 * acpi_match_hid
1359 *
1360 * Match given ids against _HID and _CIDs.
1361 */
1362 int
1363 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
1364 {
1365 int i;
1366
1367 while (*ids) {
1368 if (ad->Valid & ACPI_VALID_HID) {
1369 if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
1370 return 1;
1371 }
1372
1373 if (ad->Valid & ACPI_VALID_CID) {
1374 for (i = 0; i < ad->CompatibleIdList.Count; i++) {
1375 if (pmatch(ad->CompatibleIdList.Ids[i].String, *ids, NULL) == 2)
1376 return 1;
1377 }
1378 }
1379 ids++;
1380 }
1381
1382 return 0;
1383 }
1384
1385 /*
1386 * acpi_wake_gpe_helper
1387 *
1388 * Set/unset GPE as both Runtime and Wake.
1389 */
1390 static void
1391 acpi_wake_gpe_helper(ACPI_HANDLE handle, bool enable)
1392 {
1393 ACPI_OBJECT *elm, *obj;
1394 ACPI_INTEGER val;
1395 ACPI_BUFFER buf;
1396 ACPI_STATUS rv;
1397
1398 rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
1399
1400 if (ACPI_FAILURE(rv))
1401 return;
1402
1403 obj = buf.Pointer;
1404
1405 if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count < 2)
1406 goto out;
1407
1408 /*
1409 * As noted in ACPI 3.0 (section 7.2.10), the _PRW object is
1410 * a package in which the first element is either an integer
1411 * or again a package. In the latter case the package inside
1412 * the package element has two elements, a reference handle
1413 * and the GPE number.
1414 */
1415 elm = &obj->Package.Elements[0];
1416
1417 switch (elm->Type) {
1418
1419 case ACPI_TYPE_INTEGER:
1420 val = elm->Integer.Value;
1421 break;
1422
1423 case ACPI_TYPE_PACKAGE:
1424
1425 if (elm->Package.Count < 2)
1426 goto out;
1427
1428 if (elm->Package.Elements[0].Type != ACPI_TYPE_LOCAL_REFERENCE)
1429 goto out;
1430
1431 if (elm->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
1432 goto out;
1433
1434 val = elm->Package.Elements[1].Integer.Value;
1435 break;
1436
1437 default:
1438 goto out;
1439 }
1440
1441 if (enable) {
1442 (void)AcpiSetGpeType(NULL, val, ACPI_GPE_TYPE_WAKE_RUN);
1443 (void)AcpiEnableGpe(NULL, val, ACPI_NOT_ISR);
1444 } else
1445 (void)AcpiDisableGpe(NULL, val, ACPI_NOT_ISR);
1446
1447 out:
1448 ACPI_FREE(buf.Pointer);
1449 }
1450
1451 /*
1452 * acpi_clear_wake_gpe
1453 *
1454 * Clear GPE as both Runtime and Wake.
1455 */
1456 void
1457 acpi_clear_wake_gpe(ACPI_HANDLE handle)
1458 {
1459 acpi_wake_gpe_helper(handle, false);
1460 }
1461
1462 /*
1463 * acpi_set_wake_gpe
1464 *
1465 * Set GPE as both Runtime and Wake.
1466 */
1467 void
1468 acpi_set_wake_gpe(ACPI_HANDLE handle)
1469 {
1470 acpi_wake_gpe_helper(handle, true);
1471 }
1472
1473
1474 /*****************************************************************************
1475 * ACPI sleep support.
1476 *****************************************************************************/
1477
1478 static int
1479 is_available_state(struct acpi_softc *sc, int state)
1480 {
1481 UINT8 type_a, type_b;
1482
1483 return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
1484 &type_a, &type_b));
1485 }
1486
1487 /*
1488 * acpi_enter_sleep_state:
1489 *
1490 * Enter to the specified sleep state.
1491 */
1492
1493 ACPI_STATUS
1494 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1495 {
1496 int err;
1497 ACPI_STATUS ret = AE_OK;
1498
1499 if (state == acpi_sleepstate)
1500 return AE_OK;
1501
1502 aprint_normal_dev(sc->sc_dev, "entering state %d\n", state);
1503
1504 switch (state) {
1505 case ACPI_STATE_S0:
1506 break;
1507 case ACPI_STATE_S1:
1508 case ACPI_STATE_S2:
1509 case ACPI_STATE_S3:
1510 case ACPI_STATE_S4:
1511 if (!is_available_state(sc, state)) {
1512 aprint_error_dev(sc->sc_dev,
1513 "ACPI S%d not available on this platform\n", state);
1514 break;
1515 }
1516
1517 acpi_wakedev_commit(sc);
1518
1519 if (state != ACPI_STATE_S1 && !pmf_system_suspend(PMF_Q_NONE)) {
1520 aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1521 break;
1522 }
1523
1524 ret = AcpiEnterSleepStatePrep(state);
1525 if (ACPI_FAILURE(ret)) {
1526 aprint_error_dev(sc->sc_dev,
1527 "failed preparing to sleep (%s)\n",
1528 AcpiFormatException(ret));
1529 break;
1530 }
1531
1532 acpi_sleepstate = state;
1533 if (state == ACPI_STATE_S1) {
1534 /* just enter the state */
1535 acpi_md_OsDisableInterrupt();
1536 ret = AcpiEnterSleepState((UINT8)state);
1537 if (ACPI_FAILURE(ret))
1538 aprint_error_dev(sc->sc_dev,
1539 "failed to enter sleep state S1: %s\n",
1540 AcpiFormatException(ret));
1541 AcpiLeaveSleepState((UINT8)state);
1542 } else {
1543 err = acpi_md_sleep(state);
1544 if (state == ACPI_STATE_S4)
1545 AcpiEnable();
1546 pmf_system_bus_resume(PMF_Q_NONE);
1547 AcpiLeaveSleepState((UINT8)state);
1548 pmf_system_resume(PMF_Q_NONE);
1549 }
1550
1551 break;
1552 case ACPI_STATE_S5:
1553 ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1554 if (ACPI_FAILURE(ret)) {
1555 aprint_error_dev(sc->sc_dev,
1556 "failed preparing to sleep (%s)\n",
1557 AcpiFormatException(ret));
1558 break;
1559 }
1560 DELAY(1000000);
1561 acpi_sleepstate = state;
1562 acpi_md_OsDisableInterrupt();
1563 AcpiEnterSleepState(ACPI_STATE_S5);
1564 aprint_error_dev(sc->sc_dev, "WARNING powerdown failed!\n");
1565 break;
1566 }
1567
1568 acpi_sleepstate = ACPI_STATE_S0;
1569 return ret;
1570 }
1571
1572 #if defined(ACPI_ACTIVATE_DEV)
1573 /* XXX This very incomplete */
1574 ACPI_STATUS
1575 acpi_allocate_resources(ACPI_HANDLE handle)
1576 {
1577 ACPI_BUFFER bufp, bufc, bufn;
1578 ACPI_RESOURCE *resp, *resc, *resn;
1579 ACPI_RESOURCE_IRQ *irq;
1580 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1581 ACPI_STATUS rv;
1582 uint delta;
1583
1584 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1585 if (ACPI_FAILURE(rv))
1586 goto out;
1587 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1588 if (ACPI_FAILURE(rv)) {
1589 goto out1;
1590 }
1591
1592 bufn.Length = 1000;
1593 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1594 resp = bufp.Pointer;
1595 resc = bufc.Pointer;
1596 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
1597 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1598 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
1599 resp = ACPI_NEXT_RESOURCE(resp);
1600 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
1601 break;
1602 /* Found identical Id */
1603 resn->Type = resc->Type;
1604 switch (resc->Type) {
1605 case ACPI_RESOURCE_TYPE_IRQ:
1606 memcpy(&resn->Data, &resp->Data,
1607 sizeof(ACPI_RESOURCE_IRQ));
1608 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1609 irq->Interrupts[0] =
1610 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1611 Interrupts[irq->InterruptCount-1];
1612 irq->InterruptCount = 1;
1613 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
1614 break;
1615 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1616 memcpy(&resn->Data, &resp->Data,
1617 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
1618 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
1619 #if 0
1620 /*
1621 * XXX not duplicating the interrupt logic above
1622 * because its not clear what it accomplishes.
1623 */
1624 xirq->Interrupts[0] =
1625 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
1626 Interrupts[irq->NumberOfInterrupts-1];
1627 xirq->NumberOfInterrupts = 1;
1628 #endif
1629 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
1630 break;
1631 case ACPI_RESOURCE_TYPE_IO:
1632 memcpy(&resn->Data, &resp->Data,
1633 sizeof(ACPI_RESOURCE_IO));
1634 resn->Length = resp->Length;
1635 break;
1636 default:
1637 printf("acpi_allocate_resources: res=%d\n", resc->Type);
1638 rv = AE_BAD_DATA;
1639 goto out2;
1640 }
1641 resc = ACPI_NEXT_RESOURCE(resc);
1642 resn = ACPI_NEXT_RESOURCE(resn);
1643 resp = ACPI_NEXT_RESOURCE(resp);
1644 delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1645 if (delta >=
1646 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
1647 bufn.Length *= 2;
1648 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1649 M_ACPI, M_WAITOK);
1650 resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1651 }
1652 }
1653 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1654 printf("acpi_allocate_resources: resc not exhausted\n");
1655 rv = AE_BAD_DATA;
1656 goto out3;
1657 }
1658
1659 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
1660 rv = AcpiSetCurrentResources(handle, &bufn);
1661 if (ACPI_FAILURE(rv)) {
1662 printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1663 AcpiFormatException(rv));
1664 }
1665
1666 out3:
1667 free(bufn.Pointer, M_ACPI);
1668 out2:
1669 ACPI_FREE(bufc.Pointer);
1670 out1:
1671 ACPI_FREE(bufp.Pointer);
1672 out:
1673 return rv;
1674 }
1675 #endif /* ACPI_ACTIVATE_DEV */
1676
1677 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1678 {
1679 const struct sysctlnode *node;
1680 const struct sysctlnode *ssnode;
1681
1682 if (sysctl_createv(clog, 0, NULL, NULL,
1683 CTLFLAG_PERMANENT,
1684 CTLTYPE_NODE, "hw", NULL,
1685 NULL, 0, NULL, 0,
1686 CTL_HW, CTL_EOL) != 0)
1687 return;
1688
1689 if (sysctl_createv(clog, 0, NULL, &node,
1690 CTLFLAG_PERMANENT,
1691 CTLTYPE_NODE, "acpi", NULL,
1692 NULL, 0, NULL, 0,
1693 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
1694 return;
1695
1696 sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
1697 CTLTYPE_QUAD, "root",
1698 SYSCTL_DESCR("ACPI root pointer"),
1699 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1700 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1701 sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
1702 CTLTYPE_STRING, "supported_states",
1703 SYSCTL_DESCR("Supported ACPI system states"),
1704 NULL, 0, acpi_supported_states, 0,
1705 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1706
1707 /* ACPI sleepstate sysctl */
1708 if (sysctl_createv(NULL, 0, NULL, &node,
1709 CTLFLAG_PERMANENT,
1710 CTLTYPE_NODE, "machdep", NULL,
1711 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
1712 return;
1713 if (sysctl_createv(NULL, 0, &node, &ssnode,
1714 CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
1715 NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
1716 CTL_EOL) != 0)
1717 return;
1718 }
1719
1720 static int
1721 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1722 {
1723 int error, t;
1724 struct sysctlnode node;
1725
1726 node = *rnode;
1727 t = acpi_sleepstate;
1728 node.sysctl_data = &t;
1729 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1730 if (error || newp == NULL)
1731 return error;
1732
1733 if (acpi_softc == NULL)
1734 return ENOSYS;
1735
1736 acpi_enter_sleep_state(acpi_softc, t);
1737
1738 return 0;
1739 }
1740
1741 static ACPI_TABLE_HEADER *
1742 acpi_map_rsdt(void)
1743 {
1744 ACPI_PHYSICAL_ADDRESS paddr;
1745 ACPI_TABLE_RSDP *rsdp;
1746
1747 paddr = AcpiOsGetRootPointer();
1748 if (paddr == 0) {
1749 printf("ACPI: couldn't get root pointer\n");
1750 return NULL;
1751 }
1752 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1753 if (rsdp == NULL) {
1754 printf("ACPI: couldn't map RSDP\n");
1755 return NULL;
1756 }
1757 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1758 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
1759 else
1760 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
1761 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1762
1763 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1764 }
1765
1766 static void
1767 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1768 {
1769 if (rsdt == NULL)
1770 return;
1771
1772 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1773 }
1774