2 * Copyright (c) 2003-2005 Nate Lawson (SDG)
3 * Copyright (c) 2001 Michael Smith
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/power.h>
44 #include <dev/pci/pcivar.h>
45 #include <machine/atomic.h>
46 #include <machine/bus.h>
49 #include <contrib/dev/acpica/acpi.h>
50 #include <dev/acpica/acpivar.h>
53 * Support for ACPI Processor devices, including C[1-3] sleep states.
56 /* Hooks for the ACPI CA debugging infrastructure */
57 #define _COMPONENT ACPI_PROCESSOR
58 ACPI_MODULE_NAME("PROCESSOR")
61 struct resource *p_lvlx; /* Register to read to enter state. */
62 uint32_t type; /* C1-3 (C4 and up treated as C3). */
63 uint32_t trans_lat; /* Transition latency (usec). */
64 uint32_t power; /* Power consumed (mW). */
65 int res_type; /* Resource type for p_lvlx. */
67 #define MAX_CX_STATES 8
69 struct acpi_cpu_softc {
71 ACPI_HANDLE cpu_handle;
72 struct pcpu *cpu_pcpu;
73 uint32_t cpu_acpi_id; /* ACPI processor id */
74 uint32_t cpu_p_blk; /* ACPI P_BLK location */
75 uint32_t cpu_p_blk_len; /* P_BLK length (must be 6). */
76 struct acpi_cx cpu_cx_states[MAX_CX_STATES];
77 int cpu_cx_count; /* Number of valid Cx states. */
78 int cpu_prev_sleep;/* Last idle sleep duration. */
79 int cpu_features; /* Child driver supported features. */
81 int cpu_non_c3; /* Index of lowest non-C3 state. */
82 int cpu_short_slp; /* Count of < 1us sleeps. */
83 u_int cpu_cx_stats[MAX_CX_STATES];/* Cx usage history. */
84 /* Values for sysctl. */
85 struct sysctl_ctx_list cpu_sysctl_ctx;
86 struct sysctl_oid *cpu_sysctl_tree;
88 char cpu_cx_supported[64];
92 struct acpi_cpu_device {
93 struct resource_list ad_rl;
96 #define CPU_GET_REG(reg, width) \
97 (bus_space_read_ ## width(rman_get_bustag((reg)), \
98 rman_get_bushandle((reg)), 0))
99 #define CPU_SET_REG(reg, width, val) \
100 (bus_space_write_ ## width(rman_get_bustag((reg)), \
101 rman_get_bushandle((reg)), 0, (val)))
103 #define PM_USEC(x) ((x) >> 2) /* ~4 clocks per usec (3.57955 Mhz) */
105 #define ACPI_NOTIFY_CX_STATES 0x81 /* _CST changed. */
107 #define CPU_QUIRK_NO_C3 (1<<0) /* C3-type states are not usable. */
108 #define CPU_QUIRK_NO_BM_CTRL (1<<2) /* No bus mastering control. */
110 #define PCI_VENDOR_INTEL 0x8086
111 #define PCI_DEVICE_82371AB_3 0x7113 /* PIIX4 chipset for quirks. */
112 #define PCI_REVISION_A_STEP 0
113 #define PCI_REVISION_B_STEP 1
114 #define PCI_REVISION_4E 2
115 #define PCI_REVISION_4M 3
117 /* Platform hardware resource information. */
118 static uint32_t cpu_smi_cmd; /* Value to write to SMI_CMD. */
119 static uint8_t cpu_cst_cnt; /* Indicate we are _CST aware. */
120 static int cpu_quirks; /* Indicate any hardware bugs. */
123 static int cpu_disable_idle; /* Disable entry to idle function */
124 static int cpu_cx_count; /* Number of valid Cx states */
126 /* Values for sysctl. */
127 static struct sysctl_ctx_list cpu_sysctl_ctx;
128 static struct sysctl_oid *cpu_sysctl_tree;
129 static int cpu_cx_generic;
130 static int cpu_cx_lowest;
132 static device_t *cpu_devices;
133 static int cpu_ndevices;
134 static struct acpi_cpu_softc **cpu_softc;
135 ACPI_SERIAL_DECL(cpu, "ACPI CPU");
137 static int acpi_cpu_probe(device_t dev);
138 static int acpi_cpu_attach(device_t dev);
139 static int acpi_cpu_suspend(device_t dev);
140 static int acpi_cpu_resume(device_t dev);
141 static int acpi_pcpu_get_id(uint32_t idx, uint32_t *acpi_id,
143 static struct resource_list *acpi_cpu_get_rlist(device_t dev, device_t child);
144 static device_t acpi_cpu_add_child(device_t dev, int order, const char *name,
146 static int acpi_cpu_read_ivar(device_t dev, device_t child, int index,
148 static int acpi_cpu_shutdown(device_t dev);
149 static void acpi_cpu_cx_probe(struct acpi_cpu_softc *sc);
150 static void acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc);
151 static int acpi_cpu_cx_cst(struct acpi_cpu_softc *sc);
152 static void acpi_cpu_startup(void *arg);
153 static void acpi_cpu_startup_cx(struct acpi_cpu_softc *sc);
154 static void acpi_cpu_idle(void);
155 static void acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context);
156 static int acpi_cpu_quirks(void);
157 static int acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS);
158 static int acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val);
159 static int acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
160 static int acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
162 static device_method_t acpi_cpu_methods[] = {
163 /* Device interface */
164 DEVMETHOD(device_probe, acpi_cpu_probe),
165 DEVMETHOD(device_attach, acpi_cpu_attach),
166 DEVMETHOD(device_detach, bus_generic_detach),
167 DEVMETHOD(device_shutdown, acpi_cpu_shutdown),
168 DEVMETHOD(device_suspend, acpi_cpu_suspend),
169 DEVMETHOD(device_resume, acpi_cpu_resume),
172 DEVMETHOD(bus_add_child, acpi_cpu_add_child),
173 DEVMETHOD(bus_read_ivar, acpi_cpu_read_ivar),
174 DEVMETHOD(bus_get_resource_list, acpi_cpu_get_rlist),
175 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
176 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
177 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
178 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
179 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
180 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
181 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
182 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
183 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
188 static driver_t acpi_cpu_driver = {
191 sizeof(struct acpi_cpu_softc),
194 static devclass_t acpi_cpu_devclass;
195 DRIVER_MODULE(cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0);
196 MODULE_DEPEND(cpu, acpi, 1, 1, 1);
199 acpi_cpu_probe(device_t dev)
207 if (acpi_disabled("cpu") || acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
210 handle = acpi_get_handle(dev);
211 if (cpu_softc == NULL)
212 cpu_softc = malloc(sizeof(struct acpi_cpu_softc *) *
213 (mp_maxid + 1), M_TEMP /* XXX */, M_WAITOK | M_ZERO);
215 /* Get our Processor object. */
217 buf.Length = ACPI_ALLOCATE_BUFFER;
218 status = AcpiEvaluateObject(handle, NULL, NULL, &buf);
219 if (ACPI_FAILURE(status)) {
220 device_printf(dev, "probe failed to get Processor obj - %s\n",
221 AcpiFormatException(status));
224 obj = (ACPI_OBJECT *)buf.Pointer;
225 if (obj->Type != ACPI_TYPE_PROCESSOR) {
226 device_printf(dev, "Processor object has bad type %d\n", obj->Type);
232 * Find the processor associated with our unit. We could use the
233 * ProcId as a key, however, some boxes do not have the same values
234 * in their Processor object as the ProcId values in the MADT.
236 acpi_id = obj->Processor.ProcId;
238 if (acpi_pcpu_get_id(device_get_unit(dev), &acpi_id, &cpu_id) != 0)
242 * Check if we already probed this processor. We scan the bus twice
243 * so it's possible we've already seen this one.
245 if (cpu_softc[cpu_id] != NULL)
248 /* Mark this processor as in-use and save our derived id for attach. */
249 cpu_softc[cpu_id] = (void *)1;
250 acpi_set_magic(dev, cpu_id);
251 device_set_desc(dev, "ACPI CPU");
257 acpi_cpu_attach(device_t dev)
260 ACPI_OBJECT arg[4], *obj;
261 ACPI_OBJECT_LIST arglist;
262 struct pcpu *pcpu_data;
263 struct acpi_cpu_softc *sc;
264 struct acpi_softc *acpi_sc;
267 int cpu_id, drv_count, i;
271 /* UUID needed by _OSC evaluation */
272 static uint8_t cpu_oscuuid[16] = { 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29,
273 0xBE, 0x47, 0x9E, 0xBD, 0xD8, 0x70,
274 0x58, 0x71, 0x39, 0x53 };
276 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
278 sc = device_get_softc(dev);
280 sc->cpu_handle = acpi_get_handle(dev);
281 cpu_id = acpi_get_magic(dev);
282 cpu_softc[cpu_id] = sc;
283 pcpu_data = pcpu_find(cpu_id);
284 pcpu_data->pc_device = dev;
285 sc->cpu_pcpu = pcpu_data;
286 cpu_smi_cmd = AcpiGbl_FADT.SmiCommand;
287 cpu_cst_cnt = AcpiGbl_FADT.CstControl;
290 buf.Length = ACPI_ALLOCATE_BUFFER;
291 status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf);
292 if (ACPI_FAILURE(status)) {
293 device_printf(dev, "attach failed to get Processor obj - %s\n",
294 AcpiFormatException(status));
297 obj = (ACPI_OBJECT *)buf.Pointer;
298 sc->cpu_p_blk = obj->Processor.PblkAddress;
299 sc->cpu_p_blk_len = obj->Processor.PblkLength;
300 sc->cpu_acpi_id = obj->Processor.ProcId;
302 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_BLK at %#x/%d\n",
303 device_get_unit(dev), sc->cpu_p_blk, sc->cpu_p_blk_len));
306 * If this is the first cpu we attach, create and initialize the generic
307 * resources that will be used by all acpi cpu devices.
309 if (device_get_unit(dev) == 0) {
310 /* Assume we won't be using generic Cx mode by default */
311 cpu_cx_generic = FALSE;
313 /* Install hw.acpi.cpu sysctl tree */
314 acpi_sc = acpi_device_get_parent_softc(dev);
315 sysctl_ctx_init(&cpu_sysctl_ctx);
316 cpu_sysctl_tree = SYSCTL_ADD_NODE(&cpu_sysctl_ctx,
317 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, "cpu",
318 CTLFLAG_RD, 0, "node for CPU children");
320 /* Queue post cpu-probing task handler */
321 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cpu_startup, NULL);
325 * Before calling any CPU methods, collect child driver feature hints
326 * and notify ACPI of them. We support unified SMP power control
327 * so advertise this ourselves. Note this is not the same as independent
328 * SMP control where each CPU can have different settings.
330 sc->cpu_features = ACPI_CAP_SMP_SAME | ACPI_CAP_SMP_SAME_C3;
331 if (devclass_get_drivers(acpi_cpu_devclass, &drivers, &drv_count) == 0) {
332 for (i = 0; i < drv_count; i++) {
333 if (ACPI_GET_FEATURES(drivers[i], &features) == 0)
334 sc->cpu_features |= features;
336 free(drivers, M_TEMP);
340 * CPU capabilities are specified as a buffer of 32-bit integers:
341 * revision, count, and one or more capabilities. The revision of
342 * "1" is not specified anywhere but seems to match Linux.
344 if (sc->cpu_features) {
345 arglist.Pointer = arg;
347 arg[0].Type = ACPI_TYPE_BUFFER;
348 arg[0].Buffer.Length = sizeof(cap_set);
349 arg[0].Buffer.Pointer = (uint8_t *)cap_set;
350 cap_set[0] = 1; /* revision */
351 cap_set[1] = 1; /* number of capabilities integers */
352 cap_set[2] = sc->cpu_features;
353 AcpiEvaluateObject(sc->cpu_handle, "_PDC", &arglist, NULL);
356 * On some systems we need to evaluate _OSC so that the ASL
357 * loads the _PSS and/or _PDC methods at runtime.
359 * TODO: evaluate failure of _OSC.
361 arglist.Pointer = arg;
363 arg[0].Type = ACPI_TYPE_BUFFER;
364 arg[0].Buffer.Length = sizeof(cpu_oscuuid);
365 arg[0].Buffer.Pointer = cpu_oscuuid; /* UUID */
366 arg[1].Type = ACPI_TYPE_INTEGER;
367 arg[1].Integer.Value = 1; /* revision */
368 arg[2].Type = ACPI_TYPE_INTEGER;
369 arg[2].Integer.Value = 1; /* count */
370 arg[3].Type = ACPI_TYPE_BUFFER;
371 arg[3].Buffer.Length = sizeof(cap_set); /* Capabilities buffer */
372 arg[3].Buffer.Pointer = (uint8_t *)cap_set;
374 AcpiEvaluateObject(sc->cpu_handle, "_OSC", &arglist, NULL);
377 /* Probe for Cx state support. */
378 acpi_cpu_cx_probe(sc);
380 /* Finally, call identify and probe/attach for child devices. */
381 bus_generic_probe(dev);
382 bus_generic_attach(dev);
388 * Disable any entry to the idle function during suspend and re-enable it
392 acpi_cpu_suspend(device_t dev)
396 error = bus_generic_suspend(dev);
399 cpu_disable_idle = TRUE;
404 acpi_cpu_resume(device_t dev)
407 cpu_disable_idle = FALSE;
408 return (bus_generic_resume(dev));
412 * Find the nth present CPU and return its pc_cpuid as well as set the
413 * pc_acpi_id from the most reliable source.
416 acpi_pcpu_get_id(uint32_t idx, uint32_t *acpi_id, uint32_t *cpu_id)
418 struct pcpu *pcpu_data;
421 KASSERT(acpi_id != NULL, ("Null acpi_id"));
422 KASSERT(cpu_id != NULL, ("Null cpu_id"));
423 for (i = 0; i <= mp_maxid; i++) {
426 pcpu_data = pcpu_find(i);
427 KASSERT(pcpu_data != NULL, ("no pcpu data for %d", i));
430 * If pc_acpi_id was not initialized (e.g., a non-APIC UP box)
431 * override it with the value from the ASL. Otherwise, if the
432 * two don't match, prefer the MADT-derived value. Finally,
433 * return the pc_cpuid to reference this processor.
435 if (pcpu_data->pc_acpi_id == 0xffffffff)
436 pcpu_data->pc_acpi_id = *acpi_id;
437 else if (pcpu_data->pc_acpi_id != *acpi_id)
438 *acpi_id = pcpu_data->pc_acpi_id;
439 *cpu_id = pcpu_data->pc_cpuid;
447 static struct resource_list *
448 acpi_cpu_get_rlist(device_t dev, device_t child)
450 struct acpi_cpu_device *ad;
452 ad = device_get_ivars(child);
459 acpi_cpu_add_child(device_t dev, int order, const char *name, int unit)
461 struct acpi_cpu_device *ad;
464 if ((ad = malloc(sizeof(*ad), M_TEMP, M_NOWAIT | M_ZERO)) == NULL)
467 resource_list_init(&ad->ad_rl);
469 child = device_add_child_ordered(dev, order, name, unit);
471 device_set_ivars(child, ad);
478 acpi_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
480 struct acpi_cpu_softc *sc;
482 sc = device_get_softc(dev);
484 case ACPI_IVAR_HANDLE:
485 *result = (uintptr_t)sc->cpu_handle;
488 *result = (uintptr_t)sc->cpu_pcpu;
497 acpi_cpu_shutdown(device_t dev)
499 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
501 /* Allow children to shutdown first. */
502 bus_generic_shutdown(dev);
504 /* Disable any entry to the idle function. */
505 cpu_disable_idle = TRUE;
507 /* Signal and wait for all processors to exit acpi_cpu_idle(). */
508 smp_rendezvous(NULL, NULL, NULL, NULL);
514 acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
516 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
518 /* Use initial sleep value of 1 sec. to start with lowest idle state. */
519 sc->cpu_prev_sleep = 1000000;
520 sc->cpu_cx_lowest = 0;
523 * Check for the ACPI 2.0 _CST sleep states object. If we can't find
524 * any, we'll revert to generic FADT/P_BLK Cx control method which will
525 * be handled by acpi_cpu_startup. We need to defer to after having
526 * probed all the cpus in the system before probing for generic Cx
527 * states as we may already have found cpus with valid _CST packages
529 if (!cpu_cx_generic && acpi_cpu_cx_cst(sc) != 0) {
531 * We were unable to find a _CST package for this cpu or there
532 * was an error parsing it. Switch back to generic mode.
534 cpu_cx_generic = TRUE;
536 device_printf(sc->cpu_dev, "switching to generic Cx mode\n");
540 * TODO: _CSD Package should be checked here.
545 acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc)
547 ACPI_GENERIC_ADDRESS gas;
548 struct acpi_cx *cx_ptr;
550 sc->cpu_cx_count = 0;
551 cx_ptr = sc->cpu_cx_states;
553 /* Use initial sleep value of 1 sec. to start with lowest idle state. */
554 sc->cpu_prev_sleep = 1000000;
556 /* C1 has been required since just after ACPI 1.0 */
557 cx_ptr->type = ACPI_STATE_C1;
558 cx_ptr->trans_lat = 0;
563 * The spec says P_BLK must be 6 bytes long. However, some systems
564 * use it to indicate a fractional set of features present so we
565 * take 5 as C2. Some may also have a value of 7 to indicate
566 * another C3 but most use _CST for this (as required) and having
567 * "only" C1-C3 is not a hardship.
569 if (sc->cpu_p_blk_len < 5)
572 /* Validate and allocate resources for C2 (P_LVL2). */
573 gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
575 if (AcpiGbl_FADT.C2Latency <= 100) {
576 gas.Address = sc->cpu_p_blk + 4;
577 acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid,
578 &gas, &cx_ptr->p_lvlx, RF_SHAREABLE);
579 if (cx_ptr->p_lvlx != NULL) {
581 cx_ptr->type = ACPI_STATE_C2;
582 cx_ptr->trans_lat = AcpiGbl_FADT.C2Latency;
587 if (sc->cpu_p_blk_len < 6)
590 /* Validate and allocate resources for C3 (P_LVL3). */
591 if (AcpiGbl_FADT.C3Latency <= 1000) {
592 gas.Address = sc->cpu_p_blk + 5;
593 acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid, &gas,
594 &cx_ptr->p_lvlx, RF_SHAREABLE);
595 if (cx_ptr->p_lvlx != NULL) {
597 cx_ptr->type = ACPI_STATE_C3;
598 cx_ptr->trans_lat = AcpiGbl_FADT.C3Latency;
604 /* Update the largest cx_count seen so far */
605 if (sc->cpu_cx_count > cpu_cx_count)
606 cpu_cx_count = sc->cpu_cx_count;
610 * Parse a _CST package and set up its Cx states. Since the _CST object
611 * can change dynamically, our notify handler may call this function
612 * to clean up and probe the new _CST package.
615 acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
617 struct acpi_cx *cx_ptr;
625 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
628 buf.Length = ACPI_ALLOCATE_BUFFER;
629 status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
630 if (ACPI_FAILURE(status))
633 /* _CST is a package with a count and at least one Cx package. */
634 top = (ACPI_OBJECT *)buf.Pointer;
635 if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
636 device_printf(sc->cpu_dev, "invalid _CST package\n");
637 AcpiOsFree(buf.Pointer);
640 if (count != top->Package.Count - 1) {
641 device_printf(sc->cpu_dev, "invalid _CST state count (%d != %d)\n",
642 count, top->Package.Count - 1);
643 count = top->Package.Count - 1;
645 if (count > MAX_CX_STATES) {
646 device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
647 count = MAX_CX_STATES;
650 /* Set up all valid states. */
651 sc->cpu_cx_count = 0;
652 cx_ptr = sc->cpu_cx_states;
653 for (i = 0; i < count; i++) {
654 pkg = &top->Package.Elements[i + 1];
655 if (!ACPI_PKG_VALID(pkg, 4) ||
656 acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
657 acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
658 acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
660 device_printf(sc->cpu_dev, "skipping invalid Cx state package\n");
664 /* Validate the state to see if we should use it. */
665 switch (cx_ptr->type) {
672 if (cx_ptr->trans_lat > 100) {
673 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
674 "acpi_cpu%d: C2[%d] not available.\n",
675 device_get_unit(sc->cpu_dev), i));
682 if (cx_ptr->trans_lat > 1000 ||
683 (cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
685 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
686 "acpi_cpu%d: C3[%d] not available.\n",
687 device_get_unit(sc->cpu_dev), i));
694 /* Free up any previous register. */
695 if (cx_ptr->p_lvlx != NULL) {
696 bus_release_resource(sc->cpu_dev, 0, 0, cx_ptr->p_lvlx);
697 cx_ptr->p_lvlx = NULL;
701 /* Allocate the control register for C2 or C3. */
702 acpi_PkgGas(sc->cpu_dev, pkg, 0, &cx_ptr->res_type, &sc->cpu_rid,
703 &cx_ptr->p_lvlx, RF_SHAREABLE);
704 if (cx_ptr->p_lvlx) {
706 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
707 "acpi_cpu%d: Got C%d - %d latency\n",
708 device_get_unit(sc->cpu_dev), cx_ptr->type,
714 AcpiOsFree(buf.Pointer);
720 * Call this *after* all CPUs have been attached.
723 acpi_cpu_startup(void *arg)
725 struct acpi_cpu_softc *sc;
728 /* Get set of CPU devices */
729 devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
732 * Setup any quirks that might necessary now that we have probed
738 if (cpu_cx_generic) {
740 * We are using generic Cx mode, probe for available Cx states
741 * for all processors.
743 for (i = 0; i < cpu_ndevices; i++) {
744 sc = device_get_softc(cpu_devices[i]);
745 acpi_cpu_generic_cx_probe(sc);
749 * Find the highest Cx state common to all CPUs
750 * in the system, taking quirks into account.
752 for (i = 0; i < cpu_ndevices; i++) {
753 sc = device_get_softc(cpu_devices[i]);
754 if (sc->cpu_cx_count < cpu_cx_count)
755 cpu_cx_count = sc->cpu_cx_count;
759 * We are using _CST mode, remove C3 state if necessary.
760 * Update the largest Cx state supported in the global cpu_cx_count.
761 * It will be used in the global Cx sysctl handler.
762 * As we now know for sure that we will be using _CST mode
763 * install our notify handler.
765 for (i = 0; i < cpu_ndevices; i++) {
766 sc = device_get_softc(cpu_devices[i]);
767 if (cpu_quirks && CPU_QUIRK_NO_C3) {
768 sc->cpu_cx_count = sc->cpu_non_c3 + 1;
770 if (sc->cpu_cx_count > cpu_cx_count)
771 cpu_cx_count = sc->cpu_cx_count;
772 AcpiInstallNotifyHandler(sc->cpu_handle, ACPI_DEVICE_NOTIFY,
773 acpi_cpu_notify, sc);
777 /* Perform Cx final initialization. */
778 for (i = 0; i < cpu_ndevices; i++) {
779 sc = device_get_softc(cpu_devices[i]);
780 acpi_cpu_startup_cx(sc);
783 /* Add a sysctl handler to handle global Cx lowest setting */
784 SYSCTL_ADD_PROC(&cpu_sysctl_ctx, SYSCTL_CHILDREN(cpu_sysctl_tree),
785 OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
786 NULL, 0, acpi_cpu_global_cx_lowest_sysctl, "A",
787 "Global lowest Cx sleep state to use");
789 /* Take over idling from cpu_idle_default(). */
791 cpu_disable_idle = FALSE;
792 cpu_idle_hook = acpi_cpu_idle;
796 acpi_cpu_startup_cx(struct acpi_cpu_softc *sc)
802 * Set up the list of Cx states
805 sbuf_new(&sb, sc->cpu_cx_supported, sizeof(sc->cpu_cx_supported),
807 for (i = 0; i < sc->cpu_cx_count; i++) {
808 sbuf_printf(&sb, "C%d/%d ", i + 1, sc->cpu_cx_states[i].trans_lat);
809 if (sc->cpu_cx_states[i].type < ACPI_STATE_C3)
815 SYSCTL_ADD_STRING(&sc->cpu_sysctl_ctx,
816 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
817 OID_AUTO, "cx_supported", CTLFLAG_RD,
818 sc->cpu_cx_supported, 0,
819 "Cx/microsecond values for supported Cx states");
820 SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
821 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
822 OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
823 (void *)sc, 0, acpi_cpu_cx_lowest_sysctl, "A",
824 "lowest Cx sleep state to use");
825 SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
826 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
827 OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
828 (void *)sc, 0, acpi_cpu_usage_sysctl, "A",
829 "percent usage for each Cx state");
832 /* Signal platform that we can handle _CST notification. */
833 if (!cpu_cx_generic && cpu_cst_cnt != 0) {
835 AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
842 * Idle the CPU in the lowest state possible. This function is called with
843 * interrupts disabled. Note that once it re-enables interrupts, a task
844 * switch can occur so do not access shared data (i.e. the softc) after
845 * interrupts are re-enabled.
850 struct acpi_cpu_softc *sc;
851 struct acpi_cx *cx_next;
852 uint32_t start_time, end_time;
853 int bm_active, cx_next_idx, i;
855 /* If disabled, return immediately. */
856 if (cpu_disable_idle) {
862 * Look up our CPU id to get our softc. If it's NULL, we'll use C1
863 * since there is no ACPI processor object for this CPU. This occurs
864 * for logical CPUs in the HTT case.
866 sc = cpu_softc[PCPU_GET(cpuid)];
873 * If we slept 100 us or more, use the lowest Cx state. Otherwise,
874 * find the lowest state that has a latency less than or equal to
875 * the length of our last sleep.
877 cx_next_idx = sc->cpu_cx_lowest;
878 if (sc->cpu_prev_sleep < 100) {
880 * If we sleep too short all the time, this system may not implement
881 * C2/3 correctly (i.e. reads return immediately). In this case,
882 * back off and use the next higher level.
883 * It seems that when you have a dual core cpu (like the Intel Core Duo)
884 * that both cores will get out of C3 state as soon as one of them
885 * requires it. This breaks the sleep detection logic as the sleep
886 * counter is local to each cpu. Disable the sleep logic for now as a
887 * workaround if there's more than one CPU. The right fix would probably
888 * be to add quirks for system that don't really support C3 state.
890 if (mp_ncpus < 2 && sc->cpu_prev_sleep <= 1) {
892 if (sc->cpu_short_slp == 1000 && sc->cpu_cx_lowest != 0) {
893 if (sc->cpu_non_c3 == sc->cpu_cx_lowest && sc->cpu_non_c3 != 0)
896 sc->cpu_short_slp = 0;
897 device_printf(sc->cpu_dev,
898 "too many short sleeps, backing off to C%d\n",
899 sc->cpu_cx_lowest + 1);
902 sc->cpu_short_slp = 0;
904 for (i = sc->cpu_cx_lowest; i >= 0; i--)
905 if (sc->cpu_cx_states[i].trans_lat <= sc->cpu_prev_sleep) {
912 * Check for bus master activity. If there was activity, clear
913 * the bit and use the lowest non-C3 state. Note that the USB
914 * driver polling for new devices keeps this bit set all the
915 * time if USB is loaded.
917 if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
918 AcpiGetRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active);
919 if (bm_active != 0) {
920 AcpiSetRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
921 cx_next_idx = min(cx_next_idx, sc->cpu_non_c3);
925 /* Select the next state and update statistics. */
926 cx_next = &sc->cpu_cx_states[cx_next_idx];
927 sc->cpu_cx_stats[cx_next_idx]++;
928 KASSERT(cx_next->type != ACPI_STATE_C0, ("acpi_cpu_idle: C0 sleep"));
931 * Execute HLT (or equivalent) and wait for an interrupt. We can't
932 * calculate the time spent in C1 since the place we wake up is an
933 * ISR. Assume we slept one quantum and return.
935 if (cx_next->type == ACPI_STATE_C1) {
936 sc->cpu_prev_sleep = 1000000 / hz;
942 * For C3, disable bus master arbitration and enable bus master wake
943 * if BM control is available, otherwise flush the CPU cache.
945 if (cx_next->type == ACPI_STATE_C3) {
946 if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
947 AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 1);
948 AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
950 ACPI_FLUSH_CPU_CACHE();
954 * Read from P_LVLx to enter C2(+), checking time spent asleep.
955 * Use the ACPI timer for measuring sleep time. Since we need to
956 * get the time very close to the CPU start/stop clock logic, this
957 * is the only reliable time source.
959 AcpiHwLowLevelRead(32, &start_time, &AcpiGbl_FADT.XPmTimerBlock);
960 CPU_GET_REG(cx_next->p_lvlx, 1);
963 * Read the end time twice. Since it may take an arbitrary time
964 * to enter the idle state, the first read may be executed before
965 * the processor has stopped. Doing it again provides enough
966 * margin that we are certain to have a correct value.
968 AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT.XPmTimerBlock);
969 AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT.XPmTimerBlock);
971 /* Enable bus master arbitration and disable bus master wakeup. */
972 if (cx_next->type == ACPI_STATE_C3 &&
973 (cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
974 AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 0);
975 AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
979 /* Find the actual time asleep in microseconds, minus overhead. */
980 end_time = acpi_TimerDelta(end_time, start_time);
981 sc->cpu_prev_sleep = PM_USEC(end_time) - cx_next->trans_lat;
985 * Re-evaluate the _CST object when we are notified that it changed.
987 * XXX Re-evaluation disabled until locking is done.
990 acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context)
992 struct acpi_cpu_softc *sc = (struct acpi_cpu_softc *)context;
994 if (notify != ACPI_NOTIFY_CX_STATES)
997 device_printf(sc->cpu_dev, "Cx states changed\n");
998 /* acpi_cpu_cx_cst(sc); */
1002 acpi_cpu_quirks(void)
1006 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1009 * Bus mastering arbitration control is needed to keep caches coherent
1010 * while sleeping in C3. If it's not present but a working flush cache
1011 * instruction is present, flush the caches before entering C3 instead.
1012 * Otherwise, just disable C3 completely.
1014 if (AcpiGbl_FADT.Pm2ControlBlock == 0 ||
1015 AcpiGbl_FADT.Pm2ControlLength == 0) {
1016 if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) &&
1017 (AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0) {
1018 cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
1019 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1020 "acpi_cpu: no BM control, using flush cache method\n"));
1022 cpu_quirks |= CPU_QUIRK_NO_C3;
1023 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1024 "acpi_cpu: no BM control, C3 not available\n"));
1029 * If we are using generic Cx mode, C3 on multiple CPUs requires using
1030 * the expensive flush cache instruction.
1032 if (cpu_cx_generic && mp_ncpus > 1) {
1033 cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
1034 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1035 "acpi_cpu: SMP, using flush cache mode for C3\n"));
1038 /* Look for various quirks of the PIIX4 part. */
1039 acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
1040 if (acpi_dev != NULL) {
1041 switch (pci_get_revid(acpi_dev)) {
1043 * Disable C3 support for all PIIX4 chipsets. Some of these parts
1044 * do not report the BMIDE status to the BM status register and
1045 * others have a livelock bug if Type-F DMA is enabled. Linux
1046 * works around the BMIDE bug by reading the BM status directly
1047 * but we take the simpler approach of disabling C3 for these
1050 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
1051 * Livelock") from the January 2002 PIIX4 specification update.
1052 * Applies to all PIIX4 models.
1054 case PCI_REVISION_4E:
1055 case PCI_REVISION_4M:
1056 cpu_quirks |= CPU_QUIRK_NO_C3;
1057 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1058 "acpi_cpu: working around PIIX4 bug, disabling C3\n"));
1069 acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS)
1071 struct acpi_cpu_softc *sc;
1075 uintmax_t fract, sum, whole;
1077 sc = (struct acpi_cpu_softc *) arg1;
1079 for (i = 0; i < sc->cpu_cx_count; i++)
1080 sum += sc->cpu_cx_stats[i];
1081 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1082 for (i = 0; i < sc->cpu_cx_count; i++) {
1084 whole = (uintmax_t)sc->cpu_cx_stats[i] * 100;
1085 fract = (whole % sum) * 100;
1086 sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
1087 (u_int)(fract / sum));
1089 sbuf_printf(&sb, "0%% ");
1093 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1100 acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val)
1104 ACPI_SERIAL_ASSERT(cpu);
1105 sc->cpu_cx_lowest = val;
1107 /* If not disabling, cache the new lowest non-C3 state. */
1109 for (i = sc->cpu_cx_lowest; i >= 0; i--) {
1110 if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
1116 /* Reset the statistics counters. */
1117 bzero(sc->cpu_cx_stats, sizeof(sc->cpu_cx_stats));
1122 acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1124 struct acpi_cpu_softc *sc;
1128 sc = (struct acpi_cpu_softc *) arg1;
1129 snprintf(state, sizeof(state), "C%d", sc->cpu_cx_lowest + 1);
1130 error = sysctl_handle_string(oidp, state, sizeof(state), req);
1131 if (error != 0 || req->newptr == NULL)
1133 if (strlen(state) < 2 || toupper(state[0]) != 'C')
1135 val = (int) strtol(state + 1, NULL, 10) - 1;
1136 if (val < 0 || val > sc->cpu_cx_count - 1)
1139 ACPI_SERIAL_BEGIN(cpu);
1140 acpi_cpu_set_cx_lowest(sc, val);
1141 ACPI_SERIAL_END(cpu);
1147 acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1149 struct acpi_cpu_softc *sc;
1153 snprintf(state, sizeof(state), "C%d", cpu_cx_lowest + 1);
1154 error = sysctl_handle_string(oidp, state, sizeof(state), req);
1155 if (error != 0 || req->newptr == NULL)
1157 if (strlen(state) < 2 || toupper(state[0]) != 'C')
1159 val = (int) strtol(state + 1, NULL, 10) - 1;
1160 if (val < 0 || val > cpu_cx_count - 1)
1162 cpu_cx_lowest = val;
1164 /* Update the new lowest useable Cx state for all CPUs. */
1165 ACPI_SERIAL_BEGIN(cpu);
1166 for (i = 0; i < cpu_ndevices; i++) {
1167 sc = device_get_softc(cpu_devices[i]);
1168 acpi_cpu_set_cx_lowest(sc, val);
1170 ACPI_SERIAL_END(cpu);