Bring in YONETANI Tomokazu's acpi-update-2.patch (27-May-2004), a major
[dragonfly.git] / sys / dev / acpica5 / acpi.c
CommitLineData
5ed44076
MD
1/*-
2 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * Copyright (c) 2000, 2001 Michael Smith
5 * Copyright (c) 2000 BSDi
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
f9d8cd12
MD
29 * $FreeBSD: src/sys/dev/acpica/acpi.c,v 1.145 2004/05/06 08:04:34 njl Exp $
30 * $DragonFly: src/sys/dev/acpica5/acpi.c,v 1.5 2004/06/27 08:52:39 dillon Exp $
5ed44076
MD
31 */
32
33#include "opt_acpi.h"
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/proc.h>
37#include <sys/fcntl.h>
38#include <sys/malloc.h>
39#include <sys/bus.h>
40#include <sys/conf.h>
41#include <sys/ioccom.h>
42#include <sys/reboot.h>
43#include <sys/sysctl.h>
44#include <sys/ctype.h>
45#include <sys/linker.h>
46#include <sys/power.h>
47#include <sys/sbuf.h>
48
49#include <machine/clock.h>
f9d8cd12 50#include <machine/globaldata.h>
5ed44076
MD
51#include <machine/resource.h>
52#include <machine/bus.h>
53#include <sys/rman.h>
54#include <bus/isa/isavar.h>
55
56#include "acpi.h"
57#include <dev/acpica5/acpivar.h>
58#include <dev/acpica5/acpiio.h>
59#include <acnamesp.h>
60
61MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
62
63/* Hooks for the ACPI CA debugging infrastructure */
64#define _COMPONENT ACPI_BUS
65ACPI_MODULE_NAME("ACPI")
66
67static d_open_t acpiopen;
68static d_close_t acpiclose;
69static d_ioctl_t acpiioctl;
70
71#define CDEV_MAJOR 152
72static struct cdevsw acpi_cdevsw = {
73 .d_name = "acpi",
74 .d_maj = CDEV_MAJOR,
75 .d_flags = 0,
76 .d_port = NULL,
455fcd7e 77 .d_clone = NULL,
5ed44076
MD
78 .old_open = acpiopen,
79 .old_close = acpiclose,
80 .old_ioctl = acpiioctl
81};
82
5ed44076
MD
83#if __FreeBSD_version >= 500000
84struct mtx acpi_mutex;
85#endif
86
f9d8cd12
MD
87struct acpi_quirks {
88 char *OemId;
89 uint32_t OemRevision;
90 char *value;
91};
92
93#define ACPI_OEM_REV_ANY 0
94
95static struct acpi_quirks acpi_quirks_table[] = {
96#ifdef notyet
97 /* Bad PCI routing table. Used on some SuperMicro boards. */
98 { "PTLTD ", 0x06040000, "pci_link" },
99#endif
100
101 { NULL, 0, NULL }
102};
103
5ed44076
MD
104static int acpi_modevent(struct module *mod, int event, void *junk);
105static void acpi_identify(driver_t *driver, device_t parent);
106static int acpi_probe(device_t dev);
107static int acpi_attach(device_t dev);
f9d8cd12 108static void acpi_quirks_set(void);
5ed44076
MD
109static device_t acpi_add_child(device_t bus, int order, const char *name,
110 int unit);
111static int acpi_print_child(device_t bus, device_t child);
112static int acpi_read_ivar(device_t dev, device_t child, int index,
113 uintptr_t *result);
114static int acpi_write_ivar(device_t dev, device_t child, int index,
115 uintptr_t value);
116static int acpi_set_resource(device_t dev, device_t child, int type,
117 int rid, u_long start, u_long count);
118static int acpi_get_resource(device_t dev, device_t child, int type,
119 int rid, u_long *startp, u_long *countp);
120static struct resource *acpi_alloc_resource(device_t bus, device_t child,
121 int type, int *rid, u_long start, u_long end,
122 u_long count, u_int flags);
123static int acpi_release_resource(device_t bus, device_t child, int type,
124 int rid, struct resource *r);
125static uint32_t acpi_isa_get_logicalid(device_t dev);
126static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
127static int acpi_isa_pnp_probe(device_t bus, device_t child,
128 struct isa_pnp_id *ids);
129static void acpi_probe_children(device_t bus);
130static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
131 void *context, void **status);
132static void acpi_shutdown_pre_sync(void *arg, int howto);
133static void acpi_shutdown_final(void *arg, int howto);
f9d8cd12 134static void acpi_shutdown_poweroff(void *arg);
5ed44076
MD
135static void acpi_enable_fixed_events(struct acpi_softc *sc);
136static void acpi_system_eventhandler_sleep(void *arg, int state);
137static void acpi_system_eventhandler_wakeup(void *arg, int state);
138static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
139static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
140static int acpi_pm_func(u_long cmd, void *arg, ...);
f9d8cd12
MD
141static int acpi_child_location_str_method(device_t acdev, device_t child,
142 char *buf, size_t buflen);
143static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
144 char *buf, size_t buflen);
5ed44076
MD
145
146static device_method_t acpi_methods[] = {
147 /* Device interface */
148 DEVMETHOD(device_identify, acpi_identify),
149 DEVMETHOD(device_probe, acpi_probe),
150 DEVMETHOD(device_attach, acpi_attach),
151 DEVMETHOD(device_detach, bus_generic_detach),
152 DEVMETHOD(device_shutdown, bus_generic_shutdown),
153 DEVMETHOD(device_suspend, bus_generic_suspend),
154 DEVMETHOD(device_resume, bus_generic_resume),
155
156 /* Bus interface */
157 DEVMETHOD(bus_add_child, acpi_add_child),
158 DEVMETHOD(bus_print_child, acpi_print_child),
159 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
160 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
161 DEVMETHOD(bus_set_resource, acpi_set_resource),
162 DEVMETHOD(bus_get_resource, acpi_get_resource),
163 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
164 DEVMETHOD(bus_release_resource, acpi_release_resource),
f9d8cd12
MD
165 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
166 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
5ed44076
MD
167 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
168 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
169 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
170 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
171 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
172
173 /* ISA emulation */
174 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
175
176 {0, 0}
177};
178
179static driver_t acpi_driver = {
180 "acpi",
181 acpi_methods,
182 sizeof(struct acpi_softc),
183};
184
185static devclass_t acpi_devclass;
186DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
f9d8cd12
MD
187MODULE_VERSION(acpi, 1);
188
189static const char* sleep_state_names[] = {
190 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
5ed44076
MD
191
192SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
193static char acpi_ca_version[12];
194SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
195 acpi_ca_version, 0, "Version of Intel ACPI-CA");
196
f9d8cd12
MD
197/*
198 * Allow override of whether methods execute in parallel or not.
199 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
200 * errors for AML that really can't handle parallel method execution.
201 * It is off by default since this breaks recursive methods and
202 * some IBMs use such code.
203 */
204static int acpi_serialize_methods;
205TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
206
207/*
208 * Allow override of whether to support the _OSI method. This allows us
209 * to claim compatibility with various MS OSs without changing the value
210 * we report for _OS. This is enabled by default since it fixes some
211 * problems with interrupt routing although it can be disabled if it
212 * causes problems. See the definition of "AcpiGbl_ValidOsiStrings" for
213 * a list of systems we claim.
214 */
215static int acpi_osi_method = TRUE;
216TUNABLE_INT("hw.acpi.osi_method", &acpi_osi_method);
217
5ed44076
MD
218/*
219 * ACPI can only be loaded as a module by the loader; activating it after
220 * system bootstrap time is not useful, and can be fatal to the system.
221 * It also cannot be unloaded, since the entire system bus heirarchy hangs
222 * off it.
223 */
224static int
225acpi_modevent(struct module *mod, int event, void *junk)
226{
227 switch(event) {
228 case MOD_LOAD:
229 if (!cold) {
230 printf("The ACPI driver cannot be loaded after boot.\n");
231 return (EPERM);
232 }
233 break;
234 case MOD_UNLOAD:
235 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
236 return (EBUSY);
237 break;
238 default:
239 break;
240 }
241 return (0);
242}
243
244/*
245 * Perform early initialization.
246 */
247ACPI_STATUS
248acpi_Startup(void)
249{
250#ifdef ACPI_DEBUGGER
251 char *debugpoint;
252#endif
253 static int error, started = 0;
254
255 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
256
257 if (started)
f9d8cd12 258 return_VALUE (error);
5ed44076
MD
259 started = 1;
260
261#if __FreeBSD_version >= 500000
262 /* Initialise the ACPI mutex */
263 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
264#endif
265
f9d8cd12
MD
266#if notyet
267 /*
268 * Set the globals from our tunables. This is needed because ACPI-CA
269 * uses UINT8 for some values and we have no tunable_uint8.
270 */
271 AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
272 AcpiGbl_CreateOsiMethod = acpi_osi_method;
273 AcpiGbl_LeaveWakeGpesDisabled = FALSE;
274#endif /* notyet */
275
5ed44076
MD
276 /* Start up the ACPI CA subsystem. */
277#ifdef ACPI_DEBUGGER
278 debugpoint = getenv("debug.acpi.debugger");
279 if (debugpoint) {
280 if (!strcmp(debugpoint, "init"))
281 acpi_EnterDebugger();
282 freeenv(debugpoint);
283 }
284#endif
285 if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
286 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
f9d8cd12 287 return_VALUE (error);
5ed44076
MD
288 }
289#ifdef ACPI_DEBUGGER
290 debugpoint = getenv("debug.acpi.debugger");
291 if (debugpoint) {
292 if (!strcmp(debugpoint, "tables"))
293 acpi_EnterDebugger();
294 freeenv(debugpoint);
295 }
296#endif
297
298 if (ACPI_FAILURE(error = AcpiLoadTables())) {
299 printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
300 return_VALUE(error);
301 }
f9d8cd12
MD
302
303 /* Set up any quirks we have for this XSDT. */
304 acpi_quirks_set();
305 if (acpi_disabled("acpi"))
306 return_VALUE (AE_ERROR);
307
308 return_VALUE (AE_OK);
5ed44076
MD
309}
310
311/*
312 * Detect ACPI, perform early initialisation
313 */
314static void
315acpi_identify(driver_t *driver, device_t parent)
316{
317 device_t child;
318
319 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
320
321 if (!cold)
322 return_VOID;
323
324 /* Check that we haven't been disabled with a hint. */
325 if (resource_disabled("acpi", 0))
326 return_VOID;
327
5ed44076
MD
328 /* Make sure we're not being doubly invoked. */
329 if (device_find_child(parent, "acpi", 0) != NULL)
330 return_VOID;
331
332 /* Initialize ACPI-CA. */
333 if (ACPI_FAILURE(acpi_Startup()))
334 return_VOID;
335
f9d8cd12
MD
336 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
337
5ed44076
MD
338 /* Attach the actual ACPI device. */
339 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
340 device_printf(parent, "ACPI: could not attach\n");
341 return_VOID;
342 }
343}
344
345/*
346 * Fetch some descriptive data from ACPI to put in our attach message
347 */
348static int
349acpi_probe(device_t dev)
350{
351 ACPI_TABLE_HEADER th;
352 char buf[20];
353 int error;
354 struct sbuf sb;
355 ACPI_STATUS status;
356 ACPI_LOCK_DECL;
357
358 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
359
360 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
361 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
362
363 device_printf(dev, "Other PM system enabled.\n");
364 return_VALUE(ENXIO);
365 }
366
367 ACPI_LOCK;
368
369 if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
370 device_printf(dev, "couldn't get XSDT header: %s\n",
371 AcpiFormatException(status));
372 error = ENXIO;
373 } else {
374 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
375 sbuf_bcat(&sb, th.OemId, 6);
376 sbuf_trim(&sb);
377 sbuf_putc(&sb, ' ');
378 sbuf_bcat(&sb, th.OemTableId, 8);
379 sbuf_trim(&sb);
380 sbuf_finish(&sb);
381 device_set_desc_copy(dev, sbuf_data(&sb));
382 sbuf_delete(&sb);
383 error = 0;
384 }
385 ACPI_UNLOCK;
386 return_VALUE(error);
387}
388
389static int
390acpi_attach(device_t dev)
391{
392 struct acpi_softc *sc;
393 ACPI_STATUS status;
f9d8cd12 394 int error, state;
5ed44076 395 UINT32 flags;
f9d8cd12 396 UINT8 TypeA, TypeB;
5ed44076
MD
397 char *env;
398#ifdef ACPI_DEBUGGER
399 char *debugpoint;
400#endif
401 ACPI_LOCK_DECL;
402
403 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
404 ACPI_LOCK;
405 sc = device_get_softc(dev);
406 bzero(sc, sizeof(*sc));
407 sc->acpi_dev = dev;
408
409#ifdef ACPI_DEBUGGER
410 debugpoint = getenv("debug.acpi.debugger");
411 if (debugpoint) {
412 if (!strcmp(debugpoint, "spaces"))
413 acpi_EnterDebugger();
414 freeenv(debugpoint);
415 }
416#endif
417
418 /* Install the default address space handlers. */
419 error = ENXIO;
420 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
421 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
422 if (ACPI_FAILURE(status)) {
423 device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
424 AcpiFormatException(status));
425 goto out;
426 }
427 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
428 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
429 if (ACPI_FAILURE(status)) {
430 device_printf(dev, "Could not initialise SystemIO handler: %s\n",
431 AcpiFormatException(status));
432 goto out;
433 }
434 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
435 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
436 if (ACPI_FAILURE(status)) {
437 device_printf(dev, "could not initialise PciConfig handler: %s\n",
438 AcpiFormatException(status));
439 goto out;
440 }
441
442 /*
443 * Bring ACPI fully online.
444 *
445 * Note that some systems (specifically, those with namespace evaluation
446 * issues that require the avoidance of parts of the namespace) must
447 * avoid running _INI and _STA on everything, as well as dodging the final
448 * object init pass.
449 *
450 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
451 *
452 * XXX We should arrange for the object init pass after we have attached
453 * all our child devices, but on many systems it works here.
454 */
455#ifdef ACPI_DEBUGGER
456 debugpoint = getenv("debug.acpi.debugger");
457 if (debugpoint) {
458 if (!strcmp(debugpoint, "enable"))
459 acpi_EnterDebugger();
460 freeenv(debugpoint);
461 }
462#endif
463 flags = 0;
464 if (testenv("debug.acpi.avoid"))
465 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
466 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
467 device_printf(dev, "Could not enable ACPI: %s\n",
468 AcpiFormatException(status));
469 goto out;
470 }
471
472 /*
473 * Call the ECDT probe function to provide EC functionality before
474 * the namespace has been evaluated.
475 */
476 acpi_ec_ecdt_probe(dev);
477
478 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
479 device_printf(dev, "Could not initialize ACPI objects: %s\n",
480 AcpiFormatException(status));
481 goto out;
482 }
483
484 /*
485 * Setup our sysctl tree.
486 *
487 * XXX: This doesn't check to make sure that none of these fail.
488 */
489 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
490 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
491 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
492 device_get_name(dev), CTLFLAG_RD, 0, "");
493 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
494 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
495 0, 0, acpi_supported_sleep_state_sysctl, "A", "");
496 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
497 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
498 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
499 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
500 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
501 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
502 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
503 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
504 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
505 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
506 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
507 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
508 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
509 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
510 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
511 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
512 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
513 &sc->acpi_sleep_delay, 0, "sleep delay");
514 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
515 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
516 &sc->acpi_s4bios, 0, "S4BIOS mode");
517 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
518 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
519 &sc->acpi_verbose, 0, "verbose mode");
520 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
521 OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
522 &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
523
524 /*
525 * Default to 5 seconds before sleeping to give some machines time to
526 * stabilize.
527 */
528 sc->acpi_sleep_delay = 5;
529 sc->acpi_disable_on_poweroff = 1;
530 if (bootverbose)
531 sc->acpi_verbose = 1;
532 if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
533 sc->acpi_verbose = 1;
534 freeenv(env);
535 }
536
537 /* Only enable S4BIOS by default if the FACS says it is available. */
538 if (AcpiGbl_FACS->S4Bios_f != 0)
539 sc->acpi_s4bios = 1;
540
541 /*
f9d8cd12
MD
542 * Dispatch the default sleep state to devices. The lid switch is set
543 * to NONE by default to avoid surprising users.
5ed44076 544 */
f9d8cd12
MD
545 sc->acpi_power_button_sx = ACPI_STATE_S5;
546 sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
5ed44076
MD
547 sc->acpi_standby_sx = ACPI_STATE_S1;
548 sc->acpi_suspend_sx = ACPI_STATE_S3;
549
f9d8cd12
MD
550 /* Pick the first valid sleep state for the sleep button default. */
551 sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
552 for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
553 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
554 sc->acpi_sleep_button_sx = state;
555 break;
556 }
557
5ed44076
MD
558 acpi_enable_fixed_events(sc);
559
560 /*
561 * Scan the namespace and attach/initialise children.
562 */
563#ifdef ACPI_DEBUGGER
564 debugpoint = getenv("debug.acpi.debugger");
565 if (debugpoint) {
566 if (!strcmp(debugpoint, "probe"))
567 acpi_EnterDebugger();
568 freeenv(debugpoint);
569 }
570#endif
571
572 /* Register our shutdown handlers */
573 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
574 SHUTDOWN_PRI_LAST);
575 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
576 SHUTDOWN_PRI_LAST);
577
578 /*
579 * Register our acpi event handlers.
580 * XXX should be configurable eg. via userland policy manager.
581 */
582 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
583 sc, ACPI_EVENT_PRI_LAST);
584 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
585 sc, ACPI_EVENT_PRI_LAST);
586
587 /* Flag our initial states. */
588 sc->acpi_enabled = 1;
589 sc->acpi_sstate = ACPI_STATE_S0;
590 sc->acpi_sleep_disabled = 0;
591
592 /* Create the control device */
e4c9c0c8 593 cdevsw_add(&acpi_cdevsw, 0, 0);
5ed44076
MD
594 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
595 "acpi");
596 sc->acpi_dev_t->si_drv1 = sc;
597
598#ifdef ACPI_DEBUGGER
599 debugpoint = getenv("debug.acpi.debugger");
600 if (debugpoint) {
601 if (strcmp(debugpoint, "running") == 0)
602 acpi_EnterDebugger();
603 freeenv(debugpoint);
604 }
605#endif
606
607#ifdef ACPI_USE_THREADS
608 if ((error = acpi_task_thread_init()))
609 goto out;
610#endif
611
612 if ((error = acpi_machdep_init(dev)))
613 goto out;
614
615 /* Register ACPI again to pass the correct argument of pm_func. */
616 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
617
618 if (!acpi_disabled("bus"))
619 acpi_probe_children(dev);
620
621 error = 0;
622
623 out:
624 ACPI_UNLOCK;
625 return_VALUE (error);
626}
627
f9d8cd12
MD
628static void
629acpi_quirks_set()
630{
631 XSDT_DESCRIPTOR *xsdt;
632 struct acpi_quirks *quirk;
633 char *env, *tmp;
634 int len;
635
636 /*
637 * If the user loaded a custom table or disabled "quirks", leave
638 * the settings alone.
639 */
640 len = 0;
641 if ((env = getenv("acpi_dsdt_load")) != NULL) {
642 /* XXX No strcasecmp but this is good enough. */
643 if (*env == 'Y' || *env == 'y')
644 goto out;
645 freeenv(env);
646 }
647 if ((env = getenv("debug.acpi.disabled")) != NULL) {
648 if (strstr("quirks", env) != NULL)
649 goto out;
650 len = strlen(env);
651 }
652
653 /*
654 * Search through our quirk table and concatenate the disabled
655 * values with whatever we find.
656 */
657 xsdt = AcpiGbl_XSDT;
658 for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
659 if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
660 (xsdt->OemRevision == quirk->OemRevision ||
661 quirk->OemRevision == ACPI_OEM_REV_ANY)) {
662 len += strlen(quirk->value) + 2;
663 if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
664 goto out;
665 sprintf(tmp, "%s %s", env ? env : "", quirk->value);
666#ifdef notyet
667 setenv("debug.acpi.disabled", tmp);
668#endif /* notyet */
669 free(tmp, M_TEMP);
670 break;
671 }
672 }
673
674out:
675 if (env)
676 freeenv(env);
677}
678
5ed44076
MD
679/*
680 * Handle a new device being added
681 */
682static device_t
683acpi_add_child(device_t bus, int order, const char *name, int unit)
684{
685 struct acpi_device *ad;
686 device_t child;
687
8c1d15dd 688 ad = malloc(sizeof(*ad), M_ACPIDEV, M_INTWAIT | M_ZERO);
5ed44076
MD
689
690 resource_list_init(&ad->ad_rl);
691
692 child = device_add_child_ordered(bus, order, name, unit);
693 if (child != NULL)
694 device_set_ivars(child, ad);
695 return (child);
696}
697
698static int
699acpi_print_child(device_t bus, device_t child)
700{
701 struct acpi_device *adev = device_get_ivars(child);
702 struct resource_list *rl = &adev->ad_rl;
703 int retval = 0;
704
705 retval += bus_print_child_header(bus, child);
706 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
707 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
708 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
709 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
710 retval += bus_print_child_footer(bus, child);
711
712 return (retval);
713}
714
f9d8cd12
MD
715/* Location hint for devctl(8) */
716static int
717acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
718 size_t buflen)
719{
720 struct acpi_device *dinfo = device_get_ivars(child);
721
722 if (dinfo->ad_handle)
723 snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
724 else
725 snprintf(buf, buflen, "magic=unknown");
726 return (0);
727}
728
729/* PnP information for devctl(8) */
730static int
731acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
732 size_t buflen)
733{
734 ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
735 ACPI_DEVICE_INFO *adinfo;
736 struct acpi_device *dinfo = device_get_ivars(child);
737 char *end;
738 int error;
739
740 error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
741 adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
742
743 if (error)
744 snprintf(buf, buflen, "Unknown");
745 else
746 snprintf(buf, buflen, "_HID=%s _UID=%lu",
747 (adinfo->Valid & ACPI_VALID_HID) ?
748 adinfo->HardwareId.Value : "UNKNOWN",
749 (adinfo->Valid & ACPI_VALID_UID) ?
750 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
751
752 if (adinfo)
753 AcpiOsFree(adinfo);
754
755 return (0);
756}
5ed44076
MD
757
758/*
759 * Handle per-device ivars
760 */
761static int
762acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
763{
764 struct acpi_device *ad;
765
766 if ((ad = device_get_ivars(child)) == NULL) {
767 printf("device has no ivars\n");
768 return (ENOENT);
769 }
770
771 /* ACPI and ISA compatibility ivars */
772 switch(index) {
773 case ACPI_IVAR_HANDLE:
774 *(ACPI_HANDLE *)result = ad->ad_handle;
775 break;
776 case ACPI_IVAR_MAGIC:
777 *(int *)result = ad->ad_magic;
778 break;
779 case ACPI_IVAR_PRIVATE:
780 *(void **)result = ad->ad_private;
781 break;
782 case ISA_IVAR_VENDORID:
783 case ISA_IVAR_SERIAL:
784 case ISA_IVAR_COMPATID:
785 *(int *)result = -1;
786 break;
787 case ISA_IVAR_LOGICALID:
788 *(int *)result = acpi_isa_get_logicalid(child);
789 break;
790 default:
791 return (ENOENT);
792 }
793
794 return (0);
795}
796
797static int
798acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
799{
800 struct acpi_device *ad;
801
802 if ((ad = device_get_ivars(child)) == NULL) {
803 printf("device has no ivars\n");
804 return (ENOENT);
805 }
806
807 switch(index) {
808 case ACPI_IVAR_HANDLE:
809 ad->ad_handle = (ACPI_HANDLE)value;
810 break;
811 case ACPI_IVAR_MAGIC:
812 ad->ad_magic = (int)value;
813 break;
814 case ACPI_IVAR_PRIVATE:
815 ad->ad_private = (void *)value;
816 break;
817 default:
818 panic("bad ivar write request (%d)", index);
819 return (ENOENT);
820 }
821
822 return (0);
823}
824
5ed44076
MD
825/*
826 * Handle child resource allocation/removal
827 */
828static int
829acpi_set_resource(device_t dev, device_t child, int type, int rid,
830 u_long start, u_long count)
831{
832 struct acpi_device *ad = device_get_ivars(child);
833 struct resource_list *rl = &ad->ad_rl;
834
835 resource_list_add(rl, type, rid, start, start + count -1, count);
836
837 return(0);
838}
839
840static int
841acpi_get_resource(device_t dev, device_t child, int type, int rid,
842 u_long *startp, u_long *countp)
843{
844 struct acpi_device *ad = device_get_ivars(child);
845 struct resource_list *rl = &ad->ad_rl;
846 struct resource_list_entry *rle;
847
848 rle = resource_list_find(rl, type, rid);
849 if (!rle)
850 return(ENOENT);
851
852 if (startp)
853 *startp = rle->start;
854 if (countp)
855 *countp = rle->count;
856
857 return (0);
858}
859
860static struct resource *
861acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
862 u_long start, u_long end, u_long count, u_int flags)
863{
864 struct acpi_device *ad = device_get_ivars(child);
865 struct resource_list *rl = &ad->ad_rl;
866
867 return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
868 flags));
869}
870
871static int
872acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
873{
874 struct acpi_device *ad = device_get_ivars(child);
875 struct resource_list *rl = &ad->ad_rl;
876
877 return (resource_list_release(rl, bus, child, type, rid, r));
878}
879
880/* Allocate an IO port or memory resource, given its GAS. */
881struct resource *
882acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
883{
884 int type;
885
886 if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
887 gas->RegisterBitWidth < 8)
888 return (NULL);
889
890 switch (gas->AddressSpaceId) {
891 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
892 type = SYS_RES_MEMORY;
893 break;
894 case ACPI_ADR_SPACE_SYSTEM_IO:
895 type = SYS_RES_IOPORT;
896 break;
897 default:
898 return (NULL);
899 }
900
901 bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
f9d8cd12 902 return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
5ed44076
MD
903}
904
905/*
906 * Handle ISA-like devices probing for a PnP ID to match.
907 */
908#define PNP_EISAID(s) \
909 ((((s[0] - '@') & 0x1f) << 2) \
910 | (((s[1] - '@') & 0x18) >> 3) \
911 | (((s[1] - '@') & 0x07) << 13) \
912 | (((s[2] - '@') & 0x1f) << 8) \
913 | (PNP_HEXTONUM(s[4]) << 16) \
914 | (PNP_HEXTONUM(s[3]) << 20) \
915 | (PNP_HEXTONUM(s[6]) << 24) \
916 | (PNP_HEXTONUM(s[5]) << 28))
917
918static uint32_t
919acpi_isa_get_logicalid(device_t dev)
920{
921 ACPI_DEVICE_INFO *devinfo;
922 ACPI_BUFFER buf;
923 ACPI_HANDLE h;
924 ACPI_STATUS error;
925 u_int32_t pnpid;
926 ACPI_LOCK_DECL;
927
928 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
929
930 pnpid = 0;
931 buf.Pointer = NULL;
932 buf.Length = ACPI_ALLOCATE_BUFFER;
933
934 ACPI_LOCK;
935
936 /* Fetch and validate the HID. */
937 if ((h = acpi_get_handle(dev)) == NULL)
938 goto out;
939 error = AcpiGetObjectInfo(h, &buf);
940 if (ACPI_FAILURE(error))
941 goto out;
942 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
943
944 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
945 pnpid = PNP_EISAID(devinfo->HardwareId.Value);
946
947out:
948 if (buf.Pointer != NULL)
949 AcpiOsFree(buf.Pointer);
950 ACPI_UNLOCK;
951 return_VALUE (pnpid);
952}
953
954static int
955acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
956{
957 ACPI_DEVICE_INFO *devinfo;
958 ACPI_BUFFER buf;
959 ACPI_HANDLE h;
960 ACPI_STATUS error;
961 uint32_t *pnpid;
962 int valid, i;
963 ACPI_LOCK_DECL;
964
965 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
966
967 pnpid = cids;
968 valid = 0;
969 buf.Pointer = NULL;
970 buf.Length = ACPI_ALLOCATE_BUFFER;
971
972 ACPI_LOCK;
973
974 /* Fetch and validate the CID */
975 if ((h = acpi_get_handle(dev)) == NULL)
976 goto out;
977 error = AcpiGetObjectInfo(h, &buf);
978 if (ACPI_FAILURE(error))
979 goto out;
980 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
981 if ((devinfo->Valid & ACPI_VALID_CID) == 0)
982 goto out;
983
984 if (devinfo->CompatibilityId.Count < count)
985 count = devinfo->CompatibilityId.Count;
986 for (i = 0; i < count; i++) {
987 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
988 continue;
989 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
990 valid++;
991 }
992
993out:
994 if (buf.Pointer != NULL)
995 AcpiOsFree(buf.Pointer);
996 ACPI_UNLOCK;
997 return_VALUE (valid);
998}
999
1000static int
1001acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1002{
1003 int result, cid_count, i;
1004 uint32_t lid, cids[8];
1005
1006 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1007
1008 /*
1009 * ISA-style drivers attached to ACPI may persist and
1010 * probe manually if we return ENOENT. We never want
1011 * that to happen, so don't ever return it.
1012 */
1013 result = ENXIO;
1014
1015 /* Scan the supplied IDs for a match */
1016 lid = acpi_isa_get_logicalid(child);
1017 cid_count = acpi_isa_get_compatid(child, cids, 8);
1018 while (ids && ids->ip_id) {
1019 if (lid == ids->ip_id) {
1020 result = 0;
1021 goto out;
1022 }
1023 for (i = 0; i < cid_count; i++) {
1024 if (cids[i] == ids->ip_id) {
1025 result = 0;
1026 goto out;
1027 }
1028 }
1029 ids++;
1030 }
1031
1032 out:
1033 return_VALUE (result);
1034}
1035
1036/*
1037 * Scan relevant portions of the ACPI namespace and attach child devices.
1038 *
1039 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1040 * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
1041 */
1042static void
1043acpi_probe_children(device_t bus)
1044{
1045 ACPI_HANDLE parent;
1046 ACPI_STATUS status;
1047 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
1048 int i;
1049
1050 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1051 ACPI_ASSERTLOCK;
1052
1053 /* Create any static children by calling device identify methods. */
1054 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1055 bus_generic_probe(bus);
1056
1057 /*
1058 * Scan the namespace and insert placeholders for all the devices that
1059 * we find.
1060 *
1061 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1062 * we want to create nodes for all devices, not just those that are
1063 * currently present. (This assumes that we don't want to create/remove
1064 * devices as they appear, which might be smarter.)
1065 */
1066 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1067 for (i = 0; scopes[i] != NULL; i++) {
1068 status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1069 if (ACPI_SUCCESS(status)) {
1070 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1071 bus, NULL);
1072 }
1073 }
1074
1075 /*
1076 * Scan all of the child devices we have created and let them probe/attach.
1077 */
1078 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
1079 bus_generic_attach(bus);
1080
1081 /*
1082 * Some of these children may have attached others as part of their attach
1083 * process (eg. the root PCI bus driver), so rescan.
1084 */
1085 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
1086 bus_generic_attach(bus);
1087
1088 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1089 return_VOID;
1090}
1091
1092/*
1093 * Evaluate a child device and determine whether we might attach a device to
1094 * it.
1095 */
1096static ACPI_STATUS
1097acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1098{
1099 ACPI_OBJECT_TYPE type;
1100 device_t child, bus = (device_t)context;
1101
1102 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1103
1104 /* Skip this device if we think we'll have trouble with it. */
1105 if (acpi_avoid(handle))
1106 return_ACPI_STATUS (AE_OK);
1107
1108 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1109 switch(type) {
1110 case ACPI_TYPE_DEVICE:
1111 case ACPI_TYPE_PROCESSOR:
1112 case ACPI_TYPE_THERMAL:
1113 case ACPI_TYPE_POWER:
1114 if (acpi_disabled("children"))
1115 break;
1116
1117 /*
1118 * Create a placeholder device for this node. Sort the placeholder
1119 * so that the probe/attach passes will run breadth-first.
1120 */
1121 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1122 acpi_name(handle)));
1123 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1124 if (child == NULL)
1125 break;
1126 acpi_set_handle(child, handle);
1127
1128 /*
1129 * Check that the device is present. If it's not present,
1130 * leave it disabled (so that we have a device_t attached to
1131 * the handle, but we don't probe it).
1132 */
1133 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1134 device_disable(child);
1135 break;
1136 }
1137
1138 /*
1139 * Get the device's resource settings and attach them.
1140 * Note that if the device has _PRS but no _CRS, we need
1141 * to decide when it's appropriate to try to configure the
1142 * device. Ignore the return value here; it's OK for the
1143 * device not to have any resources.
1144 */
f9d8cd12 1145 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
5ed44076
MD
1146
1147 /* If we're debugging, probe/attach now rather than later */
1148 ACPI_DEBUG_EXEC(device_probe_and_attach(child));
1149 break;
1150 }
1151 }
1152
1153 return_ACPI_STATUS (AE_OK);
1154}
1155
1156static void
1157acpi_shutdown_pre_sync(void *arg, int howto)
1158{
1159 struct acpi_softc *sc = arg;
1160
1161 ACPI_ASSERTLOCK;
1162
1163 /*
1164 * Disable all ACPI events before soft off, otherwise the system
1165 * will be turned on again on some laptops.
1166 *
1167 * XXX this should probably be restricted to masking some events just
1168 * before powering down, since we may still need ACPI during the
1169 * shutdown process.
1170 */
1171 if (sc->acpi_disable_on_poweroff)
1172 acpi_Disable(sc);
1173}
1174
1175static void
1176acpi_shutdown_final(void *arg, int howto)
1177{
1178 ACPI_STATUS status;
5ed44076
MD
1179 ACPI_ASSERTLOCK;
1180
f9d8cd12
MD
1181 /*
1182 * If powering off, run the actual shutdown code on each processor.
1183 * It will only perform the shutdown on the BSP. Some chipsets do
1184 * not power off the system correctly if called from an AP.
1185 */
5ed44076 1186 if ((howto & RB_POWEROFF) != 0) {
f9d8cd12 1187 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
5ed44076
MD
1188 if (ACPI_FAILURE(status)) {
1189 printf("AcpiEnterSleepStatePrep failed - %s\n",
1190 AcpiFormatException(status));
1191 return;
1192 }
f9d8cd12
MD
1193 printf("Powering system off using ACPI\n");
1194#ifdef notyet
1195 smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL);
1196#else
1197 acpi_shutdown_poweroff(NULL);
1198#endif /* notyet */
5ed44076
MD
1199 } else {
1200 printf("Shutting down ACPI\n");
1201 AcpiTerminate();
1202 }
1203}
1204
f9d8cd12
MD
1205/*
1206 * Since this function may be called with locks held or in an unknown
1207 * context, it cannot allocate memory, acquire locks, sleep, etc.
1208 */
1209static void
1210acpi_shutdown_poweroff(void *arg)
1211{
1212 ACPI_STATUS status;
1213
1214 ACPI_ASSERTLOCK;
1215
1216 /* Only attempt to power off if this is the BSP (cpuid 0). */
1217 if (mdcpu->mi.gd_cpuid != 0)
1218 return;
1219
1220 ACPI_DISABLE_IRQS();
1221 status = AcpiEnterSleepState(ACPI_STATE_S5);
1222 if (ACPI_FAILURE(status)) {
1223 printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
1224 } else {
1225 DELAY(1000000);
1226 printf("ACPI power-off failed - timeout\n");
1227 }
1228}
1229
5ed44076
MD
1230static void
1231acpi_enable_fixed_events(struct acpi_softc *sc)
1232{
1233 static int first_time = 1;
1234
1235 ACPI_ASSERTLOCK;
1236
1237 /* Enable and clear fixed events and install handlers. */
1238 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
1239 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1240 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1241 acpi_event_power_button_sleep, sc);
1242 if (first_time)
1243 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
1244 }
1245 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
1246 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1247 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1248 acpi_event_sleep_button_sleep, sc);
1249 if (first_time)
1250 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
1251 }
1252
1253 first_time = 0;
1254}
1255
1256/*
1257 * Returns true if the device is actually present and should
1258 * be attached to. This requires the present, enabled, UI-visible
1259 * and diagnostics-passed bits to be set.
1260 */
1261BOOLEAN
1262acpi_DeviceIsPresent(device_t dev)
1263{
1264 ACPI_DEVICE_INFO *devinfo;
1265 ACPI_HANDLE h;
1266 ACPI_BUFFER buf;
1267 ACPI_STATUS error;
1268 int ret;
1269
1270 ACPI_ASSERTLOCK;
1271
1272 ret = FALSE;
1273 if ((h = acpi_get_handle(dev)) == NULL)
1274 return (FALSE);
1275 buf.Pointer = NULL;
1276 buf.Length = ACPI_ALLOCATE_BUFFER;
1277 error = AcpiGetObjectInfo(h, &buf);
1278 if (ACPI_FAILURE(error))
1279 return (FALSE);
1280 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1281
1282 /* If no _STA method, must be present */
1283 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1284 ret = TRUE;
1285
1286 /* Return true for 'present' and 'functioning' */
1287 if ((devinfo->CurrentStatus & 0x9) == 0x9)
1288 ret = TRUE;
1289
1290 AcpiOsFree(buf.Pointer);
1291 return (ret);
1292}
1293
1294/*
1295 * Returns true if the battery is actually present and inserted.
1296 */
1297BOOLEAN
1298acpi_BatteryIsPresent(device_t dev)
1299{
1300 ACPI_DEVICE_INFO *devinfo;
1301 ACPI_HANDLE h;
1302 ACPI_BUFFER buf;
1303 ACPI_STATUS error;
1304 int ret;
1305
1306 ACPI_ASSERTLOCK;
1307
1308 ret = FALSE;
1309 if ((h = acpi_get_handle(dev)) == NULL)
1310 return (FALSE);
1311 buf.Pointer = NULL;
1312 buf.Length = ACPI_ALLOCATE_BUFFER;
1313 error = AcpiGetObjectInfo(h, &buf);
1314 if (ACPI_FAILURE(error))
1315 return (FALSE);
1316 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1317
1318 /* If no _STA method, must be present */
1319 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1320 ret = TRUE;
1321
1322 /* Return true for 'present' and 'functioning' */
1323 if ((devinfo->CurrentStatus & 0x19) == 0x19)
1324 ret = TRUE;
1325
1326 AcpiOsFree(buf.Pointer);
1327 return (ret);
1328}
1329
1330/*
1331 * Match a HID string against a device
1332 */
1333BOOLEAN
1334acpi_MatchHid(device_t dev, char *hid)
1335{
1336 ACPI_DEVICE_INFO *devinfo;
1337 ACPI_HANDLE h;
1338 ACPI_BUFFER buf;
1339 ACPI_STATUS error;
1340 int ret, i;
1341
1342 ACPI_ASSERTLOCK;
1343
1344 ret = FALSE;
1345 if (hid == NULL)
1346 return (FALSE);
1347 if ((h = acpi_get_handle(dev)) == NULL)
1348 return (FALSE);
1349 buf.Pointer = NULL;
1350 buf.Length = ACPI_ALLOCATE_BUFFER;
1351 error = AcpiGetObjectInfo(h, &buf);
1352 if (ACPI_FAILURE(error))
1353 return (FALSE);
1354 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1355
1356 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1357 strcmp(hid, devinfo->HardwareId.Value) == 0)
1358 ret = TRUE;
1359 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
1360 for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
1361 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
1362 ret = TRUE;
1363 break;
1364 }
1365 }
1366 }
1367
1368 AcpiOsFree(buf.Pointer);
1369 return (ret);
1370}
1371
1372/*
1373 * Return the handle of a named object within our scope, ie. that of (parent)
1374 * or one if its parents.
1375 */
1376ACPI_STATUS
1377acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1378{
1379 ACPI_HANDLE r;
1380 ACPI_STATUS status;
1381
1382 ACPI_ASSERTLOCK;
1383
1384 /* Walk back up the tree to the root */
1385 for (;;) {
1386 status = AcpiGetHandle(parent, path, &r);
1387 if (ACPI_SUCCESS(status)) {
1388 *result = r;
1389 return (AE_OK);
1390 }
1391 if (status != AE_NOT_FOUND)
1392 return (AE_OK);
1393 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1394 return (AE_NOT_FOUND);
1395 parent = r;
1396 }
1397}
1398
f9d8cd12
MD
1399/* Find the difference between two PM tick counts. */
1400uint32_t
1401acpi_TimerDelta(uint32_t end, uint32_t start)
1402{
1403 uint32_t delta;
1404
1405 if (end >= start)
1406 delta = end - start;
1407 else if (AcpiGbl_FADT->TmrValExt == 0)
1408 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1409 else
1410 delta = ((0xFFFFFFFF - start) + end + 1);
1411 return (delta);
1412}
1413
5ed44076
MD
1414/*
1415 * Allocate a buffer with a preset data size.
1416 */
1417ACPI_BUFFER *
1418acpi_AllocBuffer(int size)
1419{
1420 ACPI_BUFFER *buf;
1421
8c1d15dd 1422 buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_INTWAIT);
5ed44076
MD
1423 buf->Length = size;
1424 buf->Pointer = (void *)(buf + 1);
1425 return (buf);
1426}
1427
f9d8cd12
MD
1428ACPI_STATUS
1429acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1430{
1431 ACPI_OBJECT arg1;
1432 ACPI_OBJECT_LIST args;
1433
1434 ACPI_ASSERTLOCK;
1435
1436 arg1.Type = ACPI_TYPE_INTEGER;
1437 arg1.Integer.Value = number;
1438 args.Count = 1;
1439 args.Pointer = &arg1;
1440
1441 return (AcpiEvaluateObject(handle, path, &args, NULL));
1442}
1443
5ed44076
MD
1444/*
1445 * Evaluate a path that should return an integer.
1446 */
1447ACPI_STATUS
f9d8cd12 1448acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
5ed44076
MD
1449{
1450 ACPI_STATUS status;
1451 ACPI_BUFFER buf;
1452 ACPI_OBJECT param;
1453
1454 ACPI_ASSERTLOCK;
1455
1456 if (handle == NULL)
1457 handle = ACPI_ROOT_OBJECT;
1458
1459 /*
1460 * Assume that what we've been pointed at is an Integer object, or
1461 * a method that will return an Integer.
1462 */
1463 buf.Pointer = &param;
1464 buf.Length = sizeof(param);
1465 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1466 if (ACPI_SUCCESS(status)) {
1467 if (param.Type == ACPI_TYPE_INTEGER)
1468 *number = param.Integer.Value;
1469 else
1470 status = AE_TYPE;
1471 }
1472
1473 /*
1474 * In some applications, a method that's expected to return an Integer
1475 * may instead return a Buffer (probably to simplify some internal
1476 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1477 * convert it into an Integer as best we can.
1478 *
1479 * This is a hack.
1480 */
1481 if (status == AE_BUFFER_OVERFLOW) {
1482 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1483 status = AE_NO_MEMORY;
1484 } else {
1485 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1486 if (ACPI_SUCCESS(status))
1487 status = acpi_ConvertBufferToInteger(&buf, number);
1488 AcpiOsFree(buf.Pointer);
1489 }
1490 }
1491 return (status);
1492}
1493
1494ACPI_STATUS
f9d8cd12 1495acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
5ed44076
MD
1496{
1497 ACPI_OBJECT *p;
f9d8cd12 1498 UINT8 *val;
5ed44076
MD
1499 int i;
1500
1501 p = (ACPI_OBJECT *)bufp->Pointer;
1502 if (p->Type == ACPI_TYPE_INTEGER) {
1503 *number = p->Integer.Value;
1504 return (AE_OK);
1505 }
1506 if (p->Type != ACPI_TYPE_BUFFER)
1507 return (AE_TYPE);
1508 if (p->Buffer.Length > sizeof(int))
1509 return (AE_BAD_DATA);
1510
1511 *number = 0;
f9d8cd12 1512 val = p->Buffer.Pointer;
5ed44076 1513 for (i = 0; i < p->Buffer.Length; i++)
f9d8cd12 1514 *number += val[i] << (i * 8);
5ed44076
MD
1515 return (AE_OK);
1516}
1517
1518/*
1519 * Iterate over the elements of an a package object, calling the supplied
1520 * function for each element.
1521 *
1522 * XXX possible enhancement might be to abort traversal on error.
1523 */
1524ACPI_STATUS
1525acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1526 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1527{
1528 ACPI_OBJECT *comp;
1529 int i;
1530
1531 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1532 return (AE_BAD_PARAMETER);
1533
1534 /* Iterate over components */
1535 i = 0;
1536 comp = pkg->Package.Elements;
1537 for (; i < pkg->Package.Count; i++, comp++)
1538 func(comp, arg);
1539
1540 return (AE_OK);
1541}
1542
1543/*
1544 * Find the (index)th resource object in a set.
1545 */
1546ACPI_STATUS
1547acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1548{
1549 ACPI_RESOURCE *rp;
1550 int i;
1551
1552 rp = (ACPI_RESOURCE *)buf->Pointer;
1553 i = index;
1554 while (i-- > 0) {
1555 /* Range check */
1556 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1557 return (AE_BAD_PARAMETER);
1558
1559 /* Check for terminator */
1560 if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
1561 return (AE_NOT_FOUND);
1562 rp = ACPI_NEXT_RESOURCE(rp);
1563 }
1564 if (resp != NULL)
1565 *resp = rp;
1566
1567 return (AE_OK);
1568}
1569
1570/*
1571 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1572 *
1573 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1574 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1575 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1576 * resources.
1577 */
1578#define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1579
1580ACPI_STATUS
1581acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1582{
1583 ACPI_RESOURCE *rp;
1584 void *newp;
1585
1586 /* Initialise the buffer if necessary. */
1587 if (buf->Pointer == NULL) {
1588 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1589 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1590 return (AE_NO_MEMORY);
1591 rp = (ACPI_RESOURCE *)buf->Pointer;
1592 rp->Id = ACPI_RSTYPE_END_TAG;
1593 rp->Length = 0;
1594 }
1595 if (res == NULL)
1596 return (AE_OK);
1597
1598 /*
1599 * Scan the current buffer looking for the terminator.
1600 * This will either find the terminator or hit the end
1601 * of the buffer and return an error.
1602 */
1603 rp = (ACPI_RESOURCE *)buf->Pointer;
1604 for (;;) {
1605 /* Range check, don't go outside the buffer */
1606 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1607 return (AE_BAD_PARAMETER);
1608 if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
1609 break;
1610 rp = ACPI_NEXT_RESOURCE(rp);
1611 }
1612
1613 /*
1614 * Check the size of the buffer and expand if required.
1615 *
1616 * Required size is:
1617 * size of existing resources before terminator +
1618 * size of new resource and header +
1619 * size of terminator.
1620 *
1621 * Note that this loop should really only run once, unless
1622 * for some reason we are stuffing a *really* huge resource.
1623 */
1624 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1625 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
1626 ACPI_RESOURCE_LENGTH) >= buf->Length) {
1627 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1628 return (AE_NO_MEMORY);
1629 bcopy(buf->Pointer, newp, buf->Length);
1630 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1631 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1632 AcpiOsFree(buf->Pointer);
1633 buf->Pointer = newp;
1634 buf->Length += buf->Length;
1635 }
1636
1637 /* Insert the new resource. */
1638 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
1639
1640 /* And add the terminator. */
1641 rp = ACPI_NEXT_RESOURCE(rp);
1642 rp->Id = ACPI_RSTYPE_END_TAG;
1643 rp->Length = 0;
1644
1645 return (AE_OK);
1646}
1647
1648/*
1649 * Set interrupt model.
1650 */
1651ACPI_STATUS
1652acpi_SetIntrModel(int model)
1653{
f9d8cd12 1654 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
5ed44076
MD
1655}
1656
1657#define ACPI_MINIMUM_AWAKETIME 5
1658
1659static void
1660acpi_sleep_enable(void *arg)
1661{
1662 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1663}
1664
1665/*
1666 * Set the system sleep state
1667 *
1668 * Currently we support S1-S5 but S4 is only S4BIOS
1669 */
1670ACPI_STATUS
1671acpi_SetSleepState(struct acpi_softc *sc, int state)
1672{
1673 ACPI_STATUS status = AE_OK;
1674 UINT8 TypeA;
1675 UINT8 TypeB;
1676
1677 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1678 ACPI_ASSERTLOCK;
1679
1680 /* Avoid reentry if already attempting to suspend. */
1681 if (sc->acpi_sstate != ACPI_STATE_S0)
1682 return_ACPI_STATUS (AE_BAD_PARAMETER);
1683
1684 /* We recently woke up so don't suspend again for a while. */
1685 if (sc->acpi_sleep_disabled)
1686 return_ACPI_STATUS (AE_OK);
1687
1688 switch (state) {
1689 case ACPI_STATE_S1:
1690 case ACPI_STATE_S2:
1691 case ACPI_STATE_S3:
1692 case ACPI_STATE_S4:
1693 status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
1694 if (status == AE_NOT_FOUND) {
1695 device_printf(sc->acpi_dev,
1696 "Sleep state S%d not supported by BIOS\n", state);
1697 break;
1698 } else if (ACPI_FAILURE(status)) {
1699 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1700 AcpiFormatException(status));
1701 break;
1702 }
1703
1704 sc->acpi_sstate = state;
1705 sc->acpi_sleep_disabled = 1;
1706
1707 /* Inform all devices that we are going to sleep. */
1708 if (DEVICE_SUSPEND(root_bus) != 0) {
1709 /*
1710 * Re-wake the system.
1711 *
1712 * XXX note that a better two-pass approach with a 'veto' pass
1713 * followed by a "real thing" pass would be better, but the
1714 * current bus interface does not provide for this.
1715 */
1716 DEVICE_RESUME(root_bus);
1717 return_ACPI_STATUS (AE_ERROR);
1718 }
1719
1720 status = AcpiEnterSleepStatePrep(state);
1721 if (ACPI_FAILURE(status)) {
1722 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1723 AcpiFormatException(status));
1724 break;
1725 }
1726
1727 if (sc->acpi_sleep_delay > 0)
1728 DELAY(sc->acpi_sleep_delay * 1000000);
1729
1730 if (state != ACPI_STATE_S1) {
1731 acpi_sleep_machdep(sc, state);
1732
1733 /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
1734 if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
1735 ACPI_MUTEX_NOT_ACQUIRED) {
1736
1737 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1738 }
1739
1740 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1741 if (state == ACPI_STATE_S4)
1742 AcpiEnable();
1743 } else {
1744 status = AcpiEnterSleepState((UINT8)state);
1745 if (ACPI_FAILURE(status)) {
1746 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1747 AcpiFormatException(status));
1748 break;
1749 }
1750 }
1751 AcpiLeaveSleepState((UINT8)state);
1752 DEVICE_RESUME(root_bus);
1753 sc->acpi_sstate = ACPI_STATE_S0;
1754 acpi_enable_fixed_events(sc);
1755 break;
1756 case ACPI_STATE_S5:
1757 /*
1758 * Shut down cleanly and power off. This will call us back through the
1759 * shutdown handlers.
1760 */
1761 shutdown_nice(RB_POWEROFF);
1762 break;
1763 case ACPI_STATE_S0:
1764 default:
1765 status = AE_BAD_PARAMETER;
1766 break;
1767 }
1768
1769 /* Disable a second sleep request for a short period */
1770 if (sc->acpi_sleep_disabled)
1771 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1772
1773 return_ACPI_STATUS (status);
1774}
1775
1776/*
1777 * Enable/Disable ACPI
1778 */
1779ACPI_STATUS
1780acpi_Enable(struct acpi_softc *sc)
1781{
1782 ACPI_STATUS status;
1783 u_int32_t flags;
1784
1785 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1786 ACPI_ASSERTLOCK;
1787
1788 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
1789 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1790 if (!sc->acpi_enabled)
1791 status = AcpiEnableSubsystem(flags);
1792 else
1793 status = AE_OK;
1794
1795 if (status == AE_OK)
1796 sc->acpi_enabled = 1;
1797
1798 return_ACPI_STATUS (status);
1799}
1800
1801ACPI_STATUS
1802acpi_Disable(struct acpi_softc *sc)
1803{
1804 ACPI_STATUS status;
1805
1806 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1807 ACPI_ASSERTLOCK;
1808
1809 if (sc->acpi_enabled)
1810 status = AcpiDisable();
1811 else
1812 status = AE_OK;
1813
1814 if (status == AE_OK)
1815 sc->acpi_enabled = 0;
1816
1817 return_ACPI_STATUS (status);
1818}
1819
1820/*
1821 * ACPI Event Handlers
1822 */
1823
1824/* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
1825
1826static void
1827acpi_system_eventhandler_sleep(void *arg, int state)
1828{
1829 ACPI_LOCK_DECL;
1830 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1831
1832 ACPI_LOCK;
1833 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
1834 acpi_SetSleepState((struct acpi_softc *)arg, state);
1835 ACPI_UNLOCK;
1836 return_VOID;
1837}
1838
1839static void
1840acpi_system_eventhandler_wakeup(void *arg, int state)
1841{
1842 ACPI_LOCK_DECL;
1843 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1844
1845 /* Well, what to do? :-) */
1846
1847 ACPI_LOCK;
1848 ACPI_UNLOCK;
1849
1850 return_VOID;
1851}
1852
1853/*
1854 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
1855 */
1856UINT32
1857acpi_event_power_button_sleep(void *context)
1858{
1859 struct acpi_softc *sc = (struct acpi_softc *)context;
1860
1861 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1862
1863 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
1864
1865 return_VALUE (ACPI_INTERRUPT_HANDLED);
1866}
1867
1868UINT32
1869acpi_event_power_button_wake(void *context)
1870{
1871 struct acpi_softc *sc = (struct acpi_softc *)context;
1872
1873 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1874
1875 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
1876
1877 return_VALUE (ACPI_INTERRUPT_HANDLED);
1878}
1879
1880UINT32
1881acpi_event_sleep_button_sleep(void *context)
1882{
1883 struct acpi_softc *sc = (struct acpi_softc *)context;
1884
1885 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1886
1887 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
1888
1889 return_VALUE (ACPI_INTERRUPT_HANDLED);
1890}
1891
1892UINT32
1893acpi_event_sleep_button_wake(void *context)
1894{
1895 struct acpi_softc *sc = (struct acpi_softc *)context;
1896
1897 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1898
1899 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
1900
1901 return_VALUE (ACPI_INTERRUPT_HANDLED);
1902}
1903
1904/*
1905 * XXX This is kinda ugly, and should not be here.
1906 */
1907struct acpi_staticbuf {
1908 ACPI_BUFFER buffer;
1909 char data[512];
1910};
1911
1912char *
1913acpi_name(ACPI_HANDLE handle)
1914{
1915 static struct acpi_staticbuf buf;
1916
1917 ACPI_ASSERTLOCK;
1918
1919 buf.buffer.Length = 512;
1920 buf.buffer.Pointer = &buf.data[0];
1921
1922 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
1923 return (buf.buffer.Pointer);
1924
1925 return ("(unknown path)");
1926}
1927
1928/*
1929 * Debugging/bug-avoidance. Avoid trying to fetch info on various
1930 * parts of the namespace.
1931 */
1932int
1933acpi_avoid(ACPI_HANDLE handle)
1934{
1935 char *cp, *env, *np;
1936 int len;
1937
1938 np = acpi_name(handle);
1939 if (*np == '\\')
1940 np++;
1941 if ((env = getenv("debug.acpi.avoid")) == NULL)
1942 return (0);
1943
1944 /* Scan the avoid list checking for a match */
1945 cp = env;
1946 for (;;) {
1947 while ((*cp != 0) && isspace(*cp))
1948 cp++;
1949 if (*cp == 0)
1950 break;
1951 len = 0;
1952 while ((cp[len] != 0) && !isspace(cp[len]))
1953 len++;
1954 if (!strncmp(cp, np, len)) {
1955 freeenv(env);
1956 return(1);
1957 }
1958 cp += len;
1959 }
1960 freeenv(env);
1961
1962 return (0);
1963}
1964
1965/*
1966 * Debugging/bug-avoidance. Disable ACPI subsystem components.
1967 */
1968int
1969acpi_disabled(char *subsys)
1970{
1971 char *cp, *env;
1972 int len;
1973
f9d8cd12 1974 if ((env = getenv("debug.acpi.disabled")) == NULL)
5ed44076 1975 return (0);
f9d8cd12 1976 if (strcmp(env, "all") == 0) {
5ed44076
MD
1977 freeenv(env);
1978 return (1);
1979 }
1980
f9d8cd12 1981 /* Scan the disable list, checking for a match. */
5ed44076
MD
1982 cp = env;
1983 for (;;) {
f9d8cd12 1984 while (*cp != '\0' && isspace(*cp))
5ed44076 1985 cp++;
f9d8cd12 1986 if (*cp == '\0')
5ed44076
MD
1987 break;
1988 len = 0;
f9d8cd12 1989 while (cp[len] != '\0' && !isspace(cp[len]))
5ed44076 1990 len++;
f9d8cd12 1991 if (strncmp(cp, subsys, len) == 0) {
5ed44076
MD
1992 freeenv(env);
1993 return (1);
1994 }
1995 cp += len;
1996 }
1997 freeenv(env);
1998
1999 return (0);
2000}
2001
2002/*
2003 * Device wake capability enable/disable.
2004 */
2005void
2006acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
2007{
5ed44076
MD
2008 /*
2009 * TBD: All Power Resources referenced by elements 2 through N
2010 * of the _PRW object are put into the ON state.
2011 */
2012
f9d8cd12 2013 (void)acpi_SetInteger(h, "_PSW", enable);
5ed44076
MD
2014}
2015
2016void
2017acpi_device_enable_wake_event(ACPI_HANDLE h)
2018{
2019 struct acpi_softc *sc;
f9d8cd12
MD
2020 uint32_t gpe_bit, lowest_wake;
2021 ACPI_HANDLE handle;
5ed44076
MD
2022 ACPI_STATUS status;
2023 ACPI_BUFFER prw_buffer;
f9d8cd12 2024 ACPI_OBJECT *res, *res2;
5ed44076
MD
2025
2026 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2027
2028 sc = devclass_get_softc(acpi_devclass, 0);
2029 if (sc == NULL)
2030 return;
2031
2032 /*
f9d8cd12
MD
2033 * The _PRW object (7.2.9) is only required for devices that have the
2034 * ability to wake the system from a sleeping state.
5ed44076 2035 */
f9d8cd12 2036 prw_buffer.Pointer = NULL;
5ed44076
MD
2037 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2038 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2039 if (ACPI_FAILURE(status))
2040 return;
5ed44076
MD
2041 res = (ACPI_OBJECT *)prw_buffer.Pointer;
2042 if (res == NULL)
2043 return;
f9d8cd12 2044 if (!ACPI_PKG_VALID(res, 2))
5ed44076 2045 goto out;
5ed44076
MD
2046
2047 /*
f9d8cd12
MD
2048 * Element 1 of the _PRW object:
2049 * The lowest power system sleeping state that can be entered while still
2050 * providing wake functionality. The sleeping state being entered must
2051 * be less than (i.e., higher power) or equal to this value.
5ed44076 2052 */
f9d8cd12 2053 if (acpi_PkgInt32(res, 1, &lowest_wake) != 0)
5ed44076 2054 goto out;
f9d8cd12 2055 if (sc->acpi_sstate > lowest_wake)
5ed44076
MD
2056 goto out;
2057
2058 /*
f9d8cd12 2059 * Element 0 of the _PRW object:
5ed44076 2060 */
f9d8cd12 2061 switch (res->Package.Elements[0].Type) {
5ed44076 2062 case ACPI_TYPE_INTEGER:
f9d8cd12 2063 /*
5ed44076
MD
2064 * If the data type of this package element is numeric, then this
2065 * _PRW package element is the bit index in the GPEx_EN, in the
2066 * GPE blocks described in the FADT, of the enable bit that is
2067 * enabled for the wake event.
2068 */
f9d8cd12
MD
2069 gpe_bit = res->Package.Elements[0].Integer.Value;
2070 status = AcpiEnableGpe(NULL, gpe_bit, ACPI_EVENT_WAKE_ENABLE);
5ed44076 2071 if (ACPI_FAILURE(status))
f9d8cd12
MD
2072 printf("wake enable: AcpiEnableGpe failed for %u\n",
2073 gpe_bit);
5ed44076
MD
2074 break;
2075 case ACPI_TYPE_PACKAGE:
2076 /*
5ed44076
MD
2077 * If the data type of this package element is a package, then this
2078 * _PRW package element is itself a package containing two
f9d8cd12 2079 * elements. The first is an object reference to the GPE Block
5ed44076 2080 * device that contains the GPE that will be triggered by the wake
f9d8cd12 2081 * event. The second element is numeric and it contains the bit
5ed44076
MD
2082 * index in the GPEx_EN, in the GPE Block referenced by the
2083 * first element in the package, of the enable bit that is enabled for
2084 * the wake event.
f9d8cd12 2085 *
5ed44076
MD
2086 * For example, if this field is a package then it is of the form:
2087 * Package() {\_SB.PCI0.ISA.GPE, 2}
2088 */
f9d8cd12
MD
2089 res2 = &res->Package.Elements[0];
2090 if (!ACPI_PKG_VALID(res2, 2))
2091 goto out;
2092 handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2093 if (handle == NULL || acpi_PkgInt32(res2, 1, &gpe_bit) != 0)
2094 goto out;
2095 status = AcpiEnableGpe(handle, gpe_bit, ACPI_EVENT_WAKE_ENABLE);
2096 if (ACPI_FAILURE(status))
2097 printf("wake enable: AcpiEnableGpe (package) failed for %u\n",
2098 gpe_bit);
5ed44076
MD
2099 break;
2100 default:
2101 break;
2102 }
2103
2104out:
2105 if (prw_buffer.Pointer != NULL)
2106 AcpiOsFree(prw_buffer.Pointer);
2107}
2108
2109/*
2110 * Control interface.
2111 *
2112 * We multiplex ioctls for all participating ACPI devices here. Individual
2113 * drivers wanting to be accessible via /dev/acpi should use the
2114 * register/deregister interface to make their handlers visible.
2115 */
2116struct acpi_ioctl_hook
2117{
2118 TAILQ_ENTRY(acpi_ioctl_hook) link;
2119 u_long cmd;
2120 acpi_ioctl_fn fn;
2121 void *arg;
2122};
2123
2124static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
2125static int acpi_ioctl_hooks_initted;
2126
2127/*
2128 * Register an ioctl handler.
2129 */
2130int
2131acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
2132{
2133 struct acpi_ioctl_hook *hp;
2134
8c1d15dd 2135 hp = malloc(sizeof(*hp), M_ACPIDEV, M_INTWAIT);
5ed44076
MD
2136 hp->cmd = cmd;
2137 hp->fn = fn;
2138 hp->arg = arg;
2139 if (acpi_ioctl_hooks_initted == 0) {
2140 TAILQ_INIT(&acpi_ioctl_hooks);
2141 acpi_ioctl_hooks_initted = 1;
2142 }
2143 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
2144 return (0);
2145}
2146
2147/*
2148 * Deregister an ioctl handler.
2149 */
2150void
2151acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
2152{
2153 struct acpi_ioctl_hook *hp;
2154
2155 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
2156 if ((hp->cmd == cmd) && (hp->fn == fn))
2157 break;
2158
2159 if (hp != NULL) {
2160 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
2161 free(hp, M_ACPIDEV);
2162 }
2163}
2164
2165static int
2166acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
2167{
2168 return (0);
2169}
2170
2171static int
2172acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
2173{
2174 return (0);
2175}
2176
2177static int
2178acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
2179{
2180 struct acpi_softc *sc;
2181 struct acpi_ioctl_hook *hp;
2182 int error, xerror, state;
2183 ACPI_LOCK_DECL;
2184
2185 ACPI_LOCK;
2186
2187 error = state = 0;
2188 sc = dev->si_drv1;
2189
2190 /*
2191 * Scan the list of registered ioctls, looking for handlers.
2192 */
2193 if (acpi_ioctl_hooks_initted) {
2194 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2195 if (hp->cmd == cmd) {
2196 xerror = hp->fn(cmd, addr, hp->arg);
2197 if (xerror != 0)
2198 error = xerror;
2199 goto out;
2200 }
2201 }
2202 }
2203
2204 /*
2205 * Core ioctls are not permitted for non-writable user.
2206 * Currently, other ioctls just fetch information.
2207 * Not changing system behavior.
2208 */
2209 if((flag & FWRITE) == 0)
2210 return (EPERM);
2211
2212 /* Core system ioctls. */
2213 switch (cmd) {
2214 case ACPIIO_ENABLE:
2215 if (ACPI_FAILURE(acpi_Enable(sc)))
2216 error = ENXIO;
2217 break;
2218 case ACPIIO_DISABLE:
2219 if (ACPI_FAILURE(acpi_Disable(sc)))
2220 error = ENXIO;
2221 break;
2222 case ACPIIO_SETSLPSTATE:
2223 if (!sc->acpi_enabled) {
2224 error = ENXIO;
2225 break;
2226 }
2227 state = *(int *)addr;
2228 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
2229 if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
2230 error = EINVAL;
2231 } else {
2232 error = EINVAL;
2233 }
2234 break;
2235 default:
2236 if (error == 0)
2237 error = EINVAL;
2238 break;
2239 }
2240
2241out:
2242 ACPI_UNLOCK;
2243 return (error);
2244}
2245
2246static int
2247acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2248{
2249 char sleep_state[4];
2250 char buf[16];
2251 int error;
2252 UINT8 state, TypeA, TypeB;
2253
2254 buf[0] = '\0';
f9d8cd12 2255 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) {
5ed44076
MD
2256 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2257 sprintf(sleep_state, "S%d ", state);
2258 strcat(buf, sleep_state);
2259 }
2260 }
2261 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2262 return (error);
2263}
2264
2265static int
2266acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2267{
2268 char sleep_state[10];
2269 int error;
2270 u_int new_state, old_state;
2271
2272 old_state = *(u_int *)oidp->oid_arg1;
f9d8cd12 2273 if (old_state > ACPI_S_STATES_MAX + 1) {
5ed44076
MD
2274 strcpy(sleep_state, "unknown");
2275 } else {
2276 bzero(sleep_state, sizeof(sleep_state));
2277 strncpy(sleep_state, sleep_state_names[old_state],
2278 sizeof(sleep_state_names[old_state]));
2279 }
2280 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
2281 if (error == 0 && req->newptr != NULL) {
2282 new_state = ACPI_STATE_S0;
2283 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
2284 if (strncmp(sleep_state, sleep_state_names[new_state],
2285 sizeof(sleep_state)) == 0)
2286 break;
2287 }
2288 if (new_state <= ACPI_S_STATES_MAX + 1) {
2289 if (new_state != old_state)
2290 *(u_int *)oidp->oid_arg1 = new_state;
2291 } else {
2292 error = EINVAL;
2293 }
2294 }
2295
2296 return (error);
2297}
2298
2299/* Inform devctl(4) when we receive a Notify. */
2300void
2301acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
2302{
2303 char notify_buf[16];
2304 ACPI_BUFFER handle_buf;
2305 ACPI_STATUS status;
2306
2307 if (subsystem == NULL)
2308 return;
2309
2310 handle_buf.Pointer = NULL;
2311 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
2312 status = AcpiNsHandleToPathname(h, &handle_buf);
2313 if (ACPI_FAILURE(status))
2314 return;
2315 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
2316#if 0
2317 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
2318#endif
2319 AcpiOsFree(handle_buf.Pointer);
2320}
2321
2322#ifdef ACPI_DEBUG
2323/*
2324 * Support for parsing debug options from the kernel environment.
2325 *
2326 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
2327 * by specifying the names of the bits in the debug.acpi.layer and
2328 * debug.acpi.level environment variables. Bits may be unset by
2329 * prefixing the bit name with !.
2330 */
2331struct debugtag
2332{
2333 char *name;
2334 UINT32 value;
2335};
2336
2337static struct debugtag dbg_layer[] = {
2338 {"ACPI_UTILITIES", ACPI_UTILITIES},
2339 {"ACPI_HARDWARE", ACPI_HARDWARE},
2340 {"ACPI_EVENTS", ACPI_EVENTS},
2341 {"ACPI_TABLES", ACPI_TABLES},
2342 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
2343 {"ACPI_PARSER", ACPI_PARSER},
2344 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
2345 {"ACPI_EXECUTER", ACPI_EXECUTER},
2346 {"ACPI_RESOURCES", ACPI_RESOURCES},
2347 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
2348 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
2349 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
2350 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
2351
5ed44076
MD
2352 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
2353 {"ACPI_BATTERY", ACPI_BATTERY},
f9d8cd12 2354 {"ACPI_BUS", ACPI_BUS},
5ed44076 2355 {"ACPI_BUTTON", ACPI_BUTTON},
f9d8cd12
MD
2356 {"ACPI_EC", ACPI_EC},
2357 {"ACPI_FAN", ACPI_FAN},
2358 {"ACPI_POWERRES", ACPI_POWERRES},
5ed44076
MD
2359 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
2360 {"ACPI_THERMAL", ACPI_THERMAL},
f9d8cd12 2361 {"ACPI_TIMER", ACPI_TIMER},
5ed44076
MD
2362 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
2363 {NULL, 0}
2364};
2365
2366static struct debugtag dbg_level[] = {
2367 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
2368 {"ACPI_LV_WARN", ACPI_LV_WARN},
2369 {"ACPI_LV_INIT", ACPI_LV_INIT},
2370 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
2371 {"ACPI_LV_INFO", ACPI_LV_INFO},
2372 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
2373
2374 /* Trace verbosity level 1 [Standard Trace Level] */
2375 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
2376 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
2377 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
2378 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
2379 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
2380 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
2381 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
2382 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
2383 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
2384 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
2385 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
2386 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
2387 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
2388 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
2389 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
2390
2391 /* Trace verbosity level 2 [Function tracing and memory allocation] */
2392 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
2393 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
2394 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
2395 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
2396 {"ACPI_LV_ALL", ACPI_LV_ALL},
2397
2398 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2399 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
2400 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
2401 {"ACPI_LV_IO", ACPI_LV_IO},
2402 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
2403 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
2404
2405 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
2406 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
2407 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
2408 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
2409 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
2410 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
2411 {NULL, 0}
2412};
2413
2414static void
2415acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
2416{
2417 char *ep;
2418 int i, l;
2419 int set;
2420
2421 while (*cp) {
2422 if (isspace(*cp)) {
2423 cp++;
2424 continue;
2425 }
2426 ep = cp;
2427 while (*ep && !isspace(*ep))
2428 ep++;
2429 if (*cp == '!') {
2430 set = 0;
2431 cp++;
2432 if (cp == ep)
2433 continue;
2434 } else {
2435 set = 1;
2436 }
2437 l = ep - cp;
2438 for (i = 0; tag[i].name != NULL; i++) {
2439 if (!strncmp(cp, tag[i].name, l)) {
2440 if (set)
2441 *flag |= tag[i].value;
2442 else
2443 *flag &= ~tag[i].value;
5ed44076
MD
2444 }
2445 }
2446 cp = ep;
2447 }
2448}
2449
2450static void
2451acpi_set_debugging(void *junk)
2452{
f9d8cd12 2453 char *layer, *level;
5ed44076
MD
2454
2455 if (cold) {
2456 AcpiDbgLayer = 0;
2457 AcpiDbgLevel = 0;
2458 }
2459
f9d8cd12
MD
2460 layer = getenv("debug.acpi.layer");
2461 level = getenv("debug.acpi.level");
2462 if (layer == NULL && level == NULL)
2463 return;
5ed44076 2464
f9d8cd12
MD
2465 printf("ACPI set debug");
2466 if (layer != NULL) {
2467 if (strcmp("NONE", layer) != 0)
2468 printf(" layer '%s'", layer);
2469 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
2470 freeenv(layer);
2471 }
2472 if (level != NULL) {
2473 if (strcmp("NONE", level) != 0)
2474 printf(" level '%s'", level);
2475 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
2476 freeenv(level);
5ed44076 2477 }
f9d8cd12 2478 printf("\n");
5ed44076
MD
2479}
2480SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2481 NULL);
2482
2483static int
2484acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
2485{
2486 int error, *dbg;
2487 struct debugtag *tag;
2488 struct sbuf sb;
2489
2490 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
2491 return (ENOMEM);
2492 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
2493 tag = &dbg_layer[0];
2494 dbg = &AcpiDbgLayer;
2495 } else {
2496 tag = &dbg_level[0];
2497 dbg = &AcpiDbgLevel;
2498 }
2499
2500 /* Get old values if this is a get request. */
2501 if (*dbg == 0) {
2502 sbuf_cpy(&sb, "NONE");
2503 } else if (req->newptr == NULL) {
2504 for (; tag->name != NULL; tag++) {
2505 if ((*dbg & tag->value) == tag->value)
2506 sbuf_printf(&sb, "%s ", tag->name);
2507 }
2508 }
2509 sbuf_trim(&sb);
2510 sbuf_finish(&sb);
2511
f9d8cd12
MD
2512 /* Copy out the old values to the user. */
2513 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
5ed44076
MD
2514 sbuf_delete(&sb);
2515
2516 /* If the user is setting a string, parse it. */
2517 if (error == 0 && req->newptr != NULL) {
2518 *dbg = 0;
2519 setenv((char *)oidp->oid_arg1, (char *)req->newptr);
2520 acpi_set_debugging(NULL);
2521 }
2522
2523 return (error);
2524}
2525SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
2526 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
2527SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
2528 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
2529#endif
2530
2531static int
2532acpi_pm_func(u_long cmd, void *arg, ...)
2533{
2534 int state, acpi_state;
2535 int error;
2536 struct acpi_softc *sc;
2537 va_list ap;
2538
2539 error = 0;
2540 switch (cmd) {
2541 case POWER_CMD_SUSPEND:
2542 sc = (struct acpi_softc *)arg;
2543 if (sc == NULL) {
2544 error = EINVAL;
2545 goto out;
2546 }
2547
2548 va_start(ap, arg);
2549 state = va_arg(ap, int);
2550 va_end(ap);
2551
2552 switch (state) {
2553 case POWER_SLEEP_STATE_STANDBY:
2554 acpi_state = sc->acpi_standby_sx;
2555 break;
2556 case POWER_SLEEP_STATE_SUSPEND:
2557 acpi_state = sc->acpi_suspend_sx;
2558 break;
2559 case POWER_SLEEP_STATE_HIBERNATE:
2560 acpi_state = ACPI_STATE_S4;
2561 break;
2562 default:
2563 error = EINVAL;
2564 goto out;
2565 }
2566
2567 acpi_SetSleepState(sc, acpi_state);
2568 break;
2569 default:
2570 error = EINVAL;
2571 goto out;
2572 }
2573
2574out:
2575 return (error);
2576}
2577
2578static void
2579acpi_pm_register(void *arg)
2580{
2581 if (!cold || resource_disabled("acpi", 0))
2582 return;
2583
2584 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2585}
2586
2587SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);