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