#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpuhelper.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/globaldata.h>
#include <sys/power.h>
#include <sys/proc.h>
#include <sys/sbuf.h>
#include <sys/serialize.h>
#include <sys/msgport2.h>
#include <sys/microtime_pcpu.h>
#include <sys/cpu_topology.h>
#include <bus/pci/pcivar.h>
#include <machine/atomic.h>
#include <machine/globaldata.h>
#include <machine/md_var.h>
#include <machine/smp.h>
#include <sys/rman.h>
#include "acpi.h"
#include "acpivar.h"
#include "acpi_cpu.h"
#include "acpi_cpu_cstate.h"
#define _COMPONENT ACPI_PROCESSOR
ACPI_MODULE_NAME("PROCESSOR")
#define MAX_CX_STATES 8
struct acpi_cst_softc {
device_t cst_dev;
struct acpi_cpu_softc *cst_parent;
ACPI_HANDLE cst_handle;
int cst_cpuid;
uint32_t cst_flags;
uint32_t cst_p_blk;
uint32_t cst_p_blk_len;
struct acpi_cst_cx cst_cx_states[MAX_CX_STATES];
int cst_cx_count;
int cst_prev_sleep;
int cst_non_c3;
u_long cst_cx_stats[MAX_CX_STATES];
int cst_cx_lowest;
int cst_cx_lowest_req;
char cst_cx_supported[64];
};
#define ACPI_CST_FLAG_PROBING 0x1
#define ACPI_CST_FLAG_ATTACHED 0x2
#define ACPI_CST_FLAG_MATCH_HT 0x4
#define PCI_VENDOR_INTEL 0x8086
#define PCI_DEVICE_82371AB_3 0x7113
#define PCI_REVISION_A_STEP 0
#define PCI_REVISION_B_STEP 1
#define PCI_REVISION_4E 2
#define PCI_REVISION_4M 3
#define PIIX4_DEVACTB_REG 0x58
#define PIIX4_BRLD_EN_IRQ0 (1<<0)
#define PIIX4_BRLD_EN_IRQ (1<<1)
#define PIIX4_BRLD_EN_IRQ8 (1<<5)
#define PIIX4_STOP_BREAK_MASK (PIIX4_BRLD_EN_IRQ0 | \
PIIX4_BRLD_EN_IRQ | \
PIIX4_BRLD_EN_IRQ8)
#define PIIX4_PCNTRL_BST_EN (1<<10)
static uint32_t acpi_cst_smi_cmd;
static uint8_t acpi_cst_ctrl;
int acpi_cst_quirks;
static boolean_t acpi_cst_use_fadt;
static boolean_t acpi_cst_disable_idle;
static int acpi_cst_cx_count;
static int acpi_cst_cx_lowest;
static int acpi_cst_cx_lowest_req;
static device_t *acpi_cst_devices;
static int acpi_cst_ndevices;
static struct acpi_cst_softc **acpi_cst_softc;
static struct lwkt_serialize acpi_cst_slize = LWKT_SERIALIZE_INITIALIZER;
static int acpi_cst_probe(device_t);
static int acpi_cst_attach(device_t);
static int acpi_cst_suspend(device_t);
static int acpi_cst_resume(device_t);
static int acpi_cst_shutdown(device_t);
static void acpi_cst_notify(device_t);
static void acpi_cst_postattach(void *);
static void acpi_cst_idle(void);
static void acpi_cst_copy(struct acpi_cst_softc *,
const struct acpi_cst_softc *);
static void acpi_cst_cx_probe(struct acpi_cst_softc *);
static void acpi_cst_cx_probe_fadt(struct acpi_cst_softc *);
static int acpi_cst_cx_probe_cst(struct acpi_cst_softc *, int);
static int acpi_cst_cx_reprobe_cst(struct acpi_cst_softc *);
static void acpi_cst_startup(struct acpi_cst_softc *);
static void acpi_cst_support_list(struct acpi_cst_softc *);
static int acpi_cst_set_lowest(struct acpi_cst_softc *, int);
static int acpi_cst_set_lowest_oncpu(struct acpi_cst_softc *, int);
static void acpi_cst_non_c3(struct acpi_cst_softc *);
static void acpi_cst_global_cx_count(void);
static int acpi_cst_set_quirks(void);
static void acpi_cst_c3_bm_rld(struct acpi_cst_softc *);
static void acpi_cst_free_resource(struct acpi_cst_softc *, int);
static void acpi_cst_c1_halt(void);
static int acpi_cst_usage_sysctl(SYSCTL_HANDLER_ARGS);
static int acpi_cst_lowest_sysctl(SYSCTL_HANDLER_ARGS);
static int acpi_cst_lowest_use_sysctl(SYSCTL_HANDLER_ARGS);
static int acpi_cst_global_lowest_sysctl(SYSCTL_HANDLER_ARGS);
static int acpi_cst_global_lowest_use_sysctl(SYSCTL_HANDLER_ARGS);
static int acpi_cst_cx_setup(struct acpi_cst_cx *cx);
static void acpi_cst_c1_halt_enter(const struct acpi_cst_cx *);
static void acpi_cst_cx_io_enter(const struct acpi_cst_cx *);
int acpi_cst_force_bmarb;
TUNABLE_INT("hw.acpi.cpu.cst.force_bmarb", &acpi_cst_force_bmarb);
int acpi_cst_force_bmsts;
TUNABLE_INT("hw.acpi.cpu.cst.force_bmsts", &acpi_cst_force_bmsts);
static device_method_t acpi_cst_methods[] = {
DEVMETHOD(device_probe, acpi_cst_probe),
DEVMETHOD(device_attach, acpi_cst_attach),
DEVMETHOD(device_detach, bus_generic_detach),
DEVMETHOD(device_shutdown, acpi_cst_shutdown),
DEVMETHOD(device_suspend, acpi_cst_suspend),
DEVMETHOD(device_resume, acpi_cst_resume),
DEVMETHOD(bus_add_child, bus_generic_add_child),
DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
DEVMETHOD(bus_get_resource_list, bus_generic_get_resource_list),
DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
DEVMETHOD(bus_driver_added, bus_generic_driver_added),
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
DEVMETHOD_END
};
static driver_t acpi_cst_driver = {
"cpu_cst",
acpi_cst_methods,
sizeof(struct acpi_cst_softc),
.gpri = KOBJ_GPRI_ACPI+2
};
static devclass_t acpi_cst_devclass;
DRIVER_MODULE(cpu_cst, cpu, acpi_cst_driver, acpi_cst_devclass, NULL, NULL);
MODULE_DEPEND(cpu_cst, acpi, 1, 1, 1);
static int
acpi_cst_probe(device_t dev)
{
int cpu_id;
if (acpi_disabled("cpu_cst") || acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
return (ENXIO);
cpu_id = acpi_get_magic(dev);
if (acpi_cst_softc == NULL)
acpi_cst_softc = kmalloc(sizeof(struct acpi_cst_softc *) *
SMP_MAXCPU, M_TEMP , M_INTWAIT | M_ZERO);
if (acpi_cst_softc[cpu_id] != NULL) {
device_printf(dev, "CPU%d cstate already exist\n", cpu_id);
return (ENXIO);
}
acpi_cst_softc[cpu_id] = device_get_softc(dev);
device_set_desc(dev, "ACPI CPU C-State");
return (0);
}
static int
acpi_cst_attach(device_t dev)
{
ACPI_BUFFER buf;
ACPI_OBJECT *obj;
struct acpi_cst_softc *sc;
ACPI_STATUS status;
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
sc = device_get_softc(dev);
sc->cst_dev = dev;
sc->cst_parent = device_get_softc(device_get_parent(dev));
sc->cst_handle = acpi_get_handle(dev);
sc->cst_cpuid = acpi_get_magic(dev);
acpi_cst_softc[sc->cst_cpuid] = sc;
acpi_cst_smi_cmd = AcpiGbl_FADT.SmiCommand;
acpi_cst_ctrl = AcpiGbl_FADT.CstControl;
buf.Pointer = NULL;
buf.Length = ACPI_ALLOCATE_BUFFER;
status = AcpiEvaluateObject(sc->cst_handle, NULL, NULL, &buf);
if (ACPI_FAILURE(status)) {
device_printf(dev, "attach failed to get Processor obj - %s\n",
AcpiFormatException(status));
acpi_cst_softc[sc->cst_cpuid] = NULL;
return (ENXIO);
}
obj = (ACPI_OBJECT *)buf.Pointer;
sc->cst_p_blk = obj->Processor.PblkAddress;
sc->cst_p_blk_len = obj->Processor.PblkLength;
AcpiOsFree(obj);
ACPI_DEBUG_PRINT((ACPI_DB_INFO, "cpu_cst%d: P_BLK at %#x/%d\n",
device_get_unit(dev), sc->cst_p_blk, sc->cst_p_blk_len));
if (device_get_unit(dev) == 0) {
acpi_cst_use_fadt = FALSE;
AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cst_postattach, NULL);
}
acpi_cst_cx_probe(sc);
sc->cst_flags |= ACPI_CST_FLAG_ATTACHED;
return (0);
}
static int
acpi_cst_suspend(device_t dev)
{
int error;
error = bus_generic_suspend(dev);
if (error)
return (error);
acpi_cst_disable_idle = TRUE;
return (0);
}
static int
acpi_cst_resume(device_t dev)
{
acpi_cst_disable_idle = FALSE;
return (bus_generic_resume(dev));
}
static int
acpi_cst_shutdown(device_t dev)
{
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
bus_generic_shutdown(dev);
acpi_cst_disable_idle = TRUE;
return_VALUE (0);
}
static void
acpi_cst_cx_probe(struct acpi_cst_softc *sc)
{
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
sc->cst_prev_sleep = 1000000;
sc->cst_cx_lowest = 0;
sc->cst_cx_lowest_req = 0;
if (!acpi_cst_use_fadt && acpi_cst_cx_probe_cst(sc, 0) != 0) {
acpi_cst_use_fadt = TRUE;
if (bootverbose)
device_printf(sc->cst_dev, "switching to FADT Cx mode\n");
}
}
static void
acpi_cst_cx_probe_fadt(struct acpi_cst_softc *sc)
{
struct acpi_cst_cx *cx_ptr;
int error;
acpi_cst_free_resource(sc, 0);
sc->cst_cx_count = 0;
cx_ptr = sc->cst_cx_states;
sc->cst_prev_sleep = 1000000;
cx_ptr->gas.SpaceId = ACPI_ADR_SPACE_FIXED_HARDWARE;
cx_ptr->type = ACPI_STATE_C1;
cx_ptr->trans_lat = 0;
cx_ptr->enter = acpi_cst_c1_halt_enter;
error = acpi_cst_cx_setup(cx_ptr);
if (error)
panic("C1 FADT HALT setup failed: %d", error);
cx_ptr++;
sc->cst_cx_count++;
if (ncpus > 1 && (AcpiGbl_FADT.Flags & ACPI_FADT_C2_MP_SUPPORTED) == 0)
return;
if (sc->cst_p_blk_len < 5)
return;
if (AcpiGbl_FADT.C2Latency <= 100) {
cx_ptr->gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
cx_ptr->gas.BitWidth = 8;
cx_ptr->gas.Address = sc->cst_p_blk + 4;
cx_ptr->rid = sc->cst_parent->cpu_next_rid;
acpi_bus_alloc_gas(sc->cst_dev, &cx_ptr->res_type, &cx_ptr->rid,
&cx_ptr->gas, &cx_ptr->res, RF_SHAREABLE);
if (cx_ptr->res != NULL) {
sc->cst_parent->cpu_next_rid++;
cx_ptr->type = ACPI_STATE_C2;
cx_ptr->trans_lat = AcpiGbl_FADT.C2Latency;
cx_ptr->enter = acpi_cst_cx_io_enter;
cx_ptr->btag = rman_get_bustag(cx_ptr->res);
cx_ptr->bhand = rman_get_bushandle(cx_ptr->res);
error = acpi_cst_cx_setup(cx_ptr);
if (error)
panic("C2 FADT I/O setup failed: %d", error);
cx_ptr++;
sc->cst_cx_count++;
sc->cst_non_c3 = 1;
}
}
if (sc->cst_p_blk_len < 6)
return;
if (AcpiGbl_FADT.C3Latency <= 1000 &&
!(acpi_cst_quirks & ACPI_CST_QUIRK_NO_C3)) {
cx_ptr->gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
cx_ptr->gas.BitWidth = 8;
cx_ptr->gas.Address = sc->cst_p_blk + 5;
cx_ptr->rid = sc->cst_parent->cpu_next_rid;
acpi_bus_alloc_gas(sc->cst_dev, &cx_ptr->res_type, &cx_ptr->rid,
&cx_ptr->gas, &cx_ptr->res, RF_SHAREABLE);
if (cx_ptr->res != NULL) {
sc->cst_parent->cpu_next_rid++;
cx_ptr->type = ACPI_STATE_C3;
cx_ptr->trans_lat = AcpiGbl_FADT.C3Latency;
cx_ptr->enter = acpi_cst_cx_io_enter;
cx_ptr->btag = rman_get_bustag(cx_ptr->res);
cx_ptr->bhand = rman_get_bushandle(cx_ptr->res);
error = acpi_cst_cx_setup(cx_ptr);
if (error)
panic("C3 FADT I/O setup failed: %d", error);
cx_ptr++;
sc->cst_cx_count++;
}
}
}
static void
acpi_cst_copy(struct acpi_cst_softc *dst_sc,
const struct acpi_cst_softc *src_sc)
{
dst_sc->cst_non_c3 = src_sc->cst_non_c3;
dst_sc->cst_cx_count = src_sc->cst_cx_count;
memcpy(dst_sc->cst_cx_states, src_sc->cst_cx_states,
sizeof(dst_sc->cst_cx_states));
}
static int
acpi_cst_cx_probe_cst(struct acpi_cst_softc *sc, int reprobe)
{
struct acpi_cst_cx *cx_ptr;
ACPI_STATUS status;
ACPI_BUFFER buf;
ACPI_OBJECT *top;
ACPI_OBJECT *pkg;
uint32_t count;
int i;
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
if (reprobe)
cpuhelper_assert(sc->cst_cpuid, true);
buf.Pointer = NULL;
buf.Length = ACPI_ALLOCATE_BUFFER;
status = AcpiEvaluateObject(sc->cst_handle, "_CST", NULL, &buf);
if (ACPI_FAILURE(status))
return (ENXIO);
top = (ACPI_OBJECT *)buf.Pointer;
if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
device_printf(sc->cst_dev, "invalid _CST package\n");
AcpiOsFree(buf.Pointer);
return (ENXIO);
}
if (count != top->Package.Count - 1) {
device_printf(sc->cst_dev, "invalid _CST state count (%d != %d)\n",
count, top->Package.Count - 1);
count = top->Package.Count - 1;
}
if (count > MAX_CX_STATES) {
device_printf(sc->cst_dev, "_CST has too many states (%d)\n", count);
count = MAX_CX_STATES;
}
sc->cst_flags |= ACPI_CST_FLAG_PROBING | ACPI_CST_FLAG_MATCH_HT;
cpu_sfence();
acpi_cst_free_resource(sc, 0);
sc->cst_cx_count = 0;
cx_ptr = sc->cst_cx_states;
for (i = 0; i < count; i++) {
int error;
pkg = &top->Package.Elements[i + 1];
if (!ACPI_PKG_VALID(pkg, 4) ||
acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
device_printf(sc->cst_dev, "skipping invalid Cx state package\n");
continue;
}
switch (cx_ptr->type) {
case ACPI_STATE_C1:
sc->cst_non_c3 = i;
cx_ptr->enter = acpi_cst_c1_halt_enter;
error = acpi_cst_cx_setup(cx_ptr);
if (error)
panic("C1 CST HALT setup failed: %d", error);
if (sc->cst_cx_count != 0) {
sc->cst_flags &= ~ACPI_CST_FLAG_MATCH_HT;
}
cx_ptr++;
sc->cst_cx_count++;
continue;
case ACPI_STATE_C2:
sc->cst_non_c3 = i;
break;
case ACPI_STATE_C3:
default:
if ((acpi_cst_quirks & ACPI_CST_QUIRK_NO_C3) != 0) {
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"cpu_cst%d: C3[%d] not available.\n",
device_get_unit(sc->cst_dev), i));
continue;
}
break;
}
KASSERT(cx_ptr->res == NULL, ("still has res"));
acpi_PkgRawGas(pkg, 0, &cx_ptr->gas);
if (cx_ptr->gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE)
sc->cst_flags &= ~ACPI_CST_FLAG_MATCH_HT;
cx_ptr->rid = sc->cst_parent->cpu_next_rid;
acpi_bus_alloc_gas(sc->cst_dev, &cx_ptr->res_type, &cx_ptr->rid,
&cx_ptr->gas, &cx_ptr->res, RF_SHAREABLE);
if (cx_ptr->res != NULL) {
sc->cst_parent->cpu_next_rid++;
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"cpu_cst%d: Got C%d - %d latency\n",
device_get_unit(sc->cst_dev), cx_ptr->type,
cx_ptr->trans_lat));
cx_ptr->enter = acpi_cst_cx_io_enter;
cx_ptr->btag = rman_get_bustag(cx_ptr->res);
cx_ptr->bhand = rman_get_bushandle(cx_ptr->res);
error = acpi_cst_cx_setup(cx_ptr);
if (error)
panic("C%d CST I/O setup failed: %d", cx_ptr->type, error);
cx_ptr++;
sc->cst_cx_count++;
} else {
error = acpi_cst_cx_setup(cx_ptr);
if (!error) {
KASSERT(cx_ptr->enter != NULL,
("C%d enter is not set", cx_ptr->type));
cx_ptr++;
sc->cst_cx_count++;
}
}
}
AcpiOsFree(buf.Pointer);
if (sc->cst_flags & ACPI_CST_FLAG_MATCH_HT) {
cpumask_t mask;
mask = get_cpumask_from_level(sc->cst_cpuid, CORE_LEVEL);
if (CPUMASK_TESTNZERO(mask)) {
int cpu;
for (cpu = 0; cpu < ncpus; ++cpu) {
struct acpi_cst_softc *sc1 = acpi_cst_softc[cpu];
if (sc1 == NULL || sc1 == sc ||
(sc1->cst_flags & ACPI_CST_FLAG_ATTACHED) == 0 ||
(sc1->cst_flags & ACPI_CST_FLAG_MATCH_HT) == 0)
continue;
if (!CPUMASK_TESTBIT(mask, sc1->cst_cpuid))
continue;
if (sc1->cst_cx_count != sc->cst_cx_count) {
struct acpi_cst_softc *src_sc, *dst_sc;
if (bootverbose) {
device_printf(sc->cst_dev,
"inconstent C-state count: %d, %s has %d\n",
sc->cst_cx_count,
device_get_nameunit(sc1->cst_dev),
sc1->cst_cx_count);
}
if (sc1->cst_cx_count > sc->cst_cx_count) {
src_sc = sc1;
dst_sc = sc;
} else {
src_sc = sc;
dst_sc = sc1;
}
acpi_cst_copy(dst_sc, src_sc);
}
}
}
}
if (reprobe) {
if ((acpi_cst_quirks & ACPI_CST_QUIRK_NO_BM) == 0) {
for (i = 0; i < sc->cst_cx_count; ++i) {
struct acpi_cst_cx *cx = &sc->cst_cx_states[i];
if (cx->type >= ACPI_STATE_C3) {
AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
break;
}
}
}
acpi_cst_set_lowest_oncpu(sc, sc->cst_cx_lowest_req);
}
acpi_cst_non_c3(sc);
cpu_sfence();
sc->cst_flags &= ~ACPI_CST_FLAG_PROBING;
return (0);
}
static void
acpi_cst_cx_reprobe_cst_handler(struct cpuhelper_msg *msg)
{
int error;
error = acpi_cst_cx_probe_cst(msg->ch_cbarg, 1);
cpuhelper_replymsg(msg, error);
}
static int
acpi_cst_cx_reprobe_cst(struct acpi_cst_softc *sc)
{
struct cpuhelper_msg msg;
cpuhelper_initmsg(&msg, &curthread->td_msgport,
acpi_cst_cx_reprobe_cst_handler, sc, MSGF_PRIORITY);
return (cpuhelper_domsg(&msg, sc->cst_cpuid));
}
static void
acpi_cst_postattach(void *arg)
{
struct acpi_cst_softc *sc;
int i;
devclass_get_devices(acpi_cst_devclass, &acpi_cst_devices,
&acpi_cst_ndevices);
acpi_cst_set_quirks();
if (acpi_cst_use_fadt) {
for (i = 0; i < acpi_cst_ndevices; i++) {
sc = device_get_softc(acpi_cst_devices[i]);
acpi_cst_cx_probe_fadt(sc);
}
} else {
for (i = 0; i < acpi_cst_ndevices; i++) {
sc = device_get_softc(acpi_cst_devices[i]);
if (acpi_cst_quirks & ACPI_CST_QUIRK_NO_C3) {
acpi_cst_free_resource(sc, sc->cst_non_c3 + 1);
sc->cst_cx_count = sc->cst_non_c3 + 1;
}
sc->cst_parent->cpu_cst_notify = acpi_cst_notify;
}
}
acpi_cst_global_cx_count();
for (i = 0; i < acpi_cst_ndevices; i++) {
sc = device_get_softc(acpi_cst_devices[i]);
acpi_cst_startup(sc);
if (sc->cst_parent->glob_sysctl_tree != NULL) {
struct acpi_cpu_softc *cpu = sc->cst_parent;
SYSCTL_ADD_PROC(&cpu->glob_sysctl_ctx,
SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
OID_AUTO, "cx_lowest",
CTLTYPE_STRING | CTLFLAG_RW, NULL, 0,
acpi_cst_global_lowest_sysctl, "A",
"Requested global lowest Cx sleep state");
SYSCTL_ADD_PROC(&cpu->glob_sysctl_ctx,
SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
OID_AUTO, "cx_lowest_use",
CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
acpi_cst_global_lowest_use_sysctl, "A",
"Global lowest Cx sleep state to use");
}
}
acpi_cst_cx_lowest = 0;
acpi_cst_cx_lowest_req = 0;
acpi_cst_disable_idle = FALSE;
cpu_sfence();
cpu_idle_hook = acpi_cst_idle;
}
static void
acpi_cst_support_list(struct acpi_cst_softc *sc)
{
struct sbuf sb;
int i;
sbuf_new(&sb, sc->cst_cx_supported, sizeof(sc->cst_cx_supported),
SBUF_FIXEDLEN);
for (i = 0; i < sc->cst_cx_count; i++)
sbuf_printf(&sb, "C%d/%d ", i + 1, sc->cst_cx_states[i].trans_lat);
sbuf_trim(&sb);
sbuf_finish(&sb);
}
static void
acpi_cst_c3_bm_rld_handler(struct cpuhelper_msg *msg)
{
AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
cpuhelper_replymsg(msg, 0);
}
static void
acpi_cst_c3_bm_rld(struct acpi_cst_softc *sc)
{
struct cpuhelper_msg msg;
cpuhelper_initmsg(&msg, &curthread->td_msgport,
acpi_cst_c3_bm_rld_handler, sc, MSGF_PRIORITY);
cpuhelper_domsg(&msg, sc->cst_cpuid);
}
static void
acpi_cst_startup(struct acpi_cst_softc *sc)
{
struct acpi_cpu_softc *cpu = sc->cst_parent;
int i, bm_rld_done = 0;
for (i = 0; i < sc->cst_cx_count; ++i) {
struct acpi_cst_cx *cx = &sc->cst_cx_states[i];
int error;
if (cx->type >= ACPI_STATE_C3 && !bm_rld_done &&
(acpi_cst_quirks & ACPI_CST_QUIRK_NO_BM) == 0) {
acpi_cst_c3_bm_rld(sc);
bm_rld_done = 1;
}
error = acpi_cst_cx_setup(cx);
if (error)
panic("C%d startup setup failed: %d", i + 1, error);
}
acpi_cst_support_list(sc);
SYSCTL_ADD_STRING(&cpu->pcpu_sysctl_ctx,
SYSCTL_CHILDREN(cpu->pcpu_sysctl_tree),
OID_AUTO, "cx_supported", CTLFLAG_RD,
sc->cst_cx_supported, 0,
"Cx/microsecond values for supported Cx states");
SYSCTL_ADD_PROC(&cpu->pcpu_sysctl_ctx,
SYSCTL_CHILDREN(cpu->pcpu_sysctl_tree),
OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
(void *)sc, 0, acpi_cst_lowest_sysctl, "A",
"requested lowest Cx sleep state");
SYSCTL_ADD_PROC(&cpu->pcpu_sysctl_ctx,
SYSCTL_CHILDREN(cpu->pcpu_sysctl_tree),
OID_AUTO, "cx_lowest_use", CTLTYPE_STRING | CTLFLAG_RD,
(void *)sc, 0, acpi_cst_lowest_use_sysctl, "A",
"lowest Cx sleep state to use");
SYSCTL_ADD_PROC(&cpu->pcpu_sysctl_ctx,
SYSCTL_CHILDREN(cpu->pcpu_sysctl_tree),
OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
(void *)sc, 0, acpi_cst_usage_sysctl, "A",
"percent usage for each Cx state");
#ifdef notyet
if (!acpi_cst_use_fadt && acpi_cst_ctrl != 0) {
ACPI_LOCK(acpi);
AcpiOsWritePort(acpi_cst_smi_cmd, acpi_cst_ctrl, 8);
ACPI_UNLOCK(acpi);
}
#endif
}
static void
acpi_cst_idle(void)
{
struct acpi_cst_softc *sc;
struct acpi_cst_cx *cx_next;
union microtime_pcpu start, end;
int cx_next_idx, i, tdiff, bm_arb_disabled = 0;
if (acpi_cst_disable_idle) {
ACPI_ENABLE_IRQS();
return;
}
sc = acpi_cst_softc[mdcpu->mi.gd_cpuid];
if (sc == NULL) {
acpi_cst_c1_halt();
return;
}
if (sc->cst_flags & ACPI_CST_FLAG_PROBING) {
acpi_cst_c1_halt();
return;
}
cx_next_idx = 0;
for (i = sc->cst_cx_lowest; i >= 0; i--) {
if (sc->cst_cx_states[i].trans_lat * 3 <= sc->cst_prev_sleep) {
cx_next_idx = i;
break;
}
}
cx_next = &sc->cst_cx_states[cx_next_idx];
if (cx_next->flags & ACPI_CST_CX_FLAG_BM_STS) {
int bm_active;
AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active);
if (bm_active != 0) {
AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
cx_next_idx = sc->cst_non_c3;
}
}
cx_next = &sc->cst_cx_states[cx_next_idx];
sc->cst_cx_stats[cx_next_idx]++;
KASSERT(cx_next->type != ACPI_STATE_C0, ("C0 sleep"));
if (cx_next->type == ACPI_STATE_C1) {
sc->cst_prev_sleep = (sc->cst_prev_sleep * 3 + 500000 / hz) / 4;
cx_next->enter(cx_next);
return;
}
if (cx_next->preamble == ACPI_CST_CX_PREAMBLE_BM_ARB) {
AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
bm_arb_disabled = 1;
} else if (cx_next->preamble == ACPI_CST_CX_PREAMBLE_WBINVD) {
ACPI_FLUSH_CPU_CACHE();
}
microtime_pcpu_get(&start);
cpu_mfence();
cx_next->enter(cx_next);
cpu_mfence();
microtime_pcpu_get(&end);
if (bm_arb_disabled)
AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
ACPI_ENABLE_IRQS();
tdiff = microtime_pcpu_diff(&start, &end);
sc->cst_prev_sleep = (sc->cst_prev_sleep * 3 + tdiff) / 4;
}
static void
acpi_cst_notify(device_t dev)
{
struct acpi_cst_softc *sc = device_get_softc(dev);
cpuhelper_assert(mycpuid, false);
lwkt_serialize_enter(&acpi_cst_slize);
acpi_cst_cx_reprobe_cst(sc);
acpi_cst_support_list(sc);
acpi_cst_global_cx_count();
if (acpi_cst_cx_lowest_req < acpi_cst_cx_count)
acpi_cst_cx_lowest = acpi_cst_cx_lowest_req;
if (acpi_cst_cx_lowest > acpi_cst_cx_count - 1)
acpi_cst_cx_lowest = acpi_cst_cx_count - 1;
lwkt_serialize_exit(&acpi_cst_slize);
}
static int
acpi_cst_set_quirks(void)
{
device_t acpi_dev;
uint32_t val;
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
if (AcpiGbl_FADT.Pm2ControlBlock == 0 ||
AcpiGbl_FADT.Pm2ControlLength == 0) {
if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) &&
(AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0) {
acpi_cst_quirks |= ACPI_CST_QUIRK_NO_BM;
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"cpu_cst: no BM control, using flush cache method\n"));
} else {
acpi_cst_quirks |= ACPI_CST_QUIRK_NO_C3;
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"cpu_cst: no BM control, C3 not available\n"));
}
}
acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
if (acpi_dev != NULL) {
switch (pci_get_revid(acpi_dev)) {
case PCI_REVISION_A_STEP:
case PCI_REVISION_B_STEP:
case PCI_REVISION_4E:
case PCI_REVISION_4M:
acpi_cst_quirks |= ACPI_CST_QUIRK_NO_C3;
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"cpu_cst: working around PIIX4 bug, disabling C3\n"));
val = pci_read_config(acpi_dev, PIIX4_DEVACTB_REG, 4);
if ((val & PIIX4_STOP_BREAK_MASK) != PIIX4_STOP_BREAK_MASK) {
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"cpu_cst: PIIX4: enabling IRQs to generate Stop Break\n"));
val |= PIIX4_STOP_BREAK_MASK;
pci_write_config(acpi_dev, PIIX4_DEVACTB_REG, val, 4);
}
AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_RLD, &val);
if (val) {
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"cpu_cst: PIIX4: reset BRLD_EN_BM\n"));
AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
}
break;
default:
break;
}
}
return (0);
}
static int
acpi_cst_usage_sysctl(SYSCTL_HANDLER_ARGS)
{
struct acpi_cst_softc *sc;
struct sbuf sb;
char buf[128];
int i;
uintmax_t fract, sum, whole;
sc = (struct acpi_cst_softc *) arg1;
sum = 0;
for (i = 0; i < sc->cst_cx_count; i++)
sum += sc->cst_cx_stats[i];
sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
for (i = 0; i < sc->cst_cx_count; i++) {
if (sum > 0) {
whole = (uintmax_t)sc->cst_cx_stats[i] * 100;
fract = (whole % sum) * 100;
sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
(u_int)(fract / sum));
} else
sbuf_printf(&sb, "0.00%% ");
}
sbuf_printf(&sb, "last %dus", sc->cst_prev_sleep);
sbuf_trim(&sb);
sbuf_finish(&sb);
sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
sbuf_delete(&sb);
return (0);
}
static int
acpi_cst_set_lowest_oncpu(struct acpi_cst_softc *sc, int val)
{
int old_lowest, error = 0, old_lowest_req;
uint32_t old_type, type;
KKASSERT(mycpuid == sc->cst_cpuid);
old_lowest_req = sc->cst_cx_lowest_req;
sc->cst_cx_lowest_req = val;
if (val > sc->cst_cx_count - 1)
val = sc->cst_cx_count - 1;
old_lowest = atomic_swap_int(&sc->cst_cx_lowest, val);
old_type = sc->cst_cx_states[old_lowest].type;
type = sc->cst_cx_states[val].type;
if (old_type >= ACPI_STATE_C3 && type < ACPI_STATE_C3) {
cputimer_intr_powersave_remreq();
} else if (type >= ACPI_STATE_C3 && old_type < ACPI_STATE_C3) {
error = cputimer_intr_powersave_addreq();
if (error) {
sc->cst_cx_lowest_req = old_lowest_req;
sc->cst_cx_lowest = old_lowest;
}
}
if (error)
return error;
acpi_cst_non_c3(sc);
bzero(sc->cst_cx_stats, sizeof(sc->cst_cx_stats));
return (0);
}
static void
acpi_cst_set_lowest_handler(struct cpuhelper_msg *msg)
{
int error;
error = acpi_cst_set_lowest_oncpu(msg->ch_cbarg, msg->ch_cbarg1);
cpuhelper_replymsg(msg, error);
}
static int
acpi_cst_set_lowest(struct acpi_cst_softc *sc, int val)
{
struct cpuhelper_msg msg;
cpuhelper_initmsg(&msg, &curthread->td_msgport,
acpi_cst_set_lowest_handler, sc, MSGF_PRIORITY);
msg.ch_cbarg1 = val;
return (cpuhelper_domsg(&msg, sc->cst_cpuid));
}
static int
acpi_cst_lowest_sysctl(SYSCTL_HANDLER_ARGS)
{
struct acpi_cst_softc *sc;
char state[8];
int val, error;
sc = (struct acpi_cst_softc *)arg1;
ksnprintf(state, sizeof(state), "C%d", sc->cst_cx_lowest_req + 1);
error = sysctl_handle_string(oidp, state, sizeof(state), req);
if (error != 0 || req->newptr == NULL)
return (error);
if (strlen(state) < 2 || toupper(state[0]) != 'C')
return (EINVAL);
val = (int) strtol(state + 1, NULL, 10) - 1;
if (val < 0)
return (EINVAL);
lwkt_serialize_enter(&acpi_cst_slize);
error = acpi_cst_set_lowest(sc, val);
lwkt_serialize_exit(&acpi_cst_slize);
return error;
}
static int
acpi_cst_lowest_use_sysctl(SYSCTL_HANDLER_ARGS)
{
struct acpi_cst_softc *sc;
char state[8];
sc = (struct acpi_cst_softc *)arg1;
ksnprintf(state, sizeof(state), "C%d", sc->cst_cx_lowest + 1);
return sysctl_handle_string(oidp, state, sizeof(state), req);
}
static int
acpi_cst_global_lowest_sysctl(SYSCTL_HANDLER_ARGS)
{
struct acpi_cst_softc *sc;
char state[8];
int val, error, i;
ksnprintf(state, sizeof(state), "C%d", acpi_cst_cx_lowest_req + 1);
error = sysctl_handle_string(oidp, state, sizeof(state), req);
if (error != 0 || req->newptr == NULL)
return (error);
if (strlen(state) < 2 || toupper(state[0]) != 'C')
return (EINVAL);
val = (int) strtol(state + 1, NULL, 10) - 1;
if (val < 0)
return (EINVAL);
lwkt_serialize_enter(&acpi_cst_slize);
acpi_cst_cx_lowest_req = val;
acpi_cst_cx_lowest = val;
if (acpi_cst_cx_lowest > acpi_cst_cx_count - 1)
acpi_cst_cx_lowest = acpi_cst_cx_count - 1;
for (i = 0; i < acpi_cst_ndevices; i++) {
sc = device_get_softc(acpi_cst_devices[i]);
error = acpi_cst_set_lowest(sc, val);
if (error) {
KKASSERT(i == 0);
break;
}
}
lwkt_serialize_exit(&acpi_cst_slize);
return error;
}
static int
acpi_cst_global_lowest_use_sysctl(SYSCTL_HANDLER_ARGS)
{
char state[8];
ksnprintf(state, sizeof(state), "C%d", acpi_cst_cx_lowest + 1);
return sysctl_handle_string(oidp, state, sizeof(state), req);
}
static void
acpi_cst_c1_halt(void)
{
cpu_idle_halt();
}
static void
acpi_cst_non_c3(struct acpi_cst_softc *sc)
{
int i;
sc->cst_non_c3 = 0;
for (i = sc->cst_cx_lowest; i >= 0; i--) {
if (sc->cst_cx_states[i].type < ACPI_STATE_C3) {
sc->cst_non_c3 = i;
break;
}
}
if (bootverbose)
device_printf(sc->cst_dev, "non-C3 %d\n", sc->cst_non_c3);
}
static void
acpi_cst_global_cx_count(void)
{
struct acpi_cst_softc *sc;
int i;
if (acpi_cst_ndevices == 0) {
acpi_cst_cx_count = 0;
return;
}
sc = device_get_softc(acpi_cst_devices[0]);
acpi_cst_cx_count = sc->cst_cx_count;
for (i = 1; i < acpi_cst_ndevices; i++) {
struct acpi_cst_softc *sc = device_get_softc(acpi_cst_devices[i]);
if (sc->cst_cx_count < acpi_cst_cx_count)
acpi_cst_cx_count = sc->cst_cx_count;
}
if (bootverbose)
kprintf("cpu_cst: global Cx count %d\n", acpi_cst_cx_count);
}
static void
acpi_cst_c1_halt_enter(const struct acpi_cst_cx *cx __unused)
{
acpi_cst_c1_halt();
}
static void
acpi_cst_cx_io_enter(const struct acpi_cst_cx *cx)
{
uint64_t dummy;
bus_space_read_1(cx->btag, cx->bhand, 0);
AcpiRead(&dummy, &AcpiGbl_FADT.XPmTimerBlock);
}
static int
acpi_cst_cx_setup(struct acpi_cst_cx *cx)
{
cx->flags &= ~ACPI_CST_CX_FLAG_BM_STS;
cx->preamble = ACPI_CST_CX_PREAMBLE_NONE;
if (cx->type >= ACPI_STATE_C3) {
if ((acpi_cst_quirks & ACPI_CST_QUIRK_NO_BM) == 0)
cx->flags |= ACPI_CST_CX_FLAG_BM_STS;
if (ncpus == 1 && (acpi_cst_quirks & ACPI_CST_QUIRK_NO_BM) == 0)
cx->preamble = ACPI_CST_CX_PREAMBLE_BM_ARB;
else
cx->preamble = ACPI_CST_CX_PREAMBLE_WBINVD;
}
return acpi_cst_md_cx_setup(cx);
}
static void
acpi_cst_free_resource(struct acpi_cst_softc *sc, int start)
{
int i;
for (i = start; i < MAX_CX_STATES; ++i) {
struct acpi_cst_cx *cx = &sc->cst_cx_states[i];
if (cx->res != NULL)
bus_release_resource(sc->cst_dev, cx->res_type, cx->rid, cx->res);
memset(cx, 0, sizeof(*cx));
}
}