#define PSMI_1_7
#include <sys/smp_impldefs.h>
#include <sys/psm.h>
#include <sys/psm_modctl.h>
#include <sys/pit.h>
#include <sys/cmn_err.h>
#include <sys/strlog.h>
#include <sys/clock.h>
#include <sys/debug.h>
#include <sys/rtc.h>
#include <sys/x86_archext.h>
#include <sys/cpupart.h>
#include <sys/cpuvar.h>
#include <sys/cpu_event.h>
#include <sys/cmt.h>
#include <sys/cpu.h>
#include <sys/disp.h>
#include <sys/archsystm.h>
#include <sys/machsystm.h>
#include <sys/sysmacros.h>
#include <sys/memlist.h>
#include <sys/param.h>
#include <sys/promif.h>
#include <sys/cpu_pm.h>
#if defined(__xpv)
#include <sys/hypervisor.h>
#endif
#include <sys/mach_intr.h>
#include <vm/hat_i86.h>
#include <sys/kdi_machimpl.h>
#include <sys/sdt.h>
#include <sys/hpet.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/cpc_pcbe.h>
#include <sys/prom_debug.h>
#include <sys/tsc.h>
#define OFFSETOF(s, m) (size_t)(&(((s *)0)->m))
static int mp_disable_intr(processorid_t cpun);
static void mp_enable_intr(processorid_t cpun);
static void mach_init();
static void mach_picinit();
static int machhztomhz(uint64_t cpu_freq_hz);
static uint64_t mach_getcpufreq(void);
static void mach_fixcpufreq(void);
static int mach_clkinit(int, int *);
static void mach_smpinit(void);
static int mach_softlvl_to_vect(int ipl);
static void mach_get_platform(int owner);
static void mach_construct_info();
static int mach_translate_irq(dev_info_t *dip, int irqno);
static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
psm_intr_op_t, int *);
static void mach_notify_error(int level, char *errmsg);
static hrtime_t dummy_hrtime(void);
static void dummy_scalehrtime(hrtime_t *);
static uint64_t dummy_unscalehrtime(hrtime_t);
void cpu_idle(void);
static void cpu_wakeup(cpu_t *, int);
#ifndef __xpv
void cpu_idle_mwait(void);
static void cpu_wakeup_mwait(cpu_t *, int);
#endif
static int mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp);
extern void return_instr();
extern void pc_gethrestime(timestruc_t *);
extern int cpuid_get_coreid(cpu_t *);
extern int cpuid_get_chipid(cpu_t *);
void (*psm_shutdownf)(int, int) = (void (*)(int, int))return_instr;
void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr;
void (*psm_notifyf)(int) = (void (*)(int))return_instr;
void (*psm_set_idle_cpuf)(int) = (void (*)(int))return_instr;
void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr;
void (*psminitf)() = mach_init;
void (*picinitf)() = return_instr;
int (*clkinitf)(int, int *) = (int (*)(int, int *))return_instr;
int (*ap_mlsetup)() = (int (*)(void))return_instr;
void (*send_dirintf)() = return_instr;
void (*setspl)(int) = (void (*)(int))return_instr;
int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
int (*get_pending_spl)(void) = (int (*)(void))return_instr;
int (*addintr)(void *, int, avfunc, char *, int, caddr_t, caddr_t,
uint64_t *, dev_info_t *) = NULL;
void (*remintr)(void *, int, avfunc, int) = NULL;
void (*kdisetsoftint)(int, struct av_softinfo *)=
(void (*)(int, struct av_softinfo *))return_instr;
void (*setsoftint)(int, struct av_softinfo *)=
(void (*)(int, struct av_softinfo *))return_instr;
int (*slvltovect)(int) = (int (*)(int))return_instr;
int (*setlvl)(int, int *) = (int (*)(int, int *))return_instr;
void (*setlvlx)(int, int) = (void (*)(int, int))return_instr;
int (*psm_disable_intr)(int) = mp_disable_intr;
void (*psm_enable_intr)(int) = mp_enable_intr;
hrtime_t (*gethrtimef)(void) = dummy_hrtime;
hrtime_t (*gethrtimeunscaledf)(void) = dummy_hrtime;
void (*scalehrtimef)(hrtime_t *) = dummy_scalehrtime;
uint64_t (*unscalehrtimef)(hrtime_t) = dummy_unscalehrtime;
int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
int (*psm_get_clockirq)(int) = NULL;
int (*psm_get_ipivect)(int, int) = NULL;
uchar_t (*psm_get_ioapicid)(uchar_t) = NULL;
uint32_t (*psm_get_localapicid)(uint32_t) = NULL;
uchar_t (*psm_xlate_vector_by_irq)(uchar_t) = NULL;
int (*psm_get_pir_ipivect)(void) = NULL;
void (*psm_send_pir_ipi)(processorid_t) = NULL;
void (*psm_cmci_setup)(processorid_t, boolean_t) = NULL;
int (*psm_clkinit)(int) = NULL;
void (*psm_timer_reprogram)(hrtime_t) = NULL;
void (*psm_timer_enable)(void) = NULL;
void (*psm_timer_disable)(void) = NULL;
void (*psm_post_cyclic_setup)(void *arg) = NULL;
int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
int *) = mach_intr_ops;
int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *))
return_instr;
void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
void (*hrtime_tick)(void) = return_instr;
int (*psm_cpu_create_devinfo)(cpu_t *, dev_info_t **) = mach_cpu_create_devinfo;
int (*psm_cpu_get_devinfo)(cpu_t *, dev_info_t **) = NULL;
ddi_irm_pool_t *apix_irm_pool_p = NULL;
#ifdef __xpv
int tsc_gethrtime_enable = 0;
#else
int tsc_gethrtime_enable = 1;
#endif
int tsc_gethrtime_initted = 0;
int gethrtime_hires = 0;
static struct psm_ops mach_ops;
static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
static ushort_t mach_ver[4] = {0, 0, 0, 0};
void *psm_vt_ops = NULL;
int idle_cpu_use_hlt = 1;
#ifndef __xpv
int idle_cpu_prefer_mwait = 1;
int idle_cpu_assert_cflush_monitor = 1;
int idle_cpu_no_deep_c = 0;
void (*non_deep_idle_cpu)() = cpu_idle;
void (*non_deep_idle_disp_enq_thread)(cpu_t *, int);
hpet_t hpet;
#endif
uint_t cp_haltset_fanout = 0;
int
pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
{
switch (hw) {
case PGHW_IPIPE:
if (is_x86_feature(x86_featureset, X86FSET_HTT)) {
return (1);
} else {
return (0);
}
case PGHW_FPU:
if (cpuid_get_cores_per_compunit(cp) > 1)
return (1);
else
return (0);
case PGHW_PROCNODE:
if (cpuid_get_procnodes_per_pkg(cp) > 1)
return (1);
else
return (0);
case PGHW_CHIP:
if (is_x86_feature(x86_featureset, X86FSET_CMP) ||
is_x86_feature(x86_featureset, X86FSET_HTT))
return (1);
else
return (0);
case PGHW_CACHE:
if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
return (1);
else
return (0);
case PGHW_POW_ACTIVE:
if (cpupm_domain_id(cp, CPUPM_DTYPE_ACTIVE) != (id_t)-1)
return (1);
else
return (0);
case PGHW_POW_IDLE:
if (cpupm_domain_id(cp, CPUPM_DTYPE_IDLE) != (id_t)-1)
return (1);
else
return (0);
default:
return (0);
}
}
int
pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
{
id_t pgp_a, pgp_b;
pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
if (pgp_a == -1 || pgp_b == -1)
return (-1);
return (pgp_a == pgp_b);
}
id_t
pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
{
switch (hw) {
case PGHW_IPIPE:
return (cpuid_get_coreid(cpu));
case PGHW_CACHE:
return (cpuid_get_last_lvl_cacheid(cpu));
case PGHW_FPU:
return (cpuid_get_compunitid(cpu));
case PGHW_PROCNODE:
return (cpuid_get_procnodeid(cpu));
case PGHW_CHIP:
return (cpuid_get_chipid(cpu));
case PGHW_POW_ACTIVE:
return (cpupm_domain_id(cpu, CPUPM_DTYPE_ACTIVE));
case PGHW_POW_IDLE:
return (cpupm_domain_id(cpu, CPUPM_DTYPE_IDLE));
default:
return (-1);
}
}
pghw_type_t
pg_plat_hw_rank(pghw_type_t hw1, pghw_type_t hw2)
{
int i, rank1, rank2;
static pghw_type_t hw_hier[] = {
PGHW_IPIPE,
PGHW_CACHE,
PGHW_FPU,
PGHW_PROCNODE,
PGHW_CHIP,
PGHW_POW_IDLE,
PGHW_POW_ACTIVE,
PGHW_NUM_COMPONENTS
};
rank1 = 0;
rank2 = 0;
for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
if (hw_hier[i] == hw1)
rank1 = i;
if (hw_hier[i] == hw2)
rank2 = i;
}
if (rank1 > rank2)
return (hw1);
else
return (hw2);
}
pg_cmt_policy_t
pg_plat_cmt_policy(pghw_type_t hw)
{
switch (hw) {
case PGHW_FPU:
case PGHW_CACHE:
return (CMT_BALANCE|CMT_AFFINITY);
default:
return (CMT_NO_POLICY);
}
}
id_t
pg_plat_get_core_id(cpu_t *cpu)
{
return ((id_t)cpuid_get_coreid(cpu));
}
void
cmp_set_nosteal_interval(void)
{
nosteal_nsec = 100000UL;
}
static hrtime_t
dummy_hrtime(void)
{
return (0);
}
static void
dummy_scalehrtime(hrtime_t *ticks)
{}
static uint64_t
dummy_unscalehrtime(hrtime_t nsecs)
{
return ((uint64_t)nsecs);
}
void
cpu_idle_adaptive(void)
{
(*CPU->cpu_m.mcpu_idle_cpu)();
}
static void
cpu_idle_check_wakeup(void *arg)
{
sti();
SMT_PAUSE();
cli();
}
void
cpu_idle(void)
{
cpu_t *cpup = CPU;
processorid_t cpu_sid = cpup->cpu_seqid;
cpupart_t *cp = cpup->cpu_part;
int hset_update = 1;
if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
hset_update = 0;
if (hset_update) {
cpup->cpu_disp_flags |= CPU_DISP_HALTED;
bitset_atomic_add(&cp->cp_haltset, cpu_sid);
}
if (disp_anywork()) {
if (hset_update) {
cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
bitset_atomic_del(&cp->cp_haltset, cpu_sid);
}
return;
}
cli();
if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) {
cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
sti();
return;
}
if (cpup->cpu_disp->disp_nrunnable != 0) {
if (hset_update) {
cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
bitset_atomic_del(&cp->cp_haltset, cpu_sid);
}
sti();
return;
}
if (cpu_idle_enter(IDLE_STATE_C1, 0,
cpu_idle_check_wakeup, NULL) == 0) {
mach_cpu_idle();
cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
}
if (hset_update) {
cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
bitset_atomic_del(&cp->cp_haltset, cpu_sid);
}
}
static void
cpu_wakeup(cpu_t *cpu, int bound)
{
uint_t cpu_found;
processorid_t cpu_sid;
cpupart_t *cp;
cp = cpu->cpu_part;
cpu_sid = cpu->cpu_seqid;
if (bitset_in_set(&cp->cp_haltset, cpu_sid)) {
bitset_atomic_del(&cp->cp_haltset, cpu_sid);
if (cpu != CPU)
poke_cpu(cpu->cpu_id);
return;
} else {
if (cpu->cpu_thread == cpu->cpu_idle_thread ||
cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
return;
}
if (bound)
return;
do {
cpu_found = bitset_find(&cp->cp_haltset);
if (cpu_found == (uint_t)-1)
return;
} while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0);
if (cpu_found != CPU->cpu_seqid) {
poke_cpu(cpu_seq[cpu_found]->cpu_id);
}
}
#ifndef __xpv
static void
cpu_idle_mwait_check_wakeup(void *arg)
{
volatile uint32_t *mcpu_mwait = (volatile uint32_t *)arg;
ASSERT(arg != NULL);
if (*mcpu_mwait != MWAIT_HALTED) {
cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
} else {
sti();
SMT_PAUSE();
cli();
}
}
void
cpu_idle_mwait(void)
{
volatile uint32_t *mcpu_mwait = CPU->cpu_m.mcpu_mwait;
cpu_t *cpup = CPU;
processorid_t cpu_sid = cpup->cpu_seqid;
cpupart_t *cp = cpup->cpu_part;
int hset_update = 1;
*mcpu_mwait = MWAIT_HALTED;
if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
hset_update = 0;
if (hset_update) {
cpup->cpu_disp_flags |= CPU_DISP_HALTED;
bitset_atomic_add(&cp->cp_haltset, cpu_sid);
}
if (disp_anywork()) {
if (hset_update) {
cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
bitset_atomic_del(&cp->cp_haltset, cpu_sid);
}
return;
}
i86_monitor(mcpu_mwait, 0, 0);
if (*mcpu_mwait == MWAIT_HALTED) {
if (cpu_idle_enter(IDLE_STATE_C1, 0,
cpu_idle_mwait_check_wakeup, (void *)mcpu_mwait) == 0) {
if (*mcpu_mwait == MWAIT_HALTED) {
i86_mwait(0, 0);
}
cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
}
}
if (hset_update) {
cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
bitset_atomic_del(&cp->cp_haltset, cpu_sid);
}
}
static void
cpu_wakeup_mwait(cpu_t *cp, int bound)
{
cpupart_t *cpu_part;
uint_t cpu_found;
processorid_t cpu_sid;
cpu_part = cp->cpu_part;
cpu_sid = cp->cpu_seqid;
if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) {
bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid);
MWAIT_WAKEUP(cp);
return;
} else {
if (cp->cpu_thread == cp->cpu_idle_thread ||
cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
return;
}
if (bound || ncpus == 1)
return;
do {
cpu_found = bitset_find(&cpu_part->cp_haltset);
if (cpu_found == (uint_t)-1)
return;
} while (bitset_atomic_test_and_del(&cpu_part->cp_haltset,
cpu_found) < 0);
MWAIT_WAKEUP(cpu_seq[cpu_found]);
}
#endif
void (*cpu_pause_handler)(volatile char *) = NULL;
static int
mp_disable_intr(int cpun)
{
affinity_set(cpun);
splx(XC_SYS_PIL - 1);
CPU->cpu_intr_actv |= (1 << (XC_SYS_PIL - 1));
set_base_spl();
affinity_clear();
return (DDI_SUCCESS);
}
static void
mp_enable_intr(int cpun)
{
affinity_set(cpun);
CPU->cpu_intr_actv &= ~(1 << (XC_SYS_PIL - 1));
set_base_spl();
(void) spl0();
affinity_clear();
}
static void
mach_get_platform(int owner)
{
void **srv_opsp;
void **clt_opsp;
int i;
int total_ops;
srv_opsp = (void **)mach_set[0];
clt_opsp = (void **)mach_set[owner];
if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
total_ops = sizeof (struct psm_ops_ver01) /
sizeof (void (*)(void));
else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
sizeof (void (*)(void));
else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
sizeof (void (*)(void));
else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
sizeof (void (*)(void));
else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
sizeof (void (*)(void));
else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_5)
total_ops = OFFSETOF(struct psm_ops, psm_state) /
sizeof (void (*)(void));
else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_6)
total_ops = OFFSETOF(struct psm_ops, psm_cpu_ops) /
sizeof (void (*)(void));
else
total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
mach_ver[0] = mach_ver[owner];
for (i = 0; i < total_ops; i++)
if (clt_opsp[i] != NULL)
srv_opsp[i] = clt_opsp[i];
}
static void
mach_construct_info()
{
struct psm_sw *swp;
int mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
int conflict_owner = 0;
if (psmsw->psw_forw == psmsw)
panic("No valid PSM modules found");
mutex_enter(&psmsw_lock);
for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
continue;
mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
mach_cnt[swp->psw_infop->p_owner]++;
}
mutex_exit(&psmsw_lock);
mach_get_platform(PSM_OWN_SYS_DEFAULT);
if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
conflict_owner = PSM_OWN_EXCLUSIVE;
if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
conflict_owner = PSM_OWN_OVERRIDE;
if (conflict_owner) {
cmn_err(CE_WARN,
"Conflicts detected on the following PSM modules:");
mutex_enter(&psmsw_lock);
for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
if (swp->psw_infop->p_owner == conflict_owner)
cmn_err(CE_WARN, "%s ",
swp->psw_infop->p_mach_idstring);
}
mutex_exit(&psmsw_lock);
cmn_err(CE_WARN,
"Setting the system back to SINGLE processor mode!");
cmn_err(CE_WARN,
"Please edit /etc/mach to remove the invalid PSM module.");
return;
}
if (mach_set[PSM_OWN_EXCLUSIVE])
mach_get_platform(PSM_OWN_EXCLUSIVE);
if (mach_set[PSM_OWN_OVERRIDE])
mach_get_platform(PSM_OWN_OVERRIDE);
}
static void
mach_init()
{
struct psm_ops *pops;
PRM_POINT("mach_construct_info()");
mach_construct_info();
pops = mach_set[0];
picinitf = mach_picinit;
clkinitf = mach_clkinit;
psm_get_clockirq = pops->psm_get_clockirq;
slvltovect = mach_softlvl_to_vect;
addspl = pops->psm_addspl;
delspl = pops->psm_delspl;
if (pops->psm_translate_irq)
psm_translate_irq = pops->psm_translate_irq;
if (pops->psm_intr_ops)
psm_intr_ops = pops->psm_intr_ops;
#if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
if (pops->psm_tod_get)
cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
(void *)pops->psm_tod_get);
if (pops->psm_tod_set)
cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
(void *)pops->psm_tod_set);
#endif
if (pops->psm_notify_error) {
psm_notify_error = mach_notify_error;
notify_error = pops->psm_notify_error;
}
PRM_POINT("psm_softinit()");
(*pops->psm_softinit)();
#ifndef __xpv
non_deep_idle_disp_enq_thread = disp_enq_thread;
#endif
PRM_DEBUG(idle_cpu_use_hlt);
if (idle_cpu_use_hlt) {
idle_cpu = cpu_idle_adaptive;
CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
#ifndef __xpv
if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
idle_cpu_prefer_mwait) {
CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
if (CPU->cpu_m.mcpu_mwait == NULL) {
#ifdef DEBUG
cmn_err(CE_NOTE, "Using hlt idle. Cannot "
"handle cpu 0 mwait size.");
#endif
idle_cpu_prefer_mwait = 0;
CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
} else {
CPU->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
}
} else {
CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
}
non_deep_idle_cpu = CPU->cpu_m.mcpu_idle_cpu;
if (idle_cpu_no_deep_c) {
idle_cpu = non_deep_idle_cpu;
}
#endif
}
PRM_POINT("mach_smpinit()");
mach_smpinit();
}
static void
mach_smpinit(void)
{
struct psm_ops *pops;
processorid_t cpu_id;
int cnt;
cpuset_t cpumask;
pops = mach_set[0];
CPUSET_ZERO(cpumask);
cpu_id = -1;
cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
for (cnt = 0; cpu_id != -1 && cnt < boot_ncpus; cnt++) {
CPUSET_ADD(cpumask, cpu_id);
cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
}
mp_cpus = cpumask;
ap_mlsetup = pops->psm_post_cpu_start;
send_dirintf = pops->psm_send_ipi;
if (pops->psm_shutdown)
psm_shutdownf = pops->psm_shutdown;
if (pops->psm_preshutdown)
psm_preshutdownf = pops->psm_preshutdown;
if (pops->psm_notify_func)
psm_notifyf = pops->psm_notify_func;
if (pops->psm_set_idlecpu)
psm_set_idle_cpuf = pops->psm_set_idlecpu;
if (pops->psm_unset_idlecpu)
psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
psm_clkinit = pops->psm_clkinit;
if (pops->psm_timer_reprogram)
psm_timer_reprogram = pops->psm_timer_reprogram;
if (pops->psm_timer_enable)
psm_timer_enable = pops->psm_timer_enable;
if (pops->psm_timer_disable)
psm_timer_disable = pops->psm_timer_disable;
if (pops->psm_post_cyclic_setup)
psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
if (pops->psm_state)
psm_state = pops->psm_state;
if (pops->psm_disable_intr)
psm_disable_intr = pops->psm_disable_intr;
if (pops->psm_enable_intr)
psm_enable_intr = pops->psm_enable_intr;
psm_get_ipivect = pops->psm_get_ipivect;
if (cnt < 2 && plat_dr_support_cpu() == B_FALSE)
return;
if (pops->psm_cpu_start == NULL)
return;
if (idle_cpu_use_hlt) {
disp_enq_thread = cpu_wakeup;
#ifndef __xpv
if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
idle_cpu_prefer_mwait)
disp_enq_thread = cpu_wakeup_mwait;
non_deep_idle_disp_enq_thread = disp_enq_thread;
#endif
}
psm_get_pir_ipivect = pops->psm_get_pir_ipivect;
psm_send_pir_ipi = pops->psm_send_pir_ipi;
psm_cmci_setup = pops->psm_cmci_setup;
(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_intr",
(*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
NULL, NULL, NULL, NULL);
(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
}
static void
mach_picinit()
{
struct psm_ops *pops;
pops = mach_set[0];
setlvl = pops->psm_intr_enter;
setlvlx = pops->psm_intr_exit;
(*pops->psm_picinit)();
setspl = pops->psm_setspl;
cli();
setspl(CPU->cpu_pri);
}
uint_t cpu_freq;
uint64_t cpu_freq_hz;
#define MEGA_HZ 1000000
#ifdef __xpv
int xpv_cpufreq_workaround = 1;
int xpv_cpufreq_verbose = 0;
#endif
static uint64_t
mach_getcpufreq(void)
{
#ifndef __xpv
return (tsc_get_freq());
#else
vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time;
uint64_t cpu_hz;
if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) {
cpu_hz = 1000 * xpv_cpu_khz();
} else {
cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul;
if (vti->tsc_shift < 0)
cpu_hz <<= -vti->tsc_shift;
else
cpu_hz >>= vti->tsc_shift;
}
if (xpv_cpufreq_verbose)
printf("mach_getcpufreq: system_mul 0x%x, shift %d, "
"cpu_hz %" PRId64 "Hz\n",
vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz);
return (cpu_hz);
#endif
}
static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
static void
mach_fixcpufreq(void)
{
uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
freq = (uint32_t)cpu_freq;
mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
mul = (freq + 25) / 50;
near50 = mul * 50;
delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
if (delta66 < delta50) {
fixed = near66;
delta = delta66;
} else {
fixed = near50;
delta = delta50;
}
if (fixed > INT_MAX)
return;
i = sizeof (x86_cpu_freq) / sizeof (int);
while (i > 0) {
i--;
if (x86_cpu_freq[i] <= freq) {
mul = freq - x86_cpu_freq[i];
if (mul < delta) {
fixed = x86_cpu_freq[i];
delta = mul;
}
break;
}
mul = x86_cpu_freq[i] - freq;
if (mul < delta) {
fixed = x86_cpu_freq[i];
delta = mul;
}
}
if (6 < delta)
return;
cpu_freq = (int)fixed;
}
static int
machhztomhz(uint64_t cpu_freq_hz)
{
uint64_t cpu_mhz;
cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
if (cpu_mhz > INT_MAX)
return (0);
return ((int)cpu_mhz);
}
static int
mach_clkinit(int preferred_mode, int *set_mode)
{
struct psm_ops *pops;
int resolution;
pops = mach_set[0];
cpu_freq_hz = mach_getcpufreq();
cpu_freq = machhztomhz(cpu_freq_hz);
#ifdef __xpv
if (pops->psm_hrtimeinit)
(*pops->psm_hrtimeinit)();
gethrtimef = pops->psm_gethrtime;
gethrtimeunscaledf = gethrtimef;
#endif
mach_fixcpufreq();
if (mach_ver[0] >= PSM_INFO_VER01_3) {
if (preferred_mode == TIMER_ONESHOT) {
resolution = (*pops->psm_clkinit)(0);
if (resolution != 0) {
*set_mode = TIMER_ONESHOT;
return (resolution);
}
}
resolution = (*pops->psm_clkinit)(hz);
*set_mode = TIMER_PERIODIC;
return (resolution);
} else {
(void) (*pops->psm_clkinit)(hz);
*set_mode = TIMER_PERIODIC;
return (nsec_per_tick);
}
}
static int
mach_softlvl_to_vect(int ipl)
{
setsoftint = av_set_softint_pending;
kdisetsoftint = kdi_av_set_softint_pending;
return (PSM_SV_SOFTWARE);
}
#ifdef DEBUG
cpuset_t cpufailset;
#endif
int
mach_cpu_start(struct cpu *cp, void *ctx)
{
struct psm_ops *pops = mach_set[0];
processorid_t id = cp->cpu_id;
#ifdef DEBUG
if (CPU_IN_SET(cpufailset, id))
return (0);
#endif
return ((*pops->psm_cpu_start)(id, ctx));
}
int
mach_cpuid_start(processorid_t id, void *ctx)
{
struct psm_ops *pops = mach_set[0];
#ifdef DEBUG
if (CPU_IN_SET(cpufailset, id))
return (0);
#endif
return ((*pops->psm_cpu_start)(id, ctx));
}
int
mach_cpu_stop(cpu_t *cp, void *ctx)
{
struct psm_ops *pops = mach_set[0];
psm_cpu_request_t request;
if (pops->psm_cpu_ops == NULL) {
return (ENOTSUP);
}
ASSERT(cp->cpu_id != -1);
request.pcr_cmd = PSM_CPU_STOP;
request.req.cpu_stop.cpuid = cp->cpu_id;
request.req.cpu_stop.ctx = ctx;
return ((*pops->psm_cpu_ops)(&request));
}
int
mach_cpu_add(mach_cpu_add_arg_t *argp, processorid_t *cpuidp)
{
int rc;
struct psm_ops *pops = mach_set[0];
psm_cpu_request_t request;
if (pops->psm_cpu_ops == NULL) {
return (ENOTSUP);
}
request.pcr_cmd = PSM_CPU_ADD;
request.req.cpu_add.argp = argp;
request.req.cpu_add.cpuid = -1;
rc = (*pops->psm_cpu_ops)(&request);
if (rc == 0) {
ASSERT(request.req.cpu_add.cpuid != -1);
*cpuidp = request.req.cpu_add.cpuid;
}
return (rc);
}
int
mach_cpu_remove(processorid_t cpuid)
{
struct psm_ops *pops = mach_set[0];
psm_cpu_request_t request;
if (pops->psm_cpu_ops == NULL) {
return (ENOTSUP);
}
request.pcr_cmd = PSM_CPU_REMOVE;
request.req.cpu_remove.cpuid = cpuid;
return ((*pops->psm_cpu_ops)(&request));
}
static int
mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp)
{
int rv;
dev_info_t *dip;
static kmutex_t cpu_node_lock;
static dev_info_t *cpu_nex_devi = NULL;
ASSERT(cp != NULL);
ASSERT(dipp != NULL);
*dipp = NULL;
if (cpu_nex_devi == NULL) {
mutex_enter(&cpu_node_lock);
cpu_nex_devi = ddi_find_devinfo("cpus", -1, 0);
if (cpu_nex_devi == NULL) {
ndi_devi_enter(ddi_root_node());
rv = ndi_devi_alloc(ddi_root_node(), "cpus",
(pnode_t)DEVI_SID_NODEID, &dip);
if (rv != NDI_SUCCESS) {
mutex_exit(&cpu_node_lock);
cmn_err(CE_CONT,
"?failed to create cpu nexus device.\n");
return (PSM_FAILURE);
}
ASSERT(dip != NULL);
(void) ndi_devi_online(dip, 0);
ndi_devi_exit(ddi_root_node());
cpu_nex_devi = dip;
}
mutex_exit(&cpu_node_lock);
}
ndi_devi_enter(cpu_nex_devi);
dip = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID, -1);
if (dip == NULL) {
cmn_err(CE_CONT,
"?failed to create device node for cpu%d.\n", cp->cpu_id);
rv = PSM_FAILURE;
} else {
*dipp = dip;
(void) ndi_hold_devi(dip);
rv = PSM_SUCCESS;
}
ndi_devi_exit(cpu_nex_devi);
return (rv);
}
int
mach_cpu_create_device_node(struct cpu *cp, dev_info_t **dipp)
{
int rv;
dev_info_t *dip = NULL;
ASSERT(psm_cpu_create_devinfo != NULL);
rv = psm_cpu_create_devinfo(cp, &dip);
if (rv == PSM_SUCCESS) {
cpuid_set_cpu_properties(dip, cp->cpu_id, cp->cpu_m.mcpu_cpi);
if (i_ddi_attach_node_hierarchy(ddi_get_parent(dip)) ==
DDI_SUCCESS) {
(void) ndi_devi_online(dip,
NDI_ONLINE_ATTACH | NDI_CONFIG);
}
if (dipp != NULL) {
*dipp = dip;
} else {
(void) ndi_rele_devi(dip);
}
}
return (rv);
}
int
mach_cpu_get_device_node(struct cpu *cp, dev_info_t **dipp)
{
*dipp = NULL;
if (psm_cpu_get_devinfo != NULL) {
if (psm_cpu_get_devinfo(cp, dipp) == PSM_SUCCESS) {
return (PSM_SUCCESS);
}
}
return (PSM_FAILURE);
}
static int
mach_translate_irq(dev_info_t *dip, int irqno)
{
return (irqno);
}
static void
mach_notify_error(int level, char *errmsg)
{
if (level & SL_FATAL)
(*notify_error)(CE_PANIC, errmsg);
else if (level & SL_WARN)
(*notify_error)(CE_WARN, errmsg);
else if (level & SL_NOTE)
(*notify_error)(CE_NOTE, errmsg);
else if (level & SL_CONSOLE)
(*notify_error)(CE_CONT, errmsg);
}
static int
mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
psm_intr_op_t intr_op, int *result)
{
struct intrspec *ispec;
switch (intr_op) {
case PSM_INTR_OP_CHECK_MSI:
*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
DDI_INTR_TYPE_MSIX);
break;
case PSM_INTR_OP_ALLOC_VECTORS:
if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
*result = 1;
else
*result = 0;
break;
case PSM_INTR_OP_FREE_VECTORS:
break;
case PSM_INTR_OP_NAVAIL_VECTORS:
if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
*result = 1;
else
*result = 0;
break;
case PSM_INTR_OP_XLATE_VECTOR:
ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
*result = psm_translate_irq(dip, ispec->intrspec_vec);
break;
case PSM_INTR_OP_GET_CAP:
*result = 0;
break;
case PSM_INTR_OP_GET_PENDING:
case PSM_INTR_OP_CLEAR_MASK:
case PSM_INTR_OP_SET_MASK:
case PSM_INTR_OP_GET_SHARED:
case PSM_INTR_OP_SET_PRI:
case PSM_INTR_OP_SET_CAP:
case PSM_INTR_OP_SET_CPU:
case PSM_INTR_OP_GET_INTR:
default:
return (PSM_FAILURE);
}
return (PSM_SUCCESS);
}
int
pg_cmt_load_bal_hw(pghw_type_t hw)
{
if (hw == PGHW_IPIPE ||
hw == PGHW_FPU ||
hw == PGHW_PROCNODE ||
hw == PGHW_CHIP)
return (1);
else
return (0);
}
int
pg_cmt_affinity_hw(pghw_type_t hw)
{
if (hw == PGHW_CACHE)
return (1);
else
return (0);
}
int
cu_plat_cpc_init(cpu_t *cp, kcpc_request_list_t *reqs, int nreqs)
{
const char *impl_name;
if (pcbe_ops == NULL)
return (-1);
impl_name = pcbe_ops->pcbe_impl_name();
if (impl_name != NULL && strcmp(impl_name, PCBE_IMPL_NAME_P4HT) == 0)
return (0);
else
return (-1);
}