HEADSUP: cpufreq import complete, acpi_throttling changed
Jung-uk Kim
jkim at niksun.com
Tue Feb 8 14:43:27 PST 2005
On Sunday 06 February 2005 04:21 pm, Nate Lawson wrote:
> I've finished the major work of importing cpufreq. As part of
> this, the sysctls for acpi throttling have been removed. The
> power_profile script has been updated, so you can use
> performance/economy_cpu_freq= in rc.conf to set AC on/offline cpu
> frequencies. The acpi throttling support has been compiled into
> acpi_perf.ko so load that to get throttling. Do a sysctl dev.cpu
> to get an understanding of the cpufreq sysctls.
>
> If you have throttling, please test the new configuration to be
> sure it still works as before. Final upcoming work will be manpage
> support and bugfixing as necessary.
>
> I'd also like to issue a call for developers to help by porting
> older drivers (like longrun) and out-of-tree new drivers (like
> powernow-k7/k8 and speedstep-m) to the cpufreq interface. It's
> very simple and you just have to figure out if your driver is
> absolute or relative, and convert values to the proper units
> (percent in hundredths, power in mW, etc.) Once that is done,
> please send me the diff for review before commit so I can make sure
> it works properly.
>
> Thanks,
> --
> Nate
I ported Athlon 64's Cool'n'Quiet part of 'acpi_ppc' to the cpufreq
interface. This is really quick-and-dirty version (i. e., lots of
cut-and-paste's from acpi_perf and acpi_ppc) but it seems to work.
The original driver is available from here:
http://www.spa.is.uec.ac.jp/~nfukuda/software/
1. Create sys/modules/acpi/acpi_cnq directory.
2. Copy acpi_cnq.c to sys/dev/acpica directory.
3. Copy Makefile to sys/modules/acpi/acpi_cnq directory.
4. 'cd sys/modules/acpi/acpi_cnq'.
5. Run 'make' and 'make install'.
6. 'kldload acpi_cnq'
That should do.
Cheers,
Jung-uk Kim
-------------- next part --------------
/*-
* Copyright (c) 2003-2005 Nate Lawson (SDG)
* Copyright (c) 2004,2005 FUKUDA Nobuhiko <nfukuda at spa.is.uec.ac.jp>
* Copyright (c) 2005 Jung-uk Kim
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/power.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/sbuf.h>
#include <sys/pcpu.h>
#include <machine/bus_pio.h>
#include <machine/bus.h>
#include <machine/md_var.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include "acpi.h"
#include <dev/acpica/acpivar.h>
#include "cpufreq_if.h"
/*
* Support for ACPI processor performance states (Px) according to
* section x of the ACPI specification via AMD's Cool'n'Quiet.
*/
struct acpi_px {
uint32_t core_freq;
uint32_t power;
uint32_t trans_lat;
uint32_t bm_lat;
uint32_t ctrl_val;
uint32_t sts_val;
};
struct acpi_cnq_softc {
device_t dev;
ACPI_HANDLE handle;
struct acpi_px *px_states; /* ACPI perf states. */
uint32_t px_count; /* Total number of perf states. */
uint32_t px_max_avail; /* Lowest index state available. */
int px_curr_state; /* Active state index. */
};
#define MSR_FIDVID_CTL 0xc0010041
#define MSR_FIDVID_STATUS 0xc0010042
#define write_control(qw) wrmsr(MSR_FIDVID_CTL, (qw))
#define read_status() rdmsr(MSR_FIDVID_STATUS)
#define control_irt(dw) (((dw) >> 30) & 0x03)
#define control_rvo(dw) (((dw) >> 28) & 0x03)
#define control_pll(dw) (((dw) >> 20) & 0x7f)
#define control_mvs(dw) (((dw) >> 18) & 0x03)
#define control_vst(dw) (((dw) >> 11) & 0x7f)
#define control_vid(dw) (((dw) >> 6) & 0x1f)
#define control_fid(dw) ( (dw) & 0x3f)
#define status_vid(qw) (((qw) >> 32) & 0x1f)
#define status_fid(qw) ( (qw) & 0x3f)
#define count_off_irt(irt) DELAY(10 * (1 << (irt)))
#define count_off_vst(vst) DELAY(20 * (vst))
#define FID_TO_VCO_FID(fid) (((fid) < 8) ? 8 + ((fid) << 1) : (fid))
#define write_fidvid(fid, vid, cnt) \
write_control(((cnt) << 32) | (1ULL << 16) | ((vid) << 8) | (fid))
#define READ_PENDING_WAIT(qw) \
do { (qw) = read_status(); } while ((qw) & (1ULL << 31))
#define ACPI_NOTIFY_PERF_STATES 0x80 /* _PSS changed. */
static void acpi_cnq_identify(driver_t *driver, device_t parent);
static int acpi_cnq_probe(device_t dev);
static int acpi_cnq_attach(device_t dev);
static int acpi_cnq_detach(device_t dev);
static int acpi_cnq_evaluate(device_t dev);
static int acpi_cnq_transit(struct acpi_cnq_softc *sc, const int);
static int acpi_px_to_set(device_t dev, struct acpi_px *px,
struct cf_setting *set);
static void acpi_px_startup(void *arg);
static void acpi_px_available(struct acpi_cnq_softc *sc);
static void acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context);
static int acpi_px_settings(device_t dev, struct cf_setting *sets,
int *count, int *type);
static int acpi_px_set(device_t dev, const struct cf_setting *set);
static int acpi_px_get(device_t dev, struct cf_setting *set);
static device_method_t acpi_cnq_methods[] = {
/* Device interface */
DEVMETHOD(device_identify, acpi_cnq_identify),
DEVMETHOD(device_probe, acpi_cnq_probe),
DEVMETHOD(device_attach, acpi_cnq_attach),
DEVMETHOD(device_detach, acpi_cnq_detach),
/* cpufreq interface */
DEVMETHOD(cpufreq_drv_set, acpi_px_set),
DEVMETHOD(cpufreq_drv_get, acpi_px_get),
DEVMETHOD(cpufreq_drv_settings, acpi_px_settings),
{0, 0}
};
static driver_t acpi_cnq_driver = {
"acpi_cnq",
acpi_cnq_methods,
sizeof(struct acpi_cnq_softc),
};
static devclass_t acpi_cnq_devclass;
DRIVER_MODULE(acpi_cnq, cpu, acpi_cnq_driver, acpi_cnq_devclass, 0, 0);
MODULE_DEPEND(acpi_cnq, acpi, 1, 1, 1);
MALLOC_DEFINE(M_ACPICNQ, "acpi_cnq", "Cool'n'Quiet Performance states");
static void
acpi_cnq_identify(driver_t *driver, device_t parent)
{
ACPI_HANDLE handle;
/* Make sure we're not being doubly invoked. */
if (device_find_child(parent, "acpi_cnq", 0) != NULL)
return;
/* Get the handle for the Processor object and check for perf states. */
handle = acpi_get_handle(parent);
if (handle == NULL)
return;
if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PSS", NULL, NULL)))
return;
if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
u_int32_t regs[4];
switch (cpu_id & 0x0f000f00) {
case 0x00000f00: /* K8 family */
do_cpuid(0x80000000, regs);
if (regs[0] < 0x80000007) /* EAX[31:0] */
return;
break;
do_cpuid(0x80000007, regs);
if ((regs[3] & 0x6) != 0x6) /* EDX[2:1] = 11b */
return;
break;
default:
return;
break;
}
}
if (BUS_ADD_CHILD(parent, 0, "acpi_cnq", 0) == NULL)
device_printf(parent, "add acpi_cnq child failed\n");
}
static int
acpi_cnq_probe(device_t dev)
{
device_t perf_dev;
ACPI_HANDLE handle;
ACPI_OBJECT *obj, *pkg;
ACPI_BUFFER buf;
ACPI_GENERIC_ADDRESS gas;
int error;
/* If the ACPI perf driver has attached, let it manage things. */
perf_dev = devclass_get_device(devclass_find("acpi_perf"), 0);
if (perf_dev && device_is_attached(perf_dev))
goto out;
error = ENXIO;
buf.Pointer = NULL;
buf.Length = ACPI_ALLOCATE_BUFFER;
handle = acpi_get_handle(dev);
if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PCT", NULL, &buf)))
goto out;
pkg = (ACPI_OBJECT *)buf.Pointer;
if (ACPI_PKG_VALID(pkg, 2)) {
/* Performance Control Register */
obj = &pkg->Package.Elements[0];
if (obj != NULL && obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3)
goto out;
memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas));
if (gas.AddressSpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE)
goto out;
/* Performance Status Register */
obj = &pkg->Package.Elements[1];
if (obj != NULL && obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3)
goto out;
memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas));
if (gas.AddressSpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE)
goto out;
}
device_set_desc(dev, "AMD64 Cool'n'Quiet");
error = 0;
out:
if (buf.Pointer)
AcpiOsFree(buf.Pointer);
return (error);
}
static int
acpi_cnq_attach(device_t dev)
{
struct acpi_cnq_softc *sc;
sc = device_get_softc(dev);
sc->dev = dev;
sc->handle = acpi_get_handle(dev);
sc->px_max_avail = 0;
sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
if (acpi_cnq_evaluate(dev) != 0)
return (ENXIO);
cpufreq_register(dev);
AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_px_startup, NULL);
return (0);
}
static int
acpi_cnq_detach(device_t dev)
{
/* TODO: teardown registers, remove notify handler. */
return (ENXIO);
}
/* Probe and setup any valid performance states (Px). */
static int
acpi_cnq_evaluate(device_t dev)
{
struct acpi_cnq_softc *sc;
ACPI_BUFFER buf;
ACPI_OBJECT *pkg, *res;
ACPI_STATUS status;
int error, i, j;
uint32_t *p;
/* Get the control values and parameters for each state. */
error = ENXIO;
sc = device_get_softc(dev);
buf.Pointer = NULL;
buf.Length = ACPI_ALLOCATE_BUFFER;
status = AcpiEvaluateObject(sc->handle, "_PSS", NULL, &buf);
if (ACPI_FAILURE(status))
return (ENXIO);
pkg = (ACPI_OBJECT *)buf.Pointer;
if (!ACPI_PKG_VALID(pkg, 1)) {
device_printf(dev, "invalid top level _PSS package\n");
goto out;
}
sc->px_count = pkg->Package.Count;
sc->px_states = malloc(sc->px_count * sizeof(struct acpi_px),
M_ACPICNQ, M_WAITOK | M_ZERO);
if (sc->px_states == NULL)
goto out;
/*
* Each state is a package of {CoreFreq, Power, TransitionLatency,
* BusMasterLatency, ControlVal, StatusVal}, sorted from highest
* performance to lowest.
*/
for (i = 0; i < sc->px_count; i++) {
res = &pkg->Package.Elements[i];
if (!ACPI_PKG_VALID(res, 6)) {
device_printf(dev, "invalid _PSS package\n");
continue;
}
p = &sc->px_states[i].core_freq;
for (j = 0; j < 6; j++, p++)
acpi_PkgInt32(res, j, p);
}
AcpiOsFree(buf.Pointer);
/* Get the control and status registers (one of each). */
buf.Pointer = NULL;
buf.Length = ACPI_ALLOCATE_BUFFER;
status = AcpiEvaluateObject(sc->handle, "_PCT", NULL, &buf);
if (ACPI_FAILURE(status))
goto out;
/* Check the package of two registers, each a Buffer in GAS format. */
pkg = (ACPI_OBJECT *)buf.Pointer;
if (!ACPI_PKG_VALID(pkg, 2)) {
device_printf(dev, "invalid perf register package\n");
goto out;
}
/* Get our current limit and register for notifies. */
acpi_px_available(sc);
AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
acpi_px_notify, sc);
error = 0;
out:
if (error) {
if (sc->px_states)
free(sc->px_states, M_ACPICNQ);
sc->px_count = 0;
}
if (buf.Pointer)
AcpiOsFree(buf.Pointer);
return (error);
}
/*
* AMD K8 Cool'n'Quiet
*
* Reference:
* - BIOS and Kernel Developer's Guide
* for the AMD Athlon 64 and AMD Opteron Processors, 26094 Rev.3.12
* - linux-2.6.3/arch/i386/kernel/cpu/cpufreq/powernow-k8.[ch]
*/
static int
acpi_cnq_transit(struct acpi_cnq_softc *sc, const int state)
{
u_int irt, rvo, pll, mvs, vst, vid, fid;
u_int val, rvo_steps, cur_vid, cur_fid, vco_fid, vco_cur_fid, vco_diff;
u_int64_t v64;
v64 = sc->px_states[state].ctrl_val;
irt = control_irt(v64);
rvo = control_rvo(v64);
pll = control_pll(v64);
mvs = control_mvs(v64);
vst = control_vst(v64);
vid = control_vid(v64);
fid = control_fid(v64);
READ_PENDING_WAIT(v64);
cur_vid = status_vid(v64);
cur_fid = status_fid(v64);
/* Phase 1 */
while (cur_vid > vid) {
val = cur_vid - (1 << mvs);
write_fidvid(cur_fid, (val > 0) ? val : 0, 1ULL);
READ_PENDING_WAIT(v64);
cur_vid = status_vid(v64);
count_off_vst(vst);
}
for (rvo_steps = rvo; rvo_steps > 0 && cur_vid > 0; rvo_steps--) {
write_fidvid(cur_fid, cur_vid - 1, 1ULL);
READ_PENDING_WAIT(v64);
cur_vid = status_vid(v64);
count_off_vst(vst);
}
/* Phase 2 */
if (cur_fid != fid) {
vco_fid = FID_TO_VCO_FID(fid);
vco_cur_fid = FID_TO_VCO_FID(cur_fid);
vco_diff = (vco_cur_fid < vco_fid) ?
vco_fid - vco_cur_fid : vco_cur_fid - vco_fid;
while (vco_diff > 2) {
if (fid > cur_fid) {
if (cur_fid > 6)
val = cur_fid + 2;
else
val = FID_TO_VCO_FID(cur_fid) + 2;
} else
val = cur_fid - 2;
write_fidvid(val, cur_vid, pll * 200ULL);
READ_PENDING_WAIT(v64);
cur_fid = status_fid(v64);
count_off_irt(irt);
vco_cur_fid = FID_TO_VCO_FID(cur_fid);
vco_diff = (vco_cur_fid < vco_fid) ?
vco_fid - vco_cur_fid : vco_cur_fid - vco_fid;
}
write_fidvid(fid, cur_vid, pll * 200ULL);
READ_PENDING_WAIT(v64);
cur_fid = status_fid(v64);
count_off_irt(irt);
}
/* Phase 3 */
if (cur_vid != vid) {
write_fidvid(cur_fid, vid, 1ULL);
READ_PENDING_WAIT(v64);
cur_vid = status_vid(v64);
count_off_vst(vst);
}
/* Done */
if (cur_vid == vid && cur_fid == fid)
return 0;
return -1;
}
static void
acpi_px_startup(void *arg)
{
/* Signal to the platform that we are taking over CPU control. */
if (AcpiGbl_FADT->PstateCnt == 0)
return;
ACPI_LOCK(acpi);
AcpiOsWritePort(AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->PstateCnt, 8);
ACPI_UNLOCK(acpi);
}
static void
acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context)
{
struct acpi_cnq_softc *sc;
sc = context;
if (notify != ACPI_NOTIFY_PERF_STATES)
return;
acpi_px_available(sc);
/* TODO: Implement notification when frequency changes. */
}
/*
* Find the highest currently-supported performance state.
* This can be called at runtime (e.g., due to a docking event) at
* the request of a Notify on the processor object.
*/
static void
acpi_px_available(struct acpi_cnq_softc *sc)
{
ACPI_STATUS status;
struct cf_setting set;
status = acpi_GetInteger(sc->handle, "_PPC", &sc->px_max_avail);
/* If the old state is too high, set current state to the new max. */
if (ACPI_SUCCESS(status)) {
if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN &&
sc->px_curr_state > sc->px_max_avail) {
acpi_px_to_set(sc->dev,
&sc->px_states[sc->px_max_avail], &set);
acpi_px_set(sc->dev, &set);
}
} else
sc->px_max_avail = 0;
}
static int
acpi_px_to_set(device_t dev, struct acpi_px *px, struct cf_setting *set)
{
if (px == NULL || set == NULL)
return (EINVAL);
set->freq = px->core_freq;
set->power = px->power;
/* XXX Include BM latency too? */
set->lat = px->trans_lat;
set->volts = CPUFREQ_VAL_UNKNOWN;
set->dev = dev;
return (0);
}
static int
acpi_px_settings(device_t dev, struct cf_setting *sets, int *count, int *type)
{
struct acpi_cnq_softc *sc;
int x, y;
sc = device_get_softc(dev);
if (sets == NULL || count == NULL)
return (EINVAL);
if (*count < sc->px_count - sc->px_max_avail)
return (ENOMEM);
/* Return a list of settings that are currently valid. */
y = 0;
for (x = sc->px_max_avail; x < sc->px_count; x++, y++)
acpi_px_to_set(dev, &sc->px_states[x], &sets[y]);
*count = sc->px_count - sc->px_max_avail;
*type = CPUFREQ_TYPE_ABSOLUTE;
return (0);
}
static int
acpi_px_set(device_t dev, const struct cf_setting *set)
{
struct acpi_cnq_softc *sc;
int i;
if (set == NULL)
return (EINVAL);
sc = device_get_softc(dev);
/* Look up appropriate state, based on frequency. */
for (i = sc->px_max_avail; i < sc->px_count; i++) {
if (CPUFREQ_CMP(set->freq, sc->px_states[i].core_freq))
break;
}
if (i == sc->px_count)
return (EINVAL);
if (acpi_cnq_transit(sc, i) != 0) {
device_printf(dev, "Px transition to %d failed\n",
sc->px_states[i].core_freq);
return (ENXIO);
} else
sc->px_curr_state = i;
return (0);
}
static int
acpi_px_get(device_t dev, struct cf_setting *set)
{
struct acpi_cnq_softc *sc;
uint64_t rate;
int i;
struct pcpu *pc;
if (set == NULL)
return (EINVAL);
sc = device_get_softc(dev);
/* If we've set the rate before, use the cached value. */
if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN) {
acpi_px_to_set(dev, &sc->px_states[sc->px_curr_state], set);
return (0);
}
/* Otherwise, estimate and try to match against our settings. */
pc = cpu_get_pcpu(dev);
if (pc == NULL)
return (ENXIO);
cpu_est_clockrate(pc->pc_cpuid, &rate);
rate /= 1000000;
for (i = 0; i < sc->px_count; i++) {
if (CPUFREQ_CMP(sc->px_states[i].core_freq, rate)) {
sc->px_curr_state = i;
acpi_px_to_set(dev, &sc->px_states[i], set);
break;
}
}
/* No match, give up. */
if (i == sc->px_count) {
sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
set->freq = CPUFREQ_VAL_UNKNOWN;
}
return (0);
}
-------------- next part --------------
# $FreeBSD$
.PATH: ${.CURDIR}/../../../dev/acpica
CFLAGS+= -I${.CURDIR}/../../../contrib/dev/acpica
KMOD= acpi_cnq
WARNS?= 2
SRCS= acpi_cnq.c
SRCS+= acpi_if.h bus_if.h cpufreq_if.h device_if.h opt_acpi.h \
pci_if.h
.include <bsd.kmod.mk>
More information about the freebsd-acpi
mailing list