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