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