9ae3a8
From b0ac5450a5fcb7e0c9cd5baf511283af375cc18f Mon Sep 17 00:00:00 2001
9ae3a8
From: Vadim Rozenfeld <vrozenfe@redhat.com>
9ae3a8
Date: Mon, 3 Mar 2014 12:09:24 +0100
9ae3a8
Subject: [PATCH 12/12] kvm: add support for hyper-v timers
9ae3a8
9ae3a8
RH-Author: Vadim Rozenfeld <vrozenfe@redhat.com>
9ae3a8
Message-id: <1393848564-10511-13-git-send-email-vrozenfe@redhat.com>
9ae3a8
Patchwork-id: 57968
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm v4 PATCH 12/12] kvm: add support for hyper-v timers
9ae3a8
Bugzilla: 1057173
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
9ae3a8
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
RH-Acked-by: Juan Quintela <quintela@redhat.com>
9ae3a8
9ae3a8
http://msdn.microsoft.com/en-us/library/windows/hardware/ff541625%28v=vs.85%29.aspx
9ae3a8
9ae3a8
This code is generic for activating reference time counter or virtual reference time stamp counter
9ae3a8
9ae3a8
Signed-off-by: Vadim Rozenfeld <vrozenfe@redhat.com>
9ae3a8
Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
(cherry picked from commit 48a5f3bcbbbe59a3120a39106bfda59fd1933fbc)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	linux-headers/asm-x86/hyperv.h
9ae3a8
	linux-headers/linux/kvm.h
9ae3a8
	target-i386/kvm.c
9ae3a8
---
9ae3a8
 linux-headers/asm-x86/hyperv.h |  3 +++
9ae3a8
 linux-headers/linux/kvm.h      |  1 +
9ae3a8
 target-i386/cpu-qom.h          |  1 +
9ae3a8
 target-i386/cpu.c              |  1 +
9ae3a8
 target-i386/cpu.h              |  1 +
9ae3a8
 target-i386/kvm.c              | 20 +++++++++++++++++++-
9ae3a8
 target-i386/machine.c          | 22 ++++++++++++++++++++++
9ae3a8
 7 files changed, 48 insertions(+), 1 deletion(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 linux-headers/asm-x86/hyperv.h |    3 +++
9ae3a8
 linux-headers/linux/kvm.h      |    1 +
9ae3a8
 target-i386/cpu-qom.h          |    1 +
9ae3a8
 target-i386/cpu.c              |    1 +
9ae3a8
 target-i386/cpu.h              |    1 +
9ae3a8
 target-i386/kvm.c              |   20 +++++++++++++++++++-
9ae3a8
 target-i386/machine.c          |   22 ++++++++++++++++++++++
9ae3a8
 7 files changed, 48 insertions(+), 1 deletions(-)
9ae3a8
9ae3a8
diff --git a/linux-headers/asm-x86/hyperv.h b/linux-headers/asm-x86/hyperv.h
9ae3a8
index b80420b..9711819 100644
9ae3a8
--- a/linux-headers/asm-x86/hyperv.h
9ae3a8
+++ b/linux-headers/asm-x86/hyperv.h
9ae3a8
@@ -136,6 +136,9 @@
9ae3a8
 /* MSR used to read the per-partition time reference counter */
9ae3a8
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
9ae3a8
 
9ae3a8
+/* A partition's reference time stamp counter (TSC) page */
9ae3a8
+#define HV_X64_MSR_REFERENCE_TSC		0x40000021
9ae3a8
+
9ae3a8
 /* Define the virtual APIC registers */
9ae3a8
 #define HV_X64_MSR_EOI				0x40000070
9ae3a8
 #define HV_X64_MSR_ICR				0x40000071
9ae3a8
diff --git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h
9ae3a8
index e32ef4a..4b93099 100644
9ae3a8
--- a/linux-headers/linux/kvm.h
9ae3a8
+++ b/linux-headers/linux/kvm.h
9ae3a8
@@ -666,6 +666,7 @@ struct kvm_ppc_smmu_info {
9ae3a8
 #define KVM_CAP_IRQ_MPIC 90
9ae3a8
 #define KVM_CAP_PPC_RTAS 91
9ae3a8
 #define KVM_CAP_IRQ_XICS 92
9ae3a8
+#define KVM_CAP_HYPERV_TIME 96
9ae3a8
 
9ae3a8
 #ifdef KVM_CAP_IRQ_ROUTING
9ae3a8
 
9ae3a8
diff --git a/target-i386/cpu-qom.h b/target-i386/cpu-qom.h
9ae3a8
index 99ba65f..0b01e8f 100644
9ae3a8
--- a/target-i386/cpu-qom.h
9ae3a8
+++ b/target-i386/cpu-qom.h
9ae3a8
@@ -69,6 +69,7 @@ typedef struct X86CPU {
9ae3a8
     bool hyperv_vapic;
9ae3a8
     bool hyperv_relaxed_timing;
9ae3a8
     int hyperv_spinlock_attempts;
9ae3a8
+    bool hyperv_time;
9ae3a8
     bool check_cpuid;
9ae3a8
     bool enforce_cpuid;
9ae3a8
 
9ae3a8
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
9ae3a8
index 8421dfc..005e190 100644
9ae3a8
--- a/target-i386/cpu.c
9ae3a8
+++ b/target-i386/cpu.c
9ae3a8
@@ -2621,6 +2621,7 @@ static Property x86_cpu_properties[] = {
9ae3a8
     { .name  = "hv-spinlocks", .info  = &qdev_prop_spinlocks },
9ae3a8
     DEFINE_PROP_BOOL("hv-relaxed", X86CPU, hyperv_relaxed_timing, false),
9ae3a8
     DEFINE_PROP_BOOL("hv-vapic", X86CPU, hyperv_vapic, false),
9ae3a8
+    DEFINE_PROP_BOOL("hv-time", X86CPU, hyperv_time, false),
9ae3a8
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, false),
9ae3a8
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
9ae3a8
     DEFINE_PROP_END_OF_LIST()
9ae3a8
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
9ae3a8
index 0773499..ad40aab 100644
9ae3a8
--- a/target-i386/cpu.h
9ae3a8
+++ b/target-i386/cpu.h
9ae3a8
@@ -847,6 +847,7 @@ typedef struct CPUX86State {
9ae3a8
     uint64_t msr_hv_hypercall;
9ae3a8
     uint64_t msr_hv_guest_os_id;
9ae3a8
     uint64_t msr_hv_vapic;
9ae3a8
+    uint64_t msr_hv_tsc;
9ae3a8
 
9ae3a8
     /* exception/interrupt handling */
9ae3a8
     int error_code;
9ae3a8
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
9ae3a8
index a2946c4..ba2cd7f 100644
9ae3a8
--- a/target-i386/kvm.c
9ae3a8
+++ b/target-i386/kvm.c
9ae3a8
@@ -72,6 +72,7 @@ static bool has_msr_kvm_steal_time;
9ae3a8
 static int lm_capable_kernel;
9ae3a8
 static bool has_msr_hv_hypercall;
9ae3a8
 static bool has_msr_hv_vapic;
9ae3a8
+static bool has_msr_hv_tsc;
9ae3a8
 
9ae3a8
 static bool has_msr_architectural_pmu;
9ae3a8
 static uint32_t num_architectural_pmu_counters;
9ae3a8
@@ -438,6 +439,7 @@ static bool hyperv_enabled(X86CPU *cpu)
9ae3a8
     CPUState *cs = CPU(cpu);
9ae3a8
     return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 &&
9ae3a8
            (hyperv_hypercall_available(cpu) ||
9ae3a8
+            cpu->hyperv_time  ||
9ae3a8
             cpu->hyperv_relaxed_timing);
9ae3a8
 }
9ae3a8
 
9ae3a8
@@ -495,7 +497,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
9ae3a8
             c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
9ae3a8
             has_msr_hv_vapic = true;
9ae3a8
         }
9ae3a8
-
9ae3a8
+        if (cpu->hyperv_time &&
9ae3a8
+            kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
9ae3a8
+            c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
9ae3a8
+            c->eax |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE;
9ae3a8
+            c->eax |= 0x200;
9ae3a8
+            has_msr_hv_tsc = true;
9ae3a8
+        }
9ae3a8
         c = &cpuid_data.entries[cpuid_i++];
9ae3a8
         c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
9ae3a8
         if (cpu->hyperv_relaxed_timing) {
9ae3a8
@@ -1207,6 +1215,10 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
9ae3a8
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE,
9ae3a8
                               env->msr_hv_vapic);
9ae3a8
         }
9ae3a8
+        if (has_msr_hv_tsc) {
9ae3a8
+            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC,
9ae3a8
+                              env->msr_hv_tsc);
9ae3a8
+        }
9ae3a8
     }
9ae3a8
     if (env->mcg_cap) {
9ae3a8
         int i;
9ae3a8
@@ -1485,6 +1497,9 @@ static int kvm_get_msrs(X86CPU *cpu)
9ae3a8
     if (has_msr_hv_vapic) {
9ae3a8
         msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE;
9ae3a8
     }
9ae3a8
+    if (has_msr_hv_tsc) {
9ae3a8
+        msrs[n++].index = HV_X64_MSR_REFERENCE_TSC;
9ae3a8
+    }
9ae3a8
 
9ae3a8
     msr_data.info.nmsrs = n;
9ae3a8
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
9ae3a8
@@ -1596,6 +1611,9 @@ static int kvm_get_msrs(X86CPU *cpu)
9ae3a8
         case HV_X64_MSR_APIC_ASSIST_PAGE:
9ae3a8
             env->msr_hv_vapic = msrs[i].data;
9ae3a8
             break;
9ae3a8
+        case HV_X64_MSR_REFERENCE_TSC:
9ae3a8
+            env->msr_hv_tsc = msrs[i].data;
9ae3a8
+            break;
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
diff --git a/target-i386/machine.c b/target-i386/machine.c
9ae3a8
index 021390b..42b049b 100644
9ae3a8
--- a/target-i386/machine.c
9ae3a8
+++ b/target-i386/machine.c
9ae3a8
@@ -545,6 +545,25 @@ static const VMStateDescription vmstate_msr_hyperv_vapic = {
9ae3a8
     }
9ae3a8
 };
9ae3a8
 
9ae3a8
+static bool hyperv_time_enable_needed(void *opaque)
9ae3a8
+{
9ae3a8
+    X86CPU *cpu = opaque;
9ae3a8
+    CPUX86State *env = &cpu->env;
9ae3a8
+
9ae3a8
+    return env->msr_hv_tsc != 0;
9ae3a8
+}
9ae3a8
+
9ae3a8
+static const VMStateDescription vmstate_msr_hyperv_time = {
9ae3a8
+    .name = "cpu/msr_hyperv_time",
9ae3a8
+    .version_id = 1,
9ae3a8
+    .minimum_version_id = 1,
9ae3a8
+    .minimum_version_id_old = 1,
9ae3a8
+    .fields      = (VMStateField []) {
9ae3a8
+        VMSTATE_UINT64(env.msr_hv_tsc, X86CPU),
9ae3a8
+        VMSTATE_END_OF_LIST()
9ae3a8
+    }
9ae3a8
+};
9ae3a8
+
9ae3a8
 const VMStateDescription vmstate_x86_cpu = {
9ae3a8
     .name = "cpu",
9ae3a8
     .version_id = 12,
9ae3a8
@@ -682,6 +701,9 @@ const VMStateDescription vmstate_x86_cpu = {
9ae3a8
         }, {
9ae3a8
             .vmsd = &vmstate_msr_hyperv_vapic,
9ae3a8
             .needed = hyperv_vapic_enable_needed,
9ae3a8
+        }, {
9ae3a8
+            .vmsd = &vmstate_msr_hyperv_time,
9ae3a8
+            .needed = hyperv_time_enable_needed,
9ae3a8
         } , {
9ae3a8
             /* empty */
9ae3a8
         }
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8