#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/file.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ppmvar.h>
uint_t ppm_do_vcore = 0;
static int
ppm_cpu_next(ppm_domain_t *domp, int level)
{
#ifdef DEBUG
char *str = "ppm_cpu_next";
#endif
ppm_dc_t *dc;
int index = level - 1;
int ret = 0;
dc = ppm_lookup_dc(domp, PPMDC_CPU_NEXT);
for (; dc && (dc->cmd == PPMDC_CPU_NEXT); dc = dc->next) {
switch (dc->method) {
case PPMDC_CPUSPEEDKIO:
ret = ldi_ioctl(dc->lh, dc->m_un.cpu.iowr,
(intptr_t)index, FWRITE | FKIOCTL, kcred, NULL);
if (ret)
return (ret);
break;
default:
PPMD(D_CPU, ("%s: unsupported method(0x%x)\n",
str, dc->method))
return (-1);
}
}
return (ret);
}
static int
ppm_cpu_pre_chng(ppm_domain_t *domp, int oldl, int speedup)
{
#ifdef DEBUG
char *str = "ppm_cpu_pre_chng";
#endif
ppm_dc_t *dc;
int lowest;
int ret = 0;
dc = ppm_lookup_dc(domp, PPMDC_PRE_CHNG);
for (; dc && (dc->cmd == PPMDC_PRE_CHNG); dc = dc->next) {
switch (dc->method) {
case PPMDC_VCORE:
lowest = domp->devlist->lowest;
if ((oldl != lowest) || (speedup != 1))
break;
if (ppm_do_vcore > 0) {
ret = ldi_ioctl(dc->lh,
dc->m_un.cpu.iowr,
(intptr_t)&dc->m_un.cpu.val,
FWRITE | FKIOCTL, kcred, NULL);
if (ret != 0)
return (ret);
if (dc->m_un.cpu.delay > 0)
drv_usecwait(dc->m_un.cpu.delay);
}
break;
default:
PPMD(D_CPU, ("%s: unsupported method(0x%x)\n",
str, dc->method))
return (-1);
}
}
return (ret);
}
static int
ppm_cpu_go(ppm_domain_t *domp, int level)
{
ppm_dc_t *dc;
int ret = 0;
dc = ppm_lookup_dc(domp, PPMDC_CPU_GO);
if (dc == NULL) {
return (ret);
}
switch (dc->method) {
case PPMDC_KIO:
ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
(intptr_t)dc->m_un.kio.val, FWRITE | FKIOCTL,
kcred, NULL);
break;
default:
return (-1);
}
return (ret);
}
static int
ppm_cpu_post_chng(ppm_domain_t *domp, int newl, int speedup)
{
#ifdef DEBUG
char *str = "ppm_cpu_post_chng";
#endif
ppm_dc_t *dc;
int lowest;
int ret = 0;
dc = ppm_lookup_dc(domp, PPMDC_POST_CHNG);
for (; dc && (dc->cmd == PPMDC_POST_CHNG); dc = dc->next) {
switch (dc->method) {
case PPMDC_VCORE:
lowest = domp->devlist->lowest;
if ((newl != lowest) || (speedup != 0))
break;
if (ppm_do_vcore > 0) {
ret = ldi_ioctl(dc->lh,
dc->m_un.cpu.iowr,
(intptr_t)&dc->m_un.cpu.val,
FWRITE | FKIOCTL, kcred, NULL);
if (ret != 0)
return (ret);
if (dc->m_un.cpu.delay > 0)
drv_usecwait(dc->m_un.cpu.delay);
}
break;
default:
PPMD(D_CPU, ("%s: unsupported method(0x%x)\n",
str, dc->method))
return (-1);
}
}
return (ret);
}
int
ppm_change_cpu_power(ppm_dev_t *ppmd, int newlevel)
{
#ifdef DEBUG
char *str = "ppm_change_cpu_power";
#endif
ppm_unit_t *unitp;
ppm_domain_t *domp;
ppm_dev_t *cpup;
dev_info_t *dip;
int level, oldlevel;
int speedup, incr, lowest, highest;
char *chstr;
int ret;
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
ASSERT(unitp);
domp = ppmd->domp;
cpup = domp->devlist;
lowest = cpup->lowest;
highest = cpup->highest;
oldlevel = (cpup->level == PM_LEVEL_UNKNOWN) ? highest : cpup->level;
dip = cpup->dip;
ASSERT(dip);
PPMD(D_CPU, ("%s: old %d, new %d, highest %d, lowest %d\n",
str, oldlevel, newlevel, highest, lowest))
if (newlevel > oldlevel) {
chstr = "UP";
speedup = 1;
incr = 1;
} else if (newlevel < oldlevel) {
chstr = "DOWN";
speedup = 0;
incr = -1;
} else
return (DDI_SUCCESS);
for (level = oldlevel+incr; level != newlevel+incr; level += incr) {
for (; cpup; cpup = cpup->next) {
if (cpup->level == level)
continue;
ret = pm_power(cpup->dip, 0, level);
PPMD(D_CPU, ("%s: \"%s\", %s to level %d, ret %d\n",
str, cpup->path, chstr, level, ret))
if (ret == DDI_SUCCESS) {
cpup->level = level;
cpup->rplvl = PM_LEVEL_UNKNOWN;
continue;
}
if (speedup == 0)
ret = ppm_revert_cpu_power(cpup, level - incr);
return (ret);
}
cpup = domp->devlist;
ret = ppm_cpu_next(domp, level);
if (ret != 0) {
(void) ppm_revert_cpu_power(cpup, level - incr);
return (ret);
}
ret = ppm_cpu_pre_chng(domp, level - incr, speedup);
if (ret != 0) {
(void) ppm_revert_cpu_power(cpup, level - incr);
(void) ppm_cpu_next(domp, level - incr);
return (ret);
}
if ((oldlevel == highest) && (speedup == 0))
drv_usecwait(1);
ret = ppm_cpu_go(domp, level);
if (ret != 0) {
(void) ppm_revert_cpu_power(cpup, level - incr);
(void) ppm_cpu_next(domp, level - incr);
return (ret);
}
ret = ppm_cpu_post_chng(domp, level, speedup);
if (ret != 0)
return (ret);
}
return (DDI_SUCCESS);
}
boolean_t
ppm_manage_early_cpus(dev_info_t *dip, int new, int *result)
{
ppm_dev_t *ppmd = PPM_GET_PRIVATE(dip);
int ret;
if (ppmd->level == PM_LEVEL_UNKNOWN && new == ppmd->highest) {
ret = pm_power(dip, 0, new);
if (ret != DDI_SUCCESS) {
PPMD(D_CPU, ("ppm_manage_early_cpus: pm_power() "
"failed to change power level to %d", new))
} else {
ppmd->level = new;
ppmd->rplvl = PM_LEVEL_UNKNOWN;
}
*result = ret;
return (B_TRUE);
}
return (B_FALSE);
}