Skip to contentSkip to conversation

X86: Hyper-V: Get the local APIC timer frequency from the hypervisor

PATCH Superseded K. Y. Srinivasan 2 participants 3 messages Aug 26, 2013 – Aug 27, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 X86: Hyper-V: Get the local APIC timer frequency from the hypervisor

Hyper-V supports a mechanism for retrieving the local API frequency.Use this and bypass the calibration code in the kernel. This would allow us to boot the Linux kernel as a "modern VM" on Hyper-V where many of the legacy devices (such as PIT) are not emulated.

I would like to thank Olaf Hering <[email protected]>, Jan Beulich <[email protected]> and H. Peter Anvin <[email protected]> for their help in this effort.

---
 arch/x86/include/uapi/asm/hyperv.h |   13 +++++++++++++
 arch/x86/kernel/cpu/mshyperv.c     |   17 +++++++++++++++++
 2 files changed, 30 insertions(+), 0 deletions(-)
arch/x86/include/uapi/asm/hyperv.h +13 -0
index b80420b..df3d680 100644
@@ -27,6 +27,13 @@
 #define HV_X64_MSR_VP_RUNTIME_AVAILABLE		(1 << 0)
 /* Partition Reference Counter (HV_X64_MSR_TIME_REF_COUNT) available*/
 #define HV_X64_MSR_TIME_REF_COUNT_AVAILABLE	(1 << 1)
+
+/* Local APIC timer frequency MSR (HV_X64_MSR_APIC_FREQUENCY) is available */
+#define HV_X64_MSR_APIC_FREQUENCY_AVAILABLE (1 << 11)
+
+/* TSC frequency MSR (HV_X64_MSR_TSC_FREQUENCY) is available */
+#define HV_X64_MSR_TSC_FREQUENCY_AVAILABLE (1 << 11)
+
 /*
  * Basic SynIC MSRs (HV_X64_MSR_SCONTROL through HV_X64_MSR_EOM
  * and HV_X64_MSR_SINT0 through HV_X64_MSR_SINT15) available
@@ -136,6 +143,12 @@
 /* MSR used to read the per-partition time reference counter */
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
 
+/* MSR used to retrive the TSC frequency */
+#define HV_X64_MSR_TSC_FREQUENCY		0x40000022
+
+/* MSR used to retrive the local APIC timer frequency */
+#define HV_X64_MSR_APIC_FREQUENCY		0x40000023
+
 /* Define the virtual APIC registers */
 #define HV_X64_MSR_EOI				0x40000070
 #define HV_X64_MSR_ICR				0x40000071
arch/x86/kernel/cpu/mshyperv.c +17 -0
index 71a39f3..0593fd9 100644
@@ -23,6 +23,7 @@
 #include <asm/desc.h>
 #include <asm/idle.h>
 #include <asm/irq_regs.h>
+#include <asm/i8259.h>
 
 struct ms_hyperv_info ms_hyperv;
 EXPORT_SYMBOL_GPL(ms_hyperv);
@@ -76,6 +77,22 @@ static void __init ms_hyperv_init_platform(void)
 	printk(KERN_INFO "HyperV: features 0x%x, hints 0x%x\n",
 	       ms_hyperv.features, ms_hyperv.hints);
 
+	if (ms_hyperv.features & HV_X64_MSR_APIC_FREQUENCY_AVAILABLE) {
+		/*
+		 * There is no need to calibrate APIC timer frequency;
+		 * nor is there a need to calibrate timer.
+		 */
+		legacy_pic = &null_legacy_pic;
+
+		/*
+		 * Get the APIC frequency.
+		 */
+		rdmsrl(HV_X64_MSR_APIC_FREQUENCY, lapic_timer_frequency);
+		lapic_timer_frequency /= HZ;
+		printk(KERN_INFO "HyperV: LAPIC Timer Frequency: 0x%x\n",
+				lapic_timer_frequency);
+	}
+
 	if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
 		clocksource_register_hz(&hyperv_cs, NSEC_PER_SEC/100);
 }
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 2 lines
Quoted 1 line
Quoted 1 line · K. Y. Srinivasan

Hyper-V supports a mechanism for retrieving the local API frequency.

"APIC"?

arch/x86/include/uapi/asm/hyperv.h line 35

Quoted 10 lines
arch/x86/include/uapi/asm/hyperv.h +6 -0
@@ -27,6 +27,13 @@
 #define HV_X64_MSR_VP_RUNTIME_AVAILABLE		(1 << 0)
 /* Partition Reference Counter (HV_X64_MSR_TIME_REF_COUNT) available*/
 #define HV_X64_MSR_TIME_REF_COUNT_AVAILABLE	(1 << 1)
+
+/* Local APIC timer frequency MSR (HV_X64_MSR_APIC_FREQUENCY) is available */
+#define HV_X64_MSR_APIC_FREQUENCY_AVAILABLE (1 << 11)
+
+/* TSC frequency MSR (HV_X64_MSR_TSC_FREQUENCY) is available */
+#define HV_X64_MSR_TSC_FREQUENCY_AVAILABLE (1 << 11)

Are these two really the same bit? If so, why two different names?

arch/x86/include/uapi/asm/hyperv.h line 150

Quoted 9 lines
arch/x86/include/uapi/asm/hyperv.h +5 -0
@@ -136,6 +143,12 @@
 /* MSR used to read the per-partition time reference counter */
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
 
+/* MSR used to retrive the TSC frequency */
+#define HV_X64_MSR_TSC_FREQUENCY		0x40000022
+
+/* MSR used to retrive the local APIC timer frequency */
+#define HV_X64_MSR_APIC_FREQUENCY		0x40000023

"retrieve" (twice)?

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 85

Quoted 10 lines
arch/x86/kernel/cpu/mshyperv.c +6 -0
@@ -76,6 +77,22 @@ static void __init ms_hyperv_init_platform(void)
 	printk(KERN_INFO "HyperV: features 0x%x, hints 0x%x\n",
 	       ms_hyperv.features, ms_hyperv.hints);
 
+	if (ms_hyperv.features & HV_X64_MSR_APIC_FREQUENCY_AVAILABLE) {
+		/*
+		 * There is no need to calibrate APIC timer frequency;
+		 * nor is there a need to calibrate timer.
+		 */
+		legacy_pic = &null_legacy_pic;

This clearly disables more than just the calibration, so the comment may be misleading to future readers.

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 91

Quoted 6 lines
arch/x86/kernel/cpu/mshyperv.c +6 -0
+
+		/*
+		 * Get the APIC frequency.
+		 */
+		rdmsrl(HV_X64_MSR_APIC_FREQUENCY, lapic_timer_frequency);
+		lapic_timer_frequency /= HZ;

Is this safe? I.e. are the high 32 bits of the MSR guaranteed to be zero, now and forever?

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 93

Quoted 2 lines
arch/x86/kernel/cpu/mshyperv.c +2 -0
+		printk(KERN_INFO "HyperV: LAPIC Timer Frequency: 0x%x\n",
+				lapic_timer_frequency);

As a minor note, I generally recommend using %#x as being one byte shorter than the spelled out 0x%x.

Jan

#2 lore raw
Quoted 66 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, August 27, 2013 12:26 AM
To: KY Srinivasan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/1] X86: Hyper-V: Get the local APIC timer frequency from the hypervisor

Quoted 2 lines
Quoted 1 line
Quoted 1 line · K. Y. Srinivasan

Hyper-V supports a mechanism for retrieving the local API frequency.

"APIC"?

arch/x86/include/uapi/asm/hyperv.h line 31

Quoted 6 lines
arch/x86/include/uapi/asm/hyperv.h +2 -0
@@ -27,6 +27,13 @@
 #define HV_X64_MSR_VP_RUNTIME_AVAILABLE		(1 << 0)
 /* Partition Reference Counter (HV_X64_MSR_TIME_REF_COUNT) available*/
 #define HV_X64_MSR_TIME_REF_COUNT_AVAILABLE	(1 << 1)
+
+/* Local APIC timer frequency MSR (HV_X64_MSR_APIC_FREQUENCY) is

available */

arch/x86/include/uapi/asm/hyperv.h line 35

Quoted 4 lines
arch/x86/include/uapi/asm/hyperv.h +4 -0
+#define HV_X64_MSR_APIC_FREQUENCY_AVAILABLE (1 << 11)
+
+/* TSC frequency MSR (HV_X64_MSR_TSC_FREQUENCY) is available */
+#define HV_X64_MSR_TSC_FREQUENCY_AVAILABLE (1 << 11)

Are these two really the same bit? If so, why two different names?

arch/x86/include/uapi/asm/hyperv.h line 150

Quoted 9 lines
arch/x86/include/uapi/asm/hyperv.h +5 -0
@@ -136,6 +143,12 @@
 /* MSR used to read the per-partition time reference counter */
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
 
+/* MSR used to retrive the TSC frequency */
+#define HV_X64_MSR_TSC_FREQUENCY		0x40000022
+
+/* MSR used to retrive the local APIC timer frequency */
+#define HV_X64_MSR_APIC_FREQUENCY		0x40000023

"retrieve" (twice)?

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 85

Quoted 10 lines
arch/x86/kernel/cpu/mshyperv.c +6 -0
@@ -76,6 +77,22 @@ static void __init ms_hyperv_init_platform(void)
 	printk(KERN_INFO "HyperV: features 0x%x, hints 0x%x\n",
 	       ms_hyperv.features, ms_hyperv.hints);
 
+	if (ms_hyperv.features & HV_X64_MSR_APIC_FREQUENCY_AVAILABLE) {
+		/*
+		 * There is no need to calibrate APIC timer frequency;
+		 * nor is there a need to calibrate timer.
+		 */
+		legacy_pic = &null_legacy_pic;

This clearly disables more than just the calibration, so the comment may be misleading to future readers.

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void)

Quoted 5 lines
arch/x86/kernel/cpu/mshyperv.c +5 -0
+
+		/*
+		 * Get the APIC frequency.
+		 */
+		rdmsrl(HV_X64_MSR_APIC_FREQUENCY,

lapic_timer_frequency);

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 91

Quoted 1 line
arch/x86/kernel/cpu/mshyperv.c +1 -0
+		lapic_timer_frequency /= HZ;

Is this safe? I.e. are the high 32 bits of the MSR guaranteed to be zero, now and forever?

Quoted 7 lines

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 93

Quoted 2 lines
arch/x86/kernel/cpu/mshyperv.c +2 -0
+		printk(KERN_INFO "HyperV: LAPIC Timer Frequency: 0x%x\n",
+				lapic_timer_frequency);

As a minor note, I generally recommend using %#x as being one byte shorter than the spelled out 0x%x.

Jan

Thanks Jan. I will fix the issues you have pointed out and resubmit the patch.

Regards,

K. Y