#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/timetc.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_extern.h>
#include <vm/vm_kern.h>
#include <dev/hyperv/include/hyperv.h>
#include <dev/hyperv/include/hyperv_busdma.h>
#include <dev/hyperv/vmbus/hyperv_var.h>
#include <dev/hyperv/vmbus/x86/hyperv_machdep.h>
#include <dev/hyperv/vmbus/x86/hyperv_reg.h>
#include <dev/hyperv/vmbus/hyperv_common_reg.h>
#define HYPERV_FREEBSD_BUILD 0ULL
#define HYPERV_FREEBSD_VERSION ((uint64_t)__FreeBSD_version)
#define HYPERV_FREEBSD_OSID 0ULL
void hyperv_init_tc(void);
int hypercall_page_setup(vm_paddr_t);
void hypercall_disable(void);
bool hyperv_identify_features(void);
u_int hyperv_ver_major;
u_int hyperv_features;
u_int hyperv_recommends;
hyperv_tc64_t hyperv_tc64;
static u_int hyperv_pm_features;
static u_int hyperv_features3;
static u_int hyperv_get_timecount(struct timecounter *);
static struct timecounter hyperv_timecounter = {
.tc_get_timecount = hyperv_get_timecount,
.tc_poll_pps = NULL,
.tc_counter_mask = 0xffffffff,
.tc_frequency = HYPERV_TIMER_FREQ,
.tc_name = "Hyper-V",
.tc_quality = 2000,
.tc_flags = 0,
.tc_priv = NULL
};
static u_int
hyperv_get_timecount(struct timecounter *tc __unused)
{
return rdmsr(MSR_HV_TIME_REF_COUNT);
}
static uint64_t
hyperv_tc64_rdmsr(void)
{
return (rdmsr(MSR_HV_TIME_REF_COUNT));
}
void
hyperv_init_tc(void)
{
if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
tc_init(&hyperv_timecounter);
hyperv_tc64 = hyperv_tc64_rdmsr;
}
}
int
hypercall_page_setup(vm_paddr_t paddr)
{
uint64_t hc, hc_orig;
hc_orig = rdmsr(MSR_HV_HYPERCALL);
hc = ((paddr >> PAGE_SHIFT) << MSR_HV_HYPERCALL_PGSHIFT) |
(hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) | MSR_HV_HYPERCALL_ENABLE;
wrmsr(MSR_HV_HYPERCALL, hc);
hc = rdmsr(MSR_HV_HYPERCALL);
if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) {
printf("hyperv: Hypercall setup failed\n");
vm_guest = VM_GUEST_VM;
return (-1);
}
return (0);
}
void
hypercall_disable(void)
{
uint64_t hc;
hc = rdmsr(MSR_HV_HYPERCALL);
wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK));
}
bool
hyperv_identify_features(void)
{
u_int regs[4];
unsigned int maxleaf;
if (vm_guest != VM_GUEST_HV)
return (false);
do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
maxleaf = regs[0];
if (maxleaf < CPUID_LEAF_HV_LIMITS)
return (false);
do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
if (regs[0] != CPUID_HV_IFACE_HYPERV)
return (false);
do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) {
return (false);
}
hyperv_features = regs[0];
hyperv_pm_features = regs[2];
hyperv_features3 = regs[3];
do_cpuid(CPUID_LEAF_HV_IDENTITY, regs);
hyperv_ver_major = regs[1] >> 16;
printf("Hyper-V Version: %d.%d.%d [SP%d]\n", hyperv_ver_major,
regs[1] & 0xffff, regs[0], regs[2]);
printf(" Features=0x%b\n", hyperv_features,
"\020"
"\001VPRUNTIME"
"\002TMREFCNT"
"\003SYNIC"
"\004SYNTM"
"\005APIC"
"\006HYPERCALL"
"\007VPINDEX"
"\010RESET"
"\011STATS"
"\012REFTSC"
"\013IDLE"
"\014TMFREQ"
"\015DEBUG");
printf(" PM Features=0x%b [C%u]\n",
(hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK),
"\020"
"\005C3HPET",
CPUPM_HV_CSTATE(hyperv_pm_features));
printf(" Features3=0x%b\n", hyperv_features3,
"\020"
"\001MWAIT"
"\002DEBUG"
"\003PERFMON"
"\004PCPUDPE"
"\005XMMHC"
"\006IDLE"
"\007SLEEP"
"\010NUMA"
"\011TMFREQ"
"\012SYNCMC"
"\013CRASH"
"\014DEBUGMSR"
"\015NPIEP"
"\016HVDIS");
do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs);
hyperv_recommends = regs[0];
if (bootverbose)
printf(" Recommends: %08x %08x\n", regs[0], regs[1]);
do_cpuid(CPUID_LEAF_HV_LIMITS, regs);
if (bootverbose) {
printf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n", regs[0], regs[1],
regs[2]);
}
if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) {
do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs);
if (bootverbose) {
printf(" HW Features: %08x, AMD: %08x\n", regs[0],
regs[3]);
}
}
return (true);
}