#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/clock.h>
#include <sys/x_call.h>
#include <sys/cpuvar.h>
#include <sys/promif.h>
#include <sys/kmem.h>
#include <sys/machsystm.h>
#include <sys/ivintr.h>
#include <sys/cyclic.h>
#include <sys/cyclic_impl.h>
uint64_t cbe_level14_inum;
cyclic_id_t cbe_hres_cyclic;
static hrtime_t cbe_hrtime_max;
static hrtime_t cbe_suspend_delta = 0;
static hrtime_t cbe_suspend_time = 0;
static uint64_t
hrtime2tick(hrtime_t ts)
{
hrtime_t q = ts / NANOSEC;
hrtime_t r = ts - (q * NANOSEC);
return (q * sys_tick_freq + ((r * sys_tick_freq) / NANOSEC));
}
uint64_t
unscalehrtime(hrtime_t ts)
{
uint64_t unscale = 0;
hrtime_t rescale;
hrtime_t diff = ts;
while (diff > nsec_per_sys_tick) {
unscale += hrtime2tick(diff);
rescale = unscale;
scalehrtime(&rescale);
diff = ts - rescale;
}
return (unscale);
}
static int
cbe_level1()
{
cyclic_softint(CPU, CY_LOW_LEVEL);
return (1);
}
static int
cbe_level10()
{
cyclic_softint(CPU, CY_LOCK_LEVEL);
return (1);
}
static void
cbe_enable(cyb_arg_t arg)
{
int pstate_save = disable_vec_intr();
intr_enqueue_req(PIL_14, cbe_level14_inum);
enable_vec_intr(pstate_save);
}
static void
cbe_disable(cyb_arg_t arg)
{
int pstate_save = disable_vec_intr();
tickcmpr_disable();
intr_dequeue_req(PIL_14, cbe_level14_inum);
enable_vec_intr(pstate_save);
}
static void
cbe_reprogram(cyb_arg_t arg, hrtime_t time)
{
if (time >= cbe_hrtime_max)
time = cbe_hrtime_max;
tickcmpr_set(unscalehrtime(time));
}
static void
cbe_softint(cyb_arg_t arg, cyc_level_t level)
{
cbe_data_t *data = (cbe_data_t *)arg;
switch (level) {
case CY_LOW_LEVEL:
setsoftint(data->cbe_level1_inum);
break;
case CY_LOCK_LEVEL:
setsoftint(data->cbe_level10_inum);
break;
default:
panic("cbe_softint: unexpected soft level %d", level);
}
}
static cyc_cookie_t
cbe_set_level(cyb_arg_t arg, cyc_level_t level)
{
int ipl;
switch (level) {
case CY_LOW_LEVEL:
ipl = CBE_LOW_PIL;
break;
case CY_LOCK_LEVEL:
ipl = CBE_LOCK_PIL;
break;
case CY_HIGH_LEVEL:
ipl = CBE_HIGH_PIL;
break;
default:
panic("cbe_set_level: unexpected level %d", level);
}
return (splr(ipl));
}
static void
cbe_restore_level(cyb_arg_t arg, cyc_cookie_t cookie)
{
splx(cookie);
}
static void
cbe_xcall_handler(uint64_t arg1, uint64_t arg2)
{
cyc_func_t func = (cyc_func_t)arg1;
void *arg = (void *)arg2;
(*func)(arg);
}
static void
cbe_xcall(cyb_arg_t arg, cpu_t *dest, cyc_func_t func, void *farg)
{
kpreempt_disable();
xc_one(dest->cpu_id, cbe_xcall_handler, (uint64_t)func, (uint64_t)farg);
kpreempt_enable();
}
static cyb_arg_t
cbe_configure(cpu_t *cpu)
{
cbe_data_t *new_data = kmem_alloc(sizeof (cbe_data_t), KM_SLEEP);
new_data->cbe_level10_inum = add_softintr(PIL_10,
(softintrfunc)cbe_level10, 0, SOFTINT_ST);
new_data->cbe_level1_inum = add_softintr(PIL_1,
(softintrfunc)cbe_level1, 0, SOFTINT_ST);
return (new_data);
}
static void
cbe_unconfigure(cyb_arg_t arg)
{
cbe_data_t *data = (cbe_data_t *)arg;
(void) rem_softintr(data->cbe_level10_inum);
(void) rem_softintr(data->cbe_level1_inum);
kmem_free(data, sizeof (cbe_data_t));
}
static void
cbe_suspend(cyb_arg_t arg)
{
cbe_suspend_time = gethrtime_unscaled();
cbe_suspend_delta = 0;
}
static void
cbe_resume(cyb_arg_t arg)
{
hrtime_t now;
if ((now = gethrtime_unscaled()) < cbe_suspend_time) {
if (cbe_suspend_delta == 0) {
cbe_suspend_delta = cbe_suspend_time - now;
}
tick_write_delta(cbe_suspend_delta);
}
}
void
cbe_hres_tick(void)
{
dtrace_hres_tick();
hres_tick();
}
void
cbe_init_pre(void)
{
}
void
cbe_init(void)
{
cyc_handler_t hdlr;
cyc_time_t when;
hrtime_t resolution = NANOSEC / sys_tick_freq;
cyc_backend_t cbe = {
cbe_configure,
cbe_unconfigure,
cbe_enable,
cbe_disable,
cbe_reprogram,
cbe_softint,
cbe_set_level,
cbe_restore_level,
cbe_xcall,
cbe_suspend,
cbe_resume
};
cbe_level14_inum = add_softintr(CBE_HIGH_PIL,
(softintrfunc)cbe_level14, 0, SOFTINT_MT);
cbe_hrtime_max = gethrtime_max();
if (resolution == 0)
resolution = 1;
mutex_enter(&cpu_lock);
cyclic_init(&cbe, resolution);
hrtime_base = gethrtime();
hres_last_tick = gethrtime_unscaled();
hdlr.cyh_level = CY_HIGH_LEVEL;
hdlr.cyh_func = (cyc_func_t)cbe_hres_tick;
hdlr.cyh_arg = NULL;
when.cyt_when = 0;
when.cyt_interval = nsec_per_tick;
cbe_hres_cyclic = cyclic_add(&hdlr, &when);
mutex_exit(&cpu_lock);
clkstart();
}