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 * Copyright (c) 2025 The FreeBSD Foundation
8 *
9 * Portions of this software were developed by Aymeric Wibo
10 * <obiwac@freebsd.org> under sponsorship from the FreeBSD Foundation.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #include "opt_acpi.h"
36
37 #include <sys/param.h>
38 #include <sys/eventhandler.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <sys/conf.h>
46 #include <sys/ioccom.h>
47 #include <sys/reboot.h>
48 #include <sys/sysctl.h>
49 #include <sys/ctype.h>
50 #include <sys/linker.h>
51 #include <sys/mount.h>
52 #include <sys/power.h>
53 #include <sys/sbuf.h>
54 #include <sys/sched.h>
55 #include <sys/smp.h>
56 #include <sys/timetc.h>
57 #include <sys/uuid.h>
58
59 #if defined(__i386__) || defined(__amd64__)
60 #include <machine/clock.h>
61 #include <machine/pci_cfgreg.h>
62 #include <x86/cputypes.h>
63 #include <x86/x86_var.h>
64 #endif
65 #include <machine/resource.h>
66 #include <machine/bus.h>
67 #include <sys/rman.h>
68 #include <isa/isavar.h>
69 #include <isa/pnpvar.h>
70
71 #include <contrib/dev/acpica/include/acpi.h>
72 #include <contrib/dev/acpica/include/accommon.h>
73 #include <contrib/dev/acpica/include/acnamesp.h>
74
75 #include <dev/acpica/acpivar.h>
76 #include <dev/acpica/acpiio.h>
77
78 #include <dev/pci/pcivar.h>
79
80 #include <vm/vm_param.h>
81
82 static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
83
84 /* Hooks for the ACPI CA debugging infrastructure */
85 #define _COMPONENT ACPI_BUS
86 ACPI_MODULE_NAME("ACPI")
87
88 static d_open_t acpiopen;
89 static d_close_t acpiclose;
90 static d_ioctl_t acpiioctl;
91
92 static struct cdevsw acpi_cdevsw = {
93 .d_version = D_VERSION,
94 .d_open = acpiopen,
95 .d_close = acpiclose,
96 .d_ioctl = acpiioctl,
97 .d_name = "acpi",
98 };
99
100 struct acpi_interface {
101 ACPI_STRING *data;
102 int num;
103 };
104
105 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
106
107 /* Global mutex for locking access to the ACPI subsystem. */
108 struct mtx acpi_mutex;
109 struct callout acpi_sleep_timer;
110
111 /* Bitmap of device quirks. */
112 int acpi_quirks;
113
114 /* Supported sleep states and types. */
115 static bool acpi_supported_stypes[POWER_STYPE_COUNT];
116 static bool acpi_supported_sstates[ACPI_S_STATE_COUNT];
117
118 static void acpi_lookup(void *arg, const char *name, device_t *dev);
119 static int acpi_modevent(struct module *mod, int event, void *junk);
120
121 static device_probe_t acpi_probe;
122 static device_attach_t acpi_attach;
123 static device_suspend_t acpi_suspend;
124 static device_resume_t acpi_resume;
125 static device_shutdown_t acpi_shutdown;
126
127 static bus_add_child_t acpi_add_child;
128 static bus_print_child_t acpi_print_child;
129 static bus_probe_nomatch_t acpi_probe_nomatch;
130 static bus_driver_added_t acpi_driver_added;
131 static bus_child_deleted_t acpi_child_deleted;
132 static bus_read_ivar_t acpi_read_ivar;
133 static bus_write_ivar_t acpi_write_ivar;
134 static bus_get_resource_list_t acpi_get_rlist;
135 static bus_get_rman_t acpi_get_rman;
136 static bus_set_resource_t acpi_set_resource;
137 static bus_alloc_resource_t acpi_alloc_resource;
138 static bus_adjust_resource_t acpi_adjust_resource;
139 static bus_release_resource_t acpi_release_resource;
140 static bus_delete_resource_t acpi_delete_resource;
141 static bus_activate_resource_t acpi_activate_resource;
142 static bus_deactivate_resource_t acpi_deactivate_resource;
143 static bus_map_resource_t acpi_map_resource;
144 static bus_unmap_resource_t acpi_unmap_resource;
145 static bus_child_pnpinfo_t acpi_child_pnpinfo_method;
146 static bus_child_location_t acpi_child_location_method;
147 static bus_hint_device_unit_t acpi_hint_device_unit;
148 static bus_get_property_t acpi_bus_get_prop;
149 static bus_get_device_path_t acpi_get_device_path;
150 static bus_get_domain_t acpi_get_domain_method;
151
152 static acpi_id_probe_t acpi_device_id_probe;
153 static acpi_evaluate_object_t acpi_device_eval_obj;
154 static acpi_get_property_t acpi_device_get_prop;
155 static acpi_scan_children_t acpi_device_scan_children;
156
157 static isa_pnp_probe_t acpi_isa_pnp_probe;
158
159 static void acpi_reserve_resources(device_t dev);
160 static int acpi_sysres_alloc(device_t dev);
161 static uint32_t acpi_isa_get_logicalid(device_t dev);
162 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
163 static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
164 void *context, void **retval);
165 static ACPI_STATUS acpi_find_dsd(struct acpi_device *ad);
166 static void acpi_platform_osc(device_t dev);
167 static void acpi_probe_children(device_t bus);
168 static void acpi_probe_order(ACPI_HANDLE handle, int *order);
169 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
170 void *context, void **status);
171 static void acpi_sleep_enable(void *arg);
172 static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
173 static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc,
174 enum power_stype stype);
175 static void acpi_shutdown_final(void *arg, int howto);
176 static void acpi_enable_fixed_events(struct acpi_softc *sc);
177 static void acpi_resync_clock(struct acpi_softc *sc);
178 static int acpi_wake_sleep_prep(ACPI_HANDLE handle,
179 enum power_stype stype);
180 static int acpi_wake_run_prep(ACPI_HANDLE handle, enum power_stype stype);
181 static int acpi_wake_prep_walk(enum power_stype stype);
182 static int acpi_wake_sysctl_walk(device_t dev);
183 static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
184 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
185 static void acpi_system_eventhandler_sleep(void *arg,
186 enum power_stype stype);
187 static void acpi_system_eventhandler_wakeup(void *arg,
188 enum power_stype stype);
189 static enum power_stype acpi_sstate_to_stype(int sstate);
190 static int acpi_sname_to_sstate(const char *sname);
191 static const char *acpi_sstate_to_sname(int sstate);
192 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
193 static int acpi_stype_sysctl(SYSCTL_HANDLER_ARGS);
194 static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
195 static int acpi_stype_to_sstate(struct acpi_softc *sc, enum power_stype stype);
196 static int acpi_pm_func(u_long cmd, void *arg, enum power_stype stype);
197 static void acpi_enable_pcie(void);
198 static void acpi_reset_interfaces(device_t dev);
199
200 static device_method_t acpi_methods[] = {
201 /* Device interface */
202 DEVMETHOD(device_probe, acpi_probe),
203 DEVMETHOD(device_attach, acpi_attach),
204 DEVMETHOD(device_shutdown, acpi_shutdown),
205 DEVMETHOD(device_detach, bus_generic_detach),
206 DEVMETHOD(device_suspend, acpi_suspend),
207 DEVMETHOD(device_resume, acpi_resume),
208
209 /* Bus interface */
210 DEVMETHOD(bus_add_child, acpi_add_child),
211 DEVMETHOD(bus_print_child, acpi_print_child),
212 DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch),
213 DEVMETHOD(bus_driver_added, acpi_driver_added),
214 DEVMETHOD(bus_child_deleted, acpi_child_deleted),
215 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
216 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
217 DEVMETHOD(bus_get_resource_list, acpi_get_rlist),
218 DEVMETHOD(bus_get_rman, acpi_get_rman),
219 DEVMETHOD(bus_set_resource, acpi_set_resource),
220 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
221 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
222 DEVMETHOD(bus_adjust_resource, acpi_adjust_resource),
223 DEVMETHOD(bus_release_resource, acpi_release_resource),
224 DEVMETHOD(bus_delete_resource, acpi_delete_resource),
225 DEVMETHOD(bus_activate_resource, acpi_activate_resource),
226 DEVMETHOD(bus_deactivate_resource, acpi_deactivate_resource),
227 DEVMETHOD(bus_map_resource, acpi_map_resource),
228 DEVMETHOD(bus_unmap_resource, acpi_unmap_resource),
229 DEVMETHOD(bus_child_pnpinfo, acpi_child_pnpinfo_method),
230 DEVMETHOD(bus_child_location, acpi_child_location_method),
231 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
232 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
233 DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit),
234 DEVMETHOD(bus_get_cpus, acpi_get_cpus),
235 DEVMETHOD(bus_get_domain, acpi_get_domain_method),
236 DEVMETHOD(bus_get_property, acpi_bus_get_prop),
237 DEVMETHOD(bus_get_device_path, acpi_get_device_path),
238
239 /* ACPI bus */
240 DEVMETHOD(acpi_id_probe, acpi_device_id_probe),
241 DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj),
242 DEVMETHOD(acpi_get_property, acpi_device_get_prop),
243 DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep),
244 DEVMETHOD(acpi_scan_children, acpi_device_scan_children),
245
246 /* ISA emulation */
247 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
248
249 DEVMETHOD_END
250 };
251
252 static driver_t acpi_driver = {
253 "acpi",
254 acpi_methods,
255 sizeof(struct acpi_softc),
256 };
257
258 EARLY_DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_modevent, 0,
259 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
260 MODULE_VERSION(acpi, 1);
261
262 ACPI_SERIAL_DECL(acpi, "ACPI root bus");
263
264 /* Local pools for managing system resources for ACPI child devices. */
265 static struct rman acpi_rman_io, acpi_rman_mem;
266
267 #define ACPI_MINIMUM_AWAKETIME 5
268
269 /* Holds the description of the acpi0 device. */
270 static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
271
272 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
273 "ACPI debugging");
274 static char acpi_ca_version[12];
275 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
276 acpi_ca_version, 0, "Version of Intel ACPI-CA");
277
278 /*
279 * Allow overriding _OSI methods.
280 */
281 static char acpi_install_interface[256];
282 TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface,
283 sizeof(acpi_install_interface));
284 static char acpi_remove_interface[256];
285 TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface,
286 sizeof(acpi_remove_interface));
287
288 /* Allow users to dump Debug objects without ACPI debugger. */
289 static int acpi_debug_objects;
290 TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects);
291 SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects,
292 CTLFLAG_RW | CTLTYPE_INT | CTLFLAG_MPSAFE, NULL, 0,
293 acpi_debug_objects_sysctl, "I",
294 "Enable Debug objects");
295
296 /* Allow the interpreter to ignore common mistakes in BIOS. */
297 static int acpi_interpreter_slack = 1;
298 TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
299 SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
300 &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
301
302 /* Ignore register widths set by FADT and use default widths instead. */
303 static int acpi_ignore_reg_width = 1;
304 TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width);
305 SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN,
306 &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT");
307
308 /* Allow users to override quirks. */
309 TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
310
311 int acpi_susp_bounce;
312 SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
313 &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
314
315 #if defined(__amd64__) || defined(__i386__)
316 int acpi_override_isa_irq_polarity;
317 #endif
318
319 /*
320 * ACPI standard UUID for Device Specific Data Package
321 * "Device Properties UUID for _DSD" Rev. 2.0
322 */
323 static const struct uuid acpi_dsd_uuid = {
324 0xdaffd814, 0x6eba, 0x4d8c, 0x8a, 0x91,
325 { 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01 }
326 };
327
328 /*
329 * ACPI can only be loaded as a module by the loader; activating it after
330 * system bootstrap time is not useful, and can be fatal to the system.
331 * It also cannot be unloaded, since the entire system bus hierarchy hangs
332 * off it.
333 */
334 static int
acpi_modevent(struct module * mod,int event,void * junk)335 acpi_modevent(struct module *mod, int event, void *junk)
336 {
337 switch (event) {
338 case MOD_LOAD:
339 if (!cold) {
340 printf("The ACPI driver cannot be loaded after boot.\n");
341 return (EPERM);
342 }
343 break;
344 case MOD_UNLOAD:
345 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
346 return (EBUSY);
347 break;
348 default:
349 break;
350 }
351 return (0);
352 }
353
354 /*
355 * Perform early initialization.
356 */
357 ACPI_STATUS
acpi_Startup(void)358 acpi_Startup(void)
359 {
360 static int started = 0;
361 ACPI_STATUS status;
362 int val;
363
364 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
365
366 /* Only run the startup code once. The MADT driver also calls this. */
367 if (started)
368 return_VALUE (AE_OK);
369 started = 1;
370
371 /*
372 * Initialize the ACPICA subsystem.
373 */
374 if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) {
375 printf("ACPI: Could not initialize Subsystem: %s\n",
376 AcpiFormatException(status));
377 return_VALUE (status);
378 }
379
380 /*
381 * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
382 * if more tables exist.
383 */
384 if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
385 printf("ACPI: Table initialisation failed: %s\n",
386 AcpiFormatException(status));
387 return_VALUE (status);
388 }
389
390 /* Set up any quirks we have for this system. */
391 if (acpi_quirks == ACPI_Q_OK)
392 acpi_table_quirks(&acpi_quirks);
393
394 /* If the user manually set the disabled hint to 0, force-enable ACPI. */
395 if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
396 acpi_quirks &= ~ACPI_Q_BROKEN;
397 if (acpi_quirks & ACPI_Q_BROKEN) {
398 printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n");
399 status = AE_SUPPORT;
400 }
401
402 return_VALUE (status);
403 }
404
405 /*
406 * Detect ACPI and perform early initialisation.
407 */
408 int
acpi_identify(void)409 acpi_identify(void)
410 {
411 ACPI_TABLE_RSDP *rsdp;
412 ACPI_TABLE_HEADER *rsdt;
413 ACPI_PHYSICAL_ADDRESS paddr;
414 struct sbuf sb;
415
416 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
417
418 if (!cold)
419 return (ENXIO);
420
421 /* Check that we haven't been disabled with a hint. */
422 if (resource_disabled("acpi", 0))
423 return (ENXIO);
424
425 /* Check for other PM systems. */
426 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
427 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
428 printf("ACPI identify failed, other PM system enabled.\n");
429 return (ENXIO);
430 }
431
432 /* Initialize root tables. */
433 if (ACPI_FAILURE(acpi_Startup())) {
434 printf("ACPI: Try disabling either ACPI or apic support.\n");
435 return (ENXIO);
436 }
437
438 if ((paddr = AcpiOsGetRootPointer()) == 0 ||
439 (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
440 return (ENXIO);
441 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
442 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
443 else
444 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
445 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
446
447 if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
448 return (ENXIO);
449 sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
450 sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
451 sbuf_trim(&sb);
452 sbuf_putc(&sb, ' ');
453 sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
454 sbuf_trim(&sb);
455 sbuf_finish(&sb);
456 sbuf_delete(&sb);
457 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
458
459 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
460
461 return (0);
462 }
463
464 /*
465 * Fetch some descriptive data from ACPI to put in our attach message.
466 */
467 static int
acpi_probe(device_t dev)468 acpi_probe(device_t dev)
469 {
470
471 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
472
473 device_set_desc(dev, acpi_desc);
474
475 return_VALUE (BUS_PROBE_NOWILDCARD);
476 }
477
478 static int
acpi_attach(device_t dev)479 acpi_attach(device_t dev)
480 {
481 struct acpi_softc *sc;
482 ACPI_STATUS status;
483 int error, state;
484 UINT32 flags;
485 UINT8 TypeA, TypeB;
486 char *env;
487 enum power_stype stype;
488
489 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
490
491 sc = device_get_softc(dev);
492 sc->acpi_dev = dev;
493 callout_init(&sc->susp_force_to, 1);
494
495 error = ENXIO;
496
497 /* Initialize resource manager. */
498 acpi_rman_io.rm_type = RMAN_ARRAY;
499 acpi_rman_io.rm_start = 0;
500 acpi_rman_io.rm_end = 0xffff;
501 acpi_rman_io.rm_descr = "ACPI I/O ports";
502 if (rman_init(&acpi_rman_io) != 0)
503 panic("acpi rman_init IO ports failed");
504 acpi_rman_mem.rm_type = RMAN_ARRAY;
505 acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
506 if (rman_init(&acpi_rman_mem) != 0)
507 panic("acpi rman_init memory failed");
508
509 resource_list_init(&sc->sysres_rl);
510
511 /* Initialise the ACPI mutex */
512 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
513
514 /*
515 * Set the globals from our tunables. This is needed because ACPI-CA
516 * uses UINT8 for some values and we have no tunable_byte.
517 */
518 AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
519 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
520 AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE;
521
522 #ifndef ACPI_DEBUG
523 /*
524 * Disable all debugging layers and levels.
525 */
526 AcpiDbgLayer = 0;
527 AcpiDbgLevel = 0;
528 #endif
529
530 /* Override OS interfaces if the user requested. */
531 acpi_reset_interfaces(dev);
532
533 /* Load ACPI name space. */
534 status = AcpiLoadTables();
535 if (ACPI_FAILURE(status)) {
536 device_printf(dev, "Could not load Namespace: %s\n",
537 AcpiFormatException(status));
538 goto out;
539 }
540
541 /* Handle MCFG table if present. */
542 acpi_enable_pcie();
543
544 /*
545 * Note that some systems (specifically, those with namespace evaluation
546 * issues that require the avoidance of parts of the namespace) must
547 * avoid running _INI and _STA on everything, as well as dodging the final
548 * object init pass.
549 *
550 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
551 *
552 * XXX We should arrange for the object init pass after we have attached
553 * all our child devices, but on many systems it works here.
554 */
555 flags = 0;
556 if (testenv("debug.acpi.avoid"))
557 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
558
559 /* Bring the hardware and basic handlers online. */
560 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
561 device_printf(dev, "Could not enable ACPI: %s\n",
562 AcpiFormatException(status));
563 goto out;
564 }
565
566 /*
567 * Call the ECDT probe function to provide EC functionality before
568 * the namespace has been evaluated.
569 *
570 * XXX This happens before the sysresource devices have been probed and
571 * attached so its resources come from nexus0. In practice, this isn't
572 * a problem but should be addressed eventually.
573 */
574 acpi_ec_ecdt_probe(dev);
575
576 /* Bring device objects and regions online. */
577 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
578 device_printf(dev, "Could not initialize ACPI objects: %s\n",
579 AcpiFormatException(status));
580 goto out;
581 }
582
583 /*
584 * Setup our sysctl tree.
585 *
586 * XXX: This doesn't check to make sure that none of these fail.
587 */
588 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
589 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
590 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, device_get_name(dev),
591 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
592 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
593 OID_AUTO, "supported_sleep_state",
594 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
595 0, 0, acpi_supported_sleep_state_sysctl, "A",
596 "List supported ACPI sleep states.");
597 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
598 OID_AUTO, "power_button_state",
599 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
600 &sc->acpi_power_button_stype, 0, acpi_stype_sysctl, "A",
601 "Power button ACPI sleep state.");
602 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
603 OID_AUTO, "sleep_button_state",
604 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
605 &sc->acpi_sleep_button_stype, 0, acpi_stype_sysctl, "A",
606 "Sleep button ACPI sleep state.");
607 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
608 OID_AUTO, "lid_switch_state",
609 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
610 &sc->acpi_lid_switch_stype, 0, acpi_stype_sysctl, "A",
611 "Lid ACPI sleep state. Set to s2idle or s2mem if you want to suspend "
612 "your laptop when close the lid.");
613 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
614 OID_AUTO, "standby_state",
615 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
616 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A",
617 "ACPI Sx state to use when going standby (S1 or S2).");
618 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
619 OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
620 "sleep delay in seconds");
621 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
622 OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0,
623 "Use S4BIOS when hibernating.");
624 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
625 OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
626 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
627 OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
628 &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
629 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
630 OID_AUTO, "handle_reboot", CTLFLAG_RW,
631 &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
632
633 #if defined(__amd64__) || defined(__i386__)
634 /*
635 * Enable workaround for incorrect ISA IRQ polarity by default on
636 * systems with Intel CPUs.
637 */
638 if (cpu_vendor_id == CPU_VENDOR_INTEL)
639 acpi_override_isa_irq_polarity = 1;
640 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
641 OID_AUTO, "override_isa_irq_polarity", CTLFLAG_RDTUN,
642 &acpi_override_isa_irq_polarity, 0,
643 "Force active-hi polarity for edge-triggered ISA IRQs");
644 #endif
645
646 /*
647 * Default to 1 second before sleeping to give some machines time to
648 * stabilize.
649 */
650 sc->acpi_sleep_delay = 1;
651 if (bootverbose)
652 sc->acpi_verbose = 1;
653 if ((env = kern_getenv("hw.acpi.verbose")) != NULL) {
654 if (strcmp(env, "0") != 0)
655 sc->acpi_verbose = 1;
656 freeenv(env);
657 }
658
659 /* Only enable reboot by default if the FADT says it is available. */
660 if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER)
661 sc->acpi_handle_reboot = 1;
662
663 #if !ACPI_REDUCED_HARDWARE
664 /* Only enable S4BIOS by default if the FACS says it is available. */
665 if (AcpiGbl_FACS != NULL && AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
666 sc->acpi_s4bios = 1;
667 #endif
668
669 /*
670 * Probe all supported ACPI sleep states. Awake (S0) is always supported.
671 */
672 acpi_supported_sstates[ACPI_STATE_S0] = TRUE;
673 acpi_supported_stypes[POWER_STYPE_AWAKE] = true;
674 for (state = ACPI_STATE_S1; state <= ACPI_STATE_S5; state++)
675 if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT,
676 __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) &&
677 ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
678 acpi_supported_sstates[state] = TRUE;
679 acpi_supported_stypes[acpi_sstate_to_stype(state)] = true;
680 }
681
682 /*
683 * Dispatch the default sleep type to devices. The lid switch is set
684 * to UNKNOWN by default to avoid surprising users.
685 */
686 sc->acpi_power_button_stype = acpi_supported_stypes[POWER_STYPE_POWEROFF] ?
687 POWER_STYPE_POWEROFF : POWER_STYPE_UNKNOWN;
688 sc->acpi_lid_switch_stype = POWER_STYPE_UNKNOWN;
689
690 sc->acpi_standby_sx = ACPI_STATE_UNKNOWN;
691 if (acpi_supported_sstates[ACPI_STATE_S1])
692 sc->acpi_standby_sx = ACPI_STATE_S1;
693 else if (acpi_supported_sstates[ACPI_STATE_S2])
694 sc->acpi_standby_sx = ACPI_STATE_S2;
695
696 /* Pick the first valid sleep type for the sleep button default. */
697 sc->acpi_sleep_button_stype = POWER_STYPE_UNKNOWN;
698 for (stype = POWER_STYPE_STANDBY; stype <= POWER_STYPE_HIBERNATE; stype++)
699 if (acpi_supported_stypes[stype]) {
700 sc->acpi_sleep_button_stype = stype;
701 break;
702 }
703
704 acpi_enable_fixed_events(sc);
705
706 /*
707 * Scan the namespace and attach/initialise children.
708 */
709
710 /* Register our shutdown handler. */
711 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
712 SHUTDOWN_PRI_LAST + 150);
713
714 /*
715 * Register our acpi event handlers.
716 * XXX should be configurable eg. via userland policy manager.
717 */
718 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
719 sc, ACPI_EVENT_PRI_LAST);
720 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
721 sc, ACPI_EVENT_PRI_LAST);
722
723 /* Flag our initial states. */
724 sc->acpi_enabled = TRUE;
725 sc->acpi_stype = POWER_STYPE_AWAKE;
726 sc->acpi_sleep_disabled = TRUE;
727
728 /* Create the control device */
729 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664,
730 "acpi");
731 sc->acpi_dev_t->si_drv1 = sc;
732
733 if ((error = acpi_machdep_init(dev)))
734 goto out;
735
736 /* Register ACPI again to pass the correct argument of pm_func. */
737 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc,
738 acpi_supported_stypes);
739
740 acpi_platform_osc(dev);
741
742 if (!acpi_disabled("bus")) {
743 EVENTHANDLER_REGISTER(dev_lookup, acpi_lookup, NULL, 1000);
744 acpi_probe_children(dev);
745 }
746
747 /* Update all GPEs and enable runtime GPEs. */
748 status = AcpiUpdateAllGpes();
749 if (ACPI_FAILURE(status))
750 device_printf(dev, "Could not update all GPEs: %s\n",
751 AcpiFormatException(status));
752
753 /* Allow sleep request after a while. */
754 callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0);
755 callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
756 acpi_sleep_enable, sc);
757
758 error = 0;
759
760 out:
761 return_VALUE (error);
762 }
763
764 static int
acpi_stype_to_sstate(struct acpi_softc * sc,enum power_stype stype)765 acpi_stype_to_sstate(struct acpi_softc *sc, enum power_stype stype)
766 {
767 switch (stype) {
768 case POWER_STYPE_AWAKE:
769 return (ACPI_STATE_S0);
770 case POWER_STYPE_STANDBY:
771 return (sc->acpi_standby_sx);
772 case POWER_STYPE_SUSPEND_TO_MEM:
773 return (ACPI_STATE_S3);
774 case POWER_STYPE_HIBERNATE:
775 return (ACPI_STATE_S4);
776 case POWER_STYPE_POWEROFF:
777 return (ACPI_STATE_S5);
778 case POWER_STYPE_SUSPEND_TO_IDLE:
779 case POWER_STYPE_COUNT:
780 case POWER_STYPE_UNKNOWN:
781 return (ACPI_STATE_UNKNOWN);
782 }
783 return (ACPI_STATE_UNKNOWN);
784 }
785
786 /*
787 * XXX It would be nice if we didn't need this function, but we'd need
788 * acpi_EnterSleepState and acpi_ReqSleepState to take in actual ACPI S-states,
789 * which won't be possible at the moment because suspend-to-idle (which is not
790 * an ACPI S-state nor maps to one) will be implemented here.
791 *
792 * In the future, we should make generic a lot of the logic in these functions
793 * to enable suspend-to-idle on non-ACPI builds, and then make
794 * acpi_EnterSleepState and acpi_ReqSleepState truly take in ACPI S-states
795 * again.
796 */
797 static enum power_stype
acpi_sstate_to_stype(int sstate)798 acpi_sstate_to_stype(int sstate)
799 {
800 switch (sstate) {
801 case ACPI_STATE_S0:
802 return (POWER_STYPE_AWAKE);
803 case ACPI_STATE_S1:
804 case ACPI_STATE_S2:
805 return (POWER_STYPE_STANDBY);
806 case ACPI_STATE_S3:
807 return (POWER_STYPE_SUSPEND_TO_MEM);
808 case ACPI_STATE_S4:
809 return (POWER_STYPE_HIBERNATE);
810 case ACPI_STATE_S5:
811 return (POWER_STYPE_POWEROFF);
812 }
813 return (POWER_STYPE_UNKNOWN);
814 }
815
816 static void
acpi_set_power_children(device_t dev,int state)817 acpi_set_power_children(device_t dev, int state)
818 {
819 device_t child;
820 device_t *devlist;
821 int dstate, i, numdevs;
822
823 if (device_get_children(dev, &devlist, &numdevs) != 0)
824 return;
825
826 /*
827 * Retrieve and set D-state for the sleep state if _SxD is present.
828 * Skip children who aren't attached since they are handled separately.
829 */
830 for (i = 0; i < numdevs; i++) {
831 child = devlist[i];
832 dstate = state;
833 if (device_is_attached(child) &&
834 acpi_device_pwr_for_sleep(dev, child, &dstate) == 0)
835 acpi_set_powerstate(child, dstate);
836 }
837 free(devlist, M_TEMP);
838 }
839
840 static int
acpi_suspend(device_t dev)841 acpi_suspend(device_t dev)
842 {
843 int error;
844
845 bus_topo_assert();
846
847 error = bus_generic_suspend(dev);
848 if (error == 0)
849 acpi_set_power_children(dev, ACPI_STATE_D3);
850
851 return (error);
852 }
853
854 static int
acpi_resume(device_t dev)855 acpi_resume(device_t dev)
856 {
857
858 bus_topo_assert();
859
860 acpi_set_power_children(dev, ACPI_STATE_D0);
861
862 return (bus_generic_resume(dev));
863 }
864
865 static int
acpi_shutdown(device_t dev)866 acpi_shutdown(device_t dev)
867 {
868
869 bus_topo_assert();
870
871 /* Allow children to shutdown first. */
872 bus_generic_shutdown(dev);
873
874 /*
875 * Enable any GPEs that are able to power-on the system (i.e., RTC).
876 * Also, disable any that are not valid for this state (most).
877 */
878 acpi_wake_prep_walk(ACPI_STATE_S5);
879
880 return (0);
881 }
882
883 /*
884 * Handle a new device being added
885 */
886 static device_t
acpi_add_child(device_t bus,u_int order,const char * name,int unit)887 acpi_add_child(device_t bus, u_int order, const char *name, int unit)
888 {
889 struct acpi_device *ad;
890 device_t child;
891
892 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
893 return (NULL);
894
895 ad->ad_domain = ACPI_DEV_DOMAIN_UNKNOWN;
896 resource_list_init(&ad->ad_rl);
897
898 child = device_add_child_ordered(bus, order, name, unit);
899 if (child != NULL)
900 device_set_ivars(child, ad);
901 else
902 free(ad, M_ACPIDEV);
903 return (child);
904 }
905
906 static int
acpi_print_child(device_t bus,device_t child)907 acpi_print_child(device_t bus, device_t child)
908 {
909 struct acpi_device *adev = device_get_ivars(child);
910 struct resource_list *rl = &adev->ad_rl;
911 int retval = 0;
912
913 retval += bus_print_child_header(bus, child);
914 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
915 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
916 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
917 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd");
918 if (device_get_flags(child))
919 retval += printf(" flags %#x", device_get_flags(child));
920 retval += bus_print_child_domain(bus, child);
921 retval += bus_print_child_footer(bus, child);
922
923 return (retval);
924 }
925
926 /*
927 * If this device is an ACPI child but no one claimed it, attempt
928 * to power it off. We'll power it back up when a driver is added.
929 *
930 * XXX Disabled for now since many necessary devices (like fdc and
931 * ATA) don't claim the devices we created for them but still expect
932 * them to be powered up.
933 */
934 static void
acpi_probe_nomatch(device_t bus,device_t child)935 acpi_probe_nomatch(device_t bus, device_t child)
936 {
937 #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
938 acpi_set_powerstate(child, ACPI_STATE_D3);
939 #endif
940 }
941
942 /*
943 * If a new driver has a chance to probe a child, first power it up.
944 *
945 * XXX Disabled for now (see acpi_probe_nomatch for details).
946 */
947 static void
acpi_driver_added(device_t dev,driver_t * driver)948 acpi_driver_added(device_t dev, driver_t *driver)
949 {
950 device_t child, *devlist;
951 int i, numdevs;
952
953 DEVICE_IDENTIFY(driver, dev);
954 if (device_get_children(dev, &devlist, &numdevs))
955 return;
956 for (i = 0; i < numdevs; i++) {
957 child = devlist[i];
958 if (device_get_state(child) == DS_NOTPRESENT) {
959 #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
960 acpi_set_powerstate(child, ACPI_STATE_D0);
961 if (device_probe_and_attach(child) != 0)
962 acpi_set_powerstate(child, ACPI_STATE_D3);
963 #else
964 device_probe_and_attach(child);
965 #endif
966 }
967 }
968 free(devlist, M_TEMP);
969 }
970
971 /* Location hint for devctl(8) */
972 static int
acpi_child_location_method(device_t cbdev,device_t child,struct sbuf * sb)973 acpi_child_location_method(device_t cbdev, device_t child, struct sbuf *sb)
974 {
975 struct acpi_device *dinfo = device_get_ivars(child);
976 int pxm;
977
978 if (dinfo->ad_handle) {
979 sbuf_printf(sb, "handle=%s", acpi_name(dinfo->ad_handle));
980 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) {
981 sbuf_printf(sb, " _PXM=%d", pxm);
982 }
983 }
984 return (0);
985 }
986
987 /* PnP information for devctl(8) */
988 int
acpi_pnpinfo(ACPI_HANDLE handle,struct sbuf * sb)989 acpi_pnpinfo(ACPI_HANDLE handle, struct sbuf *sb)
990 {
991 ACPI_DEVICE_INFO *adinfo;
992
993 if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &adinfo))) {
994 sbuf_printf(sb, "unknown");
995 return (0);
996 }
997
998 sbuf_printf(sb, "_HID=%s _UID=%lu _CID=%s",
999 (adinfo->Valid & ACPI_VALID_HID) ?
1000 adinfo->HardwareId.String : "none",
1001 (adinfo->Valid & ACPI_VALID_UID) ?
1002 strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL,
1003 ((adinfo->Valid & ACPI_VALID_CID) &&
1004 adinfo->CompatibleIdList.Count > 0) ?
1005 adinfo->CompatibleIdList.Ids[0].String : "none");
1006 AcpiOsFree(adinfo);
1007
1008 return (0);
1009 }
1010
1011 static int
acpi_child_pnpinfo_method(device_t cbdev,device_t child,struct sbuf * sb)1012 acpi_child_pnpinfo_method(device_t cbdev, device_t child, struct sbuf *sb)
1013 {
1014 struct acpi_device *dinfo = device_get_ivars(child);
1015
1016 return (acpi_pnpinfo(dinfo->ad_handle, sb));
1017 }
1018
1019 /*
1020 * Note: the check for ACPI locator may be redundant. However, this routine is
1021 * suitable for both busses whose only locator is ACPI and as a building block
1022 * for busses that have multiple locators to cope with.
1023 */
1024 int
acpi_get_acpi_device_path(device_t bus,device_t child,const char * locator,struct sbuf * sb)1025 acpi_get_acpi_device_path(device_t bus, device_t child, const char *locator, struct sbuf *sb)
1026 {
1027 if (strcmp(locator, BUS_LOCATOR_ACPI) == 0) {
1028 ACPI_HANDLE *handle = acpi_get_handle(child);
1029
1030 if (handle != NULL)
1031 sbuf_printf(sb, "%s", acpi_name(handle));
1032 return (0);
1033 }
1034
1035 return (bus_generic_get_device_path(bus, child, locator, sb));
1036 }
1037
1038 static int
acpi_get_device_path(device_t bus,device_t child,const char * locator,struct sbuf * sb)1039 acpi_get_device_path(device_t bus, device_t child, const char *locator, struct sbuf *sb)
1040 {
1041 struct acpi_device *dinfo = device_get_ivars(child);
1042
1043 if (strcmp(locator, BUS_LOCATOR_ACPI) == 0)
1044 return (acpi_get_acpi_device_path(bus, child, locator, sb));
1045
1046 if (strcmp(locator, BUS_LOCATOR_UEFI) == 0) {
1047 ACPI_DEVICE_INFO *adinfo;
1048 if (!ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo)) &&
1049 dinfo->ad_handle != 0 && (adinfo->Valid & ACPI_VALID_HID)) {
1050 const char *hid = adinfo->HardwareId.String;
1051 u_long uid = (adinfo->Valid & ACPI_VALID_UID) ?
1052 strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL;
1053 u_long hidval;
1054
1055 /*
1056 * In UEFI Stanard Version 2.6, Section 9.6.1.6 Text
1057 * Device Node Reference, there's an insanely long table
1058 * 98. This implements the relevant bits from that
1059 * table. Newer versions appear to have not required
1060 * anything new. The EDK2 firmware presents both PciRoot
1061 * and PcieRoot as PciRoot. Follow the EDK2 standard.
1062 */
1063 if (strncmp("PNP", hid, 3) != 0)
1064 goto nomatch;
1065 hidval = strtoul(hid + 3, NULL, 16);
1066 switch (hidval) {
1067 case 0x0301:
1068 sbuf_printf(sb, "Keyboard(0x%lx)", uid);
1069 break;
1070 case 0x0401:
1071 sbuf_printf(sb, "ParallelPort(0x%lx)", uid);
1072 break;
1073 case 0x0501:
1074 sbuf_printf(sb, "Serial(0x%lx)", uid);
1075 break;
1076 case 0x0604:
1077 sbuf_printf(sb, "Floppy(0x%lx)", uid);
1078 break;
1079 case 0x0a03:
1080 case 0x0a08:
1081 sbuf_printf(sb, "PciRoot(0x%lx)", uid);
1082 break;
1083 default: /* Everything else gets a generic encode */
1084 nomatch:
1085 sbuf_printf(sb, "Acpi(%s,0x%lx)", hid, uid);
1086 break;
1087 }
1088 }
1089 /* Not handled: AcpiAdr... unsure how to know it's one */
1090 }
1091
1092 /* For the rest, punt to the default handler */
1093 return (bus_generic_get_device_path(bus, child, locator, sb));
1094 }
1095
1096 /*
1097 * Handle device deletion.
1098 */
1099 static void
acpi_child_deleted(device_t dev,device_t child)1100 acpi_child_deleted(device_t dev, device_t child)
1101 {
1102 struct acpi_device *dinfo = device_get_ivars(child);
1103
1104 if (acpi_get_device(dinfo->ad_handle) == child)
1105 AcpiDetachData(dinfo->ad_handle, acpi_fake_objhandler);
1106 }
1107
1108 /*
1109 * Handle per-device ivars
1110 */
1111 static int
acpi_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)1112 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
1113 {
1114 struct acpi_device *ad;
1115
1116 if ((ad = device_get_ivars(child)) == NULL) {
1117 device_printf(child, "device has no ivars\n");
1118 return (ENOENT);
1119 }
1120
1121 /* ACPI and ISA compatibility ivars */
1122 switch(index) {
1123 case ACPI_IVAR_HANDLE:
1124 *(ACPI_HANDLE *)result = ad->ad_handle;
1125 break;
1126 case ACPI_IVAR_PRIVATE:
1127 *(void **)result = ad->ad_private;
1128 break;
1129 case ACPI_IVAR_FLAGS:
1130 *(int *)result = ad->ad_flags;
1131 break;
1132 case ACPI_IVAR_DOMAIN:
1133 *(int *)result = ad->ad_domain;
1134 break;
1135 case ISA_IVAR_VENDORID:
1136 case ISA_IVAR_SERIAL:
1137 case ISA_IVAR_COMPATID:
1138 *(int *)result = -1;
1139 break;
1140 case ISA_IVAR_LOGICALID:
1141 *(int *)result = acpi_isa_get_logicalid(child);
1142 break;
1143 case PCI_IVAR_CLASS:
1144 *(uint8_t*)result = (ad->ad_cls_class >> 16) & 0xff;
1145 break;
1146 case PCI_IVAR_SUBCLASS:
1147 *(uint8_t*)result = (ad->ad_cls_class >> 8) & 0xff;
1148 break;
1149 case PCI_IVAR_PROGIF:
1150 *(uint8_t*)result = (ad->ad_cls_class >> 0) & 0xff;
1151 break;
1152 default:
1153 return (ENOENT);
1154 }
1155
1156 return (0);
1157 }
1158
1159 static int
acpi_write_ivar(device_t dev,device_t child,int index,uintptr_t value)1160 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
1161 {
1162 struct acpi_device *ad;
1163
1164 if ((ad = device_get_ivars(child)) == NULL) {
1165 device_printf(child, "device has no ivars\n");
1166 return (ENOENT);
1167 }
1168
1169 switch(index) {
1170 case ACPI_IVAR_HANDLE:
1171 ad->ad_handle = (ACPI_HANDLE)value;
1172 break;
1173 case ACPI_IVAR_PRIVATE:
1174 ad->ad_private = (void *)value;
1175 break;
1176 case ACPI_IVAR_FLAGS:
1177 ad->ad_flags = (int)value;
1178 break;
1179 case ACPI_IVAR_DOMAIN:
1180 ad->ad_domain = (int)value;
1181 break;
1182 default:
1183 panic("bad ivar write request (%d)", index);
1184 return (ENOENT);
1185 }
1186
1187 return (0);
1188 }
1189
1190 /*
1191 * Handle child resource allocation/removal
1192 */
1193 static struct resource_list *
acpi_get_rlist(device_t dev,device_t child)1194 acpi_get_rlist(device_t dev, device_t child)
1195 {
1196 struct acpi_device *ad;
1197
1198 ad = device_get_ivars(child);
1199 return (&ad->ad_rl);
1200 }
1201
1202 static int
acpi_match_resource_hint(device_t dev,int type,long value)1203 acpi_match_resource_hint(device_t dev, int type, long value)
1204 {
1205 struct acpi_device *ad = device_get_ivars(dev);
1206 struct resource_list *rl = &ad->ad_rl;
1207 struct resource_list_entry *rle;
1208
1209 STAILQ_FOREACH(rle, rl, link) {
1210 if (rle->type != type)
1211 continue;
1212 if (rle->start <= value && rle->end >= value)
1213 return (1);
1214 }
1215 return (0);
1216 }
1217
1218 /*
1219 * Does this device match because the resources match?
1220 */
1221 static bool
acpi_hint_device_matches_resources(device_t child,const char * name,int unit)1222 acpi_hint_device_matches_resources(device_t child, const char *name,
1223 int unit)
1224 {
1225 long value;
1226 bool matches;
1227
1228 /*
1229 * Check for matching resources. We must have at least one match.
1230 * Since I/O and memory resources cannot be shared, if we get a
1231 * match on either of those, ignore any mismatches in IRQs or DRQs.
1232 *
1233 * XXX: We may want to revisit this to be more lenient and wire
1234 * as long as it gets one match.
1235 */
1236 matches = false;
1237 if (resource_long_value(name, unit, "port", &value) == 0) {
1238 /*
1239 * Floppy drive controllers are notorious for having a
1240 * wide variety of resources not all of which include the
1241 * first port that is specified by the hint (typically
1242 * 0x3f0) (see the comment above fdc_isa_alloc_resources()
1243 * in fdc_isa.c). However, they do all seem to include
1244 * port + 2 (e.g. 0x3f2) so for a floppy device, look for
1245 * 'value + 2' in the port resources instead of the hint
1246 * value.
1247 */
1248 if (strcmp(name, "fdc") == 0)
1249 value += 2;
1250 if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
1251 matches = true;
1252 else
1253 return false;
1254 }
1255 if (resource_long_value(name, unit, "maddr", &value) == 0) {
1256 if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
1257 matches = true;
1258 else
1259 return false;
1260 }
1261
1262 /*
1263 * If either the I/O address and/or the memory address matched, then
1264 * assumed this devices matches and that any mismatch in other resources
1265 * will be resolved by siltently ignoring those other resources. Otherwise
1266 * all further resources must match.
1267 */
1268 if (matches) {
1269 return (true);
1270 }
1271 if (resource_long_value(name, unit, "irq", &value) == 0) {
1272 if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
1273 matches = true;
1274 else
1275 return false;
1276 }
1277 if (resource_long_value(name, unit, "drq", &value) == 0) {
1278 if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
1279 matches = true;
1280 else
1281 return false;
1282 }
1283 return matches;
1284 }
1285
1286
1287 /*
1288 * Wire device unit numbers based on resource matches in hints.
1289 */
1290 static void
acpi_hint_device_unit(device_t acdev,device_t child,const char * name,int * unitp)1291 acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
1292 int *unitp)
1293 {
1294 device_location_cache_t *cache;
1295 const char *s;
1296 int line, unit;
1297 bool matches;
1298
1299 /*
1300 * Iterate over all the hints for the devices with the specified
1301 * name to see if one's resources are a subset of this device.
1302 */
1303 line = 0;
1304 cache = dev_wired_cache_init();
1305 while (resource_find_dev(&line, name, &unit, "at", NULL) == 0) {
1306 /* Must have an "at" for acpi or isa. */
1307 resource_string_value(name, unit, "at", &s);
1308 matches = false;
1309 if (strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
1310 strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)
1311 matches = acpi_hint_device_matches_resources(child, name, unit);
1312 else
1313 matches = dev_wired_cache_match(cache, child, s);
1314
1315 if (matches) {
1316 /* We have a winner! */
1317 *unitp = unit;
1318 break;
1319 }
1320 }
1321 dev_wired_cache_fini(cache);
1322 }
1323
1324 /*
1325 * Fetch the NUMA domain for a device by mapping the value returned by
1326 * _PXM to a NUMA domain. If the device does not have a _PXM method,
1327 * -2 is returned. If any other error occurs, -1 is returned.
1328 */
1329 int
acpi_pxm_parse(device_t dev)1330 acpi_pxm_parse(device_t dev)
1331 {
1332 #ifdef NUMA
1333 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1334 ACPI_HANDLE handle;
1335 ACPI_STATUS status;
1336 int pxm;
1337
1338 handle = acpi_get_handle(dev);
1339 if (handle == NULL)
1340 return (-2);
1341 status = acpi_GetInteger(handle, "_PXM", &pxm);
1342 if (ACPI_SUCCESS(status))
1343 return (acpi_map_pxm_to_vm_domainid(pxm));
1344 if (status == AE_NOT_FOUND)
1345 return (-2);
1346 #endif
1347 #endif
1348 return (-1);
1349 }
1350
1351 int
acpi_get_cpus(device_t dev,device_t child,enum cpu_sets op,size_t setsize,cpuset_t * cpuset)1352 acpi_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
1353 cpuset_t *cpuset)
1354 {
1355 int d, error;
1356
1357 d = acpi_pxm_parse(child);
1358 if (d < 0)
1359 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
1360
1361 switch (op) {
1362 case LOCAL_CPUS:
1363 if (setsize != sizeof(cpuset_t))
1364 return (EINVAL);
1365 *cpuset = cpuset_domain[d];
1366 return (0);
1367 case INTR_CPUS:
1368 error = bus_generic_get_cpus(dev, child, op, setsize, cpuset);
1369 if (error != 0)
1370 return (error);
1371 if (setsize != sizeof(cpuset_t))
1372 return (EINVAL);
1373 CPU_AND(cpuset, cpuset, &cpuset_domain[d]);
1374 return (0);
1375 default:
1376 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
1377 }
1378 }
1379
1380 static int
acpi_get_domain_method(device_t dev,device_t child,int * domain)1381 acpi_get_domain_method(device_t dev, device_t child, int *domain)
1382 {
1383 int error;
1384
1385 error = acpi_read_ivar(dev, child, ACPI_IVAR_DOMAIN,
1386 (uintptr_t *)domain);
1387 if (error == 0 && *domain != ACPI_DEV_DOMAIN_UNKNOWN)
1388 return (0);
1389 return (ENOENT);
1390 }
1391
1392 static struct rman *
acpi_get_rman(device_t bus,int type,u_int flags)1393 acpi_get_rman(device_t bus, int type, u_int flags)
1394 {
1395 /* Only memory and IO resources are managed. */
1396 switch (type) {
1397 case SYS_RES_IOPORT:
1398 return (&acpi_rman_io);
1399 case SYS_RES_MEMORY:
1400 return (&acpi_rman_mem);
1401 default:
1402 return (NULL);
1403 }
1404 }
1405
1406 /*
1407 * Pre-allocate/manage all memory and IO resources. Since rman can't handle
1408 * duplicates, we merge any in the sysresource attach routine.
1409 */
1410 static int
acpi_sysres_alloc(device_t dev)1411 acpi_sysres_alloc(device_t dev)
1412 {
1413 struct acpi_softc *sc = device_get_softc(dev);
1414 struct resource *res;
1415 struct resource_list_entry *rle;
1416 struct rman *rm;
1417 device_t *children;
1418 int child_count, i;
1419
1420 /*
1421 * Probe/attach any sysresource devices. This would be unnecessary if we
1422 * had multi-pass probe/attach.
1423 */
1424 if (device_get_children(dev, &children, &child_count) != 0)
1425 return (ENXIO);
1426 for (i = 0; i < child_count; i++) {
1427 if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1428 device_probe_and_attach(children[i]);
1429 }
1430 free(children, M_TEMP);
1431
1432 STAILQ_FOREACH(rle, &sc->sysres_rl, link) {
1433 if (rle->res != NULL) {
1434 device_printf(dev, "duplicate resource for %jx\n", rle->start);
1435 continue;
1436 }
1437
1438 /* Only memory and IO resources are valid here. */
1439 rm = acpi_get_rman(dev, rle->type, 0);
1440 if (rm == NULL)
1441 continue;
1442
1443 /* Pre-allocate resource and add to our rman pool. */
1444 res = bus_alloc_resource(dev, rle->type,
1445 &rle->rid, rle->start, rle->start + rle->count - 1, rle->count,
1446 RF_ACTIVE | RF_UNMAPPED);
1447 if (res != NULL) {
1448 rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1449 rle->res = res;
1450 } else if (bootverbose)
1451 device_printf(dev, "reservation of %jx, %jx (%d) failed\n",
1452 rle->start, rle->count, rle->type);
1453 }
1454 return (0);
1455 }
1456
1457 /*
1458 * Reserve declared resources for active devices found during the
1459 * namespace scan once the boot-time attach of devices has completed.
1460 *
1461 * Ideally reserving firmware-assigned resources would work in a
1462 * depth-first traversal of the device namespace, but this is
1463 * complicated. In particular, not all resources are enumerated by
1464 * ACPI (e.g. PCI bridges and devices enumerate their resources via
1465 * other means). Some systems also enumerate devices via ACPI behind
1466 * PCI bridges but without a matching a PCI device_t enumerated via
1467 * PCI bus scanning, the device_t's end up as direct children of
1468 * acpi0. Doing this scan late is not ideal, but works for now.
1469 */
1470 static void
acpi_reserve_resources(device_t dev)1471 acpi_reserve_resources(device_t dev)
1472 {
1473 struct resource_list_entry *rle;
1474 struct resource_list *rl;
1475 struct acpi_device *ad;
1476 device_t *children;
1477 int child_count, i;
1478
1479 if (device_get_children(dev, &children, &child_count) != 0)
1480 return;
1481 for (i = 0; i < child_count; i++) {
1482 ad = device_get_ivars(children[i]);
1483 rl = &ad->ad_rl;
1484
1485 /* Don't reserve system resources. */
1486 if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1487 continue;
1488
1489 STAILQ_FOREACH(rle, rl, link) {
1490 /*
1491 * Don't reserve IRQ resources. There are many sticky things
1492 * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET
1493 * when using legacy routing).
1494 */
1495 if (rle->type == SYS_RES_IRQ)
1496 continue;
1497
1498 /*
1499 * Don't reserve the resource if it is already allocated.
1500 * The acpi_ec(4) driver can allocate its resources early
1501 * if ECDT is present.
1502 */
1503 if (rle->res != NULL)
1504 continue;
1505
1506 /*
1507 * Try to reserve the resource from our parent. If this
1508 * fails because the resource is a system resource, just
1509 * let it be. The resource range is already reserved so
1510 * that other devices will not use it. If the driver
1511 * needs to allocate the resource, then
1512 * acpi_alloc_resource() will sub-alloc from the system
1513 * resource.
1514 */
1515 resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid,
1516 rle->start, rle->end, rle->count, 0);
1517 }
1518 }
1519 free(children, M_TEMP);
1520 }
1521
1522 static int
acpi_set_resource(device_t dev,device_t child,int type,int rid,rman_res_t start,rman_res_t count)1523 acpi_set_resource(device_t dev, device_t child, int type, int rid,
1524 rman_res_t start, rman_res_t count)
1525 {
1526 struct acpi_device *ad = device_get_ivars(child);
1527 struct resource_list *rl = &ad->ad_rl;
1528 rman_res_t end;
1529
1530 #ifdef INTRNG
1531 /* map with default for now */
1532 if (type == SYS_RES_IRQ)
1533 start = (rman_res_t)acpi_map_intr(child, (u_int)start,
1534 acpi_get_handle(child));
1535 #endif
1536
1537 /* If the resource is already allocated, fail. */
1538 if (resource_list_busy(rl, type, rid))
1539 return (EBUSY);
1540
1541 /* If the resource is already reserved, release it. */
1542 if (resource_list_reserved(rl, type, rid))
1543 resource_list_unreserve(rl, dev, child, type, rid);
1544
1545 /* Add the resource. */
1546 end = (start + count - 1);
1547 resource_list_add(rl, type, rid, start, end, count);
1548 return (0);
1549 }
1550
1551 static struct resource *
acpi_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)1552 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
1553 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1554 {
1555 #ifndef INTRNG
1556 ACPI_RESOURCE ares;
1557 #endif
1558 struct acpi_device *ad;
1559 struct resource_list_entry *rle;
1560 struct resource_list *rl;
1561 struct resource *res;
1562 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
1563
1564 /*
1565 * First attempt at allocating the resource. For direct children,
1566 * use resource_list_alloc() to handle reserved resources. For
1567 * other devices, pass the request up to our parent.
1568 */
1569 if (bus == device_get_parent(child)) {
1570 ad = device_get_ivars(child);
1571 rl = &ad->ad_rl;
1572
1573 /*
1574 * Simulate the behavior of the ISA bus for direct children
1575 * devices. That is, if a non-default range is specified for
1576 * a resource that doesn't exist, use bus_set_resource() to
1577 * add the resource before allocating it. Note that these
1578 * resources will not be reserved.
1579 */
1580 if (!isdefault && resource_list_find(rl, type, *rid) == NULL)
1581 resource_list_add(rl, type, *rid, start, end, count);
1582 res = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
1583 flags);
1584 #ifndef INTRNG
1585 if (res != NULL && type == SYS_RES_IRQ) {
1586 /*
1587 * Since bus_config_intr() takes immediate effect, we cannot
1588 * configure the interrupt associated with a device when we
1589 * parse the resources but have to defer it until a driver
1590 * actually allocates the interrupt via bus_alloc_resource().
1591 *
1592 * XXX: Should we handle the lookup failing?
1593 */
1594 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
1595 acpi_config_intr(child, &ares);
1596 }
1597 #endif
1598
1599 /*
1600 * If this is an allocation of the "default" range for a given
1601 * RID, fetch the exact bounds for this resource from the
1602 * resource list entry to try to allocate the range from the
1603 * system resource regions.
1604 */
1605 if (res == NULL && isdefault) {
1606 rle = resource_list_find(rl, type, *rid);
1607 if (rle != NULL) {
1608 start = rle->start;
1609 end = rle->end;
1610 count = rle->count;
1611 }
1612 }
1613 } else
1614 res = bus_generic_alloc_resource(bus, child, type, rid,
1615 start, end, count, flags);
1616
1617 /*
1618 * If the first attempt failed and this is an allocation of a
1619 * specific range, try to satisfy the request via a suballocation
1620 * from our system resource regions.
1621 */
1622 if (res == NULL && start + count - 1 == end)
1623 res = bus_generic_rman_alloc_resource(bus, child, type, rid, start, end,
1624 count, flags);
1625 return (res);
1626 }
1627
1628 static bool
acpi_is_resource_managed(device_t bus,struct resource * r)1629 acpi_is_resource_managed(device_t bus, struct resource *r)
1630 {
1631 struct rman *rm;
1632
1633 rm = acpi_get_rman(bus, rman_get_type(r), rman_get_flags(r));
1634 if (rm == NULL)
1635 return (false);
1636 return (rman_is_region_manager(r, rm));
1637 }
1638
1639 static struct resource *
acpi_managed_resource(device_t bus,struct resource * r)1640 acpi_managed_resource(device_t bus, struct resource *r)
1641 {
1642 struct acpi_softc *sc = device_get_softc(bus);
1643 struct resource_list_entry *rle;
1644
1645 KASSERT(acpi_is_resource_managed(bus, r),
1646 ("resource %p is not suballocated", r));
1647
1648 STAILQ_FOREACH(rle, &sc->sysres_rl, link) {
1649 if (rle->type != rman_get_type(r) || rle->res == NULL)
1650 continue;
1651 if (rman_get_start(r) >= rman_get_start(rle->res) &&
1652 rman_get_end(r) <= rman_get_end(rle->res))
1653 return (rle->res);
1654 }
1655 return (NULL);
1656 }
1657
1658 static int
acpi_adjust_resource(device_t bus,device_t child,struct resource * r,rman_res_t start,rman_res_t end)1659 acpi_adjust_resource(device_t bus, device_t child, struct resource *r,
1660 rman_res_t start, rman_res_t end)
1661 {
1662
1663 if (acpi_is_resource_managed(bus, r))
1664 return (rman_adjust_resource(r, start, end));
1665 return (bus_generic_adjust_resource(bus, child, r, start, end));
1666 }
1667
1668 static int
acpi_release_resource(device_t bus,device_t child,struct resource * r)1669 acpi_release_resource(device_t bus, device_t child, struct resource *r)
1670 {
1671 /*
1672 * If this resource belongs to one of our internal managers,
1673 * deactivate it and release it to the local pool.
1674 */
1675 if (acpi_is_resource_managed(bus, r))
1676 return (bus_generic_rman_release_resource(bus, child, r));
1677
1678 return (bus_generic_rl_release_resource(bus, child, r));
1679 }
1680
1681 static void
acpi_delete_resource(device_t bus,device_t child,int type,int rid)1682 acpi_delete_resource(device_t bus, device_t child, int type, int rid)
1683 {
1684 struct resource_list *rl;
1685
1686 rl = acpi_get_rlist(bus, child);
1687 if (resource_list_busy(rl, type, rid)) {
1688 device_printf(bus, "delete_resource: Resource still owned by child"
1689 " (type=%d, rid=%d)\n", type, rid);
1690 return;
1691 }
1692 if (resource_list_reserved(rl, type, rid))
1693 resource_list_unreserve(rl, bus, child, type, rid);
1694 resource_list_delete(rl, type, rid);
1695 }
1696
1697 static int
acpi_activate_resource(device_t bus,device_t child,struct resource * r)1698 acpi_activate_resource(device_t bus, device_t child, struct resource *r)
1699 {
1700 if (acpi_is_resource_managed(bus, r))
1701 return (bus_generic_rman_activate_resource(bus, child, r));
1702 return (bus_generic_activate_resource(bus, child, r));
1703 }
1704
1705 static int
acpi_deactivate_resource(device_t bus,device_t child,struct resource * r)1706 acpi_deactivate_resource(device_t bus, device_t child, struct resource *r)
1707 {
1708 if (acpi_is_resource_managed(bus, r))
1709 return (bus_generic_rman_deactivate_resource(bus, child, r));
1710 return (bus_generic_deactivate_resource(bus, child, r));
1711 }
1712
1713 static int
acpi_map_resource(device_t bus,device_t child,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)1714 acpi_map_resource(device_t bus, device_t child, struct resource *r,
1715 struct resource_map_request *argsp, struct resource_map *map)
1716 {
1717 struct resource_map_request args;
1718 struct resource *sysres;
1719 rman_res_t length, start;
1720 int error;
1721
1722 if (!acpi_is_resource_managed(bus, r))
1723 return (bus_generic_map_resource(bus, child, r, argsp, map));
1724
1725 /* Resources must be active to be mapped. */
1726 if (!(rman_get_flags(r) & RF_ACTIVE))
1727 return (ENXIO);
1728
1729 resource_init_map_request(&args);
1730 error = resource_validate_map_request(r, argsp, &args, &start, &length);
1731 if (error)
1732 return (error);
1733
1734 sysres = acpi_managed_resource(bus, r);
1735 if (sysres == NULL)
1736 return (ENOENT);
1737
1738 args.offset = start - rman_get_start(sysres);
1739 args.length = length;
1740 return (bus_map_resource(bus, sysres, &args, map));
1741 }
1742
1743 static int
acpi_unmap_resource(device_t bus,device_t child,struct resource * r,struct resource_map * map)1744 acpi_unmap_resource(device_t bus, device_t child, struct resource *r,
1745 struct resource_map *map)
1746 {
1747 struct resource *sysres;
1748
1749 if (!acpi_is_resource_managed(bus, r))
1750 return (bus_generic_unmap_resource(bus, child, r, map));
1751
1752 sysres = acpi_managed_resource(bus, r);
1753 if (sysres == NULL)
1754 return (ENOENT);
1755 return (bus_unmap_resource(bus, sysres, map));
1756 }
1757
1758 /* Allocate an IO port or memory resource, given its GAS. */
1759 int
acpi_bus_alloc_gas(device_t dev,int * type,int * rid,ACPI_GENERIC_ADDRESS * gas,struct resource ** res,u_int flags)1760 acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1761 struct resource **res, u_int flags)
1762 {
1763 int error, res_type;
1764
1765 error = ENOMEM;
1766 if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1767 return (EINVAL);
1768
1769 /* We only support memory and IO spaces. */
1770 switch (gas->SpaceId) {
1771 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1772 res_type = SYS_RES_MEMORY;
1773 break;
1774 case ACPI_ADR_SPACE_SYSTEM_IO:
1775 res_type = SYS_RES_IOPORT;
1776 break;
1777 default:
1778 return (EOPNOTSUPP);
1779 }
1780
1781 /*
1782 * If the register width is less than 8, assume the BIOS author means
1783 * it is a bit field and just allocate a byte.
1784 */
1785 if (gas->BitWidth && gas->BitWidth < 8)
1786 gas->BitWidth = 8;
1787
1788 /* Validate the address after we're sure we support the space. */
1789 if (gas->Address == 0 || gas->BitWidth == 0)
1790 return (EINVAL);
1791
1792 bus_set_resource(dev, res_type, *rid, gas->Address,
1793 gas->BitWidth / 8);
1794 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1795 if (*res != NULL) {
1796 *type = res_type;
1797 error = 0;
1798 } else
1799 bus_delete_resource(dev, res_type, *rid);
1800
1801 return (error);
1802 }
1803
1804 /* Probe _HID and _CID for compatible ISA PNP ids. */
1805 static uint32_t
acpi_isa_get_logicalid(device_t dev)1806 acpi_isa_get_logicalid(device_t dev)
1807 {
1808 ACPI_DEVICE_INFO *devinfo;
1809 ACPI_HANDLE h;
1810 uint32_t pnpid;
1811
1812 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1813
1814 /* Fetch and validate the HID. */
1815 if ((h = acpi_get_handle(dev)) == NULL ||
1816 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1817 return_VALUE (0);
1818
1819 pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
1820 devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
1821 PNP_EISAID(devinfo->HardwareId.String) : 0;
1822 AcpiOsFree(devinfo);
1823
1824 return_VALUE (pnpid);
1825 }
1826
1827 static int
acpi_isa_get_compatid(device_t dev,uint32_t * cids,int count)1828 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1829 {
1830 ACPI_DEVICE_INFO *devinfo;
1831 ACPI_PNP_DEVICE_ID *ids;
1832 ACPI_HANDLE h;
1833 uint32_t *pnpid;
1834 int i, valid;
1835
1836 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1837
1838 pnpid = cids;
1839
1840 /* Fetch and validate the CID */
1841 if ((h = acpi_get_handle(dev)) == NULL ||
1842 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1843 return_VALUE (0);
1844
1845 if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
1846 AcpiOsFree(devinfo);
1847 return_VALUE (0);
1848 }
1849
1850 if (devinfo->CompatibleIdList.Count < count)
1851 count = devinfo->CompatibleIdList.Count;
1852 ids = devinfo->CompatibleIdList.Ids;
1853 for (i = 0, valid = 0; i < count; i++)
1854 if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
1855 strncmp(ids[i].String, "PNP", 3) == 0) {
1856 *pnpid++ = PNP_EISAID(ids[i].String);
1857 valid++;
1858 }
1859 AcpiOsFree(devinfo);
1860
1861 return_VALUE (valid);
1862 }
1863
1864 static int
acpi_device_id_probe(device_t bus,device_t dev,char ** ids,char ** match)1865 acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match)
1866 {
1867 ACPI_HANDLE h;
1868 ACPI_OBJECT_TYPE t;
1869 int rv;
1870 int i;
1871
1872 h = acpi_get_handle(dev);
1873 if (ids == NULL || h == NULL)
1874 return (ENXIO);
1875 t = acpi_get_type(dev);
1876 if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
1877 return (ENXIO);
1878
1879 /* Try to match one of the array of IDs with a HID or CID. */
1880 for (i = 0; ids[i] != NULL; i++) {
1881 rv = acpi_MatchHid(h, ids[i]);
1882 if (rv == ACPI_MATCHHID_NOMATCH)
1883 continue;
1884
1885 if (match != NULL) {
1886 *match = ids[i];
1887 }
1888 return ((rv == ACPI_MATCHHID_HID)?
1889 BUS_PROBE_DEFAULT : BUS_PROBE_LOW_PRIORITY);
1890 }
1891 return (ENXIO);
1892 }
1893
1894 static ACPI_STATUS
acpi_device_eval_obj(device_t bus,device_t dev,ACPI_STRING pathname,ACPI_OBJECT_LIST * parameters,ACPI_BUFFER * ret)1895 acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1896 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1897 {
1898 ACPI_HANDLE h;
1899
1900 if (dev == NULL)
1901 h = ACPI_ROOT_OBJECT;
1902 else if ((h = acpi_get_handle(dev)) == NULL)
1903 return (AE_BAD_PARAMETER);
1904 return (AcpiEvaluateObject(h, pathname, parameters, ret));
1905 }
1906
1907 static ACPI_STATUS
acpi_device_get_prop(device_t bus,device_t dev,ACPI_STRING propname,const ACPI_OBJECT ** value)1908 acpi_device_get_prop(device_t bus, device_t dev, ACPI_STRING propname,
1909 const ACPI_OBJECT **value)
1910 {
1911 const ACPI_OBJECT *pkg, *name, *val;
1912 struct acpi_device *ad;
1913 ACPI_STATUS status;
1914 int i;
1915
1916 ad = device_get_ivars(dev);
1917
1918 if (ad == NULL || propname == NULL)
1919 return (AE_BAD_PARAMETER);
1920 if (ad->dsd_pkg == NULL) {
1921 if (ad->dsd.Pointer == NULL) {
1922 status = acpi_find_dsd(ad);
1923 if (ACPI_FAILURE(status))
1924 return (status);
1925 } else {
1926 return (AE_NOT_FOUND);
1927 }
1928 }
1929
1930 for (i = 0; i < ad->dsd_pkg->Package.Count; i ++) {
1931 pkg = &ad->dsd_pkg->Package.Elements[i];
1932 if (pkg->Type != ACPI_TYPE_PACKAGE || pkg->Package.Count != 2)
1933 continue;
1934
1935 name = &pkg->Package.Elements[0];
1936 val = &pkg->Package.Elements[1];
1937 if (name->Type != ACPI_TYPE_STRING)
1938 continue;
1939 if (strncmp(propname, name->String.Pointer, name->String.Length) == 0) {
1940 if (value != NULL)
1941 *value = val;
1942
1943 return (AE_OK);
1944 }
1945 }
1946
1947 return (AE_NOT_FOUND);
1948 }
1949
1950 static ACPI_STATUS
acpi_find_dsd(struct acpi_device * ad)1951 acpi_find_dsd(struct acpi_device *ad)
1952 {
1953 const ACPI_OBJECT *dsd, *guid, *pkg;
1954 ACPI_STATUS status;
1955
1956 ad->dsd.Length = ACPI_ALLOCATE_BUFFER;
1957 ad->dsd.Pointer = NULL;
1958 ad->dsd_pkg = NULL;
1959
1960 status = AcpiEvaluateObject(ad->ad_handle, "_DSD", NULL, &ad->dsd);
1961 if (ACPI_FAILURE(status))
1962 return (status);
1963
1964 dsd = ad->dsd.Pointer;
1965 guid = &dsd->Package.Elements[0];
1966 pkg = &dsd->Package.Elements[1];
1967
1968 if (guid->Type != ACPI_TYPE_BUFFER || pkg->Type != ACPI_TYPE_PACKAGE ||
1969 guid->Buffer.Length != sizeof(acpi_dsd_uuid))
1970 return (AE_NOT_FOUND);
1971 if (memcmp(guid->Buffer.Pointer, &acpi_dsd_uuid,
1972 sizeof(acpi_dsd_uuid)) == 0) {
1973
1974 ad->dsd_pkg = pkg;
1975 return (AE_OK);
1976 }
1977
1978 return (AE_NOT_FOUND);
1979 }
1980
1981 static ssize_t
acpi_bus_get_prop_handle(const ACPI_OBJECT * hobj,void * propvalue,size_t size)1982 acpi_bus_get_prop_handle(const ACPI_OBJECT *hobj, void *propvalue, size_t size)
1983 {
1984 ACPI_OBJECT *pobj;
1985 ACPI_HANDLE h;
1986
1987 if (hobj->Type != ACPI_TYPE_PACKAGE)
1988 goto err;
1989 if (hobj->Package.Count != 1)
1990 goto err;
1991
1992 pobj = &hobj->Package.Elements[0];
1993 if (pobj == NULL)
1994 goto err;
1995 if (pobj->Type != ACPI_TYPE_LOCAL_REFERENCE)
1996 goto err;
1997
1998 h = acpi_GetReference(NULL, pobj);
1999 if (h == NULL)
2000 goto err;
2001
2002 if (propvalue != NULL && size >= sizeof(ACPI_HANDLE))
2003 *(ACPI_HANDLE *)propvalue = h;
2004 return (sizeof(ACPI_HANDLE));
2005
2006 err:
2007 return (-1);
2008 }
2009
2010 static ssize_t
acpi_bus_get_prop(device_t bus,device_t child,const char * propname,void * propvalue,size_t size,device_property_type_t type)2011 acpi_bus_get_prop(device_t bus, device_t child, const char *propname,
2012 void *propvalue, size_t size, device_property_type_t type)
2013 {
2014 ACPI_STATUS status;
2015 const ACPI_OBJECT *obj;
2016
2017 status = acpi_device_get_prop(bus, child, __DECONST(char *, propname),
2018 &obj);
2019 if (ACPI_FAILURE(status))
2020 return (-1);
2021
2022 switch (type) {
2023 case DEVICE_PROP_ANY:
2024 case DEVICE_PROP_BUFFER:
2025 case DEVICE_PROP_UINT32:
2026 case DEVICE_PROP_UINT64:
2027 break;
2028 case DEVICE_PROP_HANDLE:
2029 return (acpi_bus_get_prop_handle(obj, propvalue, size));
2030 default:
2031 return (-1);
2032 }
2033
2034 switch (obj->Type) {
2035 case ACPI_TYPE_INTEGER:
2036 if (type == DEVICE_PROP_UINT32) {
2037 if (propvalue != NULL && size >= sizeof(uint32_t))
2038 *((uint32_t *)propvalue) = obj->Integer.Value;
2039 return (sizeof(uint32_t));
2040 }
2041 if (propvalue != NULL && size >= sizeof(uint64_t))
2042 *((uint64_t *) propvalue) = obj->Integer.Value;
2043 return (sizeof(uint64_t));
2044
2045 case ACPI_TYPE_STRING:
2046 if (type != DEVICE_PROP_ANY &&
2047 type != DEVICE_PROP_BUFFER)
2048 return (-1);
2049
2050 if (propvalue != NULL && size > 0)
2051 memcpy(propvalue, obj->String.Pointer,
2052 MIN(size, obj->String.Length));
2053 return (obj->String.Length);
2054
2055 case ACPI_TYPE_BUFFER:
2056 if (propvalue != NULL && size > 0)
2057 memcpy(propvalue, obj->Buffer.Pointer,
2058 MIN(size, obj->Buffer.Length));
2059 return (obj->Buffer.Length);
2060
2061 case ACPI_TYPE_PACKAGE:
2062 if (propvalue != NULL && size >= sizeof(ACPI_OBJECT *)) {
2063 *((ACPI_OBJECT **) propvalue) =
2064 __DECONST(ACPI_OBJECT *, obj);
2065 }
2066 return (sizeof(ACPI_OBJECT *));
2067
2068 case ACPI_TYPE_LOCAL_REFERENCE:
2069 if (propvalue != NULL && size >= sizeof(ACPI_HANDLE)) {
2070 ACPI_HANDLE h;
2071
2072 h = acpi_GetReference(NULL,
2073 __DECONST(ACPI_OBJECT *, obj));
2074 memcpy(propvalue, h, sizeof(ACPI_HANDLE));
2075 }
2076 return (sizeof(ACPI_HANDLE));
2077 default:
2078 return (0);
2079 }
2080 }
2081
2082 int
acpi_device_pwr_for_sleep(device_t bus,device_t dev,int * dstate)2083 acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
2084 {
2085 struct acpi_softc *sc;
2086 ACPI_HANDLE handle;
2087 ACPI_STATUS status;
2088 char sxd[8];
2089
2090 handle = acpi_get_handle(dev);
2091
2092 /*
2093 * XXX If we find these devices, don't try to power them down.
2094 * The serial and IRDA ports on my T23 hang the system when
2095 * set to D3 and it appears that such legacy devices may
2096 * need special handling in their drivers.
2097 */
2098 if (dstate == NULL || handle == NULL ||
2099 acpi_MatchHid(handle, "PNP0500") ||
2100 acpi_MatchHid(handle, "PNP0501") ||
2101 acpi_MatchHid(handle, "PNP0502") ||
2102 acpi_MatchHid(handle, "PNP0510") ||
2103 acpi_MatchHid(handle, "PNP0511"))
2104 return (ENXIO);
2105
2106 /*
2107 * Override next state with the value from _SxD, if present.
2108 * Note illegal _S0D is evaluated because some systems expect this.
2109 */
2110 sc = device_get_softc(bus);
2111 snprintf(sxd, sizeof(sxd), "_S%dD", acpi_stype_to_sstate(sc, sc->acpi_stype));
2112 status = acpi_GetInteger(handle, sxd, dstate);
2113 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
2114 device_printf(dev, "failed to get %s on %s: %s\n", sxd,
2115 acpi_name(handle), AcpiFormatException(status));
2116 return (ENXIO);
2117 }
2118
2119 return (0);
2120 }
2121
2122 /* Callback arg for our implementation of walking the namespace. */
2123 struct acpi_device_scan_ctx {
2124 acpi_scan_cb_t user_fn;
2125 void *arg;
2126 ACPI_HANDLE parent;
2127 };
2128
2129 static ACPI_STATUS
acpi_device_scan_cb(ACPI_HANDLE h,UINT32 level,void * arg,void ** retval)2130 acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
2131 {
2132 struct acpi_device_scan_ctx *ctx;
2133 device_t dev, old_dev;
2134 ACPI_STATUS status;
2135 ACPI_OBJECT_TYPE type;
2136
2137 /*
2138 * Skip this device if we think we'll have trouble with it or it is
2139 * the parent where the scan began.
2140 */
2141 ctx = (struct acpi_device_scan_ctx *)arg;
2142 if (acpi_avoid(h) || h == ctx->parent)
2143 return (AE_OK);
2144
2145 /* If this is not a valid device type (e.g., a method), skip it. */
2146 if (ACPI_FAILURE(AcpiGetType(h, &type)))
2147 return (AE_OK);
2148 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
2149 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
2150 return (AE_OK);
2151
2152 /*
2153 * Call the user function with the current device. If it is unchanged
2154 * afterwards, return. Otherwise, we update the handle to the new dev.
2155 */
2156 old_dev = acpi_get_device(h);
2157 dev = old_dev;
2158 status = ctx->user_fn(h, &dev, level, ctx->arg);
2159 if (ACPI_FAILURE(status) || old_dev == dev)
2160 return (status);
2161
2162 /* Remove the old child and its connection to the handle. */
2163 if (old_dev != NULL)
2164 device_delete_child(device_get_parent(old_dev), old_dev);
2165
2166 /* Recreate the handle association if the user created a device. */
2167 if (dev != NULL)
2168 AcpiAttachData(h, acpi_fake_objhandler, dev);
2169
2170 return (AE_OK);
2171 }
2172
2173 static ACPI_STATUS
acpi_device_scan_children(device_t bus,device_t dev,int max_depth,acpi_scan_cb_t user_fn,void * arg)2174 acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
2175 acpi_scan_cb_t user_fn, void *arg)
2176 {
2177 ACPI_HANDLE h;
2178 struct acpi_device_scan_ctx ctx;
2179
2180 if (acpi_disabled("children"))
2181 return (AE_OK);
2182
2183 if (dev == NULL)
2184 h = ACPI_ROOT_OBJECT;
2185 else if ((h = acpi_get_handle(dev)) == NULL)
2186 return (AE_BAD_PARAMETER);
2187 ctx.user_fn = user_fn;
2188 ctx.arg = arg;
2189 ctx.parent = h;
2190 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
2191 acpi_device_scan_cb, NULL, &ctx, NULL));
2192 }
2193
2194 /*
2195 * Even though ACPI devices are not PCI, we use the PCI approach for setting
2196 * device power states since it's close enough to ACPI.
2197 */
2198 int
acpi_set_powerstate(device_t child,int state)2199 acpi_set_powerstate(device_t child, int state)
2200 {
2201 ACPI_HANDLE h;
2202 ACPI_STATUS status;
2203
2204 h = acpi_get_handle(child);
2205 if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
2206 return (EINVAL);
2207 if (h == NULL)
2208 return (0);
2209
2210 /* Ignore errors if the power methods aren't present. */
2211 status = acpi_pwr_switch_consumer(h, state);
2212 if (ACPI_SUCCESS(status)) {
2213 if (bootverbose)
2214 device_printf(child, "set ACPI power state %s on %s\n",
2215 acpi_d_state_to_str(state), acpi_name(h));
2216 } else if (status != AE_NOT_FOUND)
2217 device_printf(child,
2218 "failed to set ACPI power state %s on %s: %s\n",
2219 acpi_d_state_to_str(state), acpi_name(h),
2220 AcpiFormatException(status));
2221
2222 return (0);
2223 }
2224
2225 static int
acpi_isa_pnp_probe(device_t bus,device_t child,struct isa_pnp_id * ids)2226 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
2227 {
2228 int result, cid_count, i;
2229 uint32_t lid, cids[8];
2230
2231 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2232
2233 /*
2234 * ISA-style drivers attached to ACPI may persist and
2235 * probe manually if we return ENOENT. We never want
2236 * that to happen, so don't ever return it.
2237 */
2238 result = ENXIO;
2239
2240 /* Scan the supplied IDs for a match */
2241 lid = acpi_isa_get_logicalid(child);
2242 cid_count = acpi_isa_get_compatid(child, cids, 8);
2243 while (ids && ids->ip_id) {
2244 if (lid == ids->ip_id) {
2245 result = 0;
2246 goto out;
2247 }
2248 for (i = 0; i < cid_count; i++) {
2249 if (cids[i] == ids->ip_id) {
2250 result = 0;
2251 goto out;
2252 }
2253 }
2254 ids++;
2255 }
2256
2257 out:
2258 if (result == 0 && ids->ip_desc)
2259 device_set_desc(child, ids->ip_desc);
2260
2261 return_VALUE (result);
2262 }
2263
2264 /*
2265 * Look for a MCFG table. If it is present, use the settings for
2266 * domain (segment) 0 to setup PCI config space access via the memory
2267 * map.
2268 *
2269 * On non-x86 architectures (arm64 for now), this will be done from the
2270 * PCI host bridge driver.
2271 */
2272 static void
acpi_enable_pcie(void)2273 acpi_enable_pcie(void)
2274 {
2275 #if defined(__i386__) || defined(__amd64__)
2276 ACPI_TABLE_HEADER *hdr;
2277 ACPI_MCFG_ALLOCATION *alloc, *end;
2278 ACPI_STATUS status;
2279
2280 status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
2281 if (ACPI_FAILURE(status))
2282 return;
2283
2284 end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
2285 alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
2286 while (alloc < end) {
2287 pcie_cfgregopen(alloc->Address, alloc->PciSegment,
2288 alloc->StartBusNumber, alloc->EndBusNumber);
2289 alloc++;
2290 }
2291 #endif
2292 }
2293
2294 static void
acpi_platform_osc(device_t dev)2295 acpi_platform_osc(device_t dev)
2296 {
2297 ACPI_HANDLE sb_handle;
2298 ACPI_STATUS status;
2299 uint32_t cap_set[2];
2300
2301 /* 0811B06E-4A27-44F9-8D60-3CBBC22E7B48 */
2302 static uint8_t acpi_platform_uuid[ACPI_UUID_LENGTH] = {
2303 0x6e, 0xb0, 0x11, 0x08, 0x27, 0x4a, 0xf9, 0x44,
2304 0x8d, 0x60, 0x3c, 0xbb, 0xc2, 0x2e, 0x7b, 0x48
2305 };
2306
2307 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2308 return;
2309
2310 cap_set[1] = 0x10; /* APEI Support */
2311 status = acpi_EvaluateOSC(sb_handle, acpi_platform_uuid, 1,
2312 nitems(cap_set), cap_set, cap_set, false);
2313 if (ACPI_FAILURE(status)) {
2314 if (status == AE_NOT_FOUND)
2315 return;
2316 device_printf(dev, "_OSC failed: %s\n",
2317 AcpiFormatException(status));
2318 return;
2319 }
2320 }
2321
2322 /*
2323 * Scan all of the ACPI namespace and attach child devices.
2324 *
2325 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
2326 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
2327 * However, in violation of the spec, some systems place their PCI link
2328 * devices in \, so we have to walk the whole namespace. We check the
2329 * type of namespace nodes, so this should be ok.
2330 */
2331 static void
acpi_probe_children(device_t bus)2332 acpi_probe_children(device_t bus)
2333 {
2334
2335 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2336
2337 /*
2338 * Scan the namespace and insert placeholders for all the devices that
2339 * we find. We also probe/attach any early devices.
2340 *
2341 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
2342 * we want to create nodes for all devices, not just those that are
2343 * currently present. (This assumes that we don't want to create/remove
2344 * devices as they appear, which might be smarter.)
2345 */
2346 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
2347 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
2348 NULL, bus, NULL);
2349
2350 /* Pre-allocate resources for our rman from any sysresource devices. */
2351 acpi_sysres_alloc(bus);
2352
2353 /* Create any static children by calling device identify methods. */
2354 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
2355 bus_identify_children(bus);
2356
2357 /* Probe/attach all children, created statically and from the namespace. */
2358 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_attach_children\n"));
2359 bus_attach_children(bus);
2360
2361 /*
2362 * Reserve resources allocated to children but not yet allocated
2363 * by a driver.
2364 */
2365 acpi_reserve_resources(bus);
2366
2367 /* Attach wake sysctls. */
2368 acpi_wake_sysctl_walk(bus);
2369
2370 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
2371 return_VOID;
2372 }
2373
2374 /*
2375 * Determine the probe order for a given device.
2376 */
2377 static void
acpi_probe_order(ACPI_HANDLE handle,int * order)2378 acpi_probe_order(ACPI_HANDLE handle, int *order)
2379 {
2380 ACPI_OBJECT_TYPE type;
2381
2382 /*
2383 * 0. CPUs
2384 * 1. I/O port and memory system resource holders
2385 * 2. Clocks and timers (to handle early accesses)
2386 * 3. Embedded controllers (to handle early accesses)
2387 * 4. PCI Link Devices
2388 */
2389 AcpiGetType(handle, &type);
2390 if (type == ACPI_TYPE_PROCESSOR)
2391 *order = 0;
2392 else if (acpi_MatchHid(handle, "PNP0C01") ||
2393 acpi_MatchHid(handle, "PNP0C02"))
2394 *order = 1;
2395 else if (acpi_MatchHid(handle, "PNP0100") ||
2396 acpi_MatchHid(handle, "PNP0103") ||
2397 acpi_MatchHid(handle, "PNP0B00"))
2398 *order = 2;
2399 else if (acpi_MatchHid(handle, "PNP0C09"))
2400 *order = 3;
2401 else if (acpi_MatchHid(handle, "PNP0C0F"))
2402 *order = 4;
2403 }
2404
2405 /*
2406 * Evaluate a child device and determine whether we might attach a device to
2407 * it.
2408 */
2409 static ACPI_STATUS
acpi_probe_child(ACPI_HANDLE handle,UINT32 level,void * context,void ** status)2410 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2411 {
2412 ACPI_DEVICE_INFO *devinfo;
2413 struct acpi_device *ad;
2414 struct acpi_prw_data prw;
2415 ACPI_OBJECT_TYPE type;
2416 ACPI_HANDLE h;
2417 device_t bus, child;
2418 char *handle_str;
2419 int d, order;
2420
2421 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2422
2423 if (acpi_disabled("children"))
2424 return_ACPI_STATUS (AE_OK);
2425
2426 /* Skip this device if we think we'll have trouble with it. */
2427 if (acpi_avoid(handle))
2428 return_ACPI_STATUS (AE_OK);
2429
2430 bus = (device_t)context;
2431 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
2432 handle_str = acpi_name(handle);
2433 switch (type) {
2434 case ACPI_TYPE_DEVICE:
2435 /*
2436 * Since we scan from \, be sure to skip system scope objects.
2437 * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around
2438 * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run
2439 * during the initialization and \_TZ_ is to support Notify() on it.
2440 */
2441 if (strcmp(handle_str, "\\_SB_") == 0 ||
2442 strcmp(handle_str, "\\_TZ_") == 0)
2443 break;
2444 if (acpi_parse_prw(handle, &prw) == 0)
2445 AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit);
2446
2447 /*
2448 * Ignore devices that do not have a _HID or _CID. They should
2449 * be discovered by other buses (e.g. the PCI bus driver).
2450 */
2451 if (!acpi_has_hid(handle))
2452 break;
2453 /* FALLTHROUGH */
2454 case ACPI_TYPE_PROCESSOR:
2455 case ACPI_TYPE_THERMAL:
2456 case ACPI_TYPE_POWER:
2457 /*
2458 * Create a placeholder device for this node. Sort the
2459 * placeholder so that the probe/attach passes will run
2460 * breadth-first. Orders less than ACPI_DEV_BASE_ORDER
2461 * are reserved for special objects (i.e., system
2462 * resources).
2463 */
2464 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
2465 order = level * 10 + ACPI_DEV_BASE_ORDER;
2466 acpi_probe_order(handle, &order);
2467 child = BUS_ADD_CHILD(bus, order, NULL, DEVICE_UNIT_ANY);
2468 if (child == NULL)
2469 break;
2470
2471 /* Associate the handle with the device_t and vice versa. */
2472 acpi_set_handle(child, handle);
2473 AcpiAttachData(handle, acpi_fake_objhandler, child);
2474
2475 /*
2476 * Check that the device is present. If it's not present,
2477 * leave it disabled (so that we have a device_t attached to
2478 * the handle, but we don't probe it).
2479 *
2480 * XXX PCI link devices sometimes report "present" but not
2481 * "functional" (i.e. if disabled). Go ahead and probe them
2482 * anyway since we may enable them later.
2483 */
2484 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
2485 /* Never disable PCI link devices. */
2486 if (acpi_MatchHid(handle, "PNP0C0F"))
2487 break;
2488
2489 /*
2490 * RTC Device should be enabled for CMOS register space
2491 * unless FADT indicate it is not present.
2492 * (checked in RTC probe routine.)
2493 */
2494 if (acpi_MatchHid(handle, "PNP0B00"))
2495 break;
2496
2497 /*
2498 * Docking stations should remain enabled since the system
2499 * may be undocked at boot.
2500 */
2501 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
2502 break;
2503
2504 device_disable(child);
2505 break;
2506 }
2507
2508 /*
2509 * Get the device's resource settings and attach them.
2510 * Note that if the device has _PRS but no _CRS, we need
2511 * to decide when it's appropriate to try to configure the
2512 * device. Ignore the return value here; it's OK for the
2513 * device not to have any resources.
2514 */
2515 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
2516
2517 ad = device_get_ivars(child);
2518 ad->ad_cls_class = 0xffffff;
2519 if (ACPI_SUCCESS(AcpiGetObjectInfo(handle, &devinfo))) {
2520 if ((devinfo->Valid & ACPI_VALID_CLS) != 0 &&
2521 devinfo->ClassCode.Length >= ACPI_PCICLS_STRING_SIZE) {
2522 ad->ad_cls_class = strtoul(devinfo->ClassCode.String,
2523 NULL, 16);
2524 }
2525 AcpiOsFree(devinfo);
2526 }
2527
2528 d = acpi_pxm_parse(child);
2529 if (d >= 0)
2530 ad->ad_domain = d;
2531 break;
2532 }
2533 }
2534
2535 return_ACPI_STATUS (AE_OK);
2536 }
2537
2538 /*
2539 * AcpiAttachData() requires an object handler but never uses it. This is a
2540 * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
2541 */
2542 void
acpi_fake_objhandler(ACPI_HANDLE h,void * data)2543 acpi_fake_objhandler(ACPI_HANDLE h, void *data)
2544 {
2545 }
2546
2547 static void
acpi_shutdown_final(void * arg,int howto)2548 acpi_shutdown_final(void *arg, int howto)
2549 {
2550 struct acpi_softc *sc = (struct acpi_softc *)arg;
2551 register_t intr;
2552 ACPI_STATUS status;
2553
2554 /*
2555 * XXX Shutdown code should only run on the BSP (cpuid 0).
2556 * Some chipsets do not power off the system correctly if called from
2557 * an AP.
2558 */
2559 if ((howto & RB_POWEROFF) != 0) {
2560 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
2561 if (ACPI_FAILURE(status)) {
2562 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2563 AcpiFormatException(status));
2564 return;
2565 }
2566 device_printf(sc->acpi_dev, "Powering system off\n");
2567 intr = intr_disable();
2568 status = AcpiEnterSleepState(ACPI_STATE_S5);
2569 if (ACPI_FAILURE(status)) {
2570 intr_restore(intr);
2571 device_printf(sc->acpi_dev, "power-off failed - %s\n",
2572 AcpiFormatException(status));
2573 } else {
2574 DELAY(1000000);
2575 intr_restore(intr);
2576 device_printf(sc->acpi_dev, "power-off failed - timeout\n");
2577 }
2578 } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) {
2579 /* Reboot using the reset register. */
2580 status = AcpiReset();
2581 if (ACPI_SUCCESS(status)) {
2582 DELAY(1000000);
2583 device_printf(sc->acpi_dev, "reset failed - timeout\n");
2584 } else if (status != AE_NOT_EXIST)
2585 device_printf(sc->acpi_dev, "reset failed - %s\n",
2586 AcpiFormatException(status));
2587 } else if (sc->acpi_do_disable && !KERNEL_PANICKED()) {
2588 /*
2589 * Only disable ACPI if the user requested. On some systems, writing
2590 * the disable value to SMI_CMD hangs the system.
2591 */
2592 device_printf(sc->acpi_dev, "Shutting down\n");
2593 AcpiTerminate();
2594 }
2595 }
2596
2597 static void
acpi_enable_fixed_events(struct acpi_softc * sc)2598 acpi_enable_fixed_events(struct acpi_softc *sc)
2599 {
2600 static int first_time = 1;
2601
2602 /* Enable and clear fixed events and install handlers. */
2603 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
2604 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
2605 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
2606 acpi_event_power_button_sleep, sc);
2607 if (first_time)
2608 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
2609 }
2610 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
2611 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
2612 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
2613 acpi_event_sleep_button_sleep, sc);
2614 if (first_time)
2615 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
2616 }
2617
2618 first_time = 0;
2619 }
2620
2621 /*
2622 * Returns true if the device is actually present and should
2623 * be attached to. This requires the present, enabled, UI-visible
2624 * and diagnostics-passed bits to be set.
2625 */
2626 BOOLEAN
acpi_DeviceIsPresent(device_t dev)2627 acpi_DeviceIsPresent(device_t dev)
2628 {
2629 ACPI_HANDLE h;
2630 UINT32 s;
2631 ACPI_STATUS status;
2632
2633 h = acpi_get_handle(dev);
2634 if (h == NULL)
2635 return (FALSE);
2636
2637 #ifdef ACPI_EARLY_EPYC_WAR
2638 /*
2639 * Certain Treadripper boards always returns 0 for FreeBSD because it
2640 * only returns non-zero for the OS string "Windows 2015". Otherwise it
2641 * will return zero. Force them to always be treated as present.
2642 * Beata versions were worse: they always returned 0.
2643 */
2644 if (acpi_MatchHid(h, "AMDI0020") || acpi_MatchHid(h, "AMDI0010"))
2645 return (TRUE);
2646 #endif
2647
2648 status = acpi_GetInteger(h, "_STA", &s);
2649
2650 /*
2651 * If no _STA method or if it failed, then assume that
2652 * the device is present.
2653 */
2654 if (ACPI_FAILURE(status))
2655 return (TRUE);
2656
2657 return (ACPI_DEVICE_PRESENT(s) ? TRUE : FALSE);
2658 }
2659
2660 /*
2661 * Returns true if the battery is actually present and inserted.
2662 */
2663 BOOLEAN
acpi_BatteryIsPresent(device_t dev)2664 acpi_BatteryIsPresent(device_t dev)
2665 {
2666 ACPI_HANDLE h;
2667 UINT32 s;
2668 ACPI_STATUS status;
2669
2670 h = acpi_get_handle(dev);
2671 if (h == NULL)
2672 return (FALSE);
2673 status = acpi_GetInteger(h, "_STA", &s);
2674
2675 /*
2676 * If no _STA method or if it failed, then assume that
2677 * the device is present.
2678 */
2679 if (ACPI_FAILURE(status))
2680 return (TRUE);
2681
2682 return (ACPI_BATTERY_PRESENT(s) ? TRUE : FALSE);
2683 }
2684
2685 /*
2686 * Returns true if a device has at least one valid device ID.
2687 */
2688 BOOLEAN
acpi_has_hid(ACPI_HANDLE h)2689 acpi_has_hid(ACPI_HANDLE h)
2690 {
2691 ACPI_DEVICE_INFO *devinfo;
2692 BOOLEAN ret;
2693
2694 if (h == NULL ||
2695 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2696 return (FALSE);
2697
2698 ret = FALSE;
2699 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
2700 ret = TRUE;
2701 else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2702 if (devinfo->CompatibleIdList.Count > 0)
2703 ret = TRUE;
2704
2705 AcpiOsFree(devinfo);
2706 return (ret);
2707 }
2708
2709 /*
2710 * Match a HID string against a handle
2711 * returns ACPI_MATCHHID_HID if _HID match
2712 * ACPI_MATCHHID_CID if _CID match and not _HID match.
2713 * ACPI_MATCHHID_NOMATCH=0 if no match.
2714 */
2715 int
acpi_MatchHid(ACPI_HANDLE h,const char * hid)2716 acpi_MatchHid(ACPI_HANDLE h, const char *hid)
2717 {
2718 ACPI_DEVICE_INFO *devinfo;
2719 BOOLEAN ret;
2720 int i;
2721
2722 if (hid == NULL || h == NULL ||
2723 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2724 return (ACPI_MATCHHID_NOMATCH);
2725
2726 ret = ACPI_MATCHHID_NOMATCH;
2727 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
2728 strcmp(hid, devinfo->HardwareId.String) == 0)
2729 ret = ACPI_MATCHHID_HID;
2730 else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2731 for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
2732 if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
2733 ret = ACPI_MATCHHID_CID;
2734 break;
2735 }
2736 }
2737
2738 AcpiOsFree(devinfo);
2739 return (ret);
2740 }
2741
2742 /*
2743 * Return the handle of a named object within our scope, ie. that of (parent)
2744 * or one if its parents.
2745 */
2746 ACPI_STATUS
acpi_GetHandleInScope(ACPI_HANDLE parent,char * path,ACPI_HANDLE * result)2747 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
2748 {
2749 ACPI_HANDLE r;
2750 ACPI_STATUS status;
2751
2752 /* Walk back up the tree to the root */
2753 for (;;) {
2754 status = AcpiGetHandle(parent, path, &r);
2755 if (ACPI_SUCCESS(status)) {
2756 *result = r;
2757 return (AE_OK);
2758 }
2759 /* XXX Return error here? */
2760 if (status != AE_NOT_FOUND)
2761 return (AE_OK);
2762 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
2763 return (AE_NOT_FOUND);
2764 parent = r;
2765 }
2766 }
2767
2768 ACPI_STATUS
acpi_GetProperty(device_t dev,ACPI_STRING propname,const ACPI_OBJECT ** value)2769 acpi_GetProperty(device_t dev, ACPI_STRING propname,
2770 const ACPI_OBJECT **value)
2771 {
2772 device_t bus = device_get_parent(dev);
2773
2774 return (ACPI_GET_PROPERTY(bus, dev, propname, value));
2775 }
2776
2777 /*
2778 * Allocate a buffer with a preset data size.
2779 */
2780 ACPI_BUFFER *
acpi_AllocBuffer(int size)2781 acpi_AllocBuffer(int size)
2782 {
2783 ACPI_BUFFER *buf;
2784
2785 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2786 return (NULL);
2787 buf->Length = size;
2788 buf->Pointer = (void *)(buf + 1);
2789 return (buf);
2790 }
2791
2792 ACPI_STATUS
acpi_SetInteger(ACPI_HANDLE handle,char * path,UINT32 number)2793 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2794 {
2795 ACPI_OBJECT arg1;
2796 ACPI_OBJECT_LIST args;
2797
2798 arg1.Type = ACPI_TYPE_INTEGER;
2799 arg1.Integer.Value = number;
2800 args.Count = 1;
2801 args.Pointer = &arg1;
2802
2803 return (AcpiEvaluateObject(handle, path, &args, NULL));
2804 }
2805
2806 /*
2807 * Evaluate a path that should return an integer.
2808 */
2809 ACPI_STATUS
acpi_GetInteger(ACPI_HANDLE handle,char * path,UINT32 * number)2810 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2811 {
2812 ACPI_STATUS status;
2813 ACPI_BUFFER buf;
2814 ACPI_OBJECT param;
2815
2816 if (handle == NULL)
2817 handle = ACPI_ROOT_OBJECT;
2818
2819 /*
2820 * Assume that what we've been pointed at is an Integer object, or
2821 * a method that will return an Integer.
2822 */
2823 buf.Pointer = ¶m;
2824 buf.Length = sizeof(param);
2825 status = AcpiEvaluateObject(handle, path, NULL, &buf);
2826 if (ACPI_SUCCESS(status)) {
2827 if (param.Type == ACPI_TYPE_INTEGER)
2828 *number = param.Integer.Value;
2829 else
2830 status = AE_TYPE;
2831 }
2832
2833 /*
2834 * In some applications, a method that's expected to return an Integer
2835 * may instead return a Buffer (probably to simplify some internal
2836 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
2837 * convert it into an Integer as best we can.
2838 *
2839 * This is a hack.
2840 */
2841 if (status == AE_BUFFER_OVERFLOW) {
2842 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2843 status = AE_NO_MEMORY;
2844 } else {
2845 status = AcpiEvaluateObject(handle, path, NULL, &buf);
2846 if (ACPI_SUCCESS(status))
2847 status = acpi_ConvertBufferToInteger(&buf, number);
2848 AcpiOsFree(buf.Pointer);
2849 }
2850 }
2851 return (status);
2852 }
2853
2854 ACPI_STATUS
acpi_ConvertBufferToInteger(ACPI_BUFFER * bufp,UINT32 * number)2855 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2856 {
2857 ACPI_OBJECT *p;
2858 UINT8 *val;
2859 int i;
2860
2861 p = (ACPI_OBJECT *)bufp->Pointer;
2862 if (p->Type == ACPI_TYPE_INTEGER) {
2863 *number = p->Integer.Value;
2864 return (AE_OK);
2865 }
2866 if (p->Type != ACPI_TYPE_BUFFER)
2867 return (AE_TYPE);
2868 if (p->Buffer.Length > sizeof(int))
2869 return (AE_BAD_DATA);
2870
2871 *number = 0;
2872 val = p->Buffer.Pointer;
2873 for (i = 0; i < p->Buffer.Length; i++)
2874 *number += val[i] << (i * 8);
2875 return (AE_OK);
2876 }
2877
2878 /*
2879 * Iterate over the elements of an a package object, calling the supplied
2880 * function for each element.
2881 *
2882 * XXX possible enhancement might be to abort traversal on error.
2883 */
2884 ACPI_STATUS
acpi_ForeachPackageObject(ACPI_OBJECT * pkg,void (* func)(ACPI_OBJECT * comp,void * arg),void * arg)2885 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2886 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2887 {
2888 ACPI_OBJECT *comp;
2889 int i;
2890
2891 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2892 return (AE_BAD_PARAMETER);
2893
2894 /* Iterate over components */
2895 i = 0;
2896 comp = pkg->Package.Elements;
2897 for (; i < pkg->Package.Count; i++, comp++)
2898 func(comp, arg);
2899
2900 return (AE_OK);
2901 }
2902
2903 /*
2904 * Find the (index)th resource object in a set.
2905 */
2906 ACPI_STATUS
acpi_FindIndexedResource(ACPI_BUFFER * buf,int index,ACPI_RESOURCE ** resp)2907 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
2908 {
2909 ACPI_RESOURCE *rp;
2910 int i;
2911
2912 rp = (ACPI_RESOURCE *)buf->Pointer;
2913 i = index;
2914 while (i-- > 0) {
2915 /* Range check */
2916 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2917 return (AE_BAD_PARAMETER);
2918
2919 /* Check for terminator */
2920 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2921 return (AE_NOT_FOUND);
2922 rp = ACPI_NEXT_RESOURCE(rp);
2923 }
2924 if (resp != NULL)
2925 *resp = rp;
2926
2927 return (AE_OK);
2928 }
2929
2930 /*
2931 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
2932 *
2933 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
2934 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
2935 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
2936 * resources.
2937 */
2938 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
2939
2940 ACPI_STATUS
acpi_AppendBufferResource(ACPI_BUFFER * buf,ACPI_RESOURCE * res)2941 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
2942 {
2943 ACPI_RESOURCE *rp;
2944 void *newp;
2945
2946 /* Initialise the buffer if necessary. */
2947 if (buf->Pointer == NULL) {
2948 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
2949 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
2950 return (AE_NO_MEMORY);
2951 rp = (ACPI_RESOURCE *)buf->Pointer;
2952 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
2953 rp->Length = ACPI_RS_SIZE_MIN;
2954 }
2955 if (res == NULL)
2956 return (AE_OK);
2957
2958 /*
2959 * Scan the current buffer looking for the terminator.
2960 * This will either find the terminator or hit the end
2961 * of the buffer and return an error.
2962 */
2963 rp = (ACPI_RESOURCE *)buf->Pointer;
2964 for (;;) {
2965 /* Range check, don't go outside the buffer */
2966 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2967 return (AE_BAD_PARAMETER);
2968 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2969 break;
2970 rp = ACPI_NEXT_RESOURCE(rp);
2971 }
2972
2973 /*
2974 * Check the size of the buffer and expand if required.
2975 *
2976 * Required size is:
2977 * size of existing resources before terminator +
2978 * size of new resource and header +
2979 * size of terminator.
2980 *
2981 * Note that this loop should really only run once, unless
2982 * for some reason we are stuffing a *really* huge resource.
2983 */
2984 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2985 res->Length + ACPI_RS_SIZE_NO_DATA +
2986 ACPI_RS_SIZE_MIN) >= buf->Length) {
2987 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
2988 return (AE_NO_MEMORY);
2989 bcopy(buf->Pointer, newp, buf->Length);
2990 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2991 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
2992 AcpiOsFree(buf->Pointer);
2993 buf->Pointer = newp;
2994 buf->Length += buf->Length;
2995 }
2996
2997 /* Insert the new resource. */
2998 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
2999
3000 /* And add the terminator. */
3001 rp = ACPI_NEXT_RESOURCE(rp);
3002 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
3003 rp->Length = ACPI_RS_SIZE_MIN;
3004
3005 return (AE_OK);
3006 }
3007
3008 UINT64
acpi_DSMQuery(ACPI_HANDLE h,const uint8_t * uuid,int revision)3009 acpi_DSMQuery(ACPI_HANDLE h, const uint8_t *uuid, int revision)
3010 {
3011 /*
3012 * ACPI spec 9.1.1 defines this.
3013 *
3014 * "Arg2: Function Index Represents a specific function whose meaning is
3015 * specific to the UUID and Revision ID. Function indices should start
3016 * with 1. Function number zero is a query function (see the special
3017 * return code defined below)."
3018 */
3019 ACPI_BUFFER buf;
3020 ACPI_OBJECT *obj;
3021 UINT64 ret = 0;
3022 int i;
3023
3024 if (!ACPI_SUCCESS(acpi_EvaluateDSM(h, uuid, revision, 0, NULL, &buf))) {
3025 ACPI_INFO(("Failed to enumerate DSM functions\n"));
3026 return (0);
3027 }
3028
3029 obj = (ACPI_OBJECT *)buf.Pointer;
3030 KASSERT(obj, ("Object not allowed to be NULL\n"));
3031
3032 /*
3033 * From ACPI 6.2 spec 9.1.1:
3034 * If Function Index = 0, a Buffer containing a function index bitfield.
3035 * Otherwise, the return value and type depends on the UUID and revision
3036 * ID (see below).
3037 */
3038 switch (obj->Type) {
3039 case ACPI_TYPE_BUFFER:
3040 for (i = 0; i < MIN(obj->Buffer.Length, sizeof(ret)); i++)
3041 ret |= (((uint64_t)obj->Buffer.Pointer[i]) << (i * 8));
3042 break;
3043 case ACPI_TYPE_INTEGER:
3044 ACPI_BIOS_WARNING((AE_INFO,
3045 "Possibly buggy BIOS with ACPI_TYPE_INTEGER for function enumeration\n"));
3046 ret = obj->Integer.Value;
3047 break;
3048 default:
3049 ACPI_WARNING((AE_INFO, "Unexpected return type %u\n", obj->Type));
3050 };
3051
3052 AcpiOsFree(obj);
3053 return ret;
3054 }
3055
3056 /*
3057 * DSM may return multiple types depending on the function. It is therefore
3058 * unsafe to use the typed evaluation. It is highly recommended that the caller
3059 * check the type of the returned object.
3060 */
3061 ACPI_STATUS
acpi_EvaluateDSM(ACPI_HANDLE handle,const uint8_t * uuid,int revision,UINT64 function,ACPI_OBJECT * package,ACPI_BUFFER * out_buf)3062 acpi_EvaluateDSM(ACPI_HANDLE handle, const uint8_t *uuid, int revision,
3063 UINT64 function, ACPI_OBJECT *package, ACPI_BUFFER *out_buf)
3064 {
3065 return (acpi_EvaluateDSMTyped(handle, uuid, revision, function,
3066 package, out_buf, ACPI_TYPE_ANY));
3067 }
3068
3069 ACPI_STATUS
acpi_EvaluateDSMTyped(ACPI_HANDLE handle,const uint8_t * uuid,int revision,UINT64 function,ACPI_OBJECT * package,ACPI_BUFFER * out_buf,ACPI_OBJECT_TYPE type)3070 acpi_EvaluateDSMTyped(ACPI_HANDLE handle, const uint8_t *uuid, int revision,
3071 UINT64 function, ACPI_OBJECT *package, ACPI_BUFFER *out_buf,
3072 ACPI_OBJECT_TYPE type)
3073 {
3074 ACPI_OBJECT arg[4];
3075 ACPI_OBJECT_LIST arglist;
3076 ACPI_BUFFER buf;
3077 ACPI_STATUS status;
3078
3079 if (out_buf == NULL)
3080 return (AE_NO_MEMORY);
3081
3082 arg[0].Type = ACPI_TYPE_BUFFER;
3083 arg[0].Buffer.Length = ACPI_UUID_LENGTH;
3084 arg[0].Buffer.Pointer = __DECONST(uint8_t *, uuid);
3085 arg[1].Type = ACPI_TYPE_INTEGER;
3086 arg[1].Integer.Value = revision;
3087 arg[2].Type = ACPI_TYPE_INTEGER;
3088 arg[2].Integer.Value = function;
3089 if (package) {
3090 arg[3] = *package;
3091 } else {
3092 arg[3].Type = ACPI_TYPE_PACKAGE;
3093 arg[3].Package.Count = 0;
3094 arg[3].Package.Elements = NULL;
3095 }
3096
3097 arglist.Pointer = arg;
3098 arglist.Count = 4;
3099 buf.Pointer = NULL;
3100 buf.Length = ACPI_ALLOCATE_BUFFER;
3101 status = AcpiEvaluateObjectTyped(handle, "_DSM", &arglist, &buf, type);
3102 if (ACPI_FAILURE(status))
3103 return (status);
3104
3105 KASSERT(ACPI_SUCCESS(status), ("Unexpected status"));
3106
3107 *out_buf = buf;
3108 return (status);
3109 }
3110
3111 ACPI_STATUS
acpi_EvaluateOSC(ACPI_HANDLE handle,uint8_t * uuid,int revision,int count,uint32_t * caps_in,uint32_t * caps_out,bool query)3112 acpi_EvaluateOSC(ACPI_HANDLE handle, uint8_t *uuid, int revision, int count,
3113 uint32_t *caps_in, uint32_t *caps_out, bool query)
3114 {
3115 ACPI_OBJECT arg[4], *ret;
3116 ACPI_OBJECT_LIST arglist;
3117 ACPI_BUFFER buf;
3118 ACPI_STATUS status;
3119
3120 arglist.Pointer = arg;
3121 arglist.Count = 4;
3122 arg[0].Type = ACPI_TYPE_BUFFER;
3123 arg[0].Buffer.Length = ACPI_UUID_LENGTH;
3124 arg[0].Buffer.Pointer = uuid;
3125 arg[1].Type = ACPI_TYPE_INTEGER;
3126 arg[1].Integer.Value = revision;
3127 arg[2].Type = ACPI_TYPE_INTEGER;
3128 arg[2].Integer.Value = count;
3129 arg[3].Type = ACPI_TYPE_BUFFER;
3130 arg[3].Buffer.Length = count * sizeof(*caps_in);
3131 arg[3].Buffer.Pointer = (uint8_t *)caps_in;
3132 caps_in[0] = query ? 1 : 0;
3133 buf.Pointer = NULL;
3134 buf.Length = ACPI_ALLOCATE_BUFFER;
3135 status = AcpiEvaluateObjectTyped(handle, "_OSC", &arglist, &buf,
3136 ACPI_TYPE_BUFFER);
3137 if (ACPI_FAILURE(status))
3138 return (status);
3139 if (caps_out != NULL) {
3140 ret = buf.Pointer;
3141 if (ret->Buffer.Length != count * sizeof(*caps_out)) {
3142 AcpiOsFree(buf.Pointer);
3143 return (AE_BUFFER_OVERFLOW);
3144 }
3145 bcopy(ret->Buffer.Pointer, caps_out, ret->Buffer.Length);
3146 }
3147 AcpiOsFree(buf.Pointer);
3148 return (status);
3149 }
3150
3151 /*
3152 * Set interrupt model.
3153 */
3154 ACPI_STATUS
acpi_SetIntrModel(int model)3155 acpi_SetIntrModel(int model)
3156 {
3157
3158 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
3159 }
3160
3161 /*
3162 * Walk subtables of a table and call a callback routine for each
3163 * subtable. The caller should provide the first subtable and a
3164 * pointer to the end of the table. This can be used to walk tables
3165 * such as MADT and SRAT that use subtable entries.
3166 */
3167 void
acpi_walk_subtables(void * first,void * end,acpi_subtable_handler * handler,void * arg)3168 acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
3169 void *arg)
3170 {
3171 ACPI_SUBTABLE_HEADER *entry;
3172
3173 for (entry = first; (void *)entry < end; ) {
3174 /* Avoid an infinite loop if we hit a bogus entry. */
3175 if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
3176 return;
3177
3178 handler(entry, arg);
3179 entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
3180 }
3181 }
3182
3183 /*
3184 * DEPRECATED. This interface has serious deficiencies and will be
3185 * removed.
3186 *
3187 * Immediately enter the sleep state. In the old model, acpiconf(8) ran
3188 * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
3189 */
3190 ACPI_STATUS
acpi_SetSleepState(struct acpi_softc * sc,int state)3191 acpi_SetSleepState(struct acpi_softc *sc, int state)
3192 {
3193 static int once;
3194
3195 if (!once) {
3196 device_printf(sc->acpi_dev,
3197 "warning: acpi_SetSleepState() deprecated, need to update your software\n");
3198 once = 1;
3199 }
3200 return (acpi_EnterSleepState(sc, state));
3201 }
3202
3203 #if defined(__amd64__) || defined(__i386__)
3204 static void
acpi_sleep_force_task(void * context)3205 acpi_sleep_force_task(void *context)
3206 {
3207 struct acpi_softc *sc = (struct acpi_softc *)context;
3208
3209 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_stype)))
3210 device_printf(sc->acpi_dev, "force sleep state %s failed\n",
3211 power_stype_to_name(sc->acpi_next_stype));
3212 }
3213
3214 static void
acpi_sleep_force(void * arg)3215 acpi_sleep_force(void *arg)
3216 {
3217 struct acpi_softc *sc = (struct acpi_softc *)arg;
3218
3219 device_printf(sc->acpi_dev,
3220 "suspend request timed out, forcing sleep now\n");
3221 /*
3222 * XXX Suspending from callout causes freezes in DEVICE_SUSPEND().
3223 * Suspend from acpi_task thread instead.
3224 */
3225 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3226 acpi_sleep_force_task, sc)))
3227 device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n");
3228 }
3229 #endif
3230
3231 /*
3232 * Request that the system enter the given suspend state. All /dev/apm
3233 * devices and devd(8) will be notified. Userland then has a chance to
3234 * save state and acknowledge the request. The system sleeps once all
3235 * acks are in.
3236 */
3237 int
acpi_ReqSleepState(struct acpi_softc * sc,enum power_stype stype)3238 acpi_ReqSleepState(struct acpi_softc *sc, enum power_stype stype)
3239 {
3240 #if defined(__amd64__) || defined(__i386__)
3241 struct apm_clone_data *clone;
3242 ACPI_STATUS status;
3243
3244 if (stype < POWER_STYPE_AWAKE || stype >= POWER_STYPE_COUNT)
3245 return (EINVAL);
3246 if (!acpi_supported_stypes[stype])
3247 return (EOPNOTSUPP);
3248
3249 /*
3250 * If a reboot/shutdown/suspend request is already in progress or
3251 * suspend is blocked due to an upcoming shutdown, just return.
3252 */
3253 if (rebooting || sc->acpi_next_stype != POWER_STYPE_AWAKE ||
3254 suspend_blocked)
3255 return (0);
3256
3257 /* Wait until sleep is enabled. */
3258 while (sc->acpi_sleep_disabled) {
3259 AcpiOsSleep(1000);
3260 }
3261
3262 ACPI_LOCK(acpi);
3263
3264 sc->acpi_next_stype = stype;
3265
3266 /* S5 (soft-off) should be entered directly with no waiting. */
3267 if (stype == POWER_STYPE_POWEROFF) {
3268 ACPI_UNLOCK(acpi);
3269 status = acpi_EnterSleepState(sc, stype);
3270 return (ACPI_SUCCESS(status) ? 0 : ENXIO);
3271 }
3272
3273 /* Record the pending state and notify all apm devices. */
3274 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
3275 clone->notify_status = APM_EV_NONE;
3276 if ((clone->flags & ACPI_EVF_DEVD) == 0) {
3277 selwakeuppri(&clone->sel_read, PZERO);
3278 KNOTE_LOCKED(&clone->sel_read.si_note, 0);
3279 }
3280 }
3281
3282 /* If devd(8) is not running, immediately enter the sleep state. */
3283 if (!devctl_process_running()) {
3284 ACPI_UNLOCK(acpi);
3285 status = acpi_EnterSleepState(sc, stype);
3286 return (ACPI_SUCCESS(status) ? 0 : ENXIO);
3287 }
3288
3289 /*
3290 * Set a timeout to fire if userland doesn't ack the suspend request
3291 * in time. This way we still eventually go to sleep if we were
3292 * overheating or running low on battery, even if userland is hung.
3293 * We cancel this timeout once all userland acks are in or the
3294 * suspend request is aborted.
3295 */
3296 callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
3297 ACPI_UNLOCK(acpi);
3298
3299 /* Now notify devd(8) also. */
3300 acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, stype);
3301
3302 return (0);
3303 #else
3304 /* This platform does not support acpi suspend/resume. */
3305 return (EOPNOTSUPP);
3306 #endif
3307 }
3308
3309 /*
3310 * Acknowledge (or reject) a pending sleep state. The caller has
3311 * prepared for suspend and is now ready for it to proceed. If the
3312 * error argument is non-zero, it indicates suspend should be cancelled
3313 * and gives an errno value describing why. Once all votes are in,
3314 * we suspend the system.
3315 */
3316 int
acpi_AckSleepState(struct apm_clone_data * clone,int error)3317 acpi_AckSleepState(struct apm_clone_data *clone, int error)
3318 {
3319 #if defined(__amd64__) || defined(__i386__)
3320 struct acpi_softc *sc;
3321 int ret, sleeping;
3322
3323 /* If no pending sleep type, return an error. */
3324 ACPI_LOCK(acpi);
3325 sc = clone->acpi_sc;
3326 if (sc->acpi_next_stype == POWER_STYPE_AWAKE) {
3327 ACPI_UNLOCK(acpi);
3328 return (ENXIO);
3329 }
3330
3331 /* Caller wants to abort suspend process. */
3332 if (error) {
3333 sc->acpi_next_stype = POWER_STYPE_AWAKE;
3334 callout_stop(&sc->susp_force_to);
3335 device_printf(sc->acpi_dev,
3336 "listener on %s cancelled the pending suspend\n",
3337 devtoname(clone->cdev));
3338 ACPI_UNLOCK(acpi);
3339 return (0);
3340 }
3341
3342 /*
3343 * Mark this device as acking the suspend request. Then, walk through
3344 * all devices, seeing if they agree yet. We only count devices that
3345 * are writable since read-only devices couldn't ack the request.
3346 */
3347 sleeping = TRUE;
3348 clone->notify_status = APM_EV_ACKED;
3349 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
3350 if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
3351 clone->notify_status != APM_EV_ACKED) {
3352 sleeping = FALSE;
3353 break;
3354 }
3355 }
3356
3357 /* If all devices have voted "yes", we will suspend now. */
3358 if (sleeping)
3359 callout_stop(&sc->susp_force_to);
3360 ACPI_UNLOCK(acpi);
3361 ret = 0;
3362 if (sleeping) {
3363 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_stype)))
3364 ret = ENODEV;
3365 }
3366 return (ret);
3367 #else
3368 /* This platform does not support acpi suspend/resume. */
3369 return (EOPNOTSUPP);
3370 #endif
3371 }
3372
3373 static void
acpi_sleep_enable(void * arg)3374 acpi_sleep_enable(void *arg)
3375 {
3376 struct acpi_softc *sc = (struct acpi_softc *)arg;
3377
3378 ACPI_LOCK_ASSERT(acpi);
3379
3380 /* Reschedule if the system is not fully up and running. */
3381 if (!AcpiGbl_SystemAwakeAndRunning) {
3382 callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
3383 return;
3384 }
3385
3386 sc->acpi_sleep_disabled = FALSE;
3387 }
3388
3389 static ACPI_STATUS
acpi_sleep_disable(struct acpi_softc * sc)3390 acpi_sleep_disable(struct acpi_softc *sc)
3391 {
3392 ACPI_STATUS status;
3393
3394 /* Fail if the system is not fully up and running. */
3395 if (!AcpiGbl_SystemAwakeAndRunning)
3396 return (AE_ERROR);
3397
3398 ACPI_LOCK(acpi);
3399 status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
3400 sc->acpi_sleep_disabled = TRUE;
3401 ACPI_UNLOCK(acpi);
3402
3403 return (status);
3404 }
3405
3406 enum acpi_sleep_state {
3407 ACPI_SS_NONE,
3408 ACPI_SS_GPE_SET,
3409 ACPI_SS_DEV_SUSPEND,
3410 ACPI_SS_SLP_PREP,
3411 ACPI_SS_SLEPT,
3412 };
3413
3414 /*
3415 * Enter the desired system sleep state.
3416 *
3417 * Currently we support S1-S5 but S4 is only S4BIOS
3418 */
3419 static ACPI_STATUS
acpi_EnterSleepState(struct acpi_softc * sc,enum power_stype stype)3420 acpi_EnterSleepState(struct acpi_softc *sc, enum power_stype stype)
3421 {
3422 register_t intr;
3423 ACPI_STATUS status;
3424 ACPI_EVENT_STATUS power_button_status;
3425 enum acpi_sleep_state slp_state;
3426 int sleep_result;
3427
3428 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, stype);
3429
3430 if (stype <= POWER_STYPE_AWAKE || stype >= POWER_STYPE_COUNT)
3431 return_ACPI_STATUS (AE_BAD_PARAMETER);
3432 if (!acpi_supported_stypes[stype]) {
3433 device_printf(sc->acpi_dev, "Sleep type %s not supported on this "
3434 "platform\n", power_stype_to_name(stype));
3435 return (AE_SUPPORT);
3436 }
3437
3438 /* Re-entry once we're suspending is not allowed. */
3439 status = acpi_sleep_disable(sc);
3440 if (ACPI_FAILURE(status)) {
3441 device_printf(sc->acpi_dev,
3442 "suspend request ignored (not ready yet)\n");
3443 return (status);
3444 }
3445
3446 if (stype == POWER_STYPE_POWEROFF) {
3447 /*
3448 * Shut down cleanly and power off. This will call us back through the
3449 * shutdown handlers.
3450 */
3451 shutdown_nice(RB_POWEROFF);
3452 return_ACPI_STATUS (AE_OK);
3453 }
3454
3455 EVENTHANDLER_INVOKE(power_suspend_early);
3456 stop_all_proc();
3457 suspend_all_fs();
3458 EVENTHANDLER_INVOKE(power_suspend);
3459
3460 #ifdef EARLY_AP_STARTUP
3461 MPASS(mp_ncpus == 1 || smp_started);
3462 thread_lock(curthread);
3463 sched_bind(curthread, 0);
3464 thread_unlock(curthread);
3465 #else
3466 if (smp_started) {
3467 thread_lock(curthread);
3468 sched_bind(curthread, 0);
3469 thread_unlock(curthread);
3470 }
3471 #endif
3472
3473 /*
3474 * Be sure to hold bus topology lock across DEVICE_SUSPEND/RESUME.
3475 */
3476 bus_topo_lock();
3477
3478 slp_state = ACPI_SS_NONE;
3479
3480 sc->acpi_stype = stype;
3481
3482 /* Enable any GPEs as appropriate and requested by the user. */
3483 acpi_wake_prep_walk(stype);
3484 slp_state = ACPI_SS_GPE_SET;
3485
3486 /*
3487 * Inform all devices that we are going to sleep. If at least one
3488 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
3489 *
3490 * XXX Note that a better two-pass approach with a 'veto' pass
3491 * followed by a "real thing" pass would be better, but the current
3492 * bus interface does not provide for this.
3493 */
3494 if (DEVICE_SUSPEND(root_bus) != 0) {
3495 device_printf(sc->acpi_dev, "device_suspend failed\n");
3496 goto backout;
3497 }
3498 slp_state = ACPI_SS_DEV_SUSPEND;
3499
3500 status = AcpiEnterSleepStatePrep(stype);
3501 if (ACPI_FAILURE(status)) {
3502 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
3503 AcpiFormatException(status));
3504 goto backout;
3505 }
3506 slp_state = ACPI_SS_SLP_PREP;
3507
3508 if (sc->acpi_sleep_delay > 0)
3509 DELAY(sc->acpi_sleep_delay * 1000000);
3510
3511 suspendclock();
3512 intr = intr_disable();
3513 if (stype != POWER_STYPE_STANDBY) {
3514 sleep_result = acpi_sleep_machdep(sc, stype);
3515 acpi_wakeup_machdep(sc, stype, sleep_result, 0);
3516
3517 /*
3518 * XXX According to ACPI specification SCI_EN bit should be restored
3519 * by ACPI platform (BIOS, firmware) to its pre-sleep state.
3520 * Unfortunately some BIOSes fail to do that and that leads to
3521 * unexpected and serious consequences during wake up like a system
3522 * getting stuck in SMI handlers.
3523 * This hack is picked up from Linux, which claims that it follows
3524 * Windows behavior.
3525 */
3526 if (sleep_result == 1 && stype != POWER_STYPE_HIBERNATE)
3527 AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT);
3528
3529 if (sleep_result == 1 && stype == POWER_STYPE_SUSPEND_TO_MEM) {
3530 /*
3531 * Prevent mis-interpretation of the wakeup by power button
3532 * as a request for power off.
3533 * Ideally we should post an appropriate wakeup event,
3534 * perhaps using acpi_event_power_button_wake or alike.
3535 *
3536 * Clearing of power button status after wakeup is mandated
3537 * by ACPI specification in section "Fixed Power Button".
3538 *
3539 * XXX As of ACPICA 20121114 AcpiGetEventStatus provides
3540 * status as 0/1 corressponding to inactive/active despite
3541 * its type being ACPI_EVENT_STATUS. In other words,
3542 * we should not test for ACPI_EVENT_FLAG_SET for time being.
3543 */
3544 if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON,
3545 &power_button_status)) && power_button_status != 0) {
3546 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
3547 device_printf(sc->acpi_dev,
3548 "cleared fixed power button status\n");
3549 }
3550 }
3551
3552 intr_restore(intr);
3553
3554 /* call acpi_wakeup_machdep() again with interrupt enabled */
3555 acpi_wakeup_machdep(sc, stype, sleep_result, 1);
3556
3557 AcpiLeaveSleepStatePrep(stype);
3558
3559 if (sleep_result == -1)
3560 goto backout;
3561
3562 /* Re-enable ACPI hardware on wakeup from hibernate. */
3563 if (stype == POWER_STYPE_HIBERNATE)
3564 AcpiEnable();
3565 } else {
3566 status = AcpiEnterSleepState(stype);
3567 intr_restore(intr);
3568 AcpiLeaveSleepStatePrep(stype);
3569 if (ACPI_FAILURE(status)) {
3570 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
3571 AcpiFormatException(status));
3572 goto backout;
3573 }
3574 }
3575 slp_state = ACPI_SS_SLEPT;
3576
3577 /*
3578 * Back out state according to how far along we got in the suspend
3579 * process. This handles both the error and success cases.
3580 */
3581 backout:
3582 if (slp_state >= ACPI_SS_SLP_PREP)
3583 resumeclock();
3584 if (slp_state >= ACPI_SS_GPE_SET) {
3585 acpi_wake_prep_walk(stype);
3586 sc->acpi_stype = POWER_STYPE_AWAKE;
3587 }
3588 if (slp_state >= ACPI_SS_DEV_SUSPEND)
3589 DEVICE_RESUME(root_bus);
3590 if (slp_state >= ACPI_SS_SLP_PREP)
3591 AcpiLeaveSleepState(stype);
3592 if (slp_state >= ACPI_SS_SLEPT) {
3593 #if defined(__i386__) || defined(__amd64__)
3594 /* NB: we are still using ACPI timecounter at this point. */
3595 resume_TSC();
3596 #endif
3597 acpi_resync_clock(sc);
3598 acpi_enable_fixed_events(sc);
3599 }
3600 sc->acpi_next_stype = POWER_STYPE_AWAKE;
3601
3602 bus_topo_unlock();
3603
3604 #ifdef EARLY_AP_STARTUP
3605 thread_lock(curthread);
3606 sched_unbind(curthread);
3607 thread_unlock(curthread);
3608 #else
3609 if (smp_started) {
3610 thread_lock(curthread);
3611 sched_unbind(curthread);
3612 thread_unlock(curthread);
3613 }
3614 #endif
3615
3616 resume_all_fs();
3617 resume_all_proc();
3618
3619 EVENTHANDLER_INVOKE(power_resume);
3620
3621 /* Allow another sleep request after a while. */
3622 callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
3623
3624 /* Run /etc/rc.resume after we are back. */
3625 if (devctl_process_running())
3626 acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, stype);
3627
3628 return_ACPI_STATUS (status);
3629 }
3630
3631 static void
acpi_resync_clock(struct acpi_softc * sc)3632 acpi_resync_clock(struct acpi_softc *sc)
3633 {
3634
3635 /*
3636 * Warm up timecounter again and reset system clock.
3637 */
3638 (void)timecounter->tc_get_timecount(timecounter);
3639 inittodr(time_second + sc->acpi_sleep_delay);
3640 }
3641
3642 /* Enable or disable the device's wake GPE. */
3643 int
acpi_wake_set_enable(device_t dev,int enable)3644 acpi_wake_set_enable(device_t dev, int enable)
3645 {
3646 struct acpi_prw_data prw;
3647 ACPI_STATUS status;
3648 int flags;
3649
3650 /* Make sure the device supports waking the system and get the GPE. */
3651 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
3652 return (ENXIO);
3653
3654 flags = acpi_get_flags(dev);
3655 if (enable) {
3656 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
3657 ACPI_GPE_ENABLE);
3658 if (ACPI_FAILURE(status)) {
3659 device_printf(dev, "enable wake failed\n");
3660 return (ENXIO);
3661 }
3662 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
3663 } else {
3664 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
3665 ACPI_GPE_DISABLE);
3666 if (ACPI_FAILURE(status)) {
3667 device_printf(dev, "disable wake failed\n");
3668 return (ENXIO);
3669 }
3670 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
3671 }
3672
3673 return (0);
3674 }
3675
3676 static int
acpi_wake_sleep_prep(ACPI_HANDLE handle,enum power_stype stype)3677 acpi_wake_sleep_prep(ACPI_HANDLE handle, enum power_stype stype)
3678 {
3679 int sstate;
3680 struct acpi_prw_data prw;
3681 device_t dev;
3682 struct acpi_softc *sc;
3683
3684 /* Check that this is a wake-capable device and get its GPE. */
3685 if (acpi_parse_prw(handle, &prw) != 0)
3686 return (ENXIO);
3687 dev = acpi_get_device(handle);
3688
3689 sc = device_get_softc(dev);
3690 sstate = acpi_stype_to_sstate(sc, stype);
3691
3692 /*
3693 * The destination sleep state must be less than (i.e., higher power)
3694 * or equal to the value specified by _PRW. If this GPE cannot be
3695 * enabled for the next sleep state, then disable it. If it can and
3696 * the user requested it be enabled, turn on any required power resources
3697 * and set _PSW.
3698 */
3699 if (sstate > prw.lowest_wake) {
3700 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE);
3701 if (bootverbose)
3702 device_printf(dev, "wake_prep disabled wake for %s (%s)\n",
3703 acpi_name(handle), power_stype_to_name(stype));
3704 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
3705 acpi_pwr_wake_enable(handle, 1);
3706 acpi_SetInteger(handle, "_PSW", 1);
3707 if (bootverbose)
3708 device_printf(dev, "wake_prep enabled for %s (%s)\n",
3709 acpi_name(handle), power_stype_to_name(stype));
3710 }
3711
3712 return (0);
3713 }
3714
3715 static int
acpi_wake_run_prep(ACPI_HANDLE handle,enum power_stype stype)3716 acpi_wake_run_prep(ACPI_HANDLE handle, enum power_stype stype)
3717 {
3718 int sstate;
3719 struct acpi_prw_data prw;
3720 device_t dev;
3721 struct acpi_softc *sc;
3722
3723 /*
3724 * Check that this is a wake-capable device and get its GPE. Return
3725 * now if the user didn't enable this device for wake.
3726 */
3727 if (acpi_parse_prw(handle, &prw) != 0)
3728 return (ENXIO);
3729 dev = acpi_get_device(handle);
3730 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
3731 return (0);
3732
3733 sc = device_get_softc(dev);
3734 sstate = acpi_stype_to_sstate(sc, stype);
3735
3736 /*
3737 * If this GPE couldn't be enabled for the previous sleep state, it was
3738 * disabled before going to sleep so re-enable it. If it was enabled,
3739 * clear _PSW and turn off any power resources it used.
3740 */
3741 if (sstate > prw.lowest_wake) {
3742 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE);
3743 if (bootverbose)
3744 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
3745 } else {
3746 acpi_SetInteger(handle, "_PSW", 0);
3747 acpi_pwr_wake_enable(handle, 0);
3748 if (bootverbose)
3749 device_printf(dev, "run_prep cleaned up for %s\n",
3750 acpi_name(handle));
3751 }
3752
3753 return (0);
3754 }
3755
3756 static ACPI_STATUS
acpi_wake_prep(ACPI_HANDLE handle,UINT32 level,void * context,void ** status)3757 acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
3758 {
3759 enum power_stype stype;
3760
3761 /* If suspending, run the sleep prep function, otherwise wake. */
3762 stype = *(enum power_stype *)context;
3763 if (AcpiGbl_SystemAwakeAndRunning)
3764 acpi_wake_sleep_prep(handle, stype);
3765 else
3766 acpi_wake_run_prep(handle, stype);
3767 return (AE_OK);
3768 }
3769
3770 /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
3771 static int
acpi_wake_prep_walk(enum power_stype stype)3772 acpi_wake_prep_walk(enum power_stype stype)
3773 {
3774 ACPI_HANDLE sb_handle;
3775
3776 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
3777 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
3778 acpi_wake_prep, NULL, &stype, NULL);
3779 return (0);
3780 }
3781
3782 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
3783 static int
acpi_wake_sysctl_walk(device_t dev)3784 acpi_wake_sysctl_walk(device_t dev)
3785 {
3786 int error, i, numdevs;
3787 device_t *devlist;
3788 device_t child;
3789 ACPI_STATUS status;
3790
3791 error = device_get_children(dev, &devlist, &numdevs);
3792 if (error != 0 || numdevs == 0) {
3793 if (numdevs == 0)
3794 free(devlist, M_TEMP);
3795 return (error);
3796 }
3797 for (i = 0; i < numdevs; i++) {
3798 child = devlist[i];
3799 acpi_wake_sysctl_walk(child);
3800 if (!device_is_attached(child))
3801 continue;
3802 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
3803 if (ACPI_SUCCESS(status)) {
3804 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
3805 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
3806 "wake", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, child, 0,
3807 acpi_wake_set_sysctl, "I", "Device set to wake the system");
3808 }
3809 }
3810 free(devlist, M_TEMP);
3811
3812 return (0);
3813 }
3814
3815 /* Enable or disable wake from userland. */
3816 static int
acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)3817 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
3818 {
3819 int enable, error;
3820 device_t dev;
3821
3822 dev = (device_t)arg1;
3823 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
3824
3825 error = sysctl_handle_int(oidp, &enable, 0, req);
3826 if (error != 0 || req->newptr == NULL)
3827 return (error);
3828 if (enable != 0 && enable != 1)
3829 return (EINVAL);
3830
3831 return (acpi_wake_set_enable(dev, enable));
3832 }
3833
3834 /* Parse a device's _PRW into a structure. */
3835 int
acpi_parse_prw(ACPI_HANDLE h,struct acpi_prw_data * prw)3836 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
3837 {
3838 ACPI_STATUS status;
3839 ACPI_BUFFER prw_buffer;
3840 ACPI_OBJECT *res, *res2;
3841 int error, i, power_count;
3842
3843 if (h == NULL || prw == NULL)
3844 return (EINVAL);
3845
3846 /*
3847 * The _PRW object (7.2.9) is only required for devices that have the
3848 * ability to wake the system from a sleeping state.
3849 */
3850 error = EINVAL;
3851 prw_buffer.Pointer = NULL;
3852 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
3853 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
3854 if (ACPI_FAILURE(status))
3855 return (ENOENT);
3856 res = (ACPI_OBJECT *)prw_buffer.Pointer;
3857 if (res == NULL)
3858 return (ENOENT);
3859 if (!ACPI_PKG_VALID(res, 2))
3860 goto out;
3861
3862 /*
3863 * Element 1 of the _PRW object:
3864 * The lowest power system sleeping state that can be entered while still
3865 * providing wake functionality. The sleeping state being entered must
3866 * be less than (i.e., higher power) or equal to this value.
3867 */
3868 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
3869 goto out;
3870
3871 /*
3872 * Element 0 of the _PRW object:
3873 */
3874 switch (res->Package.Elements[0].Type) {
3875 case ACPI_TYPE_INTEGER:
3876 /*
3877 * If the data type of this package element is numeric, then this
3878 * _PRW package element is the bit index in the GPEx_EN, in the
3879 * GPE blocks described in the FADT, of the enable bit that is
3880 * enabled for the wake event.
3881 */
3882 prw->gpe_handle = NULL;
3883 prw->gpe_bit = res->Package.Elements[0].Integer.Value;
3884 error = 0;
3885 break;
3886 case ACPI_TYPE_PACKAGE:
3887 /*
3888 * If the data type of this package element is a package, then this
3889 * _PRW package element is itself a package containing two
3890 * elements. The first is an object reference to the GPE Block
3891 * device that contains the GPE that will be triggered by the wake
3892 * event. The second element is numeric and it contains the bit
3893 * index in the GPEx_EN, in the GPE Block referenced by the
3894 * first element in the package, of the enable bit that is enabled for
3895 * the wake event.
3896 *
3897 * For example, if this field is a package then it is of the form:
3898 * Package() {\_SB.PCI0.ISA.GPE, 2}
3899 */
3900 res2 = &res->Package.Elements[0];
3901 if (!ACPI_PKG_VALID(res2, 2))
3902 goto out;
3903 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
3904 if (prw->gpe_handle == NULL)
3905 goto out;
3906 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
3907 goto out;
3908 error = 0;
3909 break;
3910 default:
3911 goto out;
3912 }
3913
3914 /* Elements 2 to N of the _PRW object are power resources. */
3915 power_count = res->Package.Count - 2;
3916 if (power_count > ACPI_PRW_MAX_POWERRES) {
3917 printf("ACPI device %s has too many power resources\n", acpi_name(h));
3918 power_count = 0;
3919 }
3920 prw->power_res_count = power_count;
3921 for (i = 0; i < power_count; i++)
3922 prw->power_res[i] = res->Package.Elements[i];
3923
3924 out:
3925 if (prw_buffer.Pointer != NULL)
3926 AcpiOsFree(prw_buffer.Pointer);
3927 return (error);
3928 }
3929
3930 /*
3931 * ACPI Event Handlers
3932 */
3933
3934 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
3935
3936 static void
acpi_system_eventhandler_sleep(void * arg,enum power_stype stype)3937 acpi_system_eventhandler_sleep(void *arg, enum power_stype stype)
3938 {
3939 struct acpi_softc *sc = (struct acpi_softc *)arg;
3940 int ret;
3941
3942 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, stype);
3943
3944 /* Check if button action is disabled or unknown. */
3945 if (stype == ACPI_STATE_UNKNOWN)
3946 return;
3947
3948 /*
3949 * Request that the system prepare to enter the given suspend state. We can
3950 * totally pass an ACPI S-state to an enum power_stype.
3951 */
3952 ret = acpi_ReqSleepState(sc, stype);
3953 if (ret != 0)
3954 device_printf(sc->acpi_dev,
3955 "request to enter state %s failed (err %d)\n",
3956 power_stype_to_name(stype), ret);
3957
3958 return_VOID;
3959 }
3960
3961 static void
acpi_system_eventhandler_wakeup(void * arg,enum power_stype stype)3962 acpi_system_eventhandler_wakeup(void *arg, enum power_stype stype)
3963 {
3964
3965 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, stype);
3966
3967 /* Currently, nothing to do for wakeup. */
3968
3969 return_VOID;
3970 }
3971
3972 /*
3973 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
3974 */
3975 static void
acpi_invoke_sleep_eventhandler(void * context)3976 acpi_invoke_sleep_eventhandler(void *context)
3977 {
3978
3979 EVENTHANDLER_INVOKE(acpi_sleep_event, *(enum power_stype *)context);
3980 }
3981
3982 static void
acpi_invoke_wake_eventhandler(void * context)3983 acpi_invoke_wake_eventhandler(void *context)
3984 {
3985
3986 EVENTHANDLER_INVOKE(acpi_wakeup_event, *(enum power_stype *)context);
3987 }
3988
3989 UINT32
acpi_event_power_button_sleep(void * context)3990 acpi_event_power_button_sleep(void *context)
3991 {
3992 #if defined(__amd64__) || defined(__i386__)
3993 struct acpi_softc *sc = (struct acpi_softc *)context;
3994 #else
3995 (void)context;
3996 #endif
3997
3998 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3999
4000 #if defined(__amd64__) || defined(__i386__)
4001 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
4002 acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_stype)))
4003 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
4004 #else
4005 shutdown_nice(RB_POWEROFF);
4006 #endif
4007
4008 return_VALUE (ACPI_INTERRUPT_HANDLED);
4009 }
4010
4011 UINT32
acpi_event_power_button_wake(void * context)4012 acpi_event_power_button_wake(void *context)
4013 {
4014 struct acpi_softc *sc = (struct acpi_softc *)context;
4015
4016 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
4017
4018 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
4019 acpi_invoke_wake_eventhandler, &sc->acpi_power_button_stype)))
4020 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
4021 return_VALUE (ACPI_INTERRUPT_HANDLED);
4022 }
4023
4024 UINT32
acpi_event_sleep_button_sleep(void * context)4025 acpi_event_sleep_button_sleep(void *context)
4026 {
4027 struct acpi_softc *sc = (struct acpi_softc *)context;
4028
4029 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
4030
4031 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
4032 acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_stype)))
4033 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
4034 return_VALUE (ACPI_INTERRUPT_HANDLED);
4035 }
4036
4037 UINT32
acpi_event_sleep_button_wake(void * context)4038 acpi_event_sleep_button_wake(void *context)
4039 {
4040 struct acpi_softc *sc = (struct acpi_softc *)context;
4041
4042 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
4043
4044 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
4045 acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_stype)))
4046 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
4047 return_VALUE (ACPI_INTERRUPT_HANDLED);
4048 }
4049
4050 /*
4051 * XXX This static buffer is suboptimal. There is no locking so only
4052 * use this for single-threaded callers.
4053 */
4054 char *
acpi_name(ACPI_HANDLE handle)4055 acpi_name(ACPI_HANDLE handle)
4056 {
4057 ACPI_BUFFER buf;
4058 static char data[256];
4059
4060 buf.Length = sizeof(data);
4061 buf.Pointer = data;
4062
4063 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
4064 return (data);
4065 return ("(unknown)");
4066 }
4067
4068 /*
4069 * Debugging/bug-avoidance. Avoid trying to fetch info on various
4070 * parts of the namespace.
4071 */
4072 int
acpi_avoid(ACPI_HANDLE handle)4073 acpi_avoid(ACPI_HANDLE handle)
4074 {
4075 char *cp, *env, *np;
4076 int len;
4077
4078 np = acpi_name(handle);
4079 if (*np == '\\')
4080 np++;
4081 if ((env = kern_getenv("debug.acpi.avoid")) == NULL)
4082 return (0);
4083
4084 /* Scan the avoid list checking for a match */
4085 cp = env;
4086 for (;;) {
4087 while (*cp != 0 && isspace(*cp))
4088 cp++;
4089 if (*cp == 0)
4090 break;
4091 len = 0;
4092 while (cp[len] != 0 && !isspace(cp[len]))
4093 len++;
4094 if (!strncmp(cp, np, len)) {
4095 freeenv(env);
4096 return(1);
4097 }
4098 cp += len;
4099 }
4100 freeenv(env);
4101
4102 return (0);
4103 }
4104
4105 /*
4106 * Debugging/bug-avoidance. Disable ACPI subsystem components.
4107 */
4108 int
acpi_disabled(char * subsys)4109 acpi_disabled(char *subsys)
4110 {
4111 char *cp, *env;
4112 int len;
4113
4114 if ((env = kern_getenv("debug.acpi.disabled")) == NULL)
4115 return (0);
4116 if (strcmp(env, "all") == 0) {
4117 freeenv(env);
4118 return (1);
4119 }
4120
4121 /* Scan the disable list, checking for a match. */
4122 cp = env;
4123 for (;;) {
4124 while (*cp != '\0' && isspace(*cp))
4125 cp++;
4126 if (*cp == '\0')
4127 break;
4128 len = 0;
4129 while (cp[len] != '\0' && !isspace(cp[len]))
4130 len++;
4131 if (strncmp(cp, subsys, len) == 0) {
4132 freeenv(env);
4133 return (1);
4134 }
4135 cp += len;
4136 }
4137 freeenv(env);
4138
4139 return (0);
4140 }
4141
4142 static void
acpi_lookup(void * arg,const char * name,device_t * dev)4143 acpi_lookup(void *arg, const char *name, device_t *dev)
4144 {
4145 ACPI_HANDLE handle;
4146
4147 if (*dev != NULL)
4148 return;
4149
4150 /*
4151 * Allow any handle name that is specified as an absolute path and
4152 * starts with '\'. We could restrict this to \_SB and friends,
4153 * but see acpi_probe_children() for notes on why we scan the entire
4154 * namespace for devices.
4155 *
4156 * XXX: The pathname argument to AcpiGetHandle() should be fixed to
4157 * be const.
4158 */
4159 if (name[0] != '\\')
4160 return;
4161 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, __DECONST(char *, name),
4162 &handle)))
4163 return;
4164 *dev = acpi_get_device(handle);
4165 }
4166
4167 /*
4168 * Control interface.
4169 *
4170 * We multiplex ioctls for all participating ACPI devices here. Individual
4171 * drivers wanting to be accessible via /dev/acpi should use the
4172 * register/deregister interface to make their handlers visible.
4173 */
4174 struct acpi_ioctl_hook
4175 {
4176 TAILQ_ENTRY(acpi_ioctl_hook) link;
4177 u_long cmd;
4178 acpi_ioctl_fn fn;
4179 void *arg;
4180 };
4181
4182 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
4183 static int acpi_ioctl_hooks_initted;
4184
4185 int
acpi_register_ioctl(u_long cmd,acpi_ioctl_fn fn,void * arg)4186 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
4187 {
4188 struct acpi_ioctl_hook *hp;
4189
4190 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
4191 return (ENOMEM);
4192 hp->cmd = cmd;
4193 hp->fn = fn;
4194 hp->arg = arg;
4195
4196 ACPI_LOCK(acpi);
4197 if (acpi_ioctl_hooks_initted == 0) {
4198 TAILQ_INIT(&acpi_ioctl_hooks);
4199 acpi_ioctl_hooks_initted = 1;
4200 }
4201 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
4202 ACPI_UNLOCK(acpi);
4203
4204 return (0);
4205 }
4206
4207 void
acpi_deregister_ioctl(u_long cmd,acpi_ioctl_fn fn)4208 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
4209 {
4210 struct acpi_ioctl_hook *hp;
4211
4212 ACPI_LOCK(acpi);
4213 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
4214 if (hp->cmd == cmd && hp->fn == fn)
4215 break;
4216
4217 if (hp != NULL) {
4218 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
4219 free(hp, M_ACPIDEV);
4220 }
4221 ACPI_UNLOCK(acpi);
4222 }
4223
4224 static int
acpiopen(struct cdev * dev,int flag,int fmt,struct thread * td)4225 acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
4226 {
4227 return (0);
4228 }
4229
4230 static int
acpiclose(struct cdev * dev,int flag,int fmt,struct thread * td)4231 acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
4232 {
4233 return (0);
4234 }
4235
4236 static int
acpiioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)4237 acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
4238 {
4239 struct acpi_softc *sc;
4240 struct acpi_ioctl_hook *hp;
4241 int error;
4242 int sstate;
4243
4244 error = 0;
4245 hp = NULL;
4246 sc = dev->si_drv1;
4247
4248 /*
4249 * Scan the list of registered ioctls, looking for handlers.
4250 */
4251 ACPI_LOCK(acpi);
4252 if (acpi_ioctl_hooks_initted)
4253 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
4254 if (hp->cmd == cmd)
4255 break;
4256 }
4257 ACPI_UNLOCK(acpi);
4258 if (hp)
4259 return (hp->fn(cmd, addr, hp->arg));
4260
4261 /*
4262 * Core ioctls are not permitted for non-writable user.
4263 * Currently, other ioctls just fetch information.
4264 * Not changing system behavior.
4265 */
4266 if ((flag & FWRITE) == 0)
4267 return (EPERM);
4268
4269 /* Core system ioctls. */
4270 switch (cmd) {
4271 case ACPIIO_REQSLPSTATE:
4272 sstate = *(int *)addr;
4273 if (sstate != ACPI_STATE_S5)
4274 return (acpi_ReqSleepState(sc, acpi_sstate_to_stype(sstate)));
4275 device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
4276 error = EOPNOTSUPP;
4277 break;
4278 case ACPIIO_ACKSLPSTATE:
4279 error = *(int *)addr;
4280 error = acpi_AckSleepState(sc->acpi_clone, error);
4281 break;
4282 case ACPIIO_SETSLPSTATE: /* DEPRECATED */
4283 sstate = *(int *)addr;
4284 if (sstate < ACPI_STATE_S0 || sstate > ACPI_STATE_S5)
4285 return (EINVAL);
4286 if (!acpi_supported_sstates[sstate])
4287 return (EOPNOTSUPP);
4288 if (ACPI_FAILURE(acpi_SetSleepState(sc, acpi_sstate_to_stype(sstate))))
4289 error = ENXIO;
4290 break;
4291 default:
4292 error = ENXIO;
4293 break;
4294 }
4295
4296 return (error);
4297 }
4298
4299 static int
acpi_sname_to_sstate(const char * sname)4300 acpi_sname_to_sstate(const char *sname)
4301 {
4302 int sstate;
4303
4304 if (toupper(sname[0]) == 'S') {
4305 sstate = sname[1] - '0';
4306 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
4307 sname[2] == '\0')
4308 return (sstate);
4309 } else if (strcasecmp(sname, "NONE") == 0)
4310 return (ACPI_STATE_UNKNOWN);
4311 return (-1);
4312 }
4313
4314 static const char *
acpi_sstate_to_sname(int state)4315 acpi_sstate_to_sname(int state)
4316 {
4317 static const char *snames[ACPI_S_STATE_COUNT] = {"S0", "S1", "S2", "S3",
4318 "S4", "S5"};
4319
4320 if (state == ACPI_STATE_UNKNOWN)
4321 return ("NONE");
4322 if (state >= ACPI_STATE_S0 && state < ACPI_S_STATE_COUNT)
4323 return (snames[state]);
4324 return (NULL);
4325 }
4326
4327 static int
acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)4328 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
4329 {
4330 int error;
4331 struct sbuf sb;
4332 UINT8 state;
4333
4334 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
4335 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
4336 if (acpi_supported_sstates[state])
4337 sbuf_printf(&sb, "%s ", acpi_sstate_to_sname(state));
4338 sbuf_trim(&sb);
4339 sbuf_finish(&sb);
4340 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
4341 sbuf_delete(&sb);
4342 return (error);
4343 }
4344
4345
4346 static int
acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)4347 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
4348 {
4349 char sleep_state[10];
4350 int error;
4351 int new_sstate, old_sstate;
4352
4353 old_sstate = *(int *)oidp->oid_arg1;
4354 strlcpy(sleep_state, acpi_sstate_to_sname(old_sstate), sizeof(sleep_state));
4355 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
4356 if (error == 0 && req->newptr != NULL) {
4357 new_sstate = acpi_sname_to_sstate(sleep_state);
4358 if (new_sstate < 0)
4359 return (EINVAL);
4360 if (new_sstate < ACPI_S_STATE_COUNT &&
4361 !acpi_supported_sstates[new_sstate])
4362 return (EOPNOTSUPP);
4363 if (new_sstate != old_sstate)
4364 *(int *)oidp->oid_arg1 = new_sstate;
4365 }
4366 return (error);
4367 }
4368
4369 static int
acpi_stype_sysctl(SYSCTL_HANDLER_ARGS)4370 acpi_stype_sysctl(SYSCTL_HANDLER_ARGS)
4371 {
4372 char name[10];
4373 int err;
4374 int sstate;
4375 enum power_stype new_stype, old_stype;
4376
4377 old_stype = *(enum power_stype *)oidp->oid_arg1;
4378 strlcpy(name, power_stype_to_name(old_stype), sizeof(name));
4379 err = sysctl_handle_string(oidp, name, sizeof(name), req);
4380 if (err != 0 || req->newptr == NULL)
4381 return (err);
4382
4383 new_stype = power_name_to_stype(name);
4384 if (new_stype == POWER_STYPE_UNKNOWN) {
4385 sstate = acpi_sname_to_sstate(name);
4386 if (sstate < 0)
4387 return (EINVAL);
4388 printf("warning: this sysctl expects a sleep type, but an ACPI S-state has "
4389 "been passed to it. This functionality is deprecated; see acpi(4).\n");
4390 MPASS(sstate < ACPI_S_STATE_COUNT);
4391 if (acpi_supported_sstates[sstate] == false)
4392 return (EOPNOTSUPP);
4393 new_stype = acpi_sstate_to_stype(sstate);
4394 }
4395
4396 if (acpi_supported_stypes[new_stype] == false)
4397 return (EOPNOTSUPP);
4398 if (new_stype != old_stype)
4399 *(enum power_stype *)oidp->oid_arg1 = new_stype;
4400 return (0);
4401 }
4402
4403 /* Inform devctl(4) when we receive a Notify. */
4404 void
acpi_UserNotify(const char * subsystem,ACPI_HANDLE h,uint8_t notify)4405 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
4406 {
4407 char notify_buf[16];
4408 ACPI_BUFFER handle_buf;
4409 ACPI_STATUS status;
4410
4411 if (subsystem == NULL)
4412 return;
4413
4414 handle_buf.Pointer = NULL;
4415 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
4416 status = AcpiNsHandleToPathname(h, &handle_buf, FALSE);
4417 if (ACPI_FAILURE(status))
4418 return;
4419 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
4420 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
4421 AcpiOsFree(handle_buf.Pointer);
4422 }
4423
4424 #ifdef ACPI_DEBUG
4425 /*
4426 * Support for parsing debug options from the kernel environment.
4427 *
4428 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
4429 * by specifying the names of the bits in the debug.acpi.layer and
4430 * debug.acpi.level environment variables. Bits may be unset by
4431 * prefixing the bit name with !.
4432 */
4433 struct debugtag
4434 {
4435 char *name;
4436 UINT32 value;
4437 };
4438
4439 static struct debugtag dbg_layer[] = {
4440 {"ACPI_UTILITIES", ACPI_UTILITIES},
4441 {"ACPI_HARDWARE", ACPI_HARDWARE},
4442 {"ACPI_EVENTS", ACPI_EVENTS},
4443 {"ACPI_TABLES", ACPI_TABLES},
4444 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
4445 {"ACPI_PARSER", ACPI_PARSER},
4446 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
4447 {"ACPI_EXECUTER", ACPI_EXECUTER},
4448 {"ACPI_RESOURCES", ACPI_RESOURCES},
4449 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
4450 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
4451 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
4452 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
4453
4454 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
4455 {"ACPI_BATTERY", ACPI_BATTERY},
4456 {"ACPI_BUS", ACPI_BUS},
4457 {"ACPI_BUTTON", ACPI_BUTTON},
4458 {"ACPI_EC", ACPI_EC},
4459 {"ACPI_FAN", ACPI_FAN},
4460 {"ACPI_POWERRES", ACPI_POWERRES},
4461 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
4462 {"ACPI_THERMAL", ACPI_THERMAL},
4463 {"ACPI_TIMER", ACPI_TIMER},
4464 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
4465 {NULL, 0}
4466 };
4467
4468 static struct debugtag dbg_level[] = {
4469 {"ACPI_LV_INIT", ACPI_LV_INIT},
4470 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
4471 {"ACPI_LV_INFO", ACPI_LV_INFO},
4472 {"ACPI_LV_REPAIR", ACPI_LV_REPAIR},
4473 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
4474
4475 /* Trace verbosity level 1 [Standard Trace Level] */
4476 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
4477 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
4478 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
4479 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
4480 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
4481 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
4482 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
4483 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
4484 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
4485 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
4486 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
4487 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
4488 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
4489 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
4490 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
4491
4492 /* Trace verbosity level 2 [Function tracing and memory allocation] */
4493 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
4494 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
4495 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
4496 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
4497 {"ACPI_LV_ALL", ACPI_LV_ALL},
4498
4499 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
4500 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
4501 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
4502 {"ACPI_LV_IO", ACPI_LV_IO},
4503 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
4504 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
4505
4506 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
4507 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
4508 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
4509 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
4510 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
4511 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
4512 {NULL, 0}
4513 };
4514
4515 static void
acpi_parse_debug(char * cp,struct debugtag * tag,UINT32 * flag)4516 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
4517 {
4518 char *ep;
4519 int i, l;
4520 int set;
4521
4522 while (*cp) {
4523 if (isspace(*cp)) {
4524 cp++;
4525 continue;
4526 }
4527 ep = cp;
4528 while (*ep && !isspace(*ep))
4529 ep++;
4530 if (*cp == '!') {
4531 set = 0;
4532 cp++;
4533 if (cp == ep)
4534 continue;
4535 } else {
4536 set = 1;
4537 }
4538 l = ep - cp;
4539 for (i = 0; tag[i].name != NULL; i++) {
4540 if (!strncmp(cp, tag[i].name, l)) {
4541 if (set)
4542 *flag |= tag[i].value;
4543 else
4544 *flag &= ~tag[i].value;
4545 }
4546 }
4547 cp = ep;
4548 }
4549 }
4550
4551 static void
acpi_set_debugging(void * junk)4552 acpi_set_debugging(void *junk)
4553 {
4554 char *layer, *level;
4555
4556 if (cold) {
4557 AcpiDbgLayer = 0;
4558 AcpiDbgLevel = 0;
4559 }
4560
4561 layer = kern_getenv("debug.acpi.layer");
4562 level = kern_getenv("debug.acpi.level");
4563 if (layer == NULL && level == NULL)
4564 return;
4565
4566 printf("ACPI set debug");
4567 if (layer != NULL) {
4568 if (strcmp("NONE", layer) != 0)
4569 printf(" layer '%s'", layer);
4570 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
4571 freeenv(layer);
4572 }
4573 if (level != NULL) {
4574 if (strcmp("NONE", level) != 0)
4575 printf(" level '%s'", level);
4576 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
4577 freeenv(level);
4578 }
4579 printf("\n");
4580 }
4581
4582 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
4583 NULL);
4584
4585 static int
acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)4586 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
4587 {
4588 int error, *dbg;
4589 struct debugtag *tag;
4590 struct sbuf sb;
4591 char temp[128];
4592
4593 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
4594 return (ENOMEM);
4595 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
4596 tag = &dbg_layer[0];
4597 dbg = &AcpiDbgLayer;
4598 } else {
4599 tag = &dbg_level[0];
4600 dbg = &AcpiDbgLevel;
4601 }
4602
4603 /* Get old values if this is a get request. */
4604 ACPI_SERIAL_BEGIN(acpi);
4605 if (*dbg == 0) {
4606 sbuf_cpy(&sb, "NONE");
4607 } else if (req->newptr == NULL) {
4608 for (; tag->name != NULL; tag++) {
4609 if ((*dbg & tag->value) == tag->value)
4610 sbuf_printf(&sb, "%s ", tag->name);
4611 }
4612 }
4613 sbuf_trim(&sb);
4614 sbuf_finish(&sb);
4615 strlcpy(temp, sbuf_data(&sb), sizeof(temp));
4616 sbuf_delete(&sb);
4617
4618 error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
4619
4620 /* Check for error or no change */
4621 if (error == 0 && req->newptr != NULL) {
4622 *dbg = 0;
4623 kern_setenv((char *)oidp->oid_arg1, temp);
4624 acpi_set_debugging(NULL);
4625 }
4626 ACPI_SERIAL_END(acpi);
4627
4628 return (error);
4629 }
4630
4631 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer,
4632 CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, "debug.acpi.layer", 0,
4633 acpi_debug_sysctl, "A",
4634 "");
4635 SYSCTL_PROC(_debug_acpi, OID_AUTO, level,
4636 CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, "debug.acpi.level", 0,
4637 acpi_debug_sysctl, "A",
4638 "");
4639 #endif /* ACPI_DEBUG */
4640
4641 static int
acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)4642 acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
4643 {
4644 int error;
4645 int old;
4646
4647 old = acpi_debug_objects;
4648 error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
4649 if (error != 0 || req->newptr == NULL)
4650 return (error);
4651 if (old == acpi_debug_objects || (old && acpi_debug_objects))
4652 return (0);
4653
4654 ACPI_SERIAL_BEGIN(acpi);
4655 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
4656 ACPI_SERIAL_END(acpi);
4657
4658 return (0);
4659 }
4660
4661 static int
acpi_parse_interfaces(char * str,struct acpi_interface * iface)4662 acpi_parse_interfaces(char *str, struct acpi_interface *iface)
4663 {
4664 char *p;
4665 size_t len;
4666 int i, j;
4667
4668 p = str;
4669 while (isspace(*p) || *p == ',')
4670 p++;
4671 len = strlen(p);
4672 if (len == 0)
4673 return (0);
4674 p = strdup(p, M_TEMP);
4675 for (i = 0; i < len; i++)
4676 if (p[i] == ',')
4677 p[i] = '\0';
4678 i = j = 0;
4679 while (i < len)
4680 if (isspace(p[i]) || p[i] == '\0')
4681 i++;
4682 else {
4683 i += strlen(p + i) + 1;
4684 j++;
4685 }
4686 if (j == 0) {
4687 free(p, M_TEMP);
4688 return (0);
4689 }
4690 iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK);
4691 iface->num = j;
4692 i = j = 0;
4693 while (i < len)
4694 if (isspace(p[i]) || p[i] == '\0')
4695 i++;
4696 else {
4697 iface->data[j] = p + i;
4698 i += strlen(p + i) + 1;
4699 j++;
4700 }
4701
4702 return (j);
4703 }
4704
4705 static void
acpi_free_interfaces(struct acpi_interface * iface)4706 acpi_free_interfaces(struct acpi_interface *iface)
4707 {
4708
4709 free(iface->data[0], M_TEMP);
4710 free(iface->data, M_TEMP);
4711 }
4712
4713 static void
acpi_reset_interfaces(device_t dev)4714 acpi_reset_interfaces(device_t dev)
4715 {
4716 struct acpi_interface list;
4717 ACPI_STATUS status;
4718 int i;
4719
4720 if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) {
4721 for (i = 0; i < list.num; i++) {
4722 status = AcpiInstallInterface(list.data[i]);
4723 if (ACPI_FAILURE(status))
4724 device_printf(dev,
4725 "failed to install _OSI(\"%s\"): %s\n",
4726 list.data[i], AcpiFormatException(status));
4727 else if (bootverbose)
4728 device_printf(dev, "installed _OSI(\"%s\")\n",
4729 list.data[i]);
4730 }
4731 acpi_free_interfaces(&list);
4732 }
4733 if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) {
4734 for (i = 0; i < list.num; i++) {
4735 status = AcpiRemoveInterface(list.data[i]);
4736 if (ACPI_FAILURE(status))
4737 device_printf(dev,
4738 "failed to remove _OSI(\"%s\"): %s\n",
4739 list.data[i], AcpiFormatException(status));
4740 else if (bootverbose)
4741 device_printf(dev, "removed _OSI(\"%s\")\n",
4742 list.data[i]);
4743 }
4744 acpi_free_interfaces(&list);
4745 }
4746 }
4747
4748 static int
acpi_pm_func(u_long cmd,void * arg,enum power_stype stype)4749 acpi_pm_func(u_long cmd, void *arg, enum power_stype stype)
4750 {
4751 int error;
4752 struct acpi_softc *sc;
4753
4754 error = 0;
4755 switch (cmd) {
4756 case POWER_CMD_SUSPEND:
4757 sc = (struct acpi_softc *)arg;
4758 if (sc == NULL) {
4759 error = EINVAL;
4760 goto out;
4761 }
4762 if (ACPI_FAILURE(acpi_EnterSleepState(sc, stype)))
4763 error = ENXIO;
4764 break;
4765 default:
4766 error = EINVAL;
4767 goto out;
4768 }
4769
4770 out:
4771 return (error);
4772 }
4773
4774 static void
acpi_pm_register(void * arg)4775 acpi_pm_register(void *arg)
4776 {
4777 if (!cold || resource_disabled("acpi", 0))
4778 return;
4779
4780 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL,
4781 acpi_supported_stypes);
4782 }
4783
4784 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, NULL);
4785