FreeBSD/Linux Kernel Cross Reference
sys/dev/acpica/acpi.c
1 /*-
2 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * Copyright (c) 2000, 2001 Michael Smith
5 * Copyright (c) 2000 BSDi
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: releng/7.3/sys/dev/acpica/acpi.c 198618 2009-10-29 17:15:22Z jhb $");
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/module.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/ioccom.h>
43 #include <sys/reboot.h>
44 #include <sys/sysctl.h>
45 #include <sys/ctype.h>
46 #include <sys/linker.h>
47 #include <sys/power.h>
48 #include <sys/sbuf.h>
49 #include <sys/smp.h>
50
51 #if defined(__i386__) || defined(__amd64__)
52 #include <machine/pci_cfgreg.h>
53 #endif
54 #include <machine/resource.h>
55 #include <machine/bus.h>
56 #include <sys/rman.h>
57 #include <isa/isavar.h>
58 #include <isa/pnpvar.h>
59
60 #include <contrib/dev/acpica/acpi.h>
61 #include <dev/acpica/acpivar.h>
62 #include <dev/acpica/acpiio.h>
63 #include <contrib/dev/acpica/achware.h>
64 #include <contrib/dev/acpica/acnamesp.h>
65
66 #include "pci_if.h"
67 #include <dev/pci/pcivar.h>
68 #include <dev/pci/pci_private.h>
69
70 #include <vm/vm_param.h>
71
72 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
73
74 /* Hooks for the ACPI CA debugging infrastructure */
75 #define _COMPONENT ACPI_BUS
76 ACPI_MODULE_NAME("ACPI")
77
78 static d_open_t acpiopen;
79 static d_close_t acpiclose;
80 static d_ioctl_t acpiioctl;
81
82 static struct cdevsw acpi_cdevsw = {
83 .d_version = D_VERSION,
84 .d_open = acpiopen,
85 .d_close = acpiclose,
86 .d_ioctl = acpiioctl,
87 .d_name = "acpi",
88 };
89
90 /* Global mutex for locking access to the ACPI subsystem. */
91 struct mtx acpi_mutex;
92
93 /* Bitmap of device quirks. */
94 int acpi_quirks;
95
96 static int acpi_modevent(struct module *mod, int event, void *junk);
97 static void acpi_identify(driver_t *driver, device_t parent);
98 static int acpi_probe(device_t dev);
99 static int acpi_attach(device_t dev);
100 static int acpi_suspend(device_t dev);
101 static int acpi_resume(device_t dev);
102 static int acpi_shutdown(device_t dev);
103 static device_t acpi_add_child(device_t bus, int order, const char *name,
104 int unit);
105 static int acpi_print_child(device_t bus, device_t child);
106 static void acpi_probe_nomatch(device_t bus, device_t child);
107 static void acpi_driver_added(device_t dev, driver_t *driver);
108 static int acpi_read_ivar(device_t dev, device_t child, int index,
109 uintptr_t *result);
110 static int acpi_write_ivar(device_t dev, device_t child, int index,
111 uintptr_t value);
112 static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
113 static int acpi_sysres_alloc(device_t dev);
114 static struct resource *acpi_alloc_resource(device_t bus, device_t child,
115 int type, int *rid, u_long start, u_long end,
116 u_long count, u_int flags);
117 static int acpi_release_resource(device_t bus, device_t child, int type,
118 int rid, struct resource *r);
119 static void acpi_delete_resource(device_t bus, device_t child, int type,
120 int rid);
121 static uint32_t acpi_isa_get_logicalid(device_t dev);
122 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
123 static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids);
124 static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
125 ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
126 ACPI_BUFFER *ret);
127 static int acpi_device_pwr_for_sleep(device_t bus, device_t dev,
128 int *dstate);
129 static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
130 void *context, void **retval);
131 static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
132 int max_depth, acpi_scan_cb_t user_fn, void *arg);
133 static int acpi_set_powerstate_method(device_t bus, device_t child,
134 int state);
135 static int acpi_isa_pnp_probe(device_t bus, device_t child,
136 struct isa_pnp_id *ids);
137 static void acpi_probe_children(device_t bus);
138 static void acpi_probe_order(ACPI_HANDLE handle, int *order);
139 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
140 void *context, void **status);
141 static BOOLEAN acpi_MatchHid(ACPI_HANDLE h, const char *hid);
142 static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
143 static void acpi_shutdown_final(void *arg, int howto);
144 static void acpi_enable_fixed_events(struct acpi_softc *sc);
145 static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
146 static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
147 static int acpi_wake_prep_walk(int sstate);
148 static int acpi_wake_sysctl_walk(device_t dev);
149 static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
150 static void acpi_system_eventhandler_sleep(void *arg, int state);
151 static void acpi_system_eventhandler_wakeup(void *arg, int state);
152 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
153 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
154 static int acpi_pm_func(u_long cmd, void *arg, ...);
155 static int acpi_child_location_str_method(device_t acdev, device_t child,
156 char *buf, size_t buflen);
157 static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
158 char *buf, size_t buflen);
159 #if defined(__i386__) || defined(__amd64__)
160 static void acpi_enable_pcie(void);
161 #endif
162
163 static device_method_t acpi_methods[] = {
164 /* Device interface */
165 DEVMETHOD(device_identify, acpi_identify),
166 DEVMETHOD(device_probe, acpi_probe),
167 DEVMETHOD(device_attach, acpi_attach),
168 DEVMETHOD(device_shutdown, acpi_shutdown),
169 DEVMETHOD(device_detach, bus_generic_detach),
170 DEVMETHOD(device_suspend, acpi_suspend),
171 DEVMETHOD(device_resume, acpi_resume),
172
173 /* Bus interface */
174 DEVMETHOD(bus_add_child, acpi_add_child),
175 DEVMETHOD(bus_print_child, acpi_print_child),
176 DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch),
177 DEVMETHOD(bus_driver_added, acpi_driver_added),
178 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
179 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
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),
183 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
184 DEVMETHOD(bus_release_resource, acpi_release_resource),
185 DEVMETHOD(bus_delete_resource, acpi_delete_resource),
186 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
187 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
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
193 /* ACPI bus */
194 DEVMETHOD(acpi_id_probe, acpi_device_id_probe),
195 DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj),
196 DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep),
197 DEVMETHOD(acpi_scan_children, acpi_device_scan_children),
198
199 /* PCI emulation */
200 DEVMETHOD(pci_set_powerstate, acpi_set_powerstate_method),
201
202 /* ISA emulation */
203 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
204
205 {0, 0}
206 };
207
208 static driver_t acpi_driver = {
209 "acpi",
210 acpi_methods,
211 sizeof(struct acpi_softc),
212 };
213
214 static devclass_t acpi_devclass;
215 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
216 MODULE_VERSION(acpi, 1);
217
218 ACPI_SERIAL_DECL(acpi, "ACPI root bus");
219
220 /* Local pools for managing system resources for ACPI child devices. */
221 static struct rman acpi_rman_io, acpi_rman_mem;
222
223 #define ACPI_MINIMUM_AWAKETIME 5
224
225 static const char* sleep_state_names[] = {
226 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
227
228 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
229 static char acpi_ca_version[12];
230 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
231 acpi_ca_version, 0, "Version of Intel ACPI-CA");
232
233 /*
234 * Allow override of whether methods execute in parallel or not.
235 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
236 * errors for AML that really can't handle parallel method execution.
237 * It is off by default since this breaks recursive methods and
238 * some IBMs use such code.
239 */
240 static int acpi_serialize_methods;
241 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
242
243 /* Power devices off and on in suspend and resume. XXX Remove once tested. */
244 static int acpi_do_powerstate = 1;
245 TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate);
246 SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW,
247 &acpi_do_powerstate, 1, "Turn off devices when suspending.");
248
249 /* Allow users to override quirks. */
250 TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
251
252 static int acpi_susp_bounce;
253 SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
254 &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
255
256 /*
257 * ACPI can only be loaded as a module by the loader; activating it after
258 * system bootstrap time is not useful, and can be fatal to the system.
259 * It also cannot be unloaded, since the entire system bus heirarchy hangs
260 * off it.
261 */
262 static int
263 acpi_modevent(struct module *mod, int event, void *junk)
264 {
265 switch (event) {
266 case MOD_LOAD:
267 if (!cold) {
268 printf("The ACPI driver cannot be loaded after boot.\n");
269 return (EPERM);
270 }
271 break;
272 case MOD_UNLOAD:
273 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
274 return (EBUSY);
275 break;
276 default:
277 break;
278 }
279 return (0);
280 }
281
282 /*
283 * Perform early initialization.
284 */
285 ACPI_STATUS
286 acpi_Startup(void)
287 {
288 static int started = 0;
289 ACPI_STATUS status;
290 int val;
291
292 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
293
294 /* Only run the startup code once. The MADT driver also calls this. */
295 if (started)
296 return_VALUE (AE_OK);
297 started = 1;
298
299 /*
300 * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
301 * if more tables exist.
302 */
303 if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
304 printf("ACPI: Table initialisation failed: %s\n",
305 AcpiFormatException(status));
306 return_VALUE (status);
307 }
308
309 /* Set up any quirks we have for this system. */
310 if (acpi_quirks == ACPI_Q_OK)
311 acpi_table_quirks(&acpi_quirks);
312
313 /* If the user manually set the disabled hint to 0, force-enable ACPI. */
314 if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
315 acpi_quirks &= ~ACPI_Q_BROKEN;
316 if (acpi_quirks & ACPI_Q_BROKEN) {
317 printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n");
318 status = AE_SUPPORT;
319 }
320
321 return_VALUE (status);
322 }
323
324 /*
325 * Detect ACPI, perform early initialisation
326 */
327 static void
328 acpi_identify(driver_t *driver, device_t parent)
329 {
330 device_t child;
331
332 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
333
334 if (!cold)
335 return_VOID;
336
337 /* Check that we haven't been disabled with a hint. */
338 if (resource_disabled("acpi", 0))
339 return_VOID;
340
341 /* Make sure we're not being doubly invoked. */
342 if (device_find_child(parent, "acpi", 0) != NULL)
343 return_VOID;
344
345 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
346
347 /* Initialize root tables. */
348 if (ACPI_FAILURE(acpi_Startup())) {
349 printf("ACPI: Try disabling either ACPI or apic support.\n");
350 return_VOID;
351 }
352
353 /* Attach the actual ACPI device. */
354 if ((child = BUS_ADD_CHILD(parent, 10, "acpi", 0)) == NULL) {
355 device_printf(parent, "device_identify failed\n");
356 return_VOID;
357 }
358 }
359
360 /*
361 * Fetch some descriptive data from ACPI to put in our attach message.
362 */
363 static int
364 acpi_probe(device_t dev)
365 {
366 ACPI_TABLE_RSDP *rsdp;
367 ACPI_TABLE_HEADER *rsdt;
368 ACPI_PHYSICAL_ADDRESS paddr;
369 char buf[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
370 struct sbuf sb;
371
372 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
373
374 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
375 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
376 device_printf(dev, "probe failed, other PM system enabled.\n");
377 return_VALUE (ENXIO);
378 }
379
380 if ((paddr = AcpiOsGetRootPointer()) == 0 ||
381 (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
382 return_VALUE (ENXIO);
383 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
384 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
385 else
386 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
387 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
388
389 if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
390 return_VALUE (ENXIO);
391 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
392 sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
393 sbuf_trim(&sb);
394 sbuf_putc(&sb, ' ');
395 sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
396 sbuf_trim(&sb);
397 sbuf_finish(&sb);
398 device_set_desc_copy(dev, sbuf_data(&sb));
399 sbuf_delete(&sb);
400 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
401
402 return_VALUE (0);
403 }
404
405 static int
406 acpi_attach(device_t dev)
407 {
408 struct acpi_softc *sc;
409 ACPI_TABLE_FACS *facs;
410 ACPI_STATUS status;
411 int error, state;
412 UINT32 flags;
413 UINT8 TypeA, TypeB;
414 char *env;
415
416 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
417
418 sc = device_get_softc(dev);
419 sc->acpi_dev = dev;
420 callout_init(&sc->susp_force_to, TRUE);
421
422 error = ENXIO;
423
424 /* Initialize resource manager. */
425 acpi_rman_io.rm_type = RMAN_ARRAY;
426 acpi_rman_io.rm_start = 0;
427 acpi_rman_io.rm_end = 0xffff;
428 acpi_rman_io.rm_descr = "ACPI I/O ports";
429 if (rman_init(&acpi_rman_io) != 0)
430 panic("acpi rman_init IO ports failed");
431 acpi_rman_mem.rm_type = RMAN_ARRAY;
432 acpi_rman_mem.rm_start = 0;
433 acpi_rman_mem.rm_end = ~0ul;
434 acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
435 if (rman_init(&acpi_rman_mem) != 0)
436 panic("acpi rman_init memory failed");
437
438 /* Initialise the ACPI mutex */
439 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
440
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 = acpi_serialize_methods;
446 AcpiGbl_EnableInterpreterSlack = TRUE;
447
448 /* Start up the ACPI CA subsystem. */
449 status = AcpiInitializeSubsystem();
450 if (ACPI_FAILURE(status)) {
451 device_printf(dev, "Could not initialize Subsystem: %s\n",
452 AcpiFormatException(status));
453 goto out;
454 }
455
456 /* Load ACPI name space. */
457 status = AcpiLoadTables();
458 if (ACPI_FAILURE(status)) {
459 device_printf(dev, "Could not load Namespace: %s\n",
460 AcpiFormatException(status));
461 goto out;
462 }
463
464 #if defined(__i386__) || defined(__amd64__)
465 /* Handle MCFG table if present. */
466 acpi_enable_pcie();
467 #endif
468
469 /* Install the default address space handlers. */
470 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
471 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
472 if (ACPI_FAILURE(status)) {
473 device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
474 AcpiFormatException(status));
475 goto out;
476 }
477 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
478 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
479 if (ACPI_FAILURE(status)) {
480 device_printf(dev, "Could not initialise SystemIO handler: %s\n",
481 AcpiFormatException(status));
482 goto out;
483 }
484 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
485 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
486 if (ACPI_FAILURE(status)) {
487 device_printf(dev, "could not initialise PciConfig handler: %s\n",
488 AcpiFormatException(status));
489 goto out;
490 }
491
492 /*
493 * Note that some systems (specifically, those with namespace evaluation
494 * issues that require the avoidance of parts of the namespace) must
495 * avoid running _INI and _STA on everything, as well as dodging the final
496 * object init pass.
497 *
498 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
499 *
500 * XXX We should arrange for the object init pass after we have attached
501 * all our child devices, but on many systems it works here.
502 */
503 flags = 0;
504 if (testenv("debug.acpi.avoid"))
505 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
506
507 /* Bring the hardware and basic handlers online. */
508 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
509 device_printf(dev, "Could not enable ACPI: %s\n",
510 AcpiFormatException(status));
511 goto out;
512 }
513
514 /*
515 * Call the ECDT probe function to provide EC functionality before
516 * the namespace has been evaluated.
517 *
518 * XXX This happens before the sysresource devices have been probed and
519 * attached so its resources come from nexus0. In practice, this isn't
520 * a problem but should be addressed eventually.
521 */
522 acpi_ec_ecdt_probe(dev);
523
524 /* Bring device objects and regions online. */
525 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
526 device_printf(dev, "Could not initialize ACPI objects: %s\n",
527 AcpiFormatException(status));
528 goto out;
529 }
530
531 /*
532 * Setup our sysctl tree.
533 *
534 * XXX: This doesn't check to make sure that none of these fail.
535 */
536 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
537 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
538 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
539 device_get_name(dev), CTLFLAG_RD, 0, "");
540 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
541 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
542 0, 0, acpi_supported_sleep_state_sysctl, "A", "");
543 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
544 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
545 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
546 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
547 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
548 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
549 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
550 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
551 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
552 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
553 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
554 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
555 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
556 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
557 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
558 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
559 OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
560 "sleep delay");
561 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
562 OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
563 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
564 OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
565 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
566 OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
567 &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
568 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
569 OID_AUTO, "handle_reboot", CTLFLAG_RW,
570 &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
571
572 /*
573 * Default to 1 second before sleeping to give some machines time to
574 * stabilize.
575 */
576 sc->acpi_sleep_delay = 1;
577 if (bootverbose)
578 sc->acpi_verbose = 1;
579 if ((env = getenv("hw.acpi.verbose")) != NULL) {
580 if (strcmp(env, "") != 0)
581 sc->acpi_verbose = 1;
582 freeenv(env);
583 }
584
585 /* Only enable S4BIOS by default if the FACS says it is available. */
586 status = AcpiGetTable(ACPI_SIG_FACS, 0, (ACPI_TABLE_HEADER **)&facs);
587 if (ACPI_FAILURE(status)) {
588 device_printf(dev, "couldn't get FACS: %s\n",
589 AcpiFormatException(status));
590 error = ENXIO;
591 goto out;
592 }
593 if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
594 sc->acpi_s4bios = 1;
595
596 /*
597 * Dispatch the default sleep state to devices. The lid switch is set
598 * to NONE by default to avoid surprising users.
599 */
600 sc->acpi_power_button_sx = ACPI_STATE_S5;
601 sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
602 sc->acpi_standby_sx = ACPI_STATE_S1;
603 sc->acpi_suspend_sx = ACPI_STATE_S3;
604
605 /* Pick the first valid sleep state for the sleep button default. */
606 sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
607 for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
608 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
609 sc->acpi_sleep_button_sx = state;
610 break;
611 }
612
613 acpi_enable_fixed_events(sc);
614
615 /*
616 * Scan the namespace and attach/initialise children.
617 */
618
619 /* Register our shutdown handler. */
620 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
621 SHUTDOWN_PRI_LAST);
622
623 /*
624 * Register our acpi event handlers.
625 * XXX should be configurable eg. via userland policy manager.
626 */
627 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
628 sc, ACPI_EVENT_PRI_LAST);
629 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
630 sc, ACPI_EVENT_PRI_LAST);
631
632 /* Flag our initial states. */
633 sc->acpi_enabled = 1;
634 sc->acpi_sstate = ACPI_STATE_S0;
635 sc->acpi_sleep_disabled = 0;
636
637 /* Create the control device */
638 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
639 "acpi");
640 sc->acpi_dev_t->si_drv1 = sc;
641
642 if ((error = acpi_machdep_init(dev)))
643 goto out;
644
645 /* Register ACPI again to pass the correct argument of pm_func. */
646 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
647
648 if (!acpi_disabled("bus"))
649 acpi_probe_children(dev);
650
651 error = 0;
652
653 out:
654 return_VALUE (error);
655 }
656
657 static int
658 acpi_suspend(device_t dev)
659 {
660 device_t child, *devlist;
661 int error, i, numdevs, pstate;
662
663 GIANT_REQUIRED;
664
665 /* First give child devices a chance to suspend. */
666 error = bus_generic_suspend(dev);
667 if (error)
668 return (error);
669
670 /*
671 * Now, set them into the appropriate power state, usually D3. If the
672 * device has an _SxD method for the next sleep state, use that power
673 * state instead.
674 */
675 device_get_children(dev, &devlist, &numdevs);
676 for (i = 0; i < numdevs; i++) {
677 /* If the device is not attached, we've powered it down elsewhere. */
678 child = devlist[i];
679 if (!device_is_attached(child))
680 continue;
681
682 /*
683 * Default to D3 for all sleep states. The _SxD method is optional
684 * so set the powerstate even if it's absent.
685 */
686 pstate = PCI_POWERSTATE_D3;
687 error = acpi_device_pwr_for_sleep(device_get_parent(child),
688 child, &pstate);
689 if ((error == 0 || error == ESRCH) && acpi_do_powerstate)
690 pci_set_powerstate(child, pstate);
691 }
692 free(devlist, M_TEMP);
693 error = 0;
694
695 return (error);
696 }
697
698 static int
699 acpi_resume(device_t dev)
700 {
701 ACPI_HANDLE handle;
702 int i, numdevs;
703 device_t child, *devlist;
704
705 GIANT_REQUIRED;
706
707 /*
708 * Put all devices in D0 before resuming them. Call _S0D on each one
709 * since some systems expect this.
710 */
711 device_get_children(dev, &devlist, &numdevs);
712 for (i = 0; i < numdevs; i++) {
713 child = devlist[i];
714 handle = acpi_get_handle(child);
715 if (handle)
716 AcpiEvaluateObject(handle, "_S0D", NULL, NULL);
717 if (device_is_attached(child) && acpi_do_powerstate)
718 pci_set_powerstate(child, PCI_POWERSTATE_D0);
719 }
720 free(devlist, M_TEMP);
721
722 return (bus_generic_resume(dev));
723 }
724
725 static int
726 acpi_shutdown(device_t dev)
727 {
728
729 GIANT_REQUIRED;
730
731 /* Allow children to shutdown first. */
732 bus_generic_shutdown(dev);
733
734 /*
735 * Enable any GPEs that are able to power-on the system (i.e., RTC).
736 * Also, disable any that are not valid for this state (most).
737 */
738 acpi_wake_prep_walk(ACPI_STATE_S5);
739
740 return (0);
741 }
742
743 /*
744 * Handle a new device being added
745 */
746 static device_t
747 acpi_add_child(device_t bus, int order, const char *name, int unit)
748 {
749 struct acpi_device *ad;
750 device_t child;
751
752 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
753 return (NULL);
754
755 resource_list_init(&ad->ad_rl);
756
757 child = device_add_child_ordered(bus, order, name, unit);
758 if (child != NULL)
759 device_set_ivars(child, ad);
760 else
761 free(ad, M_ACPIDEV);
762 return (child);
763 }
764
765 static int
766 acpi_print_child(device_t bus, device_t child)
767 {
768 struct acpi_device *adev = device_get_ivars(child);
769 struct resource_list *rl = &adev->ad_rl;
770 int retval = 0;
771
772 retval += bus_print_child_header(bus, child);
773 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
774 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
775 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
776 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
777 if (device_get_flags(child))
778 retval += printf(" flags %#x", device_get_flags(child));
779 retval += bus_print_child_footer(bus, child);
780
781 return (retval);
782 }
783
784 /*
785 * If this device is an ACPI child but no one claimed it, attempt
786 * to power it off. We'll power it back up when a driver is added.
787 *
788 * XXX Disabled for now since many necessary devices (like fdc and
789 * ATA) don't claim the devices we created for them but still expect
790 * them to be powered up.
791 */
792 static void
793 acpi_probe_nomatch(device_t bus, device_t child)
794 {
795
796 /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
797 }
798
799 /*
800 * If a new driver has a chance to probe a child, first power it up.
801 *
802 * XXX Disabled for now (see acpi_probe_nomatch for details).
803 */
804 static void
805 acpi_driver_added(device_t dev, driver_t *driver)
806 {
807 device_t child, *devlist;
808 int i, numdevs;
809
810 DEVICE_IDENTIFY(driver, dev);
811 device_get_children(dev, &devlist, &numdevs);
812 for (i = 0; i < numdevs; i++) {
813 child = devlist[i];
814 if (device_get_state(child) == DS_NOTPRESENT) {
815 /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */
816 if (device_probe_and_attach(child) != 0)
817 ; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
818 }
819 }
820 free(devlist, M_TEMP);
821 }
822
823 /* Location hint for devctl(8) */
824 static int
825 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
826 size_t buflen)
827 {
828 struct acpi_device *dinfo = device_get_ivars(child);
829
830 if (dinfo->ad_handle)
831 snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
832 else
833 snprintf(buf, buflen, "unknown");
834 return (0);
835 }
836
837 /* PnP information for devctl(8) */
838 static int
839 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
840 size_t buflen)
841 {
842 ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
843 ACPI_DEVICE_INFO *adinfo;
844 struct acpi_device *dinfo = device_get_ivars(child);
845 char *end;
846 int error;
847
848 error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
849 adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
850 if (error)
851 snprintf(buf, buflen, "unknown");
852 else
853 snprintf(buf, buflen, "_HID=%s _UID=%lu",
854 (adinfo->Valid & ACPI_VALID_HID) ?
855 adinfo->HardwareId.Value : "none",
856 (adinfo->Valid & ACPI_VALID_UID) ?
857 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
858 if (adinfo)
859 AcpiOsFree(adinfo);
860
861 return (0);
862 }
863
864 /*
865 * Handle per-device ivars
866 */
867 static int
868 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
869 {
870 struct acpi_device *ad;
871
872 if ((ad = device_get_ivars(child)) == NULL) {
873 printf("device has no ivars\n");
874 return (ENOENT);
875 }
876
877 /* ACPI and ISA compatibility ivars */
878 switch(index) {
879 case ACPI_IVAR_HANDLE:
880 *(ACPI_HANDLE *)result = ad->ad_handle;
881 break;
882 case ACPI_IVAR_MAGIC:
883 *(uintptr_t *)result = ad->ad_magic;
884 break;
885 case ACPI_IVAR_PRIVATE:
886 *(void **)result = ad->ad_private;
887 break;
888 case ACPI_IVAR_FLAGS:
889 *(int *)result = ad->ad_flags;
890 break;
891 case ISA_IVAR_VENDORID:
892 case ISA_IVAR_SERIAL:
893 case ISA_IVAR_COMPATID:
894 *(int *)result = -1;
895 break;
896 case ISA_IVAR_LOGICALID:
897 *(int *)result = acpi_isa_get_logicalid(child);
898 break;
899 default:
900 return (ENOENT);
901 }
902
903 return (0);
904 }
905
906 static int
907 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
908 {
909 struct acpi_device *ad;
910
911 if ((ad = device_get_ivars(child)) == NULL) {
912 printf("device has no ivars\n");
913 return (ENOENT);
914 }
915
916 switch(index) {
917 case ACPI_IVAR_HANDLE:
918 ad->ad_handle = (ACPI_HANDLE)value;
919 break;
920 case ACPI_IVAR_MAGIC:
921 ad->ad_magic = (uintptr_t)value;
922 break;
923 case ACPI_IVAR_PRIVATE:
924 ad->ad_private = (void *)value;
925 break;
926 case ACPI_IVAR_FLAGS:
927 ad->ad_flags = (int)value;
928 break;
929 default:
930 panic("bad ivar write request (%d)", index);
931 return (ENOENT);
932 }
933
934 return (0);
935 }
936
937 /*
938 * Handle child resource allocation/removal
939 */
940 static struct resource_list *
941 acpi_get_rlist(device_t dev, device_t child)
942 {
943 struct acpi_device *ad;
944
945 ad = device_get_ivars(child);
946 return (&ad->ad_rl);
947 }
948
949 /*
950 * Pre-allocate/manage all memory and IO resources. Since rman can't handle
951 * duplicates, we merge any in the sysresource attach routine.
952 */
953 static int
954 acpi_sysres_alloc(device_t dev)
955 {
956 struct resource *res;
957 struct resource_list *rl;
958 struct resource_list_entry *rle;
959 struct rman *rm;
960 char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
961 device_t *children;
962 int child_count, i;
963
964 /*
965 * Probe/attach any sysresource devices. This would be unnecessary if we
966 * had multi-pass probe/attach.
967 */
968 if (device_get_children(dev, &children, &child_count) != 0)
969 return (ENXIO);
970 for (i = 0; i < child_count; i++) {
971 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
972 device_probe_and_attach(children[i]);
973 }
974 free(children, M_TEMP);
975
976 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
977 STAILQ_FOREACH(rle, rl, link) {
978 if (rle->res != NULL) {
979 device_printf(dev, "duplicate resource for %lx\n", rle->start);
980 continue;
981 }
982
983 /* Only memory and IO resources are valid here. */
984 switch (rle->type) {
985 case SYS_RES_IOPORT:
986 rm = &acpi_rman_io;
987 break;
988 case SYS_RES_MEMORY:
989 rm = &acpi_rman_mem;
990 break;
991 default:
992 continue;
993 }
994
995 /* Pre-allocate resource and add to our rman pool. */
996 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
997 &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
998 if (res != NULL) {
999 rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1000 rle->res = res;
1001 } else
1002 device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
1003 rle->start, rle->count, rle->type);
1004 }
1005 return (0);
1006 }
1007
1008 static struct resource *
1009 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
1010 u_long start, u_long end, u_long count, u_int flags)
1011 {
1012 ACPI_RESOURCE ares;
1013 struct acpi_device *ad = device_get_ivars(child);
1014 struct resource_list *rl = &ad->ad_rl;
1015 struct resource_list_entry *rle;
1016 struct resource *res;
1017 struct rman *rm;
1018
1019 res = NULL;
1020
1021 /* We only handle memory and IO resources through rman. */
1022 switch (type) {
1023 case SYS_RES_IOPORT:
1024 rm = &acpi_rman_io;
1025 break;
1026 case SYS_RES_MEMORY:
1027 rm = &acpi_rman_mem;
1028 break;
1029 default:
1030 rm = NULL;
1031 }
1032
1033 ACPI_SERIAL_BEGIN(acpi);
1034
1035 /*
1036 * If this is an allocation of the "default" range for a given RID, and
1037 * we know what the resources for this device are (i.e., they're on the
1038 * child's resource list), use those start/end values.
1039 */
1040 if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) {
1041 rle = resource_list_find(rl, type, *rid);
1042 if (rle == NULL)
1043 goto out;
1044 start = rle->start;
1045 end = rle->end;
1046 count = rle->count;
1047 }
1048
1049 /*
1050 * If this is an allocation of a specific range, see if we can satisfy
1051 * the request from our system resource regions. If we can't, pass the
1052 * request up to the parent.
1053 */
1054 if (start + count - 1 == end && rm != NULL)
1055 res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1056 child);
1057 if (res == NULL) {
1058 res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
1059 start, end, count, flags);
1060 } else {
1061 rman_set_rid(res, *rid);
1062
1063 /* If requested, activate the resource using the parent's method. */
1064 if (flags & RF_ACTIVE)
1065 if (bus_activate_resource(child, type, *rid, res) != 0) {
1066 rman_release_resource(res);
1067 res = NULL;
1068 goto out;
1069 }
1070 }
1071
1072 if (res != NULL && device_get_parent(child) == bus)
1073 switch (type) {
1074 case SYS_RES_IRQ:
1075 /*
1076 * Since bus_config_intr() takes immediate effect, we cannot
1077 * configure the interrupt associated with a device when we
1078 * parse the resources but have to defer it until a driver
1079 * actually allocates the interrupt via bus_alloc_resource().
1080 *
1081 * XXX: Should we handle the lookup failing?
1082 */
1083 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
1084 acpi_config_intr(child, &ares);
1085 break;
1086 }
1087
1088 out:
1089 ACPI_SERIAL_END(acpi);
1090 return (res);
1091 }
1092
1093 static int
1094 acpi_release_resource(device_t bus, device_t child, int type, int rid,
1095 struct resource *r)
1096 {
1097 struct rman *rm;
1098 int ret;
1099
1100 /* We only handle memory and IO resources through rman. */
1101 switch (type) {
1102 case SYS_RES_IOPORT:
1103 rm = &acpi_rman_io;
1104 break;
1105 case SYS_RES_MEMORY:
1106 rm = &acpi_rman_mem;
1107 break;
1108 default:
1109 rm = NULL;
1110 }
1111
1112 ACPI_SERIAL_BEGIN(acpi);
1113
1114 /*
1115 * If this resource belongs to one of our internal managers,
1116 * deactivate it and release it to the local pool. If it doesn't,
1117 * pass this request up to the parent.
1118 */
1119 if (rm != NULL && rman_is_region_manager(r, rm)) {
1120 if (rman_get_flags(r) & RF_ACTIVE) {
1121 ret = bus_deactivate_resource(child, type, rid, r);
1122 if (ret != 0)
1123 goto out;
1124 }
1125 ret = rman_release_resource(r);
1126 } else
1127 ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
1128
1129 out:
1130 ACPI_SERIAL_END(acpi);
1131 return (ret);
1132 }
1133
1134 static void
1135 acpi_delete_resource(device_t bus, device_t child, int type, int rid)
1136 {
1137 struct resource_list *rl;
1138
1139 rl = acpi_get_rlist(bus, child);
1140 resource_list_delete(rl, type, rid);
1141 }
1142
1143 /* Allocate an IO port or memory resource, given its GAS. */
1144 int
1145 acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1146 struct resource **res, u_int flags)
1147 {
1148 int error, res_type;
1149
1150 error = ENOMEM;
1151 if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1152 return (EINVAL);
1153
1154 /* We only support memory and IO spaces. */
1155 switch (gas->SpaceId) {
1156 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1157 res_type = SYS_RES_MEMORY;
1158 break;
1159 case ACPI_ADR_SPACE_SYSTEM_IO:
1160 res_type = SYS_RES_IOPORT;
1161 break;
1162 default:
1163 return (EOPNOTSUPP);
1164 }
1165
1166 /*
1167 * If the register width is less than 8, assume the BIOS author means
1168 * it is a bit field and just allocate a byte.
1169 */
1170 if (gas->BitWidth && gas->BitWidth < 8)
1171 gas->BitWidth = 8;
1172
1173 /* Validate the address after we're sure we support the space. */
1174 if (gas->Address == 0 || gas->BitWidth == 0)
1175 return (EINVAL);
1176
1177 bus_set_resource(dev, res_type, *rid, gas->Address,
1178 gas->BitWidth / 8);
1179 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1180 if (*res != NULL) {
1181 *type = res_type;
1182 error = 0;
1183 } else
1184 bus_delete_resource(dev, res_type, *rid);
1185
1186 return (error);
1187 }
1188
1189 /* Probe _HID and _CID for compatible ISA PNP ids. */
1190 static uint32_t
1191 acpi_isa_get_logicalid(device_t dev)
1192 {
1193 ACPI_DEVICE_INFO *devinfo;
1194 ACPI_BUFFER buf;
1195 ACPI_HANDLE h;
1196 ACPI_STATUS error;
1197 u_int32_t pnpid;
1198
1199 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1200
1201 pnpid = 0;
1202 buf.Pointer = NULL;
1203 buf.Length = ACPI_ALLOCATE_BUFFER;
1204
1205 /* Fetch and validate the HID. */
1206 if ((h = acpi_get_handle(dev)) == NULL)
1207 goto out;
1208 error = AcpiGetObjectInfo(h, &buf);
1209 if (ACPI_FAILURE(error))
1210 goto out;
1211 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1212
1213 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
1214 pnpid = PNP_EISAID(devinfo->HardwareId.Value);
1215
1216 out:
1217 if (buf.Pointer != NULL)
1218 AcpiOsFree(buf.Pointer);
1219 return_VALUE (pnpid);
1220 }
1221
1222 static int
1223 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1224 {
1225 ACPI_DEVICE_INFO *devinfo;
1226 ACPI_BUFFER buf;
1227 ACPI_HANDLE h;
1228 ACPI_STATUS error;
1229 uint32_t *pnpid;
1230 int valid, i;
1231
1232 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1233
1234 pnpid = cids;
1235 valid = 0;
1236 buf.Pointer = NULL;
1237 buf.Length = ACPI_ALLOCATE_BUFFER;
1238
1239 /* Fetch and validate the CID */
1240 if ((h = acpi_get_handle(dev)) == NULL)
1241 goto out;
1242 error = AcpiGetObjectInfo(h, &buf);
1243 if (ACPI_FAILURE(error))
1244 goto out;
1245 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1246 if ((devinfo->Valid & ACPI_VALID_CID) == 0)
1247 goto out;
1248
1249 if (devinfo->CompatibilityId.Count < count)
1250 count = devinfo->CompatibilityId.Count;
1251 for (i = 0; i < count; i++) {
1252 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
1253 continue;
1254 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
1255 valid++;
1256 }
1257
1258 out:
1259 if (buf.Pointer != NULL)
1260 AcpiOsFree(buf.Pointer);
1261 return_VALUE (valid);
1262 }
1263
1264 static char *
1265 acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1266 {
1267 ACPI_HANDLE h;
1268 int i;
1269
1270 h = acpi_get_handle(dev);
1271 if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
1272 return (NULL);
1273
1274 /* Try to match one of the array of IDs with a HID or CID. */
1275 for (i = 0; ids[i] != NULL; i++) {
1276 if (acpi_MatchHid(h, ids[i]))
1277 return (ids[i]);
1278 }
1279 return (NULL);
1280 }
1281
1282 static ACPI_STATUS
1283 acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1284 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1285 {
1286 ACPI_HANDLE h;
1287
1288 if (dev == NULL)
1289 h = ACPI_ROOT_OBJECT;
1290 else if ((h = acpi_get_handle(dev)) == NULL)
1291 return (AE_BAD_PARAMETER);
1292 return (AcpiEvaluateObject(h, pathname, parameters, ret));
1293 }
1294
1295 static int
1296 acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
1297 {
1298 struct acpi_softc *sc;
1299 ACPI_HANDLE handle;
1300 ACPI_STATUS status;
1301 char sxd[8];
1302 int error;
1303
1304 sc = device_get_softc(bus);
1305 handle = acpi_get_handle(dev);
1306
1307 /*
1308 * XXX If we find these devices, don't try to power them down.
1309 * The serial and IRDA ports on my T23 hang the system when
1310 * set to D3 and it appears that such legacy devices may
1311 * need special handling in their drivers.
1312 */
1313 if (handle == NULL ||
1314 acpi_MatchHid(handle, "PNP0500") ||
1315 acpi_MatchHid(handle, "PNP0501") ||
1316 acpi_MatchHid(handle, "PNP0502") ||
1317 acpi_MatchHid(handle, "PNP0510") ||
1318 acpi_MatchHid(handle, "PNP0511"))
1319 return (ENXIO);
1320
1321 /*
1322 * Override next state with the value from _SxD, if present. If no
1323 * dstate argument was provided, don't fetch the return value.
1324 */
1325 snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
1326 if (dstate)
1327 status = acpi_GetInteger(handle, sxd, dstate);
1328 else
1329 status = AcpiEvaluateObject(handle, sxd, NULL, NULL);
1330
1331 switch (status) {
1332 case AE_OK:
1333 error = 0;
1334 break;
1335 case AE_NOT_FOUND:
1336 error = ESRCH;
1337 break;
1338 default:
1339 error = ENXIO;
1340 break;
1341 }
1342
1343 return (error);
1344 }
1345
1346 /* Callback arg for our implementation of walking the namespace. */
1347 struct acpi_device_scan_ctx {
1348 acpi_scan_cb_t user_fn;
1349 void *arg;
1350 ACPI_HANDLE parent;
1351 };
1352
1353 static ACPI_STATUS
1354 acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1355 {
1356 struct acpi_device_scan_ctx *ctx;
1357 device_t dev, old_dev;
1358 ACPI_STATUS status;
1359 ACPI_OBJECT_TYPE type;
1360
1361 /*
1362 * Skip this device if we think we'll have trouble with it or it is
1363 * the parent where the scan began.
1364 */
1365 ctx = (struct acpi_device_scan_ctx *)arg;
1366 if (acpi_avoid(h) || h == ctx->parent)
1367 return (AE_OK);
1368
1369 /* If this is not a valid device type (e.g., a method), skip it. */
1370 if (ACPI_FAILURE(AcpiGetType(h, &type)))
1371 return (AE_OK);
1372 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1373 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1374 return (AE_OK);
1375
1376 /*
1377 * Call the user function with the current device. If it is unchanged
1378 * afterwards, return. Otherwise, we update the handle to the new dev.
1379 */
1380 old_dev = acpi_get_device(h);
1381 dev = old_dev;
1382 status = ctx->user_fn(h, &dev, level, ctx->arg);
1383 if (ACPI_FAILURE(status) || old_dev == dev)
1384 return (status);
1385
1386 /* Remove the old child and its connection to the handle. */
1387 if (old_dev != NULL) {
1388 device_delete_child(device_get_parent(old_dev), old_dev);
1389 AcpiDetachData(h, acpi_fake_objhandler);
1390 }
1391
1392 /* Recreate the handle association if the user created a device. */
1393 if (dev != NULL)
1394 AcpiAttachData(h, acpi_fake_objhandler, dev);
1395
1396 return (AE_OK);
1397 }
1398
1399 static ACPI_STATUS
1400 acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1401 acpi_scan_cb_t user_fn, void *arg)
1402 {
1403 ACPI_HANDLE h;
1404 struct acpi_device_scan_ctx ctx;
1405
1406 if (acpi_disabled("children"))
1407 return (AE_OK);
1408
1409 if (dev == NULL)
1410 h = ACPI_ROOT_OBJECT;
1411 else if ((h = acpi_get_handle(dev)) == NULL)
1412 return (AE_BAD_PARAMETER);
1413 ctx.user_fn = user_fn;
1414 ctx.arg = arg;
1415 ctx.parent = h;
1416 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1417 acpi_device_scan_cb, &ctx, NULL));
1418 }
1419
1420 /*
1421 * Even though ACPI devices are not PCI, we use the PCI approach for setting
1422 * device power states since it's close enough to ACPI.
1423 */
1424 static int
1425 acpi_set_powerstate_method(device_t bus, device_t child, int state)
1426 {
1427 ACPI_HANDLE h;
1428 ACPI_STATUS status;
1429 int error;
1430
1431 error = 0;
1432 h = acpi_get_handle(child);
1433 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
1434 return (EINVAL);
1435 if (h == NULL)
1436 return (0);
1437
1438 /* Ignore errors if the power methods aren't present. */
1439 status = acpi_pwr_switch_consumer(h, state);
1440 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND
1441 && status != AE_BAD_PARAMETER)
1442 device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n",
1443 state, acpi_name(h), AcpiFormatException(status));
1444
1445 return (error);
1446 }
1447
1448 static int
1449 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1450 {
1451 int result, cid_count, i;
1452 uint32_t lid, cids[8];
1453
1454 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1455
1456 /*
1457 * ISA-style drivers attached to ACPI may persist and
1458 * probe manually if we return ENOENT. We never want
1459 * that to happen, so don't ever return it.
1460 */
1461 result = ENXIO;
1462
1463 /* Scan the supplied IDs for a match */
1464 lid = acpi_isa_get_logicalid(child);
1465 cid_count = acpi_isa_get_compatid(child, cids, 8);
1466 while (ids && ids->ip_id) {
1467 if (lid == ids->ip_id) {
1468 result = 0;
1469 goto out;
1470 }
1471 for (i = 0; i < cid_count; i++) {
1472 if (cids[i] == ids->ip_id) {
1473 result = 0;
1474 goto out;
1475 }
1476 }
1477 ids++;
1478 }
1479
1480 out:
1481 if (result == 0 && ids->ip_desc)
1482 device_set_desc(child, ids->ip_desc);
1483
1484 return_VALUE (result);
1485 }
1486
1487 #if defined(__i386__) || defined(__amd64__)
1488 /*
1489 * Look for a MCFG table. If it is present, use the settings for
1490 * domain (segment) 0 to setup PCI config space access via the memory
1491 * map.
1492 */
1493 static void
1494 acpi_enable_pcie(void)
1495 {
1496 ACPI_TABLE_HEADER *hdr;
1497 ACPI_MCFG_ALLOCATION *alloc, *end;
1498 ACPI_STATUS status;
1499
1500 status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
1501 if (ACPI_FAILURE(status))
1502 return;
1503
1504 end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
1505 alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
1506 while (alloc < end) {
1507 if (alloc->PciSegment == 0) {
1508 pcie_cfgregopen(alloc->Address, alloc->StartBusNumber,
1509 alloc->EndBusNumber);
1510 return;
1511 }
1512 alloc++;
1513 }
1514 }
1515 #endif
1516
1517 /*
1518 * Scan all of the ACPI namespace and attach child devices.
1519 *
1520 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
1521 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
1522 * However, in violation of the spec, some systems place their PCI link
1523 * devices in \, so we have to walk the whole namespace. We check the
1524 * type of namespace nodes, so this should be ok.
1525 */
1526 static void
1527 acpi_probe_children(device_t bus)
1528 {
1529
1530 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1531
1532 /*
1533 * Scan the namespace and insert placeholders for all the devices that
1534 * we find. We also probe/attach any early devices.
1535 *
1536 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1537 * we want to create nodes for all devices, not just those that are
1538 * currently present. (This assumes that we don't want to create/remove
1539 * devices as they appear, which might be smarter.)
1540 */
1541 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1542 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
1543 bus, NULL);
1544
1545 /* Pre-allocate resources for our rman from any sysresource devices. */
1546 acpi_sysres_alloc(bus);
1547
1548 /* Create any static children by calling device identify methods. */
1549 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1550 bus_generic_probe(bus);
1551
1552 /* Probe/attach all children, created staticly and from the namespace. */
1553 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
1554 bus_generic_attach(bus);
1555
1556 /*
1557 * Some of these children may have attached others as part of their attach
1558 * process (eg. the root PCI bus driver), so rescan.
1559 */
1560 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
1561 bus_generic_attach(bus);
1562
1563 /* Attach wake sysctls. */
1564 acpi_wake_sysctl_walk(bus);
1565
1566 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1567 return_VOID;
1568 }
1569
1570 /*
1571 * Determine the probe order for a given device.
1572 */
1573 static void
1574 acpi_probe_order(ACPI_HANDLE handle, int *order)
1575 {
1576 ACPI_OBJECT_TYPE type;
1577
1578 /*
1579 * 1. I/O port and memory system resource holders
1580 * 2. Embedded controllers (to handle early accesses)
1581 * 3. PCI Link Devices
1582 * 100000. CPUs
1583 */
1584 AcpiGetType(handle, &type);
1585 if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02"))
1586 *order = 1;
1587 else if (acpi_MatchHid(handle, "PNP0C09"))
1588 *order = 2;
1589 else if (acpi_MatchHid(handle, "PNP0C0F"))
1590 *order = 3;
1591 else if (type == ACPI_TYPE_PROCESSOR)
1592 *order = 100000;
1593 }
1594
1595 /*
1596 * Evaluate a child device and determine whether we might attach a device to
1597 * it.
1598 */
1599 static ACPI_STATUS
1600 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1601 {
1602 ACPI_OBJECT_TYPE type;
1603 ACPI_HANDLE h;
1604 device_t bus, child;
1605 int order;
1606 char *handle_str, **search;
1607 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL};
1608
1609 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1610
1611 /* Skip this device if we think we'll have trouble with it. */
1612 if (acpi_avoid(handle))
1613 return_ACPI_STATUS (AE_OK);
1614
1615 bus = (device_t)context;
1616 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1617 switch (type) {
1618 case ACPI_TYPE_DEVICE:
1619 case ACPI_TYPE_PROCESSOR:
1620 case ACPI_TYPE_THERMAL:
1621 case ACPI_TYPE_POWER:
1622 if (acpi_disabled("children"))
1623 break;
1624
1625 /*
1626 * Since we scan from \, be sure to skip system scope objects.
1627 * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?)
1628 */
1629 handle_str = acpi_name(handle);
1630 for (search = scopes; *search != NULL; search++) {
1631 if (strcmp(handle_str, *search) == 0)
1632 break;
1633 }
1634 if (*search != NULL)
1635 break;
1636
1637 /*
1638 * Create a placeholder device for this node. Sort the
1639 * placeholder so that the probe/attach passes will run
1640 * breadth-first. Orders less than ACPI_DEV_BASE_ORDER
1641 * are reserved for special objects (i.e., system
1642 * resources). CPU devices have a very high order to
1643 * ensure they are probed after other devices.
1644 */
1645 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1646 order = level * 10 + 100;
1647 acpi_probe_order(handle, &order);
1648 child = BUS_ADD_CHILD(bus, order, NULL, -1);
1649 if (child == NULL)
1650 break;
1651
1652 /* Associate the handle with the device_t and vice versa. */
1653 acpi_set_handle(child, handle);
1654 AcpiAttachData(handle, acpi_fake_objhandler, child);
1655
1656 /*
1657 * Check that the device is present. If it's not present,
1658 * leave it disabled (so that we have a device_t attached to
1659 * the handle, but we don't probe it).
1660 *
1661 * XXX PCI link devices sometimes report "present" but not
1662 * "functional" (i.e. if disabled). Go ahead and probe them
1663 * anyway since we may enable them later.
1664 */
1665 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1666 /* Never disable PCI link devices. */
1667 if (acpi_MatchHid(handle, "PNP0C0F"))
1668 break;
1669 /*
1670 * Docking stations should remain enabled since the system
1671 * may be undocked at boot.
1672 */
1673 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
1674 break;
1675
1676 device_disable(child);
1677 break;
1678 }
1679
1680 /*
1681 * Get the device's resource settings and attach them.
1682 * Note that if the device has _PRS but no _CRS, we need
1683 * to decide when it's appropriate to try to configure the
1684 * device. Ignore the return value here; it's OK for the
1685 * device not to have any resources.
1686 */
1687 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1688 break;
1689 }
1690 }
1691
1692 return_ACPI_STATUS (AE_OK);
1693 }
1694
1695 /*
1696 * AcpiAttachData() requires an object handler but never uses it. This is a
1697 * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
1698 */
1699 void
1700 acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
1701 {
1702 }
1703
1704 static void
1705 acpi_shutdown_final(void *arg, int howto)
1706 {
1707 struct acpi_softc *sc;
1708 ACPI_STATUS status;
1709
1710 /*
1711 * XXX Shutdown code should only run on the BSP (cpuid 0).
1712 * Some chipsets do not power off the system correctly if called from
1713 * an AP.
1714 */
1715 sc = arg;
1716 if ((howto & RB_POWEROFF) != 0) {
1717 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1718 if (ACPI_FAILURE(status)) {
1719 printf("AcpiEnterSleepStatePrep failed - %s\n",
1720 AcpiFormatException(status));
1721 return;
1722 }
1723 printf("Powering system off using ACPI\n");
1724 ACPI_DISABLE_IRQS();
1725 status = AcpiEnterSleepState(ACPI_STATE_S5);
1726 if (ACPI_FAILURE(status)) {
1727 printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
1728 } else {
1729 DELAY(1000000);
1730 printf("ACPI power-off failed - timeout\n");
1731 }
1732 } else if ((howto & RB_HALT) == 0 &&
1733 (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) &&
1734 sc->acpi_handle_reboot) {
1735 /* Reboot using the reset register. */
1736 status = AcpiHwLowLevelWrite(
1737 AcpiGbl_FADT.ResetRegister.BitWidth,
1738 AcpiGbl_FADT.ResetValue, &AcpiGbl_FADT.ResetRegister);
1739 if (ACPI_FAILURE(status)) {
1740 printf("ACPI reset failed - %s\n", AcpiFormatException(status));
1741 } else {
1742 DELAY(1000000);
1743 printf("ACPI reset failed - timeout\n");
1744 }
1745 } else if (sc->acpi_do_disable && panicstr == NULL) {
1746 /*
1747 * Only disable ACPI if the user requested. On some systems, writing
1748 * the disable value to SMI_CMD hangs the system.
1749 */
1750 printf("Shutting down ACPI\n");
1751 AcpiTerminate();
1752 }
1753 }
1754
1755 static void
1756 acpi_enable_fixed_events(struct acpi_softc *sc)
1757 {
1758 static int first_time = 1;
1759
1760 /* Enable and clear fixed events and install handlers. */
1761 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
1762 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1763 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1764 acpi_event_power_button_sleep, sc);
1765 if (first_time)
1766 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
1767 }
1768 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1769 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1770 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1771 acpi_event_sleep_button_sleep, sc);
1772 if (first_time)
1773 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
1774 }
1775
1776 first_time = 0;
1777 }
1778
1779 /*
1780 * Returns true if the device is actually present and should
1781 * be attached to. This requires the present, enabled, UI-visible
1782 * and diagnostics-passed bits to be set.
1783 */
1784 BOOLEAN
1785 acpi_DeviceIsPresent(device_t dev)
1786 {
1787 ACPI_DEVICE_INFO *devinfo;
1788 ACPI_HANDLE h;
1789 ACPI_BUFFER buf;
1790 ACPI_STATUS error;
1791 int ret;
1792
1793 ret = FALSE;
1794 if ((h = acpi_get_handle(dev)) == NULL)
1795 return (FALSE);
1796 buf.Pointer = NULL;
1797 buf.Length = ACPI_ALLOCATE_BUFFER;
1798 error = AcpiGetObjectInfo(h, &buf);
1799 if (ACPI_FAILURE(error))
1800 return (FALSE);
1801 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1802
1803 /* If no _STA method, must be present */
1804 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1805 ret = TRUE;
1806
1807 /* Return true for 'present' and 'functioning' */
1808 if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
1809 ret = TRUE;
1810
1811 AcpiOsFree(buf.Pointer);
1812 return (ret);
1813 }
1814
1815 /*
1816 * Returns true if the battery is actually present and inserted.
1817 */
1818 BOOLEAN
1819 acpi_BatteryIsPresent(device_t dev)
1820 {
1821 ACPI_DEVICE_INFO *devinfo;
1822 ACPI_HANDLE h;
1823 ACPI_BUFFER buf;
1824 ACPI_STATUS error;
1825 int ret;
1826
1827 ret = FALSE;
1828 if ((h = acpi_get_handle(dev)) == NULL)
1829 return (FALSE);
1830 buf.Pointer = NULL;
1831 buf.Length = ACPI_ALLOCATE_BUFFER;
1832 error = AcpiGetObjectInfo(h, &buf);
1833 if (ACPI_FAILURE(error))
1834 return (FALSE);
1835 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1836
1837 /* If no _STA method, must be present */
1838 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1839 ret = TRUE;
1840
1841 /* Return true for 'present', 'battery present', and 'functioning' */
1842 if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
1843 ret = TRUE;
1844
1845 AcpiOsFree(buf.Pointer);
1846 return (ret);
1847 }
1848
1849 /*
1850 * Match a HID string against a handle
1851 */
1852 static BOOLEAN
1853 acpi_MatchHid(ACPI_HANDLE h, const char *hid)
1854 {
1855 ACPI_DEVICE_INFO *devinfo;
1856 ACPI_BUFFER buf;
1857 ACPI_STATUS error;
1858 int ret, i;
1859
1860 ret = FALSE;
1861 if (hid == NULL || h == NULL)
1862 return (ret);
1863 buf.Pointer = NULL;
1864 buf.Length = ACPI_ALLOCATE_BUFFER;
1865 error = AcpiGetObjectInfo(h, &buf);
1866 if (ACPI_FAILURE(error))
1867 return (ret);
1868 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1869
1870 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1871 strcmp(hid, devinfo->HardwareId.Value) == 0)
1872 ret = TRUE;
1873 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
1874 for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
1875 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
1876 ret = TRUE;
1877 break;
1878 }
1879 }
1880 }
1881
1882 AcpiOsFree(buf.Pointer);
1883 return (ret);
1884 }
1885
1886 /*
1887 * Return the handle of a named object within our scope, ie. that of (parent)
1888 * or one if its parents.
1889 */
1890 ACPI_STATUS
1891 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1892 {
1893 ACPI_HANDLE r;
1894 ACPI_STATUS status;
1895
1896 /* Walk back up the tree to the root */
1897 for (;;) {
1898 status = AcpiGetHandle(parent, path, &r);
1899 if (ACPI_SUCCESS(status)) {
1900 *result = r;
1901 return (AE_OK);
1902 }
1903 /* XXX Return error here? */
1904 if (status != AE_NOT_FOUND)
1905 return (AE_OK);
1906 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1907 return (AE_NOT_FOUND);
1908 parent = r;
1909 }
1910 }
1911
1912 /* Find the difference between two PM tick counts. */
1913 uint32_t
1914 acpi_TimerDelta(uint32_t end, uint32_t start)
1915 {
1916 uint32_t delta;
1917
1918 if (end >= start)
1919 delta = end - start;
1920 else if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
1921 delta = ((0xFFFFFFFF - start) + end + 1);
1922 else
1923 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1924 return (delta);
1925 }
1926
1927 /*
1928 * Allocate a buffer with a preset data size.
1929 */
1930 ACPI_BUFFER *
1931 acpi_AllocBuffer(int size)
1932 {
1933 ACPI_BUFFER *buf;
1934
1935 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1936 return (NULL);
1937 buf->Length = size;
1938 buf->Pointer = (void *)(buf + 1);
1939 return (buf);
1940 }
1941
1942 ACPI_STATUS
1943 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1944 {
1945 ACPI_OBJECT arg1;
1946 ACPI_OBJECT_LIST args;
1947
1948 arg1.Type = ACPI_TYPE_INTEGER;
1949 arg1.Integer.Value = number;
1950 args.Count = 1;
1951 args.Pointer = &arg1;
1952
1953 return (AcpiEvaluateObject(handle, path, &args, NULL));
1954 }
1955
1956 /*
1957 * Evaluate a path that should return an integer.
1958 */
1959 ACPI_STATUS
1960 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1961 {
1962 ACPI_STATUS status;
1963 ACPI_BUFFER buf;
1964 ACPI_OBJECT param;
1965
1966 if (handle == NULL)
1967 handle = ACPI_ROOT_OBJECT;
1968
1969 /*
1970 * Assume that what we've been pointed at is an Integer object, or
1971 * a method that will return an Integer.
1972 */
1973 buf.Pointer = ¶m;
1974 buf.Length = sizeof(param);
1975 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1976 if (ACPI_SUCCESS(status)) {
1977 if (param.Type == ACPI_TYPE_INTEGER)
1978 *number = param.Integer.Value;
1979 else
1980 status = AE_TYPE;
1981 }
1982
1983 /*
1984 * In some applications, a method that's expected to return an Integer
1985 * may instead return a Buffer (probably to simplify some internal
1986 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1987 * convert it into an Integer as best we can.
1988 *
1989 * This is a hack.
1990 */
1991 if (status == AE_BUFFER_OVERFLOW) {
1992 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1993 status = AE_NO_MEMORY;
1994 } else {
1995 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1996 if (ACPI_SUCCESS(status))
1997 status = acpi_ConvertBufferToInteger(&buf, number);
1998 AcpiOsFree(buf.Pointer);
1999 }
2000 }
2001 return (status);
2002 }
2003
2004 ACPI_STATUS
2005 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2006 {
2007 ACPI_OBJECT *p;
2008 UINT8 *val;
2009 int i;
2010
2011 p = (ACPI_OBJECT *)bufp->Pointer;
2012 if (p->Type == ACPI_TYPE_INTEGER) {
2013 *number = p->Integer.Value;
2014 return (AE_OK);
2015 }
2016 if (p->Type != ACPI_TYPE_BUFFER)
2017 return (AE_TYPE);
2018 if (p->Buffer.Length > sizeof(int))
2019 return (AE_BAD_DATA);
2020
2021 *number = 0;
2022 val = p->Buffer.Pointer;
2023 for (i = 0; i < p->Buffer.Length; i++)
2024 *number += val[i] << (i * 8);
2025 return (AE_OK);
2026 }
2027
2028 /*
2029 * Iterate over the elements of an a package object, calling the supplied
2030 * function for each element.
2031 *
2032 * XXX possible enhancement might be to abort traversal on error.
2033 */
2034 ACPI_STATUS
2035 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2036 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2037 {
2038 ACPI_OBJECT *comp;
2039 int i;
2040
2041 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2042 return (AE_BAD_PARAMETER);
2043
2044 /* Iterate over components */
2045 i = 0;
2046 comp = pkg->Package.Elements;
2047 for (; i < pkg->Package.Count; i++, comp++)
2048 func(comp, arg);
2049
2050 return (AE_OK);
2051 }
2052
2053 /*
2054 * Find the (index)th resource object in a set.
2055 */
2056 ACPI_STATUS
2057 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
2058 {
2059 ACPI_RESOURCE *rp;
2060 int i;
2061
2062 rp = (ACPI_RESOURCE *)buf->Pointer;
2063 i = index;
2064 while (i-- > 0) {
2065 /* Range check */
2066 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2067 return (AE_BAD_PARAMETER);
2068
2069 /* Check for terminator */
2070 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2071 return (AE_NOT_FOUND);
2072 rp = ACPI_NEXT_RESOURCE(rp);
2073 }
2074 if (resp != NULL)
2075 *resp = rp;
2076
2077 return (AE_OK);
2078 }
2079
2080 /*
2081 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
2082 *
2083 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
2084 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
2085 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
2086 * resources.
2087 */
2088 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
2089
2090 ACPI_STATUS
2091 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
2092 {
2093 ACPI_RESOURCE *rp;
2094 void *newp;
2095
2096 /* Initialise the buffer if necessary. */
2097 if (buf->Pointer == NULL) {
2098 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
2099 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
2100 return (AE_NO_MEMORY);
2101 rp = (ACPI_RESOURCE *)buf->Pointer;
2102 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
2103 rp->Length = 0;
2104 }
2105 if (res == NULL)
2106 return (AE_OK);
2107
2108 /*
2109 * Scan the current buffer looking for the terminator.
2110 * This will either find the terminator or hit the end
2111 * of the buffer and return an error.
2112 */
2113 rp = (ACPI_RESOURCE *)buf->Pointer;
2114 for (;;) {
2115 /* Range check, don't go outside the buffer */
2116 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2117 return (AE_BAD_PARAMETER);
2118 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2119 break;
2120 rp = ACPI_NEXT_RESOURCE(rp);
2121 }
2122
2123 /*
2124 * Check the size of the buffer and expand if required.
2125 *
2126 * Required size is:
2127 * size of existing resources before terminator +
2128 * size of new resource and header +
2129 * size of terminator.
2130 *
2131 * Note that this loop should really only run once, unless
2132 * for some reason we are stuffing a *really* huge resource.
2133 */
2134 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2135 res->Length + ACPI_RS_SIZE_NO_DATA +
2136 ACPI_RS_SIZE_MIN) >= buf->Length) {
2137 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
2138 return (AE_NO_MEMORY);
2139 bcopy(buf->Pointer, newp, buf->Length);
2140 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2141 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
2142 AcpiOsFree(buf->Pointer);
2143 buf->Pointer = newp;
2144 buf->Length += buf->Length;
2145 }
2146
2147 /* Insert the new resource. */
2148 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
2149
2150 /* And add the terminator. */
2151 rp = ACPI_NEXT_RESOURCE(rp);
2152 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
2153 rp->Length = 0;
2154
2155 return (AE_OK);
2156 }
2157
2158 /*
2159 * Set interrupt model.
2160 */
2161 ACPI_STATUS
2162 acpi_SetIntrModel(int model)
2163 {
2164
2165 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2166 }
2167
2168 /*
2169 * Walk subtables of a table and call a callback routine for each
2170 * subtable. The caller should provide the first subtable and a
2171 * pointer to the end of the table. This can be used to walk tables
2172 * such as MADT and SRAT that use subtable entries.
2173 */
2174 void
2175 acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
2176 void *arg)
2177 {
2178 ACPI_SUBTABLE_HEADER *entry;
2179
2180 for (entry = first; (void *)entry < end; ) {
2181 /* Avoid an infinite loop if we hit a bogus entry. */
2182 if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
2183 return;
2184
2185 handler(entry, arg);
2186 entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
2187 }
2188 }
2189
2190 /*
2191 * DEPRECATED. This interface has serious deficiencies and will be
2192 * removed.
2193 *
2194 * Immediately enter the sleep state. In the old model, acpiconf(8) ran
2195 * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
2196 */
2197 ACPI_STATUS
2198 acpi_SetSleepState(struct acpi_softc *sc, int state)
2199 {
2200 static int once;
2201
2202 if (!once) {
2203 printf(
2204 "warning: acpi_SetSleepState() deprecated, need to update your software\n");
2205 once = 1;
2206 }
2207 return (acpi_EnterSleepState(sc, state));
2208 }
2209
2210 static void
2211 acpi_sleep_force(void *arg)
2212 {
2213 struct acpi_softc *sc;
2214
2215 printf("acpi: suspend request timed out, forcing sleep now\n");
2216 sc = arg;
2217 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2218 printf("acpi: force sleep state S%d failed\n", sc->acpi_next_sstate);
2219 }
2220
2221 /*
2222 * Request that the system enter the given suspend state. All /dev/apm
2223 * devices and devd(8) will be notified. Userland then has a chance to
2224 * save state and acknowledge the request. The system sleeps once all
2225 * acks are in.
2226 */
2227 int
2228 acpi_ReqSleepState(struct acpi_softc *sc, int state)
2229 {
2230 struct apm_clone_data *clone;
2231
2232 if (state < ACPI_STATE_S1 || state > ACPI_STATE_S5)
2233 return (EINVAL);
2234
2235 /* S5 (soft-off) should be entered directly with no waiting. */
2236 if (state == ACPI_STATE_S5) {
2237 if (ACPI_SUCCESS(acpi_EnterSleepState(sc, state)))
2238 return (0);
2239 else
2240 return (ENXIO);
2241 }
2242
2243 #if !defined(__i386__)
2244 /* This platform does not support acpi suspend/resume. */
2245 return (EOPNOTSUPP);
2246 #endif
2247
2248 /* If a suspend request is already in progress, just return. */
2249 ACPI_LOCK(acpi);
2250 if (sc->acpi_next_sstate != 0) {
2251 ACPI_UNLOCK(acpi);
2252 return (0);
2253 }
2254
2255 /* Record the pending state and notify all apm devices. */
2256 sc->acpi_next_sstate = state;
2257 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
2258 clone->notify_status = APM_EV_NONE;
2259 if ((clone->flags & ACPI_EVF_DEVD) == 0) {
2260 selwakeuppri(&clone->sel_read, PZERO);
2261 KNOTE_UNLOCKED(&clone->sel_read.si_note, 0);
2262 }
2263 }
2264
2265 /* If devd(8) is not running, immediately enter the sleep state. */
2266 if (devctl_process_running() == FALSE) {
2267 ACPI_UNLOCK(acpi);
2268 if (ACPI_SUCCESS(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) {
2269 return (0);
2270 } else {
2271 return (ENXIO);
2272 }
2273 }
2274
2275 /* Now notify devd(8) also. */
2276 acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
2277
2278 /*
2279 * Set a timeout to fire if userland doesn't ack the suspend request
2280 * in time. This way we still eventually go to sleep if we were
2281 * overheating or running low on battery, even if userland is hung.
2282 * We cancel this timeout once all userland acks are in or the
2283 * suspend request is aborted.
2284 */
2285 callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
2286 ACPI_UNLOCK(acpi);
2287 return (0);
2288 }
2289
2290 /*
2291 * Acknowledge (or reject) a pending sleep state. The caller has
2292 * prepared for suspend and is now ready for it to proceed. If the
2293 * error argument is non-zero, it indicates suspend should be cancelled
2294 * and gives an errno value describing why. Once all votes are in,
2295 * we suspend the system.
2296 */
2297 int
2298 acpi_AckSleepState(struct apm_clone_data *clone, int error)
2299 {
2300 struct acpi_softc *sc;
2301 int ret, sleeping;
2302
2303 #if !defined(__i386__)
2304 /* This platform does not support acpi suspend/resume. */
2305 return (EOPNOTSUPP);
2306 #endif
2307
2308 /* If no pending sleep state, return an error. */
2309 ACPI_LOCK(acpi);
2310 sc = clone->acpi_sc;
2311 if (sc->acpi_next_sstate == 0) {
2312 ACPI_UNLOCK(acpi);
2313 return (ENXIO);
2314 }
2315
2316 /* Caller wants to abort suspend process. */
2317 if (error) {
2318 sc->acpi_next_sstate = 0;
2319 callout_stop(&sc->susp_force_to);
2320 printf("acpi: listener on %s cancelled the pending suspend\n",
2321 devtoname(clone->cdev));
2322 ACPI_UNLOCK(acpi);
2323 return (0);
2324 }
2325
2326 /*
2327 * Mark this device as acking the suspend request. Then, walk through
2328 * all devices, seeing if they agree yet. We only count devices that
2329 * are writable since read-only devices couldn't ack the request.
2330 */
2331 clone->notify_status = APM_EV_ACKED;
2332 sleeping = TRUE;
2333 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
2334 if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
2335 clone->notify_status != APM_EV_ACKED) {
2336 sleeping = FALSE;
2337 break;
2338 }
2339 }
2340
2341 /* If all devices have voted "yes", we will suspend now. */
2342 if (sleeping)
2343 callout_stop(&sc->susp_force_to);
2344 ACPI_UNLOCK(acpi);
2345 ret = 0;
2346 if (sleeping) {
2347 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2348 ret = ENODEV;
2349 }
2350
2351 return (ret);
2352 }
2353
2354 static void
2355 acpi_sleep_enable(void *arg)
2356 {
2357
2358 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
2359 }
2360
2361 enum acpi_sleep_state {
2362 ACPI_SS_NONE,
2363 ACPI_SS_GPE_SET,
2364 ACPI_SS_DEV_SUSPEND,
2365 ACPI_SS_SLP_PREP,
2366 ACPI_SS_SLEPT,
2367 };
2368
2369 /*
2370 * Enter the desired system sleep state.
2371 *
2372 * Currently we support S1-S5 but S4 is only S4BIOS
2373 */
2374 static ACPI_STATUS
2375 acpi_EnterSleepState(struct acpi_softc *sc, int state)
2376 {
2377 ACPI_STATUS status;
2378 UINT8 TypeA;
2379 UINT8 TypeB;
2380 enum acpi_sleep_state slp_state;
2381
2382 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2383
2384 /* Re-entry once we're suspending is not allowed. */
2385 status = AE_OK;
2386 ACPI_LOCK(acpi);
2387 if (sc->acpi_sleep_disabled) {
2388 ACPI_UNLOCK(acpi);
2389 printf("acpi: suspend request ignored (not ready yet)\n");
2390 return (AE_ERROR);
2391 }
2392 sc->acpi_sleep_disabled = 1;
2393 ACPI_UNLOCK(acpi);
2394
2395 /*
2396 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
2397 * drivers need this.
2398 */
2399 mtx_lock(&Giant);
2400 slp_state = ACPI_SS_NONE;
2401 switch (state) {
2402 case ACPI_STATE_S1:
2403 case ACPI_STATE_S2:
2404 case ACPI_STATE_S3:
2405 case ACPI_STATE_S4:
2406 status = AcpiGetSleepTypeData(state, &TypeA, &TypeB);
2407 if (status == AE_NOT_FOUND) {
2408 device_printf(sc->acpi_dev,
2409 "Sleep state S%d not supported by BIOS\n", state);
2410 break;
2411 } else if (ACPI_FAILURE(status)) {
2412 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
2413 AcpiFormatException(status));
2414 break;
2415 }
2416
2417 sc->acpi_sstate = state;
2418
2419 /* Enable any GPEs as appropriate and requested by the user. */
2420 acpi_wake_prep_walk(state);
2421 slp_state = ACPI_SS_GPE_SET;
2422
2423 /*
2424 * Inform all devices that we are going to sleep. If at least one
2425 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
2426 *
2427 * XXX Note that a better two-pass approach with a 'veto' pass
2428 * followed by a "real thing" pass would be better, but the current
2429 * bus interface does not provide for this.
2430 */
2431 if (DEVICE_SUSPEND(root_bus) != 0) {
2432 device_printf(sc->acpi_dev, "device_suspend failed\n");
2433 break;
2434 }
2435 slp_state = ACPI_SS_DEV_SUSPEND;
2436
2437 /* If testing device suspend only, back out of everything here. */
2438 if (acpi_susp_bounce)
2439 break;
2440
2441 status = AcpiEnterSleepStatePrep(state);
2442 if (ACPI_FAILURE(status)) {
2443 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2444 AcpiFormatException(status));
2445 break;
2446 }
2447 slp_state = ACPI_SS_SLP_PREP;
2448
2449 if (sc->acpi_sleep_delay > 0)
2450 DELAY(sc->acpi_sleep_delay * 1000000);
2451
2452 if (state != ACPI_STATE_S1) {
2453 acpi_sleep_machdep(sc, state);
2454
2455 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
2456 if (state == ACPI_STATE_S4)
2457 AcpiEnable();
2458 } else {
2459 ACPI_DISABLE_IRQS();
2460 status = AcpiEnterSleepState(state);
2461 if (ACPI_FAILURE(status)) {
2462 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2463 AcpiFormatException(status));
2464 break;
2465 }
2466 }
2467 slp_state = ACPI_SS_SLEPT;
2468 break;
2469 case ACPI_STATE_S5:
2470 /*
2471 * Shut down cleanly and power off. This will call us back through the
2472 * shutdown handlers.
2473 */
2474 shutdown_nice(RB_POWEROFF);
2475 break;
2476 case ACPI_STATE_S0:
2477 default:
2478 status = AE_BAD_PARAMETER;
2479 break;
2480 }
2481
2482 /*
2483 * Back out state according to how far along we got in the suspend
2484 * process. This handles both the error and success cases.
2485 */
2486 sc->acpi_next_sstate = 0;
2487 if (slp_state >= ACPI_SS_GPE_SET) {
2488 acpi_wake_prep_walk(state);
2489 sc->acpi_sstate = ACPI_STATE_S0;
2490 }
2491 if (slp_state >= ACPI_SS_SLP_PREP)
2492 AcpiLeaveSleepState(state);
2493 if (slp_state >= ACPI_SS_DEV_SUSPEND)
2494 DEVICE_RESUME(root_bus);
2495 if (slp_state >= ACPI_SS_SLEPT)
2496 acpi_enable_fixed_events(sc);
2497
2498 /* Allow another sleep request after a while. */
2499 if (state != ACPI_STATE_S5)
2500 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
2501
2502 /* Run /etc/rc.resume after we are back. */
2503 acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
2504
2505 mtx_unlock(&Giant);
2506 return_ACPI_STATUS (status);
2507 }
2508
2509 /* Initialize a device's wake GPE. */
2510 int
2511 acpi_wake_init(device_t dev, int type)
2512 {
2513 struct acpi_prw_data prw;
2514
2515 /* Evaluate _PRW to find the GPE. */
2516 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2517 return (ENXIO);
2518
2519 /* Set the requested type for the GPE (runtime, wake, or both). */
2520 if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
2521 device_printf(dev, "set GPE type failed\n");
2522 return (ENXIO);
2523 }
2524
2525 return (0);
2526 }
2527
2528 /* Enable or disable the device's wake GPE. */
2529 int
2530 acpi_wake_set_enable(device_t dev, int enable)
2531 {
2532 struct acpi_prw_data prw;
2533 ACPI_STATUS status;
2534 int flags;
2535
2536 /* Make sure the device supports waking the system and get the GPE. */
2537 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2538 return (ENXIO);
2539
2540 flags = acpi_get_flags(dev);
2541 if (enable) {
2542 status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2543 if (ACPI_FAILURE(status)) {
2544 device_printf(dev, "enable wake failed\n");
2545 return (ENXIO);
2546 }
2547 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2548 } else {
2549 status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2550 if (ACPI_FAILURE(status)) {
2551 device_printf(dev, "disable wake failed\n");
2552 return (ENXIO);
2553 }
2554 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2555 }
2556
2557 return (0);
2558 }
2559
2560 static int
2561 acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2562 {
2563 struct acpi_prw_data prw;
2564 device_t dev;
2565
2566 /* Check that this is a wake-capable device and get its GPE. */
2567 if (acpi_parse_prw(handle, &prw) != 0)
2568 return (ENXIO);
2569 dev = acpi_get_device(handle);
2570
2571 /*
2572 * The destination sleep state must be less than (i.e., higher power)
2573 * or equal to the value specified by _PRW. If this GPE cannot be
2574 * enabled for the next sleep state, then disable it. If it can and
2575 * the user requested it be enabled, turn on any required power resources
2576 * and set _PSW.
2577 */
2578 if (sstate > prw.lowest_wake) {
2579 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2580 if (bootverbose)
2581 device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2582 acpi_name(handle), sstate);
2583 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2584 acpi_pwr_wake_enable(handle, 1);
2585 acpi_SetInteger(handle, "_PSW", 1);
2586 if (bootverbose)
2587 device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2588 acpi_name(handle), sstate);
2589 }
2590
2591 return (0);
2592 }
2593
2594 static int
2595 acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2596 {
2597 struct acpi_prw_data prw;
2598 device_t dev;
2599
2600 /*
2601 * Check that this is a wake-capable device and get its GPE. Return
2602 * now if the user didn't enable this device for wake.
2603 */
2604 if (acpi_parse_prw(handle, &prw) != 0)
2605 return (ENXIO);
2606 dev = acpi_get_device(handle);
2607 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2608 return (0);
2609
2610 /*
2611 * If this GPE couldn't be enabled for the previous sleep state, it was
2612 * disabled before going to sleep so re-enable it. If it was enabled,
2613 * clear _PSW and turn off any power resources it used.
2614 */
2615 if (sstate > prw.lowest_wake) {
2616 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2617 if (bootverbose)
2618 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2619 } else {
2620 acpi_SetInteger(handle, "_PSW", 0);
2621 acpi_pwr_wake_enable(handle, 0);
2622 if (bootverbose)
2623 device_printf(dev, "run_prep cleaned up for %s\n",
2624 acpi_name(handle));
2625 }
2626
2627 return (0);
2628 }
2629
2630 static ACPI_STATUS
2631 acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2632 {
2633 int sstate;
2634
2635 /* If suspending, run the sleep prep function, otherwise wake. */
2636 sstate = *(int *)context;
2637 if (AcpiGbl_SystemAwakeAndRunning)
2638 acpi_wake_sleep_prep(handle, sstate);
2639 else
2640 acpi_wake_run_prep(handle, sstate);
2641 return (AE_OK);
2642 }
2643
2644 /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2645 static int
2646 acpi_wake_prep_walk(int sstate)
2647 {
2648 ACPI_HANDLE sb_handle;
2649
2650 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2651 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
2652 acpi_wake_prep, &sstate, NULL);
2653 return (0);
2654 }
2655
2656 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
2657 static int
2658 acpi_wake_sysctl_walk(device_t dev)
2659 {
2660 int error, i, numdevs;
2661 device_t *devlist;
2662 device_t child;
2663 ACPI_STATUS status;
2664
2665 error = device_get_children(dev, &devlist, &numdevs);
2666 if (error != 0 || numdevs == 0) {
2667 if (numdevs == 0)
2668 free(devlist, M_TEMP);
2669 return (error);
2670 }
2671 for (i = 0; i < numdevs; i++) {
2672 child = devlist[i];
2673 acpi_wake_sysctl_walk(child);
2674 if (!device_is_attached(child))
2675 continue;
2676 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2677 if (ACPI_SUCCESS(status)) {
2678 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
2679 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
2680 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
2681 acpi_wake_set_sysctl, "I", "Device set to wake the system");
2682 }
2683 }
2684 free(devlist, M_TEMP);
2685
2686 return (0);
2687 }
2688
2689 /* Enable or disable wake from userland. */
2690 static int
2691 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
2692 {
2693 int enable, error;
2694 device_t dev;
2695
2696 dev = (device_t)arg1;
2697 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
2698
2699 error = sysctl_handle_int(oidp, &enable, 0, req);
2700 if (error != 0 || req->newptr == NULL)
2701 return (error);
2702 if (enable != 0 && enable != 1)
2703 return (EINVAL);
2704
2705 return (acpi_wake_set_enable(dev, enable));
2706 }
2707
2708 /* Parse a device's _PRW into a structure. */
2709 int
2710 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2711 {
2712 ACPI_STATUS status;
2713 ACPI_BUFFER prw_buffer;
2714 ACPI_OBJECT *res, *res2;
2715 int error, i, power_count;
2716
2717 if (h == NULL || prw == NULL)
2718 return (EINVAL);
2719
2720 /*
2721 * The _PRW object (7.2.9) is only required for devices that have the
2722 * ability to wake the system from a sleeping state.
2723 */
2724 error = EINVAL;
2725 prw_buffer.Pointer = NULL;
2726 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2727 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2728 if (ACPI_FAILURE(status))
2729 return (ENOENT);
2730 res = (ACPI_OBJECT *)prw_buffer.Pointer;
2731 if (res == NULL)
2732 return (ENOENT);
2733 if (!ACPI_PKG_VALID(res, 2))
2734 goto out;
2735
2736 /*
2737 * Element 1 of the _PRW object:
2738 * The lowest power system sleeping state that can be entered while still
2739 * providing wake functionality. The sleeping state being entered must
2740 * be less than (i.e., higher power) or equal to this value.
2741 */
2742 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2743 goto out;
2744
2745 /*
2746 * Element 0 of the _PRW object:
2747 */
2748 switch (res->Package.Elements[0].Type) {
2749 case ACPI_TYPE_INTEGER:
2750 /*
2751 * If the data type of this package element is numeric, then this
2752 * _PRW package element is the bit index in the GPEx_EN, in the
2753 * GPE blocks described in the FADT, of the enable bit that is
2754 * enabled for the wake event.
2755 */
2756 prw->gpe_handle = NULL;
2757 prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2758 error = 0;
2759 break;
2760 case ACPI_TYPE_PACKAGE:
2761 /*
2762 * If the data type of this package element is a package, then this
2763 * _PRW package element is itself a package containing two
2764 * elements. The first is an object reference to the GPE Block
2765 * device that contains the GPE that will be triggered by the wake
2766 * event. The second element is numeric and it contains the bit
2767 * index in the GPEx_EN, in the GPE Block referenced by the
2768 * first element in the package, of the enable bit that is enabled for
2769 * the wake event.
2770 *
2771 * For example, if this field is a package then it is of the form:
2772 * Package() {\_SB.PCI0.ISA.GPE, 2}
2773 */
2774 res2 = &res->Package.Elements[0];
2775 if (!ACPI_PKG_VALID(res2, 2))
2776 goto out;
2777 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2778 if (prw->gpe_handle == NULL)
2779 goto out;
2780 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2781 goto out;
2782 error = 0;
2783 break;
2784 default:
2785 goto out;
2786 }
2787
2788 /* Elements 2 to N of the _PRW object are power resources. */
2789 power_count = res->Package.Count - 2;
2790 if (power_count > ACPI_PRW_MAX_POWERRES) {
2791 printf("ACPI device %s has too many power resources\n", acpi_name(h));
2792 power_count = 0;
2793 }
2794 prw->power_res_count = power_count;
2795 for (i = 0; i < power_count; i++)
2796 prw->power_res[i] = res->Package.Elements[i];
2797
2798 out:
2799 if (prw_buffer.Pointer != NULL)
2800 AcpiOsFree(prw_buffer.Pointer);
2801 return (error);
2802 }
2803
2804 /*
2805 * ACPI Event Handlers
2806 */
2807
2808 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
2809
2810 static void
2811 acpi_system_eventhandler_sleep(void *arg, int state)
2812 {
2813 int ret;
2814
2815 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2816
2817 /* Check if button action is disabled. */
2818 if (state == ACPI_S_STATES_MAX + 1)
2819 return;
2820
2821 /* Request that the system prepare to enter the given suspend state. */
2822 ret = acpi_ReqSleepState((struct acpi_softc *)arg, state);
2823 if (ret != 0)
2824 printf("acpi: request to enter state S%d failed (err %d)\n",
2825 state, ret);
2826
2827 return_VOID;
2828 }
2829
2830 static void
2831 acpi_system_eventhandler_wakeup(void *arg, int state)
2832 {
2833
2834 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2835
2836 /* Currently, nothing to do for wakeup. */
2837
2838 return_VOID;
2839 }
2840
2841 /*
2842 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
2843 */
2844 UINT32
2845 acpi_event_power_button_sleep(void *context)
2846 {
2847 struct acpi_softc *sc = (struct acpi_softc *)context;
2848
2849 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2850
2851 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
2852
2853 return_VALUE (ACPI_INTERRUPT_HANDLED);
2854 }
2855
2856 UINT32
2857 acpi_event_power_button_wake(void *context)
2858 {
2859 struct acpi_softc *sc = (struct acpi_softc *)context;
2860
2861 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2862
2863 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
2864
2865 return_VALUE (ACPI_INTERRUPT_HANDLED);
2866 }
2867
2868 UINT32
2869 acpi_event_sleep_button_sleep(void *context)
2870 {
2871 struct acpi_softc *sc = (struct acpi_softc *)context;
2872
2873 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2874
2875 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
2876
2877 return_VALUE (ACPI_INTERRUPT_HANDLED);
2878 }
2879
2880 UINT32
2881 acpi_event_sleep_button_wake(void *context)
2882 {
2883 struct acpi_softc *sc = (struct acpi_softc *)context;
2884
2885 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2886
2887 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
2888
2889 return_VALUE (ACPI_INTERRUPT_HANDLED);
2890 }
2891
2892 /*
2893 * XXX This static buffer is suboptimal. There is no locking so only
2894 * use this for single-threaded callers.
2895 */
2896 char *
2897 acpi_name(ACPI_HANDLE handle)
2898 {
2899 ACPI_BUFFER buf;
2900 static char data[256];
2901
2902 buf.Length = sizeof(data);
2903 buf.Pointer = data;
2904
2905 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
2906 return (data);
2907 return ("(unknown)");
2908 }
2909
2910 /*
2911 * Debugging/bug-avoidance. Avoid trying to fetch info on various
2912 * parts of the namespace.
2913 */
2914 int
2915 acpi_avoid(ACPI_HANDLE handle)
2916 {
2917 char *cp, *env, *np;
2918 int len;
2919
2920 np = acpi_name(handle);
2921 if (*np == '\\')
2922 np++;
2923 if ((env = getenv("debug.acpi.avoid")) == NULL)
2924 return (0);
2925
2926 /* Scan the avoid list checking for a match */
2927 cp = env;
2928 for (;;) {
2929 while (*cp != 0 && isspace(*cp))
2930 cp++;
2931 if (*cp == 0)
2932 break;
2933 len = 0;
2934 while (cp[len] != 0 && !isspace(cp[len]))
2935 len++;
2936 if (!strncmp(cp, np, len)) {
2937 freeenv(env);
2938 return(1);
2939 }
2940 cp += len;
2941 }
2942 freeenv(env);
2943
2944 return (0);
2945 }
2946
2947 /*
2948 * Debugging/bug-avoidance. Disable ACPI subsystem components.
2949 */
2950 int
2951 acpi_disabled(char *subsys)
2952 {
2953 char *cp, *env;
2954 int len;
2955
2956 if ((env = getenv("debug.acpi.disabled")) == NULL)
2957 return (0);
2958 if (strcmp(env, "all") == 0) {
2959 freeenv(env);
2960 return (1);
2961 }
2962
2963 /* Scan the disable list, checking for a match. */
2964 cp = env;
2965 for (;;) {
2966 while (*cp != '\0' && isspace(*cp))
2967 cp++;
2968 if (*cp == '\0')
2969 break;
2970 len = 0;
2971 while (cp[len] != '\0' && !isspace(cp[len]))
2972 len++;
2973 if (strncmp(cp, subsys, len) == 0) {
2974 freeenv(env);
2975 return (1);
2976 }
2977 cp += len;
2978 }
2979 freeenv(env);
2980
2981 return (0);
2982 }
2983
2984 /*
2985 * Control interface.
2986 *
2987 * We multiplex ioctls for all participating ACPI devices here. Individual
2988 * drivers wanting to be accessible via /dev/acpi should use the
2989 * register/deregister interface to make their handlers visible.
2990 */
2991 struct acpi_ioctl_hook
2992 {
2993 TAILQ_ENTRY(acpi_ioctl_hook) link;
2994 u_long cmd;
2995 acpi_ioctl_fn fn;
2996 void *arg;
2997 };
2998
2999 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
3000 static int acpi_ioctl_hooks_initted;
3001
3002 int
3003 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
3004 {
3005 struct acpi_ioctl_hook *hp;
3006
3007 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
3008 return (ENOMEM);
3009 hp->cmd = cmd;
3010 hp->fn = fn;
3011 hp->arg = arg;
3012
3013 ACPI_LOCK(acpi);
3014 if (acpi_ioctl_hooks_initted == 0) {
3015 TAILQ_INIT(&acpi_ioctl_hooks);
3016 acpi_ioctl_hooks_initted = 1;
3017 }
3018 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
3019 ACPI_UNLOCK(acpi);
3020
3021 return (0);
3022 }
3023
3024 void
3025 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
3026 {
3027 struct acpi_ioctl_hook *hp;
3028
3029 ACPI_LOCK(acpi);
3030 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
3031 if (hp->cmd == cmd && hp->fn == fn)
3032 break;
3033
3034 if (hp != NULL) {
3035 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
3036 free(hp, M_ACPIDEV);
3037 }
3038 ACPI_UNLOCK(acpi);
3039 }
3040
3041 static int
3042 acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td)
3043 {
3044 return (0);
3045 }
3046
3047 static int
3048 acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td)
3049 {
3050 return (0);
3051 }
3052
3053 static int
3054 acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
3055 {
3056 struct acpi_softc *sc;
3057 struct acpi_ioctl_hook *hp;
3058 int error, state;
3059
3060 error = 0;
3061 hp = NULL;
3062 sc = dev->si_drv1;
3063
3064 /*
3065 * Scan the list of registered ioctls, looking for handlers.
3066 */
3067 ACPI_LOCK(acpi);
3068 if (acpi_ioctl_hooks_initted)
3069 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3070 if (hp->cmd == cmd)
3071 break;
3072 }
3073 ACPI_UNLOCK(acpi);
3074 if (hp)
3075 return (hp->fn(cmd, addr, hp->arg));
3076
3077 /*
3078 * Core ioctls are not permitted for non-writable user.
3079 * Currently, other ioctls just fetch information.
3080 * Not changing system behavior.
3081 */
3082 if ((flag & FWRITE) == 0)
3083 return (EPERM);
3084
3085 /* Core system ioctls. */
3086 switch (cmd) {
3087 case ACPIIO_REQSLPSTATE:
3088 state = *(int *)addr;
3089 if (state != ACPI_STATE_S5)
3090 error = acpi_ReqSleepState(sc, state);
3091 else {
3092 printf("power off via acpi ioctl not supported\n");
3093 error = ENXIO;
3094 }
3095 break;
3096 case ACPIIO_ACKSLPSTATE:
3097 error = *(int *)addr;
3098 error = acpi_AckSleepState(sc->acpi_clone, error);
3099 break;
3100 case ACPIIO_SETSLPSTATE: /* DEPRECATED */
3101 error = EINVAL;
3102 state = *(int *)addr;
3103 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
3104 if (ACPI_SUCCESS(acpi_SetSleepState(sc, state)))
3105 error = 0;
3106 break;
3107 default:
3108 error = ENXIO;
3109 break;
3110 }
3111
3112 return (error);
3113 }
3114
3115 static int
3116 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3117 {
3118 int error;
3119 struct sbuf sb;
3120 UINT8 state, TypeA, TypeB;
3121
3122 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3123 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++)
3124 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
3125 sbuf_printf(&sb, "S%d ", state);
3126 sbuf_trim(&sb);
3127 sbuf_finish(&sb);
3128 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3129 sbuf_delete(&sb);
3130 return (error);
3131 }
3132
3133 static int
3134 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3135 {
3136 char sleep_state[10];
3137 int error;
3138 u_int new_state, old_state;
3139
3140 old_state = *(u_int *)oidp->oid_arg1;
3141 if (old_state > ACPI_S_STATES_MAX + 1)
3142 strlcpy(sleep_state, "unknown", sizeof(sleep_state));
3143 else
3144 strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state));
3145 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
3146 if (error == 0 && req->newptr != NULL) {
3147 new_state = ACPI_STATE_S0;
3148 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++)
3149 if (strcmp(sleep_state, sleep_state_names[new_state]) == 0)
3150 break;
3151 if (new_state <= ACPI_S_STATES_MAX + 1) {
3152 if (new_state != old_state)
3153 *(u_int *)oidp->oid_arg1 = new_state;
3154 } else
3155 error = EINVAL;
3156 }
3157
3158 return (error);
3159 }
3160
3161 /* Inform devctl(4) when we receive a Notify. */
3162 void
3163 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
3164 {
3165 char notify_buf[16];
3166 ACPI_BUFFER handle_buf;
3167 ACPI_STATUS status;
3168
3169 if (subsystem == NULL)
3170 return;
3171
3172 handle_buf.Pointer = NULL;
3173 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
3174 status = AcpiNsHandleToPathname(h, &handle_buf);
3175 if (ACPI_FAILURE(status))
3176 return;
3177 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
3178 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
3179 AcpiOsFree(handle_buf.Pointer);
3180 }
3181
3182 #ifdef ACPI_DEBUG
3183 /*
3184 * Support for parsing debug options from the kernel environment.
3185 *
3186 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
3187 * by specifying the names of the bits in the debug.acpi.layer and
3188 * debug.acpi.level environment variables. Bits may be unset by
3189 * prefixing the bit name with !.
3190 */
3191 struct debugtag
3192 {
3193 char *name;
3194 UINT32 value;
3195 };
3196
3197 static struct debugtag dbg_layer[] = {
3198 {"ACPI_UTILITIES", ACPI_UTILITIES},
3199 {"ACPI_HARDWARE", ACPI_HARDWARE},
3200 {"ACPI_EVENTS", ACPI_EVENTS},
3201 {"ACPI_TABLES", ACPI_TABLES},
3202 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
3203 {"ACPI_PARSER", ACPI_PARSER},
3204 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
3205 {"ACPI_EXECUTER", ACPI_EXECUTER},
3206 {"ACPI_RESOURCES", ACPI_RESOURCES},
3207 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
3208 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
3209 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
3210 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
3211
3212 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
3213 {"ACPI_BATTERY", ACPI_BATTERY},
3214 {"ACPI_BUS", ACPI_BUS},
3215 {"ACPI_BUTTON", ACPI_BUTTON},
3216 {"ACPI_EC", ACPI_EC},
3217 {"ACPI_FAN", ACPI_FAN},
3218 {"ACPI_POWERRES", ACPI_POWERRES},
3219 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
3220 {"ACPI_THERMAL", ACPI_THERMAL},
3221 {"ACPI_TIMER", ACPI_TIMER},
3222 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
3223 {NULL, 0}
3224 };
3225
3226 static struct debugtag dbg_level[] = {
3227 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
3228 {"ACPI_LV_WARN", ACPI_LV_WARN},
3229 {"ACPI_LV_INIT", ACPI_LV_INIT},
3230 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
3231 {"ACPI_LV_INFO", ACPI_LV_INFO},
3232 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
3233
3234 /* Trace verbosity level 1 [Standard Trace Level] */
3235 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
3236 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
3237 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
3238 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
3239 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
3240 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
3241 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
3242 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
3243 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
3244 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
3245 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
3246 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
3247 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
3248 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
3249 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
3250
3251 /* Trace verbosity level 2 [Function tracing and memory allocation] */
3252 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
3253 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
3254 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
3255 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
3256 {"ACPI_LV_ALL", ACPI_LV_ALL},
3257
3258 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
3259 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
3260 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
3261 {"ACPI_LV_IO", ACPI_LV_IO},
3262 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
3263 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
3264
3265 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
3266 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
3267 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
3268 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
3269 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
3270 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
3271 {NULL, 0}
3272 };
3273
3274 static void
3275 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
3276 {
3277 char *ep;
3278 int i, l;
3279 int set;
3280
3281 while (*cp) {
3282 if (isspace(*cp)) {
3283 cp++;
3284 continue;
3285 }
3286 ep = cp;
3287 while (*ep && !isspace(*ep))
3288 ep++;
3289 if (*cp == '!') {
3290 set = 0;
3291 cp++;
3292 if (cp == ep)
3293 continue;
3294 } else {
3295 set = 1;
3296 }
3297 l = ep - cp;
3298 for (i = 0; tag[i].name != NULL; i++) {
3299 if (!strncmp(cp, tag[i].name, l)) {
3300 if (set)
3301 *flag |= tag[i].value;
3302 else
3303 *flag &= ~tag[i].value;
3304 }
3305 }
3306 cp = ep;
3307 }
3308 }
3309
3310 static void
3311 acpi_set_debugging(void *junk)
3312 {
3313 char *layer, *level;
3314
3315 if (cold) {
3316 AcpiDbgLayer = 0;
3317 AcpiDbgLevel = 0;
3318 }
3319
3320 layer = getenv("debug.acpi.layer");
3321 level = getenv("debug.acpi.level");
3322 if (layer == NULL && level == NULL)
3323 return;
3324
3325 printf("ACPI set debug");
3326 if (layer != NULL) {
3327 if (strcmp("NONE", layer) != 0)
3328 printf(" layer '%s'", layer);
3329 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
3330 freeenv(layer);
3331 }
3332 if (level != NULL) {
3333 if (strcmp("NONE", level) != 0)
3334 printf(" level '%s'", level);
3335 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
3336 freeenv(level);
3337 }
3338 printf("\n");
3339 }
3340
3341 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3342 NULL);
3343
3344 static int
3345 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
3346 {
3347 int error, *dbg;
3348 struct debugtag *tag;
3349 struct sbuf sb;
3350
3351 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
3352 return (ENOMEM);
3353 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
3354 tag = &dbg_layer[0];
3355 dbg = &AcpiDbgLayer;
3356 } else {
3357 tag = &dbg_level[0];
3358 dbg = &AcpiDbgLevel;
3359 }
3360
3361 /* Get old values if this is a get request. */
3362 ACPI_SERIAL_BEGIN(acpi);
3363 if (*dbg == 0) {
3364 sbuf_cpy(&sb, "NONE");
3365 } else if (req->newptr == NULL) {
3366 for (; tag->name != NULL; tag++) {
3367 if ((*dbg & tag->value) == tag->value)
3368 sbuf_printf(&sb, "%s ", tag->name);
3369 }
3370 }
3371 sbuf_trim(&sb);
3372 sbuf_finish(&sb);
3373
3374 /* Copy out the old values to the user. */
3375 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
3376 sbuf_delete(&sb);
3377
3378 /* If the user is setting a string, parse it. */
3379 if (error == 0 && req->newptr != NULL) {
3380 *dbg = 0;
3381 setenv((char *)oidp->oid_arg1, (char *)req->newptr);
3382 acpi_set_debugging(NULL);
3383 }
3384 ACPI_SERIAL_END(acpi);
3385
3386 return (error);
3387 }
3388
3389 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
3390 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
3391 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
3392 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3393 #endif /* ACPI_DEBUG */
3394
3395 static int
3396 acpi_pm_func(u_long cmd, void *arg, ...)
3397 {
3398 int state, acpi_state;
3399 int error;
3400 struct acpi_softc *sc;
3401 va_list ap;
3402
3403 error = 0;
3404 switch (cmd) {
3405 case POWER_CMD_SUSPEND:
3406 sc = (struct acpi_softc *)arg;
3407 if (sc == NULL) {
3408 error = EINVAL;
3409 goto out;
3410 }
3411
3412 va_start(ap, arg);
3413 state = va_arg(ap, int);
3414 va_end(ap);
3415
3416 switch (state) {
3417 case POWER_SLEEP_STATE_STANDBY:
3418 acpi_state = sc->acpi_standby_sx;
3419 break;
3420 case POWER_SLEEP_STATE_SUSPEND:
3421 acpi_state = sc->acpi_suspend_sx;
3422 break;
3423 case POWER_SLEEP_STATE_HIBERNATE:
3424 acpi_state = ACPI_STATE_S4;
3425 break;
3426 default:
3427 error = EINVAL;
3428 goto out;
3429 }
3430
3431 if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
3432 error = ENXIO;
3433 break;
3434 default:
3435 error = EINVAL;
3436 goto out;
3437 }
3438
3439 out:
3440 return (error);
3441 }
3442
3443 static void
3444 acpi_pm_register(void *arg)
3445 {
3446 if (!cold || resource_disabled("acpi", 0))
3447 return;
3448
3449 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3450 }
3451
3452 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
Cache object: f89bcb884e55aa32c5c70e1b8778538e
|