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.
55 * TODO: implement scans of all CPUs to be sure all Cx states are
59 /* Hooks for the ACPI CA debugging infrastructure */
60 #define _COMPONENT ACPI_PROCESSOR
61 ACPI_MODULE_NAME("PROCESSOR")
64 struct resource *p_lvlx; /* Register to read to enter state. */
65 uint32_t type; /* C1-3 (C4 and up treated as C3). */
66 uint32_t trans_lat; /* Transition latency (usec). */
67 uint32_t power; /* Power consumed (mW). */
68 int res_type; /* Resource type for p_lvlx. */
70 #define MAX_CX_STATES 8
72 struct acpi_cpu_softc {
74 ACPI_HANDLE cpu_handle;
75 struct pcpu *cpu_pcpu;
76 uint32_t cpu_acpi_id; /* ACPI processor id */
77 uint32_t cpu_p_blk; /* ACPI P_BLK location */
78 uint32_t cpu_p_blk_len; /* P_BLK length (must be 6). */
79 struct acpi_cx cpu_cx_states[MAX_CX_STATES];
80 int cpu_cx_count; /* Number of valid Cx states. */
81 int cpu_prev_sleep;/* Last idle sleep duration. */
82 int cpu_features; /* Child driver supported features. */
85 struct acpi_cpu_device {
86 struct resource_list ad_rl;
89 #define CPU_GET_REG(reg, width) \
90 (bus_space_read_ ## width(rman_get_bustag((reg)), \
91 rman_get_bushandle((reg)), 0))
92 #define CPU_SET_REG(reg, width, val) \
93 (bus_space_write_ ## width(rman_get_bustag((reg)), \
94 rman_get_bushandle((reg)), 0, (val)))
96 #define PM_USEC(x) ((x) >> 2) /* ~4 clocks per usec (3.57955 Mhz) */
98 #define ACPI_NOTIFY_CX_STATES 0x81 /* _CST changed. */
100 #define CPU_QUIRK_NO_C3 (1<<0) /* C3-type states are not usable. */
101 #define CPU_QUIRK_NO_BM_CTRL (1<<2) /* No bus mastering control. */
103 #define PCI_VENDOR_INTEL 0x8086
104 #define PCI_DEVICE_82371AB_3 0x7113 /* PIIX4 chipset for quirks. */
105 #define PCI_REVISION_A_STEP 0
106 #define PCI_REVISION_B_STEP 1
107 #define PCI_REVISION_4E 2
108 #define PCI_REVISION_4M 3
110 /* Platform hardware resource information. */
111 static uint32_t cpu_smi_cmd; /* Value to write to SMI_CMD. */
112 static uint8_t cpu_cst_cnt; /* Indicate we are _CST aware. */
113 static int cpu_rid; /* Driver-wide resource id. */
114 static int cpu_quirks; /* Indicate any hardware bugs. */
117 static int cpu_cx_count; /* Number of valid states */
118 static int cpu_non_c3; /* Index of lowest non-C3 state. */
119 static int cpu_short_slp; /* Count of < 1us sleeps. */
120 static u_int cpu_cx_stats[MAX_CX_STATES];/* Cx usage history. */
122 /* Values for sysctl. */
123 static struct sysctl_ctx_list acpi_cpu_sysctl_ctx;
124 static struct sysctl_oid *acpi_cpu_sysctl_tree;
125 static int cpu_cx_lowest;
126 static char cpu_cx_supported[64];
128 static device_t *cpu_devices;
129 static int cpu_ndevices;
130 static struct acpi_cpu_softc **cpu_softc;
131 ACPI_SERIAL_DECL(cpu, "ACPI CPU");
133 static int acpi_cpu_probe(device_t dev);
134 static int acpi_cpu_attach(device_t dev);
135 static int acpi_pcpu_get_id(uint32_t idx, uint32_t *acpi_id,
137 static struct resource_list *acpi_cpu_get_rlist(device_t dev, device_t child);
138 static device_t acpi_cpu_add_child(device_t dev, int order, const char *name,
140 static int acpi_cpu_read_ivar(device_t dev, device_t child, int index,
142 static int acpi_cpu_shutdown(device_t dev);
143 static int acpi_cpu_cx_probe(struct acpi_cpu_softc *sc);
144 static int acpi_cpu_cx_cst(struct acpi_cpu_softc *sc);
145 static void acpi_cpu_startup(void *arg);
146 static void acpi_cpu_startup_cx(void);
147 static void acpi_cpu_idle(void);
148 static void acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context);
149 static int acpi_cpu_quirks(struct acpi_cpu_softc *sc);
150 static int acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS);
151 static int acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
153 static device_method_t acpi_cpu_methods[] = {
154 /* Device interface */
155 DEVMETHOD(device_probe, acpi_cpu_probe),
156 DEVMETHOD(device_attach, acpi_cpu_attach),
157 DEVMETHOD(device_detach, bus_generic_detach),
158 DEVMETHOD(device_shutdown, acpi_cpu_shutdown),
159 DEVMETHOD(device_suspend, bus_generic_suspend),
160 DEVMETHOD(device_resume, bus_generic_resume),
163 DEVMETHOD(bus_add_child, acpi_cpu_add_child),
164 DEVMETHOD(bus_read_ivar, acpi_cpu_read_ivar),
165 DEVMETHOD(bus_get_resource_list, acpi_cpu_get_rlist),
166 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
167 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
168 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
169 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
170 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
171 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
172 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
173 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
174 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
179 static driver_t acpi_cpu_driver = {
182 sizeof(struct acpi_cpu_softc),
185 static devclass_t acpi_cpu_devclass;
186 DRIVER_MODULE(cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0);
187 MODULE_DEPEND(cpu, acpi, 1, 1, 1);
190 acpi_cpu_probe(device_t dev)
198 if (acpi_disabled("cpu") || acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
201 handle = acpi_get_handle(dev);
202 if (cpu_softc == NULL)
203 cpu_softc = malloc(sizeof(struct acpi_cpu_softc *) *
204 (mp_maxid + 1), M_TEMP /* XXX */, M_WAITOK | M_ZERO);
206 /* Get our Processor object. */
208 buf.Length = ACPI_ALLOCATE_BUFFER;
209 status = AcpiEvaluateObject(handle, NULL, NULL, &buf);
210 if (ACPI_FAILURE(status)) {
211 device_printf(dev, "probe failed to get Processor obj - %s\n",
212 AcpiFormatException(status));
215 obj = (ACPI_OBJECT *)buf.Pointer;
216 if (obj->Type != ACPI_TYPE_PROCESSOR) {
217 device_printf(dev, "Processor object has bad type %d\n", obj->Type);
223 * Find the processor associated with our unit. We could use the
224 * ProcId as a key, however, some boxes do not have the same values
225 * in their Processor object as the ProcId values in the MADT.
227 acpi_id = obj->Processor.ProcId;
229 if (acpi_pcpu_get_id(device_get_unit(dev), &acpi_id, &cpu_id) != 0)
233 * Check if we already probed this processor. We scan the bus twice
234 * so it's possible we've already seen this one.
236 if (cpu_softc[cpu_id] != NULL)
239 /* Mark this processor as in-use and save our derived id for attach. */
240 cpu_softc[cpu_id] = (void *)1;
241 acpi_set_magic(dev, cpu_id);
242 device_set_desc(dev, "ACPI CPU");
248 acpi_cpu_attach(device_t dev)
251 ACPI_OBJECT arg, *obj;
252 ACPI_OBJECT_LIST arglist;
253 struct pcpu *pcpu_data;
254 struct acpi_cpu_softc *sc;
255 struct acpi_softc *acpi_sc;
258 int cpu_id, drv_count, i;
262 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
264 sc = device_get_softc(dev);
266 sc->cpu_handle = acpi_get_handle(dev);
267 cpu_id = acpi_get_magic(dev);
268 cpu_softc[cpu_id] = sc;
269 pcpu_data = pcpu_find(cpu_id);
270 pcpu_data->pc_device = dev;
271 sc->cpu_pcpu = pcpu_data;
272 cpu_smi_cmd = AcpiGbl_FADT->SmiCmd;
273 cpu_cst_cnt = AcpiGbl_FADT->CstCnt;
276 buf.Length = ACPI_ALLOCATE_BUFFER;
277 status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf);
278 if (ACPI_FAILURE(status)) {
279 device_printf(dev, "attach failed to get Processor obj - %s\n",
280 AcpiFormatException(status));
283 obj = (ACPI_OBJECT *)buf.Pointer;
284 sc->cpu_p_blk = obj->Processor.PblkAddress;
285 sc->cpu_p_blk_len = obj->Processor.PblkLength;
286 sc->cpu_acpi_id = obj->Processor.ProcId;
288 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_BLK at %#x/%d\n",
289 device_get_unit(dev), sc->cpu_p_blk, sc->cpu_p_blk_len));
291 acpi_sc = acpi_device_get_parent_softc(dev);
292 sysctl_ctx_init(&acpi_cpu_sysctl_ctx);
293 acpi_cpu_sysctl_tree = SYSCTL_ADD_NODE(&acpi_cpu_sysctl_ctx,
294 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, "cpu",
298 * Before calling any CPU methods, collect child driver feature hints
299 * and notify ACPI of them. We support unified SMP power control
300 * so advertise this ourselves. Note this is not the same as independent
301 * SMP control where each CPU can have different settings.
303 sc->cpu_features = ACPI_CAP_SMP_SAME | ACPI_CAP_SMP_SAME_C3;
304 if (devclass_get_drivers(acpi_cpu_devclass, &drivers, &drv_count) == 0) {
305 for (i = 0; i < drv_count; i++) {
306 if (ACPI_GET_FEATURES(drivers[i], &features) == 0)
307 sc->cpu_features |= features;
309 free(drivers, M_TEMP);
313 * CPU capabilities are specified as a buffer of 32-bit integers:
314 * revision, count, and one or more capabilities. The revision of
315 * "1" is not specified anywhere but seems to match Linux. We should
316 * also support _OSC here.
318 if (sc->cpu_features) {
319 arglist.Pointer = &arg;
321 arg.Type = ACPI_TYPE_BUFFER;
322 arg.Buffer.Length = sizeof(cap_set);
323 arg.Buffer.Pointer = (uint8_t *)cap_set;
324 cap_set[0] = 1; /* revision */
325 cap_set[1] = 1; /* number of capabilities integers */
326 cap_set[2] = sc->cpu_features;
327 AcpiEvaluateObject(sc->cpu_handle, "_PDC", &arglist, NULL);
331 * Probe for Cx state support. If it isn't present, free up unused
334 if (acpi_cpu_cx_probe(sc) == 0) {
335 status = AcpiInstallNotifyHandler(sc->cpu_handle, ACPI_DEVICE_NOTIFY,
336 acpi_cpu_notify, sc);
337 if (device_get_unit(dev) == 0)
338 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cpu_startup, NULL);
340 sysctl_ctx_free(&acpi_cpu_sysctl_ctx);
342 /* Finally, call identify and probe/attach for child devices. */
343 bus_generic_probe(dev);
344 bus_generic_attach(dev);
350 * Find the nth present CPU and return its pc_cpuid as well as set the
351 * pc_acpi_id from the most reliable source.
354 acpi_pcpu_get_id(uint32_t idx, uint32_t *acpi_id, uint32_t *cpu_id)
356 struct pcpu *pcpu_data;
359 KASSERT(acpi_id != NULL, ("Null acpi_id"));
360 KASSERT(cpu_id != NULL, ("Null cpu_id"));
361 for (i = 0; i <= mp_maxid; i++) {
364 pcpu_data = pcpu_find(i);
365 KASSERT(pcpu_data != NULL, ("no pcpu data for %d", i));
368 * If pc_acpi_id was not initialized (e.g., a non-APIC UP box)
369 * override it with the value from the ASL. Otherwise, if the
370 * two don't match, prefer the MADT-derived value. Finally,
371 * return the pc_cpuid to reference this processor.
373 if (pcpu_data->pc_acpi_id == 0xffffffff)
374 pcpu_data->pc_acpi_id = *acpi_id;
375 else if (pcpu_data->pc_acpi_id != *acpi_id)
376 *acpi_id = pcpu_data->pc_acpi_id;
377 *cpu_id = pcpu_data->pc_cpuid;
385 static struct resource_list *
386 acpi_cpu_get_rlist(device_t dev, device_t child)
388 struct acpi_cpu_device *ad;
390 ad = device_get_ivars(child);
397 acpi_cpu_add_child(device_t dev, int order, const char *name, int unit)
399 struct acpi_cpu_device *ad;
402 if ((ad = malloc(sizeof(*ad), M_TEMP, M_NOWAIT | M_ZERO)) == NULL)
405 resource_list_init(&ad->ad_rl);
407 child = device_add_child_ordered(dev, order, name, unit);
409 device_set_ivars(child, ad);
416 acpi_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
418 struct acpi_cpu_softc *sc;
420 sc = device_get_softc(dev);
422 case ACPI_IVAR_HANDLE:
423 *result = (uintptr_t)sc->cpu_handle;
426 *result = (uintptr_t)sc->cpu_pcpu;
435 acpi_cpu_shutdown(device_t dev)
437 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
439 /* Allow children to shutdown first. */
440 bus_generic_shutdown(dev);
442 /* Disable any entry to the idle function. */
445 /* Signal and wait for all processors to exit acpi_cpu_idle(). */
446 smp_rendezvous(NULL, NULL, NULL, NULL);
452 acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
454 ACPI_GENERIC_ADDRESS gas;
455 struct acpi_cx *cx_ptr;
458 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
461 * Bus mastering arbitration control is needed to keep caches coherent
462 * while sleeping in C3. If it's not present but a working flush cache
463 * instruction is present, flush the caches before entering C3 instead.
464 * Otherwise, just disable C3 completely.
466 if (AcpiGbl_FADT->V1_Pm2CntBlk == 0 || AcpiGbl_FADT->Pm2CntLen == 0) {
467 if (AcpiGbl_FADT->WbInvd && AcpiGbl_FADT->WbInvdFlush == 0) {
468 cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
469 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
470 "acpi_cpu%d: no BM control, using flush cache method\n",
471 device_get_unit(sc->cpu_dev)));
473 cpu_quirks |= CPU_QUIRK_NO_C3;
474 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
475 "acpi_cpu%d: no BM control, C3 not available\n",
476 device_get_unit(sc->cpu_dev)));
481 * First, check for the ACPI 2.0 _CST sleep states object.
482 * If not usable, fall back to the P_BLK's P_LVL2 and P_LVL3.
484 sc->cpu_cx_count = 0;
485 error = acpi_cpu_cx_cst(sc);
487 cx_ptr = sc->cpu_cx_states;
489 /* C1 has been required since just after ACPI 1.0 */
490 cx_ptr->type = ACPI_STATE_C1;
491 cx_ptr->trans_lat = 0;
497 * The spec says P_BLK must be 6 bytes long. However, some systems
498 * use it to indicate a fractional set of features present so we
499 * take 5 as C2. Some may also have a value of 7 to indicate
500 * another C3 but most use _CST for this (as required) and having
501 * "only" C1-C3 is not a hardship.
503 if (sc->cpu_p_blk_len < 5)
506 /* Validate and allocate resources for C2 (P_LVL2). */
507 gas.AddressSpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
508 gas.RegisterBitWidth = 8;
509 if (AcpiGbl_FADT->Plvl2Lat <= 100) {
510 gas.Address = sc->cpu_p_blk + 4;
511 acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &cpu_rid, &gas,
513 if (cx_ptr->p_lvlx != NULL) {
515 cx_ptr->type = ACPI_STATE_C2;
516 cx_ptr->trans_lat = AcpiGbl_FADT->Plvl2Lat;
522 if (sc->cpu_p_blk_len < 6)
525 /* Validate and allocate resources for C3 (P_LVL3). */
526 if (AcpiGbl_FADT->Plvl3Lat <= 1000 &&
527 (cpu_quirks & CPU_QUIRK_NO_C3) == 0) {
528 gas.Address = sc->cpu_p_blk + 5;
529 acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &cpu_rid, &gas,
531 if (cx_ptr->p_lvlx != NULL) {
533 cx_ptr->type = ACPI_STATE_C3;
534 cx_ptr->trans_lat = AcpiGbl_FADT->Plvl3Lat;
542 /* If no valid registers were found, don't attach. */
543 if (sc->cpu_cx_count == 0)
546 /* Use initial sleep value of 1 sec. to start with lowest idle state. */
547 sc->cpu_prev_sleep = 1000000;
553 * Parse a _CST package and set up its Cx states. Since the _CST object
554 * can change dynamically, our notify handler may call this function
555 * to clean up and probe the new _CST package.
558 acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
560 struct acpi_cx *cx_ptr;
568 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
571 buf.Length = ACPI_ALLOCATE_BUFFER;
572 status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
573 if (ACPI_FAILURE(status))
576 /* _CST is a package with a count and at least one Cx package. */
577 top = (ACPI_OBJECT *)buf.Pointer;
578 if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
579 device_printf(sc->cpu_dev, "Invalid _CST package\n");
580 AcpiOsFree(buf.Pointer);
583 if (count != top->Package.Count - 1) {
584 device_printf(sc->cpu_dev, "Invalid _CST state count (%d != %d)\n",
585 count, top->Package.Count - 1);
586 count = top->Package.Count - 1;
588 if (count > MAX_CX_STATES) {
589 device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
590 count = MAX_CX_STATES;
593 /* Set up all valid states. */
594 sc->cpu_cx_count = 0;
595 cx_ptr = sc->cpu_cx_states;
596 for (i = 0; i < count; i++) {
597 pkg = &top->Package.Elements[i + 1];
598 if (!ACPI_PKG_VALID(pkg, 4) ||
599 acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
600 acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
601 acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
603 device_printf(sc->cpu_dev, "skipping invalid Cx state package\n");
607 /* Validate the state to see if we should use it. */
608 switch (cx_ptr->type) {
615 if (cx_ptr->trans_lat > 100) {
616 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
617 "acpi_cpu%d: C2[%d] not available.\n",
618 device_get_unit(sc->cpu_dev), i));
625 if (cx_ptr->trans_lat > 1000 ||
626 (cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
628 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
629 "acpi_cpu%d: C3[%d] not available.\n",
630 device_get_unit(sc->cpu_dev), i));
637 /* Free up any previous register. */
638 if (cx_ptr->p_lvlx != NULL) {
639 bus_release_resource(sc->cpu_dev, 0, 0, cx_ptr->p_lvlx);
640 cx_ptr->p_lvlx = NULL;
644 /* Allocate the control register for C2 or C3. */
645 acpi_PkgGas(sc->cpu_dev, pkg, 0, &cx_ptr->res_type, &cpu_rid,
647 if (cx_ptr->p_lvlx) {
649 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
650 "acpi_cpu%d: Got C%d - %d latency\n",
651 device_get_unit(sc->cpu_dev), cx_ptr->type,
657 AcpiOsFree(buf.Pointer);
663 * Call this *after* all CPUs have been attached.
666 acpi_cpu_startup(void *arg)
668 struct acpi_cpu_softc *sc;
671 /* Get set of CPU devices */
672 devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
674 /* Check for quirks via the first CPU device. */
675 sc = device_get_softc(cpu_devices[0]);
679 * Make sure all the processors' Cx counts match. We should probably
680 * also check the contents of each. However, no known systems have
681 * non-matching Cx counts so we'll deal with this later.
683 count = MAX_CX_STATES;
684 for (i = 0; i < cpu_ndevices; i++) {
685 sc = device_get_softc(cpu_devices[i]);
686 count = min(sc->cpu_cx_count, count);
688 cpu_cx_count = count;
690 /* Perform Cx final initialization. */
691 sc = device_get_softc(cpu_devices[0]);
692 if (cpu_cx_count > 0)
693 acpi_cpu_startup_cx();
697 acpi_cpu_startup_cx()
699 struct acpi_cpu_softc *sc;
704 * Set up the list of Cx states, eliminating C3 states by truncating
705 * cpu_cx_count if quirks indicate C3 is not usable.
707 sc = device_get_softc(cpu_devices[0]);
708 sbuf_new(&sb, cpu_cx_supported, sizeof(cpu_cx_supported), SBUF_FIXEDLEN);
709 for (i = 0; i < cpu_cx_count; i++) {
710 if ((cpu_quirks & CPU_QUIRK_NO_C3) == 0 ||
711 sc->cpu_cx_states[i].type != ACPI_STATE_C3)
712 sbuf_printf(&sb, "C%d/%d ", i + 1, sc->cpu_cx_states[i].trans_lat);
718 SYSCTL_ADD_STRING(&acpi_cpu_sysctl_ctx,
719 SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
720 OID_AUTO, "cx_supported", CTLFLAG_RD, cpu_cx_supported,
721 0, "Cx/microsecond values for supported Cx states");
722 SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx,
723 SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
724 OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
725 NULL, 0, acpi_cpu_cx_lowest_sysctl, "A",
726 "lowest Cx sleep state to use");
727 SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx,
728 SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
729 OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
730 NULL, 0, acpi_cpu_usage_sysctl, "A",
731 "percent usage for each Cx state");
734 /* Signal platform that we can handle _CST notification. */
735 if (cpu_cst_cnt != 0) {
737 AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
742 /* Take over idling from cpu_idle_default(). */
743 cpu_idle_hook = acpi_cpu_idle;
747 * Idle the CPU in the lowest state possible. This function is called with
748 * interrupts disabled. Note that once it re-enables interrupts, a task
749 * switch can occur so do not access shared data (i.e. the softc) after
750 * interrupts are re-enabled.
755 struct acpi_cpu_softc *sc;
756 struct acpi_cx *cx_next;
757 uint32_t start_time, end_time;
758 int bm_active, cx_next_idx, i;
760 /* If disabled, return immediately. */
761 if (cpu_cx_count == 0) {
767 * Look up our CPU id to get our softc. If it's NULL, we'll use C1
768 * since there is no ACPI processor object for this CPU. This occurs
769 * for logical CPUs in the HTT case.
771 sc = cpu_softc[PCPU_GET(cpuid)];
778 * If we slept 100 us or more, use the lowest Cx state. Otherwise,
779 * find the lowest state that has a latency less than or equal to
780 * the length of our last sleep.
782 cx_next_idx = cpu_cx_lowest;
783 if (sc->cpu_prev_sleep < 100) {
785 * If we sleep too short all the time, this system may not implement
786 * C2/3 correctly (i.e. reads return immediately). In this case,
787 * back off and use the next higher level.
789 if (sc->cpu_prev_sleep <= 1) {
791 if (cpu_short_slp == 1000 && cpu_cx_lowest != 0) {
792 if (cpu_non_c3 == cpu_cx_lowest && cpu_non_c3 != 0)
796 device_printf(sc->cpu_dev,
797 "too many short sleeps, backing off to C%d\n",
803 for (i = cpu_cx_lowest; i >= 0; i--)
804 if (sc->cpu_cx_states[i].trans_lat <= sc->cpu_prev_sleep) {
811 * Check for bus master activity. If there was activity, clear
812 * the bit and use the lowest non-C3 state. Note that the USB
813 * driver polling for new devices keeps this bit set all the
814 * time if USB is loaded.
816 if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
817 AcpiGetRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active,
818 ACPI_MTX_DO_NOT_LOCK);
819 if (bm_active != 0) {
820 AcpiSetRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1,
821 ACPI_MTX_DO_NOT_LOCK);
822 cx_next_idx = min(cx_next_idx, cpu_non_c3);
826 /* Select the next state and update statistics. */
827 cx_next = &sc->cpu_cx_states[cx_next_idx];
828 cpu_cx_stats[cx_next_idx]++;
829 KASSERT(cx_next->type != ACPI_STATE_C0, ("acpi_cpu_idle: C0 sleep"));
832 * Execute HLT (or equivalent) and wait for an interrupt. We can't
833 * calculate the time spent in C1 since the place we wake up is an
834 * ISR. Assume we slept one quantum and return.
836 if (cx_next->type == ACPI_STATE_C1) {
837 sc->cpu_prev_sleep = 1000000 / hz;
843 * For C3, disable bus master arbitration and enable bus master wake
844 * if BM control is available, otherwise flush the CPU cache.
846 if (cx_next->type == ACPI_STATE_C3) {
847 if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
848 AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 1, ACPI_MTX_DO_NOT_LOCK);
849 AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 1,
850 ACPI_MTX_DO_NOT_LOCK);
852 ACPI_FLUSH_CPU_CACHE();
856 * Read from P_LVLx to enter C2(+), checking time spent asleep.
857 * Use the ACPI timer for measuring sleep time. Since we need to
858 * get the time very close to the CPU start/stop clock logic, this
859 * is the only reliable time source.
861 AcpiHwLowLevelRead(32, &start_time, &AcpiGbl_FADT->XPmTmrBlk);
862 CPU_GET_REG(cx_next->p_lvlx, 1);
865 * Read the end time twice. Since it may take an arbitrary time
866 * to enter the idle state, the first read may be executed before
867 * the processor has stopped. Doing it again provides enough
868 * margin that we are certain to have a correct value.
870 AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT->XPmTmrBlk);
871 AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT->XPmTmrBlk);
873 /* Enable bus master arbitration and disable bus master wakeup. */
874 if (cx_next->type == ACPI_STATE_C3 &&
875 (cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
876 AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 0, ACPI_MTX_DO_NOT_LOCK);
877 AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 0, ACPI_MTX_DO_NOT_LOCK);
881 /* Find the actual time asleep in microseconds, minus overhead. */
882 end_time = acpi_TimerDelta(end_time, start_time);
883 sc->cpu_prev_sleep = PM_USEC(end_time) - cx_next->trans_lat;
887 * Re-evaluate the _CST object when we are notified that it changed.
889 * XXX Re-evaluation disabled until locking is done.
892 acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context)
894 struct acpi_cpu_softc *sc = (struct acpi_cpu_softc *)context;
896 if (notify != ACPI_NOTIFY_CX_STATES)
899 device_printf(sc->cpu_dev, "Cx states changed\n");
900 /* acpi_cpu_cx_cst(sc); */
904 acpi_cpu_quirks(struct acpi_cpu_softc *sc)
909 * C3 on multiple CPUs requires using the expensive flush cache
913 cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
915 /* Look for various quirks of the PIIX4 part. */
916 acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
917 if (acpi_dev != NULL) {
918 switch (pci_get_revid(acpi_dev)) {
920 * Disable C3 support for all PIIX4 chipsets. Some of these parts
921 * do not report the BMIDE status to the BM status register and
922 * others have a livelock bug if Type-F DMA is enabled. Linux
923 * works around the BMIDE bug by reading the BM status directly
924 * but we take the simpler approach of disabling C3 for these
927 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
928 * Livelock") from the January 2002 PIIX4 specification update.
929 * Applies to all PIIX4 models.
931 case PCI_REVISION_4E:
932 case PCI_REVISION_4M:
933 cpu_quirks |= CPU_QUIRK_NO_C3;
944 acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS)
949 uintmax_t fract, sum, whole;
952 for (i = 0; i < cpu_cx_count; i++)
953 sum += cpu_cx_stats[i];
954 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
955 for (i = 0; i < cpu_cx_count; i++) {
957 whole = (uintmax_t)cpu_cx_stats[i] * 100;
958 fract = (whole % sum) * 100;
959 sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
960 (u_int)(fract / sum));
962 sbuf_printf(&sb, "0%% ");
966 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
973 acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
975 struct acpi_cpu_softc *sc;
979 sc = device_get_softc(cpu_devices[0]);
980 snprintf(state, sizeof(state), "C%d", cpu_cx_lowest + 1);
981 error = sysctl_handle_string(oidp, state, sizeof(state), req);
982 if (error != 0 || req->newptr == NULL)
984 if (strlen(state) < 2 || toupper(state[0]) != 'C')
986 val = (int) strtol(state + 1, NULL, 10) - 1;
987 if (val < 0 || val > cpu_cx_count - 1)
990 ACPI_SERIAL_BEGIN(cpu);
993 /* If not disabling, cache the new lowest non-C3 state. */
995 for (i = cpu_cx_lowest; i >= 0; i--) {
996 if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
1002 /* Reset the statistics counters. */
1003 bzero(cpu_cx_stats, sizeof(cpu_cx_stats));
1004 ACPI_SERIAL_END(cpu);